4 * CUPS API test program for CUPS.
6 * Copyright 2007-2013 by Apple Inc.
7 * Copyright 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() - Main entry.
20 * dests_equal() - Determine whether two destinations are equal.
24 * Include necessary headers...
27 #undef _CUPS_NO_DEPRECATED
28 #include "string-private.h"
38 static int dests_equal(cups_dest_t
*a
, cups_dest_t
*b
);
39 static int enum_cb(void *user_data
, unsigned flags
, cups_dest_t
*dest
);
40 static void show_diffs(cups_dest_t
*a
, cups_dest_t
*b
);
44 * 'main()' - Main entry.
47 int /* O - Exit status */
48 main(int argc
, /* I - Number of command-line arguments */
49 char *argv
[]) /* I - Command-line arguments */
51 int status
= 0, /* Exit status */
53 num_dests
; /* Number of destinations */
54 cups_dest_t
*dests
, /* Destinations */
55 *dest
, /* Current destination */
56 *named_dest
; /* Current named destination */
57 const char *ppdfile
; /* PPD file */
58 ppd_file_t
*ppd
; /* PPD file data */
59 int num_jobs
; /* Number of jobs for queue */
60 cups_job_t
*jobs
; /* Jobs for queue */
65 if (!strcmp(argv
[1], "enum"))
67 cups_ptype_t mask
= CUPS_PRINTER_LOCAL
,
68 /* Printer type mask */
69 type
= CUPS_PRINTER_LOCAL
;
71 int msec
= 0; /* Timeout in milliseconds */
74 for (i
= 2; i
< argc
; i
++)
75 if (isdigit(argv
[i
][0] & 255) || argv
[i
][0] == '.')
76 msec
= (int)(atof(argv
[i
]) * 1000);
77 else if (!_cups_strcasecmp(argv
[i
], "bw"))
79 mask
|= CUPS_PRINTER_BW
;
80 type
|= CUPS_PRINTER_BW
;
82 else if (!_cups_strcasecmp(argv
[i
], "color"))
84 mask
|= CUPS_PRINTER_COLOR
;
85 type
|= CUPS_PRINTER_COLOR
;
87 else if (!_cups_strcasecmp(argv
[i
], "mono"))
89 mask
|= CUPS_PRINTER_COLOR
;
91 else if (!_cups_strcasecmp(argv
[i
], "duplex"))
93 mask
|= CUPS_PRINTER_DUPLEX
;
94 type
|= CUPS_PRINTER_DUPLEX
;
96 else if (!_cups_strcasecmp(argv
[i
], "simplex"))
98 mask
|= CUPS_PRINTER_DUPLEX
;
100 else if (!_cups_strcasecmp(argv
[i
], "staple"))
102 mask
|= CUPS_PRINTER_STAPLE
;
103 type
|= CUPS_PRINTER_STAPLE
;
105 else if (!_cups_strcasecmp(argv
[i
], "copies"))
107 mask
|= CUPS_PRINTER_COPIES
;
108 type
|= CUPS_PRINTER_COPIES
;
110 else if (!_cups_strcasecmp(argv
[i
], "collate"))
112 mask
|= CUPS_PRINTER_COLLATE
;
113 type
|= CUPS_PRINTER_COLLATE
;
115 else if (!_cups_strcasecmp(argv
[i
], "punch"))
117 mask
|= CUPS_PRINTER_PUNCH
;
118 type
|= CUPS_PRINTER_PUNCH
;
120 else if (!_cups_strcasecmp(argv
[i
], "cover"))
122 mask
|= CUPS_PRINTER_COVER
;
123 type
|= CUPS_PRINTER_COVER
;
125 else if (!_cups_strcasecmp(argv
[i
], "bind"))
127 mask
|= CUPS_PRINTER_BIND
;
128 type
|= CUPS_PRINTER_BIND
;
130 else if (!_cups_strcasecmp(argv
[i
], "sort"))
132 mask
|= CUPS_PRINTER_SORT
;
133 type
|= CUPS_PRINTER_SORT
;
135 else if (!_cups_strcasecmp(argv
[i
], "mfp"))
137 mask
|= CUPS_PRINTER_MFP
;
138 type
|= CUPS_PRINTER_MFP
;
140 else if (!_cups_strcasecmp(argv
[i
], "printer"))
142 mask
|= CUPS_PRINTER_MFP
;
144 else if (!_cups_strcasecmp(argv
[i
], "large"))
146 mask
|= CUPS_PRINTER_LARGE
;
147 type
|= CUPS_PRINTER_LARGE
;
149 else if (!_cups_strcasecmp(argv
[i
], "medium"))
151 mask
|= CUPS_PRINTER_MEDIUM
;
152 type
|= CUPS_PRINTER_MEDIUM
;
154 else if (!_cups_strcasecmp(argv
[i
], "small"))
156 mask
|= CUPS_PRINTER_SMALL
;
157 type
|= CUPS_PRINTER_SMALL
;
160 fprintf(stderr
, "Unknown argument \"%s\" ignored...\n", argv
[i
]);
162 cupsEnumDests(CUPS_DEST_FLAGS_NONE
, msec
, NULL
, type
, mask
, enum_cb
, NULL
);
164 else if (!strcmp(argv
[1], "password"))
166 const char *pass
= cupsGetPassword("Password:");
167 /* Password string */
170 printf("Password entered: %s\n", pass
);
172 puts("No password entered.");
174 else if (!strcmp(argv
[1], "ppd") && argc
== 3)
177 * ./testcups ppd printer
180 http_status_t http_status
; /* Status */
181 char buffer
[1024]; /* PPD filename */
182 time_t modtime
= 0; /* Last modified */
184 if ((http_status
= cupsGetPPD3(CUPS_HTTP_DEFAULT
, argv
[2], &modtime
,
185 buffer
, sizeof(buffer
))) != HTTP_STATUS_OK
)
186 printf("Unable to get PPD: %d (%s)\n", (int)http_status
,
187 cupsLastErrorString());
191 else if (!strcmp(argv
[1], "print") && argc
== 5)
194 * ./testcups print printer file interval
197 int interval
, /* Interval between writes */
199 cups_file_t
*fp
; /* Print file */
200 char buffer
[16384]; /* Read/write buffer */
201 ssize_t bytes
; /* Bytes read/written */
203 if ((fp
= cupsFileOpen(argv
[3], "r")) == NULL
)
205 printf("Unable to open \"%s\": %s\n", argv
[2], strerror(errno
));
209 if ((job_id
= cupsCreateJob(CUPS_HTTP_DEFAULT
, argv
[2], "testcups", 0,
212 printf("Unable to create print job on %s: %s\n", argv
[1],
213 cupsLastErrorString());
217 interval
= atoi(argv
[4]);
219 if (cupsStartDocument(CUPS_HTTP_DEFAULT
, argv
[1], job_id
, argv
[2],
220 CUPS_FORMAT_AUTO
, 1) != HTTP_STATUS_CONTINUE
)
222 puts("Unable to start document!");
226 while ((bytes
= cupsFileRead(fp
, buffer
, sizeof(buffer
))) > 0)
228 printf("Writing %d bytes...\n", (int)bytes
);
230 if (cupsWriteRequestData(CUPS_HTTP_DEFAULT
, buffer
,
231 bytes
) != HTTP_STATUS_CONTINUE
)
233 puts("Unable to write bytes!");
243 if (cupsFinishDocument(CUPS_HTTP_DEFAULT
,
244 argv
[1]) > IPP_STATUS_OK_IGNORED_OR_SUBSTITUTED
)
246 puts("Unable to finish document!");
254 puts("Run basic unit tests:");
258 puts("Enumerate printers (for N seconds, -1 for indefinitely):");
260 puts(" ./testcups enum [seconds]");
262 puts("Ask for a password:");
264 puts(" ./testcups password");
266 puts("Get the PPD file:");
268 puts(" ./testcups ppd printer");
270 puts("Print a file (interval controls delay between buffers in seconds):");
272 puts(" ./testcups print printer file interval");
283 fputs("cupsGetDests: ", stdout
);
286 num_dests
= cupsGetDests(&dests
);
295 printf("PASS (%d dests)\n", num_dests
);
297 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
299 printf(" %s", dest
->name
);
302 printf(" /%s", dest
->instance
);
304 if (dest
->is_default
)
305 puts(" ***DEFAULT***");
315 fputs("cupsGetDest(NULL): ", stdout
);
318 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) == NULL
)
320 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
321 if (dest
->is_default
)
330 puts("PASS (no default)");
335 printf("PASS (%s)\n", dest
->name
);
338 * cupsGetNamedDest(NULL, NULL, NULL)
341 fputs("cupsGetNamedDest(NULL, NULL, NULL): ", stdout
);
344 if ((named_dest
= cupsGetNamedDest(NULL
, NULL
, NULL
)) == NULL
||
345 !dests_equal(dest
, named_dest
))
348 puts("PASS (no default)");
351 puts("FAIL (different values)");
352 show_diffs(dest
, named_dest
);
357 puts("FAIL (no default)");
362 printf("PASS (%s)\n", named_dest
->name
);
365 cupsFreeDests(1, named_dest
);
368 * cupsGetDest(printer)
371 printf("cupsGetDest(\"%s\"): ", dests
[num_dests
/ 2].name
);
374 if ((dest
= cupsGetDest(dests
[num_dests
/ 2].name
, NULL
, num_dests
,
384 * cupsGetNamedDest(NULL, printer, instance)
387 printf("cupsGetNamedDest(NULL, \"%s\", \"%s\"): ", dest
->name
,
388 dest
->instance
? dest
->instance
: "(null)");
391 if ((named_dest
= cupsGetNamedDest(NULL
, dest
->name
,
392 dest
->instance
)) == NULL
||
393 !dests_equal(dest
, named_dest
))
397 puts("FAIL (different values)");
398 show_diffs(dest
, named_dest
);
401 puts("FAIL (no destination)");
410 cupsFreeDests(1, named_dest
);
416 fputs("cupsPrintFile: ", stdout
);
419 if (cupsPrintFile(dest
->name
, "../data/testprint", "Test Page",
420 dest
->num_options
, dest
->options
) <= 0)
422 printf("FAIL (%s)\n", cupsLastErrorString());
429 * cupsGetPPD(printer)
432 fputs("cupsGetPPD(): ", stdout
);
435 if ((ppdfile
= cupsGetPPD(dest
->name
)) == NULL
)
447 fputs("ppdOpenFile(): ", stdout
);
450 if ((ppd
= ppdOpenFile(ppdfile
)) == NULL
)
466 fputs("cupsGetJobs: ", stdout
);
469 num_jobs
= cupsGetJobs(&jobs
, NULL
, 0, -1);
479 cupsFreeJobs(num_jobs
, jobs
);
480 cupsFreeDests(num_dests
, dests
);
487 * 'dests_equal()' - Determine whether two destinations are equal.
490 static int /* O - 1 if equal, 0 if not equal */
491 dests_equal(cups_dest_t
*a
, /* I - First destination */
492 cups_dest_t
*b
) /* I - Second destination */
494 int i
; /* Looping var */
495 cups_option_t
*aoption
; /* Current option */
496 const char *bval
; /* Option value */
505 if (_cups_strcasecmp(a
->name
, b
->name
) ||
506 (a
->instance
&& !b
->instance
) ||
507 (!a
->instance
&& b
->instance
) ||
508 (a
->instance
&& _cups_strcasecmp(a
->instance
, b
->instance
)) ||
509 a
->num_options
!= b
->num_options
)
512 for (i
= a
->num_options
, aoption
= a
->options
; i
> 0; i
--, aoption
++)
513 if ((bval
= cupsGetOption(aoption
->name
, b
->num_options
,
514 b
->options
)) == NULL
||
515 strcmp(aoption
->value
, bval
))
523 * 'enum_cb()' - Report additions and removals.
526 static int /* O - 1 to continue, 0 to stop */
527 enum_cb(void *user_data
, /* I - User data (unused) */
528 unsigned flags
, /* I - Destination flags */
529 cups_dest_t
*dest
) /* I - Destination */
531 int i
; /* Looping var */
532 cups_option_t
*option
; /* Current option */
535 if (flags
& CUPS_DEST_FLAGS_REMOVED
)
536 printf("Removed '%s':\n", dest
->name
);
538 printf("Added '%s':\n", dest
->name
);
540 for (i
= dest
->num_options
, option
= dest
->options
; i
> 0; i
--, option
++)
541 printf(" %s=\"%s\"\n", option
->name
, option
->value
);
550 * 'show_diffs()' - Show differences between two destinations.
554 show_diffs(cups_dest_t
*a
, /* I - First destination */
555 cups_dest_t
*b
) /* I - Second destination */
557 int i
; /* Looping var */
558 cups_option_t
*aoption
; /* Current option */
559 const char *bval
; /* Option value */
565 puts(" Item cupsGetDest cupsGetNamedDest");
566 puts(" -------------------- -------------------- --------------------");
568 if (_cups_strcasecmp(a
->name
, b
->name
))
569 printf(" name %-20.20s %-20.20s\n", a
->name
, b
->name
);
571 if ((a
->instance
&& !b
->instance
) ||
572 (!a
->instance
&& b
->instance
) ||
573 (a
->instance
&& _cups_strcasecmp(a
->instance
, b
->instance
)))
574 printf(" instance %-20.20s %-20.20s\n",
575 a
->instance
? a
->instance
: "(null)",
576 b
->instance
? b
->instance
: "(null)");
578 if (a
->num_options
!= b
->num_options
)
579 printf(" num_options %-20d %-20d\n", a
->num_options
,
582 for (i
= a
->num_options
, aoption
= a
->options
; i
> 0; i
--, aoption
++)
583 if ((bval
= cupsGetOption(aoption
->name
, b
->num_options
,
584 b
->options
)) == NULL
||
585 strcmp(aoption
->value
, bval
))
586 printf(" %-20.20s %-20.20s %-20.20s\n", aoption
->name
,
587 aoption
->value
, bval
? bval
: "(null)");