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], "print") && argc
== 5)
177 * ./testcups printer file interval
180 int interval
, /* Interval between writes */
182 cups_file_t
*fp
; /* Print file */
183 char buffer
[16384]; /* Read/write buffer */
184 ssize_t bytes
; /* Bytes read/written */
186 if ((fp
= cupsFileOpen(argv
[3], "r")) == NULL
)
188 printf("Unable to open \"%s\": %s\n", argv
[2], strerror(errno
));
192 if ((job_id
= cupsCreateJob(CUPS_HTTP_DEFAULT
, argv
[2], "testcups", 0,
195 printf("Unable to create print job on %s: %s\n", argv
[1],
196 cupsLastErrorString());
200 interval
= atoi(argv
[4]);
202 if (cupsStartDocument(CUPS_HTTP_DEFAULT
, argv
[1], job_id
, argv
[2],
203 CUPS_FORMAT_AUTO
, 1) != HTTP_STATUS_CONTINUE
)
205 puts("Unable to start document!");
209 while ((bytes
= cupsFileRead(fp
, buffer
, sizeof(buffer
))) > 0)
211 printf("Writing %d bytes...\n", (int)bytes
);
213 if (cupsWriteRequestData(CUPS_HTTP_DEFAULT
, buffer
,
214 bytes
) != HTTP_STATUS_CONTINUE
)
216 puts("Unable to write bytes!");
226 if (cupsFinishDocument(CUPS_HTTP_DEFAULT
,
227 argv
[1]) > IPP_STATUS_OK_IGNORED_OR_SUBSTITUTED
)
229 puts("Unable to finish document!");
237 puts("Run basic unit tests:");
241 puts("Enumerate printers (for N seconds, -1 for indefinitely):");
243 puts(" ./testcups enum [seconds]");
245 puts("Ask for a password:");
247 puts(" ./testcups password");
249 puts("Print a file (interval controls delay between buffers in seconds):");
251 puts(" ./testcups print printer file interval");
262 fputs("cupsGetDests: ", stdout
);
265 num_dests
= cupsGetDests(&dests
);
274 printf("PASS (%d dests)\n", num_dests
);
276 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
278 printf(" %s", dest
->name
);
281 printf(" /%s", dest
->instance
);
283 if (dest
->is_default
)
284 puts(" ***DEFAULT***");
294 fputs("cupsGetDest(NULL): ", stdout
);
297 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) == NULL
)
299 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
300 if (dest
->is_default
)
309 puts("PASS (no default)");
314 printf("PASS (%s)\n", dest
->name
);
317 * cupsGetNamedDest(NULL, NULL, NULL)
320 fputs("cupsGetNamedDest(NULL, NULL, NULL): ", stdout
);
323 if ((named_dest
= cupsGetNamedDest(NULL
, NULL
, NULL
)) == NULL
||
324 !dests_equal(dest
, named_dest
))
327 puts("PASS (no default)");
330 puts("FAIL (different values)");
331 show_diffs(dest
, named_dest
);
336 puts("FAIL (no default)");
341 printf("PASS (%s)\n", named_dest
->name
);
344 cupsFreeDests(1, named_dest
);
347 * cupsGetDest(printer)
350 printf("cupsGetDest(\"%s\"): ", dests
[num_dests
/ 2].name
);
353 if ((dest
= cupsGetDest(dests
[num_dests
/ 2].name
, NULL
, num_dests
,
363 * cupsGetNamedDest(NULL, printer, instance)
366 printf("cupsGetNamedDest(NULL, \"%s\", \"%s\"): ", dest
->name
,
367 dest
->instance
? dest
->instance
: "(null)");
370 if ((named_dest
= cupsGetNamedDest(NULL
, dest
->name
,
371 dest
->instance
)) == NULL
||
372 !dests_equal(dest
, named_dest
))
376 puts("FAIL (different values)");
377 show_diffs(dest
, named_dest
);
380 puts("FAIL (no destination)");
389 cupsFreeDests(1, named_dest
);
395 fputs("cupsPrintFile: ", stdout
);
398 if (cupsPrintFile(dest
->name
, "../data/testprint", "Test Page",
399 dest
->num_options
, dest
->options
) <= 0)
401 printf("FAIL (%s)\n", cupsLastErrorString());
408 * cupsGetPPD(printer)
411 fputs("cupsGetPPD(): ", stdout
);
414 if ((ppdfile
= cupsGetPPD(dest
->name
)) == NULL
)
426 fputs("ppdOpenFile(): ", stdout
);
429 if ((ppd
= ppdOpenFile(ppdfile
)) == NULL
)
445 fputs("cupsGetJobs: ", stdout
);
448 num_jobs
= cupsGetJobs(&jobs
, NULL
, 0, -1);
458 cupsFreeJobs(num_jobs
, jobs
);
459 cupsFreeDests(num_dests
, dests
);
466 * 'dests_equal()' - Determine whether two destinations are equal.
469 static int /* O - 1 if equal, 0 if not equal */
470 dests_equal(cups_dest_t
*a
, /* I - First destination */
471 cups_dest_t
*b
) /* I - Second destination */
473 int i
; /* Looping var */
474 cups_option_t
*aoption
; /* Current option */
475 const char *bval
; /* Option value */
484 if (_cups_strcasecmp(a
->name
, b
->name
) ||
485 (a
->instance
&& !b
->instance
) ||
486 (!a
->instance
&& b
->instance
) ||
487 (a
->instance
&& _cups_strcasecmp(a
->instance
, b
->instance
)) ||
488 a
->num_options
!= b
->num_options
)
491 for (i
= a
->num_options
, aoption
= a
->options
; i
> 0; i
--, aoption
++)
492 if ((bval
= cupsGetOption(aoption
->name
, b
->num_options
,
493 b
->options
)) == NULL
||
494 strcmp(aoption
->value
, bval
))
502 * 'enum_cb()' - Report additions and removals.
505 static int /* O - 1 to continue, 0 to stop */
506 enum_cb(void *user_data
, /* I - User data (unused) */
507 unsigned flags
, /* I - Destination flags */
508 cups_dest_t
*dest
) /* I - Destination */
510 int i
; /* Looping var */
511 cups_option_t
*option
; /* Current option */
514 if (flags
& CUPS_DEST_FLAGS_REMOVED
)
515 printf("Removed '%s':\n", dest
->name
);
517 printf("Added '%s':\n", dest
->name
);
519 for (i
= dest
->num_options
, option
= dest
->options
; i
> 0; i
--, option
++)
520 printf(" %s=\"%s\"\n", option
->name
, option
->value
);
529 * 'show_diffs()' - Show differences between two destinations.
533 show_diffs(cups_dest_t
*a
, /* I - First destination */
534 cups_dest_t
*b
) /* I - Second destination */
536 int i
; /* Looping var */
537 cups_option_t
*aoption
; /* Current option */
538 const char *bval
; /* Option value */
544 puts(" Item cupsGetDest cupsGetNamedDest");
545 puts(" -------------------- -------------------- --------------------");
547 if (_cups_strcasecmp(a
->name
, b
->name
))
548 printf(" name %-20.20s %-20.20s\n", a
->name
, b
->name
);
550 if ((a
->instance
&& !b
->instance
) ||
551 (!a
->instance
&& b
->instance
) ||
552 (a
->instance
&& _cups_strcasecmp(a
->instance
, b
->instance
)))
553 printf(" instance %-20.20s %-20.20s\n",
554 a
->instance
? a
->instance
: "(null)",
555 b
->instance
? b
->instance
: "(null)");
557 if (a
->num_options
!= b
->num_options
)
558 printf(" num_options %-20d %-20d\n", a
->num_options
,
561 for (i
= a
->num_options
, aoption
= a
->options
; i
> 0; i
--, aoption
++)
562 if ((bval
= cupsGetOption(aoption
->name
, b
->num_options
,
563 b
->options
)) == NULL
||
564 strcmp(aoption
->value
, bval
))
565 printf(" %-20.20s %-20.20s %-20.20s\n", aoption
->name
,
566 aoption
->value
, bval
? bval
: "(null)");