]>
git.ipfire.org Git - thirdparty/cups.git/blob - systemv/lpoptions.c
ca148cd63f24fc7d5384db53c5c7e65c53f5819a
2 * Printer option program for CUPS.
4 * Copyright 2007-2016 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include <cups/cups-private.h>
15 #include <cups/ppd-private.h>
22 static void list_group(ppd_file_t
*ppd
, ppd_group_t
*group
);
23 static void list_options(cups_dest_t
*dest
);
24 static void usage(void) __attribute__((noreturn
));
28 * 'main()' - Main entry.
31 int /* O - Exit status */
32 main(int argc
, /* I - Number of command-line arguments */
33 char *argv
[]) /* I - Command-line arguments */
35 int i
, j
; /* Looping vars */
36 int changes
; /* Did we make changes? */
37 int num_options
; /* Number of options */
38 cups_option_t
*options
; /* Options */
39 int num_dests
; /* Number of destinations */
40 cups_dest_t
*dests
; /* Destinations */
41 cups_dest_t
*dest
; /* Current destination */
42 char *opt
, /* Option pointer */
43 *printer
, /* Printer name */
44 *instance
, /* Instance name */
45 *option
; /* Current option */
51 * Loop through the command-line arguments...
61 for (i
= 1; i
< argc
; i
++)
63 if (argv
[i
][0] == '-')
65 for (opt
= argv
[i
] + 1; *opt
; opt
++)
69 case 'd' : /* -d printer */
73 opt
+= strlen(opt
) - 1;
84 if ((instance
= strrchr(printer
, '/')) != NULL
)
88 num_dests
= cupsGetDests(&dests
);
90 if (num_dests
== 0 || !dests
|| (dest
= cupsGetDest(printer
, instance
, num_dests
, dests
)) == NULL
)
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 */
118 cupsSetServer(opt
+ 1);
119 opt
+= strlen(opt
) - 1;
127 cupsSetServer(argv
[i
]);
131 case 'E' : /* Encrypt connection */
132 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
135 case 'l' : /* -l (list options) */
139 num_dests
= cupsGetDests(&dests
);
141 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) == NULL
)
146 _cupsLangPuts(stderr
, _("lpoptions: No printers."));
153 case 'o' : /* -o option[=value] */
157 num_dests
= cupsGetDests(&dests
);
159 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) == NULL
)
164 _cupsLangPuts(stderr
, _("lpoptions: No printers."));
168 for (j
= 0; j
< dest
->num_options
; j
++)
169 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
170 num_options
= cupsAddOption(dest
->options
[j
].name
,
171 dest
->options
[j
].value
,
172 num_options
, &options
);
177 num_options
= cupsParseOptions(opt
+ 1, num_options
, &options
);
178 opt
+= strlen(opt
) - 1;
186 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
192 case 'p' : /* -p printer */
196 opt
+= strlen(opt
) - 1;
207 if ((instance
= strrchr(printer
, '/')) != NULL
)
211 num_dests
= cupsGetDests(&dests
);
213 if ((dest
= cupsGetDest(printer
, instance
, num_dests
, dests
)) == NULL
)
215 num_dests
= cupsAddDest(printer
, instance
, num_dests
, &dests
);
216 dest
= cupsGetDest(printer
, instance
, num_dests
, dests
);
220 _cupsLangPrintf(stderr
, _("lpoptions: Unable to add printer or instance: %s"), strerror(errno
));
225 for (j
= 0; j
< dest
->num_options
; j
++)
226 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
227 num_options
= cupsAddOption(dest
->options
[j
].name
,
228 dest
->options
[j
].value
,
229 num_options
, &options
);
232 case 'r' : /* -r option (remove) */
236 num_dests
= cupsGetDests(&dests
);
238 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) == NULL
)
243 _cupsLangPuts(stderr
, _("lpoptions: No printers."));
247 for (j
= 0; j
< dest
->num_options
; j
++)
248 if (cupsGetOption(dest
->options
[j
].name
, num_options
,
250 num_options
= cupsAddOption(dest
->options
[j
].name
,
251 dest
->options
[j
].value
,
252 num_options
, &options
);
258 opt
+= strlen(opt
) - 1;
269 num_options
= cupsRemoveOption(option
, num_options
, &options
);
274 case 'x' : /* -x printer */
278 opt
+= strlen(opt
) - 1;
289 if ((instance
= strrchr(printer
, '/')) != NULL
)
293 num_dests
= cupsGetDests(&dests
);
295 num_dests
= cupsRemoveDest(printer
, instance
, num_dests
, &dests
);
297 cupsSetDests(num_dests
, dests
);
314 num_dests
= cupsGetDests(&dests
);
318 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) != NULL
)
320 for (j
= 0; j
< dest
->num_options
; j
++)
321 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
322 num_options
= cupsAddOption(dest
->options
[j
].name
,
323 dest
->options
[j
].value
,
324 num_options
, &options
);
334 * Set printer options...
337 cupsFreeOptions(dest
->num_options
, dest
->options
);
339 dest
->num_options
= num_options
;
340 dest
->options
= options
;
342 cupsSetDests(num_dests
, dests
);
344 else if (changes
== 0)
346 char buffer
[10240], /* String for options */
347 *ptr
; /* Pointer into string */
349 num_options
= dest
->num_options
;
350 options
= dest
->options
;
352 for (i
= 0, ptr
= buffer
;
353 ptr
< (buffer
+ sizeof(buffer
) - 1) && i
< num_options
;
359 if (!options
[i
].value
[0])
360 strlcpy(ptr
, options
[i
].name
, sizeof(buffer
) - (size_t)(ptr
- buffer
));
361 else if (strchr(options
[i
].value
, ' ') != NULL
||
362 strchr(options
[i
].value
, '\t') != NULL
)
363 snprintf(ptr
, sizeof(buffer
) - (size_t)(ptr
- buffer
), "%s=\'%s\'", options
[i
].name
, options
[i
].value
);
365 snprintf(ptr
, sizeof(buffer
) - (size_t)(ptr
- buffer
), "%s=%s", options
[i
].name
, options
[i
].value
);
370 _cupsLangPuts(stdout
, buffer
);
377 * 'list_group()' - List printer-specific options from the PPD group.
381 list_group(ppd_file_t
*ppd
, /* I - PPD file */
382 ppd_group_t
*group
) /* I - Group to show */
384 int i
, j
; /* Looping vars */
385 ppd_option_t
*option
; /* Current option */
386 ppd_choice_t
*choice
; /* Current choice */
387 ppd_group_t
*subgroup
; /* Current subgroup */
388 char buffer
[10240], /* Option string buffer */
389 *ptr
; /* Pointer into option string */
392 for (i
= group
->num_options
, option
= group
->options
; i
> 0; i
--, option
++)
394 if (!_cups_strcasecmp(option
->keyword
, "PageRegion"))
397 snprintf(buffer
, sizeof(buffer
), "%s/%s:", option
->keyword
, option
->text
);
399 for (j
= option
->num_choices
, choice
= option
->choices
,
400 ptr
= buffer
+ strlen(buffer
);
401 j
> 0 && ptr
< (buffer
+ sizeof(buffer
) - 1);
404 if (!_cups_strcasecmp(choice
->choice
, "Custom"))
406 ppd_coption_t
*coption
; /* Custom option */
407 ppd_cparam_t
*cparam
; /* Custom parameter */
408 static const char * const types
[] =
409 { /* Parameter types */
421 if ((coption
= ppdFindCustomOption(ppd
, option
->keyword
)) == NULL
||
422 cupsArrayCount(coption
->params
) == 0)
423 snprintf(ptr
, sizeof(buffer
) - (size_t)(ptr
- buffer
), " %sCustom", choice
->marked
? "*" : "");
424 else if (!_cups_strcasecmp(option
->keyword
, "PageSize") ||
425 !_cups_strcasecmp(option
->keyword
, "PageRegion"))
426 snprintf(ptr
, sizeof(buffer
) - (size_t)(ptr
- buffer
), " %sCustom.WIDTHxHEIGHT", choice
->marked
? "*" : "");
429 cparam
= (ppd_cparam_t
*)cupsArrayFirst(coption
->params
);
431 if (cupsArrayCount(coption
->params
) == 1)
432 snprintf(ptr
, sizeof(buffer
) - (size_t)(ptr
- buffer
), " %sCustom.%s", choice
->marked
? "*" : "", types
[cparam
->type
]);
435 const char *prefix
; /* Prefix string */
445 snprintf(ptr
, sizeof(buffer
) - (size_t)(ptr
- buffer
), "%s%s=%s", prefix
, cparam
->name
, types
[cparam
->type
]);
446 cparam
= (ppd_cparam_t
*)cupsArrayNext(coption
->params
);
451 if (ptr
< (buffer
+ sizeof(buffer
) - 1))
452 strlcpy(ptr
, "}", sizeof(buffer
) - (size_t)(ptr
- buffer
));
456 else if (choice
->marked
)
457 snprintf(ptr
, sizeof(buffer
) - (size_t)(ptr
- buffer
), " *%s", choice
->choice
);
459 snprintf(ptr
, sizeof(buffer
) - (size_t)(ptr
- buffer
), " %s", choice
->choice
);
464 _cupsLangPuts(stdout
, buffer
);
467 for (i
= group
->num_subgroups
, subgroup
= group
->subgroups
; i
> 0; i
--, subgroup
++)
468 list_group(ppd
, subgroup
);
473 * 'list_options()' - List printer-specific options from the PPD file.
477 list_options(cups_dest_t
*dest
) /* I - Destination to list */
479 int i
; /* Looping var */
480 const char *filename
; /* PPD filename */
481 ppd_file_t
*ppd
; /* PPD data */
482 ppd_group_t
*group
; /* Current group */
485 if ((filename
= cupsGetPPD(dest
->name
)) == NULL
)
487 _cupsLangPrintf(stderr
, _("lpoptions: Unable to get PPD file for %s: %s"),
488 dest
->name
, cupsLastErrorString());
492 if ((ppd
= ppdOpenFile(filename
)) == NULL
)
495 _cupsLangPrintf(stderr
, _("lpoptions: Unable to open PPD file for %s."),
500 ppdMarkDefaults(ppd
);
501 cupsMarkOptions(ppd
, dest
->num_options
, dest
->options
);
503 for (i
= ppd
->num_groups
, group
= ppd
->groups
; i
> 0; i
--, group
++)
504 list_group(ppd
, group
);
512 * 'usage()' - Show program usage and exit.
518 _cupsLangPuts(stdout
,
519 _("Usage: lpoptions [-h server] [-E] -d printer\n"
520 " lpoptions [-h server] [-E] [-p printer] -l\n"
521 " lpoptions [-h server] [-E] -p printer -o "
522 "option[=value] ...\n"
523 " lpoptions [-h server] [-E] -x printer"));