]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cgi-bin/ipp-var.c
Fix build errors on Fedora.
[thirdparty/cups.git] / cgi-bin / ipp-var.c
index 1b63056fa096be4fbefc1424b8be634f3be80d45..764cf8a4da17d27e6d4bdda5d10bf28950a35165 100644 (file)
@@ -1,31 +1,16 @@
 /*
- * "$Id: ipp-var.c 7940 2008-09-16 00:45:16Z mike $"
+ * "$Id$"
  *
- *   CGI <-> IPP variable routines for the Common UNIX Printing System (CUPS).
+ * CGI <-> IPP variable routines for CUPS.
  *
- *   Copyright 2007-2009 by Apple Inc.
- *   Copyright 1997-2007 by Easy Software Products.
+ * Copyright 2007-2014 by Apple Inc.
+ * Copyright 1997-2007 by Easy Software Products.
  *
- *   These coded instructions, statements, and computer programs are the
- *   property of Apple Inc. and are protected by Federal copyright
- *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
- *   which should have been included with this file.  If this file is
- *   file is missing or damaged, see the license at "http://www.cups.org/".
- *
- * Contents:
- *
- *   cgiGetAttributes()    - Get the list of attributes that are needed by the
- *                           template file.
- *   cgiGetIPPObjects()    - Get the objects in an IPP response.
- *   cgiMoveJobs()         - Move one or more jobs.
- *   cgiPrintCommand()     - Print a CUPS command job.
- *   cgiPrintTestPage()    - Print a test page.
- *   cgiRewriteURL()       - Rewrite a printer URI into a web browser URL...
- *   cgiSetIPPObjectVars() - Set CGI variables from an IPP object.
- *   cgiSetIPPVars()       - Set CGI variables from an IPP response.
- *   cgiShowIPPError()     - Show the last IPP error message.
- *   cgiShowJobs()         - Show print jobs.
- *   cgiText()             - Return localized text.
+ * These coded instructions, statements, and computer programs are the
+ * property of Apple Inc. and are protected by Federal copyright
+ * law.  Distribution and use rights are outlined in the file "LICENSE.txt"
+ * which should have been included with this file.  If this file is
+ * file is missing or damaged, see the license at "http://www.cups.org/".
  */
 
 /*
@@ -65,7 +50,7 @@ cgiGetAttributes(ipp_t      *request, /* I - IPP request */
   {
     for (i = 0; lang[i] && i < 15; i ++)
       if (isalnum(lang[i] & 255))
-        locale[i] = tolower(lang[i]);
+        locale[i] = (char)tolower(lang[i]);
       else
         locale[i] = '_';
 
@@ -107,7 +92,7 @@ cgiGetAttributes(ipp_t      *request,        /* I - IPP request */
   while ((ch = getc(in)) != EOF)
     if (ch == '\\')
       getc(in);
-    else if (ch == '{' && num_attrs < (sizeof(attrs) / sizeof(attrs[0])))
+    else if (ch == '{' && num_attrs < (int)(sizeof(attrs) / sizeof(attrs[0])))
     {
      /*
       * Grab the name...
@@ -123,13 +108,13 @@ cgiGetAttributes(ipp_t      *request,     /* I - IPP request */
          if (ch == '_')
            *nameptr++ = '-';
          else
-            *nameptr++ = ch;
+            *nameptr++ = (char)ch;
        }
 
       *nameptr = '\0';
 
       if (!strncmp(name, "printer_state_history", 21))
-        strcpy(name, "printer_state_history");
+        strlcpy(name, "printer_state_history", sizeof(name));
 
      /*
       * Possibly add it to the list of attributes...
@@ -285,10 +270,14 @@ cgiMoveJobs(http_t     *http,             /* I - Connection to server */
 
 
  /*
-  * See who is logged in...
+  * Make sure we have a username...
   */
 
-  user = getenv("REMOTE_USER");
+  if ((user = getenv("REMOTE_USER")) == NULL)
+  {
+    puts("Status: 401\n");
+    exit(0);
+  }
 
  /*
   * See if the user has already selected a new destination...
@@ -333,7 +322,7 @@ cgiMoveJobs(http_t     *http,               /* I - Connection to server */
                    NULL, job_uri);
       ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
                    "requested-attributes", NULL, "job-printer-uri");
