]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/encode.c
Load cups into easysw/current.
[thirdparty/cups.git] / cups / encode.c
CommitLineData
ef416fc2 1/*
bd7854cb 2 * "$Id: encode.c 5128 2006-02-17 18:59:03Z mike $"
ef416fc2 3 *
4 * Option encoding routines for the Common UNIX Printing System (CUPS).
5 *
fa73b229 6 * Copyright 1997-2006 by Easy Software Products.
ef416fc2 7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * This file is subject to the Apple OS-Developed Software exception.
25 *
26 * Contents:
27 *
fa73b229 28 * cupsEncodeOptions() - Encode printer options into IPP attributes.
29 * cupsEncodeOptions2() - Encode printer options into IPP attributes for
30 * a group.
31 * compare_ipp_options() - Compare two IPP options.
ef416fc2 32 */
33
34/*
35 * Include necessary headers...
36 */
37
38#include "cups.h"
fa73b229 39#include "ipp-private.h"
ef416fc2 40#include <stdlib.h>
41#include <ctype.h>
42#include "string.h"
43#include "debug.h"
44
45
46/*
47 * Local list of option names and the value tags they should use...
fa73b229 48 *
49 * **** THIS LIST MUST BE SORTED ****
ef416fc2 50 */
51
52typedef struct
53{
54 const char *name;
55 ipp_tag_t value_tag;
56 ipp_tag_t group_tag;
57} _ipp_option_t;
58
59static const _ipp_option_t ipp_options[] =
60{
61 { "blackplot", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
62 { "brightness", IPP_TAG_INTEGER, IPP_TAG_JOB },
63 { "columns", IPP_TAG_INTEGER, IPP_TAG_JOB },
64 { "copies", IPP_TAG_INTEGER, IPP_TAG_JOB },
65 { "document-format", IPP_TAG_MIMETYPE, IPP_TAG_OPERATION },
66 { "finishings", IPP_TAG_ENUM, IPP_TAG_JOB },
67 { "fitplot", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
68 { "gamma", IPP_TAG_INTEGER, IPP_TAG_JOB },
69 { "hue", IPP_TAG_INTEGER, IPP_TAG_JOB },
70 { "job-k-limit", IPP_TAG_INTEGER, IPP_TAG_JOB },
71 { "job-page-limit", IPP_TAG_INTEGER, IPP_TAG_JOB },
72 { "job-priority", IPP_TAG_INTEGER, IPP_TAG_JOB },
73 { "job-quota-period", IPP_TAG_INTEGER, IPP_TAG_JOB },
bd7854cb 74 { "job-uuid", IPP_TAG_URI, IPP_TAG_JOB },
ef416fc2 75 { "landscape", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
76 { "media", IPP_TAG_KEYWORD, IPP_TAG_JOB },
77 { "mirror", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
78 { "natural-scaling", IPP_TAG_INTEGER, IPP_TAG_JOB },
79 { "notify-charset", IPP_TAG_CHARSET, IPP_TAG_SUBSCRIPTION },
80 { "notify-events", IPP_TAG_KEYWORD, IPP_TAG_SUBSCRIPTION },
81 { "notify-lease-time", IPP_TAG_INTEGER, IPP_TAG_SUBSCRIPTION },
82 { "notify-natural-language", IPP_TAG_LANGUAGE, IPP_TAG_SUBSCRIPTION },
83 { "notify-pull-method", IPP_TAG_KEYWORD, IPP_TAG_SUBSCRIPTION },
84 { "notify-recipient", IPP_TAG_URI, IPP_TAG_SUBSCRIPTION },
85 { "notify-time-interval", IPP_TAG_INTEGER, IPP_TAG_SUBSCRIPTION },
86 { "notify-user-data", IPP_TAG_STRING, IPP_TAG_SUBSCRIPTION },
87 { "number-up", IPP_TAG_INTEGER, IPP_TAG_JOB },
88 { "orientation-requested", IPP_TAG_ENUM, IPP_TAG_JOB },
89 { "page-bottom", IPP_TAG_INTEGER, IPP_TAG_JOB },
90 { "page-left", IPP_TAG_INTEGER, IPP_TAG_JOB },
91 { "page-ranges", IPP_TAG_RANGE, IPP_TAG_JOB },
92 { "page-right", IPP_TAG_INTEGER, IPP_TAG_JOB },
93 { "page-top", IPP_TAG_INTEGER, IPP_TAG_JOB },
94 { "penwidth", IPP_TAG_INTEGER, IPP_TAG_JOB },
95 { "ppi", IPP_TAG_INTEGER, IPP_TAG_JOB },
96 { "prettyprint", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
fa73b229 97 { "printer-info", IPP_TAG_TEXT, IPP_TAG_PRINTER },
98 { "printer-is-accepting-jobs",IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
99 { "printer-is-shared", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
100 { "printer-make-and-model", IPP_TAG_TEXT, IPP_TAG_PRINTER },
101 { "printer-more-info", IPP_TAG_URI, IPP_TAG_PRINTER },
ef416fc2 102 { "printer-resolution", IPP_TAG_RESOLUTION, IPP_TAG_JOB },
fa73b229 103 { "printer-state", IPP_TAG_ENUM, IPP_TAG_PRINTER },
104 { "printer-state-change-time",IPP_TAG_INTEGER, IPP_TAG_PRINTER },
105 { "printer-state-reasons", IPP_TAG_KEYWORD, IPP_TAG_PRINTER },
106 { "printer-type", IPP_TAG_ENUM, IPP_TAG_PRINTER },
ef416fc2 107 { "printer-uri", IPP_TAG_URI, IPP_TAG_OPERATION },
108 { "print-quality", IPP_TAG_ENUM, IPP_TAG_JOB },
fa73b229 109 { "queued-job-count", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 110 { "raw", IPP_TAG_MIMETYPE, IPP_TAG_OPERATION },
111 { "saturation", IPP_TAG_INTEGER, IPP_TAG_JOB },
112 { "scaling", IPP_TAG_INTEGER, IPP_TAG_JOB },
113 { "sides", IPP_TAG_KEYWORD, IPP_TAG_JOB },
114 { "wrap", IPP_TAG_BOOLEAN, IPP_TAG_JOB }
115};
116
117
fa73b229 118/*
119 * Local functions...
120 */
121
122static int compare_ipp_options(_ipp_option_t *a, _ipp_option_t *b);
123
124
ef416fc2 125/*
126 * 'cupsEncodeOptions()' - Encode printer options into IPP attributes.
127 *
128 * This function adds operation, job, and then subscription attributes,
129 * in that order. Use the cupsEncodeOptions2() function to add attributes
130 * for a single group.
131 */
132
133void
134cupsEncodeOptions(ipp_t *ipp, /* I - Request to add to */
135 int num_options, /* I - Number of options */
136 cups_option_t *options) /* I - Options */
137{
138 DEBUG_printf(("cupsEncodeOptions(%p, %d, %p)\n", ipp, num_options, options));
139
140 /*
141 * Add the options in the proper groups & order...
142 */
143
144 cupsEncodeOptions2(ipp, num_options, options, IPP_TAG_OPERATION);
145 cupsEncodeOptions2(ipp, num_options, options, IPP_TAG_JOB);
146 cupsEncodeOptions2(ipp, num_options, options, IPP_TAG_SUBSCRIPTION);
147}
148
149
150/*
151 * 'cupsEncodeOptions2()' - Encode printer options into IPP attributes for a group.
152 *
153 * This function only adds attributes for a single group. Call this
154 * function multiple times for each group, or use cupsEncodeOptions()
155 * to add the standard groups.
156 *
157 * @since CUPS 1.2@
158 */
159
160void
161cupsEncodeOptions2(
162 ipp_t *ipp, /* I - Request to add to */
163 int num_options, /* I - Number of options */
164 cups_option_t *options, /* I - Options */
165 ipp_tag_t group_tag) /* I - Group to encode */
166{
167 int i, j; /* Looping vars */
168 int count; /* Number of values */
169 char *s, /* Pointer into option value */
170 *val, /* Pointer to option value */
171 *copy, /* Copy of option value */
172 *sep; /* Option separator */
173 ipp_attribute_t *attr; /* IPP attribute */
174 ipp_tag_t value_tag; /* IPP value tag */
175
176
177 DEBUG_printf(("cupsEncodeOptions2(ipp=%p, num_options=%d, options=%p, group_tag=%x)\n",
178 ipp, num_options, options, group_tag));
179
180 /*
181 * Range check input...
182 */
183
184 if (ipp == NULL || num_options < 1 || options == NULL)
185 return;
186
187 /*
188 * Do special handling for the document-format/raw options...
189 */
190
191 if (group_tag == IPP_TAG_OPERATION)
192 {
193 /*
194 * Handle the document format stuff first...
195 */
196
197 if ((val = (char *)cupsGetOption("document-format", num_options, options)) != NULL)
198 ippAddString(ipp, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format",
199 NULL, val);
200 else if (cupsGetOption("raw", num_options, options))
201 ippAddString(ipp, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format",
202 NULL, "application/vnd.cups-raw");
203 else
204 ippAddString(ipp, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format",
205 NULL, "application/octet-stream");
206 }
207
208 /*
209 * Then loop through the options...
210 */
211
212 for (i = 0; i < num_options; i ++)
213 {
fa73b229 214 _ipp_option_t key, /* Search key */
215 *match; /* Matching attribute */
216
217
ef416fc2 218 /*
219 * Skip document format options that are handled above...
220 */
221
222 if (!strcasecmp(options[i].name, "raw") ||
223 !strcasecmp(options[i].name, "document-format") ||
224 !options[i].name[0])
225 continue;
226
227 /*
228 * Figure out the proper value and group tags for this option...
229 */
230
fa73b229 231 key.name = options[i].name;
232 match = (_ipp_option_t *)bsearch(&key, ipp_options,
233 sizeof(ipp_options) /
234 sizeof(ipp_options[0]),
235 sizeof(ipp_options[0]),
236 (int (*)(const void *,
237 const void *))
238 compare_ipp_options);
ef416fc2 239
fa73b229 240 if (match)
ef416fc2 241 {
fa73b229 242 if (match->group_tag != group_tag)
ef416fc2 243 continue;
244
fa73b229 245 value_tag = match->value_tag;
ef416fc2 246 }
247 else if (group_tag != IPP_TAG_JOB)
248 continue;
249 else if (!strcasecmp(options[i].value, "true") ||
250 !strcasecmp(options[i].value, "false"))
251 value_tag = IPP_TAG_BOOLEAN;
252 else
253 value_tag = IPP_TAG_NAME;
254
255 /*
256 * Count the number of values...
257 */
258
259 for (count = 1, sep = options[i].value; *sep; sep ++)
260 {
261 if (*sep == '\'')
262 {
263 /*
264 * Skip quoted option value...
265 */
266
267 sep ++;
268
269 while (*sep && *sep != '\'')
270 sep ++;
271
272 if (!*sep)
273 sep --;
274 }
275 else if (*sep == '\"')
276 {
277 /*
278 * Skip quoted option value...
279 */
280
281 sep ++;
282
283 while (*sep && *sep != '\"')
284 sep ++;
285
286 if (!*sep)
287 sep --;
288 }
289 else if (*sep == ',')
290 count ++;
291 else if (*sep == '\\' && sep[1])
292 sep ++;
293 }
294
295 DEBUG_printf(("cupsEncodeOptions2: option = \'%s\', count = %d\n",
296 options[i].name, count));
297
298 /*
299 * Allocate memory for the attribute values...
300 */
301
302 if ((attr = _ipp_add_attr(ipp, count)) == NULL)
303 {
304 /*
305 * Ran out of memory!
306 */
307
308 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for attributes!");
309 return;
310 }
311
312 /*
313 * Now figure out what type of value we have...
314 */
315
316 attr->group_tag = group_tag;
317 attr->value_tag = value_tag;
318
319 /*
320 * Copy the name over...
321 */
322
323 if ((attr->name = strdup(options[i].name)) == NULL)
324 {
325 /*
326 * Ran out of memory!
327 */
328
329 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for name!");
330 return;
331 }
332
333 if (count > 1)
334 {
335 /*
336 * Make a copy of the value we can fiddle with...
337 */
338
339 if ((copy = strdup(options[i].value)) == NULL)
340 {
341 /*
342 * Ran out of memory!
343 */
344
345 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for value copy!");
346 return;
347 }
348
349 val = copy;
350 }
351 else
352 {
353 /*
354 * Since we have a single value, use the value directly...
355 */
356
357 val = options[i].value;
358 copy = NULL;
359 }
360
361 /*
362 * Scan the value string for values...
363 */
364
365 for (j = 0; j < count; val = sep, j ++)
366 {
367 /*
368 * Find the end of this value and mark it if needed...
369 */
370
371 if ((sep = strchr(val, ',')) != NULL)
372 *sep++ = '\0';
373 else
374 sep = val + strlen(val);
375
376 /*
377 * Copy the option value(s) over as needed by the type...
378 */
379
380 switch (attr->value_tag)
381 {
382 case IPP_TAG_INTEGER :
383 case IPP_TAG_ENUM :
384 /*
385 * Integer/enumeration value...
386 */
387
388 attr->values[j].integer = strtol(val, &s, 0);
389
390 DEBUG_printf(("cupsEncodeOptions2: Added integer option value %d...\n",
391 attr->values[j].integer));
392 break;
393
394 case IPP_TAG_BOOLEAN :
395 if (!strcasecmp(val, "true") ||
396 !strcasecmp(val, "on") ||
397 !strcasecmp(val, "yes"))
398 {
399 /*
400 * Boolean value - true...
401 */
402
403 attr->values[j].boolean = 1;
404
405 DEBUG_puts("cupsEncodeOptions2: Added boolean true value...");
406 }
407 else
408 {
409 /*
410 * Boolean value - false...
411 */
412
413 attr->values[j].boolean = 0;
414
415 DEBUG_puts("cupsEncodeOptions2: Added boolean false value...");
416 }
417 break;
418
419 case IPP_TAG_RANGE :
420 /*
421 * Range...
422 */
423
424 if (*val == '-')
425 {
426 attr->values[j].range.lower = 1;
427 s = val;
428 }
429 else
430 attr->values[j].range.lower = strtol(val, &s, 0);
431
432 if (*s == '-')
433 {
434 if (s[1])
435 attr->values[j].range.upper = strtol(s + 1, NULL, 0);
436 else
437 attr->values[j].range.upper = 2147483647;
438 }
439 else
440 attr->values[j].range.upper = attr->values[j].range.lower;
441
442 DEBUG_printf(("cupsEncodeOptions2: Added range option value %d-%d...\n",
443 attr->values[j].range.lower,
444 attr->values[j].range.upper));
445 break;
446
447 case IPP_TAG_RESOLUTION :
448 /*
449 * Resolution...
450 */
451
452 attr->values[j].resolution.xres = strtol(val, &s, 0);
453
454 if (*s == 'x')
455 attr->values[j].resolution.yres = strtol(s + 1, &s, 0);
456 else
457 attr->values[j].resolution.yres = attr->values[j].resolution.xres;
458
459 if (strcasecmp(s, "dpc") == 0)
460 attr->values[j].resolution.units = IPP_RES_PER_CM;
461 else
462 attr->values[j].resolution.units = IPP_RES_PER_INCH;
463
464 DEBUG_printf(("cupsEncodeOptions2: Added resolution option value %s...\n",
465 val));
466 break;
467
468 case IPP_TAG_STRING :
469 /*
470 * octet-string
471 */
472
473 attr->values[j].unknown.length = strlen(val);
474 attr->values[j].unknown.data = strdup(val);
475
476 DEBUG_printf(("cupsEncodeOptions2: Added octet-string value \"%s\"...\n",
477 attr->values[j].unknown.data));
478 break;
479
480 default :
481 if ((attr->values[j].string.text = strdup(val)) == NULL)
482 {
483 /*
484 * Ran out of memory!
485 */
486
487 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for string!");
488 return;
489 }
490
491 DEBUG_printf(("cupsEncodeOptions2: Added string value \"%s\"...\n",
492 val));
493 break;
494 }
495 }
496 }
497}
498
499
500/*
fa73b229 501 * 'compare_ipp_options()' - Compare two IPP options.
502 */
503
504static int /* O - Result of comparison */
505compare_ipp_options(_ipp_option_t *a, /* I - First option */
506 _ipp_option_t *b) /* I - Second option */
507{
508 return (strcmp(a->name, b->name));
509}
510
511
512/*
bd7854cb 513 * End of "$Id: encode.c 5128 2006-02-17 18:59:03Z mike $".
ef416fc2 514 */