]>
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 CUPS.
6 * Copyright 2007-2010 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/cups-private.h>
34 static void list_group(ppd_file_t
*ppd
, ppd_group_t
*group
);
35 static void list_options(cups_dest_t
*dest
);
36 static void usage(void);
40 * 'main()' - Main entry.
43 int /* O - Exit status */
44 main(int argc
, /* I - Number of command-line arguments */
45 char *argv
[]) /* I - Command-line arguments */
47 int i
, j
; /* Looping vars */
48 int changes
; /* Did we make changes? */
49 int num_options
; /* Number of options */
50 cups_option_t
*options
; /* Options */
51 int num_dests
; /* Number of destinations */
52 cups_dest_t
*dests
; /* Destinations */
53 cups_dest_t
*dest
; /* Current destination */
54 char *printer
, /* Printer name */
55 *instance
, /* Instance name */
56 *option
; /* Current option */
62 * Loop through the command-line arguments...
72 for (i
= 1; i
< argc
; i
++)
73 if (argv
[i
][0] == '-')
77 case 'd' : /* -d printer */
79 printer
= argv
[i
] + 2;
89 if ((instance
= strrchr(printer
, '/')) != NULL
)
93 num_dests
= cupsGetDests(&dests
);
95 if (num_dests
== 0 || !dests
||
96 (dest
= cupsGetDest(printer
, instance
, num_dests
,
100 _("lpoptions: Unknown printer or class\n"));
105 * Set the default destination...
108 for (j
= 0; j
< num_dests
; j
++)
109 dests
[j
].is_default
= 0;
111 dest
->is_default
= 1;
113 cupsSetDests(num_dests
, dests
);
115 for (j
= 0; j
< dest
->num_options
; j
++)
116 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
117 num_options
= cupsAddOption(dest
->options
[j
].name
,
118 dest
->options
[j
].value
,
119 num_options
, &options
);
122 case 'h' : /* -h server */
124 cupsSetServer(argv
[i
] + 2);
131 cupsSetServer(argv
[i
]);
135 case 'E' : /* Encrypt connection */
136 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
139 case 'l' : /* -l (list options) */
143 num_dests
= cupsGetDests(&dests
);
145 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) == NULL
)
150 _cupsLangPuts(stderr
, _("lpoptions: No printers\n"));
157 case 'o' : /* -o option[=value] */
161 num_dests
= cupsGetDests(&dests
);
163 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) == NULL
)
168 _cupsLangPuts(stderr
, _("lpoptions: No printers\n"));
172 for (j
= 0; j
< dest
->num_options
; j
++)
173 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
174 num_options
= cupsAddOption(dest
->options
[j
].name
,
175 dest
->options
[j
].value
,
176 num_options
, &options
);
180 num_options
= cupsParseOptions(argv
[i
] + 2, num_options
, &options
);
187 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
193 case 'p' : /* -p printer */
195 printer
= argv
[i
] + 2;
205 if ((instance
= strrchr(printer
, '/')) != NULL
)
209 num_dests
= cupsGetDests(&dests
);
211 if ((dest
= cupsGetDest(printer
, instance
, num_dests
, dests
)) == NULL
)
213 num_dests
= cupsAddDest(printer
, instance
, num_dests
, &dests
);
214 dest
= cupsGetDest(printer
, instance
, num_dests
, dests
);
218 _cupsLangPrintf(stderr
,
219 _("lpoptions: Unable to add printer or "
226 for (j
= 0; j
< dest
->num_options
; j
++)
227 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
228 num_options
= cupsAddOption(dest
->options
[j
].name
,
229 dest
->options
[j
].value
,
230 num_options
, &options
);
233 case 'r' : /* -r option (remove) */
237 num_dests
= cupsGetDests(&dests
);
239 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) == NULL
)
244 _cupsLangPuts(stderr
, _("lpoptions: No printers\n"));
248 for (j
= 0; j
< dest
->num_options
; j
++)
249 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
250 num_options
= cupsAddOption(dest
->options
[j
].name
,
251 dest
->options
[j
].value
,
252 num_options
, &options
);
256 option
= argv
[i
] + 2;
266 for (j
= 0; j
< num_options
; j
++)
267 if (!strcasecmp(options
[j
].name
, option
))
270 * Remove this option...
276 memcpy(options
+ j
, options
+ j
+ 1,
277 sizeof(cups_option_t
) * (num_options
- j
));
284 case 'x' : /* -x printer */
286 printer
= argv
[i
] + 2;
296 if ((instance
= strrchr(printer
, '/')) != NULL
)
300 num_dests
= cupsGetDests(&dests
);
302 if ((dest
= cupsGetDest(printer
, instance
, num_dests
, dests
)) != NULL
)
304 cupsFreeOptions(dest
->num_options
, dest
->options
);
307 * If we are "deleting" the default printer, then just set the
308 * number of options to 0; if it is also the system default
309 * then cupsSetDests() will remove it for us...
312 if (dest
->is_default
)
314 dest
->num_options
= 0;
315 dest
->options
= NULL
;
323 memcpy(dest
, dest
+ 1, (num_dests
- j
) * sizeof(cups_dest_t
));
327 cupsSetDests(num_dests
, dests
);
340 num_dests
= cupsGetDests(&dests
);
344 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) != NULL
)
346 for (j
= 0; j
< dest
->num_options
; j
++)
347 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
348 num_options
= cupsAddOption(dest
->options
[j
].name
,
349 dest
->options
[j
].value
,
350 num_options
, &options
);
360 * Set printer options...
363 cupsFreeOptions(dest
->num_options
, dest
->options
);
365 dest
->num_options
= num_options
;
366 dest
->options
= options
;
368 cupsSetDests(num_dests
, dests
);
370 else if (changes
== 0)
372 num_options
= dest
->num_options
;
373 options
= dest
->options
;
375 for (i
= 0; i
< num_options
; i
++)
378 _cupsLangPuts(stdout
, " ");
380 if (!options
[i
].value
[0])
381 _cupsLangPrintf(stdout
, "%s", options
[i
].name
);
382 else if (strchr(options
[i
].value
, ' ') != NULL
||
383 strchr(options
[i
].value
, '\t') != NULL
)
384 _cupsLangPrintf(stdout
, "%s=\'%s\'", options
[i
].name
,
387 _cupsLangPrintf(stdout
, "%s=%s", options
[i
].name
,
391 _cupsLangPuts(stdout
, "\n");
398 * 'list_group()' - List printer-specific options from the PPD group.
402 list_group(ppd_file_t
*ppd
, /* I - PPD file */
403 ppd_group_t
*group
) /* I - Group to show */
405 int i
, j
; /* Looping vars */
406 ppd_option_t
*option
; /* Current option */
407 ppd_choice_t
*choice
; /* Current choice */
408 ppd_group_t
*subgroup
; /* Current subgroup */
411 for (i
= group
->num_options
, option
= group
->options
; i
> 0; i
--, option
++)
413 if (!strcasecmp(option
->keyword
, "PageRegion"))
416 _cupsLangPrintf(stdout
, "%s/%s:", option
->keyword
, option
->text
);
418 for (j
= option
->num_choices
, choice
= option
->choices
;
421 if (!strcasecmp(choice
->choice
, "Custom"))
423 ppd_coption_t
*coption
; /* Custom option */
424 ppd_cparam_t
*cparam
; /* Custom parameter */
425 static const char * const types
[] =
426 { /* Parameter types */
438 if ((coption
= ppdFindCustomOption(ppd
, option
->keyword
)) == NULL
||
439 cupsArrayCount(coption
->params
) == 0)
440 _cupsLangPrintf(stdout
, " %sCustom", choice
->marked
? "*" : "");
441 else if (!strcasecmp(option
->keyword
, "PageSize") ||
442 !strcasecmp(option
->keyword
, "PageRegion"))
443 _cupsLangPrintf(stdout
, " %sCustom.WIDTHxHEIGHT",
444 choice
->marked
? "*" : "");
447 cparam
= (ppd_cparam_t
*)cupsArrayFirst(coption
->params
);
449 if (cupsArrayCount(coption
->params
) == 1)
450 _cupsLangPrintf(stdout
, " %sCustom.%s", choice
->marked
? "*" : "",
451 types
[cparam
->type
]);
454 const char *prefix
; /* Prefix string */
464 _cupsLangPrintf(stdout
, "%s%s=%s", prefix
, cparam
->name
,
465 types
[cparam
->type
]);
466 cparam
= (ppd_cparam_t
*)cupsArrayNext(coption
->params
);
470 _cupsLangPuts(stdout
, "}");
474 else if (choice
->marked
)
475 _cupsLangPrintf(stdout
, " *%s", choice
->choice
);
477 _cupsLangPrintf(stdout
, " %s", choice
->choice
);
479 _cupsLangPuts(stdout
, "\n");
482 for (i
= group
->num_subgroups
, subgroup
= group
->subgroups
; i
> 0; i
--, subgroup
++)
483 list_group(ppd
, subgroup
);
488 * 'list_options()' - List printer-specific options from the PPD file.
492 list_options(cups_dest_t
*dest
) /* I - Destination to list */
494 int i
; /* Looping var */
495 const char *filename
; /* PPD filename */
496 ppd_file_t
*ppd
; /* PPD data */
497 ppd_group_t
*group
; /* Current group */
500 if ((filename
= cupsGetPPD(dest
->name
)) == NULL
)
502 _cupsLangPrintf(stderr
,
503 _("lpoptions: Unable to get PPD file for %s: %s\n"),
504 dest
->name
, cupsLastErrorString());
508 if ((ppd
= ppdOpenFile(filename
)) == NULL
)
511 _cupsLangPrintf(stderr
,
512 _("lpoptions: Unable to open PPD file for %s\n"),
517 ppdMarkDefaults(ppd
);
518 cupsMarkOptions(ppd
, dest
->num_options
, dest
->options
);
520 for (i
= ppd
->num_groups
, group
= ppd
->groups
; i
> 0; i
--, group
++)
521 list_group(ppd
, group
);
529 * 'usage()' - Show program usage and exit.
535 _cupsLangPuts(stdout
,
536 _("Usage: lpoptions [-h server] [-E] -d printer\n"
537 " lpoptions [-h server] [-E] [-p printer] -l\n"
538 " lpoptions [-h server] [-E] -p printer -o "
539 "option[=value] ...\n"
540 " lpoptions [-h server] [-E] -x printer\n"));
547 * End of "$Id: lpoptions.c 7720 2008-07-11 22:46:21Z mike $".