]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
5a738aea | 2 | * "$Id: options.c 7278 2008-01-31 01:23:09Z mike $" |
ef416fc2 | 3 | * |
4 | * Option routines for the Common UNIX Printing System (CUPS). | |
5 | * | |
91c84a35 | 6 | * Copyright 2007-2008 by Apple Inc. |
f7deaa1a | 7 | * Copyright 1997-2007 by Easy Software Products. |
ef416fc2 | 8 | * |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 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/". | |
ef416fc2 | 14 | * |
15 | * This file is subject to the Apple OS-Developed Software exception. | |
16 | * | |
17 | * Contents: | |
18 | * | |
5a738aea MS |
19 | * cupsAddOption() - Add an option to an option array. |
20 | * cupsFreeOptions() - Free all memory used by options. | |
21 | * cupsGetOption() - Get an option value. | |
22 | * cupsParseOptions() - Parse options from a command-line argument. | |
23 | * cupsRemoveOption() - Remove an option from an option array. | |
ef416fc2 | 24 | */ |
25 | ||
26 | /* | |
27 | * Include necessary headers... | |
28 | */ | |
29 | ||
30 | #include "cups.h" | |
31 | #include <stdlib.h> | |
32 | #include <ctype.h> | |
33 | #include "string.h" | |
34 | #include "debug.h" | |
35 | ||
36 | ||
37 | /* | |
38 | * 'cupsAddOption()' - Add an option to an option array. | |
5a738aea MS |
39 | * |
40 | * New option arrays can be initialized simply by passing 0 for the | |
41 | * "num_options" parameter. | |
ef416fc2 | 42 | */ |
43 | ||
5a738aea MS |
44 | int /* O - Number of options */ |
45 | cupsAddOption(const char *name, /* I - Name of option */ | |
46 | const char *value, /* I - Value of option */ | |
47 | int num_options,/* I - Number of options */ | |
ef416fc2 | 48 | cups_option_t **options) /* IO - Pointer to options */ |
49 | { | |
50 | int i; /* Looping var */ | |
51 | cups_option_t *temp; /* Pointer to new option */ | |
52 | ||
53 | ||
54 | if (name == NULL || !name[0] || value == NULL || | |
55 | options == NULL || num_options < 0) | |
56 | return (num_options); | |
57 | ||
58 | /* | |
59 | * Look for an existing option with the same name... | |
60 | */ | |
61 | ||
62 | for (i = 0, temp = *options; i < num_options; i ++, temp ++) | |
2e4ff8af | 63 | if (!strcasecmp(temp->name, name)) |
ef416fc2 | 64 | break; |
65 | ||
66 | if (i >= num_options) | |
67 | { | |
68 | /* | |
69 | * No matching option name... | |
70 | */ | |
71 | ||
72 | if (num_options == 0) | |
73 | temp = (cups_option_t *)malloc(sizeof(cups_option_t)); | |
74 | else | |
75 | temp = (cups_option_t *)realloc(*options, sizeof(cups_option_t) * | |
76 | (num_options + 1)); | |
77 | ||
78 | if (temp == NULL) | |
79 | return (0); | |
80 | ||
81 | *options = temp; | |
82 | temp += num_options; | |
2e4ff8af | 83 | temp->name = _cupsStrAlloc(name); |
ef416fc2 | 84 | num_options ++; |
85 | } | |
86 | else | |
87 | { | |
88 | /* | |
89 | * Match found; free the old value... | |
90 | */ | |
91 | ||
2e4ff8af | 92 | _cupsStrFree(temp->value); |
ef416fc2 | 93 | } |
94 | ||
2e4ff8af | 95 | temp->value = _cupsStrAlloc(value); |
ef416fc2 | 96 | |
97 | return (num_options); | |
98 | } | |
99 | ||
100 | ||
101 | /* | |
102 | * 'cupsFreeOptions()' - Free all memory used by options. | |
103 | */ | |
104 | ||
105 | void | |
106 | cupsFreeOptions( | |
107 | int num_options, /* I - Number of options */ | |
108 | cups_option_t *options) /* I - Pointer to options */ | |
109 | { | |
110 | int i; /* Looping var */ | |
111 | ||
112 | ||
113 | if (num_options <= 0 || options == NULL) | |
114 | return; | |
115 | ||
116 | for (i = 0; i < num_options; i ++) | |
117 | { | |
2e4ff8af MS |
118 | _cupsStrFree(options[i].name); |
119 | _cupsStrFree(options[i].value); | |
ef416fc2 | 120 | } |
121 | ||
122 | free(options); | |
123 | } | |
124 | ||
125 | ||
126 | /* | |
127 | * 'cupsGetOption()' - Get an option value. | |
128 | */ | |
129 | ||
5a738aea | 130 | const char * /* O - Option value or @code NULL@ */ |
ef416fc2 | 131 | cupsGetOption(const char *name, /* I - Name of option */ |
132 | int num_options,/* I - Number of options */ | |
133 | cups_option_t *options) /* I - Options */ | |
134 | { | |
135 | int i; /* Looping var */ | |
136 | ||
137 | ||
138 | if (name == NULL || num_options <= 0 || options == NULL) | |
139 | return (NULL); | |
140 | ||
141 | for (i = 0; i < num_options; i ++) | |
142 | if (strcasecmp(options[i].name, name) == 0) | |
143 | return (options[i].value); | |
144 | ||
145 | return (NULL); | |
146 | } | |
147 | ||
148 | ||
ef416fc2 | 149 | /* |
b423cd4c | 150 | * 'cupsParseOptions()' - Parse options from a command-line argument. |
151 | * | |
152 | * This function converts space-delimited name/value pairs according | |
153 | * to the PAPI text option ABNF specification. Collection values | |
154 | * ("name={a=... b=... c=...}") are stored with the curley brackets | |
5a738aea MS |
155 | * intact - use @code cupsParseOptions@ on the value to extract the |
156 | * collection attributes. | |
b423cd4c | 157 | */ |
158 | ||
159 | int /* O - Number of options found */ | |
160 | cupsParseOptions( | |
161 | const char *arg, /* I - Argument to parse */ | |
162 | int num_options, /* I - Number of options */ | |
163 | cups_option_t **options) /* O - Options found */ | |
164 | { | |
165 | char *copyarg, /* Copy of input string */ | |
166 | *ptr, /* Pointer into string */ | |
167 | *name, /* Pointer to name */ | |
5a738aea MS |
168 | *value, /* Pointer to value */ |
169 | quote; /* Quote character */ | |
b423cd4c | 170 | |
171 | ||
91c84a35 MS |
172 | /* |
173 | * Range check input... | |
174 | */ | |
175 | ||
176 | if (!arg) | |
177 | return (num_options); | |
178 | ||
179 | if (!options || num_options < 0) | |
b423cd4c | 180 | return (0); |
181 | ||
182 | /* | |
183 | * Make a copy of the argument string and then divide it up... | |
184 | */ | |
185 | ||
91c84a35 MS |
186 | if ((copyarg = strdup(arg)) == NULL) |
187 | return (num_options); | |
188 | ||
189 | ptr = copyarg; | |
b423cd4c | 190 | |
191 | /* | |
192 | * Skip leading spaces... | |
193 | */ | |
194 | ||
195 | while (isspace(*ptr & 255)) | |
196 | ptr ++; | |
197 | ||
198 | /* | |
199 | * Loop through the string... | |
200 | */ | |
201 | ||
202 | while (*ptr != '\0') | |
203 | { | |
204 | /* | |
205 | * Get the name up to a SPACE, =, or end-of-string... | |
206 | */ | |
207 | ||
208 | name = ptr; | |
5a738aea | 209 | while (!isspace(*ptr & 255) && *ptr != '=' && *ptr) |
b423cd4c | 210 | ptr ++; |
211 | ||
212 | /* | |
213 | * Avoid an empty name... | |
214 | */ | |
215 | ||
216 | if (ptr == name) | |
217 | break; | |
218 | ||
219 | /* | |
220 | * Skip trailing spaces... | |
221 | */ | |
222 | ||
223 | while (isspace(*ptr & 255)) | |
224 | *ptr++ = '\0'; | |
225 | ||
226 | if (*ptr != '=') | |
227 | { | |
228 | /* | |
5a738aea | 229 | * Boolean option... |
b423cd4c | 230 | */ |
231 | ||
5a738aea | 232 | if (!strncasecmp(name, "no", 2)) |
b423cd4c | 233 | num_options = cupsAddOption(name + 2, "false", num_options, |
234 | options); | |
235 | else | |
236 | num_options = cupsAddOption(name, "true", num_options, options); | |
237 | ||
238 | continue; | |
239 | } | |
240 | ||
241 | /* | |
242 | * Remove = and parse the value... | |
243 | */ | |
244 | ||
245 | *ptr++ = '\0'; | |
246 | ||
5a738aea | 247 | if (*ptr == '\'' || *ptr == '\"') |
b423cd4c | 248 | { |
249 | /* | |
250 | * Quoted string constant... | |
251 | */ | |
252 | ||
5a738aea | 253 | quote = *ptr++; |
b423cd4c | 254 | value = ptr; |
255 | ||
5a738aea | 256 | while (*ptr != quote && *ptr) |
b423cd4c | 257 | { |
5a738aea | 258 | if (*ptr == '\\' && ptr[1]) |
b423cd4c | 259 | _cups_strcpy(ptr, ptr + 1); |
260 | ||
261 | ptr ++; | |
262 | } | |
263 | ||
264 | if (*ptr != '\0') | |
265 | *ptr++ = '\0'; | |
266 | } | |
267 | else if (*ptr == '{') | |
268 | { | |
269 | /* | |
270 | * Collection value... | |
271 | */ | |
272 | ||
273 | int depth; | |
274 | ||
275 | value = ptr; | |
276 | ||
277 | for (depth = 1; *ptr; ptr ++) | |
278 | if (*ptr == '{') | |
279 | depth ++; | |
280 | else if (*ptr == '}') | |
281 | { | |
282 | depth --; | |
283 | if (!depth) | |
284 | { | |
285 | ptr ++; | |
286 | ||
287 | if (*ptr != ',') | |
288 | break; | |
289 | } | |
290 | } | |
5a738aea | 291 | else if (*ptr == '\\' && ptr[1]) |
b423cd4c | 292 | _cups_strcpy(ptr, ptr + 1); |
293 | ||
294 | if (*ptr != '\0') | |
295 | *ptr++ = '\0'; | |
296 | } | |
297 | else | |
298 | { | |
299 | /* | |
300 | * Normal space-delimited string... | |
301 | */ | |
302 | ||
303 | value = ptr; | |
304 | ||
5a738aea | 305 | while (!isspace(*ptr & 255) && *ptr) |
b423cd4c | 306 | { |
5a738aea | 307 | if (*ptr == '\\' && ptr[1]) |
b423cd4c | 308 | _cups_strcpy(ptr, ptr + 1); |
309 | ||
310 | ptr ++; | |
311 | } | |
312 | } | |
313 | ||
314 | /* | |
315 | * Skip trailing whitespace... | |
316 | */ | |
317 | ||
318 | while (isspace(*ptr & 255)) | |
319 | *ptr++ = '\0'; | |
320 | ||
321 | /* | |
322 | * Add the string value... | |
323 | */ | |
324 | ||
325 | num_options = cupsAddOption(name, value, num_options, options); | |
326 | } | |
327 | ||
328 | /* | |
329 | * Free the copy of the argument we made and return the number of options | |
330 | * found. | |
331 | */ | |
332 | ||
333 | free(copyarg); | |
334 | ||
335 | return (num_options); | |
336 | } | |
337 | ||
338 | ||
339 | /* | |
f7deaa1a | 340 | * 'cupsRemoveOption()' - Remove an option from an option array. |
b423cd4c | 341 | * |
342 | * @since CUPS 1.2@ | |
343 | */ | |
344 | ||
345 | int /* O - New number of options */ | |
346 | cupsRemoveOption( | |
347 | const char *name, /* I - Option name */ | |
348 | int num_options, /* I - Current number of options */ | |
349 | cups_option_t **options) /* IO - Options */ | |
350 | { | |
351 | int i; /* Looping var */ | |
352 | cups_option_t *option; /* Current option */ | |
353 | ||
354 | ||
355 | /* | |
356 | * Range check input... | |
357 | */ | |
358 | ||
359 | if (!name || num_options < 1 || !options) | |
360 | return (num_options); | |
361 | ||
362 | /* | |
363 | * Loop for the option... | |
364 | */ | |
365 | ||
366 | for (i = num_options, option = *options; i > 0; i --, option ++) | |
367 | if (!strcasecmp(name, option->name)) | |
368 | break; | |
369 | ||
370 | if (i) | |
371 | { | |
372 | /* | |
373 | * Remove this option from the array... | |
374 | */ | |
375 | ||
376 | num_options --; | |
377 | i --; | |
378 | ||
2e4ff8af MS |
379 | _cupsStrFree(option->name); |
380 | _cupsStrFree(option->value); | |
b423cd4c | 381 | |
382 | if (i > 0) | |
f7deaa1a | 383 | memmove(option, option + 1, i * sizeof(cups_option_t)); |
b423cd4c | 384 | } |
385 | ||
386 | /* | |
387 | * Return the new number of options... | |
388 | */ | |
389 | ||
390 | return (num_options); | |
391 | } | |
392 | ||
393 | ||
09a101d6 | 394 | /* |
5a738aea | 395 | * End of "$Id: options.c 7278 2008-01-31 01:23:09Z mike $". |
ef416fc2 | 396 | */ |