]>
git.ipfire.org Git - thirdparty/cups.git/blob - systemv/lpoptions.c
132523be66c3036718564944e18a774123be7732
2 * "$Id: lpoptions.c 6649 2007-07-11 21:46:42Z mike $"
4 * Printer option program for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 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 ((dest
= cupsGetDest(printer
, instance
, num_dests
, dests
)) == NULL
)
101 _cupsLangPuts(stderr
,
102 _("lpoptions: Unknown printer or class!\n"));
107 * Set the default destination...
110 for (j
= 0; j
< num_dests
; j
++)
111 dests
[j
].is_default
= 0;
113 dest
->is_default
= 1;
115 cupsSetDests(num_dests
, dests
);
117 for (j
= 0; j
< dest
->num_options
; j
++)
118 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
119 num_options
= cupsAddOption(dest
->options
[j
].name
,
120 dest
->options
[j
].value
,
121 num_options
, &options
);
124 case 'h' : /* -h server */
126 cupsSetServer(argv
[i
] + 2);
133 cupsSetServer(argv
[i
]);
137 case 'E' : /* Encrypt connection */
138 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
141 case 'l' : /* -l (list options) */
145 num_dests
= cupsGetDests(&dests
);
147 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) == NULL
)
152 _cupsLangPuts(stderr
, _("lpoptions: No printers!?!\n"));
159 case 'o' : /* -o option[=value] */
163 num_dests
= cupsGetDests(&dests
);
165 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) == NULL
)
170 _cupsLangPuts(stderr
, _("lpoptions: No printers!?!\n"));
174 for (j
= 0; j
< dest
->num_options
; j
++)
175 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
176 num_options
= cupsAddOption(dest
->options
[j
].name
,
177 dest
->options
[j
].value
,
178 num_options
, &options
);
182 num_options
= cupsParseOptions(argv
[i
] + 2, num_options
, &options
);
189 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
195 case 'p' : /* -p printer */
197 printer
= argv
[i
] + 2;
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
,
221 _("lpoptions: Unable to add printer or "
228 for (j
= 0; j
< dest
->num_options
; j
++)
229 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
230 num_options
= cupsAddOption(dest
->options
[j
].name
,
231 dest
->options
[j
].value
,
232 num_options
, &options
);
235 case 'r' : /* -r option (remove) */
239 num_dests
= cupsGetDests(&dests
);
241 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) == NULL
)
246 _cupsLangPuts(stderr
, _("lpoptions: No printers!?!\n"));
250 for (j
= 0; j
< dest
->num_options
; j
++)
251 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
252 num_options
= cupsAddOption(dest
->options
[j
].name
,
253 dest
->options
[j
].value
,
254 num_options
, &options
);
258 option
= argv
[i
] + 2;
268 for (j
= 0; j
< num_options
; j
++)
269 if (!strcasecmp(options
[j
].name
, option
))
272 * Remove this option...
278 memcpy(options
+ j
, options
+ j
+ 1,
279 sizeof(cups_option_t
) * (num_options
- j
));
286 case 'x' : /* -x printer */
288 printer
= argv
[i
] + 2;
298 if ((instance
= strrchr(printer
, '/')) != NULL
)
302 num_dests
= cupsGetDests(&dests
);
304 if ((dest
= cupsGetDest(printer
, instance
, num_dests
, dests
)) != NULL
)
306 cupsFreeOptions(dest
->num_options
, dest
->options
);
309 * If we are "deleting" the default printer, then just set the
310 * number of options to 0; if it is also the system default
311 * then cupsSetDests() will remove it for us...
314 if (dest
->is_default
)
316 dest
->num_options
= 0;
317 dest
->options
= NULL
;
325 memcpy(dest
, dest
+ 1, (num_dests
- j
) * sizeof(cups_dest_t
));
329 cupsSetDests(num_dests
, dests
);
342 num_dests
= cupsGetDests(&dests
);
346 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) != NULL
)
348 for (j
= 0; j
< dest
->num_options
; j
++)
349 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
350 num_options
= cupsAddOption(dest
->options
[j
].name
,
351 dest
->options
[j
].value
,
352 num_options
, &options
);
362 * Set printer options...
365 cupsFreeOptions(dest
->num_options
, dest
->options
);
367 dest
->num_options
= num_options
;
368 dest
->options
= options
;
370 cupsSetDests(num_dests
, dests
);
372 else if (changes
== 0)
374 num_options
= dest
->num_options
;
375 options
= dest
->options
;
377 for (i
= 0; i
< num_options
; i
++)
380 _cupsLangPuts(stdout
, " ");
382 if (!options
[i
].value
[0])
383 _cupsLangPrintf(stdout
, "%s", options
[i
].name
);
384 else if (strchr(options
[i
].value
, ' ') != NULL
||
385 strchr(options
[i
].value
, '\t') != NULL
)
386 _cupsLangPrintf(stdout
, "%s=\'%s\'", options
[i
].name
,
389 _cupsLangPrintf(stdout
, "%s=%s", options
[i
].name
,
393 _cupsLangPuts(stdout
, "\n");
400 * 'list_group()' - List printer-specific options from the PPD group.
404 list_group(ppd_file_t
*ppd
, /* I - PPD file */
405 ppd_group_t
*group
) /* I - Group to show */
407 int i
, j
; /* Looping vars */
408 ppd_option_t
*option
; /* Current option */
409 ppd_choice_t
*choice
; /* Current choice */
410 ppd_group_t
*subgroup
; /* Current subgroup */
413 for (i
= group
->num_options
, option
= group
->options
; i
> 0; i
--, option
++)
415 if (!strcasecmp(option
->keyword
, "PageRegion"))
418 _cupsLangPrintf(stdout
, "%s/%s:", option
->keyword
, option
->text
);
420 for (j
= option
->num_choices
, choice
= option
->choices
;
423 if (!strcasecmp(choice
->choice
, "Custom"))
425 ppd_coption_t
*coption
; /* Custom option */
426 ppd_cparam_t
*cparam
; /* Custom parameter */
427 static const char * const types
[] =
428 { /* Parameter types */
440 if ((coption
= ppdFindCustomOption(ppd
, option
->keyword
)) == NULL
||
441 cupsArrayCount(coption
->params
) == 0)
442 _cupsLangPrintf(stdout
, " %sCustom", choice
->marked
? "*" : "");
443 else if (!strcasecmp(option
->keyword
, "PageSize") ||
444 !strcasecmp(option
->keyword
, "PageRegion"))
445 _cupsLangPrintf(stdout
, " %sCustom.WIDTHxHEIGHT",
446 choice
->marked
? "*" : "");
449 cparam
= (ppd_cparam_t
*)cupsArrayFirst(coption
->params
);
451 if (cupsArrayCount(coption
->params
) == 1)
452 _cupsLangPrintf(stdout
, " %sCustom.%s", choice
->marked
? "*" : "",
453 types
[cparam
->type
]);
456 const char *prefix
; /* Prefix string */
466 _cupsLangPrintf(stdout
, "%s%s=%s", choice
->marked
? "*" : "",
467 cparam
->name
, types
[cparam
->type
]);
468 cparam
= (ppd_cparam_t
*)cupsArrayNext(coption
->params
);
472 _cupsLangPuts(stdout
, "}");
476 else if (choice
->marked
)
477 _cupsLangPrintf(stdout
, " *%s", choice
->choice
);
479 _cupsLangPrintf(stdout
, " %s", choice
->choice
);
481 _cupsLangPuts(stdout
, "\n");
484 for (i
= group
->num_subgroups
, subgroup
= group
->subgroups
; i
> 0; i
--, subgroup
++)
485 list_group(ppd
, subgroup
);
490 * 'list_options()' - List printer-specific options from the PPD file.
494 list_options(cups_dest_t
*dest
) /* I - Destination to list */
496 int i
; /* Looping var */
497 const char *filename
; /* PPD filename */
498 ppd_file_t
*ppd
; /* PPD data */
499 ppd_group_t
*group
; /* Current group */
502 if ((filename
= cupsGetPPD(dest
->name
)) == NULL
)
504 _cupsLangPrintf(stderr
,
505 _("lpoptions: Unable to get PPD file for %s: %s\n"),
506 dest
->name
, cupsLastErrorString());
510 if ((ppd
= ppdOpenFile(filename
)) == NULL
)
513 _cupsLangPrintf(stderr
,
514 _("lpoptions: Unable to open PPD file for %s!\n"),
519 ppdMarkDefaults(ppd
);
520 cupsMarkOptions(ppd
, dest
->num_options
, dest
->options
);
522 for (i
= ppd
->num_groups
, group
= ppd
->groups
; i
> 0; i
--, group
++)
523 list_group(ppd
, group
);
531 * 'usage()' - Show program usage and exit.
537 _cupsLangPuts(stdout
,
538 _("Usage: lpoptions [-h server] [-E] -d printer\n"
539 " lpoptions [-h server] [-E] [-p printer] -l\n"
540 " lpoptions [-h server] [-E] -p printer -o "
541 "option[=value] ...\n"
542 " lpoptions [-h server] [-E] -x printer\n"));
549 * End of "$Id: lpoptions.c 6649 2007-07-11 21:46:42Z mike $".