/*
- * "$Id: template.c 4921 2006-01-12 21:26:26Z mike $"
+ * "$Id: template.c 5548 2006-05-19 19:38:31Z mike $"
*
* CGI template function.
*
*/
#include "cgi-private.h"
+#include <errno.h>
/*
* Local functions...
*/
-static void cgi_copy(FILE *out, FILE *in, int element, char term);
+static void cgi_copy(FILE *out, FILE *in, int element, char term,
+ int indent);
static void cgi_puts(const char *s, FILE *out);
+static void cgi_puturi(const char *s, FILE *out);
/*
FILE *in; /* Input file */
+ fprintf(stderr, "DEBUG: cgiCopyTemplateFile(out=%p, tmpl=\"%s\")\n", out,
+ tmpl ? tmpl : "(null)");
+
/*
* Open the template file...
*/
if ((in = fopen(tmpl, "r")) == NULL)
+ {
+ fprintf(stderr, "ERROR: Unable to open template file \"%s\" - %s\n",
+ tmpl ? tmpl : "(null)", strerror(errno));
return;
+ }
/*
* Parse the file to the end...
*/
- cgi_copy(out, in, 0, 0);
+ cgi_copy(out, in, 0, 0, 0);
/*
* Close the template file and return...
FILE *in; /* Input file */
+ fprintf(stderr, "DEBUG: cgiCopyTemplateLang(tmpl=\"%s\")\n",
+ tmpl ? tmpl : "(null)");
+
/*
* Convert the language to a locale name...
*/
if ((lang = getenv("LANG")) != NULL)
{
for (i = 0; lang[i] && i < 15; i ++)
- if (isalnum(lang[i] & 255))
+ if (isalnum(lang[i] & 255) || lang[i] == '_')
locale[i] = tolower(lang[i]);
- else
+ else if (lang[i] == '-')
locale[i] = '_';
+ else
+ break;
locale[i] = '\0';
}
else
locale[0] = '\0';
+ fprintf(stderr, "DEBUG: locale=\"%s\"...\n", locale);
+
/*
* See if we have a template file for this language...
*/
snprintf(filename, sizeof(filename), "%s/%s", directory, tmpl);
}
+ fprintf(stderr, "DEBUG: Template file is \"%s\"...\n", filename);
+
/*
* Open the template file...
*/
if ((in = fopen(filename, "r")) == NULL)
+ {
+ fprintf(stderr, "ERROR: Unable to open template file \"%s\" - %s\n",
+ filename, strerror(errno));
return;
+ }
/*
* Parse the file to the end...
*/
- cgi_copy(stdout, in, 0, 0);
+ cgi_copy(stdout, in, 0, 0, 0);
/*
* Close the template file and return...
*/
static void
-cgi_copy(FILE *out, /* I - Output file */
- FILE *in, /* I - Input file */
- int element, /* I - Element number (0 to N) */
- char term) /* I - Terminating character */
+cgi_copy(FILE *out, /* I - Output file */
+ FILE *in, /* I - Input file */
+ int element, /* I - Element number (0 to N) */
+ char term, /* I - Terminating character */
+ int indent) /* I - Debug info indentation */
{
- int ch; /* Character from file */
- char op; /* Operation */
- char name[255], /* Name of variable */
- *nameptr, /* Pointer into name */
- innername[255], /* Inner comparison name */
- *innerptr, /* Pointer into inner name */
- *s; /* String pointer */
- const char *value; /* Value of variable */
- const char *innerval; /* Inner value */
- const char *outptr; /* Output string pointer */
- char outval[1024], /* Formatted output string */
- compare[1024]; /* Comparison string */
- int result; /* Result of comparison */
-
+ int ch; /* Character from file */
+ char op; /* Operation */
+ char name[255], /* Name of variable */
+ *nameptr, /* Pointer into name */
+ innername[255], /* Inner comparison name */
+ *innerptr, /* Pointer into inner name */
+ *s; /* String pointer */
+ const char *value; /* Value of variable */
+ const char *innerval; /* Inner value */
+ const char *outptr; /* Output string pointer */
+ char outval[1024], /* Formatted output string */
+ compare[1024]; /* Comparison string */
+ int result; /* Result of comparison */
+ int uriencode; /* Encode as URI */
+
+
+ fprintf(stderr, "DEBUG: %*sStarting at file position %ld...\n", indent, "",
+ ftell(in));
/*
* Parse the file to the end...
* Get a variable name...
*/
+ uriencode = 0;
+
for (s = name; (ch = getc(in)) != EOF;)
if (strchr("}]<>=! \t\n", ch))
break;
+ else if (s == name && ch == '%')
+ uriencode = 1;
else if (s > name && ch == '?')
break;
else if (s < (name + sizeof(name) - 1))
if (s == name && isspace(ch & 255))
{
+ fprintf(stderr, "DEBUG: %*sLone { at %ld...\n", indent, "", ftell(in));
+
if (out)
{
putc('{', out);
continue;
}
+ if (ch == '}')
+ fprintf(stderr, "DEBUG: %*s\"{%s}\" at %ld...\n", indent, "", name,
+ ftell(in));
+
/*
* See if it has a value...
*/
pos = ftell(in);
+ fprintf(stderr, "DEBUG: %*sLooping on \"%s\" at %ld, count=%d...\n",
+ indent, "", name + 1, pos, count);
+
if (count > 0)
{
for (i = 0; i < count; i ++)
{
- fseek(in, pos, SEEK_SET);
- cgi_copy(out, in, i, '}');
+ if (i)
+ fseek(in, pos, SEEK_SET);
+
+ cgi_copy(out, in, i, '}', indent + 2);
}
}
else
- cgi_copy(NULL, in, 0, '}');
+ cgi_copy(NULL, in, 0, '}', indent + 2);
+
+ fprintf(stderr, "DEBUG: %*sFinished looping on \"%s\"...\n", indent,
+ "", name + 1);
continue;
}
*/
if (out)
- cgi_puts(outptr, out);
+ {
+ if (uriencode)
+ cgi_puturi(outptr, out);
+ else
+ cgi_puts(outptr, out);
+ }
continue;
}
* {name!value?true:false} Not equal
*/
+ op = ch;
+
if (ch == '?')
{
/*
* Test for existance...
*/
- result = cgiGetArray(name, element) != NULL && outptr[0];
+ result = cgiGetArray(name, element) != NULL && outptr[0];
+ compare[0] = '\0';
}
else
{
* Compare to a string...
*/
- op = ch;
-
for (s = compare; (ch = getc(in)) != EOF;)
if (ch == '?')
break;
*s = '\0';
if (ch != '?')
+ {
+ fprintf(stderr,
+ "DEBUG: %*sBad terminator '%c' at file position %ld...\n",
+ indent, "", ch, ftell(in));
return;
+ }
/*
* Do the comparison...
}
}
+ fprintf(stderr,
+ "DEBUG: %*sStarting \"{%s%c%s\" at %ld, result=%d...\n",
+ indent, "", name, op, compare, ftell(in), result);
+
if (result)
{
/*
* Comparison true; output first part and ignore second...
*/
- cgi_copy(out, in, element, ':');
- cgi_copy(NULL, in, element, '}');
+ fprintf(stderr, "DEBUG: %*sOutput first part...\n", indent, "");
+ cgi_copy(out, in, element, ':', indent + 2);
+
+ fprintf(stderr, "DEBUG: %*sSkip second part...\n", indent, "");
+ cgi_copy(NULL, in, element, '}', indent + 2);
}
else
{
* Comparison false; ignore first part and output second...
*/
- cgi_copy(NULL, in, element, ':');
- cgi_copy(out, in, element, '}');
+ fprintf(stderr, "DEBUG: %*sSkip first part...\n", indent, "");
+ cgi_copy(NULL, in, element, ':', indent + 2);
+
+ fprintf(stderr, "DEBUG: %*sOutput second part...\n", indent, "");
+ cgi_copy(out, in, element, '}', indent + 2);
}
+
+ fprintf(stderr, "DEBUG: %*sFinished \"{%s%c%s\", out=%p...\n", indent, "",
+ name, op, compare, out);
}
else if (ch == '\\') /* Quoted char */
{
else if (out)
putc(ch, out);
+ if (ch == EOF)
+ fprintf(stderr, "DEBUG: %*sReturning at file position %ld on EOF...\n",
+ indent, "", ftell(in));
+ else
+ fprintf(stderr,
+ "DEBUG: %*sReturning at file position %ld on character '%c'...\n",
+ indent, "", ftell(in), ch);
+
+ if (ch == EOF && term)
+ fprintf(stderr, "ERROR: %*sSaw EOF, expected '%c'!\n", indent, "", term);
+
/*
* Flush any pending output...
*/
/*
- * End of "$Id: template.c 4921 2006-01-12 21:26:26Z mike $".
+ * 'cgi_puturi()' - Put a URI string to the output file, quoting as needed...
+ */
+
+static void
+cgi_puturi(const char *s, /* I - String to output */
+ FILE *out) /* I - Output file */
+{
+ while (*s)
+ {
+ if (strchr("%&+ <>#=", *s) || *s & 128)
+ fprintf(out, "%%%02X", *s & 255);
+ else
+ putc(*s, out);
+
+ s ++;
+ }
+}
+
+
+/*
+ * End of "$Id: template.c 5548 2006-05-19 19:38:31Z mike $".
*/