]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - test/ipptool.c
Fix build errors on Fedora.
[thirdparty/cups.git] / test / ipptool.c
index bb101ae3f1fedd3f0455388943a8fda8e2d951b4..00124c1f0d48b953921f046e3ba62c779c5ecbdb 100644 (file)
@@ -1,48 +1,18 @@
 /*
  * "$Id$"
  *
- *   ipptool command for CUPS.
+ * ipptool command for CUPS.
  *
- *   Copyright 2007-2013 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/".
+ * 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/".
  *
- *   This file is subject to the Apple OS-Developed Software exception.
- *
- * Contents:
- *
- *   main()              - Parse options and do tests.
- *   add_stringf()       - Add a formatted string to an array.
- *   compare_vars()      - Compare two variables.
- *   do_tests()          - Do tests as specified in the test file.
- *   expand_variables()  - Expand variables in a string.
- *   expect_matches()    - Return true if the tag matches the specification.
- *   get_collection()    - Get a collection value from the current test file.
- *   get_filename()      - Get a filename based on the current test file.
- *   get_token()         - Get a token from a file.
- *   get_variable()      - Get the value of a variable.
- *   iso_date()          - Return an ISO 8601 date/time string for the given IPP
- *                         dateTime value.
- *   password_cb()       - Password callback for authenticated tests.
- *   print_attr()        - Print an attribute on the screen.
- *   print_col()         - Print a collection attribute on the screen.
- *   print_csv()         - Print a line of CSV text.
- *   print_fatal_error() - Print a fatal error message.
- *   print_line()        - Print a line of formatted or CSV text.
- *   print_xml_header()  - Print a standard XML plist header.
- *   print_xml_string()  - Print an XML string with escaping.
- *   print_xml_trailer() - Print the XML trailer with success/fail value.
- *   set_variable()      - Set a variable value.
- *   sigterm_handler()   - Handle SIGINT and SIGTERM.
- *   timeout_cb()        - Handle HTTP timeouts.
- *   usage()             - Show program usage.
- *   validate_attr()     - Determine whether an attribute is valid.
- *   with_value()        - Test a WITH-VALUE predicate.
+ * This file is subject to the Apple OS-Developed Software exception.
  */
 
 /*
@@ -153,11 +123,11 @@ typedef struct _cups_vars_s               /**** Set of variables ****/
  * Globals...
  */
 
-_cups_transfer_t Transfer = _CUPS_TRANSFER_AUTO;
+static _cups_transfer_t Transfer = _CUPS_TRANSFER_AUTO;
                                        /* How to transfer requests */
-_cups_output_t Output = _CUPS_OUTPUT_LIST;
+static _cups_output_t  Output = _CUPS_OUTPUT_LIST;
                                        /* Output mode */
-int            Cancel = 0,             /* Cancel test? */
+static int     Cancel = 0,             /* Cancel test? */
                IgnoreErrors = 0,       /* Ignore errors? */
                StopAfterIncludeError = 0,
                                        /* Stop after include errors? */
@@ -168,22 +138,7 @@ int                Cancel = 0,             /* Cancel test? */
                PassCount = 0,          /* Number of passing tests */
                FailCount = 0,          /* Number of failing tests */
                SkipCount = 0;          /* Number of skipped tests */
-char           *Password = NULL;       /* Password from URI */
-const char * const URIStatusStrings[] =        /* URI status strings */
-{
-  "URI too large",
-  "Bad arguments to function",
-  "Bad resource in URI",
-  "Bad port number in URI",
-  "Bad hostname/address in URI",
-  "Bad username in URI",
-  "Bad scheme in URI",
-  "Bad/empty URI",
-  "OK",
-  "Missing scheme in URI",
-  "Unknown scheme in URI",
-  "Missing resource in URI"
-};
+static char    *Password = NULL;       /* Password from URI */
 
 
 /*
@@ -353,8 +308,9 @@ main(int  argc,                             /* I - Number of command-line args */
 
              if (i >= argc)
              {
-               _cupsLangPuts(stderr,
-                             _("ipptool: Missing timeout for \"-T\"."));
+               _cupsLangPrintf(stderr,
+                               _("%s: Missing timeout for \"-T\"."),
+                               "ipptool");
                usage();
               }
 
