2 * CUPS API test program for CUPS.
4 * Copyright 2007-2017 by Apple Inc.
5 * Copyright 2007 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers...
20 #undef _CUPS_NO_DEPRECATED
21 #include "string-private.h"
31 static int dests_equal(cups_dest_t
*a
, cups_dest_t
*b
);
32 static int enum_cb(void *user_data
, unsigned flags
, cups_dest_t
*dest
);
33 static void show_diffs(cups_dest_t
*a
, cups_dest_t
*b
);
37 * 'main()' - Main entry.
40 int /* O - Exit status */
41 main(int argc
, /* I - Number of command-line arguments */
42 char *argv
[]) /* I - Command-line arguments */
44 int status
= 0, /* Exit status */
46 num_dests
; /* Number of destinations */
47 cups_dest_t
*dests
, /* Destinations */
48 *dest
, /* Current destination */
49 *named_dest
; /* Current named destination */
50 const char *dest_name
, /* Destination name */
51 *dval
, /* Destination value */
52 *ppdfile
; /* PPD file */
53 ppd_file_t
*ppd
; /* PPD file data */
54 int num_jobs
; /* Number of jobs for queue */
55 cups_job_t
*jobs
; /* Jobs for queue */
60 if (!strcmp(argv
[1], "enum"))
62 cups_ptype_t mask
= CUPS_PRINTER_LOCAL
,
63 /* Printer type mask */
64 type
= CUPS_PRINTER_LOCAL
;
66 int msec
= 0; /* Timeout in milliseconds */
69 for (i
= 2; i
< argc
; i
++)
70 if (isdigit(argv
[i
][0] & 255) || argv
[i
][0] == '.')
71 msec
= (int)(atof(argv
[i
]) * 1000);
72 else if (!_cups_strcasecmp(argv
[i
], "bw"))
74 mask
|= CUPS_PRINTER_BW
;
75 type
|= CUPS_PRINTER_BW
;
77 else if (!_cups_strcasecmp(argv
[i
], "color"))
79 mask
|= CUPS_PRINTER_COLOR
;
80 type
|= CUPS_PRINTER_COLOR
;
82 else if (!_cups_strcasecmp(argv
[i
], "mono"))
84 mask
|= CUPS_PRINTER_COLOR
;
86 else if (!_cups_strcasecmp(argv
[i
], "duplex"))
88 mask
|= CUPS_PRINTER_DUPLEX
;
89 type
|= CUPS_PRINTER_DUPLEX
;
91 else if (!_cups_strcasecmp(argv
[i
], "simplex"))
93 mask
|= CUPS_PRINTER_DUPLEX
;
95 else if (!_cups_strcasecmp(argv
[i
], "staple"))
97 mask
|= CUPS_PRINTER_STAPLE
;
98 type
|= CUPS_PRINTER_STAPLE
;
100 else if (!_cups_strcasecmp(argv
[i
], "copies"))
102 mask
|= CUPS_PRINTER_COPIES
;
103 type
|= CUPS_PRINTER_COPIES
;
105 else if (!_cups_strcasecmp(argv
[i
], "collate"))
107 mask
|= CUPS_PRINTER_COLLATE
;
108 type
|= CUPS_PRINTER_COLLATE
;
110 else if (!_cups_strcasecmp(argv
[i
], "punch"))
112 mask
|= CUPS_PRINTER_PUNCH
;
113 type
|= CUPS_PRINTER_PUNCH
;
115 else if (!_cups_strcasecmp(argv
[i
], "cover"))
117 mask
|= CUPS_PRINTER_COVER
;
118 type
|= CUPS_PRINTER_COVER
;
120 else if (!_cups_strcasecmp(argv
[i
], "bind"))
122 mask
|= CUPS_PRINTER_BIND
;
123 type
|= CUPS_PRINTER_BIND
;
125 else if (!_cups_strcasecmp(argv
[i
], "sort"))
127 mask
|= CUPS_PRINTER_SORT
;
128 type
|= CUPS_PRINTER_SORT
;
130 else if (!_cups_strcasecmp(argv
[i
], "mfp"))
132 mask
|= CUPS_PRINTER_MFP
;
133 type
|= CUPS_PRINTER_MFP
;
135 else if (!_cups_strcasecmp(argv
[i
], "printer"))
137 mask
|= CUPS_PRINTER_MFP
;
139 else if (!_cups_strcasecmp(argv
[i
], "large"))
141 mask
|= CUPS_PRINTER_LARGE
;
142 type
|= CUPS_PRINTER_LARGE
;
144 else if (!_cups_strcasecmp(argv
[i
], "medium"))
146 mask
|= CUPS_PRINTER_MEDIUM
;
147 type
|= CUPS_PRINTER_MEDIUM
;
149 else if (!_cups_strcasecmp(argv
[i
], "small"))
151 mask
|= CUPS_PRINTER_SMALL
;
152 type
|= CUPS_PRINTER_SMALL
;
155 fprintf(stderr
, "Unknown argument \"%s\" ignored...\n", argv
[i
]);
157 cupsEnumDests(CUPS_DEST_FLAGS_NONE
, msec
, NULL
, type
, mask
, enum_cb
, NULL
);
159 else if (!strcmp(argv
[1], "password"))
161 const char *pass
= cupsGetPassword("Password:");
162 /* Password string */
165 printf("Password entered: %s\n", pass
);
167 puts("No password entered.");
169 else if (!strcmp(argv
[1], "ppd") && argc
== 3)
172 * ./testcups ppd printer
175 http_status_t http_status
; /* Status */
176 char buffer
[1024]; /* PPD filename */
177 time_t modtime
= 0; /* Last modified */
179 if ((http_status
= cupsGetPPD3(CUPS_HTTP_DEFAULT
, argv
[2], &modtime
,
180 buffer
, sizeof(buffer
))) != HTTP_STATUS_OK
)
181 printf("Unable to get PPD: %d (%s)\n", (int)http_status
,
182 cupsLastErrorString());
186 else if (!strcmp(argv
[1], "print") && argc
== 5)
189 * ./testcups print printer file interval
192 int interval
, /* Interval between writes */
194 cups_file_t
*fp
; /* Print file */
195 char buffer
[16384]; /* Read/write buffer */
196 ssize_t bytes
; /* Bytes read/written */
198 if ((fp
= cupsFileOpen(argv
[3], "r")) == NULL
)
200 printf("Unable to open \"%s\": %s\n", argv
[2], strerror(errno
));
204 if ((job_id
= cupsCreateJob(CUPS_HTTP_DEFAULT
, argv
[2], "testcups", 0,
207 printf("Unable to create print job on %s: %s\n", argv
[1],
208 cupsLastErrorString());
212 interval
= atoi(argv
[4]);
214 if (cupsStartDocument(CUPS_HTTP_DEFAULT
, argv
[1], job_id
, argv
[2],
215 CUPS_FORMAT_AUTO
, 1) != HTTP_STATUS_CONTINUE
)
217 puts("Unable to start document!");
221 while ((bytes
= cupsFileRead(fp
, buffer
, sizeof(buffer
))) > 0)
223 printf("Writing %d bytes...\n", (int)bytes
);
225 if (cupsWriteRequestData(CUPS_HTTP_DEFAULT
, buffer
, (size_t)bytes
) != HTTP_STATUS_CONTINUE
)
227 puts("Unable to write bytes!");
232 sleep((unsigned)interval
);
237 if (cupsFinishDocument(CUPS_HTTP_DEFAULT
,
238 argv
[1]) > IPP_STATUS_OK_IGNORED_OR_SUBSTITUTED
)
240 puts("Unable to finish document!");
248 puts("Run basic unit tests:");
252 puts("Enumerate printers (for N seconds, -1 for indefinitely):");
254 puts(" ./testcups enum [seconds]");
256 puts("Ask for a password:");
258 puts(" ./testcups password");
260 puts("Get the PPD file:");
262 puts(" ./testcups ppd printer");
264 puts("Print a file (interval controls delay between buffers in seconds):");
266 puts(" ./testcups print printer file interval");
277 fputs("cupsGetDests: ", stdout
);
280 num_dests
= cupsGetDests(&dests
);
289 printf("PASS (%d dests)\n", num_dests
);
291 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
293 printf(" %s", dest
->name
);
296 printf(" /%s", dest
->instance
);
298 if (dest
->is_default
)
299 puts(" ***DEFAULT***");
309 fputs("cupsGetDest(NULL): ", stdout
);
312 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) == NULL
)
314 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
315 if (dest
->is_default
)
324 puts("PASS (no default)");
329 printf("PASS (%s)\n", dest
->name
);
332 * cupsGetNamedDest(NULL, NULL, NULL)
335 fputs("cupsGetNamedDest(NULL, NULL, NULL): ", stdout
);
338 if ((named_dest
= cupsGetNamedDest(NULL
, NULL
, NULL
)) == NULL
||
339 !dests_equal(dest
, named_dest
))
342 puts("PASS (no default)");
345 puts("FAIL (different values)");
346 show_diffs(dest
, named_dest
);
351 puts("FAIL (no default)");
356 printf("PASS (%s)\n", named_dest
->name
);
359 cupsFreeDests(1, named_dest
);
362 * cupsGetDest(printer)
365 for (i
= 0, dest_name
= NULL
; i
< num_dests
; i
++)
367 if ((dval
= cupsGetOption("printer-is-temporary", dests
[i
].num_options
, dest
[i
].options
)) != NULL
&& !strcmp(dval
, "false"))
369 dest_name
= dests
[i
].name
;
374 printf("cupsGetDest(\"%s\"): ", dest_name
? dest_name
: "(null)");
377 if ((dest
= cupsGetDest(dest_name
, NULL
, num_dests
, dests
)) == NULL
)
386 * cupsGetNamedDest(NULL, printer, instance)
389 printf("cupsGetNamedDest(NULL, \"%s\", \"%s\"): ", dest
->name
,
390 dest
->instance
? dest
->instance
: "(null)");
393 if ((named_dest
= cupsGetNamedDest(NULL
, dest
->name
, dest
->instance
)) == NULL
||
394 !dests_equal(dest
, named_dest
))
398 puts("FAIL (different values)");
399 show_diffs(dest
, named_dest
);
402 puts("FAIL (no destination)");
411 cupsFreeDests(1, named_dest
);
417 fputs("cupsPrintFile: ", stdout
);
420 if (cupsPrintFile(dest
->name
, "../test/testfile.pdf", "Test Page",
421 dest
->num_options
, dest
->options
) <= 0)
423 printf("FAIL (%s)\n", cupsLastErrorString());
430 * cupsGetPPD(printer)
433 fputs("cupsGetPPD: ", stdout
);
436 if ((ppdfile
= cupsGetPPD(dest
->name
)) == NULL
)
448 fputs("ppdOpenFile: ", stdout
);
451 if ((ppd
= ppdOpenFile(ppdfile
)) == NULL
)
467 fputs("cupsGetJobs: ", stdout
);
470 num_jobs
= cupsGetJobs(&jobs
, NULL
, 0, -1);
480 cupsFreeJobs(num_jobs
, jobs
);
481 cupsFreeDests(num_dests
, dests
);
488 * 'dests_equal()' - Determine whether two destinations are equal.
491 static int /* O - 1 if equal, 0 if not equal */
492 dests_equal(cups_dest_t
*a
, /* I - First destination */
493 cups_dest_t
*b
) /* I - Second destination */
495 int i
; /* Looping var */
496 cups_option_t
*aoption
; /* Current option */
497 const char *bval
; /* Option value */
506 if (_cups_strcasecmp(a
->name
, b
->name
) ||
507 (a
->instance
&& !b
->instance
) ||
508 (!a
->instance
&& b
->instance
) ||
509 (a
->instance
&& _cups_strcasecmp(a
->instance
, b
->instance
)) ||
510 a
->num_options
!= b
->num_options
)
513 for (i
= a
->num_options
, aoption
= a
->options
; i
> 0; i
--, aoption
++)
514 if ((bval
= cupsGetOption(aoption
->name
, b
->num_options
,
515 b
->options
)) == NULL
||
516 strcmp(aoption
->value
, bval
))
524 * 'enum_cb()' - Report additions and removals.
527 static int /* O - 1 to continue, 0 to stop */
528 enum_cb(void *user_data
, /* I - User data (unused) */
529 unsigned flags
, /* I - Destination flags */
530 cups_dest_t
*dest
) /* I - Destination */
532 int i
; /* Looping var */
533 cups_option_t
*option
; /* Current option */
538 if (flags
& CUPS_DEST_FLAGS_REMOVED
)
539 printf("Removed '%s':\n", dest
->name
);
541 printf("Added '%s':\n", dest
->name
);
543 for (i
= dest
->num_options
, option
= dest
->options
; i
> 0; i
--, option
++)
544 printf(" %s=\"%s\"\n", option
->name
, option
->value
);
553 * 'show_diffs()' - Show differences between two destinations.
557 show_diffs(cups_dest_t
*a
, /* I - First destination */
558 cups_dest_t
*b
) /* I - Second destination */
560 int i
; /* Looping var */
561 cups_option_t
*aoption
; /* Current option */
562 cups_option_t
*boption
; /* Current option */
563 const char *bval
; /* Option value */
569 puts(" Item cupsGetDest cupsGetNamedDest");
570 puts(" -------------------- ------------------------ ------------------------");
572 if (_cups_strcasecmp(a
->name
, b
->name
))
573 printf(" name %-24.24s %-24.24s\n", a
->name
, b
->name
);
575 if ((a
->instance
&& !b
->instance
) ||
576 (!a
->instance
&& b
->instance
) ||
577 (a
->instance
&& _cups_strcasecmp(a
->instance
, b
->instance
)))
578 printf(" instance %-24.24s %-24.24s\n",
579 a
->instance
? a
->instance
: "(null)",
580 b
->instance
? b
->instance
: "(null)");
582 if (a
->num_options
!= b
->num_options
)
583 printf(" num_options %-24d %-24d\n", a
->num_options
,
586 for (i
= a
->num_options
, aoption
= a
->options
; i
> 0; i
--, aoption
++)
587 if ((bval
= cupsGetOption(aoption
->name
, b
->num_options
,
588 b
->options
)) == NULL
||
589 strcmp(aoption
->value
, bval
))
590 printf(" %-20.20s %-24.24s %-24.24s\n", aoption
->name
,
591 aoption
->value
, bval
? bval
: "(null)");
593 for (i
= b
->num_options
, boption
= b
->options
; i
> 0; i
--, boption
++)
594 if (!cupsGetOption(boption
->name
, a
->num_options
, a
->options
))
595 printf(" %-20.20s %-24.24s %-24.24s\n", boption
->name
,
596 boption
->value
, "(null)");