]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cgi-bin/template.c
Import CUPS 1.4svn-r7226.
[thirdparty/cups.git] / cgi-bin / template.c
index 9677e44feaf91ffa780dcfab78d507589415d750..661f6395b9d737086ec05cb7fba43fd788e3724b 100644 (file)
@@ -1,9 +1,9 @@
 /*
- * "$Id: template.c 6649 2007-07-11 21:46:42Z mike $"
+ * "$Id: template.c 6986 2007-09-25 15:34:52Z mike $"
  *
  *   CGI template function.
  *
- *   Copyright 2007 by Apple Inc.
+ *   Copyright 2007-2008 by Apple Inc.
  *   Copyright 1997-2006 by Easy Software Products.
  *
  *   These coded instructions, statements, and computer programs are the
@@ -26,6 +26,7 @@
 
 #include "cgi-private.h"
 #include <errno.h>
+#include <regex.h>
 
 
 /*
@@ -50,9 +51,16 @@ cgiCopyTemplateFile(FILE       *out, /* I - Output file */
   FILE *in;                            /* Input file */
 
 
-  fprintf(stderr, "DEBUG: cgiCopyTemplateFile(out=%p, tmpl=\"%s\")\n", out,
+  fprintf(stderr, "DEBUG2: cgiCopyTemplateFile(out=%p, tmpl=\"%s\")\n", out,
           tmpl ? tmpl : "(null)");
 
+ /*
+  * Range check input...
+  */
+
+  if (!tmpl || !out)
+    return;
+
  /*
   * Open the template file...
   */
@@ -85,37 +93,34 @@ cgiCopyTemplateFile(FILE       *out,        /* I - Output file */
 void
 cgiCopyTemplateLang(const char *tmpl)  /* I - Base filename */
 {
-  int          i;                      /* Looping var */
   char         filename[1024],         /* Filename */
-               locale[16];             /* Locale name */
+               locale[16],             /* Locale name */
+               *locptr;                /* Pointer into locale name */
   const char   *directory,             /* Directory for templates */
                *lang;                  /* Language */
   FILE         *in;                    /* Input file */
 
 
-  fprintf(stderr, "DEBUG: cgiCopyTemplateLang(tmpl=\"%s\")\n",
+  fprintf(stderr, "DEBUG2: cgiCopyTemplateLang(tmpl=\"%s\")\n",
           tmpl ? tmpl : "(null)");
 
  /*
   * Convert the language to a locale name...
   */
 
+  locale[0] = '\0';
+
   if ((lang = getenv("LANG")) != NULL)
   {
-    for (i = 0; lang[i] && i < 15; i ++)
-      if (isalnum(lang[i] & 255) || lang[i] == '_')
-        locale[i] = tolower(lang[i]);
-      else if (lang[i] == '-')
-        locale[i] = '_';
-      else
-        break;
+    locale[0] = '/';
+    strlcpy(locale + 1, lang, sizeof(locale) - 1);
 
-    locale[i] = '\0';
+    if ((locptr = strchr(locale, '.')) != NULL)
+      *locptr = '\0';                  /* Strip charset */
   }
-  else
-    locale[0] = '\0';
 
-  fprintf(stderr, "DEBUG: locale=\"%s\"...\n", locale);
+  fprintf(stderr, "DEBUG: lang=\"%s\", locale=\"%s\"...\n",
+          lang ? lang : "(null)", locale);
 
  /*
   * See if we have a template file for this language...
@@ -123,23 +128,26 @@ cgiCopyTemplateLang(const char *tmpl)     /* I - Base filename */
 
   directory = cgiGetTemplateDir();
 
-  snprintf(filename, sizeof(filename), "%s/%s/%s", directory, locale, tmpl);
-  if (access(filename, 0))
+  snprintf(filename, sizeof(filename), "%s%s/%s", directory, locale, tmpl);
+  if ((in = fopen(filename, "r")) == NULL)
   {
-    locale[2] = '\0';
+    locale[3] = '\0';
 
-    snprintf(filename, sizeof(filename), "%s/%s/%s", directory, locale, tmpl);
-    if (access(filename, 0))
+    snprintf(filename, sizeof(filename), "%s%s/%s", directory, locale, tmpl);
+    if ((in = fopen(filename, "r")) == NULL)
+    {
       snprintf(filename, sizeof(filename), "%s/%s", directory, tmpl);
+      in = fopen(filename, "r");
+    }
   }
 
-  fprintf(stderr, "DEBUG: Template file is \"%s\"...\n", filename);
+  fprintf(stderr, "DEBUG2: Template file is \"%s\"...\n", filename);
 
  /*
   * Open the template file...
   */
 
-  if ((in = fopen(filename, "r")) == NULL)
+  if (!in)
   {
     fprintf(stderr, "ERROR: Unable to open template file \"%s\" - %s\n",
             filename, strerror(errno));
@@ -229,9 +237,10 @@ cgi_copy(FILE *out,                        /* I - Output file */
                compare[1024];          /* Comparison string */
   int          result;                 /* Result of comparison */
   int          uriencode;              /* Encode as URI */
+  regex_t      re;                     /* Regular expression to match */
 
 
-  fprintf(stderr, "DEBUG: %*sStarting at file position %ld...\n", indent, "",
+  fprintf(stderr, "DEBUG2: %*sStarting at file position %ld...\n", indent, "",
           ftell(in));
 
  /*
@@ -250,7 +259,7 @@ cgi_copy(FILE *out,                 /* I - Output file */
       uriencode = 0;
 
       for (s = name; (ch = getc(in)) != EOF;)
-        if (strchr("}]<>=! \t\n", ch))
+        if (strchr("}]<>=!~ \t\n", ch))
           break;
        else if (s == name && ch == '%')
          uriencode = 1;
@@ -263,7 +272,7 @@ cgi_copy(FILE *out,                 /* I - Output file */
 
       if (s == name && isspace(ch & 255))
       {
-        fprintf(stderr, "DEBUG: %*sLone { at %ld...\n", indent, "", ftell(in));
+        fprintf(stderr, "DEBUG2: %*sLone { at %ld...\n", indent, "", ftell(in));
 
         if (out)
        {
@@ -275,7 +284,7 @@ cgi_copy(FILE *out,                 /* I - Output file */
       }
 
       if (ch == '}')
-       fprintf(stderr, "DEBUG: %*s\"{%s}\" at %ld...\n", indent, "", name,
+       fprintf(stderr, "DEBUG2: %*s\"{%s}\" at %ld...\n", indent, "", name,
                ftell(in));
 
      /*
@@ -339,7 +348,7 @@ cgi_copy(FILE *out,                 /* I - Output file */
 
        pos = ftell(in);
 
-        fprintf(stderr, "DEBUG: %*sLooping on \"%s\" at %ld, count=%d...\n",
+        fprintf(stderr, "DEBUG2: %*sLooping on \"%s\" at %ld, count=%d...\n",
                indent, "", name + 1, pos, count);
 
         if (count > 0)
@@ -355,7 +364,7 @@ cgi_copy(FILE *out,                 /* I - Output file */
        else
          cgi_copy(NULL, in, 0, '}', indent + 2);
 
-        fprintf(stderr, "DEBUG: %*sFinished looping on \"%s\"...\n", indent,
+        fprintf(stderr, "DEBUG2: %*sFinished looping on \"%s\"...\n", indent,
                "", name + 1);
 
         continue;
@@ -400,6 +409,8 @@ cgi_copy(FILE *out,                 /* I - Output file */
        {
          if (uriencode)
            cgi_puturi(outptr, out);
+         else if (!strcasecmp(name, "?cupsdconf_default"))
+           fputs(outptr, stdout);
          else
            cgi_puts(outptr, out);
         }
@@ -415,6 +426,7 @@ cgi_copy(FILE *out,                 /* I - Output file */
       *   {name<value?true:false}    Less than
       *   {name>value?true:false}    Greater than
       *   {name!value?true:false}    Not equal
+      *   {name~refex?true:false}    Regex match
       */
 
       op = ch;
@@ -491,7 +503,7 @@ cgi_copy(FILE *out,                 /* I - Output file */
         if (ch != '?')
        {
          fprintf(stderr,
-                 "DEBUG: %*sBad terminator '%c' at file position %ld...\n",
+                 "DEBUG2: %*sBad terminator '%c' at file position %ld...\n",
                  indent, "", ch, ftell(in));
          return;
        }
@@ -514,6 +526,39 @@ cgi_copy(FILE *out,                        /* I - Output file */
          case '!' :
              result = strcasecmp(outptr, compare) != 0;
              break;
+         case '~' :
+             fprintf(stderr, "DEBUG: Regular expression \"%s\"\n", compare);
+
+             if (regcomp(&re, compare, REG_EXTENDED | REG_ICASE))
+             {
+               fprintf(stderr,
+                       "ERROR: Unable to compile regular expresion \"%s\"!\n",
+                       compare);
+               result = 0;
+             }
+             else
+             {
+               regmatch_t matches[10];
+
+               result = 0;
+
+               if (!regexec(&re, outptr, 10, matches, 0))
+               {
+                 int i;
+                 for (i = 0; i < 10; i ++)
+                 {
+                   fprintf(stderr, "DEBUG: matches[%d].rm_so=%d\n", i,
+                           (int)matches[i].rm_so);
+                   if (matches[i].rm_so < 0)
+                     break;
+
+                   result ++;
+                 }
+               }
+
+               regfree(&re);
+             }
+             break;
          default :
              result = 1;
              break;
@@ -521,7 +566,7 @@ cgi_copy(FILE *out,                 /* I - Output file */
       }
 
       fprintf(stderr,
-              "DEBUG: %*sStarting \"{%s%c%s\" at %ld, result=%d...\n",
+              "DEBUG2: %*sStarting \"{%s%c%s\" at %ld, result=%d...\n",
              indent, "", name, op, compare, ftell(in), result);
 
       if (result)
@@ -530,10 +575,10 @@ cgi_copy(FILE *out,                       /* I - Output file */
        * Comparison true; output first part and ignore second...
        */
 
-        fprintf(stderr, "DEBUG: %*sOutput first part...\n", indent, "");
+        fprintf(stderr, "DEBUG2: %*sOutput first part...\n", indent, "");
        cgi_copy(out, in, element, ':', indent + 2);
 
-        fprintf(stderr, "DEBUG: %*sSkip second part...\n", indent, "");
+        fprintf(stderr, "DEBUG2: %*sSkip second part...\n", indent, "");
        cgi_copy(NULL, in, element, '}', indent + 2);
       }
       else
@@ -542,14 +587,14 @@ cgi_copy(FILE *out,                       /* I - Output file */
        * Comparison false; ignore first part and output second...
        */
 
-        fprintf(stderr, "DEBUG: %*sSkip first part...\n", indent, "");
+        fprintf(stderr, "DEBUG2: %*sSkip first part...\n", indent, "");
        cgi_copy(NULL, in, element, ':', indent + 2);
 
-        fprintf(stderr, "DEBUG: %*sOutput second part...\n", indent, "");
+        fprintf(stderr, "DEBUG2: %*sOutput second part...\n", indent, "");
        cgi_copy(out, in, element, '}', indent + 2);
       }
 
-      fprintf(stderr, "DEBUG: %*sFinished \"{%s%c%s\", out=%p...\n", indent, "",
+      fprintf(stderr, "DEBUG2: %*sFinished \"{%s%c%s\", out=%p...\n", indent, "",
               name, op, compare, out);
     }
     else if (ch == '\\')       /* Quoted char */
@@ -563,11 +608,11 @@ cgi_copy(FILE *out,                       /* I - Output file */
       putc(ch, out);
 
   if (ch == EOF)
-    fprintf(stderr, "DEBUG: %*sReturning at file position %ld on EOF...\n",
+    fprintf(stderr, "DEBUG2: %*sReturning at file position %ld on EOF...\n",
            indent, "", ftell(in));
   else
     fprintf(stderr,
-            "DEBUG: %*sReturning at file position %ld on character '%c'...\n",
+            "DEBUG2: %*sReturning at file position %ld on character '%c'...\n",
            indent, "", ftell(in), ch);
 
   if (ch == EOF && term)
@@ -661,5 +706,5 @@ cgi_puturi(const char *s,           /* I - String to output */
 
 
 /*
- * End of "$Id: template.c 6649 2007-07-11 21:46:42Z mike $".
+ * End of "$Id: template.c 6986 2007-09-25 15:34:52Z mike $".
  */