]>
git.ipfire.org Git - thirdparty/cups.git/blob - systemv/lpoptions.c
4 * Printer option program for CUPS.
6 * Copyright 2007-2014 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 * Include necessary headers...
20 #include <cups/cups-private.h>
27 static void list_group(ppd_file_t
*ppd
, ppd_group_t
*group
);
28 static void list_options(cups_dest_t
*dest
);
29 static void usage(void) __attribute__((noreturn
));
33 * 'main()' - Main entry.
36 int /* O - Exit status */
37 main(int argc
, /* I - Number of command-line arguments */
38 char *argv
[]) /* I - Command-line arguments */
40 int i
, j
; /* Looping vars */
41 int changes
; /* Did we make changes? */
42 int num_options
; /* Number of options */
43 cups_option_t
*options
; /* Options */
44 int num_dests
; /* Number of destinations */
45 cups_dest_t
*dests
; /* Destinations */
46 cups_dest_t
*dest
; /* Current destination */
47 char *printer
, /* Printer name */
48 *instance
, /* Instance name */
49 *option
; /* Current option */
55 * Loop through the command-line arguments...
65 for (i
= 1; i
< argc
; i
++)
66 if (argv
[i
][0] == '-')
70 case 'd' : /* -d printer */
72 printer
= argv
[i
] + 2;
82 if ((instance
= strrchr(printer
, '/')) != NULL
)
86 num_dests
= cupsGetDests(&dests
);
88 if (num_dests
== 0 || !dests
||
89 (dest
= cupsGetDest(printer
, instance
, num_dests
,
92 _cupsLangPuts(stderr
, _("lpoptions: Unknown printer or class."));
97 * Set the default destination...
100 for (j
= 0; j
< num_dests
; j
++)
101 dests
[j
].is_default
= 0;
103 dest
->is_default
= 1;
105 cupsSetDests(num_dests
, dests
);
107 for (j
= 0; j
< dest
->num_options
; j
++)
108 if (cupsGetOption(dest
->options
[j
].name
, num_options
,
110 num_options
= cupsAddOption(dest
->options
[j
].name
,
111 dest
->options
[j
].value
,
112 num_options
, &options
);
115 case 'h' : /* -h server */
117 cupsSetServer(argv
[i
] + 2);
124 cupsSetServer(argv
[i
]);
128 case 'E' : /* Encrypt connection */
129 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
132 case 'l' : /* -l (list options) */
136 num_dests
= cupsGetDests(&dests
);
138 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) == NULL
)
143 _cupsLangPuts(stderr
, _("lpoptions: No printers."));
150 case 'o' : /* -o option[=value] */
154 num_dests
= cupsGetDests(&dests
);
156 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) == NULL
)
161 _cupsLangPuts(stderr
, _("lpoptions: No printers."));
165 for (j
= 0; j
< dest
->num_options
; j
++)
166 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
167 num_options
= cupsAddOption(dest
->options
[j
].name
,
168 dest
->options
[j
].value
,
169 num_options
, &options
);
173 num_options
= cupsParseOptions(argv
[i
] + 2, num_options
, &options
);
180 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
186 case 'p' : /* -p printer */
188 printer
= argv
[i
] + 2;
198 if ((instance
= strrchr(printer
, '/')) != NULL
)
202 num_dests
= cupsGetDests(&dests
);
204 if ((dest
= cupsGetDest(printer
, instance
, num_dests
, dests
)) == NULL
)
206 num_dests
= cupsAddDest(printer
, instance
, num_dests
, &dests
);
207 dest
= cupsGetDest(printer
, instance
, num_dests
, dests
);
211 _cupsLangPrintf(stderr
,
212 _("lpoptions: Unable to add printer or "
219 for (j
= 0; j
< dest
->num_options
; j
++)
220 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
221 num_options
= cupsAddOption(dest
->options
[j
].name
,
222 dest
->options
[j
].value
,
223 num_options
, &options
);
226 case 'r' : /* -r option (remove) */
230 num_dests
= cupsGetDests(&dests
);
232 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) == NULL
)
237 _cupsLangPuts(stderr
, _("lpoptions: No printers."));
241 for (j
= 0; j
< dest
->num_options
; j
++)
242 if (cupsGetOption(dest
->options
[j
].name
, num_options
,
244 num_options
= cupsAddOption(dest
->options
[j
].name
,
245 dest
->options
[j
].value
,
246 num_options
, &options
);
250 option
= argv
[i
] + 2;
260 for (j
= 0; j
< num_options
; j
++)
261 if (!_cups_strcasecmp(options
[j
].name
, option
))
264 * Remove this option...
270 memmove(options
+ j
, options
+ j
+ 1, sizeof(cups_option_t
) * (size_t)(num_options
- j
));
277 case 'x' : /* -x printer */
279 printer
= argv
[i
] + 2;
289 if ((instance
= strrchr(printer
, '/')) != NULL
)
293 num_dests
= cupsGetDests(&dests
);
295 if ((dest
= cupsGetDest(printer
, instance
, num_dests
,
298 cupsFreeOptions(dest
->num_options
, dest
->options
);
301 * If we are "deleting" the default printer, then just set the
302 * number of options to 0; if it is also the system default
303 * then cupsSetDests() will remove it for us...
306 if (dest
->is_default
)
308 dest
->num_options
= 0;
309 dest
->options
= NULL
;
317 memmove(dest
, dest
+ 1, (size_t)(num_dests
- j
) * sizeof(cups_dest_t
));
321 cupsSetDests(num_dests
, dests
);
334 num_dests
= cupsGetDests(&dests
);
338 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) != NULL
)
340 for (j
= 0; j
< dest
->num_options
; j
++)
341 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
342 num_options
= cupsAddOption(dest
->options
[j
].name
,
343 dest
->options
[j
].value
,
344 num_options
, &options
);
354 * Set printer options...
357 cupsFreeOptions(dest
->num_options
, dest
->options
);
359 dest
->num_options
= num_options
;
360 dest
->options
= options
;
362 cupsSetDests(num_dests
, dests
);
364 else if (changes
== 0)
366 char buffer
[10240], /* String for options */
367 *ptr
; /* Pointer into string */
369 num_options
= dest
->num_options
;
370 options
= dest
->options
;
372 for (i
= 0, ptr
= buffer
;
373 ptr
< (buffer
+ sizeof(buffer
) - 1) && i
< num_options
;
379 if (!options
[i
].value
[0])
380 strlcpy(ptr
, options
[i
].name
, sizeof(buffer
) - (size_t)(ptr
- buffer
));
381 else if (strchr(options
[i
].value
, ' ') != NULL
||
382 strchr(options
[i
].value
, '\t') != NULL
)
383 snprintf(ptr
, sizeof(buffer
) - (size_t)(ptr
- buffer
), "%s=\'%s\'", options
[i
].name
, options
[i
].value
);
385 snprintf(ptr
, sizeof(buffer
) - (size_t)(ptr
- buffer
), "%s=%s", options
[i
].name
, options
[i
].value
);
390 _cupsLangPuts(stdout
, buffer
);
397 * 'list_group()' - List printer-specific options from the PPD group.
401 list_group(ppd_file_t
*ppd
, /* I - PPD file */
402 ppd_group_t
*group
) /* I - Group to show */
404 int i
, j
; /* Looping vars */
405 ppd_option_t
*option
; /* Current option */
406 ppd_choice_t
*choice
; /* Current choice */
407 ppd_group_t
*subgroup
; /* Current subgroup */
408 char buffer
[10240], /* Option string buffer */
409 *ptr
; /* Pointer into option string */
412 for (i
= group
->num_options
, option
= group
->options
; i
> 0; i
--, option
++)
414 if (!_cups_strcasecmp(option
->keyword
, "PageRegion"))
417 snprintf(buffer
, sizeof(buffer
), "%s/%s:", option
->keyword
, option
->text
);
419 for (j
= option
->num_choices
, choice
= option
->choices
,
420 ptr
= buffer
+ strlen(buffer
);
421 j
> 0 && ptr
< (buffer
+ sizeof(buffer
) - 1);
424 if (!_cups_strcasecmp(choice
->choice
, "Custom"))
426 ppd_coption_t
*coption
; /* Custom option */
427 ppd_cparam_t
*cparam
; /* Custom parameter */
428 static const char * const types
[] =
429 { /* Parameter types */
441 if ((coption
= ppdFindCustomOption(ppd
, option
->keyword
)) == NULL
||
442 cupsArrayCount(coption
->params
) == 0)
443 snprintf(ptr
, sizeof(buffer
) - (size_t)(ptr
- buffer
), " %sCustom", choice
->marked
? "*" : "");
444 else if (!_cups_strcasecmp(option
->keyword
, "PageSize") ||
445 !_cups_strcasecmp(option
->keyword
, "PageRegion"))
446 snprintf(ptr
, sizeof(buffer
) - (size_t)(ptr
- buffer
), " %sCustom.WIDTHxHEIGHT", choice
->marked
? "*" : "");
449 cparam
= (ppd_cparam_t
*)cupsArrayFirst(coption
->params
);
451 if (cupsArrayCount(coption
->params
) == 1)
452 snprintf(ptr
, sizeof(buffer
) - (size_t)(ptr
- buffer
), " %sCustom.%s", choice
->marked
? "*" : "", types
[cparam
->type
]);
455 const char *prefix
; /* Prefix string */
465 snprintf(ptr
, sizeof(buffer
) - (size_t)(ptr
- buffer
), "%s%s=%s", prefix
, cparam
->name
, types
[cparam
->type
]);
466 cparam
= (ppd_cparam_t
*)cupsArrayNext(coption
->params
);
471 if (ptr
< (buffer
+ sizeof(buffer
) - 1))
472 strlcpy(ptr
, "}", sizeof(buffer
) - (size_t)(ptr
- buffer
));
476 else if (choice
->marked
)
477 snprintf(ptr
, sizeof(buffer
) - (size_t)(ptr
- buffer
), " *%s", choice
->choice
);
479 snprintf(ptr
, sizeof(buffer
) - (size_t)(ptr
- buffer
), " %s", choice
->choice
);
484 _cupsLangPuts(stdout
, buffer
);
487 for (i
= group
->num_subgroups
, subgroup
= group
->subgroups
; i
> 0; i
--, subgroup
++)
488 list_group(ppd
, subgroup
);
493 * 'list_options()' - List printer-specific options from the PPD file.
497 list_options(cups_dest_t
*dest
) /* I - Destination to list */
499 int i
; /* Looping var */
500 const char *filename
; /* PPD filename */
501 ppd_file_t
*ppd
; /* PPD data */
502 ppd_group_t
*group
; /* Current group */
505 if ((filename
= cupsGetPPD(dest
->name
)) == NULL
)
507 _cupsLangPrintf(stderr
, _("lpoptions: Unable to get PPD file for %s: %s"),
508 dest
->name
, cupsLastErrorString());
512 if ((ppd
= ppdOpenFile(filename
)) == NULL
)
515 _cupsLangPrintf(stderr
, _("lpoptions: Unable to open PPD file for %s."),
520 ppdMarkDefaults(ppd
);
521 cupsMarkOptions(ppd
, dest
->num_options
, dest
->options
);
523 for (i
= ppd
->num_groups
, group
= ppd
->groups
; i
> 0; i
--, group
++)
524 list_group(ppd
, group
);
532 * 'usage()' - Show program usage and exit.
538 _cupsLangPuts(stdout
,
539 _("Usage: lpoptions [-h server] [-E] -d printer\n"
540 " lpoptions [-h server] [-E] [-p printer] -l\n"
541 " lpoptions [-h server] [-E] -p printer -o "
542 "option[=value] ...\n"
543 " lpoptions [-h server] [-E] -x printer"));