4 * ipptool command for CUPS.
6 * Copyright 2007-2015 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * Include necessary headers...
22 #include <cups/cups-private.h>
23 #include <cups/file-private.h>
37 #endif /* !O_BINARY */
44 typedef enum _cups_transfer_e
/**** How to send request data ****/
46 _CUPS_TRANSFER_AUTO
, /* Chunk for files, length for static */
47 _CUPS_TRANSFER_CHUNKED
, /* Chunk always */
48 _CUPS_TRANSFER_LENGTH
/* Length always */
51 typedef enum _cups_output_e
/**** Output mode ****/
53 _CUPS_OUTPUT_QUIET
, /* No output */
54 _CUPS_OUTPUT_TEST
, /* Traditional CUPS test output */
55 _CUPS_OUTPUT_PLIST
, /* XML plist test output */
56 _CUPS_OUTPUT_LIST
, /* Tabular list output */
57 _CUPS_OUTPUT_CSV
/* Comma-separated values output */
60 typedef enum _cups_with_e
/**** WITH flags ****/
62 _CUPS_WITH_LITERAL
= 0, /* Match string is a literal value */
63 _CUPS_WITH_ALL
= 1, /* Must match all values */
64 _CUPS_WITH_REGEX
= 2, /* Match string is a regular expression */
65 _CUPS_WITH_HOSTNAME
= 4, /* Match string is a URI hostname */
66 _CUPS_WITH_RESOURCE
= 8, /* Match string is a URI resource */
67 _CUPS_WITH_SCHEME
= 16 /* Match string is a URI scheme */
70 typedef struct _cups_expect_s
/**** Expected attribute info ****/
72 int optional
, /* Optional attribute? */
73 not_expect
, /* Don't expect attribute? */
74 expect_all
; /* Expect all attributes to match/not match */
75 char *name
, /* Attribute name */
76 *of_type
, /* Type name */
77 *same_count_as
, /* Parallel attribute name */
78 *if_defined
, /* Only required if variable defined */
79 *if_not_defined
, /* Only required if variable is not defined */
80 *with_value
, /* Attribute must include this value */
81 *with_value_from
, /* Attribute must have one of the values in this attribute */
82 *define_match
, /* Variable to define on match */
83 *define_no_match
, /* Variable to define on no-match */
84 *define_value
; /* Variable to define with value */
85 int repeat_limit
, /* Maximum number of times to repeat */
86 repeat_match
, /* Repeat test on match */
87 repeat_no_match
, /* Repeat test on no match */
88 with_flags
, /* WITH flags */
89 count
; /* Expected count if > 0 */
90 ipp_tag_t in_group
; /* IN-GROUP value */
93 typedef struct _cups_status_s
/**** Status info ****/
95 ipp_status_t status
; /* Expected status code */
96 char *if_defined
, /* Only if variable is defined */
97 *if_not_defined
, /* Only if variable is not defined */
98 *define_match
, /* Variable to define on match */
99 *define_no_match
, /* Variable to define on no-match */
100 *define_value
; /* Variable to define with value */
101 int repeat_limit
, /* Maximum number of times to repeat */
102 repeat_match
, /* Repeat the test when it does not match */
103 repeat_no_match
; /* Repeat the test when it matches */
106 typedef struct _cups_var_s
/**** Variable ****/
108 char *name
, /* Name of variable */
109 *value
; /* Value of variable */
112 typedef struct _cups_vars_s
/**** Set of variables ****/
114 char *uri
, /* URI for printer */
115 *filename
, /* Filename */
116 scheme
[64], /* Scheme from URI */
117 userpass
[256], /* Username/password from URI */
118 hostname
[256], /* Hostname from URI */
119 resource
[1024]; /* Resource path from URI */
120 int port
; /* Port number from URI */
121 http_encryption_t encryption
; /* Encryption for connection? */
122 double timeout
; /* Timeout for connection */
123 int family
; /* Address family */
124 cups_array_t
*vars
; /* Array of variables */
132 static _cups_transfer_t Transfer
= _CUPS_TRANSFER_AUTO
;
133 /* How to transfer requests */
134 static _cups_output_t Output
= _CUPS_OUTPUT_LIST
;
136 static int Cancel
= 0, /* Cancel test? */
137 IgnoreErrors
= 0, /* Ignore errors? */
138 StopAfterIncludeError
= 0,
139 /* Stop after include errors? */
140 Verbosity
= 0, /* Show all attributes? */
141 Version
= 11, /* Default IPP version */
142 XMLHeader
= 0, /* 1 if header is written */
143 TestCount
= 0, /* Number of tests run */
144 PassCount
= 0, /* Number of passing tests */
145 FailCount
= 0, /* Number of failing tests */
146 SkipCount
= 0; /* Number of skipped tests */
147 static char *Username
= NULL
, /* Username from URI */
148 *Password
= NULL
; /* Password from URI */
149 static int PasswordTries
= 0; /* Number of tries with password */
156 static void add_stringf(cups_array_t
*a
, const char *s
, ...) __attribute__ ((__format__ (__printf__
, 2, 3)));
157 static int compare_vars(_cups_var_t
*a
, _cups_var_t
*b
);
158 static int do_tests(FILE *outfile
, _cups_vars_t
*vars
, const char *testfile
);
159 static void expand_variables(_cups_vars_t
*vars
, char *dst
, const char *src
, size_t dstsize
) __attribute__((nonnull(1,2,3)));
160 static int expect_matches(_cups_expect_t
*expect
, ipp_tag_t value_tag
);
161 static ipp_t
*get_collection(FILE *outfile
, _cups_vars_t
*vars
, FILE *fp
, int *linenum
);
162 static char *get_filename(const char *testfile
, char *dst
, const char *src
, size_t dstsize
);
163 static const char *get_string(ipp_attribute_t
*attr
, int element
, int flags
, char *buffer
, size_t bufsize
);
164 static char *get_token(FILE *fp
, char *buf
, int buflen
, int *linenum
);
165 static char *get_variable(_cups_vars_t
*vars
, const char *name
);
166 static char *iso_date(ipp_uchar_t
*date
);
167 static const char *password_cb(const char *prompt
);
168 static void pause_message(const char *message
);
169 static void print_attr(FILE *outfile
, int format
, ipp_attribute_t
*attr
, ipp_tag_t
*group
);
170 static void print_csv(FILE *outfile
, ipp_attribute_t
*attr
, int num_displayed
, char **displayed
, size_t *widths
);
171 static void print_fatal_error(FILE *outfile
, const char *s
, ...) __attribute__ ((__format__ (__printf__
, 2, 3)));
172 static void print_line(FILE *outfile
, ipp_attribute_t
*attr
, int num_displayed
, char **displayed
, size_t *widths
);
173 static void print_xml_header(FILE *outfile
);
174 static void print_xml_string(FILE *outfile
, const char *element
, const char *s
);
175 static void print_xml_trailer(FILE *outfile
, int success
, const char *message
);
176 static void set_variable(FILE *outfile
, _cups_vars_t
*vars
, const char *name
, const char *value
);
178 static void sigterm_handler(int sig
);
180 static int timeout_cb(http_t
*http
, void *user_data
);
181 static void usage(void) __attribute__((noreturn
));
182 static int validate_attr(FILE *outfile
, cups_array_t
*errors
, ipp_attribute_t
*attr
);
183 static int with_value(FILE *outfile
, cups_array_t
*errors
, char *value
, int flags
, ipp_attribute_t
*attr
, char *matchbuf
, size_t matchlen
);
184 static int with_value_from(cups_array_t
*errors
, ipp_attribute_t
*fromattr
, ipp_attribute_t
*attr
, char *matchbuf
, size_t matchlen
);
188 * 'main()' - Parse options and do tests.
191 int /* O - Exit status */
192 main(int argc
, /* I - Number of command-line args */
193 char *argv
[]) /* I - Command-line arguments */
195 int i
; /* Looping var */
196 int status
; /* Status of tests... */
197 FILE *outfile
= stdout
;
199 char *opt
, /* Current option */
200 name
[1024], /* Name/value buffer */
201 *value
, /* Pointer to value */
202 filename
[1024], /* Real filename */
203 testname
[1024], /* Real test filename */
204 uri
[1024]; /* Copy of printer URI */
205 const char *ext
, /* Extension on filename */
206 *testfile
; /* Test file to use */
207 int interval
, /* Test interval in microseconds */
208 repeat
; /* Repeat count */
209 _cups_vars_t vars
; /* Variables */
210 http_uri_status_t uri_status
; /* URI separation status */
211 _cups_globals_t
*cg
= _cupsGlobals();
217 * Catch SIGINT and SIGTERM...
220 signal(SIGINT
, sigterm_handler
);
221 signal(SIGTERM
, sigterm_handler
);
225 * Initialize the locale and variables...
228 _cupsSetLocale(argv
);
230 memset(&vars
, 0, sizeof(vars
));
231 vars
.family
= AF_UNSPEC
;
232 vars
.vars
= cupsArrayNew((cups_array_func_t
)compare_vars
, NULL
);
237 * ipptool URI testfile
245 for (i
= 1; i
< argc
; i
++)
247 if (!strcmp(argv
[i
], "--help"))
251 else if (!strcmp(argv
[i
], "--stop-after-include-error"))
253 StopAfterIncludeError
= 1;
255 else if (!strcmp(argv
[i
], "--version"))
260 else if (argv
[i
][0] == '-')
262 for (opt
= argv
[i
] + 1; *opt
; opt
++)
266 case '4' : /* Connect using IPv4 only */
267 vars
.family
= AF_INET
;
271 case '6' : /* Connect using IPv6 only */
272 vars
.family
= AF_INET6
;
274 #endif /* AF_INET6 */
276 case 'C' : /* Enable HTTP chunking */
277 Transfer
= _CUPS_TRANSFER_CHUNKED
;
280 case 'E' : /* Encrypt with TLS */
282 vars
.encryption
= HTTP_ENCRYPT_REQUIRED
;
284 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."),
286 #endif /* HAVE_SSL */
289 case 'I' : /* Ignore errors */
293 case 'L' : /* Disable HTTP chunking */
294 Transfer
= _CUPS_TRANSFER_LENGTH
;
297 case 'P' : /* Output to plist file */
302 _cupsLangPrintf(stderr
, _("%s: Missing filename for \"-P\"."), "ipptool");
306 if (outfile
!= stdout
)
309 if ((outfile
= fopen(argv
[i
], "w")) == NULL
)
311 _cupsLangPrintf(stderr
, _("%s: Unable to open \"%s\": %s"), "ipptool", argv
[i
], strerror(errno
));
315 Output
= _CUPS_OUTPUT_PLIST
;
317 if (interval
|| repeat
)
319 _cupsLangPuts(stderr
, _("ipptool: \"-i\" and \"-n\" are incompatible with \"-P\" and \"-X\"."));
324 case 'S' : /* Encrypt with SSL */
326 vars
.encryption
= HTTP_ENCRYPT_ALWAYS
;
328 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."),
330 #endif /* HAVE_SSL */
333 case 'T' : /* Set timeout */
338 _cupsLangPrintf(stderr
,
339 _("%s: Missing timeout for \"-T\"."),
344 vars
.timeout
= _cupsStrScand(argv
[i
], NULL
, localeconv());
347 case 'V' : /* Set IPP version */
352 _cupsLangPrintf(stderr
,
353 _("%s: Missing version for \"-V\"."),
358 if (!strcmp(argv
[i
], "1.0"))
360 else if (!strcmp(argv
[i
], "1.1"))
362 else if (!strcmp(argv
[i
], "2.0"))
364 else if (!strcmp(argv
[i
], "2.1"))
366 else if (!strcmp(argv
[i
], "2.2"))
370 _cupsLangPrintf(stderr
,
371 _("%s: Bad version %s for \"-V\"."),
377 case 'X' : /* Produce XML output */
378 Output
= _CUPS_OUTPUT_PLIST
;
380 if (interval
|| repeat
)
382 _cupsLangPuts(stderr
, _("ipptool: \"-i\" and \"-n\" are incompatible with \"-P\" and \"-X\"."));
387 case 'c' : /* CSV output */
388 Output
= _CUPS_OUTPUT_CSV
;
391 case 'd' : /* Define a variable */
396 _cupsLangPuts(stderr
,
397 _("ipptool: Missing name=value for \"-d\"."));
401 strlcpy(name
, argv
[i
], sizeof(name
));
402 if ((value
= strchr(name
, '=')) != NULL
)
405 value
= name
+ strlen(name
);
407 set_variable(outfile
, &vars
, name
, value
);
410 case 'f' : /* Set the default test filename */
415 _cupsLangPuts(stderr
,
416 _("ipptool: Missing filename for \"-f\"."));
423 vars
.filename
= NULL
;
426 if (access(argv
[i
], 0))
432 snprintf(filename
, sizeof(filename
), "%s.gz", argv
[i
]);
433 if (access(filename
, 0) && filename
[0] != '/'
435 && (!isalpha(filename
[0] & 255) || filename
[1] != ':')
439 snprintf(filename
, sizeof(filename
), "%s/ipptool/%s",
440 cg
->cups_datadir
, argv
[i
]);
441 if (access(filename
, 0))
443 snprintf(filename
, sizeof(filename
), "%s/ipptool/%s.gz",
444 cg
->cups_datadir
, argv
[i
]);
445 if (access(filename
, 0))
446 vars
.filename
= strdup(argv
[i
]);
448 vars
.filename
= strdup(filename
);
451 vars
.filename
= strdup(filename
);
454 vars
.filename
= strdup(filename
);
457 vars
.filename
= strdup(argv
[i
]);
459 if ((ext
= strrchr(vars
.filename
, '.')) != NULL
)
462 * Guess the MIME media type based on the extension...
465 if (!_cups_strcasecmp(ext
, ".gif"))
466 set_variable(outfile
, &vars
, "filetype", "image/gif");
467 else if (!_cups_strcasecmp(ext
, ".htm") ||
468 !_cups_strcasecmp(ext
, ".htm.gz") ||
469 !_cups_strcasecmp(ext
, ".html") ||
470 !_cups_strcasecmp(ext
, ".html.gz"))
471 set_variable(outfile
, &vars
, "filetype", "text/html");
472 else if (!_cups_strcasecmp(ext
, ".jpg") ||
473 !_cups_strcasecmp(ext
, ".jpeg"))
474 set_variable(outfile
, &vars
, "filetype", "image/jpeg");
475 else if (!_cups_strcasecmp(ext
, ".pcl") ||
476 !_cups_strcasecmp(ext
, ".pcl.gz"))
477 set_variable(outfile
, &vars
, "filetype", "application/vnd.hp-PCL");
478 else if (!_cups_strcasecmp(ext
, ".pdf"))
479 set_variable(outfile
, &vars
, "filetype", "application/pdf");
480 else if (!_cups_strcasecmp(ext
, ".png"))
481 set_variable(outfile
, &vars
, "filetype", "image/png");
482 else if (!_cups_strcasecmp(ext
, ".ps") ||
483 !_cups_strcasecmp(ext
, ".ps.gz"))
484 set_variable(outfile
, &vars
, "filetype", "application/postscript");
485 else if (!_cups_strcasecmp(ext
, ".pwg") ||
486 !_cups_strcasecmp(ext
, ".pwg.gz") ||
487 !_cups_strcasecmp(ext
, ".ras") ||
488 !_cups_strcasecmp(ext
, ".ras.gz"))
489 set_variable(outfile
, &vars
, "filetype", "image/pwg-raster");
490 else if (!_cups_strcasecmp(ext
, ".tif") ||
491 !_cups_strcasecmp(ext
, ".tiff"))
492 set_variable(outfile
, &vars
, "filetype", "image/tiff");
493 else if (!_cups_strcasecmp(ext
, ".txt") ||
494 !_cups_strcasecmp(ext
, ".txt.gz"))
495 set_variable(outfile
, &vars
, "filetype", "text/plain");
496 else if (!_cups_strcasecmp(ext
, ".urf") ||
497 !_cups_strcasecmp(ext
, ".urf.gz"))
498 set_variable(outfile
, &vars
, "filetype", "image/urf");
499 else if (!_cups_strcasecmp(ext
, ".xps"))
500 set_variable(outfile
, &vars
, "filetype", "application/openxps");
502 set_variable(outfile
, &vars
, "filetype", "application/octet-stream");
507 * Use the "auto-type" MIME media type...
510 set_variable(outfile
, &vars
, "filetype", "application/octet-stream");
514 case 'i' : /* Test every N seconds */
519 _cupsLangPuts(stderr
,
520 _("ipptool: Missing seconds for \"-i\"."));
525 interval
= (int)(_cupsStrScand(argv
[i
], NULL
, localeconv()) *
529 _cupsLangPuts(stderr
,
530 _("ipptool: Invalid seconds for \"-i\"."));
535 if (Output
== _CUPS_OUTPUT_PLIST
&& interval
)
537 _cupsLangPuts(stderr
, _("ipptool: \"-i\" and \"-n\" are incompatible with \"-P\" and \"-X\"."));
542 case 'l' : /* List as a table */
543 Output
= _CUPS_OUTPUT_LIST
;
546 case 'n' : /* Repeat count */
551 _cupsLangPuts(stderr
,
552 _("ipptool: Missing count for \"-n\"."));
556 repeat
= atoi(argv
[i
]);
558 if (Output
== _CUPS_OUTPUT_PLIST
&& repeat
)
560 _cupsLangPuts(stderr
, _("ipptool: \"-i\" and \"-n\" are incompatible with \"-P\" and \"-X\"."));
565 case 'q' : /* Be quiet */
566 Output
= _CUPS_OUTPUT_QUIET
;
569 case 't' : /* CUPS test output */
570 Output
= _CUPS_OUTPUT_TEST
;
573 case 'v' : /* Be verbose */
578 _cupsLangPrintf(stderr
, _("ipptool: Unknown option \"-%c\"."),
584 else if (!strncmp(argv
[i
], "ipp://", 6) || !strncmp(argv
[i
], "http://", 7)
586 || !strncmp(argv
[i
], "ipps://", 7)
587 || !strncmp(argv
[i
], "https://", 8)
588 #endif /* HAVE_SSL */
597 _cupsLangPuts(stderr
, _("ipptool: May only specify a single URI."));
602 if (!strncmp(argv
[i
], "ipps://", 7) || !strncmp(argv
[i
], "https://", 8))
603 vars
.encryption
= HTTP_ENCRYPT_ALWAYS
;
604 #endif /* HAVE_SSL */
607 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
, vars
.uri
,
608 vars
.scheme
, sizeof(vars
.scheme
),
609 vars
.userpass
, sizeof(vars
.userpass
),
610 vars
.hostname
, sizeof(vars
.hostname
),
612 vars
.resource
, sizeof(vars
.resource
));
614 if (uri_status
!= HTTP_URI_OK
)
616 _cupsLangPrintf(stderr
, _("ipptool: Bad URI - %s."), httpURIStatusString(uri_status
));
620 if (vars
.userpass
[0])
622 if ((Password
= strchr(vars
.userpass
, ':')) != NULL
)
625 Username
= vars
.userpass
;
626 cupsSetPasswordCB(password_cb
);
627 set_variable(outfile
, &vars
, "uriuser", vars
.userpass
);
630 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), vars
.scheme
, NULL
,
631 vars
.hostname
, vars
.port
, vars
.resource
);
642 _cupsLangPuts(stderr
, _("ipptool: URI required before test file."));
643 _cupsLangPuts(stderr
, argv
[i
]);
647 if (access(argv
[i
], 0) && argv
[i
][0] != '/'
649 && (!isalpha(argv
[i
][0] & 255) || argv
[i
][1] != ':')
653 snprintf(testname
, sizeof(testname
), "%s/ipptool/%s", cg
->cups_datadir
,
655 if (access(testname
, 0))
663 if (!do_tests(outfile
, &vars
, testfile
))
668 if (!vars
.uri
|| !testfile
)
672 * Loop if the interval is set...
675 if (Output
== _CUPS_OUTPUT_PLIST
)
676 print_xml_trailer(outfile
, !status
, NULL
);
677 else if (interval
> 0 && repeat
> 0)
681 usleep((useconds_t
)interval
);
682 do_tests(outfile
, &vars
, testfile
);
686 else if (interval
> 0)
690 usleep((useconds_t
)interval
);
691 do_tests(outfile
, &vars
, testfile
);
695 if ((Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
)) && TestCount
> 1)
698 * Show a summary report if there were multiple tests...
701 printf("\nSummary: %d tests, %d passed, %d failed, %d skipped\n"
702 "Score: %d%%\n", TestCount
, PassCount
, FailCount
, SkipCount
,
703 100 * (PassCount
+ SkipCount
) / TestCount
);
715 * 'add_stringf()' - Add a formatted string to an array.
719 add_stringf(cups_array_t
*a
, /* I - Array */
720 const char *s
, /* I - Printf-style format string */
721 ...) /* I - Additional args as needed */
723 char buffer
[10240]; /* Format buffer */
724 va_list ap
; /* Argument pointer */
728 * Don't bother is the array is NULL...
735 * Format the message...
739 vsnprintf(buffer
, sizeof(buffer
), s
, ap
);
743 * Add it to the array...
746 cupsArrayAdd(a
, buffer
);
751 * 'compare_vars()' - Compare two variables.
754 static int /* O - Result of comparison */
755 compare_vars(_cups_var_t
*a
, /* I - First variable */
756 _cups_var_t
*b
) /* I - Second variable */
758 return (_cups_strcasecmp(a
->name
, b
->name
));
763 * 'do_tests()' - Do tests as specified in the test file.
766 static int /* 1 = success, 0 = failure */
767 do_tests(FILE *outfile
, /* I - Output file */
768 _cups_vars_t
*vars
, /* I - Variables */
769 const char *testfile
) /* I - Test file to use */
771 int i
, /* Looping var */
772 linenum
, /* Current line number */
773 pass
, /* Did we pass the test? */
774 prev_pass
= 1, /* Did we pass the previous test? */
775 request_id
, /* Current request ID */
776 show_header
= 1, /* Show the test header? */
777 ignore_errors
, /* Ignore test failures? */
778 skip_previous
= 0, /* Skip on previous test failure? */
779 repeat_count
, /* Repeat count */
780 repeat_interval
, /* Repeat interval */
781 repeat_prev
, /* Previous repeat interval */
782 repeat_test
; /* Repeat a test? */
783 http_t
*http
= NULL
; /* HTTP connection to server */
784 FILE *fp
= NULL
; /* Test file */
785 char resource
[512], /* Resource for request */
786 token
[1024], /* Token from file */
787 *tokenptr
, /* Pointer into token */
788 temp
[1024], /* Temporary string */
789 buffer
[8192], /* Copy buffer */
790 compression
[16]; /* COMPRESSION value */
791 ipp_t
*request
= NULL
, /* IPP request */
792 *response
= NULL
; /* IPP response */
793 size_t length
; /* Length of IPP request */
794 http_status_t status
; /* HTTP status */
795 cups_file_t
*reqfile
; /* File to send */
796 ssize_t bytes
; /* Bytes read/written */
797 char attr
[128]; /* Attribute name */
798 ipp_op_t op
; /* Operation */
799 ipp_tag_t group
; /* Current group */
800 ipp_tag_t value
; /* Current value type */
801 ipp_attribute_t
*attrptr
, /* Attribute pointer */
802 *found
, /* Found attribute */
803 *lastcol
= NULL
; /* Last collection attribute */
804 char name
[1024], /* Name of test */
805 file_id
[1024], /* File identifier */
806 test_id
[1024]; /* Test identifier */
807 char filename
[1024]; /* Filename */
808 _cups_transfer_t transfer
; /* To chunk or not to chunk */
809 int version
, /* IPP version number to use */
810 skip_test
; /* Skip this test? */
811 int num_statuses
= 0; /* Number of valid status codes */
812 _cups_status_t statuses
[100], /* Valid status codes */
813 *last_status
; /* Last STATUS (for predicates) */
814 int num_expects
= 0; /* Number of expected attributes */
815 _cups_expect_t expects
[200], /* Expected attributes */
816 *expect
, /* Current expected attribute */
817 *last_expect
; /* Last EXPECT (for predicates) */
818 int num_displayed
= 0; /* Number of displayed attributes */
819 char *displayed
[200]; /* Displayed attributes */
820 size_t widths
[200]; /* Width of columns */
821 cups_array_t
*a
, /* Duplicate attribute array */
822 *errors
= NULL
; /* Errors array */
823 const char *error
; /* Current error */
827 * Open the test file...
830 if ((fp
= fopen(testfile
, "r")) == NULL
)
832 print_fatal_error(outfile
, "Unable to open test file %s - %s", testfile
,
839 * Connect to the server...
842 if ((http
= httpConnect2(vars
->hostname
, vars
->port
, NULL
, vars
->family
,
843 vars
->encryption
, 1, 30000, NULL
)) == NULL
)
845 print_fatal_error(outfile
, "Unable to connect to %s on port %d - %s", vars
->hostname
,
846 vars
->port
, cupsLastErrorString());
852 httpSetDefaultField(http
, HTTP_FIELD_ACCEPT_ENCODING
,
853 "deflate, gzip, identity");
855 httpSetDefaultField(http
, HTTP_FIELD_ACCEPT_ENCODING
, "identity");
856 #endif /* HAVE_LIBZ */
858 if (vars
->timeout
> 0.0)
859 httpSetTimeout(http
, vars
->timeout
, timeout_cb
, NULL
);
865 CUPS_SRAND(time(NULL
));
867 errors
= cupsArrayNew3(NULL
, NULL
, NULL
, 0, (cups_acopy_func_t
)strdup
,
868 (cups_afree_func_t
)free
);
872 request_id
= (CUPS_RAND() % 1000) * 137 + 1;
874 while (!Cancel
&& get_token(fp
, token
, sizeof(token
), &linenum
) != NULL
)
877 * Expect an open brace...
880 if (!strcmp(token
, "DEFINE"))
886 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
887 get_token(fp
, temp
, sizeof(temp
), &linenum
))
889 expand_variables(vars
, token
, temp
, sizeof(token
));
890 set_variable(outfile
, vars
, attr
, token
);
894 print_fatal_error(outfile
, "Missing DEFINE name and/or value on line %d.",
902 else if (!strcmp(token
, "DEFINE-DEFAULT"))
905 * DEFINE-DEFAULT name value
908 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
909 get_token(fp
, temp
, sizeof(temp
), &linenum
))
911 expand_variables(vars
, token
, temp
, sizeof(token
));
912 if (!get_variable(vars
, attr
))
913 set_variable(outfile
, vars
, attr
, token
);
917 print_fatal_error(outfile
, "Missing DEFINE-DEFAULT name and/or value on line "
925 else if (!strcmp(token
, "FILE-ID"))
931 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
933 expand_variables(vars
, file_id
, temp
, sizeof(file_id
));
937 print_fatal_error(outfile
, "Missing FILE-ID value on line %d.", linenum
);
944 else if (!strcmp(token
, "IGNORE-ERRORS"))
951 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
952 (!_cups_strcasecmp(temp
, "yes") || !_cups_strcasecmp(temp
, "no")))
954 IgnoreErrors
= !_cups_strcasecmp(temp
, "yes");
958 print_fatal_error(outfile
, "Missing IGNORE-ERRORS value on line %d.", linenum
);
965 else if (!strcmp(token
, "INCLUDE"))
972 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
975 * Map the filename to and then run the tests...
978 if (!do_tests(outfile
, vars
, get_filename(testfile
, filename
, temp
, sizeof(filename
))))
982 if (StopAfterIncludeError
)
988 print_fatal_error(outfile
, "Missing INCLUDE filename on line %d.", linenum
);
996 else if (!strcmp(token
, "INCLUDE-IF-DEFINED"))
999 * INCLUDE-IF-DEFINED name "filename"
1000 * INCLUDE-IF-DEFINED name <filename>
1003 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
1004 get_token(fp
, temp
, sizeof(temp
), &linenum
))
1007 * Map the filename to and then run the tests...
1010 if (get_variable(vars
, attr
) &&
1011 !do_tests(outfile
, vars
, get_filename(testfile
, filename
, temp
, sizeof(filename
))))
1015 if (StopAfterIncludeError
)
1021 print_fatal_error(outfile
, "Missing INCLUDE-IF-DEFINED name or filename on line "
1030 else if (!strcmp(token
, "INCLUDE-IF-NOT-DEFINED"))
1033 * INCLUDE-IF-NOT-DEFINED name "filename"
1034 * INCLUDE-IF-NOT-DEFINED name <filename>
1037 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
1038 get_token(fp
, temp
, sizeof(temp
), &linenum
))
1041 * Map the filename to and then run the tests...
1044 if (!get_variable(vars
, attr
) &&
1045 !do_tests(outfile
, vars
, get_filename(testfile
, filename
, temp
, sizeof(filename
))))
1049 if (StopAfterIncludeError
)
1055 print_fatal_error(outfile
, "Missing INCLUDE-IF-NOT-DEFINED name or filename on "
1056 "line %d.", linenum
);
1064 else if (!strcmp(token
, "SKIP-IF-DEFINED"))
1067 * SKIP-IF-DEFINED variable
1070 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1072 if (get_variable(vars
, temp
))
1077 print_fatal_error(outfile
, "Missing SKIP-IF-DEFINED variable on line %d.",
1083 else if (!strcmp(token
, "SKIP-IF-NOT-DEFINED"))
1086 * SKIP-IF-NOT-DEFINED variable
1089 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1091 if (!get_variable(vars
, temp
))
1096 print_fatal_error(outfile
, "Missing SKIP-IF-NOT-DEFINED variable on line %d.",
1102 else if (!strcmp(token
, "STOP-AFTER-INCLUDE-ERROR"))
1105 * STOP-AFTER-INCLUDE-ERROR yes
1106 * STOP-AFTER-INCLUDE-ERROR no
1109 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
1110 (!_cups_strcasecmp(temp
, "yes") || !_cups_strcasecmp(temp
, "no")))
1112 StopAfterIncludeError
= !_cups_strcasecmp(temp
, "yes");
1116 print_fatal_error(outfile
, "Missing STOP-AFTER-INCLUDE-ERROR value on line %d.",
1124 else if (!strcmp(token
, "TRANSFER"))
1132 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1134 if (!strcmp(temp
, "auto"))
1135 Transfer
= _CUPS_TRANSFER_AUTO
;
1136 else if (!strcmp(temp
, "chunked"))
1137 Transfer
= _CUPS_TRANSFER_CHUNKED
;
1138 else if (!strcmp(temp
, "length"))
1139 Transfer
= _CUPS_TRANSFER_LENGTH
;
1142 print_fatal_error(outfile
, "Bad TRANSFER value \"%s\" on line %d.", temp
,
1150 print_fatal_error(outfile
, "Missing TRANSFER value on line %d.", linenum
);
1157 else if (!strcmp(token
, "VERSION"))
1159 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1161 if (!strcmp(temp
, "1.0"))
1163 else if (!strcmp(temp
, "1.1"))
1165 else if (!strcmp(temp
, "2.0"))
1167 else if (!strcmp(temp
, "2.1"))
1169 else if (!strcmp(temp
, "2.2"))
1173 print_fatal_error(outfile
, "Bad VERSION \"%s\" on line %d.", temp
, linenum
);
1180 print_fatal_error(outfile
, "Missing VERSION number on line %d.", linenum
);
1187 else if (strcmp(token
, "{"))
1189 print_fatal_error(outfile
, "Unexpected token %s seen on line %d.", token
, linenum
);
1195 * Initialize things...
1200 if (Output
== _CUPS_OUTPUT_PLIST
)
1201 print_xml_header(outfile
);
1202 if (Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
!= stdout
))
1203 printf("\"%s\":\n", testfile
);
1208 strlcpy(resource
, vars
->resource
, sizeof(resource
));
1213 group
= IPP_TAG_ZERO
;
1214 ignore_errors
= IgnoreErrors
;
1222 transfer
= Transfer
;
1223 compression
[0] = '\0';
1225 strlcpy(name
, testfile
, sizeof(name
));
1226 if (strrchr(name
, '.') != NULL
)
1227 *strrchr(name
, '.') = '\0';
1230 * Parse until we see a close brace...
1233 while (get_token(fp
, token
, sizeof(token
), &linenum
) != NULL
)
1235 if (_cups_strcasecmp(token
, "COUNT") &&
1236 _cups_strcasecmp(token
, "DEFINE-MATCH") &&
1237 _cups_strcasecmp(token
, "DEFINE-NO-MATCH") &&
1238 _cups_strcasecmp(token
, "DEFINE-VALUE") &&
1239 _cups_strcasecmp(token
, "IF-DEFINED") &&
1240 _cups_strcasecmp(token
, "IF-NOT-DEFINED") &&
1241 _cups_strcasecmp(token
, "IN-GROUP") &&
1242 _cups_strcasecmp(token
, "OF-TYPE") &&
1243 _cups_strcasecmp(token
, "REPEAT-LIMIT") &&
1244 _cups_strcasecmp(token
, "REPEAT-MATCH") &&
1245 _cups_strcasecmp(token
, "REPEAT-NO-MATCH") &&
1246 _cups_strcasecmp(token
, "SAME-COUNT-AS") &&
1247 _cups_strcasecmp(token
, "WITH-ALL-VALUES") &&
1248 _cups_strcasecmp(token
, "WITH-ALL-HOSTNAMES") &&
1249 _cups_strcasecmp(token
, "WITH-ALL-RESOURCES") &&
1250 _cups_strcasecmp(token
, "WITH-ALL-SCHEMES") &&
1251 _cups_strcasecmp(token
, "WITH-HOSTNAME") &&
1252 _cups_strcasecmp(token
, "WITH-RESOURCE") &&
1253 _cups_strcasecmp(token
, "WITH-SCHEME") &&
1254 _cups_strcasecmp(token
, "WITH-VALUE") &&
1255 _cups_strcasecmp(token
, "WITH-VALUE-FROM"))
1258 if (_cups_strcasecmp(token
, "DEFINE-MATCH") &&
1259 _cups_strcasecmp(token
, "DEFINE-NO-MATCH") &&
1260 _cups_strcasecmp(token
, "IF-DEFINED") &&
1261 _cups_strcasecmp(token
, "IF-NOT-DEFINED") &&
1262 _cups_strcasecmp(token
, "REPEAT-LIMIT") &&
1263 _cups_strcasecmp(token
, "REPEAT-MATCH") &&
1264 _cups_strcasecmp(token
, "REPEAT-NO-MATCH"))
1267 if (!strcmp(token
, "}"))
1269 else if (!strcmp(token
, "{") && lastcol
)
1272 * Another collection value
1275 ipp_t
*col
= get_collection(outfile
, vars
, fp
, &linenum
);
1276 /* Collection value */
1280 ippSetCollection(request
, &lastcol
, ippGetCount(lastcol
), col
);
1288 else if (!strcmp(token
, "COMPRESSION"))
1292 * COMPRESSION deflate
1296 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1298 expand_variables(vars
, compression
, temp
, sizeof(compression
));
1300 if (strcmp(compression
, "none") && strcmp(compression
, "deflate") &&
1301 strcmp(compression
, "gzip"))
1303 if (strcmp(compression
, "none"))
1304 #endif /* HAVE_LIBZ */
1306 print_fatal_error(outfile
, "Unsupported COMPRESSION value '%s' on line %d.",
1307 compression
, linenum
);
1312 if (!strcmp(compression
, "none"))
1313 compression
[0] = '\0';
1317 print_fatal_error(outfile
, "Missing COMPRESSION value on line %d.", linenum
);
1322 else if (!strcmp(token
, "DEFINE"))
1328 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
1329 get_token(fp
, temp
, sizeof(temp
), &linenum
))
1331 expand_variables(vars
, token
, temp
, sizeof(token
));
1332 set_variable(outfile
, vars
, attr
, token
);
1336 print_fatal_error(outfile
, "Missing DEFINE name and/or value on line %d.",
1342 else if (!strcmp(token
, "IGNORE-ERRORS"))
1349 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
1350 (!_cups_strcasecmp(temp
, "yes") || !_cups_strcasecmp(temp
, "no")))
1352 ignore_errors
= !_cups_strcasecmp(temp
, "yes");
1356 print_fatal_error(outfile
, "Missing IGNORE-ERRORS value on line %d.", linenum
);
1363 else if (!_cups_strcasecmp(token
, "NAME"))
1369 get_token(fp
, temp
, sizeof(temp
), &linenum
);
1370 expand_variables(vars
, name
, temp
, sizeof(name
));
1372 else if (!_cups_strcasecmp(token
, "PAUSE"))
1375 * Pause with a message...
1378 get_token(fp
, token
, sizeof(token
), &linenum
);
1379 pause_message(token
);
1381 else if (!strcmp(token
, "REQUEST-ID"))
1388 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1390 if (isdigit(temp
[0] & 255))
1391 request_id
= atoi(temp
);
1392 else if (!_cups_strcasecmp(temp
, "random"))
1393 request_id
= (CUPS_RAND() % 1000) * 137 + 1;
1396 print_fatal_error(outfile
, "Bad REQUEST-ID value \"%s\" on line %d.", temp
,
1404 print_fatal_error(outfile
, "Missing REQUEST-ID value on line %d.", linenum
);
1409 else if (!strcmp(token
, "SKIP-IF-DEFINED"))
1412 * SKIP-IF-DEFINED variable
1415 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1417 if (get_variable(vars
, temp
))
1422 print_fatal_error(outfile
, "Missing SKIP-IF-DEFINED value on line %d.",
1428 else if (!strcmp(token
, "SKIP-IF-MISSING"))
1431 * SKIP-IF-MISSING filename
1434 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1436 expand_variables(vars
, token
, temp
, sizeof(token
));
1437 get_filename(testfile
, filename
, token
, sizeof(filename
));
1439 if (access(filename
, R_OK
))
1444 print_fatal_error(outfile
, "Missing SKIP-IF-MISSING filename on line %d.",
1450 else if (!strcmp(token
, "SKIP-IF-NOT-DEFINED"))
1453 * SKIP-IF-NOT-DEFINED variable
1456 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1458 if (!get_variable(vars
, temp
))
1463 print_fatal_error(outfile
, "Missing SKIP-IF-NOT-DEFINED value on line %d.",
1469 else if (!strcmp(token
, "SKIP-PREVIOUS-ERROR"))
1472 * SKIP-PREVIOUS-ERROR yes
1473 * SKIP-PREVIOUS-ERROR no
1476 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
1477 (!_cups_strcasecmp(temp
, "yes") || !_cups_strcasecmp(temp
, "no")))
1479 skip_previous
= !_cups_strcasecmp(temp
, "yes");
1483 print_fatal_error(outfile
, "Missing SKIP-PREVIOUS-ERROR value on line %d.", linenum
);
1490 else if (!strcmp(token
, "TEST-ID"))
1496 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1498 expand_variables(vars
, test_id
, temp
, sizeof(test_id
));
1502 print_fatal_error(outfile
, "Missing TEST-ID value on line %d.", linenum
);
1509 else if (!strcmp(token
, "TRANSFER"))
1517 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1519 if (!strcmp(temp
, "auto"))
1520 transfer
= _CUPS_TRANSFER_AUTO
;
1521 else if (!strcmp(temp
, "chunked"))
1522 transfer
= _CUPS_TRANSFER_CHUNKED
;
1523 else if (!strcmp(temp
, "length"))
1524 transfer
= _CUPS_TRANSFER_LENGTH
;
1527 print_fatal_error(outfile
, "Bad TRANSFER value \"%s\" on line %d.", temp
,
1535 print_fatal_error(outfile
, "Missing TRANSFER value on line %d.", linenum
);
1540 else if (!_cups_strcasecmp(token
, "VERSION"))
1542 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1544 if (!strcmp(temp
, "0.0"))
1546 else if (!strcmp(temp
, "1.0"))
1548 else if (!strcmp(temp
, "1.1"))
1550 else if (!strcmp(temp
, "2.0"))
1552 else if (!strcmp(temp
, "2.1"))
1554 else if (!strcmp(temp
, "2.2"))
1558 print_fatal_error(outfile
, "Bad VERSION \"%s\" on line %d.", temp
, linenum
);
1565 print_fatal_error(outfile
, "Missing VERSION number on line %d.", linenum
);
1570 else if (!_cups_strcasecmp(token
, "RESOURCE"))
1576 if (!get_token(fp
, resource
, sizeof(resource
), &linenum
))
1578 print_fatal_error(outfile
, "Missing RESOURCE path on line %d.", linenum
);
1583 else if (!_cups_strcasecmp(token
, "OPERATION"))
1589 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1591 print_fatal_error(outfile
, "Missing OPERATION code on line %d.", linenum
);
1596 expand_variables(vars
, token
, temp
, sizeof(token
));
1598 if ((op
= ippOpValue(token
)) == (ipp_op_t
)-1 &&
1599 (op
= (ipp_op_t
)strtol(token
, NULL
, 0)) == 0)
1601 print_fatal_error(outfile
, "Bad OPERATION code \"%s\" on line %d.", token
,
1607 else if (!_cups_strcasecmp(token
, "GROUP"))
1610 * Attribute group...
1613 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1615 print_fatal_error(outfile
, "Missing GROUP tag on line %d.", linenum
);
1620 if ((value
= ippTagValue(token
)) < 0)
1622 print_fatal_error(outfile
, "Bad GROUP tag \"%s\" on line %d.", token
, linenum
);
1628 ippAddSeparator(request
);
1632 else if (!_cups_strcasecmp(token
, "DELAY"))
1635 * Delay before operation...
1640 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1642 print_fatal_error(outfile
, "Missing DELAY value on line %d.", linenum
);
1647 expand_variables(vars
, token
, temp
, sizeof(token
));
1649 if ((delay
= _cupsStrScand(token
, NULL
, localeconv())) <= 0.0)
1651 print_fatal_error(outfile
, "Bad DELAY value \"%s\" on line %d.", token
,
1658 if (Output
== _CUPS_OUTPUT_TEST
)
1659 printf(" [%g second delay]\n", delay
);
1661 usleep((useconds_t
)(1000000.0 * delay
));
1664 else if (!_cups_strcasecmp(token
, "ATTR"))
1670 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1672 print_fatal_error(outfile
, "Missing ATTR value tag on line %d.", linenum
);
1677 if ((value
= ippTagValue(token
)) == IPP_TAG_ZERO
)
1679 print_fatal_error(outfile
, "Bad ATTR value tag \"%s\" on line %d.", token
,
1685 if (!get_token(fp
, attr
, sizeof(attr
), &linenum
))
1687 print_fatal_error(outfile
, "Missing ATTR name on line %d.", linenum
);
1692 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1694 print_fatal_error(outfile
, "Missing ATTR value on line %d.", linenum
);
1699 expand_variables(vars
, token
, temp
, sizeof(token
));
1704 case IPP_TAG_BOOLEAN
:
1705 if (!_cups_strcasecmp(token
, "true"))
1706 attrptr
= ippAddBoolean(request
, group
, attr
, 1);
1708 attrptr
= ippAddBoolean(request
, group
, attr
, (char)atoi(token
));
1711 case IPP_TAG_INTEGER
:
1713 if (!strchr(token
, ','))
1715 if (isdigit(token
[0] & 255) || token
[0] == '-')
1716 attrptr
= ippAddInteger(request
, group
, value
, attr
, (int)strtol(token
, &tokenptr
, 0));
1720 if ((i
= ippEnumValue(attr
, tokenptr
)) >= 0)
1722 attrptr
= ippAddInteger(request
, group
, value
, attr
, i
);
1723 tokenptr
+= strlen(tokenptr
);
1729 int values
[100], /* Values */
1730 num_values
= 1; /* Number of values */
1732 if (!isdigit(token
[0] & 255) && token
[0] != '-' && value
== IPP_TAG_ENUM
)
1734 char *ptr
; /* Pointer to next terminator */
1736 if ((ptr
= strchr(token
, ',')) != NULL
)
1739 ptr
= token
+ strlen(token
);
1741 if ((i
= ippEnumValue(attr
, token
)) < 0)
1747 i
= (int)strtol(tokenptr
, &tokenptr
, 0);
1751 while (tokenptr
&& *tokenptr
&&
1752 num_values
< (int)(sizeof(values
) / sizeof(values
[0])))
1754 if (*tokenptr
== ',')
1757 if (!isdigit(*tokenptr
& 255) && *tokenptr
!= '-')
1759 char *ptr
; /* Pointer to next terminator */
1761 if (value
!= IPP_TAG_ENUM
)
1764 if ((ptr
= strchr(tokenptr
, ',')) != NULL
)
1767 ptr
= tokenptr
+ strlen(tokenptr
);
1769 if ((i
= ippEnumValue(attr
, tokenptr
)) < 0)
1775 i
= (int)strtol(tokenptr
, &tokenptr
, 0);
1777 values
[num_values
++] = i
;
1780 attrptr
= ippAddIntegers(request
, group
, value
, attr
, num_values
, values
);
1783 if ((!token
[0] || !tokenptr
|| *tokenptr
) && !skip_test
)
1785 print_fatal_error(outfile
, "Bad %s value \'%s\' for \"%s\" on line %d.",
1786 ippTagString(value
), token
, attr
, linenum
);
1792 case IPP_TAG_RESOLUTION
:
1794 int xres
, /* X resolution */
1795 yres
; /* Y resolution */
1796 char *ptr
; /* Pointer into value */
1798 xres
= yres
= (int)strtol(token
, (char **)&ptr
, 10);
1799 if (ptr
> token
&& xres
> 0)
1802 yres
= (int)strtol(ptr
+ 1, (char **)&ptr
, 10);
1805 if (ptr
<= token
|| xres
<= 0 || yres
<= 0 || !ptr
||
1806 (_cups_strcasecmp(ptr
, "dpi") &&
1807 _cups_strcasecmp(ptr
, "dpc") &&
1808 _cups_strcasecmp(ptr
, "dpcm") &&
1809 _cups_strcasecmp(ptr
, "other")))
1814 print_fatal_error(outfile
, "Bad resolution value \'%s\' for \"%s\" on line %d.", token
, attr
, linenum
);
1819 if (!_cups_strcasecmp(ptr
, "dpi"))
1820 attrptr
= ippAddResolution(request
, group
, attr
, IPP_RES_PER_INCH
, xres
, yres
);
1821 else if (!_cups_strcasecmp(ptr
, "dpc") ||
1822 !_cups_strcasecmp(ptr
, "dpcm"))
1823 attrptr
= ippAddResolution(request
, group
, attr
, IPP_RES_PER_CM
, xres
, yres
);
1825 attrptr
= ippAddResolution(request
, group
, attr
, (ipp_res_t
)0, xres
, yres
);
1829 case IPP_TAG_RANGE
:
1831 int lowers
[4], /* Lower value */
1832 uppers
[4], /* Upper values */
1833 num_vals
; /* Number of values */
1836 num_vals
= sscanf(token
, "%d-%d,%d-%d,%d-%d,%d-%d",
1837 lowers
+ 0, uppers
+ 0,
1838 lowers
+ 1, uppers
+ 1,
1839 lowers
+ 2, uppers
+ 2,
1840 lowers
+ 3, uppers
+ 3);
1842 if ((num_vals
& 1) || num_vals
== 0)
1847 print_fatal_error(outfile
, "Bad rangeOfInteger value \'%s\' for \"%s\" on line %d.", token
, attr
, linenum
);
1852 attrptr
= ippAddRanges(request
, group
, attr
, num_vals
/ 2, lowers
,
1857 case IPP_TAG_BEGIN_COLLECTION
:
1858 if (!strcmp(token
, "{"))
1860 ipp_t
*col
= get_collection(outfile
, vars
, fp
, &linenum
);
1861 /* Collection value */
1865 attrptr
= lastcol
= ippAddCollection(request
, group
, attr
, col
);
1878 print_fatal_error(outfile
, "Bad ATTR collection value for \"%s\" on line %d.", attr
, linenum
);
1885 ipp_t
*col
; /* Collection value */
1886 long savepos
= ftell(fp
); /* Save position of file */
1887 int savelinenum
= linenum
; /* Save line number */
1889 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1892 if (strcmp(token
, ","))
1894 fseek(fp
, savepos
, SEEK_SET
);
1895 linenum
= savelinenum
;
1899 if (!get_token(fp
, token
, sizeof(token
), &linenum
) || strcmp(token
, "{"))
1901 print_fatal_error(outfile
, "Unexpected \"%s\" on line %d.", token
, linenum
);
1907 if ((col
= get_collection(outfile
, vars
, fp
, &linenum
)) == NULL
)
1910 ippSetCollection(request
, &attrptr
, ippGetCount(attrptr
), col
);
1913 while (!strcmp(token
, "{"));
1916 case IPP_TAG_STRING
:
1917 attrptr
= ippAddOctetString(request
, group
, attr
, token
, (int)strlen(token
));
1921 print_fatal_error(outfile
, "Unsupported ATTR value tag %s for \"%s\" on line %d.", ippTagString(value
), attr
, linenum
);
1925 case IPP_TAG_TEXTLANG
:
1926 case IPP_TAG_NAMELANG
:
1929 case IPP_TAG_KEYWORD
:
1931 case IPP_TAG_URISCHEME
:
1932 case IPP_TAG_CHARSET
:
1933 case IPP_TAG_LANGUAGE
:
1934 case IPP_TAG_MIMETYPE
:
1935 if (!strchr(token
, ','))
1936 attrptr
= ippAddString(request
, group
, value
, attr
, NULL
, token
);
1940 * Multiple string values...
1943 int num_values
; /* Number of values */
1944 char *values
[100], /* Values */
1945 *ptr
; /* Pointer to next value */
1951 for (ptr
= strchr(token
, ','); ptr
; ptr
= strchr(ptr
, ','))
1953 if (ptr
> token
&& ptr
[-1] == '\\')
1954 _cups_strcpy(ptr
- 1, ptr
);
1958 values
[num_values
] = ptr
;
1963 attrptr
= ippAddStrings(request
, group
, value
, attr
, num_values
,
1964 NULL
, (const char **)values
);
1969 if (!attrptr
&& !skip_test
)
1971 print_fatal_error(outfile
, "Unable to add attribute \"%s\" on line %d.", attr
, linenum
);
1976 else if (!_cups_strcasecmp(token
, "FILE"))
1982 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1984 print_fatal_error(outfile
, "Missing FILE filename on line %d.", linenum
);
1989 expand_variables(vars
, token
, temp
, sizeof(token
));
1990 get_filename(testfile
, filename
, token
, sizeof(filename
));
1992 if (access(filename
, R_OK
))
1994 print_fatal_error(outfile
, "Filename \"%s\" on line %d cannot be read.",
1996 print_fatal_error(outfile
, "Filename mapped to \"%s\".", filename
);
2001 else if (!_cups_strcasecmp(token
, "STATUS"))
2007 if (num_statuses
>= (int)(sizeof(statuses
) / sizeof(statuses
[0])))
2009 print_fatal_error(outfile
, "Too many STATUS's on line %d.", linenum
);
2014 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2016 print_fatal_error(outfile
, "Missing STATUS code on line %d.", linenum
);
2021 if ((statuses
[num_statuses
].status
= ippErrorValue(token
))
2022 == (ipp_status_t
)-1 &&
2023 (statuses
[num_statuses
].status
= (ipp_status_t
)strtol(token
, NULL
, 0)) == 0)
2025 print_fatal_error(outfile
, "Bad STATUS code \"%s\" on line %d.", token
,
2031 last_status
= statuses
+ num_statuses
;
2034 last_status
->define_match
= NULL
;
2035 last_status
->define_no_match
= NULL
;
2036 last_status
->if_defined
= NULL
;
2037 last_status
->if_not_defined
= NULL
;
2038 last_status
->repeat_limit
= 1000;
2039 last_status
->repeat_match
= 0;
2040 last_status
->repeat_no_match
= 0;
2042 else if (!_cups_strcasecmp(token
, "EXPECT") || !_cups_strcasecmp(token
, "EXPECT-ALL"))
2045 * Expected attributes...
2048 int expect_all
= !_cups_strcasecmp(token
, "EXPECT-ALL");
2050 if (num_expects
>= (int)(sizeof(expects
) / sizeof(expects
[0])))
2052 print_fatal_error(outfile
, "Too many EXPECT's on line %d.", linenum
);
2057 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2059 print_fatal_error(outfile
, "Missing EXPECT name on line %d.", linenum
);
2064 last_expect
= expects
+ num_expects
;
2067 memset(last_expect
, 0, sizeof(_cups_expect_t
));
2068 last_expect
->repeat_limit
= 1000;
2069 last_expect
->expect_all
= expect_all
;
2071 if (token
[0] == '!')
2073 last_expect
->not_expect
= 1;
2074 last_expect
->name
= strdup(token
+ 1);
2076 else if (token
[0] == '?')
2078 last_expect
->optional
= 1;
2079 last_expect
->name
= strdup(token
+ 1);
2082 last_expect
->name
= strdup(token
);
2084 else if (!_cups_strcasecmp(token
, "COUNT"))
2086 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2088 print_fatal_error(outfile
, "Missing COUNT number on line %d.", linenum
);
2093 if ((i
= atoi(token
)) <= 0)
2095 print_fatal_error(outfile
, "Bad COUNT \"%s\" on line %d.", token
, linenum
);
2101 last_expect
->count
= i
;
2104 print_fatal_error(outfile
, "COUNT without a preceding EXPECT on line %d.",
2110 else if (!_cups_strcasecmp(token
, "DEFINE-MATCH"))
2112 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2114 print_fatal_error(outfile
, "Missing DEFINE-MATCH variable on line %d.",
2121 last_expect
->define_match
= strdup(token
);
2122 else if (last_status
)
2123 last_status
->define_match
= strdup(token
);
2126 print_fatal_error(outfile
, "DEFINE-MATCH without a preceding EXPECT or STATUS "
2127 "on line %d.", linenum
);
2132 else if (!_cups_strcasecmp(token
, "DEFINE-NO-MATCH"))
2134 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2136 print_fatal_error(outfile
, "Missing DEFINE-NO-MATCH variable on line %d.",
2143 last_expect
->define_no_match
= strdup(token
);
2144 else if (last_status
)
2145 last_status
->define_no_match
= strdup(token
);
2148 print_fatal_error(outfile
, "DEFINE-NO-MATCH without a preceding EXPECT or "
2149 "STATUS on line %d.", linenum
);
2154 else if (!_cups_strcasecmp(token
, "DEFINE-VALUE"))
2156 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2158 print_fatal_error(outfile
, "Missing DEFINE-VALUE variable on line %d.",
2165 last_expect
->define_value
= strdup(token
);
2168 print_fatal_error(outfile
, "DEFINE-VALUE without a preceding EXPECT on "
2169 "line %d.", linenum
);
2174 else if (!_cups_strcasecmp(token
, "OF-TYPE"))
2176 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2178 print_fatal_error(outfile
, "Missing OF-TYPE value tag(s) on line %d.",
2185 last_expect
->of_type
= strdup(token
);
2188 print_fatal_error(outfile
, "OF-TYPE without a preceding EXPECT on line %d.",
2194 else if (!_cups_strcasecmp(token
, "IN-GROUP"))
2196 ipp_tag_t in_group
; /* IN-GROUP value */
2199 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2201 print_fatal_error(outfile
, "Missing IN-GROUP group tag on line %d.", linenum
);
2206 if ((in_group
= ippTagValue(token
)) == (ipp_tag_t
)-1)
2209 else if (last_expect
)
2210 last_expect
->in_group
= in_group
;
2213 print_fatal_error(outfile
, "IN-GROUP without a preceding EXPECT on line %d.",
2219 else if (!_cups_strcasecmp(token
, "REPEAT-LIMIT"))
2221 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2223 print_fatal_error(outfile
, "Missing REPEAT-LIMIT value on line %d.", linenum
);
2227 else if (atoi(token
) <= 0)
2229 print_fatal_error(outfile
, "Bad REPEAT-LIMIT value on line %d.", linenum
);
2235 last_status
->repeat_limit
= atoi(token
);
2236 else if (last_expect
)
2237 last_expect
->repeat_limit
= atoi(token
);
2240 print_fatal_error(outfile
, "REPEAT-LIMIT without a preceding EXPECT or STATUS "
2241 "on line %d.", linenum
);
2246 else if (!_cups_strcasecmp(token
, "REPEAT-MATCH"))
2249 last_status
->repeat_match
= 1;
2250 else if (last_expect
)
2251 last_expect
->repeat_match
= 1;
2254 print_fatal_error(outfile
, "REPEAT-MATCH without a preceding EXPECT or STATUS "
2255 "on line %d.", linenum
);
2260 else if (!_cups_strcasecmp(token
, "REPEAT-NO-MATCH"))
2263 last_status
->repeat_no_match
= 1;
2264 else if (last_expect
)
2265 last_expect
->repeat_no_match
= 1;
2268 print_fatal_error(outfile
, "REPEAT-NO-MATCH without a preceding EXPECT or "
2269 "STATUS on ine %d.", linenum
);
2274 else if (!_cups_strcasecmp(token
, "SAME-COUNT-AS"))
2276 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2278 print_fatal_error(outfile
, "Missing SAME-COUNT-AS name on line %d.", linenum
);
2284 last_expect
->same_count_as
= strdup(token
);
2287 print_fatal_error(outfile
, "SAME-COUNT-AS without a preceding EXPECT on line "
2293 else if (!_cups_strcasecmp(token
, "IF-DEFINED"))
2295 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2297 print_fatal_error(outfile
, "Missing IF-DEFINED name on line %d.", linenum
);
2303 last_expect
->if_defined
= strdup(token
);
2304 else if (last_status
)
2305 last_status
->if_defined
= strdup(token
);
2308 print_fatal_error(outfile
, "IF-DEFINED without a preceding EXPECT or STATUS "
2309 "on line %d.", linenum
);
2314 else if (!_cups_strcasecmp(token
, "IF-NOT-DEFINED"))
2316 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2318 print_fatal_error(outfile
, "Missing IF-NOT-DEFINED name on line %d.", linenum
);
2324 last_expect
->if_not_defined
= strdup(token
);
2325 else if (last_status
)
2326 last_status
->if_not_defined
= strdup(token
);
2329 print_fatal_error(outfile
, "IF-NOT-DEFINED without a preceding EXPECT or STATUS "
2330 "on line %d.", linenum
);
2335 else if (!_cups_strcasecmp(token
, "WITH-ALL-VALUES") ||
2336 !_cups_strcasecmp(token
, "WITH-ALL-HOSTNAMES") ||
2337 !_cups_strcasecmp(token
, "WITH-ALL-RESOURCES") ||
2338 !_cups_strcasecmp(token
, "WITH-ALL-SCHEMES") ||
2339 !_cups_strcasecmp(token
, "WITH-HOSTNAME") ||
2340 !_cups_strcasecmp(token
, "WITH-RESOURCE") ||
2341 !_cups_strcasecmp(token
, "WITH-SCHEME") ||
2342 !_cups_strcasecmp(token
, "WITH-VALUE"))
2346 if (!_cups_strcasecmp(token
, "WITH-ALL-HOSTNAMES") ||
2347 !_cups_strcasecmp(token
, "WITH-HOSTNAME"))
2348 last_expect
->with_flags
= _CUPS_WITH_HOSTNAME
;
2349 else if (!_cups_strcasecmp(token
, "WITH-ALL-RESOURCES") ||
2350 !_cups_strcasecmp(token
, "WITH-RESOURCE"))
2351 last_expect
->with_flags
= _CUPS_WITH_RESOURCE
;
2352 else if (!_cups_strcasecmp(token
, "WITH-ALL-SCHEMES") ||
2353 !_cups_strcasecmp(token
, "WITH-SCHEME"))
2354 last_expect
->with_flags
= _CUPS_WITH_SCHEME
;
2356 if (!_cups_strncasecmp(token
, "WITH-ALL-", 9))
2357 last_expect
->with_flags
|= _CUPS_WITH_ALL
;
2360 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
2362 print_fatal_error(outfile
, "Missing %s value on line %d.", token
, linenum
);
2370 * Expand any variables in the value and then save it.
2373 expand_variables(vars
, token
, temp
, sizeof(token
));
2375 tokenptr
= token
+ strlen(token
) - 1;
2377 if (token
[0] == '/' && tokenptr
> token
&& *tokenptr
== '/')
2380 * WITH-VALUE is a POSIX extended regular expression.
2383 last_expect
->with_value
= calloc(1, (size_t)(tokenptr
- token
));
2384 last_expect
->with_flags
|= _CUPS_WITH_REGEX
;
2386 if (last_expect
->with_value
)
2387 memcpy(last_expect
->with_value
, token
+ 1, (size_t)(tokenptr
- token
- 1));
2392 * WITH-VALUE is a literal value...
2395 char *ptr
; /* Pointer into value */
2397 for (ptr
= token
; *ptr
; ptr
++)
2399 if (*ptr
== '\\' && ptr
[1])
2402 * Remove \ from \foo...
2405 _cups_strcpy(ptr
, ptr
+ 1);
2409 last_expect
->with_value
= strdup(token
);
2410 last_expect
->with_flags
|= _CUPS_WITH_LITERAL
;
2415 print_fatal_error(outfile
, "%s without a preceding EXPECT on line %d.", token
,
2421 else if (!_cups_strcasecmp(token
, "WITH-VALUE-FROM"))
2423 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
2425 print_fatal_error(outfile
, "Missing %s value on line %d.", token
, linenum
);
2433 * Expand any variables in the value and then save it.
2436 expand_variables(vars
, token
, temp
, sizeof(token
));
2438 last_expect
->with_value_from
= strdup(token
);
2439 last_expect
->with_flags
= _CUPS_WITH_LITERAL
;
2443 print_fatal_error(outfile
, "%s without a preceding EXPECT on line %d.", token
,
2449 else if (!_cups_strcasecmp(token
, "DISPLAY"))
2452 * Display attributes...
2455 if (num_displayed
>= (int)(sizeof(displayed
) / sizeof(displayed
[0])))
2457 print_fatal_error(outfile
, "Too many DISPLAY's on line %d", linenum
);
2462 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2464 print_fatal_error(outfile
, "Missing DISPLAY name on line %d.", linenum
);
2469 displayed
[num_displayed
] = strdup(token
);
2474 print_fatal_error(outfile
, "Unexpected token %s seen on line %d.", token
,
2482 * Submit the IPP request...
2487 ippSetVersion(request
, version
/ 10, version
% 10);
2488 ippSetOperation(request
, op
);
2489 ippSetRequestId(request
, request_id
);
2491 if (Output
== _CUPS_OUTPUT_PLIST
)
2493 fputs("<dict>\n", outfile
);
2494 fputs("<key>Name</key>\n", outfile
);
2495 print_xml_string(outfile
, "string", name
);
2498 fputs("<key>FileId</key>\n", outfile
);
2499 print_xml_string(outfile
, "string", file_id
);
2503 fputs("<key>TestId</key>\n", outfile
);
2504 print_xml_string(outfile
, "string", test_id
);
2506 fputs("<key>Version</key>\n", outfile
);
2507 fprintf(outfile
, "<string>%d.%d</string>\n", version
/ 10, version
% 10);
2508 fputs("<key>Operation</key>\n", outfile
);
2509 print_xml_string(outfile
, "string", ippOpString(op
));
2510 fputs("<key>RequestId</key>\n", outfile
);
2511 fprintf(outfile
, "<integer>%d</integer>\n", request_id
);
2512 fputs("<key>RequestAttributes</key>\n", outfile
);
2513 fputs("<array>\n", outfile
);
2516 fputs("<dict>\n", outfile
);
2517 for (attrptr
= request
->attrs
,
2518 group
= attrptr
? attrptr
->group_tag
: IPP_TAG_ZERO
;
2520 attrptr
= attrptr
->next
)
2521 print_attr(outfile
, Output
, attrptr
, &group
);
2522 fputs("</dict>\n", outfile
);
2524 fputs("</array>\n", outfile
);
2527 if (Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
!= stdout
))
2531 printf(" %s:\n", ippOpString(op
));
2533 for (attrptr
= request
->attrs
; attrptr
; attrptr
= attrptr
->next
)
2534 print_attr(stdout
, _CUPS_OUTPUT_TEST
, attrptr
, NULL
);
2537 printf(" %-68.68s [", name
);
2541 if ((skip_previous
&& !prev_pass
) || skip_test
)
2548 if (Output
== _CUPS_OUTPUT_PLIST
)
2550 fputs("<key>Successful</key>\n", outfile
);
2551 fputs("<true />\n", outfile
);
2552 fputs("<key>Skipped</key>\n", outfile
);
2553 fputs("<true />\n", outfile
);
2554 fputs("<key>StatusCode</key>\n", outfile
);
2555 print_xml_string(outfile
, "string", "skip");
2556 fputs("<key>ResponseAttributes</key>\n", outfile
);
2557 fputs("<dict />\n", outfile
);
2560 if (Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
!= stdout
))
2568 repeat_interval
= 1;
2575 status
= HTTP_STATUS_OK
;
2577 if (transfer
== _CUPS_TRANSFER_CHUNKED
||
2578 (transfer
== _CUPS_TRANSFER_AUTO
&& filename
[0]))
2581 * Send request using chunking - a 0 length means "chunk".
2589 * Send request using content length...
2592 length
= ippLength(request
);
2594 if (filename
[0] && (reqfile
= cupsFileOpen(filename
, "r")) != NULL
)
2597 * Read the file to get the uncompressed file size...
2600 while ((bytes
= cupsFileRead(reqfile
, buffer
, sizeof(buffer
))) > 0)
2601 length
+= (size_t)bytes
;
2603 cupsFileClose(reqfile
);
2608 * Send the request...
2615 if (status
!= HTTP_STATUS_ERROR
)
2617 while (!response
&& !Cancel
&& prev_pass
)
2619 status
= cupsSendRequest(http
, request
, resource
, length
);
2623 httpSetField(http
, HTTP_FIELD_CONTENT_ENCODING
, compression
);
2624 #endif /* HAVE_LIBZ */
2626 if (!Cancel
&& status
== HTTP_STATUS_CONTINUE
&&
2627 request
->state
== IPP_DATA
&& filename
[0])
2629 if ((reqfile
= cupsFileOpen(filename
, "r")) != NULL
)
2632 (bytes
= cupsFileRead(reqfile
, buffer
, sizeof(buffer
))) > 0)
2633 if ((status
= cupsWriteRequestData(http
, buffer
, (size_t)bytes
)) != HTTP_STATUS_CONTINUE
)
2636 cupsFileClose(reqfile
);
2640 snprintf(buffer
, sizeof(buffer
), "%s: %s", filename
,
2642 _cupsSetError(IPP_INTERNAL_ERROR
, buffer
, 0);
2644 status
= HTTP_STATUS_ERROR
;
2649 * Get the server's response...
2652 if (!Cancel
&& status
!= HTTP_STATUS_ERROR
)
2654 response
= cupsGetResponse(http
, resource
);
2655 status
= httpGetStatus(http
);
2658 if (!Cancel
&& status
== HTTP_STATUS_ERROR
&& http
->error
!= EINVAL
&&
2660 http
->error
!= WSAETIMEDOUT
)
2662 http
->error
!= ETIMEDOUT
)
2665 if (httpReconnect(http
))
2668 else if (status
== HTTP_STATUS_ERROR
|| status
== HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
)
2673 else if (status
!= HTTP_STATUS_OK
)
2677 if (status
== HTTP_STATUS_UNAUTHORIZED
)
2685 if (!Cancel
&& status
== HTTP_STATUS_ERROR
&& http
->error
!= EINVAL
&&
2687 http
->error
!= WSAETIMEDOUT
)
2689 http
->error
!= ETIMEDOUT
)
2692 if (httpReconnect(http
))
2695 else if (status
== HTTP_STATUS_ERROR
)
2698 httpReconnect(http
);
2702 else if (status
!= HTTP_STATUS_OK
)
2709 * Check results of request...
2712 cupsArrayClear(errors
);
2714 if (http
->version
!= HTTP_1_1
)
2715 add_stringf(errors
, "Bad HTTP version (%d.%d)", http
->version
/ 100,
2716 http
->version
% 100);
2721 * No response, log error...
2724 add_stringf(errors
, "IPP request failed with status %s (%s)",
2725 ippErrorString(cupsLastError()),
2726 cupsLastErrorString());
2731 * Collect common attribute values...
2734 if ((attrptr
= ippFindAttribute(response
, "job-id",
2735 IPP_TAG_INTEGER
)) != NULL
)
2737 snprintf(temp
, sizeof(temp
), "%d", attrptr
->values
[0].integer
);
2738 set_variable(outfile
, vars
, "job-id", temp
);
2741 if ((attrptr
= ippFindAttribute(response
, "job-uri",
2742 IPP_TAG_URI
)) != NULL
)
2743 set_variable(outfile
, vars
, "job-uri", attrptr
->values
[0].string
.text
);
2745 if ((attrptr
= ippFindAttribute(response
, "notify-subscription-id",
2746 IPP_TAG_INTEGER
)) != NULL
)
2748 snprintf(temp
, sizeof(temp
), "%d", attrptr
->values
[0].integer
);
2749 set_variable(outfile
, vars
, "notify-subscription-id", temp
);
2753 * Check response, validating groups and attributes and logging errors
2757 if (response
->state
!= IPP_DATA
)
2759 "Missing end-of-attributes-tag in response "
2760 "(RFC 2910 section 3.5.1)");
2763 (response
->request
.status
.version
[0] != (version
/ 10) ||
2764 response
->request
.status
.version
[1] != (version
% 10)))
2766 "Bad version %d.%d in response - expected %d.%d "
2767 "(RFC 2911 section 3.1.8).",
2768 response
->request
.status
.version
[0],
2769 response
->request
.status
.version
[1],
2770 version
/ 10, version
% 10);
2772 if (response
->request
.status
.request_id
!= request_id
)
2774 "Bad request ID %d in response - expected %d "
2775 "(RFC 2911 section 3.1.1)",
2776 response
->request
.status
.request_id
, request_id
);
2778 attrptr
= response
->attrs
;
2781 "Missing first attribute \"attributes-charset "
2782 "(charset)\" in group operation-attributes-tag "
2783 "(RFC 2911 section 3.1.4).");
2786 if (!attrptr
->name
||
2787 attrptr
->value_tag
!= IPP_TAG_CHARSET
||
2788 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2789 attrptr
->num_values
!= 1 ||
2790 strcmp(attrptr
->name
, "attributes-charset"))
2792 "Bad first attribute \"%s (%s%s)\" in group %s, "
2793 "expected \"attributes-charset (charset)\" in "
2794 "group operation-attributes-tag (RFC 2911 section "
2796 attrptr
->name
? attrptr
->name
: "(null)",
2797 attrptr
->num_values
> 1 ? "1setOf " : "",
2798 ippTagString(attrptr
->value_tag
),
2799 ippTagString(attrptr
->group_tag
));
2801 attrptr
= attrptr
->next
;
2804 "Missing second attribute \"attributes-natural-"
2805 "language (naturalLanguage)\" in group "
2806 "operation-attributes-tag (RFC 2911 section "
2808 else if (!attrptr
->name
||
2809 attrptr
->value_tag
!= IPP_TAG_LANGUAGE
||
2810 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2811 attrptr
->num_values
!= 1 ||
2812 strcmp(attrptr
->name
, "attributes-natural-language"))
2814 "Bad first attribute \"%s (%s%s)\" in group %s, "
2815 "expected \"attributes-natural-language "
2816 "(naturalLanguage)\" in group "
2817 "operation-attributes-tag (RFC 2911 section "
2819 attrptr
->name
? attrptr
->name
: "(null)",
2820 attrptr
->num_values
> 1 ? "1setOf " : "",
2821 ippTagString(attrptr
->value_tag
),
2822 ippTagString(attrptr
->group_tag
));
2825 if ((attrptr
= ippFindAttribute(response
, "status-message",
2826 IPP_TAG_ZERO
)) != NULL
)
2828 if (attrptr
->value_tag
!= IPP_TAG_TEXT
)
2830 "status-message (text(255)) has wrong value tag "
2831 "%s (RFC 2911 section 3.1.6.2).",
2832 ippTagString(attrptr
->value_tag
));
2833 if (attrptr
->group_tag
!= IPP_TAG_OPERATION
)
2835 "status-message (text(255)) has wrong group tag "
2836 "%s (RFC 2911 section 3.1.6.2).",
2837 ippTagString(attrptr
->group_tag
));
2838 if (attrptr
->num_values
!= 1)
2840 "status-message (text(255)) has %d values "
2841 "(RFC 2911 section 3.1.6.2).",
2842 attrptr
->num_values
);
2843 if (attrptr
->value_tag
== IPP_TAG_TEXT
&&
2844 strlen(attrptr
->values
[0].string
.text
) > 255)
2846 "status-message (text(255)) has bad length %d"
2847 " (RFC 2911 section 3.1.6.2).",
2848 (int)strlen(attrptr
->values
[0].string
.text
));
2851 if ((attrptr
= ippFindAttribute(response
, "detailed-status-message",
2852 IPP_TAG_ZERO
)) != NULL
)
2854 if (attrptr
->value_tag
!= IPP_TAG_TEXT
)
2856 "detailed-status-message (text(MAX)) has wrong "
2857 "value tag %s (RFC 2911 section 3.1.6.3).",
2858 ippTagString(attrptr
->value_tag
));
2859 if (attrptr
->group_tag
!= IPP_TAG_OPERATION
)
2861 "detailed-status-message (text(MAX)) has wrong "
2862 "group tag %s (RFC 2911 section 3.1.6.3).",
2863 ippTagString(attrptr
->group_tag
));
2864 if (attrptr
->num_values
!= 1)
2866 "detailed-status-message (text(MAX)) has %d values"
2867 " (RFC 2911 section 3.1.6.3).",
2868 attrptr
->num_values
);
2869 if (attrptr
->value_tag
== IPP_TAG_TEXT
&&
2870 strlen(attrptr
->values
[0].string
.text
) > 1023)
2872 "detailed-status-message (text(MAX)) has bad "
2873 "length %d (RFC 2911 section 3.1.6.3).",
2874 (int)strlen(attrptr
->values
[0].string
.text
));
2877 a
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
2879 for (attrptr
= response
->attrs
,
2880 group
= attrptr
? attrptr
->group_tag
: IPP_TAG_ZERO
;
2882 attrptr
= attrptr
->next
)
2884 if (attrptr
->group_tag
!= group
)
2886 int out_of_order
= 0; /* Are attribute groups out-of-order? */
2889 switch (attrptr
->group_tag
)
2894 case IPP_TAG_OPERATION
:
2898 case IPP_TAG_UNSUPPORTED_GROUP
:
2899 if (group
!= IPP_TAG_OPERATION
)
2904 case IPP_TAG_PRINTER
:
2905 if (group
!= IPP_TAG_OPERATION
&&
2906 group
!= IPP_TAG_UNSUPPORTED_GROUP
)
2910 case IPP_TAG_SUBSCRIPTION
:
2911 if (group
> attrptr
->group_tag
&&
2912 group
!= IPP_TAG_DOCUMENT
)
2917 if (group
> attrptr
->group_tag
)
2923 add_stringf(errors
, "Attribute groups out of order (%s < %s)",
2924 ippTagString(attrptr
->group_tag
),
2925 ippTagString(group
));
2927 if (attrptr
->group_tag
!= IPP_TAG_ZERO
)
2928 group
= attrptr
->group_tag
;
2931 validate_attr(outfile
, errors
, attrptr
);
2935 if (cupsArrayFind(a
, attrptr
->name
))
2936 add_stringf(errors
, "Duplicate \"%s\" attribute in %s group",
2937 attrptr
->name
, ippTagString(group
));
2939 cupsArrayAdd(a
, attrptr
->name
);
2946 * Now check the test-defined expected status-code and attribute
2950 for (i
= 0; i
< num_statuses
; i
++)
2952 if (statuses
[i
].if_defined
&&
2953 !get_variable(vars
, statuses
[i
].if_defined
))
2956 if (statuses
[i
].if_not_defined
&&
2957 get_variable(vars
, statuses
[i
].if_not_defined
))
2960 if (response
->request
.status
.status_code
== statuses
[i
].status
)
2962 if (statuses
[i
].repeat_match
&&
2963 repeat_count
< statuses
[i
].repeat_limit
)
2966 if (statuses
[i
].define_match
)
2967 set_variable(outfile
, vars
, statuses
[i
].define_match
, "1");
2973 if (statuses
[i
].repeat_no_match
&&
2974 repeat_count
< statuses
[i
].repeat_limit
)
2977 if (statuses
[i
].define_no_match
)
2979 set_variable(outfile
, vars
, statuses
[i
].define_no_match
, "1");
2985 if (i
== num_statuses
&& num_statuses
> 0)
2987 for (i
= 0; i
< num_statuses
; i
++)
2989 if (statuses
[i
].if_defined
&&
2990 !get_variable(vars
, statuses
[i
].if_defined
))
2993 if (statuses
[i
].if_not_defined
&&
2994 get_variable(vars
, statuses
[i
].if_not_defined
))
2997 if (!statuses
[i
].repeat_match
)
2998 add_stringf(errors
, "EXPECTED: STATUS %s (got %s)",
2999 ippErrorString(statuses
[i
].status
),
3000 ippErrorString(cupsLastError()));
3003 if ((attrptr
= ippFindAttribute(response
, "status-message",
3004 IPP_TAG_TEXT
)) != NULL
)
3005 add_stringf(errors
, "status-message=\"%s\"",
3006 attrptr
->values
[0].string
.text
);
3009 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
3011 if (expect
->if_defined
&& !get_variable(vars
, expect
->if_defined
))
3014 if (expect
->if_not_defined
&&
3015 get_variable(vars
, expect
->if_not_defined
))
3018 found
= ippFindAttribute(response
, expect
->name
, IPP_TAG_ZERO
);
3022 if ((found
&& expect
->not_expect
) ||
3023 (!found
&& !(expect
->not_expect
|| expect
->optional
)) ||
3024 (found
&& !expect_matches(expect
, found
->value_tag
)) ||
3025 (found
&& expect
->in_group
&&
3026 found
->group_tag
!= expect
->in_group
))
3028 if (expect
->define_no_match
)
3029 set_variable(outfile
, vars
, expect
->define_no_match
, "1");
3030 else if (!expect
->define_match
&& !expect
->define_value
)
3032 if (found
&& expect
->not_expect
)
3033 add_stringf(errors
, "NOT EXPECTED: %s", expect
->name
);
3034 else if (!found
&& !(expect
->not_expect
|| expect
->optional
))
3035 add_stringf(errors
, "EXPECTED: %s", expect
->name
);
3038 if (!expect_matches(expect
, found
->value_tag
))
3039 add_stringf(errors
, "EXPECTED: %s OF-TYPE %s (got %s)",
3040 expect
->name
, expect
->of_type
,
3041 ippTagString(found
->value_tag
));
3043 if (expect
->in_group
&& found
->group_tag
!= expect
->in_group
)
3044 add_stringf(errors
, "EXPECTED: %s IN-GROUP %s (got %s).",
3045 expect
->name
, ippTagString(expect
->in_group
),
3046 ippTagString(found
->group_tag
));
3050 if (expect
->repeat_no_match
&&
3051 repeat_count
< expect
->repeat_limit
)
3058 ippAttributeString(found
, buffer
, sizeof(buffer
));
3060 if (found
&& expect
->with_value_from
&& !with_value_from(NULL
, ippFindAttribute(response
, expect
->with_value_from
, IPP_TAG_ZERO
), found
, buffer
, sizeof(buffer
)))
3062 if (expect
->define_no_match
)
3063 set_variable(outfile
, vars
, expect
->define_no_match
, "1");
3064 else if (!expect
->define_match
&& !expect
->define_value
&& !expect
->repeat_match
&& !expect
->repeat_no_match
)
3066 add_stringf(errors
, "EXPECTED: %s WITH-VALUES-FROM %s", expect
->name
, expect
->with_value_from
);
3068 with_value_from(errors
, ippFindAttribute(response
, expect
->with_value_from
, IPP_TAG_ZERO
), found
, buffer
, sizeof(buffer
));
3071 if (expect
->repeat_no_match
&& repeat_count
< expect
->repeat_limit
)
3076 else if (found
&& !with_value(outfile
, NULL
, expect
->with_value
, expect
->with_flags
, found
, buffer
, sizeof(buffer
)))
3078 if (expect
->define_no_match
)
3079 set_variable(outfile
, vars
, expect
->define_no_match
, "1");
3080 else if (!expect
->define_match
&& !expect
->define_value
&&
3081 !expect
->repeat_match
&& !expect
->repeat_no_match
)
3083 if (expect
->with_flags
& _CUPS_WITH_REGEX
)
3084 add_stringf(errors
, "EXPECTED: %s %s /%s/",
3086 (expect
->with_flags
& _CUPS_WITH_ALL
) ?
3087 "WITH-ALL-VALUES" : "WITH-VALUE",
3088 expect
->with_value
);
3090 add_stringf(errors
, "EXPECTED: %s %s \"%s\"",
3092 (expect
->with_flags
& _CUPS_WITH_ALL
) ?
3093 "WITH-ALL-VALUES" : "WITH-VALUE",
3094 expect
->with_value
);
3096 with_value(outfile
, errors
, expect
->with_value
, expect
->with_flags
, found
,
3097 buffer
, sizeof(buffer
));
3100 if (expect
->repeat_no_match
&&
3101 repeat_count
< expect
->repeat_limit
)
3107 if (found
&& expect
->count
> 0 &&
3108 found
->num_values
!= expect
->count
)
3110 if (expect
->define_no_match
)
3111 set_variable(outfile
, vars
, expect
->define_no_match
, "1");
3112 else if (!expect
->define_match
&& !expect
->define_value
)
3114 add_stringf(errors
, "EXPECTED: %s COUNT %d (got %d)", expect
->name
,
3115 expect
->count
, found
->num_values
);
3118 if (expect
->repeat_no_match
&&
3119 repeat_count
< expect
->repeat_limit
)
3125 if (found
&& expect
->same_count_as
)
3127 attrptr
= ippFindAttribute(response
, expect
->same_count_as
,
3130 if (!attrptr
|| attrptr
->num_values
!= found
->num_values
)
3132 if (expect
->define_no_match
)
3133 set_variable(outfile
, vars
, expect
->define_no_match
, "1");
3134 else if (!expect
->define_match
&& !expect
->define_value
)
3138 "EXPECTED: %s (%d values) SAME-COUNT-AS %s "
3139 "(not returned)", expect
->name
,
3140 found
->num_values
, expect
->same_count_as
);
3141 else if (attrptr
->num_values
!= found
->num_values
)
3143 "EXPECTED: %s (%d values) SAME-COUNT-AS %s "
3144 "(%d values)", expect
->name
, found
->num_values
,
3145 expect
->same_count_as
, attrptr
->num_values
);
3148 if (expect
->repeat_no_match
&&
3149 repeat_count
< expect
->repeat_limit
)
3156 if (found
&& expect
->define_match
)
3157 set_variable(outfile
, vars
, expect
->define_match
, "1");
3159 if (found
&& expect
->define_value
)
3161 if (!expect
->with_value
)
3163 int last
= ippGetCount(found
) - 1;
3164 /* Last element in attribute */
3166 switch (ippGetValueTag(found
))
3169 case IPP_TAG_INTEGER
:
3170 snprintf(buffer
, sizeof(buffer
), "%d", ippGetInteger(found
, last
));
3173 case IPP_TAG_BOOLEAN
:
3174 if (ippGetBoolean(found
, last
))
3175 strlcpy(buffer
, "true", sizeof(buffer
));
3177 strlcpy(buffer
, "false", sizeof(buffer
));
3180 case IPP_TAG_RESOLUTION
:
3182 int xres
, /* Horizontal resolution */
3183 yres
; /* Vertical resolution */
3184 ipp_res_t units
; /* Resolution units */
3186 xres
= ippGetResolution(found
, last
, &yres
, &units
);
3189 snprintf(buffer
, sizeof(buffer
), "%d%s", xres
, units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
3191 snprintf(buffer
, sizeof(buffer
), "%dx%d%s", xres
, yres
, units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
3195 case IPP_TAG_CHARSET
:
3196 case IPP_TAG_KEYWORD
:
3197 case IPP_TAG_LANGUAGE
:
3198 case IPP_TAG_MIMETYPE
:
3200 case IPP_TAG_NAMELANG
:
3202 case IPP_TAG_TEXTLANG
:
3204 case IPP_TAG_URISCHEME
:
3205 strlcpy(buffer
, ippGetString(found
, last
, NULL
), sizeof(buffer
));
3209 ippAttributeString(found
, buffer
, sizeof(buffer
));
3214 set_variable(outfile
, vars
, expect
->define_value
, buffer
);
3217 if (found
&& expect
->repeat_match
&&
3218 repeat_count
< expect
->repeat_limit
)
3221 while (expect
->expect_all
&& (found
= ippFindNextAttribute(response
, expect
->name
, IPP_TAG_ZERO
)) != NULL
);
3226 * If we are going to repeat this test, sleep 1 second so we don't flood
3227 * the printer with requests...
3232 if (Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
!= stdout
))
3234 printf("%04d]\n", repeat_count
);
3237 if (num_displayed
> 0)
3239 for (attrptr
= ippFirstAttribute(response
); attrptr
; attrptr
= ippNextAttribute(response
))
3241 const char *attrname
= ippGetName(attrptr
);
3244 for (i
= 0; i
< num_displayed
; i
++)
3246 if (!strcmp(displayed
[i
], attrname
))
3248 print_attr(stdout
, _CUPS_OUTPUT_TEST
, attrptr
, NULL
);
3257 sleep((unsigned)repeat_interval
);
3258 repeat_interval
= _cupsNextDelay(repeat_interval
, &repeat_prev
);
3260 if (Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
!= stdout
))
3262 printf(" %-68.68s [", name
);
3267 while (repeat_test
);
3273 if (cupsArrayCount(errors
) > 0)
3274 prev_pass
= pass
= 0;
3281 if (Output
== _CUPS_OUTPUT_PLIST
)
3283 fputs("<key>Successful</key>\n", outfile
);
3284 fputs(prev_pass
? "<true />\n" : "<false />\n", outfile
);
3285 fputs("<key>StatusCode</key>\n", outfile
);
3286 print_xml_string(outfile
, "string", ippErrorString(cupsLastError()));
3287 fputs("<key>ResponseAttributes</key>\n", outfile
);
3288 fputs("<array>\n", outfile
);
3289 fputs("<dict>\n", outfile
);
3290 for (attrptr
= response
? response
->attrs
: NULL
,
3291 group
= attrptr
? attrptr
->group_tag
: IPP_TAG_ZERO
;
3293 attrptr
= attrptr
->next
)
3294 print_attr(outfile
, Output
, attrptr
, &group
);
3295 fputs("</dict>\n", outfile
);
3296 fputs("</array>\n", outfile
);
3299 if (Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
!= stdout
))
3301 puts(prev_pass
? "PASS]" : "FAIL]");
3303 if (!prev_pass
|| (Verbosity
&& response
))
3305 printf(" RECEIVED: %lu bytes in response\n",
3306 (unsigned long)ippLength(response
));
3307 printf(" status-code = %s (%s)\n", ippErrorString(cupsLastError()),
3308 cupsLastErrorString());
3310 if (Verbosity
&& response
)
3312 for (attrptr
= response
->attrs
;
3314 attrptr
= attrptr
->next
)
3315 print_attr(stdout
, _CUPS_OUTPUT_TEST
, attrptr
, NULL
);
3319 else if (!prev_pass
&& Output
!= _CUPS_OUTPUT_QUIET
)
3320 fprintf(stderr
, "%s\n", cupsLastErrorString());
3322 if (prev_pass
&& Output
>= _CUPS_OUTPUT_LIST
&& !Verbosity
&&
3325 size_t width
; /* Length of value */
3327 for (i
= 0; i
< num_displayed
; i
++)
3329 widths
[i
] = strlen(displayed
[i
]);
3331 for (attrptr
= ippFindAttribute(response
, displayed
[i
], IPP_TAG_ZERO
);
3333 attrptr
= ippFindNextAttribute(response
, displayed
[i
],
3336 width
= ippAttributeString(attrptr
, NULL
, 0);
3337 if (width
> widths
[i
])
3342 if (Output
== _CUPS_OUTPUT_CSV
)
3343 print_csv(outfile
, NULL
, num_displayed
, displayed
, widths
);
3345 print_line(outfile
, NULL
, num_displayed
, displayed
, widths
);
3347 attrptr
= response
->attrs
;
3351 while (attrptr
&& attrptr
->group_tag
<= IPP_TAG_OPERATION
)
3352 attrptr
= attrptr
->next
;
3356 if (Output
== _CUPS_OUTPUT_CSV
)
3357 print_csv(outfile
, attrptr
, num_displayed
, displayed
, widths
);
3359 print_line(outfile
, attrptr
, num_displayed
, displayed
, widths
);
3361 while (attrptr
&& attrptr
->group_tag
> IPP_TAG_OPERATION
)
3362 attrptr
= attrptr
->next
;
3366 else if (!prev_pass
)
3368 if (Output
== _CUPS_OUTPUT_PLIST
)
3370 fputs("<key>Errors</key>\n", outfile
);
3371 fputs("<array>\n", outfile
);
3373 for (error
= (char *)cupsArrayFirst(errors
);
3375 error
= (char *)cupsArrayNext(errors
))
3376 print_xml_string(outfile
, "string", error
);
3378 fputs("</array>\n", outfile
);
3381 if (Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
!= stdout
))
3383 for (error
= (char *)cupsArrayFirst(errors
);
3385 error
= (char *)cupsArrayNext(errors
))
3386 printf(" %s\n", error
);
3390 if (num_displayed
> 0 && !Verbosity
&& response
&& (Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
!= stdout
)))
3392 for (attrptr
= response
->attrs
;
3394 attrptr
= attrptr
->next
)
3398 for (i
= 0; i
< num_displayed
; i
++)
3400 if (!strcmp(displayed
[i
], attrptr
->name
))
3402 print_attr(outfile
, Output
, attrptr
, NULL
);
3412 if (Output
== _CUPS_OUTPUT_PLIST
)
3413 fputs("</dict>\n", outfile
);
3417 ippDelete(response
);
3420 for (i
= 0; i
< num_statuses
; i
++)
3422 if (statuses
[i
].if_defined
)
3423 free(statuses
[i
].if_defined
);
3424 if (statuses
[i
].if_not_defined
)
3425 free(statuses
[i
].if_not_defined
);
3426 if (statuses
[i
].define_match
)
3427 free(statuses
[i
].define_match
);
3428 if (statuses
[i
].define_no_match
)
3429 free(statuses
[i
].define_no_match
);
3433 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
3436 if (expect
->of_type
)
3437 free(expect
->of_type
);
3438 if (expect
->same_count_as
)
3439 free(expect
->same_count_as
);
3440 if (expect
->if_defined
)
3441 free(expect
->if_defined
);
3442 if (expect
->if_not_defined
)
3443 free(expect
->if_not_defined
);
3444 if (expect
->with_value
)
3445 free(expect
->with_value
);
3446 if (expect
->define_match
)
3447 free(expect
->define_match
);
3448 if (expect
->define_no_match
)
3449 free(expect
->define_no_match
);
3450 if (expect
->define_value
)
3451 free(expect
->define_value
);
3455 for (i
= 0; i
< num_displayed
; i
++)
3459 if (!ignore_errors
&& !prev_pass
)
3465 cupsArrayDelete(errors
);
3472 ippDelete(response
);
3474 for (i
= 0; i
< num_statuses
; i
++)
3476 if (statuses
[i
].if_defined
)
3477 free(statuses
[i
].if_defined
);
3478 if (statuses
[i
].if_not_defined
)
3479 free(statuses
[i
].if_not_defined
);
3480 if (statuses
[i
].define_match
)
3481 free(statuses
[i
].define_match
);
3482 if (statuses
[i
].define_no_match
)
3483 free(statuses
[i
].define_no_match
);
3486 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
3489 if (expect
->of_type
)
3490 free(expect
->of_type
);
3491 if (expect
->same_count_as
)
3492 free(expect
->same_count_as
);
3493 if (expect
->if_defined
)
3494 free(expect
->if_defined
);
3495 if (expect
->if_not_defined
)
3496 free(expect
->if_not_defined
);
3497 if (expect
->with_value
)
3498 free(expect
->with_value
);
3499 if (expect
->define_match
)
3500 free(expect
->define_match
);
3501 if (expect
->define_no_match
)
3502 free(expect
->define_no_match
);
3503 if (expect
->define_value
)
3504 free(expect
->define_value
);
3507 for (i
= 0; i
< num_displayed
; i
++)
3515 * 'expand_variables()' - Expand variables in a string.
3519 expand_variables(_cups_vars_t
*vars
, /* I - Variables */
3520 char *dst
, /* I - Destination string buffer */
3521 const char *src
, /* I - Source string */
3522 size_t dstsize
) /* I - Size of destination buffer */
3524 char *dstptr
, /* Pointer into destination */
3525 *dstend
, /* End of destination */
3526 temp
[256], /* Temporary string */
3527 *tempptr
; /* Pointer into temporary string */
3528 const char *value
; /* Value to substitute */
3532 dstend
= dst
+ dstsize
- 1;
3534 while (*src
&& dstptr
< dstend
)
3539 * Substitute a string/number...
3542 if (!strncmp(src
, "$$", 2))
3547 else if (!strncmp(src
, "$ENV[", 5))
3549 strlcpy(temp
, src
+ 5, sizeof(temp
));
3551 for (tempptr
= temp
; *tempptr
; tempptr
++)
3552 if (*tempptr
== ']')
3558 value
= getenv(temp
);
3559 src
+= tempptr
- temp
+ 5;
3566 strlcpy(temp
, src
, sizeof(temp
));
3567 if ((tempptr
= strchr(temp
, '}')) != NULL
)
3570 tempptr
= temp
+ strlen(temp
);
3574 strlcpy(temp
, src
+ 1, sizeof(temp
));
3576 for (tempptr
= temp
; *tempptr
; tempptr
++)
3577 if (!isalnum(*tempptr
& 255) && *tempptr
!= '-' && *tempptr
!= '_')
3584 if (!strcmp(temp
, "uri"))
3586 else if (!strcmp(temp
, "filename"))
3587 value
= vars
->filename
;
3588 else if (!strcmp(temp
, "scheme") || !strcmp(temp
, "method"))
3589 value
= vars
->scheme
;
3590 else if (!strcmp(temp
, "username"))
3591 value
= vars
->userpass
;
3592 else if (!strcmp(temp
, "hostname"))
3593 value
= vars
->hostname
;
3594 else if (!strcmp(temp
, "port"))
3596 snprintf(temp
, sizeof(temp
), "%d", vars
->port
);
3599 else if (!strcmp(temp
, "resource"))
3600 value
= vars
->resource
;
3601 else if (!strcmp(temp
, "user"))
3604 value
= get_variable(vars
, temp
);
3606 src
+= tempptr
- temp
+ 1;
3611 strlcpy(dstptr
, value
, (size_t)(dstend
- dstptr
+ 1));
3612 dstptr
+= strlen(dstptr
);
3624 * 'expect_matches()' - Return true if the tag matches the specification.
3627 static int /* O - 1 if matches, 0 otherwise */
3629 _cups_expect_t
*expect
, /* I - Expected attribute */
3630 ipp_tag_t value_tag
) /* I - Value tag for attribute */
3632 int match
; /* Match? */
3633 char *of_type
, /* Type name to match */
3634 *next
, /* Next name to match */
3635 sep
; /* Separator character */
3639 * If we don't expect a particular type, return immediately...
3642 if (!expect
->of_type
)
3646 * Parse the "of_type" value since the string can contain multiple attribute
3647 * types separated by "," or "|"...
3650 for (of_type
= expect
->of_type
, match
= 0; !match
&& *of_type
; of_type
= next
)
3653 * Find the next separator, and set it (temporarily) to nul if present.
3656 for (next
= of_type
; *next
&& *next
!= '|' && *next
!= ','; next
++);
3658 if ((sep
= *next
) != '\0')
3662 * Support some meta-types to make it easier to write the test file.
3665 if (!strcmp(of_type
, "text"))
3666 match
= value_tag
== IPP_TAG_TEXTLANG
|| value_tag
== IPP_TAG_TEXT
;
3667 else if (!strcmp(of_type
, "name"))
3668 match
= value_tag
== IPP_TAG_NAMELANG
|| value_tag
== IPP_TAG_NAME
;
3669 else if (!strcmp(of_type
, "collection"))
3670 match
= value_tag
== IPP_TAG_BEGIN_COLLECTION
;
3672 match
= value_tag
== ippTagValue(of_type
);
3675 * Restore the separator if we have one...
3687 * 'get_collection()' - Get a collection value from the current test file.
3690 static ipp_t
* /* O - Collection value */
3691 get_collection(FILE *outfile
, /* I - Output file */
3692 _cups_vars_t
*vars
, /* I - Variables */
3693 FILE *fp
, /* I - File to read from */
3694 int *linenum
) /* IO - Line number */
3696 char token
[1024], /* Token from file */
3697 temp
[1024], /* Temporary string */
3698 attr
[128]; /* Attribute name */
3699 ipp_tag_t value
; /* Current value type */
3700 ipp_t
*col
= ippNew(); /* Collection value */
3701 ipp_attribute_t
*lastcol
= NULL
; /* Last collection attribute */
3704 while (get_token(fp
, token
, sizeof(token
), linenum
) != NULL
)
3706 if (!strcmp(token
, "}"))
3708 else if (!strcmp(token
, "{") && lastcol
)
3711 * Another collection value
3714 ipp_t
*subcol
= get_collection(outfile
, vars
, fp
, linenum
);
3715 /* Collection value */
3718 ippSetCollection(col
, &lastcol
, ippGetCount(lastcol
), subcol
);
3722 else if (!_cups_strcasecmp(token
, "MEMBER"))
3730 if (!get_token(fp
, token
, sizeof(token
), linenum
))
3732 print_fatal_error(outfile
, "Missing MEMBER value tag on line %d.", *linenum
);
3736 if ((value
= ippTagValue(token
)) == IPP_TAG_ZERO
)
3738 print_fatal_error(outfile
, "Bad MEMBER value tag \"%s\" on line %d.", token
,
3743 if (!get_token(fp
, attr
, sizeof(attr
), linenum
))
3745 print_fatal_error(outfile
, "Missing MEMBER name on line %d.", *linenum
);
3749 if (!get_token(fp
, temp
, sizeof(temp
), linenum
))
3751 print_fatal_error(outfile
, "Missing MEMBER value on line %d.", *linenum
);
3755 expand_variables(vars
, token
, temp
, sizeof(token
));
3759 case IPP_TAG_BOOLEAN
:
3760 if (!_cups_strcasecmp(token
, "true"))
3761 ippAddBoolean(col
, IPP_TAG_ZERO
, attr
, 1);
3763 ippAddBoolean(col
, IPP_TAG_ZERO
, attr
, (char)atoi(token
));
3766 case IPP_TAG_INTEGER
:
3768 ippAddInteger(col
, IPP_TAG_ZERO
, value
, attr
, atoi(token
));
3771 case IPP_TAG_RESOLUTION
:
3773 int xres
, /* X resolution */
3774 yres
; /* Y resolution */
3775 char units
[6]; /* Units */
3777 if (sscanf(token
, "%dx%d%5s", &xres
, &yres
, units
) != 3 ||
3778 (_cups_strcasecmp(units
, "dpi") &&
3779 _cups_strcasecmp(units
, "dpc") &&
3780 _cups_strcasecmp(units
, "dpcm") &&
3781 _cups_strcasecmp(units
, "other")))
3783 print_fatal_error(outfile
, "Bad resolution value \"%s\" on line %d.",
3788 if (!_cups_strcasecmp(units
, "dpi"))
3789 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, IPP_RES_PER_INCH
, xres
, yres
);
3790 else if (!_cups_strcasecmp(units
, "dpc") ||
3791 !_cups_strcasecmp(units
, "dpcm"))
3792 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, IPP_RES_PER_CM
, xres
, yres
);
3794 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, (ipp_res_t
)0, xres
, yres
);
3798 case IPP_TAG_RANGE
:
3800 int lowers
[4], /* Lower value */
3801 uppers
[4], /* Upper values */
3802 num_vals
; /* Number of values */
3805 num_vals
= sscanf(token
, "%d-%d,%d-%d,%d-%d,%d-%d",
3806 lowers
+ 0, uppers
+ 0,
3807 lowers
+ 1, uppers
+ 1,
3808 lowers
+ 2, uppers
+ 2,
3809 lowers
+ 3, uppers
+ 3);
3811 if ((num_vals
& 1) || num_vals
== 0)
3813 print_fatal_error(outfile
, "Bad rangeOfInteger value \"%s\" on line %d.",
3818 ippAddRanges(col
, IPP_TAG_ZERO
, attr
, num_vals
/ 2, lowers
,
3823 case IPP_TAG_BEGIN_COLLECTION
:
3824 if (!strcmp(token
, "{"))
3826 ipp_t
*subcol
= get_collection(outfile
, vars
, fp
, linenum
);
3827 /* Collection value */
3831 lastcol
= ippAddCollection(col
, IPP_TAG_ZERO
, attr
, subcol
);
3839 print_fatal_error(outfile
, "Bad collection value on line %d.", *linenum
);
3843 case IPP_TAG_STRING
:
3844 ippAddOctetString(col
, IPP_TAG_ZERO
, attr
, token
, (int)strlen(token
));
3848 if (!strchr(token
, ','))
3849 ippAddString(col
, IPP_TAG_ZERO
, value
, attr
, NULL
, token
);
3853 * Multiple string values...
3856 int num_values
; /* Number of values */
3857 char *values
[100], /* Values */
3858 *ptr
; /* Pointer to next value */
3864 for (ptr
= strchr(token
, ','); ptr
; ptr
= strchr(ptr
, ','))
3867 values
[num_values
] = ptr
;
3871 ippAddStrings(col
, IPP_TAG_ZERO
, value
, attr
, num_values
,
3872 NULL
, (const char **)values
);
3879 print_fatal_error(outfile
, "Unexpected token %s seen on line %d.", token
, *linenum
);
3887 * If we get here there was a parse error; free memory and return.
3899 * 'get_filename()' - Get a filename based on the current test file.
3902 static char * /* O - Filename */
3903 get_filename(const char *testfile
, /* I - Current test file */
3904 char *dst
, /* I - Destination filename */
3905 const char *src
, /* I - Source filename */
3906 size_t dstsize
) /* I - Size of destination buffer */
3908 char *dstptr
; /* Pointer into destination */
3909 _cups_globals_t
*cg
= _cupsGlobals();
3913 if (*src
== '<' && src
[strlen(src
) - 1] == '>')
3916 * Map <filename> to CUPS_DATADIR/ipptool/filename...
3919 snprintf(dst
, dstsize
, "%s/ipptool/%s", cg
->cups_datadir
, src
+ 1);
3920 dstptr
= dst
+ strlen(dst
) - 1;
3924 else if (*src
== '/' || !strchr(testfile
, '/')
3926 || (isalpha(*src
& 255) && src
[1] == ':')
3931 * Use the path as-is...
3934 strlcpy(dst
, src
, dstsize
);
3939 * Make path relative to testfile...
3942 strlcpy(dst
, testfile
, dstsize
);
3943 if ((dstptr
= strrchr(dst
, '/')) != NULL
)
3946 dstptr
= dst
; /* Should never happen */
3948 strlcpy(dstptr
, src
, dstsize
- (size_t)(dstptr
- dst
));
3956 * 'get_string()' - Get a pointer to a string value or the portion of interest.
3959 static const char * /* O - Pointer to string */
3960 get_string(ipp_attribute_t
*attr
, /* I - IPP attribute */
3961 int element
, /* I - Element to fetch */
3962 int flags
, /* I - Value ("with") flags */
3963 char *buffer
, /* I - Temporary buffer */
3964 size_t bufsize
) /* I - Size of temporary buffer */
3966 const char *value
; /* Value */
3967 char *ptr
, /* Pointer into value */
3968 scheme
[256], /* URI scheme */
3969 userpass
[256], /* Username/password */
3970 hostname
[256], /* Hostname */
3971 resource
[1024]; /* Resource */
3972 int port
; /* Port number */
3975 value
= ippGetString(attr
, element
, NULL
);
3977 if (flags
& _CUPS_WITH_HOSTNAME
)
3979 if (httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), buffer
, (int)bufsize
, &port
, resource
, sizeof(resource
)) < HTTP_URI_STATUS_OK
)
3982 ptr
= buffer
+ strlen(buffer
) - 1;
3983 if (ptr
>= buffer
&& *ptr
== '.')
3984 *ptr
= '\0'; /* Drop trailing "." */
3988 else if (flags
& _CUPS_WITH_RESOURCE
)
3990 if (httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), hostname
, sizeof(hostname
), &port
, buffer
, (int)bufsize
) < HTTP_URI_STATUS_OK
)
3995 else if (flags
& _CUPS_WITH_SCHEME
)
3997 if (httpSeparateURI(HTTP_URI_CODING_ALL
, value
, buffer
, (int)bufsize
, userpass
, sizeof(userpass
), hostname
, sizeof(hostname
), &port
, resource
, sizeof(resource
)) < HTTP_URI_STATUS_OK
)
4002 else if (ippGetValueTag(attr
) == IPP_TAG_URI
&& (!strncmp(value
, "ipp://", 6) || !strncmp(value
, "http://", 7) || !strncmp(value
, "ipps://", 7) || !strncmp(value
, "https://", 8)))
4004 http_uri_status_t status
= httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), hostname
, sizeof(hostname
), &port
, resource
, sizeof(resource
));
4006 if (status
< HTTP_URI_STATUS_OK
)
4017 * Normalize URI with no trailing dot...
4020 if ((ptr
= hostname
+ strlen(hostname
) - 1) >= hostname
&& *ptr
== '.')
4023 httpAssembleURI(HTTP_URI_CODING_ALL
, buffer
, (int)bufsize
, scheme
, userpass
, hostname
, port
, resource
);
4034 * 'get_token()' - Get a token from a file.
4037 static char * /* O - Token from file or NULL on EOF */
4038 get_token(FILE *fp
, /* I - File to read from */
4039 char *buf
, /* I - Buffer to read into */
4040 int buflen
, /* I - Length of buffer */
4041 int *linenum
) /* IO - Current line number */
4043 int ch
, /* Character from file */
4044 quote
; /* Quoting character */
4045 char *bufptr
, /* Pointer into buffer */
4046 *bufend
; /* End of buffer */
4052 * Skip whitespace...
4055 while (isspace(ch
= getc(fp
)))
4067 else if (ch
== '\'' || ch
== '\"')
4070 * Quoted text or regular expression...
4075 bufend
= buf
+ buflen
- 1;
4077 while ((ch
= getc(fp
)) != EOF
)
4082 * Escape next character...
4085 if (bufptr
< bufend
)
4086 *bufptr
++ = (char)ch
;
4088 if ((ch
= getc(fp
)) != EOF
&& bufptr
< bufend
)
4089 *bufptr
++ = (char)ch
;
4091 else if (ch
== quote
)
4093 else if (bufptr
< bufend
)
4094 *bufptr
++ = (char)ch
;
4107 while ((ch
= getc(fp
)) != EOF
)
4113 else if (ch
== '{' || ch
== '}' || ch
== ',')
4123 * Whitespace delimited text...
4129 bufend
= buf
+ buflen
- 1;
4131 while ((ch
= getc(fp
)) != EOF
)
4132 if (isspace(ch
) || ch
== '#')
4134 else if (bufptr
< bufend
)
4135 *bufptr
++ = (char)ch
;
4139 else if (ch
== '\n')
4151 * 'get_variable()' - Get the value of a variable.
4154 static char * /* O - Value or NULL */
4155 get_variable(_cups_vars_t
*vars
, /* I - Variables */
4156 const char *name
) /* I - Variable name */
4158 _cups_var_t key
, /* Search key */
4159 *match
; /* Matching variable, if any */
4162 key
.name
= (char *)name
;
4163 match
= cupsArrayFind(vars
->vars
, &key
);
4165 return (match
? match
->value
: NULL
);
4170 * 'iso_date()' - Return an ISO 8601 date/time string for the given IPP dateTime
4174 static char * /* O - ISO 8601 date/time string */
4175 iso_date(ipp_uchar_t
*date
) /* I - IPP (RFC 1903) date/time value */
4177 time_t utctime
; /* UTC time since 1970 */
4178 struct tm
*utcdate
; /* UTC date/time */
4179 static char buffer
[255]; /* String buffer */
4182 utctime
= ippDateToTime(date
);
4183 utcdate
= gmtime(&utctime
);
4185 snprintf(buffer
, sizeof(buffer
), "%04d-%02d-%02dT%02d:%02d:%02dZ",
4186 utcdate
->tm_year
+ 1900, utcdate
->tm_mon
+ 1, utcdate
->tm_mday
,
4187 utcdate
->tm_hour
, utcdate
->tm_min
, utcdate
->tm_sec
);
4194 * 'password_cb()' - Password callback for authenticated tests.
4197 static const char * /* O - Password */
4198 password_cb(const char *prompt
) /* I - Prompt (unused) */
4202 if (PasswordTries
< 3)
4206 cupsSetUser(Username
);
4216 * 'pause_message()' - Display the message and pause until the user presses a key.
4220 pause_message(const char *message
) /* I - Message */
4223 HANDLE tty
; /* Console handle */
4224 DWORD mode
; /* Console mode */
4225 char key
; /* Key press */
4226 DWORD bytes
; /* Bytes read for key press */
4230 * Disable input echo and set raw input...
4233 if ((tty
= GetStdHandle(STD_INPUT_HANDLE
)) == INVALID_HANDLE_VALUE
)
4236 if (!GetConsoleMode(tty
, &mode
))
4239 if (!SetConsoleMode(tty
, 0))
4243 int tty
; /* /dev/tty - never read from stdin */
4244 struct termios original
, /* Original input mode */
4245 noecho
; /* No echo input mode */
4246 char key
; /* Current key press */
4250 * Disable input echo and set raw input...
4253 if ((tty
= open("/dev/tty", O_RDONLY
)) < 0)
4256 if (tcgetattr(tty
, &original
))
4263 noecho
.c_lflag
&= (tcflag_t
)~(ICANON
| ECHO
| ECHOE
| ISIG
);
4265 if (tcsetattr(tty
, TCSAFLUSH
, &noecho
))
4273 * Display the prompt...
4276 printf("%s\n---- PRESS ANY KEY ----", message
);
4284 ReadFile(tty
, &key
, 1, &bytes
, NULL
);
4290 SetConsoleMode(tty
, mode
);
4303 tcsetattr(tty
, TCSAFLUSH
, &original
);
4308 * Erase the "press any key" prompt...
4311 fputs("\r \r", stdout
);
4317 * 'print_attr()' - Print an attribute on the screen.
4321 print_attr(FILE *outfile
, /* I - Output file */
4322 int format
, /* I - Output format */
4323 ipp_attribute_t
*attr
, /* I - Attribute to print */
4324 ipp_tag_t
*group
) /* IO - Current group */
4326 int i
; /* Looping var */
4327 ipp_attribute_t
*colattr
; /* Collection attribute */
4330 if (format
== _CUPS_OUTPUT_PLIST
)
4332 if (!attr
->name
|| (group
&& *group
!= attr
->group_tag
))
4334 if (attr
->group_tag
!= IPP_TAG_ZERO
)
4336 fputs("</dict>\n", outfile
);
4337 fputs("<dict>\n", outfile
);
4341 *group
= attr
->group_tag
;
4347 print_xml_string(outfile
, "key", attr
->name
);
4348 if (attr
->num_values
> 1)
4349 fputs("<array>\n", outfile
);
4351 switch (attr
->value_tag
)
4353 case IPP_TAG_INTEGER
:
4355 for (i
= 0; i
< attr
->num_values
; i
++)
4356 fprintf(outfile
, "<integer>%d</integer>\n", attr
->values
[i
].integer
);
4359 case IPP_TAG_BOOLEAN
:
4360 for (i
= 0; i
< attr
->num_values
; i
++)
4361 fputs(attr
->values
[i
].boolean
? "<true />\n" : "<false />\n", outfile
);
4364 case IPP_TAG_RANGE
:
4365 for (i
= 0; i
< attr
->num_values
; i
++)
4366 fprintf(outfile
, "<dict><key>lower</key><integer>%d</integer>"
4367 "<key>upper</key><integer>%d</integer></dict>\n",
4368 attr
->values
[i
].range
.lower
, attr
->values
[i
].range
.upper
);
4371 case IPP_TAG_RESOLUTION
:
4372 for (i
= 0; i
< attr
->num_values
; i
++)
4373 fprintf(outfile
, "<dict><key>xres</key><integer>%d</integer>"
4374 "<key>yres</key><integer>%d</integer>"
4375 "<key>units</key><string>%s</string></dict>\n",
4376 attr
->values
[i
].resolution
.xres
,
4377 attr
->values
[i
].resolution
.yres
,
4378 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
4383 for (i
= 0; i
< attr
->num_values
; i
++)
4384 fprintf(outfile
, "<date>%s</date>\n", iso_date(attr
->values
[i
].date
));
4387 case IPP_TAG_STRING
:
4388 for (i
= 0; i
< attr
->num_values
; i
++)
4390 char buffer
[IPP_MAX_LENGTH
* 5 / 4 + 1];
4393 fprintf(outfile
, "<data>%s</data>\n",
4394 httpEncode64_2(buffer
, sizeof(buffer
),
4395 attr
->values
[i
].unknown
.data
,
4396 attr
->values
[i
].unknown
.length
));
4402 case IPP_TAG_KEYWORD
:
4403 case IPP_TAG_CHARSET
:
4405 case IPP_TAG_MIMETYPE
:
4406 case IPP_TAG_LANGUAGE
:
4407 for (i
= 0; i
< attr
->num_values
; i
++)
4408 print_xml_string(outfile
, "string", attr
->values
[i
].string
.text
);
4411 case IPP_TAG_TEXTLANG
:
4412 case IPP_TAG_NAMELANG
:
4413 for (i
= 0; i
< attr
->num_values
; i
++)
4415 fputs("<dict><key>language</key><string>", outfile
);
4416 print_xml_string(outfile
, NULL
, attr
->values
[i
].string
.language
);
4417 fputs("</string><key>string</key><string>", outfile
);
4418 print_xml_string(outfile
, NULL
, attr
->values
[i
].string
.text
);
4419 fputs("</string></dict>\n", outfile
);
4423 case IPP_TAG_BEGIN_COLLECTION
:
4424 for (i
= 0; i
< attr
->num_values
; i
++)
4426 fputs("<dict>\n", outfile
);
4427 for (colattr
= attr
->values
[i
].collection
->attrs
;
4429 colattr
= colattr
->next
)
4430 print_attr(outfile
, format
, colattr
, NULL
);
4431 fputs("</dict>\n", outfile
);
4436 fprintf(outfile
, "<string><<%s>></string>\n", ippTagString(attr
->value_tag
));
4440 if (attr
->num_values
> 1)
4441 fputs("</array>\n", outfile
);
4445 char buffer
[8192]; /* Value buffer */
4447 if (format
== _CUPS_OUTPUT_TEST
)
4451 fputs(" -- separator --\n", outfile
);
4455 fprintf(outfile
, " %s (%s%s) = ", attr
->name
, attr
->num_values
> 1 ? "1setOf " : "", ippTagString(attr
->value_tag
));
4458 ippAttributeString(attr
, buffer
, sizeof(buffer
));
4459 fprintf(outfile
, "%s\n", buffer
);
4465 * 'print_csv()' - Print a line of CSV text.
4470 FILE *outfile
, /* I - Output file */
4471 ipp_attribute_t
*attr
, /* I - First attribute for line */
4472 int num_displayed
, /* I - Number of attributes to display */
4473 char **displayed
, /* I - Attributes to display */
4474 size_t *widths
) /* I - Column widths */
4476 int i
; /* Looping var */
4477 size_t maxlength
; /* Max length of all columns */
4478 char *buffer
, /* String buffer */
4479 *bufptr
; /* Pointer into buffer */
4480 ipp_attribute_t
*current
; /* Current attribute */
4484 * Get the maximum string length we have to show and allocate...
4487 for (i
= 1, maxlength
= widths
[0]; i
< num_displayed
; i
++)
4488 if (widths
[i
] > maxlength
)
4489 maxlength
= widths
[i
];
4493 if ((buffer
= malloc(maxlength
)) == NULL
)
4497 * Loop through the attributes to display...
4502 for (i
= 0; i
< num_displayed
; i
++)
4505 fputc(',', outfile
);
4509 for (current
= attr
; current
; current
= current
->next
)
4513 else if (!strcmp(current
->name
, displayed
[i
]))
4515 ippAttributeString(current
, buffer
, maxlength
);
4520 if (strchr(buffer
, ',') != NULL
|| strchr(buffer
, '\"') != NULL
||
4521 strchr(buffer
, '\\') != NULL
)
4523 putc('\"', outfile
);
4524 for (bufptr
= buffer
; *bufptr
; bufptr
++)
4526 if (*bufptr
== '\\' || *bufptr
== '\"')
4527 putc('\\', outfile
);
4528 putc(*bufptr
, outfile
);
4530 putc('\"', outfile
);
4533 fputs(buffer
, outfile
);
4535 putc('\n', outfile
);
4539 for (i
= 0; i
< num_displayed
; i
++)
4544 fputs(displayed
[i
], outfile
);
4546 putc('\n', outfile
);
4554 * 'print_fatal_error()' - Print a fatal error message.
4558 print_fatal_error(FILE *outfile
, /* I - Output file */
4559 const char *s
, /* I - Printf-style format string */
4560 ...) /* I - Additional arguments as needed */
4562 char buffer
[10240]; /* Format buffer */
4563 va_list ap
; /* Pointer to arguments */
4567 * Format the error message...
4571 vsnprintf(buffer
, sizeof(buffer
), s
, ap
);
4578 if (Output
== _CUPS_OUTPUT_PLIST
)
4580 print_xml_header(outfile
);
4581 print_xml_trailer(outfile
, 0, buffer
);
4584 _cupsLangPrintf(stderr
, "ipptool: %s", buffer
);
4589 * 'print_line()' - Print a line of formatted or CSV text.
4594 FILE *outfile
, /* I - Output file */
4595 ipp_attribute_t
*attr
, /* I - First attribute for line */
4596 int num_displayed
, /* I - Number of attributes to display */
4597 char **displayed
, /* I - Attributes to display */
4598 size_t *widths
) /* I - Column widths */
4600 int i
; /* Looping var */
4601 size_t maxlength
; /* Max length of all columns */
4602 char *buffer
; /* String buffer */
4603 ipp_attribute_t
*current
; /* Current attribute */
4607 * Get the maximum string length we have to show and allocate...
4610 for (i
= 1, maxlength
= widths
[0]; i
< num_displayed
; i
++)
4611 if (widths
[i
] > maxlength
)
4612 maxlength
= widths
[i
];
4616 if ((buffer
= malloc(maxlength
)) == NULL
)
4620 * Loop through the attributes to display...
4625 for (i
= 0; i
< num_displayed
; i
++)
4632 for (current
= attr
; current
; current
= current
->next
)
4636 else if (!strcmp(current
->name
, displayed
[i
]))
4638 ippAttributeString(current
, buffer
, maxlength
);
4643 fprintf(outfile
, "%*s", (int)-widths
[i
], buffer
);
4645 putc('\n', outfile
);
4649 for (i
= 0; i
< num_displayed
; i
++)
4654 fprintf(outfile
, "%*s", (int)-widths
[i
], displayed
[i
]);
4656 putc('\n', outfile
);
4658 for (i
= 0; i
< num_displayed
; i
++)
4663 memset(buffer
, '-', widths
[i
]);
4664 buffer
[widths
[i
]] = '\0';
4665 fputs(buffer
, outfile
);
4667 putc('\n', outfile
);
4675 * 'print_xml_header()' - Print a standard XML plist header.
4679 print_xml_header(FILE *outfile
) /* I - Output file */
4683 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", outfile
);
4684 fputs("<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
4685 "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n", outfile
);
4686 fputs("<plist version=\"1.0\">\n", outfile
);
4687 fputs("<dict>\n", outfile
);
4688 fputs("<key>ipptoolVersion</key>\n", outfile
);
4689 fputs("<string>" CUPS_SVERSION
"</string>\n", outfile
);
4690 fputs("<key>Transfer</key>\n", outfile
);
4691 fprintf(outfile
, "<string>%s</string>\n",
4692 Transfer
== _CUPS_TRANSFER_AUTO
? "auto" :
4693 Transfer
== _CUPS_TRANSFER_CHUNKED
? "chunked" : "length");
4694 fputs("<key>Tests</key>\n", outfile
);
4695 fputs("<array>\n", outfile
);
4703 * 'print_xml_string()' - Print an XML string with escaping.
4707 print_xml_string(FILE *outfile
, /* I - Output file */
4708 const char *element
, /* I - Element name or NULL */
4709 const char *s
) /* I - String to print */
4712 fprintf(outfile
, "<%s>", element
);
4717 fputs("&", outfile
);
4719 fputs("<", outfile
);
4721 fputs(">", outfile
);
4722 else if ((*s
& 0xe0) == 0xc0)
4725 * Validate UTF-8 two-byte sequence...
4728 if ((s
[1] & 0xc0) != 0x80)
4735 putc(*s
++, outfile
);
4739 else if ((*s
& 0xf0) == 0xe0)
4742 * Validate UTF-8 three-byte sequence...
4745 if ((s
[1] & 0xc0) != 0x80 || (s
[2] & 0xc0) != 0x80)
4752 putc(*s
++, outfile
);
4753 putc(*s
++, outfile
);
4757 else if ((*s
& 0xf8) == 0xf0)
4760 * Validate UTF-8 four-byte sequence...
4763 if ((s
[1] & 0xc0) != 0x80 || (s
[2] & 0xc0) != 0x80 ||
4764 (s
[3] & 0xc0) != 0x80)
4771 putc(*s
++, outfile
);
4772 putc(*s
++, outfile
);
4773 putc(*s
++, outfile
);
4777 else if ((*s
& 0x80) || (*s
< ' ' && !isspace(*s
& 255)))
4780 * Invalid control character...
4792 fprintf(outfile
, "</%s>\n", element
);
4797 * 'print_xml_trailer()' - Print the XML trailer with success/fail value.
4801 print_xml_trailer(FILE *outfile
, /* I - Output file */
4802 int success
, /* I - 1 on success, 0 on failure */
4803 const char *message
) /* I - Error message or NULL */
4807 fputs("</array>\n", outfile
);
4808 fputs("<key>Successful</key>\n", outfile
);
4809 fputs(success
? "<true />\n" : "<false />\n", outfile
);
4812 fputs("<key>ErrorMessage</key>\n", outfile
);
4813 print_xml_string(outfile
, "string", message
);
4815 fputs("</dict>\n", outfile
);
4816 fputs("</plist>\n", outfile
);
4824 * 'set_variable()' - Set a variable value.
4828 set_variable(FILE *outfile
, /* I - Output file */
4829 _cups_vars_t
*vars
, /* I - Variables */
4830 const char *name
, /* I - Variable name */
4831 const char *value
) /* I - Value string */
4833 _cups_var_t key
, /* Search key */
4834 *var
; /* New variable */
4837 if (!_cups_strcasecmp(name
, "filename"))
4840 free(vars
->filename
);
4842 vars
->filename
= strdup(value
);
4846 key
.name
= (char *)name
;
4847 if ((var
= cupsArrayFind(vars
->vars
, &key
)) != NULL
)
4850 var
->value
= strdup(value
);
4852 else if ((var
= malloc(sizeof(_cups_var_t
))) == NULL
)
4854 print_fatal_error(outfile
, "Unable to allocate memory for variable \"%s\".", name
);
4859 var
->name
= strdup(name
);
4860 var
->value
= strdup(value
);
4862 cupsArrayAdd(vars
->vars
, var
);
4869 * 'sigterm_handler()' - Handle SIGINT and SIGTERM.
4873 sigterm_handler(int sig
) /* I - Signal number (unused) */
4879 signal(SIGINT
, SIG_DFL
);
4880 signal(SIGTERM
, SIG_DFL
);
4886 * 'timeout_cb()' - Handle HTTP timeouts.
4889 static int /* O - 1 to continue, 0 to cancel */
4890 timeout_cb(http_t
*http
, /* I - Connection to server */
4891 void *user_data
) /* I - User data (unused) */
4893 int buffered
= 0; /* Bytes buffered but not yet sent */
4899 * If the socket still have data waiting to be sent to the printer (as can
4900 * happen if the printer runs out of paper), continue to wait until the output
4901 * buffer is empty...
4904 #ifdef SO_NWRITE /* OS X and some versions of Linux */
4905 socklen_t len
= sizeof(buffered
); /* Size of return value */
4907 if (getsockopt(httpGetFd(http
), SOL_SOCKET
, SO_NWRITE
, &buffered
, &len
))
4910 #elif defined(SIOCOUTQ) /* Others except Windows */
4911 if (ioctl(httpGetFd(http
), SIOCOUTQ
, &buffered
))
4914 #else /* Windows (not possible) */
4916 #endif /* SO_NWRITE */
4918 return (buffered
> 0);
4923 * 'usage()' - Show program usage.
4929 _cupsLangPuts(stderr
, _("Usage: ipptool [options] URI filename [ ... "
4931 _cupsLangPuts(stderr
, _("Options:"));
4932 _cupsLangPuts(stderr
, _(" --help Show help."));
4933 _cupsLangPuts(stderr
, _(" --stop-after-include-error\n"
4934 " Stop tests after a failed INCLUDE."));
4935 _cupsLangPuts(stderr
, _(" --version Show version."));
4936 _cupsLangPuts(stderr
, _(" -4 Connect using IPv4."));
4937 _cupsLangPuts(stderr
, _(" -6 Connect using IPv6."));
4938 _cupsLangPuts(stderr
, _(" -C Send requests using "
4939 "chunking (default)."));
4940 _cupsLangPuts(stdout
, _(" -E Test with HTTP Upgrade to "
4942 _cupsLangPuts(stderr
, _(" -I Ignore errors."));
4943 _cupsLangPuts(stderr
, _(" -L Send requests using "
4944 "content-length."));
4945 _cupsLangPuts(stderr
, _(" -P filename.plist Produce XML plist to a file and test report to standard output."));
4946 _cupsLangPuts(stderr
, _(" -S Test with SSL "
4948 _cupsLangPuts(stderr
, _(" -T seconds Set the receive/send "
4949 "timeout in seconds."));
4950 _cupsLangPuts(stderr
, _(" -V version Set default IPP "
4952 _cupsLangPuts(stderr
, _(" -X Produce XML plist instead "
4954 _cupsLangPuts(stderr
, _(" -c Produce CSV output."));
4955 _cupsLangPuts(stderr
, _(" -d name=value Set named variable to "
4957 _cupsLangPuts(stderr
, _(" -f filename Set default request "
4959 _cupsLangPuts(stderr
, _(" -i seconds Repeat the last file with "
4960 "the given time interval."));
4961 _cupsLangPuts(stderr
, _(" -l Produce plain text output."));
4962 _cupsLangPuts(stderr
, _(" -n count Repeat the last file the "
4963 "given number of times."));
4964 _cupsLangPuts(stderr
, _(" -q Run silently."));
4965 _cupsLangPuts(stderr
, _(" -t Produce a test report."));
4966 _cupsLangPuts(stderr
, _(" -v Be verbose."));
4973 * 'validate_attr()' - Determine whether an attribute is valid.
4976 static int /* O - 1 if valid, 0 otherwise */
4977 validate_attr(FILE *outfile
, /* I - Output file */
4978 cups_array_t
*errors
, /* I - Errors array */
4979 ipp_attribute_t
*attr
) /* I - Attribute to validate */
4981 int i
; /* Looping var */
4982 char scheme
[64], /* Scheme from URI */
4983 userpass
[256], /* Username/password from URI */
4984 hostname
[256], /* Hostname from URI */
4985 resource
[1024]; /* Resource from URI */
4986 int port
, /* Port number from URI */
4987 uri_status
, /* URI separation status */
4988 valid
= 1; /* Is the attribute valid? */
4989 const char *ptr
; /* Pointer into string */
4990 ipp_attribute_t
*colattr
; /* Collection attribute */
4991 regex_t re
; /* Regular expression */
4992 ipp_uchar_t
*date
; /* Current date value */
5003 * Validate the attribute name.
5006 for (ptr
= attr
->name
; *ptr
; ptr
++)
5007 if (!isalnum(*ptr
& 255) && *ptr
!= '-' && *ptr
!= '.' && *ptr
!= '_')
5010 if (*ptr
|| ptr
== attr
->name
)
5015 "\"%s\": Bad attribute name - invalid character "
5016 "(RFC 2911 section 4.1.3).", attr
->name
);
5019 if ((ptr
- attr
->name
) > 255)
5024 "\"%s\": Bad attribute name - bad length "
5025 "(RFC 2911 section 4.1.3).", attr
->name
);
5028 switch (attr
->value_tag
)
5030 case IPP_TAG_INTEGER
:
5033 case IPP_TAG_BOOLEAN
:
5034 for (i
= 0; i
< attr
->num_values
; i
++)
5036 if (attr
->values
[i
].boolean
!= 0 &&
5037 attr
->values
[i
].boolean
!= 1)
5042 "\"%s\": Bad boolen value %d "
5043 "(RFC 2911 section 4.1.11).", attr
->name
,
5044 attr
->values
[i
].boolean
);
5050 for (i
= 0; i
< attr
->num_values
; i
++)
5052 if (attr
->values
[i
].integer
< 1)
5057 "\"%s\": Bad enum value %d - out of range "
5058 "(RFC 2911 section 4.1.4).", attr
->name
,
5059 attr
->values
[i
].integer
);
5064 case IPP_TAG_STRING
:
5065 for (i
= 0; i
< attr
->num_values
; i
++)
5067 if (attr
->values
[i
].unknown
.length
> IPP_MAX_OCTETSTRING
)
5072 "\"%s\": Bad octetString value - bad length %d "
5073 "(RFC 2911 section 4.1.10).", attr
->name
,
5074 attr
->values
[i
].unknown
.length
);
5080 for (i
= 0; i
< attr
->num_values
; i
++)
5082 date
= attr
->values
[i
].date
;
5084 if (date
[2] < 1 || date
[2] > 12)
5089 "\"%s\": Bad dateTime month %u "
5090 "(RFC 2911 section 4.1.14).", attr
->name
, date
[2]);
5093 if (date
[3] < 1 || date
[3] > 31)
5098 "\"%s\": Bad dateTime day %u "
5099 "(RFC 2911 section 4.1.14).", attr
->name
, date
[3]);
5107 "\"%s\": Bad dateTime hours %u "
5108 "(RFC 2911 section 4.1.14).", attr
->name
, date
[4]);
5116 "\"%s\": Bad dateTime minutes %u "
5117 "(RFC 2911 section 4.1.14).", attr
->name
, date
[5]);
5125 "\"%s\": Bad dateTime seconds %u "
5126 "(RFC 2911 section 4.1.14).", attr
->name
, date
[6]);
5134 "\"%s\": Bad dateTime deciseconds %u "
5135 "(RFC 2911 section 4.1.14).", attr
->name
, date
[7]);
5138 if (date
[8] != '-' && date
[8] != '+')
5143 "\"%s\": Bad dateTime UTC sign '%c' "
5144 "(RFC 2911 section 4.1.14).", attr
->name
, date
[8]);
5152 "\"%s\": Bad dateTime UTC hours %u "
5153 "(RFC 2911 section 4.1.14).", attr
->name
, date
[9]);
5161 "\"%s\": Bad dateTime UTC minutes %u "
5162 "(RFC 2911 section 4.1.14).", attr
->name
, date
[10]);
5167 case IPP_TAG_RESOLUTION
:
5168 for (i
= 0; i
< attr
->num_values
; i
++)
5170 if (attr
->values
[i
].resolution
.xres
<= 0)
5175 "\"%s\": Bad resolution value %dx%d%s - cross "
5176 "feed resolution must be positive "
5177 "(RFC 2911 section 4.1.15).", attr
->name
,
5178 attr
->values
[i
].resolution
.xres
,
5179 attr
->values
[i
].resolution
.yres
,
5180 attr
->values
[i
].resolution
.units
==
5181 IPP_RES_PER_INCH
? "dpi" :
5182 attr
->values
[i
].resolution
.units
==
5183 IPP_RES_PER_CM
? "dpcm" : "unknown");
5186 if (attr
->values
[i
].resolution
.yres
<= 0)
5191 "\"%s\": Bad resolution value %dx%d%s - feed "
5192 "resolution must be positive "
5193 "(RFC 2911 section 4.1.15).", attr
->name
,
5194 attr
->values
[i
].resolution
.xres
,
5195 attr
->values
[i
].resolution
.yres
,
5196 attr
->values
[i
].resolution
.units
==
5197 IPP_RES_PER_INCH
? "dpi" :
5198 attr
->values
[i
].resolution
.units
==
5199 IPP_RES_PER_CM
? "dpcm" : "unknown");
5202 if (attr
->values
[i
].resolution
.units
!= IPP_RES_PER_INCH
&&
5203 attr
->values
[i
].resolution
.units
!= IPP_RES_PER_CM
)
5208 "\"%s\": Bad resolution value %dx%d%s - bad "
5209 "units value (RFC 2911 section 4.1.15).",
5210 attr
->name
, attr
->values
[i
].resolution
.xres
,
5211 attr
->values
[i
].resolution
.yres
,
5212 attr
->values
[i
].resolution
.units
==
5213 IPP_RES_PER_INCH
? "dpi" :
5214 attr
->values
[i
].resolution
.units
==
5215 IPP_RES_PER_CM
? "dpcm" : "unknown");
5220 case IPP_TAG_RANGE
:
5221 for (i
= 0; i
< attr
->num_values
; i
++)
5223 if (attr
->values
[i
].range
.lower
> attr
->values
[i
].range
.upper
)
5228 "\"%s\": Bad rangeOfInteger value %d-%d - lower "
5229 "greater than upper (RFC 2911 section 4.1.13).",
5230 attr
->name
, attr
->values
[i
].range
.lower
,
5231 attr
->values
[i
].range
.upper
);
5236 case IPP_TAG_BEGIN_COLLECTION
:
5237 for (i
= 0; i
< attr
->num_values
; i
++)
5239 for (colattr
= attr
->values
[i
].collection
->attrs
;
5241 colattr
= colattr
->next
)
5243 if (!validate_attr(outfile
, NULL
, colattr
))
5250 if (colattr
&& errors
)
5252 add_stringf(errors
, "\"%s\": Bad collection value.", attr
->name
);
5256 validate_attr(outfile
, errors
, colattr
);
5257 colattr
= colattr
->next
;
5264 case IPP_TAG_TEXTLANG
:
5265 for (i
= 0; i
< attr
->num_values
; i
++)
5267 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
5269 if ((*ptr
& 0xe0) == 0xc0)
5272 if ((*ptr
& 0xc0) != 0x80)
5275 else if ((*ptr
& 0xf0) == 0xe0)
5278 if ((*ptr
& 0xc0) != 0x80)
5281 if ((*ptr
& 0xc0) != 0x80)
5284 else if ((*ptr
& 0xf8) == 0xf0)
5287 if ((*ptr
& 0xc0) != 0x80)
5290 if ((*ptr
& 0xc0) != 0x80)
5293 if ((*ptr
& 0xc0) != 0x80)
5296 else if (*ptr
& 0x80)
5305 "\"%s\": Bad text value \"%s\" - bad UTF-8 "
5306 "sequence (RFC 2911 section 4.1.1).", attr
->name
,
5307 attr
->values
[i
].string
.text
);
5310 if ((ptr
- attr
->values
[i
].string
.text
) > (IPP_MAX_TEXT
- 1))
5315 "\"%s\": Bad text value \"%s\" - bad length %d "
5316 "(RFC 2911 section 4.1.1).", attr
->name
,
5317 attr
->values
[i
].string
.text
,
5318 (int)strlen(attr
->values
[i
].string
.text
));
5324 case IPP_TAG_NAMELANG
:
5325 for (i
= 0; i
< attr
->num_values
; i
++)
5327 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
5329 if ((*ptr
& 0xe0) == 0xc0)
5332 if ((*ptr
& 0xc0) != 0x80)
5335 else if ((*ptr
& 0xf0) == 0xe0)
5338 if ((*ptr
& 0xc0) != 0x80)
5341 if ((*ptr
& 0xc0) != 0x80)
5344 else if ((*ptr
& 0xf8) == 0xf0)
5347 if ((*ptr
& 0xc0) != 0x80)
5350 if ((*ptr
& 0xc0) != 0x80)
5353 if ((*ptr
& 0xc0) != 0x80)
5356 else if (*ptr
& 0x80)
5365 "\"%s\": Bad name value \"%s\" - bad UTF-8 "
5366 "sequence (RFC 2911 section 4.1.2).", attr
->name
,
5367 attr
->values
[i
].string
.text
);
5370 if ((ptr
- attr
->values
[i
].string
.text
) > (IPP_MAX_NAME
- 1))
5375 "\"%s\": Bad name value \"%s\" - bad length %d "
5376 "(RFC 2911 section 4.1.2).", attr
->name
,
5377 attr
->values
[i
].string
.text
,
5378 (int)strlen(attr
->values
[i
].string
.text
));
5383 case IPP_TAG_KEYWORD
:
5384 for (i
= 0; i
< attr
->num_values
; i
++)
5386 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
5387 if (!isalnum(*ptr
& 255) && *ptr
!= '-' && *ptr
!= '.' &&
5391 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
5396 "\"%s\": Bad keyword value \"%s\" - invalid "
5397 "character (RFC 2911 section 4.1.3).",
5398 attr
->name
, attr
->values
[i
].string
.text
);
5401 if ((ptr
- attr
->values
[i
].string
.text
) > (IPP_MAX_KEYWORD
- 1))
5406 "\"%s\": Bad keyword value \"%s\" - bad "
5407 "length %d (RFC 2911 section 4.1.3).",
5408 attr
->name
, attr
->values
[i
].string
.text
,
5409 (int)strlen(attr
->values
[i
].string
.text
));
5415 for (i
= 0; i
< attr
->num_values
; i
++)
5417 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
,
5418 attr
->values
[i
].string
.text
,
5419 scheme
, sizeof(scheme
),
5420 userpass
, sizeof(userpass
),
5421 hostname
, sizeof(hostname
),
5422 &port
, resource
, sizeof(resource
));
5424 if (uri_status
< HTTP_URI_OK
)
5429 "\"%s\": Bad URI value \"%s\" - %s "
5430 "(RFC 2911 section 4.1.5).", attr
->name
,
5431 attr
->values
[i
].string
.text
,
5432 httpURIStatusString(uri_status
));
5435 if (strlen(attr
->values
[i
].string
.text
) > (IPP_MAX_URI
- 1))
5440 "\"%s\": Bad URI value \"%s\" - bad length %d "
5441 "(RFC 2911 section 4.1.5).", attr
->name
,
5442 attr
->values
[i
].string
.text
,
5443 (int)strlen(attr
->values
[i
].string
.text
));
5448 case IPP_TAG_URISCHEME
:
5449 for (i
= 0; i
< attr
->num_values
; i
++)
5451 ptr
= attr
->values
[i
].string
.text
;
5452 if (islower(*ptr
& 255))
5454 for (ptr
++; *ptr
; ptr
++)
5455 if (!islower(*ptr
& 255) && !isdigit(*ptr
& 255) &&
5456 *ptr
!= '+' && *ptr
!= '-' && *ptr
!= '.')
5460 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
5465 "\"%s\": Bad uriScheme value \"%s\" - bad "
5466 "characters (RFC 2911 section 4.1.6).",
5467 attr
->name
, attr
->values
[i
].string
.text
);
5470 if ((ptr
- attr
->values
[i
].string
.text
) > (IPP_MAX_URISCHEME
- 1))
5475 "\"%s\": Bad uriScheme value \"%s\" - bad "
5476 "length %d (RFC 2911 section 4.1.6).",
5477 attr
->name
, attr
->values
[i
].string
.text
,
5478 (int)strlen(attr
->values
[i
].string
.text
));
5483 case IPP_TAG_CHARSET
:
5484 for (i
= 0; i
< attr
->num_values
; i
++)
5486 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
5487 if (!isprint(*ptr
& 255) || isupper(*ptr
& 255) ||
5488 isspace(*ptr
& 255))
5491 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
5496 "\"%s\": Bad charset value \"%s\" - bad "
5497 "characters (RFC 2911 section 4.1.7).",
5498 attr
->name
, attr
->values
[i
].string
.text
);
5501 if ((ptr
- attr
->values
[i
].string
.text
) > (IPP_MAX_CHARSET
- 1))
5506 "\"%s\": Bad charset value \"%s\" - bad "
5507 "length %d (RFC 2911 section 4.1.7).",
5508 attr
->name
, attr
->values
[i
].string
.text
,
5509 (int)strlen(attr
->values
[i
].string
.text
));
5514 case IPP_TAG_LANGUAGE
:
5516 * The following regular expression is derived from the ABNF for
5517 * language tags in RFC 4646. All I can say is that this is the
5518 * easiest way to check the values...
5521 if ((i
= regcomp(&re
,
5523 "(([a-z]{2,3}(-[a-z][a-z][a-z]){0,3})|[a-z]{4,8})"
5525 "(-[a-z][a-z][a-z][a-z]){0,1}" /* script */
5526 "(-([a-z][a-z]|[0-9][0-9][0-9])){0,1}" /* region */
5527 "(-([a-z]{5,8}|[0-9][0-9][0-9]))*" /* variant */
5528 "(-[a-wy-z](-[a-z0-9]{2,8})+)*" /* extension */
5529 "(-x(-[a-z0-9]{1,8})+)*" /* privateuse */
5531 "x(-[a-z0-9]{1,8})+" /* privateuse */
5533 "[a-z]{1,3}(-[a-z][0-9]{2,8}){1,2}" /* grandfathered */
5535 REG_NOSUB
| REG_EXTENDED
)) != 0)
5537 char temp
[256]; /* Temporary error string */
5539 regerror(i
, &re
, temp
, sizeof(temp
));
5540 print_fatal_error(outfile
, "Unable to compile naturalLanguage regular "
5541 "expression: %s.", temp
);
5545 for (i
= 0; i
< attr
->num_values
; i
++)
5547 if (regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
5552 "\"%s\": Bad naturalLanguage value \"%s\" - bad "
5553 "characters (RFC 2911 section 4.1.8).",
5554 attr
->name
, attr
->values
[i
].string
.text
);
5557 if (strlen(attr
->values
[i
].string
.text
) > (IPP_MAX_LANGUAGE
- 1))
5562 "\"%s\": Bad naturalLanguage value \"%s\" - bad "
5563 "length %d (RFC 2911 section 4.1.8).",
5564 attr
->name
, attr
->values
[i
].string
.text
,
5565 (int)strlen(attr
->values
[i
].string
.text
));
5572 case IPP_TAG_MIMETYPE
:
5574 * The following regular expression is derived from the ABNF for
5575 * language tags in RFC 2045 and 4288. All I can say is that this is
5576 * the easiest way to check the values...
5579 if ((i
= regcomp(&re
,
5581 "[-a-zA-Z0-9!#$&.+^_]{1,127}" /* type-name */
5583 "[-a-zA-Z0-9!#$&.+^_]{1,127}" /* subtype-name */
5584 "(;[-a-zA-Z0-9!#$&.+^_]{1,127}=" /* parameter= */
5585 "([-a-zA-Z0-9!#$&.+^_]{1,127}|\"[^\"]*\"))*"
5588 REG_NOSUB
| REG_EXTENDED
)) != 0)
5590 char temp
[256]; /* Temporary error string */
5592 regerror(i
, &re
, temp
, sizeof(temp
));
5593 print_fatal_error(outfile
, "Unable to compile mimeMediaType regular "
5594 "expression: %s.", temp
);
5598 for (i
= 0; i
< attr
->num_values
; i
++)
5600 if (regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
5605 "\"%s\": Bad mimeMediaType value \"%s\" - bad "
5606 "characters (RFC 2911 section 4.1.9).",
5607 attr
->name
, attr
->values
[i
].string
.text
);
5610 if (strlen(attr
->values
[i
].string
.text
) > (IPP_MAX_MIMETYPE
- 1))
5615 "\"%s\": Bad mimeMediaType value \"%s\" - bad "
5616 "length %d (RFC 2911 section 4.1.9).",
5617 attr
->name
, attr
->values
[i
].string
.text
,
5618 (int)strlen(attr
->values
[i
].string
.text
));
5634 * 'with_value()' - Test a WITH-VALUE predicate.
5637 static int /* O - 1 on match, 0 on non-match */
5638 with_value(FILE *outfile
, /* I - Output file */
5639 cups_array_t
*errors
, /* I - Errors array */
5640 char *value
, /* I - Value string */
5641 int flags
, /* I - Flags for match */
5642 ipp_attribute_t
*attr
, /* I - Attribute to compare */
5643 char *matchbuf
, /* I - Buffer to hold matching value */
5644 size_t matchlen
) /* I - Length of match buffer */
5646 int i
, /* Looping var */
5648 char temp
[1024], /* Temporary value string */
5649 *valptr
; /* Pointer into value */
5653 match
= (flags
& _CUPS_WITH_ALL
) ? 1 : 0;
5656 * NULL matches everything.
5659 if (!value
|| !*value
)
5663 * Compare the value string to the attribute value.
5666 switch (attr
->value_tag
)
5668 case IPP_TAG_INTEGER
:
5670 for (i
= 0; i
< attr
->num_values
; i
++)
5672 char op
, /* Comparison operator */
5673 *nextptr
; /* Next pointer */
5674 int intvalue
, /* Integer value */
5675 valmatch
= 0; /* Does the current value match? */
5679 while (isspace(*valptr
& 255) || isdigit(*valptr
& 255) ||
5680 *valptr
== '-' || *valptr
== ',' || *valptr
== '<' ||
5681 *valptr
== '=' || *valptr
== '>')
5684 while (*valptr
&& !isdigit(*valptr
& 255) && *valptr
!= '-')
5686 if (*valptr
== '<' || *valptr
== '>' || *valptr
== '=')
5694 intvalue
= (int)strtol(valptr
, &nextptr
, 0);
5695 if (nextptr
== valptr
)
5699 if ((op
== '=' && attr
->values
[i
].integer
== intvalue
) ||
5700 (op
== '<' && attr
->values
[i
].integer
< intvalue
) ||
5701 (op
== '>' && attr
->values
[i
].integer
> intvalue
))
5704 snprintf(matchbuf
, matchlen
, "%d", attr
->values
[i
].integer
);
5711 if (flags
& _CUPS_WITH_ALL
)
5726 if (!match
&& errors
)
5728 for (i
= 0; i
< attr
->num_values
; i
++)
5729 add_stringf(errors
, "GOT: %s=%d", attr
->name
,
5730 attr
->values
[i
].integer
);
5734 case IPP_TAG_RANGE
:
5735 for (i
= 0; i
< attr
->num_values
; i
++)
5737 char op
, /* Comparison operator */
5738 *nextptr
; /* Next pointer */
5739 int intvalue
, /* Integer value */
5740 valmatch
= 0; /* Does the current value match? */
5744 while (isspace(*valptr
& 255) || isdigit(*valptr
& 255) ||
5745 *valptr
== '-' || *valptr
== ',' || *valptr
== '<' ||
5746 *valptr
== '=' || *valptr
== '>')
5749 while (*valptr
&& !isdigit(*valptr
& 255) && *valptr
!= '-')
5751 if (*valptr
== '<' || *valptr
== '>' || *valptr
== '=')
5759 intvalue
= (int)strtol(valptr
, &nextptr
, 0);
5760 if (nextptr
== valptr
)
5764 if ((op
== '=' && (attr
->values
[i
].range
.lower
== intvalue
||
5765 attr
->values
[i
].range
.upper
== intvalue
)) ||
5766 (op
== '<' && attr
->values
[i
].range
.upper
< intvalue
) ||
5767 (op
== '>' && attr
->values
[i
].range
.upper
> intvalue
))
5770 snprintf(matchbuf
, matchlen
, "%d-%d",
5771 attr
->values
[0].range
.lower
,
5772 attr
->values
[0].range
.upper
);
5779 if (flags
& _CUPS_WITH_ALL
)
5794 if (!match
&& errors
)
5796 for (i
= 0; i
< attr
->num_values
; i
++)
5797 add_stringf(errors
, "GOT: %s=%d-%d", attr
->name
,
5798 attr
->values
[i
].range
.lower
,
5799 attr
->values
[i
].range
.upper
);
5803 case IPP_TAG_BOOLEAN
:
5804 for (i
= 0; i
< attr
->num_values
; i
++)
5806 if (!strcmp(value
, "true") == attr
->values
[i
].boolean
)
5809 strlcpy(matchbuf
, value
, matchlen
);
5811 if (!(flags
& _CUPS_WITH_ALL
))
5817 else if (flags
& _CUPS_WITH_ALL
)
5824 if (!match
&& errors
)
5826 for (i
= 0; i
< attr
->num_values
; i
++)
5827 add_stringf(errors
, "GOT: %s=%s", attr
->name
,
5828 attr
->values
[i
].boolean
? "true" : "false");
5832 case IPP_TAG_RESOLUTION
:
5833 for (i
= 0; i
< attr
->num_values
; i
++)
5835 if (attr
->values
[i
].resolution
.xres
==
5836 attr
->values
[i
].resolution
.yres
)
5837 snprintf(temp
, sizeof(temp
), "%d%s",
5838 attr
->values
[i
].resolution
.xres
,
5839 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
5842 snprintf(temp
, sizeof(temp
), "%dx%d%s",
5843 attr
->values
[i
].resolution
.xres
,
5844 attr
->values
[i
].resolution
.yres
,
5845 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
5848 if (!strcmp(value
, temp
))
5851 strlcpy(matchbuf
, value
, matchlen
);
5853 if (!(flags
& _CUPS_WITH_ALL
))
5859 else if (flags
& _CUPS_WITH_ALL
)
5866 if (!match
&& errors
)
5868 for (i
= 0; i
< attr
->num_values
; i
++)
5870 if (attr
->values
[i
].resolution
.xres
==
5871 attr
->values
[i
].resolution
.yres
)
5872 snprintf(temp
, sizeof(temp
), "%d%s",
5873 attr
->values
[i
].resolution
.xres
,
5874 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
5877 snprintf(temp
, sizeof(temp
), "%dx%d%s",
5878 attr
->values
[i
].resolution
.xres
,
5879 attr
->values
[i
].resolution
.yres
,
5880 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
5883 if (strcmp(value
, temp
))
5884 add_stringf(errors
, "GOT: %s=%s", attr
->name
, temp
);
5889 case IPP_TAG_NOVALUE
:
5890 case IPP_TAG_UNKNOWN
:
5893 case IPP_TAG_CHARSET
:
5894 case IPP_TAG_KEYWORD
:
5895 case IPP_TAG_LANGUAGE
:
5896 case IPP_TAG_MIMETYPE
:
5898 case IPP_TAG_NAMELANG
:
5900 case IPP_TAG_TEXTLANG
:
5902 case IPP_TAG_URISCHEME
:
5903 if (flags
& _CUPS_WITH_REGEX
)
5906 * Value is an extended, case-sensitive POSIX regular expression...
5909 regex_t re
; /* Regular expression */
5911 if ((i
= regcomp(&re
, value
, REG_EXTENDED
| REG_NOSUB
)) != 0)
5913 regerror(i
, &re
, temp
, sizeof(temp
));
5915 print_fatal_error(outfile
, "Unable to compile WITH-VALUE regular expression "
5916 "\"%s\" - %s", value
, temp
);
5921 * See if ALL of the values match the given regular expression.
5924 for (i
= 0; i
< attr
->num_values
; i
++)
5926 if (!regexec(&re
, get_string(attr
, i
, flags
, temp
, sizeof(temp
)),
5931 get_string(attr
, i
, flags
, temp
, sizeof(temp
)),
5934 if (!(flags
& _CUPS_WITH_ALL
))
5940 else if (flags
& _CUPS_WITH_ALL
)
5949 else if (ippGetValueTag(attr
) == IPP_TAG_URI
)
5951 if (!strncmp(value
, "ipp://", 6) || !strncmp(value
, "http://", 7) || !strncmp(value
, "ipps://", 7) || !strncmp(value
, "https://", 8))
5953 char scheme
[256], /* URI scheme */
5954 userpass
[256], /* username:password, if any */
5955 hostname
[256], /* hostname */
5956 *hostptr
, /* Pointer into hostname */
5957 resource
[1024]; /* Resource path */
5958 int port
; /* Port number */
5960 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
== '.')
5963 * Strip trailing "." in hostname of URI...
5967 httpAssembleURI(HTTP_URI_CODING_ALL
, temp
, sizeof(temp
), scheme
, userpass
, hostname
, port
, resource
);
5973 * Value is a literal URI string, see if the value(s) match...
5976 for (i
= 0; i
< attr
->num_values
; i
++)
5978 if (!strcmp(value
, get_string(attr
, i
, flags
, temp
, sizeof(temp
))))
5982 get_string(attr
, i
, flags
, temp
, sizeof(temp
)),
5985 if (!(flags
& _CUPS_WITH_ALL
))
5991 else if (flags
& _CUPS_WITH_ALL
)
6001 * Value is a literal string, see if the value(s) match...
6004 for (i
= 0; i
< attr
->num_values
; i
++)
6006 if (!strcmp(value
, get_string(attr
, i
, flags
, temp
, sizeof(temp
))))
6010 get_string(attr
, i
, flags
, temp
, sizeof(temp
)),
6013 if (!(flags
& _CUPS_WITH_ALL
))
6019 else if (flags
& _CUPS_WITH_ALL
)
6027 if (!match
&& errors
)
6029 for (i
= 0; i
< attr
->num_values
; i
++)
6030 add_stringf(errors
, "GOT: %s=\"%s\"", attr
->name
,
6031 attr
->values
[i
].string
.text
);
6044 * 'with_value_from()' - Test a WITH-VALUE-FROM predicate.
6047 static int /* O - 1 on match, 0 on non-match */
6049 cups_array_t
*errors
, /* I - Errors array */
6050 ipp_attribute_t
*fromattr
, /* I - "From" attribute */
6051 ipp_attribute_t
*attr
, /* I - Attribute to compare */
6052 char *matchbuf
, /* I - Buffer to hold matching value */
6053 size_t matchlen
) /* I - Length of match buffer */
6055 int i
, j
, /* Looping vars */
6056 count
= ippGetCount(attr
), /* Number of attribute values */
6057 match
= 1; /* Match? */
6063 * Compare the from value(s) to the attribute value(s)...
6066 switch (ippGetValueTag(attr
))
6068 case IPP_TAG_INTEGER
:
6069 if (ippGetValueTag(fromattr
) != IPP_TAG_INTEGER
&& ippGetValueTag(fromattr
) != IPP_TAG_RANGE
)
6070 goto wrong_value_tag
;
6072 for (i
= 0; i
< count
; i
++)
6074 int value
= ippGetInteger(attr
, i
);
6075 /* Current integer value */
6077 if (ippContainsInteger(fromattr
, value
))
6080 snprintf(matchbuf
, matchlen
, "%d", value
);
6084 add_stringf(errors
, "GOT: %s=%d", ippGetName(attr
), value
);
6091 if (ippGetValueTag(fromattr
) != IPP_TAG_ENUM
)
6092 goto wrong_value_tag
;
6094 for (i
= 0; i
< count
; i
++)
6096 int value
= ippGetInteger(attr
, i
);
6097 /* Current integer value */
6099 if (ippContainsInteger(fromattr
, value
))
6102 snprintf(matchbuf
, matchlen
, "%d", value
);
6106 add_stringf(errors
, "GOT: %s=%d", ippGetName(attr
), value
);
6112 case IPP_TAG_RESOLUTION
:
6113 if (ippGetValueTag(fromattr
) != IPP_TAG_RESOLUTION
)
6114 goto wrong_value_tag
;
6116 for (i
= 0; i
< count
; i
++)
6120 int fromcount
= ippGetCount(fromattr
);
6121 int fromxres
, fromyres
;
6122 ipp_res_t fromunits
;
6124 xres
= ippGetResolution(attr
, i
, &yres
, &units
);
6126 for (j
= 0; j
< fromcount
; j
++)
6128 fromxres
= ippGetResolution(fromattr
, j
, &fromyres
, &fromunits
);
6129 if (fromxres
== xres
&& fromyres
== yres
&& fromunits
== units
)
6138 snprintf(matchbuf
, matchlen
, "%d%s", xres
, units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
6140 snprintf(matchbuf
, matchlen
, "%dx%d%s", xres
, yres
, units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
6146 add_stringf(errors
, "GOT: %s=%d%s", ippGetName(attr
), xres
, units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
6148 add_stringf(errors
, "GOT: %s=%dx%d%s", ippGetName(attr
), xres
, yres
, units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
6155 case IPP_TAG_NOVALUE
:
6156 case IPP_TAG_UNKNOWN
:
6159 case IPP_TAG_CHARSET
:
6160 case IPP_TAG_KEYWORD
:
6161 case IPP_TAG_LANGUAGE
:
6162 case IPP_TAG_MIMETYPE
:
6164 case IPP_TAG_NAMELANG
:
6166 case IPP_TAG_TEXTLANG
:
6168 case IPP_TAG_URISCHEME
:
6169 for (i
= 0; i
< count
; i
++)
6171 const char *value
= ippGetString(attr
, i
, NULL
);
6172 /* Current string value */
6174 if (ippContainsString(fromattr
, value
))
6177 strlcpy(matchbuf
, value
, matchlen
);
6181 add_stringf(errors
, "GOT: %s='%s'", ippGetName(attr
), value
);
6194 /* value tag mismatch between fromattr and attr */
6197 add_stringf(errors
, "GOT: %s OF-TYPE %s", ippGetName(attr
), ippTagString(ippGetValueTag(attr
)));