]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
148d3699 | 2 | * ipptool command for CUPS. |
ef416fc2 | 3 | * |
08102776 MS |
4 | * Copyright © 2007-2018 by Apple Inc. |
5 | * Copyright © 1997-2007 by Easy Software Products. | |
ef416fc2 | 6 | * |
08102776 MS |
7 | * Licensed under Apache License v2.0. See the file "LICENSE" for more |
8 | * information. | |
ef416fc2 | 9 | */ |
10 | ||
11 | /* | |
12 | * Include necessary headers... | |
13 | */ | |
14 | ||
71e16022 | 15 | #include <cups/cups-private.h> |
f8b3a85b | 16 | #include <regex.h> |
85dda01c | 17 | #include <sys/stat.h> |
24a06ed3 | 18 | #ifdef _WIN32 |
9fcdd250 | 19 | # include <windows.h> |
33c9220c MS |
20 | # ifndef R_OK |
21 | # define R_OK 0 | |
22 | # endif /* !R_OK */ | |
db8b865d | 23 | #else |
83e08001 | 24 | # include <signal.h> |
9fcdd250 | 25 | # include <termios.h> |
24a06ed3 | 26 | #endif /* _WIN32 */ |
5a662dc0 MS |
27 | #ifndef O_BINARY |
28 | # define O_BINARY 0 | |
29 | #endif /* !O_BINARY */ | |
30 | ||
31 | ||
32 | /* | |
33 | * Types... | |
34 | */ | |
35 | ||
aaf19ab0 | 36 | typedef enum _cups_transfer_e /**** How to send request data ****/ |
f8b3a85b MS |
37 | { |
38 | _CUPS_TRANSFER_AUTO, /* Chunk for files, length for static */ | |
39 | _CUPS_TRANSFER_CHUNKED, /* Chunk always */ | |
40 | _CUPS_TRANSFER_LENGTH /* Length always */ | |
41 | } _cups_transfer_t; | |
42 | ||
aaf19ab0 MS |
43 | typedef enum _cups_output_e /**** Output mode ****/ |
44 | { | |
45 | _CUPS_OUTPUT_QUIET, /* No output */ | |
46 | _CUPS_OUTPUT_TEST, /* Traditional CUPS test output */ | |
47 | _CUPS_OUTPUT_PLIST, /* XML plist test output */ | |
6646208b | 48 | _CUPS_OUTPUT_IPPSERVER, /* ippserver attribute file output */ |
aaf19ab0 MS |
49 | _CUPS_OUTPUT_LIST, /* Tabular list output */ |
50 | _CUPS_OUTPUT_CSV /* Comma-separated values output */ | |
51 | } _cups_output_t; | |
52 | ||
82cc1f9a MS |
53 | typedef enum _cups_with_e /**** WITH flags ****/ |
54 | { | |
55 | _CUPS_WITH_LITERAL = 0, /* Match string is a literal value */ | |
56 | _CUPS_WITH_ALL = 1, /* Must match all values */ | |
890a10b7 MS |
57 | _CUPS_WITH_REGEX = 2, /* Match string is a regular expression */ |
58 | _CUPS_WITH_HOSTNAME = 4, /* Match string is a URI hostname */ | |
59 | _CUPS_WITH_RESOURCE = 8, /* Match string is a URI resource */ | |
60 | _CUPS_WITH_SCHEME = 16 /* Match string is a URI scheme */ | |
82cc1f9a MS |
61 | } _cups_with_t; |
62 | ||
5a662dc0 MS |
63 | typedef struct _cups_expect_s /**** Expected attribute info ****/ |
64 | { | |
f8b3a85b | 65 | int optional, /* Optional attribute? */ |
e83646d5 MS |
66 | not_expect, /* Don't expect attribute? */ |
67 | expect_all; /* Expect all attributes to match/not match */ | |
f8b3a85b MS |
68 | char *name, /* Attribute name */ |
69 | *of_type, /* Type name */ | |
70 | *same_count_as, /* Parallel attribute name */ | |
71 | *if_defined, /* Only required if variable defined */ | |
7cf5915e | 72 | *if_not_defined, /* Only required if variable is not defined */ |
030ae6a1 | 73 | *with_value, /* Attribute must include this value */ |
a21c36fa | 74 | *with_value_from, /* Attribute must have one of the values in this attribute */ |
030ae6a1 MS |
75 | *define_match, /* Variable to define on match */ |
76 | *define_no_match, /* Variable to define on no-match */ | |
77 | *define_value; /* Variable to define with value */ | |
a29fd7dd MS |
78 | int repeat_limit, /* Maximum number of times to repeat */ |
79 | repeat_match, /* Repeat test on match */ | |
83e08001 | 80 | repeat_no_match, /* Repeat test on no match */ |
82cc1f9a | 81 | with_flags, /* WITH flags */ |
f8b3a85b MS |
82 | count; /* Expected count if > 0 */ |
83 | ipp_tag_t in_group; /* IN-GROUP value */ | |
5a662dc0 | 84 | } _cups_expect_t; |
ef416fc2 | 85 | |
f8b3a85b MS |
86 | typedef struct _cups_status_s /**** Status info ****/ |
87 | { | |
88 | ipp_status_t status; /* Expected status code */ | |
89 | char *if_defined, /* Only if variable is defined */ | |
0fa6c7fa MS |
90 | *if_not_defined, /* Only if variable is not defined */ |
91 | *define_match, /* Variable to define on match */ | |
92 | *define_no_match, /* Variable to define on no-match */ | |
93 | *define_value; /* Variable to define with value */ | |
a29fd7dd MS |
94 | int repeat_limit, /* Maximum number of times to repeat */ |
95 | repeat_match, /* Repeat the test when it does not match */ | |
83e08001 | 96 | repeat_no_match; /* Repeat the test when it matches */ |
f8b3a85b MS |
97 | } _cups_status_t; |
98 | ||
08102776 | 99 | typedef struct _cups_testdata_s /**** Test Data ****/ |
f8b3a85b | 100 | { |
08102776 MS |
101 | /* Global Options */ |
102 | http_encryption_t encryption; /* Encryption for connection */ | |
1106b00e | 103 | int family; /* Address family */ |
08102776 MS |
104 | _cups_output_t output; /* Output mode */ |
105 | int stop_after_include_error; | |
106 | /* Stop after include errors? */ | |
107 | double timeout; /* Timeout for connection */ | |
108 | int validate_headers, /* Validate HTTP headers in response? */ | |
109 | verbosity; /* Show all attributes? */ | |
110 | ||
111 | /* Test Defaults */ | |
112 | int def_ignore_errors; /* Default IGNORE-ERRORS value */ | |
113 | _cups_transfer_t def_transfer; /* Default TRANSFER value */ | |
114 | int def_version; /* Default IPP version */ | |
115 | ||
116 | /* Global State */ | |
117 | http_t *http; /* HTTP connection to printer/server */ | |
118 | cups_file_t *outfile; /* Output file */ | |
119 | int show_header, /* Show the test header? */ | |
120 | xml_header; /* 1 if XML plist header was written */ | |
8e2e09c1 MS |
121 | int pass, /* Have we passed all tests? */ |
122 | test_count, /* Number of tests (total) */ | |
08102776 MS |
123 | pass_count, /* Number of tests that passed */ |
124 | fail_count, /* Number of tests that failed */ | |
125 | skip_count; /* Number of tests that were skipped */ | |
126 | ||
127 | /* Per-Test State */ | |
128 | cups_array_t *errors; /* Errors array */ | |
8e2e09c1 MS |
129 | int prev_pass, /* Result of previous test */ |
130 | skip_previous; /* Skip on previous test failure? */ | |
08102776 MS |
131 | char compression[16]; /* COMPRESSION value */ |
132 | useconds_t delay; /* Initial delay */ | |
133 | int num_displayed; /* Number of displayed attributes */ | |
134 | char *displayed[200]; /* Displayed attributes */ | |
135 | int num_expects; /* Number of expected attributes */ | |
136 | _cups_expect_t expects[200], /* Expected attributes */ | |
137 | *expect, /* Current expected attribute */ | |
138 | *last_expect; /* Last EXPECT (for predicates) */ | |
8e2e09c1 MS |
139 | char file[1024], /* Data filename */ |
140 | file_id[1024]; /* File identifier */ | |
08102776 MS |
141 | int ignore_errors; /* Ignore test failures? */ |
142 | char name[1024]; /* Test name */ | |
08102776 MS |
143 | useconds_t repeat_interval; /* Repeat interval (delay) */ |
144 | int request_id; /* Current request ID */ | |
145 | char resource[512]; /* Resource for request */ | |
146 | int skip_test, /* Skip this test? */ | |
147 | num_statuses; /* Number of valid status codes */ | |
148 | _cups_status_t statuses[100], /* Valid status codes */ | |
149 | *last_status; /* Last STATUS (for predicates) */ | |
150 | char test_id[1024]; /* Test identifier */ | |
151 | _cups_transfer_t transfer; /* To chunk or not to chunk */ | |
152 | int version; /* IPP version number to use */ | |
153 | } _cups_testdata_t; | |
f8b3a85b | 154 | |
ef416fc2 | 155 | |
e00b005a | 156 | /* |
157 | * Globals... | |
158 | */ | |
159 | ||
08102776 | 160 | static int Cancel = 0; /* Cancel test? */ |
e00b005a | 161 | |
162 | ||
ef416fc2 | 163 | /* |
164 | * Local functions... | |
165 | */ | |
166 | ||
a32af27c | 167 | static void add_stringf(cups_array_t *a, const char *s, ...) _CUPS_FORMAT(2, 3); |
2a75f21b | 168 | static int compare_uris(const char *a, const char *b); |
a32af27c | 169 | static void copy_hex_string(char *buffer, unsigned char *data, int datalen, size_t bufsize); |
8e2e09c1 MS |
170 | static int do_test(_ipp_file_t *f, _ipp_vars_t *vars, _cups_testdata_t *data); |
171 | static int do_tests(const char *testfile, _ipp_vars_t *vars, _cups_testdata_t *data); | |
08102776 | 172 | static int error_cb(_ipp_file_t *f, _cups_testdata_t *data, const char *error); |
5a662dc0 | 173 | static int expect_matches(_cups_expect_t *expect, ipp_tag_t value_tag); |
1b01c393 MS |
174 | static char *get_filename(const char *testfile, char *dst, const char *src, size_t dstsize); |
175 | static const char *get_string(ipp_attribute_t *attr, int element, int flags, char *buffer, size_t bufsize); | |
08102776 | 176 | static void init_data(_cups_testdata_t *data); |
6646208b | 177 | static char *iso_date(const ipp_uchar_t *date); |
9fcdd250 | 178 | static void pause_message(const char *message); |
8e2e09c1 | 179 | static void print_attr(cups_file_t *outfile, int output, ipp_attribute_t *attr, ipp_tag_t *group); |
a492d345 | 180 | static void print_csv(_cups_testdata_t *data, ipp_t *ipp, ipp_attribute_t *attr, int num_displayed, char **displayed, size_t *widths); |
a32af27c | 181 | static void print_fatal_error(_cups_testdata_t *data, const char *s, ...) _CUPS_FORMAT(2, 3); |
08102776 MS |
182 | static void print_ippserver_attr(_cups_testdata_t *data, ipp_attribute_t *attr, int indent); |
183 | static void print_ippserver_string(_cups_testdata_t *data, const char *s, size_t len); | |
a492d345 | 184 | static void print_line(_cups_testdata_t *data, ipp_t *ipp, ipp_attribute_t *attr, int num_displayed, char **displayed, size_t *widths); |
08102776 | 185 | static void print_xml_header(_cups_testdata_t *data); |
8e2e09c1 | 186 | static void print_xml_string(cups_file_t *outfile, const char *element, const char *s); |
08102776 | 187 | static void print_xml_trailer(_cups_testdata_t *data, int success, const char *message); |
24a06ed3 | 188 | #ifndef _WIN32 |
85dda01c | 189 | static void sigterm_handler(int sig); |
24a06ed3 | 190 | #endif /* _WIN32 */ |
10d09e33 | 191 | static int timeout_cb(http_t *http, void *user_data); |
08102776 | 192 | static int token_cb(_ipp_file_t *f, _ipp_vars_t *vars, _cups_testdata_t *data, const char *token); |
a32af27c | 193 | static void usage(void) _CUPS_NORETURN; |
ea4dcf9f | 194 | static const char *with_flags_string(int flags); |
8e2e09c1 | 195 | static int with_value(_cups_testdata_t *data, cups_array_t *errors, char *value, int flags, ipp_attribute_t *attr, char *matchbuf, size_t matchlen); |
a21c36fa | 196 | static int with_value_from(cups_array_t *errors, ipp_attribute_t *fromattr, ipp_attribute_t *attr, char *matchbuf, size_t matchlen); |
ef416fc2 | 197 | |
198 | ||
199 | /* | |
200 | * 'main()' - Parse options and do tests. | |
201 | */ | |
202 | ||
203 | int /* O - Exit status */ | |
5a662dc0 | 204 | main(int argc, /* I - Number of command-line args */ |
ef416fc2 | 205 | char *argv[]) /* I - Command-line arguments */ |
206 | { | |
f8b3a85b MS |
207 | int i; /* Looping var */ |
208 | int status; /* Status of tests... */ | |
209 | char *opt, /* Current option */ | |
210 | name[1024], /* Name/value buffer */ | |
aaf19ab0 MS |
211 | *value, /* Pointer to value */ |
212 | filename[1024], /* Real filename */ | |
8e2e09c1 | 213 | testname[1024]; /* Real test filename */ |
83e08001 MS |
214 | const char *ext, /* Extension on filename */ |
215 | *testfile; /* Test file to use */ | |
10d09e33 | 216 | int interval, /* Test interval in microseconds */ |
aaf19ab0 | 217 | repeat; /* Repeat count */ |
08102776 MS |
218 | _cups_testdata_t data; /* Test data */ |
219 | _ipp_vars_t vars; /* Variables */ | |
aaf19ab0 MS |
220 | _cups_globals_t *cg = _cupsGlobals(); |
221 | /* Global data */ | |
222 | ||
f8b3a85b | 223 | |
24a06ed3 | 224 | #ifndef _WIN32 |
85dda01c MS |
225 | /* |
226 | * Catch SIGINT and SIGTERM... | |
227 | */ | |
228 | ||
229 | signal(SIGINT, sigterm_handler); | |
230 | signal(SIGTERM, sigterm_handler); | |
24a06ed3 | 231 | #endif /* !_WIN32 */ |
f8b3a85b MS |
232 | |
233 | /* | |
234 | * Initialize the locale and variables... | |
235 | */ | |
236 | ||
237 | _cupsSetLocale(argv); | |
ef416fc2 | 238 | |
08102776 MS |
239 | init_data(&data); |
240 | ||
a166e933 | 241 | _ippVarsInit(&vars, NULL, (_ipp_ferror_cb_t)error_cb, (_ipp_ftoken_cb_t)token_cb); |
ef416fc2 | 242 | |
243 | /* | |
244 | * We need at least: | |
245 | * | |
aaf19ab0 | 246 | * ipptool URI testfile |
ef416fc2 | 247 | */ |
248 | ||
8ca02f3c | 249 | interval = 0; |
aaf19ab0 MS |
250 | repeat = 0; |
251 | status = 0; | |
252 | testfile = NULL; | |
e00b005a | 253 | |
254 | for (i = 1; i < argc; i ++) | |
ef416fc2 | 255 | { |
9475ec92 MS |
256 | if (!strcmp(argv[i], "--help")) |
257 | { | |
258 | usage(); | |
259 | } | |
6646208b MS |
260 | else if (!strcmp(argv[i], "--ippserver")) |
261 | { | |
262 | i ++; | |
263 | ||
264 | if (i >= argc) | |
265 | { | |
266 | _cupsLangPuts(stderr, _("ipptool: Missing filename for \"--ippserver\".")); | |
267 | usage(); | |
268 | } | |
269 | ||
08102776 | 270 | if (data.outfile != cupsFileStdout()) |
6646208b MS |
271 | usage(); |
272 | ||
08102776 | 273 | if ((data.outfile = cupsFileOpen(argv[i], "w")) == NULL) |
6646208b MS |
274 | { |
275 | _cupsLangPrintf(stderr, _("%s: Unable to open \"%s\": %s"), "ipptool", argv[i], strerror(errno)); | |
276 | exit(1); | |
277 | } | |
278 | ||
08102776 | 279 | data.output = _CUPS_OUTPUT_IPPSERVER; |
6646208b | 280 | } |
9475ec92 MS |
281 | else if (!strcmp(argv[i], "--stop-after-include-error")) |
282 | { | |
8e2e09c1 | 283 | data.stop_after_include_error = 1; |
9475ec92 MS |
284 | } |
285 | else if (!strcmp(argv[i], "--version")) | |
286 | { | |
287 | puts(CUPS_SVERSION); | |
288 | return (0); | |
289 | } | |
290 | else if (argv[i][0] == '-') | |
e00b005a | 291 | { |
5a662dc0 | 292 | for (opt = argv[i] + 1; *opt; opt ++) |
b94498cf | 293 | { |
5a662dc0 MS |
294 | switch (*opt) |
295 | { | |
1106b00e | 296 | case '4' : /* Connect using IPv4 only */ |
08102776 | 297 | data.family = AF_INET; |
1106b00e MS |
298 | break; |
299 | ||
300 | #ifdef AF_INET6 | |
301 | case '6' : /* Connect using IPv6 only */ | |
08102776 | 302 | data.family = AF_INET6; |
1106b00e MS |
303 | break; |
304 | #endif /* AF_INET6 */ | |
305 | ||
aaf19ab0 | 306 | case 'C' : /* Enable HTTP chunking */ |
8e2e09c1 | 307 | data.def_transfer = _CUPS_TRANSFER_CHUNKED; |
aaf19ab0 MS |
308 | break; |
309 | ||
310 | case 'E' : /* Encrypt with TLS */ | |
f8b3a85b | 311 | #ifdef HAVE_SSL |
08102776 | 312 | data.encryption = HTTP_ENCRYPT_REQUIRED; |
f8b3a85b | 313 | #else |
0837b7e8 | 314 | _cupsLangPrintf(stderr, _("%s: Sorry, no encryption support."), |
f8b3a85b MS |
315 | argv[0]); |
316 | #endif /* HAVE_SSL */ | |
317 | break; | |
318 | ||
030ae6a1 | 319 | case 'I' : /* Ignore errors */ |
8e2e09c1 | 320 | data.def_ignore_errors = 1; |
030ae6a1 MS |
321 | break; |
322 | ||
aaf19ab0 | 323 | case 'L' : /* Disable HTTP chunking */ |
8e2e09c1 | 324 | data.def_transfer = _CUPS_TRANSFER_LENGTH; |
aaf19ab0 MS |
325 | break; |
326 | ||
9fcdd250 MS |
327 | case 'P' : /* Output to plist file */ |
328 | i ++; | |
329 | ||
330 | if (i >= argc) | |
331 | { | |
332 | _cupsLangPrintf(stderr, _("%s: Missing filename for \"-P\"."), "ipptool"); | |
333 | usage(); | |
334 | } | |
335 | ||
08102776 | 336 | if (data.outfile != cupsFileStdout()) |
9fcdd250 MS |
337 | usage(); |
338 | ||
08102776 | 339 | if ((data.outfile = cupsFileOpen(argv[i], "w")) == NULL) |
9fcdd250 MS |
340 | { |
341 | _cupsLangPrintf(stderr, _("%s: Unable to open \"%s\": %s"), "ipptool", argv[i], strerror(errno)); | |
342 | exit(1); | |
343 | } | |
344 | ||
08102776 | 345 | data.output = _CUPS_OUTPUT_PLIST; |
9fcdd250 MS |
346 | |
347 | if (interval || repeat) | |
348 | { | |
b0206260 | 349 | _cupsLangPuts(stderr, _("ipptool: \"-i\" and \"-n\" are incompatible with \"-P\" and \"-X\".")); |
9fcdd250 MS |
350 | usage(); |
351 | } | |
352 | break; | |
353 | ||
aaf19ab0 MS |
354 | case 'S' : /* Encrypt with SSL */ |
355 | #ifdef HAVE_SSL | |
08102776 | 356 | data.encryption = HTTP_ENCRYPT_ALWAYS; |
aaf19ab0 | 357 | #else |
0837b7e8 | 358 | _cupsLangPrintf(stderr, _("%s: Sorry, no encryption support."), |
aaf19ab0 MS |
359 | argv[0]); |
360 | #endif /* HAVE_SSL */ | |
361 | break; | |
362 | ||
10d09e33 MS |
363 | case 'T' : /* Set timeout */ |
364 | i ++; | |
365 | ||
366 | if (i >= argc) | |
367 | { | |
70752071 MS |
368 | _cupsLangPrintf(stderr, |
369 | _("%s: Missing timeout for \"-T\"."), | |
370 | "ipptool"); | |
10d09e33 MS |
371 | usage(); |
372 | } | |
373 | ||
08102776 | 374 | data.timeout = _cupsStrScand(argv[i], NULL, localeconv()); |
10d09e33 MS |
375 | break; |
376 | ||
f8b3a85b MS |
377 | case 'V' : /* Set IPP version */ |
378 | i ++; | |
379 | ||
380 | if (i >= argc) | |
381 | { | |
70752071 MS |
382 | _cupsLangPrintf(stderr, |
383 | _("%s: Missing version for \"-V\"."), | |
384 | "ipptool"); | |
f8b3a85b MS |
385 | usage(); |
386 | } | |
387 | ||
388 | if (!strcmp(argv[i], "1.0")) | |
8e2e09c1 MS |
389 | { |
390 | data.def_version = 10; | |
391 | } | |
f8b3a85b | 392 | else if (!strcmp(argv[i], "1.1")) |
8e2e09c1 MS |
393 | { |
394 | data.def_version = 11; | |
395 | } | |
f8b3a85b | 396 | else if (!strcmp(argv[i], "2.0")) |
8e2e09c1 MS |
397 | { |
398 | data.def_version = 20; | |
399 | } | |
f8b3a85b | 400 | else if (!strcmp(argv[i], "2.1")) |
8e2e09c1 MS |
401 | { |
402 | data.def_version = 21; | |
403 | } | |
f8b3a85b | 404 | else if (!strcmp(argv[i], "2.2")) |
8e2e09c1 MS |
405 | { |
406 | data.def_version = 22; | |
407 | } | |
f8b3a85b MS |
408 | else |
409 | { | |
8e2e09c1 | 410 | _cupsLangPrintf(stderr, _("%s: Bad version %s for \"-V\"."), "ipptool", argv[i]); |
f8b3a85b MS |
411 | usage(); |
412 | } | |
413 | break; | |
414 | ||
415 | case 'X' : /* Produce XML output */ | |
08102776 | 416 | data.output = _CUPS_OUTPUT_PLIST; |
f8b3a85b | 417 | |
aaf19ab0 | 418 | if (interval || repeat) |
f8b3a85b | 419 | { |
b0206260 | 420 | _cupsLangPuts(stderr, _("ipptool: \"-i\" and \"-n\" are incompatible with \"-P\" and \"-X\".")); |
f8b3a85b MS |
421 | usage(); |
422 | } | |
423 | break; | |
424 | ||
aaf19ab0 | 425 | case 'c' : /* CSV output */ |
08102776 | 426 | data.output = _CUPS_OUTPUT_CSV; |
5a662dc0 | 427 | break; |
b94498cf | 428 | |
5a662dc0 MS |
429 | case 'd' : /* Define a variable */ |
430 | i ++; | |
8ca02f3c | 431 | |
5a662dc0 MS |
432 | if (i >= argc) |
433 | { | |
f8b3a85b | 434 | _cupsLangPuts(stderr, |
0837b7e8 | 435 | _("ipptool: Missing name=value for \"-d\".")); |
5a662dc0 MS |
436 | usage(); |
437 | } | |
f8b3a85b MS |
438 | |
439 | strlcpy(name, argv[i], sizeof(name)); | |
440 | if ((value = strchr(name, '=')) != NULL) | |
441 | *value++ = '\0'; | |
5a662dc0 | 442 | else |
f8b3a85b MS |
443 | value = name + strlen(name); |
444 | ||
08102776 | 445 | _ippVarsSet(&vars, name, value); |
f8b3a85b MS |
446 | break; |
447 | ||
448 | case 'f' : /* Set the default test filename */ | |
449 | i ++; | |
450 | ||
451 | if (i >= argc) | |
452 | { | |
453 | _cupsLangPuts(stderr, | |
0837b7e8 | 454 | _("ipptool: Missing filename for \"-f\".")); |
f8b3a85b MS |
455 | usage(); |
456 | } | |
457 | ||
9b66acc5 | 458 | if (access(argv[i], 0)) |
aaf19ab0 | 459 | { |
9b66acc5 MS |
460 | /* |
461 | * Try filename.gz... | |
462 | */ | |
463 | ||
464 | snprintf(filename, sizeof(filename), "%s.gz", argv[i]); | |
12f89d24 | 465 | if (access(filename, 0) && filename[0] != '/' |
24a06ed3 | 466 | #ifdef _WIN32 |
12f89d24 | 467 | && (!isalpha(filename[0] & 255) || filename[1] != ':') |
24a06ed3 | 468 | #endif /* _WIN32 */ |
12f89d24 | 469 | ) |
9b66acc5 | 470 | { |
8e2e09c1 | 471 | snprintf(filename, sizeof(filename), "%s/ipptool/%s", cg->cups_datadir, argv[i]); |
9b66acc5 MS |
472 | if (access(filename, 0)) |
473 | { | |
8e2e09c1 | 474 | snprintf(filename, sizeof(filename), "%s/ipptool/%s.gz", cg->cups_datadir, argv[i]); |
9b66acc5 | 475 | if (access(filename, 0)) |
8e2e09c1 | 476 | strlcpy(filename, argv[i], sizeof(filename)); |
9b66acc5 | 477 | } |
9b66acc5 | 478 | } |
9b66acc5 | 479 | } |
83e08001 | 480 | else |
8e2e09c1 MS |
481 | strlcpy(filename, argv[i], sizeof(filename)); |
482 | ||
483 | _ippVarsSet(&vars, "filename", filename); | |
83e08001 | 484 | |
8e2e09c1 | 485 | if ((ext = strrchr(filename, '.')) != NULL) |
83e08001 MS |
486 | { |
487 | /* | |
488 | * Guess the MIME media type based on the extension... | |
489 | */ | |
490 | ||
491 | if (!_cups_strcasecmp(ext, ".gif")) | |
08102776 | 492 | _ippVarsSet(&vars, "filetype", "image/gif"); |
83e08001 | 493 | else if (!_cups_strcasecmp(ext, ".htm") || |
9b66acc5 MS |
494 | !_cups_strcasecmp(ext, ".htm.gz") || |
495 | !_cups_strcasecmp(ext, ".html") || | |
496 | !_cups_strcasecmp(ext, ".html.gz")) | |
08102776 | 497 | _ippVarsSet(&vars, "filetype", "text/html"); |
9fcdd250 MS |
498 | else if (!_cups_strcasecmp(ext, ".jpg") || |
499 | !_cups_strcasecmp(ext, ".jpeg")) | |
08102776 | 500 | _ippVarsSet(&vars, "filetype", "image/jpeg"); |
a29fd7dd MS |
501 | else if (!_cups_strcasecmp(ext, ".pcl") || |
502 | !_cups_strcasecmp(ext, ".pcl.gz")) | |
08102776 | 503 | _ippVarsSet(&vars, "filetype", "application/vnd.hp-PCL"); |
83e08001 | 504 | else if (!_cups_strcasecmp(ext, ".pdf")) |
08102776 | 505 | _ippVarsSet(&vars, "filetype", "application/pdf"); |
83e08001 | 506 | else if (!_cups_strcasecmp(ext, ".png")) |
08102776 | 507 | _ippVarsSet(&vars, "filetype", "image/png"); |
9b66acc5 MS |
508 | else if (!_cups_strcasecmp(ext, ".ps") || |
509 | !_cups_strcasecmp(ext, ".ps.gz")) | |
08102776 | 510 | _ippVarsSet(&vars, "filetype", "application/postscript"); |
db8b865d MS |
511 | else if (!_cups_strcasecmp(ext, ".pwg") || |
512 | !_cups_strcasecmp(ext, ".pwg.gz") || | |
513 | !_cups_strcasecmp(ext, ".ras") || | |
9b66acc5 | 514 | !_cups_strcasecmp(ext, ".ras.gz")) |
08102776 | 515 | _ippVarsSet(&vars, "filetype", "image/pwg-raster"); |
6a87b7d7 MS |
516 | else if (!_cups_strcasecmp(ext, ".tif") || |
517 | !_cups_strcasecmp(ext, ".tiff")) | |
08102776 | 518 | _ippVarsSet(&vars, "filetype", "image/tiff"); |
9b66acc5 MS |
519 | else if (!_cups_strcasecmp(ext, ".txt") || |
520 | !_cups_strcasecmp(ext, ".txt.gz")) | |
08102776 | 521 | _ippVarsSet(&vars, "filetype", "text/plain"); |
6a87b7d7 MS |
522 | else if (!_cups_strcasecmp(ext, ".urf") || |
523 | !_cups_strcasecmp(ext, ".urf.gz")) | |
08102776 | 524 | _ippVarsSet(&vars, "filetype", "image/urf"); |
83e08001 | 525 | else if (!_cups_strcasecmp(ext, ".xps")) |
08102776 | 526 | _ippVarsSet(&vars, "filetype", "application/openxps"); |
aaf19ab0 | 527 | else |
08102776 | 528 | _ippVarsSet(&vars, "filetype", "application/octet-stream"); |
aaf19ab0 MS |
529 | } |
530 | else | |
83e08001 MS |
531 | { |
532 | /* | |
533 | * Use the "auto-type" MIME media type... | |
534 | */ | |
535 | ||
08102776 | 536 | _ippVarsSet(&vars, "filetype", "application/octet-stream"); |
83e08001 | 537 | } |
5a662dc0 MS |
538 | break; |
539 | ||
345e10ca | 540 | case 'h' : /* Validate response headers */ |
8e2e09c1 | 541 | data.validate_headers = 1; |
345e10ca MS |
542 | break; |
543 | ||
5a662dc0 | 544 | case 'i' : /* Test every N seconds */ |
aaf19ab0 | 545 | i ++; |
5a662dc0 MS |
546 | |
547 | if (i >= argc) | |
548 | { | |
8e2e09c1 | 549 | _cupsLangPuts(stderr, _("ipptool: Missing seconds for \"-i\".")); |
5a662dc0 MS |
550 | usage(); |
551 | } | |
552 | else | |
10d09e33 | 553 | { |
8e2e09c1 | 554 | interval = (int)(_cupsStrScand(argv[i], NULL, localeconv()) * 1000000.0); |
10d09e33 MS |
555 | if (interval <= 0) |
556 | { | |
8e2e09c1 | 557 | _cupsLangPuts(stderr, _("ipptool: Invalid seconds for \"-i\".")); |
10d09e33 MS |
558 | usage(); |
559 | } | |
560 | } | |
f8b3a85b | 561 | |
08102776 | 562 | if ((data.output == _CUPS_OUTPUT_PLIST || data.output == _CUPS_OUTPUT_IPPSERVER) && interval) |
f8b3a85b | 563 | { |
6646208b | 564 | _cupsLangPuts(stderr, _("ipptool: \"-i\" and \"-n\" are incompatible with \"--ippserver\", \"-P\", and \"-X\".")); |
f8b3a85b MS |
565 | usage(); |
566 | } | |
5a662dc0 MS |
567 | break; |
568 | ||
aaf19ab0 | 569 | case 'l' : /* List as a table */ |
08102776 | 570 | data.output = _CUPS_OUTPUT_LIST; |
aaf19ab0 MS |
571 | break; |
572 | ||
573 | case 'n' : /* Repeat count */ | |
574 | i ++; | |
575 | ||
576 | if (i >= argc) | |
577 | { | |
8e2e09c1 | 578 | _cupsLangPuts(stderr, _("ipptool: Missing count for \"-n\".")); |
aaf19ab0 MS |
579 | usage(); |
580 | } | |
581 | else | |
582 | repeat = atoi(argv[i]); | |
583 | ||
08102776 | 584 | if ((data.output == _CUPS_OUTPUT_PLIST || data.output == _CUPS_OUTPUT_IPPSERVER) && repeat) |
aaf19ab0 | 585 | { |
6646208b | 586 | _cupsLangPuts(stderr, _("ipptool: \"-i\" and \"-n\" are incompatible with \"--ippserver\", \"-P\", and \"-X\".")); |
aaf19ab0 MS |
587 | usage(); |
588 | } | |
589 | break; | |
590 | ||
591 | case 'q' : /* Be quiet */ | |
08102776 | 592 | data.output = _CUPS_OUTPUT_QUIET; |
aaf19ab0 MS |
593 | break; |
594 | ||
595 | case 't' : /* CUPS test output */ | |
08102776 | 596 | data.output = _CUPS_OUTPUT_TEST; |
f8b3a85b MS |
597 | break; |
598 | ||
5a662dc0 | 599 | case 'v' : /* Be verbose */ |
8e2e09c1 | 600 | data.verbosity ++; |
5a662dc0 MS |
601 | break; |
602 | ||
603 | default : | |
08102776 | 604 | _cupsLangPrintf(stderr, _("%s: Unknown option \"-%c\"."), "ipptool", *opt); |
5a662dc0 | 605 | usage(); |
5a662dc0 | 606 | } |
8ca02f3c | 607 | } |
e00b005a | 608 | } |
7cf5915e MS |
609 | else if (!strncmp(argv[i], "ipp://", 6) || !strncmp(argv[i], "http://", 7) |
610 | #ifdef HAVE_SSL | |
08102776 | 611 | || !strncmp(argv[i], "ipps://", 7) || !strncmp(argv[i], "https://", 8) |
7cf5915e MS |
612 | #endif /* HAVE_SSL */ |
613 | ) | |
e00b005a | 614 | { |
615 | /* | |
616 | * Set URI... | |
617 | */ | |
ef416fc2 | 618 | |
f8b3a85b | 619 | if (vars.uri) |
5a662dc0 | 620 | { |
0837b7e8 | 621 | _cupsLangPuts(stderr, _("ipptool: May only specify a single URI.")); |
5a662dc0 MS |
622 | usage(); |
623 | } | |
e00b005a | 624 | |
7cf5915e MS |
625 | #ifdef HAVE_SSL |
626 | if (!strncmp(argv[i], "ipps://", 7) || !strncmp(argv[i], "https://", 8)) | |
08102776 | 627 | data.encryption = HTTP_ENCRYPT_ALWAYS; |
7cf5915e MS |
628 | #endif /* HAVE_SSL */ |
629 | ||
08102776 | 630 | if (!_ippVarsSet(&vars, "uri", argv[i])) |
f8b3a85b | 631 | { |
08102776 | 632 | _cupsLangPrintf(stderr, _("ipptool: Bad URI \"%s\"."), argv[i]); |
f8b3a85b MS |
633 | return (1); |
634 | } | |
635 | ||
08102776 MS |
636 | if (vars.username[0] && vars.password) |
637 | cupsSetPasswordCB2(_ippVarsPasswordCB, &vars); | |
e00b005a | 638 | } |
639 | else | |
640 | { | |
641 | /* | |
642 | * Run test... | |
643 | */ | |
644 | ||
f8b3a85b MS |
645 | if (!vars.uri) |
646 | { | |
aaf19ab0 | 647 | _cupsLangPuts(stderr, _("ipptool: URI required before test file.")); |
1b6d468c | 648 | _cupsLangPuts(stderr, argv[i]); |
f8b3a85b MS |
649 | usage(); |
650 | } | |
651 | ||
12f89d24 | 652 | if (access(argv[i], 0) && argv[i][0] != '/' |
24a06ed3 | 653 | #ifdef _WIN32 |
12f89d24 | 654 | && (!isalpha(argv[i][0] & 255) || argv[i][1] != ':') |
24a06ed3 | 655 | #endif /* _WIN32 */ |
12f89d24 | 656 | ) |
aaf19ab0 | 657 | { |
8e2e09c1 | 658 | snprintf(testname, sizeof(testname), "%s/ipptool/%s", cg->cups_datadir, argv[i]); |
aaf19ab0 MS |
659 | if (access(testname, 0)) |
660 | testfile = argv[i]; | |
661 | else | |
662 | testfile = testname; | |
663 | } | |
664 | else | |
665 | testfile = argv[i]; | |
e00b005a | 666 | |
8e2e09c1 | 667 | if (!do_tests(testfile, &vars, &data)) |
f8b3a85b | 668 | status = 1; |
e00b005a | 669 | } |
670 | } | |
ef416fc2 | 671 | |
f8b3a85b | 672 | if (!vars.uri || !testfile) |
5a662dc0 | 673 | usage(); |
ef416fc2 | 674 | |
8ca02f3c | 675 | /* |
676 | * Loop if the interval is set... | |
677 | */ | |
678 | ||
08102776 | 679 | if (data.output == _CUPS_OUTPUT_PLIST) |
8e2e09c1 | 680 | print_xml_trailer(&data, !status, NULL); |
10d09e33 | 681 | else if (interval > 0 && repeat > 0) |
aaf19ab0 MS |
682 | { |
683 | while (repeat > 1) | |
684 | { | |
7e86f2f6 | 685 | usleep((useconds_t)interval); |
8e2e09c1 | 686 | do_tests(testfile, &vars, &data); |
aaf19ab0 MS |
687 | repeat --; |
688 | } | |
689 | } | |
10d09e33 | 690 | else if (interval > 0) |
8ca02f3c | 691 | { |
692 | for (;;) | |
693 | { | |
7e86f2f6 | 694 | usleep((useconds_t)interval); |
8e2e09c1 | 695 | do_tests(testfile, &vars, &data); |
8ca02f3c | 696 | } |
697 | } | |
9fcdd250 | 698 | |
8e2e09c1 | 699 | if ((data.output == _CUPS_OUTPUT_TEST || (data.output == _CUPS_OUTPUT_PLIST && data.outfile)) && data.test_count > 1) |
9b66acc5 MS |
700 | { |
701 | /* | |
702 | * Show a summary report if there were multiple tests... | |
703 | */ | |
704 | ||
8e2e09c1 | 705 | cupsFilePrintf(cupsFileStdout(), "\nSummary: %d tests, %d passed, %d failed, %d skipped\nScore: %d%%\n", data.test_count, data.pass_count, data.fail_count, data.skip_count, 100 * (data.pass_count + data.skip_count) / data.test_count); |
9b66acc5 | 706 | } |
8ca02f3c | 707 | |
8e2e09c1 | 708 | cupsFileClose(data.outfile); |
35fc2243 MS |
709 | |
710 | /* | |
ef416fc2 | 711 | * Exit... |
712 | */ | |
713 | ||
e00b005a | 714 | return (status); |
ef416fc2 | 715 | } |
716 | ||
f8b3a85b | 717 | |
dcb445bc MS |
718 | /* |
719 | * 'add_stringf()' - Add a formatted string to an array. | |
720 | */ | |
721 | ||
722 | static void | |
723 | add_stringf(cups_array_t *a, /* I - Array */ | |
724 | const char *s, /* I - Printf-style format string */ | |
725 | ...) /* I - Additional args as needed */ | |
726 | { | |
727 | char buffer[10240]; /* Format buffer */ | |
728 | va_list ap; /* Argument pointer */ | |
729 | ||
730 | ||
731 | /* | |
732 | * Don't bother is the array is NULL... | |
733 | */ | |
734 | ||
735 | if (!a) | |
736 | return; | |
737 | ||
738 | /* | |
739 | * Format the message... | |
740 | */ | |
741 | ||
742 | va_start(ap, s); | |
743 | vsnprintf(buffer, sizeof(buffer), s, ap); | |
744 | va_end(ap); | |
745 | ||
746 | /* | |
747 | * Add it to the array... | |
748 | */ | |
749 | ||
750 | cupsArrayAdd(a, buffer); | |
751 | } | |
752 | ||
753 | ||
2a75f21b MS |
754 | /* |
755 | * 'compare_uris()' - Compare two URIs... | |
756 | */ | |
757 | ||
758 | static int /* O - Result of comparison */ | |
759 | compare_uris(const char *a, /* I - First URI */ | |
760 | const char *b) /* I - Second URI */ | |
761 | { | |
762 | char ascheme[32], /* Components of first URI */ | |
763 | auserpass[256], | |
764 | ahost[256], | |
765 | aresource[256]; | |
766 | int aport; | |
767 | char bscheme[32], /* Components of second URI */ | |
768 | buserpass[256], | |
769 | bhost[256], | |
770 | bresource[256]; | |
771 | int bport; | |
772 | char *ptr; /* Pointer into string */ | |
773 | int result; /* Result of comparison */ | |
774 | ||
775 | ||
776 | /* | |
777 | * Separate the URIs into their components... | |
778 | */ | |
779 | ||
780 | if (httpSeparateURI(HTTP_URI_CODING_ALL, a, ascheme, sizeof(ascheme), auserpass, sizeof(auserpass), ahost, sizeof(ahost), &aport, aresource, sizeof(aresource)) < HTTP_URI_STATUS_OK) | |
781 | return (-1); | |
782 | ||
783 | if (httpSeparateURI(HTTP_URI_CODING_ALL, b, bscheme, sizeof(bscheme), buserpass, sizeof(buserpass), bhost, sizeof(bhost), &bport, bresource, sizeof(bresource)) < HTTP_URI_STATUS_OK) | |
784 | return (-1); | |
785 | ||
786 | /* | |
787 | * Strip trailing dots from the host components, if present... | |
788 | */ | |
789 | ||
790 | if ((ptr = ahost + strlen(ahost) - 1) > ahost && *ptr == '.') | |
791 | *ptr = '\0'; | |
792 | ||
793 | if ((ptr = bhost + strlen(bhost) - 1) > bhost && *ptr == '.') | |
794 | *ptr = '\0'; | |
795 | ||
796 | /* | |
797 | * Compare each component... | |
798 | */ | |
799 | ||
800 | if ((result = _cups_strcasecmp(ascheme, bscheme)) != 0) | |
801 | return (result); | |
802 | ||
803 | if ((result = strcmp(auserpass, buserpass)) != 0) | |
804 | return (result); | |
805 | ||
806 | if ((result = _cups_strcasecmp(ahost, bhost)) != 0) | |
807 | return (result); | |
808 | ||
809 | if (aport != bport) | |
810 | return (aport - bport); | |
811 | ||
812 | if (!_cups_strcasecmp(ascheme, "mailto") || !_cups_strcasecmp(ascheme, "urn")) | |
813 | return (_cups_strcasecmp(aresource, bresource)); | |
814 | else | |
815 | return (strcmp(aresource, bresource)); | |
816 | } | |
817 | ||
818 | ||
a32af27c MS |
819 | /* |
820 | * 'copy_hex_string()' - Copy an octetString to a C string and encode as hex if | |
821 | * needed. | |
822 | */ | |
823 | ||
824 | static void | |
825 | copy_hex_string(char *buffer, /* I - String buffer */ | |
826 | unsigned char *data, /* I - octetString data */ | |
827 | int datalen, /* I - octetString length */ | |
828 | size_t bufsize) /* I - Size of string buffer */ | |
829 | { | |
830 | char *bufptr, /* Pointer into string buffer */ | |
831 | *bufend = buffer + bufsize - 2; | |
832 | /* End of string buffer */ | |
833 | unsigned char *dataptr, /* Pointer into octetString data */ | |
834 | *dataend = data + datalen; | |
835 | /* End of octetString data */ | |
836 | static const char *hexdigits = "0123456789ABCDEF"; | |
837 | /* Hex digits */ | |
838 | ||
839 | ||
840 | /* | |
841 | * First see if there are any non-ASCII bytes in the octetString... | |
842 | */ | |
843 | ||
844 | for (dataptr = data; dataptr < dataend; dataptr ++) | |
845 | if (*dataptr < 0x20 || *dataptr >= 0x7f) | |
846 | break; | |
847 | ||
848 | if (*dataptr) | |
849 | { | |
850 | /* | |
851 | * Yes, encode as hex... | |
852 | */ | |
853 | ||
854 | *buffer = '<'; | |
855 | ||
856 | for (bufptr = buffer + 1, dataptr = data; bufptr < bufend && dataptr < dataend; dataptr ++) | |
857 | { | |
858 | *bufptr++ = hexdigits[*dataptr >> 4]; | |
859 | *bufptr++ = hexdigits[*dataptr & 15]; | |
860 | } | |
861 | ||
862 | if (bufptr < bufend) | |
863 | *bufptr++ = '>'; | |
864 | ||
865 | *bufptr = '\0'; | |
866 | } | |
867 | else | |
868 | { | |
869 | /* | |
870 | * No, copy as a string... | |
871 | */ | |
872 | ||
873 | if ((size_t)datalen > bufsize) | |
874 | datalen = (int)bufsize - 1; | |
875 | ||
876 | memcpy(buffer, data, datalen); | |
877 | buffer[datalen] = '\0'; | |
878 | } | |
879 | } | |
880 | ||
881 | ||
f8b3a85b | 882 | /* |
08102776 | 883 | * 'do_test()' - Do a single test from the test file. |
f8b3a85b MS |
884 | */ |
885 | ||
08102776 | 886 | static int /* O - 1 on success, 0 on failure */ |
8e2e09c1 | 887 | do_test(_ipp_file_t *f, /* I - IPP data file */ |
08102776 | 888 | _ipp_vars_t *vars, /* I - IPP variables */ |
8e2e09c1 | 889 | _cups_testdata_t *data) /* I - Test data */ |
ef416fc2 | 890 | |
ef416fc2 | 891 | { |
8e2e09c1 MS |
892 | int i, /* Looping var */ |
893 | status_ok, /* Did we get a matching status? */ | |
894 | repeat_count = 0, /* Repeat count */ | |
895 | repeat_test; /* Repeat the test? */ | |
896 | _cups_expect_t *expect; /* Current expected attribute */ | |
897 | ipp_t *request, /* IPP request */ | |
898 | *response; /* IPP response */ | |
85dda01c MS |
899 | size_t length; /* Length of IPP request */ |
900 | http_status_t status; /* HTTP status */ | |
8e2e09c1 | 901 | cups_array_t *a; /* Duplicate attribute array */ |
ef416fc2 | 902 | ipp_tag_t group; /* Current group */ |
5a662dc0 | 903 | ipp_attribute_t *attrptr, /* Attribute pointer */ |
8e2e09c1 MS |
904 | *found; /* Found attribute */ |
905 | char temp[1024]; /* Temporary string */ | |
906 | cups_file_t *reqfile; /* File to send */ | |
907 | ssize_t bytes; /* Bytes read/written */ | |
908 | char buffer[131072]; /* Copy buffer */ | |
aaf19ab0 | 909 | size_t widths[200]; /* Width of columns */ |
dcb445bc | 910 | const char *error; /* Current error */ |
ef416fc2 | 911 | |
912 | ||
8e2e09c1 MS |
913 | if (Cancel) |
914 | return (0); | |
915 | ||
ef416fc2 | 916 | /* |
8e2e09c1 | 917 | * Take over control of the attributes in the request... |
ef416fc2 | 918 | */ |
919 | ||
8e2e09c1 MS |
920 | request = f->attrs; |
921 | f->attrs = NULL; | |
ef416fc2 | 922 | |
923 | /* | |
8e2e09c1 | 924 | * Submit the IPP request... |
ef416fc2 | 925 | */ |
926 | ||
8e2e09c1 | 927 | data->test_count ++; |
1106b00e | 928 | |
8e2e09c1 MS |
929 | ippSetVersion(request, data->version / 10, data->version % 10); |
930 | ippSetRequestId(request, data->request_id); | |
ef416fc2 | 931 | |
8e2e09c1 MS |
932 | if (data->output == _CUPS_OUTPUT_PLIST) |
933 | { | |
934 | cupsFilePuts(data->outfile, "<dict>\n"); | |
935 | cupsFilePuts(data->outfile, "<key>Name</key>\n"); | |
936 | print_xml_string(data->outfile, "string", data->name); | |
937 | if (data->file_id[0]) | |
938 | { | |
939 | cupsFilePuts(data->outfile, "<key>FileId</key>\n"); | |
940 | print_xml_string(data->outfile, "string", data->file_id); | |
941 | } | |
942 | if (data->test_id[0]) | |
943 | { | |
944 | cupsFilePuts(data->outfile, "<key>TestId</key>\n"); | |
945 | print_xml_string(data->outfile, "string", data->test_id); | |
946 | } | |
947 | cupsFilePuts(data->outfile, "<key>Version</key>\n"); | |
948 | cupsFilePrintf(data->outfile, "<string>%d.%d</string>\n", data->version / 10, data->version % 10); | |
949 | cupsFilePuts(data->outfile, "<key>Operation</key>\n"); | |
950 | print_xml_string(data->outfile, "string", ippOpString(ippGetOperation(request))); | |
951 | cupsFilePuts(data->outfile, "<key>RequestId</key>\n"); | |
952 | cupsFilePrintf(data->outfile, "<integer>%d</integer>\n", data->request_id); | |
953 | cupsFilePuts(data->outfile, "<key>RequestAttributes</key>\n"); | |
954 | cupsFilePuts(data->outfile, "<array>\n"); | |
a492d345 | 955 | if (ippFirstAttribute(request)) |
8e2e09c1 MS |
956 | { |
957 | cupsFilePuts(data->outfile, "<dict>\n"); | |
958 | for (attrptr = ippFirstAttribute(request), group = ippGetGroupTag(attrptr); attrptr; attrptr = ippNextAttribute(request)) | |
959 | print_attr(data->outfile, data->output, attrptr, &group); | |
960 | cupsFilePuts(data->outfile, "</dict>\n"); | |
961 | } | |
962 | cupsFilePuts(data->outfile, "</array>\n"); | |
963 | } | |
10d09e33 | 964 | |
8e2e09c1 MS |
965 | if (data->output == _CUPS_OUTPUT_TEST || (data->output == _CUPS_OUTPUT_PLIST && data->outfile != cupsFileStdout())) |
966 | { | |
967 | if (data->verbosity) | |
968 | { | |
969 | cupsFilePrintf(cupsFileStdout(), " %s:\n", ippOpString(ippGetOperation(request))); | |
ef416fc2 | 970 | |
8e2e09c1 MS |
971 | for (attrptr = ippFirstAttribute(request); attrptr; attrptr = ippNextAttribute(request)) |
972 | print_attr(cupsFileStdout(), _CUPS_OUTPUT_TEST, attrptr, NULL); | |
973 | } | |
f8b3a85b | 974 | |
8e2e09c1 MS |
975 | cupsFilePrintf(cupsFileStdout(), " %-68.68s [", data->name); |
976 | } | |
ef416fc2 | 977 | |
8e2e09c1 | 978 | if ((data->skip_previous && !data->prev_pass) || data->skip_test) |
ef416fc2 | 979 | { |
8e2e09c1 MS |
980 | data->skip_count ++; |
981 | ||
982 | ippDelete(request); | |
983 | request = NULL; | |
984 | response = NULL; | |
ef416fc2 | 985 | |
8e2e09c1 | 986 | if (data->output == _CUPS_OUTPUT_PLIST) |
f8b3a85b | 987 | { |
8e2e09c1 MS |
988 | cupsFilePuts(data->outfile, "<key>Successful</key>\n"); |
989 | cupsFilePuts(data->outfile, "<true />\n"); | |
990 | cupsFilePuts(data->outfile, "<key>Skipped</key>\n"); | |
991 | cupsFilePuts(data->outfile, "<true />\n"); | |
992 | cupsFilePuts(data->outfile, "<key>StatusCode</key>\n"); | |
993 | print_xml_string(data->outfile, "string", "skip"); | |
994 | cupsFilePuts(data->outfile, "<key>ResponseAttributes</key>\n"); | |
995 | cupsFilePuts(data->outfile, "<dict />\n"); | |
996 | } | |
f8b3a85b | 997 | |
8e2e09c1 MS |
998 | if (data->output == _CUPS_OUTPUT_TEST || (data->output == _CUPS_OUTPUT_PLIST && data->outfile != cupsFileStdout())) |
999 | cupsFilePuts(cupsFileStdout(), "SKIP]\n"); | |
030ae6a1 | 1000 | |
8e2e09c1 MS |
1001 | goto skip_error; |
1002 | } | |
321d8d57 | 1003 | |
8e2e09c1 | 1004 | vars->password_tries = 0; |
321d8d57 | 1005 | |
8e2e09c1 MS |
1006 | do |
1007 | { | |
1008 | if (data->delay > 0) | |
1009 | usleep(data->delay); | |
1d47b929 | 1010 | |
8e2e09c1 MS |
1011 | data->delay = data->repeat_interval; |
1012 | repeat_count ++; | |
1d47b929 | 1013 | |
8e2e09c1 MS |
1014 | status = HTTP_STATUS_OK; |
1015 | ||
1016 | if (data->transfer == _CUPS_TRANSFER_CHUNKED || (data->transfer == _CUPS_TRANSFER_AUTO && data->file[0])) | |
030ae6a1 MS |
1017 | { |
1018 | /* | |
8e2e09c1 | 1019 | * Send request using chunking - a 0 length means "chunk". |
030ae6a1 MS |
1020 | */ |
1021 | ||
8e2e09c1 | 1022 | length = 0; |
f8b3a85b | 1023 | } |
8e2e09c1 | 1024 | else |
f8b3a85b MS |
1025 | { |
1026 | /* | |
8e2e09c1 | 1027 | * Send request using content length... |
f8b3a85b MS |
1028 | */ |
1029 | ||
8e2e09c1 MS |
1030 | length = ippLength(request); |
1031 | ||
1032 | if (data->file[0] && (reqfile = cupsFileOpen(data->file, "r")) != NULL) | |
f8b3a85b MS |
1033 | { |
1034 | /* | |
8e2e09c1 | 1035 | * Read the file to get the uncompressed file size... |
f8b3a85b MS |
1036 | */ |
1037 | ||
8e2e09c1 MS |
1038 | while ((bytes = cupsFileRead(reqfile, buffer, sizeof(buffer))) > 0) |
1039 | length += (size_t)bytes; | |
030ae6a1 | 1040 | |
8e2e09c1 | 1041 | cupsFileClose(reqfile); |
f8b3a85b | 1042 | } |
f8b3a85b | 1043 | } |
321d8d57 | 1044 | |
8e2e09c1 MS |
1045 | /* |
1046 | * Send the request... | |
1047 | */ | |
1048 | ||
1049 | data->prev_pass = 1; | |
1050 | repeat_test = 0; | |
1051 | response = NULL; | |
1052 | ||
1053 | if (status != HTTP_STATUS_ERROR) | |
1054 | { | |
1055 | while (!response && !Cancel && data->prev_pass) | |
321d8d57 | 1056 | { |
8e2e09c1 MS |
1057 | status = cupsSendRequest(data->http, request, data->resource, length); |
1058 | ||
1059 | #ifdef HAVE_LIBZ | |
1060 | if (data->compression[0]) | |
1061 | httpSetField(data->http, HTTP_FIELD_CONTENT_ENCODING, data->compression); | |
1062 | #endif /* HAVE_LIBZ */ | |
1063 | ||
1064 | if (!Cancel && status == HTTP_STATUS_CONTINUE && ippGetState(request) == IPP_DATA && data->file[0]) | |
1065 | { | |
1066 | if ((reqfile = cupsFileOpen(data->file, "r")) != NULL) | |
1067 | { | |
1068 | while (!Cancel && (bytes = cupsFileRead(reqfile, buffer, sizeof(buffer))) > 0) | |
1069 | { | |
1070 | if ((status = cupsWriteRequestData(data->http, buffer, (size_t)bytes)) != HTTP_STATUS_CONTINUE) | |
1071 | break; | |
1072 | } | |
1073 | ||
1074 | cupsFileClose(reqfile); | |
1075 | } | |
1076 | else | |
1077 | { | |
1078 | snprintf(buffer, sizeof(buffer), "%s: %s", data->file, strerror(errno)); | |
1079 | _cupsSetError(IPP_INTERNAL_ERROR, buffer, 0); | |
1080 | ||
1081 | status = HTTP_STATUS_ERROR; | |
1082 | } | |
1083 | } | |
1084 | ||
321d8d57 | 1085 | /* |
8e2e09c1 | 1086 | * Get the server's response... |
321d8d57 MS |
1087 | */ |
1088 | ||
8e2e09c1 | 1089 | if (!Cancel && status != HTTP_STATUS_ERROR) |
321d8d57 | 1090 | { |
8e2e09c1 MS |
1091 | response = cupsGetResponse(data->http, data->resource); |
1092 | status = httpGetStatus(data->http); | |
1093 | } | |
321d8d57 | 1094 | |
8e2e09c1 | 1095 | if (!Cancel && status == HTTP_STATUS_ERROR && httpError(data->http) != EINVAL && |
24a06ed3 | 1096 | #ifdef _WIN32 |
8e2e09c1 MS |
1097 | httpError(data->http) != WSAETIMEDOUT) |
1098 | #else | |
1099 | httpError(data->http) != ETIMEDOUT) | |
24a06ed3 | 1100 | #endif /* _WIN32 */ |
8e2e09c1 MS |
1101 | { |
1102 | if (httpReconnect2(data->http, 30000, NULL)) | |
1103 | data->prev_pass = 0; | |
1104 | } | |
1105 | else if (status == HTTP_STATUS_ERROR || status == HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED) | |
1106 | { | |
1107 | data->prev_pass = 0; | |
1108 | break; | |
1109 | } | |
1110 | else if (status != HTTP_STATUS_OK) | |
1111 | { | |
1112 | httpFlush(data->http); | |
1113 | ||
1114 | if (status == HTTP_STATUS_UNAUTHORIZED) | |
1115 | continue; | |
1116 | ||
1117 | break; | |
321d8d57 MS |
1118 | } |
1119 | } | |
8e2e09c1 | 1120 | } |
321d8d57 | 1121 | |
8e2e09c1 | 1122 | if (!Cancel && status == HTTP_STATUS_ERROR && httpError(data->http) != EINVAL && |
24a06ed3 | 1123 | #ifdef _WIN32 |
8e2e09c1 MS |
1124 | httpError(data->http) != WSAETIMEDOUT) |
1125 | #else | |
1126 | httpError(data->http) != ETIMEDOUT) | |
24a06ed3 | 1127 | #endif /* _WIN32 */ |
8e2e09c1 MS |
1128 | { |
1129 | if (httpReconnect2(data->http, 30000, NULL)) | |
1130 | data->prev_pass = 0; | |
321d8d57 | 1131 | } |
8e2e09c1 | 1132 | else if (status == HTTP_STATUS_ERROR) |
321d8d57 | 1133 | { |
8e2e09c1 MS |
1134 | if (!Cancel) |
1135 | httpReconnect2(data->http, 30000, NULL); | |
321d8d57 | 1136 | |
8e2e09c1 MS |
1137 | data->prev_pass = 0; |
1138 | } | |
1139 | else if (status != HTTP_STATUS_OK) | |
1140 | { | |
1141 | httpFlush(data->http); | |
1142 | data->prev_pass = 0; | |
1143 | } | |
321d8d57 | 1144 | |
8e2e09c1 MS |
1145 | /* |
1146 | * Check results of request... | |
1147 | */ | |
321d8d57 | 1148 | |
8e2e09c1 | 1149 | cupsArrayClear(data->errors); |
321d8d57 | 1150 | |
8e2e09c1 MS |
1151 | if (httpGetVersion(data->http) != HTTP_1_1) |
1152 | { | |
1153 | int version = httpGetVersion(data->http); | |
1154 | ||
1155 | add_stringf(data->errors, "Bad HTTP version (%d.%d)", version / 100, version % 100); | |
321d8d57 | 1156 | } |
8e2e09c1 MS |
1157 | |
1158 | if (data->validate_headers) | |
030ae6a1 | 1159 | { |
8e2e09c1 | 1160 | const char *header; /* HTTP header value */ |
030ae6a1 | 1161 | |
8e2e09c1 MS |
1162 | if ((header = httpGetField(data->http, HTTP_FIELD_CONTENT_TYPE)) == NULL || _cups_strcasecmp(header, "application/ipp")) |
1163 | add_stringf(data->errors, "Bad HTTP Content-Type in response (%s)", header && *header ? header : "<missing>"); | |
1164 | ||
1165 | if ((header = httpGetField(data->http, HTTP_FIELD_DATE)) != NULL && *header && httpGetDateTime(header) == 0) | |
1166 | add_stringf(data->errors, "Bad HTTP Date in response (%s)", header); | |
030ae6a1 | 1167 | } |
8e2e09c1 MS |
1168 | |
1169 | if (!response) | |
030ae6a1 MS |
1170 | { |
1171 | /* | |
8e2e09c1 | 1172 | * No response, log error... |
030ae6a1 MS |
1173 | */ |
1174 | ||
8e2e09c1 | 1175 | add_stringf(data->errors, "IPP request failed with status %s (%s)", ippErrorString(cupsLastError()), cupsLastErrorString()); |
030ae6a1 | 1176 | } |
8e2e09c1 | 1177 | else |
9475ec92 MS |
1178 | { |
1179 | /* | |
8e2e09c1 | 1180 | * Collect common attribute values... |
9475ec92 MS |
1181 | */ |
1182 | ||
8e2e09c1 | 1183 | if ((attrptr = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER)) != NULL) |
9475ec92 | 1184 | { |
a492d345 | 1185 | snprintf(temp, sizeof(temp), "%d", ippGetInteger(attrptr, 0)); |
8e2e09c1 | 1186 | _ippVarsSet(vars, "job-id", temp); |
9475ec92 | 1187 | } |
8e2e09c1 | 1188 | |
a492d345 MS |
1189 | if ((attrptr = ippFindAttribute(response, "job-uri", IPP_TAG_URI)) != NULL) |
1190 | _ippVarsSet(vars, "job-uri", ippGetString(attrptr, 0, NULL)); | |
8e2e09c1 | 1191 | |
a492d345 | 1192 | if ((attrptr = ippFindAttribute(response, "notify-subscription-id", IPP_TAG_INTEGER)) != NULL) |
9475ec92 | 1193 | { |
a492d345 | 1194 | snprintf(temp, sizeof(temp), "%d", ippGetInteger(attrptr, 0)); |
8e2e09c1 | 1195 | _ippVarsSet(vars, "notify-subscription-id", temp); |
9475ec92 MS |
1196 | } |
1197 | ||
f8b3a85b | 1198 | /* |
8e2e09c1 MS |
1199 | * Check response, validating groups and attributes and logging errors |
1200 | * as needed... | |
f8b3a85b MS |
1201 | */ |
1202 | ||
8e2e09c1 MS |
1203 | if (ippGetState(response) != IPP_DATA) |
1204 | add_stringf(data->errors, "Missing end-of-attributes-tag in response (RFC 2910 section 3.5.1)"); | |
1205 | ||
1206 | if (data->version) | |
f8b3a85b | 1207 | { |
8e2e09c1 MS |
1208 | int major, minor; /* IPP version */ |
1209 | ||
1210 | major = ippGetVersion(response, &minor); | |
1211 | ||
1212 | if (major != (data->version / 10) || minor != (data->version % 10)) | |
1213 | add_stringf(data->errors, "Bad version %d.%d in response - expected %d.%d (RFC 2911 section 3.1.8).", major, minor, data->version / 10, data->version % 10); | |
f8b3a85b | 1214 | } |
8e2e09c1 MS |
1215 | |
1216 | if (ippGetRequestId(response) != data->request_id) | |
1217 | add_stringf(data->errors, "Bad request ID %d in response - expected %d (RFC 2911 section 3.1.1)", ippGetRequestId(response), data->request_id); | |
1218 | ||
1219 | attrptr = ippFirstAttribute(response); | |
1220 | if (!attrptr) | |
f8b3a85b | 1221 | { |
8e2e09c1 | 1222 | add_stringf(data->errors, "Missing first attribute \"attributes-charset (charset)\" in group operation-attributes-tag (RFC 2911 section 3.1.4)."); |
f8b3a85b | 1223 | } |
8e2e09c1 MS |
1224 | else |
1225 | { | |
1226 | if (!ippGetName(attrptr) || ippGetValueTag(attrptr) != IPP_TAG_CHARSET || ippGetGroupTag(attrptr) != IPP_TAG_OPERATION || ippGetCount(attrptr) != 1 ||strcmp(ippGetName(attrptr), "attributes-charset")) | |
1227 | add_stringf(data->errors, "Bad first attribute \"%s (%s%s)\" in group %s, expected \"attributes-charset (charset)\" in group operation-attributes-tag (RFC 2911 section 3.1.4).", ippGetName(attrptr) ? ippGetName(attrptr) : "(null)", ippGetCount(attrptr) > 1 ? "1setOf " : "", ippTagString(ippGetValueTag(attrptr)), ippTagString(ippGetGroupTag(attrptr))); | |
f8b3a85b | 1228 | |
8e2e09c1 MS |
1229 | attrptr = ippNextAttribute(response); |
1230 | if (!attrptr) | |
1231 | add_stringf(data->errors, "Missing second attribute \"attributes-natural-language (naturalLanguage)\" in group operation-attributes-tag (RFC 2911 section 3.1.4)."); | |
1232 | else if (!ippGetName(attrptr) || ippGetValueTag(attrptr) != IPP_TAG_LANGUAGE || ippGetGroupTag(attrptr) != IPP_TAG_OPERATION || ippGetCount(attrptr) != 1 || strcmp(ippGetName(attrptr), "attributes-natural-language")) | |
1233 | add_stringf(data->errors, "Bad first attribute \"%s (%s%s)\" in group %s, expected \"attributes-natural-language (naturalLanguage)\" in group operation-attributes-tag (RFC 2911 section 3.1.4).", ippGetName(attrptr) ? ippGetName(attrptr) : "(null)", ippGetCount(attrptr) > 1 ? "1setOf " : "", ippTagString(ippGetValueTag(attrptr)), ippTagString(ippGetGroupTag(attrptr))); | |
1234 | } | |
1235 | ||
1236 | if ((attrptr = ippFindAttribute(response, "status-message", IPP_TAG_ZERO)) != NULL) | |
1237 | { | |
a492d345 MS |
1238 | const char *status_message = ippGetString(attrptr, 0, NULL); |
1239 | /* String value */ | |
1240 | ||
1241 | if (ippGetValueTag(attrptr) != IPP_TAG_TEXT) | |
1242 | add_stringf(data->errors, "status-message (text(255)) has wrong value tag %s (RFC 2911 section 3.1.6.2).", ippTagString(ippGetValueTag(attrptr))); | |
1243 | if (ippGetGroupTag(attrptr) != IPP_TAG_OPERATION) | |
1244 | add_stringf(data->errors, "status-message (text(255)) has wrong group tag %s (RFC 2911 section 3.1.6.2).", ippTagString(ippGetGroupTag(attrptr))); | |
1245 | if (ippGetCount(attrptr) != 1) | |
1246 | add_stringf(data->errors, "status-message (text(255)) has %d values (RFC 2911 section 3.1.6.2).", ippGetCount(attrptr)); | |
1247 | if (status_message && strlen(status_message) > 255) | |
1248 | add_stringf(data->errors, "status-message (text(255)) has bad length %d (RFC 2911 section 3.1.6.2).", (int)strlen(status_message)); | |
8e2e09c1 MS |
1249 | } |
1250 | ||
1251 | if ((attrptr = ippFindAttribute(response, "detailed-status-message", | |
1252 | IPP_TAG_ZERO)) != NULL) | |
1253 | { | |
a492d345 MS |
1254 | const char *detailed_status_message = ippGetString(attrptr, 0, NULL); |
1255 | /* String value */ | |
1256 | ||
1257 | if (ippGetValueTag(attrptr) != IPP_TAG_TEXT) | |
8e2e09c1 MS |
1258 | add_stringf(data->errors, |
1259 | "detailed-status-message (text(MAX)) has wrong " | |
1260 | "value tag %s (RFC 2911 section 3.1.6.3).", | |
a492d345 MS |
1261 | ippTagString(ippGetValueTag(attrptr))); |
1262 | if (ippGetGroupTag(attrptr) != IPP_TAG_OPERATION) | |
8e2e09c1 MS |
1263 | add_stringf(data->errors, |
1264 | "detailed-status-message (text(MAX)) has wrong " | |
1265 | "group tag %s (RFC 2911 section 3.1.6.3).", | |
a492d345 MS |
1266 | ippTagString(ippGetGroupTag(attrptr))); |
1267 | if (ippGetCount(attrptr) != 1) | |
8e2e09c1 MS |
1268 | add_stringf(data->errors, |
1269 | "detailed-status-message (text(MAX)) has %d values" | |
1270 | " (RFC 2911 section 3.1.6.3).", | |
a492d345 MS |
1271 | ippGetCount(attrptr)); |
1272 | if (detailed_status_message && strlen(detailed_status_message) > 1023) | |
8e2e09c1 MS |
1273 | add_stringf(data->errors, |
1274 | "detailed-status-message (text(MAX)) has bad " | |
1275 | "length %d (RFC 2911 section 3.1.6.3).", | |
a492d345 | 1276 | (int)strlen(detailed_status_message)); |
8e2e09c1 MS |
1277 | } |
1278 | ||
1279 | a = cupsArrayNew((cups_array_func_t)strcmp, NULL); | |
1280 | ||
a492d345 | 1281 | for (attrptr = ippFirstAttribute(response), group = ippGetGroupTag(attrptr); |
8e2e09c1 | 1282 | attrptr; |
a492d345 | 1283 | attrptr = ippNextAttribute(response)) |
85dda01c | 1284 | { |
a492d345 | 1285 | if (ippGetGroupTag(attrptr) != group) |
85dda01c | 1286 | { |
8e2e09c1 MS |
1287 | int out_of_order = 0; /* Are attribute groups out-of-order? */ |
1288 | cupsArrayClear(a); | |
85dda01c | 1289 | |
a492d345 | 1290 | switch (ippGetGroupTag(attrptr)) |
8e2e09c1 MS |
1291 | { |
1292 | case IPP_TAG_ZERO : | |
1293 | break; | |
85dda01c | 1294 | |
8e2e09c1 MS |
1295 | case IPP_TAG_OPERATION : |
1296 | out_of_order = 1; | |
1297 | break; | |
83e08001 | 1298 | |
8e2e09c1 MS |
1299 | case IPP_TAG_UNSUPPORTED_GROUP : |
1300 | if (group != IPP_TAG_OPERATION) | |
1301 | out_of_order = 1; | |
1302 | break; | |
c41769ff | 1303 | |
8e2e09c1 MS |
1304 | case IPP_TAG_JOB : |
1305 | case IPP_TAG_PRINTER : | |
a492d345 | 1306 | if (group != IPP_TAG_OPERATION && group != IPP_TAG_UNSUPPORTED_GROUP) |
8e2e09c1 MS |
1307 | out_of_order = 1; |
1308 | break; | |
9b66acc5 | 1309 | |
8e2e09c1 | 1310 | case IPP_TAG_SUBSCRIPTION : |
a492d345 | 1311 | if (group > ippGetGroupTag(attrptr) && group != IPP_TAG_DOCUMENT) |
8e2e09c1 MS |
1312 | out_of_order = 1; |
1313 | break; | |
83e08001 | 1314 | |
8e2e09c1 | 1315 | default : |
a492d345 | 1316 | if (group > ippGetGroupTag(attrptr)) |
8e2e09c1 MS |
1317 | out_of_order = 1; |
1318 | break; | |
83e08001 | 1319 | } |
85dda01c | 1320 | |
8e2e09c1 MS |
1321 | if (out_of_order) |
1322 | add_stringf(data->errors, "Attribute groups out of order (%s < %s)", | |
a492d345 | 1323 | ippTagString(ippGetGroupTag(attrptr)), |
8e2e09c1 | 1324 | ippTagString(group)); |
85dda01c | 1325 | |
a492d345 MS |
1326 | if (ippGetGroupTag(attrptr) != IPP_TAG_ZERO) |
1327 | group = ippGetGroupTag(attrptr); | |
8e2e09c1 | 1328 | } |
83e08001 | 1329 | |
2da2477d MS |
1330 | if (!ippValidateAttribute(attrptr)) |
1331 | cupsArrayAdd(data->errors, (void *)cupsLastErrorString()); | |
6961465f | 1332 | |
a492d345 | 1333 | if (ippGetName(attrptr)) |
8e2e09c1 | 1334 | { |
d07cb8d5 | 1335 | if (cupsArrayFind(a, (void *)ippGetName(attrptr)) && data->output < _CUPS_OUTPUT_LIST) |
8e2e09c1 | 1336 | add_stringf(data->errors, "Duplicate \"%s\" attribute in %s group", |
a492d345 | 1337 | ippGetName(attrptr), ippTagString(group)); |
6961465f | 1338 | |
a492d345 | 1339 | cupsArrayAdd(a, (void *)ippGetName(attrptr)); |
85dda01c MS |
1340 | } |
1341 | } | |
85dda01c | 1342 | |
8e2e09c1 | 1343 | cupsArrayDelete(a); |
12f89d24 | 1344 | |
83e08001 | 1345 | /* |
8e2e09c1 MS |
1346 | * Now check the test-defined expected status-code and attribute |
1347 | * values... | |
83e08001 | 1348 | */ |
ef416fc2 | 1349 | |
8e2e09c1 | 1350 | for (i = 0, status_ok = 0; i < data->num_statuses; i ++) |
dcb445bc | 1351 | { |
8e2e09c1 MS |
1352 | if (data->statuses[i].if_defined && |
1353 | !_ippVarsGet(vars, data->statuses[i].if_defined)) | |
1354 | continue; | |
f8b3a85b | 1355 | |
8e2e09c1 MS |
1356 | if (data->statuses[i].if_not_defined && |
1357 | _ippVarsGet(vars, data->statuses[i].if_not_defined)) | |
1358 | continue; | |
f8b3a85b | 1359 | |
8e2e09c1 | 1360 | if (ippGetStatusCode(response) == data->statuses[i].status) |
83e08001 | 1361 | { |
8e2e09c1 | 1362 | status_ok = 1; |
ef416fc2 | 1363 | |
8e2e09c1 MS |
1364 | if (data->statuses[i].repeat_match && repeat_count < data->statuses[i].repeat_limit) |
1365 | repeat_test = 1; | |
f8b3a85b | 1366 | |
8e2e09c1 MS |
1367 | if (data->statuses[i].define_match) |
1368 | _ippVarsSet(vars, data->statuses[i].define_match, "1"); | |
83e08001 | 1369 | } |
dcb445bc | 1370 | else |
83e08001 | 1371 | { |
8e2e09c1 MS |
1372 | if (data->statuses[i].repeat_no_match && repeat_count < data->statuses[i].repeat_limit) |
1373 | repeat_test = 1; | |
f8b3a85b | 1374 | |
8e2e09c1 | 1375 | if (data->statuses[i].define_no_match) |
83e08001 | 1376 | { |
8e2e09c1 MS |
1377 | _ippVarsSet(vars, data->statuses[i].define_no_match, "1"); |
1378 | status_ok = 1; | |
83e08001 | 1379 | } |
f8b3a85b | 1380 | } |
8e2e09c1 | 1381 | } |
ef416fc2 | 1382 | |
8e2e09c1 MS |
1383 | if (!status_ok && data->num_statuses > 0) |
1384 | { | |
1385 | for (i = 0; i < data->num_statuses; i ++) | |
83e08001 | 1386 | { |
8e2e09c1 MS |
1387 | if (data->statuses[i].if_defined && |
1388 | !_ippVarsGet(vars, data->statuses[i].if_defined)) | |
83e08001 | 1389 | continue; |
f8b3a85b | 1390 | |
8e2e09c1 MS |
1391 | if (data->statuses[i].if_not_defined && |
1392 | _ippVarsGet(vars, data->statuses[i].if_not_defined)) | |
83e08001 | 1393 | continue; |
f8b3a85b | 1394 | |
8e2e09c1 MS |
1395 | if (!data->statuses[i].repeat_match || repeat_count >= data->statuses[i].repeat_limit) |
1396 | add_stringf(data->errors, "EXPECTED: STATUS %s (got %s)", | |
1397 | ippErrorString(data->statuses[i].status), | |
1398 | ippErrorString(cupsLastError())); | |
1399 | } | |
2a75f21b | 1400 | |
8e2e09c1 MS |
1401 | if ((attrptr = ippFindAttribute(response, "status-message", |
1402 | IPP_TAG_TEXT)) != NULL) | |
a492d345 | 1403 | add_stringf(data->errors, "status-message=\"%s\"", ippGetString(attrptr, 0, NULL)); |
8e2e09c1 | 1404 | } |
ef416fc2 | 1405 | |
8e2e09c1 MS |
1406 | for (i = data->num_expects, expect = data->expects; i > 0; i --, expect ++) |
1407 | { | |
c5b7774c MS |
1408 | ipp_attribute_t *group_found; /* Found parent attribute for group tests */ |
1409 | ||
8e2e09c1 MS |
1410 | if (expect->if_defined && !_ippVarsGet(vars, expect->if_defined)) |
1411 | continue; | |
0fa6c7fa | 1412 | |
8e2e09c1 MS |
1413 | if (expect->if_not_defined && |
1414 | _ippVarsGet(vars, expect->if_not_defined)) | |
1415 | continue; | |
f8b3a85b | 1416 | |
a32af27c MS |
1417 | if ((found = ippFindAttribute(response, expect->name, IPP_TAG_ZERO)) != NULL && expect->in_group && expect->in_group != ippGetGroupTag(found)) |
1418 | { | |
1419 | while ((found = ippFindNextAttribute(response, expect->name, IPP_TAG_ZERO)) != NULL) | |
1420 | if (expect->in_group == ippGetGroupTag(found)) | |
1421 | break; | |
1422 | } | |
8e2e09c1 MS |
1423 | |
1424 | do | |
dcb445bc | 1425 | { |
c5b7774c MS |
1426 | group_found = found; |
1427 | ||
1428 | if (expect->in_group && strchr(expect->name, '/')) | |
1429 | { | |
1430 | char group_name[256],/* Parent attribute name */ | |
1431 | *group_ptr; /* Pointer into parent attribute name */ | |
1432 | ||
1433 | strlcpy(group_name, expect->name, sizeof(group_name)); | |
1434 | if ((group_ptr = strchr(group_name, '/')) != NULL) | |
1435 | *group_ptr = '\0'; | |
1436 | ||
1437 | group_found = ippFindAttribute(response, group_name, IPP_TAG_ZERO); | |
1438 | } | |
1439 | ||
8e2e09c1 MS |
1440 | if ((found && expect->not_expect) || |
1441 | (!found && !(expect->not_expect || expect->optional)) || | |
a492d345 | 1442 | (found && !expect_matches(expect, ippGetValueTag(found))) || |
c5b7774c | 1443 | (group_found && expect->in_group && ippGetGroupTag(group_found) != expect->in_group)) |
dcb445bc | 1444 | { |
8e2e09c1 MS |
1445 | if (expect->define_no_match) |
1446 | _ippVarsSet(vars, expect->define_no_match, "1"); | |
1447 | else if (!expect->define_match && !expect->define_value) | |
1448 | { | |
1449 | if (found && expect->not_expect) | |
1450 | add_stringf(data->errors, "NOT EXPECTED: %s", expect->name); | |
1451 | else if (!found && !(expect->not_expect || expect->optional)) | |
1452 | add_stringf(data->errors, "EXPECTED: %s", expect->name); | |
1453 | else if (found) | |
1454 | { | |
a492d345 | 1455 | if (!expect_matches(expect, ippGetValueTag(found))) |
8e2e09c1 MS |
1456 | add_stringf(data->errors, "EXPECTED: %s OF-TYPE %s (got %s)", |
1457 | expect->name, expect->of_type, | |
a492d345 | 1458 | ippTagString(ippGetValueTag(found))); |
8e2e09c1 | 1459 | |
c5b7774c | 1460 | if (expect->in_group && ippGetGroupTag(group_found) != expect->in_group) |
8e2e09c1 MS |
1461 | add_stringf(data->errors, "EXPECTED: %s IN-GROUP %s (got %s).", |
1462 | expect->name, ippTagString(expect->in_group), | |
c5b7774c | 1463 | ippTagString(ippGetGroupTag(group_found))); |
8e2e09c1 MS |
1464 | } |
1465 | } | |
d7225fc2 | 1466 | |
8e2e09c1 MS |
1467 | if (expect->repeat_no_match && repeat_count < expect->repeat_limit) |
1468 | repeat_test = 1; | |
8e2e09c1 MS |
1469 | break; |
1470 | } | |
d7225fc2 | 1471 | |
8e2e09c1 MS |
1472 | if (found) |
1473 | ippAttributeString(found, buffer, sizeof(buffer)); | |
d7225fc2 | 1474 | |
8e2e09c1 MS |
1475 | if (found && expect->with_value_from && !with_value_from(NULL, ippFindAttribute(response, expect->with_value_from, IPP_TAG_ZERO), found, buffer, sizeof(buffer))) |
1476 | { | |
1477 | if (expect->define_no_match) | |
1478 | _ippVarsSet(vars, expect->define_no_match, "1"); | |
1479 | else if (!expect->define_match && !expect->define_value && ((!expect->repeat_match && !expect->repeat_no_match) || repeat_count >= expect->repeat_limit)) | |
dcb445bc | 1480 | { |
8e2e09c1 | 1481 | add_stringf(data->errors, "EXPECTED: %s WITH-VALUES-FROM %s", expect->name, expect->with_value_from); |
5a662dc0 | 1482 | |
8e2e09c1 | 1483 | with_value_from(data->errors, ippFindAttribute(response, expect->with_value_from, IPP_TAG_ZERO), found, buffer, sizeof(buffer)); |
dcb445bc | 1484 | } |
030ae6a1 | 1485 | |
8e2e09c1 MS |
1486 | if (expect->repeat_no_match && repeat_count < expect->repeat_limit) |
1487 | repeat_test = 1; | |
9b66acc5 | 1488 | |
8e2e09c1 MS |
1489 | break; |
1490 | } | |
1491 | else if (found && !with_value(data, NULL, expect->with_value, expect->with_flags, found, buffer, sizeof(buffer))) | |
1492 | { | |
1493 | if (expect->define_no_match) | |
1494 | _ippVarsSet(vars, expect->define_no_match, "1"); | |
1495 | else if (!expect->define_match && !expect->define_value && | |
1496 | !expect->repeat_match && (!expect->repeat_no_match || repeat_count >= expect->repeat_limit)) | |
a21c36fa | 1497 | { |
8e2e09c1 MS |
1498 | if (expect->with_flags & _CUPS_WITH_REGEX) |
1499 | add_stringf(data->errors, "EXPECTED: %s %s /%s/", expect->name, with_flags_string(expect->with_flags), expect->with_value); | |
1500 | else | |
1501 | add_stringf(data->errors, "EXPECTED: %s %s \"%s\"", expect->name, with_flags_string(expect->with_flags), expect->with_value); | |
a21c36fa | 1502 | |
8e2e09c1 MS |
1503 | with_value(data, data->errors, expect->with_value, expect->with_flags, found, buffer, sizeof(buffer)); |
1504 | } | |
a21c36fa | 1505 | |
8e2e09c1 MS |
1506 | if (expect->repeat_no_match && |
1507 | repeat_count < expect->repeat_limit) | |
1508 | repeat_test = 1; | |
a21c36fa | 1509 | |
8e2e09c1 MS |
1510 | break; |
1511 | } | |
1512 | ||
a492d345 | 1513 | if (found && expect->count > 0 && ippGetCount(found) != expect->count) |
8e2e09c1 MS |
1514 | { |
1515 | if (expect->define_no_match) | |
1516 | _ippVarsSet(vars, expect->define_no_match, "1"); | |
1517 | else if (!expect->define_match && !expect->define_value) | |
dcb445bc | 1518 | { |
8e2e09c1 | 1519 | add_stringf(data->errors, "EXPECTED: %s COUNT %d (got %d)", expect->name, |
a492d345 | 1520 | expect->count, ippGetCount(found)); |
8e2e09c1 | 1521 | } |
ea4dcf9f | 1522 | |
8e2e09c1 MS |
1523 | if (expect->repeat_no_match && |
1524 | repeat_count < expect->repeat_limit) | |
1525 | repeat_test = 1; | |
f8b3a85b | 1526 | |
8e2e09c1 MS |
1527 | break; |
1528 | } | |
d7225fc2 | 1529 | |
8e2e09c1 MS |
1530 | if (found && expect->same_count_as) |
1531 | { | |
1532 | attrptr = ippFindAttribute(response, expect->same_count_as, | |
1533 | IPP_TAG_ZERO); | |
5a662dc0 | 1534 | |
a492d345 | 1535 | if (!attrptr || ippGetCount(attrptr) != ippGetCount(found)) |
83e08001 | 1536 | { |
030ae6a1 | 1537 | if (expect->define_no_match) |
08102776 | 1538 | _ippVarsSet(vars, expect->define_no_match, "1"); |
9b66acc5 | 1539 | else if (!expect->define_match && !expect->define_value) |
dcb445bc | 1540 | { |
8e2e09c1 MS |
1541 | if (!attrptr) |
1542 | add_stringf(data->errors, | |
1543 | "EXPECTED: %s (%d values) SAME-COUNT-AS %s " | |
1544 | "(not returned)", expect->name, | |
a492d345 MS |
1545 | ippGetCount(found), expect->same_count_as); |
1546 | else if (ippGetCount(attrptr) != ippGetCount(found)) | |
8e2e09c1 MS |
1547 | add_stringf(data->errors, |
1548 | "EXPECTED: %s (%d values) SAME-COUNT-AS %s " | |
a492d345 MS |
1549 | "(%d values)", expect->name, ippGetCount(found), |
1550 | expect->same_count_as, ippGetCount(attrptr)); | |
dcb445bc | 1551 | } |
030ae6a1 | 1552 | |
a29fd7dd | 1553 | if (expect->repeat_no_match && |
e83646d5 | 1554 | repeat_count < expect->repeat_limit) |
d7225fc2 | 1555 | repeat_test = 1; |
030ae6a1 | 1556 | |
e83646d5 | 1557 | break; |
83e08001 | 1558 | } |
8e2e09c1 | 1559 | } |
83e08001 | 1560 | |
8e2e09c1 MS |
1561 | if (found && expect->define_match) |
1562 | _ippVarsSet(vars, expect->define_match, "1"); | |
1563 | ||
1564 | if (found && expect->define_value) | |
1565 | { | |
1566 | if (!expect->with_value) | |
c2c30ebc | 1567 | { |
8e2e09c1 MS |
1568 | int last = ippGetCount(found) - 1; |
1569 | /* Last element in attribute */ | |
a654c79d | 1570 | |
8e2e09c1 | 1571 | switch (ippGetValueTag(found)) |
a654c79d | 1572 | { |
8e2e09c1 MS |
1573 | case IPP_TAG_ENUM : |
1574 | case IPP_TAG_INTEGER : | |
1575 | snprintf(buffer, sizeof(buffer), "%d", ippGetInteger(found, last)); | |
1576 | break; | |
a654c79d | 1577 | |
8e2e09c1 MS |
1578 | case IPP_TAG_BOOLEAN : |
1579 | if (ippGetBoolean(found, last)) | |
1580 | strlcpy(buffer, "true", sizeof(buffer)); | |
1581 | else | |
1582 | strlcpy(buffer, "false", sizeof(buffer)); | |
1583 | break; | |
a654c79d | 1584 | |
8e2e09c1 MS |
1585 | case IPP_TAG_RESOLUTION : |
1586 | { | |
1587 | int xres, /* Horizontal resolution */ | |
1588 | yres; /* Vertical resolution */ | |
1589 | ipp_res_t units; /* Resolution units */ | |
a654c79d | 1590 | |
8e2e09c1 | 1591 | xres = ippGetResolution(found, last, &yres, &units); |
a654c79d | 1592 | |
8e2e09c1 MS |
1593 | if (xres == yres) |
1594 | snprintf(buffer, sizeof(buffer), "%d%s", xres, units == IPP_RES_PER_INCH ? "dpi" : "dpcm"); | |
e83646d5 | 1595 | else |
8e2e09c1 MS |
1596 | snprintf(buffer, sizeof(buffer), "%dx%d%s", xres, yres, units == IPP_RES_PER_INCH ? "dpi" : "dpcm"); |
1597 | } | |
1598 | break; | |
1599 | ||
1600 | case IPP_TAG_CHARSET : | |
1601 | case IPP_TAG_KEYWORD : | |
1602 | case IPP_TAG_LANGUAGE : | |
1603 | case IPP_TAG_MIMETYPE : | |
1604 | case IPP_TAG_NAME : | |
1605 | case IPP_TAG_NAMELANG : | |
1606 | case IPP_TAG_TEXT : | |
1607 | case IPP_TAG_TEXTLANG : | |
1608 | case IPP_TAG_URI : | |
1609 | case IPP_TAG_URISCHEME : | |
1610 | strlcpy(buffer, ippGetString(found, last, NULL), sizeof(buffer)); | |
1611 | break; | |
c2c30ebc | 1612 | |
8e2e09c1 MS |
1613 | default : |
1614 | ippAttributeString(found, buffer, sizeof(buffer)); | |
1615 | break; | |
1616 | } | |
e83646d5 | 1617 | } |
83e08001 | 1618 | |
8e2e09c1 MS |
1619 | _ippVarsSet(vars, expect->define_value, buffer); |
1620 | } | |
1621 | ||
1622 | if (found && expect->repeat_match && | |
1623 | repeat_count < expect->repeat_limit) | |
1624 | repeat_test = 1; | |
83e08001 | 1625 | } |
8e2e09c1 | 1626 | while (expect->expect_all && (found = ippFindNextAttribute(response, expect->name, IPP_TAG_ZERO)) != NULL); |
ef416fc2 | 1627 | } |
8e2e09c1 | 1628 | } |
83e08001 | 1629 | |
8e2e09c1 MS |
1630 | /* |
1631 | * If we are going to repeat this test, display intermediate results... | |
1632 | */ | |
83e08001 | 1633 | |
8e2e09c1 MS |
1634 | if (repeat_test) |
1635 | { | |
1636 | if (data->output == _CUPS_OUTPUT_TEST || (data->output == _CUPS_OUTPUT_PLIST && data->outfile != cupsFileStdout())) | |
a29fd7dd | 1637 | { |
8e2e09c1 | 1638 | cupsFilePrintf(cupsFileStdout(), "%04d]\n", repeat_count); |
35fc2243 | 1639 | \ |
8e2e09c1 MS |
1640 | if (data->num_displayed > 0) |
1641 | { | |
1642 | for (attrptr = ippFirstAttribute(response); attrptr; attrptr = ippNextAttribute(response)) | |
4a838088 | 1643 | { |
8e2e09c1 MS |
1644 | const char *attrname = ippGetName(attrptr); |
1645 | if (attrname) | |
4a838088 | 1646 | { |
8e2e09c1 | 1647 | for (i = 0; i < data->num_displayed; i ++) |
4a838088 | 1648 | { |
8e2e09c1 | 1649 | if (!strcmp(data->displayed[i], attrname)) |
4a838088 | 1650 | { |
8e2e09c1 MS |
1651 | print_attr(cupsFileStdout(), _CUPS_OUTPUT_TEST, attrptr, NULL); |
1652 | break; | |
4a838088 MS |
1653 | } |
1654 | } | |
1655 | } | |
1656 | } | |
a29fd7dd | 1657 | } |
8e2e09c1 | 1658 | } |
b908d72c | 1659 | |
8e2e09c1 MS |
1660 | if (data->output == _CUPS_OUTPUT_TEST || (data->output == _CUPS_OUTPUT_PLIST && data->outfile != cupsFileStdout())) |
1661 | { | |
1662 | cupsFilePrintf(cupsFileStdout(), " %-68.68s [", data->name); | |
a29fd7dd | 1663 | } |
8e2e09c1 MS |
1664 | |
1665 | ippDelete(response); | |
1666 | response = NULL; | |
f8b3a85b | 1667 | } |
8e2e09c1 MS |
1668 | } |
1669 | while (repeat_test); | |
83e08001 | 1670 | |
8e2e09c1 | 1671 | ippDelete(request); |
83e08001 | 1672 | |
8e2e09c1 | 1673 | request = NULL; |
9b66acc5 | 1674 | |
8e2e09c1 MS |
1675 | if (cupsArrayCount(data->errors) > 0) |
1676 | data->prev_pass = data->pass = 0; | |
dcb445bc | 1677 | |
8e2e09c1 MS |
1678 | if (data->prev_pass) |
1679 | data->pass_count ++; | |
1680 | else | |
1681 | data->fail_count ++; | |
f8b3a85b | 1682 | |
8e2e09c1 MS |
1683 | if (data->output == _CUPS_OUTPUT_PLIST) |
1684 | { | |
1685 | cupsFilePuts(data->outfile, "<key>Successful</key>\n"); | |
1686 | cupsFilePuts(data->outfile, data->prev_pass ? "<true />\n" : "<false />\n"); | |
1687 | cupsFilePuts(data->outfile, "<key>StatusCode</key>\n"); | |
1688 | print_xml_string(data->outfile, "string", ippErrorString(cupsLastError())); | |
1689 | cupsFilePuts(data->outfile, "<key>ResponseAttributes</key>\n"); | |
1690 | cupsFilePuts(data->outfile, "<array>\n"); | |
1691 | cupsFilePuts(data->outfile, "<dict>\n"); | |
a492d345 | 1692 | for (attrptr = ippFirstAttribute(response), group = ippGetGroupTag(attrptr); |
8e2e09c1 | 1693 | attrptr; |
a492d345 | 1694 | attrptr = ippNextAttribute(response)) |
8e2e09c1 MS |
1695 | print_attr(data->outfile, data->output, attrptr, &group); |
1696 | cupsFilePuts(data->outfile, "</dict>\n"); | |
1697 | cupsFilePuts(data->outfile, "</array>\n"); | |
1698 | } | |
1699 | else if (data->output == _CUPS_OUTPUT_IPPSERVER && response) | |
1700 | { | |
a492d345 | 1701 | for (attrptr = ippFirstAttribute(response); attrptr; attrptr = ippNextAttribute(response)) |
6646208b | 1702 | { |
8e2e09c1 MS |
1703 | if (!ippGetName(attrptr) || ippGetGroupTag(attrptr) != IPP_TAG_PRINTER) |
1704 | continue; | |
6646208b | 1705 | |
8e2e09c1 | 1706 | print_ippserver_attr(data, attrptr, 0); |
6646208b | 1707 | } |
8e2e09c1 MS |
1708 | } |
1709 | ||
1710 | if (data->output == _CUPS_OUTPUT_TEST || (data->output == _CUPS_OUTPUT_PLIST && data->outfile != cupsFileStdout())) | |
1711 | { | |
1712 | cupsFilePuts(cupsFileStdout(), data->prev_pass ? "PASS]\n" : "FAIL]\n"); | |
9fcdd250 | 1713 | |
8e2e09c1 | 1714 | if (!data->prev_pass || (data->verbosity && response)) |
f8b3a85b | 1715 | { |
8e2e09c1 MS |
1716 | cupsFilePrintf(cupsFileStdout(), " RECEIVED: %lu bytes in response\n", (unsigned long)ippLength(response)); |
1717 | cupsFilePrintf(cupsFileStdout(), " status-code = %s (%s)\n", ippErrorString(cupsLastError()), cupsLastErrorString()); | |
ef416fc2 | 1718 | |
8e2e09c1 | 1719 | if (data->verbosity && response) |
ef416fc2 | 1720 | { |
a492d345 | 1721 | for (attrptr = ippFirstAttribute(response); attrptr; attrptr = ippNextAttribute(response)) |
8e2e09c1 | 1722 | print_attr(cupsFileStdout(), _CUPS_OUTPUT_TEST, attrptr, NULL); |
f8b3a85b MS |
1723 | } |
1724 | } | |
8e2e09c1 MS |
1725 | } |
1726 | else if (!data->prev_pass && data->output != _CUPS_OUTPUT_QUIET) | |
1727 | fprintf(stderr, "%s\n", cupsLastErrorString()); | |
1728 | ||
1729 | if (data->prev_pass && data->output >= _CUPS_OUTPUT_LIST && !data->verbosity && data->num_displayed > 0) | |
1730 | { | |
1731 | size_t width; /* Length of value */ | |
f8b3a85b | 1732 | |
8e2e09c1 | 1733 | for (i = 0; i < data->num_displayed; i ++) |
f8b3a85b | 1734 | { |
8e2e09c1 | 1735 | widths[i] = strlen(data->displayed[i]); |
d7225fc2 | 1736 | |
8e2e09c1 MS |
1737 | for (attrptr = ippFindAttribute(response, data->displayed[i], IPP_TAG_ZERO); |
1738 | attrptr; | |
1739 | attrptr = ippFindNextAttribute(response, data->displayed[i], IPP_TAG_ZERO)) | |
aaf19ab0 | 1740 | { |
8e2e09c1 MS |
1741 | width = ippAttributeString(attrptr, NULL, 0); |
1742 | if (width > widths[i]) | |
1743 | widths[i] = width; | |
d7225fc2 | 1744 | } |
8e2e09c1 | 1745 | } |
aaf19ab0 | 1746 | |
8e2e09c1 | 1747 | if (data->output == _CUPS_OUTPUT_CSV) |
a492d345 | 1748 | print_csv(data, NULL, NULL, data->num_displayed, data->displayed, widths); |
8e2e09c1 | 1749 | else |
a492d345 | 1750 | print_line(data, NULL, NULL, data->num_displayed, data->displayed, widths); |
aaf19ab0 | 1751 | |
a492d345 | 1752 | attrptr = ippFirstAttribute(response); |
aaf19ab0 | 1753 | |
8e2e09c1 MS |
1754 | while (attrptr) |
1755 | { | |
a492d345 MS |
1756 | while (attrptr && ippGetGroupTag(attrptr) <= IPP_TAG_OPERATION) |
1757 | attrptr = ippNextAttribute(response); | |
aaf19ab0 | 1758 | |
8e2e09c1 MS |
1759 | if (attrptr) |
1760 | { | |
1761 | if (data->output == _CUPS_OUTPUT_CSV) | |
a492d345 | 1762 | print_csv(data, response, attrptr, data->num_displayed, data->displayed, widths); |
8e2e09c1 | 1763 | else |
a492d345 | 1764 | print_line(data, response, attrptr, data->num_displayed, data->displayed, widths); |
aaf19ab0 | 1765 | |
a492d345 MS |
1766 | while (attrptr && ippGetGroupTag(attrptr) > IPP_TAG_OPERATION) |
1767 | attrptr = ippNextAttribute(response); | |
aaf19ab0 | 1768 | } |
f8b3a85b | 1769 | } |
8e2e09c1 MS |
1770 | } |
1771 | else if (!data->prev_pass) | |
1772 | { | |
1773 | if (data->output == _CUPS_OUTPUT_PLIST) | |
f8b3a85b | 1774 | { |
8e2e09c1 MS |
1775 | cupsFilePuts(data->outfile, "<key>Errors</key>\n"); |
1776 | cupsFilePuts(data->outfile, "<array>\n"); | |
f8b3a85b | 1777 | |
8e2e09c1 MS |
1778 | for (error = (char *)cupsArrayFirst(data->errors); |
1779 | error; | |
1780 | error = (char *)cupsArrayNext(data->errors)) | |
1781 | print_xml_string(data->outfile, "string", error); | |
f8b3a85b | 1782 | |
8e2e09c1 MS |
1783 | cupsFilePuts(data->outfile, "</array>\n"); |
1784 | } | |
9fcdd250 | 1785 | |
8e2e09c1 MS |
1786 | if (data->output == _CUPS_OUTPUT_TEST || (data->output == _CUPS_OUTPUT_PLIST && data->outfile != cupsFileStdout())) |
1787 | { | |
1788 | for (error = (char *)cupsArrayFirst(data->errors); | |
1789 | error; | |
1790 | error = (char *)cupsArrayNext(data->errors)) | |
1791 | cupsFilePrintf(cupsFileStdout(), " %s\n", error); | |
f8b3a85b | 1792 | } |
8e2e09c1 | 1793 | } |
f8b3a85b | 1794 | |
8e2e09c1 MS |
1795 | if (data->num_displayed > 0 && !data->verbosity && response && (data->output == _CUPS_OUTPUT_TEST || (data->output == _CUPS_OUTPUT_PLIST && data->outfile != cupsFileStdout()))) |
1796 | { | |
a492d345 | 1797 | for (attrptr = ippFirstAttribute(response); attrptr; attrptr = ippNextAttribute(response)) |
d7225fc2 | 1798 | { |
a492d345 | 1799 | if (ippGetName(attrptr)) |
d7225fc2 | 1800 | { |
8e2e09c1 | 1801 | for (i = 0; i < data->num_displayed; i ++) |
d7225fc2 | 1802 | { |
a492d345 | 1803 | if (!strcmp(data->displayed[i], ippGetName(attrptr))) |
d7225fc2 | 1804 | { |
8e2e09c1 MS |
1805 | print_attr(data->outfile, data->output, attrptr, NULL); |
1806 | break; | |
d7225fc2 MS |
1807 | } |
1808 | } | |
1809 | } | |
1810 | } | |
ef416fc2 | 1811 | } |
1812 | ||
8e2e09c1 | 1813 | skip_error: |
dcb445bc | 1814 | |
8e2e09c1 MS |
1815 | if (data->output == _CUPS_OUTPUT_PLIST) |
1816 | cupsFilePuts(data->outfile, "</dict>\n"); | |
f8b3a85b | 1817 | |
f8b3a85b | 1818 | ippDelete(response); |
8e2e09c1 | 1819 | response = NULL; |
f8b3a85b | 1820 | |
8e2e09c1 | 1821 | for (i = 0; i < data->num_statuses; i ++) |
f8b3a85b | 1822 | { |
8e2e09c1 MS |
1823 | if (data->statuses[i].if_defined) |
1824 | free(data->statuses[i].if_defined); | |
1825 | if (data->statuses[i].if_not_defined) | |
1826 | free(data->statuses[i].if_not_defined); | |
1827 | if (data->statuses[i].define_match) | |
1828 | free(data->statuses[i].define_match); | |
1829 | if (data->statuses[i].define_no_match) | |
1830 | free(data->statuses[i].define_no_match); | |
f8b3a85b | 1831 | } |
8e2e09c1 | 1832 | data->num_statuses = 0; |
f8b3a85b | 1833 | |
8e2e09c1 | 1834 | for (i = data->num_expects, expect = data->expects; i > 0; i --, expect ++) |
f8b3a85b MS |
1835 | { |
1836 | free(expect->name); | |
1837 | if (expect->of_type) | |
1838 | free(expect->of_type); | |
1839 | if (expect->same_count_as) | |
1840 | free(expect->same_count_as); | |
1841 | if (expect->if_defined) | |
1842 | free(expect->if_defined); | |
7cf5915e MS |
1843 | if (expect->if_not_defined) |
1844 | free(expect->if_not_defined); | |
f8b3a85b MS |
1845 | if (expect->with_value) |
1846 | free(expect->with_value); | |
030ae6a1 MS |
1847 | if (expect->define_match) |
1848 | free(expect->define_match); | |
1849 | if (expect->define_no_match) | |
1850 | free(expect->define_no_match); | |
1851 | if (expect->define_value) | |
1852 | free(expect->define_value); | |
f8b3a85b | 1853 | } |
8e2e09c1 MS |
1854 | data->num_expects = 0; |
1855 | ||
1856 | for (i = 0; i < data->num_displayed; i ++) | |
1857 | free(data->displayed[i]); | |
1858 | data->num_displayed = 0; | |
1859 | ||
1860 | return (data->ignore_errors || data->prev_pass); | |
1861 | } | |
1862 | ||
1863 | ||
1864 | /* | |
1865 | * 'do_tests()' - Do tests as specified in the test file. | |
1866 | */ | |
1867 | ||
1868 | static int /* O - 1 on success, 0 on failure */ | |
1869 | do_tests(const char *testfile, /* I - Test file to use */ | |
1870 | _ipp_vars_t *vars, /* I - Variables */ | |
1871 | _cups_testdata_t *data) /* I - Test data */ | |
1872 | { | |
1873 | http_encryption_t encryption; /* Encryption mode */ | |
1874 | ||
1875 | ||
1876 | /* | |
1877 | * Connect to the printer/server... | |
1878 | */ | |
1879 | ||
1880 | if (!_cups_strcasecmp(vars->scheme, "https") || !_cups_strcasecmp(vars->scheme, "ipps")) | |
1881 | encryption = HTTP_ENCRYPTION_ALWAYS; | |
1882 | else | |
1883 | encryption = data->encryption; | |
1884 | ||
1885 | if ((data->http = httpConnect2(vars->host, vars->port, NULL, data->family, encryption, 1, 30000, NULL)) == NULL) | |
1886 | { | |
1887 | print_fatal_error(data, "Unable to connect to \"%s\" on port %d - %s", vars->host, vars->port, cupsLastErrorString()); | |
1888 | return (0); | |
1889 | } | |
1890 | ||
1891 | #ifdef HAVE_LIBZ | |
1892 | httpSetDefaultField(data->http, HTTP_FIELD_ACCEPT_ENCODING, "deflate, gzip, identity"); | |
1893 | #else | |
1894 | httpSetDefaultField(data->http, HTTP_FIELD_ACCEPT_ENCODING, "identity"); | |
1895 | #endif /* HAVE_LIBZ */ | |
f8b3a85b | 1896 | |
8e2e09c1 MS |
1897 | if (data->timeout > 0.0) |
1898 | httpSetTimeout(data->http, data->timeout, timeout_cb, NULL); | |
f8b3a85b | 1899 | |
8e2e09c1 MS |
1900 | /* |
1901 | * Run tests... | |
1902 | */ | |
1903 | ||
a166e933 | 1904 | _ippFileParse(vars, testfile, (void *)data); |
8e2e09c1 MS |
1905 | |
1906 | /* | |
1907 | * Close connection and return... | |
1908 | */ | |
1909 | ||
1910 | httpClose(data->http); | |
1911 | data->http = NULL; | |
1912 | ||
1913 | return (data->pass); | |
f8b3a85b MS |
1914 | } |
1915 | ||
1916 | ||
1917 | /* | |
08102776 | 1918 | * 'error_cb()' - Print/add an error message. |
f8b3a85b MS |
1919 | */ |
1920 | ||
08102776 MS |
1921 | static int /* O - 1 to continue, 0 to stop */ |
1922 | error_cb(_ipp_file_t *f, /* I - IPP file data */ | |
1923 | _cups_testdata_t *data, /* I - Test data */ | |
1924 | const char *error) /* I - Error message */ | |
f8b3a85b | 1925 | { |
8e2e09c1 MS |
1926 | (void)f; |
1927 | ||
1928 | print_fatal_error(data, "%s", error); | |
1929 | ||
08102776 | 1930 | return (1); |
f8b3a85b MS |
1931 | } |
1932 | ||
1933 | ||
1934 | /* | |
1935 | * 'expect_matches()' - Return true if the tag matches the specification. | |
1936 | */ | |
aaf19ab0 | 1937 | |
5a662dc0 MS |
1938 | static int /* O - 1 if matches, 0 otherwise */ |
1939 | expect_matches( | |
1940 | _cups_expect_t *expect, /* I - Expected attribute */ | |
1941 | ipp_tag_t value_tag) /* I - Value tag for attribute */ | |
1942 | { | |
1943 | int match; /* Match? */ | |
1944 | char *of_type, /* Type name to match */ | |
f8b3a85b MS |
1945 | *next, /* Next name to match */ |
1946 | sep; /* Separator character */ | |
5a662dc0 MS |
1947 | |
1948 | ||
1949 | /* | |
1950 | * If we don't expect a particular type, return immediately... | |
1951 | */ | |
1952 | ||
1953 | if (!expect->of_type) | |
1954 | return (1); | |
1955 | ||
8e2e09c1 MS |
1956 | /* |
1957 | * Parse the "of_type" value since the string can contain multiple attribute | |
1958 | * types separated by "," or "|"... | |
1959 | */ | |
f8b3a85b | 1960 | |
8e2e09c1 MS |
1961 | for (of_type = expect->of_type, match = 0; !match && *of_type; of_type = next) |
1962 | { | |
1963 | /* | |
1964 | * Find the next separator, and set it (temporarily) to nul if present. | |
1965 | */ | |
f8b3a85b | 1966 | |
8e2e09c1 | 1967 | for (next = of_type; *next && *next != '|' && *next != ','; next ++); |
f8b3a85b | 1968 | |
8e2e09c1 MS |
1969 | if ((sep = *next) != '\0') |
1970 | *next = '\0'; | |
f8b3a85b | 1971 | |
8e2e09c1 MS |
1972 | /* |
1973 | * Support some meta-types to make it easier to write the test file. | |
1974 | */ | |
f8b3a85b | 1975 | |
8e2e09c1 MS |
1976 | if (!strcmp(of_type, "text")) |
1977 | match = value_tag == IPP_TAG_TEXTLANG || value_tag == IPP_TAG_TEXT; | |
1978 | else if (!strcmp(of_type, "name")) | |
1979 | match = value_tag == IPP_TAG_NAMELANG || value_tag == IPP_TAG_NAME; | |
1980 | else if (!strcmp(of_type, "collection")) | |
1981 | match = value_tag == IPP_TAG_BEGIN_COLLECTION; | |
1982 | else | |
1983 | match = value_tag == ippTagValue(of_type); | |
f8b3a85b | 1984 | |
8e2e09c1 MS |
1985 | /* |
1986 | * Restore the separator if we have one... | |
1987 | */ | |
f8b3a85b | 1988 | |
8e2e09c1 MS |
1989 | if (sep) |
1990 | *next++ = sep; | |
1991 | } | |
f8b3a85b | 1992 | |
8e2e09c1 | 1993 | return (match); |
f8b3a85b MS |
1994 | } |
1995 | ||
1996 | ||
1997 | /* | |
1998 | * 'get_filename()' - Get a filename based on the current test file. | |
1999 | */ | |
2000 | ||
2001 | static char * /* O - Filename */ | |
2002 | get_filename(const char *testfile, /* I - Current test file */ | |
2003 | char *dst, /* I - Destination filename */ | |
2004 | const char *src, /* I - Source filename */ | |
2005 | size_t dstsize) /* I - Size of destination buffer */ | |
2006 | { | |
2007 | char *dstptr; /* Pointer into destination */ | |
2008 | _cups_globals_t *cg = _cupsGlobals(); | |
2009 | /* Global data */ | |
2010 | ||
2011 | ||
2012 | if (*src == '<' && src[strlen(src) - 1] == '>') | |
2013 | { | |
2014 | /* | |
aaf19ab0 | 2015 | * Map <filename> to CUPS_DATADIR/ipptool/filename... |
f8b3a85b MS |
2016 | */ |
2017 | ||
aaf19ab0 | 2018 | snprintf(dst, dstsize, "%s/ipptool/%s", cg->cups_datadir, src + 1); |
f8b3a85b MS |
2019 | dstptr = dst + strlen(dst) - 1; |
2020 | if (*dstptr == '>') | |
2021 | *dstptr = '\0'; | |
2022 | } | |
8e2e09c1 | 2023 | else if (!access(src, R_OK) || *src == '/' |
24a06ed3 | 2024 | #ifdef _WIN32 |
12f89d24 | 2025 | || (isalpha(*src & 255) && src[1] == ':') |
24a06ed3 | 2026 | #endif /* _WIN32 */ |
12f89d24 | 2027 | ) |
f8b3a85b MS |
2028 | { |
2029 | /* | |
2030 | * Use the path as-is... | |
2031 | */ | |
2032 | ||
2033 | strlcpy(dst, src, dstsize); | |
2034 | } | |
2035 | else | |
2036 | { | |
2037 | /* | |
2038 | * Make path relative to testfile... | |
2039 | */ | |
2040 | ||
2041 | strlcpy(dst, testfile, dstsize); | |
2042 | if ((dstptr = strrchr(dst, '/')) != NULL) | |
2043 | dstptr ++; | |
2044 | else | |
2045 | dstptr = dst; /* Should never happen */ | |
2046 | ||
7e86f2f6 | 2047 | strlcpy(dstptr, src, dstsize - (size_t)(dstptr - dst)); |
f8b3a85b MS |
2048 | } |
2049 | ||
2050 | return (dst); | |
2051 | } | |
2052 | ||
2053 | ||
890a10b7 MS |
2054 | /* |
2055 | * 'get_string()' - Get a pointer to a string value or the portion of interest. | |
2056 | */ | |
2057 | ||
1b01c393 | 2058 | static const char * /* O - Pointer to string */ |
890a10b7 MS |
2059 | get_string(ipp_attribute_t *attr, /* I - IPP attribute */ |
2060 | int element, /* I - Element to fetch */ | |
2061 | int flags, /* I - Value ("with") flags */ | |
2062 | char *buffer, /* I - Temporary buffer */ | |
2063 | size_t bufsize) /* I - Size of temporary buffer */ | |
2064 | { | |
1b01c393 MS |
2065 | const char *value; /* Value */ |
2066 | char *ptr, /* Pointer into value */ | |
2067 | scheme[256], /* URI scheme */ | |
2068 | userpass[256], /* Username/password */ | |
2069 | hostname[256], /* Hostname */ | |
2070 | resource[1024]; /* Resource */ | |
2071 | int port; /* Port number */ | |
890a10b7 MS |
2072 | |
2073 | ||
1b01c393 | 2074 | value = ippGetString(attr, element, NULL); |
890a10b7 MS |
2075 | |
2076 | if (flags & _CUPS_WITH_HOSTNAME) | |
2077 | { | |
1b01c393 | 2078 | if (httpSeparateURI(HTTP_URI_CODING_ALL, value, scheme, sizeof(scheme), userpass, sizeof(userpass), buffer, (int)bufsize, &port, resource, sizeof(resource)) < HTTP_URI_STATUS_OK) |
7e86f2f6 MS |
2079 | buffer[0] = '\0'; |
2080 | ||
e9f9e650 MS |
2081 | ptr = buffer + strlen(buffer) - 1; |
2082 | if (ptr >= buffer && *ptr == '.') | |
2083 | *ptr = '\0'; /* Drop trailing "." */ | |
2084 | ||
7e86f2f6 | 2085 | return (buffer); |
890a10b7 MS |
2086 | } |
2087 | else if (flags & _CUPS_WITH_RESOURCE) | |
2088 | { | |
1b01c393 | 2089 | if (httpSeparateURI(HTTP_URI_CODING_ALL, value, scheme, sizeof(scheme), userpass, sizeof(userpass), hostname, sizeof(hostname), &port, buffer, (int)bufsize) < HTTP_URI_STATUS_OK) |
7e86f2f6 MS |
2090 | buffer[0] = '\0'; |
2091 | ||
2092 | return (buffer); | |
890a10b7 MS |
2093 | } |
2094 | else if (flags & _CUPS_WITH_SCHEME) | |
2095 | { | |
1b01c393 MS |
2096 | if (httpSeparateURI(HTTP_URI_CODING_ALL, value, buffer, (int)bufsize, userpass, sizeof(userpass), hostname, sizeof(hostname), &port, resource, sizeof(resource)) < HTTP_URI_STATUS_OK) |
2097 | buffer[0] = '\0'; | |
2098 | ||
2099 | return (buffer); | |
2100 | } | |
2101 | else if (ippGetValueTag(attr) == IPP_TAG_URI && (!strncmp(value, "ipp://", 6) || !strncmp(value, "http://", 7) || !strncmp(value, "ipps://", 7) || !strncmp(value, "https://", 8))) | |
2102 | { | |
2103 | http_uri_status_t status = httpSeparateURI(HTTP_URI_CODING_ALL, value, scheme, sizeof(scheme), userpass, sizeof(userpass), hostname, sizeof(hostname), &port, resource, sizeof(resource)); | |
2104 | ||
2105 | if (status < HTTP_URI_STATUS_OK) | |
2106 | { | |
2107 | /* | |
2108 | * Bad URI... | |
2109 | */ | |
2110 | ||
7e86f2f6 | 2111 | buffer[0] = '\0'; |
1b01c393 MS |
2112 | } |
2113 | else | |
2114 | { | |
2115 | /* | |
2116 | * Normalize URI with no trailing dot... | |
2117 | */ | |
2118 | ||
2119 | if ((ptr = hostname + strlen(hostname) - 1) >= hostname && *ptr == '.') | |
2120 | *ptr = '\0'; | |
2121 | ||
40e7858e | 2122 | httpAssembleURI(HTTP_URI_CODING_ALL, buffer, (int)bufsize, scheme, userpass, hostname, port, resource); |
1b01c393 | 2123 | } |
7e86f2f6 MS |
2124 | |
2125 | return (buffer); | |
890a10b7 MS |
2126 | } |
2127 | else | |
1b01c393 | 2128 | return (value); |
890a10b7 MS |
2129 | } |
2130 | ||
2131 | ||
f8b3a85b | 2132 | /* |
08102776 | 2133 | * 'init_data()' - Initialize test data. |
f8b3a85b MS |
2134 | */ |
2135 | ||
08102776 MS |
2136 | static void |
2137 | init_data(_cups_testdata_t *data) /* I - Data */ | |
f8b3a85b | 2138 | { |
08102776 MS |
2139 | memset(data, 0, sizeof(_cups_testdata_t)); |
2140 | ||
2141 | data->output = _CUPS_OUTPUT_LIST; | |
2142 | data->outfile = cupsFileStdout(); | |
2143 | data->family = AF_UNSPEC; | |
2144 | data->def_transfer = _CUPS_TRANSFER_AUTO; | |
2145 | data->def_version = 11; | |
8e2e09c1 MS |
2146 | data->errors = cupsArrayNew3(NULL, NULL, NULL, 0, (cups_acopy_func_t)strdup, (cups_afree_func_t)free); |
2147 | data->pass = 1; | |
08102776 | 2148 | data->prev_pass = 1; |
8e2e09c1 | 2149 | data->request_id = (CUPS_RAND() % 1000) * 137 + 1; |
08102776 | 2150 | data->show_header = 1; |
f8b3a85b MS |
2151 | } |
2152 | ||
2153 | ||
2154 | /* | |
2155 | * 'iso_date()' - Return an ISO 8601 date/time string for the given IPP dateTime | |
2156 | * value. | |
2157 | */ | |
2158 | ||
2159 | static char * /* O - ISO 8601 date/time string */ | |
08102776 | 2160 | iso_date(const ipp_uchar_t *date) /* I - IPP (RFC 1903) date/time value */ |
f8b3a85b | 2161 | { |
a4845881 MS |
2162 | time_t utctime; /* UTC time since 1970 */ |
2163 | struct tm *utcdate; /* UTC date/time */ | |
f8b3a85b MS |
2164 | static char buffer[255]; /* String buffer */ |
2165 | ||
2166 | ||
a4845881 MS |
2167 | utctime = ippDateToTime(date); |
2168 | utcdate = gmtime(&utctime); | |
2169 | ||
2170 | snprintf(buffer, sizeof(buffer), "%04d-%02d-%02dT%02d:%02d:%02dZ", | |
2171 | utcdate->tm_year + 1900, utcdate->tm_mon + 1, utcdate->tm_mday, | |
2172 | utcdate->tm_hour, utcdate->tm_min, utcdate->tm_sec); | |
f8b3a85b MS |
2173 | |
2174 | return (buffer); | |
2175 | } | |
2176 | ||
2177 | ||
9fcdd250 MS |
2178 | /* |
2179 | * 'pause_message()' - Display the message and pause until the user presses a key. | |
2180 | */ | |
2181 | ||
2182 | static void | |
2183 | pause_message(const char *message) /* I - Message */ | |
2184 | { | |
24a06ed3 | 2185 | #ifdef _WIN32 |
9fcdd250 MS |
2186 | HANDLE tty; /* Console handle */ |
2187 | DWORD mode; /* Console mode */ | |
2188 | char key; /* Key press */ | |
2189 | DWORD bytes; /* Bytes read for key press */ | |
2190 | ||
2191 | ||
2192 | /* | |
2193 | * Disable input echo and set raw input... | |
2194 | */ | |
2195 | ||
2196 | if ((tty = GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE) | |
2197 | return; | |
2198 | ||
2199 | if (!GetConsoleMode(tty, &mode)) | |
2200 | return; | |
2201 | ||
2202 | if (!SetConsoleMode(tty, 0)) | |
2203 | return; | |
2204 | ||
2205 | #else | |
2206 | int tty; /* /dev/tty - never read from stdin */ | |
2207 | struct termios original, /* Original input mode */ | |
2208 | noecho; /* No echo input mode */ | |
2209 | char key; /* Current key press */ | |
2210 | ||
2211 | ||
2212 | /* | |
2213 | * Disable input echo and set raw input... | |
2214 | */ | |
2215 | ||
2216 | if ((tty = open("/dev/tty", O_RDONLY)) < 0) | |
2217 | return; | |
2218 | ||
2219 | if (tcgetattr(tty, &original)) | |
2220 | { | |
2221 | close(tty); | |
2222 | return; | |
2223 | } | |
2224 | ||
2225 | noecho = original; | |
2226 | noecho.c_lflag &= (tcflag_t)~(ICANON | ECHO | ECHOE | ISIG); | |
2227 | ||
2228 | if (tcsetattr(tty, TCSAFLUSH, &noecho)) | |
2229 | { | |
2230 | close(tty); | |
2231 | return; | |
2232 | } | |
24a06ed3 | 2233 | #endif /* _WIN32 */ |
9fcdd250 MS |
2234 | |
2235 | /* | |
2236 | * Display the prompt... | |
2237 | */ | |
2238 | ||
35fc2243 | 2239 | cupsFilePrintf(cupsFileStdout(), "%s\n---- PRESS ANY KEY ----", message); |
9fcdd250 | 2240 | |
24a06ed3 | 2241 | #ifdef _WIN32 |
9fcdd250 MS |
2242 | /* |
2243 | * Read a key... | |
2244 | */ | |
2245 | ||
2246 | ReadFile(tty, &key, 1, &bytes, NULL); | |
2247 | ||
2248 | /* | |
2249 | * Cleanup... | |
2250 | */ | |
2251 | ||
2252 | SetConsoleMode(tty, mode); | |
2253 | ||
2254 | #else | |
2255 | /* | |
2256 | * Read a key... | |
2257 | */ | |
2258 | ||
2259 | read(tty, &key, 1); | |
2260 | ||
2261 | /* | |
2262 | * Cleanup... | |
2263 | */ | |
2264 | ||
2265 | tcsetattr(tty, TCSAFLUSH, &original); | |
2266 | close(tty); | |
24a06ed3 | 2267 | #endif /* _WIN32 */ |
9fcdd250 MS |
2268 | |
2269 | /* | |
2270 | * Erase the "press any key" prompt... | |
2271 | */ | |
2272 | ||
2a75f21b | 2273 | cupsFilePuts(cupsFileStdout(), "\r \r"); |
9fcdd250 MS |
2274 | } |
2275 | ||
2276 | ||
ef416fc2 | 2277 | /* |
2278 | * 'print_attr()' - Print an attribute on the screen. | |
2279 | */ | |
2280 | ||
5a662dc0 | 2281 | static void |
8e2e09c1 MS |
2282 | print_attr(cups_file_t *outfile, /* I - Output file */ |
2283 | int output, /* I - Output format */ | |
9fcdd250 | 2284 | ipp_attribute_t *attr, /* I - Attribute to print */ |
e60ec91f | 2285 | ipp_tag_t *group) /* IO - Current group */ |
ef416fc2 | 2286 | { |
a492d345 MS |
2287 | int i, /* Looping var */ |
2288 | count; /* Number of values */ | |
f8b3a85b | 2289 | ipp_attribute_t *colattr; /* Collection attribute */ |
ef416fc2 | 2290 | |
2291 | ||
8e2e09c1 | 2292 | if (output == _CUPS_OUTPUT_PLIST) |
ef416fc2 | 2293 | { |
a492d345 | 2294 | if (!ippGetName(attr) || (group && *group != ippGetGroupTag(attr))) |
f8b3a85b | 2295 | { |
a492d345 | 2296 | if (ippGetGroupTag(attr) != IPP_TAG_ZERO) |
c1420c87 | 2297 | { |
8e2e09c1 MS |
2298 | cupsFilePuts(outfile, "</dict>\n"); |
2299 | cupsFilePuts(outfile, "<dict>\n"); | |
c1420c87 | 2300 | } |
e60ec91f MS |
2301 | |
2302 | if (group) | |
a492d345 | 2303 | *group = ippGetGroupTag(attr); |
f8b3a85b MS |
2304 | } |
2305 | ||
a492d345 | 2306 | if (!ippGetName(attr)) |
e60ec91f MS |
2307 | return; |
2308 | ||
a492d345 MS |
2309 | print_xml_string(outfile, "key", ippGetName(attr)); |
2310 | if ((count = ippGetCount(attr)) > 1) | |
8e2e09c1 | 2311 | cupsFilePuts(outfile, "<array>\n"); |
ef416fc2 | 2312 | |
a492d345 | 2313 | switch (ippGetValueTag(attr)) |
9b66acc5 MS |
2314 | { |
2315 | case IPP_TAG_INTEGER : | |
2316 | case IPP_TAG_ENUM : | |
a492d345 MS |
2317 | for (i = 0; i < count; i ++) |
2318 | cupsFilePrintf(outfile, "<integer>%d</integer>\n", ippGetInteger(attr, i)); | |
9b66acc5 | 2319 | break; |
ef416fc2 | 2320 | |
9b66acc5 | 2321 | case IPP_TAG_BOOLEAN : |
a492d345 MS |
2322 | for (i = 0; i < count; i ++) |
2323 | cupsFilePuts(outfile, ippGetBoolean(attr, i) ? "<true />\n" : "<false />\n"); | |
9b66acc5 | 2324 | break; |
ef416fc2 | 2325 | |
9b66acc5 | 2326 | case IPP_TAG_RANGE : |
a492d345 MS |
2327 | for (i = 0; i < count; i ++) |
2328 | { | |
2329 | int lower, upper; /* Lower and upper ranges */ | |
2330 | ||
2331 | lower = ippGetRange(attr, i, &upper); | |
2332 | cupsFilePrintf(outfile, "<dict><key>lower</key><integer>%d</integer><key>upper</key><integer>%d</integer></dict>\n", lower, upper); | |
2333 | } | |
9b66acc5 | 2334 | break; |
ef416fc2 | 2335 | |
9b66acc5 | 2336 | case IPP_TAG_RESOLUTION : |
a492d345 MS |
2337 | for (i = 0; i < count; i ++) |
2338 | { | |
2339 | int xres, yres; /* Resolution values */ | |
2340 | ipp_res_t units; /* Resolution units */ | |
2341 | ||
2342 | xres = ippGetResolution(attr, i, &yres, &units); | |
2343 | cupsFilePrintf(outfile, "<dict><key>xres</key><integer>%d</integer><key>yres</key><integer>%d</integer><key>units</key><string>%s</string></dict>\n", xres, yres, units == IPP_RES_PER_INCH ? "dpi" : "dpcm"); | |
2344 | } | |
9b66acc5 | 2345 | break; |
ef416fc2 | 2346 | |
9b66acc5 | 2347 | case IPP_TAG_DATE : |
a492d345 MS |
2348 | for (i = 0; i < count; i ++) |
2349 | cupsFilePrintf(outfile, "<date>%s</date>\n", iso_date(ippGetDate(attr, i))); | |
9b66acc5 | 2350 | break; |
f8b3a85b | 2351 | |
9b66acc5 | 2352 | case IPP_TAG_STRING : |
a492d345 | 2353 | for (i = 0; i < count; i ++) |
5a9febac | 2354 | { |
a492d345 MS |
2355 | int datalen; /* Length of data */ |
2356 | void *data = ippGetOctetString(attr, i, &datalen); | |
2357 | /* Data */ | |
9fcdd250 | 2358 | char buffer[IPP_MAX_LENGTH * 5 / 4 + 1]; |
a492d345 | 2359 | /* Base64 output buffer */ |
5a9febac | 2360 | |
a492d345 | 2361 | cupsFilePrintf(outfile, "<data>%s</data>\n", httpEncode64_2(buffer, sizeof(buffer), data, datalen)); |
5a9febac MS |
2362 | } |
2363 | break; | |
2364 | ||
9b66acc5 MS |
2365 | case IPP_TAG_TEXT : |
2366 | case IPP_TAG_NAME : | |
2367 | case IPP_TAG_KEYWORD : | |
9b66acc5 | 2368 | case IPP_TAG_URI : |
1a00f9bf MS |
2369 | case IPP_TAG_URISCHEME : |
2370 | case IPP_TAG_CHARSET : | |
9b66acc5 | 2371 | case IPP_TAG_LANGUAGE : |
1a00f9bf | 2372 | case IPP_TAG_MIMETYPE : |
a492d345 MS |
2373 | for (i = 0; i < count; i ++) |
2374 | print_xml_string(outfile, "string", ippGetString(attr, i, NULL)); | |
9b66acc5 | 2375 | break; |
f8b3a85b | 2376 | |
9b66acc5 MS |
2377 | case IPP_TAG_TEXTLANG : |
2378 | case IPP_TAG_NAMELANG : | |
a492d345 | 2379 | for (i = 0; i < count; i ++) |
9fcdd250 | 2380 | { |
a492d345 MS |
2381 | const char *s, /* String */ |
2382 | *lang; /* Language */ | |
2383 | ||
2384 | s = ippGetString(attr, i, &lang); | |
8e2e09c1 | 2385 | cupsFilePuts(outfile, "<dict><key>language</key><string>"); |
a492d345 | 2386 | print_xml_string(outfile, NULL, lang); |
8e2e09c1 | 2387 | cupsFilePuts(outfile, "</string><key>string</key><string>"); |
a492d345 | 2388 | print_xml_string(outfile, NULL, s); |
8e2e09c1 | 2389 | cupsFilePuts(outfile, "</string></dict>\n"); |
9fcdd250 | 2390 | } |
9b66acc5 | 2391 | break; |
ef416fc2 | 2392 | |
9b66acc5 | 2393 | case IPP_TAG_BEGIN_COLLECTION : |
a492d345 | 2394 | for (i = 0; i < count; i ++) |
f8b3a85b | 2395 | { |
a492d345 MS |
2396 | ipp_t *col = ippGetCollection(attr, i); |
2397 | /* Collection value */ | |
2398 | ||
8e2e09c1 | 2399 | cupsFilePuts(outfile, "<dict>\n"); |
a492d345 | 2400 | for (colattr = ippFirstAttribute(col); colattr; colattr = ippNextAttribute(col)) |
8e2e09c1 MS |
2401 | print_attr(outfile, output, colattr, NULL); |
2402 | cupsFilePuts(outfile, "</dict>\n"); | |
f8b3a85b | 2403 | } |
9b66acc5 | 2404 | break; |
ef416fc2 | 2405 | |
9b66acc5 | 2406 | default : |
a492d345 | 2407 | cupsFilePrintf(outfile, "<string><<%s>></string>\n", ippTagString(ippGetValueTag(attr))); |
9b66acc5 MS |
2408 | break; |
2409 | } | |
ef416fc2 | 2410 | |
a492d345 | 2411 | if (count > 1) |
8e2e09c1 | 2412 | cupsFilePuts(outfile, "</array>\n"); |
f8b3a85b MS |
2413 | } |
2414 | else | |
9b66acc5 | 2415 | { |
2c9bf31e | 2416 | char buffer[131072]; /* Value buffer */ |
9b66acc5 | 2417 | |
8e2e09c1 | 2418 | if (output == _CUPS_OUTPUT_TEST) |
9b66acc5 | 2419 | { |
a492d345 | 2420 | if (!ippGetName(attr)) |
9b66acc5 | 2421 | { |
8e2e09c1 | 2422 | cupsFilePuts(outfile, " -- separator --\n"); |
9b66acc5 MS |
2423 | return; |
2424 | } | |
2425 | ||
a492d345 | 2426 | cupsFilePrintf(outfile, " %s (%s%s) = ", ippGetName(attr), ippGetCount(attr) > 1 ? "1setOf " : "", ippTagString(ippGetValueTag(attr))); |
9b66acc5 MS |
2427 | } |
2428 | ||
a2326b5b | 2429 | ippAttributeString(attr, buffer, sizeof(buffer)); |
8e2e09c1 | 2430 | cupsFilePrintf(outfile, "%s\n", buffer); |
9b66acc5 | 2431 | } |
ef416fc2 | 2432 | } |
2433 | ||
2434 | ||
aaf19ab0 MS |
2435 | /* |
2436 | * 'print_csv()' - Print a line of CSV text. | |
2437 | */ | |
2438 | ||
2439 | static void | |
2440 | print_csv( | |
08102776 | 2441 | _cups_testdata_t *data, /* I - Test data */ |
a492d345 | 2442 | ipp_t *ipp, /* I - Response message */ |
08102776 MS |
2443 | ipp_attribute_t *attr, /* I - First attribute for line */ |
2444 | int num_displayed, /* I - Number of attributes to display */ | |
2445 | char **displayed, /* I - Attributes to display */ | |
2446 | size_t *widths) /* I - Column widths */ | |
aaf19ab0 MS |
2447 | { |
2448 | int i; /* Looping var */ | |
2449 | size_t maxlength; /* Max length of all columns */ | |
2450 | char *buffer, /* String buffer */ | |
2451 | *bufptr; /* Pointer into buffer */ | |
2452 | ipp_attribute_t *current; /* Current attribute */ | |
2453 | ||
2454 | ||
2455 | /* | |
2456 | * Get the maximum string length we have to show and allocate... | |
2457 | */ | |
2458 | ||
2459 | for (i = 1, maxlength = widths[0]; i < num_displayed; i ++) | |
2460 | if (widths[i] > maxlength) | |
2461 | maxlength = widths[i]; | |
2462 | ||
2463 | maxlength += 2; | |
2464 | ||
2465 | if ((buffer = malloc(maxlength)) == NULL) | |
2466 | return; | |
2467 | ||
2468 | /* | |
2469 | * Loop through the attributes to display... | |
2470 | */ | |
2471 | ||
2472 | if (attr) | |
2473 | { | |
2474 | for (i = 0; i < num_displayed; i ++) | |
2475 | { | |
2476 | if (i) | |
08102776 | 2477 | cupsFilePutChar(data->outfile, ','); |
aaf19ab0 MS |
2478 | |
2479 | buffer[0] = '\0'; | |
2480 | ||
a492d345 | 2481 | for (current = attr; current; current = ippNextAttribute(ipp)) |
aaf19ab0 | 2482 | { |
a492d345 | 2483 | if (!ippGetName(current)) |
aaf19ab0 | 2484 | break; |
a492d345 | 2485 | else if (!strcmp(ippGetName(current), displayed[i])) |
aaf19ab0 | 2486 | { |
a2326b5b | 2487 | ippAttributeString(current, buffer, maxlength); |
aaf19ab0 MS |
2488 | break; |
2489 | } | |
2490 | } | |
2491 | ||
2492 | if (strchr(buffer, ',') != NULL || strchr(buffer, '\"') != NULL || | |
2493 | strchr(buffer, '\\') != NULL) | |
2494 | { | |
2a75f21b | 2495 | cupsFilePutChar(cupsFileStdout(), '\"'); |
aaf19ab0 MS |
2496 | for (bufptr = buffer; *bufptr; bufptr ++) |
2497 | { | |
2498 | if (*bufptr == '\\' || *bufptr == '\"') | |
2a75f21b MS |
2499 | cupsFilePutChar(cupsFileStdout(), '\\'); |
2500 | cupsFilePutChar(cupsFileStdout(), *bufptr); | |
aaf19ab0 | 2501 | } |
2a75f21b | 2502 | cupsFilePutChar(cupsFileStdout(), '\"'); |
aaf19ab0 MS |
2503 | } |
2504 | else | |
08102776 | 2505 | cupsFilePuts(data->outfile, buffer); |
aaf19ab0 | 2506 | } |
2a75f21b | 2507 | cupsFilePutChar(cupsFileStdout(), '\n'); |
aaf19ab0 MS |
2508 | } |
2509 | else | |
2510 | { | |
2511 | for (i = 0; i < num_displayed; i ++) | |
2512 | { | |
2513 | if (i) | |
2a75f21b | 2514 | cupsFilePutChar(cupsFileStdout(), ','); |
aaf19ab0 | 2515 | |
08102776 | 2516 | cupsFilePuts(data->outfile, displayed[i]); |
aaf19ab0 | 2517 | } |
2a75f21b | 2518 | cupsFilePutChar(cupsFileStdout(), '\n'); |
aaf19ab0 MS |
2519 | } |
2520 | ||
2521 | free(buffer); | |
2522 | } | |
2523 | ||
2524 | ||
f8b3a85b MS |
2525 | /* |
2526 | * 'print_fatal_error()' - Print a fatal error message. | |
2527 | */ | |
2528 | ||
2529 | static void | |
08102776 MS |
2530 | print_fatal_error( |
2531 | _cups_testdata_t *data, /* I - Test data */ | |
2532 | const char *s, /* I - Printf-style format string */ | |
2533 | ...) /* I - Additional arguments as needed */ | |
f8b3a85b MS |
2534 | { |
2535 | char buffer[10240]; /* Format buffer */ | |
2536 | va_list ap; /* Pointer to arguments */ | |
2537 | ||
2538 | ||
2539 | /* | |
2540 | * Format the error message... | |
2541 | */ | |
2542 | ||
2543 | va_start(ap, s); | |
2544 | vsnprintf(buffer, sizeof(buffer), s, ap); | |
2545 | va_end(ap); | |
2546 | ||
2547 | /* | |
2548 | * Then output it... | |
2549 | */ | |
2550 | ||
8e2e09c1 | 2551 | if (data->output == _CUPS_OUTPUT_PLIST) |
f8b3a85b | 2552 | { |
8e2e09c1 MS |
2553 | print_xml_header(data); |
2554 | print_xml_trailer(data, 0, buffer); | |
f8b3a85b | 2555 | } |
9fcdd250 MS |
2556 | |
2557 | _cupsLangPrintf(stderr, "ipptool: %s", buffer); | |
aaf19ab0 MS |
2558 | } |
2559 | ||
2560 | ||
6646208b MS |
2561 | /* |
2562 | * 'print_ippserver_attr()' - Print a attribute suitable for use by ippserver. | |
2563 | */ | |
2564 | ||
2565 | static void | |
2566 | print_ippserver_attr( | |
08102776 MS |
2567 | _cups_testdata_t *data, /* I - Test data */ |
2568 | ipp_attribute_t *attr, /* I - Attribute to print */ | |
2569 | int indent) /* I - Indentation level */ | |
6646208b MS |
2570 | { |
2571 | int i, /* Looping var */ | |
2572 | count = ippGetCount(attr); | |
2573 | /* Number of values */ | |
2574 | ipp_attribute_t *colattr; /* Collection attribute */ | |
2575 | ||
2576 | ||
2577 | if (indent == 0) | |
08102776 | 2578 | cupsFilePrintf(data->outfile, "ATTR %s %s", ippTagString(ippGetValueTag(attr)), ippGetName(attr)); |
6646208b | 2579 | else |
08102776 | 2580 | cupsFilePrintf(data->outfile, "%*sMEMBER %s %s", indent, "", ippTagString(ippGetValueTag(attr)), ippGetName(attr)); |
6646208b MS |
2581 | |
2582 | switch (ippGetValueTag(attr)) | |
2583 | { | |
2584 | case IPP_TAG_INTEGER : | |
2585 | case IPP_TAG_ENUM : | |
2586 | for (i = 0; i < count; i ++) | |
08102776 | 2587 | cupsFilePrintf(data->outfile, "%s%d", i ? "," : " ", ippGetInteger(attr, i)); |
6646208b MS |
2588 | break; |
2589 | ||
2590 | case IPP_TAG_BOOLEAN : | |
08102776 | 2591 | cupsFilePuts(data->outfile, ippGetBoolean(attr, 0) ? " true" : " false"); |
6646208b MS |
2592 | |
2593 | for (i = 1; i < count; i ++) | |
08102776 | 2594 | cupsFilePuts(data->outfile, ippGetBoolean(attr, 1) ? ",true" : ",false"); |
6646208b MS |
2595 | break; |
2596 | ||
2597 | case IPP_TAG_RANGE : | |
2598 | for (i = 0; i < count; i ++) | |
2599 | { | |
2600 | int upper, lower = ippGetRange(attr, i, &upper); | |
2601 | ||
08102776 | 2602 | cupsFilePrintf(data->outfile, "%s%d-%d", i ? "," : " ", lower, upper); |
6646208b MS |
2603 | } |
2604 | break; | |
2605 | ||
2606 | case IPP_TAG_RESOLUTION : | |
2607 | for (i = 0; i < count; i ++) | |
2608 | { | |
2609 | ipp_res_t units; | |
2610 | int yres, xres = ippGetResolution(attr, i, &yres, &units); | |
2611 | ||
08102776 | 2612 | cupsFilePrintf(data->outfile, "%s%dx%d%s", i ? "," : " ", xres, yres, units == IPP_RES_PER_INCH ? "dpi" : "dpcm"); |
6646208b MS |
2613 | } |
2614 | break; | |
2615 | ||
2616 | case IPP_TAG_DATE : | |
2617 | for (i = 0; i < count; i ++) | |
08102776 | 2618 | cupsFilePrintf(data->outfile, "%s%s", i ? "," : " ", iso_date(ippGetDate(attr, i))); |
6646208b MS |
2619 | break; |
2620 | ||
2621 | case IPP_TAG_STRING : | |
2622 | for (i = 0; i < count; i ++) | |
2623 | { | |
2624 | int len; | |
2625 | const char *s = (const char *)ippGetOctetString(attr, i, &len); | |
2626 | ||
08102776 | 2627 | cupsFilePuts(data->outfile, i ? "," : " "); |
8e2e09c1 | 2628 | print_ippserver_string(data, s, (size_t)len); |
6646208b MS |
2629 | } |
2630 | break; | |
2631 | ||
2632 | case IPP_TAG_TEXT : | |
2633 | case IPP_TAG_TEXTLANG : | |
2634 | case IPP_TAG_NAME : | |
2635 | case IPP_TAG_NAMELANG : | |
2636 | case IPP_TAG_KEYWORD : | |
2637 | case IPP_TAG_URI : | |
2638 | case IPP_TAG_URISCHEME : | |
2639 | case IPP_TAG_CHARSET : | |
2640 | case IPP_TAG_LANGUAGE : | |
2641 | case IPP_TAG_MIMETYPE : | |
2642 | for (i = 0; i < count; i ++) | |
2643 | { | |
2644 | const char *s = ippGetString(attr, i, NULL); | |
2645 | ||
08102776 | 2646 | cupsFilePuts(data->outfile, i ? "," : " "); |
8e2e09c1 | 2647 | print_ippserver_string(data, s, strlen(s)); |
6646208b MS |
2648 | } |
2649 | break; | |
2650 | ||
2651 | case IPP_TAG_BEGIN_COLLECTION : | |
a492d345 | 2652 | for (i = 0; i < count; i ++) |
6646208b MS |
2653 | { |
2654 | ipp_t *col = ippGetCollection(attr, i); | |
2655 | ||
08102776 | 2656 | cupsFilePuts(data->outfile, i ? ",{\n" : " {\n"); |
6646208b | 2657 | for (colattr = ippFirstAttribute(col); colattr; colattr = ippNextAttribute(col)) |
8e2e09c1 | 2658 | print_ippserver_attr(data, colattr, indent + 4); |
08102776 | 2659 | cupsFilePrintf(data->outfile, "%*s}", indent, ""); |
6646208b MS |
2660 | } |
2661 | break; | |
2662 | ||
2663 | default : | |
c64170e5 | 2664 | /* Out-of-band value */ |
6646208b MS |
2665 | break; |
2666 | } | |
2667 | ||
08102776 | 2668 | cupsFilePuts(data->outfile, "\n"); |
6646208b MS |
2669 | } |
2670 | ||
2671 | ||
2672 | /* | |
2673 | * 'print_ippserver_string()' - Print a string suitable for use by ippserver. | |
2674 | */ | |
2675 | ||
2676 | static void | |
2677 | print_ippserver_string( | |
08102776 MS |
2678 | _cups_testdata_t *data, /* I - Test data */ |
2679 | const char *s, /* I - String to print */ | |
2680 | size_t len) /* I - Length of string */ | |
6646208b | 2681 | { |
08102776 | 2682 | cupsFilePutChar(data->outfile, '\"'); |
6646208b MS |
2683 | while (len > 0) |
2684 | { | |
2685 | if (*s == '\"') | |
08102776 MS |
2686 | cupsFilePutChar(data->outfile, '\\'); |
2687 | cupsFilePutChar(data->outfile, *s); | |
6646208b MS |
2688 | |
2689 | s ++; | |
2690 | len --; | |
2691 | } | |
08102776 | 2692 | cupsFilePutChar(data->outfile, '\"'); |
6646208b MS |
2693 | } |
2694 | ||
2695 | ||
aaf19ab0 MS |
2696 | /* |
2697 | * 'print_line()' - Print a line of formatted or CSV text. | |
2698 | */ | |
2699 | ||
2700 | static void | |
2701 | print_line( | |
08102776 | 2702 | _cups_testdata_t *data, /* I - Test data */ |
a492d345 | 2703 | ipp_t *ipp, /* I - Response message */ |
08102776 MS |
2704 | ipp_attribute_t *attr, /* I - First attribute for line */ |
2705 | int num_displayed, /* I - Number of attributes to display */ | |
2706 | char **displayed, /* I - Attributes to display */ | |
2707 | size_t *widths) /* I - Column widths */ | |
aaf19ab0 MS |
2708 | { |
2709 | int i; /* Looping var */ | |
2710 | size_t maxlength; /* Max length of all columns */ | |
2711 | char *buffer; /* String buffer */ | |
2712 | ipp_attribute_t *current; /* Current attribute */ | |
2713 | ||
2714 | ||
2715 | /* | |
2716 | * Get the maximum string length we have to show and allocate... | |
2717 | */ | |
2718 | ||
2719 | for (i = 1, maxlength = widths[0]; i < num_displayed; i ++) | |
2720 | if (widths[i] > maxlength) | |
2721 | maxlength = widths[i]; | |
2722 | ||
2723 | maxlength += 2; | |
2724 | ||
2725 | if ((buffer = malloc(maxlength)) == NULL) | |
2726 | return; | |
2727 | ||
2728 | /* | |
2729 | * Loop through the attributes to display... | |
2730 | */ | |
2731 | ||
2732 | if (attr) | |
2733 | { | |
2734 | for (i = 0; i < num_displayed; i ++) | |
2735 | { | |
2736 | if (i) | |
2a75f21b | 2737 | cupsFilePutChar(cupsFileStdout(), ' '); |
aaf19ab0 MS |
2738 | |
2739 | buffer[0] = '\0'; | |
2740 | ||
a492d345 | 2741 | for (current = attr; current; current = ippNextAttribute(ipp)) |
aaf19ab0 | 2742 | { |
a492d345 | 2743 | if (!ippGetName(current)) |
aaf19ab0 | 2744 | break; |
a492d345 | 2745 | else if (!strcmp(ippGetName(current), displayed[i])) |
aaf19ab0 | 2746 | { |
a2326b5b | 2747 | ippAttributeString(current, buffer, maxlength); |
aaf19ab0 MS |
2748 | break; |
2749 | } | |
2750 | } | |
2751 | ||
08102776 | 2752 | cupsFilePrintf(data->outfile, "%*s", (int)-widths[i], buffer); |
aaf19ab0 | 2753 | } |
2a75f21b | 2754 | cupsFilePutChar(cupsFileStdout(), '\n'); |
aaf19ab0 MS |
2755 | } |
2756 | else | |
2757 | { | |
2758 | for (i = 0; i < num_displayed; i ++) | |
2759 | { | |
2760 | if (i) | |
2a75f21b | 2761 | cupsFilePutChar(cupsFileStdout(), ' '); |
aaf19ab0 | 2762 | |
08102776 | 2763 | cupsFilePrintf(data->outfile, "%*s", (int)-widths[i], displayed[i]); |
aaf19ab0 | 2764 | } |
2a75f21b | 2765 | cupsFilePutChar(cupsFileStdout(), '\n'); |
aaf19ab0 MS |
2766 | |
2767 | for (i = 0; i < num_displayed; i ++) | |
2768 | { | |
2769 | if (i) | |
2a75f21b | 2770 | cupsFilePutChar(cupsFileStdout(), ' '); |
aaf19ab0 MS |
2771 | |
2772 | memset(buffer, '-', widths[i]); | |
2773 | buffer[widths[i]] = '\0'; | |
08102776 | 2774 | cupsFilePuts(data->outfile, buffer); |
aaf19ab0 | 2775 | } |
2a75f21b | 2776 | cupsFilePutChar(cupsFileStdout(), '\n'); |
aaf19ab0 MS |
2777 | } |
2778 | ||
2779 | free(buffer); | |
f8b3a85b MS |
2780 | } |
2781 | ||
2782 | ||
f8b3a85b MS |
2783 | /* |
2784 | * 'print_xml_header()' - Print a standard XML plist header. | |
2785 | */ | |
2786 | ||
2787 | static void | |
08102776 | 2788 | print_xml_header(_cups_testdata_t *data)/* I - Test data */ |
f8b3a85b | 2789 | { |
08102776 | 2790 | if (!data->xml_header) |
f8b3a85b | 2791 | { |
08102776 | 2792 | cupsFilePuts(data->outfile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); |
8e2e09c1 | 2793 | cupsFilePuts(data->outfile, "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"); |
08102776 MS |
2794 | cupsFilePuts(data->outfile, "<plist version=\"1.0\">\n"); |
2795 | cupsFilePuts(data->outfile, "<dict>\n"); | |
2796 | cupsFilePuts(data->outfile, "<key>ipptoolVersion</key>\n"); | |
2797 | cupsFilePuts(data->outfile, "<string>" CUPS_SVERSION "</string>\n"); | |
2798 | cupsFilePuts(data->outfile, "<key>Transfer</key>\n"); | |
8e2e09c1 | 2799 | cupsFilePrintf(data->outfile, "<string>%s</string>\n", data->transfer == _CUPS_TRANSFER_AUTO ? "auto" : data->transfer == _CUPS_TRANSFER_CHUNKED ? "chunked" : "length"); |
08102776 MS |
2800 | cupsFilePuts(data->outfile, "<key>Tests</key>\n"); |
2801 | cupsFilePuts(data->outfile, "<array>\n"); | |
f8b3a85b | 2802 | |
08102776 | 2803 | data->xml_header = 1; |
f8b3a85b MS |
2804 | } |
2805 | } | |
2806 | ||
2807 | ||
2808 | /* | |
2809 | * 'print_xml_string()' - Print an XML string with escaping. | |
2810 | */ | |
2811 | ||
2812 | static void | |
8e2e09c1 MS |
2813 | print_xml_string(cups_file_t *outfile, /* I - Test data */ |
2814 | const char *element, /* I - Element name or NULL */ | |
2815 | const char *s) /* I - String to print */ | |
f8b3a85b MS |
2816 | { |
2817 | if (element) | |
8e2e09c1 | 2818 | cupsFilePrintf(outfile, "<%s>", element); |
f8b3a85b MS |
2819 | |
2820 | while (*s) | |
2821 | { | |
2822 | if (*s == '&') | |
8e2e09c1 | 2823 | cupsFilePuts(outfile, "&"); |
f8b3a85b | 2824 | else if (*s == '<') |
8e2e09c1 | 2825 | cupsFilePuts(outfile, "<"); |
f8b3a85b | 2826 | else if (*s == '>') |
8e2e09c1 | 2827 | cupsFilePuts(outfile, ">"); |
a4845881 MS |
2828 | else if ((*s & 0xe0) == 0xc0) |
2829 | { | |
2830 | /* | |
2831 | * Validate UTF-8 two-byte sequence... | |
2832 | */ | |
2833 | ||
2834 | if ((s[1] & 0xc0) != 0x80) | |
2835 | { | |
8e2e09c1 | 2836 | cupsFilePutChar(outfile, '?'); |
a4845881 MS |
2837 | s ++; |
2838 | } | |
2839 | else | |
2840 | { | |
8e2e09c1 MS |
2841 | cupsFilePutChar(outfile, *s++); |
2842 | cupsFilePutChar(outfile, *s); | |
a4845881 MS |
2843 | } |
2844 | } | |
2845 | else if ((*s & 0xf0) == 0xe0) | |
2846 | { | |
2847 | /* | |
2848 | * Validate UTF-8 three-byte sequence... | |
2849 | */ | |
2850 | ||
2851 | if ((s[1] & 0xc0) != 0x80 || (s[2] & 0xc0) != 0x80) | |
2852 | { | |
8e2e09c1 | 2853 | cupsFilePutChar(outfile, '?'); |
a4845881 MS |
2854 | s += 2; |
2855 | } | |
2856 | else | |
2857 | { | |
8e2e09c1 MS |
2858 | cupsFilePutChar(outfile, *s++); |
2859 | cupsFilePutChar(outfile, *s++); | |
2860 | cupsFilePutChar(outfile, *s); | |
a4845881 MS |
2861 | } |
2862 | } | |
2863 | else if ((*s & 0xf8) == 0xf0) | |
2864 | { | |
2865 | /* | |
2866 | * Validate UTF-8 four-byte sequence... | |
2867 | */ | |
2868 | ||
2869 | if ((s[1] & 0xc0) != 0x80 || (s[2] & 0xc0) != 0x80 || | |
2870 | (s[3] & 0xc0) != 0x80) | |
2871 | { | |
8e2e09c1 | 2872 | cupsFilePutChar(outfile, '?'); |
a4845881 MS |
2873 | s += 3; |
2874 | } | |
2875 | else | |
2876 | { | |
8e2e09c1 MS |
2877 | cupsFilePutChar(outfile, *s++); |
2878 | cupsFilePutChar(outfile, *s++); | |
2879 | cupsFilePutChar(outfile, *s++); | |
2880 | cupsFilePutChar(outfile, *s); | |
a4845881 MS |
2881 | } |
2882 | } | |
2883 | else if ((*s & 0x80) || (*s < ' ' && !isspace(*s & 255))) | |
2884 | { | |
2885 | /* | |
2886 | * Invalid control character... | |
2887 | */ | |
2888 | ||
8e2e09c1 | 2889 | cupsFilePutChar(outfile, '?'); |
a4845881 | 2890 | } |
f8b3a85b | 2891 | else |
8e2e09c1 | 2892 | cupsFilePutChar(outfile, *s); |
f8b3a85b MS |
2893 | |
2894 | s ++; | |
2895 | } | |
2896 | ||
2897 | if (element) | |
8e2e09c1 | 2898 | cupsFilePrintf(outfile, "</%s>\n", element); |
f8b3a85b MS |
2899 | } |
2900 | ||
2901 | ||
2902 | /* | |
2903 | * 'print_xml_trailer()' - Print the XML trailer with success/fail value. | |
2904 | */ | |
2905 | ||
2906 | static void | |
08102776 MS |
2907 | print_xml_trailer( |
2908 | _cups_testdata_t *data, /* I - Test data */ | |
2909 | int success, /* I - 1 on success, 0 on failure */ | |
2910 | const char *message) /* I - Error message or NULL */ | |
f8b3a85b | 2911 | { |
08102776 | 2912 | if (data->xml_header) |
f8b3a85b | 2913 | { |
08102776 MS |
2914 | cupsFilePuts(data->outfile, "</array>\n"); |
2915 | cupsFilePuts(data->outfile, "<key>Successful</key>\n"); | |
2916 | cupsFilePuts(data->outfile, success ? "<true />\n" : "<false />\n"); | |
f8b3a85b MS |
2917 | if (message) |
2918 | { | |
08102776 MS |
2919 | cupsFilePuts(data->outfile, "<key>ErrorMessage</key>\n"); |
2920 | print_xml_string(data->outfile, "string", message); | |
f8b3a85b | 2921 | } |
08102776 MS |
2922 | cupsFilePuts(data->outfile, "</dict>\n"); |
2923 | cupsFilePuts(data->outfile, "</plist>\n"); | |
f8b3a85b | 2924 | |
08102776 | 2925 | data->xml_header = 0; |
f8b3a85b MS |
2926 | } |
2927 | } | |
2928 | ||
2929 | ||
24a06ed3 | 2930 | #ifndef _WIN32 |
85dda01c MS |
2931 | /* |
2932 | * 'sigterm_handler()' - Handle SIGINT and SIGTERM. | |
2933 | */ | |
2934 | ||
2935 | static void | |
2936 | sigterm_handler(int sig) /* I - Signal number (unused) */ | |
2937 | { | |
2938 | (void)sig; | |
2939 | ||
2940 | Cancel = 1; | |
9b66acc5 MS |
2941 | |
2942 | signal(SIGINT, SIG_DFL); | |
2943 | signal(SIGTERM, SIG_DFL); | |
85dda01c | 2944 | } |
24a06ed3 | 2945 | #endif /* !_WIN32 */ |
85dda01c MS |
2946 | |
2947 | ||
10d09e33 MS |
2948 | /* |
2949 | * 'timeout_cb()' - Handle HTTP timeouts. | |
2950 | */ | |
2951 | ||
2952 | static int /* O - 1 to continue, 0 to cancel */ | |
26340b35 | 2953 | timeout_cb(http_t *http, /* I - Connection to server */ |
10d09e33 MS |
2954 | void *user_data) /* I - User data (unused) */ |
2955 | { | |
26340b35 MS |
2956 | int buffered = 0; /* Bytes buffered but not yet sent */ |
2957 | ||
2958 | ||
e6b1a6a9 MS |
2959 | (void)user_data; |
2960 | ||
08102776 | 2961 | /* |
26340b35 MS |
2962 | * If the socket still have data waiting to be sent to the printer (as can |
2963 | * happen if the printer runs out of paper), continue to wait until the output | |
2964 | * buffer is empty... | |
2965 | */ | |
2966 | ||
8072030b | 2967 | #ifdef SO_NWRITE /* macOS and some versions of Linux */ |
26340b35 MS |
2968 | socklen_t len = sizeof(buffered); /* Size of return value */ |
2969 | ||
2970 | if (getsockopt(httpGetFd(http), SOL_SOCKET, SO_NWRITE, &buffered, &len)) | |
2971 | buffered = 0; | |
2972 | ||
2973 | #elif defined(SIOCOUTQ) /* Others except Windows */ | |
2974 | if (ioctl(httpGetFd(http), SIOCOUTQ, &buffered)) | |
2975 | buffered = 0; | |
2976 | ||
2977 | #else /* Windows (not possible) */ | |
10d09e33 | 2978 | (void)http; |
26340b35 | 2979 | #endif /* SO_NWRITE */ |
10d09e33 | 2980 | |
26340b35 | 2981 | return (buffered > 0); |
10d09e33 MS |
2982 | } |
2983 | ||
2984 | ||
08102776 MS |
2985 | /* |
2986 | * 'token_cb()' - Parse test file-specific tokens and run tests. | |
2987 | */ | |
2988 | ||
2989 | static int /* O - 1 to continue, 0 to stop */ | |
2990 | token_cb(_ipp_file_t *f, /* I - IPP file data */ | |
2991 | _ipp_vars_t *vars, /* I - IPP variables */ | |
2992 | _cups_testdata_t *data, /* I - Test data */ | |
2993 | const char *token) /* I - Current token */ | |
2994 | { | |
2995 | char name[1024], /* Name string */ | |
2996 | temp[1024], /* Temporary string */ | |
8e2e09c1 MS |
2997 | value[1024], /* Value string */ |
2998 | *ptr; /* Pointer into value */ | |
2999 | ||
08102776 | 3000 | |
8e2e09c1 MS |
3001 | if (!token) |
3002 | { | |
3003 | /* | |
3004 | * Initialize state as needed (nothing for now...) | |
3005 | */ | |
08102776 | 3006 | |
8e2e09c1 MS |
3007 | return (1); |
3008 | } | |
3009 | else if (f->attrs) | |
08102776 MS |
3010 | { |
3011 | /* | |
3012 | * Parse until we see a close brace... | |
3013 | */ | |
3014 | ||
3015 | if (_cups_strcasecmp(token, "COUNT") && | |
3016 | _cups_strcasecmp(token, "DEFINE-MATCH") && | |
3017 | _cups_strcasecmp(token, "DEFINE-NO-MATCH") && | |
3018 | _cups_strcasecmp(token, "DEFINE-VALUE") && | |
3019 | _cups_strcasecmp(token, "IF-DEFINED") && | |
3020 | _cups_strcasecmp(token, "IF-NOT-DEFINED") && | |
3021 | _cups_strcasecmp(token, "IN-GROUP") && | |
3022 | _cups_strcasecmp(token, "OF-TYPE") && | |
3023 | _cups_strcasecmp(token, "REPEAT-LIMIT") && | |
3024 | _cups_strcasecmp(token, "REPEAT-MATCH") && | |
3025 | _cups_strcasecmp(token, "REPEAT-NO-MATCH") && | |
3026 | _cups_strcasecmp(token, "SAME-COUNT-AS") && | |
3027 | _cups_strcasecmp(token, "WITH-ALL-VALUES") && | |
3028 | _cups_strcasecmp(token, "WITH-ALL-HOSTNAMES") && | |
3029 | _cups_strcasecmp(token, "WITH-ALL-RESOURCES") && | |
3030 | _cups_strcasecmp(token, "WITH-ALL-SCHEMES") && | |
3031 | _cups_strcasecmp(token, "WITH-HOSTNAME") && | |
3032 | _cups_strcasecmp(token, "WITH-RESOURCE") && | |
3033 | _cups_strcasecmp(token, "WITH-SCHEME") && | |
3034 | _cups_strcasecmp(token, "WITH-VALUE") && | |
3035 | _cups_strcasecmp(token, "WITH-VALUE-FROM")) | |
3036 | data->last_expect = NULL; | |
3037 | ||
3038 | if (_cups_strcasecmp(token, "DEFINE-MATCH") && | |
3039 | _cups_strcasecmp(token, "DEFINE-NO-MATCH") && | |
3040 | _cups_strcasecmp(token, "IF-DEFINED") && | |
3041 | _cups_strcasecmp(token, "IF-NOT-DEFINED") && | |
3042 | _cups_strcasecmp(token, "REPEAT-LIMIT") && | |
3043 | _cups_strcasecmp(token, "REPEAT-MATCH") && | |
3044 | _cups_strcasecmp(token, "REPEAT-NO-MATCH")) | |
3045 | data->last_status = NULL; | |
3046 | ||
3047 | if (!strcmp(token, "}")) | |
3048 | { | |
8e2e09c1 | 3049 | return (do_test(f, vars, data)); |
08102776 MS |
3050 | } |
3051 | else if (!strcmp(token, "COMPRESSION")) | |
3052 | { | |
3053 | /* | |
3054 | * COMPRESSION none | |
3055 | * COMPRESSION deflate | |
3056 | * COMPRESSION gzip | |
3057 | */ | |
3058 | ||
3059 | if (_ippFileReadToken(f, temp, sizeof(temp))) | |
3060 | { | |
3061 | _ippVarsExpand(vars, data->compression, temp, sizeof(data->compression)); | |
3062 | #ifdef HAVE_LIBZ | |
3063 | if (strcmp(data->compression, "none") && strcmp(data->compression, "deflate") && | |
3064 | strcmp(data->compression, "gzip")) | |
3065 | #else | |
3066 | if (strcmp(data->compression, "none")) | |
3067 | #endif /* HAVE_LIBZ */ | |
3068 | { | |
3069 | print_fatal_error(data, "Unsupported COMPRESSION value \"%s\" on line %d of \"%s\".", data->compression, f->linenum, f->filename); | |
3070 | return (0); | |
3071 | } | |
3072 | ||
8e2e09c1 | 3073 | if (!strcmp(data->compression, "none")) |
08102776 MS |
3074 | data->compression[0] = '\0'; |
3075 | } | |
3076 | else | |
3077 | { | |
3078 | print_fatal_error(data, "Missing COMPRESSION value on line %d of \"%s\".", f->linenum, f->filename); | |
3079 | return (0); | |
3080 | } | |
3081 | } | |
3082 | else if (!strcmp(token, "DEFINE")) | |
3083 | { | |
3084 | /* | |
3085 | * DEFINE name value | |
3086 | */ | |
3087 | ||
3088 | if (_ippFileReadToken(f, name, sizeof(name)) && _ippFileReadToken(f, temp, sizeof(temp))) | |
3089 | { | |
3090 | _ippVarsExpand(vars, value, temp, sizeof(value)); | |
3091 | _ippVarsSet(vars, name, value); | |
3092 | } | |
3093 | else | |
3094 | { | |
3095 | print_fatal_error(data, "Missing DEFINE name and/or value on line %d of \"%s\".", f->linenum, f->filename); | |
3096 | return (0); | |
3097 | } | |
3098 | } | |
3099 | else if (!strcmp(token, "IGNORE-ERRORS")) | |
3100 | { | |
3101 | /* | |
3102 | * IGNORE-ERRORS yes | |
3103 | * IGNORE-ERRORS no | |
3104 | */ | |
3105 | ||
3106 | if (_ippFileReadToken(f, temp, sizeof(temp)) && (!_cups_strcasecmp(temp, "yes") || !_cups_strcasecmp(temp, "no"))) | |
3107 | { | |
3108 | data->ignore_errors = !_cups_strcasecmp(temp, "yes"); | |
3109 | } | |
3110 | else | |
3111 | { | |
3112 | print_fatal_error(data, "Missing IGNORE-ERRORS value on line %d of \"%s\".", f->linenum, f->filename); | |
3113 | return (0); | |
3114 | } | |
3115 | } | |
3116 | else if (!_cups_strcasecmp(token, "NAME")) | |
3117 | { | |
3118 | /* | |
3119 | * Name of test... | |
3120 | */ | |
3121 | ||
3122 | _ippFileReadToken(f, temp, sizeof(temp)); | |
3123 | _ippVarsExpand(vars, data->name, temp, sizeof(data->name)); | |
3124 | } | |
3125 | else if (!_cups_strcasecmp(token, "PAUSE")) | |
3126 | { | |
3127 | /* | |
3128 | * Pause with a message... | |
3129 | */ | |
3130 | ||
3131 | if (_ippFileReadToken(f, temp, sizeof(temp))) | |
3132 | { | |
3133 | pause_message(temp); | |
3134 | } | |
3135 | else | |
3136 | { | |
3137 | print_fatal_error(data, "Missing PAUSE message on line %d of \"%s\".", f->linenum, f->filename); | |
3138 | return (0); | |
3139 | } | |
3140 | } | |
3141 | else if (!strcmp(token, "REQUEST-ID")) | |
3142 | { | |
3143 | /* | |
3144 | * REQUEST-ID # | |
3145 | * REQUEST-ID random | |
3146 | */ | |
3147 | ||
3148 | if (_ippFileReadToken(f, temp, sizeof(temp))) | |
3149 | { | |
3150 | if (isdigit(temp[0] & 255)) | |
3151 | { | |
3152 | data->request_id = atoi(temp); | |
3153 | } | |
3154 | else if (!_cups_strcasecmp(temp, "random")) | |
3155 | { | |
3156 | data->request_id = (CUPS_RAND() % 1000) * 137 + 1; | |
3157 | } | |
3158 | else | |
3159 | { | |
3160 | print_fatal_error(data, "Bad REQUEST-ID value \"%s\" on line %d of \"%s\".", temp, f->linenum, f->filename); | |
3161 | return (0); | |
3162 | } | |
3163 | } | |
3164 | else | |
3165 | { | |
3166 | print_fatal_error(data, "Missing REQUEST-ID value on line %d of \"%s\".", f->linenum, f->filename); | |
3167 | return (0); | |
3168 | } | |
3169 | } | |
3170 | else if (!strcmp(token, "SKIP-IF-DEFINED")) | |
3171 | { | |
3172 | /* | |
3173 | * SKIP-IF-DEFINED variable | |
3174 | */ | |
3175 | ||
3176 | if (_ippFileReadToken(f, name, sizeof(name))) | |
3177 | { | |
3178 | if (_ippVarsGet(vars, name)) | |
3179 | data->skip_test = 1; | |
3180 | } | |
3181 | else | |
3182 | { | |
3183 | print_fatal_error(data, "Missing SKIP-IF-DEFINED value on line %d of \"%s\".", f->linenum, f->filename); | |
3184 | return (0); | |
3185 | } | |
3186 | } | |
3187 | else if (!strcmp(token, "SKIP-IF-MISSING")) | |
3188 | { | |
3189 | /* | |
3190 | * SKIP-IF-MISSING filename | |
3191 | */ | |
3192 | ||
3193 | if (_ippFileReadToken(f, temp, sizeof(temp))) | |
3194 | { | |
3195 | char filename[1024]; /* Filename */ | |
3196 | ||
3197 | _ippVarsExpand(vars, value, temp, sizeof(value)); | |
3198 | get_filename(f->filename, filename, temp, sizeof(filename)); | |
3199 | ||
3200 | if (access(filename, R_OK)) | |
3201 | data->skip_test = 1; | |
3202 | } | |
3203 | else | |
3204 | { | |
3205 | print_fatal_error(data, "Missing SKIP-IF-MISSING filename on line %d of \"%s\".", f->linenum, f->filename); | |
3206 | return (0); | |
3207 | } | |
3208 | } | |
3209 | else if (!strcmp(token, "SKIP-IF-NOT-DEFINED")) | |
3210 | { | |
3211 | /* | |
3212 | * SKIP-IF-NOT-DEFINED variable | |
3213 | */ | |
3214 | ||
3215 | if (_ippFileReadToken(f, name, sizeof(name))) | |
3216 | { | |
3217 | if (!_ippVarsGet(vars, name)) | |
3218 | data->skip_test = 1; | |
3219 | } | |
3220 | else | |
3221 | { | |
3222 | print_fatal_error(data, "Missing SKIP-IF-NOT-DEFINED value on line %d of \"%s\".", f->linenum, f->filename); | |
3223 | return (0); | |
3224 | } | |
3225 | } | |
3226 | else if (!strcmp(token, "SKIP-PREVIOUS-ERROR")) | |
3227 | { | |
3228 | /* | |
3229 | * SKIP-PREVIOUS-ERROR yes | |
3230 | * SKIP-PREVIOUS-ERROR no | |
3231 | */ | |
3232 | ||
3233 | if (_ippFileReadToken(f, temp, sizeof(temp)) && (!_cups_strcasecmp(temp, "yes") || !_cups_strcasecmp(temp, "no"))) | |
3234 | { | |
3235 | data->skip_previous = !_cups_strcasecmp(temp, "yes"); | |
3236 | } | |
3237 | else | |
3238 | { | |
3239 | print_fatal_error(data, "Missing SKIP-PREVIOUS-ERROR value on line %d of \"%s\".", f->linenum, f->filename); | |
3240 | return (0); | |
3241 | } | |
3242 | } | |
3243 | else if (!strcmp(token, "TEST-ID")) | |
3244 | { | |
3245 | /* | |
3246 | * TEST-ID "string" | |
3247 | */ | |
3248 | ||
3249 | if (_ippFileReadToken(f, temp, sizeof(temp))) | |
3250 | { | |
3251 | _ippVarsExpand(vars, data->test_id, temp, sizeof(data->test_id)); | |
3252 | } | |
3253 | else | |
3254 | { | |
3255 | print_fatal_error(data, "Missing TEST-ID value on line %d of \"%s\".", f->linenum, f->filename); | |
3256 | return (0); | |
3257 | } | |
3258 | } | |
3259 | else if (!strcmp(token, "TRANSFER")) | |
3260 | { | |
3261 | /* | |
3262 | * TRANSFER auto | |
3263 | * TRANSFER chunked | |
3264 | * TRANSFER length | |
3265 | */ | |
3266 | ||
3267 | if (_ippFileReadToken(f, temp, sizeof(temp))) | |
3268 | { | |
3269 | if (!strcmp(temp, "auto")) | |
3270 | { | |
3271 | data->transfer = _CUPS_TRANSFER_AUTO; | |
3272 | } | |
3273 | else if (!strcmp(temp, "chunked")) | |
3274 | { | |
3275 | data->transfer = _CUPS_TRANSFER_CHUNKED; | |
3276 | } | |
3277 | else if (!strcmp(temp, "length")) | |
3278 | { | |
3279 | data->transfer = _CUPS_TRANSFER_LENGTH; | |
3280 | } | |
3281 | else | |
3282 | { | |
3283 | print_fatal_error(data, "Bad TRANSFER value \"%s\" on line %d of \"%s\".", temp, f->linenum, f->filename); | |
3284 | return (0); | |
3285 | } | |
3286 | } | |
3287 | else | |
3288 | { | |
3289 | print_fatal_error(data, "Missing TRANSFER value on line %d of \"%s\".", f->linenum, f->filename); | |
3290 | return (0); | |
3291 | } | |
3292 | } | |
3293 | else if (!_cups_strcasecmp(token, "VERSION")) | |
3294 | { | |
3295 | if (_ippFileReadToken(f, temp, sizeof(temp))) | |
3296 | { | |
3297 | if (!strcmp(temp, "0.0")) | |
3298 | { | |
3299 | data->version = 0; | |
3300 | } | |
3301 | else if (!strcmp(temp, "1.0")) | |
3302 | { | |
3303 | data->version = 10; | |
3304 | } | |
3305 | else if (!strcmp(temp, "1.1")) | |
3306 | { | |
3307 | data->version = 11; | |
3308 | } | |
3309 | else if (!strcmp(temp, "2.0")) | |
3310 | { | |
3311 | data->version = 20; | |
3312 | } | |
3313 | else if (!strcmp(temp, "2.1")) | |
3314 | { | |
3315 | data->version = 21; | |
3316 | } | |
3317 | else if (!strcmp(temp, "2.2")) | |
3318 | { | |
3319 | data->version = 22; | |
3320 | } | |
3321 | else | |
3322 | { | |
3323 | print_fatal_error(data, "Bad VERSION \"%s\" on line %d of \"%s\".", temp, f->linenum, f->filename); | |
3324 | return (0); | |
3325 | } | |
3326 | } | |
3327 | else | |
3328 | { | |
3329 | print_fatal_error(data, "Missing VERSION number on line %d of \"%s\".", f->linenum, f->filename); | |
8e2e09c1 | 3330 | return (0); |
08102776 MS |
3331 | } |
3332 | } | |
3333 | else if (!_cups_strcasecmp(token, "RESOURCE")) | |
3334 | { | |
3335 | /* | |
3336 | * Resource name... | |
3337 | */ | |
3338 | ||
8e2e09c1 | 3339 | if (!_ippFileReadToken(f, data->resource, sizeof(data->resource))) |
08102776 MS |
3340 | { |
3341 | print_fatal_error(data, "Missing RESOURCE path on line %d of \"%s\".", f->linenum, f->filename); | |
8e2e09c1 | 3342 | return (0); |
08102776 MS |
3343 | } |
3344 | } | |
3345 | else if (!_cups_strcasecmp(token, "OPERATION")) | |
3346 | { | |
3347 | /* | |
3348 | * Operation... | |
3349 | */ | |
3350 | ||
8e2e09c1 MS |
3351 | ipp_op_t op; /* Operation code */ |
3352 | ||
08102776 MS |
3353 | if (!_ippFileReadToken(f, temp, sizeof(temp))) |
3354 | { | |
3355 | print_fatal_error(data, "Missing OPERATION code on line %d of \"%s\".", f->linenum, f->filename); | |
8e2e09c1 | 3356 | return (0); |
08102776 MS |
3357 | } |
3358 | ||
8e2e09c1 | 3359 | _ippVarsExpand(vars, value, temp, sizeof(value)); |
08102776 | 3360 | |
8e2e09c1 | 3361 | if ((op = ippOpValue(value)) == (ipp_op_t)-1 && (op = (ipp_op_t)strtol(value, NULL, 0)) == 0) |
08102776 | 3362 | { |
8e2e09c1 MS |
3363 | print_fatal_error(data, "Bad OPERATION code \"%s\" on line %d of \"%s\".", temp, f->linenum, f->filename); |
3364 | return (0); | |
08102776 | 3365 | } |
8e2e09c1 MS |
3366 | |
3367 | ippSetOperation(f->attrs, op); | |
08102776 MS |
3368 | } |
3369 | else if (!_cups_strcasecmp(token, "GROUP")) | |
3370 | { | |
3371 | /* | |
3372 | * Attribute group... | |
3373 | */ | |
3374 | ||
8e2e09c1 MS |
3375 | ipp_tag_t group_tag; /* Group tag */ |
3376 | ||
3377 | if (!_ippFileReadToken(f, temp, sizeof(temp))) | |
08102776 MS |
3378 | { |
3379 | print_fatal_error(data, "Missing GROUP tag on line %d of \"%s\".", f->linenum, f->filename); | |
8e2e09c1 | 3380 | return (0); |
08102776 MS |
3381 | } |
3382 | ||
8e2e09c1 | 3383 | if ((group_tag = ippTagValue(temp)) == IPP_TAG_ZERO || group_tag >= IPP_TAG_UNSUPPORTED_VALUE) |
08102776 | 3384 | { |
8e2e09c1 MS |
3385 | print_fatal_error(data, "Bad GROUP tag \"%s\" on line %d of \"%s\".", temp, f->linenum, f->filename); |
3386 | return (0); | |
08102776 MS |
3387 | } |
3388 | ||
8e2e09c1 MS |
3389 | if (group_tag == f->group_tag) |
3390 | ippAddSeparator(f->attrs); | |
08102776 | 3391 | |
8e2e09c1 | 3392 | f->group_tag = group_tag; |
08102776 MS |
3393 | } |
3394 | else if (!_cups_strcasecmp(token, "DELAY")) | |
3395 | { | |
3396 | /* | |
3397 | * Delay before operation... | |
3398 | */ | |
3399 | ||
3400 | double dval; /* Delay value */ | |
3401 | ||
3402 | if (!_ippFileReadToken(f, temp, sizeof(temp))) | |
3403 | { | |
3404 | print_fatal_error(data, "Missing DELAY value on line %d of \"%s\".", f->linenum, f->filename); | |
8e2e09c1 | 3405 | return (0); |
08102776 MS |
3406 | } |
3407 | ||
8e2e09c1 | 3408 | _ippVarsExpand(vars, value, temp, sizeof(value)); |
08102776 | 3409 | |
8e2e09c1 | 3410 | if ((dval = _cupsStrScand(value, &ptr, localeconv())) < 0.0 || (*ptr && *ptr != ',')) |
08102776 | 3411 | { |
8e2e09c1 MS |
3412 | print_fatal_error(data, "Bad DELAY value \"%s\" on line %d of \"%s\".", value, f->linenum, f->filename); |
3413 | return (0); | |
08102776 MS |
3414 | } |
3415 | ||
8e2e09c1 | 3416 | data->delay = (useconds_t)(1000000.0 * dval); |
08102776 | 3417 | |
8e2e09c1 | 3418 | if (*ptr == ',') |
08102776 | 3419 | { |
8e2e09c1 | 3420 | if ((dval = _cupsStrScand(ptr + 1, &ptr, localeconv())) <= 0.0 || *ptr) |
08102776 | 3421 | { |
8e2e09c1 MS |
3422 | print_fatal_error(data, "Bad DELAY value \"%s\" on line %d of \"%s\".", value, f->linenum, f->filename); |
3423 | return (0); | |
08102776 MS |
3424 | } |
3425 | ||
8e2e09c1 | 3426 | data->repeat_interval = (useconds_t)(1000000.0 * dval); |
08102776 MS |
3427 | } |
3428 | else | |
8e2e09c1 | 3429 | data->repeat_interval = data->delay; |
08102776 MS |
3430 | } |
3431 | else if (!_cups_strcasecmp(token, "FILE")) | |
3432 | { | |
3433 | /* | |
3434 | * File... | |
3435 | */ | |
3436 | ||
3437 | if (!_ippFileReadToken(f, temp, sizeof(temp))) | |
3438 | { | |
3439 | print_fatal_error(data, "Missing FILE filename on line %d of \"%s\".", f->linenum, f->filename); | |
8e2e09c1 | 3440 | return (0); |
08102776 MS |
3441 | } |
3442 | ||
8e2e09c1 MS |
3443 | _ippVarsExpand(vars, value, temp, sizeof(value)); |
3444 | get_filename(f->filename, data->file, value, sizeof(data->file)); | |
08102776 | 3445 | |
8e2e09c1 | 3446 | if (access(data->file, R_OK)) |
08102776 | 3447 | { |
8e2e09c1 MS |
3448 | print_fatal_error(data, "Filename \"%s\" (mapped to \"%s\") on line %d of \"%s\" cannot be read.", value, data->file, f->linenum, f->filename); |
3449 | return (0); | |
08102776 MS |
3450 | } |
3451 | } | |
3452 | else if (!_cups_strcasecmp(token, "STATUS")) | |
3453 | { | |
3454 | /* | |
3455 | * Status... | |
3456 | */ | |
3457 | ||
8e2e09c1 | 3458 | if (data->num_statuses >= (int)(sizeof(data->statuses) / sizeof(data->statuses[0]))) |
08102776 MS |
3459 | { |
3460 | print_fatal_error(data, "Too many STATUS's on line %d of \"%s\".", f->linenum, f->filename); | |
8e2e09c1 | 3461 | return (0); |
08102776 MS |
3462 | } |
3463 | ||
8e2e09c1 | 3464 | if (!_ippFileReadToken(f, temp, sizeof(temp))) |
08102776 MS |
3465 | { |
3466 | print_fatal_error(data, "Missing STATUS code on line %d of \"%s\".", f->linenum, f->filename); | |
8e2e09c1 | 3467 | return (0); |
08102776 MS |
3468 | } |
3469 | ||
8e2e09c1 | 3470 | if ((data->statuses[data->num_statuses].status = ippErrorValue(temp)) == (ipp_status_t)-1 && (data->statuses[data->num_statuses].status = (ipp_status_t)strtol(temp, NULL, 0)) == 0) |
08102776 | 3471 | { |
8e2e09c1 MS |
3472 | print_fatal_error(data, "Bad STATUS code \"%s\" on line %d of \"%s\".", temp, f->linenum, f->filename); |
3473 | return (0); | |
08102776 MS |
3474 | } |
3475 | ||
8e2e09c1 MS |
3476 | data->last_status = data->statuses + data->num_statuses; |
3477 | data->num_statuses ++; | |
08102776 | 3478 | |
8e2e09c1 MS |
3479 | data->last_status->define_match = NULL; |
3480 | data->last_status->define_no_match = NULL; | |
3481 | data->last_status->if_defined = NULL; | |
3482 | data->last_status->if_not_defined = NULL; | |
3483 | data->last_status->repeat_limit = 1000; | |
3484 | data->last_status->repeat_match = 0; | |
3485 | data->last_status->repeat_no_match = 0; | |
08102776 MS |
3486 | } |
3487 | else if (!_cups_strcasecmp(token, "EXPECT") || !_cups_strcasecmp(token, "EXPECT-ALL")) | |
3488 | { | |
3489 | /* | |
3490 | * Expected attributes... | |
3491 | */ | |
3492 | ||
3493 | int expect_all = !_cups_strcasecmp(token, "EXPECT-ALL"); | |
3494 | ||
8e2e09c1 | 3495 | if (data->num_expects >= (int)(sizeof(data->expects) / sizeof(data->expects[0]))) |
08102776 MS |
3496 | { |
3497 | print_fatal_error(data, "Too many EXPECT's on line %d of \"%s\".", f->linenum, f->filename); | |
8e2e09c1 | 3498 | return (0); |
08102776 MS |
3499 | } |
3500 | ||
8e2e09c1 | 3501 | if (!_ippFileReadToken(f, name, sizeof(name))) |
08102776 MS |
3502 | { |
3503 | print_fatal_error(data, "Missing EXPECT name on line %d of \"%s\".", f->linenum, f->filename); | |
8e2e09c1 | 3504 | return (0); |
08102776 MS |
3505 | } |
3506 | ||
8e2e09c1 MS |
3507 | data->last_expect = data->expects + data->num_expects; |
3508 | data->num_expects ++; | |
08102776 | 3509 | |
8e2e09c1 MS |
3510 | memset(data->last_expect, 0, sizeof(_cups_expect_t)); |
3511 | data->last_expect->repeat_limit = 1000; | |
3512 | data->last_expect->expect_all = expect_all; | |
08102776 | 3513 | |
8e2e09c1 | 3514 | if (name[0] == '!') |
08102776 | 3515 | { |
8e2e09c1 MS |
3516 | data->last_expect->not_expect = 1; |
3517 | data->last_expect->name = strdup(name + 1); | |
08102776 | 3518 | } |
8e2e09c1 | 3519 | else if (name[0] == '?') |
08102776 | 3520 | { |
8e2e09c1 MS |
3521 | data->last_expect->optional = 1; |
3522 | data->last_expect->name = strdup(name + 1); | |
08102776 MS |
3523 | } |
3524 | else | |
8e2e09c1 | 3525 | data->last_expect->name = strdup(name); |
08102776 MS |
3526 | } |
3527 | else if (!_cups_strcasecmp(token, "COUNT")) | |
3528 | { | |
8e2e09c1 MS |
3529 | int count; /* Count value */ |
3530 | ||
3531 | if (!_ippFileReadToken(f, temp, sizeof(temp))) | |
08102776 MS |
3532 | { |
3533 | print_fatal_error(data, "Missing COUNT number on line %d of \"%s\".", f->linenum, f->filename); | |
8e2e09c1 | 3534 | return (0); |
08102776 MS |
3535 | } |
3536 | ||
8e2e09c1 | 3537 | if ((count = atoi(temp)) <= 0) |
08102776 | 3538 | { |
8e2e09c1 MS |
3539 | print_fatal_error(data, "Bad COUNT \"%s\" on line %d of \"%s\".", temp, f->linenum, f->filename); |
3540 | return (0); | |
08102776 MS |
3541 | } |
3542 | ||
8e2e09c1 MS |
3543 | if (data->last_expect) |
3544 | { | |
3545 | data->last_expect->count = count; | |
3546 | } | |
08102776 MS |
3547 | else |
3548 | { | |
8e2e09c1 MS |
3549 | print_fatal_error(data, "COUNT without a preceding EXPECT on line %d of \"%s\".", f->linenum, f->filename); |
3550 | return (0); | |
08102776 MS |
3551 | } |
3552 | } | |
3553 | else if (!_cups_strcasecmp(token, "DEFINE-MATCH")) | |
3554 | { | |
8e2e09c1 | 3555 | if (!_ippFileReadToken(f, temp, sizeof(temp))) |
08102776 | 3556 | { |
8e2e09c1 MS |
3557 | print_fatal_error(data, "Missing DEFINE-MATCH variable on line %d of \"%s\".", f->linenum, f->filename); |
3558 | return (0); | |
08102776 MS |
3559 | } |
3560 | ||
8e2e09c1 MS |
3561 | if (data->last_expect) |
3562 | { | |
3563 | data->last_expect->define_match = strdup(temp); | |
3564 | } | |
3565 | else if (data->last_status) | |
3566 | { | |
3567 | data->last_status->define_match = strdup(temp); | |
3568 | } | |
08102776 MS |
3569 | else |
3570 | { | |
8e2e09c1 MS |
3571 | print_fatal_error(data, "DEFINE-MATCH without a preceding EXPECT or STATUS on line %d of \"%s\".", f->linenum, f->filename); |
3572 | return (0); | |
08102776 MS |
3573 | } |
3574 | } | |
3575 | else if (!_cups_strcasecmp(token, "DEFINE-NO-MATCH")) | |
3576 | { | |
8e2e09c1 | 3577 | if (!_ippFileReadToken(f, temp, sizeof(temp))) |
08102776 | 3578 | { |
8e2e09c1 MS |
3579 | print_fatal_error(data, "Missing DEFINE-NO-MATCH variable on line %d of \"%s\".", f->linenum, f->filename); |
3580 | return (0); | |
08102776 MS |
3581 | } |
3582 | ||
8e2e09c1 MS |
3583 | if (data->last_expect) |
3584 | { | |
3585 | data->last_expect->define_no_match = strdup(temp); | |
3586 | } | |
3587 | else if (data->last_status) | |
3588 | { | |
3589 | data->last_status->define_no_match = strdup(temp); | |
3590 | } | |
08102776 MS |
3591 | else |
3592 | { | |
8e2e09c1 MS |
3593 | print_fatal_error(data, "DEFINE-NO-MATCH without a preceding EXPECT or STATUS on line %d of \"%s\".", f->linenum, f->filename); |
3594 | return (0); | |
08102776 MS |
3595 | } |
3596 | } | |
3597 | else if (!_cups_strcasecmp(token, "DEFINE-VALUE")) | |
3598 | { | |
8e2e09c1 | 3599 | if (!_ippFileReadToken(f, temp, sizeof(temp))) |
08102776 | 3600 | { |
8e2e09c1 MS |
3601 | print_fatal_error(data, "Missing DEFINE-VALUE variable on line %d of \"%s\".", f->linenum, f->filename); |
3602 | return (0); | |
08102776 MS |
3603 | } |
3604 | ||
8e2e09c1 MS |
3605 | if (data->last_expect) |
3606 | { | |
3607 | data->last_expect->define_value = strdup(temp); | |
3608 | } | |
08102776 MS |
3609 | else |
3610 | { | |
8e2e09c1 MS |
3611 | print_fatal_error(data, "DEFINE-VALUE without a preceding EXPECT on line %d of \"%s\".", f->linenum, f->filename); |
3612 | return (0); | |
08102776 MS |
3613 | } |
3614 | } | |
3615 | else if (!_cups_strcasecmp(token, "OF-TYPE")) | |
3616 | { | |
8e2e09c1 | 3617 | if (!_ippFileReadToken(f, temp, sizeof(temp))) |
08102776 | 3618 | { |
8e2e09c1 MS |
3619 | print_fatal_error(data, "Missing OF-TYPE value tag(s) on line %d of \"%s\".", f->linenum, f->filename); |
3620 | return (0); | |
08102776 MS |
3621 | } |
3622 | ||
8e2e09c1 MS |
3623 | if (data->last_expect) |
3624 | { | |
3625 | data->last_expect->of_type = strdup(temp); | |
3626 | } | |
08102776 MS |
3627 | else |
3628 | { | |
8e2e09c1 MS |
3629 | print_fatal_error(data, "OF-TYPE without a preceding EXPECT on line %d of \"%s\".", f->linenum, f->filename); |
3630 | return (0); | |
08102776 MS |
3631 | } |
3632 | } | |
3633 | else if (!_cups_strcasecmp(token, "IN-GROUP")) | |
3634 | { | |
8e2e09c1 | 3635 | ipp_tag_t in_group; /* IN-GROUP value */ |
08102776 | 3636 | |
8e2e09c1 | 3637 | if (!_ippFileReadToken(f, temp, sizeof(temp))) |
08102776 MS |
3638 | { |
3639 | print_fatal_error(data, "Missing IN-GROUP group tag on line %d of \"%s\".", f->linenum, f->filename); | |
8e2e09c1 | 3640 | return (0); |
08102776 MS |
3641 | } |
3642 | ||
8e2e09c1 MS |
3643 | if ((in_group = ippTagValue(temp)) == IPP_TAG_ZERO || in_group >= IPP_TAG_UNSUPPORTED_VALUE) |
3644 | { | |
3645 | print_fatal_error(data, "Bad IN-GROUP group tag \"%s\" on line %d of \"%s\".", temp, f->linenum, f->filename); | |
3646 | return (0); | |
3647 | } | |
3648 | else if (data->last_expect) | |
08102776 | 3649 | { |
8e2e09c1 | 3650 | data->last_expect->in_group = in_group; |
08102776 | 3651 | } |
08102776 MS |
3652 | else |
3653 | { | |
3654 | print_fatal_error(data, "IN-GROUP without a preceding EXPECT on line %d of \"%s\".", f->linenum, f->filename); | |
8e2e09c1 | 3655 | return (0); |
08102776 MS |
3656 | } |
3657 | } | |
3658 | else if (!_cups_strcasecmp(token, "REPEAT-LIMIT")) | |
3659 | { | |
8e2e09c1 | 3660 | if (!_ippFileReadToken(f, temp, sizeof(temp))) |
08102776 MS |
3661 | { |
3662 | print_fatal_error(data, "Missing REPEAT-LIMIT value on line %d of \"%s\".", f->linenum, f->filename); | |
8e2e09c1 | 3663 | return (0); |
08102776 | 3664 | } |
8e2e09c1 | 3665 | else if (atoi(temp) <= 0) |
08102776 MS |
3666 | { |
3667 | print_fatal_error(data, "Bad REPEAT-LIMIT value on line %d of \"%s\".", f->linenum, f->filename); | |
8e2e09c1 | 3668 | return (0); |
08102776 MS |
3669 | } |
3670 | ||
8e2e09c1 MS |
3671 | if (data->last_status) |
3672 | { | |
3673 | data->last_status->repeat_limit = atoi(temp); | |
3674 | } | |
3675 | else if (data->last_expect) | |
3676 | { | |
3677 | data->last_expect->repeat_limit = atoi(temp); | |
3678 | } | |
08102776 MS |
3679 | else |
3680 | { | |
3681 | print_fatal_error(data, "REPEAT-LIMIT without a preceding EXPECT or STATUS on line %d of \"%s\".", f->linenum, f->filename); | |
8e2e09c1 | 3682 | return (0); |
08102776 MS |
3683 | } |
3684 | } | |
3685 | else if (!_cups_strcasecmp(token, "REPEAT-MATCH")) | |
3686 | { | |
8e2e09c1 MS |
3687 | if (data->last_status) |
3688 | { | |
3689 | data->last_status->repeat_match = 1; | |
3690 | } | |
3691 | else if (data->last_expect) | |
3692 | { | |
3693 | data->last_expect->repeat_match = 1; | |
3694 | } | |
08102776 MS |
3695 | else |
3696 | { | |
3697 | print_fatal_error(data, "REPEAT-MATCH without a preceding EXPECT or STATUS on line %d of \"%s\".", f->linenum, f->filename); | |
8e2e09c1 | 3698 | return (0); |
08102776 MS |
3699 | } |
3700 | } | |
3701 | else if (!_cups_strcasecmp(token, "REPEAT-NO-MATCH")) | |
3702 | { | |
8e2e09c1 MS |
3703 | if (data->last_status) |
3704 | { | |
3705 | data->last_status->repeat_no_match = 1; | |
3706 | } | |
3707 | else if (data->last_expect) | |
3708 | { | |
3709 | data->last_expect->repeat_no_match = 1; | |
3710 | } | |
08102776 MS |
3711 | else |
3712 | { | |
8e2e09c1 MS |
3713 | print_fatal_error(data, "REPEAT-NO-MATCH without a preceding EXPECT or STATUS on line %d of \"%s\".", f->linenum, f->filename); |
3714 | return (0); | |
08102776 MS |
3715 | } |
3716 | } | |
3717 | else if (!_cups_strcasecmp(token, "SAME-COUNT-AS")) | |
3718 | { | |
8e2e09c1 | 3719 | if (!_ippFileReadToken(f, temp, sizeof(temp))) |
08102776 MS |
3720 | { |
3721 | print_fatal_error(data, "Missing SAME-COUNT-AS name on line %d of \"%s\".", f->linenum, f->filename); | |
8e2e09c1 | 3722 | return (0); |
08102776 MS |
3723 | } |
3724 | ||
8e2e09c1 MS |
3725 | if (data->last_expect) |
3726 | { | |
3727 | data->last_expect->same_count_as = strdup(temp); | |
3728 | } | |
08102776 MS |
3729 | else |
3730 | { | |
8e2e09c1 MS |
3731 | print_fatal_error(data, "SAME-COUNT-AS without a preceding EXPECT on line %d of \"%s\".", f->linenum, f->filename); |
3732 | return (0); | |
08102776 MS |
3733 | } |
3734 | } | |
3735 | else if (!_cups_strcasecmp(token, "IF-DEFINED")) | |
3736 | { | |
8e2e09c1 | 3737 | if (!_ippFileReadToken(f, temp, sizeof(temp))) |
08102776 MS |
3738 | { |
3739 | print_fatal_error(data, "Missing IF-DEFINED name on line %d of \"%s\".", f->linenum, f->filename); | |
8e2e09c1 | 3740 | return (0); |
08102776 MS |
3741 | } |
3742 | ||
8e2e09c1 MS |
3743 | if (data->last_expect) |
3744 | { | |
3745 | data->last_expect->if_defined = strdup(temp); | |
3746 | } | |
3747 | else if (data->last_status) | |
3748 | { | |
3749 | data->last_status->if_defined = strdup(temp); | |
3750 | } | |
08102776 MS |
3751 | else |
3752 | { | |
3753 | print_fatal_error(data, "IF-DEFINED without a preceding EXPECT or STATUS on line %d of \"%s\".", f->linenum, f->filename); | |
8e2e09c1 | 3754 | return (0); |
08102776 MS |
3755 | } |
3756 | } | |
3757 | else if (!_cups_strcasecmp(token, "IF-NOT-DEFINED")) | |
3758 | { | |
8e2e09c1 | 3759 | if (!_ippFileReadToken(f, temp, sizeof(temp))) |
08102776 MS |
3760 | { |
3761 | print_fatal_error(data, "Missing IF-NOT-DEFINED name on line %d of \"%s\".", f->linenum, f->filename); | |
8e2e09c1 | 3762 | return (0); |
08102776 MS |
3763 | } |
3764 | ||
8e2e09c1 MS |
3765 | if (data->last_expect) |
3766 | { | |
3767 | data->last_expect->if_not_defined = strdup(temp); | |
3768 | } | |
3769 | else if (data->last_status) | |
3770 | { | |
3771 | data->last_status->if_not_defined = strdup(temp); | |
3772 | } | |
08102776 MS |
3773 | else |
3774 | { | |
3775 | print_fatal_error(data, "IF-NOT-DEFINED without a preceding EXPECT or STATUS on line %d of \"%s\".", f->linenum, f->filename); | |
8e2e09c1 | 3776 | return (0); |
08102776 MS |
3777 | } |
3778 | } | |
3779 | else if (!_cups_strcasecmp(token, "WITH-ALL-VALUES") || | |
3780 | !_cups_strcasecmp(token, "WITH-ALL-HOSTNAMES") || | |
3781 | !_cups_strcasecmp(token, "WITH-ALL-RESOURCES") || | |
3782 | !_cups_strcasecmp(token, "WITH-ALL-SCHEMES") || | |
3783 | !_cups_strcasecmp(token, "WITH-HOSTNAME") || | |
3784 | !_cups_strcasecmp(token, "WITH-RESOURCE") || | |
3785 | !_cups_strcasecmp(token, "WITH-SCHEME") || | |
3786 | !_cups_strcasecmp(token, "WITH-VALUE")) | |
3787 | { | |
8e2e09c1 | 3788 | off_t lastpos; /* Last file position */ |
2064e071 | 3789 | int lastline; /* Last line number */ |
8e2e09c1 MS |
3790 | |
3791 | if (data->last_expect) | |
3792 | { | |
3793 | if (!_cups_strcasecmp(token, "WITH-ALL-HOSTNAMES") || !_cups_strcasecmp(token, "WITH-HOSTNAME")) | |
3794 | data->last_expect->with_flags = _CUPS_WITH_HOSTNAME; | |
3795 | else if (!_cups_strcasecmp(token, "WITH-ALL-RESOURCES") || !_cups_strcasecmp(token, "WITH-RESOURCE")) | |
3796 | data->last_expect->with_flags = _CUPS_WITH_RESOURCE; | |
3797 | else if (!_cups_strcasecmp(token, "WITH-ALL-SCHEMES") || !_cups_strcasecmp(token, "WITH-SCHEME")) | |
3798 | data->last_expect->with_flags = _CUPS_WITH_SCHEME; | |
08102776 MS |
3799 | |
3800 | if (!_cups_strncasecmp(token, "WITH-ALL-", 9)) | |
8e2e09c1 | 3801 | data->last_expect->with_flags |= _CUPS_WITH_ALL; |
08102776 MS |
3802 | } |
3803 | ||
3804 | if (!_ippFileReadToken(f, temp, sizeof(temp))) | |
3805 | { | |
3806 | print_fatal_error(data, "Missing %s value on line %d of \"%s\".", token, f->linenum, f->filename); | |
8e2e09c1 MS |
3807 | return (0); |
3808 | } | |
3809 | ||
3810 | /* | |
3811 | * Read additional comma-delimited values - needed since legacy test files | |
3812 | * will have unquoted WITH-VALUE values with commas... | |
3813 | */ | |
3814 | ||
3815 | ptr = temp + strlen(temp); | |
3816 | ||
3817 | for (;;) | |
3818 | { | |
2064e071 MS |
3819 | lastpos = cupsFileTell(f->fp); |
3820 | lastline = f->linenum; | |
3821 | ptr += strlen(ptr); | |
8e2e09c1 MS |
3822 | |
3823 | if (!_ippFileReadToken(f, ptr, (sizeof(temp) - (size_t)(ptr - temp)))) | |
3824 | break; | |
3825 | ||
3826 | if (!strcmp(ptr, ",")) | |
3827 | { | |
3828 | /* | |
3829 | * Append a value... | |
3830 | */ | |
3831 | ||
3832 | ptr += strlen(ptr); | |
3833 | ||
3834 | if (!_ippFileReadToken(f, ptr, (sizeof(temp) - (size_t)(ptr - temp)))) | |
3835 | break; | |
3836 | } | |
3837 | else | |
3838 | { | |
3839 | /* | |
3840 | * Not another value, stop here... | |
3841 | */ | |
3842 | ||
3843 | cupsFileSeek(f->fp, lastpos); | |
2064e071 | 3844 | f->linenum = lastline; |
8e2e09c1 MS |
3845 | *ptr = '\0'; |
3846 | break; | |
3847 | } | |
08102776 MS |
3848 | } |
3849 | ||
8e2e09c1 | 3850 | if (data->last_expect) |
08102776 MS |
3851 | { |
3852 | /* | |
3853 | * Expand any variables in the value and then save it. | |
3854 | */ | |
3855 | ||
8e2e09c1 | 3856 | _ippVarsExpand(vars, value, temp, sizeof(value)); |
08102776 | 3857 | |
8e2e09c1 | 3858 | ptr = value + strlen(value) - 1; |
08102776 | 3859 | |
8e2e09c1 | 3860 | if (value[0] == '/' && ptr > value && *ptr == '/') |
08102776 MS |
3861 | { |
3862 | /* | |
3863 | * WITH-VALUE is a POSIX extended regular expression. | |
3864 | */ | |
3865 | ||
8e2e09c1 MS |
3866 | data->last_expect->with_value = calloc(1, (size_t)(ptr - value)); |
3867 | data->last_expect->with_flags |= _CUPS_WITH_REGEX; | |
08102776 | 3868 | |
8e2e09c1 MS |
3869 | if (data->last_expect->with_value) |
3870 | memcpy(data->last_expect->with_value, value + 1, (size_t)(ptr - value - 1)); | |
08102776 MS |
3871 | } |
3872 | else | |
3873 | { | |
3874 | /* | |
3875 | * WITH-VALUE is a literal value... | |
3876 | */ | |
3877 | ||
8e2e09c1 | 3878 | for (ptr = value; *ptr; ptr ++) |
08102776 MS |
3879 | { |
3880 | if (*ptr == '\\' && ptr[1]) | |
3881 | { | |
3882 | /* | |
3883 | * Remove \ from \foo... | |
3884 | */ | |
3885 | ||
3886 | _cups_strcpy(ptr, ptr + 1); | |
3887 | } | |
3888 | } | |
3889 | ||
8e2e09c1 MS |
3890 | data->last_expect->with_value = strdup(value); |
3891 | data->last_expect->with_flags |= _CUPS_WITH_LITERAL; | |
08102776 MS |
3892 | } |
3893 | } | |
3894 | else | |
3895 | { | |
3896 | print_fatal_error(data, "%s without a preceding EXPECT on line %d of \"%s\".", token, f->linenum, f->filename); | |
8e2e09c1 | 3897 | return (0); |
08102776 MS |
3898 | } |
3899 | } | |
3900 | else if (!_cups_strcasecmp(token, "WITH-VALUE-FROM")) | |
3901 | { | |
3902 | if (!_ippFileReadToken(f, temp, sizeof(temp))) | |
3903 | { | |
3904 | print_fatal_error(data, "Missing %s value on line %d of \"%s\".", token, f->linenum, f->filename); | |
8e2e09c1 | 3905 | return (0); |
08102776 MS |
3906 | } |
3907 | ||
8e2e09c1 | 3908 | if (data->last_expect) |
08102776 MS |
3909 | { |
3910 | /* | |
3911 | * Expand any variables in the value and then save it. | |
3912 | */ | |
3913 | ||
8e2e09c1 | 3914 | _ippVarsExpand(vars, value, temp, sizeof(value)); |
08102776 | 3915 | |
8e2e09c1 MS |
3916 | data->last_expect->with_value_from = strdup(value); |
3917 | data->last_expect->with_flags = _CUPS_WITH_LITERAL; | |
08102776 MS |
3918 | } |
3919 | else | |
3920 | { | |
3921 | print_fatal_error(data, "%s without a preceding EXPECT on line %d of \"%s\".", token, f->linenum, f->filename); | |
8e2e09c1 | 3922 | return (0); |
08102776 MS |
3923 | } |
3924 | } | |
3925 | else if (!_cups_strcasecmp(token, "DISPLAY")) | |
3926 | { | |
3927 | /* | |
3928 | * Display attributes... | |
3929 | */ | |
3930 | ||
8e2e09c1 | 3931 | if (data->num_displayed >= (int)(sizeof(data->displayed) / sizeof(data->displayed[0]))) |
08102776 | 3932 | { |
8e2e09c1 MS |
3933 | print_fatal_error(data, "Too many DISPLAY's on line %d of \"%s\".", f->linenum, f->filename); |
3934 | return (0); | |
08102776 MS |
3935 | } |
3936 | ||
8e2e09c1 | 3937 | if (!_ippFileReadToken(f, temp, sizeof(temp))) |
08102776 MS |
3938 | { |
3939 | print_fatal_error(data, "Missing DISPLAY name on line %d of \"%s\".", f->linenum, f->filename); | |
8e2e09c1 | 3940 | return (0); |
08102776 MS |
3941 | } |
3942 | ||
8e2e09c1 MS |
3943 | data->displayed[data->num_displayed] = strdup(temp); |
3944 | data->num_displayed ++; | |
08102776 MS |
3945 | } |
3946 | else | |
3947 | { | |
3948 | print_fatal_error(data, "Unexpected token %s seen on line %d of \"%s\".", token, f->linenum, f->filename); | |
3949 | return (0); | |
3950 | } | |
3951 | } | |
3952 | else | |
3953 | { | |
3954 | /* | |
3955 | * Scan for the start of a test (open brace)... | |
3956 | */ | |
3957 | ||
3958 | if (!strcmp(token, "{")) | |
3959 | { | |
3960 | /* | |
3961 | * Start new test... | |
3962 | */ | |
3963 | ||
08102776 MS |
3964 | if (data->show_header) |
3965 | { | |
3966 | if (data->output == _CUPS_OUTPUT_PLIST) | |
3967 | print_xml_header(data); | |
3968 | ||
3969 | if (data->output == _CUPS_OUTPUT_TEST || (data->output == _CUPS_OUTPUT_PLIST && data->outfile != cupsFileStdout())) | |
3970 | cupsFilePrintf(cupsFileStdout(), "\"%s\":\n", f->filename); | |
3971 | ||
3972 | data->show_header = 0; | |
3973 | } | |
3974 | ||
3975 | data->compression[0] = '\0'; | |
3976 | data->delay = 0; | |
8e2e09c1 | 3977 | data->num_expects = 0; |
08102776 | 3978 | data->last_expect = NULL; |
8e2e09c1 | 3979 | data->file[0] = '\0'; |
08102776 MS |
3980 | data->ignore_errors = data->def_ignore_errors; |
3981 | strlcpy(data->name, f->filename, sizeof(data->name)); | |
3982 | if ((ptr = strrchr(data->name, '.')) != NULL) | |
3983 | *ptr = '\0'; | |
08102776 MS |
3984 | data->repeat_interval = 5000000; |
3985 | data->request_id ++; | |
3986 | strlcpy(data->resource, vars->resource, sizeof(data->resource)); | |
3987 | data->skip_previous = 0; | |
3988 | data->skip_test = 0; | |
8e2e09c1 | 3989 | data->num_statuses = 0; |
08102776 MS |
3990 | data->last_status = NULL; |
3991 | data->test_id[0] = '\0'; | |
3992 | data->transfer = data->def_transfer; | |
3993 | data->version = data->def_version; | |
3994 | ||
3995 | f->attrs = ippNew(); | |
3996 | f->group_tag = IPP_TAG_ZERO; | |
3997 | } | |
3998 | else if (!strcmp(token, "DEFINE")) | |
3999 | { | |
4000 | /* | |
4001 | * DEFINE name value | |
4002 | */ | |
4003 | ||
4004 | if (_ippFileReadToken(f, name, sizeof(name)) && _ippFileReadToken(f, temp, sizeof(temp))) | |
4005 | { | |
4006 | _ippVarsExpand(vars, value, temp, sizeof(value)); | |
4007 | _ippVarsSet(vars, name, value); | |
4008 | } | |
4009 | else | |
4010 | { | |
4011 | print_fatal_error(data, "Missing DEFINE name and/or value on line %d of \"%s\".", f->linenum, f->filename); | |
4012 | return (0); | |
4013 | } | |
4014 | } | |
4015 | else if (!strcmp(token, "DEFINE-DEFAULT")) | |
4016 | { | |
4017 | /* | |
4018 | * DEFINE-DEFAULT name value | |
4019 | */ | |
4020 | ||
4021 | if (_ippFileReadToken(f, name, sizeof(name)) && _ippFileReadToken(f, temp, sizeof(temp))) | |
4022 | { | |
4023 | if (!_ippVarsGet(vars, name)) | |
4024 | { | |
4025 | _ippVarsExpand(vars, value, temp, sizeof(value)); | |
4026 | _ippVarsSet(vars, name, value); | |
4027 | } | |
4028 | } | |
4029 | else | |
4030 | { | |
4031 | print_fatal_error(data, "Missing DEFINE-DEFAULT name and/or value on line %d of \"%s\".", f->linenum, f->filename); | |
4032 | return (0); | |
4033 | } | |
4034 | } | |
4035 | else if (!strcmp(token, "FILE-ID")) | |
4036 | { | |
4037 | /* | |
4038 | * FILE-ID "string" | |
4039 | */ | |
4040 | ||
4041 | if (_ippFileReadToken(f, temp, sizeof(temp))) | |
4042 | { | |
4043 | _ippVarsExpand(vars, data->file_id, temp, sizeof(data->file_id)); | |
4044 | } | |
4045 | else | |
4046 | { | |
4047 | print_fatal_error(data, "Missing FILE-ID value on line %d of \"%s\".", f->linenum, f->filename); | |
4048 | return (0); | |
4049 | } | |
4050 | } | |
4051 | else if (!strcmp(token, "IGNORE-ERRORS")) | |
4052 | { | |
4053 | /* | |
4054 | * IGNORE-ERRORS yes | |
4055 | * IGNORE-ERRORS no | |
4056 | */ | |
4057 | ||
4058 | if (_ippFileReadToken(f, temp, sizeof(temp)) && (!_cups_strcasecmp(temp, "yes") || !_cups_strcasecmp(temp, "no"))) | |
4059 | { | |
4060 | data->def_ignore_errors = !_cups_strcasecmp(temp, "yes"); | |
4061 | } | |
4062 | else | |
4063 | { | |
4064 | print_fatal_error(data, "Missing IGNORE-ERRORS value on line %d of \"%s\".", f->linenum, f->filename); | |
4065 | return (0); | |
4066 | } | |
4067 | } | |
4068 | else if (!strcmp(token, "INCLUDE")) | |
4069 | { | |
4070 | /* | |
4071 | * INCLUDE "filename" | |
4072 | * INCLUDE <filename> | |
4073 | */ | |
4074 | ||
4075 | if (_ippFileReadToken(f, temp, sizeof(temp))) | |
4076 | { | |
4077 | /* | |
4078 | * Map the filename to and then run the tests... | |
4079 | */ | |
4080 | ||
8e2e09c1 MS |
4081 | _cups_testdata_t inc_data; /* Data for included file */ |
4082 | char filename[1024]; /* Mapped filename */ | |
08102776 | 4083 | |
8e2e09c1 MS |
4084 | memcpy(&inc_data, data, sizeof(inc_data)); |
4085 | inc_data.http = NULL; | |
4086 | inc_data.pass = 1; | |
4087 | inc_data.prev_pass = 1; | |
4088 | inc_data.show_header = 1; | |
08102776 | 4089 | |
8e2e09c1 MS |
4090 | if (!do_tests(get_filename(f->filename, filename, temp, sizeof(filename)), vars, &inc_data) && data->stop_after_include_error) |
4091 | { | |
4092 | data->pass = data->prev_pass = 0; | |
08102776 | 4093 | return (0); |
8e2e09c1 | 4094 | } |
08102776 MS |
4095 | } |
4096 | else | |
4097 | { | |
4098 | print_fatal_error(data, "Missing INCLUDE filename on line %d of \"%s\".", f->linenum, f->filename); | |
4099 | return (0); | |
4100 | } | |
4101 | ||
4102 | data->show_header = 1; | |
4103 | } | |
4104 | else if (!strcmp(token, "INCLUDE-IF-DEFINED")) | |
4105 | { | |
4106 | /* | |
4107 | * INCLUDE-IF-DEFINED name "filename" | |
4108 | * INCLUDE-IF-DEFINED name <filename> | |
4109 | */ | |
4110 | ||
4111 | if (_ippFileReadToken(f, name, sizeof(name)) && _ippFileReadToken(f, temp, sizeof(temp))) | |
4112 | { | |
4113 | /* | |
4114 | * Map the filename to and then run the tests... | |
4115 | */ | |
4116 | ||
8e2e09c1 MS |
4117 | _cups_testdata_t inc_data; /* Data for included file */ |
4118 | char filename[1024]; /* Mapped filename */ | |
08102776 | 4119 | |
8e2e09c1 MS |
4120 | memcpy(&inc_data, data, sizeof(inc_data)); |
4121 | inc_data.http = NULL; | |
4122 | inc_data.pass = 1; | |
4123 | inc_data.prev_pass = 1; | |
4124 | inc_data.show_header = 1; | |
08102776 | 4125 | |
8e2e09c1 MS |
4126 | if (!do_tests(get_filename(f->filename, filename, temp, sizeof(filename)), vars, &inc_data) && data->stop_after_include_error) |
4127 | { | |
4128 | data->pass = data->prev_pass = 0; | |
08102776 | 4129 | return (0); |
8e2e09c1 | 4130 | } |
08102776 MS |
4131 | } |
4132 | else | |
4133 | { | |
4134 | print_fatal_error(data, "Missing INCLUDE-IF-DEFINED name or filename on line %d of \"%s\".", f->linenum, f->filename); | |
4135 | return (0); | |
4136 | } | |
4137 | ||
4138 | data->show_header = 1; | |
4139 | } | |
4140 | else if (!strcmp(token, "INCLUDE-IF-NOT-DEFINED")) | |
4141 | { | |
4142 | /* | |
4143 | * INCLUDE-IF-NOT-DEFINED name "filename" | |
4144 | * INCLUDE-IF-NOT-DEFINED name <filename> | |
4145 | */ | |
4146 | ||
4147 | if (_ippFileReadToken(f, name, sizeof(name)) && _ippFileReadToken(f, temp, sizeof(temp))) | |
4148 | { | |
4149 | /* | |
4150 | * Map the filename to and then run the tests... | |
4151 | */ | |
4152 | ||
8e2e09c1 MS |
4153 | _cups_testdata_t inc_data; /* Data for included file */ |
4154 | char filename[1024]; /* Mapped filename */ | |
08102776 | 4155 | |
8e2e09c1 MS |
4156 | memcpy(&inc_data, data, sizeof(inc_data)); |
4157 | inc_data.http = NULL; | |
4158 | inc_data.pass = 1; | |
4159 | inc_data.prev_pass = 1; | |
4160 | inc_data.show_header = 1; | |
08102776 | 4161 | |
8e2e09c1 MS |
4162 | if (!do_tests(get_filename(f->filename, filename, temp, sizeof(filename)), vars, &inc_data) && data->stop_after_include_error) |
4163 | { | |
4164 | data->pass = data->prev_pass = 0; | |
08102776 | 4165 | return (0); |
8e2e09c1 | 4166 | } |
08102776 MS |
4167 | } |
4168 | else | |
4169 | { | |
4170 | print_fatal_error(data, "Missing INCLUDE-IF-NOT-DEFINED name or filename on line %d of \"%s\".", f->linenum, f->filename); | |
4171 | return (0); | |
4172 | } | |
4173 | ||
4174 | data->show_header = 1; | |
4175 | } | |
4176 | else if (!strcmp(token, "SKIP-IF-DEFINED")) | |
4177 | { | |
4178 | /* | |
4179 | * SKIP-IF-DEFINED variable | |
4180 | */ | |
4181 | ||
4182 | if (_ippFileReadToken(f, name, sizeof(name))) | |
4183 | { | |
4184 | if (_ippVarsGet(vars, name)) | |
4185 | data->skip_test = 1; | |
4186 | } | |
4187 | else | |
4188 | { | |
4189 | print_fatal_error(data, "Missing SKIP-IF-DEFINED variable on line %d of \"%s\".", f->linenum, f->filename); | |
4190 | return (0); | |
4191 | } | |
4192 | } | |
4193 | else if (!strcmp(token, "SKIP-IF-NOT-DEFINED")) | |
4194 | { | |
4195 | /* | |
4196 | * SKIP-IF-NOT-DEFINED variable | |
4197 | */ | |
4198 | ||
4199 | if (_ippFileReadToken(f, name, sizeof(name))) | |
4200 | { | |
4201 | if (!_ippVarsGet(vars, name)) | |
4202 | data->skip_test = 1; | |
4203 | } | |
4204 | else | |
4205 | { | |
4206 | print_fatal_error(data, "Missing SKIP-IF-NOT-DEFINED variable on line %d of \"%s\".", f->linenum, f->filename); | |
4207 | return (0); | |
4208 | } | |
4209 | } | |
4210 | else if (!strcmp(token, "STOP-AFTER-INCLUDE-ERROR")) | |
4211 | { | |
4212 | /* | |
4213 | * STOP-AFTER-INCLUDE-ERROR yes | |
4214 | * STOP-AFTER-INCLUDE-ERROR no | |
4215 | */ | |
4216 | ||
4217 | if (_ippFileReadToken(f, temp, sizeof(temp)) && (!_cups_strcasecmp(temp, "yes") || !_cups_strcasecmp(temp, "no"))) | |
4218 | { | |
4219 | data->stop_after_include_error = !_cups_strcasecmp(temp, "yes"); | |
4220 | } | |
4221 | else | |
4222 | { | |
4223 | print_fatal_error(data, "Missing STOP-AFTER-INCLUDE-ERROR value on line %d of \"%s\".", f->linenum, f->filename); | |
4224 | return (0); | |
4225 | } | |
4226 | } | |
4227 | else if (!strcmp(token, "TRANSFER")) | |
4228 | { | |
4229 | /* | |
4230 | * TRANSFER auto | |
4231 | * TRANSFER chunked | |
4232 | * TRANSFER length | |
4233 | */ | |
4234 | ||
4235 | if (_ippFileReadToken(f, temp, sizeof(temp))) | |
4236 | { | |
4237 | if (!strcmp(temp, "auto")) | |
4238 | data->def_transfer = _CUPS_TRANSFER_AUTO; | |
4239 | else if (!strcmp(temp, "chunked")) | |
4240 | data->def_transfer = _CUPS_TRANSFER_CHUNKED; | |
4241 | else if (!strcmp(temp, "length")) | |
4242 | data->def_transfer = _CUPS_TRANSFER_LENGTH; | |
4243 | else | |
4244 | { | |
4245 | print_fatal_error(data, "Bad TRANSFER value \"%s\" on line %d of \"%s\".", temp, f->linenum, f->filename); | |
4246 | return (0); | |
4247 | } | |
4248 | } | |
4249 | else | |
4250 | { | |
4251 | print_fatal_error(data, "Missing TRANSFER value on line %d of \"%s\".", f->linenum, f->filename); | |
4252 | return (0); | |
4253 | } | |
4254 | } | |
4255 | else if (!strcmp(token, "VERSION")) | |
4256 | { | |
4257 | if (_ippFileReadToken(f, temp, sizeof(temp))) | |
4258 | { | |
4259 | if (!strcmp(temp, "1.0")) | |
4260 | data->def_version = 10; | |
4261 | else if (!strcmp(temp, "1.1")) | |
4262 | data->def_version = 11; | |
4263 | else if (!strcmp(temp, "2.0")) | |
4264 | data->def_version = 20; | |
4265 | else if (!strcmp(temp, "2.1")) | |
4266 | data->def_version = 21; | |
4267 | else if (!strcmp(temp, "2.2")) | |
4268 | data->def_version = 22; | |
4269 | else | |
4270 | { | |
4271 | print_fatal_error(data, "Bad VERSION \"%s\" on line %d of \"%s\".", temp, f->linenum, f->filename); | |
4272 | return (0); | |
4273 | } | |
4274 | } | |
4275 | else | |
4276 | { | |
4277 | print_fatal_error(data, "Missing VERSION number on line %d of \"%s\".", f->linenum, f->filename); | |
4278 | return (0); | |
4279 | } | |
4280 | } | |
4281 | else | |
4282 | { | |
4283 | print_fatal_error(data, "Unexpected token %s seen on line %d of \"%s\".", token, f->linenum, f->filename); | |
4284 | return (0); | |
4285 | } | |
4286 | } | |
4287 | ||
4288 | return (1); | |
4289 | } | |
4290 | ||
4291 | ||
ef416fc2 | 4292 | /* |
e00b005a | 4293 | * 'usage()' - Show program usage. |
4294 | */ | |
4295 | ||
5a662dc0 MS |
4296 | static void |
4297 | usage(void) | |
e00b005a | 4298 | { |
2a75f21b | 4299 | _cupsLangPuts(stderr, _("Usage: ipptool [options] URI filename [ ... filenameN ]")); |
0837b7e8 | 4300 | _cupsLangPuts(stderr, _("Options:")); |
1b6d468c | 4301 | _cupsLangPuts(stderr, _(" --help Show help.")); |
6646208b | 4302 | _cupsLangPuts(stderr, _(" --ippserver filename Produce ippserver attribute file.")); |
1b6d468c MS |
4303 | _cupsLangPuts(stderr, _(" --stop-after-include-error\n" |
4304 | " Stop tests after a failed INCLUDE.")); | |
4305 | _cupsLangPuts(stderr, _(" --version Show version.")); | |
84315f46 MS |
4306 | _cupsLangPuts(stderr, _(" -4 Connect using IPv4.")); |
4307 | _cupsLangPuts(stderr, _(" -6 Connect using IPv6.")); | |
4308 | _cupsLangPuts(stderr, _(" -C Send requests using " | |
4309 | "chunking (default).")); | |
2a75f21b | 4310 | _cupsLangPuts(stderr, _(" -E Test with encryption using HTTP Upgrade to TLS.")); |
84315f46 | 4311 | _cupsLangPuts(stderr, _(" -I Ignore errors.")); |
2a75f21b | 4312 | _cupsLangPuts(stderr, _(" -L Send requests using content-length.")); |
1b6d468c | 4313 | _cupsLangPuts(stderr, _(" -P filename.plist Produce XML plist to a file and test report to standard output.")); |
2a75f21b MS |
4314 | _cupsLangPuts(stderr, _(" -S Test with encryption using HTTPS.")); |
4315 | _cupsLangPuts(stderr, _(" -T seconds Set the receive/send timeout in seconds.")); | |
4316 | _cupsLangPuts(stderr, _(" -V version Set default IPP version.")); | |
4317 | _cupsLangPuts(stderr, _(" -X Produce XML plist instead of plain text.")); | |
1b6d468c | 4318 | _cupsLangPuts(stderr, _(" -c Produce CSV output.")); |
2a75f21b MS |
4319 | _cupsLangPuts(stderr, _(" -d name=value Set named variable to value.")); |
4320 | _cupsLangPuts(stderr, _(" -f filename Set default request filename.")); | |
345e10ca | 4321 | _cupsLangPuts(stderr, _(" -h Validate HTTP response headers.")); |
2a75f21b | 4322 | _cupsLangPuts(stderr, _(" -i seconds Repeat the last file with the given time interval.")); |
1b6d468c | 4323 | _cupsLangPuts(stderr, _(" -l Produce plain text output.")); |
2a75f21b | 4324 | _cupsLangPuts(stderr, _(" -n count Repeat the last file the given number of times.")); |
f3c17241 | 4325 | _cupsLangPuts(stderr, _(" -q Run silently.")); |
84315f46 | 4326 | _cupsLangPuts(stderr, _(" -t Produce a test report.")); |
f3c17241 | 4327 | _cupsLangPuts(stderr, _(" -v Be verbose.")); |
e00b005a | 4328 | |
4329 | exit(1); | |
4330 | } | |
4331 | ||
4332 | ||
ea4dcf9f MS |
4333 | /* |
4334 | * 'with_flags_string()' - Return the "WITH-xxx" predicate that corresponds to | |
4335 | the flags. | |
4336 | */ | |
4337 | ||
4338 | static const char * /* O - WITH-xxx string */ | |
4339 | with_flags_string(int flags) /* I - WITH flags */ | |
4340 | { | |
4341 | if (flags & _CUPS_WITH_ALL) | |
4342 | { | |
4343 | if (flags & _CUPS_WITH_HOSTNAME) | |
4344 | return ("WITH-ALL-HOSTNAMES"); | |
4345 | else if (flags & _CUPS_WITH_RESOURCE) | |
4346 | return ("WITH-ALL-RESOURCES"); | |
4347 | else if (flags & _CUPS_WITH_SCHEME) | |
4348 | return ("WITH-ALL-SCHEMES"); | |
4349 | else | |
4350 | return ("WITH-ALL-VALUES"); | |
4351 | } | |
4352 | else if (flags & _CUPS_WITH_HOSTNAME) | |
4353 | return ("WITH-HOSTNAME"); | |
4354 | else if (flags & _CUPS_WITH_RESOURCE) | |
4355 | return ("WITH-RESOURCE"); | |
4356 | else if (flags & _CUPS_WITH_SCHEME) | |
4357 | return ("WITH-SCHEME"); | |
4358 | else | |
4359 | return ("WITH-VALUE"); | |
4360 | } | |
4361 | ||
4362 | ||
f8b3a85b MS |
4363 | /* |
4364 | * 'with_value()' - Test a WITH-VALUE predicate. | |
4365 | */ | |
4366 | ||
4367 | static int /* O - 1 on match, 0 on non-match */ | |
8e2e09c1 MS |
4368 | with_value(_cups_testdata_t *data, /* I - Test data */ |
4369 | cups_array_t *errors, /* I - Errors array */ | |
4370 | char *value, /* I - Value string */ | |
4371 | int flags, /* I - Flags for match */ | |
4372 | ipp_attribute_t *attr, /* I - Attribute to compare */ | |
4373 | char *matchbuf, /* I - Buffer to hold matching value */ | |
4374 | size_t matchlen) /* I - Length of match buffer */ | |
f8b3a85b | 4375 | { |
a492d345 MS |
4376 | int i, /* Looping var */ |
4377 | count, /* Number of values */ | |
4378 | match; /* Match? */ | |
4379 | char temp[1024], /* Temporary value string */ | |
4380 | *valptr; /* Pointer into value */ | |
4381 | const char *name; /* Attribute name */ | |
f8b3a85b MS |
4382 | |
4383 | ||
9b66acc5 | 4384 | *matchbuf = '\0'; |
82cc1f9a | 4385 | match = (flags & _CUPS_WITH_ALL) ? 1 : 0; |
9b66acc5 | 4386 | |
f8b3a85b MS |
4387 | /* |
4388 | * NULL matches everything. | |
4389 | */ | |
4390 | ||
4391 | if (!value || !*value) | |
4392 | return (1); | |
4393 | ||
4394 | /* | |
4395 | * Compare the value string to the attribute value. | |
4396 | */ | |
4397 | ||
a492d345 MS |
4398 | name = ippGetName(attr); |
4399 | count = ippGetCount(attr); | |
4400 | ||
4401 | switch (ippGetValueTag(attr)) | |
f8b3a85b MS |
4402 | { |
4403 | case IPP_TAG_INTEGER : | |
4404 | case IPP_TAG_ENUM : | |
7f4d7b8a | 4405 | for (i = 0; i < count; i ++) |
f8b3a85b MS |
4406 | { |
4407 | char op, /* Comparison operator */ | |
4408 | *nextptr; /* Next pointer */ | |
82cc1f9a | 4409 | int intvalue, /* Integer value */ |
a492d345 MS |
4410 | attrvalue = ippGetInteger(attr, i), |
4411 | /* Attribute value */ | |
82cc1f9a | 4412 | valmatch = 0; /* Does the current value match? */ |
f8b3a85b MS |
4413 | |
4414 | valptr = value; | |
f8b3a85b MS |
4415 | |
4416 | while (isspace(*valptr & 255) || isdigit(*valptr & 255) || | |
4417 | *valptr == '-' || *valptr == ',' || *valptr == '<' || | |
4418 | *valptr == '=' || *valptr == '>') | |
4419 | { | |
4420 | op = '='; | |
4421 | while (*valptr && !isdigit(*valptr & 255) && *valptr != '-') | |
4422 | { | |
4423 | if (*valptr == '<' || *valptr == '>' || *valptr == '=') | |
4424 | op = *valptr; | |
4425 | valptr ++; | |
4426 | } | |
4427 | ||
4428 | if (!*valptr) | |
4429 | break; | |
4430 | ||
7e86f2f6 | 4431 | intvalue = (int)strtol(valptr, &nextptr, 0); |
f8b3a85b MS |
4432 | if (nextptr == valptr) |
4433 | break; | |
4434 | valptr = nextptr; | |
4435 | ||
a492d345 MS |
4436 | if ((op == '=' && attrvalue == intvalue) || |
4437 | (op == '<' && attrvalue < intvalue) || | |
4438 | (op == '>' && attrvalue > intvalue)) | |
f8b3a85b | 4439 | { |
82cc1f9a | 4440 | if (!matchbuf[0]) |
a492d345 | 4441 | snprintf(matchbuf, matchlen, "%d", attrvalue); |
82cc1f9a MS |
4442 | |
4443 | valmatch = 1; | |
4444 | break; | |
f8b3a85b MS |
4445 | } |
4446 | } | |
82cc1f9a MS |
4447 | |
4448 | if (flags & _CUPS_WITH_ALL) | |
4449 | { | |
4450 | if (!valmatch) | |
4451 | { | |
4452 | match = 0; | |
4453 | break; | |
4454 | } | |
4455 | } | |
4456 | else if (valmatch) | |
4457 | { | |
4458 | match = 1; | |
4459 | break; | |
4460 | } | |
f8b3a85b | 4461 | } |
7cf5915e | 4462 | |
82cc1f9a | 4463 | if (!match && errors) |
7cf5915e | 4464 | { |
7f4d7b8a | 4465 | for (i = 0; i < count; i ++) |
a492d345 | 4466 | add_stringf(data->errors, "GOT: %s=%d", name, ippGetInteger(attr, i)); |
7cf5915e | 4467 | } |
f8b3a85b MS |
4468 | break; |
4469 | ||
10d09e33 | 4470 | case IPP_TAG_RANGE : |
7f4d7b8a | 4471 | for (i = 0; i < count; i ++) |
10d09e33 MS |
4472 | { |
4473 | char op, /* Comparison operator */ | |
4474 | *nextptr; /* Next pointer */ | |
82cc1f9a | 4475 | int intvalue, /* Integer value */ |
a492d345 MS |
4476 | lower, /* Lower range */ |
4477 | upper, /* Upper range */ | |
82cc1f9a | 4478 | valmatch = 0; /* Does the current value match? */ |
10d09e33 | 4479 | |
a492d345 | 4480 | lower = ippGetRange(attr, i, &upper); |
10d09e33 | 4481 | valptr = value; |
10d09e33 MS |
4482 | |
4483 | while (isspace(*valptr & 255) || isdigit(*valptr & 255) || | |
4484 | *valptr == '-' || *valptr == ',' || *valptr == '<' || | |
4485 | *valptr == '=' || *valptr == '>') | |
4486 | { | |
4487 | op = '='; | |
4488 | while (*valptr && !isdigit(*valptr & 255) && *valptr != '-') | |
4489 | { | |
4490 | if (*valptr == '<' || *valptr == '>' || *valptr == '=') | |
4491 | op = *valptr; | |
4492 | valptr ++; | |
4493 | } | |
4494 | ||
4495 | if (!*valptr) | |
4496 | break; | |
4497 | ||
7e86f2f6 | 4498 | intvalue = (int)strtol(valptr, &nextptr, 0); |
10d09e33 MS |
4499 | if (nextptr == valptr) |
4500 | break; | |
4501 | valptr = nextptr; | |
4502 | ||
a492d345 MS |
4503 | if ((op == '=' && (lower == intvalue || upper == intvalue)) || |
4504 | (op == '<' && upper < intvalue) || | |
4505 | (op == '>' && upper > intvalue)) | |
10d09e33 | 4506 | { |
82cc1f9a | 4507 | if (!matchbuf[0]) |
a492d345 | 4508 | snprintf(matchbuf, matchlen, "%d-%d", lower, upper); |
82cc1f9a MS |
4509 | |
4510 | valmatch = 1; | |
4511 | break; | |
10d09e33 MS |
4512 | } |
4513 | } | |
82cc1f9a MS |
4514 | |
4515 | if (flags & _CUPS_WITH_ALL) | |
4516 | { | |
4517 | if (!valmatch) | |
4518 | { | |
4519 | match = 0; | |
4520 | break; | |
4521 | } | |
4522 | } | |
4523 | else if (valmatch) | |
4524 | { | |
4525 | match = 1; | |
4526 | break; | |
4527 | } | |
10d09e33 MS |
4528 | } |
4529 | ||
82cc1f9a | 4530 | if (!match && errors) |
10d09e33 | 4531 | { |
7f4d7b8a | 4532 | for (i = 0; i < count; i ++) |
a492d345 MS |
4533 | { |
4534 | int lower, upper; /* Range values */ | |
4535 | ||
4536 | lower = ippGetRange(attr, i, &upper); | |
4537 | add_stringf(data->errors, "GOT: %s=%d-%d", name, lower, upper); | |
4538 | } | |
10d09e33 MS |
4539 | } |
4540 | break; | |
4541 | ||
f8b3a85b | 4542 | case IPP_TAG_BOOLEAN : |
7f4d7b8a | 4543 | for (i = 0; i < count; i ++) |
f8b3a85b | 4544 | { |
a492d345 | 4545 | if ((!strcmp(value, "true")) == ippGetBoolean(attr, i)) |
9b66acc5 | 4546 | { |
82cc1f9a MS |
4547 | if (!matchbuf[0]) |
4548 | strlcpy(matchbuf, value, matchlen); | |
4549 | ||
4550 | if (!(flags & _CUPS_WITH_ALL)) | |
4551 | { | |
4552 | match = 1; | |
4553 | break; | |
4554 | } | |
4555 | } | |
4556 | else if (flags & _CUPS_WITH_ALL) | |
4557 | { | |
4558 | match = 0; | |
4559 | break; | |
9b66acc5 | 4560 | } |
f8b3a85b | 4561 | } |
7cf5915e | 4562 | |
82cc1f9a | 4563 | if (!match && errors) |
7cf5915e | 4564 | { |
7f4d7b8a | 4565 | for (i = 0; i < count; i ++) |
a492d345 | 4566 | add_stringf(data->errors, "GOT: %s=%s", name, ippGetBoolean(attr, i) ? "true" : "false"); |
7cf5915e | 4567 | } |
f8b3a85b MS |
4568 | break; |
4569 | ||
db8b865d | 4570 | case IPP_TAG_RESOLUTION : |
7f4d7b8a | 4571 | for (i = 0; i < count; i ++) |
db8b865d | 4572 | { |
a492d345 MS |
4573 | int xres, yres; /* Resolution values */ |
4574 | ipp_res_t units; /* Resolution units */ | |
4575 | ||
4576 | xres = ippGetResolution(attr, i, &yres, &units); | |
4577 | if (xres == yres) | |
4578 | snprintf(temp, sizeof(temp), "%d%s", xres, units == IPP_RES_PER_INCH ? "dpi" : "dpcm"); | |
db8b865d | 4579 | else |
a492d345 | 4580 | snprintf(temp, sizeof(temp), "%dx%d%s", xres, yres, units == IPP_RES_PER_INCH ? "dpi" : "dpcm"); |
db8b865d MS |
4581 | |
4582 | if (!strcmp(value, temp)) | |
4583 | { | |
4584 | if (!matchbuf[0]) | |
4585 | strlcpy(matchbuf, value, matchlen); | |
4586 | ||
4587 | if (!(flags & _CUPS_WITH_ALL)) | |
4588 | { | |
4589 | match = 1; | |
4590 | break; | |
4591 | } | |
4592 | } | |
4593 | else if (flags & _CUPS_WITH_ALL) | |
4594 | { | |
4595 | match = 0; | |
4596 | break; | |
4597 | } | |
4598 | } | |
4599 | ||
4600 | if (!match && errors) | |
4601 | { | |
7f4d7b8a | 4602 | for (i = 0; i < count; i ++) |
db8b865d | 4603 | { |
a492d345 MS |
4604 | int xres, yres; /* Resolution values */ |
4605 | ipp_res_t units; /* Resolution units */ | |
4606 | ||
4607 | xres = ippGetResolution(attr, i, &yres, &units); | |
4608 | if (xres == yres) | |
4609 | snprintf(temp, sizeof(temp), "%d%s", xres, units == IPP_RES_PER_INCH ? "dpi" : "dpcm"); | |
db8b865d | 4610 | else |
a492d345 | 4611 | snprintf(temp, sizeof(temp), "%dx%d%s", xres, yres, units == IPP_RES_PER_INCH ? "dpi" : "dpcm"); |
db8b865d MS |
4612 | |
4613 | if (strcmp(value, temp)) | |
a492d345 | 4614 | add_stringf(data->errors, "GOT: %s=%s", name, temp); |
db8b865d MS |
4615 | } |
4616 | } | |
4617 | break; | |
4618 | ||
f8b3a85b | 4619 | case IPP_TAG_NOVALUE : |
d7225fc2 MS |
4620 | case IPP_TAG_UNKNOWN : |
4621 | return (1); | |
f8b3a85b MS |
4622 | |
4623 | case IPP_TAG_CHARSET : | |
4624 | case IPP_TAG_KEYWORD : | |
4625 | case IPP_TAG_LANGUAGE : | |
4626 | case IPP_TAG_MIMETYPE : | |
4627 | case IPP_TAG_NAME : | |
4628 | case IPP_TAG_NAMELANG : | |
4629 | case IPP_TAG_TEXT : | |
4630 | case IPP_TAG_TEXTLANG : | |
4631 | case IPP_TAG_URI : | |
4632 | case IPP_TAG_URISCHEME : | |
82cc1f9a | 4633 | if (flags & _CUPS_WITH_REGEX) |
f8b3a85b MS |
4634 | { |
4635 | /* | |
4636 | * Value is an extended, case-sensitive POSIX regular expression... | |
4637 | */ | |
4638 | ||
4639 | regex_t re; /* Regular expression */ | |
4640 | ||
4641 | if ((i = regcomp(&re, value, REG_EXTENDED | REG_NOSUB)) != 0) | |
4642 | { | |
f8b3a85b MS |
4643 | regerror(i, &re, temp, sizeof(temp)); |
4644 | ||
a492d345 | 4645 | print_fatal_error(data, "Unable to compile WITH-VALUE regular expression \"%s\" - %s", value, temp); |
f8b3a85b MS |
4646 | return (0); |
4647 | } | |
4648 | ||
4649 | /* | |
4650 | * See if ALL of the values match the given regular expression. | |
4651 | */ | |
4652 | ||
7f4d7b8a | 4653 | for (i = 0; i < count; i ++) |
f8b3a85b | 4654 | { |
890a10b7 MS |
4655 | if (!regexec(&re, get_string(attr, i, flags, temp, sizeof(temp)), |
4656 | 0, NULL, 0)) | |
7cf5915e | 4657 | { |
82cc1f9a | 4658 | if (!matchbuf[0]) |
a492d345 | 4659 | strlcpy(matchbuf, get_string(attr, i, flags, temp, sizeof(temp)), matchlen); |
82cc1f9a MS |
4660 | |
4661 | if (!(flags & _CUPS_WITH_ALL)) | |
4662 | { | |
4663 | match = 1; | |
7cf5915e | 4664 | break; |
82cc1f9a MS |
4665 | } |
4666 | } | |
4667 | else if (flags & _CUPS_WITH_ALL) | |
4668 | { | |
4669 | match = 0; | |
4670 | break; | |
7cf5915e | 4671 | } |
f8b3a85b MS |
4672 | } |
4673 | ||
4674 | regfree(&re); | |
f8b3a85b | 4675 | } |
ea4dcf9f | 4676 | else if (ippGetValueTag(attr) == IPP_TAG_URI && !(flags & (_CUPS_WITH_SCHEME | _CUPS_WITH_HOSTNAME | _CUPS_WITH_RESOURCE))) |
1b01c393 | 4677 | { |
1b01c393 MS |
4678 | /* |
4679 | * Value is a literal URI string, see if the value(s) match... | |
4680 | */ | |
4681 | ||
7f4d7b8a | 4682 | for (i = 0; i < count; i ++) |
1b01c393 | 4683 | { |
2a75f21b | 4684 | if (!compare_uris(value, get_string(attr, i, flags, temp, sizeof(temp)))) |
1b01c393 MS |
4685 | { |
4686 | if (!matchbuf[0]) | |
a492d345 | 4687 | strlcpy(matchbuf, get_string(attr, i, flags, temp, sizeof(temp)), matchlen); |
1b01c393 MS |
4688 | |
4689 | if (!(flags & _CUPS_WITH_ALL)) | |
4690 | { | |
4691 | match = 1; | |
4692 | break; | |
4693 | } | |
4694 | } | |
4695 | else if (flags & _CUPS_WITH_ALL) | |
4696 | { | |
4697 | match = 0; | |
4698 | break; | |
4699 | } | |
4700 | } | |
4701 | } | |
f8b3a85b MS |
4702 | else |
4703 | { | |
4704 | /* | |
82cc1f9a | 4705 | * Value is a literal string, see if the value(s) match... |
f8b3a85b MS |
4706 | */ |
4707 | ||
7f4d7b8a | 4708 | for (i = 0; i < count; i ++) |
f8b3a85b | 4709 | { |
ea4dcf9f MS |
4710 | int result; |
4711 | ||
4712 | switch (ippGetValueTag(attr)) | |
4713 | { | |
4714 | case IPP_TAG_URI : | |
4715 | /* | |
4716 | * Some URI components are case-sensitive, some not... | |
4717 | */ | |
4718 | ||
4719 | if (flags & (_CUPS_WITH_SCHEME | _CUPS_WITH_HOSTNAME)) | |
4720 | result = _cups_strcasecmp(value, get_string(attr, i, flags, temp, sizeof(temp))); | |
4721 | else | |
4722 | result = strcmp(value, get_string(attr, i, flags, temp, sizeof(temp))); | |
4723 | break; | |
4724 | ||
4725 | case IPP_TAG_MIMETYPE : | |
4726 | case IPP_TAG_NAME : | |
4727 | case IPP_TAG_NAMELANG : | |
4728 | case IPP_TAG_TEXT : | |
4729 | case IPP_TAG_TEXTLANG : | |
4730 | /* | |
4731 | * mimeMediaType, nameWithoutLanguage, nameWithLanguage, | |
4732 | * textWithoutLanguage, and textWithLanguage are defined to | |
4733 | * be case-insensitive strings... | |
4734 | */ | |
4735 | ||
4736 | result = _cups_strcasecmp(value, get_string(attr, i, flags, temp, sizeof(temp))); | |
4737 | break; | |
4738 | ||
4739 | default : | |
4740 | /* | |
4741 | * Other string syntaxes are defined as lowercased so we use | |
4742 | * case-sensitive comparisons to catch problems... | |
4743 | */ | |
4744 | ||
4745 | result = strcmp(value, get_string(attr, i, flags, temp, sizeof(temp))); | |
4746 | break; | |
4747 | } | |
4748 | ||
4749 | if (!result) | |
9b66acc5 | 4750 | { |
82cc1f9a | 4751 | if (!matchbuf[0]) |
a492d345 | 4752 | strlcpy(matchbuf, get_string(attr, i, flags, temp, sizeof(temp)), matchlen); |
7cf5915e | 4753 | |
82cc1f9a MS |
4754 | if (!(flags & _CUPS_WITH_ALL)) |
4755 | { | |
4756 | match = 1; | |
4757 | break; | |
4758 | } | |
4759 | } | |
4760 | else if (flags & _CUPS_WITH_ALL) | |
4761 | { | |
4762 | match = 0; | |
4763 | break; | |
4764 | } | |
7cf5915e | 4765 | } |
f8b3a85b | 4766 | } |
82cc1f9a MS |
4767 | |
4768 | if (!match && errors) | |
4769 | { | |
7f4d7b8a | 4770 | for (i = 0; i < count; i ++) |
a492d345 | 4771 | add_stringf(data->errors, "GOT: %s=\"%s\"", name, ippGetString(attr, i, NULL)); |
82cc1f9a | 4772 | } |
f8b3a85b MS |
4773 | break; |
4774 | ||
a32af27c MS |
4775 | case IPP_TAG_STRING : |
4776 | { | |
4777 | unsigned char withdata[1023], /* WITH-VALUE data */ | |
4778 | *adata; /* Pointer to octetString data */ | |
4779 | int withlen, /* Length of WITH-VALUE data */ | |
4780 | adatalen; /* Length of octetString */ | |
4781 | ||
4782 | if (*value == '<') | |
4783 | { | |
4784 | /* | |
4785 | * Grab hex-encoded value... | |
4786 | */ | |
4787 | ||
29bda83e | 4788 | if ((withlen = (int)strlen(value)) & 1 || withlen > (int)(2 * (sizeof(withdata) + 1))) |
a32af27c MS |
4789 | { |
4790 | print_fatal_error(data, "Bad WITH-VALUE hex value."); | |
4791 | return (0); | |
4792 | } | |
4793 | ||
4794 | withlen = withlen / 2 - 1; | |
4795 | ||
4796 | for (valptr = value + 1, adata = withdata; *valptr; valptr += 2) | |
4797 | { | |
4798 | int ch; /* Current character/byte */ | |
4799 | ||
4800 | if (isdigit(valptr[0])) | |
4801 | ch = (valptr[0] - '0') << 4; | |
4802 | else if (isalpha(valptr[0])) | |
4803 | ch = (tolower(valptr[0]) - 'a' + 10) << 4; | |
4804 | else | |
4805 | break; | |
4806 | ||
4807 | if (isdigit(valptr[1])) | |
4808 | ch |= valptr[1] - '0'; | |
4809 | else if (isalpha(valptr[1])) | |
4810 | ch |= tolower(valptr[1]) - 'a' + 10; | |
4811 | else | |
4812 | break; | |
4813 | ||
4814 | *adata++ = (unsigned char)ch; | |
4815 | } | |
4816 | ||
4817 | if (*valptr) | |
4818 | { | |
4819 | print_fatal_error(data, "Bad WITH-VALUE hex value."); | |
4820 | return (0); | |
4821 | } | |
4822 | } | |
4823 | else | |
4824 | { | |
4825 | /* | |
4826 | * Copy literal string value... | |
4827 | */ | |
4828 | ||
4829 | withlen = (int)strlen(value); | |
4830 | ||
4831 | memcpy(withdata, value, withlen); | |
4832 | } | |
4833 | ||
4834 | for (i = 0; i < count; i ++) | |
4835 | { | |
4836 | adata = ippGetOctetString(attr, i, &adatalen); | |
4837 | ||
4838 | if (withlen == adatalen && !memcmp(withdata, adata, (size_t)withlen)) | |
4839 | { | |
4840 | if (!matchbuf[0]) | |
4841 | copy_hex_string(matchbuf, adata, adatalen, matchlen); | |
4842 | ||
4843 | if (!(flags & _CUPS_WITH_ALL)) | |
4844 | { | |
4845 | match = 1; | |
4846 | break; | |
4847 | } | |
4848 | } | |
4849 | else if (flags & _CUPS_WITH_ALL) | |
4850 | { | |
4851 | match = 0; | |
4852 | break; | |
4853 | } | |
4854 | } | |
4855 | ||
4856 | if (!match && errors) | |
4857 | { | |
4858 | for (i = 0; i < count; i ++) | |
4859 | { | |
4860 | adata = ippGetOctetString(attr, i, &adatalen); | |
4861 | copy_hex_string(temp, adata, adatalen, sizeof(temp)); | |
4862 | add_stringf(data->errors, "GOT: %s=\"%s\"", name, temp); | |
4863 | } | |
4864 | } | |
4865 | } | |
4866 | break; | |
4867 | ||
f8b3a85b MS |
4868 | default : |
4869 | break; | |
4870 | } | |
4871 | ||
82cc1f9a | 4872 | return (match); |
f8b3a85b MS |
4873 | } |
4874 | ||
4875 | ||
a21c36fa MS |
4876 | /* |
4877 | * 'with_value_from()' - Test a WITH-VALUE-FROM predicate. | |
4878 | */ | |
4879 | ||
4880 | static int /* O - 1 on match, 0 on non-match */ | |
4881 | with_value_from( | |
4882 | cups_array_t *errors, /* I - Errors array */ | |
4883 | ipp_attribute_t *fromattr, /* I - "From" attribute */ | |
4884 | ipp_attribute_t *attr, /* I - Attribute to compare */ | |
4885 | char *matchbuf, /* I - Buffer to hold matching value */ | |
4886 | size_t matchlen) /* I - Length of match buffer */ | |
4887 | { | |
4888 | int i, j, /* Looping vars */ | |
4889 | count = ippGetCount(attr), /* Number of attribute values */ | |
4890 | match = 1; /* Match? */ | |
4891 | ||
4892 | ||
4893 | *matchbuf = '\0'; | |
4894 | ||
4895 | /* | |
4896 | * Compare the from value(s) to the attribute value(s)... | |
4897 | */ | |
4898 | ||
4899 | switch (ippGetValueTag(attr)) | |
4900 | { | |
4901 | case IPP_TAG_INTEGER : | |
4902 | if (ippGetValueTag(fromattr) != IPP_TAG_INTEGER && ippGetValueTag(fromattr) != IPP_TAG_RANGE) | |
4903 | goto wrong_value_tag; | |
4904 | ||
4905 | for (i = 0; i < count; i ++) | |
4906 | { | |
4907 | int value = ippGetInteger(attr, i); | |
4908 | /* Current integer value */ | |
4909 | ||
4910 | if (ippContainsInteger(fromattr, value)) | |
4911 | { | |
4912 | if (!matchbuf[0]) | |
4913 | snprintf(matchbuf, matchlen, "%d", value); | |
4914 | } | |
4915 | else | |
4916 | { | |
4917 | add_stringf(errors, "GOT: %s=%d", ippGetName(attr), value); | |
4918 | match = 0; | |
4919 | } | |
4920 | } | |
4921 | break; | |
4922 | ||
4923 | case IPP_TAG_ENUM : | |
4924 | if (ippGetValueTag(fromattr) != IPP_TAG_ENUM) | |
4925 | goto wrong_value_tag; | |
4926 | ||
4927 | for (i = 0; i < count; i ++) | |
4928 | { | |
4929 | int value = ippGetInteger(attr, i); | |
4930 | /* Current integer value */ | |
4931 | ||
4932 | if (ippContainsInteger(fromattr, value)) | |
4933 | { | |
4934 | if (!matchbuf[0]) | |
4935 | snprintf(matchbuf, matchlen, "%d", value); | |
4936 | } | |
4937 | else | |
4938 | { | |
4939 | add_stringf(errors, "GOT: %s=%d", ippGetName(attr), value); | |
4940 | match = 0; | |
4941 | } | |
4942 | } | |
4943 | break; | |
4944 | ||
4945 | case IPP_TAG_RESOLUTION : | |
4946 | if (ippGetValueTag(fromattr) != IPP_TAG_RESOLUTION) | |
4947 | goto wrong_value_tag; | |
4948 | ||
4949 | for (i = 0; i < count; i ++) | |
4950 | { | |
4951 | int xres, yres; | |
4952 | ipp_res_t units; | |
4953 | int fromcount = ippGetCount(fromattr); | |
4954 | int fromxres, fromyres; | |
4955 | ipp_res_t fromunits; | |
4956 | ||
4957 | xres = ippGetResolution(attr, i, &yres, &units); | |
4958 | ||
4959 | for (j = 0; j < fromcount; j ++) | |
4960 | { | |
4961 | fromxres = ippGetResolution(fromattr, j, &fromyres, &fromunits); | |
4962 | if (fromxres == xres && fromyres == yres && fromunits == units) | |
4963 | break; | |
4964 | } | |
4965 | ||
4966 | if (j < fromcount) | |
4967 | { | |
4968 | if (!matchbuf[0]) | |
4969 | { | |
4970 | if (xres == yres) | |
4971 | snprintf(matchbuf, matchlen, "%d%s", xres, units == IPP_RES_PER_INCH ? "dpi" : "dpcm"); | |
4972 | else | |
4973 | snprintf(matchbuf, matchlen, "%dx%d%s", xres, yres, units == IPP_RES_PER_INCH ? "dpi" : "dpcm"); | |
4974 | } | |
4975 | } | |
4976 | else | |
4977 | { | |
4978 | if (xres == yres) | |
4979 | add_stringf(errors, "GOT: %s=%d%s", ippGetName(attr), xres, units == IPP_RES_PER_INCH ? "dpi" : "dpcm"); | |
4980 | else | |
4981 | add_stringf(errors, "GOT: %s=%dx%d%s", ippGetName(attr), xres, yres, units == IPP_RES_PER_INCH ? "dpi" : "dpcm"); | |
4982 | ||
4983 | match = 0; | |
4984 | } | |
4985 | } | |
4986 | break; | |
4987 | ||
4988 | case IPP_TAG_NOVALUE : | |
4989 | case IPP_TAG_UNKNOWN : | |
4990 | return (1); | |
4991 | ||
4992 | case IPP_TAG_CHARSET : | |
4993 | case IPP_TAG_KEYWORD : | |
4994 | case IPP_TAG_LANGUAGE : | |
4995 | case IPP_TAG_MIMETYPE : | |
4996 | case IPP_TAG_NAME : | |
4997 | case IPP_TAG_NAMELANG : | |
4998 | case IPP_TAG_TEXT : | |
4999 | case IPP_TAG_TEXTLANG : | |
a21c36fa MS |
5000 | case IPP_TAG_URISCHEME : |
5001 | for (i = 0; i < count; i ++) | |
5002 | { | |
5003 | const char *value = ippGetString(attr, i, NULL); | |
5004 | /* Current string value */ | |
5005 | ||
5006 | if (ippContainsString(fromattr, value)) | |
5007 | { | |
5008 | if (!matchbuf[0]) | |
5009 | strlcpy(matchbuf, value, matchlen); | |
5010 | } | |
5011 | else | |
5012 | { | |
5013 | add_stringf(errors, "GOT: %s='%s'", ippGetName(attr), value); | |
5014 | match = 0; | |
5015 | } | |
5016 | } | |
5017 | break; | |
5018 | ||
ea4dcf9f MS |
5019 | case IPP_TAG_URI : |
5020 | for (i = 0; i < count; i ++) | |
5021 | { | |
5022 | const char *value = ippGetString(attr, i, NULL); | |
5023 | /* Current string value */ | |
5024 | int fromcount = ippGetCount(fromattr); | |
5025 | ||
5026 | for (j = 0; j < fromcount; j ++) | |
5027 | { | |
5028 | if (!compare_uris(value, ippGetString(fromattr, j, NULL))) | |
5029 | { | |
5030 | if (!matchbuf[0]) | |
5031 | strlcpy(matchbuf, value, matchlen); | |
5032 | break; | |
5033 | } | |
5034 | } | |
5035 | ||
5036 | if (j >= fromcount) | |
5037 | { | |
5038 | add_stringf(errors, "GOT: %s='%s'", ippGetName(attr), value); | |
5039 | match = 0; | |
5040 | } | |
5041 | } | |
5042 | break; | |
5043 | ||
a21c36fa MS |
5044 | default : |
5045 | match = 0; | |
5046 | break; | |
5047 | } | |
5048 | ||
5049 | return (match); | |
5050 | ||
5051 | /* value tag mismatch between fromattr and attr */ | |
5052 | wrong_value_tag : | |
5053 | ||
5054 | add_stringf(errors, "GOT: %s OF-TYPE %s", ippGetName(attr), ippTagString(ippGetValueTag(attr))); | |
5055 | ||
5056 | return (0); | |
5057 | } |