2 * "$Id: encode.c 5753 2006-07-18 19:53:24Z 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 * _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 { "finishings", IPP_TAG_ENUM
, IPP_TAG_JOB
},
65 { "finishings-default", IPP_TAG_ENUM
, IPP_TAG_PRINTER
},
66 { "fitplot", IPP_TAG_BOOLEAN
, IPP_TAG_JOB
},
67 { "fitplot-default", IPP_TAG_BOOLEAN
, IPP_TAG_PRINTER
},
68 { "gamma", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
69 { "gamma-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
70 { "hue", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
71 { "hue-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
72 { "job-k-limit", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
73 { "job-page-limit", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
74 { "job-priority", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
75 { "job-quota-period", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
76 { "job-uuid", IPP_TAG_URI
, IPP_TAG_JOB
},
77 { "landscape", IPP_TAG_BOOLEAN
, IPP_TAG_JOB
},
78 { "media", IPP_TAG_KEYWORD
, IPP_TAG_JOB
},
79 { "mirror", IPP_TAG_BOOLEAN
, IPP_TAG_JOB
},
80 { "mirror-default", IPP_TAG_BOOLEAN
, IPP_TAG_PRINTER
},
81 { "natural-scaling", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
82 { "natural-scaling-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
83 { "notify-charset", IPP_TAG_CHARSET
, IPP_TAG_SUBSCRIPTION
},
84 { "notify-events", IPP_TAG_KEYWORD
, IPP_TAG_SUBSCRIPTION
},
85 { "notify-lease-time", IPP_TAG_INTEGER
, IPP_TAG_SUBSCRIPTION
},
86 { "notify-natural-language", IPP_TAG_LANGUAGE
, IPP_TAG_SUBSCRIPTION
},
87 { "notify-pull-method", IPP_TAG_KEYWORD
, IPP_TAG_SUBSCRIPTION
},
88 { "notify-recipient", IPP_TAG_URI
, IPP_TAG_SUBSCRIPTION
},
89 { "notify-time-interval", IPP_TAG_INTEGER
, IPP_TAG_SUBSCRIPTION
},
90 { "notify-user-data", IPP_TAG_STRING
, IPP_TAG_SUBSCRIPTION
},
91 { "number-up", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
92 { "number-up-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
93 { "orientation-requested", IPP_TAG_ENUM
, IPP_TAG_JOB
},
94 { "orientation-requested-default", IPP_TAG_ENUM
, IPP_TAG_PRINTER
},
95 { "page-bottom", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
96 { "page-bottom-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
97 { "page-left", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
98 { "page-left-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
99 { "page-ranges", IPP_TAG_RANGE
, IPP_TAG_JOB
},
100 { "page-ranges-default", IPP_TAG_RANGE
, IPP_TAG_PRINTER
},
101 { "page-right", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
102 { "page-right-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
103 { "page-top", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
104 { "page-top-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
105 { "penwidth", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
106 { "penwidth-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
107 { "ppi", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
108 { "ppi-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
109 { "prettyprint", IPP_TAG_BOOLEAN
, IPP_TAG_JOB
},
110 { "prettyprint-default", IPP_TAG_BOOLEAN
, IPP_TAG_PRINTER
},
111 { "print-quality", IPP_TAG_ENUM
, IPP_TAG_JOB
},
112 { "print-quality-default", IPP_TAG_ENUM
, IPP_TAG_PRINTER
},
113 { "printer-error-policy", IPP_TAG_NAME
, IPP_TAG_PRINTER
},
114 { "printer-info", IPP_TAG_TEXT
, IPP_TAG_PRINTER
},
115 { "printer-is-accepting-jobs",IPP_TAG_BOOLEAN
, IPP_TAG_PRINTER
},
116 { "printer-is-shared", IPP_TAG_BOOLEAN
, IPP_TAG_PRINTER
},
117 { "printer-make-and-model", IPP_TAG_TEXT
, IPP_TAG_PRINTER
},
118 { "printer-more-info", IPP_TAG_URI
, IPP_TAG_PRINTER
},
119 { "printer-op-policy", IPP_TAG_NAME
, IPP_TAG_PRINTER
},
120 { "printer-resolution", IPP_TAG_RESOLUTION
, IPP_TAG_JOB
},
121 { "printer-state", IPP_TAG_ENUM
, IPP_TAG_PRINTER
},
122 { "printer-state-change-time",IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
123 { "printer-state-reasons", IPP_TAG_KEYWORD
, IPP_TAG_PRINTER
},
124 { "printer-type", IPP_TAG_ENUM
, IPP_TAG_PRINTER
},
125 { "printer-uri", IPP_TAG_URI
, IPP_TAG_OPERATION
},
126 { "queued-job-count", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
127 { "raw", IPP_TAG_MIMETYPE
, IPP_TAG_OPERATION
},
128 { "requesting-user-name-allowed", IPP_TAG_NAME
, IPP_TAG_PRINTER
},
129 { "requesting-user-name-denied", IPP_TAG_NAME
, IPP_TAG_PRINTER
},
130 { "resolution", IPP_TAG_RESOLUTION
, IPP_TAG_JOB
},
131 { "resolution-default", IPP_TAG_RESOLUTION
, IPP_TAG_PRINTER
},
132 { "saturation", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
133 { "saturation-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
134 { "scaling", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
135 { "scaling-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
136 { "sides", IPP_TAG_KEYWORD
, IPP_TAG_JOB
},
137 { "sides-default", IPP_TAG_KEYWORD
, IPP_TAG_PRINTER
},
138 { "wrap", IPP_TAG_BOOLEAN
, IPP_TAG_JOB
},
139 { "wrap-default", IPP_TAG_BOOLEAN
, IPP_TAG_PRINTER
}
147 static int compare_ipp_options(_ipp_option_t
*a
, _ipp_option_t
*b
);
151 * 'cupsEncodeOptions()' - Encode printer options into IPP attributes.
153 * This function adds operation, job, and then subscription attributes,
154 * in that order. Use the cupsEncodeOptions2() function to add attributes
155 * for a single group.
159 cupsEncodeOptions(ipp_t
*ipp
, /* I - Request to add to */
160 int num_options
, /* I - Number of options */
161 cups_option_t
*options
) /* I - Options */
163 DEBUG_printf(("cupsEncodeOptions(%p, %d, %p)\n", ipp
, num_options
, options
));
166 * Add the options in the proper groups & order...
169 cupsEncodeOptions2(ipp
, num_options
, options
, IPP_TAG_OPERATION
);
170 cupsEncodeOptions2(ipp
, num_options
, options
, IPP_TAG_JOB
);
171 cupsEncodeOptions2(ipp
, num_options
, options
, IPP_TAG_SUBSCRIPTION
);
176 * 'cupsEncodeOptions2()' - Encode printer options into IPP attributes for a group.
178 * This function only adds attributes for a single group. Call this
179 * function multiple times for each group, or use cupsEncodeOptions()
180 * to add the standard groups.
187 ipp_t
*ipp
, /* I - Request to add to */
188 int num_options
, /* I - Number of options */
189 cups_option_t
*options
, /* I - Options */
190 ipp_tag_t group_tag
) /* I - Group to encode */
192 int i
, j
; /* Looping vars */
193 int count
; /* Number of values */
194 char *s
, /* Pointer into option value */
195 *val
, /* Pointer to option value */
196 *copy
, /* Copy of option value */
197 *sep
; /* Option separator */
198 ipp_attribute_t
*attr
; /* IPP attribute */
199 ipp_tag_t value_tag
; /* IPP value tag */
200 cups_option_t
*option
; /* Current option */
203 DEBUG_printf(("cupsEncodeOptions2(ipp=%p, num_options=%d, options=%p, "
204 "group_tag=%x)\n", ipp
, num_options
, options
, group_tag
));
207 * Range check input...
210 if (!ipp
|| num_options
< 1 || !options
)
214 * Do special handling for the document-format/raw options...
217 if (group_tag
== IPP_TAG_OPERATION
)
220 * Handle the document format stuff first...
223 if ((val
= (char *)cupsGetOption("document-format", num_options
, options
)) != NULL
)
224 ippAddString(ipp
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
, "document-format",
226 else if (cupsGetOption("raw", num_options
, options
))
227 ippAddString(ipp
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
, "document-format",
228 NULL
, "application/vnd.cups-raw");
230 ippAddString(ipp
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
, "document-format",
231 NULL
, "application/octet-stream");
235 * Then loop through the options...
238 for (i
= num_options
, option
= options
; i
> 0; i
--, option
++)
240 _ipp_option_t
*match
; /* Matching attribute */
244 * Skip document format options that are handled above...
247 if (!strcasecmp(option
->name
, "raw") ||
248 !strcasecmp(option
->name
, "document-format") ||
253 * Figure out the proper value and group tags for this option...
256 if ((match
= _ippFindOption(option
->name
)) != NULL
)
258 if (match
->group_tag
!= group_tag
)
261 value_tag
= match
->value_tag
;
265 int namelen
; /* Length of name */
268 namelen
= strlen(option
->name
);
270 if (namelen
< 9 || strcmp(option
->name
+ namelen
- 8, "-default"))
272 if (group_tag
!= IPP_TAG_JOB
)
275 else if (group_tag
!= IPP_TAG_PRINTER
)
278 if (!strcasecmp(option
->value
, "true") ||
279 !strcasecmp(option
->value
, "false"))
280 value_tag
= IPP_TAG_BOOLEAN
;
282 value_tag
= IPP_TAG_NAME
;
286 * Count the number of values...
289 for (count
= 1, sep
= option
->value
; *sep
; sep
++)
294 * Skip quoted option value...
299 while (*sep
&& *sep
!= '\'')
305 else if (*sep
== '\"')
308 * Skip quoted option value...
313 while (*sep
&& *sep
!= '\"')
319 else if (*sep
== ',')
321 else if (*sep
== '\\' && sep
[1])
325 DEBUG_printf(("cupsEncodeOptions2: option = \'%s\', count = %d\n",
326 option
->name
, count
));
329 * Allocate memory for the attribute values...
332 if ((attr
= _ippAddAttr(ipp
, count
)) == NULL
)
338 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for attributes!");
343 * Now figure out what type of value we have...
346 attr
->group_tag
= group_tag
;
347 attr
->value_tag
= value_tag
;
350 * Copy the name over...
353 if ((attr
->name
= _cupsStrAlloc(option
->name
)) == NULL
)
359 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for name!");
366 * Make a copy of the value we can fiddle with...
369 if ((copy
= strdup(option
->value
)) == NULL
)
375 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for value copy!");
384 * Since we have a single value, use the value directly...
392 * Scan the value string for values...
395 for (j
= 0; j
< count
; val
= sep
, j
++)
398 * Find the end of this value and mark it if needed...
401 if ((sep
= strchr(val
, ',')) != NULL
)
404 sep
= val
+ strlen(val
);
407 * Copy the option value(s) over as needed by the type...
410 switch (attr
->value_tag
)
412 case IPP_TAG_INTEGER
:
415 * Integer/enumeration value...
418 attr
->values
[j
].integer
= strtol(val
, &s
, 0);
420 DEBUG_printf(("cupsEncodeOptions2: Added integer option value %d...\n",
421 attr
->values
[j
].integer
));
424 case IPP_TAG_BOOLEAN
:
425 if (!strcasecmp(val
, "true") ||
426 !strcasecmp(val
, "on") ||
427 !strcasecmp(val
, "yes"))
430 * Boolean value - true...
433 attr
->values
[j
].boolean
= 1;
435 DEBUG_puts("cupsEncodeOptions2: Added boolean true value...");
440 * Boolean value - false...
443 attr
->values
[j
].boolean
= 0;
445 DEBUG_puts("cupsEncodeOptions2: Added boolean false value...");
456 attr
->values
[j
].range
.lower
= 1;
460 attr
->values
[j
].range
.lower
= strtol(val
, &s
, 0);
465 attr
->values
[j
].range
.upper
= strtol(s
+ 1, NULL
, 0);
467 attr
->values
[j
].range
.upper
= 2147483647;
470 attr
->values
[j
].range
.upper
= attr
->values
[j
].range
.lower
;
472 DEBUG_printf(("cupsEncodeOptions2: Added range option value %d-%d...\n",
473 attr
->values
[j
].range
.lower
,
474 attr
->values
[j
].range
.upper
));
477 case IPP_TAG_RESOLUTION
:
482 attr
->values
[j
].resolution
.xres
= strtol(val
, &s
, 0);
485 attr
->values
[j
].resolution
.yres
= strtol(s
+ 1, &s
, 0);
487 attr
->values
[j
].resolution
.yres
= attr
->values
[j
].resolution
.xres
;
489 if (!strcasecmp(s
, "dpc"))
490 attr
->values
[j
].resolution
.units
= IPP_RES_PER_CM
;
492 attr
->values
[j
].resolution
.units
= IPP_RES_PER_INCH
;
494 DEBUG_printf(("cupsEncodeOptions2: Added resolution option value %s...\n",
498 case IPP_TAG_STRING
:
503 attr
->values
[j
].unknown
.length
= strlen(val
);
504 attr
->values
[j
].unknown
.data
= _cupsStrAlloc(val
);
506 DEBUG_printf(("cupsEncodeOptions2: Added octet-string value \"%s\"...\n",
507 attr
->values
[j
].unknown
.data
));
511 if ((attr
->values
[j
].string
.text
= _cupsStrAlloc(val
)) == NULL
)
517 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for string!");
521 DEBUG_printf(("cupsEncodeOptions2: Added string value \"%s\"...\n",
534 * '_ippFindOption()' - Find the attribute information for an option.
537 _ipp_option_t
* /* O - Attribute information */
538 _ippFindOption(const char *name
) /* I - Option/attribute name */
540 _ipp_option_t key
; /* Search key */
544 * Lookup the proper value and group tags for this option...
549 return ((_ipp_option_t
*)bsearch(&key
, ipp_options
,
550 sizeof(ipp_options
) / sizeof(ipp_options
[0]),
551 sizeof(ipp_options
[0]),
552 (int (*)(const void *, const void *))
553 compare_ipp_options
));
558 * 'compare_ipp_options()' - Compare two IPP options.
561 static int /* O - Result of comparison */
562 compare_ipp_options(_ipp_option_t
*a
, /* I - First option */
563 _ipp_option_t
*b
) /* I - Second option */
565 return (strcmp(a
->name
, b
->name
));
570 * End of "$Id: encode.c 5753 2006-07-18 19:53:24Z mike $".