]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - backend/ipp.c
Full sweep of all Clang warnings, plus some bug fixes for incorrect memcpy usage.
[thirdparty/cups.git] / backend / ipp.c
index 8c297c5dfabf286c64480ed37911133a3e1e19de..c47f44225a6032b8048d86750bae15e1adaa1c6c 100644 (file)
@@ -3,7 +3,7 @@
  *
  * IPP backend for CUPS.
  *
- * Copyright 2007-2013 by Apple Inc.
+ * Copyright 2007-2014 by Apple Inc.
  * Copyright 1997-2007 by Easy Software Products, all rights reserved.
  *
  * These coded instructions, statements, and computer programs are the
@@ -79,7 +79,7 @@ typedef struct _cups_monitor_s                /**** Monitoring data ****/
 static const char      *auth_info_required;
                                        /* New auth-info-required value */
 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
-static int             child_pid = 0;  /* Child process ID */
+static pid_t           child_pid = 0;  /* Child process ID */
 #endif /* HAVE_GSSAPI && HAVE_XPC */
 static const char * const jattrs[] =   /* Job attributes we want */
 {
@@ -163,7 +163,7 @@ static ipp_t                *new_request(ipp_op_t op, int version, const char *uri,
                                     ppd_file_t *ppd,
                                     ipp_attribute_t *media_col_sup,
                                     ipp_attribute_t *doc_handling_sup,
-                                    int print_color_mode);
+                                    ipp_attribute_t *print_color_mode_sup);
 static const char      *password_cb(const char *prompt, http_t *http,
                                     const char *method, const char *resource,
                                     int *user_data);
@@ -250,12 +250,12 @@ main(int  argc,                           /* I - Number of command-line args */
   ipp_attribute_t *doc_handling_sup;   /* multiple-document-handling-supported */
   ipp_attribute_t *printer_state;      /* printer-state attribute */
   ipp_attribute_t *printer_accepting;  /* printer-is-accepting-jobs */
+  ipp_attribute_t *print_color_mode_sup;/* Does printer support print-color-mode? */
   int          create_job = 0,         /* Does printer support Create-Job? */
                get_job_attrs = 0,      /* Does printer support Get-Job-Attributes? */
                send_document = 0,      /* Does printer support Send-Document? */
                validate_job = 0,       /* Does printer support Validate-Job? */
-               print_color_mode = 0;   /* Does printer support print-color-mode? */
-  int          copies,                 /* Number of copies for job */
+               copies,                 /* Number of copies for job */
                copies_remaining;       /* Number of copies remaining */
   const char   *content_type,          /* CONTENT_TYPE environment variable */
                *final_content_type,    /* FINAL_CONTENT_TYPE environment var */
@@ -785,7 +785,7 @@ main(int  argc,                             /* I - Number of command-line args */
              break;
         }
 
-       sleep(delay);
+       sleep((unsigned)delay);
 
         delay = _cupsNextDelay(delay, &prev_delay);
       }
@@ -831,15 +831,16 @@ main(int  argc,                           /* I - Number of command-line args */
   */
 
 #ifdef HAVE_LIBZ
-  compression_sup  = NULL;
+  compression_sup      = NULL;
 #endif /* HAVE_LIBZ */
-  copies_sup       = NULL;
-  cups_version     = NULL;
-  format_sup       = NULL;
-  media_col_sup    = NULL;
-  supported        = NULL;
-  operations_sup   = NULL;
-  doc_handling_sup = NULL;
+  copies_sup           = NULL;
+  cups_version         = NULL;
+  format_sup           = NULL;
+  media_col_sup        = NULL;
+  supported            = NULL;
+  operations_sup       = NULL;
+  doc_handling_sup     = NULL;
+  print_color_mode_sup = NULL;
 
   do
   {
@@ -854,9 +855,7 @@ main(int  argc,                             /* I - Number of command-line args */
     */
 
     request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
-    request->request.op.version[0] = version / 10;
-    request->request.op.version[1] = version % 10;
-
+    ippSetVersion(request, version / 10, version % 10);
     ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
                 NULL, uri);
 
@@ -905,7 +904,7 @@ main(int  argc,                             /* I - Number of command-line args */
 
         report_printer_state(supported);
 
-       sleep(delay);
+       sleep((unsigned)delay);
 
         delay = _cupsNextDelay(delay, &prev_delay);
       }
@@ -1019,12 +1018,13 @@ main(int  argc,                         /* I - Number of command-line args */
 
        report_printer_state(supported);
 
-       sleep(delay);
+       sleep((unsigned)delay);
 
        delay = _cupsNextDelay(delay, &prev_delay);
 
        ippDelete(supported);
-       supported = NULL;
+       supported  = NULL;
+       ipp_status = IPP_STATUS_ERROR_BUSY;
        continue;
       }
     }
@@ -1100,9 +1100,7 @@ main(int  argc,                           /* I - Number of command-line args */
                media_col_sup->values[i].string.text);
     }
 
