2 * "$Id: ppd.c 5302 2006-03-18 00:49:17Z mike $"
4 * PPD file routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * PostScript is a trademark of Adobe Systems, Inc.
26 * This code and any derivative of it may be used and distributed
27 * freely under the terms of the GNU General Public License when
28 * used with GNU Ghostscript or its derivatives. Use of the code
29 * (or any derivative of it) with software other than GNU
30 * GhostScript (or its derivatives) is governed by the CUPS license
33 * This file is subject to the Apple OS-Developed Software exception.
37 * ppdClose() - Free all memory used by the PPD file.
38 * ppdErrorString() - Returns the text assocated with a status.
39 * ppdLastError() - Return the status from the last ppdOpen*().
40 * ppdOpen() - Read a PPD file into memory.
41 * ppdOpen2() - Read a PPD file into memory.
42 * ppdOpenFd() - Read a PPD file into memory.
43 * ppdOpenFile() - Read a PPD file into memory.
44 * ppdSetConformance() - Set the conformance level for PPD files.
45 * ppd_add_attr() - Add an attribute to the PPD data.
46 * ppd_add_choice() - Add a choice to an option.
47 * ppd_add_size() - Add a page size.
48 * ppd_compare_attrs() - Compare two attributes.
49 * ppd_compare_coptions() - Compare two custom options.
50 * ppd_compare_cparams() - Compare two custom parameters.
51 * ppd_compare_options() - Compare two options.
52 * ppd_decode() - Decode a string value...
53 * ppd_free_group() - Free a single UI group.
54 * ppd_free_option() - Free a single option.
55 * ppd_get_coption() - Get a custom option record.
56 * ppd_get_cparam() - Get a custom parameter record.
57 * ppd_get_encoding() - Get the CUPS encoding value for the given
59 * ppd_get_group() - Find or create the named group as needed.
60 * ppd_get_option() - Find or create the named option as needed.
61 * ppd_read() - Read a line from a PPD file, skipping comment
66 * Include necessary headers.
78 #if defined(WIN32) || defined(__EMX__)
79 # define READ_BINARY "rb" /* Open a binary file for reading */
80 # define WRITE_BINARY "wb" /* Open a binary file for writing */
82 # define READ_BINARY "r" /* Open a binary file for reading */
83 # define WRITE_BINARY "w" /* Open a binary file for writing */
84 #endif /* WIN32 || __EMX__ */
86 #define ppd_free(p) if (p) free(p) /* Safe free macro */
88 #define PPD_KEYWORD 1 /* Line contained a keyword */
89 #define PPD_OPTION 2 /* Line contained an option name */
90 #define PPD_TEXT 4 /* Line contained human-readable text */
91 #define PPD_STRING 8 /* Line contained a string or code */
98 static ppd_attr_t
*ppd_add_attr(ppd_file_t
*ppd
, const char *name
,
99 const char *spec
, const char *text
,
101 static ppd_choice_t
*ppd_add_choice(ppd_option_t
*option
, const char *name
);
102 static ppd_size_t
*ppd_add_size(ppd_file_t
*ppd
, const char *name
);
103 static int ppd_compare_attrs(ppd_attr_t
*a
, ppd_attr_t
*b
);
104 static int ppd_compare_coptions(ppd_coption_t
*a
,
106 static int ppd_compare_cparams(ppd_cparam_t
*a
, ppd_cparam_t
*b
);
107 static int ppd_compare_options(ppd_option_t
*a
, ppd_option_t
*b
);
108 static int ppd_decode(char *string
);
109 static void ppd_free_group(ppd_group_t
*group
);
110 static void ppd_free_option(ppd_option_t
*option
);
111 static ppd_coption_t
*ppd_get_coption(ppd_file_t
*ppd
, const char *name
);
112 static ppd_cparam_t
*ppd_get_cparam(ppd_coption_t
*opt
,
115 static cups_encoding_t
ppd_get_encoding(const char *name
);
116 static ppd_group_t
*ppd_get_group(ppd_file_t
*ppd
, const char *name
,
117 const char *text
, _cups_globals_t
*cg
,
118 cups_encoding_t encoding
);
119 static ppd_option_t
*ppd_get_option(ppd_group_t
*group
, const char *name
);
120 static int ppd_read(cups_file_t
*fp
, char *keyword
, char *option
,
121 char *text
, char **string
, int ignoreblank
,
122 _cups_globals_t
*cg
);
126 * 'ppdClose()' - Free all memory used by the PPD file.
130 ppdClose(ppd_file_t
*ppd
) /* I - PPD file record */
132 int i
; /* Looping var */
133 ppd_emul_t
*emul
; /* Current emulation */
134 ppd_group_t
*group
; /* Current group */
135 char **font
; /* Current font */
136 char **filter
; /* Current filter */
137 ppd_attr_t
**attr
; /* Current attribute */
138 ppd_coption_t
*coption
; /* Current custom option */
139 ppd_cparam_t
*cparam
; /* Current custom parameter */
143 * Range check arguments...
150 * Free all strings at the top level...
153 ppd_free(ppd
->lang_encoding
);
154 ppd_free(ppd
->nickname
);
155 ppd_free(ppd
->patches
);
156 ppd_free(ppd
->jcl_begin
);
157 ppd_free(ppd
->jcl_end
);
158 ppd_free(ppd
->jcl_ps
);
161 * Free any emulations...
164 if (ppd
->num_emulations
> 0)
166 for (i
= ppd
->num_emulations
, emul
= ppd
->emulations
; i
> 0; i
--, emul
++)
168 ppd_free(emul
->start
);
169 ppd_free(emul
->stop
);
172 ppd_free(ppd
->emulations
);
176 * Free any UI groups, subgroups, and options...
179 if (ppd
->num_groups
> 0)
181 for (i
= ppd
->num_groups
, group
= ppd
->groups
; i
> 0; i
--, group
++)
182 ppd_free_group(group
);
184 ppd_free(ppd
->groups
);
187 cupsArrayDelete(ppd
->options
);
190 * Free any page sizes...
193 if (ppd
->num_sizes
> 0)
194 ppd_free(ppd
->sizes
);
197 * Free any constraints...
200 if (ppd
->num_consts
> 0)
201 ppd_free(ppd
->consts
);
204 * Free any filters...
207 if (ppd
->num_filters
> 0)
209 for (i
= ppd
->num_filters
, filter
= ppd
->filters
; i
> 0; i
--, filter
++)
214 ppd_free(ppd
->filters
);
221 if (ppd
->num_fonts
> 0)
223 for (i
= ppd
->num_fonts
, font
= ppd
->fonts
; i
> 0; i
--, font
++)
228 ppd_free(ppd
->fonts
);
232 * Free any profiles...
235 if (ppd
->num_profiles
> 0)
236 ppd_free(ppd
->profiles
);
239 * Free any attributes...
242 if (ppd
->num_attrs
> 0)
244 for (i
= ppd
->num_attrs
, attr
= ppd
->attrs
; i
> 0; i
--, attr
++)
246 ppd_free((*attr
)->value
);
250 ppd_free(ppd
->attrs
);
253 cupsArrayDelete(ppd
->sorted_attrs
);
256 * Free custom options...
259 for (coption
= (ppd_coption_t
*)cupsArrayFirst(ppd
->coptions
);
261 coption
= (ppd_coption_t
*)cupsArrayNext(ppd
->coptions
))
263 for (cparam
= (ppd_cparam_t
*)cupsArrayFirst(coption
->params
);
265 cparam
= (ppd_cparam_t
*)cupsArrayNext(coption
->params
))
267 switch (cparam
->type
)
269 case PPD_CUSTOM_PASSCODE
:
270 case PPD_CUSTOM_PASSWORD
:
271 case PPD_CUSTOM_STRING
:
272 ppd_free(cparam
->current
.custom_string
);
282 cupsArrayDelete(coption
->params
);
287 cupsArrayDelete(ppd
->coptions
);
290 * Free the whole record...
298 * 'ppdErrorString()' - Returns the text assocated with a status.
300 * @since CUPS 1.1.19@
303 const char * /* O - Status string */
304 ppdErrorString(ppd_status_t status
) /* I - PPD status */
306 static const char * const messages
[] =/* Status messages */
309 _("Unable to open PPD file"),
310 _("NULL PPD file pointer"),
311 _("Memory allocation error"),
312 _("Missing PPD-Adobe-4.x header"),
313 _("Missing value string"),
316 _("OpenGroup without a CloseGroup first"),
317 _("Bad OpenUI/JCLOpenUI"),
318 _("OpenUI/JCLOpenUI without a CloseUI/JCLCloseUI first"),
319 _("Bad OrderDependency"),
320 _("Bad UIConstraints"),
321 _("Missing asterisk in column 1"),
322 _("Line longer than the maximum allowed (255 characters)"),
323 _("Illegal control character"),
324 _("Illegal main keyword string"),
325 _("Illegal option keyword string"),
326 _("Illegal translation string"),
327 _("Illegal whitespace character"),
328 _("Bad custom parameter")
332 if (status
< PPD_OK
|| status
> PPD_ILLEGAL_WHITESPACE
)
333 return (_cupsLangString(cupsLangDefault(), _("Unknown")));
335 return (_cupsLangString(cupsLangDefault(), messages
[status
]));
340 * 'ppdLastError()' - Return the status from the last ppdOpen*().
342 * @since CUPS 1.1.19@
345 ppd_status_t
/* O - Status code */
346 ppdLastError(int *line
) /* O - Line number */
348 _cups_globals_t
*cg
= _cupsGlobals();
353 *line
= cg
->ppd_line
;
355 return (cg
->ppd_status
);
360 * 'ppdOpen()' - Read a PPD file into memory.
363 ppd_file_t
* /* O - PPD file record */
364 ppdOpen(FILE *fp
) /* I - File to read from */
366 ppd_file_t
*ppd
; /* PPD file record */
367 cups_file_t
*cf
; /* CUPS file */
371 * Reopen the stdio file as a CUPS file...
374 if ((cf
= cupsFileOpenFd(fileno(fp
), "r")) == NULL
)
378 * Load the PPD file using the newer API...
384 * Close the CUPS file and return the PPD...
394 * 'ppdOpen2()' - Read a PPD file into memory.
399 ppd_file_t
* /* O - PPD file record */
400 ppdOpen2(cups_file_t
*fp
) /* I - File to read from */
402 int i
, j
, k
; /* Looping vars */
403 int count
; /* Temporary count */
404 ppd_file_t
*ppd
; /* PPD file record */
405 ppd_group_t
*group
, /* Current group */
406 *subgroup
; /* Current sub-group */
407 ppd_option_t
*option
; /* Current option */
408 ppd_choice_t
*choice
; /* Current choice */
409 ppd_const_t
*constraint
; /* Current constraint */
410 ppd_size_t
*size
; /* Current page size */
411 int mask
; /* Line data mask */
412 char keyword
[PPD_MAX_NAME
],
413 /* Keyword from file */
415 /* Option from file */
417 /* Human-readable text from file */
418 *string
, /* Code/text from file */
419 *sptr
, /* Pointer into string */
420 *nameptr
, /* Pointer into name */
421 *temp
, /* Temporary string pointer */
422 **tempfonts
; /* Temporary fonts pointer */
423 float order
; /* Order dependency number */
424 ppd_section_t section
; /* Order dependency section */
425 ppd_profile_t
*profile
; /* Pointer to color profile */
426 char **filter
; /* Pointer to filter */
427 cups_lang_t
*language
; /* Default language */
428 struct lconv
*loc
; /* Locale data */
429 int ui_keyword
; /* Is this line a UI keyword? */
430 cups_encoding_t encoding
; /* Encoding of PPD file */
431 _cups_globals_t
*cg
= _cupsGlobals();
433 static const char * const ui_keywords
[] =
435 #ifdef CUPS_USE_FULL_UI_KEYWORDS_LIST
437 * Adobe defines some 41 keywords as "UI", meaning that they are
438 * user interface elements and that they should be treated as such
439 * even if the PPD creator doesn't use Open/CloseUI around them.
441 * Since this can cause previously invisible options to appear and
442 * confuse users, the default is to only treat the PageSize and
443 * PageRegion keywords this way.
445 /* Boolean keywords */
455 /* PickOne keywords */
468 "JCLFrameBufferSize",
489 #else /* !CUPS_USE_FULL_UI_KEYWORDS_LIST */
492 #endif /* CUPS_USE_FULL_UI_KEYWORDS_LIST */
497 * Default to "OK" status...
500 cg
->ppd_status
= PPD_OK
;
504 * Range check input...
509 cg
->ppd_status
= PPD_NULL_FILE
;
514 * Grab the first line and make sure it reads '*PPD-Adobe: "major.minor"'...
517 mask
= ppd_read(fp
, keyword
, name
, text
, &string
, 0, cg
);
519 DEBUG_printf(("mask=%x, keyword=\"%s\"...\n", mask
, keyword
));
522 strcmp(keyword
, "PPD-Adobe") ||
523 string
== NULL
|| string
[0] != '4')
526 * Either this is not a PPD file, or it is not a 4.x PPD file.
529 if (cg
->ppd_status
== PPD_OK
)
530 cg
->ppd_status
= PPD_MISSING_PPDADOBE4
;
537 DEBUG_printf(("ppdOpen: keyword = %s, string = %p\n", keyword
, string
));
542 * Allocate memory for the PPD file record...
545 if ((ppd
= calloc(1, sizeof(ppd_file_t
))) == NULL
)
547 cg
->ppd_status
= PPD_ALLOC_ERROR
;
552 ppd
->language_level
= 1;
553 ppd
->color_device
= 0;
554 ppd
->colorspace
= PPD_CS_GRAY
;
555 ppd
->landscape
= -90;
556 ppd
->coptions
= cupsArrayNew((cups_array_func_t
)ppd_compare_coptions
,
560 * Get the default language for the user...
563 language
= cupsLangDefault();
567 * Read lines from the PPD file and add them to the file record...
575 encoding
= CUPS_ISO8859_1
;
577 while ((mask
= ppd_read(fp
, keyword
, name
, text
, &string
, 1, cg
)) != 0)
580 printf("mask = %x, keyword = \"%s\"", mask
, keyword
);
583 printf(", name = \"%s\"", name
);
586 printf(", text = \"%s\"", text
);
590 if (strlen(string
) > 40)
591 printf(", string = %p", string
);
593 printf(", string = \"%s\"", string
);
599 if (strcmp(keyword
, "CloseUI") && strcmp(keyword
, "CloseGroup") &&
600 strcmp(keyword
, "CloseSubGroup") && strncmp(keyword
, "Default", 7) &&
601 strcmp(keyword
, "JCLCloseUI") && strcmp(keyword
, "JCLOpenUI") &&
602 strcmp(keyword
, "OpenUI") && strcmp(keyword
, "OpenGroup") &&
603 strcmp(keyword
, "OpenSubGroup") && string
== NULL
)
606 * Need a string value!
609 cg
->ppd_status
= PPD_MISSING_VALUE
;
615 * Certain main keywords (as defined by the PPD spec) may be used
616 * without the usual OpenUI/CloseUI stuff. Presumably this is just
617 * so that Adobe wouldn't completely break compatibility with PPD
618 * files prior to v4.0 of the spec, but it is hopelessly
619 * inconsistent... Catch these main keywords and automatically
620 * create the corresponding option, as needed...
626 * Previous line was a UI keyword...
633 if (option
== NULL
&&
634 (mask
& (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
)) ==
635 (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
))
637 for (i
= 0; i
< (int)(sizeof(ui_keywords
) / sizeof(ui_keywords
[0])); i
++)
638 if (!strcmp(keyword
, ui_keywords
[i
]))
641 if (i
< (int)(sizeof(ui_keywords
) / sizeof(ui_keywords
[0])))
644 * Create the option in the appropriate group...
649 DEBUG_printf(("**** FOUND ADOBE UI KEYWORD %s WITHOUT OPENUI!\n",
654 if ((group
= ppd_get_group(ppd
, "General", _("General"), cg
,
658 DEBUG_printf(("Adding to group %s...\n", group
->text
));
659 option
= ppd_get_option(group
, keyword
);
663 option
= ppd_get_option(group
, keyword
);
667 cg
->ppd_status
= PPD_ALLOC_ERROR
;
673 * Now fill in the initial information for the option...
676 if (!strncmp(keyword
, "JCL", 3))
677 option
->section
= PPD_ORDER_JCL
;
679 option
->section
= PPD_ORDER_ANY
;
681 option
->order
= 10.0f
;
684 option
->ui
= PPD_UI_BOOLEAN
;
686 option
->ui
= PPD_UI_PICKONE
;
688 for (j
= 0; j
< ppd
->num_attrs
; j
++)
689 if (!strncmp(ppd
->attrs
[j
]->name
, "Default", 7) &&
690 !strcmp(ppd
->attrs
[j
]->name
+ 7, keyword
) &&
691 ppd
->attrs
[j
]->value
)
693 DEBUG_printf(("Setting Default%s to %s via attribute...\n",
694 option
->keyword
, ppd
->attrs
[j
]->value
));
695 strlcpy(option
->defchoice
, ppd
->attrs
[j
]->value
,
696 sizeof(option
->defchoice
));
700 if (!strcmp(keyword
, "PageSize"))
701 strlcpy(option
->text
, _("Media Size"), sizeof(option
->text
));
702 else if (!strcmp(keyword
, "MediaType"))
703 strlcpy(option
->text
, _("Media Type"), sizeof(option
->text
));
704 else if (!strcmp(keyword
, "InputSlot"))
705 strlcpy(option
->text
, _("Media Source"), sizeof(option
->text
));
706 else if (!strcmp(keyword
, "ColorModel"))
707 strlcpy(option
->text
, _("Output Mode"), sizeof(option
->text
));
708 else if (!strcmp(keyword
, "Resolution"))
709 strlcpy(option
->text
, _("Resolution"), sizeof(option
->text
));
711 strlcpy(option
->text
, keyword
, sizeof(option
->text
));
715 if (!strcmp(keyword
, "LanguageLevel"))
716 ppd
->language_level
= atoi(string
);
717 else if (!strcmp(keyword
, "LanguageEncoding"))
720 * Say all PPD files are UTF-8, since we convert to UTF-8...
723 ppd
->lang_encoding
= strdup("UTF-8");
724 encoding
= ppd_get_encoding(string
);
726 else if (!strcmp(keyword
, "LanguageVersion"))
727 ppd
->lang_version
= string
;
728 else if (!strcmp(keyword
, "Manufacturer"))
729 ppd
->manufacturer
= string
;
730 else if (!strcmp(keyword
, "ModelName"))
731 ppd
->modelname
= string
;
732 else if (!strcmp(keyword
, "Protocols"))
733 ppd
->protocols
= string
;
734 else if (!strcmp(keyword
, "PCFileName"))
735 ppd
->pcfilename
= string
;
736 else if (!strcmp(keyword
, "NickName"))
738 if (encoding
!= CUPS_UTF8
)
740 cups_utf8_t utf8
[256]; /* UTF-8 version of NickName */
743 cupsCharsetToUTF8(utf8
, string
, sizeof(utf8
), encoding
);
744 ppd
->nickname
= strdup((char *)utf8
);
747 ppd
->nickname
= strdup(string
);
749 else if (!strcmp(keyword
, "Product"))
750 ppd
->product
= string
;
751 else if (!strcmp(keyword
, "ShortNickName"))
752 ppd
->shortnickname
= string
;
753 else if (!strcmp(keyword
, "TTRasterizer"))
754 ppd
->ttrasterizer
= string
;
755 else if (!strcmp(keyword
, "JCLBegin"))
757 ppd
->jcl_begin
= strdup(string
);
758 ppd_decode(ppd
->jcl_begin
); /* Decode quoted string */
760 else if (!strcmp(keyword
, "JCLEnd"))
762 ppd
->jcl_end
= strdup(string
);
763 ppd_decode(ppd
->jcl_end
); /* Decode quoted string */
765 else if (!strcmp(keyword
, "JCLToPSInterpreter"))
767 ppd
->jcl_ps
= strdup(string
);
768 ppd_decode(ppd
->jcl_ps
); /* Decode quoted string */
770 else if (!strcmp(keyword
, "AccurateScreensSupport"))
771 ppd
->accurate_screens
= !strcmp(string
, "True");
772 else if (!strcmp(keyword
, "ColorDevice"))
773 ppd
->color_device
= !strcmp(string
, "True");
774 else if (!strcmp(keyword
, "ContoneOnly"))
775 ppd
->contone_only
= !strcmp(string
, "True");
776 else if (!strcmp(keyword
, "cupsFlipDuplex"))
777 ppd
->flip_duplex
= !strcmp(string
, "True");
778 else if (!strcmp(keyword
, "cupsManualCopies"))
779 ppd
->manual_copies
= !strcmp(string
, "True");
780 else if (!strcmp(keyword
, "cupsModelNumber"))
781 ppd
->model_number
= atoi(string
);
782 else if (!strcmp(keyword
, "cupsColorProfile"))
784 if (ppd
->num_profiles
== 0)
785 profile
= malloc(sizeof(ppd_profile_t
));
787 profile
= realloc(ppd
->profiles
, sizeof(ppd_profile_t
) *
788 (ppd
->num_profiles
+ 1));
790 ppd
->profiles
= profile
;
791 profile
+= ppd
->num_profiles
;
792 ppd
->num_profiles
++;
794 memset(profile
, 0, sizeof(ppd_profile_t
));
795 strlcpy(profile
->resolution
, name
, sizeof(profile
->resolution
));
796 strlcpy(profile
->media_type
, text
, sizeof(profile
->media_type
));
798 profile
->density
= _cupsStrScand(string
, &sptr
, loc
);
799 profile
->gamma
= _cupsStrScand(sptr
, &sptr
, loc
);
800 profile
->matrix
[0][0] = _cupsStrScand(sptr
, &sptr
, loc
);
801 profile
->matrix
[0][1] = _cupsStrScand(sptr
, &sptr
, loc
);
802 profile
->matrix
[0][2] = _cupsStrScand(sptr
, &sptr
, loc
);
803 profile
->matrix
[1][0] = _cupsStrScand(sptr
, &sptr
, loc
);
804 profile
->matrix
[1][1] = _cupsStrScand(sptr
, &sptr
, loc
);
805 profile
->matrix
[1][2] = _cupsStrScand(sptr
, &sptr
, loc
);
806 profile
->matrix
[2][0] = _cupsStrScand(sptr
, &sptr
, loc
);
807 profile
->matrix
[2][1] = _cupsStrScand(sptr
, &sptr
, loc
);
808 profile
->matrix
[2][2] = _cupsStrScand(sptr
, &sptr
, loc
);
810 else if (!strcmp(keyword
, "cupsFilter"))
812 if (ppd
->num_filters
== 0)
813 filter
= malloc(sizeof(char *));
815 filter
= realloc(ppd
->filters
, sizeof(char *) * (ppd
->num_filters
+ 1));
821 cg
->ppd_status
= PPD_ALLOC_ERROR
;
826 ppd
->filters
= filter
;
827 filter
+= ppd
->num_filters
;
831 * Copy filter string and prevent it from being freed below...
837 else if (!strcmp(keyword
, "Throughput"))
838 ppd
->throughput
= atoi(string
);
839 else if (!strcmp(keyword
, "Font"))
842 * Add this font to the list of available fonts...
845 if (ppd
->num_fonts
== 0)
846 tempfonts
= (char **)malloc(sizeof(char *));
848 tempfonts
= (char **)realloc(ppd
->fonts
,
849 sizeof(char *) * (ppd
->num_fonts
+ 1));
851 if (tempfonts
== NULL
)
853 cg
->ppd_status
= PPD_ALLOC_ERROR
;
858 ppd
->fonts
= tempfonts
;
859 ppd
->fonts
[ppd
->num_fonts
] = strdup(name
);
862 else if (!strncmp(keyword
, "ParamCustom", 11))
864 ppd_coption_t
*coption
; /* Custom option */
865 ppd_cparam_t
*cparam
; /* Custom parameter */
866 int corder
; /* Order number */
867 char ctype
[33], /* Data type */
868 cminimum
[65], /* Minimum value */
869 cmaximum
[65]; /* Maximum value */
873 * Get the custom option and parameter...
876 if ((coption
= ppd_get_coption(ppd
, keyword
+ 11)) == NULL
)
878 cg
->ppd_status
= PPD_ALLOC_ERROR
;
883 if ((cparam
= ppd_get_cparam(coption
, name
, text
)) == NULL
)
885 cg
->ppd_status
= PPD_ALLOC_ERROR
;
891 * Get the parameter data...
894 if (sscanf(string
, "%d%32s%64s%64s", &corder
, ctype
, cminimum
,
897 cg
->ppd_status
= PPD_BAD_CUSTOM_PARAM
;
902 cparam
->order
= corder
;
904 if (!strcmp(ctype
, "curve"))
906 cparam
->type
= PPD_CUSTOM_CURVE
;
907 cparam
->minimum
.custom_curve
= _cupsStrScand(cminimum
, NULL
, loc
);
908 cparam
->maximum
.custom_curve
= _cupsStrScand(cmaximum
, NULL
, loc
);
910 else if (!strcmp(ctype
, "int"))
912 cparam
->type
= PPD_CUSTOM_INT
;
913 cparam
->minimum
.custom_int
= atoi(cminimum
);
914 cparam
->maximum
.custom_int
= atoi(cmaximum
);
916 else if (!strcmp(ctype
, "invcurve"))
918 cparam
->type
= PPD_CUSTOM_INVCURVE
;
919 cparam
->minimum
.custom_invcurve
= _cupsStrScand(cminimum
, NULL
, loc
);
920 cparam
->maximum
.custom_invcurve
= _cupsStrScand(cmaximum
, NULL
, loc
);
922 else if (!strcmp(ctype
, "passcode"))
924 cparam
->type
= PPD_CUSTOM_PASSCODE
;
925 cparam
->minimum
.custom_passcode
= atoi(cminimum
);
926 cparam
->maximum
.custom_passcode
= atoi(cmaximum
);
928 else if (!strcmp(ctype
, "password"))
930 cparam
->type
= PPD_CUSTOM_PASSWORD
;
931 cparam
->minimum
.custom_password
= atoi(cminimum
);
932 cparam
->maximum
.custom_password
= atoi(cmaximum
);
934 else if (!strcmp(ctype
, "points"))
936 cparam
->type
= PPD_CUSTOM_POINTS
;
937 cparam
->minimum
.custom_points
= _cupsStrScand(cminimum
, NULL
, loc
);
938 cparam
->maximum
.custom_points
= _cupsStrScand(cmaximum
, NULL
, loc
);
940 else if (!strcmp(ctype
, "real"))
942 cparam
->type
= PPD_CUSTOM_REAL
;
943 cparam
->minimum
.custom_real
= _cupsStrScand(cminimum
, NULL
, loc
);
944 cparam
->maximum
.custom_real
= _cupsStrScand(cmaximum
, NULL
, loc
);
946 else if (!strcmp(ctype
, "string"))
948 cparam
->type
= PPD_CUSTOM_STRING
;
949 cparam
->minimum
.custom_string
= atoi(cminimum
);
950 cparam
->maximum
.custom_string
= atoi(cmaximum
);
954 cg
->ppd_status
= PPD_BAD_CUSTOM_PARAM
;
960 * Now special-case for CustomPageSize...
963 if (!strcmp(coption
->keyword
, "PageSize"))
965 if (!strcmp(name
, "Width"))
967 ppd
->custom_min
[0] = cparam
->minimum
.custom_points
;
968 ppd
->custom_max
[0] = cparam
->maximum
.custom_points
;
970 else if (!strcmp(name
, "Height"))
972 ppd
->custom_min
[1] = cparam
->minimum
.custom_points
;
973 ppd
->custom_max
[1] = cparam
->maximum
.custom_points
;
977 else if (!strcmp(keyword
, "HWMargins"))
979 for (i
= 0, sptr
= string
; i
< 4; i
++)
980 ppd
->custom_margins
[i
] = _cupsStrScand(sptr
, &sptr
, loc
);
982 else if (!strncmp(keyword
, "Custom", 6) && !strcmp(name
, "True"))
984 DEBUG_puts("Processing Custom option...");
987 * Get the option and custom option...
990 if ((option
= ppdFindOption(ppd
, keyword
+ 6)) == NULL
)
992 ppd_group_t
*gtemp
; /* Temporary group */
995 DEBUG_printf(("%s option not found for %s...\n", keyword
+ 6, keyword
));
997 if ((gtemp
= ppd_get_group(ppd
, "General", _("General"), cg
,
1000 DEBUG_puts("Unable to get general group!");
1005 if ((option
= ppd_get_option(gtemp
, keyword
+ 6)) == NULL
)
1007 DEBUG_printf(("Unable to get %s option!\n", keyword
+ 6));
1009 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1015 if (!ppd_get_coption(ppd
, keyword
+ 6))
1017 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1023 * Add the "custom" option...
1026 if ((choice
= ppd_add_choice(option
, "Custom")) == NULL
)
1028 DEBUG_puts("Unable to add Custom choice!");
1030 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1035 strlcpy(choice
->text
, text
[0] ? text
: _("Custom"),
1036 sizeof(choice
->text
));
1038 choice
->code
= string
;
1039 string
= NULL
; /* Don't add as an attribute below */
1043 * Now process custom page sizes specially...
1046 if (!strcmp(keyword
, "CustomPageSize"))
1048 ppd
->variable_sizes
= 1;
1051 * Add a "Custom" page size entry...
1054 ppd_add_size(ppd
, "Custom");
1057 else if (!strcmp(keyword
, "LandscapeOrientation"))
1059 if (!strcmp(string
, "Minus90"))
1060 ppd
->landscape
= -90;
1061 else if (!strcmp(string
, "Plus90"))
1062 ppd
->landscape
= 90;
1064 else if (!strcmp(keyword
, "Emulators"))
1066 for (count
= 1, sptr
= string
; sptr
!= NULL
;)
1067 if ((sptr
= strchr(sptr
, ' ')) != NULL
)
1070 while (*sptr
== ' ')
1074 ppd
->num_emulations
= count
;
1075 ppd
->emulations
= calloc(count
, sizeof(ppd_emul_t
));
1077 for (i
= 0, sptr
= string
; i
< count
; i
++)
1079 for (nameptr
= ppd
->emulations
[i
].name
;
1080 *sptr
!= '\0' && *sptr
!= ' ';
1082 if (nameptr
< (ppd
->emulations
[i
].name
+ sizeof(ppd
->emulations
[i
].name
) - 1))
1087 while (*sptr
== ' ')
1091 else if (!strncmp(keyword
, "StartEmulator_", 14))
1095 for (i
= 0; i
< ppd
->num_emulations
; i
++)
1096 if (!strcmp(keyword
+ 14, ppd
->emulations
[i
].name
))
1098 ppd
->emulations
[i
].start
= string
;
1102 else if (!strncmp(keyword
, "StopEmulator_", 13))
1106 for (i
= 0; i
< ppd
->num_emulations
; i
++)
1107 if (!strcmp(keyword
+ 13, ppd
->emulations
[i
].name
))
1109 ppd
->emulations
[i
].stop
= string
;
1113 else if (!strcmp(keyword
, "JobPatchFile"))
1115 if (ppd
->patches
== NULL
)
1116 ppd
->patches
= strdup(string
);
1119 temp
= realloc(ppd
->patches
, strlen(ppd
->patches
) +
1120 strlen(string
) + 1);
1123 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1128 ppd
->patches
= temp
;
1130 strcpy(ppd
->patches
+ strlen(ppd
->patches
), string
);
1133 else if (!strcmp(keyword
, "OpenUI"))
1136 * Don't allow nesting of options...
1139 if (option
&& cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1141 cg
->ppd_status
= PPD_NESTED_OPEN_UI
;
1147 * Add an option record to the current sub-group, group, or file...
1151 _cups_strcpy(name
, name
+ 1); /* Eliminate leading asterisk */
1153 for (i
= (int)strlen(name
) - 1; i
> 0 && isspace(name
[i
] & 255); i
--)
1154 name
[i
] = '\0'; /* Eliminate trailing spaces */
1156 DEBUG_printf(("OpenUI of %s in group %s...\n", name
,
1157 group
? group
->text
: "(null)"));
1159 if (subgroup
!= NULL
)
1160 option
= ppd_get_option(subgroup
, name
);
1161 else if (group
== NULL
)
1163 if ((group
= ppd_get_group(ppd
, "General", _("General"), cg
,
1167 DEBUG_printf(("Adding to group %s...\n", group
->text
));
1168 option
= ppd_get_option(group
, name
);
1172 option
= ppd_get_option(group
, name
);
1176 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1182 * Now fill in the initial information for the option...
1185 if (string
&& !strcmp(string
, "PickMany"))
1186 option
->ui
= PPD_UI_PICKMANY
;
1187 else if (string
&& !strcmp(string
, "Boolean"))
1188 option
->ui
= PPD_UI_BOOLEAN
;
1189 else if (string
&& !strcmp(string
, "PickOne"))
1190 option
->ui
= PPD_UI_PICKONE
;
1191 else if (cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1193 cg
->ppd_status
= PPD_BAD_OPEN_UI
;
1198 option
->ui
= PPD_UI_PICKONE
;
1200 for (j
= 0; j
< ppd
->num_attrs
; j
++)
1201 if (!strncmp(ppd
->attrs
[j
]->name
, "Default", 7) &&
1202 !strcmp(ppd
->attrs
[j
]->name
+ 7, name
) &&
1203 ppd
->attrs
[j
]->value
)
1205 DEBUG_printf(("Setting Default%s to %s via attribute...\n",
1206 option
->keyword
, ppd
->attrs
[j
]->value
));
1207 strlcpy(option
->defchoice
, ppd
->attrs
[j
]->value
,
1208 sizeof(option
->defchoice
));
1213 cupsCharsetToUTF8((cups_utf8_t
*)option
->text
, text
,
1214 sizeof(option
->text
), encoding
);
1217 if (!strcmp(name
, "PageSize"))
1218 strlcpy(option
->text
, _("Media Size"), sizeof(option
->text
));
1219 else if (!strcmp(name
, "MediaType"))
1220 strlcpy(option
->text
, _("Media Type"), sizeof(option
->text
));
1221 else if (!strcmp(name
, "InputSlot"))
1222 strlcpy(option
->text
, _("Media Source"), sizeof(option
->text
));
1223 else if (!strcmp(name
, "ColorModel"))
1224 strlcpy(option
->text
, _("Output Mode"), sizeof(option
->text
));
1225 else if (!strcmp(name
, "Resolution"))
1226 strlcpy(option
->text
, _("Resolution"), sizeof(option
->text
));
1228 strlcpy(option
->text
, name
, sizeof(option
->text
));
1231 option
->section
= PPD_ORDER_ANY
;
1236 else if (!strcmp(keyword
, "JCLOpenUI"))
1239 * Don't allow nesting of options...
1242 if (option
&& cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1244 cg
->ppd_status
= PPD_NESTED_OPEN_UI
;
1250 * Find the JCL group, and add if needed...
1253 group
= ppd_get_group(ppd
, "JCL", _("JCL"), cg
, encoding
);
1259 * Add an option record to the current JCLs...
1263 _cups_strcpy(name
, name
+ 1);
1265 option
= ppd_get_option(group
, name
);
1269 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1275 * Now fill in the initial information for the option...
1278 if (string
&& !strcmp(string
, "PickMany"))
1279 option
->ui
= PPD_UI_PICKMANY
;
1280 else if (string
&& !strcmp(string
, "Boolean"))
1281 option
->ui
= PPD_UI_BOOLEAN
;
1282 else if (string
&& !strcmp(string
, "PickOne"))
1283 option
->ui
= PPD_UI_PICKONE
;
1286 cg
->ppd_status
= PPD_BAD_OPEN_UI
;
1291 for (j
= 0; j
< ppd
->num_attrs
; j
++)
1292 if (!strncmp(ppd
->attrs
[j
]->name
, "Default", 7) &&
1293 !strcmp(ppd
->attrs
[j
]->name
+ 7, name
) &&
1294 ppd
->attrs
[j
]->value
)
1296 DEBUG_printf(("Setting Default%s to %s via attribute...\n",
1297 option
->keyword
, ppd
->attrs
[j
]->value
));
1298 strlcpy(option
->defchoice
, ppd
->attrs
[j
]->value
,
1299 sizeof(option
->defchoice
));
1304 cupsCharsetToUTF8((cups_utf8_t
*)option
->text
, text
,
1305 sizeof(option
->text
), encoding
);
1307 strlcpy(option
->text
, name
, sizeof(option
->text
));
1309 option
->section
= PPD_ORDER_JCL
;
1315 else if (!strcmp(keyword
, "CloseUI") || !strcmp(keyword
, "JCLCloseUI"))
1322 else if (!strcmp(keyword
, "OpenGroup"))
1325 * Open a new group...
1330 cg
->ppd_status
= PPD_NESTED_OPEN_GROUP
;
1337 cg
->ppd_status
= PPD_BAD_OPEN_GROUP
;
1343 * Separate the group name from the text (name/text)...
1346 if ((sptr
= strchr(string
, '/')) != NULL
)
1352 * Fix up the text...
1358 * Find/add the group...
1361 group
= ppd_get_group(ppd
, string
, sptr
, cg
, encoding
);
1369 else if (!strcmp(keyword
, "CloseGroup"))
1376 else if (!strcmp(keyword
, "OrderDependency") ||
1377 !strcmp(keyword
, "NonUIOrderDependency"))
1379 order
= _cupsStrScand(string
, &sptr
, loc
);
1381 if (!sptr
|| sscanf(sptr
, "%40s%40s", name
, keyword
) != 2)
1383 cg
->ppd_status
= PPD_BAD_ORDER_DEPENDENCY
;
1388 if (keyword
[0] == '*')
1389 _cups_strcpy(keyword
, keyword
+ 1);
1391 if (!strcmp(name
, "ExitServer"))
1392 section
= PPD_ORDER_EXIT
;
1393 else if (!strcmp(name
, "Prolog"))
1394 section
= PPD_ORDER_PROLOG
;
1395 else if (!strcmp(name
, "DocumentSetup"))
1396 section
= PPD_ORDER_DOCUMENT
;
1397 else if (!strcmp(name
, "PageSetup"))
1398 section
= PPD_ORDER_PAGE
;
1399 else if (!strcmp(name
, "JCLSetup"))
1400 section
= PPD_ORDER_JCL
;
1402 section
= PPD_ORDER_ANY
;
1410 * Only valid for Non-UI options...
1413 for (i
= ppd
->num_groups
, gtemp
= ppd
->groups
; i
> 0; i
--, gtemp
++)
1414 if (gtemp
->text
[0] == '\0')
1418 for (i
= 0; i
< gtemp
->num_options
; i
++)
1419 if (!strcmp(keyword
, gtemp
->options
[i
].keyword
))
1421 gtemp
->options
[i
].section
= section
;
1422 gtemp
->options
[i
].order
= order
;
1428 option
->section
= section
;
1429 option
->order
= order
;
1435 else if (!strncmp(keyword
, "Default", 7))
1441 * Drop UI text, if any, from value...
1444 if (strchr(string
, '/') != NULL
)
1445 *strchr(string
, '/') = '\0';
1448 * Assign the default value as appropriate...
1451 if (!strcmp(keyword
, "DefaultColorSpace"))
1454 * Set default colorspace...
1457 if (!strcmp(string
, "CMY"))
1458 ppd
->colorspace
= PPD_CS_CMY
;
1459 else if (!strcmp(string
, "CMYK"))
1460 ppd
->colorspace
= PPD_CS_CMYK
;
1461 else if (!strcmp(string
, "RGB"))
1462 ppd
->colorspace
= PPD_CS_RGB
;
1463 else if (!strcmp(string
, "RGBK"))
1464 ppd
->colorspace
= PPD_CS_RGBK
;
1465 else if (!strcmp(string
, "N"))
1466 ppd
->colorspace
= PPD_CS_N
;
1468 ppd
->colorspace
= PPD_CS_GRAY
;
1470 else if (option
&& !strcmp(keyword
+ 7, option
->keyword
))
1473 * Set the default as part of the current option...
1476 DEBUG_printf(("Setting %s to %s...\n", keyword
, string
));
1478 strlcpy(option
->defchoice
, string
, sizeof(option
->defchoice
));
1480 DEBUG_printf(("%s is now %s...\n", keyword
, option
->defchoice
));
1485 * Lookup option and set if it has been defined...
1488 ppd_option_t
*toption
; /* Temporary option */
1491 if ((toption
= ppdFindOption(ppd
, keyword
+ 7)) != NULL
)
1493 DEBUG_printf(("Setting %s to %s...\n", keyword
, string
));
1494 strlcpy(toption
->defchoice
, string
, sizeof(toption
->defchoice
));
1498 else if (!strcmp(keyword
, "UIConstraints") ||
1499 !strcmp(keyword
, "NonUIConstraints"))
1501 if (ppd
->num_consts
== 0)
1502 constraint
= calloc(1, sizeof(ppd_const_t
));
1504 constraint
= realloc(ppd
->consts
,
1505 (ppd
->num_consts
+ 1) * sizeof(ppd_const_t
));
1507 if (constraint
== NULL
)
1509 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1514 ppd
->consts
= constraint
;
1515 constraint
+= ppd
->num_consts
;
1518 switch (sscanf(string
, "%40s%40s%40s%40s", constraint
->option1
,
1519 constraint
->choice1
, constraint
->option2
,
1520 constraint
->choice2
))
1522 case 0 : /* Error */
1523 case 1 : /* Error */
1524 cg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1527 case 2 : /* Two options... */
1529 * The following strcpy's are safe, as optionN and
1530 * choiceN are all the same size (size defined by PPD spec...)
1533 if (constraint
->option1
[0] == '*')
1534 _cups_strcpy(constraint
->option1
, constraint
->option1
+ 1);
1536 if (constraint
->choice1
[0] == '*')
1537 _cups_strcpy(constraint
->option2
, constraint
->choice1
+ 1);
1539 _cups_strcpy(constraint
->option2
, constraint
->choice1
);
1541 constraint
->choice1
[0] = '\0';
1542 constraint
->choice2
[0] = '\0';
1545 case 3 : /* Two options, one choice... */
1547 * The following _cups_strcpy's are safe, as optionN and
1548 * choiceN are all the same size (size defined by PPD spec...)
1551 if (constraint
->option1
[0] == '*')
1552 _cups_strcpy(constraint
->option1
, constraint
->option1
+ 1);
1554 if (constraint
->choice1
[0] == '*')
1556 _cups_strcpy(constraint
->choice2
, constraint
->option2
);
1557 _cups_strcpy(constraint
->option2
, constraint
->choice1
+ 1);
1558 constraint
->choice1
[0] = '\0';
1562 if (constraint
->option2
[0] == '*')
1563 _cups_strcpy(constraint
->option2
, constraint
->option2
+ 1);
1565 constraint
->choice2
[0] = '\0';
1569 case 4 : /* Two options, two choices... */
1570 if (constraint
->option1
[0] == '*')
1571 _cups_strcpy(constraint
->option1
, constraint
->option1
+ 1);
1573 if (constraint
->option2
[0] == '*')
1574 _cups_strcpy(constraint
->option2
, constraint
->option2
+ 1);
1581 else if (!strcmp(keyword
, "PaperDimension"))
1583 if ((size
= ppdPageSize(ppd
, name
)) == NULL
)
1584 size
= ppd_add_size(ppd
, name
);
1589 * Unable to add or find size!
1592 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1597 size
->width
= _cupsStrScand(string
, &sptr
, loc
);
1598 size
->length
= _cupsStrScand(sptr
, NULL
, loc
);
1603 else if (!strcmp(keyword
, "ImageableArea"))
1605 if ((size
= ppdPageSize(ppd
, name
)) == NULL
)
1606 size
= ppd_add_size(ppd
, name
);
1611 * Unable to add or find size!
1614 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1619 size
->left
= _cupsStrScand(string
, &sptr
, loc
);
1620 size
->bottom
= _cupsStrScand(sptr
, &sptr
, loc
);
1621 size
->right
= _cupsStrScand(sptr
, &sptr
, loc
);
1622 size
->top
= _cupsStrScand(sptr
, NULL
, loc
);
1627 else if (option
!= NULL
&&
1628 (mask
& (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
)) ==
1629 (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
) &&
1630 !strcmp(keyword
, option
->keyword
))
1632 DEBUG_printf(("group = %p, subgroup = %p\n", group
, subgroup
));
1634 if (!strcmp(keyword
, "PageSize"))
1637 * Add a page size...
1640 if (ppdPageSize(ppd
, name
) == NULL
)
1641 ppd_add_size(ppd
, name
);
1645 * Add the option choice...
1648 choice
= ppd_add_choice(option
, name
);
1651 cupsCharsetToUTF8((cups_utf8_t
*)choice
->text
, text
,
1652 sizeof(choice
->text
), encoding
);
1653 else if (!strcmp(name
, "True"))
1654 strcpy(choice
->text
, _("Yes"));
1655 else if (!strcmp(name
, "False"))
1656 strcpy(choice
->text
, _("No"));
1658 strlcpy(choice
->text
, name
, sizeof(choice
->text
));
1660 if (option
->section
== PPD_ORDER_JCL
)
1661 ppd_decode(string
); /* Decode quoted string */
1663 choice
->code
= string
;
1664 string
= NULL
; /* Don't add as an attribute below */
1668 * Add remaining lines with keywords and string values as attributes...
1672 (mask
& (PPD_KEYWORD
| PPD_STRING
)) == (PPD_KEYWORD
| PPD_STRING
))
1673 ppd_add_attr(ppd
, keyword
, name
, text
, string
);
1679 * Reset language preferences...
1682 cupsLangFree(language
);
1686 printf("Premature EOF at %lu...\n", (unsigned long)ftell(fp
));
1689 if (cg
->ppd_status
!= PPD_OK
)
1692 * Had an error reading the PPD file, cannot continue!
1701 * Create the sorted options array and set the option back-pointer for
1702 * each choice and custom option...
1705 ppd
->options
= cupsArrayNew((cups_array_func_t
)ppd_compare_options
, NULL
);
1707 for (i
= ppd
->num_groups
, group
= ppd
->groups
;
1711 for (j
= group
->num_options
, option
= group
->options
;
1715 ppd_coption_t
*coption
; /* Custom option */
1718 cupsArrayAdd(ppd
->options
, option
);
1720 for (k
= 0; k
< option
->num_choices
; k
++)
1721 option
->choices
[k
].option
= option
;
1723 if ((coption
= ppdFindCustomOption(ppd
, option
->keyword
)) != NULL
)
1724 coption
->option
= option
;
1729 * Return the PPD file structure...
1735 * Common exit point for errors to save code size...
1744 cupsLangFree(language
);
1751 * 'ppdOpenFd()' - Read a PPD file into memory.
1754 ppd_file_t
* /* O - PPD file record */
1755 ppdOpenFd(int fd
) /* I - File to read from */
1757 cups_file_t
*fp
; /* CUPS file pointer */
1758 ppd_file_t
*ppd
; /* PPD file record */
1759 _cups_globals_t
*cg
= _cupsGlobals();
1764 * Set the line number to 0...
1770 * Range check input...
1775 cg
->ppd_status
= PPD_NULL_FILE
;
1781 * Try to open the file and parse it...
1784 if ((fp
= cupsFileOpenFd(fd
, "r")) != NULL
)
1792 cg
->ppd_status
= PPD_FILE_OPEN_ERROR
;
1801 * 'ppdOpenFile()' - Read a PPD file into memory.
1804 ppd_file_t
* /* O - PPD file record */
1805 ppdOpenFile(const char *filename
) /* I - File to read from */
1807 cups_file_t
*fp
; /* File pointer */
1808 ppd_file_t
*ppd
; /* PPD file record */
1809 _cups_globals_t
*cg
= _cupsGlobals();
1814 * Set the line number to 0...
1820 * Range check input...
1823 if (filename
== NULL
)
1825 cg
->ppd_status
= PPD_NULL_FILE
;
1831 * Try to open the file and parse it...
1834 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
1842 cg
->ppd_status
= PPD_FILE_OPEN_ERROR
;
1851 * 'ppdSetConformance()' - Set the conformance level for PPD files.
1853 * @since CUPS 1.1.20@
1857 ppdSetConformance(ppd_conform_t c
) /* I - Conformance level */
1859 _cups_globals_t
*cg
= _cupsGlobals();
1863 cg
->ppd_conform
= c
;
1868 * 'ppd_add_attr()' - Add an attribute to the PPD data.
1871 static ppd_attr_t
* /* O - New attribute */
1872 ppd_add_attr(ppd_file_t
*ppd
, /* I - PPD file data */
1873 const char *name
, /* I - Attribute name */
1874 const char *spec
, /* I - Specifier string, if any */
1875 const char *text
, /* I - Text string, if any */
1876 const char *value
) /* I - Value of attribute */
1878 ppd_attr_t
**ptr
, /* New array */
1879 *temp
; /* New attribute */
1883 * Range check input...
1886 if (ppd
== NULL
|| name
== NULL
|| spec
== NULL
)
1890 * Create the array as needed...
1893 if (!ppd
->sorted_attrs
)
1894 ppd
->sorted_attrs
= cupsArrayNew((cups_array_func_t
)ppd_compare_attrs
,
1898 * Allocate memory for the new attribute...
1901 if (ppd
->num_attrs
== 0)
1902 ptr
= malloc(sizeof(ppd_attr_t
*));
1904 ptr
= realloc(ppd
->attrs
, (ppd
->num_attrs
+ 1) * sizeof(ppd_attr_t
*));
1910 ptr
+= ppd
->num_attrs
;
1912 if ((temp
= calloc(1, sizeof(ppd_attr_t
))) == NULL
)
1923 strlcpy(temp
->name
, name
, sizeof(temp
->name
));
1924 strlcpy(temp
->spec
, spec
, sizeof(temp
->spec
));
1925 strlcpy(temp
->text
, text
, sizeof(temp
->text
));
1926 temp
->value
= (char *)value
;
1929 * Add the attribute to the sorted array...
1932 cupsArrayAdd(ppd
->sorted_attrs
, temp
);
1935 * Return the attribute...
1943 * 'ppd_add_choice()' - Add a choice to an option.
1946 static ppd_choice_t
* /* O - Named choice */
1947 ppd_add_choice(ppd_option_t
*option
, /* I - Option */
1948 const char *name
) /* I - Name of choice */
1950 ppd_choice_t
*choice
; /* Choice */
1953 if (option
->num_choices
== 0)
1954 choice
= malloc(sizeof(ppd_choice_t
));
1956 choice
= realloc(option
->choices
,
1957 sizeof(ppd_choice_t
) * (option
->num_choices
+ 1));
1962 option
->choices
= choice
;
1963 choice
+= option
->num_choices
;
1964 option
->num_choices
++;
1966 memset(choice
, 0, sizeof(ppd_choice_t
));
1967 strlcpy(choice
->choice
, name
, sizeof(choice
->choice
));
1974 * 'ppd_add_size()' - Add a page size.
1977 static ppd_size_t
* /* O - Named size */
1978 ppd_add_size(ppd_file_t
*ppd
, /* I - PPD file */
1979 const char *name
) /* I - Name of size */
1981 ppd_size_t
*size
; /* Size */
1984 if (ppd
->num_sizes
== 0)
1985 size
= malloc(sizeof(ppd_size_t
));
1987 size
= realloc(ppd
->sizes
, sizeof(ppd_size_t
) * (ppd
->num_sizes
+ 1));
1993 size
+= ppd
->num_sizes
;
1996 memset(size
, 0, sizeof(ppd_size_t
));
1997 strlcpy(size
->name
, name
, sizeof(size
->name
));
2004 * 'ppd_compare_attrs()' - Compare two attributes.
2007 static int /* O - Result of comparison */
2008 ppd_compare_attrs(ppd_attr_t
*a
, /* I - First attribute */
2009 ppd_attr_t
*b
) /* I - Second attribute */
2011 int ret
; /* Result of comparison */
2014 if ((ret
= strcasecmp(a
->name
, b
->name
)) != 0)
2016 else if (a
->spec
[0] && b
->spec
[0])
2017 return (strcasecmp(a
->spec
, b
->spec
));
2024 * 'ppd_compare_coptions()' - Compare two custom options.
2027 static int /* O - Result of comparison */
2028 ppd_compare_coptions(ppd_coption_t
*a
, /* I - First option */
2029 ppd_coption_t
*b
) /* I - Second option */
2031 return (strcasecmp(a
->keyword
, b
->keyword
));
2036 * 'ppd_compare_cparams()' - Compare two custom parameters.
2039 static int /* O - Result of comparison */
2040 ppd_compare_cparams(ppd_cparam_t
*a
, /* I - First parameter */
2041 ppd_cparam_t
*b
) /* I - Second parameter */
2043 return (strcasecmp(a
->name
, b
->name
));
2048 * 'ppd_compare_options()' - Compare two options.
2051 static int /* O - Result of comparison */
2052 ppd_compare_options(ppd_option_t
*a
, /* I - First option */
2053 ppd_option_t
*b
) /* I - Second option */
2055 return (strcasecmp(a
->keyword
, b
->keyword
));
2060 * 'ppd_decode()' - Decode a string value...
2063 static int /* O - Length of decoded string */
2064 ppd_decode(char *string
) /* I - String to decode */
2066 char *inptr
, /* Input pointer */
2067 *outptr
; /* Output pointer */
2073 while (*inptr
!= '\0')
2074 if (*inptr
== '<' && isxdigit(inptr
[1] & 255))
2077 * Convert hex to 8-bit values...
2081 while (isxdigit(*inptr
& 255))
2083 if (isalpha(*inptr
))
2084 *outptr
= (tolower(*inptr
) - 'a' + 10) << 4;
2086 *outptr
= (*inptr
- '0') << 4;
2090 if (!isxdigit(*inptr
& 255))
2093 if (isalpha(*inptr
))
2094 *outptr
|= tolower(*inptr
) - 'a' + 10;
2096 *outptr
|= *inptr
- '0';
2102 while (*inptr
!= '>' && *inptr
!= '\0')
2104 while (*inptr
== '>')
2108 *outptr
++ = *inptr
++;
2112 return ((int)(outptr
- string
));
2117 * 'ppd_free_group()' - Free a single UI group.
2121 ppd_free_group(ppd_group_t
*group
) /* I - Group to free */
2123 int i
; /* Looping var */
2124 ppd_option_t
*option
; /* Current option */
2125 ppd_group_t
*subgroup
; /* Current sub-group */
2128 if (group
->num_options
> 0)
2130 for (i
= group
->num_options
, option
= group
->options
;
2133 ppd_free_option(option
);
2135 ppd_free(group
->options
);
2138 if (group
->num_subgroups
> 0)
2140 for (i
= group
->num_subgroups
, subgroup
= group
->subgroups
;
2143 ppd_free_group(subgroup
);
2145 ppd_free(group
->subgroups
);
2151 * 'ppd_free_option()' - Free a single option.
2155 ppd_free_option(ppd_option_t
*option
) /* I - Option to free */
2157 int i
; /* Looping var */
2158 ppd_choice_t
*choice
; /* Current choice */
2161 if (option
->num_choices
> 0)
2163 for (i
= option
->num_choices
, choice
= option
->choices
;
2167 ppd_free(choice
->code
);
2170 ppd_free(option
->choices
);
2176 * 'ppd_get_coption()' - Get a custom option record.
2179 static ppd_coption_t
* /* O - Custom option... */
2180 ppd_get_coption(ppd_file_t
*ppd
, /* I - PPD file */
2181 const char *name
) /* I - Name of option */
2183 ppd_coption_t
*copt
; /* New custom option */
2187 * See if the option already exists...
2190 if ((copt
= ppdFindCustomOption(ppd
, name
)) != NULL
)
2194 * Not found, so create the custom option record...
2197 if ((copt
= calloc(1, sizeof(ppd_coption_t
))) == NULL
)
2200 strlcpy(copt
->keyword
, name
, sizeof(copt
->keyword
));
2202 copt
->params
= cupsArrayNew((cups_array_func_t
)ppd_compare_cparams
, NULL
);
2204 cupsArrayAdd(ppd
->coptions
, copt
);
2207 * Return the new record...
2215 * 'ppd_get_cparam()' - Get a custom parameter record.
2218 static ppd_cparam_t
* /* O - Extended option... */
2219 ppd_get_cparam(ppd_coption_t
*opt
, /* I - PPD file */
2220 const char *param
, /* I - Name of parameter */
2221 const char *text
) /* I - Human-readable text */
2223 ppd_cparam_t
*cparam
; /* New custom parameter */
2227 * See if the parameter already exists...
2230 if ((cparam
= ppdFindCustomParam(opt
, param
)) != NULL
)
2234 * Not found, so create the custom parameter record...
2237 if ((cparam
= calloc(1, sizeof(ppd_cparam_t
))) == NULL
)
2240 strlcpy(cparam
->name
, param
, sizeof(cparam
->name
));
2241 strlcpy(cparam
->text
, text
, sizeof(cparam
->text
));
2244 * Add this record to the array...
2247 cupsArrayAdd(opt
->params
, cparam
);
2250 * Return the new record...
2258 * 'ppd_get_encoding()' - Get the CUPS encoding value for the given
2262 static cups_encoding_t
/* O - CUPS encoding value */
2263 ppd_get_encoding(const char *name
) /* I - LanguageEncoding string */
2265 if (!strcasecmp(name
, "ISOLatin1"))
2266 return (CUPS_ISO8859_1
);
2267 else if (!strcasecmp(name
, "ISOLatin2"))
2268 return (CUPS_ISO8859_2
);
2269 else if (!strcasecmp(name
, "ISOLatin5"))
2270 return (CUPS_ISO8859_5
);
2271 else if (!strcasecmp(name
, "JIS83-RKSJ"))
2272 return (CUPS_WINDOWS_932
);
2273 else if (!strcasecmp(name
, "MacStandard"))
2274 return (CUPS_MAC_ROMAN
);
2275 else if (!strcasecmp(name
, "WindowsANSI"))
2276 return (CUPS_WINDOWS_1252
);
2283 * 'ppd_get_group()' - Find or create the named group as needed.
2286 static ppd_group_t
* /* O - Named group */
2287 ppd_get_group(ppd_file_t
*ppd
, /* I - PPD file */
2288 const char *name
, /* I - Name of group */
2289 const char *text
, /* I - Text for group */
2290 _cups_globals_t
*cg
, /* I - Global data */
2291 cups_encoding_t encoding
) /* I - Encoding of text */
2293 int i
; /* Looping var */
2294 ppd_group_t
*group
; /* Group */
2297 DEBUG_printf(("ppd_get_group(ppd=%p, name=\"%s\", text=\"%s\", cg=%p)\n",
2298 ppd
, name
, text
, cg
));
2300 for (i
= ppd
->num_groups
, group
= ppd
->groups
; i
> 0; i
--, group
++)
2301 if (!strcmp(group
->name
, name
))
2306 DEBUG_printf(("Adding group %s...\n", name
));
2308 if (cg
->ppd_conform
== PPD_CONFORM_STRICT
&& strlen(text
) >= sizeof(group
->text
))
2310 cg
->ppd_status
= PPD_ILLEGAL_TRANSLATION
;
2315 if (ppd
->num_groups
== 0)
2316 group
= malloc(sizeof(ppd_group_t
));
2318 group
= realloc(ppd
->groups
,
2319 (ppd
->num_groups
+ 1) * sizeof(ppd_group_t
));
2323 cg
->ppd_status
= PPD_ALLOC_ERROR
;
2328 ppd
->groups
= group
;
2329 group
+= ppd
->num_groups
;
2332 memset(group
, 0, sizeof(ppd_group_t
));
2333 strlcpy(group
->name
, name
, sizeof(group
->name
));
2335 cupsCharsetToUTF8((cups_utf8_t
*)group
->text
, text
,
2336 sizeof(group
->text
), encoding
);
2344 * 'ppd_get_option()' - Find or create the named option as needed.
2347 static ppd_option_t
* /* O - Named option */
2348 ppd_get_option(ppd_group_t
*group
, /* I - Group */
2349 const char *name
) /* I - Name of option */
2351 int i
; /* Looping var */
2352 ppd_option_t
*option
; /* Option */
2355 DEBUG_printf(("ppd_get_option(group=%p(\"%s\"), name=\"%s\")\n",
2356 group
, group
->name
, name
));
2358 for (i
= group
->num_options
, option
= group
->options
; i
> 0; i
--, option
++)
2359 if (!strcmp(option
->keyword
, name
))
2364 if (group
->num_options
== 0)
2365 option
= malloc(sizeof(ppd_option_t
));
2367 option
= realloc(group
->options
,
2368 (group
->num_options
+ 1) * sizeof(ppd_option_t
));
2373 group
->options
= option
;
2374 option
+= group
->num_options
;
2375 group
->num_options
++;
2377 memset(option
, 0, sizeof(ppd_option_t
));
2378 strlcpy(option
->keyword
, name
, sizeof(option
->keyword
));
2386 * 'ppd_read()' - Read a line from a PPD file, skipping comment lines as
2390 static int /* O - Bitmask of fields read */
2391 ppd_read(cups_file_t
*fp
, /* I - File to read from */
2392 char *keyword
, /* O - Keyword from line */
2393 char *option
, /* O - Option from line */
2394 char *text
, /* O - Human-readable text from line */
2395 char **string
, /* O - Code/string data */
2396 int ignoreblank
, /* I - Ignore blank lines? */
2397 _cups_globals_t
*cg
) /* I - Global data */
2399 int ch
, /* Character from file */
2400 col
, /* Column in line */
2401 colon
, /* Colon seen? */
2402 endquote
, /* Waiting for an end quote */
2403 mask
, /* Mask to be returned */
2404 startline
, /* Start line */
2405 textlen
; /* Length of text */
2406 char *keyptr
, /* Keyword pointer */
2407 *optptr
, /* Option pointer */
2408 *textptr
, /* Text pointer */
2409 *strptr
, /* Pointer into string */
2410 *lineptr
, /* Current position in line buffer */
2411 *line
; /* Line buffer */
2412 int linesize
; /* Current size of line buffer */
2415 * Range check everything...
2418 if (!fp
|| !keyword
|| !option
|| !text
|| !string
)
2422 * Now loop until we have a valid line...
2427 startline
= cg
->ppd_line
+ 1;
2429 line
= malloc(linesize
);
2444 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
2446 if (lineptr
>= (line
+ linesize
- 1))
2449 * Expand the line buffer...
2452 char *temp
; /* Temporary line pointer */
2456 if (linesize
> 262144)
2459 * Don't allow lines longer than 256k!
2462 cg
->ppd_line
= startline
;
2463 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2470 temp
= realloc(line
, linesize
);
2473 cg
->ppd_line
= startline
;
2474 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2481 lineptr
= temp
+ (lineptr
- line
);
2485 if (ch
== '\r' || ch
== '\n')
2488 * Line feed or carriage return...
2497 * Check for a trailing line feed...
2500 if ((ch
= cupsFilePeekChar(fp
)) == EOF
)
2507 cupsFileGetChar(fp
);
2510 if (lineptr
== line
&& ignoreblank
)
2511 continue; /* Skip blank lines */
2515 if (!endquote
) /* Continue for multi-line text */
2520 else if (ch
< ' ' && ch
!= '\t' && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2523 * Other control characters...
2526 cg
->ppd_line
= startline
;
2527 cg
->ppd_status
= PPD_ILLEGAL_CHARACTER
;
2533 else if (ch
!= 0x1a)
2536 * Any other character...
2542 if (col
> (PPD_MAX_LINE
- 1))
2545 * Line is too long...
2548 cg
->ppd_line
= startline
;
2549 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2556 if (ch
== ':' && strncmp(line
, "*%", 2) != 0)
2559 if (ch
== '\"' && colon
)
2560 endquote
= !endquote
;
2567 * Didn't finish this quoted string...
2570 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
2573 else if (ch
== '\r' || ch
== '\n')
2581 * Check for a trailing line feed...
2584 if ((ch
= cupsFilePeekChar(fp
)) == EOF
)
2587 cupsFileGetChar(fp
);
2592 else if (ch
< ' ' && ch
!= '\t' && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2595 * Other control characters...
2598 cg
->ppd_line
= startline
;
2599 cg
->ppd_status
= PPD_ILLEGAL_CHARACTER
;
2605 else if (ch
!= 0x1a)
2609 if (col
> (PPD_MAX_LINE
- 1))
2612 * Line is too long...
2615 cg
->ppd_line
= startline
;
2616 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2628 * Didn't finish this line...
2631 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
2632 if (ch
== '\r' || ch
== '\n')
2635 * Line feed or carriage return...
2644 * Check for a trailing line feed...
2647 if ((ch
= cupsFilePeekChar(fp
)) == EOF
)
2650 cupsFileGetChar(fp
);
2655 else if (ch
< ' ' && ch
!= '\t' && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2658 * Other control characters...
2661 cg
->ppd_line
= startline
;
2662 cg
->ppd_status
= PPD_ILLEGAL_CHARACTER
;
2668 else if (ch
!= 0x1a)
2672 if (col
> (PPD_MAX_LINE
- 1))
2675 * Line is too long...
2678 cg
->ppd_line
= startline
;
2679 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2688 if (lineptr
> line
&& lineptr
[-1] == '\n')
2693 DEBUG_printf(("LINE = \"%s\"\n", line
));
2696 * The dynamically created PPDs for older style Mac OS X
2697 * drivers include a large blob of data inserted as comments
2698 * at the end of the file. As an optimization we can stop
2699 * reading the PPD when we get to the start of this data.
2702 if (!strcmp(line
, "*%APLWORKSET START"))
2708 if (ch
== EOF
&& lineptr
== line
)
2726 if ((!line
[0] || /* Blank line */
2727 !strncmp(line
, "*%", 2) || /* Comment line */
2728 !strcmp(line
, "*End")) && /* End of multi-line string */
2729 ignoreblank
) /* Ignore these? */
2731 startline
= cg
->ppd_line
+ 1;
2735 if (!strcmp(line
, "*")) /* (Bad) comment line */
2737 if (cg
->ppd_conform
== PPD_CONFORM_RELAXED
)
2739 startline
= cg
->ppd_line
+ 1;
2744 cg
->ppd_line
= startline
;
2745 cg
->ppd_status
= PPD_ILLEGAL_MAIN_KEYWORD
;
2752 if (line
[0] != '*') /* All lines start with an asterisk */
2755 * Allow lines consisting of just whitespace...
2758 for (lineptr
= line
; *lineptr
; lineptr
++)
2759 if (!isspace(*lineptr
& 255))
2764 cg
->ppd_status
= PPD_MISSING_ASTERISK
;
2768 else if (ignoreblank
)
2783 while (*lineptr
!= '\0' && *lineptr
!= ':' && !isspace(*lineptr
& 255))
2785 if (*lineptr
<= ' ' || *lineptr
> 126 || *lineptr
== '/' ||
2786 (keyptr
- keyword
) >= (PPD_MAX_NAME
- 1))
2788 cg
->ppd_status
= PPD_ILLEGAL_MAIN_KEYWORD
;
2793 *keyptr
++ = *lineptr
++;
2798 if (!strcmp(keyword
, "End"))
2801 mask
|= PPD_KEYWORD
;
2803 /* DEBUG_printf(("keyword = \"%s\", lineptr = \"%s\"\n", keyword, lineptr));*/
2805 if (isspace(*lineptr
& 255))
2808 * Get an option name...
2811 while (isspace(*lineptr
& 255))
2816 while (*lineptr
!= '\0' && !isspace(*lineptr
& 255) && *lineptr
!= ':' &&
2819 if (*lineptr
<= ' ' || *lineptr
> 126 ||
2820 (optptr
- option
) >= (PPD_MAX_NAME
- 1))
2822 cg
->ppd_status
= PPD_ILLEGAL_OPTION_KEYWORD
;
2827 *optptr
++ = *lineptr
++;
2832 if (isspace(*lineptr
& 255) && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2834 cg
->ppd_status
= PPD_ILLEGAL_WHITESPACE
;
2839 while (isspace(*lineptr
& 255))
2844 /* DEBUG_printf(("option = \"%s\", lineptr = \"%s\"\n", option, lineptr));*/
2846 if (*lineptr
== '/')
2849 * Get human-readable text...
2856 while (*lineptr
!= '\0' && *lineptr
!= '\n' && *lineptr
!= ':')
2858 if (((unsigned char)*lineptr
< ' ' && *lineptr
!= '\t') ||
2859 (textptr
- text
) >= (PPD_MAX_LINE
- 1))
2861 cg
->ppd_status
= PPD_ILLEGAL_TRANSLATION
;
2866 *textptr
++ = *lineptr
++;
2870 textlen
= ppd_decode(text
);
2872 if (textlen
> PPD_MAX_TEXT
&& cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2874 cg
->ppd_status
= PPD_ILLEGAL_TRANSLATION
;
2882 /* DEBUG_printf(("text = \"%s\", lineptr = \"%s\"\n", text, lineptr));*/
2885 if (isspace(*lineptr
& 255) && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2887 cg
->ppd_status
= PPD_ILLEGAL_WHITESPACE
;
2892 while (isspace(*lineptr
& 255))
2895 if (*lineptr
== ':')
2898 * Get string after triming leading and trailing whitespace...
2902 while (isspace(*lineptr
& 255))
2905 strptr
= lineptr
+ strlen(lineptr
) - 1;
2906 while (strptr
>= lineptr
&& isspace(*strptr
& 255))
2909 if (*strptr
== '\"')
2912 * Quoted string by itself...
2915 *string
= malloc(strlen(lineptr
) + 1);
2919 for (; *lineptr
!= '\0'; lineptr
++)
2920 if (*lineptr
!= '\"')
2921 *strptr
++ = *lineptr
;
2926 *string
= strdup(lineptr
);
2928 /* DEBUG_printf(("string = \"%s\", lineptr = \"%s\"\n", *string, lineptr));*/
2942 * End of "$Id: ppd.c 5302 2006-03-18 00:49:17Z mike $".