2 * ipptool command for CUPS.
4 * Copyright 2007-2017 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers...
20 #include <cups/cups-private.h>
21 #include <cups/file-private.h>
35 #endif /* !O_BINARY */
42 typedef enum _cups_transfer_e
/**** How to send request data ****/
44 _CUPS_TRANSFER_AUTO
, /* Chunk for files, length for static */
45 _CUPS_TRANSFER_CHUNKED
, /* Chunk always */
46 _CUPS_TRANSFER_LENGTH
/* Length always */
49 typedef enum _cups_output_e
/**** Output mode ****/
51 _CUPS_OUTPUT_QUIET
, /* No output */
52 _CUPS_OUTPUT_TEST
, /* Traditional CUPS test output */
53 _CUPS_OUTPUT_PLIST
, /* XML plist test output */
54 _CUPS_OUTPUT_LIST
, /* Tabular list output */
55 _CUPS_OUTPUT_CSV
/* Comma-separated values output */
58 typedef enum _cups_with_e
/**** WITH flags ****/
60 _CUPS_WITH_LITERAL
= 0, /* Match string is a literal value */
61 _CUPS_WITH_ALL
= 1, /* Must match all values */
62 _CUPS_WITH_REGEX
= 2, /* Match string is a regular expression */
63 _CUPS_WITH_HOSTNAME
= 4, /* Match string is a URI hostname */
64 _CUPS_WITH_RESOURCE
= 8, /* Match string is a URI resource */
65 _CUPS_WITH_SCHEME
= 16 /* Match string is a URI scheme */
68 typedef struct _cups_expect_s
/**** Expected attribute info ****/
70 int optional
, /* Optional attribute? */
71 not_expect
, /* Don't expect attribute? */
72 expect_all
; /* Expect all attributes to match/not match */
73 char *name
, /* Attribute name */
74 *of_type
, /* Type name */
75 *same_count_as
, /* Parallel attribute name */
76 *if_defined
, /* Only required if variable defined */
77 *if_not_defined
, /* Only required if variable is not defined */
78 *with_value
, /* Attribute must include this value */
79 *with_value_from
, /* Attribute must have one of the values in this attribute */
80 *define_match
, /* Variable to define on match */
81 *define_no_match
, /* Variable to define on no-match */
82 *define_value
; /* Variable to define with value */
83 int repeat_limit
, /* Maximum number of times to repeat */
84 repeat_match
, /* Repeat test on match */
85 repeat_no_match
, /* Repeat test on no match */
86 with_flags
, /* WITH flags */
87 count
; /* Expected count if > 0 */
88 ipp_tag_t in_group
; /* IN-GROUP value */
91 typedef struct _cups_status_s
/**** Status info ****/
93 ipp_status_t status
; /* Expected status code */
94 char *if_defined
, /* Only if variable is defined */
95 *if_not_defined
, /* Only if variable is not defined */
96 *define_match
, /* Variable to define on match */
97 *define_no_match
, /* Variable to define on no-match */
98 *define_value
; /* Variable to define with value */
99 int repeat_limit
, /* Maximum number of times to repeat */
100 repeat_match
, /* Repeat the test when it does not match */
101 repeat_no_match
; /* Repeat the test when it matches */
104 typedef struct _cups_var_s
/**** Variable ****/
106 char *name
, /* Name of variable */
107 *value
; /* Value of variable */
110 typedef struct _cups_vars_s
/**** Set of variables ****/
112 char *uri
, /* URI for printer */
113 *filename
, /* Filename */
114 scheme
[64], /* Scheme from URI */
115 userpass
[256], /* Username/password from URI */
116 hostname
[256], /* Hostname from URI */
117 resource
[1024]; /* Resource path from URI */
118 int port
; /* Port number from URI */
119 http_encryption_t encryption
; /* Encryption for connection? */
120 double timeout
; /* Timeout for connection */
121 int family
; /* Address family */
122 cups_array_t
*vars
; /* Array of variables */
130 static _cups_transfer_t Transfer
= _CUPS_TRANSFER_AUTO
;
131 /* How to transfer requests */
132 static _cups_output_t Output
= _CUPS_OUTPUT_LIST
;
134 static int Cancel
= 0, /* Cancel test? */
135 IgnoreErrors
= 0, /* Ignore errors? */
136 StopAfterIncludeError
= 0,
137 /* Stop after include errors? */
138 Verbosity
= 0, /* Show all attributes? */
139 Version
= 11, /* Default IPP version */
140 XMLHeader
= 0, /* 1 if header is written */
141 TestCount
= 0, /* Number of tests run */
142 PassCount
= 0, /* Number of passing tests */
143 FailCount
= 0, /* Number of failing tests */
144 SkipCount
= 0; /* Number of skipped tests */
145 static char *Username
= NULL
, /* Username from URI */
146 *Password
= NULL
; /* Password from URI */
147 static int PasswordTries
= 0; /* Number of tries with password */
154 static void add_stringf(cups_array_t
*a
, const char *s
, ...) __attribute__ ((__format__ (__printf__
, 2, 3)));
155 static int compare_uris(const char *a
, const char *b
);
156 static int compare_vars(_cups_var_t
*a
, _cups_var_t
*b
);
157 static int do_tests(cups_file_t
*outfile
, _cups_vars_t
*vars
, const char *testfile
);
158 static void expand_variables(_cups_vars_t
*vars
, char *dst
, const char *src
, size_t dstsize
) __attribute__((nonnull(1,2,3)));
159 static int expect_matches(_cups_expect_t
*expect
, ipp_tag_t value_tag
);
160 static ipp_t
*get_collection(cups_file_t
*outfile
, _cups_vars_t
*vars
, cups_file_t
*fp
, int *linenum
);
161 static char *get_filename(const char *testfile
, char *dst
, const char *src
, size_t dstsize
);
162 static const char *get_string(ipp_attribute_t
*attr
, int element
, int flags
, char *buffer
, size_t bufsize
);
163 static char *get_token(cups_file_t
*fp
, char *buf
, int buflen
, int *linenum
);
164 static char *get_variable(_cups_vars_t
*vars
, const char *name
);
165 static char *iso_date(ipp_uchar_t
*date
);
166 static const char *password_cb(const char *prompt
);
167 static void pause_message(const char *message
);
168 static void print_attr(cups_file_t
*outfile
, int format
, ipp_attribute_t
*attr
, ipp_tag_t
*group
);
169 static void print_csv(cups_file_t
*outfile
, ipp_attribute_t
*attr
, int num_displayed
, char **displayed
, size_t *widths
);
170 static void print_fatal_error(cups_file_t
*outfile
, const char *s
, ...) __attribute__ ((__format__ (__printf__
, 2, 3)));
171 static void print_line(cups_file_t
*outfile
, ipp_attribute_t
*attr
, int num_displayed
, char **displayed
, size_t *widths
);
172 static void print_xml_header(cups_file_t
*outfile
);
173 static void print_xml_string(cups_file_t
*outfile
, const char *element
, const char *s
);
174 static void print_xml_trailer(cups_file_t
*outfile
, int success
, const char *message
);
175 static void set_variable(cups_file_t
*outfile
, _cups_vars_t
*vars
, const char *name
, const char *value
);
177 static void sigterm_handler(int sig
);
179 static int timeout_cb(http_t
*http
, void *user_data
);
180 static void usage(void) __attribute__((noreturn
));
181 static int validate_attr(cups_file_t
*outfile
, cups_array_t
*errors
, ipp_attribute_t
*attr
);
182 static int with_value(cups_file_t
*outfile
, cups_array_t
*errors
, char *value
, int flags
, ipp_attribute_t
*attr
, char *matchbuf
, size_t matchlen
);
183 static int with_value_from(cups_array_t
*errors
, ipp_attribute_t
*fromattr
, ipp_attribute_t
*attr
, char *matchbuf
, size_t matchlen
);
187 * 'main()' - Parse options and do tests.
190 int /* O - Exit status */
191 main(int argc
, /* I - Number of command-line args */
192 char *argv
[]) /* I - Command-line arguments */
194 int i
; /* Looping var */
195 int status
; /* Status of tests... */
196 cups_file_t
*outfile
= cupsFileStdout();
198 char *opt
, /* Current option */
199 name
[1024], /* Name/value buffer */
200 *value
, /* Pointer to value */
201 filename
[1024], /* Real filename */
202 testname
[1024], /* Real test filename */
203 uri
[1024]; /* Copy of printer URI */
204 const char *ext
, /* Extension on filename */
205 *testfile
; /* Test file to use */
206 int interval
, /* Test interval in microseconds */
207 repeat
; /* Repeat count */
208 _cups_vars_t vars
; /* Variables */
209 http_uri_status_t uri_status
; /* URI separation status */
210 _cups_globals_t
*cg
= _cupsGlobals();
216 * Catch SIGINT and SIGTERM...
219 signal(SIGINT
, sigterm_handler
);
220 signal(SIGTERM
, sigterm_handler
);
224 * Initialize the locale and variables...
227 _cupsSetLocale(argv
);
229 memset(&vars
, 0, sizeof(vars
));
230 vars
.family
= AF_UNSPEC
;
231 vars
.vars
= cupsArrayNew((cups_array_func_t
)compare_vars
, NULL
);
236 * ipptool URI testfile
244 for (i
= 1; i
< argc
; i
++)
246 if (!strcmp(argv
[i
], "--help"))
250 else if (!strcmp(argv
[i
], "--stop-after-include-error"))
252 StopAfterIncludeError
= 1;
254 else if (!strcmp(argv
[i
], "--version"))
259 else if (argv
[i
][0] == '-')
261 for (opt
= argv
[i
] + 1; *opt
; opt
++)
265 case '4' : /* Connect using IPv4 only */
266 vars
.family
= AF_INET
;
270 case '6' : /* Connect using IPv6 only */
271 vars
.family
= AF_INET6
;
273 #endif /* AF_INET6 */
275 case 'C' : /* Enable HTTP chunking */
276 Transfer
= _CUPS_TRANSFER_CHUNKED
;
279 case 'E' : /* Encrypt with TLS */
281 vars
.encryption
= HTTP_ENCRYPT_REQUIRED
;
283 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."),
285 #endif /* HAVE_SSL */
288 case 'I' : /* Ignore errors */
292 case 'L' : /* Disable HTTP chunking */
293 Transfer
= _CUPS_TRANSFER_LENGTH
;
296 case 'P' : /* Output to plist file */
301 _cupsLangPrintf(stderr
, _("%s: Missing filename for \"-P\"."), "ipptool");
305 if (outfile
!= cupsFileStdout())
308 if ((outfile
= cupsFileOpen(argv
[i
], "w")) == NULL
)
310 _cupsLangPrintf(stderr
, _("%s: Unable to open \"%s\": %s"), "ipptool", argv
[i
], strerror(errno
));
314 Output
= _CUPS_OUTPUT_PLIST
;
316 if (interval
|| repeat
)
318 _cupsLangPuts(stderr
, _("ipptool: \"-i\" and \"-n\" are incompatible with \"-P\" and \"-X\"."));
323 case 'S' : /* Encrypt with SSL */
325 vars
.encryption
= HTTP_ENCRYPT_ALWAYS
;
327 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."),
329 #endif /* HAVE_SSL */
332 case 'T' : /* Set timeout */
337 _cupsLangPrintf(stderr
,
338 _("%s: Missing timeout for \"-T\"."),
343 vars
.timeout
= _cupsStrScand(argv
[i
], NULL
, localeconv());
346 case 'V' : /* Set IPP version */
351 _cupsLangPrintf(stderr
,
352 _("%s: Missing version for \"-V\"."),
357 if (!strcmp(argv
[i
], "1.0"))
359 else if (!strcmp(argv
[i
], "1.1"))
361 else if (!strcmp(argv
[i
], "2.0"))
363 else if (!strcmp(argv
[i
], "2.1"))
365 else if (!strcmp(argv
[i
], "2.2"))
369 _cupsLangPrintf(stderr
,
370 _("%s: Bad version %s for \"-V\"."),
376 case 'X' : /* Produce XML output */
377 Output
= _CUPS_OUTPUT_PLIST
;
379 if (interval
|| repeat
)
381 _cupsLangPuts(stderr
, _("ipptool: \"-i\" and \"-n\" are incompatible with \"-P\" and \"-X\"."));
386 case 'c' : /* CSV output */
387 Output
= _CUPS_OUTPUT_CSV
;
390 case 'd' : /* Define a variable */
395 _cupsLangPuts(stderr
,
396 _("ipptool: Missing name=value for \"-d\"."));
400 strlcpy(name
, argv
[i
], sizeof(name
));
401 if ((value
= strchr(name
, '=')) != NULL
)
404 value
= name
+ strlen(name
);
406 set_variable(outfile
, &vars
, name
, value
);
409 case 'f' : /* Set the default test filename */
414 _cupsLangPuts(stderr
,
415 _("ipptool: Missing filename for \"-f\"."));
422 vars
.filename
= NULL
;
425 if (access(argv
[i
], 0))
431 snprintf(filename
, sizeof(filename
), "%s.gz", argv
[i
]);
432 if (access(filename
, 0) && filename
[0] != '/'
434 && (!isalpha(filename
[0] & 255) || filename
[1] != ':')
438 snprintf(filename
, sizeof(filename
), "%s/ipptool/%s",
439 cg
->cups_datadir
, argv
[i
]);
440 if (access(filename
, 0))
442 snprintf(filename
, sizeof(filename
), "%s/ipptool/%s.gz",
443 cg
->cups_datadir
, argv
[i
]);
444 if (access(filename
, 0))
445 vars
.filename
= strdup(argv
[i
]);
447 vars
.filename
= strdup(filename
);
450 vars
.filename
= strdup(filename
);
453 vars
.filename
= strdup(filename
);
456 vars
.filename
= strdup(argv
[i
]);
458 if ((ext
= strrchr(vars
.filename
, '.')) != NULL
)
461 * Guess the MIME media type based on the extension...
464 if (!_cups_strcasecmp(ext
, ".gif"))
465 set_variable(outfile
, &vars
, "filetype", "image/gif");
466 else if (!_cups_strcasecmp(ext
, ".htm") ||
467 !_cups_strcasecmp(ext
, ".htm.gz") ||
468 !_cups_strcasecmp(ext
, ".html") ||
469 !_cups_strcasecmp(ext
, ".html.gz"))
470 set_variable(outfile
, &vars
, "filetype", "text/html");
471 else if (!_cups_strcasecmp(ext
, ".jpg") ||
472 !_cups_strcasecmp(ext
, ".jpeg"))
473 set_variable(outfile
, &vars
, "filetype", "image/jpeg");
474 else if (!_cups_strcasecmp(ext
, ".pcl") ||
475 !_cups_strcasecmp(ext
, ".pcl.gz"))
476 set_variable(outfile
, &vars
, "filetype", "application/vnd.hp-PCL");
477 else if (!_cups_strcasecmp(ext
, ".pdf"))
478 set_variable(outfile
, &vars
, "filetype", "application/pdf");
479 else if (!_cups_strcasecmp(ext
, ".png"))
480 set_variable(outfile
, &vars
, "filetype", "image/png");
481 else if (!_cups_strcasecmp(ext
, ".ps") ||
482 !_cups_strcasecmp(ext
, ".ps.gz"))
483 set_variable(outfile
, &vars
, "filetype", "application/postscript");
484 else if (!_cups_strcasecmp(ext
, ".pwg") ||
485 !_cups_strcasecmp(ext
, ".pwg.gz") ||
486 !_cups_strcasecmp(ext
, ".ras") ||
487 !_cups_strcasecmp(ext
, ".ras.gz"))
488 set_variable(outfile
, &vars
, "filetype", "image/pwg-raster");
489 else if (!_cups_strcasecmp(ext
, ".tif") ||
490 !_cups_strcasecmp(ext
, ".tiff"))
491 set_variable(outfile
, &vars
, "filetype", "image/tiff");
492 else if (!_cups_strcasecmp(ext
, ".txt") ||
493 !_cups_strcasecmp(ext
, ".txt.gz"))
494 set_variable(outfile
, &vars
, "filetype", "text/plain");
495 else if (!_cups_strcasecmp(ext
, ".urf") ||
496 !_cups_strcasecmp(ext
, ".urf.gz"))
497 set_variable(outfile
, &vars
, "filetype", "image/urf");
498 else if (!_cups_strcasecmp(ext
, ".xps"))
499 set_variable(outfile
, &vars
, "filetype", "application/openxps");
501 set_variable(outfile
, &vars
, "filetype", "application/octet-stream");
506 * Use the "auto-type" MIME media type...
509 set_variable(outfile
, &vars
, "filetype", "application/octet-stream");
513 case 'i' : /* Test every N seconds */
518 _cupsLangPuts(stderr
,
519 _("ipptool: Missing seconds for \"-i\"."));
524 interval
= (int)(_cupsStrScand(argv
[i
], NULL
, localeconv()) *
528 _cupsLangPuts(stderr
,
529 _("ipptool: Invalid seconds for \"-i\"."));
534 if (Output
== _CUPS_OUTPUT_PLIST
&& interval
)
536 _cupsLangPuts(stderr
, _("ipptool: \"-i\" and \"-n\" are incompatible with \"-P\" and \"-X\"."));
541 case 'l' : /* List as a table */
542 Output
= _CUPS_OUTPUT_LIST
;
545 case 'n' : /* Repeat count */
550 _cupsLangPuts(stderr
,
551 _("ipptool: Missing count for \"-n\"."));
555 repeat
= atoi(argv
[i
]);
557 if (Output
== _CUPS_OUTPUT_PLIST
&& repeat
)
559 _cupsLangPuts(stderr
, _("ipptool: \"-i\" and \"-n\" are incompatible with \"-P\" and \"-X\"."));
564 case 'q' : /* Be quiet */
565 Output
= _CUPS_OUTPUT_QUIET
;
568 case 't' : /* CUPS test output */
569 Output
= _CUPS_OUTPUT_TEST
;
572 case 'v' : /* Be verbose */
577 _cupsLangPrintf(stderr
, _("ipptool: Unknown option \"-%c\"."),
583 else if (!strncmp(argv
[i
], "ipp://", 6) || !strncmp(argv
[i
], "http://", 7)
585 || !strncmp(argv
[i
], "ipps://", 7)
586 || !strncmp(argv
[i
], "https://", 8)
587 #endif /* HAVE_SSL */
596 _cupsLangPuts(stderr
, _("ipptool: May only specify a single URI."));
601 if (!strncmp(argv
[i
], "ipps://", 7) || !strncmp(argv
[i
], "https://", 8))
602 vars
.encryption
= HTTP_ENCRYPT_ALWAYS
;
603 #endif /* HAVE_SSL */
606 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
, vars
.uri
,
607 vars
.scheme
, sizeof(vars
.scheme
),
608 vars
.userpass
, sizeof(vars
.userpass
),
609 vars
.hostname
, sizeof(vars
.hostname
),
611 vars
.resource
, sizeof(vars
.resource
));
613 if (uri_status
!= HTTP_URI_OK
)
615 _cupsLangPrintf(stderr
, _("ipptool: Bad URI - %s."), httpURIStatusString(uri_status
));
619 if (vars
.userpass
[0])
621 if ((Password
= strchr(vars
.userpass
, ':')) != NULL
)
624 Username
= vars
.userpass
;
625 cupsSetPasswordCB(password_cb
);
626 set_variable(outfile
, &vars
, "uriuser", vars
.userpass
);
629 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), vars
.scheme
, NULL
,
630 vars
.hostname
, vars
.port
, vars
.resource
);
641 _cupsLangPuts(stderr
, _("ipptool: URI required before test file."));
642 _cupsLangPuts(stderr
, argv
[i
]);
646 if (access(argv
[i
], 0) && argv
[i
][0] != '/'
648 && (!isalpha(argv
[i
][0] & 255) || argv
[i
][1] != ':')
652 snprintf(testname
, sizeof(testname
), "%s/ipptool/%s", cg
->cups_datadir
,
654 if (access(testname
, 0))
662 if (!do_tests(outfile
, &vars
, testfile
))
667 if (!vars
.uri
|| !testfile
)
671 * Loop if the interval is set...
674 if (Output
== _CUPS_OUTPUT_PLIST
)
675 print_xml_trailer(outfile
, !status
, NULL
);
676 else if (interval
> 0 && repeat
> 0)
680 usleep((useconds_t
)interval
);
681 do_tests(outfile
, &vars
, testfile
);
685 else if (interval
> 0)
689 usleep((useconds_t
)interval
);
690 do_tests(outfile
, &vars
, testfile
);
694 if ((Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
)) && TestCount
> 1)
697 * Show a summary report if there were multiple tests...
700 cupsFilePrintf(cupsFileStdout(), "\nSummary: %d tests, %d passed, %d failed, %d skipped\nScore: %d%%\n", TestCount
, PassCount
, FailCount
, SkipCount
, 100 * (PassCount
+ SkipCount
) / TestCount
);
703 cupsFileClose(outfile
);
714 * 'add_stringf()' - Add a formatted string to an array.
718 add_stringf(cups_array_t
*a
, /* I - Array */
719 const char *s
, /* I - Printf-style format string */
720 ...) /* I - Additional args as needed */
722 char buffer
[10240]; /* Format buffer */
723 va_list ap
; /* Argument pointer */
727 * Don't bother is the array is NULL...
734 * Format the message...
738 vsnprintf(buffer
, sizeof(buffer
), s
, ap
);
742 * Add it to the array...
745 cupsArrayAdd(a
, buffer
);
750 * 'compare_uris()' - Compare two URIs...
753 static int /* O - Result of comparison */
754 compare_uris(const char *a
, /* I - First URI */
755 const char *b
) /* I - Second URI */
757 char ascheme
[32], /* Components of first URI */
762 char bscheme
[32], /* Components of second URI */
767 char *ptr
; /* Pointer into string */
768 int result
; /* Result of comparison */
772 * Separate the URIs into their components...
775 if (httpSeparateURI(HTTP_URI_CODING_ALL
, a
, ascheme
, sizeof(ascheme
), auserpass
, sizeof(auserpass
), ahost
, sizeof(ahost
), &aport
, aresource
, sizeof(aresource
)) < HTTP_URI_STATUS_OK
)
778 if (httpSeparateURI(HTTP_URI_CODING_ALL
, b
, bscheme
, sizeof(bscheme
), buserpass
, sizeof(buserpass
), bhost
, sizeof(bhost
), &bport
, bresource
, sizeof(bresource
)) < HTTP_URI_STATUS_OK
)
782 * Strip trailing dots from the host components, if present...
785 if ((ptr
= ahost
+ strlen(ahost
) - 1) > ahost
&& *ptr
== '.')
788 if ((ptr
= bhost
+ strlen(bhost
) - 1) > bhost
&& *ptr
== '.')
792 * Compare each component...
795 if ((result
= _cups_strcasecmp(ascheme
, bscheme
)) != 0)
798 if ((result
= strcmp(auserpass
, buserpass
)) != 0)
801 if ((result
= _cups_strcasecmp(ahost
, bhost
)) != 0)
805 return (aport
- bport
);
807 if (!_cups_strcasecmp(ascheme
, "mailto") || !_cups_strcasecmp(ascheme
, "urn"))
808 return (_cups_strcasecmp(aresource
, bresource
));
810 return (strcmp(aresource
, bresource
));
815 * 'compare_vars()' - Compare two variables.
818 static int /* O - Result of comparison */
819 compare_vars(_cups_var_t
*a
, /* I - First variable */
820 _cups_var_t
*b
) /* I - Second variable */
822 return (_cups_strcasecmp(a
->name
, b
->name
));
827 * 'do_tests()' - Do tests as specified in the test file.
830 static int /* 1 = success, 0 = failure */
831 do_tests(cups_file_t
*outfile
, /* I - Output file */
832 _cups_vars_t
*vars
, /* I - Variables */
833 const char *testfile
) /* I - Test file to use */
835 int i
, /* Looping var */
836 linenum
, /* Current line number */
837 pass
, /* Did we pass the test? */
838 prev_pass
= 1, /* Did we pass the previous test? */
839 request_id
, /* Current request ID */
840 show_header
= 1, /* Show the test header? */
841 ignore_errors
, /* Ignore test failures? */
842 skip_previous
= 0, /* Skip on previous test failure? */
843 repeat_count
, /* Repeat count */
844 repeat_test
; /* Repeat a test? */
845 useconds_t delay
, /* Initial delay */
846 repeat_interval
; /* Repeat interval (delay) */
847 http_t
*http
= NULL
; /* HTTP connection to server */
848 cups_file_t
*fp
= NULL
; /* Test file */
849 char resource
[512], /* Resource for request */
850 token
[1024], /* Token from file */
851 *tokenptr
, /* Pointer into token */
852 temp
[1024], /* Temporary string */
853 buffer
[131072], /* Copy buffer */
854 compression
[16]; /* COMPRESSION value */
855 ipp_t
*request
= NULL
, /* IPP request */
856 *response
= NULL
; /* IPP response */
857 size_t length
; /* Length of IPP request */
858 http_status_t status
; /* HTTP status */
859 cups_file_t
*reqfile
; /* File to send */
860 ssize_t bytes
; /* Bytes read/written */
861 char attr
[128]; /* Attribute name */
862 ipp_op_t op
; /* Operation */
863 ipp_tag_t group
; /* Current group */
864 ipp_tag_t value
; /* Current value type */
865 ipp_attribute_t
*attrptr
, /* Attribute pointer */
866 *found
, /* Found attribute */
867 *lastcol
= NULL
; /* Last collection attribute */
868 char name
[1024], /* Name of test */
869 file_id
[1024], /* File identifier */
870 test_id
[1024]; /* Test identifier */
871 char filename
[1024]; /* Filename */
872 _cups_transfer_t transfer
; /* To chunk or not to chunk */
873 int version
, /* IPP version number to use */
874 skip_test
; /* Skip this test? */
875 int num_statuses
= 0; /* Number of valid status codes */
876 _cups_status_t statuses
[100], /* Valid status codes */
877 *last_status
; /* Last STATUS (for predicates) */
878 int status_ok
, /* Did we get a matching status? */
879 num_expects
= 0; /* Number of expected attributes */
880 _cups_expect_t expects
[200], /* Expected attributes */
881 *expect
, /* Current expected attribute */
882 *last_expect
; /* Last EXPECT (for predicates) */
883 int num_displayed
= 0; /* Number of displayed attributes */
884 char *displayed
[200]; /* Displayed attributes */
885 size_t widths
[200]; /* Width of columns */
886 cups_array_t
*a
, /* Duplicate attribute array */
887 *errors
= NULL
; /* Errors array */
888 const char *error
; /* Current error */
892 * Open the test file...
895 if ((fp
= cupsFileOpen(testfile
, "r")) == NULL
)
897 print_fatal_error(outfile
, "Unable to open test file %s - %s", testfile
,
904 * Connect to the server...
907 if ((http
= httpConnect2(vars
->hostname
, vars
->port
, NULL
, vars
->family
,
908 vars
->encryption
, 1, 30000, NULL
)) == NULL
)
910 print_fatal_error(outfile
, "Unable to connect to %s on port %d - %s", vars
->hostname
,
911 vars
->port
, cupsLastErrorString());
917 httpSetDefaultField(http
, HTTP_FIELD_ACCEPT_ENCODING
,
918 "deflate, gzip, identity");
920 httpSetDefaultField(http
, HTTP_FIELD_ACCEPT_ENCODING
, "identity");
921 #endif /* HAVE_LIBZ */
923 if (vars
->timeout
> 0.0)
924 httpSetTimeout(http
, vars
->timeout
, timeout_cb
, NULL
);
930 CUPS_SRAND((unsigned)time(NULL
));
932 errors
= cupsArrayNew3(NULL
, NULL
, NULL
, 0, (cups_acopy_func_t
)strdup
,
933 (cups_afree_func_t
)free
);
937 request_id
= (CUPS_RAND() % 1000) * 137 + 1;
939 while (!Cancel
&& get_token(fp
, token
, sizeof(token
), &linenum
) != NULL
)
942 * Expect an open brace...
945 if (!strcmp(token
, "DEFINE"))
951 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
952 get_token(fp
, temp
, sizeof(temp
), &linenum
))
954 expand_variables(vars
, token
, temp
, sizeof(token
));
955 set_variable(outfile
, vars
, attr
, token
);
959 print_fatal_error(outfile
, "Missing DEFINE name and/or value on line %d.",
967 else if (!strcmp(token
, "DEFINE-DEFAULT"))
970 * DEFINE-DEFAULT name value
973 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
974 get_token(fp
, temp
, sizeof(temp
), &linenum
))
976 expand_variables(vars
, token
, temp
, sizeof(token
));
977 if (!get_variable(vars
, attr
))
978 set_variable(outfile
, vars
, attr
, token
);
982 print_fatal_error(outfile
, "Missing DEFINE-DEFAULT name and/or value on line "
990 else if (!strcmp(token
, "FILE-ID"))
996 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
998 expand_variables(vars
, file_id
, temp
, sizeof(file_id
));
1002 print_fatal_error(outfile
, "Missing FILE-ID value on line %d.", linenum
);
1009 else if (!strcmp(token
, "IGNORE-ERRORS"))
1016 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
1017 (!_cups_strcasecmp(temp
, "yes") || !_cups_strcasecmp(temp
, "no")))
1019 IgnoreErrors
= !_cups_strcasecmp(temp
, "yes");
1023 print_fatal_error(outfile
, "Missing IGNORE-ERRORS value on line %d.", linenum
);
1030 else if (!strcmp(token
, "INCLUDE"))
1033 * INCLUDE "filename"
1034 * INCLUDE <filename>
1037 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1040 * Map the filename to and then run the tests...
1043 if (!do_tests(outfile
, vars
, get_filename(testfile
, filename
, temp
, sizeof(filename
))))
1047 if (StopAfterIncludeError
)
1053 print_fatal_error(outfile
, "Missing INCLUDE filename on line %d.", linenum
);
1061 else if (!strcmp(token
, "INCLUDE-IF-DEFINED"))
1064 * INCLUDE-IF-DEFINED name "filename"
1065 * INCLUDE-IF-DEFINED name <filename>
1068 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
1069 get_token(fp
, temp
, sizeof(temp
), &linenum
))
1072 * Map the filename to and then run the tests...
1075 if (get_variable(vars
, attr
) &&
1076 !do_tests(outfile
, vars
, get_filename(testfile
, filename
, temp
, sizeof(filename
))))
1080 if (StopAfterIncludeError
)
1086 print_fatal_error(outfile
, "Missing INCLUDE-IF-DEFINED name or filename on line "
1095 else if (!strcmp(token
, "INCLUDE-IF-NOT-DEFINED"))
1098 * INCLUDE-IF-NOT-DEFINED name "filename"
1099 * INCLUDE-IF-NOT-DEFINED name <filename>
1102 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
1103 get_token(fp
, temp
, sizeof(temp
), &linenum
))
1106 * Map the filename to and then run the tests...
1109 if (!get_variable(vars
, attr
) &&
1110 !do_tests(outfile
, vars
, get_filename(testfile
, filename
, temp
, sizeof(filename
))))
1114 if (StopAfterIncludeError
)
1120 print_fatal_error(outfile
, "Missing INCLUDE-IF-NOT-DEFINED name or filename on "
1121 "line %d.", linenum
);
1129 else if (!strcmp(token
, "SKIP-IF-DEFINED"))
1132 * SKIP-IF-DEFINED variable
1135 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1137 if (get_variable(vars
, temp
))
1142 print_fatal_error(outfile
, "Missing SKIP-IF-DEFINED variable on line %d.",
1148 else if (!strcmp(token
, "SKIP-IF-NOT-DEFINED"))
1151 * SKIP-IF-NOT-DEFINED variable
1154 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1156 if (!get_variable(vars
, temp
))
1161 print_fatal_error(outfile
, "Missing SKIP-IF-NOT-DEFINED variable on line %d.",
1167 else if (!strcmp(token
, "STOP-AFTER-INCLUDE-ERROR"))
1170 * STOP-AFTER-INCLUDE-ERROR yes
1171 * STOP-AFTER-INCLUDE-ERROR no
1174 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
1175 (!_cups_strcasecmp(temp
, "yes") || !_cups_strcasecmp(temp
, "no")))
1177 StopAfterIncludeError
= !_cups_strcasecmp(temp
, "yes");
1181 print_fatal_error(outfile
, "Missing STOP-AFTER-INCLUDE-ERROR value on line %d.",
1189 else if (!strcmp(token
, "TRANSFER"))
1197 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1199 if (!strcmp(temp
, "auto"))
1200 Transfer
= _CUPS_TRANSFER_AUTO
;
1201 else if (!strcmp(temp
, "chunked"))
1202 Transfer
= _CUPS_TRANSFER_CHUNKED
;
1203 else if (!strcmp(temp
, "length"))
1204 Transfer
= _CUPS_TRANSFER_LENGTH
;
1207 print_fatal_error(outfile
, "Bad TRANSFER value \"%s\" on line %d.", temp
,
1215 print_fatal_error(outfile
, "Missing TRANSFER value on line %d.", linenum
);
1222 else if (!strcmp(token
, "VERSION"))
1224 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1226 if (!strcmp(temp
, "1.0"))
1228 else if (!strcmp(temp
, "1.1"))
1230 else if (!strcmp(temp
, "2.0"))
1232 else if (!strcmp(temp
, "2.1"))
1234 else if (!strcmp(temp
, "2.2"))
1238 print_fatal_error(outfile
, "Bad VERSION \"%s\" on line %d.", temp
, linenum
);
1245 print_fatal_error(outfile
, "Missing VERSION number on line %d.", linenum
);
1252 else if (strcmp(token
, "{"))
1254 print_fatal_error(outfile
, "Unexpected token %s seen on line %d.", token
, linenum
);
1260 * Initialize things...
1265 if (Output
== _CUPS_OUTPUT_PLIST
)
1266 print_xml_header(outfile
);
1267 if (Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
!= cupsFileStdout()))
1268 cupsFilePrintf(cupsFileStdout(), "\"%s\":\n", testfile
);
1273 strlcpy(resource
, vars
->resource
, sizeof(resource
));
1278 group
= IPP_TAG_ZERO
;
1279 ignore_errors
= IgnoreErrors
;
1287 transfer
= Transfer
;
1288 compression
[0] = '\0';
1291 repeat_interval
= 5000000;
1293 strlcpy(name
, testfile
, sizeof(name
));
1294 if (strrchr(name
, '.') != NULL
)
1295 *strrchr(name
, '.') = '\0';
1298 * Parse until we see a close brace...
1301 while (get_token(fp
, token
, sizeof(token
), &linenum
) != NULL
)
1303 if (_cups_strcasecmp(token
, "COUNT") &&
1304 _cups_strcasecmp(token
, "DEFINE-MATCH") &&
1305 _cups_strcasecmp(token
, "DEFINE-NO-MATCH") &&
1306 _cups_strcasecmp(token
, "DEFINE-VALUE") &&
1307 _cups_strcasecmp(token
, "IF-DEFINED") &&
1308 _cups_strcasecmp(token
, "IF-NOT-DEFINED") &&
1309 _cups_strcasecmp(token
, "IN-GROUP") &&
1310 _cups_strcasecmp(token
, "OF-TYPE") &&
1311 _cups_strcasecmp(token
, "REPEAT-LIMIT") &&
1312 _cups_strcasecmp(token
, "REPEAT-MATCH") &&
1313 _cups_strcasecmp(token
, "REPEAT-NO-MATCH") &&
1314 _cups_strcasecmp(token
, "SAME-COUNT-AS") &&
1315 _cups_strcasecmp(token
, "WITH-ALL-VALUES") &&
1316 _cups_strcasecmp(token
, "WITH-ALL-HOSTNAMES") &&
1317 _cups_strcasecmp(token
, "WITH-ALL-RESOURCES") &&
1318 _cups_strcasecmp(token
, "WITH-ALL-SCHEMES") &&
1319 _cups_strcasecmp(token
, "WITH-HOSTNAME") &&
1320 _cups_strcasecmp(token
, "WITH-RESOURCE") &&
1321 _cups_strcasecmp(token
, "WITH-SCHEME") &&
1322 _cups_strcasecmp(token
, "WITH-VALUE") &&
1323 _cups_strcasecmp(token
, "WITH-VALUE-FROM"))
1326 if (_cups_strcasecmp(token
, "DEFINE-MATCH") &&
1327 _cups_strcasecmp(token
, "DEFINE-NO-MATCH") &&
1328 _cups_strcasecmp(token
, "IF-DEFINED") &&
1329 _cups_strcasecmp(token
, "IF-NOT-DEFINED") &&
1330 _cups_strcasecmp(token
, "REPEAT-LIMIT") &&
1331 _cups_strcasecmp(token
, "REPEAT-MATCH") &&
1332 _cups_strcasecmp(token
, "REPEAT-NO-MATCH"))
1335 if (!strcmp(token
, "}"))
1337 else if (!strcmp(token
, "{") && lastcol
)
1340 * Another collection value
1343 ipp_t
*col
= get_collection(outfile
, vars
, fp
, &linenum
);
1344 /* Collection value */
1348 ippSetCollection(request
, &lastcol
, ippGetCount(lastcol
), col
);
1356 else if (!strcmp(token
, "COMPRESSION"))
1360 * COMPRESSION deflate
1364 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1366 expand_variables(vars
, compression
, temp
, sizeof(compression
));
1368 if (strcmp(compression
, "none") && strcmp(compression
, "deflate") &&
1369 strcmp(compression
, "gzip"))
1371 if (strcmp(compression
, "none"))
1372 #endif /* HAVE_LIBZ */
1374 print_fatal_error(outfile
, "Unsupported COMPRESSION value '%s' on line %d.",
1375 compression
, linenum
);
1380 if (!strcmp(compression
, "none"))
1381 compression
[0] = '\0';
1385 print_fatal_error(outfile
, "Missing COMPRESSION value on line %d.", linenum
);
1390 else if (!strcmp(token
, "DEFINE"))
1396 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
1397 get_token(fp
, temp
, sizeof(temp
), &linenum
))
1399 expand_variables(vars
, token
, temp
, sizeof(token
));
1400 set_variable(outfile
, vars
, attr
, token
);
1404 print_fatal_error(outfile
, "Missing DEFINE name and/or value on line %d.",
1410 else if (!strcmp(token
, "IGNORE-ERRORS"))
1417 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
1418 (!_cups_strcasecmp(temp
, "yes") || !_cups_strcasecmp(temp
, "no")))
1420 ignore_errors
= !_cups_strcasecmp(temp
, "yes");
1424 print_fatal_error(outfile
, "Missing IGNORE-ERRORS value on line %d.", linenum
);
1431 else if (!_cups_strcasecmp(token
, "NAME"))
1437 get_token(fp
, temp
, sizeof(temp
), &linenum
);
1438 expand_variables(vars
, name
, temp
, sizeof(name
));
1440 else if (!_cups_strcasecmp(token
, "PAUSE"))
1443 * Pause with a message...
1446 get_token(fp
, token
, sizeof(token
), &linenum
);
1447 pause_message(token
);
1449 else if (!strcmp(token
, "REQUEST-ID"))
1456 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1458 if (isdigit(temp
[0] & 255))
1459 request_id
= atoi(temp
);
1460 else if (!_cups_strcasecmp(temp
, "random"))
1461 request_id
= (CUPS_RAND() % 1000) * 137 + 1;
1464 print_fatal_error(outfile
, "Bad REQUEST-ID value \"%s\" on line %d.", temp
,
1472 print_fatal_error(outfile
, "Missing REQUEST-ID value on line %d.", linenum
);
1477 else if (!strcmp(token
, "SKIP-IF-DEFINED"))
1480 * SKIP-IF-DEFINED variable
1483 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1485 if (get_variable(vars
, temp
))
1490 print_fatal_error(outfile
, "Missing SKIP-IF-DEFINED value on line %d.",
1496 else if (!strcmp(token
, "SKIP-IF-MISSING"))
1499 * SKIP-IF-MISSING filename
1502 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1504 expand_variables(vars
, token
, temp
, sizeof(token
));
1505 get_filename(testfile
, filename
, token
, sizeof(filename
));
1507 if (access(filename
, R_OK
))
1512 print_fatal_error(outfile
, "Missing SKIP-IF-MISSING filename on line %d.",
1518 else if (!strcmp(token
, "SKIP-IF-NOT-DEFINED"))
1521 * SKIP-IF-NOT-DEFINED variable
1524 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1526 if (!get_variable(vars
, temp
))
1531 print_fatal_error(outfile
, "Missing SKIP-IF-NOT-DEFINED value on line %d.",
1537 else if (!strcmp(token
, "SKIP-PREVIOUS-ERROR"))
1540 * SKIP-PREVIOUS-ERROR yes
1541 * SKIP-PREVIOUS-ERROR no
1544 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
1545 (!_cups_strcasecmp(temp
, "yes") || !_cups_strcasecmp(temp
, "no")))
1547 skip_previous
= !_cups_strcasecmp(temp
, "yes");
1551 print_fatal_error(outfile
, "Missing SKIP-PREVIOUS-ERROR value on line %d.", linenum
);
1558 else if (!strcmp(token
, "TEST-ID"))
1564 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1566 expand_variables(vars
, test_id
, temp
, sizeof(test_id
));
1570 print_fatal_error(outfile
, "Missing TEST-ID value on line %d.", linenum
);
1577 else if (!strcmp(token
, "TRANSFER"))
1585 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1587 if (!strcmp(temp
, "auto"))
1588 transfer
= _CUPS_TRANSFER_AUTO
;
1589 else if (!strcmp(temp
, "chunked"))
1590 transfer
= _CUPS_TRANSFER_CHUNKED
;
1591 else if (!strcmp(temp
, "length"))
1592 transfer
= _CUPS_TRANSFER_LENGTH
;
1595 print_fatal_error(outfile
, "Bad TRANSFER value \"%s\" on line %d.", temp
,
1603 print_fatal_error(outfile
, "Missing TRANSFER value on line %d.", linenum
);
1608 else if (!_cups_strcasecmp(token
, "VERSION"))
1610 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1612 if (!strcmp(temp
, "0.0"))
1614 else if (!strcmp(temp
, "1.0"))
1616 else if (!strcmp(temp
, "1.1"))
1618 else if (!strcmp(temp
, "2.0"))
1620 else if (!strcmp(temp
, "2.1"))
1622 else if (!strcmp(temp
, "2.2"))
1626 print_fatal_error(outfile
, "Bad VERSION \"%s\" on line %d.", temp
, linenum
);
1633 print_fatal_error(outfile
, "Missing VERSION number on line %d.", linenum
);
1638 else if (!_cups_strcasecmp(token
, "RESOURCE"))
1644 if (!get_token(fp
, resource
, sizeof(resource
), &linenum
))
1646 print_fatal_error(outfile
, "Missing RESOURCE path on line %d.", linenum
);
1651 else if (!_cups_strcasecmp(token
, "OPERATION"))
1657 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1659 print_fatal_error(outfile
, "Missing OPERATION code on line %d.", linenum
);
1664 expand_variables(vars
, token
, temp
, sizeof(token
));
1666 if ((op
= ippOpValue(token
)) == (ipp_op_t
)-1 &&
1667 (op
= (ipp_op_t
)strtol(token
, NULL
, 0)) == 0)
1669 print_fatal_error(outfile
, "Bad OPERATION code \"%s\" on line %d.", token
,
1675 else if (!_cups_strcasecmp(token
, "GROUP"))
1678 * Attribute group...
1681 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1683 print_fatal_error(outfile
, "Missing GROUP tag on line %d.", linenum
);
1688 if ((value
= ippTagValue(token
)) == IPP_TAG_ZERO
|| value
>= IPP_TAG_UNSUPPORTED_VALUE
)
1690 print_fatal_error(outfile
, "Bad GROUP tag \"%s\" on line %d.", token
, linenum
);
1696 ippAddSeparator(request
);
1700 else if (!_cups_strcasecmp(token
, "DELAY"))
1703 * Delay before operation...
1706 double dval
; /* Delay value */
1708 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1710 print_fatal_error(outfile
, "Missing DELAY value on line %d.", linenum
);
1715 expand_variables(vars
, token
, temp
, sizeof(token
));
1717 if ((dval
= _cupsStrScand(token
, &tokenptr
, localeconv())) <= 0.0 || (*tokenptr
&& *tokenptr
!= ','))
1719 print_fatal_error(outfile
, "Bad DELAY value \"%s\" on line %d.", token
,
1725 delay
= (useconds_t
)(1000000.0 * dval
);
1727 if (*tokenptr
== ',')
1729 if ((dval
= _cupsStrScand(tokenptr
+ 1, &tokenptr
, localeconv())) <= 0.0 || *tokenptr
)
1731 print_fatal_error(outfile
, "Bad DELAY value \"%s\" on line %d.", token
,
1737 repeat_interval
= (useconds_t
)(1000000.0 * dval
);
1740 repeat_interval
= delay
;
1742 else if (!_cups_strcasecmp(token
, "ATTR"))
1748 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1750 print_fatal_error(outfile
, "Missing ATTR value tag on line %d.", linenum
);
1755 if ((value
= ippTagValue(token
)) < IPP_TAG_UNSUPPORTED_VALUE
)
1757 print_fatal_error(outfile
, "Bad ATTR value tag \"%s\" on line %d.", token
,
1763 if (!get_token(fp
, attr
, sizeof(attr
), &linenum
))
1765 print_fatal_error(outfile
, "Missing ATTR name on line %d.", linenum
);
1770 if (value
< IPP_TAG_INTEGER
)
1773 * Add out-of-band value - no value string needed...
1778 else if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1780 print_fatal_error(outfile
, "Missing ATTR value on line %d.", linenum
);
1786 expand_variables(vars
, token
, temp
, sizeof(token
));
1794 if (value
< IPP_TAG_INTEGER
)
1797 * Add out-of-band value...
1800 attrptr
= ippAddOutOfBand(request
, group
, value
, attr
);
1804 print_fatal_error(outfile
, "Unsupported ATTR value tag %s for \"%s\" on line %d.", ippTagString(value
), attr
, linenum
);
1810 case IPP_TAG_BOOLEAN
:
1811 if (!_cups_strcasecmp(token
, "true"))
1812 attrptr
= ippAddBoolean(request
, group
, attr
, 1);
1814 attrptr
= ippAddBoolean(request
, group
, attr
, (char)atoi(token
));
1817 case IPP_TAG_INTEGER
:
1819 if (!strchr(token
, ','))
1821 if (isdigit(token
[0] & 255) || token
[0] == '-')
1822 attrptr
= ippAddInteger(request
, group
, value
, attr
, (int)strtol(token
, &tokenptr
, 0));
1826 if ((i
= ippEnumValue(attr
, tokenptr
)) >= 0)
1828 attrptr
= ippAddInteger(request
, group
, value
, attr
, i
);
1829 tokenptr
+= strlen(tokenptr
);
1835 int values
[100], /* Values */
1836 num_values
= 1; /* Number of values */
1838 if (!isdigit(token
[0] & 255) && token
[0] != '-' && value
== IPP_TAG_ENUM
)
1840 char *ptr
; /* Pointer to next terminator */
1842 if ((ptr
= strchr(token
, ',')) != NULL
)
1845 ptr
= token
+ strlen(token
);
1847 if ((i
= ippEnumValue(attr
, token
)) < 0)
1853 i
= (int)strtol(token
, &tokenptr
, 0);
1857 while (tokenptr
&& *tokenptr
&&
1858 num_values
< (int)(sizeof(values
) / sizeof(values
[0])))
1860 if (*tokenptr
== ',')
1863 if (!isdigit(*tokenptr
& 255) && *tokenptr
!= '-')
1865 char *ptr
; /* Pointer to next terminator */
1867 if (value
!= IPP_TAG_ENUM
)
1870 if ((ptr
= strchr(tokenptr
, ',')) != NULL
)
1873 ptr
= tokenptr
+ strlen(tokenptr
);
1875 if ((i
= ippEnumValue(attr
, tokenptr
)) < 0)
1881 i
= (int)strtol(tokenptr
, &tokenptr
, 0);
1883 values
[num_values
++] = i
;
1886 attrptr
= ippAddIntegers(request
, group
, value
, attr
, num_values
, values
);
1889 if ((!token
[0] || !tokenptr
|| *tokenptr
) && !skip_test
)
1891 print_fatal_error(outfile
, "Bad %s value \'%s\' for \"%s\" on line %d.",
1892 ippTagString(value
), token
, attr
, linenum
);
1898 case IPP_TAG_RESOLUTION
:
1900 int xres
, /* X resolution */
1901 yres
; /* Y resolution */
1902 char *ptr
; /* Pointer into value */
1904 xres
= yres
= (int)strtol(token
, (char **)&ptr
, 10);
1905 if (ptr
> token
&& xres
> 0)
1908 yres
= (int)strtol(ptr
+ 1, (char **)&ptr
, 10);
1911 if (ptr
<= token
|| xres
<= 0 || yres
<= 0 || !ptr
||
1912 (_cups_strcasecmp(ptr
, "dpi") &&
1913 _cups_strcasecmp(ptr
, "dpc") &&
1914 _cups_strcasecmp(ptr
, "dpcm") &&
1915 _cups_strcasecmp(ptr
, "other")))
1920 print_fatal_error(outfile
, "Bad resolution value \'%s\' for \"%s\" on line %d.", token
, attr
, linenum
);
1925 if (!_cups_strcasecmp(ptr
, "dpi"))
1926 attrptr
= ippAddResolution(request
, group
, attr
, IPP_RES_PER_INCH
, xres
, yres
);
1927 else if (!_cups_strcasecmp(ptr
, "dpc") ||
1928 !_cups_strcasecmp(ptr
, "dpcm"))
1929 attrptr
= ippAddResolution(request
, group
, attr
, IPP_RES_PER_CM
, xres
, yres
);
1931 attrptr
= ippAddResolution(request
, group
, attr
, (ipp_res_t
)0, xres
, yres
);
1935 case IPP_TAG_RANGE
:
1937 int lowers
[4], /* Lower value */
1938 uppers
[4], /* Upper values */
1939 num_vals
; /* Number of values */
1942 num_vals
= sscanf(token
, "%d-%d,%d-%d,%d-%d,%d-%d",
1943 lowers
+ 0, uppers
+ 0,
1944 lowers
+ 1, uppers
+ 1,
1945 lowers
+ 2, uppers
+ 2,
1946 lowers
+ 3, uppers
+ 3);
1948 if ((num_vals
& 1) || num_vals
== 0)
1953 print_fatal_error(outfile
, "Bad rangeOfInteger value \'%s\' for \"%s\" on line %d.", token
, attr
, linenum
);
1958 attrptr
= ippAddRanges(request
, group
, attr
, num_vals
/ 2, lowers
,
1963 case IPP_TAG_BEGIN_COLLECTION
:
1964 if (!strcmp(token
, "{"))
1966 ipp_t
*col
= get_collection(outfile
, vars
, fp
, &linenum
);
1967 /* Collection value */
1971 attrptr
= lastcol
= ippAddCollection(request
, group
, attr
, col
);
1984 print_fatal_error(outfile
, "Bad ATTR collection value for \"%s\" on line %d.", attr
, linenum
);
1991 ipp_t
*col
; /* Collection value */
1992 off_t savepos
= cupsFileTell(fp
); /* Save position of file */
1993 int savelinenum
= linenum
; /* Save line number */
1995 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1998 if (strcmp(token
, ","))
2000 cupsFileSeek(fp
, savepos
);
2001 linenum
= savelinenum
;
2005 if (!get_token(fp
, token
, sizeof(token
), &linenum
) || strcmp(token
, "{"))
2007 print_fatal_error(outfile
, "Unexpected \"%s\" on line %d.", token
, linenum
);
2013 if ((col
= get_collection(outfile
, vars
, fp
, &linenum
)) == NULL
)
2016 ippSetCollection(request
, &attrptr
, ippGetCount(attrptr
), col
);
2019 while (!strcmp(token
, "{"));
2022 case IPP_TAG_STRING
:
2023 attrptr
= ippAddOctetString(request
, group
, attr
, token
, (int)strlen(token
));
2026 case IPP_TAG_TEXTLANG
:
2027 case IPP_TAG_NAMELANG
:
2030 case IPP_TAG_KEYWORD
:
2032 case IPP_TAG_URISCHEME
:
2033 case IPP_TAG_CHARSET
:
2034 case IPP_TAG_LANGUAGE
:
2035 case IPP_TAG_MIMETYPE
:
2036 if (!strchr(token
, ','))
2037 attrptr
= ippAddString(request
, group
, value
, attr
, NULL
, token
);
2041 * Multiple string values...
2044 int num_values
; /* Number of values */
2045 char *values
[100], /* Values */
2046 *ptr
; /* Pointer to next value */
2052 for (ptr
= strchr(token
, ','); ptr
; ptr
= strchr(ptr
, ','))
2054 if (ptr
> token
&& ptr
[-1] == '\\')
2055 _cups_strcpy(ptr
- 1, ptr
);
2059 values
[num_values
] = ptr
;
2064 attrptr
= ippAddStrings(request
, group
, value
, attr
, num_values
,
2065 NULL
, (const char **)values
);
2070 if (!attrptr
&& !skip_test
)
2072 print_fatal_error(outfile
, "Unable to add attribute \"%s\" on line %d.", attr
, linenum
);
2077 else if (!_cups_strcasecmp(token
, "FILE"))
2083 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
2085 print_fatal_error(outfile
, "Missing FILE filename on line %d.", linenum
);
2090 expand_variables(vars
, token
, temp
, sizeof(token
));
2091 get_filename(testfile
, filename
, token
, sizeof(filename
));
2093 if (access(filename
, R_OK
))
2095 print_fatal_error(outfile
, "Filename \"%s\" on line %d cannot be read.",
2097 print_fatal_error(outfile
, "Filename mapped to \"%s\".", filename
);
2102 else if (!_cups_strcasecmp(token
, "STATUS"))
2108 if (num_statuses
>= (int)(sizeof(statuses
) / sizeof(statuses
[0])))
2110 print_fatal_error(outfile
, "Too many STATUS's on line %d.", linenum
);
2115 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2117 print_fatal_error(outfile
, "Missing STATUS code on line %d.", linenum
);
2122 if ((statuses
[num_statuses
].status
= ippErrorValue(token
))
2123 == (ipp_status_t
)-1 &&
2124 (statuses
[num_statuses
].status
= (ipp_status_t
)strtol(token
, NULL
, 0)) == 0)
2126 print_fatal_error(outfile
, "Bad STATUS code \"%s\" on line %d.", token
,
2132 last_status
= statuses
+ num_statuses
;
2135 last_status
->define_match
= NULL
;
2136 last_status
->define_no_match
= NULL
;
2137 last_status
->if_defined
= NULL
;
2138 last_status
->if_not_defined
= NULL
;
2139 last_status
->repeat_limit
= 1000;
2140 last_status
->repeat_match
= 0;
2141 last_status
->repeat_no_match
= 0;
2143 else if (!_cups_strcasecmp(token
, "EXPECT") || !_cups_strcasecmp(token
, "EXPECT-ALL"))
2146 * Expected attributes...
2149 int expect_all
= !_cups_strcasecmp(token
, "EXPECT-ALL");
2151 if (num_expects
>= (int)(sizeof(expects
) / sizeof(expects
[0])))
2153 print_fatal_error(outfile
, "Too many EXPECT's on line %d.", linenum
);
2158 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2160 print_fatal_error(outfile
, "Missing EXPECT name on line %d.", linenum
);
2165 last_expect
= expects
+ num_expects
;
2168 memset(last_expect
, 0, sizeof(_cups_expect_t
));
2169 last_expect
->repeat_limit
= 1000;
2170 last_expect
->expect_all
= expect_all
;
2172 if (token
[0] == '!')
2174 last_expect
->not_expect
= 1;
2175 last_expect
->name
= strdup(token
+ 1);
2177 else if (token
[0] == '?')
2179 last_expect
->optional
= 1;
2180 last_expect
->name
= strdup(token
+ 1);
2183 last_expect
->name
= strdup(token
);
2185 else if (!_cups_strcasecmp(token
, "COUNT"))
2187 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2189 print_fatal_error(outfile
, "Missing COUNT number on line %d.", linenum
);
2194 if ((i
= atoi(token
)) <= 0)
2196 print_fatal_error(outfile
, "Bad COUNT \"%s\" on line %d.", token
, linenum
);
2202 last_expect
->count
= i
;
2205 print_fatal_error(outfile
, "COUNT without a preceding EXPECT on line %d.",
2211 else if (!_cups_strcasecmp(token
, "DEFINE-MATCH"))
2213 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2215 print_fatal_error(outfile
, "Missing DEFINE-MATCH variable on line %d.",
2222 last_expect
->define_match
= strdup(token
);
2223 else if (last_status
)
2224 last_status
->define_match
= strdup(token
);
2227 print_fatal_error(outfile
, "DEFINE-MATCH without a preceding EXPECT or STATUS "
2228 "on line %d.", linenum
);
2233 else if (!_cups_strcasecmp(token
, "DEFINE-NO-MATCH"))
2235 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2237 print_fatal_error(outfile
, "Missing DEFINE-NO-MATCH variable on line %d.",
2244 last_expect
->define_no_match
= strdup(token
);
2245 else if (last_status
)
2246 last_status
->define_no_match
= strdup(token
);
2249 print_fatal_error(outfile
, "DEFINE-NO-MATCH without a preceding EXPECT or "
2250 "STATUS on line %d.", linenum
);
2255 else if (!_cups_strcasecmp(token
, "DEFINE-VALUE"))
2257 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2259 print_fatal_error(outfile
, "Missing DEFINE-VALUE variable on line %d.",
2266 last_expect
->define_value
= strdup(token
);
2269 print_fatal_error(outfile
, "DEFINE-VALUE without a preceding EXPECT on "
2270 "line %d.", linenum
);
2275 else if (!_cups_strcasecmp(token
, "OF-TYPE"))
2277 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2279 print_fatal_error(outfile
, "Missing OF-TYPE value tag(s) on line %d.",
2286 last_expect
->of_type
= strdup(token
);
2289 print_fatal_error(outfile
, "OF-TYPE without a preceding EXPECT on line %d.",
2295 else if (!_cups_strcasecmp(token
, "IN-GROUP"))
2297 ipp_tag_t in_group
; /* IN-GROUP value */
2300 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2302 print_fatal_error(outfile
, "Missing IN-GROUP group tag on line %d.", linenum
);
2307 if ((in_group
= ippTagValue(token
)) == IPP_TAG_ZERO
|| in_group
>= IPP_TAG_UNSUPPORTED_VALUE
)
2309 print_fatal_error(outfile
, "Bad IN-GROUP group tag \"%s\" on line %d.", token
, linenum
);
2313 else if (last_expect
)
2314 last_expect
->in_group
= in_group
;
2317 print_fatal_error(outfile
, "IN-GROUP without a preceding EXPECT on line %d.", linenum
);
2322 else if (!_cups_strcasecmp(token
, "REPEAT-LIMIT"))
2324 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2326 print_fatal_error(outfile
, "Missing REPEAT-LIMIT value on line %d.", linenum
);
2330 else if (atoi(token
) <= 0)
2332 print_fatal_error(outfile
, "Bad REPEAT-LIMIT value on line %d.", linenum
);
2338 last_status
->repeat_limit
= atoi(token
);
2339 else if (last_expect
)
2340 last_expect
->repeat_limit
= atoi(token
);
2343 print_fatal_error(outfile
, "REPEAT-LIMIT without a preceding EXPECT or STATUS on line %d.", linenum
);
2348 else if (!_cups_strcasecmp(token
, "REPEAT-MATCH"))
2351 last_status
->repeat_match
= 1;
2352 else if (last_expect
)
2353 last_expect
->repeat_match
= 1;
2356 print_fatal_error(outfile
, "REPEAT-MATCH without a preceding EXPECT or STATUS on line %d.", linenum
);
2361 else if (!_cups_strcasecmp(token
, "REPEAT-NO-MATCH"))
2364 last_status
->repeat_no_match
= 1;
2365 else if (last_expect
)
2366 last_expect
->repeat_no_match
= 1;
2369 print_fatal_error(outfile
, "REPEAT-NO-MATCH without a preceding EXPECT or STATUS on ine %d.", linenum
);
2374 else if (!_cups_strcasecmp(token
, "SAME-COUNT-AS"))
2376 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2378 print_fatal_error(outfile
, "Missing SAME-COUNT-AS name on line %d.", linenum
);
2384 last_expect
->same_count_as
= strdup(token
);
2387 print_fatal_error(outfile
, "SAME-COUNT-AS without a preceding EXPECT on line "
2393 else if (!_cups_strcasecmp(token
, "IF-DEFINED"))
2395 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2397 print_fatal_error(outfile
, "Missing IF-DEFINED name on line %d.", linenum
);
2403 last_expect
->if_defined
= strdup(token
);
2404 else if (last_status
)
2405 last_status
->if_defined
= strdup(token
);
2408 print_fatal_error(outfile
, "IF-DEFINED without a preceding EXPECT or STATUS on line %d.", linenum
);
2413 else if (!_cups_strcasecmp(token
, "IF-NOT-DEFINED"))
2415 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2417 print_fatal_error(outfile
, "Missing IF-NOT-DEFINED name on line %d.", linenum
);
2423 last_expect
->if_not_defined
= strdup(token
);
2424 else if (last_status
)
2425 last_status
->if_not_defined
= strdup(token
);
2428 print_fatal_error(outfile
, "IF-NOT-DEFINED without a preceding EXPECT or STATUS on line %d.", linenum
);
2433 else if (!_cups_strcasecmp(token
, "WITH-ALL-VALUES") ||
2434 !_cups_strcasecmp(token
, "WITH-ALL-HOSTNAMES") ||
2435 !_cups_strcasecmp(token
, "WITH-ALL-RESOURCES") ||
2436 !_cups_strcasecmp(token
, "WITH-ALL-SCHEMES") ||
2437 !_cups_strcasecmp(token
, "WITH-HOSTNAME") ||
2438 !_cups_strcasecmp(token
, "WITH-RESOURCE") ||
2439 !_cups_strcasecmp(token
, "WITH-SCHEME") ||
2440 !_cups_strcasecmp(token
, "WITH-VALUE"))
2444 if (!_cups_strcasecmp(token
, "WITH-ALL-HOSTNAMES") ||
2445 !_cups_strcasecmp(token
, "WITH-HOSTNAME"))
2446 last_expect
->with_flags
= _CUPS_WITH_HOSTNAME
;
2447 else if (!_cups_strcasecmp(token
, "WITH-ALL-RESOURCES") ||
2448 !_cups_strcasecmp(token
, "WITH-RESOURCE"))
2449 last_expect
->with_flags
= _CUPS_WITH_RESOURCE
;
2450 else if (!_cups_strcasecmp(token
, "WITH-ALL-SCHEMES") ||
2451 !_cups_strcasecmp(token
, "WITH-SCHEME"))
2452 last_expect
->with_flags
= _CUPS_WITH_SCHEME
;
2454 if (!_cups_strncasecmp(token
, "WITH-ALL-", 9))
2455 last_expect
->with_flags
|= _CUPS_WITH_ALL
;
2458 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
2460 print_fatal_error(outfile
, "Missing %s value on line %d.", token
, linenum
);
2468 * Expand any variables in the value and then save it.
2471 expand_variables(vars
, token
, temp
, sizeof(token
));
2473 tokenptr
= token
+ strlen(token
) - 1;
2475 if (token
[0] == '/' && tokenptr
> token
&& *tokenptr
== '/')
2478 * WITH-VALUE is a POSIX extended regular expression.
2481 last_expect
->with_value
= calloc(1, (size_t)(tokenptr
- token
));
2482 last_expect
->with_flags
|= _CUPS_WITH_REGEX
;
2484 if (last_expect
->with_value
)
2485 memcpy(last_expect
->with_value
, token
+ 1, (size_t)(tokenptr
- token
- 1));
2490 * WITH-VALUE is a literal value...
2493 char *ptr
; /* Pointer into value */
2495 for (ptr
= token
; *ptr
; ptr
++)
2497 if (*ptr
== '\\' && ptr
[1])
2500 * Remove \ from \foo...
2503 _cups_strcpy(ptr
, ptr
+ 1);
2507 last_expect
->with_value
= strdup(token
);
2508 last_expect
->with_flags
|= _CUPS_WITH_LITERAL
;
2513 print_fatal_error(outfile
, "%s without a preceding EXPECT on line %d.", token
, linenum
);
2518 else if (!_cups_strcasecmp(token
, "WITH-VALUE-FROM"))
2520 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
2522 print_fatal_error(outfile
, "Missing %s value on line %d.", token
, linenum
);
2530 * Expand any variables in the value and then save it.
2533 expand_variables(vars
, token
, temp
, sizeof(token
));
2535 last_expect
->with_value_from
= strdup(token
);
2536 last_expect
->with_flags
= _CUPS_WITH_LITERAL
;
2540 print_fatal_error(outfile
, "%s without a preceding EXPECT on line %d.", token
, linenum
);
2545 else if (!_cups_strcasecmp(token
, "DISPLAY"))
2548 * Display attributes...
2551 if (num_displayed
>= (int)(sizeof(displayed
) / sizeof(displayed
[0])))
2553 print_fatal_error(outfile
, "Too many DISPLAY's on line %d", linenum
);
2558 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2560 print_fatal_error(outfile
, "Missing DISPLAY name on line %d.", linenum
);
2565 displayed
[num_displayed
] = strdup(token
);
2570 print_fatal_error(outfile
, "Unexpected token %s seen on line %d.", token
, linenum
);
2577 * Submit the IPP request...
2582 ippSetVersion(request
, version
/ 10, version
% 10);
2583 ippSetOperation(request
, op
);
2584 ippSetRequestId(request
, request_id
);
2586 if (Output
== _CUPS_OUTPUT_PLIST
)
2588 cupsFilePuts(outfile
, "<dict>\n");
2589 cupsFilePuts(outfile
, "<key>Name</key>\n");
2590 print_xml_string(outfile
, "string", name
);
2593 cupsFilePuts(outfile
, "<key>FileId</key>\n");
2594 print_xml_string(outfile
, "string", file_id
);
2598 cupsFilePuts(outfile
, "<key>TestId</key>\n");
2599 print_xml_string(outfile
, "string", test_id
);
2601 cupsFilePuts(outfile
, "<key>Version</key>\n");
2602 cupsFilePrintf(outfile
, "<string>%d.%d</string>\n", version
/ 10, version
% 10);
2603 cupsFilePuts(outfile
, "<key>Operation</key>\n");
2604 print_xml_string(outfile
, "string", ippOpString(op
));
2605 cupsFilePuts(outfile
, "<key>RequestId</key>\n");
2606 cupsFilePrintf(outfile
, "<integer>%d</integer>\n", request_id
);
2607 cupsFilePuts(outfile
, "<key>RequestAttributes</key>\n");
2608 cupsFilePuts(outfile
, "<array>\n");
2611 cupsFilePuts(outfile
, "<dict>\n");
2612 for (attrptr
= request
->attrs
,
2613 group
= attrptr
? attrptr
->group_tag
: IPP_TAG_ZERO
;
2615 attrptr
= attrptr
->next
)
2616 print_attr(outfile
, Output
, attrptr
, &group
);
2617 cupsFilePuts(outfile
, "</dict>\n");
2619 cupsFilePuts(outfile
, "</array>\n");
2622 if (Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
!= cupsFileStdout()))
2626 cupsFilePrintf(cupsFileStdout(), " %s:\n", ippOpString(op
));
2628 for (attrptr
= request
->attrs
; attrptr
; attrptr
= attrptr
->next
)
2629 print_attr(cupsFileStdout(), _CUPS_OUTPUT_TEST
, attrptr
, NULL
);
2632 cupsFilePrintf(cupsFileStdout(), " %-68.68s [", name
);
2635 if ((skip_previous
&& !prev_pass
) || skip_test
)
2642 if (Output
== _CUPS_OUTPUT_PLIST
)
2644 cupsFilePuts(outfile
, "<key>Successful</key>\n");
2645 cupsFilePuts(outfile
, "<true />\n");
2646 cupsFilePuts(outfile
, "<key>Skipped</key>\n");
2647 cupsFilePuts(outfile
, "<true />\n");
2648 cupsFilePuts(outfile
, "<key>StatusCode</key>\n");
2649 print_xml_string(outfile
, "string", "skip");
2650 cupsFilePuts(outfile
, "<key>ResponseAttributes</key>\n");
2651 cupsFilePuts(outfile
, "<dict />\n");
2654 if (Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
!= cupsFileStdout()))
2655 cupsFilePuts(cupsFileStdout(), "SKIP]\n");
2667 delay
= repeat_interval
;
2670 status
= HTTP_STATUS_OK
;
2672 if (transfer
== _CUPS_TRANSFER_CHUNKED
||
2673 (transfer
== _CUPS_TRANSFER_AUTO
&& filename
[0]))
2676 * Send request using chunking - a 0 length means "chunk".
2684 * Send request using content length...
2687 length
= ippLength(request
);
2689 if (filename
[0] && (reqfile
= cupsFileOpen(filename
, "r")) != NULL
)
2692 * Read the file to get the uncompressed file size...
2695 while ((bytes
= cupsFileRead(reqfile
, buffer
, sizeof(buffer
))) > 0)
2696 length
+= (size_t)bytes
;
2698 cupsFileClose(reqfile
);
2703 * Send the request...
2710 if (status
!= HTTP_STATUS_ERROR
)
2712 while (!response
&& !Cancel
&& prev_pass
)
2714 status
= cupsSendRequest(http
, request
, resource
, length
);
2718 httpSetField(http
, HTTP_FIELD_CONTENT_ENCODING
, compression
);
2719 #endif /* HAVE_LIBZ */
2721 if (!Cancel
&& status
== HTTP_STATUS_CONTINUE
&&
2722 request
->state
== IPP_DATA
&& filename
[0])
2724 if ((reqfile
= cupsFileOpen(filename
, "r")) != NULL
)
2727 (bytes
= cupsFileRead(reqfile
, buffer
, sizeof(buffer
))) > 0)
2728 if ((status
= cupsWriteRequestData(http
, buffer
, (size_t)bytes
)) != HTTP_STATUS_CONTINUE
)
2731 cupsFileClose(reqfile
);
2735 snprintf(buffer
, sizeof(buffer
), "%s: %s", filename
,
2737 _cupsSetError(IPP_INTERNAL_ERROR
, buffer
, 0);
2739 status
= HTTP_STATUS_ERROR
;
2744 * Get the server's response...
2747 if (!Cancel
&& status
!= HTTP_STATUS_ERROR
)
2749 response
= cupsGetResponse(http
, resource
);
2750 status
= httpGetStatus(http
);
2753 if (!Cancel
&& status
== HTTP_STATUS_ERROR
&& http
->error
!= EINVAL
&&
2755 http
->error
!= WSAETIMEDOUT
)
2757 http
->error
!= ETIMEDOUT
)
2760 if (httpReconnect2(http
, 30000, NULL
))
2763 else if (status
== HTTP_STATUS_ERROR
|| status
== HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
)
2768 else if (status
!= HTTP_STATUS_OK
)
2772 if (status
== HTTP_STATUS_UNAUTHORIZED
)
2780 if (!Cancel
&& status
== HTTP_STATUS_ERROR
&& http
->error
!= EINVAL
&&
2782 http
->error
!= WSAETIMEDOUT
)
2784 http
->error
!= ETIMEDOUT
)
2787 if (httpReconnect2(http
, 30000, NULL
))
2790 else if (status
== HTTP_STATUS_ERROR
)
2793 httpReconnect2(http
, 30000, NULL
);
2797 else if (status
!= HTTP_STATUS_OK
)
2804 * Check results of request...
2807 cupsArrayClear(errors
);
2809 if (http
->version
!= HTTP_1_1
)
2810 add_stringf(errors
, "Bad HTTP version (%d.%d)", http
->version
/ 100,
2811 http
->version
% 100);
2816 * No response, log error...
2819 add_stringf(errors
, "IPP request failed with status %s (%s)",
2820 ippErrorString(cupsLastError()),
2821 cupsLastErrorString());
2826 * Collect common attribute values...
2829 if ((attrptr
= ippFindAttribute(response
, "job-id",
2830 IPP_TAG_INTEGER
)) != NULL
)
2832 snprintf(temp
, sizeof(temp
), "%d", attrptr
->values
[0].integer
);
2833 set_variable(outfile
, vars
, "job-id", temp
);
2836 if ((attrptr
= ippFindAttribute(response
, "job-uri",
2837 IPP_TAG_URI
)) != NULL
)
2838 set_variable(outfile
, vars
, "job-uri", attrptr
->values
[0].string
.text
);
2840 if ((attrptr
= ippFindAttribute(response
, "notify-subscription-id",
2841 IPP_TAG_INTEGER
)) != NULL
)
2843 snprintf(temp
, sizeof(temp
), "%d", attrptr
->values
[0].integer
);
2844 set_variable(outfile
, vars
, "notify-subscription-id", temp
);
2848 * Check response, validating groups and attributes and logging errors
2852 if (response
->state
!= IPP_DATA
)
2854 "Missing end-of-attributes-tag in response "
2855 "(RFC 2910 section 3.5.1)");
2858 (response
->request
.status
.version
[0] != (version
/ 10) ||
2859 response
->request
.status
.version
[1] != (version
% 10)))
2861 "Bad version %d.%d in response - expected %d.%d "
2862 "(RFC 2911 section 3.1.8).",
2863 response
->request
.status
.version
[0],
2864 response
->request
.status
.version
[1],
2865 version
/ 10, version
% 10);
2867 if (response
->request
.status
.request_id
!= request_id
)
2869 "Bad request ID %d in response - expected %d "
2870 "(RFC 2911 section 3.1.1)",
2871 response
->request
.status
.request_id
, request_id
);
2873 attrptr
= response
->attrs
;
2876 "Missing first attribute \"attributes-charset "
2877 "(charset)\" in group operation-attributes-tag "
2878 "(RFC 2911 section 3.1.4).");
2881 if (!attrptr
->name
||
2882 attrptr
->value_tag
!= IPP_TAG_CHARSET
||
2883 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2884 attrptr
->num_values
!= 1 ||
2885 strcmp(attrptr
->name
, "attributes-charset"))
2887 "Bad first attribute \"%s (%s%s)\" in group %s, "
2888 "expected \"attributes-charset (charset)\" in "
2889 "group operation-attributes-tag (RFC 2911 section "
2891 attrptr
->name
? attrptr
->name
: "(null)",
2892 attrptr
->num_values
> 1 ? "1setOf " : "",
2893 ippTagString(attrptr
->value_tag
),
2894 ippTagString(attrptr
->group_tag
));
2896 attrptr
= attrptr
->next
;
2899 "Missing second attribute \"attributes-natural-"
2900 "language (naturalLanguage)\" in group "
2901 "operation-attributes-tag (RFC 2911 section "
2903 else if (!attrptr
->name
||
2904 attrptr
->value_tag
!= IPP_TAG_LANGUAGE
||
2905 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2906 attrptr
->num_values
!= 1 ||
2907 strcmp(attrptr
->name
, "attributes-natural-language"))
2909 "Bad first attribute \"%s (%s%s)\" in group %s, "
2910 "expected \"attributes-natural-language "
2911 "(naturalLanguage)\" in group "
2912 "operation-attributes-tag (RFC 2911 section "
2914 attrptr
->name
? attrptr
->name
: "(null)",
2915 attrptr
->num_values
> 1 ? "1setOf " : "",
2916 ippTagString(attrptr
->value_tag
),
2917 ippTagString(attrptr
->group_tag
));
2920 if ((attrptr
= ippFindAttribute(response
, "status-message",
2921 IPP_TAG_ZERO
)) != NULL
)
2923 if (attrptr
->value_tag
!= IPP_TAG_TEXT
)
2925 "status-message (text(255)) has wrong value tag "
2926 "%s (RFC 2911 section 3.1.6.2).",
2927 ippTagString(attrptr
->value_tag
));
2928 if (attrptr
->group_tag
!= IPP_TAG_OPERATION
)
2930 "status-message (text(255)) has wrong group tag "
2931 "%s (RFC 2911 section 3.1.6.2).",
2932 ippTagString(attrptr
->group_tag
));
2933 if (attrptr
->num_values
!= 1)
2935 "status-message (text(255)) has %d values "
2936 "(RFC 2911 section 3.1.6.2).",
2937 attrptr
->num_values
);
2938 if (attrptr
->value_tag
== IPP_TAG_TEXT
&&
2939 strlen(attrptr
->values
[0].string
.text
) > 255)
2941 "status-message (text(255)) has bad length %d"
2942 " (RFC 2911 section 3.1.6.2).",
2943 (int)strlen(attrptr
->values
[0].string
.text
));
2946 if ((attrptr
= ippFindAttribute(response
, "detailed-status-message",
2947 IPP_TAG_ZERO
)) != NULL
)
2949 if (attrptr
->value_tag
!= IPP_TAG_TEXT
)
2951 "detailed-status-message (text(MAX)) has wrong "
2952 "value tag %s (RFC 2911 section 3.1.6.3).",
2953 ippTagString(attrptr
->value_tag
));
2954 if (attrptr
->group_tag
!= IPP_TAG_OPERATION
)
2956 "detailed-status-message (text(MAX)) has wrong "
2957 "group tag %s (RFC 2911 section 3.1.6.3).",
2958 ippTagString(attrptr
->group_tag
));
2959 if (attrptr
->num_values
!= 1)
2961 "detailed-status-message (text(MAX)) has %d values"
2962 " (RFC 2911 section 3.1.6.3).",
2963 attrptr
->num_values
);
2964 if (attrptr
->value_tag
== IPP_TAG_TEXT
&&
2965 strlen(attrptr
->values
[0].string
.text
) > 1023)
2967 "detailed-status-message (text(MAX)) has bad "
2968 "length %d (RFC 2911 section 3.1.6.3).",
2969 (int)strlen(attrptr
->values
[0].string
.text
));
2972 a
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
2974 for (attrptr
= response
->attrs
,
2975 group
= attrptr
? attrptr
->group_tag
: IPP_TAG_ZERO
;
2977 attrptr
= attrptr
->next
)
2979 if (attrptr
->group_tag
!= group
)
2981 int out_of_order
= 0; /* Are attribute groups out-of-order? */
2984 switch (attrptr
->group_tag
)
2989 case IPP_TAG_OPERATION
:
2993 case IPP_TAG_UNSUPPORTED_GROUP
:
2994 if (group
!= IPP_TAG_OPERATION
)
2999 case IPP_TAG_PRINTER
:
3000 if (group
!= IPP_TAG_OPERATION
&&
3001 group
!= IPP_TAG_UNSUPPORTED_GROUP
)
3005 case IPP_TAG_SUBSCRIPTION
:
3006 if (group
> attrptr
->group_tag
&&
3007 group
!= IPP_TAG_DOCUMENT
)
3012 if (group
> attrptr
->group_tag
)
3018 add_stringf(errors
, "Attribute groups out of order (%s < %s)",
3019 ippTagString(attrptr
->group_tag
),
3020 ippTagString(group
));
3022 if (attrptr
->group_tag
!= IPP_TAG_ZERO
)
3023 group
= attrptr
->group_tag
;
3026 validate_attr(outfile
, errors
, attrptr
);
3030 if (cupsArrayFind(a
, attrptr
->name
))
3031 add_stringf(errors
, "Duplicate \"%s\" attribute in %s group",
3032 attrptr
->name
, ippTagString(group
));
3034 cupsArrayAdd(a
, attrptr
->name
);
3041 * Now check the test-defined expected status-code and attribute
3045 for (i
= 0, status_ok
= 0; i
< num_statuses
; i
++)
3047 if (statuses
[i
].if_defined
&&
3048 !get_variable(vars
, statuses
[i
].if_defined
))
3051 if (statuses
[i
].if_not_defined
&&
3052 get_variable(vars
, statuses
[i
].if_not_defined
))
3055 if (ippGetStatusCode(response
) == statuses
[i
].status
)
3059 if (statuses
[i
].repeat_match
&& repeat_count
< statuses
[i
].repeat_limit
)
3062 if (statuses
[i
].define_match
)
3063 set_variable(outfile
, vars
, statuses
[i
].define_match
, "1");
3067 if (statuses
[i
].repeat_no_match
&& repeat_count
< statuses
[i
].repeat_limit
)
3070 if (statuses
[i
].define_no_match
)
3072 set_variable(outfile
, vars
, statuses
[i
].define_no_match
, "1");
3078 if (!status_ok
&& num_statuses
> 0)
3080 for (i
= 0; i
< num_statuses
; i
++)
3082 if (statuses
[i
].if_defined
&&
3083 !get_variable(vars
, statuses
[i
].if_defined
))
3086 if (statuses
[i
].if_not_defined
&&
3087 get_variable(vars
, statuses
[i
].if_not_defined
))
3090 if (!statuses
[i
].repeat_match
|| repeat_count
>= statuses
[i
].repeat_limit
)
3091 add_stringf(errors
, "EXPECTED: STATUS %s (got %s)",
3092 ippErrorString(statuses
[i
].status
),
3093 ippErrorString(cupsLastError()));
3096 if ((attrptr
= ippFindAttribute(response
, "status-message",
3097 IPP_TAG_TEXT
)) != NULL
)
3098 add_stringf(errors
, "status-message=\"%s\"",
3099 attrptr
->values
[0].string
.text
);
3102 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
3104 if (expect
->if_defined
&& !get_variable(vars
, expect
->if_defined
))
3107 if (expect
->if_not_defined
&&
3108 get_variable(vars
, expect
->if_not_defined
))
3111 found
= ippFindAttribute(response
, expect
->name
, IPP_TAG_ZERO
);
3115 if ((found
&& expect
->not_expect
) ||
3116 (!found
&& !(expect
->not_expect
|| expect
->optional
)) ||
3117 (found
&& !expect_matches(expect
, found
->value_tag
)) ||
3118 (found
&& expect
->in_group
&&
3119 found
->group_tag
!= expect
->in_group
))
3121 if (expect
->define_no_match
)
3122 set_variable(outfile
, vars
, expect
->define_no_match
, "1");
3123 else if (!expect
->define_match
&& !expect
->define_value
)
3125 if (found
&& expect
->not_expect
)
3126 add_stringf(errors
, "NOT EXPECTED: %s", expect
->name
);
3127 else if (!found
&& !(expect
->not_expect
|| expect
->optional
))
3128 add_stringf(errors
, "EXPECTED: %s", expect
->name
);
3131 if (!expect_matches(expect
, found
->value_tag
))
3132 add_stringf(errors
, "EXPECTED: %s OF-TYPE %s (got %s)",
3133 expect
->name
, expect
->of_type
,
3134 ippTagString(found
->value_tag
));
3136 if (expect
->in_group
&& found
->group_tag
!= expect
->in_group
)
3137 add_stringf(errors
, "EXPECTED: %s IN-GROUP %s (got %s).",
3138 expect
->name
, ippTagString(expect
->in_group
),
3139 ippTagString(found
->group_tag
));
3143 if (expect
->repeat_no_match
&& repeat_count
< expect
->repeat_limit
)
3150 ippAttributeString(found
, buffer
, sizeof(buffer
));
3152 if (found
&& expect
->with_value_from
&& !with_value_from(NULL
, ippFindAttribute(response
, expect
->with_value_from
, IPP_TAG_ZERO
), found
, buffer
, sizeof(buffer
)))
3154 if (expect
->define_no_match
)
3155 set_variable(outfile
, vars
, expect
->define_no_match
, "1");
3156 else if (!expect
->define_match
&& !expect
->define_value
&& ((!expect
->repeat_match
&& !expect
->repeat_no_match
) || repeat_count
>= expect
->repeat_limit
))
3158 add_stringf(errors
, "EXPECTED: %s WITH-VALUES-FROM %s", expect
->name
, expect
->with_value_from
);
3160 with_value_from(errors
, ippFindAttribute(response
, expect
->with_value_from
, IPP_TAG_ZERO
), found
, buffer
, sizeof(buffer
));
3163 if (expect
->repeat_no_match
&& repeat_count
< expect
->repeat_limit
)
3168 else if (found
&& !with_value(outfile
, NULL
, expect
->with_value
, expect
->with_flags
, found
, buffer
, sizeof(buffer
)))
3170 if (expect
->define_no_match
)
3171 set_variable(outfile
, vars
, expect
->define_no_match
, "1");
3172 else if (!expect
->define_match
&& !expect
->define_value
&&
3173 !expect
->repeat_match
&& (!expect
->repeat_no_match
|| repeat_count
>= expect
->repeat_limit
))
3175 if (expect
->with_flags
& _CUPS_WITH_REGEX
)
3176 add_stringf(errors
, "EXPECTED: %s %s /%s/",
3178 (expect
->with_flags
& _CUPS_WITH_ALL
) ?
3179 "WITH-ALL-VALUES" : "WITH-VALUE",
3180 expect
->with_value
);
3182 add_stringf(errors
, "EXPECTED: %s %s \"%s\"",
3184 (expect
->with_flags
& _CUPS_WITH_ALL
) ?
3185 "WITH-ALL-VALUES" : "WITH-VALUE",
3186 expect
->with_value
);
3188 with_value(outfile
, errors
, expect
->with_value
, expect
->with_flags
, found
,
3189 buffer
, sizeof(buffer
));
3192 if (expect
->repeat_no_match
&&
3193 repeat_count
< expect
->repeat_limit
)
3199 if (found
&& expect
->count
> 0 &&
3200 found
->num_values
!= expect
->count
)
3202 if (expect
->define_no_match
)
3203 set_variable(outfile
, vars
, expect
->define_no_match
, "1");
3204 else if (!expect
->define_match
&& !expect
->define_value
)
3206 add_stringf(errors
, "EXPECTED: %s COUNT %d (got %d)", expect
->name
,
3207 expect
->count
, found
->num_values
);
3210 if (expect
->repeat_no_match
&&
3211 repeat_count
< expect
->repeat_limit
)
3217 if (found
&& expect
->same_count_as
)
3219 attrptr
= ippFindAttribute(response
, expect
->same_count_as
,
3222 if (!attrptr
|| attrptr
->num_values
!= found
->num_values
)
3224 if (expect
->define_no_match
)
3225 set_variable(outfile
, vars
, expect
->define_no_match
, "1");
3226 else if (!expect
->define_match
&& !expect
->define_value
)
3230 "EXPECTED: %s (%d values) SAME-COUNT-AS %s "
3231 "(not returned)", expect
->name
,
3232 found
->num_values
, expect
->same_count_as
);
3233 else if (attrptr
->num_values
!= found
->num_values
)
3235 "EXPECTED: %s (%d values) SAME-COUNT-AS %s "
3236 "(%d values)", expect
->name
, found
->num_values
,
3237 expect
->same_count_as
, attrptr
->num_values
);
3240 if (expect
->repeat_no_match
&&
3241 repeat_count
< expect
->repeat_limit
)
3248 if (found
&& expect
->define_match
)
3249 set_variable(outfile
, vars
, expect
->define_match
, "1");
3251 if (found
&& expect
->define_value
)
3253 if (!expect
->with_value
)
3255 int last
= ippGetCount(found
) - 1;
3256 /* Last element in attribute */
3258 switch (ippGetValueTag(found
))
3261 case IPP_TAG_INTEGER
:
3262 snprintf(buffer
, sizeof(buffer
), "%d", ippGetInteger(found
, last
));
3265 case IPP_TAG_BOOLEAN
:
3266 if (ippGetBoolean(found
, last
))
3267 strlcpy(buffer
, "true", sizeof(buffer
));
3269 strlcpy(buffer
, "false", sizeof(buffer
));
3272 case IPP_TAG_RESOLUTION
:
3274 int xres
, /* Horizontal resolution */
3275 yres
; /* Vertical resolution */
3276 ipp_res_t units
; /* Resolution units */
3278 xres
= ippGetResolution(found
, last
, &yres
, &units
);
3281 snprintf(buffer
, sizeof(buffer
), "%d%s", xres
, units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
3283 snprintf(buffer
, sizeof(buffer
), "%dx%d%s", xres
, yres
, units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
3287 case IPP_TAG_CHARSET
:
3288 case IPP_TAG_KEYWORD
:
3289 case IPP_TAG_LANGUAGE
:
3290 case IPP_TAG_MIMETYPE
:
3292 case IPP_TAG_NAMELANG
:
3294 case IPP_TAG_TEXTLANG
:
3296 case IPP_TAG_URISCHEME
:
3297 strlcpy(buffer
, ippGetString(found
, last
, NULL
), sizeof(buffer
));
3301 ippAttributeString(found
, buffer
, sizeof(buffer
));
3306 set_variable(outfile
, vars
, expect
->define_value
, buffer
);
3309 if (found
&& expect
->repeat_match
&&
3310 repeat_count
< expect
->repeat_limit
)
3313 while (expect
->expect_all
&& (found
= ippFindNextAttribute(response
, expect
->name
, IPP_TAG_ZERO
)) != NULL
);
3318 * If we are going to repeat this test, sleep 1 second so we don't flood
3319 * the printer with requests...
3324 if (Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
!= cupsFileStdout()))
3326 cupsFilePrintf(cupsFileStdout(), "%04d]\n", repeat_count
);
3328 if (num_displayed
> 0)
3330 for (attrptr
= ippFirstAttribute(response
); attrptr
; attrptr
= ippNextAttribute(response
))
3332 const char *attrname
= ippGetName(attrptr
);
3335 for (i
= 0; i
< num_displayed
; i
++)
3337 if (!strcmp(displayed
[i
], attrname
))
3339 print_attr(cupsFileStdout(), _CUPS_OUTPUT_TEST
, attrptr
, NULL
);
3348 if (Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
!= cupsFileStdout()))
3350 cupsFilePrintf(cupsFileStdout(), " %-68.68s [", name
);
3354 while (repeat_test
);
3360 if (cupsArrayCount(errors
) > 0)
3361 prev_pass
= pass
= 0;
3368 if (Output
== _CUPS_OUTPUT_PLIST
)
3370 cupsFilePuts(outfile
, "<key>Successful</key>\n");
3371 cupsFilePuts(outfile
, prev_pass
? "<true />\n" : "<false />\n");
3372 cupsFilePuts(outfile
, "<key>StatusCode</key>\n");
3373 print_xml_string(outfile
, "string", ippErrorString(cupsLastError()));
3374 cupsFilePuts(outfile
, "<key>ResponseAttributes</key>\n");
3375 cupsFilePuts(outfile
, "<array>\n");
3376 cupsFilePuts(outfile
, "<dict>\n");
3377 for (attrptr
= response
? response
->attrs
: NULL
,
3378 group
= attrptr
? attrptr
->group_tag
: IPP_TAG_ZERO
;
3380 attrptr
= attrptr
->next
)
3381 print_attr(outfile
, Output
, attrptr
, &group
);
3382 cupsFilePuts(outfile
, "</dict>\n");
3383 cupsFilePuts(outfile
, "</array>\n");
3386 if (Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
!= cupsFileStdout()))
3388 cupsFilePuts(cupsFileStdout(), prev_pass
? "PASS]\n" : "FAIL]\n");
3390 if (!prev_pass
|| (Verbosity
&& response
))
3392 cupsFilePrintf(cupsFileStdout(), " RECEIVED: %lu bytes in response\n", (unsigned long)ippLength(response
));
3393 cupsFilePrintf(cupsFileStdout(), " status-code = %s (%s)\n", ippErrorString(cupsLastError()), cupsLastErrorString());
3395 if (Verbosity
&& response
)
3397 for (attrptr
= response
->attrs
;
3399 attrptr
= attrptr
->next
)
3400 print_attr(cupsFileStdout(), _CUPS_OUTPUT_TEST
, attrptr
, NULL
);
3404 else if (!prev_pass
&& Output
!= _CUPS_OUTPUT_QUIET
)
3405 fprintf(stderr
, "%s\n", cupsLastErrorString());
3407 if (prev_pass
&& Output
>= _CUPS_OUTPUT_LIST
&& !Verbosity
&&
3410 size_t width
; /* Length of value */
3412 for (i
= 0; i
< num_displayed
; i
++)
3414 widths
[i
] = strlen(displayed
[i
]);
3416 for (attrptr
= ippFindAttribute(response
, displayed
[i
], IPP_TAG_ZERO
);
3418 attrptr
= ippFindNextAttribute(response
, displayed
[i
],
3421 width
= ippAttributeString(attrptr
, NULL
, 0);
3422 if (width
> widths
[i
])
3427 if (Output
== _CUPS_OUTPUT_CSV
)
3428 print_csv(outfile
, NULL
, num_displayed
, displayed
, widths
);
3430 print_line(outfile
, NULL
, num_displayed
, displayed
, widths
);
3432 attrptr
= response
->attrs
;
3436 while (attrptr
&& attrptr
->group_tag
<= IPP_TAG_OPERATION
)
3437 attrptr
= attrptr
->next
;
3441 if (Output
== _CUPS_OUTPUT_CSV
)
3442 print_csv(outfile
, attrptr
, num_displayed
, displayed
, widths
);
3444 print_line(outfile
, attrptr
, num_displayed
, displayed
, widths
);
3446 while (attrptr
&& attrptr
->group_tag
> IPP_TAG_OPERATION
)
3447 attrptr
= attrptr
->next
;
3451 else if (!prev_pass
)
3453 if (Output
== _CUPS_OUTPUT_PLIST
)
3455 cupsFilePuts(outfile
, "<key>Errors</key>\n");
3456 cupsFilePuts(outfile
, "<array>\n");
3458 for (error
= (char *)cupsArrayFirst(errors
);
3460 error
= (char *)cupsArrayNext(errors
))
3461 print_xml_string(outfile
, "string", error
);
3463 cupsFilePuts(outfile
, "</array>\n");
3466 if (Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
!= cupsFileStdout()))
3468 for (error
= (char *)cupsArrayFirst(errors
);
3470 error
= (char *)cupsArrayNext(errors
))
3471 cupsFilePrintf(cupsFileStdout(), " %s\n", error
);
3475 if (num_displayed
> 0 && !Verbosity
&& response
&& (Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
!= cupsFileStdout())))
3477 for (attrptr
= response
->attrs
;
3479 attrptr
= attrptr
->next
)
3483 for (i
= 0; i
< num_displayed
; i
++)
3485 if (!strcmp(displayed
[i
], attrptr
->name
))
3487 print_attr(outfile
, Output
, attrptr
, NULL
);
3497 if (Output
== _CUPS_OUTPUT_PLIST
)
3498 cupsFilePuts(outfile
, "</dict>\n");
3500 ippDelete(response
);
3503 for (i
= 0; i
< num_statuses
; i
++)
3505 if (statuses
[i
].if_defined
)
3506 free(statuses
[i
].if_defined
);
3507 if (statuses
[i
].if_not_defined
)
3508 free(statuses
[i
].if_not_defined
);
3509 if (statuses
[i
].define_match
)
3510 free(statuses
[i
].define_match
);
3511 if (statuses
[i
].define_no_match
)
3512 free(statuses
[i
].define_no_match
);
3516 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
3519 if (expect
->of_type
)
3520 free(expect
->of_type
);
3521 if (expect
->same_count_as
)
3522 free(expect
->same_count_as
);
3523 if (expect
->if_defined
)
3524 free(expect
->if_defined
);
3525 if (expect
->if_not_defined
)
3526 free(expect
->if_not_defined
);
3527 if (expect
->with_value
)
3528 free(expect
->with_value
);
3529 if (expect
->define_match
)
3530 free(expect
->define_match
);
3531 if (expect
->define_no_match
)
3532 free(expect
->define_no_match
);
3533 if (expect
->define_value
)
3534 free(expect
->define_value
);
3538 for (i
= 0; i
< num_displayed
; i
++)
3542 if (!ignore_errors
&& !prev_pass
)
3548 cupsArrayDelete(errors
);
3555 ippDelete(response
);
3557 for (i
= 0; i
< num_statuses
; i
++)
3559 if (statuses
[i
].if_defined
)
3560 free(statuses
[i
].if_defined
);
3561 if (statuses
[i
].if_not_defined
)
3562 free(statuses
[i
].if_not_defined
);
3563 if (statuses
[i
].define_match
)
3564 free(statuses
[i
].define_match
);
3565 if (statuses
[i
].define_no_match
)
3566 free(statuses
[i
].define_no_match
);
3569 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
3572 if (expect
->of_type
)
3573 free(expect
->of_type
);
3574 if (expect
->same_count_as
)
3575 free(expect
->same_count_as
);
3576 if (expect
->if_defined
)
3577 free(expect
->if_defined
);
3578 if (expect
->if_not_defined
)
3579 free(expect
->if_not_defined
);
3580 if (expect
->with_value
)
3581 free(expect
->with_value
);
3582 if (expect
->define_match
)
3583 free(expect
->define_match
);
3584 if (expect
->define_no_match
)
3585 free(expect
->define_no_match
);
3586 if (expect
->define_value
)
3587 free(expect
->define_value
);
3590 for (i
= 0; i
< num_displayed
; i
++)
3598 * 'expand_variables()' - Expand variables in a string.
3602 expand_variables(_cups_vars_t
*vars
, /* I - Variables */
3603 char *dst
, /* I - Destination string buffer */
3604 const char *src
, /* I - Source string */
3605 size_t dstsize
) /* I - Size of destination buffer */
3607 char *dstptr
, /* Pointer into destination */
3608 *dstend
, /* End of destination */
3609 temp
[256], /* Temporary string */
3610 *tempptr
; /* Pointer into temporary string */
3611 const char *value
; /* Value to substitute */
3615 dstend
= dst
+ dstsize
- 1;
3617 while (*src
&& dstptr
< dstend
)
3622 * Substitute a string/number...
3625 if (!strncmp(src
, "$$", 2))
3630 else if (!strncmp(src
, "$ENV[", 5))
3632 strlcpy(temp
, src
+ 5, sizeof(temp
));
3634 for (tempptr
= temp
; *tempptr
; tempptr
++)
3635 if (*tempptr
== ']')
3641 value
= getenv(temp
);
3642 src
+= tempptr
- temp
+ 5;
3649 strlcpy(temp
, src
, sizeof(temp
));
3650 if ((tempptr
= strchr(temp
, '}')) != NULL
)
3653 tempptr
= temp
+ strlen(temp
);
3657 strlcpy(temp
, src
+ 1, sizeof(temp
));
3659 for (tempptr
= temp
; *tempptr
; tempptr
++)
3660 if (!isalnum(*tempptr
& 255) && *tempptr
!= '-' && *tempptr
!= '_')
3667 if (!strcmp(temp
, "uri"))
3669 else if (!strcmp(temp
, "filename"))
3670 value
= vars
->filename
;
3671 else if (!strcmp(temp
, "scheme") || !strcmp(temp
, "method"))
3672 value
= vars
->scheme
;
3673 else if (!strcmp(temp
, "username"))
3674 value
= vars
->userpass
;
3675 else if (!strcmp(temp
, "hostname"))
3676 value
= vars
->hostname
;
3677 else if (!strcmp(temp
, "port"))
3679 snprintf(temp
, sizeof(temp
), "%d", vars
->port
);
3682 else if (!strcmp(temp
, "resource"))
3683 value
= vars
->resource
;
3684 else if (!strcmp(temp
, "user"))
3687 value
= get_variable(vars
, temp
);
3689 src
+= tempptr
- temp
+ 1;
3694 strlcpy(dstptr
, value
, (size_t)(dstend
- dstptr
+ 1));
3695 dstptr
+= strlen(dstptr
);
3707 * 'expect_matches()' - Return true if the tag matches the specification.
3710 static int /* O - 1 if matches, 0 otherwise */
3712 _cups_expect_t
*expect
, /* I - Expected attribute */
3713 ipp_tag_t value_tag
) /* I - Value tag for attribute */
3715 int match
; /* Match? */
3716 char *of_type
, /* Type name to match */
3717 *next
, /* Next name to match */
3718 sep
; /* Separator character */
3722 * If we don't expect a particular type, return immediately...
3725 if (!expect
->of_type
)
3729 * Parse the "of_type" value since the string can contain multiple attribute
3730 * types separated by "," or "|"...
3733 for (of_type
= expect
->of_type
, match
= 0; !match
&& *of_type
; of_type
= next
)
3736 * Find the next separator, and set it (temporarily) to nul if present.
3739 for (next
= of_type
; *next
&& *next
!= '|' && *next
!= ','; next
++);
3741 if ((sep
= *next
) != '\0')
3745 * Support some meta-types to make it easier to write the test file.
3748 if (!strcmp(of_type
, "text"))
3749 match
= value_tag
== IPP_TAG_TEXTLANG
|| value_tag
== IPP_TAG_TEXT
;
3750 else if (!strcmp(of_type
, "name"))
3751 match
= value_tag
== IPP_TAG_NAMELANG
|| value_tag
== IPP_TAG_NAME
;
3752 else if (!strcmp(of_type
, "collection"))
3753 match
= value_tag
== IPP_TAG_BEGIN_COLLECTION
;
3755 match
= value_tag
== ippTagValue(of_type
);
3758 * Restore the separator if we have one...
3770 * 'get_collection()' - Get a collection value from the current test file.
3773 static ipp_t
* /* O - Collection value */
3774 get_collection(cups_file_t
*outfile
, /* I - Output file */
3775 _cups_vars_t
*vars
, /* I - Variables */
3776 cups_file_t
*fp
, /* I - File to read from */
3777 int *linenum
) /* IO - Line number */
3779 char token
[1024], /* Token from file */
3780 temp
[1024], /* Temporary string */
3781 attr
[128]; /* Attribute name */
3782 ipp_tag_t value
; /* Current value type */
3783 ipp_t
*col
= ippNew(); /* Collection value */
3784 ipp_attribute_t
*lastcol
= NULL
; /* Last collection attribute */
3787 while (get_token(fp
, token
, sizeof(token
), linenum
) != NULL
)
3789 if (!strcmp(token
, "}"))
3791 else if (!strcmp(token
, "{") && lastcol
)
3794 * Another collection value
3797 ipp_t
*subcol
= get_collection(outfile
, vars
, fp
, linenum
);
3798 /* Collection value */
3801 ippSetCollection(col
, &lastcol
, ippGetCount(lastcol
), subcol
);
3805 else if (!_cups_strcasecmp(token
, "MEMBER"))
3813 if (!get_token(fp
, token
, sizeof(token
), linenum
))
3815 print_fatal_error(outfile
, "Missing MEMBER value tag on line %d.", *linenum
);
3819 if ((value
= ippTagValue(token
)) < IPP_TAG_UNSUPPORTED_VALUE
)
3821 print_fatal_error(outfile
, "Bad MEMBER value tag \"%s\" on line %d.", token
,
3826 if (!get_token(fp
, attr
, sizeof(attr
), linenum
))
3828 print_fatal_error(outfile
, "Missing MEMBER name on line %d.", *linenum
);
3832 if (value
< IPP_TAG_INTEGER
)
3835 * Out-of-band member attributes have no value...
3840 else if (!get_token(fp
, temp
, sizeof(temp
), linenum
))
3842 print_fatal_error(outfile
, "Missing MEMBER value on line %d.", *linenum
);
3847 expand_variables(vars
, token
, temp
, sizeof(token
));
3853 if (value
< IPP_TAG_INTEGER
)
3856 * Add out-of-band value...
3859 ippAddOutOfBand(col
, IPP_TAG_ZERO
, value
, attr
);
3863 print_fatal_error(outfile
, "Unsupported MEMBER value tag %s for \"%s\" on line %d.", ippTagString(value
), attr
, *linenum
);
3868 case IPP_TAG_BOOLEAN
:
3869 if (!_cups_strcasecmp(token
, "true"))
3870 ippAddBoolean(col
, IPP_TAG_ZERO
, attr
, 1);
3872 ippAddBoolean(col
, IPP_TAG_ZERO
, attr
, (char)atoi(token
));
3875 case IPP_TAG_INTEGER
:
3877 ippAddInteger(col
, IPP_TAG_ZERO
, value
, attr
, atoi(token
));
3880 case IPP_TAG_RESOLUTION
:
3882 int xres
, /* X resolution */
3883 yres
; /* Y resolution */
3884 char units
[6]; /* Units */
3886 if (sscanf(token
, "%dx%d%5s", &xres
, &yres
, units
) != 3 ||
3887 (_cups_strcasecmp(units
, "dpi") &&
3888 _cups_strcasecmp(units
, "dpc") &&
3889 _cups_strcasecmp(units
, "dpcm") &&
3890 _cups_strcasecmp(units
, "other")))
3892 print_fatal_error(outfile
, "Bad resolution value \"%s\" on line %d.",
3897 if (!_cups_strcasecmp(units
, "dpi"))
3898 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, IPP_RES_PER_INCH
, xres
, yres
);
3899 else if (!_cups_strcasecmp(units
, "dpc") ||
3900 !_cups_strcasecmp(units
, "dpcm"))
3901 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, IPP_RES_PER_CM
, xres
, yres
);
3903 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, (ipp_res_t
)0, xres
, yres
);
3907 case IPP_TAG_RANGE
:
3909 int lowers
[4], /* Lower value */
3910 uppers
[4], /* Upper values */
3911 num_vals
; /* Number of values */
3914 num_vals
= sscanf(token
, "%d-%d,%d-%d,%d-%d,%d-%d",
3915 lowers
+ 0, uppers
+ 0,
3916 lowers
+ 1, uppers
+ 1,
3917 lowers
+ 2, uppers
+ 2,
3918 lowers
+ 3, uppers
+ 3);
3920 if ((num_vals
& 1) || num_vals
== 0)
3922 print_fatal_error(outfile
, "Bad rangeOfInteger value \"%s\" on line %d.",
3927 ippAddRanges(col
, IPP_TAG_ZERO
, attr
, num_vals
/ 2, lowers
,
3932 case IPP_TAG_BEGIN_COLLECTION
:
3933 if (!strcmp(token
, "{"))
3935 ipp_t
*subcol
= get_collection(outfile
, vars
, fp
, linenum
);
3936 /* Collection value */
3940 lastcol
= ippAddCollection(col
, IPP_TAG_ZERO
, attr
, subcol
);
3948 print_fatal_error(outfile
, "Bad collection value on line %d.", *linenum
);
3953 case IPP_TAG_STRING
:
3954 ippAddOctetString(col
, IPP_TAG_ZERO
, attr
, token
, (int)strlen(token
));
3957 case IPP_TAG_TEXTLANG
:
3958 case IPP_TAG_NAMELANG
:
3961 case IPP_TAG_KEYWORD
:
3963 case IPP_TAG_URISCHEME
:
3964 case IPP_TAG_CHARSET
:
3965 case IPP_TAG_LANGUAGE
:
3966 case IPP_TAG_MIMETYPE
:
3967 if (!strchr(token
, ','))
3968 ippAddString(col
, IPP_TAG_ZERO
, value
, attr
, NULL
, token
);
3972 * Multiple string values...
3975 int num_values
; /* Number of values */
3976 char *values
[100], /* Values */
3977 *ptr
; /* Pointer to next value */
3983 for (ptr
= strchr(token
, ','); ptr
; ptr
= strchr(ptr
, ','))
3985 if (ptr
> token
&& ptr
[-1] == '\\')
3986 _cups_strcpy(ptr
- 1, ptr
);
3990 values
[num_values
] = ptr
;
3995 ippAddStrings(col
, IPP_TAG_ZERO
, value
, attr
, num_values
,
3996 NULL
, (const char **)values
);
4003 print_fatal_error(outfile
, "Unexpected token %s seen on line %d.", token
, *linenum
);
4011 * If we get here there was a parse error; free memory and return.
4023 * 'get_filename()' - Get a filename based on the current test file.
4026 static char * /* O - Filename */
4027 get_filename(const char *testfile
, /* I - Current test file */
4028 char *dst
, /* I - Destination filename */
4029 const char *src
, /* I - Source filename */
4030 size_t dstsize
) /* I - Size of destination buffer */
4032 char *dstptr
; /* Pointer into destination */
4033 _cups_globals_t
*cg
= _cupsGlobals();
4037 if (*src
== '<' && src
[strlen(src
) - 1] == '>')
4040 * Map <filename> to CUPS_DATADIR/ipptool/filename...
4043 snprintf(dst
, dstsize
, "%s/ipptool/%s", cg
->cups_datadir
, src
+ 1);
4044 dstptr
= dst
+ strlen(dst
) - 1;
4048 else if (*src
== '/' || !strchr(testfile
, '/')
4050 || (isalpha(*src
& 255) && src
[1] == ':')
4055 * Use the path as-is...
4058 strlcpy(dst
, src
, dstsize
);
4063 * Make path relative to testfile...
4066 strlcpy(dst
, testfile
, dstsize
);
4067 if ((dstptr
= strrchr(dst
, '/')) != NULL
)
4070 dstptr
= dst
; /* Should never happen */
4072 strlcpy(dstptr
, src
, dstsize
- (size_t)(dstptr
- dst
));
4080 * 'get_string()' - Get a pointer to a string value or the portion of interest.
4083 static const char * /* O - Pointer to string */
4084 get_string(ipp_attribute_t
*attr
, /* I - IPP attribute */
4085 int element
, /* I - Element to fetch */
4086 int flags
, /* I - Value ("with") flags */
4087 char *buffer
, /* I - Temporary buffer */
4088 size_t bufsize
) /* I - Size of temporary buffer */
4090 const char *value
; /* Value */
4091 char *ptr
, /* Pointer into value */
4092 scheme
[256], /* URI scheme */
4093 userpass
[256], /* Username/password */
4094 hostname
[256], /* Hostname */
4095 resource
[1024]; /* Resource */
4096 int port
; /* Port number */
4099 value
= ippGetString(attr
, element
, NULL
);
4101 if (flags
& _CUPS_WITH_HOSTNAME
)
4103 if (httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), buffer
, (int)bufsize
, &port
, resource
, sizeof(resource
)) < HTTP_URI_STATUS_OK
)
4106 ptr
= buffer
+ strlen(buffer
) - 1;
4107 if (ptr
>= buffer
&& *ptr
== '.')
4108 *ptr
= '\0'; /* Drop trailing "." */
4112 else if (flags
& _CUPS_WITH_RESOURCE
)
4114 if (httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), hostname
, sizeof(hostname
), &port
, buffer
, (int)bufsize
) < HTTP_URI_STATUS_OK
)
4119 else if (flags
& _CUPS_WITH_SCHEME
)
4121 if (httpSeparateURI(HTTP_URI_CODING_ALL
, value
, buffer
, (int)bufsize
, userpass
, sizeof(userpass
), hostname
, sizeof(hostname
), &port
, resource
, sizeof(resource
)) < HTTP_URI_STATUS_OK
)
4126 else if (ippGetValueTag(attr
) == IPP_TAG_URI
&& (!strncmp(value
, "ipp://", 6) || !strncmp(value
, "http://", 7) || !strncmp(value
, "ipps://", 7) || !strncmp(value
, "https://", 8)))
4128 http_uri_status_t status
= httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), hostname
, sizeof(hostname
), &port
, resource
, sizeof(resource
));
4130 if (status
< HTTP_URI_STATUS_OK
)
4141 * Normalize URI with no trailing dot...
4144 if ((ptr
= hostname
+ strlen(hostname
) - 1) >= hostname
&& *ptr
== '.')
4147 httpAssembleURI(HTTP_URI_CODING_ALL
, buffer
, (int)bufsize
, scheme
, userpass
, hostname
, port
, resource
);
4158 * 'get_token()' - Get a token from a file.
4161 static char * /* O - Token from file or NULL on EOF */
4162 get_token(cups_file_t
*fp
, /* I - File to read from */
4163 char *buf
, /* I - Buffer to read into */
4164 int buflen
, /* I - Length of buffer */
4165 int *linenum
) /* IO - Current line number */
4167 int ch
, /* Character from file */
4168 quote
; /* Quoting character */
4169 char *bufptr
, /* Pointer into buffer */
4170 *bufend
; /* End of buffer */
4176 * Skip whitespace...
4179 while (isspace(ch
= cupsFileGetChar(fp
)))
4191 else if (ch
== '\'' || ch
== '\"')
4194 * Quoted text or regular expression...
4199 bufend
= buf
+ buflen
- 1;
4201 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
4206 * Escape next character...
4209 if (bufptr
< bufend
)
4210 *bufptr
++ = (char)ch
;
4212 if ((ch
= cupsFileGetChar(fp
)) != EOF
&& bufptr
< bufend
)
4213 *bufptr
++ = (char)ch
;
4215 else if (ch
== quote
)
4217 else if (bufptr
< bufend
)
4218 *bufptr
++ = (char)ch
;
4231 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
4237 else if (ch
== '{' || ch
== '}' || ch
== ',')
4247 * Whitespace delimited text...
4250 cupsFileSeek(fp
, cupsFileTell(fp
) - 1);
4253 bufend
= buf
+ buflen
- 1;
4255 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
4256 if (isspace(ch
) || ch
== '#')
4258 else if (bufptr
< bufend
)
4259 *bufptr
++ = (char)ch
;
4262 cupsFileSeek(fp
, cupsFileTell(fp
) - 1);
4263 else if (ch
== '\n')
4275 * 'get_variable()' - Get the value of a variable.
4278 static char * /* O - Value or NULL */
4279 get_variable(_cups_vars_t
*vars
, /* I - Variables */
4280 const char *name
) /* I - Variable name */
4282 _cups_var_t key
, /* Search key */
4283 *match
; /* Matching variable, if any */
4286 key
.name
= (char *)name
;
4287 match
= cupsArrayFind(vars
->vars
, &key
);
4289 return (match
? match
->value
: NULL
);
4294 * 'iso_date()' - Return an ISO 8601 date/time string for the given IPP dateTime
4298 static char * /* O - ISO 8601 date/time string */
4299 iso_date(ipp_uchar_t
*date
) /* I - IPP (RFC 1903) date/time value */
4301 time_t utctime
; /* UTC time since 1970 */
4302 struct tm
*utcdate
; /* UTC date/time */
4303 static char buffer
[255]; /* String buffer */
4306 utctime
= ippDateToTime(date
);
4307 utcdate
= gmtime(&utctime
);
4309 snprintf(buffer
, sizeof(buffer
), "%04d-%02d-%02dT%02d:%02d:%02dZ",
4310 utcdate
->tm_year
+ 1900, utcdate
->tm_mon
+ 1, utcdate
->tm_mday
,
4311 utcdate
->tm_hour
, utcdate
->tm_min
, utcdate
->tm_sec
);
4318 * 'password_cb()' - Password callback for authenticated tests.
4321 static const char * /* O - Password */
4322 password_cb(const char *prompt
) /* I - Prompt (unused) */
4326 if (PasswordTries
< 3)
4330 cupsSetUser(Username
);
4340 * 'pause_message()' - Display the message and pause until the user presses a key.
4344 pause_message(const char *message
) /* I - Message */
4347 HANDLE tty
; /* Console handle */
4348 DWORD mode
; /* Console mode */
4349 char key
; /* Key press */
4350 DWORD bytes
; /* Bytes read for key press */
4354 * Disable input echo and set raw input...
4357 if ((tty
= GetStdHandle(STD_INPUT_HANDLE
)) == INVALID_HANDLE_VALUE
)
4360 if (!GetConsoleMode(tty
, &mode
))
4363 if (!SetConsoleMode(tty
, 0))
4367 int tty
; /* /dev/tty - never read from stdin */
4368 struct termios original
, /* Original input mode */
4369 noecho
; /* No echo input mode */
4370 char key
; /* Current key press */
4374 * Disable input echo and set raw input...
4377 if ((tty
= open("/dev/tty", O_RDONLY
)) < 0)
4380 if (tcgetattr(tty
, &original
))
4387 noecho
.c_lflag
&= (tcflag_t
)~(ICANON
| ECHO
| ECHOE
| ISIG
);
4389 if (tcsetattr(tty
, TCSAFLUSH
, &noecho
))
4397 * Display the prompt...
4400 cupsFilePrintf(cupsFileStdout(), "%s\n---- PRESS ANY KEY ----", message
);
4407 ReadFile(tty
, &key
, 1, &bytes
, NULL
);
4413 SetConsoleMode(tty
, mode
);
4426 tcsetattr(tty
, TCSAFLUSH
, &original
);
4431 * Erase the "press any key" prompt...
4434 cupsFilePuts(cupsFileStdout(), "\r \r");
4439 * 'print_attr()' - Print an attribute on the screen.
4443 print_attr(cups_file_t
*outfile
, /* I - Output file */
4444 int format
, /* I - Output format */
4445 ipp_attribute_t
*attr
, /* I - Attribute to print */
4446 ipp_tag_t
*group
) /* IO - Current group */
4448 int i
; /* Looping var */
4449 ipp_attribute_t
*colattr
; /* Collection attribute */
4452 if (format
== _CUPS_OUTPUT_PLIST
)
4454 if (!attr
->name
|| (group
&& *group
!= attr
->group_tag
))
4456 if (attr
->group_tag
!= IPP_TAG_ZERO
)
4458 cupsFilePuts(outfile
, "</dict>\n");
4459 cupsFilePuts(outfile
, "<dict>\n");
4463 *group
= attr
->group_tag
;
4469 print_xml_string(outfile
, "key", attr
->name
);
4470 if (attr
->num_values
> 1)
4471 cupsFilePuts(outfile
, "<array>\n");
4473 switch (attr
->value_tag
)
4475 case IPP_TAG_INTEGER
:
4477 for (i
= 0; i
< attr
->num_values
; i
++)
4478 cupsFilePrintf(outfile
, "<integer>%d</integer>\n", attr
->values
[i
].integer
);
4481 case IPP_TAG_BOOLEAN
:
4482 for (i
= 0; i
< attr
->num_values
; i
++)
4483 cupsFilePuts(outfile
, attr
->values
[i
].boolean
? "<true />\n" : "<false />\n");
4486 case IPP_TAG_RANGE
:
4487 for (i
= 0; i
< attr
->num_values
; i
++)
4488 cupsFilePrintf(outfile
, "<dict><key>lower</key><integer>%d</integer>"
4489 "<key>upper</key><integer>%d</integer></dict>\n",
4490 attr
->values
[i
].range
.lower
, attr
->values
[i
].range
.upper
);
4493 case IPP_TAG_RESOLUTION
:
4494 for (i
= 0; i
< attr
->num_values
; i
++)
4495 cupsFilePrintf(outfile
, "<dict><key>xres</key><integer>%d</integer>"
4496 "<key>yres</key><integer>%d</integer>"
4497 "<key>units</key><string>%s</string></dict>\n",
4498 attr
->values
[i
].resolution
.xres
,
4499 attr
->values
[i
].resolution
.yres
,
4500 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
4505 for (i
= 0; i
< attr
->num_values
; i
++)
4506 cupsFilePrintf(outfile
, "<date>%s</date>\n", iso_date(attr
->values
[i
].date
));
4509 case IPP_TAG_STRING
:
4510 for (i
= 0; i
< attr
->num_values
; i
++)
4512 char buffer
[IPP_MAX_LENGTH
* 5 / 4 + 1];
4515 cupsFilePrintf(outfile
, "<data>%s</data>\n",
4516 httpEncode64_2(buffer
, sizeof(buffer
),
4517 attr
->values
[i
].unknown
.data
,
4518 attr
->values
[i
].unknown
.length
));
4524 case IPP_TAG_KEYWORD
:
4526 case IPP_TAG_URISCHEME
:
4527 case IPP_TAG_CHARSET
:
4528 case IPP_TAG_LANGUAGE
:
4529 case IPP_TAG_MIMETYPE
:
4530 for (i
= 0; i
< attr
->num_values
; i
++)
4531 print_xml_string(outfile
, "string", attr
->values
[i
].string
.text
);
4534 case IPP_TAG_TEXTLANG
:
4535 case IPP_TAG_NAMELANG
:
4536 for (i
= 0; i
< attr
->num_values
; i
++)
4538 cupsFilePuts(outfile
, "<dict><key>language</key><string>");
4539 print_xml_string(outfile
, NULL
, attr
->values
[i
].string
.language
);
4540 cupsFilePuts(outfile
, "</string><key>string</key><string>");
4541 print_xml_string(outfile
, NULL
, attr
->values
[i
].string
.text
);
4542 cupsFilePuts(outfile
, "</string></dict>\n");
4546 case IPP_TAG_BEGIN_COLLECTION
:
4547 for (i
= 0; i
< attr
->num_values
; i
++)
4549 cupsFilePuts(outfile
, "<dict>\n");
4550 for (colattr
= attr
->values
[i
].collection
->attrs
;
4552 colattr
= colattr
->next
)
4553 print_attr(outfile
, format
, colattr
, NULL
);
4554 cupsFilePuts(outfile
, "</dict>\n");
4559 cupsFilePrintf(outfile
, "<string><<%s>></string>\n", ippTagString(attr
->value_tag
));
4563 if (attr
->num_values
> 1)
4564 cupsFilePuts(outfile
, "</array>\n");
4568 char buffer
[131072]; /* Value buffer */
4570 if (format
== _CUPS_OUTPUT_TEST
)
4574 cupsFilePuts(outfile
, " -- separator --\n");
4578 cupsFilePrintf(outfile
, " %s (%s%s) = ", attr
->name
, attr
->num_values
> 1 ? "1setOf " : "", ippTagString(attr
->value_tag
));
4581 ippAttributeString(attr
, buffer
, sizeof(buffer
));
4582 cupsFilePrintf(outfile
, "%s\n", buffer
);
4588 * 'print_csv()' - Print a line of CSV text.
4593 cups_file_t
*outfile
, /* I - Output file */
4594 ipp_attribute_t
*attr
, /* I - First attribute for line */
4595 int num_displayed
, /* I - Number of attributes to display */
4596 char **displayed
, /* I - Attributes to display */
4597 size_t *widths
) /* I - Column widths */
4599 int i
; /* Looping var */
4600 size_t maxlength
; /* Max length of all columns */
4601 char *buffer
, /* String buffer */
4602 *bufptr
; /* Pointer into buffer */
4603 ipp_attribute_t
*current
; /* Current attribute */
4607 * Get the maximum string length we have to show and allocate...
4610 for (i
= 1, maxlength
= widths
[0]; i
< num_displayed
; i
++)
4611 if (widths
[i
] > maxlength
)
4612 maxlength
= widths
[i
];
4616 if ((buffer
= malloc(maxlength
)) == NULL
)
4620 * Loop through the attributes to display...
4625 for (i
= 0; i
< num_displayed
; i
++)
4628 cupsFilePutChar(outfile
, ',');
4632 for (current
= attr
; current
; current
= current
->next
)
4636 else if (!strcmp(current
->name
, displayed
[i
]))
4638 ippAttributeString(current
, buffer
, maxlength
);
4643 if (strchr(buffer
, ',') != NULL
|| strchr(buffer
, '\"') != NULL
||
4644 strchr(buffer
, '\\') != NULL
)
4646 cupsFilePutChar(cupsFileStdout(), '\"');
4647 for (bufptr
= buffer
; *bufptr
; bufptr
++)
4649 if (*bufptr
== '\\' || *bufptr
== '\"')
4650 cupsFilePutChar(cupsFileStdout(), '\\');
4651 cupsFilePutChar(cupsFileStdout(), *bufptr
);
4653 cupsFilePutChar(cupsFileStdout(), '\"');
4656 cupsFilePuts(outfile
, buffer
);
4658 cupsFilePutChar(cupsFileStdout(), '\n');
4662 for (i
= 0; i
< num_displayed
; i
++)
4665 cupsFilePutChar(cupsFileStdout(), ',');
4667 cupsFilePuts(outfile
, displayed
[i
]);
4669 cupsFilePutChar(cupsFileStdout(), '\n');
4677 * 'print_fatal_error()' - Print a fatal error message.
4681 print_fatal_error(cups_file_t
*outfile
, /* I - Output file */
4682 const char *s
, /* I - Printf-style format string */
4683 ...) /* I - Additional arguments as needed */
4685 char buffer
[10240]; /* Format buffer */
4686 va_list ap
; /* Pointer to arguments */
4690 * Format the error message...
4694 vsnprintf(buffer
, sizeof(buffer
), s
, ap
);
4701 if (Output
== _CUPS_OUTPUT_PLIST
)
4703 print_xml_header(outfile
);
4704 print_xml_trailer(outfile
, 0, buffer
);
4707 _cupsLangPrintf(stderr
, "ipptool: %s", buffer
);
4712 * 'print_line()' - Print a line of formatted or CSV text.
4717 cups_file_t
*outfile
, /* I - Output file */
4718 ipp_attribute_t
*attr
, /* I - First attribute for line */
4719 int num_displayed
, /* I - Number of attributes to display */
4720 char **displayed
, /* I - Attributes to display */
4721 size_t *widths
) /* I - Column widths */
4723 int i
; /* Looping var */
4724 size_t maxlength
; /* Max length of all columns */
4725 char *buffer
; /* String buffer */
4726 ipp_attribute_t
*current
; /* Current attribute */
4730 * Get the maximum string length we have to show and allocate...
4733 for (i
= 1, maxlength
= widths
[0]; i
< num_displayed
; i
++)
4734 if (widths
[i
] > maxlength
)
4735 maxlength
= widths
[i
];
4739 if ((buffer
= malloc(maxlength
)) == NULL
)
4743 * Loop through the attributes to display...
4748 for (i
= 0; i
< num_displayed
; i
++)
4751 cupsFilePutChar(cupsFileStdout(), ' ');
4755 for (current
= attr
; current
; current
= current
->next
)
4759 else if (!strcmp(current
->name
, displayed
[i
]))
4761 ippAttributeString(current
, buffer
, maxlength
);
4766 cupsFilePrintf(outfile
, "%*s", (int)-widths
[i
], buffer
);
4768 cupsFilePutChar(cupsFileStdout(), '\n');
4772 for (i
= 0; i
< num_displayed
; i
++)
4775 cupsFilePutChar(cupsFileStdout(), ' ');
4777 cupsFilePrintf(outfile
, "%*s", (int)-widths
[i
], displayed
[i
]);
4779 cupsFilePutChar(cupsFileStdout(), '\n');
4781 for (i
= 0; i
< num_displayed
; i
++)
4784 cupsFilePutChar(cupsFileStdout(), ' ');
4786 memset(buffer
, '-', widths
[i
]);
4787 buffer
[widths
[i
]] = '\0';
4788 cupsFilePuts(outfile
, buffer
);
4790 cupsFilePutChar(cupsFileStdout(), '\n');
4798 * 'print_xml_header()' - Print a standard XML plist header.
4802 print_xml_header(cups_file_t
*outfile
) /* I - Output file */
4806 cupsFilePuts(outfile
, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
4807 cupsFilePuts(outfile
, "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
4808 "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n");
4809 cupsFilePuts(outfile
, "<plist version=\"1.0\">\n");
4810 cupsFilePuts(outfile
, "<dict>\n");
4811 cupsFilePuts(outfile
, "<key>ipptoolVersion</key>\n");
4812 cupsFilePuts(outfile
, "<string>" CUPS_SVERSION
"</string>\n");
4813 cupsFilePuts(outfile
, "<key>Transfer</key>\n");
4814 cupsFilePrintf(outfile
, "<string>%s</string>\n",
4815 Transfer
== _CUPS_TRANSFER_AUTO
? "auto" :
4816 Transfer
== _CUPS_TRANSFER_CHUNKED
? "chunked" : "length");
4817 cupsFilePuts(outfile
, "<key>Tests</key>\n");
4818 cupsFilePuts(outfile
, "<array>\n");
4826 * 'print_xml_string()' - Print an XML string with escaping.
4830 print_xml_string(cups_file_t
*outfile
, /* I - Output file */
4831 const char *element
, /* I - Element name or NULL */
4832 const char *s
) /* I - String to print */
4835 cupsFilePrintf(outfile
, "<%s>", element
);
4840 cupsFilePuts(outfile
, "&");
4842 cupsFilePuts(outfile
, "<");
4844 cupsFilePuts(outfile
, ">");
4845 else if ((*s
& 0xe0) == 0xc0)
4848 * Validate UTF-8 two-byte sequence...
4851 if ((s
[1] & 0xc0) != 0x80)
4853 cupsFilePutChar(cupsFileStdout(), '?');
4858 cupsFilePutChar(cupsFileStdout(), *s
++);
4859 cupsFilePutChar(cupsFileStdout(), *s
);
4862 else if ((*s
& 0xf0) == 0xe0)
4865 * Validate UTF-8 three-byte sequence...
4868 if ((s
[1] & 0xc0) != 0x80 || (s
[2] & 0xc0) != 0x80)
4870 cupsFilePutChar(cupsFileStdout(), '?');
4875 cupsFilePutChar(cupsFileStdout(), *s
++);
4876 cupsFilePutChar(cupsFileStdout(), *s
++);
4877 cupsFilePutChar(cupsFileStdout(), *s
);
4880 else if ((*s
& 0xf8) == 0xf0)
4883 * Validate UTF-8 four-byte sequence...
4886 if ((s
[1] & 0xc0) != 0x80 || (s
[2] & 0xc0) != 0x80 ||
4887 (s
[3] & 0xc0) != 0x80)
4889 cupsFilePutChar(cupsFileStdout(), '?');
4894 cupsFilePutChar(cupsFileStdout(), *s
++);
4895 cupsFilePutChar(cupsFileStdout(), *s
++);
4896 cupsFilePutChar(cupsFileStdout(), *s
++);
4897 cupsFilePutChar(cupsFileStdout(), *s
);
4900 else if ((*s
& 0x80) || (*s
< ' ' && !isspace(*s
& 255)))
4903 * Invalid control character...
4906 cupsFilePutChar(cupsFileStdout(), '?');
4909 cupsFilePutChar(cupsFileStdout(), *s
);
4915 cupsFilePrintf(outfile
, "</%s>\n", element
);
4920 * 'print_xml_trailer()' - Print the XML trailer with success/fail value.
4924 print_xml_trailer(cups_file_t
*outfile
, /* I - Output file */
4925 int success
, /* I - 1 on success, 0 on failure */
4926 const char *message
) /* I - Error message or NULL */
4930 cupsFilePuts(outfile
, "</array>\n");
4931 cupsFilePuts(outfile
, "<key>Successful</key>\n");
4932 cupsFilePuts(outfile
, success
? "<true />\n" : "<false />\n");
4935 cupsFilePuts(outfile
, "<key>ErrorMessage</key>\n");
4936 print_xml_string(outfile
, "string", message
);
4938 cupsFilePuts(outfile
, "</dict>\n");
4939 cupsFilePuts(outfile
, "</plist>\n");
4947 * 'set_variable()' - Set a variable value.
4951 set_variable(cups_file_t
*outfile
, /* I - Output file */
4952 _cups_vars_t
*vars
, /* I - Variables */
4953 const char *name
, /* I - Variable name */
4954 const char *value
) /* I - Value string */
4956 _cups_var_t key
, /* Search key */
4957 *var
; /* New variable */
4960 if (!_cups_strcasecmp(name
, "filename"))
4963 free(vars
->filename
);
4965 vars
->filename
= strdup(value
);
4969 key
.name
= (char *)name
;
4970 if ((var
= cupsArrayFind(vars
->vars
, &key
)) != NULL
)
4973 var
->value
= strdup(value
);
4975 else if ((var
= malloc(sizeof(_cups_var_t
))) == NULL
)
4977 print_fatal_error(outfile
, "Unable to allocate memory for variable \"%s\".", name
);
4982 var
->name
= strdup(name
);
4983 var
->value
= strdup(value
);
4985 cupsArrayAdd(vars
->vars
, var
);
4992 * 'sigterm_handler()' - Handle SIGINT and SIGTERM.
4996 sigterm_handler(int sig
) /* I - Signal number (unused) */
5002 signal(SIGINT
, SIG_DFL
);
5003 signal(SIGTERM
, SIG_DFL
);
5009 * 'timeout_cb()' - Handle HTTP timeouts.
5012 static int /* O - 1 to continue, 0 to cancel */
5013 timeout_cb(http_t
*http
, /* I - Connection to server */
5014 void *user_data
) /* I - User data (unused) */
5016 int buffered
= 0; /* Bytes buffered but not yet sent */
5022 * If the socket still have data waiting to be sent to the printer (as can
5023 * happen if the printer runs out of paper), continue to wait until the output
5024 * buffer is empty...
5027 #ifdef SO_NWRITE /* macOS and some versions of Linux */
5028 socklen_t len
= sizeof(buffered
); /* Size of return value */
5030 if (getsockopt(httpGetFd(http
), SOL_SOCKET
, SO_NWRITE
, &buffered
, &len
))
5033 #elif defined(SIOCOUTQ) /* Others except Windows */
5034 if (ioctl(httpGetFd(http
), SIOCOUTQ
, &buffered
))
5037 #else /* Windows (not possible) */
5039 #endif /* SO_NWRITE */
5041 return (buffered
> 0);
5046 * 'usage()' - Show program usage.
5052 _cupsLangPuts(stderr
, _("Usage: ipptool [options] URI filename [ ... filenameN ]"));
5053 _cupsLangPuts(stderr
, _("Options:"));
5054 _cupsLangPuts(stderr
, _(" --help Show help."));
5055 _cupsLangPuts(stderr
, _(" --stop-after-include-error\n"
5056 " Stop tests after a failed INCLUDE."));
5057 _cupsLangPuts(stderr
, _(" --version Show version."));
5058 _cupsLangPuts(stderr
, _(" -4 Connect using IPv4."));
5059 _cupsLangPuts(stderr
, _(" -6 Connect using IPv6."));
5060 _cupsLangPuts(stderr
, _(" -C Send requests using "
5061 "chunking (default)."));
5062 _cupsLangPuts(stderr
, _(" -E Test with encryption using HTTP Upgrade to TLS."));
5063 _cupsLangPuts(stderr
, _(" -I Ignore errors."));
5064 _cupsLangPuts(stderr
, _(" -L Send requests using content-length."));
5065 _cupsLangPuts(stderr
, _(" -P filename.plist Produce XML plist to a file and test report to standard output."));
5066 _cupsLangPuts(stderr
, _(" -S Test with encryption using HTTPS."));
5067 _cupsLangPuts(stderr
, _(" -T seconds Set the receive/send timeout in seconds."));
5068 _cupsLangPuts(stderr
, _(" -V version Set default IPP version."));
5069 _cupsLangPuts(stderr
, _(" -X Produce XML plist instead of plain text."));
5070 _cupsLangPuts(stderr
, _(" -c Produce CSV output."));
5071 _cupsLangPuts(stderr
, _(" -d name=value Set named variable to value."));
5072 _cupsLangPuts(stderr
, _(" -f filename Set default request filename."));
5073 _cupsLangPuts(stderr
, _(" -i seconds Repeat the last file with the given time interval."));
5074 _cupsLangPuts(stderr
, _(" -l Produce plain text output."));
5075 _cupsLangPuts(stderr
, _(" -n count Repeat the last file the given number of times."));
5076 _cupsLangPuts(stderr
, _(" -q Run silently."));
5077 _cupsLangPuts(stderr
, _(" -t Produce a test report."));
5078 _cupsLangPuts(stderr
, _(" -v Be verbose."));
5085 * 'validate_attr()' - Determine whether an attribute is valid.
5088 static int /* O - 1 if valid, 0 otherwise */
5089 validate_attr(cups_file_t
*outfile
, /* I - Output file */
5090 cups_array_t
*errors
, /* I - Errors array */
5091 ipp_attribute_t
*attr
) /* I - Attribute to validate */
5093 int i
; /* Looping var */
5094 char scheme
[64], /* Scheme from URI */
5095 userpass
[256], /* Username/password from URI */
5096 hostname
[256], /* Hostname from URI */
5097 resource
[1024]; /* Resource from URI */
5098 int port
, /* Port number from URI */
5099 uri_status
, /* URI separation status */
5100 valid
= 1; /* Is the attribute valid? */
5101 const char *ptr
; /* Pointer into string */
5102 ipp_attribute_t
*colattr
; /* Collection attribute */
5103 regex_t re
; /* Regular expression */
5104 ipp_uchar_t
*date
; /* Current date value */
5115 * Validate the attribute name.
5118 for (ptr
= attr
->name
; *ptr
; ptr
++)
5119 if (!isalnum(*ptr
& 255) && *ptr
!= '-' && *ptr
!= '.' && *ptr
!= '_')
5122 if (*ptr
|| ptr
== attr
->name
)
5127 "\"%s\": Bad attribute name - invalid character "
5128 "(RFC 2911 section 4.1.3).", attr
->name
);
5131 if ((ptr
- attr
->name
) > 255)
5136 "\"%s\": Bad attribute name - bad length "
5137 "(RFC 2911 section 4.1.3).", attr
->name
);
5140 switch (attr
->value_tag
)
5142 case IPP_TAG_INTEGER
:
5145 case IPP_TAG_BOOLEAN
:
5146 for (i
= 0; i
< attr
->num_values
; i
++)
5148 if (attr
->values
[i
].boolean
!= 0 &&
5149 attr
->values
[i
].boolean
!= 1)
5154 "\"%s\": Bad boolen value %d "
5155 "(RFC 2911 section 4.1.11).", attr
->name
,
5156 attr
->values
[i
].boolean
);
5162 for (i
= 0; i
< attr
->num_values
; i
++)
5164 if (attr
->values
[i
].integer
< 1)
5169 "\"%s\": Bad enum value %d - out of range "
5170 "(RFC 2911 section 4.1.4).", attr
->name
,
5171 attr
->values
[i
].integer
);
5176 case IPP_TAG_STRING
:
5177 for (i
= 0; i
< attr
->num_values
; i
++)
5179 if (attr
->values
[i
].unknown
.length
> IPP_MAX_OCTETSTRING
)
5184 "\"%s\": Bad octetString value - bad length %d "
5185 "(RFC 2911 section 4.1.10).", attr
->name
,
5186 attr
->values
[i
].unknown
.length
);
5192 for (i
= 0; i
< attr
->num_values
; i
++)
5194 date
= attr
->values
[i
].date
;
5196 if (date
[2] < 1 || date
[2] > 12)
5201 "\"%s\": Bad dateTime month %u "
5202 "(RFC 2911 section 4.1.14).", attr
->name
, date
[2]);
5205 if (date
[3] < 1 || date
[3] > 31)
5210 "\"%s\": Bad dateTime day %u "
5211 "(RFC 2911 section 4.1.14).", attr
->name
, date
[3]);
5219 "\"%s\": Bad dateTime hours %u "
5220 "(RFC 2911 section 4.1.14).", attr
->name
, date
[4]);
5228 "\"%s\": Bad dateTime minutes %u "
5229 "(RFC 2911 section 4.1.14).", attr
->name
, date
[5]);
5237 "\"%s\": Bad dateTime seconds %u "
5238 "(RFC 2911 section 4.1.14).", attr
->name
, date
[6]);
5246 "\"%s\": Bad dateTime deciseconds %u "
5247 "(RFC 2911 section 4.1.14).", attr
->name
, date
[7]);
5250 if (date
[8] != '-' && date
[8] != '+')
5255 "\"%s\": Bad dateTime UTC sign '%c' "
5256 "(RFC 2911 section 4.1.14).", attr
->name
, date
[8]);
5264 "\"%s\": Bad dateTime UTC hours %u "
5265 "(RFC 2911 section 4.1.14).", attr
->name
, date
[9]);
5273 "\"%s\": Bad dateTime UTC minutes %u "
5274 "(RFC 2911 section 4.1.14).", attr
->name
, date
[10]);
5279 case IPP_TAG_RESOLUTION
:
5280 for (i
= 0; i
< attr
->num_values
; i
++)
5282 if (attr
->values
[i
].resolution
.xres
<= 0)
5287 "\"%s\": Bad resolution value %dx%d%s - cross "
5288 "feed resolution must be positive "
5289 "(RFC 2911 section 4.1.15).", attr
->name
,
5290 attr
->values
[i
].resolution
.xres
,
5291 attr
->values
[i
].resolution
.yres
,
5292 attr
->values
[i
].resolution
.units
==
5293 IPP_RES_PER_INCH
? "dpi" :
5294 attr
->values
[i
].resolution
.units
==
5295 IPP_RES_PER_CM
? "dpcm" : "unknown");
5298 if (attr
->values
[i
].resolution
.yres
<= 0)
5303 "\"%s\": Bad resolution value %dx%d%s - feed "
5304 "resolution must be positive "
5305 "(RFC 2911 section 4.1.15).", attr
->name
,
5306 attr
->values
[i
].resolution
.xres
,
5307 attr
->values
[i
].resolution
.yres
,
5308 attr
->values
[i
].resolution
.units
==
5309 IPP_RES_PER_INCH
? "dpi" :
5310 attr
->values
[i
].resolution
.units
==
5311 IPP_RES_PER_CM
? "dpcm" : "unknown");
5314 if (attr
->values
[i
].resolution
.units
!= IPP_RES_PER_INCH
&&
5315 attr
->values
[i
].resolution
.units
!= IPP_RES_PER_CM
)
5320 "\"%s\": Bad resolution value %dx%d%s - bad "
5321 "units value (RFC 2911 section 4.1.15).",
5322 attr
->name
, attr
->values
[i
].resolution
.xres
,
5323 attr
->values
[i
].resolution
.yres
,
5324 attr
->values
[i
].resolution
.units
==
5325 IPP_RES_PER_INCH
? "dpi" :
5326 attr
->values
[i
].resolution
.units
==
5327 IPP_RES_PER_CM
? "dpcm" : "unknown");
5332 case IPP_TAG_RANGE
:
5333 for (i
= 0; i
< attr
->num_values
; i
++)
5335 if (attr
->values
[i
].range
.lower
> attr
->values
[i
].range
.upper
)
5340 "\"%s\": Bad rangeOfInteger value %d-%d - lower "
5341 "greater than upper (RFC 2911 section 4.1.13).",
5342 attr
->name
, attr
->values
[i
].range
.lower
,
5343 attr
->values
[i
].range
.upper
);
5348 case IPP_TAG_BEGIN_COLLECTION
:
5349 for (i
= 0; i
< attr
->num_values
; i
++)
5351 for (colattr
= attr
->values
[i
].collection
->attrs
;
5353 colattr
= colattr
->next
)
5355 if (!validate_attr(outfile
, NULL
, colattr
))
5362 if (colattr
&& errors
)
5364 add_stringf(errors
, "\"%s\": Bad collection value.", attr
->name
);
5368 validate_attr(outfile
, errors
, colattr
);
5369 colattr
= colattr
->next
;
5376 case IPP_TAG_TEXTLANG
:
5377 for (i
= 0; i
< attr
->num_values
; i
++)
5379 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
5381 if ((*ptr
& 0xe0) == 0xc0)
5384 if ((*ptr
& 0xc0) != 0x80)
5387 else if ((*ptr
& 0xf0) == 0xe0)
5390 if ((*ptr
& 0xc0) != 0x80)
5393 if ((*ptr
& 0xc0) != 0x80)
5396 else if ((*ptr
& 0xf8) == 0xf0)
5399 if ((*ptr
& 0xc0) != 0x80)
5402 if ((*ptr
& 0xc0) != 0x80)
5405 if ((*ptr
& 0xc0) != 0x80)
5408 else if (*ptr
& 0x80)
5417 "\"%s\": Bad text value \"%s\" - bad UTF-8 "
5418 "sequence (RFC 2911 section 4.1.1).", attr
->name
,
5419 attr
->values
[i
].string
.text
);
5422 if ((ptr
- attr
->values
[i
].string
.text
) > (IPP_MAX_TEXT
- 1))
5427 "\"%s\": Bad text value \"%s\" - bad length %d "
5428 "(RFC 2911 section 4.1.1).", attr
->name
,
5429 attr
->values
[i
].string
.text
,
5430 (int)strlen(attr
->values
[i
].string
.text
));
5436 case IPP_TAG_NAMELANG
:
5437 for (i
= 0; i
< attr
->num_values
; i
++)
5439 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
5441 if ((*ptr
& 0xe0) == 0xc0)
5444 if ((*ptr
& 0xc0) != 0x80)
5447 else if ((*ptr
& 0xf0) == 0xe0)
5450 if ((*ptr
& 0xc0) != 0x80)
5453 if ((*ptr
& 0xc0) != 0x80)
5456 else if ((*ptr
& 0xf8) == 0xf0)
5459 if ((*ptr
& 0xc0) != 0x80)
5462 if ((*ptr
& 0xc0) != 0x80)
5465 if ((*ptr
& 0xc0) != 0x80)
5468 else if (*ptr
& 0x80)
5477 "\"%s\": Bad name value \"%s\" - bad UTF-8 "
5478 "sequence (RFC 2911 section 4.1.2).", attr
->name
,
5479 attr
->values
[i
].string
.text
);
5482 if ((ptr
- attr
->values
[i
].string
.text
) > (IPP_MAX_NAME
- 1))
5487 "\"%s\": Bad name value \"%s\" - bad length %d "
5488 "(RFC 2911 section 4.1.2).", attr
->name
,
5489 attr
->values
[i
].string
.text
,
5490 (int)strlen(attr
->values
[i
].string
.text
));
5495 case IPP_TAG_KEYWORD
:
5496 for (i
= 0; i
< attr
->num_values
; i
++)
5498 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
5499 if (!isalnum(*ptr
& 255) && *ptr
!= '-' && *ptr
!= '.' &&
5503 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
5508 "\"%s\": Bad keyword value \"%s\" - invalid "
5509 "character (RFC 2911 section 4.1.3).",
5510 attr
->name
, attr
->values
[i
].string
.text
);
5513 if ((ptr
- attr
->values
[i
].string
.text
) > (IPP_MAX_KEYWORD
- 1))
5518 "\"%s\": Bad keyword value \"%s\" - bad "
5519 "length %d (RFC 2911 section 4.1.3).",
5520 attr
->name
, attr
->values
[i
].string
.text
,
5521 (int)strlen(attr
->values
[i
].string
.text
));
5527 for (i
= 0; i
< attr
->num_values
; i
++)
5529 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
,
5530 attr
->values
[i
].string
.text
,
5531 scheme
, sizeof(scheme
),
5532 userpass
, sizeof(userpass
),
5533 hostname
, sizeof(hostname
),
5534 &port
, resource
, sizeof(resource
));
5536 if (uri_status
< HTTP_URI_OK
)
5541 "\"%s\": Bad URI value \"%s\" - %s "
5542 "(RFC 2911 section 4.1.5).", attr
->name
,
5543 attr
->values
[i
].string
.text
,
5544 httpURIStatusString(uri_status
));
5547 if (strlen(attr
->values
[i
].string
.text
) > (IPP_MAX_URI
- 1))
5552 "\"%s\": Bad URI value \"%s\" - bad length %d "
5553 "(RFC 2911 section 4.1.5).", attr
->name
,
5554 attr
->values
[i
].string
.text
,
5555 (int)strlen(attr
->values
[i
].string
.text
));
5560 case IPP_TAG_URISCHEME
:
5561 for (i
= 0; i
< attr
->num_values
; i
++)
5563 ptr
= attr
->values
[i
].string
.text
;
5564 if (islower(*ptr
& 255))
5566 for (ptr
++; *ptr
; ptr
++)
5567 if (!islower(*ptr
& 255) && !isdigit(*ptr
& 255) &&
5568 *ptr
!= '+' && *ptr
!= '-' && *ptr
!= '.')
5572 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
5577 "\"%s\": Bad uriScheme value \"%s\" - bad "
5578 "characters (RFC 2911 section 4.1.6).",
5579 attr
->name
, attr
->values
[i
].string
.text
);
5582 if ((ptr
- attr
->values
[i
].string
.text
) > (IPP_MAX_URISCHEME
- 1))
5587 "\"%s\": Bad uriScheme value \"%s\" - bad "
5588 "length %d (RFC 2911 section 4.1.6).",
5589 attr
->name
, attr
->values
[i
].string
.text
,
5590 (int)strlen(attr
->values
[i
].string
.text
));
5595 case IPP_TAG_CHARSET
:
5596 for (i
= 0; i
< attr
->num_values
; i
++)
5598 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
5599 if (!isprint(*ptr
& 255) || isupper(*ptr
& 255) ||
5600 isspace(*ptr
& 255))
5603 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
5608 "\"%s\": Bad charset value \"%s\" - bad "
5609 "characters (RFC 2911 section 4.1.7).",
5610 attr
->name
, attr
->values
[i
].string
.text
);
5613 if ((ptr
- attr
->values
[i
].string
.text
) > (IPP_MAX_CHARSET
- 1))
5618 "\"%s\": Bad charset value \"%s\" - bad "
5619 "length %d (RFC 2911 section 4.1.7).",
5620 attr
->name
, attr
->values
[i
].string
.text
,
5621 (int)strlen(attr
->values
[i
].string
.text
));
5626 case IPP_TAG_LANGUAGE
:
5628 * The following regular expression is derived from the ABNF for
5629 * language tags in RFC 4646. All I can say is that this is the
5630 * easiest way to check the values...
5633 if ((i
= regcomp(&re
,
5635 "(([a-z]{2,3}(-[a-z][a-z][a-z]){0,3})|[a-z]{4,8})"
5637 "(-[a-z][a-z][a-z][a-z]){0,1}" /* script */
5638 "(-([a-z][a-z]|[0-9][0-9][0-9])){0,1}" /* region */
5639 "(-([a-z]{5,8}|[0-9][0-9][0-9]))*" /* variant */
5640 "(-[a-wy-z](-[a-z0-9]{2,8})+)*" /* extension */
5641 "(-x(-[a-z0-9]{1,8})+)*" /* privateuse */
5643 "x(-[a-z0-9]{1,8})+" /* privateuse */
5645 "[a-z]{1,3}(-[a-z][0-9]{2,8}){1,2}" /* grandfathered */
5647 REG_NOSUB
| REG_EXTENDED
)) != 0)
5649 char temp
[256]; /* Temporary error string */
5651 regerror(i
, &re
, temp
, sizeof(temp
));
5652 print_fatal_error(outfile
, "Unable to compile naturalLanguage regular "
5653 "expression: %s.", temp
);
5657 for (i
= 0; i
< attr
->num_values
; i
++)
5659 if (regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
5664 "\"%s\": Bad naturalLanguage value \"%s\" - bad "
5665 "characters (RFC 2911 section 4.1.8).",
5666 attr
->name
, attr
->values
[i
].string
.text
);
5669 if (strlen(attr
->values
[i
].string
.text
) > (IPP_MAX_LANGUAGE
- 1))
5674 "\"%s\": Bad naturalLanguage value \"%s\" - bad "
5675 "length %d (RFC 2911 section 4.1.8).",
5676 attr
->name
, attr
->values
[i
].string
.text
,
5677 (int)strlen(attr
->values
[i
].string
.text
));
5684 case IPP_TAG_MIMETYPE
:
5686 * The following regular expression is derived from the ABNF for
5687 * language tags in RFC 2045 and 4288. All I can say is that this is
5688 * the easiest way to check the values...
5691 if ((i
= regcomp(&re
,
5693 "[-a-zA-Z0-9!#$&.+^_]{1,127}" /* type-name */
5695 "[-a-zA-Z0-9!#$&.+^_]{1,127}" /* subtype-name */
5696 "(;[-a-zA-Z0-9!#$&.+^_]{1,127}=" /* parameter= */
5697 "([-a-zA-Z0-9!#$&.+^_]{1,127}|\"[^\"]*\"))*"
5700 REG_NOSUB
| REG_EXTENDED
)) != 0)
5702 char temp
[256]; /* Temporary error string */
5704 regerror(i
, &re
, temp
, sizeof(temp
));
5705 print_fatal_error(outfile
, "Unable to compile mimeMediaType regular "
5706 "expression: %s.", temp
);
5710 for (i
= 0; i
< attr
->num_values
; i
++)
5712 if (regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
5717 "\"%s\": Bad mimeMediaType value \"%s\" - bad "
5718 "characters (RFC 2911 section 4.1.9).",
5719 attr
->name
, attr
->values
[i
].string
.text
);
5722 if (strlen(attr
->values
[i
].string
.text
) > (IPP_MAX_MIMETYPE
- 1))
5727 "\"%s\": Bad mimeMediaType value \"%s\" - bad "
5728 "length %d (RFC 2911 section 4.1.9).",
5729 attr
->name
, attr
->values
[i
].string
.text
,
5730 (int)strlen(attr
->values
[i
].string
.text
));
5746 * 'with_value()' - Test a WITH-VALUE predicate.
5749 static int /* O - 1 on match, 0 on non-match */
5750 with_value(cups_file_t
*outfile
, /* I - Output file */
5751 cups_array_t
*errors
, /* I - Errors array */
5752 char *value
, /* I - Value string */
5753 int flags
, /* I - Flags for match */
5754 ipp_attribute_t
*attr
, /* I - Attribute to compare */
5755 char *matchbuf
, /* I - Buffer to hold matching value */
5756 size_t matchlen
) /* I - Length of match buffer */
5758 int i
, /* Looping var */
5760 char temp
[1024], /* Temporary value string */
5761 *valptr
; /* Pointer into value */
5765 match
= (flags
& _CUPS_WITH_ALL
) ? 1 : 0;
5768 * NULL matches everything.
5771 if (!value
|| !*value
)
5775 * Compare the value string to the attribute value.
5778 switch (attr
->value_tag
)
5780 case IPP_TAG_INTEGER
:
5782 for (i
= 0; i
< attr
->num_values
; i
++)
5784 char op
, /* Comparison operator */
5785 *nextptr
; /* Next pointer */
5786 int intvalue
, /* Integer value */
5787 valmatch
= 0; /* Does the current value match? */
5791 while (isspace(*valptr
& 255) || isdigit(*valptr
& 255) ||
5792 *valptr
== '-' || *valptr
== ',' || *valptr
== '<' ||
5793 *valptr
== '=' || *valptr
== '>')
5796 while (*valptr
&& !isdigit(*valptr
& 255) && *valptr
!= '-')
5798 if (*valptr
== '<' || *valptr
== '>' || *valptr
== '=')
5806 intvalue
= (int)strtol(valptr
, &nextptr
, 0);
5807 if (nextptr
== valptr
)
5811 if ((op
== '=' && attr
->values
[i
].integer
== intvalue
) ||
5812 (op
== '<' && attr
->values
[i
].integer
< intvalue
) ||
5813 (op
== '>' && attr
->values
[i
].integer
> intvalue
))
5816 snprintf(matchbuf
, matchlen
, "%d", attr
->values
[i
].integer
);
5823 if (flags
& _CUPS_WITH_ALL
)
5838 if (!match
&& errors
)
5840 for (i
= 0; i
< attr
->num_values
; i
++)
5841 add_stringf(errors
, "GOT: %s=%d", attr
->name
,
5842 attr
->values
[i
].integer
);
5846 case IPP_TAG_RANGE
:
5847 for (i
= 0; i
< attr
->num_values
; i
++)
5849 char op
, /* Comparison operator */
5850 *nextptr
; /* Next pointer */
5851 int intvalue
, /* Integer value */
5852 valmatch
= 0; /* Does the current value match? */
5856 while (isspace(*valptr
& 255) || isdigit(*valptr
& 255) ||
5857 *valptr
== '-' || *valptr
== ',' || *valptr
== '<' ||
5858 *valptr
== '=' || *valptr
== '>')
5861 while (*valptr
&& !isdigit(*valptr
& 255) && *valptr
!= '-')
5863 if (*valptr
== '<' || *valptr
== '>' || *valptr
== '=')
5871 intvalue
= (int)strtol(valptr
, &nextptr
, 0);
5872 if (nextptr
== valptr
)
5876 if ((op
== '=' && (attr
->values
[i
].range
.lower
== intvalue
||
5877 attr
->values
[i
].range
.upper
== intvalue
)) ||
5878 (op
== '<' && attr
->values
[i
].range
.upper
< intvalue
) ||
5879 (op
== '>' && attr
->values
[i
].range
.upper
> intvalue
))
5882 snprintf(matchbuf
, matchlen
, "%d-%d",
5883 attr
->values
[0].range
.lower
,
5884 attr
->values
[0].range
.upper
);
5891 if (flags
& _CUPS_WITH_ALL
)
5906 if (!match
&& errors
)
5908 for (i
= 0; i
< attr
->num_values
; i
++)
5909 add_stringf(errors
, "GOT: %s=%d-%d", attr
->name
,
5910 attr
->values
[i
].range
.lower
,
5911 attr
->values
[i
].range
.upper
);
5915 case IPP_TAG_BOOLEAN
:
5916 for (i
= 0; i
< attr
->num_values
; i
++)
5918 if ((!strcmp(value
, "true")) == attr
->values
[i
].boolean
)
5921 strlcpy(matchbuf
, value
, matchlen
);
5923 if (!(flags
& _CUPS_WITH_ALL
))
5929 else if (flags
& _CUPS_WITH_ALL
)
5936 if (!match
&& errors
)
5938 for (i
= 0; i
< attr
->num_values
; i
++)
5939 add_stringf(errors
, "GOT: %s=%s", attr
->name
,
5940 attr
->values
[i
].boolean
? "true" : "false");
5944 case IPP_TAG_RESOLUTION
:
5945 for (i
= 0; i
< attr
->num_values
; i
++)
5947 if (attr
->values
[i
].resolution
.xres
==
5948 attr
->values
[i
].resolution
.yres
)
5949 snprintf(temp
, sizeof(temp
), "%d%s",
5950 attr
->values
[i
].resolution
.xres
,
5951 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
5954 snprintf(temp
, sizeof(temp
), "%dx%d%s",
5955 attr
->values
[i
].resolution
.xres
,
5956 attr
->values
[i
].resolution
.yres
,
5957 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
5960 if (!strcmp(value
, temp
))
5963 strlcpy(matchbuf
, value
, matchlen
);
5965 if (!(flags
& _CUPS_WITH_ALL
))
5971 else if (flags
& _CUPS_WITH_ALL
)
5978 if (!match
&& errors
)
5980 for (i
= 0; i
< attr
->num_values
; i
++)
5982 if (attr
->values
[i
].resolution
.xres
==
5983 attr
->values
[i
].resolution
.yres
)
5984 snprintf(temp
, sizeof(temp
), "%d%s",
5985 attr
->values
[i
].resolution
.xres
,
5986 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
5989 snprintf(temp
, sizeof(temp
), "%dx%d%s",
5990 attr
->values
[i
].resolution
.xres
,
5991 attr
->values
[i
].resolution
.yres
,
5992 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
5995 if (strcmp(value
, temp
))
5996 add_stringf(errors
, "GOT: %s=%s", attr
->name
, temp
);
6001 case IPP_TAG_NOVALUE
:
6002 case IPP_TAG_UNKNOWN
:
6005 case IPP_TAG_CHARSET
:
6006 case IPP_TAG_KEYWORD
:
6007 case IPP_TAG_LANGUAGE
:
6008 case IPP_TAG_MIMETYPE
:
6010 case IPP_TAG_NAMELANG
:
6012 case IPP_TAG_TEXTLANG
:
6014 case IPP_TAG_URISCHEME
:
6015 if (flags
& _CUPS_WITH_REGEX
)
6018 * Value is an extended, case-sensitive POSIX regular expression...
6021 regex_t re
; /* Regular expression */
6023 if ((i
= regcomp(&re
, value
, REG_EXTENDED
| REG_NOSUB
)) != 0)
6025 regerror(i
, &re
, temp
, sizeof(temp
));
6027 print_fatal_error(outfile
, "Unable to compile WITH-VALUE regular expression "
6028 "\"%s\" - %s", value
, temp
);
6033 * See if ALL of the values match the given regular expression.
6036 for (i
= 0; i
< attr
->num_values
; i
++)
6038 if (!regexec(&re
, get_string(attr
, i
, flags
, temp
, sizeof(temp
)),
6043 get_string(attr
, i
, flags
, temp
, sizeof(temp
)),
6046 if (!(flags
& _CUPS_WITH_ALL
))
6052 else if (flags
& _CUPS_WITH_ALL
)
6061 else if (ippGetValueTag(attr
) == IPP_TAG_URI
)
6064 * Value is a literal URI string, see if the value(s) match...
6067 for (i
= 0; i
< attr
->num_values
; i
++)
6069 if (!compare_uris(value
, get_string(attr
, i
, flags
, temp
, sizeof(temp
))))
6073 get_string(attr
, i
, flags
, temp
, sizeof(temp
)),
6076 if (!(flags
& _CUPS_WITH_ALL
))
6082 else if (flags
& _CUPS_WITH_ALL
)
6092 * Value is a literal string, see if the value(s) match...
6095 for (i
= 0; i
< attr
->num_values
; i
++)
6097 if (!strcmp(value
, get_string(attr
, i
, flags
, temp
, sizeof(temp
))))
6101 get_string(attr
, i
, flags
, temp
, sizeof(temp
)),
6104 if (!(flags
& _CUPS_WITH_ALL
))
6110 else if (flags
& _CUPS_WITH_ALL
)
6118 if (!match
&& errors
)
6120 for (i
= 0; i
< attr
->num_values
; i
++)
6121 add_stringf(errors
, "GOT: %s=\"%s\"", attr
->name
,
6122 attr
->values
[i
].string
.text
);
6135 * 'with_value_from()' - Test a WITH-VALUE-FROM predicate.
6138 static int /* O - 1 on match, 0 on non-match */
6140 cups_array_t
*errors
, /* I - Errors array */
6141 ipp_attribute_t
*fromattr
, /* I - "From" attribute */
6142 ipp_attribute_t
*attr
, /* I - Attribute to compare */
6143 char *matchbuf
, /* I - Buffer to hold matching value */
6144 size_t matchlen
) /* I - Length of match buffer */
6146 int i
, j
, /* Looping vars */
6147 count
= ippGetCount(attr
), /* Number of attribute values */
6148 match
= 1; /* Match? */
6154 * Compare the from value(s) to the attribute value(s)...
6157 switch (ippGetValueTag(attr
))
6159 case IPP_TAG_INTEGER
:
6160 if (ippGetValueTag(fromattr
) != IPP_TAG_INTEGER
&& ippGetValueTag(fromattr
) != IPP_TAG_RANGE
)
6161 goto wrong_value_tag
;
6163 for (i
= 0; i
< count
; i
++)
6165 int value
= ippGetInteger(attr
, i
);
6166 /* Current integer value */
6168 if (ippContainsInteger(fromattr
, value
))
6171 snprintf(matchbuf
, matchlen
, "%d", value
);
6175 add_stringf(errors
, "GOT: %s=%d", ippGetName(attr
), value
);
6182 if (ippGetValueTag(fromattr
) != IPP_TAG_ENUM
)
6183 goto wrong_value_tag
;
6185 for (i
= 0; i
< count
; i
++)
6187 int value
= ippGetInteger(attr
, i
);
6188 /* Current integer value */
6190 if (ippContainsInteger(fromattr
, value
))
6193 snprintf(matchbuf
, matchlen
, "%d", value
);
6197 add_stringf(errors
, "GOT: %s=%d", ippGetName(attr
), value
);
6203 case IPP_TAG_RESOLUTION
:
6204 if (ippGetValueTag(fromattr
) != IPP_TAG_RESOLUTION
)
6205 goto wrong_value_tag
;
6207 for (i
= 0; i
< count
; i
++)
6211 int fromcount
= ippGetCount(fromattr
);
6212 int fromxres
, fromyres
;
6213 ipp_res_t fromunits
;
6215 xres
= ippGetResolution(attr
, i
, &yres
, &units
);
6217 for (j
= 0; j
< fromcount
; j
++)
6219 fromxres
= ippGetResolution(fromattr
, j
, &fromyres
, &fromunits
);
6220 if (fromxres
== xres
&& fromyres
== yres
&& fromunits
== units
)
6229 snprintf(matchbuf
, matchlen
, "%d%s", xres
, units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
6231 snprintf(matchbuf
, matchlen
, "%dx%d%s", xres
, yres
, units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
6237 add_stringf(errors
, "GOT: %s=%d%s", ippGetName(attr
), xres
, units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
6239 add_stringf(errors
, "GOT: %s=%dx%d%s", ippGetName(attr
), xres
, yres
, units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
6246 case IPP_TAG_NOVALUE
:
6247 case IPP_TAG_UNKNOWN
:
6250 case IPP_TAG_CHARSET
:
6251 case IPP_TAG_KEYWORD
:
6252 case IPP_TAG_LANGUAGE
:
6253 case IPP_TAG_MIMETYPE
:
6255 case IPP_TAG_NAMELANG
:
6257 case IPP_TAG_TEXTLANG
:
6259 case IPP_TAG_URISCHEME
:
6260 for (i
= 0; i
< count
; i
++)
6262 const char *value
= ippGetString(attr
, i
, NULL
);
6263 /* Current string value */
6265 if (ippContainsString(fromattr
, value
))
6268 strlcpy(matchbuf
, value
, matchlen
);
6272 add_stringf(errors
, "GOT: %s='%s'", ippGetName(attr
), value
);
6285 /* value tag mismatch between fromattr and attr */
6288 add_stringf(errors
, "GOT: %s OF-TYPE %s", ippGetName(attr
), ippTagString(ippGetValueTag(attr
)));