-    print_color_mode = ippFindAttribute(supported,
-                                       "print-color-mode-supported",
-                                       IPP_TAG_KEYWORD) != NULL;
+    print_color_mode_sup = ippFindAttribute(supported, "print-color-mode-supported", IPP_TAG_KEYWORD);
 
     if ((operations_sup = ippFindAttribute(supported, "operations-supported",
                                           IPP_TAG_ENUM)) != NULL)
@@ -1309,7 +1307,7 @@ main(int  argc,                           /* I - Number of command-line args */
 
     _cupsLangPrintFilter(stderr, "INFO", _("Copying print data."));
 
-    if ((compatsize = write(fd, buffer, bytes)) < 0)
+    if ((compatsize = write(fd, buffer, (size_t)bytes)) < 0)
     {
       perror("DEBUG: Unable to write temporary file");
       return (CUPS_BACKEND_FAILED);
@@ -1384,7 +1382,7 @@ main(int  argc,                           /* I - Number of command-line args */
     request = new_request(IPP_VALIDATE_JOB, version, uri, argv[2],
                           monitor.job_name, num_options, options, compression,
                          copies_sup ? copies : 1, document_format, pc, ppd,
-                         media_col_sup, doc_handling_sup, print_color_mode);
+                         media_col_sup, doc_handling_sup, print_color_mode_sup);
 
     response = cupsDoRequest(http, request, resource);
 
@@ -1470,7 +1468,7 @@ main(int  argc,                           /* I - Number of command-line args */
                          version, uri, argv[2], monitor.job_name, num_options,
                          options, compression, copies_sup ? copies : 1,
                          document_format, pc, ppd, media_col_sup,
-                         doc_handling_sup, print_color_mode);
+                         doc_handling_sup, print_color_mode_sup);
 
    /*
     * Do the request...
@@ -1507,7 +1505,7 @@ main(int  argc,                           /* I - Number of command-line args */
        else
        {
          fd          = 0;
-         http_status = cupsWriteRequestData(http, buffer, bytes);
+         http_status = cupsWriteRequestData(http, buffer, (size_t)bytes);
         }
 
         while (http_status == HTTP_CONTINUE &&
@@ -1533,7 +1531,7 @@ main(int  argc,                           /* I - Number of command-line args */
             {
              fprintf(stderr, "DEBUG: Read %d bytes...\n", (int)bytes);
 
-             if ((http_status = cupsWriteRequestData(http, buffer, bytes))
+             if ((http_status = cupsWriteRequestData(http, buffer, (size_t)bytes))
                      != HTTP_CONTINUE)
                break;
            }
@@ -1669,8 +1667,7 @@ main(int  argc,                           /* I - Number of command-line args */
        */
 
        request = ippNewRequest(IPP_SEND_DOCUMENT);
-       request->request.op.version[0] = version / 10;
-       request->request.op.version[1] = version % 10;
+       ippSetVersion(request, version / 10, version % 10);
 
        ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
                     NULL, uri);
@@ -1703,7 +1700,7 @@ main(int  argc,                           /* I - Number of command-line args */
          if (num_files == 0)
          {
            fd          = 0;
-           http_status = cupsWriteRequestData(http, buffer, bytes);
+           http_status = cupsWriteRequestData(http, buffer, (size_t)bytes);
          }
          else
          {
@@ -1722,7 +1719,7 @@ main(int  argc,                           /* I - Number of command-line args */
          while (!job_canceled && http_status == HTTP_CONTINUE &&
                 (bytes = read(fd, buffer, sizeof(buffer))) > 0)
          {
-           if ((http_status = cupsWriteRequestData(http, buffer, bytes))
+           if ((http_status = cupsWriteRequestData(http, buffer, (size_t)bytes))
                    != HTTP_CONTINUE)
              break;
            else
@@ -1855,6 +1852,8 @@ main(int  argc,                           /* I - Number of command-line args */
     if (!job_id || !waitjob || !get_job_attrs)
       continue;
 
+    fputs("STATE: +cups-waiting-for-job-completed\n", stderr);
+
     _cupsLangPrintFilter(stderr, "INFO", _("Waiting for job to complete."));
 
     for (delay = _cupsNextDelay(0, &prev_delay); !job_canceled;)
@@ -1879,8 +1878,7 @@ main(int  argc,                           /* I - Number of command-line args */
       */
 
       request = ippNewRequest(IPP_GET_JOB_ATTRIBUTES);
-      request->request.op.version[0] = version / 10;
-      request->request.op.version[1] = version % 10;
+      ippSetVersion(request, version / 10, version % 10);
 
       ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
                   NULL, uri);
@@ -2005,7 +2003,7 @@ main(int  argc,                           /* I - Number of command-line args */
       * Wait before polling again...
       */
 
-      sleep(delay);
+      sleep((unsigned)delay);
 
       delay = _cupsNextDelay(delay, &prev_delay);
     }
@@ -2139,8 +2137,7 @@ cancel_job(http_t     *http,              /* I - HTTP connection */
   _cupsLangPrintFilter(stderr, "INFO", _("Canceling print job."));
 
   request = ippNewRequest(IPP_CANCEL_JOB);
-  request->request.op.version[0] = version / 10;
-  request->request.op.version[1] = version % 10;
+  ippSetVersion(request, version / 10, version % 10);
 
   ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
                NULL, uri);
@@ -2185,8 +2182,7 @@ check_printer_state(
   */
 
   request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
-  request->request.op.version[0] = version / 10;
-  request->request.op.version[1] = version % 10;
+  ippSetVersion(request, version / 10, version % 10);
 
   ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
               NULL, uri);
@@ -2289,8 +2285,7 @@ monitor_printer(
       job_op  = (monitor->job_id > 0 && monitor->get_job_attrs) ?
                     IPP_GET_JOB_ATTRIBUTES : IPP_GET_JOBS;
       request = ippNewRequest(job_op);
-      request->request.op.version[0] = monitor->version / 10;
-      request->request.op.version[1] = monitor->version % 10;
+      ippSetVersion(request, monitor->version / 10, monitor->version % 10);
 
       ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
                   NULL, monitor->uri);
@@ -2352,7 +2347,7 @@ monitor_printer(
               job_name = attr->values[0].string.text;
             else if (!strcmp(attr->name, "job-state") &&
                     attr->value_tag == IPP_TAG_ENUM)
-              job_state = attr->values[0].integer;
+              job_state = (ipp_jstate_t)attr->values[0].integer;
             else if (!strcmp(attr->name, "job-originating-user-name") &&
                     (attr->value_tag == IPP_TAG_NAME ||
                      attr->value_tag == IPP_TAG_NAMELANG))
@@ -2439,7 +2434,7 @@ monitor_printer(
     * Sleep for N seconds...
     */
 
-    sleep(delay);
+    sleep((unsigned)delay);
 
     delay = _cupsNextDelay(delay, &prev_delay);
   }
@@ -2483,7 +2478,8 @@ new_request(
     ppd_file_t      *ppd,              /* I - PPD file data */
     ipp_attribute_t *media_col_sup,    /* I - media-col-supported values */
     ipp_attribute_t *doc_handling_sup,  /* I - multiple-document-handling-supported values */
-    int             print_color_mode)  /* I - Printer supports print-color-mode */
+    ipp_attribute_t *print_color_mode_sup)
+                                       /* I - Printer supports print-color-mode */
 {
   int          i;                      /* Looping var */
   ipp_t                *request;               /* Request data */
@@ -2505,9 +2501,8 @@ new_request(
   * Create the IPP request...
   */
 
-  request                        = ippNewRequest(op);
-  request->request.op.version[0] = version / 10;
-  request->request.op.version[1] = version % 10;
+  request = ippNewRequest(op);
+  ippSetVersion(request, version / 10, version % 10);
 
   fprintf(stderr, "DEBUG: %s IPP/%d.%d\n",
          ippOpString(request->request.op.operation_id),
@@ -2574,8 +2569,7 @@ new_request(
           (keyword = cupsGetOption("job-password", num_options,
                                    options)) != NULL)
       {
-        ippAddOctetString(request, IPP_TAG_OPERATION, "job-password",
-                          keyword, strlen(keyword));
+        ippAddOctetString(request, IPP_TAG_OPERATION, "job-password", keyword, (int)strlen(keyword));
 
         if ((keyword = cupsGetOption("job-password-encryption", num_options,
                                     options)) == NULL)
@@ -2654,10 +2648,17 @@ new_request(
                 }
                 break;
             case IPP_TAG_STRING :
-                ippAddOctetString(request, IPP_TAG_JOB, mandatory, keyword,
-                                  strlen(keyword));
+                ippAddOctetString(request, IPP_TAG_JOB, mandatory, keyword, (int)strlen(keyword));
                 break;
             default :
+                if (!strcmp(mandatory, "print-color-mode") && !strcmp(keyword, "monochrome"))
+                {
+                  if (ippContainsString(print_color_mode_sup, "auto-monochrome"))
+                    keyword = "auto-monochrome";
+                  else if (ippContainsString(print_color_mode_sup, "process-monochrome") && !ippContainsString(print_color_mode_sup, "monochrome"))
+                    keyword = "process-monochrome";
+                }
+
                 ippAddString(request, IPP_TAG_JOB, value_tag, mandatory,
                              NULL, keyword);
                 break;
@@ -2732,22 +2733,32 @@ new_request(
        ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "output-bin",
                     NULL, keyword);
 
-      color_attr_name = print_color_mode ? "print-color-mode" : "output-mode";
+      color_attr_name = print_color_mode_sup ? "print-color-mode" : "output-mode";
 
       if ((keyword = cupsGetOption("print-color-mode", num_options,
-                                  options)) != NULL)
-       ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, color_attr_name,
-                    NULL, keyword);
-      else if ((choice = ppdFindMarkedChoice(ppd, "ColorModel")) != NULL)
+                                  options)) == NULL)
       {
-       if (!_cups_strcasecmp(choice->choice, "Gray"))
-         ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD,
-                      color_attr_name, NULL, "monochrome");
-       else
-         ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD,
-                      color_attr_name, NULL, "color");
+       if ((choice = ppdFindMarkedChoice(ppd, "ColorModel")) != NULL)
+       {
+         if (!_cups_strcasecmp(choice->choice, "Gray"))
+           keyword = "monochrome";
+         else
+           keyword = "color";
+       }
+      }
+
+      if (keyword && !strcmp(keyword, "monochrome"))
+      {
+       if (ippContainsString(print_color_mode_sup, "auto-monochrome"))
+         keyword = "auto-monochrome";
+       else if (ippContainsString(print_color_mode_sup, "process-monochrome") && !ippContainsString(print_color_mode_sup, "monochrome"))
+         keyword = "process-monochrome";
       }
 
+      if (keyword)
+       ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, color_attr_name,
+                    NULL, keyword);
+
       if ((keyword = cupsGetOption("print-quality", num_options,
                                   options)) != NULL)
        ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_ENUM, "print-quality",
@@ -3009,7 +3020,7 @@ quote_string(const char *s,               /* I - String */
   {
     if (*s == '\\' || *s == '\"' || *s == '\'')
     {
-      if (q < (qend - 4))
+      if (qptr < (qend - 4))
       {
        *qptr++ = '\\';
        *qptr++ = '\\';
@@ -3058,16 +3069,14 @@ report_attr(ipp_attribute_t *attr)      /* I - Attribute */
     {
       case IPP_TAG_INTEGER :
       case IPP_TAG_ENUM :
-          snprintf(valptr, sizeof(value) - (valptr - value), "%d",
-                  attr->values[i].integer);
+          snprintf(valptr, sizeof(value) - (size_t)(valptr - value), "%d", attr->values[i].integer);
          valptr += strlen(valptr);
          break;
 
       case IPP_TAG_TEXT :
       case IPP_TAG_NAME :
       case IPP_TAG_KEYWORD :
-          quote_string(attr->values[i].string.text, valptr,
-                       value + sizeof(value) - valptr);
+          quote_string(attr->values[i].string.text, valptr, (size_t)(value + sizeof(value) - valptr));
           valptr += strlen(valptr);
           break;
 
@@ -3318,12 +3327,12 @@ run_as_user(char       *argv[],         /* I - Command-line arguments */
 
   if (response)
   {
-    child_pid = xpc_dictionary_get_int64(response, "child-pid");
+    child_pid = (pid_t)xpc_dictionary_get_int64(response, "child-pid");
 
     xpc_release(response);
 
     if (child_pid)
-      fprintf(stderr, "DEBUG: Child PID=%d.\n", child_pid);
+      fprintf(stderr, "DEBUG: Child PID=%d.\n", (int)child_pid);
     else
     {
       _cupsLangPrintFilter(stderr, "ERROR",
@@ -3369,7 +3378,7 @@ run_as_user(char       *argv[],           /* I - Command-line arguments */
 
   if (response)
   {
-    status = xpc_dictionary_get_int64(response, "status");
+    status = (int)xpc_dictionary_get_int64(response, "status");
 
     if (status == SIGTERM || status == SIGKILL || status == SIGPIPE)
     {
@@ -3568,8 +3577,7 @@ update_reasons(ipp_attribute_t *attr,     /* I - printer-state-reasons or NULL */
               temp = (char *)cupsArrayNext(state_reasons))
            if (!strncmp(temp, "cups-remote-", 12))
            {
-             snprintf(remptr, sizeof(rem) - (remptr - rem), "%s%s", remprefix,
-                      temp);
+             snprintf(remptr, sizeof(rem) - (size_t)(remptr - rem), "%s%s", remprefix, temp);
              remptr    += strlen(remptr);
              remprefix = ",";
 
@@ -3582,8 +3590,7 @@ update_reasons(ipp_attribute_t *attr,     /* I - printer-state-reasons or NULL */
 
         cupsArrayAdd(state_reasons, reason);
 
-        snprintf(addptr, sizeof(add) - (addptr - add), "%s%s", addprefix,
-                reason);
+        snprintf(addptr, sizeof(add) - (size_t)(addptr - add), "%s%s", addprefix, reason);
        addptr    += strlen(addptr);
        addprefix = ",";
       }
@@ -3601,8 +3608,7 @@ update_reasons(ipp_attribute_t *attr,     /* I - printer-state-reasons or NULL */
     {
       if (cupsArrayFind(state_reasons, reason))
       {
-       snprintf(remptr, sizeof(rem) - (remptr - rem), "%s%s", remprefix,
-                reason);
+       snprintf(remptr, sizeof(rem) - (size_t)(remptr - rem), "%s%s", remprefix, reason);
        remptr    += strlen(remptr);
        remprefix = ",";
 
@@ -3622,8 +3628,7 @@ update_reasons(ipp_attribute_t *attr,     /* I - printer-state-reasons or NULL */
     {
       if (strncmp(reason, "cups-", 5) && !cupsArrayFind(new_reasons, reason))
       {
-       snprintf(remptr, sizeof(rem) - (remptr - rem), "%s%s", remprefix,
-                reason);
+       snprintf(remptr, sizeof(rem) - (size_t)(remptr - rem), "%s%s", remprefix, reason);
        remptr    += strlen(remptr);
        remprefix = ",";
 
@@ -3639,8 +3644,7 @@ update_reasons(ipp_attribute_t *attr,     /* I - printer-state-reasons or NULL */
       {
         cupsArrayAdd(state_reasons, reason);
 
-        snprintf(addptr, sizeof(add) - (addptr - add), "%s%s", addprefix,
-                reason);
+        snprintf(addptr, sizeof(add) - (size_t)(addptr - add), "%s%s", addprefix, reason);
        addptr    += strlen(addptr);
        addprefix = ",";
       }