]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/options.c
4 * Option routines for CUPS.
6 * Copyright 2007-2012 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...
33 #include "cups-private.h"
40 static int cups_compare_options(cups_option_t
*a
, cups_option_t
*b
);
41 static int cups_find_option(const char *name
, int num_options
,
42 cups_option_t
*option
, int prev
, int *rdiff
);
46 * 'cupsAddOption()' - Add an option to an option array.
48 * New option arrays can be initialized simply by passing 0 for the
49 * "num_options" parameter.
52 int /* O - Number of options */
53 cupsAddOption(const char *name
, /* I - Name of option */
54 const char *value
, /* I - Value of option */
55 int num_options
,/* I - Number of options */
56 cups_option_t
**options
) /* IO - Pointer to options */
58 cups_option_t
*temp
; /* Pointer to new option */
59 int insert
, /* Insertion point */
60 diff
; /* Result of search */
63 DEBUG_printf(("2cupsAddOption(name=\"%s\", value=\"%s\", num_options=%d, "
64 "options=%p)", name
, value
, num_options
, options
));
66 if (!name
|| !name
[0] || !value
|| !options
|| num_options
< 0)
68 DEBUG_printf(("3cupsAddOption: Returning %d", num_options
));
73 * Look for an existing option with the same name...
83 insert
= cups_find_option(name
, num_options
, *options
, num_options
- 1,
93 * No matching option name...
96 DEBUG_printf(("4cupsAddOption: New option inserted at index %d...",
100 temp
= (cups_option_t
*)malloc(sizeof(cups_option_t
));
102 temp
= (cups_option_t
*)realloc(*options
, sizeof(cups_option_t
) *
107 DEBUG_puts("3cupsAddOption: Unable to expand option array, returning 0");
113 if (insert
< num_options
)
115 DEBUG_printf(("4cupsAddOption: Shifting %d options...",
116 (int)(num_options
- insert
)));
117 memmove(temp
+ insert
+ 1, temp
+ insert
,
118 (num_options
- insert
) * sizeof(cups_option_t
));
122 temp
->name
= _cupsStrAlloc(name
);
128 * Match found; free the old value...
131 DEBUG_printf(("4cupsAddOption: Option already exists at index %d...",
134 temp
= *options
+ insert
;
135 _cupsStrFree(temp
->value
);
138 temp
->value
= _cupsStrAlloc(value
);
140 DEBUG_printf(("3cupsAddOption: Returning %d", num_options
));
142 return (num_options
);
147 * 'cupsFreeOptions()' - Free all memory used by options.
152 int num_options
, /* I - Number of options */
153 cups_option_t
*options
) /* I - Pointer to options */
155 int i
; /* Looping var */
158 DEBUG_printf(("cupsFreeOptions(num_options=%d, options=%p)", num_options
,
161 if (num_options
<= 0 || !options
)
164 for (i
= 0; i
< num_options
; i
++)
166 _cupsStrFree(options
[i
].name
);
167 _cupsStrFree(options
[i
].value
);
175 * 'cupsGetOption()' - Get an option value.
178 const char * /* O - Option value or @code NULL@ */
179 cupsGetOption(const char *name
, /* I - Name of option */
180 int num_options
,/* I - Number of options */
181 cups_option_t
*options
) /* I - Options */
183 int diff
, /* Result of comparison */
184 match
; /* Matching index */
187 DEBUG_printf(("2cupsGetOption(name=\"%s\", num_options=%d, options=%p)",
188 name
, num_options
, options
));
190 if (!name
|| num_options
<= 0 || !options
)
192 DEBUG_puts("3cupsGetOption: Returning NULL");
196 match
= cups_find_option(name
, num_options
, options
, -1, &diff
);
200 DEBUG_printf(("3cupsGetOption: Returning \"%s\"", options
[match
].value
));
201 return (options
[match
].value
);
204 DEBUG_puts("3cupsGetOption: Returning NULL");
210 * 'cupsParseOptions()' - Parse options from a command-line argument.
212 * This function converts space-delimited name/value pairs according
213 * to the PAPI text option ABNF specification. Collection values
214 * ("name={a=... b=... c=...}") are stored with the curley brackets
215 * intact - use @code cupsParseOptions@ on the value to extract the
216 * collection attributes.
219 int /* O - Number of options found */
221 const char *arg
, /* I - Argument to parse */
222 int num_options
, /* I - Number of options */
223 cups_option_t
**options
) /* O - Options found */
225 char *copyarg
, /* Copy of input string */
226 *ptr
, /* Pointer into string */
227 *name
, /* Pointer to name */
228 *value
, /* Pointer to value */
229 sep
, /* Separator character */
230 quote
; /* Quote character */
233 DEBUG_printf(("cupsParseOptions(arg=\"%s\", num_options=%d, options=%p)",
234 arg
, num_options
, options
));
237 * Range check input...
242 DEBUG_printf(("1cupsParseOptions: Returning %d", num_options
));
243 return (num_options
);
246 if (!options
|| num_options
< 0)
248 DEBUG_puts("1cupsParseOptions: Returning 0");
253 * Make a copy of the argument string and then divide it up...
256 if ((copyarg
= strdup(arg
)) == NULL
)
258 DEBUG_puts("1cupsParseOptions: Unable to copy arg string");
259 DEBUG_printf(("1cupsParseOptions: Returning %d", num_options
));
260 return (num_options
);
266 * Remove surrounding {} so we can parse "{name=value ... name=value}"...
269 if ((ptr
= copyarg
+ strlen(copyarg
) - 1) > copyarg
&& *ptr
== '}')
281 * Skip leading spaces...
284 while (_cups_isspace(*ptr
))
288 * Loop through the string...
294 * Get the name up to a SPACE, =, or end-of-string...
298 while (!strchr("\f\n\r\t\v =", *ptr
) && *ptr
)
302 * Avoid an empty name...
309 * Skip trailing spaces...
312 while (_cups_isspace(*ptr
))
315 if ((sep
= *ptr
) == '=')
318 DEBUG_printf(("2cupsParseOptions: name=\"%s\"", name
));
326 if (!_cups_strncasecmp(name
, "no", 2))
327 num_options
= cupsAddOption(name
+ 2, "false", num_options
,
330 num_options
= cupsAddOption(name
, "true", num_options
, options
);
336 * Remove = and parse the value...
341 while (*ptr
&& !_cups_isspace(*ptr
))
345 else if (*ptr
== '\'' || *ptr
== '\"')
348 * Quoted string constant...
352 _cups_strcpy(ptr
, ptr
+ 1);
354 while (*ptr
!= quote
&& *ptr
)
356 if (*ptr
== '\\' && ptr
[1])
357 _cups_strcpy(ptr
, ptr
+ 1);
363 _cups_strcpy(ptr
, ptr
+ 1);
365 else if (*ptr
== '{')
368 * Collection value...
373 for (depth
= 0; *ptr
; ptr
++)
377 else if (*ptr
== '}')
386 else if (*ptr
== '\\' && ptr
[1])
387 _cups_strcpy(ptr
, ptr
+ 1);
393 * Normal space-delimited string...
396 while (*ptr
&& !_cups_isspace(*ptr
))
398 if (*ptr
== '\\' && ptr
[1])
399 _cups_strcpy(ptr
, ptr
+ 1);
409 DEBUG_printf(("2cupsParseOptions: value=\"%s\"", value
));
412 * Skip trailing whitespace...
415 while (_cups_isspace(*ptr
))
419 * Add the string value...
422 num_options
= cupsAddOption(name
, value
, num_options
, options
);
426 * Free the copy of the argument we made and return the number of options
432 DEBUG_printf(("1cupsParseOptions: Returning %d", num_options
));
434 return (num_options
);
439 * 'cupsRemoveOption()' - Remove an option from an option array.
441 * @since CUPS 1.2/OS X 10.5@
444 int /* O - New number of options */
446 const char *name
, /* I - Option name */
447 int num_options
, /* I - Current number of options */
448 cups_option_t
**options
) /* IO - Options */
450 int i
; /* Looping var */
451 cups_option_t
*option
; /* Current option */
454 DEBUG_printf(("2cupsRemoveOption(name=\"%s\", num_options=%d, options=%p)",
455 name
, num_options
, options
));
458 * Range check input...
461 if (!name
|| num_options
< 1 || !options
)
463 DEBUG_printf(("3cupsRemoveOption: Returning %d", num_options
));
464 return (num_options
);
468 * Loop for the option...
471 for (i
= num_options
, option
= *options
; i
> 0; i
--, option
++)
472 if (!_cups_strcasecmp(name
, option
->name
))
478 * Remove this option from the array...
481 DEBUG_puts("4cupsRemoveOption: Found option, removing it...");
486 _cupsStrFree(option
->name
);
487 _cupsStrFree(option
->value
);
490 memmove(option
, option
+ 1, i
* sizeof(cups_option_t
));
494 * Return the new number of options...
497 DEBUG_printf(("3cupsRemoveOption: Returning %d", num_options
));
498 return (num_options
);
503 * '_cupsGet1284Values()' - Get 1284 device ID keys and values.
505 * The returned dictionary is a CUPS option array that can be queried with
506 * cupsGetOption and freed with cupsFreeOptions.
509 int /* O - Number of key/value pairs */
511 const char *device_id
, /* I - IEEE-1284 device ID string */
512 cups_option_t
**values
) /* O - Array of key/value pairs */
514 int num_values
; /* Number of values */
515 char key
[256], /* Key string */
516 value
[256], /* Value string */
517 *ptr
; /* Pointer into key/value */
521 * Range check input...
527 if (!device_id
|| !values
)
531 * Parse the 1284 device ID value into keys and values. The format is
532 * repeating sequences of:
534 * [whitespace]key:value[whitespace];
540 while (_cups_isspace(*device_id
))
546 for (ptr
= key
; *device_id
&& *device_id
!= ':'; device_id
++)
547 if (ptr
< (key
+ sizeof(key
) - 1))
553 while (ptr
> key
&& _cups_isspace(ptr
[-1]))
559 while (_cups_isspace(*device_id
))
565 for (ptr
= value
; *device_id
&& *device_id
!= ';'; device_id
++)
566 if (ptr
< (value
+ sizeof(value
) - 1))
572 while (ptr
> value
&& _cups_isspace(ptr
[-1]))
578 num_values
= cupsAddOption(key
, value
, num_values
, values
);
586 * 'cups_compare_options()' - Compare two options.
589 static int /* O - Result of comparison */
590 cups_compare_options(cups_option_t
*a
, /* I - First option */
591 cups_option_t
*b
) /* I - Second option */
593 return (_cups_strcasecmp(a
->name
, b
->name
));
598 * 'cups_find_option()' - Find an option using a binary search.
601 static int /* O - Index of match */
603 const char *name
, /* I - Option name */
604 int num_options
, /* I - Number of options */
605 cups_option_t
*options
, /* I - Options */
606 int prev
, /* I - Previous index */
607 int *rdiff
) /* O - Difference of match */
609 int left
, /* Low mark for binary search */
610 right
, /* High mark for binary search */
611 current
, /* Current index */
612 diff
; /* Result of comparison */
613 cups_option_t key
; /* Search key */
616 DEBUG_printf(("7cups_find_option(name=\"%s\", num_options=%d, options=%p, "
617 "prev=%d, rdiff=%p)", name
, num_options
, options
, prev
,
621 for (left
= 0; left
< num_options
; left
++)
622 DEBUG_printf(("9cups_find_option: options[%d].name=\"%s\", .value=\"%s\"",
623 left
, options
[left
].name
, options
[left
].value
));
626 key
.name
= (char *)name
;
631 * Start search on either side of previous...
634 if ((diff
= cups_compare_options(&key
, options
+ prev
)) == 0 ||
635 (diff
< 0 && prev
== 0) ||
636 (diff
> 0 && prev
== (num_options
- 1)))
644 * Start with previous on right side...
653 * Start wih previous on left side...
657 right
= num_options
- 1;
663 * Start search in the middle...
667 right
= num_options
- 1;
672 current
= (left
+ right
) / 2;
673 diff
= cups_compare_options(&key
, options
+ current
);
682 while ((right
- left
) > 1);
687 * Check the last 1 or 2 elements...
690 if ((diff
= cups_compare_options(&key
, options
+ left
)) <= 0)
694 diff
= cups_compare_options(&key
, options
+ right
);
700 * Return the closest destination and the difference...