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 ipp_t
*request
= NULL
, /* IPP request */
770 *response
= NULL
; /* IPP response */
771 size_t length
; /* Length of IPP request */
772 http_status_t status
; /* HTTP status */
773 cups_file_t
*reqfile
; /* File to send */
774 ssize_t bytes
; /* Bytes read/written */
775 char attr
[128]; /* Attribute name */
776 ipp_op_t op
; /* Operation */
777 ipp_tag_t group
; /* Current group */
778 ipp_tag_t value
; /* Current value type */
779 ipp_attribute_t
*attrptr
, /* Attribute pointer */
780 *found
, /* Found attribute */
781 *lastcol
= NULL
; /* Last collection attribute */
782 char name
[1024]; /* Name of test */
783 char filename
[1024]; /* Filename */
784 _cups_transfer_t transfer
; /* To chunk or not to chunk */
785 int version
, /* IPP version number to use */
786 skip_test
; /* Skip this test? */
787 int num_statuses
= 0; /* Number of valid status codes */
788 _cups_status_t statuses
[100], /* Valid status codes */
789 *last_status
; /* Last STATUS (for predicates) */
790 int num_expects
= 0; /* Number of expected attributes */
791 _cups_expect_t expects
[200], /* Expected attributes */
792 *expect
, /* Current expected attribute */
793 *last_expect
; /* Last EXPECT (for predicates) */
794 int num_displayed
= 0; /* Number of displayed attributes */
795 char *displayed
[200]; /* Displayed attributes */
796 size_t widths
[200]; /* Width of columns */
797 cups_array_t
*a
, /* Duplicate attribute array */
798 *errors
= NULL
; /* Errors array */
799 const char *error
; /* Current error */
803 * Open the test file...
806 if ((fp
= fopen(testfile
, "r")) == NULL
)
808 print_fatal_error("Unable to open test file %s - %s", testfile
,
815 * Connect to the server...
818 if ((http
= _httpCreate(vars
->hostname
, vars
->port
, NULL
, vars
->encryption
,
819 vars
->family
)) == NULL
)
821 print_fatal_error("Unable to connect to %s on port %d - %s", vars
->hostname
,
822 vars
->port
, cupsLastErrorString());
827 if (httpReconnect(http
))
829 print_fatal_error("Unable to connect to %s on port %d - %s", vars
->hostname
,
830 vars
->port
, cupsLastErrorString());
835 if (vars
->timeout
> 0.0)
836 httpSetTimeout(http
, vars
->timeout
, timeout_cb
, NULL
);
842 CUPS_SRAND(time(NULL
));
844 errors
= cupsArrayNew3(NULL
, NULL
, NULL
, 0, (cups_acopy_func_t
)strdup
,
845 (cups_afree_func_t
)free
);
848 request_id
= (CUPS_RAND() % 1000) * 137 + 1;
850 while (!Cancel
&& get_token(fp
, token
, sizeof(token
), &linenum
) != NULL
)
853 * Expect an open brace...
856 if (!strcmp(token
, "DEFINE"))
862 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
863 get_token(fp
, temp
, sizeof(temp
), &linenum
))
865 expand_variables(vars
, token
, temp
, sizeof(token
));
866 set_variable(vars
, attr
, token
);
870 print_fatal_error("Missing DEFINE name and/or value on line %d.",
878 else if (!strcmp(token
, "DEFINE-DEFAULT"))
881 * DEFINE-DEFAULT name value
884 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
885 get_token(fp
, temp
, sizeof(temp
), &linenum
))
887 expand_variables(vars
, token
, temp
, sizeof(token
));
888 if (!get_variable(vars
, attr
))
889 set_variable(vars
, attr
, token
);
893 print_fatal_error("Missing DEFINE-DEFAULT name and/or value on line "
901 else if (!strcmp(token
, "IGNORE-ERRORS"))
908 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
909 (!_cups_strcasecmp(temp
, "yes") || !_cups_strcasecmp(temp
, "no")))
911 IgnoreErrors
= !_cups_strcasecmp(temp
, "yes");
915 print_fatal_error("Missing IGNORE-ERRORS value on line %d.", linenum
);
922 else if (!strcmp(token
, "INCLUDE"))
929 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
932 * Map the filename to and then run the tests...
935 if (!do_tests(vars
, get_filename(testfile
, filename
, temp
,
946 print_fatal_error("Missing INCLUDE filename on line %d.", linenum
);
954 else if (!strcmp(token
, "INCLUDE-IF-DEFINED"))
957 * INCLUDE-IF-DEFINED name "filename"
958 * INCLUDE-IF-DEFINED name <filename>
961 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
962 get_token(fp
, temp
, sizeof(temp
), &linenum
))
965 * Map the filename to and then run the tests...
968 if (get_variable(vars
, attr
) &&
969 !do_tests(vars
, get_filename(testfile
, filename
, temp
,
980 print_fatal_error("Missing INCLUDE-IF-DEFINED name or filename on line "
989 else if (!strcmp(token
, "INCLUDE-IF-NOT-DEFINED"))
992 * INCLUDE-IF-NOT-DEFINED name "filename"
993 * INCLUDE-IF-NOT-DEFINED name <filename>
996 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
997 get_token(fp
, temp
, sizeof(temp
), &linenum
))
1000 * Map the filename to and then run the tests...
1003 if (!get_variable(vars
, attr
) &&
1004 !do_tests(vars
, get_filename(testfile
, filename
, temp
,
1015 print_fatal_error("Missing INCLUDE-IF-NOT-DEFINED name or filename on "
1016 "line %d.", linenum
);
1024 else if (!strcmp(token
, "SKIP-IF-DEFINED"))
1027 * SKIP-IF-DEFINED variable
1030 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1032 if (get_variable(vars
, temp
))
1037 print_fatal_error("Missing SKIP-IF-DEFINED variable on line %d.",
1043 else if (!strcmp(token
, "SKIP-IF-NOT-DEFINED"))
1046 * SKIP-IF-NOT-DEFINED variable
1049 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1051 if (!get_variable(vars
, temp
))
1056 print_fatal_error("Missing SKIP-IF-NOT-DEFINED variable on line %d.",
1062 else if (!strcmp(token
, "TRANSFER"))
1070 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1072 if (!strcmp(temp
, "auto"))
1073 Transfer
= _CUPS_TRANSFER_AUTO
;
1074 else if (!strcmp(temp
, "chunked"))
1075 Transfer
= _CUPS_TRANSFER_CHUNKED
;
1076 else if (!strcmp(temp
, "length"))
1077 Transfer
= _CUPS_TRANSFER_LENGTH
;
1080 print_fatal_error("Bad TRANSFER value \"%s\" on line %d.", temp
,
1088 print_fatal_error("Missing TRANSFER value on line %d.", linenum
);
1095 else if (!strcmp(token
, "VERSION"))
1097 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1099 if (!strcmp(temp
, "1.0"))
1101 else if (!strcmp(temp
, "1.1"))
1103 else if (!strcmp(temp
, "2.0"))
1105 else if (!strcmp(temp
, "2.1"))
1107 else if (!strcmp(temp
, "2.2"))
1111 print_fatal_error("Bad VERSION \"%s\" on line %d.", temp
, linenum
);
1118 print_fatal_error("Missing VERSION number on line %d.", linenum
);
1125 else if (strcmp(token
, "{"))
1127 print_fatal_error("Unexpected token %s seen on line %d.", token
, linenum
);
1133 * Initialize things...
1138 if (Output
== _CUPS_OUTPUT_PLIST
)
1140 else if (Output
== _CUPS_OUTPUT_TEST
)
1141 printf("\"%s\":\n", testfile
);
1146 strlcpy(resource
, vars
->resource
, sizeof(resource
));
1151 group
= IPP_TAG_ZERO
;
1152 ignore_errors
= IgnoreErrors
;
1159 transfer
= Transfer
;
1161 strlcpy(name
, testfile
, sizeof(name
));
1162 if (strrchr(name
, '.') != NULL
)
1163 *strrchr(name
, '.') = '\0';
1166 * Parse until we see a close brace...
1169 while (get_token(fp
, token
, sizeof(token
), &linenum
) != NULL
)
1171 if (_cups_strcasecmp(token
, "COUNT") &&
1172 _cups_strcasecmp(token
, "DEFINE-MATCH") &&
1173 _cups_strcasecmp(token
, "DEFINE-NO-MATCH") &&
1174 _cups_strcasecmp(token
, "DEFINE-VALUE") &&
1175 _cups_strcasecmp(token
, "IF-DEFINED") &&
1176 _cups_strcasecmp(token
, "IF-NOT-DEFINED") &&
1177 _cups_strcasecmp(token
, "IN-GROUP") &&
1178 _cups_strcasecmp(token
, "OF-TYPE") &&
1179 _cups_strcasecmp(token
, "REPEAT-LIMIT") &&
1180 _cups_strcasecmp(token
, "REPEAT-MATCH") &&
1181 _cups_strcasecmp(token
, "REPEAT-NO-MATCH") &&
1182 _cups_strcasecmp(token
, "SAME-COUNT-AS") &&
1183 _cups_strcasecmp(token
, "WITH-ALL-VALUES") &&
1184 _cups_strcasecmp(token
, "WITH-VALUE"))
1187 if (_cups_strcasecmp(token
, "IF-DEFINED") &&
1188 _cups_strcasecmp(token
, "IF-NOT-DEFINED") &&
1189 _cups_strcasecmp(token
, "REPEAT-LIMIT") &&
1190 _cups_strcasecmp(token
, "REPEAT-MATCH") &&
1191 _cups_strcasecmp(token
, "REPEAT-NO-MATCH"))
1194 if (!strcmp(token
, "}"))
1196 else if (!strcmp(token
, "{") && lastcol
)
1199 * Another collection value
1202 ipp_t
*col
= get_collection(vars
, fp
, &linenum
);
1203 /* Collection value */
1207 ipp_attribute_t
*tempcol
; /* Pointer to new buffer */
1211 * Reallocate memory...
1214 if ((tempcol
= realloc(lastcol
, sizeof(ipp_attribute_t
) +
1215 (lastcol
->num_values
+ 1) *
1216 sizeof(_ipp_value_t
))) == NULL
)
1218 print_fatal_error("Unable to allocate memory on line %d.", linenum
);
1223 if (tempcol
!= lastcol
)
1226 * Reset pointers in the list...
1230 request
->prev
->next
= tempcol
;
1232 request
->attrs
= tempcol
;
1234 lastcol
= request
->current
= request
->last
= tempcol
;
1237 lastcol
->values
[lastcol
->num_values
].collection
= col
;
1238 lastcol
->num_values
++;
1246 else if (!strcmp(token
, "DEFINE"))
1252 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
1253 get_token(fp
, temp
, sizeof(temp
), &linenum
))
1255 expand_variables(vars
, token
, temp
, sizeof(token
));
1256 set_variable(vars
, attr
, token
);
1260 print_fatal_error("Missing DEFINE name and/or value on line %d.",
1266 else if (!strcmp(token
, "IGNORE-ERRORS"))
1273 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
1274 (!_cups_strcasecmp(temp
, "yes") || !_cups_strcasecmp(temp
, "no")))
1276 ignore_errors
= !_cups_strcasecmp(temp
, "yes");
1280 print_fatal_error("Missing IGNORE-ERRORS value on line %d.", linenum
);
1287 else if (!_cups_strcasecmp(token
, "NAME"))
1293 get_token(fp
, name
, sizeof(name
), &linenum
);
1295 else if (!strcmp(token
, "REQUEST-ID"))
1302 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1304 if (isdigit(temp
[0] & 255))
1305 request_id
= atoi(temp
);
1306 else if (!_cups_strcasecmp(temp
, "random"))
1307 request_id
= (CUPS_RAND() % 1000) * 137 + 1;
1310 print_fatal_error("Bad REQUEST-ID value \"%s\" on line %d.", temp
,
1318 print_fatal_error("Missing REQUEST-ID value on line %d.", linenum
);
1323 else if (!strcmp(token
, "SKIP-IF-DEFINED"))
1326 * SKIP-IF-DEFINED variable
1329 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1331 if (get_variable(vars
, temp
))
1336 print_fatal_error("Missing SKIP-IF-DEFINED value on line %d.",
1342 else if (!strcmp(token
, "SKIP-IF-NOT-DEFINED"))
1345 * SKIP-IF-NOT-DEFINED variable
1348 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1350 if (!get_variable(vars
, temp
))
1355 print_fatal_error("Missing SKIP-IF-NOT-DEFINED value on line %d.",
1361 else if (!strcmp(token
, "SKIP-PREVIOUS-ERROR"))
1364 * SKIP-PREVIOUS-ERROR yes
1365 * SKIP-PREVIOUS-ERROR no
1368 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
1369 (!_cups_strcasecmp(temp
, "yes") || !_cups_strcasecmp(temp
, "no")))
1371 skip_previous
= !_cups_strcasecmp(temp
, "yes");
1375 print_fatal_error("Missing SKIP-PREVIOUS-ERROR value on line %d.", linenum
);
1382 else if (!strcmp(token
, "TRANSFER"))
1390 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1392 if (!strcmp(temp
, "auto"))
1393 transfer
= _CUPS_TRANSFER_AUTO
;
1394 else if (!strcmp(temp
, "chunked"))
1395 transfer
= _CUPS_TRANSFER_CHUNKED
;
1396 else if (!strcmp(temp
, "length"))
1397 transfer
= _CUPS_TRANSFER_LENGTH
;
1400 print_fatal_error("Bad TRANSFER value \"%s\" on line %d.", temp
,
1408 print_fatal_error("Missing TRANSFER value on line %d.", linenum
);
1413 else if (!_cups_strcasecmp(token
, "VERSION"))
1415 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1417 if (!strcmp(temp
, "0.0"))
1419 else if (!strcmp(temp
, "1.0"))
1421 else if (!strcmp(temp
, "1.1"))
1423 else if (!strcmp(temp
, "2.0"))
1425 else if (!strcmp(temp
, "2.1"))
1427 else if (!strcmp(temp
, "2.2"))
1431 print_fatal_error("Bad VERSION \"%s\" on line %d.", temp
, linenum
);
1438 print_fatal_error("Missing VERSION number on line %d.", linenum
);
1443 else if (!_cups_strcasecmp(token
, "RESOURCE"))
1449 if (!get_token(fp
, resource
, sizeof(resource
), &linenum
))
1451 print_fatal_error("Missing RESOURCE path on line %d.", linenum
);
1456 else if (!_cups_strcasecmp(token
, "OPERATION"))
1462 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1464 print_fatal_error("Missing OPERATION code on line %d.", linenum
);
1469 if ((op
= ippOpValue(token
)) == (ipp_op_t
)-1 &&
1470 (op
= strtol(token
, NULL
, 0)) == 0)
1472 print_fatal_error("Bad OPERATION code \"%s\" on line %d.", token
,
1478 else if (!_cups_strcasecmp(token
, "GROUP"))
1481 * Attribute group...
1484 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1486 print_fatal_error("Missing GROUP tag on line %d.", linenum
);
1491 if ((value
= ippTagValue(token
)) < 0)
1493 print_fatal_error("Bad GROUP tag \"%s\" on line %d.", token
, linenum
);
1499 ippAddSeparator(request
);
1503 else if (!_cups_strcasecmp(token
, "DELAY"))
1506 * Delay before operation...
1511 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1513 print_fatal_error("Missing DELAY value on line %d.", linenum
);
1518 if ((delay
= _cupsStrScand(token
, NULL
, localeconv())) <= 0.0)
1520 print_fatal_error("Bad DELAY value \"%s\" on line %d.", token
,
1527 if (Output
== _CUPS_OUTPUT_TEST
)
1528 printf(" [%g second delay]\n", delay
);
1530 usleep((int)(1000000.0 * delay
));
1533 else if (!_cups_strcasecmp(token
, "ATTR"))
1539 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1541 print_fatal_error("Missing ATTR value tag on line %d.", linenum
);
1546 if ((value
= ippTagValue(token
)) == IPP_TAG_ZERO
)
1548 print_fatal_error("Bad ATTR value tag \"%s\" on line %d.", token
,
1554 if (!get_token(fp
, attr
, sizeof(attr
), &linenum
))
1556 print_fatal_error("Missing ATTR name on line %d.", linenum
);
1561 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1563 print_fatal_error("Missing ATTR value on line %d.", linenum
);
1568 expand_variables(vars
, token
, temp
, sizeof(token
));
1573 case IPP_TAG_BOOLEAN
:
1574 if (!_cups_strcasecmp(token
, "true"))
1575 attrptr
= ippAddBoolean(request
, group
, attr
, 1);
1577 attrptr
= ippAddBoolean(request
, group
, attr
, atoi(token
));
1580 case IPP_TAG_INTEGER
:
1582 if (!strchr(token
, ','))
1583 attrptr
= ippAddInteger(request
, group
, value
, attr
,
1584 strtol(token
, &tokenptr
, 0));
1587 int values
[100], /* Values */
1588 num_values
= 1; /* Number of values */
1590 values
[0] = strtol(token
, &tokenptr
, 10);
1591 while (tokenptr
&& *tokenptr
&&
1592 num_values
< (int)(sizeof(values
) / sizeof(values
[0])))
1594 if (*tokenptr
== ',')
1596 else if (!isdigit(*tokenptr
& 255) && *tokenptr
!= '-')
1599 values
[num_values
] = strtol(tokenptr
, &tokenptr
, 0);
1603 attrptr
= ippAddIntegers(request
, group
, value
, attr
, num_values
, values
);
1606 if (!tokenptr
|| *tokenptr
)
1608 print_fatal_error("Bad %s value \"%s\" on line %d.",
1609 ippTagString(value
), token
, linenum
);
1615 case IPP_TAG_RESOLUTION
:
1617 int xres
, /* X resolution */
1618 yres
; /* Y resolution */
1619 char *ptr
; /* Pointer into value */
1621 xres
= yres
= strtol(token
, (char **)&ptr
, 10);
1622 if (ptr
> token
&& xres
> 0)
1625 yres
= strtol(ptr
+ 1, (char **)&ptr
, 10);
1628 if (ptr
<= token
|| xres
<= 0 || yres
<= 0 || !ptr
||
1629 (_cups_strcasecmp(ptr
, "dpi") &&
1630 _cups_strcasecmp(ptr
, "dpc") &&
1631 _cups_strcasecmp(ptr
, "dpcm") &&
1632 _cups_strcasecmp(ptr
, "other")))
1634 print_fatal_error("Bad resolution value \"%s\" on line %d.",
1640 if (!_cups_strcasecmp(ptr
, "dpi"))
1641 attrptr
= ippAddResolution(request
, group
, attr
, IPP_RES_PER_INCH
,
1643 else if (!_cups_strcasecmp(ptr
, "dpc") ||
1644 !_cups_strcasecmp(ptr
, "dpcm"))
1645 attrptr
= ippAddResolution(request
, group
, attr
, IPP_RES_PER_CM
,
1648 attrptr
= ippAddResolution(request
, group
, attr
, (ipp_res_t
)0,
1653 case IPP_TAG_RANGE
:
1655 int lowers
[4], /* Lower value */
1656 uppers
[4], /* Upper values */
1657 num_vals
; /* Number of values */
1660 num_vals
= sscanf(token
, "%d-%d,%d-%d,%d-%d,%d-%d",
1661 lowers
+ 0, uppers
+ 0,
1662 lowers
+ 1, uppers
+ 1,
1663 lowers
+ 2, uppers
+ 2,
1664 lowers
+ 3, uppers
+ 3);
1666 if ((num_vals
& 1) || num_vals
== 0)
1668 print_fatal_error("Bad rangeOfInteger value \"%s\" on line "
1669 "%d.", token
, linenum
);
1674 attrptr
= ippAddRanges(request
, group
, attr
, num_vals
/ 2, lowers
,
1679 case IPP_TAG_BEGIN_COLLECTION
:
1680 if (!strcmp(token
, "{"))
1682 ipp_t
*col
= get_collection(vars
, fp
, &linenum
);
1683 /* Collection value */
1687 attrptr
= lastcol
= ippAddCollection(request
, group
, attr
, col
);
1698 print_fatal_error("Bad ATTR collection value on line %d.",
1705 case IPP_TAG_STRING
:
1706 attrptr
= ippAddOctetString(request
, group
, attr
, token
,
1711 print_fatal_error("Unsupported ATTR value tag %s on line %d.",
1712 ippTagString(value
), linenum
);
1716 case IPP_TAG_TEXTLANG
:
1717 case IPP_TAG_NAMELANG
:
1720 case IPP_TAG_KEYWORD
:
1722 case IPP_TAG_URISCHEME
:
1723 case IPP_TAG_CHARSET
:
1724 case IPP_TAG_LANGUAGE
:
1725 case IPP_TAG_MIMETYPE
:
1726 if (!strchr(token
, ','))
1727 attrptr
= ippAddString(request
, group
, value
, attr
, NULL
, token
);
1731 * Multiple string values...
1734 int num_values
; /* Number of values */
1735 char *values
[100], /* Values */
1736 *ptr
; /* Pointer to next value */
1742 for (ptr
= strchr(token
, ','); ptr
; ptr
= strchr(ptr
, ','))
1745 values
[num_values
] = ptr
;
1749 attrptr
= ippAddStrings(request
, group
, value
, attr
, num_values
,
1750 NULL
, (const char **)values
);
1757 print_fatal_error("Unable to add attribute on line %d: %s", linenum
,
1758 cupsLastErrorString());
1763 else if (!_cups_strcasecmp(token
, "FILE"))
1769 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1771 print_fatal_error("Missing FILE filename on line %d.", linenum
);
1776 expand_variables(vars
, token
, temp
, sizeof(token
));
1777 get_filename(testfile
, filename
, token
, sizeof(filename
));
1779 else if (!_cups_strcasecmp(token
, "STATUS"))
1785 if (num_statuses
>= (int)(sizeof(statuses
) / sizeof(statuses
[0])))
1787 print_fatal_error("Too many STATUS's on line %d.", linenum
);
1792 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1794 print_fatal_error("Missing STATUS code on line %d.", linenum
);
1799 if ((statuses
[num_statuses
].status
= ippErrorValue(token
))
1800 == (ipp_status_t
)-1 &&
1801 (statuses
[num_statuses
].status
= strtol(token
, NULL
, 0)) == 0)
1803 print_fatal_error("Bad STATUS code \"%s\" on line %d.", token
,
1809 last_status
= statuses
+ num_statuses
;
1812 last_status
->if_defined
= NULL
;
1813 last_status
->if_not_defined
= NULL
;
1814 last_status
->repeat_limit
= 1000;
1815 last_status
->repeat_match
= 0;
1816 last_status
->repeat_no_match
= 0;
1818 else if (!_cups_strcasecmp(token
, "EXPECT"))
1821 * Expected attributes...
1824 if (num_expects
>= (int)(sizeof(expects
) / sizeof(expects
[0])))
1826 print_fatal_error("Too many EXPECT's on line %d.", linenum
);
1831 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1833 print_fatal_error("Missing EXPECT name on line %d.", linenum
);
1838 last_expect
= expects
+ num_expects
;
1841 memset(last_expect
, 0, sizeof(_cups_expect_t
));
1842 last_expect
->repeat_limit
= 1000;
1844 if (token
[0] == '!')
1846 last_expect
->not_expect
= 1;
1847 last_expect
->name
= strdup(token
+ 1);
1849 else if (token
[0] == '?')
1851 last_expect
->optional
= 1;
1852 last_expect
->name
= strdup(token
+ 1);
1855 last_expect
->name
= strdup(token
);
1857 else if (!_cups_strcasecmp(token
, "COUNT"))
1859 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1861 print_fatal_error("Missing COUNT number on line %d.", linenum
);
1866 if ((i
= atoi(token
)) <= 0)
1868 print_fatal_error("Bad COUNT \"%s\" on line %d.", token
, linenum
);
1874 last_expect
->count
= i
;
1877 print_fatal_error("COUNT without a preceding EXPECT on line %d.",
1883 else if (!_cups_strcasecmp(token
, "DEFINE-MATCH"))
1885 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1887 print_fatal_error("Missing DEFINE-MATCH variable on line %d.",
1894 last_expect
->define_match
= strdup(token
);
1897 print_fatal_error("DEFINE-MATCH without a preceding EXPECT on line "
1903 else if (!_cups_strcasecmp(token
, "DEFINE-NO-MATCH"))
1905 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1907 print_fatal_error("Missing DEFINE-NO-MATCH variable on line %d.",
1914 last_expect
->define_no_match
= strdup(token
);
1917 print_fatal_error("DEFINE-NO-MATCH without a preceding EXPECT on "
1918 "line %d.", linenum
);
1923 else if (!_cups_strcasecmp(token
, "DEFINE-VALUE"))
1925 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1927 print_fatal_error("Missing DEFINE-VALUE variable on line %d.",
1934 last_expect
->define_value
= strdup(token
);
1937 print_fatal_error("DEFINE-VALUE without a preceding EXPECT on line "
1943 else if (!_cups_strcasecmp(token
, "OF-TYPE"))
1945 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1947 print_fatal_error("Missing OF-TYPE value tag(s) on line %d.",
1954 last_expect
->of_type
= strdup(token
);
1957 print_fatal_error("OF-TYPE without a preceding EXPECT on line %d.",
1963 else if (!_cups_strcasecmp(token
, "IN-GROUP"))
1965 ipp_tag_t in_group
; /* IN-GROUP value */
1968 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1970 print_fatal_error("Missing IN-GROUP group tag on line %d.", linenum
);
1975 if ((in_group
= ippTagValue(token
)) == (ipp_tag_t
)-1)
1978 else if (last_expect
)
1979 last_expect
->in_group
= in_group
;
1982 print_fatal_error("IN-GROUP without a preceding EXPECT on line %d.",
1988 else if (!_cups_strcasecmp(token
, "REPEAT-LIMIT"))
1990 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1992 print_fatal_error("Missing REPEAT-LIMIT value on line %d.", linenum
);
1996 else if (atoi(token
) <= 0)
1998 print_fatal_error("Bad REPEAT-LIMIT value on line %d.", linenum
);
2004 last_status
->repeat_limit
= atoi(token
);
2005 else if (last_expect
)
2006 last_expect
->repeat_limit
= atoi(token
);
2009 print_fatal_error("REPEAT-LIMIT without a preceding EXPECT or STATUS "
2010 "on line %d.", linenum
);
2015 else if (!_cups_strcasecmp(token
, "REPEAT-MATCH"))
2018 last_status
->repeat_match
= 1;
2019 else if (last_expect
)
2020 last_expect
->repeat_match
= 1;
2023 print_fatal_error("REPEAT-MATCH without a preceding EXPECT or STATUS "
2024 "on line %d.", linenum
);
2029 else if (!_cups_strcasecmp(token
, "REPEAT-NO-MATCH"))
2032 last_status
->repeat_no_match
= 1;
2033 else if (last_expect
)
2034 last_expect
->repeat_no_match
= 1;
2037 print_fatal_error("REPEAT-NO-MATCH without a preceding EXPECT or "
2038 "STATUS on ine %d.", linenum
);
2043 else if (!_cups_strcasecmp(token
, "SAME-COUNT-AS"))
2045 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2047 print_fatal_error("Missing SAME-COUNT-AS name on line %d.", linenum
);
2053 last_expect
->same_count_as
= strdup(token
);
2056 print_fatal_error("SAME-COUNT-AS without a preceding EXPECT on line "
2062 else if (!_cups_strcasecmp(token
, "IF-DEFINED"))
2064 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2066 print_fatal_error("Missing IF-DEFINED name on line %d.", linenum
);
2072 last_expect
->if_defined
= strdup(token
);
2073 else if (last_status
)
2074 last_status
->if_defined
= strdup(token
);
2077 print_fatal_error("IF-DEFINED without a preceding EXPECT or STATUS "
2078 "on line %d.", linenum
);
2083 else if (!_cups_strcasecmp(token
, "IF-NOT-DEFINED"))
2085 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2087 print_fatal_error("Missing IF-NOT-DEFINED name on line %d.", linenum
);
2093 last_expect
->if_not_defined
= strdup(token
);
2094 else if (last_status
)
2095 last_status
->if_not_defined
= strdup(token
);
2098 print_fatal_error("IF-NOT-DEFINED without a preceding EXPECT or STATUS "
2099 "on line %d.", linenum
);
2104 else if (!_cups_strcasecmp(token
, "WITH-ALL-VALUES") ||
2105 !_cups_strcasecmp(token
, "WITH-VALUE"))
2107 if (!_cups_strcasecmp(token
, "WITH-ALL-VALUES") && last_expect
)
2108 last_expect
->with_flags
= _CUPS_WITH_ALL
;
2110 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
2112 print_fatal_error("Missing %s value on line %d.", token
, linenum
);
2120 * Expand any variables in the value and then save it.
2123 expand_variables(vars
, token
, temp
, sizeof(token
));
2125 tokenptr
= token
+ strlen(token
) - 1;
2127 if (token
[0] == '/' && tokenptr
> token
&& *tokenptr
== '/')
2130 * WITH-VALUE is a POSIX extended regular expression.
2133 last_expect
->with_value
= calloc(1, tokenptr
- token
);
2134 last_expect
->with_flags
|= _CUPS_WITH_REGEX
;
2136 if (last_expect
->with_value
)
2137 memcpy(last_expect
->with_value
, token
+ 1, tokenptr
- token
- 1);
2142 * WITH-VALUE is a literal value...
2145 last_expect
->with_value
= strdup(token
);
2146 last_expect
->with_flags
|= _CUPS_WITH_LITERAL
;
2151 print_fatal_error("%s without a preceding EXPECT on line %d.", token
,
2157 else if (!_cups_strcasecmp(token
, "DISPLAY"))
2160 * Display attributes...
2163 if (num_displayed
>= (int)(sizeof(displayed
) / sizeof(displayed
[0])))
2165 print_fatal_error("Too many DISPLAY's on line %d", linenum
);
2170 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2172 print_fatal_error("Missing DISPLAY name on line %d.", linenum
);
2177 displayed
[num_displayed
] = strdup(token
);
2182 print_fatal_error("Unexpected token %s seen on line %d.", token
,
2190 * Submit the IPP request...
2195 request
->request
.op
.version
[0] = version
/ 10;
2196 request
->request
.op
.version
[1] = version
% 10;
2197 request
->request
.op
.operation_id
= op
;
2198 request
->request
.op
.request_id
= request_id
;
2200 if (Output
== _CUPS_OUTPUT_PLIST
)
2203 puts("<key>Name</key>");
2204 print_xml_string("string", name
);
2205 puts("<key>Operation</key>");
2206 print_xml_string("string", ippOpString(op
));
2207 puts("<key>RequestAttributes</key>");
2212 for (attrptr
= request
->attrs
, group
= attrptr
->group_tag
;
2214 attrptr
= attrptr
->next
)
2215 print_attr(attrptr
, &group
);
2220 else if (Output
== _CUPS_OUTPUT_TEST
)
2224 printf(" %s:\n", ippOpString(op
));
2226 for (attrptr
= request
->attrs
; attrptr
; attrptr
= attrptr
->next
)
2227 print_attr(attrptr
, NULL
);
2230 printf(" %-68.68s [", name
);
2234 if ((skip_previous
&& !prev_pass
) || skip_test
)
2241 if (Output
== _CUPS_OUTPUT_PLIST
)
2243 puts("<key>Successful</key>");
2245 puts("<key>StatusCode</key>");
2246 print_xml_string("string", "skip");
2247 puts("<key>ResponseAttributes</key>");
2251 else if (Output
== _CUPS_OUTPUT_TEST
)
2258 repeat_interval
= 1;
2267 if (transfer
== _CUPS_TRANSFER_CHUNKED
||
2268 (transfer
== _CUPS_TRANSFER_AUTO
&& filename
[0]))
2271 * Send request using chunking - a 0 length means "chunk".
2279 * Send request using content length...
2282 length
= ippLength(request
);
2284 if (filename
[0] && (reqfile
= cupsFileOpen(filename
, "r")) != NULL
)
2287 * Read the file to get the uncompressed file size...
2290 while ((bytes
= cupsFileRead(reqfile
, buffer
, sizeof(buffer
))) > 0)
2293 cupsFileClose(reqfile
);
2298 * Send the request...
2305 if (status
!= HTTP_ERROR
)
2307 while (!response
&& !Cancel
&& prev_pass
)
2309 status
= cupsSendRequest(http
, request
, resource
, length
);
2311 if (!Cancel
&& status
== HTTP_CONTINUE
&&
2312 request
->state
== IPP_DATA
&& filename
[0])
2314 if ((reqfile
= cupsFileOpen(filename
, "r")) != NULL
)
2317 (bytes
= cupsFileRead(reqfile
, buffer
,
2318 sizeof(buffer
))) > 0)
2319 if ((status
= cupsWriteRequestData(http
, buffer
,
2320 bytes
)) != HTTP_CONTINUE
)
2323 cupsFileClose(reqfile
);
2327 snprintf(buffer
, sizeof(buffer
), "%s: %s", filename
,
2329 _cupsSetError(IPP_INTERNAL_ERROR
, buffer
, 0);
2331 status
= HTTP_ERROR
;
2336 * Get the server's response...
2339 if (!Cancel
&& status
!= HTTP_ERROR
)
2341 response
= cupsGetResponse(http
, resource
);
2342 status
= httpGetStatus(http
);
2345 if (!Cancel
&& status
== HTTP_ERROR
&& http
->error
!= EINVAL
&&
2347 http
->error
!= WSAETIMEDOUT
)
2349 http
->error
!= ETIMEDOUT
)
2352 if (httpReconnect(http
))
2355 else if (status
== HTTP_ERROR
)
2360 else if (status
!= HTTP_OK
)
2368 if (!Cancel
&& status
== HTTP_ERROR
&& http
->error
!= EINVAL
&&
2370 http
->error
!= WSAETIMEDOUT
)
2372 http
->error
!= ETIMEDOUT
)
2375 if (httpReconnect(http
))
2378 else if (status
== HTTP_ERROR
)
2381 httpReconnect(http
);
2385 else if (status
!= HTTP_OK
)
2392 * Check results of request...
2395 cupsArrayClear(errors
);
2397 if (http
->version
!= HTTP_1_1
)
2398 add_stringf(errors
, "Bad HTTP version (%d.%d)", http
->version
/ 100,
2399 http
->version
% 100);
2404 * No response, log error...
2407 add_stringf(errors
, "IPP request failed with status %s (%s)",
2408 ippErrorString(cupsLastError()),
2409 cupsLastErrorString());
2414 * Collect common attribute values...
2417 if ((attrptr
= ippFindAttribute(response
, "job-id",
2418 IPP_TAG_INTEGER
)) != NULL
)
2420 snprintf(temp
, sizeof(temp
), "%d", attrptr
->values
[0].integer
);
2421 set_variable(vars
, "job-id", temp
);
2424 if ((attrptr
= ippFindAttribute(response
, "job-uri",
2425 IPP_TAG_URI
)) != NULL
)
2426 set_variable(vars
, "job-uri", attrptr
->values
[0].string
.text
);
2428 if ((attrptr
= ippFindAttribute(response
, "notify-subscription-id",
2429 IPP_TAG_INTEGER
)) != NULL
)
2431 snprintf(temp
, sizeof(temp
), "%d", attrptr
->values
[0].integer
);
2432 set_variable(vars
, "notify-subscription-id", temp
);
2436 * Check response, validating groups and attributes and logging errors
2440 if (response
->state
!= IPP_DATA
)
2442 "Missing end-of-attributes-tag in response "
2443 "(RFC 2910 section 3.5.1)");
2446 (response
->request
.status
.version
[0] != (version
/ 10) ||
2447 response
->request
.status
.version
[1] != (version
% 10)))
2449 "Bad version %d.%d in response - expected %d.%d "
2450 "(RFC 2911 section 3.1.8).",
2451 response
->request
.status
.version
[0],
2452 response
->request
.status
.version
[1],
2453 version
/ 10, version
% 10);
2455 if (response
->request
.status
.request_id
!= request_id
)
2457 "Bad request ID %d in response - expected %d "
2458 "(RFC 2911 section 3.1.1)",
2459 response
->request
.status
.request_id
, request_id
);
2461 attrptr
= response
->attrs
;
2464 "Missing first attribute \"attributes-charset "
2465 "(charset)\" in group operation-attributes-tag "
2466 "(RFC 2911 section 3.1.4).");
2469 if (!attrptr
->name
||
2470 attrptr
->value_tag
!= IPP_TAG_CHARSET
||
2471 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2472 attrptr
->num_values
!= 1 ||
2473 strcmp(attrptr
->name
, "attributes-charset"))
2475 "Bad first attribute \"%s (%s%s)\" in group %s, "
2476 "expected \"attributes-charset (charset)\" in "
2477 "group operation-attributes-tag (RFC 2911 section "
2479 attrptr
->name
? attrptr
->name
: "(null)",
2480 attrptr
->num_values
> 1 ? "1setOf " : "",
2481 ippTagString(attrptr
->value_tag
),
2482 ippTagString(attrptr
->group_tag
));
2484 attrptr
= attrptr
->next
;
2487 "Missing second attribute \"attributes-natural-"
2488 "language (naturalLanguage)\" in group "
2489 "operation-attributes-tag (RFC 2911 section "
2491 else if (!attrptr
->name
||
2492 attrptr
->value_tag
!= IPP_TAG_LANGUAGE
||
2493 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2494 attrptr
->num_values
!= 1 ||
2495 strcmp(attrptr
->name
, "attributes-natural-language"))
2497 "Bad first attribute \"%s (%s%s)\" in group %s, "
2498 "expected \"attributes-natural-language "
2499 "(naturalLanguage)\" in group "
2500 "operation-attributes-tag (RFC 2911 section "
2502 attrptr
->name
? attrptr
->name
: "(null)",
2503 attrptr
->num_values
> 1 ? "1setOf " : "",
2504 ippTagString(attrptr
->value_tag
),
2505 ippTagString(attrptr
->group_tag
));
2508 if ((attrptr
= ippFindAttribute(response
, "status-message",
2509 IPP_TAG_ZERO
)) != NULL
)
2511 if (attrptr
->value_tag
!= IPP_TAG_TEXT
)
2513 "status-message (text(255)) has wrong value tag "
2514 "%s (RFC 2911 section 3.1.6.2).",
2515 ippTagString(attrptr
->value_tag
));
2516 if (attrptr
->group_tag
!= IPP_TAG_OPERATION
)
2518 "status-message (text(255)) has wrong group tag "
2519 "%s (RFC 2911 section 3.1.6.2).",
2520 ippTagString(attrptr
->group_tag
));
2521 if (attrptr
->num_values
!= 1)
2523 "status-message (text(255)) has %d values "
2524 "(RFC 2911 section 3.1.6.2).",
2525 attrptr
->num_values
);
2526 if (attrptr
->value_tag
== IPP_TAG_TEXT
&&
2527 strlen(attrptr
->values
[0].string
.text
) > 255)
2529 "status-message (text(255)) has bad length %d"
2530 " (RFC 2911 section 3.1.6.2).",
2531 (int)strlen(attrptr
->values
[0].string
.text
));
2534 if ((attrptr
= ippFindAttribute(response
, "detailed-status-message",
2535 IPP_TAG_ZERO
)) != NULL
)
2537 if (attrptr
->value_tag
!= IPP_TAG_TEXT
)
2539 "detailed-status-message (text(MAX)) has wrong "
2540 "value tag %s (RFC 2911 section 3.1.6.3).",
2541 ippTagString(attrptr
->value_tag
));
2542 if (attrptr
->group_tag
!= IPP_TAG_OPERATION
)
2544 "detailed-status-message (text(MAX)) has wrong "
2545 "group tag %s (RFC 2911 section 3.1.6.3).",
2546 ippTagString(attrptr
->group_tag
));
2547 if (attrptr
->num_values
!= 1)
2549 "detailed-status-message (text(MAX)) has %d values"
2550 " (RFC 2911 section 3.1.6.3).",
2551 attrptr
->num_values
);
2552 if (attrptr
->value_tag
== IPP_TAG_TEXT
&&
2553 strlen(attrptr
->values
[0].string
.text
) > 1023)
2555 "detailed-status-message (text(MAX)) has bad "
2556 "length %d (RFC 2911 section 3.1.6.3).",
2557 (int)strlen(attrptr
->values
[0].string
.text
));
2560 a
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
2562 for (attrptr
= response
->attrs
, group
= attrptr
->group_tag
;
2564 attrptr
= attrptr
->next
)
2566 if (attrptr
->group_tag
!= group
)
2568 int out_of_order
= 0; /* Are attribute groups out-of-order? */
2572 switch (attrptr
->group_tag
)
2577 case IPP_TAG_OPERATION
:
2581 case IPP_TAG_UNSUPPORTED_GROUP
:
2582 if (group
!= IPP_TAG_OPERATION
)
2587 case IPP_TAG_PRINTER
:
2588 if (group
!= IPP_TAG_OPERATION
&&
2589 group
!= IPP_TAG_UNSUPPORTED_GROUP
)
2593 case IPP_TAG_SUBSCRIPTION
:
2594 if (group
> attrptr
->group_tag
&&
2595 group
!= IPP_TAG_DOCUMENT
)
2600 if (group
> attrptr
->group_tag
)
2606 add_stringf(errors
, "Attribute groups out of order (%s < %s)",
2607 ippTagString(attrptr
->group_tag
),
2608 ippTagString(group
));
2610 if (attrptr
->group_tag
!= IPP_TAG_ZERO
)
2611 group
= attrptr
->group_tag
;
2614 validate_attr(errors
, attrptr
);
2618 if (cupsArrayFind(a
, attrptr
->name
))
2619 add_stringf(errors
, "Duplicate \"%s\" attribute in %s group",
2620 attrptr
->name
, ippTagString(group
));
2622 cupsArrayAdd(a
, attrptr
->name
);
2629 * Now check the test-defined expected status-code and attribute
2633 for (i
= 0; i
< num_statuses
; i
++)
2635 if (statuses
[i
].if_defined
&&
2636 !get_variable(vars
, statuses
[i
].if_defined
))
2639 if (statuses
[i
].if_not_defined
&&
2640 get_variable(vars
, statuses
[i
].if_not_defined
))
2643 if (response
->request
.status
.status_code
== statuses
[i
].status
)
2645 if (statuses
[i
].repeat_match
&&
2646 repeat_count
< statuses
[i
].repeat_limit
)
2651 else if (statuses
[i
].repeat_no_match
&&
2652 repeat_count
< statuses
[i
].repeat_limit
)
2656 if (i
== num_statuses
&& num_statuses
> 0)
2658 for (i
= 0; i
< num_statuses
; i
++)
2660 if (statuses
[i
].if_defined
&&
2661 !get_variable(vars
, statuses
[i
].if_defined
))
2664 if (statuses
[i
].if_not_defined
&&
2665 get_variable(vars
, statuses
[i
].if_not_defined
))
2668 if (!statuses
[i
].repeat_match
)
2669 add_stringf(errors
, "EXPECTED: STATUS %s (got %s)",
2670 ippErrorString(statuses
[i
].status
),
2671 ippErrorString(cupsLastError()));
2674 if ((attrptr
= ippFindAttribute(response
, "status-message",
2675 IPP_TAG_TEXT
)) != NULL
)
2676 add_stringf(errors
, "status-message=\"%s\"",
2677 attrptr
->values
[0].string
.text
);
2680 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
2682 if (expect
->if_defined
&& !get_variable(vars
, expect
->if_defined
))
2685 if (expect
->if_not_defined
&&
2686 get_variable(vars
, expect
->if_not_defined
))
2689 found
= ippFindAttribute(response
, expect
->name
, IPP_TAG_ZERO
);
2691 if ((found
&& expect
->not_expect
) ||
2692 (!found
&& !(expect
->not_expect
|| expect
->optional
)) ||
2693 (found
&& !expect_matches(expect
, found
->value_tag
)) ||
2694 (found
&& expect
->in_group
&&
2695 found
->group_tag
!= expect
->in_group
))
2697 if (expect
->define_no_match
)
2698 set_variable(vars
, expect
->define_no_match
, "1");
2699 else if (!expect
->define_match
&& !expect
->define_value
)
2701 if (found
&& expect
->not_expect
)
2702 add_stringf(errors
, "NOT EXPECTED: %s", expect
->name
);
2703 else if (!found
&& !(expect
->not_expect
|| expect
->optional
))
2704 add_stringf(errors
, "EXPECTED: %s", expect
->name
);
2707 if (!expect_matches(expect
, found
->value_tag
))
2708 add_stringf(errors
, "EXPECTED: %s OF-TYPE %s (got %s)",
2709 expect
->name
, expect
->of_type
,
2710 ippTagString(found
->value_tag
));
2712 if (expect
->in_group
&& found
->group_tag
!= expect
->in_group
)
2713 add_stringf(errors
, "EXPECTED: %s IN-GROUP %s (got %s).",
2714 expect
->name
, ippTagString(expect
->in_group
),
2715 ippTagString(found
->group_tag
));
2719 if (expect
->repeat_no_match
&&
2720 repeat_count
< expect
->repeat_limit
)
2727 ippAttributeString(found
, buffer
, sizeof(buffer
));
2730 !with_value(NULL
, expect
->with_value
, expect
->with_flags
, found
,
2731 buffer
, sizeof(buffer
)))
2733 if (expect
->define_no_match
)
2734 set_variable(vars
, expect
->define_no_match
, "1");
2735 else if (!expect
->define_match
&& !expect
->define_value
&&
2736 !expect
->repeat_match
&& !expect
->repeat_no_match
)
2738 if (expect
->with_flags
& _CUPS_WITH_REGEX
)
2739 add_stringf(errors
, "EXPECTED: %s %s /%s/",
2741 (expect
->with_flags
& _CUPS_WITH_ALL
) ?
2742 "WITH-ALL-VALUES" : "WITH-VALUE",
2743 expect
->with_value
);
2745 add_stringf(errors
, "EXPECTED: %s %s \"%s\"",
2747 (expect
->with_flags
& _CUPS_WITH_ALL
) ?
2748 "WITH-ALL-VALUES" : "WITH-VALUE",
2749 expect
->with_value
);
2751 with_value(errors
, expect
->with_value
, expect
->with_flags
, found
,
2752 buffer
, sizeof(buffer
));
2755 if (expect
->repeat_no_match
&&
2756 repeat_count
< expect
->repeat_limit
)
2762 if (found
&& expect
->count
> 0 &&
2763 found
->num_values
!= expect
->count
)
2765 if (expect
->define_no_match
)
2766 set_variable(vars
, expect
->define_no_match
, "1");
2767 else if (!expect
->define_match
&& !expect
->define_value
)
2769 add_stringf(errors
, "EXPECTED: %s COUNT %d (got %d)", expect
->name
,
2770 expect
->count
, found
->num_values
);
2773 if (expect
->repeat_no_match
&&
2774 repeat_count
< expect
->repeat_limit
)
2780 if (found
&& expect
->same_count_as
)
2782 attrptr
= ippFindAttribute(response
, expect
->same_count_as
,
2785 if (!attrptr
|| attrptr
->num_values
!= found
->num_values
)
2787 if (expect
->define_no_match
)
2788 set_variable(vars
, expect
->define_no_match
, "1");
2789 else if (!expect
->define_match
&& !expect
->define_value
)
2793 "EXPECTED: %s (%d values) SAME-COUNT-AS %s "
2794 "(not returned)", expect
->name
,
2795 found
->num_values
, expect
->same_count_as
);
2796 else if (attrptr
->num_values
!= found
->num_values
)
2798 "EXPECTED: %s (%d values) SAME-COUNT-AS %s "
2799 "(%d values)", expect
->name
, found
->num_values
,
2800 expect
->same_count_as
, attrptr
->num_values
);
2803 if (expect
->repeat_no_match
&&
2804 repeat_count
< expect
->repeat_limit
)
2811 if (found
&& expect
->define_match
)
2812 set_variable(vars
, expect
->define_match
, "1");
2814 if (found
&& expect
->define_value
)
2815 set_variable(vars
, expect
->define_value
, buffer
);
2817 if (found
&& expect
->repeat_match
&&
2818 repeat_count
< expect
->repeat_limit
)
2824 * If we are going to repeat this test, sleep 1 second so we don't flood
2825 * the printer with requests...
2830 if (Output
== _CUPS_OUTPUT_TEST
)
2832 printf("%04d]\n", repeat_count
);
2836 sleep(repeat_interval
);
2837 repeat_interval
= _cupsNextDelay(repeat_interval
, &repeat_prev
);
2839 if (Output
== _CUPS_OUTPUT_TEST
)
2841 printf(" %-68.68s [", name
);
2846 while (repeat_test
);
2852 if (cupsArrayCount(errors
) > 0)
2853 prev_pass
= pass
= 0;
2860 if (Output
== _CUPS_OUTPUT_PLIST
)
2862 puts("<key>Successful</key>");
2863 puts(prev_pass
? "<true />" : "<false />");
2864 puts("<key>StatusCode</key>");
2865 print_xml_string("string", ippErrorString(cupsLastError()));
2866 puts("<key>ResponseAttributes</key>");
2869 for (attrptr
= response
? response
->attrs
: NULL
,
2870 group
= attrptr
? attrptr
->group_tag
: IPP_TAG_ZERO
;
2872 attrptr
= attrptr
->next
)
2873 print_attr(attrptr
, &group
);
2877 else if (Output
== _CUPS_OUTPUT_TEST
)
2879 puts(prev_pass
? "PASS]" : "FAIL]");
2881 if (!prev_pass
|| (Verbosity
&& response
))
2883 printf(" RECEIVED: %lu bytes in response\n",
2884 (unsigned long)ippLength(response
));
2885 printf(" status-code = %s (%s)\n", ippErrorString(cupsLastError()),
2886 cupsLastErrorString());
2890 for (attrptr
= response
->attrs
;
2892 attrptr
= attrptr
->next
)
2893 print_attr(attrptr
, NULL
);
2897 else if (!prev_pass
)
2898 fprintf(stderr
, "%s\n", cupsLastErrorString());
2900 if (prev_pass
&& Output
>= _CUPS_OUTPUT_LIST
&& !Verbosity
&&
2903 size_t width
; /* Length of value */
2905 for (i
= 0; i
< num_displayed
; i
++)
2907 widths
[i
] = strlen(displayed
[i
]);
2909 for (attrptr
= ippFindAttribute(response
, displayed
[i
], IPP_TAG_ZERO
);
2911 attrptr
= ippFindNextAttribute(response
, displayed
[i
],
2914 width
= ippAttributeString(attrptr
, NULL
, 0);
2915 if (width
> widths
[i
])
2920 if (Output
== _CUPS_OUTPUT_CSV
)
2921 print_csv(NULL
, num_displayed
, displayed
, widths
);
2923 print_line(NULL
, num_displayed
, displayed
, widths
);
2925 attrptr
= response
->attrs
;
2929 while (attrptr
&& attrptr
->group_tag
<= IPP_TAG_OPERATION
)
2930 attrptr
= attrptr
->next
;
2934 if (Output
== _CUPS_OUTPUT_CSV
)
2935 print_csv(attrptr
, num_displayed
, displayed
, widths
);
2937 print_line(attrptr
, num_displayed
, displayed
, widths
);
2939 while (attrptr
&& attrptr
->group_tag
> IPP_TAG_OPERATION
)
2940 attrptr
= attrptr
->next
;
2944 else if (!prev_pass
)
2946 if (Output
== _CUPS_OUTPUT_PLIST
)
2948 puts("<key>Errors</key>");
2951 for (error
= (char *)cupsArrayFirst(errors
);
2953 error
= (char *)cupsArrayNext(errors
))
2954 print_xml_string("string", error
);
2960 for (error
= (char *)cupsArrayFirst(errors
);
2962 error
= (char *)cupsArrayNext(errors
))
2963 printf(" %s\n", error
);
2967 if (num_displayed
> 0 && !Verbosity
&& response
&&
2968 (Output
== _CUPS_OUTPUT_TEST
|| Output
== _CUPS_OUTPUT_PLIST
))
2970 for (attrptr
= response
->attrs
;
2972 attrptr
= attrptr
->next
)
2976 for (i
= 0; i
< num_displayed
; i
++)
2978 if (!strcmp(displayed
[i
], attrptr
->name
))
2980 print_attr(attrptr
, NULL
);
2990 if (Output
== _CUPS_OUTPUT_PLIST
)
2995 ippDelete(response
);
2998 for (i
= 0; i
< num_statuses
; i
++)
3000 if (statuses
[i
].if_defined
)
3001 free(statuses
[i
].if_defined
);
3002 if (statuses
[i
].if_not_defined
)
3003 free(statuses
[i
].if_not_defined
);
3007 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
3010 if (expect
->of_type
)
3011 free(expect
->of_type
);
3012 if (expect
->same_count_as
)
3013 free(expect
->same_count_as
);
3014 if (expect
->if_defined
)
3015 free(expect
->if_defined
);
3016 if (expect
->if_not_defined
)
3017 free(expect
->if_not_defined
);
3018 if (expect
->with_value
)
3019 free(expect
->with_value
);
3020 if (expect
->define_match
)
3021 free(expect
->define_match
);
3022 if (expect
->define_no_match
)
3023 free(expect
->define_no_match
);
3024 if (expect
->define_value
)
3025 free(expect
->define_value
);
3029 for (i
= 0; i
< num_displayed
; i
++)
3033 if (!ignore_errors
&& !prev_pass
)
3039 cupsArrayDelete(errors
);
3046 ippDelete(response
);
3048 for (i
= 0; i
< num_statuses
; i
++)
3050 if (statuses
[i
].if_defined
)
3051 free(statuses
[i
].if_defined
);
3052 if (statuses
[i
].if_not_defined
)
3053 free(statuses
[i
].if_not_defined
);
3056 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
3059 if (expect
->of_type
)
3060 free(expect
->of_type
);
3061 if (expect
->same_count_as
)
3062 free(expect
->same_count_as
);
3063 if (expect
->if_defined
)
3064 free(expect
->if_defined
);
3065 if (expect
->if_not_defined
)
3066 free(expect
->if_not_defined
);
3067 if (expect
->with_value
)
3068 free(expect
->with_value
);
3069 if (expect
->define_match
)
3070 free(expect
->define_match
);
3071 if (expect
->define_no_match
)
3072 free(expect
->define_no_match
);
3073 if (expect
->define_value
)
3074 free(expect
->define_value
);
3077 for (i
= 0; i
< num_displayed
; i
++)
3085 * 'expand_variables()' - Expand variables in a string.
3089 expand_variables(_cups_vars_t
*vars
, /* I - Variables */
3090 char *dst
, /* I - Destination string buffer */
3091 const char *src
, /* I - Source string */
3092 size_t dstsize
) /* I - Size of destination buffer */
3094 char *dstptr
, /* Pointer into destination */
3095 *dstend
, /* End of destination */
3096 temp
[256], /* Temporary string */
3097 *tempptr
; /* Pointer into temporary string */
3098 const char *value
; /* Value to substitute */
3102 dstend
= dst
+ dstsize
- 1;
3104 while (*src
&& dstptr
< dstend
)
3109 * Substitute a string/number...
3112 if (!strncmp(src
, "$$", 2))
3117 else if (!strncmp(src
, "$ENV[", 5))
3119 strlcpy(temp
, src
+ 5, sizeof(temp
));
3121 for (tempptr
= temp
; *tempptr
; tempptr
++)
3122 if (*tempptr
== ']')
3128 value
= getenv(temp
);
3129 src
+= tempptr
- temp
+ 5;
3133 strlcpy(temp
, src
+ 1, sizeof(temp
));
3135 for (tempptr
= temp
; *tempptr
; tempptr
++)
3136 if (!isalnum(*tempptr
& 255) && *tempptr
!= '-' && *tempptr
!= '_')
3142 if (!strcmp(temp
, "uri"))
3144 else if (!strcmp(temp
, "filename"))
3145 value
= vars
->filename
;
3146 else if (!strcmp(temp
, "scheme") || !strcmp(temp
, "method"))
3147 value
= vars
->scheme
;
3148 else if (!strcmp(temp
, "username"))
3149 value
= vars
->userpass
;
3150 else if (!strcmp(temp
, "hostname"))
3151 value
= vars
->hostname
;
3152 else if (!strcmp(temp
, "port"))
3154 snprintf(temp
, sizeof(temp
), "%d", vars
->port
);
3157 else if (!strcmp(temp
, "resource"))
3158 value
= vars
->resource
;
3159 else if (!strcmp(temp
, "user"))
3162 value
= get_variable(vars
, temp
);
3164 src
+= tempptr
- temp
+ 1;
3174 strlcpy(dstptr
, value
, dstend
- dstptr
+ 1);
3175 dstptr
+= strlen(dstptr
);
3187 * 'expect_matches()' - Return true if the tag matches the specification.
3190 static int /* O - 1 if matches, 0 otherwise */
3192 _cups_expect_t
*expect
, /* I - Expected attribute */
3193 ipp_tag_t value_tag
) /* I - Value tag for attribute */
3195 int match
; /* Match? */
3196 char *of_type
, /* Type name to match */
3197 *next
, /* Next name to match */
3198 sep
; /* Separator character */
3202 * If we don't expect a particular type, return immediately...
3205 if (!expect
->of_type
)
3209 * Parse the "of_type" value since the string can contain multiple attribute
3210 * types separated by "," or "|"...
3213 for (of_type
= expect
->of_type
, match
= 0; !match
&& *of_type
; of_type
= next
)
3216 * Find the next separator, and set it (temporarily) to nul if present.
3219 for (next
= of_type
; *next
&& *next
!= '|' && *next
!= ','; next
++);
3221 if ((sep
= *next
) != '\0')
3225 * Support some meta-types to make it easier to write the test file.
3228 if (!strcmp(of_type
, "text"))
3229 match
= value_tag
== IPP_TAG_TEXTLANG
|| value_tag
== IPP_TAG_TEXT
;
3230 else if (!strcmp(of_type
, "name"))
3231 match
= value_tag
== IPP_TAG_NAMELANG
|| value_tag
== IPP_TAG_NAME
;
3232 else if (!strcmp(of_type
, "collection"))
3233 match
= value_tag
== IPP_TAG_BEGIN_COLLECTION
;
3235 match
= value_tag
== ippTagValue(of_type
);
3238 * Restore the separator if we have one...
3250 * 'get_collection()' - Get a collection value from the current test file.
3253 static ipp_t
* /* O - Collection value */
3254 get_collection(_cups_vars_t
*vars
, /* I - Variables */
3255 FILE *fp
, /* I - File to read from */
3256 int *linenum
) /* IO - Line number */
3258 char token
[1024], /* Token from file */
3259 temp
[1024], /* Temporary string */
3260 attr
[128]; /* Attribute name */
3261 ipp_tag_t value
; /* Current value type */
3262 ipp_t
*col
= ippNew(); /* Collection value */
3263 ipp_attribute_t
*lastcol
= NULL
; /* Last collection attribute */
3266 while (get_token(fp
, token
, sizeof(token
), linenum
) != NULL
)
3268 if (!strcmp(token
, "}"))
3270 else if (!strcmp(token
, "{") && lastcol
)
3273 * Another collection value
3276 ipp_t
*subcol
= get_collection(vars
, fp
, linenum
);
3277 /* Collection value */
3281 ipp_attribute_t
*tempcol
; /* Pointer to new buffer */
3285 * Reallocate memory...
3288 if ((tempcol
= realloc(lastcol
, sizeof(ipp_attribute_t
) +
3289 (lastcol
->num_values
+ 1) *
3290 sizeof(_ipp_value_t
))) == NULL
)
3292 print_fatal_error("Unable to allocate memory on line %d.", *linenum
);
3296 if (tempcol
!= lastcol
)
3299 * Reset pointers in the list...
3303 col
->prev
->next
= tempcol
;
3305 col
->attrs
= tempcol
;
3307 lastcol
= col
->current
= col
->last
= tempcol
;
3310 lastcol
->values
[lastcol
->num_values
].collection
= subcol
;
3311 lastcol
->num_values
++;
3316 else if (!_cups_strcasecmp(token
, "MEMBER"))
3324 if (!get_token(fp
, token
, sizeof(token
), linenum
))
3326 print_fatal_error("Missing MEMBER value tag on line %d.", *linenum
);
3330 if ((value
= ippTagValue(token
)) == IPP_TAG_ZERO
)
3332 print_fatal_error("Bad MEMBER value tag \"%s\" on line %d.", token
,
3337 if (!get_token(fp
, attr
, sizeof(attr
), linenum
))
3339 print_fatal_error("Missing MEMBER name on line %d.", *linenum
);
3343 if (!get_token(fp
, temp
, sizeof(temp
), linenum
))
3345 print_fatal_error("Missing MEMBER value on line %d.", *linenum
);
3349 expand_variables(vars
, token
, temp
, sizeof(token
));
3353 case IPP_TAG_BOOLEAN
:
3354 if (!_cups_strcasecmp(token
, "true"))
3355 ippAddBoolean(col
, IPP_TAG_ZERO
, attr
, 1);
3357 ippAddBoolean(col
, IPP_TAG_ZERO
, attr
, atoi(token
));
3360 case IPP_TAG_INTEGER
:
3362 ippAddInteger(col
, IPP_TAG_ZERO
, value
, attr
, atoi(token
));
3365 case IPP_TAG_RESOLUTION
:
3367 int xres
, /* X resolution */
3368 yres
; /* Y resolution */
3369 char units
[6]; /* Units */
3371 if (sscanf(token
, "%dx%d%5s", &xres
, &yres
, units
) != 3 ||
3372 (_cups_strcasecmp(units
, "dpi") &&
3373 _cups_strcasecmp(units
, "dpc") &&
3374 _cups_strcasecmp(units
, "dpcm") &&
3375 _cups_strcasecmp(units
, "other")))
3377 print_fatal_error("Bad resolution value \"%s\" on line %d.",
3382 if (!_cups_strcasecmp(units
, "dpi"))
3383 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, xres
, yres
,
3385 else if (!_cups_strcasecmp(units
, "dpc") ||
3386 !_cups_strcasecmp(units
, "dpcm"))
3387 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, xres
, yres
,
3390 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, xres
, yres
,
3395 case IPP_TAG_RANGE
:
3397 int lowers
[4], /* Lower value */
3398 uppers
[4], /* Upper values */
3399 num_vals
; /* Number of values */
3402 num_vals
= sscanf(token
, "%d-%d,%d-%d,%d-%d,%d-%d",
3403 lowers
+ 0, uppers
+ 0,
3404 lowers
+ 1, uppers
+ 1,
3405 lowers
+ 2, uppers
+ 2,
3406 lowers
+ 3, uppers
+ 3);
3408 if ((num_vals
& 1) || num_vals
== 0)
3410 print_fatal_error("Bad rangeOfInteger value \"%s\" on line %d.",
3415 ippAddRanges(col
, IPP_TAG_ZERO
, attr
, num_vals
/ 2, lowers
,
3420 case IPP_TAG_BEGIN_COLLECTION
:
3421 if (!strcmp(token
, "{"))
3423 ipp_t
*subcol
= get_collection(vars
, fp
, linenum
);
3424 /* Collection value */
3428 lastcol
= ippAddCollection(col
, IPP_TAG_ZERO
, attr
, subcol
);
3436 print_fatal_error("Bad collection value on line %d.", *linenum
);
3442 if (!strchr(token
, ','))
3443 ippAddString(col
, IPP_TAG_ZERO
, value
, attr
, NULL
, token
);
3447 * Multiple string values...
3450 int num_values
; /* Number of values */
3451 char *values
[100], /* Values */
3452 *ptr
; /* Pointer to next value */
3458 for (ptr
= strchr(token
, ','); ptr
; ptr
= strchr(ptr
, ','))
3461 values
[num_values
] = ptr
;
3465 ippAddStrings(col
, IPP_TAG_ZERO
, value
, attr
, num_values
,
3466 NULL
, (const char **)values
);
3476 * If we get here there was a parse error; free memory and return.
3488 * 'get_filename()' - Get a filename based on the current test file.
3491 static char * /* O - Filename */
3492 get_filename(const char *testfile
, /* I - Current test file */
3493 char *dst
, /* I - Destination filename */
3494 const char *src
, /* I - Source filename */
3495 size_t dstsize
) /* I - Size of destination buffer */
3497 char *dstptr
; /* Pointer into destination */
3498 _cups_globals_t
*cg
= _cupsGlobals();
3502 if (*src
== '<' && src
[strlen(src
) - 1] == '>')
3505 * Map <filename> to CUPS_DATADIR/ipptool/filename...
3508 snprintf(dst
, dstsize
, "%s/ipptool/%s", cg
->cups_datadir
, src
+ 1);
3509 dstptr
= dst
+ strlen(dst
) - 1;
3513 else if (*src
== '/' || !strchr(testfile
, '/')
3515 || (isalpha(*src
& 255) && src
[1] == ':')
3520 * Use the path as-is...
3523 strlcpy(dst
, src
, dstsize
);
3528 * Make path relative to testfile...
3531 strlcpy(dst
, testfile
, dstsize
);
3532 if ((dstptr
= strrchr(dst
, '/')) != NULL
)
3535 dstptr
= dst
; /* Should never happen */
3537 strlcpy(dstptr
, src
, dstsize
- (dstptr
- dst
));
3545 * 'get_token()' - Get a token from a file.
3548 static char * /* O - Token from file or NULL on EOF */
3549 get_token(FILE *fp
, /* I - File to read from */
3550 char *buf
, /* I - Buffer to read into */
3551 int buflen
, /* I - Length of buffer */
3552 int *linenum
) /* IO - Current line number */
3554 int ch
, /* Character from file */
3555 quote
; /* Quoting character */
3556 char *bufptr
, /* Pointer into buffer */
3557 *bufend
; /* End of buffer */
3563 * Skip whitespace...
3566 while (isspace(ch
= getc(fp
)))
3578 else if (ch
== '\'' || ch
== '\"')
3581 * Quoted text or regular expression...
3586 bufend
= buf
+ buflen
- 1;
3588 while ((ch
= getc(fp
)) != EOF
)
3593 * Escape next character...
3596 if (bufptr
< bufend
)
3599 if ((ch
= getc(fp
)) != EOF
&& bufptr
< bufend
)
3602 else if (ch
== quote
)
3604 else if (bufptr
< bufend
)
3618 while ((ch
= getc(fp
)) != EOF
)
3627 * Whitespace delimited text...
3633 bufend
= buf
+ buflen
- 1;
3635 while ((ch
= getc(fp
)) != EOF
)
3636 if (isspace(ch
) || ch
== '#')
3638 else if (bufptr
< bufend
)
3643 else if (ch
== '\n')
3655 * 'get_variable()' - Get the value of a variable.
3658 static char * /* O - Value or NULL */
3659 get_variable(_cups_vars_t
*vars
, /* I - Variables */
3660 const char *name
) /* I - Variable name */
3662 _cups_var_t key
, /* Search key */
3663 *match
; /* Matching variable, if any */
3666 key
.name
= (char *)name
;
3667 match
= cupsArrayFind(vars
->vars
, &key
);
3669 return (match
? match
->value
: NULL
);
3674 * 'iso_date()' - Return an ISO 8601 date/time string for the given IPP dateTime
3678 static char * /* O - ISO 8601 date/time string */
3679 iso_date(ipp_uchar_t
*date
) /* I - IPP (RFC 1903) date/time value */
3681 time_t utctime
; /* UTC time since 1970 */
3682 struct tm
*utcdate
; /* UTC date/time */
3683 static char buffer
[255]; /* String buffer */
3686 utctime
= ippDateToTime(date
);
3687 utcdate
= gmtime(&utctime
);
3689 snprintf(buffer
, sizeof(buffer
), "%04d-%02d-%02dT%02d:%02d:%02dZ",
3690 utcdate
->tm_year
+ 1900, utcdate
->tm_mon
+ 1, utcdate
->tm_mday
,
3691 utcdate
->tm_hour
, utcdate
->tm_min
, utcdate
->tm_sec
);
3698 * 'password_cb()' - Password callback for authenticated tests.
3701 static const char * /* O - Password */
3702 password_cb(const char *prompt
) /* I - Prompt (unused) */
3711 * 'print_attr()' - Print an attribute on the screen.
3715 print_attr(ipp_attribute_t
*attr
, /* I - Attribute to print */
3716 ipp_tag_t
*group
) /* IO - Current group */
3718 int i
; /* Looping var */
3719 ipp_attribute_t
*colattr
; /* Collection attribute */
3722 if (Output
== _CUPS_OUTPUT_PLIST
)
3724 if (!attr
->name
|| (group
&& *group
!= attr
->group_tag
))
3730 *group
= attr
->group_tag
;
3736 print_xml_string("key", attr
->name
);
3737 if (attr
->num_values
> 1)
3740 switch (attr
->value_tag
)
3742 case IPP_TAG_INTEGER
:
3744 for (i
= 0; i
< attr
->num_values
; i
++)
3745 if (Output
== _CUPS_OUTPUT_PLIST
)
3746 printf("<integer>%d</integer>\n", attr
->values
[i
].integer
);
3748 printf("%d ", attr
->values
[i
].integer
);
3751 case IPP_TAG_BOOLEAN
:
3752 for (i
= 0; i
< attr
->num_values
; i
++)
3753 if (Output
== _CUPS_OUTPUT_PLIST
)
3754 puts(attr
->values
[i
].boolean
? "<true />" : "<false />");
3755 else if (attr
->values
[i
].boolean
)
3756 fputs("true ", stdout
);
3758 fputs("false ", stdout
);
3761 case IPP_TAG_RANGE
:
3762 for (i
= 0; i
< attr
->num_values
; i
++)
3763 if (Output
== _CUPS_OUTPUT_PLIST
)
3764 printf("<dict><key>lower</key><integer>%d</integer>"
3765 "<key>upper</key><integer>%d</integer></dict>\n",
3766 attr
->values
[i
].range
.lower
, attr
->values
[i
].range
.upper
);
3768 printf("%d-%d ", attr
->values
[i
].range
.lower
,
3769 attr
->values
[i
].range
.upper
);
3772 case IPP_TAG_RESOLUTION
:
3773 for (i
= 0; i
< attr
->num_values
; i
++)
3774 if (Output
== _CUPS_OUTPUT_PLIST
)
3775 printf("<dict><key>xres</key><integer>%d</integer>"
3776 "<key>yres</key><integer>%d</integer>"
3777 "<key>units</key><string>%s</string></dict>\n",
3778 attr
->values
[i
].resolution
.xres
,
3779 attr
->values
[i
].resolution
.yres
,
3780 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3783 printf("%dx%d%s ", attr
->values
[i
].resolution
.xres
,
3784 attr
->values
[i
].resolution
.yres
,
3785 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3790 for (i
= 0; i
< attr
->num_values
; i
++)
3791 if (Output
== _CUPS_OUTPUT_PLIST
)
3792 printf("<date>%s</date>\n", iso_date(attr
->values
[i
].date
));
3794 printf("%s ", iso_date(attr
->values
[i
].date
));
3797 case IPP_TAG_STRING
:
3798 for (i
= 0; i
< attr
->num_values
; i
++)
3800 if (Output
== _CUPS_OUTPUT_PLIST
)
3802 char buffer
[IPP_MAX_LENGTH
* 5 / 4 + 1];
3805 printf("<data>%s</data>\n",
3806 httpEncode64_2(buffer
, sizeof(buffer
),
3807 attr
->values
[i
].unknown
.data
,
3808 attr
->values
[i
].unknown
.length
));
3812 char *ptr
, /* Pointer into data */
3813 *end
; /* End of data */
3816 for (ptr
= attr
->values
[i
].unknown
.data
,
3817 end
= ptr
+ attr
->values
[i
].unknown
.length
;
3821 if (*ptr
== '\\' || *ptr
== '\"')
3822 printf("\\%c", *ptr
);
3823 else if (!isprint(*ptr
& 255))
3824 printf("\\%03o", *ptr
& 255);
3835 case IPP_TAG_KEYWORD
:
3836 case IPP_TAG_CHARSET
:
3838 case IPP_TAG_MIMETYPE
:
3839 case IPP_TAG_LANGUAGE
:
3840 for (i
= 0; i
< attr
->num_values
; i
++)
3841 if (Output
== _CUPS_OUTPUT_PLIST
)
3842 print_xml_string("string", attr
->values
[i
].string
.text
);
3844 printf("\"%s\" ", attr
->values
[i
].string
.text
);
3847 case IPP_TAG_TEXTLANG
:
3848 case IPP_TAG_NAMELANG
:
3849 for (i
= 0; i
< attr
->num_values
; i
++)
3850 if (Output
== _CUPS_OUTPUT_PLIST
)
3852 fputs("<dict><key>language</key><string>", stdout
);
3853 print_xml_string(NULL
, attr
->values
[i
].string
.language
);
3854 fputs("</string><key>string</key><string>", stdout
);
3855 print_xml_string(NULL
, attr
->values
[i
].string
.text
);
3856 puts("</string></dict>");
3859 printf("\"%s\"[%s] ", attr
->values
[i
].string
.text
,
3860 attr
->values
[i
].string
.language
);
3863 case IPP_TAG_BEGIN_COLLECTION
:
3864 for (i
= 0; i
< attr
->num_values
; i
++)
3866 if (Output
== _CUPS_OUTPUT_PLIST
)
3869 for (colattr
= attr
->values
[i
].collection
->attrs
;
3871 colattr
= colattr
->next
)
3872 print_attr(colattr
, NULL
);
3880 print_col(attr
->values
[i
].collection
);
3886 if (Output
== _CUPS_OUTPUT_PLIST
)
3887 printf("<string><<%s>></string>\n",
3888 ippTagString(attr
->value_tag
));
3890 fputs(ippTagString(attr
->value_tag
), stdout
);
3894 if (attr
->num_values
> 1)
3899 char buffer
[8192]; /* Value buffer */
3901 if (Output
== _CUPS_OUTPUT_TEST
)
3905 puts(" -- separator --");
3909 printf(" %s (%s%s) = ", attr
->name
,
3910 attr
->num_values
> 1 ? "1setOf " : "",
3911 ippTagString(attr
->value_tag
));
3914 ippAttributeString(attr
, buffer
, sizeof(buffer
));
3921 * 'print_col()' - Print a collection attribute on the screen.
3925 print_col(ipp_t
*col
) /* I - Collection attribute to print */
3927 int i
; /* Looping var */
3928 ipp_attribute_t
*attr
; /* Current attribute in collection */
3931 fputs("{ ", stdout
);
3932 for (attr
= col
->attrs
; attr
; attr
= attr
->next
)
3934 printf("%s (%s%s) = ", attr
->name
, attr
->num_values
> 1 ? "1setOf " : "",
3935 ippTagString(attr
->value_tag
));
3937 switch (attr
->value_tag
)
3939 case IPP_TAG_INTEGER
:
3941 for (i
= 0; i
< attr
->num_values
; i
++)
3942 printf("%d ", attr
->values
[i
].integer
);
3945 case IPP_TAG_BOOLEAN
:
3946 for (i
= 0; i
< attr
->num_values
; i
++)
3947 if (attr
->values
[i
].boolean
)
3953 case IPP_TAG_NOVALUE
:
3957 case IPP_TAG_RANGE
:
3958 for (i
= 0; i
< attr
->num_values
; i
++)
3959 printf("%d-%d ", attr
->values
[i
].range
.lower
,
3960 attr
->values
[i
].range
.upper
);
3963 case IPP_TAG_RESOLUTION
:
3964 for (i
= 0; i
< attr
->num_values
; i
++)
3965 printf("%dx%d%s ", attr
->values
[i
].resolution
.xres
,
3966 attr
->values
[i
].resolution
.yres
,
3967 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3971 case IPP_TAG_STRING
:
3974 case IPP_TAG_KEYWORD
:
3975 case IPP_TAG_CHARSET
:
3977 case IPP_TAG_MIMETYPE
:
3978 case IPP_TAG_LANGUAGE
:
3979 for (i
= 0; i
< attr
->num_values
; i
++)
3980 printf("\"%s\" ", attr
->values
[i
].string
.text
);
3983 case IPP_TAG_TEXTLANG
:
3984 case IPP_TAG_NAMELANG
:
3985 for (i
= 0; i
< attr
->num_values
; i
++)
3986 printf("\"%s\"[%s] ", attr
->values
[i
].string
.text
,
3987 attr
->values
[i
].string
.language
);
3990 case IPP_TAG_BEGIN_COLLECTION
:
3991 for (i
= 0; i
< attr
->num_values
; i
++)
3993 print_col(attr
->values
[i
].collection
);
3999 break; /* anti-compiler-warning-code */
4008 * 'print_csv()' - Print a line of CSV text.
4013 ipp_attribute_t
*attr
, /* I - First attribute for line */
4014 int num_displayed
, /* I - Number of attributes to display */
4015 char **displayed
, /* I - Attributes to display */
4016 size_t *widths
) /* I - Column widths */
4018 int i
; /* Looping var */
4019 size_t maxlength
; /* Max length of all columns */
4020 char *buffer
, /* String buffer */
4021 *bufptr
; /* Pointer into buffer */
4022 ipp_attribute_t
*current
; /* Current attribute */
4026 * Get the maximum string length we have to show and allocate...
4029 for (i
= 1, maxlength
= widths
[0]; i
< num_displayed
; i
++)
4030 if (widths
[i
] > maxlength
)
4031 maxlength
= widths
[i
];
4035 if ((buffer
= malloc(maxlength
)) == NULL
)
4039 * Loop through the attributes to display...
4044 for (i
= 0; i
< num_displayed
; i
++)
4051 for (current
= attr
; current
; current
= current
->next
)
4055 else if (!strcmp(current
->name
, displayed
[i
]))
4057 ippAttributeString(current
, buffer
, maxlength
);
4062 if (strchr(buffer
, ',') != NULL
|| strchr(buffer
, '\"') != NULL
||
4063 strchr(buffer
, '\\') != NULL
)
4066 for (bufptr
= buffer
; *bufptr
; bufptr
++)
4068 if (*bufptr
== '\\' || *bufptr
== '\"')
4075 fputs(buffer
, stdout
);
4081 for (i
= 0; i
< num_displayed
; i
++)
4086 fputs(displayed
[i
], stdout
);
4096 * 'print_fatal_error()' - Print a fatal error message.
4100 print_fatal_error(const char *s
, /* I - Printf-style format string */
4101 ...) /* I - Additional arguments as needed */
4103 char buffer
[10240]; /* Format buffer */
4104 va_list ap
; /* Pointer to arguments */
4108 * Format the error message...
4112 vsnprintf(buffer
, sizeof(buffer
), s
, ap
);
4119 if (Output
== _CUPS_OUTPUT_PLIST
)
4122 print_xml_trailer(0, buffer
);
4125 _cupsLangPrintf(stderr
, "ipptool: %s", buffer
);
4130 * 'print_line()' - Print a line of formatted or CSV text.
4135 ipp_attribute_t
*attr
, /* I - First attribute for line */
4136 int num_displayed
, /* I - Number of attributes to display */
4137 char **displayed
, /* I - Attributes to display */
4138 size_t *widths
) /* I - Column widths */
4140 int i
; /* Looping var */
4141 size_t maxlength
; /* Max length of all columns */
4142 char *buffer
; /* String buffer */
4143 ipp_attribute_t
*current
; /* Current attribute */
4147 * Get the maximum string length we have to show and allocate...
4150 for (i
= 1, maxlength
= widths
[0]; i
< num_displayed
; i
++)
4151 if (widths
[i
] > maxlength
)
4152 maxlength
= widths
[i
];
4156 if ((buffer
= malloc(maxlength
)) == NULL
)
4160 * Loop through the attributes to display...
4165 for (i
= 0; i
< num_displayed
; i
++)
4172 for (current
= attr
; current
; current
= current
->next
)
4176 else if (!strcmp(current
->name
, displayed
[i
]))
4178 ippAttributeString(current
, buffer
, maxlength
);
4183 printf("%*s", (int)-widths
[i
], buffer
);
4189 for (i
= 0; i
< num_displayed
; i
++)
4194 printf("%*s", (int)-widths
[i
], displayed
[i
]);
4198 for (i
= 0; i
< num_displayed
; i
++)
4203 memset(buffer
, '-', widths
[i
]);
4204 buffer
[widths
[i
]] = '\0';
4205 fputs(buffer
, stdout
);
4215 * 'print_xml_header()' - Print a standard XML plist header.
4219 print_xml_header(void)
4223 puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
4224 puts("<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
4225 "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
4226 puts("<plist version=\"1.0\">");
4228 puts("<key>Transfer</key>");
4229 printf("<string>%s</string>\n",
4230 Transfer
== _CUPS_TRANSFER_AUTO
? "auto" :
4231 Transfer
== _CUPS_TRANSFER_CHUNKED
? "chunked" : "length");
4232 puts("<key>Tests</key>");
4241 * 'print_xml_string()' - Print an XML string with escaping.
4245 print_xml_string(const char *element
, /* I - Element name or NULL */
4246 const char *s
) /* I - String to print */
4249 printf("<%s>", element
);
4254 fputs("&", stdout
);
4256 fputs("<", stdout
);
4258 fputs(">", stdout
);
4259 else if ((*s
& 0xe0) == 0xc0)
4262 * Validate UTF-8 two-byte sequence...
4265 if ((s
[1] & 0xc0) != 0x80)
4276 else if ((*s
& 0xf0) == 0xe0)
4279 * Validate UTF-8 three-byte sequence...
4282 if ((s
[1] & 0xc0) != 0x80 || (s
[2] & 0xc0) != 0x80)
4294 else if ((*s
& 0xf8) == 0xf0)
4297 * Validate UTF-8 four-byte sequence...
4300 if ((s
[1] & 0xc0) != 0x80 || (s
[2] & 0xc0) != 0x80 ||
4301 (s
[3] & 0xc0) != 0x80)
4314 else if ((*s
& 0x80) || (*s
< ' ' && !isspace(*s
& 255)))
4317 * Invalid control character...
4329 printf("</%s>\n", element
);
4334 * 'print_xml_trailer()' - Print the XML trailer with success/fail value.
4338 print_xml_trailer(int success
, /* I - 1 on success, 0 on failure */
4339 const char *message
) /* I - Error message or NULL */
4344 puts("<key>Successful</key>");
4345 puts(success
? "<true />" : "<false />");
4348 puts("<key>ErrorMessage</key>");
4349 print_xml_string("string", message
);
4360 * 'set_variable()' - Set a variable value.
4364 set_variable(_cups_vars_t
*vars
, /* I - Variables */
4365 const char *name
, /* I - Variable name */
4366 const char *value
) /* I - Value string */
4368 _cups_var_t key
, /* Search key */
4369 *var
; /* New variable */
4372 if (!_cups_strcasecmp(name
, "filename"))
4375 free(vars
->filename
);
4377 vars
->filename
= strdup(value
);
4381 key
.name
= (char *)name
;
4382 if ((var
= cupsArrayFind(vars
->vars
, &key
)) != NULL
)
4385 var
->value
= strdup(value
);
4387 else if ((var
= malloc(sizeof(_cups_var_t
))) == NULL
)
4389 print_fatal_error("Unable to allocate memory for variable \"%s\".", name
);
4394 var
->name
= strdup(name
);
4395 var
->value
= strdup(value
);
4397 cupsArrayAdd(vars
->vars
, var
);
4404 * 'sigterm_handler()' - Handle SIGINT and SIGTERM.
4408 sigterm_handler(int sig
) /* I - Signal number (unused) */
4414 signal(SIGINT
, SIG_DFL
);
4415 signal(SIGTERM
, SIG_DFL
);
4421 * 'timeout_cb()' - Handle HTTP timeouts.
4424 static int /* O - 1 to continue, 0 to cancel */
4425 timeout_cb(http_t
*http
, /* I - Connection to server (unused) */
4426 void *user_data
) /* I - User data (unused) */
4431 /* Always cancel on timeout */
4437 * 'usage()' - Show program usage.
4443 _cupsLangPuts(stderr
, _("Usage: ipptool [options] URI filename [ ... "
4445 _cupsLangPuts(stderr
, _("Options:"));
4446 _cupsLangPuts(stderr
, _(" -4 Connect using IPv4."));
4447 _cupsLangPuts(stderr
, _(" -6 Connect using IPv6."));
4448 _cupsLangPuts(stderr
, _(" -C Send requests using "
4449 "chunking (default)."));
4450 _cupsLangPuts(stdout
, _(" -E Test with HTTP Upgrade to "
4452 _cupsLangPuts(stderr
, _(" -I Ignore errors."));
4453 _cupsLangPuts(stderr
, _(" -L Send requests using "
4454 "content-length."));
4455 _cupsLangPuts(stderr
, _(" -S Test with SSL "
4457 _cupsLangPuts(stderr
, _(" -T seconds Set the receive/send "
4458 "timeout in seconds."));
4459 _cupsLangPuts(stderr
, _(" -V version Set default IPP "
4461 _cupsLangPuts(stderr
, _(" -X Produce XML plist instead "
4463 _cupsLangPuts(stderr
, _(" -d name=value Set named variable to "
4465 _cupsLangPuts(stderr
, _(" -f filename Set default request "
4467 _cupsLangPuts(stderr
, _(" -i seconds Repeat the last file with "
4468 "the given time interval."));
4469 _cupsLangPuts(stderr
, _(" -n count Repeat the last file the "
4470 "given number of times."));
4471 _cupsLangPuts(stderr
, _(" -q Run silently."));
4472 _cupsLangPuts(stderr
, _(" -t Produce a test report."));
4473 _cupsLangPuts(stderr
, _(" -v Be verbose."));
4480 * 'validate_attr()' - Determine whether an attribute is valid.
4483 static int /* O - 1 if valid, 0 otherwise */
4484 validate_attr(cups_array_t
*errors
, /* I - Errors array */
4485 ipp_attribute_t
*attr
) /* I - Attribute to validate */
4487 int i
; /* Looping var */
4488 char scheme
[64], /* Scheme from URI */
4489 userpass
[256], /* Username/password from URI */
4490 hostname
[256], /* Hostname from URI */
4491 resource
[1024]; /* Resource from URI */
4492 int port
, /* Port number from URI */
4493 uri_status
, /* URI separation status */
4494 valid
= 1; /* Is the attribute valid? */
4495 const char *ptr
; /* Pointer into string */
4496 ipp_attribute_t
*colattr
; /* Collection attribute */
4497 regex_t re
; /* Regular expression */
4498 ipp_uchar_t
*date
; /* Current date value */
4509 * Validate the attribute name.
4512 for (ptr
= attr
->name
; *ptr
; ptr
++)
4513 if (!isalnum(*ptr
& 255) && *ptr
!= '-' && *ptr
!= '.' && *ptr
!= '_')
4516 if (*ptr
|| ptr
== attr
->name
)
4521 "\"%s\": Bad attribute name - invalid character "
4522 "(RFC 2911 section 4.1.3).", attr
->name
);
4525 if ((ptr
- attr
->name
) > 255)
4530 "\"%s\": Bad attribute name - bad length "
4531 "(RFC 2911 section 4.1.3).", attr
->name
);
4534 switch (attr
->value_tag
)
4536 case IPP_TAG_INTEGER
:
4539 case IPP_TAG_BOOLEAN
:
4540 for (i
= 0; i
< attr
->num_values
; i
++)
4542 if (attr
->values
[i
].boolean
!= 0 &&
4543 attr
->values
[i
].boolean
!= 1)
4548 "\"%s\": Bad boolen value %d "
4549 "(RFC 2911 section 4.1.10).", attr
->name
,
4550 attr
->values
[i
].boolean
);
4556 for (i
= 0; i
< attr
->num_values
; i
++)
4558 if (attr
->values
[i
].integer
< 1)
4563 "\"%s\": Bad enum value %d - out of range "
4564 "(RFC 2911 section 4.1.4).", attr
->name
,
4565 attr
->values
[i
].integer
);
4570 case IPP_TAG_STRING
:
4571 for (i
= 0; i
< attr
->num_values
; i
++)
4573 if (attr
->values
[i
].unknown
.length
> 1023)
4578 "\"%s\": Bad octetString value - bad length %d "
4579 "(RFC 2911 section 4.1.10).", attr
->name
,
4580 attr
->values
[i
].unknown
.length
);
4586 for (i
= 0; i
< attr
->num_values
; i
++)
4588 date
= attr
->values
[i
].date
;
4590 if (date
[2] < 1 || date
[2] > 12)
4595 "\"%s\": Bad dateTime month %u "
4596 "(RFC 2911 section 4.1.13).", attr
->name
, date
[2]);
4599 if (date
[3] < 1 || date
[3] > 31)
4604 "\"%s\": Bad dateTime day %u "
4605 "(RFC 2911 section 4.1.13).", attr
->name
, date
[3]);
4613 "\"%s\": Bad dateTime hours %u "
4614 "(RFC 2911 section 4.1.13).", attr
->name
, date
[4]);
4622 "\"%s\": Bad dateTime minutes %u "
4623 "(RFC 2911 section 4.1.13).", attr
->name
, date
[5]);
4631 "\"%s\": Bad dateTime seconds %u "
4632 "(RFC 2911 section 4.1.13).", attr
->name
, date
[6]);
4640 "\"%s\": Bad dateTime deciseconds %u "
4641 "(RFC 2911 section 4.1.13).", attr
->name
, date
[7]);
4644 if (date
[8] != '-' && date
[8] != '+')
4649 "\"%s\": Bad dateTime UTC sign '%c' "
4650 "(RFC 2911 section 4.1.13).", attr
->name
, date
[8]);
4658 "\"%s\": Bad dateTime UTC hours %u "
4659 "(RFC 2911 section 4.1.13).", attr
->name
, date
[9]);
4667 "\"%s\": Bad dateTime UTC minutes %u "
4668 "(RFC 2911 section 4.1.13).", attr
->name
, date
[10]);
4673 case IPP_TAG_RESOLUTION
:
4674 for (i
= 0; i
< attr
->num_values
; i
++)
4676 if (attr
->values
[i
].resolution
.xres
<= 0)
4681 "\"%s\": Bad resolution value %dx%d%s - cross "
4682 "feed resolution must be positive "
4683 "(RFC 2911 section 4.1.13).", attr
->name
,
4684 attr
->values
[i
].resolution
.xres
,
4685 attr
->values
[i
].resolution
.yres
,
4686 attr
->values
[i
].resolution
.units
==
4687 IPP_RES_PER_INCH
? "dpi" :
4688 attr
->values
[i
].resolution
.units
==
4689 IPP_RES_PER_CM
? "dpcm" : "unknown");
4692 if (attr
->values
[i
].resolution
.yres
<= 0)
4697 "\"%s\": Bad resolution value %dx%d%s - feed "
4698 "resolution must be positive "
4699 "(RFC 2911 section 4.1.13).", attr
->name
,
4700 attr
->values
[i
].resolution
.xres
,
4701 attr
->values
[i
].resolution
.yres
,
4702 attr
->values
[i
].resolution
.units
==
4703 IPP_RES_PER_INCH
? "dpi" :
4704 attr
->values
[i
].resolution
.units
==
4705 IPP_RES_PER_CM
? "dpcm" : "unknown");
4708 if (attr
->values
[i
].resolution
.units
!= IPP_RES_PER_INCH
&&
4709 attr
->values
[i
].resolution
.units
!= IPP_RES_PER_CM
)
4714 "\"%s\": Bad resolution value %dx%d%s - bad "
4715 "units value (RFC 2911 section 4.1.13).",
4716 attr
->name
, attr
->values
[i
].resolution
.xres
,
4717 attr
->values
[i
].resolution
.yres
,
4718 attr
->values
[i
].resolution
.units
==
4719 IPP_RES_PER_INCH
? "dpi" :
4720 attr
->values
[i
].resolution
.units
==
4721 IPP_RES_PER_CM
? "dpcm" : "unknown");
4726 case IPP_TAG_RANGE
:
4727 for (i
= 0; i
< attr
->num_values
; i
++)
4729 if (attr
->values
[i
].range
.lower
> attr
->values
[i
].range
.upper
)
4734 "\"%s\": Bad rangeOfInteger value %d-%d - lower "
4735 "greater than upper (RFC 2911 section 4.1.13).",
4736 attr
->name
, attr
->values
[i
].range
.lower
,
4737 attr
->values
[i
].range
.upper
);
4742 case IPP_TAG_BEGIN_COLLECTION
:
4743 for (i
= 0; i
< attr
->num_values
; i
++)
4745 for (colattr
= attr
->values
[i
].collection
->attrs
;
4747 colattr
= colattr
->next
)
4749 if (!validate_attr(NULL
, colattr
))
4756 if (colattr
&& errors
)
4758 add_stringf(errors
, "\"%s\": Bad collection value.", attr
->name
);
4762 validate_attr(errors
, colattr
);
4763 colattr
= colattr
->next
;
4770 case IPP_TAG_TEXTLANG
:
4771 for (i
= 0; i
< attr
->num_values
; i
++)
4773 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
4775 if ((*ptr
& 0xe0) == 0xc0)
4778 if ((*ptr
& 0xc0) != 0x80)
4781 else if ((*ptr
& 0xf0) == 0xe0)
4784 if ((*ptr
& 0xc0) != 0x80)
4787 if ((*ptr
& 0xc0) != 0x80)
4790 else if ((*ptr
& 0xf8) == 0xf0)
4793 if ((*ptr
& 0xc0) != 0x80)
4796 if ((*ptr
& 0xc0) != 0x80)
4799 if ((*ptr
& 0xc0) != 0x80)
4802 else if (*ptr
& 0x80)
4811 "\"%s\": Bad text value \"%s\" - bad UTF-8 "
4812 "sequence (RFC 2911 section 4.1.1).", attr
->name
,
4813 attr
->values
[i
].string
.text
);
4816 if ((ptr
- attr
->values
[i
].string
.text
) > 1023)
4821 "\"%s\": Bad text value \"%s\" - bad length %d "
4822 "(RFC 2911 section 4.1.1).", attr
->name
,
4823 attr
->values
[i
].string
.text
,
4824 (int)strlen(attr
->values
[i
].string
.text
));
4830 case IPP_TAG_NAMELANG
:
4831 for (i
= 0; i
< attr
->num_values
; i
++)
4833 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
4835 if ((*ptr
& 0xe0) == 0xc0)
4838 if ((*ptr
& 0xc0) != 0x80)
4841 else if ((*ptr
& 0xf0) == 0xe0)
4844 if ((*ptr
& 0xc0) != 0x80)
4847 if ((*ptr
& 0xc0) != 0x80)
4850 else if ((*ptr
& 0xf8) == 0xf0)
4853 if ((*ptr
& 0xc0) != 0x80)
4856 if ((*ptr
& 0xc0) != 0x80)
4859 if ((*ptr
& 0xc0) != 0x80)
4862 else if (*ptr
& 0x80)
4871 "\"%s\": Bad name value \"%s\" - bad UTF-8 "
4872 "sequence (RFC 2911 section 4.1.2).", attr
->name
,
4873 attr
->values
[i
].string
.text
);
4876 if ((ptr
- attr
->values
[i
].string
.text
) > 1023)
4881 "\"%s\": Bad name value \"%s\" - bad length %d "
4882 "(RFC 2911 section 4.1.2).", attr
->name
,
4883 attr
->values
[i
].string
.text
,
4884 (int)strlen(attr
->values
[i
].string
.text
));
4889 case IPP_TAG_KEYWORD
:
4890 for (i
= 0; i
< attr
->num_values
; i
++)
4892 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
4893 if (!isalnum(*ptr
& 255) && *ptr
!= '-' && *ptr
!= '.' &&
4897 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
4902 "\"%s\": Bad keyword value \"%s\" - invalid "
4903 "character (RFC 2911 section 4.1.3).",
4904 attr
->name
, attr
->values
[i
].string
.text
);
4907 if ((ptr
- attr
->values
[i
].string
.text
) > 255)
4912 "\"%s\": Bad keyword value \"%s\" - bad "
4913 "length %d (RFC 2911 section 4.1.3).",
4914 attr
->name
, attr
->values
[i
].string
.text
,
4915 (int)strlen(attr
->values
[i
].string
.text
));
4921 for (i
= 0; i
< attr
->num_values
; i
++)
4923 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
,
4924 attr
->values
[i
].string
.text
,
4925 scheme
, sizeof(scheme
),
4926 userpass
, sizeof(userpass
),
4927 hostname
, sizeof(hostname
),
4928 &port
, resource
, sizeof(resource
));
4930 if (uri_status
< HTTP_URI_OK
)
4935 "\"%s\": Bad URI value \"%s\" - %s "
4936 "(RFC 2911 section 4.1.5).", attr
->name
,
4937 attr
->values
[i
].string
.text
,
4938 URIStatusStrings
[uri_status
-
4939 HTTP_URI_OVERFLOW
]);
4942 if (strlen(attr
->values
[i
].string
.text
) > 1023)
4947 "\"%s\": Bad URI value \"%s\" - bad length %d "
4948 "(RFC 2911 section 4.1.5).", attr
->name
,
4949 attr
->values
[i
].string
.text
,
4950 (int)strlen(attr
->values
[i
].string
.text
));
4955 case IPP_TAG_URISCHEME
:
4956 for (i
= 0; i
< attr
->num_values
; i
++)
4958 ptr
= attr
->values
[i
].string
.text
;
4959 if (islower(*ptr
& 255))
4961 for (ptr
++; *ptr
; ptr
++)
4962 if (!islower(*ptr
& 255) && !isdigit(*ptr
& 255) &&
4963 *ptr
!= '+' && *ptr
!= '-' && *ptr
!= '.')
4967 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
4972 "\"%s\": Bad uriScheme value \"%s\" - bad "
4973 "characters (RFC 2911 section 4.1.6).",
4974 attr
->name
, attr
->values
[i
].string
.text
);
4977 if ((ptr
- attr
->values
[i
].string
.text
) > 63)
4982 "\"%s\": Bad uriScheme value \"%s\" - bad "
4983 "length %d (RFC 2911 section 4.1.6).",
4984 attr
->name
, attr
->values
[i
].string
.text
,
4985 (int)strlen(attr
->values
[i
].string
.text
));
4990 case IPP_TAG_CHARSET
:
4991 for (i
= 0; i
< attr
->num_values
; i
++)
4993 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
4994 if (!isprint(*ptr
& 255) || isupper(*ptr
& 255) ||
4995 isspace(*ptr
& 255))
4998 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
5003 "\"%s\": Bad charset value \"%s\" - bad "
5004 "characters (RFC 2911 section 4.1.7).",
5005 attr
->name
, attr
->values
[i
].string
.text
);
5008 if ((ptr
- attr
->values
[i
].string
.text
) > 40)
5013 "\"%s\": Bad charset value \"%s\" - bad "
5014 "length %d (RFC 2911 section 4.1.7).",
5015 attr
->name
, attr
->values
[i
].string
.text
,
5016 (int)strlen(attr
->values
[i
].string
.text
));
5021 case IPP_TAG_LANGUAGE
:
5023 * The following regular expression is derived from the ABNF for
5024 * language tags in RFC 4646. All I can say is that this is the
5025 * easiest way to check the values...
5028 if ((i
= regcomp(&re
,
5030 "(([a-z]{2,3}(-[a-z][a-z][a-z]){0,3})|[a-z]{4,8})"
5032 "(-[a-z][a-z][a-z][a-z]){0,1}" /* script */
5033 "(-([a-z][a-z]|[0-9][0-9][0-9])){0,1}" /* region */
5034 "(-([a-z]{5,8}|[0-9][0-9][0-9]))*" /* variant */
5035 "(-[a-wy-z](-[a-z0-9]{2,8})+)*" /* extension */
5036 "(-x(-[a-z0-9]{1,8})+)*" /* privateuse */
5038 "x(-[a-z0-9]{1,8})+" /* privateuse */
5040 "[a-z]{1,3}(-[a-z][0-9]{2,8}){1,2}" /* grandfathered */
5042 REG_NOSUB
| REG_EXTENDED
)) != 0)
5044 char temp
[256]; /* Temporary error string */
5046 regerror(i
, &re
, temp
, sizeof(temp
));
5047 print_fatal_error("Unable to compile naturalLanguage regular "
5048 "expression: %s.", temp
);
5052 for (i
= 0; i
< attr
->num_values
; i
++)
5054 if (regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
5059 "\"%s\": Bad naturalLanguage value \"%s\" - bad "
5060 "characters (RFC 2911 section 4.1.8).",
5061 attr
->name
, attr
->values
[i
].string
.text
);
5064 if (strlen(attr
->values
[i
].string
.text
) > 63)
5069 "\"%s\": Bad naturalLanguage value \"%s\" - bad "
5070 "length %d (RFC 2911 section 4.1.8).",
5071 attr
->name
, attr
->values
[i
].string
.text
,
5072 (int)strlen(attr
->values
[i
].string
.text
));
5079 case IPP_TAG_MIMETYPE
:
5081 * The following regular expression is derived from the ABNF for
5082 * language tags in RFC 2045 and 4288. All I can say is that this is
5083 * the easiest way to check the values...
5086 if ((i
= regcomp(&re
,
5088 "[-a-zA-Z0-9!#$&.+^_]{1,127}" /* type-name */
5090 "[-a-zA-Z0-9!#$&.+^_]{1,127}" /* subtype-name */
5091 "(;[-a-zA-Z0-9!#$&.+^_]{1,127}=" /* parameter= */
5092 "([-a-zA-Z0-9!#$&.+^_]{1,127}|\"[^\"]*\"))*"
5095 REG_NOSUB
| REG_EXTENDED
)) != 0)
5097 char temp
[256]; /* Temporary error string */
5099 regerror(i
, &re
, temp
, sizeof(temp
));
5100 print_fatal_error("Unable to compile mimeMediaType regular "
5101 "expression: %s.", temp
);
5105 for (i
= 0; i
< attr
->num_values
; i
++)
5107 if (regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
5112 "\"%s\": Bad mimeMediaType value \"%s\" - bad "
5113 "characters (RFC 2911 section 4.1.9).",
5114 attr
->name
, attr
->values
[i
].string
.text
);
5117 if (strlen(attr
->values
[i
].string
.text
) > 255)
5122 "\"%s\": Bad mimeMediaType value \"%s\" - bad "
5123 "length %d (RFC 2911 section 4.1.9).",
5124 attr
->name
, attr
->values
[i
].string
.text
,
5125 (int)strlen(attr
->values
[i
].string
.text
));
5141 * 'with_value()' - Test a WITH-VALUE predicate.
5144 static int /* O - 1 on match, 0 on non-match */
5145 with_value(cups_array_t
*errors
, /* I - Errors array */
5146 char *value
, /* I - Value string */
5147 int flags
, /* I - Flags for match */
5148 ipp_attribute_t
*attr
, /* I - Attribute to compare */
5149 char *matchbuf
, /* I - Buffer to hold matching value */
5150 size_t matchlen
) /* I - Length of match buffer */
5152 int i
, /* Looping var */
5154 char *valptr
; /* Pointer into value */
5158 match
= (flags
& _CUPS_WITH_ALL
) ? 1 : 0;
5161 * NULL matches everything.
5164 if (!value
|| !*value
)
5168 * Compare the value string to the attribute value.
5171 switch (attr
->value_tag
)
5173 case IPP_TAG_INTEGER
:
5175 for (i
= 0; i
< attr
->num_values
; i
++)
5177 char op
, /* Comparison operator */
5178 *nextptr
; /* Next pointer */
5179 int intvalue
, /* Integer value */
5180 valmatch
= 0; /* Does the current value match? */
5184 while (isspace(*valptr
& 255) || isdigit(*valptr
& 255) ||
5185 *valptr
== '-' || *valptr
== ',' || *valptr
== '<' ||
5186 *valptr
== '=' || *valptr
== '>')
5189 while (*valptr
&& !isdigit(*valptr
& 255) && *valptr
!= '-')
5191 if (*valptr
== '<' || *valptr
== '>' || *valptr
== '=')
5199 intvalue
= strtol(valptr
, &nextptr
, 0);
5200 if (nextptr
== valptr
)
5204 if ((op
== '=' && attr
->values
[i
].integer
== intvalue
) ||
5205 (op
== '<' && attr
->values
[i
].integer
< intvalue
) ||
5206 (op
== '>' && attr
->values
[i
].integer
> intvalue
))
5209 snprintf(matchbuf
, matchlen
, "%d",
5210 attr
->values
[i
].integer
);
5217 if (flags
& _CUPS_WITH_ALL
)
5232 if (!match
&& errors
)
5234 for (i
= 0; i
< attr
->num_values
; i
++)
5235 add_stringf(errors
, "GOT: %s=%d", attr
->name
,
5236 attr
->values
[i
].integer
);
5240 case IPP_TAG_RANGE
:
5241 for (i
= 0; i
< attr
->num_values
; i
++)
5243 char op
, /* Comparison operator */
5244 *nextptr
; /* Next pointer */
5245 int intvalue
, /* Integer value */
5246 valmatch
= 0; /* Does the current value match? */
5250 while (isspace(*valptr
& 255) || isdigit(*valptr
& 255) ||
5251 *valptr
== '-' || *valptr
== ',' || *valptr
== '<' ||
5252 *valptr
== '=' || *valptr
== '>')
5255 while (*valptr
&& !isdigit(*valptr
& 255) && *valptr
!= '-')
5257 if (*valptr
== '<' || *valptr
== '>' || *valptr
== '=')
5265 intvalue
= strtol(valptr
, &nextptr
, 0);
5266 if (nextptr
== valptr
)
5270 if ((op
== '=' && (attr
->values
[i
].range
.lower
== intvalue
||
5271 attr
->values
[i
].range
.upper
== intvalue
)) ||
5272 (op
== '<' && attr
->values
[i
].range
.upper
< intvalue
) ||
5273 (op
== '>' && attr
->values
[i
].range
.upper
> intvalue
))
5276 snprintf(matchbuf
, matchlen
, "%d-%d",
5277 attr
->values
[0].range
.lower
,
5278 attr
->values
[0].range
.upper
);
5285 if (flags
& _CUPS_WITH_ALL
)
5300 if (!match
&& errors
)
5302 for (i
= 0; i
< attr
->num_values
; i
++)
5303 add_stringf(errors
, "GOT: %s=%d-%d", attr
->name
,
5304 attr
->values
[i
].range
.lower
,
5305 attr
->values
[i
].range
.upper
);
5309 case IPP_TAG_BOOLEAN
:
5310 for (i
= 0; i
< attr
->num_values
; i
++)
5312 if (!strcmp(value
, "true") == attr
->values
[i
].boolean
)
5315 strlcpy(matchbuf
, value
, matchlen
);
5317 if (!(flags
& _CUPS_WITH_ALL
))
5323 else if (flags
& _CUPS_WITH_ALL
)
5330 if (!match
&& errors
)
5332 for (i
= 0; i
< attr
->num_values
; i
++)
5333 add_stringf(errors
, "GOT: %s=%s", attr
->name
,
5334 attr
->values
[i
].boolean
? "true" : "false");
5338 case IPP_TAG_NOVALUE
:
5339 case IPP_TAG_UNKNOWN
:
5342 case IPP_TAG_CHARSET
:
5343 case IPP_TAG_KEYWORD
:
5344 case IPP_TAG_LANGUAGE
:
5345 case IPP_TAG_MIMETYPE
:
5347 case IPP_TAG_NAMELANG
:
5349 case IPP_TAG_TEXTLANG
:
5351 case IPP_TAG_URISCHEME
:
5352 if (flags
& _CUPS_WITH_REGEX
)
5355 * Value is an extended, case-sensitive POSIX regular expression...
5358 regex_t re
; /* Regular expression */
5360 if ((i
= regcomp(&re
, value
, REG_EXTENDED
| REG_NOSUB
)) != 0)
5362 char temp
[256]; /* Temporary string */
5364 regerror(i
, &re
, temp
, sizeof(temp
));
5366 print_fatal_error("Unable to compile WITH-VALUE regular expression "
5367 "\"%s\" - %s", value
, temp
);
5372 * See if ALL of the values match the given regular expression.
5375 for (i
= 0; i
< attr
->num_values
; i
++)
5377 if (!regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
5380 strlcpy(matchbuf
, attr
->values
[i
].string
.text
, matchlen
);
5382 if (!(flags
& _CUPS_WITH_ALL
))
5388 else if (flags
& _CUPS_WITH_ALL
)
5400 * Value is a literal string, see if the value(s) match...
5403 for (i
= 0; i
< attr
->num_values
; i
++)
5405 if (!strcmp(value
, attr
->values
[i
].string
.text
))
5408 strlcpy(matchbuf
, attr
->values
[i
].string
.text
, matchlen
);
5410 if (!(flags
& _CUPS_WITH_ALL
))
5416 else if (flags
& _CUPS_WITH_ALL
)
5424 if (!match
&& errors
)
5426 for (i
= 0; i
< attr
->num_values
; i
++)
5427 add_stringf(errors
, "GOT: %s=\"%s\"", attr
->name
,
5428 attr
->values
[i
].string
.text
);