4 * ipptool command for CUPS.
6 * Copyright 2007-2011 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 * compare_vars() - Compare two variables.
21 * do_tests() - Do tests as specified in the test file.
22 * expand_variables() - Expand variables in a string.
23 * expect_matches() - Return true if the tag matches the specification.
24 * get_collection() - Get a collection value from the current test file.
25 * get_filename() - Get a filename based on the current test file.
26 * get_token() - Get a token from a file.
27 * get_variable() - Get the value of a variable.
28 * iso_date() - Return an ISO 8601 date/time string for the given IPP
30 * password_cb() - Password callback for authenticated tests.
31 * print_attr() - Print an attribute on the screen.
32 * print_col() - Print a collection attribute on the screen.
33 * print_csv() - Print a line of CSV text.
34 * print_fatal_error() - Print a fatal error message.
35 * print_line() - Print a line of formatted or CSV text.
36 * print_test_error() - Print a test error message.
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 struct _cups_expect_s
/**** Expected attribute info ****/
86 int optional
, /* Optional attribute? */
87 not_expect
; /* Don't expect attribute? */
88 char *name
, /* Attribute name */
89 *of_type
, /* Type name */
90 *same_count_as
, /* Parallel attribute name */
91 *if_defined
, /* Only required if variable defined */
92 *if_not_defined
, /* Only required if variable is not defined */
93 *with_value
, /* Attribute must include this value */
94 *define_match
, /* Variable to define on match */
95 *define_no_match
, /* Variable to define on no-match */
96 *define_value
; /* Variable to define with value */
97 int repeat_match
, /* Repeat test on match */
98 repeat_no_match
, /* Repeat test on no match */
99 with_regex
, /* WITH-VALUE is a regular expression */
100 count
; /* Expected count if > 0 */
101 ipp_tag_t in_group
; /* IN-GROUP value */
104 typedef struct _cups_status_s
/**** Status info ****/
106 ipp_status_t status
; /* Expected status code */
107 char *if_defined
, /* Only if variable is defined */
108 *if_not_defined
; /* Only if variable is not defined */
109 int repeat_match
, /* Repeat the test when it does not match */
110 repeat_no_match
; /* Repeat the test when it matches */
113 typedef struct _cups_var_s
/**** Variable ****/
115 char *name
, /* Name of variable */
116 *value
; /* Value of variable */
119 typedef struct _cups_vars_s
/**** Set of variables ****/
121 char *uri
, /* URI for printer */
122 *filename
, /* Filename */
123 scheme
[64], /* Scheme from URI */
124 userpass
[256], /* Username/password from URI */
125 hostname
[256], /* Hostname from URI */
126 resource
[1024]; /* Resource path from URI */
127 int port
; /* Port number from URI */
128 http_encryption_t encryption
; /* Encryption for connection? */
129 double timeout
; /* Timeout for connection */
130 int family
; /* Address family */
131 cups_array_t
*vars
; /* Array of variables */
139 _cups_transfer_t Transfer
= _CUPS_TRANSFER_AUTO
;
140 /* How to transfer requests */
141 _cups_output_t Output
= _CUPS_OUTPUT_LIST
;
143 int Cancel
= 0, /* Cancel test? */
144 IgnoreErrors
= 0, /* Ignore errors? */
145 Verbosity
= 0, /* Show all attributes? */
146 Version
= 11, /* Default IPP version */
147 XMLHeader
= 0, /* 1 if header is written */
148 TestCount
= 0, /* Number of tests run */
149 PassCount
= 0, /* Number of passing tests */
150 FailCount
= 0, /* Number of failing tests */
151 SkipCount
= 0; /* Number of skipped tests */
152 char *Password
= NULL
; /* Password from URI */
153 const char * const URIStatusStrings
[] = /* URI status strings */
156 "Bad arguments to function",
157 "Bad resource in URI",
158 "Bad port number in URI",
159 "Bad hostname/address in URI",
160 "Bad username in URI",
164 "Missing scheme in URI",
165 "Unknown scheme in URI",
166 "Missing resource in URI"
174 static int compare_vars(_cups_var_t
*a
, _cups_var_t
*b
);
175 static int do_tests(_cups_vars_t
*vars
, const char *testfile
);
176 static void expand_variables(_cups_vars_t
*vars
, char *dst
, const char *src
,
177 size_t dstsize
) __attribute__((nonnull(1,2,3)));
178 static int expect_matches(_cups_expect_t
*expect
, ipp_tag_t value_tag
);
179 static ipp_t
*get_collection(_cups_vars_t
*vars
, FILE *fp
, int *linenum
);
180 static char *get_filename(const char *testfile
, char *dst
, const char *src
,
182 static char *get_token(FILE *fp
, char *buf
, int buflen
,
184 static char *get_variable(_cups_vars_t
*vars
, const char *name
);
185 static char *iso_date(ipp_uchar_t
*date
);
186 static const char *password_cb(const char *prompt
);
187 static void print_attr(ipp_attribute_t
*attr
, ipp_tag_t
*group
);
188 static void print_col(ipp_t
*col
);
189 static void print_csv(ipp_attribute_t
*attr
, int num_displayed
,
190 char **displayed
, size_t *widths
);
191 static void print_fatal_error(const char *s
, ...)
192 __attribute__ ((__format__ (__printf__
, 1, 2)));
193 static void print_line(ipp_attribute_t
*attr
, int num_displayed
,
194 char **displayed
, size_t *widths
);
195 static void print_test_error(const char *s
, ...)
196 __attribute__ ((__format__ (__printf__
, 1, 2)));
197 static void print_xml_header(void);
198 static void print_xml_string(const char *element
, const char *s
);
199 static void print_xml_trailer(int success
, const char *message
);
200 static void set_variable(_cups_vars_t
*vars
, const char *name
,
203 static void sigterm_handler(int sig
);
205 static int timeout_cb(http_t
*http
, void *user_data
);
206 static void usage(void) __attribute__((noreturn
));
207 static int validate_attr(ipp_attribute_t
*attr
, int print
);
208 static int with_value(char *value
, int regex
, ipp_attribute_t
*attr
,
209 int report
, char *matchbuf
, size_t matchlen
);
213 * 'main()' - Parse options and do tests.
216 int /* O - Exit status */
217 main(int argc
, /* I - Number of command-line args */
218 char *argv
[]) /* I - Command-line arguments */
220 int i
; /* Looping var */
221 int status
; /* Status of tests... */
222 char *opt
, /* Current option */
223 name
[1024], /* Name/value buffer */
224 *value
, /* Pointer to value */
225 filename
[1024], /* Real filename */
226 testname
[1024], /* Real test filename */
227 uri
[1024]; /* Copy of printer URI */
228 const char *ext
, /* Extension on filename */
229 *testfile
; /* Test file to use */
230 int interval
, /* Test interval in microseconds */
231 repeat
; /* Repeat count */
232 _cups_vars_t vars
; /* Variables */
233 http_uri_status_t uri_status
; /* URI separation status */
234 _cups_globals_t
*cg
= _cupsGlobals();
240 * Catch SIGINT and SIGTERM...
243 signal(SIGINT
, sigterm_handler
);
244 signal(SIGTERM
, sigterm_handler
);
248 * Initialize the locale and variables...
251 _cupsSetLocale(argv
);
253 memset(&vars
, 0, sizeof(vars
));
254 vars
.family
= AF_UNSPEC
;
255 vars
.vars
= cupsArrayNew((cups_array_func_t
)compare_vars
, NULL
);
260 * ipptool URI testfile
268 for (i
= 1; i
< argc
; i
++)
270 if (argv
[i
][0] == '-')
272 for (opt
= argv
[i
] + 1; *opt
; opt
++)
276 case '4' : /* Connect using IPv4 only */
277 vars
.family
= AF_INET
;
281 case '6' : /* Connect using IPv6 only */
282 vars
.family
= AF_INET6
;
284 #endif /* AF_INET6 */
286 case 'C' : /* Enable HTTP chunking */
287 Transfer
= _CUPS_TRANSFER_CHUNKED
;
290 case 'E' : /* Encrypt with TLS */
292 vars
.encryption
= HTTP_ENCRYPT_REQUIRED
;
294 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."),
296 #endif /* HAVE_SSL */
299 case 'I' : /* Ignore errors */
303 case 'L' : /* Disable HTTP chunking */
304 Transfer
= _CUPS_TRANSFER_LENGTH
;
307 case 'S' : /* Encrypt with SSL */
309 vars
.encryption
= HTTP_ENCRYPT_ALWAYS
;
311 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."),
313 #endif /* HAVE_SSL */
316 case 'T' : /* Set timeout */
321 _cupsLangPuts(stderr
,
322 _("ipptool: Missing timeout for \"-T\"."));
326 vars
.timeout
= _cupsStrScand(argv
[i
], NULL
, localeconv());
329 case 'V' : /* Set IPP version */
334 _cupsLangPuts(stderr
,
335 _("ipptool: Missing version for \"-V\"."));
339 if (!strcmp(argv
[i
], "1.0"))
341 else if (!strcmp(argv
[i
], "1.1"))
343 else if (!strcmp(argv
[i
], "2.0"))
345 else if (!strcmp(argv
[i
], "2.1"))
347 else if (!strcmp(argv
[i
], "2.2"))
351 _cupsLangPrintf(stderr
,
352 _("ipptool: Bad version %s for \"-V\"."),
358 case 'X' : /* Produce XML output */
359 Output
= _CUPS_OUTPUT_PLIST
;
361 if (interval
|| repeat
)
363 _cupsLangPuts(stderr
, _("ipptool: \"-i\" and \"-n\" are "
364 "incompatible with -X\"."));
369 case 'c' : /* CSV output */
370 Output
= _CUPS_OUTPUT_CSV
;
373 case 'd' : /* Define a variable */
378 _cupsLangPuts(stderr
,
379 _("ipptool: Missing name=value for \"-d\"."));
383 strlcpy(name
, argv
[i
], sizeof(name
));
384 if ((value
= strchr(name
, '=')) != NULL
)
387 value
= name
+ strlen(name
);
389 set_variable(&vars
, name
, value
);
392 case 'f' : /* Set the default test filename */
397 _cupsLangPuts(stderr
,
398 _("ipptool: Missing filename for \"-f\"."));
405 if (access(argv
[i
], 0))
411 snprintf(filename
, sizeof(filename
), "%s.gz", argv
[i
]);
412 if (access(filename
, 0) && filename
[0] != '/')
414 snprintf(filename
, sizeof(filename
), "%s/ipptool/%s",
415 cg
->cups_datadir
, argv
[i
]);
416 if (access(filename
, 0))
418 snprintf(filename
, sizeof(filename
), "%s/ipptool/%s.gz",
419 cg
->cups_datadir
, argv
[i
]);
420 if (access(filename
, 0))
421 vars
.filename
= strdup(argv
[i
]);
424 vars
.filename
= strdup(filename
);
427 vars
.filename
= strdup(filename
);
430 vars
.filename
= strdup(argv
[i
]);
432 if ((ext
= strrchr(vars
.filename
, '.')) != NULL
)
435 * Guess the MIME media type based on the extension...
438 if (!_cups_strcasecmp(ext
, ".gif"))
439 set_variable(&vars
, "filetype", "image/gif");
440 else if (!_cups_strcasecmp(ext
, ".htm") ||
441 !_cups_strcasecmp(ext
, ".htm.gz") ||
442 !_cups_strcasecmp(ext
, ".html") ||
443 !_cups_strcasecmp(ext
, ".html.gz"))
444 set_variable(&vars
, "filetype", "text/html");
445 else if (!_cups_strcasecmp(ext
, ".jpg"))
446 set_variable(&vars
, "filetype", "image/jpeg");
447 else if (!_cups_strcasecmp(ext
, ".pdf"))
448 set_variable(&vars
, "filetype", "application/pdf");
449 else if (!_cups_strcasecmp(ext
, ".png"))
450 set_variable(&vars
, "filetype", "image/png");
451 else if (!_cups_strcasecmp(ext
, ".ps") ||
452 !_cups_strcasecmp(ext
, ".ps.gz"))
453 set_variable(&vars
, "filetype", "application/postscript");
454 else if (!_cups_strcasecmp(ext
, ".ras") ||
455 !_cups_strcasecmp(ext
, ".ras.gz"))
456 set_variable(&vars
, "filetype", "image/pwg-raster");
457 else if (!_cups_strcasecmp(ext
, ".txt") ||
458 !_cups_strcasecmp(ext
, ".txt.gz"))
459 set_variable(&vars
, "filetype", "text/plain");
460 else if (!_cups_strcasecmp(ext
, ".xps"))
461 set_variable(&vars
, "filetype", "application/openxps");
463 set_variable(&vars
, "filetype", "application/octet-stream");
468 * Use the "auto-type" MIME media type...
471 set_variable(&vars
, "filetype", "application/octet-stream");
475 case 'i' : /* Test every N seconds */
480 _cupsLangPuts(stderr
,
481 _("ipptool: Missing seconds for \"-i\"."));
486 interval
= (int)(_cupsStrScand(argv
[i
], NULL
, localeconv()) *
490 _cupsLangPuts(stderr
,
491 _("ipptool: Invalid seconds for \"-i\"."));
496 if (Output
== _CUPS_OUTPUT_PLIST
&& interval
)
498 _cupsLangPuts(stderr
, _("ipptool: \"-i\" is incompatible with "
504 case 'l' : /* List as a table */
505 Output
= _CUPS_OUTPUT_LIST
;
508 case 'n' : /* Repeat count */
513 _cupsLangPuts(stderr
,
514 _("ipptool: Missing count for \"-n\"."));
518 repeat
= atoi(argv
[i
]);
520 if (Output
== _CUPS_OUTPUT_PLIST
&& repeat
)
522 _cupsLangPuts(stderr
, _("ipptool: \"-n\" is incompatible with "
528 case 'q' : /* Be quiet */
529 Output
= _CUPS_OUTPUT_QUIET
;
532 case 't' : /* CUPS test output */
533 Output
= _CUPS_OUTPUT_TEST
;
536 case 'v' : /* Be verbose */
541 _cupsLangPrintf(stderr
, _("ipptool: Unknown option \"-%c\"."),
548 else if (!strncmp(argv
[i
], "ipp://", 6) || !strncmp(argv
[i
], "http://", 7)
550 || !strncmp(argv
[i
], "ipps://", 7)
551 || !strncmp(argv
[i
], "https://", 8)
552 #endif /* HAVE_SSL */
561 _cupsLangPuts(stderr
, _("ipptool: May only specify a single URI."));
566 if (!strncmp(argv
[i
], "ipps://", 7) || !strncmp(argv
[i
], "https://", 8))
567 vars
.encryption
= HTTP_ENCRYPT_ALWAYS
;
568 #endif /* HAVE_SSL */
571 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
, vars
.uri
,
572 vars
.scheme
, sizeof(vars
.scheme
),
573 vars
.userpass
, sizeof(vars
.userpass
),
574 vars
.hostname
, sizeof(vars
.hostname
),
576 vars
.resource
, sizeof(vars
.resource
));
578 if (uri_status
!= HTTP_URI_OK
)
580 _cupsLangPrintf(stderr
, _("ipptool: Bad URI - %s."),
581 URIStatusStrings
[uri_status
- HTTP_URI_OVERFLOW
]);
585 if (vars
.userpass
[0])
587 if ((Password
= strchr(vars
.userpass
, ':')) != NULL
)
590 cupsSetUser(vars
.userpass
);
591 cupsSetPasswordCB(password_cb
);
592 set_variable(&vars
, "uriuser", vars
.userpass
);
595 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), vars
.scheme
, NULL
,
596 vars
.hostname
, vars
.port
, vars
.resource
);
607 _cupsLangPuts(stderr
, _("ipptool: URI required before test file."));
611 if (access(argv
[i
], 0) && argv
[i
][0] != '/')
613 snprintf(testname
, sizeof(testname
), "%s/ipptool/%s", cg
->cups_datadir
,
615 if (access(testname
, 0))
623 if (!do_tests(&vars
, testfile
))
628 if (!vars
.uri
|| !testfile
)
632 * Loop if the interval is set...
635 if (Output
== _CUPS_OUTPUT_PLIST
)
636 print_xml_trailer(!status
, NULL
);
637 else if (interval
> 0 && repeat
> 0)
642 do_tests(&vars
, testfile
);
646 else if (interval
> 0)
651 do_tests(&vars
, testfile
);
654 else if (Output
== _CUPS_OUTPUT_TEST
&& TestCount
> 1)
657 * Show a summary report if there were multiple tests...
660 printf("\nSummary: %d tests, %d passed, %d failed, %d skipped\n"
661 "Score: %d%%\n", TestCount
, PassCount
, FailCount
, SkipCount
,
662 100 * (PassCount
+ SkipCount
) / TestCount
);
674 * 'compare_vars()' - Compare two variables.
677 static int /* O - Result of comparison */
678 compare_vars(_cups_var_t
*a
, /* I - First variable */
679 _cups_var_t
*b
) /* I - Second variable */
681 return (_cups_strcasecmp(a
->name
, b
->name
));
686 * 'do_tests()' - Do tests as specified in the test file.
689 static int /* 1 = success, 0 = failure */
690 do_tests(_cups_vars_t
*vars
, /* I - Variables */
691 const char *testfile
) /* I - Test file to use */
693 int i
, /* Looping var */
694 linenum
, /* Current line number */
695 pass
, /* Did we pass the test? */
696 prev_pass
= 1, /* Did we pass the previous test? */
697 request_id
, /* Current request ID */
698 show_header
= 1, /* Show the test header? */
699 ignore_errors
, /* Ignore test failures? */
700 skip_previous
= 0, /* Skip on previous test failure? */
701 repeat_test
; /* Repeat a test? */
702 http_t
*http
= NULL
; /* HTTP connection to server */
703 FILE *fp
= NULL
; /* Test file */
704 char resource
[512], /* Resource for request */
705 token
[1024], /* Token from file */
706 *tokenptr
, /* Pointer into token */
707 temp
[1024], /* Temporary string */
708 buffer
[8192]; /* Copy buffer */
709 ipp_t
*request
= NULL
, /* IPP request */
710 *response
= NULL
; /* IPP response */
711 size_t length
; /* Length of IPP request */
712 http_status_t status
; /* HTTP status */
713 cups_file_t
*reqfile
; /* File to send */
714 ssize_t bytes
; /* Bytes read/written */
715 char attr
[128]; /* Attribute name */
716 ipp_op_t op
; /* Operation */
717 ipp_tag_t group
; /* Current group */
718 ipp_tag_t value
; /* Current value type */
719 ipp_attribute_t
*attrptr
, /* Attribute pointer */
720 *found
, /* Found attribute */
721 *lastcol
= NULL
; /* Last collection attribute */
722 char name
[1024]; /* Name of test */
723 char filename
[1024]; /* Filename */
724 _cups_transfer_t transfer
; /* To chunk or not to chunk */
725 int version
, /* IPP version number to use */
726 skip_test
; /* Skip this test? */
727 int num_statuses
= 0; /* Number of valid status codes */
728 _cups_status_t statuses
[100], /* Valid status codes */
729 *last_status
; /* Last STATUS (for predicates) */
730 int num_expects
= 0; /* Number of expected attributes */
731 _cups_expect_t expects
[200], /* Expected attributes */
732 *expect
, /* Current expected attribute */
733 *last_expect
; /* Last EXPECT (for predicates) */
734 int num_displayed
= 0; /* Number of displayed attributes */
735 char *displayed
[200]; /* Displayed attributes */
736 size_t widths
[200]; /* Width of columns */
740 * Open the test file...
743 if ((fp
= fopen(testfile
, "r")) == NULL
)
745 print_fatal_error("Unable to open test file %s - %s", testfile
,
752 * Connect to the server...
755 if ((http
= _httpCreate(vars
->hostname
, vars
->port
, NULL
, vars
->encryption
,
756 vars
->family
)) == NULL
)
758 print_fatal_error("Unable to connect to %s on port %d - %s", vars
->hostname
,
759 vars
->port
, cupsLastErrorString());
764 if (httpReconnect(http
))
766 print_fatal_error("Unable to connect to %s on port %d - %s", vars
->hostname
,
767 vars
->port
, cupsLastErrorString());
772 if (vars
->timeout
> 0.0)
773 httpSetTimeout(http
, vars
->timeout
, timeout_cb
, NULL
);
779 CUPS_SRAND(time(NULL
));
783 request_id
= (CUPS_RAND() % 1000) * 137 + 1;
785 while (!Cancel
&& get_token(fp
, token
, sizeof(token
), &linenum
) != NULL
)
788 * Expect an open brace...
791 if (!strcmp(token
, "DEFINE"))
797 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
798 get_token(fp
, temp
, sizeof(temp
), &linenum
))
800 expand_variables(vars
, token
, temp
, sizeof(token
));
801 set_variable(vars
, attr
, token
);
805 print_fatal_error("Missing DEFINE name and/or value on line %d.",
813 else if (!strcmp(token
, "DEFINE-DEFAULT"))
816 * DEFINE-DEFAULT name value
819 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
820 get_token(fp
, temp
, sizeof(temp
), &linenum
))
822 expand_variables(vars
, token
, temp
, sizeof(token
));
823 if (!get_variable(vars
, attr
))
824 set_variable(vars
, attr
, token
);
828 print_fatal_error("Missing DEFINE-DEFAULT name and/or value on line "
836 else if (!strcmp(token
, "IGNORE-ERRORS"))
843 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
844 (!_cups_strcasecmp(temp
, "yes") || !_cups_strcasecmp(temp
, "no")))
846 IgnoreErrors
= !_cups_strcasecmp(temp
, "yes");
850 print_fatal_error("Missing IGNORE-ERRORS value on line %d.", linenum
);
857 else if (!strcmp(token
, "INCLUDE"))
864 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
867 * Map the filename to and then run the tests...
870 if (!do_tests(vars
, get_filename(testfile
, filename
, temp
,
881 print_fatal_error("Missing INCLUDE filename on line %d.", linenum
);
889 else if (!strcmp(token
, "INCLUDE-IF-DEFINED"))
892 * INCLUDE-IF-DEFINED name "filename"
893 * INCLUDE-IF-DEFINED name <filename>
896 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
897 get_token(fp
, temp
, sizeof(temp
), &linenum
))
900 * Map the filename to and then run the tests...
903 if (get_variable(vars
, attr
) &&
904 !do_tests(vars
, get_filename(testfile
, filename
, temp
,
915 print_fatal_error("Missing INCLUDE-IF-DEFINED name or filename on line "
924 else if (!strcmp(token
, "INCLUDE-IF-NOT-DEFINED"))
927 * INCLUDE-IF-NOT-DEFINED name "filename"
928 * INCLUDE-IF-NOT-DEFINED name <filename>
931 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
932 get_token(fp
, temp
, sizeof(temp
), &linenum
))
935 * Map the filename to and then run the tests...
938 if (!get_variable(vars
, attr
) &&
939 !do_tests(vars
, get_filename(testfile
, filename
, temp
,
950 print_fatal_error("Missing INCLUDE-IF-NOT-DEFINED name or filename on "
951 "line %d.", linenum
);
959 else if (!strcmp(token
, "SKIP-IF-DEFINED"))
962 * SKIP-IF-DEFINED variable
965 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
967 if (get_variable(vars
, temp
))
972 print_fatal_error("Missing SKIP-IF-DEFINED variable on line %d.",
978 else if (!strcmp(token
, "SKIP-IF-NOT-DEFINED"))
981 * SKIP-IF-NOT-DEFINED variable
984 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
986 if (!get_variable(vars
, temp
))
991 print_fatal_error("Missing SKIP-IF-NOT-DEFINED variable on line %d.",
997 else if (!strcmp(token
, "TRANSFER"))
1005 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1007 if (!strcmp(temp
, "auto"))
1008 Transfer
= _CUPS_TRANSFER_AUTO
;
1009 else if (!strcmp(temp
, "chunked"))
1010 Transfer
= _CUPS_TRANSFER_CHUNKED
;
1011 else if (!strcmp(temp
, "length"))
1012 Transfer
= _CUPS_TRANSFER_LENGTH
;
1015 print_fatal_error("Bad TRANSFER value \"%s\" on line %d.", temp
,
1023 print_fatal_error("Missing TRANSFER value on line %d.", linenum
);
1030 else if (!strcmp(token
, "VERSION"))
1032 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1034 if (!strcmp(temp
, "1.0"))
1036 else if (!strcmp(temp
, "1.1"))
1038 else if (!strcmp(temp
, "2.0"))
1040 else if (!strcmp(temp
, "2.1"))
1042 else if (!strcmp(temp
, "2.2"))
1046 print_fatal_error("Bad VERSION \"%s\" on line %d.", temp
, linenum
);
1053 print_fatal_error("Missing VERSION number on line %d.", linenum
);
1060 else if (strcmp(token
, "{"))
1062 print_fatal_error("Unexpected token %s seen on line %d.", token
, linenum
);
1068 * Initialize things...
1073 if (Output
== _CUPS_OUTPUT_PLIST
)
1075 else if (Output
== _CUPS_OUTPUT_TEST
)
1076 printf("\"%s\":\n", testfile
);
1081 strlcpy(resource
, vars
->resource
, sizeof(resource
));
1086 group
= IPP_TAG_ZERO
;
1087 ignore_errors
= IgnoreErrors
;
1094 transfer
= Transfer
;
1096 strlcpy(name
, testfile
, sizeof(name
));
1097 if (strrchr(name
, '.') != NULL
)
1098 *strrchr(name
, '.') = '\0';
1101 * Parse until we see a close brace...
1104 while (get_token(fp
, token
, sizeof(token
), &linenum
) != NULL
)
1106 if (_cups_strcasecmp(token
, "COUNT") &&
1107 _cups_strcasecmp(token
, "DEFINE-MATCH") &&
1108 _cups_strcasecmp(token
, "DEFINE-NO-MATCH") &&
1109 _cups_strcasecmp(token
, "DEFINE-VALUE") &&
1110 _cups_strcasecmp(token
, "IF-DEFINED") &&
1111 _cups_strcasecmp(token
, "IF-NOT-DEFINED") &&
1112 _cups_strcasecmp(token
, "IN-GROUP") &&
1113 _cups_strcasecmp(token
, "OF-TYPE") &&
1114 _cups_strcasecmp(token
, "REPEAT-MATCH") &&
1115 _cups_strcasecmp(token
, "REPEAT-NO-MATCH") &&
1116 _cups_strcasecmp(token
, "SAME-COUNT-AS") &&
1117 _cups_strcasecmp(token
, "WITH-VALUE"))
1120 if (_cups_strcasecmp(token
, "IF-DEFINED") &&
1121 _cups_strcasecmp(token
, "IF-NOT-DEFINED") &&
1122 _cups_strcasecmp(token
, "REPEAT-MATCH") &&
1123 _cups_strcasecmp(token
, "REPEAT-NO-MATCH"))
1126 if (!strcmp(token
, "}"))
1128 else if (!strcmp(token
, "{") && lastcol
)
1131 * Another collection value
1134 ipp_t
*col
= get_collection(vars
, fp
, &linenum
);
1135 /* Collection value */
1139 ipp_attribute_t
*tempcol
; /* Pointer to new buffer */
1143 * Reallocate memory...
1146 if ((tempcol
= realloc(lastcol
, sizeof(ipp_attribute_t
) +
1147 (lastcol
->num_values
+ 1) *
1148 sizeof(ipp_value_t
))) == NULL
)
1150 print_fatal_error("Unable to allocate memory on line %d.", linenum
);
1155 if (tempcol
!= lastcol
)
1158 * Reset pointers in the list...
1162 request
->prev
->next
= tempcol
;
1164 request
->attrs
= tempcol
;
1166 lastcol
= request
->current
= request
->last
= tempcol
;
1169 lastcol
->values
[lastcol
->num_values
].collection
= col
;
1170 lastcol
->num_values
++;
1178 else if (!strcmp(token
, "DEFINE"))
1184 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
1185 get_token(fp
, temp
, sizeof(temp
), &linenum
))
1187 expand_variables(vars
, token
, temp
, sizeof(token
));
1188 set_variable(vars
, attr
, token
);
1192 print_fatal_error("Missing DEFINE name and/or value on line %d.",
1198 else if (!strcmp(token
, "IGNORE-ERRORS"))
1205 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
1206 (!_cups_strcasecmp(temp
, "yes") || !_cups_strcasecmp(temp
, "no")))
1208 ignore_errors
= !_cups_strcasecmp(temp
, "yes");
1212 print_fatal_error("Missing IGNORE-ERRORS value on line %d.", linenum
);
1219 else if (!_cups_strcasecmp(token
, "NAME"))
1225 get_token(fp
, name
, sizeof(name
), &linenum
);
1227 else if (!strcmp(token
, "REQUEST-ID"))
1234 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1236 if (isdigit(temp
[0] & 255))
1237 request_id
= atoi(temp
);
1238 else if (!_cups_strcasecmp(temp
, "random"))
1239 request_id
= (CUPS_RAND() % 1000) * 137 + 1;
1242 print_fatal_error("Bad REQUEST-ID value \"%s\" on line %d.", temp
,
1250 print_fatal_error("Missing REQUEST-ID value on line %d.", linenum
);
1255 else if (!strcmp(token
, "SKIP-IF-DEFINED"))
1258 * SKIP-IF-DEFINED variable
1261 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1263 if (get_variable(vars
, temp
))
1268 print_fatal_error("Missing SKIP-IF-DEFINED value on line %d.",
1274 else if (!strcmp(token
, "SKIP-IF-NOT-DEFINED"))
1277 * SKIP-IF-NOT-DEFINED variable
1280 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1282 if (!get_variable(vars
, temp
))
1287 print_fatal_error("Missing SKIP-IF-NOT-DEFINED value on line %d.",
1293 else if (!strcmp(token
, "SKIP-PREVIOUS-ERROR"))
1296 * SKIP-PREVIOUS-ERROR yes
1297 * SKIP-PREVIOUS-ERROR no
1300 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
1301 (!_cups_strcasecmp(temp
, "yes") || !_cups_strcasecmp(temp
, "no")))
1303 skip_previous
= !_cups_strcasecmp(temp
, "yes");
1307 print_fatal_error("Missing SKIP-PREVIOUS-ERROR value on line %d.", linenum
);
1314 else if (!strcmp(token
, "TRANSFER"))
1322 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1324 if (!strcmp(temp
, "auto"))
1325 transfer
= _CUPS_TRANSFER_AUTO
;
1326 else if (!strcmp(temp
, "chunked"))
1327 transfer
= _CUPS_TRANSFER_CHUNKED
;
1328 else if (!strcmp(temp
, "length"))
1329 transfer
= _CUPS_TRANSFER_LENGTH
;
1332 print_fatal_error("Bad TRANSFER value \"%s\" on line %d.", temp
,
1340 print_fatal_error("Missing TRANSFER value on line %d.", linenum
);
1345 else if (!_cups_strcasecmp(token
, "VERSION"))
1347 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1349 if (!strcmp(temp
, "0.0"))
1351 else if (!strcmp(temp
, "1.0"))
1353 else if (!strcmp(temp
, "1.1"))
1355 else if (!strcmp(temp
, "2.0"))
1357 else if (!strcmp(temp
, "2.1"))
1359 else if (!strcmp(temp
, "2.2"))
1363 print_fatal_error("Bad VERSION \"%s\" on line %d.", temp
, linenum
);
1370 print_fatal_error("Missing VERSION number on line %d.", linenum
);
1375 else if (!_cups_strcasecmp(token
, "RESOURCE"))
1381 if (!get_token(fp
, resource
, sizeof(resource
), &linenum
))
1383 print_fatal_error("Missing RESOURCE path on line %d.", linenum
);
1388 else if (!_cups_strcasecmp(token
, "OPERATION"))
1394 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1396 print_fatal_error("Missing OPERATION code on line %d.", linenum
);
1401 if ((op
= ippOpValue(token
)) == (ipp_op_t
)-1 &&
1402 (op
= strtol(token
, NULL
, 0)) == 0)
1404 print_fatal_error("Bad OPERATION code \"%s\" on line %d.", token
,
1410 else if (!_cups_strcasecmp(token
, "GROUP"))
1413 * Attribute group...
1416 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1418 print_fatal_error("Missing GROUP tag on line %d.", linenum
);
1423 if ((value
= ippTagValue(token
)) < 0)
1425 print_fatal_error("Bad GROUP tag \"%s\" on line %d.", token
, linenum
);
1431 ippAddSeparator(request
);
1435 else if (!_cups_strcasecmp(token
, "DELAY"))
1438 * Delay before operation...
1443 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1445 print_fatal_error("Missing DELAY value on line %d.", linenum
);
1450 if ((delay
= _cupsStrScand(token
, NULL
, localeconv())) <= 0.0)
1452 print_fatal_error("Bad DELAY value \"%s\" on line %d.", token
,
1459 if (Output
== _CUPS_OUTPUT_TEST
)
1460 printf(" [%g second delay]\n", delay
);
1462 usleep((int)(1000000.0 * delay
));
1465 else if (!_cups_strcasecmp(token
, "ATTR"))
1471 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1473 print_fatal_error("Missing ATTR value tag on line %d.", linenum
);
1478 if ((value
= ippTagValue(token
)) == IPP_TAG_ZERO
)
1480 print_fatal_error("Bad ATTR value tag \"%s\" on line %d.", token
,
1486 if (!get_token(fp
, attr
, sizeof(attr
), &linenum
))
1488 print_fatal_error("Missing ATTR name on line %d.", linenum
);
1493 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1495 print_fatal_error("Missing ATTR value on line %d.", linenum
);
1500 expand_variables(vars
, token
, temp
, sizeof(token
));
1504 case IPP_TAG_BOOLEAN
:
1505 if (!_cups_strcasecmp(token
, "true"))
1506 ippAddBoolean(request
, group
, attr
, 1);
1508 ippAddBoolean(request
, group
, attr
, atoi(token
));
1511 case IPP_TAG_INTEGER
:
1513 if (!strchr(token
, ','))
1514 ippAddInteger(request
, group
, value
, attr
,
1515 strtol(token
, &tokenptr
, 0));
1518 int values
[100], /* Values */
1519 num_values
= 1; /* Number of values */
1521 values
[0] = strtol(token
, &tokenptr
, 10);
1522 while (tokenptr
&& *tokenptr
&&
1523 num_values
< (int)(sizeof(values
) / sizeof(values
[0])))
1525 if (*tokenptr
== ',')
1527 else if (!isdigit(*tokenptr
& 255) && *tokenptr
!= '-')
1530 values
[num_values
] = strtol(tokenptr
, &tokenptr
, 0);
1534 ippAddIntegers(request
, group
, value
, attr
, num_values
, values
);
1537 if (!tokenptr
|| *tokenptr
)
1539 print_fatal_error("Bad %s value \"%s\" on line %d.",
1540 ippTagString(value
), token
, linenum
);
1546 case IPP_TAG_RESOLUTION
:
1548 int xres
, /* X resolution */
1549 yres
; /* Y resolution */
1550 char *ptr
; /* Pointer into value */
1552 xres
= yres
= strtol(token
, (char **)&ptr
, 10);
1553 if (ptr
> token
&& xres
> 0)
1556 yres
= strtol(ptr
+ 1, (char **)&ptr
, 10);
1559 if (ptr
<= token
|| xres
<= 0 || yres
<= 0 || !ptr
||
1560 (_cups_strcasecmp(ptr
, "dpi") && _cups_strcasecmp(ptr
, "dpc") &&
1561 _cups_strcasecmp(ptr
, "other")))
1563 print_fatal_error("Bad resolution value \"%s\" on line %d.",
1569 if (!_cups_strcasecmp(ptr
, "dpi"))
1570 ippAddResolution(request
, group
, attr
, IPP_RES_PER_INCH
,
1572 else if (!_cups_strcasecmp(ptr
, "dpc"))
1573 ippAddResolution(request
, group
, attr
, IPP_RES_PER_CM
,
1576 ippAddResolution(request
, group
, attr
, (ipp_res_t
)0,
1581 case IPP_TAG_RANGE
:
1583 int lowers
[4], /* Lower value */
1584 uppers
[4], /* Upper values */
1585 num_vals
; /* Number of values */
1588 num_vals
= sscanf(token
, "%d-%d,%d-%d,%d-%d,%d-%d",
1589 lowers
+ 0, uppers
+ 0,
1590 lowers
+ 1, uppers
+ 1,
1591 lowers
+ 2, uppers
+ 2,
1592 lowers
+ 3, uppers
+ 3);
1594 if ((num_vals
& 1) || num_vals
== 0)
1596 print_fatal_error("Bad rangeOfInteger value \"%s\" on line "
1597 "%d.", token
, linenum
);
1602 ippAddRanges(request
, group
, attr
, num_vals
/ 2, lowers
,
1607 case IPP_TAG_BEGIN_COLLECTION
:
1608 if (!strcmp(token
, "{"))
1610 ipp_t
*col
= get_collection(vars
, fp
, &linenum
);
1611 /* Collection value */
1615 lastcol
= ippAddCollection(request
, group
, attr
, col
);
1626 print_fatal_error("Bad ATTR collection value on line %d.",
1634 print_fatal_error("Unsupported ATTR value tag %s on line %d.",
1635 ippTagString(value
), linenum
);
1639 case IPP_TAG_TEXTLANG
:
1640 case IPP_TAG_NAMELANG
:
1643 case IPP_TAG_KEYWORD
:
1645 case IPP_TAG_URISCHEME
:
1646 case IPP_TAG_CHARSET
:
1647 case IPP_TAG_LANGUAGE
:
1648 case IPP_TAG_MIMETYPE
:
1649 if (!strchr(token
, ','))
1650 ippAddString(request
, group
, value
, attr
, NULL
, token
);
1654 * Multiple string values...
1657 int num_values
; /* Number of values */
1658 char *values
[100], /* Values */
1659 *ptr
; /* Pointer to next value */
1665 for (ptr
= strchr(token
, ','); ptr
; ptr
= strchr(ptr
, ','))
1668 values
[num_values
] = ptr
;
1672 ippAddStrings(request
, group
, value
, attr
, num_values
,
1673 NULL
, (const char **)values
);
1678 else if (!_cups_strcasecmp(token
, "FILE"))
1684 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1686 print_fatal_error("Missing FILE filename on line %d.", linenum
);
1691 expand_variables(vars
, token
, temp
, sizeof(token
));
1692 get_filename(testfile
, filename
, token
, sizeof(filename
));
1694 else if (!_cups_strcasecmp(token
, "STATUS"))
1700 if (num_statuses
>= (int)(sizeof(statuses
) / sizeof(statuses
[0])))
1702 print_fatal_error("Too many STATUS's on line %d.", linenum
);
1707 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1709 print_fatal_error("Missing STATUS code on line %d.", linenum
);
1714 if ((statuses
[num_statuses
].status
= ippErrorValue(token
))
1715 == (ipp_status_t
)-1 &&
1716 (statuses
[num_statuses
].status
= strtol(token
, NULL
, 0)) == 0)
1718 print_fatal_error("Bad STATUS code \"%s\" on line %d.", token
,
1724 last_status
= statuses
+ num_statuses
;
1727 last_status
->if_defined
= NULL
;
1728 last_status
->if_not_defined
= NULL
;
1729 last_status
->repeat_match
= 0;
1730 last_status
->repeat_no_match
= 0;
1732 else if (!_cups_strcasecmp(token
, "EXPECT"))
1735 * Expected attributes...
1738 if (num_expects
>= (int)(sizeof(expects
) / sizeof(expects
[0])))
1740 print_fatal_error("Too many EXPECT's on line %d.", linenum
);
1745 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1747 print_fatal_error("Missing EXPECT name on line %d.", linenum
);
1752 last_expect
= expects
+ num_expects
;
1755 memset(last_expect
, 0, sizeof(_cups_expect_t
));
1757 if (token
[0] == '!')
1759 last_expect
->not_expect
= 1;
1760 last_expect
->name
= strdup(token
+ 1);
1762 else if (token
[0] == '?')
1764 last_expect
->optional
= 1;
1765 last_expect
->name
= strdup(token
+ 1);
1768 last_expect
->name
= strdup(token
);
1770 else if (!_cups_strcasecmp(token
, "COUNT"))
1772 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1774 print_fatal_error("Missing COUNT number on line %d.", linenum
);
1779 if ((i
= atoi(token
)) <= 0)
1781 print_fatal_error("Bad COUNT \"%s\" on line %d.", token
, linenum
);
1787 last_expect
->count
= i
;
1790 print_fatal_error("COUNT without a preceding EXPECT on line %d.",
1796 else if (!_cups_strcasecmp(token
, "DEFINE-MATCH"))
1798 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1800 print_fatal_error("Missing DEFINE-MATCH variable on line %d.",
1807 last_expect
->define_match
= strdup(token
);
1810 print_fatal_error("DEFINE-MATCH without a preceding EXPECT on line "
1816 else if (!_cups_strcasecmp(token
, "DEFINE-NO-MATCH"))
1818 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1820 print_fatal_error("Missing DEFINE-NO-MATCH variable on line %d.",
1827 last_expect
->define_no_match
= strdup(token
);
1830 print_fatal_error("DEFINE-NO-MATCH without a preceding EXPECT on "
1831 "line %d.", linenum
);
1836 else if (!_cups_strcasecmp(token
, "DEFINE-VALUE"))
1838 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1840 print_fatal_error("Missing DEFINE-VALUE variable on line %d.",
1847 last_expect
->define_value
= strdup(token
);
1850 print_fatal_error("DEFINE-VALUE without a preceding EXPECT on line "
1856 else if (!_cups_strcasecmp(token
, "OF-TYPE"))
1858 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1860 print_fatal_error("Missing OF-TYPE value tag(s) on line %d.",
1867 last_expect
->of_type
= strdup(token
);
1870 print_fatal_error("OF-TYPE without a preceding EXPECT on line %d.",
1876 else if (!_cups_strcasecmp(token
, "IN-GROUP"))
1878 ipp_tag_t in_group
; /* IN-GROUP value */
1881 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1883 print_fatal_error("Missing IN-GROUP group tag on line %d.", linenum
);
1888 if ((in_group
= ippTagValue(token
)) == (ipp_tag_t
)-1)
1891 else if (last_expect
)
1892 last_expect
->in_group
= in_group
;
1895 print_fatal_error("IN-GROUP without a preceding EXPECT on line %d.",
1901 else if (!_cups_strcasecmp(token
, "REPEAT-MATCH"))
1904 last_status
->repeat_match
= 1;
1905 else if (last_expect
)
1906 last_expect
->repeat_match
= 1;
1909 print_fatal_error("REPEAT-MATCH without a preceding EXPECT or STATUS "
1910 "on line %d.", linenum
);
1915 else if (!_cups_strcasecmp(token
, "REPEAT-NO-MATCH"))
1918 last_status
->repeat_no_match
= 1;
1919 else if (last_expect
)
1920 last_expect
->repeat_no_match
= 1;
1923 print_fatal_error("REPEAT-NO-MATCH without a preceding EXPECT or "
1924 "STATUS on ine %d.", linenum
);
1929 else if (!_cups_strcasecmp(token
, "SAME-COUNT-AS"))
1931 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1933 print_fatal_error("Missing SAME-COUNT-AS name on line %d.", linenum
);
1939 last_expect
->same_count_as
= strdup(token
);
1942 print_fatal_error("SAME-COUNT-AS without a preceding EXPECT on line "
1948 else if (!_cups_strcasecmp(token
, "IF-DEFINED"))
1950 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1952 print_fatal_error("Missing IF-DEFINED name on line %d.", linenum
);
1958 last_expect
->if_defined
= strdup(token
);
1959 else if (last_status
)
1960 last_status
->if_defined
= strdup(token
);
1963 print_fatal_error("IF-DEFINED without a preceding EXPECT or STATUS "
1964 "on line %d.", linenum
);
1969 else if (!_cups_strcasecmp(token
, "IF-NOT-DEFINED"))
1971 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1973 print_fatal_error("Missing IF-NOT-DEFINED name on line %d.", linenum
);
1979 last_expect
->if_not_defined
= strdup(token
);
1980 else if (last_status
)
1981 last_status
->if_not_defined
= strdup(token
);
1984 print_fatal_error("IF-NOT-DEFINED without a preceding EXPECT or STATUS "
1985 "on line %d.", linenum
);
1990 else if (!_cups_strcasecmp(token
, "WITH-VALUE"))
1992 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1994 print_fatal_error("Missing WITH-VALUE value on line %d.", linenum
);
2002 * Expand any variables in the value and then save it.
2005 expand_variables(vars
, token
, temp
, sizeof(token
));
2007 tokenptr
= token
+ strlen(token
) - 1;
2009 if (token
[0] == '/' && tokenptr
> token
&& *tokenptr
== '/')
2012 * WITH-VALUE is a POSIX extended regular expression.
2015 last_expect
->with_value
= calloc(1, tokenptr
- token
);
2016 last_expect
->with_regex
= 1;
2018 if (last_expect
->with_value
)
2019 memcpy(last_expect
->with_value
, token
+ 1, tokenptr
- token
- 1);
2024 * WITH-VALUE is a literal value...
2027 last_expect
->with_value
= strdup(token
);
2032 print_fatal_error("WITH-VALUE without a preceding EXPECT on line %d.",
2038 else if (!_cups_strcasecmp(token
, "DISPLAY"))
2041 * Display attributes...
2044 if (num_displayed
>= (int)(sizeof(displayed
) / sizeof(displayed
[0])))
2046 print_fatal_error("Too many DISPLAY's on line %d", linenum
);
2051 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2053 print_fatal_error("Missing DISPLAY name on line %d.", linenum
);
2058 displayed
[num_displayed
] = strdup(token
);
2063 print_fatal_error("Unexpected token %s seen on line %d.", token
,
2071 * Submit the IPP request...
2076 request
->request
.op
.version
[0] = version
/ 10;
2077 request
->request
.op
.version
[1] = version
% 10;
2078 request
->request
.op
.operation_id
= op
;
2079 request
->request
.op
.request_id
= request_id
;
2081 if (Output
== _CUPS_OUTPUT_PLIST
)
2084 puts("<key>Name</key>");
2085 print_xml_string("string", name
);
2086 puts("<key>Operation</key>");
2087 print_xml_string("string", ippOpString(op
));
2088 puts("<key>RequestAttributes</key>");
2093 for (attrptr
= request
->attrs
, group
= attrptr
->group_tag
;
2095 attrptr
= attrptr
->next
)
2096 print_attr(attrptr
, &group
);
2101 else if (Output
== _CUPS_OUTPUT_TEST
)
2105 printf(" %s:\n", ippOpString(op
));
2107 for (attrptr
= request
->attrs
; attrptr
; attrptr
= attrptr
->next
)
2108 print_attr(attrptr
, NULL
);
2111 printf(" %-69.69s [", name
);
2115 if ((skip_previous
&& !prev_pass
) || skip_test
)
2122 if (Output
== _CUPS_OUTPUT_PLIST
)
2124 puts("<key>Successful</key>");
2126 puts("<key>StatusCode</key>");
2127 print_xml_string("string", "skip");
2128 puts("<key>ResponseAttributes</key>");
2132 else if (Output
== _CUPS_OUTPUT_TEST
)
2142 if (transfer
== _CUPS_TRANSFER_CHUNKED
||
2143 (transfer
== _CUPS_TRANSFER_AUTO
&& filename
[0]))
2146 * Send request using chunking - a 0 length means "chunk".
2154 * Send request using content length...
2157 length
= ippLength(request
);
2159 if (filename
[0] && (reqfile
= cupsFileOpen(filename
, "r")) != NULL
)
2162 * Read the file to get the uncompressed file size...
2165 while ((bytes
= cupsFileRead(reqfile
, buffer
, sizeof(buffer
))) > 0)
2168 cupsFileClose(reqfile
);
2173 * Send the request...
2180 if (status
!= HTTP_ERROR
)
2182 while (!response
&& !Cancel
&& prev_pass
)
2184 status
= cupsSendRequest(http
, request
, resource
, length
);
2186 if (!Cancel
&& status
== HTTP_CONTINUE
&&
2187 request
->state
== IPP_DATA
&& filename
[0])
2189 if ((reqfile
= cupsFileOpen(filename
, "r")) != NULL
)
2192 (bytes
= cupsFileRead(reqfile
, buffer
,
2193 sizeof(buffer
))) > 0)
2194 if ((status
= cupsWriteRequestData(http
, buffer
,
2195 bytes
)) != HTTP_CONTINUE
)
2198 cupsFileClose(reqfile
);
2202 snprintf(buffer
, sizeof(buffer
), "%s: %s", filename
,
2204 _cupsSetError(IPP_INTERNAL_ERROR
, buffer
, 0);
2206 status
= HTTP_ERROR
;
2211 * Get the server's response...
2214 if (!Cancel
&& status
!= HTTP_ERROR
)
2216 response
= cupsGetResponse(http
, resource
);
2217 status
= httpGetStatus(http
);
2220 if (!Cancel
&& status
== HTTP_ERROR
&&
2222 http
->error
!= WSAETIMEDOUT
)
2224 http
->error
!= ETIMEDOUT
)
2227 if (httpReconnect(http
))
2230 else if (status
== HTTP_ERROR
)
2233 httpReconnect(http
);
2241 * Check results of request...
2245 prev_pass
= pass
= 0;
2248 if (http
->version
!= HTTP_1_1
)
2249 prev_pass
= pass
= 0;
2251 if (response
->state
!= IPP_DATA
)
2252 prev_pass
= pass
= 0;
2254 if (response
->request
.status
.request_id
!= request_id
)
2255 prev_pass
= pass
= 0;
2258 (response
->request
.status
.version
[0] != (version
/ 10) ||
2259 response
->request
.status
.version
[1] != (version
% 10)))
2260 prev_pass
= pass
= 0;
2262 if ((attrptr
= ippFindAttribute(response
, "job-id",
2263 IPP_TAG_INTEGER
)) != NULL
)
2265 snprintf(temp
, sizeof(temp
), "%d", attrptr
->values
[0].integer
);
2266 set_variable(vars
, "job-id", temp
);
2269 if ((attrptr
= ippFindAttribute(response
, "job-uri",
2270 IPP_TAG_URI
)) != NULL
)
2271 set_variable(vars
, "job-uri", attrptr
->values
[0].string
.text
);
2273 if ((attrptr
= ippFindAttribute(response
, "notify-subscription-id",
2274 IPP_TAG_INTEGER
)) != NULL
)
2276 snprintf(temp
, sizeof(temp
), "%d", attrptr
->values
[0].integer
);
2277 set_variable(vars
, "notify-subscription-id", temp
);
2280 attrptr
= response
->attrs
;
2281 if (!attrptr
|| !attrptr
->name
||
2282 attrptr
->value_tag
!= IPP_TAG_CHARSET
||
2283 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2284 attrptr
->num_values
!= 1 ||
2285 strcmp(attrptr
->name
, "attributes-charset"))
2286 prev_pass
= pass
= 0;
2290 attrptr
= attrptr
->next
;
2291 if (!attrptr
|| !attrptr
->name
||
2292 attrptr
->value_tag
!= IPP_TAG_LANGUAGE
||
2293 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2294 attrptr
->num_values
!= 1 ||
2295 strcmp(attrptr
->name
, "attributes-natural-language"))
2296 prev_pass
= pass
= 0;
2299 if ((attrptr
= ippFindAttribute(response
, "status-message",
2300 IPP_TAG_ZERO
)) != NULL
&&
2301 (attrptr
->value_tag
!= IPP_TAG_TEXT
||
2302 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2303 attrptr
->num_values
!= 1 ||
2304 (attrptr
->value_tag
== IPP_TAG_TEXT
&&
2305 strlen(attrptr
->values
[0].string
.text
) > 255)))
2306 prev_pass
= pass
= 0;
2308 if ((attrptr
= ippFindAttribute(response
, "detailed-status-message",
2309 IPP_TAG_ZERO
)) != NULL
&&
2310 (attrptr
->value_tag
!= IPP_TAG_TEXT
||
2311 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2312 attrptr
->num_values
!= 1 ||
2313 (attrptr
->value_tag
== IPP_TAG_TEXT
&&
2314 strlen(attrptr
->values
[0].string
.text
) > 1023)))
2315 prev_pass
= pass
= 0;
2317 for (attrptr
= response
->attrs
, group
= attrptr
->group_tag
;
2319 attrptr
= attrptr
->next
)
2321 if (attrptr
->group_tag
< group
&& attrptr
->group_tag
!= IPP_TAG_ZERO
)
2323 prev_pass
= pass
= 0;
2327 if (!validate_attr(attrptr
, 0))
2329 prev_pass
= pass
= 0;
2334 for (i
= 0; i
< num_statuses
; i
++)
2336 if (statuses
[i
].if_defined
&&
2337 !get_variable(vars
, statuses
[i
].if_defined
))
2340 if (statuses
[i
].if_not_defined
&&
2341 get_variable(vars
, statuses
[i
].if_not_defined
))
2344 if (response
->request
.status
.status_code
== statuses
[i
].status
)
2346 if (statuses
[i
].repeat_match
)
2351 else if (statuses
[i
].repeat_no_match
)
2355 if (i
== num_statuses
&& num_statuses
> 0)
2356 prev_pass
= pass
= 0;
2359 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
2361 if (expect
->if_defined
&& !get_variable(vars
, expect
->if_defined
))
2364 if (expect
->if_not_defined
&&
2365 get_variable(vars
, expect
->if_not_defined
))
2368 found
= ippFindAttribute(response
, expect
->name
, IPP_TAG_ZERO
);
2370 if ((found
&& expect
->not_expect
) ||
2371 (!found
&& !(expect
->not_expect
|| expect
->optional
)) ||
2372 (found
&& !expect_matches(expect
, found
->value_tag
)) ||
2373 (found
&& expect
->in_group
&&
2374 found
->group_tag
!= expect
->in_group
))
2376 if (expect
->define_no_match
)
2377 set_variable(vars
, expect
->define_no_match
, "1");
2378 else if (!expect
->define_match
)
2379 prev_pass
= pass
= 0;
2381 if (expect
->repeat_no_match
)
2388 _ippAttrString(found
, buffer
, sizeof(buffer
));
2391 !with_value(expect
->with_value
, expect
->with_regex
, found
, 0,
2392 buffer
, sizeof(buffer
)))
2394 if (expect
->define_no_match
)
2395 set_variable(vars
, expect
->define_no_match
, "1");
2396 else if (!expect
->define_match
&& !expect
->define_value
)
2397 prev_pass
= pass
= 0;
2399 if (expect
->repeat_no_match
)
2405 if (found
&& expect
->count
> 0 &&
2406 found
->num_values
!= expect
->count
)
2408 if (expect
->define_no_match
)
2409 set_variable(vars
, expect
->define_no_match
, "1");
2410 else if (!expect
->define_match
&& !expect
->define_value
)
2411 prev_pass
= pass
= 0;
2413 if (expect
->repeat_no_match
)
2419 if (found
&& expect
->same_count_as
)
2421 attrptr
= ippFindAttribute(response
, expect
->same_count_as
,
2424 if (!attrptr
|| attrptr
->num_values
!= found
->num_values
)
2426 if (expect
->define_no_match
)
2427 set_variable(vars
, expect
->define_no_match
, "1");
2428 else if (!expect
->define_match
&& !expect
->define_value
)
2429 prev_pass
= pass
= 0;
2431 if (expect
->repeat_no_match
)
2438 if (found
&& expect
->define_match
)
2439 set_variable(vars
, expect
->define_match
, "1");
2441 if (found
&& expect
->define_value
)
2442 set_variable(vars
, expect
->define_value
, buffer
);
2444 if (found
&& expect
->repeat_match
)
2451 * If we are going to repeat this test, sleep 1 second so we don't flood
2452 * the printer with requests...
2458 while (repeat_test
);
2469 if (Output
== _CUPS_OUTPUT_PLIST
)
2471 puts("<key>Successful</key>");
2472 puts(prev_pass
? "<true />" : "<false />");
2473 puts("<key>StatusCode</key>");
2474 print_xml_string("string", ippErrorString(cupsLastError()));
2475 puts("<key>ResponseAttributes</key>");
2478 for (attrptr
= response
? response
->attrs
: NULL
,
2479 group
= attrptr
? attrptr
->group_tag
: IPP_TAG_ZERO
;
2481 attrptr
= attrptr
->next
)
2482 print_attr(attrptr
, &group
);
2486 else if (Output
== _CUPS_OUTPUT_TEST
)
2488 puts(prev_pass
? "PASS]" : "FAIL]");
2490 if (Verbosity
&& response
)
2492 printf(" RECEIVED: %lu bytes in response\n",
2493 (unsigned long)ippLength(response
));
2494 printf(" status-code = %x (%s)\n", cupsLastError(),
2495 ippErrorString(cupsLastError()));
2497 for (attrptr
= response
->attrs
;
2499 attrptr
= attrptr
->next
)
2501 print_attr(attrptr
, NULL
);
2505 else if (!prev_pass
)
2506 fprintf(stderr
, "%s\n", cupsLastErrorString());
2508 if (prev_pass
&& Output
!= _CUPS_OUTPUT_PLIST
&&
2509 Output
!= _CUPS_OUTPUT_QUIET
&& !Verbosity
&& num_displayed
> 0)
2511 if (Output
>= _CUPS_OUTPUT_LIST
)
2513 size_t width
; /* Length of value */
2516 for (i
= 0; i
< num_displayed
; i
++)
2518 widths
[i
] = strlen(displayed
[i
]);
2520 for (attrptr
= ippFindAttribute(response
, displayed
[i
], IPP_TAG_ZERO
);
2522 attrptr
= ippFindNextAttribute(response
, displayed
[i
],
2525 width
= _ippAttrString(attrptr
, NULL
, 0);
2526 if (width
> widths
[i
])
2531 if (Output
== _CUPS_OUTPUT_CSV
)
2532 print_csv(NULL
, num_displayed
, displayed
, widths
);
2534 print_line(NULL
, num_displayed
, displayed
, widths
);
2536 attrptr
= response
->attrs
;
2540 while (attrptr
&& attrptr
->group_tag
<= IPP_TAG_OPERATION
)
2541 attrptr
= attrptr
->next
;
2545 if (Output
== _CUPS_OUTPUT_CSV
)
2546 print_csv(attrptr
, num_displayed
, displayed
, widths
);
2548 print_line(attrptr
, num_displayed
, displayed
, widths
);
2550 while (attrptr
&& attrptr
->group_tag
> IPP_TAG_OPERATION
)
2551 attrptr
= attrptr
->next
;
2557 for (attrptr
= response
->attrs
;
2559 attrptr
= attrptr
->next
)
2563 for (i
= 0; i
< num_displayed
; i
++)
2565 if (!strcmp(displayed
[i
], attrptr
->name
))
2567 print_attr(attrptr
, NULL
);
2575 else if (!prev_pass
)
2577 if (Output
== _CUPS_OUTPUT_PLIST
)
2579 puts("<key>Errors</key>");
2583 if (http
->version
!= HTTP_1_1
)
2584 print_test_error("Bad HTTP version (%d.%d)", http
->version
/ 100,
2585 http
->version
% 100);
2588 print_test_error("IPP request failed with status %s (%s)",
2589 ippErrorString(cupsLastError()),
2590 cupsLastErrorString());
2593 if (response
->state
!= IPP_DATA
)
2594 print_test_error("Missing end-of-attributes-tag in response "
2595 "(RFC 2910 section 3.5.1)");
2598 (response
->request
.status
.version
[0] != (version
/ 10) ||
2599 response
->request
.status
.version
[1] != (version
% 10)))
2600 print_test_error("Bad version %d.%d in response - expected %d.%d "
2601 "(RFC 2911 section 3.1.8).",
2602 response
->request
.status
.version
[0],
2603 response
->request
.status
.version
[1],
2604 version
/ 10, version
% 10);
2606 if (response
->request
.status
.request_id
!= request_id
)
2607 print_test_error("Bad request ID %d in response - expected %d "
2608 "(RFC 2911 section 3.1.1)",
2609 response
->request
.status
.request_id
, request_id
);
2611 attrptr
= response
->attrs
;
2613 print_test_error("Missing first attribute \"attributes-charset "
2614 "(charset)\" in group operation-attributes-tag "
2615 "(RFC 2911 section 3.1.4).");
2618 if (!attrptr
->name
||
2619 attrptr
->value_tag
!= IPP_TAG_CHARSET
||
2620 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2621 attrptr
->num_values
!= 1 ||
2622 strcmp(attrptr
->name
, "attributes-charset"))
2623 print_test_error("Bad first attribute \"%s (%s%s)\" in group %s, "
2624 "expected \"attributes-charset (charset)\" in "
2625 "group operation-attributes-tag (RFC 2911 section "
2627 attrptr
->name
? attrptr
->name
: "(null)",
2628 attrptr
->num_values
> 1 ? "1setOf " : "",
2629 ippTagString(attrptr
->value_tag
),
2630 ippTagString(attrptr
->group_tag
));
2632 attrptr
= attrptr
->next
;
2634 print_test_error("Missing second attribute \"attributes-natural-"
2635 "language (naturalLanguage)\" in group "
2636 "operation-attributes-tag (RFC 2911 section "
2638 else if (!attrptr
->name
||
2639 attrptr
->value_tag
!= IPP_TAG_LANGUAGE
||
2640 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2641 attrptr
->num_values
!= 1 ||
2642 strcmp(attrptr
->name
, "attributes-natural-language"))
2643 print_test_error("Bad first attribute \"%s (%s%s)\" in group %s, "
2644 "expected \"attributes-natural-language "
2645 "(naturalLanguage)\" in group "
2646 "operation-attributes-tag (RFC 2911 section "
2648 attrptr
->name
? attrptr
->name
: "(null)",
2649 attrptr
->num_values
> 1 ? "1setOf " : "",
2650 ippTagString(attrptr
->value_tag
),
2651 ippTagString(attrptr
->group_tag
));
2654 if ((attrptr
= ippFindAttribute(response
, "status-message",
2655 IPP_TAG_ZERO
)) != NULL
)
2657 if (attrptr
->value_tag
!= IPP_TAG_TEXT
)
2658 print_test_error("status-message (text(255)) has wrong value tag "
2659 "%s (RFC 2911 section 3.1.6.2).",
2660 ippTagString(attrptr
->value_tag
));
2661 if (attrptr
->group_tag
!= IPP_TAG_OPERATION
)
2662 print_test_error("status-message (text(255)) has wrong group tag "
2663 "%s (RFC 2911 section 3.1.6.2).",
2664 ippTagString(attrptr
->group_tag
));
2665 if (attrptr
->num_values
!= 1)
2666 print_test_error("status-message (text(255)) has %d values "
2667 "(RFC 2911 section 3.1.6.2).",
2668 attrptr
->num_values
);
2669 if (attrptr
->value_tag
== IPP_TAG_TEXT
&&
2670 strlen(attrptr
->values
[0].string
.text
) > 255)
2671 print_test_error("status-message (text(255)) has bad length %d"
2672 " (RFC 2911 section 3.1.6.2).",
2673 (int)strlen(attrptr
->values
[0].string
.text
));
2676 if ((attrptr
= ippFindAttribute(response
, "detailed-status-message",
2677 IPP_TAG_ZERO
)) != NULL
)
2679 if (attrptr
->value_tag
!= IPP_TAG_TEXT
)
2680 print_test_error("detailed-status-message (text(MAX)) has wrong "
2681 "value tag %s (RFC 2911 section 3.1.6.3).",
2682 ippTagString(attrptr
->value_tag
));
2683 if (attrptr
->group_tag
!= IPP_TAG_OPERATION
)
2684 print_test_error("detailed-status-message (text(MAX)) has wrong "
2685 "group tag %s (RFC 2911 section 3.1.6.3).",
2686 ippTagString(attrptr
->group_tag
));
2687 if (attrptr
->num_values
!= 1)
2688 print_test_error("detailed-status-message (text(MAX)) has %d values"
2689 " (RFC 2911 section 3.1.6.3).",
2690 attrptr
->num_values
);
2691 if (attrptr
->value_tag
== IPP_TAG_TEXT
&&
2692 strlen(attrptr
->values
[0].string
.text
) > 1023)
2693 print_test_error("detailed-status-message (text(MAX)) has bad "
2694 "length %d (RFC 2911 section 3.1.6.3).",
2695 (int)strlen(attrptr
->values
[0].string
.text
));
2698 for (attrptr
= response
->attrs
, group
= attrptr
->group_tag
;
2700 attrptr
= attrptr
->next
)
2702 if (attrptr
->group_tag
< group
&& attrptr
->group_tag
!= IPP_TAG_ZERO
)
2703 print_test_error("Attribute groups out of order (%s < %s)",
2704 ippTagString(attrptr
->group_tag
),
2705 ippTagString(group
));
2707 validate_attr(attrptr
, 1);
2710 for (i
= 0; i
< num_statuses
; i
++)
2712 if (statuses
[i
].if_defined
&&
2713 !get_variable(vars
, statuses
[i
].if_defined
))
2716 if (statuses
[i
].if_not_defined
&&
2717 get_variable(vars
, statuses
[i
].if_not_defined
))
2720 if (response
->request
.status
.status_code
== statuses
[i
].status
)
2724 if (i
== num_statuses
&& num_statuses
> 0)
2726 for (i
= 0; i
< num_statuses
; i
++)
2728 if (statuses
[i
].if_defined
&&
2729 !get_variable(vars
, statuses
[i
].if_defined
))
2732 if (statuses
[i
].if_not_defined
&&
2733 get_variable(vars
, statuses
[i
].if_not_defined
))
2736 print_test_error("EXPECTED: STATUS %s (got %s)",
2737 ippErrorString(statuses
[i
].status
),
2738 ippErrorString(cupsLastError()));
2741 if ((attrptr
= ippFindAttribute(response
, "status-message",
2742 IPP_TAG_TEXT
)) != NULL
)
2743 print_test_error("status-message=\"%s\"",
2744 attrptr
->values
[0].string
.text
);
2747 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
2749 if (expect
->define_match
|| expect
->define_no_match
||
2750 expect
->define_value
)
2753 if (expect
->if_defined
&& !get_variable(vars
, expect
->if_defined
))
2756 if (expect
->if_not_defined
&&
2757 get_variable(vars
, expect
->if_not_defined
))
2760 found
= ippFindAttribute(response
, expect
->name
, IPP_TAG_ZERO
);
2762 if (found
&& expect
->not_expect
)
2763 print_test_error("NOT EXPECTED: %s", expect
->name
);
2764 else if (!found
&& !(expect
->not_expect
|| expect
->optional
))
2765 print_test_error("EXPECTED: %s", expect
->name
);
2768 if (!expect_matches(expect
, found
->value_tag
))
2769 print_test_error("EXPECTED: %s OF-TYPE %s (got %s)",
2770 expect
->name
, expect
->of_type
,
2771 ippTagString(found
->value_tag
));
2773 if (expect
->in_group
&& found
->group_tag
!= expect
->in_group
)
2774 print_test_error("EXPECTED: %s IN-GROUP %s (got %s).",
2775 expect
->name
, ippTagString(expect
->in_group
),
2776 ippTagString(found
->group_tag
));
2778 if (!with_value(expect
->with_value
, expect
->with_regex
, found
, 0,
2779 buffer
, sizeof(buffer
)))
2781 if (expect
->with_regex
)
2782 print_test_error("EXPECTED: %s WITH-VALUE /%s/",
2783 expect
->name
, expect
->with_value
);
2785 print_test_error("EXPECTED: %s WITH-VALUE \"%s\"",
2786 expect
->name
, expect
->with_value
);
2788 with_value(expect
->with_value
, expect
->with_regex
, found
, 1,
2789 buffer
, sizeof(buffer
));
2792 if (expect
->count
> 0 && found
->num_values
!= expect
->count
)
2794 print_test_error("EXPECTED: %s COUNT %d (got %d)", expect
->name
,
2795 expect
->count
, found
->num_values
);
2798 if (expect
->same_count_as
)
2800 attrptr
= ippFindAttribute(response
, expect
->same_count_as
,
2804 print_test_error("EXPECTED: %s (%d values) SAME-COUNT-AS %s "
2805 "(not returned)", expect
->name
,
2806 found
->num_values
, expect
->same_count_as
);
2807 else if (attrptr
->num_values
!= found
->num_values
)
2808 print_test_error("EXPECTED: %s (%d values) SAME-COUNT-AS %s "
2809 "(%d values)", expect
->name
, found
->num_values
,
2810 expect
->same_count_as
, attrptr
->num_values
);
2816 if (Output
== _CUPS_OUTPUT_PLIST
)
2822 if (Output
== _CUPS_OUTPUT_PLIST
)
2825 ippDelete(response
);
2828 for (i
= 0; i
< num_statuses
; i
++)
2830 if (statuses
[i
].if_defined
)
2831 free(statuses
[i
].if_defined
);
2832 if (statuses
[i
].if_not_defined
)
2833 free(statuses
[i
].if_not_defined
);
2837 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
2840 if (expect
->of_type
)
2841 free(expect
->of_type
);
2842 if (expect
->same_count_as
)
2843 free(expect
->same_count_as
);
2844 if (expect
->if_defined
)
2845 free(expect
->if_defined
);
2846 if (expect
->if_not_defined
)
2847 free(expect
->if_not_defined
);
2848 if (expect
->with_value
)
2849 free(expect
->with_value
);
2850 if (expect
->define_match
)
2851 free(expect
->define_match
);
2852 if (expect
->define_no_match
)
2853 free(expect
->define_no_match
);
2854 if (expect
->define_value
)
2855 free(expect
->define_value
);
2859 for (i
= 0; i
< num_displayed
; i
++)
2863 if (!ignore_errors
&& !prev_pass
)
2874 ippDelete(response
);
2876 for (i
= 0; i
< num_statuses
; i
++)
2878 if (statuses
[i
].if_defined
)
2879 free(statuses
[i
].if_defined
);
2880 if (statuses
[i
].if_not_defined
)
2881 free(statuses
[i
].if_not_defined
);
2884 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
2887 if (expect
->of_type
)
2888 free(expect
->of_type
);
2889 if (expect
->same_count_as
)
2890 free(expect
->same_count_as
);
2891 if (expect
->if_defined
)
2892 free(expect
->if_defined
);
2893 if (expect
->if_not_defined
)
2894 free(expect
->if_not_defined
);
2895 if (expect
->with_value
)
2896 free(expect
->with_value
);
2897 if (expect
->define_match
)
2898 free(expect
->define_match
);
2899 if (expect
->define_no_match
)
2900 free(expect
->define_no_match
);
2901 if (expect
->define_value
)
2902 free(expect
->define_value
);
2905 for (i
= 0; i
< num_displayed
; i
++)
2913 * 'expand_variables()' - Expand variables in a string.
2917 expand_variables(_cups_vars_t
*vars
, /* I - Variables */
2918 char *dst
, /* I - Destination string buffer */
2919 const char *src
, /* I - Source string */
2920 size_t dstsize
) /* I - Size of destination buffer */
2922 char *dstptr
, /* Pointer into destination */
2923 *dstend
, /* End of destination */
2924 temp
[256], /* Temporary string */
2925 *tempptr
; /* Pointer into temporary string */
2926 const char *value
; /* Value to substitute */
2930 dstend
= dst
+ dstsize
- 1;
2932 while (*src
&& dstptr
< dstend
)
2937 * Substitute a string/number...
2940 if (!strncmp(src
, "$$", 2))
2945 else if (!strncmp(src
, "$ENV[", 5))
2947 strlcpy(temp
, src
+ 5, sizeof(temp
));
2949 for (tempptr
= temp
; *tempptr
; tempptr
++)
2950 if (*tempptr
== ']')
2956 value
= getenv(temp
);
2957 src
+= tempptr
- temp
+ 5;
2961 strlcpy(temp
, src
+ 1, sizeof(temp
));
2963 for (tempptr
= temp
; *tempptr
; tempptr
++)
2964 if (!isalnum(*tempptr
& 255) && *tempptr
!= '-' && *tempptr
!= '_')
2970 if (!strcmp(temp
, "uri"))
2972 else if (!strcmp(temp
, "filename"))
2973 value
= vars
->filename
;
2974 else if (!strcmp(temp
, "scheme") || !strcmp(temp
, "method"))
2975 value
= vars
->scheme
;
2976 else if (!strcmp(temp
, "username"))
2977 value
= vars
->userpass
;
2978 else if (!strcmp(temp
, "hostname"))
2979 value
= vars
->hostname
;
2980 else if (!strcmp(temp
, "port"))
2982 snprintf(temp
, sizeof(temp
), "%d", vars
->port
);
2985 else if (!strcmp(temp
, "resource"))
2986 value
= vars
->resource
;
2987 else if (!strcmp(temp
, "user"))
2990 value
= get_variable(vars
, temp
);
2992 src
+= tempptr
- temp
+ 1;
3002 strlcpy(dstptr
, value
, dstend
- dstptr
+ 1);
3003 dstptr
+= strlen(dstptr
);
3015 * 'expect_matches()' - Return true if the tag matches the specification.
3018 static int /* O - 1 if matches, 0 otherwise */
3020 _cups_expect_t
*expect
, /* I - Expected attribute */
3021 ipp_tag_t value_tag
) /* I - Value tag for attribute */
3023 int match
; /* Match? */
3024 char *of_type
, /* Type name to match */
3025 *next
, /* Next name to match */
3026 sep
; /* Separator character */
3030 * If we don't expect a particular type, return immediately...
3033 if (!expect
->of_type
)
3037 * Parse the "of_type" value since the string can contain multiple attribute
3038 * types separated by "," or "|"...
3041 for (of_type
= expect
->of_type
, match
= 0; !match
&& *of_type
; of_type
= next
)
3044 * Find the next separator, and set it (temporarily) to nul if present.
3047 for (next
= of_type
; *next
&& *next
!= '|' && *next
!= ','; next
++);
3049 if ((sep
= *next
) != '\0')
3053 * Support some meta-types to make it easier to write the test file.
3056 if (!strcmp(of_type
, "text"))
3057 match
= value_tag
== IPP_TAG_TEXTLANG
|| value_tag
== IPP_TAG_TEXT
;
3058 else if (!strcmp(of_type
, "name"))
3059 match
= value_tag
== IPP_TAG_NAMELANG
|| value_tag
== IPP_TAG_NAME
;
3060 else if (!strcmp(of_type
, "collection"))
3061 match
= value_tag
== IPP_TAG_BEGIN_COLLECTION
;
3063 match
= value_tag
== ippTagValue(of_type
);
3066 * Restore the separator if we have one...
3078 * 'get_collection()' - Get a collection value from the current test file.
3081 static ipp_t
* /* O - Collection value */
3082 get_collection(_cups_vars_t
*vars
, /* I - Variables */
3083 FILE *fp
, /* I - File to read from */
3084 int *linenum
) /* IO - Line number */
3086 char token
[1024], /* Token from file */
3087 temp
[1024], /* Temporary string */
3088 attr
[128]; /* Attribute name */
3089 ipp_tag_t value
; /* Current value type */
3090 ipp_t
*col
= ippNew(); /* Collection value */
3091 ipp_attribute_t
*lastcol
= NULL
; /* Last collection attribute */
3094 while (get_token(fp
, token
, sizeof(token
), linenum
) != NULL
)
3096 if (!strcmp(token
, "}"))
3098 else if (!strcmp(token
, "{") && lastcol
)
3101 * Another collection value
3104 ipp_t
*subcol
= get_collection(vars
, fp
, linenum
);
3105 /* Collection value */
3109 ipp_attribute_t
*tempcol
; /* Pointer to new buffer */
3113 * Reallocate memory...
3116 if ((tempcol
= realloc(lastcol
, sizeof(ipp_attribute_t
) +
3117 (lastcol
->num_values
+ 1) *
3118 sizeof(ipp_value_t
))) == NULL
)
3120 print_fatal_error("Unable to allocate memory on line %d.", *linenum
);
3124 if (tempcol
!= lastcol
)
3127 * Reset pointers in the list...
3131 col
->prev
->next
= tempcol
;
3133 col
->attrs
= tempcol
;
3135 lastcol
= col
->current
= col
->last
= tempcol
;
3138 lastcol
->values
[lastcol
->num_values
].collection
= subcol
;
3139 lastcol
->num_values
++;
3144 else if (!_cups_strcasecmp(token
, "MEMBER"))
3152 if (!get_token(fp
, token
, sizeof(token
), linenum
))
3154 print_fatal_error("Missing MEMBER value tag on line %d.", *linenum
);
3158 if ((value
= ippTagValue(token
)) == IPP_TAG_ZERO
)
3160 print_fatal_error("Bad MEMBER value tag \"%s\" on line %d.", token
,
3165 if (!get_token(fp
, attr
, sizeof(attr
), linenum
))
3167 print_fatal_error("Missing MEMBER name on line %d.", *linenum
);
3171 if (!get_token(fp
, temp
, sizeof(temp
), linenum
))
3173 print_fatal_error("Missing MEMBER value on line %d.", *linenum
);
3177 expand_variables(vars
, token
, temp
, sizeof(token
));
3181 case IPP_TAG_BOOLEAN
:
3182 if (!_cups_strcasecmp(token
, "true"))
3183 ippAddBoolean(col
, IPP_TAG_ZERO
, attr
, 1);
3185 ippAddBoolean(col
, IPP_TAG_ZERO
, attr
, atoi(token
));
3188 case IPP_TAG_INTEGER
:
3190 ippAddInteger(col
, IPP_TAG_ZERO
, value
, attr
, atoi(token
));
3193 case IPP_TAG_RESOLUTION
:
3195 int xres
, /* X resolution */
3196 yres
; /* Y resolution */
3197 char units
[6]; /* Units */
3199 if (sscanf(token
, "%dx%d%5s", &xres
, &yres
, units
) != 3 ||
3200 (_cups_strcasecmp(units
, "dpi") && _cups_strcasecmp(units
, "dpc") &&
3201 _cups_strcasecmp(units
, "other")))
3203 print_fatal_error("Bad resolution value \"%s\" on line %d.",
3208 if (!_cups_strcasecmp(units
, "dpi"))
3209 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, xres
, yres
,
3211 else if (!_cups_strcasecmp(units
, "dpc"))
3212 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, xres
, yres
,
3215 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, xres
, yres
,
3220 case IPP_TAG_RANGE
:
3222 int lowers
[4], /* Lower value */
3223 uppers
[4], /* Upper values */
3224 num_vals
; /* Number of values */
3227 num_vals
= sscanf(token
, "%d-%d,%d-%d,%d-%d,%d-%d",
3228 lowers
+ 0, uppers
+ 0,
3229 lowers
+ 1, uppers
+ 1,
3230 lowers
+ 2, uppers
+ 2,
3231 lowers
+ 3, uppers
+ 3);
3233 if ((num_vals
& 1) || num_vals
== 0)
3235 print_fatal_error("Bad rangeOfInteger value \"%s\" on line %d.",
3240 ippAddRanges(col
, IPP_TAG_ZERO
, attr
, num_vals
/ 2, lowers
,
3245 case IPP_TAG_BEGIN_COLLECTION
:
3246 if (!strcmp(token
, "{"))
3248 ipp_t
*subcol
= get_collection(vars
, fp
, linenum
);
3249 /* Collection value */
3253 lastcol
= ippAddCollection(col
, IPP_TAG_ZERO
, attr
, subcol
);
3261 print_fatal_error("Bad collection value on line %d.", *linenum
);
3267 if (!strchr(token
, ','))
3268 ippAddString(col
, IPP_TAG_ZERO
, value
, attr
, NULL
, token
);
3272 * Multiple string values...
3275 int num_values
; /* Number of values */
3276 char *values
[100], /* Values */
3277 *ptr
; /* Pointer to next value */
3283 for (ptr
= strchr(token
, ','); ptr
; ptr
= strchr(ptr
, ','))
3286 values
[num_values
] = ptr
;
3290 ippAddStrings(col
, IPP_TAG_ZERO
, value
, attr
, num_values
,
3291 NULL
, (const char **)values
);
3301 * If we get here there was a parse error; free memory and return.
3313 * 'get_filename()' - Get a filename based on the current test file.
3316 static char * /* O - Filename */
3317 get_filename(const char *testfile
, /* I - Current test file */
3318 char *dst
, /* I - Destination filename */
3319 const char *src
, /* I - Source filename */
3320 size_t dstsize
) /* I - Size of destination buffer */
3322 char *dstptr
; /* Pointer into destination */
3323 _cups_globals_t
*cg
= _cupsGlobals();
3327 if (*src
== '<' && src
[strlen(src
) - 1] == '>')
3330 * Map <filename> to CUPS_DATADIR/ipptool/filename...
3333 snprintf(dst
, dstsize
, "%s/ipptool/%s", cg
->cups_datadir
, src
+ 1);
3334 dstptr
= dst
+ strlen(dst
) - 1;
3338 else if (*src
== '/' || !strchr(testfile
, '/'))
3341 * Use the path as-is...
3344 strlcpy(dst
, src
, dstsize
);
3349 * Make path relative to testfile...
3352 strlcpy(dst
, testfile
, dstsize
);
3353 if ((dstptr
= strrchr(dst
, '/')) != NULL
)
3356 dstptr
= dst
; /* Should never happen */
3358 strlcpy(dstptr
, src
, dstsize
- (dstptr
- dst
));
3366 * 'get_token()' - Get a token from a file.
3369 static char * /* O - Token from file or NULL on EOF */
3370 get_token(FILE *fp
, /* I - File to read from */
3371 char *buf
, /* I - Buffer to read into */
3372 int buflen
, /* I - Length of buffer */
3373 int *linenum
) /* IO - Current line number */
3375 int ch
, /* Character from file */
3376 quote
; /* Quoting character */
3377 char *bufptr
, /* Pointer into buffer */
3378 *bufend
; /* End of buffer */
3384 * Skip whitespace...
3387 while (isspace(ch
= getc(fp
)))
3399 else if (ch
== '\'' || ch
== '\"')
3402 * Quoted text or regular expression...
3407 bufend
= buf
+ buflen
- 1;
3409 while ((ch
= getc(fp
)) != EOF
)
3414 * Escape next character...
3417 if (bufptr
< bufend
)
3420 if ((ch
= getc(fp
)) != EOF
&& bufptr
< bufend
)
3423 else if (ch
== quote
)
3425 else if (bufptr
< bufend
)
3439 while ((ch
= getc(fp
)) != EOF
)
3448 * Whitespace delimited text...
3454 bufend
= buf
+ buflen
- 1;
3456 while ((ch
= getc(fp
)) != EOF
)
3457 if (isspace(ch
) || ch
== '#')
3459 else if (bufptr
< bufend
)
3464 else if (ch
== '\n')
3476 * 'get_variable()' - Get the value of a variable.
3479 static char * /* O - Value or NULL */
3480 get_variable(_cups_vars_t
*vars
, /* I - Variables */
3481 const char *name
) /* I - Variable name */
3483 _cups_var_t key
, /* Search key */
3484 *match
; /* Matching variable, if any */
3487 key
.name
= (char *)name
;
3488 match
= cupsArrayFind(vars
->vars
, &key
);
3490 return (match
? match
->value
: NULL
);
3495 * 'iso_date()' - Return an ISO 8601 date/time string for the given IPP dateTime
3499 static char * /* O - ISO 8601 date/time string */
3500 iso_date(ipp_uchar_t
*date
) /* I - IPP (RFC 1903) date/time value */
3502 time_t utctime
; /* UTC time since 1970 */
3503 struct tm
*utcdate
; /* UTC date/time */
3504 static char buffer
[255]; /* String buffer */
3507 utctime
= ippDateToTime(date
);
3508 utcdate
= gmtime(&utctime
);
3510 snprintf(buffer
, sizeof(buffer
), "%04d-%02d-%02dT%02d:%02d:%02dZ",
3511 utcdate
->tm_year
+ 1900, utcdate
->tm_mon
+ 1, utcdate
->tm_mday
,
3512 utcdate
->tm_hour
, utcdate
->tm_min
, utcdate
->tm_sec
);
3519 * 'password_cb()' - Password callback for authenticated tests.
3522 static const char * /* O - Password */
3523 password_cb(const char *prompt
) /* I - Prompt (unused) */
3532 * 'print_attr()' - Print an attribute on the screen.
3536 print_attr(ipp_attribute_t
*attr
, /* I - Attribute to print */
3537 ipp_tag_t
*group
) /* IO - Current group */
3539 int i
; /* Looping var */
3540 ipp_attribute_t
*colattr
; /* Collection attribute */
3543 if (Output
== _CUPS_OUTPUT_PLIST
)
3545 if (!attr
->name
|| (group
&& *group
!= attr
->group_tag
))
3551 *group
= attr
->group_tag
;
3557 print_xml_string("key", attr
->name
);
3558 if (attr
->num_values
> 1)
3561 switch (attr
->value_tag
)
3563 case IPP_TAG_INTEGER
:
3565 for (i
= 0; i
< attr
->num_values
; i
++)
3566 if (Output
== _CUPS_OUTPUT_PLIST
)
3567 printf("<integer>%d</integer>\n", attr
->values
[i
].integer
);
3569 printf("%d ", attr
->values
[i
].integer
);
3572 case IPP_TAG_BOOLEAN
:
3573 for (i
= 0; i
< attr
->num_values
; i
++)
3574 if (Output
== _CUPS_OUTPUT_PLIST
)
3575 puts(attr
->values
[i
].boolean
? "<true />" : "<false />");
3576 else if (attr
->values
[i
].boolean
)
3577 fputs("true ", stdout
);
3579 fputs("false ", stdout
);
3582 case IPP_TAG_RANGE
:
3583 for (i
= 0; i
< attr
->num_values
; i
++)
3584 if (Output
== _CUPS_OUTPUT_PLIST
)
3585 printf("<dict><key>lower</key><integer>%d</integer>"
3586 "<key>upper</key><integer>%d</integer></dict>\n",
3587 attr
->values
[i
].range
.lower
, attr
->values
[i
].range
.upper
);
3589 printf("%d-%d ", attr
->values
[i
].range
.lower
,
3590 attr
->values
[i
].range
.upper
);
3593 case IPP_TAG_RESOLUTION
:
3594 for (i
= 0; i
< attr
->num_values
; i
++)
3595 if (Output
== _CUPS_OUTPUT_PLIST
)
3596 printf("<dict><key>xres</key><integer>%d</integer>"
3597 "<key>yres</key><integer>%d</integer>"
3598 "<key>units</key><string>%s</string></dict>\n",
3599 attr
->values
[i
].resolution
.xres
,
3600 attr
->values
[i
].resolution
.yres
,
3601 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3604 printf("%dx%d%s ", attr
->values
[i
].resolution
.xres
,
3605 attr
->values
[i
].resolution
.yres
,
3606 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3611 for (i
= 0; i
< attr
->num_values
; i
++)
3612 if (Output
== _CUPS_OUTPUT_PLIST
)
3613 printf("<date>%s</date>\n", iso_date(attr
->values
[i
].date
));
3615 printf("%s ", iso_date(attr
->values
[i
].date
));
3618 case IPP_TAG_STRING
:
3621 case IPP_TAG_KEYWORD
:
3622 case IPP_TAG_CHARSET
:
3624 case IPP_TAG_MIMETYPE
:
3625 case IPP_TAG_LANGUAGE
:
3626 for (i
= 0; i
< attr
->num_values
; i
++)
3627 if (Output
== _CUPS_OUTPUT_PLIST
)
3628 print_xml_string("string", attr
->values
[i
].string
.text
);
3630 printf("\"%s\" ", attr
->values
[i
].string
.text
);
3633 case IPP_TAG_TEXTLANG
:
3634 case IPP_TAG_NAMELANG
:
3635 for (i
= 0; i
< attr
->num_values
; i
++)
3636 if (Output
== _CUPS_OUTPUT_PLIST
)
3638 fputs("<dict><key>language</key><string>", stdout
);
3639 print_xml_string(NULL
, attr
->values
[i
].string
.charset
);
3640 fputs("</string><key>string</key><string>", stdout
);
3641 print_xml_string(NULL
, attr
->values
[i
].string
.text
);
3642 puts("</string></dict>");
3645 printf("\"%s\"(%s) ", attr
->values
[i
].string
.text
,
3646 attr
->values
[i
].string
.charset
);
3649 case IPP_TAG_BEGIN_COLLECTION
:
3650 for (i
= 0; i
< attr
->num_values
; i
++)
3652 if (Output
== _CUPS_OUTPUT_PLIST
)
3655 for (colattr
= attr
->values
[i
].collection
->attrs
;
3657 colattr
= colattr
->next
)
3658 print_attr(colattr
, NULL
);
3666 print_col(attr
->values
[i
].collection
);
3672 if (Output
== _CUPS_OUTPUT_PLIST
)
3673 printf("<string><<%s>></string>\n",
3674 ippTagString(attr
->value_tag
));
3676 fputs(ippTagString(attr
->value_tag
), stdout
);
3680 if (attr
->num_values
> 1)
3685 char buffer
[8192]; /* Value buffer */
3687 if (Output
== _CUPS_OUTPUT_TEST
)
3691 puts(" -- separator --");
3695 printf(" %s (%s%s) = ", attr
->name
,
3696 attr
->num_values
> 1 ? "1setOf " : "",
3697 ippTagString(attr
->value_tag
));
3700 _ippAttrString(attr
, buffer
, sizeof(buffer
));
3707 * 'print_col()' - Print a collection attribute on the screen.
3711 print_col(ipp_t
*col
) /* I - Collection attribute to print */
3713 int i
; /* Looping var */
3714 ipp_attribute_t
*attr
; /* Current attribute in collection */
3717 fputs("{ ", stdout
);
3718 for (attr
= col
->attrs
; attr
; attr
= attr
->next
)
3720 printf("%s (%s%s) = ", attr
->name
, attr
->num_values
> 1 ? "1setOf " : "",
3721 ippTagString(attr
->value_tag
));
3723 switch (attr
->value_tag
)
3725 case IPP_TAG_INTEGER
:
3727 for (i
= 0; i
< attr
->num_values
; i
++)
3728 printf("%d ", attr
->values
[i
].integer
);
3731 case IPP_TAG_BOOLEAN
:
3732 for (i
= 0; i
< attr
->num_values
; i
++)
3733 if (attr
->values
[i
].boolean
)
3739 case IPP_TAG_NOVALUE
:
3743 case IPP_TAG_RANGE
:
3744 for (i
= 0; i
< attr
->num_values
; i
++)
3745 printf("%d-%d ", attr
->values
[i
].range
.lower
,
3746 attr
->values
[i
].range
.upper
);
3749 case IPP_TAG_RESOLUTION
:
3750 for (i
= 0; i
< attr
->num_values
; i
++)
3751 printf("%dx%d%s ", attr
->values
[i
].resolution
.xres
,
3752 attr
->values
[i
].resolution
.yres
,
3753 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3757 case IPP_TAG_STRING
:
3760 case IPP_TAG_KEYWORD
:
3761 case IPP_TAG_CHARSET
:
3763 case IPP_TAG_MIMETYPE
:
3764 case IPP_TAG_LANGUAGE
:
3765 for (i
= 0; i
< attr
->num_values
; i
++)
3766 printf("\"%s\" ", attr
->values
[i
].string
.text
);
3769 case IPP_TAG_TEXTLANG
:
3770 case IPP_TAG_NAMELANG
:
3771 for (i
= 0; i
< attr
->num_values
; i
++)
3772 printf("\"%s\",%s ", attr
->values
[i
].string
.text
,
3773 attr
->values
[i
].string
.charset
);
3776 case IPP_TAG_BEGIN_COLLECTION
:
3777 for (i
= 0; i
< attr
->num_values
; i
++)
3779 print_col(attr
->values
[i
].collection
);
3785 break; /* anti-compiler-warning-code */
3794 * 'print_csv()' - Print a line of CSV text.
3799 ipp_attribute_t
*attr
, /* I - First attribute for line */
3800 int num_displayed
, /* I - Number of attributes to display */
3801 char **displayed
, /* I - Attributes to display */
3802 size_t *widths
) /* I - Column widths */
3804 int i
; /* Looping var */
3805 size_t maxlength
; /* Max length of all columns */
3806 char *buffer
, /* String buffer */
3807 *bufptr
; /* Pointer into buffer */
3808 ipp_attribute_t
*current
; /* Current attribute */
3812 * Get the maximum string length we have to show and allocate...
3815 for (i
= 1, maxlength
= widths
[0]; i
< num_displayed
; i
++)
3816 if (widths
[i
] > maxlength
)
3817 maxlength
= widths
[i
];
3821 if ((buffer
= malloc(maxlength
)) == NULL
)
3825 * Loop through the attributes to display...
3830 for (i
= 0; i
< num_displayed
; i
++)
3837 for (current
= attr
; current
; current
= current
->next
)
3841 else if (!strcmp(current
->name
, displayed
[i
]))
3843 _ippAttrString(current
, buffer
, maxlength
);
3848 if (strchr(buffer
, ',') != NULL
|| strchr(buffer
, '\"') != NULL
||
3849 strchr(buffer
, '\\') != NULL
)
3852 for (bufptr
= buffer
; *bufptr
; bufptr
++)
3854 if (*bufptr
== '\\' || *bufptr
== '\"')
3861 fputs(buffer
, stdout
);
3867 for (i
= 0; i
< num_displayed
; i
++)
3872 fputs(displayed
[i
], stdout
);
3882 * 'print_fatal_error()' - Print a fatal error message.
3886 print_fatal_error(const char *s
, /* I - Printf-style format string */
3887 ...) /* I - Additional arguments as needed */
3889 char buffer
[10240]; /* Format buffer */
3890 va_list ap
; /* Pointer to arguments */
3894 * Format the error message...
3898 vsnprintf(buffer
, sizeof(buffer
), s
, ap
);
3905 if (Output
== _CUPS_OUTPUT_PLIST
)
3908 print_xml_trailer(0, buffer
);
3911 _cupsLangPrintf(stderr
, "ipptool: %s", buffer
);
3916 * 'print_line()' - Print a line of formatted or CSV text.
3921 ipp_attribute_t
*attr
, /* I - First attribute for line */
3922 int num_displayed
, /* I - Number of attributes to display */
3923 char **displayed
, /* I - Attributes to display */
3924 size_t *widths
) /* I - Column widths */
3926 int i
; /* Looping var */
3927 size_t maxlength
; /* Max length of all columns */
3928 char *buffer
; /* String buffer */
3929 ipp_attribute_t
*current
; /* Current attribute */
3933 * Get the maximum string length we have to show and allocate...
3936 for (i
= 1, maxlength
= widths
[0]; i
< num_displayed
; i
++)
3937 if (widths
[i
] > maxlength
)
3938 maxlength
= widths
[i
];
3942 if ((buffer
= malloc(maxlength
)) == NULL
)
3946 * Loop through the attributes to display...
3951 for (i
= 0; i
< num_displayed
; i
++)
3958 for (current
= attr
; current
; current
= current
->next
)
3962 else if (!strcmp(current
->name
, displayed
[i
]))
3964 _ippAttrString(current
, buffer
, maxlength
);
3969 printf("%*s", (int)-widths
[i
], buffer
);
3975 for (i
= 0; i
< num_displayed
; i
++)
3980 printf("%*s", (int)-widths
[i
], displayed
[i
]);
3984 for (i
= 0; i
< num_displayed
; i
++)
3989 memset(buffer
, '-', widths
[i
]);
3990 buffer
[widths
[i
]] = '\0';
3991 fputs(buffer
, stdout
);
4001 * 'print_test_error()' - Print a test error message.
4005 print_test_error(const char *s
, /* I - Printf-style format string */
4006 ...) /* I - Additional arguments as needed */
4008 char buffer
[10240]; /* Format buffer */
4009 va_list ap
; /* Pointer to arguments */
4013 * Format the error message...
4017 vsnprintf(buffer
, sizeof(buffer
), s
, ap
);
4024 if (Output
== _CUPS_OUTPUT_PLIST
)
4025 print_xml_string("string", buffer
);
4027 printf(" %s\n", buffer
);
4032 * 'print_xml_header()' - Print a standard XML plist header.
4036 print_xml_header(void)
4040 puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
4041 puts("<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
4042 "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
4043 puts("<plist version=\"1.0\">");
4045 puts("<key>Transfer</key>");
4046 printf("<string>%s</string>\n",
4047 Transfer
== _CUPS_TRANSFER_AUTO
? "auto" :
4048 Transfer
== _CUPS_TRANSFER_CHUNKED
? "chunked" : "length");
4049 puts("<key>Tests</key>");
4058 * 'print_xml_string()' - Print an XML string with escaping.
4062 print_xml_string(const char *element
, /* I - Element name or NULL */
4063 const char *s
) /* I - String to print */
4066 printf("<%s>", element
);
4071 fputs("&", stdout
);
4073 fputs("<", stdout
);
4075 fputs(">", stdout
);
4076 else if ((*s
& 0xe0) == 0xc0)
4079 * Validate UTF-8 two-byte sequence...
4082 if ((s
[1] & 0xc0) != 0x80)
4093 else if ((*s
& 0xf0) == 0xe0)
4096 * Validate UTF-8 three-byte sequence...
4099 if ((s
[1] & 0xc0) != 0x80 || (s
[2] & 0xc0) != 0x80)
4111 else if ((*s
& 0xf8) == 0xf0)
4114 * Validate UTF-8 four-byte sequence...
4117 if ((s
[1] & 0xc0) != 0x80 || (s
[2] & 0xc0) != 0x80 ||
4118 (s
[3] & 0xc0) != 0x80)
4131 else if ((*s
& 0x80) || (*s
< ' ' && !isspace(*s
& 255)))
4134 * Invalid control character...
4146 printf("</%s>\n", element
);
4151 * 'print_xml_trailer()' - Print the XML trailer with success/fail value.
4155 print_xml_trailer(int success
, /* I - 1 on success, 0 on failure */
4156 const char *message
) /* I - Error message or NULL */
4161 puts("<key>Successful</key>");
4162 puts(success
? "<true />" : "<false />");
4165 puts("<key>ErrorMessage</key>");
4166 print_xml_string("string", message
);
4177 * 'set_variable()' - Set a variable value.
4181 set_variable(_cups_vars_t
*vars
, /* I - Variables */
4182 const char *name
, /* I - Variable name */
4183 const char *value
) /* I - Value string */
4185 _cups_var_t key
, /* Search key */
4186 *var
; /* New variable */
4189 if (!_cups_strcasecmp(name
, "filename"))
4192 free(vars
->filename
);
4194 vars
->filename
= strdup(value
);
4198 key
.name
= (char *)name
;
4199 if ((var
= cupsArrayFind(vars
->vars
, &key
)) != NULL
)
4202 var
->value
= strdup(value
);
4204 else if ((var
= malloc(sizeof(_cups_var_t
))) == NULL
)
4206 print_fatal_error("Unable to allocate memory for variable \"%s\".", name
);
4211 var
->name
= strdup(name
);
4212 var
->value
= strdup(value
);
4214 cupsArrayAdd(vars
->vars
, var
);
4221 * 'sigterm_handler()' - Handle SIGINT and SIGTERM.
4225 sigterm_handler(int sig
) /* I - Signal number (unused) */
4231 signal(SIGINT
, SIG_DFL
);
4232 signal(SIGTERM
, SIG_DFL
);
4238 * 'timeout_cb()' - Handle HTTP timeouts.
4241 static int /* O - 1 to continue, 0 to cancel */
4242 timeout_cb(http_t
*http
, /* I - Connection to server (unused) */
4243 void *user_data
) /* I - User data (unused) */
4248 /* Always cancel on timeout */
4254 * 'usage()' - Show program usage.
4260 _cupsLangPuts(stderr
, _("Usage: ipptool [options] URI filename [ ... "
4262 _cupsLangPuts(stderr
, _("Options:"));
4263 _cupsLangPuts(stderr
, _(" -4 Connect using IPv4."));
4264 _cupsLangPuts(stderr
, _(" -6 Connect using IPv6."));
4265 _cupsLangPuts(stderr
, _(" -C Send requests using "
4266 "chunking (default)."));
4267 _cupsLangPuts(stderr
, _(" -E Test with TLS "
4269 _cupsLangPuts(stderr
, _(" -I Ignore errors."));
4270 _cupsLangPuts(stderr
, _(" -L Send requests using "
4271 "content-length."));
4272 _cupsLangPuts(stderr
, _(" -S Test with SSL "
4274 _cupsLangPuts(stderr
, _(" -T Set the receive/send "
4275 "timeout in seconds."));
4276 _cupsLangPuts(stderr
, _(" -V version Set default IPP "
4278 _cupsLangPuts(stderr
, _(" -X Produce XML plist instead "
4280 _cupsLangPuts(stderr
, _(" -d name=value Set named variable to "
4282 _cupsLangPuts(stderr
, _(" -f filename Set default request "
4284 _cupsLangPuts(stderr
, _(" -i seconds Repeat the last file with "
4285 "the given time interval."));
4286 _cupsLangPuts(stderr
, _(" -n count Repeat the last file the "
4287 "given number of times."));
4288 _cupsLangPuts(stderr
, _(" -q Be quiet - no output "
4290 _cupsLangPuts(stderr
, _(" -t Produce a test report."));
4291 _cupsLangPuts(stderr
, _(" -v Show all attributes sent "
4299 * 'validate_attr()' - Determine whether an attribute is valid.
4302 static int /* O - 1 if valid, 0 otherwise */
4303 validate_attr(ipp_attribute_t
*attr
, /* I - Attribute to validate */
4304 int print
) /* I - 1 = report issues to stdout */
4306 int i
; /* Looping var */
4307 char scheme
[64], /* Scheme from URI */
4308 userpass
[256], /* Username/password from URI */
4309 hostname
[256], /* Hostname from URI */
4310 resource
[1024]; /* Resource from URI */
4311 int port
, /* Port number from URI */
4312 uri_status
, /* URI separation status */
4313 valid
= 1; /* Is the attribute valid? */
4314 const char *ptr
; /* Pointer into string */
4315 ipp_attribute_t
*colattr
; /* Collection attribute */
4316 regex_t re
; /* Regular expression */
4317 ipp_uchar_t
*date
; /* Current date value */
4328 * Validate the attribute name.
4331 for (ptr
= attr
->name
; *ptr
; ptr
++)
4332 if (!isalnum(*ptr
& 255) && *ptr
!= '-' && *ptr
!= '.' && *ptr
!= '_')
4335 if (*ptr
|| ptr
== attr
->name
)
4340 print_test_error("\"%s\": Bad attribute name - invalid character (RFC "
4341 "2911 section 4.1.3).", attr
->name
);
4344 if ((ptr
- attr
->name
) > 255)
4349 print_test_error("\"%s\": Bad attribute name - bad length (RFC 2911 "
4350 "section 4.1.3).", attr
->name
);
4353 switch (attr
->value_tag
)
4355 case IPP_TAG_INTEGER
:
4358 case IPP_TAG_BOOLEAN
:
4359 for (i
= 0; i
< attr
->num_values
; i
++)
4361 if (attr
->values
[i
].boolean
!= 0 &&
4362 attr
->values
[i
].boolean
!= 1)
4367 print_test_error("\"%s\": Bad boolen value %d (RFC 2911 section "
4368 "4.1.10).", attr
->name
, attr
->values
[i
].boolean
);
4376 for (i
= 0; i
< attr
->num_values
; i
++)
4378 if (attr
->values
[i
].integer
< 1)
4383 print_test_error("\"%s\": Bad enum value %d - out of range "
4384 "(RFC 2911 section 4.1.4).", attr
->name
,
4385 attr
->values
[i
].integer
);
4392 case IPP_TAG_STRING
:
4393 for (i
= 0; i
< attr
->num_values
; i
++)
4395 if (attr
->values
[i
].unknown
.length
> 1023)
4400 print_test_error("\"%s\": Bad octetString value - bad length %d "
4401 "(RFC 2911 section 4.1.10).", attr
->name
,
4402 attr
->values
[i
].unknown
.length
);
4410 for (i
= 0; i
< attr
->num_values
; i
++)
4412 date
= attr
->values
[i
].date
;
4414 if (date
[2] < 1 || date
[2] > 12)
4419 print_test_error("\"%s\": Bad dateTime month %u (RFC 2911 "
4420 "section 4.1.13).", attr
->name
, date
[2]);
4425 if (date
[3] < 1 || date
[3] > 31)
4430 print_test_error("\"%s\": Bad dateTime day %u (RFC 2911 "
4431 "section 4.1.13).", attr
->name
, date
[3]);
4441 print_test_error("\"%s\": Bad dateTime hours %u (RFC 2911 "
4442 "section 4.1.13).", attr
->name
, date
[4]);
4452 print_test_error("\"%s\": Bad dateTime minutes %u (RFC 2911 "
4453 "section 4.1.13).", attr
->name
, date
[5]);
4463 print_test_error("\"%s\": Bad dateTime seconds %u (RFC 2911 "
4464 "section 4.1.13).", attr
->name
, date
[6]);
4474 print_test_error("\"%s\": Bad dateTime deciseconds %u (RFC 2911 "
4475 "section 4.1.13).", attr
->name
, date
[7]);
4480 if (date
[8] != '-' && date
[8] != '+')
4485 print_test_error("\"%s\": Bad dateTime UTC sign '%c' (RFC 2911 "
4486 "section 4.1.13).", attr
->name
, date
[8]);
4496 print_test_error("\"%s\": Bad dateTime UTC hours %u (RFC 2911 "
4497 "section 4.1.13).", attr
->name
, date
[9]);
4507 print_test_error("\"%s\": Bad dateTime UTC minutes %u (RFC 2911 "
4508 "section 4.1.13).", attr
->name
, date
[10]);
4515 case IPP_TAG_RESOLUTION
:
4516 for (i
= 0; i
< attr
->num_values
; i
++)
4518 if (attr
->values
[i
].resolution
.xres
<= 0)
4523 print_test_error("\"%s\": Bad resolution value %dx%d%s - cross "
4524 "feed resolution must be positive (RFC 2911 "
4525 "section 4.1.13).", attr
->name
,
4526 attr
->values
[i
].resolution
.xres
,
4527 attr
->values
[i
].resolution
.yres
,
4528 attr
->values
[i
].resolution
.units
==
4529 IPP_RES_PER_INCH
? "dpi" :
4530 attr
->values
[i
].resolution
.units
==
4531 IPP_RES_PER_CM
? "dpc" : "unknown");
4536 if (attr
->values
[i
].resolution
.yres
<= 0)
4541 print_test_error("\"%s\": Bad resolution value %dx%d%s - feed "
4542 "resolution must be positive (RFC 2911 section "
4543 "4.1.13).", attr
->name
,
4544 attr
->values
[i
].resolution
.xres
,
4545 attr
->values
[i
].resolution
.yres
,
4546 attr
->values
[i
].resolution
.units
==
4547 IPP_RES_PER_INCH
? "dpi" :
4548 attr
->values
[i
].resolution
.units
==
4549 IPP_RES_PER_CM
? "dpc" : "unknown");
4554 if (attr
->values
[i
].resolution
.units
!= IPP_RES_PER_INCH
&&
4555 attr
->values
[i
].resolution
.units
!= IPP_RES_PER_CM
)
4560 print_test_error("\"%s\": Bad resolution value %dx%d%s - bad "
4561 "units value (RFC 2911 section 4.1.13).",
4562 attr
->name
, attr
->values
[i
].resolution
.xres
,
4563 attr
->values
[i
].resolution
.yres
,
4564 attr
->values
[i
].resolution
.units
==
4565 IPP_RES_PER_INCH
? "dpi" :
4566 attr
->values
[i
].resolution
.units
==
4567 IPP_RES_PER_CM
? "dpc" : "unknown");
4574 case IPP_TAG_RANGE
:
4575 for (i
= 0; i
< attr
->num_values
; i
++)
4577 if (attr
->values
[i
].range
.lower
> attr
->values
[i
].range
.upper
)
4582 print_test_error("\"%s\": Bad rangeOfInteger value %d-%d - lower "
4583 "greater than upper (RFC 2911 section 4.1.13).",
4584 attr
->name
, attr
->values
[i
].range
.lower
,
4585 attr
->values
[i
].range
.upper
);
4592 case IPP_TAG_BEGIN_COLLECTION
:
4593 for (i
= 0; i
< attr
->num_values
; i
++)
4595 for (colattr
= attr
->values
[i
].collection
->attrs
;
4597 colattr
= colattr
->next
)
4599 if (!validate_attr(colattr
, 0))
4606 if (colattr
&& print
)
4608 print_test_error("\"%s\": Bad collection value.", attr
->name
);
4612 validate_attr(colattr
, print
);
4613 colattr
= colattr
->next
;
4620 case IPP_TAG_TEXTLANG
:
4621 for (i
= 0; i
< attr
->num_values
; i
++)
4623 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
4625 if ((*ptr
& 0xe0) == 0xc0)
4628 if ((*ptr
& 0xc0) != 0x80)
4631 else if ((*ptr
& 0xf0) == 0xe0)
4634 if ((*ptr
& 0xc0) != 0x80)
4637 if ((*ptr
& 0xc0) != 0x80)
4640 else if ((*ptr
& 0xf8) == 0xf0)
4643 if ((*ptr
& 0xc0) != 0x80)
4646 if ((*ptr
& 0xc0) != 0x80)
4649 if ((*ptr
& 0xc0) != 0x80)
4652 else if (*ptr
& 0x80)
4661 print_test_error("\"%s\": Bad text value \"%s\" - bad UTF-8 "
4662 "sequence (RFC 2911 section 4.1.1).", attr
->name
,
4663 attr
->values
[i
].string
.text
);
4668 if ((ptr
- attr
->values
[i
].string
.text
) > 1023)
4673 print_test_error("\"%s\": Bad text value \"%s\" - bad length %d "
4674 "(RFC 2911 section 4.1.1).", attr
->name
,
4675 attr
->values
[i
].string
.text
,
4676 (int)strlen(attr
->values
[i
].string
.text
));
4684 case IPP_TAG_NAMELANG
:
4685 for (i
= 0; i
< attr
->num_values
; i
++)
4687 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
4689 if ((*ptr
& 0xe0) == 0xc0)
4692 if ((*ptr
& 0xc0) != 0x80)
4695 else if ((*ptr
& 0xf0) == 0xe0)
4698 if ((*ptr
& 0xc0) != 0x80)
4701 if ((*ptr
& 0xc0) != 0x80)
4704 else if ((*ptr
& 0xf8) == 0xf0)
4707 if ((*ptr
& 0xc0) != 0x80)
4710 if ((*ptr
& 0xc0) != 0x80)
4713 if ((*ptr
& 0xc0) != 0x80)
4716 else if (*ptr
& 0x80)
4725 print_test_error("\"%s\": Bad name value \"%s\" - bad UTF-8 "
4726 "sequence (RFC 2911 section 4.1.2).", attr
->name
,
4727 attr
->values
[i
].string
.text
);
4732 if ((ptr
- attr
->values
[i
].string
.text
) > 1023)
4737 print_test_error("\"%s\": Bad name value \"%s\" - bad length %d "
4738 "(RFC 2911 section 4.1.2).", attr
->name
,
4739 attr
->values
[i
].string
.text
,
4740 (int)strlen(attr
->values
[i
].string
.text
));
4747 case IPP_TAG_KEYWORD
:
4748 for (i
= 0; i
< attr
->num_values
; i
++)
4750 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
4751 if (!isalnum(*ptr
& 255) && *ptr
!= '-' && *ptr
!= '.' &&
4755 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
4760 print_test_error("\"%s\": Bad keyword value \"%s\" - invalid "
4761 "character (RFC 2911 section 4.1.3).",
4762 attr
->name
, attr
->values
[i
].string
.text
);
4767 if ((ptr
- attr
->values
[i
].string
.text
) > 255)
4772 print_test_error("\"%s\": Bad keyword value \"%s\" - bad "
4773 "length %d (RFC 2911 section 4.1.3).",
4774 attr
->name
, attr
->values
[i
].string
.text
,
4775 (int)strlen(attr
->values
[i
].string
.text
));
4783 for (i
= 0; i
< attr
->num_values
; i
++)
4785 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
,
4786 attr
->values
[i
].string
.text
,
4787 scheme
, sizeof(scheme
),
4788 userpass
, sizeof(userpass
),
4789 hostname
, sizeof(hostname
),
4790 &port
, resource
, sizeof(resource
));
4792 if (uri_status
< HTTP_URI_OK
)
4797 print_test_error("\"%s\": Bad URI value \"%s\" - %s "
4798 "(RFC 2911 section 4.1.5).", attr
->name
,
4799 attr
->values
[i
].string
.text
,
4800 URIStatusStrings
[uri_status
-
4801 HTTP_URI_OVERFLOW
]);
4806 if (strlen(attr
->values
[i
].string
.text
) > 1023)
4811 print_test_error("\"%s\": Bad URI value \"%s\" - bad length %d "
4812 "(RFC 2911 section 4.1.5).", attr
->name
,
4813 attr
->values
[i
].string
.text
,
4814 (int)strlen(attr
->values
[i
].string
.text
));
4821 case IPP_TAG_URISCHEME
:
4822 for (i
= 0; i
< attr
->num_values
; i
++)
4824 ptr
= attr
->values
[i
].string
.text
;
4825 if (islower(*ptr
& 255))
4827 for (ptr
++; *ptr
; ptr
++)
4828 if (!islower(*ptr
& 255) && !isdigit(*ptr
& 255) &&
4829 *ptr
!= '+' && *ptr
!= '-' && *ptr
!= '.')
4833 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
4838 print_test_error("\"%s\": Bad uriScheme value \"%s\" - bad "
4839 "characters (RFC 2911 section 4.1.6).",
4840 attr
->name
, attr
->values
[i
].string
.text
);
4845 if ((ptr
- attr
->values
[i
].string
.text
) > 63)
4850 print_test_error("\"%s\": Bad uriScheme value \"%s\" - bad "
4851 "length %d (RFC 2911 section 4.1.6).",
4852 attr
->name
, attr
->values
[i
].string
.text
,
4853 (int)strlen(attr
->values
[i
].string
.text
));
4860 case IPP_TAG_CHARSET
:
4861 for (i
= 0; i
< attr
->num_values
; i
++)
4863 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
4864 if (!isprint(*ptr
& 255) || isupper(*ptr
& 255) ||
4865 isspace(*ptr
& 255))
4868 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
4873 print_test_error("\"%s\": Bad charset value \"%s\" - bad "
4874 "characters (RFC 2911 section 4.1.7).",
4875 attr
->name
, attr
->values
[i
].string
.text
);
4880 if ((ptr
- attr
->values
[i
].string
.text
) > 40)
4885 print_test_error("\"%s\": Bad charset value \"%s\" - bad "
4886 "length %d (RFC 2911 section 4.1.7).",
4887 attr
->name
, attr
->values
[i
].string
.text
,
4888 (int)strlen(attr
->values
[i
].string
.text
));
4895 case IPP_TAG_LANGUAGE
:
4897 * The following regular expression is derived from the ABNF for
4898 * language tags in RFC 4646. All I can say is that this is the
4899 * easiest way to check the values...
4902 if ((i
= regcomp(&re
,
4904 "(([a-z]{2,3}(-[a-z][a-z][a-z]){0,3})|[a-z]{4,8})"
4906 "(-[a-z][a-z][a-z][a-z]){0,1}" /* script */
4907 "(-([a-z][a-z]|[0-9][0-9][0-9])){0,1}" /* region */
4908 "(-([a-z]{5,8}|[0-9][0-9][0-9]))*" /* variant */
4909 "(-[a-wy-z](-[a-z0-9]{2,8})+)*" /* extension */
4910 "(-x(-[a-z0-9]{1,8})+)*" /* privateuse */
4912 "x(-[a-z0-9]{1,8})+" /* privateuse */
4914 "[a-z]{1,3}(-[a-z][0-9]{2,8}){1,2}" /* grandfathered */
4916 REG_NOSUB
| REG_EXTENDED
)) != 0)
4918 char temp
[256]; /* Temporary error string */
4920 regerror(i
, &re
, temp
, sizeof(temp
));
4921 print_fatal_error("Unable to compile naturalLanguage regular "
4922 "expression: %s.", temp
);
4925 for (i
= 0; i
< attr
->num_values
; i
++)
4927 if (regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
4932 print_test_error("\"%s\": Bad naturalLanguage value \"%s\" - bad "
4933 "characters (RFC 2911 section 4.1.8).",
4934 attr
->name
, attr
->values
[i
].string
.text
);
4939 if (strlen(attr
->values
[i
].string
.text
) > 63)
4944 print_test_error("\"%s\": Bad naturalLanguage value \"%s\" - bad "
4945 "length %d (RFC 2911 section 4.1.8).",
4946 attr
->name
, attr
->values
[i
].string
.text
,
4947 (int)strlen(attr
->values
[i
].string
.text
));
4956 case IPP_TAG_MIMETYPE
:
4958 * The following regular expression is derived from the ABNF for
4959 * language tags in RFC 2045 and 4288. All I can say is that this is
4960 * the easiest way to check the values...
4963 if ((i
= regcomp(&re
,
4965 "[-a-zA-Z0-9!#$&.+^_]{1,127}" /* type-name */
4967 "[-a-zA-Z0-9!#$&.+^_]{1,127}" /* subtype-name */
4968 "(;[-a-zA-Z0-9!#$&.+^_]{1,127}=" /* parameter= */
4969 "([-a-zA-Z0-9!#$&.+^_]{1,127}|\"[^\"]*\"))*"
4972 REG_NOSUB
| REG_EXTENDED
)) != 0)
4974 char temp
[256]; /* Temporary error string */
4976 regerror(i
, &re
, temp
, sizeof(temp
));
4977 print_fatal_error("Unable to compile mimeMediaType regular "
4978 "expression: %s.", temp
);
4981 for (i
= 0; i
< attr
->num_values
; i
++)
4983 if (regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
4988 print_test_error("\"%s\": Bad mimeMediaType value \"%s\" - bad "
4989 "characters (RFC 2911 section 4.1.9).",
4990 attr
->name
, attr
->values
[i
].string
.text
);
4995 if (strlen(attr
->values
[i
].string
.text
) > 255)
5000 print_test_error("\"%s\": Bad mimeMediaType value \"%s\" - bad "
5001 "length %d (RFC 2911 section 4.1.9).",
5002 attr
->name
, attr
->values
[i
].string
.text
,
5003 (int)strlen(attr
->values
[i
].string
.text
));
5019 * 'with_value()' - Test a WITH-VALUE predicate.
5022 static int /* O - 1 on match, 0 on non-match */
5023 with_value(char *value
, /* I - Value string */
5024 int regex
, /* I - Value is a regular expression */
5025 ipp_attribute_t
*attr
, /* I - Attribute to compare */
5026 int report
, /* I - 1 = report failures */
5027 char *matchbuf
, /* I - Buffer to hold matching value */
5028 size_t matchlen
) /* I - Length of match buffer */
5030 int i
; /* Looping var */
5031 char *valptr
; /* Pointer into value */
5037 * NULL matches everything.
5040 if (!value
|| !*value
)
5044 * Compare the value string to the attribute value.
5047 switch (attr
->value_tag
)
5049 case IPP_TAG_INTEGER
:
5051 for (i
= 0; i
< attr
->num_values
; i
++)
5053 char op
, /* Comparison operator */
5054 *nextptr
; /* Next pointer */
5055 int intvalue
; /* Integer value */
5059 if (!strncmp(valptr
, "no-value,", 9))
5062 while (isspace(*valptr
& 255) || isdigit(*valptr
& 255) ||
5063 *valptr
== '-' || *valptr
== ',' || *valptr
== '<' ||
5064 *valptr
== '=' || *valptr
== '>')
5067 while (*valptr
&& !isdigit(*valptr
& 255) && *valptr
!= '-')
5069 if (*valptr
== '<' || *valptr
== '>' || *valptr
== '=')
5077 intvalue
= strtol(valptr
, &nextptr
, 0);
5078 if (nextptr
== valptr
)
5085 if (attr
->values
[i
].integer
== intvalue
)
5087 snprintf(matchbuf
, matchlen
, "%d", attr
->values
[i
].integer
);
5092 if (attr
->values
[i
].integer
< intvalue
)
5094 snprintf(matchbuf
, matchlen
, "%d", attr
->values
[i
].integer
);
5099 if (attr
->values
[i
].integer
> intvalue
)
5101 snprintf(matchbuf
, matchlen
, "%d", attr
->values
[i
].integer
);
5111 for (i
= 0; i
< attr
->num_values
; i
++)
5112 print_test_error("GOT: %s=%d", attr
->name
, attr
->values
[i
].integer
);
5116 case IPP_TAG_RANGE
:
5117 for (i
= 0; i
< attr
->num_values
; i
++)
5119 char op
, /* Comparison operator */
5120 *nextptr
; /* Next pointer */
5121 int intvalue
; /* Integer value */
5125 if (!strncmp(valptr
, "no-value,", 9))
5128 while (isspace(*valptr
& 255) || isdigit(*valptr
& 255) ||
5129 *valptr
== '-' || *valptr
== ',' || *valptr
== '<' ||
5130 *valptr
== '=' || *valptr
== '>')
5133 while (*valptr
&& !isdigit(*valptr
& 255) && *valptr
!= '-')
5135 if (*valptr
== '<' || *valptr
== '>' || *valptr
== '=')
5143 intvalue
= strtol(valptr
, &nextptr
, 0);
5144 if (nextptr
== valptr
)
5151 if (attr
->values
[i
].range
.lower
== intvalue
||
5152 attr
->values
[i
].range
.upper
== intvalue
)
5154 snprintf(matchbuf
, matchlen
, "%d-%d",
5155 attr
->values
[i
].range
.lower
,
5156 attr
->values
[i
].range
.upper
);
5161 if (attr
->values
[i
].range
.upper
< intvalue
)
5163 snprintf(matchbuf
, matchlen
, "%d-%d",
5164 attr
->values
[i
].range
.lower
,
5165 attr
->values
[i
].range
.upper
);
5170 if (attr
->values
[i
].range
.lower
> intvalue
)
5172 snprintf(matchbuf
, matchlen
, "%d-%d",
5173 attr
->values
[i
].range
.lower
,
5174 attr
->values
[i
].range
.upper
);
5184 for (i
= 0; i
< attr
->num_values
; i
++)
5185 print_test_error("GOT: %s=%d-%d", attr
->name
,
5186 attr
->values
[i
].range
.lower
,
5187 attr
->values
[i
].range
.upper
);
5191 case IPP_TAG_BOOLEAN
:
5192 for (i
= 0; i
< attr
->num_values
; i
++)
5194 if (!strcmp(value
, "true") == attr
->values
[i
].boolean
)
5196 strlcpy(matchbuf
, value
, matchlen
);
5203 for (i
= 0; i
< attr
->num_values
; i
++)
5204 print_test_error("GOT: %s=%s", attr
->name
,
5205 attr
->values
[i
].boolean
? "true" : "false");
5209 case IPP_TAG_NOVALUE
:
5210 if (!strcmp(value
, "no-value") || !strncmp(value
, "no-value,", 9))
5212 strlcpy(matchbuf
, "no-value", matchlen
);
5218 case IPP_TAG_CHARSET
:
5219 case IPP_TAG_KEYWORD
:
5220 case IPP_TAG_LANGUAGE
:
5221 case IPP_TAG_MIMETYPE
:
5223 case IPP_TAG_NAMELANG
:
5225 case IPP_TAG_TEXTLANG
:
5227 case IPP_TAG_URISCHEME
:
5231 * Value is an extended, case-sensitive POSIX regular expression...
5234 regex_t re
; /* Regular expression */
5236 if ((i
= regcomp(&re
, value
, REG_EXTENDED
| REG_NOSUB
)) != 0)
5238 char temp
[256]; /* Temporary string */
5240 regerror(i
, &re
, temp
, sizeof(temp
));
5242 print_fatal_error("Unable to compile WITH-VALUE regular expression "
5243 "\"%s\" - %s", value
, temp
);
5248 * See if ALL of the values match the given regular expression.
5251 for (i
= 0; i
< attr
->num_values
; i
++)
5253 if (regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
5256 print_test_error("GOT: %s=\"%s\"", attr
->name
,
5257 attr
->values
[i
].string
.text
);
5265 if (i
== attr
->num_values
)
5266 strlcpy(matchbuf
, attr
->values
[0].string
.text
, matchlen
);
5268 return (i
== attr
->num_values
);
5273 * Value is a literal string, see if at least one value matches the
5277 for (i
= 0; i
< attr
->num_values
; i
++)
5279 if (!strcmp(value
, attr
->values
[i
].string
.text
))
5281 strlcpy(matchbuf
, attr
->values
[i
].string
.text
, matchlen
);
5288 for (i
= 0; i
< attr
->num_values
; i
++)
5289 print_test_error("GOT: %s=\"%s\"", attr
->name
,
5290 attr
->values
[i
].string
.text
);