]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
75bd9771 | 2 | * "$Id: lpoptions.c 7669 2008-06-17 22:02:33Z mike $" |
ef416fc2 | 3 | * |
4 | * Printer option program for the Common UNIX Printing System (CUPS). | |
5 | * | |
bc44d920 | 6 | * Copyright 2007 by Apple Inc. |
ef416fc2 | 7 | * Copyright 1997-2006 by Easy Software Products. |
8 | * | |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 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/". | |
ef416fc2 | 14 | * |
15 | * Contents: | |
16 | * | |
17 | * main() - Main entry. | |
18 | * list_group() - List printer-specific options from the PPD group. | |
19 | * list_options() - List printer-specific options from the PPD file. | |
20 | * usage() - Show program usage and exit. | |
21 | */ | |
22 | ||
23 | /* | |
24 | * Include necessary headers... | |
25 | */ | |
26 | ||
27 | #include <cups/string.h> | |
28 | #include <cups/cups.h> | |
29 | #include <cups/i18n.h> | |
30 | #include <stdlib.h> | |
31 | #include <errno.h> | |
32 | ||
33 | ||
34 | /* | |
35 | * Local functions... | |
36 | */ | |
37 | ||
dd1abb6b MS |
38 | static void list_group(ppd_file_t *ppd, ppd_group_t *group); |
39 | static void list_options(cups_dest_t *dest); | |
40 | static void usage(void); | |
ef416fc2 | 41 | |
42 | ||
43 | /* | |
44 | * 'main()' - Main entry. | |
45 | */ | |
46 | ||
47 | int /* O - Exit status */ | |
48 | main(int argc, /* I - Number of command-line arguments */ | |
49 | char *argv[]) /* I - Command-line arguments */ | |
50 | { | |
51 | int i, j; /* Looping vars */ | |
52 | int changes; /* Did we make changes? */ | |
53 | int num_options; /* Number of options */ | |
54 | cups_option_t *options; /* Options */ | |
55 | int num_dests; /* Number of destinations */ | |
56 | cups_dest_t *dests; /* Destinations */ | |
57 | cups_dest_t *dest; /* Current destination */ | |
58 | char *printer, /* Printer name */ | |
59 | *instance, /* Instance name */ | |
60 | *option; /* Current option */ | |
61 | ||
62 | ||
07725fee | 63 | _cupsSetLocale(argv); |
d09495fa | 64 | |
ef416fc2 | 65 | /* |
66 | * Loop through the command-line arguments... | |
67 | */ | |
68 | ||
69 | dest = NULL; | |
70 | num_dests = 0; | |
71 | dests = NULL; | |
72 | num_options = 0; | |
73 | options = NULL; | |
74 | changes = 0; | |
75 | ||
76 | for (i = 1; i < argc; i ++) | |
77 | if (argv[i][0] == '-') | |
78 | { | |
79 | switch (argv[i][1]) | |
80 | { | |
81 | case 'd' : /* -d printer */ | |
82 | if (argv[i][2]) | |
83 | printer = argv[i] + 2; | |
84 | else | |
85 | { | |
86 | i ++; | |
87 | if (i >= argc) | |
88 | usage(); | |
89 | ||
90 | printer = argv[i]; | |
91 | } | |
92 | ||
93 | if ((instance = strrchr(printer, '/')) != NULL) | |
94 | *instance++ = '\0'; | |
95 | ||
96 | if (num_dests == 0) | |
97 | num_dests = cupsGetDests(&dests); | |
98 | ||
99 | if ((dest = cupsGetDest(printer, instance, num_dests, dests)) == NULL) | |
100 | { | |
fa73b229 | 101 | _cupsLangPuts(stderr, |
ef416fc2 | 102 | _("lpoptions: Unknown printer or class!\n")); |
103 | return (1); | |
104 | } | |
105 | ||
106 | /* | |
107 | * Set the default destination... | |
108 | */ | |
109 | ||
110 | for (j = 0; j < num_dests; j ++) | |
111 | dests[j].is_default = 0; | |
112 | ||
113 | dest->is_default = 1; | |
114 | ||
115 | cupsSetDests(num_dests, dests); | |
116 | ||
117 | for (j = 0; j < dest->num_options; j ++) | |
118 | if (cupsGetOption(dest->options[j].name, num_options, options) == NULL) | |
119 | num_options = cupsAddOption(dest->options[j].name, | |
120 | dest->options[j].value, | |
121 | num_options, &options); | |
122 | break; | |
123 | ||
124 | case 'h' : /* -h server */ | |
125 | if (argv[i][2]) | |
126 | cupsSetServer(argv[i] + 2); | |
127 | else | |
128 | { | |
129 | i ++; | |
130 | if (i >= argc) | |
131 | usage(); | |
132 | ||
133 | cupsSetServer(argv[i]); | |
134 | } | |
135 | break; | |
136 | ||
137 | case 'E' : /* Encrypt connection */ | |
138 | cupsSetEncryption(HTTP_ENCRYPT_REQUIRED); | |
139 | break; | |
140 | ||
141 | case 'l' : /* -l (list options) */ | |
142 | if (dest == NULL) | |
143 | { | |
144 | if (num_dests == 0) | |
145 | num_dests = cupsGetDests(&dests); | |
146 | ||
147 | if ((dest = cupsGetDest(NULL, NULL, num_dests, dests)) == NULL) | |
148 | dest = dests; | |
149 | } | |
150 | ||
151 | if (dest == NULL) | |
fa73b229 | 152 | _cupsLangPuts(stderr, _("lpoptions: No printers!?!\n")); |
ef416fc2 | 153 | else |
154 | list_options(dest); | |
155 | ||
156 | changes = -1; | |
157 | break; | |
158 | ||
159 | case 'o' : /* -o option[=value] */ | |
8ca02f3c | 160 | if (dest == NULL) |
161 | { | |
162 | if (num_dests == 0) | |
163 | num_dests = cupsGetDests(&dests); | |
164 | ||
165 | if ((dest = cupsGetDest(NULL, NULL, num_dests, dests)) == NULL) | |
166 | dest = dests; | |
167 | ||
7594b224 | 168 | if (dest == NULL) |
169 | { | |
170 | _cupsLangPuts(stderr, _("lpoptions: No printers!?!\n")); | |
171 | return (1); | |
172 | } | |
173 | ||
8ca02f3c | 174 | for (j = 0; j < dest->num_options; j ++) |
175 | if (cupsGetOption(dest->options[j].name, num_options, options) == NULL) | |
176 | num_options = cupsAddOption(dest->options[j].name, | |
177 | dest->options[j].value, | |
178 | num_options, &options); | |
179 | } | |
180 | ||
ef416fc2 | 181 | if (argv[i][2]) |
182 | num_options = cupsParseOptions(argv[i] + 2, num_options, &options); | |
183 | else | |
184 | { | |
185 | i ++; | |
186 | if (i >= argc) | |
187 | usage(); | |
188 | ||
189 | num_options = cupsParseOptions(argv[i], num_options, &options); | |
190 | } | |
191 | ||
192 | changes = 1; | |
193 | break; | |
194 | ||
195 | case 'p' : /* -p printer */ | |
196 | if (argv[i][2]) | |
197 | printer = argv[i] + 2; | |
198 | else | |
199 | { | |
200 | i ++; | |
201 | if (i >= argc) | |
202 | usage(); | |
203 | ||
204 | printer = argv[i]; | |
205 | } | |
206 | ||
207 | if ((instance = strrchr(printer, '/')) != NULL) | |
208 | *instance++ = '\0'; | |
209 | ||
210 | if (num_dests == 0) | |
211 | num_dests = cupsGetDests(&dests); | |
212 | ||
213 | if ((dest = cupsGetDest(printer, instance, num_dests, dests)) == NULL) | |
214 | { | |
215 | num_dests = cupsAddDest(printer, instance, num_dests, &dests); | |
216 | dest = cupsGetDest(printer, instance, num_dests, dests); | |
217 | ||
218 | if (dest == NULL) | |
219 | { | |
fa73b229 | 220 | _cupsLangPrintf(stderr, |
ef416fc2 | 221 | _("lpoptions: Unable to add printer or " |
222 | "instance: %s\n"), | |
223 | strerror(errno)); | |
224 | return (1); | |
225 | } | |
226 | } | |
227 | ||
228 | for (j = 0; j < dest->num_options; j ++) | |
229 | if (cupsGetOption(dest->options[j].name, num_options, options) == NULL) | |
230 | num_options = cupsAddOption(dest->options[j].name, | |
231 | dest->options[j].value, | |
232 | num_options, &options); | |
233 | break; | |
234 | ||
235 | case 'r' : /* -r option (remove) */ | |
8ca02f3c | 236 | if (dest == NULL) |
237 | { | |
238 | if (num_dests == 0) | |
239 | num_dests = cupsGetDests(&dests); | |
240 | ||
241 | if ((dest = cupsGetDest(NULL, NULL, num_dests, dests)) == NULL) | |
242 | dest = dests; | |
243 | ||
7594b224 | 244 | if (dest == NULL) |
245 | { | |
246 | _cupsLangPuts(stderr, _("lpoptions: No printers!?!\n")); | |
247 | return (1); | |
248 | } | |
249 | ||
8ca02f3c | 250 | for (j = 0; j < dest->num_options; j ++) |
251 | if (cupsGetOption(dest->options[j].name, num_options, options) == NULL) | |
252 | num_options = cupsAddOption(dest->options[j].name, | |
253 | dest->options[j].value, | |
254 | num_options, &options); | |
255 | } | |
256 | ||
ef416fc2 | 257 | if (argv[i][2]) |
258 | option = argv[i] + 2; | |
259 | else | |
260 | { | |
261 | i ++; | |
262 | if (i >= argc) | |
263 | usage(); | |
264 | ||
265 | option = argv[i]; | |
266 | } | |
267 | ||
268 | for (j = 0; j < num_options; j ++) | |
8ca02f3c | 269 | if (!strcasecmp(options[j].name, option)) |
ef416fc2 | 270 | { |
271 | /* | |
272 | * Remove this option... | |
273 | */ | |
274 | ||
275 | num_options --; | |
276 | ||
277 | if (j < num_options) | |
278 | memcpy(options + j, options + j + 1, | |
279 | sizeof(cups_option_t) * (num_options - j)); | |
280 | break; | |
281 | } | |
282 | ||
283 | changes = 1; | |
284 | break; | |
285 | ||
286 | case 'x' : /* -x printer */ | |
287 | if (argv[i][2]) | |
288 | printer = argv[i] + 2; | |
289 | else | |
290 | { | |
291 | i ++; | |
292 | if (i >= argc) | |
293 | usage(); | |
294 | ||
295 | printer = argv[i]; | |
296 | } | |
297 | ||
298 | if ((instance = strrchr(printer, '/')) != NULL) | |
299 | *instance++ = '\0'; | |
300 | ||
301 | if (num_dests == 0) | |
302 | num_dests = cupsGetDests(&dests); | |
303 | ||
304 | if ((dest = cupsGetDest(printer, instance, num_dests, dests)) != NULL) | |
305 | { | |
306 | cupsFreeOptions(dest->num_options, dest->options); | |
307 | ||
308 | /* | |
309 | * If we are "deleting" the default printer, then just set the | |
310 | * number of options to 0; if it is also the system default | |
311 | * then cupsSetDests() will remove it for us... | |
312 | */ | |
313 | ||
314 | if (dest->is_default) | |
315 | { | |
316 | dest->num_options = 0; | |
317 | dest->options = NULL; | |
318 | } | |
319 | else | |
320 | { | |
321 | num_dests --; | |
322 | ||
323 | j = dest - dests; | |
324 | if (j < num_dests) | |
325 | memcpy(dest, dest + 1, (num_dests - j) * sizeof(cups_dest_t)); | |
326 | } | |
327 | } | |
328 | ||
329 | cupsSetDests(num_dests, dests); | |
330 | dest = NULL; | |
331 | changes = -1; | |
332 | break; | |
333 | ||
334 | default : | |
335 | usage(); | |
336 | } | |
337 | } | |
338 | else | |
339 | usage(); | |
340 | ||
341 | if (num_dests == 0) | |
342 | num_dests = cupsGetDests(&dests); | |
343 | ||
344 | if (dest == NULL) | |
345 | { | |
346 | if ((dest = cupsGetDest(NULL, NULL, num_dests, dests)) != NULL) | |
347 | { | |
348 | for (j = 0; j < dest->num_options; j ++) | |
349 | if (cupsGetOption(dest->options[j].name, num_options, options) == NULL) | |
350 | num_options = cupsAddOption(dest->options[j].name, | |
351 | dest->options[j].value, | |
352 | num_options, &options); | |
353 | } | |
354 | } | |
355 | ||
356 | if (dest == NULL) | |
357 | return (0); | |
358 | ||
359 | if (changes > 0) | |
360 | { | |
361 | /* | |
362 | * Set printer options... | |
363 | */ | |
364 | ||
365 | cupsFreeOptions(dest->num_options, dest->options); | |
366 | ||
367 | dest->num_options = num_options; | |
368 | dest->options = options; | |
369 | ||
370 | cupsSetDests(num_dests, dests); | |
371 | } | |
372 | else if (changes == 0) | |
373 | { | |
374 | num_options = dest->num_options; | |
375 | options = dest->options; | |
376 | ||
377 | for (i = 0; i < num_options; i ++) | |
378 | { | |
379 | if (i) | |
fa73b229 | 380 | _cupsLangPuts(stdout, " "); |
ef416fc2 | 381 | |
382 | if (!options[i].value[0]) | |
fa73b229 | 383 | _cupsLangPrintf(stdout, "%s", options[i].name); |
ef416fc2 | 384 | else if (strchr(options[i].value, ' ') != NULL || |
385 | strchr(options[i].value, '\t') != NULL) | |
fa73b229 | 386 | _cupsLangPrintf(stdout, "%s=\'%s\'", options[i].name, |
ef416fc2 | 387 | options[i].value); |
388 | else | |
fa73b229 | 389 | _cupsLangPrintf(stdout, "%s=%s", options[i].name, |
ef416fc2 | 390 | options[i].value); |
391 | } | |
392 | ||
fa73b229 | 393 | _cupsLangPuts(stdout, "\n"); |
ef416fc2 | 394 | } |
395 | ||
396 | return (0); | |
397 | } | |
398 | ||
399 | /* | |
400 | * 'list_group()' - List printer-specific options from the PPD group. | |
401 | */ | |
402 | ||
dd1abb6b MS |
403 | static void |
404 | list_group(ppd_file_t *ppd, /* I - PPD file */ | |
405 | ppd_group_t *group) /* I - Group to show */ | |
ef416fc2 | 406 | { |
dd1abb6b MS |
407 | int i, j; /* Looping vars */ |
408 | ppd_option_t *option; /* Current option */ | |
409 | ppd_choice_t *choice; /* Current choice */ | |
410 | ppd_group_t *subgroup; /* Current subgroup */ | |
ef416fc2 | 411 | |
412 | ||
413 | for (i = group->num_options, option = group->options; i > 0; i --, option ++) | |
414 | { | |
dd1abb6b MS |
415 | if (!strcasecmp(option->keyword, "PageRegion")) |
416 | continue; | |
417 | ||
fa73b229 | 418 | _cupsLangPrintf(stdout, "%s/%s:", option->keyword, option->text); |
ef416fc2 | 419 | |
dd1abb6b MS |
420 | for (j = option->num_choices, choice = option->choices; |
421 | j > 0; | |
422 | j --, choice ++) | |
423 | if (!strcasecmp(choice->choice, "Custom")) | |
424 | { | |
425 | ppd_coption_t *coption; /* Custom option */ | |
426 | ppd_cparam_t *cparam; /* Custom parameter */ | |
427 | static const char * const types[] = | |
428 | { /* Parameter types */ | |
429 | "CURVE", | |
430 | "INTEGER", | |
431 | "INVCURVE", | |
432 | "PASSCODE", | |
433 | "PASSWORD", | |
434 | "POINTS", | |
435 | "REAL", | |
436 | "STRING" | |
437 | }; | |
438 | ||
439 | ||
440 | if ((coption = ppdFindCustomOption(ppd, option->keyword)) == NULL || | |
441 | cupsArrayCount(coption->params) == 0) | |
442 | _cupsLangPrintf(stdout, " %sCustom", choice->marked ? "*" : ""); | |
443 | else if (!strcasecmp(option->keyword, "PageSize") || | |
444 | !strcasecmp(option->keyword, "PageRegion")) | |
445 | _cupsLangPrintf(stdout, " %sCustom.WIDTHxHEIGHT", | |
446 | choice->marked ? "*" : ""); | |
447 | else | |
448 | { | |
449 | cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params); | |
450 | ||
451 | if (cupsArrayCount(coption->params) == 1) | |
452 | _cupsLangPrintf(stdout, " %sCustom.%s", choice->marked ? "*" : "", | |
453 | types[cparam->type]); | |
454 | else | |
455 | { | |
456 | const char *prefix; /* Prefix string */ | |
457 | ||
458 | ||
459 | if (choice->marked) | |
460 | prefix = " *{"; | |
461 | else | |
462 | prefix = " {"; | |
463 | ||
464 | while (cparam) | |
465 | { | |
466 | _cupsLangPrintf(stdout, "%s%s=%s", choice->marked ? "*" : "", | |
467 | cparam->name, types[cparam->type]); | |
468 | cparam = (ppd_cparam_t *)cupsArrayNext(coption->params); | |
469 | prefix = " "; | |
470 | } | |
471 | ||
472 | _cupsLangPuts(stdout, "}"); | |
473 | } | |
474 | } | |
475 | } | |
476 | else if (choice->marked) | |
fa73b229 | 477 | _cupsLangPrintf(stdout, " *%s", choice->choice); |
ef416fc2 | 478 | else |
fa73b229 | 479 | _cupsLangPrintf(stdout, " %s", choice->choice); |
ef416fc2 | 480 | |
fa73b229 | 481 | _cupsLangPuts(stdout, "\n"); |
ef416fc2 | 482 | } |
483 | ||
484 | for (i = group->num_subgroups, subgroup = group->subgroups; i > 0; i --, subgroup ++) | |
dd1abb6b | 485 | list_group(ppd, subgroup); |
ef416fc2 | 486 | } |
487 | ||
488 | ||
489 | /* | |
490 | * 'list_options()' - List printer-specific options from the PPD file. | |
491 | */ | |
492 | ||
dd1abb6b MS |
493 | static void |
494 | list_options(cups_dest_t *dest) /* I - Destination to list */ | |
ef416fc2 | 495 | { |
dd1abb6b MS |
496 | int i; /* Looping var */ |
497 | const char *filename; /* PPD filename */ | |
498 | ppd_file_t *ppd; /* PPD data */ | |
499 | ppd_group_t *group; /* Current group */ | |
ef416fc2 | 500 | |
501 | ||
502 | if ((filename = cupsGetPPD(dest->name)) == NULL) | |
503 | { | |
fa73b229 | 504 | _cupsLangPrintf(stderr, |
b86bc4cf | 505 | _("lpoptions: Unable to get PPD file for %s: %s\n"), |
506 | dest->name, cupsLastErrorString()); | |
ef416fc2 | 507 | return; |
508 | } | |
509 | ||
510 | if ((ppd = ppdOpenFile(filename)) == NULL) | |
511 | { | |
512 | unlink(filename); | |
fa73b229 | 513 | _cupsLangPrintf(stderr, |
ef416fc2 | 514 | _("lpoptions: Unable to open PPD file for %s!\n"), |
515 | dest->name); | |
516 | return; | |
517 | } | |
518 | ||
519 | ppdMarkDefaults(ppd); | |
520 | cupsMarkOptions(ppd, dest->num_options, dest->options); | |
521 | ||
522 | for (i = ppd->num_groups, group = ppd->groups; i > 0; i --, group ++) | |
dd1abb6b | 523 | list_group(ppd, group); |
ef416fc2 | 524 | |
525 | ppdClose(ppd); | |
526 | unlink(filename); | |
527 | } | |
528 | ||
529 | ||
530 | /* | |
531 | * 'usage()' - Show program usage and exit. | |
532 | */ | |
533 | ||
dd1abb6b | 534 | static void |
ef416fc2 | 535 | usage(void) |
536 | { | |
fa73b229 | 537 | _cupsLangPuts(stdout, |
ef416fc2 | 538 | _("Usage: lpoptions [-h server] [-E] -d printer\n" |
539 | " lpoptions [-h server] [-E] [-p printer] -l\n" | |
540 | " lpoptions [-h server] [-E] -p printer -o " | |
541 | "option[=value] ...\n" | |
542 | " lpoptions [-h server] [-E] -x printer\n")); | |
543 | ||
544 | exit(1); | |
545 | } | |
546 | ||
547 | ||
548 | /* | |
75bd9771 | 549 | * End of "$Id: lpoptions.c 7669 2008-06-17 22:02:33Z mike $". |
ef416fc2 | 550 | */ |