]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/options.c
073edaee95ac3bff0527f9448424e649eba3bec7
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, "
53 "options=%p)", name
, value
, num_options
, options
));
55 if (!name
|| !name
[0] || !value
|| !options
|| num_options
< 0)
57 DEBUG_printf(("3cupsAddOption: Returning %d", num_options
));
62 * Look for an existing option with the same name...
72 insert
= cups_find_option(name
, num_options
, *options
, num_options
- 1,
82 * No matching option name...
85 DEBUG_printf(("4cupsAddOption: New option inserted at index %d...",
89 temp
= (cups_option_t
*)malloc(sizeof(cups_option_t
));
91 temp
= (cups_option_t
*)realloc(*options
, sizeof(cups_option_t
) * (size_t)(num_options
+ 1));
95 DEBUG_puts("3cupsAddOption: Unable to expand option array, returning 0");
101 if (insert
< num_options
)
103 DEBUG_printf(("4cupsAddOption: Shifting %d options...",
104 (int)(num_options
- insert
)));
105 memmove(temp
+ insert
+ 1, temp
+ insert
, (size_t)(num_options
- insert
) * sizeof(cups_option_t
));
109 temp
->name
= _cupsStrAlloc(name
);
115 * Match found; free the old value...
118 DEBUG_printf(("4cupsAddOption: Option already exists at index %d...",
121 temp
= *options
+ insert
;
122 _cupsStrFree(temp
->value
);
125 temp
->value
= _cupsStrAlloc(value
);
127 DEBUG_printf(("3cupsAddOption: Returning %d", num_options
));
129 return (num_options
);
134 * 'cupsFreeOptions()' - Free all memory used by options.
139 int num_options
, /* I - Number of options */
140 cups_option_t
*options
) /* I - Pointer to options */
142 int i
; /* Looping var */
145 DEBUG_printf(("cupsFreeOptions(num_options=%d, options=%p)", num_options
,
148 if (num_options
<= 0 || !options
)
151 for (i
= 0; i
< num_options
; i
++)
153 _cupsStrFree(options
[i
].name
);
154 _cupsStrFree(options
[i
].value
);
162 * 'cupsGetOption()' - Get an option value.
165 const char * /* O - Option value or @code NULL@ */
166 cupsGetOption(const char *name
, /* I - Name of option */
167 int num_options
,/* I - Number of options */
168 cups_option_t
*options
) /* I - Options */
170 int diff
, /* Result of comparison */
171 match
; /* Matching index */
174 DEBUG_printf(("2cupsGetOption(name=\"%s\", num_options=%d, options=%p)",
175 name
, num_options
, options
));
177 if (!name
|| num_options
<= 0 || !options
)
179 DEBUG_puts("3cupsGetOption: Returning NULL");
183 match
= cups_find_option(name
, num_options
, options
, -1, &diff
);
187 DEBUG_printf(("3cupsGetOption: Returning \"%s\"", options
[match
].value
));
188 return (options
[match
].value
);
191 DEBUG_puts("3cupsGetOption: Returning NULL");
197 * 'cupsParseOptions()' - Parse options from a command-line argument.
199 * This function converts space-delimited name/value pairs according
200 * to the PAPI text option ABNF specification. Collection values
201 * ("name={a=... b=... c=...}") are stored with the curley brackets
202 * intact - use @code cupsParseOptions@ on the value to extract the
203 * collection attributes.
206 int /* O - Number of options found */
208 const char *arg
, /* I - Argument to parse */
209 int num_options
, /* I - Number of options */
210 cups_option_t
**options
) /* O - Options found */
212 char *copyarg
, /* Copy of input string */
213 *ptr
, /* Pointer into string */
214 *name
, /* Pointer to name */
215 *value
, /* Pointer to value */
216 sep
, /* Separator character */
217 quote
; /* Quote character */
220 DEBUG_printf(("cupsParseOptions(arg=\"%s\", num_options=%d, options=%p)",
221 arg
, num_options
, options
));
224 * Range check input...
229 DEBUG_printf(("1cupsParseOptions: Returning %d", num_options
));
230 return (num_options
);
233 if (!options
|| num_options
< 0)
235 DEBUG_puts("1cupsParseOptions: Returning 0");
240 * Make a copy of the argument string and then divide it up...
243 if ((copyarg
= strdup(arg
)) == NULL
)
245 DEBUG_puts("1cupsParseOptions: Unable to copy arg string");
246 DEBUG_printf(("1cupsParseOptions: Returning %d", num_options
));
247 return (num_options
);
253 * Remove surrounding {} so we can parse "{name=value ... name=value}"...
256 if ((ptr
= copyarg
+ strlen(copyarg
) - 1) > copyarg
&& *ptr
== '}')
268 * Skip leading spaces...
271 while (_cups_isspace(*ptr
))
275 * Loop through the string...
281 * Get the name up to a SPACE, =, or end-of-string...
285 while (!strchr("\f\n\r\t\v =", *ptr
) && *ptr
)
289 * Avoid an empty name...
296 * Skip trailing spaces...
299 while (_cups_isspace(*ptr
))
302 if ((sep
= *ptr
) == '=')
305 DEBUG_printf(("2cupsParseOptions: name=\"%s\"", name
));
313 if (!_cups_strncasecmp(name
, "no", 2))
314 num_options
= cupsAddOption(name
+ 2, "false", num_options
,
317 num_options
= cupsAddOption(name
, "true", num_options
, options
);
323 * Remove = and parse the value...
328 while (*ptr
&& !_cups_isspace(*ptr
))
332 else if (*ptr
== '\'' || *ptr
== '\"')
335 * Quoted string constant...
339 _cups_strcpy(ptr
, ptr
+ 1);
341 while (*ptr
!= quote
&& *ptr
)
343 if (*ptr
== '\\' && ptr
[1])
344 _cups_strcpy(ptr
, ptr
+ 1);
350 _cups_strcpy(ptr
, ptr
+ 1);
352 else if (*ptr
== '{')
355 * Collection value...
360 for (depth
= 0; *ptr
; ptr
++)
364 else if (*ptr
== '}')
373 else if (*ptr
== '\\' && ptr
[1])
374 _cups_strcpy(ptr
, ptr
+ 1);
380 * Normal space-delimited string...
383 while (*ptr
&& !_cups_isspace(*ptr
))
385 if (*ptr
== '\\' && ptr
[1])
386 _cups_strcpy(ptr
, ptr
+ 1);
396 DEBUG_printf(("2cupsParseOptions: value=\"%s\"", value
));
399 * Skip trailing whitespace...
402 while (_cups_isspace(*ptr
))
406 * Add the string value...
409 num_options
= cupsAddOption(name
, value
, num_options
, options
);
413 * Free the copy of the argument we made and return the number of options
419 DEBUG_printf(("1cupsParseOptions: Returning %d", num_options
));
421 return (num_options
);
426 * 'cupsRemoveOption()' - Remove an option from an option array.
428 * @since CUPS 1.2/OS X 10.5@
431 int /* O - New number of options */
433 const char *name
, /* I - Option name */
434 int num_options
, /* I - Current number of options */
435 cups_option_t
**options
) /* IO - Options */
437 int i
; /* Looping var */
438 cups_option_t
*option
; /* Current option */
441 DEBUG_printf(("2cupsRemoveOption(name=\"%s\", num_options=%d, options=%p)",
442 name
, num_options
, options
));
445 * Range check input...
448 if (!name
|| num_options
< 1 || !options
)
450 DEBUG_printf(("3cupsRemoveOption: Returning %d", num_options
));
451 return (num_options
);
455 * Loop for the option...
458 for (i
= num_options
, option
= *options
; i
> 0; i
--, option
++)
459 if (!_cups_strcasecmp(name
, option
->name
))
465 * Remove this option from the array...
468 DEBUG_puts("4cupsRemoveOption: Found option, removing it...");
473 _cupsStrFree(option
->name
);
474 _cupsStrFree(option
->value
);
477 memmove(option
, option
+ 1, (size_t)i
* sizeof(cups_option_t
));
481 * Return the new number of options...
484 DEBUG_printf(("3cupsRemoveOption: Returning %d", num_options
));
485 return (num_options
);
490 * '_cupsGet1284Values()' - Get 1284 device ID keys and values.
492 * The returned dictionary is a CUPS option array that can be queried with
493 * cupsGetOption and freed with cupsFreeOptions.
496 int /* O - Number of key/value pairs */
498 const char *device_id
, /* I - IEEE-1284 device ID string */
499 cups_option_t
**values
) /* O - Array of key/value pairs */
501 int num_values
; /* Number of values */
502 char key
[256], /* Key string */
503 value
[256], /* Value string */
504 *ptr
; /* Pointer into key/value */
508 * Range check input...
514 if (!device_id
|| !values
)
518 * Parse the 1284 device ID value into keys and values. The format is
519 * repeating sequences of:
521 * [whitespace]key:value[whitespace];
527 while (_cups_isspace(*device_id
))
533 for (ptr
= key
; *device_id
&& *device_id
!= ':'; device_id
++)
534 if (ptr
< (key
+ sizeof(key
) - 1))
540 while (ptr
> key
&& _cups_isspace(ptr
[-1]))
546 while (_cups_isspace(*device_id
))
552 for (ptr
= value
; *device_id
&& *device_id
!= ';'; device_id
++)
553 if (ptr
< (value
+ sizeof(value
) - 1))
559 while (ptr
> value
&& _cups_isspace(ptr
[-1]))
565 num_values
= cupsAddOption(key
, value
, num_values
, values
);
573 * 'cups_compare_options()' - Compare two options.
576 static int /* O - Result of comparison */
577 cups_compare_options(cups_option_t
*a
, /* I - First option */
578 cups_option_t
*b
) /* I - Second option */
580 return (_cups_strcasecmp(a
->name
, b
->name
));
585 * 'cups_find_option()' - Find an option using a binary search.
588 static int /* O - Index of match */
590 const char *name
, /* I - Option name */
591 int num_options
, /* I - Number of options */
592 cups_option_t
*options
, /* I - Options */
593 int prev
, /* I - Previous index */
594 int *rdiff
) /* O - Difference of match */
596 int left
, /* Low mark for binary search */
597 right
, /* High mark for binary search */
598 current
, /* Current index */
599 diff
; /* Result of comparison */
600 cups_option_t key
; /* Search key */
603 DEBUG_printf(("7cups_find_option(name=\"%s\", num_options=%d, options=%p, "
604 "prev=%d, rdiff=%p)", name
, num_options
, options
, prev
,
608 for (left
= 0; left
< num_options
; left
++)
609 DEBUG_printf(("9cups_find_option: options[%d].name=\"%s\", .value=\"%s\"",
610 left
, options
[left
].name
, options
[left
].value
));
613 key
.name
= (char *)name
;
618 * Start search on either side of previous...
621 if ((diff
= cups_compare_options(&key
, options
+ prev
)) == 0 ||
622 (diff
< 0 && prev
== 0) ||
623 (diff
> 0 && prev
== (num_options
- 1)))
631 * Start with previous on right side...
640 * Start wih previous on left side...
644 right
= num_options
- 1;
650 * Start search in the middle...
654 right
= num_options
- 1;
659 current
= (left
+ right
) / 2;
660 diff
= cups_compare_options(&key
, options
+ current
);
669 while ((right
- left
) > 1);
674 * Check the last 1 or 2 elements...
677 if ((diff
= cups_compare_options(&key
, options
+ left
)) <= 0)
681 diff
= cups_compare_options(&key
, options
+ right
);
687 * Return the closest destination and the difference...