]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cgi-bin/template.c
Load cups into easysw/current.
[thirdparty/cups.git] / cgi-bin / template.c
index dfcd1b85e8e72792ef2cdf082ee69ee62a03110b..774d7fe8fb69fb0b08aa99a3c99e4af24e94bfb4 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * "$Id: template.c 4921 2006-01-12 21:26:26Z mike $"
+ * "$Id: template.c 5113 2006-02-16 12:02:44Z 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);
 
 
@@ -56,18 +58,25 @@ cgiCopyTemplateFile(FILE       *out,        /* I - Output file */
   FILE *in;                            /* Input file */
 
 
+  fprintf(stderr, "DEBUG: cgiCopyTemplateFile(out=%p, tmpl=\"%s\")\n", out,
+          tmpl);
+
  /*
   * Open the template file...
   */
 
   if ((in = fopen(tmpl, "r")) == NULL)
+  {
+    fprintf(stderr, "ERROR: Unable to open template file \"%s\" - %s\n",
+            tmpl, 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...
@@ -92,6 +101,8 @@ cgiCopyTemplateLang(const char *tmpl)        /* I - Base filename */
   FILE         *in;                    /* Input file */
 
 
+  fprintf(stderr, "DEBUG: cgiCopyTemplateLang(tmpl=\"%s\")\n", tmpl);
+
  /*
   * Convert the language to a locale name...
   */
@@ -101,14 +112,18 @@ cgiCopyTemplateLang(const char *tmpl)     /* I - Base filename */
     for (i = 0; lang[i] && i < 15; i ++)
       if (isalnum(lang[i] & 255))
         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...
   */
@@ -125,18 +140,24 @@ cgiCopyTemplateLang(const char *tmpl)     /* I - Base filename */
       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...
@@ -195,25 +216,29 @@ cgiSetServerVersion(void)
  */
 
 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 */
+
+
+  fprintf(stderr, "DEBUG: %*sStarting at file position %ld...\n", indent, "",
+          ftell(in));
 
  /*
   * Parse the file to the end...
@@ -240,6 +265,8 @@ cgi_copy(FILE *out,         /* I - Output file */
 
       if (s == name && isspace(ch & 255))
       {
+        fprintf(stderr, "DEBUG: %*sLone { at %ld...\n", indent, "", ftell(in));
+
         if (out)
        {
           putc('{', out);
@@ -249,6 +276,10 @@ cgi_copy(FILE *out,                /* I - Output file */
        continue;
       }
 
+      if (ch == '}')
+       fprintf(stderr, "DEBUG: %*s\"{%s}\" at %ld...\n", indent, "", name,
+               ftell(in));
+
      /*
       * See if it has a value...
       */
@@ -310,16 +341,24 @@ cgi_copy(FILE *out,               /* I - Output file */
 
        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;
       }
@@ -375,13 +414,16 @@ cgi_copy(FILE *out,               /* I - Output file */
       *   {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
       {
@@ -389,8 +431,6 @@ cgi_copy(FILE *out,         /* I - Output file */
         * Compare to a string...
        */
 
-        op = ch;
-
        for (s = compare; (ch = getc(in)) != EOF;)
           if (ch == '?')
             break;
@@ -446,7 +486,12 @@ cgi_copy(FILE *out,                /* I - Output file */
         *s = '\0';
 
         if (ch != '?')
+       {
+         fprintf(stderr,
+                 "DEBUG: %*sBad terminator '%c' at file position %ld...\n",
+                 indent, "", ch, ftell(in));
          return;
+       }
 
        /*
         * Do the comparison...
@@ -472,14 +517,21 @@ cgi_copy(FILE *out,               /* I - Output file */
        }
       }
 
+      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
       {
@@ -487,9 +539,15 @@ cgi_copy(FILE *out,                /* I - Output file */
        * 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 */
     {
@@ -501,6 +559,17 @@ cgi_copy(FILE *out,                /* I - Output file */
     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...
   */
@@ -569,5 +638,5 @@ cgi_puts(const char *s,                     /* I - String to output */
 
 
 /*
- * End of "$Id: template.c 4921 2006-01-12 21:26:26Z mike $".
+ * End of "$Id: template.c 5113 2006-02-16 12:02:44Z mike $".
  */