2 * "$Id: ppd.c 7552 2008-05-12 17:42:15Z mike $"
4 * PPD file routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * PostScript is a trademark of Adobe Systems, Inc.
17 * This code and any derivative of it may be used and distributed
18 * freely under the terms of the GNU General Public License when
19 * used with GNU Ghostscript or its derivatives. Use of the code
20 * (or any derivative of it) with software other than GNU
21 * GhostScript (or its derivatives) is governed by the CUPS license
24 * This file is subject to the Apple OS-Developed Software exception.
28 * ppdClose() - Free all memory used by the PPD file.
29 * ppdErrorString() - Returns the text assocated with a status.
30 * _ppdGetEncoding() - Get the CUPS encoding value for the given
32 * ppdLastError() - Return the status from the last ppdOpen*().
33 * ppdOpen() - Read a PPD file into memory.
34 * ppdOpen2() - Read a PPD file into memory.
35 * ppdOpenFd() - Read a PPD file into memory.
36 * ppdOpenFile() - Read a PPD file into memory.
37 * ppdSetConformance() - Set the conformance level for PPD files.
38 * ppd_add_attr() - Add an attribute to the PPD data.
39 * ppd_add_choice() - Add a choice to an option.
40 * ppd_add_size() - Add a page size.
41 * ppd_compare_attrs() - Compare two attributes.
42 * ppd_compare_choices() - Compare two choices...
43 * ppd_compare_consts() - Compare two constraints.
44 * ppd_compare_coptions() - Compare two custom options.
45 * ppd_compare_cparams() - Compare two custom parameters.
46 * ppd_compare_options() - Compare two options.
47 * ppd_decode() - Decode a string value...
48 * ppd_free_group() - Free a single UI group.
49 * ppd_free_option() - Free a single option.
50 * ppd_get_coption() - Get a custom option record.
51 * ppd_get_cparam() - Get a custom parameter record.
52 * ppd_get_group() - Find or create the named group as needed.
53 * ppd_get_option() - Find or create the named option as needed.
54 * ppd_hash_option() - Generate a hash of the option name...
55 * ppd_read() - Read a line from a PPD file, skipping comment
60 * Include necessary headers.
72 #if defined(WIN32) || defined(__EMX__)
73 # define READ_BINARY "rb" /* Open a binary file for reading */
74 # define WRITE_BINARY "wb" /* Open a binary file for writing */
76 # define READ_BINARY "r" /* Open a binary file for reading */
77 # define WRITE_BINARY "w" /* Open a binary file for writing */
78 #endif /* WIN32 || __EMX__ */
80 #define ppd_free(p) if (p) free(p) /* Safe free macro */
82 #define PPD_KEYWORD 1 /* Line contained a keyword */
83 #define PPD_OPTION 2 /* Line contained an option name */
84 #define PPD_TEXT 4 /* Line contained human-readable text */
85 #define PPD_STRING 8 /* Line contained a string or code */
87 #define PPD_HASHSIZE 512 /* Size of hash */
91 * Line buffer structure...
94 typedef struct _ppd_line_s
96 char *buffer
; /* Pointer to buffer */
97 size_t bufsize
; /* Size of the buffer */
105 static ppd_attr_t
*ppd_add_attr(ppd_file_t
*ppd
, const char *name
,
106 const char *spec
, const char *text
,
108 static ppd_choice_t
*ppd_add_choice(ppd_option_t
*option
, const char *name
);
109 static ppd_size_t
*ppd_add_size(ppd_file_t
*ppd
, const char *name
);
110 static int ppd_compare_attrs(ppd_attr_t
*a
, ppd_attr_t
*b
);
111 static int ppd_compare_choices(ppd_choice_t
*a
, ppd_choice_t
*b
);
112 static int ppd_compare_consts(ppd_const_t
*a
, ppd_const_t
*b
);
113 static int ppd_compare_coptions(ppd_coption_t
*a
,
115 static int ppd_compare_cparams(ppd_cparam_t
*a
, ppd_cparam_t
*b
);
116 static int ppd_compare_options(ppd_option_t
*a
, ppd_option_t
*b
);
117 static int ppd_decode(char *string
);
118 static void ppd_free_group(ppd_group_t
*group
);
119 static void ppd_free_option(ppd_option_t
*option
);
120 static ppd_coption_t
*ppd_get_coption(ppd_file_t
*ppd
, const char *name
);
121 static ppd_cparam_t
*ppd_get_cparam(ppd_coption_t
*opt
,
124 static ppd_group_t
*ppd_get_group(ppd_file_t
*ppd
, const char *name
,
125 const char *text
, _cups_globals_t
*cg
,
126 cups_encoding_t encoding
);
127 static ppd_option_t
*ppd_get_option(ppd_group_t
*group
, const char *name
);
128 static int ppd_hash_option(ppd_option_t
*option
);
129 static int ppd_read(cups_file_t
*fp
, _ppd_line_t
*line
,
130 char *keyword
, char *option
, char *text
,
131 char **string
, int ignoreblank
,
132 _cups_globals_t
*cg
);
136 * 'ppdClose()' - Free all memory used by the PPD file.
140 ppdClose(ppd_file_t
*ppd
) /* I - PPD file record */
142 int i
; /* Looping var */
143 ppd_emul_t
*emul
; /* Current emulation */
144 ppd_group_t
*group
; /* Current group */
145 char **font
; /* Current font */
146 char **filter
; /* Current filter */
147 ppd_attr_t
**attr
; /* Current attribute */
148 ppd_coption_t
*coption
; /* Current custom option */
149 ppd_cparam_t
*cparam
; /* Current custom parameter */
153 * Range check arguments...
160 * Free all strings at the top level...
163 _cupsStrFree(ppd
->lang_encoding
);
164 _cupsStrFree(ppd
->nickname
);
165 _cupsStrFree(ppd
->patches
);
166 _cupsStrFree(ppd
->jcl_begin
);
167 _cupsStrFree(ppd
->jcl_end
);
168 _cupsStrFree(ppd
->jcl_ps
);
171 * Free any emulations...
174 if (ppd
->num_emulations
> 0)
176 for (i
= ppd
->num_emulations
, emul
= ppd
->emulations
; i
> 0; i
--, emul
++)
178 _cupsStrFree(emul
->start
);
179 _cupsStrFree(emul
->stop
);
182 ppd_free(ppd
->emulations
);
186 * Free any UI groups, subgroups, and options...
189 if (ppd
->num_groups
> 0)
191 for (i
= ppd
->num_groups
, group
= ppd
->groups
; i
> 0; i
--, group
++)
192 ppd_free_group(group
);
194 ppd_free(ppd
->groups
);
197 cupsArrayDelete(ppd
->options
);
198 cupsArrayDelete(ppd
->marked
);
201 * Free any page sizes...
204 if (ppd
->num_sizes
> 0)
205 ppd_free(ppd
->sizes
);
208 * Free any constraints...
211 if (ppd
->num_consts
> 0)
212 ppd_free(ppd
->consts
);
215 * Free any filters...
218 if (ppd
->num_filters
> 0)
220 for (i
= ppd
->num_filters
, filter
= ppd
->filters
; i
> 0; i
--, filter
++)
221 _cupsStrFree(*filter
);
223 ppd_free(ppd
->filters
);
230 if (ppd
->num_fonts
> 0)
232 for (i
= ppd
->num_fonts
, font
= ppd
->fonts
; i
> 0; i
--, font
++)
235 ppd_free(ppd
->fonts
);
239 * Free any profiles...
242 if (ppd
->num_profiles
> 0)
243 ppd_free(ppd
->profiles
);
246 * Free any attributes...
249 if (ppd
->num_attrs
> 0)
251 for (i
= ppd
->num_attrs
, attr
= ppd
->attrs
; i
> 0; i
--, attr
++)
253 _cupsStrFree((*attr
)->value
);
257 ppd_free(ppd
->attrs
);
260 cupsArrayDelete(ppd
->sorted_attrs
);
263 * Free custom options...
266 for (coption
= (ppd_coption_t
*)cupsArrayFirst(ppd
->coptions
);
268 coption
= (ppd_coption_t
*)cupsArrayNext(ppd
->coptions
))
270 for (cparam
= (ppd_cparam_t
*)cupsArrayFirst(coption
->params
);
272 cparam
= (ppd_cparam_t
*)cupsArrayNext(coption
->params
))
274 switch (cparam
->type
)
276 case PPD_CUSTOM_PASSCODE
:
277 case PPD_CUSTOM_PASSWORD
:
278 case PPD_CUSTOM_STRING
:
279 _cupsStrFree(cparam
->current
.custom_string
);
289 cupsArrayDelete(coption
->params
);
294 cupsArrayDelete(ppd
->coptions
);
297 * Free the whole record...
305 * 'ppdErrorString()' - Returns the text assocated with a status.
307 * @since CUPS 1.1.19@
310 const char * /* O - Status string */
311 ppdErrorString(ppd_status_t status
) /* I - PPD status */
313 static const char * const messages
[] =/* Status messages */
316 _("Unable to open PPD file"),
317 _("NULL PPD file pointer"),
318 _("Memory allocation error"),
319 _("Missing PPD-Adobe-4.x header"),
320 _("Missing value string"),
323 _("OpenGroup without a CloseGroup first"),
324 _("Bad OpenUI/JCLOpenUI"),
325 _("OpenUI/JCLOpenUI without a CloseUI/JCLCloseUI first"),
326 _("Bad OrderDependency"),
327 _("Bad UIConstraints"),
328 _("Missing asterisk in column 1"),
329 _("Line longer than the maximum allowed (255 characters)"),
330 _("Illegal control character"),
331 _("Illegal main keyword string"),
332 _("Illegal option keyword string"),
333 _("Illegal translation string"),
334 _("Illegal whitespace character"),
335 _("Bad custom parameter")
339 if (status
< PPD_OK
|| status
> PPD_ILLEGAL_WHITESPACE
)
340 return (_cupsLangString(cupsLangDefault(), _("Unknown")));
342 return (_cupsLangString(cupsLangDefault(), messages
[status
]));
347 * '_ppdGetEncoding()' - Get the CUPS encoding value for the given
351 cups_encoding_t
/* O - CUPS encoding value */
352 _ppdGetEncoding(const char *name
) /* I - LanguageEncoding string */
354 if (!strcasecmp(name
, "ISOLatin1"))
355 return (CUPS_ISO8859_1
);
356 else if (!strcasecmp(name
, "ISOLatin2"))
357 return (CUPS_ISO8859_2
);
358 else if (!strcasecmp(name
, "ISOLatin5"))
359 return (CUPS_ISO8859_5
);
360 else if (!strcasecmp(name
, "JIS83-RKSJ"))
361 return (CUPS_WINDOWS_932
);
362 else if (!strcasecmp(name
, "MacStandard"))
363 return (CUPS_MAC_ROMAN
);
364 else if (!strcasecmp(name
, "WindowsANSI"))
365 return (CUPS_WINDOWS_1252
);
372 * 'ppdLastError()' - Return the status from the last ppdOpen*().
374 * @since CUPS 1.1.19@
377 ppd_status_t
/* O - Status code */
378 ppdLastError(int *line
) /* O - Line number */
380 _cups_globals_t
*cg
= _cupsGlobals();
385 *line
= cg
->ppd_line
;
387 return (cg
->ppd_status
);
392 * 'ppdOpen()' - Read a PPD file into memory.
395 ppd_file_t
* /* O - PPD file record */
396 ppdOpen(FILE *fp
) /* I - File to read from */
398 ppd_file_t
*ppd
; /* PPD file record */
399 cups_file_t
*cf
; /* CUPS file */
403 * Reopen the stdio file as a CUPS file...
406 if ((cf
= cupsFileOpenFd(fileno(fp
), "r")) == NULL
)
410 * Load the PPD file using the newer API...
416 * Close the CUPS file and return the PPD...
426 * 'ppdOpen2()' - Read a PPD file into memory.
431 ppd_file_t
* /* O - PPD file record or @code NULL@ if the PPD file could not be opened. */
432 ppdOpen2(cups_file_t
*fp
) /* I - File to read from */
434 int i
, j
, k
; /* Looping vars */
435 int count
; /* Temporary count */
436 _ppd_line_t line
; /* Line buffer */
437 ppd_file_t
*ppd
; /* PPD file record */
438 ppd_group_t
*group
, /* Current group */
439 *subgroup
; /* Current sub-group */
440 ppd_option_t
*option
; /* Current option */
441 ppd_choice_t
*choice
; /* Current choice */
442 ppd_const_t
*constraint
; /* Current constraint */
443 ppd_size_t
*size
; /* Current page size */
444 int mask
; /* Line data mask */
445 char keyword
[PPD_MAX_NAME
],
446 /* Keyword from file */
448 /* Option from file */
450 /* Human-readable text from file */
451 *string
, /* Code/text from file */
452 *sptr
, /* Pointer into string */
453 *nameptr
, /* Pointer into name */
454 *temp
, /* Temporary string pointer */
455 **tempfonts
; /* Temporary fonts pointer */
456 float order
; /* Order dependency number */
457 ppd_section_t section
; /* Order dependency section */
458 ppd_profile_t
*profile
; /* Pointer to color profile */
459 char **filter
; /* Pointer to filter */
460 cups_lang_t
*language
; /* Default language */
461 struct lconv
*loc
; /* Locale data */
462 int ui_keyword
; /* Is this line a UI keyword? */
463 cups_encoding_t encoding
; /* Encoding of PPD file */
464 _cups_globals_t
*cg
= _cupsGlobals();
466 char custom_name
[PPD_MAX_NAME
];
467 /* CustomFoo attribute name */
468 ppd_attr_t
*custom_attr
; /* CustomFoo attribute */
469 static const char * const ui_keywords
[] =
471 #ifdef CUPS_USE_FULL_UI_KEYWORDS_LIST
473 * Adobe defines some 41 keywords as "UI", meaning that they are
474 * user interface elements and that they should be treated as such
475 * even if the PPD creator doesn't use Open/CloseUI around them.
477 * Since this can cause previously invisible options to appear and
478 * confuse users, the default is to only treat the PageSize and
479 * PageRegion keywords this way.
481 /* Boolean keywords */
491 /* PickOne keywords */
504 "JCLFrameBufferSize",
525 #else /* !CUPS_USE_FULL_UI_KEYWORDS_LIST */
528 #endif /* CUPS_USE_FULL_UI_KEYWORDS_LIST */
533 * Default to "OK" status...
536 cg
->ppd_status
= PPD_OK
;
540 * Range check input...
545 cg
->ppd_status
= PPD_NULL_FILE
;
550 * Grab the first line and make sure it reads '*PPD-Adobe: "major.minor"'...
556 mask
= ppd_read(fp
, &line
, keyword
, name
, text
, &string
, 0, cg
);
558 DEBUG_printf(("mask=%x, keyword=\"%s\"...\n", mask
, keyword
));
561 strcmp(keyword
, "PPD-Adobe") ||
562 string
== NULL
|| string
[0] != '4')
565 * Either this is not a PPD file, or it is not a 4.x PPD file.
568 if (cg
->ppd_status
== PPD_OK
)
569 cg
->ppd_status
= PPD_MISSING_PPDADOBE4
;
571 _cupsStrFree(string
);
576 DEBUG_printf(("ppdOpen: keyword = %s, string = %p\n", keyword
, string
));
578 _cupsStrFree(string
);
581 * Allocate memory for the PPD file record...
584 if ((ppd
= calloc(1, sizeof(ppd_file_t
))) == NULL
)
586 cg
->ppd_status
= PPD_ALLOC_ERROR
;
591 ppd
->language_level
= 2;
592 ppd
->color_device
= 0;
593 ppd
->colorspace
= PPD_CS_N
;
594 ppd
->landscape
= -90;
595 ppd
->coptions
= cupsArrayNew((cups_array_func_t
)ppd_compare_coptions
,
599 * Get the default language for the user...
602 language
= cupsLangDefault();
606 * Read lines from the PPD file and add them to the file record...
614 encoding
= CUPS_ISO8859_1
;
616 while ((mask
= ppd_read(fp
, &line
, keyword
, name
, text
, &string
, 1, cg
)) != 0)
618 DEBUG_printf(("mask=%x, keyword=\"%s\", name=\"%s\", text=\"%s\", "
619 "string=%d chars...", mask
, keyword
, name
, text
,
620 string
? (int)strlen(string
) : 0));
622 if (strncmp(keyword
, "Default", 7) && !string
)
625 * Need a string value!
628 cg
->ppd_status
= PPD_MISSING_VALUE
;
634 * Certain main keywords (as defined by the PPD spec) may be used
635 * without the usual OpenUI/CloseUI stuff. Presumably this is just
636 * so that Adobe wouldn't completely break compatibility with PPD
637 * files prior to v4.0 of the spec, but it is hopelessly
638 * inconsistent... Catch these main keywords and automatically
639 * create the corresponding option, as needed...
645 * Previous line was a UI keyword...
652 if (option
== NULL
&&
653 (mask
& (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
)) ==
654 (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
))
656 for (i
= 0; i
< (int)(sizeof(ui_keywords
) / sizeof(ui_keywords
[0])); i
++)
657 if (!strcmp(keyword
, ui_keywords
[i
]))
660 if (i
< (int)(sizeof(ui_keywords
) / sizeof(ui_keywords
[0])))
663 * Create the option in the appropriate group...
668 DEBUG_printf(("**** FOUND ADOBE UI KEYWORD %s WITHOUT OPENUI!\n",
673 if ((group
= ppd_get_group(ppd
, "General", _("General"), cg
,
677 DEBUG_printf(("Adding to group %s...\n", group
->text
));
678 option
= ppd_get_option(group
, keyword
);
682 option
= ppd_get_option(group
, keyword
);
686 cg
->ppd_status
= PPD_ALLOC_ERROR
;
692 * Now fill in the initial information for the option...
695 if (!strncmp(keyword
, "JCL", 3))
696 option
->section
= PPD_ORDER_JCL
;
698 option
->section
= PPD_ORDER_ANY
;
700 option
->order
= 10.0f
;
703 option
->ui
= PPD_UI_BOOLEAN
;
705 option
->ui
= PPD_UI_PICKONE
;
707 for (j
= 0; j
< ppd
->num_attrs
; j
++)
708 if (!strncmp(ppd
->attrs
[j
]->name
, "Default", 7) &&
709 !strcmp(ppd
->attrs
[j
]->name
+ 7, keyword
) &&
710 ppd
->attrs
[j
]->value
)
712 DEBUG_printf(("Setting Default%s to %s via attribute...\n",
713 option
->keyword
, ppd
->attrs
[j
]->value
));
714 strlcpy(option
->defchoice
, ppd
->attrs
[j
]->value
,
715 sizeof(option
->defchoice
));
719 if (!strcmp(keyword
, "PageSize"))
720 strlcpy(option
->text
, _("Media Size"), sizeof(option
->text
));
721 else if (!strcmp(keyword
, "MediaType"))
722 strlcpy(option
->text
, _("Media Type"), sizeof(option
->text
));
723 else if (!strcmp(keyword
, "InputSlot"))
724 strlcpy(option
->text
, _("Media Source"), sizeof(option
->text
));
725 else if (!strcmp(keyword
, "ColorModel"))
726 strlcpy(option
->text
, _("Output Mode"), sizeof(option
->text
));
727 else if (!strcmp(keyword
, "Resolution"))
728 strlcpy(option
->text
, _("Resolution"), sizeof(option
->text
));
730 strlcpy(option
->text
, keyword
, sizeof(option
->text
));
734 if (!strcmp(keyword
, "LanguageLevel"))
735 ppd
->language_level
= atoi(string
);
736 else if (!strcmp(keyword
, "LanguageEncoding"))
739 * Say all PPD files are UTF-8, since we convert to UTF-8...
742 ppd
->lang_encoding
= _cupsStrAlloc("UTF-8");
743 encoding
= _ppdGetEncoding(string
);
745 else if (!strcmp(keyword
, "LanguageVersion"))
746 ppd
->lang_version
= string
;
747 else if (!strcmp(keyword
, "Manufacturer"))
748 ppd
->manufacturer
= string
;
749 else if (!strcmp(keyword
, "ModelName"))
750 ppd
->modelname
= string
;
751 else if (!strcmp(keyword
, "Protocols"))
752 ppd
->protocols
= string
;
753 else if (!strcmp(keyword
, "PCFileName"))
754 ppd
->pcfilename
= string
;
755 else if (!strcmp(keyword
, "NickName"))
757 if (encoding
!= CUPS_UTF8
)
759 cups_utf8_t utf8
[256]; /* UTF-8 version of NickName */
762 cupsCharsetToUTF8(utf8
, string
, sizeof(utf8
), encoding
);
763 ppd
->nickname
= _cupsStrAlloc((char *)utf8
);
766 ppd
->nickname
= _cupsStrAlloc(string
);
768 else if (!strcmp(keyword
, "Product"))
769 ppd
->product
= string
;
770 else if (!strcmp(keyword
, "ShortNickName"))
771 ppd
->shortnickname
= string
;
772 else if (!strcmp(keyword
, "TTRasterizer"))
773 ppd
->ttrasterizer
= string
;
774 else if (!strcmp(keyword
, "JCLBegin"))
776 ppd
->jcl_begin
= _cupsStrAlloc(string
);
777 ppd_decode(ppd
->jcl_begin
); /* Decode quoted string */
779 else if (!strcmp(keyword
, "JCLEnd"))
781 ppd
->jcl_end
= _cupsStrAlloc(string
);
782 ppd_decode(ppd
->jcl_end
); /* Decode quoted string */
784 else if (!strcmp(keyword
, "JCLToPSInterpreter"))
786 ppd
->jcl_ps
= _cupsStrAlloc(string
);
787 ppd_decode(ppd
->jcl_ps
); /* Decode quoted string */
789 else if (!strcmp(keyword
, "AccurateScreensSupport"))
790 ppd
->accurate_screens
= !strcmp(string
, "True");
791 else if (!strcmp(keyword
, "ColorDevice"))
792 ppd
->color_device
= !strcmp(string
, "True");
793 else if (!strcmp(keyword
, "ContoneOnly"))
794 ppd
->contone_only
= !strcmp(string
, "True");
795 else if (!strcmp(keyword
, "cupsFlipDuplex"))
796 ppd
->flip_duplex
= !strcmp(string
, "True");
797 else if (!strcmp(keyword
, "cupsManualCopies"))
798 ppd
->manual_copies
= !strcmp(string
, "True");
799 else if (!strcmp(keyword
, "cupsModelNumber"))
800 ppd
->model_number
= atoi(string
);
801 else if (!strcmp(keyword
, "cupsColorProfile"))
803 if (ppd
->num_profiles
== 0)
804 profile
= malloc(sizeof(ppd_profile_t
));
806 profile
= realloc(ppd
->profiles
, sizeof(ppd_profile_t
) *
807 (ppd
->num_profiles
+ 1));
811 cg
->ppd_status
= PPD_ALLOC_ERROR
;
816 ppd
->profiles
= profile
;
817 profile
+= ppd
->num_profiles
;
818 ppd
->num_profiles
++;
820 memset(profile
, 0, sizeof(ppd_profile_t
));
821 strlcpy(profile
->resolution
, name
, sizeof(profile
->resolution
));
822 strlcpy(profile
->media_type
, text
, sizeof(profile
->media_type
));
824 profile
->density
= (float)_cupsStrScand(string
, &sptr
, loc
);
825 profile
->gamma
= (float)_cupsStrScand(sptr
, &sptr
, loc
);
826 profile
->matrix
[0][0] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
827 profile
->matrix
[0][1] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
828 profile
->matrix
[0][2] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
829 profile
->matrix
[1][0] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
830 profile
->matrix
[1][1] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
831 profile
->matrix
[1][2] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
832 profile
->matrix
[2][0] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
833 profile
->matrix
[2][1] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
834 profile
->matrix
[2][2] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
836 else if (!strcmp(keyword
, "cupsFilter"))
838 if (ppd
->num_filters
== 0)
839 filter
= malloc(sizeof(char *));
841 filter
= realloc(ppd
->filters
, sizeof(char *) * (ppd
->num_filters
+ 1));
845 cg
->ppd_status
= PPD_ALLOC_ERROR
;
850 ppd
->filters
= filter
;
851 filter
+= ppd
->num_filters
;
855 * Copy filter string and prevent it from being freed below...
861 else if (!strcmp(keyword
, "Throughput"))
862 ppd
->throughput
= atoi(string
);
863 else if (!strcmp(keyword
, "Font"))
866 * Add this font to the list of available fonts...
869 if (ppd
->num_fonts
== 0)
870 tempfonts
= (char **)malloc(sizeof(char *));
872 tempfonts
= (char **)realloc(ppd
->fonts
,
873 sizeof(char *) * (ppd
->num_fonts
+ 1));
875 if (tempfonts
== NULL
)
877 cg
->ppd_status
= PPD_ALLOC_ERROR
;
882 ppd
->fonts
= tempfonts
;
883 ppd
->fonts
[ppd
->num_fonts
] = _cupsStrAlloc(name
);
886 else if (!strncmp(keyword
, "ParamCustom", 11))
888 ppd_coption_t
*coption
; /* Custom option */
889 ppd_cparam_t
*cparam
; /* Custom parameter */
890 int corder
; /* Order number */
891 char ctype
[33], /* Data type */
892 cminimum
[65], /* Minimum value */
893 cmaximum
[65]; /* Maximum value */
897 * Get the custom option and parameter...
900 if ((coption
= ppd_get_coption(ppd
, keyword
+ 11)) == NULL
)
902 cg
->ppd_status
= PPD_ALLOC_ERROR
;
907 if ((cparam
= ppd_get_cparam(coption
, name
, text
)) == NULL
)
909 cg
->ppd_status
= PPD_ALLOC_ERROR
;
915 * Get the parameter data...
918 if (sscanf(string
, "%d%32s%64s%64s", &corder
, ctype
, cminimum
,
921 cg
->ppd_status
= PPD_BAD_CUSTOM_PARAM
;
926 cparam
->order
= corder
;
928 if (!strcmp(ctype
, "curve"))
930 cparam
->type
= PPD_CUSTOM_CURVE
;
931 cparam
->minimum
.custom_curve
= (float)_cupsStrScand(cminimum
, NULL
, loc
);
932 cparam
->maximum
.custom_curve
= (float)_cupsStrScand(cmaximum
, NULL
, loc
);
934 else if (!strcmp(ctype
, "int"))
936 cparam
->type
= PPD_CUSTOM_INT
;
937 cparam
->minimum
.custom_int
= atoi(cminimum
);
938 cparam
->maximum
.custom_int
= atoi(cmaximum
);
940 else if (!strcmp(ctype
, "invcurve"))
942 cparam
->type
= PPD_CUSTOM_INVCURVE
;
943 cparam
->minimum
.custom_invcurve
= (float)_cupsStrScand(cminimum
, NULL
, loc
);
944 cparam
->maximum
.custom_invcurve
= (float)_cupsStrScand(cmaximum
, NULL
, loc
);
946 else if (!strcmp(ctype
, "passcode"))
948 cparam
->type
= PPD_CUSTOM_PASSCODE
;
949 cparam
->minimum
.custom_passcode
= atoi(cminimum
);
950 cparam
->maximum
.custom_passcode
= atoi(cmaximum
);
952 else if (!strcmp(ctype
, "password"))
954 cparam
->type
= PPD_CUSTOM_PASSWORD
;
955 cparam
->minimum
.custom_password
= atoi(cminimum
);
956 cparam
->maximum
.custom_password
= atoi(cmaximum
);
958 else if (!strcmp(ctype
, "points"))
960 cparam
->type
= PPD_CUSTOM_POINTS
;
961 cparam
->minimum
.custom_points
= (float)_cupsStrScand(cminimum
, NULL
, loc
);
962 cparam
->maximum
.custom_points
= (float)_cupsStrScand(cmaximum
, NULL
, loc
);
964 else if (!strcmp(ctype
, "real"))
966 cparam
->type
= PPD_CUSTOM_REAL
;
967 cparam
->minimum
.custom_real
= (float)_cupsStrScand(cminimum
, NULL
, loc
);
968 cparam
->maximum
.custom_real
= (float)_cupsStrScand(cmaximum
, NULL
, loc
);
970 else if (!strcmp(ctype
, "string"))
972 cparam
->type
= PPD_CUSTOM_STRING
;
973 cparam
->minimum
.custom_string
= atoi(cminimum
);
974 cparam
->maximum
.custom_string
= atoi(cmaximum
);
978 cg
->ppd_status
= PPD_BAD_CUSTOM_PARAM
;
984 * Now special-case for CustomPageSize...
987 if (!strcmp(coption
->keyword
, "PageSize"))
989 if (!strcmp(name
, "Width"))
991 ppd
->custom_min
[0] = cparam
->minimum
.custom_points
;
992 ppd
->custom_max
[0] = cparam
->maximum
.custom_points
;
994 else if (!strcmp(name
, "Height"))
996 ppd
->custom_min
[1] = cparam
->minimum
.custom_points
;
997 ppd
->custom_max
[1] = cparam
->maximum
.custom_points
;
1001 else if (!strcmp(keyword
, "HWMargins"))
1003 for (i
= 0, sptr
= string
; i
< 4; i
++)
1004 ppd
->custom_margins
[i
] = (float)_cupsStrScand(sptr
, &sptr
, loc
);
1006 else if (!strncmp(keyword
, "Custom", 6) && !strcmp(name
, "True") && !option
)
1008 ppd_option_t
*custom_option
; /* Custom option */
1010 DEBUG_puts("Processing Custom option...");
1013 * Get the option and custom option...
1016 if (!ppd_get_coption(ppd
, keyword
+ 6))
1018 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1023 if (option
&& !strcasecmp(option
->keyword
, keyword
+ 6))
1024 custom_option
= option
;
1026 custom_option
= ppdFindOption(ppd
, keyword
+ 6);
1031 * Add the "custom" option...
1034 if ((choice
= ppd_add_choice(custom_option
, "Custom")) == NULL
)
1036 DEBUG_puts("Unable to add Custom choice!");
1038 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1043 strlcpy(choice
->text
, text
[0] ? text
: _("Custom"),
1044 sizeof(choice
->text
));
1046 choice
->code
= _cupsStrAlloc(string
);
1050 * Now process custom page sizes specially...
1053 if (!strcmp(keyword
, "CustomPageSize"))
1056 * Add a "Custom" page size entry...
1059 ppd
->variable_sizes
= 1;
1061 ppd_add_size(ppd
, "Custom");
1063 if (option
&& !strcasecmp(option
->keyword
, "PageRegion"))
1064 custom_option
= option
;
1066 custom_option
= ppdFindOption(ppd
, "PageRegion");
1070 if ((choice
= ppd_add_choice(custom_option
, "Custom")) == NULL
)
1072 DEBUG_puts("Unable to add Custom choice!");
1074 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1079 strlcpy(choice
->text
, text
[0] ? text
: _("Custom"),
1080 sizeof(choice
->text
));
1084 else if (!strcmp(keyword
, "LandscapeOrientation"))
1086 if (!strcmp(string
, "Minus90"))
1087 ppd
->landscape
= -90;
1088 else if (!strcmp(string
, "Plus90"))
1089 ppd
->landscape
= 90;
1091 else if (!strcmp(keyword
, "Emulators"))
1093 for (count
= 1, sptr
= string
; sptr
!= NULL
;)
1094 if ((sptr
= strchr(sptr
, ' ')) != NULL
)
1097 while (*sptr
== ' ')
1101 ppd
->num_emulations
= count
;
1102 if ((ppd
->emulations
= calloc(count
, sizeof(ppd_emul_t
))) == NULL
)
1104 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1109 for (i
= 0, sptr
= string
; i
< count
; i
++)
1111 for (nameptr
= ppd
->emulations
[i
].name
;
1112 *sptr
!= '\0' && *sptr
!= ' ';
1114 if (nameptr
< (ppd
->emulations
[i
].name
+ sizeof(ppd
->emulations
[i
].name
) - 1))
1119 while (*sptr
== ' ')
1123 else if (!strncmp(keyword
, "StartEmulator_", 14))
1127 for (i
= 0; i
< ppd
->num_emulations
; i
++)
1128 if (!strcmp(keyword
+ 14, ppd
->emulations
[i
].name
))
1130 ppd
->emulations
[i
].start
= string
;
1134 else if (!strncmp(keyword
, "StopEmulator_", 13))
1138 for (i
= 0; i
< ppd
->num_emulations
; i
++)
1139 if (!strcmp(keyword
+ 13, ppd
->emulations
[i
].name
))
1141 ppd
->emulations
[i
].stop
= string
;
1145 else if (!strcmp(keyword
, "JobPatchFile"))
1147 if (ppd
->patches
== NULL
)
1148 ppd
->patches
= _cupsStrAlloc(string
);
1151 temp
= realloc(ppd
->patches
, strlen(ppd
->patches
) +
1152 strlen(string
) + 1);
1155 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1160 ppd
->patches
= temp
;
1162 strcpy(ppd
->patches
+ strlen(ppd
->patches
), string
);
1165 else if (!strcmp(keyword
, "OpenUI"))
1168 * Don't allow nesting of options...
1171 if (option
&& cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1173 cg
->ppd_status
= PPD_NESTED_OPEN_UI
;
1179 * Add an option record to the current sub-group, group, or file...
1182 DEBUG_printf(("name=\"%s\" (%d)\n", name
, (int)strlen(name
)));
1185 _cups_strcpy(name
, name
+ 1); /* Eliminate leading asterisk */
1187 for (i
= (int)strlen(name
) - 1; i
> 0 && isspace(name
[i
] & 255); i
--)
1188 name
[i
] = '\0'; /* Eliminate trailing spaces */
1190 DEBUG_printf(("OpenUI of %s in group %s...\n", name
,
1191 group
? group
->text
: "(null)"));
1193 if (subgroup
!= NULL
)
1194 option
= ppd_get_option(subgroup
, name
);
1195 else if (group
== NULL
)
1197 if ((group
= ppd_get_group(ppd
, "General", _("General"), cg
,
1201 DEBUG_printf(("Adding to group %s...\n", group
->text
));
1202 option
= ppd_get_option(group
, name
);
1206 option
= ppd_get_option(group
, name
);
1210 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1216 * Now fill in the initial information for the option...
1219 if (string
&& !strcmp(string
, "PickMany"))
1220 option
->ui
= PPD_UI_PICKMANY
;
1221 else if (string
&& !strcmp(string
, "Boolean"))
1222 option
->ui
= PPD_UI_BOOLEAN
;
1223 else if (string
&& !strcmp(string
, "PickOne"))
1224 option
->ui
= PPD_UI_PICKONE
;
1225 else if (cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1227 cg
->ppd_status
= PPD_BAD_OPEN_UI
;
1232 option
->ui
= PPD_UI_PICKONE
;
1234 for (j
= 0; j
< ppd
->num_attrs
; j
++)
1235 if (!strncmp(ppd
->attrs
[j
]->name
, "Default", 7) &&
1236 !strcmp(ppd
->attrs
[j
]->name
+ 7, name
) &&
1237 ppd
->attrs
[j
]->value
)
1239 DEBUG_printf(("Setting Default%s to %s via attribute...\n",
1240 option
->keyword
, ppd
->attrs
[j
]->value
));
1241 strlcpy(option
->defchoice
, ppd
->attrs
[j
]->value
,
1242 sizeof(option
->defchoice
));
1247 cupsCharsetToUTF8((cups_utf8_t
*)option
->text
, text
,
1248 sizeof(option
->text
), encoding
);
1251 if (!strcmp(name
, "PageSize"))
1252 strlcpy(option
->text
, _("Media Size"), sizeof(option
->text
));
1253 else if (!strcmp(name
, "MediaType"))
1254 strlcpy(option
->text
, _("Media Type"), sizeof(option
->text
));
1255 else if (!strcmp(name
, "InputSlot"))
1256 strlcpy(option
->text
, _("Media Source"), sizeof(option
->text
));
1257 else if (!strcmp(name
, "ColorModel"))
1258 strlcpy(option
->text
, _("Output Mode"), sizeof(option
->text
));
1259 else if (!strcmp(name
, "Resolution"))
1260 strlcpy(option
->text
, _("Resolution"), sizeof(option
->text
));
1262 strlcpy(option
->text
, name
, sizeof(option
->text
));
1265 option
->section
= PPD_ORDER_ANY
;
1267 _cupsStrFree(string
);
1271 * Add a custom option choice if we have already seen a CustomFoo
1275 if (!strcasecmp(name
, "PageRegion"))
1276 strcpy(custom_name
, "CustomPageSize");
1278 snprintf(custom_name
, sizeof(custom_name
), "Custom%s", name
);
1280 if ((custom_attr
= ppdFindAttr(ppd
, custom_name
, "True")) != NULL
)
1282 if ((choice
= ppd_add_choice(option
, "Custom")) == NULL
)
1284 DEBUG_puts("Unable to add Custom choice!");
1286 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1291 strlcpy(choice
->text
,
1292 custom_attr
->text
[0] ? custom_attr
->text
: _("Custom"),
1293 sizeof(choice
->text
));
1294 choice
->code
= _cupsStrAlloc(custom_attr
->value
);
1297 else if (!strcmp(keyword
, "JCLOpenUI"))
1300 * Don't allow nesting of options...
1303 if (option
&& cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1305 cg
->ppd_status
= PPD_NESTED_OPEN_UI
;
1311 * Find the JCL group, and add if needed...
1314 group
= ppd_get_group(ppd
, "JCL", _("JCL"), cg
, encoding
);
1320 * Add an option record to the current JCLs...
1324 _cups_strcpy(name
, name
+ 1);
1326 option
= ppd_get_option(group
, name
);
1330 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1336 * Now fill in the initial information for the option...
1339 if (string
&& !strcmp(string
, "PickMany"))
1340 option
->ui
= PPD_UI_PICKMANY
;
1341 else if (string
&& !strcmp(string
, "Boolean"))
1342 option
->ui
= PPD_UI_BOOLEAN
;
1343 else if (string
&& !strcmp(string
, "PickOne"))
1344 option
->ui
= PPD_UI_PICKONE
;
1347 cg
->ppd_status
= PPD_BAD_OPEN_UI
;
1352 for (j
= 0; j
< ppd
->num_attrs
; j
++)
1353 if (!strncmp(ppd
->attrs
[j
]->name
, "Default", 7) &&
1354 !strcmp(ppd
->attrs
[j
]->name
+ 7, name
) &&
1355 ppd
->attrs
[j
]->value
)
1357 DEBUG_printf(("Setting Default%s to %s via attribute...\n",
1358 option
->keyword
, ppd
->attrs
[j
]->value
));
1359 strlcpy(option
->defchoice
, ppd
->attrs
[j
]->value
,
1360 sizeof(option
->defchoice
));
1365 cupsCharsetToUTF8((cups_utf8_t
*)option
->text
, text
,
1366 sizeof(option
->text
), encoding
);
1368 strlcpy(option
->text
, name
, sizeof(option
->text
));
1370 option
->section
= PPD_ORDER_JCL
;
1373 _cupsStrFree(string
);
1377 * Add a custom option choice if we have already seen a CustomFoo
1381 snprintf(custom_name
, sizeof(custom_name
), "Custom%s", name
);
1383 if ((custom_attr
= ppdFindAttr(ppd
, custom_name
, "True")) != NULL
)
1385 if ((choice
= ppd_add_choice(option
, "Custom")) == NULL
)
1387 DEBUG_puts("Unable to add Custom choice!");
1389 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1394 strlcpy(choice
->text
,
1395 custom_attr
->text
[0] ? custom_attr
->text
: _("Custom"),
1396 sizeof(choice
->text
));
1397 choice
->code
= _cupsStrAlloc(custom_attr
->value
);
1400 else if (!strcmp(keyword
, "CloseUI") || !strcmp(keyword
, "JCLCloseUI"))
1404 _cupsStrFree(string
);
1407 else if (!strcmp(keyword
, "OpenGroup"))
1410 * Open a new group...
1415 cg
->ppd_status
= PPD_NESTED_OPEN_GROUP
;
1422 cg
->ppd_status
= PPD_BAD_OPEN_GROUP
;
1428 * Separate the group name from the text (name/text)...
1431 if ((sptr
= strchr(string
, '/')) != NULL
)
1437 * Fix up the text...
1443 * Find/add the group...
1446 group
= ppd_get_group(ppd
, string
, sptr
, cg
, encoding
);
1451 _cupsStrFree(string
);
1454 else if (!strcmp(keyword
, "CloseGroup"))
1458 _cupsStrFree(string
);
1461 else if (!strcmp(keyword
, "OrderDependency"))
1463 order
= (float)_cupsStrScand(string
, &sptr
, loc
);
1465 if (!sptr
|| sscanf(sptr
, "%40s%40s", name
, keyword
) != 2)
1467 cg
->ppd_status
= PPD_BAD_ORDER_DEPENDENCY
;
1472 if (keyword
[0] == '*')
1473 _cups_strcpy(keyword
, keyword
+ 1);
1475 if (!strcmp(name
, "ExitServer"))
1476 section
= PPD_ORDER_EXIT
;
1477 else if (!strcmp(name
, "Prolog"))
1478 section
= PPD_ORDER_PROLOG
;
1479 else if (!strcmp(name
, "DocumentSetup"))
1480 section
= PPD_ORDER_DOCUMENT
;
1481 else if (!strcmp(name
, "PageSetup"))
1482 section
= PPD_ORDER_PAGE
;
1483 else if (!strcmp(name
, "JCLSetup"))
1484 section
= PPD_ORDER_JCL
;
1486 section
= PPD_ORDER_ANY
;
1494 * Only valid for Non-UI options...
1497 for (i
= ppd
->num_groups
, gtemp
= ppd
->groups
; i
> 0; i
--, gtemp
++)
1498 if (gtemp
->text
[0] == '\0')
1502 for (i
= 0; i
< gtemp
->num_options
; i
++)
1503 if (!strcmp(keyword
, gtemp
->options
[i
].keyword
))
1505 gtemp
->options
[i
].section
= section
;
1506 gtemp
->options
[i
].order
= order
;
1512 option
->section
= section
;
1513 option
->order
= order
;
1516 _cupsStrFree(string
);
1519 else if (!strncmp(keyword
, "Default", 7))
1525 * Drop UI text, if any, from value...
1528 if (strchr(string
, '/') != NULL
)
1529 *strchr(string
, '/') = '\0';
1532 * Assign the default value as appropriate...
1535 if (!strcmp(keyword
, "DefaultColorSpace"))
1538 * Set default colorspace...
1541 if (!strcmp(string
, "CMY"))
1542 ppd
->colorspace
= PPD_CS_CMY
;
1543 else if (!strcmp(string
, "CMYK"))
1544 ppd
->colorspace
= PPD_CS_CMYK
;
1545 else if (!strcmp(string
, "RGB"))
1546 ppd
->colorspace
= PPD_CS_RGB
;
1547 else if (!strcmp(string
, "RGBK"))
1548 ppd
->colorspace
= PPD_CS_RGBK
;
1549 else if (!strcmp(string
, "N"))
1550 ppd
->colorspace
= PPD_CS_N
;
1552 ppd
->colorspace
= PPD_CS_GRAY
;
1554 else if (option
&& !strcmp(keyword
+ 7, option
->keyword
))
1557 * Set the default as part of the current option...
1560 DEBUG_printf(("Setting %s to %s...\n", keyword
, string
));
1562 strlcpy(option
->defchoice
, string
, sizeof(option
->defchoice
));
1564 DEBUG_printf(("%s is now %s...\n", keyword
, option
->defchoice
));
1569 * Lookup option and set if it has been defined...
1572 ppd_option_t
*toption
; /* Temporary option */
1575 if ((toption
= ppdFindOption(ppd
, keyword
+ 7)) != NULL
)
1577 DEBUG_printf(("Setting %s to %s...\n", keyword
, string
));
1578 strlcpy(toption
->defchoice
, string
, sizeof(toption
->defchoice
));
1582 else if (!strcmp(keyword
, "UIConstraints") ||
1583 !strcmp(keyword
, "NonUIConstraints"))
1585 if (ppd
->num_consts
== 0)
1586 constraint
= calloc(2, sizeof(ppd_const_t
));
1588 constraint
= realloc(ppd
->consts
,
1589 (ppd
->num_consts
+ 2) * sizeof(ppd_const_t
));
1591 if (constraint
== NULL
)
1593 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1598 ppd
->consts
= constraint
;
1599 constraint
+= ppd
->num_consts
;
1602 switch (sscanf(string
, "%40s%40s%40s%40s", constraint
->option1
,
1603 constraint
->choice1
, constraint
->option2
,
1604 constraint
->choice2
))
1606 case 0 : /* Error */
1607 case 1 : /* Error */
1608 cg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1611 case 2 : /* Two options... */
1613 * Check for broken constraints like "* Option"...
1616 if (cg
->ppd_conform
== PPD_CONFORM_STRICT
&&
1617 (!strcmp(constraint
->option1
, "*") ||
1618 !strcmp(constraint
->choice1
, "*")))
1620 cg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1625 * The following strcpy's are safe, as optionN and
1626 * choiceN are all the same size (size defined by PPD spec...)
1629 if (constraint
->option1
[0] == '*')
1630 _cups_strcpy(constraint
->option1
, constraint
->option1
+ 1);
1631 else if (cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1633 cg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1637 if (constraint
->choice1
[0] == '*')
1638 _cups_strcpy(constraint
->option2
, constraint
->choice1
+ 1);
1639 else if (cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1641 cg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1645 constraint
->choice1
[0] = '\0';
1646 constraint
->choice2
[0] = '\0';
1649 case 3 : /* Two options, one choice... */
1651 * Check for broken constraints like "* Option"...
1654 if (cg
->ppd_conform
== PPD_CONFORM_STRICT
&&
1655 (!strcmp(constraint
->option1
, "*") ||
1656 !strcmp(constraint
->choice1
, "*") ||
1657 !strcmp(constraint
->option2
, "*")))
1659 cg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1664 * The following _cups_strcpy's are safe, as optionN and
1665 * choiceN are all the same size (size defined by PPD spec...)
1668 if (constraint
->option1
[0] == '*')
1669 _cups_strcpy(constraint
->option1
, constraint
->option1
+ 1);
1670 else if (cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1672 cg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1676 if (constraint
->choice1
[0] == '*')
1678 if (cg
->ppd_conform
== PPD_CONFORM_STRICT
&&
1679 constraint
->option2
[0] == '*')
1681 cg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1685 _cups_strcpy(constraint
->choice2
, constraint
->option2
);
1686 _cups_strcpy(constraint
->option2
, constraint
->choice1
+ 1);
1687 constraint
->choice1
[0] = '\0';
1691 if (constraint
->option2
[0] == '*')
1692 _cups_strcpy(constraint
->option2
, constraint
->option2
+ 1);
1693 else if (cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1695 cg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1699 constraint
->choice2
[0] = '\0';
1703 case 4 : /* Two options, two choices... */
1705 * Check for broken constraints like "* Option"...
1708 if (cg
->ppd_conform
== PPD_CONFORM_STRICT
&&
1709 (!strcmp(constraint
->option1
, "*") ||
1710 !strcmp(constraint
->choice1
, "*") ||
1711 !strcmp(constraint
->option2
, "*") ||
1712 !strcmp(constraint
->choice2
, "*")))
1714 cg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1718 if (constraint
->option1
[0] == '*')
1719 _cups_strcpy(constraint
->option1
, constraint
->option1
+ 1);
1720 else if (cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1722 cg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1726 if (cg
->ppd_conform
== PPD_CONFORM_STRICT
&&
1727 constraint
->choice1
[0] == '*')
1729 cg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1733 if (constraint
->option2
[0] == '*')
1734 _cups_strcpy(constraint
->option2
, constraint
->option2
+ 1);
1735 else if (cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1737 cg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1741 if (cg
->ppd_conform
== PPD_CONFORM_STRICT
&&
1742 constraint
->choice2
[0] == '*')
1744 cg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1751 * For CustomPageSize and InputSlot/ManualFeed, create a duplicate
1752 * constraint for PageRegion...
1755 if (!strcasecmp(constraint
->option1
, "CustomPageSize") &&
1756 (!strcasecmp(constraint
->option2
, "InputSlot") ||
1757 !strcasecmp(constraint
->option2
, "ManualFeed")))
1761 strcpy(constraint
[1].option1
, "PageRegion");
1762 strcpy(constraint
[1].choice1
, "Custom");
1763 strcpy(constraint
[1].option2
, constraint
->option2
);
1764 strcpy(constraint
[1].choice2
, constraint
->choice2
);
1766 else if (!strcasecmp(constraint
->option2
, "CustomPageSize") &&
1767 (!strcasecmp(constraint
->option1
, "InputSlot") ||
1768 !strcasecmp(constraint
->option1
, "ManualFeed")))
1772 strcpy(constraint
[1].option1
, constraint
->option1
);
1773 strcpy(constraint
[1].choice1
, constraint
->choice1
);
1774 strcpy(constraint
[1].option2
, "PageRegion");
1775 strcpy(constraint
[1].choice2
, "Custom");
1779 * Handle CustomFoo option constraints...
1782 if (!strncasecmp(constraint
->option1
, "Custom", 6) &&
1783 !strcasecmp(constraint
->choice1
, "True"))
1785 _cups_strcpy(constraint
->option1
, constraint
->option1
+ 6);
1786 strcpy(constraint
->choice1
, "Custom");
1789 if (!strncasecmp(constraint
->option2
, "Custom", 6) &&
1790 !strcasecmp(constraint
->choice2
, "True"))
1792 _cups_strcpy(constraint
->option2
, constraint
->option2
+ 6);
1793 strcpy(constraint
->choice2
, "Custom");
1797 * Don't add this one as an attribute...
1800 _cupsStrFree(string
);
1803 else if (!strcmp(keyword
, "PaperDimension"))
1805 if ((size
= ppdPageSize(ppd
, name
)) == NULL
)
1806 size
= ppd_add_size(ppd
, name
);
1811 * Unable to add or find size!
1814 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1819 size
->width
= (float)_cupsStrScand(string
, &sptr
, loc
);
1820 size
->length
= (float)_cupsStrScand(sptr
, NULL
, loc
);
1822 _cupsStrFree(string
);
1825 else if (!strcmp(keyword
, "ImageableArea"))
1827 if ((size
= ppdPageSize(ppd
, name
)) == NULL
)
1828 size
= ppd_add_size(ppd
, name
);
1833 * Unable to add or find size!
1836 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1841 size
->left
= (float)_cupsStrScand(string
, &sptr
, loc
);
1842 size
->bottom
= (float)_cupsStrScand(sptr
, &sptr
, loc
);
1843 size
->right
= (float)_cupsStrScand(sptr
, &sptr
, loc
);
1844 size
->top
= (float)_cupsStrScand(sptr
, NULL
, loc
);
1846 _cupsStrFree(string
);
1849 else if (option
!= NULL
&&
1850 (mask
& (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
)) ==
1851 (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
) &&
1852 !strcmp(keyword
, option
->keyword
))
1854 DEBUG_printf(("group = %p, subgroup = %p\n", group
, subgroup
));
1856 if (!strcmp(keyword
, "PageSize"))
1859 * Add a page size...
1862 if (ppdPageSize(ppd
, name
) == NULL
)
1863 ppd_add_size(ppd
, name
);
1867 * Add the option choice...
1870 if ((choice
= ppd_add_choice(option
, name
)) == NULL
)
1872 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1878 cupsCharsetToUTF8((cups_utf8_t
*)choice
->text
, text
,
1879 sizeof(choice
->text
), encoding
);
1880 else if (!strcmp(name
, "True"))
1881 strcpy(choice
->text
, _("Yes"));
1882 else if (!strcmp(name
, "False"))
1883 strcpy(choice
->text
, _("No"));
1885 strlcpy(choice
->text
, name
, sizeof(choice
->text
));
1887 if (option
->section
== PPD_ORDER_JCL
)
1888 ppd_decode(string
); /* Decode quoted string */
1890 choice
->code
= string
;
1891 string
= NULL
; /* Don't add as an attribute below */
1895 * Add remaining lines with keywords and string values as attributes...
1899 (mask
& (PPD_KEYWORD
| PPD_STRING
)) == (PPD_KEYWORD
| PPD_STRING
))
1900 ppd_add_attr(ppd
, keyword
, name
, text
, string
);
1902 _cupsStrFree(string
);
1909 * Reset language preferences...
1912 cupsLangFree(language
);
1915 if (!cupsFileEOF(fp
))
1916 DEBUG_printf(("Premature EOF at %lu...\n",
1917 (unsigned long)cupsFileTell(fp
)));
1920 if (cg
->ppd_status
!= PPD_OK
)
1923 * Had an error reading the PPD file, cannot continue!
1932 * Create the sorted options array and set the option back-pointer for
1933 * each choice and custom option...
1936 ppd
->options
= cupsArrayNew2((cups_array_func_t
)ppd_compare_options
, NULL
,
1937 (cups_ahash_func_t
)ppd_hash_option
,
1940 for (i
= ppd
->num_groups
, group
= ppd
->groups
;
1944 for (j
= group
->num_options
, option
= group
->options
;
1948 ppd_coption_t
*coption
; /* Custom option */
1951 cupsArrayAdd(ppd
->options
, option
);
1953 for (k
= 0; k
< option
->num_choices
; k
++)
1954 option
->choices
[k
].option
= option
;
1956 if ((coption
= ppdFindCustomOption(ppd
, option
->keyword
)) != NULL
)
1957 coption
->option
= option
;
1962 * Sort the constraints...
1965 if (ppd
->num_consts
> 1)
1966 qsort(ppd
->consts
, ppd
->num_consts
, sizeof(ppd_const_t
),
1967 (int (*)(const void *, const void *))ppd_compare_consts
);
1970 * Create an array to track the marked choices...
1973 ppd
->marked
= cupsArrayNew((cups_array_func_t
)ppd_compare_choices
, NULL
);
1976 * Return the PPD file structure...
1982 * Common exit point for errors to save code size...
1990 _cupsStrFree(string
);
1994 cupsLangFree(language
);
2001 * 'ppdOpenFd()' - Read a PPD file into memory.
2004 ppd_file_t
* /* O - PPD file record or @code NULL@ if the PPD file could not be opened. */
2005 ppdOpenFd(int fd
) /* I - File to read from */
2007 cups_file_t
*fp
; /* CUPS file pointer */
2008 ppd_file_t
*ppd
; /* PPD file record */
2009 _cups_globals_t
*cg
= _cupsGlobals();
2014 * Set the line number to 0...
2020 * Range check input...
2025 cg
->ppd_status
= PPD_NULL_FILE
;
2031 * Try to open the file and parse it...
2034 if ((fp
= cupsFileOpenFd(fd
, "r")) != NULL
)
2042 cg
->ppd_status
= PPD_FILE_OPEN_ERROR
;
2051 * 'ppdOpenFile()' - Read a PPD file into memory.
2054 ppd_file_t
* /* O - PPD file record or @code NULL@ if the PPD file could not be opened. */
2055 ppdOpenFile(const char *filename
) /* I - File to read from */
2057 cups_file_t
*fp
; /* File pointer */
2058 ppd_file_t
*ppd
; /* PPD file record */
2059 _cups_globals_t
*cg
= _cupsGlobals();
2064 * Set the line number to 0...
2070 * Range check input...
2073 if (filename
== NULL
)
2075 cg
->ppd_status
= PPD_NULL_FILE
;
2081 * Try to open the file and parse it...
2084 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
2092 cg
->ppd_status
= PPD_FILE_OPEN_ERROR
;
2101 * 'ppdSetConformance()' - Set the conformance level for PPD files.
2103 * @since CUPS 1.1.20@
2107 ppdSetConformance(ppd_conform_t c
) /* I - Conformance level */
2109 _cups_globals_t
*cg
= _cupsGlobals();
2113 cg
->ppd_conform
= c
;
2118 * 'ppd_add_attr()' - Add an attribute to the PPD data.
2121 static ppd_attr_t
* /* O - New attribute */
2122 ppd_add_attr(ppd_file_t
*ppd
, /* I - PPD file data */
2123 const char *name
, /* I - Attribute name */
2124 const char *spec
, /* I - Specifier string, if any */
2125 const char *text
, /* I - Text string, if any */
2126 const char *value
) /* I - Value of attribute */
2128 ppd_attr_t
**ptr
, /* New array */
2129 *temp
; /* New attribute */
2133 * Range check input...
2136 if (ppd
== NULL
|| name
== NULL
|| spec
== NULL
)
2140 * Create the array as needed...
2143 if (!ppd
->sorted_attrs
)
2144 ppd
->sorted_attrs
= cupsArrayNew((cups_array_func_t
)ppd_compare_attrs
,
2148 * Allocate memory for the new attribute...
2151 if (ppd
->num_attrs
== 0)
2152 ptr
= malloc(sizeof(ppd_attr_t
*));
2154 ptr
= realloc(ppd
->attrs
, (ppd
->num_attrs
+ 1) * sizeof(ppd_attr_t
*));
2160 ptr
+= ppd
->num_attrs
;
2162 if ((temp
= calloc(1, sizeof(ppd_attr_t
))) == NULL
)
2173 strlcpy(temp
->name
, name
, sizeof(temp
->name
));
2174 strlcpy(temp
->spec
, spec
, sizeof(temp
->spec
));
2175 strlcpy(temp
->text
, text
, sizeof(temp
->text
));
2176 temp
->value
= (char *)value
;
2179 * Add the attribute to the sorted array...
2182 cupsArrayAdd(ppd
->sorted_attrs
, temp
);
2185 * Return the attribute...
2193 * 'ppd_add_choice()' - Add a choice to an option.
2196 static ppd_choice_t
* /* O - Named choice */
2197 ppd_add_choice(ppd_option_t
*option
, /* I - Option */
2198 const char *name
) /* I - Name of choice */
2200 ppd_choice_t
*choice
; /* Choice */
2203 if (option
->num_choices
== 0)
2204 choice
= malloc(sizeof(ppd_choice_t
));
2206 choice
= realloc(option
->choices
,
2207 sizeof(ppd_choice_t
) * (option
->num_choices
+ 1));
2212 option
->choices
= choice
;
2213 choice
+= option
->num_choices
;
2214 option
->num_choices
++;
2216 memset(choice
, 0, sizeof(ppd_choice_t
));
2217 strlcpy(choice
->choice
, name
, sizeof(choice
->choice
));
2224 * 'ppd_add_size()' - Add a page size.
2227 static ppd_size_t
* /* O - Named size */
2228 ppd_add_size(ppd_file_t
*ppd
, /* I - PPD file */
2229 const char *name
) /* I - Name of size */
2231 ppd_size_t
*size
; /* Size */
2234 if (ppd
->num_sizes
== 0)
2235 size
= malloc(sizeof(ppd_size_t
));
2237 size
= realloc(ppd
->sizes
, sizeof(ppd_size_t
) * (ppd
->num_sizes
+ 1));
2243 size
+= ppd
->num_sizes
;
2246 memset(size
, 0, sizeof(ppd_size_t
));
2247 strlcpy(size
->name
, name
, sizeof(size
->name
));
2254 * 'ppd_compare_attrs()' - Compare two attributes.
2257 static int /* O - Result of comparison */
2258 ppd_compare_attrs(ppd_attr_t
*a
, /* I - First attribute */
2259 ppd_attr_t
*b
) /* I - Second attribute */
2261 int ret
; /* Result of comparison */
2264 if ((ret
= strcasecmp(a
->name
, b
->name
)) != 0)
2267 return (strcasecmp(a
->spec
, b
->spec
));
2272 * 'ppd_compare_choices()' - Compare two choices...
2275 static int /* O - Result of comparison */
2276 ppd_compare_choices(ppd_choice_t
*a
, /* I - First choice */
2277 ppd_choice_t
*b
) /* I - Second choice */
2279 return (strcmp(a
->option
->keyword
, b
->option
->keyword
));
2284 * 'ppd_compare_consts()' - Compare two constraints.
2287 static int /* O - Result of comparison */
2288 ppd_compare_consts(ppd_const_t
*a
, /* I - First constraint */
2289 ppd_const_t
*b
) /* I - Second constraint */
2291 int ret
; /* Result of comparison */
2294 if ((ret
= strcmp(a
->option1
, b
->option1
)) != 0)
2296 else if ((ret
= strcmp(a
->choice1
, b
->choice1
)) != 0)
2298 else if ((ret
= strcmp(a
->option2
, b
->option2
)) != 0)
2301 return (strcmp(a
->choice2
, b
->choice2
));
2306 * 'ppd_compare_coptions()' - Compare two custom options.
2309 static int /* O - Result of comparison */
2310 ppd_compare_coptions(ppd_coption_t
*a
, /* I - First option */
2311 ppd_coption_t
*b
) /* I - Second option */
2313 return (strcasecmp(a
->keyword
, b
->keyword
));
2318 * 'ppd_compare_cparams()' - Compare two custom parameters.
2321 static int /* O - Result of comparison */
2322 ppd_compare_cparams(ppd_cparam_t
*a
, /* I - First parameter */
2323 ppd_cparam_t
*b
) /* I - Second parameter */
2325 return (strcasecmp(a
->name
, b
->name
));
2330 * 'ppd_compare_options()' - Compare two options.
2333 static int /* O - Result of comparison */
2334 ppd_compare_options(ppd_option_t
*a
, /* I - First option */
2335 ppd_option_t
*b
) /* I - Second option */
2337 return (strcasecmp(a
->keyword
, b
->keyword
));
2342 * 'ppd_decode()' - Decode a string value...
2345 static int /* O - Length of decoded string */
2346 ppd_decode(char *string
) /* I - String to decode */
2348 char *inptr
, /* Input pointer */
2349 *outptr
; /* Output pointer */
2355 while (*inptr
!= '\0')
2356 if (*inptr
== '<' && isxdigit(inptr
[1] & 255))
2359 * Convert hex to 8-bit values...
2363 while (isxdigit(*inptr
& 255))
2365 if (isalpha(*inptr
))
2366 *outptr
= (tolower(*inptr
) - 'a' + 10) << 4;
2368 *outptr
= (*inptr
- '0') << 4;
2372 if (!isxdigit(*inptr
& 255))
2375 if (isalpha(*inptr
))
2376 *outptr
|= tolower(*inptr
) - 'a' + 10;
2378 *outptr
|= *inptr
- '0';
2384 while (*inptr
!= '>' && *inptr
!= '\0')
2386 while (*inptr
== '>')
2390 *outptr
++ = *inptr
++;
2394 return ((int)(outptr
- string
));
2399 * 'ppd_free_group()' - Free a single UI group.
2403 ppd_free_group(ppd_group_t
*group
) /* I - Group to free */
2405 int i
; /* Looping var */
2406 ppd_option_t
*option
; /* Current option */
2407 ppd_group_t
*subgroup
; /* Current sub-group */
2410 if (group
->num_options
> 0)
2412 for (i
= group
->num_options
, option
= group
->options
;
2415 ppd_free_option(option
);
2417 ppd_free(group
->options
);
2420 if (group
->num_subgroups
> 0)
2422 for (i
= group
->num_subgroups
, subgroup
= group
->subgroups
;
2425 ppd_free_group(subgroup
);
2427 ppd_free(group
->subgroups
);
2433 * 'ppd_free_option()' - Free a single option.
2437 ppd_free_option(ppd_option_t
*option
) /* I - Option to free */
2439 int i
; /* Looping var */
2440 ppd_choice_t
*choice
; /* Current choice */
2443 if (option
->num_choices
> 0)
2445 for (i
= option
->num_choices
, choice
= option
->choices
;
2449 _cupsStrFree(choice
->code
);
2452 ppd_free(option
->choices
);
2458 * 'ppd_get_coption()' - Get a custom option record.
2461 static ppd_coption_t
* /* O - Custom option... */
2462 ppd_get_coption(ppd_file_t
*ppd
, /* I - PPD file */
2463 const char *name
) /* I - Name of option */
2465 ppd_coption_t
*copt
; /* New custom option */
2469 * See if the option already exists...
2472 if ((copt
= ppdFindCustomOption(ppd
, name
)) != NULL
)
2476 * Not found, so create the custom option record...
2479 if ((copt
= calloc(1, sizeof(ppd_coption_t
))) == NULL
)
2482 strlcpy(copt
->keyword
, name
, sizeof(copt
->keyword
));
2484 copt
->params
= cupsArrayNew((cups_array_func_t
)ppd_compare_cparams
, NULL
);
2486 cupsArrayAdd(ppd
->coptions
, copt
);
2489 * Return the new record...
2497 * 'ppd_get_cparam()' - Get a custom parameter record.
2500 static ppd_cparam_t
* /* O - Extended option... */
2501 ppd_get_cparam(ppd_coption_t
*opt
, /* I - PPD file */
2502 const char *param
, /* I - Name of parameter */
2503 const char *text
) /* I - Human-readable text */
2505 ppd_cparam_t
*cparam
; /* New custom parameter */
2509 * See if the parameter already exists...
2512 if ((cparam
= ppdFindCustomParam(opt
, param
)) != NULL
)
2516 * Not found, so create the custom parameter record...
2519 if ((cparam
= calloc(1, sizeof(ppd_cparam_t
))) == NULL
)
2522 strlcpy(cparam
->name
, param
, sizeof(cparam
->name
));
2523 strlcpy(cparam
->text
, text
[0] ? text
: param
, sizeof(cparam
->text
));
2526 * Add this record to the array...
2529 cupsArrayAdd(opt
->params
, cparam
);
2532 * Return the new record...
2540 * 'ppd_get_group()' - Find or create the named group as needed.
2543 static ppd_group_t
* /* O - Named group */
2544 ppd_get_group(ppd_file_t
*ppd
, /* I - PPD file */
2545 const char *name
, /* I - Name of group */
2546 const char *text
, /* I - Text for group */
2547 _cups_globals_t
*cg
, /* I - Global data */
2548 cups_encoding_t encoding
) /* I - Encoding of text */
2550 int i
; /* Looping var */
2551 ppd_group_t
*group
; /* Group */
2554 DEBUG_printf(("ppd_get_group(ppd=%p, name=\"%s\", text=\"%s\", cg=%p)\n",
2555 ppd
, name
, text
, cg
));
2557 for (i
= ppd
->num_groups
, group
= ppd
->groups
; i
> 0; i
--, group
++)
2558 if (!strcmp(group
->name
, name
))
2563 DEBUG_printf(("Adding group %s...\n", name
));
2565 if (cg
->ppd_conform
== PPD_CONFORM_STRICT
&& strlen(text
) >= sizeof(group
->text
))
2567 cg
->ppd_status
= PPD_ILLEGAL_TRANSLATION
;
2572 if (ppd
->num_groups
== 0)
2573 group
= malloc(sizeof(ppd_group_t
));
2575 group
= realloc(ppd
->groups
,
2576 (ppd
->num_groups
+ 1) * sizeof(ppd_group_t
));
2580 cg
->ppd_status
= PPD_ALLOC_ERROR
;
2585 ppd
->groups
= group
;
2586 group
+= ppd
->num_groups
;
2589 memset(group
, 0, sizeof(ppd_group_t
));
2590 strlcpy(group
->name
, name
, sizeof(group
->name
));
2592 cupsCharsetToUTF8((cups_utf8_t
*)group
->text
, text
,
2593 sizeof(group
->text
), encoding
);
2601 * 'ppd_get_option()' - Find or create the named option as needed.
2604 static ppd_option_t
* /* O - Named option */
2605 ppd_get_option(ppd_group_t
*group
, /* I - Group */
2606 const char *name
) /* I - Name of option */
2608 int i
; /* Looping var */
2609 ppd_option_t
*option
; /* Option */
2612 DEBUG_printf(("ppd_get_option(group=%p(\"%s\"), name=\"%s\")\n",
2613 group
, group
->name
, name
));
2615 for (i
= group
->num_options
, option
= group
->options
; i
> 0; i
--, option
++)
2616 if (!strcmp(option
->keyword
, name
))
2621 if (group
->num_options
== 0)
2622 option
= malloc(sizeof(ppd_option_t
));
2624 option
= realloc(group
->options
,
2625 (group
->num_options
+ 1) * sizeof(ppd_option_t
));
2630 group
->options
= option
;
2631 option
+= group
->num_options
;
2632 group
->num_options
++;
2634 memset(option
, 0, sizeof(ppd_option_t
));
2635 strlcpy(option
->keyword
, name
, sizeof(option
->keyword
));
2643 * 'ppd_hash_option()' - Generate a hash of the option name...
2646 static int /* O - Hash index */
2647 ppd_hash_option(ppd_option_t
*option
) /* I - Option */
2649 int hash
= 0; /* Hash index */
2650 const char *k
; /* Pointer into keyword */
2653 for (hash
= option
->keyword
[0], k
= option
->keyword
+ 1; *k
;)
2654 hash
= 33 * hash
+ *k
++;
2656 return (hash
& 511);
2661 * 'ppd_read()' - Read a line from a PPD file, skipping comment lines as
2665 static int /* O - Bitmask of fields read */
2666 ppd_read(cups_file_t
*fp
, /* I - File to read from */
2667 _ppd_line_t
*line
, /* I - Line buffer */
2668 char *keyword
, /* O - Keyword from line */
2669 char *option
, /* O - Option from line */
2670 char *text
, /* O - Human-readable text from line */
2671 char **string
, /* O - Code/string data */
2672 int ignoreblank
, /* I - Ignore blank lines? */
2673 _cups_globals_t
*cg
) /* I - Global data */
2675 int ch
, /* Character from file */
2676 col
, /* Column in line */
2677 colon
, /* Colon seen? */
2678 endquote
, /* Waiting for an end quote */
2679 mask
, /* Mask to be returned */
2680 startline
, /* Start line */
2681 textlen
; /* Length of text */
2682 char *keyptr
, /* Keyword pointer */
2683 *optptr
, /* Option pointer */
2684 *textptr
, /* Text pointer */
2685 *strptr
, /* Pointer into string */
2686 *lineptr
; /* Current position in line buffer */
2690 * Now loop until we have a valid line...
2695 startline
= cg
->ppd_line
+ 1;
2699 line
->bufsize
= 1024;
2700 line
->buffer
= malloc(1024);
2712 lineptr
= line
->buffer
;
2716 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
2718 if (lineptr
>= (line
->buffer
+ line
->bufsize
- 1))
2721 * Expand the line buffer...
2724 char *temp
; /* Temporary line pointer */
2727 line
->bufsize
+= 1024;
2728 if (line
->bufsize
> 262144)
2731 * Don't allow lines longer than 256k!
2734 cg
->ppd_line
= startline
;
2735 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2740 temp
= realloc(line
->buffer
, line
->bufsize
);
2743 cg
->ppd_line
= startline
;
2744 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2749 lineptr
= temp
+ (lineptr
- line
->buffer
);
2750 line
->buffer
= temp
;
2753 if (ch
== '\r' || ch
== '\n')
2756 * Line feed or carriage return...
2765 * Check for a trailing line feed...
2768 if ((ch
= cupsFilePeekChar(fp
)) == EOF
)
2775 cupsFileGetChar(fp
);
2778 if (lineptr
== line
->buffer
&& ignoreblank
)
2779 continue; /* Skip blank lines */
2783 if (!endquote
) /* Continue for multi-line text */
2788 else if (ch
< ' ' && ch
!= '\t' && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2791 * Other control characters...
2794 cg
->ppd_line
= startline
;
2795 cg
->ppd_status
= PPD_ILLEGAL_CHARACTER
;
2799 else if (ch
!= 0x1a)
2802 * Any other character...
2808 if (col
> (PPD_MAX_LINE
- 1))
2811 * Line is too long...
2814 cg
->ppd_line
= startline
;
2815 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2820 if (ch
== ':' && strncmp(line
->buffer
, "*%", 2) != 0)
2823 if (ch
== '\"' && colon
)
2824 endquote
= !endquote
;
2831 * Didn't finish this quoted string...
2834 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
2837 else if (ch
== '\r' || ch
== '\n')
2845 * Check for a trailing line feed...
2848 if ((ch
= cupsFilePeekChar(fp
)) == EOF
)
2851 cupsFileGetChar(fp
);
2856 else if (ch
< ' ' && ch
!= '\t' && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2859 * Other control characters...
2862 cg
->ppd_line
= startline
;
2863 cg
->ppd_status
= PPD_ILLEGAL_CHARACTER
;
2867 else if (ch
!= 0x1a)
2871 if (col
> (PPD_MAX_LINE
- 1))
2874 * Line is too long...
2877 cg
->ppd_line
= startline
;
2878 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2888 * Didn't finish this line...
2891 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
2892 if (ch
== '\r' || ch
== '\n')
2895 * Line feed or carriage return...
2904 * Check for a trailing line feed...
2907 if ((ch
= cupsFilePeekChar(fp
)) == EOF
)
2910 cupsFileGetChar(fp
);
2915 else if (ch
< ' ' && ch
!= '\t' && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2918 * Other control characters...
2921 cg
->ppd_line
= startline
;
2922 cg
->ppd_status
= PPD_ILLEGAL_CHARACTER
;
2926 else if (ch
!= 0x1a)
2930 if (col
> (PPD_MAX_LINE
- 1))
2933 * Line is too long...
2936 cg
->ppd_line
= startline
;
2937 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2944 if (lineptr
> line
->buffer
&& lineptr
[-1] == '\n')
2949 DEBUG_printf(("LINE=\"%s\"\n", line
->buffer
));
2952 * The dynamically created PPDs for older style Mac OS X
2953 * drivers include a large blob of data inserted as comments
2954 * at the end of the file. As an optimization we can stop
2955 * reading the PPD when we get to the start of this data.
2958 if (!strcmp(line
->buffer
, "*%APLWORKSET START"))
2961 if (ch
== EOF
&& lineptr
== line
->buffer
)
2969 lineptr
= line
->buffer
+ 1;
2976 if ((!line
->buffer
[0] || /* Blank line */
2977 !strncmp(line
->buffer
, "*%", 2) || /* Comment line */
2978 !strcmp(line
->buffer
, "*End")) && /* End of multi-line string */
2979 ignoreblank
) /* Ignore these? */
2981 startline
= cg
->ppd_line
+ 1;
2985 if (!strcmp(line
->buffer
, "*")) /* (Bad) comment line */
2987 if (cg
->ppd_conform
== PPD_CONFORM_RELAXED
)
2989 startline
= cg
->ppd_line
+ 1;
2994 cg
->ppd_line
= startline
;
2995 cg
->ppd_status
= PPD_ILLEGAL_MAIN_KEYWORD
;
3001 if (line
->buffer
[0] != '*') /* All lines start with an asterisk */
3004 * Allow lines consisting of just whitespace...
3007 for (lineptr
= line
->buffer
; *lineptr
; lineptr
++)
3008 if (!isspace(*lineptr
& 255))
3013 cg
->ppd_status
= PPD_MISSING_ASTERISK
;
3016 else if (ignoreblank
)
3028 while (*lineptr
!= '\0' && *lineptr
!= ':' && !isspace(*lineptr
& 255))
3030 if (*lineptr
<= ' ' || *lineptr
> 126 || *lineptr
== '/' ||
3031 (keyptr
- keyword
) >= (PPD_MAX_NAME
- 1))
3033 cg
->ppd_status
= PPD_ILLEGAL_MAIN_KEYWORD
;
3037 *keyptr
++ = *lineptr
++;
3042 if (!strcmp(keyword
, "End"))
3045 mask
|= PPD_KEYWORD
;
3047 /* DEBUG_printf(("keyword = \"%s\", lineptr = \"%s\"\n", keyword, lineptr));*/
3049 if (isspace(*lineptr
& 255))
3052 * Get an option name...
3055 while (isspace(*lineptr
& 255))
3060 while (*lineptr
!= '\0' && !isspace(*lineptr
& 255) && *lineptr
!= ':' &&
3063 if (*lineptr
<= ' ' || *lineptr
> 126 ||
3064 (optptr
- option
) >= (PPD_MAX_NAME
- 1))
3066 cg
->ppd_status
= PPD_ILLEGAL_OPTION_KEYWORD
;
3070 *optptr
++ = *lineptr
++;
3075 if (isspace(*lineptr
& 255) && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
3077 cg
->ppd_status
= PPD_ILLEGAL_WHITESPACE
;
3081 while (isspace(*lineptr
& 255))
3086 /* DEBUG_printf(("option = \"%s\", lineptr = \"%s\"\n", option, lineptr));*/
3088 if (*lineptr
== '/')
3091 * Get human-readable text...
3098 while (*lineptr
!= '\0' && *lineptr
!= '\n' && *lineptr
!= ':')
3100 if (((unsigned char)*lineptr
< ' ' && *lineptr
!= '\t') ||
3101 (textptr
- text
) >= (PPD_MAX_LINE
- 1))
3103 cg
->ppd_status
= PPD_ILLEGAL_TRANSLATION
;
3107 *textptr
++ = *lineptr
++;
3111 textlen
= ppd_decode(text
);
3113 if (textlen
> PPD_MAX_TEXT
&& cg
->ppd_conform
== PPD_CONFORM_STRICT
)
3115 cg
->ppd_status
= PPD_ILLEGAL_TRANSLATION
;
3122 /* DEBUG_printf(("text = \"%s\", lineptr = \"%s\"\n", text, lineptr));*/
3125 if (isspace(*lineptr
& 255) && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
3127 cg
->ppd_status
= PPD_ILLEGAL_WHITESPACE
;
3131 while (isspace(*lineptr
& 255))
3134 if (*lineptr
== ':')
3137 * Get string after triming leading and trailing whitespace...
3141 while (isspace(*lineptr
& 255))
3144 strptr
= lineptr
+ strlen(lineptr
) - 1;
3145 while (strptr
>= lineptr
&& isspace(*strptr
& 255))
3148 if (*strptr
== '\"')
3151 * Quoted string by itself, remove quotes...
3158 *string
= _cupsStrAlloc(lineptr
);
3160 /* DEBUG_printf(("string = \"%s\", lineptr = \"%s\"\n", *string, lineptr));*/
3172 * End of "$Id: ppd.c 7552 2008-05-12 17:42:15Z mike $".