2 * PPD file routines for CUPS.
4 * Copyright 2007-2018 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"
25 #define ppd_free(p) if (p) free(p) /* Safe free macro */
27 #define PPD_KEYWORD 1 /* Line contained a keyword */
28 #define PPD_OPTION 2 /* Line contained an option name */
29 #define PPD_TEXT 4 /* Line contained human-readable text */
30 #define PPD_STRING 8 /* Line contained a string or code */
32 #define PPD_HASHSIZE 512 /* Size of hash */
36 * Line buffer structure...
39 typedef struct _ppd_line_s
41 char *buffer
; /* Pointer to buffer */
42 size_t bufsize
; /* Size of the buffer */
50 static _cups_threadkey_t ppd_globals_key
= _CUPS_THREADKEY_INITIALIZER
;
51 /* Thread local storage key */
53 static pthread_once_t ppd_globals_key_once
= PTHREAD_ONCE_INIT
;
54 /* One-time initialization object */
55 #endif /* HAVE_PTHREAD_H */
62 static ppd_attr_t
*ppd_add_attr(ppd_file_t
*ppd
, const char *name
,
63 const char *spec
, const char *text
,
65 static ppd_choice_t
*ppd_add_choice(ppd_option_t
*option
, const char *name
);
66 static ppd_size_t
*ppd_add_size(ppd_file_t
*ppd
, const char *name
);
67 static int ppd_compare_attrs(ppd_attr_t
*a
, ppd_attr_t
*b
);
68 static int ppd_compare_choices(ppd_choice_t
*a
, ppd_choice_t
*b
);
69 static int ppd_compare_coptions(ppd_coption_t
*a
,
71 static int ppd_compare_options(ppd_option_t
*a
, ppd_option_t
*b
);
72 static int ppd_decode(char *string
);
73 static void ppd_free_filters(ppd_file_t
*ppd
);
74 static void ppd_free_group(ppd_group_t
*group
);
75 static void ppd_free_option(ppd_option_t
*option
);
76 static ppd_coption_t
*ppd_get_coption(ppd_file_t
*ppd
, const char *name
);
77 static ppd_cparam_t
*ppd_get_cparam(ppd_coption_t
*opt
,
80 static ppd_group_t
*ppd_get_group(ppd_file_t
*ppd
, const char *name
,
81 const char *text
, _ppd_globals_t
*pg
,
82 cups_encoding_t encoding
);
83 static ppd_option_t
*ppd_get_option(ppd_group_t
*group
, const char *name
);
84 static _ppd_globals_t
*ppd_globals_alloc(void);
85 #if defined(HAVE_PTHREAD_H) || defined(_WIN32)
86 static void ppd_globals_free(_ppd_globals_t
*g
);
87 #endif /* HAVE_PTHREAD_H || _WIN32 */
89 static void ppd_globals_init(void);
90 #endif /* HAVE_PTHREAD_H */
91 static int ppd_hash_option(ppd_option_t
*option
);
92 static int ppd_read(cups_file_t
*fp
, _ppd_line_t
*line
,
93 char *keyword
, char *option
, char *text
,
94 char **string
, int ignoreblank
,
96 static int ppd_update_filters(ppd_file_t
*ppd
,
101 * 'ppdClose()' - Free all memory used by the PPD file.
105 ppdClose(ppd_file_t
*ppd
) /* I - PPD file record */
107 int i
; /* Looping var */
108 ppd_emul_t
*emul
; /* Current emulation */
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 emulations...
139 if (ppd
->num_emulations
> 0)
141 for (i
= ppd
->num_emulations
, emul
= ppd
->emulations
; i
> 0; i
--, emul
++)
143 _cupsStrFree(emul
->start
);
144 _cupsStrFree(emul
->stop
);
147 ppd_free(ppd
->emulations
);
151 * Free any UI groups, subgroups, and options...
154 if (ppd
->num_groups
> 0)
156 for (i
= ppd
->num_groups
, group
= ppd
->groups
; i
> 0; i
--, group
++)
157 ppd_free_group(group
);
159 ppd_free(ppd
->groups
);
162 cupsArrayDelete(ppd
->options
);
163 cupsArrayDelete(ppd
->marked
);
166 * Free any page sizes...
169 if (ppd
->num_sizes
> 0)
170 ppd_free(ppd
->sizes
);
173 * Free any constraints...
176 if (ppd
->num_consts
> 0)
177 ppd_free(ppd
->consts
);
180 * Free any filters...
183 ppd_free_filters(ppd
);
189 if (ppd
->num_fonts
> 0)
191 for (i
= ppd
->num_fonts
, font
= ppd
->fonts
; i
> 0; i
--, font
++)
194 ppd_free(ppd
->fonts
);
198 * Free any profiles...
201 if (ppd
->num_profiles
> 0)
202 ppd_free(ppd
->profiles
);
205 * Free any attributes...
208 if (ppd
->num_attrs
> 0)
210 for (i
= ppd
->num_attrs
, attr
= ppd
->attrs
; i
> 0; i
--, attr
++)
212 _cupsStrFree((*attr
)->value
);
216 ppd_free(ppd
->attrs
);
219 cupsArrayDelete(ppd
->sorted_attrs
);
222 * Free custom options...
225 for (coption
= (ppd_coption_t
*)cupsArrayFirst(ppd
->coptions
);
227 coption
= (ppd_coption_t
*)cupsArrayNext(ppd
->coptions
))
229 for (cparam
= (ppd_cparam_t
*)cupsArrayFirst(coption
->params
);
231 cparam
= (ppd_cparam_t
*)cupsArrayNext(coption
->params
))
233 switch (cparam
->type
)
235 case PPD_CUSTOM_PASSCODE
:
236 case PPD_CUSTOM_PASSWORD
:
237 case PPD_CUSTOM_STRING
:
238 _cupsStrFree(cparam
->current
.custom_string
);
248 cupsArrayDelete(coption
->params
);
253 cupsArrayDelete(ppd
->coptions
);
256 * Free constraints...
259 if (ppd
->cups_uiconstraints
)
261 _ppd_cups_uiconsts_t
*consts
; /* Current constraints */
264 for (consts
= (_ppd_cups_uiconsts_t
*)cupsArrayFirst(ppd
->cups_uiconstraints
);
266 consts
= (_ppd_cups_uiconsts_t
*)cupsArrayNext(ppd
->cups_uiconstraints
))
268 free(consts
->constraints
);
272 cupsArrayDelete(ppd
->cups_uiconstraints
);
276 * Free any PPD cache/mapping data...
280 _ppdCacheDestroy(ppd
->cache
);
283 * Free the whole record...
291 * 'ppdErrorString()' - Returns the text associated with a status.
293 * @since CUPS 1.1.19/macOS 10.3@
296 const char * /* O - Status string */
297 ppdErrorString(ppd_status_t status
) /* I - PPD status */
299 static const char * const messages
[] =/* Status messages */
302 _("Unable to open PPD file"),
303 _("NULL PPD file pointer"),
304 _("Memory allocation error"),
305 _("Missing PPD-Adobe-4.x header"),
306 _("Missing value string"),
309 _("OpenGroup without a CloseGroup first"),
310 _("Bad OpenUI/JCLOpenUI"),
311 _("OpenUI/JCLOpenUI without a CloseUI/JCLCloseUI first"),
312 _("Bad OrderDependency"),
313 _("Bad UIConstraints"),
314 _("Missing asterisk in column 1"),
315 _("Line longer than the maximum allowed (255 characters)"),
316 _("Illegal control character"),
317 _("Illegal main keyword string"),
318 _("Illegal option keyword string"),
319 _("Illegal translation string"),
320 _("Illegal whitespace character"),
321 _("Bad custom parameter"),
322 _("Missing option keyword"),
323 _("Bad value string"),
324 _("Missing CloseGroup"),
325 _("Bad CloseUI/JCLCloseUI"),
326 _("Missing CloseUI/JCLCloseUI")
330 if (status
< PPD_OK
|| status
>= PPD_MAX_STATUS
)
331 return (_cupsLangString(cupsLangDefault(), _("Unknown")));
333 return (_cupsLangString(cupsLangDefault(), messages
[status
]));
338 * '_ppdGetEncoding()' - Get the CUPS encoding value for the given
342 cups_encoding_t
/* O - CUPS encoding value */
343 _ppdGetEncoding(const char *name
) /* I - LanguageEncoding string */
345 if (!_cups_strcasecmp(name
, "ISOLatin1"))
346 return (CUPS_ISO8859_1
);
347 else if (!_cups_strcasecmp(name
, "ISOLatin2"))
348 return (CUPS_ISO8859_2
);
349 else if (!_cups_strcasecmp(name
, "ISOLatin5"))
350 return (CUPS_ISO8859_5
);
351 else if (!_cups_strcasecmp(name
, "JIS83-RKSJ"))
352 return (CUPS_JIS_X0213
);
353 else if (!_cups_strcasecmp(name
, "MacStandard"))
354 return (CUPS_MAC_ROMAN
);
355 else if (!_cups_strcasecmp(name
, "WindowsANSI"))
356 return (CUPS_WINDOWS_1252
);
363 * '_ppdGlobals()' - Return a pointer to thread local storage
366 _ppd_globals_t
* /* O - Pointer to global data */
369 _ppd_globals_t
*pg
; /* Pointer to global data */
372 #ifdef HAVE_PTHREAD_H
374 * Initialize the global data exactly once...
377 pthread_once(&ppd_globals_key_once
, ppd_globals_init
);
378 #endif /* HAVE_PTHREAD_H */
381 * See if we have allocated the data yet...
384 if ((pg
= (_ppd_globals_t
*)_cupsThreadGetData(ppd_globals_key
)) == NULL
)
387 * No, allocate memory as set the pointer for the key...
390 if ((pg
= ppd_globals_alloc()) != NULL
)
391 _cupsThreadSetData(ppd_globals_key
, pg
);
395 * Return the pointer to the data...
403 * 'ppdLastError()' - Return the status from the last ppdOpen*().
405 * @since CUPS 1.1.19/macOS 10.3@
408 ppd_status_t
/* O - Status code */
409 ppdLastError(int *line
) /* O - Line number */
411 _ppd_globals_t
*pg
= _ppdGlobals();
416 *line
= pg
->ppd_line
;
418 return (pg
->ppd_status
);
423 * '_ppdOpen()' - Read a PPD file into memory.
425 * @since CUPS 1.2/macOS 10.5@
428 ppd_file_t
* /* O - PPD file record or @code NULL@ if the PPD file could not be opened. */
430 cups_file_t
*fp
, /* I - File to read from */
431 _ppd_localization_t localization
) /* I - Localization to load */
433 int i
, j
, k
; /* Looping vars */
434 int count
; /* Temporary count */
435 _ppd_line_t line
; /* Line buffer */
436 ppd_file_t
*ppd
; /* PPD file record */
437 ppd_group_t
*group
, /* Current group */
438 *subgroup
; /* Current sub-group */
439 ppd_option_t
*option
; /* Current option */
440 ppd_choice_t
*choice
; /* Current choice */
441 ppd_const_t
*constraint
; /* Current constraint */
442 ppd_size_t
*size
; /* Current page size */
443 int mask
; /* Line data mask */
444 char keyword
[PPD_MAX_NAME
],
445 /* Keyword from file */
447 /* Option from file */
449 /* Human-readable text from file */
450 *string
, /* Code/text from file */
451 *sptr
, /* Pointer into string */
452 *nameptr
, /* Pointer into name */
453 *temp
, /* Temporary string pointer */
454 **tempfonts
; /* Temporary fonts pointer */
455 float order
; /* Order dependency number */
456 ppd_section_t section
; /* Order dependency section */
457 ppd_profile_t
*profile
; /* Pointer to color profile */
458 char **filter
; /* Pointer to filter */
459 struct lconv
*loc
; /* Locale data */
460 int ui_keyword
; /* Is this line a UI keyword? */
461 cups_lang_t
*lang
; /* Language data */
462 cups_encoding_t encoding
; /* Encoding of PPD file */
463 _ppd_globals_t
*pg
= _ppdGlobals();
465 char custom_name
[PPD_MAX_NAME
];
466 /* CustomFoo attribute name */
467 ppd_attr_t
*custom_attr
; /* CustomFoo attribute */
468 char ll
[7], /* Base language + '.' */
469 ll_CC
[7]; /* Language w/country + '.' */
470 size_t ll_len
= 0, /* Base language length */
471 ll_CC_len
= 0; /* Language w/country length */
472 static const char * const ui_keywords
[] =
474 #ifdef CUPS_USE_FULL_UI_KEYWORDS_LIST
476 * Adobe defines some 41 keywords as "UI", meaning that they are
477 * user interface elements and that they should be treated as such
478 * even if the PPD creator doesn't use Open/CloseUI around them.
480 * Since this can cause previously invisible options to appear and
481 * confuse users, the default is to only treat the PageSize and
482 * PageRegion keywords this way.
484 /* Boolean keywords */
494 /* PickOne keywords */
507 "JCLFrameBufferSize",
528 #else /* !CUPS_USE_FULL_UI_KEYWORDS_LIST */
531 #endif /* CUPS_USE_FULL_UI_KEYWORDS_LIST */
533 static const char * const color_keywords
[] = /* Keywords associated with color profiles */
540 DEBUG_printf(("_ppdOpen(fp=%p)", fp
));
543 * Default to "OK" status...
546 pg
->ppd_status
= PPD_OK
;
550 * Range check input...
555 pg
->ppd_status
= PPD_NULL_FILE
;
560 * If only loading a single localization set up the strings to match...
563 if (localization
== _PPD_LOCALIZATION_DEFAULT
)
565 if ((lang
= cupsLangDefault()) == NULL
)
568 snprintf(ll_CC
, sizeof(ll_CC
), "%s.", lang
->language
);
571 * <rdar://problem/22130168>
572 * <rdar://problem/27245567>
574 * Need to use a different base language for some locales...
577 if (!strcmp(lang
->language
, "zh_HK"))
578 { /* Traditional Chinese + variants */
579 strlcpy(ll_CC
, "zh_TW.", sizeof(ll_CC
));
580 strlcpy(ll
, "zh_", sizeof(ll
));
582 else if (!strncmp(lang
->language
, "zh", 2))
583 strlcpy(ll
, "zh_", sizeof(ll
)); /* Any Chinese variant */
584 else if (!strncmp(lang
->language
, "jp", 2))
585 { /* Any Japanese variant */
586 strlcpy(ll_CC
, "ja", sizeof(ll_CC
));
587 strlcpy(ll
, "jp", sizeof(ll
));
589 else if (!strncmp(lang
->language
, "nb", 2) || !strncmp(lang
->language
, "no", 2))
590 { /* Any Norwegian variant */
591 strlcpy(ll_CC
, "nb", sizeof(ll_CC
));
592 strlcpy(ll
, "no", sizeof(ll
));
595 snprintf(ll
, sizeof(ll
), "%2.2s.", lang
->language
);
597 ll_CC_len
= strlen(ll_CC
);
600 DEBUG_printf(("2_ppdOpen: Loading localizations matching \"%s\" and \"%s\"",
605 * Grab the first line and make sure it reads '*PPD-Adobe: "major.minor"'...
611 mask
= ppd_read(fp
, &line
, keyword
, name
, text
, &string
, 0, pg
);
613 DEBUG_printf(("2_ppdOpen: mask=%x, keyword=\"%s\"...", mask
, keyword
));
616 strcmp(keyword
, "PPD-Adobe") ||
617 string
== NULL
|| string
[0] != '4')
620 * Either this is not a PPD file, or it is not a 4.x PPD file.
623 if (pg
->ppd_status
== PPD_OK
)
624 pg
->ppd_status
= PPD_MISSING_PPDADOBE4
;
626 _cupsStrFree(string
);
627 ppd_free(line
.buffer
);
632 DEBUG_printf(("2_ppdOpen: keyword=%s, string=%p", keyword
, string
));
634 _cupsStrFree(string
);
637 * Allocate memory for the PPD file record...
640 if ((ppd
= calloc(1, sizeof(ppd_file_t
))) == NULL
)
642 pg
->ppd_status
= PPD_ALLOC_ERROR
;
644 _cupsStrFree(string
);
645 ppd_free(line
.buffer
);
650 ppd
->language_level
= 2;
651 ppd
->color_device
= 0;
652 ppd
->colorspace
= PPD_CS_N
;
653 ppd
->landscape
= -90;
654 ppd
->coptions
= cupsArrayNew((cups_array_func_t
)ppd_compare_coptions
,
658 * Read lines from the PPD file and add them to the file record...
666 encoding
= CUPS_ISO8859_1
;
669 while ((mask
= ppd_read(fp
, &line
, keyword
, name
, text
, &string
, 1, pg
)) != 0)
671 DEBUG_printf(("2_ppdOpen: mask=%x, keyword=\"%s\", name=\"%s\", "
672 "text=\"%s\", string=%d chars...", mask
, keyword
, name
, text
,
673 string
? (int)strlen(string
) : 0));
675 if (strncmp(keyword
, "Default", 7) && !string
&&
676 pg
->ppd_conform
!= PPD_CONFORM_RELAXED
)
679 * Need a string value!
682 pg
->ppd_status
= PPD_MISSING_VALUE
;
690 * Certain main keywords (as defined by the PPD spec) may be used
691 * without the usual OpenUI/CloseUI stuff. Presumably this is just
692 * so that Adobe wouldn't completely break compatibility with PPD
693 * files prior to v4.0 of the spec, but it is hopelessly
694 * inconsistent... Catch these main keywords and automatically
695 * create the corresponding option, as needed...
701 * Previous line was a UI keyword...
709 * If we are filtering out keyword localizations, see if this line needs to
713 if (localization
!= _PPD_LOCALIZATION_ALL
&&
714 (temp
= strchr(keyword
, '.')) != NULL
&&
715 ((temp
- keyword
) == 2 || (temp
- keyword
) == 5) &&
716 _cups_isalpha(keyword
[0]) &&
717 _cups_isalpha(keyword
[1]) &&
718 (keyword
[2] == '.' ||
719 (keyword
[2] == '_' && _cups_isalpha(keyword
[3]) &&
720 _cups_isalpha(keyword
[4]) && keyword
[5] == '.')))
722 if (localization
== _PPD_LOCALIZATION_NONE
||
723 (localization
== _PPD_LOCALIZATION_DEFAULT
&&
724 strncmp(ll_CC
, keyword
, ll_CC_len
) &&
725 strncmp(ll
, keyword
, ll_len
)))
727 DEBUG_printf(("2_ppdOpen: Ignoring localization: \"%s\"\n", keyword
));
730 else if (localization
== _PPD_LOCALIZATION_ICC_PROFILES
)
733 * Only load localizations for the color profile related keywords...
737 i
< (int)(sizeof(color_keywords
) / sizeof(color_keywords
[0]));
740 if (!_cups_strcasecmp(temp
, color_keywords
[i
]))
744 if (i
>= (int)(sizeof(color_keywords
) / sizeof(color_keywords
[0])))
746 DEBUG_printf(("2_ppdOpen: Ignoring localization: \"%s\"\n", keyword
));
752 if (option
== NULL
&&
753 (mask
& (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
)) ==
754 (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
))
756 for (i
= 0; i
< (int)(sizeof(ui_keywords
) / sizeof(ui_keywords
[0])); i
++)
757 if (!strcmp(keyword
, ui_keywords
[i
]))
760 if (i
< (int)(sizeof(ui_keywords
) / sizeof(ui_keywords
[0])))
763 * Create the option in the appropriate group...
768 DEBUG_printf(("2_ppdOpen: FOUND ADOBE UI KEYWORD %s WITHOUT OPENUI!",
773 if ((group
= ppd_get_group(ppd
, "General", _("General"), pg
,
777 DEBUG_printf(("2_ppdOpen: Adding to group %s...", group
->text
));
778 option
= ppd_get_option(group
, keyword
);
782 option
= ppd_get_option(group
, keyword
);
786 pg
->ppd_status
= PPD_ALLOC_ERROR
;
792 * Now fill in the initial information for the option...
795 if (!strncmp(keyword
, "JCL", 3))
796 option
->section
= PPD_ORDER_JCL
;
798 option
->section
= PPD_ORDER_ANY
;
800 option
->order
= 10.0f
;
803 option
->ui
= PPD_UI_BOOLEAN
;
805 option
->ui
= PPD_UI_PICKONE
;
807 for (j
= 0; j
< ppd
->num_attrs
; j
++)
808 if (!strncmp(ppd
->attrs
[j
]->name
, "Default", 7) &&
809 !strcmp(ppd
->attrs
[j
]->name
+ 7, keyword
) &&
810 ppd
->attrs
[j
]->value
)
812 DEBUG_printf(("2_ppdOpen: Setting Default%s to %s via attribute...",
813 option
->keyword
, ppd
->attrs
[j
]->value
));
814 strlcpy(option
->defchoice
, ppd
->attrs
[j
]->value
,
815 sizeof(option
->defchoice
));
819 if (!strcmp(keyword
, "PageSize"))
820 strlcpy(option
->text
, _("Media Size"), sizeof(option
->text
));
821 else if (!strcmp(keyword
, "MediaType"))
822 strlcpy(option
->text
, _("Media Type"), sizeof(option
->text
));
823 else if (!strcmp(keyword
, "InputSlot"))
824 strlcpy(option
->text
, _("Media Source"), sizeof(option
->text
));
825 else if (!strcmp(keyword
, "ColorModel"))
826 strlcpy(option
->text
, _("Output Mode"), sizeof(option
->text
));
827 else if (!strcmp(keyword
, "Resolution"))
828 strlcpy(option
->text
, _("Resolution"), sizeof(option
->text
));
830 strlcpy(option
->text
, keyword
, sizeof(option
->text
));
834 if (!strcmp(keyword
, "LanguageLevel"))
835 ppd
->language_level
= atoi(string
);
836 else if (!strcmp(keyword
, "LanguageEncoding"))
839 * Say all PPD files are UTF-8, since we convert to UTF-8...
842 ppd
->lang_encoding
= _cupsStrAlloc("UTF-8");
843 encoding
= _ppdGetEncoding(string
);
845 else if (!strcmp(keyword
, "LanguageVersion"))
846 ppd
->lang_version
= string
;
847 else if (!strcmp(keyword
, "Manufacturer"))
848 ppd
->manufacturer
= string
;
849 else if (!strcmp(keyword
, "ModelName"))
850 ppd
->modelname
= string
;
851 else if (!strcmp(keyword
, "Protocols"))
852 ppd
->protocols
= string
;
853 else if (!strcmp(keyword
, "PCFileName"))
854 ppd
->pcfilename
= string
;
855 else if (!strcmp(keyword
, "NickName"))
857 if (encoding
!= CUPS_UTF8
)
859 cups_utf8_t utf8
[256]; /* UTF-8 version of NickName */
862 cupsCharsetToUTF8(utf8
, string
, sizeof(utf8
), encoding
);
863 ppd
->nickname
= _cupsStrAlloc((char *)utf8
);
866 ppd
->nickname
= _cupsStrAlloc(string
);
868 else if (!strcmp(keyword
, "Product"))
869 ppd
->product
= string
;
870 else if (!strcmp(keyword
, "ShortNickName"))
871 ppd
->shortnickname
= string
;
872 else if (!strcmp(keyword
, "TTRasterizer"))
873 ppd
->ttrasterizer
= string
;
874 else if (!strcmp(keyword
, "JCLBegin"))
876 ppd
->jcl_begin
= _cupsStrAlloc(string
);
877 ppd_decode(ppd
->jcl_begin
); /* Decode quoted string */
879 else if (!strcmp(keyword
, "JCLEnd"))
881 ppd
->jcl_end
= _cupsStrAlloc(string
);
882 ppd_decode(ppd
->jcl_end
); /* Decode quoted string */
884 else if (!strcmp(keyword
, "JCLToPSInterpreter"))
886 ppd
->jcl_ps
= _cupsStrAlloc(string
);
887 ppd_decode(ppd
->jcl_ps
); /* Decode quoted string */
889 else if (!strcmp(keyword
, "AccurateScreensSupport"))
890 ppd
->accurate_screens
= !strcmp(string
, "True");
891 else if (!strcmp(keyword
, "ColorDevice"))
892 ppd
->color_device
= !strcmp(string
, "True");
893 else if (!strcmp(keyword
, "ContoneOnly"))
894 ppd
->contone_only
= !strcmp(string
, "True");
895 else if (!strcmp(keyword
, "cupsFlipDuplex"))
896 ppd
->flip_duplex
= !strcmp(string
, "True");
897 else if (!strcmp(keyword
, "cupsManualCopies"))
898 ppd
->manual_copies
= !strcmp(string
, "True");
899 else if (!strcmp(keyword
, "cupsModelNumber"))
900 ppd
->model_number
= atoi(string
);
901 else if (!strcmp(keyword
, "cupsColorProfile"))
903 if (ppd
->num_profiles
== 0)
904 profile
= malloc(sizeof(ppd_profile_t
));
906 profile
= realloc(ppd
->profiles
, sizeof(ppd_profile_t
) * (size_t)(ppd
->num_profiles
+ 1));
910 pg
->ppd_status
= PPD_ALLOC_ERROR
;
915 ppd
->profiles
= profile
;
916 profile
+= ppd
->num_profiles
;
917 ppd
->num_profiles
++;
919 memset(profile
, 0, sizeof(ppd_profile_t
));
920 strlcpy(profile
->resolution
, name
, sizeof(profile
->resolution
));
921 strlcpy(profile
->media_type
, text
, sizeof(profile
->media_type
));
923 profile
->density
= (float)_cupsStrScand(string
, &sptr
, loc
);
924 profile
->gamma
= (float)_cupsStrScand(sptr
, &sptr
, loc
);
925 profile
->matrix
[0][0] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
926 profile
->matrix
[0][1] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
927 profile
->matrix
[0][2] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
928 profile
->matrix
[1][0] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
929 profile
->matrix
[1][1] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
930 profile
->matrix
[1][2] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
931 profile
->matrix
[2][0] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
932 profile
->matrix
[2][1] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
933 profile
->matrix
[2][2] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
935 else if (!strcmp(keyword
, "cupsFilter"))
937 if (ppd
->num_filters
== 0)
938 filter
= malloc(sizeof(char *));
940 filter
= realloc(ppd
->filters
, sizeof(char *) * (size_t)(ppd
->num_filters
+ 1));
944 pg
->ppd_status
= PPD_ALLOC_ERROR
;
949 ppd
->filters
= filter
;
950 filter
+= ppd
->num_filters
;
954 * Retain a copy of the filter string...
957 *filter
= _cupsStrRetain(string
);
959 else if (!strcmp(keyword
, "Throughput"))
960 ppd
->throughput
= atoi(string
);
961 else if (!strcmp(keyword
, "Font"))
964 * Add this font to the list of available fonts...
967 if (ppd
->num_fonts
== 0)
968 tempfonts
= (char **)malloc(sizeof(char *));
970 tempfonts
= (char **)realloc(ppd
->fonts
, sizeof(char *) * (size_t)(ppd
->num_fonts
+ 1));
972 if (tempfonts
== NULL
)
974 pg
->ppd_status
= PPD_ALLOC_ERROR
;
979 ppd
->fonts
= tempfonts
;
980 ppd
->fonts
[ppd
->num_fonts
] = _cupsStrAlloc(name
);
983 else if (!strncmp(keyword
, "ParamCustom", 11))
985 ppd_coption_t
*coption
; /* Custom option */
986 ppd_cparam_t
*cparam
; /* Custom parameter */
987 int corder
; /* Order number */
988 char ctype
[33], /* Data type */
989 cminimum
[65], /* Minimum value */
990 cmaximum
[65]; /* Maximum value */
994 * Get the custom option and parameter...
997 if ((coption
= ppd_get_coption(ppd
, keyword
+ 11)) == NULL
)
999 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1004 if ((cparam
= ppd_get_cparam(coption
, name
, text
)) == NULL
)
1006 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1012 * Get the parameter data...
1016 sscanf(string
, "%d%32s%64s%64s", &corder
, ctype
, cminimum
,
1019 pg
->ppd_status
= PPD_BAD_CUSTOM_PARAM
;
1024 cparam
->order
= corder
;
1026 if (!strcmp(ctype
, "curve"))
1028 cparam
->type
= PPD_CUSTOM_CURVE
;
1029 cparam
->minimum
.custom_curve
= (float)_cupsStrScand(cminimum
, NULL
, loc
);
1030 cparam
->maximum
.custom_curve
= (float)_cupsStrScand(cmaximum
, NULL
, loc
);
1032 else if (!strcmp(ctype
, "int"))
1034 cparam
->type
= PPD_CUSTOM_INT
;
1035 cparam
->minimum
.custom_int
= atoi(cminimum
);
1036 cparam
->maximum
.custom_int
= atoi(cmaximum
);
1038 else if (!strcmp(ctype
, "invcurve"))
1040 cparam
->type
= PPD_CUSTOM_INVCURVE
;
1041 cparam
->minimum
.custom_invcurve
= (float)_cupsStrScand(cminimum
, NULL
, loc
);
1042 cparam
->maximum
.custom_invcurve
= (float)_cupsStrScand(cmaximum
, NULL
, loc
);
1044 else if (!strcmp(ctype
, "passcode"))
1046 cparam
->type
= PPD_CUSTOM_PASSCODE
;
1047 cparam
->minimum
.custom_passcode
= atoi(cminimum
);
1048 cparam
->maximum
.custom_passcode
= atoi(cmaximum
);
1050 else if (!strcmp(ctype
, "password"))
1052 cparam
->type
= PPD_CUSTOM_PASSWORD
;
1053 cparam
->minimum
.custom_password
= atoi(cminimum
);
1054 cparam
->maximum
.custom_password
= atoi(cmaximum
);
1056 else if (!strcmp(ctype
, "points"))
1058 cparam
->type
= PPD_CUSTOM_POINTS
;
1059 cparam
->minimum
.custom_points
= (float)_cupsStrScand(cminimum
, NULL
, loc
);
1060 cparam
->maximum
.custom_points
= (float)_cupsStrScand(cmaximum
, NULL
, loc
);
1062 else if (!strcmp(ctype
, "real"))
1064 cparam
->type
= PPD_CUSTOM_REAL
;
1065 cparam
->minimum
.custom_real
= (float)_cupsStrScand(cminimum
, NULL
, loc
);
1066 cparam
->maximum
.custom_real
= (float)_cupsStrScand(cmaximum
, NULL
, loc
);
1068 else if (!strcmp(ctype
, "string"))
1070 cparam
->type
= PPD_CUSTOM_STRING
;
1071 cparam
->minimum
.custom_string
= atoi(cminimum
);
1072 cparam
->maximum
.custom_string
= atoi(cmaximum
);
1076 pg
->ppd_status
= PPD_BAD_CUSTOM_PARAM
;
1082 * Now special-case for CustomPageSize...
1085 if (!strcmp(coption
->keyword
, "PageSize"))
1087 if (!strcmp(name
, "Width"))
1089 ppd
->custom_min
[0] = cparam
->minimum
.custom_points
;
1090 ppd
->custom_max
[0] = cparam
->maximum
.custom_points
;
1092 else if (!strcmp(name
, "Height"))
1094 ppd
->custom_min
[1] = cparam
->minimum
.custom_points
;
1095 ppd
->custom_max
[1] = cparam
->maximum
.custom_points
;
1099 else if (!strcmp(keyword
, "HWMargins"))
1101 for (i
= 0, sptr
= string
; i
< 4; i
++)
1102 ppd
->custom_margins
[i
] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
1104 else if (!strncmp(keyword
, "Custom", 6) && !strcmp(name
, "True") && !option
)
1106 ppd_option_t
*custom_option
; /* Custom option */
1108 DEBUG_puts("2_ppdOpen: Processing Custom option...");
1111 * Get the option and custom option...
1114 if (!ppd_get_coption(ppd
, keyword
+ 6))
1116 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1121 if (option
&& !_cups_strcasecmp(option
->keyword
, keyword
+ 6))
1122 custom_option
= option
;
1124 custom_option
= ppdFindOption(ppd
, keyword
+ 6);
1129 * Add the "custom" option...
1132 if ((choice
= ppdFindChoice(custom_option
, "Custom")) == NULL
)
1133 if ((choice
= ppd_add_choice(custom_option
, "Custom")) == NULL
)
1135 DEBUG_puts("1_ppdOpen: Unable to add Custom choice!");
1137 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1142 strlcpy(choice
->text
, text
[0] ? text
: _("Custom"),
1143 sizeof(choice
->text
));
1145 choice
->code
= _cupsStrAlloc(string
);
1147 if (custom_option
->section
== PPD_ORDER_JCL
)
1148 ppd_decode(choice
->code
);
1152 * Now process custom page sizes specially...
1155 if (!strcmp(keyword
, "CustomPageSize"))
1158 * Add a "Custom" page size entry...
1161 ppd
->variable_sizes
= 1;
1163 ppd_add_size(ppd
, "Custom");
1165 if (option
&& !_cups_strcasecmp(option
->keyword
, "PageRegion"))
1166 custom_option
= option
;
1168 custom_option
= ppdFindOption(ppd
, "PageRegion");
1172 if ((choice
= ppdFindChoice(custom_option
, "Custom")) == NULL
)
1173 if ((choice
= ppd_add_choice(custom_option
, "Custom")) == NULL
)
1175 DEBUG_puts("1_ppdOpen: Unable to add Custom choice!");
1177 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1182 strlcpy(choice
->text
, text
[0] ? text
: _("Custom"),
1183 sizeof(choice
->text
));
1187 else if (!strcmp(keyword
, "LandscapeOrientation"))
1189 if (!strcmp(string
, "Minus90"))
1190 ppd
->landscape
= -90;
1191 else if (!strcmp(string
, "Plus90"))
1192 ppd
->landscape
= 90;
1194 else if (!strcmp(keyword
, "Emulators") && string
)
1196 for (count
= 1, sptr
= string
; sptr
!= NULL
;)
1197 if ((sptr
= strchr(sptr
, ' ')) != NULL
)
1200 while (*sptr
== ' ')
1204 ppd
->num_emulations
= count
;
1205 if ((ppd
->emulations
= calloc((size_t)count
, sizeof(ppd_emul_t
))) == NULL
)
1207 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1212 for (i
= 0, sptr
= string
; i
< count
; i
++)
1214 for (nameptr
= ppd
->emulations
[i
].name
;
1215 *sptr
!= '\0' && *sptr
!= ' ';
1217 if (nameptr
< (ppd
->emulations
[i
].name
+ sizeof(ppd
->emulations
[i
].name
) - 1))
1222 while (*sptr
== ' ')
1226 else if (!strncmp(keyword
, "StartEmulator_", 14))
1230 for (i
= 0; i
< ppd
->num_emulations
; i
++)
1231 if (!strcmp(keyword
+ 14, ppd
->emulations
[i
].name
))
1233 ppd
->emulations
[i
].start
= string
;
1237 else if (!strncmp(keyword
, "StopEmulator_", 13))
1241 for (i
= 0; i
< ppd
->num_emulations
; i
++)
1242 if (!strcmp(keyword
+ 13, ppd
->emulations
[i
].name
))
1244 ppd
->emulations
[i
].stop
= string
;
1248 else if (!strcmp(keyword
, "JobPatchFile"))
1251 * CUPS STR #3421: Check for "*JobPatchFile: int: string"
1254 if (isdigit(*string
& 255))
1256 for (sptr
= string
+ 1; isdigit(*sptr
& 255); sptr
++);
1261 * Found "*JobPatchFile: int: string"...
1264 pg
->ppd_status
= PPD_BAD_VALUE
;
1270 if (!name
[0] && pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1273 * Found "*JobPatchFile: string"...
1276 pg
->ppd_status
= PPD_MISSING_OPTION_KEYWORD
;
1281 if (ppd
->patches
== NULL
)
1282 ppd
->patches
= strdup(string
);
1285 temp
= realloc(ppd
->patches
, strlen(ppd
->patches
) +
1286 strlen(string
) + 1);
1289 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1294 ppd
->patches
= temp
;
1296 memcpy(ppd
->patches
+ strlen(ppd
->patches
), string
, strlen(string
) + 1);
1299 else if (!strcmp(keyword
, "OpenUI"))
1302 * Don't allow nesting of options...
1305 if (option
&& pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1307 pg
->ppd_status
= PPD_NESTED_OPEN_UI
;
1313 * Add an option record to the current sub-group, group, or file...
1316 DEBUG_printf(("2_ppdOpen: name=\"%s\" (%d)", name
, (int)strlen(name
)));
1319 _cups_strcpy(name
, name
+ 1); /* Eliminate leading asterisk */
1321 for (i
= (int)strlen(name
) - 1; i
> 0 && _cups_isspace(name
[i
]); i
--)
1322 name
[i
] = '\0'; /* Eliminate trailing spaces */
1324 DEBUG_printf(("2_ppdOpen: OpenUI of %s in group %s...", name
,
1325 group
? group
->text
: "(null)"));
1327 if (subgroup
!= NULL
)
1328 option
= ppd_get_option(subgroup
, name
);
1329 else if (group
== NULL
)
1331 if ((group
= ppd_get_group(ppd
, "General", _("General"), pg
,
1335 DEBUG_printf(("2_ppdOpen: Adding to group %s...", group
->text
));
1336 option
= ppd_get_option(group
, name
);
1340 option
= ppd_get_option(group
, name
);
1344 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1350 * Now fill in the initial information for the option...
1353 if (string
&& !strcmp(string
, "PickMany"))
1354 option
->ui
= PPD_UI_PICKMANY
;
1355 else if (string
&& !strcmp(string
, "Boolean"))
1356 option
->ui
= PPD_UI_BOOLEAN
;
1357 else if (string
&& !strcmp(string
, "PickOne"))
1358 option
->ui
= PPD_UI_PICKONE
;
1359 else if (pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1361 pg
->ppd_status
= PPD_BAD_OPEN_UI
;
1366 option
->ui
= PPD_UI_PICKONE
;
1368 for (j
= 0; j
< ppd
->num_attrs
; j
++)
1369 if (!strncmp(ppd
->attrs
[j
]->name
, "Default", 7) &&
1370 !strcmp(ppd
->attrs
[j
]->name
+ 7, name
) &&
1371 ppd
->attrs
[j
]->value
)
1373 DEBUG_printf(("2_ppdOpen: Setting Default%s to %s via attribute...",
1374 option
->keyword
, ppd
->attrs
[j
]->value
));
1375 strlcpy(option
->defchoice
, ppd
->attrs
[j
]->value
,
1376 sizeof(option
->defchoice
));
1381 cupsCharsetToUTF8((cups_utf8_t
*)option
->text
, text
,
1382 sizeof(option
->text
), encoding
);
1385 if (!strcmp(name
, "PageSize"))
1386 strlcpy(option
->text
, _("Media Size"), sizeof(option
->text
));
1387 else if (!strcmp(name
, "MediaType"))
1388 strlcpy(option
->text
, _("Media Type"), sizeof(option
->text
));
1389 else if (!strcmp(name
, "InputSlot"))
1390 strlcpy(option
->text
, _("Media Source"), sizeof(option
->text
));
1391 else if (!strcmp(name
, "ColorModel"))
1392 strlcpy(option
->text
, _("Output Mode"), sizeof(option
->text
));
1393 else if (!strcmp(name
, "Resolution"))
1394 strlcpy(option
->text
, _("Resolution"), sizeof(option
->text
));
1396 strlcpy(option
->text
, name
, sizeof(option
->text
));
1399 option
->section
= PPD_ORDER_ANY
;
1401 _cupsStrFree(string
);
1405 * Add a custom option choice if we have already seen a CustomFoo
1409 if (!_cups_strcasecmp(name
, "PageRegion"))
1410 strlcpy(custom_name
, "CustomPageSize", sizeof(custom_name
));
1412 snprintf(custom_name
, sizeof(custom_name
), "Custom%s", name
);
1414 if ((custom_attr
= ppdFindAttr(ppd
, custom_name
, "True")) != NULL
)
1416 if ((choice
= ppdFindChoice(option
, "Custom")) == NULL
)
1417 if ((choice
= ppd_add_choice(option
, "Custom")) == NULL
)
1419 DEBUG_puts("1_ppdOpen: Unable to add Custom choice!");
1421 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1426 strlcpy(choice
->text
,
1427 custom_attr
->text
[0] ? custom_attr
->text
: _("Custom"),
1428 sizeof(choice
->text
));
1429 choice
->code
= _cupsStrRetain(custom_attr
->value
);
1432 else if (!strcmp(keyword
, "JCLOpenUI"))
1435 * Don't allow nesting of options...
1438 if (option
&& pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1440 pg
->ppd_status
= PPD_NESTED_OPEN_UI
;
1446 * Find the JCL group, and add if needed...
1449 group
= ppd_get_group(ppd
, "JCL", _("JCL"), pg
, encoding
);
1455 * Add an option record to the current JCLs...
1459 _cups_strcpy(name
, name
+ 1);
1461 option
= ppd_get_option(group
, name
);
1465 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1471 * Now fill in the initial information for the option...
1474 if (string
&& !strcmp(string
, "PickMany"))
1475 option
->ui
= PPD_UI_PICKMANY
;
1476 else if (string
&& !strcmp(string
, "Boolean"))
1477 option
->ui
= PPD_UI_BOOLEAN
;
1478 else if (string
&& !strcmp(string
, "PickOne"))
1479 option
->ui
= PPD_UI_PICKONE
;
1482 pg
->ppd_status
= PPD_BAD_OPEN_UI
;
1487 for (j
= 0; j
< ppd
->num_attrs
; j
++)
1488 if (!strncmp(ppd
->attrs
[j
]->name
, "Default", 7) &&
1489 !strcmp(ppd
->attrs
[j
]->name
+ 7, name
) &&
1490 ppd
->attrs
[j
]->value
)
1492 DEBUG_printf(("2_ppdOpen: Setting Default%s to %s via attribute...",
1493 option
->keyword
, ppd
->attrs
[j
]->value
));
1494 strlcpy(option
->defchoice
, ppd
->attrs
[j
]->value
,
1495 sizeof(option
->defchoice
));
1500 cupsCharsetToUTF8((cups_utf8_t
*)option
->text
, text
,
1501 sizeof(option
->text
), encoding
);
1503 strlcpy(option
->text
, name
, sizeof(option
->text
));
1505 option
->section
= PPD_ORDER_JCL
;
1508 _cupsStrFree(string
);
1512 * Add a custom option choice if we have already seen a CustomFoo
1516 snprintf(custom_name
, sizeof(custom_name
), "Custom%s", name
);
1518 if ((custom_attr
= ppdFindAttr(ppd
, custom_name
, "True")) != NULL
)
1520 if ((choice
= ppd_add_choice(option
, "Custom")) == NULL
)
1522 DEBUG_puts("1_ppdOpen: Unable to add Custom choice!");
1524 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1529 strlcpy(choice
->text
,
1530 custom_attr
->text
[0] ? custom_attr
->text
: _("Custom"),
1531 sizeof(choice
->text
));
1532 choice
->code
= _cupsStrRetain(custom_attr
->value
);
1535 else if (!strcmp(keyword
, "CloseUI"))
1537 if ((!option
|| option
->section
== PPD_ORDER_JCL
) && pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1539 pg
->ppd_status
= PPD_BAD_CLOSE_UI
;
1546 _cupsStrFree(string
);
1549 else if (!strcmp(keyword
, "JCLCloseUI"))
1551 if ((!option
|| option
->section
!= PPD_ORDER_JCL
) && pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1553 pg
->ppd_status
= PPD_BAD_CLOSE_UI
;
1560 _cupsStrFree(string
);
1563 else if (!strcmp(keyword
, "OpenGroup"))
1566 * Open a new group...
1571 pg
->ppd_status
= PPD_NESTED_OPEN_GROUP
;
1578 pg
->ppd_status
= PPD_BAD_OPEN_GROUP
;
1584 * Separate the group name from the text (name/text)...
1587 if ((sptr
= strchr(string
, '/')) != NULL
)
1593 * Fix up the text...
1599 * Find/add the group...
1602 group
= ppd_get_group(ppd
, string
, sptr
, pg
, encoding
);
1607 _cupsStrFree(string
);
1610 else if (!strcmp(keyword
, "CloseGroup"))
1614 _cupsStrFree(string
);
1617 else if (!strcmp(keyword
, "OrderDependency"))
1619 order
= (float)_cupsStrScand(string
, &sptr
, loc
);
1621 if (!sptr
|| sscanf(sptr
, "%40s%40s", name
, keyword
) != 2)
1623 pg
->ppd_status
= PPD_BAD_ORDER_DEPENDENCY
;
1628 if (keyword
[0] == '*')
1629 _cups_strcpy(keyword
, keyword
+ 1);
1631 if (!strcmp(name
, "ExitServer"))
1632 section
= PPD_ORDER_EXIT
;
1633 else if (!strcmp(name
, "Prolog"))
1634 section
= PPD_ORDER_PROLOG
;
1635 else if (!strcmp(name
, "DocumentSetup"))
1636 section
= PPD_ORDER_DOCUMENT
;
1637 else if (!strcmp(name
, "PageSetup"))
1638 section
= PPD_ORDER_PAGE
;
1639 else if (!strcmp(name
, "JCLSetup"))
1640 section
= PPD_ORDER_JCL
;
1642 section
= PPD_ORDER_ANY
;
1650 * Only valid for Non-UI options...
1653 for (i
= ppd
->num_groups
, gtemp
= ppd
->groups
; i
> 0; i
--, gtemp
++)
1654 if (gtemp
->text
[0] == '\0')
1658 for (i
= 0; i
< gtemp
->num_options
; i
++)
1659 if (!strcmp(keyword
, gtemp
->options
[i
].keyword
))
1661 gtemp
->options
[i
].section
= section
;
1662 gtemp
->options
[i
].order
= order
;
1668 option
->section
= section
;
1669 option
->order
= order
;
1672 _cupsStrFree(string
);
1675 else if (!strncmp(keyword
, "Default", 7))
1681 * Drop UI text, if any, from value...
1684 if (strchr(string
, '/') != NULL
)
1685 *strchr(string
, '/') = '\0';
1688 * Assign the default value as appropriate...
1691 if (!strcmp(keyword
, "DefaultColorSpace"))
1694 * Set default colorspace...
1697 if (!strcmp(string
, "CMY"))
1698 ppd
->colorspace
= PPD_CS_CMY
;
1699 else if (!strcmp(string
, "CMYK"))
1700 ppd
->colorspace
= PPD_CS_CMYK
;
1701 else if (!strcmp(string
, "RGB"))
1702 ppd
->colorspace
= PPD_CS_RGB
;
1703 else if (!strcmp(string
, "RGBK"))
1704 ppd
->colorspace
= PPD_CS_RGBK
;
1705 else if (!strcmp(string
, "N"))
1706 ppd
->colorspace
= PPD_CS_N
;
1708 ppd
->colorspace
= PPD_CS_GRAY
;
1710 else if (option
&& !strcmp(keyword
+ 7, option
->keyword
))
1713 * Set the default as part of the current option...
1716 DEBUG_printf(("2_ppdOpen: Setting %s to %s...", keyword
, string
));
1718 strlcpy(option
->defchoice
, string
, sizeof(option
->defchoice
));
1720 DEBUG_printf(("2_ppdOpen: %s is now %s...", keyword
, option
->defchoice
));
1725 * Lookup option and set if it has been defined...
1728 ppd_option_t
*toption
; /* Temporary option */
1731 if ((toption
= ppdFindOption(ppd
, keyword
+ 7)) != NULL
)
1733 DEBUG_printf(("2_ppdOpen: Setting %s to %s...", keyword
, string
));
1734 strlcpy(toption
->defchoice
, string
, sizeof(toption
->defchoice
));
1738 else if (!strcmp(keyword
, "UIConstraints") ||
1739 !strcmp(keyword
, "NonUIConstraints"))
1743 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1747 if (ppd
->num_consts
== 0)
1748 constraint
= calloc(2, sizeof(ppd_const_t
));
1750 constraint
= realloc(ppd
->consts
, (size_t)(ppd
->num_consts
+ 2) * sizeof(ppd_const_t
));
1752 if (constraint
== NULL
)
1754 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1759 ppd
->consts
= constraint
;
1760 constraint
+= ppd
->num_consts
;
1763 switch (sscanf(string
, "%40s%40s%40s%40s", constraint
->option1
,
1764 constraint
->choice1
, constraint
->option2
,
1765 constraint
->choice2
))
1767 case 0 : /* Error */
1768 case 1 : /* Error */
1769 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1772 case 2 : /* Two options... */
1774 * Check for broken constraints like "* Option"...
1777 if (pg
->ppd_conform
== PPD_CONFORM_STRICT
&&
1778 (!strcmp(constraint
->option1
, "*") ||
1779 !strcmp(constraint
->choice1
, "*")))
1781 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1786 * The following strcpy's are safe, as optionN and
1787 * choiceN are all the same size (size defined by PPD spec...)
1790 if (constraint
->option1
[0] == '*')
1791 _cups_strcpy(constraint
->option1
, constraint
->option1
+ 1);
1792 else if (pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1794 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1798 if (constraint
->choice1
[0] == '*')
1799 _cups_strcpy(constraint
->option2
, constraint
->choice1
+ 1);
1800 else if (pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1802 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1806 constraint
->choice1
[0] = '\0';
1807 constraint
->choice2
[0] = '\0';
1810 case 3 : /* Two options, one choice... */
1812 * Check for broken constraints like "* Option"...
1815 if (pg
->ppd_conform
== PPD_CONFORM_STRICT
&&
1816 (!strcmp(constraint
->option1
, "*") ||
1817 !strcmp(constraint
->choice1
, "*") ||
1818 !strcmp(constraint
->option2
, "*")))
1820 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1825 * The following _cups_strcpy's are safe, as optionN and
1826 * choiceN are all the same size (size defined by PPD spec...)
1829 if (constraint
->option1
[0] == '*')
1830 _cups_strcpy(constraint
->option1
, constraint
->option1
+ 1);
1831 else if (pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1833 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1837 if (constraint
->choice1
[0] == '*')
1839 if (pg
->ppd_conform
== PPD_CONFORM_STRICT
&&
1840 constraint
->option2
[0] == '*')
1842 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1846 _cups_strcpy(constraint
->choice2
, constraint
->option2
);
1847 _cups_strcpy(constraint
->option2
, constraint
->choice1
+ 1);
1848 constraint
->choice1
[0] = '\0';
1852 if (constraint
->option2
[0] == '*')
1853 _cups_strcpy(constraint
->option2
, constraint
->option2
+ 1);
1854 else if (pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1856 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1860 constraint
->choice2
[0] = '\0';
1864 case 4 : /* Two options, two choices... */
1866 * Check for broken constraints like "* Option"...
1869 if (pg
->ppd_conform
== PPD_CONFORM_STRICT
&&
1870 (!strcmp(constraint
->option1
, "*") ||
1871 !strcmp(constraint
->choice1
, "*") ||
1872 !strcmp(constraint
->option2
, "*") ||
1873 !strcmp(constraint
->choice2
, "*")))
1875 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1879 if (constraint
->option1
[0] == '*')
1880 _cups_strcpy(constraint
->option1
, constraint
->option1
+ 1);
1881 else if (pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1883 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1887 if (pg
->ppd_conform
== PPD_CONFORM_STRICT
&&
1888 constraint
->choice1
[0] == '*')
1890 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1894 if (constraint
->option2
[0] == '*')
1895 _cups_strcpy(constraint
->option2
, constraint
->option2
+ 1);
1896 else if (pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1898 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1902 if (pg
->ppd_conform
== PPD_CONFORM_STRICT
&&
1903 constraint
->choice2
[0] == '*')
1905 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1912 * Don't add this one as an attribute...
1915 _cupsStrFree(string
);
1918 else if (!strcmp(keyword
, "PaperDimension"))
1920 if ((size
= ppdPageSize(ppd
, name
)) == NULL
)
1921 size
= ppd_add_size(ppd
, name
);
1926 * Unable to add or find size!
1929 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1934 size
->width
= (float)_cupsStrScand(string
, &sptr
, loc
);
1935 size
->length
= (float)_cupsStrScand(sptr
, NULL
, loc
);
1937 _cupsStrFree(string
);
1940 else if (!strcmp(keyword
, "ImageableArea"))
1942 if ((size
= ppdPageSize(ppd
, name
)) == NULL
)
1943 size
= ppd_add_size(ppd
, name
);
1948 * Unable to add or find size!
1951 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1956 size
->left
= (float)_cupsStrScand(string
, &sptr
, loc
);
1957 size
->bottom
= (float)_cupsStrScand(sptr
, &sptr
, loc
);
1958 size
->right
= (float)_cupsStrScand(sptr
, &sptr
, loc
);
1959 size
->top
= (float)_cupsStrScand(sptr
, NULL
, loc
);
1961 _cupsStrFree(string
);
1964 else if (option
!= NULL
&&
1965 (mask
& (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
)) ==
1966 (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
) &&
1967 !strcmp(keyword
, option
->keyword
))
1969 DEBUG_printf(("2_ppdOpen: group=%p, subgroup=%p", group
, subgroup
));
1971 if (!strcmp(keyword
, "PageSize"))
1974 * Add a page size...
1977 if (ppdPageSize(ppd
, name
) == NULL
)
1978 ppd_add_size(ppd
, name
);
1982 * Add the option choice...
1985 if ((choice
= ppd_add_choice(option
, name
)) == NULL
)
1987 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1993 cupsCharsetToUTF8((cups_utf8_t
*)choice
->text
, text
,
1994 sizeof(choice
->text
), encoding
);
1995 else if (!strcmp(name
, "True"))
1996 strlcpy(choice
->text
, _("Yes"), sizeof(choice
->text
));
1997 else if (!strcmp(name
, "False"))
1998 strlcpy(choice
->text
, _("No"), sizeof(choice
->text
));
2000 strlcpy(choice
->text
, name
, sizeof(choice
->text
));
2002 if (option
->section
== PPD_ORDER_JCL
)
2003 ppd_decode(string
); /* Decode quoted string */
2005 choice
->code
= string
;
2006 string
= NULL
; /* Don't add as an attribute below */
2010 * Add remaining lines with keywords and string values as attributes...
2014 (mask
& (PPD_KEYWORD
| PPD_STRING
)) == (PPD_KEYWORD
| PPD_STRING
))
2015 ppd_add_attr(ppd
, keyword
, name
, text
, string
);
2017 _cupsStrFree(string
);
2021 * Check for a missing CloseUI/JCLCloseUI...
2024 if (option
&& pg
->ppd_conform
== PPD_CONFORM_STRICT
)
2026 pg
->ppd_status
= PPD_MISSING_CLOSE_UI
;
2031 * Check for a missing CloseGroup...
2034 if (group
&& pg
->ppd_conform
== PPD_CONFORM_STRICT
)
2036 pg
->ppd_status
= PPD_MISSING_CLOSE_GROUP
;
2040 ppd_free(line
.buffer
);
2043 * Reset language preferences...
2047 if (!cupsFileEOF(fp
))
2048 DEBUG_printf(("1_ppdOpen: Premature EOF at %lu...\n",
2049 (unsigned long)cupsFileTell(fp
)));
2052 if (pg
->ppd_status
!= PPD_OK
)
2055 * Had an error reading the PPD file, cannot continue!
2064 * Update the filters array as needed...
2067 if (!ppd_update_filters(ppd
, pg
))
2075 * Create the sorted options array and set the option back-pointer for
2076 * each choice and custom option...
2079 ppd
->options
= cupsArrayNew2((cups_array_func_t
)ppd_compare_options
, NULL
,
2080 (cups_ahash_func_t
)ppd_hash_option
,
2083 for (i
= ppd
->num_groups
, group
= ppd
->groups
;
2087 for (j
= group
->num_options
, option
= group
->options
;
2091 ppd_coption_t
*coption
; /* Custom option */
2094 cupsArrayAdd(ppd
->options
, option
);
2096 for (k
= 0; k
< option
->num_choices
; k
++)
2097 option
->choices
[k
].option
= option
;
2099 if ((coption
= ppdFindCustomOption(ppd
, option
->keyword
)) != NULL
)
2100 coption
->option
= option
;
2105 * Create an array to track the marked choices...
2108 ppd
->marked
= cupsArrayNew((cups_array_func_t
)ppd_compare_choices
, NULL
);
2111 * Return the PPD file structure...
2117 * Common exit point for errors to save code size...
2122 _cupsStrFree(string
);
2123 ppd_free(line
.buffer
);
2132 * 'ppdOpen()' - Read a PPD file into memory.
2135 ppd_file_t
* /* O - PPD file record */
2136 ppdOpen(FILE *fp
) /* I - File to read from */
2138 ppd_file_t
*ppd
; /* PPD file record */
2139 cups_file_t
*cf
; /* CUPS file */
2143 * Reopen the stdio file as a CUPS file...
2146 if ((cf
= cupsFileOpenFd(fileno(fp
), "r")) == NULL
)
2150 * Load the PPD file using the newer API...
2153 ppd
= _ppdOpen(cf
, _PPD_LOCALIZATION_DEFAULT
);
2156 * Close the CUPS file and return the PPD...
2166 * 'ppdOpen2()' - Read a PPD file into memory.
2168 * @since CUPS 1.2/macOS 10.5@
2171 ppd_file_t
* /* O - PPD file record or @code NULL@ if the PPD file could not be opened. */
2172 ppdOpen2(cups_file_t
*fp
) /* I - File to read from */
2174 return _ppdOpen(fp
, _PPD_LOCALIZATION_DEFAULT
);
2179 * 'ppdOpenFd()' - Read a PPD file into memory.
2182 ppd_file_t
* /* O - PPD file record or @code NULL@ if the PPD file could not be opened. */
2183 ppdOpenFd(int fd
) /* I - File to read from */
2185 cups_file_t
*fp
; /* CUPS file pointer */
2186 ppd_file_t
*ppd
; /* PPD file record */
2187 _ppd_globals_t
*pg
= _ppdGlobals();
2192 * Set the line number to 0...
2198 * Range check input...
2203 pg
->ppd_status
= PPD_NULL_FILE
;
2209 * Try to open the file and parse it...
2212 if ((fp
= cupsFileOpenFd(fd
, "r")) != NULL
)
2220 pg
->ppd_status
= PPD_FILE_OPEN_ERROR
;
2229 * '_ppdOpenFile()' - Read a PPD file into memory.
2232 ppd_file_t
* /* O - PPD file record or @code NULL@ if the PPD file could not be opened. */
2233 _ppdOpenFile(const char *filename
, /* I - File to read from */
2234 _ppd_localization_t localization
) /* I - Localization to load */
2236 cups_file_t
*fp
; /* File pointer */
2237 ppd_file_t
*ppd
; /* PPD file record */
2238 _ppd_globals_t
*pg
= _ppdGlobals();
2243 * Set the line number to 0...
2249 * Range check input...
2252 if (filename
== NULL
)
2254 pg
->ppd_status
= PPD_NULL_FILE
;
2260 * Try to open the file and parse it...
2263 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
2265 ppd
= _ppdOpen(fp
, localization
);
2271 pg
->ppd_status
= PPD_FILE_OPEN_ERROR
;
2280 * 'ppdOpenFile()' - Read a PPD file into memory.
2283 ppd_file_t
* /* O - PPD file record or @code NULL@ if the PPD file could not be opened. */
2284 ppdOpenFile(const char *filename
) /* I - File to read from */
2286 return _ppdOpenFile(filename
, _PPD_LOCALIZATION_DEFAULT
);
2291 * 'ppdSetConformance()' - Set the conformance level for PPD files.
2293 * @since CUPS 1.1.20/macOS 10.4@
2297 ppdSetConformance(ppd_conform_t c
) /* I - Conformance level */
2299 _ppd_globals_t
*pg
= _ppdGlobals();
2303 pg
->ppd_conform
= c
;
2308 * 'ppd_add_attr()' - Add an attribute to the PPD data.
2311 static ppd_attr_t
* /* O - New attribute */
2312 ppd_add_attr(ppd_file_t
*ppd
, /* I - PPD file data */
2313 const char *name
, /* I - Attribute name */
2314 const char *spec
, /* I - Specifier string, if any */
2315 const char *text
, /* I - Text string, if any */
2316 const char *value
) /* I - Value of attribute */
2318 ppd_attr_t
**ptr
, /* New array */
2319 *temp
; /* New attribute */
2323 * Range check input...
2326 if (ppd
== NULL
|| name
== NULL
|| spec
== NULL
)
2330 * Create the array as needed...
2333 if (!ppd
->sorted_attrs
)
2334 ppd
->sorted_attrs
= cupsArrayNew((cups_array_func_t
)ppd_compare_attrs
,
2338 * Allocate memory for the new attribute...
2341 if (ppd
->num_attrs
== 0)
2342 ptr
= malloc(sizeof(ppd_attr_t
*));
2344 ptr
= realloc(ppd
->attrs
, (size_t)(ppd
->num_attrs
+ 1) * sizeof(ppd_attr_t
*));
2350 ptr
+= ppd
->num_attrs
;
2352 if ((temp
= calloc(1, sizeof(ppd_attr_t
))) == NULL
)
2363 strlcpy(temp
->name
, name
, sizeof(temp
->name
));
2364 strlcpy(temp
->spec
, spec
, sizeof(temp
->spec
));
2365 strlcpy(temp
->text
, text
, sizeof(temp
->text
));
2366 temp
->value
= (char *)value
;
2369 * Add the attribute to the sorted array...
2372 cupsArrayAdd(ppd
->sorted_attrs
, temp
);
2375 * Return the attribute...
2383 * 'ppd_add_choice()' - Add a choice to an option.
2386 static ppd_choice_t
* /* O - Named choice */
2387 ppd_add_choice(ppd_option_t
*option
, /* I - Option */
2388 const char *name
) /* I - Name of choice */
2390 ppd_choice_t
*choice
; /* Choice */
2393 if (option
->num_choices
== 0)
2394 choice
= malloc(sizeof(ppd_choice_t
));
2396 choice
= realloc(option
->choices
, sizeof(ppd_choice_t
) * (size_t)(option
->num_choices
+ 1));
2401 option
->choices
= choice
;
2402 choice
+= option
->num_choices
;
2403 option
->num_choices
++;
2405 memset(choice
, 0, sizeof(ppd_choice_t
));
2406 strlcpy(choice
->choice
, name
, sizeof(choice
->choice
));
2413 * 'ppd_add_size()' - Add a page size.
2416 static ppd_size_t
* /* O - Named size */
2417 ppd_add_size(ppd_file_t
*ppd
, /* I - PPD file */
2418 const char *name
) /* I - Name of size */
2420 ppd_size_t
*size
; /* Size */
2423 if (ppd
->num_sizes
== 0)
2424 size
= malloc(sizeof(ppd_size_t
));
2426 size
= realloc(ppd
->sizes
, sizeof(ppd_size_t
) * (size_t)(ppd
->num_sizes
+ 1));
2432 size
+= ppd
->num_sizes
;
2435 memset(size
, 0, sizeof(ppd_size_t
));
2436 strlcpy(size
->name
, name
, sizeof(size
->name
));
2443 * 'ppd_compare_attrs()' - Compare two attributes.
2446 static int /* O - Result of comparison */
2447 ppd_compare_attrs(ppd_attr_t
*a
, /* I - First attribute */
2448 ppd_attr_t
*b
) /* I - Second attribute */
2450 return (_cups_strcasecmp(a
->name
, b
->name
));
2455 * 'ppd_compare_choices()' - Compare two choices...
2458 static int /* O - Result of comparison */
2459 ppd_compare_choices(ppd_choice_t
*a
, /* I - First choice */
2460 ppd_choice_t
*b
) /* I - Second choice */
2462 return (strcmp(a
->option
->keyword
, b
->option
->keyword
));
2467 * 'ppd_compare_coptions()' - Compare two custom options.
2470 static int /* O - Result of comparison */
2471 ppd_compare_coptions(ppd_coption_t
*a
, /* I - First option */
2472 ppd_coption_t
*b
) /* I - Second option */
2474 return (_cups_strcasecmp(a
->keyword
, b
->keyword
));
2479 * 'ppd_compare_options()' - Compare two options.
2482 static int /* O - Result of comparison */
2483 ppd_compare_options(ppd_option_t
*a
, /* I - First option */
2484 ppd_option_t
*b
) /* I - Second option */
2486 return (_cups_strcasecmp(a
->keyword
, b
->keyword
));
2491 * 'ppd_decode()' - Decode a string value...
2494 static int /* O - Length of decoded string */
2495 ppd_decode(char *string
) /* I - String to decode */
2497 char *inptr
, /* Input pointer */
2498 *outptr
; /* Output pointer */
2504 while (*inptr
!= '\0')
2505 if (*inptr
== '<' && isxdigit(inptr
[1] & 255))
2508 * Convert hex to 8-bit values...
2512 while (isxdigit(*inptr
& 255))
2514 if (_cups_isalpha(*inptr
))
2515 *outptr
= (char)((tolower(*inptr
) - 'a' + 10) << 4);
2517 *outptr
= (char)((*inptr
- '0') << 4);
2521 if (!isxdigit(*inptr
& 255))
2524 if (_cups_isalpha(*inptr
))
2525 *outptr
|= (char)(tolower(*inptr
) - 'a' + 10);
2527 *outptr
|= (char)(*inptr
- '0');
2533 while (*inptr
!= '>' && *inptr
!= '\0')
2535 while (*inptr
== '>')
2539 *outptr
++ = *inptr
++;
2543 return ((int)(outptr
- string
));
2548 * 'ppd_free_filters()' - Free the filters array.
2552 ppd_free_filters(ppd_file_t
*ppd
) /* I - PPD file */
2554 int i
; /* Looping var */
2555 char **filter
; /* Current filter */
2558 if (ppd
->num_filters
> 0)
2560 for (i
= ppd
->num_filters
, filter
= ppd
->filters
; i
> 0; i
--, filter
++)
2561 _cupsStrFree(*filter
);
2563 ppd_free(ppd
->filters
);
2565 ppd
->num_filters
= 0;
2566 ppd
->filters
= NULL
;
2572 * 'ppd_free_group()' - Free a single UI group.
2576 ppd_free_group(ppd_group_t
*group
) /* I - Group to free */
2578 int i
; /* Looping var */
2579 ppd_option_t
*option
; /* Current option */
2580 ppd_group_t
*subgroup
; /* Current sub-group */
2583 if (group
->num_options
> 0)
2585 for (i
= group
->num_options
, option
= group
->options
;
2588 ppd_free_option(option
);
2590 ppd_free(group
->options
);
2593 if (group
->num_subgroups
> 0)
2595 for (i
= group
->num_subgroups
, subgroup
= group
->subgroups
;
2598 ppd_free_group(subgroup
);
2600 ppd_free(group
->subgroups
);
2606 * 'ppd_free_option()' - Free a single option.
2610 ppd_free_option(ppd_option_t
*option
) /* I - Option to free */
2612 int i
; /* Looping var */
2613 ppd_choice_t
*choice
; /* Current choice */
2616 if (option
->num_choices
> 0)
2618 for (i
= option
->num_choices
, choice
= option
->choices
;
2622 _cupsStrFree(choice
->code
);
2625 ppd_free(option
->choices
);
2631 * 'ppd_get_coption()' - Get a custom option record.
2634 static ppd_coption_t
* /* O - Custom option... */
2635 ppd_get_coption(ppd_file_t
*ppd
, /* I - PPD file */
2636 const char *name
) /* I - Name of option */
2638 ppd_coption_t
*copt
; /* New custom option */
2642 * See if the option already exists...
2645 if ((copt
= ppdFindCustomOption(ppd
, name
)) != NULL
)
2649 * Not found, so create the custom option record...
2652 if ((copt
= calloc(1, sizeof(ppd_coption_t
))) == NULL
)
2655 strlcpy(copt
->keyword
, name
, sizeof(copt
->keyword
));
2657 copt
->params
= cupsArrayNew((cups_array_func_t
)NULL
, NULL
);
2659 cupsArrayAdd(ppd
->coptions
, copt
);
2662 * Return the new record...
2670 * 'ppd_get_cparam()' - Get a custom parameter record.
2673 static ppd_cparam_t
* /* O - Extended option... */
2674 ppd_get_cparam(ppd_coption_t
*opt
, /* I - PPD file */
2675 const char *param
, /* I - Name of parameter */
2676 const char *text
) /* I - Human-readable text */
2678 ppd_cparam_t
*cparam
; /* New custom parameter */
2682 * See if the parameter already exists...
2685 if ((cparam
= ppdFindCustomParam(opt
, param
)) != NULL
)
2689 * Not found, so create the custom parameter record...
2692 if ((cparam
= calloc(1, sizeof(ppd_cparam_t
))) == NULL
)
2695 strlcpy(cparam
->name
, param
, sizeof(cparam
->name
));
2696 strlcpy(cparam
->text
, text
[0] ? text
: param
, sizeof(cparam
->text
));
2699 * Add this record to the array...
2702 cupsArrayAdd(opt
->params
, cparam
);
2705 * Return the new record...
2713 * 'ppd_get_group()' - Find or create the named group as needed.
2716 static ppd_group_t
* /* O - Named group */
2717 ppd_get_group(ppd_file_t
*ppd
, /* I - PPD file */
2718 const char *name
, /* I - Name of group */
2719 const char *text
, /* I - Text for group */
2720 _ppd_globals_t
*pg
, /* I - Global data */
2721 cups_encoding_t encoding
) /* I - Encoding of text */
2723 int i
; /* Looping var */
2724 ppd_group_t
*group
; /* Group */
2727 DEBUG_printf(("7ppd_get_group(ppd=%p, name=\"%s\", text=\"%s\", cg=%p)",
2728 ppd
, name
, text
, pg
));
2730 for (i
= ppd
->num_groups
, group
= ppd
->groups
; i
> 0; i
--, group
++)
2731 if (!strcmp(group
->name
, name
))
2736 DEBUG_printf(("8ppd_get_group: Adding group %s...", name
));
2738 if (pg
->ppd_conform
== PPD_CONFORM_STRICT
&& strlen(text
) >= sizeof(group
->text
))
2740 pg
->ppd_status
= PPD_ILLEGAL_TRANSLATION
;
2745 if (ppd
->num_groups
== 0)
2746 group
= malloc(sizeof(ppd_group_t
));
2748 group
= realloc(ppd
->groups
, (size_t)(ppd
->num_groups
+ 1) * sizeof(ppd_group_t
));
2752 pg
->ppd_status
= PPD_ALLOC_ERROR
;
2757 ppd
->groups
= group
;
2758 group
+= ppd
->num_groups
;
2761 memset(group
, 0, sizeof(ppd_group_t
));
2762 strlcpy(group
->name
, name
, sizeof(group
->name
));
2764 cupsCharsetToUTF8((cups_utf8_t
*)group
->text
, text
,
2765 sizeof(group
->text
), encoding
);
2773 * 'ppd_get_option()' - Find or create the named option as needed.
2776 static ppd_option_t
* /* O - Named option */
2777 ppd_get_option(ppd_group_t
*group
, /* I - Group */
2778 const char *name
) /* I - Name of option */
2780 int i
; /* Looping var */
2781 ppd_option_t
*option
; /* Option */
2784 DEBUG_printf(("7ppd_get_option(group=%p(\"%s\"), name=\"%s\")",
2785 group
, group
->name
, name
));
2787 for (i
= group
->num_options
, option
= group
->options
; i
> 0; i
--, option
++)
2788 if (!strcmp(option
->keyword
, name
))
2793 if (group
->num_options
== 0)
2794 option
= malloc(sizeof(ppd_option_t
));
2796 option
= realloc(group
->options
, (size_t)(group
->num_options
+ 1) * sizeof(ppd_option_t
));
2801 group
->options
= option
;
2802 option
+= group
->num_options
;
2803 group
->num_options
++;
2805 memset(option
, 0, sizeof(ppd_option_t
));
2806 strlcpy(option
->keyword
, name
, sizeof(option
->keyword
));
2814 * 'ppd_globals_alloc()' - Allocate and initialize global data.
2817 static _ppd_globals_t
* /* O - Pointer to global data */
2818 ppd_globals_alloc(void)
2820 return ((_ppd_globals_t
*)calloc(1, sizeof(_ppd_globals_t
)));
2825 * 'ppd_globals_free()' - Free global data.
2828 #if defined(HAVE_PTHREAD_H) || defined(_WIN32)
2830 ppd_globals_free(_ppd_globals_t
*pg
) /* I - Pointer to global data */
2834 #endif /* HAVE_PTHREAD_H || _WIN32 */
2837 #ifdef HAVE_PTHREAD_H
2839 * 'ppd_globals_init()' - Initialize per-thread globals...
2843 ppd_globals_init(void)
2846 * Register the global data for this thread...
2849 pthread_key_create(&ppd_globals_key
, (void (*)(void *))ppd_globals_free
);
2851 #endif /* HAVE_PTHREAD_H */
2855 * 'ppd_hash_option()' - Generate a hash of the option name...
2858 static int /* O - Hash index */
2859 ppd_hash_option(ppd_option_t
*option
) /* I - Option */
2861 int hash
= 0; /* Hash index */
2862 const char *k
; /* Pointer into keyword */
2865 for (hash
= option
->keyword
[0], k
= option
->keyword
+ 1; *k
;)
2866 hash
= 33 * hash
+ *k
++;
2868 return (hash
& 511);
2873 * 'ppd_read()' - Read a line from a PPD file, skipping comment lines as
2877 static int /* O - Bitmask of fields read */
2878 ppd_read(cups_file_t
*fp
, /* I - File to read from */
2879 _ppd_line_t
*line
, /* I - Line buffer */
2880 char *keyword
, /* O - Keyword from line */
2881 char *option
, /* O - Option from line */
2882 char *text
, /* O - Human-readable text from line */
2883 char **string
, /* O - Code/string data */
2884 int ignoreblank
, /* I - Ignore blank lines? */
2885 _ppd_globals_t
*pg
) /* I - Global data */
2887 int ch
, /* Character from file */
2888 col
, /* Column in line */
2889 colon
, /* Colon seen? */
2890 endquote
, /* Waiting for an end quote */
2891 mask
, /* Mask to be returned */
2892 startline
, /* Start line */
2893 textlen
; /* Length of text */
2894 char *keyptr
, /* Keyword pointer */
2895 *optptr
, /* Option pointer */
2896 *textptr
, /* Text pointer */
2897 *strptr
, /* Pointer into string */
2898 *lineptr
; /* Current position in line buffer */
2902 * Now loop until we have a valid line...
2907 startline
= pg
->ppd_line
+ 1;
2911 line
->bufsize
= 1024;
2912 line
->buffer
= malloc(1024);
2924 lineptr
= line
->buffer
;
2928 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
2930 if (lineptr
>= (line
->buffer
+ line
->bufsize
- 1))
2933 * Expand the line buffer...
2936 char *temp
; /* Temporary line pointer */
2939 line
->bufsize
+= 1024;
2940 if (line
->bufsize
> 262144)
2943 * Don't allow lines longer than 256k!
2946 pg
->ppd_line
= startline
;
2947 pg
->ppd_status
= PPD_LINE_TOO_LONG
;
2952 temp
= realloc(line
->buffer
, line
->bufsize
);
2955 pg
->ppd_line
= startline
;
2956 pg
->ppd_status
= PPD_LINE_TOO_LONG
;
2961 lineptr
= temp
+ (lineptr
- line
->buffer
);
2962 line
->buffer
= temp
;
2965 if (ch
== '\r' || ch
== '\n')
2968 * Line feed or carriage return...
2977 * Check for a trailing line feed...
2980 if ((ch
= cupsFilePeekChar(fp
)) == EOF
)
2987 cupsFileGetChar(fp
);
2990 if (lineptr
== line
->buffer
&& ignoreblank
)
2991 continue; /* Skip blank lines */
2995 if (!endquote
) /* Continue for multi-line text */
3000 else if (ch
< ' ' && ch
!= '\t' && pg
->ppd_conform
== PPD_CONFORM_STRICT
)
3003 * Other control characters...
3006 pg
->ppd_line
= startline
;
3007 pg
->ppd_status
= PPD_ILLEGAL_CHARACTER
;
3011 else if (ch
!= 0x1a)
3014 * Any other character...
3017 *lineptr
++ = (char)ch
;
3020 if (col
> (PPD_MAX_LINE
- 1))
3023 * Line is too long...
3026 pg
->ppd_line
= startline
;
3027 pg
->ppd_status
= PPD_LINE_TOO_LONG
;
3032 if (ch
== ':' && strncmp(line
->buffer
, "*%", 2) != 0)
3035 if (ch
== '\"' && colon
)
3036 endquote
= !endquote
;
3043 * Didn't finish this quoted string...
3046 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
3049 else if (ch
== '\r' || ch
== '\n')
3057 * Check for a trailing line feed...
3060 if ((ch
= cupsFilePeekChar(fp
)) == EOF
)
3063 cupsFileGetChar(fp
);
3066 else if (ch
< ' ' && ch
!= '\t' && pg
->ppd_conform
== PPD_CONFORM_STRICT
)
3069 * Other control characters...
3072 pg
->ppd_line
= startline
;
3073 pg
->ppd_status
= PPD_ILLEGAL_CHARACTER
;
3077 else if (ch
!= 0x1a)
3081 if (col
> (PPD_MAX_LINE
- 1))
3084 * Line is too long...
3087 pg
->ppd_line
= startline
;
3088 pg
->ppd_status
= PPD_LINE_TOO_LONG
;
3098 * Didn't finish this line...
3101 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
3102 if (ch
== '\r' || ch
== '\n')
3105 * Line feed or carriage return...
3114 * Check for a trailing line feed...
3117 if ((ch
= cupsFilePeekChar(fp
)) == EOF
)
3120 cupsFileGetChar(fp
);
3125 else if (ch
< ' ' && ch
!= '\t' && pg
->ppd_conform
== PPD_CONFORM_STRICT
)
3128 * Other control characters...
3131 pg
->ppd_line
= startline
;
3132 pg
->ppd_status
= PPD_ILLEGAL_CHARACTER
;
3136 else if (ch
!= 0x1a)
3140 if (col
> (PPD_MAX_LINE
- 1))
3143 * Line is too long...
3146 pg
->ppd_line
= startline
;
3147 pg
->ppd_status
= PPD_LINE_TOO_LONG
;
3154 if (lineptr
> line
->buffer
&& lineptr
[-1] == '\n')
3159 DEBUG_printf(("9ppd_read: LINE=\"%s\"", line
->buffer
));
3162 * The dynamically created PPDs for older style macOS
3163 * drivers include a large blob of data inserted as comments
3164 * at the end of the file. As an optimization we can stop
3165 * reading the PPD when we get to the start of this data.
3168 if (!strcmp(line
->buffer
, "*%APLWORKSET START"))
3171 if (ch
== EOF
&& lineptr
== line
->buffer
)
3179 lineptr
= line
->buffer
+ 1;
3186 if ((!line
->buffer
[0] || /* Blank line */
3187 !strncmp(line
->buffer
, "*%", 2) || /* Comment line */
3188 !strcmp(line
->buffer
, "*End")) && /* End of multi-line string */
3189 ignoreblank
) /* Ignore these? */
3191 startline
= pg
->ppd_line
+ 1;
3195 if (!strcmp(line
->buffer
, "*")) /* (Bad) comment line */
3197 if (pg
->ppd_conform
== PPD_CONFORM_RELAXED
)
3199 startline
= pg
->ppd_line
+ 1;
3204 pg
->ppd_line
= startline
;
3205 pg
->ppd_status
= PPD_ILLEGAL_MAIN_KEYWORD
;
3211 if (line
->buffer
[0] != '*') /* All lines start with an asterisk */
3214 * Allow lines consisting of just whitespace...
3217 for (lineptr
= line
->buffer
; *lineptr
; lineptr
++)
3218 if (*lineptr
&& !_cups_isspace(*lineptr
))
3223 pg
->ppd_status
= PPD_MISSING_ASTERISK
;
3226 else if (ignoreblank
)
3238 while (*lineptr
&& *lineptr
!= ':' && !_cups_isspace(*lineptr
))
3240 if (*lineptr
<= ' ' || *lineptr
> 126 || *lineptr
== '/' ||
3241 (keyptr
- keyword
) >= (PPD_MAX_NAME
- 1))
3243 pg
->ppd_status
= PPD_ILLEGAL_MAIN_KEYWORD
;
3247 *keyptr
++ = *lineptr
++;
3252 if (!strcmp(keyword
, "End"))
3255 mask
|= PPD_KEYWORD
;
3257 if (_cups_isspace(*lineptr
))
3260 * Get an option name...
3263 while (_cups_isspace(*lineptr
))
3268 while (*lineptr
&& !_cups_isspace(*lineptr
) && *lineptr
!= ':' &&
3271 if (*lineptr
<= ' ' || *lineptr
> 126 ||
3272 (optptr
- option
) >= (PPD_MAX_NAME
- 1))
3274 pg
->ppd_status
= PPD_ILLEGAL_OPTION_KEYWORD
;
3278 *optptr
++ = *lineptr
++;
3283 if (_cups_isspace(*lineptr
) && pg
->ppd_conform
== PPD_CONFORM_STRICT
)
3285 pg
->ppd_status
= PPD_ILLEGAL_WHITESPACE
;
3289 while (_cups_isspace(*lineptr
))
3294 if (*lineptr
== '/')
3297 * Get human-readable text...
3304 while (*lineptr
!= '\0' && *lineptr
!= '\n' && *lineptr
!= ':')
3306 if (((unsigned char)*lineptr
< ' ' && *lineptr
!= '\t') ||
3307 (textptr
- text
) >= (PPD_MAX_LINE
- 1))
3309 pg
->ppd_status
= PPD_ILLEGAL_TRANSLATION
;
3313 *textptr
++ = *lineptr
++;
3317 textlen
= ppd_decode(text
);
3319 if (textlen
> PPD_MAX_TEXT
&& pg
->ppd_conform
== PPD_CONFORM_STRICT
)
3321 pg
->ppd_status
= PPD_ILLEGAL_TRANSLATION
;
3329 if (_cups_isspace(*lineptr
) && pg
->ppd_conform
== PPD_CONFORM_STRICT
)
3331 pg
->ppd_status
= PPD_ILLEGAL_WHITESPACE
;
3335 while (_cups_isspace(*lineptr
))
3338 if (*lineptr
== ':')
3341 * Get string after triming leading and trailing whitespace...
3345 while (_cups_isspace(*lineptr
))
3348 strptr
= lineptr
+ strlen(lineptr
) - 1;
3349 while (strptr
>= lineptr
&& _cups_isspace(*strptr
))
3352 if (*strptr
== '\"')
3355 * Quoted string by itself, remove quotes...
3362 *string
= _cupsStrAlloc(lineptr
);
3374 * 'ppd_update_filters()' - Update the filters array as needed.
3376 * This function re-populates the filters array with cupsFilter2 entries that
3377 * have been stripped of the destination MIME media types and any maxsize hints.
3379 * (All for backwards-compatibility)
3382 static int /* O - 1 on success, 0 on failure */
3383 ppd_update_filters(ppd_file_t
*ppd
, /* I - PPD file */
3384 _ppd_globals_t
*pg
) /* I - Global data */
3386 ppd_attr_t
*attr
; /* Current cupsFilter2 value */
3387 char srcsuper
[16], /* Source MIME media type */
3389 dstsuper
[16], /* Destination MIME media type */
3391 program
[1024], /* Command to run */
3392 *ptr
, /* Pointer into command to run */
3393 buffer
[1024], /* Re-written cupsFilter value */
3394 **filter
; /* Current filter */
3395 int cost
; /* Cost of filter */
3398 DEBUG_printf(("4ppd_update_filters(ppd=%p, cg=%p)", ppd
, pg
));
3401 * See if we have any cupsFilter2 lines...
3404 if ((attr
= ppdFindAttr(ppd
, "cupsFilter2", NULL
)) == NULL
)
3406 DEBUG_puts("5ppd_update_filters: No cupsFilter2 keywords present.");
3411 * Yes, free the cupsFilter-defined filters and re-build...
3414 ppd_free_filters(ppd
);
3419 * Parse the cupsFilter2 string:
3421 * src/type dst/type cost program
3422 * src/type dst/type cost maxsize(n) program
3425 DEBUG_printf(("5ppd_update_filters: cupsFilter2=\"%s\"", attr
->value
));
3427 if (sscanf(attr
->value
, "%15[^/]/%255s%*[ \t]%15[^/]/%255s%d%*[ \t]%1023[^\n]",
3428 srcsuper
, srctype
, dstsuper
, dsttype
, &cost
, program
) != 6)
3430 DEBUG_puts("5ppd_update_filters: Bad cupsFilter2 line.");
3431 pg
->ppd_status
= PPD_BAD_VALUE
;
3436 DEBUG_printf(("5ppd_update_filters: srcsuper=\"%s\", srctype=\"%s\", "
3437 "dstsuper=\"%s\", dsttype=\"%s\", cost=%d, program=\"%s\"",
3438 srcsuper
, srctype
, dstsuper
, dsttype
, cost
, program
));
3440 if (!strncmp(program
, "maxsize(", 8) &&
3441 (ptr
= strchr(program
+ 8, ')')) != NULL
)
3443 DEBUG_puts("5ppd_update_filters: Found maxsize(nnn).");
3446 while (_cups_isspace(*ptr
))
3449 _cups_strcpy(program
, ptr
);
3450 DEBUG_printf(("5ppd_update_filters: New program=\"%s\"", program
));
3454 * Convert to cupsFilter format:
3456 * src/type cost program
3459 snprintf(buffer
, sizeof(buffer
), "%s/%s %d %s", srcsuper
, srctype
, cost
,
3461 DEBUG_printf(("5ppd_update_filters: Adding \"%s\".", buffer
));
3464 * Add a cupsFilter-compatible string to the filters array.
3467 if (ppd
->num_filters
== 0)
3468 filter
= malloc(sizeof(char *));
3470 filter
= realloc(ppd
->filters
, sizeof(char *) * (size_t)(ppd
->num_filters
+ 1));
3474 DEBUG_puts("5ppd_update_filters: Out of memory.");
3475 pg
->ppd_status
= PPD_ALLOC_ERROR
;
3480 ppd
->filters
= filter
;
3481 filter
+= ppd
->num_filters
;
3482 ppd
->num_filters
++;
3484 *filter
= _cupsStrAlloc(buffer
);
3486 while ((attr
= ppdFindNextAttr(ppd
, "cupsFilter2", NULL
)) != NULL
);
3488 DEBUG_puts("5ppd_update_filters: Completed OK.");