2 * "$Id: encode.c 5332 2006-03-23 21:29:42Z 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-error-policy", IPP_TAG_NAME
, IPP_TAG_PRINTER
},
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
},
123 { "printer-op-policy", IPP_TAG_NAME
, IPP_TAG_PRINTER
},
124 { "printer-resolution", IPP_TAG_RESOLUTION
, IPP_TAG_JOB
},
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
},
129 { "printer-uri", IPP_TAG_URI
, IPP_TAG_OPERATION
},
130 { "print-quality", IPP_TAG_ENUM
, IPP_TAG_JOB
},
131 { "print-quality-default", IPP_TAG_ENUM
, IPP_TAG_PRINTER
},
132 { "queued-job-count", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
133 { "raw", IPP_TAG_MIMETYPE
, IPP_TAG_OPERATION
},
134 { "resolution", IPP_TAG_RESOLUTION
, IPP_TAG_JOB
},
135 { "resolution-default", IPP_TAG_RESOLUTION
, IPP_TAG_PRINTER
},
136 { "saturation", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
137 { "saturation-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
138 { "scaling", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
139 { "scaling-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
140 { "sides", IPP_TAG_KEYWORD
, IPP_TAG_JOB
},
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
}
151 static int compare_ipp_options(_ipp_option_t
*a
, _ipp_option_t
*b
);
155 * 'cupsEncodeOptions()' - Encode printer options into IPP attributes.
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.
163 cupsEncodeOptions(ipp_t
*ipp
, /* I - Request to add to */
164 int num_options
, /* I - Number of options */
165 cups_option_t
*options
) /* I - Options */
167 DEBUG_printf(("cupsEncodeOptions(%p, %d, %p)\n", ipp
, num_options
, options
));
170 * Add the options in the proper groups & order...
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
);
180 * 'cupsEncodeOptions2()' - Encode printer options into IPP attributes for a group.
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.
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 */
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 */
204 cups_option_t
*option
; /* Current option */
207 DEBUG_printf(("cupsEncodeOptions2(ipp=%p, num_options=%d, options=%p, "
208 "group_tag=%x)\n", ipp
, num_options
, options
, group_tag
));
211 * Range check input...
214 if (!ipp
|| num_options
< 1 || !options
)
218 * Do special handling for the document-format/raw options...
221 if (group_tag
== IPP_TAG_OPERATION
)
224 * Handle the document format stuff first...
227 if ((val
= (char *)cupsGetOption("document-format", num_options
, options
)) != NULL
)
228 ippAddString(ipp
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
, "document-format",
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");
234 ippAddString(ipp
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
, "document-format",
235 NULL
, "application/octet-stream");
239 * Then loop through the options...
242 for (i
= num_options
, option
= options
; i
> 0; i
--, option
++)
244 _ipp_option_t key
, /* Search key */
245 *match
; /* Matching attribute */
249 * Skip document format options that are handled above...
252 if (!strcasecmp(option
->name
, "raw") ||
253 !strcasecmp(option
->name
, "document-format") ||
258 * Figure out the proper value and group tags for this option...
261 key
.name
= option
->name
;
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 *,
268 compare_ipp_options
);
272 if (match
->group_tag
!= group_tag
)
275 value_tag
= match
->value_tag
;
279 int namelen
; /* Length of name */
282 namelen
= strlen(option
->name
);
284 if (namelen
< 9 || strcmp(option
->name
+ namelen
- 8, "-default"))
286 if (group_tag
!= IPP_TAG_JOB
)
289 else if (group_tag
!= IPP_TAG_PRINTER
)
292 if (!strcasecmp(option
->value
, "true") ||
293 !strcasecmp(option
->value
, "false"))
294 value_tag
= IPP_TAG_BOOLEAN
;
296 value_tag
= IPP_TAG_NAME
;
300 * Count the number of values...
303 for (count
= 1, sep
= option
->value
; *sep
; sep
++)
308 * Skip quoted option value...
313 while (*sep
&& *sep
!= '\'')
319 else if (*sep
== '\"')
322 * Skip quoted option value...
327 while (*sep
&& *sep
!= '\"')
333 else if (*sep
== ',')
335 else if (*sep
== '\\' && sep
[1])
339 DEBUG_printf(("cupsEncodeOptions2: option = \'%s\', count = %d\n",
340 option
->name
, count
));
343 * Allocate memory for the attribute values...
346 if ((attr
= _ippAddAttr(ipp
, count
)) == NULL
)
352 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for attributes!");
357 * Now figure out what type of value we have...
360 attr
->group_tag
= group_tag
;
361 attr
->value_tag
= value_tag
;
364 * Copy the name over...
367 if ((attr
->name
= strdup(option
->name
)) == NULL
)
373 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for name!");
380 * Make a copy of the value we can fiddle with...
383 if ((copy
= strdup(option
->value
)) == NULL
)
389 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for value copy!");
398 * Since we have a single value, use the value directly...
406 * Scan the value string for values...
409 for (j
= 0; j
< count
; val
= sep
, j
++)
412 * Find the end of this value and mark it if needed...
415 if ((sep
= strchr(val
, ',')) != NULL
)
418 sep
= val
+ strlen(val
);
421 * Copy the option value(s) over as needed by the type...
424 switch (attr
->value_tag
)
426 case IPP_TAG_INTEGER
:
429 * Integer/enumeration value...
432 attr
->values
[j
].integer
= strtol(val
, &s
, 0);
434 DEBUG_printf(("cupsEncodeOptions2: Added integer option value %d...\n",
435 attr
->values
[j
].integer
));
438 case IPP_TAG_BOOLEAN
:
439 if (!strcasecmp(val
, "true") ||
440 !strcasecmp(val
, "on") ||
441 !strcasecmp(val
, "yes"))
444 * Boolean value - true...
447 attr
->values
[j
].boolean
= 1;
449 DEBUG_puts("cupsEncodeOptions2: Added boolean true value...");
454 * Boolean value - false...
457 attr
->values
[j
].boolean
= 0;
459 DEBUG_puts("cupsEncodeOptions2: Added boolean false value...");
470 attr
->values
[j
].range
.lower
= 1;
474 attr
->values
[j
].range
.lower
= strtol(val
, &s
, 0);
479 attr
->values
[j
].range
.upper
= strtol(s
+ 1, NULL
, 0);
481 attr
->values
[j
].range
.upper
= 2147483647;
484 attr
->values
[j
].range
.upper
= attr
->values
[j
].range
.lower
;
486 DEBUG_printf(("cupsEncodeOptions2: Added range option value %d-%d...\n",
487 attr
->values
[j
].range
.lower
,
488 attr
->values
[j
].range
.upper
));
491 case IPP_TAG_RESOLUTION
:
496 attr
->values
[j
].resolution
.xres
= strtol(val
, &s
, 0);
499 attr
->values
[j
].resolution
.yres
= strtol(s
+ 1, &s
, 0);
501 attr
->values
[j
].resolution
.yres
= attr
->values
[j
].resolution
.xres
;
503 if (!strcasecmp(s
, "dpc"))
504 attr
->values
[j
].resolution
.units
= IPP_RES_PER_CM
;
506 attr
->values
[j
].resolution
.units
= IPP_RES_PER_INCH
;
508 DEBUG_printf(("cupsEncodeOptions2: Added resolution option value %s...\n",
512 case IPP_TAG_STRING
:
517 attr
->values
[j
].unknown
.length
= strlen(val
);
518 attr
->values
[j
].unknown
.data
= strdup(val
);
520 DEBUG_printf(("cupsEncodeOptions2: Added octet-string value \"%s\"...\n",
521 attr
->values
[j
].unknown
.data
));
525 if ((attr
->values
[j
].string
.text
= strdup(val
)) == NULL
)
531 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for string!");
535 DEBUG_printf(("cupsEncodeOptions2: Added string value \"%s\"...\n",
545 * 'compare_ipp_options()' - Compare two IPP options.
548 static int /* O - Result of comparison */
549 compare_ipp_options(_ipp_option_t
*a
, /* I - First option */
550 _ipp_option_t
*b
) /* I - Second option */
552 return (strcmp(a
->name
, b
->name
));
557 * End of "$Id: encode.c 5332 2006-03-23 21:29:42Z mike $".