]> 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 774d7fe8fb69fb0b08aa99a3c99e4af24e94bfb4..d2fd9b8e3c09de431b237dabedd789a86c61965d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * "$Id: template.c 5113 2006-02-16 12:02:44Z mike $"
+ * "$Id: template.c 5548 2006-05-19 19:38:31Z mike $"
  *
  *   CGI template function.
  *
@@ -44,6 +44,7 @@
 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);
 
 
 /*
@@ -59,7 +60,7 @@ cgiCopyTemplateFile(FILE       *out,  /* I - Output file */
 
 
   fprintf(stderr, "DEBUG: cgiCopyTemplateFile(out=%p, tmpl=\"%s\")\n", out,
-          tmpl);
+          tmpl ? tmpl : "(null)");
 
  /*
   * Open the template file...
@@ -68,7 +69,7 @@ cgiCopyTemplateFile(FILE       *out,  /* I - Output file */
   if ((in = fopen(tmpl, "r")) == NULL)
   {
     fprintf(stderr, "ERROR: Unable to open template file \"%s\" - %s\n",
-            tmpl, strerror(errno));
+            tmpl ? tmpl : "(null)", strerror(errno));
     return;
   }
 
@@ -101,7 +102,8 @@ cgiCopyTemplateLang(const char *tmpl)       /* I - Base filename */
   FILE         *in;                    /* Input file */
 
 
-  fprintf(stderr, "DEBUG: cgiCopyTemplateLang(tmpl=\"%s\")\n", tmpl);
+  fprintf(stderr, "DEBUG: cgiCopyTemplateLang(tmpl=\"%s\")\n",
+          tmpl ? tmpl : "(null)");
 
  /*
   * Convert the language to a locale name...
@@ -110,7 +112,7 @@ cgiCopyTemplateLang(const char *tmpl)       /* I - Base filename */
   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 if (lang[i] == '-')
         locale[i] = '_';
@@ -235,6 +237,7 @@ cgi_copy(FILE *out,                 /* I - Output file */
   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, "",
@@ -253,9 +256,13 @@ cgi_copy(FILE *out,                        /* I - Output file */
       * 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))
@@ -399,7 +406,12 @@ cgi_copy(FILE *out,                        /* I - Output file */
         */
 
        if (out)
-         cgi_puts(outptr, out);
+       {
+         if (uriencode)
+           cgi_puturi(outptr, out);
+         else
+           cgi_puts(outptr, out);
+        }
 
         continue;
       }
@@ -638,5 +650,25 @@ cgi_puts(const char *s,                    /* I - String to output */
 
 
 /*
- * End of "$Id: template.c 5113 2006-02-16 12:02:44Z 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 $".
  */