4 * ipptool command for CUPS.
6 * Copyright 2007-2012 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * main() - Parse options and do tests.
20 * add_stringf() - Add a formatted string to an array.
21 * compare_vars() - Compare two variables.
22 * do_tests() - Do tests as specified in the test file.
23 * expand_variables() - Expand variables in a string.
24 * expect_matches() - Return true if the tag matches the specification.
25 * get_collection() - Get a collection value from the current test file.
26 * get_filename() - Get a filename based on the current test file.
27 * get_token() - Get a token from a file.
28 * get_variable() - Get the value of a variable.
29 * iso_date() - Return an ISO 8601 date/time string for the given IPP
31 * password_cb() - Password callback for authenticated tests.
32 * print_attr() - Print an attribute on the screen.
33 * print_col() - Print a collection attribute on the screen.
34 * print_csv() - Print a line of CSV text.
35 * print_fatal_error() - Print a fatal error message.
36 * print_line() - Print a line of formatted or CSV text.
37 * print_xml_header() - Print a standard XML plist header.
38 * print_xml_string() - Print an XML string with escaping.
39 * print_xml_trailer() - Print the XML trailer with success/fail value.
40 * set_variable() - Set a variable value.
41 * sigterm_handler() - Handle SIGINT and SIGTERM.
42 * timeout_cb() - Handle HTTP timeouts.
43 * usage() - Show program usage.
44 * validate_attr() - Determine whether an attribute is valid.
45 * with_value() - Test a WITH-VALUE predicate.
49 * Include necessary headers...
52 #include <cups/cups-private.h>
53 #include <cups/file-private.h>
61 #endif /* !O_BINARY */
68 typedef enum _cups_transfer_e
/**** How to send request data ****/
70 _CUPS_TRANSFER_AUTO
, /* Chunk for files, length for static */
71 _CUPS_TRANSFER_CHUNKED
, /* Chunk always */
72 _CUPS_TRANSFER_LENGTH
/* Length always */
75 typedef enum _cups_output_e
/**** Output mode ****/
77 _CUPS_OUTPUT_QUIET
, /* No output */
78 _CUPS_OUTPUT_TEST
, /* Traditional CUPS test output */
79 _CUPS_OUTPUT_PLIST
, /* XML plist test output */
80 _CUPS_OUTPUT_LIST
, /* Tabular list output */
81 _CUPS_OUTPUT_CSV
/* Comma-separated values output */
84 typedef enum _cups_with_e
/**** WITH flags ****/
86 _CUPS_WITH_LITERAL
= 0, /* Match string is a literal value */
87 _CUPS_WITH_ALL
= 1, /* Must match all values */
88 _CUPS_WITH_REGEX
= 2 /* Match string is a regular expression */
91 typedef struct _cups_expect_s
/**** Expected attribute info ****/
93 int optional
, /* Optional attribute? */
94 not_expect
; /* Don't expect attribute? */
95 char *name
, /* Attribute name */
96 *of_type
, /* Type name */
97 *same_count_as
, /* Parallel attribute name */
98 *if_defined
, /* Only required if variable defined */
99 *if_not_defined
, /* Only required if variable is not defined */
100 *with_value
, /* Attribute must include this value */
101 *define_match
, /* Variable to define on match */
102 *define_no_match
, /* Variable to define on no-match */
103 *define_value
; /* Variable to define with value */
104 int repeat_limit
, /* Maximum number of times to repeat */
105 repeat_match
, /* Repeat test on match */
106 repeat_no_match
, /* Repeat test on no match */
107 with_flags
, /* WITH flags */
108 count
; /* Expected count if > 0 */
109 ipp_tag_t in_group
; /* IN-GROUP value */
112 typedef struct _cups_status_s
/**** Status info ****/
114 ipp_status_t status
; /* Expected status code */
115 char *if_defined
, /* Only if variable is defined */
116 *if_not_defined
; /* Only if variable is not defined */
117 int repeat_limit
, /* Maximum number of times to repeat */
118 repeat_match
, /* Repeat the test when it does not match */
119 repeat_no_match
; /* Repeat the test when it matches */
122 typedef struct _cups_var_s
/**** Variable ****/
124 char *name
, /* Name of variable */
125 *value
; /* Value of variable */
128 typedef struct _cups_vars_s
/**** Set of variables ****/
130 char *uri
, /* URI for printer */
131 *filename
, /* Filename */
132 scheme
[64], /* Scheme from URI */
133 userpass
[256], /* Username/password from URI */
134 hostname
[256], /* Hostname from URI */
135 resource
[1024]; /* Resource path from URI */
136 int port
; /* Port number from URI */
137 http_encryption_t encryption
; /* Encryption for connection? */
138 double timeout
; /* Timeout for connection */
139 int family
; /* Address family */
140 cups_array_t
*vars
; /* Array of variables */
148 _cups_transfer_t Transfer
= _CUPS_TRANSFER_AUTO
;
149 /* How to transfer requests */
150 _cups_output_t Output
= _CUPS_OUTPUT_LIST
;
152 int Cancel
= 0, /* Cancel test? */
153 IgnoreErrors
= 0, /* Ignore errors? */
154 Verbosity
= 0, /* Show all attributes? */
155 Version
= 11, /* Default IPP version */
156 XMLHeader
= 0, /* 1 if header is written */
157 TestCount
= 0, /* Number of tests run */
158 PassCount
= 0, /* Number of passing tests */
159 FailCount
= 0, /* Number of failing tests */
160 SkipCount
= 0; /* Number of skipped tests */
161 char *Password
= NULL
; /* Password from URI */
162 const char * const URIStatusStrings
[] = /* URI status strings */
165 "Bad arguments to function",
166 "Bad resource in URI",
167 "Bad port number in URI",
168 "Bad hostname/address in URI",
169 "Bad username in URI",
173 "Missing scheme in URI",
174 "Unknown scheme in URI",
175 "Missing resource in URI"
183 static void add_stringf(cups_array_t
*a
, const char *s
, ...)
184 __attribute__ ((__format__ (__printf__
, 2, 3)));
185 static int compare_vars(_cups_var_t
*a
, _cups_var_t
*b
);
186 static int do_tests(_cups_vars_t
*vars
, const char *testfile
);
187 static void expand_variables(_cups_vars_t
*vars
, char *dst
, const char *src
,
188 size_t dstsize
) __attribute__((nonnull(1,2,3)));
189 static int expect_matches(_cups_expect_t
*expect
, ipp_tag_t value_tag
);
190 static ipp_t
*get_collection(_cups_vars_t
*vars
, FILE *fp
, int *linenum
);
191 static char *get_filename(const char *testfile
, char *dst
, const char *src
,
193 static char *get_token(FILE *fp
, char *buf
, int buflen
,
195 static char *get_variable(_cups_vars_t
*vars
, const char *name
);
196 static char *iso_date(ipp_uchar_t
*date
);
197 static const char *password_cb(const char *prompt
);
198 static void print_attr(ipp_attribute_t
*attr
, ipp_tag_t
*group
);
199 static void print_col(ipp_t
*col
);
200 static void print_csv(ipp_attribute_t
*attr
, int num_displayed
,
201 char **displayed
, size_t *widths
);
202 static void print_fatal_error(const char *s
, ...)
203 __attribute__ ((__format__ (__printf__
, 1, 2)));
204 static void print_line(ipp_attribute_t
*attr
, int num_displayed
,
205 char **displayed
, size_t *widths
);
206 static void print_xml_header(void);
207 static void print_xml_string(const char *element
, const char *s
);
208 static void print_xml_trailer(int success
, const char *message
);
209 static void set_variable(_cups_vars_t
*vars
, const char *name
,
212 static void sigterm_handler(int sig
);
214 static int timeout_cb(http_t
*http
, void *user_data
);
215 static void usage(void) __attribute__((noreturn
));
216 static int validate_attr(cups_array_t
*errors
, ipp_attribute_t
*attr
);
217 static int with_value(cups_array_t
*errors
, char *value
, int flags
,
218 ipp_attribute_t
*attr
, char *matchbuf
,
223 * 'main()' - Parse options and do tests.
226 int /* O - Exit status */
227 main(int argc
, /* I - Number of command-line args */
228 char *argv
[]) /* I - Command-line arguments */
230 int i
; /* Looping var */
231 int status
; /* Status of tests... */
232 char *opt
, /* Current option */
233 name
[1024], /* Name/value buffer */
234 *value
, /* Pointer to value */
235 filename
[1024], /* Real filename */
236 testname
[1024], /* Real test filename */
237 uri
[1024]; /* Copy of printer URI */
238 const char *ext
, /* Extension on filename */
239 *testfile
; /* Test file to use */
240 int interval
, /* Test interval in microseconds */
241 repeat
; /* Repeat count */
242 _cups_vars_t vars
; /* Variables */
243 http_uri_status_t uri_status
; /* URI separation status */
244 _cups_globals_t
*cg
= _cupsGlobals();
250 * Catch SIGINT and SIGTERM...
253 signal(SIGINT
, sigterm_handler
);
254 signal(SIGTERM
, sigterm_handler
);
258 * Initialize the locale and variables...
261 _cupsSetLocale(argv
);
263 memset(&vars
, 0, sizeof(vars
));
264 vars
.family
= AF_UNSPEC
;
265 vars
.vars
= cupsArrayNew((cups_array_func_t
)compare_vars
, NULL
);
270 * ipptool URI testfile
278 for (i
= 1; i
< argc
; i
++)
280 if (argv
[i
][0] == '-')
282 for (opt
= argv
[i
] + 1; *opt
; opt
++)
286 case '4' : /* Connect using IPv4 only */
287 vars
.family
= AF_INET
;
291 case '6' : /* Connect using IPv6 only */
292 vars
.family
= AF_INET6
;
294 #endif /* AF_INET6 */
296 case 'C' : /* Enable HTTP chunking */
297 Transfer
= _CUPS_TRANSFER_CHUNKED
;
300 case 'E' : /* Encrypt with TLS */
302 vars
.encryption
= HTTP_ENCRYPT_REQUIRED
;
304 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."),
306 #endif /* HAVE_SSL */
309 case 'I' : /* Ignore errors */
313 case 'L' : /* Disable HTTP chunking */
314 Transfer
= _CUPS_TRANSFER_LENGTH
;
317 case 'S' : /* Encrypt with SSL */
319 vars
.encryption
= HTTP_ENCRYPT_ALWAYS
;
321 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."),
323 #endif /* HAVE_SSL */
326 case 'T' : /* Set timeout */
331 _cupsLangPuts(stderr
,
332 _("ipptool: Missing timeout for \"-T\"."));
336 vars
.timeout
= _cupsStrScand(argv
[i
], NULL
, localeconv());
339 case 'V' : /* Set IPP version */
344 _cupsLangPuts(stderr
,
345 _("ipptool: Missing version for \"-V\"."));
349 if (!strcmp(argv
[i
], "1.0"))
351 else if (!strcmp(argv
[i
], "1.1"))
353 else if (!strcmp(argv
[i
], "2.0"))
355 else if (!strcmp(argv
[i
], "2.1"))
357 else if (!strcmp(argv
[i
], "2.2"))
361 _cupsLangPrintf(stderr
,
362 _("ipptool: Bad version %s for \"-V\"."),
368 case 'X' : /* Produce XML output */
369 Output
= _CUPS_OUTPUT_PLIST
;
371 if (interval
|| repeat
)
373 _cupsLangPuts(stderr
, _("ipptool: \"-i\" and \"-n\" are "
374 "incompatible with -X\"."));
379 case 'c' : /* CSV output */
380 Output
= _CUPS_OUTPUT_CSV
;
383 case 'd' : /* Define a variable */
388 _cupsLangPuts(stderr
,
389 _("ipptool: Missing name=value for \"-d\"."));
393 strlcpy(name
, argv
[i
], sizeof(name
));
394 if ((value
= strchr(name
, '=')) != NULL
)
397 value
= name
+ strlen(name
);
399 set_variable(&vars
, name
, value
);
402 case 'f' : /* Set the default test filename */
407 _cupsLangPuts(stderr
,
408 _("ipptool: Missing filename for \"-f\"."));
415 if (access(argv
[i
], 0))
421 snprintf(filename
, sizeof(filename
), "%s.gz", argv
[i
]);
422 if (access(filename
, 0) && filename
[0] != '/'
424 && (!isalpha(filename
[0] & 255) || filename
[1] != ':')
428 snprintf(filename
, sizeof(filename
), "%s/ipptool/%s",
429 cg
->cups_datadir
, argv
[i
]);
430 if (access(filename
, 0))
432 snprintf(filename
, sizeof(filename
), "%s/ipptool/%s.gz",
433 cg
->cups_datadir
, argv
[i
]);
434 if (access(filename
, 0))
435 vars
.filename
= strdup(argv
[i
]);
438 vars
.filename
= strdup(filename
);
441 vars
.filename
= strdup(filename
);
444 vars
.filename
= strdup(argv
[i
]);
446 if ((ext
= strrchr(vars
.filename
, '.')) != NULL
)
449 * Guess the MIME media type based on the extension...
452 if (!_cups_strcasecmp(ext
, ".gif"))
453 set_variable(&vars
, "filetype", "image/gif");
454 else if (!_cups_strcasecmp(ext
, ".htm") ||
455 !_cups_strcasecmp(ext
, ".htm.gz") ||
456 !_cups_strcasecmp(ext
, ".html") ||
457 !_cups_strcasecmp(ext
, ".html.gz"))
458 set_variable(&vars
, "filetype", "text/html");
459 else if (!_cups_strcasecmp(ext
, ".jpg"))
460 set_variable(&vars
, "filetype", "image/jpeg");
461 else if (!_cups_strcasecmp(ext
, ".pcl") ||
462 !_cups_strcasecmp(ext
, ".pcl.gz"))
463 set_variable(&vars
, "filetype", "application/vnd.hp-PCL");
464 else if (!_cups_strcasecmp(ext
, ".pdf"))
465 set_variable(&vars
, "filetype", "application/pdf");
466 else if (!_cups_strcasecmp(ext
, ".png"))
467 set_variable(&vars
, "filetype", "image/png");
468 else if (!_cups_strcasecmp(ext
, ".ps") ||
469 !_cups_strcasecmp(ext
, ".ps.gz"))
470 set_variable(&vars
, "filetype", "application/postscript");
471 else if (!_cups_strcasecmp(ext
, ".ras") ||
472 !_cups_strcasecmp(ext
, ".ras.gz"))
473 set_variable(&vars
, "filetype", "image/pwg-raster");
474 else if (!_cups_strcasecmp(ext
, ".txt") ||
475 !_cups_strcasecmp(ext
, ".txt.gz"))
476 set_variable(&vars
, "filetype", "text/plain");
477 else if (!_cups_strcasecmp(ext
, ".xps"))
478 set_variable(&vars
, "filetype", "application/openxps");
480 set_variable(&vars
, "filetype", "application/octet-stream");
485 * Use the "auto-type" MIME media type...
488 set_variable(&vars
, "filetype", "application/octet-stream");
492 case 'i' : /* Test every N seconds */
497 _cupsLangPuts(stderr
,
498 _("ipptool: Missing seconds for \"-i\"."));
503 interval
= (int)(_cupsStrScand(argv
[i
], NULL
, localeconv()) *
507 _cupsLangPuts(stderr
,
508 _("ipptool: Invalid seconds for \"-i\"."));
513 if (Output
== _CUPS_OUTPUT_PLIST
&& interval
)
515 _cupsLangPuts(stderr
, _("ipptool: \"-i\" and \"-n\" are "
516 "incompatible with -X\"."));
521 case 'l' : /* List as a table */
522 Output
= _CUPS_OUTPUT_LIST
;
525 case 'n' : /* Repeat count */
530 _cupsLangPuts(stderr
,
531 _("ipptool: Missing count for \"-n\"."));
535 repeat
= atoi(argv
[i
]);
537 if (Output
== _CUPS_OUTPUT_PLIST
&& repeat
)
539 _cupsLangPuts(stderr
, _("ipptool: \"-i\" and \"-n\" are "
540 "incompatible with -X\"."));
545 case 'q' : /* Be quiet */
546 Output
= _CUPS_OUTPUT_QUIET
;
549 case 't' : /* CUPS test output */
550 Output
= _CUPS_OUTPUT_TEST
;
553 case 'v' : /* Be verbose */
558 _cupsLangPrintf(stderr
, _("ipptool: Unknown option \"-%c\"."),
565 else if (!strncmp(argv
[i
], "ipp://", 6) || !strncmp(argv
[i
], "http://", 7)
567 || !strncmp(argv
[i
], "ipps://", 7)
568 || !strncmp(argv
[i
], "https://", 8)
569 #endif /* HAVE_SSL */
578 _cupsLangPuts(stderr
, _("ipptool: May only specify a single URI."));
583 if (!strncmp(argv
[i
], "ipps://", 7) || !strncmp(argv
[i
], "https://", 8))
584 vars
.encryption
= HTTP_ENCRYPT_ALWAYS
;
585 #endif /* HAVE_SSL */
588 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
, vars
.uri
,
589 vars
.scheme
, sizeof(vars
.scheme
),
590 vars
.userpass
, sizeof(vars
.userpass
),
591 vars
.hostname
, sizeof(vars
.hostname
),
593 vars
.resource
, sizeof(vars
.resource
));
595 if (uri_status
!= HTTP_URI_OK
)
597 _cupsLangPrintf(stderr
, _("ipptool: Bad URI - %s."),
598 URIStatusStrings
[uri_status
- HTTP_URI_OVERFLOW
]);
602 if (vars
.userpass
[0])
604 if ((Password
= strchr(vars
.userpass
, ':')) != NULL
)
607 cupsSetUser(vars
.userpass
);
608 cupsSetPasswordCB(password_cb
);
609 set_variable(&vars
, "uriuser", vars
.userpass
);
612 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), vars
.scheme
, NULL
,
613 vars
.hostname
, vars
.port
, vars
.resource
);
624 _cupsLangPuts(stderr
, _("ipptool: URI required before test file."));
628 if (access(argv
[i
], 0) && argv
[i
][0] != '/'
630 && (!isalpha(argv
[i
][0] & 255) || argv
[i
][1] != ':')
634 snprintf(testname
, sizeof(testname
), "%s/ipptool/%s", cg
->cups_datadir
,
636 if (access(testname
, 0))
644 if (!do_tests(&vars
, testfile
))
649 if (!vars
.uri
|| !testfile
)
653 * Loop if the interval is set...
656 if (Output
== _CUPS_OUTPUT_PLIST
)
657 print_xml_trailer(!status
, NULL
);
658 else if (interval
> 0 && repeat
> 0)
663 do_tests(&vars
, testfile
);
667 else if (interval
> 0)
672 do_tests(&vars
, testfile
);
675 else if (Output
== _CUPS_OUTPUT_TEST
&& TestCount
> 1)
678 * Show a summary report if there were multiple tests...
681 printf("\nSummary: %d tests, %d passed, %d failed, %d skipped\n"
682 "Score: %d%%\n", TestCount
, PassCount
, FailCount
, SkipCount
,
683 100 * (PassCount
+ SkipCount
) / TestCount
);
695 * 'add_stringf()' - Add a formatted string to an array.
699 add_stringf(cups_array_t
*a
, /* I - Array */
700 const char *s
, /* I - Printf-style format string */
701 ...) /* I - Additional args as needed */
703 char buffer
[10240]; /* Format buffer */
704 va_list ap
; /* Argument pointer */
708 * Don't bother is the array is NULL...
715 * Format the message...
719 vsnprintf(buffer
, sizeof(buffer
), s
, ap
);
723 * Add it to the array...
726 cupsArrayAdd(a
, buffer
);
731 * 'compare_vars()' - Compare two variables.
734 static int /* O - Result of comparison */
735 compare_vars(_cups_var_t
*a
, /* I - First variable */
736 _cups_var_t
*b
) /* I - Second variable */
738 return (_cups_strcasecmp(a
->name
, b
->name
));
743 * 'do_tests()' - Do tests as specified in the test file.
746 static int /* 1 = success, 0 = failure */
747 do_tests(_cups_vars_t
*vars
, /* I - Variables */
748 const char *testfile
) /* I - Test file to use */
750 int i
, /* Looping var */
751 linenum
, /* Current line number */
752 pass
, /* Did we pass the test? */
753 prev_pass
= 1, /* Did we pass the previous test? */
754 request_id
, /* Current request ID */
755 show_header
= 1, /* Show the test header? */
756 ignore_errors
, /* Ignore test failures? */
757 skip_previous
= 0, /* Skip on previous test failure? */
758 repeat_count
, /* Repeat count */
759 repeat_interval
, /* Repeat interval */
760 repeat_prev
, /* Previous repeat interval */
761 repeat_test
; /* Repeat a test? */
762 http_t
*http
= NULL
; /* HTTP connection to server */
763 FILE *fp
= NULL
; /* Test file */
764 char resource
[512], /* Resource for request */
765 token
[1024], /* Token from file */
766 *tokenptr
, /* Pointer into token */
767 temp
[1024], /* Temporary string */
768 buffer
[8192], /* Copy buffer */
769 compression
[16]; /* COMPRESSION value */
770 ipp_t
*request
= NULL
, /* IPP request */
771 *response
= NULL
; /* IPP response */
772 size_t length
; /* Length of IPP request */
773 http_status_t status
; /* HTTP status */
774 cups_file_t
*reqfile
; /* File to send */
775 ssize_t bytes
; /* Bytes read/written */
776 char attr
[128]; /* Attribute name */
777 ipp_op_t op
; /* Operation */
778 ipp_tag_t group
; /* Current group */
779 ipp_tag_t value
; /* Current value type */
780 ipp_attribute_t
*attrptr
, /* Attribute pointer */
781 *found
, /* Found attribute */
782 *lastcol
= NULL
; /* Last collection attribute */
783 char name
[1024]; /* Name of test */
784 char filename
[1024]; /* Filename */
785 _cups_transfer_t transfer
; /* To chunk or not to chunk */
786 int version
, /* IPP version number to use */
787 skip_test
; /* Skip this test? */
788 int num_statuses
= 0; /* Number of valid status codes */
789 _cups_status_t statuses
[100], /* Valid status codes */
790 *last_status
; /* Last STATUS (for predicates) */
791 int num_expects
= 0; /* Number of expected attributes */
792 _cups_expect_t expects
[200], /* Expected attributes */
793 *expect
, /* Current expected attribute */
794 *last_expect
; /* Last EXPECT (for predicates) */
795 int num_displayed
= 0; /* Number of displayed attributes */
796 char *displayed
[200]; /* Displayed attributes */
797 size_t widths
[200]; /* Width of columns */
798 cups_array_t
*a
, /* Duplicate attribute array */
799 *errors
= NULL
; /* Errors array */
800 const char *error
; /* Current error */
804 * Open the test file...
807 if ((fp
= fopen(testfile
, "r")) == NULL
)
809 print_fatal_error("Unable to open test file %s - %s", testfile
,
816 * Connect to the server...
819 if ((http
= _httpCreate(vars
->hostname
, vars
->port
, NULL
, vars
->family
,
820 vars
->encryption
, 1, _HTTP_MODE_CLIENT
)) == NULL
)
822 print_fatal_error("Unable to connect to %s on port %d - %s", vars
->hostname
,
823 vars
->port
, cupsLastErrorString());
828 if (httpReconnect(http
))
830 print_fatal_error("Unable to connect to %s on port %d - %s", vars
->hostname
,
831 vars
->port
, cupsLastErrorString());
836 if (vars
->timeout
> 0.0)
837 httpSetTimeout(http
, vars
->timeout
, timeout_cb
, NULL
);
843 CUPS_SRAND(time(NULL
));
845 errors
= cupsArrayNew3(NULL
, NULL
, NULL
, 0, (cups_acopy_func_t
)strdup
,
846 (cups_afree_func_t
)free
);
849 request_id
= (CUPS_RAND() % 1000) * 137 + 1;
851 while (!Cancel
&& get_token(fp
, token
, sizeof(token
), &linenum
) != NULL
)
854 * Expect an open brace...
857 if (!strcmp(token
, "DEFINE"))
863 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
864 get_token(fp
, temp
, sizeof(temp
), &linenum
))
866 expand_variables(vars
, token
, temp
, sizeof(token
));
867 set_variable(vars
, attr
, token
);
871 print_fatal_error("Missing DEFINE name and/or value on line %d.",
879 else if (!strcmp(token
, "DEFINE-DEFAULT"))
882 * DEFINE-DEFAULT name value
885 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
886 get_token(fp
, temp
, sizeof(temp
), &linenum
))
888 expand_variables(vars
, token
, temp
, sizeof(token
));
889 if (!get_variable(vars
, attr
))
890 set_variable(vars
, attr
, token
);
894 print_fatal_error("Missing DEFINE-DEFAULT name and/or value on line "
902 else if (!strcmp(token
, "IGNORE-ERRORS"))
909 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
910 (!_cups_strcasecmp(temp
, "yes") || !_cups_strcasecmp(temp
, "no")))
912 IgnoreErrors
= !_cups_strcasecmp(temp
, "yes");
916 print_fatal_error("Missing IGNORE-ERRORS value on line %d.", linenum
);
923 else if (!strcmp(token
, "INCLUDE"))
930 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
933 * Map the filename to and then run the tests...
936 if (!do_tests(vars
, get_filename(testfile
, filename
, temp
,
947 print_fatal_error("Missing INCLUDE filename on line %d.", linenum
);
955 else if (!strcmp(token
, "INCLUDE-IF-DEFINED"))
958 * INCLUDE-IF-DEFINED name "filename"
959 * INCLUDE-IF-DEFINED name <filename>
962 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
963 get_token(fp
, temp
, sizeof(temp
), &linenum
))
966 * Map the filename to and then run the tests...
969 if (get_variable(vars
, attr
) &&
970 !do_tests(vars
, get_filename(testfile
, filename
, temp
,
981 print_fatal_error("Missing INCLUDE-IF-DEFINED name or filename on line "
990 else if (!strcmp(token
, "INCLUDE-IF-NOT-DEFINED"))
993 * INCLUDE-IF-NOT-DEFINED name "filename"
994 * INCLUDE-IF-NOT-DEFINED name <filename>
997 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
998 get_token(fp
, temp
, sizeof(temp
), &linenum
))
1001 * Map the filename to and then run the tests...
1004 if (!get_variable(vars
, attr
) &&
1005 !do_tests(vars
, get_filename(testfile
, filename
, temp
,
1016 print_fatal_error("Missing INCLUDE-IF-NOT-DEFINED name or filename on "
1017 "line %d.", linenum
);
1025 else if (!strcmp(token
, "SKIP-IF-DEFINED"))
1028 * SKIP-IF-DEFINED variable
1031 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1033 if (get_variable(vars
, temp
))
1038 print_fatal_error("Missing SKIP-IF-DEFINED variable on line %d.",
1044 else if (!strcmp(token
, "SKIP-IF-NOT-DEFINED"))
1047 * SKIP-IF-NOT-DEFINED variable
1050 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1052 if (!get_variable(vars
, temp
))
1057 print_fatal_error("Missing SKIP-IF-NOT-DEFINED variable on line %d.",
1063 else if (!strcmp(token
, "TRANSFER"))
1071 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1073 if (!strcmp(temp
, "auto"))
1074 Transfer
= _CUPS_TRANSFER_AUTO
;
1075 else if (!strcmp(temp
, "chunked"))
1076 Transfer
= _CUPS_TRANSFER_CHUNKED
;
1077 else if (!strcmp(temp
, "length"))
1078 Transfer
= _CUPS_TRANSFER_LENGTH
;
1081 print_fatal_error("Bad TRANSFER value \"%s\" on line %d.", temp
,
1089 print_fatal_error("Missing TRANSFER value on line %d.", linenum
);
1096 else if (!strcmp(token
, "VERSION"))
1098 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1100 if (!strcmp(temp
, "1.0"))
1102 else if (!strcmp(temp
, "1.1"))
1104 else if (!strcmp(temp
, "2.0"))
1106 else if (!strcmp(temp
, "2.1"))
1108 else if (!strcmp(temp
, "2.2"))
1112 print_fatal_error("Bad VERSION \"%s\" on line %d.", temp
, linenum
);
1119 print_fatal_error("Missing VERSION number on line %d.", linenum
);
1126 else if (strcmp(token
, "{"))
1128 print_fatal_error("Unexpected token %s seen on line %d.", token
, linenum
);
1134 * Initialize things...
1139 if (Output
== _CUPS_OUTPUT_PLIST
)
1141 else if (Output
== _CUPS_OUTPUT_TEST
)
1142 printf("\"%s\":\n", testfile
);
1147 strlcpy(resource
, vars
->resource
, sizeof(resource
));
1152 group
= IPP_TAG_ZERO
;
1153 ignore_errors
= IgnoreErrors
;
1160 transfer
= Transfer
;
1161 compression
[0] = '\0';
1163 strlcpy(name
, testfile
, sizeof(name
));
1164 if (strrchr(name
, '.') != NULL
)
1165 *strrchr(name
, '.') = '\0';
1168 * Parse until we see a close brace...
1171 while (get_token(fp
, token
, sizeof(token
), &linenum
) != NULL
)
1173 if (_cups_strcasecmp(token
, "COUNT") &&
1174 _cups_strcasecmp(token
, "DEFINE-MATCH") &&
1175 _cups_strcasecmp(token
, "DEFINE-NO-MATCH") &&
1176 _cups_strcasecmp(token
, "DEFINE-VALUE") &&
1177 _cups_strcasecmp(token
, "IF-DEFINED") &&
1178 _cups_strcasecmp(token
, "IF-NOT-DEFINED") &&
1179 _cups_strcasecmp(token
, "IN-GROUP") &&
1180 _cups_strcasecmp(token
, "OF-TYPE") &&
1181 _cups_strcasecmp(token
, "REPEAT-LIMIT") &&
1182 _cups_strcasecmp(token
, "REPEAT-MATCH") &&
1183 _cups_strcasecmp(token
, "REPEAT-NO-MATCH") &&
1184 _cups_strcasecmp(token
, "SAME-COUNT-AS") &&
1185 _cups_strcasecmp(token
, "WITH-ALL-VALUES") &&
1186 _cups_strcasecmp(token
, "WITH-VALUE"))
1189 if (_cups_strcasecmp(token
, "IF-DEFINED") &&
1190 _cups_strcasecmp(token
, "IF-NOT-DEFINED") &&
1191 _cups_strcasecmp(token
, "REPEAT-LIMIT") &&
1192 _cups_strcasecmp(token
, "REPEAT-MATCH") &&
1193 _cups_strcasecmp(token
, "REPEAT-NO-MATCH"))
1196 if (!strcmp(token
, "}"))
1198 else if (!strcmp(token
, "{") && lastcol
)
1201 * Another collection value
1204 ipp_t
*col
= get_collection(vars
, fp
, &linenum
);
1205 /* Collection value */
1209 ipp_attribute_t
*tempcol
; /* Pointer to new buffer */
1213 * Reallocate memory...
1216 if ((tempcol
= realloc(lastcol
, sizeof(ipp_attribute_t
) +
1217 (lastcol
->num_values
+ 1) *
1218 sizeof(_ipp_value_t
))) == NULL
)
1220 print_fatal_error("Unable to allocate memory on line %d.", linenum
);
1225 if (tempcol
!= lastcol
)
1228 * Reset pointers in the list...
1232 request
->prev
->next
= tempcol
;
1234 request
->attrs
= tempcol
;
1236 lastcol
= request
->current
= request
->last
= tempcol
;
1239 lastcol
->values
[lastcol
->num_values
].collection
= col
;
1240 lastcol
->num_values
++;
1248 else if (!strcmp(token
, "COMPRESSION"))
1252 * COMPRESSION deflate
1256 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1258 expand_variables(vars
, compression
, temp
, sizeof(compression
));
1260 if (strcmp(compression
, "none") && strcmp(compression
, "deflate") &&
1261 strcmp(compression
, "gzip"))
1263 if (strcmp(compression
, "none"))
1264 #endif /* HAVE_LIBZ */
1266 print_fatal_error("Unsupported COMPRESSION value '%s' on line %d.",
1267 compression
, linenum
);
1272 if (!strcmp(compression
, "none"))
1273 compression
[0] = '\0';
1277 print_fatal_error("Missing COMPRESSION value on line %d.", linenum
);
1282 else if (!strcmp(token
, "DEFINE"))
1288 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
1289 get_token(fp
, temp
, sizeof(temp
), &linenum
))
1291 expand_variables(vars
, token
, temp
, sizeof(token
));
1292 set_variable(vars
, attr
, token
);
1296 print_fatal_error("Missing DEFINE name and/or value on line %d.",
1302 else if (!strcmp(token
, "IGNORE-ERRORS"))
1309 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
1310 (!_cups_strcasecmp(temp
, "yes") || !_cups_strcasecmp(temp
, "no")))
1312 ignore_errors
= !_cups_strcasecmp(temp
, "yes");
1316 print_fatal_error("Missing IGNORE-ERRORS value on line %d.", linenum
);
1323 else if (!_cups_strcasecmp(token
, "NAME"))
1329 get_token(fp
, name
, sizeof(name
), &linenum
);
1331 else if (!strcmp(token
, "REQUEST-ID"))
1338 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1340 if (isdigit(temp
[0] & 255))
1341 request_id
= atoi(temp
);
1342 else if (!_cups_strcasecmp(temp
, "random"))
1343 request_id
= (CUPS_RAND() % 1000) * 137 + 1;
1346 print_fatal_error("Bad REQUEST-ID value \"%s\" on line %d.", temp
,
1354 print_fatal_error("Missing REQUEST-ID value on line %d.", linenum
);
1359 else if (!strcmp(token
, "SKIP-IF-DEFINED"))
1362 * SKIP-IF-DEFINED variable
1365 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1367 if (get_variable(vars
, temp
))
1372 print_fatal_error("Missing SKIP-IF-DEFINED value on line %d.",
1378 else if (!strcmp(token
, "SKIP-IF-NOT-DEFINED"))
1381 * SKIP-IF-NOT-DEFINED variable
1384 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1386 if (!get_variable(vars
, temp
))
1391 print_fatal_error("Missing SKIP-IF-NOT-DEFINED value on line %d.",
1397 else if (!strcmp(token
, "SKIP-PREVIOUS-ERROR"))
1400 * SKIP-PREVIOUS-ERROR yes
1401 * SKIP-PREVIOUS-ERROR no
1404 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
1405 (!_cups_strcasecmp(temp
, "yes") || !_cups_strcasecmp(temp
, "no")))
1407 skip_previous
= !_cups_strcasecmp(temp
, "yes");
1411 print_fatal_error("Missing SKIP-PREVIOUS-ERROR value on line %d.", linenum
);
1418 else if (!strcmp(token
, "TRANSFER"))
1426 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1428 if (!strcmp(temp
, "auto"))
1429 transfer
= _CUPS_TRANSFER_AUTO
;
1430 else if (!strcmp(temp
, "chunked"))
1431 transfer
= _CUPS_TRANSFER_CHUNKED
;
1432 else if (!strcmp(temp
, "length"))
1433 transfer
= _CUPS_TRANSFER_LENGTH
;
1436 print_fatal_error("Bad TRANSFER value \"%s\" on line %d.", temp
,
1444 print_fatal_error("Missing TRANSFER value on line %d.", linenum
);
1449 else if (!_cups_strcasecmp(token
, "VERSION"))
1451 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1453 if (!strcmp(temp
, "0.0"))
1455 else if (!strcmp(temp
, "1.0"))
1457 else if (!strcmp(temp
, "1.1"))
1459 else if (!strcmp(temp
, "2.0"))
1461 else if (!strcmp(temp
, "2.1"))
1463 else if (!strcmp(temp
, "2.2"))
1467 print_fatal_error("Bad VERSION \"%s\" on line %d.", temp
, linenum
);
1474 print_fatal_error("Missing VERSION number on line %d.", linenum
);
1479 else if (!_cups_strcasecmp(token
, "RESOURCE"))
1485 if (!get_token(fp
, resource
, sizeof(resource
), &linenum
))
1487 print_fatal_error("Missing RESOURCE path on line %d.", linenum
);
1492 else if (!_cups_strcasecmp(token
, "OPERATION"))
1498 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1500 print_fatal_error("Missing OPERATION code on line %d.", linenum
);
1505 if ((op
= ippOpValue(token
)) == (ipp_op_t
)-1 &&
1506 (op
= strtol(token
, NULL
, 0)) == 0)
1508 print_fatal_error("Bad OPERATION code \"%s\" on line %d.", token
,
1514 else if (!_cups_strcasecmp(token
, "GROUP"))
1517 * Attribute group...
1520 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1522 print_fatal_error("Missing GROUP tag on line %d.", linenum
);
1527 if ((value
= ippTagValue(token
)) < 0)
1529 print_fatal_error("Bad GROUP tag \"%s\" on line %d.", token
, linenum
);
1535 ippAddSeparator(request
);
1539 else if (!_cups_strcasecmp(token
, "DELAY"))
1542 * Delay before operation...
1547 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1549 print_fatal_error("Missing DELAY value on line %d.", linenum
);
1554 if ((delay
= _cupsStrScand(token
, NULL
, localeconv())) <= 0.0)
1556 print_fatal_error("Bad DELAY value \"%s\" on line %d.", token
,
1563 if (Output
== _CUPS_OUTPUT_TEST
)
1564 printf(" [%g second delay]\n", delay
);
1566 usleep((int)(1000000.0 * delay
));
1569 else if (!_cups_strcasecmp(token
, "ATTR"))
1575 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1577 print_fatal_error("Missing ATTR value tag on line %d.", linenum
);
1582 if ((value
= ippTagValue(token
)) == IPP_TAG_ZERO
)
1584 print_fatal_error("Bad ATTR value tag \"%s\" on line %d.", token
,
1590 if (!get_token(fp
, attr
, sizeof(attr
), &linenum
))
1592 print_fatal_error("Missing ATTR name on line %d.", linenum
);
1597 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1599 print_fatal_error("Missing ATTR value on line %d.", linenum
);
1604 expand_variables(vars
, token
, temp
, sizeof(token
));
1609 case IPP_TAG_BOOLEAN
:
1610 if (!_cups_strcasecmp(token
, "true"))
1611 attrptr
= ippAddBoolean(request
, group
, attr
, 1);
1613 attrptr
= ippAddBoolean(request
, group
, attr
, atoi(token
));
1616 case IPP_TAG_INTEGER
:
1618 if (!strchr(token
, ','))
1619 attrptr
= ippAddInteger(request
, group
, value
, attr
,
1620 strtol(token
, &tokenptr
, 0));
1623 int values
[100], /* Values */
1624 num_values
= 1; /* Number of values */
1626 values
[0] = strtol(token
, &tokenptr
, 10);
1627 while (tokenptr
&& *tokenptr
&&
1628 num_values
< (int)(sizeof(values
) / sizeof(values
[0])))
1630 if (*tokenptr
== ',')
1632 else if (!isdigit(*tokenptr
& 255) && *tokenptr
!= '-')
1635 values
[num_values
] = strtol(tokenptr
, &tokenptr
, 0);
1639 attrptr
= ippAddIntegers(request
, group
, value
, attr
, num_values
, values
);
1642 if (!tokenptr
|| *tokenptr
)
1644 print_fatal_error("Bad %s value \"%s\" on line %d.",
1645 ippTagString(value
), token
, linenum
);
1651 case IPP_TAG_RESOLUTION
:
1653 int xres
, /* X resolution */
1654 yres
; /* Y resolution */
1655 char *ptr
; /* Pointer into value */
1657 xres
= yres
= strtol(token
, (char **)&ptr
, 10);
1658 if (ptr
> token
&& xres
> 0)
1661 yres
= strtol(ptr
+ 1, (char **)&ptr
, 10);
1664 if (ptr
<= token
|| xres
<= 0 || yres
<= 0 || !ptr
||
1665 (_cups_strcasecmp(ptr
, "dpi") &&
1666 _cups_strcasecmp(ptr
, "dpc") &&
1667 _cups_strcasecmp(ptr
, "dpcm") &&
1668 _cups_strcasecmp(ptr
, "other")))
1670 print_fatal_error("Bad resolution value \"%s\" on line %d.",
1676 if (!_cups_strcasecmp(ptr
, "dpi"))
1677 attrptr
= ippAddResolution(request
, group
, attr
, IPP_RES_PER_INCH
,
1679 else if (!_cups_strcasecmp(ptr
, "dpc") ||
1680 !_cups_strcasecmp(ptr
, "dpcm"))
1681 attrptr
= ippAddResolution(request
, group
, attr
, IPP_RES_PER_CM
,
1684 attrptr
= ippAddResolution(request
, group
, attr
, (ipp_res_t
)0,
1689 case IPP_TAG_RANGE
:
1691 int lowers
[4], /* Lower value */
1692 uppers
[4], /* Upper values */
1693 num_vals
; /* Number of values */
1696 num_vals
= sscanf(token
, "%d-%d,%d-%d,%d-%d,%d-%d",
1697 lowers
+ 0, uppers
+ 0,
1698 lowers
+ 1, uppers
+ 1,
1699 lowers
+ 2, uppers
+ 2,
1700 lowers
+ 3, uppers
+ 3);
1702 if ((num_vals
& 1) || num_vals
== 0)
1704 print_fatal_error("Bad rangeOfInteger value \"%s\" on line "
1705 "%d.", token
, linenum
);
1710 attrptr
= ippAddRanges(request
, group
, attr
, num_vals
/ 2, lowers
,
1715 case IPP_TAG_BEGIN_COLLECTION
:
1716 if (!strcmp(token
, "{"))
1718 ipp_t
*col
= get_collection(vars
, fp
, &linenum
);
1719 /* Collection value */
1723 attrptr
= lastcol
= ippAddCollection(request
, group
, attr
, col
);
1734 print_fatal_error("Bad ATTR collection value on line %d.",
1741 case IPP_TAG_STRING
:
1742 attrptr
= ippAddOctetString(request
, group
, attr
, token
,
1747 print_fatal_error("Unsupported ATTR value tag %s on line %d.",
1748 ippTagString(value
), linenum
);
1752 case IPP_TAG_TEXTLANG
:
1753 case IPP_TAG_NAMELANG
:
1756 case IPP_TAG_KEYWORD
:
1758 case IPP_TAG_URISCHEME
:
1759 case IPP_TAG_CHARSET
:
1760 case IPP_TAG_LANGUAGE
:
1761 case IPP_TAG_MIMETYPE
:
1762 if (!strchr(token
, ','))
1763 attrptr
= ippAddString(request
, group
, value
, attr
, NULL
, token
);
1767 * Multiple string values...
1770 int num_values
; /* Number of values */
1771 char *values
[100], /* Values */
1772 *ptr
; /* Pointer to next value */
1778 for (ptr
= strchr(token
, ','); ptr
; ptr
= strchr(ptr
, ','))
1780 if (ptr
> token
&& ptr
[-1] == '\\')
1781 _cups_strcpy(ptr
- 1, ptr
);
1785 values
[num_values
] = ptr
;
1790 attrptr
= ippAddStrings(request
, group
, value
, attr
, num_values
,
1791 NULL
, (const char **)values
);
1798 print_fatal_error("Unable to add attribute on line %d: %s", linenum
,
1799 cupsLastErrorString());
1804 else if (!_cups_strcasecmp(token
, "FILE"))
1810 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1812 print_fatal_error("Missing FILE filename on line %d.", linenum
);
1817 expand_variables(vars
, token
, temp
, sizeof(token
));
1818 get_filename(testfile
, filename
, token
, sizeof(filename
));
1820 else if (!_cups_strcasecmp(token
, "STATUS"))
1826 if (num_statuses
>= (int)(sizeof(statuses
) / sizeof(statuses
[0])))
1828 print_fatal_error("Too many STATUS's on line %d.", linenum
);
1833 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1835 print_fatal_error("Missing STATUS code on line %d.", linenum
);
1840 if ((statuses
[num_statuses
].status
= ippErrorValue(token
))
1841 == (ipp_status_t
)-1 &&
1842 (statuses
[num_statuses
].status
= strtol(token
, NULL
, 0)) == 0)
1844 print_fatal_error("Bad STATUS code \"%s\" on line %d.", token
,
1850 last_status
= statuses
+ num_statuses
;
1853 last_status
->if_defined
= NULL
;
1854 last_status
->if_not_defined
= NULL
;
1855 last_status
->repeat_limit
= 1000;
1856 last_status
->repeat_match
= 0;
1857 last_status
->repeat_no_match
= 0;
1859 else if (!_cups_strcasecmp(token
, "EXPECT"))
1862 * Expected attributes...
1865 if (num_expects
>= (int)(sizeof(expects
) / sizeof(expects
[0])))
1867 print_fatal_error("Too many EXPECT's on line %d.", linenum
);
1872 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1874 print_fatal_error("Missing EXPECT name on line %d.", linenum
);
1879 last_expect
= expects
+ num_expects
;
1882 memset(last_expect
, 0, sizeof(_cups_expect_t
));
1883 last_expect
->repeat_limit
= 1000;
1885 if (token
[0] == '!')
1887 last_expect
->not_expect
= 1;
1888 last_expect
->name
= strdup(token
+ 1);
1890 else if (token
[0] == '?')
1892 last_expect
->optional
= 1;
1893 last_expect
->name
= strdup(token
+ 1);
1896 last_expect
->name
= strdup(token
);
1898 else if (!_cups_strcasecmp(token
, "COUNT"))
1900 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1902 print_fatal_error("Missing COUNT number on line %d.", linenum
);
1907 if ((i
= atoi(token
)) <= 0)
1909 print_fatal_error("Bad COUNT \"%s\" on line %d.", token
, linenum
);
1915 last_expect
->count
= i
;
1918 print_fatal_error("COUNT without a preceding EXPECT on line %d.",
1924 else if (!_cups_strcasecmp(token
, "DEFINE-MATCH"))
1926 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1928 print_fatal_error("Missing DEFINE-MATCH variable on line %d.",
1935 last_expect
->define_match
= strdup(token
);
1938 print_fatal_error("DEFINE-MATCH without a preceding EXPECT on line "
1944 else if (!_cups_strcasecmp(token
, "DEFINE-NO-MATCH"))
1946 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1948 print_fatal_error("Missing DEFINE-NO-MATCH variable on line %d.",
1955 last_expect
->define_no_match
= strdup(token
);
1958 print_fatal_error("DEFINE-NO-MATCH without a preceding EXPECT on "
1959 "line %d.", linenum
);
1964 else if (!_cups_strcasecmp(token
, "DEFINE-VALUE"))
1966 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1968 print_fatal_error("Missing DEFINE-VALUE variable on line %d.",
1975 last_expect
->define_value
= strdup(token
);
1978 print_fatal_error("DEFINE-VALUE without a preceding EXPECT on line "
1984 else if (!_cups_strcasecmp(token
, "OF-TYPE"))
1986 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1988 print_fatal_error("Missing OF-TYPE value tag(s) on line %d.",
1995 last_expect
->of_type
= strdup(token
);
1998 print_fatal_error("OF-TYPE without a preceding EXPECT on line %d.",
2004 else if (!_cups_strcasecmp(token
, "IN-GROUP"))
2006 ipp_tag_t in_group
; /* IN-GROUP value */
2009 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2011 print_fatal_error("Missing IN-GROUP group tag on line %d.", linenum
);
2016 if ((in_group
= ippTagValue(token
)) == (ipp_tag_t
)-1)
2019 else if (last_expect
)
2020 last_expect
->in_group
= in_group
;
2023 print_fatal_error("IN-GROUP without a preceding EXPECT on line %d.",
2029 else if (!_cups_strcasecmp(token
, "REPEAT-LIMIT"))
2031 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2033 print_fatal_error("Missing REPEAT-LIMIT value on line %d.", linenum
);
2037 else if (atoi(token
) <= 0)
2039 print_fatal_error("Bad REPEAT-LIMIT value on line %d.", linenum
);
2045 last_status
->repeat_limit
= atoi(token
);
2046 else if (last_expect
)
2047 last_expect
->repeat_limit
= atoi(token
);
2050 print_fatal_error("REPEAT-LIMIT without a preceding EXPECT or STATUS "
2051 "on line %d.", linenum
);
2056 else if (!_cups_strcasecmp(token
, "REPEAT-MATCH"))
2059 last_status
->repeat_match
= 1;
2060 else if (last_expect
)
2061 last_expect
->repeat_match
= 1;
2064 print_fatal_error("REPEAT-MATCH without a preceding EXPECT or STATUS "
2065 "on line %d.", linenum
);
2070 else if (!_cups_strcasecmp(token
, "REPEAT-NO-MATCH"))
2073 last_status
->repeat_no_match
= 1;
2074 else if (last_expect
)
2075 last_expect
->repeat_no_match
= 1;
2078 print_fatal_error("REPEAT-NO-MATCH without a preceding EXPECT or "
2079 "STATUS on ine %d.", linenum
);
2084 else if (!_cups_strcasecmp(token
, "SAME-COUNT-AS"))
2086 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2088 print_fatal_error("Missing SAME-COUNT-AS name on line %d.", linenum
);
2094 last_expect
->same_count_as
= strdup(token
);
2097 print_fatal_error("SAME-COUNT-AS without a preceding EXPECT on line "
2103 else if (!_cups_strcasecmp(token
, "IF-DEFINED"))
2105 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2107 print_fatal_error("Missing IF-DEFINED name on line %d.", linenum
);
2113 last_expect
->if_defined
= strdup(token
);
2114 else if (last_status
)
2115 last_status
->if_defined
= strdup(token
);
2118 print_fatal_error("IF-DEFINED without a preceding EXPECT or STATUS "
2119 "on line %d.", linenum
);
2124 else if (!_cups_strcasecmp(token
, "IF-NOT-DEFINED"))
2126 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2128 print_fatal_error("Missing IF-NOT-DEFINED name on line %d.", linenum
);
2134 last_expect
->if_not_defined
= strdup(token
);
2135 else if (last_status
)
2136 last_status
->if_not_defined
= strdup(token
);
2139 print_fatal_error("IF-NOT-DEFINED without a preceding EXPECT or STATUS "
2140 "on line %d.", linenum
);
2145 else if (!_cups_strcasecmp(token
, "WITH-ALL-VALUES") ||
2146 !_cups_strcasecmp(token
, "WITH-VALUE"))
2148 if (!_cups_strcasecmp(token
, "WITH-ALL-VALUES") && last_expect
)
2149 last_expect
->with_flags
= _CUPS_WITH_ALL
;
2151 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
2153 print_fatal_error("Missing %s value on line %d.", token
, linenum
);
2161 * Expand any variables in the value and then save it.
2164 expand_variables(vars
, token
, temp
, sizeof(token
));
2166 tokenptr
= token
+ strlen(token
) - 1;
2168 if (token
[0] == '/' && tokenptr
> token
&& *tokenptr
== '/')
2171 * WITH-VALUE is a POSIX extended regular expression.
2174 last_expect
->with_value
= calloc(1, tokenptr
- token
);
2175 last_expect
->with_flags
|= _CUPS_WITH_REGEX
;
2177 if (last_expect
->with_value
)
2178 memcpy(last_expect
->with_value
, token
+ 1, tokenptr
- token
- 1);
2183 * WITH-VALUE is a literal value...
2186 char *ptr
; /* Pointer into value */
2188 for (ptr
= token
; *ptr
; ptr
++)
2190 if (*ptr
== '\\' && ptr
[1])
2193 * Remove \ from \foo...
2196 _cups_strcpy(ptr
, ptr
+ 1);
2200 last_expect
->with_value
= strdup(token
);
2201 last_expect
->with_flags
|= _CUPS_WITH_LITERAL
;
2206 print_fatal_error("%s without a preceding EXPECT on line %d.", token
,
2212 else if (!_cups_strcasecmp(token
, "DISPLAY"))
2215 * Display attributes...
2218 if (num_displayed
>= (int)(sizeof(displayed
) / sizeof(displayed
[0])))
2220 print_fatal_error("Too many DISPLAY's on line %d", linenum
);
2225 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2227 print_fatal_error("Missing DISPLAY name on line %d.", linenum
);
2232 displayed
[num_displayed
] = strdup(token
);
2237 print_fatal_error("Unexpected token %s seen on line %d.", token
,
2245 * Submit the IPP request...
2250 request
->request
.op
.version
[0] = version
/ 10;
2251 request
->request
.op
.version
[1] = version
% 10;
2252 request
->request
.op
.operation_id
= op
;
2253 request
->request
.op
.request_id
= request_id
;
2255 if (Output
== _CUPS_OUTPUT_PLIST
)
2258 puts("<key>Name</key>");
2259 print_xml_string("string", name
);
2260 puts("<key>Operation</key>");
2261 print_xml_string("string", ippOpString(op
));
2262 puts("<key>RequestAttributes</key>");
2267 for (attrptr
= request
->attrs
, group
= attrptr
->group_tag
;
2269 attrptr
= attrptr
->next
)
2270 print_attr(attrptr
, &group
);
2275 else if (Output
== _CUPS_OUTPUT_TEST
)
2279 printf(" %s:\n", ippOpString(op
));
2281 for (attrptr
= request
->attrs
; attrptr
; attrptr
= attrptr
->next
)
2282 print_attr(attrptr
, NULL
);
2285 printf(" %-68.68s [", name
);
2289 if ((skip_previous
&& !prev_pass
) || skip_test
)
2296 if (Output
== _CUPS_OUTPUT_PLIST
)
2298 puts("<key>Successful</key>");
2300 puts("<key>StatusCode</key>");
2301 print_xml_string("string", "skip");
2302 puts("<key>ResponseAttributes</key>");
2306 else if (Output
== _CUPS_OUTPUT_TEST
)
2313 repeat_interval
= 1;
2322 if (transfer
== _CUPS_TRANSFER_CHUNKED
||
2323 (transfer
== _CUPS_TRANSFER_AUTO
&& filename
[0]))
2326 * Send request using chunking - a 0 length means "chunk".
2334 * Send request using content length...
2337 length
= ippLength(request
);
2339 if (filename
[0] && (reqfile
= cupsFileOpen(filename
, "r")) != NULL
)
2342 * Read the file to get the uncompressed file size...
2345 while ((bytes
= cupsFileRead(reqfile
, buffer
, sizeof(buffer
))) > 0)
2348 cupsFileClose(reqfile
);
2353 * Send the request...
2360 if (status
!= HTTP_ERROR
)
2362 while (!response
&& !Cancel
&& prev_pass
)
2364 status
= cupsSendRequest(http
, request
, resource
, length
);
2368 httpSetField(http
, HTTP_FIELD_CONTENT_ENCODING
, compression
);
2369 #endif /* HAVE_LIBZ */
2371 if (!Cancel
&& status
== HTTP_CONTINUE
&&
2372 request
->state
== IPP_DATA
&& filename
[0])
2374 if ((reqfile
= cupsFileOpen(filename
, "r")) != NULL
)
2377 (bytes
= cupsFileRead(reqfile
, buffer
,
2378 sizeof(buffer
))) > 0)
2379 if ((status
= cupsWriteRequestData(http
, buffer
,
2380 bytes
)) != HTTP_CONTINUE
)
2383 cupsFileClose(reqfile
);
2387 snprintf(buffer
, sizeof(buffer
), "%s: %s", filename
,
2389 _cupsSetError(IPP_INTERNAL_ERROR
, buffer
, 0);
2391 status
= HTTP_ERROR
;
2396 * Get the server's response...
2399 if (!Cancel
&& status
!= HTTP_ERROR
)
2401 response
= cupsGetResponse(http
, resource
);
2402 status
= httpGetStatus(http
);
2405 if (!Cancel
&& status
== HTTP_ERROR
&& http
->error
!= EINVAL
&&
2407 http
->error
!= WSAETIMEDOUT
)
2409 http
->error
!= ETIMEDOUT
)
2412 if (httpReconnect(http
))
2415 else if (status
== HTTP_ERROR
)
2420 else if (status
!= HTTP_OK
)
2428 if (!Cancel
&& status
== HTTP_ERROR
&& http
->error
!= EINVAL
&&
2430 http
->error
!= WSAETIMEDOUT
)
2432 http
->error
!= ETIMEDOUT
)
2435 if (httpReconnect(http
))
2438 else if (status
== HTTP_ERROR
)
2441 httpReconnect(http
);
2445 else if (status
!= HTTP_OK
)
2452 * Check results of request...
2455 cupsArrayClear(errors
);
2457 if (http
->version
!= HTTP_1_1
)
2458 add_stringf(errors
, "Bad HTTP version (%d.%d)", http
->version
/ 100,
2459 http
->version
% 100);
2464 * No response, log error...
2467 add_stringf(errors
, "IPP request failed with status %s (%s)",
2468 ippErrorString(cupsLastError()),
2469 cupsLastErrorString());
2474 * Collect common attribute values...
2477 if ((attrptr
= ippFindAttribute(response
, "job-id",
2478 IPP_TAG_INTEGER
)) != NULL
)
2480 snprintf(temp
, sizeof(temp
), "%d", attrptr
->values
[0].integer
);
2481 set_variable(vars
, "job-id", temp
);
2484 if ((attrptr
= ippFindAttribute(response
, "job-uri",
2485 IPP_TAG_URI
)) != NULL
)
2486 set_variable(vars
, "job-uri", attrptr
->values
[0].string
.text
);
2488 if ((attrptr
= ippFindAttribute(response
, "notify-subscription-id",
2489 IPP_TAG_INTEGER
)) != NULL
)
2491 snprintf(temp
, sizeof(temp
), "%d", attrptr
->values
[0].integer
);
2492 set_variable(vars
, "notify-subscription-id", temp
);
2496 * Check response, validating groups and attributes and logging errors
2500 if (response
->state
!= IPP_DATA
)
2502 "Missing end-of-attributes-tag in response "
2503 "(RFC 2910 section 3.5.1)");
2506 (response
->request
.status
.version
[0] != (version
/ 10) ||
2507 response
->request
.status
.version
[1] != (version
% 10)))
2509 "Bad version %d.%d in response - expected %d.%d "
2510 "(RFC 2911 section 3.1.8).",
2511 response
->request
.status
.version
[0],
2512 response
->request
.status
.version
[1],
2513 version
/ 10, version
% 10);
2515 if (response
->request
.status
.request_id
!= request_id
)
2517 "Bad request ID %d in response - expected %d "
2518 "(RFC 2911 section 3.1.1)",
2519 response
->request
.status
.request_id
, request_id
);
2521 attrptr
= response
->attrs
;
2524 "Missing first attribute \"attributes-charset "
2525 "(charset)\" in group operation-attributes-tag "
2526 "(RFC 2911 section 3.1.4).");
2529 if (!attrptr
->name
||
2530 attrptr
->value_tag
!= IPP_TAG_CHARSET
||
2531 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2532 attrptr
->num_values
!= 1 ||
2533 strcmp(attrptr
->name
, "attributes-charset"))
2535 "Bad first attribute \"%s (%s%s)\" in group %s, "
2536 "expected \"attributes-charset (charset)\" in "
2537 "group operation-attributes-tag (RFC 2911 section "
2539 attrptr
->name
? attrptr
->name
: "(null)",
2540 attrptr
->num_values
> 1 ? "1setOf " : "",
2541 ippTagString(attrptr
->value_tag
),
2542 ippTagString(attrptr
->group_tag
));
2544 attrptr
= attrptr
->next
;
2547 "Missing second attribute \"attributes-natural-"
2548 "language (naturalLanguage)\" in group "
2549 "operation-attributes-tag (RFC 2911 section "
2551 else if (!attrptr
->name
||
2552 attrptr
->value_tag
!= IPP_TAG_LANGUAGE
||
2553 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2554 attrptr
->num_values
!= 1 ||
2555 strcmp(attrptr
->name
, "attributes-natural-language"))
2557 "Bad first attribute \"%s (%s%s)\" in group %s, "
2558 "expected \"attributes-natural-language "
2559 "(naturalLanguage)\" in group "
2560 "operation-attributes-tag (RFC 2911 section "
2562 attrptr
->name
? attrptr
->name
: "(null)",
2563 attrptr
->num_values
> 1 ? "1setOf " : "",
2564 ippTagString(attrptr
->value_tag
),
2565 ippTagString(attrptr
->group_tag
));
2568 if ((attrptr
= ippFindAttribute(response
, "status-message",
2569 IPP_TAG_ZERO
)) != NULL
)
2571 if (attrptr
->value_tag
!= IPP_TAG_TEXT
)
2573 "status-message (text(255)) has wrong value tag "
2574 "%s (RFC 2911 section 3.1.6.2).",
2575 ippTagString(attrptr
->value_tag
));
2576 if (attrptr
->group_tag
!= IPP_TAG_OPERATION
)
2578 "status-message (text(255)) has wrong group tag "
2579 "%s (RFC 2911 section 3.1.6.2).",
2580 ippTagString(attrptr
->group_tag
));
2581 if (attrptr
->num_values
!= 1)
2583 "status-message (text(255)) has %d values "
2584 "(RFC 2911 section 3.1.6.2).",
2585 attrptr
->num_values
);
2586 if (attrptr
->value_tag
== IPP_TAG_TEXT
&&
2587 strlen(attrptr
->values
[0].string
.text
) > 255)
2589 "status-message (text(255)) has bad length %d"
2590 " (RFC 2911 section 3.1.6.2).",
2591 (int)strlen(attrptr
->values
[0].string
.text
));
2594 if ((attrptr
= ippFindAttribute(response
, "detailed-status-message",
2595 IPP_TAG_ZERO
)) != NULL
)
2597 if (attrptr
->value_tag
!= IPP_TAG_TEXT
)
2599 "detailed-status-message (text(MAX)) has wrong "
2600 "value tag %s (RFC 2911 section 3.1.6.3).",
2601 ippTagString(attrptr
->value_tag
));
2602 if (attrptr
->group_tag
!= IPP_TAG_OPERATION
)
2604 "detailed-status-message (text(MAX)) has wrong "
2605 "group tag %s (RFC 2911 section 3.1.6.3).",
2606 ippTagString(attrptr
->group_tag
));
2607 if (attrptr
->num_values
!= 1)
2609 "detailed-status-message (text(MAX)) has %d values"
2610 " (RFC 2911 section 3.1.6.3).",
2611 attrptr
->num_values
);
2612 if (attrptr
->value_tag
== IPP_TAG_TEXT
&&
2613 strlen(attrptr
->values
[0].string
.text
) > 1023)
2615 "detailed-status-message (text(MAX)) has bad "
2616 "length %d (RFC 2911 section 3.1.6.3).",
2617 (int)strlen(attrptr
->values
[0].string
.text
));
2620 a
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
2622 for (attrptr
= response
->attrs
, group
= attrptr
->group_tag
;
2624 attrptr
= attrptr
->next
)
2626 if (attrptr
->group_tag
!= group
)
2628 int out_of_order
= 0; /* Are attribute groups out-of-order? */
2632 switch (attrptr
->group_tag
)
2637 case IPP_TAG_OPERATION
:
2641 case IPP_TAG_UNSUPPORTED_GROUP
:
2642 if (group
!= IPP_TAG_OPERATION
)
2647 case IPP_TAG_PRINTER
:
2648 if (group
!= IPP_TAG_OPERATION
&&
2649 group
!= IPP_TAG_UNSUPPORTED_GROUP
)
2653 case IPP_TAG_SUBSCRIPTION
:
2654 if (group
> attrptr
->group_tag
&&
2655 group
!= IPP_TAG_DOCUMENT
)
2660 if (group
> attrptr
->group_tag
)
2666 add_stringf(errors
, "Attribute groups out of order (%s < %s)",
2667 ippTagString(attrptr
->group_tag
),
2668 ippTagString(group
));
2670 if (attrptr
->group_tag
!= IPP_TAG_ZERO
)
2671 group
= attrptr
->group_tag
;
2674 validate_attr(errors
, attrptr
);
2678 if (cupsArrayFind(a
, attrptr
->name
))
2679 add_stringf(errors
, "Duplicate \"%s\" attribute in %s group",
2680 attrptr
->name
, ippTagString(group
));
2682 cupsArrayAdd(a
, attrptr
->name
);
2689 * Now check the test-defined expected status-code and attribute
2693 for (i
= 0; i
< num_statuses
; i
++)
2695 if (statuses
[i
].if_defined
&&
2696 !get_variable(vars
, statuses
[i
].if_defined
))
2699 if (statuses
[i
].if_not_defined
&&
2700 get_variable(vars
, statuses
[i
].if_not_defined
))
2703 if (response
->request
.status
.status_code
== statuses
[i
].status
)
2705 if (statuses
[i
].repeat_match
&&
2706 repeat_count
< statuses
[i
].repeat_limit
)
2711 else if (statuses
[i
].repeat_no_match
&&
2712 repeat_count
< statuses
[i
].repeat_limit
)
2716 if (i
== num_statuses
&& num_statuses
> 0)
2718 for (i
= 0; i
< num_statuses
; i
++)
2720 if (statuses
[i
].if_defined
&&
2721 !get_variable(vars
, statuses
[i
].if_defined
))
2724 if (statuses
[i
].if_not_defined
&&
2725 get_variable(vars
, statuses
[i
].if_not_defined
))
2728 if (!statuses
[i
].repeat_match
)
2729 add_stringf(errors
, "EXPECTED: STATUS %s (got %s)",
2730 ippErrorString(statuses
[i
].status
),
2731 ippErrorString(cupsLastError()));
2734 if ((attrptr
= ippFindAttribute(response
, "status-message",
2735 IPP_TAG_TEXT
)) != NULL
)
2736 add_stringf(errors
, "status-message=\"%s\"",
2737 attrptr
->values
[0].string
.text
);
2740 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
2742 if (expect
->if_defined
&& !get_variable(vars
, expect
->if_defined
))
2745 if (expect
->if_not_defined
&&
2746 get_variable(vars
, expect
->if_not_defined
))
2749 found
= ippFindAttribute(response
, expect
->name
, IPP_TAG_ZERO
);
2751 if ((found
&& expect
->not_expect
) ||
2752 (!found
&& !(expect
->not_expect
|| expect
->optional
)) ||
2753 (found
&& !expect_matches(expect
, found
->value_tag
)) ||
2754 (found
&& expect
->in_group
&&
2755 found
->group_tag
!= expect
->in_group
))
2757 if (expect
->define_no_match
)
2758 set_variable(vars
, expect
->define_no_match
, "1");
2759 else if (!expect
->define_match
&& !expect
->define_value
)
2761 if (found
&& expect
->not_expect
)
2762 add_stringf(errors
, "NOT EXPECTED: %s", expect
->name
);
2763 else if (!found
&& !(expect
->not_expect
|| expect
->optional
))
2764 add_stringf(errors
, "EXPECTED: %s", expect
->name
);
2767 if (!expect_matches(expect
, found
->value_tag
))
2768 add_stringf(errors
, "EXPECTED: %s OF-TYPE %s (got %s)",
2769 expect
->name
, expect
->of_type
,
2770 ippTagString(found
->value_tag
));
2772 if (expect
->in_group
&& found
->group_tag
!= expect
->in_group
)
2773 add_stringf(errors
, "EXPECTED: %s IN-GROUP %s (got %s).",
2774 expect
->name
, ippTagString(expect
->in_group
),
2775 ippTagString(found
->group_tag
));
2779 if (expect
->repeat_no_match
&&
2780 repeat_count
< expect
->repeat_limit
)
2787 ippAttributeString(found
, buffer
, sizeof(buffer
));
2790 !with_value(NULL
, expect
->with_value
, expect
->with_flags
, found
,
2791 buffer
, sizeof(buffer
)))
2793 if (expect
->define_no_match
)
2794 set_variable(vars
, expect
->define_no_match
, "1");
2795 else if (!expect
->define_match
&& !expect
->define_value
&&
2796 !expect
->repeat_match
&& !expect
->repeat_no_match
)
2798 if (expect
->with_flags
& _CUPS_WITH_REGEX
)
2799 add_stringf(errors
, "EXPECTED: %s %s /%s/",
2801 (expect
->with_flags
& _CUPS_WITH_ALL
) ?
2802 "WITH-ALL-VALUES" : "WITH-VALUE",
2803 expect
->with_value
);
2805 add_stringf(errors
, "EXPECTED: %s %s \"%s\"",
2807 (expect
->with_flags
& _CUPS_WITH_ALL
) ?
2808 "WITH-ALL-VALUES" : "WITH-VALUE",
2809 expect
->with_value
);
2811 with_value(errors
, expect
->with_value
, expect
->with_flags
, found
,
2812 buffer
, sizeof(buffer
));
2815 if (expect
->repeat_no_match
&&
2816 repeat_count
< expect
->repeat_limit
)
2822 if (found
&& expect
->count
> 0 &&
2823 found
->num_values
!= expect
->count
)
2825 if (expect
->define_no_match
)
2826 set_variable(vars
, expect
->define_no_match
, "1");
2827 else if (!expect
->define_match
&& !expect
->define_value
)
2829 add_stringf(errors
, "EXPECTED: %s COUNT %d (got %d)", expect
->name
,
2830 expect
->count
, found
->num_values
);
2833 if (expect
->repeat_no_match
&&
2834 repeat_count
< expect
->repeat_limit
)
2840 if (found
&& expect
->same_count_as
)
2842 attrptr
= ippFindAttribute(response
, expect
->same_count_as
,
2845 if (!attrptr
|| attrptr
->num_values
!= found
->num_values
)
2847 if (expect
->define_no_match
)
2848 set_variable(vars
, expect
->define_no_match
, "1");
2849 else if (!expect
->define_match
&& !expect
->define_value
)
2853 "EXPECTED: %s (%d values) SAME-COUNT-AS %s "
2854 "(not returned)", expect
->name
,
2855 found
->num_values
, expect
->same_count_as
);
2856 else if (attrptr
->num_values
!= found
->num_values
)
2858 "EXPECTED: %s (%d values) SAME-COUNT-AS %s "
2859 "(%d values)", expect
->name
, found
->num_values
,
2860 expect
->same_count_as
, attrptr
->num_values
);
2863 if (expect
->repeat_no_match
&&
2864 repeat_count
< expect
->repeat_limit
)
2871 if (found
&& expect
->define_match
)
2872 set_variable(vars
, expect
->define_match
, "1");
2874 if (found
&& expect
->define_value
)
2875 set_variable(vars
, expect
->define_value
, buffer
);
2877 if (found
&& expect
->repeat_match
&&
2878 repeat_count
< expect
->repeat_limit
)
2884 * If we are going to repeat this test, sleep 1 second so we don't flood
2885 * the printer with requests...
2890 if (Output
== _CUPS_OUTPUT_TEST
)
2892 printf("%04d]\n", repeat_count
);
2896 sleep(repeat_interval
);
2897 repeat_interval
= _cupsNextDelay(repeat_interval
, &repeat_prev
);
2899 if (Output
== _CUPS_OUTPUT_TEST
)
2901 printf(" %-68.68s [", name
);
2906 while (repeat_test
);
2912 if (cupsArrayCount(errors
) > 0)
2913 prev_pass
= pass
= 0;
2920 if (Output
== _CUPS_OUTPUT_PLIST
)
2922 puts("<key>Successful</key>");
2923 puts(prev_pass
? "<true />" : "<false />");
2924 puts("<key>StatusCode</key>");
2925 print_xml_string("string", ippErrorString(cupsLastError()));
2926 puts("<key>ResponseAttributes</key>");
2929 for (attrptr
= response
? response
->attrs
: NULL
,
2930 group
= attrptr
? attrptr
->group_tag
: IPP_TAG_ZERO
;
2932 attrptr
= attrptr
->next
)
2933 print_attr(attrptr
, &group
);
2937 else if (Output
== _CUPS_OUTPUT_TEST
)
2939 puts(prev_pass
? "PASS]" : "FAIL]");
2941 if (!prev_pass
|| (Verbosity
&& response
))
2943 printf(" RECEIVED: %lu bytes in response\n",
2944 (unsigned long)ippLength(response
));
2945 printf(" status-code = %s (%s)\n", ippErrorString(cupsLastError()),
2946 cupsLastErrorString());
2950 for (attrptr
= response
->attrs
;
2952 attrptr
= attrptr
->next
)
2953 print_attr(attrptr
, NULL
);
2957 else if (!prev_pass
)
2958 fprintf(stderr
, "%s\n", cupsLastErrorString());
2960 if (prev_pass
&& Output
>= _CUPS_OUTPUT_LIST
&& !Verbosity
&&
2963 size_t width
; /* Length of value */
2965 for (i
= 0; i
< num_displayed
; i
++)
2967 widths
[i
] = strlen(displayed
[i
]);
2969 for (attrptr
= ippFindAttribute(response
, displayed
[i
], IPP_TAG_ZERO
);
2971 attrptr
= ippFindNextAttribute(response
, displayed
[i
],
2974 width
= ippAttributeString(attrptr
, NULL
, 0);
2975 if (width
> widths
[i
])
2980 if (Output
== _CUPS_OUTPUT_CSV
)
2981 print_csv(NULL
, num_displayed
, displayed
, widths
);
2983 print_line(NULL
, num_displayed
, displayed
, widths
);
2985 attrptr
= response
->attrs
;
2989 while (attrptr
&& attrptr
->group_tag
<= IPP_TAG_OPERATION
)
2990 attrptr
= attrptr
->next
;
2994 if (Output
== _CUPS_OUTPUT_CSV
)
2995 print_csv(attrptr
, num_displayed
, displayed
, widths
);
2997 print_line(attrptr
, num_displayed
, displayed
, widths
);
2999 while (attrptr
&& attrptr
->group_tag
> IPP_TAG_OPERATION
)
3000 attrptr
= attrptr
->next
;
3004 else if (!prev_pass
)
3006 if (Output
== _CUPS_OUTPUT_PLIST
)
3008 puts("<key>Errors</key>");
3011 for (error
= (char *)cupsArrayFirst(errors
);
3013 error
= (char *)cupsArrayNext(errors
))
3014 print_xml_string("string", error
);
3020 for (error
= (char *)cupsArrayFirst(errors
);
3022 error
= (char *)cupsArrayNext(errors
))
3023 printf(" %s\n", error
);
3027 if (num_displayed
> 0 && !Verbosity
&& response
&&
3028 (Output
== _CUPS_OUTPUT_TEST
|| Output
== _CUPS_OUTPUT_PLIST
))
3030 for (attrptr
= response
->attrs
;
3032 attrptr
= attrptr
->next
)
3036 for (i
= 0; i
< num_displayed
; i
++)
3038 if (!strcmp(displayed
[i
], attrptr
->name
))
3040 print_attr(attrptr
, NULL
);
3050 if (Output
== _CUPS_OUTPUT_PLIST
)
3055 ippDelete(response
);
3058 for (i
= 0; i
< num_statuses
; i
++)
3060 if (statuses
[i
].if_defined
)
3061 free(statuses
[i
].if_defined
);
3062 if (statuses
[i
].if_not_defined
)
3063 free(statuses
[i
].if_not_defined
);
3067 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
3070 if (expect
->of_type
)
3071 free(expect
->of_type
);
3072 if (expect
->same_count_as
)
3073 free(expect
->same_count_as
);
3074 if (expect
->if_defined
)
3075 free(expect
->if_defined
);
3076 if (expect
->if_not_defined
)
3077 free(expect
->if_not_defined
);
3078 if (expect
->with_value
)
3079 free(expect
->with_value
);
3080 if (expect
->define_match
)
3081 free(expect
->define_match
);
3082 if (expect
->define_no_match
)
3083 free(expect
->define_no_match
);
3084 if (expect
->define_value
)
3085 free(expect
->define_value
);
3089 for (i
= 0; i
< num_displayed
; i
++)
3093 if (!ignore_errors
&& !prev_pass
)
3099 cupsArrayDelete(errors
);
3106 ippDelete(response
);
3108 for (i
= 0; i
< num_statuses
; i
++)
3110 if (statuses
[i
].if_defined
)
3111 free(statuses
[i
].if_defined
);
3112 if (statuses
[i
].if_not_defined
)
3113 free(statuses
[i
].if_not_defined
);
3116 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
3119 if (expect
->of_type
)
3120 free(expect
->of_type
);
3121 if (expect
->same_count_as
)
3122 free(expect
->same_count_as
);
3123 if (expect
->if_defined
)
3124 free(expect
->if_defined
);
3125 if (expect
->if_not_defined
)
3126 free(expect
->if_not_defined
);
3127 if (expect
->with_value
)
3128 free(expect
->with_value
);
3129 if (expect
->define_match
)
3130 free(expect
->define_match
);
3131 if (expect
->define_no_match
)
3132 free(expect
->define_no_match
);
3133 if (expect
->define_value
)
3134 free(expect
->define_value
);
3137 for (i
= 0; i
< num_displayed
; i
++)
3145 * 'expand_variables()' - Expand variables in a string.
3149 expand_variables(_cups_vars_t
*vars
, /* I - Variables */
3150 char *dst
, /* I - Destination string buffer */
3151 const char *src
, /* I - Source string */
3152 size_t dstsize
) /* I - Size of destination buffer */
3154 char *dstptr
, /* Pointer into destination */
3155 *dstend
, /* End of destination */
3156 temp
[256], /* Temporary string */
3157 *tempptr
; /* Pointer into temporary string */
3158 const char *value
; /* Value to substitute */
3162 dstend
= dst
+ dstsize
- 1;
3164 while (*src
&& dstptr
< dstend
)
3169 * Substitute a string/number...
3172 if (!strncmp(src
, "$$", 2))
3177 else if (!strncmp(src
, "$ENV[", 5))
3179 strlcpy(temp
, src
+ 5, sizeof(temp
));
3181 for (tempptr
= temp
; *tempptr
; tempptr
++)
3182 if (*tempptr
== ']')
3188 value
= getenv(temp
);
3189 src
+= tempptr
- temp
+ 5;
3193 strlcpy(temp
, src
+ 1, sizeof(temp
));
3195 for (tempptr
= temp
; *tempptr
; tempptr
++)
3196 if (!isalnum(*tempptr
& 255) && *tempptr
!= '-' && *tempptr
!= '_')
3202 if (!strcmp(temp
, "uri"))
3204 else if (!strcmp(temp
, "filename"))
3205 value
= vars
->filename
;
3206 else if (!strcmp(temp
, "scheme") || !strcmp(temp
, "method"))
3207 value
= vars
->scheme
;
3208 else if (!strcmp(temp
, "username"))
3209 value
= vars
->userpass
;
3210 else if (!strcmp(temp
, "hostname"))
3211 value
= vars
->hostname
;
3212 else if (!strcmp(temp
, "port"))
3214 snprintf(temp
, sizeof(temp
), "%d", vars
->port
);
3217 else if (!strcmp(temp
, "resource"))
3218 value
= vars
->resource
;
3219 else if (!strcmp(temp
, "user"))
3222 value
= get_variable(vars
, temp
);
3224 src
+= tempptr
- temp
+ 1;
3234 strlcpy(dstptr
, value
, dstend
- dstptr
+ 1);
3235 dstptr
+= strlen(dstptr
);
3247 * 'expect_matches()' - Return true if the tag matches the specification.
3250 static int /* O - 1 if matches, 0 otherwise */
3252 _cups_expect_t
*expect
, /* I - Expected attribute */
3253 ipp_tag_t value_tag
) /* I - Value tag for attribute */
3255 int match
; /* Match? */
3256 char *of_type
, /* Type name to match */
3257 *next
, /* Next name to match */
3258 sep
; /* Separator character */
3262 * If we don't expect a particular type, return immediately...
3265 if (!expect
->of_type
)
3269 * Parse the "of_type" value since the string can contain multiple attribute
3270 * types separated by "," or "|"...
3273 for (of_type
= expect
->of_type
, match
= 0; !match
&& *of_type
; of_type
= next
)
3276 * Find the next separator, and set it (temporarily) to nul if present.
3279 for (next
= of_type
; *next
&& *next
!= '|' && *next
!= ','; next
++);
3281 if ((sep
= *next
) != '\0')
3285 * Support some meta-types to make it easier to write the test file.
3288 if (!strcmp(of_type
, "text"))
3289 match
= value_tag
== IPP_TAG_TEXTLANG
|| value_tag
== IPP_TAG_TEXT
;
3290 else if (!strcmp(of_type
, "name"))
3291 match
= value_tag
== IPP_TAG_NAMELANG
|| value_tag
== IPP_TAG_NAME
;
3292 else if (!strcmp(of_type
, "collection"))
3293 match
= value_tag
== IPP_TAG_BEGIN_COLLECTION
;
3295 match
= value_tag
== ippTagValue(of_type
);
3298 * Restore the separator if we have one...
3310 * 'get_collection()' - Get a collection value from the current test file.
3313 static ipp_t
* /* O - Collection value */
3314 get_collection(_cups_vars_t
*vars
, /* I - Variables */
3315 FILE *fp
, /* I - File to read from */
3316 int *linenum
) /* IO - Line number */
3318 char token
[1024], /* Token from file */
3319 temp
[1024], /* Temporary string */
3320 attr
[128]; /* Attribute name */
3321 ipp_tag_t value
; /* Current value type */
3322 ipp_t
*col
= ippNew(); /* Collection value */
3323 ipp_attribute_t
*lastcol
= NULL
; /* Last collection attribute */
3326 while (get_token(fp
, token
, sizeof(token
), linenum
) != NULL
)
3328 if (!strcmp(token
, "}"))
3330 else if (!strcmp(token
, "{") && lastcol
)
3333 * Another collection value
3336 ipp_t
*subcol
= get_collection(vars
, fp
, linenum
);
3337 /* Collection value */
3341 ipp_attribute_t
*tempcol
; /* Pointer to new buffer */
3345 * Reallocate memory...
3348 if ((tempcol
= realloc(lastcol
, sizeof(ipp_attribute_t
) +
3349 (lastcol
->num_values
+ 1) *
3350 sizeof(_ipp_value_t
))) == NULL
)
3352 print_fatal_error("Unable to allocate memory on line %d.", *linenum
);
3356 if (tempcol
!= lastcol
)
3359 * Reset pointers in the list...
3363 col
->prev
->next
= tempcol
;
3365 col
->attrs
= tempcol
;
3367 lastcol
= col
->current
= col
->last
= tempcol
;
3370 lastcol
->values
[lastcol
->num_values
].collection
= subcol
;
3371 lastcol
->num_values
++;
3376 else if (!_cups_strcasecmp(token
, "MEMBER"))
3384 if (!get_token(fp
, token
, sizeof(token
), linenum
))
3386 print_fatal_error("Missing MEMBER value tag on line %d.", *linenum
);
3390 if ((value
= ippTagValue(token
)) == IPP_TAG_ZERO
)
3392 print_fatal_error("Bad MEMBER value tag \"%s\" on line %d.", token
,
3397 if (!get_token(fp
, attr
, sizeof(attr
), linenum
))
3399 print_fatal_error("Missing MEMBER name on line %d.", *linenum
);
3403 if (!get_token(fp
, temp
, sizeof(temp
), linenum
))
3405 print_fatal_error("Missing MEMBER value on line %d.", *linenum
);
3409 expand_variables(vars
, token
, temp
, sizeof(token
));
3413 case IPP_TAG_BOOLEAN
:
3414 if (!_cups_strcasecmp(token
, "true"))
3415 ippAddBoolean(col
, IPP_TAG_ZERO
, attr
, 1);
3417 ippAddBoolean(col
, IPP_TAG_ZERO
, attr
, atoi(token
));
3420 case IPP_TAG_INTEGER
:
3422 ippAddInteger(col
, IPP_TAG_ZERO
, value
, attr
, atoi(token
));
3425 case IPP_TAG_RESOLUTION
:
3427 int xres
, /* X resolution */
3428 yres
; /* Y resolution */
3429 char units
[6]; /* Units */
3431 if (sscanf(token
, "%dx%d%5s", &xres
, &yres
, units
) != 3 ||
3432 (_cups_strcasecmp(units
, "dpi") &&
3433 _cups_strcasecmp(units
, "dpc") &&
3434 _cups_strcasecmp(units
, "dpcm") &&
3435 _cups_strcasecmp(units
, "other")))
3437 print_fatal_error("Bad resolution value \"%s\" on line %d.",
3442 if (!_cups_strcasecmp(units
, "dpi"))
3443 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, xres
, yres
,
3445 else if (!_cups_strcasecmp(units
, "dpc") ||
3446 !_cups_strcasecmp(units
, "dpcm"))
3447 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, xres
, yres
,
3450 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, xres
, yres
,
3455 case IPP_TAG_RANGE
:
3457 int lowers
[4], /* Lower value */
3458 uppers
[4], /* Upper values */
3459 num_vals
; /* Number of values */
3462 num_vals
= sscanf(token
, "%d-%d,%d-%d,%d-%d,%d-%d",
3463 lowers
+ 0, uppers
+ 0,
3464 lowers
+ 1, uppers
+ 1,
3465 lowers
+ 2, uppers
+ 2,
3466 lowers
+ 3, uppers
+ 3);
3468 if ((num_vals
& 1) || num_vals
== 0)
3470 print_fatal_error("Bad rangeOfInteger value \"%s\" on line %d.",
3475 ippAddRanges(col
, IPP_TAG_ZERO
, attr
, num_vals
/ 2, lowers
,
3480 case IPP_TAG_BEGIN_COLLECTION
:
3481 if (!strcmp(token
, "{"))
3483 ipp_t
*subcol
= get_collection(vars
, fp
, linenum
);
3484 /* Collection value */
3488 lastcol
= ippAddCollection(col
, IPP_TAG_ZERO
, attr
, subcol
);
3496 print_fatal_error("Bad collection value on line %d.", *linenum
);
3500 case IPP_TAG_STRING
:
3501 ippAddOctetString(col
, IPP_TAG_ZERO
, attr
, token
, strlen(token
));
3505 if (!strchr(token
, ','))
3506 ippAddString(col
, IPP_TAG_ZERO
, value
, attr
, NULL
, token
);
3510 * Multiple string values...
3513 int num_values
; /* Number of values */
3514 char *values
[100], /* Values */
3515 *ptr
; /* Pointer to next value */
3521 for (ptr
= strchr(token
, ','); ptr
; ptr
= strchr(ptr
, ','))
3524 values
[num_values
] = ptr
;
3528 ippAddStrings(col
, IPP_TAG_ZERO
, value
, attr
, num_values
,
3529 NULL
, (const char **)values
);
3539 * If we get here there was a parse error; free memory and return.
3551 * 'get_filename()' - Get a filename based on the current test file.
3554 static char * /* O - Filename */
3555 get_filename(const char *testfile
, /* I - Current test file */
3556 char *dst
, /* I - Destination filename */
3557 const char *src
, /* I - Source filename */
3558 size_t dstsize
) /* I - Size of destination buffer */
3560 char *dstptr
; /* Pointer into destination */
3561 _cups_globals_t
*cg
= _cupsGlobals();
3565 if (*src
== '<' && src
[strlen(src
) - 1] == '>')
3568 * Map <filename> to CUPS_DATADIR/ipptool/filename...
3571 snprintf(dst
, dstsize
, "%s/ipptool/%s", cg
->cups_datadir
, src
+ 1);
3572 dstptr
= dst
+ strlen(dst
) - 1;
3576 else if (*src
== '/' || !strchr(testfile
, '/')
3578 || (isalpha(*src
& 255) && src
[1] == ':')
3583 * Use the path as-is...
3586 strlcpy(dst
, src
, dstsize
);
3591 * Make path relative to testfile...
3594 strlcpy(dst
, testfile
, dstsize
);
3595 if ((dstptr
= strrchr(dst
, '/')) != NULL
)
3598 dstptr
= dst
; /* Should never happen */
3600 strlcpy(dstptr
, src
, dstsize
- (dstptr
- dst
));
3608 * 'get_token()' - Get a token from a file.
3611 static char * /* O - Token from file or NULL on EOF */
3612 get_token(FILE *fp
, /* I - File to read from */
3613 char *buf
, /* I - Buffer to read into */
3614 int buflen
, /* I - Length of buffer */
3615 int *linenum
) /* IO - Current line number */
3617 int ch
, /* Character from file */
3618 quote
; /* Quoting character */
3619 char *bufptr
, /* Pointer into buffer */
3620 *bufend
; /* End of buffer */
3626 * Skip whitespace...
3629 while (isspace(ch
= getc(fp
)))
3641 else if (ch
== '\'' || ch
== '\"')
3644 * Quoted text or regular expression...
3649 bufend
= buf
+ buflen
- 1;
3651 while ((ch
= getc(fp
)) != EOF
)
3656 * Escape next character...
3659 if (bufptr
< bufend
)
3662 if ((ch
= getc(fp
)) != EOF
&& bufptr
< bufend
)
3665 else if (ch
== quote
)
3667 else if (bufptr
< bufend
)
3681 while ((ch
= getc(fp
)) != EOF
)
3690 * Whitespace delimited text...
3696 bufend
= buf
+ buflen
- 1;
3698 while ((ch
= getc(fp
)) != EOF
)
3699 if (isspace(ch
) || ch
== '#')
3701 else if (bufptr
< bufend
)
3706 else if (ch
== '\n')
3718 * 'get_variable()' - Get the value of a variable.
3721 static char * /* O - Value or NULL */
3722 get_variable(_cups_vars_t
*vars
, /* I - Variables */
3723 const char *name
) /* I - Variable name */
3725 _cups_var_t key
, /* Search key */
3726 *match
; /* Matching variable, if any */
3729 key
.name
= (char *)name
;
3730 match
= cupsArrayFind(vars
->vars
, &key
);
3732 return (match
? match
->value
: NULL
);
3737 * 'iso_date()' - Return an ISO 8601 date/time string for the given IPP dateTime
3741 static char * /* O - ISO 8601 date/time string */
3742 iso_date(ipp_uchar_t
*date
) /* I - IPP (RFC 1903) date/time value */
3744 time_t utctime
; /* UTC time since 1970 */
3745 struct tm
*utcdate
; /* UTC date/time */
3746 static char buffer
[255]; /* String buffer */
3749 utctime
= ippDateToTime(date
);
3750 utcdate
= gmtime(&utctime
);
3752 snprintf(buffer
, sizeof(buffer
), "%04d-%02d-%02dT%02d:%02d:%02dZ",
3753 utcdate
->tm_year
+ 1900, utcdate
->tm_mon
+ 1, utcdate
->tm_mday
,
3754 utcdate
->tm_hour
, utcdate
->tm_min
, utcdate
->tm_sec
);
3761 * 'password_cb()' - Password callback for authenticated tests.
3764 static const char * /* O - Password */
3765 password_cb(const char *prompt
) /* I - Prompt (unused) */
3774 * 'print_attr()' - Print an attribute on the screen.
3778 print_attr(ipp_attribute_t
*attr
, /* I - Attribute to print */
3779 ipp_tag_t
*group
) /* IO - Current group */
3781 int i
; /* Looping var */
3782 ipp_attribute_t
*colattr
; /* Collection attribute */
3785 if (Output
== _CUPS_OUTPUT_PLIST
)
3787 if (!attr
->name
|| (group
&& *group
!= attr
->group_tag
))
3793 *group
= attr
->group_tag
;
3799 print_xml_string("key", attr
->name
);
3800 if (attr
->num_values
> 1)
3803 switch (attr
->value_tag
)
3805 case IPP_TAG_INTEGER
:
3807 for (i
= 0; i
< attr
->num_values
; i
++)
3808 if (Output
== _CUPS_OUTPUT_PLIST
)
3809 printf("<integer>%d</integer>\n", attr
->values
[i
].integer
);
3811 printf("%d ", attr
->values
[i
].integer
);
3814 case IPP_TAG_BOOLEAN
:
3815 for (i
= 0; i
< attr
->num_values
; i
++)
3816 if (Output
== _CUPS_OUTPUT_PLIST
)
3817 puts(attr
->values
[i
].boolean
? "<true />" : "<false />");
3818 else if (attr
->values
[i
].boolean
)
3819 fputs("true ", stdout
);
3821 fputs("false ", stdout
);
3824 case IPP_TAG_RANGE
:
3825 for (i
= 0; i
< attr
->num_values
; i
++)
3826 if (Output
== _CUPS_OUTPUT_PLIST
)
3827 printf("<dict><key>lower</key><integer>%d</integer>"
3828 "<key>upper</key><integer>%d</integer></dict>\n",
3829 attr
->values
[i
].range
.lower
, attr
->values
[i
].range
.upper
);
3831 printf("%d-%d ", attr
->values
[i
].range
.lower
,
3832 attr
->values
[i
].range
.upper
);
3835 case IPP_TAG_RESOLUTION
:
3836 for (i
= 0; i
< attr
->num_values
; i
++)
3837 if (Output
== _CUPS_OUTPUT_PLIST
)
3838 printf("<dict><key>xres</key><integer>%d</integer>"
3839 "<key>yres</key><integer>%d</integer>"
3840 "<key>units</key><string>%s</string></dict>\n",
3841 attr
->values
[i
].resolution
.xres
,
3842 attr
->values
[i
].resolution
.yres
,
3843 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3846 printf("%dx%d%s ", attr
->values
[i
].resolution
.xres
,
3847 attr
->values
[i
].resolution
.yres
,
3848 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3853 for (i
= 0; i
< attr
->num_values
; i
++)
3854 if (Output
== _CUPS_OUTPUT_PLIST
)
3855 printf("<date>%s</date>\n", iso_date(attr
->values
[i
].date
));
3857 printf("%s ", iso_date(attr
->values
[i
].date
));
3860 case IPP_TAG_STRING
:
3861 for (i
= 0; i
< attr
->num_values
; i
++)
3863 if (Output
== _CUPS_OUTPUT_PLIST
)
3865 char buffer
[IPP_MAX_LENGTH
* 5 / 4 + 1];
3868 printf("<data>%s</data>\n",
3869 httpEncode64_2(buffer
, sizeof(buffer
),
3870 attr
->values
[i
].unknown
.data
,
3871 attr
->values
[i
].unknown
.length
));
3875 char *ptr
, /* Pointer into data */
3876 *end
; /* End of data */
3879 for (ptr
= attr
->values
[i
].unknown
.data
,
3880 end
= ptr
+ attr
->values
[i
].unknown
.length
;
3884 if (*ptr
== '\\' || *ptr
== '\"')
3885 printf("\\%c", *ptr
);
3886 else if (!isprint(*ptr
& 255))
3887 printf("\\%03o", *ptr
& 255);
3898 case IPP_TAG_KEYWORD
:
3899 case IPP_TAG_CHARSET
:
3901 case IPP_TAG_MIMETYPE
:
3902 case IPP_TAG_LANGUAGE
:
3903 for (i
= 0; i
< attr
->num_values
; i
++)
3904 if (Output
== _CUPS_OUTPUT_PLIST
)
3905 print_xml_string("string", attr
->values
[i
].string
.text
);
3907 printf("\"%s\" ", attr
->values
[i
].string
.text
);
3910 case IPP_TAG_TEXTLANG
:
3911 case IPP_TAG_NAMELANG
:
3912 for (i
= 0; i
< attr
->num_values
; i
++)
3913 if (Output
== _CUPS_OUTPUT_PLIST
)
3915 fputs("<dict><key>language</key><string>", stdout
);
3916 print_xml_string(NULL
, attr
->values
[i
].string
.language
);
3917 fputs("</string><key>string</key><string>", stdout
);
3918 print_xml_string(NULL
, attr
->values
[i
].string
.text
);
3919 puts("</string></dict>");
3922 printf("\"%s\"[%s] ", attr
->values
[i
].string
.text
,
3923 attr
->values
[i
].string
.language
);
3926 case IPP_TAG_BEGIN_COLLECTION
:
3927 for (i
= 0; i
< attr
->num_values
; i
++)
3929 if (Output
== _CUPS_OUTPUT_PLIST
)
3932 for (colattr
= attr
->values
[i
].collection
->attrs
;
3934 colattr
= colattr
->next
)
3935 print_attr(colattr
, NULL
);
3943 print_col(attr
->values
[i
].collection
);
3949 if (Output
== _CUPS_OUTPUT_PLIST
)
3950 printf("<string><<%s>></string>\n",
3951 ippTagString(attr
->value_tag
));
3953 fputs(ippTagString(attr
->value_tag
), stdout
);
3957 if (attr
->num_values
> 1)
3962 char buffer
[8192]; /* Value buffer */
3964 if (Output
== _CUPS_OUTPUT_TEST
)
3968 puts(" -- separator --");
3972 printf(" %s (%s%s) = ", attr
->name
,
3973 attr
->num_values
> 1 ? "1setOf " : "",
3974 ippTagString(attr
->value_tag
));
3977 ippAttributeString(attr
, buffer
, sizeof(buffer
));
3984 * 'print_col()' - Print a collection attribute on the screen.
3988 print_col(ipp_t
*col
) /* I - Collection attribute to print */
3990 int i
; /* Looping var */
3991 ipp_attribute_t
*attr
; /* Current attribute in collection */
3994 fputs("{ ", stdout
);
3995 for (attr
= col
->attrs
; attr
; attr
= attr
->next
)
3997 printf("%s (%s%s) = ", attr
->name
, attr
->num_values
> 1 ? "1setOf " : "",
3998 ippTagString(attr
->value_tag
));
4000 switch (attr
->value_tag
)
4002 case IPP_TAG_INTEGER
:
4004 for (i
= 0; i
< attr
->num_values
; i
++)
4005 printf("%d ", attr
->values
[i
].integer
);
4008 case IPP_TAG_BOOLEAN
:
4009 for (i
= 0; i
< attr
->num_values
; i
++)
4010 if (attr
->values
[i
].boolean
)
4016 case IPP_TAG_NOVALUE
:
4020 case IPP_TAG_RANGE
:
4021 for (i
= 0; i
< attr
->num_values
; i
++)
4022 printf("%d-%d ", attr
->values
[i
].range
.lower
,
4023 attr
->values
[i
].range
.upper
);
4026 case IPP_TAG_RESOLUTION
:
4027 for (i
= 0; i
< attr
->num_values
; i
++)
4028 printf("%dx%d%s ", attr
->values
[i
].resolution
.xres
,
4029 attr
->values
[i
].resolution
.yres
,
4030 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
4034 case IPP_TAG_STRING
:
4037 case IPP_TAG_KEYWORD
:
4038 case IPP_TAG_CHARSET
:
4040 case IPP_TAG_MIMETYPE
:
4041 case IPP_TAG_LANGUAGE
:
4042 for (i
= 0; i
< attr
->num_values
; i
++)
4043 printf("\"%s\" ", attr
->values
[i
].string
.text
);
4046 case IPP_TAG_TEXTLANG
:
4047 case IPP_TAG_NAMELANG
:
4048 for (i
= 0; i
< attr
->num_values
; i
++)
4049 printf("\"%s\"[%s] ", attr
->values
[i
].string
.text
,
4050 attr
->values
[i
].string
.language
);
4053 case IPP_TAG_BEGIN_COLLECTION
:
4054 for (i
= 0; i
< attr
->num_values
; i
++)
4056 print_col(attr
->values
[i
].collection
);
4062 break; /* anti-compiler-warning-code */
4071 * 'print_csv()' - Print a line of CSV text.
4076 ipp_attribute_t
*attr
, /* I - First attribute for line */
4077 int num_displayed
, /* I - Number of attributes to display */
4078 char **displayed
, /* I - Attributes to display */
4079 size_t *widths
) /* I - Column widths */
4081 int i
; /* Looping var */
4082 size_t maxlength
; /* Max length of all columns */
4083 char *buffer
, /* String buffer */
4084 *bufptr
; /* Pointer into buffer */
4085 ipp_attribute_t
*current
; /* Current attribute */
4089 * Get the maximum string length we have to show and allocate...
4092 for (i
= 1, maxlength
= widths
[0]; i
< num_displayed
; i
++)
4093 if (widths
[i
] > maxlength
)
4094 maxlength
= widths
[i
];
4098 if ((buffer
= malloc(maxlength
)) == NULL
)
4102 * Loop through the attributes to display...
4107 for (i
= 0; i
< num_displayed
; i
++)
4114 for (current
= attr
; current
; current
= current
->next
)
4118 else if (!strcmp(current
->name
, displayed
[i
]))
4120 ippAttributeString(current
, buffer
, maxlength
);
4125 if (strchr(buffer
, ',') != NULL
|| strchr(buffer
, '\"') != NULL
||
4126 strchr(buffer
, '\\') != NULL
)
4129 for (bufptr
= buffer
; *bufptr
; bufptr
++)
4131 if (*bufptr
== '\\' || *bufptr
== '\"')
4138 fputs(buffer
, stdout
);
4144 for (i
= 0; i
< num_displayed
; i
++)
4149 fputs(displayed
[i
], stdout
);
4159 * 'print_fatal_error()' - Print a fatal error message.
4163 print_fatal_error(const char *s
, /* I - Printf-style format string */
4164 ...) /* I - Additional arguments as needed */
4166 char buffer
[10240]; /* Format buffer */
4167 va_list ap
; /* Pointer to arguments */
4171 * Format the error message...
4175 vsnprintf(buffer
, sizeof(buffer
), s
, ap
);
4182 if (Output
== _CUPS_OUTPUT_PLIST
)
4185 print_xml_trailer(0, buffer
);
4188 _cupsLangPrintf(stderr
, "ipptool: %s", buffer
);
4193 * 'print_line()' - Print a line of formatted or CSV text.
4198 ipp_attribute_t
*attr
, /* I - First attribute for line */
4199 int num_displayed
, /* I - Number of attributes to display */
4200 char **displayed
, /* I - Attributes to display */
4201 size_t *widths
) /* I - Column widths */
4203 int i
; /* Looping var */
4204 size_t maxlength
; /* Max length of all columns */
4205 char *buffer
; /* String buffer */
4206 ipp_attribute_t
*current
; /* Current attribute */
4210 * Get the maximum string length we have to show and allocate...
4213 for (i
= 1, maxlength
= widths
[0]; i
< num_displayed
; i
++)
4214 if (widths
[i
] > maxlength
)
4215 maxlength
= widths
[i
];
4219 if ((buffer
= malloc(maxlength
)) == NULL
)
4223 * Loop through the attributes to display...
4228 for (i
= 0; i
< num_displayed
; i
++)
4235 for (current
= attr
; current
; current
= current
->next
)
4239 else if (!strcmp(current
->name
, displayed
[i
]))
4241 ippAttributeString(current
, buffer
, maxlength
);
4246 printf("%*s", (int)-widths
[i
], buffer
);
4252 for (i
= 0; i
< num_displayed
; i
++)
4257 printf("%*s", (int)-widths
[i
], displayed
[i
]);
4261 for (i
= 0; i
< num_displayed
; i
++)
4266 memset(buffer
, '-', widths
[i
]);
4267 buffer
[widths
[i
]] = '\0';
4268 fputs(buffer
, stdout
);
4278 * 'print_xml_header()' - Print a standard XML plist header.
4282 print_xml_header(void)
4286 puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
4287 puts("<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
4288 "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
4289 puts("<plist version=\"1.0\">");
4291 puts("<key>Transfer</key>");
4292 printf("<string>%s</string>\n",
4293 Transfer
== _CUPS_TRANSFER_AUTO
? "auto" :
4294 Transfer
== _CUPS_TRANSFER_CHUNKED
? "chunked" : "length");
4295 puts("<key>Tests</key>");
4304 * 'print_xml_string()' - Print an XML string with escaping.
4308 print_xml_string(const char *element
, /* I - Element name or NULL */
4309 const char *s
) /* I - String to print */
4312 printf("<%s>", element
);
4317 fputs("&", stdout
);
4319 fputs("<", stdout
);
4321 fputs(">", stdout
);
4322 else if ((*s
& 0xe0) == 0xc0)
4325 * Validate UTF-8 two-byte sequence...
4328 if ((s
[1] & 0xc0) != 0x80)
4339 else if ((*s
& 0xf0) == 0xe0)
4342 * Validate UTF-8 three-byte sequence...
4345 if ((s
[1] & 0xc0) != 0x80 || (s
[2] & 0xc0) != 0x80)
4357 else if ((*s
& 0xf8) == 0xf0)
4360 * Validate UTF-8 four-byte sequence...
4363 if ((s
[1] & 0xc0) != 0x80 || (s
[2] & 0xc0) != 0x80 ||
4364 (s
[3] & 0xc0) != 0x80)
4377 else if ((*s
& 0x80) || (*s
< ' ' && !isspace(*s
& 255)))
4380 * Invalid control character...
4392 printf("</%s>\n", element
);
4397 * 'print_xml_trailer()' - Print the XML trailer with success/fail value.
4401 print_xml_trailer(int success
, /* I - 1 on success, 0 on failure */
4402 const char *message
) /* I - Error message or NULL */
4407 puts("<key>Successful</key>");
4408 puts(success
? "<true />" : "<false />");
4411 puts("<key>ErrorMessage</key>");
4412 print_xml_string("string", message
);
4423 * 'set_variable()' - Set a variable value.
4427 set_variable(_cups_vars_t
*vars
, /* I - Variables */
4428 const char *name
, /* I - Variable name */
4429 const char *value
) /* I - Value string */
4431 _cups_var_t key
, /* Search key */
4432 *var
; /* New variable */
4435 if (!_cups_strcasecmp(name
, "filename"))
4438 free(vars
->filename
);
4440 vars
->filename
= strdup(value
);
4444 key
.name
= (char *)name
;
4445 if ((var
= cupsArrayFind(vars
->vars
, &key
)) != NULL
)
4448 var
->value
= strdup(value
);
4450 else if ((var
= malloc(sizeof(_cups_var_t
))) == NULL
)
4452 print_fatal_error("Unable to allocate memory for variable \"%s\".", name
);
4457 var
->name
= strdup(name
);
4458 var
->value
= strdup(value
);
4460 cupsArrayAdd(vars
->vars
, var
);
4467 * 'sigterm_handler()' - Handle SIGINT and SIGTERM.
4471 sigterm_handler(int sig
) /* I - Signal number (unused) */
4477 signal(SIGINT
, SIG_DFL
);
4478 signal(SIGTERM
, SIG_DFL
);
4484 * 'timeout_cb()' - Handle HTTP timeouts.
4487 static int /* O - 1 to continue, 0 to cancel */
4488 timeout_cb(http_t
*http
, /* I - Connection to server (unused) */
4489 void *user_data
) /* I - User data (unused) */
4494 /* Always cancel on timeout */
4500 * 'usage()' - Show program usage.
4506 _cupsLangPuts(stderr
, _("Usage: ipptool [options] URI filename [ ... "
4508 _cupsLangPuts(stderr
, _("Options:"));
4509 _cupsLangPuts(stderr
, _(" -4 Connect using IPv4."));
4510 _cupsLangPuts(stderr
, _(" -6 Connect using IPv6."));
4511 _cupsLangPuts(stderr
, _(" -C Send requests using "
4512 "chunking (default)."));
4513 _cupsLangPuts(stdout
, _(" -E Test with HTTP Upgrade to "
4515 _cupsLangPuts(stderr
, _(" -I Ignore errors."));
4516 _cupsLangPuts(stderr
, _(" -L Send requests using "
4517 "content-length."));
4518 _cupsLangPuts(stderr
, _(" -S Test with SSL "
4520 _cupsLangPuts(stderr
, _(" -T seconds Set the receive/send "
4521 "timeout in seconds."));
4522 _cupsLangPuts(stderr
, _(" -V version Set default IPP "
4524 _cupsLangPuts(stderr
, _(" -X Produce XML plist instead "
4526 _cupsLangPuts(stderr
, _(" -d name=value Set named variable to "
4528 _cupsLangPuts(stderr
, _(" -f filename Set default request "
4530 _cupsLangPuts(stderr
, _(" -i seconds Repeat the last file with "
4531 "the given time interval."));
4532 _cupsLangPuts(stderr
, _(" -n count Repeat the last file the "
4533 "given number of times."));
4534 _cupsLangPuts(stderr
, _(" -q Run silently."));
4535 _cupsLangPuts(stderr
, _(" -t Produce a test report."));
4536 _cupsLangPuts(stderr
, _(" -v Be verbose."));
4543 * 'validate_attr()' - Determine whether an attribute is valid.
4546 static int /* O - 1 if valid, 0 otherwise */
4547 validate_attr(cups_array_t
*errors
, /* I - Errors array */
4548 ipp_attribute_t
*attr
) /* I - Attribute to validate */
4550 int i
; /* Looping var */
4551 char scheme
[64], /* Scheme from URI */
4552 userpass
[256], /* Username/password from URI */
4553 hostname
[256], /* Hostname from URI */
4554 resource
[1024]; /* Resource from URI */
4555 int port
, /* Port number from URI */
4556 uri_status
, /* URI separation status */
4557 valid
= 1; /* Is the attribute valid? */
4558 const char *ptr
; /* Pointer into string */
4559 ipp_attribute_t
*colattr
; /* Collection attribute */
4560 regex_t re
; /* Regular expression */
4561 ipp_uchar_t
*date
; /* Current date value */
4572 * Validate the attribute name.
4575 for (ptr
= attr
->name
; *ptr
; ptr
++)
4576 if (!isalnum(*ptr
& 255) && *ptr
!= '-' && *ptr
!= '.' && *ptr
!= '_')
4579 if (*ptr
|| ptr
== attr
->name
)
4584 "\"%s\": Bad attribute name - invalid character "
4585 "(RFC 2911 section 4.1.3).", attr
->name
);
4588 if ((ptr
- attr
->name
) > 255)
4593 "\"%s\": Bad attribute name - bad length "
4594 "(RFC 2911 section 4.1.3).", attr
->name
);
4597 switch (attr
->value_tag
)
4599 case IPP_TAG_INTEGER
:
4602 case IPP_TAG_BOOLEAN
:
4603 for (i
= 0; i
< attr
->num_values
; i
++)
4605 if (attr
->values
[i
].boolean
!= 0 &&
4606 attr
->values
[i
].boolean
!= 1)
4611 "\"%s\": Bad boolen value %d "
4612 "(RFC 2911 section 4.1.10).", attr
->name
,
4613 attr
->values
[i
].boolean
);
4619 for (i
= 0; i
< attr
->num_values
; i
++)
4621 if (attr
->values
[i
].integer
< 1)
4626 "\"%s\": Bad enum value %d - out of range "
4627 "(RFC 2911 section 4.1.4).", attr
->name
,
4628 attr
->values
[i
].integer
);
4633 case IPP_TAG_STRING
:
4634 for (i
= 0; i
< attr
->num_values
; i
++)
4636 if (attr
->values
[i
].unknown
.length
> IPP_MAX_OCTETSTRING
)
4641 "\"%s\": Bad octetString value - bad length %d "
4642 "(RFC 2911 section 4.1.10).", attr
->name
,
4643 attr
->values
[i
].unknown
.length
);
4649 for (i
= 0; i
< attr
->num_values
; i
++)
4651 date
= attr
->values
[i
].date
;
4653 if (date
[2] < 1 || date
[2] > 12)
4658 "\"%s\": Bad dateTime month %u "
4659 "(RFC 2911 section 4.1.13).", attr
->name
, date
[2]);
4662 if (date
[3] < 1 || date
[3] > 31)
4667 "\"%s\": Bad dateTime day %u "
4668 "(RFC 2911 section 4.1.13).", attr
->name
, date
[3]);
4676 "\"%s\": Bad dateTime hours %u "
4677 "(RFC 2911 section 4.1.13).", attr
->name
, date
[4]);
4685 "\"%s\": Bad dateTime minutes %u "
4686 "(RFC 2911 section 4.1.13).", attr
->name
, date
[5]);
4694 "\"%s\": Bad dateTime seconds %u "
4695 "(RFC 2911 section 4.1.13).", attr
->name
, date
[6]);
4703 "\"%s\": Bad dateTime deciseconds %u "
4704 "(RFC 2911 section 4.1.13).", attr
->name
, date
[7]);
4707 if (date
[8] != '-' && date
[8] != '+')
4712 "\"%s\": Bad dateTime UTC sign '%c' "
4713 "(RFC 2911 section 4.1.13).", attr
->name
, date
[8]);
4721 "\"%s\": Bad dateTime UTC hours %u "
4722 "(RFC 2911 section 4.1.13).", attr
->name
, date
[9]);
4730 "\"%s\": Bad dateTime UTC minutes %u "
4731 "(RFC 2911 section 4.1.13).", attr
->name
, date
[10]);
4736 case IPP_TAG_RESOLUTION
:
4737 for (i
= 0; i
< attr
->num_values
; i
++)
4739 if (attr
->values
[i
].resolution
.xres
<= 0)
4744 "\"%s\": Bad resolution value %dx%d%s - cross "
4745 "feed resolution must be positive "
4746 "(RFC 2911 section 4.1.13).", attr
->name
,
4747 attr
->values
[i
].resolution
.xres
,
4748 attr
->values
[i
].resolution
.yres
,
4749 attr
->values
[i
].resolution
.units
==
4750 IPP_RES_PER_INCH
? "dpi" :
4751 attr
->values
[i
].resolution
.units
==
4752 IPP_RES_PER_CM
? "dpcm" : "unknown");
4755 if (attr
->values
[i
].resolution
.yres
<= 0)
4760 "\"%s\": Bad resolution value %dx%d%s - feed "
4761 "resolution must be positive "
4762 "(RFC 2911 section 4.1.13).", attr
->name
,
4763 attr
->values
[i
].resolution
.xres
,
4764 attr
->values
[i
].resolution
.yres
,
4765 attr
->values
[i
].resolution
.units
==
4766 IPP_RES_PER_INCH
? "dpi" :
4767 attr
->values
[i
].resolution
.units
==
4768 IPP_RES_PER_CM
? "dpcm" : "unknown");
4771 if (attr
->values
[i
].resolution
.units
!= IPP_RES_PER_INCH
&&
4772 attr
->values
[i
].resolution
.units
!= IPP_RES_PER_CM
)
4777 "\"%s\": Bad resolution value %dx%d%s - bad "
4778 "units value (RFC 2911 section 4.1.13).",
4779 attr
->name
, attr
->values
[i
].resolution
.xres
,
4780 attr
->values
[i
].resolution
.yres
,
4781 attr
->values
[i
].resolution
.units
==
4782 IPP_RES_PER_INCH
? "dpi" :
4783 attr
->values
[i
].resolution
.units
==
4784 IPP_RES_PER_CM
? "dpcm" : "unknown");
4789 case IPP_TAG_RANGE
:
4790 for (i
= 0; i
< attr
->num_values
; i
++)
4792 if (attr
->values
[i
].range
.lower
> attr
->values
[i
].range
.upper
)
4797 "\"%s\": Bad rangeOfInteger value %d-%d - lower "
4798 "greater than upper (RFC 2911 section 4.1.13).",
4799 attr
->name
, attr
->values
[i
].range
.lower
,
4800 attr
->values
[i
].range
.upper
);
4805 case IPP_TAG_BEGIN_COLLECTION
:
4806 for (i
= 0; i
< attr
->num_values
; i
++)
4808 for (colattr
= attr
->values
[i
].collection
->attrs
;
4810 colattr
= colattr
->next
)
4812 if (!validate_attr(NULL
, colattr
))
4819 if (colattr
&& errors
)
4821 add_stringf(errors
, "\"%s\": Bad collection value.", attr
->name
);
4825 validate_attr(errors
, colattr
);
4826 colattr
= colattr
->next
;
4833 case IPP_TAG_TEXTLANG
:
4834 for (i
= 0; i
< attr
->num_values
; i
++)
4836 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
4838 if ((*ptr
& 0xe0) == 0xc0)
4841 if ((*ptr
& 0xc0) != 0x80)
4844 else if ((*ptr
& 0xf0) == 0xe0)
4847 if ((*ptr
& 0xc0) != 0x80)
4850 if ((*ptr
& 0xc0) != 0x80)
4853 else if ((*ptr
& 0xf8) == 0xf0)
4856 if ((*ptr
& 0xc0) != 0x80)
4859 if ((*ptr
& 0xc0) != 0x80)
4862 if ((*ptr
& 0xc0) != 0x80)
4865 else if (*ptr
& 0x80)
4874 "\"%s\": Bad text value \"%s\" - bad UTF-8 "
4875 "sequence (RFC 2911 section 4.1.1).", attr
->name
,
4876 attr
->values
[i
].string
.text
);
4879 if ((ptr
- attr
->values
[i
].string
.text
) > (IPP_MAX_TEXT
- 1))
4884 "\"%s\": Bad text value \"%s\" - bad length %d "
4885 "(RFC 2911 section 4.1.1).", attr
->name
,
4886 attr
->values
[i
].string
.text
,
4887 (int)strlen(attr
->values
[i
].string
.text
));
4893 case IPP_TAG_NAMELANG
:
4894 for (i
= 0; i
< attr
->num_values
; i
++)
4896 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
4898 if ((*ptr
& 0xe0) == 0xc0)
4901 if ((*ptr
& 0xc0) != 0x80)
4904 else if ((*ptr
& 0xf0) == 0xe0)
4907 if ((*ptr
& 0xc0) != 0x80)
4910 if ((*ptr
& 0xc0) != 0x80)
4913 else if ((*ptr
& 0xf8) == 0xf0)
4916 if ((*ptr
& 0xc0) != 0x80)
4919 if ((*ptr
& 0xc0) != 0x80)
4922 if ((*ptr
& 0xc0) != 0x80)
4925 else if (*ptr
& 0x80)
4934 "\"%s\": Bad name value \"%s\" - bad UTF-8 "
4935 "sequence (RFC 2911 section 4.1.2).", attr
->name
,
4936 attr
->values
[i
].string
.text
);
4939 if ((ptr
- attr
->values
[i
].string
.text
) > (IPP_MAX_NAME
- 1))
4944 "\"%s\": Bad name value \"%s\" - bad length %d "
4945 "(RFC 2911 section 4.1.2).", attr
->name
,
4946 attr
->values
[i
].string
.text
,
4947 (int)strlen(attr
->values
[i
].string
.text
));
4952 case IPP_TAG_KEYWORD
:
4953 for (i
= 0; i
< attr
->num_values
; i
++)
4955 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
4956 if (!isalnum(*ptr
& 255) && *ptr
!= '-' && *ptr
!= '.' &&
4960 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
4965 "\"%s\": Bad keyword value \"%s\" - invalid "
4966 "character (RFC 2911 section 4.1.3).",
4967 attr
->name
, attr
->values
[i
].string
.text
);
4970 if ((ptr
- attr
->values
[i
].string
.text
) > (IPP_MAX_KEYWORD
- 1))
4975 "\"%s\": Bad keyword value \"%s\" - bad "
4976 "length %d (RFC 2911 section 4.1.3).",
4977 attr
->name
, attr
->values
[i
].string
.text
,
4978 (int)strlen(attr
->values
[i
].string
.text
));
4984 for (i
= 0; i
< attr
->num_values
; i
++)
4986 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
,
4987 attr
->values
[i
].string
.text
,
4988 scheme
, sizeof(scheme
),
4989 userpass
, sizeof(userpass
),
4990 hostname
, sizeof(hostname
),
4991 &port
, resource
, sizeof(resource
));
4993 if (uri_status
< HTTP_URI_OK
)
4998 "\"%s\": Bad URI value \"%s\" - %s "
4999 "(RFC 2911 section 4.1.5).", attr
->name
,
5000 attr
->values
[i
].string
.text
,
5001 URIStatusStrings
[uri_status
-
5002 HTTP_URI_OVERFLOW
]);
5005 if (strlen(attr
->values
[i
].string
.text
) > (IPP_MAX_URI
- 1))
5010 "\"%s\": Bad URI value \"%s\" - bad length %d "
5011 "(RFC 2911 section 4.1.5).", attr
->name
,
5012 attr
->values
[i
].string
.text
,
5013 (int)strlen(attr
->values
[i
].string
.text
));
5018 case IPP_TAG_URISCHEME
:
5019 for (i
= 0; i
< attr
->num_values
; i
++)
5021 ptr
= attr
->values
[i
].string
.text
;
5022 if (islower(*ptr
& 255))
5024 for (ptr
++; *ptr
; ptr
++)
5025 if (!islower(*ptr
& 255) && !isdigit(*ptr
& 255) &&
5026 *ptr
!= '+' && *ptr
!= '-' && *ptr
!= '.')
5030 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
5035 "\"%s\": Bad uriScheme value \"%s\" - bad "
5036 "characters (RFC 2911 section 4.1.6).",
5037 attr
->name
, attr
->values
[i
].string
.text
);
5040 if ((ptr
- attr
->values
[i
].string
.text
) > (IPP_MAX_URISCHEME
- 1))
5045 "\"%s\": Bad uriScheme value \"%s\" - bad "
5046 "length %d (RFC 2911 section 4.1.6).",
5047 attr
->name
, attr
->values
[i
].string
.text
,
5048 (int)strlen(attr
->values
[i
].string
.text
));
5053 case IPP_TAG_CHARSET
:
5054 for (i
= 0; i
< attr
->num_values
; i
++)
5056 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
5057 if (!isprint(*ptr
& 255) || isupper(*ptr
& 255) ||
5058 isspace(*ptr
& 255))
5061 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
5066 "\"%s\": Bad charset value \"%s\" - bad "
5067 "characters (RFC 2911 section 4.1.7).",
5068 attr
->name
, attr
->values
[i
].string
.text
);
5071 if ((ptr
- attr
->values
[i
].string
.text
) > (IPP_MAX_CHARSET
- 1))
5076 "\"%s\": Bad charset value \"%s\" - bad "
5077 "length %d (RFC 2911 section 4.1.7).",
5078 attr
->name
, attr
->values
[i
].string
.text
,
5079 (int)strlen(attr
->values
[i
].string
.text
));
5084 case IPP_TAG_LANGUAGE
:
5086 * The following regular expression is derived from the ABNF for
5087 * language tags in RFC 4646. All I can say is that this is the
5088 * easiest way to check the values...
5091 if ((i
= regcomp(&re
,
5093 "(([a-z]{2,3}(-[a-z][a-z][a-z]){0,3})|[a-z]{4,8})"
5095 "(-[a-z][a-z][a-z][a-z]){0,1}" /* script */
5096 "(-([a-z][a-z]|[0-9][0-9][0-9])){0,1}" /* region */
5097 "(-([a-z]{5,8}|[0-9][0-9][0-9]))*" /* variant */
5098 "(-[a-wy-z](-[a-z0-9]{2,8})+)*" /* extension */
5099 "(-x(-[a-z0-9]{1,8})+)*" /* privateuse */
5101 "x(-[a-z0-9]{1,8})+" /* privateuse */
5103 "[a-z]{1,3}(-[a-z][0-9]{2,8}){1,2}" /* grandfathered */
5105 REG_NOSUB
| REG_EXTENDED
)) != 0)
5107 char temp
[256]; /* Temporary error string */
5109 regerror(i
, &re
, temp
, sizeof(temp
));
5110 print_fatal_error("Unable to compile naturalLanguage regular "
5111 "expression: %s.", temp
);
5115 for (i
= 0; i
< attr
->num_values
; i
++)
5117 if (regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
5122 "\"%s\": Bad naturalLanguage value \"%s\" - bad "
5123 "characters (RFC 2911 section 4.1.8).",
5124 attr
->name
, attr
->values
[i
].string
.text
);
5127 if (strlen(attr
->values
[i
].string
.text
) > (IPP_MAX_LANGUAGE
- 1))
5132 "\"%s\": Bad naturalLanguage value \"%s\" - bad "
5133 "length %d (RFC 2911 section 4.1.8).",
5134 attr
->name
, attr
->values
[i
].string
.text
,
5135 (int)strlen(attr
->values
[i
].string
.text
));
5142 case IPP_TAG_MIMETYPE
:
5144 * The following regular expression is derived from the ABNF for
5145 * language tags in RFC 2045 and 4288. All I can say is that this is
5146 * the easiest way to check the values...
5149 if ((i
= regcomp(&re
,
5151 "[-a-zA-Z0-9!#$&.+^_]{1,127}" /* type-name */
5153 "[-a-zA-Z0-9!#$&.+^_]{1,127}" /* subtype-name */
5154 "(;[-a-zA-Z0-9!#$&.+^_]{1,127}=" /* parameter= */
5155 "([-a-zA-Z0-9!#$&.+^_]{1,127}|\"[^\"]*\"))*"
5158 REG_NOSUB
| REG_EXTENDED
)) != 0)
5160 char temp
[256]; /* Temporary error string */
5162 regerror(i
, &re
, temp
, sizeof(temp
));
5163 print_fatal_error("Unable to compile mimeMediaType regular "
5164 "expression: %s.", temp
);
5168 for (i
= 0; i
< attr
->num_values
; i
++)
5170 if (regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
5175 "\"%s\": Bad mimeMediaType value \"%s\" - bad "
5176 "characters (RFC 2911 section 4.1.9).",
5177 attr
->name
, attr
->values
[i
].string
.text
);
5180 if (strlen(attr
->values
[i
].string
.text
) > (IPP_MAX_MIMETYPE
- 1))
5185 "\"%s\": Bad mimeMediaType value \"%s\" - bad "
5186 "length %d (RFC 2911 section 4.1.9).",
5187 attr
->name
, attr
->values
[i
].string
.text
,
5188 (int)strlen(attr
->values
[i
].string
.text
));
5204 * 'with_value()' - Test a WITH-VALUE predicate.
5207 static int /* O - 1 on match, 0 on non-match */
5208 with_value(cups_array_t
*errors
, /* I - Errors array */
5209 char *value
, /* I - Value string */
5210 int flags
, /* I - Flags for match */
5211 ipp_attribute_t
*attr
, /* I - Attribute to compare */
5212 char *matchbuf
, /* I - Buffer to hold matching value */
5213 size_t matchlen
) /* I - Length of match buffer */
5215 int i
, /* Looping var */
5217 char *valptr
; /* Pointer into value */
5221 match
= (flags
& _CUPS_WITH_ALL
) ? 1 : 0;
5224 * NULL matches everything.
5227 if (!value
|| !*value
)
5231 * Compare the value string to the attribute value.
5234 switch (attr
->value_tag
)
5236 case IPP_TAG_INTEGER
:
5238 for (i
= 0; i
< attr
->num_values
; i
++)
5240 char op
, /* Comparison operator */
5241 *nextptr
; /* Next pointer */
5242 int intvalue
, /* Integer value */
5243 valmatch
= 0; /* Does the current value match? */
5247 while (isspace(*valptr
& 255) || isdigit(*valptr
& 255) ||
5248 *valptr
== '-' || *valptr
== ',' || *valptr
== '<' ||
5249 *valptr
== '=' || *valptr
== '>')
5252 while (*valptr
&& !isdigit(*valptr
& 255) && *valptr
!= '-')
5254 if (*valptr
== '<' || *valptr
== '>' || *valptr
== '=')
5262 intvalue
= strtol(valptr
, &nextptr
, 0);
5263 if (nextptr
== valptr
)
5267 if ((op
== '=' && attr
->values
[i
].integer
== intvalue
) ||
5268 (op
== '<' && attr
->values
[i
].integer
< intvalue
) ||
5269 (op
== '>' && attr
->values
[i
].integer
> intvalue
))
5272 snprintf(matchbuf
, matchlen
, "%d",
5273 attr
->values
[i
].integer
);
5280 if (flags
& _CUPS_WITH_ALL
)
5295 if (!match
&& errors
)
5297 for (i
= 0; i
< attr
->num_values
; i
++)
5298 add_stringf(errors
, "GOT: %s=%d", attr
->name
,
5299 attr
->values
[i
].integer
);
5303 case IPP_TAG_RANGE
:
5304 for (i
= 0; i
< attr
->num_values
; i
++)
5306 char op
, /* Comparison operator */
5307 *nextptr
; /* Next pointer */
5308 int intvalue
, /* Integer value */
5309 valmatch
= 0; /* Does the current value match? */
5313 while (isspace(*valptr
& 255) || isdigit(*valptr
& 255) ||
5314 *valptr
== '-' || *valptr
== ',' || *valptr
== '<' ||
5315 *valptr
== '=' || *valptr
== '>')
5318 while (*valptr
&& !isdigit(*valptr
& 255) && *valptr
!= '-')
5320 if (*valptr
== '<' || *valptr
== '>' || *valptr
== '=')
5328 intvalue
= strtol(valptr
, &nextptr
, 0);
5329 if (nextptr
== valptr
)
5333 if ((op
== '=' && (attr
->values
[i
].range
.lower
== intvalue
||
5334 attr
->values
[i
].range
.upper
== intvalue
)) ||
5335 (op
== '<' && attr
->values
[i
].range
.upper
< intvalue
) ||
5336 (op
== '>' && attr
->values
[i
].range
.upper
> intvalue
))
5339 snprintf(matchbuf
, matchlen
, "%d-%d",
5340 attr
->values
[0].range
.lower
,
5341 attr
->values
[0].range
.upper
);
5348 if (flags
& _CUPS_WITH_ALL
)
5363 if (!match
&& errors
)
5365 for (i
= 0; i
< attr
->num_values
; i
++)
5366 add_stringf(errors
, "GOT: %s=%d-%d", attr
->name
,
5367 attr
->values
[i
].range
.lower
,
5368 attr
->values
[i
].range
.upper
);
5372 case IPP_TAG_BOOLEAN
:
5373 for (i
= 0; i
< attr
->num_values
; i
++)
5375 if (!strcmp(value
, "true") == attr
->values
[i
].boolean
)
5378 strlcpy(matchbuf
, value
, matchlen
);
5380 if (!(flags
& _CUPS_WITH_ALL
))
5386 else if (flags
& _CUPS_WITH_ALL
)
5393 if (!match
&& errors
)
5395 for (i
= 0; i
< attr
->num_values
; i
++)
5396 add_stringf(errors
, "GOT: %s=%s", attr
->name
,
5397 attr
->values
[i
].boolean
? "true" : "false");
5401 case IPP_TAG_NOVALUE
:
5402 case IPP_TAG_UNKNOWN
:
5405 case IPP_TAG_CHARSET
:
5406 case IPP_TAG_KEYWORD
:
5407 case IPP_TAG_LANGUAGE
:
5408 case IPP_TAG_MIMETYPE
:
5410 case IPP_TAG_NAMELANG
:
5412 case IPP_TAG_TEXTLANG
:
5414 case IPP_TAG_URISCHEME
:
5415 if (flags
& _CUPS_WITH_REGEX
)
5418 * Value is an extended, case-sensitive POSIX regular expression...
5421 regex_t re
; /* Regular expression */
5423 if ((i
= regcomp(&re
, value
, REG_EXTENDED
| REG_NOSUB
)) != 0)
5425 char temp
[256]; /* Temporary string */
5427 regerror(i
, &re
, temp
, sizeof(temp
));
5429 print_fatal_error("Unable to compile WITH-VALUE regular expression "
5430 "\"%s\" - %s", value
, temp
);
5435 * See if ALL of the values match the given regular expression.
5438 for (i
= 0; i
< attr
->num_values
; i
++)
5440 if (!regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
5443 strlcpy(matchbuf
, attr
->values
[i
].string
.text
, matchlen
);
5445 if (!(flags
& _CUPS_WITH_ALL
))
5451 else if (flags
& _CUPS_WITH_ALL
)
5463 * Value is a literal string, see if the value(s) match...
5466 for (i
= 0; i
< attr
->num_values
; i
++)
5468 if (!strcmp(value
, attr
->values
[i
].string
.text
))
5471 strlcpy(matchbuf
, attr
->values
[i
].string
.text
, matchlen
);
5473 if (!(flags
& _CUPS_WITH_ALL
))
5479 else if (flags
& _CUPS_WITH_ALL
)
5487 if (!match
&& errors
)
5489 for (i
= 0; i
< attr
->num_values
; i
++)
5490 add_stringf(errors
, "GOT: %s=\"%s\"", attr
->name
,
5491 attr
->values
[i
].string
.text
);