]>
git.ipfire.org Git - thirdparty/cups.git/blob - systemv/lpoptions.c
2 * Printer option program for CUPS.
4 * Copyright © 2007-2018 by Apple Inc.
5 * Copyright © 1997-2006 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
12 * Include necessary headers...
15 #include <cups/cups-private.h>
16 #include <cups/ppd-private.h>
23 static void list_group(ppd_file_t
*ppd
, ppd_group_t
*group
);
24 static void list_options(cups_dest_t
*dest
);
25 static void usage(void) _CUPS_NORETURN
;
29 * 'main()' - Main entry.
32 int /* O - Exit status */
33 main(int argc
, /* I - Number of command-line arguments */
34 char *argv
[]) /* I - Command-line arguments */
36 int i
, j
; /* Looping vars */
37 int changes
; /* Did we make changes? */
38 int num_options
; /* Number of options */
39 cups_option_t
*options
; /* Options */
40 int num_dests
; /* Number of destinations */
41 cups_dest_t
*dests
; /* Destinations */
42 cups_dest_t
*dest
; /* Current destination */
43 char *opt
, /* Option pointer */
44 *printer
, /* Printer name */
45 *instance
, /* Instance name */
46 *option
; /* Current option */
52 * Loop through the command-line arguments...
62 for (i
= 1; i
< argc
; i
++)
64 if (argv
[i
][0] == '-')
66 for (opt
= argv
[i
] + 1; *opt
; opt
++)
70 case 'd' : /* -d printer */
74 opt
+= strlen(opt
) - 1;
85 if ((instance
= strrchr(printer
, '/')) != NULL
)
89 num_dests
= cupsGetDests(&dests
);
91 if (num_dests
== 0 || !dests
|| (dest
= cupsGetDest(printer
, instance
, num_dests
, dests
)) == NULL
)
93 _cupsLangPuts(stderr
, _("lpoptions: Unknown printer or class."));
98 * Set the default destination...
101 for (j
= 0; j
< num_dests
; j
++)
102 dests
[j
].is_default
= 0;
104 dest
->is_default
= 1;
106 cupsSetDests(num_dests
, dests
);
108 for (j
= 0; j
< dest
->num_options
; j
++)
109 if (cupsGetOption(dest
->options
[j
].name
, num_options
,
111 num_options
= cupsAddOption(dest
->options
[j
].name
,
112 dest
->options
[j
].value
,
113 num_options
, &options
);
116 case 'h' : /* -h server */
119 cupsSetServer(opt
+ 1);
120 opt
+= strlen(opt
) - 1;
128 cupsSetServer(argv
[i
]);
132 case 'E' : /* Encrypt connection */
133 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
136 case 'l' : /* -l (list options) */
140 num_dests
= cupsGetDests(&dests
);
142 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) == NULL
)
147 _cupsLangPuts(stderr
, _("lpoptions: No printers."));
154 case 'o' : /* -o option[=value] */
158 num_dests
= cupsGetDests(&dests
);
160 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) == NULL
)
165 _cupsLangPuts(stderr
, _("lpoptions: No printers."));
169 for (j
= 0; j
< dest
->num_options
; j
++)
170 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
171 num_options
= cupsAddOption(dest
->options
[j
].name
,
172 dest
->options
[j
].value
,
173 num_options
, &options
);
178 num_options
= cupsParseOptions(opt
+ 1, num_options
, &options
);
179 opt
+= strlen(opt
) - 1;
187 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
193 case 'p' : /* -p printer */
197 opt
+= strlen(opt
) - 1;
208 if ((instance
= strrchr(printer
, '/')) != NULL
)
212 num_dests
= cupsGetDests(&dests
);
214 if ((dest
= cupsGetDest(printer
, instance
, num_dests
, dests
)) == NULL
)
216 num_dests
= cupsAddDest(printer
, instance
, num_dests
, &dests
);
217 dest
= cupsGetDest(printer
, instance
, num_dests
, dests
);
221 _cupsLangPrintf(stderr
, _("lpoptions: Unable to add printer or instance: %s"), strerror(errno
));
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."));
248 for (j
= 0; j
< dest
->num_options
; j
++)
249 if (cupsGetOption(dest
->options
[j
].name
, num_options
,
251 num_options
= cupsAddOption(dest
->options
[j
].name
,
252 dest
->options
[j
].value
,
253 num_options
, &options
);
259 opt
+= strlen(opt
) - 1;
270 num_options
= cupsRemoveOption(option
, num_options
, &options
);
275 case 'x' : /* -x printer */
279 opt
+= strlen(opt
) - 1;
290 if ((instance
= strrchr(printer
, '/')) != NULL
)
294 num_dests
= cupsGetDests(&dests
);
296 num_dests
= cupsRemoveDest(printer
, instance
, num_dests
, &dests
);
298 cupsSetDests(num_dests
, dests
);
315 num_dests
= cupsGetDests(&dests
);
319 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) != NULL
)
321 for (j
= 0; j
< dest
->num_options
; j
++)
322 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
323 num_options
= cupsAddOption(dest
->options
[j
].name
,
324 dest
->options
[j
].value
,
325 num_options
, &options
);
335 * Set printer options...
338 cupsFreeOptions(dest
->num_options
, dest
->options
);
340 dest
->num_options
= num_options
;
341 dest
->options
= options
;
343 cupsSetDests(num_dests
, dests
);
345 else if (changes
== 0)
347 char buffer
[10240], /* String for options */
348 *ptr
; /* Pointer into string */
350 num_options
= dest
->num_options
;
351 options
= dest
->options
;
353 for (i
= 0, ptr
= buffer
;
354 ptr
< (buffer
+ sizeof(buffer
) - 1) && i
< num_options
;
360 if (!options
[i
].value
[0])
361 strlcpy(ptr
, options
[i
].name
, sizeof(buffer
) - (size_t)(ptr
- buffer
));
362 else if (strchr(options
[i
].value
, ' ') != NULL
||
363 strchr(options
[i
].value
, '\t') != NULL
)
364 snprintf(ptr
, sizeof(buffer
) - (size_t)(ptr
- buffer
), "%s=\'%s\'", options
[i
].name
, options
[i
].value
);
366 snprintf(ptr
, sizeof(buffer
) - (size_t)(ptr
- buffer
), "%s=%s", options
[i
].name
, options
[i
].value
);
371 _cupsLangPuts(stdout
, buffer
);
378 * 'list_group()' - List printer-specific options from the PPD group.
382 list_group(ppd_file_t
*ppd
, /* I - PPD file */
383 ppd_group_t
*group
) /* I - Group to show */
385 int i
, j
; /* Looping vars */
386 ppd_option_t
*option
; /* Current option */
387 ppd_choice_t
*choice
; /* Current choice */
388 ppd_group_t
*subgroup
; /* Current subgroup */
389 char buffer
[10240], /* Option string buffer */
390 *ptr
; /* Pointer into option string */
393 for (i
= group
->num_options
, option
= group
->options
; i
> 0; i
--, option
++)
395 if (!_cups_strcasecmp(option
->keyword
, "PageRegion"))
398 snprintf(buffer
, sizeof(buffer
), "%s/%s:", option
->keyword
, option
->text
);
400 for (j
= option
->num_choices
, choice
= option
->choices
,
401 ptr
= buffer
+ strlen(buffer
);
402 j
> 0 && ptr
< (buffer
+ sizeof(buffer
) - 1);
405 if (!_cups_strcasecmp(choice
->choice
, "Custom"))
407 ppd_coption_t
*coption
; /* Custom option */
408 ppd_cparam_t
*cparam
; /* Custom parameter */
409 static const char * const types
[] =
410 { /* Parameter types */
422 if ((coption
= ppdFindCustomOption(ppd
, option
->keyword
)) == NULL
||
423 cupsArrayCount(coption
->params
) == 0)
424 snprintf(ptr
, sizeof(buffer
) - (size_t)(ptr
- buffer
), " %sCustom", choice
->marked
? "*" : "");
425 else if (!_cups_strcasecmp(option
->keyword
, "PageSize") ||
426 !_cups_strcasecmp(option
->keyword
, "PageRegion"))
427 snprintf(ptr
, sizeof(buffer
) - (size_t)(ptr
- buffer
), " %sCustom.WIDTHxHEIGHT", choice
->marked
? "*" : "");
430 cparam
= (ppd_cparam_t
*)cupsArrayFirst(coption
->params
);
432 if (cupsArrayCount(coption
->params
) == 1)
433 snprintf(ptr
, sizeof(buffer
) - (size_t)(ptr
- buffer
), " %sCustom.%s", choice
->marked
? "*" : "", types
[cparam
->type
]);
436 const char *prefix
; /* Prefix string */
446 snprintf(ptr
, sizeof(buffer
) - (size_t)(ptr
- buffer
), "%s%s=%s", prefix
, cparam
->name
, types
[cparam
->type
]);
447 cparam
= (ppd_cparam_t
*)cupsArrayNext(coption
->params
);
452 if (ptr
< (buffer
+ sizeof(buffer
) - 1))
453 strlcpy(ptr
, "}", sizeof(buffer
) - (size_t)(ptr
- buffer
));
457 else if (choice
->marked
)
458 snprintf(ptr
, sizeof(buffer
) - (size_t)(ptr
- buffer
), " *%s", choice
->choice
);
460 snprintf(ptr
, sizeof(buffer
) - (size_t)(ptr
- buffer
), " %s", choice
->choice
);
465 _cupsLangPuts(stdout
, buffer
);
468 for (i
= group
->num_subgroups
, subgroup
= group
->subgroups
; i
> 0; i
--, subgroup
++)
469 list_group(ppd
, subgroup
);
474 * 'list_options()' - List printer-specific options from the PPD file.
478 list_options(cups_dest_t
*dest
) /* I - Destination to list */
480 http_t
*http
; /* Connection to destination */
481 char resource
[1024]; /* Resource path */
482 int i
; /* Looping var */
483 const char *filename
; /* PPD filename */
484 ppd_file_t
*ppd
; /* PPD data */
485 ppd_group_t
*group
; /* Current group */
488 if ((http
= cupsConnectDest(dest
, CUPS_DEST_FLAGS_NONE
, 30000, NULL
, resource
, sizeof(resource
), NULL
, NULL
)) == NULL
)
490 _cupsLangPrintf(stderr
, _("lpoptions: Unable to get PPD file for %s: %s"),
491 dest
->name
, cupsLastErrorString());
495 if ((filename
= cupsGetPPD2(http
, dest
->name
)) == NULL
)
499 _cupsLangPrintf(stderr
, _("lpoptions: Unable to get PPD file for %s: %s"),
500 dest
->name
, cupsLastErrorString());
506 if ((ppd
= ppdOpenFile(filename
)) == NULL
)
509 _cupsLangPrintf(stderr
, _("lpoptions: Unable to open PPD file for %s."),
514 ppdMarkDefaults(ppd
);
515 cupsMarkOptions(ppd
, dest
->num_options
, dest
->options
);
517 for (i
= ppd
->num_groups
, group
= ppd
->groups
; i
> 0; i
--, group
++)
518 list_group(ppd
, group
);
526 * 'usage()' - Show program usage and exit.
532 _cupsLangPuts(stdout
,
533 _("Usage: lpoptions [-h server] [-E] -d printer\n"
534 " lpoptions [-h server] [-E] [-p printer] -l\n"
535 " lpoptions [-h server] [-E] -p printer -o "
536 "option[=value] ...\n"
537 " lpoptions [-h server] [-E] -x printer"));