]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/options.c
55159efb493b00129357467cd2f75c3ada35bb63
2 * "$Id: options.c,v 1.33 2004/02/25 20:14:51 mike Exp $"
4 * Option routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2004 by Easy Software Products.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636-3111 USA
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * cupsAddOption() - Add an option to an option array.
29 * cupsFreeOptions() - Free all memory used by options.
30 * cupsGetOption() - Get an option value.
31 * cupsParseOptions() - Parse options from a command-line argument.
32 * cupsMarkOptions() - Mark command-line options in a PPD file.
36 * Include necessary headers...
47 * 'cupsAddOption()' - Add an option to an option array.
50 int /* O - Number of options */
51 cupsAddOption(const char *name
, /* I - Name of option */
52 const char *value
, /* I - Value of option */
53 int num_options
, /* I - Number of options */
54 cups_option_t
**options
) /* IO - Pointer to options */
56 int i
; /* Looping var */
57 cups_option_t
*temp
; /* Pointer to new option */
60 if (name
== NULL
|| !name
[0] || value
== NULL
||
61 options
== NULL
|| num_options
< 0)
65 * Look for an existing option with the same name...
68 for (i
= 0, temp
= *options
; i
< num_options
; i
++, temp
++)
69 if (strcasecmp(temp
->name
, name
) == 0)
75 * No matching option name...
79 temp
= (cups_option_t
*)malloc(sizeof(cups_option_t
));
81 temp
= (cups_option_t
*)realloc(*options
, sizeof(cups_option_t
) *
89 temp
->name
= strdup(name
);
95 * Match found; free the old value...
101 temp
->value
= strdup(value
);
103 return (num_options
);
108 * 'cupsFreeOptions()' - Free all memory used by options.
112 cupsFreeOptions(int num_options
, /* I - Number of options */
113 cups_option_t
*options
) /* I - Pointer to options */
115 int i
; /* Looping var */
118 if (num_options
<= 0 || options
== NULL
)
121 for (i
= 0; i
< num_options
; i
++)
123 free(options
[i
].name
);
124 free(options
[i
].value
);
132 * 'cupsGetOption()' - Get an option value.
135 const char * /* O - Option value or NULL */
136 cupsGetOption(const char *name
, /* I - Name of option */
137 int num_options
,/* I - Number of options */
138 cups_option_t
*options
) /* I - Options */
140 int i
; /* Looping var */
143 if (name
== NULL
|| num_options
<= 0 || options
== NULL
)
146 for (i
= 0; i
< num_options
; i
++)
147 if (strcasecmp(options
[i
].name
, name
) == 0)
148 return (options
[i
].value
);
155 * 'cupsParseOptions()' - Parse options from a command-line argument.
158 int /* O - Number of options found */
159 cupsParseOptions(const char *arg
, /* I - Argument to parse */
160 int num_options
, /* I - Number of options */
161 cups_option_t
**options
) /* O - Options found */
163 char *copyarg
, /* Copy of input string */
164 *ptr
, /* Pointer into string */
165 *name
, /* Pointer to name */
166 *value
; /* Pointer to value */
169 if (arg
== NULL
|| options
== NULL
|| num_options
< 0)
173 * Make a copy of the argument string and then divide it up...
176 copyarg
= strdup(arg
);
180 * Skip leading spaces...
183 while (isspace(*ptr
& 255))
187 * Loop through the string...
193 * Get the name up to a SPACE, =, or end-of-string...
197 while (!isspace(*ptr
& 255) && *ptr
!= '=' && *ptr
!= '\0')
201 * Avoid an empty name...
208 * Skip trailing spaces...
211 while (isspace(*ptr
& 255))
217 * Start of another option...
220 if (strncasecmp(name
, "no", 2) == 0)
221 num_options
= cupsAddOption(name
+ 2, "false", num_options
,
224 num_options
= cupsAddOption(name
, "true", num_options
, options
);
230 * Remove = and parse the value...
238 * Quoted string constant...
244 while (*ptr
!= '\'' && *ptr
!= '\0')
247 cups_strcpy(ptr
, ptr
+ 1);
255 else if (*ptr
== '\"')
258 * Double-quoted string constant...
264 while (*ptr
!= '\"' && *ptr
!= '\0')
267 cups_strcpy(ptr
, ptr
+ 1);
275 else if (*ptr
== '{')
278 * Collection value...
285 for (depth
= 1; *ptr
; ptr
++)
288 else if (*ptr
== '}')
299 else if (*ptr
== '\\')
300 cups_strcpy(ptr
, ptr
+ 1);
308 * Normal space-delimited string...
313 while (!isspace(*ptr
& 255) && *ptr
!= '\0')
316 cups_strcpy(ptr
, ptr
+ 1);
323 * Skip trailing whitespace...
326 while (isspace(*ptr
& 255))
330 * Add the string value...
333 num_options
= cupsAddOption(name
, value
, num_options
, options
);
337 * Free the copy of the argument we made and return the number of options
343 return (num_options
);
348 * 'cupsMarkOptions()' - Mark command-line options in a PPD file.
351 int /* O - 1 if conflicting */
352 cupsMarkOptions(ppd_file_t
*ppd
, /* I - PPD file */
353 int num_options
, /* I - Number of options */
354 cups_option_t
*options
) /* I - Options */
356 int i
; /* Looping var */
357 int conflict
; /* Option conflicts */
358 char *val
, /* Pointer into value */
359 *ptr
, /* Pointer into string */
360 s
[255]; /* Temporary string */
361 cups_option_t
*optptr
; /* Current option */
368 if (ppd
== NULL
|| num_options
<= 0 || options
== NULL
)
377 for (i
= num_options
, optptr
= options
; i
> 0; i
--, optptr
++)
378 if (strcasecmp(optptr
->name
, "media") == 0)
381 * Loop through the option string, separating it at commas and
382 * marking each individual option.
385 for (val
= optptr
->value
; *val
;)
388 * Extract the sub-option from the string...
391 for (ptr
= s
; *val
&& *val
!= ',' && (ptr
- s
) < (sizeof(s
) - 1);)
402 if (cupsGetOption("PageSize", num_options
, options
) == NULL
)
403 if (ppdMarkOption(ppd
, "PageSize", s
))
406 if (cupsGetOption("InputSlot", num_options
, options
) == NULL
)
407 if (ppdMarkOption(ppd
, "InputSlot", s
))
410 if (cupsGetOption("MediaType", num_options
, options
) == NULL
)
411 if (ppdMarkOption(ppd
, "MediaType", s
))
414 if (cupsGetOption("EFMediaQualityMode", num_options
, options
) == NULL
)
415 if (ppdMarkOption(ppd
, "EFMediaQualityMode", s
)) /* EFI */
418 if (strcasecmp(s
, "manual") == 0 &&
419 cupsGetOption("ManualFeed", num_options
, options
) == NULL
)
420 if (ppdMarkOption(ppd
, "ManualFeed", "True"))
424 else if (strcasecmp(optptr
->name
, "sides") == 0)
426 if (cupsGetOption("Duplex", num_options
, options
) != NULL
||
427 cupsGetOption("JCLDuplex", num_options
, options
) != NULL
||
428 cupsGetOption("EFDuplex", num_options
, options
) != NULL
||
429 cupsGetOption("KD03Duplex", num_options
, options
) != NULL
)
432 * Don't override the PPD option with the IPP attribute...
437 else if (strcasecmp(optptr
->value
, "one-sided") == 0)
439 if (ppdMarkOption(ppd
, "Duplex", "None"))
441 if (ppdMarkOption(ppd
, "JCLDuplex", "None")) /* Samsung */
443 if (ppdMarkOption(ppd
, "EFDuplex", "None")) /* EFI */
445 if (ppdMarkOption(ppd
, "KD03Duplex", "None")) /* Kodak */
448 else if (strcasecmp(optptr
->value
, "two-sided-long-edge") == 0)
450 if (ppdMarkOption(ppd
, "Duplex", "DuplexNoTumble"))
452 if (ppdMarkOption(ppd
, "JCLDuplex", "DuplexNoTumble")) /* Samsung */
454 if (ppdMarkOption(ppd
, "EFDuplex", "DuplexNoTumble")) /* EFI */
456 if (ppdMarkOption(ppd
, "KD03Duplex", "DuplexNoTumble")) /* Kodak */
459 else if (strcasecmp(optptr
->value
, "two-sided-short-edge") == 0)
461 if (ppdMarkOption(ppd
, "Duplex", "DuplexTumble"))
463 if (ppdMarkOption(ppd
, "JCLDuplex", "DuplexTumble")) /* Samsung */
465 if (ppdMarkOption(ppd
, "EFDuplex", "DuplexTumble")) /* EFI */
467 if (ppdMarkOption(ppd
, "KD03Duplex", "DuplexTumble")) /* Kodak */
471 else if (strcasecmp(optptr
->name
, "resolution") == 0 ||
472 strcasecmp(optptr
->name
, "printer-resolution") == 0)
474 if (ppdMarkOption(ppd
, "Resolution", optptr
->value
))
476 if (ppdMarkOption(ppd
, "SetResolution", optptr
->value
))
477 /* Calcomp, Linotype, QMS, Summagraphics, Tektronix, Varityper */
479 if (ppdMarkOption(ppd
, "JCLResolution", optptr
->value
)) /* HP */
481 if (ppdMarkOption(ppd
, "CNRes_PGP", optptr
->value
)) /* Canon */
484 else if (strcasecmp(optptr
->name
, "output-bin") == 0)
486 if (cupsGetOption("OutputBin", num_options
, options
) == NULL
)
487 if (ppdMarkOption(ppd
, "OutputBin", optptr
->value
))
490 else if (ppdMarkOption(ppd
, optptr
->name
, optptr
->value
))
498 * End of "$Id: options.c,v 1.33 2004/02/25 20:14:51 mike Exp $".