]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/options.c
2 * "$Id: options.c 7278 2008-01-31 01:23:09Z mike $"
4 * Option routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2007 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/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * cupsAddOption() - Add an option to an option array.
20 * cupsFreeOptions() - Free all memory used by options.
21 * cupsGetOption() - Get an option value.
22 * cupsParseOptions() - Parse options from a command-line argument.
23 * cupsRemoveOption() - Remove an option from an option array.
27 * Include necessary headers...
38 * 'cupsAddOption()' - Add an option to an option array.
40 * New option arrays can be initialized simply by passing 0 for the
41 * "num_options" parameter.
44 int /* O - Number of options */
45 cupsAddOption(const char *name
, /* I - Name of option */
46 const char *value
, /* I - Value of option */
47 int num_options
,/* I - Number of options */
48 cups_option_t
**options
) /* IO - Pointer to options */
50 int i
; /* Looping var */
51 cups_option_t
*temp
; /* Pointer to new option */
54 if (name
== NULL
|| !name
[0] || value
== NULL
||
55 options
== NULL
|| num_options
< 0)
59 * Look for an existing option with the same name...
62 for (i
= 0, temp
= *options
; i
< num_options
; i
++, temp
++)
63 if (!strcasecmp(temp
->name
, name
))
69 * No matching option name...
73 temp
= (cups_option_t
*)malloc(sizeof(cups_option_t
));
75 temp
= (cups_option_t
*)realloc(*options
, sizeof(cups_option_t
) *
83 temp
->name
= _cupsStrAlloc(name
);
89 * Match found; free the old value...
92 _cupsStrFree(temp
->value
);
95 temp
->value
= _cupsStrAlloc(value
);
102 * 'cupsFreeOptions()' - Free all memory used by options.
107 int num_options
, /* I - Number of options */
108 cups_option_t
*options
) /* I - Pointer to options */
110 int i
; /* Looping var */
113 if (num_options
<= 0 || options
== NULL
)
116 for (i
= 0; i
< num_options
; i
++)
118 _cupsStrFree(options
[i
].name
);
119 _cupsStrFree(options
[i
].value
);
127 * 'cupsGetOption()' - Get an option value.
130 const char * /* O - Option value or @code NULL@ */
131 cupsGetOption(const char *name
, /* I - Name of option */
132 int num_options
,/* I - Number of options */
133 cups_option_t
*options
) /* I - Options */
135 int i
; /* Looping var */
138 if (name
== NULL
|| num_options
<= 0 || options
== NULL
)
141 for (i
= 0; i
< num_options
; i
++)
142 if (strcasecmp(options
[i
].name
, name
) == 0)
143 return (options
[i
].value
);
150 * 'cupsParseOptions()' - Parse options from a command-line argument.
152 * This function converts space-delimited name/value pairs according
153 * to the PAPI text option ABNF specification. Collection values
154 * ("name={a=... b=... c=...}") are stored with the curley brackets
155 * intact - use @code cupsParseOptions@ on the value to extract the
156 * collection attributes.
159 int /* O - Number of options found */
161 const char *arg
, /* I - Argument to parse */
162 int num_options
, /* I - Number of options */
163 cups_option_t
**options
) /* O - Options found */
165 char *copyarg
, /* Copy of input string */
166 *ptr
, /* Pointer into string */
167 *name
, /* Pointer to name */
168 *value
, /* Pointer to value */
169 quote
; /* Quote character */
173 * Range check input...
177 return (num_options
);
179 if (!options
|| num_options
< 0)
183 * Make a copy of the argument string and then divide it up...
186 if ((copyarg
= strdup(arg
)) == NULL
)
187 return (num_options
);
192 * Skip leading spaces...
195 while (isspace(*ptr
& 255))
199 * Loop through the string...
205 * Get the name up to a SPACE, =, or end-of-string...
209 while (!isspace(*ptr
& 255) && *ptr
!= '=' && *ptr
)
213 * Avoid an empty name...
220 * Skip trailing spaces...
223 while (isspace(*ptr
& 255))
232 if (!strncasecmp(name
, "no", 2))
233 num_options
= cupsAddOption(name
+ 2, "false", num_options
,
236 num_options
= cupsAddOption(name
, "true", num_options
, options
);
242 * Remove = and parse the value...
247 if (*ptr
== '\'' || *ptr
== '\"')
250 * Quoted string constant...
256 while (*ptr
!= quote
&& *ptr
)
258 if (*ptr
== '\\' && ptr
[1])
259 _cups_strcpy(ptr
, ptr
+ 1);
267 else if (*ptr
== '{')
270 * Collection value...
277 for (depth
= 1; *ptr
; ptr
++)
280 else if (*ptr
== '}')
291 else if (*ptr
== '\\' && ptr
[1])
292 _cups_strcpy(ptr
, ptr
+ 1);
300 * Normal space-delimited string...
305 while (!isspace(*ptr
& 255) && *ptr
)
307 if (*ptr
== '\\' && ptr
[1])
308 _cups_strcpy(ptr
, ptr
+ 1);
315 * Skip trailing whitespace...
318 while (isspace(*ptr
& 255))
322 * Add the string value...
325 num_options
= cupsAddOption(name
, value
, num_options
, options
);
329 * Free the copy of the argument we made and return the number of options
335 return (num_options
);
340 * 'cupsRemoveOption()' - Remove an option from an option array.
345 int /* O - New number of options */
347 const char *name
, /* I - Option name */
348 int num_options
, /* I - Current number of options */
349 cups_option_t
**options
) /* IO - Options */
351 int i
; /* Looping var */
352 cups_option_t
*option
; /* Current option */
356 * Range check input...
359 if (!name
|| num_options
< 1 || !options
)
360 return (num_options
);
363 * Loop for the option...
366 for (i
= num_options
, option
= *options
; i
> 0; i
--, option
++)
367 if (!strcasecmp(name
, option
->name
))
373 * Remove this option from the array...
379 _cupsStrFree(option
->name
);
380 _cupsStrFree(option
->value
);
383 memmove(option
, option
+ 1, i
* sizeof(cups_option_t
));
387 * Return the new number of options...
390 return (num_options
);
395 * End of "$Id: options.c 7278 2008-01-31 01:23:09Z mike $".