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.",
1706 print_fatal_error("Unsupported ATTR value tag %s on line %d.",
1707 ippTagString(value
), linenum
);
1711 case IPP_TAG_TEXTLANG
:
1712 case IPP_TAG_NAMELANG
:
1715 case IPP_TAG_KEYWORD
:
1717 case IPP_TAG_URISCHEME
:
1718 case IPP_TAG_CHARSET
:
1719 case IPP_TAG_LANGUAGE
:
1720 case IPP_TAG_MIMETYPE
:
1721 if (!strchr(token
, ','))
1722 attrptr
= ippAddString(request
, group
, value
, attr
, NULL
, token
);
1726 * Multiple string values...
1729 int num_values
; /* Number of values */
1730 char *values
[100], /* Values */
1731 *ptr
; /* Pointer to next value */
1737 for (ptr
= strchr(token
, ','); ptr
; ptr
= strchr(ptr
, ','))
1740 values
[num_values
] = ptr
;
1744 attrptr
= ippAddStrings(request
, group
, value
, attr
, num_values
,
1745 NULL
, (const char **)values
);
1752 print_fatal_error("Unable to add attribute on line %d: %s", linenum
,
1753 cupsLastErrorString());
1758 else if (!_cups_strcasecmp(token
, "FILE"))
1764 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1766 print_fatal_error("Missing FILE filename on line %d.", linenum
);
1771 expand_variables(vars
, token
, temp
, sizeof(token
));
1772 get_filename(testfile
, filename
, token
, sizeof(filename
));
1774 else if (!_cups_strcasecmp(token
, "STATUS"))
1780 if (num_statuses
>= (int)(sizeof(statuses
) / sizeof(statuses
[0])))
1782 print_fatal_error("Too many STATUS's on line %d.", linenum
);
1787 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1789 print_fatal_error("Missing STATUS code on line %d.", linenum
);
1794 if ((statuses
[num_statuses
].status
= ippErrorValue(token
))
1795 == (ipp_status_t
)-1 &&
1796 (statuses
[num_statuses
].status
= strtol(token
, NULL
, 0)) == 0)
1798 print_fatal_error("Bad STATUS code \"%s\" on line %d.", token
,
1804 last_status
= statuses
+ num_statuses
;
1807 last_status
->if_defined
= NULL
;
1808 last_status
->if_not_defined
= NULL
;
1809 last_status
->repeat_limit
= 1000;
1810 last_status
->repeat_match
= 0;
1811 last_status
->repeat_no_match
= 0;
1813 else if (!_cups_strcasecmp(token
, "EXPECT"))
1816 * Expected attributes...
1819 if (num_expects
>= (int)(sizeof(expects
) / sizeof(expects
[0])))
1821 print_fatal_error("Too many EXPECT's on line %d.", linenum
);
1826 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1828 print_fatal_error("Missing EXPECT name on line %d.", linenum
);
1833 last_expect
= expects
+ num_expects
;
1836 memset(last_expect
, 0, sizeof(_cups_expect_t
));
1837 last_expect
->repeat_limit
= 1000;
1839 if (token
[0] == '!')
1841 last_expect
->not_expect
= 1;
1842 last_expect
->name
= strdup(token
+ 1);
1844 else if (token
[0] == '?')
1846 last_expect
->optional
= 1;
1847 last_expect
->name
= strdup(token
+ 1);
1850 last_expect
->name
= strdup(token
);
1852 else if (!_cups_strcasecmp(token
, "COUNT"))
1854 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1856 print_fatal_error("Missing COUNT number on line %d.", linenum
);
1861 if ((i
= atoi(token
)) <= 0)
1863 print_fatal_error("Bad COUNT \"%s\" on line %d.", token
, linenum
);
1869 last_expect
->count
= i
;
1872 print_fatal_error("COUNT without a preceding EXPECT on line %d.",
1878 else if (!_cups_strcasecmp(token
, "DEFINE-MATCH"))
1880 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1882 print_fatal_error("Missing DEFINE-MATCH variable on line %d.",
1889 last_expect
->define_match
= strdup(token
);
1892 print_fatal_error("DEFINE-MATCH without a preceding EXPECT on line "
1898 else if (!_cups_strcasecmp(token
, "DEFINE-NO-MATCH"))
1900 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1902 print_fatal_error("Missing DEFINE-NO-MATCH variable on line %d.",
1909 last_expect
->define_no_match
= strdup(token
);
1912 print_fatal_error("DEFINE-NO-MATCH without a preceding EXPECT on "
1913 "line %d.", linenum
);
1918 else if (!_cups_strcasecmp(token
, "DEFINE-VALUE"))
1920 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1922 print_fatal_error("Missing DEFINE-VALUE variable on line %d.",
1929 last_expect
->define_value
= strdup(token
);
1932 print_fatal_error("DEFINE-VALUE without a preceding EXPECT on line "
1938 else if (!_cups_strcasecmp(token
, "OF-TYPE"))
1940 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1942 print_fatal_error("Missing OF-TYPE value tag(s) on line %d.",
1949 last_expect
->of_type
= strdup(token
);
1952 print_fatal_error("OF-TYPE without a preceding EXPECT on line %d.",
1958 else if (!_cups_strcasecmp(token
, "IN-GROUP"))
1960 ipp_tag_t in_group
; /* IN-GROUP value */
1963 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1965 print_fatal_error("Missing IN-GROUP group tag on line %d.", linenum
);
1970 if ((in_group
= ippTagValue(token
)) == (ipp_tag_t
)-1)
1973 else if (last_expect
)
1974 last_expect
->in_group
= in_group
;
1977 print_fatal_error("IN-GROUP without a preceding EXPECT on line %d.",
1983 else if (!_cups_strcasecmp(token
, "REPEAT-LIMIT"))
1985 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1987 print_fatal_error("Missing REPEAT-LIMIT value on line %d.", linenum
);
1991 else if (atoi(token
) <= 0)
1993 print_fatal_error("Bad REPEAT-LIMIT value on line %d.", linenum
);
1999 last_status
->repeat_limit
= atoi(token
);
2000 else if (last_expect
)
2001 last_expect
->repeat_limit
= atoi(token
);
2004 print_fatal_error("REPEAT-LIMIT without a preceding EXPECT or STATUS "
2005 "on line %d.", linenum
);
2010 else if (!_cups_strcasecmp(token
, "REPEAT-MATCH"))
2013 last_status
->repeat_match
= 1;
2014 else if (last_expect
)
2015 last_expect
->repeat_match
= 1;
2018 print_fatal_error("REPEAT-MATCH without a preceding EXPECT or STATUS "
2019 "on line %d.", linenum
);
2024 else if (!_cups_strcasecmp(token
, "REPEAT-NO-MATCH"))
2027 last_status
->repeat_no_match
= 1;
2028 else if (last_expect
)
2029 last_expect
->repeat_no_match
= 1;
2032 print_fatal_error("REPEAT-NO-MATCH without a preceding EXPECT or "
2033 "STATUS on ine %d.", linenum
);
2038 else if (!_cups_strcasecmp(token
, "SAME-COUNT-AS"))
2040 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2042 print_fatal_error("Missing SAME-COUNT-AS name on line %d.", linenum
);
2048 last_expect
->same_count_as
= strdup(token
);
2051 print_fatal_error("SAME-COUNT-AS without a preceding EXPECT on line "
2057 else if (!_cups_strcasecmp(token
, "IF-DEFINED"))
2059 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2061 print_fatal_error("Missing IF-DEFINED name on line %d.", linenum
);
2067 last_expect
->if_defined
= strdup(token
);
2068 else if (last_status
)
2069 last_status
->if_defined
= strdup(token
);
2072 print_fatal_error("IF-DEFINED without a preceding EXPECT or STATUS "
2073 "on line %d.", linenum
);
2078 else if (!_cups_strcasecmp(token
, "IF-NOT-DEFINED"))
2080 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2082 print_fatal_error("Missing IF-NOT-DEFINED name on line %d.", linenum
);
2088 last_expect
->if_not_defined
= strdup(token
);
2089 else if (last_status
)
2090 last_status
->if_not_defined
= strdup(token
);
2093 print_fatal_error("IF-NOT-DEFINED without a preceding EXPECT or STATUS "
2094 "on line %d.", linenum
);
2099 else if (!_cups_strcasecmp(token
, "WITH-ALL-VALUES") ||
2100 !_cups_strcasecmp(token
, "WITH-VALUE"))
2102 if (!_cups_strcasecmp(token
, "WITH-ALL-VALUES") && last_expect
)
2103 last_expect
->with_flags
= _CUPS_WITH_ALL
;
2105 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
2107 print_fatal_error("Missing %s value on line %d.", token
, linenum
);
2115 * Expand any variables in the value and then save it.
2118 expand_variables(vars
, token
, temp
, sizeof(token
));
2120 tokenptr
= token
+ strlen(token
) - 1;
2122 if (token
[0] == '/' && tokenptr
> token
&& *tokenptr
== '/')
2125 * WITH-VALUE is a POSIX extended regular expression.
2128 last_expect
->with_value
= calloc(1, tokenptr
- token
);
2129 last_expect
->with_flags
|= _CUPS_WITH_REGEX
;
2131 if (last_expect
->with_value
)
2132 memcpy(last_expect
->with_value
, token
+ 1, tokenptr
- token
- 1);
2137 * WITH-VALUE is a literal value...
2140 last_expect
->with_value
= strdup(token
);
2141 last_expect
->with_flags
|= _CUPS_WITH_LITERAL
;
2146 print_fatal_error("%s without a preceding EXPECT on line %d.", token
,
2152 else if (!_cups_strcasecmp(token
, "DISPLAY"))
2155 * Display attributes...
2158 if (num_displayed
>= (int)(sizeof(displayed
) / sizeof(displayed
[0])))
2160 print_fatal_error("Too many DISPLAY's on line %d", linenum
);
2165 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2167 print_fatal_error("Missing DISPLAY name on line %d.", linenum
);
2172 displayed
[num_displayed
] = strdup(token
);
2177 print_fatal_error("Unexpected token %s seen on line %d.", token
,
2185 * Submit the IPP request...
2190 request
->request
.op
.version
[0] = version
/ 10;
2191 request
->request
.op
.version
[1] = version
% 10;
2192 request
->request
.op
.operation_id
= op
;
2193 request
->request
.op
.request_id
= request_id
;
2195 if (Output
== _CUPS_OUTPUT_PLIST
)
2198 puts("<key>Name</key>");
2199 print_xml_string("string", name
);
2200 puts("<key>Operation</key>");
2201 print_xml_string("string", ippOpString(op
));
2202 puts("<key>RequestAttributes</key>");
2207 for (attrptr
= request
->attrs
, group
= attrptr
->group_tag
;
2209 attrptr
= attrptr
->next
)
2210 print_attr(attrptr
, &group
);
2215 else if (Output
== _CUPS_OUTPUT_TEST
)
2219 printf(" %s:\n", ippOpString(op
));
2221 for (attrptr
= request
->attrs
; attrptr
; attrptr
= attrptr
->next
)
2222 print_attr(attrptr
, NULL
);
2225 printf(" %-68.68s [", name
);
2229 if ((skip_previous
&& !prev_pass
) || skip_test
)
2236 if (Output
== _CUPS_OUTPUT_PLIST
)
2238 puts("<key>Successful</key>");
2240 puts("<key>StatusCode</key>");
2241 print_xml_string("string", "skip");
2242 puts("<key>ResponseAttributes</key>");
2246 else if (Output
== _CUPS_OUTPUT_TEST
)
2253 repeat_interval
= 1;
2262 if (transfer
== _CUPS_TRANSFER_CHUNKED
||
2263 (transfer
== _CUPS_TRANSFER_AUTO
&& filename
[0]))
2266 * Send request using chunking - a 0 length means "chunk".
2274 * Send request using content length...
2277 length
= ippLength(request
);
2279 if (filename
[0] && (reqfile
= cupsFileOpen(filename
, "r")) != NULL
)
2282 * Read the file to get the uncompressed file size...
2285 while ((bytes
= cupsFileRead(reqfile
, buffer
, sizeof(buffer
))) > 0)
2288 cupsFileClose(reqfile
);
2293 * Send the request...
2300 if (status
!= HTTP_ERROR
)
2302 while (!response
&& !Cancel
&& prev_pass
)
2304 status
= cupsSendRequest(http
, request
, resource
, length
);
2306 if (!Cancel
&& status
== HTTP_CONTINUE
&&
2307 request
->state
== IPP_DATA
&& filename
[0])
2309 if ((reqfile
= cupsFileOpen(filename
, "r")) != NULL
)
2312 (bytes
= cupsFileRead(reqfile
, buffer
,
2313 sizeof(buffer
))) > 0)
2314 if ((status
= cupsWriteRequestData(http
, buffer
,
2315 bytes
)) != HTTP_CONTINUE
)
2318 cupsFileClose(reqfile
);
2322 snprintf(buffer
, sizeof(buffer
), "%s: %s", filename
,
2324 _cupsSetError(IPP_INTERNAL_ERROR
, buffer
, 0);
2326 status
= HTTP_ERROR
;
2331 * Get the server's response...
2334 if (!Cancel
&& status
!= HTTP_ERROR
)
2336 response
= cupsGetResponse(http
, resource
);
2337 status
= httpGetStatus(http
);
2340 if (!Cancel
&& status
== HTTP_ERROR
&& http
->error
!= EINVAL
&&
2342 http
->error
!= WSAETIMEDOUT
)
2344 http
->error
!= ETIMEDOUT
)
2347 if (httpReconnect(http
))
2350 else if (status
== HTTP_ERROR
)
2355 else if (status
!= HTTP_OK
)
2363 if (!Cancel
&& status
== HTTP_ERROR
&& http
->error
!= EINVAL
&&
2365 http
->error
!= WSAETIMEDOUT
)
2367 http
->error
!= ETIMEDOUT
)
2370 if (httpReconnect(http
))
2373 else if (status
== HTTP_ERROR
)
2376 httpReconnect(http
);
2380 else if (status
!= HTTP_OK
)
2387 * Check results of request...
2390 cupsArrayClear(errors
);
2392 if (http
->version
!= HTTP_1_1
)
2393 add_stringf(errors
, "Bad HTTP version (%d.%d)", http
->version
/ 100,
2394 http
->version
% 100);
2399 * No response, log error...
2402 add_stringf(errors
, "IPP request failed with status %s (%s)",
2403 ippErrorString(cupsLastError()),
2404 cupsLastErrorString());
2409 * Collect common attribute values...
2412 if ((attrptr
= ippFindAttribute(response
, "job-id",
2413 IPP_TAG_INTEGER
)) != NULL
)
2415 snprintf(temp
, sizeof(temp
), "%d", attrptr
->values
[0].integer
);
2416 set_variable(vars
, "job-id", temp
);
2419 if ((attrptr
= ippFindAttribute(response
, "job-uri",
2420 IPP_TAG_URI
)) != NULL
)
2421 set_variable(vars
, "job-uri", attrptr
->values
[0].string
.text
);
2423 if ((attrptr
= ippFindAttribute(response
, "notify-subscription-id",
2424 IPP_TAG_INTEGER
)) != NULL
)
2426 snprintf(temp
, sizeof(temp
), "%d", attrptr
->values
[0].integer
);
2427 set_variable(vars
, "notify-subscription-id", temp
);
2431 * Check response, validating groups and attributes and logging errors
2435 if (response
->state
!= IPP_DATA
)
2437 "Missing end-of-attributes-tag in response "
2438 "(RFC 2910 section 3.5.1)");
2441 (response
->request
.status
.version
[0] != (version
/ 10) ||
2442 response
->request
.status
.version
[1] != (version
% 10)))
2444 "Bad version %d.%d in response - expected %d.%d "
2445 "(RFC 2911 section 3.1.8).",
2446 response
->request
.status
.version
[0],
2447 response
->request
.status
.version
[1],
2448 version
/ 10, version
% 10);
2450 if (response
->request
.status
.request_id
!= request_id
)
2452 "Bad request ID %d in response - expected %d "
2453 "(RFC 2911 section 3.1.1)",
2454 response
->request
.status
.request_id
, request_id
);
2456 attrptr
= response
->attrs
;
2459 "Missing first attribute \"attributes-charset "
2460 "(charset)\" in group operation-attributes-tag "
2461 "(RFC 2911 section 3.1.4).");
2464 if (!attrptr
->name
||
2465 attrptr
->value_tag
!= IPP_TAG_CHARSET
||
2466 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2467 attrptr
->num_values
!= 1 ||
2468 strcmp(attrptr
->name
, "attributes-charset"))
2470 "Bad first attribute \"%s (%s%s)\" in group %s, "
2471 "expected \"attributes-charset (charset)\" in "
2472 "group operation-attributes-tag (RFC 2911 section "
2474 attrptr
->name
? attrptr
->name
: "(null)",
2475 attrptr
->num_values
> 1 ? "1setOf " : "",
2476 ippTagString(attrptr
->value_tag
),
2477 ippTagString(attrptr
->group_tag
));
2479 attrptr
= attrptr
->next
;
2482 "Missing second attribute \"attributes-natural-"
2483 "language (naturalLanguage)\" in group "
2484 "operation-attributes-tag (RFC 2911 section "
2486 else if (!attrptr
->name
||
2487 attrptr
->value_tag
!= IPP_TAG_LANGUAGE
||
2488 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2489 attrptr
->num_values
!= 1 ||
2490 strcmp(attrptr
->name
, "attributes-natural-language"))
2492 "Bad first attribute \"%s (%s%s)\" in group %s, "
2493 "expected \"attributes-natural-language "
2494 "(naturalLanguage)\" in group "
2495 "operation-attributes-tag (RFC 2911 section "
2497 attrptr
->name
? attrptr
->name
: "(null)",
2498 attrptr
->num_values
> 1 ? "1setOf " : "",
2499 ippTagString(attrptr
->value_tag
),
2500 ippTagString(attrptr
->group_tag
));
2503 if ((attrptr
= ippFindAttribute(response
, "status-message",
2504 IPP_TAG_ZERO
)) != NULL
)
2506 if (attrptr
->value_tag
!= IPP_TAG_TEXT
)
2508 "status-message (text(255)) has wrong value tag "
2509 "%s (RFC 2911 section 3.1.6.2).",
2510 ippTagString(attrptr
->value_tag
));
2511 if (attrptr
->group_tag
!= IPP_TAG_OPERATION
)
2513 "status-message (text(255)) has wrong group tag "
2514 "%s (RFC 2911 section 3.1.6.2).",
2515 ippTagString(attrptr
->group_tag
));
2516 if (attrptr
->num_values
!= 1)
2518 "status-message (text(255)) has %d values "
2519 "(RFC 2911 section 3.1.6.2).",
2520 attrptr
->num_values
);
2521 if (attrptr
->value_tag
== IPP_TAG_TEXT
&&
2522 strlen(attrptr
->values
[0].string
.text
) > 255)
2524 "status-message (text(255)) has bad length %d"
2525 " (RFC 2911 section 3.1.6.2).",
2526 (int)strlen(attrptr
->values
[0].string
.text
));
2529 if ((attrptr
= ippFindAttribute(response
, "detailed-status-message",
2530 IPP_TAG_ZERO
)) != NULL
)
2532 if (attrptr
->value_tag
!= IPP_TAG_TEXT
)
2534 "detailed-status-message (text(MAX)) has wrong "
2535 "value tag %s (RFC 2911 section 3.1.6.3).",
2536 ippTagString(attrptr
->value_tag
));
2537 if (attrptr
->group_tag
!= IPP_TAG_OPERATION
)
2539 "detailed-status-message (text(MAX)) has wrong "
2540 "group tag %s (RFC 2911 section 3.1.6.3).",
2541 ippTagString(attrptr
->group_tag
));
2542 if (attrptr
->num_values
!= 1)
2544 "detailed-status-message (text(MAX)) has %d values"
2545 " (RFC 2911 section 3.1.6.3).",
2546 attrptr
->num_values
);
2547 if (attrptr
->value_tag
== IPP_TAG_TEXT
&&
2548 strlen(attrptr
->values
[0].string
.text
) > 1023)
2550 "detailed-status-message (text(MAX)) has bad "
2551 "length %d (RFC 2911 section 3.1.6.3).",
2552 (int)strlen(attrptr
->values
[0].string
.text
));
2555 a
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
2557 for (attrptr
= response
->attrs
, group
= attrptr
->group_tag
;
2559 attrptr
= attrptr
->next
)
2561 if (attrptr
->group_tag
!= group
)
2563 int out_of_order
= 0; /* Are attribute groups out-of-order? */
2567 switch (attrptr
->group_tag
)
2572 case IPP_TAG_OPERATION
:
2576 case IPP_TAG_UNSUPPORTED_GROUP
:
2577 if (group
!= IPP_TAG_OPERATION
)
2582 case IPP_TAG_PRINTER
:
2583 if (group
!= IPP_TAG_OPERATION
&&
2584 group
!= IPP_TAG_UNSUPPORTED_GROUP
)
2588 case IPP_TAG_SUBSCRIPTION
:
2589 if (group
> attrptr
->group_tag
&&
2590 group
!= IPP_TAG_DOCUMENT
)
2595 if (group
> attrptr
->group_tag
)
2601 add_stringf(errors
, "Attribute groups out of order (%s < %s)",
2602 ippTagString(attrptr
->group_tag
),
2603 ippTagString(group
));
2605 if (attrptr
->group_tag
!= IPP_TAG_ZERO
)
2606 group
= attrptr
->group_tag
;
2609 validate_attr(errors
, attrptr
);
2613 if (cupsArrayFind(a
, attrptr
->name
))
2614 add_stringf(errors
, "Duplicate \"%s\" attribute in %s group",
2615 attrptr
->name
, ippTagString(group
));
2617 cupsArrayAdd(a
, attrptr
->name
);
2624 * Now check the test-defined expected status-code and attribute
2628 for (i
= 0; i
< num_statuses
; i
++)
2630 if (statuses
[i
].if_defined
&&
2631 !get_variable(vars
, statuses
[i
].if_defined
))
2634 if (statuses
[i
].if_not_defined
&&
2635 get_variable(vars
, statuses
[i
].if_not_defined
))
2638 if (response
->request
.status
.status_code
== statuses
[i
].status
)
2640 if (statuses
[i
].repeat_match
&&
2641 repeat_count
< statuses
[i
].repeat_limit
)
2646 else if (statuses
[i
].repeat_no_match
&&
2647 repeat_count
< statuses
[i
].repeat_limit
)
2651 if (i
== num_statuses
&& num_statuses
> 0)
2653 for (i
= 0; i
< num_statuses
; i
++)
2655 if (statuses
[i
].if_defined
&&
2656 !get_variable(vars
, statuses
[i
].if_defined
))
2659 if (statuses
[i
].if_not_defined
&&
2660 get_variable(vars
, statuses
[i
].if_not_defined
))
2663 add_stringf(errors
, "EXPECTED: STATUS %s (got %s)",
2664 ippErrorString(statuses
[i
].status
),
2665 ippErrorString(cupsLastError()));
2668 if ((attrptr
= ippFindAttribute(response
, "status-message",
2669 IPP_TAG_TEXT
)) != NULL
)
2670 add_stringf(errors
, "status-message=\"%s\"",
2671 attrptr
->values
[0].string
.text
);
2674 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
2676 if (expect
->if_defined
&& !get_variable(vars
, expect
->if_defined
))
2679 if (expect
->if_not_defined
&&
2680 get_variable(vars
, expect
->if_not_defined
))
2683 found
= ippFindAttribute(response
, expect
->name
, IPP_TAG_ZERO
);
2685 if ((found
&& expect
->not_expect
) ||
2686 (!found
&& !(expect
->not_expect
|| expect
->optional
)) ||
2687 (found
&& !expect_matches(expect
, found
->value_tag
)) ||
2688 (found
&& expect
->in_group
&&
2689 found
->group_tag
!= expect
->in_group
))
2691 if (expect
->define_no_match
)
2692 set_variable(vars
, expect
->define_no_match
, "1");
2693 else if (!expect
->define_match
&& !expect
->define_value
)
2695 if (found
&& expect
->not_expect
)
2696 add_stringf(errors
, "NOT EXPECTED: %s", expect
->name
);
2697 else if (!found
&& !(expect
->not_expect
|| expect
->optional
))
2698 add_stringf(errors
, "EXPECTED: %s", expect
->name
);
2701 if (!expect_matches(expect
, found
->value_tag
))
2702 add_stringf(errors
, "EXPECTED: %s OF-TYPE %s (got %s)",
2703 expect
->name
, expect
->of_type
,
2704 ippTagString(found
->value_tag
));
2706 if (expect
->in_group
&& found
->group_tag
!= expect
->in_group
)
2707 add_stringf(errors
, "EXPECTED: %s IN-GROUP %s (got %s).",
2708 expect
->name
, ippTagString(expect
->in_group
),
2709 ippTagString(found
->group_tag
));
2713 if (expect
->repeat_no_match
&&
2714 repeat_count
< expect
->repeat_limit
)
2721 ippAttributeString(found
, buffer
, sizeof(buffer
));
2724 !with_value(NULL
, expect
->with_value
, expect
->with_flags
, found
,
2725 buffer
, sizeof(buffer
)))
2727 if (expect
->define_no_match
)
2728 set_variable(vars
, expect
->define_no_match
, "1");
2729 else if (!expect
->define_match
&& !expect
->define_value
)
2731 if (expect
->with_flags
& _CUPS_WITH_REGEX
)
2732 add_stringf(errors
, "EXPECTED: %s %s /%s/",
2734 (expect
->with_flags
& _CUPS_WITH_ALL
) ?
2735 "WITH-ALL-VALUES" : "WITH-VALUE",
2736 expect
->with_value
);
2738 add_stringf(errors
, "EXPECTED: %s %s \"%s\"",
2740 (expect
->with_flags
& _CUPS_WITH_ALL
) ?
2741 "WITH-ALL-VALUES" : "WITH-VALUE",
2742 expect
->with_value
);
2744 with_value(errors
, expect
->with_value
, expect
->with_flags
, found
,
2745 buffer
, sizeof(buffer
));
2748 if (expect
->repeat_no_match
&&
2749 repeat_count
< expect
->repeat_limit
)
2755 if (found
&& expect
->count
> 0 &&
2756 found
->num_values
!= expect
->count
)
2758 if (expect
->define_no_match
)
2759 set_variable(vars
, expect
->define_no_match
, "1");
2760 else if (!expect
->define_match
&& !expect
->define_value
)
2762 add_stringf(errors
, "EXPECTED: %s COUNT %d (got %d)", expect
->name
,
2763 expect
->count
, found
->num_values
);
2766 if (expect
->repeat_no_match
&&
2767 repeat_count
< expect
->repeat_limit
)
2773 if (found
&& expect
->same_count_as
)
2775 attrptr
= ippFindAttribute(response
, expect
->same_count_as
,
2778 if (!attrptr
|| attrptr
->num_values
!= found
->num_values
)
2780 if (expect
->define_no_match
)
2781 set_variable(vars
, expect
->define_no_match
, "1");
2782 else if (!expect
->define_match
&& !expect
->define_value
)
2786 "EXPECTED: %s (%d values) SAME-COUNT-AS %s "
2787 "(not returned)", expect
->name
,
2788 found
->num_values
, expect
->same_count_as
);
2789 else if (attrptr
->num_values
!= found
->num_values
)
2791 "EXPECTED: %s (%d values) SAME-COUNT-AS %s "
2792 "(%d values)", expect
->name
, found
->num_values
,
2793 expect
->same_count_as
, attrptr
->num_values
);
2796 if (expect
->repeat_no_match
&&
2797 repeat_count
< expect
->repeat_limit
)
2804 if (found
&& expect
->define_match
)
2805 set_variable(vars
, expect
->define_match
, "1");
2807 if (found
&& expect
->define_value
)
2808 set_variable(vars
, expect
->define_value
, buffer
);
2810 if (found
&& expect
->repeat_match
&&
2811 repeat_count
< expect
->repeat_limit
)
2817 * If we are going to repeat this test, sleep 1 second so we don't flood
2818 * the printer with requests...
2823 if (Output
== _CUPS_OUTPUT_TEST
)
2825 printf("%04d]\n", repeat_count
);
2829 sleep(repeat_interval
);
2830 repeat_interval
= _cupsNextDelay(repeat_interval
, &repeat_prev
);
2832 if (Output
== _CUPS_OUTPUT_TEST
)
2834 printf(" %-68.68s [", name
);
2839 while (repeat_test
);
2845 if (cupsArrayCount(errors
) > 0)
2846 prev_pass
= pass
= 0;
2853 if (Output
== _CUPS_OUTPUT_PLIST
)
2855 puts("<key>Successful</key>");
2856 puts(prev_pass
? "<true />" : "<false />");
2857 puts("<key>StatusCode</key>");
2858 print_xml_string("string", ippErrorString(cupsLastError()));
2859 puts("<key>ResponseAttributes</key>");
2862 for (attrptr
= response
? response
->attrs
: NULL
,
2863 group
= attrptr
? attrptr
->group_tag
: IPP_TAG_ZERO
;
2865 attrptr
= attrptr
->next
)
2866 print_attr(attrptr
, &group
);
2870 else if (Output
== _CUPS_OUTPUT_TEST
)
2872 puts(prev_pass
? "PASS]" : "FAIL]");
2874 if (!prev_pass
|| (Verbosity
&& response
))
2876 printf(" RECEIVED: %lu bytes in response\n",
2877 (unsigned long)ippLength(response
));
2878 printf(" status-code = %s (%s)\n", ippErrorString(cupsLastError()),
2879 cupsLastErrorString());
2883 for (attrptr
= response
->attrs
;
2885 attrptr
= attrptr
->next
)
2886 print_attr(attrptr
, NULL
);
2890 else if (!prev_pass
)
2891 fprintf(stderr
, "%s\n", cupsLastErrorString());
2893 if (prev_pass
&& Output
>= _CUPS_OUTPUT_LIST
&& !Verbosity
&&
2896 size_t width
; /* Length of value */
2898 for (i
= 0; i
< num_displayed
; i
++)
2900 widths
[i
] = strlen(displayed
[i
]);
2902 for (attrptr
= ippFindAttribute(response
, displayed
[i
], IPP_TAG_ZERO
);
2904 attrptr
= ippFindNextAttribute(response
, displayed
[i
],
2907 width
= ippAttributeString(attrptr
, NULL
, 0);
2908 if (width
> widths
[i
])
2913 if (Output
== _CUPS_OUTPUT_CSV
)
2914 print_csv(NULL
, num_displayed
, displayed
, widths
);
2916 print_line(NULL
, num_displayed
, displayed
, widths
);
2918 attrptr
= response
->attrs
;
2922 while (attrptr
&& attrptr
->group_tag
<= IPP_TAG_OPERATION
)
2923 attrptr
= attrptr
->next
;
2927 if (Output
== _CUPS_OUTPUT_CSV
)
2928 print_csv(attrptr
, num_displayed
, displayed
, widths
);
2930 print_line(attrptr
, num_displayed
, displayed
, widths
);
2932 while (attrptr
&& attrptr
->group_tag
> IPP_TAG_OPERATION
)
2933 attrptr
= attrptr
->next
;
2937 else if (!prev_pass
)
2939 if (Output
== _CUPS_OUTPUT_PLIST
)
2941 puts("<key>Errors</key>");
2944 for (error
= (char *)cupsArrayFirst(errors
);
2946 error
= (char *)cupsArrayNext(errors
))
2947 print_xml_string("string", error
);
2953 for (error
= (char *)cupsArrayFirst(errors
);
2955 error
= (char *)cupsArrayNext(errors
))
2956 printf(" %s\n", error
);
2960 if (num_displayed
> 0 && !Verbosity
&& response
&&
2961 (Output
== _CUPS_OUTPUT_TEST
|| Output
== _CUPS_OUTPUT_PLIST
))
2963 for (attrptr
= response
->attrs
;
2965 attrptr
= attrptr
->next
)
2969 for (i
= 0; i
< num_displayed
; i
++)
2971 if (!strcmp(displayed
[i
], attrptr
->name
))
2973 print_attr(attrptr
, NULL
);
2983 if (Output
== _CUPS_OUTPUT_PLIST
)
2988 ippDelete(response
);
2991 for (i
= 0; i
< num_statuses
; i
++)
2993 if (statuses
[i
].if_defined
)
2994 free(statuses
[i
].if_defined
);
2995 if (statuses
[i
].if_not_defined
)
2996 free(statuses
[i
].if_not_defined
);
3000 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
3003 if (expect
->of_type
)
3004 free(expect
->of_type
);
3005 if (expect
->same_count_as
)
3006 free(expect
->same_count_as
);
3007 if (expect
->if_defined
)
3008 free(expect
->if_defined
);
3009 if (expect
->if_not_defined
)
3010 free(expect
->if_not_defined
);
3011 if (expect
->with_value
)
3012 free(expect
->with_value
);
3013 if (expect
->define_match
)
3014 free(expect
->define_match
);
3015 if (expect
->define_no_match
)
3016 free(expect
->define_no_match
);
3017 if (expect
->define_value
)
3018 free(expect
->define_value
);
3022 for (i
= 0; i
< num_displayed
; i
++)
3026 if (!ignore_errors
&& !prev_pass
)
3032 cupsArrayDelete(errors
);
3039 ippDelete(response
);
3041 for (i
= 0; i
< num_statuses
; i
++)
3043 if (statuses
[i
].if_defined
)
3044 free(statuses
[i
].if_defined
);
3045 if (statuses
[i
].if_not_defined
)
3046 free(statuses
[i
].if_not_defined
);
3049 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
3052 if (expect
->of_type
)
3053 free(expect
->of_type
);
3054 if (expect
->same_count_as
)
3055 free(expect
->same_count_as
);
3056 if (expect
->if_defined
)
3057 free(expect
->if_defined
);
3058 if (expect
->if_not_defined
)
3059 free(expect
->if_not_defined
);
3060 if (expect
->with_value
)
3061 free(expect
->with_value
);
3062 if (expect
->define_match
)
3063 free(expect
->define_match
);
3064 if (expect
->define_no_match
)
3065 free(expect
->define_no_match
);
3066 if (expect
->define_value
)
3067 free(expect
->define_value
);
3070 for (i
= 0; i
< num_displayed
; i
++)
3078 * 'expand_variables()' - Expand variables in a string.
3082 expand_variables(_cups_vars_t
*vars
, /* I - Variables */
3083 char *dst
, /* I - Destination string buffer */
3084 const char *src
, /* I - Source string */
3085 size_t dstsize
) /* I - Size of destination buffer */
3087 char *dstptr
, /* Pointer into destination */
3088 *dstend
, /* End of destination */
3089 temp
[256], /* Temporary string */
3090 *tempptr
; /* Pointer into temporary string */
3091 const char *value
; /* Value to substitute */
3095 dstend
= dst
+ dstsize
- 1;
3097 while (*src
&& dstptr
< dstend
)
3102 * Substitute a string/number...
3105 if (!strncmp(src
, "$$", 2))
3110 else if (!strncmp(src
, "$ENV[", 5))
3112 strlcpy(temp
, src
+ 5, sizeof(temp
));
3114 for (tempptr
= temp
; *tempptr
; tempptr
++)
3115 if (*tempptr
== ']')
3121 value
= getenv(temp
);
3122 src
+= tempptr
- temp
+ 5;
3126 strlcpy(temp
, src
+ 1, sizeof(temp
));
3128 for (tempptr
= temp
; *tempptr
; tempptr
++)
3129 if (!isalnum(*tempptr
& 255) && *tempptr
!= '-' && *tempptr
!= '_')
3135 if (!strcmp(temp
, "uri"))
3137 else if (!strcmp(temp
, "filename"))
3138 value
= vars
->filename
;
3139 else if (!strcmp(temp
, "scheme") || !strcmp(temp
, "method"))
3140 value
= vars
->scheme
;
3141 else if (!strcmp(temp
, "username"))
3142 value
= vars
->userpass
;
3143 else if (!strcmp(temp
, "hostname"))
3144 value
= vars
->hostname
;
3145 else if (!strcmp(temp
, "port"))
3147 snprintf(temp
, sizeof(temp
), "%d", vars
->port
);
3150 else if (!strcmp(temp
, "resource"))
3151 value
= vars
->resource
;
3152 else if (!strcmp(temp
, "user"))
3155 value
= get_variable(vars
, temp
);
3157 src
+= tempptr
- temp
+ 1;
3167 strlcpy(dstptr
, value
, dstend
- dstptr
+ 1);
3168 dstptr
+= strlen(dstptr
);
3180 * 'expect_matches()' - Return true if the tag matches the specification.
3183 static int /* O - 1 if matches, 0 otherwise */
3185 _cups_expect_t
*expect
, /* I - Expected attribute */
3186 ipp_tag_t value_tag
) /* I - Value tag for attribute */
3188 int match
; /* Match? */
3189 char *of_type
, /* Type name to match */
3190 *next
, /* Next name to match */
3191 sep
; /* Separator character */
3195 * If we don't expect a particular type, return immediately...
3198 if (!expect
->of_type
)
3202 * Parse the "of_type" value since the string can contain multiple attribute
3203 * types separated by "," or "|"...
3206 for (of_type
= expect
->of_type
, match
= 0; !match
&& *of_type
; of_type
= next
)
3209 * Find the next separator, and set it (temporarily) to nul if present.
3212 for (next
= of_type
; *next
&& *next
!= '|' && *next
!= ','; next
++);
3214 if ((sep
= *next
) != '\0')
3218 * Support some meta-types to make it easier to write the test file.
3221 if (!strcmp(of_type
, "text"))
3222 match
= value_tag
== IPP_TAG_TEXTLANG
|| value_tag
== IPP_TAG_TEXT
;
3223 else if (!strcmp(of_type
, "name"))
3224 match
= value_tag
== IPP_TAG_NAMELANG
|| value_tag
== IPP_TAG_NAME
;
3225 else if (!strcmp(of_type
, "collection"))
3226 match
= value_tag
== IPP_TAG_BEGIN_COLLECTION
;
3228 match
= value_tag
== ippTagValue(of_type
);
3231 * Restore the separator if we have one...
3243 * 'get_collection()' - Get a collection value from the current test file.
3246 static ipp_t
* /* O - Collection value */
3247 get_collection(_cups_vars_t
*vars
, /* I - Variables */
3248 FILE *fp
, /* I - File to read from */
3249 int *linenum
) /* IO - Line number */
3251 char token
[1024], /* Token from file */
3252 temp
[1024], /* Temporary string */
3253 attr
[128]; /* Attribute name */
3254 ipp_tag_t value
; /* Current value type */
3255 ipp_t
*col
= ippNew(); /* Collection value */
3256 ipp_attribute_t
*lastcol
= NULL
; /* Last collection attribute */
3259 while (get_token(fp
, token
, sizeof(token
), linenum
) != NULL
)
3261 if (!strcmp(token
, "}"))
3263 else if (!strcmp(token
, "{") && lastcol
)
3266 * Another collection value
3269 ipp_t
*subcol
= get_collection(vars
, fp
, linenum
);
3270 /* Collection value */
3274 ipp_attribute_t
*tempcol
; /* Pointer to new buffer */
3278 * Reallocate memory...
3281 if ((tempcol
= realloc(lastcol
, sizeof(ipp_attribute_t
) +
3282 (lastcol
->num_values
+ 1) *
3283 sizeof(_ipp_value_t
))) == NULL
)
3285 print_fatal_error("Unable to allocate memory on line %d.", *linenum
);
3289 if (tempcol
!= lastcol
)
3292 * Reset pointers in the list...
3296 col
->prev
->next
= tempcol
;
3298 col
->attrs
= tempcol
;
3300 lastcol
= col
->current
= col
->last
= tempcol
;
3303 lastcol
->values
[lastcol
->num_values
].collection
= subcol
;
3304 lastcol
->num_values
++;
3309 else if (!_cups_strcasecmp(token
, "MEMBER"))
3317 if (!get_token(fp
, token
, sizeof(token
), linenum
))
3319 print_fatal_error("Missing MEMBER value tag on line %d.", *linenum
);
3323 if ((value
= ippTagValue(token
)) == IPP_TAG_ZERO
)
3325 print_fatal_error("Bad MEMBER value tag \"%s\" on line %d.", token
,
3330 if (!get_token(fp
, attr
, sizeof(attr
), linenum
))
3332 print_fatal_error("Missing MEMBER name on line %d.", *linenum
);
3336 if (!get_token(fp
, temp
, sizeof(temp
), linenum
))
3338 print_fatal_error("Missing MEMBER value on line %d.", *linenum
);
3342 expand_variables(vars
, token
, temp
, sizeof(token
));
3346 case IPP_TAG_BOOLEAN
:
3347 if (!_cups_strcasecmp(token
, "true"))
3348 ippAddBoolean(col
, IPP_TAG_ZERO
, attr
, 1);
3350 ippAddBoolean(col
, IPP_TAG_ZERO
, attr
, atoi(token
));
3353 case IPP_TAG_INTEGER
:
3355 ippAddInteger(col
, IPP_TAG_ZERO
, value
, attr
, atoi(token
));
3358 case IPP_TAG_RESOLUTION
:
3360 int xres
, /* X resolution */
3361 yres
; /* Y resolution */
3362 char units
[6]; /* Units */
3364 if (sscanf(token
, "%dx%d%5s", &xres
, &yres
, units
) != 3 ||
3365 (_cups_strcasecmp(units
, "dpi") &&
3366 _cups_strcasecmp(units
, "dpc") &&
3367 _cups_strcasecmp(units
, "dpcm") &&
3368 _cups_strcasecmp(units
, "other")))
3370 print_fatal_error("Bad resolution value \"%s\" on line %d.",
3375 if (!_cups_strcasecmp(units
, "dpi"))
3376 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, xres
, yres
,
3378 else if (!_cups_strcasecmp(units
, "dpc") ||
3379 !_cups_strcasecmp(units
, "dpcm"))
3380 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, xres
, yres
,
3383 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, xres
, yres
,
3388 case IPP_TAG_RANGE
:
3390 int lowers
[4], /* Lower value */
3391 uppers
[4], /* Upper values */
3392 num_vals
; /* Number of values */
3395 num_vals
= sscanf(token
, "%d-%d,%d-%d,%d-%d,%d-%d",
3396 lowers
+ 0, uppers
+ 0,
3397 lowers
+ 1, uppers
+ 1,
3398 lowers
+ 2, uppers
+ 2,
3399 lowers
+ 3, uppers
+ 3);
3401 if ((num_vals
& 1) || num_vals
== 0)
3403 print_fatal_error("Bad rangeOfInteger value \"%s\" on line %d.",
3408 ippAddRanges(col
, IPP_TAG_ZERO
, attr
, num_vals
/ 2, lowers
,
3413 case IPP_TAG_BEGIN_COLLECTION
:
3414 if (!strcmp(token
, "{"))
3416 ipp_t
*subcol
= get_collection(vars
, fp
, linenum
);
3417 /* Collection value */
3421 lastcol
= ippAddCollection(col
, IPP_TAG_ZERO
, attr
, subcol
);
3429 print_fatal_error("Bad collection value on line %d.", *linenum
);
3435 if (!strchr(token
, ','))
3436 ippAddString(col
, IPP_TAG_ZERO
, value
, attr
, NULL
, token
);
3440 * Multiple string values...
3443 int num_values
; /* Number of values */
3444 char *values
[100], /* Values */
3445 *ptr
; /* Pointer to next value */
3451 for (ptr
= strchr(token
, ','); ptr
; ptr
= strchr(ptr
, ','))
3454 values
[num_values
] = ptr
;
3458 ippAddStrings(col
, IPP_TAG_ZERO
, value
, attr
, num_values
,
3459 NULL
, (const char **)values
);
3469 * If we get here there was a parse error; free memory and return.
3481 * 'get_filename()' - Get a filename based on the current test file.
3484 static char * /* O - Filename */
3485 get_filename(const char *testfile
, /* I - Current test file */
3486 char *dst
, /* I - Destination filename */
3487 const char *src
, /* I - Source filename */
3488 size_t dstsize
) /* I - Size of destination buffer */
3490 char *dstptr
; /* Pointer into destination */
3491 _cups_globals_t
*cg
= _cupsGlobals();
3495 if (*src
== '<' && src
[strlen(src
) - 1] == '>')
3498 * Map <filename> to CUPS_DATADIR/ipptool/filename...
3501 snprintf(dst
, dstsize
, "%s/ipptool/%s", cg
->cups_datadir
, src
+ 1);
3502 dstptr
= dst
+ strlen(dst
) - 1;
3506 else if (*src
== '/' || !strchr(testfile
, '/')
3508 || (isalpha(*src
& 255) && src
[1] == ':')
3513 * Use the path as-is...
3516 strlcpy(dst
, src
, dstsize
);
3521 * Make path relative to testfile...
3524 strlcpy(dst
, testfile
, dstsize
);
3525 if ((dstptr
= strrchr(dst
, '/')) != NULL
)
3528 dstptr
= dst
; /* Should never happen */
3530 strlcpy(dstptr
, src
, dstsize
- (dstptr
- dst
));
3538 * 'get_token()' - Get a token from a file.
3541 static char * /* O - Token from file or NULL on EOF */
3542 get_token(FILE *fp
, /* I - File to read from */
3543 char *buf
, /* I - Buffer to read into */
3544 int buflen
, /* I - Length of buffer */
3545 int *linenum
) /* IO - Current line number */
3547 int ch
, /* Character from file */
3548 quote
; /* Quoting character */
3549 char *bufptr
, /* Pointer into buffer */
3550 *bufend
; /* End of buffer */
3556 * Skip whitespace...
3559 while (isspace(ch
= getc(fp
)))
3571 else if (ch
== '\'' || ch
== '\"')
3574 * Quoted text or regular expression...
3579 bufend
= buf
+ buflen
- 1;
3581 while ((ch
= getc(fp
)) != EOF
)
3586 * Escape next character...
3589 if (bufptr
< bufend
)
3592 if ((ch
= getc(fp
)) != EOF
&& bufptr
< bufend
)
3595 else if (ch
== quote
)
3597 else if (bufptr
< bufend
)
3611 while ((ch
= getc(fp
)) != EOF
)
3620 * Whitespace delimited text...
3626 bufend
= buf
+ buflen
- 1;
3628 while ((ch
= getc(fp
)) != EOF
)
3629 if (isspace(ch
) || ch
== '#')
3631 else if (bufptr
< bufend
)
3636 else if (ch
== '\n')
3648 * 'get_variable()' - Get the value of a variable.
3651 static char * /* O - Value or NULL */
3652 get_variable(_cups_vars_t
*vars
, /* I - Variables */
3653 const char *name
) /* I - Variable name */
3655 _cups_var_t key
, /* Search key */
3656 *match
; /* Matching variable, if any */
3659 key
.name
= (char *)name
;
3660 match
= cupsArrayFind(vars
->vars
, &key
);
3662 return (match
? match
->value
: NULL
);
3667 * 'iso_date()' - Return an ISO 8601 date/time string for the given IPP dateTime
3671 static char * /* O - ISO 8601 date/time string */
3672 iso_date(ipp_uchar_t
*date
) /* I - IPP (RFC 1903) date/time value */
3674 time_t utctime
; /* UTC time since 1970 */
3675 struct tm
*utcdate
; /* UTC date/time */
3676 static char buffer
[255]; /* String buffer */
3679 utctime
= ippDateToTime(date
);
3680 utcdate
= gmtime(&utctime
);
3682 snprintf(buffer
, sizeof(buffer
), "%04d-%02d-%02dT%02d:%02d:%02dZ",
3683 utcdate
->tm_year
+ 1900, utcdate
->tm_mon
+ 1, utcdate
->tm_mday
,
3684 utcdate
->tm_hour
, utcdate
->tm_min
, utcdate
->tm_sec
);
3691 * 'password_cb()' - Password callback for authenticated tests.
3694 static const char * /* O - Password */
3695 password_cb(const char *prompt
) /* I - Prompt (unused) */
3704 * 'print_attr()' - Print an attribute on the screen.
3708 print_attr(ipp_attribute_t
*attr
, /* I - Attribute to print */
3709 ipp_tag_t
*group
) /* IO - Current group */
3711 int i
; /* Looping var */
3712 ipp_attribute_t
*colattr
; /* Collection attribute */
3715 if (Output
== _CUPS_OUTPUT_PLIST
)
3717 if (!attr
->name
|| (group
&& *group
!= attr
->group_tag
))
3723 *group
= attr
->group_tag
;
3729 print_xml_string("key", attr
->name
);
3730 if (attr
->num_values
> 1)
3733 switch (attr
->value_tag
)
3735 case IPP_TAG_INTEGER
:
3737 for (i
= 0; i
< attr
->num_values
; i
++)
3738 if (Output
== _CUPS_OUTPUT_PLIST
)
3739 printf("<integer>%d</integer>\n", attr
->values
[i
].integer
);
3741 printf("%d ", attr
->values
[i
].integer
);
3744 case IPP_TAG_BOOLEAN
:
3745 for (i
= 0; i
< attr
->num_values
; i
++)
3746 if (Output
== _CUPS_OUTPUT_PLIST
)
3747 puts(attr
->values
[i
].boolean
? "<true />" : "<false />");
3748 else if (attr
->values
[i
].boolean
)
3749 fputs("true ", stdout
);
3751 fputs("false ", stdout
);
3754 case IPP_TAG_RANGE
:
3755 for (i
= 0; i
< attr
->num_values
; i
++)
3756 if (Output
== _CUPS_OUTPUT_PLIST
)
3757 printf("<dict><key>lower</key><integer>%d</integer>"
3758 "<key>upper</key><integer>%d</integer></dict>\n",
3759 attr
->values
[i
].range
.lower
, attr
->values
[i
].range
.upper
);
3761 printf("%d-%d ", attr
->values
[i
].range
.lower
,
3762 attr
->values
[i
].range
.upper
);
3765 case IPP_TAG_RESOLUTION
:
3766 for (i
= 0; i
< attr
->num_values
; i
++)
3767 if (Output
== _CUPS_OUTPUT_PLIST
)
3768 printf("<dict><key>xres</key><integer>%d</integer>"
3769 "<key>yres</key><integer>%d</integer>"
3770 "<key>units</key><string>%s</string></dict>\n",
3771 attr
->values
[i
].resolution
.xres
,
3772 attr
->values
[i
].resolution
.yres
,
3773 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3776 printf("%dx%d%s ", attr
->values
[i
].resolution
.xres
,
3777 attr
->values
[i
].resolution
.yres
,
3778 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3783 for (i
= 0; i
< attr
->num_values
; i
++)
3784 if (Output
== _CUPS_OUTPUT_PLIST
)
3785 printf("<date>%s</date>\n", iso_date(attr
->values
[i
].date
));
3787 printf("%s ", iso_date(attr
->values
[i
].date
));
3790 case IPP_TAG_STRING
:
3793 case IPP_TAG_KEYWORD
:
3794 case IPP_TAG_CHARSET
:
3796 case IPP_TAG_MIMETYPE
:
3797 case IPP_TAG_LANGUAGE
:
3798 for (i
= 0; i
< attr
->num_values
; i
++)
3799 if (Output
== _CUPS_OUTPUT_PLIST
)
3800 print_xml_string("string", attr
->values
[i
].string
.text
);
3802 printf("\"%s\" ", attr
->values
[i
].string
.text
);
3805 case IPP_TAG_TEXTLANG
:
3806 case IPP_TAG_NAMELANG
:
3807 for (i
= 0; i
< attr
->num_values
; i
++)
3808 if (Output
== _CUPS_OUTPUT_PLIST
)
3810 fputs("<dict><key>language</key><string>", stdout
);
3811 print_xml_string(NULL
, attr
->values
[i
].string
.language
);
3812 fputs("</string><key>string</key><string>", stdout
);
3813 print_xml_string(NULL
, attr
->values
[i
].string
.text
);
3814 puts("</string></dict>");
3817 printf("\"%s\"[%s] ", attr
->values
[i
].string
.text
,
3818 attr
->values
[i
].string
.language
);
3821 case IPP_TAG_BEGIN_COLLECTION
:
3822 for (i
= 0; i
< attr
->num_values
; i
++)
3824 if (Output
== _CUPS_OUTPUT_PLIST
)
3827 for (colattr
= attr
->values
[i
].collection
->attrs
;
3829 colattr
= colattr
->next
)
3830 print_attr(colattr
, NULL
);
3838 print_col(attr
->values
[i
].collection
);
3844 if (Output
== _CUPS_OUTPUT_PLIST
)
3845 printf("<string><<%s>></string>\n",
3846 ippTagString(attr
->value_tag
));
3848 fputs(ippTagString(attr
->value_tag
), stdout
);
3852 if (attr
->num_values
> 1)
3857 char buffer
[8192]; /* Value buffer */
3859 if (Output
== _CUPS_OUTPUT_TEST
)
3863 puts(" -- separator --");
3867 printf(" %s (%s%s) = ", attr
->name
,
3868 attr
->num_values
> 1 ? "1setOf " : "",
3869 ippTagString(attr
->value_tag
));
3872 ippAttributeString(attr
, buffer
, sizeof(buffer
));
3879 * 'print_col()' - Print a collection attribute on the screen.
3883 print_col(ipp_t
*col
) /* I - Collection attribute to print */
3885 int i
; /* Looping var */
3886 ipp_attribute_t
*attr
; /* Current attribute in collection */
3889 fputs("{ ", stdout
);
3890 for (attr
= col
->attrs
; attr
; attr
= attr
->next
)
3892 printf("%s (%s%s) = ", attr
->name
, attr
->num_values
> 1 ? "1setOf " : "",
3893 ippTagString(attr
->value_tag
));
3895 switch (attr
->value_tag
)
3897 case IPP_TAG_INTEGER
:
3899 for (i
= 0; i
< attr
->num_values
; i
++)
3900 printf("%d ", attr
->values
[i
].integer
);
3903 case IPP_TAG_BOOLEAN
:
3904 for (i
= 0; i
< attr
->num_values
; i
++)
3905 if (attr
->values
[i
].boolean
)
3911 case IPP_TAG_NOVALUE
:
3915 case IPP_TAG_RANGE
:
3916 for (i
= 0; i
< attr
->num_values
; i
++)
3917 printf("%d-%d ", attr
->values
[i
].range
.lower
,
3918 attr
->values
[i
].range
.upper
);
3921 case IPP_TAG_RESOLUTION
:
3922 for (i
= 0; i
< attr
->num_values
; i
++)
3923 printf("%dx%d%s ", attr
->values
[i
].resolution
.xres
,
3924 attr
->values
[i
].resolution
.yres
,
3925 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3929 case IPP_TAG_STRING
:
3932 case IPP_TAG_KEYWORD
:
3933 case IPP_TAG_CHARSET
:
3935 case IPP_TAG_MIMETYPE
:
3936 case IPP_TAG_LANGUAGE
:
3937 for (i
= 0; i
< attr
->num_values
; i
++)
3938 printf("\"%s\" ", attr
->values
[i
].string
.text
);
3941 case IPP_TAG_TEXTLANG
:
3942 case IPP_TAG_NAMELANG
:
3943 for (i
= 0; i
< attr
->num_values
; i
++)
3944 printf("\"%s\"[%s] ", attr
->values
[i
].string
.text
,
3945 attr
->values
[i
].string
.language
);
3948 case IPP_TAG_BEGIN_COLLECTION
:
3949 for (i
= 0; i
< attr
->num_values
; i
++)
3951 print_col(attr
->values
[i
].collection
);
3957 break; /* anti-compiler-warning-code */
3966 * 'print_csv()' - Print a line of CSV text.
3971 ipp_attribute_t
*attr
, /* I - First attribute for line */
3972 int num_displayed
, /* I - Number of attributes to display */
3973 char **displayed
, /* I - Attributes to display */
3974 size_t *widths
) /* I - Column widths */
3976 int i
; /* Looping var */
3977 size_t maxlength
; /* Max length of all columns */
3978 char *buffer
, /* String buffer */
3979 *bufptr
; /* Pointer into buffer */
3980 ipp_attribute_t
*current
; /* Current attribute */
3984 * Get the maximum string length we have to show and allocate...
3987 for (i
= 1, maxlength
= widths
[0]; i
< num_displayed
; i
++)
3988 if (widths
[i
] > maxlength
)
3989 maxlength
= widths
[i
];
3993 if ((buffer
= malloc(maxlength
)) == NULL
)
3997 * Loop through the attributes to display...
4002 for (i
= 0; i
< num_displayed
; i
++)
4009 for (current
= attr
; current
; current
= current
->next
)
4013 else if (!strcmp(current
->name
, displayed
[i
]))
4015 ippAttributeString(current
, buffer
, maxlength
);
4020 if (strchr(buffer
, ',') != NULL
|| strchr(buffer
, '\"') != NULL
||
4021 strchr(buffer
, '\\') != NULL
)
4024 for (bufptr
= buffer
; *bufptr
; bufptr
++)
4026 if (*bufptr
== '\\' || *bufptr
== '\"')
4033 fputs(buffer
, stdout
);
4039 for (i
= 0; i
< num_displayed
; i
++)
4044 fputs(displayed
[i
], stdout
);
4054 * 'print_fatal_error()' - Print a fatal error message.
4058 print_fatal_error(const char *s
, /* I - Printf-style format string */
4059 ...) /* I - Additional arguments as needed */
4061 char buffer
[10240]; /* Format buffer */
4062 va_list ap
; /* Pointer to arguments */
4066 * Format the error message...
4070 vsnprintf(buffer
, sizeof(buffer
), s
, ap
);
4077 if (Output
== _CUPS_OUTPUT_PLIST
)
4080 print_xml_trailer(0, buffer
);
4083 _cupsLangPrintf(stderr
, "ipptool: %s", buffer
);
4088 * 'print_line()' - Print a line of formatted or CSV text.
4093 ipp_attribute_t
*attr
, /* I - First attribute for line */
4094 int num_displayed
, /* I - Number of attributes to display */
4095 char **displayed
, /* I - Attributes to display */
4096 size_t *widths
) /* I - Column widths */
4098 int i
; /* Looping var */
4099 size_t maxlength
; /* Max length of all columns */
4100 char *buffer
; /* String buffer */
4101 ipp_attribute_t
*current
; /* Current attribute */
4105 * Get the maximum string length we have to show and allocate...
4108 for (i
= 1, maxlength
= widths
[0]; i
< num_displayed
; i
++)
4109 if (widths
[i
] > maxlength
)
4110 maxlength
= widths
[i
];
4114 if ((buffer
= malloc(maxlength
)) == NULL
)
4118 * Loop through the attributes to display...
4123 for (i
= 0; i
< num_displayed
; i
++)
4130 for (current
= attr
; current
; current
= current
->next
)
4134 else if (!strcmp(current
->name
, displayed
[i
]))
4136 ippAttributeString(current
, buffer
, maxlength
);
4141 printf("%*s", (int)-widths
[i
], buffer
);
4147 for (i
= 0; i
< num_displayed
; i
++)
4152 printf("%*s", (int)-widths
[i
], displayed
[i
]);
4156 for (i
= 0; i
< num_displayed
; i
++)
4161 memset(buffer
, '-', widths
[i
]);
4162 buffer
[widths
[i
]] = '\0';
4163 fputs(buffer
, stdout
);
4173 * 'print_xml_header()' - Print a standard XML plist header.
4177 print_xml_header(void)
4181 puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
4182 puts("<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
4183 "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
4184 puts("<plist version=\"1.0\">");
4186 puts("<key>Transfer</key>");
4187 printf("<string>%s</string>\n",
4188 Transfer
== _CUPS_TRANSFER_AUTO
? "auto" :
4189 Transfer
== _CUPS_TRANSFER_CHUNKED
? "chunked" : "length");
4190 puts("<key>Tests</key>");
4199 * 'print_xml_string()' - Print an XML string with escaping.
4203 print_xml_string(const char *element
, /* I - Element name or NULL */
4204 const char *s
) /* I - String to print */
4207 printf("<%s>", element
);
4212 fputs("&", stdout
);
4214 fputs("<", stdout
);
4216 fputs(">", stdout
);
4217 else if ((*s
& 0xe0) == 0xc0)
4220 * Validate UTF-8 two-byte sequence...
4223 if ((s
[1] & 0xc0) != 0x80)
4234 else if ((*s
& 0xf0) == 0xe0)
4237 * Validate UTF-8 three-byte sequence...
4240 if ((s
[1] & 0xc0) != 0x80 || (s
[2] & 0xc0) != 0x80)
4252 else if ((*s
& 0xf8) == 0xf0)
4255 * Validate UTF-8 four-byte sequence...
4258 if ((s
[1] & 0xc0) != 0x80 || (s
[2] & 0xc0) != 0x80 ||
4259 (s
[3] & 0xc0) != 0x80)
4272 else if ((*s
& 0x80) || (*s
< ' ' && !isspace(*s
& 255)))
4275 * Invalid control character...
4287 printf("</%s>\n", element
);
4292 * 'print_xml_trailer()' - Print the XML trailer with success/fail value.
4296 print_xml_trailer(int success
, /* I - 1 on success, 0 on failure */
4297 const char *message
) /* I - Error message or NULL */
4302 puts("<key>Successful</key>");
4303 puts(success
? "<true />" : "<false />");
4306 puts("<key>ErrorMessage</key>");
4307 print_xml_string("string", message
);
4318 * 'set_variable()' - Set a variable value.
4322 set_variable(_cups_vars_t
*vars
, /* I - Variables */
4323 const char *name
, /* I - Variable name */
4324 const char *value
) /* I - Value string */
4326 _cups_var_t key
, /* Search key */
4327 *var
; /* New variable */
4330 if (!_cups_strcasecmp(name
, "filename"))
4333 free(vars
->filename
);
4335 vars
->filename
= strdup(value
);
4339 key
.name
= (char *)name
;
4340 if ((var
= cupsArrayFind(vars
->vars
, &key
)) != NULL
)
4343 var
->value
= strdup(value
);
4345 else if ((var
= malloc(sizeof(_cups_var_t
))) == NULL
)
4347 print_fatal_error("Unable to allocate memory for variable \"%s\".", name
);
4352 var
->name
= strdup(name
);
4353 var
->value
= strdup(value
);
4355 cupsArrayAdd(vars
->vars
, var
);
4362 * 'sigterm_handler()' - Handle SIGINT and SIGTERM.
4366 sigterm_handler(int sig
) /* I - Signal number (unused) */
4372 signal(SIGINT
, SIG_DFL
);
4373 signal(SIGTERM
, SIG_DFL
);
4379 * 'timeout_cb()' - Handle HTTP timeouts.
4382 static int /* O - 1 to continue, 0 to cancel */
4383 timeout_cb(http_t
*http
, /* I - Connection to server (unused) */
4384 void *user_data
) /* I - User data (unused) */
4389 /* Always cancel on timeout */
4395 * 'usage()' - Show program usage.
4401 _cupsLangPuts(stderr
, _("Usage: ipptool [options] URI filename [ ... "
4403 _cupsLangPuts(stderr
, _("Options:"));
4404 _cupsLangPuts(stderr
, _(" -4 Connect using IPv4."));
4405 _cupsLangPuts(stderr
, _(" -6 Connect using IPv6."));
4406 _cupsLangPuts(stderr
, _(" -C Send requests using "
4407 "chunking (default)."));
4408 _cupsLangPuts(stdout
, _(" -E Test with HTTP Upgrade to "
4410 _cupsLangPuts(stderr
, _(" -I Ignore errors."));
4411 _cupsLangPuts(stderr
, _(" -L Send requests using "
4412 "content-length."));
4413 _cupsLangPuts(stderr
, _(" -S Test with SSL "
4415 _cupsLangPuts(stderr
, _(" -T seconds Set the receive/send "
4416 "timeout in seconds."));
4417 _cupsLangPuts(stderr
, _(" -V version Set default IPP "
4419 _cupsLangPuts(stderr
, _(" -X Produce XML plist instead "
4421 _cupsLangPuts(stderr
, _(" -d name=value Set named variable to "
4423 _cupsLangPuts(stderr
, _(" -f filename Set default request "
4425 _cupsLangPuts(stderr
, _(" -i seconds Repeat the last file with "
4426 "the given time interval."));
4427 _cupsLangPuts(stderr
, _(" -n count Repeat the last file the "
4428 "given number of times."));
4429 _cupsLangPuts(stderr
, _(" -q Run silently."));
4430 _cupsLangPuts(stderr
, _(" -t Produce a test report."));
4431 _cupsLangPuts(stderr
, _(" -v Be verbose."));
4438 * 'validate_attr()' - Determine whether an attribute is valid.
4441 static int /* O - 1 if valid, 0 otherwise */
4442 validate_attr(cups_array_t
*errors
, /* I - Errors array */
4443 ipp_attribute_t
*attr
) /* I - Attribute to validate */
4445 int i
; /* Looping var */
4446 char scheme
[64], /* Scheme from URI */
4447 userpass
[256], /* Username/password from URI */
4448 hostname
[256], /* Hostname from URI */
4449 resource
[1024]; /* Resource from URI */
4450 int port
, /* Port number from URI */
4451 uri_status
, /* URI separation status */
4452 valid
= 1; /* Is the attribute valid? */
4453 const char *ptr
; /* Pointer into string */
4454 ipp_attribute_t
*colattr
; /* Collection attribute */
4455 regex_t re
; /* Regular expression */
4456 ipp_uchar_t
*date
; /* Current date value */
4467 * Validate the attribute name.
4470 for (ptr
= attr
->name
; *ptr
; ptr
++)
4471 if (!isalnum(*ptr
& 255) && *ptr
!= '-' && *ptr
!= '.' && *ptr
!= '_')
4474 if (*ptr
|| ptr
== attr
->name
)
4479 "\"%s\": Bad attribute name - invalid character "
4480 "(RFC 2911 section 4.1.3).", attr
->name
);
4483 if ((ptr
- attr
->name
) > 255)
4488 "\"%s\": Bad attribute name - bad length "
4489 "(RFC 2911 section 4.1.3).", attr
->name
);
4492 switch (attr
->value_tag
)
4494 case IPP_TAG_INTEGER
:
4497 case IPP_TAG_BOOLEAN
:
4498 for (i
= 0; i
< attr
->num_values
; i
++)
4500 if (attr
->values
[i
].boolean
!= 0 &&
4501 attr
->values
[i
].boolean
!= 1)
4506 "\"%s\": Bad boolen value %d "
4507 "(RFC 2911 section 4.1.10).", attr
->name
,
4508 attr
->values
[i
].boolean
);
4514 for (i
= 0; i
< attr
->num_values
; i
++)
4516 if (attr
->values
[i
].integer
< 1)
4521 "\"%s\": Bad enum value %d - out of range "
4522 "(RFC 2911 section 4.1.4).", attr
->name
,
4523 attr
->values
[i
].integer
);
4528 case IPP_TAG_STRING
:
4529 for (i
= 0; i
< attr
->num_values
; i
++)
4531 if (attr
->values
[i
].unknown
.length
> 1023)
4536 "\"%s\": Bad octetString value - bad length %d "
4537 "(RFC 2911 section 4.1.10).", attr
->name
,
4538 attr
->values
[i
].unknown
.length
);
4544 for (i
= 0; i
< attr
->num_values
; i
++)
4546 date
= attr
->values
[i
].date
;
4548 if (date
[2] < 1 || date
[2] > 12)
4553 "\"%s\": Bad dateTime month %u "
4554 "(RFC 2911 section 4.1.13).", attr
->name
, date
[2]);
4557 if (date
[3] < 1 || date
[3] > 31)
4562 "\"%s\": Bad dateTime day %u "
4563 "(RFC 2911 section 4.1.13).", attr
->name
, date
[3]);
4571 "\"%s\": Bad dateTime hours %u "
4572 "(RFC 2911 section 4.1.13).", attr
->name
, date
[4]);
4580 "\"%s\": Bad dateTime minutes %u "
4581 "(RFC 2911 section 4.1.13).", attr
->name
, date
[5]);
4589 "\"%s\": Bad dateTime seconds %u "
4590 "(RFC 2911 section 4.1.13).", attr
->name
, date
[6]);
4598 "\"%s\": Bad dateTime deciseconds %u "
4599 "(RFC 2911 section 4.1.13).", attr
->name
, date
[7]);
4602 if (date
[8] != '-' && date
[8] != '+')
4607 "\"%s\": Bad dateTime UTC sign '%c' "
4608 "(RFC 2911 section 4.1.13).", attr
->name
, date
[8]);
4616 "\"%s\": Bad dateTime UTC hours %u "
4617 "(RFC 2911 section 4.1.13).", attr
->name
, date
[9]);
4625 "\"%s\": Bad dateTime UTC minutes %u "
4626 "(RFC 2911 section 4.1.13).", attr
->name
, date
[10]);
4631 case IPP_TAG_RESOLUTION
:
4632 for (i
= 0; i
< attr
->num_values
; i
++)
4634 if (attr
->values
[i
].resolution
.xres
<= 0)
4639 "\"%s\": Bad resolution value %dx%d%s - cross "
4640 "feed resolution must be positive "
4641 "(RFC 2911 section 4.1.13).", attr
->name
,
4642 attr
->values
[i
].resolution
.xres
,
4643 attr
->values
[i
].resolution
.yres
,
4644 attr
->values
[i
].resolution
.units
==
4645 IPP_RES_PER_INCH
? "dpi" :
4646 attr
->values
[i
].resolution
.units
==
4647 IPP_RES_PER_CM
? "dpcm" : "unknown");
4650 if (attr
->values
[i
].resolution
.yres
<= 0)
4655 "\"%s\": Bad resolution value %dx%d%s - feed "
4656 "resolution must be positive "
4657 "(RFC 2911 section 4.1.13).", attr
->name
,
4658 attr
->values
[i
].resolution
.xres
,
4659 attr
->values
[i
].resolution
.yres
,
4660 attr
->values
[i
].resolution
.units
==
4661 IPP_RES_PER_INCH
? "dpi" :
4662 attr
->values
[i
].resolution
.units
==
4663 IPP_RES_PER_CM
? "dpcm" : "unknown");
4666 if (attr
->values
[i
].resolution
.units
!= IPP_RES_PER_INCH
&&
4667 attr
->values
[i
].resolution
.units
!= IPP_RES_PER_CM
)
4672 "\"%s\": Bad resolution value %dx%d%s - bad "
4673 "units value (RFC 2911 section 4.1.13).",
4674 attr
->name
, attr
->values
[i
].resolution
.xres
,
4675 attr
->values
[i
].resolution
.yres
,
4676 attr
->values
[i
].resolution
.units
==
4677 IPP_RES_PER_INCH
? "dpi" :
4678 attr
->values
[i
].resolution
.units
==
4679 IPP_RES_PER_CM
? "dpcm" : "unknown");
4684 case IPP_TAG_RANGE
:
4685 for (i
= 0; i
< attr
->num_values
; i
++)
4687 if (attr
->values
[i
].range
.lower
> attr
->values
[i
].range
.upper
)
4692 "\"%s\": Bad rangeOfInteger value %d-%d - lower "
4693 "greater than upper (RFC 2911 section 4.1.13).",
4694 attr
->name
, attr
->values
[i
].range
.lower
,
4695 attr
->values
[i
].range
.upper
);
4700 case IPP_TAG_BEGIN_COLLECTION
:
4701 for (i
= 0; i
< attr
->num_values
; i
++)
4703 for (colattr
= attr
->values
[i
].collection
->attrs
;
4705 colattr
= colattr
->next
)
4707 if (!validate_attr(NULL
, colattr
))
4714 if (colattr
&& errors
)
4716 add_stringf(errors
, "\"%s\": Bad collection value.", attr
->name
);
4720 validate_attr(errors
, colattr
);
4721 colattr
= colattr
->next
;
4728 case IPP_TAG_TEXTLANG
:
4729 for (i
= 0; i
< attr
->num_values
; i
++)
4731 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
4733 if ((*ptr
& 0xe0) == 0xc0)
4736 if ((*ptr
& 0xc0) != 0x80)
4739 else if ((*ptr
& 0xf0) == 0xe0)
4742 if ((*ptr
& 0xc0) != 0x80)
4745 if ((*ptr
& 0xc0) != 0x80)
4748 else if ((*ptr
& 0xf8) == 0xf0)
4751 if ((*ptr
& 0xc0) != 0x80)
4754 if ((*ptr
& 0xc0) != 0x80)
4757 if ((*ptr
& 0xc0) != 0x80)
4760 else if (*ptr
& 0x80)
4769 "\"%s\": Bad text value \"%s\" - bad UTF-8 "
4770 "sequence (RFC 2911 section 4.1.1).", attr
->name
,
4771 attr
->values
[i
].string
.text
);
4774 if ((ptr
- attr
->values
[i
].string
.text
) > 1023)
4779 "\"%s\": Bad text value \"%s\" - bad length %d "
4780 "(RFC 2911 section 4.1.1).", attr
->name
,
4781 attr
->values
[i
].string
.text
,
4782 (int)strlen(attr
->values
[i
].string
.text
));
4788 case IPP_TAG_NAMELANG
:
4789 for (i
= 0; i
< attr
->num_values
; i
++)
4791 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
4793 if ((*ptr
& 0xe0) == 0xc0)
4796 if ((*ptr
& 0xc0) != 0x80)
4799 else if ((*ptr
& 0xf0) == 0xe0)
4802 if ((*ptr
& 0xc0) != 0x80)
4805 if ((*ptr
& 0xc0) != 0x80)
4808 else if ((*ptr
& 0xf8) == 0xf0)
4811 if ((*ptr
& 0xc0) != 0x80)
4814 if ((*ptr
& 0xc0) != 0x80)
4817 if ((*ptr
& 0xc0) != 0x80)
4820 else if (*ptr
& 0x80)
4829 "\"%s\": Bad name value \"%s\" - bad UTF-8 "
4830 "sequence (RFC 2911 section 4.1.2).", attr
->name
,
4831 attr
->values
[i
].string
.text
);
4834 if ((ptr
- attr
->values
[i
].string
.text
) > 1023)
4839 "\"%s\": Bad name value \"%s\" - bad length %d "
4840 "(RFC 2911 section 4.1.2).", attr
->name
,
4841 attr
->values
[i
].string
.text
,
4842 (int)strlen(attr
->values
[i
].string
.text
));
4847 case IPP_TAG_KEYWORD
:
4848 for (i
= 0; i
< attr
->num_values
; i
++)
4850 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
4851 if (!isalnum(*ptr
& 255) && *ptr
!= '-' && *ptr
!= '.' &&
4855 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
4860 "\"%s\": Bad keyword value \"%s\" - invalid "
4861 "character (RFC 2911 section 4.1.3).",
4862 attr
->name
, attr
->values
[i
].string
.text
);
4865 if ((ptr
- attr
->values
[i
].string
.text
) > 255)
4870 "\"%s\": Bad keyword value \"%s\" - bad "
4871 "length %d (RFC 2911 section 4.1.3).",
4872 attr
->name
, attr
->values
[i
].string
.text
,
4873 (int)strlen(attr
->values
[i
].string
.text
));
4879 for (i
= 0; i
< attr
->num_values
; i
++)
4881 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
,
4882 attr
->values
[i
].string
.text
,
4883 scheme
, sizeof(scheme
),
4884 userpass
, sizeof(userpass
),
4885 hostname
, sizeof(hostname
),
4886 &port
, resource
, sizeof(resource
));
4888 if (uri_status
< HTTP_URI_OK
)
4893 "\"%s\": Bad URI value \"%s\" - %s "
4894 "(RFC 2911 section 4.1.5).", attr
->name
,
4895 attr
->values
[i
].string
.text
,
4896 URIStatusStrings
[uri_status
-
4897 HTTP_URI_OVERFLOW
]);
4900 if (strlen(attr
->values
[i
].string
.text
) > 1023)
4905 "\"%s\": Bad URI value \"%s\" - bad length %d "
4906 "(RFC 2911 section 4.1.5).", attr
->name
,
4907 attr
->values
[i
].string
.text
,
4908 (int)strlen(attr
->values
[i
].string
.text
));
4913 case IPP_TAG_URISCHEME
:
4914 for (i
= 0; i
< attr
->num_values
; i
++)
4916 ptr
= attr
->values
[i
].string
.text
;
4917 if (islower(*ptr
& 255))
4919 for (ptr
++; *ptr
; ptr
++)
4920 if (!islower(*ptr
& 255) && !isdigit(*ptr
& 255) &&
4921 *ptr
!= '+' && *ptr
!= '-' && *ptr
!= '.')
4925 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
4930 "\"%s\": Bad uriScheme value \"%s\" - bad "
4931 "characters (RFC 2911 section 4.1.6).",
4932 attr
->name
, attr
->values
[i
].string
.text
);
4935 if ((ptr
- attr
->values
[i
].string
.text
) > 63)
4940 "\"%s\": Bad uriScheme value \"%s\" - bad "
4941 "length %d (RFC 2911 section 4.1.6).",
4942 attr
->name
, attr
->values
[i
].string
.text
,
4943 (int)strlen(attr
->values
[i
].string
.text
));
4948 case IPP_TAG_CHARSET
:
4949 for (i
= 0; i
< attr
->num_values
; i
++)
4951 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
4952 if (!isprint(*ptr
& 255) || isupper(*ptr
& 255) ||
4953 isspace(*ptr
& 255))
4956 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
4961 "\"%s\": Bad charset value \"%s\" - bad "
4962 "characters (RFC 2911 section 4.1.7).",
4963 attr
->name
, attr
->values
[i
].string
.text
);
4966 if ((ptr
- attr
->values
[i
].string
.text
) > 40)
4971 "\"%s\": Bad charset value \"%s\" - bad "
4972 "length %d (RFC 2911 section 4.1.7).",
4973 attr
->name
, attr
->values
[i
].string
.text
,
4974 (int)strlen(attr
->values
[i
].string
.text
));
4979 case IPP_TAG_LANGUAGE
:
4981 * The following regular expression is derived from the ABNF for
4982 * language tags in RFC 4646. All I can say is that this is the
4983 * easiest way to check the values...
4986 if ((i
= regcomp(&re
,
4988 "(([a-z]{2,3}(-[a-z][a-z][a-z]){0,3})|[a-z]{4,8})"
4990 "(-[a-z][a-z][a-z][a-z]){0,1}" /* script */
4991 "(-([a-z][a-z]|[0-9][0-9][0-9])){0,1}" /* region */
4992 "(-([a-z]{5,8}|[0-9][0-9][0-9]))*" /* variant */
4993 "(-[a-wy-z](-[a-z0-9]{2,8})+)*" /* extension */
4994 "(-x(-[a-z0-9]{1,8})+)*" /* privateuse */
4996 "x(-[a-z0-9]{1,8})+" /* privateuse */
4998 "[a-z]{1,3}(-[a-z][0-9]{2,8}){1,2}" /* grandfathered */
5000 REG_NOSUB
| REG_EXTENDED
)) != 0)
5002 char temp
[256]; /* Temporary error string */
5004 regerror(i
, &re
, temp
, sizeof(temp
));
5005 print_fatal_error("Unable to compile naturalLanguage regular "
5006 "expression: %s.", temp
);
5010 for (i
= 0; i
< attr
->num_values
; i
++)
5012 if (regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
5017 "\"%s\": Bad naturalLanguage value \"%s\" - bad "
5018 "characters (RFC 2911 section 4.1.8).",
5019 attr
->name
, attr
->values
[i
].string
.text
);
5022 if (strlen(attr
->values
[i
].string
.text
) > 63)
5027 "\"%s\": Bad naturalLanguage value \"%s\" - bad "
5028 "length %d (RFC 2911 section 4.1.8).",
5029 attr
->name
, attr
->values
[i
].string
.text
,
5030 (int)strlen(attr
->values
[i
].string
.text
));
5037 case IPP_TAG_MIMETYPE
:
5039 * The following regular expression is derived from the ABNF for
5040 * language tags in RFC 2045 and 4288. All I can say is that this is
5041 * the easiest way to check the values...
5044 if ((i
= regcomp(&re
,
5046 "[-a-zA-Z0-9!#$&.+^_]{1,127}" /* type-name */
5048 "[-a-zA-Z0-9!#$&.+^_]{1,127}" /* subtype-name */
5049 "(;[-a-zA-Z0-9!#$&.+^_]{1,127}=" /* parameter= */
5050 "([-a-zA-Z0-9!#$&.+^_]{1,127}|\"[^\"]*\"))*"
5053 REG_NOSUB
| REG_EXTENDED
)) != 0)
5055 char temp
[256]; /* Temporary error string */
5057 regerror(i
, &re
, temp
, sizeof(temp
));
5058 print_fatal_error("Unable to compile mimeMediaType regular "
5059 "expression: %s.", temp
);
5063 for (i
= 0; i
< attr
->num_values
; i
++)
5065 if (regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
5070 "\"%s\": Bad mimeMediaType value \"%s\" - bad "
5071 "characters (RFC 2911 section 4.1.9).",
5072 attr
->name
, attr
->values
[i
].string
.text
);
5075 if (strlen(attr
->values
[i
].string
.text
) > 255)
5080 "\"%s\": Bad mimeMediaType value \"%s\" - bad "
5081 "length %d (RFC 2911 section 4.1.9).",
5082 attr
->name
, attr
->values
[i
].string
.text
,
5083 (int)strlen(attr
->values
[i
].string
.text
));
5099 * 'with_value()' - Test a WITH-VALUE predicate.
5102 static int /* O - 1 on match, 0 on non-match */
5103 with_value(cups_array_t
*errors
, /* I - Errors array */
5104 char *value
, /* I - Value string */
5105 int flags
, /* I - Flags for match */
5106 ipp_attribute_t
*attr
, /* I - Attribute to compare */
5107 char *matchbuf
, /* I - Buffer to hold matching value */
5108 size_t matchlen
) /* I - Length of match buffer */
5110 int i
, /* Looping var */
5112 char *valptr
; /* Pointer into value */
5116 match
= (flags
& _CUPS_WITH_ALL
) ? 1 : 0;
5119 * NULL matches everything.
5122 if (!value
|| !*value
)
5126 * Compare the value string to the attribute value.
5129 switch (attr
->value_tag
)
5131 case IPP_TAG_INTEGER
:
5133 for (i
= 0; i
< attr
->num_values
; i
++)
5135 char op
, /* Comparison operator */
5136 *nextptr
; /* Next pointer */
5137 int intvalue
, /* Integer value */
5138 valmatch
= 0; /* Does the current value match? */
5142 while (isspace(*valptr
& 255) || isdigit(*valptr
& 255) ||
5143 *valptr
== '-' || *valptr
== ',' || *valptr
== '<' ||
5144 *valptr
== '=' || *valptr
== '>')
5147 while (*valptr
&& !isdigit(*valptr
& 255) && *valptr
!= '-')
5149 if (*valptr
== '<' || *valptr
== '>' || *valptr
== '=')
5157 intvalue
= strtol(valptr
, &nextptr
, 0);
5158 if (nextptr
== valptr
)
5162 if ((op
== '=' && attr
->values
[i
].integer
== intvalue
) ||
5163 (op
== '<' && attr
->values
[i
].integer
< intvalue
) ||
5164 (op
== '>' && attr
->values
[i
].integer
> intvalue
))
5167 snprintf(matchbuf
, matchlen
, "%d",
5168 attr
->values
[i
].integer
);
5175 if (flags
& _CUPS_WITH_ALL
)
5190 if (!match
&& errors
)
5192 for (i
= 0; i
< attr
->num_values
; i
++)
5193 add_stringf(errors
, "GOT: %s=%d", attr
->name
,
5194 attr
->values
[i
].integer
);
5198 case IPP_TAG_RANGE
:
5199 for (i
= 0; i
< attr
->num_values
; i
++)
5201 char op
, /* Comparison operator */
5202 *nextptr
; /* Next pointer */
5203 int intvalue
, /* Integer value */
5204 valmatch
= 0; /* Does the current value match? */
5208 while (isspace(*valptr
& 255) || isdigit(*valptr
& 255) ||
5209 *valptr
== '-' || *valptr
== ',' || *valptr
== '<' ||
5210 *valptr
== '=' || *valptr
== '>')
5213 while (*valptr
&& !isdigit(*valptr
& 255) && *valptr
!= '-')
5215 if (*valptr
== '<' || *valptr
== '>' || *valptr
== '=')
5223 intvalue
= strtol(valptr
, &nextptr
, 0);
5224 if (nextptr
== valptr
)
5228 if ((op
== '=' && (attr
->values
[i
].range
.lower
== intvalue
||
5229 attr
->values
[i
].range
.upper
== intvalue
)) ||
5230 (op
== '<' && attr
->values
[i
].range
.upper
< intvalue
) ||
5231 (op
== '>' && attr
->values
[i
].range
.upper
> intvalue
))
5234 snprintf(matchbuf
, matchlen
, "%d-%d",
5235 attr
->values
[0].range
.lower
,
5236 attr
->values
[0].range
.upper
);
5243 if (flags
& _CUPS_WITH_ALL
)
5258 if (!match
&& errors
)
5260 for (i
= 0; i
< attr
->num_values
; i
++)
5261 add_stringf(errors
, "GOT: %s=%d-%d", attr
->name
,
5262 attr
->values
[i
].range
.lower
,
5263 attr
->values
[i
].range
.upper
);
5267 case IPP_TAG_BOOLEAN
:
5268 for (i
= 0; i
< attr
->num_values
; i
++)
5270 if (!strcmp(value
, "true") == attr
->values
[i
].boolean
)
5273 strlcpy(matchbuf
, value
, matchlen
);
5275 if (!(flags
& _CUPS_WITH_ALL
))
5281 else if (flags
& _CUPS_WITH_ALL
)
5288 if (!match
&& errors
)
5290 for (i
= 0; i
< attr
->num_values
; i
++)
5291 add_stringf(errors
, "GOT: %s=%s", attr
->name
,
5292 attr
->values
[i
].boolean
? "true" : "false");
5296 case IPP_TAG_NOVALUE
:
5297 case IPP_TAG_UNKNOWN
:
5300 case IPP_TAG_CHARSET
:
5301 case IPP_TAG_KEYWORD
:
5302 case IPP_TAG_LANGUAGE
:
5303 case IPP_TAG_MIMETYPE
:
5305 case IPP_TAG_NAMELANG
:
5307 case IPP_TAG_TEXTLANG
:
5309 case IPP_TAG_URISCHEME
:
5310 if (flags
& _CUPS_WITH_REGEX
)
5313 * Value is an extended, case-sensitive POSIX regular expression...
5316 regex_t re
; /* Regular expression */
5318 if ((i
= regcomp(&re
, value
, REG_EXTENDED
| REG_NOSUB
)) != 0)
5320 char temp
[256]; /* Temporary string */
5322 regerror(i
, &re
, temp
, sizeof(temp
));
5324 print_fatal_error("Unable to compile WITH-VALUE regular expression "
5325 "\"%s\" - %s", value
, temp
);
5330 * See if ALL of the values match the given regular expression.
5333 for (i
= 0; i
< attr
->num_values
; i
++)
5335 if (!regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
5338 strlcpy(matchbuf
, attr
->values
[i
].string
.text
, matchlen
);
5340 if (!(flags
& _CUPS_WITH_ALL
))
5346 else if (flags
& _CUPS_WITH_ALL
)
5358 * Value is a literal string, see if the value(s) match...
5361 for (i
= 0; i
< attr
->num_values
; i
++)
5363 if (!strcmp(value
, attr
->values
[i
].string
.text
))
5366 strlcpy(matchbuf
, attr
->values
[i
].string
.text
, matchlen
);
5368 if (!(flags
& _CUPS_WITH_ALL
))
5374 else if (flags
& _CUPS_WITH_ALL
)
5382 if (!match
&& errors
)
5384 for (i
= 0; i
< attr
->num_values
; i
++)
5385 add_stringf(errors
, "GOT: %s=\"%s\"", attr
->name
,
5386 attr
->values
[i
].string
.text
);