]>
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 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include "cups-private.h"
21 static int cups_compare_options(cups_option_t
*a
, cups_option_t
*b
);
22 static int cups_find_option(const char *name
, int num_options
,
23 cups_option_t
*option
, int prev
, int *rdiff
);
27 * 'cupsAddIntegerOption()' - Add an integer option to an option array.
29 * New option arrays can be initialized simply by passing 0 for the
30 * "num_options" parameter.
32 * @since CUPS 2.2.4/macOS 10.13@
35 int /* O - Number of options */
37 const char *name
, /* I - Name of option */
38 int value
, /* I - Value of option */
39 int num_options
, /* I - Number of options */
40 cups_option_t
**options
) /* IO - Pointer to options */
42 char strvalue
[32]; /* String value */
45 snprintf(strvalue
, sizeof(strvalue
), "%d", value
);
47 return (cupsAddOption(name
, strvalue
, num_options
, options
));
52 * 'cupsAddOption()' - Add an option to an option array.
54 * New option arrays can be initialized simply by passing 0 for the
55 * "num_options" parameter.
58 int /* O - Number of options */
59 cupsAddOption(const char *name
, /* I - Name of option */
60 const char *value
, /* I - Value of option */
61 int num_options
,/* I - Number of options */
62 cups_option_t
**options
) /* IO - Pointer to options */
64 cups_option_t
*temp
; /* Pointer to new option */
65 int insert
, /* Insertion point */
66 diff
; /* Result of search */
69 DEBUG_printf(("2cupsAddOption(name=\"%s\", value=\"%s\", num_options=%d, options=%p)", name
, value
, num_options
, (void *)options
));
71 if (!name
|| !name
[0] || !value
|| !options
|| num_options
< 0)
73 DEBUG_printf(("3cupsAddOption: Returning %d", num_options
));
77 if (!_cups_strcasecmp(name
, "cupsPrintQuality"))
78 num_options
= cupsRemoveOption("print-quality", num_options
, options
);
79 else if (!_cups_strcasecmp(name
, "print-quality"))
80 num_options
= cupsRemoveOption("cupsPrintQuality", num_options
, options
);
83 * Look for an existing option with the same name...
93 insert
= cups_find_option(name
, num_options
, *options
, num_options
- 1,
103 * No matching option name...
106 DEBUG_printf(("4cupsAddOption: New option inserted at index %d...",
109 if (num_options
== 0)
110 temp
= (cups_option_t
*)malloc(sizeof(cups_option_t
));
112 temp
= (cups_option_t
*)realloc(*options
, sizeof(cups_option_t
) * (size_t)(num_options
+ 1));
116 DEBUG_puts("3cupsAddOption: Unable to expand option array, returning 0");
122 if (insert
< num_options
)
124 DEBUG_printf(("4cupsAddOption: Shifting %d options...",
125 (int)(num_options
- insert
)));
126 memmove(temp
+ insert
+ 1, temp
+ insert
, (size_t)(num_options
- insert
) * sizeof(cups_option_t
));
130 temp
->name
= _cupsStrAlloc(name
);
136 * Match found; free the old value...
139 DEBUG_printf(("4cupsAddOption: Option already exists at index %d...",
142 temp
= *options
+ insert
;
143 _cupsStrFree(temp
->value
);
146 temp
->value
= _cupsStrAlloc(value
);
148 DEBUG_printf(("3cupsAddOption: Returning %d", num_options
));
150 return (num_options
);
155 * 'cupsFreeOptions()' - Free all memory used by options.
160 int num_options
, /* I - Number of options */
161 cups_option_t
*options
) /* I - Pointer to options */
163 int i
; /* Looping var */
166 DEBUG_printf(("cupsFreeOptions(num_options=%d, options=%p)", num_options
, (void *)options
));
168 if (num_options
<= 0 || !options
)
171 for (i
= 0; i
< num_options
; i
++)
173 _cupsStrFree(options
[i
].name
);
174 _cupsStrFree(options
[i
].value
);
182 * 'cupsGetIntegerOption()' - Get an integer option value.
184 * INT_MIN is returned when the option does not exist, is not an integer, or
185 * exceeds the range of values for the "int" type.
187 * @since CUPS 2.2.4/macOS 10.13@
190 int /* O - Option value or @code INT_MIN@ */
191 cupsGetIntegerOption(
192 const char *name
, /* I - Name of option */
193 int num_options
, /* I - Number of options */
194 cups_option_t
*options
) /* I - Options */
196 const char *value
= cupsGetOption(name
, num_options
, options
);
197 /* String value of option */
198 char *ptr
; /* Pointer into string value */
199 long intvalue
; /* Integer value */
202 if (!value
|| !*value
)
205 intvalue
= strtol(value
, &ptr
, 10);
206 if (intvalue
< INT_MIN
|| intvalue
> INT_MAX
|| *ptr
)
209 return ((int)intvalue
);
214 * 'cupsGetOption()' - Get an option value.
217 const char * /* O - Option value or @code NULL@ */
218 cupsGetOption(const char *name
, /* I - Name of option */
219 int num_options
,/* I - Number of options */
220 cups_option_t
*options
) /* I - Options */
222 int diff
, /* Result of comparison */
223 match
; /* Matching index */
226 DEBUG_printf(("2cupsGetOption(name=\"%s\", num_options=%d, options=%p)", name
, num_options
, (void *)options
));
228 if (!name
|| num_options
<= 0 || !options
)
230 DEBUG_puts("3cupsGetOption: Returning NULL");
234 match
= cups_find_option(name
, num_options
, options
, -1, &diff
);
238 DEBUG_printf(("3cupsGetOption: Returning \"%s\"", options
[match
].value
));
239 return (options
[match
].value
);
242 DEBUG_puts("3cupsGetOption: Returning NULL");
248 * 'cupsParseOptions()' - Parse options from a command-line argument.
250 * This function converts space-delimited name/value pairs according
251 * to the PAPI text option ABNF specification. Collection values
252 * ("name={a=... b=... c=...}") are stored with the curley brackets
253 * intact - use @code cupsParseOptions@ on the value to extract the
254 * collection attributes.
257 int /* O - Number of options found */
259 const char *arg
, /* I - Argument to parse */
260 int num_options
, /* I - Number of options */
261 cups_option_t
**options
) /* O - Options found */
263 char *copyarg
, /* Copy of input string */
264 *ptr
, /* Pointer into string */
265 *name
, /* Pointer to name */
266 *value
, /* Pointer to value */
267 sep
, /* Separator character */
268 quote
; /* Quote character */
271 DEBUG_printf(("cupsParseOptions(arg=\"%s\", num_options=%d, options=%p)", arg
, num_options
, (void *)options
));
274 * Range check input...
279 DEBUG_printf(("1cupsParseOptions: Returning %d", num_options
));
280 return (num_options
);
283 if (!options
|| num_options
< 0)
285 DEBUG_puts("1cupsParseOptions: Returning 0");
290 * Make a copy of the argument string and then divide it up...
293 if ((copyarg
= strdup(arg
)) == NULL
)
295 DEBUG_puts("1cupsParseOptions: Unable to copy arg string");
296 DEBUG_printf(("1cupsParseOptions: Returning %d", num_options
));
297 return (num_options
);
303 * Remove surrounding {} so we can parse "{name=value ... name=value}"...
306 if ((ptr
= copyarg
+ strlen(copyarg
) - 1) > copyarg
&& *ptr
== '}')
318 * Skip leading spaces...
321 while (_cups_isspace(*ptr
))
325 * Loop through the string...
331 * Get the name up to a SPACE, =, or end-of-string...
335 while (!strchr("\f\n\r\t\v =", *ptr
) && *ptr
)
339 * Avoid an empty name...
346 * Skip trailing spaces...
349 while (_cups_isspace(*ptr
))
352 if ((sep
= *ptr
) == '=')
355 DEBUG_printf(("2cupsParseOptions: name=\"%s\"", name
));
363 if (!_cups_strncasecmp(name
, "no", 2))
364 num_options
= cupsAddOption(name
+ 2, "false", num_options
,
367 num_options
= cupsAddOption(name
, "true", num_options
, options
);
373 * Remove = and parse the value...
378 while (*ptr
&& !_cups_isspace(*ptr
))
382 else if (*ptr
== '\'' || *ptr
== '\"')
385 * Quoted string constant...
389 _cups_strcpy(ptr
, ptr
+ 1);
391 while (*ptr
!= quote
&& *ptr
)
393 if (*ptr
== '\\' && ptr
[1])
394 _cups_strcpy(ptr
, ptr
+ 1);
400 _cups_strcpy(ptr
, ptr
+ 1);
402 else if (*ptr
== '{')
405 * Collection value...
410 for (depth
= 0; *ptr
; ptr
++)
414 else if (*ptr
== '}')
423 else if (*ptr
== '\\' && ptr
[1])
424 _cups_strcpy(ptr
, ptr
+ 1);
430 * Normal space-delimited string...
433 while (*ptr
&& !_cups_isspace(*ptr
))
435 if (*ptr
== '\\' && ptr
[1])
436 _cups_strcpy(ptr
, ptr
+ 1);
446 DEBUG_printf(("2cupsParseOptions: value=\"%s\"", value
));
449 * Skip trailing whitespace...
452 while (_cups_isspace(*ptr
))
456 * Add the string value...
459 num_options
= cupsAddOption(name
, value
, num_options
, options
);
463 * Free the copy of the argument we made and return the number of options
469 DEBUG_printf(("1cupsParseOptions: Returning %d", num_options
));
471 return (num_options
);
476 * 'cupsRemoveOption()' - Remove an option from an option array.
478 * @since CUPS 1.2/macOS 10.5@
481 int /* O - New number of options */
483 const char *name
, /* I - Option name */
484 int num_options
, /* I - Current number of options */
485 cups_option_t
**options
) /* IO - Options */
487 int i
; /* Looping var */
488 cups_option_t
*option
; /* Current option */
491 DEBUG_printf(("2cupsRemoveOption(name=\"%s\", num_options=%d, options=%p)", name
, num_options
, (void *)options
));
494 * Range check input...
497 if (!name
|| num_options
< 1 || !options
)
499 DEBUG_printf(("3cupsRemoveOption: Returning %d", num_options
));
500 return (num_options
);
504 * Loop for the option...
507 for (i
= num_options
, option
= *options
; i
> 0; i
--, option
++)
508 if (!_cups_strcasecmp(name
, option
->name
))
514 * Remove this option from the array...
517 DEBUG_puts("4cupsRemoveOption: Found option, removing it...");
522 _cupsStrFree(option
->name
);
523 _cupsStrFree(option
->value
);
526 memmove(option
, option
+ 1, (size_t)i
* sizeof(cups_option_t
));
530 * Return the new number of options...
533 DEBUG_printf(("3cupsRemoveOption: Returning %d", num_options
));
534 return (num_options
);
539 * '_cupsGet1284Values()' - Get 1284 device ID keys and values.
541 * The returned dictionary is a CUPS option array that can be queried with
542 * cupsGetOption and freed with cupsFreeOptions.
545 int /* O - Number of key/value pairs */
547 const char *device_id
, /* I - IEEE-1284 device ID string */
548 cups_option_t
**values
) /* O - Array of key/value pairs */
550 int num_values
; /* Number of values */
551 char key
[256], /* Key string */
552 value
[256], /* Value string */
553 *ptr
; /* Pointer into key/value */
557 * Range check input...
563 if (!device_id
|| !values
)
567 * Parse the 1284 device ID value into keys and values. The format is
568 * repeating sequences of:
570 * [whitespace]key:value[whitespace];
576 while (_cups_isspace(*device_id
))
582 for (ptr
= key
; *device_id
&& *device_id
!= ':'; device_id
++)
583 if (ptr
< (key
+ sizeof(key
) - 1))
589 while (ptr
> key
&& _cups_isspace(ptr
[-1]))
595 while (_cups_isspace(*device_id
))
601 for (ptr
= value
; *device_id
&& *device_id
!= ';'; device_id
++)
602 if (ptr
< (value
+ sizeof(value
) - 1))
608 while (ptr
> value
&& _cups_isspace(ptr
[-1]))
614 num_values
= cupsAddOption(key
, value
, num_values
, values
);
622 * 'cups_compare_options()' - Compare two options.
625 static int /* O - Result of comparison */
626 cups_compare_options(cups_option_t
*a
, /* I - First option */
627 cups_option_t
*b
) /* I - Second option */
629 return (_cups_strcasecmp(a
->name
, b
->name
));
634 * 'cups_find_option()' - Find an option using a binary search.
637 static int /* O - Index of match */
639 const char *name
, /* I - Option name */
640 int num_options
, /* I - Number of options */
641 cups_option_t
*options
, /* I - Options */
642 int prev
, /* I - Previous index */
643 int *rdiff
) /* O - Difference of match */
645 int left
, /* Low mark for binary search */
646 right
, /* High mark for binary search */
647 current
, /* Current index */
648 diff
; /* Result of comparison */
649 cups_option_t key
; /* Search key */
652 DEBUG_printf(("7cups_find_option(name=\"%s\", num_options=%d, options=%p, prev=%d, rdiff=%p)", name
, num_options
, (void *)options
, prev
, (void *)rdiff
));
655 for (left
= 0; left
< num_options
; left
++)
656 DEBUG_printf(("9cups_find_option: options[%d].name=\"%s\", .value=\"%s\"",
657 left
, options
[left
].name
, options
[left
].value
));
660 key
.name
= (char *)name
;
665 * Start search on either side of previous...
668 if ((diff
= cups_compare_options(&key
, options
+ prev
)) == 0 ||
669 (diff
< 0 && prev
== 0) ||
670 (diff
> 0 && prev
== (num_options
- 1)))
678 * Start with previous on right side...
687 * Start wih previous on left side...
691 right
= num_options
- 1;
697 * Start search in the middle...
701 right
= num_options
- 1;
706 current
= (left
+ right
) / 2;
707 diff
= cups_compare_options(&key
, options
+ current
);
716 while ((right
- left
) > 1);
721 * Check the last 1 or 2 elements...
724 if ((diff
= cups_compare_options(&key
, options
+ left
)) <= 0)
728 diff
= cups_compare_options(&key
, options
+ right
);
734 * Return the closest destination and the difference...