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_KEYWORD 1 /* Line contained a keyword */
27 #define PPD_OPTION 2 /* Line contained an option name */
28 #define PPD_TEXT 4 /* Line contained human-readable text */
29 #define PPD_STRING 8 /* Line contained a string or code */
31 #define PPD_HASHSIZE 512 /* Size of hash */
35 * Line buffer structure...
38 typedef struct _ppd_line_s
40 char *buffer
; /* Pointer to buffer */
41 size_t bufsize
; /* Size of the buffer */
49 static _cups_threadkey_t ppd_globals_key
= _CUPS_THREADKEY_INITIALIZER
;
50 /* Thread local storage key */
52 static pthread_once_t ppd_globals_key_once
= PTHREAD_ONCE_INIT
;
53 /* One-time initialization object */
54 #endif /* HAVE_PTHREAD_H */
61 static ppd_attr_t
*ppd_add_attr(ppd_file_t
*ppd
, const char *name
,
62 const char *spec
, const char *text
,
64 static ppd_choice_t
*ppd_add_choice(ppd_option_t
*option
, const char *name
);
65 static ppd_size_t
*ppd_add_size(ppd_file_t
*ppd
, const char *name
);
66 static int ppd_compare_attrs(ppd_attr_t
*a
, ppd_attr_t
*b
);
67 static int ppd_compare_choices(ppd_choice_t
*a
, ppd_choice_t
*b
);
68 static int ppd_compare_coptions(ppd_coption_t
*a
,
70 static int ppd_compare_options(ppd_option_t
*a
, ppd_option_t
*b
);
71 static int ppd_decode(char *string
);
72 static void ppd_free_filters(ppd_file_t
*ppd
);
73 static void ppd_free_group(ppd_group_t
*group
);
74 static void ppd_free_option(ppd_option_t
*option
);
75 static ppd_coption_t
*ppd_get_coption(ppd_file_t
*ppd
, const char *name
);
76 static ppd_cparam_t
*ppd_get_cparam(ppd_coption_t
*opt
,
79 static ppd_group_t
*ppd_get_group(ppd_file_t
*ppd
, const char *name
,
80 const char *text
, _ppd_globals_t
*pg
,
81 cups_encoding_t encoding
);
82 static ppd_option_t
*ppd_get_option(ppd_group_t
*group
, const char *name
);
83 static _ppd_globals_t
*ppd_globals_alloc(void);
84 #if defined(HAVE_PTHREAD_H) || defined(_WIN32)
85 static void ppd_globals_free(_ppd_globals_t
*g
);
86 #endif /* HAVE_PTHREAD_H || _WIN32 */
88 static void ppd_globals_init(void);
89 #endif /* HAVE_PTHREAD_H */
90 static int ppd_hash_option(ppd_option_t
*option
);
91 static int ppd_read(cups_file_t
*fp
, _ppd_line_t
*line
,
92 char *keyword
, char *option
, char *text
,
93 char **string
, int ignoreblank
,
95 static int ppd_update_filters(ppd_file_t
*ppd
,
100 * 'ppdClose()' - Free all memory used by the PPD file.
104 ppdClose(ppd_file_t
*ppd
) /* I - PPD file record */
106 int i
; /* Looping var */
107 ppd_group_t
*group
; /* Current group */
108 char **font
; /* Current font */
109 ppd_attr_t
**attr
; /* Current attribute */
110 ppd_coption_t
*coption
; /* Current custom option */
111 ppd_cparam_t
*cparam
; /* Current custom parameter */
115 * Range check arguments...
122 * Free all strings at the top level...
125 free(ppd
->lang_encoding
);
128 free(ppd
->jcl_begin
);
133 * Free any UI groups, subgroups, and options...
136 if (ppd
->num_groups
> 0)
138 for (i
= ppd
->num_groups
, group
= ppd
->groups
; i
> 0; i
--, group
++)
139 ppd_free_group(group
);
144 cupsArrayDelete(ppd
->options
);
145 cupsArrayDelete(ppd
->marked
);
148 * Free any page sizes...
151 if (ppd
->num_sizes
> 0)
155 * Free any constraints...
158 if (ppd
->num_consts
> 0)
162 * Free any filters...
165 ppd_free_filters(ppd
);
171 if (ppd
->num_fonts
> 0)
173 for (i
= ppd
->num_fonts
, font
= ppd
->fonts
; i
> 0; i
--, font
++)
180 * Free any profiles...
183 if (ppd
->num_profiles
> 0)
187 * Free any attributes...
190 if (ppd
->num_attrs
> 0)
192 for (i
= ppd
->num_attrs
, attr
= ppd
->attrs
; i
> 0; i
--, attr
++)
194 free((*attr
)->value
);
201 cupsArrayDelete(ppd
->sorted_attrs
);
204 * Free custom options...
207 for (coption
= (ppd_coption_t
*)cupsArrayFirst(ppd
->coptions
);
209 coption
= (ppd_coption_t
*)cupsArrayNext(ppd
->coptions
))
211 for (cparam
= (ppd_cparam_t
*)cupsArrayFirst(coption
->params
);
213 cparam
= (ppd_cparam_t
*)cupsArrayNext(coption
->params
))
215 switch (cparam
->type
)
217 case PPD_CUSTOM_PASSCODE
:
218 case PPD_CUSTOM_PASSWORD
:
219 case PPD_CUSTOM_STRING
:
220 free(cparam
->current
.custom_string
);
230 cupsArrayDelete(coption
->params
);
235 cupsArrayDelete(ppd
->coptions
);
238 * Free constraints...
241 if (ppd
->cups_uiconstraints
)
243 _ppd_cups_uiconsts_t
*consts
; /* Current constraints */
246 for (consts
= (_ppd_cups_uiconsts_t
*)cupsArrayFirst(ppd
->cups_uiconstraints
);
248 consts
= (_ppd_cups_uiconsts_t
*)cupsArrayNext(ppd
->cups_uiconstraints
))
250 free(consts
->constraints
);
254 cupsArrayDelete(ppd
->cups_uiconstraints
);
258 * Free any PPD cache/mapping data...
262 _ppdCacheDestroy(ppd
->cache
);
265 * Free the whole record...
273 * 'ppdErrorString()' - Returns the text associated with a status.
275 * @since CUPS 1.1.19/macOS 10.3@
278 const char * /* O - Status string */
279 ppdErrorString(ppd_status_t status
) /* I - PPD status */
281 static const char * const messages
[] =/* Status messages */
284 _("Unable to open PPD file"),
285 _("NULL PPD file pointer"),
286 _("Memory allocation error"),
287 _("Missing PPD-Adobe-4.x header"),
288 _("Missing value string"),
291 _("OpenGroup without a CloseGroup first"),
292 _("Bad OpenUI/JCLOpenUI"),
293 _("OpenUI/JCLOpenUI without a CloseUI/JCLCloseUI first"),
294 _("Bad OrderDependency"),
295 _("Bad UIConstraints"),
296 _("Missing asterisk in column 1"),
297 _("Line longer than the maximum allowed (255 characters)"),
298 _("Illegal control character"),
299 _("Illegal main keyword string"),
300 _("Illegal option keyword string"),
301 _("Illegal translation string"),
302 _("Illegal whitespace character"),
303 _("Bad custom parameter"),
304 _("Missing option keyword"),
305 _("Bad value string"),
306 _("Missing CloseGroup"),
307 _("Bad CloseUI/JCLCloseUI"),
308 _("Missing CloseUI/JCLCloseUI")
312 if (status
< PPD_OK
|| status
>= PPD_MAX_STATUS
)
313 return (_cupsLangString(cupsLangDefault(), _("Unknown")));
315 return (_cupsLangString(cupsLangDefault(), messages
[status
]));
320 * '_ppdGetEncoding()' - Get the CUPS encoding value for the given
324 cups_encoding_t
/* O - CUPS encoding value */
325 _ppdGetEncoding(const char *name
) /* I - LanguageEncoding string */
327 if (!_cups_strcasecmp(name
, "ISOLatin1"))
328 return (CUPS_ISO8859_1
);
329 else if (!_cups_strcasecmp(name
, "ISOLatin2"))
330 return (CUPS_ISO8859_2
);
331 else if (!_cups_strcasecmp(name
, "ISOLatin5"))
332 return (CUPS_ISO8859_5
);
333 else if (!_cups_strcasecmp(name
, "JIS83-RKSJ"))
334 return (CUPS_JIS_X0213
);
335 else if (!_cups_strcasecmp(name
, "MacStandard"))
336 return (CUPS_MAC_ROMAN
);
337 else if (!_cups_strcasecmp(name
, "WindowsANSI"))
338 return (CUPS_WINDOWS_1252
);
345 * '_ppdGlobals()' - Return a pointer to thread local storage
348 _ppd_globals_t
* /* O - Pointer to global data */
351 _ppd_globals_t
*pg
; /* Pointer to global data */
354 #ifdef HAVE_PTHREAD_H
356 * Initialize the global data exactly once...
359 pthread_once(&ppd_globals_key_once
, ppd_globals_init
);
360 #endif /* HAVE_PTHREAD_H */
363 * See if we have allocated the data yet...
366 if ((pg
= (_ppd_globals_t
*)_cupsThreadGetData(ppd_globals_key
)) == NULL
)
369 * No, allocate memory as set the pointer for the key...
372 if ((pg
= ppd_globals_alloc()) != NULL
)
373 _cupsThreadSetData(ppd_globals_key
, pg
);
377 * Return the pointer to the data...
385 * 'ppdLastError()' - Return the status from the last ppdOpen*().
387 * @since CUPS 1.1.19/macOS 10.3@
390 ppd_status_t
/* O - Status code */
391 ppdLastError(int *line
) /* O - Line number */
393 _ppd_globals_t
*pg
= _ppdGlobals();
398 *line
= pg
->ppd_line
;
400 return (pg
->ppd_status
);
405 * '_ppdOpen()' - Read a PPD file into memory.
407 * @since CUPS 1.2/macOS 10.5@
410 ppd_file_t
* /* O - PPD file record or @code NULL@ if the PPD file could not be opened. */
412 cups_file_t
*fp
, /* I - File to read from */
413 _ppd_localization_t localization
) /* I - Localization to load */
415 int i
, j
, k
; /* Looping vars */
416 _ppd_line_t line
; /* Line buffer */
417 ppd_file_t
*ppd
; /* PPD file record */
418 ppd_group_t
*group
, /* Current group */
419 *subgroup
; /* Current sub-group */
420 ppd_option_t
*option
; /* Current option */
421 ppd_choice_t
*choice
; /* Current choice */
422 ppd_const_t
*constraint
; /* Current constraint */
423 ppd_size_t
*size
; /* Current page size */
424 int mask
; /* Line data mask */
425 char keyword
[PPD_MAX_NAME
],
426 /* Keyword from file */
428 /* Option from file */
430 /* Human-readable text from file */
431 *string
, /* Code/text from file */
432 *sptr
, /* Pointer into string */
433 *temp
, /* Temporary string pointer */
434 **tempfonts
; /* Temporary fonts pointer */
435 float order
; /* Order dependency number */
436 ppd_section_t section
; /* Order dependency section */
437 ppd_profile_t
*profile
; /* Pointer to color profile */
438 char **filter
; /* Pointer to filter */
439 struct lconv
*loc
; /* Locale data */
440 int ui_keyword
; /* Is this line a UI keyword? */
441 cups_lang_t
*lang
; /* Language data */
442 cups_encoding_t encoding
; /* Encoding of PPD file */
443 _ppd_globals_t
*pg
= _ppdGlobals();
445 char custom_name
[PPD_MAX_NAME
];
446 /* CustomFoo attribute name */
447 ppd_attr_t
*custom_attr
; /* CustomFoo attribute */
448 char ll
[7], /* Base language + '.' */
449 ll_CC
[7]; /* Language w/country + '.' */
450 size_t ll_len
= 0, /* Base language length */
451 ll_CC_len
= 0; /* Language w/country length */
452 static const char * const ui_keywords
[] =
454 #ifdef CUPS_USE_FULL_UI_KEYWORDS_LIST
456 * Adobe defines some 41 keywords as "UI", meaning that they are
457 * user interface elements and that they should be treated as such
458 * even if the PPD creator doesn't use Open/CloseUI around them.
460 * Since this can cause previously invisible options to appear and
461 * confuse users, the default is to only treat the PageSize and
462 * PageRegion keywords this way.
464 /* Boolean keywords */
474 /* PickOne keywords */
487 "JCLFrameBufferSize",
508 #else /* !CUPS_USE_FULL_UI_KEYWORDS_LIST */
511 #endif /* CUPS_USE_FULL_UI_KEYWORDS_LIST */
513 static const char * const color_keywords
[] = /* Keywords associated with color profiles */
520 DEBUG_printf(("_ppdOpen(fp=%p)", fp
));
523 * Default to "OK" status...
526 pg
->ppd_status
= PPD_OK
;
530 * Range check input...
535 pg
->ppd_status
= PPD_NULL_FILE
;
540 * If only loading a single localization set up the strings to match...
543 if (localization
== _PPD_LOCALIZATION_DEFAULT
)
545 if ((lang
= cupsLangDefault()) == NULL
)
548 snprintf(ll_CC
, sizeof(ll_CC
), "%s.", lang
->language
);
551 * <rdar://problem/22130168>
552 * <rdar://problem/27245567>
554 * Need to use a different base language for some locales...
557 if (!strcmp(lang
->language
, "zh_HK"))
558 { /* Traditional Chinese + variants */
559 strlcpy(ll_CC
, "zh_TW.", sizeof(ll_CC
));
560 strlcpy(ll
, "zh_", sizeof(ll
));
562 else if (!strncmp(lang
->language
, "zh", 2))
563 strlcpy(ll
, "zh_", sizeof(ll
)); /* Any Chinese variant */
564 else if (!strncmp(lang
->language
, "jp", 2))
565 { /* Any Japanese variant */
566 strlcpy(ll_CC
, "ja", sizeof(ll_CC
));
567 strlcpy(ll
, "jp", sizeof(ll
));
569 else if (!strncmp(lang
->language
, "nb", 2) || !strncmp(lang
->language
, "no", 2))
570 { /* Any Norwegian variant */
571 strlcpy(ll_CC
, "nb", sizeof(ll_CC
));
572 strlcpy(ll
, "no", sizeof(ll
));
575 snprintf(ll
, sizeof(ll
), "%2.2s.", lang
->language
);
577 ll_CC_len
= strlen(ll_CC
);
580 DEBUG_printf(("2_ppdOpen: Loading localizations matching \"%s\" and \"%s\"",
585 * Grab the first line and make sure it reads '*PPD-Adobe: "major.minor"'...
591 mask
= ppd_read(fp
, &line
, keyword
, name
, text
, &string
, 0, pg
);
593 DEBUG_printf(("2_ppdOpen: mask=%x, keyword=\"%s\"...", mask
, keyword
));
596 strcmp(keyword
, "PPD-Adobe") ||
597 string
== NULL
|| string
[0] != '4')
600 * Either this is not a PPD file, or it is not a 4.x PPD file.
603 if (pg
->ppd_status
== PPD_OK
)
604 pg
->ppd_status
= PPD_MISSING_PPDADOBE4
;
612 DEBUG_printf(("2_ppdOpen: keyword=%s, string=%p", keyword
, string
));
615 * Allocate memory for the PPD file record...
618 if ((ppd
= calloc(1, sizeof(ppd_file_t
))) == NULL
)
620 pg
->ppd_status
= PPD_ALLOC_ERROR
;
631 ppd
->language_level
= 2;
632 ppd
->color_device
= 0;
633 ppd
->colorspace
= PPD_CS_N
;
634 ppd
->landscape
= -90;
635 ppd
->coptions
= cupsArrayNew((cups_array_func_t
)ppd_compare_coptions
, NULL
);
638 * Read lines from the PPD file and add them to the file record...
646 encoding
= CUPS_ISO8859_1
;
649 while ((mask
= ppd_read(fp
, &line
, keyword
, name
, text
, &string
, 1, pg
)) != 0)
651 DEBUG_printf(("2_ppdOpen: mask=%x, keyword=\"%s\", name=\"%s\", "
652 "text=\"%s\", string=%d chars...", mask
, keyword
, name
, text
,
653 string
? (int)strlen(string
) : 0));
655 if (strncmp(keyword
, "Default", 7) && !string
&&
656 pg
->ppd_conform
!= PPD_CONFORM_RELAXED
)
659 * Need a string value!
662 pg
->ppd_status
= PPD_MISSING_VALUE
;
670 * Certain main keywords (as defined by the PPD spec) may be used
671 * without the usual OpenUI/CloseUI stuff. Presumably this is just
672 * so that Adobe wouldn't completely break compatibility with PPD
673 * files prior to v4.0 of the spec, but it is hopelessly
674 * inconsistent... Catch these main keywords and automatically
675 * create the corresponding option, as needed...
681 * Previous line was a UI keyword...
689 * If we are filtering out keyword localizations, see if this line needs to
693 if (localization
!= _PPD_LOCALIZATION_ALL
&&
694 (temp
= strchr(keyword
, '.')) != NULL
&&
695 ((temp
- keyword
) == 2 || (temp
- keyword
) == 5) &&
696 _cups_isalpha(keyword
[0]) &&
697 _cups_isalpha(keyword
[1]) &&
698 (keyword
[2] == '.' ||
699 (keyword
[2] == '_' && _cups_isalpha(keyword
[3]) &&
700 _cups_isalpha(keyword
[4]) && keyword
[5] == '.')))
702 if (localization
== _PPD_LOCALIZATION_NONE
||
703 (localization
== _PPD_LOCALIZATION_DEFAULT
&&
704 strncmp(ll_CC
, keyword
, ll_CC_len
) &&
705 strncmp(ll
, keyword
, ll_len
)))
707 DEBUG_printf(("2_ppdOpen: Ignoring localization: \"%s\"\n", keyword
));
712 else if (localization
== _PPD_LOCALIZATION_ICC_PROFILES
)
715 * Only load localizations for the color profile related keywords...
719 i
< (int)(sizeof(color_keywords
) / sizeof(color_keywords
[0]));
722 if (!_cups_strcasecmp(temp
, color_keywords
[i
]))
726 if (i
>= (int)(sizeof(color_keywords
) / sizeof(color_keywords
[0])))
728 DEBUG_printf(("2_ppdOpen: Ignoring localization: \"%s\"\n", keyword
));
736 if (option
== NULL
&&
737 (mask
& (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
)) ==
738 (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
))
740 for (i
= 0; i
< (int)(sizeof(ui_keywords
) / sizeof(ui_keywords
[0])); i
++)
741 if (!strcmp(keyword
, ui_keywords
[i
]))
744 if (i
< (int)(sizeof(ui_keywords
) / sizeof(ui_keywords
[0])))
747 * Create the option in the appropriate group...
752 DEBUG_printf(("2_ppdOpen: FOUND ADOBE UI KEYWORD %s WITHOUT OPENUI!",
757 if ((group
= ppd_get_group(ppd
, "General", _("General"), pg
,
761 DEBUG_printf(("2_ppdOpen: Adding to group %s...", group
->text
));
762 option
= ppd_get_option(group
, keyword
);
766 option
= ppd_get_option(group
, keyword
);
770 pg
->ppd_status
= PPD_ALLOC_ERROR
;
776 * Now fill in the initial information for the option...
779 if (!strncmp(keyword
, "JCL", 3))
780 option
->section
= PPD_ORDER_JCL
;
782 option
->section
= PPD_ORDER_ANY
;
784 option
->order
= 10.0f
;
787 option
->ui
= PPD_UI_BOOLEAN
;
789 option
->ui
= PPD_UI_PICKONE
;
791 for (j
= 0; j
< ppd
->num_attrs
; j
++)
792 if (!strncmp(ppd
->attrs
[j
]->name
, "Default", 7) &&
793 !strcmp(ppd
->attrs
[j
]->name
+ 7, keyword
) &&
794 ppd
->attrs
[j
]->value
)
796 DEBUG_printf(("2_ppdOpen: Setting Default%s to %s via attribute...",
797 option
->keyword
, ppd
->attrs
[j
]->value
));
798 strlcpy(option
->defchoice
, ppd
->attrs
[j
]->value
,
799 sizeof(option
->defchoice
));
803 if (!strcmp(keyword
, "PageSize"))
804 strlcpy(option
->text
, _("Media Size"), sizeof(option
->text
));
805 else if (!strcmp(keyword
, "MediaType"))
806 strlcpy(option
->text
, _("Media Type"), sizeof(option
->text
));
807 else if (!strcmp(keyword
, "InputSlot"))
808 strlcpy(option
->text
, _("Media Source"), sizeof(option
->text
));
809 else if (!strcmp(keyword
, "ColorModel"))
810 strlcpy(option
->text
, _("Output Mode"), sizeof(option
->text
));
811 else if (!strcmp(keyword
, "Resolution"))
812 strlcpy(option
->text
, _("Resolution"), sizeof(option
->text
));
814 strlcpy(option
->text
, keyword
, sizeof(option
->text
));
818 if (!strcmp(keyword
, "LanguageLevel"))
819 ppd
->language_level
= atoi(string
);
820 else if (!strcmp(keyword
, "LanguageEncoding"))
823 * Say all PPD files are UTF-8, since we convert to UTF-8...
826 ppd
->lang_encoding
= strdup("UTF-8");
827 encoding
= _ppdGetEncoding(string
);
829 else if (!strcmp(keyword
, "LanguageVersion"))
830 ppd
->lang_version
= string
;
831 else if (!strcmp(keyword
, "Manufacturer"))
832 ppd
->manufacturer
= string
;
833 else if (!strcmp(keyword
, "ModelName"))
834 ppd
->modelname
= string
;
835 else if (!strcmp(keyword
, "Protocols"))
836 ppd
->protocols
= string
;
837 else if (!strcmp(keyword
, "PCFileName"))
838 ppd
->pcfilename
= string
;
839 else if (!strcmp(keyword
, "NickName"))
841 if (encoding
!= CUPS_UTF8
)
843 cups_utf8_t utf8
[256]; /* UTF-8 version of NickName */
846 cupsCharsetToUTF8(utf8
, string
, sizeof(utf8
), encoding
);
847 ppd
->nickname
= strdup((char *)utf8
);
850 ppd
->nickname
= strdup(string
);
852 else if (!strcmp(keyword
, "Product"))
853 ppd
->product
= string
;
854 else if (!strcmp(keyword
, "ShortNickName"))
855 ppd
->shortnickname
= string
;
856 else if (!strcmp(keyword
, "TTRasterizer"))
857 ppd
->ttrasterizer
= string
;
858 else if (!strcmp(keyword
, "JCLBegin"))
860 ppd
->jcl_begin
= strdup(string
);
861 ppd_decode(ppd
->jcl_begin
); /* Decode quoted string */
863 else if (!strcmp(keyword
, "JCLEnd"))
865 ppd
->jcl_end
= strdup(string
);
866 ppd_decode(ppd
->jcl_end
); /* Decode quoted string */
868 else if (!strcmp(keyword
, "JCLToPSInterpreter"))
870 ppd
->jcl_ps
= strdup(string
);
871 ppd_decode(ppd
->jcl_ps
); /* Decode quoted string */
873 else if (!strcmp(keyword
, "AccurateScreensSupport"))
874 ppd
->accurate_screens
= !strcmp(string
, "True");
875 else if (!strcmp(keyword
, "ColorDevice"))
876 ppd
->color_device
= !strcmp(string
, "True");
877 else if (!strcmp(keyword
, "ContoneOnly"))
878 ppd
->contone_only
= !strcmp(string
, "True");
879 else if (!strcmp(keyword
, "cupsFlipDuplex"))
880 ppd
->flip_duplex
= !strcmp(string
, "True");
881 else if (!strcmp(keyword
, "cupsManualCopies"))
882 ppd
->manual_copies
= !strcmp(string
, "True");
883 else if (!strcmp(keyword
, "cupsModelNumber"))
884 ppd
->model_number
= atoi(string
);
885 else if (!strcmp(keyword
, "cupsColorProfile"))
887 if (ppd
->num_profiles
== 0)
888 profile
= malloc(sizeof(ppd_profile_t
));
890 profile
= realloc(ppd
->profiles
, sizeof(ppd_profile_t
) * (size_t)(ppd
->num_profiles
+ 1));
894 pg
->ppd_status
= PPD_ALLOC_ERROR
;
899 ppd
->profiles
= profile
;
900 profile
+= ppd
->num_profiles
;
901 ppd
->num_profiles
++;
903 memset(profile
, 0, sizeof(ppd_profile_t
));
904 strlcpy(profile
->resolution
, name
, sizeof(profile
->resolution
));
905 strlcpy(profile
->media_type
, text
, sizeof(profile
->media_type
));
907 profile
->density
= (float)_cupsStrScand(string
, &sptr
, loc
);
908 profile
->gamma
= (float)_cupsStrScand(sptr
, &sptr
, loc
);
909 profile
->matrix
[0][0] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
910 profile
->matrix
[0][1] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
911 profile
->matrix
[0][2] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
912 profile
->matrix
[1][0] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
913 profile
->matrix
[1][1] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
914 profile
->matrix
[1][2] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
915 profile
->matrix
[2][0] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
916 profile
->matrix
[2][1] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
917 profile
->matrix
[2][2] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
919 else if (!strcmp(keyword
, "cupsFilter"))
921 if (ppd
->num_filters
== 0)
922 filter
= malloc(sizeof(char *));
924 filter
= realloc(ppd
->filters
, sizeof(char *) * (size_t)(ppd
->num_filters
+ 1));
928 pg
->ppd_status
= PPD_ALLOC_ERROR
;
933 ppd
->filters
= filter
;
934 filter
+= ppd
->num_filters
;
938 * Make a copy of the filter string...
941 *filter
= strdup(string
);
943 else if (!strcmp(keyword
, "Throughput"))
944 ppd
->throughput
= atoi(string
);
945 else if (!strcmp(keyword
, "Font"))
948 * Add this font to the list of available fonts...
951 if (ppd
->num_fonts
== 0)
952 tempfonts
= (char **)malloc(sizeof(char *));
954 tempfonts
= (char **)realloc(ppd
->fonts
, sizeof(char *) * (size_t)(ppd
->num_fonts
+ 1));
956 if (tempfonts
== NULL
)
958 pg
->ppd_status
= PPD_ALLOC_ERROR
;
963 ppd
->fonts
= tempfonts
;
964 ppd
->fonts
[ppd
->num_fonts
] = strdup(name
);
967 else if (!strncmp(keyword
, "ParamCustom", 11))
969 ppd_coption_t
*coption
; /* Custom option */
970 ppd_cparam_t
*cparam
; /* Custom parameter */
971 int corder
; /* Order number */
972 char ctype
[33], /* Data type */
973 cminimum
[65], /* Minimum value */
974 cmaximum
[65]; /* Maximum value */
978 * Get the custom option and parameter...
981 if ((coption
= ppd_get_coption(ppd
, keyword
+ 11)) == NULL
)
983 pg
->ppd_status
= PPD_ALLOC_ERROR
;
988 if ((cparam
= ppd_get_cparam(coption
, name
, text
)) == NULL
)
990 pg
->ppd_status
= PPD_ALLOC_ERROR
;
995 if (cparam
->type
!= PPD_CUSTOM_UNKNOWN
)
997 pg
->ppd_status
= PPD_BAD_CUSTOM_PARAM
;
1003 * Get the parameter data...
1007 sscanf(string
, "%d%32s%64s%64s", &corder
, ctype
, cminimum
,
1010 pg
->ppd_status
= PPD_BAD_CUSTOM_PARAM
;
1015 cparam
->order
= corder
;
1017 if (!strcmp(ctype
, "curve"))
1019 cparam
->type
= PPD_CUSTOM_CURVE
;
1020 cparam
->minimum
.custom_curve
= (float)_cupsStrScand(cminimum
, NULL
, loc
);
1021 cparam
->maximum
.custom_curve
= (float)_cupsStrScand(cmaximum
, NULL
, loc
);
1023 else if (!strcmp(ctype
, "int"))
1025 cparam
->type
= PPD_CUSTOM_INT
;
1026 cparam
->minimum
.custom_int
= atoi(cminimum
);
1027 cparam
->maximum
.custom_int
= atoi(cmaximum
);
1029 else if (!strcmp(ctype
, "invcurve"))
1031 cparam
->type
= PPD_CUSTOM_INVCURVE
;
1032 cparam
->minimum
.custom_invcurve
= (float)_cupsStrScand(cminimum
, NULL
, loc
);
1033 cparam
->maximum
.custom_invcurve
= (float)_cupsStrScand(cmaximum
, NULL
, loc
);
1035 else if (!strcmp(ctype
, "passcode"))
1037 cparam
->type
= PPD_CUSTOM_PASSCODE
;
1038 cparam
->minimum
.custom_passcode
= atoi(cminimum
);
1039 cparam
->maximum
.custom_passcode
= atoi(cmaximum
);
1041 else if (!strcmp(ctype
, "password"))
1043 cparam
->type
= PPD_CUSTOM_PASSWORD
;
1044 cparam
->minimum
.custom_password
= atoi(cminimum
);
1045 cparam
->maximum
.custom_password
= atoi(cmaximum
);
1047 else if (!strcmp(ctype
, "points"))
1049 cparam
->type
= PPD_CUSTOM_POINTS
;
1050 cparam
->minimum
.custom_points
= (float)_cupsStrScand(cminimum
, NULL
, loc
);
1051 cparam
->maximum
.custom_points
= (float)_cupsStrScand(cmaximum
, NULL
, loc
);
1053 else if (!strcmp(ctype
, "real"))
1055 cparam
->type
= PPD_CUSTOM_REAL
;
1056 cparam
->minimum
.custom_real
= (float)_cupsStrScand(cminimum
, NULL
, loc
);
1057 cparam
->maximum
.custom_real
= (float)_cupsStrScand(cmaximum
, NULL
, loc
);
1059 else if (!strcmp(ctype
, "string"))
1061 cparam
->type
= PPD_CUSTOM_STRING
;
1062 cparam
->minimum
.custom_string
= atoi(cminimum
);
1063 cparam
->maximum
.custom_string
= atoi(cmaximum
);
1067 pg
->ppd_status
= PPD_BAD_CUSTOM_PARAM
;
1073 * Now special-case for CustomPageSize...
1076 if (!strcmp(coption
->keyword
, "PageSize"))
1078 if (!strcmp(name
, "Width"))
1080 ppd
->custom_min
[0] = cparam
->minimum
.custom_points
;
1081 ppd
->custom_max
[0] = cparam
->maximum
.custom_points
;
1083 else if (!strcmp(name
, "Height"))
1085 ppd
->custom_min
[1] = cparam
->minimum
.custom_points
;
1086 ppd
->custom_max
[1] = cparam
->maximum
.custom_points
;
1090 else if (!strcmp(keyword
, "HWMargins"))
1092 for (i
= 0, sptr
= string
; i
< 4; i
++)
1093 ppd
->custom_margins
[i
] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
1095 else if (!strncmp(keyword
, "Custom", 6) && !strcmp(name
, "True") && !option
)
1097 ppd_option_t
*custom_option
; /* Custom option */
1099 DEBUG_puts("2_ppdOpen: Processing Custom option...");
1102 * Get the option and custom option...
1105 if (!ppd_get_coption(ppd
, keyword
+ 6))
1107 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1112 if (option
&& !_cups_strcasecmp(option
->keyword
, keyword
+ 6))
1113 custom_option
= option
;
1115 custom_option
= ppdFindOption(ppd
, keyword
+ 6);
1120 * Add the "custom" option...
1123 if ((choice
= ppdFindChoice(custom_option
, "Custom")) == NULL
)
1124 if ((choice
= ppd_add_choice(custom_option
, "Custom")) == NULL
)
1126 DEBUG_puts("1_ppdOpen: Unable to add Custom choice!");
1128 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1133 strlcpy(choice
->text
, text
[0] ? text
: _("Custom"),
1134 sizeof(choice
->text
));
1136 choice
->code
= strdup(string
);
1138 if (custom_option
->section
== PPD_ORDER_JCL
)
1139 ppd_decode(choice
->code
);
1143 * Now process custom page sizes specially...
1146 if (!strcmp(keyword
, "CustomPageSize"))
1149 * Add a "Custom" page size entry...
1152 ppd
->variable_sizes
= 1;
1154 ppd_add_size(ppd
, "Custom");
1156 if (option
&& !_cups_strcasecmp(option
->keyword
, "PageRegion"))
1157 custom_option
= option
;
1159 custom_option
= ppdFindOption(ppd
, "PageRegion");
1163 if ((choice
= ppdFindChoice(custom_option
, "Custom")) == NULL
)
1164 if ((choice
= ppd_add_choice(custom_option
, "Custom")) == NULL
)
1166 DEBUG_puts("1_ppdOpen: Unable to add Custom choice!");
1168 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1173 strlcpy(choice
->text
, text
[0] ? text
: _("Custom"),
1174 sizeof(choice
->text
));
1178 else if (!strcmp(keyword
, "LandscapeOrientation"))
1180 if (!strcmp(string
, "Minus90"))
1181 ppd
->landscape
= -90;
1182 else if (!strcmp(string
, "Plus90"))
1183 ppd
->landscape
= 90;
1185 else if (!strcmp(keyword
, "Emulators") && string
&& ppd
->num_emulations
== 0)
1188 * Issue #5562: Samsung printer drivers incorrectly use Emulators keyword
1189 * to configure themselves
1191 * The Emulators keyword was loaded but never used by anything in CUPS,
1192 * and has no valid purpose in CUPS. The old code was removed due to a
1193 * memory leak (Issue #5475), so the following (new) code supports a single
1194 * name for the Emulators keyword, allowing these drivers to work until we
1195 * remove PPD and driver support entirely in a future version of CUPS.
1198 ppd
->num_emulations
= 1;
1199 ppd
->emulations
= calloc(1, sizeof(ppd_emul_t
));
1201 strlcpy(ppd
->emulations
[0].name
, string
, sizeof(ppd
->emulations
[0].name
));
1203 else if (!strcmp(keyword
, "JobPatchFile"))
1206 * CUPS STR #3421: Check for "*JobPatchFile: int: string"
1209 if (isdigit(*string
& 255))
1211 for (sptr
= string
+ 1; isdigit(*sptr
& 255); sptr
++);
1216 * Found "*JobPatchFile: int: string"...
1219 pg
->ppd_status
= PPD_BAD_VALUE
;
1225 if (!name
[0] && pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1228 * Found "*JobPatchFile: string"...
1231 pg
->ppd_status
= PPD_MISSING_OPTION_KEYWORD
;
1236 if (ppd
->patches
== NULL
)
1237 ppd
->patches
= strdup(string
);
1240 temp
= realloc(ppd
->patches
, strlen(ppd
->patches
) +
1241 strlen(string
) + 1);
1244 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1249 ppd
->patches
= temp
;
1251 memcpy(ppd
->patches
+ strlen(ppd
->patches
), string
, strlen(string
) + 1);
1254 else if (!strcmp(keyword
, "OpenUI"))
1257 * Don't allow nesting of options...
1260 if (option
&& pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1262 pg
->ppd_status
= PPD_NESTED_OPEN_UI
;
1268 * Add an option record to the current sub-group, group, or file...
1271 DEBUG_printf(("2_ppdOpen: name=\"%s\" (%d)", name
, (int)strlen(name
)));
1274 _cups_strcpy(name
, name
+ 1); /* Eliminate leading asterisk */
1276 for (i
= (int)strlen(name
) - 1; i
> 0 && _cups_isspace(name
[i
]); i
--)
1277 name
[i
] = '\0'; /* Eliminate trailing spaces */
1279 DEBUG_printf(("2_ppdOpen: OpenUI of %s in group %s...", name
,
1280 group
? group
->text
: "(null)"));
1282 if (subgroup
!= NULL
)
1283 option
= ppd_get_option(subgroup
, name
);
1284 else if (group
== NULL
)
1286 if ((group
= ppd_get_group(ppd
, "General", _("General"), pg
,
1290 DEBUG_printf(("2_ppdOpen: Adding to group %s...", group
->text
));
1291 option
= ppd_get_option(group
, name
);
1295 option
= ppd_get_option(group
, name
);
1299 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1305 * Now fill in the initial information for the option...
1308 if (string
&& !strcmp(string
, "PickMany"))
1309 option
->ui
= PPD_UI_PICKMANY
;
1310 else if (string
&& !strcmp(string
, "Boolean"))
1311 option
->ui
= PPD_UI_BOOLEAN
;
1312 else if (string
&& !strcmp(string
, "PickOne"))
1313 option
->ui
= PPD_UI_PICKONE
;
1314 else if (pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1316 pg
->ppd_status
= PPD_BAD_OPEN_UI
;
1321 option
->ui
= PPD_UI_PICKONE
;
1323 for (j
= 0; j
< ppd
->num_attrs
; j
++)
1324 if (!strncmp(ppd
->attrs
[j
]->name
, "Default", 7) &&
1325 !strcmp(ppd
->attrs
[j
]->name
+ 7, name
) &&
1326 ppd
->attrs
[j
]->value
)
1328 DEBUG_printf(("2_ppdOpen: Setting Default%s to %s via attribute...",
1329 option
->keyword
, ppd
->attrs
[j
]->value
));
1330 strlcpy(option
->defchoice
, ppd
->attrs
[j
]->value
,
1331 sizeof(option
->defchoice
));
1336 cupsCharsetToUTF8((cups_utf8_t
*)option
->text
, text
,
1337 sizeof(option
->text
), encoding
);
1340 if (!strcmp(name
, "PageSize"))
1341 strlcpy(option
->text
, _("Media Size"), sizeof(option
->text
));
1342 else if (!strcmp(name
, "MediaType"))
1343 strlcpy(option
->text
, _("Media Type"), sizeof(option
->text
));
1344 else if (!strcmp(name
, "InputSlot"))
1345 strlcpy(option
->text
, _("Media Source"), sizeof(option
->text
));
1346 else if (!strcmp(name
, "ColorModel"))
1347 strlcpy(option
->text
, _("Output Mode"), sizeof(option
->text
));
1348 else if (!strcmp(name
, "Resolution"))
1349 strlcpy(option
->text
, _("Resolution"), sizeof(option
->text
));
1351 strlcpy(option
->text
, name
, sizeof(option
->text
));
1354 option
->section
= PPD_ORDER_ANY
;
1360 * Add a custom option choice if we have already seen a CustomFoo
1364 if (!_cups_strcasecmp(name
, "PageRegion"))
1365 strlcpy(custom_name
, "CustomPageSize", sizeof(custom_name
));
1367 snprintf(custom_name
, sizeof(custom_name
), "Custom%s", name
);
1369 if ((custom_attr
= ppdFindAttr(ppd
, custom_name
, "True")) != NULL
)
1371 if ((choice
= ppdFindChoice(option
, "Custom")) == NULL
)
1372 if ((choice
= ppd_add_choice(option
, "Custom")) == NULL
)
1374 DEBUG_puts("1_ppdOpen: Unable to add Custom choice!");
1376 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1381 strlcpy(choice
->text
,
1382 custom_attr
->text
[0] ? custom_attr
->text
: _("Custom"),
1383 sizeof(choice
->text
));
1384 choice
->code
= strdup(custom_attr
->value
);
1387 else if (!strcmp(keyword
, "JCLOpenUI"))
1390 * Don't allow nesting of options...
1393 if (option
&& pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1395 pg
->ppd_status
= PPD_NESTED_OPEN_UI
;
1401 * Find the JCL group, and add if needed...
1404 group
= ppd_get_group(ppd
, "JCL", _("JCL"), pg
, encoding
);
1410 * Add an option record to the current JCLs...
1414 _cups_strcpy(name
, name
+ 1);
1416 option
= ppd_get_option(group
, name
);
1420 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1426 * Now fill in the initial information for the option...
1429 if (string
&& !strcmp(string
, "PickMany"))
1430 option
->ui
= PPD_UI_PICKMANY
;
1431 else if (string
&& !strcmp(string
, "Boolean"))
1432 option
->ui
= PPD_UI_BOOLEAN
;
1433 else if (string
&& !strcmp(string
, "PickOne"))
1434 option
->ui
= PPD_UI_PICKONE
;
1437 pg
->ppd_status
= PPD_BAD_OPEN_UI
;
1442 for (j
= 0; j
< ppd
->num_attrs
; j
++)
1443 if (!strncmp(ppd
->attrs
[j
]->name
, "Default", 7) &&
1444 !strcmp(ppd
->attrs
[j
]->name
+ 7, name
) &&
1445 ppd
->attrs
[j
]->value
)
1447 DEBUG_printf(("2_ppdOpen: Setting Default%s to %s via attribute...",
1448 option
->keyword
, ppd
->attrs
[j
]->value
));
1449 strlcpy(option
->defchoice
, ppd
->attrs
[j
]->value
,
1450 sizeof(option
->defchoice
));
1455 cupsCharsetToUTF8((cups_utf8_t
*)option
->text
, text
,
1456 sizeof(option
->text
), encoding
);
1458 strlcpy(option
->text
, name
, sizeof(option
->text
));
1460 option
->section
= PPD_ORDER_JCL
;
1467 * Add a custom option choice if we have already seen a CustomFoo
1471 snprintf(custom_name
, sizeof(custom_name
), "Custom%s", name
);
1473 if ((custom_attr
= ppdFindAttr(ppd
, custom_name
, "True")) != NULL
)
1475 if ((choice
= ppd_add_choice(option
, "Custom")) == NULL
)
1477 DEBUG_puts("1_ppdOpen: Unable to add Custom choice!");
1479 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1484 strlcpy(choice
->text
,
1485 custom_attr
->text
[0] ? custom_attr
->text
: _("Custom"),
1486 sizeof(choice
->text
));
1487 choice
->code
= strdup(custom_attr
->value
);
1490 else if (!strcmp(keyword
, "CloseUI"))
1492 if ((!option
|| option
->section
== PPD_ORDER_JCL
) && pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1494 pg
->ppd_status
= PPD_BAD_CLOSE_UI
;
1504 else if (!strcmp(keyword
, "JCLCloseUI"))
1506 if ((!option
|| option
->section
!= PPD_ORDER_JCL
) && pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1508 pg
->ppd_status
= PPD_BAD_CLOSE_UI
;
1518 else if (!strcmp(keyword
, "OpenGroup"))
1521 * Open a new group...
1526 pg
->ppd_status
= PPD_NESTED_OPEN_GROUP
;
1533 pg
->ppd_status
= PPD_BAD_OPEN_GROUP
;
1539 * Separate the group name from the text (name/text)...
1542 if ((sptr
= strchr(string
, '/')) != NULL
)
1548 * Fix up the text...
1554 * Find/add the group...
1557 group
= ppd_get_group(ppd
, string
, sptr
, pg
, encoding
);
1565 else if (!strcmp(keyword
, "CloseGroup"))
1572 else if (!strcmp(keyword
, "OrderDependency"))
1574 order
= (float)_cupsStrScand(string
, &sptr
, loc
);
1576 if (!sptr
|| sscanf(sptr
, "%40s%40s", name
, keyword
) != 2)
1578 pg
->ppd_status
= PPD_BAD_ORDER_DEPENDENCY
;
1583 if (keyword
[0] == '*')
1584 _cups_strcpy(keyword
, keyword
+ 1);
1586 if (!strcmp(name
, "ExitServer"))
1587 section
= PPD_ORDER_EXIT
;
1588 else if (!strcmp(name
, "Prolog"))
1589 section
= PPD_ORDER_PROLOG
;
1590 else if (!strcmp(name
, "DocumentSetup"))
1591 section
= PPD_ORDER_DOCUMENT
;
1592 else if (!strcmp(name
, "PageSetup"))
1593 section
= PPD_ORDER_PAGE
;
1594 else if (!strcmp(name
, "JCLSetup"))
1595 section
= PPD_ORDER_JCL
;
1597 section
= PPD_ORDER_ANY
;
1605 * Only valid for Non-UI options...
1608 for (i
= ppd
->num_groups
, gtemp
= ppd
->groups
; i
> 0; i
--, gtemp
++)
1609 if (gtemp
->text
[0] == '\0')
1613 for (i
= 0; i
< gtemp
->num_options
; i
++)
1614 if (!strcmp(keyword
, gtemp
->options
[i
].keyword
))
1616 gtemp
->options
[i
].section
= section
;
1617 gtemp
->options
[i
].order
= order
;
1623 option
->section
= section
;
1624 option
->order
= order
;
1630 else if (!strncmp(keyword
, "Default", 7))
1636 * Drop UI text, if any, from value...
1639 if (strchr(string
, '/') != NULL
)
1640 *strchr(string
, '/') = '\0';
1643 * Assign the default value as appropriate...
1646 if (!strcmp(keyword
, "DefaultColorSpace"))
1649 * Set default colorspace...
1652 if (!strcmp(string
, "CMY"))
1653 ppd
->colorspace
= PPD_CS_CMY
;
1654 else if (!strcmp(string
, "CMYK"))
1655 ppd
->colorspace
= PPD_CS_CMYK
;
1656 else if (!strcmp(string
, "RGB"))
1657 ppd
->colorspace
= PPD_CS_RGB
;
1658 else if (!strcmp(string
, "RGBK"))
1659 ppd
->colorspace
= PPD_CS_RGBK
;
1660 else if (!strcmp(string
, "N"))
1661 ppd
->colorspace
= PPD_CS_N
;
1663 ppd
->colorspace
= PPD_CS_GRAY
;
1665 else if (option
&& !strcmp(keyword
+ 7, option
->keyword
))
1668 * Set the default as part of the current option...
1671 DEBUG_printf(("2_ppdOpen: Setting %s to %s...", keyword
, string
));
1673 strlcpy(option
->defchoice
, string
, sizeof(option
->defchoice
));
1675 DEBUG_printf(("2_ppdOpen: %s is now %s...", keyword
, option
->defchoice
));
1680 * Lookup option and set if it has been defined...
1683 ppd_option_t
*toption
; /* Temporary option */
1686 if ((toption
= ppdFindOption(ppd
, keyword
+ 7)) != NULL
)
1688 DEBUG_printf(("2_ppdOpen: Setting %s to %s...", keyword
, string
));
1689 strlcpy(toption
->defchoice
, string
, sizeof(toption
->defchoice
));
1693 else if (!strcmp(keyword
, "UIConstraints") ||
1694 !strcmp(keyword
, "NonUIConstraints"))
1698 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1702 if (ppd
->num_consts
== 0)
1703 constraint
= calloc(2, sizeof(ppd_const_t
));
1705 constraint
= realloc(ppd
->consts
, (size_t)(ppd
->num_consts
+ 2) * sizeof(ppd_const_t
));
1707 if (constraint
== NULL
)
1709 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1714 ppd
->consts
= constraint
;
1715 constraint
+= ppd
->num_consts
;
1718 switch (sscanf(string
, "%40s%40s%40s%40s", constraint
->option1
,
1719 constraint
->choice1
, constraint
->option2
,
1720 constraint
->choice2
))
1722 case 0 : /* Error */
1723 case 1 : /* Error */
1724 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1727 case 2 : /* Two options... */
1729 * Check for broken constraints like "* Option"...
1732 if (pg
->ppd_conform
== PPD_CONFORM_STRICT
&&
1733 (!strcmp(constraint
->option1
, "*") ||
1734 !strcmp(constraint
->choice1
, "*")))
1736 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1741 * The following strcpy's are safe, as optionN and
1742 * choiceN are all the same size (size defined by PPD spec...)
1745 if (constraint
->option1
[0] == '*')
1746 _cups_strcpy(constraint
->option1
, constraint
->option1
+ 1);
1747 else if (pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1749 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1753 if (constraint
->choice1
[0] == '*')
1754 _cups_strcpy(constraint
->option2
, constraint
->choice1
+ 1);
1755 else if (pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1757 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1761 constraint
->choice1
[0] = '\0';
1762 constraint
->choice2
[0] = '\0';
1765 case 3 : /* Two options, one choice... */
1767 * Check for broken constraints like "* Option"...
1770 if (pg
->ppd_conform
== PPD_CONFORM_STRICT
&&
1771 (!strcmp(constraint
->option1
, "*") ||
1772 !strcmp(constraint
->choice1
, "*") ||
1773 !strcmp(constraint
->option2
, "*")))
1775 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1780 * The following _cups_strcpy's are safe, as optionN and
1781 * choiceN are all the same size (size defined by PPD spec...)
1784 if (constraint
->option1
[0] == '*')
1785 _cups_strcpy(constraint
->option1
, constraint
->option1
+ 1);
1786 else if (pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1788 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1792 if (constraint
->choice1
[0] == '*')
1794 if (pg
->ppd_conform
== PPD_CONFORM_STRICT
&&
1795 constraint
->option2
[0] == '*')
1797 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1801 _cups_strcpy(constraint
->choice2
, constraint
->option2
);
1802 _cups_strcpy(constraint
->option2
, constraint
->choice1
+ 1);
1803 constraint
->choice1
[0] = '\0';
1807 if (constraint
->option2
[0] == '*')
1808 _cups_strcpy(constraint
->option2
, constraint
->option2
+ 1);
1809 else if (pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1811 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1815 constraint
->choice2
[0] = '\0';
1819 case 4 : /* Two options, two choices... */
1821 * Check for broken constraints like "* Option"...
1824 if (pg
->ppd_conform
== PPD_CONFORM_STRICT
&&
1825 (!strcmp(constraint
->option1
, "*") ||
1826 !strcmp(constraint
->choice1
, "*") ||
1827 !strcmp(constraint
->option2
, "*") ||
1828 !strcmp(constraint
->choice2
, "*")))
1830 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1834 if (constraint
->option1
[0] == '*')
1835 _cups_strcpy(constraint
->option1
, constraint
->option1
+ 1);
1836 else if (pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1838 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1842 if (pg
->ppd_conform
== PPD_CONFORM_STRICT
&&
1843 constraint
->choice1
[0] == '*')
1845 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1849 if (constraint
->option2
[0] == '*')
1850 _cups_strcpy(constraint
->option2
, constraint
->option2
+ 1);
1851 else if (pg
->ppd_conform
== PPD_CONFORM_STRICT
)
1853 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1857 if (pg
->ppd_conform
== PPD_CONFORM_STRICT
&&
1858 constraint
->choice2
[0] == '*')
1860 pg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1867 * Don't add this one as an attribute...
1873 else if (!strcmp(keyword
, "PaperDimension"))
1875 if (!_cups_strcasecmp(name
, "custom") || !_cups_strncasecmp(name
, "custom.", 7))
1877 pg
->ppd_status
= PPD_ILLEGAL_OPTION_KEYWORD
;
1882 if ((size
= ppdPageSize(ppd
, name
)) == NULL
)
1883 size
= ppd_add_size(ppd
, name
);
1888 * Unable to add or find size!
1891 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1896 size
->width
= (float)_cupsStrScand(string
, &sptr
, loc
);
1897 size
->length
= (float)_cupsStrScand(sptr
, NULL
, loc
);
1902 else if (!strcmp(keyword
, "ImageableArea"))
1904 if (!_cups_strcasecmp(name
, "custom") || !_cups_strncasecmp(name
, "custom.", 7))
1906 pg
->ppd_status
= PPD_ILLEGAL_OPTION_KEYWORD
;
1911 if ((size
= ppdPageSize(ppd
, name
)) == NULL
)
1912 size
= ppd_add_size(ppd
, name
);
1917 * Unable to add or find size!
1920 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1925 size
->left
= (float)_cupsStrScand(string
, &sptr
, loc
);
1926 size
->bottom
= (float)_cupsStrScand(sptr
, &sptr
, loc
);
1927 size
->right
= (float)_cupsStrScand(sptr
, &sptr
, loc
);
1928 size
->top
= (float)_cupsStrScand(sptr
, NULL
, loc
);
1933 else if (option
!= NULL
&&
1934 (mask
& (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
)) ==
1935 (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
) &&
1936 !strcmp(keyword
, option
->keyword
))
1938 DEBUG_printf(("2_ppdOpen: group=%p, subgroup=%p", group
, subgroup
));
1940 if (!_cups_strcasecmp(name
, "custom") || !_cups_strncasecmp(name
, "custom.", 7))
1942 pg
->ppd_status
= PPD_ILLEGAL_OPTION_KEYWORD
;
1947 if (!strcmp(keyword
, "PageSize"))
1950 * Add a page size...
1953 if (ppdPageSize(ppd
, name
) == NULL
)
1954 ppd_add_size(ppd
, name
);
1958 * Add the option choice...
1961 if ((choice
= ppd_add_choice(option
, name
)) == NULL
)
1963 pg
->ppd_status
= PPD_ALLOC_ERROR
;
1969 cupsCharsetToUTF8((cups_utf8_t
*)choice
->text
, text
,
1970 sizeof(choice
->text
), encoding
);
1971 else if (!strcmp(name
, "True"))
1972 strlcpy(choice
->text
, _("Yes"), sizeof(choice
->text
));
1973 else if (!strcmp(name
, "False"))
1974 strlcpy(choice
->text
, _("No"), sizeof(choice
->text
));
1976 strlcpy(choice
->text
, name
, sizeof(choice
->text
));
1978 if (option
->section
== PPD_ORDER_JCL
)
1979 ppd_decode(string
); /* Decode quoted string */
1981 choice
->code
= string
;
1982 string
= NULL
; /* Don't add as an attribute below */
1986 * Add remaining lines with keywords and string values as attributes...
1990 (mask
& (PPD_KEYWORD
| PPD_STRING
)) == (PPD_KEYWORD
| PPD_STRING
))
1991 ppd_add_attr(ppd
, keyword
, name
, text
, string
);
1997 * Check for a missing CloseUI/JCLCloseUI...
2000 if (option
&& pg
->ppd_conform
== PPD_CONFORM_STRICT
)
2002 pg
->ppd_status
= PPD_MISSING_CLOSE_UI
;
2007 * Check for a missing CloseGroup...
2010 if (group
&& pg
->ppd_conform
== PPD_CONFORM_STRICT
)
2012 pg
->ppd_status
= PPD_MISSING_CLOSE_GROUP
;
2019 * Reset language preferences...
2023 if (!cupsFileEOF(fp
))
2024 DEBUG_printf(("1_ppdOpen: Premature EOF at %lu...\n",
2025 (unsigned long)cupsFileTell(fp
)));
2028 if (pg
->ppd_status
!= PPD_OK
)
2031 * Had an error reading the PPD file, cannot continue!
2040 * Update the filters array as needed...
2043 if (!ppd_update_filters(ppd
, pg
))
2051 * Create the sorted options array and set the option back-pointer for
2052 * each choice and custom option...
2055 ppd
->options
= cupsArrayNew2((cups_array_func_t
)ppd_compare_options
, NULL
,
2056 (cups_ahash_func_t
)ppd_hash_option
,
2059 for (i
= ppd
->num_groups
, group
= ppd
->groups
;
2063 for (j
= group
->num_options
, option
= group
->options
;
2067 ppd_coption_t
*coption
; /* Custom option */
2070 cupsArrayAdd(ppd
->options
, option
);
2072 for (k
= 0; k
< option
->num_choices
; k
++)
2073 option
->choices
[k
].option
= option
;
2075 if ((coption
= ppdFindCustomOption(ppd
, option
->keyword
)) != NULL
)
2076 coption
->option
= option
;
2081 * Create an array to track the marked choices...
2084 ppd
->marked
= cupsArrayNew((cups_array_func_t
)ppd_compare_choices
, NULL
);
2087 * Return the PPD file structure...
2093 * Common exit point for errors to save code size...
2108 * 'ppdOpen()' - Read a PPD file into memory.
2111 ppd_file_t
* /* O - PPD file record */
2112 ppdOpen(FILE *fp
) /* I - File to read from */
2114 ppd_file_t
*ppd
; /* PPD file record */
2115 cups_file_t
*cf
; /* CUPS file */
2119 * Reopen the stdio file as a CUPS file...
2122 if ((cf
= cupsFileOpenFd(fileno(fp
), "r")) == NULL
)
2126 * Load the PPD file using the newer API...
2129 ppd
= _ppdOpen(cf
, _PPD_LOCALIZATION_DEFAULT
);
2132 * Close the CUPS file and return the PPD...
2142 * 'ppdOpen2()' - Read a PPD file into memory.
2144 * @since CUPS 1.2/macOS 10.5@
2147 ppd_file_t
* /* O - PPD file record or @code NULL@ if the PPD file could not be opened. */
2148 ppdOpen2(cups_file_t
*fp
) /* I - File to read from */
2150 return _ppdOpen(fp
, _PPD_LOCALIZATION_DEFAULT
);
2155 * 'ppdOpenFd()' - Read a PPD file into memory.
2158 ppd_file_t
* /* O - PPD file record or @code NULL@ if the PPD file could not be opened. */
2159 ppdOpenFd(int fd
) /* I - File to read from */
2161 cups_file_t
*fp
; /* CUPS file pointer */
2162 ppd_file_t
*ppd
; /* PPD file record */
2163 _ppd_globals_t
*pg
= _ppdGlobals();
2168 * Set the line number to 0...
2174 * Range check input...
2179 pg
->ppd_status
= PPD_NULL_FILE
;
2185 * Try to open the file and parse it...
2188 if ((fp
= cupsFileOpenFd(fd
, "r")) != NULL
)
2196 pg
->ppd_status
= PPD_FILE_OPEN_ERROR
;
2205 * '_ppdOpenFile()' - Read a PPD file into memory.
2208 ppd_file_t
* /* O - PPD file record or @code NULL@ if the PPD file could not be opened. */
2209 _ppdOpenFile(const char *filename
, /* I - File to read from */
2210 _ppd_localization_t localization
) /* I - Localization to load */
2212 cups_file_t
*fp
; /* File pointer */
2213 ppd_file_t
*ppd
; /* PPD file record */
2214 _ppd_globals_t
*pg
= _ppdGlobals();
2219 * Set the line number to 0...
2225 * Range check input...
2228 if (filename
== NULL
)
2230 pg
->ppd_status
= PPD_NULL_FILE
;
2236 * Try to open the file and parse it...
2239 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
2241 ppd
= _ppdOpen(fp
, localization
);
2247 pg
->ppd_status
= PPD_FILE_OPEN_ERROR
;
2256 * 'ppdOpenFile()' - Read a PPD file into memory.
2259 ppd_file_t
* /* O - PPD file record or @code NULL@ if the PPD file could not be opened. */
2260 ppdOpenFile(const char *filename
) /* I - File to read from */
2262 return _ppdOpenFile(filename
, _PPD_LOCALIZATION_DEFAULT
);
2267 * 'ppdSetConformance()' - Set the conformance level for PPD files.
2269 * @since CUPS 1.1.20/macOS 10.4@
2273 ppdSetConformance(ppd_conform_t c
) /* I - Conformance level */
2275 _ppd_globals_t
*pg
= _ppdGlobals();
2279 pg
->ppd_conform
= c
;
2284 * 'ppd_add_attr()' - Add an attribute to the PPD data.
2287 static ppd_attr_t
* /* O - New attribute */
2288 ppd_add_attr(ppd_file_t
*ppd
, /* I - PPD file data */
2289 const char *name
, /* I - Attribute name */
2290 const char *spec
, /* I - Specifier string, if any */
2291 const char *text
, /* I - Text string, if any */
2292 const char *value
) /* I - Value of attribute */
2294 ppd_attr_t
**ptr
, /* New array */
2295 *temp
; /* New attribute */
2299 * Range check input...
2302 if (ppd
== NULL
|| name
== NULL
|| spec
== NULL
)
2306 * Create the array as needed...
2309 if (!ppd
->sorted_attrs
)
2310 ppd
->sorted_attrs
= cupsArrayNew((cups_array_func_t
)ppd_compare_attrs
,
2314 * Allocate memory for the new attribute...
2317 if (ppd
->num_attrs
== 0)
2318 ptr
= malloc(sizeof(ppd_attr_t
*));
2320 ptr
= realloc(ppd
->attrs
, (size_t)(ppd
->num_attrs
+ 1) * sizeof(ppd_attr_t
*));
2326 ptr
+= ppd
->num_attrs
;
2328 if ((temp
= calloc(1, sizeof(ppd_attr_t
))) == NULL
)
2339 strlcpy(temp
->name
, name
, sizeof(temp
->name
));
2340 strlcpy(temp
->spec
, spec
, sizeof(temp
->spec
));
2341 strlcpy(temp
->text
, text
, sizeof(temp
->text
));
2342 temp
->value
= (char *)value
;
2345 * Add the attribute to the sorted array...
2348 cupsArrayAdd(ppd
->sorted_attrs
, temp
);
2351 * Return the attribute...
2359 * 'ppd_add_choice()' - Add a choice to an option.
2362 static ppd_choice_t
* /* O - Named choice */
2363 ppd_add_choice(ppd_option_t
*option
, /* I - Option */
2364 const char *name
) /* I - Name of choice */
2366 ppd_choice_t
*choice
; /* Choice */
2369 if (option
->num_choices
== 0)
2370 choice
= malloc(sizeof(ppd_choice_t
));
2372 choice
= realloc(option
->choices
, sizeof(ppd_choice_t
) * (size_t)(option
->num_choices
+ 1));
2377 option
->choices
= choice
;
2378 choice
+= option
->num_choices
;
2379 option
->num_choices
++;
2381 memset(choice
, 0, sizeof(ppd_choice_t
));
2382 strlcpy(choice
->choice
, name
, sizeof(choice
->choice
));
2389 * 'ppd_add_size()' - Add a page size.
2392 static ppd_size_t
* /* O - Named size */
2393 ppd_add_size(ppd_file_t
*ppd
, /* I - PPD file */
2394 const char *name
) /* I - Name of size */
2396 ppd_size_t
*size
; /* Size */
2399 if (ppd
->num_sizes
== 0)
2400 size
= malloc(sizeof(ppd_size_t
));
2402 size
= realloc(ppd
->sizes
, sizeof(ppd_size_t
) * (size_t)(ppd
->num_sizes
+ 1));
2408 size
+= ppd
->num_sizes
;
2411 memset(size
, 0, sizeof(ppd_size_t
));
2412 strlcpy(size
->name
, name
, sizeof(size
->name
));
2419 * 'ppd_compare_attrs()' - Compare two attributes.
2422 static int /* O - Result of comparison */
2423 ppd_compare_attrs(ppd_attr_t
*a
, /* I - First attribute */
2424 ppd_attr_t
*b
) /* I - Second attribute */
2426 return (_cups_strcasecmp(a
->name
, b
->name
));
2431 * 'ppd_compare_choices()' - Compare two choices...
2434 static int /* O - Result of comparison */
2435 ppd_compare_choices(ppd_choice_t
*a
, /* I - First choice */
2436 ppd_choice_t
*b
) /* I - Second choice */
2438 return (strcmp(a
->option
->keyword
, b
->option
->keyword
));
2443 * 'ppd_compare_coptions()' - Compare two custom options.
2446 static int /* O - Result of comparison */
2447 ppd_compare_coptions(ppd_coption_t
*a
, /* I - First option */
2448 ppd_coption_t
*b
) /* I - Second option */
2450 return (_cups_strcasecmp(a
->keyword
, b
->keyword
));
2455 * 'ppd_compare_options()' - Compare two options.
2458 static int /* O - Result of comparison */
2459 ppd_compare_options(ppd_option_t
*a
, /* I - First option */
2460 ppd_option_t
*b
) /* I - Second option */
2462 return (_cups_strcasecmp(a
->keyword
, b
->keyword
));
2467 * 'ppd_decode()' - Decode a string value...
2470 static int /* O - Length of decoded string */
2471 ppd_decode(char *string
) /* I - String to decode */
2473 char *inptr
, /* Input pointer */
2474 *outptr
; /* Output pointer */
2480 while (*inptr
!= '\0')
2481 if (*inptr
== '<' && isxdigit(inptr
[1] & 255))
2484 * Convert hex to 8-bit values...
2488 while (isxdigit(*inptr
& 255))
2490 if (_cups_isalpha(*inptr
))
2491 *outptr
= (char)((tolower(*inptr
) - 'a' + 10) << 4);
2493 *outptr
= (char)((*inptr
- '0') << 4);
2497 if (!isxdigit(*inptr
& 255))
2500 if (_cups_isalpha(*inptr
))
2501 *outptr
|= (char)(tolower(*inptr
) - 'a' + 10);
2503 *outptr
|= (char)(*inptr
- '0');
2509 while (*inptr
!= '>' && *inptr
!= '\0')
2511 while (*inptr
== '>')
2515 *outptr
++ = *inptr
++;
2519 return ((int)(outptr
- string
));
2524 * 'ppd_free_filters()' - Free the filters array.
2528 ppd_free_filters(ppd_file_t
*ppd
) /* I - PPD file */
2530 int i
; /* Looping var */
2531 char **filter
; /* Current filter */
2534 if (ppd
->num_filters
> 0)
2536 for (i
= ppd
->num_filters
, filter
= ppd
->filters
; i
> 0; i
--, filter
++)
2541 ppd
->num_filters
= 0;
2542 ppd
->filters
= NULL
;
2548 * 'ppd_free_group()' - Free a single UI group.
2552 ppd_free_group(ppd_group_t
*group
) /* I - Group to free */
2554 int i
; /* Looping var */
2555 ppd_option_t
*option
; /* Current option */
2556 ppd_group_t
*subgroup
; /* Current sub-group */
2559 if (group
->num_options
> 0)
2561 for (i
= group
->num_options
, option
= group
->options
;
2564 ppd_free_option(option
);
2566 free(group
->options
);
2569 if (group
->num_subgroups
> 0)
2571 for (i
= group
->num_subgroups
, subgroup
= group
->subgroups
;
2574 ppd_free_group(subgroup
);
2576 free(group
->subgroups
);
2582 * 'ppd_free_option()' - Free a single option.
2586 ppd_free_option(ppd_option_t
*option
) /* I - Option to free */
2588 int i
; /* Looping var */
2589 ppd_choice_t
*choice
; /* Current choice */
2592 if (option
->num_choices
> 0)
2594 for (i
= option
->num_choices
, choice
= option
->choices
;
2601 free(option
->choices
);
2607 * 'ppd_get_coption()' - Get a custom option record.
2610 static ppd_coption_t
* /* O - Custom option... */
2611 ppd_get_coption(ppd_file_t
*ppd
, /* I - PPD file */
2612 const char *name
) /* I - Name of option */
2614 ppd_coption_t
*copt
; /* New custom option */
2618 * See if the option already exists...
2621 if ((copt
= ppdFindCustomOption(ppd
, name
)) != NULL
)
2625 * Not found, so create the custom option record...
2628 if ((copt
= calloc(1, sizeof(ppd_coption_t
))) == NULL
)
2631 strlcpy(copt
->keyword
, name
, sizeof(copt
->keyword
));
2633 copt
->params
= cupsArrayNew((cups_array_func_t
)NULL
, NULL
);
2635 cupsArrayAdd(ppd
->coptions
, copt
);
2638 * Return the new record...
2646 * 'ppd_get_cparam()' - Get a custom parameter record.
2649 static ppd_cparam_t
* /* O - Extended option... */
2650 ppd_get_cparam(ppd_coption_t
*opt
, /* I - PPD file */
2651 const char *param
, /* I - Name of parameter */
2652 const char *text
) /* I - Human-readable text */
2654 ppd_cparam_t
*cparam
; /* New custom parameter */
2658 * See if the parameter already exists...
2661 if ((cparam
= ppdFindCustomParam(opt
, param
)) != NULL
)
2665 * Not found, so create the custom parameter record...
2668 if ((cparam
= calloc(1, sizeof(ppd_cparam_t
))) == NULL
)
2671 cparam
->type
= PPD_CUSTOM_UNKNOWN
;
2672 strlcpy(cparam
->name
, param
, sizeof(cparam
->name
));
2673 strlcpy(cparam
->text
, text
[0] ? text
: param
, sizeof(cparam
->text
));
2676 * Add this record to the array...
2679 cupsArrayAdd(opt
->params
, cparam
);
2682 * Return the new record...
2690 * 'ppd_get_group()' - Find or create the named group as needed.
2693 static ppd_group_t
* /* O - Named group */
2694 ppd_get_group(ppd_file_t
*ppd
, /* I - PPD file */
2695 const char *name
, /* I - Name of group */
2696 const char *text
, /* I - Text for group */
2697 _ppd_globals_t
*pg
, /* I - Global data */
2698 cups_encoding_t encoding
) /* I - Encoding of text */
2700 int i
; /* Looping var */
2701 ppd_group_t
*group
; /* Group */
2704 DEBUG_printf(("7ppd_get_group(ppd=%p, name=\"%s\", text=\"%s\", cg=%p)",
2705 ppd
, name
, text
, pg
));
2707 for (i
= ppd
->num_groups
, group
= ppd
->groups
; i
> 0; i
--, group
++)
2708 if (!strcmp(group
->name
, name
))
2713 DEBUG_printf(("8ppd_get_group: Adding group %s...", name
));
2715 if (pg
->ppd_conform
== PPD_CONFORM_STRICT
&& strlen(text
) >= sizeof(group
->text
))
2717 pg
->ppd_status
= PPD_ILLEGAL_TRANSLATION
;
2722 if (ppd
->num_groups
== 0)
2723 group
= malloc(sizeof(ppd_group_t
));
2725 group
= realloc(ppd
->groups
, (size_t)(ppd
->num_groups
+ 1) * sizeof(ppd_group_t
));
2729 pg
->ppd_status
= PPD_ALLOC_ERROR
;
2734 ppd
->groups
= group
;
2735 group
+= ppd
->num_groups
;
2738 memset(group
, 0, sizeof(ppd_group_t
));
2739 strlcpy(group
->name
, name
, sizeof(group
->name
));
2741 cupsCharsetToUTF8((cups_utf8_t
*)group
->text
, text
,
2742 sizeof(group
->text
), encoding
);
2750 * 'ppd_get_option()' - Find or create the named option as needed.
2753 static ppd_option_t
* /* O - Named option */
2754 ppd_get_option(ppd_group_t
*group
, /* I - Group */
2755 const char *name
) /* I - Name of option */
2757 int i
; /* Looping var */
2758 ppd_option_t
*option
; /* Option */
2761 DEBUG_printf(("7ppd_get_option(group=%p(\"%s\"), name=\"%s\")",
2762 group
, group
->name
, name
));
2764 for (i
= group
->num_options
, option
= group
->options
; i
> 0; i
--, option
++)
2765 if (!strcmp(option
->keyword
, name
))
2770 if (group
->num_options
== 0)
2771 option
= malloc(sizeof(ppd_option_t
));
2773 option
= realloc(group
->options
, (size_t)(group
->num_options
+ 1) * sizeof(ppd_option_t
));
2778 group
->options
= option
;
2779 option
+= group
->num_options
;
2780 group
->num_options
++;
2782 memset(option
, 0, sizeof(ppd_option_t
));
2783 strlcpy(option
->keyword
, name
, sizeof(option
->keyword
));
2791 * 'ppd_globals_alloc()' - Allocate and initialize global data.
2794 static _ppd_globals_t
* /* O - Pointer to global data */
2795 ppd_globals_alloc(void)
2797 return ((_ppd_globals_t
*)calloc(1, sizeof(_ppd_globals_t
)));
2802 * 'ppd_globals_free()' - Free global data.
2805 #if defined(HAVE_PTHREAD_H) || defined(_WIN32)
2807 ppd_globals_free(_ppd_globals_t
*pg
) /* I - Pointer to global data */
2811 #endif /* HAVE_PTHREAD_H || _WIN32 */
2814 #ifdef HAVE_PTHREAD_H
2816 * 'ppd_globals_init()' - Initialize per-thread globals...
2820 ppd_globals_init(void)
2823 * Register the global data for this thread...
2826 pthread_key_create(&ppd_globals_key
, (void (*)(void *))ppd_globals_free
);
2828 #endif /* HAVE_PTHREAD_H */
2832 * 'ppd_hash_option()' - Generate a hash of the option name...
2835 static int /* O - Hash index */
2836 ppd_hash_option(ppd_option_t
*option
) /* I - Option */
2838 int hash
= 0; /* Hash index */
2839 const char *k
; /* Pointer into keyword */
2842 for (hash
= option
->keyword
[0], k
= option
->keyword
+ 1; *k
;)
2843 hash
= 33 * hash
+ *k
++;
2845 return (hash
& 511);
2850 * 'ppd_read()' - Read a line from a PPD file, skipping comment lines as
2854 static int /* O - Bitmask of fields read */
2855 ppd_read(cups_file_t
*fp
, /* I - File to read from */
2856 _ppd_line_t
*line
, /* I - Line buffer */
2857 char *keyword
, /* O - Keyword from line */
2858 char *option
, /* O - Option from line */
2859 char *text
, /* O - Human-readable text from line */
2860 char **string
, /* O - Code/string data */
2861 int ignoreblank
, /* I - Ignore blank lines? */
2862 _ppd_globals_t
*pg
) /* I - Global data */
2864 int ch
, /* Character from file */
2865 col
, /* Column in line */
2866 colon
, /* Colon seen? */
2867 endquote
, /* Waiting for an end quote */
2868 mask
, /* Mask to be returned */
2869 startline
, /* Start line */
2870 textlen
; /* Length of text */
2871 char *keyptr
, /* Keyword pointer */
2872 *optptr
, /* Option pointer */
2873 *textptr
, /* Text pointer */
2874 *strptr
, /* Pointer into string */
2875 *lineptr
; /* Current position in line buffer */
2879 * Now loop until we have a valid line...
2884 startline
= pg
->ppd_line
+ 1;
2888 line
->bufsize
= 1024;
2889 line
->buffer
= malloc(1024);
2901 lineptr
= line
->buffer
;
2905 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
2907 if (lineptr
>= (line
->buffer
+ line
->bufsize
- 1))
2910 * Expand the line buffer...
2913 char *temp
; /* Temporary line pointer */
2916 line
->bufsize
+= 1024;
2917 if (line
->bufsize
> 262144)
2920 * Don't allow lines longer than 256k!
2923 pg
->ppd_line
= startline
;
2924 pg
->ppd_status
= PPD_LINE_TOO_LONG
;
2929 temp
= realloc(line
->buffer
, line
->bufsize
);
2932 pg
->ppd_line
= startline
;
2933 pg
->ppd_status
= PPD_LINE_TOO_LONG
;
2938 lineptr
= temp
+ (lineptr
- line
->buffer
);
2939 line
->buffer
= temp
;
2942 if (ch
== '\r' || ch
== '\n')
2945 * Line feed or carriage return...
2954 * Check for a trailing line feed...
2957 if ((ch
= cupsFilePeekChar(fp
)) == EOF
)
2964 cupsFileGetChar(fp
);
2967 if (lineptr
== line
->buffer
&& ignoreblank
)
2968 continue; /* Skip blank lines */
2972 if (!endquote
) /* Continue for multi-line text */
2977 else if (ch
< ' ' && ch
!= '\t' && pg
->ppd_conform
== PPD_CONFORM_STRICT
)
2980 * Other control characters...
2983 pg
->ppd_line
= startline
;
2984 pg
->ppd_status
= PPD_ILLEGAL_CHARACTER
;
2988 else if (ch
!= 0x1a)
2991 * Any other character...
2994 *lineptr
++ = (char)ch
;
2997 if (col
> (PPD_MAX_LINE
- 1))
3000 * Line is too long...
3003 pg
->ppd_line
= startline
;
3004 pg
->ppd_status
= PPD_LINE_TOO_LONG
;
3009 if (ch
== ':' && strncmp(line
->buffer
, "*%", 2) != 0)
3012 if (ch
== '\"' && colon
)
3013 endquote
= !endquote
;
3020 * Didn't finish this quoted string...
3023 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
3026 else if (ch
== '\r' || ch
== '\n')
3034 * Check for a trailing line feed...
3037 if ((ch
= cupsFilePeekChar(fp
)) == EOF
)
3040 cupsFileGetChar(fp
);
3043 else if (ch
< ' ' && ch
!= '\t' && pg
->ppd_conform
== PPD_CONFORM_STRICT
)
3046 * Other control characters...
3049 pg
->ppd_line
= startline
;
3050 pg
->ppd_status
= PPD_ILLEGAL_CHARACTER
;
3054 else if (ch
!= 0x1a)
3058 if (col
> (PPD_MAX_LINE
- 1))
3061 * Line is too long...
3064 pg
->ppd_line
= startline
;
3065 pg
->ppd_status
= PPD_LINE_TOO_LONG
;
3075 * Didn't finish this line...
3078 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
3079 if (ch
== '\r' || ch
== '\n')
3082 * Line feed or carriage return...
3091 * Check for a trailing line feed...
3094 if ((ch
= cupsFilePeekChar(fp
)) == EOF
)
3097 cupsFileGetChar(fp
);
3102 else if (ch
< ' ' && ch
!= '\t' && pg
->ppd_conform
== PPD_CONFORM_STRICT
)
3105 * Other control characters...
3108 pg
->ppd_line
= startline
;
3109 pg
->ppd_status
= PPD_ILLEGAL_CHARACTER
;
3113 else if (ch
!= 0x1a)
3117 if (col
> (PPD_MAX_LINE
- 1))
3120 * Line is too long...
3123 pg
->ppd_line
= startline
;
3124 pg
->ppd_status
= PPD_LINE_TOO_LONG
;
3131 if (lineptr
> line
->buffer
&& lineptr
[-1] == '\n')
3136 DEBUG_printf(("9ppd_read: LINE=\"%s\"", line
->buffer
));
3139 * The dynamically created PPDs for older style macOS
3140 * drivers include a large blob of data inserted as comments
3141 * at the end of the file. As an optimization we can stop
3142 * reading the PPD when we get to the start of this data.
3145 if (!strcmp(line
->buffer
, "*%APLWORKSET START"))
3148 if (ch
== EOF
&& lineptr
== line
->buffer
)
3156 lineptr
= line
->buffer
+ 1;
3163 if ((!line
->buffer
[0] || /* Blank line */
3164 !strncmp(line
->buffer
, "*%", 2) || /* Comment line */
3165 !strcmp(line
->buffer
, "*End")) && /* End of multi-line string */
3166 ignoreblank
) /* Ignore these? */
3168 startline
= pg
->ppd_line
+ 1;
3172 if (!strcmp(line
->buffer
, "*")) /* (Bad) comment line */
3174 if (pg
->ppd_conform
== PPD_CONFORM_RELAXED
)
3176 startline
= pg
->ppd_line
+ 1;
3181 pg
->ppd_line
= startline
;
3182 pg
->ppd_status
= PPD_ILLEGAL_MAIN_KEYWORD
;
3188 if (line
->buffer
[0] != '*') /* All lines start with an asterisk */
3191 * Allow lines consisting of just whitespace...
3194 for (lineptr
= line
->buffer
; *lineptr
; lineptr
++)
3195 if (*lineptr
&& !_cups_isspace(*lineptr
))
3200 pg
->ppd_status
= PPD_MISSING_ASTERISK
;
3203 else if (ignoreblank
)
3215 while (*lineptr
&& *lineptr
!= ':' && !_cups_isspace(*lineptr
))
3217 if (*lineptr
<= ' ' || *lineptr
> 126 || *lineptr
== '/' ||
3218 (keyptr
- keyword
) >= (PPD_MAX_NAME
- 1))
3220 pg
->ppd_status
= PPD_ILLEGAL_MAIN_KEYWORD
;
3224 *keyptr
++ = *lineptr
++;
3229 if (!strcmp(keyword
, "End"))
3232 mask
|= PPD_KEYWORD
;
3234 if (_cups_isspace(*lineptr
))
3237 * Get an option name...
3240 while (_cups_isspace(*lineptr
))
3245 while (*lineptr
&& !_cups_isspace(*lineptr
) && *lineptr
!= ':' &&
3248 if (*lineptr
<= ' ' || *lineptr
> 126 ||
3249 (optptr
- option
) >= (PPD_MAX_NAME
- 1))
3251 pg
->ppd_status
= PPD_ILLEGAL_OPTION_KEYWORD
;
3255 *optptr
++ = *lineptr
++;
3260 if (_cups_isspace(*lineptr
) && pg
->ppd_conform
== PPD_CONFORM_STRICT
)
3262 pg
->ppd_status
= PPD_ILLEGAL_WHITESPACE
;
3266 while (_cups_isspace(*lineptr
))
3271 if (*lineptr
== '/')
3274 * Get human-readable text...
3281 while (*lineptr
!= '\0' && *lineptr
!= '\n' && *lineptr
!= ':')
3283 if (((unsigned char)*lineptr
< ' ' && *lineptr
!= '\t') ||
3284 (textptr
- text
) >= (PPD_MAX_LINE
- 1))
3286 pg
->ppd_status
= PPD_ILLEGAL_TRANSLATION
;
3290 *textptr
++ = *lineptr
++;
3294 textlen
= ppd_decode(text
);
3296 if (textlen
> PPD_MAX_TEXT
&& pg
->ppd_conform
== PPD_CONFORM_STRICT
)
3298 pg
->ppd_status
= PPD_ILLEGAL_TRANSLATION
;
3306 if (_cups_isspace(*lineptr
) && pg
->ppd_conform
== PPD_CONFORM_STRICT
)
3308 pg
->ppd_status
= PPD_ILLEGAL_WHITESPACE
;
3312 while (_cups_isspace(*lineptr
))
3315 if (*lineptr
== ':')
3318 * Get string after triming leading and trailing whitespace...
3322 while (_cups_isspace(*lineptr
))
3325 strptr
= lineptr
+ strlen(lineptr
) - 1;
3326 while (strptr
>= lineptr
&& _cups_isspace(*strptr
))
3329 if (*strptr
== '\"')
3332 * Quoted string by itself, remove quotes...
3339 *string
= strdup(lineptr
);
3351 * 'ppd_update_filters()' - Update the filters array as needed.
3353 * This function re-populates the filters array with cupsFilter2 entries that
3354 * have been stripped of the destination MIME media types and any maxsize hints.
3356 * (All for backwards-compatibility)
3359 static int /* O - 1 on success, 0 on failure */
3360 ppd_update_filters(ppd_file_t
*ppd
, /* I - PPD file */
3361 _ppd_globals_t
*pg
) /* I - Global data */
3363 ppd_attr_t
*attr
; /* Current cupsFilter2 value */
3364 char srcsuper
[16], /* Source MIME media type */
3366 dstsuper
[16], /* Destination MIME media type */
3368 program
[1024], /* Command to run */
3369 *ptr
, /* Pointer into command to run */
3370 buffer
[1024], /* Re-written cupsFilter value */
3371 **filter
; /* Current filter */
3372 int cost
; /* Cost of filter */
3375 DEBUG_printf(("4ppd_update_filters(ppd=%p, cg=%p)", ppd
, pg
));
3378 * See if we have any cupsFilter2 lines...
3381 if ((attr
= ppdFindAttr(ppd
, "cupsFilter2", NULL
)) == NULL
)
3383 DEBUG_puts("5ppd_update_filters: No cupsFilter2 keywords present.");
3388 * Yes, free the cupsFilter-defined filters and re-build...
3391 ppd_free_filters(ppd
);
3396 * Parse the cupsFilter2 string:
3398 * src/type dst/type cost program
3399 * src/type dst/type cost maxsize(n) program
3402 DEBUG_printf(("5ppd_update_filters: cupsFilter2=\"%s\"", attr
->value
));
3404 if (sscanf(attr
->value
, "%15[^/]/%255s%*[ \t]%15[^/]/%255s%d%*[ \t]%1023[^\n]",
3405 srcsuper
, srctype
, dstsuper
, dsttype
, &cost
, program
) != 6)
3407 DEBUG_puts("5ppd_update_filters: Bad cupsFilter2 line.");
3408 pg
->ppd_status
= PPD_BAD_VALUE
;
3413 DEBUG_printf(("5ppd_update_filters: srcsuper=\"%s\", srctype=\"%s\", "
3414 "dstsuper=\"%s\", dsttype=\"%s\", cost=%d, program=\"%s\"",
3415 srcsuper
, srctype
, dstsuper
, dsttype
, cost
, program
));
3417 if (!strncmp(program
, "maxsize(", 8) &&
3418 (ptr
= strchr(program
+ 8, ')')) != NULL
)
3420 DEBUG_puts("5ppd_update_filters: Found maxsize(nnn).");
3423 while (_cups_isspace(*ptr
))
3426 _cups_strcpy(program
, ptr
);
3427 DEBUG_printf(("5ppd_update_filters: New program=\"%s\"", program
));
3431 * Convert to cupsFilter format:
3433 * src/type cost program
3436 snprintf(buffer
, sizeof(buffer
), "%s/%s %d %s", srcsuper
, srctype
, cost
,
3438 DEBUG_printf(("5ppd_update_filters: Adding \"%s\".", buffer
));
3441 * Add a cupsFilter-compatible string to the filters array.
3444 if (ppd
->num_filters
== 0)
3445 filter
= malloc(sizeof(char *));
3447 filter
= realloc(ppd
->filters
, sizeof(char *) * (size_t)(ppd
->num_filters
+ 1));
3451 DEBUG_puts("5ppd_update_filters: Out of memory.");
3452 pg
->ppd_status
= PPD_ALLOC_ERROR
;
3457 ppd
->filters
= filter
;
3458 filter
+= ppd
->num_filters
;
3459 ppd
->num_filters
++;
3461 *filter
= strdup(buffer
);
3463 while ((attr
= ppdFindNextAttr(ppd
, "cupsFilter2", NULL
)) != NULL
);
3465 DEBUG_puts("5ppd_update_filters: Completed OK.");