-      
+
       if ((response = cupsDoRequest(http, request, "/")) != NULL)
       {
         if ((attr = ippFindAttribute(response, "job-printer-uri",
@@ -358,7 +347,7 @@ cgiMoveJobs(http_t     *http,               /* I - Connection to server */
        */
 
         cgiStartHTML(cgiText(_("Move Job")));
-       cgiShowIPPError(_("Unable to find destination for job!"));
+       cgiShowIPPError(_("Unable to find destination for job"));
        cgiEndHTML();
        return;
       }
@@ -400,7 +389,7 @@ cgiMoveJobs(http_t     *http,               /* I - Connection to server */
         * If the name is not the same as the current destination, add it!
        */
 
-        if (strcasecmp(name, dest))
+        if (_cups_strcasecmp(name, dest))
        {
          cgiSetArray("JOB_PRINTER_URI", i, attr->values[0].string.text);
          cgiSetArray("JOB_PRINTER_NAME", i, name);
@@ -550,7 +539,7 @@ cgiPrintCommand(http_t     *http,   /* I - Connection to server */
   ipp_t                *request,               /* Get-Job-Attributes request */
                *response;              /* Get-Job-Attributes response */
   ipp_attribute_t *attr;               /* Current job attribute */
-  static const char const *job_attrs[] =/* Job attributes we want */
+  static const char * const job_attrs[] =/* Job attributes we want */
                {
                  "job-state",
                  "job-printer-state-message"
@@ -591,7 +580,7 @@ cgiPrintCommand(http_t     *http,   /* I - Connection to server */
   if ((job_id = cupsCreateJob(http, dest, title,
                              1, &hold_option)) < 1)
   {
-    cgiSetVariable("MESSAGE", cgiText(_("Unable to send command to printer driver!")));
+    cgiSetVariable("MESSAGE", cgiText(_("Unable to send command to printer driver")));
     cgiSetVariable("ERROR", cupsLastErrorString());
     cgiStartHTML(title);
     cgiCopyTemplateLang("error.tmpl");
@@ -611,7 +600,7 @@ cgiPrintCommand(http_t     *http,   /* I - Connection to server */
 
   if (cupsLastError() >= IPP_REDIRECTION_OTHER_SITE)
   {
-    cgiSetVariable("MESSAGE", cgiText(_("Unable to send command to printer driver!")));
+    cgiSetVariable("MESSAGE", cgiText(_("Unable to send command to printer driver")));
     cgiSetVariable("ERROR", cupsLastErrorString());
     cgiStartHTML(title);
     cgiCopyTemplateLang("error.tmpl");
@@ -650,7 +639,8 @@ cgiPrintCommand(http_t     *http,   /* I - Connection to server */
        cgiSetIPPVars(response, NULL, NULL, NULL, 0);
 
       attr = ippFindAttribute(response, "job-state", IPP_TAG_ENUM);
-      if (!attr || attr->values[0].integer >= IPP_JOB_STOPPED)
+      if (!attr || attr->values[0].integer >= IPP_JOB_STOPPED ||
+          attr->values[0].integer == IPP_JOB_HELD)
       {
        ippDelete(response);
        break;
@@ -787,7 +777,7 @@ cgiPrintTestPage(http_t     *http,  /* I - Connection to server */
   cgiStartHTML(cgiText(_("Print Test Page")));
 
   if (cupsLastError() > IPP_OK_CONFLICT)
-    cgiShowIPPError(_("Unable to print test page:"));
+    cgiShowIPPError(_("Unable to print test page"));
   else
   {
     cgiSetVariable("PRINTER_NAME", dest);
@@ -901,18 +891,18 @@ cgiRewriteURL(const char *uri,            /* I - Current URI */
     * Map local access to a local URI...
     */
 
-    if (!strcasecmp(hostname, "127.0.0.1") ||
-       !strcasecmp(hostname, "[::1]") ||
-       !strcasecmp(hostname, "localhost") ||
-       !strncasecmp(hostname, "localhost.", 10) ||
-       !strcasecmp(hostname, server) ||
-       !strcasecmp(hostname, servername))
+    if (!_cups_strcasecmp(hostname, "127.0.0.1") ||
+       !_cups_strcasecmp(hostname, "[::1]") ||
+       !_cups_strcasecmp(hostname, "localhost") ||
+       !_cups_strncasecmp(hostname, "localhost.", 10) ||
+       !_cups_strcasecmp(hostname, server) ||
+       !_cups_strcasecmp(hostname, servername))
     {
      /*
       * Make URI relative to the current server...
       */
 
-      strlcpy(url, resource, urlsize);
+      strlcpy(url, resource, (size_t)urlsize);
     }
     else
     {
@@ -921,17 +911,13 @@ cgiRewriteURL(const char *uri,            /* I - Current URI */
       */
 
       if (userpass[0])
-       snprintf(url, urlsize, "%s://%s@%s:%d%s",
-                ishttps ? "https" : "http",
-                userpass, hostname, port, resource);
+       snprintf(url, (size_t)urlsize, "%s://%s@%s:%d%s", ishttps ? "https" : "http", userpass, hostname, port, resource);
       else
-       snprintf(url, urlsize, "%s://%s:%d%s", 
-                ishttps ? "https" : "http",
-                hostname, port, resource);
+       snprintf(url, (size_t)urlsize, "%s://%s:%d%s", ishttps ? "https" : "http", hostname, port, resource);
     }
   }
   else
-    strlcpy(url, uri, urlsize);
+    strlcpy(url, uri, (size_t)urlsize);
 
   return (url);
 }
@@ -1034,7 +1020,7 @@ cgiSetIPPObjectVars(
          *valptr++ = ' ';
         }
 
-        remaining = sizeof(value) - (valptr - value);
+        remaining = sizeof(value) - (size_t)(valptr - value);
 
         if (!strcmp(attr->values[i].string.text, "printer-stopped"))
          strlcpy(valptr, _("Printer Paused"), remaining);
@@ -1172,7 +1158,7 @@ cgiSetIPPObjectVars(
     for (i = 0; i < attr->num_values; i ++)
     {
       if (i)
-       strlcat(valptr, ", ", sizeof(value) - (valptr - value));
+       strlcat(valptr, ", ", sizeof(value) - (size_t)(valptr - value));
 
       valptr += strlen(valptr);
 
@@ -1187,34 +1173,34 @@ cgiSetIPPObjectVars(
               t    = (time_t)attr->values[i].integer;
              date = localtime(&t);
 
-             strftime(valptr, sizeof(value) - (valptr - value), "%c", date);
+             strftime(valptr, sizeof(value) - (size_t)(valptr - value), "%c", date);
            }
            else
-             snprintf(valptr, sizeof(value) - (valptr - value),
+             snprintf(valptr, sizeof(value) - (size_t)(valptr - value),
                       "%d", attr->values[i].integer);
            break;
 
        case IPP_TAG_BOOLEAN :
-           snprintf(valptr, sizeof(value) - (valptr - value),
+           snprintf(valptr, sizeof(value) - (size_t)(valptr - value),
                     "%d", attr->values[i].boolean);
            break;
 
        case IPP_TAG_NOVALUE :
-           strlcat(valptr, "novalue", sizeof(value) - (valptr - value));
+           strlcat(valptr, "novalue", sizeof(value) - (size_t)(valptr - value));
            break;
 
        case IPP_TAG_RANGE :
-           snprintf(valptr, sizeof(value) - (valptr - value),
+           snprintf(valptr, sizeof(value) - (size_t)(valptr - value),
                     "%d-%d", attr->values[i].range.lower,
                     attr->values[i].range.upper);
            break;
 
        case IPP_TAG_RESOLUTION :
-           snprintf(valptr, sizeof(value) - (valptr - value),
+           snprintf(valptr, sizeof(value) - (size_t)(valptr - value),
                     "%dx%d%s", attr->values[i].resolution.xres,
                     attr->values[i].resolution.yres,
                     attr->values[i].resolution.units == IPP_RES_PER_INCH ?
-                        "dpi" : "dpc");
+                        "dpi" : "dpcm");
            break;
 
        case IPP_TAG_URI :
@@ -1233,13 +1219,13 @@ cgiSetIPPObjectVars(
                cgiRewriteURL(attr->values[i].string.text, url,
                              sizeof(url), NULL);
 
-                snprintf(valptr, sizeof(value) - (valptr - value),
+                snprintf(valptr, sizeof(value) - (size_t)(valptr - value),
                         "<A HREF=\"%s\">%s</A>", url,
                         strrchr(attr->values[i].string.text, '/') + 1);
              }
              else
                cgiRewriteURL(attr->values[i].string.text, valptr,
-                             sizeof(value) - (valptr - value), NULL);
+                             (int)(sizeof(value) - (size_t)(valptr - value)), NULL);
               break;
             }
 
@@ -1251,7 +1237,7 @@ cgiSetIPPObjectVars(
        case IPP_TAG_LANGUAGE :
        case IPP_TAG_MIMETYPE :
            strlcat(valptr, attr->values[i].string.text,
-                   sizeof(value) - (valptr - value));
+                   sizeof(value) - (size_t)(valptr - value));
            break;
 
         case IPP_TAG_BEGIN_COLLECTION :
@@ -1342,7 +1328,7 @@ cgiSetIPPVars(ipp_t      *response,       /* I - Response data to be copied... */
             (filter->value_tag >= IPP_TAG_TEXTLANG &&
              filter->value_tag <= IPP_TAG_MIMETYPE)) &&
            filter->values[0].string.text != NULL &&
-           !strcasecmp(filter->values[0].string.text, filter_value))
+           !_cups_strcasecmp(filter->values[0].string.text, filter_value))
          break;
 
       if (!filter)
@@ -1397,7 +1383,9 @@ cgiShowJobs(http_t     *http,             /* I - Connection to server */
   int                  ascending,      /* Order of jobs (0 = descending) */
                        first,          /* First job to show */
                        count;          /* Number of jobs */
-  const char           *var;           /* Form variable */
+  const char           *var,           /* Form variable */
+                       *query,         /* Query string */
+                       *section;       /* Section in web interface */
   void                 *search;        /* Search data */
   char                 url[1024],      /* Printer URI */
                        val[1024];      /* Form variable */
@@ -1425,7 +1413,7 @@ cgiShowJobs(http_t     *http,             /* I - Connection to server */
     ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
                 "ipp://localhost/");
 
-  if ((which_jobs = cgiGetVariable("which_jobs")) != NULL)
+  if ((which_jobs = cgiGetVariable("which_jobs")) != NULL && *which_jobs)
     ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "which-jobs",
                  NULL, which_jobs);
 
@@ -1441,11 +1429,14 @@ cgiShowJobs(http_t     *http,           /* I - Connection to server */
     * Get a list of matching job objects.
     */
 
-    if ((var = cgiGetVariable("QUERY")) != NULL &&
+    if ((query = cgiGetVariable("QUERY")) != NULL &&
         !cgiGetVariable("CLEAR"))
-      search = cgiCompileSearch(var);
+      search = cgiCompileSearch(query);
     else
+    {
+      query  = NULL;
       search = NULL;
+    }
 
     jobs  = cgiGetIPPObjects(response, search);
     count = cupsArrayCount(jobs);
@@ -1470,16 +1461,28 @@ cgiShowJobs(http_t     *http,           /* I - Connection to server */
     if (first < 0)
       first = 0;
 
+    if ((var = cgiGetVariable("ORDER")) != NULL && *var)
+      ascending = !_cups_strcasecmp(var, "asc");
+    else
+      ascending = !which_jobs || !*which_jobs ||
+                  !_cups_strcasecmp(which_jobs, "not-completed");
+
+    section = cgiGetVariable("SECTION");
+
+    cgiClearVariables();
+
+    if (query)
+      cgiSetVariable("QUERY", query);
+
+    cgiSetVariable("ORDER", ascending ? "asc" : "dec");
+
+    cgiSetVariable("SECTION", section);
+
     sprintf(val, "%d", count);
     cgiSetVariable("TOTAL", val);
 
-    if ((var = cgiGetVariable("ORDER")) != NULL)
-      ascending = !strcasecmp(var, "asc");
-    else
-    {
-      ascending = !which_jobs || !strcasecmp(which_jobs, "not-completed");
-      cgiSetVariable("ORDER", ascending ? "asc" : "dec");
-    }
+    if (which_jobs)
+      cgiSetVariable("WHICH_JOBS", which_jobs);
 
     if (ascending)
     {
@@ -1501,7 +1504,11 @@ cgiShowJobs(http_t     *http,            /* I - Connection to server */
     */
 
     if (dest)
-      snprintf(val, sizeof(val), "/%s/%s",  cgiGetVariable("SECTION"), dest);
+    {
+      snprintf(val, sizeof(val), "/%s/%s", section, dest);
+      cgiSetVariable("PRINTER_NAME", dest);
+      cgiSetVariable("PRINTER_URI_SUPPORTED", val);
+    }
     else
       strlcpy(val, "/jobs/", sizeof(val));
 
@@ -1563,5 +1570,5 @@ cgiText(const char *message)              /* I - Message */
 
 
 /*
- * End of "$Id: ipp-var.c 7940 2008-09-16 00:45:16Z mike $".
+ * End of "$Id$".
  */