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