]>
git.ipfire.org Git - thirdparty/cups.git/blob - systemv/lpoptions.c
2 * "$Id: lpoptions.c 7720 2008-07-11 22:46:21Z mike $"
4 * Printer option program for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2006 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/".
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.
24 * Include necessary headers...
27 #include <cups/string.h>
28 #include <cups/cups.h>
29 #include <cups/i18n.h>
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);
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 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 */
66 * Loop through the command-line arguments...
76 for (i
= 1; i
< argc
; i
++)
77 if (argv
[i
][0] == '-')
81 case 'd' : /* -d printer */
83 printer
= argv
[i
] + 2;
93 if ((instance
= strrchr(printer
, '/')) != NULL
)
97 num_dests
= cupsGetDests(&dests
);
99 if (num_dests
== 0 || !dests
||
100 (dest
= cupsGetDest(printer
, instance
, num_dests
,
103 _cupsLangPuts(stderr
,
104 _("lpoptions: Unknown printer or class!\n"));
109 * Set the default destination...
112 for (j
= 0; j
< num_dests
; j
++)
113 dests
[j
].is_default
= 0;
115 dest
->is_default
= 1;
117 cupsSetDests(num_dests
, dests
);
119 for (j
= 0; j
< dest
->num_options
; j
++)
120 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
121 num_options
= cupsAddOption(dest
->options
[j
].name
,
122 dest
->options
[j
].value
,
123 num_options
, &options
);
126 case 'h' : /* -h server */
128 cupsSetServer(argv
[i
] + 2);
135 cupsSetServer(argv
[i
]);
139 case 'E' : /* Encrypt connection */
140 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
143 case 'l' : /* -l (list options) */
147 num_dests
= cupsGetDests(&dests
);
149 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) == NULL
)
154 _cupsLangPuts(stderr
, _("lpoptions: No printers!?!\n"));
161 case 'o' : /* -o option[=value] */
165 num_dests
= cupsGetDests(&dests
);
167 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) == NULL
)
172 _cupsLangPuts(stderr
, _("lpoptions: No printers!?!\n"));
176 for (j
= 0; j
< dest
->num_options
; j
++)
177 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
178 num_options
= cupsAddOption(dest
->options
[j
].name
,
179 dest
->options
[j
].value
,
180 num_options
, &options
);
184 num_options
= cupsParseOptions(argv
[i
] + 2, num_options
, &options
);
191 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
197 case 'p' : /* -p printer */
199 printer
= argv
[i
] + 2;
209 if ((instance
= strrchr(printer
, '/')) != NULL
)
213 num_dests
= cupsGetDests(&dests
);
215 if ((dest
= cupsGetDest(printer
, instance
, num_dests
, dests
)) == NULL
)
217 num_dests
= cupsAddDest(printer
, instance
, num_dests
, &dests
);
218 dest
= cupsGetDest(printer
, instance
, num_dests
, dests
);
222 _cupsLangPrintf(stderr
,
223 _("lpoptions: Unable to add printer or "
230 for (j
= 0; j
< dest
->num_options
; j
++)
231 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
232 num_options
= cupsAddOption(dest
->options
[j
].name
,
233 dest
->options
[j
].value
,
234 num_options
, &options
);
237 case 'r' : /* -r option (remove) */
241 num_dests
= cupsGetDests(&dests
);
243 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) == NULL
)
248 _cupsLangPuts(stderr
, _("lpoptions: No printers!?!\n"));
252 for (j
= 0; j
< dest
->num_options
; j
++)
253 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
254 num_options
= cupsAddOption(dest
->options
[j
].name
,
255 dest
->options
[j
].value
,
256 num_options
, &options
);
260 option
= argv
[i
] + 2;
270 for (j
= 0; j
< num_options
; j
++)
271 if (!strcasecmp(options
[j
].name
, option
))
274 * Remove this option...
280 memcpy(options
+ j
, options
+ j
+ 1,
281 sizeof(cups_option_t
) * (num_options
- j
));
288 case 'x' : /* -x printer */
290 printer
= argv
[i
] + 2;
300 if ((instance
= strrchr(printer
, '/')) != NULL
)
304 num_dests
= cupsGetDests(&dests
);
306 if ((dest
= cupsGetDest(printer
, instance
, num_dests
, dests
)) != NULL
)
308 cupsFreeOptions(dest
->num_options
, dest
->options
);
311 * If we are "deleting" the default printer, then just set the
312 * number of options to 0; if it is also the system default
313 * then cupsSetDests() will remove it for us...
316 if (dest
->is_default
)
318 dest
->num_options
= 0;
319 dest
->options
= NULL
;
327 memcpy(dest
, dest
+ 1, (num_dests
- j
) * sizeof(cups_dest_t
));
331 cupsSetDests(num_dests
, dests
);
344 num_dests
= cupsGetDests(&dests
);
348 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) != NULL
)
350 for (j
= 0; j
< dest
->num_options
; j
++)
351 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
352 num_options
= cupsAddOption(dest
->options
[j
].name
,
353 dest
->options
[j
].value
,
354 num_options
, &options
);
364 * Set printer options...
367 cupsFreeOptions(dest
->num_options
, dest
->options
);
369 dest
->num_options
= num_options
;
370 dest
->options
= options
;
372 cupsSetDests(num_dests
, dests
);
374 else if (changes
== 0)
376 num_options
= dest
->num_options
;
377 options
= dest
->options
;
379 for (i
= 0; i
< num_options
; i
++)
382 _cupsLangPuts(stdout
, " ");
384 if (!options
[i
].value
[0])
385 _cupsLangPrintf(stdout
, "%s", options
[i
].name
);
386 else if (strchr(options
[i
].value
, ' ') != NULL
||
387 strchr(options
[i
].value
, '\t') != NULL
)
388 _cupsLangPrintf(stdout
, "%s=\'%s\'", options
[i
].name
,
391 _cupsLangPrintf(stdout
, "%s=%s", options
[i
].name
,
395 _cupsLangPuts(stdout
, "\n");
402 * 'list_group()' - List printer-specific options from the PPD group.
406 list_group(ppd_file_t
*ppd
, /* I - PPD file */
407 ppd_group_t
*group
) /* I - Group to show */
409 int i
, j
; /* Looping vars */
410 ppd_option_t
*option
; /* Current option */
411 ppd_choice_t
*choice
; /* Current choice */
412 ppd_group_t
*subgroup
; /* Current subgroup */
415 for (i
= group
->num_options
, option
= group
->options
; i
> 0; i
--, option
++)
417 if (!strcasecmp(option
->keyword
, "PageRegion"))
420 _cupsLangPrintf(stdout
, "%s/%s:", option
->keyword
, option
->text
);
422 for (j
= option
->num_choices
, choice
= option
->choices
;
425 if (!strcasecmp(choice
->choice
, "Custom"))
427 ppd_coption_t
*coption
; /* Custom option */
428 ppd_cparam_t
*cparam
; /* Custom parameter */
429 static const char * const types
[] =
430 { /* Parameter types */
442 if ((coption
= ppdFindCustomOption(ppd
, option
->keyword
)) == NULL
||
443 cupsArrayCount(coption
->params
) == 0)
444 _cupsLangPrintf(stdout
, " %sCustom", choice
->marked
? "*" : "");
445 else if (!strcasecmp(option
->keyword
, "PageSize") ||
446 !strcasecmp(option
->keyword
, "PageRegion"))
447 _cupsLangPrintf(stdout
, " %sCustom.WIDTHxHEIGHT",
448 choice
->marked
? "*" : "");
451 cparam
= (ppd_cparam_t
*)cupsArrayFirst(coption
->params
);
453 if (cupsArrayCount(coption
->params
) == 1)
454 _cupsLangPrintf(stdout
, " %sCustom.%s", choice
->marked
? "*" : "",
455 types
[cparam
->type
]);
458 const char *prefix
; /* Prefix string */
468 _cupsLangPrintf(stdout
, "%s%s=%s", prefix
, cparam
->name
,
469 types
[cparam
->type
]);
470 cparam
= (ppd_cparam_t
*)cupsArrayNext(coption
->params
);
474 _cupsLangPuts(stdout
, "}");
478 else if (choice
->marked
)
479 _cupsLangPrintf(stdout
, " *%s", choice
->choice
);
481 _cupsLangPrintf(stdout
, " %s", choice
->choice
);
483 _cupsLangPuts(stdout
, "\n");
486 for (i
= group
->num_subgroups
, subgroup
= group
->subgroups
; i
> 0; i
--, subgroup
++)
487 list_group(ppd
, subgroup
);
492 * 'list_options()' - List printer-specific options from the PPD file.
496 list_options(cups_dest_t
*dest
) /* I - Destination to list */
498 int i
; /* Looping var */
499 const char *filename
; /* PPD filename */
500 ppd_file_t
*ppd
; /* PPD data */
501 ppd_group_t
*group
; /* Current group */
504 if ((filename
= cupsGetPPD(dest
->name
)) == NULL
)
506 _cupsLangPrintf(stderr
,
507 _("lpoptions: Unable to get PPD file for %s: %s\n"),
508 dest
->name
, cupsLastErrorString());
512 if ((ppd
= ppdOpenFile(filename
)) == NULL
)
515 _cupsLangPrintf(stderr
,
516 _("lpoptions: Unable to open PPD file for %s!\n"),
521 ppdMarkDefaults(ppd
);
522 cupsMarkOptions(ppd
, dest
->num_options
, dest
->options
);
524 for (i
= ppd
->num_groups
, group
= ppd
->groups
; i
> 0; i
--, group
++)
525 list_group(ppd
, group
);
533 * 'usage()' - Show program usage and exit.
539 _cupsLangPuts(stdout
,
540 _("Usage: lpoptions [-h server] [-E] -d printer\n"
541 " lpoptions [-h server] [-E] [-p printer] -l\n"
542 " lpoptions [-h server] [-E] -p printer -o "
543 "option[=value] ...\n"
544 " lpoptions [-h server] [-E] -x printer\n"));
551 * End of "$Id: lpoptions.c 7720 2008-07-11 22:46:21Z mike $".