]> 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/*
757d2cad 2 * "$Id: encode.c 5230 2006-03-05 17:01:51Z 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 },
b423cd4c 62 { "blackplot-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
ef416fc2 63 { "brightness", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 64 { "brightness-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 65 { "columns", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 66 { "columns-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 67 { "copies", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 68 { "copies-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 69 { "document-format", IPP_TAG_MIMETYPE, IPP_TAG_OPERATION },
70 { "finishings", IPP_TAG_ENUM, IPP_TAG_JOB },
b423cd4c 71 { "finishings-default", IPP_TAG_ENUM, IPP_TAG_PRINTER },
ef416fc2 72 { "fitplot", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
b423cd4c 73 { "fitplot-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
ef416fc2 74 { "gamma", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 75 { "gamma-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 76 { "hue", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 77 { "hue-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 78 { "job-k-limit", IPP_TAG_INTEGER, IPP_TAG_JOB },
79 { "job-page-limit", IPP_TAG_INTEGER, IPP_TAG_JOB },
80 { "job-priority", IPP_TAG_INTEGER, IPP_TAG_JOB },
81 { "job-quota-period", IPP_TAG_INTEGER, IPP_TAG_JOB },
bd7854cb 82 { "job-uuid", IPP_TAG_URI, IPP_TAG_JOB },
ef416fc2 83 { "landscape", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
84 { "media", IPP_TAG_KEYWORD, IPP_TAG_JOB },
85 { "mirror", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
b423cd4c 86 { "mirror-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
ef416fc2 87 { "natural-scaling", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 88 { "natural-scaling-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 89 { "notify-charset", IPP_TAG_CHARSET, IPP_TAG_SUBSCRIPTION },
90 { "notify-events", IPP_TAG_KEYWORD, IPP_TAG_SUBSCRIPTION },
91 { "notify-lease-time", IPP_TAG_INTEGER, IPP_TAG_SUBSCRIPTION },
92 { "notify-natural-language", IPP_TAG_LANGUAGE, IPP_TAG_SUBSCRIPTION },
93 { "notify-pull-method", IPP_TAG_KEYWORD, IPP_TAG_SUBSCRIPTION },
94 { "notify-recipient", IPP_TAG_URI, IPP_TAG_SUBSCRIPTION },
95 { "notify-time-interval", IPP_TAG_INTEGER, IPP_TAG_SUBSCRIPTION },
96 { "notify-user-data", IPP_TAG_STRING, IPP_TAG_SUBSCRIPTION },
97 { "number-up", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 98 { "number-up-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 99 { "orientation-requested", IPP_TAG_ENUM, IPP_TAG_JOB },
b423cd4c 100 { "orientation-requested-default", IPP_TAG_ENUM, IPP_TAG_PRINTER },
ef416fc2 101 { "page-bottom", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 102 { "page-bottom-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 103 { "page-left", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 104 { "page-left-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 105 { "page-ranges", IPP_TAG_RANGE, IPP_TAG_JOB },
b423cd4c 106 { "page-ranges-default", IPP_TAG_RANGE, IPP_TAG_PRINTER },
ef416fc2 107 { "page-right", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 108 { "page-right-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 109 { "page-top", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 110 { "page-top-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 111 { "penwidth", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 112 { "penwidth-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 113 { "ppi", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 114 { "ppi-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 115 { "prettyprint", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
b423cd4c 116 { "prettyprint-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
fa73b229 117 { "printer-info", IPP_TAG_TEXT, IPP_TAG_PRINTER },
118 { "printer-is-accepting-jobs",IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
119 { "printer-is-shared", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
120 { "printer-make-and-model", IPP_TAG_TEXT, IPP_TAG_PRINTER },
121 { "printer-more-info", IPP_TAG_URI, IPP_TAG_PRINTER },
ef416fc2 122 { "printer-resolution", IPP_TAG_RESOLUTION, IPP_TAG_JOB },
fa73b229 123 { "printer-state", IPP_TAG_ENUM, IPP_TAG_PRINTER },
124 { "printer-state-change-time",IPP_TAG_INTEGER, IPP_TAG_PRINTER },
125 { "printer-state-reasons", IPP_TAG_KEYWORD, IPP_TAG_PRINTER },
126 { "printer-type", IPP_TAG_ENUM, IPP_TAG_PRINTER },
ef416fc2 127 { "printer-uri", IPP_TAG_URI, IPP_TAG_OPERATION },
128 { "print-quality", IPP_TAG_ENUM, IPP_TAG_JOB },
b423cd4c 129 { "print-quality-default", IPP_TAG_ENUM, IPP_TAG_PRINTER },
fa73b229 130 { "queued-job-count", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 131 { "raw", IPP_TAG_MIMETYPE, IPP_TAG_OPERATION },
b423cd4c 132 { "resolution", IPP_TAG_RESOLUTION, IPP_TAG_JOB },
133 { "resolution-default", IPP_TAG_RESOLUTION, IPP_TAG_PRINTER },
ef416fc2 134 { "saturation", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 135 { "saturation-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 136 { "scaling", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 137 { "scaling-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 138 { "sides", IPP_TAG_KEYWORD, IPP_TAG_JOB },
b423cd4c 139 { "sides-default", IPP_TAG_KEYWORD, IPP_TAG_PRINTER },
140 { "wrap", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
141 { "wrap-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER }
ef416fc2 142};
143
144
fa73b229 145/*
146 * Local functions...
147 */
148
149static int compare_ipp_options(_ipp_option_t *a, _ipp_option_t *b);
150
151
ef416fc2 152/*
153 * 'cupsEncodeOptions()' - Encode printer options into IPP attributes.
154 *
155 * This function adds operation, job, and then subscription attributes,
156 * in that order. Use the cupsEncodeOptions2() function to add attributes
157 * for a single group.
158 */
159
160void
161cupsEncodeOptions(ipp_t *ipp, /* I - Request to add to */
162 int num_options, /* I - Number of options */
163 cups_option_t *options) /* I - Options */
164{
165 DEBUG_printf(("cupsEncodeOptions(%p, %d, %p)\n", ipp, num_options, options));
166
167 /*
168 * Add the options in the proper groups & order...
169 */
170
171 cupsEncodeOptions2(ipp, num_options, options, IPP_TAG_OPERATION);
172 cupsEncodeOptions2(ipp, num_options, options, IPP_TAG_JOB);
173 cupsEncodeOptions2(ipp, num_options, options, IPP_TAG_SUBSCRIPTION);
174}
175
176
177/*
178 * 'cupsEncodeOptions2()' - Encode printer options into IPP attributes for a group.
179 *
180 * This function only adds attributes for a single group. Call this
181 * function multiple times for each group, or use cupsEncodeOptions()
182 * to add the standard groups.
183 *
184 * @since CUPS 1.2@
185 */
186
187void
188cupsEncodeOptions2(
189 ipp_t *ipp, /* I - Request to add to */
190 int num_options, /* I - Number of options */
191 cups_option_t *options, /* I - Options */
192 ipp_tag_t group_tag) /* I - Group to encode */
193{
194 int i, j; /* Looping vars */
195 int count; /* Number of values */
196 char *s, /* Pointer into option value */
197 *val, /* Pointer to option value */
198 *copy, /* Copy of option value */
199 *sep; /* Option separator */
200 ipp_attribute_t *attr; /* IPP attribute */
201 ipp_tag_t value_tag; /* IPP value tag */
b423cd4c 202 cups_option_t *option; /* Current option */
ef416fc2 203
204
b423cd4c 205 DEBUG_printf(("cupsEncodeOptions2(ipp=%p, num_options=%d, options=%p, "
206 "group_tag=%x)\n", ipp, num_options, options, group_tag));
ef416fc2 207
208 /*
209 * Range check input...
210 */
211
b423cd4c 212 if (!ipp || num_options < 1 || !options)
ef416fc2 213 return;
214
215 /*
216 * Do special handling for the document-format/raw options...
217 */
218
219 if (group_tag == IPP_TAG_OPERATION)
220 {
221 /*
222 * Handle the document format stuff first...
223 */
224
225 if ((val = (char *)cupsGetOption("document-format", num_options, options)) != NULL)
226 ippAddString(ipp, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format",
227 NULL, val);
228 else if (cupsGetOption("raw", num_options, options))
229 ippAddString(ipp, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format",
230 NULL, "application/vnd.cups-raw");
231 else
232 ippAddString(ipp, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format",
233 NULL, "application/octet-stream");
234 }
235
236 /*
237 * Then loop through the options...
238 */
239
b423cd4c 240 for (i = num_options, option = options; i > 0; i --, option ++)
ef416fc2 241 {
fa73b229 242 _ipp_option_t key, /* Search key */
243 *match; /* Matching attribute */
244
245
ef416fc2 246 /*
247 * Skip document format options that are handled above...
248 */
249
b423cd4c 250 if (!strcasecmp(option->name, "raw") ||
251 !strcasecmp(option->name, "document-format") ||
252 !option->name[0])
ef416fc2 253 continue;
254
255 /*
256 * Figure out the proper value and group tags for this option...
257 */
258
b423cd4c 259 key.name = option->name;
fa73b229 260 match = (_ipp_option_t *)bsearch(&key, ipp_options,
261 sizeof(ipp_options) /
262 sizeof(ipp_options[0]),
263 sizeof(ipp_options[0]),
264 (int (*)(const void *,
265 const void *))
266 compare_ipp_options);
ef416fc2 267
fa73b229 268 if (match)
ef416fc2 269 {
fa73b229 270 if (match->group_tag != group_tag)
ef416fc2 271 continue;
272
fa73b229 273 value_tag = match->value_tag;
ef416fc2 274 }
ef416fc2 275 else
b423cd4c 276 {
277 int namelen; /* Length of name */
278
279
280 namelen = strlen(option->name);
281
282 if (namelen < 9 || strcmp(option->name + namelen - 8, "-default"))
283 {
284 if (group_tag != IPP_TAG_JOB)
285 continue;
286 }
287 else if (group_tag != IPP_TAG_PRINTER)
288 continue;
289
290 if (!strcasecmp(option->value, "true") ||
291 !strcasecmp(option->value, "false"))
292 value_tag = IPP_TAG_BOOLEAN;
293 else
294 value_tag = IPP_TAG_NAME;
295 }
ef416fc2 296
297 /*
298 * Count the number of values...
299 */
300
b423cd4c 301 for (count = 1, sep = option->value; *sep; sep ++)
ef416fc2 302 {
303 if (*sep == '\'')
304 {
305 /*
306 * Skip quoted option value...
307 */
308
309 sep ++;
310
311 while (*sep && *sep != '\'')
312 sep ++;
313
314 if (!*sep)
315 sep --;
316 }
317 else if (*sep == '\"')
318 {
319 /*
320 * Skip quoted option value...
321 */
322
323 sep ++;
324
325 while (*sep && *sep != '\"')
326 sep ++;
327
328 if (!*sep)
329 sep --;
330 }
331 else if (*sep == ',')
332 count ++;
333 else if (*sep == '\\' && sep[1])
334 sep ++;
335 }
336
337 DEBUG_printf(("cupsEncodeOptions2: option = \'%s\', count = %d\n",
b423cd4c 338 option->name, count));
ef416fc2 339
340 /*
341 * Allocate memory for the attribute values...
342 */
343
757d2cad 344 if ((attr = _ippAddAttr(ipp, count)) == NULL)
ef416fc2 345 {
346 /*
347 * Ran out of memory!
348 */
349
350 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for attributes!");
351 return;
352 }
353
354 /*
355 * Now figure out what type of value we have...
356 */
357
358 attr->group_tag = group_tag;
359 attr->value_tag = value_tag;
360
361 /*
362 * Copy the name over...
363 */
364
b423cd4c 365 if ((attr->name = strdup(option->name)) == NULL)
ef416fc2 366 {
367 /*
368 * Ran out of memory!
369 */
370
371 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for name!");
372 return;
373 }
374
375 if (count > 1)
376 {
377 /*
378 * Make a copy of the value we can fiddle with...
379 */
380
b423cd4c 381 if ((copy = strdup(option->value)) == NULL)
ef416fc2 382 {
383 /*
384 * Ran out of memory!
385 */
386
387 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for value copy!");
388 return;
389 }
390
391 val = copy;
392 }
393 else
394 {
395 /*
396 * Since we have a single value, use the value directly...
397 */
398
b423cd4c 399 val = option->value;
ef416fc2 400 copy = NULL;
401 }
402
403 /*
404 * Scan the value string for values...
405 */
406
407 for (j = 0; j < count; val = sep, j ++)
408 {
409 /*
410 * Find the end of this value and mark it if needed...
411 */
412
413 if ((sep = strchr(val, ',')) != NULL)
414 *sep++ = '\0';
415 else
416 sep = val + strlen(val);
417
418 /*
419 * Copy the option value(s) over as needed by the type...
420 */
421
422 switch (attr->value_tag)
423 {
424 case IPP_TAG_INTEGER :
425 case IPP_TAG_ENUM :
426 /*
427 * Integer/enumeration value...
428 */
429
430 attr->values[j].integer = strtol(val, &s, 0);
431
432 DEBUG_printf(("cupsEncodeOptions2: Added integer option value %d...\n",
433 attr->values[j].integer));
434 break;
435
436 case IPP_TAG_BOOLEAN :
437 if (!strcasecmp(val, "true") ||
438 !strcasecmp(val, "on") ||
439 !strcasecmp(val, "yes"))
440 {
441 /*
442 * Boolean value - true...
443 */
444
445 attr->values[j].boolean = 1;
446
447 DEBUG_puts("cupsEncodeOptions2: Added boolean true value...");
448 }
449 else
450 {
451 /*
452 * Boolean value - false...
453 */
454
455 attr->values[j].boolean = 0;
456
457 DEBUG_puts("cupsEncodeOptions2: Added boolean false value...");
458 }
459 break;
460
461 case IPP_TAG_RANGE :
462 /*
463 * Range...
464 */
465
466 if (*val == '-')
467 {
468 attr->values[j].range.lower = 1;
469 s = val;
470 }
471 else
472 attr->values[j].range.lower = strtol(val, &s, 0);
473
474 if (*s == '-')
475 {
476 if (s[1])
477 attr->values[j].range.upper = strtol(s + 1, NULL, 0);
478 else
479 attr->values[j].range.upper = 2147483647;
480 }
481 else
482 attr->values[j].range.upper = attr->values[j].range.lower;
483
484 DEBUG_printf(("cupsEncodeOptions2: Added range option value %d-%d...\n",
485 attr->values[j].range.lower,
486 attr->values[j].range.upper));
487 break;
488
489 case IPP_TAG_RESOLUTION :
490 /*
491 * Resolution...
492 */
493
494 attr->values[j].resolution.xres = strtol(val, &s, 0);
495
496 if (*s == 'x')
497 attr->values[j].resolution.yres = strtol(s + 1, &s, 0);
498 else
499 attr->values[j].resolution.yres = attr->values[j].resolution.xres;
500
b423cd4c 501 if (!strcasecmp(s, "dpc"))
ef416fc2 502 attr->values[j].resolution.units = IPP_RES_PER_CM;
503 else
504 attr->values[j].resolution.units = IPP_RES_PER_INCH;
505
506 DEBUG_printf(("cupsEncodeOptions2: Added resolution option value %s...\n",
507 val));
508 break;
509
510 case IPP_TAG_STRING :
511 /*
512 * octet-string
513 */
514
515 attr->values[j].unknown.length = strlen(val);
516 attr->values[j].unknown.data = strdup(val);
517
518 DEBUG_printf(("cupsEncodeOptions2: Added octet-string value \"%s\"...\n",
519 attr->values[j].unknown.data));
520 break;
521
522 default :
523 if ((attr->values[j].string.text = strdup(val)) == NULL)
524 {
525 /*
526 * Ran out of memory!
527 */
528
529 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for string!");
530 return;
531 }
532
533 DEBUG_printf(("cupsEncodeOptions2: Added string value \"%s\"...\n",
534 val));
535 break;
536 }
537 }
538 }
539}
540
541
542/*
fa73b229 543 * 'compare_ipp_options()' - Compare two IPP options.
544 */
545
546static int /* O - Result of comparison */
547compare_ipp_options(_ipp_option_t *a, /* I - First option */
548 _ipp_option_t *b) /* I - Second option */
549{
550 return (strcmp(a->name, b->name));
551}
552
553
554/*
757d2cad 555 * End of "$Id: encode.c 5230 2006-03-05 17:01:51Z mike $".
ef416fc2 556 */