]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/options.c
2 * "$Id: options.c 8181 2008-12-10 17:29:57Z mike $"
4 * Option routines for CUPS.
6 * Copyright 2007-2010 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.
24 * _cupsGet1284Values() - Get 1284 device ID keys and values.
25 * cups_compare_options() - Compare two options.
26 * cups_find_option() - Find an option using a binary search.
30 * Include necessary headers...
44 static int cups_compare_options(cups_option_t
*a
, cups_option_t
*b
);
45 static int cups_find_option(const char *name
, int num_options
,
46 cups_option_t
*option
, int prev
, int *rdiff
);
50 * 'cupsAddOption()' - Add an option to an option array.
52 * New option arrays can be initialized simply by passing 0 for the
53 * "num_options" parameter.
56 int /* O - Number of options */
57 cupsAddOption(const char *name
, /* I - Name of option */
58 const char *value
, /* I - Value of option */
59 int num_options
,/* I - Number of options */
60 cups_option_t
**options
) /* IO - Pointer to options */
62 cups_option_t
*temp
; /* Pointer to new option */
63 int insert
, /* Insertion point */
64 diff
; /* Result of search */
67 DEBUG_printf(("2cupsAddOption(name=\"%s\", value=\"%s\", num_options=%d, "
68 "options=%p)", name
, value
, num_options
, options
));
70 if (!name
|| !name
[0] || !value
|| !options
|| num_options
< 0)
72 DEBUG_printf(("3cupsAddOption: Returning %d", num_options
));
77 * Look for an existing option with the same name...
87 insert
= cups_find_option(name
, num_options
, *options
, num_options
- 1,
97 * No matching option name...
100 DEBUG_printf(("4cupsAddOption: New option inserted at index %d...",
103 if (num_options
== 0)
104 temp
= (cups_option_t
*)malloc(sizeof(cups_option_t
));
106 temp
= (cups_option_t
*)realloc(*options
, sizeof(cups_option_t
) *
111 DEBUG_puts("3cupsAddOption: Unable to expand option array, returning 0");
117 if (insert
< num_options
)
119 DEBUG_printf(("4cupsAddOption: Shifting %d options...",
120 (int)(num_options
- insert
)));
121 memmove(temp
+ insert
+ 1, temp
+ insert
,
122 (num_options
- insert
) * sizeof(cups_option_t
));
126 temp
->name
= _cupsStrAlloc(name
);
132 * Match found; free the old value...
135 DEBUG_printf(("4cupsAddOption: Option already exists at index %d...",
138 temp
= *options
+ insert
;
139 _cupsStrFree(temp
->value
);
142 temp
->value
= _cupsStrAlloc(value
);
144 DEBUG_printf(("3cupsAddOption: Returning %d", num_options
));
146 return (num_options
);
151 * 'cupsFreeOptions()' - Free all memory used by options.
156 int num_options
, /* I - Number of options */
157 cups_option_t
*options
) /* I - Pointer to options */
159 int i
; /* Looping var */
162 DEBUG_printf(("cupsFreeOptions(num_options=%d, options=%p)", num_options
,
165 if (num_options
<= 0 || !options
)
168 for (i
= 0; i
< num_options
; i
++)
170 _cupsStrFree(options
[i
].name
);
171 _cupsStrFree(options
[i
].value
);
179 * 'cupsGetOption()' - Get an option value.
182 const char * /* O - Option value or @code NULL@ */
183 cupsGetOption(const char *name
, /* I - Name of option */
184 int num_options
,/* I - Number of options */
185 cups_option_t
*options
) /* I - Options */
187 int diff
, /* Result of comparison */
188 match
; /* Matching index */
191 DEBUG_printf(("2cupsGetOption(name=\"%s\", num_options=%d, options=%p)",
192 name
, num_options
, options
));
194 if (!name
|| num_options
<= 0 || !options
)
196 DEBUG_puts("3cupsGetOption: Returning NULL");
200 match
= cups_find_option(name
, num_options
, options
, -1, &diff
);
204 DEBUG_printf(("3cupsGetOption: Returning \"%s\"", options
[match
].value
));
205 return (options
[match
].value
);
208 DEBUG_puts("3cupsGetOption: Returning NULL");
214 * 'cupsParseOptions()' - Parse options from a command-line argument.
216 * This function converts space-delimited name/value pairs according
217 * to the PAPI text option ABNF specification. Collection values
218 * ("name={a=... b=... c=...}") are stored with the curley brackets
219 * intact - use @code cupsParseOptions@ on the value to extract the
220 * collection attributes.
223 int /* O - Number of options found */
225 const char *arg
, /* I - Argument to parse */
226 int num_options
, /* I - Number of options */
227 cups_option_t
**options
) /* O - Options found */
229 char *copyarg
, /* Copy of input string */
230 *ptr
, /* Pointer into string */
231 *name
, /* Pointer to name */
232 *value
, /* Pointer to value */
233 sep
, /* Separator character */
234 quote
; /* Quote character */
237 DEBUG_printf(("cupsParseOptions(arg=\"%s\", num_options=%d, options=%p)",
238 arg
, num_options
, options
));
241 * Range check input...
246 DEBUG_printf(("1cupsParseOptions: Returning %d", num_options
));
247 return (num_options
);
250 if (!options
|| num_options
< 0)
252 DEBUG_puts("1cupsParseOptions: Returning 0");
257 * Make a copy of the argument string and then divide it up...
260 if ((copyarg
= strdup(arg
)) == NULL
)
262 DEBUG_puts("1cupsParseOptions: Unable to copy arg string");
263 DEBUG_printf(("1cupsParseOptions: Returning %d", num_options
));
264 return (num_options
);
270 * Remove surrounding {} so we can parse "{name=value ... name=value}"...
273 if ((ptr
= copyarg
+ strlen(copyarg
) - 1) > copyarg
&& *ptr
== '}')
285 * Skip leading spaces...
288 while (isspace(*ptr
& 255))
292 * Loop through the string...
298 * Get the name up to a SPACE, =, or end-of-string...
302 while (!isspace(*ptr
& 255) && *ptr
!= '=' && *ptr
)
306 * Avoid an empty name...
313 * Skip trailing spaces...
316 while (isspace(*ptr
& 255))
319 if ((sep
= *ptr
) == '=')
322 DEBUG_printf(("2cupsParseOptions: name=\"%s\"", name
));
330 if (!strncasecmp(name
, "no", 2))
331 num_options
= cupsAddOption(name
+ 2, "false", num_options
,
334 num_options
= cupsAddOption(name
, "true", num_options
, options
);
340 * Remove = and parse the value...
345 while (*ptr
&& !isspace(*ptr
& 255))
349 else if (*ptr
== '\'' || *ptr
== '\"')
352 * Quoted string constant...
356 _cups_strcpy(ptr
, ptr
+ 1);
358 while (*ptr
!= quote
&& *ptr
)
360 if (*ptr
== '\\' && ptr
[1])
361 _cups_strcpy(ptr
, ptr
+ 1);
367 _cups_strcpy(ptr
, ptr
+ 1);
369 else if (*ptr
== '{')
372 * Collection value...
377 for (depth
= 0; *ptr
; ptr
++)
381 else if (*ptr
== '}')
390 else if (*ptr
== '\\' && ptr
[1])
391 _cups_strcpy(ptr
, ptr
+ 1);
397 * Normal space-delimited string...
400 while (!isspace(*ptr
& 255) && *ptr
)
402 if (*ptr
== '\\' && ptr
[1])
403 _cups_strcpy(ptr
, ptr
+ 1);
413 DEBUG_printf(("2cupsParseOptions: value=\"%s\"", value
));
416 * Skip trailing whitespace...
419 while (isspace(*ptr
& 255))
423 * Add the string value...
426 num_options
= cupsAddOption(name
, value
, num_options
, options
);
430 * Free the copy of the argument we made and return the number of options
436 DEBUG_printf(("1cupsParseOptions: Returning %d", num_options
));
438 return (num_options
);
443 * 'cupsRemoveOption()' - Remove an option from an option array.
445 * @since CUPS 1.2/Mac OS X 10.5@
448 int /* O - New number of options */
450 const char *name
, /* I - Option name */
451 int num_options
, /* I - Current number of options */
452 cups_option_t
**options
) /* IO - Options */
454 int i
; /* Looping var */
455 cups_option_t
*option
; /* Current option */
458 DEBUG_printf(("2cupsRemoveOption(name=\"%s\", num_options=%d, options=%p)",
459 name
, num_options
, options
));
462 * Range check input...
465 if (!name
|| num_options
< 1 || !options
)
467 DEBUG_printf(("3cupsRemoveOption: Returning %d", num_options
));
468 return (num_options
);
472 * Loop for the option...
475 for (i
= num_options
, option
= *options
; i
> 0; i
--, option
++)
476 if (!strcasecmp(name
, option
->name
))
482 * Remove this option from the array...
485 DEBUG_puts("4cupsRemoveOption: Found option, removing it...");
490 _cupsStrFree(option
->name
);
491 _cupsStrFree(option
->value
);
494 memmove(option
, option
+ 1, i
* sizeof(cups_option_t
));
498 * Return the new number of options...
501 DEBUG_printf(("3cupsRemoveOption: Returning %d", num_options
));
502 return (num_options
);
507 * '_cupsGet1284Values()' - Get 1284 device ID keys and values.
509 * The returned dictionary is a CUPS option array that can be queried with
510 * cupsGetOption and freed with cupsFreeOptions.
513 int /* O - Number of key/value pairs */
515 const char *device_id
, /* I - IEEE-1284 device ID string */
516 cups_option_t
**values
) /* O - Array of key/value pairs */
518 int num_values
; /* Number of values */
519 char key
[256], /* Key string */
520 value
[256], /* Value string */
521 *ptr
; /* Pointer into key/value */
525 * Range check input...
531 if (!device_id
|| !values
)
535 * Parse the 1284 device ID value into keys and values. The format is
536 * repeating sequences of:
538 * [whitespace]key:value[whitespace];
544 while (isspace(*device_id
& 255))
550 for (ptr
= key
; *device_id
&& *device_id
!= ':'; device_id
++)
551 if (ptr
< (key
+ sizeof(key
) - 1))
557 while (ptr
> key
&& isspace(ptr
[-1] & 255))
563 while (isspace(*device_id
& 255))
569 for (ptr
= value
; *device_id
&& *device_id
!= ';'; device_id
++)
570 if (ptr
< (value
+ sizeof(value
) - 1))
576 while (ptr
> value
&& isspace(ptr
[-1] & 255))
582 num_values
= cupsAddOption(key
, value
, num_values
, values
);
590 * 'cups_compare_options()' - Compare two options.
593 static int /* O - Result of comparison */
594 cups_compare_options(cups_option_t
*a
, /* I - First option */
595 cups_option_t
*b
) /* I - Second option */
597 return (strcasecmp(a
->name
, b
->name
));
602 * 'cups_find_option()' - Find an option using a binary search.
605 static int /* O - Index of match */
607 const char *name
, /* I - Option name */
608 int num_options
, /* I - Number of options */
609 cups_option_t
*options
, /* I - Options */
610 int prev
, /* I - Previous index */
611 int *rdiff
) /* O - Difference of match */
613 int left
, /* Low mark for binary search */
614 right
, /* High mark for binary search */
615 current
, /* Current index */
616 diff
; /* Result of comparison */
617 cups_option_t key
; /* Search key */
620 DEBUG_printf(("7cups_find_option(name=\"%s\", num_options=%d, options=%p, "
621 "prev=%d, rdiff=%p)", name
, num_options
, options
, prev
,
625 for (left
= 0; left
< num_options
; left
++)
626 DEBUG_printf(("9cups_find_option: options[%d].name=\"%s\", .value=\"%s\"",
627 left
, options
[left
].name
, options
[left
].value
));
630 key
.name
= (char *)name
;
635 * Start search on either side of previous...
638 if ((diff
= cups_compare_options(&key
, options
+ prev
)) == 0 ||
639 (diff
< 0 && prev
== 0) ||
640 (diff
> 0 && prev
== (num_options
- 1)))
648 * Start with previous on right side...
657 * Start wih previous on left side...
661 right
= num_options
- 1;
667 * Start search in the middle...
671 right
= num_options
- 1;
676 current
= (left
+ right
) / 2;
677 diff
= cups_compare_options(&key
, options
+ current
);
686 while ((right
- left
) > 1);
691 * Check the last 1 or 2 elements...
694 if ((diff
= cups_compare_options(&key
, options
+ left
)) <= 0)
698 diff
= cups_compare_options(&key
, options
+ right
);
704 * Return the closest destination and the difference...
714 * End of "$Id: options.c 8181 2008-12-10 17:29:57Z mike $".