]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/options.c
986ae1ca2cf9b628b737289ee282795f6c77a74f
2 * Option routines for CUPS.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers...
20 #include "cups-private.h"
27 static int cups_compare_options(cups_option_t
*a
, cups_option_t
*b
);
28 static int cups_find_option(const char *name
, int num_options
,
29 cups_option_t
*option
, int prev
, int *rdiff
);
33 * 'cupsAddOption()' - Add an option to an option array.
35 * New option arrays can be initialized simply by passing 0 for the
36 * "num_options" parameter.
39 int /* O - Number of options */
40 cupsAddOption(const char *name
, /* I - Name of option */
41 const char *value
, /* I - Value of option */
42 int num_options
,/* I - Number of options */
43 cups_option_t
**options
) /* IO - Pointer to options */
45 cups_option_t
*temp
; /* Pointer to new option */
46 int insert
, /* Insertion point */
47 diff
; /* Result of search */
50 DEBUG_printf(("2cupsAddOption(name=\"%s\", value=\"%s\", num_options=%d, options=%p)", name
, value
, num_options
, (void *)options
));
52 if (!name
|| !name
[0] || !value
|| !options
|| num_options
< 0)
54 DEBUG_printf(("3cupsAddOption: Returning %d", num_options
));
59 * Look for an existing option with the same name...
69 insert
= cups_find_option(name
, num_options
, *options
, num_options
- 1,
79 * No matching option name...
82 DEBUG_printf(("4cupsAddOption: New option inserted at index %d...",
86 temp
= (cups_option_t
*)malloc(sizeof(cups_option_t
));
88 temp
= (cups_option_t
*)realloc(*options
, sizeof(cups_option_t
) * (size_t)(num_options
+ 1));
92 DEBUG_puts("3cupsAddOption: Unable to expand option array, returning 0");
98 if (insert
< num_options
)
100 DEBUG_printf(("4cupsAddOption: Shifting %d options...",
101 (int)(num_options
- insert
)));
102 memmove(temp
+ insert
+ 1, temp
+ insert
, (size_t)(num_options
- insert
) * sizeof(cups_option_t
));
106 temp
->name
= _cupsStrAlloc(name
);
112 * Match found; free the old value...
115 DEBUG_printf(("4cupsAddOption: Option already exists at index %d...",
118 temp
= *options
+ insert
;
119 _cupsStrFree(temp
->value
);
122 temp
->value
= _cupsStrAlloc(value
);
124 DEBUG_printf(("3cupsAddOption: Returning %d", num_options
));
126 return (num_options
);
131 * 'cupsFreeOptions()' - Free all memory used by options.
136 int num_options
, /* I - Number of options */
137 cups_option_t
*options
) /* I - Pointer to options */
139 int i
; /* Looping var */
142 DEBUG_printf(("cupsFreeOptions(num_options=%d, options=%p)", num_options
, (void *)options
));
144 if (num_options
<= 0 || !options
)
147 for (i
= 0; i
< num_options
; i
++)
149 _cupsStrFree(options
[i
].name
);
150 _cupsStrFree(options
[i
].value
);
158 * 'cupsGetOption()' - Get an option value.
161 const char * /* O - Option value or @code NULL@ */
162 cupsGetOption(const char *name
, /* I - Name of option */
163 int num_options
,/* I - Number of options */
164 cups_option_t
*options
) /* I - Options */
166 int diff
, /* Result of comparison */
167 match
; /* Matching index */
170 DEBUG_printf(("2cupsGetOption(name=\"%s\", num_options=%d, options=%p)", name
, num_options
, (void *)options
));
172 if (!name
|| num_options
<= 0 || !options
)
174 DEBUG_puts("3cupsGetOption: Returning NULL");
178 match
= cups_find_option(name
, num_options
, options
, -1, &diff
);
182 DEBUG_printf(("3cupsGetOption: Returning \"%s\"", options
[match
].value
));
183 return (options
[match
].value
);
186 DEBUG_puts("3cupsGetOption: Returning NULL");
192 * 'cupsParseOptions()' - Parse options from a command-line argument.
194 * This function converts space-delimited name/value pairs according
195 * to the PAPI text option ABNF specification. Collection values
196 * ("name={a=... b=... c=...}") are stored with the curley brackets
197 * intact - use @code cupsParseOptions@ on the value to extract the
198 * collection attributes.
201 int /* O - Number of options found */
203 const char *arg
, /* I - Argument to parse */
204 int num_options
, /* I - Number of options */
205 cups_option_t
**options
) /* O - Options found */
207 char *copyarg
, /* Copy of input string */
208 *ptr
, /* Pointer into string */
209 *name
, /* Pointer to name */
210 *value
, /* Pointer to value */
211 sep
, /* Separator character */
212 quote
; /* Quote character */
215 DEBUG_printf(("cupsParseOptions(arg=\"%s\", num_options=%d, options=%p)", arg
, num_options
, (void *)options
));
218 * Range check input...
223 DEBUG_printf(("1cupsParseOptions: Returning %d", num_options
));
224 return (num_options
);
227 if (!options
|| num_options
< 0)
229 DEBUG_puts("1cupsParseOptions: Returning 0");
234 * Make a copy of the argument string and then divide it up...
237 if ((copyarg
= strdup(arg
)) == NULL
)
239 DEBUG_puts("1cupsParseOptions: Unable to copy arg string");
240 DEBUG_printf(("1cupsParseOptions: Returning %d", num_options
));
241 return (num_options
);
247 * Remove surrounding {} so we can parse "{name=value ... name=value}"...
250 if ((ptr
= copyarg
+ strlen(copyarg
) - 1) > copyarg
&& *ptr
== '}')
262 * Skip leading spaces...
265 while (_cups_isspace(*ptr
))
269 * Loop through the string...
275 * Get the name up to a SPACE, =, or end-of-string...
279 while (!strchr("\f\n\r\t\v =", *ptr
) && *ptr
)
283 * Avoid an empty name...
290 * Skip trailing spaces...
293 while (_cups_isspace(*ptr
))
296 if ((sep
= *ptr
) == '=')
299 DEBUG_printf(("2cupsParseOptions: name=\"%s\"", name
));
307 if (!_cups_strncasecmp(name
, "no", 2))
308 num_options
= cupsAddOption(name
+ 2, "false", num_options
,
311 num_options
= cupsAddOption(name
, "true", num_options
, options
);
317 * Remove = and parse the value...
322 while (*ptr
&& !_cups_isspace(*ptr
))
326 else if (*ptr
== '\'' || *ptr
== '\"')
329 * Quoted string constant...
333 _cups_strcpy(ptr
, ptr
+ 1);
335 while (*ptr
!= quote
&& *ptr
)
337 if (*ptr
== '\\' && ptr
[1])
338 _cups_strcpy(ptr
, ptr
+ 1);
344 _cups_strcpy(ptr
, ptr
+ 1);
346 else if (*ptr
== '{')
349 * Collection value...
354 for (depth
= 0; *ptr
; ptr
++)
358 else if (*ptr
== '}')
367 else if (*ptr
== '\\' && ptr
[1])
368 _cups_strcpy(ptr
, ptr
+ 1);
374 * Normal space-delimited string...
377 while (*ptr
&& !_cups_isspace(*ptr
))
379 if (*ptr
== '\\' && ptr
[1])
380 _cups_strcpy(ptr
, ptr
+ 1);
390 DEBUG_printf(("2cupsParseOptions: value=\"%s\"", value
));
393 * Skip trailing whitespace...
396 while (_cups_isspace(*ptr
))
400 * Add the string value...
403 num_options
= cupsAddOption(name
, value
, num_options
, options
);
407 * Free the copy of the argument we made and return the number of options
413 DEBUG_printf(("1cupsParseOptions: Returning %d", num_options
));
415 return (num_options
);
420 * 'cupsRemoveOption()' - Remove an option from an option array.
422 * @since CUPS 1.2/OS X 10.5@
425 int /* O - New number of options */
427 const char *name
, /* I - Option name */
428 int num_options
, /* I - Current number of options */
429 cups_option_t
**options
) /* IO - Options */
431 int i
; /* Looping var */
432 cups_option_t
*option
; /* Current option */
435 DEBUG_printf(("2cupsRemoveOption(name=\"%s\", num_options=%d, options=%p)", name
, num_options
, (void *)options
));
438 * Range check input...
441 if (!name
|| num_options
< 1 || !options
)
443 DEBUG_printf(("3cupsRemoveOption: Returning %d", num_options
));
444 return (num_options
);
448 * Loop for the option...
451 for (i
= num_options
, option
= *options
; i
> 0; i
--, option
++)
452 if (!_cups_strcasecmp(name
, option
->name
))
458 * Remove this option from the array...
461 DEBUG_puts("4cupsRemoveOption: Found option, removing it...");
466 _cupsStrFree(option
->name
);
467 _cupsStrFree(option
->value
);
470 memmove(option
, option
+ 1, (size_t)i
* sizeof(cups_option_t
));
474 * Return the new number of options...
477 DEBUG_printf(("3cupsRemoveOption: Returning %d", num_options
));
478 return (num_options
);
483 * '_cupsGet1284Values()' - Get 1284 device ID keys and values.
485 * The returned dictionary is a CUPS option array that can be queried with
486 * cupsGetOption and freed with cupsFreeOptions.
489 int /* O - Number of key/value pairs */
491 const char *device_id
, /* I - IEEE-1284 device ID string */
492 cups_option_t
**values
) /* O - Array of key/value pairs */
494 int num_values
; /* Number of values */
495 char key
[256], /* Key string */
496 value
[256], /* Value string */
497 *ptr
; /* Pointer into key/value */
501 * Range check input...
507 if (!device_id
|| !values
)
511 * Parse the 1284 device ID value into keys and values. The format is
512 * repeating sequences of:
514 * [whitespace]key:value[whitespace];
520 while (_cups_isspace(*device_id
))
526 for (ptr
= key
; *device_id
&& *device_id
!= ':'; device_id
++)
527 if (ptr
< (key
+ sizeof(key
) - 1))
533 while (ptr
> key
&& _cups_isspace(ptr
[-1]))
539 while (_cups_isspace(*device_id
))
545 for (ptr
= value
; *device_id
&& *device_id
!= ';'; device_id
++)
546 if (ptr
< (value
+ sizeof(value
) - 1))
552 while (ptr
> value
&& _cups_isspace(ptr
[-1]))
558 num_values
= cupsAddOption(key
, value
, num_values
, values
);
566 * 'cups_compare_options()' - Compare two options.
569 static int /* O - Result of comparison */
570 cups_compare_options(cups_option_t
*a
, /* I - First option */
571 cups_option_t
*b
) /* I - Second option */
573 return (_cups_strcasecmp(a
->name
, b
->name
));
578 * 'cups_find_option()' - Find an option using a binary search.
581 static int /* O - Index of match */
583 const char *name
, /* I - Option name */
584 int num_options
, /* I - Number of options */
585 cups_option_t
*options
, /* I - Options */
586 int prev
, /* I - Previous index */
587 int *rdiff
) /* O - Difference of match */
589 int left
, /* Low mark for binary search */
590 right
, /* High mark for binary search */
591 current
, /* Current index */
592 diff
; /* Result of comparison */
593 cups_option_t key
; /* Search key */
596 DEBUG_printf(("7cups_find_option(name=\"%s\", num_options=%d, options=%p, prev=%d, rdiff=%p)", name
, num_options
, (void *)options
, prev
, (void *)rdiff
));
599 for (left
= 0; left
< num_options
; left
++)
600 DEBUG_printf(("9cups_find_option: options[%d].name=\"%s\", .value=\"%s\"",
601 left
, options
[left
].name
, options
[left
].value
));
604 key
.name
= (char *)name
;
609 * Start search on either side of previous...
612 if ((diff
= cups_compare_options(&key
, options
+ prev
)) == 0 ||
613 (diff
< 0 && prev
== 0) ||
614 (diff
> 0 && prev
== (num_options
- 1)))
622 * Start with previous on right side...
631 * Start wih previous on left side...
635 right
= num_options
- 1;
641 * Start search in the middle...
645 right
= num_options
- 1;
650 current
= (left
+ right
) / 2;
651 diff
= cups_compare_options(&key
, options
+ current
);
660 while ((right
- left
) > 1);
665 * Check the last 1 or 2 elements...
668 if ((diff
= cups_compare_options(&key
, options
+ left
)) <= 0)
672 diff
= cups_compare_options(&key
, options
+ right
);
678 * Return the closest destination and the difference...