]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/options.c
2 * Option routines for CUPS.
4 * Copyright 2007-2017 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 * 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 * 'cupsAddIntegerOption()' - Add an integer option to an option array.
35 * New option arrays can be initialized simply by passing 0 for the
36 * "num_options" parameter.
38 * @since CUPS 2.2.4/macOS 10.13@
41 int /* O - Number of options */
43 const char *name
, /* I - Name of option */
44 int value
, /* I - Value of option */
45 int num_options
, /* I - Number of options */
46 cups_option_t
**options
) /* IO - Pointer to options */
48 char strvalue
[32]; /* String value */
51 snprintf(strvalue
, sizeof(strvalue
), "%d", value
);
53 return (cupsAddOption(name
, strvalue
, num_options
, options
));
58 * 'cupsAddOption()' - Add an option to an option array.
60 * New option arrays can be initialized simply by passing 0 for the
61 * "num_options" parameter.
64 int /* O - Number of options */
65 cupsAddOption(const char *name
, /* I - Name of option */
66 const char *value
, /* I - Value of option */
67 int num_options
,/* I - Number of options */
68 cups_option_t
**options
) /* IO - Pointer to options */
70 cups_option_t
*temp
; /* Pointer to new option */
71 int insert
, /* Insertion point */
72 diff
; /* Result of search */
75 DEBUG_printf(("2cupsAddOption(name=\"%s\", value=\"%s\", num_options=%d, options=%p)", name
, value
, num_options
, (void *)options
));
77 if (!name
|| !name
[0] || !value
|| !options
|| num_options
< 0)
79 DEBUG_printf(("3cupsAddOption: Returning %d", num_options
));
83 if (!_cups_strcasecmp(name
, "cupsPrintQuality"))
84 num_options
= cupsRemoveOption("print-quality", num_options
, options
);
85 else if (!_cups_strcasecmp(name
, "print-quality"))
86 num_options
= cupsRemoveOption("cupsPrintQuality", num_options
, options
);
89 * Look for an existing option with the same name...
99 insert
= cups_find_option(name
, num_options
, *options
, num_options
- 1,
109 * No matching option name...
112 DEBUG_printf(("4cupsAddOption: New option inserted at index %d...",
115 if (num_options
== 0)
116 temp
= (cups_option_t
*)malloc(sizeof(cups_option_t
));
118 temp
= (cups_option_t
*)realloc(*options
, sizeof(cups_option_t
) * (size_t)(num_options
+ 1));
122 DEBUG_puts("3cupsAddOption: Unable to expand option array, returning 0");
128 if (insert
< num_options
)
130 DEBUG_printf(("4cupsAddOption: Shifting %d options...",
131 (int)(num_options
- insert
)));
132 memmove(temp
+ insert
+ 1, temp
+ insert
, (size_t)(num_options
- insert
) * sizeof(cups_option_t
));
136 temp
->name
= _cupsStrAlloc(name
);
142 * Match found; free the old value...
145 DEBUG_printf(("4cupsAddOption: Option already exists at index %d...",
148 temp
= *options
+ insert
;
149 _cupsStrFree(temp
->value
);
152 temp
->value
= _cupsStrAlloc(value
);
154 DEBUG_printf(("3cupsAddOption: Returning %d", num_options
));
156 return (num_options
);
161 * 'cupsFreeOptions()' - Free all memory used by options.
166 int num_options
, /* I - Number of options */
167 cups_option_t
*options
) /* I - Pointer to options */
169 int i
; /* Looping var */
172 DEBUG_printf(("cupsFreeOptions(num_options=%d, options=%p)", num_options
, (void *)options
));
174 if (num_options
<= 0 || !options
)
177 for (i
= 0; i
< num_options
; i
++)
179 _cupsStrFree(options
[i
].name
);
180 _cupsStrFree(options
[i
].value
);
188 * 'cupsGetIntegerOption()' - Get an integer option value.
190 * INT_MIN is returned when the option does not exist, is not an integer, or
191 * exceeds the range of values for the "int" type.
193 * @since CUPS 2.2.4/macOS 10.13@
196 int /* O - Option value or @code INT_MIN@ */
197 cupsGetIntegerOption(
198 const char *name
, /* I - Name of option */
199 int num_options
, /* I - Number of options */
200 cups_option_t
*options
) /* I - Options */
202 const char *value
= cupsGetOption(name
, num_options
, options
);
203 /* String value of option */
204 char *ptr
; /* Pointer into string value */
205 long intvalue
; /* Integer value */
208 if (!value
|| !*value
)
211 intvalue
= strtol(value
, &ptr
, 10);
212 if (intvalue
< INT_MIN
|| intvalue
> INT_MAX
|| *ptr
)
215 return ((int)intvalue
);
220 * 'cupsGetOption()' - Get an option value.
223 const char * /* O - Option value or @code NULL@ */
224 cupsGetOption(const char *name
, /* I - Name of option */
225 int num_options
,/* I - Number of options */
226 cups_option_t
*options
) /* I - Options */
228 int diff
, /* Result of comparison */
229 match
; /* Matching index */
232 DEBUG_printf(("2cupsGetOption(name=\"%s\", num_options=%d, options=%p)", name
, num_options
, (void *)options
));
234 if (!name
|| num_options
<= 0 || !options
)
236 DEBUG_puts("3cupsGetOption: Returning NULL");
240 match
= cups_find_option(name
, num_options
, options
, -1, &diff
);
244 DEBUG_printf(("3cupsGetOption: Returning \"%s\"", options
[match
].value
));
245 return (options
[match
].value
);
248 DEBUG_puts("3cupsGetOption: Returning NULL");
254 * 'cupsParseOptions()' - Parse options from a command-line argument.
256 * This function converts space-delimited name/value pairs according
257 * to the PAPI text option ABNF specification. Collection values
258 * ("name={a=... b=... c=...}") are stored with the curley brackets
259 * intact - use @code cupsParseOptions@ on the value to extract the
260 * collection attributes.
263 int /* O - Number of options found */
265 const char *arg
, /* I - Argument to parse */
266 int num_options
, /* I - Number of options */
267 cups_option_t
**options
) /* O - Options found */
269 char *copyarg
, /* Copy of input string */
270 *ptr
, /* Pointer into string */
271 *name
, /* Pointer to name */
272 *value
, /* Pointer to value */
273 sep
, /* Separator character */
274 quote
; /* Quote character */
277 DEBUG_printf(("cupsParseOptions(arg=\"%s\", num_options=%d, options=%p)", arg
, num_options
, (void *)options
));
280 * Range check input...
285 DEBUG_printf(("1cupsParseOptions: Returning %d", num_options
));
286 return (num_options
);
289 if (!options
|| num_options
< 0)
291 DEBUG_puts("1cupsParseOptions: Returning 0");
296 * Make a copy of the argument string and then divide it up...
299 if ((copyarg
= strdup(arg
)) == NULL
)
301 DEBUG_puts("1cupsParseOptions: Unable to copy arg string");
302 DEBUG_printf(("1cupsParseOptions: Returning %d", num_options
));
303 return (num_options
);
309 * Remove surrounding {} so we can parse "{name=value ... name=value}"...
312 if ((ptr
= copyarg
+ strlen(copyarg
) - 1) > copyarg
&& *ptr
== '}')
324 * Skip leading spaces...
327 while (_cups_isspace(*ptr
))
331 * Loop through the string...
337 * Get the name up to a SPACE, =, or end-of-string...
341 while (!strchr("\f\n\r\t\v =", *ptr
) && *ptr
)
345 * Avoid an empty name...
352 * Skip trailing spaces...
355 while (_cups_isspace(*ptr
))
358 if ((sep
= *ptr
) == '=')
361 DEBUG_printf(("2cupsParseOptions: name=\"%s\"", name
));
369 if (!_cups_strncasecmp(name
, "no", 2))
370 num_options
= cupsAddOption(name
+ 2, "false", num_options
,
373 num_options
= cupsAddOption(name
, "true", num_options
, options
);
379 * Remove = and parse the value...
384 while (*ptr
&& !_cups_isspace(*ptr
))
388 else if (*ptr
== '\'' || *ptr
== '\"')
391 * Quoted string constant...
395 _cups_strcpy(ptr
, ptr
+ 1);
397 while (*ptr
!= quote
&& *ptr
)
399 if (*ptr
== '\\' && ptr
[1])
400 _cups_strcpy(ptr
, ptr
+ 1);
406 _cups_strcpy(ptr
, ptr
+ 1);
408 else if (*ptr
== '{')
411 * Collection value...
416 for (depth
= 0; *ptr
; ptr
++)
420 else if (*ptr
== '}')
429 else if (*ptr
== '\\' && ptr
[1])
430 _cups_strcpy(ptr
, ptr
+ 1);
436 * Normal space-delimited string...
439 while (*ptr
&& !_cups_isspace(*ptr
))
441 if (*ptr
== '\\' && ptr
[1])
442 _cups_strcpy(ptr
, ptr
+ 1);
452 DEBUG_printf(("2cupsParseOptions: value=\"%s\"", value
));
455 * Skip trailing whitespace...
458 while (_cups_isspace(*ptr
))
462 * Add the string value...
465 num_options
= cupsAddOption(name
, value
, num_options
, options
);
469 * Free the copy of the argument we made and return the number of options
475 DEBUG_printf(("1cupsParseOptions: Returning %d", num_options
));
477 return (num_options
);
482 * 'cupsRemoveOption()' - Remove an option from an option array.
484 * @since CUPS 1.2/macOS 10.5@
487 int /* O - New number of options */
489 const char *name
, /* I - Option name */
490 int num_options
, /* I - Current number of options */
491 cups_option_t
**options
) /* IO - Options */
493 int i
; /* Looping var */
494 cups_option_t
*option
; /* Current option */
497 DEBUG_printf(("2cupsRemoveOption(name=\"%s\", num_options=%d, options=%p)", name
, num_options
, (void *)options
));
500 * Range check input...
503 if (!name
|| num_options
< 1 || !options
)
505 DEBUG_printf(("3cupsRemoveOption: Returning %d", num_options
));
506 return (num_options
);
510 * Loop for the option...
513 for (i
= num_options
, option
= *options
; i
> 0; i
--, option
++)
514 if (!_cups_strcasecmp(name
, option
->name
))
520 * Remove this option from the array...
523 DEBUG_puts("4cupsRemoveOption: Found option, removing it...");
528 _cupsStrFree(option
->name
);
529 _cupsStrFree(option
->value
);
532 memmove(option
, option
+ 1, (size_t)i
* sizeof(cups_option_t
));
536 * Return the new number of options...
539 DEBUG_printf(("3cupsRemoveOption: Returning %d", num_options
));
540 return (num_options
);
545 * '_cupsGet1284Values()' - Get 1284 device ID keys and values.
547 * The returned dictionary is a CUPS option array that can be queried with
548 * cupsGetOption and freed with cupsFreeOptions.
551 int /* O - Number of key/value pairs */
553 const char *device_id
, /* I - IEEE-1284 device ID string */
554 cups_option_t
**values
) /* O - Array of key/value pairs */
556 int num_values
; /* Number of values */
557 char key
[256], /* Key string */
558 value
[256], /* Value string */
559 *ptr
; /* Pointer into key/value */
563 * Range check input...
569 if (!device_id
|| !values
)
573 * Parse the 1284 device ID value into keys and values. The format is
574 * repeating sequences of:
576 * [whitespace]key:value[whitespace];
582 while (_cups_isspace(*device_id
))
588 for (ptr
= key
; *device_id
&& *device_id
!= ':'; device_id
++)
589 if (ptr
< (key
+ sizeof(key
) - 1))
595 while (ptr
> key
&& _cups_isspace(ptr
[-1]))
601 while (_cups_isspace(*device_id
))
607 for (ptr
= value
; *device_id
&& *device_id
!= ';'; device_id
++)
608 if (ptr
< (value
+ sizeof(value
) - 1))
614 while (ptr
> value
&& _cups_isspace(ptr
[-1]))
620 num_values
= cupsAddOption(key
, value
, num_values
, values
);
628 * 'cups_compare_options()' - Compare two options.
631 static int /* O - Result of comparison */
632 cups_compare_options(cups_option_t
*a
, /* I - First option */
633 cups_option_t
*b
) /* I - Second option */
635 return (_cups_strcasecmp(a
->name
, b
->name
));
640 * 'cups_find_option()' - Find an option using a binary search.
643 static int /* O - Index of match */
645 const char *name
, /* I - Option name */
646 int num_options
, /* I - Number of options */
647 cups_option_t
*options
, /* I - Options */
648 int prev
, /* I - Previous index */
649 int *rdiff
) /* O - Difference of match */
651 int left
, /* Low mark for binary search */
652 right
, /* High mark for binary search */
653 current
, /* Current index */
654 diff
; /* Result of comparison */
655 cups_option_t key
; /* Search key */
658 DEBUG_printf(("7cups_find_option(name=\"%s\", num_options=%d, options=%p, prev=%d, rdiff=%p)", name
, num_options
, (void *)options
, prev
, (void *)rdiff
));
661 for (left
= 0; left
< num_options
; left
++)
662 DEBUG_printf(("9cups_find_option: options[%d].name=\"%s\", .value=\"%s\"",
663 left
, options
[left
].name
, options
[left
].value
));
666 key
.name
= (char *)name
;
671 * Start search on either side of previous...
674 if ((diff
= cups_compare_options(&key
, options
+ prev
)) == 0 ||
675 (diff
< 0 && prev
== 0) ||
676 (diff
> 0 && prev
== (num_options
- 1)))
684 * Start with previous on right side...
693 * Start wih previous on left side...
697 right
= num_options
- 1;
703 * Start search in the middle...
707 right
= num_options
- 1;
712 current
= (left
+ right
) / 2;
713 diff
= cups_compare_options(&key
, options
+ current
);
722 while ((right
- left
) > 1);
727 * Check the last 1 or 2 elements...
730 if ((diff
= cups_compare_options(&key
, options
+ left
)) <= 0)
734 diff
= cups_compare_options(&key
, options
+ right
);
740 * Return the closest destination and the difference...