4 * PPD file routines for CUPS.
6 * Copyright 2007-2015 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * PostScript is a trademark of Adobe Systems, Inc.
17 * This code and any derivative of it may be used and distributed
18 * freely under the terms of the GNU General Public License when
19 * used with GNU Ghostscript or its derivatives. Use of the code
20 * (or any derivative of it) with software other than GNU
21 * GhostScript (or its derivatives) is governed by the CUPS license
24 * This file is subject to the Apple OS-Developed Software exception.
28 * Include necessary headers.
31 #include "cups-private.h"
32 #include "ppd-private.h"
39 #define ppd_free(p) if (p) free(p) /* Safe free macro */
41 #define PPD_KEYWORD 1 /* Line contained a keyword */
42 #define PPD_OPTION 2 /* Line contained an option name */
43 #define PPD_TEXT 4 /* Line contained human-readable text */
44 #define PPD_STRING 8 /* Line contained a string or code */
46 #define PPD_HASHSIZE 512 /* Size of hash */
50 * Line buffer structure...
53 typedef struct _ppd_line_s
55 char *buffer
; /* Pointer to buffer */
56 size_t bufsize
; /* Size of the buffer */
64 static ppd_attr_t
*ppd_add_attr(ppd_file_t
*ppd
, const char *name
,
65 const char *spec
, const char *text
,
67 static ppd_choice_t
*ppd_add_choice(ppd_option_t
*option
, const char *name
);
68 static ppd_size_t
*ppd_add_size(ppd_file_t
*ppd
, const char *name
);
69 static int ppd_compare_attrs(ppd_attr_t
*a
, ppd_attr_t
*b
);
70 static int ppd_compare_choices(ppd_choice_t
*a
, ppd_choice_t
*b
);
71 static int ppd_compare_coptions(ppd_coption_t
*a
,
73 static int ppd_compare_options(ppd_option_t
*a
, ppd_option_t
*b
);
74 static int ppd_decode(char *string
);
75 static void ppd_free_filters(ppd_file_t
*ppd
);
76 static void ppd_free_group(ppd_group_t
*group
);
77 static void ppd_free_option(ppd_option_t
*option
);
78 static ppd_coption_t
*ppd_get_coption(ppd_file_t
*ppd
, const char *name
);
79 static ppd_cparam_t
*ppd_get_cparam(ppd_coption_t
*opt
,
82 static ppd_group_t
*ppd_get_group(ppd_file_t
*ppd
, const char *name
,
83 const char *text
, _cups_globals_t
*cg
,
84 cups_encoding_t encoding
);
85 static ppd_option_t
*ppd_get_option(ppd_group_t
*group
, const char *name
);
86 static int ppd_hash_option(ppd_option_t
*option
);
87 static int ppd_read(cups_file_t
*fp
, _ppd_line_t
*line
,
88 char *keyword
, char *option
, char *text
,
89 char **string
, int ignoreblank
,
91 static int ppd_update_filters(ppd_file_t
*ppd
,
96 * 'ppdClose()' - Free all memory used by the PPD file.
100 ppdClose(ppd_file_t
*ppd
) /* I - PPD file record */
102 int i
; /* Looping var */
103 ppd_emul_t
*emul
; /* Current emulation */
104 ppd_group_t
*group
; /* Current group */
105 char **font
; /* Current font */
106 ppd_attr_t
**attr
; /* Current attribute */
107 ppd_coption_t
*coption
; /* Current custom option */
108 ppd_cparam_t
*cparam
; /* Current custom parameter */
112 * Range check arguments...
119 * Free all strings at the top level...
122 _cupsStrFree(ppd
->lang_encoding
);
123 _cupsStrFree(ppd
->nickname
);
126 _cupsStrFree(ppd
->jcl_begin
);
127 _cupsStrFree(ppd
->jcl_end
);
128 _cupsStrFree(ppd
->jcl_ps
);
131 * Free any emulations...
134 if (ppd
->num_emulations
> 0)
136 for (i
= ppd
->num_emulations
, emul
= ppd
->emulations
; i
> 0; i
--, emul
++)
138 _cupsStrFree(emul
->start
);
139 _cupsStrFree(emul
->stop
);
142 ppd_free(ppd
->emulations
);
146 * Free any UI groups, subgroups, and options...
149 if (ppd
->num_groups
> 0)
151 for (i
= ppd
->num_groups
, group
= ppd
->groups
; i
> 0; i
--, group
++)
152 ppd_free_group(group
);
154 ppd_free(ppd
->groups
);
157 cupsArrayDelete(ppd
->options
);
158 cupsArrayDelete(ppd
->marked
);
161 * Free any page sizes...
164 if (ppd
->num_sizes
> 0)
165 ppd_free(ppd
->sizes
);
168 * Free any constraints...
171 if (ppd
->num_consts
> 0)
172 ppd_free(ppd
->consts
);
175 * Free any filters...
178 ppd_free_filters(ppd
);
184 if (ppd
->num_fonts
> 0)
186 for (i
= ppd
->num_fonts
, font
= ppd
->fonts
; i
> 0; i
--, font
++)
189 ppd_free(ppd
->fonts
);
193 * Free any profiles...
196 if (ppd
->num_profiles
> 0)
197 ppd_free(ppd
->profiles
);
200 * Free any attributes...
203 if (ppd
->num_attrs
> 0)
205 for (i
= ppd
->num_attrs
, attr
= ppd
->attrs
; i
> 0; i
--, attr
++)
207 _cupsStrFree((*attr
)->value
);
211 ppd_free(ppd
->attrs
);
214 cupsArrayDelete(ppd
->sorted_attrs
);
217 * Free custom options...
220 for (coption
= (ppd_coption_t
*)cupsArrayFirst(ppd
->coptions
);
222 coption
= (ppd_coption_t
*)cupsArrayNext(ppd
->coptions
))
224 for (cparam
= (ppd_cparam_t
*)cupsArrayFirst(coption
->params
);
226 cparam
= (ppd_cparam_t
*)cupsArrayNext(coption
->params
))
228 switch (cparam
->type
)
230 case PPD_CUSTOM_PASSCODE
:
231 case PPD_CUSTOM_PASSWORD
:
232 case PPD_CUSTOM_STRING
:
233 _cupsStrFree(cparam
->current
.custom_string
);
243 cupsArrayDelete(coption
->params
);
248 cupsArrayDelete(ppd
->coptions
);
251 * Free constraints...
254 if (ppd
->cups_uiconstraints
)
256 _ppd_cups_uiconsts_t
*consts
; /* Current constraints */
259 for (consts
= (_ppd_cups_uiconsts_t
*)cupsArrayFirst(ppd
->cups_uiconstraints
);
261 consts
= (_ppd_cups_uiconsts_t
*)cupsArrayNext(ppd
->cups_uiconstraints
))
263 free(consts
->constraints
);
267 cupsArrayDelete(ppd
->cups_uiconstraints
);
271 * Free any PPD cache/mapping data...
275 _ppdCacheDestroy(ppd
->cache
);
278 * Free the whole record...
286 * 'ppdErrorString()' - Returns the text associated with a status.
288 * @since CUPS 1.1.19/OS X 10.3@
291 const char * /* O - Status string */
292 ppdErrorString(ppd_status_t status
) /* I - PPD status */
294 static const char * const messages
[] =/* Status messages */
297 _("Unable to open PPD file"),
298 _("NULL PPD file pointer"),
299 _("Memory allocation error"),
300 _("Missing PPD-Adobe-4.x header"),
301 _("Missing value string"),
304 _("OpenGroup without a CloseGroup first"),
305 _("Bad OpenUI/JCLOpenUI"),
306 _("OpenUI/JCLOpenUI without a CloseUI/JCLCloseUI first"),
307 _("Bad OrderDependency"),
308 _("Bad UIConstraints"),
309 _("Missing asterisk in column 1"),
310 _("Line longer than the maximum allowed (255 characters)"),
311 _("Illegal control character"),
312 _("Illegal main keyword string"),
313 _("Illegal option keyword string"),
314 _("Illegal translation string"),
315 _("Illegal whitespace character"),
316 _("Bad custom parameter"),
317 _("Missing option keyword"),
318 _("Bad value string"),
319 _("Missing CloseGroup")
323 if (status
< PPD_OK
|| status
>= PPD_MAX_STATUS
)
324 return (_cupsLangString(cupsLangDefault(), _("Unknown")));
326 return (_cupsLangString(cupsLangDefault(), messages
[status
]));
331 * '_ppdGetEncoding()' - Get the CUPS encoding value for the given
335 cups_encoding_t
/* O - CUPS encoding value */
336 _ppdGetEncoding(const char *name
) /* I - LanguageEncoding string */
338 if (!_cups_strcasecmp(name
, "ISOLatin1"))
339 return (CUPS_ISO8859_1
);
340 else if (!_cups_strcasecmp(name
, "ISOLatin2"))
341 return (CUPS_ISO8859_2
);
342 else if (!_cups_strcasecmp(name
, "ISOLatin5"))
343 return (CUPS_ISO8859_5
);
344 else if (!_cups_strcasecmp(name
, "JIS83-RKSJ"))
345 return (CUPS_JIS_X0213
);
346 else if (!_cups_strcasecmp(name
, "MacStandard"))
347 return (CUPS_MAC_ROMAN
);
348 else if (!_cups_strcasecmp(name
, "WindowsANSI"))
349 return (CUPS_WINDOWS_1252
);
356 * 'ppdLastError()' - Return the status from the last ppdOpen*().
358 * @since CUPS 1.1.19/OS X 10.3@
361 ppd_status_t
/* O - Status code */
362 ppdLastError(int *line
) /* O - Line number */
364 _cups_globals_t
*cg
= _cupsGlobals();
369 *line
= cg
->ppd_line
;
371 return (cg
->ppd_status
);
376 * '_ppdOpen()' - Read a PPD file into memory.
378 * @since CUPS 1.2/OS X 10.5@
381 ppd_file_t
* /* O - PPD file record or @code NULL@ if the PPD file could not be opened. */
383 cups_file_t
*fp
, /* I - File to read from */
384 _ppd_localization_t localization
) /* I - Localization to load */
386 int i
, j
, k
; /* Looping vars */
387 int count
; /* Temporary count */
388 _ppd_line_t line
; /* Line buffer */
389 ppd_file_t
*ppd
; /* PPD file record */
390 ppd_group_t
*group
, /* Current group */
391 *subgroup
; /* Current sub-group */
392 ppd_option_t
*option
; /* Current option */
393 ppd_choice_t
*choice
; /* Current choice */
394 ppd_const_t
*constraint
; /* Current constraint */
395 ppd_size_t
*size
; /* Current page size */
396 int mask
; /* Line data mask */
397 char keyword
[PPD_MAX_NAME
],
398 /* Keyword from file */
400 /* Option from file */
402 /* Human-readable text from file */
403 *string
, /* Code/text from file */
404 *sptr
, /* Pointer into string */
405 *nameptr
, /* Pointer into name */
406 *temp
, /* Temporary string pointer */
407 **tempfonts
; /* Temporary fonts pointer */
408 float order
; /* Order dependency number */
409 ppd_section_t section
; /* Order dependency section */
410 ppd_profile_t
*profile
; /* Pointer to color profile */
411 char **filter
; /* Pointer to filter */
412 struct lconv
*loc
; /* Locale data */
413 int ui_keyword
; /* Is this line a UI keyword? */
414 cups_lang_t
*lang
; /* Language data */
415 cups_encoding_t encoding
; /* Encoding of PPD file */
416 _cups_globals_t
*cg
= _cupsGlobals();
418 char custom_name
[PPD_MAX_NAME
];
419 /* CustomFoo attribute name */
420 ppd_attr_t
*custom_attr
; /* CustomFoo attribute */
421 char ll
[7], /* Base language + '.' */
422 ll_CC
[7]; /* Language w/country + '.' */
423 size_t ll_len
= 0, /* Base language length */
424 ll_CC_len
= 0; /* Language w/country length */
425 static const char * const ui_keywords
[] =
427 #ifdef CUPS_USE_FULL_UI_KEYWORDS_LIST
429 * Adobe defines some 41 keywords as "UI", meaning that they are
430 * user interface elements and that they should be treated as such
431 * even if the PPD creator doesn't use Open/CloseUI around them.
433 * Since this can cause previously invisible options to appear and
434 * confuse users, the default is to only treat the PageSize and
435 * PageRegion keywords this way.
437 /* Boolean keywords */
447 /* PickOne keywords */
460 "JCLFrameBufferSize",
481 #else /* !CUPS_USE_FULL_UI_KEYWORDS_LIST */
484 #endif /* CUPS_USE_FULL_UI_KEYWORDS_LIST */
486 static const char * const color_keywords
[] = /* Keywords associated with color profiles */
493 DEBUG_printf(("_ppdOpen(fp=%p)", fp
));
496 * Default to "OK" status...
499 cg
->ppd_status
= PPD_OK
;
503 * Range check input...
508 cg
->ppd_status
= PPD_NULL_FILE
;
513 * If only loading a single localization set up the strings to match...
516 if (localization
== _PPD_LOCALIZATION_DEFAULT
)
518 if ((lang
= cupsLangDefault()) == NULL
)
521 snprintf(ll_CC
, sizeof(ll_CC
), "%s.", lang
->language
);
524 * <rdar://problem/22130168>
526 * Need to use a different base language for some locales...
529 if (!strcmp(lang
->language
, "zh_HK"))
530 strlcpy(ll
, "zh_TW.", sizeof(ll
));
532 snprintf(ll
, sizeof(ll
), "%2.2s.", lang
->language
);
534 ll_CC_len
= strlen(ll_CC
);
537 DEBUG_printf(("2_ppdOpen: Loading localizations matching \"%s\" and \"%s\"",
542 * Grab the first line and make sure it reads '*PPD-Adobe: "major.minor"'...
548 mask
= ppd_read(fp
, &line
, keyword
, name
, text
, &string
, 0, cg
);
550 DEBUG_printf(("2_ppdOpen: mask=%x, keyword=\"%s\"...", mask
, keyword
));
553 strcmp(keyword
, "PPD-Adobe") ||
554 string
== NULL
|| string
[0] != '4')
557 * Either this is not a PPD file, or it is not a 4.x PPD file.
560 if (cg
->ppd_status
== PPD_OK
)
561 cg
->ppd_status
= PPD_MISSING_PPDADOBE4
;
563 _cupsStrFree(string
);
564 ppd_free(line
.buffer
);
569 DEBUG_printf(("2_ppdOpen: keyword=%s, string=%p", keyword
, string
));
571 _cupsStrFree(string
);
574 * Allocate memory for the PPD file record...
577 if ((ppd
= calloc(1, sizeof(ppd_file_t
))) == NULL
)
579 cg
->ppd_status
= PPD_ALLOC_ERROR
;
581 _cupsStrFree(string
);
582 ppd_free(line
.buffer
);
587 ppd
->language_level
= 2;
588 ppd
->color_device
= 0;
589 ppd
->colorspace
= PPD_CS_N
;
590 ppd
->landscape
= -90;
591 ppd
->coptions
= cupsArrayNew((cups_array_func_t
)ppd_compare_coptions
,
595 * Read lines from the PPD file and add them to the file record...
603 encoding
= CUPS_ISO8859_1
;
606 while ((mask
= ppd_read(fp
, &line
, keyword
, name
, text
, &string
, 1, cg
)) != 0)
608 DEBUG_printf(("2_ppdOpen: mask=%x, keyword=\"%s\", name=\"%s\", "
609 "text=\"%s\", string=%d chars...", mask
, keyword
, name
, text
,
610 string
? (int)strlen(string
) : 0));
612 if (strncmp(keyword
, "Default", 7) && !string
&&
613 cg
->ppd_conform
!= PPD_CONFORM_RELAXED
)
616 * Need a string value!
619 cg
->ppd_status
= PPD_MISSING_VALUE
;
627 * Certain main keywords (as defined by the PPD spec) may be used
628 * without the usual OpenUI/CloseUI stuff. Presumably this is just
629 * so that Adobe wouldn't completely break compatibility with PPD
630 * files prior to v4.0 of the spec, but it is hopelessly
631 * inconsistent... Catch these main keywords and automatically
632 * create the corresponding option, as needed...
638 * Previous line was a UI keyword...
646 * If we are filtering out keyword localizations, see if this line needs to
650 if (localization
!= _PPD_LOCALIZATION_ALL
&&
651 (temp
= strchr(keyword
, '.')) != NULL
&&
652 ((temp
- keyword
) == 2 || (temp
- keyword
) == 5) &&
653 _cups_isalpha(keyword
[0]) &&
654 _cups_isalpha(keyword
[1]) &&
655 (keyword
[2] == '.' ||
656 (keyword
[2] == '_' && _cups_isalpha(keyword
[3]) &&
657 _cups_isalpha(keyword
[4]) && keyword
[5] == '.')))
659 if (localization
== _PPD_LOCALIZATION_NONE
||
660 (localization
== _PPD_LOCALIZATION_DEFAULT
&&
661 strncmp(ll_CC
, keyword
, ll_CC_len
) &&
662 strncmp(ll
, keyword
, ll_len
)))
664 DEBUG_printf(("2_ppdOpen: Ignoring localization: \"%s\"\n", keyword
));
667 else if (localization
== _PPD_LOCALIZATION_ICC_PROFILES
)
670 * Only load localizations for the color profile related keywords...
674 i
< (int)(sizeof(color_keywords
) / sizeof(color_keywords
[0]));
677 if (!_cups_strcasecmp(temp
, color_keywords
[i
]))
681 if (i
>= (int)(sizeof(color_keywords
) / sizeof(color_keywords
[0])))
683 DEBUG_printf(("2_ppdOpen: Ignoring localization: \"%s\"\n", keyword
));
689 if (option
== NULL
&&
690 (mask
& (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
)) ==
691 (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
))
693 for (i
= 0; i
< (int)(sizeof(ui_keywords
) / sizeof(ui_keywords
[0])); i
++)
694 if (!strcmp(keyword
, ui_keywords
[i
]))
697 if (i
< (int)(sizeof(ui_keywords
) / sizeof(ui_keywords
[0])))
700 * Create the option in the appropriate group...
705 DEBUG_printf(("2_ppdOpen: FOUND ADOBE UI KEYWORD %s WITHOUT OPENUI!",
710 if ((group
= ppd_get_group(ppd
, "General", _("General"), cg
,
714 DEBUG_printf(("2_ppdOpen: Adding to group %s...", group
->text
));
715 option
= ppd_get_option(group
, keyword
);
719 option
= ppd_get_option(group
, keyword
);
723 cg
->ppd_status
= PPD_ALLOC_ERROR
;
729 * Now fill in the initial information for the option...
732 if (!strncmp(keyword
, "JCL", 3))
733 option
->section
= PPD_ORDER_JCL
;
735 option
->section
= PPD_ORDER_ANY
;
737 option
->order
= 10.0f
;
740 option
->ui
= PPD_UI_BOOLEAN
;
742 option
->ui
= PPD_UI_PICKONE
;
744 for (j
= 0; j
< ppd
->num_attrs
; j
++)
745 if (!strncmp(ppd
->attrs
[j
]->name
, "Default", 7) &&
746 !strcmp(ppd
->attrs
[j
]->name
+ 7, keyword
) &&
747 ppd
->attrs
[j
]->value
)
749 DEBUG_printf(("2_ppdOpen: Setting Default%s to %s via attribute...",
750 option
->keyword
, ppd
->attrs
[j
]->value
));
751 strlcpy(option
->defchoice
, ppd
->attrs
[j
]->value
,
752 sizeof(option
->defchoice
));
756 if (!strcmp(keyword
, "PageSize"))
757 strlcpy(option
->text
, _("Media Size"), sizeof(option
->text
));
758 else if (!strcmp(keyword
, "MediaType"))
759 strlcpy(option
->text
, _("Media Type"), sizeof(option
->text
));
760 else if (!strcmp(keyword
, "InputSlot"))
761 strlcpy(option
->text
, _("Media Source"), sizeof(option
->text
));
762 else if (!strcmp(keyword
, "ColorModel"))
763 strlcpy(option
->text
, _("Output Mode"), sizeof(option
->text
));
764 else if (!strcmp(keyword
, "Resolution"))
765 strlcpy(option
->text
, _("Resolution"), sizeof(option
->text
));
767 strlcpy(option
->text
, keyword
, sizeof(option
->text
));
771 if (!strcmp(keyword
, "LanguageLevel"))
772 ppd
->language_level
= atoi(string
);
773 else if (!strcmp(keyword
, "LanguageEncoding"))
776 * Say all PPD files are UTF-8, since we convert to UTF-8...
779 ppd
->lang_encoding
= _cupsStrAlloc("UTF-8");
780 encoding
= _ppdGetEncoding(string
);
782 else if (!strcmp(keyword
, "LanguageVersion"))
783 ppd
->lang_version
= string
;
784 else if (!strcmp(keyword
, "Manufacturer"))
785 ppd
->manufacturer
= string
;
786 else if (!strcmp(keyword
, "ModelName"))
787 ppd
->modelname
= string
;
788 else if (!strcmp(keyword
, "Protocols"))
789 ppd
->protocols
= string
;
790 else if (!strcmp(keyword
, "PCFileName"))
791 ppd
->pcfilename
= string
;
792 else if (!strcmp(keyword
, "NickName"))
794 if (encoding
!= CUPS_UTF8
)
796 cups_utf8_t utf8
[256]; /* UTF-8 version of NickName */
799 cupsCharsetToUTF8(utf8
, string
, sizeof(utf8
), encoding
);
800 ppd
->nickname
= _cupsStrAlloc((char *)utf8
);
803 ppd
->nickname
= _cupsStrAlloc(string
);
805 else if (!strcmp(keyword
, "Product"))
806 ppd
->product
= string
;
807 else if (!strcmp(keyword
, "ShortNickName"))
808 ppd
->shortnickname
= string
;
809 else if (!strcmp(keyword
, "TTRasterizer"))
810 ppd
->ttrasterizer
= string
;
811 else if (!strcmp(keyword
, "JCLBegin"))
813 ppd
->jcl_begin
= _cupsStrAlloc(string
);
814 ppd_decode(ppd
->jcl_begin
); /* Decode quoted string */
816 else if (!strcmp(keyword
, "JCLEnd"))
818 ppd
->jcl_end
= _cupsStrAlloc(string
);
819 ppd_decode(ppd
->jcl_end
); /* Decode quoted string */
821 else if (!strcmp(keyword
, "JCLToPSInterpreter"))
823 ppd
->jcl_ps
= _cupsStrAlloc(string
);
824 ppd_decode(ppd
->jcl_ps
); /* Decode quoted string */
826 else if (!strcmp(keyword
, "AccurateScreensSupport"))
827 ppd
->accurate_screens
= !strcmp(string
, "True");
828 else if (!strcmp(keyword
, "ColorDevice"))
829 ppd
->color_device
= !strcmp(string
, "True");
830 else if (!strcmp(keyword
, "ContoneOnly"))
831 ppd
->contone_only
= !strcmp(string
, "True");
832 else if (!strcmp(keyword
, "cupsFlipDuplex"))
833 ppd
->flip_duplex
= !strcmp(string
, "True");
834 else if (!strcmp(keyword
, "cupsManualCopies"))
835 ppd
->manual_copies
= !strcmp(string
, "True");
836 else if (!strcmp(keyword
, "cupsModelNumber"))
837 ppd
->model_number
= atoi(string
);
838 else if (!strcmp(keyword
, "cupsColorProfile"))
840 if (ppd
->num_profiles
== 0)
841 profile
= malloc(sizeof(ppd_profile_t
));
843 profile
= realloc(ppd
->profiles
, sizeof(ppd_profile_t
) * (size_t)(ppd
->num_profiles
+ 1));
847 cg
->ppd_status
= PPD_ALLOC_ERROR
;
852 ppd
->profiles
= profile
;
853 profile
+= ppd
->num_profiles
;
854 ppd
->num_profiles
++;
856 memset(profile
, 0, sizeof(ppd_profile_t
));
857 strlcpy(profile
->resolution
, name
, sizeof(profile
->resolution
));
858 strlcpy(profile
->media_type
, text
, sizeof(profile
->media_type
));
860 profile
->density
= (float)_cupsStrScand(string
, &sptr
, loc
);
861 profile
->gamma
= (float)_cupsStrScand(sptr
, &sptr
, loc
);
862 profile
->matrix
[0][0] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
863 profile
->matrix
[0][1] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
864 profile
->matrix
[0][2] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
865 profile
->matrix
[1][0] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
866 profile
->matrix
[1][1] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
867 profile
->matrix
[1][2] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
868 profile
->matrix
[2][0] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
869 profile
->matrix
[2][1] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
870 profile
->matrix
[2][2] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
872 else if (!strcmp(keyword
, "cupsFilter"))
874 if (ppd
->num_filters
== 0)
875 filter
= malloc(sizeof(char *));
877 filter
= realloc(ppd
->filters
, sizeof(char *) * (size_t)(ppd
->num_filters
+ 1));
881 cg
->ppd_status
= PPD_ALLOC_ERROR
;
886 ppd
->filters
= filter
;
887 filter
+= ppd
->num_filters
;
891 * Retain a copy of the filter string...
894 *filter
= _cupsStrRetain(string
);
896 else if (!strcmp(keyword
, "Throughput"))
897 ppd
->throughput
= atoi(string
);
898 else if (!strcmp(keyword
, "Font"))
901 * Add this font to the list of available fonts...
904 if (ppd
->num_fonts
== 0)
905 tempfonts
= (char **)malloc(sizeof(char *));
907 tempfonts
= (char **)realloc(ppd
->fonts
, sizeof(char *) * (size_t)(ppd
->num_fonts
+ 1));
909 if (tempfonts
== NULL
)
911 cg
->ppd_status
= PPD_ALLOC_ERROR
;
916 ppd
->fonts
= tempfonts
;
917 ppd
->fonts
[ppd
->num_fonts
] = _cupsStrAlloc(name
);
920 else if (!strncmp(keyword
, "ParamCustom", 11))
922 ppd_coption_t
*coption
; /* Custom option */
923 ppd_cparam_t
*cparam
; /* Custom parameter */
924 int corder
; /* Order number */
925 char ctype
[33], /* Data type */
926 cminimum
[65], /* Minimum value */
927 cmaximum
[65]; /* Maximum value */
931 * Get the custom option and parameter...
934 if ((coption
= ppd_get_coption(ppd
, keyword
+ 11)) == NULL
)
936 cg
->ppd_status
= PPD_ALLOC_ERROR
;
941 if ((cparam
= ppd_get_cparam(coption
, name
, text
)) == NULL
)
943 cg
->ppd_status
= PPD_ALLOC_ERROR
;
949 * Get the parameter data...
953 sscanf(string
, "%d%32s%64s%64s", &corder
, ctype
, cminimum
,
956 cg
->ppd_status
= PPD_BAD_CUSTOM_PARAM
;
961 cparam
->order
= corder
;
963 if (!strcmp(ctype
, "curve"))
965 cparam
->type
= PPD_CUSTOM_CURVE
;
966 cparam
->minimum
.custom_curve
= (float)_cupsStrScand(cminimum
, NULL
, loc
);
967 cparam
->maximum
.custom_curve
= (float)_cupsStrScand(cmaximum
, NULL
, loc
);
969 else if (!strcmp(ctype
, "int"))
971 cparam
->type
= PPD_CUSTOM_INT
;
972 cparam
->minimum
.custom_int
= atoi(cminimum
);
973 cparam
->maximum
.custom_int
= atoi(cmaximum
);
975 else if (!strcmp(ctype
, "invcurve"))
977 cparam
->type
= PPD_CUSTOM_INVCURVE
;
978 cparam
->minimum
.custom_invcurve
= (float)_cupsStrScand(cminimum
, NULL
, loc
);
979 cparam
->maximum
.custom_invcurve
= (float)_cupsStrScand(cmaximum
, NULL
, loc
);
981 else if (!strcmp(ctype
, "passcode"))
983 cparam
->type
= PPD_CUSTOM_PASSCODE
;
984 cparam
->minimum
.custom_passcode
= atoi(cminimum
);
985 cparam
->maximum
.custom_passcode
= atoi(cmaximum
);
987 else if (!strcmp(ctype
, "password"))
989 cparam
->type
= PPD_CUSTOM_PASSWORD
;
990 cparam
->minimum
.custom_password
= atoi(cminimum
);
991 cparam
->maximum
.custom_password
= atoi(cmaximum
);
993 else if (!strcmp(ctype
, "points"))
995 cparam
->type
= PPD_CUSTOM_POINTS
;
996 cparam
->minimum
.custom_points
= (float)_cupsStrScand(cminimum
, NULL
, loc
);
997 cparam
->maximum
.custom_points
= (float)_cupsStrScand(cmaximum
, NULL
, loc
);
999 else if (!strcmp(ctype
, "real"))
1001 cparam
->type
= PPD_CUSTOM_REAL
;
1002 cparam
->minimum
.custom_real
= (float)_cupsStrScand(cminimum
, NULL
, loc
);
1003 cparam
->maximum
.custom_real
= (float)_cupsStrScand(cmaximum
, NULL
, loc
);
1005 else if (!strcmp(ctype
, "string"))
1007 cparam
->type
= PPD_CUSTOM_STRING
;
1008 cparam
->minimum
.custom_string
= atoi(cminimum
);
1009 cparam
->maximum
.custom_string
= atoi(cmaximum
);
1013 cg
->ppd_status
= PPD_BAD_CUSTOM_PARAM
;
1019 * Now special-case for CustomPageSize...
1022 if (!strcmp(coption
->keyword
, "PageSize"))
1024 if (!strcmp(name
, "Width"))
1026 ppd
->custom_min
[0] = cparam
->minimum
.custom_points
;
1027 ppd
->custom_max
[0] = cparam
->maximum
.custom_points
;
1029 else if (!strcmp(name
, "Height"))
1031 ppd
->custom_min
[1] = cparam
->minimum
.custom_points
;
1032 ppd
->custom_max
[1] = cparam
->maximum
.custom_points
;
1036 else if (!strcmp(keyword
, "HWMargins"))
1038 for (i
= 0, sptr
= string
; i
< 4; i
++)
1039 ppd
->custom_margins
[i
] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
1041 else if (!strncmp(keyword
, "Custom", 6) && !strcmp(name
, "True") && !option
)
1043 ppd_option_t
*custom_option
; /* Custom option */
1045 DEBUG_puts("2_ppdOpen: Processing Custom option...");
1048 * Get the option and custom option...
1051 if (!ppd_get_coption(ppd
, keyword
+ 6))
1053 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1058 if (option
&& !_cups_strcasecmp(option
->keyword
, keyword
+ 6))
1059 custom_option
= option
;
1061 custom_option
= ppdFindOption(ppd
, keyword
+ 6);
1066 * Add the "custom" option...
1069 if ((choice
= ppdFindChoice(custom_option
, "Custom")) == NULL
)
1070 if ((choice
= ppd_add_choice(custom_option
, "Custom")) == NULL
)
1072 DEBUG_puts("1_ppdOpen: Unable to add Custom choice!");
1074 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1079 strlcpy(choice
->text
, text
[0] ? text
: _("Custom"),
1080 sizeof(choice
->text
));
1082 choice
->code
= _cupsStrAlloc(string
);
1084 if (custom_option
->section
== PPD_ORDER_JCL
)
1085 ppd_decode(choice
->code
);
1089 * Now process custom page sizes specially...
1092 if (!strcmp(keyword
, "CustomPageSize"))
1095 * Add a "Custom" page size entry...
1098 ppd
->variable_sizes
= 1;
1100 ppd_add_size(ppd
, "Custom");
1102 if (option
&& !_cups_strcasecmp(option
->keyword
, "PageRegion"))
1103 custom_option
= option
;
1105 custom_option
= ppdFindOption(ppd
, "PageRegion");
1109 if ((choice
= ppdFindChoice(custom_option
, "Custom")) == NULL
)
1110 if ((choice
= ppd_add_choice(custom_option
, "Custom")) == NULL
)
1112 DEBUG_puts("1_ppdOpen: Unable to add Custom choice!");
1114 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1119 strlcpy(choice
->text
, text
[0] ? text
: _("Custom"),
1120 sizeof(choice
->text
));
1124 else if (!strcmp(keyword
, "LandscapeOrientation"))
1126 if (!strcmp(string
, "Minus90"))
1127 ppd
->landscape
= -90;
1128 else if (!strcmp(string
, "Plus90"))
1129 ppd
->landscape
= 90;
1131 else if (!strcmp(keyword
, "Emulators") && string
)
1133 for (count
= 1, sptr
= string
; sptr
!= NULL
;)
1134 if ((sptr
= strchr(sptr
, ' ')) != NULL
)
1137 while (*sptr
== ' ')
1141 ppd
->num_emulations
= count
;
1142 if ((ppd
->emulations
= calloc((size_t)count
, sizeof(ppd_emul_t
))) == NULL
)
1144 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1149 for (i
= 0, sptr
= string
; i
< count
; i
++)
1151 for (nameptr
= ppd
->emulations
[i
].name
;
1152 *sptr
!= '\0' && *sptr
!= ' ';
1154 if (nameptr
< (ppd
->emulations
[i
].name
+ sizeof(ppd
->emulations
[i
].name
) - 1))
1159 while (*sptr
== ' ')
1163 else if (!strncmp(keyword
, "StartEmulator_", 14))
1167 for (i
= 0; i
< ppd
->num_emulations
; i
++)
1168 if (!strcmp(keyword
+ 14, ppd
->emulations
[i
].name
))
1170 ppd
->emulations
[i
].start
= string
;
1174 else if (!strncmp(keyword
, "StopEmulator_", 13))
1178 for (i
= 0; i
< ppd
->num_emulations
; i
++)
1179 if (!strcmp(keyword
+ 13, ppd
->emulations
[i
].name
))
1181 ppd
->emulations
[i
].stop
= string
;
1185 else if (!strcmp(keyword
, "JobPatchFile"))
1188 * CUPS STR #3421: Check for "*JobPatchFile: int: string"
1191 if (isdigit(*string
& 255))
1193 for (sptr
= string
+ 1; isdigit(*sptr
& 255); sptr
++);
1198 * Found "*JobPatchFile: int: string"...
1201 cg
->ppd_status
= PPD_BAD_VALUE
;
1207 if (!name
[0] && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1210 * Found "*JobPatchFile: string"...
1213 cg
->ppd_status
= PPD_MISSING_OPTION_KEYWORD
;
1218 if (ppd
->patches
== NULL
)
1219 ppd
->patches
= strdup(string
);
1222 temp
= realloc(ppd
->patches
, strlen(ppd
->patches
) +
1223 strlen(string
) + 1);
1226 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1231 ppd
->patches
= temp
;
1233 memcpy(ppd
->patches
+ strlen(ppd
->patches
), string
, strlen(string
) + 1);
1236 else if (!strcmp(keyword
, "OpenUI"))
1239 * Don't allow nesting of options...
1242 if (option
&& cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1244 cg
->ppd_status
= PPD_NESTED_OPEN_UI
;
1250 * Add an option record to the current sub-group, group, or file...
1253 DEBUG_printf(("2_ppdOpen: name=\"%s\" (%d)", name
, (int)strlen(name
)));
1256 _cups_strcpy(name
, name
+ 1); /* Eliminate leading asterisk */
1258 for (i
= (int)strlen(name
) - 1; i
> 0 && _cups_isspace(name
[i
]); i
--)
1259 name
[i
] = '\0'; /* Eliminate trailing spaces */
1261 DEBUG_printf(("2_ppdOpen: OpenUI of %s in group %s...", name
,
1262 group
? group
->text
: "(null)"));
1264 if (subgroup
!= NULL
)
1265 option
= ppd_get_option(subgroup
, name
);
1266 else if (group
== NULL
)
1268 if ((group
= ppd_get_group(ppd
, "General", _("General"), cg
,
1272 DEBUG_printf(("2_ppdOpen: Adding to group %s...", group
->text
));
1273 option
= ppd_get_option(group
, name
);
1277 option
= ppd_get_option(group
, name
);
1281 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1287 * Now fill in the initial information for the option...
1290 if (string
&& !strcmp(string
, "PickMany"))
1291 option
->ui
= PPD_UI_PICKMANY
;
1292 else if (string
&& !strcmp(string
, "Boolean"))
1293 option
->ui
= PPD_UI_BOOLEAN
;
1294 else if (string
&& !strcmp(string
, "PickOne"))
1295 option
->ui
= PPD_UI_PICKONE
;
1296 else if (cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1298 cg
->ppd_status
= PPD_BAD_OPEN_UI
;
1303 option
->ui
= PPD_UI_PICKONE
;
1305 for (j
= 0; j
< ppd
->num_attrs
; j
++)
1306 if (!strncmp(ppd
->attrs
[j
]->name
, "Default", 7) &&
1307 !strcmp(ppd
->attrs
[j
]->name
+ 7, name
) &&
1308 ppd
->attrs
[j
]->value
)
1310 DEBUG_printf(("2_ppdOpen: Setting Default%s to %s via attribute...",
1311 option
->keyword
, ppd
->attrs
[j
]->value
));
1312 strlcpy(option
->defchoice
, ppd
->attrs
[j
]->value
,
1313 sizeof(option
->defchoice
));
1318 cupsCharsetToUTF8((cups_utf8_t
*)option
->text
, text
,
1319 sizeof(option
->text
), encoding
);
1322 if (!strcmp(name
, "PageSize"))
1323 strlcpy(option
->text
, _("Media Size"), sizeof(option
->text
));
1324 else if (!strcmp(name
, "MediaType"))
1325 strlcpy(option
->text
, _("Media Type"), sizeof(option
->text
));
1326 else if (!strcmp(name
, "InputSlot"))
1327 strlcpy(option
->text
, _("Media Source"), sizeof(option
->text
));
1328 else if (!strcmp(name
, "ColorModel"))
1329 strlcpy(option
->text
, _("Output Mode"), sizeof(option
->text
));
1330 else if (!strcmp(name
, "Resolution"))
1331 strlcpy(option
->text
, _("Resolution"), sizeof(option
->text
));
1333 strlcpy(option
->text
, name
, sizeof(option
->text
));
1336 option
->section
= PPD_ORDER_ANY
;
1338 _cupsStrFree(string
);
1342 * Add a custom option choice if we have already seen a CustomFoo
1346 if (!_cups_strcasecmp(name
, "PageRegion"))
1347 strlcpy(custom_name
, "CustomPageSize", sizeof(custom_name
));
1349 snprintf(custom_name
, sizeof(custom_name
), "Custom%s", name
);
1351 if ((custom_attr
= ppdFindAttr(ppd
, custom_name
, "True")) != NULL
)
1353 if ((choice
= ppdFindChoice(option
, "Custom")) == NULL
)
1354 if ((choice
= ppd_add_choice(option
, "Custom")) == NULL
)
1356 DEBUG_puts("1_ppdOpen: Unable to add Custom choice!");
1358 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1363 strlcpy(choice
->text
,
1364 custom_attr
->text
[0] ? custom_attr
->text
: _("Custom"),
1365 sizeof(choice
->text
));
1366 choice
->code
= _cupsStrRetain(custom_attr
->value
);
1369 else if (!strcmp(keyword
, "JCLOpenUI"))
1372 * Don't allow nesting of options...
1375 if (option
&& cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1377 cg
->ppd_status
= PPD_NESTED_OPEN_UI
;
1383 * Find the JCL group, and add if needed...
1386 group
= ppd_get_group(ppd
, "JCL", _("JCL"), cg
, encoding
);
1392 * Add an option record to the current JCLs...
1396 _cups_strcpy(name
, name
+ 1);
1398 option
= ppd_get_option(group
, name
);
1402 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1408 * Now fill in the initial information for the option...
1411 if (string
&& !strcmp(string
, "PickMany"))
1412 option
->ui
= PPD_UI_PICKMANY
;
1413 else if (string
&& !strcmp(string
, "Boolean"))
1414 option
->ui
= PPD_UI_BOOLEAN
;
1415 else if (string
&& !strcmp(string
, "PickOne"))
1416 option
->ui
= PPD_UI_PICKONE
;
1419 cg
->ppd_status
= PPD_BAD_OPEN_UI
;
1424 for (j
= 0; j
< ppd
->num_attrs
; j
++)
1425 if (!strncmp(ppd
->attrs
[j
]->name
, "Default", 7) &&
1426 !strcmp(ppd
->attrs
[j
]->name
+ 7, name
) &&
1427 ppd
->attrs
[j
]->value
)
1429 DEBUG_printf(("2_ppdOpen: Setting Default%s to %s via attribute...",
1430 option
->keyword
, ppd
->attrs
[j
]->value
));
1431 strlcpy(option
->defchoice
, ppd
->attrs
[j
]->value
,
1432 sizeof(option
->defchoice
));
1437 cupsCharsetToUTF8((cups_utf8_t
*)option
->text
, text
,
1438 sizeof(option
->text
), encoding
);
1440 strlcpy(option
->text
, name
, sizeof(option
->text
));
1442 option
->section
= PPD_ORDER_JCL
;
1445 _cupsStrFree(string
);
1449 * Add a custom option choice if we have already seen a CustomFoo
1453 snprintf(custom_name
, sizeof(custom_name
), "Custom%s", name
);
1455 if ((custom_attr
= ppdFindAttr(ppd
, custom_name
, "True")) != NULL
)
1457 if ((choice
= ppd_add_choice(option
, "Custom")) == NULL
)
1459 DEBUG_puts("1_ppdOpen: Unable to add Custom choice!");
1461 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1466 strlcpy(choice
->text
,
1467 custom_attr
->text
[0] ? custom_attr
->text
: _("Custom"),
1468 sizeof(choice
->text
));
1469 choice
->code
= _cupsStrRetain(custom_attr
->value
);
1472 else if (!strcmp(keyword
, "CloseUI") || !strcmp(keyword
, "JCLCloseUI"))
1476 _cupsStrFree(string
);
1479 else if (!strcmp(keyword
, "OpenGroup"))
1482 * Open a new group...
1487 cg
->ppd_status
= PPD_NESTED_OPEN_GROUP
;
1494 cg
->ppd_status
= PPD_BAD_OPEN_GROUP
;
1500 * Separate the group name from the text (name/text)...
1503 if ((sptr
= strchr(string
, '/')) != NULL
)
1509 * Fix up the text...
1515 * Find/add the group...
1518 group
= ppd_get_group(ppd
, string
, sptr
, cg
, encoding
);
1523 _cupsStrFree(string
);
1526 else if (!strcmp(keyword
, "CloseGroup"))
1530 _cupsStrFree(string
);
1533 else if (!strcmp(keyword
, "OrderDependency"))
1535 order
= (float)_cupsStrScand(string
, &sptr
, loc
);
1537 if (!sptr
|| sscanf(sptr
, "%40s%40s", name
, keyword
) != 2)
1539 cg
->ppd_status
= PPD_BAD_ORDER_DEPENDENCY
;
1544 if (keyword
[0] == '*')
1545 _cups_strcpy(keyword
, keyword
+ 1);
1547 if (!strcmp(name
, "ExitServer"))
1548 section
= PPD_ORDER_EXIT
;
1549 else if (!strcmp(name
, "Prolog"))
1550 section
= PPD_ORDER_PROLOG
;
1551 else if (!strcmp(name
, "DocumentSetup"))
1552 section
= PPD_ORDER_DOCUMENT
;
1553 else if (!strcmp(name
, "PageSetup"))
1554 section
= PPD_ORDER_PAGE
;
1555 else if (!strcmp(name
, "JCLSetup"))
1556 section
= PPD_ORDER_JCL
;
1558 section
= PPD_ORDER_ANY
;
1566 * Only valid for Non-UI options...
1569 for (i
= ppd
->num_groups
, gtemp
= ppd
->groups
; i
> 0; i
--, gtemp
++)
1570 if (gtemp
->text
[0] == '\0')
1574 for (i
= 0; i
< gtemp
->num_options
; i
++)
1575 if (!strcmp(keyword
, gtemp
->options
[i
].keyword
))
1577 gtemp
->options
[i
].section
= section
;
1578 gtemp
->options
[i
].order
= order
;
1584 option
->section
= section
;
1585 option
->order
= order
;
1588 _cupsStrFree(string
);
1591 else if (!strncmp(keyword
, "Default", 7))
1597 * Drop UI text, if any, from value...
1600 if (strchr(string
, '/') != NULL
)
1601 *strchr(string
, '/') = '\0';
1604 * Assign the default value as appropriate...
1607 if (!strcmp(keyword
, "DefaultColorSpace"))
1610 * Set default colorspace...
1613 if (!strcmp(string
, "CMY"))
1614 ppd
->colorspace
= PPD_CS_CMY
;
1615 else if (!strcmp(string
, "CMYK"))
1616 ppd
->colorspace
= PPD_CS_CMYK
;
1617 else if (!strcmp(string
, "RGB"))
1618 ppd
->colorspace
= PPD_CS_RGB
;
1619 else if (!strcmp(string
, "RGBK"))
1620 ppd
->colorspace
= PPD_CS_RGBK
;
1621 else if (!strcmp(string
, "N"))
1622 ppd
->colorspace
= PPD_CS_N
;
1624 ppd
->colorspace
= PPD_CS_GRAY
;
1626 else if (option
&& !strcmp(keyword
+ 7, option
->keyword
))
1629 * Set the default as part of the current option...
1632 DEBUG_printf(("2_ppdOpen: Setting %s to %s...", keyword
, string
));
1634 strlcpy(option
->defchoice
, string
, sizeof(option
->defchoice
));
1636 DEBUG_printf(("2_ppdOpen: %s is now %s...", keyword
, option
->defchoice
));
1641 * Lookup option and set if it has been defined...
1644 ppd_option_t
*toption
; /* Temporary option */
1647 if ((toption
= ppdFindOption(ppd
, keyword
+ 7)) != NULL
)
1649 DEBUG_printf(("2_ppdOpen: Setting %s to %s...", keyword
, string
));
1650 strlcpy(toption
->defchoice
, string
, sizeof(toption
->defchoice
));
1654 else if (!strcmp(keyword
, "UIConstraints") ||
1655 !strcmp(keyword
, "NonUIConstraints"))
1659 cg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1663 if (ppd
->num_consts
== 0)
1664 constraint
= calloc(2, sizeof(ppd_const_t
));
1666 constraint
= realloc(ppd
->consts
, (size_t)(ppd
->num_consts
+ 2) * sizeof(ppd_const_t
));
1668 if (constraint
== NULL
)
1670 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1675 ppd
->consts
= constraint
;
1676 constraint
+= ppd
->num_consts
;
1679 switch (sscanf(string
, "%40s%40s%40s%40s", constraint
->option1
,
1680 constraint
->choice1
, constraint
->option2
,
1681 constraint
->choice2
))
1683 case 0 : /* Error */
1684 case 1 : /* Error */
1685 cg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1688 case 2 : /* Two options... */
1690 * Check for broken constraints like "* Option"...
1693 if (cg
->ppd_conform
== PPD_CONFORM_STRICT
&&
1694 (!strcmp(constraint
->option1
, "*") ||
1695 !strcmp(constraint
->choice1
, "*")))
1697 cg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1702 * The following strcpy's are safe, as optionN and
1703 * choiceN are all the same size (size defined by PPD spec...)
1706 if (constraint
->option1
[0] == '*')
1707 _cups_strcpy(constraint
->option1
, constraint
->option1
+ 1);
1708 else if (cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1710 cg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1714 if (constraint
->choice1
[0] == '*')
1715 _cups_strcpy(constraint
->option2
, constraint
->choice1
+ 1);
1716 else if (cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1718 cg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1722 constraint
->choice1
[0] = '\0';
1723 constraint
->choice2
[0] = '\0';
1726 case 3 : /* Two options, one choice... */
1728 * Check for broken constraints like "* Option"...
1731 if (cg
->ppd_conform
== PPD_CONFORM_STRICT
&&
1732 (!strcmp(constraint
->option1
, "*") ||
1733 !strcmp(constraint
->choice1
, "*") ||
1734 !strcmp(constraint
->option2
, "*")))
1736 cg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1741 * The following _cups_strcpy's are safe, as optionN and
1742 * choiceN are all the same size (size defined by PPD spec...)
1745 if (constraint
->option1
[0] == '*')
1746 _cups_strcpy(constraint
->option1
, constraint
->option1
+ 1);
1747 else if (cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1749 cg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1753 if (constraint
->choice1
[0] == '*')
1755 if (cg
->ppd_conform
== PPD_CONFORM_STRICT
&&
1756 constraint
->option2
[0] == '*')
1758 cg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1762 _cups_strcpy(constraint
->choice2
, constraint
->option2
);
1763 _cups_strcpy(constraint
->option2
, constraint
->choice1
+ 1);
1764 constraint
->choice1
[0] = '\0';
1768 if (constraint
->option2
[0] == '*')
1769 _cups_strcpy(constraint
->option2
, constraint
->option2
+ 1);
1770 else if (cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1772 cg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1776 constraint
->choice2
[0] = '\0';
1780 case 4 : /* Two options, two choices... */
1782 * Check for broken constraints like "* Option"...
1785 if (cg
->ppd_conform
== PPD_CONFORM_STRICT
&&
1786 (!strcmp(constraint
->option1
, "*") ||
1787 !strcmp(constraint
->choice1
, "*") ||
1788 !strcmp(constraint
->option2
, "*") ||
1789 !strcmp(constraint
->choice2
, "*")))
1791 cg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1795 if (constraint
->option1
[0] == '*')
1796 _cups_strcpy(constraint
->option1
, constraint
->option1
+ 1);
1797 else if (cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1799 cg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1803 if (cg
->ppd_conform
== PPD_CONFORM_STRICT
&&
1804 constraint
->choice1
[0] == '*')
1806 cg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1810 if (constraint
->option2
[0] == '*')
1811 _cups_strcpy(constraint
->option2
, constraint
->option2
+ 1);
1812 else if (cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1814 cg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1818 if (cg
->ppd_conform
== PPD_CONFORM_STRICT
&&
1819 constraint
->choice2
[0] == '*')
1821 cg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1828 * Don't add this one as an attribute...
1831 _cupsStrFree(string
);
1834 else if (!strcmp(keyword
, "PaperDimension"))
1836 if ((size
= ppdPageSize(ppd
, name
)) == NULL
)
1837 size
= ppd_add_size(ppd
, name
);
1842 * Unable to add or find size!
1845 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1850 size
->width
= (float)_cupsStrScand(string
, &sptr
, loc
);
1851 size
->length
= (float)_cupsStrScand(sptr
, NULL
, loc
);
1853 _cupsStrFree(string
);
1856 else if (!strcmp(keyword
, "ImageableArea"))
1858 if ((size
= ppdPageSize(ppd
, name
)) == NULL
)
1859 size
= ppd_add_size(ppd
, name
);
1864 * Unable to add or find size!
1867 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1872 size
->left
= (float)_cupsStrScand(string
, &sptr
, loc
);
1873 size
->bottom
= (float)_cupsStrScand(sptr
, &sptr
, loc
);
1874 size
->right
= (float)_cupsStrScand(sptr
, &sptr
, loc
);
1875 size
->top
= (float)_cupsStrScand(sptr
, NULL
, loc
);
1877 _cupsStrFree(string
);
1880 else if (option
!= NULL
&&
1881 (mask
& (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
)) ==
1882 (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
) &&
1883 !strcmp(keyword
, option
->keyword
))
1885 DEBUG_printf(("2_ppdOpen: group=%p, subgroup=%p", group
, subgroup
));
1887 if (!strcmp(keyword
, "PageSize"))
1890 * Add a page size...
1893 if (ppdPageSize(ppd
, name
) == NULL
)
1894 ppd_add_size(ppd
, name
);
1898 * Add the option choice...
1901 if ((choice
= ppd_add_choice(option
, name
)) == NULL
)
1903 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1909 cupsCharsetToUTF8((cups_utf8_t
*)choice
->text
, text
,
1910 sizeof(choice
->text
), encoding
);
1911 else if (!strcmp(name
, "True"))
1912 strlcpy(choice
->text
, _("Yes"), sizeof(choice
->text
));
1913 else if (!strcmp(name
, "False"))
1914 strlcpy(choice
->text
, _("No"), sizeof(choice
->text
));
1916 strlcpy(choice
->text
, name
, sizeof(choice
->text
));
1918 if (option
->section
== PPD_ORDER_JCL
)
1919 ppd_decode(string
); /* Decode quoted string */
1921 choice
->code
= string
;
1922 string
= NULL
; /* Don't add as an attribute below */
1926 * Add remaining lines with keywords and string values as attributes...
1930 (mask
& (PPD_KEYWORD
| PPD_STRING
)) == (PPD_KEYWORD
| PPD_STRING
))
1931 ppd_add_attr(ppd
, keyword
, name
, text
, string
);
1933 _cupsStrFree(string
);
1937 * Check for a missing CloseGroup...
1940 if (group
&& cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1942 cg
->ppd_status
= PPD_MISSING_CLOSE_GROUP
;
1946 ppd_free(line
.buffer
);
1949 * Reset language preferences...
1953 if (!cupsFileEOF(fp
))
1954 DEBUG_printf(("1_ppdOpen: Premature EOF at %lu...\n",
1955 (unsigned long)cupsFileTell(fp
)));
1958 if (cg
->ppd_status
!= PPD_OK
)
1961 * Had an error reading the PPD file, cannot continue!
1970 * Update the filters array as needed...
1973 if (!ppd_update_filters(ppd
, cg
))
1981 * Create the sorted options array and set the option back-pointer for
1982 * each choice and custom option...
1985 ppd
->options
= cupsArrayNew2((cups_array_func_t
)ppd_compare_options
, NULL
,
1986 (cups_ahash_func_t
)ppd_hash_option
,
1989 for (i
= ppd
->num_groups
, group
= ppd
->groups
;
1993 for (j
= group
->num_options
, option
= group
->options
;
1997 ppd_coption_t
*coption
; /* Custom option */
2000 cupsArrayAdd(ppd
->options
, option
);
2002 for (k
= 0; k
< option
->num_choices
; k
++)
2003 option
->choices
[k
].option
= option
;
2005 if ((coption
= ppdFindCustomOption(ppd
, option
->keyword
)) != NULL
)
2006 coption
->option
= option
;
2011 * Create an array to track the marked choices...
2014 ppd
->marked
= cupsArrayNew((cups_array_func_t
)ppd_compare_choices
, NULL
);
2017 * Return the PPD file structure...
2023 * Common exit point for errors to save code size...
2028 _cupsStrFree(string
);
2029 ppd_free(line
.buffer
);
2038 * 'ppdOpen()' - Read a PPD file into memory.
2041 ppd_file_t
* /* O - PPD file record */
2042 ppdOpen(FILE *fp
) /* I - File to read from */
2044 ppd_file_t
*ppd
; /* PPD file record */
2045 cups_file_t
*cf
; /* CUPS file */
2049 * Reopen the stdio file as a CUPS file...
2052 if ((cf
= cupsFileOpenFd(fileno(fp
), "r")) == NULL
)
2056 * Load the PPD file using the newer API...
2059 ppd
= _ppdOpen(cf
, _PPD_LOCALIZATION_DEFAULT
);
2062 * Close the CUPS file and return the PPD...
2072 * 'ppdOpen2()' - Read a PPD file into memory.
2074 * @since CUPS 1.2/OS X 10.5@
2077 ppd_file_t
* /* O - PPD file record or @code NULL@ if the PPD file could not be opened. */
2078 ppdOpen2(cups_file_t
*fp
) /* I - File to read from */
2080 return _ppdOpen(fp
, _PPD_LOCALIZATION_DEFAULT
);
2085 * 'ppdOpenFd()' - Read a PPD file into memory.
2088 ppd_file_t
* /* O - PPD file record or @code NULL@ if the PPD file could not be opened. */
2089 ppdOpenFd(int fd
) /* I - File to read from */
2091 cups_file_t
*fp
; /* CUPS file pointer */
2092 ppd_file_t
*ppd
; /* PPD file record */
2093 _cups_globals_t
*cg
= _cupsGlobals();
2098 * Set the line number to 0...
2104 * Range check input...
2109 cg
->ppd_status
= PPD_NULL_FILE
;
2115 * Try to open the file and parse it...
2118 if ((fp
= cupsFileOpenFd(fd
, "r")) != NULL
)
2126 cg
->ppd_status
= PPD_FILE_OPEN_ERROR
;
2135 * '_ppdOpenFile()' - Read a PPD file into memory.
2138 ppd_file_t
* /* O - PPD file record or @code NULL@ if the PPD file could not be opened. */
2139 _ppdOpenFile(const char *filename
, /* I - File to read from */
2140 _ppd_localization_t localization
) /* I - Localization to load */
2142 cups_file_t
*fp
; /* File pointer */
2143 ppd_file_t
*ppd
; /* PPD file record */
2144 _cups_globals_t
*cg
= _cupsGlobals();
2149 * Set the line number to 0...
2155 * Range check input...
2158 if (filename
== NULL
)
2160 cg
->ppd_status
= PPD_NULL_FILE
;
2166 * Try to open the file and parse it...
2169 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
2171 ppd
= _ppdOpen(fp
, localization
);
2177 cg
->ppd_status
= PPD_FILE_OPEN_ERROR
;
2186 * 'ppdOpenFile()' - Read a PPD file into memory.
2189 ppd_file_t
* /* O - PPD file record or @code NULL@ if the PPD file could not be opened. */
2190 ppdOpenFile(const char *filename
) /* I - File to read from */
2192 return _ppdOpenFile(filename
, _PPD_LOCALIZATION_DEFAULT
);
2197 * 'ppdSetConformance()' - Set the conformance level for PPD files.
2199 * @since CUPS 1.1.20/OS X 10.4@
2203 ppdSetConformance(ppd_conform_t c
) /* I - Conformance level */
2205 _cups_globals_t
*cg
= _cupsGlobals();
2209 cg
->ppd_conform
= c
;
2214 * 'ppd_add_attr()' - Add an attribute to the PPD data.
2217 static ppd_attr_t
* /* O - New attribute */
2218 ppd_add_attr(ppd_file_t
*ppd
, /* I - PPD file data */
2219 const char *name
, /* I - Attribute name */
2220 const char *spec
, /* I - Specifier string, if any */
2221 const char *text
, /* I - Text string, if any */
2222 const char *value
) /* I - Value of attribute */
2224 ppd_attr_t
**ptr
, /* New array */
2225 *temp
; /* New attribute */
2229 * Range check input...
2232 if (ppd
== NULL
|| name
== NULL
|| spec
== NULL
)
2236 * Create the array as needed...
2239 if (!ppd
->sorted_attrs
)
2240 ppd
->sorted_attrs
= cupsArrayNew((cups_array_func_t
)ppd_compare_attrs
,
2244 * Allocate memory for the new attribute...
2247 if (ppd
->num_attrs
== 0)
2248 ptr
= malloc(sizeof(ppd_attr_t
*));
2250 ptr
= realloc(ppd
->attrs
, (size_t)(ppd
->num_attrs
+ 1) * sizeof(ppd_attr_t
*));
2256 ptr
+= ppd
->num_attrs
;
2258 if ((temp
= calloc(1, sizeof(ppd_attr_t
))) == NULL
)
2269 strlcpy(temp
->name
, name
, sizeof(temp
->name
));
2270 strlcpy(temp
->spec
, spec
, sizeof(temp
->spec
));
2271 strlcpy(temp
->text
, text
, sizeof(temp
->text
));
2272 temp
->value
= (char *)value
;
2275 * Add the attribute to the sorted array...
2278 cupsArrayAdd(ppd
->sorted_attrs
, temp
);
2281 * Return the attribute...
2289 * 'ppd_add_choice()' - Add a choice to an option.
2292 static ppd_choice_t
* /* O - Named choice */
2293 ppd_add_choice(ppd_option_t
*option
, /* I - Option */
2294 const char *name
) /* I - Name of choice */
2296 ppd_choice_t
*choice
; /* Choice */
2299 if (option
->num_choices
== 0)
2300 choice
= malloc(sizeof(ppd_choice_t
));
2302 choice
= realloc(option
->choices
, sizeof(ppd_choice_t
) * (size_t)(option
->num_choices
+ 1));
2307 option
->choices
= choice
;
2308 choice
+= option
->num_choices
;
2309 option
->num_choices
++;
2311 memset(choice
, 0, sizeof(ppd_choice_t
));
2312 strlcpy(choice
->choice
, name
, sizeof(choice
->choice
));
2319 * 'ppd_add_size()' - Add a page size.
2322 static ppd_size_t
* /* O - Named size */
2323 ppd_add_size(ppd_file_t
*ppd
, /* I - PPD file */
2324 const char *name
) /* I - Name of size */
2326 ppd_size_t
*size
; /* Size */
2329 if (ppd
->num_sizes
== 0)
2330 size
= malloc(sizeof(ppd_size_t
));
2332 size
= realloc(ppd
->sizes
, sizeof(ppd_size_t
) * (size_t)(ppd
->num_sizes
+ 1));
2338 size
+= ppd
->num_sizes
;
2341 memset(size
, 0, sizeof(ppd_size_t
));
2342 strlcpy(size
->name
, name
, sizeof(size
->name
));
2349 * 'ppd_compare_attrs()' - Compare two attributes.
2352 static int /* O - Result of comparison */
2353 ppd_compare_attrs(ppd_attr_t
*a
, /* I - First attribute */
2354 ppd_attr_t
*b
) /* I - Second attribute */
2356 return (_cups_strcasecmp(a
->name
, b
->name
));
2361 * 'ppd_compare_choices()' - Compare two choices...
2364 static int /* O - Result of comparison */
2365 ppd_compare_choices(ppd_choice_t
*a
, /* I - First choice */
2366 ppd_choice_t
*b
) /* I - Second choice */
2368 return (strcmp(a
->option
->keyword
, b
->option
->keyword
));
2373 * 'ppd_compare_coptions()' - Compare two custom options.
2376 static int /* O - Result of comparison */
2377 ppd_compare_coptions(ppd_coption_t
*a
, /* I - First option */
2378 ppd_coption_t
*b
) /* I - Second option */
2380 return (_cups_strcasecmp(a
->keyword
, b
->keyword
));
2385 * 'ppd_compare_options()' - Compare two options.
2388 static int /* O - Result of comparison */
2389 ppd_compare_options(ppd_option_t
*a
, /* I - First option */
2390 ppd_option_t
*b
) /* I - Second option */
2392 return (_cups_strcasecmp(a
->keyword
, b
->keyword
));
2397 * 'ppd_decode()' - Decode a string value...
2400 static int /* O - Length of decoded string */
2401 ppd_decode(char *string
) /* I - String to decode */
2403 char *inptr
, /* Input pointer */
2404 *outptr
; /* Output pointer */
2410 while (*inptr
!= '\0')
2411 if (*inptr
== '<' && isxdigit(inptr
[1] & 255))
2414 * Convert hex to 8-bit values...
2418 while (isxdigit(*inptr
& 255))
2420 if (_cups_isalpha(*inptr
))
2421 *outptr
= (char)((tolower(*inptr
) - 'a' + 10) << 4);
2423 *outptr
= (char)((*inptr
- '0') << 4);
2427 if (!isxdigit(*inptr
& 255))
2430 if (_cups_isalpha(*inptr
))
2431 *outptr
|= (char)(tolower(*inptr
) - 'a' + 10);
2433 *outptr
|= (char)(*inptr
- '0');
2439 while (*inptr
!= '>' && *inptr
!= '\0')
2441 while (*inptr
== '>')
2445 *outptr
++ = *inptr
++;
2449 return ((int)(outptr
- string
));
2454 * 'ppd_free_filters()' - Free the filters array.
2458 ppd_free_filters(ppd_file_t
*ppd
) /* I - PPD file */
2460 int i
; /* Looping var */
2461 char **filter
; /* Current filter */
2464 if (ppd
->num_filters
> 0)
2466 for (i
= ppd
->num_filters
, filter
= ppd
->filters
; i
> 0; i
--, filter
++)
2467 _cupsStrFree(*filter
);
2469 ppd_free(ppd
->filters
);
2471 ppd
->num_filters
= 0;
2472 ppd
->filters
= NULL
;
2478 * 'ppd_free_group()' - Free a single UI group.
2482 ppd_free_group(ppd_group_t
*group
) /* I - Group to free */
2484 int i
; /* Looping var */
2485 ppd_option_t
*option
; /* Current option */
2486 ppd_group_t
*subgroup
; /* Current sub-group */
2489 if (group
->num_options
> 0)
2491 for (i
= group
->num_options
, option
= group
->options
;
2494 ppd_free_option(option
);
2496 ppd_free(group
->options
);
2499 if (group
->num_subgroups
> 0)
2501 for (i
= group
->num_subgroups
, subgroup
= group
->subgroups
;
2504 ppd_free_group(subgroup
);
2506 ppd_free(group
->subgroups
);
2512 * 'ppd_free_option()' - Free a single option.
2516 ppd_free_option(ppd_option_t
*option
) /* I - Option to free */
2518 int i
; /* Looping var */
2519 ppd_choice_t
*choice
; /* Current choice */
2522 if (option
->num_choices
> 0)
2524 for (i
= option
->num_choices
, choice
= option
->choices
;
2528 _cupsStrFree(choice
->code
);
2531 ppd_free(option
->choices
);
2537 * 'ppd_get_coption()' - Get a custom option record.
2540 static ppd_coption_t
* /* O - Custom option... */
2541 ppd_get_coption(ppd_file_t
*ppd
, /* I - PPD file */
2542 const char *name
) /* I - Name of option */
2544 ppd_coption_t
*copt
; /* New custom option */
2548 * See if the option already exists...
2551 if ((copt
= ppdFindCustomOption(ppd
, name
)) != NULL
)
2555 * Not found, so create the custom option record...
2558 if ((copt
= calloc(1, sizeof(ppd_coption_t
))) == NULL
)
2561 strlcpy(copt
->keyword
, name
, sizeof(copt
->keyword
));
2563 copt
->params
= cupsArrayNew((cups_array_func_t
)NULL
, NULL
);
2565 cupsArrayAdd(ppd
->coptions
, copt
);
2568 * Return the new record...
2576 * 'ppd_get_cparam()' - Get a custom parameter record.
2579 static ppd_cparam_t
* /* O - Extended option... */
2580 ppd_get_cparam(ppd_coption_t
*opt
, /* I - PPD file */
2581 const char *param
, /* I - Name of parameter */
2582 const char *text
) /* I - Human-readable text */
2584 ppd_cparam_t
*cparam
; /* New custom parameter */
2588 * See if the parameter already exists...
2591 if ((cparam
= ppdFindCustomParam(opt
, param
)) != NULL
)
2595 * Not found, so create the custom parameter record...
2598 if ((cparam
= calloc(1, sizeof(ppd_cparam_t
))) == NULL
)
2601 strlcpy(cparam
->name
, param
, sizeof(cparam
->name
));
2602 strlcpy(cparam
->text
, text
[0] ? text
: param
, sizeof(cparam
->text
));
2605 * Add this record to the array...
2608 cupsArrayAdd(opt
->params
, cparam
);
2611 * Return the new record...
2619 * 'ppd_get_group()' - Find or create the named group as needed.
2622 static ppd_group_t
* /* O - Named group */
2623 ppd_get_group(ppd_file_t
*ppd
, /* I - PPD file */
2624 const char *name
, /* I - Name of group */
2625 const char *text
, /* I - Text for group */
2626 _cups_globals_t
*cg
, /* I - Global data */
2627 cups_encoding_t encoding
) /* I - Encoding of text */
2629 int i
; /* Looping var */
2630 ppd_group_t
*group
; /* Group */
2633 DEBUG_printf(("7ppd_get_group(ppd=%p, name=\"%s\", text=\"%s\", cg=%p)",
2634 ppd
, name
, text
, cg
));
2636 for (i
= ppd
->num_groups
, group
= ppd
->groups
; i
> 0; i
--, group
++)
2637 if (!strcmp(group
->name
, name
))
2642 DEBUG_printf(("8ppd_get_group: Adding group %s...", name
));
2644 if (cg
->ppd_conform
== PPD_CONFORM_STRICT
&& strlen(text
) >= sizeof(group
->text
))
2646 cg
->ppd_status
= PPD_ILLEGAL_TRANSLATION
;
2651 if (ppd
->num_groups
== 0)
2652 group
= malloc(sizeof(ppd_group_t
));
2654 group
= realloc(ppd
->groups
, (size_t)(ppd
->num_groups
+ 1) * sizeof(ppd_group_t
));
2658 cg
->ppd_status
= PPD_ALLOC_ERROR
;
2663 ppd
->groups
= group
;
2664 group
+= ppd
->num_groups
;
2667 memset(group
, 0, sizeof(ppd_group_t
));
2668 strlcpy(group
->name
, name
, sizeof(group
->name
));
2670 cupsCharsetToUTF8((cups_utf8_t
*)group
->text
, text
,
2671 sizeof(group
->text
), encoding
);
2679 * 'ppd_get_option()' - Find or create the named option as needed.
2682 static ppd_option_t
* /* O - Named option */
2683 ppd_get_option(ppd_group_t
*group
, /* I - Group */
2684 const char *name
) /* I - Name of option */
2686 int i
; /* Looping var */
2687 ppd_option_t
*option
; /* Option */
2690 DEBUG_printf(("7ppd_get_option(group=%p(\"%s\"), name=\"%s\")",
2691 group
, group
->name
, name
));
2693 for (i
= group
->num_options
, option
= group
->options
; i
> 0; i
--, option
++)
2694 if (!strcmp(option
->keyword
, name
))
2699 if (group
->num_options
== 0)
2700 option
= malloc(sizeof(ppd_option_t
));
2702 option
= realloc(group
->options
, (size_t)(group
->num_options
+ 1) * sizeof(ppd_option_t
));
2707 group
->options
= option
;
2708 option
+= group
->num_options
;
2709 group
->num_options
++;
2711 memset(option
, 0, sizeof(ppd_option_t
));
2712 strlcpy(option
->keyword
, name
, sizeof(option
->keyword
));
2720 * 'ppd_hash_option()' - Generate a hash of the option name...
2723 static int /* O - Hash index */
2724 ppd_hash_option(ppd_option_t
*option
) /* I - Option */
2726 int hash
= 0; /* Hash index */
2727 const char *k
; /* Pointer into keyword */
2730 for (hash
= option
->keyword
[0], k
= option
->keyword
+ 1; *k
;)
2731 hash
= 33 * hash
+ *k
++;
2733 return (hash
& 511);
2738 * 'ppd_read()' - Read a line from a PPD file, skipping comment lines as
2742 static int /* O - Bitmask of fields read */
2743 ppd_read(cups_file_t
*fp
, /* I - File to read from */
2744 _ppd_line_t
*line
, /* I - Line buffer */
2745 char *keyword
, /* O - Keyword from line */
2746 char *option
, /* O - Option from line */
2747 char *text
, /* O - Human-readable text from line */
2748 char **string
, /* O - Code/string data */
2749 int ignoreblank
, /* I - Ignore blank lines? */
2750 _cups_globals_t
*cg
) /* I - Global data */
2752 int ch
, /* Character from file */
2753 col
, /* Column in line */
2754 colon
, /* Colon seen? */
2755 endquote
, /* Waiting for an end quote */
2756 mask
, /* Mask to be returned */
2757 startline
, /* Start line */
2758 textlen
; /* Length of text */
2759 char *keyptr
, /* Keyword pointer */
2760 *optptr
, /* Option pointer */
2761 *textptr
, /* Text pointer */
2762 *strptr
, /* Pointer into string */
2763 *lineptr
; /* Current position in line buffer */
2767 * Now loop until we have a valid line...
2772 startline
= cg
->ppd_line
+ 1;
2776 line
->bufsize
= 1024;
2777 line
->buffer
= malloc(1024);
2789 lineptr
= line
->buffer
;
2793 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
2795 if (lineptr
>= (line
->buffer
+ line
->bufsize
- 1))
2798 * Expand the line buffer...
2801 char *temp
; /* Temporary line pointer */
2804 line
->bufsize
+= 1024;
2805 if (line
->bufsize
> 262144)
2808 * Don't allow lines longer than 256k!
2811 cg
->ppd_line
= startline
;
2812 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2817 temp
= realloc(line
->buffer
, line
->bufsize
);
2820 cg
->ppd_line
= startline
;
2821 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2826 lineptr
= temp
+ (lineptr
- line
->buffer
);
2827 line
->buffer
= temp
;
2830 if (ch
== '\r' || ch
== '\n')
2833 * Line feed or carriage return...
2842 * Check for a trailing line feed...
2845 if ((ch
= cupsFilePeekChar(fp
)) == EOF
)
2852 cupsFileGetChar(fp
);
2855 if (lineptr
== line
->buffer
&& ignoreblank
)
2856 continue; /* Skip blank lines */
2860 if (!endquote
) /* Continue for multi-line text */
2865 else if (ch
< ' ' && ch
!= '\t' && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2868 * Other control characters...
2871 cg
->ppd_line
= startline
;
2872 cg
->ppd_status
= PPD_ILLEGAL_CHARACTER
;
2876 else if (ch
!= 0x1a)
2879 * Any other character...
2882 *lineptr
++ = (char)ch
;
2885 if (col
> (PPD_MAX_LINE
- 1))
2888 * Line is too long...
2891 cg
->ppd_line
= startline
;
2892 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2897 if (ch
== ':' && strncmp(line
->buffer
, "*%", 2) != 0)
2900 if (ch
== '\"' && colon
)
2901 endquote
= !endquote
;
2908 * Didn't finish this quoted string...
2911 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
2914 else if (ch
== '\r' || ch
== '\n')
2922 * Check for a trailing line feed...
2925 if ((ch
= cupsFilePeekChar(fp
)) == EOF
)
2928 cupsFileGetChar(fp
);
2931 else if (ch
< ' ' && ch
!= '\t' && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2934 * Other control characters...
2937 cg
->ppd_line
= startline
;
2938 cg
->ppd_status
= PPD_ILLEGAL_CHARACTER
;
2942 else if (ch
!= 0x1a)
2946 if (col
> (PPD_MAX_LINE
- 1))
2949 * Line is too long...
2952 cg
->ppd_line
= startline
;
2953 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2963 * Didn't finish this line...
2966 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
2967 if (ch
== '\r' || ch
== '\n')
2970 * Line feed or carriage return...
2979 * Check for a trailing line feed...
2982 if ((ch
= cupsFilePeekChar(fp
)) == EOF
)
2985 cupsFileGetChar(fp
);
2990 else if (ch
< ' ' && ch
!= '\t' && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2993 * Other control characters...
2996 cg
->ppd_line
= startline
;
2997 cg
->ppd_status
= PPD_ILLEGAL_CHARACTER
;
3001 else if (ch
!= 0x1a)
3005 if (col
> (PPD_MAX_LINE
- 1))
3008 * Line is too long...
3011 cg
->ppd_line
= startline
;
3012 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
3019 if (lineptr
> line
->buffer
&& lineptr
[-1] == '\n')
3024 DEBUG_printf(("9ppd_read: LINE=\"%s\"", line
->buffer
));
3027 * The dynamically created PPDs for older style OS X
3028 * drivers include a large blob of data inserted as comments
3029 * at the end of the file. As an optimization we can stop
3030 * reading the PPD when we get to the start of this data.
3033 if (!strcmp(line
->buffer
, "*%APLWORKSET START"))
3036 if (ch
== EOF
&& lineptr
== line
->buffer
)
3044 lineptr
= line
->buffer
+ 1;
3051 if ((!line
->buffer
[0] || /* Blank line */
3052 !strncmp(line
->buffer
, "*%", 2) || /* Comment line */
3053 !strcmp(line
->buffer
, "*End")) && /* End of multi-line string */
3054 ignoreblank
) /* Ignore these? */
3056 startline
= cg
->ppd_line
+ 1;
3060 if (!strcmp(line
->buffer
, "*")) /* (Bad) comment line */
3062 if (cg
->ppd_conform
== PPD_CONFORM_RELAXED
)
3064 startline
= cg
->ppd_line
+ 1;
3069 cg
->ppd_line
= startline
;
3070 cg
->ppd_status
= PPD_ILLEGAL_MAIN_KEYWORD
;
3076 if (line
->buffer
[0] != '*') /* All lines start with an asterisk */
3079 * Allow lines consisting of just whitespace...
3082 for (lineptr
= line
->buffer
; *lineptr
; lineptr
++)
3083 if (*lineptr
&& !_cups_isspace(*lineptr
))
3088 cg
->ppd_status
= PPD_MISSING_ASTERISK
;
3091 else if (ignoreblank
)
3103 while (*lineptr
&& *lineptr
!= ':' && !_cups_isspace(*lineptr
))
3105 if (*lineptr
<= ' ' || *lineptr
> 126 || *lineptr
== '/' ||
3106 (keyptr
- keyword
) >= (PPD_MAX_NAME
- 1))
3108 cg
->ppd_status
= PPD_ILLEGAL_MAIN_KEYWORD
;
3112 *keyptr
++ = *lineptr
++;
3117 if (!strcmp(keyword
, "End"))
3120 mask
|= PPD_KEYWORD
;
3122 if (_cups_isspace(*lineptr
))
3125 * Get an option name...
3128 while (_cups_isspace(*lineptr
))
3133 while (*lineptr
&& !_cups_isspace(*lineptr
) && *lineptr
!= ':' &&
3136 if (*lineptr
<= ' ' || *lineptr
> 126 ||
3137 (optptr
- option
) >= (PPD_MAX_NAME
- 1))
3139 cg
->ppd_status
= PPD_ILLEGAL_OPTION_KEYWORD
;
3143 *optptr
++ = *lineptr
++;
3148 if (_cups_isspace(*lineptr
) && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
3150 cg
->ppd_status
= PPD_ILLEGAL_WHITESPACE
;
3154 while (_cups_isspace(*lineptr
))
3159 if (*lineptr
== '/')
3162 * Get human-readable text...
3169 while (*lineptr
!= '\0' && *lineptr
!= '\n' && *lineptr
!= ':')
3171 if (((unsigned char)*lineptr
< ' ' && *lineptr
!= '\t') ||
3172 (textptr
- text
) >= (PPD_MAX_LINE
- 1))
3174 cg
->ppd_status
= PPD_ILLEGAL_TRANSLATION
;
3178 *textptr
++ = *lineptr
++;
3182 textlen
= ppd_decode(text
);
3184 if (textlen
> PPD_MAX_TEXT
&& cg
->ppd_conform
== PPD_CONFORM_STRICT
)
3186 cg
->ppd_status
= PPD_ILLEGAL_TRANSLATION
;
3194 if (_cups_isspace(*lineptr
) && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
3196 cg
->ppd_status
= PPD_ILLEGAL_WHITESPACE
;
3200 while (_cups_isspace(*lineptr
))
3203 if (*lineptr
== ':')
3206 * Get string after triming leading and trailing whitespace...
3210 while (_cups_isspace(*lineptr
))
3213 strptr
= lineptr
+ strlen(lineptr
) - 1;
3214 while (strptr
>= lineptr
&& _cups_isspace(*strptr
))
3217 if (*strptr
== '\"')
3220 * Quoted string by itself, remove quotes...
3227 *string
= _cupsStrAlloc(lineptr
);
3239 * 'ppd_update_filters()' - Update the filters array as needed.
3241 * This function re-populates the filters array with cupsFilter2 entries that
3242 * have been stripped of the destination MIME media types and any maxsize hints.
3244 * (All for backwards-compatibility)
3247 static int /* O - 1 on success, 0 on failure */
3248 ppd_update_filters(ppd_file_t
*ppd
,/* I - PPD file */
3249 _cups_globals_t
*cg
) /* I - Global data */
3251 ppd_attr_t
*attr
; /* Current cupsFilter2 value */
3252 char srcsuper
[16], /* Source MIME media type */
3254 dstsuper
[16], /* Destination MIME media type */
3256 program
[1024], /* Command to run */
3257 *ptr
, /* Pointer into command to run */
3258 buffer
[1024], /* Re-written cupsFilter value */
3259 **filter
; /* Current filter */
3260 int cost
; /* Cost of filter */
3263 DEBUG_printf(("4ppd_update_filters(ppd=%p, cg=%p)", ppd
, cg
));
3266 * See if we have any cupsFilter2 lines...
3269 if ((attr
= ppdFindAttr(ppd
, "cupsFilter2", NULL
)) == NULL
)
3271 DEBUG_puts("5ppd_update_filters: No cupsFilter2 keywords present.");
3276 * Yes, free the cupsFilter-defined filters and re-build...
3279 ppd_free_filters(ppd
);
3284 * Parse the cupsFilter2 string:
3286 * src/type dst/type cost program
3287 * src/type dst/type cost maxsize(n) program
3290 DEBUG_printf(("5ppd_update_filters: cupsFilter2=\"%s\"", attr
->value
));
3292 if (sscanf(attr
->value
, "%15[^/]/%255s%*[ \t]%15[^/]/%255s%d%*[ \t]%1023[^\n]",
3293 srcsuper
, srctype
, dstsuper
, dsttype
, &cost
, program
) != 6)
3295 DEBUG_puts("5ppd_update_filters: Bad cupsFilter2 line.");
3296 cg
->ppd_status
= PPD_BAD_VALUE
;
3301 DEBUG_printf(("5ppd_update_filters: srcsuper=\"%s\", srctype=\"%s\", "
3302 "dstsuper=\"%s\", dsttype=\"%s\", cost=%d, program=\"%s\"",
3303 srcsuper
, srctype
, dstsuper
, dsttype
, cost
, program
));
3305 if (!strncmp(program
, "maxsize(", 8) &&
3306 (ptr
= strchr(program
+ 8, ')')) != NULL
)
3308 DEBUG_puts("5ppd_update_filters: Found maxsize(nnn).");
3311 while (_cups_isspace(*ptr
))
3314 _cups_strcpy(program
, ptr
);
3315 DEBUG_printf(("5ppd_update_filters: New program=\"%s\"", program
));
3319 * Convert to cupsFilter format:
3321 * src/type cost program
3324 snprintf(buffer
, sizeof(buffer
), "%s/%s %d %s", srcsuper
, srctype
, cost
,
3326 DEBUG_printf(("5ppd_update_filters: Adding \"%s\".", buffer
));
3329 * Add a cupsFilter-compatible string to the filters array.
3332 if (ppd
->num_filters
== 0)
3333 filter
= malloc(sizeof(char *));
3335 filter
= realloc(ppd
->filters
, sizeof(char *) * (size_t)(ppd
->num_filters
+ 1));
3339 DEBUG_puts("5ppd_update_filters: Out of memory.");
3340 cg
->ppd_status
= PPD_ALLOC_ERROR
;
3345 ppd
->filters
= filter
;
3346 filter
+= ppd
->num_filters
;
3347 ppd
->num_filters
++;
3349 *filter
= _cupsStrAlloc(buffer
);
3351 while ((attr
= ppdFindNextAttr(ppd
, "cupsFilter2", NULL
)) != NULL
);
3353 DEBUG_puts("5ppd_update_filters: Completed OK.");