4 * ipptool command for CUPS.
6 * Copyright 2007-2011 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 * main() - Parse options and do tests.
20 * compare_vars() - Compare two variables.
21 * do_tests() - Do tests as specified in the test file.
22 * expand_variables() - Expand variables in a string.
23 * expect_matches() - Return true if the tag matches the specification.
24 * get_collection() - Get a collection value from the current test file.
25 * get_filename() - Get a filename based on the current test file.
26 * get_token() - Get a token from a file.
27 * get_variable() - Get the value of a variable.
28 * iso_date() - Return an ISO 8601 date/time string for the given IPP
30 * password_cb() - Password callback for authenticated tests.
31 * print_attr() - Print an attribute on the screen.
32 * print_col() - Print a collection attribute on the screen.
33 * print_csv() - Print a line of CSV text.
34 * print_fatal_error() - Print a fatal error message.
35 * print_line() - Print a line of formatted or CSV text.
36 * print_test_error() - Print a test error message.
37 * print_xml_header() - Print a standard XML plist header.
38 * print_xml_string() - Print an XML string with escaping.
39 * print_xml_trailer() - Print the XML trailer with success/fail value.
40 * set_variable() - Set a variable value.
41 * timeout_cb() - Handle HTTP timeouts.
42 * usage() - Show program usage.
43 * validate_attr() - Determine whether an attribute is valid.
44 * with_value() - Test a WITH-VALUE predicate.
48 * Include necessary headers...
51 #include <cups/cups-private.h>
52 #include <cups/file-private.h>
57 #endif /* !O_BINARY */
64 typedef enum _cups_transfer_e
/**** How to send request data ****/
66 _CUPS_TRANSFER_AUTO
, /* Chunk for files, length for static */
67 _CUPS_TRANSFER_CHUNKED
, /* Chunk always */
68 _CUPS_TRANSFER_LENGTH
/* Length always */
71 typedef enum _cups_output_e
/**** Output mode ****/
73 _CUPS_OUTPUT_QUIET
, /* No output */
74 _CUPS_OUTPUT_TEST
, /* Traditional CUPS test output */
75 _CUPS_OUTPUT_PLIST
, /* XML plist test output */
76 _CUPS_OUTPUT_LIST
, /* Tabular list output */
77 _CUPS_OUTPUT_CSV
/* Comma-separated values output */
80 typedef struct _cups_expect_s
/**** Expected attribute info ****/
82 int optional
, /* Optional attribute? */
83 not_expect
; /* Don't expect attribute? */
84 char *name
, /* Attribute name */
85 *of_type
, /* Type name */
86 *same_count_as
, /* Parallel attribute name */
87 *if_defined
, /* Only required if variable defined */
88 *if_not_defined
, /* Only required if variable is not defined */
89 *with_value
, /* Attribute must include this value */
90 *define_match
, /* Variable to define on match */
91 *define_no_match
, /* Variable to define on no-match */
92 *define_value
; /* Variable to define with value */
93 int with_regex
, /* WITH-VALUE is a regular expression */
94 count
; /* Expected count if > 0 */
95 ipp_tag_t in_group
; /* IN-GROUP value */
98 typedef struct _cups_status_s
/**** Status info ****/
100 ipp_status_t status
; /* Expected status code */
101 char *if_defined
, /* Only if variable is defined */
102 *if_not_defined
; /* Only if variable is not defined */
105 typedef struct _cups_var_s
/**** Variable ****/
107 char *name
, /* Name of variable */
108 *value
; /* Value of variable */
111 typedef struct _cups_vars_s
/**** Set of variables ****/
113 const char *uri
, /* URI for printer */
114 *filename
; /* Filename */
115 char scheme
[64], /* Scheme from URI */
116 userpass
[256], /* Username/password from URI */
117 hostname
[256], /* Hostname from URI */
118 resource
[1024]; /* Resource path from URI */
119 int port
; /* Port number from URI */
120 http_encryption_t encryption
; /* Encryption for connection? */
121 double timeout
; /* Timeout for connection */
122 int family
; /* Address family */
123 cups_array_t
*vars
; /* Array of variables */
131 _cups_transfer_t Transfer
= _CUPS_TRANSFER_AUTO
;
132 /* How to transfer requests */
133 _cups_output_t Output
= _CUPS_OUTPUT_LIST
;
135 int IgnoreErrors
= 0, /* Ignore errors? */
136 Verbosity
= 0, /* Show all attributes? */
137 Version
= 11, /* Default IPP version */
138 XMLHeader
= 0; /* 1 if header is written */
139 char *Password
= NULL
; /* Password from URI */
140 const char * const URIStatusStrings
[] = /* URI status strings */
143 "Bad arguments to function",
144 "Bad resource in URI",
145 "Bad port number in URI",
146 "Bad hostname/address in URI",
147 "Bad username in URI",
151 "Missing scheme in URI",
152 "Unknown scheme in URI",
153 "Missing resource in URI"
161 static int compare_vars(_cups_var_t
*a
, _cups_var_t
*b
);
162 static int do_tests(_cups_vars_t
*vars
, const char *testfile
);
163 static void expand_variables(_cups_vars_t
*vars
, char *dst
, const char *src
,
166 __attribute((nonnull(1,2,3)))
167 #endif /* __GNUC__ */
169 static int expect_matches(_cups_expect_t
*expect
, ipp_tag_t value_tag
);
170 static ipp_t
*get_collection(_cups_vars_t
*vars
, FILE *fp
, int *linenum
);
171 static char *get_filename(const char *testfile
, char *dst
, const char *src
,
173 static char *get_token(FILE *fp
, char *buf
, int buflen
,
175 static char *get_variable(_cups_vars_t
*vars
, const char *name
);
176 static char *iso_date(ipp_uchar_t
*date
);
177 static const char *password_cb(const char *prompt
);
178 static void print_attr(ipp_attribute_t
*attr
, ipp_tag_t
*group
);
179 static void print_col(ipp_t
*col
);
180 static void print_csv(ipp_attribute_t
*attr
, int num_displayed
,
181 char **displayed
, size_t *widths
);
182 static void print_fatal_error(const char *s
, ...)
184 __attribute__ ((__format__ (__printf__
, 1, 2)))
185 #endif /* __GNUC__ */
187 static void print_line(ipp_attribute_t
*attr
, int num_displayed
,
188 char **displayed
, size_t *widths
);
189 static void print_test_error(const char *s
, ...)
191 __attribute__ ((__format__ (__printf__
, 1, 2)))
192 #endif /* __GNUC__ */
194 static void print_xml_header(void);
195 static void print_xml_string(const char *element
, const char *s
);
196 static void print_xml_trailer(int success
, const char *message
);
197 static void set_variable(_cups_vars_t
*vars
, const char *name
,
199 static int timeout_cb(http_t
*http
, void *user_data
);
200 static void usage(void);
201 static int validate_attr(ipp_attribute_t
*attr
, int print
);
202 static int with_value(char *value
, int regex
, ipp_attribute_t
*attr
,
207 * 'main()' - Parse options and do tests.
210 int /* O - Exit status */
211 main(int argc
, /* I - Number of command-line args */
212 char *argv
[]) /* I - Command-line arguments */
214 int i
; /* Looping var */
215 int status
; /* Status of tests... */
216 char *opt
, /* Current option */
217 name
[1024], /* Name/value buffer */
218 *value
, /* Pointer to value */
219 filename
[1024], /* Real filename */
220 testname
[1024]; /* Real test filename */
221 const char *testfile
; /* Test file to use */
222 int interval
, /* Test interval in microseconds */
223 repeat
; /* Repeat count */
224 _cups_vars_t vars
; /* Variables */
225 http_uri_status_t uri_status
; /* URI separation status */
226 _cups_globals_t
*cg
= _cupsGlobals();
232 * Initialize the locale and variables...
235 _cupsSetLocale(argv
);
237 memset(&vars
, 0, sizeof(vars
));
238 vars
.family
= AF_UNSPEC
;
239 vars
.vars
= cupsArrayNew((cups_array_func_t
)compare_vars
, NULL
);
244 * ipptool URI testfile
252 for (i
= 1; i
< argc
; i
++)
254 if (argv
[i
][0] == '-')
256 for (opt
= argv
[i
] + 1; *opt
; opt
++)
260 case '4' : /* Connect using IPv4 only */
261 vars
.family
= AF_INET
;
265 case '6' : /* Connect using IPv6 only */
266 vars
.family
= AF_INET6
;
268 #endif /* AF_INET6 */
270 case 'C' : /* Enable HTTP chunking */
271 Transfer
= _CUPS_TRANSFER_CHUNKED
;
274 case 'E' : /* Encrypt with TLS */
276 vars
.encryption
= HTTP_ENCRYPT_REQUIRED
;
278 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."),
280 #endif /* HAVE_SSL */
283 case 'I' : /* Ignore errors */
287 case 'L' : /* Disable HTTP chunking */
288 Transfer
= _CUPS_TRANSFER_LENGTH
;
291 case 'S' : /* Encrypt with SSL */
293 vars
.encryption
= HTTP_ENCRYPT_ALWAYS
;
295 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."),
297 #endif /* HAVE_SSL */
300 case 'T' : /* Set timeout */
305 _cupsLangPuts(stderr
,
306 _("ipptool: Missing timeout for \"-T\"."));
310 vars
.timeout
= _cupsStrScand(argv
[i
], NULL
, localeconv());
313 case 'V' : /* Set IPP version */
318 _cupsLangPuts(stderr
,
319 _("ipptool: Missing version for \"-V\"."));
323 if (!strcmp(argv
[i
], "1.0"))
325 else if (!strcmp(argv
[i
], "1.1"))
327 else if (!strcmp(argv
[i
], "2.0"))
329 else if (!strcmp(argv
[i
], "2.1"))
331 else if (!strcmp(argv
[i
], "2.2"))
335 _cupsLangPrintf(stderr
,
336 _("ipptool: Bad version %s for \"-V\"."),
342 case 'X' : /* Produce XML output */
343 Output
= _CUPS_OUTPUT_PLIST
;
345 if (interval
|| repeat
)
347 _cupsLangPuts(stderr
, _("ipptool: \"-i\" and \"-n\" are "
348 "incompatible with -X\"."));
353 case 'c' : /* CSV output */
354 Output
= _CUPS_OUTPUT_CSV
;
357 case 'd' : /* Define a variable */
362 _cupsLangPuts(stderr
,
363 _("ipptool: Missing name=value for \"-d\"."));
367 strlcpy(name
, argv
[i
], sizeof(name
));
368 if ((value
= strchr(name
, '=')) != NULL
)
371 value
= name
+ strlen(name
);
373 set_variable(&vars
, name
, value
);
376 case 'f' : /* Set the default test filename */
381 _cupsLangPuts(stderr
,
382 _("ipptool: Missing filename for \"-f\"."));
386 if (access(argv
[i
], 0) && argv
[i
][0] != '/')
388 snprintf(filename
, sizeof(filename
), "%s/ipptool/%s",
389 cg
->cups_datadir
, argv
[i
]);
390 if (access(argv
[i
], 0))
391 vars
.filename
= argv
[i
];
393 vars
.filename
= filename
;
396 vars
.filename
= argv
[i
];
399 case 'i' : /* Test every N seconds */
404 _cupsLangPuts(stderr
,
405 _("ipptool: Missing seconds for \"-i\"."));
410 interval
= (int)(_cupsStrScand(argv
[i
], NULL
, localeconv()) *
414 _cupsLangPuts(stderr
,
415 _("ipptool: Invalid seconds for \"-i\"."));
420 if (Output
== _CUPS_OUTPUT_PLIST
&& interval
)
422 _cupsLangPuts(stderr
, _("ipptool: \"-i\" is incompatible with "
428 case 'l' : /* List as a table */
429 Output
= _CUPS_OUTPUT_LIST
;
432 case 'n' : /* Repeat count */
437 _cupsLangPuts(stderr
,
438 _("ipptool: Missing count for \"-n\"."));
442 repeat
= atoi(argv
[i
]);
444 if (Output
== _CUPS_OUTPUT_PLIST
&& repeat
)
446 _cupsLangPuts(stderr
, _("ipptool: \"-n\" is incompatible with "
452 case 'q' : /* Be quiet */
453 Output
= _CUPS_OUTPUT_QUIET
;
456 case 't' : /* CUPS test output */
457 Output
= _CUPS_OUTPUT_TEST
;
460 case 'v' : /* Be verbose */
465 _cupsLangPrintf(stderr
, _("ipptool: Unknown option \"-%c\"."),
472 else if (!strncmp(argv
[i
], "ipp://", 6) || !strncmp(argv
[i
], "http://", 7)
474 || !strncmp(argv
[i
], "ipps://", 7)
475 || !strncmp(argv
[i
], "https://", 8)
476 #endif /* HAVE_SSL */
485 _cupsLangPuts(stderr
, _("ipptool: May only specify a single URI."));
490 if (!strncmp(argv
[i
], "ipps://", 7) || !strncmp(argv
[i
], "https://", 8))
491 vars
.encryption
= HTTP_ENCRYPT_ALWAYS
;
492 #endif /* HAVE_SSL */
495 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
, vars
.uri
,
496 vars
.scheme
, sizeof(vars
.scheme
),
497 vars
.userpass
, sizeof(vars
.userpass
),
498 vars
.hostname
, sizeof(vars
.hostname
),
500 vars
.resource
, sizeof(vars
.resource
));
502 if (uri_status
!= HTTP_URI_OK
)
504 _cupsLangPrintf(stderr
, _("ipptool: Bad URI - %s."),
505 URIStatusStrings
[uri_status
- HTTP_URI_OVERFLOW
]);
509 if (vars
.userpass
[0])
511 if ((Password
= strchr(vars
.userpass
, ':')) != NULL
)
514 cupsSetUser(vars
.userpass
);
515 cupsSetPasswordCB(password_cb
);
516 set_variable(&vars
, "uriuser", vars
.userpass
);
527 _cupsLangPuts(stderr
, _("ipptool: URI required before test file."));
531 if (access(argv
[i
], 0) && argv
[i
][0] != '/')
533 snprintf(testname
, sizeof(testname
), "%s/ipptool/%s", cg
->cups_datadir
,
535 if (access(testname
, 0))
543 if (!do_tests(&vars
, testfile
))
548 if (!vars
.uri
|| !testfile
)
552 * Loop if the interval is set...
555 if (Output
== _CUPS_OUTPUT_PLIST
)
556 print_xml_trailer(!status
, NULL
);
557 else if (interval
> 0 && repeat
> 0)
562 do_tests(&vars
, testfile
);
566 else if (interval
> 0)
571 do_tests(&vars
, testfile
);
584 * 'compare_vars()' - Compare two variables.
587 static int /* O - Result of comparison */
588 compare_vars(_cups_var_t
*a
, /* I - First variable */
589 _cups_var_t
*b
) /* I - Second variable */
591 return (strcasecmp(a
->name
, b
->name
));
596 * 'do_tests()' - Do tests as specified in the test file.
599 static int /* 1 = success, 0 = failure */
600 do_tests(_cups_vars_t
*vars
, /* I - Variables */
601 const char *testfile
) /* I - Test file to use */
603 int i
, /* Looping var */
604 linenum
, /* Current line number */
605 pass
, /* Did we pass the test? */
606 prev_pass
= 1, /* Did we pass the previous test? */
607 request_id
, /* Current request ID */
608 show_header
= 1, /* Show the test header? */
609 ignore_errors
, /* Ignore test failures? */
610 skip_previous
= 0; /* Skip on previous test failure? */
611 http_t
*http
= NULL
; /* HTTP connection to server */
612 FILE *fp
= NULL
; /* Test file */
613 char resource
[512], /* Resource for request */
614 token
[1024], /* Token from file */
615 *tokenptr
, /* Pointer into token */
616 temp
[1024]; /* Temporary string */
617 ipp_t
*request
= NULL
; /* IPP request */
618 ipp_t
*response
= NULL
; /* IPP response */
619 char attr
[128]; /* Attribute name */
620 ipp_op_t op
; /* Operation */
621 ipp_tag_t group
; /* Current group */
622 ipp_tag_t value
; /* Current value type */
623 ipp_attribute_t
*attrptr
, /* Attribute pointer */
624 *found
, /* Found attribute */
625 *lastcol
= NULL
; /* Last collection attribute */
626 char name
[1024]; /* Name of test */
627 char filename
[1024]; /* Filename */
628 _cups_transfer_t transfer
; /* To chunk or not to chunk */
629 int version
, /* IPP version number to use */
630 skip_test
; /* Skip this test? */
631 int num_statuses
= 0; /* Number of valid status codes */
632 _cups_status_t statuses
[100], /* Valid status codes */
633 *last_status
; /* Last STATUS (for predicates) */
634 int num_expects
= 0; /* Number of expected attributes */
635 _cups_expect_t expects
[200], /* Expected attributes */
636 *expect
, /* Current expected attribute */
637 *last_expect
; /* Last EXPECT (for predicates) */
638 int num_displayed
= 0; /* Number of displayed attributes */
639 char *displayed
[200]; /* Displayed attributes */
640 size_t widths
[200]; /* Width of columns */
644 * Open the test file...
647 if ((fp
= fopen(testfile
, "r")) == NULL
)
649 print_fatal_error("Unable to open test file %s - %s", testfile
,
656 * Connect to the server...
659 if ((http
= _httpCreate(vars
->hostname
, vars
->port
, vars
->encryption
,
660 vars
->family
)) == NULL
)
662 print_fatal_error("Unable to connect to %s on port %d - %s", vars
->hostname
,
663 vars
->port
, strerror(errno
));
668 if (httpReconnect(http
))
670 print_fatal_error("Unable to connect to %s on port %d - %s", vars
->hostname
,
671 vars
->port
, strerror(errno
));
676 if (vars
->timeout
> 0.0)
677 _httpSetTimeout(http
, vars
->timeout
, timeout_cb
, NULL
);
683 CUPS_SRAND(time(NULL
));
687 request_id
= (CUPS_RAND() % 1000) * 137 + 1;
689 while (get_token(fp
, token
, sizeof(token
), &linenum
) != NULL
)
692 * Expect an open brace...
695 if (!strcmp(token
, "DEFINE"))
701 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
702 get_token(fp
, temp
, sizeof(temp
), &linenum
))
704 expand_variables(vars
, token
, temp
, sizeof(token
));
705 set_variable(vars
, attr
, token
);
709 print_fatal_error("Missing DEFINE name and/or value on line %d.",
717 else if (!strcmp(token
, "IGNORE-ERRORS"))
724 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
725 (!strcasecmp(temp
, "yes") || !strcasecmp(temp
, "no")))
727 IgnoreErrors
= !strcasecmp(temp
, "yes");
731 print_fatal_error("Missing IGNORE-ERRORS value on line %d.", linenum
);
738 else if (!strcmp(token
, "INCLUDE"))
745 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
748 * Map the filename to and then run the tests...
751 if (!do_tests(vars
, get_filename(testfile
, filename
, temp
,
762 print_fatal_error("Missing INCLUDE filename on line %d.", linenum
);
770 else if (!strcmp(token
, "SKIP-IF-DEFINED"))
773 * SKIP-IF-DEFINED variable
776 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
778 if (get_variable(vars
, temp
))
783 print_fatal_error("Missing SKIP-IF-DEFINED value on line %d.", linenum
);
788 else if (!strcmp(token
, "SKIP-IF-NOT-DEFINED"))
791 * SKIP-IF-NOT-DEFINED variable
794 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
796 if (!get_variable(vars
, temp
))
801 print_fatal_error("Missing SKIP-IF-NOT-DEFINED value on line %d.",
807 else if (!strcmp(token
, "TRANSFER"))
815 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
817 if (!strcmp(temp
, "auto"))
818 Transfer
= _CUPS_TRANSFER_AUTO
;
819 else if (!strcmp(temp
, "chunked"))
820 Transfer
= _CUPS_TRANSFER_CHUNKED
;
821 else if (!strcmp(temp
, "length"))
822 Transfer
= _CUPS_TRANSFER_LENGTH
;
825 print_fatal_error("Bad TRANSFER value \"%s\" on line %d.", temp
,
833 print_fatal_error("Missing TRANSFER value on line %d.", linenum
);
840 else if (!strcmp(token
, "VERSION"))
842 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
844 if (!strcmp(temp
, "1.0"))
846 else if (!strcmp(temp
, "1.1"))
848 else if (!strcmp(temp
, "2.0"))
850 else if (!strcmp(temp
, "2.1"))
852 else if (!strcmp(temp
, "2.2"))
856 print_fatal_error("Bad VERSION \"%s\" on line %d.", temp
, linenum
);
863 print_fatal_error("Missing VERSION number on line %d.", linenum
);
870 else if (strcmp(token
, "{"))
872 print_fatal_error("Unexpected token %s seen on line %d.", token
, linenum
);
878 * Initialize things...
883 if (Output
== _CUPS_OUTPUT_PLIST
)
885 else if (Output
== _CUPS_OUTPUT_TEST
)
886 printf("\"%s\":\n", testfile
);
891 strlcpy(resource
, vars
->resource
, sizeof(resource
));
896 group
= IPP_TAG_ZERO
;
897 ignore_errors
= IgnoreErrors
;
905 strlcpy(name
, testfile
, sizeof(name
));
906 if (strrchr(name
, '.') != NULL
)
907 *strrchr(name
, '.') = '\0';
910 * Parse until we see a close brace...
913 while (get_token(fp
, token
, sizeof(token
), &linenum
) != NULL
)
915 if (strcasecmp(token
, "COUNT") &&
916 strcasecmp(token
, "DEFINE-MATCH") &&
917 strcasecmp(token
, "DEFINE-NO-MATCH") &&
918 strcasecmp(token
, "DEFINE-VALUE") &&
919 strcasecmp(token
, "IF-DEFINED") &&
920 strcasecmp(token
, "IF-NOT-DEFINED") &&
921 strcasecmp(token
, "IN-GROUP") &&
922 strcasecmp(token
, "OF-TYPE") &&
923 strcasecmp(token
, "SAME-COUNT-AS") &&
924 strcasecmp(token
, "WITH-VALUE"))
927 if (strcasecmp(token
, "IF-DEFINED") &&
928 strcasecmp(token
, "IF-NOT-DEFINED"))
931 if (!strcmp(token
, "}"))
933 else if (!strcmp(token
, "{") && lastcol
)
936 * Another collection value
939 ipp_t
*col
= get_collection(vars
, fp
, &linenum
);
940 /* Collection value */
944 ipp_attribute_t
*tempcol
; /* Pointer to new buffer */
948 * Reallocate memory...
951 if ((tempcol
= realloc(lastcol
, sizeof(ipp_attribute_t
) +
952 (lastcol
->num_values
+ 1) *
953 sizeof(ipp_value_t
))) == NULL
)
955 print_fatal_error("Unable to allocate memory on line %d.", linenum
);
960 if (tempcol
!= lastcol
)
963 * Reset pointers in the list...
967 request
->prev
->next
= tempcol
;
969 request
->attrs
= tempcol
;
971 lastcol
= request
->current
= request
->last
= tempcol
;
974 lastcol
->values
[lastcol
->num_values
].collection
= col
;
975 lastcol
->num_values
++;
983 else if (!strcmp(token
, "DEFINE"))
989 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
990 get_token(fp
, temp
, sizeof(temp
), &linenum
))
992 expand_variables(vars
, token
, temp
, sizeof(token
));
993 set_variable(vars
, attr
, token
);
997 print_fatal_error("Missing DEFINE name and/or value on line %d.",
1003 else if (!strcmp(token
, "IGNORE-ERRORS"))
1010 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
1011 (!strcasecmp(temp
, "yes") || !strcasecmp(temp
, "no")))
1013 ignore_errors
= !strcasecmp(temp
, "yes");
1017 print_fatal_error("Missing IGNORE-ERRORS value on line %d.", linenum
);
1024 else if (!strcasecmp(token
, "NAME"))
1030 get_token(fp
, name
, sizeof(name
), &linenum
);
1032 else if (!strcmp(token
, "REQUEST-ID"))
1039 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1041 if (isdigit(temp
[0] & 255))
1042 request_id
= atoi(temp
);
1043 else if (!strcasecmp(temp
, "random"))
1044 request_id
= (CUPS_RAND() % 1000) * 137 + 1;
1047 print_fatal_error("Bad REQUEST-ID value \"%s\" on line %d.", temp
,
1055 print_fatal_error("Missing REQUEST-ID value on line %d.", linenum
);
1060 else if (!strcmp(token
, "SKIP-IF-DEFINED"))
1063 * SKIP-IF-DEFINED variable
1066 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1068 if (get_variable(vars
, temp
))
1073 print_fatal_error("Missing SKIP-IF-DEFINED value on line %d.",
1079 else if (!strcmp(token
, "SKIP-IF-NOT-DEFINED"))
1082 * SKIP-IF-NOT-DEFINED variable
1085 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1087 if (!get_variable(vars
, temp
))
1092 print_fatal_error("Missing SKIP-IF-NOT-DEFINED value on line %d.",
1098 else if (!strcmp(token
, "SKIP-PREVIOUS-ERROR"))
1101 * SKIP-PREVIOUS-ERROR yes
1102 * SKIP-PREVIOUS-ERROR no
1105 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
1106 (!strcasecmp(temp
, "yes") || !strcasecmp(temp
, "no")))
1108 skip_previous
= !strcasecmp(temp
, "yes");
1112 print_fatal_error("Missing SKIP-PREVIOUS-ERROR value on line %d.", linenum
);
1119 else if (!strcmp(token
, "TRANSFER"))
1127 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1129 if (!strcmp(temp
, "auto"))
1130 transfer
= _CUPS_TRANSFER_AUTO
;
1131 else if (!strcmp(temp
, "chunked"))
1132 transfer
= _CUPS_TRANSFER_CHUNKED
;
1133 else if (!strcmp(temp
, "length"))
1134 transfer
= _CUPS_TRANSFER_LENGTH
;
1137 print_fatal_error("Bad TRANSFER value \"%s\" on line %d.", temp
,
1145 print_fatal_error("Missing TRANSFER value on line %d.", linenum
);
1150 else if (!strcasecmp(token
, "VERSION"))
1152 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1154 if (!strcmp(temp
, "0.0"))
1156 else if (!strcmp(temp
, "1.0"))
1158 else if (!strcmp(temp
, "1.1"))
1160 else if (!strcmp(temp
, "2.0"))
1162 else if (!strcmp(temp
, "2.1"))
1164 else if (!strcmp(temp
, "2.2"))
1168 print_fatal_error("Bad VERSION \"%s\" on line %d.", temp
, linenum
);
1175 print_fatal_error("Missing VERSION number on line %d.", linenum
);
1180 else if (!strcasecmp(token
, "RESOURCE"))
1186 if (!get_token(fp
, resource
, sizeof(resource
), &linenum
))
1188 print_fatal_error("Missing RESOURCE path on line %d.", linenum
);
1193 else if (!strcasecmp(token
, "OPERATION"))
1199 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1201 print_fatal_error("Missing OPERATION code on line %d.", linenum
);
1206 if ((op
= ippOpValue(token
)) < 0 && (op
= strtol(token
, NULL
, 0)) == 0)
1208 print_fatal_error("Bad OPERATION code \"%s\" on line %d.", token
,
1214 else if (!strcasecmp(token
, "GROUP"))
1217 * Attribute group...
1220 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1222 print_fatal_error("Missing GROUP tag on line %d.", linenum
);
1227 if ((value
= ippTagValue(token
)) < 0)
1229 print_fatal_error("Bad GROUP tag \"%s\" on line %d.", token
, linenum
);
1235 ippAddSeparator(request
);
1239 else if (!strcasecmp(token
, "DELAY"))
1242 * Delay before operation...
1247 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1249 print_fatal_error("Missing DELAY value on line %d.", linenum
);
1254 if ((delay
= _cupsStrScand(token
, NULL
, localeconv())) <= 0.0)
1256 print_fatal_error("Bad DELAY value \"%s\" on line %d.", token
,
1263 if (Output
== _CUPS_OUTPUT_TEST
)
1264 printf(" [%g second delay]\n", delay
);
1266 usleep((int)(1000000.0 * delay
));
1269 else if (!strcasecmp(token
, "ATTR"))
1275 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1277 print_fatal_error("Missing ATTR value tag on line %d.", linenum
);
1282 if ((value
= ippTagValue(token
)) == IPP_TAG_ZERO
)
1284 print_fatal_error("Bad ATTR value tag \"%s\" on line %d.", token
,
1290 if (!get_token(fp
, attr
, sizeof(attr
), &linenum
))
1292 print_fatal_error("Missing ATTR name on line %d.", linenum
);
1297 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1299 print_fatal_error("Missing ATTR value on line %d.", linenum
);
1304 expand_variables(vars
, token
, temp
, sizeof(token
));
1308 case IPP_TAG_BOOLEAN
:
1309 if (!strcasecmp(token
, "true"))
1310 ippAddBoolean(request
, group
, attr
, 1);
1312 ippAddBoolean(request
, group
, attr
, atoi(token
));
1315 case IPP_TAG_INTEGER
:
1317 if (!strchr(token
, ','))
1318 ippAddInteger(request
, group
, value
, attr
,
1319 strtol(token
, &tokenptr
, 0));
1322 int values
[100], /* Values */
1323 num_values
= 1; /* Number of values */
1325 values
[0] = strtol(token
, &tokenptr
, 10);
1326 while (tokenptr
&& *tokenptr
&&
1327 num_values
< (int)(sizeof(values
) / sizeof(values
[0])))
1329 if (*tokenptr
== ',')
1331 else if (!isdigit(*tokenptr
& 255) && *tokenptr
!= '-')
1334 values
[num_values
] = strtol(tokenptr
, &tokenptr
, 0);
1338 ippAddIntegers(request
, group
, value
, attr
, num_values
, values
);
1341 if (!tokenptr
|| *tokenptr
)
1343 print_fatal_error("Bad %s value \"%s\" on line %d.",
1344 ippTagString(value
), token
, linenum
);
1350 case IPP_TAG_RESOLUTION
:
1352 int xres
, /* X resolution */
1353 yres
; /* Y resolution */
1354 char *ptr
; /* Pointer into value */
1356 xres
= yres
= strtol(token
, (char **)&ptr
, 10);
1357 if (ptr
> token
&& xres
> 0)
1360 yres
= strtol(ptr
+ 1, (char **)&ptr
, 10);
1363 if (ptr
<= token
|| xres
<= 0 || yres
<= 0 || !ptr
||
1364 (strcasecmp(ptr
, "dpi") && strcasecmp(ptr
, "dpc") &&
1365 strcasecmp(ptr
, "other")))
1367 print_fatal_error("Bad resolution value \"%s\" on line %d.",
1373 if (!strcasecmp(ptr
, "dpi"))
1374 ippAddResolution(request
, group
, attr
, IPP_RES_PER_INCH
,
1376 else if (!strcasecmp(ptr
, "dpc"))
1377 ippAddResolution(request
, group
, attr
, IPP_RES_PER_CM
,
1380 ippAddResolution(request
, group
, attr
, (ipp_res_t
)0,
1385 case IPP_TAG_RANGE
:
1387 int lowers
[4], /* Lower value */
1388 uppers
[4], /* Upper values */
1389 num_vals
; /* Number of values */
1392 num_vals
= sscanf(token
, "%d-%d,%d-%d,%d-%d,%d-%d",
1393 lowers
+ 0, uppers
+ 0,
1394 lowers
+ 1, uppers
+ 1,
1395 lowers
+ 2, uppers
+ 2,
1396 lowers
+ 3, uppers
+ 3);
1398 if ((num_vals
& 1) || num_vals
== 0)
1400 print_fatal_error("Bad rangeOfInteger value \"%s\" on line "
1401 "%d.", token
, linenum
);
1406 ippAddRanges(request
, group
, attr
, num_vals
/ 2, lowers
,
1411 case IPP_TAG_BEGIN_COLLECTION
:
1412 if (!strcmp(token
, "{"))
1414 ipp_t
*col
= get_collection(vars
, fp
, &linenum
);
1415 /* Collection value */
1419 lastcol
= ippAddCollection(request
, group
, attr
, col
);
1430 print_fatal_error("Bad ATTR collection value on line %d.",
1438 print_fatal_error("Unsupported ATTR value tag %s on line %d.",
1439 ippTagString(value
), linenum
);
1443 case IPP_TAG_TEXTLANG
:
1444 case IPP_TAG_NAMELANG
:
1447 case IPP_TAG_KEYWORD
:
1449 case IPP_TAG_URISCHEME
:
1450 case IPP_TAG_CHARSET
:
1451 case IPP_TAG_LANGUAGE
:
1452 case IPP_TAG_MIMETYPE
:
1453 if (!strchr(token
, ','))
1454 ippAddString(request
, group
, value
, attr
, NULL
, token
);
1458 * Multiple string values...
1461 int num_values
; /* Number of values */
1462 char *values
[100], /* Values */
1463 *ptr
; /* Pointer to next value */
1469 for (ptr
= strchr(token
, ','); ptr
; ptr
= strchr(ptr
, ','))
1472 values
[num_values
] = ptr
;
1476 ippAddStrings(request
, group
, value
, attr
, num_values
,
1477 NULL
, (const char **)values
);
1482 else if (!strcasecmp(token
, "FILE"))
1488 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1490 print_fatal_error("Missing FILE filename on line %d.", linenum
);
1495 expand_variables(vars
, token
, temp
, sizeof(token
));
1496 get_filename(testfile
, filename
, token
, sizeof(filename
));
1498 else if (!strcasecmp(token
, "STATUS"))
1504 if (num_statuses
>= (int)(sizeof(statuses
) / sizeof(statuses
[0])))
1506 print_fatal_error("Too many STATUS's on line %d.", linenum
);
1511 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1513 print_fatal_error("Missing STATUS code on line %d.", linenum
);
1518 if ((statuses
[num_statuses
].status
= ippErrorValue(token
)) < 0 &&
1519 (statuses
[num_statuses
].status
= strtol(token
, NULL
, 0)) == 0)
1521 print_fatal_error("Bad STATUS code \"%s\" on line %d.", token
,
1527 last_status
= statuses
+ num_statuses
;
1530 last_status
->if_defined
= NULL
;
1531 last_status
->if_not_defined
= NULL
;
1533 else if (!strcasecmp(token
, "EXPECT"))
1536 * Expected attributes...
1539 if (num_expects
>= (int)(sizeof(expects
) / sizeof(expects
[0])))
1541 print_fatal_error("Too many EXPECT's on line %d.", linenum
);
1546 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1548 print_fatal_error("Missing EXPECT name on line %d.", linenum
);
1553 last_expect
= expects
+ num_expects
;
1556 memset(last_expect
, 0, sizeof(_cups_expect_t
));
1558 if (token
[0] == '!')
1560 last_expect
->not_expect
= 1;
1561 last_expect
->name
= strdup(token
+ 1);
1563 else if (token
[0] == '?')
1565 last_expect
->optional
= 1;
1566 last_expect
->name
= strdup(token
+ 1);
1569 last_expect
->name
= strdup(token
);
1571 else if (!strcasecmp(token
, "COUNT"))
1573 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1575 print_fatal_error("Missing COUNT number on line %d.", linenum
);
1580 if ((i
= atoi(token
)) <= 0)
1582 print_fatal_error("Bad COUNT \"%s\" on line %d.", token
, linenum
);
1588 last_expect
->count
= i
;
1591 print_fatal_error("COUNT without a preceding EXPECT on line %d.",
1597 else if (!strcasecmp(token
, "DEFINE-MATCH"))
1599 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1601 print_fatal_error("Missing DEFINE-MATCH variable on line %d.",
1608 last_expect
->define_match
= strdup(token
);
1611 print_fatal_error("DEFINE-MATCH without a preceding EXPECT on line "
1617 else if (!strcasecmp(token
, "DEFINE-NO-MATCH"))
1619 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1621 print_fatal_error("Missing DEFINE-NO-MATCH variable on line %d.",
1628 last_expect
->define_no_match
= strdup(token
);
1631 print_fatal_error("DEFINE-NO-MATCH without a preceding EXPECT on "
1632 "line %d.", linenum
);
1637 else if (!strcasecmp(token
, "DEFINE-VALUE"))
1639 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1641 print_fatal_error("Missing DEFINE-VALUE variable on line %d.",
1648 last_expect
->define_value
= strdup(token
);
1651 print_fatal_error("DEFINE-VALUE without a preceding EXPECT on line "
1657 else if (!strcasecmp(token
, "OF-TYPE"))
1659 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1661 print_fatal_error("Missing OF-TYPE value tag(s) on line %d.",
1668 last_expect
->of_type
= strdup(token
);
1671 print_fatal_error("OF-TYPE without a preceding EXPECT on line %d.",
1677 else if (!strcasecmp(token
, "IN-GROUP"))
1679 ipp_tag_t in_group
; /* IN-GROUP value */
1682 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1684 print_fatal_error("Missing IN-GROUP group tag on line %d.", linenum
);
1689 if ((in_group
= ippTagValue(token
)) == (ipp_tag_t
)-1)
1692 else if (last_expect
)
1693 last_expect
->in_group
= in_group
;
1696 print_fatal_error("IN-GROUP without a preceding EXPECT on line %d.",
1702 else if (!strcasecmp(token
, "SAME-COUNT-AS"))
1704 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1706 print_fatal_error("Missing SAME-COUNT-AS name on line %d.", linenum
);
1712 last_expect
->same_count_as
= strdup(token
);
1715 print_fatal_error("SAME-COUNT-AS without a preceding EXPECT on line "
1721 else if (!strcasecmp(token
, "IF-DEFINED"))
1723 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1725 print_fatal_error("Missing IF-DEFINED name on line %d.", linenum
);
1731 last_expect
->if_defined
= strdup(token
);
1732 else if (last_status
)
1733 last_status
->if_defined
= strdup(token
);
1736 print_fatal_error("IF-DEFINED without a preceding EXPECT or STATUS "
1737 "on line %d.", linenum
);
1742 else if (!strcasecmp(token
, "IF-NOT-DEFINED"))
1744 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1746 print_fatal_error("Missing IF-NOT-DEFINED name on line %d.", linenum
);
1752 last_expect
->if_not_defined
= strdup(token
);
1753 else if (last_status
)
1754 last_status
->if_not_defined
= strdup(token
);
1757 print_fatal_error("IF-NOT-DEFINED without a preceding EXPECT or STATUS "
1758 "on line %d.", linenum
);
1763 else if (!strcasecmp(token
, "WITH-VALUE"))
1765 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1767 print_fatal_error("Missing WITH-VALUE value on line %d.", linenum
);
1775 * Expand any variables in the value and then save it.
1778 expand_variables(vars
, token
, temp
, sizeof(token
));
1780 tokenptr
= token
+ strlen(token
) - 1;
1782 if (token
[0] == '/' && tokenptr
> token
&& *tokenptr
== '/')
1785 * WITH-VALUE is a POSIX extended regular expression.
1788 last_expect
->with_value
= calloc(1, tokenptr
- token
);
1789 last_expect
->with_regex
= 1;
1791 if (last_expect
->with_value
)
1792 memcpy(last_expect
->with_value
, token
+ 1, tokenptr
- token
- 1);
1797 * WITH-VALUE is a literal value...
1800 last_expect
->with_value
= strdup(token
);
1805 print_fatal_error("WITH-VALUE without a preceding EXPECT on line %d.",
1811 else if (!strcasecmp(token
, "DISPLAY"))
1814 * Display attributes...
1817 if (num_displayed
>= (int)(sizeof(displayed
) / sizeof(displayed
[0])))
1819 print_fatal_error("Too many DISPLAY's on line %d", linenum
);
1824 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1826 print_fatal_error("Missing DISPLAY name on line %d.", linenum
);
1831 displayed
[num_displayed
] = strdup(token
);
1836 print_fatal_error("Unexpected token %s seen on line %d.", token
,
1844 * Submit the IPP request...
1847 request
->request
.op
.version
[0] = version
/ 10;
1848 request
->request
.op
.version
[1] = version
% 10;
1849 request
->request
.op
.operation_id
= op
;
1850 request
->request
.op
.request_id
= request_id
;
1852 if (Output
== _CUPS_OUTPUT_PLIST
)
1855 puts("<key>Name</key>");
1856 print_xml_string("string", name
);
1857 puts("<key>Operation</key>");
1858 print_xml_string("string", ippOpString(op
));
1859 puts("<key>RequestAttributes</key>");
1862 for (attrptr
= request
->attrs
, group
= attrptr
->group_tag
;
1864 attrptr
= attrptr
->next
)
1865 print_attr(attrptr
, &group
);
1869 else if (Output
== _CUPS_OUTPUT_TEST
)
1873 printf(" %s:\n", ippOpString(op
));
1875 for (attrptr
= request
->attrs
; attrptr
; attrptr
= attrptr
->next
)
1876 print_attr(attrptr
, NULL
);
1879 printf(" %-69.69s [", name
);
1883 if ((skip_previous
&& !prev_pass
) || skip_test
)
1888 if (Output
== _CUPS_OUTPUT_PLIST
)
1890 puts("<key>Successful</key>");
1892 puts("<key>StatusCode</key>");
1893 print_xml_string("string", "skip");
1894 puts("<key>ResponseAttributes</key>");
1898 else if (Output
== _CUPS_OUTPUT_TEST
)
1904 if (transfer
== _CUPS_TRANSFER_CHUNKED
||
1905 (transfer
== _CUPS_TRANSFER_AUTO
&& filename
[0]))
1908 * Send request using chunking...
1911 http_status_t status
= cupsSendRequest(http
, request
, resource
, 0);
1913 if (status
== HTTP_CONTINUE
&& filename
[0])
1915 int fd
; /* File to send */
1916 char buffer
[8192]; /* Copy buffer */
1917 ssize_t bytes
; /* Bytes read/written */
1919 if ((fd
= open(filename
, O_RDONLY
| O_BINARY
)) >= 0)
1921 while ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1922 if ((status
= cupsWriteRequestData(http
, buffer
,
1923 bytes
)) != HTTP_CONTINUE
)
1928 snprintf(buffer
, sizeof(buffer
), "%s: %s", filename
, strerror(errno
));
1929 _cupsSetError(IPP_INTERNAL_ERROR
, buffer
, 0);
1931 status
= HTTP_ERROR
;
1937 if (status
== HTTP_CONTINUE
)
1938 response
= cupsGetResponse(http
, resource
);
1942 else if (filename
[0])
1943 response
= cupsDoFileRequest(http
, request
, resource
, filename
);
1945 response
= cupsDoRequest(http
, request
, resource
);
1951 prev_pass
= pass
= 0;
1954 if (http
->version
!= HTTP_1_1
)
1955 prev_pass
= pass
= 0;
1957 if (response
->request
.status
.request_id
!= request_id
)
1958 prev_pass
= pass
= 0;
1961 (response
->request
.status
.version
[0] != (version
/ 10) ||
1962 response
->request
.status
.version
[1] != (version
% 10)))
1963 prev_pass
= pass
= 0;
1965 if ((attrptr
= ippFindAttribute(response
, "job-id",
1966 IPP_TAG_INTEGER
)) != NULL
)
1968 snprintf(temp
, sizeof(temp
), "%d", attrptr
->values
[0].integer
);
1969 set_variable(vars
, "job-id", temp
);
1972 if ((attrptr
= ippFindAttribute(response
, "job-uri",
1973 IPP_TAG_URI
)) != NULL
)
1974 set_variable(vars
, "job-uri", attrptr
->values
[0].string
.text
);
1976 if ((attrptr
= ippFindAttribute(response
, "notify-subscription-id",
1977 IPP_TAG_INTEGER
)) != NULL
)
1979 snprintf(temp
, sizeof(temp
), "%d", attrptr
->values
[0].integer
);
1980 set_variable(vars
, "notify-subscription-id", temp
);
1983 attrptr
= response
->attrs
;
1984 if (!attrptr
|| !attrptr
->name
||
1985 attrptr
->value_tag
!= IPP_TAG_CHARSET
||
1986 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
1987 attrptr
->num_values
!= 1 ||
1988 strcmp(attrptr
->name
, "attributes-charset"))
1989 prev_pass
= pass
= 0;
1993 attrptr
= attrptr
->next
;
1994 if (!attrptr
|| !attrptr
->name
||
1995 attrptr
->value_tag
!= IPP_TAG_LANGUAGE
||
1996 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
1997 attrptr
->num_values
!= 1 ||
1998 strcmp(attrptr
->name
, "attributes-natural-language"))
1999 prev_pass
= pass
= 0;
2002 if ((attrptr
= ippFindAttribute(response
, "status-message",
2003 IPP_TAG_ZERO
)) != NULL
&&
2004 (attrptr
->value_tag
!= IPP_TAG_TEXT
||
2005 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2006 attrptr
->num_values
!= 1 ||
2007 (attrptr
->value_tag
== IPP_TAG_TEXT
&&
2008 strlen(attrptr
->values
[0].string
.text
) > 255)))
2009 prev_pass
= pass
= 0;
2011 if ((attrptr
= ippFindAttribute(response
, "detailed-status-message",
2012 IPP_TAG_ZERO
)) != NULL
&&
2013 (attrptr
->value_tag
!= IPP_TAG_TEXT
||
2014 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2015 attrptr
->num_values
!= 1 ||
2016 (attrptr
->value_tag
== IPP_TAG_TEXT
&&
2017 strlen(attrptr
->values
[0].string
.text
) > 1023)))
2018 prev_pass
= pass
= 0;
2020 for (attrptr
= response
->attrs
, group
= attrptr
->group_tag
;
2022 attrptr
= attrptr
->next
)
2024 if (attrptr
->group_tag
< group
&& attrptr
->group_tag
!= IPP_TAG_ZERO
)
2026 prev_pass
= pass
= 0;
2030 if (!validate_attr(attrptr
, 0))
2032 prev_pass
= pass
= 0;
2037 for (i
= 0; i
< num_statuses
; i
++)
2039 if (statuses
[i
].if_defined
&&
2040 !get_variable(vars
, statuses
[i
].if_defined
))
2043 if (statuses
[i
].if_not_defined
&&
2044 get_variable(vars
, statuses
[i
].if_not_defined
))
2047 if (response
->request
.status
.status_code
== statuses
[i
].status
)
2051 if (i
== num_statuses
&& num_statuses
> 0)
2052 prev_pass
= pass
= 0;
2055 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
2057 if (expect
->if_defined
&& !get_variable(vars
, expect
->if_defined
))
2060 if (expect
->if_not_defined
&&
2061 get_variable(vars
, expect
->if_not_defined
))
2064 found
= ippFindAttribute(response
, expect
->name
, IPP_TAG_ZERO
);
2066 if ((found
&& expect
->not_expect
) ||
2067 (!found
&& !(expect
->not_expect
|| expect
->optional
)) ||
2068 (found
&& !expect_matches(expect
, found
->value_tag
)) ||
2069 (found
&& expect
->in_group
&&
2070 found
->group_tag
!= expect
->in_group
))
2072 if (expect
->define_no_match
)
2073 set_variable(vars
, expect
->define_no_match
, "1");
2074 else if (!expect
->define_match
)
2075 prev_pass
= pass
= 0;
2081 !with_value(expect
->with_value
, expect
->with_regex
, found
, 0))
2083 if (expect
->define_no_match
)
2084 set_variable(vars
, expect
->define_no_match
, "1");
2085 else if (!expect
->define_match
)
2086 prev_pass
= pass
= 0;
2091 if (found
&& expect
->count
> 0 && found
->num_values
!= expect
->count
)
2093 if (expect
->define_no_match
)
2094 set_variable(vars
, expect
->define_no_match
, "1");
2095 else if (!expect
->define_match
)
2096 prev_pass
= pass
= 0;
2101 if (found
&& expect
->same_count_as
)
2103 attrptr
= ippFindAttribute(response
, expect
->same_count_as
,
2106 if (!attrptr
|| attrptr
->num_values
!= found
->num_values
)
2108 if (expect
->define_no_match
)
2109 set_variable(vars
, expect
->define_no_match
, "1");
2110 else if (!expect
->define_match
)
2111 prev_pass
= pass
= 0;
2117 if (found
&& expect
->define_match
)
2118 set_variable(vars
, expect
->define_match
, "1");
2120 if (found
&& expect
->define_value
)
2122 _ippAttrString(found
, token
, sizeof(token
));
2123 set_variable(vars
, expect
->define_value
, token
);
2129 if (Output
== _CUPS_OUTPUT_PLIST
)
2131 puts("<key>Successful</key>");
2132 puts(prev_pass
? "<true />" : "<false />");
2133 puts("<key>StatusCode</key>");
2134 print_xml_string("string", ippErrorString(cupsLastError()));
2135 puts("<key>ResponseAttributes</key>");
2138 for (attrptr
= response
? response
->attrs
: NULL
,
2139 group
= attrptr
? attrptr
->group_tag
: IPP_TAG_ZERO
;
2141 attrptr
= attrptr
->next
)
2142 print_attr(attrptr
, &group
);
2146 else if (Output
== _CUPS_OUTPUT_TEST
)
2148 puts(prev_pass
? "PASS]" : "FAIL]");
2150 if (Verbosity
&& response
)
2152 printf(" RECEIVED: %lu bytes in response\n",
2153 (unsigned long)ippLength(response
));
2154 printf(" status-code = %x (%s)\n", cupsLastError(),
2155 ippErrorString(cupsLastError()));
2157 for (attrptr
= response
->attrs
;
2159 attrptr
= attrptr
->next
)
2161 print_attr(attrptr
, NULL
);
2165 else if (!prev_pass
)
2166 fprintf(stderr
, "%s\n", cupsLastErrorString());
2168 if (prev_pass
&& Output
!= _CUPS_OUTPUT_PLIST
&&
2169 Output
!= _CUPS_OUTPUT_QUIET
&& !Verbosity
&& num_displayed
> 0)
2171 if (Output
>= _CUPS_OUTPUT_LIST
)
2173 size_t width
; /* Length of value */
2176 for (i
= 0; i
< num_displayed
; i
++)
2178 widths
[i
] = strlen(displayed
[i
]);
2180 for (attrptr
= ippFindAttribute(response
, displayed
[i
], IPP_TAG_ZERO
);
2182 attrptr
= ippFindNextAttribute(response
, displayed
[i
],
2185 width
= _ippAttrString(attrptr
, NULL
, 0);
2186 if (width
> widths
[i
])
2191 if (Output
== _CUPS_OUTPUT_CSV
)
2192 print_csv(NULL
, num_displayed
, displayed
, widths
);
2194 print_line(NULL
, num_displayed
, displayed
, widths
);
2196 attrptr
= response
->attrs
;
2200 while (attrptr
&& attrptr
->group_tag
<= IPP_TAG_OPERATION
)
2201 attrptr
= attrptr
->next
;
2205 if (Output
== _CUPS_OUTPUT_CSV
)
2206 print_csv(attrptr
, num_displayed
, displayed
, widths
);
2208 print_line(attrptr
, num_displayed
, displayed
, widths
);
2210 while (attrptr
&& attrptr
->group_tag
> IPP_TAG_OPERATION
)
2211 attrptr
= attrptr
->next
;
2217 for (attrptr
= response
->attrs
;
2219 attrptr
= attrptr
->next
)
2223 for (i
= 0; i
< num_displayed
; i
++)
2225 if (!strcmp(displayed
[i
], attrptr
->name
))
2227 print_attr(attrptr
, NULL
);
2235 else if (!prev_pass
)
2237 if (Output
== _CUPS_OUTPUT_PLIST
)
2239 puts("<key>Errors</key>");
2243 if (http
->version
!= HTTP_1_1
)
2244 print_test_error("Bad HTTP version (%d.%d)", http
->version
/ 100,
2245 http
->version
% 100);
2248 print_test_error("IPP request failed with status %s (%s)",
2249 ippErrorString(cupsLastError()),
2250 cupsLastErrorString());
2254 (response
->request
.status
.version
[0] != (version
/ 10) ||
2255 response
->request
.status
.version
[1] != (version
% 10)))
2256 print_test_error("Bad version %d.%d in response - expected %d.%d "
2257 "(RFC 2911 section 3.1.8).",
2258 response
->request
.status
.version
[0],
2259 response
->request
.status
.version
[1],
2260 version
/ 10, version
% 10);
2262 if (response
->request
.status
.request_id
!= request_id
)
2263 print_test_error("Bad request ID %d in response - expected %d "
2264 "(RFC 2911 section 3.1.1)",
2265 response
->request
.status
.request_id
, request_id
);
2267 attrptr
= response
->attrs
;
2269 print_test_error("Missing first attribute \"attributes-charset "
2270 "(charset)\" in group operation-attributes-tag "
2271 "(RFC 2911 section 3.1.4).");
2274 if (!attrptr
->name
||
2275 attrptr
->value_tag
!= IPP_TAG_CHARSET
||
2276 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2277 attrptr
->num_values
!= 1 ||
2278 strcmp(attrptr
->name
, "attributes-charset"))
2279 print_test_error("Bad first attribute \"%s (%s%s)\" in group %s, "
2280 "expected \"attributes-charset (charset)\" in "
2281 "group operation-attributes-tag (RFC 2911 section "
2283 attrptr
->name
? attrptr
->name
: "(null)",
2284 attrptr
->num_values
> 1 ? "1setOf " : "",
2285 ippTagString(attrptr
->value_tag
),
2286 ippTagString(attrptr
->group_tag
));
2288 attrptr
= attrptr
->next
;
2290 print_test_error("Missing second attribute \"attributes-natural-"
2291 "language (naturalLanguage)\" in group "
2292 "operation-attributes-tag (RFC 2911 section "
2294 else if (!attrptr
->name
||
2295 attrptr
->value_tag
!= IPP_TAG_LANGUAGE
||
2296 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2297 attrptr
->num_values
!= 1 ||
2298 strcmp(attrptr
->name
, "attributes-natural-language"))
2299 print_test_error("Bad first attribute \"%s (%s%s)\" in group %s, "
2300 "expected \"attributes-natural-language "
2301 "(naturalLanguage)\" in group "
2302 "operation-attributes-tag (RFC 2911 section "
2304 attrptr
->name
? attrptr
->name
: "(null)",
2305 attrptr
->num_values
> 1 ? "1setOf " : "",
2306 ippTagString(attrptr
->value_tag
),
2307 ippTagString(attrptr
->group_tag
));
2310 if ((attrptr
= ippFindAttribute(response
, "status-message",
2311 IPP_TAG_ZERO
)) != NULL
)
2313 if (attrptr
->value_tag
!= IPP_TAG_TEXT
)
2314 print_test_error("status-message (text(255)) has wrong value tag "
2315 "%s (RFC 2911 section 3.1.6.2).",
2316 ippTagString(attrptr
->value_tag
));
2317 if (attrptr
->group_tag
!= IPP_TAG_OPERATION
)
2318 print_test_error("status-message (text(255)) has wrong group tag "
2319 "%s (RFC 2911 section 3.1.6.2).",
2320 ippTagString(attrptr
->group_tag
));
2321 if (attrptr
->num_values
!= 1)
2322 print_test_error("status-message (text(255)) has %d values "
2323 "(RFC 2911 section 3.1.6.2).",
2324 attrptr
->num_values
);
2325 if (attrptr
->value_tag
== IPP_TAG_TEXT
&&
2326 strlen(attrptr
->values
[0].string
.text
) > 255)
2327 print_test_error("status-message (text(255)) has bad length %d"
2328 " (RFC 2911 section 3.1.6.2).",
2329 (int)strlen(attrptr
->values
[0].string
.text
));
2332 if ((attrptr
= ippFindAttribute(response
, "detailed-status-message",
2333 IPP_TAG_ZERO
)) != NULL
)
2335 if (attrptr
->value_tag
!= IPP_TAG_TEXT
)
2336 print_test_error("detailed-status-message (text(MAX)) has wrong "
2337 "value tag %s (RFC 2911 section 3.1.6.3).",
2338 ippTagString(attrptr
->value_tag
));
2339 if (attrptr
->group_tag
!= IPP_TAG_OPERATION
)
2340 print_test_error("detailed-status-message (text(MAX)) has wrong "
2341 "group tag %s (RFC 2911 section 3.1.6.3).",
2342 ippTagString(attrptr
->group_tag
));
2343 if (attrptr
->num_values
!= 1)
2344 print_test_error("detailed-status-message (text(MAX)) has %d values"
2345 " (RFC 2911 section 3.1.6.3).",
2346 attrptr
->num_values
);
2347 if (attrptr
->value_tag
== IPP_TAG_TEXT
&&
2348 strlen(attrptr
->values
[0].string
.text
) > 1023)
2349 print_test_error("detailed-status-message (text(MAX)) has bad "
2350 "length %d (RFC 2911 section 3.1.6.3).",
2351 (int)strlen(attrptr
->values
[0].string
.text
));
2354 for (attrptr
= response
->attrs
, group
= attrptr
->group_tag
;
2356 attrptr
= attrptr
->next
)
2358 if (attrptr
->group_tag
< group
&& attrptr
->group_tag
!= IPP_TAG_ZERO
)
2359 print_test_error("Attribute groups out of order (%s < %s)",
2360 ippTagString(attrptr
->group_tag
),
2361 ippTagString(group
));
2363 validate_attr(attrptr
, 1);
2366 for (i
= 0; i
< num_statuses
; i
++)
2368 if (statuses
[i
].if_defined
&&
2369 !get_variable(vars
, statuses
[i
].if_defined
))
2372 if (statuses
[i
].if_not_defined
&&
2373 get_variable(vars
, statuses
[i
].if_not_defined
))
2376 if (response
->request
.status
.status_code
== statuses
[i
].status
)
2380 if (i
== num_statuses
&& num_statuses
> 0)
2382 for (i
= 0; i
< num_statuses
; i
++)
2384 if (statuses
[i
].if_defined
&&
2385 !get_variable(vars
, statuses
[i
].if_defined
))
2388 if (statuses
[i
].if_not_defined
&&
2389 get_variable(vars
, statuses
[i
].if_not_defined
))
2392 print_test_error("EXPECTED: STATUS %s (got %s)",
2393 ippErrorString(statuses
[i
].status
),
2394 ippErrorString(cupsLastError()));
2397 if ((attrptr
= ippFindAttribute(response
, "status-message",
2398 IPP_TAG_TEXT
)) != NULL
)
2399 print_test_error("status-message=\"%s\"",
2400 attrptr
->values
[0].string
.text
);
2403 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
2405 if (expect
->define_match
|| expect
->define_no_match
)
2408 if (expect
->if_defined
&& !get_variable(vars
, expect
->if_defined
))
2411 if (expect
->if_not_defined
&&
2412 get_variable(vars
, expect
->if_not_defined
))
2415 found
= ippFindAttribute(response
, expect
->name
, IPP_TAG_ZERO
);
2417 if (found
&& expect
->not_expect
)
2418 print_test_error("NOT EXPECTED: %s", expect
->name
);
2419 else if (!found
&& !(expect
->not_expect
|| expect
->optional
))
2420 print_test_error("EXPECTED: %s", expect
->name
);
2423 if (!expect_matches(expect
, found
->value_tag
))
2424 print_test_error("EXPECTED: %s OF-TYPE %s (got %s)",
2425 expect
->name
, expect
->of_type
,
2426 ippTagString(found
->value_tag
));
2428 if (expect
->in_group
&& found
->group_tag
!= expect
->in_group
)
2429 print_test_error("EXPECTED: %s IN-GROUP %s (got %s).",
2430 expect
->name
, ippTagString(expect
->in_group
),
2431 ippTagString(found
->group_tag
));
2433 if (!with_value(expect
->with_value
, expect
->with_regex
, found
, 0))
2435 if (expect
->with_regex
)
2436 print_test_error("EXPECTED: %s WITH-VALUE /%s/",
2437 expect
->name
, expect
->with_value
);
2439 print_test_error("EXPECTED: %s WITH-VALUE \"%s\"",
2440 expect
->name
, expect
->with_value
);
2442 with_value(expect
->with_value
, expect
->with_regex
, found
, 1);
2445 if (expect
->count
> 0 && found
->num_values
!= expect
->count
)
2447 print_test_error("EXPECTED: %s COUNT %d (got %d)", expect
->name
,
2448 expect
->count
, found
->num_values
);
2451 if (expect
->same_count_as
)
2453 attrptr
= ippFindAttribute(response
, expect
->same_count_as
,
2457 print_test_error("EXPECTED: %s (%d values) SAME-COUNT-AS %s "
2458 "(not returned)", expect
->name
,
2459 found
->num_values
, expect
->same_count_as
);
2460 else if (attrptr
->num_values
!= found
->num_values
)
2461 print_test_error("EXPECTED: %s (%d values) SAME-COUNT-AS %s "
2462 "(%d values)", expect
->name
, found
->num_values
,
2463 expect
->same_count_as
, attrptr
->num_values
);
2469 if (Output
== _CUPS_OUTPUT_PLIST
)
2473 if (Output
== _CUPS_OUTPUT_PLIST
)
2478 ippDelete(response
);
2481 for (i
= 0; i
< num_statuses
; i
++)
2483 if (statuses
[i
].if_defined
)
2484 free(statuses
[i
].if_defined
);
2485 if (statuses
[i
].if_not_defined
)
2486 free(statuses
[i
].if_not_defined
);
2490 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
2493 if (expect
->of_type
)
2494 free(expect
->of_type
);
2495 if (expect
->same_count_as
)
2496 free(expect
->same_count_as
);
2497 if (expect
->if_defined
)
2498 free(expect
->if_defined
);
2499 if (expect
->if_not_defined
)
2500 free(expect
->if_not_defined
);
2501 if (expect
->with_value
)
2502 free(expect
->with_value
);
2503 if (expect
->define_match
)
2504 free(expect
->define_match
);
2505 if (expect
->define_no_match
)
2506 free(expect
->define_no_match
);
2507 if (expect
->define_value
)
2508 free(expect
->define_value
);
2512 for (i
= 0; i
< num_displayed
; i
++)
2516 if (!ignore_errors
&& !prev_pass
)
2527 ippDelete(response
);
2529 for (i
= 0; i
< num_statuses
; i
++)
2531 if (statuses
[i
].if_defined
)
2532 free(statuses
[i
].if_defined
);
2533 if (statuses
[i
].if_not_defined
)
2534 free(statuses
[i
].if_not_defined
);
2537 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
2540 if (expect
->of_type
)
2541 free(expect
->of_type
);
2542 if (expect
->same_count_as
)
2543 free(expect
->same_count_as
);
2544 if (expect
->if_defined
)
2545 free(expect
->if_defined
);
2546 if (expect
->if_not_defined
)
2547 free(expect
->if_not_defined
);
2548 if (expect
->with_value
)
2549 free(expect
->with_value
);
2550 if (expect
->define_match
)
2551 free(expect
->define_match
);
2552 if (expect
->define_no_match
)
2553 free(expect
->define_no_match
);
2554 if (expect
->define_value
)
2555 free(expect
->define_value
);
2558 for (i
= 0; i
< num_displayed
; i
++)
2566 * 'expand_variables()' - Expand variables in a string.
2570 expand_variables(_cups_vars_t
*vars
, /* I - Variables */
2571 char *dst
, /* I - Destination string buffer */
2572 const char *src
, /* I - Source string */
2573 size_t dstsize
) /* I - Size of destination buffer */
2575 char *dstptr
, /* Pointer into destination */
2576 *dstend
, /* End of destination */
2577 temp
[256], /* Temporary string */
2578 *tempptr
; /* Pointer into temporary string */
2579 const char *value
; /* Value to substitute */
2583 dstend
= dst
+ dstsize
- 1;
2585 while (*src
&& dstptr
< dstend
)
2590 * Substitute a string/number...
2593 if (!strncmp(src
, "$$", 2))
2598 else if (!strncmp(src
, "$ENV[", 5))
2600 strlcpy(temp
, src
+ 5, sizeof(temp
));
2602 for (tempptr
= temp
; *tempptr
; tempptr
++)
2603 if (*tempptr
== ']')
2609 value
= getenv(temp
);
2610 src
+= tempptr
- temp
+ 5;
2614 strlcpy(temp
, src
+ 1, sizeof(temp
));
2616 for (tempptr
= temp
; *tempptr
; tempptr
++)
2617 if (!isalnum(*tempptr
& 255) && *tempptr
!= '-' && *tempptr
!= '_')
2623 if (!strcmp(temp
, "uri"))
2625 else if (!strcmp(temp
, "filename"))
2626 value
= vars
->filename
;
2627 else if (!strcmp(temp
, "scheme") || !strcmp(temp
, "method"))
2628 value
= vars
->scheme
;
2629 else if (!strcmp(temp
, "username"))
2630 value
= vars
->userpass
;
2631 else if (!strcmp(temp
, "hostname"))
2632 value
= vars
->hostname
;
2633 else if (!strcmp(temp
, "port"))
2635 snprintf(temp
, sizeof(temp
), "%d", vars
->port
);
2638 else if (!strcmp(temp
, "resource"))
2639 value
= vars
->resource
;
2640 else if (!strcmp(temp
, "user"))
2643 value
= get_variable(vars
, temp
);
2645 src
+= tempptr
- temp
+ 1;
2655 strlcpy(dstptr
, value
, dstend
- dstptr
+ 1);
2656 dstptr
+= strlen(dstptr
);
2668 * 'expect_matches()' - Return true if the tag matches the specification.
2671 static int /* O - 1 if matches, 0 otherwise */
2673 _cups_expect_t
*expect
, /* I - Expected attribute */
2674 ipp_tag_t value_tag
) /* I - Value tag for attribute */
2676 int match
; /* Match? */
2677 char *of_type
, /* Type name to match */
2678 *next
, /* Next name to match */
2679 sep
; /* Separator character */
2683 * If we don't expect a particular type, return immediately...
2686 if (!expect
->of_type
)
2690 * Parse the "of_type" value since the string can contain multiple attribute
2691 * types separated by "," or "|"...
2694 for (of_type
= expect
->of_type
, match
= 0; !match
&& *of_type
; of_type
= next
)
2697 * Find the next separator, and set it (temporarily) to nul if present.
2700 for (next
= of_type
; *next
&& *next
!= '|' && *next
!= ','; next
++);
2702 if ((sep
= *next
) != '\0')
2706 * Support some meta-types to make it easier to write the test file.
2709 if (!strcmp(of_type
, "text"))
2710 match
= value_tag
== IPP_TAG_TEXTLANG
|| value_tag
== IPP_TAG_TEXT
;
2711 else if (!strcmp(of_type
, "name"))
2712 match
= value_tag
== IPP_TAG_NAMELANG
|| value_tag
== IPP_TAG_NAME
;
2713 else if (!strcmp(of_type
, "collection"))
2714 match
= value_tag
== IPP_TAG_BEGIN_COLLECTION
;
2716 match
= value_tag
== ippTagValue(of_type
);
2719 * Restore the separator if we have one...
2731 * 'get_collection()' - Get a collection value from the current test file.
2734 static ipp_t
* /* O - Collection value */
2735 get_collection(_cups_vars_t
*vars
, /* I - Variables */
2736 FILE *fp
, /* I - File to read from */
2737 int *linenum
) /* IO - Line number */
2739 char token
[1024], /* Token from file */
2740 temp
[1024], /* Temporary string */
2741 attr
[128]; /* Attribute name */
2742 ipp_tag_t value
; /* Current value type */
2743 ipp_t
*col
= ippNew(); /* Collection value */
2744 ipp_attribute_t
*lastcol
= NULL
; /* Last collection attribute */
2747 while (get_token(fp
, token
, sizeof(token
), linenum
) != NULL
)
2749 if (!strcmp(token
, "}"))
2751 else if (!strcmp(token
, "{") && lastcol
)
2754 * Another collection value
2757 ipp_t
*subcol
= get_collection(vars
, fp
, linenum
);
2758 /* Collection value */
2762 ipp_attribute_t
*tempcol
; /* Pointer to new buffer */
2766 * Reallocate memory...
2769 if ((tempcol
= realloc(lastcol
, sizeof(ipp_attribute_t
) +
2770 (lastcol
->num_values
+ 1) *
2771 sizeof(ipp_value_t
))) == NULL
)
2773 print_fatal_error("Unable to allocate memory on line %d.", *linenum
);
2777 if (tempcol
!= lastcol
)
2780 * Reset pointers in the list...
2784 col
->prev
->next
= tempcol
;
2786 col
->attrs
= tempcol
;
2788 lastcol
= col
->current
= col
->last
= tempcol
;
2791 lastcol
->values
[lastcol
->num_values
].collection
= subcol
;
2792 lastcol
->num_values
++;
2797 else if (!strcasecmp(token
, "MEMBER"))
2805 if (!get_token(fp
, token
, sizeof(token
), linenum
))
2807 print_fatal_error("Missing MEMBER value tag on line %d.", *linenum
);
2811 if ((value
= ippTagValue(token
)) == IPP_TAG_ZERO
)
2813 print_fatal_error("Bad MEMBER value tag \"%s\" on line %d.", token
,
2818 if (!get_token(fp
, attr
, sizeof(attr
), linenum
))
2820 print_fatal_error("Missing MEMBER name on line %d.", *linenum
);
2824 if (!get_token(fp
, temp
, sizeof(temp
), linenum
))
2826 print_fatal_error("Missing MEMBER value on line %d.", *linenum
);
2830 expand_variables(vars
, token
, temp
, sizeof(token
));
2834 case IPP_TAG_BOOLEAN
:
2835 if (!strcasecmp(token
, "true"))
2836 ippAddBoolean(col
, IPP_TAG_ZERO
, attr
, 1);
2838 ippAddBoolean(col
, IPP_TAG_ZERO
, attr
, atoi(token
));
2841 case IPP_TAG_INTEGER
:
2843 ippAddInteger(col
, IPP_TAG_ZERO
, value
, attr
, atoi(token
));
2846 case IPP_TAG_RESOLUTION
:
2848 int xres
, /* X resolution */
2849 yres
; /* Y resolution */
2850 char units
[6]; /* Units */
2852 if (sscanf(token
, "%dx%d%5s", &xres
, &yres
, units
) != 3 ||
2853 (strcasecmp(units
, "dpi") && strcasecmp(units
, "dpc") &&
2854 strcasecmp(units
, "other")))
2856 print_fatal_error("Bad resolution value \"%s\" on line %d.",
2861 if (!strcasecmp(units
, "dpi"))
2862 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, xres
, yres
,
2864 else if (!strcasecmp(units
, "dpc"))
2865 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, xres
, yres
,
2868 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, xres
, yres
,
2873 case IPP_TAG_RANGE
:
2875 int lowers
[4], /* Lower value */
2876 uppers
[4], /* Upper values */
2877 num_vals
; /* Number of values */
2880 num_vals
= sscanf(token
, "%d-%d,%d-%d,%d-%d,%d-%d",
2881 lowers
+ 0, uppers
+ 0,
2882 lowers
+ 1, uppers
+ 1,
2883 lowers
+ 2, uppers
+ 2,
2884 lowers
+ 3, uppers
+ 3);
2886 if ((num_vals
& 1) || num_vals
== 0)
2888 print_fatal_error("Bad rangeOfInteger value \"%s\" on line %d.",
2893 ippAddRanges(col
, IPP_TAG_ZERO
, attr
, num_vals
/ 2, lowers
,
2898 case IPP_TAG_BEGIN_COLLECTION
:
2899 if (!strcmp(token
, "{"))
2901 ipp_t
*subcol
= get_collection(vars
, fp
, linenum
);
2902 /* Collection value */
2906 lastcol
= ippAddCollection(col
, IPP_TAG_ZERO
, attr
, subcol
);
2914 print_fatal_error("Bad collection value on line %d.", *linenum
);
2920 if (!strchr(token
, ','))
2921 ippAddString(col
, IPP_TAG_ZERO
, value
, attr
, NULL
, token
);
2925 * Multiple string values...
2928 int num_values
; /* Number of values */
2929 char *values
[100], /* Values */
2930 *ptr
; /* Pointer to next value */
2936 for (ptr
= strchr(token
, ','); ptr
; ptr
= strchr(ptr
, ','))
2939 values
[num_values
] = ptr
;
2943 ippAddStrings(col
, IPP_TAG_ZERO
, value
, attr
, num_values
,
2944 NULL
, (const char **)values
);
2954 * If we get here there was a parse error; free memory and return.
2966 * 'get_filename()' - Get a filename based on the current test file.
2969 static char * /* O - Filename */
2970 get_filename(const char *testfile
, /* I - Current test file */
2971 char *dst
, /* I - Destination filename */
2972 const char *src
, /* I - Source filename */
2973 size_t dstsize
) /* I - Size of destination buffer */
2975 char *dstptr
; /* Pointer into destination */
2976 _cups_globals_t
*cg
= _cupsGlobals();
2980 if (*src
== '<' && src
[strlen(src
) - 1] == '>')
2983 * Map <filename> to CUPS_DATADIR/ipptool/filename...
2986 snprintf(dst
, dstsize
, "%s/ipptool/%s", cg
->cups_datadir
, src
+ 1);
2987 dstptr
= dst
+ strlen(dst
) - 1;
2991 else if (*src
== '/' || !strchr(testfile
, '/'))
2994 * Use the path as-is...
2997 strlcpy(dst
, src
, dstsize
);
3002 * Make path relative to testfile...
3005 strlcpy(dst
, testfile
, dstsize
);
3006 if ((dstptr
= strrchr(dst
, '/')) != NULL
)
3009 dstptr
= dst
; /* Should never happen */
3011 strlcpy(dstptr
, src
, dstsize
- (dstptr
- dst
));
3019 * 'get_token()' - Get a token from a file.
3022 static char * /* O - Token from file or NULL on EOF */
3023 get_token(FILE *fp
, /* I - File to read from */
3024 char *buf
, /* I - Buffer to read into */
3025 int buflen
, /* I - Length of buffer */
3026 int *linenum
) /* IO - Current line number */
3028 int ch
, /* Character from file */
3029 quote
; /* Quoting character */
3030 char *bufptr
, /* Pointer into buffer */
3031 *bufend
; /* End of buffer */
3037 * Skip whitespace...
3040 while (isspace(ch
= getc(fp
)))
3052 else if (ch
== '\'' || ch
== '\"')
3055 * Quoted text or regular expression...
3060 bufend
= buf
+ buflen
- 1;
3062 while ((ch
= getc(fp
)) != EOF
)
3067 * Escape next character...
3070 if (bufptr
< bufend
)
3073 if ((ch
= getc(fp
)) != EOF
&& bufptr
< bufend
)
3076 else if (ch
== quote
)
3078 else if (bufptr
< bufend
)
3092 while ((ch
= getc(fp
)) != EOF
)
3101 * Whitespace delimited text...
3107 bufend
= buf
+ buflen
- 1;
3109 while ((ch
= getc(fp
)) != EOF
)
3110 if (isspace(ch
) || ch
== '#')
3112 else if (bufptr
< bufend
)
3117 else if (ch
== '\n')
3129 * 'get_variable()' - Get the value of a variable.
3132 static char * /* O - Value or NULL */
3133 get_variable(_cups_vars_t
*vars
, /* I - Variables */
3134 const char *name
) /* I - Variable name */
3136 _cups_var_t key
, /* Search key */
3137 *match
; /* Matching variable, if any */
3140 key
.name
= (char *)name
;
3141 match
= cupsArrayFind(vars
->vars
, &key
);
3143 return (match
? match
->value
: NULL
);
3148 * 'iso_date()' - Return an ISO 8601 date/time string for the given IPP dateTime
3152 static char * /* O - ISO 8601 date/time string */
3153 iso_date(ipp_uchar_t
*date
) /* I - IPP (RFC 1903) date/time value */
3155 unsigned year
= (date
[0] << 8) + date
[1];
3157 static char buffer
[255]; /* String buffer */
3160 if (date
[9] == 0 && date
[10] == 0)
3161 snprintf(buffer
, sizeof(buffer
), "%04u-%02u-%02uT%02u:%02u:%02uZ",
3162 year
, date
[2], date
[3], date
[4], date
[5], date
[6]);
3164 snprintf(buffer
, sizeof(buffer
), "%04u-%02u-%02uT%02u:%02u:%02u%c%02u%02u",
3165 year
, date
[2], date
[3], date
[4], date
[5], date
[6],
3166 date
[8], date
[9], date
[10]);
3173 * 'password_cb()' - Password callback for authenticated tests.
3176 static const char * /* O - Password */
3177 password_cb(const char *prompt
) /* I - Prompt (unused) */
3186 * 'print_attr()' - Print an attribute on the screen.
3190 print_attr(ipp_attribute_t
*attr
, /* I - Attribute to print */
3191 ipp_tag_t
*group
) /* IO - Current group */
3193 int i
; /* Looping var */
3194 ipp_attribute_t
*colattr
; /* Collection attribute */
3197 if (Output
== _CUPS_OUTPUT_PLIST
)
3199 if (!attr
->name
|| (group
&& *group
!= attr
->group_tag
))
3205 *group
= attr
->group_tag
;
3211 print_xml_string("key", attr
->name
);
3212 if (attr
->num_values
> 1)
3215 else if (Output
== _CUPS_OUTPUT_TEST
)
3219 puts(" -- separator --");
3223 printf(" %s (%s%s) = ", attr
->name
,
3224 attr
->num_values
> 1 ? "1setOf " : "",
3225 ippTagString(attr
->value_tag
));
3228 switch (attr
->value_tag
)
3230 case IPP_TAG_INTEGER
:
3232 for (i
= 0; i
< attr
->num_values
; i
++)
3233 if (Output
== _CUPS_OUTPUT_PLIST
)
3234 printf("<integer>%d</integer>\n", attr
->values
[i
].integer
);
3236 printf("%d ", attr
->values
[i
].integer
);
3239 case IPP_TAG_BOOLEAN
:
3240 for (i
= 0; i
< attr
->num_values
; i
++)
3241 if (Output
== _CUPS_OUTPUT_PLIST
)
3242 puts(attr
->values
[i
].boolean
? "<true />" : "<false />");
3243 else if (attr
->values
[i
].boolean
)
3244 fputs("true ", stdout
);
3246 fputs("false ", stdout
);
3249 case IPP_TAG_RANGE
:
3250 for (i
= 0; i
< attr
->num_values
; i
++)
3251 if (Output
== _CUPS_OUTPUT_PLIST
)
3252 printf("<dict><key>lower</key><integer>%d</integer>"
3253 "<key>upper</key><integer>%d</integer></dict>\n",
3254 attr
->values
[i
].range
.lower
, attr
->values
[i
].range
.upper
);
3256 printf("%d-%d ", attr
->values
[i
].range
.lower
,
3257 attr
->values
[i
].range
.upper
);
3260 case IPP_TAG_RESOLUTION
:
3261 for (i
= 0; i
< attr
->num_values
; i
++)
3262 if (Output
== _CUPS_OUTPUT_PLIST
)
3263 printf("<dict><key>xres</key><integer>%d</integer>"
3264 "<key>yres</key><integer>%d</integer>"
3265 "<key>units</key><string>%s</string></dict>\n",
3266 attr
->values
[i
].resolution
.xres
,
3267 attr
->values
[i
].resolution
.yres
,
3268 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3271 printf("%dx%d%s ", attr
->values
[i
].resolution
.xres
,
3272 attr
->values
[i
].resolution
.yres
,
3273 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3278 for (i
= 0; i
< attr
->num_values
; i
++)
3279 if (Output
== _CUPS_OUTPUT_PLIST
)
3280 printf("<date>%s</date>\n", iso_date(attr
->values
[i
].date
));
3282 printf("%s ", iso_date(attr
->values
[i
].date
));
3285 case IPP_TAG_STRING
:
3288 case IPP_TAG_KEYWORD
:
3289 case IPP_TAG_CHARSET
:
3291 case IPP_TAG_MIMETYPE
:
3292 case IPP_TAG_LANGUAGE
:
3293 for (i
= 0; i
< attr
->num_values
; i
++)
3294 if (Output
== _CUPS_OUTPUT_PLIST
)
3295 print_xml_string("string", attr
->values
[i
].string
.text
);
3297 printf("\"%s\" ", attr
->values
[i
].string
.text
);
3300 case IPP_TAG_TEXTLANG
:
3301 case IPP_TAG_NAMELANG
:
3302 for (i
= 0; i
< attr
->num_values
; i
++)
3303 if (Output
== _CUPS_OUTPUT_PLIST
)
3305 fputs("<dict><key>language</key><string>", stdout
);
3306 print_xml_string(NULL
, attr
->values
[i
].string
.charset
);
3307 fputs("</string><key>string</key><string>", stdout
);
3308 print_xml_string(NULL
, attr
->values
[i
].string
.text
);
3309 puts("</string></dict>");
3312 printf("\"%s\"(%s) ", attr
->values
[i
].string
.text
,
3313 attr
->values
[i
].string
.charset
);
3316 case IPP_TAG_BEGIN_COLLECTION
:
3317 for (i
= 0; i
< attr
->num_values
; i
++)
3319 if (Output
== _CUPS_OUTPUT_PLIST
)
3322 for (colattr
= attr
->values
[i
].collection
->attrs
;
3324 colattr
= colattr
->next
)
3325 print_attr(colattr
, NULL
);
3333 print_col(attr
->values
[i
].collection
);
3339 if (Output
== _CUPS_OUTPUT_PLIST
)
3340 printf("<string><<%s>></string>\n",
3341 ippTagString(attr
->value_tag
));
3343 fputs(ippTagString(attr
->value_tag
), stdout
);
3347 if (Output
== _CUPS_OUTPUT_PLIST
)
3349 if (attr
->num_values
> 1)
3358 * 'print_col()' - Print a collection attribute on the screen.
3362 print_col(ipp_t
*col
) /* I - Collection attribute to print */
3364 int i
; /* Looping var */
3365 ipp_attribute_t
*attr
; /* Current attribute in collection */
3368 fputs("{ ", stdout
);
3369 for (attr
= col
->attrs
; attr
; attr
= attr
->next
)
3371 printf("%s (%s%s) = ", attr
->name
, attr
->num_values
> 1 ? "1setOf " : "",
3372 ippTagString(attr
->value_tag
));
3374 switch (attr
->value_tag
)
3376 case IPP_TAG_INTEGER
:
3378 for (i
= 0; i
< attr
->num_values
; i
++)
3379 printf("%d ", attr
->values
[i
].integer
);
3382 case IPP_TAG_BOOLEAN
:
3383 for (i
= 0; i
< attr
->num_values
; i
++)
3384 if (attr
->values
[i
].boolean
)
3390 case IPP_TAG_NOVALUE
:
3394 case IPP_TAG_RANGE
:
3395 for (i
= 0; i
< attr
->num_values
; i
++)
3396 printf("%d-%d ", attr
->values
[i
].range
.lower
,
3397 attr
->values
[i
].range
.upper
);
3400 case IPP_TAG_RESOLUTION
:
3401 for (i
= 0; i
< attr
->num_values
; i
++)
3402 printf("%dx%d%s ", attr
->values
[i
].resolution
.xres
,
3403 attr
->values
[i
].resolution
.yres
,
3404 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3408 case IPP_TAG_STRING
:
3411 case IPP_TAG_KEYWORD
:
3412 case IPP_TAG_CHARSET
:
3414 case IPP_TAG_MIMETYPE
:
3415 case IPP_TAG_LANGUAGE
:
3416 for (i
= 0; i
< attr
->num_values
; i
++)
3417 printf("\"%s\" ", attr
->values
[i
].string
.text
);
3420 case IPP_TAG_TEXTLANG
:
3421 case IPP_TAG_NAMELANG
:
3422 for (i
= 0; i
< attr
->num_values
; i
++)
3423 printf("\"%s\",%s ", attr
->values
[i
].string
.text
,
3424 attr
->values
[i
].string
.charset
);
3427 case IPP_TAG_BEGIN_COLLECTION
:
3428 for (i
= 0; i
< attr
->num_values
; i
++)
3430 print_col(attr
->values
[i
].collection
);
3436 break; /* anti-compiler-warning-code */
3445 * 'print_csv()' - Print a line of CSV text.
3450 ipp_attribute_t
*attr
, /* I - First attribute for line */
3451 int num_displayed
, /* I - Number of attributes to display */
3452 char **displayed
, /* I - Attributes to display */
3453 size_t *widths
) /* I - Column widths */
3455 int i
; /* Looping var */
3456 size_t maxlength
; /* Max length of all columns */
3457 char *buffer
, /* String buffer */
3458 *bufptr
; /* Pointer into buffer */
3459 ipp_attribute_t
*current
; /* Current attribute */
3463 * Get the maximum string length we have to show and allocate...
3466 for (i
= 1, maxlength
= widths
[0]; i
< num_displayed
; i
++)
3467 if (widths
[i
] > maxlength
)
3468 maxlength
= widths
[i
];
3472 if ((buffer
= malloc(maxlength
)) == NULL
)
3476 * Loop through the attributes to display...
3481 for (i
= 0; i
< num_displayed
; i
++)
3488 for (current
= attr
; current
; current
= current
->next
)
3492 else if (!strcmp(current
->name
, displayed
[i
]))
3494 _ippAttrString(current
, buffer
, maxlength
);
3499 if (strchr(buffer
, ',') != NULL
|| strchr(buffer
, '\"') != NULL
||
3500 strchr(buffer
, '\\') != NULL
)
3503 for (bufptr
= buffer
; *bufptr
; bufptr
++)
3505 if (*bufptr
== '\\' || *bufptr
== '\"')
3512 fputs(buffer
, stdout
);
3518 for (i
= 0; i
< num_displayed
; i
++)
3523 fputs(displayed
[i
], stdout
);
3533 * 'print_fatal_error()' - Print a fatal error message.
3537 print_fatal_error(const char *s
, /* I - Printf-style format string */
3538 ...) /* I - Additional arguments as needed */
3540 char buffer
[10240]; /* Format buffer */
3541 va_list ap
; /* Pointer to arguments */
3545 * Format the error message...
3549 vsnprintf(buffer
, sizeof(buffer
), s
, ap
);
3556 if (Output
== _CUPS_OUTPUT_PLIST
)
3559 print_xml_trailer(0, buffer
);
3562 _cupsLangPrintf(stderr
, "ipptool: %s", buffer
);
3567 * 'print_line()' - Print a line of formatted or CSV text.
3572 ipp_attribute_t
*attr
, /* I - First attribute for line */
3573 int num_displayed
, /* I - Number of attributes to display */
3574 char **displayed
, /* I - Attributes to display */
3575 size_t *widths
) /* I - Column widths */
3577 int i
; /* Looping var */
3578 size_t maxlength
; /* Max length of all columns */
3579 char *buffer
; /* String buffer */
3580 ipp_attribute_t
*current
; /* Current attribute */
3584 * Get the maximum string length we have to show and allocate...
3587 for (i
= 1, maxlength
= widths
[0]; i
< num_displayed
; i
++)
3588 if (widths
[i
] > maxlength
)
3589 maxlength
= widths
[i
];
3593 if ((buffer
= malloc(maxlength
)) == NULL
)
3597 * Loop through the attributes to display...
3602 for (i
= 0; i
< num_displayed
; i
++)
3609 for (current
= attr
; current
; current
= current
->next
)
3613 else if (!strcmp(current
->name
, displayed
[i
]))
3615 _ippAttrString(current
, buffer
, maxlength
);
3620 printf("%*s", (int)-widths
[i
], buffer
);
3626 for (i
= 0; i
< num_displayed
; i
++)
3631 printf("%*s", (int)-widths
[i
], displayed
[i
]);
3635 for (i
= 0; i
< num_displayed
; i
++)
3640 memset(buffer
, '-', widths
[i
]);
3641 buffer
[widths
[i
]] = '\0';
3642 fputs(buffer
, stdout
);
3652 * 'print_test_error()' - Print a test error message.
3656 print_test_error(const char *s
, /* I - Printf-style format string */
3657 ...) /* I - Additional arguments as needed */
3659 char buffer
[10240]; /* Format buffer */
3660 va_list ap
; /* Pointer to arguments */
3664 * Format the error message...
3668 vsnprintf(buffer
, sizeof(buffer
), s
, ap
);
3675 if (Output
== _CUPS_OUTPUT_PLIST
)
3676 print_xml_string("string", buffer
);
3678 printf(" %s\n", buffer
);
3683 * 'print_xml_header()' - Print a standard XML plist header.
3687 print_xml_header(void)
3691 puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
3692 puts("<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
3693 "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
3694 puts("<plist version=\"1.0\">");
3696 puts("<key>Transfer</key>");
3697 printf("<string>%s</string>\n",
3698 Transfer
== _CUPS_TRANSFER_AUTO
? "auto" :
3699 Transfer
== _CUPS_TRANSFER_CHUNKED
? "chunked" : "length");
3700 puts("<key>Tests</key>");
3709 * 'print_xml_string()' - Print an XML string with escaping.
3713 print_xml_string(const char *element
, /* I - Element name or NULL */
3714 const char *s
) /* I - String to print */
3717 printf("<%s>", element
);
3722 fputs("&", stdout
);
3724 fputs("<", stdout
);
3726 fputs(">", stdout
);
3734 printf("</%s>\n", element
);
3739 * 'print_xml_trailer()' - Print the XML trailer with success/fail value.
3743 print_xml_trailer(int success
, /* I - 1 on success, 0 on failure */
3744 const char *message
) /* I - Error message or NULL */
3749 puts("<key>Successful</key>");
3750 puts(success
? "<true />" : "<false />");
3753 puts("<key>ErrorMessage</key>");
3754 print_xml_string("string", message
);
3765 * 'set_variable()' - Set a variable value.
3769 set_variable(_cups_vars_t
*vars
, /* I - Variables */
3770 const char *name
, /* I - Variable name */
3771 const char *value
) /* I - Value string */
3773 _cups_var_t key
, /* Search key */
3774 *var
; /* New variable */
3777 key
.name
= (char *)name
;
3778 if ((var
= cupsArrayFind(vars
->vars
, &key
)) != NULL
)
3781 var
->value
= strdup(value
);
3783 else if ((var
= malloc(sizeof(_cups_var_t
))) == NULL
)
3785 print_fatal_error("Unable to allocate memory for variable \"%s\".", name
);
3790 var
->name
= strdup(name
);
3791 var
->value
= strdup(value
);
3793 cupsArrayAdd(vars
->vars
, var
);
3799 * 'timeout_cb()' - Handle HTTP timeouts.
3802 static int /* O - 1 to continue, 0 to cancel */
3803 timeout_cb(http_t
*http
, /* I - Connection to server (unused) */
3804 void *user_data
) /* I - User data (unused) */
3814 * 'usage()' - Show program usage.
3820 _cupsLangPuts(stderr
, _("Usage: ipptool [options] URI filename [ ... "
3822 _cupsLangPuts(stderr
, _("Options:"));
3823 _cupsLangPuts(stderr
, _(" -4 Connect using IPv4."));
3824 _cupsLangPuts(stderr
, _(" -6 Connect using IPv6."));
3825 _cupsLangPuts(stderr
, _(" -C Send requests using chunking "
3827 _cupsLangPuts(stderr
, _(" -E Test with TLS encryption."));
3828 _cupsLangPuts(stderr
, _(" -I Ignore errors."));
3829 _cupsLangPuts(stderr
, _(" -L Send requests using "
3830 "content-length."));
3831 _cupsLangPuts(stderr
, _(" -S Test with SSL encryption."));
3832 _cupsLangPuts(stderr
, _(" -T Set the receive/send timeout in "
3834 _cupsLangPuts(stderr
, _(" -V version Set default IPP version."));
3835 _cupsLangPuts(stderr
, _(" -X Produce XML plist instead of plain "
3837 _cupsLangPuts(stderr
, _(" -d name=value Define variable."));
3838 _cupsLangPuts(stderr
, _(" -f filename Set default request filename."));
3839 _cupsLangPuts(stderr
, _(" -i seconds Repeat the last file with the "
3840 "given time interval."));
3841 _cupsLangPuts(stderr
, _(" -n count Repeat the last file the given "
3842 "number of times."));
3843 _cupsLangPuts(stderr
, _(" -q Be quiet - no output except "
3845 _cupsLangPuts(stderr
, _(" -t Produce a test report."));
3846 _cupsLangPuts(stderr
, _(" -v Show all attributes sent and "
3854 * 'validate_attr()' - Determine whether an attribute is valid.
3857 static int /* O - 1 if valid, 0 otherwise */
3858 validate_attr(ipp_attribute_t
*attr
, /* I - Attribute to validate */
3859 int print
) /* I - 1 = report issues to stdout */
3861 int i
; /* Looping var */
3862 char scheme
[64], /* Scheme from URI */
3863 userpass
[256], /* Username/password from URI */
3864 hostname
[256], /* Hostname from URI */
3865 resource
[1024]; /* Resource from URI */
3866 int port
, /* Port number from URI */
3867 uri_status
, /* URI separation status */
3868 valid
= 1; /* Is the attribute valid? */
3869 const char *ptr
; /* Pointer into string */
3870 ipp_attribute_t
*colattr
; /* Collection attribute */
3871 regex_t re
; /* Regular expression */
3872 ipp_uchar_t
*date
; /* Current date value */
3883 * Validate the attribute name.
3886 for (ptr
= attr
->name
; *ptr
; ptr
++)
3887 if (!isalnum(*ptr
& 255) && *ptr
!= '-' && *ptr
!= '.' && *ptr
!= '_')
3890 if (*ptr
|| ptr
== attr
->name
)
3895 print_test_error("\"%s\": Bad attribute name - invalid character (RFC "
3896 "2911 section 4.1.3).", attr
->name
);
3899 if ((ptr
- attr
->name
) > 255)
3904 print_test_error("\"%s\": Bad attribute name - bad length (RFC 2911 "
3905 "section 4.1.3).", attr
->name
);
3908 switch (attr
->value_tag
)
3910 case IPP_TAG_INTEGER
:
3913 case IPP_TAG_BOOLEAN
:
3914 for (i
= 0; i
< attr
->num_values
; i
++)
3916 if (attr
->values
[i
].boolean
!= 0 &&
3917 attr
->values
[i
].boolean
!= 1)
3922 print_test_error("\"%s\": Bad boolen value %d (RFC 2911 section "
3923 "4.1.10).", attr
->name
, attr
->values
[i
].boolean
);
3931 for (i
= 0; i
< attr
->num_values
; i
++)
3933 if (attr
->values
[i
].integer
< 1)
3938 print_test_error("\"%s\": Bad enum value %d - out of range "
3939 "(RFC 2911 section 4.1.4).", attr
->name
,
3940 attr
->values
[i
].integer
);
3947 case IPP_TAG_STRING
:
3948 for (i
= 0; i
< attr
->num_values
; i
++)
3950 if (attr
->values
[i
].unknown
.length
> 1023)
3955 print_test_error("\"%s\": Bad octetString value - bad length %d "
3956 "(RFC 2911 section 4.1.10).", attr
->name
,
3957 attr
->values
[i
].unknown
.length
);
3965 for (i
= 0; i
< attr
->num_values
; i
++)
3967 date
= attr
->values
[i
].date
;
3969 if (date
[2] < 1 || date
[2] > 12)
3974 print_test_error("\"%s\": Bad dateTime month %u (RFC 2911 "
3975 "section 4.1.13).", attr
->name
, date
[2]);
3980 if (date
[3] < 1 || date
[3] > 31)
3985 print_test_error("\"%s\": Bad dateTime day %u (RFC 2911 "
3986 "section 4.1.13).", attr
->name
, date
[3]);
3996 print_test_error("\"%s\": Bad dateTime hours %u (RFC 2911 "
3997 "section 4.1.13).", attr
->name
, date
[4]);
4007 print_test_error("\"%s\": Bad dateTime minutes %u (RFC 2911 "
4008 "section 4.1.13).", attr
->name
, date
[5]);
4018 print_test_error("\"%s\": Bad dateTime seconds %u (RFC 2911 "
4019 "section 4.1.13).", attr
->name
, date
[6]);
4029 print_test_error("\"%s\": Bad dateTime deciseconds %u (RFC 2911 "
4030 "section 4.1.13).", attr
->name
, date
[7]);
4035 if (date
[8] != '-' && date
[8] != '+')
4040 print_test_error("\"%s\": Bad dateTime UTC sign '%c' (RFC 2911 "
4041 "section 4.1.13).", attr
->name
, date
[8]);
4051 print_test_error("\"%s\": Bad dateTime UTC hours %u (RFC 2911 "
4052 "section 4.1.13).", attr
->name
, date
[9]);
4062 print_test_error("\"%s\": Bad dateTime UTC minutes %u (RFC 2911 "
4063 "section 4.1.13).", attr
->name
, date
[10]);
4070 case IPP_TAG_RESOLUTION
:
4071 for (i
= 0; i
< attr
->num_values
; i
++)
4073 if (attr
->values
[i
].resolution
.xres
<= 0)
4078 print_test_error("\"%s\": Bad resolution value %dx%d%s - cross "
4079 "feed resolution must be positive (RFC 2911 "
4080 "section 4.1.13).", attr
->name
,
4081 attr
->values
[i
].resolution
.xres
,
4082 attr
->values
[i
].resolution
.yres
,
4083 attr
->values
[i
].resolution
.units
==
4084 IPP_RES_PER_INCH
? "dpi" :
4085 attr
->values
[i
].resolution
.units
==
4086 IPP_RES_PER_CM
? "dpc" : "unknown");
4091 if (attr
->values
[i
].resolution
.yres
<= 0)
4096 print_test_error("\"%s\": Bad resolution value %dx%d%s - feed "
4097 "resolution must be positive (RFC 2911 section "
4098 "4.1.13).", attr
->name
,
4099 attr
->values
[i
].resolution
.xres
,
4100 attr
->values
[i
].resolution
.yres
,
4101 attr
->values
[i
].resolution
.units
==
4102 IPP_RES_PER_INCH
? "dpi" :
4103 attr
->values
[i
].resolution
.units
==
4104 IPP_RES_PER_CM
? "dpc" : "unknown");
4109 if (attr
->values
[i
].resolution
.units
!= IPP_RES_PER_INCH
&&
4110 attr
->values
[i
].resolution
.units
!= IPP_RES_PER_CM
)
4115 print_test_error("\"%s\": Bad resolution value %dx%d%s - bad "
4116 "units value (RFC 2911 section 4.1.13).",
4117 attr
->name
, attr
->values
[i
].resolution
.xres
,
4118 attr
->values
[i
].resolution
.yres
,
4119 attr
->values
[i
].resolution
.units
==
4120 IPP_RES_PER_INCH
? "dpi" :
4121 attr
->values
[i
].resolution
.units
==
4122 IPP_RES_PER_CM
? "dpc" : "unknown");
4129 case IPP_TAG_RANGE
:
4130 for (i
= 0; i
< attr
->num_values
; i
++)
4132 if (attr
->values
[i
].range
.lower
> attr
->values
[i
].range
.upper
)
4137 print_test_error("\"%s\": Bad rangeOfInteger value %d-%d - lower "
4138 "greater than upper (RFC 2911 section 4.1.13).",
4139 attr
->name
, attr
->values
[i
].range
.lower
,
4140 attr
->values
[i
].range
.upper
);
4147 case IPP_TAG_BEGIN_COLLECTION
:
4148 for (i
= 0; i
< attr
->num_values
; i
++)
4150 for (colattr
= attr
->values
[i
].collection
->attrs
;
4152 colattr
= colattr
->next
)
4154 if (!validate_attr(colattr
, 0))
4161 if (colattr
&& print
)
4163 print_test_error("\"%s\": Bad collection value.", attr
->name
);
4167 validate_attr(colattr
, print
);
4168 colattr
= colattr
->next
;
4175 case IPP_TAG_TEXTLANG
:
4176 for (i
= 0; i
< attr
->num_values
; i
++)
4178 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
4180 if ((*ptr
& 0xe0) == 0xc0)
4183 if ((*ptr
& 0xc0) != 0x80)
4186 else if ((*ptr
& 0xf0) == 0xe0)
4189 if ((*ptr
& 0xc0) != 0x80)
4192 if ((*ptr
& 0xc0) != 0x80)
4195 else if ((*ptr
& 0xf8) == 0xf0)
4198 if ((*ptr
& 0xc0) != 0x80)
4201 if ((*ptr
& 0xc0) != 0x80)
4204 if ((*ptr
& 0xc0) != 0x80)
4207 else if (*ptr
& 0x80)
4216 print_test_error("\"%s\": Bad text value \"%s\" - bad UTF-8 "
4217 "sequence (RFC 2911 section 4.1.1).", attr
->name
,
4218 attr
->values
[i
].string
.text
);
4223 if ((ptr
- attr
->values
[i
].string
.text
) > 1023)
4228 print_test_error("\"%s\": Bad text value \"%s\" - bad length %d "
4229 "(RFC 2911 section 4.1.1).", attr
->name
,
4230 attr
->values
[i
].string
.text
,
4231 (int)strlen(attr
->values
[i
].string
.text
));
4239 case IPP_TAG_NAMELANG
:
4240 for (i
= 0; i
< attr
->num_values
; i
++)
4242 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
4244 if ((*ptr
& 0xe0) == 0xc0)
4247 if ((*ptr
& 0xc0) != 0x80)
4250 else if ((*ptr
& 0xf0) == 0xe0)
4253 if ((*ptr
& 0xc0) != 0x80)
4256 if ((*ptr
& 0xc0) != 0x80)
4259 else if ((*ptr
& 0xf8) == 0xf0)
4262 if ((*ptr
& 0xc0) != 0x80)
4265 if ((*ptr
& 0xc0) != 0x80)
4268 if ((*ptr
& 0xc0) != 0x80)
4271 else if (*ptr
& 0x80)
4280 print_test_error("\"%s\": Bad name value \"%s\" - bad UTF-8 "
4281 "sequence (RFC 2911 section 4.1.2).", attr
->name
,
4282 attr
->values
[i
].string
.text
);
4287 if ((ptr
- attr
->values
[i
].string
.text
) > 1023)
4292 print_test_error("\"%s\": Bad name value \"%s\" - bad length %d "
4293 "(RFC 2911 section 4.1.2).", attr
->name
,
4294 attr
->values
[i
].string
.text
,
4295 (int)strlen(attr
->values
[i
].string
.text
));
4302 case IPP_TAG_KEYWORD
:
4303 for (i
= 0; i
< attr
->num_values
; i
++)
4305 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
4306 if (!isalnum(*ptr
& 255) && *ptr
!= '-' && *ptr
!= '.' &&
4310 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
4315 print_test_error("\"%s\": Bad keyword value \"%s\" - invalid "
4316 "character (RFC 2911 section 4.1.3).",
4317 attr
->name
, attr
->values
[i
].string
.text
);
4322 if ((ptr
- attr
->values
[i
].string
.text
) > 255)
4327 print_test_error("\"%s\": Bad keyword value \"%s\" - bad "
4328 "length %d (RFC 2911 section 4.1.3).",
4329 attr
->name
, attr
->values
[i
].string
.text
,
4330 (int)strlen(attr
->values
[i
].string
.text
));
4338 for (i
= 0; i
< attr
->num_values
; i
++)
4340 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
,
4341 attr
->values
[i
].string
.text
,
4342 scheme
, sizeof(scheme
),
4343 userpass
, sizeof(userpass
),
4344 hostname
, sizeof(hostname
),
4345 &port
, resource
, sizeof(resource
));
4347 if (uri_status
< HTTP_URI_OK
)
4352 print_test_error("\"%s\": Bad URI value \"%s\" - %s "
4353 "(RFC 2911 section 4.1.5).", attr
->name
,
4354 attr
->values
[i
].string
.text
,
4355 URIStatusStrings
[uri_status
-
4356 HTTP_URI_OVERFLOW
]);
4361 if (strlen(attr
->values
[i
].string
.text
) > 1023)
4366 print_test_error("\"%s\": Bad URI value \"%s\" - bad length %d "
4367 "(RFC 2911 section 4.1.5).", attr
->name
,
4368 attr
->values
[i
].string
.text
,
4369 (int)strlen(attr
->values
[i
].string
.text
));
4376 case IPP_TAG_URISCHEME
:
4377 for (i
= 0; i
< attr
->num_values
; i
++)
4379 ptr
= attr
->values
[i
].string
.text
;
4380 if (islower(*ptr
& 255))
4382 for (ptr
++; *ptr
; ptr
++)
4383 if (!islower(*ptr
& 255) && !isdigit(*ptr
& 255) &&
4384 *ptr
!= '+' && *ptr
!= '-' && *ptr
!= '.')
4388 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
4393 print_test_error("\"%s\": Bad uriScheme value \"%s\" - bad "
4394 "characters (RFC 2911 section 4.1.6).",
4395 attr
->name
, attr
->values
[i
].string
.text
);
4400 if ((ptr
- attr
->values
[i
].string
.text
) > 63)
4405 print_test_error("\"%s\": Bad uriScheme value \"%s\" - bad "
4406 "length %d (RFC 2911 section 4.1.6).",
4407 attr
->name
, attr
->values
[i
].string
.text
,
4408 (int)strlen(attr
->values
[i
].string
.text
));
4415 case IPP_TAG_CHARSET
:
4416 for (i
= 0; i
< attr
->num_values
; i
++)
4418 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
4419 if (!isprint(*ptr
& 255) || isupper(*ptr
& 255) ||
4420 isspace(*ptr
& 255))
4423 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
4428 print_test_error("\"%s\": Bad charset value \"%s\" - bad "
4429 "characters (RFC 2911 section 4.1.7).",
4430 attr
->name
, attr
->values
[i
].string
.text
);
4435 if ((ptr
- attr
->values
[i
].string
.text
) > 40)
4440 print_test_error("\"%s\": Bad charset value \"%s\" - bad "
4441 "length %d (RFC 2911 section 4.1.7).",
4442 attr
->name
, attr
->values
[i
].string
.text
,
4443 (int)strlen(attr
->values
[i
].string
.text
));
4450 case IPP_TAG_LANGUAGE
:
4452 * The following regular expression is derived from the ABNF for
4453 * language tags in RFC 4646. All I can say is that this is the
4454 * easiest way to check the values...
4457 if ((i
= regcomp(&re
,
4459 "(([a-z]{2,3}(-[a-z][a-z][a-z]){0,3})|[a-z]{4,8})"
4461 "(-[a-z][a-z][a-z][a-z]){0,1}" /* script */
4462 "(-([a-z][a-z]|[0-9][0-9][0-9])){0,1}" /* region */
4463 "(-([a-z]{5,8}|[0-9][0-9][0-9]))*" /* variant */
4464 "(-[a-wy-z](-[a-z0-9]{2,8})+)*" /* extension */
4465 "(-x(-[a-z0-9]{1,8})+)*" /* privateuse */
4467 "x(-[a-z0-9]{1,8})+" /* privateuse */
4469 "[a-z]{1,3}(-[a-z][0-9]{2,8}){1,2}" /* grandfathered */
4471 REG_NOSUB
| REG_EXTENDED
)) != 0)
4473 char temp
[256]; /* Temporary error string */
4475 regerror(i
, &re
, temp
, sizeof(temp
));
4476 print_fatal_error("Unable to compile naturalLanguage regular "
4477 "expression: %s.", temp
);
4480 for (i
= 0; i
< attr
->num_values
; i
++)
4482 if (regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
4487 print_test_error("\"%s\": Bad naturalLanguage value \"%s\" - bad "
4488 "characters (RFC 2911 section 4.1.8).",
4489 attr
->name
, attr
->values
[i
].string
.text
);
4494 if (strlen(attr
->values
[i
].string
.text
) > 63)
4499 print_test_error("\"%s\": Bad naturalLanguage value \"%s\" - bad "
4500 "length %d (RFC 2911 section 4.1.8).",
4501 attr
->name
, attr
->values
[i
].string
.text
,
4502 (int)strlen(attr
->values
[i
].string
.text
));
4511 case IPP_TAG_MIMETYPE
:
4513 * The following regular expression is derived from the ABNF for
4514 * language tags in RFC 2045 and 4288. All I can say is that this is
4515 * the easiest way to check the values...
4518 if ((i
= regcomp(&re
,
4520 "[-a-zA-Z0-9!#$&.+^_]{1,127}" /* type-name */
4522 "[-a-zA-Z0-9!#$&.+^_]{1,127}" /* subtype-name */
4523 "(;[-a-zA-Z0-9!#$&.+^_]{1,127}=" /* parameter= */
4524 "([-a-zA-Z0-9!#$&.+^_]{1,127}|\"[^\"]*\"))*"
4527 REG_NOSUB
| REG_EXTENDED
)) != 0)
4529 char temp
[256]; /* Temporary error string */
4531 regerror(i
, &re
, temp
, sizeof(temp
));
4532 print_fatal_error("Unable to compile mimeMediaType regular "
4533 "expression: %s.", temp
);
4536 for (i
= 0; i
< attr
->num_values
; i
++)
4538 if (regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
4543 print_test_error("\"%s\": Bad mimeMediaType value \"%s\" - bad "
4544 "characters (RFC 2911 section 4.1.9).",
4545 attr
->name
, attr
->values
[i
].string
.text
);
4550 if (strlen(attr
->values
[i
].string
.text
) > 255)
4555 print_test_error("\"%s\": Bad mimeMediaType value \"%s\" - bad "
4556 "length %d (RFC 2911 section 4.1.9).",
4557 attr
->name
, attr
->values
[i
].string
.text
,
4558 (int)strlen(attr
->values
[i
].string
.text
));
4574 * 'with_value()' - Test a WITH-VALUE predicate.
4577 static int /* O - 1 on match, 0 on non-match */
4578 with_value(char *value
, /* I - Value string */
4579 int regex
, /* I - Value is a regular expression */
4580 ipp_attribute_t
*attr
, /* I - Attribute to compare */
4581 int report
) /* I - 1 = report failures */
4583 int i
; /* Looping var */
4584 char *valptr
; /* Pointer into value */
4588 * NULL matches everything.
4591 if (!value
|| !*value
)
4595 * Compare the value string to the attribute value.
4598 switch (attr
->value_tag
)
4600 case IPP_TAG_INTEGER
:
4602 for (i
= 0; i
< attr
->num_values
; i
++)
4604 char op
, /* Comparison operator */
4605 *nextptr
; /* Next pointer */
4606 int intvalue
; /* Integer value */
4610 if (!strncmp(valptr
, "no-value,", 9))
4613 while (isspace(*valptr
& 255) || isdigit(*valptr
& 255) ||
4614 *valptr
== '-' || *valptr
== ',' || *valptr
== '<' ||
4615 *valptr
== '=' || *valptr
== '>')
4618 while (*valptr
&& !isdigit(*valptr
& 255) && *valptr
!= '-')
4620 if (*valptr
== '<' || *valptr
== '>' || *valptr
== '=')
4628 intvalue
= strtol(valptr
, &nextptr
, 0);
4629 if (nextptr
== valptr
)
4636 if (attr
->values
[i
].integer
== intvalue
)
4640 if (attr
->values
[i
].integer
< intvalue
)
4644 if (attr
->values
[i
].integer
> intvalue
)
4653 for (i
= 0; i
< attr
->num_values
; i
++)
4654 print_test_error("GOT: %s=%d", attr
->name
, attr
->values
[i
].integer
);
4658 case IPP_TAG_RANGE
:
4659 for (i
= 0; i
< attr
->num_values
; i
++)
4661 char op
, /* Comparison operator */
4662 *nextptr
; /* Next pointer */
4663 int intvalue
; /* Integer value */
4667 if (!strncmp(valptr
, "no-value,", 9))
4670 while (isspace(*valptr
& 255) || isdigit(*valptr
& 255) ||
4671 *valptr
== '-' || *valptr
== ',' || *valptr
== '<' ||
4672 *valptr
== '=' || *valptr
== '>')
4675 while (*valptr
&& !isdigit(*valptr
& 255) && *valptr
!= '-')
4677 if (*valptr
== '<' || *valptr
== '>' || *valptr
== '=')
4685 intvalue
= strtol(valptr
, &nextptr
, 0);
4686 if (nextptr
== valptr
)
4693 if (attr
->values
[i
].range
.lower
== intvalue
||
4694 attr
->values
[i
].range
.upper
== intvalue
)
4698 if (attr
->values
[i
].range
.upper
< intvalue
)
4702 if (attr
->values
[i
].range
.upper
> intvalue
)
4711 for (i
= 0; i
< attr
->num_values
; i
++)
4712 print_test_error("GOT: %s=%d-%d", attr
->name
,
4713 attr
->values
[i
].range
.lower
,
4714 attr
->values
[i
].range
.upper
);
4718 case IPP_TAG_BOOLEAN
:
4719 for (i
= 0; i
< attr
->num_values
; i
++)
4721 if (!strcmp(value
, "true") == attr
->values
[i
].boolean
)
4727 for (i
= 0; i
< attr
->num_values
; i
++)
4728 print_test_error("GOT: %s=%s", attr
->name
,
4729 attr
->values
[i
].boolean
? "true" : "false");
4733 case IPP_TAG_NOVALUE
:
4734 return (!strcmp(value
, "no-value") || !strncmp(value
, "no-value,", 9));
4736 case IPP_TAG_CHARSET
:
4737 case IPP_TAG_KEYWORD
:
4738 case IPP_TAG_LANGUAGE
:
4739 case IPP_TAG_MIMETYPE
:
4741 case IPP_TAG_NAMELANG
:
4743 case IPP_TAG_TEXTLANG
:
4745 case IPP_TAG_URISCHEME
:
4749 * Value is an extended, case-sensitive POSIX regular expression...
4752 regex_t re
; /* Regular expression */
4754 if ((i
= regcomp(&re
, value
, REG_EXTENDED
| REG_NOSUB
)) != 0)
4756 char temp
[256]; /* Temporary string */
4758 regerror(i
, &re
, temp
, sizeof(temp
));
4760 print_fatal_error("Unable to compile WITH-VALUE regular expression "
4761 "\"%s\" - %s", value
, temp
);
4766 * See if ALL of the values match the given regular expression.
4769 for (i
= 0; i
< attr
->num_values
; i
++)
4771 if (regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
4774 print_test_error("GOT: %s=\"%s\"", attr
->name
,
4775 attr
->values
[i
].string
.text
);
4783 return (i
== attr
->num_values
);
4788 * Value is a literal string, see if at least one value matches the
4792 for (i
= 0; i
< attr
->num_values
; i
++)
4794 if (!strcmp(value
, attr
->values
[i
].string
.text
))
4800 for (i
= 0; i
< attr
->num_values
; i
++)
4801 print_test_error("GOT: %s=\"%s\"", attr
->name
,
4802 attr
->values
[i
].string
.text
);