2 * "$Id: encode.c 5151 2006-02-22 22:43:17Z mike $"
4 * Option encoding routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products.
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
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * cupsEncodeOptions() - Encode printer options into IPP attributes.
29 * cupsEncodeOptions2() - Encode printer options into IPP attributes for
31 * compare_ipp_options() - Compare two IPP options.
35 * Include necessary headers...
39 #include "ipp-private.h"
47 * Local list of option names and the value tags they should use...
49 * **** THIS LIST MUST BE SORTED ****
59 static const _ipp_option_t ipp_options
[] =
61 { "blackplot", IPP_TAG_BOOLEAN
, IPP_TAG_JOB
},
62 { "blackplot-default", IPP_TAG_BOOLEAN
, IPP_TAG_PRINTER
},
63 { "brightness", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
64 { "brightness-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
65 { "columns", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
66 { "columns-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
67 { "copies", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
68 { "copies-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
69 { "document-format", IPP_TAG_MIMETYPE
, IPP_TAG_OPERATION
},
70 { "finishings", IPP_TAG_ENUM
, IPP_TAG_JOB
},
71 { "finishings-default", IPP_TAG_ENUM
, IPP_TAG_PRINTER
},
72 { "fitplot", IPP_TAG_BOOLEAN
, IPP_TAG_JOB
},
73 { "fitplot-default", IPP_TAG_BOOLEAN
, IPP_TAG_PRINTER
},
74 { "gamma", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
75 { "gamma-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
76 { "hue", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
77 { "hue-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
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
},
82 { "job-uuid", IPP_TAG_URI
, IPP_TAG_JOB
},
83 { "landscape", IPP_TAG_BOOLEAN
, IPP_TAG_JOB
},
84 { "media", IPP_TAG_KEYWORD
, IPP_TAG_JOB
},
85 { "mirror", IPP_TAG_BOOLEAN
, IPP_TAG_JOB
},
86 { "mirror-default", IPP_TAG_BOOLEAN
, IPP_TAG_PRINTER
},
87 { "natural-scaling", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
88 { "natural-scaling-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
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
},
98 { "number-up-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
99 { "orientation-requested", IPP_TAG_ENUM
, IPP_TAG_JOB
},
100 { "orientation-requested-default", IPP_TAG_ENUM
, IPP_TAG_PRINTER
},
101 { "page-bottom", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
102 { "page-bottom-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
103 { "page-left", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
104 { "page-left-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
105 { "page-ranges", IPP_TAG_RANGE
, IPP_TAG_JOB
},
106 { "page-ranges-default", IPP_TAG_RANGE
, IPP_TAG_PRINTER
},
107 { "page-right", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
108 { "page-right-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
109 { "page-top", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
110 { "page-top-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
111 { "penwidth", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
112 { "penwidth-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
113 { "ppi", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
114 { "ppi-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
115 { "prettyprint", IPP_TAG_BOOLEAN
, IPP_TAG_JOB
},
116 { "prettyprint-default", IPP_TAG_BOOLEAN
, IPP_TAG_PRINTER
},
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
},
122 { "printer-resolution", IPP_TAG_RESOLUTION
, IPP_TAG_JOB
},
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
},
127 { "printer-uri", IPP_TAG_URI
, IPP_TAG_OPERATION
},
128 { "print-quality", IPP_TAG_ENUM
, IPP_TAG_JOB
},
129 { "print-quality-default", IPP_TAG_ENUM
, IPP_TAG_PRINTER
},
130 { "queued-job-count", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
131 { "raw", IPP_TAG_MIMETYPE
, IPP_TAG_OPERATION
},
132 { "resolution", IPP_TAG_RESOLUTION
, IPP_TAG_JOB
},
133 { "resolution-default", IPP_TAG_RESOLUTION
, IPP_TAG_PRINTER
},
134 { "saturation", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
135 { "saturation-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
136 { "scaling", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
137 { "scaling-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
138 { "sides", IPP_TAG_KEYWORD
, IPP_TAG_JOB
},
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
}
149 static int compare_ipp_options(_ipp_option_t
*a
, _ipp_option_t
*b
);
153 * 'cupsEncodeOptions()' - Encode printer options into IPP attributes.
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.
161 cupsEncodeOptions(ipp_t
*ipp
, /* I - Request to add to */
162 int num_options
, /* I - Number of options */
163 cups_option_t
*options
) /* I - Options */
165 DEBUG_printf(("cupsEncodeOptions(%p, %d, %p)\n", ipp
, num_options
, options
));
168 * Add the options in the proper groups & order...
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
);
178 * 'cupsEncodeOptions2()' - Encode printer options into IPP attributes for a group.
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.
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 */
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 */
202 cups_option_t
*option
; /* Current option */
205 DEBUG_printf(("cupsEncodeOptions2(ipp=%p, num_options=%d, options=%p, "
206 "group_tag=%x)\n", ipp
, num_options
, options
, group_tag
));
209 * Range check input...
212 if (!ipp
|| num_options
< 1 || !options
)
216 * Do special handling for the document-format/raw options...
219 if (group_tag
== IPP_TAG_OPERATION
)
222 * Handle the document format stuff first...
225 if ((val
= (char *)cupsGetOption("document-format", num_options
, options
)) != NULL
)
226 ippAddString(ipp
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
, "document-format",
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");
232 ippAddString(ipp
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
, "document-format",
233 NULL
, "application/octet-stream");
237 * Then loop through the options...
240 for (i
= num_options
, option
= options
; i
> 0; i
--, option
++)
242 _ipp_option_t key
, /* Search key */
243 *match
; /* Matching attribute */
247 * Skip document format options that are handled above...
250 if (!strcasecmp(option
->name
, "raw") ||
251 !strcasecmp(option
->name
, "document-format") ||
256 * Figure out the proper value and group tags for this option...
259 key
.name
= option
->name
;
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 *,
266 compare_ipp_options
);
270 if (match
->group_tag
!= group_tag
)
273 value_tag
= match
->value_tag
;
277 int namelen
; /* Length of name */
280 namelen
= strlen(option
->name
);
282 if (namelen
< 9 || strcmp(option
->name
+ namelen
- 8, "-default"))
284 if (group_tag
!= IPP_TAG_JOB
)
287 else if (group_tag
!= IPP_TAG_PRINTER
)
290 if (!strcasecmp(option
->value
, "true") ||
291 !strcasecmp(option
->value
, "false"))
292 value_tag
= IPP_TAG_BOOLEAN
;
294 value_tag
= IPP_TAG_NAME
;
298 * Count the number of values...
301 for (count
= 1, sep
= option
->value
; *sep
; sep
++)
306 * Skip quoted option value...
311 while (*sep
&& *sep
!= '\'')
317 else if (*sep
== '\"')
320 * Skip quoted option value...
325 while (*sep
&& *sep
!= '\"')
331 else if (*sep
== ',')
333 else if (*sep
== '\\' && sep
[1])
337 DEBUG_printf(("cupsEncodeOptions2: option = \'%s\', count = %d\n",
338 option
->name
, count
));
341 * Allocate memory for the attribute values...
344 if ((attr
= _ipp_add_attr(ipp
, count
)) == NULL
)
350 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for attributes!");
355 * Now figure out what type of value we have...
358 attr
->group_tag
= group_tag
;
359 attr
->value_tag
= value_tag
;
362 * Copy the name over...
365 if ((attr
->name
= strdup(option
->name
)) == NULL
)
371 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for name!");
378 * Make a copy of the value we can fiddle with...
381 if ((copy
= strdup(option
->value
)) == NULL
)
387 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for value copy!");
396 * Since we have a single value, use the value directly...
404 * Scan the value string for values...
407 for (j
= 0; j
< count
; val
= sep
, j
++)
410 * Find the end of this value and mark it if needed...
413 if ((sep
= strchr(val
, ',')) != NULL
)
416 sep
= val
+ strlen(val
);
419 * Copy the option value(s) over as needed by the type...
422 switch (attr
->value_tag
)
424 case IPP_TAG_INTEGER
:
427 * Integer/enumeration value...
430 attr
->values
[j
].integer
= strtol(val
, &s
, 0);
432 DEBUG_printf(("cupsEncodeOptions2: Added integer option value %d...\n",
433 attr
->values
[j
].integer
));
436 case IPP_TAG_BOOLEAN
:
437 if (!strcasecmp(val
, "true") ||
438 !strcasecmp(val
, "on") ||
439 !strcasecmp(val
, "yes"))
442 * Boolean value - true...
445 attr
->values
[j
].boolean
= 1;
447 DEBUG_puts("cupsEncodeOptions2: Added boolean true value...");
452 * Boolean value - false...
455 attr
->values
[j
].boolean
= 0;
457 DEBUG_puts("cupsEncodeOptions2: Added boolean false value...");
468 attr
->values
[j
].range
.lower
= 1;
472 attr
->values
[j
].range
.lower
= strtol(val
, &s
, 0);
477 attr
->values
[j
].range
.upper
= strtol(s
+ 1, NULL
, 0);
479 attr
->values
[j
].range
.upper
= 2147483647;
482 attr
->values
[j
].range
.upper
= attr
->values
[j
].range
.lower
;
484 DEBUG_printf(("cupsEncodeOptions2: Added range option value %d-%d...\n",
485 attr
->values
[j
].range
.lower
,
486 attr
->values
[j
].range
.upper
));
489 case IPP_TAG_RESOLUTION
:
494 attr
->values
[j
].resolution
.xres
= strtol(val
, &s
, 0);
497 attr
->values
[j
].resolution
.yres
= strtol(s
+ 1, &s
, 0);
499 attr
->values
[j
].resolution
.yres
= attr
->values
[j
].resolution
.xres
;
501 if (!strcasecmp(s
, "dpc"))
502 attr
->values
[j
].resolution
.units
= IPP_RES_PER_CM
;
504 attr
->values
[j
].resolution
.units
= IPP_RES_PER_INCH
;
506 DEBUG_printf(("cupsEncodeOptions2: Added resolution option value %s...\n",
510 case IPP_TAG_STRING
:
515 attr
->values
[j
].unknown
.length
= strlen(val
);
516 attr
->values
[j
].unknown
.data
= strdup(val
);
518 DEBUG_printf(("cupsEncodeOptions2: Added octet-string value \"%s\"...\n",
519 attr
->values
[j
].unknown
.data
));
523 if ((attr
->values
[j
].string
.text
= strdup(val
)) == NULL
)
529 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for string!");
533 DEBUG_printf(("cupsEncodeOptions2: Added string value \"%s\"...\n",
543 * 'compare_ipp_options()' - Compare two IPP options.
546 static int /* O - Result of comparison */
547 compare_ipp_options(_ipp_option_t
*a
, /* I - First option */
548 _ipp_option_t
*b
) /* I - Second option */
550 return (strcmp(a
->name
, b
->name
));
555 * End of "$Id: encode.c 5151 2006-02-22 22:43:17Z mike $".