2 * PPD file routines for CUPS.
4 * Copyright © 2007-2019 by Apple Inc.
5 * Copyright © 1997-2007 by Easy Software Products, all rights reserved.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
10 * PostScript is a trademark of Adobe Systems, Inc.
14 * Include necessary headers.
17 #include "cups-private.h"
18 #include "ppd-private.h"
19 #include "debug-internal.h"
26 #define ppd_free(p) if (p) free(p) /* Safe free macro */
28 #define PPD_KEYWORD 1 /* Line contained a keyword */
29 #define PPD_OPTION 2 /* Line contained an option name */
30 #define PPD_TEXT 4 /* Line contained human-readable text */
31 #define PPD_STRING 8 /* Line contained a string or code */
33 #define PPD_HASHSIZE 512 /* Size of hash */
37 * Line buffer structure...
40 typedef struct _ppd_line_s
42 char *buffer
; /* Pointer to buffer */
43 size_t bufsize
; /* Size of the buffer */
51 static _cups_threadkey_t ppd_globals_key
= _CUPS_THREADKEY_INITIALIZER
;
52 /* Thread local storage key */
54 static pthread_once_t ppd_globals_key_once
= PTHREAD_ONCE_INIT
;
55 /* One-time initialization object */
56 #endif /* HAVE_PTHREAD_H */
63 static ppd_attr_t
*ppd_add_attr(ppd_file_t
*ppd
, const char *name
,
64 const char *spec
, const char *text
,
66 static ppd_choice_t
*ppd_add_choice(ppd_option_t
*option
, const char *name
);
67 static ppd_size_t
*ppd_add_size(ppd_file_t
*ppd
, const char *name
);
68 static int ppd_compare_attrs(ppd_attr_t
*a
, ppd_attr_t
*b
);
69 static int ppd_compare_choices(ppd_choice_t
*a
, ppd_choice_t
*b
);
70 static int ppd_compare_coptions(ppd_coption_t
*a
,
72 static int ppd_compare_options(ppd_option_t
*a
, ppd_option_t
*b
);
73 static int ppd_decode(char *string
);
74 static void ppd_free_filters(ppd_file_t
*ppd
);
75 static void ppd_free_group(ppd_group_t
*group
);
76 static void ppd_free_option(ppd_option_t
*option
);
77 static ppd_coption_t
*ppd_get_coption(ppd_file_t
*ppd
, const char *name
);
78 static ppd_cparam_t
*ppd_get_cparam(ppd_coption_t
*opt
,
81 static ppd_group_t
*ppd_get_group(ppd_file_t
*ppd
, const char *name
,
82 const char *text
, _ppd_globals_t
*pg
,
83 cups_encoding_t encoding
);
84 static ppd_option_t
*ppd_get_option(ppd_group_t
*group
, const char *name
);
85 static _ppd_globals_t
*ppd_globals_alloc(void);
86 #if defined(HAVE_PTHREAD_H) || defined(_WIN32)
87 static void ppd_globals_free(_ppd_globals_t
*g
);
88 #endif /* HAVE_PTHREAD_H || _WIN32 */
90 static void ppd_globals_init(void);
91 #endif /* HAVE_PTHREAD_H */
92 static int ppd_hash_option(ppd_option_t
*option
);
93 static int ppd_read(cups_file_t
*fp
, _ppd_line_t
*line
,
94 char *keyword
, char *option
, char *text
,
95 char **string
, int ignoreblank
,
97 static int ppd_update_filters(ppd_file_t
*ppd
,
102 * 'ppdClose()' - Free all memory used by the PPD file.
106 ppdClose(ppd_file_t
*ppd
) /* I - PPD file record */
108 int i
; /* Looping var */
109 ppd_group_t
*group
; /* Current group */
110 char **font
; /* Current font */
111 ppd_attr_t
**attr
; /* Current attribute */
112 ppd_coption_t
*coption
; /* Current custom option */
113 ppd_cparam_t
*cparam
; /* Current custom parameter */
117 * Range check arguments...
124 * Free all strings at the top level...
127 _cupsStrFree(ppd
->lang_encoding
);
128 _cupsStrFree(ppd
->nickname
);
131 _cupsStrFree(ppd
->jcl_begin
);
132 _cupsStrFree(ppd
->jcl_end
);
133 _cupsStrFree(ppd
->jcl_ps
);
136 * Free any UI groups, subgroups, and options...
139 if (ppd
->num_groups
> 0)
141 for (i
= ppd
->num_groups
, group
= ppd
->groups
; i
> 0; i
--, group
++)
142 ppd_free_group(group
);
144 ppd_free(ppd
->groups
);
147 cupsArrayDelete(ppd
->options
);
148 cupsArrayDelete(ppd
->marked
);
151 * Free any page sizes...
154 if (ppd
->num_sizes
> 0)
155 ppd_free(ppd
->sizes
);
158 * Free any constraints...
161 if (ppd
->num_consts
> 0)
162 ppd_free(ppd
->consts
);
165 * Free any filters...
168 ppd_free_filters(ppd
);
174 if (ppd
->num_fonts
> 0)
176 for (i
= ppd
->num_fonts
, font
= ppd
->fonts
; i
> 0; i
--, font
++)
179 ppd_free(ppd
->fonts
);
183 * Free any profiles...
186 if (ppd
->num_profiles
> 0)
187 ppd_free(ppd
->profiles
);
190 * Free any attributes...
193 if (ppd
->num_attrs
> 0)
195 for (i
= ppd
->num_attrs
, attr
= ppd
->attrs
; i
> 0; i
--, attr
++)
197 _cupsStrFree((*attr
)->value
);
201 ppd_free(ppd
->attrs
);
204 cupsArrayDelete(ppd
->sorted_attrs
);
207 * Free custom options...
210 for (coption
= (ppd_coption_t
*)cupsArrayFirst(ppd
->coptions
);
212 coption
= (ppd_coption_t
*)cupsArrayNext(ppd
->coptions
))
214 for (cparam
= (ppd_cparam_t
*)cupsArrayFirst(coption
->params
);
216 cparam
= (ppd_cparam_t
*)cupsArrayNext(coption
->params
))
218 switch (cparam
->type
)
220 case PPD_CUSTOM_PASSCODE
:
221 case PPD_CUSTOM_PASSWORD
:
222 case PPD_CUSTOM_STRING
:
223 _cupsStrFree(cparam
->current
.custom_string
);
233 cupsArrayDelete(coption
->params
);
238 cupsArrayDelete(ppd
->coptions
);
241 * Free constraints...
244 if (ppd
->cups_uiconstraints
)
246 _ppd_cups_uiconsts_t
*consts
; /* Current constraints */
249 for (consts
= (_ppd_cups_uiconsts_t
*)cupsArrayFirst(ppd
->cups_uiconstraints
);
251 consts
= (_ppd_cups_uiconsts_t
*)cupsArrayNext(ppd
->cups_uiconstraints
))
253 free(consts
->constraints
);
257 cupsArrayDelete(ppd
->cups_uiconstraints
);
261 * Free any PPD cache/mapping data...
265 _ppdCacheDestroy(ppd
->cache
);
268 * Free the whole record...
276 * 'ppdErrorString()' - Returns the text associated with a status.
278 * @since CUPS 1.1.19/macOS 10.3@
281 const char * /* O - Status string */
282 ppdErrorString(ppd_status_t status
) /* I - PPD status */
284 static const char * const messages
[] =/* Status messages */
287 _("Unable to open PPD file"),
288 _("NULL PPD file pointer"),
289 _("Memory allocation error"),
290 _("Missing PPD-Adobe-4.x header"),
291 _("Missing value string"),
294 _("OpenGroup without a CloseGroup first"),
295 _("Bad OpenUI/JCLOpenUI"),
296 _("OpenUI/JCLOpenUI without a CloseUI/JCLCloseUI first"),
297 _("Bad OrderDependency"),
298 _("Bad UIConstraints"),
299 _("Missing asterisk in column 1"),
300 _("Line longer than the maximum allowed (255 characters)"),
301 _("Illegal control character"),
302 _("Illegal main keyword string"),
303 _("Illegal option keyword string"),
304 _("Illegal translation string"),
305 _("Illegal whitespace character"),
306 _("Bad custom parameter"),
307 _("Missing option keyword"),
308 _("Bad value string"),
309 _("Missing CloseGroup"),
310 _("Bad CloseUI/JCLCloseUI"),
311 _("Missing CloseUI/JCLCloseUI")
315 if (status
< PPD_OK
|| status
>= PPD_MAX_STATUS
)
316 return (_cupsLangString(cupsLangDefault(), _("Unknown")));
318 return (_cupsLangString(cupsLangDefault(), messages
[status
]));
323 * '_ppdGetEncoding()' - Get the CUPS encoding value for the given
327 cups_encoding_t
/* O - CUPS encoding value */
328 _ppdGetEncoding(const char *name
) /* I - LanguageEncoding string */
330 if (!_cups_strcasecmp(name
, "ISOLatin1"))
331 return (CUPS_ISO8859_1
);
332 else if (!_cups_strcasecmp(name
, "ISOLatin2"))
333 return (CUPS_ISO8859_2
);
334 else if (!_cups_strcasecmp(name
, "ISOLatin5"))
335 return (CUPS_ISO8859_5
);
336 else if (!_cups_strcasecmp(name
, "JIS83-RKSJ"))
337 return (CUPS_JIS_X0213
);
338 else if (!_cups_strcasecmp(name
, "MacStandard"))
339 return (CUPS_MAC_ROMAN
);
340 else if (!_cups_strcasecmp(name
, "WindowsANSI"))
341 return (CUPS_WINDOWS_1252
);
348 * '_ppdGlobals()' - Return a pointer to thread local storage
351 _ppd_globals_t
* /* O - Pointer to global data */
354 _ppd_globals_t
*pg
; /* Pointer to global data */
357 #ifdef HAVE_PTHREAD_H
359 * Initialize the global data exactly once...
362 pthread_once(&ppd_globals_key_once
, ppd_globals_init
);
363 #endif /* HAVE_PTHREAD_H */
366 * See if we have allocated the data yet...
369 if ((pg
= (_ppd_globals_t
*)_cupsThreadGetData(ppd_globals_key
)) == NULL
)
372 * No, allocate memory as set the pointer for the key...
375 if ((pg
= ppd_globals_alloc()) != NULL
)
376 _cupsThreadSetData(ppd_globals_key
, pg
);
380 * Return the pointer to the data...
388 * 'ppdLastError()' - Return the status from the last ppdOpen*().
390 * @since CUPS 1.1.19/macOS 10.3@
393 ppd_status_t
/* O - Status code */
394 ppdLastError(int *line
) /* O - Line number */
396 _ppd_globals_t
*pg
= _ppdGlobals();
401 *line
= pg
->ppd_line
;
403 return (pg
->ppd_status
);
408 * '_ppdOpen()' - Read a PPD file into memory.
410 * @since CUPS 1.2/macOS 10.5@
413 ppd_file_t
* /* O - PPD file record or @code NULL@ if the PPD file could not be opened. */
415 cups_file_t
*fp
, /* I - File to read from */
416 _ppd_localization_t localization
) /* I - Localization to load */
418 int i
, j
, k
; /* Looping vars */
419 _ppd_line_t line
; /* Line buffer */
420 ppd_file_t
*ppd
; /* PPD file record */
421 ppd_group_t
*group
, /* Current group */
422 *subgroup
; /* Current sub-group */
423 ppd_option_t
*option
; /* Current option */
424 ppd_choice_t
*choice
; /* Current choice */
425 ppd_const_t
*constraint
; /* Current constraint */
426 ppd_size_t
*size
; /* Current page size */
427 int mask
; /* Line data mask */
428 char keyword
[PPD_MAX_NAME
],
429 /* Keyword from file */
431 /* Option from file */
433 /* Human-readable text from file */
434 *string
, /* Code/text from file */
435 *sptr
, /* Pointer into string */
436 *temp
, /* Temporary string pointer */
437 **tempfonts
; /* Temporary fonts pointer */
438 float order
; /* Order dependency number */
439 ppd_section_t section
; /* Order dependency section */
440 ppd_profile_t
*profile
; /* Pointer to color profile */
441 char **filter
; /* Pointer to filter */
442 struct lconv
*loc
; /* Locale data */
443 int ui_keyword
; /* Is this line a UI keyword? */
444 cups_lang_t
*lang
; /* Language data */
445 cups_encoding_t encoding
; /* Encoding of PPD file */
446 _ppd_globals_t
*pg
= _ppdGlobals();
448 char custom_name
[PPD_MAX_NAME
];
449 /* CustomFoo attribute name */
450 ppd_attr_t
*custom_attr
; /* CustomFoo attribute */
451 char ll
[7], /* Base language + '.' */
452 ll_CC
[7]; /* Language w/country + '.' */
453 size_t ll_len
= 0, /* Base language length */
454 ll_CC_len
= 0; /* Language w/country length */
455 static const char * const ui_keywords
[] =
457 #ifdef CUPS_USE_FULL_UI_KEYWORDS_LIST
459 * Adobe defines some 41 keywords as "UI", meaning that they are
460 * user interface elements and that they should be treated as such
461 * even if the PPD creator doesn't use Open/CloseUI around them.
463 * Since this can cause previously invisible options to appear and
464 * confuse users, the default is to only treat the PageSize and
465 * PageRegion keywords this way.
467 /* Boolean keywords */
477 /* PickOne keywords */
490 "JCLFrameBufferSize",
511 #else /* !CUPS_USE_FULL_UI_KEYWORDS_LIST */
514 #endif /* CUPS_USE_FULL_UI_KEYWORDS_LIST */
516 static const char * const color_keywords
[] = /* Keywords associated with color profiles */
523 DEBUG_printf(("_ppdOpen(fp=%p)", fp
));
526 * Default to "OK" status...
529 pg
->ppd_status
= PPD_OK
;
533 * Range check input...
538 pg
->ppd_status
= PPD_NULL_FILE
;
543 * If only loading a single localization set up the strings to match...
546 if (localization
== _PPD_LOCALIZATION_DEFAULT
)
548 if ((lang
= cupsLangDefault()) == NULL
)
551 snprintf(ll_CC
, sizeof(ll_CC
), "%s.", lang
->language
);
554 * <rdar://problem/22130168>
555 * <rdar://problem/27245567>
557 * Need to use a different base language for some locales...
560 if (!strcmp(lang
->language
, "zh_HK"))
561 { /* Traditional Chinese + variants */
562 strlcpy(ll_CC
, "zh_TW.", sizeof(ll_CC
));
563 strlcpy(ll
, "zh_", sizeof(ll
));
565 else if (!strncmp(lang
->language
, "zh", 2))
566 strlcpy(ll
, "zh_", sizeof(ll
)); /* Any Chinese variant */
567 else if (!strncmp(lang
->language
, "jp", 2))
568 { /* Any Japanese variant */
569 strlcpy(ll_CC
, "ja", sizeof(ll_CC
));
570 strlcpy(ll
, "jp", sizeof(ll
));
572 else if (!strncmp(lang
->language
, "nb", 2) || !strncmp(lang
->language
, "no", 2))
573 { /* Any Norwegian variant */
574 strlcpy(ll_CC
, "nb", sizeof(ll_CC
));
575 strlcpy(ll
, "no", sizeof(ll
));
578 snprintf(ll
, sizeof(ll
), "%2.2s.", lang
->language
);
580 ll_CC_len
= strlen(ll_CC
);
583 DEBUG_printf(("2_ppdOpen: Loading localizations matching \"%s\" and \"%s\"",
588 * Grab the first line and make sure it reads '*PPD-Adobe: "major.minor"'...
594 mask
= ppd_read(fp
, &line
, keyword
, name
, text
, &string
, 0, pg
);
596 DEBUG_printf(("2_ppdOpen: mask=%x, keyword=\"%s\"...", mask
, keyword
));
599 strcmp(keyword
, "PPD-Adobe") ||
600 string
== NULL
|| string
[0] != '4')
603 * Either this is not a PPD file, or it is not a 4.x PPD file.
606 if (pg
->ppd_status
== PPD_OK
)
607 pg
->ppd_status
= PPD_MISSING_PPDADOBE4
;
609 _cupsStrFree(string
);
610 ppd_free(line
.buffer
);
615 DEBUG_printf(("2_ppdOpen: keyword=%s, string=%p", keyword
, string
));
617 _cupsStrFree(string
);
620 * Allocate memory for the PPD file record...
623 if ((ppd
= calloc(1, sizeof(ppd_file_t
))) == NULL
)
625 pg
->ppd_status
= PPD_ALLOC_ERROR
;
627 _cupsStrFree(string
);
628 ppd_free(line
.buffer
);
633 ppd
->language_level
= 2;
634 ppd
->color_device
= 0;
635 ppd
->colorspace
= PPD_CS_N
;
636 ppd
->landscape
= -90;
637 ppd
->coptions
= cupsArrayNew((cups_array_func_t
)ppd_compare_coptions
,
641 * Read lines from the PPD file and add them to the file record...
649 encoding
= CUPS_ISO8859_1
;
652 while ((mask
= ppd_read(fp
, &line
, keyword
, name
, text
, &string
, 1, pg
)) != 0)
654 DEBUG_printf(("2_ppdOpen: mask=%x, keyword=\"%s\", name=\"%s\", "
655 "text=\"%s\", string=%d chars...", mask
, keyword
, name
, text
,
656 string
? (int)strlen(string
) : 0));
658 if (strncmp(keyword
, "Default", 7) && !string
&&
659 pg
->ppd_conform
!= PPD_CONFORM_RELAXED
)
662 * Need a string value!
665 pg
->ppd_status
= PPD_MISSING_VALUE
;
673 * Certain main keywords (as defined by the PPD spec) may be used
674 * without the usual OpenUI/CloseUI stuff. Presumably this is just
675 * so that Adobe wouldn't completely break compatibility with PPD
676 * files prior to v4.0 of the spec, but it is hopelessly
677 * inconsistent... Catch these main keywords and automatically
678 * create the corresponding option, as needed...
684 * Previous line was a UI keyword...
692 * If we are filtering out keyword localizations, see if this line needs to
696 if (localization
!= _PPD_LOCALIZATION_ALL
&&
697 (temp
= strchr(keyword
, '.')) != NULL
&&
698 ((temp
- keyword
) == 2 || (temp
- keyword
) == 5) &&
699 _cups_isalpha(keyword
[0]) &&
700 _cups_isalpha(keyword
[1]) &&
701 (keyword
[2] == '.' ||
702 (keyword
[2] == '_' && _cups_isalpha(keyword
[3]) &&
703 _cups_isalpha(keyword
[4]) && keyword
[5] == '.')))
705 if (localization
== _PPD_LOCALIZATION_NONE
||
706 (localization
== _PPD_LOCALIZATION_DEFAULT
&&
707 strncmp(ll_CC
, keyword
, ll_CC_len
) &&
708 strncmp(ll
, keyword
, ll_len
)))
710 DEBUG_printf(("2_ppdOpen: Ignoring localization: \"%s\"\n", keyword
));
713 else if (localization
== _PPD_LOCALIZATION_ICC_PROFILES
)
716 * Only load localizations for the color profile related keywords...
720 i
< (int)(sizeof(color_keywords
) / sizeof(color_keywords
[0]));
723 if (!_cups_strcasecmp(temp
, color_keywords
[i
]))
727 if (i
>= (int)(sizeof(color_keywords
) / sizeof(color_keywords
[0])))
729 DEBUG_printf(("2_ppdOpen: Ignoring localization: \"%s\"\n", keyword
));
735 if (option
== NULL
&&
736 (mask
& (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
)) ==
737 (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
))
739 for (i
= 0; i
< (int)(sizeof(ui_keywords
) / sizeof(ui_keywords
[0])); i
++)
740 if (!strcmp(keyword
, ui_keywords
[i
]))
743 if (i
< (int)(sizeof(ui_keywords
) / sizeof(ui_keywords
[0])))
746 * Create the option in the appropriate group...
751 DEBUG_printf(("2_ppdOpen: FOUND ADOBE UI KEYWORD %s WITHOUT OPENUI!",
756 if ((group
= ppd_get_group(ppd
, "General", _("General"), pg
,
760 DEBUG_printf(("2_ppdOpen: Adding to group %s...", group
->text
));
761 option
= ppd_get_option(group
, keyword
);
765 option
= ppd_get_option(group
, keyword
);
769 pg
->ppd_status
= PPD_ALLOC_ERROR
;
775 * Now fill in the initial information for the option...
778 if (!strncmp(keyword
, "JCL", 3))
779 option
->section
= PPD_ORDER_JCL
;
781 option
->section
= PPD_ORDER_ANY
;
783 option
->order
= 10.0f
;
786 option
->ui
= PPD_UI_BOOLEAN
;
788 option
->ui
= PPD_UI_PICKONE
;
790 for (j
= 0; j
< ppd
->num_attrs
; j
++)
791 if (!strncmp(ppd
->attrs
[j
]->name
, "Default", 7) &&
792 !strcmp(ppd
->attrs
[j
]->name
+ 7, keyword
) &&
793 ppd
->attrs
[j
]->value
)
795 DEBUG_printf(("2_ppdOpen: Setting Default%s to %s via attribute...",
796 option
->keyword
, ppd
->attrs
[j
]->value
));
797 strlcpy(option
->defchoice
, ppd
->attrs
[j
]->value
,
798 sizeof(option
->defchoice
));
802 if (!strcmp(keyword
, "PageSize"))
803 strlcpy(option
->text
, _("Media Size"), sizeof(option
->text
));
804 else if (!strcmp(keyword
, "MediaType"))
805 strlcpy(option
->text
, _("Media Type"), sizeof(option
->text
));
806 else if (!strcmp(keyword
, "InputSlot"))
807 strlcpy(option
->text
, _("Media Source"), sizeof(option
->text
));
808 else if (!strcmp(keyword
, "ColorModel"))
809 strlcpy(option
->text
, _("Output Mode"), sizeof(option
->text
));
810 else if (!strcmp(keyword
, "Resolution"))
811 strlcpy(option
->text
, _("Resolution"), sizeof(option
->text
));
813 strlcpy(option
->text
, keyword
, sizeof(option
->text
));
817 if (!strcmp(keyword
, "LanguageLevel"))
818 ppd
->language_level
= atoi(string
);
819 else if (!strcmp(keyword
, "LanguageEncoding"))
822 * Say all PPD files are UTF-8, since we convert to UTF-8...
825 ppd
->lang_encoding
= _cupsStrAlloc("UTF-8");
826 encoding
= _ppdGetEncoding(string
);
828 else if (!strcmp(keyword
, "LanguageVersion"))
829 ppd
->lang_version
= string
;
830 else if (!strcmp(keyword
, "Manufacturer"))
831 ppd
->manufacturer
= string
;
832 else if (!strcmp(keyword
, "ModelName"))
833 ppd
->modelname
= string
;
834 else if (!strcmp(keyword
, "Protocols"))
835 ppd
->protocols
= string
;
836 else if (!strcmp(keyword
, "PCFileName"))
837 ppd
->pcfilename
= string
;
838 else if (!strcmp(keyword
, "NickName"))
840 if (encoding
!= CUPS_UTF8
)
842 cups_utf8_t utf8
[256]; /* UTF-8 version of NickName */
845 cupsCharsetToUTF8(utf8
, string
, sizeof(utf8
), encoding
);
846 ppd
->nickname
= _cupsStrAlloc((char *)utf8
);
849 ppd
->nickname
= _cupsStrAlloc(string
);
851 else if (!strcmp(keyword
, "Product"))
852 ppd
->product
= string
;
853 else if (!strcmp(keyword
, "ShortNickName"))
854 ppd
->shortnickname
= string
;
855 else if (!strcmp(keyword
, "TTRasterizer"))
856 ppd
->ttrasterizer
= string
;
857 else if (!strcmp(keyword
, "JCLBegin"))
859 ppd
->jcl_begin
= _cupsStrAlloc(string
);
860 ppd_decode(ppd
->jcl_begin
); /* Decode quoted string */
862 else if (!strcmp(keyword
, "JCLEnd"))
864 ppd
->jcl_end
= _cupsStrAlloc(string
);
865 ppd_decode(ppd
->jcl_end
); /* Decode quoted string */
867 else if (!strcmp(keyword
, "JCLToPSInterpreter"))
869 ppd
->jcl_ps
= _cupsStrAlloc(string
);
870 ppd_decode(ppd
->jcl_ps
); /* Decode quoted string */
872 else if (!strcmp(keyword
, "AccurateScreensSupport"))
873 ppd
->accurate_screens
= !strcmp(string
, "True");
874 else if (!strcmp(keyword
, "ColorDevice"))
875 ppd
->color_device
= !strcmp(string
, "True");
876 else if (!strcmp(keyword
, "ContoneOnly"))
877 ppd
->contone_only
= !strcmp(string
, "True");
878 else if (!strcmp(keyword
, "cupsFlipDuplex"))
879 ppd
->flip_duplex
= !strcmp(string
, "True");
880 else if (!strcmp(keyword
, "cupsManualCopies"))
881 ppd
->manual_copies
= !strcmp(string
, "True");
882 else if (!strcmp(keyword
, "cupsModelNumber"))
883 ppd
->model_number
= atoi(string
);
884 else if (!strcmp(keyword
, "cupsColorProfile"))
886 if (ppd
->num_profiles
== 0)
887 profile
= malloc(sizeof(ppd_profile_t
));
889 profile
= realloc(ppd
->profiles
, sizeof(ppd_profile_t
) * (size_t)(ppd
->num_profiles
+ 1));
893 pg
->ppd_status
= PPD_ALLOC_ERROR
;
898 ppd
->profiles
= profile
;
899 profile
+= ppd
->num_profiles
;
900 ppd
->num_profiles
++;
902 memset(profile
, 0, sizeof(ppd_profile_t
));
903 strlcpy(profile
->resolution
, name
, sizeof(profile
->resolution
));
904 strlcpy(profile
->media_type
, text
, sizeof(profile
->media_type
));
906 profile
->density
= (float)_cupsStrScand(string
, &sptr
, loc
);
907 profile
->gamma
= (float)_cupsStrScand(sptr
, &sptr
, loc
);
908 profile
->matrix
[0][0] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
909 profile
->matrix
[0][1] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
910 profile
->matrix
[0][2] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
911 profile
->matrix
[1][0] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
912 profile
->matrix
[1][1] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
913 profile
->matrix
[1][2] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
914 profile
->matrix
[2][0] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
915 profile
->matrix
[2][1] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
916 profile
->matrix
[2][2] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
918 else if (!strcmp(keyword
, "cupsFilter"))
920 if (ppd
->num_filters
== 0)
921 filter
= malloc(sizeof(char *));
923 filter
= realloc(ppd
->filters
, sizeof(char *) * (size_t)(ppd
->num_filters
+ 1));
927 pg
->ppd_status
= PPD_ALLOC_ERROR
;
932 ppd
->filters
= filter
;
933 filter
+= ppd
->num_filters
;
937 * Retain a copy of the filter string...
940 *filter
= _cupsStrRetain(string
);
942 else if (!strcmp(keyword
, "Throughput"))
943 ppd
->throughput
= atoi(string
);
944 else if (!strcmp(keyword
, "Font"))
947 * Add this font to the list of available fonts...
950 if (ppd
->num_fonts
== 0)
951 tempfonts
= (char **)malloc(sizeof(char *));
953 tempfonts
= (char **)realloc(ppd
->fonts
, sizeof(char *) * (size_t)(ppd
->num_fonts
+ 1));
955 if (tempfonts
== NULL
)
957 pg
->ppd_status
= PPD_ALLOC_ERROR
;
962 ppd
->fonts
= tempfonts
;
963 ppd
->fonts
[ppd
->num_fonts
] = _cupsStrAlloc(name
);
966 else if (!strncmp(keyword
, "ParamCustom", 11))
968 ppd_coption_t
*coption
; /* Custom option */
969 ppd_cparam_t
*cparam
; /* Custom parameter */
970 int corder
; /* Order number */
971 char ctype
[33], /* Data type */
972 cminimum
[65], /* Minimum value */
973 cmaximum
[65]; /* Maximum value */
977 * Get the custom option and parameter...
980 if ((coption
= ppd_get_coption(ppd
, keyword
+ 11)) == NULL
)
982 pg
->ppd_status
= PPD_ALLOC_ERROR
;
987 if ((cparam
= ppd_get_cparam(coption
, name
, text
)) == NULL
)
989 pg
->ppd_status
= PPD_ALLOC_ERROR
;
995 * Get the parameter data...
999 sscanf(string
, "%d%32s%64s%64s", &corder
, ctype
, cminimum
,
1002 pg
->ppd_status
= PPD_BAD_CUSTOM_PARAM
;
1007 cparam
->order
= corder
;
1009 if (!strcmp(ctype
, "curve"))
1011 cparam
->type
= PPD_CUSTOM_CURVE
;
1012 cparam
->minimum
.custom_curve
= (float)_cupsStrScand(cminimum
, NULL
, loc
);
1013 cparam
->maximum
.custom_curve
= (float)_cupsStrScand(cmaximum
, NULL
, loc
);
1015 else if (!strcmp(ctype
, "int"))
1017 cparam
->type
= PPD_CUSTOM_INT
;
1018 cparam
->minimum
.custom_int
= atoi(cminimum
);
1019 cparam
->maximum
.custom_int
= atoi(cmaximum
);
1021 else if (!strcmp(ctype
, "invcurve"))
1023 cparam
->type
= PPD_CUSTOM_INVCURVE
;
1024 cparam
->minimum
.custom_invcurve
= (float)_cupsStrScand(cminimum
, NULL
, loc
);
1025 cparam
->maximum
.custom_invcurve
= (float)_cupsStrScand(cmaximum
, NULL
, loc
);
1027 else if (!strcmp(ctype
, "passcode"))
1029 cparam
->type
= PPD_CUSTOM_PASSCODE
;
1030 cparam
->minimum
.custom_passcode
= atoi(cminimum
);
1031 cparam
->maximum
.custom_passcode
= atoi(cmaximum
);
1033 else if (!strcmp(ctype
, "password"))
1035 cparam
->type
= PPD_CUSTOM_PASSWORD
;
1036 cparam
->minimum
.custom_password
= atoi(cminimum
);
1037 cparam
->maximum
.custom_password
= atoi(cmaximum
);
1039 else if (!strcmp(ctype
, "points"))
1041 cparam
->type
= PPD_CUSTOM_POINTS
;
1042 cparam
->minimum
.custom_points
= (float)_cupsStrScand(cminimum
, NULL
, loc
);
1043 cparam
->maximum
.custom_points
= (float)_cupsStrScand(cmaximum
, NULL
, loc
);
1045 else if (!strcmp(ctype
, "real"))
1047 cparam
->type
= PPD_CUSTOM_REAL
;
1048 cparam
->minimum
.custom_real
= (float)_cupsStrScand(cminimum
, NULL
, loc
);
1049 cparam
->maximum
.custom_real
= (float)_cupsStrScand(cmaximum
, NULL
, loc
);
1051 else if (!strcmp(ctype
, "string"))
1053 cparam
->type
= PPD_CUSTOM_STRING
;
1054 cparam
->minimum
.custom_string
= atoi(cminimum
);
1055 cparam
->maximum
.custom_string
= atoi(cmaximum
);
1059 pg
->ppd_status
= PPD_BAD_CUSTOM_PARAM
;
1065 * Now special-case for CustomPageSize...
1068 if (!strcmp(coption
->keyword
, "PageSize"))
1070 if (!strcmp(name
, "Width"))
1072 ppd
->custom_min
[0] = cparam
->minimum
.custom_points
;
1073 ppd
->custom_max
[0] = cparam
->maximum
.custom_points
;
1075 else if (!strcmp(name
, "Height"))
1077 ppd
->custom_min
[1] = cparam
->minimum
.custom_points
;
1078 ppd
->custom_max
[1] = cparam
->maximum
.custom_points
;
1082 else if (!strcmp(keyword
, "HWMargins"))
1084 for (i
= 0, sptr
= string
; i
< 4; i
++)
1085 ppd
->custom_margins
[i
] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
1087 else if (!strncmp(keyword
, "Custom", 6) && !strcmp(name
, "True") && !option
)
1089 ppd_option_t
*custom_option
; /* Custom option */
1091 DEBUG_puts("2_ppdOpen: Processing Custom option...");
1094 * Get the option and custom option...
1097 if (!ppd_get_coption(ppd
, keyword
+ 6))
1099 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1104 if (option
&& !_cups_strcasecmp(option
->keyword
, keyword
+ 6))
1105 custom_option
= option
;
1107 custom_option
= ppdFindOption(ppd
, keyword
+ 6);
1112 * Add the "custom" option...
1115 if ((choice
= ppdFindChoice(custom_option
, "Custom")) == NULL
)
1116 if ((choice
= ppd_add_choice(custom_option
, "Custom")) == NULL
)
1118 DEBUG_puts("1_ppdOpen: Unable to add Custom choice!");
1120 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1125 strlcpy(choice
->text
, text
[0] ? text
: _("Custom"),
1126 sizeof(choice
->text
));
1128 choice
->code
= _cupsStrAlloc(string
);
1130 if (custom_option
->section
== PPD_ORDER_JCL
)
1131 ppd_decode(choice
->code
);
1135 * Now process custom page sizes specially...
1138 if (!strcmp(keyword
, "CustomPageSize"))
1141 * Add a "Custom" page size entry...
1144 ppd
->variable_sizes
= 1;
1146 ppd_add_size(ppd
, "Custom");
1148 if (option
&& !_cups_strcasecmp(option
->keyword
, "PageRegion"))
1149 custom_option
= option
;
1151 custom_option
= ppdFindOption(ppd
, "PageRegion");
1155 if ((choice
= ppdFindChoice(custom_option
, "Custom")) == NULL
)
1156 if ((choice
= ppd_add_choice(custom_option
, "Custom")) == NULL
)
1158 DEBUG_puts("1_ppdOpen: Unable to add Custom choice!");
1160 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1165 strlcpy(choice
->text
, text
[0] ? text
: _("Custom"),
1166 sizeof(choice
->text
));
1170 else if (!strcmp(keyword
, "LandscapeOrientation"))
1172 if (!strcmp(string
, "Minus90"))
1173 ppd
->landscape
= -90;
1174 else if (!strcmp(string
, "Plus90"))
1175 ppd
->landscape
= 90;
1177 else if (!strcmp(keyword
, "JobPatchFile"))
1180 * CUPS STR #3421: Check for "*JobPatchFile: int: string"
1183 if (isdigit(*string
& 255))
1185 for (sptr
= string
+ 1; isdigit(*sptr
& 255); sptr
++);
1190 * Found "*JobPatchFile: int: string"...
1193 pg
->ppd_status
= PPD_BAD_VALUE
;
1199 if (!name
[0] && pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1202 * Found "*JobPatchFile: string"...
1205 pg
->ppd_status
= PPD_MISSING_OPTION_KEYWORD
;
1210 if (ppd
->patches
== NULL
)
1211 ppd
->patches
= strdup(string
);
1214 temp
= realloc(ppd
->patches
, strlen(ppd
->patches
) +
1215 strlen(string
) + 1);
1218 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1223 ppd
->patches
= temp
;
1225 memcpy(ppd
->patches
+ strlen(ppd
->patches
), string
, strlen(string
) + 1);
1228 else if (!strcmp(keyword
, "OpenUI"))
1231 * Don't allow nesting of options...
1234 if (option
&& pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1236 pg
->ppd_status
= PPD_NESTED_OPEN_UI
;
1242 * Add an option record to the current sub-group, group, or file...
1245 DEBUG_printf(("2_ppdOpen: name=\"%s\" (%d)", name
, (int)strlen(name
)));
1248 _cups_strcpy(name
, name
+ 1); /* Eliminate leading asterisk */
1250 for (i
= (int)strlen(name
) - 1; i
> 0 && _cups_isspace(name
[i
]); i
--)
1251 name
[i
] = '\0'; /* Eliminate trailing spaces */
1253 DEBUG_printf(("2_ppdOpen: OpenUI of %s in group %s...", name
,
1254 group
? group
->text
: "(null)"));
1256 if (subgroup
!= NULL
)
1257 option
= ppd_get_option(subgroup
, name
);
1258 else if (group
== NULL
)
1260 if ((group
= ppd_get_group(ppd
, "General", _("General"), pg
,
1264 DEBUG_printf(("2_ppdOpen: Adding to group %s...", group
->text
));
1265 option
= ppd_get_option(group
, name
);
1269 option
= ppd_get_option(group
, name
);
1273 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1279 * Now fill in the initial information for the option...
1282 if (string
&& !strcmp(string
, "PickMany"))
1283 option
->ui
= PPD_UI_PICKMANY
;
1284 else if (string
&& !strcmp(string
, "Boolean"))
1285 option
->ui
= PPD_UI_BOOLEAN
;
1286 else if (string
&& !strcmp(string
, "PickOne"))
1287 option
->ui
= PPD_UI_PICKONE
;
1288 else if (pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1290 pg
->ppd_status
= PPD_BAD_OPEN_UI
;
1295 option
->ui
= PPD_UI_PICKONE
;
1297 for (j
= 0; j
< ppd
->num_attrs
; j
++)
1298 if (!strncmp(ppd
->attrs
[j
]->name
, "Default", 7) &&
1299 !strcmp(ppd
->attrs
[j
]->name
+ 7, name
) &&
1300 ppd
->attrs
[j
]->value
)
1302 DEBUG_printf(("2_ppdOpen: Setting Default%s to %s via attribute...",
1303 option
->keyword
, ppd
->attrs
[j
]->value
));
1304 strlcpy(option
->defchoice
, ppd
->attrs
[j
]->value
,
1305 sizeof(option
->defchoice
));
1310 cupsCharsetToUTF8((cups_utf8_t
*)option
->text
, text
,
1311 sizeof(option
->text
), encoding
);
1314 if (!strcmp(name
, "PageSize"))
1315 strlcpy(option
->text
, _("Media Size"), sizeof(option
->text
));
1316 else if (!strcmp(name
, "MediaType"))
1317 strlcpy(option
->text
, _("Media Type"), sizeof(option
->text
));
1318 else if (!strcmp(name
, "InputSlot"))
1319 strlcpy(option
->text
, _("Media Source"), sizeof(option
->text
));
1320 else if (!strcmp(name
, "ColorModel"))
1321 strlcpy(option
->text
, _("Output Mode"), sizeof(option
->text
));
1322 else if (!strcmp(name
, "Resolution"))
1323 strlcpy(option
->text
, _("Resolution"), sizeof(option
->text
));
1325 strlcpy(option
->text
, name
, sizeof(option
->text
));
1328 option
->section
= PPD_ORDER_ANY
;
1330 _cupsStrFree(string
);
1334 * Add a custom option choice if we have already seen a CustomFoo
1338 if (!_cups_strcasecmp(name
, "PageRegion"))
1339 strlcpy(custom_name
, "CustomPageSize", sizeof(custom_name
));
1341 snprintf(custom_name
, sizeof(custom_name
), "Custom%s", name
);
1343 if ((custom_attr
= ppdFindAttr(ppd
, custom_name
, "True")) != NULL
)
1345 if ((choice
= ppdFindChoice(option
, "Custom")) == NULL
)
1346 if ((choice
= ppd_add_choice(option
, "Custom")) == NULL
)
1348 DEBUG_puts("1_ppdOpen: Unable to add Custom choice!");
1350 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1355 strlcpy(choice
->text
,
1356 custom_attr
->text
[0] ? custom_attr
->text
: _("Custom"),
1357 sizeof(choice
->text
));
1358 choice
->code
= _cupsStrRetain(custom_attr
->value
);
1361 else if (!strcmp(keyword
, "JCLOpenUI"))
1364 * Don't allow nesting of options...
1367 if (option
&& pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1369 pg
->ppd_status
= PPD_NESTED_OPEN_UI
;
1375 * Find the JCL group, and add if needed...
1378 group
= ppd_get_group(ppd
, "JCL", _("JCL"), pg
, encoding
);
1384 * Add an option record to the current JCLs...
1388 _cups_strcpy(name
, name
+ 1);
1390 option
= ppd_get_option(group
, name
);
1394 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1400 * Now fill in the initial information for the option...
1403 if (string
&& !strcmp(string
, "PickMany"))
1404 option
->ui
= PPD_UI_PICKMANY
;
1405 else if (string
&& !strcmp(string
, "Boolean"))
1406 option
->ui
= PPD_UI_BOOLEAN
;
1407 else if (string
&& !strcmp(string
, "PickOne"))
1408 option
->ui
= PPD_UI_PICKONE
;
1411 pg
->ppd_status
= PPD_BAD_OPEN_UI
;
1416 for (j
= 0; j
< ppd
->num_attrs
; j
++)
1417 if (!strncmp(ppd
->attrs
[j
]->name
, "Default", 7) &&
1418 !strcmp(ppd
->attrs
[j
]->name
+ 7, name
) &&
1419 ppd
->attrs
[j
]->value
)
1421 DEBUG_printf(("2_ppdOpen: Setting Default%s to %s via attribute...",
1422 option
->keyword
, ppd
->attrs
[j
]->value
));
1423 strlcpy(option
->defchoice
, ppd
->attrs
[j
]->value
,
1424 sizeof(option
->defchoice
));
1429 cupsCharsetToUTF8((cups_utf8_t
*)option
->text
, text
,
1430 sizeof(option
->text
), encoding
);
1432 strlcpy(option
->text
, name
, sizeof(option
->text
));
1434 option
->section
= PPD_ORDER_JCL
;
1437 _cupsStrFree(string
);
1441 * Add a custom option choice if we have already seen a CustomFoo
1445 snprintf(custom_name
, sizeof(custom_name
), "Custom%s", name
);
1447 if ((custom_attr
= ppdFindAttr(ppd
, custom_name
, "True")) != NULL
)
1449 if ((choice
= ppd_add_choice(option
, "Custom")) == NULL
)
1451 DEBUG_puts("1_ppdOpen: Unable to add Custom choice!");
1453 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1458 strlcpy(choice
->text
,
1459 custom_attr
->text
[0] ? custom_attr
->text
: _("Custom"),
1460 sizeof(choice
->text
));
1461 choice
->code
= _cupsStrRetain(custom_attr
->value
);
1464 else if (!strcmp(keyword
, "CloseUI"))
1466 if ((!option
|| option
->section
== PPD_ORDER_JCL
) && pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1468 pg
->ppd_status
= PPD_BAD_CLOSE_UI
;
1475 _cupsStrFree(string
);
1478 else if (!strcmp(keyword
, "JCLCloseUI"))
1480 if ((!option
|| option
->section
!= PPD_ORDER_JCL
) && pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1482 pg
->ppd_status
= PPD_BAD_CLOSE_UI
;
1489 _cupsStrFree(string
);
1492 else if (!strcmp(keyword
, "OpenGroup"))
1495 * Open a new group...
1500 pg
->ppd_status
= PPD_NESTED_OPEN_GROUP
;
1507 pg
->ppd_status
= PPD_BAD_OPEN_GROUP
;
1513 * Separate the group name from the text (name/text)...
1516 if ((sptr
= strchr(string
, '/')) != NULL
)
1522 * Fix up the text...
1528 * Find/add the group...
1531 group
= ppd_get_group(ppd
, string
, sptr
, pg
, encoding
);
1536 _cupsStrFree(string
);
1539 else if (!strcmp(keyword
, "CloseGroup"))
1543 _cupsStrFree(string
);
1546 else if (!strcmp(keyword
, "OrderDependency"))
1548 order
= (float)_cupsStrScand(string
, &sptr
, loc
);
1550 if (!sptr
|| sscanf(sptr
, "%40s%40s", name
, keyword
) != 2)
1552 pg
->ppd_status
= PPD_BAD_ORDER_DEPENDENCY
;
1557 if (keyword
[0] == '*')
1558 _cups_strcpy(keyword
, keyword
+ 1);
1560 if (!strcmp(name
, "ExitServer"))
1561 section
= PPD_ORDER_EXIT
;
1562 else if (!strcmp(name
, "Prolog"))
1563 section
= PPD_ORDER_PROLOG
;
1564 else if (!strcmp(name
, "DocumentSetup"))
1565 section
= PPD_ORDER_DOCUMENT
;
1566 else if (!strcmp(name
, "PageSetup"))
1567 section
= PPD_ORDER_PAGE
;
1568 else if (!strcmp(name
, "JCLSetup"))
1569 section
= PPD_ORDER_JCL
;
1571 section
= PPD_ORDER_ANY
;
1579 * Only valid for Non-UI options...
1582 for (i
= ppd
->num_groups
, gtemp
= ppd
->groups
; i
> 0; i
--, gtemp
++)
1583 if (gtemp
->text
[0] == '\0')
1587 for (i
= 0; i
< gtemp
->num_options
; i
++)
1588 if (!strcmp(keyword
, gtemp
->options
[i
].keyword
))
1590 gtemp
->options
[i
].section
= section
;
1591 gtemp
->options
[i
].order
= order
;
1597 option
->section
= section
;
1598 option
->order
= order
;
1601 _cupsStrFree(string
);
1604 else if (!strncmp(keyword
, "Default", 7))
1610 * Drop UI text, if any, from value...
1613 if (strchr(string
, '/') != NULL
)
1614 *strchr(string
, '/') = '\0';
1617 * Assign the default value as appropriate...
1620 if (!strcmp(keyword
, "DefaultColorSpace"))
1623 * Set default colorspace...
1626 if (!strcmp(string
, "CMY"))
1627 ppd
->colorspace
= PPD_CS_CMY
;
1628 else if (!strcmp(string
, "CMYK"))
1629 ppd
->colorspace
= PPD_CS_CMYK
;
1630 else if (!strcmp(string
, "RGB"))
1631 ppd
->colorspace
= PPD_CS_RGB
;
1632 else if (!strcmp(string
, "RGBK"))
1633 ppd
->colorspace
= PPD_CS_RGBK
;
1634 else if (!strcmp(string
, "N"))
1635 ppd
->colorspace
= PPD_CS_N
;
1637 ppd
->colorspace
= PPD_CS_GRAY
;
1639 else if (option
&& !strcmp(keyword
+ 7, option
->keyword
))
1642 * Set the default as part of the current option...
1645 DEBUG_printf(("2_ppdOpen: Setting %s to %s...", keyword
, string
));
1647 strlcpy(option
->defchoice
, string
, sizeof(option
->defchoice
));
1649 DEBUG_printf(("2_ppdOpen: %s is now %s...", keyword
, option
->defchoice
));
1654 * Lookup option and set if it has been defined...
1657 ppd_option_t
*toption
; /* Temporary option */
1660 if ((toption
= ppdFindOption(ppd
, keyword
+ 7)) != NULL
)
1662 DEBUG_printf(("2_ppdOpen: Setting %s to %s...", keyword
, string
));
1663 strlcpy(toption
->defchoice
, string
, sizeof(toption
->defchoice
));
1667 else if (!strcmp(keyword
, "UIConstraints") ||
1668 !strcmp(keyword
, "NonUIConstraints"))
1672 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1676 if (ppd
->num_consts
== 0)
1677 constraint
= calloc(2, sizeof(ppd_const_t
));
1679 constraint
= realloc(ppd
->consts
, (size_t)(ppd
->num_consts
+ 2) * sizeof(ppd_const_t
));
1681 if (constraint
== NULL
)
1683 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1688 ppd
->consts
= constraint
;
1689 constraint
+= ppd
->num_consts
;
1692 switch (sscanf(string
, "%40s%40s%40s%40s", constraint
->option1
,
1693 constraint
->choice1
, constraint
->option2
,
1694 constraint
->choice2
))
1696 case 0 : /* Error */
1697 case 1 : /* Error */
1698 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1701 case 2 : /* Two options... */
1703 * Check for broken constraints like "* Option"...
1706 if (pg
->ppd_conform
== PPD_CONFORM_STRICT
&&
1707 (!strcmp(constraint
->option1
, "*") ||
1708 !strcmp(constraint
->choice1
, "*")))
1710 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1715 * The following strcpy's are safe, as optionN and
1716 * choiceN are all the same size (size defined by PPD spec...)
1719 if (constraint
->option1
[0] == '*')
1720 _cups_strcpy(constraint
->option1
, constraint
->option1
+ 1);
1721 else if (pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1723 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1727 if (constraint
->choice1
[0] == '*')
1728 _cups_strcpy(constraint
->option2
, constraint
->choice1
+ 1);
1729 else if (pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1731 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1735 constraint
->choice1
[0] = '\0';
1736 constraint
->choice2
[0] = '\0';
1739 case 3 : /* Two options, one choice... */
1741 * Check for broken constraints like "* Option"...
1744 if (pg
->ppd_conform
== PPD_CONFORM_STRICT
&&
1745 (!strcmp(constraint
->option1
, "*") ||
1746 !strcmp(constraint
->choice1
, "*") ||
1747 !strcmp(constraint
->option2
, "*")))
1749 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1754 * The following _cups_strcpy's are safe, as optionN and
1755 * choiceN are all the same size (size defined by PPD spec...)
1758 if (constraint
->option1
[0] == '*')
1759 _cups_strcpy(constraint
->option1
, constraint
->option1
+ 1);
1760 else if (pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1762 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1766 if (constraint
->choice1
[0] == '*')
1768 if (pg
->ppd_conform
== PPD_CONFORM_STRICT
&&
1769 constraint
->option2
[0] == '*')
1771 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1775 _cups_strcpy(constraint
->choice2
, constraint
->option2
);
1776 _cups_strcpy(constraint
->option2
, constraint
->choice1
+ 1);
1777 constraint
->choice1
[0] = '\0';
1781 if (constraint
->option2
[0] == '*')
1782 _cups_strcpy(constraint
->option2
, constraint
->option2
+ 1);
1783 else if (pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1785 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1789 constraint
->choice2
[0] = '\0';
1793 case 4 : /* Two options, two choices... */
1795 * Check for broken constraints like "* Option"...
1798 if (pg
->ppd_conform
== PPD_CONFORM_STRICT
&&
1799 (!strcmp(constraint
->option1
, "*") ||
1800 !strcmp(constraint
->choice1
, "*") ||
1801 !strcmp(constraint
->option2
, "*") ||
1802 !strcmp(constraint
->choice2
, "*")))
1804 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1808 if (constraint
->option1
[0] == '*')
1809 _cups_strcpy(constraint
->option1
, constraint
->option1
+ 1);
1810 else if (pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1812 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1816 if (pg
->ppd_conform
== PPD_CONFORM_STRICT
&&
1817 constraint
->choice1
[0] == '*')
1819 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1823 if (constraint
->option2
[0] == '*')
1824 _cups_strcpy(constraint
->option2
, constraint
->option2
+ 1);
1825 else if (pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1827 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1831 if (pg
->ppd_conform
== PPD_CONFORM_STRICT
&&
1832 constraint
->choice2
[0] == '*')
1834 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1841 * Don't add this one as an attribute...
1844 _cupsStrFree(string
);
1847 else if (!strcmp(keyword
, "PaperDimension"))
1849 if ((size
= ppdPageSize(ppd
, name
)) == NULL
)
1850 size
= ppd_add_size(ppd
, name
);
1855 * Unable to add or find size!
1858 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1863 size
->width
= (float)_cupsStrScand(string
, &sptr
, loc
);
1864 size
->length
= (float)_cupsStrScand(sptr
, NULL
, loc
);
1866 _cupsStrFree(string
);
1869 else if (!strcmp(keyword
, "ImageableArea"))
1871 if ((size
= ppdPageSize(ppd
, name
)) == NULL
)
1872 size
= ppd_add_size(ppd
, name
);
1877 * Unable to add or find size!
1880 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1885 size
->left
= (float)_cupsStrScand(string
, &sptr
, loc
);
1886 size
->bottom
= (float)_cupsStrScand(sptr
, &sptr
, loc
);
1887 size
->right
= (float)_cupsStrScand(sptr
, &sptr
, loc
);
1888 size
->top
= (float)_cupsStrScand(sptr
, NULL
, loc
);
1890 _cupsStrFree(string
);
1893 else if (option
!= NULL
&&
1894 (mask
& (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
)) ==
1895 (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
) &&
1896 !strcmp(keyword
, option
->keyword
))
1898 DEBUG_printf(("2_ppdOpen: group=%p, subgroup=%p", group
, subgroup
));
1900 if (!strcmp(keyword
, "PageSize"))
1903 * Add a page size...
1906 if (ppdPageSize(ppd
, name
) == NULL
)
1907 ppd_add_size(ppd
, name
);
1911 * Add the option choice...
1914 if ((choice
= ppd_add_choice(option
, name
)) == NULL
)
1916 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1922 cupsCharsetToUTF8((cups_utf8_t
*)choice
->text
, text
,
1923 sizeof(choice
->text
), encoding
);
1924 else if (!strcmp(name
, "True"))
1925 strlcpy(choice
->text
, _("Yes"), sizeof(choice
->text
));
1926 else if (!strcmp(name
, "False"))
1927 strlcpy(choice
->text
, _("No"), sizeof(choice
->text
));
1929 strlcpy(choice
->text
, name
, sizeof(choice
->text
));
1931 if (option
->section
== PPD_ORDER_JCL
)
1932 ppd_decode(string
); /* Decode quoted string */
1934 choice
->code
= string
;
1935 string
= NULL
; /* Don't add as an attribute below */
1939 * Add remaining lines with keywords and string values as attributes...
1943 (mask
& (PPD_KEYWORD
| PPD_STRING
)) == (PPD_KEYWORD
| PPD_STRING
))
1944 ppd_add_attr(ppd
, keyword
, name
, text
, string
);
1946 _cupsStrFree(string
);
1950 * Check for a missing CloseUI/JCLCloseUI...
1953 if (option
&& pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1955 pg
->ppd_status
= PPD_MISSING_CLOSE_UI
;
1960 * Check for a missing CloseGroup...
1963 if (group
&& pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1965 pg
->ppd_status
= PPD_MISSING_CLOSE_GROUP
;
1969 ppd_free(line
.buffer
);
1972 * Reset language preferences...
1976 if (!cupsFileEOF(fp
))
1977 DEBUG_printf(("1_ppdOpen: Premature EOF at %lu...\n",
1978 (unsigned long)cupsFileTell(fp
)));
1981 if (pg
->ppd_status
!= PPD_OK
)
1984 * Had an error reading the PPD file, cannot continue!
1993 * Update the filters array as needed...
1996 if (!ppd_update_filters(ppd
, pg
))
2004 * Create the sorted options array and set the option back-pointer for
2005 * each choice and custom option...
2008 ppd
->options
= cupsArrayNew2((cups_array_func_t
)ppd_compare_options
, NULL
,
2009 (cups_ahash_func_t
)ppd_hash_option
,
2012 for (i
= ppd
->num_groups
, group
= ppd
->groups
;
2016 for (j
= group
->num_options
, option
= group
->options
;
2020 ppd_coption_t
*coption
; /* Custom option */
2023 cupsArrayAdd(ppd
->options
, option
);
2025 for (k
= 0; k
< option
->num_choices
; k
++)
2026 option
->choices
[k
].option
= option
;
2028 if ((coption
= ppdFindCustomOption(ppd
, option
->keyword
)) != NULL
)
2029 coption
->option
= option
;
2034 * Create an array to track the marked choices...
2037 ppd
->marked
= cupsArrayNew((cups_array_func_t
)ppd_compare_choices
, NULL
);
2040 * Return the PPD file structure...
2046 * Common exit point for errors to save code size...
2051 _cupsStrFree(string
);
2052 ppd_free(line
.buffer
);
2061 * 'ppdOpen()' - Read a PPD file into memory.
2064 ppd_file_t
* /* O - PPD file record */
2065 ppdOpen(FILE *fp
) /* I - File to read from */
2067 ppd_file_t
*ppd
; /* PPD file record */
2068 cups_file_t
*cf
; /* CUPS file */
2072 * Reopen the stdio file as a CUPS file...
2075 if ((cf
= cupsFileOpenFd(fileno(fp
), "r")) == NULL
)
2079 * Load the PPD file using the newer API...
2082 ppd
= _ppdOpen(cf
, _PPD_LOCALIZATION_DEFAULT
);
2085 * Close the CUPS file and return the PPD...
2095 * 'ppdOpen2()' - Read a PPD file into memory.
2097 * @since CUPS 1.2/macOS 10.5@
2100 ppd_file_t
* /* O - PPD file record or @code NULL@ if the PPD file could not be opened. */
2101 ppdOpen2(cups_file_t
*fp
) /* I - File to read from */
2103 return _ppdOpen(fp
, _PPD_LOCALIZATION_DEFAULT
);
2108 * 'ppdOpenFd()' - Read a PPD file into memory.
2111 ppd_file_t
* /* O - PPD file record or @code NULL@ if the PPD file could not be opened. */
2112 ppdOpenFd(int fd
) /* I - File to read from */
2114 cups_file_t
*fp
; /* CUPS file pointer */
2115 ppd_file_t
*ppd
; /* PPD file record */
2116 _ppd_globals_t
*pg
= _ppdGlobals();
2121 * Set the line number to 0...
2127 * Range check input...
2132 pg
->ppd_status
= PPD_NULL_FILE
;
2138 * Try to open the file and parse it...
2141 if ((fp
= cupsFileOpenFd(fd
, "r")) != NULL
)
2149 pg
->ppd_status
= PPD_FILE_OPEN_ERROR
;
2158 * '_ppdOpenFile()' - Read a PPD file into memory.
2161 ppd_file_t
* /* O - PPD file record or @code NULL@ if the PPD file could not be opened. */
2162 _ppdOpenFile(const char *filename
, /* I - File to read from */
2163 _ppd_localization_t localization
) /* I - Localization to load */
2165 cups_file_t
*fp
; /* File pointer */
2166 ppd_file_t
*ppd
; /* PPD file record */
2167 _ppd_globals_t
*pg
= _ppdGlobals();
2172 * Set the line number to 0...
2178 * Range check input...
2181 if (filename
== NULL
)
2183 pg
->ppd_status
= PPD_NULL_FILE
;
2189 * Try to open the file and parse it...
2192 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
2194 ppd
= _ppdOpen(fp
, localization
);
2200 pg
->ppd_status
= PPD_FILE_OPEN_ERROR
;
2209 * 'ppdOpenFile()' - Read a PPD file into memory.
2212 ppd_file_t
* /* O - PPD file record or @code NULL@ if the PPD file could not be opened. */
2213 ppdOpenFile(const char *filename
) /* I - File to read from */
2215 return _ppdOpenFile(filename
, _PPD_LOCALIZATION_DEFAULT
);
2220 * 'ppdSetConformance()' - Set the conformance level for PPD files.
2222 * @since CUPS 1.1.20/macOS 10.4@
2226 ppdSetConformance(ppd_conform_t c
) /* I - Conformance level */
2228 _ppd_globals_t
*pg
= _ppdGlobals();
2232 pg
->ppd_conform
= c
;
2237 * 'ppd_add_attr()' - Add an attribute to the PPD data.
2240 static ppd_attr_t
* /* O - New attribute */
2241 ppd_add_attr(ppd_file_t
*ppd
, /* I - PPD file data */
2242 const char *name
, /* I - Attribute name */
2243 const char *spec
, /* I - Specifier string, if any */
2244 const char *text
, /* I - Text string, if any */
2245 const char *value
) /* I - Value of attribute */
2247 ppd_attr_t
**ptr
, /* New array */
2248 *temp
; /* New attribute */
2252 * Range check input...
2255 if (ppd
== NULL
|| name
== NULL
|| spec
== NULL
)
2259 * Create the array as needed...
2262 if (!ppd
->sorted_attrs
)
2263 ppd
->sorted_attrs
= cupsArrayNew((cups_array_func_t
)ppd_compare_attrs
,
2267 * Allocate memory for the new attribute...
2270 if (ppd
->num_attrs
== 0)
2271 ptr
= malloc(sizeof(ppd_attr_t
*));
2273 ptr
= realloc(ppd
->attrs
, (size_t)(ppd
->num_attrs
+ 1) * sizeof(ppd_attr_t
*));
2279 ptr
+= ppd
->num_attrs
;
2281 if ((temp
= calloc(1, sizeof(ppd_attr_t
))) == NULL
)
2292 strlcpy(temp
->name
, name
, sizeof(temp
->name
));
2293 strlcpy(temp
->spec
, spec
, sizeof(temp
->spec
));
2294 strlcpy(temp
->text
, text
, sizeof(temp
->text
));
2295 temp
->value
= (char *)value
;
2298 * Add the attribute to the sorted array...
2301 cupsArrayAdd(ppd
->sorted_attrs
, temp
);
2304 * Return the attribute...
2312 * 'ppd_add_choice()' - Add a choice to an option.
2315 static ppd_choice_t
* /* O - Named choice */
2316 ppd_add_choice(ppd_option_t
*option
, /* I - Option */
2317 const char *name
) /* I - Name of choice */
2319 ppd_choice_t
*choice
; /* Choice */
2322 if (option
->num_choices
== 0)
2323 choice
= malloc(sizeof(ppd_choice_t
));
2325 choice
= realloc(option
->choices
, sizeof(ppd_choice_t
) * (size_t)(option
->num_choices
+ 1));
2330 option
->choices
= choice
;
2331 choice
+= option
->num_choices
;
2332 option
->num_choices
++;
2334 memset(choice
, 0, sizeof(ppd_choice_t
));
2335 strlcpy(choice
->choice
, name
, sizeof(choice
->choice
));
2342 * 'ppd_add_size()' - Add a page size.
2345 static ppd_size_t
* /* O - Named size */
2346 ppd_add_size(ppd_file_t
*ppd
, /* I - PPD file */
2347 const char *name
) /* I - Name of size */
2349 ppd_size_t
*size
; /* Size */
2352 if (ppd
->num_sizes
== 0)
2353 size
= malloc(sizeof(ppd_size_t
));
2355 size
= realloc(ppd
->sizes
, sizeof(ppd_size_t
) * (size_t)(ppd
->num_sizes
+ 1));
2361 size
+= ppd
->num_sizes
;
2364 memset(size
, 0, sizeof(ppd_size_t
));
2365 strlcpy(size
->name
, name
, sizeof(size
->name
));
2372 * 'ppd_compare_attrs()' - Compare two attributes.
2375 static int /* O - Result of comparison */
2376 ppd_compare_attrs(ppd_attr_t
*a
, /* I - First attribute */
2377 ppd_attr_t
*b
) /* I - Second attribute */
2379 return (_cups_strcasecmp(a
->name
, b
->name
));
2384 * 'ppd_compare_choices()' - Compare two choices...
2387 static int /* O - Result of comparison */
2388 ppd_compare_choices(ppd_choice_t
*a
, /* I - First choice */
2389 ppd_choice_t
*b
) /* I - Second choice */
2391 return (strcmp(a
->option
->keyword
, b
->option
->keyword
));
2396 * 'ppd_compare_coptions()' - Compare two custom options.
2399 static int /* O - Result of comparison */
2400 ppd_compare_coptions(ppd_coption_t
*a
, /* I - First option */
2401 ppd_coption_t
*b
) /* I - Second option */
2403 return (_cups_strcasecmp(a
->keyword
, b
->keyword
));
2408 * 'ppd_compare_options()' - Compare two options.
2411 static int /* O - Result of comparison */
2412 ppd_compare_options(ppd_option_t
*a
, /* I - First option */
2413 ppd_option_t
*b
) /* I - Second option */
2415 return (_cups_strcasecmp(a
->keyword
, b
->keyword
));
2420 * 'ppd_decode()' - Decode a string value...
2423 static int /* O - Length of decoded string */
2424 ppd_decode(char *string
) /* I - String to decode */
2426 char *inptr
, /* Input pointer */
2427 *outptr
; /* Output pointer */
2433 while (*inptr
!= '\0')
2434 if (*inptr
== '<' && isxdigit(inptr
[1] & 255))
2437 * Convert hex to 8-bit values...
2441 while (isxdigit(*inptr
& 255))
2443 if (_cups_isalpha(*inptr
))
2444 *outptr
= (char)((tolower(*inptr
) - 'a' + 10) << 4);
2446 *outptr
= (char)((*inptr
- '0') << 4);
2450 if (!isxdigit(*inptr
& 255))
2453 if (_cups_isalpha(*inptr
))
2454 *outptr
|= (char)(tolower(*inptr
) - 'a' + 10);
2456 *outptr
|= (char)(*inptr
- '0');
2462 while (*inptr
!= '>' && *inptr
!= '\0')
2464 while (*inptr
== '>')
2468 *outptr
++ = *inptr
++;
2472 return ((int)(outptr
- string
));
2477 * 'ppd_free_filters()' - Free the filters array.
2481 ppd_free_filters(ppd_file_t
*ppd
) /* I - PPD file */
2483 int i
; /* Looping var */
2484 char **filter
; /* Current filter */
2487 if (ppd
->num_filters
> 0)
2489 for (i
= ppd
->num_filters
, filter
= ppd
->filters
; i
> 0; i
--, filter
++)
2490 _cupsStrFree(*filter
);
2492 ppd_free(ppd
->filters
);
2494 ppd
->num_filters
= 0;
2495 ppd
->filters
= NULL
;
2501 * 'ppd_free_group()' - Free a single UI group.
2505 ppd_free_group(ppd_group_t
*group
) /* I - Group to free */
2507 int i
; /* Looping var */
2508 ppd_option_t
*option
; /* Current option */
2509 ppd_group_t
*subgroup
; /* Current sub-group */
2512 if (group
->num_options
> 0)
2514 for (i
= group
->num_options
, option
= group
->options
;
2517 ppd_free_option(option
);
2519 ppd_free(group
->options
);
2522 if (group
->num_subgroups
> 0)
2524 for (i
= group
->num_subgroups
, subgroup
= group
->subgroups
;
2527 ppd_free_group(subgroup
);
2529 ppd_free(group
->subgroups
);
2535 * 'ppd_free_option()' - Free a single option.
2539 ppd_free_option(ppd_option_t
*option
) /* I - Option to free */
2541 int i
; /* Looping var */
2542 ppd_choice_t
*choice
; /* Current choice */
2545 if (option
->num_choices
> 0)
2547 for (i
= option
->num_choices
, choice
= option
->choices
;
2551 _cupsStrFree(choice
->code
);
2554 ppd_free(option
->choices
);
2560 * 'ppd_get_coption()' - Get a custom option record.
2563 static ppd_coption_t
* /* O - Custom option... */
2564 ppd_get_coption(ppd_file_t
*ppd
, /* I - PPD file */
2565 const char *name
) /* I - Name of option */
2567 ppd_coption_t
*copt
; /* New custom option */
2571 * See if the option already exists...
2574 if ((copt
= ppdFindCustomOption(ppd
, name
)) != NULL
)
2578 * Not found, so create the custom option record...
2581 if ((copt
= calloc(1, sizeof(ppd_coption_t
))) == NULL
)
2584 strlcpy(copt
->keyword
, name
, sizeof(copt
->keyword
));
2586 copt
->params
= cupsArrayNew((cups_array_func_t
)NULL
, NULL
);
2588 cupsArrayAdd(ppd
->coptions
, copt
);
2591 * Return the new record...
2599 * 'ppd_get_cparam()' - Get a custom parameter record.
2602 static ppd_cparam_t
* /* O - Extended option... */
2603 ppd_get_cparam(ppd_coption_t
*opt
, /* I - PPD file */
2604 const char *param
, /* I - Name of parameter */
2605 const char *text
) /* I - Human-readable text */
2607 ppd_cparam_t
*cparam
; /* New custom parameter */
2611 * See if the parameter already exists...
2614 if ((cparam
= ppdFindCustomParam(opt
, param
)) != NULL
)
2618 * Not found, so create the custom parameter record...
2621 if ((cparam
= calloc(1, sizeof(ppd_cparam_t
))) == NULL
)
2624 strlcpy(cparam
->name
, param
, sizeof(cparam
->name
));
2625 strlcpy(cparam
->text
, text
[0] ? text
: param
, sizeof(cparam
->text
));
2628 * Add this record to the array...
2631 cupsArrayAdd(opt
->params
, cparam
);
2634 * Return the new record...
2642 * 'ppd_get_group()' - Find or create the named group as needed.
2645 static ppd_group_t
* /* O - Named group */
2646 ppd_get_group(ppd_file_t
*ppd
, /* I - PPD file */
2647 const char *name
, /* I - Name of group */
2648 const char *text
, /* I - Text for group */
2649 _ppd_globals_t
*pg
, /* I - Global data */
2650 cups_encoding_t encoding
) /* I - Encoding of text */
2652 int i
; /* Looping var */
2653 ppd_group_t
*group
; /* Group */
2656 DEBUG_printf(("7ppd_get_group(ppd=%p, name=\"%s\", text=\"%s\", cg=%p)",
2657 ppd
, name
, text
, pg
));
2659 for (i
= ppd
->num_groups
, group
= ppd
->groups
; i
> 0; i
--, group
++)
2660 if (!strcmp(group
->name
, name
))
2665 DEBUG_printf(("8ppd_get_group: Adding group %s...", name
));
2667 if (pg
->ppd_conform
== PPD_CONFORM_STRICT
&& strlen(text
) >= sizeof(group
->text
))
2669 pg
->ppd_status
= PPD_ILLEGAL_TRANSLATION
;
2674 if (ppd
->num_groups
== 0)
2675 group
= malloc(sizeof(ppd_group_t
));
2677 group
= realloc(ppd
->groups
, (size_t)(ppd
->num_groups
+ 1) * sizeof(ppd_group_t
));
2681 pg
->ppd_status
= PPD_ALLOC_ERROR
;
2686 ppd
->groups
= group
;
2687 group
+= ppd
->num_groups
;
2690 memset(group
, 0, sizeof(ppd_group_t
));
2691 strlcpy(group
->name
, name
, sizeof(group
->name
));
2693 cupsCharsetToUTF8((cups_utf8_t
*)group
->text
, text
,
2694 sizeof(group
->text
), encoding
);
2702 * 'ppd_get_option()' - Find or create the named option as needed.
2705 static ppd_option_t
* /* O - Named option */
2706 ppd_get_option(ppd_group_t
*group
, /* I - Group */
2707 const char *name
) /* I - Name of option */
2709 int i
; /* Looping var */
2710 ppd_option_t
*option
; /* Option */
2713 DEBUG_printf(("7ppd_get_option(group=%p(\"%s\"), name=\"%s\")",
2714 group
, group
->name
, name
));
2716 for (i
= group
->num_options
, option
= group
->options
; i
> 0; i
--, option
++)
2717 if (!strcmp(option
->keyword
, name
))
2722 if (group
->num_options
== 0)
2723 option
= malloc(sizeof(ppd_option_t
));
2725 option
= realloc(group
->options
, (size_t)(group
->num_options
+ 1) * sizeof(ppd_option_t
));
2730 group
->options
= option
;
2731 option
+= group
->num_options
;
2732 group
->num_options
++;
2734 memset(option
, 0, sizeof(ppd_option_t
));
2735 strlcpy(option
->keyword
, name
, sizeof(option
->keyword
));
2743 * 'ppd_globals_alloc()' - Allocate and initialize global data.
2746 static _ppd_globals_t
* /* O - Pointer to global data */
2747 ppd_globals_alloc(void)
2749 return ((_ppd_globals_t
*)calloc(1, sizeof(_ppd_globals_t
)));
2754 * 'ppd_globals_free()' - Free global data.
2757 #if defined(HAVE_PTHREAD_H) || defined(_WIN32)
2759 ppd_globals_free(_ppd_globals_t
*pg
) /* I - Pointer to global data */
2763 #endif /* HAVE_PTHREAD_H || _WIN32 */
2766 #ifdef HAVE_PTHREAD_H
2768 * 'ppd_globals_init()' - Initialize per-thread globals...
2772 ppd_globals_init(void)
2775 * Register the global data for this thread...
2778 pthread_key_create(&ppd_globals_key
, (void (*)(void *))ppd_globals_free
);
2780 #endif /* HAVE_PTHREAD_H */
2784 * 'ppd_hash_option()' - Generate a hash of the option name...
2787 static int /* O - Hash index */
2788 ppd_hash_option(ppd_option_t
*option
) /* I - Option */
2790 int hash
= 0; /* Hash index */
2791 const char *k
; /* Pointer into keyword */
2794 for (hash
= option
->keyword
[0], k
= option
->keyword
+ 1; *k
;)
2795 hash
= 33 * hash
+ *k
++;
2797 return (hash
& 511);
2802 * 'ppd_read()' - Read a line from a PPD file, skipping comment lines as
2806 static int /* O - Bitmask of fields read */
2807 ppd_read(cups_file_t
*fp
, /* I - File to read from */
2808 _ppd_line_t
*line
, /* I - Line buffer */
2809 char *keyword
, /* O - Keyword from line */
2810 char *option
, /* O - Option from line */
2811 char *text
, /* O - Human-readable text from line */
2812 char **string
, /* O - Code/string data */
2813 int ignoreblank
, /* I - Ignore blank lines? */
2814 _ppd_globals_t
*pg
) /* I - Global data */
2816 int ch
, /* Character from file */
2817 col
, /* Column in line */
2818 colon
, /* Colon seen? */
2819 endquote
, /* Waiting for an end quote */
2820 mask
, /* Mask to be returned */
2821 startline
, /* Start line */
2822 textlen
; /* Length of text */
2823 char *keyptr
, /* Keyword pointer */
2824 *optptr
, /* Option pointer */
2825 *textptr
, /* Text pointer */
2826 *strptr
, /* Pointer into string */
2827 *lineptr
; /* Current position in line buffer */
2831 * Now loop until we have a valid line...
2836 startline
= pg
->ppd_line
+ 1;
2840 line
->bufsize
= 1024;
2841 line
->buffer
= malloc(1024);
2853 lineptr
= line
->buffer
;
2857 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
2859 if (lineptr
>= (line
->buffer
+ line
->bufsize
- 1))
2862 * Expand the line buffer...
2865 char *temp
; /* Temporary line pointer */
2868 line
->bufsize
+= 1024;
2869 if (line
->bufsize
> 262144)
2872 * Don't allow lines longer than 256k!
2875 pg
->ppd_line
= startline
;
2876 pg
->ppd_status
= PPD_LINE_TOO_LONG
;
2881 temp
= realloc(line
->buffer
, line
->bufsize
);
2884 pg
->ppd_line
= startline
;
2885 pg
->ppd_status
= PPD_LINE_TOO_LONG
;
2890 lineptr
= temp
+ (lineptr
- line
->buffer
);
2891 line
->buffer
= temp
;
2894 if (ch
== '\r' || ch
== '\n')
2897 * Line feed or carriage return...
2906 * Check for a trailing line feed...
2909 if ((ch
= cupsFilePeekChar(fp
)) == EOF
)
2916 cupsFileGetChar(fp
);
2919 if (lineptr
== line
->buffer
&& ignoreblank
)
2920 continue; /* Skip blank lines */
2924 if (!endquote
) /* Continue for multi-line text */
2929 else if (ch
< ' ' && ch
!= '\t' && pg
->ppd_conform
== PPD_CONFORM_STRICT
)
2932 * Other control characters...
2935 pg
->ppd_line
= startline
;
2936 pg
->ppd_status
= PPD_ILLEGAL_CHARACTER
;
2940 else if (ch
!= 0x1a)
2943 * Any other character...
2946 *lineptr
++ = (char)ch
;
2949 if (col
> (PPD_MAX_LINE
- 1))
2952 * Line is too long...
2955 pg
->ppd_line
= startline
;
2956 pg
->ppd_status
= PPD_LINE_TOO_LONG
;
2961 if (ch
== ':' && strncmp(line
->buffer
, "*%", 2) != 0)
2964 if (ch
== '\"' && colon
)
2965 endquote
= !endquote
;
2972 * Didn't finish this quoted string...
2975 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
2978 else if (ch
== '\r' || ch
== '\n')
2986 * Check for a trailing line feed...
2989 if ((ch
= cupsFilePeekChar(fp
)) == EOF
)
2992 cupsFileGetChar(fp
);
2995 else if (ch
< ' ' && ch
!= '\t' && pg
->ppd_conform
== PPD_CONFORM_STRICT
)
2998 * Other control characters...
3001 pg
->ppd_line
= startline
;
3002 pg
->ppd_status
= PPD_ILLEGAL_CHARACTER
;
3006 else if (ch
!= 0x1a)
3010 if (col
> (PPD_MAX_LINE
- 1))
3013 * Line is too long...
3016 pg
->ppd_line
= startline
;
3017 pg
->ppd_status
= PPD_LINE_TOO_LONG
;
3027 * Didn't finish this line...
3030 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
3031 if (ch
== '\r' || ch
== '\n')
3034 * Line feed or carriage return...
3043 * Check for a trailing line feed...
3046 if ((ch
= cupsFilePeekChar(fp
)) == EOF
)
3049 cupsFileGetChar(fp
);
3054 else if (ch
< ' ' && ch
!= '\t' && pg
->ppd_conform
== PPD_CONFORM_STRICT
)
3057 * Other control characters...
3060 pg
->ppd_line
= startline
;
3061 pg
->ppd_status
= PPD_ILLEGAL_CHARACTER
;
3065 else if (ch
!= 0x1a)
3069 if (col
> (PPD_MAX_LINE
- 1))
3072 * Line is too long...
3075 pg
->ppd_line
= startline
;
3076 pg
->ppd_status
= PPD_LINE_TOO_LONG
;
3083 if (lineptr
> line
->buffer
&& lineptr
[-1] == '\n')
3088 DEBUG_printf(("9ppd_read: LINE=\"%s\"", line
->buffer
));
3091 * The dynamically created PPDs for older style macOS
3092 * drivers include a large blob of data inserted as comments
3093 * at the end of the file. As an optimization we can stop
3094 * reading the PPD when we get to the start of this data.
3097 if (!strcmp(line
->buffer
, "*%APLWORKSET START"))
3100 if (ch
== EOF
&& lineptr
== line
->buffer
)
3108 lineptr
= line
->buffer
+ 1;
3115 if ((!line
->buffer
[0] || /* Blank line */
3116 !strncmp(line
->buffer
, "*%", 2) || /* Comment line */
3117 !strcmp(line
->buffer
, "*End")) && /* End of multi-line string */
3118 ignoreblank
) /* Ignore these? */
3120 startline
= pg
->ppd_line
+ 1;
3124 if (!strcmp(line
->buffer
, "*")) /* (Bad) comment line */
3126 if (pg
->ppd_conform
== PPD_CONFORM_RELAXED
)
3128 startline
= pg
->ppd_line
+ 1;
3133 pg
->ppd_line
= startline
;
3134 pg
->ppd_status
= PPD_ILLEGAL_MAIN_KEYWORD
;
3140 if (line
->buffer
[0] != '*') /* All lines start with an asterisk */
3143 * Allow lines consisting of just whitespace...
3146 for (lineptr
= line
->buffer
; *lineptr
; lineptr
++)
3147 if (*lineptr
&& !_cups_isspace(*lineptr
))
3152 pg
->ppd_status
= PPD_MISSING_ASTERISK
;
3155 else if (ignoreblank
)
3167 while (*lineptr
&& *lineptr
!= ':' && !_cups_isspace(*lineptr
))
3169 if (*lineptr
<= ' ' || *lineptr
> 126 || *lineptr
== '/' ||
3170 (keyptr
- keyword
) >= (PPD_MAX_NAME
- 1))
3172 pg
->ppd_status
= PPD_ILLEGAL_MAIN_KEYWORD
;
3176 *keyptr
++ = *lineptr
++;
3181 if (!strcmp(keyword
, "End"))
3184 mask
|= PPD_KEYWORD
;
3186 if (_cups_isspace(*lineptr
))
3189 * Get an option name...
3192 while (_cups_isspace(*lineptr
))
3197 while (*lineptr
&& !_cups_isspace(*lineptr
) && *lineptr
!= ':' &&
3200 if (*lineptr
<= ' ' || *lineptr
> 126 ||
3201 (optptr
- option
) >= (PPD_MAX_NAME
- 1))
3203 pg
->ppd_status
= PPD_ILLEGAL_OPTION_KEYWORD
;
3207 *optptr
++ = *lineptr
++;
3212 if (_cups_isspace(*lineptr
) && pg
->ppd_conform
== PPD_CONFORM_STRICT
)
3214 pg
->ppd_status
= PPD_ILLEGAL_WHITESPACE
;
3218 while (_cups_isspace(*lineptr
))
3223 if (*lineptr
== '/')
3226 * Get human-readable text...
3233 while (*lineptr
!= '\0' && *lineptr
!= '\n' && *lineptr
!= ':')
3235 if (((unsigned char)*lineptr
< ' ' && *lineptr
!= '\t') ||
3236 (textptr
- text
) >= (PPD_MAX_LINE
- 1))
3238 pg
->ppd_status
= PPD_ILLEGAL_TRANSLATION
;
3242 *textptr
++ = *lineptr
++;
3246 textlen
= ppd_decode(text
);
3248 if (textlen
> PPD_MAX_TEXT
&& pg
->ppd_conform
== PPD_CONFORM_STRICT
)
3250 pg
->ppd_status
= PPD_ILLEGAL_TRANSLATION
;
3258 if (_cups_isspace(*lineptr
) && pg
->ppd_conform
== PPD_CONFORM_STRICT
)
3260 pg
->ppd_status
= PPD_ILLEGAL_WHITESPACE
;
3264 while (_cups_isspace(*lineptr
))
3267 if (*lineptr
== ':')
3270 * Get string after triming leading and trailing whitespace...
3274 while (_cups_isspace(*lineptr
))
3277 strptr
= lineptr
+ strlen(lineptr
) - 1;
3278 while (strptr
>= lineptr
&& _cups_isspace(*strptr
))
3281 if (*strptr
== '\"')
3284 * Quoted string by itself, remove quotes...
3291 *string
= _cupsStrAlloc(lineptr
);
3303 * 'ppd_update_filters()' - Update the filters array as needed.
3305 * This function re-populates the filters array with cupsFilter2 entries that
3306 * have been stripped of the destination MIME media types and any maxsize hints.
3308 * (All for backwards-compatibility)
3311 static int /* O - 1 on success, 0 on failure */
3312 ppd_update_filters(ppd_file_t
*ppd
, /* I - PPD file */
3313 _ppd_globals_t
*pg
) /* I - Global data */
3315 ppd_attr_t
*attr
; /* Current cupsFilter2 value */
3316 char srcsuper
[16], /* Source MIME media type */
3318 dstsuper
[16], /* Destination MIME media type */
3320 program
[1024], /* Command to run */
3321 *ptr
, /* Pointer into command to run */
3322 buffer
[1024], /* Re-written cupsFilter value */
3323 **filter
; /* Current filter */
3324 int cost
; /* Cost of filter */
3327 DEBUG_printf(("4ppd_update_filters(ppd=%p, cg=%p)", ppd
, pg
));
3330 * See if we have any cupsFilter2 lines...
3333 if ((attr
= ppdFindAttr(ppd
, "cupsFilter2", NULL
)) == NULL
)
3335 DEBUG_puts("5ppd_update_filters: No cupsFilter2 keywords present.");
3340 * Yes, free the cupsFilter-defined filters and re-build...
3343 ppd_free_filters(ppd
);
3348 * Parse the cupsFilter2 string:
3350 * src/type dst/type cost program
3351 * src/type dst/type cost maxsize(n) program
3354 DEBUG_printf(("5ppd_update_filters: cupsFilter2=\"%s\"", attr
->value
));
3356 if (sscanf(attr
->value
, "%15[^/]/%255s%*[ \t]%15[^/]/%255s%d%*[ \t]%1023[^\n]",
3357 srcsuper
, srctype
, dstsuper
, dsttype
, &cost
, program
) != 6)
3359 DEBUG_puts("5ppd_update_filters: Bad cupsFilter2 line.");
3360 pg
->ppd_status
= PPD_BAD_VALUE
;
3365 DEBUG_printf(("5ppd_update_filters: srcsuper=\"%s\", srctype=\"%s\", "
3366 "dstsuper=\"%s\", dsttype=\"%s\", cost=%d, program=\"%s\"",
3367 srcsuper
, srctype
, dstsuper
, dsttype
, cost
, program
));
3369 if (!strncmp(program
, "maxsize(", 8) &&
3370 (ptr
= strchr(program
+ 8, ')')) != NULL
)
3372 DEBUG_puts("5ppd_update_filters: Found maxsize(nnn).");
3375 while (_cups_isspace(*ptr
))
3378 _cups_strcpy(program
, ptr
);
3379 DEBUG_printf(("5ppd_update_filters: New program=\"%s\"", program
));
3383 * Convert to cupsFilter format:
3385 * src/type cost program
3388 snprintf(buffer
, sizeof(buffer
), "%s/%s %d %s", srcsuper
, srctype
, cost
,
3390 DEBUG_printf(("5ppd_update_filters: Adding \"%s\".", buffer
));
3393 * Add a cupsFilter-compatible string to the filters array.
3396 if (ppd
->num_filters
== 0)
3397 filter
= malloc(sizeof(char *));
3399 filter
= realloc(ppd
->filters
, sizeof(char *) * (size_t)(ppd
->num_filters
+ 1));
3403 DEBUG_puts("5ppd_update_filters: Out of memory.");
3404 pg
->ppd_status
= PPD_ALLOC_ERROR
;
3409 ppd
->filters
= filter
;
3410 filter
+= ppd
->num_filters
;
3411 ppd
->num_filters
++;
3413 *filter
= _cupsStrAlloc(buffer
);
3415 while ((attr
= ppdFindNextAttr(ppd
, "cupsFilter2", NULL
)) != NULL
);
3417 DEBUG_puts("5ppd_update_filters: Completed OK.");