]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/options.c
79872890503de26eba8f93c779cb4137b7851091
4 * Option routines for CUPS.
6 * Copyright 2007-2014 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 * Include necessary headers...
22 #include "cups-private.h"
29 static int cups_compare_options(cups_option_t
*a
, cups_option_t
*b
);
30 static int cups_find_option(const char *name
, int num_options
,
31 cups_option_t
*option
, int prev
, int *rdiff
);
35 * 'cupsAddOption()' - Add an option to an option array.
37 * New option arrays can be initialized simply by passing 0 for the
38 * "num_options" parameter.
41 int /* O - Number of options */
42 cupsAddOption(const char *name
, /* I - Name of option */
43 const char *value
, /* I - Value of option */
44 int num_options
,/* I - Number of options */
45 cups_option_t
**options
) /* IO - Pointer to options */
47 cups_option_t
*temp
; /* Pointer to new option */
48 int insert
, /* Insertion point */
49 diff
; /* Result of search */
52 DEBUG_printf(("2cupsAddOption(name=\"%s\", value=\"%s\", num_options=%d, options=%p)", name
, value
, num_options
, (void *)options
));
54 if (!name
|| !name
[0] || !value
|| !options
|| num_options
< 0)
56 DEBUG_printf(("3cupsAddOption: Returning %d", num_options
));
61 * Look for an existing option with the same name...
71 insert
= cups_find_option(name
, num_options
, *options
, num_options
- 1,
81 * No matching option name...
84 DEBUG_printf(("4cupsAddOption: New option inserted at index %d...",
88 temp
= (cups_option_t
*)malloc(sizeof(cups_option_t
));
90 temp
= (cups_option_t
*)realloc(*options
, sizeof(cups_option_t
) * (size_t)(num_options
+ 1));
94 DEBUG_puts("3cupsAddOption: Unable to expand option array, returning 0");
100 if (insert
< num_options
)
102 DEBUG_printf(("4cupsAddOption: Shifting %d options...",
103 (int)(num_options
- insert
)));
104 memmove(temp
+ insert
+ 1, temp
+ insert
, (size_t)(num_options
- insert
) * sizeof(cups_option_t
));
108 temp
->name
= _cupsStrAlloc(name
);
114 * Match found; free the old value...
117 DEBUG_printf(("4cupsAddOption: Option already exists at index %d...",
120 temp
= *options
+ insert
;
121 _cupsStrFree(temp
->value
);
124 temp
->value
= _cupsStrAlloc(value
);
126 DEBUG_printf(("3cupsAddOption: Returning %d", num_options
));
128 return (num_options
);
133 * 'cupsFreeOptions()' - Free all memory used by options.
138 int num_options
, /* I - Number of options */
139 cups_option_t
*options
) /* I - Pointer to options */
141 int i
; /* Looping var */
144 DEBUG_printf(("cupsFreeOptions(num_options=%d, options=%p)", num_options
, (void *)options
));
146 if (num_options
<= 0 || !options
)
149 for (i
= 0; i
< num_options
; i
++)
151 _cupsStrFree(options
[i
].name
);
152 _cupsStrFree(options
[i
].value
);
160 * 'cupsGetOption()' - Get an option value.
163 const char * /* O - Option value or @code NULL@ */
164 cupsGetOption(const char *name
, /* I - Name of option */
165 int num_options
,/* I - Number of options */
166 cups_option_t
*options
) /* I - Options */
168 int diff
, /* Result of comparison */
169 match
; /* Matching index */
172 DEBUG_printf(("2cupsGetOption(name=\"%s\", num_options=%d, options=%p)", name
, num_options
, (void *)options
));
174 if (!name
|| num_options
<= 0 || !options
)
176 DEBUG_puts("3cupsGetOption: Returning NULL");
180 match
= cups_find_option(name
, num_options
, options
, -1, &diff
);
184 DEBUG_printf(("3cupsGetOption: Returning \"%s\"", options
[match
].value
));
185 return (options
[match
].value
);
188 DEBUG_puts("3cupsGetOption: Returning NULL");
194 * 'cupsParseOptions()' - Parse options from a command-line argument.
196 * This function converts space-delimited name/value pairs according
197 * to the PAPI text option ABNF specification. Collection values
198 * ("name={a=... b=... c=...}") are stored with the curley brackets
199 * intact - use @code cupsParseOptions@ on the value to extract the
200 * collection attributes.
203 int /* O - Number of options found */
205 const char *arg
, /* I - Argument to parse */
206 int num_options
, /* I - Number of options */
207 cups_option_t
**options
) /* O - Options found */
209 char *copyarg
, /* Copy of input string */
210 *ptr
, /* Pointer into string */
211 *name
, /* Pointer to name */
212 *value
, /* Pointer to value */
213 sep
, /* Separator character */
214 quote
; /* Quote character */
217 DEBUG_printf(("cupsParseOptions(arg=\"%s\", num_options=%d, options=%p)", arg
, num_options
, (void *)options
));
220 * Range check input...
225 DEBUG_printf(("1cupsParseOptions: Returning %d", num_options
));
226 return (num_options
);
229 if (!options
|| num_options
< 0)
231 DEBUG_puts("1cupsParseOptions: Returning 0");
236 * Make a copy of the argument string and then divide it up...
239 if ((copyarg
= strdup(arg
)) == NULL
)
241 DEBUG_puts("1cupsParseOptions: Unable to copy arg string");
242 DEBUG_printf(("1cupsParseOptions: Returning %d", num_options
));
243 return (num_options
);
249 * Remove surrounding {} so we can parse "{name=value ... name=value}"...
252 if ((ptr
= copyarg
+ strlen(copyarg
) - 1) > copyarg
&& *ptr
== '}')
264 * Skip leading spaces...
267 while (_cups_isspace(*ptr
))
271 * Loop through the string...
277 * Get the name up to a SPACE, =, or end-of-string...
281 while (!strchr("\f\n\r\t\v =", *ptr
) && *ptr
)
285 * Avoid an empty name...
292 * Skip trailing spaces...
295 while (_cups_isspace(*ptr
))
298 if ((sep
= *ptr
) == '=')
301 DEBUG_printf(("2cupsParseOptions: name=\"%s\"", name
));
309 if (!_cups_strncasecmp(name
, "no", 2))
310 num_options
= cupsAddOption(name
+ 2, "false", num_options
,
313 num_options
= cupsAddOption(name
, "true", num_options
, options
);
319 * Remove = and parse the value...
324 while (*ptr
&& !_cups_isspace(*ptr
))
328 else if (*ptr
== '\'' || *ptr
== '\"')
331 * Quoted string constant...
335 _cups_strcpy(ptr
, ptr
+ 1);
337 while (*ptr
!= quote
&& *ptr
)
339 if (*ptr
== '\\' && ptr
[1])
340 _cups_strcpy(ptr
, ptr
+ 1);
346 _cups_strcpy(ptr
, ptr
+ 1);
348 else if (*ptr
== '{')
351 * Collection value...
356 for (depth
= 0; *ptr
; ptr
++)
360 else if (*ptr
== '}')
369 else if (*ptr
== '\\' && ptr
[1])
370 _cups_strcpy(ptr
, ptr
+ 1);
376 * Normal space-delimited string...
379 while (*ptr
&& !_cups_isspace(*ptr
))
381 if (*ptr
== '\\' && ptr
[1])
382 _cups_strcpy(ptr
, ptr
+ 1);
392 DEBUG_printf(("2cupsParseOptions: value=\"%s\"", value
));
395 * Skip trailing whitespace...
398 while (_cups_isspace(*ptr
))
402 * Add the string value...
405 num_options
= cupsAddOption(name
, value
, num_options
, options
);
409 * Free the copy of the argument we made and return the number of options
415 DEBUG_printf(("1cupsParseOptions: Returning %d", num_options
));
417 return (num_options
);
422 * 'cupsRemoveOption()' - Remove an option from an option array.
424 * @since CUPS 1.2/OS X 10.5@
427 int /* O - New number of options */
429 const char *name
, /* I - Option name */
430 int num_options
, /* I - Current number of options */
431 cups_option_t
**options
) /* IO - Options */
433 int i
; /* Looping var */
434 cups_option_t
*option
; /* Current option */
437 DEBUG_printf(("2cupsRemoveOption(name=\"%s\", num_options=%d, options=%p)", name
, num_options
, (void *)options
));
440 * Range check input...
443 if (!name
|| num_options
< 1 || !options
)
445 DEBUG_printf(("3cupsRemoveOption: Returning %d", num_options
));
446 return (num_options
);
450 * Loop for the option...
453 for (i
= num_options
, option
= *options
; i
> 0; i
--, option
++)
454 if (!_cups_strcasecmp(name
, option
->name
))
460 * Remove this option from the array...
463 DEBUG_puts("4cupsRemoveOption: Found option, removing it...");
468 _cupsStrFree(option
->name
);
469 _cupsStrFree(option
->value
);
472 memmove(option
, option
+ 1, (size_t)i
* sizeof(cups_option_t
));
476 * Return the new number of options...
479 DEBUG_printf(("3cupsRemoveOption: Returning %d", num_options
));
480 return (num_options
);
485 * '_cupsGet1284Values()' - Get 1284 device ID keys and values.
487 * The returned dictionary is a CUPS option array that can be queried with
488 * cupsGetOption and freed with cupsFreeOptions.
491 int /* O - Number of key/value pairs */
493 const char *device_id
, /* I - IEEE-1284 device ID string */
494 cups_option_t
**values
) /* O - Array of key/value pairs */
496 int num_values
; /* Number of values */
497 char key
[256], /* Key string */
498 value
[256], /* Value string */
499 *ptr
; /* Pointer into key/value */
503 * Range check input...
509 if (!device_id
|| !values
)
513 * Parse the 1284 device ID value into keys and values. The format is
514 * repeating sequences of:
516 * [whitespace]key:value[whitespace];
522 while (_cups_isspace(*device_id
))
528 for (ptr
= key
; *device_id
&& *device_id
!= ':'; device_id
++)
529 if (ptr
< (key
+ sizeof(key
) - 1))
535 while (ptr
> key
&& _cups_isspace(ptr
[-1]))
541 while (_cups_isspace(*device_id
))
547 for (ptr
= value
; *device_id
&& *device_id
!= ';'; device_id
++)
548 if (ptr
< (value
+ sizeof(value
) - 1))
554 while (ptr
> value
&& _cups_isspace(ptr
[-1]))
560 num_values
= cupsAddOption(key
, value
, num_values
, values
);
568 * 'cups_compare_options()' - Compare two options.
571 static int /* O - Result of comparison */
572 cups_compare_options(cups_option_t
*a
, /* I - First option */
573 cups_option_t
*b
) /* I - Second option */
575 return (_cups_strcasecmp(a
->name
, b
->name
));
580 * 'cups_find_option()' - Find an option using a binary search.
583 static int /* O - Index of match */
585 const char *name
, /* I - Option name */
586 int num_options
, /* I - Number of options */
587 cups_option_t
*options
, /* I - Options */
588 int prev
, /* I - Previous index */
589 int *rdiff
) /* O - Difference of match */
591 int left
, /* Low mark for binary search */
592 right
, /* High mark for binary search */
593 current
, /* Current index */
594 diff
; /* Result of comparison */
595 cups_option_t key
; /* Search key */
598 DEBUG_printf(("7cups_find_option(name=\"%s\", num_options=%d, options=%p, prev=%d, rdiff=%p)", name
, num_options
, (void *)options
, prev
, (void *)rdiff
));
601 for (left
= 0; left
< num_options
; left
++)
602 DEBUG_printf(("9cups_find_option: options[%d].name=\"%s\", .value=\"%s\"",
603 left
, options
[left
].name
, options
[left
].value
));
606 key
.name
= (char *)name
;
611 * Start search on either side of previous...
614 if ((diff
= cups_compare_options(&key
, options
+ prev
)) == 0 ||
615 (diff
< 0 && prev
== 0) ||
616 (diff
> 0 && prev
== (num_options
- 1)))
624 * Start with previous on right side...
633 * Start wih previous on left side...
637 right
= num_options
- 1;
643 * Start search in the middle...
647 right
= num_options
- 1;
652 current
= (left
+ right
) / 2;
653 diff
= cups_compare_options(&key
, options
+ current
);
662 while ((right
- left
) > 1);
667 * Check the last 1 or 2 elements...
670 if ((diff
= cups_compare_options(&key
, options
+ left
)) <= 0)
674 diff
= cups_compare_options(&key
, options
+ right
);
680 * Return the closest destination and the difference...