2 * "$Id: encode.c 181 2006-06-22 20:01:18Z jlovell $"
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 { "document-format-default", IPP_TAG_MIMETYPE
, IPP_TAG_PRINTER
},
71 { "finishings", IPP_TAG_ENUM
, IPP_TAG_JOB
},
72 { "finishings-default", IPP_TAG_ENUM
, IPP_TAG_PRINTER
},
73 { "fitplot", IPP_TAG_BOOLEAN
, IPP_TAG_JOB
},
74 { "fitplot-default", IPP_TAG_BOOLEAN
, IPP_TAG_PRINTER
},
75 { "gamma", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
76 { "gamma-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
77 { "hue", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
78 { "hue-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
79 { "job-k-limit", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
80 { "job-page-limit", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
81 { "job-priority", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
82 { "job-quota-period", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
83 { "job-uuid", IPP_TAG_URI
, IPP_TAG_JOB
},
84 { "landscape", IPP_TAG_BOOLEAN
, IPP_TAG_JOB
},
85 { "media", IPP_TAG_KEYWORD
, IPP_TAG_JOB
},
86 { "mirror", IPP_TAG_BOOLEAN
, IPP_TAG_JOB
},
87 { "mirror-default", IPP_TAG_BOOLEAN
, IPP_TAG_PRINTER
},
88 { "natural-scaling", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
89 { "natural-scaling-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
90 { "notify-charset", IPP_TAG_CHARSET
, IPP_TAG_SUBSCRIPTION
},
91 { "notify-events", IPP_TAG_KEYWORD
, IPP_TAG_SUBSCRIPTION
},
92 { "notify-events-default", IPP_TAG_KEYWORD
, IPP_TAG_PRINTER
},
93 { "notify-lease-time", IPP_TAG_INTEGER
, IPP_TAG_SUBSCRIPTION
},
94 { "notify-natural-language", IPP_TAG_LANGUAGE
, IPP_TAG_SUBSCRIPTION
},
95 { "notify-pull-method", IPP_TAG_KEYWORD
, IPP_TAG_SUBSCRIPTION
},
96 { "notify-recipient", IPP_TAG_URI
, IPP_TAG_SUBSCRIPTION
},
97 { "notify-time-interval", IPP_TAG_INTEGER
, IPP_TAG_SUBSCRIPTION
},
98 { "notify-user-data", IPP_TAG_STRING
, IPP_TAG_SUBSCRIPTION
},
99 { "number-up", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
100 { "number-up-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
101 { "orientation-requested", IPP_TAG_ENUM
, IPP_TAG_JOB
},
102 { "orientation-requested-default", IPP_TAG_ENUM
, IPP_TAG_PRINTER
},
103 { "page-bottom", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
104 { "page-bottom-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
105 { "page-left", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
106 { "page-left-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
107 { "page-ranges", IPP_TAG_RANGE
, IPP_TAG_JOB
},
108 { "page-ranges-default", IPP_TAG_RANGE
, IPP_TAG_PRINTER
},
109 { "page-right", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
110 { "page-right-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
111 { "page-top", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
112 { "page-top-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
113 { "penwidth", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
114 { "penwidth-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
115 { "ppi", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
116 { "ppi-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
117 { "prettyprint", IPP_TAG_BOOLEAN
, IPP_TAG_JOB
},
118 { "prettyprint-default", IPP_TAG_BOOLEAN
, IPP_TAG_PRINTER
},
119 { "printer-error-policy", IPP_TAG_NAME
, IPP_TAG_PRINTER
},
120 { "printer-info", IPP_TAG_TEXT
, IPP_TAG_PRINTER
},
121 { "printer-is-accepting-jobs",IPP_TAG_BOOLEAN
, IPP_TAG_PRINTER
},
122 { "printer-is-shared", IPP_TAG_BOOLEAN
, IPP_TAG_PRINTER
},
123 { "printer-make-and-model", IPP_TAG_TEXT
, IPP_TAG_PRINTER
},
124 { "printer-more-info", IPP_TAG_URI
, IPP_TAG_PRINTER
},
125 { "printer-op-policy", IPP_TAG_NAME
, IPP_TAG_PRINTER
},
126 { "printer-resolution", IPP_TAG_RESOLUTION
, IPP_TAG_JOB
},
127 { "printer-state", IPP_TAG_ENUM
, IPP_TAG_PRINTER
},
128 { "printer-state-change-time",IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
129 { "printer-state-reasons", IPP_TAG_KEYWORD
, IPP_TAG_PRINTER
},
130 { "printer-type", IPP_TAG_ENUM
, IPP_TAG_PRINTER
},
131 { "printer-uri", IPP_TAG_URI
, IPP_TAG_OPERATION
},
132 { "print-quality", IPP_TAG_ENUM
, IPP_TAG_JOB
},
133 { "print-quality-default", IPP_TAG_ENUM
, IPP_TAG_PRINTER
},
134 { "queued-job-count", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
135 { "raw", IPP_TAG_MIMETYPE
, IPP_TAG_OPERATION
},
136 { "resolution", IPP_TAG_RESOLUTION
, IPP_TAG_JOB
},
137 { "resolution-default", IPP_TAG_RESOLUTION
, IPP_TAG_PRINTER
},
138 { "requesting-user-name-allowed", IPP_TAG_NAME
, IPP_TAG_PRINTER
},
139 { "requesting-user-name-denied", IPP_TAG_NAME
, IPP_TAG_PRINTER
},
140 { "saturation", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
141 { "saturation-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
142 { "scaling", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
143 { "scaling-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
144 { "sides", IPP_TAG_KEYWORD
, IPP_TAG_JOB
},
145 { "sides-default", IPP_TAG_KEYWORD
, IPP_TAG_PRINTER
},
146 { "wrap", IPP_TAG_BOOLEAN
, IPP_TAG_JOB
},
147 { "wrap-default", IPP_TAG_BOOLEAN
, IPP_TAG_PRINTER
}
155 static int compare_ipp_options(_ipp_option_t
*a
, _ipp_option_t
*b
);
159 * 'cupsEncodeOptions()' - Encode printer options into IPP attributes.
161 * This function adds operation, job, and then subscription attributes,
162 * in that order. Use the cupsEncodeOptions2() function to add attributes
163 * for a single group.
167 cupsEncodeOptions(ipp_t
*ipp
, /* I - Request to add to */
168 int num_options
, /* I - Number of options */
169 cups_option_t
*options
) /* I - Options */
171 DEBUG_printf(("cupsEncodeOptions(%p, %d, %p)\n", ipp
, num_options
, options
));
174 * Add the options in the proper groups & order...
177 cupsEncodeOptions2(ipp
, num_options
, options
, IPP_TAG_OPERATION
);
178 cupsEncodeOptions2(ipp
, num_options
, options
, IPP_TAG_JOB
);
179 cupsEncodeOptions2(ipp
, num_options
, options
, IPP_TAG_SUBSCRIPTION
);
184 * 'cupsEncodeOptions2()' - Encode printer options into IPP attributes for a group.
186 * This function only adds attributes for a single group. Call this
187 * function multiple times for each group, or use cupsEncodeOptions()
188 * to add the standard groups.
195 ipp_t
*ipp
, /* I - Request to add to */
196 int num_options
, /* I - Number of options */
197 cups_option_t
*options
, /* I - Options */
198 ipp_tag_t group_tag
) /* I - Group to encode */
200 int i
, j
; /* Looping vars */
201 int count
; /* Number of values */
202 char *s
, /* Pointer into option value */
203 *val
, /* Pointer to option value */
204 *copy
, /* Copy of option value */
205 *sep
; /* Option separator */
206 ipp_attribute_t
*attr
; /* IPP attribute */
207 ipp_tag_t value_tag
; /* IPP value tag */
208 cups_option_t
*option
; /* Current option */
211 DEBUG_printf(("cupsEncodeOptions2(ipp=%p, num_options=%d, options=%p, "
212 "group_tag=%x)\n", ipp
, num_options
, options
, group_tag
));
215 * Range check input...
218 if (!ipp
|| num_options
< 1 || !options
)
222 * Do special handling for the document-format/raw options...
225 if (group_tag
== IPP_TAG_OPERATION
)
228 * Handle the document format stuff first...
231 if ((val
= (char *)cupsGetOption("document-format", num_options
, options
)) != NULL
)
232 ippAddString(ipp
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
, "document-format",
234 else if (cupsGetOption("raw", num_options
, options
))
235 ippAddString(ipp
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
, "document-format",
236 NULL
, "application/vnd.cups-raw");
238 ippAddString(ipp
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
, "document-format",
239 NULL
, "application/octet-stream");
243 * Then loop through the options...
246 for (i
= num_options
, option
= options
; i
> 0; i
--, option
++)
248 _ipp_option_t key
, /* Search key */
249 *match
; /* Matching attribute */
253 * Skip document format options that are handled above...
256 if (!strcasecmp(option
->name
, "raw") ||
257 !strcasecmp(option
->name
, "document-format") ||
262 * Figure out the proper value and group tags for this option...
265 key
.name
= option
->name
;
266 match
= (_ipp_option_t
*)bsearch(&key
, ipp_options
,
267 sizeof(ipp_options
) /
268 sizeof(ipp_options
[0]),
269 sizeof(ipp_options
[0]),
270 (int (*)(const void *,
272 compare_ipp_options
);
276 if (match
->group_tag
!= group_tag
)
279 value_tag
= match
->value_tag
;
283 int namelen
; /* Length of name */
286 namelen
= strlen(option
->name
);
288 if (namelen
< 9 || strcmp(option
->name
+ namelen
- 8, "-default"))
290 if (group_tag
!= IPP_TAG_JOB
)
293 else if (group_tag
!= IPP_TAG_PRINTER
)
296 if (!strcasecmp(option
->value
, "true") ||
297 !strcasecmp(option
->value
, "false"))
298 value_tag
= IPP_TAG_BOOLEAN
;
300 value_tag
= IPP_TAG_NAME
;
304 * Count the number of values...
307 for (count
= 1, sep
= option
->value
; *sep
; sep
++)
312 * Skip quoted option value...
317 while (*sep
&& *sep
!= '\'')
323 else if (*sep
== '\"')
326 * Skip quoted option value...
331 while (*sep
&& *sep
!= '\"')
337 else if (*sep
== ',')
339 else if (*sep
== '\\' && sep
[1])
343 DEBUG_printf(("cupsEncodeOptions2: option = \'%s\', count = %d\n",
344 option
->name
, count
));
347 * Allocate memory for the attribute values...
350 if ((attr
= _ippAddAttr(ipp
, count
)) == NULL
)
356 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for attributes!");
361 * Now figure out what type of value we have...
364 attr
->group_tag
= group_tag
;
365 attr
->value_tag
= value_tag
;
368 * Copy the name over...
371 if ((attr
->name
= _cupsStrAlloc(option
->name
)) == NULL
)
377 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for name!");
384 * Make a copy of the value we can fiddle with...
387 if ((copy
= strdup(option
->value
)) == NULL
)
393 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for value copy!");
402 * Since we have a single value, use the value directly...
410 * Scan the value string for values...
413 for (j
= 0; j
< count
; val
= sep
, j
++)
416 * Find the end of this value and mark it if needed...
419 if ((sep
= strchr(val
, ',')) != NULL
)
422 sep
= val
+ strlen(val
);
425 * Copy the option value(s) over as needed by the type...
428 switch (attr
->value_tag
)
430 case IPP_TAG_INTEGER
:
433 * Integer/enumeration value...
436 attr
->values
[j
].integer
= strtol(val
, &s
, 0);
438 DEBUG_printf(("cupsEncodeOptions2: Added integer option value %d...\n",
439 attr
->values
[j
].integer
));
442 case IPP_TAG_BOOLEAN
:
443 if (!strcasecmp(val
, "true") ||
444 !strcasecmp(val
, "on") ||
445 !strcasecmp(val
, "yes"))
448 * Boolean value - true...
451 attr
->values
[j
].boolean
= 1;
453 DEBUG_puts("cupsEncodeOptions2: Added boolean true value...");
458 * Boolean value - false...
461 attr
->values
[j
].boolean
= 0;
463 DEBUG_puts("cupsEncodeOptions2: Added boolean false value...");
474 attr
->values
[j
].range
.lower
= 1;
478 attr
->values
[j
].range
.lower
= strtol(val
, &s
, 0);
483 attr
->values
[j
].range
.upper
= strtol(s
+ 1, NULL
, 0);
485 attr
->values
[j
].range
.upper
= 2147483647;
488 attr
->values
[j
].range
.upper
= attr
->values
[j
].range
.lower
;
490 DEBUG_printf(("cupsEncodeOptions2: Added range option value %d-%d...\n",
491 attr
->values
[j
].range
.lower
,
492 attr
->values
[j
].range
.upper
));
495 case IPP_TAG_RESOLUTION
:
500 attr
->values
[j
].resolution
.xres
= strtol(val
, &s
, 0);
503 attr
->values
[j
].resolution
.yres
= strtol(s
+ 1, &s
, 0);
505 attr
->values
[j
].resolution
.yres
= attr
->values
[j
].resolution
.xres
;
507 if (!strcasecmp(s
, "dpc"))
508 attr
->values
[j
].resolution
.units
= IPP_RES_PER_CM
;
510 attr
->values
[j
].resolution
.units
= IPP_RES_PER_INCH
;
512 DEBUG_printf(("cupsEncodeOptions2: Added resolution option value %s...\n",
516 case IPP_TAG_STRING
:
521 attr
->values
[j
].unknown
.length
= strlen(val
);
522 attr
->values
[j
].unknown
.data
= _cupsStrAlloc(val
);
524 DEBUG_printf(("cupsEncodeOptions2: Added octet-string value \"%s\"...\n",
525 attr
->values
[j
].unknown
.data
));
529 if ((attr
->values
[j
].string
.text
= _cupsStrAlloc(val
)) == NULL
)
535 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for string!");
539 DEBUG_printf(("cupsEncodeOptions2: Added string value \"%s\"...\n",
552 * 'compare_ipp_options()' - Compare two IPP options.
555 static int /* O - Result of comparison */
556 compare_ipp_options(_ipp_option_t
*a
, /* I - First option */
557 _ipp_option_t
*b
) /* I - Second option */
559 return (strcmp(a
->name
, b
->name
));
564 * End of "$Id: encode.c 181 2006-06-22 20:01:18Z jlovell $".