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 values
[0] = (int)strtol(token
, &tokenptr
, 10);
1733 while (tokenptr
&& *tokenptr
&&
1734 num_values
< (int)(sizeof(values
) / sizeof(values
[0])))
1736 if (*tokenptr
== ',')
1739 if (!isdigit(*tokenptr
& 255) && *tokenptr
!= '-')
1741 char *ptr
, ch
; /* Pointer to next terminator */
1743 if (value
!= IPP_TAG_ENUM
)
1746 if ((ptr
= strchr(tokenptr
, ',')) != NULL
)
1754 ptr
= tokenptr
+ strlen(tokenptr
);
1757 if ((i
= ippEnumValue(attr
, tokenptr
)) < 0)
1761 i
= (int)strtol(tokenptr
, &tokenptr
, 0);
1763 values
[num_values
++] = i
;
1766 attrptr
= ippAddIntegers(request
, group
, value
, attr
, num_values
, values
);
1769 if (!tokenptr
|| *tokenptr
)
1771 print_fatal_error(outfile
, "Bad %s value \"%s\" on line %d.",
1772 ippTagString(value
), token
, linenum
);
1778 case IPP_TAG_RESOLUTION
:
1780 int xres
, /* X resolution */
1781 yres
; /* Y resolution */
1782 char *ptr
; /* Pointer into value */
1784 xres
= yres
= (int)strtol(token
, (char **)&ptr
, 10);
1785 if (ptr
> token
&& xres
> 0)
1788 yres
= (int)strtol(ptr
+ 1, (char **)&ptr
, 10);
1791 if (ptr
<= token
|| xres
<= 0 || yres
<= 0 || !ptr
||
1792 (_cups_strcasecmp(ptr
, "dpi") &&
1793 _cups_strcasecmp(ptr
, "dpc") &&
1794 _cups_strcasecmp(ptr
, "dpcm") &&
1795 _cups_strcasecmp(ptr
, "other")))
1797 print_fatal_error(outfile
, "Bad resolution value \"%s\" on line %d.",
1803 if (!_cups_strcasecmp(ptr
, "dpi"))
1804 attrptr
= ippAddResolution(request
, group
, attr
, IPP_RES_PER_INCH
, xres
, yres
);
1805 else if (!_cups_strcasecmp(ptr
, "dpc") ||
1806 !_cups_strcasecmp(ptr
, "dpcm"))
1807 attrptr
= ippAddResolution(request
, group
, attr
, IPP_RES_PER_CM
, xres
, yres
);
1809 attrptr
= ippAddResolution(request
, group
, attr
, (ipp_res_t
)0, xres
, yres
);
1813 case IPP_TAG_RANGE
:
1815 int lowers
[4], /* Lower value */
1816 uppers
[4], /* Upper values */
1817 num_vals
; /* Number of values */
1820 num_vals
= sscanf(token
, "%d-%d,%d-%d,%d-%d,%d-%d",
1821 lowers
+ 0, uppers
+ 0,
1822 lowers
+ 1, uppers
+ 1,
1823 lowers
+ 2, uppers
+ 2,
1824 lowers
+ 3, uppers
+ 3);
1826 if ((num_vals
& 1) || num_vals
== 0)
1828 print_fatal_error(outfile
, "Bad rangeOfInteger value \"%s\" on line "
1829 "%d.", token
, linenum
);
1834 attrptr
= ippAddRanges(request
, group
, attr
, num_vals
/ 2, lowers
,
1839 case IPP_TAG_BEGIN_COLLECTION
:
1840 if (!strcmp(token
, "{"))
1842 ipp_t
*col
= get_collection(outfile
, vars
, fp
, &linenum
);
1843 /* Collection value */
1847 attrptr
= lastcol
= ippAddCollection(request
, group
, attr
, col
);
1858 print_fatal_error(outfile
, "Bad ATTR collection value on line %d.",
1866 ipp_t
*col
; /* Collection value */
1867 long pos
= ftell(fp
); /* Save position of file */
1869 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1872 if (strcmp(token
, ","))
1874 fseek(fp
, pos
, SEEK_SET
);
1878 if (!get_token(fp
, token
, sizeof(token
), &linenum
) || strcmp(token
, "{"))
1880 print_fatal_error(outfile
, "Unexpected \"%s\" on line %d.", token
, linenum
);
1886 if ((col
= get_collection(outfile
, vars
, fp
, &linenum
)) == NULL
)
1889 ippSetCollection(request
, &attrptr
, ippGetCount(attrptr
), col
);
1892 while (!strcmp(token
, "{"));
1895 case IPP_TAG_STRING
:
1896 attrptr
= ippAddOctetString(request
, group
, attr
, token
, (int)strlen(token
));
1900 print_fatal_error(outfile
, "Unsupported ATTR value tag %s on line %d.",
1901 ippTagString(value
), linenum
);
1905 case IPP_TAG_TEXTLANG
:
1906 case IPP_TAG_NAMELANG
:
1909 case IPP_TAG_KEYWORD
:
1911 case IPP_TAG_URISCHEME
:
1912 case IPP_TAG_CHARSET
:
1913 case IPP_TAG_LANGUAGE
:
1914 case IPP_TAG_MIMETYPE
:
1915 if (!strchr(token
, ','))
1916 attrptr
= ippAddString(request
, group
, value
, attr
, NULL
, token
);
1920 * Multiple string values...
1923 int num_values
; /* Number of values */
1924 char *values
[100], /* Values */
1925 *ptr
; /* Pointer to next value */
1931 for (ptr
= strchr(token
, ','); ptr
; ptr
= strchr(ptr
, ','))
1933 if (ptr
> token
&& ptr
[-1] == '\\')
1934 _cups_strcpy(ptr
- 1, ptr
);
1938 values
[num_values
] = ptr
;
1943 attrptr
= ippAddStrings(request
, group
, value
, attr
, num_values
,
1944 NULL
, (const char **)values
);
1951 print_fatal_error(outfile
, "Unable to add attribute on line %d: %s", linenum
,
1952 cupsLastErrorString());
1957 else if (!_cups_strcasecmp(token
, "FILE"))
1963 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1965 print_fatal_error(outfile
, "Missing FILE filename on line %d.", linenum
);
1970 expand_variables(vars
, token
, temp
, sizeof(token
));
1971 get_filename(testfile
, filename
, token
, sizeof(filename
));
1973 if (access(filename
, R_OK
))
1975 print_fatal_error(outfile
, "Filename \"%s\" on line %d cannot be read.",
1977 print_fatal_error(outfile
, "Filename mapped to \"%s\".", filename
);
1982 else if (!_cups_strcasecmp(token
, "STATUS"))
1988 if (num_statuses
>= (int)(sizeof(statuses
) / sizeof(statuses
[0])))
1990 print_fatal_error(outfile
, "Too many STATUS's on line %d.", linenum
);
1995 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1997 print_fatal_error(outfile
, "Missing STATUS code on line %d.", linenum
);
2002 if ((statuses
[num_statuses
].status
= ippErrorValue(token
))
2003 == (ipp_status_t
)-1 &&
2004 (statuses
[num_statuses
].status
= (ipp_status_t
)strtol(token
, NULL
, 0)) == 0)
2006 print_fatal_error(outfile
, "Bad STATUS code \"%s\" on line %d.", token
,
2012 last_status
= statuses
+ num_statuses
;
2015 last_status
->define_match
= NULL
;
2016 last_status
->define_no_match
= NULL
;
2017 last_status
->if_defined
= NULL
;
2018 last_status
->if_not_defined
= NULL
;
2019 last_status
->repeat_limit
= 1000;
2020 last_status
->repeat_match
= 0;
2021 last_status
->repeat_no_match
= 0;
2023 else if (!_cups_strcasecmp(token
, "EXPECT") || !_cups_strcasecmp(token
, "EXPECT-ALL"))
2026 * Expected attributes...
2029 int expect_all
= !_cups_strcasecmp(token
, "EXPECT-ALL");
2031 if (num_expects
>= (int)(sizeof(expects
) / sizeof(expects
[0])))
2033 print_fatal_error(outfile
, "Too many EXPECT's on line %d.", linenum
);
2038 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2040 print_fatal_error(outfile
, "Missing EXPECT name on line %d.", linenum
);
2045 last_expect
= expects
+ num_expects
;
2048 memset(last_expect
, 0, sizeof(_cups_expect_t
));
2049 last_expect
->repeat_limit
= 1000;
2050 last_expect
->expect_all
= expect_all
;
2052 if (token
[0] == '!')
2054 last_expect
->not_expect
= 1;
2055 last_expect
->name
= strdup(token
+ 1);
2057 else if (token
[0] == '?')
2059 last_expect
->optional
= 1;
2060 last_expect
->name
= strdup(token
+ 1);
2063 last_expect
->name
= strdup(token
);
2065 else if (!_cups_strcasecmp(token
, "COUNT"))
2067 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2069 print_fatal_error(outfile
, "Missing COUNT number on line %d.", linenum
);
2074 if ((i
= atoi(token
)) <= 0)
2076 print_fatal_error(outfile
, "Bad COUNT \"%s\" on line %d.", token
, linenum
);
2082 last_expect
->count
= i
;
2085 print_fatal_error(outfile
, "COUNT without a preceding EXPECT on line %d.",
2091 else if (!_cups_strcasecmp(token
, "DEFINE-MATCH"))
2093 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2095 print_fatal_error(outfile
, "Missing DEFINE-MATCH variable on line %d.",
2102 last_expect
->define_match
= strdup(token
);
2103 else if (last_status
)
2104 last_status
->define_match
= strdup(token
);
2107 print_fatal_error(outfile
, "DEFINE-MATCH without a preceding EXPECT or STATUS "
2108 "on line %d.", linenum
);
2113 else if (!_cups_strcasecmp(token
, "DEFINE-NO-MATCH"))
2115 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2117 print_fatal_error(outfile
, "Missing DEFINE-NO-MATCH variable on line %d.",
2124 last_expect
->define_no_match
= strdup(token
);
2125 else if (last_status
)
2126 last_status
->define_no_match
= strdup(token
);
2129 print_fatal_error(outfile
, "DEFINE-NO-MATCH without a preceding EXPECT or "
2130 "STATUS on line %d.", linenum
);
2135 else if (!_cups_strcasecmp(token
, "DEFINE-VALUE"))
2137 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2139 print_fatal_error(outfile
, "Missing DEFINE-VALUE variable on line %d.",
2146 last_expect
->define_value
= strdup(token
);
2149 print_fatal_error(outfile
, "DEFINE-VALUE without a preceding EXPECT on "
2150 "line %d.", linenum
);
2155 else if (!_cups_strcasecmp(token
, "OF-TYPE"))
2157 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2159 print_fatal_error(outfile
, "Missing OF-TYPE value tag(s) on line %d.",
2166 last_expect
->of_type
= strdup(token
);
2169 print_fatal_error(outfile
, "OF-TYPE without a preceding EXPECT on line %d.",
2175 else if (!_cups_strcasecmp(token
, "IN-GROUP"))
2177 ipp_tag_t in_group
; /* IN-GROUP value */
2180 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2182 print_fatal_error(outfile
, "Missing IN-GROUP group tag on line %d.", linenum
);
2187 if ((in_group
= ippTagValue(token
)) == (ipp_tag_t
)-1)
2190 else if (last_expect
)
2191 last_expect
->in_group
= in_group
;
2194 print_fatal_error(outfile
, "IN-GROUP without a preceding EXPECT on line %d.",
2200 else if (!_cups_strcasecmp(token
, "REPEAT-LIMIT"))
2202 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2204 print_fatal_error(outfile
, "Missing REPEAT-LIMIT value on line %d.", linenum
);
2208 else if (atoi(token
) <= 0)
2210 print_fatal_error(outfile
, "Bad REPEAT-LIMIT value on line %d.", linenum
);
2216 last_status
->repeat_limit
= atoi(token
);
2217 else if (last_expect
)
2218 last_expect
->repeat_limit
= atoi(token
);
2221 print_fatal_error(outfile
, "REPEAT-LIMIT without a preceding EXPECT or STATUS "
2222 "on line %d.", linenum
);
2227 else if (!_cups_strcasecmp(token
, "REPEAT-MATCH"))
2230 last_status
->repeat_match
= 1;
2231 else if (last_expect
)
2232 last_expect
->repeat_match
= 1;
2235 print_fatal_error(outfile
, "REPEAT-MATCH without a preceding EXPECT or STATUS "
2236 "on line %d.", linenum
);
2241 else if (!_cups_strcasecmp(token
, "REPEAT-NO-MATCH"))
2244 last_status
->repeat_no_match
= 1;
2245 else if (last_expect
)
2246 last_expect
->repeat_no_match
= 1;
2249 print_fatal_error(outfile
, "REPEAT-NO-MATCH without a preceding EXPECT or "
2250 "STATUS on ine %d.", linenum
);
2255 else if (!_cups_strcasecmp(token
, "SAME-COUNT-AS"))
2257 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2259 print_fatal_error(outfile
, "Missing SAME-COUNT-AS name on line %d.", linenum
);
2265 last_expect
->same_count_as
= strdup(token
);
2268 print_fatal_error(outfile
, "SAME-COUNT-AS without a preceding EXPECT on line "
2274 else if (!_cups_strcasecmp(token
, "IF-DEFINED"))
2276 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2278 print_fatal_error(outfile
, "Missing IF-DEFINED name on line %d.", linenum
);
2284 last_expect
->if_defined
= strdup(token
);
2285 else if (last_status
)
2286 last_status
->if_defined
= strdup(token
);
2289 print_fatal_error(outfile
, "IF-DEFINED without a preceding EXPECT or STATUS "
2290 "on line %d.", linenum
);
2295 else if (!_cups_strcasecmp(token
, "IF-NOT-DEFINED"))
2297 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2299 print_fatal_error(outfile
, "Missing IF-NOT-DEFINED name on line %d.", linenum
);
2305 last_expect
->if_not_defined
= strdup(token
);
2306 else if (last_status
)
2307 last_status
->if_not_defined
= strdup(token
);
2310 print_fatal_error(outfile
, "IF-NOT-DEFINED without a preceding EXPECT or STATUS "
2311 "on line %d.", linenum
);
2316 else if (!_cups_strcasecmp(token
, "WITH-ALL-VALUES") ||
2317 !_cups_strcasecmp(token
, "WITH-ALL-HOSTNAMES") ||
2318 !_cups_strcasecmp(token
, "WITH-ALL-RESOURCES") ||
2319 !_cups_strcasecmp(token
, "WITH-ALL-SCHEMES") ||
2320 !_cups_strcasecmp(token
, "WITH-HOSTNAME") ||
2321 !_cups_strcasecmp(token
, "WITH-RESOURCE") ||
2322 !_cups_strcasecmp(token
, "WITH-SCHEME") ||
2323 !_cups_strcasecmp(token
, "WITH-VALUE"))
2327 if (!_cups_strcasecmp(token
, "WITH-ALL-HOSTNAMES") ||
2328 !_cups_strcasecmp(token
, "WITH-HOSTNAME"))
2329 last_expect
->with_flags
= _CUPS_WITH_HOSTNAME
;
2330 else if (!_cups_strcasecmp(token
, "WITH-ALL-RESOURCES") ||
2331 !_cups_strcasecmp(token
, "WITH-RESOURCE"))
2332 last_expect
->with_flags
= _CUPS_WITH_RESOURCE
;
2333 else if (!_cups_strcasecmp(token
, "WITH-ALL-SCHEMES") ||
2334 !_cups_strcasecmp(token
, "WITH-SCHEME"))
2335 last_expect
->with_flags
= _CUPS_WITH_SCHEME
;
2337 if (!_cups_strncasecmp(token
, "WITH-ALL-", 9))
2338 last_expect
->with_flags
|= _CUPS_WITH_ALL
;
2341 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
2343 print_fatal_error(outfile
, "Missing %s value on line %d.", token
, linenum
);
2351 * Expand any variables in the value and then save it.
2354 expand_variables(vars
, token
, temp
, sizeof(token
));
2356 tokenptr
= token
+ strlen(token
) - 1;
2358 if (token
[0] == '/' && tokenptr
> token
&& *tokenptr
== '/')
2361 * WITH-VALUE is a POSIX extended regular expression.
2364 last_expect
->with_value
= calloc(1, (size_t)(tokenptr
- token
));
2365 last_expect
->with_flags
|= _CUPS_WITH_REGEX
;
2367 if (last_expect
->with_value
)
2368 memcpy(last_expect
->with_value
, token
+ 1, (size_t)(tokenptr
- token
- 1));
2373 * WITH-VALUE is a literal value...
2376 char *ptr
; /* Pointer into value */
2378 for (ptr
= token
; *ptr
; ptr
++)
2380 if (*ptr
== '\\' && ptr
[1])
2383 * Remove \ from \foo...
2386 _cups_strcpy(ptr
, ptr
+ 1);
2390 last_expect
->with_value
= strdup(token
);
2391 last_expect
->with_flags
|= _CUPS_WITH_LITERAL
;
2396 print_fatal_error(outfile
, "%s without a preceding EXPECT on line %d.", token
,
2402 else if (!_cups_strcasecmp(token
, "WITH-VALUE-FROM"))
2404 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
2406 print_fatal_error(outfile
, "Missing %s value on line %d.", token
, linenum
);
2414 * Expand any variables in the value and then save it.
2417 expand_variables(vars
, token
, temp
, sizeof(token
));
2419 last_expect
->with_value_from
= strdup(token
);
2420 last_expect
->with_flags
= _CUPS_WITH_LITERAL
;
2424 print_fatal_error(outfile
, "%s without a preceding EXPECT on line %d.", token
,
2430 else if (!_cups_strcasecmp(token
, "DISPLAY"))
2433 * Display attributes...
2436 if (num_displayed
>= (int)(sizeof(displayed
) / sizeof(displayed
[0])))
2438 print_fatal_error(outfile
, "Too many DISPLAY's on line %d", linenum
);
2443 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2445 print_fatal_error(outfile
, "Missing DISPLAY name on line %d.", linenum
);
2450 displayed
[num_displayed
] = strdup(token
);
2455 print_fatal_error(outfile
, "Unexpected token %s seen on line %d.", token
,
2463 * Submit the IPP request...
2468 ippSetVersion(request
, version
/ 10, version
% 10);
2469 ippSetOperation(request
, op
);
2470 ippSetRequestId(request
, request_id
);
2472 if (Output
== _CUPS_OUTPUT_PLIST
)
2474 fputs("<dict>\n", outfile
);
2475 fputs("<key>Name</key>\n", outfile
);
2476 print_xml_string(outfile
, "string", name
);
2479 fputs("<key>FileId</key>\n", outfile
);
2480 print_xml_string(outfile
, "string", file_id
);
2484 fputs("<key>TestId</key>\n", outfile
);
2485 print_xml_string(outfile
, "string", test_id
);
2487 fputs("<key>Version</key>\n", outfile
);
2488 fprintf(outfile
, "<string>%d.%d</string>\n", version
/ 10, version
% 10);
2489 fputs("<key>Operation</key>\n", outfile
);
2490 print_xml_string(outfile
, "string", ippOpString(op
));
2491 fputs("<key>RequestId</key>\n", outfile
);
2492 fprintf(outfile
, "<integer>%d</integer>\n", request_id
);
2493 fputs("<key>RequestAttributes</key>\n", outfile
);
2494 fputs("<array>\n", outfile
);
2497 fputs("<dict>\n", outfile
);
2498 for (attrptr
= request
->attrs
,
2499 group
= attrptr
? attrptr
->group_tag
: IPP_TAG_ZERO
;
2501 attrptr
= attrptr
->next
)
2502 print_attr(outfile
, Output
, attrptr
, &group
);
2503 fputs("</dict>\n", outfile
);
2505 fputs("</array>\n", outfile
);
2508 if (Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
!= stdout
))
2512 printf(" %s:\n", ippOpString(op
));
2514 for (attrptr
= request
->attrs
; attrptr
; attrptr
= attrptr
->next
)
2515 print_attr(stdout
, _CUPS_OUTPUT_TEST
, attrptr
, NULL
);
2518 printf(" %-68.68s [", name
);
2522 if ((skip_previous
&& !prev_pass
) || skip_test
)
2529 if (Output
== _CUPS_OUTPUT_PLIST
)
2531 fputs("<key>Successful</key>\n", outfile
);
2532 fputs("<true />\n", outfile
);
2533 fputs("<key>Skipped</key>\n", outfile
);
2534 fputs("<true />\n", outfile
);
2535 fputs("<key>StatusCode</key>\n", outfile
);
2536 print_xml_string(outfile
, "string", "skip");
2537 fputs("<key>ResponseAttributes</key>\n", outfile
);
2538 fputs("<dict />\n", outfile
);
2541 if (Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
!= stdout
))
2549 repeat_interval
= 1;
2556 status
= HTTP_STATUS_OK
;
2558 if (transfer
== _CUPS_TRANSFER_CHUNKED
||
2559 (transfer
== _CUPS_TRANSFER_AUTO
&& filename
[0]))
2562 * Send request using chunking - a 0 length means "chunk".
2570 * Send request using content length...
2573 length
= ippLength(request
);
2575 if (filename
[0] && (reqfile
= cupsFileOpen(filename
, "r")) != NULL
)
2578 * Read the file to get the uncompressed file size...
2581 while ((bytes
= cupsFileRead(reqfile
, buffer
, sizeof(buffer
))) > 0)
2582 length
+= (size_t)bytes
;
2584 cupsFileClose(reqfile
);
2589 * Send the request...
2596 if (status
!= HTTP_STATUS_ERROR
)
2598 while (!response
&& !Cancel
&& prev_pass
)
2600 status
= cupsSendRequest(http
, request
, resource
, length
);
2604 httpSetField(http
, HTTP_FIELD_CONTENT_ENCODING
, compression
);
2605 #endif /* HAVE_LIBZ */
2607 if (!Cancel
&& status
== HTTP_STATUS_CONTINUE
&&
2608 request
->state
== IPP_DATA
&& filename
[0])
2610 if ((reqfile
= cupsFileOpen(filename
, "r")) != NULL
)
2613 (bytes
= cupsFileRead(reqfile
, buffer
, sizeof(buffer
))) > 0)
2614 if ((status
= cupsWriteRequestData(http
, buffer
, (size_t)bytes
)) != HTTP_STATUS_CONTINUE
)
2617 cupsFileClose(reqfile
);
2621 snprintf(buffer
, sizeof(buffer
), "%s: %s", filename
,
2623 _cupsSetError(IPP_INTERNAL_ERROR
, buffer
, 0);
2625 status
= HTTP_STATUS_ERROR
;
2630 * Get the server's response...
2633 if (!Cancel
&& status
!= HTTP_STATUS_ERROR
)
2635 response
= cupsGetResponse(http
, resource
);
2636 status
= httpGetStatus(http
);
2639 if (!Cancel
&& status
== HTTP_STATUS_ERROR
&& http
->error
!= EINVAL
&&
2641 http
->error
!= WSAETIMEDOUT
)
2643 http
->error
!= ETIMEDOUT
)
2646 if (httpReconnect(http
))
2649 else if (status
== HTTP_STATUS_ERROR
|| status
== HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
)
2654 else if (status
!= HTTP_STATUS_OK
)
2658 if (status
== HTTP_STATUS_UNAUTHORIZED
)
2666 if (!Cancel
&& status
== HTTP_STATUS_ERROR
&& http
->error
!= EINVAL
&&
2668 http
->error
!= WSAETIMEDOUT
)
2670 http
->error
!= ETIMEDOUT
)
2673 if (httpReconnect(http
))
2676 else if (status
== HTTP_STATUS_ERROR
)
2679 httpReconnect(http
);
2683 else if (status
!= HTTP_STATUS_OK
)
2690 * Check results of request...
2693 cupsArrayClear(errors
);
2695 if (http
->version
!= HTTP_1_1
)
2696 add_stringf(errors
, "Bad HTTP version (%d.%d)", http
->version
/ 100,
2697 http
->version
% 100);
2702 * No response, log error...
2705 add_stringf(errors
, "IPP request failed with status %s (%s)",
2706 ippErrorString(cupsLastError()),
2707 cupsLastErrorString());
2712 * Collect common attribute values...
2715 if ((attrptr
= ippFindAttribute(response
, "job-id",
2716 IPP_TAG_INTEGER
)) != NULL
)
2718 snprintf(temp
, sizeof(temp
), "%d", attrptr
->values
[0].integer
);
2719 set_variable(outfile
, vars
, "job-id", temp
);
2722 if ((attrptr
= ippFindAttribute(response
, "job-uri",
2723 IPP_TAG_URI
)) != NULL
)
2724 set_variable(outfile
, vars
, "job-uri", attrptr
->values
[0].string
.text
);
2726 if ((attrptr
= ippFindAttribute(response
, "notify-subscription-id",
2727 IPP_TAG_INTEGER
)) != NULL
)
2729 snprintf(temp
, sizeof(temp
), "%d", attrptr
->values
[0].integer
);
2730 set_variable(outfile
, vars
, "notify-subscription-id", temp
);
2734 * Check response, validating groups and attributes and logging errors
2738 if (response
->state
!= IPP_DATA
)
2740 "Missing end-of-attributes-tag in response "
2741 "(RFC 2910 section 3.5.1)");
2744 (response
->request
.status
.version
[0] != (version
/ 10) ||
2745 response
->request
.status
.version
[1] != (version
% 10)))
2747 "Bad version %d.%d in response - expected %d.%d "
2748 "(RFC 2911 section 3.1.8).",
2749 response
->request
.status
.version
[0],
2750 response
->request
.status
.version
[1],
2751 version
/ 10, version
% 10);
2753 if (response
->request
.status
.request_id
!= request_id
)
2755 "Bad request ID %d in response - expected %d "
2756 "(RFC 2911 section 3.1.1)",
2757 response
->request
.status
.request_id
, request_id
);
2759 attrptr
= response
->attrs
;
2762 "Missing first attribute \"attributes-charset "
2763 "(charset)\" in group operation-attributes-tag "
2764 "(RFC 2911 section 3.1.4).");
2767 if (!attrptr
->name
||
2768 attrptr
->value_tag
!= IPP_TAG_CHARSET
||
2769 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2770 attrptr
->num_values
!= 1 ||
2771 strcmp(attrptr
->name
, "attributes-charset"))
2773 "Bad first attribute \"%s (%s%s)\" in group %s, "
2774 "expected \"attributes-charset (charset)\" in "
2775 "group operation-attributes-tag (RFC 2911 section "
2777 attrptr
->name
? attrptr
->name
: "(null)",
2778 attrptr
->num_values
> 1 ? "1setOf " : "",
2779 ippTagString(attrptr
->value_tag
),
2780 ippTagString(attrptr
->group_tag
));
2782 attrptr
= attrptr
->next
;
2785 "Missing second attribute \"attributes-natural-"
2786 "language (naturalLanguage)\" in group "
2787 "operation-attributes-tag (RFC 2911 section "
2789 else if (!attrptr
->name
||
2790 attrptr
->value_tag
!= IPP_TAG_LANGUAGE
||
2791 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2792 attrptr
->num_values
!= 1 ||
2793 strcmp(attrptr
->name
, "attributes-natural-language"))
2795 "Bad first attribute \"%s (%s%s)\" in group %s, "
2796 "expected \"attributes-natural-language "
2797 "(naturalLanguage)\" in group "
2798 "operation-attributes-tag (RFC 2911 section "
2800 attrptr
->name
? attrptr
->name
: "(null)",
2801 attrptr
->num_values
> 1 ? "1setOf " : "",
2802 ippTagString(attrptr
->value_tag
),
2803 ippTagString(attrptr
->group_tag
));
2806 if ((attrptr
= ippFindAttribute(response
, "status-message",
2807 IPP_TAG_ZERO
)) != NULL
)
2809 if (attrptr
->value_tag
!= IPP_TAG_TEXT
)
2811 "status-message (text(255)) has wrong value tag "
2812 "%s (RFC 2911 section 3.1.6.2).",
2813 ippTagString(attrptr
->value_tag
));
2814 if (attrptr
->group_tag
!= IPP_TAG_OPERATION
)
2816 "status-message (text(255)) has wrong group tag "
2817 "%s (RFC 2911 section 3.1.6.2).",
2818 ippTagString(attrptr
->group_tag
));
2819 if (attrptr
->num_values
!= 1)
2821 "status-message (text(255)) has %d values "
2822 "(RFC 2911 section 3.1.6.2).",
2823 attrptr
->num_values
);
2824 if (attrptr
->value_tag
== IPP_TAG_TEXT
&&
2825 strlen(attrptr
->values
[0].string
.text
) > 255)
2827 "status-message (text(255)) has bad length %d"
2828 " (RFC 2911 section 3.1.6.2).",
2829 (int)strlen(attrptr
->values
[0].string
.text
));
2832 if ((attrptr
= ippFindAttribute(response
, "detailed-status-message",
2833 IPP_TAG_ZERO
)) != NULL
)
2835 if (attrptr
->value_tag
!= IPP_TAG_TEXT
)
2837 "detailed-status-message (text(MAX)) has wrong "
2838 "value tag %s (RFC 2911 section 3.1.6.3).",
2839 ippTagString(attrptr
->value_tag
));
2840 if (attrptr
->group_tag
!= IPP_TAG_OPERATION
)
2842 "detailed-status-message (text(MAX)) has wrong "
2843 "group tag %s (RFC 2911 section 3.1.6.3).",
2844 ippTagString(attrptr
->group_tag
));
2845 if (attrptr
->num_values
!= 1)
2847 "detailed-status-message (text(MAX)) has %d values"
2848 " (RFC 2911 section 3.1.6.3).",
2849 attrptr
->num_values
);
2850 if (attrptr
->value_tag
== IPP_TAG_TEXT
&&
2851 strlen(attrptr
->values
[0].string
.text
) > 1023)
2853 "detailed-status-message (text(MAX)) has bad "
2854 "length %d (RFC 2911 section 3.1.6.3).",
2855 (int)strlen(attrptr
->values
[0].string
.text
));
2858 a
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
2860 for (attrptr
= response
->attrs
,
2861 group
= attrptr
? attrptr
->group_tag
: IPP_TAG_ZERO
;
2863 attrptr
= attrptr
->next
)
2865 if (attrptr
->group_tag
!= group
)
2867 int out_of_order
= 0; /* Are attribute groups out-of-order? */
2870 switch (attrptr
->group_tag
)
2875 case IPP_TAG_OPERATION
:
2879 case IPP_TAG_UNSUPPORTED_GROUP
:
2880 if (group
!= IPP_TAG_OPERATION
)
2885 case IPP_TAG_PRINTER
:
2886 if (group
!= IPP_TAG_OPERATION
&&
2887 group
!= IPP_TAG_UNSUPPORTED_GROUP
)
2891 case IPP_TAG_SUBSCRIPTION
:
2892 if (group
> attrptr
->group_tag
&&
2893 group
!= IPP_TAG_DOCUMENT
)
2898 if (group
> attrptr
->group_tag
)
2904 add_stringf(errors
, "Attribute groups out of order (%s < %s)",
2905 ippTagString(attrptr
->group_tag
),
2906 ippTagString(group
));
2908 if (attrptr
->group_tag
!= IPP_TAG_ZERO
)
2909 group
= attrptr
->group_tag
;
2912 validate_attr(outfile
, errors
, attrptr
);
2916 if (cupsArrayFind(a
, attrptr
->name
))
2917 add_stringf(errors
, "Duplicate \"%s\" attribute in %s group",
2918 attrptr
->name
, ippTagString(group
));
2920 cupsArrayAdd(a
, attrptr
->name
);
2927 * Now check the test-defined expected status-code and attribute
2931 for (i
= 0; i
< num_statuses
; i
++)
2933 if (statuses
[i
].if_defined
&&
2934 !get_variable(vars
, statuses
[i
].if_defined
))
2937 if (statuses
[i
].if_not_defined
&&
2938 get_variable(vars
, statuses
[i
].if_not_defined
))
2941 if (response
->request
.status
.status_code
== statuses
[i
].status
)
2943 if (statuses
[i
].repeat_match
&&
2944 repeat_count
< statuses
[i
].repeat_limit
)
2947 if (statuses
[i
].define_match
)
2948 set_variable(outfile
, vars
, statuses
[i
].define_match
, "1");
2954 if (statuses
[i
].repeat_no_match
&&
2955 repeat_count
< statuses
[i
].repeat_limit
)
2958 if (statuses
[i
].define_no_match
)
2960 set_variable(outfile
, vars
, statuses
[i
].define_no_match
, "1");
2966 if (i
== num_statuses
&& num_statuses
> 0)
2968 for (i
= 0; i
< num_statuses
; i
++)
2970 if (statuses
[i
].if_defined
&&
2971 !get_variable(vars
, statuses
[i
].if_defined
))
2974 if (statuses
[i
].if_not_defined
&&
2975 get_variable(vars
, statuses
[i
].if_not_defined
))
2978 if (!statuses
[i
].repeat_match
)
2979 add_stringf(errors
, "EXPECTED: STATUS %s (got %s)",
2980 ippErrorString(statuses
[i
].status
),
2981 ippErrorString(cupsLastError()));
2984 if ((attrptr
= ippFindAttribute(response
, "status-message",
2985 IPP_TAG_TEXT
)) != NULL
)
2986 add_stringf(errors
, "status-message=\"%s\"",
2987 attrptr
->values
[0].string
.text
);
2990 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
2992 if (expect
->if_defined
&& !get_variable(vars
, expect
->if_defined
))
2995 if (expect
->if_not_defined
&&
2996 get_variable(vars
, expect
->if_not_defined
))
2999 found
= ippFindAttribute(response
, expect
->name
, IPP_TAG_ZERO
);
3003 if ((found
&& expect
->not_expect
) ||
3004 (!found
&& !(expect
->not_expect
|| expect
->optional
)) ||
3005 (found
&& !expect_matches(expect
, found
->value_tag
)) ||
3006 (found
&& expect
->in_group
&&
3007 found
->group_tag
!= expect
->in_group
))
3009 if (expect
->define_no_match
)
3010 set_variable(outfile
, vars
, expect
->define_no_match
, "1");
3011 else if (!expect
->define_match
&& !expect
->define_value
)
3013 if (found
&& expect
->not_expect
)
3014 add_stringf(errors
, "NOT EXPECTED: %s", expect
->name
);
3015 else if (!found
&& !(expect
->not_expect
|| expect
->optional
))
3016 add_stringf(errors
, "EXPECTED: %s", expect
->name
);
3019 if (!expect_matches(expect
, found
->value_tag
))
3020 add_stringf(errors
, "EXPECTED: %s OF-TYPE %s (got %s)",
3021 expect
->name
, expect
->of_type
,
3022 ippTagString(found
->value_tag
));
3024 if (expect
->in_group
&& found
->group_tag
!= expect
->in_group
)
3025 add_stringf(errors
, "EXPECTED: %s IN-GROUP %s (got %s).",
3026 expect
->name
, ippTagString(expect
->in_group
),
3027 ippTagString(found
->group_tag
));
3031 if (expect
->repeat_no_match
&&
3032 repeat_count
< expect
->repeat_limit
)
3039 ippAttributeString(found
, buffer
, sizeof(buffer
));
3041 if (found
&& expect
->with_value_from
&& !with_value_from(NULL
, ippFindAttribute(response
, expect
->with_value_from
, IPP_TAG_ZERO
), found
, buffer
, sizeof(buffer
)))
3043 if (expect
->define_no_match
)
3044 set_variable(outfile
, vars
, expect
->define_no_match
, "1");
3045 else if (!expect
->define_match
&& !expect
->define_value
&& !expect
->repeat_match
&& !expect
->repeat_no_match
)
3047 add_stringf(errors
, "EXPECTED: %s WITH-VALUES-FROM %s", expect
->name
, expect
->with_value_from
);
3049 with_value_from(errors
, ippFindAttribute(response
, expect
->with_value_from
, IPP_TAG_ZERO
), found
, buffer
, sizeof(buffer
));
3052 if (expect
->repeat_no_match
&& repeat_count
< expect
->repeat_limit
)
3057 else if (found
&& !with_value(outfile
, NULL
, expect
->with_value
, expect
->with_flags
, found
, buffer
, sizeof(buffer
)))
3059 if (expect
->define_no_match
)
3060 set_variable(outfile
, vars
, expect
->define_no_match
, "1");
3061 else if (!expect
->define_match
&& !expect
->define_value
&&
3062 !expect
->repeat_match
&& !expect
->repeat_no_match
)
3064 if (expect
->with_flags
& _CUPS_WITH_REGEX
)
3065 add_stringf(errors
, "EXPECTED: %s %s /%s/",
3067 (expect
->with_flags
& _CUPS_WITH_ALL
) ?
3068 "WITH-ALL-VALUES" : "WITH-VALUE",
3069 expect
->with_value
);
3071 add_stringf(errors
, "EXPECTED: %s %s \"%s\"",
3073 (expect
->with_flags
& _CUPS_WITH_ALL
) ?
3074 "WITH-ALL-VALUES" : "WITH-VALUE",
3075 expect
->with_value
);
3077 with_value(outfile
, errors
, expect
->with_value
, expect
->with_flags
, found
,
3078 buffer
, sizeof(buffer
));
3081 if (expect
->repeat_no_match
&&
3082 repeat_count
< expect
->repeat_limit
)
3088 if (found
&& expect
->count
> 0 &&
3089 found
->num_values
!= expect
->count
)
3091 if (expect
->define_no_match
)
3092 set_variable(outfile
, vars
, expect
->define_no_match
, "1");
3093 else if (!expect
->define_match
&& !expect
->define_value
)
3095 add_stringf(errors
, "EXPECTED: %s COUNT %d (got %d)", expect
->name
,
3096 expect
->count
, found
->num_values
);
3099 if (expect
->repeat_no_match
&&
3100 repeat_count
< expect
->repeat_limit
)
3106 if (found
&& expect
->same_count_as
)
3108 attrptr
= ippFindAttribute(response
, expect
->same_count_as
,
3111 if (!attrptr
|| attrptr
->num_values
!= found
->num_values
)
3113 if (expect
->define_no_match
)
3114 set_variable(outfile
, vars
, expect
->define_no_match
, "1");
3115 else if (!expect
->define_match
&& !expect
->define_value
)
3119 "EXPECTED: %s (%d values) SAME-COUNT-AS %s "
3120 "(not returned)", expect
->name
,
3121 found
->num_values
, expect
->same_count_as
);
3122 else if (attrptr
->num_values
!= found
->num_values
)
3124 "EXPECTED: %s (%d values) SAME-COUNT-AS %s "
3125 "(%d values)", expect
->name
, found
->num_values
,
3126 expect
->same_count_as
, attrptr
->num_values
);
3129 if (expect
->repeat_no_match
&&
3130 repeat_count
< expect
->repeat_limit
)
3137 if (found
&& expect
->define_match
)
3138 set_variable(outfile
, vars
, expect
->define_match
, "1");
3140 if (found
&& expect
->define_value
)
3142 if (!expect
->with_value
)
3144 int last
= ippGetCount(found
) - 1;
3145 /* Last element in attribute */
3147 switch (ippGetValueTag(found
))
3150 case IPP_TAG_INTEGER
:
3151 snprintf(buffer
, sizeof(buffer
), "%d", ippGetInteger(found
, last
));
3154 case IPP_TAG_BOOLEAN
:
3155 if (ippGetBoolean(found
, last
))
3156 strlcpy(buffer
, "true", sizeof(buffer
));
3158 strlcpy(buffer
, "false", sizeof(buffer
));
3161 case IPP_TAG_RESOLUTION
:
3163 int xres
, /* Horizontal resolution */
3164 yres
; /* Vertical resolution */
3165 ipp_res_t units
; /* Resolution units */
3167 xres
= ippGetResolution(found
, last
, &yres
, &units
);
3170 snprintf(buffer
, sizeof(buffer
), "%d%s", xres
, units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
3172 snprintf(buffer
, sizeof(buffer
), "%dx%d%s", xres
, yres
, units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
3176 case IPP_TAG_CHARSET
:
3177 case IPP_TAG_KEYWORD
:
3178 case IPP_TAG_LANGUAGE
:
3179 case IPP_TAG_MIMETYPE
:
3181 case IPP_TAG_NAMELANG
:
3183 case IPP_TAG_TEXTLANG
:
3185 case IPP_TAG_URISCHEME
:
3186 strlcpy(buffer
, ippGetString(found
, last
, NULL
), sizeof(buffer
));
3190 ippAttributeString(found
, buffer
, sizeof(buffer
));
3195 set_variable(outfile
, vars
, expect
->define_value
, buffer
);
3198 if (found
&& expect
->repeat_match
&&
3199 repeat_count
< expect
->repeat_limit
)
3202 while (expect
->expect_all
&& (found
= ippFindNextAttribute(response
, expect
->name
, IPP_TAG_ZERO
)) != NULL
);
3207 * If we are going to repeat this test, sleep 1 second so we don't flood
3208 * the printer with requests...
3213 if (Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
!= stdout
))
3215 printf("%04d]\n", repeat_count
);
3218 if (num_displayed
> 0)
3220 for (attrptr
= ippFirstAttribute(response
); attrptr
; attrptr
= ippNextAttribute(response
))
3222 const char *attrname
= ippGetName(attrptr
);
3225 for (i
= 0; i
< num_displayed
; i
++)
3227 if (!strcmp(displayed
[i
], attrname
))
3229 print_attr(stdout
, _CUPS_OUTPUT_TEST
, attrptr
, NULL
);
3238 sleep((unsigned)repeat_interval
);
3239 repeat_interval
= _cupsNextDelay(repeat_interval
, &repeat_prev
);
3241 if (Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
!= stdout
))
3243 printf(" %-68.68s [", name
);
3248 while (repeat_test
);
3254 if (cupsArrayCount(errors
) > 0)
3255 prev_pass
= pass
= 0;
3262 if (Output
== _CUPS_OUTPUT_PLIST
)
3264 fputs("<key>Successful</key>\n", outfile
);
3265 fputs(prev_pass
? "<true />\n" : "<false />\n", outfile
);
3266 fputs("<key>StatusCode</key>\n", outfile
);
3267 print_xml_string(outfile
, "string", ippErrorString(cupsLastError()));
3268 fputs("<key>ResponseAttributes</key>\n", outfile
);
3269 fputs("<array>\n", outfile
);
3270 fputs("<dict>\n", outfile
);
3271 for (attrptr
= response
? response
->attrs
: NULL
,
3272 group
= attrptr
? attrptr
->group_tag
: IPP_TAG_ZERO
;
3274 attrptr
= attrptr
->next
)
3275 print_attr(outfile
, Output
, attrptr
, &group
);
3276 fputs("</dict>\n", outfile
);
3277 fputs("</array>\n", outfile
);
3280 if (Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
!= stdout
))
3282 puts(prev_pass
? "PASS]" : "FAIL]");
3284 if (!prev_pass
|| (Verbosity
&& response
))
3286 printf(" RECEIVED: %lu bytes in response\n",
3287 (unsigned long)ippLength(response
));
3288 printf(" status-code = %s (%s)\n", ippErrorString(cupsLastError()),
3289 cupsLastErrorString());
3291 if (Verbosity
&& response
)
3293 for (attrptr
= response
->attrs
;
3295 attrptr
= attrptr
->next
)
3296 print_attr(stdout
, _CUPS_OUTPUT_TEST
, attrptr
, NULL
);
3300 else if (!prev_pass
&& Output
!= _CUPS_OUTPUT_QUIET
)
3301 fprintf(stderr
, "%s\n", cupsLastErrorString());
3303 if (prev_pass
&& Output
>= _CUPS_OUTPUT_LIST
&& !Verbosity
&&
3306 size_t width
; /* Length of value */
3308 for (i
= 0; i
< num_displayed
; i
++)
3310 widths
[i
] = strlen(displayed
[i
]);
3312 for (attrptr
= ippFindAttribute(response
, displayed
[i
], IPP_TAG_ZERO
);
3314 attrptr
= ippFindNextAttribute(response
, displayed
[i
],
3317 width
= ippAttributeString(attrptr
, NULL
, 0);
3318 if (width
> widths
[i
])
3323 if (Output
== _CUPS_OUTPUT_CSV
)
3324 print_csv(outfile
, NULL
, num_displayed
, displayed
, widths
);
3326 print_line(outfile
, NULL
, num_displayed
, displayed
, widths
);
3328 attrptr
= response
->attrs
;
3332 while (attrptr
&& attrptr
->group_tag
<= IPP_TAG_OPERATION
)
3333 attrptr
= attrptr
->next
;
3337 if (Output
== _CUPS_OUTPUT_CSV
)
3338 print_csv(outfile
, attrptr
, num_displayed
, displayed
, widths
);
3340 print_line(outfile
, attrptr
, num_displayed
, displayed
, widths
);
3342 while (attrptr
&& attrptr
->group_tag
> IPP_TAG_OPERATION
)
3343 attrptr
= attrptr
->next
;
3347 else if (!prev_pass
)
3349 if (Output
== _CUPS_OUTPUT_PLIST
)
3351 fputs("<key>Errors</key>\n", outfile
);
3352 fputs("<array>\n", outfile
);
3354 for (error
= (char *)cupsArrayFirst(errors
);
3356 error
= (char *)cupsArrayNext(errors
))
3357 print_xml_string(outfile
, "string", error
);
3359 fputs("</array>\n", outfile
);
3362 if (Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
!= stdout
))
3364 for (error
= (char *)cupsArrayFirst(errors
);
3366 error
= (char *)cupsArrayNext(errors
))
3367 printf(" %s\n", error
);
3371 if (num_displayed
> 0 && !Verbosity
&& response
&& (Output
== _CUPS_OUTPUT_TEST
|| (Output
== _CUPS_OUTPUT_PLIST
&& outfile
!= stdout
)))
3373 for (attrptr
= response
->attrs
;
3375 attrptr
= attrptr
->next
)
3379 for (i
= 0; i
< num_displayed
; i
++)
3381 if (!strcmp(displayed
[i
], attrptr
->name
))
3383 print_attr(outfile
, Output
, attrptr
, NULL
);
3393 if (Output
== _CUPS_OUTPUT_PLIST
)
3394 fputs("</dict>\n", outfile
);
3398 ippDelete(response
);
3401 for (i
= 0; i
< num_statuses
; i
++)
3403 if (statuses
[i
].if_defined
)
3404 free(statuses
[i
].if_defined
);
3405 if (statuses
[i
].if_not_defined
)
3406 free(statuses
[i
].if_not_defined
);
3407 if (statuses
[i
].define_match
)
3408 free(statuses
[i
].define_match
);
3409 if (statuses
[i
].define_no_match
)
3410 free(statuses
[i
].define_no_match
);
3414 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
3417 if (expect
->of_type
)
3418 free(expect
->of_type
);
3419 if (expect
->same_count_as
)
3420 free(expect
->same_count_as
);
3421 if (expect
->if_defined
)
3422 free(expect
->if_defined
);
3423 if (expect
->if_not_defined
)
3424 free(expect
->if_not_defined
);
3425 if (expect
->with_value
)
3426 free(expect
->with_value
);
3427 if (expect
->define_match
)
3428 free(expect
->define_match
);
3429 if (expect
->define_no_match
)
3430 free(expect
->define_no_match
);
3431 if (expect
->define_value
)
3432 free(expect
->define_value
);
3436 for (i
= 0; i
< num_displayed
; i
++)
3440 if (!ignore_errors
&& !prev_pass
)
3446 cupsArrayDelete(errors
);
3453 ippDelete(response
);
3455 for (i
= 0; i
< num_statuses
; i
++)
3457 if (statuses
[i
].if_defined
)
3458 free(statuses
[i
].if_defined
);
3459 if (statuses
[i
].if_not_defined
)
3460 free(statuses
[i
].if_not_defined
);
3461 if (statuses
[i
].define_match
)
3462 free(statuses
[i
].define_match
);
3463 if (statuses
[i
].define_no_match
)
3464 free(statuses
[i
].define_no_match
);
3467 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
3470 if (expect
->of_type
)
3471 free(expect
->of_type
);
3472 if (expect
->same_count_as
)
3473 free(expect
->same_count_as
);
3474 if (expect
->if_defined
)
3475 free(expect
->if_defined
);
3476 if (expect
->if_not_defined
)
3477 free(expect
->if_not_defined
);
3478 if (expect
->with_value
)
3479 free(expect
->with_value
);
3480 if (expect
->define_match
)
3481 free(expect
->define_match
);
3482 if (expect
->define_no_match
)
3483 free(expect
->define_no_match
);
3484 if (expect
->define_value
)
3485 free(expect
->define_value
);
3488 for (i
= 0; i
< num_displayed
; i
++)
3496 * 'expand_variables()' - Expand variables in a string.
3500 expand_variables(_cups_vars_t
*vars
, /* I - Variables */
3501 char *dst
, /* I - Destination string buffer */
3502 const char *src
, /* I - Source string */
3503 size_t dstsize
) /* I - Size of destination buffer */
3505 char *dstptr
, /* Pointer into destination */
3506 *dstend
, /* End of destination */
3507 temp
[256], /* Temporary string */
3508 *tempptr
; /* Pointer into temporary string */
3509 const char *value
; /* Value to substitute */
3513 dstend
= dst
+ dstsize
- 1;
3515 while (*src
&& dstptr
< dstend
)
3520 * Substitute a string/number...
3523 if (!strncmp(src
, "$$", 2))
3528 else if (!strncmp(src
, "$ENV[", 5))
3530 strlcpy(temp
, src
+ 5, sizeof(temp
));
3532 for (tempptr
= temp
; *tempptr
; tempptr
++)
3533 if (*tempptr
== ']')
3539 value
= getenv(temp
);
3540 src
+= tempptr
- temp
+ 5;
3547 strlcpy(temp
, src
, sizeof(temp
));
3548 if ((tempptr
= strchr(temp
, '}')) != NULL
)
3551 tempptr
= temp
+ strlen(temp
);
3555 strlcpy(temp
, src
+ 1, sizeof(temp
));
3557 for (tempptr
= temp
; *tempptr
; tempptr
++)
3558 if (!isalnum(*tempptr
& 255) && *tempptr
!= '-' && *tempptr
!= '_')
3565 if (!strcmp(temp
, "uri"))
3567 else if (!strcmp(temp
, "filename"))
3568 value
= vars
->filename
;
3569 else if (!strcmp(temp
, "scheme") || !strcmp(temp
, "method"))
3570 value
= vars
->scheme
;
3571 else if (!strcmp(temp
, "username"))
3572 value
= vars
->userpass
;
3573 else if (!strcmp(temp
, "hostname"))
3574 value
= vars
->hostname
;
3575 else if (!strcmp(temp
, "port"))
3577 snprintf(temp
, sizeof(temp
), "%d", vars
->port
);
3580 else if (!strcmp(temp
, "resource"))
3581 value
= vars
->resource
;
3582 else if (!strcmp(temp
, "user"))
3585 value
= get_variable(vars
, temp
);
3587 src
+= tempptr
- temp
+ 1;
3592 strlcpy(dstptr
, value
, (size_t)(dstend
- dstptr
+ 1));
3593 dstptr
+= strlen(dstptr
);
3605 * 'expect_matches()' - Return true if the tag matches the specification.
3608 static int /* O - 1 if matches, 0 otherwise */
3610 _cups_expect_t
*expect
, /* I - Expected attribute */
3611 ipp_tag_t value_tag
) /* I - Value tag for attribute */
3613 int match
; /* Match? */
3614 char *of_type
, /* Type name to match */
3615 *next
, /* Next name to match */
3616 sep
; /* Separator character */
3620 * If we don't expect a particular type, return immediately...
3623 if (!expect
->of_type
)
3627 * Parse the "of_type" value since the string can contain multiple attribute
3628 * types separated by "," or "|"...
3631 for (of_type
= expect
->of_type
, match
= 0; !match
&& *of_type
; of_type
= next
)
3634 * Find the next separator, and set it (temporarily) to nul if present.
3637 for (next
= of_type
; *next
&& *next
!= '|' && *next
!= ','; next
++);
3639 if ((sep
= *next
) != '\0')
3643 * Support some meta-types to make it easier to write the test file.
3646 if (!strcmp(of_type
, "text"))
3647 match
= value_tag
== IPP_TAG_TEXTLANG
|| value_tag
== IPP_TAG_TEXT
;
3648 else if (!strcmp(of_type
, "name"))
3649 match
= value_tag
== IPP_TAG_NAMELANG
|| value_tag
== IPP_TAG_NAME
;
3650 else if (!strcmp(of_type
, "collection"))
3651 match
= value_tag
== IPP_TAG_BEGIN_COLLECTION
;
3653 match
= value_tag
== ippTagValue(of_type
);
3656 * Restore the separator if we have one...
3668 * 'get_collection()' - Get a collection value from the current test file.
3671 static ipp_t
* /* O - Collection value */
3672 get_collection(FILE *outfile
, /* I - Output file */
3673 _cups_vars_t
*vars
, /* I - Variables */
3674 FILE *fp
, /* I - File to read from */
3675 int *linenum
) /* IO - Line number */
3677 char token
[1024], /* Token from file */
3678 temp
[1024], /* Temporary string */
3679 attr
[128]; /* Attribute name */
3680 ipp_tag_t value
; /* Current value type */
3681 ipp_t
*col
= ippNew(); /* Collection value */
3682 ipp_attribute_t
*lastcol
= NULL
; /* Last collection attribute */
3685 while (get_token(fp
, token
, sizeof(token
), linenum
) != NULL
)
3687 if (!strcmp(token
, "}"))
3689 else if (!strcmp(token
, "{") && lastcol
)
3692 * Another collection value
3695 ipp_t
*subcol
= get_collection(outfile
, vars
, fp
, linenum
);
3696 /* Collection value */
3699 ippSetCollection(col
, &lastcol
, ippGetCount(lastcol
), subcol
);
3703 else if (!_cups_strcasecmp(token
, "MEMBER"))
3711 if (!get_token(fp
, token
, sizeof(token
), linenum
))
3713 print_fatal_error(outfile
, "Missing MEMBER value tag on line %d.", *linenum
);
3717 if ((value
= ippTagValue(token
)) == IPP_TAG_ZERO
)
3719 print_fatal_error(outfile
, "Bad MEMBER value tag \"%s\" on line %d.", token
,
3724 if (!get_token(fp
, attr
, sizeof(attr
), linenum
))
3726 print_fatal_error(outfile
, "Missing MEMBER name on line %d.", *linenum
);
3730 if (!get_token(fp
, temp
, sizeof(temp
), linenum
))
3732 print_fatal_error(outfile
, "Missing MEMBER value on line %d.", *linenum
);
3736 expand_variables(vars
, token
, temp
, sizeof(token
));
3740 case IPP_TAG_BOOLEAN
:
3741 if (!_cups_strcasecmp(token
, "true"))
3742 ippAddBoolean(col
, IPP_TAG_ZERO
, attr
, 1);
3744 ippAddBoolean(col
, IPP_TAG_ZERO
, attr
, (char)atoi(token
));
3747 case IPP_TAG_INTEGER
:
3749 ippAddInteger(col
, IPP_TAG_ZERO
, value
, attr
, atoi(token
));
3752 case IPP_TAG_RESOLUTION
:
3754 int xres
, /* X resolution */
3755 yres
; /* Y resolution */
3756 char units
[6]; /* Units */
3758 if (sscanf(token
, "%dx%d%5s", &xres
, &yres
, units
) != 3 ||
3759 (_cups_strcasecmp(units
, "dpi") &&
3760 _cups_strcasecmp(units
, "dpc") &&
3761 _cups_strcasecmp(units
, "dpcm") &&
3762 _cups_strcasecmp(units
, "other")))
3764 print_fatal_error(outfile
, "Bad resolution value \"%s\" on line %d.",
3769 if (!_cups_strcasecmp(units
, "dpi"))
3770 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, IPP_RES_PER_INCH
, xres
, yres
);
3771 else if (!_cups_strcasecmp(units
, "dpc") ||
3772 !_cups_strcasecmp(units
, "dpcm"))
3773 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, IPP_RES_PER_CM
, xres
, yres
);
3775 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, (ipp_res_t
)0, xres
, yres
);
3779 case IPP_TAG_RANGE
:
3781 int lowers
[4], /* Lower value */
3782 uppers
[4], /* Upper values */
3783 num_vals
; /* Number of values */
3786 num_vals
= sscanf(token
, "%d-%d,%d-%d,%d-%d,%d-%d",
3787 lowers
+ 0, uppers
+ 0,
3788 lowers
+ 1, uppers
+ 1,
3789 lowers
+ 2, uppers
+ 2,
3790 lowers
+ 3, uppers
+ 3);
3792 if ((num_vals
& 1) || num_vals
== 0)
3794 print_fatal_error(outfile
, "Bad rangeOfInteger value \"%s\" on line %d.",
3799 ippAddRanges(col
, IPP_TAG_ZERO
, attr
, num_vals
/ 2, lowers
,
3804 case IPP_TAG_BEGIN_COLLECTION
:
3805 if (!strcmp(token
, "{"))
3807 ipp_t
*subcol
= get_collection(outfile
, vars
, fp
, linenum
);
3808 /* Collection value */
3812 lastcol
= ippAddCollection(col
, IPP_TAG_ZERO
, attr
, subcol
);
3820 print_fatal_error(outfile
, "Bad collection value on line %d.", *linenum
);
3824 case IPP_TAG_STRING
:
3825 ippAddOctetString(col
, IPP_TAG_ZERO
, attr
, token
, (int)strlen(token
));
3829 if (!strchr(token
, ','))
3830 ippAddString(col
, IPP_TAG_ZERO
, value
, attr
, NULL
, token
);
3834 * Multiple string values...
3837 int num_values
; /* Number of values */
3838 char *values
[100], /* Values */
3839 *ptr
; /* Pointer to next value */
3845 for (ptr
= strchr(token
, ','); ptr
; ptr
= strchr(ptr
, ','))
3848 values
[num_values
] = ptr
;
3852 ippAddStrings(col
, IPP_TAG_ZERO
, value
, attr
, num_values
,
3853 NULL
, (const char **)values
);
3863 * If we get here there was a parse error; free memory and return.
3875 * 'get_filename()' - Get a filename based on the current test file.
3878 static char * /* O - Filename */
3879 get_filename(const char *testfile
, /* I - Current test file */
3880 char *dst
, /* I - Destination filename */
3881 const char *src
, /* I - Source filename */
3882 size_t dstsize
) /* I - Size of destination buffer */
3884 char *dstptr
; /* Pointer into destination */
3885 _cups_globals_t
*cg
= _cupsGlobals();
3889 if (*src
== '<' && src
[strlen(src
) - 1] == '>')
3892 * Map <filename> to CUPS_DATADIR/ipptool/filename...
3895 snprintf(dst
, dstsize
, "%s/ipptool/%s", cg
->cups_datadir
, src
+ 1);
3896 dstptr
= dst
+ strlen(dst
) - 1;
3900 else if (*src
== '/' || !strchr(testfile
, '/')
3902 || (isalpha(*src
& 255) && src
[1] == ':')
3907 * Use the path as-is...
3910 strlcpy(dst
, src
, dstsize
);
3915 * Make path relative to testfile...
3918 strlcpy(dst
, testfile
, dstsize
);
3919 if ((dstptr
= strrchr(dst
, '/')) != NULL
)
3922 dstptr
= dst
; /* Should never happen */
3924 strlcpy(dstptr
, src
, dstsize
- (size_t)(dstptr
- dst
));
3932 * 'get_string()' - Get a pointer to a string value or the portion of interest.
3935 static const char * /* O - Pointer to string */
3936 get_string(ipp_attribute_t
*attr
, /* I - IPP attribute */
3937 int element
, /* I - Element to fetch */
3938 int flags
, /* I - Value ("with") flags */
3939 char *buffer
, /* I - Temporary buffer */
3940 size_t bufsize
) /* I - Size of temporary buffer */
3942 const char *value
; /* Value */
3943 char *ptr
, /* Pointer into value */
3944 scheme
[256], /* URI scheme */
3945 userpass
[256], /* Username/password */
3946 hostname
[256], /* Hostname */
3947 resource
[1024]; /* Resource */
3948 int port
; /* Port number */
3951 value
= ippGetString(attr
, element
, NULL
);
3953 if (flags
& _CUPS_WITH_HOSTNAME
)
3955 if (httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), buffer
, (int)bufsize
, &port
, resource
, sizeof(resource
)) < HTTP_URI_STATUS_OK
)
3958 ptr
= buffer
+ strlen(buffer
) - 1;
3959 if (ptr
>= buffer
&& *ptr
== '.')
3960 *ptr
= '\0'; /* Drop trailing "." */
3964 else if (flags
& _CUPS_WITH_RESOURCE
)
3966 if (httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), hostname
, sizeof(hostname
), &port
, buffer
, (int)bufsize
) < HTTP_URI_STATUS_OK
)
3971 else if (flags
& _CUPS_WITH_SCHEME
)
3973 if (httpSeparateURI(HTTP_URI_CODING_ALL
, value
, buffer
, (int)bufsize
, userpass
, sizeof(userpass
), hostname
, sizeof(hostname
), &port
, resource
, sizeof(resource
)) < HTTP_URI_STATUS_OK
)
3978 else if (ippGetValueTag(attr
) == IPP_TAG_URI
&& (!strncmp(value
, "ipp://", 6) || !strncmp(value
, "http://", 7) || !strncmp(value
, "ipps://", 7) || !strncmp(value
, "https://", 8)))
3980 http_uri_status_t status
= httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), hostname
, sizeof(hostname
), &port
, resource
, sizeof(resource
));
3982 if (status
< HTTP_URI_STATUS_OK
)
3993 * Normalize URI with no trailing dot...
3996 if ((ptr
= hostname
+ strlen(hostname
) - 1) >= hostname
&& *ptr
== '.')
3999 httpAssembleURI(HTTP_URI_CODING_ALL
, buffer
, (int)bufsize
, scheme
, userpass
, hostname
, port
, resource
);
4010 * 'get_token()' - Get a token from a file.
4013 static char * /* O - Token from file or NULL on EOF */
4014 get_token(FILE *fp
, /* I - File to read from */
4015 char *buf
, /* I - Buffer to read into */
4016 int buflen
, /* I - Length of buffer */
4017 int *linenum
) /* IO - Current line number */
4019 int ch
, /* Character from file */
4020 quote
; /* Quoting character */
4021 char *bufptr
, /* Pointer into buffer */
4022 *bufend
; /* End of buffer */
4028 * Skip whitespace...
4031 while (isspace(ch
= getc(fp
)))
4043 else if (ch
== '\'' || ch
== '\"')
4046 * Quoted text or regular expression...
4051 bufend
= buf
+ buflen
- 1;
4053 while ((ch
= getc(fp
)) != EOF
)
4058 * Escape next character...
4061 if (bufptr
< bufend
)
4062 *bufptr
++ = (char)ch
;
4064 if ((ch
= getc(fp
)) != EOF
&& bufptr
< bufend
)
4065 *bufptr
++ = (char)ch
;
4067 else if (ch
== quote
)
4069 else if (bufptr
< bufend
)
4070 *bufptr
++ = (char)ch
;
4083 while ((ch
= getc(fp
)) != EOF
)
4089 else if (ch
== '{' || ch
== '}' || ch
== ',')
4099 * Whitespace delimited text...
4105 bufend
= buf
+ buflen
- 1;
4107 while ((ch
= getc(fp
)) != EOF
)
4108 if (isspace(ch
) || ch
== '#')
4110 else if (bufptr
< bufend
)
4111 *bufptr
++ = (char)ch
;
4115 else if (ch
== '\n')
4127 * 'get_variable()' - Get the value of a variable.
4130 static char * /* O - Value or NULL */
4131 get_variable(_cups_vars_t
*vars
, /* I - Variables */
4132 const char *name
) /* I - Variable name */
4134 _cups_var_t key
, /* Search key */
4135 *match
; /* Matching variable, if any */
4138 key
.name
= (char *)name
;
4139 match
= cupsArrayFind(vars
->vars
, &key
);
4141 return (match
? match
->value
: NULL
);
4146 * 'iso_date()' - Return an ISO 8601 date/time string for the given IPP dateTime
4150 static char * /* O - ISO 8601 date/time string */
4151 iso_date(ipp_uchar_t
*date
) /* I - IPP (RFC 1903) date/time value */
4153 time_t utctime
; /* UTC time since 1970 */
4154 struct tm
*utcdate
; /* UTC date/time */
4155 static char buffer
[255]; /* String buffer */
4158 utctime
= ippDateToTime(date
);
4159 utcdate
= gmtime(&utctime
);
4161 snprintf(buffer
, sizeof(buffer
), "%04d-%02d-%02dT%02d:%02d:%02dZ",
4162 utcdate
->tm_year
+ 1900, utcdate
->tm_mon
+ 1, utcdate
->tm_mday
,
4163 utcdate
->tm_hour
, utcdate
->tm_min
, utcdate
->tm_sec
);
4170 * 'password_cb()' - Password callback for authenticated tests.
4173 static const char * /* O - Password */
4174 password_cb(const char *prompt
) /* I - Prompt (unused) */
4178 if (PasswordTries
< 3)
4182 cupsSetUser(Username
);
4192 * 'pause_message()' - Display the message and pause until the user presses a key.
4196 pause_message(const char *message
) /* I - Message */
4199 HANDLE tty
; /* Console handle */
4200 DWORD mode
; /* Console mode */
4201 char key
; /* Key press */
4202 DWORD bytes
; /* Bytes read for key press */
4206 * Disable input echo and set raw input...
4209 if ((tty
= GetStdHandle(STD_INPUT_HANDLE
)) == INVALID_HANDLE_VALUE
)
4212 if (!GetConsoleMode(tty
, &mode
))
4215 if (!SetConsoleMode(tty
, 0))
4219 int tty
; /* /dev/tty - never read from stdin */
4220 struct termios original
, /* Original input mode */
4221 noecho
; /* No echo input mode */
4222 char key
; /* Current key press */
4226 * Disable input echo and set raw input...
4229 if ((tty
= open("/dev/tty", O_RDONLY
)) < 0)
4232 if (tcgetattr(tty
, &original
))
4239 noecho
.c_lflag
&= (tcflag_t
)~(ICANON
| ECHO
| ECHOE
| ISIG
);
4241 if (tcsetattr(tty
, TCSAFLUSH
, &noecho
))
4249 * Display the prompt...
4252 printf("%s\n---- PRESS ANY KEY ----", message
);
4260 ReadFile(tty
, &key
, 1, &bytes
, NULL
);
4266 SetConsoleMode(tty
, mode
);
4279 tcsetattr(tty
, TCSAFLUSH
, &original
);
4284 * Erase the "press any key" prompt...
4287 fputs("\r \r", stdout
);
4293 * 'print_attr()' - Print an attribute on the screen.
4297 print_attr(FILE *outfile
, /* I - Output file */
4298 int format
, /* I - Output format */
4299 ipp_attribute_t
*attr
, /* I - Attribute to print */
4300 ipp_tag_t
*group
) /* IO - Current group */
4302 int i
; /* Looping var */
4303 ipp_attribute_t
*colattr
; /* Collection attribute */
4306 if (format
== _CUPS_OUTPUT_PLIST
)
4308 if (!attr
->name
|| (group
&& *group
!= attr
->group_tag
))
4310 if (attr
->group_tag
!= IPP_TAG_ZERO
)
4312 fputs("</dict>\n", outfile
);
4313 fputs("<dict>\n", outfile
);
4317 *group
= attr
->group_tag
;
4323 print_xml_string(outfile
, "key", attr
->name
);
4324 if (attr
->num_values
> 1)
4325 fputs("<array>\n", outfile
);
4327 switch (attr
->value_tag
)
4329 case IPP_TAG_INTEGER
:
4331 for (i
= 0; i
< attr
->num_values
; i
++)
4332 fprintf(outfile
, "<integer>%d</integer>\n", attr
->values
[i
].integer
);
4335 case IPP_TAG_BOOLEAN
:
4336 for (i
= 0; i
< attr
->num_values
; i
++)
4337 fputs(attr
->values
[i
].boolean
? "<true />\n" : "<false />\n", outfile
);
4340 case IPP_TAG_RANGE
:
4341 for (i
= 0; i
< attr
->num_values
; i
++)
4342 fprintf(outfile
, "<dict><key>lower</key><integer>%d</integer>"
4343 "<key>upper</key><integer>%d</integer></dict>\n",
4344 attr
->values
[i
].range
.lower
, attr
->values
[i
].range
.upper
);
4347 case IPP_TAG_RESOLUTION
:
4348 for (i
= 0; i
< attr
->num_values
; i
++)
4349 fprintf(outfile
, "<dict><key>xres</key><integer>%d</integer>"
4350 "<key>yres</key><integer>%d</integer>"
4351 "<key>units</key><string>%s</string></dict>\n",
4352 attr
->values
[i
].resolution
.xres
,
4353 attr
->values
[i
].resolution
.yres
,
4354 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
4359 for (i
= 0; i
< attr
->num_values
; i
++)
4360 fprintf(outfile
, "<date>%s</date>\n", iso_date(attr
->values
[i
].date
));
4363 case IPP_TAG_STRING
:
4364 for (i
= 0; i
< attr
->num_values
; i
++)
4366 char buffer
[IPP_MAX_LENGTH
* 5 / 4 + 1];
4369 fprintf(outfile
, "<data>%s</data>\n",
4370 httpEncode64_2(buffer
, sizeof(buffer
),
4371 attr
->values
[i
].unknown
.data
,
4372 attr
->values
[i
].unknown
.length
));
4378 case IPP_TAG_KEYWORD
:
4379 case IPP_TAG_CHARSET
:
4381 case IPP_TAG_MIMETYPE
:
4382 case IPP_TAG_LANGUAGE
:
4383 for (i
= 0; i
< attr
->num_values
; i
++)
4384 print_xml_string(outfile
, "string", attr
->values
[i
].string
.text
);
4387 case IPP_TAG_TEXTLANG
:
4388 case IPP_TAG_NAMELANG
:
4389 for (i
= 0; i
< attr
->num_values
; i
++)
4391 fputs("<dict><key>language</key><string>", outfile
);
4392 print_xml_string(outfile
, NULL
, attr
->values
[i
].string
.language
);
4393 fputs("</string><key>string</key><string>", outfile
);
4394 print_xml_string(outfile
, NULL
, attr
->values
[i
].string
.text
);
4395 fputs("</string></dict>\n", outfile
);
4399 case IPP_TAG_BEGIN_COLLECTION
:
4400 for (i
= 0; i
< attr
->num_values
; i
++)
4402 fputs("<dict>\n", outfile
);
4403 for (colattr
= attr
->values
[i
].collection
->attrs
;
4405 colattr
= colattr
->next
)
4406 print_attr(outfile
, format
, colattr
, NULL
);
4407 fputs("</dict>\n", outfile
);
4412 fprintf(outfile
, "<string><<%s>></string>\n", ippTagString(attr
->value_tag
));
4416 if (attr
->num_values
> 1)
4417 fputs("</array>\n", outfile
);
4421 char buffer
[8192]; /* Value buffer */
4423 if (format
== _CUPS_OUTPUT_TEST
)
4427 fputs(" -- separator --\n", outfile
);
4431 fprintf(outfile
, " %s (%s%s) = ", attr
->name
, attr
->num_values
> 1 ? "1setOf " : "", ippTagString(attr
->value_tag
));
4434 ippAttributeString(attr
, buffer
, sizeof(buffer
));
4435 fprintf(outfile
, "%s\n", buffer
);
4441 * 'print_csv()' - Print a line of CSV text.
4446 FILE *outfile
, /* I - Output file */
4447 ipp_attribute_t
*attr
, /* I - First attribute for line */
4448 int num_displayed
, /* I - Number of attributes to display */
4449 char **displayed
, /* I - Attributes to display */
4450 size_t *widths
) /* I - Column widths */
4452 int i
; /* Looping var */
4453 size_t maxlength
; /* Max length of all columns */
4454 char *buffer
, /* String buffer */
4455 *bufptr
; /* Pointer into buffer */
4456 ipp_attribute_t
*current
; /* Current attribute */
4460 * Get the maximum string length we have to show and allocate...
4463 for (i
= 1, maxlength
= widths
[0]; i
< num_displayed
; i
++)
4464 if (widths
[i
] > maxlength
)
4465 maxlength
= widths
[i
];
4469 if ((buffer
= malloc(maxlength
)) == NULL
)
4473 * Loop through the attributes to display...
4478 for (i
= 0; i
< num_displayed
; i
++)
4481 fputc(',', outfile
);
4485 for (current
= attr
; current
; current
= current
->next
)
4489 else if (!strcmp(current
->name
, displayed
[i
]))
4491 ippAttributeString(current
, buffer
, maxlength
);
4496 if (strchr(buffer
, ',') != NULL
|| strchr(buffer
, '\"') != NULL
||
4497 strchr(buffer
, '\\') != NULL
)
4499 putc('\"', outfile
);
4500 for (bufptr
= buffer
; *bufptr
; bufptr
++)
4502 if (*bufptr
== '\\' || *bufptr
== '\"')
4503 putc('\\', outfile
);
4504 putc(*bufptr
, outfile
);
4506 putc('\"', outfile
);
4509 fputs(buffer
, outfile
);
4511 putc('\n', outfile
);
4515 for (i
= 0; i
< num_displayed
; i
++)
4520 fputs(displayed
[i
], outfile
);
4522 putc('\n', outfile
);
4530 * 'print_fatal_error()' - Print a fatal error message.
4534 print_fatal_error(FILE *outfile
, /* I - Output file */
4535 const char *s
, /* I - Printf-style format string */
4536 ...) /* I - Additional arguments as needed */
4538 char buffer
[10240]; /* Format buffer */
4539 va_list ap
; /* Pointer to arguments */
4543 * Format the error message...
4547 vsnprintf(buffer
, sizeof(buffer
), s
, ap
);
4554 if (Output
== _CUPS_OUTPUT_PLIST
)
4556 print_xml_header(outfile
);
4557 print_xml_trailer(outfile
, 0, buffer
);
4560 _cupsLangPrintf(stderr
, "ipptool: %s", buffer
);
4565 * 'print_line()' - Print a line of formatted or CSV text.
4570 FILE *outfile
, /* I - Output file */
4571 ipp_attribute_t
*attr
, /* I - First attribute for line */
4572 int num_displayed
, /* I - Number of attributes to display */
4573 char **displayed
, /* I - Attributes to display */
4574 size_t *widths
) /* I - Column widths */
4576 int i
; /* Looping var */
4577 size_t maxlength
; /* Max length of all columns */
4578 char *buffer
; /* String buffer */
4579 ipp_attribute_t
*current
; /* Current attribute */
4583 * Get the maximum string length we have to show and allocate...
4586 for (i
= 1, maxlength
= widths
[0]; i
< num_displayed
; i
++)
4587 if (widths
[i
] > maxlength
)
4588 maxlength
= widths
[i
];
4592 if ((buffer
= malloc(maxlength
)) == NULL
)
4596 * Loop through the attributes to display...
4601 for (i
= 0; i
< num_displayed
; i
++)
4608 for (current
= attr
; current
; current
= current
->next
)
4612 else if (!strcmp(current
->name
, displayed
[i
]))
4614 ippAttributeString(current
, buffer
, maxlength
);
4619 fprintf(outfile
, "%*s", (int)-widths
[i
], buffer
);
4621 putc('\n', outfile
);
4625 for (i
= 0; i
< num_displayed
; i
++)
4630 fprintf(outfile
, "%*s", (int)-widths
[i
], displayed
[i
]);
4632 putc('\n', outfile
);
4634 for (i
= 0; i
< num_displayed
; i
++)
4639 memset(buffer
, '-', widths
[i
]);
4640 buffer
[widths
[i
]] = '\0';
4641 fputs(buffer
, outfile
);
4643 putc('\n', outfile
);
4651 * 'print_xml_header()' - Print a standard XML plist header.
4655 print_xml_header(FILE *outfile
) /* I - Output file */
4659 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", outfile
);
4660 fputs("<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
4661 "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n", outfile
);
4662 fputs("<plist version=\"1.0\">\n", outfile
);
4663 fputs("<dict>\n", outfile
);
4664 fputs("<key>ipptoolVersion</key>\n", outfile
);
4665 fputs("<string>" CUPS_SVERSION
"</string>\n", outfile
);
4666 fputs("<key>Transfer</key>\n", outfile
);
4667 fprintf(outfile
, "<string>%s</string>\n",
4668 Transfer
== _CUPS_TRANSFER_AUTO
? "auto" :
4669 Transfer
== _CUPS_TRANSFER_CHUNKED
? "chunked" : "length");
4670 fputs("<key>Tests</key>\n", outfile
);
4671 fputs("<array>\n", outfile
);
4679 * 'print_xml_string()' - Print an XML string with escaping.
4683 print_xml_string(FILE *outfile
, /* I - Output file */
4684 const char *element
, /* I - Element name or NULL */
4685 const char *s
) /* I - String to print */
4688 fprintf(outfile
, "<%s>", element
);
4693 fputs("&", outfile
);
4695 fputs("<", outfile
);
4697 fputs(">", outfile
);
4698 else if ((*s
& 0xe0) == 0xc0)
4701 * Validate UTF-8 two-byte sequence...
4704 if ((s
[1] & 0xc0) != 0x80)
4711 putc(*s
++, outfile
);
4715 else if ((*s
& 0xf0) == 0xe0)
4718 * Validate UTF-8 three-byte sequence...
4721 if ((s
[1] & 0xc0) != 0x80 || (s
[2] & 0xc0) != 0x80)
4728 putc(*s
++, outfile
);
4729 putc(*s
++, outfile
);
4733 else if ((*s
& 0xf8) == 0xf0)
4736 * Validate UTF-8 four-byte sequence...
4739 if ((s
[1] & 0xc0) != 0x80 || (s
[2] & 0xc0) != 0x80 ||
4740 (s
[3] & 0xc0) != 0x80)
4747 putc(*s
++, outfile
);
4748 putc(*s
++, outfile
);
4749 putc(*s
++, outfile
);
4753 else if ((*s
& 0x80) || (*s
< ' ' && !isspace(*s
& 255)))
4756 * Invalid control character...
4768 fprintf(outfile
, "</%s>\n", element
);
4773 * 'print_xml_trailer()' - Print the XML trailer with success/fail value.
4777 print_xml_trailer(FILE *outfile
, /* I - Output file */
4778 int success
, /* I - 1 on success, 0 on failure */
4779 const char *message
) /* I - Error message or NULL */
4783 fputs("</array>\n", outfile
);
4784 fputs("<key>Successful</key>\n", outfile
);
4785 fputs(success
? "<true />\n" : "<false />\n", outfile
);
4788 fputs("<key>ErrorMessage</key>\n", outfile
);
4789 print_xml_string(outfile
, "string", message
);
4791 fputs("</dict>\n", outfile
);
4792 fputs("</plist>\n", outfile
);
4800 * 'set_variable()' - Set a variable value.
4804 set_variable(FILE *outfile
, /* I - Output file */
4805 _cups_vars_t
*vars
, /* I - Variables */
4806 const char *name
, /* I - Variable name */
4807 const char *value
) /* I - Value string */
4809 _cups_var_t key
, /* Search key */
4810 *var
; /* New variable */
4813 if (!_cups_strcasecmp(name
, "filename"))
4816 free(vars
->filename
);
4818 vars
->filename
= strdup(value
);
4822 key
.name
= (char *)name
;
4823 if ((var
= cupsArrayFind(vars
->vars
, &key
)) != NULL
)
4826 var
->value
= strdup(value
);
4828 else if ((var
= malloc(sizeof(_cups_var_t
))) == NULL
)
4830 print_fatal_error(outfile
, "Unable to allocate memory for variable \"%s\".", name
);
4835 var
->name
= strdup(name
);
4836 var
->value
= strdup(value
);
4838 cupsArrayAdd(vars
->vars
, var
);
4845 * 'sigterm_handler()' - Handle SIGINT and SIGTERM.
4849 sigterm_handler(int sig
) /* I - Signal number (unused) */
4855 signal(SIGINT
, SIG_DFL
);
4856 signal(SIGTERM
, SIG_DFL
);
4862 * 'timeout_cb()' - Handle HTTP timeouts.
4865 static int /* O - 1 to continue, 0 to cancel */
4866 timeout_cb(http_t
*http
, /* I - Connection to server */
4867 void *user_data
) /* I - User data (unused) */
4869 int buffered
= 0; /* Bytes buffered but not yet sent */
4875 * If the socket still have data waiting to be sent to the printer (as can
4876 * happen if the printer runs out of paper), continue to wait until the output
4877 * buffer is empty...
4880 #ifdef SO_NWRITE /* OS X and some versions of Linux */
4881 socklen_t len
= sizeof(buffered
); /* Size of return value */
4883 if (getsockopt(httpGetFd(http
), SOL_SOCKET
, SO_NWRITE
, &buffered
, &len
))
4886 #elif defined(SIOCOUTQ) /* Others except Windows */
4887 if (ioctl(httpGetFd(http
), SIOCOUTQ
, &buffered
))
4890 #else /* Windows (not possible) */
4892 #endif /* SO_NWRITE */
4894 return (buffered
> 0);
4899 * 'usage()' - Show program usage.
4905 _cupsLangPuts(stderr
, _("Usage: ipptool [options] URI filename [ ... "
4907 _cupsLangPuts(stderr
, _("Options:"));
4908 _cupsLangPuts(stderr
, _(" --help Show help."));
4909 _cupsLangPuts(stderr
, _(" --stop-after-include-error\n"
4910 " Stop tests after a failed INCLUDE."));
4911 _cupsLangPuts(stderr
, _(" --version Show version."));
4912 _cupsLangPuts(stderr
, _(" -4 Connect using IPv4."));
4913 _cupsLangPuts(stderr
, _(" -6 Connect using IPv6."));
4914 _cupsLangPuts(stderr
, _(" -C Send requests using "
4915 "chunking (default)."));
4916 _cupsLangPuts(stdout
, _(" -E Test with HTTP Upgrade to "
4918 _cupsLangPuts(stderr
, _(" -I Ignore errors."));
4919 _cupsLangPuts(stderr
, _(" -L Send requests using "
4920 "content-length."));
4921 _cupsLangPuts(stderr
, _(" -P filename.plist Produce XML plist to a file and test report to standard output."));
4922 _cupsLangPuts(stderr
, _(" -S Test with SSL "
4924 _cupsLangPuts(stderr
, _(" -T seconds Set the receive/send "
4925 "timeout in seconds."));
4926 _cupsLangPuts(stderr
, _(" -V version Set default IPP "
4928 _cupsLangPuts(stderr
, _(" -X Produce XML plist instead "
4930 _cupsLangPuts(stderr
, _(" -c Produce CSV output."));
4931 _cupsLangPuts(stderr
, _(" -d name=value Set named variable to "
4933 _cupsLangPuts(stderr
, _(" -f filename Set default request "
4935 _cupsLangPuts(stderr
, _(" -i seconds Repeat the last file with "
4936 "the given time interval."));
4937 _cupsLangPuts(stderr
, _(" -l Produce plain text output."));
4938 _cupsLangPuts(stderr
, _(" -n count Repeat the last file the "
4939 "given number of times."));
4940 _cupsLangPuts(stderr
, _(" -q Run silently."));
4941 _cupsLangPuts(stderr
, _(" -t Produce a test report."));
4942 _cupsLangPuts(stderr
, _(" -v Be verbose."));
4949 * 'validate_attr()' - Determine whether an attribute is valid.
4952 static int /* O - 1 if valid, 0 otherwise */
4953 validate_attr(FILE *outfile
, /* I - Output file */
4954 cups_array_t
*errors
, /* I - Errors array */
4955 ipp_attribute_t
*attr
) /* I - Attribute to validate */
4957 int i
; /* Looping var */
4958 char scheme
[64], /* Scheme from URI */
4959 userpass
[256], /* Username/password from URI */
4960 hostname
[256], /* Hostname from URI */
4961 resource
[1024]; /* Resource from URI */
4962 int port
, /* Port number from URI */
4963 uri_status
, /* URI separation status */
4964 valid
= 1; /* Is the attribute valid? */
4965 const char *ptr
; /* Pointer into string */
4966 ipp_attribute_t
*colattr
; /* Collection attribute */
4967 regex_t re
; /* Regular expression */
4968 ipp_uchar_t
*date
; /* Current date value */
4979 * Validate the attribute name.
4982 for (ptr
= attr
->name
; *ptr
; ptr
++)
4983 if (!isalnum(*ptr
& 255) && *ptr
!= '-' && *ptr
!= '.' && *ptr
!= '_')
4986 if (*ptr
|| ptr
== attr
->name
)
4991 "\"%s\": Bad attribute name - invalid character "
4992 "(RFC 2911 section 4.1.3).", attr
->name
);
4995 if ((ptr
- attr
->name
) > 255)
5000 "\"%s\": Bad attribute name - bad length "
5001 "(RFC 2911 section 4.1.3).", attr
->name
);
5004 switch (attr
->value_tag
)
5006 case IPP_TAG_INTEGER
:
5009 case IPP_TAG_BOOLEAN
:
5010 for (i
= 0; i
< attr
->num_values
; i
++)
5012 if (attr
->values
[i
].boolean
!= 0 &&
5013 attr
->values
[i
].boolean
!= 1)
5018 "\"%s\": Bad boolen value %d "
5019 "(RFC 2911 section 4.1.11).", attr
->name
,
5020 attr
->values
[i
].boolean
);
5026 for (i
= 0; i
< attr
->num_values
; i
++)
5028 if (attr
->values
[i
].integer
< 1)
5033 "\"%s\": Bad enum value %d - out of range "
5034 "(RFC 2911 section 4.1.4).", attr
->name
,
5035 attr
->values
[i
].integer
);
5040 case IPP_TAG_STRING
:
5041 for (i
= 0; i
< attr
->num_values
; i
++)
5043 if (attr
->values
[i
].unknown
.length
> IPP_MAX_OCTETSTRING
)
5048 "\"%s\": Bad octetString value - bad length %d "
5049 "(RFC 2911 section 4.1.10).", attr
->name
,
5050 attr
->values
[i
].unknown
.length
);
5056 for (i
= 0; i
< attr
->num_values
; i
++)
5058 date
= attr
->values
[i
].date
;
5060 if (date
[2] < 1 || date
[2] > 12)
5065 "\"%s\": Bad dateTime month %u "
5066 "(RFC 2911 section 4.1.14).", attr
->name
, date
[2]);
5069 if (date
[3] < 1 || date
[3] > 31)
5074 "\"%s\": Bad dateTime day %u "
5075 "(RFC 2911 section 4.1.14).", attr
->name
, date
[3]);
5083 "\"%s\": Bad dateTime hours %u "
5084 "(RFC 2911 section 4.1.14).", attr
->name
, date
[4]);
5092 "\"%s\": Bad dateTime minutes %u "
5093 "(RFC 2911 section 4.1.14).", attr
->name
, date
[5]);
5101 "\"%s\": Bad dateTime seconds %u "
5102 "(RFC 2911 section 4.1.14).", attr
->name
, date
[6]);
5110 "\"%s\": Bad dateTime deciseconds %u "
5111 "(RFC 2911 section 4.1.14).", attr
->name
, date
[7]);
5114 if (date
[8] != '-' && date
[8] != '+')
5119 "\"%s\": Bad dateTime UTC sign '%c' "
5120 "(RFC 2911 section 4.1.14).", attr
->name
, date
[8]);
5128 "\"%s\": Bad dateTime UTC hours %u "
5129 "(RFC 2911 section 4.1.14).", attr
->name
, date
[9]);
5137 "\"%s\": Bad dateTime UTC minutes %u "
5138 "(RFC 2911 section 4.1.14).", attr
->name
, date
[10]);
5143 case IPP_TAG_RESOLUTION
:
5144 for (i
= 0; i
< attr
->num_values
; i
++)
5146 if (attr
->values
[i
].resolution
.xres
<= 0)
5151 "\"%s\": Bad resolution value %dx%d%s - cross "
5152 "feed resolution must be positive "
5153 "(RFC 2911 section 4.1.15).", attr
->name
,
5154 attr
->values
[i
].resolution
.xres
,
5155 attr
->values
[i
].resolution
.yres
,
5156 attr
->values
[i
].resolution
.units
==
5157 IPP_RES_PER_INCH
? "dpi" :
5158 attr
->values
[i
].resolution
.units
==
5159 IPP_RES_PER_CM
? "dpcm" : "unknown");
5162 if (attr
->values
[i
].resolution
.yres
<= 0)
5167 "\"%s\": Bad resolution value %dx%d%s - feed "
5168 "resolution must be positive "
5169 "(RFC 2911 section 4.1.15).", attr
->name
,
5170 attr
->values
[i
].resolution
.xres
,
5171 attr
->values
[i
].resolution
.yres
,
5172 attr
->values
[i
].resolution
.units
==
5173 IPP_RES_PER_INCH
? "dpi" :
5174 attr
->values
[i
].resolution
.units
==
5175 IPP_RES_PER_CM
? "dpcm" : "unknown");
5178 if (attr
->values
[i
].resolution
.units
!= IPP_RES_PER_INCH
&&
5179 attr
->values
[i
].resolution
.units
!= IPP_RES_PER_CM
)
5184 "\"%s\": Bad resolution value %dx%d%s - bad "
5185 "units value (RFC 2911 section 4.1.15).",
5186 attr
->name
, attr
->values
[i
].resolution
.xres
,
5187 attr
->values
[i
].resolution
.yres
,
5188 attr
->values
[i
].resolution
.units
==
5189 IPP_RES_PER_INCH
? "dpi" :
5190 attr
->values
[i
].resolution
.units
==
5191 IPP_RES_PER_CM
? "dpcm" : "unknown");
5196 case IPP_TAG_RANGE
:
5197 for (i
= 0; i
< attr
->num_values
; i
++)
5199 if (attr
->values
[i
].range
.lower
> attr
->values
[i
].range
.upper
)
5204 "\"%s\": Bad rangeOfInteger value %d-%d - lower "
5205 "greater than upper (RFC 2911 section 4.1.13).",
5206 attr
->name
, attr
->values
[i
].range
.lower
,
5207 attr
->values
[i
].range
.upper
);
5212 case IPP_TAG_BEGIN_COLLECTION
:
5213 for (i
= 0; i
< attr
->num_values
; i
++)
5215 for (colattr
= attr
->values
[i
].collection
->attrs
;
5217 colattr
= colattr
->next
)
5219 if (!validate_attr(outfile
, NULL
, colattr
))
5226 if (colattr
&& errors
)
5228 add_stringf(errors
, "\"%s\": Bad collection value.", attr
->name
);
5232 validate_attr(outfile
, errors
, colattr
);
5233 colattr
= colattr
->next
;
5240 case IPP_TAG_TEXTLANG
:
5241 for (i
= 0; i
< attr
->num_values
; i
++)
5243 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
5245 if ((*ptr
& 0xe0) == 0xc0)
5248 if ((*ptr
& 0xc0) != 0x80)
5251 else if ((*ptr
& 0xf0) == 0xe0)
5254 if ((*ptr
& 0xc0) != 0x80)
5257 if ((*ptr
& 0xc0) != 0x80)
5260 else if ((*ptr
& 0xf8) == 0xf0)
5263 if ((*ptr
& 0xc0) != 0x80)
5266 if ((*ptr
& 0xc0) != 0x80)
5269 if ((*ptr
& 0xc0) != 0x80)
5272 else if (*ptr
& 0x80)
5281 "\"%s\": Bad text value \"%s\" - bad UTF-8 "
5282 "sequence (RFC 2911 section 4.1.1).", attr
->name
,
5283 attr
->values
[i
].string
.text
);
5286 if ((ptr
- attr
->values
[i
].string
.text
) > (IPP_MAX_TEXT
- 1))
5291 "\"%s\": Bad text value \"%s\" - bad length %d "
5292 "(RFC 2911 section 4.1.1).", attr
->name
,
5293 attr
->values
[i
].string
.text
,
5294 (int)strlen(attr
->values
[i
].string
.text
));
5300 case IPP_TAG_NAMELANG
:
5301 for (i
= 0; i
< attr
->num_values
; i
++)
5303 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
5305 if ((*ptr
& 0xe0) == 0xc0)
5308 if ((*ptr
& 0xc0) != 0x80)
5311 else if ((*ptr
& 0xf0) == 0xe0)
5314 if ((*ptr
& 0xc0) != 0x80)
5317 if ((*ptr
& 0xc0) != 0x80)
5320 else if ((*ptr
& 0xf8) == 0xf0)
5323 if ((*ptr
& 0xc0) != 0x80)
5326 if ((*ptr
& 0xc0) != 0x80)
5329 if ((*ptr
& 0xc0) != 0x80)
5332 else if (*ptr
& 0x80)
5341 "\"%s\": Bad name value \"%s\" - bad UTF-8 "
5342 "sequence (RFC 2911 section 4.1.2).", attr
->name
,
5343 attr
->values
[i
].string
.text
);
5346 if ((ptr
- attr
->values
[i
].string
.text
) > (IPP_MAX_NAME
- 1))
5351 "\"%s\": Bad name value \"%s\" - bad length %d "
5352 "(RFC 2911 section 4.1.2).", attr
->name
,
5353 attr
->values
[i
].string
.text
,
5354 (int)strlen(attr
->values
[i
].string
.text
));
5359 case IPP_TAG_KEYWORD
:
5360 for (i
= 0; i
< attr
->num_values
; i
++)
5362 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
5363 if (!isalnum(*ptr
& 255) && *ptr
!= '-' && *ptr
!= '.' &&
5367 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
5372 "\"%s\": Bad keyword value \"%s\" - invalid "
5373 "character (RFC 2911 section 4.1.3).",
5374 attr
->name
, attr
->values
[i
].string
.text
);
5377 if ((ptr
- attr
->values
[i
].string
.text
) > (IPP_MAX_KEYWORD
- 1))
5382 "\"%s\": Bad keyword value \"%s\" - bad "
5383 "length %d (RFC 2911 section 4.1.3).",
5384 attr
->name
, attr
->values
[i
].string
.text
,
5385 (int)strlen(attr
->values
[i
].string
.text
));
5391 for (i
= 0; i
< attr
->num_values
; i
++)
5393 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
,
5394 attr
->values
[i
].string
.text
,
5395 scheme
, sizeof(scheme
),
5396 userpass
, sizeof(userpass
),
5397 hostname
, sizeof(hostname
),
5398 &port
, resource
, sizeof(resource
));
5400 if (uri_status
< HTTP_URI_OK
)
5405 "\"%s\": Bad URI value \"%s\" - %s "
5406 "(RFC 2911 section 4.1.5).", attr
->name
,
5407 attr
->values
[i
].string
.text
,
5408 httpURIStatusString(uri_status
));
5411 if (strlen(attr
->values
[i
].string
.text
) > (IPP_MAX_URI
- 1))
5416 "\"%s\": Bad URI value \"%s\" - bad length %d "
5417 "(RFC 2911 section 4.1.5).", attr
->name
,
5418 attr
->values
[i
].string
.text
,
5419 (int)strlen(attr
->values
[i
].string
.text
));
5424 case IPP_TAG_URISCHEME
:
5425 for (i
= 0; i
< attr
->num_values
; i
++)
5427 ptr
= attr
->values
[i
].string
.text
;
5428 if (islower(*ptr
& 255))
5430 for (ptr
++; *ptr
; ptr
++)
5431 if (!islower(*ptr
& 255) && !isdigit(*ptr
& 255) &&
5432 *ptr
!= '+' && *ptr
!= '-' && *ptr
!= '.')
5436 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
5441 "\"%s\": Bad uriScheme value \"%s\" - bad "
5442 "characters (RFC 2911 section 4.1.6).",
5443 attr
->name
, attr
->values
[i
].string
.text
);
5446 if ((ptr
- attr
->values
[i
].string
.text
) > (IPP_MAX_URISCHEME
- 1))
5451 "\"%s\": Bad uriScheme value \"%s\" - bad "
5452 "length %d (RFC 2911 section 4.1.6).",
5453 attr
->name
, attr
->values
[i
].string
.text
,
5454 (int)strlen(attr
->values
[i
].string
.text
));
5459 case IPP_TAG_CHARSET
:
5460 for (i
= 0; i
< attr
->num_values
; i
++)
5462 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
5463 if (!isprint(*ptr
& 255) || isupper(*ptr
& 255) ||
5464 isspace(*ptr
& 255))
5467 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
5472 "\"%s\": Bad charset value \"%s\" - bad "
5473 "characters (RFC 2911 section 4.1.7).",
5474 attr
->name
, attr
->values
[i
].string
.text
);
5477 if ((ptr
- attr
->values
[i
].string
.text
) > (IPP_MAX_CHARSET
- 1))
5482 "\"%s\": Bad charset value \"%s\" - bad "
5483 "length %d (RFC 2911 section 4.1.7).",
5484 attr
->name
, attr
->values
[i
].string
.text
,
5485 (int)strlen(attr
->values
[i
].string
.text
));
5490 case IPP_TAG_LANGUAGE
:
5492 * The following regular expression is derived from the ABNF for
5493 * language tags in RFC 4646. All I can say is that this is the
5494 * easiest way to check the values...
5497 if ((i
= regcomp(&re
,
5499 "(([a-z]{2,3}(-[a-z][a-z][a-z]){0,3})|[a-z]{4,8})"
5501 "(-[a-z][a-z][a-z][a-z]){0,1}" /* script */
5502 "(-([a-z][a-z]|[0-9][0-9][0-9])){0,1}" /* region */
5503 "(-([a-z]{5,8}|[0-9][0-9][0-9]))*" /* variant */
5504 "(-[a-wy-z](-[a-z0-9]{2,8})+)*" /* extension */
5505 "(-x(-[a-z0-9]{1,8})+)*" /* privateuse */
5507 "x(-[a-z0-9]{1,8})+" /* privateuse */
5509 "[a-z]{1,3}(-[a-z][0-9]{2,8}){1,2}" /* grandfathered */
5511 REG_NOSUB
| REG_EXTENDED
)) != 0)
5513 char temp
[256]; /* Temporary error string */
5515 regerror(i
, &re
, temp
, sizeof(temp
));
5516 print_fatal_error(outfile
, "Unable to compile naturalLanguage regular "
5517 "expression: %s.", temp
);
5521 for (i
= 0; i
< attr
->num_values
; i
++)
5523 if (regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
5528 "\"%s\": Bad naturalLanguage value \"%s\" - bad "
5529 "characters (RFC 2911 section 4.1.8).",
5530 attr
->name
, attr
->values
[i
].string
.text
);
5533 if (strlen(attr
->values
[i
].string
.text
) > (IPP_MAX_LANGUAGE
- 1))
5538 "\"%s\": Bad naturalLanguage value \"%s\" - bad "
5539 "length %d (RFC 2911 section 4.1.8).",
5540 attr
->name
, attr
->values
[i
].string
.text
,
5541 (int)strlen(attr
->values
[i
].string
.text
));
5548 case IPP_TAG_MIMETYPE
:
5550 * The following regular expression is derived from the ABNF for
5551 * language tags in RFC 2045 and 4288. All I can say is that this is
5552 * the easiest way to check the values...
5555 if ((i
= regcomp(&re
,
5557 "[-a-zA-Z0-9!#$&.+^_]{1,127}" /* type-name */
5559 "[-a-zA-Z0-9!#$&.+^_]{1,127}" /* subtype-name */
5560 "(;[-a-zA-Z0-9!#$&.+^_]{1,127}=" /* parameter= */
5561 "([-a-zA-Z0-9!#$&.+^_]{1,127}|\"[^\"]*\"))*"
5564 REG_NOSUB
| REG_EXTENDED
)) != 0)
5566 char temp
[256]; /* Temporary error string */
5568 regerror(i
, &re
, temp
, sizeof(temp
));
5569 print_fatal_error(outfile
, "Unable to compile mimeMediaType regular "
5570 "expression: %s.", temp
);
5574 for (i
= 0; i
< attr
->num_values
; i
++)
5576 if (regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
5581 "\"%s\": Bad mimeMediaType value \"%s\" - bad "
5582 "characters (RFC 2911 section 4.1.9).",
5583 attr
->name
, attr
->values
[i
].string
.text
);
5586 if (strlen(attr
->values
[i
].string
.text
) > (IPP_MAX_MIMETYPE
- 1))
5591 "\"%s\": Bad mimeMediaType value \"%s\" - bad "
5592 "length %d (RFC 2911 section 4.1.9).",
5593 attr
->name
, attr
->values
[i
].string
.text
,
5594 (int)strlen(attr
->values
[i
].string
.text
));
5610 * 'with_value()' - Test a WITH-VALUE predicate.
5613 static int /* O - 1 on match, 0 on non-match */
5614 with_value(FILE *outfile
, /* I - Output file */
5615 cups_array_t
*errors
, /* I - Errors array */
5616 char *value
, /* I - Value string */
5617 int flags
, /* I - Flags for match */
5618 ipp_attribute_t
*attr
, /* I - Attribute to compare */
5619 char *matchbuf
, /* I - Buffer to hold matching value */
5620 size_t matchlen
) /* I - Length of match buffer */
5622 int i
, /* Looping var */
5624 char temp
[1024], /* Temporary value string */
5625 *valptr
; /* Pointer into value */
5629 match
= (flags
& _CUPS_WITH_ALL
) ? 1 : 0;
5632 * NULL matches everything.
5635 if (!value
|| !*value
)
5639 * Compare the value string to the attribute value.
5642 switch (attr
->value_tag
)
5644 case IPP_TAG_INTEGER
:
5646 for (i
= 0; i
< attr
->num_values
; i
++)
5648 char op
, /* Comparison operator */
5649 *nextptr
; /* Next pointer */
5650 int intvalue
, /* Integer value */
5651 valmatch
= 0; /* Does the current value match? */
5655 while (isspace(*valptr
& 255) || isdigit(*valptr
& 255) ||
5656 *valptr
== '-' || *valptr
== ',' || *valptr
== '<' ||
5657 *valptr
== '=' || *valptr
== '>')
5660 while (*valptr
&& !isdigit(*valptr
& 255) && *valptr
!= '-')
5662 if (*valptr
== '<' || *valptr
== '>' || *valptr
== '=')
5670 intvalue
= (int)strtol(valptr
, &nextptr
, 0);
5671 if (nextptr
== valptr
)
5675 if ((op
== '=' && attr
->values
[i
].integer
== intvalue
) ||
5676 (op
== '<' && attr
->values
[i
].integer
< intvalue
) ||
5677 (op
== '>' && attr
->values
[i
].integer
> intvalue
))
5680 snprintf(matchbuf
, matchlen
, "%d", attr
->values
[i
].integer
);
5687 if (flags
& _CUPS_WITH_ALL
)
5702 if (!match
&& errors
)
5704 for (i
= 0; i
< attr
->num_values
; i
++)
5705 add_stringf(errors
, "GOT: %s=%d", attr
->name
,
5706 attr
->values
[i
].integer
);
5710 case IPP_TAG_RANGE
:
5711 for (i
= 0; i
< attr
->num_values
; i
++)
5713 char op
, /* Comparison operator */
5714 *nextptr
; /* Next pointer */
5715 int intvalue
, /* Integer value */
5716 valmatch
= 0; /* Does the current value match? */
5720 while (isspace(*valptr
& 255) || isdigit(*valptr
& 255) ||
5721 *valptr
== '-' || *valptr
== ',' || *valptr
== '<' ||
5722 *valptr
== '=' || *valptr
== '>')
5725 while (*valptr
&& !isdigit(*valptr
& 255) && *valptr
!= '-')
5727 if (*valptr
== '<' || *valptr
== '>' || *valptr
== '=')
5735 intvalue
= (int)strtol(valptr
, &nextptr
, 0);
5736 if (nextptr
== valptr
)
5740 if ((op
== '=' && (attr
->values
[i
].range
.lower
== intvalue
||
5741 attr
->values
[i
].range
.upper
== intvalue
)) ||
5742 (op
== '<' && attr
->values
[i
].range
.upper
< intvalue
) ||
5743 (op
== '>' && attr
->values
[i
].range
.upper
> intvalue
))
5746 snprintf(matchbuf
, matchlen
, "%d-%d",
5747 attr
->values
[0].range
.lower
,
5748 attr
->values
[0].range
.upper
);
5755 if (flags
& _CUPS_WITH_ALL
)
5770 if (!match
&& errors
)
5772 for (i
= 0; i
< attr
->num_values
; i
++)
5773 add_stringf(errors
, "GOT: %s=%d-%d", attr
->name
,
5774 attr
->values
[i
].range
.lower
,
5775 attr
->values
[i
].range
.upper
);
5779 case IPP_TAG_BOOLEAN
:
5780 for (i
= 0; i
< attr
->num_values
; i
++)
5782 if (!strcmp(value
, "true") == attr
->values
[i
].boolean
)
5785 strlcpy(matchbuf
, value
, matchlen
);
5787 if (!(flags
& _CUPS_WITH_ALL
))
5793 else if (flags
& _CUPS_WITH_ALL
)
5800 if (!match
&& errors
)
5802 for (i
= 0; i
< attr
->num_values
; i
++)
5803 add_stringf(errors
, "GOT: %s=%s", attr
->name
,
5804 attr
->values
[i
].boolean
? "true" : "false");
5808 case IPP_TAG_RESOLUTION
:
5809 for (i
= 0; i
< attr
->num_values
; i
++)
5811 if (attr
->values
[i
].resolution
.xres
==
5812 attr
->values
[i
].resolution
.yres
)
5813 snprintf(temp
, sizeof(temp
), "%d%s",
5814 attr
->values
[i
].resolution
.xres
,
5815 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
5818 snprintf(temp
, sizeof(temp
), "%dx%d%s",
5819 attr
->values
[i
].resolution
.xres
,
5820 attr
->values
[i
].resolution
.yres
,
5821 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
5824 if (!strcmp(value
, temp
))
5827 strlcpy(matchbuf
, value
, matchlen
);
5829 if (!(flags
& _CUPS_WITH_ALL
))
5835 else if (flags
& _CUPS_WITH_ALL
)
5842 if (!match
&& errors
)
5844 for (i
= 0; i
< attr
->num_values
; i
++)
5846 if (attr
->values
[i
].resolution
.xres
==
5847 attr
->values
[i
].resolution
.yres
)
5848 snprintf(temp
, sizeof(temp
), "%d%s",
5849 attr
->values
[i
].resolution
.xres
,
5850 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
5853 snprintf(temp
, sizeof(temp
), "%dx%d%s",
5854 attr
->values
[i
].resolution
.xres
,
5855 attr
->values
[i
].resolution
.yres
,
5856 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
5859 if (strcmp(value
, temp
))
5860 add_stringf(errors
, "GOT: %s=%s", attr
->name
, temp
);
5865 case IPP_TAG_NOVALUE
:
5866 case IPP_TAG_UNKNOWN
:
5869 case IPP_TAG_CHARSET
:
5870 case IPP_TAG_KEYWORD
:
5871 case IPP_TAG_LANGUAGE
:
5872 case IPP_TAG_MIMETYPE
:
5874 case IPP_TAG_NAMELANG
:
5876 case IPP_TAG_TEXTLANG
:
5878 case IPP_TAG_URISCHEME
:
5879 if (flags
& _CUPS_WITH_REGEX
)
5882 * Value is an extended, case-sensitive POSIX regular expression...
5885 regex_t re
; /* Regular expression */
5887 if ((i
= regcomp(&re
, value
, REG_EXTENDED
| REG_NOSUB
)) != 0)
5889 regerror(i
, &re
, temp
, sizeof(temp
));
5891 print_fatal_error(outfile
, "Unable to compile WITH-VALUE regular expression "
5892 "\"%s\" - %s", value
, temp
);
5897 * See if ALL of the values match the given regular expression.
5900 for (i
= 0; i
< attr
->num_values
; i
++)
5902 if (!regexec(&re
, get_string(attr
, i
, flags
, temp
, sizeof(temp
)),
5907 get_string(attr
, i
, flags
, temp
, sizeof(temp
)),
5910 if (!(flags
& _CUPS_WITH_ALL
))
5916 else if (flags
& _CUPS_WITH_ALL
)
5925 else if (ippGetValueTag(attr
) == IPP_TAG_URI
)
5927 if (!strncmp(value
, "ipp://", 6) || !strncmp(value
, "http://", 7) || !strncmp(value
, "ipps://", 7) || !strncmp(value
, "https://", 8))
5929 char scheme
[256], /* URI scheme */
5930 userpass
[256], /* username:password, if any */
5931 hostname
[256], /* hostname */
5932 *hostptr
, /* Pointer into hostname */
5933 resource
[1024]; /* Resource path */
5934 int port
; /* Port number */
5936 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
== '.')
5939 * Strip trailing "." in hostname of URI...
5943 httpAssembleURI(HTTP_URI_CODING_ALL
, temp
, sizeof(temp
), scheme
, userpass
, hostname
, port
, resource
);
5949 * Value is a literal URI string, see if the value(s) match...
5952 for (i
= 0; i
< attr
->num_values
; i
++)
5954 if (!strcmp(value
, get_string(attr
, i
, flags
, temp
, sizeof(temp
))))
5958 get_string(attr
, i
, flags
, temp
, sizeof(temp
)),
5961 if (!(flags
& _CUPS_WITH_ALL
))
5967 else if (flags
& _CUPS_WITH_ALL
)
5977 * Value is a literal string, see if the value(s) match...
5980 for (i
= 0; i
< attr
->num_values
; i
++)
5982 if (!strcmp(value
, get_string(attr
, i
, flags
, temp
, sizeof(temp
))))
5986 get_string(attr
, i
, flags
, temp
, sizeof(temp
)),
5989 if (!(flags
& _CUPS_WITH_ALL
))
5995 else if (flags
& _CUPS_WITH_ALL
)
6003 if (!match
&& errors
)
6005 for (i
= 0; i
< attr
->num_values
; i
++)
6006 add_stringf(errors
, "GOT: %s=\"%s\"", attr
->name
,
6007 attr
->values
[i
].string
.text
);
6020 * 'with_value_from()' - Test a WITH-VALUE-FROM predicate.
6023 static int /* O - 1 on match, 0 on non-match */
6025 cups_array_t
*errors
, /* I - Errors array */
6026 ipp_attribute_t
*fromattr
, /* I - "From" attribute */
6027 ipp_attribute_t
*attr
, /* I - Attribute to compare */
6028 char *matchbuf
, /* I - Buffer to hold matching value */
6029 size_t matchlen
) /* I - Length of match buffer */
6031 int i
, j
, /* Looping vars */
6032 count
= ippGetCount(attr
), /* Number of attribute values */
6033 match
= 1; /* Match? */
6039 * Compare the from value(s) to the attribute value(s)...
6042 switch (ippGetValueTag(attr
))
6044 case IPP_TAG_INTEGER
:
6045 if (ippGetValueTag(fromattr
) != IPP_TAG_INTEGER
&& ippGetValueTag(fromattr
) != IPP_TAG_RANGE
)
6046 goto wrong_value_tag
;
6048 for (i
= 0; i
< count
; i
++)
6050 int value
= ippGetInteger(attr
, i
);
6051 /* Current integer value */
6053 if (ippContainsInteger(fromattr
, value
))
6056 snprintf(matchbuf
, matchlen
, "%d", value
);
6060 add_stringf(errors
, "GOT: %s=%d", ippGetName(attr
), value
);
6067 if (ippGetValueTag(fromattr
) != IPP_TAG_ENUM
)
6068 goto wrong_value_tag
;
6070 for (i
= 0; i
< count
; i
++)
6072 int value
= ippGetInteger(attr
, i
);
6073 /* Current integer value */
6075 if (ippContainsInteger(fromattr
, value
))
6078 snprintf(matchbuf
, matchlen
, "%d", value
);
6082 add_stringf(errors
, "GOT: %s=%d", ippGetName(attr
), value
);
6088 case IPP_TAG_RESOLUTION
:
6089 if (ippGetValueTag(fromattr
) != IPP_TAG_RESOLUTION
)
6090 goto wrong_value_tag
;
6092 for (i
= 0; i
< count
; i
++)
6096 int fromcount
= ippGetCount(fromattr
);
6097 int fromxres
, fromyres
;
6098 ipp_res_t fromunits
;
6100 xres
= ippGetResolution(attr
, i
, &yres
, &units
);
6102 for (j
= 0; j
< fromcount
; j
++)
6104 fromxres
= ippGetResolution(fromattr
, j
, &fromyres
, &fromunits
);
6105 if (fromxres
== xres
&& fromyres
== yres
&& fromunits
== units
)
6114 snprintf(matchbuf
, matchlen
, "%d%s", xres
, units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
6116 snprintf(matchbuf
, matchlen
, "%dx%d%s", xres
, yres
, units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
6122 add_stringf(errors
, "GOT: %s=%d%s", ippGetName(attr
), xres
, units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
6124 add_stringf(errors
, "GOT: %s=%dx%d%s", ippGetName(attr
), xres
, yres
, units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
6131 case IPP_TAG_NOVALUE
:
6132 case IPP_TAG_UNKNOWN
:
6135 case IPP_TAG_CHARSET
:
6136 case IPP_TAG_KEYWORD
:
6137 case IPP_TAG_LANGUAGE
:
6138 case IPP_TAG_MIMETYPE
:
6140 case IPP_TAG_NAMELANG
:
6142 case IPP_TAG_TEXTLANG
:
6144 case IPP_TAG_URISCHEME
:
6145 for (i
= 0; i
< count
; i
++)
6147 const char *value
= ippGetString(attr
, i
, NULL
);
6148 /* Current string value */
6150 if (ippContainsString(fromattr
, value
))
6153 strlcpy(matchbuf
, value
, matchlen
);
6157 add_stringf(errors
, "GOT: %s='%s'", ippGetName(attr
), value
);
6170 /* value tag mismatch between fromattr and attr */
6173 add_stringf(errors
, "GOT: %s OF-TYPE %s", ippGetName(attr
), ippTagString(ippGetValueTag(attr
)));