2 * "$Id: encode.c 6267 2007-02-11 23:24:22Z mike $"
4 * Option encoding routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2007 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 * _ippFindOption() - Find the attribute information for an option.
32 * compare_ipp_options() - Compare two IPP options.
36 * Include necessary headers...
40 #include "ipp-private.h"
48 * Local list of option names and the value tags they should use...
50 * **** THIS LIST MUST BE SORTED ****
53 static const _ipp_option_t ipp_options
[] =
55 { "blackplot", IPP_TAG_BOOLEAN
, IPP_TAG_JOB
},
56 { "blackplot-default", IPP_TAG_BOOLEAN
, IPP_TAG_PRINTER
},
57 { "brightness", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
58 { "brightness-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
59 { "columns", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
60 { "columns-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
61 { "copies", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
62 { "copies-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
63 { "document-format", IPP_TAG_MIMETYPE
, IPP_TAG_OPERATION
},
64 { "document-format-default", IPP_TAG_MIMETYPE
, IPP_TAG_PRINTER
},
65 { "finishings", IPP_TAG_ENUM
, IPP_TAG_JOB
},
66 { "finishings-default", IPP_TAG_ENUM
, IPP_TAG_PRINTER
},
67 { "fitplot", IPP_TAG_BOOLEAN
, IPP_TAG_JOB
},
68 { "fitplot-default", IPP_TAG_BOOLEAN
, IPP_TAG_PRINTER
},
69 { "gamma", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
70 { "gamma-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
71 { "hue", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
72 { "hue-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
73 { "job-k-limit", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
74 { "job-page-limit", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
75 { "job-priority", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
76 { "job-quota-period", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
77 { "job-uuid", IPP_TAG_URI
, IPP_TAG_JOB
},
78 { "landscape", IPP_TAG_BOOLEAN
, IPP_TAG_JOB
},
79 { "media", IPP_TAG_KEYWORD
, IPP_TAG_JOB
},
80 { "mirror", IPP_TAG_BOOLEAN
, IPP_TAG_JOB
},
81 { "mirror-default", IPP_TAG_BOOLEAN
, IPP_TAG_PRINTER
},
82 { "natural-scaling", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
83 { "natural-scaling-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
84 { "notify-charset", IPP_TAG_CHARSET
, IPP_TAG_SUBSCRIPTION
},
85 { "notify-events", IPP_TAG_KEYWORD
, IPP_TAG_SUBSCRIPTION
},
86 { "notify-events-default", IPP_TAG_KEYWORD
, IPP_TAG_PRINTER
},
87 { "notify-lease-duration", IPP_TAG_INTEGER
, IPP_TAG_SUBSCRIPTION
},
88 { "notify-lease-duration-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
89 { "notify-natural-language", IPP_TAG_LANGUAGE
, IPP_TAG_SUBSCRIPTION
},
90 { "notify-pull-method", IPP_TAG_KEYWORD
, IPP_TAG_SUBSCRIPTION
},
91 { "notify-recipient", IPP_TAG_URI
, IPP_TAG_SUBSCRIPTION
},
92 { "notify-time-interval", IPP_TAG_INTEGER
, IPP_TAG_SUBSCRIPTION
},
93 { "notify-user-data", IPP_TAG_STRING
, IPP_TAG_SUBSCRIPTION
},
94 { "number-up", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
95 { "number-up-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
96 { "orientation-requested", IPP_TAG_ENUM
, IPP_TAG_JOB
},
97 { "orientation-requested-default", IPP_TAG_ENUM
, IPP_TAG_PRINTER
},
98 { "page-bottom", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
99 { "page-bottom-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
100 { "page-left", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
101 { "page-left-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
102 { "page-ranges", IPP_TAG_RANGE
, IPP_TAG_JOB
},
103 { "page-ranges-default", IPP_TAG_RANGE
, IPP_TAG_PRINTER
},
104 { "page-right", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
105 { "page-right-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
106 { "page-top", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
107 { "page-top-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
108 { "penwidth", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
109 { "penwidth-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
110 { "ppi", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
111 { "ppi-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
112 { "prettyprint", IPP_TAG_BOOLEAN
, IPP_TAG_JOB
},
113 { "prettyprint-default", IPP_TAG_BOOLEAN
, IPP_TAG_PRINTER
},
114 { "print-quality", IPP_TAG_ENUM
, IPP_TAG_JOB
},
115 { "print-quality-default", IPP_TAG_ENUM
, IPP_TAG_PRINTER
},
116 { "printer-error-policy", IPP_TAG_NAME
, 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-op-policy", IPP_TAG_NAME
, IPP_TAG_PRINTER
},
123 { "printer-resolution", IPP_TAG_RESOLUTION
, IPP_TAG_JOB
},
124 { "printer-state", IPP_TAG_ENUM
, IPP_TAG_PRINTER
},
125 { "printer-state-change-time",IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
126 { "printer-state-reasons", IPP_TAG_KEYWORD
, IPP_TAG_PRINTER
},
127 { "printer-type", IPP_TAG_ENUM
, IPP_TAG_PRINTER
},
128 { "printer-uri", IPP_TAG_URI
, IPP_TAG_OPERATION
},
129 { "queued-job-count", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
130 { "raw", IPP_TAG_MIMETYPE
, IPP_TAG_OPERATION
},
131 { "requesting-user-name-allowed", IPP_TAG_NAME
, IPP_TAG_PRINTER
},
132 { "requesting-user-name-denied", IPP_TAG_NAME
, IPP_TAG_PRINTER
},
133 { "resolution", IPP_TAG_RESOLUTION
, IPP_TAG_JOB
},
134 { "resolution-default", IPP_TAG_RESOLUTION
, IPP_TAG_PRINTER
},
135 { "saturation", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
136 { "saturation-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
137 { "scaling", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
138 { "scaling-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
139 { "sides", IPP_TAG_KEYWORD
, IPP_TAG_JOB
},
140 { "sides-default", IPP_TAG_KEYWORD
, IPP_TAG_PRINTER
},
141 { "wrap", IPP_TAG_BOOLEAN
, IPP_TAG_JOB
},
142 { "wrap-default", IPP_TAG_BOOLEAN
, IPP_TAG_PRINTER
}
150 static int compare_ipp_options(_ipp_option_t
*a
, _ipp_option_t
*b
);
154 * 'cupsEncodeOptions()' - Encode printer options into IPP attributes.
156 * This function adds operation, job, and then subscription attributes,
157 * in that order. Use the cupsEncodeOptions2() function to add attributes
158 * for a single group.
162 cupsEncodeOptions(ipp_t
*ipp
, /* I - Request to add to */
163 int num_options
, /* I - Number of options */
164 cups_option_t
*options
) /* I - Options */
166 DEBUG_printf(("cupsEncodeOptions(%p, %d, %p)\n", ipp
, num_options
, options
));
169 * Add the options in the proper groups & order...
172 cupsEncodeOptions2(ipp
, num_options
, options
, IPP_TAG_OPERATION
);
173 cupsEncodeOptions2(ipp
, num_options
, options
, IPP_TAG_JOB
);
174 cupsEncodeOptions2(ipp
, num_options
, options
, IPP_TAG_SUBSCRIPTION
);
179 * 'cupsEncodeOptions2()' - Encode printer options into IPP attributes for a group.
181 * This function only adds attributes for a single group. Call this
182 * function multiple times for each group, or use cupsEncodeOptions()
183 * to add the standard groups.
190 ipp_t
*ipp
, /* I - Request to add to */
191 int num_options
, /* I - Number of options */
192 cups_option_t
*options
, /* I - Options */
193 ipp_tag_t group_tag
) /* I - Group to encode */
195 int i
, j
; /* Looping vars */
196 int count
; /* Number of values */
197 char *s
, /* Pointer into option value */
198 *val
, /* Pointer to option value */
199 *copy
, /* Copy of option value */
200 *sep
; /* Option separator */
201 ipp_attribute_t
*attr
; /* IPP attribute */
202 ipp_tag_t value_tag
; /* IPP value tag */
203 cups_option_t
*option
; /* Current option */
206 DEBUG_printf(("cupsEncodeOptions2(ipp=%p, num_options=%d, options=%p, "
207 "group_tag=%x)\n", ipp
, num_options
, options
, group_tag
));
210 * Range check input...
213 if (!ipp
|| num_options
< 1 || !options
)
217 * Do special handling for the document-format/raw options...
220 if (group_tag
== IPP_TAG_OPERATION
)
223 * Handle the document format stuff first...
226 if ((val
= (char *)cupsGetOption("document-format", num_options
, options
)) != NULL
)
227 ippAddString(ipp
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
, "document-format",
229 else if (cupsGetOption("raw", num_options
, options
))
230 ippAddString(ipp
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
, "document-format",
231 NULL
, "application/vnd.cups-raw");
233 ippAddString(ipp
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
, "document-format",
234 NULL
, "application/octet-stream");
238 * Then loop through the options...
241 for (i
= num_options
, option
= options
; i
> 0; i
--, option
++)
243 _ipp_option_t
*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 if ((match
= _ippFindOption(option
->name
)) != NULL
)
261 if (match
->group_tag
!= group_tag
)
264 value_tag
= match
->value_tag
;
268 int namelen
; /* Length of name */
271 namelen
= (int)strlen(option
->name
);
273 if (namelen
< 9 || strcmp(option
->name
+ namelen
- 8, "-default"))
275 if (group_tag
!= IPP_TAG_JOB
)
278 else if (group_tag
!= IPP_TAG_PRINTER
)
281 if (!strcasecmp(option
->value
, "true") ||
282 !strcasecmp(option
->value
, "false"))
283 value_tag
= IPP_TAG_BOOLEAN
;
285 value_tag
= IPP_TAG_NAME
;
289 * Count the number of values...
292 for (count
= 1, sep
= option
->value
; *sep
; sep
++)
297 * Skip quoted option value...
302 while (*sep
&& *sep
!= '\'')
308 else if (*sep
== '\"')
311 * Skip quoted option value...
316 while (*sep
&& *sep
!= '\"')
322 else if (*sep
== ',')
324 else if (*sep
== '\\' && sep
[1])
328 DEBUG_printf(("cupsEncodeOptions2: option = \'%s\', count = %d\n",
329 option
->name
, count
));
332 * Allocate memory for the attribute values...
335 if ((attr
= _ippAddAttr(ipp
, count
)) == NULL
)
341 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for attributes!");
346 * Now figure out what type of value we have...
349 attr
->group_tag
= group_tag
;
350 attr
->value_tag
= value_tag
;
353 * Copy the name over...
356 if ((attr
->name
= _cupsStrAlloc(option
->name
)) == NULL
)
362 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for name!");
369 * Make a copy of the value we can fiddle with...
372 if ((copy
= strdup(option
->value
)) == NULL
)
378 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for value copy!");
387 * Since we have a single value, use the value directly...
395 * Scan the value string for values...
398 for (j
= 0; j
< count
; val
= sep
, j
++)
401 * Find the end of this value and mark it if needed...
404 if ((sep
= strchr(val
, ',')) != NULL
)
407 sep
= val
+ strlen(val
);
410 * Copy the option value(s) over as needed by the type...
413 switch (attr
->value_tag
)
415 case IPP_TAG_INTEGER
:
418 * Integer/enumeration value...
421 attr
->values
[j
].integer
= strtol(val
, &s
, 0);
423 DEBUG_printf(("cupsEncodeOptions2: Added integer option value %d...\n",
424 attr
->values
[j
].integer
));
427 case IPP_TAG_BOOLEAN
:
428 if (!strcasecmp(val
, "true") ||
429 !strcasecmp(val
, "on") ||
430 !strcasecmp(val
, "yes"))
433 * Boolean value - true...
436 attr
->values
[j
].boolean
= 1;
438 DEBUG_puts("cupsEncodeOptions2: Added boolean true value...");
443 * Boolean value - false...
446 attr
->values
[j
].boolean
= 0;
448 DEBUG_puts("cupsEncodeOptions2: Added boolean false value...");
459 attr
->values
[j
].range
.lower
= 1;
463 attr
->values
[j
].range
.lower
= strtol(val
, &s
, 0);
468 attr
->values
[j
].range
.upper
= strtol(s
+ 1, NULL
, 0);
470 attr
->values
[j
].range
.upper
= 2147483647;
473 attr
->values
[j
].range
.upper
= attr
->values
[j
].range
.lower
;
475 DEBUG_printf(("cupsEncodeOptions2: Added range option value %d-%d...\n",
476 attr
->values
[j
].range
.lower
,
477 attr
->values
[j
].range
.upper
));
480 case IPP_TAG_RESOLUTION
:
485 attr
->values
[j
].resolution
.xres
= strtol(val
, &s
, 0);
488 attr
->values
[j
].resolution
.yres
= strtol(s
+ 1, &s
, 0);
490 attr
->values
[j
].resolution
.yres
= attr
->values
[j
].resolution
.xres
;
492 if (!strcasecmp(s
, "dpc"))
493 attr
->values
[j
].resolution
.units
= IPP_RES_PER_CM
;
495 attr
->values
[j
].resolution
.units
= IPP_RES_PER_INCH
;
497 DEBUG_printf(("cupsEncodeOptions2: Added resolution option value %s...\n",
501 case IPP_TAG_STRING
:
506 attr
->values
[j
].unknown
.length
= (int)strlen(val
);
507 attr
->values
[j
].unknown
.data
= _cupsStrAlloc(val
);
509 DEBUG_printf(("cupsEncodeOptions2: Added octet-string value \"%s\"...\n",
510 attr
->values
[j
].unknown
.data
));
514 if ((attr
->values
[j
].string
.text
= _cupsStrAlloc(val
)) == NULL
)
520 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for string!");
524 DEBUG_printf(("cupsEncodeOptions2: Added string value \"%s\"...\n",
537 * '_ippFindOption()' - Find the attribute information for an option.
540 _ipp_option_t
* /* O - Attribute information */
541 _ippFindOption(const char *name
) /* I - Option/attribute name */
543 _ipp_option_t key
; /* Search key */
547 * Lookup the proper value and group tags for this option...
552 return ((_ipp_option_t
*)bsearch(&key
, ipp_options
,
553 sizeof(ipp_options
) / sizeof(ipp_options
[0]),
554 sizeof(ipp_options
[0]),
555 (int (*)(const void *, const void *))
556 compare_ipp_options
));
561 * 'compare_ipp_options()' - Compare two IPP options.
564 static int /* O - Result of comparison */
565 compare_ipp_options(_ipp_option_t
*a
, /* I - First option */
566 _ipp_option_t
*b
) /* I - Second option */
568 return (strcmp(a
->name
, b
->name
));
573 * End of "$Id: encode.c 6267 2007-02-11 23:24:22Z mike $".