@@ -366,8 +322,9 @@ main(int  argc,                             /* I - Number of command-line args */
 
              if (i >= argc)
              {
-               _cupsLangPuts(stderr,
-                             _("ipptool: Missing version for \"-V\"."));
+               _cupsLangPrintf(stderr,
+                               _("%s: Missing version for \"-V\"."),
+                               "ipptool");
                usage();
               }
 
@@ -384,8 +341,8 @@ main(int  argc,                             /* I - Number of command-line args */
              else
              {
                _cupsLangPrintf(stderr,
-                               _("ipptool: Bad version %s for \"-V\"."),
-                               argv[i]);
+                               _("%s: Bad version %s for \"-V\"."),
+                               "ipptool", argv[i]);
                usage();
              }
              break;
@@ -590,7 +547,6 @@ main(int  argc,                             /* I - Number of command-line args */
              _cupsLangPrintf(stderr, _("ipptool: Unknown option \"-%c\"."),
                              *opt);
              usage();
-             break;
        }
       }
     }
@@ -626,8 +582,7 @@ main(int  argc,                             /* I - Number of command-line args */
 
       if (uri_status != HTTP_URI_OK)
       {
-        _cupsLangPrintf(stderr, _("ipptool: Bad URI - %s."),
-                       URIStatusStrings[uri_status - HTTP_URI_OVERFLOW]);
+        _cupsLangPrintf(stderr, _("ipptool: Bad URI - %s."), httpURIStatusString(uri_status));
         return (1);
       }
 
@@ -691,7 +646,7 @@ main(int  argc,                             /* I - Number of command-line args */
   {
     while (repeat > 1)
     {
-      usleep(interval);
+      usleep((useconds_t)interval);
       do_tests(&vars, testfile);
       repeat --;
     }
@@ -700,7 +655,7 @@ main(int  argc,                             /* I - Number of command-line args */
   {
     for (;;)
     {
-      usleep(interval);
+      usleep((useconds_t)interval);
       do_tests(&vars, testfile);
     }
   }
@@ -1290,39 +1245,8 @@ do_tests(_cups_vars_t *vars,             /* I - Variables */
 
        if (col)
        {
-         ipp_attribute_t *tempcol;     /* Pointer to new buffer */
-
-
-        /*
-         * Reallocate memory...
-         */
-
-         if ((tempcol = realloc(lastcol, sizeof(ipp_attribute_t) +
-                                         (lastcol->num_values + 1) *
-                                         sizeof(_ipp_value_t))) == NULL)
-         {
-           print_fatal_error("Unable to allocate memory on line %d.", linenum);
-           pass = 0;
-           goto test_exit;
-         }
-
-         if (tempcol != lastcol)
-         {
-          /*
-           * Reset pointers in the list...
-           */
-
-           if (request->prev)
-             request->prev->next = tempcol;
-           else
-             request->attrs = tempcol;
-
-           lastcol = request->current = request->last = tempcol;
-         }
-
-         lastcol->values[lastcol->num_values].collection = col;
-         lastcol->num_values ++;
-       }
+          ippSetCollection(request, &lastcol, ippGetCount(lastcol), col);
+        }
        else
        {
          pass = 0;
@@ -1630,7 +1554,7 @@ do_tests(_cups_vars_t *vars,              /* I - Variables */
        expand_variables(vars, token, temp, sizeof(token));
 
        if ((op = ippOpValue(token)) == (ipp_op_t)-1 &&
-           (op = strtol(token, NULL, 0)) == 0)
+           (op = (ipp_op_t)strtol(token, NULL, 0)) == 0)
        {
          print_fatal_error("Bad OPERATION code \"%s\" on line %d.", token,
                            linenum);
@@ -1692,7 +1616,7 @@ do_tests(_cups_vars_t *vars,              /* I - Variables */
          if (Output == _CUPS_OUTPUT_TEST)
            printf("    [%g second delay]\n", delay);
 
-         usleep((int)(1000000.0 * delay));
+         usleep((useconds_t)(1000000.0 * delay));
        }
       }
       else if (!_cups_strcasecmp(token, "ATTR"))
@@ -1739,20 +1663,19 @@ do_tests(_cups_vars_t *vars,            /* I - Variables */
              if (!_cups_strcasecmp(token, "true"))
                attrptr = ippAddBoolean(request, group, attr, 1);
               else
-               attrptr = ippAddBoolean(request, group, attr, atoi(token));
+               attrptr = ippAddBoolean(request, group, attr, (char)atoi(token));
              break;
 
          case IPP_TAG_INTEGER :
          case IPP_TAG_ENUM :
              if (!strchr(token, ','))
-               attrptr = ippAddInteger(request, group, value, attr,
-                                       strtol(token, &tokenptr, 0));
+               attrptr = ippAddInteger(request, group, value, attr, (int)strtol(token, &tokenptr, 0));
              else
              {
                int     values[100],    /* Values */
                        num_values = 1; /* Number of values */
 
-               values[0] = strtol(token, &tokenptr, 10);
+               values[0] = (int)strtol(token, &tokenptr, 10);
                while (tokenptr && *tokenptr &&
                       num_values < (int)(sizeof(values) / sizeof(values[0])))
                {
@@ -1761,7 +1684,7 @@ do_tests(_cups_vars_t *vars,              /* I - Variables */
                  else if (!isdigit(*tokenptr & 255) && *tokenptr != '-')
                    break;
 
-                 values[num_values] = strtol(tokenptr, &tokenptr, 0);
+                 values[num_values] = (int)strtol(tokenptr, &tokenptr, 0);
                  num_values ++;
                }
 
@@ -1783,11 +1706,11 @@ do_tests(_cups_vars_t *vars,            /* I - Variables */
                        yres;           /* Y resolution */
                char    *ptr;           /* Pointer into value */
 
-               xres = yres = strtol(token, (char **)&ptr, 10);
+               xres = yres = (int)strtol(token, (char **)&ptr, 10);
                if (ptr > token && xres > 0)
                {
                  if (*ptr == 'x')
-                 yres = strtol(ptr + 1, (char **)&ptr, 10);
+                   yres = (int)strtol(ptr + 1, (char **)&ptr, 10);
                }
 
                if (ptr <= token || xres <= 0 || yres <= 0 || !ptr ||
@@ -1803,15 +1726,12 @@ do_tests(_cups_vars_t *vars,            /* I - Variables */
                }
 
                if (!_cups_strcasecmp(ptr, "dpi"))
-                 attrptr = ippAddResolution(request, group, attr, IPP_RES_PER_INCH,
-                                            xres, yres);
+                 attrptr = ippAddResolution(request, group, attr, IPP_RES_PER_INCH, xres, yres);
                else if (!_cups_strcasecmp(ptr, "dpc") ||
                         !_cups_strcasecmp(ptr, "dpcm"))
-                 attrptr = ippAddResolution(request, group, attr, IPP_RES_PER_CM,
-                                            xres, yres);
+                 attrptr = ippAddResolution(request, group, attr, IPP_RES_PER_CM, xres, yres);
                else
-                 attrptr = ippAddResolution(request, group, attr, (ipp_res_t)0,
-                                            xres, yres);
+                 attrptr = ippAddResolution(request, group, attr, (ipp_res_t)0, xres, yres);
              }
              break;
 
@@ -1868,8 +1788,7 @@ do_tests(_cups_vars_t *vars,              /* I - Variables */
              break;
 
           case IPP_TAG_STRING :
-              attrptr = ippAddOctetString(request, group, attr, token,
-                                          strlen(token));
+              attrptr = ippAddOctetString(request, group, attr, token, (int)strlen(token));
              break;
 
          default :
@@ -1977,7 +1896,7 @@ do_tests(_cups_vars_t *vars,              /* I - Variables */
 
        if ((statuses[num_statuses].status = ippErrorValue(token))
                == (ipp_status_t)-1 &&
-           (statuses[num_statuses].status = strtol(token, NULL, 0)) == 0)
+           (statuses[num_statuses].status = (ipp_status_t)strtol(token, NULL, 0)) == 0)
        {
          print_fatal_error("Bad STATUS code \"%s\" on line %d.", token,
                            linenum);
@@ -2334,11 +2253,11 @@ do_tests(_cups_vars_t *vars,            /* I - Variables */
            * WITH-VALUE is a POSIX extended regular expression.
            */
 
-           last_expect->with_value = calloc(1, tokenptr - token);
+           last_expect->with_value = calloc(1, (size_t)(tokenptr - token));
            last_expect->with_flags |= _CUPS_WITH_REGEX;
 
            if (last_expect->with_value)
-             memcpy(last_expect->with_value, token + 1, tokenptr - token - 1);
+             memcpy(last_expect->with_value, token + 1, (size_t)(tokenptr - token - 1));
          }
          else
          {
@@ -2410,10 +2329,9 @@ do_tests(_cups_vars_t *vars,             /* I - Variables */
 
     TestCount ++;
 
-    request->request.op.version[0]   = version / 10;
-    request->request.op.version[1]   = version % 10;
-    request->request.op.operation_id = op;
-    request->request.op.request_id   = request_id;
+    ippSetVersion(request, version / 10, version % 10);
+    ippSetOperation(request, op);
+    ippSetRequestId(request, request_id);
 
     if (Output == _CUPS_OUTPUT_PLIST)
     {
@@ -2520,7 +2438,7 @@ do_tests(_cups_vars_t *vars,              /* I - Variables */
          */
 
          while ((bytes = cupsFileRead(reqfile, buffer, sizeof(buffer))) > 0)
-           length += bytes;
+           length += (size_t)bytes;
 
          cupsFileClose(reqfile);
        }
@@ -2551,10 +2469,8 @@ do_tests(_cups_vars_t *vars,             /* I - Variables */
            if ((reqfile = cupsFileOpen(filename, "r")) != NULL)
            {
              while (!Cancel &&
-                    (bytes = cupsFileRead(reqfile, buffer,
-                                          sizeof(buffer))) > 0)
-               if ((status = cupsWriteRequestData(http, buffer,
-                                                  bytes)) != HTTP_CONTINUE)
+                    (bytes = cupsFileRead(reqfile, buffer, sizeof(buffer))) > 0)
+               if ((status = cupsWriteRequestData(http, buffer, (size_t)bytes)) != HTTP_CONTINUE)
                  break;
 
              cupsFileClose(reqfile);
@@ -3086,7 +3002,7 @@ do_tests(_cups_vars_t *vars,              /* I - Variables */
           fflush(stdout);
         }
 
-        sleep(repeat_interval);
+        sleep((unsigned)repeat_interval);
         repeat_interval = _cupsNextDelay(repeat_interval, &repeat_prev);
 
        if (Output == _CUPS_OUTPUT_TEST)
@@ -3432,7 +3348,7 @@ expand_variables(_cups_vars_t *vars,      /* I - Variables */
 
       if (value)
       {
-        strlcpy(dstptr, value, dstend - dstptr + 1);
+        strlcpy(dstptr, value, (size_t)(dstend - dstptr + 1));
        dstptr += strlen(dstptr);
       }
     }
@@ -3538,39 +3454,7 @@ get_collection(_cups_vars_t *vars,       /* I  - Variables */
                                        /* Collection value */
 
       if (subcol)
-      {
-       ipp_attribute_t *tempcol;       /* Pointer to new buffer */
-
-
-       /*
-       * Reallocate memory...
-       */
-
-       if ((tempcol = realloc(lastcol, sizeof(ipp_attribute_t) +
-                                       (lastcol->num_values + 1) *
-                                       sizeof(_ipp_value_t))) == NULL)
-       {
-         print_fatal_error("Unable to allocate memory on line %d.", *linenum);
-         goto col_error;
-       }
-
-       if (tempcol != lastcol)
-       {
-        /*
-         * Reset pointers in the list...
-         */
-
-         if (col->prev)
-           col->prev->next = tempcol;
-         else
-           col->attrs = tempcol;
-
-         lastcol = col->current = col->last = tempcol;
-       }
-
-       lastcol->values[lastcol->num_values].collection = subcol;
-       lastcol->num_values ++;
-      }
+        ippSetCollection(col, &lastcol, ippGetCount(lastcol), subcol);
       else
        goto col_error;
     }
@@ -3615,7 +3499,7 @@ get_collection(_cups_vars_t *vars,        /* I  - Variables */
            if (!_cups_strcasecmp(token, "true"))
              ippAddBoolean(col, IPP_TAG_ZERO, attr, 1);
            else
-             ippAddBoolean(col, IPP_TAG_ZERO, attr, atoi(token));
+             ippAddBoolean(col, IPP_TAG_ZERO, attr, (char)atoi(token));
            break;
 
        case IPP_TAG_INTEGER :
@@ -3641,15 +3525,12 @@ get_collection(_cups_vars_t *vars,      /* I  - Variables */
              }
 
              if (!_cups_strcasecmp(units, "dpi"))
-               ippAddResolution(col, IPP_TAG_ZERO, attr, xres, yres,
-                                IPP_RES_PER_INCH);
+               ippAddResolution(col, IPP_TAG_ZERO, attr, IPP_RES_PER_INCH, xres, yres);
              else if (!_cups_strcasecmp(units, "dpc") ||
                       !_cups_strcasecmp(units, "dpcm"))
-               ippAddResolution(col, IPP_TAG_ZERO, attr, xres, yres,
-                                IPP_RES_PER_CM);
+               ippAddResolution(col, IPP_TAG_ZERO, attr, IPP_RES_PER_CM, xres, yres);
              else
-               ippAddResolution(col, IPP_TAG_ZERO, attr, xres, yres,
-                                (ipp_res_t)0);
+               ippAddResolution(col, IPP_TAG_ZERO, attr, (ipp_res_t)0, xres, yres);
            }
            break;
 
@@ -3699,7 +3580,7 @@ get_collection(_cups_vars_t *vars,        /* I  - Variables */
            }
            break;
        case IPP_TAG_STRING :
-           ippAddOctetString(col, IPP_TAG_ZERO, attr, token, strlen(token));
+           ippAddOctetString(col, IPP_TAG_ZERO, attr, token, (int)strlen(token));
            break;
 
        default :
@@ -3798,7 +3679,7 @@ get_filename(const char *testfile,        /* I - Current test file */
     else
       dstptr = dst; /* Should never happen */
 
-    strlcpy(dstptr, src, dstsize - (dstptr - dst));
+    strlcpy(dstptr, src, dstsize - (size_t)(dstptr - dst));
   }
 
   return (dst);
@@ -3828,30 +3709,24 @@ get_string(ipp_attribute_t *attr,       /* I - IPP attribute */
 
   if (flags & _CUPS_WITH_HOSTNAME)
   {
-    if (httpSeparateURI(HTTP_URI_CODING_ALL, ptr, scheme, sizeof(scheme),
-                        userpass, sizeof(userpass), buffer, bufsize, &port,
-                        resource, sizeof(resource)) < HTTP_URI_STATUS_OK)
-      return ("");
-    else
-      return (buffer);
+    if (httpSeparateURI(HTTP_URI_CODING_ALL, ptr, scheme, sizeof(scheme), userpass, sizeof(userpass), buffer, (int)bufsize, &port, resource, sizeof(resource)) < HTTP_URI_STATUS_OK)
+      buffer[0] = '\0';
+
+    return (buffer);
   }
   else if (flags & _CUPS_WITH_RESOURCE)
   {
-    if (httpSeparateURI(HTTP_URI_CODING_ALL, ptr, scheme, sizeof(scheme),
-                        userpass, sizeof(userpass), hostname, sizeof(hostname),
-                        &port, buffer, bufsize) < HTTP_URI_STATUS_OK)
-      return ("");
-    else
-      return (buffer);
+    if (httpSeparateURI(HTTP_URI_CODING_ALL, ptr, scheme, sizeof(scheme), userpass, sizeof(userpass), hostname, sizeof(hostname), &port, buffer, (int)bufsize) < HTTP_URI_STATUS_OK)
+      buffer[0] = '\0';
+
+    return (buffer);
   }
   else if (flags & _CUPS_WITH_SCHEME)
   {
-    if (httpSeparateURI(HTTP_URI_CODING_ALL, ptr, buffer, bufsize,
-                        userpass, sizeof(userpass), hostname, sizeof(hostname),
-                        &port, resource, sizeof(resource)) < HTTP_URI_STATUS_OK)
-      return ("");
-    else
-      return (buffer);
+    if (httpSeparateURI(HTTP_URI_CODING_ALL, ptr, buffer, (int)bufsize, userpass, sizeof(userpass), hostname, sizeof(hostname), &port, resource, sizeof(resource)) < HTTP_URI_STATUS_OK)
+      buffer[0] = '\0';
+
+    return (buffer);
   }
   else
     return (ptr);
@@ -3911,15 +3786,15 @@ get_token(FILE *fp,                     /* I  - File to read from */
          */
 
          if (bufptr < bufend)
-           *bufptr++ = ch;
+           *bufptr++ = (char)ch;
 
          if ((ch = getc(fp)) != EOF && bufptr < bufend)
-           *bufptr++ = ch;
+           *bufptr++ = (char)ch;
        }
        else if (ch == quote)
           break;
        else if (bufptr < bufend)
-          *bufptr++ = ch;
+          *bufptr++ = (char)ch;
       }
 
       *bufptr = '\0';
@@ -3953,7 +3828,7 @@ get_token(FILE *fp,                       /* I  - File to read from */
        if (isspace(ch) || ch == '#')
           break;
        else if (bufptr < bufend)
-          *bufptr++ = ch;
+          *bufptr++ = (char)ch;
 
       if (ch == '#')
         ungetc(ch, fp);
@@ -4750,7 +4625,9 @@ timeout_cb(http_t *http,          /* I - Connection to server */
   int          buffered = 0;           /* Bytes buffered but not yet sent */
 
 
- /*
+  (void)user_data;
+
+  /*
   * If the socket still have data waiting to be sent to the printer (as can
   * happen if the printer runs out of paper), continue to wait until the output
   * buffer is empty...
@@ -4768,7 +4645,6 @@ timeout_cb(http_t *http,          /* I - Connection to server */
 
 #else                                  /* Windows (not possible) */
   (void)http;
-  (void)user_data;
 #endif /* SO_NWRITE */
 
   return (buffered > 0);
@@ -5277,8 +5153,7 @@ validate_attr(cups_array_t    *errors,    /* I - Errors array */
                        "\"%s\": Bad URI value \"%s\" - %s "
                        "(RFC 2911 section 4.1.5).", attr->name,
                        attr->values[i].string.text,
-                       URIStatusStrings[uri_status -
-                                        HTTP_URI_OVERFLOW]);
+                       httpURIStatusString(uri_status));
          }
 
          if (strlen(attr->values[i].string.text) > (IPP_MAX_URI - 1))
@@ -5539,7 +5414,7 @@ with_value(cups_array_t    *errors,       /* I - Errors array */
             if (!*valptr)
              break;
 
-           intvalue = strtol(valptr, &nextptr, 0);
+           intvalue = (int)strtol(valptr, &nextptr, 0);
            if (nextptr == valptr)
              break;
            valptr = nextptr;
@@ -5549,8 +5424,7 @@ with_value(cups_array_t    *errors,       /* I - Errors array */
                 (op == '>' && attr->values[i].integer > intvalue))
            {
              if (!matchbuf[0])
-               snprintf(matchbuf, matchlen, "%d",
-                        attr->values[i].integer);
+               snprintf(matchbuf, matchlen, "%d", attr->values[i].integer);
 
              valmatch = 1;
              break;
@@ -5605,7 +5479,7 @@ with_value(cups_array_t    *errors,       /* I - Errors array */
             if (!*valptr)
              break;
 
-           intvalue = strtol(valptr, &nextptr, 0);
+           intvalue = (int)strtol(valptr, &nextptr, 0);
            if (nextptr == valptr)
              break;
            valptr = nextptr;