2 * ipptool command for CUPS.
4 * Copyright 2007-2016 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_vars(_cups_var_t
*a
, _cups_var_t
*b
);
156 static int do_tests(FILE *outfile
, _cups_vars_t
*vars
, const char *testfile
);
157 static void expand_variables(_cups_vars_t
*vars
, char *dst
, const char *src
, size_t dstsize
) __attribute__((nonnull(1,2,3)));
158 static int expect_matches(_cups_expect_t
*expect
, ipp_tag_t value_tag
);
159 static ipp_t
*get_collection(FILE *outfile
, _cups_vars_t
*vars
, FILE *fp
, int *linenum
);
160 static char *get_filename(const char *testfile
, char *dst
, const char *src
, size_t dstsize
);
161 static const char *get_string(ipp_attribute_t
*attr
, int element
, int flags
, char *buffer
, size_t bufsize
);
162 static char *get_token(FILE *fp
, char *buf
, int buflen
, int *linenum
);
163 static char *get_variable(_cups_vars_t
*vars
, const char *name
);
164 static char *iso_date(ipp_uchar_t
*date
);
165 static const char *password_cb(const char *prompt
);
166 static void pause_message(const char *message
);
167 static void print_attr(FILE *outfile
, int format
, ipp_attribute_t
*attr
, ipp_tag_t
*group
);
168 static void print_csv(FILE *outfile
, ipp_attribute_t
*attr
, int num_displayed
, char **displayed
, size_t *widths
);
169 static void print_fatal_error(FILE *outfile
, const char *s
, ...) __attribute__ ((__format__ (__printf__
, 2, 3)));
170 static void print_line(FILE *outfile
, ipp_attribute_t
*attr
, int num_displayed
, char **displayed
, size_t *widths
);
171 static void print_xml_header(FILE *outfile
);
172 static void print_xml_string(FILE *outfile
, const char *element
, const char *s
);
173 static void print_xml_trailer(FILE *outfile
, int success
, const char *message
);
174 static void set_variable(FILE *outfile
, _cups_vars_t
*vars
, const char *name
, const char *value
);
176 static void sigterm_handler(int sig
);
178 static int timeout_cb(http_t
*http
, void *user_data
);
179 static void usage(void) __attribute__((noreturn
));
180 static int validate_attr(FILE *outfile
, cups_array_t
*errors
, ipp_attribute_t
*attr
);
181 static int with_value(FILE *outfile
, cups_array_t
*errors
, char *value
, int flags
, ipp_attribute_t
*attr
, char *matchbuf
, size_t matchlen
);
182 static int with_value_from(cups_array_t
*errors
, ipp_attribute_t
*fromattr
, ipp_attribute_t
*attr
, char *matchbuf
, size_t matchlen
);
186 * 'main()' - Parse options and do tests.
189 int /* O - Exit status */
190 main(int argc
, /* I - Number of command-line args */
191 char *argv
[]) /* I - Command-line arguments */
193 int i
; /* Looping var */
194 int status
; /* Status of tests... */
195 FILE *outfile
= stdout
;
197 char *opt
, /* Current option */
198 name
[1024], /* Name/value buffer */
199 *value
, /* Pointer to value */
200 filename
[1024], /* Real filename */
201 testname
[1024], /* Real test filename */
202 uri
[1024]; /* Copy of printer URI */
203 const char *ext
, /* Extension on filename */
204 *testfile
; /* Test file to use */
205 int interval
, /* Test interval in microseconds */
206 repeat
; /* Repeat count */
207 _cups_vars_t vars
; /* Variables */
208 http_uri_status_t uri_status
; /* URI separation status */
209 _cups_globals_t
*cg
= _cupsGlobals();
215 * Catch SIGINT and SIGTERM...
218 signal(SIGINT
, sigterm_handler
);
219 signal(SIGTERM
, sigterm_handler
);
223 * Initialize the locale and variables...
226 _cupsSetLocale(argv
);
228 memset(&vars
, 0, sizeof(vars
));
229 vars
.family
= AF_UNSPEC
;
230 vars
.vars
= cupsArrayNew((cups_array_func_t
)compare_vars
, NULL
);
235 * ipptool URI testfile
243 for (i
= 1; i
< argc
; i
++)
245 if (!strcmp(argv
[i
], "--help"))
249 else if (!strcmp(argv
[i
], "--stop-after-include-error"))
251 StopAfterIncludeError
= 1;
253 else if (!strcmp(argv
[i
], "--version"))
258 else if (argv
[i
][0] == '-')
260 for (opt
= argv
[i
] + 1; *opt
; opt
++)
264 case '4' : /* Connect using IPv4 only */
265 vars
.family
= AF_INET
;
269 case '6' : /* Connect using IPv6 only */
270 vars
.family
= AF_INET6
;
272 #endif /* AF_INET6 */
274 case 'C' : /* Enable HTTP chunking */
275 Transfer
= _CUPS_TRANSFER_CHUNKED
;
278 case 'E' : /* Encrypt with TLS */
280 vars
.encryption
= HTTP_ENCRYPT_REQUIRED
;
282 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."),
284 #endif /* HAVE_SSL */
287 case 'I' : /* Ignore errors */
291 case 'L' : /* Disable HTTP chunking */
292 Transfer
= _CUPS_TRANSFER_LENGTH
;
295 case 'P' : /* Output to plist file */
300 _cupsLangPrintf(stderr
, _("%s: Missing filename for \"-P\"."), "ipptool");
304 if (outfile
!= stdout
)
307 if ((outfile
= fopen(argv
[i
], "w")) == NULL
)
309 _cupsLangPrintf(stderr
, _("%s: Unable to open \"%s\": %s"), "ipptool", argv
[i
], strerror(errno
));
313 Output
= _CUPS_OUTPUT_PLIST
;
315 if (interval
|| repeat
)
317 _cupsLangPuts(stderr
, _("ipptool: \"-i\" and \"-n\" are incompatible with \"-P\" and \"-X\"."));
322 case 'S' : /* Encrypt with SSL */
324 vars
.encryption
= HTTP_ENCRYPT_ALWAYS
;
326 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."),
328 #endif /* HAVE_SSL */
331 case 'T' : /* Set timeout */
336 _cupsLangPrintf(stderr
,
337 _("%s: Missing timeout for \"-T\"."),
342 vars
.timeout
= _cupsStrScand(argv
[i
], NULL
, localeconv());
345 case 'V' : /* Set IPP version */
350 _cupsLangPrintf(stderr
,
351 _("%s: Missing version for \"-V\"."),
356 if (!strcmp(argv
[i
], "1.0"))
358 else if (!strcmp(argv
[i
], "1.1"))
360 else if (!strcmp(argv
[i
], "2.0"))
362 else if (!strcmp(argv
[i
], "2.1"))
364 else if (!strcmp(argv
[i
], "2.2"))
368 _cupsLangPrintf(stderr
,
369 _("%s: Bad version %s for \"-V\"."),
375 case 'X' : /* Produce XML output */
376 Output
= _CUPS_OUTPUT_PLIST
;
378 if (interval
|| repeat
)
380 _cupsLangPuts(stderr
, _("ipptool: \"-i\" and \"-n\" are incompatible with \"-P\" and \"-X\"."));
385 case 'c' : /* CSV output */
386 Output
= _CUPS_OUTPUT_CSV
;
389 case 'd' : /* Define a variable */
394 _cupsLangPuts(stderr
,
395 _("ipptool: Missing name=value for \"-d\"."));
399 strlcpy(name
, argv
[i
], sizeof(name
));
400 if ((value
= strchr(name
, '=')) != NULL
)
403 value
= name
+ strlen(name
);
405 set_variable(outfile
, &vars
, name
, value
);
408 case 'f' : /* Set the default test filename */
413 _cupsLangPuts(stderr
,
414 _("ipptool: Missing filename for \"-f\"."));
421 vars
.filename
= NULL
;
424 if (access(argv
[i
], 0))
430 snprintf(filename
, sizeof(filename
), "%s.gz", argv
[i
]);
431 if (access(filename
, 0) && filename
[0] != '/'
433 && (!isalpha(filename
[0] & 255) || filename
[1] != ':')
437 snprintf(filename
, sizeof(filename
), "%s/ipptool/%s",
438 cg
->cups_datadir
, argv
[i
]);
439 if (access(filename
, 0))
441 snprintf(filename
, sizeof(filename
), "%s/ipptool/%s.gz",
442 cg
->cups_datadir
, argv
[i
]);
443 if (access(filename
, 0))
444 vars
.filename
= strdup(argv
[i
]);
446 vars
.filename
= strdup(filename
);
449 vars
.filename
= strdup(filename
);
452 vars
.filename
= strdup(filename
);
455 vars
.filename
= strdup(argv
[i
]);
457 if ((ext
= strrchr(vars
.filename
, '.')) != NULL
)
460 * Guess the MIME media type based on the extension...
463 if (!_cups_strcasecmp(ext
, ".gif"))
464 set_variable(outfile
, &vars
, "filetype", "image/gif");
465 else if (!_cups_strcasecmp(ext
, ".htm") ||
466 !_cups_strcasecmp(ext
, ".htm.gz") ||
467 !_cups_strcasecmp(ext
, ".html") ||
468 !_cups_strcasecmp(ext
, ".html.gz"))
469 set_variable(outfile
, &vars
, "filetype", "text/html");
470 else if (!_cups_strcasecmp(ext
, ".jpg") ||
471 !_cups_strcasecmp(ext
, ".jpeg"))
472 set_variable(outfile
, &vars
, "filetype", "image/jpeg");
473 else if (!_cups_strcasecmp(ext
, ".pcl") ||
474 !_cups_strcasecmp(ext
, ".pcl.gz"))
475 set_variable(outfile
, &vars
, "filetype", "application/vnd.hp-PCL");
476 else if (!_cups_strcasecmp(ext
, ".pdf"))
477 set_variable(outfile
, &vars
, "filetype", "application/pdf");
478 else if (!_cups_strcasecmp(ext
, ".png"))
479 set_variable(outfile
, &vars
, "filetype", "image/png");
480 else if (!_cups_strcasecmp(ext
, ".ps") ||
481 !_cups_strcasecmp(ext
, ".ps.gz"))
482 set_variable(outfile
, &vars
, "filetype", "application/postscript");
483 else if (!_cups_strcasecmp(ext
, ".pwg") ||
484 !_cups_strcasecmp(ext
, ".pwg.gz") ||
485 !_cups_strcasecmp(ext
, ".ras") ||
486 !_cups_strcasecmp(ext
, ".ras.gz"))
487 set_variable(outfile
, &vars
, "filetype", "image/pwg-raster");
488 else if (!_cups_strcasecmp(ext
, ".tif") ||
489 !_cups_strcasecmp(ext
, ".tiff"))
490 set_variable(outfile
, &vars
, "filetype", "image/tiff");
491 else if (!_cups_strcasecmp(ext
, ".txt") ||
492 !_cups_strcasecmp(ext
, ".txt.gz"))
493 set_variable(outfile
, &vars
, "filetype", "text/plain");
494 else if (!_cups_strcasecmp(ext
, ".urf") ||
495 !_cups_strcasecmp(ext
, ".urf.gz"))
496 set_variable(outfile
, &vars
, "filetype", "image/urf");
497 else if (!_cups_strcasecmp(ext
, ".xps"))
498 set_variable(outfile
, &vars
, "filetype", "application/openxps");
500 set_variable(outfile
, &vars
, "filetype", "application/octet-stream");
505 * Use the "auto-type" MIME media type...
508 set_variable(outfile
, &vars
, "filetype", "application/octet-stream");
512 case 'i' : /* Test every N seconds */
517 _cupsLangPuts(stderr
,
518 _("ipptool: Missing seconds for \"-i\"."));
523 interval
= (int)(_cupsStrScand(argv
[i
], NULL
, localeconv()) *
527 _cupsLangPuts(stderr
,
528 _("ipptool: Invalid seconds for \"-i\"."));
533 if (Output
== _CUPS_OUTPUT_PLIST
&& interval
)
535 _cupsLangPuts(stderr
, _("ipptool: \"-i\" and \"-n\" are incompatible with \"-P\" and \"-X\"."));
540 case 'l' : /* List as a table */
541 Output
= _CUPS_OUTPUT_LIST
;
544 case 'n' : /* Repeat count */
549 _cupsLangPuts(stderr
,
550 _("ipptool: Missing count for \"-n\"."));
554 repeat
= atoi(argv
[i
]);
556 if (Output
== _CUPS_OUTPUT_PLIST
&& repeat
)
558 _cupsLangPuts(stderr
, _("ipptool: \"-i\" and \"-n\" are incompatible with \"-P\" and \"-X\"."));
563 case 'q' : /* Be quiet */
564 Output
= _CUPS_OUTPUT_QUIET
;
567 case 't' : /* CUPS test output */
568 Output
= _CUPS_OUTPUT_TEST
;
571 case 'v' : /* Be verbose */
576 _cupsLangPrintf(stderr
, _("ipptool: Unknown option \"-%c\"."),
582 else if (!strncmp(argv
[i
], "ipp://", 6) || !strncmp(argv
[i
], "http://", 7)
584 || !strncmp(argv
[i
], "ipps://", 7)
585 || !strncmp(argv
[i
], "https://", 8)
586 #endif /* HAVE_SSL */
595 _cupsLangPuts(stderr
, _("ipptool: May only specify a single URI."));
600 if (!strncmp(argv
[i
], "ipps://", 7) || !strncmp(argv
[i
], "https://", 8))
601 vars
.encryption
= HTTP_ENCRYPT_ALWAYS
;
602 #endif /* HAVE_SSL */
605 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
, vars
.uri
,
606 vars
.scheme
, sizeof(vars
.scheme
),
607 vars
.userpass
, sizeof(vars
.userpass
),
608 vars
.hostname
, sizeof(vars
.hostname
),
610 vars
.resource
, sizeof(vars
.resource
));
612 if (uri_status
!= HTTP_URI_OK
)
614 _cupsLangPrintf(stderr
, _("ipptool: Bad URI - %s."), httpURIStatusString(uri_status
));
618 if (vars
.userpass
[0])
620 if ((Password
= strchr(vars
.userpass
, ':')) != NULL
)
623 Username
= vars
.userpass
;
624 cupsSetPasswordCB(password_cb
);
625 set_variable(outfile
, &vars
, "uriuser", vars
.userpass
);
628 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), vars
.scheme
, NULL
,
629 vars
.hostname
, vars
.port
, vars
.resource
);
640 _cupsLangPuts(stderr
, _("ipptool: URI required before test file."));
641 _cupsLangPuts(stderr
, argv
[i
]);
645 if (access(argv
[i
], 0) && argv
[i
][0] != '/'
647 && (!isalpha(argv
[i
][0] & 255) || argv
[i
][1] != ':')
651 snprintf(testname
, sizeof(testname
), "%s/ipptool/%s", cg
->cups_datadir
,
653 if (access(testname
, 0))
661 if (!do_tests(outfile
, &vars
, testfile
))
666 if (!vars
.uri
|| !testfile
)
670 * Loop if the interval is set...
673 if (Output
== _CUPS_OUTPUT_PLIST
)
674 print_xml_trailer(outfile
, !status
, NULL
);
675 else if (interval
> 0 && repeat
> 0)
679 usleep((useconds_t
)interval
);
680 do_tests(outfile
, &vars
, testfile
);
684 else if (interval
> 0)
688 usleep((useconds_t
)interval
);
689 do_tests(outfile
, &vars
, testfile
);
693 if ((Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
)) && TestCount
> 1)
696 * Show a summary report if there were multiple tests...
699 printf("\nSummary: %d tests, %d passed, %d failed, %d skipped\n"
700 "Score: %d%%\n", TestCount
, PassCount
, FailCount
, SkipCount
,
701 100 * (PassCount
+ SkipCount
) / TestCount
);
713 * 'add_stringf()' - Add a formatted string to an array.
717 add_stringf(cups_array_t
*a
, /* I - Array */
718 const char *s
, /* I - Printf-style format string */
719 ...) /* I - Additional args as needed */
721 char buffer
[10240]; /* Format buffer */
722 va_list ap
; /* Argument pointer */
726 * Don't bother is the array is NULL...
733 * Format the message...
737 vsnprintf(buffer
, sizeof(buffer
), s
, ap
);
741 * Add it to the array...
744 cupsArrayAdd(a
, buffer
);
749 * 'compare_vars()' - Compare two variables.
752 static int /* O - Result of comparison */
753 compare_vars(_cups_var_t
*a
, /* I - First variable */
754 _cups_var_t
*b
) /* I - Second variable */
756 return (_cups_strcasecmp(a
->name
, b
->name
));
761 * 'do_tests()' - Do tests as specified in the test file.
764 static int /* 1 = success, 0 = failure */
765 do_tests(FILE *outfile
, /* I - Output file */
766 _cups_vars_t
*vars
, /* I - Variables */
767 const char *testfile
) /* I - Test file to use */
769 int i
, /* Looping var */
770 linenum
, /* Current line number */
771 pass
, /* Did we pass the test? */
772 prev_pass
= 1, /* Did we pass the previous test? */
773 request_id
, /* Current request ID */
774 show_header
= 1, /* Show the test header? */
775 ignore_errors
, /* Ignore test failures? */
776 skip_previous
= 0, /* Skip on previous test failure? */
777 repeat_count
, /* Repeat count */
778 repeat_interval
, /* Repeat interval */
779 repeat_prev
, /* Previous repeat interval */
780 repeat_test
; /* Repeat a test? */
781 http_t
*http
= NULL
; /* HTTP connection to server */
782 FILE *fp
= NULL
; /* Test file */
783 char resource
[512], /* Resource for request */
784 token
[1024], /* Token from file */
785 *tokenptr
, /* Pointer into token */
786 temp
[1024], /* Temporary string */
787 buffer
[131072], /* Copy buffer */
788 compression
[16]; /* COMPRESSION value */
789 ipp_t
*request
= NULL
, /* IPP request */
790 *response
= NULL
; /* IPP response */
791 size_t length
; /* Length of IPP request */
792 http_status_t status
; /* HTTP status */
793 cups_file_t
*reqfile
; /* File to send */
794 ssize_t bytes
; /* Bytes read/written */
795 char attr
[128]; /* Attribute name */
796 ipp_op_t op
; /* Operation */
797 ipp_tag_t group
; /* Current group */
798 ipp_tag_t value
; /* Current value type */
799 ipp_attribute_t
*attrptr
, /* Attribute pointer */
800 *found
, /* Found attribute */
801 *lastcol
= NULL
; /* Last collection attribute */
802 char name
[1024], /* Name of test */
803 file_id
[1024], /* File identifier */
804 test_id
[1024]; /* Test identifier */
805 char filename
[1024]; /* Filename */
806 _cups_transfer_t transfer
; /* To chunk or not to chunk */
807 int version
, /* IPP version number to use */
808 skip_test
; /* Skip this test? */
809 int num_statuses
= 0; /* Number of valid status codes */
810 _cups_status_t statuses
[100], /* Valid status codes */
811 *last_status
; /* Last STATUS (for predicates) */
812 int num_expects
= 0; /* Number of expected attributes */
813 _cups_expect_t expects
[200], /* Expected attributes */
814 *expect
, /* Current expected attribute */
815 *last_expect
; /* Last EXPECT (for predicates) */
816 int num_displayed
= 0; /* Number of displayed attributes */
817 char *displayed
[200]; /* Displayed attributes */
818 size_t widths
[200]; /* Width of columns */
819 cups_array_t
*a
, /* Duplicate attribute array */
820 *errors
= NULL
; /* Errors array */
821 const char *error
; /* Current error */
825 * Open the test file...
828 if ((fp
= fopen(testfile
, "r")) == NULL
)
830 print_fatal_error(outfile
, "Unable to open test file %s - %s", testfile
,
837 * Connect to the server...
840 if ((http
= httpConnect2(vars
->hostname
, vars
->port
, NULL
, vars
->family
,
841 vars
->encryption
, 1, 30000, NULL
)) == NULL
)
843 print_fatal_error(outfile
, "Unable to connect to %s on port %d - %s", vars
->hostname
,
844 vars
->port
, cupsLastErrorString());
850 httpSetDefaultField(http
, HTTP_FIELD_ACCEPT_ENCODING
,
851 "deflate, gzip, identity");
853 httpSetDefaultField(http
, HTTP_FIELD_ACCEPT_ENCODING
, "identity");
854 #endif /* HAVE_LIBZ */
856 if (vars
->timeout
> 0.0)
857 httpSetTimeout(http
, vars
->timeout
, timeout_cb
, NULL
);
863 CUPS_SRAND(time(NULL
));
865 errors
= cupsArrayNew3(NULL
, NULL
, NULL
, 0, (cups_acopy_func_t
)strdup
,
866 (cups_afree_func_t
)free
);
870 request_id
= (CUPS_RAND() % 1000) * 137 + 1;
872 while (!Cancel
&& get_token(fp
, token
, sizeof(token
), &linenum
) != NULL
)
875 * Expect an open brace...
878 if (!strcmp(token
, "DEFINE"))
884 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
885 get_token(fp
, temp
, sizeof(temp
), &linenum
))
887 expand_variables(vars
, token
, temp
, sizeof(token
));
888 set_variable(outfile
, vars
, attr
, token
);
892 print_fatal_error(outfile
, "Missing DEFINE name and/or value on line %d.",
900 else if (!strcmp(token
, "DEFINE-DEFAULT"))
903 * DEFINE-DEFAULT name value
906 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
907 get_token(fp
, temp
, sizeof(temp
), &linenum
))
909 expand_variables(vars
, token
, temp
, sizeof(token
));
910 if (!get_variable(vars
, attr
))
911 set_variable(outfile
, vars
, attr
, token
);
915 print_fatal_error(outfile
, "Missing DEFINE-DEFAULT name and/or value on line "
923 else if (!strcmp(token
, "FILE-ID"))
929 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
931 expand_variables(vars
, file_id
, temp
, sizeof(file_id
));
935 print_fatal_error(outfile
, "Missing FILE-ID value on line %d.", linenum
);
942 else if (!strcmp(token
, "IGNORE-ERRORS"))
949 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
950 (!_cups_strcasecmp(temp
, "yes") || !_cups_strcasecmp(temp
, "no")))
952 IgnoreErrors
= !_cups_strcasecmp(temp
, "yes");
956 print_fatal_error(outfile
, "Missing IGNORE-ERRORS value on line %d.", linenum
);
963 else if (!strcmp(token
, "INCLUDE"))
970 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
973 * Map the filename to and then run the tests...
976 if (!do_tests(outfile
, vars
, get_filename(testfile
, filename
, temp
, sizeof(filename
))))
980 if (StopAfterIncludeError
)
986 print_fatal_error(outfile
, "Missing INCLUDE filename on line %d.", linenum
);
994 else if (!strcmp(token
, "INCLUDE-IF-DEFINED"))
997 * INCLUDE-IF-DEFINED name "filename"
998 * INCLUDE-IF-DEFINED name <filename>
1001 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
1002 get_token(fp
, temp
, sizeof(temp
), &linenum
))
1005 * Map the filename to and then run the tests...
1008 if (get_variable(vars
, attr
) &&
1009 !do_tests(outfile
, vars
, get_filename(testfile
, filename
, temp
, sizeof(filename
))))
1013 if (StopAfterIncludeError
)
1019 print_fatal_error(outfile
, "Missing INCLUDE-IF-DEFINED name or filename on line "
1028 else if (!strcmp(token
, "INCLUDE-IF-NOT-DEFINED"))
1031 * INCLUDE-IF-NOT-DEFINED name "filename"
1032 * INCLUDE-IF-NOT-DEFINED name <filename>
1035 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
1036 get_token(fp
, temp
, sizeof(temp
), &linenum
))
1039 * Map the filename to and then run the tests...
1042 if (!get_variable(vars
, attr
) &&
1043 !do_tests(outfile
, vars
, get_filename(testfile
, filename
, temp
, sizeof(filename
))))
1047 if (StopAfterIncludeError
)
1053 print_fatal_error(outfile
, "Missing INCLUDE-IF-NOT-DEFINED name or filename on "
1054 "line %d.", linenum
);
1062 else if (!strcmp(token
, "SKIP-IF-DEFINED"))
1065 * SKIP-IF-DEFINED variable
1068 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1070 if (get_variable(vars
, temp
))
1075 print_fatal_error(outfile
, "Missing SKIP-IF-DEFINED variable on line %d.",
1081 else if (!strcmp(token
, "SKIP-IF-NOT-DEFINED"))
1084 * SKIP-IF-NOT-DEFINED variable
1087 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1089 if (!get_variable(vars
, temp
))
1094 print_fatal_error(outfile
, "Missing SKIP-IF-NOT-DEFINED variable on line %d.",
1100 else if (!strcmp(token
, "STOP-AFTER-INCLUDE-ERROR"))
1103 * STOP-AFTER-INCLUDE-ERROR yes
1104 * STOP-AFTER-INCLUDE-ERROR no
1107 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
1108 (!_cups_strcasecmp(temp
, "yes") || !_cups_strcasecmp(temp
, "no")))
1110 StopAfterIncludeError
= !_cups_strcasecmp(temp
, "yes");
1114 print_fatal_error(outfile
, "Missing STOP-AFTER-INCLUDE-ERROR value on line %d.",
1122 else if (!strcmp(token
, "TRANSFER"))
1130 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1132 if (!strcmp(temp
, "auto"))
1133 Transfer
= _CUPS_TRANSFER_AUTO
;
1134 else if (!strcmp(temp
, "chunked"))
1135 Transfer
= _CUPS_TRANSFER_CHUNKED
;
1136 else if (!strcmp(temp
, "length"))
1137 Transfer
= _CUPS_TRANSFER_LENGTH
;
1140 print_fatal_error(outfile
, "Bad TRANSFER value \"%s\" on line %d.", temp
,
1148 print_fatal_error(outfile
, "Missing TRANSFER value on line %d.", linenum
);
1155 else if (!strcmp(token
, "VERSION"))
1157 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1159 if (!strcmp(temp
, "1.0"))
1161 else if (!strcmp(temp
, "1.1"))
1163 else if (!strcmp(temp
, "2.0"))
1165 else if (!strcmp(temp
, "2.1"))
1167 else if (!strcmp(temp
, "2.2"))
1171 print_fatal_error(outfile
, "Bad VERSION \"%s\" on line %d.", temp
, linenum
);
1178 print_fatal_error(outfile
, "Missing VERSION number on line %d.", linenum
);
1185 else if (strcmp(token
, "{"))
1187 print_fatal_error(outfile
, "Unexpected token %s seen on line %d.", token
, linenum
);
1193 * Initialize things...
1198 if (Output
== _CUPS_OUTPUT_PLIST
)
1199 print_xml_header(outfile
);
1200 if (Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
!= stdout
))
1201 printf("\"%s\":\n", testfile
);
1206 strlcpy(resource
, vars
->resource
, sizeof(resource
));
1211 group
= IPP_TAG_ZERO
;
1212 ignore_errors
= IgnoreErrors
;
1220 transfer
= Transfer
;
1221 compression
[0] = '\0';
1223 strlcpy(name
, testfile
, sizeof(name
));
1224 if (strrchr(name
, '.') != NULL
)
1225 *strrchr(name
, '.') = '\0';
1228 * Parse until we see a close brace...
1231 while (get_token(fp
, token
, sizeof(token
), &linenum
) != NULL
)
1233 if (_cups_strcasecmp(token
, "COUNT") &&
1234 _cups_strcasecmp(token
, "DEFINE-MATCH") &&
1235 _cups_strcasecmp(token
, "DEFINE-NO-MATCH") &&
1236 _cups_strcasecmp(token
, "DEFINE-VALUE") &&
1237 _cups_strcasecmp(token
, "IF-DEFINED") &&
1238 _cups_strcasecmp(token
, "IF-NOT-DEFINED") &&
1239 _cups_strcasecmp(token
, "IN-GROUP") &&
1240 _cups_strcasecmp(token
, "OF-TYPE") &&
1241 _cups_strcasecmp(token
, "REPEAT-LIMIT") &&
1242 _cups_strcasecmp(token
, "REPEAT-MATCH") &&
1243 _cups_strcasecmp(token
, "REPEAT-NO-MATCH") &&
1244 _cups_strcasecmp(token
, "SAME-COUNT-AS") &&
1245 _cups_strcasecmp(token
, "WITH-ALL-VALUES") &&
1246 _cups_strcasecmp(token
, "WITH-ALL-HOSTNAMES") &&
1247 _cups_strcasecmp(token
, "WITH-ALL-RESOURCES") &&
1248 _cups_strcasecmp(token
, "WITH-ALL-SCHEMES") &&
1249 _cups_strcasecmp(token
, "WITH-HOSTNAME") &&
1250 _cups_strcasecmp(token
, "WITH-RESOURCE") &&
1251 _cups_strcasecmp(token
, "WITH-SCHEME") &&
1252 _cups_strcasecmp(token
, "WITH-VALUE") &&
1253 _cups_strcasecmp(token
, "WITH-VALUE-FROM"))
1256 if (_cups_strcasecmp(token
, "DEFINE-MATCH") &&
1257 _cups_strcasecmp(token
, "DEFINE-NO-MATCH") &&
1258 _cups_strcasecmp(token
, "IF-DEFINED") &&
1259 _cups_strcasecmp(token
, "IF-NOT-DEFINED") &&
1260 _cups_strcasecmp(token
, "REPEAT-LIMIT") &&
1261 _cups_strcasecmp(token
, "REPEAT-MATCH") &&
1262 _cups_strcasecmp(token
, "REPEAT-NO-MATCH"))
1265 if (!strcmp(token
, "}"))
1267 else if (!strcmp(token
, "{") && lastcol
)
1270 * Another collection value
1273 ipp_t
*col
= get_collection(outfile
, vars
, fp
, &linenum
);
1274 /* Collection value */
1278 ippSetCollection(request
, &lastcol
, ippGetCount(lastcol
), col
);
1286 else if (!strcmp(token
, "COMPRESSION"))
1290 * COMPRESSION deflate
1294 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1296 expand_variables(vars
, compression
, temp
, sizeof(compression
));
1298 if (strcmp(compression
, "none") && strcmp(compression
, "deflate") &&
1299 strcmp(compression
, "gzip"))
1301 if (strcmp(compression
, "none"))
1302 #endif /* HAVE_LIBZ */
1304 print_fatal_error(outfile
, "Unsupported COMPRESSION value '%s' on line %d.",
1305 compression
, linenum
);
1310 if (!strcmp(compression
, "none"))
1311 compression
[0] = '\0';
1315 print_fatal_error(outfile
, "Missing COMPRESSION value on line %d.", linenum
);
1320 else if (!strcmp(token
, "DEFINE"))
1326 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
1327 get_token(fp
, temp
, sizeof(temp
), &linenum
))
1329 expand_variables(vars
, token
, temp
, sizeof(token
));
1330 set_variable(outfile
, vars
, attr
, token
);
1334 print_fatal_error(outfile
, "Missing DEFINE name and/or value on line %d.",
1340 else if (!strcmp(token
, "IGNORE-ERRORS"))
1347 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
1348 (!_cups_strcasecmp(temp
, "yes") || !_cups_strcasecmp(temp
, "no")))
1350 ignore_errors
= !_cups_strcasecmp(temp
, "yes");
1354 print_fatal_error(outfile
, "Missing IGNORE-ERRORS value on line %d.", linenum
);
1361 else if (!_cups_strcasecmp(token
, "NAME"))
1367 get_token(fp
, temp
, sizeof(temp
), &linenum
);
1368 expand_variables(vars
, name
, temp
, sizeof(name
));
1370 else if (!_cups_strcasecmp(token
, "PAUSE"))
1373 * Pause with a message...
1376 get_token(fp
, token
, sizeof(token
), &linenum
);
1377 pause_message(token
);
1379 else if (!strcmp(token
, "REQUEST-ID"))
1386 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1388 if (isdigit(temp
[0] & 255))
1389 request_id
= atoi(temp
);
1390 else if (!_cups_strcasecmp(temp
, "random"))
1391 request_id
= (CUPS_RAND() % 1000) * 137 + 1;
1394 print_fatal_error(outfile
, "Bad REQUEST-ID value \"%s\" on line %d.", temp
,
1402 print_fatal_error(outfile
, "Missing REQUEST-ID value on line %d.", linenum
);
1407 else if (!strcmp(token
, "SKIP-IF-DEFINED"))
1410 * SKIP-IF-DEFINED variable
1413 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1415 if (get_variable(vars
, temp
))
1420 print_fatal_error(outfile
, "Missing SKIP-IF-DEFINED value on line %d.",
1426 else if (!strcmp(token
, "SKIP-IF-MISSING"))
1429 * SKIP-IF-MISSING filename
1432 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1434 expand_variables(vars
, token
, temp
, sizeof(token
));
1435 get_filename(testfile
, filename
, token
, sizeof(filename
));
1437 if (access(filename
, R_OK
))
1442 print_fatal_error(outfile
, "Missing SKIP-IF-MISSING filename on line %d.",
1448 else if (!strcmp(token
, "SKIP-IF-NOT-DEFINED"))
1451 * SKIP-IF-NOT-DEFINED variable
1454 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1456 if (!get_variable(vars
, temp
))
1461 print_fatal_error(outfile
, "Missing SKIP-IF-NOT-DEFINED value on line %d.",
1467 else if (!strcmp(token
, "SKIP-PREVIOUS-ERROR"))
1470 * SKIP-PREVIOUS-ERROR yes
1471 * SKIP-PREVIOUS-ERROR no
1474 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
1475 (!_cups_strcasecmp(temp
, "yes") || !_cups_strcasecmp(temp
, "no")))
1477 skip_previous
= !_cups_strcasecmp(temp
, "yes");
1481 print_fatal_error(outfile
, "Missing SKIP-PREVIOUS-ERROR value on line %d.", linenum
);
1488 else if (!strcmp(token
, "TEST-ID"))
1494 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1496 expand_variables(vars
, test_id
, temp
, sizeof(test_id
));
1500 print_fatal_error(outfile
, "Missing TEST-ID value on line %d.", linenum
);
1507 else if (!strcmp(token
, "TRANSFER"))
1515 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1517 if (!strcmp(temp
, "auto"))
1518 transfer
= _CUPS_TRANSFER_AUTO
;
1519 else if (!strcmp(temp
, "chunked"))
1520 transfer
= _CUPS_TRANSFER_CHUNKED
;
1521 else if (!strcmp(temp
, "length"))
1522 transfer
= _CUPS_TRANSFER_LENGTH
;
1525 print_fatal_error(outfile
, "Bad TRANSFER value \"%s\" on line %d.", temp
,
1533 print_fatal_error(outfile
, "Missing TRANSFER value on line %d.", linenum
);
1538 else if (!_cups_strcasecmp(token
, "VERSION"))
1540 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1542 if (!strcmp(temp
, "0.0"))
1544 else if (!strcmp(temp
, "1.0"))
1546 else if (!strcmp(temp
, "1.1"))
1548 else if (!strcmp(temp
, "2.0"))
1550 else if (!strcmp(temp
, "2.1"))
1552 else if (!strcmp(temp
, "2.2"))
1556 print_fatal_error(outfile
, "Bad VERSION \"%s\" on line %d.", temp
, linenum
);
1563 print_fatal_error(outfile
, "Missing VERSION number on line %d.", linenum
);
1568 else if (!_cups_strcasecmp(token
, "RESOURCE"))
1574 if (!get_token(fp
, resource
, sizeof(resource
), &linenum
))
1576 print_fatal_error(outfile
, "Missing RESOURCE path on line %d.", linenum
);
1581 else if (!_cups_strcasecmp(token
, "OPERATION"))
1587 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1589 print_fatal_error(outfile
, "Missing OPERATION code on line %d.", linenum
);
1594 expand_variables(vars
, token
, temp
, sizeof(token
));
1596 if ((op
= ippOpValue(token
)) == (ipp_op_t
)-1 &&
1597 (op
= (ipp_op_t
)strtol(token
, NULL
, 0)) == 0)
1599 print_fatal_error(outfile
, "Bad OPERATION code \"%s\" on line %d.", token
,
1605 else if (!_cups_strcasecmp(token
, "GROUP"))
1608 * Attribute group...
1611 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1613 print_fatal_error(outfile
, "Missing GROUP tag on line %d.", linenum
);
1618 if ((value
= ippTagValue(token
)) < 0)
1620 print_fatal_error(outfile
, "Bad GROUP tag \"%s\" on line %d.", token
, linenum
);
1626 ippAddSeparator(request
);
1630 else if (!_cups_strcasecmp(token
, "DELAY"))
1633 * Delay before operation...
1638 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1640 print_fatal_error(outfile
, "Missing DELAY value on line %d.", linenum
);
1645 expand_variables(vars
, token
, temp
, sizeof(token
));
1647 if ((delay
= _cupsStrScand(token
, NULL
, localeconv())) <= 0.0)
1649 print_fatal_error(outfile
, "Bad DELAY value \"%s\" on line %d.", token
,
1656 if (Output
== _CUPS_OUTPUT_TEST
)
1657 printf(" [%g second delay]\n", delay
);
1659 usleep((useconds_t
)(1000000.0 * delay
));
1662 else if (!_cups_strcasecmp(token
, "ATTR"))
1668 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1670 print_fatal_error(outfile
, "Missing ATTR value tag on line %d.", linenum
);
1675 if ((value
= ippTagValue(token
)) == IPP_TAG_ZERO
)
1677 print_fatal_error(outfile
, "Bad ATTR value tag \"%s\" on line %d.", token
,
1683 if (!get_token(fp
, attr
, sizeof(attr
), &linenum
))
1685 print_fatal_error(outfile
, "Missing ATTR name on line %d.", linenum
);
1690 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1692 print_fatal_error(outfile
, "Missing ATTR value on line %d.", linenum
);
1697 expand_variables(vars
, token
, temp
, sizeof(token
));
1702 case IPP_TAG_BOOLEAN
:
1703 if (!_cups_strcasecmp(token
, "true"))
1704 attrptr
= ippAddBoolean(request
, group
, attr
, 1);
1706 attrptr
= ippAddBoolean(request
, group
, attr
, (char)atoi(token
));
1709 case IPP_TAG_INTEGER
:
1711 if (!strchr(token
, ','))
1713 if (isdigit(token
[0] & 255) || token
[0] == '-')
1714 attrptr
= ippAddInteger(request
, group
, value
, attr
, (int)strtol(token
, &tokenptr
, 0));
1718 if ((i
= ippEnumValue(attr
, tokenptr
)) >= 0)
1720 attrptr
= ippAddInteger(request
, group
, value
, attr
, i
);
1721 tokenptr
+= strlen(tokenptr
);
1727 int values
[100], /* Values */
1728 num_values
= 1; /* Number of values */
1730 if (!isdigit(token
[0] & 255) && token
[0] != '-' && value
== IPP_TAG_ENUM
)
1732 char *ptr
; /* Pointer to next terminator */
1734 if ((ptr
= strchr(token
, ',')) != NULL
)
1737 ptr
= token
+ strlen(token
);
1739 if ((i
= ippEnumValue(attr
, token
)) < 0)
1745 i
= (int)strtol(token
, &tokenptr
, 0);
1749 while (tokenptr
&& *tokenptr
&&
1750 num_values
< (int)(sizeof(values
) / sizeof(values
[0])))
1752 if (*tokenptr
== ',')
1755 if (!isdigit(*tokenptr
& 255) && *tokenptr
!= '-')
1757 char *ptr
; /* Pointer to next terminator */
1759 if (value
!= IPP_TAG_ENUM
)
1762 if ((ptr
= strchr(tokenptr
, ',')) != NULL
)
1765 ptr
= tokenptr
+ strlen(tokenptr
);
1767 if ((i
= ippEnumValue(attr
, tokenptr
)) < 0)
1773 i
= (int)strtol(tokenptr
, &tokenptr
, 0);
1775 values
[num_values
++] = i
;
1778 attrptr
= ippAddIntegers(request
, group
, value
, attr
, num_values
, values
);
1781 if ((!token
[0] || !tokenptr
|| *tokenptr
) && !skip_test
)
1783 print_fatal_error(outfile
, "Bad %s value \'%s\' for \"%s\" on line %d.",
1784 ippTagString(value
), token
, attr
, linenum
);
1790 case IPP_TAG_RESOLUTION
:
1792 int xres
, /* X resolution */
1793 yres
; /* Y resolution */
1794 char *ptr
; /* Pointer into value */
1796 xres
= yres
= (int)strtol(token
, (char **)&ptr
, 10);
1797 if (ptr
> token
&& xres
> 0)
1800 yres
= (int)strtol(ptr
+ 1, (char **)&ptr
, 10);
1803 if (ptr
<= token
|| xres
<= 0 || yres
<= 0 || !ptr
||
1804 (_cups_strcasecmp(ptr
, "dpi") &&
1805 _cups_strcasecmp(ptr
, "dpc") &&
1806 _cups_strcasecmp(ptr
, "dpcm") &&
1807 _cups_strcasecmp(ptr
, "other")))
1812 print_fatal_error(outfile
, "Bad resolution value \'%s\' for \"%s\" on line %d.", token
, attr
, linenum
);
1817 if (!_cups_strcasecmp(ptr
, "dpi"))
1818 attrptr
= ippAddResolution(request
, group
, attr
, IPP_RES_PER_INCH
, xres
, yres
);
1819 else if (!_cups_strcasecmp(ptr
, "dpc") ||
1820 !_cups_strcasecmp(ptr
, "dpcm"))
1821 attrptr
= ippAddResolution(request
, group
, attr
, IPP_RES_PER_CM
, xres
, yres
);
1823 attrptr
= ippAddResolution(request
, group
, attr
, (ipp_res_t
)0, xres
, yres
);
1827 case IPP_TAG_RANGE
:
1829 int lowers
[4], /* Lower value */
1830 uppers
[4], /* Upper values */
1831 num_vals
; /* Number of values */
1834 num_vals
= sscanf(token
, "%d-%d,%d-%d,%d-%d,%d-%d",
1835 lowers
+ 0, uppers
+ 0,
1836 lowers
+ 1, uppers
+ 1,
1837 lowers
+ 2, uppers
+ 2,
1838 lowers
+ 3, uppers
+ 3);
1840 if ((num_vals
& 1) || num_vals
== 0)
1845 print_fatal_error(outfile
, "Bad rangeOfInteger value \'%s\' for \"%s\" on line %d.", token
, attr
, linenum
);
1850 attrptr
= ippAddRanges(request
, group
, attr
, num_vals
/ 2, lowers
,
1855 case IPP_TAG_BEGIN_COLLECTION
:
1856 if (!strcmp(token
, "{"))
1858 ipp_t
*col
= get_collection(outfile
, vars
, fp
, &linenum
);
1859 /* Collection value */
1863 attrptr
= lastcol
= ippAddCollection(request
, group
, attr
, col
);
1876 print_fatal_error(outfile
, "Bad ATTR collection value for \"%s\" on line %d.", attr
, linenum
);
1883 ipp_t
*col
; /* Collection value */
1884 long savepos
= ftell(fp
); /* Save position of file */
1885 int savelinenum
= linenum
; /* Save line number */
1887 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1890 if (strcmp(token
, ","))
1892 fseek(fp
, savepos
, SEEK_SET
);
1893 linenum
= savelinenum
;
1897 if (!get_token(fp
, token
, sizeof(token
), &linenum
) || strcmp(token
, "{"))
1899 print_fatal_error(outfile
, "Unexpected \"%s\" on line %d.", token
, linenum
);
1905 if ((col
= get_collection(outfile
, vars
, fp
, &linenum
)) == NULL
)
1908 ippSetCollection(request
, &attrptr
, ippGetCount(attrptr
), col
);
1911 while (!strcmp(token
, "{"));
1914 case IPP_TAG_STRING
:
1915 attrptr
= ippAddOctetString(request
, group
, attr
, token
, (int)strlen(token
));
1919 print_fatal_error(outfile
, "Unsupported ATTR value tag %s for \"%s\" on line %d.", ippTagString(value
), attr
, linenum
);
1923 case IPP_TAG_TEXTLANG
:
1924 case IPP_TAG_NAMELANG
:
1927 case IPP_TAG_KEYWORD
:
1929 case IPP_TAG_URISCHEME
:
1930 case IPP_TAG_CHARSET
:
1931 case IPP_TAG_LANGUAGE
:
1932 case IPP_TAG_MIMETYPE
:
1933 if (!strchr(token
, ','))
1934 attrptr
= ippAddString(request
, group
, value
, attr
, NULL
, token
);
1938 * Multiple string values...
1941 int num_values
; /* Number of values */
1942 char *values
[100], /* Values */
1943 *ptr
; /* Pointer to next value */
1949 for (ptr
= strchr(token
, ','); ptr
; ptr
= strchr(ptr
, ','))
1951 if (ptr
> token
&& ptr
[-1] == '\\')
1952 _cups_strcpy(ptr
- 1, ptr
);
1956 values
[num_values
] = ptr
;
1961 attrptr
= ippAddStrings(request
, group
, value
, attr
, num_values
,
1962 NULL
, (const char **)values
);
1967 if (!attrptr
&& !skip_test
)
1969 print_fatal_error(outfile
, "Unable to add attribute \"%s\" on line %d.", attr
, linenum
);
1974 else if (!_cups_strcasecmp(token
, "FILE"))
1980 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1982 print_fatal_error(outfile
, "Missing FILE filename on line %d.", linenum
);
1987 expand_variables(vars
, token
, temp
, sizeof(token
));
1988 get_filename(testfile
, filename
, token
, sizeof(filename
));
1990 if (access(filename
, R_OK
))
1992 print_fatal_error(outfile
, "Filename \"%s\" on line %d cannot be read.",
1994 print_fatal_error(outfile
, "Filename mapped to \"%s\".", filename
);
1999 else if (!_cups_strcasecmp(token
, "STATUS"))
2005 if (num_statuses
>= (int)(sizeof(statuses
) / sizeof(statuses
[0])))
2007 print_fatal_error(outfile
, "Too many STATUS's on line %d.", linenum
);
2012 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2014 print_fatal_error(outfile
, "Missing STATUS code on line %d.", linenum
);
2019 if ((statuses
[num_statuses
].status
= ippErrorValue(token
))
2020 == (ipp_status_t
)-1 &&
2021 (statuses
[num_statuses
].status
= (ipp_status_t
)strtol(token
, NULL
, 0)) == 0)
2023 print_fatal_error(outfile
, "Bad STATUS code \"%s\" on line %d.", token
,
2029 last_status
= statuses
+ num_statuses
;
2032 last_status
->define_match
= NULL
;
2033 last_status
->define_no_match
= NULL
;
2034 last_status
->if_defined
= NULL
;
2035 last_status
->if_not_defined
= NULL
;
2036 last_status
->repeat_limit
= 1000;
2037 last_status
->repeat_match
= 0;
2038 last_status
->repeat_no_match
= 0;
2040 else if (!_cups_strcasecmp(token
, "EXPECT") || !_cups_strcasecmp(token
, "EXPECT-ALL"))
2043 * Expected attributes...
2046 int expect_all
= !_cups_strcasecmp(token
, "EXPECT-ALL");
2048 if (num_expects
>= (int)(sizeof(expects
) / sizeof(expects
[0])))
2050 print_fatal_error(outfile
, "Too many EXPECT's on line %d.", linenum
);
2055 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2057 print_fatal_error(outfile
, "Missing EXPECT name on line %d.", linenum
);
2062 last_expect
= expects
+ num_expects
;
2065 memset(last_expect
, 0, sizeof(_cups_expect_t
));
2066 last_expect
->repeat_limit
= 1000;
2067 last_expect
->expect_all
= expect_all
;
2069 if (token
[0] == '!')
2071 last_expect
->not_expect
= 1;
2072 last_expect
->name
= strdup(token
+ 1);
2074 else if (token
[0] == '?')
2076 last_expect
->optional
= 1;
2077 last_expect
->name
= strdup(token
+ 1);
2080 last_expect
->name
= strdup(token
);
2082 else if (!_cups_strcasecmp(token
, "COUNT"))
2084 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2086 print_fatal_error(outfile
, "Missing COUNT number on line %d.", linenum
);
2091 if ((i
= atoi(token
)) <= 0)
2093 print_fatal_error(outfile
, "Bad COUNT \"%s\" on line %d.", token
, linenum
);
2099 last_expect
->count
= i
;
2102 print_fatal_error(outfile
, "COUNT without a preceding EXPECT on line %d.",
2108 else if (!_cups_strcasecmp(token
, "DEFINE-MATCH"))
2110 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2112 print_fatal_error(outfile
, "Missing DEFINE-MATCH variable on line %d.",
2119 last_expect
->define_match
= strdup(token
);
2120 else if (last_status
)
2121 last_status
->define_match
= strdup(token
);
2124 print_fatal_error(outfile
, "DEFINE-MATCH without a preceding EXPECT or STATUS "
2125 "on line %d.", linenum
);
2130 else if (!_cups_strcasecmp(token
, "DEFINE-NO-MATCH"))
2132 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2134 print_fatal_error(outfile
, "Missing DEFINE-NO-MATCH variable on line %d.",
2141 last_expect
->define_no_match
= strdup(token
);
2142 else if (last_status
)
2143 last_status
->define_no_match
= strdup(token
);
2146 print_fatal_error(outfile
, "DEFINE-NO-MATCH without a preceding EXPECT or "
2147 "STATUS on line %d.", linenum
);
2152 else if (!_cups_strcasecmp(token
, "DEFINE-VALUE"))
2154 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2156 print_fatal_error(outfile
, "Missing DEFINE-VALUE variable on line %d.",
2163 last_expect
->define_value
= strdup(token
);
2166 print_fatal_error(outfile
, "DEFINE-VALUE without a preceding EXPECT on "
2167 "line %d.", linenum
);
2172 else if (!_cups_strcasecmp(token
, "OF-TYPE"))
2174 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2176 print_fatal_error(outfile
, "Missing OF-TYPE value tag(s) on line %d.",
2183 last_expect
->of_type
= strdup(token
);
2186 print_fatal_error(outfile
, "OF-TYPE without a preceding EXPECT on line %d.",
2192 else if (!_cups_strcasecmp(token
, "IN-GROUP"))
2194 ipp_tag_t in_group
; /* IN-GROUP value */
2197 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2199 print_fatal_error(outfile
, "Missing IN-GROUP group tag on line %d.", linenum
);
2204 if ((in_group
= ippTagValue(token
)) == (ipp_tag_t
)-1)
2207 else if (last_expect
)
2208 last_expect
->in_group
= in_group
;
2211 print_fatal_error(outfile
, "IN-GROUP without a preceding EXPECT on line %d.",
2217 else if (!_cups_strcasecmp(token
, "REPEAT-LIMIT"))
2219 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2221 print_fatal_error(outfile
, "Missing REPEAT-LIMIT value on line %d.", linenum
);
2225 else if (atoi(token
) <= 0)
2227 print_fatal_error(outfile
, "Bad REPEAT-LIMIT value on line %d.", linenum
);
2233 last_status
->repeat_limit
= atoi(token
);
2234 else if (last_expect
)
2235 last_expect
->repeat_limit
= atoi(token
);
2238 print_fatal_error(outfile
, "REPEAT-LIMIT without a preceding EXPECT or STATUS "
2239 "on line %d.", linenum
);
2244 else if (!_cups_strcasecmp(token
, "REPEAT-MATCH"))
2247 last_status
->repeat_match
= 1;
2248 else if (last_expect
)
2249 last_expect
->repeat_match
= 1;
2252 print_fatal_error(outfile
, "REPEAT-MATCH without a preceding EXPECT or STATUS "
2253 "on line %d.", linenum
);
2258 else if (!_cups_strcasecmp(token
, "REPEAT-NO-MATCH"))
2261 last_status
->repeat_no_match
= 1;
2262 else if (last_expect
)
2263 last_expect
->repeat_no_match
= 1;
2266 print_fatal_error(outfile
, "REPEAT-NO-MATCH without a preceding EXPECT or "
2267 "STATUS on ine %d.", linenum
);
2272 else if (!_cups_strcasecmp(token
, "SAME-COUNT-AS"))
2274 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2276 print_fatal_error(outfile
, "Missing SAME-COUNT-AS name on line %d.", linenum
);
2282 last_expect
->same_count_as
= strdup(token
);
2285 print_fatal_error(outfile
, "SAME-COUNT-AS without a preceding EXPECT on line "
2291 else if (!_cups_strcasecmp(token
, "IF-DEFINED"))
2293 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2295 print_fatal_error(outfile
, "Missing IF-DEFINED name on line %d.", linenum
);
2301 last_expect
->if_defined
= strdup(token
);
2302 else if (last_status
)
2303 last_status
->if_defined
= strdup(token
);
2306 print_fatal_error(outfile
, "IF-DEFINED without a preceding EXPECT or STATUS "
2307 "on line %d.", linenum
);
2312 else if (!_cups_strcasecmp(token
, "IF-NOT-DEFINED"))
2314 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2316 print_fatal_error(outfile
, "Missing IF-NOT-DEFINED name on line %d.", linenum
);
2322 last_expect
->if_not_defined
= strdup(token
);
2323 else if (last_status
)
2324 last_status
->if_not_defined
= strdup(token
);
2327 print_fatal_error(outfile
, "IF-NOT-DEFINED without a preceding EXPECT or STATUS "
2328 "on line %d.", linenum
);
2333 else if (!_cups_strcasecmp(token
, "WITH-ALL-VALUES") ||
2334 !_cups_strcasecmp(token
, "WITH-ALL-HOSTNAMES") ||
2335 !_cups_strcasecmp(token
, "WITH-ALL-RESOURCES") ||
2336 !_cups_strcasecmp(token
, "WITH-ALL-SCHEMES") ||
2337 !_cups_strcasecmp(token
, "WITH-HOSTNAME") ||
2338 !_cups_strcasecmp(token
, "WITH-RESOURCE") ||
2339 !_cups_strcasecmp(token
, "WITH-SCHEME") ||
2340 !_cups_strcasecmp(token
, "WITH-VALUE"))
2344 if (!_cups_strcasecmp(token
, "WITH-ALL-HOSTNAMES") ||
2345 !_cups_strcasecmp(token
, "WITH-HOSTNAME"))
2346 last_expect
->with_flags
= _CUPS_WITH_HOSTNAME
;
2347 else if (!_cups_strcasecmp(token
, "WITH-ALL-RESOURCES") ||
2348 !_cups_strcasecmp(token
, "WITH-RESOURCE"))
2349 last_expect
->with_flags
= _CUPS_WITH_RESOURCE
;
2350 else if (!_cups_strcasecmp(token
, "WITH-ALL-SCHEMES") ||
2351 !_cups_strcasecmp(token
, "WITH-SCHEME"))
2352 last_expect
->with_flags
= _CUPS_WITH_SCHEME
;
2354 if (!_cups_strncasecmp(token
, "WITH-ALL-", 9))
2355 last_expect
->with_flags
|= _CUPS_WITH_ALL
;
2358 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
2360 print_fatal_error(outfile
, "Missing %s value on line %d.", token
, linenum
);
2368 * Expand any variables in the value and then save it.
2371 expand_variables(vars
, token
, temp
, sizeof(token
));
2373 tokenptr
= token
+ strlen(token
) - 1;
2375 if (token
[0] == '/' && tokenptr
> token
&& *tokenptr
== '/')
2378 * WITH-VALUE is a POSIX extended regular expression.
2381 last_expect
->with_value
= calloc(1, (size_t)(tokenptr
- token
));
2382 last_expect
->with_flags
|= _CUPS_WITH_REGEX
;
2384 if (last_expect
->with_value
)
2385 memcpy(last_expect
->with_value
, token
+ 1, (size_t)(tokenptr
- token
- 1));
2390 * WITH-VALUE is a literal value...
2393 char *ptr
; /* Pointer into value */
2395 for (ptr
= token
; *ptr
; ptr
++)
2397 if (*ptr
== '\\' && ptr
[1])
2400 * Remove \ from \foo...
2403 _cups_strcpy(ptr
, ptr
+ 1);
2407 last_expect
->with_value
= strdup(token
);
2408 last_expect
->with_flags
|= _CUPS_WITH_LITERAL
;
2413 print_fatal_error(outfile
, "%s without a preceding EXPECT on line %d.", token
,
2419 else if (!_cups_strcasecmp(token
, "WITH-VALUE-FROM"))
2421 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
2423 print_fatal_error(outfile
, "Missing %s value on line %d.", token
, linenum
);
2431 * Expand any variables in the value and then save it.
2434 expand_variables(vars
, token
, temp
, sizeof(token
));
2436 last_expect
->with_value_from
= strdup(token
);
2437 last_expect
->with_flags
= _CUPS_WITH_LITERAL
;
2441 print_fatal_error(outfile
, "%s without a preceding EXPECT on line %d.", token
,
2447 else if (!_cups_strcasecmp(token
, "DISPLAY"))
2450 * Display attributes...
2453 if (num_displayed
>= (int)(sizeof(displayed
) / sizeof(displayed
[0])))
2455 print_fatal_error(outfile
, "Too many DISPLAY's on line %d", linenum
);
2460 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2462 print_fatal_error(outfile
, "Missing DISPLAY name on line %d.", linenum
);
2467 displayed
[num_displayed
] = strdup(token
);
2472 print_fatal_error(outfile
, "Unexpected token %s seen on line %d.", token
,
2480 * Submit the IPP request...
2485 ippSetVersion(request
, version
/ 10, version
% 10);
2486 ippSetOperation(request
, op
);
2487 ippSetRequestId(request
, request_id
);
2489 if (Output
== _CUPS_OUTPUT_PLIST
)
2491 fputs("<dict>\n", outfile
);
2492 fputs("<key>Name</key>\n", outfile
);
2493 print_xml_string(outfile
, "string", name
);
2496 fputs("<key>FileId</key>\n", outfile
);
2497 print_xml_string(outfile
, "string", file_id
);
2501 fputs("<key>TestId</key>\n", outfile
);
2502 print_xml_string(outfile
, "string", test_id
);
2504 fputs("<key>Version</key>\n", outfile
);
2505 fprintf(outfile
, "<string>%d.%d</string>\n", version
/ 10, version
% 10);
2506 fputs("<key>Operation</key>\n", outfile
);
2507 print_xml_string(outfile
, "string", ippOpString(op
));
2508 fputs("<key>RequestId</key>\n", outfile
);
2509 fprintf(outfile
, "<integer>%d</integer>\n", request_id
);
2510 fputs("<key>RequestAttributes</key>\n", outfile
);
2511 fputs("<array>\n", outfile
);
2514 fputs("<dict>\n", outfile
);
2515 for (attrptr
= request
->attrs
,
2516 group
= attrptr
? attrptr
->group_tag
: IPP_TAG_ZERO
;
2518 attrptr
= attrptr
->next
)
2519 print_attr(outfile
, Output
, attrptr
, &group
);
2520 fputs("</dict>\n", outfile
);
2522 fputs("</array>\n", outfile
);
2525 if (Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
!= stdout
))
2529 printf(" %s:\n", ippOpString(op
));
2531 for (attrptr
= request
->attrs
; attrptr
; attrptr
= attrptr
->next
)
2532 print_attr(stdout
, _CUPS_OUTPUT_TEST
, attrptr
, NULL
);
2535 printf(" %-68.68s [", name
);
2539 if ((skip_previous
&& !prev_pass
) || skip_test
)
2546 if (Output
== _CUPS_OUTPUT_PLIST
)
2548 fputs("<key>Successful</key>\n", outfile
);
2549 fputs("<true />\n", outfile
);
2550 fputs("<key>Skipped</key>\n", outfile
);
2551 fputs("<true />\n", outfile
);
2552 fputs("<key>StatusCode</key>\n", outfile
);
2553 print_xml_string(outfile
, "string", "skip");
2554 fputs("<key>ResponseAttributes</key>\n", outfile
);
2555 fputs("<dict />\n", outfile
);
2558 if (Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
!= stdout
))
2566 repeat_interval
= 1;
2573 status
= HTTP_STATUS_OK
;
2575 if (transfer
== _CUPS_TRANSFER_CHUNKED
||
2576 (transfer
== _CUPS_TRANSFER_AUTO
&& filename
[0]))
2579 * Send request using chunking - a 0 length means "chunk".
2587 * Send request using content length...
2590 length
= ippLength(request
);
2592 if (filename
[0] && (reqfile
= cupsFileOpen(filename
, "r")) != NULL
)
2595 * Read the file to get the uncompressed file size...
2598 while ((bytes
= cupsFileRead(reqfile
, buffer
, sizeof(buffer
))) > 0)
2599 length
+= (size_t)bytes
;
2601 cupsFileClose(reqfile
);
2606 * Send the request...
2613 if (status
!= HTTP_STATUS_ERROR
)
2615 while (!response
&& !Cancel
&& prev_pass
)
2617 status
= cupsSendRequest(http
, request
, resource
, length
);
2621 httpSetField(http
, HTTP_FIELD_CONTENT_ENCODING
, compression
);
2622 #endif /* HAVE_LIBZ */
2624 if (!Cancel
&& status
== HTTP_STATUS_CONTINUE
&&
2625 request
->state
== IPP_DATA
&& filename
[0])
2627 if ((reqfile
= cupsFileOpen(filename
, "r")) != NULL
)
2630 (bytes
= cupsFileRead(reqfile
, buffer
, sizeof(buffer
))) > 0)
2631 if ((status
= cupsWriteRequestData(http
, buffer
, (size_t)bytes
)) != HTTP_STATUS_CONTINUE
)
2634 cupsFileClose(reqfile
);
2638 snprintf(buffer
, sizeof(buffer
), "%s: %s", filename
,
2640 _cupsSetError(IPP_INTERNAL_ERROR
, buffer
, 0);
2642 status
= HTTP_STATUS_ERROR
;
2647 * Get the server's response...
2650 if (!Cancel
&& status
!= HTTP_STATUS_ERROR
)
2652 response
= cupsGetResponse(http
, resource
);
2653 status
= httpGetStatus(http
);
2656 if (!Cancel
&& status
== HTTP_STATUS_ERROR
&& http
->error
!= EINVAL
&&
2658 http
->error
!= WSAETIMEDOUT
)
2660 http
->error
!= ETIMEDOUT
)
2663 if (httpReconnect2(http
, 30000, NULL
))
2666 else if (status
== HTTP_STATUS_ERROR
|| status
== HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
)
2671 else if (status
!= HTTP_STATUS_OK
)
2675 if (status
== HTTP_STATUS_UNAUTHORIZED
)
2683 if (!Cancel
&& status
== HTTP_STATUS_ERROR
&& http
->error
!= EINVAL
&&
2685 http
->error
!= WSAETIMEDOUT
)
2687 http
->error
!= ETIMEDOUT
)
2690 if (httpReconnect2(http
, 30000, NULL
))
2693 else if (status
== HTTP_STATUS_ERROR
)
2696 httpReconnect2(http
, 30000, NULL
);
2700 else if (status
!= HTTP_STATUS_OK
)
2707 * Check results of request...
2710 cupsArrayClear(errors
);
2712 if (http
->version
!= HTTP_1_1
)
2713 add_stringf(errors
, "Bad HTTP version (%d.%d)", http
->version
/ 100,
2714 http
->version
% 100);
2719 * No response, log error...
2722 add_stringf(errors
, "IPP request failed with status %s (%s)",
2723 ippErrorString(cupsLastError()),
2724 cupsLastErrorString());
2729 * Collect common attribute values...
2732 if ((attrptr
= ippFindAttribute(response
, "job-id",
2733 IPP_TAG_INTEGER
)) != NULL
)
2735 snprintf(temp
, sizeof(temp
), "%d", attrptr
->values
[0].integer
);
2736 set_variable(outfile
, vars
, "job-id", temp
);
2739 if ((attrptr
= ippFindAttribute(response
, "job-uri",
2740 IPP_TAG_URI
)) != NULL
)
2741 set_variable(outfile
, vars
, "job-uri", attrptr
->values
[0].string
.text
);
2743 if ((attrptr
= ippFindAttribute(response
, "notify-subscription-id",
2744 IPP_TAG_INTEGER
)) != NULL
)
2746 snprintf(temp
, sizeof(temp
), "%d", attrptr
->values
[0].integer
);
2747 set_variable(outfile
, vars
, "notify-subscription-id", temp
);
2751 * Check response, validating groups and attributes and logging errors
2755 if (response
->state
!= IPP_DATA
)
2757 "Missing end-of-attributes-tag in response "
2758 "(RFC 2910 section 3.5.1)");
2761 (response
->request
.status
.version
[0] != (version
/ 10) ||
2762 response
->request
.status
.version
[1] != (version
% 10)))
2764 "Bad version %d.%d in response - expected %d.%d "
2765 "(RFC 2911 section 3.1.8).",
2766 response
->request
.status
.version
[0],
2767 response
->request
.status
.version
[1],
2768 version
/ 10, version
% 10);
2770 if (response
->request
.status
.request_id
!= request_id
)
2772 "Bad request ID %d in response - expected %d "
2773 "(RFC 2911 section 3.1.1)",
2774 response
->request
.status
.request_id
, request_id
);
2776 attrptr
= response
->attrs
;
2779 "Missing first attribute \"attributes-charset "
2780 "(charset)\" in group operation-attributes-tag "
2781 "(RFC 2911 section 3.1.4).");
2784 if (!attrptr
->name
||
2785 attrptr
->value_tag
!= IPP_TAG_CHARSET
||
2786 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2787 attrptr
->num_values
!= 1 ||
2788 strcmp(attrptr
->name
, "attributes-charset"))
2790 "Bad first attribute \"%s (%s%s)\" in group %s, "
2791 "expected \"attributes-charset (charset)\" in "
2792 "group operation-attributes-tag (RFC 2911 section "
2794 attrptr
->name
? attrptr
->name
: "(null)",
2795 attrptr
->num_values
> 1 ? "1setOf " : "",
2796 ippTagString(attrptr
->value_tag
),
2797 ippTagString(attrptr
->group_tag
));
2799 attrptr
= attrptr
->next
;
2802 "Missing second attribute \"attributes-natural-"
2803 "language (naturalLanguage)\" in group "
2804 "operation-attributes-tag (RFC 2911 section "
2806 else if (!attrptr
->name
||
2807 attrptr
->value_tag
!= IPP_TAG_LANGUAGE
||
2808 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2809 attrptr
->num_values
!= 1 ||
2810 strcmp(attrptr
->name
, "attributes-natural-language"))
2812 "Bad first attribute \"%s (%s%s)\" in group %s, "
2813 "expected \"attributes-natural-language "
2814 "(naturalLanguage)\" in group "
2815 "operation-attributes-tag (RFC 2911 section "
2817 attrptr
->name
? attrptr
->name
: "(null)",
2818 attrptr
->num_values
> 1 ? "1setOf " : "",
2819 ippTagString(attrptr
->value_tag
),
2820 ippTagString(attrptr
->group_tag
));
2823 if ((attrptr
= ippFindAttribute(response
, "status-message",
2824 IPP_TAG_ZERO
)) != NULL
)
2826 if (attrptr
->value_tag
!= IPP_TAG_TEXT
)
2828 "status-message (text(255)) has wrong value tag "
2829 "%s (RFC 2911 section 3.1.6.2).",
2830 ippTagString(attrptr
->value_tag
));
2831 if (attrptr
->group_tag
!= IPP_TAG_OPERATION
)
2833 "status-message (text(255)) has wrong group tag "
2834 "%s (RFC 2911 section 3.1.6.2).",
2835 ippTagString(attrptr
->group_tag
));
2836 if (attrptr
->num_values
!= 1)
2838 "status-message (text(255)) has %d values "
2839 "(RFC 2911 section 3.1.6.2).",
2840 attrptr
->num_values
);
2841 if (attrptr
->value_tag
== IPP_TAG_TEXT
&&
2842 strlen(attrptr
->values
[0].string
.text
) > 255)
2844 "status-message (text(255)) has bad length %d"
2845 " (RFC 2911 section 3.1.6.2).",
2846 (int)strlen(attrptr
->values
[0].string
.text
));
2849 if ((attrptr
= ippFindAttribute(response
, "detailed-status-message",
2850 IPP_TAG_ZERO
)) != NULL
)
2852 if (attrptr
->value_tag
!= IPP_TAG_TEXT
)
2854 "detailed-status-message (text(MAX)) has wrong "
2855 "value tag %s (RFC 2911 section 3.1.6.3).",
2856 ippTagString(attrptr
->value_tag
));
2857 if (attrptr
->group_tag
!= IPP_TAG_OPERATION
)
2859 "detailed-status-message (text(MAX)) has wrong "
2860 "group tag %s (RFC 2911 section 3.1.6.3).",
2861 ippTagString(attrptr
->group_tag
));
2862 if (attrptr
->num_values
!= 1)
2864 "detailed-status-message (text(MAX)) has %d values"
2865 " (RFC 2911 section 3.1.6.3).",
2866 attrptr
->num_values
);
2867 if (attrptr
->value_tag
== IPP_TAG_TEXT
&&
2868 strlen(attrptr
->values
[0].string
.text
) > 1023)
2870 "detailed-status-message (text(MAX)) has bad "
2871 "length %d (RFC 2911 section 3.1.6.3).",
2872 (int)strlen(attrptr
->values
[0].string
.text
));
2875 a
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
2877 for (attrptr
= response
->attrs
,
2878 group
= attrptr
? attrptr
->group_tag
: IPP_TAG_ZERO
;
2880 attrptr
= attrptr
->next
)
2882 if (attrptr
->group_tag
!= group
)
2884 int out_of_order
= 0; /* Are attribute groups out-of-order? */
2887 switch (attrptr
->group_tag
)
2892 case IPP_TAG_OPERATION
:
2896 case IPP_TAG_UNSUPPORTED_GROUP
:
2897 if (group
!= IPP_TAG_OPERATION
)
2902 case IPP_TAG_PRINTER
:
2903 if (group
!= IPP_TAG_OPERATION
&&
2904 group
!= IPP_TAG_UNSUPPORTED_GROUP
)
2908 case IPP_TAG_SUBSCRIPTION
:
2909 if (group
> attrptr
->group_tag
&&
2910 group
!= IPP_TAG_DOCUMENT
)
2915 if (group
> attrptr
->group_tag
)
2921 add_stringf(errors
, "Attribute groups out of order (%s < %s)",
2922 ippTagString(attrptr
->group_tag
),
2923 ippTagString(group
));
2925 if (attrptr
->group_tag
!= IPP_TAG_ZERO
)
2926 group
= attrptr
->group_tag
;
2929 validate_attr(outfile
, errors
, attrptr
);
2933 if (cupsArrayFind(a
, attrptr
->name
))
2934 add_stringf(errors
, "Duplicate \"%s\" attribute in %s group",
2935 attrptr
->name
, ippTagString(group
));
2937 cupsArrayAdd(a
, attrptr
->name
);
2944 * Now check the test-defined expected status-code and attribute
2948 for (i
= 0; i
< num_statuses
; i
++)
2950 if (statuses
[i
].if_defined
&&
2951 !get_variable(vars
, statuses
[i
].if_defined
))
2954 if (statuses
[i
].if_not_defined
&&
2955 get_variable(vars
, statuses
[i
].if_not_defined
))
2958 if (response
->request
.status
.status_code
== statuses
[i
].status
)
2960 if (statuses
[i
].repeat_match
&&
2961 repeat_count
< statuses
[i
].repeat_limit
)
2964 if (statuses
[i
].define_match
)
2965 set_variable(outfile
, vars
, statuses
[i
].define_match
, "1");
2971 if (statuses
[i
].repeat_no_match
&&
2972 repeat_count
< statuses
[i
].repeat_limit
)
2975 if (statuses
[i
].define_no_match
)
2977 set_variable(outfile
, vars
, statuses
[i
].define_no_match
, "1");
2983 if (i
== num_statuses
&& num_statuses
> 0)
2985 for (i
= 0; i
< num_statuses
; i
++)
2987 if (statuses
[i
].if_defined
&&
2988 !get_variable(vars
, statuses
[i
].if_defined
))
2991 if (statuses
[i
].if_not_defined
&&
2992 get_variable(vars
, statuses
[i
].if_not_defined
))
2995 if (!statuses
[i
].repeat_match
)
2996 add_stringf(errors
, "EXPECTED: STATUS %s (got %s)",
2997 ippErrorString(statuses
[i
].status
),
2998 ippErrorString(cupsLastError()));
3001 if ((attrptr
= ippFindAttribute(response
, "status-message",
3002 IPP_TAG_TEXT
)) != NULL
)
3003 add_stringf(errors
, "status-message=\"%s\"",
3004 attrptr
->values
[0].string
.text
);
3007 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
3009 if (expect
->if_defined
&& !get_variable(vars
, expect
->if_defined
))
3012 if (expect
->if_not_defined
&&
3013 get_variable(vars
, expect
->if_not_defined
))
3016 found
= ippFindAttribute(response
, expect
->name
, IPP_TAG_ZERO
);
3020 if ((found
&& expect
->not_expect
) ||
3021 (!found
&& !(expect
->not_expect
|| expect
->optional
)) ||
3022 (found
&& !expect_matches(expect
, found
->value_tag
)) ||
3023 (found
&& expect
->in_group
&&
3024 found
->group_tag
!= expect
->in_group
))
3026 if (expect
->define_no_match
)
3027 set_variable(outfile
, vars
, expect
->define_no_match
, "1");
3028 else if (!expect
->define_match
&& !expect
->define_value
)
3030 if (found
&& expect
->not_expect
)
3031 add_stringf(errors
, "NOT EXPECTED: %s", expect
->name
);
3032 else if (!found
&& !(expect
->not_expect
|| expect
->optional
))
3033 add_stringf(errors
, "EXPECTED: %s", expect
->name
);
3036 if (!expect_matches(expect
, found
->value_tag
))
3037 add_stringf(errors
, "EXPECTED: %s OF-TYPE %s (got %s)",
3038 expect
->name
, expect
->of_type
,
3039 ippTagString(found
->value_tag
));
3041 if (expect
->in_group
&& found
->group_tag
!= expect
->in_group
)
3042 add_stringf(errors
, "EXPECTED: %s IN-GROUP %s (got %s).",
3043 expect
->name
, ippTagString(expect
->in_group
),
3044 ippTagString(found
->group_tag
));
3048 if (expect
->repeat_no_match
&&
3049 repeat_count
< expect
->repeat_limit
)
3056 ippAttributeString(found
, buffer
, sizeof(buffer
));
3058 if (found
&& expect
->with_value_from
&& !with_value_from(NULL
, ippFindAttribute(response
, expect
->with_value_from
, IPP_TAG_ZERO
), found
, buffer
, sizeof(buffer
)))
3060 if (expect
->define_no_match
)
3061 set_variable(outfile
, vars
, expect
->define_no_match
, "1");
3062 else if (!expect
->define_match
&& !expect
->define_value
&& !expect
->repeat_match
&& !expect
->repeat_no_match
)
3064 add_stringf(errors
, "EXPECTED: %s WITH-VALUES-FROM %s", expect
->name
, expect
->with_value_from
);
3066 with_value_from(errors
, ippFindAttribute(response
, expect
->with_value_from
, IPP_TAG_ZERO
), found
, buffer
, sizeof(buffer
));
3069 if (expect
->repeat_no_match
&& repeat_count
< expect
->repeat_limit
)
3074 else if (found
&& !with_value(outfile
, NULL
, expect
->with_value
, expect
->with_flags
, found
, buffer
, sizeof(buffer
)))
3076 if (expect
->define_no_match
)
3077 set_variable(outfile
, vars
, expect
->define_no_match
, "1");
3078 else if (!expect
->define_match
&& !expect
->define_value
&&
3079 !expect
->repeat_match
&& !expect
->repeat_no_match
)
3081 if (expect
->with_flags
& _CUPS_WITH_REGEX
)
3082 add_stringf(errors
, "EXPECTED: %s %s /%s/",
3084 (expect
->with_flags
& _CUPS_WITH_ALL
) ?
3085 "WITH-ALL-VALUES" : "WITH-VALUE",
3086 expect
->with_value
);
3088 add_stringf(errors
, "EXPECTED: %s %s \"%s\"",
3090 (expect
->with_flags
& _CUPS_WITH_ALL
) ?
3091 "WITH-ALL-VALUES" : "WITH-VALUE",
3092 expect
->with_value
);
3094 with_value(outfile
, errors
, expect
->with_value
, expect
->with_flags
, found
,
3095 buffer
, sizeof(buffer
));
3098 if (expect
->repeat_no_match
&&
3099 repeat_count
< expect
->repeat_limit
)
3105 if (found
&& expect
->count
> 0 &&
3106 found
->num_values
!= expect
->count
)
3108 if (expect
->define_no_match
)
3109 set_variable(outfile
, vars
, expect
->define_no_match
, "1");
3110 else if (!expect
->define_match
&& !expect
->define_value
)
3112 add_stringf(errors
, "EXPECTED: %s COUNT %d (got %d)", expect
->name
,
3113 expect
->count
, found
->num_values
);
3116 if (expect
->repeat_no_match
&&
3117 repeat_count
< expect
->repeat_limit
)
3123 if (found
&& expect
->same_count_as
)
3125 attrptr
= ippFindAttribute(response
, expect
->same_count_as
,
3128 if (!attrptr
|| attrptr
->num_values
!= found
->num_values
)
3130 if (expect
->define_no_match
)
3131 set_variable(outfile
, vars
, expect
->define_no_match
, "1");
3132 else if (!expect
->define_match
&& !expect
->define_value
)
3136 "EXPECTED: %s (%d values) SAME-COUNT-AS %s "
3137 "(not returned)", expect
->name
,
3138 found
->num_values
, expect
->same_count_as
);
3139 else if (attrptr
->num_values
!= found
->num_values
)
3141 "EXPECTED: %s (%d values) SAME-COUNT-AS %s "
3142 "(%d values)", expect
->name
, found
->num_values
,
3143 expect
->same_count_as
, attrptr
->num_values
);
3146 if (expect
->repeat_no_match
&&
3147 repeat_count
< expect
->repeat_limit
)
3154 if (found
&& expect
->define_match
)
3155 set_variable(outfile
, vars
, expect
->define_match
, "1");
3157 if (found
&& expect
->define_value
)
3159 if (!expect
->with_value
)
3161 int last
= ippGetCount(found
) - 1;
3162 /* Last element in attribute */
3164 switch (ippGetValueTag(found
))
3167 case IPP_TAG_INTEGER
:
3168 snprintf(buffer
, sizeof(buffer
), "%d", ippGetInteger(found
, last
));
3171 case IPP_TAG_BOOLEAN
:
3172 if (ippGetBoolean(found
, last
))
3173 strlcpy(buffer
, "true", sizeof(buffer
));
3175 strlcpy(buffer
, "false", sizeof(buffer
));
3178 case IPP_TAG_RESOLUTION
:
3180 int xres
, /* Horizontal resolution */
3181 yres
; /* Vertical resolution */
3182 ipp_res_t units
; /* Resolution units */
3184 xres
= ippGetResolution(found
, last
, &yres
, &units
);
3187 snprintf(buffer
, sizeof(buffer
), "%d%s", xres
, units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
3189 snprintf(buffer
, sizeof(buffer
), "%dx%d%s", xres
, yres
, units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
3193 case IPP_TAG_CHARSET
:
3194 case IPP_TAG_KEYWORD
:
3195 case IPP_TAG_LANGUAGE
:
3196 case IPP_TAG_MIMETYPE
:
3198 case IPP_TAG_NAMELANG
:
3200 case IPP_TAG_TEXTLANG
:
3202 case IPP_TAG_URISCHEME
:
3203 strlcpy(buffer
, ippGetString(found
, last
, NULL
), sizeof(buffer
));
3207 ippAttributeString(found
, buffer
, sizeof(buffer
));
3212 set_variable(outfile
, vars
, expect
->define_value
, buffer
);
3215 if (found
&& expect
->repeat_match
&&
3216 repeat_count
< expect
->repeat_limit
)
3219 while (expect
->expect_all
&& (found
= ippFindNextAttribute(response
, expect
->name
, IPP_TAG_ZERO
)) != NULL
);
3224 * If we are going to repeat this test, sleep 1 second so we don't flood
3225 * the printer with requests...
3230 if (Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
!= stdout
))
3232 printf("%04d]\n", repeat_count
);
3235 if (num_displayed
> 0)
3237 for (attrptr
= ippFirstAttribute(response
); attrptr
; attrptr
= ippNextAttribute(response
))
3239 const char *attrname
= ippGetName(attrptr
);
3242 for (i
= 0; i
< num_displayed
; i
++)
3244 if (!strcmp(displayed
[i
], attrname
))
3246 print_attr(stdout
, _CUPS_OUTPUT_TEST
, attrptr
, NULL
);
3255 sleep((unsigned)repeat_interval
);
3256 repeat_interval
= _cupsNextDelay(repeat_interval
, &repeat_prev
);
3258 if (Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
!= stdout
))
3260 printf(" %-68.68s [", name
);
3265 while (repeat_test
);
3271 if (cupsArrayCount(errors
) > 0)
3272 prev_pass
= pass
= 0;
3279 if (Output
== _CUPS_OUTPUT_PLIST
)
3281 fputs("<key>Successful</key>\n", outfile
);
3282 fputs(prev_pass
? "<true />\n" : "<false />\n", outfile
);
3283 fputs("<key>StatusCode</key>\n", outfile
);
3284 print_xml_string(outfile
, "string", ippErrorString(cupsLastError()));
3285 fputs("<key>ResponseAttributes</key>\n", outfile
);
3286 fputs("<array>\n", outfile
);
3287 fputs("<dict>\n", outfile
);
3288 for (attrptr
= response
? response
->attrs
: NULL
,
3289 group
= attrptr
? attrptr
->group_tag
: IPP_TAG_ZERO
;
3291 attrptr
= attrptr
->next
)
3292 print_attr(outfile
, Output
, attrptr
, &group
);
3293 fputs("</dict>\n", outfile
);
3294 fputs("</array>\n", outfile
);
3297 if (Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
!= stdout
))
3299 puts(prev_pass
? "PASS]" : "FAIL]");
3301 if (!prev_pass
|| (Verbosity
&& response
))
3303 printf(" RECEIVED: %lu bytes in response\n",
3304 (unsigned long)ippLength(response
));
3305 printf(" status-code = %s (%s)\n", ippErrorString(cupsLastError()),
3306 cupsLastErrorString());
3308 if (Verbosity
&& response
)
3310 for (attrptr
= response
->attrs
;
3312 attrptr
= attrptr
->next
)
3313 print_attr(stdout
, _CUPS_OUTPUT_TEST
, attrptr
, NULL
);
3317 else if (!prev_pass
&& Output
!= _CUPS_OUTPUT_QUIET
)
3318 fprintf(stderr
, "%s\n", cupsLastErrorString());
3320 if (prev_pass
&& Output
>= _CUPS_OUTPUT_LIST
&& !Verbosity
&&
3323 size_t width
; /* Length of value */
3325 for (i
= 0; i
< num_displayed
; i
++)
3327 widths
[i
] = strlen(displayed
[i
]);
3329 for (attrptr
= ippFindAttribute(response
, displayed
[i
], IPP_TAG_ZERO
);
3331 attrptr
= ippFindNextAttribute(response
, displayed
[i
],
3334 width
= ippAttributeString(attrptr
, NULL
, 0);
3335 if (width
> widths
[i
])
3340 if (Output
== _CUPS_OUTPUT_CSV
)
3341 print_csv(outfile
, NULL
, num_displayed
, displayed
, widths
);
3343 print_line(outfile
, NULL
, num_displayed
, displayed
, widths
);
3345 attrptr
= response
->attrs
;
3349 while (attrptr
&& attrptr
->group_tag
<= IPP_TAG_OPERATION
)
3350 attrptr
= attrptr
->next
;
3354 if (Output
== _CUPS_OUTPUT_CSV
)
3355 print_csv(outfile
, attrptr
, num_displayed
, displayed
, widths
);
3357 print_line(outfile
, attrptr
, num_displayed
, displayed
, widths
);
3359 while (attrptr
&& attrptr
->group_tag
> IPP_TAG_OPERATION
)
3360 attrptr
= attrptr
->next
;
3364 else if (!prev_pass
)
3366 if (Output
== _CUPS_OUTPUT_PLIST
)
3368 fputs("<key>Errors</key>\n", outfile
);
3369 fputs("<array>\n", outfile
);
3371 for (error
= (char *)cupsArrayFirst(errors
);
3373 error
= (char *)cupsArrayNext(errors
))
3374 print_xml_string(outfile
, "string", error
);
3376 fputs("</array>\n", outfile
);
3379 if (Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
!= stdout
))
3381 for (error
= (char *)cupsArrayFirst(errors
);
3383 error
= (char *)cupsArrayNext(errors
))
3384 printf(" %s\n", error
);
3388 if (num_displayed
> 0 && !Verbosity
&& response
&& (Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
!= stdout
)))
3390 for (attrptr
= response
->attrs
;
3392 attrptr
= attrptr
->next
)
3396 for (i
= 0; i
< num_displayed
; i
++)
3398 if (!strcmp(displayed
[i
], attrptr
->name
))
3400 print_attr(outfile
, Output
, attrptr
, NULL
);
3410 if (Output
== _CUPS_OUTPUT_PLIST
)
3411 fputs("</dict>\n", outfile
);
3415 ippDelete(response
);
3418 for (i
= 0; i
< num_statuses
; i
++)
3420 if (statuses
[i
].if_defined
)
3421 free(statuses
[i
].if_defined
);
3422 if (statuses
[i
].if_not_defined
)
3423 free(statuses
[i
].if_not_defined
);
3424 if (statuses
[i
].define_match
)
3425 free(statuses
[i
].define_match
);
3426 if (statuses
[i
].define_no_match
)
3427 free(statuses
[i
].define_no_match
);
3431 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
3434 if (expect
->of_type
)
3435 free(expect
->of_type
);
3436 if (expect
->same_count_as
)
3437 free(expect
->same_count_as
);
3438 if (expect
->if_defined
)
3439 free(expect
->if_defined
);
3440 if (expect
->if_not_defined
)
3441 free(expect
->if_not_defined
);
3442 if (expect
->with_value
)
3443 free(expect
->with_value
);
3444 if (expect
->define_match
)
3445 free(expect
->define_match
);
3446 if (expect
->define_no_match
)
3447 free(expect
->define_no_match
);
3448 if (expect
->define_value
)
3449 free(expect
->define_value
);
3453 for (i
= 0; i
< num_displayed
; i
++)
3457 if (!ignore_errors
&& !prev_pass
)
3463 cupsArrayDelete(errors
);
3470 ippDelete(response
);
3472 for (i
= 0; i
< num_statuses
; i
++)
3474 if (statuses
[i
].if_defined
)
3475 free(statuses
[i
].if_defined
);
3476 if (statuses
[i
].if_not_defined
)
3477 free(statuses
[i
].if_not_defined
);
3478 if (statuses
[i
].define_match
)
3479 free(statuses
[i
].define_match
);
3480 if (statuses
[i
].define_no_match
)
3481 free(statuses
[i
].define_no_match
);
3484 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
3487 if (expect
->of_type
)
3488 free(expect
->of_type
);
3489 if (expect
->same_count_as
)
3490 free(expect
->same_count_as
);
3491 if (expect
->if_defined
)
3492 free(expect
->if_defined
);
3493 if (expect
->if_not_defined
)
3494 free(expect
->if_not_defined
);
3495 if (expect
->with_value
)
3496 free(expect
->with_value
);
3497 if (expect
->define_match
)
3498 free(expect
->define_match
);
3499 if (expect
->define_no_match
)
3500 free(expect
->define_no_match
);
3501 if (expect
->define_value
)
3502 free(expect
->define_value
);
3505 for (i
= 0; i
< num_displayed
; i
++)
3513 * 'expand_variables()' - Expand variables in a string.
3517 expand_variables(_cups_vars_t
*vars
, /* I - Variables */
3518 char *dst
, /* I - Destination string buffer */
3519 const char *src
, /* I - Source string */
3520 size_t dstsize
) /* I - Size of destination buffer */
3522 char *dstptr
, /* Pointer into destination */
3523 *dstend
, /* End of destination */
3524 temp
[256], /* Temporary string */
3525 *tempptr
; /* Pointer into temporary string */
3526 const char *value
; /* Value to substitute */
3530 dstend
= dst
+ dstsize
- 1;
3532 while (*src
&& dstptr
< dstend
)
3537 * Substitute a string/number...
3540 if (!strncmp(src
, "$$", 2))
3545 else if (!strncmp(src
, "$ENV[", 5))
3547 strlcpy(temp
, src
+ 5, sizeof(temp
));
3549 for (tempptr
= temp
; *tempptr
; tempptr
++)
3550 if (*tempptr
== ']')
3556 value
= getenv(temp
);
3557 src
+= tempptr
- temp
+ 5;
3564 strlcpy(temp
, src
, sizeof(temp
));
3565 if ((tempptr
= strchr(temp
, '}')) != NULL
)
3568 tempptr
= temp
+ strlen(temp
);
3572 strlcpy(temp
, src
+ 1, sizeof(temp
));
3574 for (tempptr
= temp
; *tempptr
; tempptr
++)
3575 if (!isalnum(*tempptr
& 255) && *tempptr
!= '-' && *tempptr
!= '_')
3582 if (!strcmp(temp
, "uri"))
3584 else if (!strcmp(temp
, "filename"))
3585 value
= vars
->filename
;
3586 else if (!strcmp(temp
, "scheme") || !strcmp(temp
, "method"))
3587 value
= vars
->scheme
;
3588 else if (!strcmp(temp
, "username"))
3589 value
= vars
->userpass
;
3590 else if (!strcmp(temp
, "hostname"))
3591 value
= vars
->hostname
;
3592 else if (!strcmp(temp
, "port"))
3594 snprintf(temp
, sizeof(temp
), "%d", vars
->port
);
3597 else if (!strcmp(temp
, "resource"))
3598 value
= vars
->resource
;
3599 else if (!strcmp(temp
, "user"))
3602 value
= get_variable(vars
, temp
);
3604 src
+= tempptr
- temp
+ 1;
3609 strlcpy(dstptr
, value
, (size_t)(dstend
- dstptr
+ 1));
3610 dstptr
+= strlen(dstptr
);
3622 * 'expect_matches()' - Return true if the tag matches the specification.
3625 static int /* O - 1 if matches, 0 otherwise */
3627 _cups_expect_t
*expect
, /* I - Expected attribute */
3628 ipp_tag_t value_tag
) /* I - Value tag for attribute */
3630 int match
; /* Match? */
3631 char *of_type
, /* Type name to match */
3632 *next
, /* Next name to match */
3633 sep
; /* Separator character */
3637 * If we don't expect a particular type, return immediately...
3640 if (!expect
->of_type
)
3644 * Parse the "of_type" value since the string can contain multiple attribute
3645 * types separated by "," or "|"...
3648 for (of_type
= expect
->of_type
, match
= 0; !match
&& *of_type
; of_type
= next
)
3651 * Find the next separator, and set it (temporarily) to nul if present.
3654 for (next
= of_type
; *next
&& *next
!= '|' && *next
!= ','; next
++);
3656 if ((sep
= *next
) != '\0')
3660 * Support some meta-types to make it easier to write the test file.
3663 if (!strcmp(of_type
, "text"))
3664 match
= value_tag
== IPP_TAG_TEXTLANG
|| value_tag
== IPP_TAG_TEXT
;
3665 else if (!strcmp(of_type
, "name"))
3666 match
= value_tag
== IPP_TAG_NAMELANG
|| value_tag
== IPP_TAG_NAME
;
3667 else if (!strcmp(of_type
, "collection"))
3668 match
= value_tag
== IPP_TAG_BEGIN_COLLECTION
;
3670 match
= value_tag
== ippTagValue(of_type
);
3673 * Restore the separator if we have one...
3685 * 'get_collection()' - Get a collection value from the current test file.
3688 static ipp_t
* /* O - Collection value */
3689 get_collection(FILE *outfile
, /* I - Output file */
3690 _cups_vars_t
*vars
, /* I - Variables */
3691 FILE *fp
, /* I - File to read from */
3692 int *linenum
) /* IO - Line number */
3694 char token
[1024], /* Token from file */
3695 temp
[1024], /* Temporary string */
3696 attr
[128]; /* Attribute name */
3697 ipp_tag_t value
; /* Current value type */
3698 ipp_t
*col
= ippNew(); /* Collection value */
3699 ipp_attribute_t
*lastcol
= NULL
; /* Last collection attribute */
3702 while (get_token(fp
, token
, sizeof(token
), linenum
) != NULL
)
3704 if (!strcmp(token
, "}"))
3706 else if (!strcmp(token
, "{") && lastcol
)
3709 * Another collection value
3712 ipp_t
*subcol
= get_collection(outfile
, vars
, fp
, linenum
);
3713 /* Collection value */
3716 ippSetCollection(col
, &lastcol
, ippGetCount(lastcol
), subcol
);
3720 else if (!_cups_strcasecmp(token
, "MEMBER"))
3728 if (!get_token(fp
, token
, sizeof(token
), linenum
))
3730 print_fatal_error(outfile
, "Missing MEMBER value tag on line %d.", *linenum
);
3734 if ((value
= ippTagValue(token
)) == IPP_TAG_ZERO
)
3736 print_fatal_error(outfile
, "Bad MEMBER value tag \"%s\" on line %d.", token
,
3741 if (!get_token(fp
, attr
, sizeof(attr
), linenum
))
3743 print_fatal_error(outfile
, "Missing MEMBER name on line %d.", *linenum
);
3747 if (!get_token(fp
, temp
, sizeof(temp
), linenum
))
3749 print_fatal_error(outfile
, "Missing MEMBER value on line %d.", *linenum
);
3753 expand_variables(vars
, token
, temp
, sizeof(token
));
3757 case IPP_TAG_BOOLEAN
:
3758 if (!_cups_strcasecmp(token
, "true"))
3759 ippAddBoolean(col
, IPP_TAG_ZERO
, attr
, 1);
3761 ippAddBoolean(col
, IPP_TAG_ZERO
, attr
, (char)atoi(token
));
3764 case IPP_TAG_INTEGER
:
3766 ippAddInteger(col
, IPP_TAG_ZERO
, value
, attr
, atoi(token
));
3769 case IPP_TAG_RESOLUTION
:
3771 int xres
, /* X resolution */
3772 yres
; /* Y resolution */
3773 char units
[6]; /* Units */
3775 if (sscanf(token
, "%dx%d%5s", &xres
, &yres
, units
) != 3 ||
3776 (_cups_strcasecmp(units
, "dpi") &&
3777 _cups_strcasecmp(units
, "dpc") &&
3778 _cups_strcasecmp(units
, "dpcm") &&
3779 _cups_strcasecmp(units
, "other")))
3781 print_fatal_error(outfile
, "Bad resolution value \"%s\" on line %d.",
3786 if (!_cups_strcasecmp(units
, "dpi"))
3787 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, IPP_RES_PER_INCH
, xres
, yres
);
3788 else if (!_cups_strcasecmp(units
, "dpc") ||
3789 !_cups_strcasecmp(units
, "dpcm"))
3790 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, IPP_RES_PER_CM
, xres
, yres
);
3792 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, (ipp_res_t
)0, xres
, yres
);
3796 case IPP_TAG_RANGE
:
3798 int lowers
[4], /* Lower value */
3799 uppers
[4], /* Upper values */
3800 num_vals
; /* Number of values */
3803 num_vals
= sscanf(token
, "%d-%d,%d-%d,%d-%d,%d-%d",
3804 lowers
+ 0, uppers
+ 0,
3805 lowers
+ 1, uppers
+ 1,
3806 lowers
+ 2, uppers
+ 2,
3807 lowers
+ 3, uppers
+ 3);
3809 if ((num_vals
& 1) || num_vals
== 0)
3811 print_fatal_error(outfile
, "Bad rangeOfInteger value \"%s\" on line %d.",
3816 ippAddRanges(col
, IPP_TAG_ZERO
, attr
, num_vals
/ 2, lowers
,
3821 case IPP_TAG_BEGIN_COLLECTION
:
3822 if (!strcmp(token
, "{"))
3824 ipp_t
*subcol
= get_collection(outfile
, vars
, fp
, linenum
);
3825 /* Collection value */
3829 lastcol
= ippAddCollection(col
, IPP_TAG_ZERO
, attr
, subcol
);
3837 print_fatal_error(outfile
, "Bad collection value on line %d.", *linenum
);
3841 case IPP_TAG_STRING
:
3842 ippAddOctetString(col
, IPP_TAG_ZERO
, attr
, token
, (int)strlen(token
));
3846 if (!strchr(token
, ','))
3847 ippAddString(col
, IPP_TAG_ZERO
, value
, attr
, NULL
, token
);
3851 * Multiple string values...
3854 int num_values
; /* Number of values */
3855 char *values
[100], /* Values */
3856 *ptr
; /* Pointer to next value */
3862 for (ptr
= strchr(token
, ','); ptr
; ptr
= strchr(ptr
, ','))
3865 values
[num_values
] = ptr
;
3869 ippAddStrings(col
, IPP_TAG_ZERO
, value
, attr
, num_values
,
3870 NULL
, (const char **)values
);
3877 print_fatal_error(outfile
, "Unexpected token %s seen on line %d.", token
, *linenum
);
3885 * If we get here there was a parse error; free memory and return.
3897 * 'get_filename()' - Get a filename based on the current test file.
3900 static char * /* O - Filename */
3901 get_filename(const char *testfile
, /* I - Current test file */
3902 char *dst
, /* I - Destination filename */
3903 const char *src
, /* I - Source filename */
3904 size_t dstsize
) /* I - Size of destination buffer */
3906 char *dstptr
; /* Pointer into destination */
3907 _cups_globals_t
*cg
= _cupsGlobals();
3911 if (*src
== '<' && src
[strlen(src
) - 1] == '>')
3914 * Map <filename> to CUPS_DATADIR/ipptool/filename...
3917 snprintf(dst
, dstsize
, "%s/ipptool/%s", cg
->cups_datadir
, src
+ 1);
3918 dstptr
= dst
+ strlen(dst
) - 1;
3922 else if (*src
== '/' || !strchr(testfile
, '/')
3924 || (isalpha(*src
& 255) && src
[1] == ':')
3929 * Use the path as-is...
3932 strlcpy(dst
, src
, dstsize
);
3937 * Make path relative to testfile...
3940 strlcpy(dst
, testfile
, dstsize
);
3941 if ((dstptr
= strrchr(dst
, '/')) != NULL
)
3944 dstptr
= dst
; /* Should never happen */
3946 strlcpy(dstptr
, src
, dstsize
- (size_t)(dstptr
- dst
));
3954 * 'get_string()' - Get a pointer to a string value or the portion of interest.
3957 static const char * /* O - Pointer to string */
3958 get_string(ipp_attribute_t
*attr
, /* I - IPP attribute */
3959 int element
, /* I - Element to fetch */
3960 int flags
, /* I - Value ("with") flags */
3961 char *buffer
, /* I - Temporary buffer */
3962 size_t bufsize
) /* I - Size of temporary buffer */
3964 const char *value
; /* Value */
3965 char *ptr
, /* Pointer into value */
3966 scheme
[256], /* URI scheme */
3967 userpass
[256], /* Username/password */
3968 hostname
[256], /* Hostname */
3969 resource
[1024]; /* Resource */
3970 int port
; /* Port number */
3973 value
= ippGetString(attr
, element
, NULL
);
3975 if (flags
& _CUPS_WITH_HOSTNAME
)
3977 if (httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), buffer
, (int)bufsize
, &port
, resource
, sizeof(resource
)) < HTTP_URI_STATUS_OK
)
3980 ptr
= buffer
+ strlen(buffer
) - 1;
3981 if (ptr
>= buffer
&& *ptr
== '.')
3982 *ptr
= '\0'; /* Drop trailing "." */
3986 else if (flags
& _CUPS_WITH_RESOURCE
)
3988 if (httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), hostname
, sizeof(hostname
), &port
, buffer
, (int)bufsize
) < HTTP_URI_STATUS_OK
)
3993 else if (flags
& _CUPS_WITH_SCHEME
)
3995 if (httpSeparateURI(HTTP_URI_CODING_ALL
, value
, buffer
, (int)bufsize
, userpass
, sizeof(userpass
), hostname
, sizeof(hostname
), &port
, resource
, sizeof(resource
)) < HTTP_URI_STATUS_OK
)
4000 else if (ippGetValueTag(attr
) == IPP_TAG_URI
&& (!strncmp(value
, "ipp://", 6) || !strncmp(value
, "http://", 7) || !strncmp(value
, "ipps://", 7) || !strncmp(value
, "https://", 8)))
4002 http_uri_status_t status
= httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), hostname
, sizeof(hostname
), &port
, resource
, sizeof(resource
));
4004 if (status
< HTTP_URI_STATUS_OK
)
4015 * Normalize URI with no trailing dot...
4018 if ((ptr
= hostname
+ strlen(hostname
) - 1) >= hostname
&& *ptr
== '.')
4021 httpAssembleURI(HTTP_URI_CODING_ALL
, buffer
, (int)bufsize
, scheme
, userpass
, hostname
, port
, resource
);
4032 * 'get_token()' - Get a token from a file.
4035 static char * /* O - Token from file or NULL on EOF */
4036 get_token(FILE *fp
, /* I - File to read from */
4037 char *buf
, /* I - Buffer to read into */
4038 int buflen
, /* I - Length of buffer */
4039 int *linenum
) /* IO - Current line number */
4041 int ch
, /* Character from file */
4042 quote
; /* Quoting character */
4043 char *bufptr
, /* Pointer into buffer */
4044 *bufend
; /* End of buffer */
4050 * Skip whitespace...
4053 while (isspace(ch
= getc(fp
)))
4065 else if (ch
== '\'' || ch
== '\"')
4068 * Quoted text or regular expression...
4073 bufend
= buf
+ buflen
- 1;
4075 while ((ch
= getc(fp
)) != EOF
)
4080 * Escape next character...
4083 if (bufptr
< bufend
)
4084 *bufptr
++ = (char)ch
;
4086 if ((ch
= getc(fp
)) != EOF
&& bufptr
< bufend
)
4087 *bufptr
++ = (char)ch
;
4089 else if (ch
== quote
)
4091 else if (bufptr
< bufend
)
4092 *bufptr
++ = (char)ch
;
4105 while ((ch
= getc(fp
)) != EOF
)
4111 else if (ch
== '{' || ch
== '}' || ch
== ',')
4121 * Whitespace delimited text...
4127 bufend
= buf
+ buflen
- 1;
4129 while ((ch
= getc(fp
)) != EOF
)
4130 if (isspace(ch
) || ch
== '#')
4132 else if (bufptr
< bufend
)
4133 *bufptr
++ = (char)ch
;
4137 else if (ch
== '\n')
4149 * 'get_variable()' - Get the value of a variable.
4152 static char * /* O - Value or NULL */
4153 get_variable(_cups_vars_t
*vars
, /* I - Variables */
4154 const char *name
) /* I - Variable name */
4156 _cups_var_t key
, /* Search key */
4157 *match
; /* Matching variable, if any */
4160 key
.name
= (char *)name
;
4161 match
= cupsArrayFind(vars
->vars
, &key
);
4163 return (match
? match
->value
: NULL
);
4168 * 'iso_date()' - Return an ISO 8601 date/time string for the given IPP dateTime
4172 static char * /* O - ISO 8601 date/time string */
4173 iso_date(ipp_uchar_t
*date
) /* I - IPP (RFC 1903) date/time value */
4175 time_t utctime
; /* UTC time since 1970 */
4176 struct tm
*utcdate
; /* UTC date/time */
4177 static char buffer
[255]; /* String buffer */
4180 utctime
= ippDateToTime(date
);
4181 utcdate
= gmtime(&utctime
);
4183 snprintf(buffer
, sizeof(buffer
), "%04d-%02d-%02dT%02d:%02d:%02dZ",
4184 utcdate
->tm_year
+ 1900, utcdate
->tm_mon
+ 1, utcdate
->tm_mday
,
4185 utcdate
->tm_hour
, utcdate
->tm_min
, utcdate
->tm_sec
);
4192 * 'password_cb()' - Password callback for authenticated tests.
4195 static const char * /* O - Password */
4196 password_cb(const char *prompt
) /* I - Prompt (unused) */
4200 if (PasswordTries
< 3)
4204 cupsSetUser(Username
);
4214 * 'pause_message()' - Display the message and pause until the user presses a key.
4218 pause_message(const char *message
) /* I - Message */
4221 HANDLE tty
; /* Console handle */
4222 DWORD mode
; /* Console mode */
4223 char key
; /* Key press */
4224 DWORD bytes
; /* Bytes read for key press */
4228 * Disable input echo and set raw input...
4231 if ((tty
= GetStdHandle(STD_INPUT_HANDLE
)) == INVALID_HANDLE_VALUE
)
4234 if (!GetConsoleMode(tty
, &mode
))
4237 if (!SetConsoleMode(tty
, 0))
4241 int tty
; /* /dev/tty - never read from stdin */
4242 struct termios original
, /* Original input mode */
4243 noecho
; /* No echo input mode */
4244 char key
; /* Current key press */
4248 * Disable input echo and set raw input...
4251 if ((tty
= open("/dev/tty", O_RDONLY
)) < 0)
4254 if (tcgetattr(tty
, &original
))
4261 noecho
.c_lflag
&= (tcflag_t
)~(ICANON
| ECHO
| ECHOE
| ISIG
);
4263 if (tcsetattr(tty
, TCSAFLUSH
, &noecho
))
4271 * Display the prompt...
4274 printf("%s\n---- PRESS ANY KEY ----", message
);
4282 ReadFile(tty
, &key
, 1, &bytes
, NULL
);
4288 SetConsoleMode(tty
, mode
);
4301 tcsetattr(tty
, TCSAFLUSH
, &original
);
4306 * Erase the "press any key" prompt...
4309 fputs("\r \r", stdout
);
4315 * 'print_attr()' - Print an attribute on the screen.
4319 print_attr(FILE *outfile
, /* I - Output file */
4320 int format
, /* I - Output format */
4321 ipp_attribute_t
*attr
, /* I - Attribute to print */
4322 ipp_tag_t
*group
) /* IO - Current group */
4324 int i
; /* Looping var */
4325 ipp_attribute_t
*colattr
; /* Collection attribute */
4328 if (format
== _CUPS_OUTPUT_PLIST
)
4330 if (!attr
->name
|| (group
&& *group
!= attr
->group_tag
))
4332 if (attr
->group_tag
!= IPP_TAG_ZERO
)
4334 fputs("</dict>\n", outfile
);
4335 fputs("<dict>\n", outfile
);
4339 *group
= attr
->group_tag
;
4345 print_xml_string(outfile
, "key", attr
->name
);
4346 if (attr
->num_values
> 1)
4347 fputs("<array>\n", outfile
);
4349 switch (attr
->value_tag
)
4351 case IPP_TAG_INTEGER
:
4353 for (i
= 0; i
< attr
->num_values
; i
++)
4354 fprintf(outfile
, "<integer>%d</integer>\n", attr
->values
[i
].integer
);
4357 case IPP_TAG_BOOLEAN
:
4358 for (i
= 0; i
< attr
->num_values
; i
++)
4359 fputs(attr
->values
[i
].boolean
? "<true />\n" : "<false />\n", outfile
);
4362 case IPP_TAG_RANGE
:
4363 for (i
= 0; i
< attr
->num_values
; i
++)
4364 fprintf(outfile
, "<dict><key>lower</key><integer>%d</integer>"
4365 "<key>upper</key><integer>%d</integer></dict>\n",
4366 attr
->values
[i
].range
.lower
, attr
->values
[i
].range
.upper
);
4369 case IPP_TAG_RESOLUTION
:
4370 for (i
= 0; i
< attr
->num_values
; i
++)
4371 fprintf(outfile
, "<dict><key>xres</key><integer>%d</integer>"
4372 "<key>yres</key><integer>%d</integer>"
4373 "<key>units</key><string>%s</string></dict>\n",
4374 attr
->values
[i
].resolution
.xres
,
4375 attr
->values
[i
].resolution
.yres
,
4376 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
4381 for (i
= 0; i
< attr
->num_values
; i
++)
4382 fprintf(outfile
, "<date>%s</date>\n", iso_date(attr
->values
[i
].date
));
4385 case IPP_TAG_STRING
:
4386 for (i
= 0; i
< attr
->num_values
; i
++)
4388 char buffer
[IPP_MAX_LENGTH
* 5 / 4 + 1];
4391 fprintf(outfile
, "<data>%s</data>\n",
4392 httpEncode64_2(buffer
, sizeof(buffer
),
4393 attr
->values
[i
].unknown
.data
,
4394 attr
->values
[i
].unknown
.length
));
4400 case IPP_TAG_KEYWORD
:
4402 case IPP_TAG_URISCHEME
:
4403 case IPP_TAG_CHARSET
:
4404 case IPP_TAG_LANGUAGE
:
4405 case IPP_TAG_MIMETYPE
:
4406 for (i
= 0; i
< attr
->num_values
; i
++)
4407 print_xml_string(outfile
, "string", attr
->values
[i
].string
.text
);
4410 case IPP_TAG_TEXTLANG
:
4411 case IPP_TAG_NAMELANG
:
4412 for (i
= 0; i
< attr
->num_values
; i
++)
4414 fputs("<dict><key>language</key><string>", outfile
);
4415 print_xml_string(outfile
, NULL
, attr
->values
[i
].string
.language
);
4416 fputs("</string><key>string</key><string>", outfile
);
4417 print_xml_string(outfile
, NULL
, attr
->values
[i
].string
.text
);
4418 fputs("</string></dict>\n", outfile
);
4422 case IPP_TAG_BEGIN_COLLECTION
:
4423 for (i
= 0; i
< attr
->num_values
; i
++)
4425 fputs("<dict>\n", outfile
);
4426 for (colattr
= attr
->values
[i
].collection
->attrs
;
4428 colattr
= colattr
->next
)
4429 print_attr(outfile
, format
, colattr
, NULL
);
4430 fputs("</dict>\n", outfile
);
4435 fprintf(outfile
, "<string><<%s>></string>\n", ippTagString(attr
->value_tag
));
4439 if (attr
->num_values
> 1)
4440 fputs("</array>\n", outfile
);
4444 char buffer
[131072]; /* Value buffer */
4446 if (format
== _CUPS_OUTPUT_TEST
)
4450 fputs(" -- separator --\n", outfile
);
4454 fprintf(outfile
, " %s (%s%s) = ", attr
->name
, attr
->num_values
> 1 ? "1setOf " : "", ippTagString(attr
->value_tag
));
4457 ippAttributeString(attr
, buffer
, sizeof(buffer
));
4458 fprintf(outfile
, "%s\n", buffer
);
4464 * 'print_csv()' - Print a line of CSV text.
4469 FILE *outfile
, /* I - Output file */
4470 ipp_attribute_t
*attr
, /* I - First attribute for line */
4471 int num_displayed
, /* I - Number of attributes to display */
4472 char **displayed
, /* I - Attributes to display */
4473 size_t *widths
) /* I - Column widths */
4475 int i
; /* Looping var */
4476 size_t maxlength
; /* Max length of all columns */
4477 char *buffer
, /* String buffer */
4478 *bufptr
; /* Pointer into buffer */
4479 ipp_attribute_t
*current
; /* Current attribute */
4483 * Get the maximum string length we have to show and allocate...
4486 for (i
= 1, maxlength
= widths
[0]; i
< num_displayed
; i
++)
4487 if (widths
[i
] > maxlength
)
4488 maxlength
= widths
[i
];
4492 if ((buffer
= malloc(maxlength
)) == NULL
)
4496 * Loop through the attributes to display...
4501 for (i
= 0; i
< num_displayed
; i
++)
4504 fputc(',', outfile
);
4508 for (current
= attr
; current
; current
= current
->next
)
4512 else if (!strcmp(current
->name
, displayed
[i
]))
4514 ippAttributeString(current
, buffer
, maxlength
);
4519 if (strchr(buffer
, ',') != NULL
|| strchr(buffer
, '\"') != NULL
||
4520 strchr(buffer
, '\\') != NULL
)
4522 putc('\"', outfile
);
4523 for (bufptr
= buffer
; *bufptr
; bufptr
++)
4525 if (*bufptr
== '\\' || *bufptr
== '\"')
4526 putc('\\', outfile
);
4527 putc(*bufptr
, outfile
);
4529 putc('\"', outfile
);
4532 fputs(buffer
, outfile
);
4534 putc('\n', outfile
);
4538 for (i
= 0; i
< num_displayed
; i
++)
4543 fputs(displayed
[i
], outfile
);
4545 putc('\n', outfile
);
4553 * 'print_fatal_error()' - Print a fatal error message.
4557 print_fatal_error(FILE *outfile
, /* I - Output file */
4558 const char *s
, /* I - Printf-style format string */
4559 ...) /* I - Additional arguments as needed */
4561 char buffer
[10240]; /* Format buffer */
4562 va_list ap
; /* Pointer to arguments */
4566 * Format the error message...
4570 vsnprintf(buffer
, sizeof(buffer
), s
, ap
);
4577 if (Output
== _CUPS_OUTPUT_PLIST
)
4579 print_xml_header(outfile
);
4580 print_xml_trailer(outfile
, 0, buffer
);
4583 _cupsLangPrintf(stderr
, "ipptool: %s", buffer
);
4588 * 'print_line()' - Print a line of formatted or CSV text.
4593 FILE *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 ipp_attribute_t
*current
; /* Current attribute */
4606 * Get the maximum string length we have to show and allocate...
4609 for (i
= 1, maxlength
= widths
[0]; i
< num_displayed
; i
++)
4610 if (widths
[i
] > maxlength
)
4611 maxlength
= widths
[i
];
4615 if ((buffer
= malloc(maxlength
)) == NULL
)
4619 * Loop through the attributes to display...
4624 for (i
= 0; i
< num_displayed
; i
++)
4631 for (current
= attr
; current
; current
= current
->next
)
4635 else if (!strcmp(current
->name
, displayed
[i
]))
4637 ippAttributeString(current
, buffer
, maxlength
);
4642 fprintf(outfile
, "%*s", (int)-widths
[i
], buffer
);
4644 putc('\n', outfile
);
4648 for (i
= 0; i
< num_displayed
; i
++)
4653 fprintf(outfile
, "%*s", (int)-widths
[i
], displayed
[i
]);
4655 putc('\n', outfile
);
4657 for (i
= 0; i
< num_displayed
; i
++)
4662 memset(buffer
, '-', widths
[i
]);
4663 buffer
[widths
[i
]] = '\0';
4664 fputs(buffer
, outfile
);
4666 putc('\n', outfile
);
4674 * 'print_xml_header()' - Print a standard XML plist header.
4678 print_xml_header(FILE *outfile
) /* I - Output file */
4682 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", outfile
);
4683 fputs("<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
4684 "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n", outfile
);
4685 fputs("<plist version=\"1.0\">\n", outfile
);
4686 fputs("<dict>\n", outfile
);
4687 fputs("<key>ipptoolVersion</key>\n", outfile
);
4688 fputs("<string>" CUPS_SVERSION
"</string>\n", outfile
);
4689 fputs("<key>Transfer</key>\n", outfile
);
4690 fprintf(outfile
, "<string>%s</string>\n",
4691 Transfer
== _CUPS_TRANSFER_AUTO
? "auto" :
4692 Transfer
== _CUPS_TRANSFER_CHUNKED
? "chunked" : "length");
4693 fputs("<key>Tests</key>\n", outfile
);
4694 fputs("<array>\n", outfile
);
4702 * 'print_xml_string()' - Print an XML string with escaping.
4706 print_xml_string(FILE *outfile
, /* I - Output file */
4707 const char *element
, /* I - Element name or NULL */
4708 const char *s
) /* I - String to print */
4711 fprintf(outfile
, "<%s>", element
);
4716 fputs("&", outfile
);
4718 fputs("<", outfile
);
4720 fputs(">", outfile
);
4721 else if ((*s
& 0xe0) == 0xc0)
4724 * Validate UTF-8 two-byte sequence...
4727 if ((s
[1] & 0xc0) != 0x80)
4734 putc(*s
++, outfile
);
4738 else if ((*s
& 0xf0) == 0xe0)
4741 * Validate UTF-8 three-byte sequence...
4744 if ((s
[1] & 0xc0) != 0x80 || (s
[2] & 0xc0) != 0x80)
4751 putc(*s
++, outfile
);
4752 putc(*s
++, outfile
);
4756 else if ((*s
& 0xf8) == 0xf0)
4759 * Validate UTF-8 four-byte sequence...
4762 if ((s
[1] & 0xc0) != 0x80 || (s
[2] & 0xc0) != 0x80 ||
4763 (s
[3] & 0xc0) != 0x80)
4770 putc(*s
++, outfile
);
4771 putc(*s
++, outfile
);
4772 putc(*s
++, outfile
);
4776 else if ((*s
& 0x80) || (*s
< ' ' && !isspace(*s
& 255)))
4779 * Invalid control character...
4791 fprintf(outfile
, "</%s>\n", element
);
4796 * 'print_xml_trailer()' - Print the XML trailer with success/fail value.
4800 print_xml_trailer(FILE *outfile
, /* I - Output file */
4801 int success
, /* I - 1 on success, 0 on failure */
4802 const char *message
) /* I - Error message or NULL */
4806 fputs("</array>\n", outfile
);
4807 fputs("<key>Successful</key>\n", outfile
);
4808 fputs(success
? "<true />\n" : "<false />\n", outfile
);
4811 fputs("<key>ErrorMessage</key>\n", outfile
);
4812 print_xml_string(outfile
, "string", message
);
4814 fputs("</dict>\n", outfile
);
4815 fputs("</plist>\n", outfile
);
4823 * 'set_variable()' - Set a variable value.
4827 set_variable(FILE *outfile
, /* I - Output file */
4828 _cups_vars_t
*vars
, /* I - Variables */
4829 const char *name
, /* I - Variable name */
4830 const char *value
) /* I - Value string */
4832 _cups_var_t key
, /* Search key */
4833 *var
; /* New variable */
4836 if (!_cups_strcasecmp(name
, "filename"))
4839 free(vars
->filename
);
4841 vars
->filename
= strdup(value
);
4845 key
.name
= (char *)name
;
4846 if ((var
= cupsArrayFind(vars
->vars
, &key
)) != NULL
)
4849 var
->value
= strdup(value
);
4851 else if ((var
= malloc(sizeof(_cups_var_t
))) == NULL
)
4853 print_fatal_error(outfile
, "Unable to allocate memory for variable \"%s\".", name
);
4858 var
->name
= strdup(name
);
4859 var
->value
= strdup(value
);
4861 cupsArrayAdd(vars
->vars
, var
);
4868 * 'sigterm_handler()' - Handle SIGINT and SIGTERM.
4872 sigterm_handler(int sig
) /* I - Signal number (unused) */
4878 signal(SIGINT
, SIG_DFL
);
4879 signal(SIGTERM
, SIG_DFL
);
4885 * 'timeout_cb()' - Handle HTTP timeouts.
4888 static int /* O - 1 to continue, 0 to cancel */
4889 timeout_cb(http_t
*http
, /* I - Connection to server */
4890 void *user_data
) /* I - User data (unused) */
4892 int buffered
= 0; /* Bytes buffered but not yet sent */
4898 * If the socket still have data waiting to be sent to the printer (as can
4899 * happen if the printer runs out of paper), continue to wait until the output
4900 * buffer is empty...
4903 #ifdef SO_NWRITE /* macOS and some versions of Linux */
4904 socklen_t len
= sizeof(buffered
); /* Size of return value */
4906 if (getsockopt(httpGetFd(http
), SOL_SOCKET
, SO_NWRITE
, &buffered
, &len
))
4909 #elif defined(SIOCOUTQ) /* Others except Windows */
4910 if (ioctl(httpGetFd(http
), SIOCOUTQ
, &buffered
))
4913 #else /* Windows (not possible) */
4915 #endif /* SO_NWRITE */
4917 return (buffered
> 0);
4922 * 'usage()' - Show program usage.
4928 _cupsLangPuts(stderr
, _("Usage: ipptool [options] URI filename [ ... "
4930 _cupsLangPuts(stderr
, _("Options:"));
4931 _cupsLangPuts(stderr
, _(" --help Show help."));
4932 _cupsLangPuts(stderr
, _(" --stop-after-include-error\n"
4933 " Stop tests after a failed INCLUDE."));
4934 _cupsLangPuts(stderr
, _(" --version Show version."));
4935 _cupsLangPuts(stderr
, _(" -4 Connect using IPv4."));
4936 _cupsLangPuts(stderr
, _(" -6 Connect using IPv6."));
4937 _cupsLangPuts(stderr
, _(" -C Send requests using "
4938 "chunking (default)."));
4939 _cupsLangPuts(stdout
, _(" -E Test with HTTP Upgrade to "
4941 _cupsLangPuts(stderr
, _(" -I Ignore errors."));
4942 _cupsLangPuts(stderr
, _(" -L Send requests using "
4943 "content-length."));
4944 _cupsLangPuts(stderr
, _(" -P filename.plist Produce XML plist to a file and test report to standard output."));
4945 _cupsLangPuts(stderr
, _(" -S Test with SSL "
4947 _cupsLangPuts(stderr
, _(" -T seconds Set the receive/send "
4948 "timeout in seconds."));
4949 _cupsLangPuts(stderr
, _(" -V version Set default IPP "
4951 _cupsLangPuts(stderr
, _(" -X Produce XML plist instead "
4953 _cupsLangPuts(stderr
, _(" -c Produce CSV output."));
4954 _cupsLangPuts(stderr
, _(" -d name=value Set named variable to "
4956 _cupsLangPuts(stderr
, _(" -f filename Set default request "
4958 _cupsLangPuts(stderr
, _(" -i seconds Repeat the last file with "
4959 "the given time interval."));
4960 _cupsLangPuts(stderr
, _(" -l Produce plain text output."));
4961 _cupsLangPuts(stderr
, _(" -n count Repeat the last file the "
4962 "given number of times."));
4963 _cupsLangPuts(stderr
, _(" -q Run silently."));
4964 _cupsLangPuts(stderr
, _(" -t Produce a test report."));
4965 _cupsLangPuts(stderr
, _(" -v Be verbose."));
4972 * 'validate_attr()' - Determine whether an attribute is valid.
4975 static int /* O - 1 if valid, 0 otherwise */
4976 validate_attr(FILE *outfile
, /* I - Output file */
4977 cups_array_t
*errors
, /* I - Errors array */
4978 ipp_attribute_t
*attr
) /* I - Attribute to validate */
4980 int i
; /* Looping var */
4981 char scheme
[64], /* Scheme from URI */
4982 userpass
[256], /* Username/password from URI */
4983 hostname
[256], /* Hostname from URI */
4984 resource
[1024]; /* Resource from URI */
4985 int port
, /* Port number from URI */
4986 uri_status
, /* URI separation status */
4987 valid
= 1; /* Is the attribute valid? */
4988 const char *ptr
; /* Pointer into string */
4989 ipp_attribute_t
*colattr
; /* Collection attribute */
4990 regex_t re
; /* Regular expression */
4991 ipp_uchar_t
*date
; /* Current date value */
5002 * Validate the attribute name.
5005 for (ptr
= attr
->name
; *ptr
; ptr
++)
5006 if (!isalnum(*ptr
& 255) && *ptr
!= '-' && *ptr
!= '.' && *ptr
!= '_')
5009 if (*ptr
|| ptr
== attr
->name
)
5014 "\"%s\": Bad attribute name - invalid character "
5015 "(RFC 2911 section 4.1.3).", attr
->name
);
5018 if ((ptr
- attr
->name
) > 255)
5023 "\"%s\": Bad attribute name - bad length "
5024 "(RFC 2911 section 4.1.3).", attr
->name
);
5027 switch (attr
->value_tag
)
5029 case IPP_TAG_INTEGER
:
5032 case IPP_TAG_BOOLEAN
:
5033 for (i
= 0; i
< attr
->num_values
; i
++)
5035 if (attr
->values
[i
].boolean
!= 0 &&
5036 attr
->values
[i
].boolean
!= 1)
5041 "\"%s\": Bad boolen value %d "
5042 "(RFC 2911 section 4.1.11).", attr
->name
,
5043 attr
->values
[i
].boolean
);
5049 for (i
= 0; i
< attr
->num_values
; i
++)
5051 if (attr
->values
[i
].integer
< 1)
5056 "\"%s\": Bad enum value %d - out of range "
5057 "(RFC 2911 section 4.1.4).", attr
->name
,
5058 attr
->values
[i
].integer
);
5063 case IPP_TAG_STRING
:
5064 for (i
= 0; i
< attr
->num_values
; i
++)
5066 if (attr
->values
[i
].unknown
.length
> IPP_MAX_OCTETSTRING
)
5071 "\"%s\": Bad octetString value - bad length %d "
5072 "(RFC 2911 section 4.1.10).", attr
->name
,
5073 attr
->values
[i
].unknown
.length
);
5079 for (i
= 0; i
< attr
->num_values
; i
++)
5081 date
= attr
->values
[i
].date
;
5083 if (date
[2] < 1 || date
[2] > 12)
5088 "\"%s\": Bad dateTime month %u "
5089 "(RFC 2911 section 4.1.14).", attr
->name
, date
[2]);
5092 if (date
[3] < 1 || date
[3] > 31)
5097 "\"%s\": Bad dateTime day %u "
5098 "(RFC 2911 section 4.1.14).", attr
->name
, date
[3]);
5106 "\"%s\": Bad dateTime hours %u "
5107 "(RFC 2911 section 4.1.14).", attr
->name
, date
[4]);
5115 "\"%s\": Bad dateTime minutes %u "
5116 "(RFC 2911 section 4.1.14).", attr
->name
, date
[5]);
5124 "\"%s\": Bad dateTime seconds %u "
5125 "(RFC 2911 section 4.1.14).", attr
->name
, date
[6]);
5133 "\"%s\": Bad dateTime deciseconds %u "
5134 "(RFC 2911 section 4.1.14).", attr
->name
, date
[7]);
5137 if (date
[8] != '-' && date
[8] != '+')
5142 "\"%s\": Bad dateTime UTC sign '%c' "
5143 "(RFC 2911 section 4.1.14).", attr
->name
, date
[8]);
5151 "\"%s\": Bad dateTime UTC hours %u "
5152 "(RFC 2911 section 4.1.14).", attr
->name
, date
[9]);
5160 "\"%s\": Bad dateTime UTC minutes %u "
5161 "(RFC 2911 section 4.1.14).", attr
->name
, date
[10]);
5166 case IPP_TAG_RESOLUTION
:
5167 for (i
= 0; i
< attr
->num_values
; i
++)
5169 if (attr
->values
[i
].resolution
.xres
<= 0)
5174 "\"%s\": Bad resolution value %dx%d%s - cross "
5175 "feed resolution must be positive "
5176 "(RFC 2911 section 4.1.15).", attr
->name
,
5177 attr
->values
[i
].resolution
.xres
,
5178 attr
->values
[i
].resolution
.yres
,
5179 attr
->values
[i
].resolution
.units
==
5180 IPP_RES_PER_INCH
? "dpi" :
5181 attr
->values
[i
].resolution
.units
==
5182 IPP_RES_PER_CM
? "dpcm" : "unknown");
5185 if (attr
->values
[i
].resolution
.yres
<= 0)
5190 "\"%s\": Bad resolution value %dx%d%s - feed "
5191 "resolution must be positive "
5192 "(RFC 2911 section 4.1.15).", attr
->name
,
5193 attr
->values
[i
].resolution
.xres
,
5194 attr
->values
[i
].resolution
.yres
,
5195 attr
->values
[i
].resolution
.units
==
5196 IPP_RES_PER_INCH
? "dpi" :
5197 attr
->values
[i
].resolution
.units
==
5198 IPP_RES_PER_CM
? "dpcm" : "unknown");
5201 if (attr
->values
[i
].resolution
.units
!= IPP_RES_PER_INCH
&&
5202 attr
->values
[i
].resolution
.units
!= IPP_RES_PER_CM
)
5207 "\"%s\": Bad resolution value %dx%d%s - bad "
5208 "units value (RFC 2911 section 4.1.15).",
5209 attr
->name
, attr
->values
[i
].resolution
.xres
,
5210 attr
->values
[i
].resolution
.yres
,
5211 attr
->values
[i
].resolution
.units
==
5212 IPP_RES_PER_INCH
? "dpi" :
5213 attr
->values
[i
].resolution
.units
==
5214 IPP_RES_PER_CM
? "dpcm" : "unknown");
5219 case IPP_TAG_RANGE
:
5220 for (i
= 0; i
< attr
->num_values
; i
++)
5222 if (attr
->values
[i
].range
.lower
> attr
->values
[i
].range
.upper
)
5227 "\"%s\": Bad rangeOfInteger value %d-%d - lower "
5228 "greater than upper (RFC 2911 section 4.1.13).",
5229 attr
->name
, attr
->values
[i
].range
.lower
,
5230 attr
->values
[i
].range
.upper
);
5235 case IPP_TAG_BEGIN_COLLECTION
:
5236 for (i
= 0; i
< attr
->num_values
; i
++)
5238 for (colattr
= attr
->values
[i
].collection
->attrs
;
5240 colattr
= colattr
->next
)
5242 if (!validate_attr(outfile
, NULL
, colattr
))
5249 if (colattr
&& errors
)
5251 add_stringf(errors
, "\"%s\": Bad collection value.", attr
->name
);
5255 validate_attr(outfile
, errors
, colattr
);
5256 colattr
= colattr
->next
;
5263 case IPP_TAG_TEXTLANG
:
5264 for (i
= 0; i
< attr
->num_values
; i
++)
5266 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
5268 if ((*ptr
& 0xe0) == 0xc0)
5271 if ((*ptr
& 0xc0) != 0x80)
5274 else if ((*ptr
& 0xf0) == 0xe0)
5277 if ((*ptr
& 0xc0) != 0x80)
5280 if ((*ptr
& 0xc0) != 0x80)
5283 else if ((*ptr
& 0xf8) == 0xf0)
5286 if ((*ptr
& 0xc0) != 0x80)
5289 if ((*ptr
& 0xc0) != 0x80)
5292 if ((*ptr
& 0xc0) != 0x80)
5295 else if (*ptr
& 0x80)
5304 "\"%s\": Bad text value \"%s\" - bad UTF-8 "
5305 "sequence (RFC 2911 section 4.1.1).", attr
->name
,
5306 attr
->values
[i
].string
.text
);
5309 if ((ptr
- attr
->values
[i
].string
.text
) > (IPP_MAX_TEXT
- 1))
5314 "\"%s\": Bad text value \"%s\" - bad length %d "
5315 "(RFC 2911 section 4.1.1).", attr
->name
,
5316 attr
->values
[i
].string
.text
,
5317 (int)strlen(attr
->values
[i
].string
.text
));
5323 case IPP_TAG_NAMELANG
:
5324 for (i
= 0; i
< attr
->num_values
; i
++)
5326 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
5328 if ((*ptr
& 0xe0) == 0xc0)
5331 if ((*ptr
& 0xc0) != 0x80)
5334 else if ((*ptr
& 0xf0) == 0xe0)
5337 if ((*ptr
& 0xc0) != 0x80)
5340 if ((*ptr
& 0xc0) != 0x80)
5343 else if ((*ptr
& 0xf8) == 0xf0)
5346 if ((*ptr
& 0xc0) != 0x80)
5349 if ((*ptr
& 0xc0) != 0x80)
5352 if ((*ptr
& 0xc0) != 0x80)
5355 else if (*ptr
& 0x80)
5364 "\"%s\": Bad name value \"%s\" - bad UTF-8 "
5365 "sequence (RFC 2911 section 4.1.2).", attr
->name
,
5366 attr
->values
[i
].string
.text
);
5369 if ((ptr
- attr
->values
[i
].string
.text
) > (IPP_MAX_NAME
- 1))
5374 "\"%s\": Bad name value \"%s\" - bad length %d "
5375 "(RFC 2911 section 4.1.2).", attr
->name
,
5376 attr
->values
[i
].string
.text
,
5377 (int)strlen(attr
->values
[i
].string
.text
));
5382 case IPP_TAG_KEYWORD
:
5383 for (i
= 0; i
< attr
->num_values
; i
++)
5385 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
5386 if (!isalnum(*ptr
& 255) && *ptr
!= '-' && *ptr
!= '.' &&
5390 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
5395 "\"%s\": Bad keyword value \"%s\" - invalid "
5396 "character (RFC 2911 section 4.1.3).",
5397 attr
->name
, attr
->values
[i
].string
.text
);
5400 if ((ptr
- attr
->values
[i
].string
.text
) > (IPP_MAX_KEYWORD
- 1))
5405 "\"%s\": Bad keyword value \"%s\" - bad "
5406 "length %d (RFC 2911 section 4.1.3).",
5407 attr
->name
, attr
->values
[i
].string
.text
,
5408 (int)strlen(attr
->values
[i
].string
.text
));
5414 for (i
= 0; i
< attr
->num_values
; i
++)
5416 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
,
5417 attr
->values
[i
].string
.text
,
5418 scheme
, sizeof(scheme
),
5419 userpass
, sizeof(userpass
),
5420 hostname
, sizeof(hostname
),
5421 &port
, resource
, sizeof(resource
));
5423 if (uri_status
< HTTP_URI_OK
)
5428 "\"%s\": Bad URI value \"%s\" - %s "
5429 "(RFC 2911 section 4.1.5).", attr
->name
,
5430 attr
->values
[i
].string
.text
,
5431 httpURIStatusString(uri_status
));
5434 if (strlen(attr
->values
[i
].string
.text
) > (IPP_MAX_URI
- 1))
5439 "\"%s\": Bad URI value \"%s\" - bad length %d "
5440 "(RFC 2911 section 4.1.5).", attr
->name
,
5441 attr
->values
[i
].string
.text
,
5442 (int)strlen(attr
->values
[i
].string
.text
));
5447 case IPP_TAG_URISCHEME
:
5448 for (i
= 0; i
< attr
->num_values
; i
++)
5450 ptr
= attr
->values
[i
].string
.text
;
5451 if (islower(*ptr
& 255))
5453 for (ptr
++; *ptr
; ptr
++)
5454 if (!islower(*ptr
& 255) && !isdigit(*ptr
& 255) &&
5455 *ptr
!= '+' && *ptr
!= '-' && *ptr
!= '.')
5459 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
5464 "\"%s\": Bad uriScheme value \"%s\" - bad "
5465 "characters (RFC 2911 section 4.1.6).",
5466 attr
->name
, attr
->values
[i
].string
.text
);
5469 if ((ptr
- attr
->values
[i
].string
.text
) > (IPP_MAX_URISCHEME
- 1))
5474 "\"%s\": Bad uriScheme value \"%s\" - bad "
5475 "length %d (RFC 2911 section 4.1.6).",
5476 attr
->name
, attr
->values
[i
].string
.text
,
5477 (int)strlen(attr
->values
[i
].string
.text
));
5482 case IPP_TAG_CHARSET
:
5483 for (i
= 0; i
< attr
->num_values
; i
++)
5485 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
5486 if (!isprint(*ptr
& 255) || isupper(*ptr
& 255) ||
5487 isspace(*ptr
& 255))
5490 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
5495 "\"%s\": Bad charset value \"%s\" - bad "
5496 "characters (RFC 2911 section 4.1.7).",
5497 attr
->name
, attr
->values
[i
].string
.text
);
5500 if ((ptr
- attr
->values
[i
].string
.text
) > (IPP_MAX_CHARSET
- 1))
5505 "\"%s\": Bad charset value \"%s\" - bad "
5506 "length %d (RFC 2911 section 4.1.7).",
5507 attr
->name
, attr
->values
[i
].string
.text
,
5508 (int)strlen(attr
->values
[i
].string
.text
));
5513 case IPP_TAG_LANGUAGE
:
5515 * The following regular expression is derived from the ABNF for
5516 * language tags in RFC 4646. All I can say is that this is the
5517 * easiest way to check the values...
5520 if ((i
= regcomp(&re
,
5522 "(([a-z]{2,3}(-[a-z][a-z][a-z]){0,3})|[a-z]{4,8})"
5524 "(-[a-z][a-z][a-z][a-z]){0,1}" /* script */
5525 "(-([a-z][a-z]|[0-9][0-9][0-9])){0,1}" /* region */
5526 "(-([a-z]{5,8}|[0-9][0-9][0-9]))*" /* variant */
5527 "(-[a-wy-z](-[a-z0-9]{2,8})+)*" /* extension */
5528 "(-x(-[a-z0-9]{1,8})+)*" /* privateuse */
5530 "x(-[a-z0-9]{1,8})+" /* privateuse */
5532 "[a-z]{1,3}(-[a-z][0-9]{2,8}){1,2}" /* grandfathered */
5534 REG_NOSUB
| REG_EXTENDED
)) != 0)
5536 char temp
[256]; /* Temporary error string */
5538 regerror(i
, &re
, temp
, sizeof(temp
));
5539 print_fatal_error(outfile
, "Unable to compile naturalLanguage regular "
5540 "expression: %s.", temp
);
5544 for (i
= 0; i
< attr
->num_values
; i
++)
5546 if (regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
5551 "\"%s\": Bad naturalLanguage value \"%s\" - bad "
5552 "characters (RFC 2911 section 4.1.8).",
5553 attr
->name
, attr
->values
[i
].string
.text
);
5556 if (strlen(attr
->values
[i
].string
.text
) > (IPP_MAX_LANGUAGE
- 1))
5561 "\"%s\": Bad naturalLanguage value \"%s\" - bad "
5562 "length %d (RFC 2911 section 4.1.8).",
5563 attr
->name
, attr
->values
[i
].string
.text
,
5564 (int)strlen(attr
->values
[i
].string
.text
));
5571 case IPP_TAG_MIMETYPE
:
5573 * The following regular expression is derived from the ABNF for
5574 * language tags in RFC 2045 and 4288. All I can say is that this is
5575 * the easiest way to check the values...
5578 if ((i
= regcomp(&re
,
5580 "[-a-zA-Z0-9!#$&.+^_]{1,127}" /* type-name */
5582 "[-a-zA-Z0-9!#$&.+^_]{1,127}" /* subtype-name */
5583 "(;[-a-zA-Z0-9!#$&.+^_]{1,127}=" /* parameter= */
5584 "([-a-zA-Z0-9!#$&.+^_]{1,127}|\"[^\"]*\"))*"
5587 REG_NOSUB
| REG_EXTENDED
)) != 0)
5589 char temp
[256]; /* Temporary error string */
5591 regerror(i
, &re
, temp
, sizeof(temp
));
5592 print_fatal_error(outfile
, "Unable to compile mimeMediaType regular "
5593 "expression: %s.", temp
);
5597 for (i
= 0; i
< attr
->num_values
; i
++)
5599 if (regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
5604 "\"%s\": Bad mimeMediaType value \"%s\" - bad "
5605 "characters (RFC 2911 section 4.1.9).",
5606 attr
->name
, attr
->values
[i
].string
.text
);
5609 if (strlen(attr
->values
[i
].string
.text
) > (IPP_MAX_MIMETYPE
- 1))
5614 "\"%s\": Bad mimeMediaType value \"%s\" - bad "
5615 "length %d (RFC 2911 section 4.1.9).",
5616 attr
->name
, attr
->values
[i
].string
.text
,
5617 (int)strlen(attr
->values
[i
].string
.text
));
5633 * 'with_value()' - Test a WITH-VALUE predicate.
5636 static int /* O - 1 on match, 0 on non-match */
5637 with_value(FILE *outfile
, /* I - Output file */
5638 cups_array_t
*errors
, /* I - Errors array */
5639 char *value
, /* I - Value string */
5640 int flags
, /* I - Flags for match */
5641 ipp_attribute_t
*attr
, /* I - Attribute to compare */
5642 char *matchbuf
, /* I - Buffer to hold matching value */
5643 size_t matchlen
) /* I - Length of match buffer */
5645 int i
, /* Looping var */
5647 char temp
[1024], /* Temporary value string */
5648 *valptr
; /* Pointer into value */
5652 match
= (flags
& _CUPS_WITH_ALL
) ? 1 : 0;
5655 * NULL matches everything.
5658 if (!value
|| !*value
)
5662 * Compare the value string to the attribute value.
5665 switch (attr
->value_tag
)
5667 case IPP_TAG_INTEGER
:
5669 for (i
= 0; i
< attr
->num_values
; i
++)
5671 char op
, /* Comparison operator */
5672 *nextptr
; /* Next pointer */
5673 int intvalue
, /* Integer value */
5674 valmatch
= 0; /* Does the current value match? */
5678 while (isspace(*valptr
& 255) || isdigit(*valptr
& 255) ||
5679 *valptr
== '-' || *valptr
== ',' || *valptr
== '<' ||
5680 *valptr
== '=' || *valptr
== '>')
5683 while (*valptr
&& !isdigit(*valptr
& 255) && *valptr
!= '-')
5685 if (*valptr
== '<' || *valptr
== '>' || *valptr
== '=')
5693 intvalue
= (int)strtol(valptr
, &nextptr
, 0);
5694 if (nextptr
== valptr
)
5698 if ((op
== '=' && attr
->values
[i
].integer
== intvalue
) ||
5699 (op
== '<' && attr
->values
[i
].integer
< intvalue
) ||
5700 (op
== '>' && attr
->values
[i
].integer
> intvalue
))
5703 snprintf(matchbuf
, matchlen
, "%d", attr
->values
[i
].integer
);
5710 if (flags
& _CUPS_WITH_ALL
)
5725 if (!match
&& errors
)
5727 for (i
= 0; i
< attr
->num_values
; i
++)
5728 add_stringf(errors
, "GOT: %s=%d", attr
->name
,
5729 attr
->values
[i
].integer
);
5733 case IPP_TAG_RANGE
:
5734 for (i
= 0; i
< attr
->num_values
; i
++)
5736 char op
, /* Comparison operator */
5737 *nextptr
; /* Next pointer */
5738 int intvalue
, /* Integer value */
5739 valmatch
= 0; /* Does the current value match? */
5743 while (isspace(*valptr
& 255) || isdigit(*valptr
& 255) ||
5744 *valptr
== '-' || *valptr
== ',' || *valptr
== '<' ||
5745 *valptr
== '=' || *valptr
== '>')
5748 while (*valptr
&& !isdigit(*valptr
& 255) && *valptr
!= '-')
5750 if (*valptr
== '<' || *valptr
== '>' || *valptr
== '=')
5758 intvalue
= (int)strtol(valptr
, &nextptr
, 0);
5759 if (nextptr
== valptr
)
5763 if ((op
== '=' && (attr
->values
[i
].range
.lower
== intvalue
||
5764 attr
->values
[i
].range
.upper
== intvalue
)) ||
5765 (op
== '<' && attr
->values
[i
].range
.upper
< intvalue
) ||
5766 (op
== '>' && attr
->values
[i
].range
.upper
> intvalue
))
5769 snprintf(matchbuf
, matchlen
, "%d-%d",
5770 attr
->values
[0].range
.lower
,
5771 attr
->values
[0].range
.upper
);
5778 if (flags
& _CUPS_WITH_ALL
)
5793 if (!match
&& errors
)
5795 for (i
= 0; i
< attr
->num_values
; i
++)
5796 add_stringf(errors
, "GOT: %s=%d-%d", attr
->name
,
5797 attr
->values
[i
].range
.lower
,
5798 attr
->values
[i
].range
.upper
);
5802 case IPP_TAG_BOOLEAN
:
5803 for (i
= 0; i
< attr
->num_values
; i
++)
5805 if ((!strcmp(value
, "true")) == attr
->values
[i
].boolean
)
5808 strlcpy(matchbuf
, value
, matchlen
);
5810 if (!(flags
& _CUPS_WITH_ALL
))
5816 else if (flags
& _CUPS_WITH_ALL
)
5823 if (!match
&& errors
)
5825 for (i
= 0; i
< attr
->num_values
; i
++)
5826 add_stringf(errors
, "GOT: %s=%s", attr
->name
,
5827 attr
->values
[i
].boolean
? "true" : "false");
5831 case IPP_TAG_RESOLUTION
:
5832 for (i
= 0; i
< attr
->num_values
; i
++)
5834 if (attr
->values
[i
].resolution
.xres
==
5835 attr
->values
[i
].resolution
.yres
)
5836 snprintf(temp
, sizeof(temp
), "%d%s",
5837 attr
->values
[i
].resolution
.xres
,
5838 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
5841 snprintf(temp
, sizeof(temp
), "%dx%d%s",
5842 attr
->values
[i
].resolution
.xres
,
5843 attr
->values
[i
].resolution
.yres
,
5844 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
5847 if (!strcmp(value
, temp
))
5850 strlcpy(matchbuf
, value
, matchlen
);
5852 if (!(flags
& _CUPS_WITH_ALL
))
5858 else if (flags
& _CUPS_WITH_ALL
)
5865 if (!match
&& errors
)
5867 for (i
= 0; i
< attr
->num_values
; i
++)
5869 if (attr
->values
[i
].resolution
.xres
==
5870 attr
->values
[i
].resolution
.yres
)
5871 snprintf(temp
, sizeof(temp
), "%d%s",
5872 attr
->values
[i
].resolution
.xres
,
5873 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
5876 snprintf(temp
, sizeof(temp
), "%dx%d%s",
5877 attr
->values
[i
].resolution
.xres
,
5878 attr
->values
[i
].resolution
.yres
,
5879 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
5882 if (strcmp(value
, temp
))
5883 add_stringf(errors
, "GOT: %s=%s", attr
->name
, temp
);
5888 case IPP_TAG_NOVALUE
:
5889 case IPP_TAG_UNKNOWN
:
5892 case IPP_TAG_CHARSET
:
5893 case IPP_TAG_KEYWORD
:
5894 case IPP_TAG_LANGUAGE
:
5895 case IPP_TAG_MIMETYPE
:
5897 case IPP_TAG_NAMELANG
:
5899 case IPP_TAG_TEXTLANG
:
5901 case IPP_TAG_URISCHEME
:
5902 if (flags
& _CUPS_WITH_REGEX
)
5905 * Value is an extended, case-sensitive POSIX regular expression...
5908 regex_t re
; /* Regular expression */
5910 if ((i
= regcomp(&re
, value
, REG_EXTENDED
| REG_NOSUB
)) != 0)
5912 regerror(i
, &re
, temp
, sizeof(temp
));
5914 print_fatal_error(outfile
, "Unable to compile WITH-VALUE regular expression "
5915 "\"%s\" - %s", value
, temp
);
5920 * See if ALL of the values match the given regular expression.
5923 for (i
= 0; i
< attr
->num_values
; i
++)
5925 if (!regexec(&re
, get_string(attr
, i
, flags
, temp
, sizeof(temp
)),
5930 get_string(attr
, i
, flags
, temp
, sizeof(temp
)),
5933 if (!(flags
& _CUPS_WITH_ALL
))
5939 else if (flags
& _CUPS_WITH_ALL
)
5948 else if (ippGetValueTag(attr
) == IPP_TAG_URI
)
5950 if (!strncmp(value
, "ipp://", 6) || !strncmp(value
, "http://", 7) || !strncmp(value
, "ipps://", 7) || !strncmp(value
, "https://", 8))
5952 char scheme
[256], /* URI scheme */
5953 userpass
[256], /* username:password, if any */
5954 hostname
[256], /* hostname */
5955 *hostptr
, /* Pointer into hostname */
5956 resource
[1024]; /* Resource path */
5957 int port
; /* Port number */
5959 if (httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), hostname
, sizeof(hostname
), &port
, resource
, sizeof(resource
)) >= HTTP_URI_STATUS_OK
&& (hostptr
= hostname
+ strlen(hostname
) - 1) > hostname
&& *hostptr
== '.')
5962 * Strip trailing "." in hostname of URI...
5966 httpAssembleURI(HTTP_URI_CODING_ALL
, temp
, sizeof(temp
), scheme
, userpass
, hostname
, port
, resource
);
5972 * Value is a literal URI string, see if the value(s) match...
5975 for (i
= 0; i
< attr
->num_values
; i
++)
5977 if (!strcmp(value
, get_string(attr
, i
, flags
, temp
, sizeof(temp
))))
5981 get_string(attr
, i
, flags
, temp
, sizeof(temp
)),
5984 if (!(flags
& _CUPS_WITH_ALL
))
5990 else if (flags
& _CUPS_WITH_ALL
)
6000 * Value is a literal string, see if the value(s) match...
6003 for (i
= 0; i
< attr
->num_values
; i
++)
6005 if (!strcmp(value
, get_string(attr
, i
, flags
, temp
, sizeof(temp
))))
6009 get_string(attr
, i
, flags
, temp
, sizeof(temp
)),
6012 if (!(flags
& _CUPS_WITH_ALL
))
6018 else if (flags
& _CUPS_WITH_ALL
)
6026 if (!match
&& errors
)
6028 for (i
= 0; i
< attr
->num_values
; i
++)
6029 add_stringf(errors
, "GOT: %s=\"%s\"", attr
->name
,
6030 attr
->values
[i
].string
.text
);
6043 * 'with_value_from()' - Test a WITH-VALUE-FROM predicate.
6046 static int /* O - 1 on match, 0 on non-match */
6048 cups_array_t
*errors
, /* I - Errors array */
6049 ipp_attribute_t
*fromattr
, /* I - "From" attribute */
6050 ipp_attribute_t
*attr
, /* I - Attribute to compare */
6051 char *matchbuf
, /* I - Buffer to hold matching value */
6052 size_t matchlen
) /* I - Length of match buffer */
6054 int i
, j
, /* Looping vars */
6055 count
= ippGetCount(attr
), /* Number of attribute values */
6056 match
= 1; /* Match? */
6062 * Compare the from value(s) to the attribute value(s)...
6065 switch (ippGetValueTag(attr
))
6067 case IPP_TAG_INTEGER
:
6068 if (ippGetValueTag(fromattr
) != IPP_TAG_INTEGER
&& ippGetValueTag(fromattr
) != IPP_TAG_RANGE
)
6069 goto wrong_value_tag
;
6071 for (i
= 0; i
< count
; i
++)
6073 int value
= ippGetInteger(attr
, i
);
6074 /* Current integer value */
6076 if (ippContainsInteger(fromattr
, value
))
6079 snprintf(matchbuf
, matchlen
, "%d", value
);
6083 add_stringf(errors
, "GOT: %s=%d", ippGetName(attr
), value
);
6090 if (ippGetValueTag(fromattr
) != IPP_TAG_ENUM
)
6091 goto wrong_value_tag
;
6093 for (i
= 0; i
< count
; i
++)
6095 int value
= ippGetInteger(attr
, i
);
6096 /* Current integer value */
6098 if (ippContainsInteger(fromattr
, value
))
6101 snprintf(matchbuf
, matchlen
, "%d", value
);
6105 add_stringf(errors
, "GOT: %s=%d", ippGetName(attr
), value
);
6111 case IPP_TAG_RESOLUTION
:
6112 if (ippGetValueTag(fromattr
) != IPP_TAG_RESOLUTION
)
6113 goto wrong_value_tag
;
6115 for (i
= 0; i
< count
; i
++)
6119 int fromcount
= ippGetCount(fromattr
);
6120 int fromxres
, fromyres
;
6121 ipp_res_t fromunits
;
6123 xres
= ippGetResolution(attr
, i
, &yres
, &units
);
6125 for (j
= 0; j
< fromcount
; j
++)
6127 fromxres
= ippGetResolution(fromattr
, j
, &fromyres
, &fromunits
);
6128 if (fromxres
== xres
&& fromyres
== yres
&& fromunits
== units
)
6137 snprintf(matchbuf
, matchlen
, "%d%s", xres
, units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
6139 snprintf(matchbuf
, matchlen
, "%dx%d%s", xres
, yres
, units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
6145 add_stringf(errors
, "GOT: %s=%d%s", ippGetName(attr
), xres
, units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
6147 add_stringf(errors
, "GOT: %s=%dx%d%s", ippGetName(attr
), xres
, yres
, units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
6154 case IPP_TAG_NOVALUE
:
6155 case IPP_TAG_UNKNOWN
:
6158 case IPP_TAG_CHARSET
:
6159 case IPP_TAG_KEYWORD
:
6160 case IPP_TAG_LANGUAGE
:
6161 case IPP_TAG_MIMETYPE
:
6163 case IPP_TAG_NAMELANG
:
6165 case IPP_TAG_TEXTLANG
:
6167 case IPP_TAG_URISCHEME
:
6168 for (i
= 0; i
< count
; i
++)
6170 const char *value
= ippGetString(attr
, i
, NULL
);
6171 /* Current string value */
6173 if (ippContainsString(fromattr
, value
))
6176 strlcpy(matchbuf
, value
, matchlen
);
6180 add_stringf(errors
, "GOT: %s='%s'", ippGetName(attr
), value
);
6193 /* value tag mismatch between fromattr and attr */
6196 add_stringf(errors
, "GOT: %s OF-TYPE %s", ippGetName(attr
), ippTagString(ippGetValueTag(attr
)));