2 * "$Id: ppd.c 5238 2006-03-07 04:41:42Z 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_group() - Find or create the named group as needed.
58 * ppd_get_option() - Find or create the named option as needed.
59 * ppd_read() - Read a line from a PPD file, skipping comment
64 * Include necessary headers.
76 #if defined(WIN32) || defined(__EMX__)
77 # define READ_BINARY "rb" /* Open a binary file for reading */
78 # define WRITE_BINARY "wb" /* Open a binary file for writing */
80 # define READ_BINARY "r" /* Open a binary file for reading */
81 # define WRITE_BINARY "w" /* Open a binary file for writing */
82 #endif /* WIN32 || __EMX__ */
84 #define ppd_free(p) if (p) free(p) /* Safe free macro */
86 #define PPD_KEYWORD 1 /* Line contained a keyword */
87 #define PPD_OPTION 2 /* Line contained an option name */
88 #define PPD_TEXT 4 /* Line contained human-readable text */
89 #define PPD_STRING 8 /* Line contained a string or code */
96 static ppd_attr_t
*ppd_add_attr(ppd_file_t
*ppd
, const char *name
,
97 const char *spec
, const char *text
,
99 static ppd_choice_t
*ppd_add_choice(ppd_option_t
*option
, const char *name
);
100 static ppd_size_t
*ppd_add_size(ppd_file_t
*ppd
, const char *name
);
101 static int ppd_compare_attrs(ppd_attr_t
*a
, ppd_attr_t
*b
);
102 static int ppd_compare_coptions(ppd_coption_t
*a
,
104 static int ppd_compare_cparams(ppd_cparam_t
*a
, ppd_cparam_t
*b
);
105 static int ppd_compare_options(ppd_option_t
*a
, ppd_option_t
*b
);
106 static int ppd_decode(char *string
);
107 static void ppd_free_group(ppd_group_t
*group
);
108 static void ppd_free_option(ppd_option_t
*option
);
109 static ppd_coption_t
*ppd_get_coption(ppd_file_t
*ppd
, const char *name
);
110 static ppd_cparam_t
*ppd_get_cparam(ppd_coption_t
*opt
,
113 static ppd_group_t
*ppd_get_group(ppd_file_t
*ppd
, const char *name
,
114 const char *text
, _cups_globals_t
*cg
);
115 static ppd_option_t
*ppd_get_option(ppd_group_t
*group
, const char *name
);
116 static int ppd_read(cups_file_t
*fp
, char *keyword
, char *option
,
117 char *text
, char **string
, int ignoreblank
,
118 _cups_globals_t
*cg
);
122 * 'ppdClose()' - Free all memory used by the PPD file.
126 ppdClose(ppd_file_t
*ppd
) /* I - PPD file record */
128 int i
; /* Looping var */
129 ppd_emul_t
*emul
; /* Current emulation */
130 ppd_group_t
*group
; /* Current group */
131 char **font
; /* Current font */
132 char **filter
; /* Current filter */
133 ppd_attr_t
**attr
; /* Current attribute */
134 ppd_coption_t
*coption
; /* Current custom option */
135 ppd_cparam_t
*cparam
; /* Current custom parameter */
139 * Range check arguments...
146 * Free all strings at the top level...
149 ppd_free(ppd
->patches
);
150 ppd_free(ppd
->jcl_begin
);
151 ppd_free(ppd
->jcl_end
);
152 ppd_free(ppd
->jcl_ps
);
155 * Free any emulations...
158 if (ppd
->num_emulations
> 0)
160 for (i
= ppd
->num_emulations
, emul
= ppd
->emulations
; i
> 0; i
--, emul
++)
162 ppd_free(emul
->start
);
163 ppd_free(emul
->stop
);
166 ppd_free(ppd
->emulations
);
170 * Free any UI groups, subgroups, and options...
173 if (ppd
->num_groups
> 0)
175 for (i
= ppd
->num_groups
, group
= ppd
->groups
; i
> 0; i
--, group
++)
176 ppd_free_group(group
);
178 ppd_free(ppd
->groups
);
181 cupsArrayDelete(ppd
->options
);
184 * Free any page sizes...
187 if (ppd
->num_sizes
> 0)
188 ppd_free(ppd
->sizes
);
191 * Free any constraints...
194 if (ppd
->num_consts
> 0)
195 ppd_free(ppd
->consts
);
198 * Free any filters...
201 if (ppd
->num_filters
> 0)
203 for (i
= ppd
->num_filters
, filter
= ppd
->filters
; i
> 0; i
--, filter
++)
208 ppd_free(ppd
->filters
);
215 if (ppd
->num_fonts
> 0)
217 for (i
= ppd
->num_fonts
, font
= ppd
->fonts
; i
> 0; i
--, font
++)
222 ppd_free(ppd
->fonts
);
226 * Free any profiles...
229 if (ppd
->num_profiles
> 0)
230 ppd_free(ppd
->profiles
);
233 * Free any attributes...
236 if (ppd
->num_attrs
> 0)
238 for (i
= ppd
->num_attrs
, attr
= ppd
->attrs
; i
> 0; i
--, attr
++)
240 ppd_free((*attr
)->value
);
244 ppd_free(ppd
->attrs
);
247 cupsArrayDelete(ppd
->sorted_attrs
);
250 * Free custom options...
253 for (coption
= (ppd_coption_t
*)cupsArrayFirst(ppd
->coptions
);
255 coption
= (ppd_coption_t
*)cupsArrayNext(ppd
->coptions
))
257 for (cparam
= (ppd_cparam_t
*)cupsArrayFirst(coption
->params
);
259 cparam
= (ppd_cparam_t
*)cupsArrayNext(coption
->params
))
261 switch (cparam
->type
)
263 case PPD_CUSTOM_PASSCODE
:
264 case PPD_CUSTOM_PASSWORD
:
265 case PPD_CUSTOM_STRING
:
266 ppd_free(cparam
->current
.custom_string
);
276 cupsArrayDelete(coption
->params
);
281 cupsArrayDelete(ppd
->coptions
);
284 * Free the whole record...
292 * 'ppdErrorString()' - Returns the text assocated with a status.
294 * @since CUPS 1.1.19@
297 const char * /* O - Status string */
298 ppdErrorString(ppd_status_t status
) /* I - PPD status */
300 static const char * const messages
[] =/* Status messages */
303 _("Unable to open PPD file"),
304 _("NULL PPD file pointer"),
305 _("Memory allocation error"),
306 _("Missing PPD-Adobe-4.x header"),
307 _("Missing value string"),
310 _("OpenGroup without a CloseGroup first"),
311 _("Bad OpenUI/JCLOpenUI"),
312 _("OpenUI/JCLOpenUI without a CloseUI/JCLCloseUI first"),
313 _("Bad OrderDependency"),
314 _("Bad UIConstraints"),
315 _("Missing asterisk in column 1"),
316 _("Line longer than the maximum allowed (255 characters)"),
317 _("Illegal control character"),
318 _("Illegal main keyword string"),
319 _("Illegal option keyword string"),
320 _("Illegal translation string"),
321 _("Illegal whitespace character"),
322 _("Bad custom parameter")
326 if (status
< PPD_OK
|| status
> PPD_ILLEGAL_WHITESPACE
)
327 return (_cupsLangString(cupsLangDefault(), _("Unknown")));
329 return (_cupsLangString(cupsLangDefault(), messages
[status
]));
334 * 'ppdLastError()' - Return the status from the last ppdOpen*().
336 * @since CUPS 1.1.19@
339 ppd_status_t
/* O - Status code */
340 ppdLastError(int *line
) /* O - Line number */
342 _cups_globals_t
*cg
= _cupsGlobals();
347 *line
= cg
->ppd_line
;
349 return (cg
->ppd_status
);
354 * 'ppdOpen()' - Read a PPD file into memory.
357 ppd_file_t
* /* O - PPD file record */
358 ppdOpen(FILE *fp
) /* I - File to read from */
360 ppd_file_t
*ppd
; /* PPD file record */
361 cups_file_t
*cf
; /* CUPS file */
365 * Reopen the stdio file as a CUPS file...
368 if ((cf
= cupsFileOpenFd(fileno(fp
), "r")) == NULL
)
372 * Load the PPD file using the newer API...
378 * Close the CUPS file and return the PPD...
388 * 'ppdOpen2()' - Read a PPD file into memory.
393 ppd_file_t
* /* O - PPD file record */
394 ppdOpen2(cups_file_t
*fp
) /* I - File to read from */
396 int i
, j
, k
; /* Looping vars */
397 int count
; /* Temporary count */
398 ppd_file_t
*ppd
; /* PPD file record */
399 ppd_group_t
*group
, /* Current group */
400 *subgroup
; /* Current sub-group */
401 ppd_option_t
*option
; /* Current option */
402 ppd_choice_t
*choice
; /* Current choice */
403 ppd_const_t
*constraint
; /* Current constraint */
404 ppd_size_t
*size
; /* Current page size */
405 int mask
; /* Line data mask */
406 char keyword
[PPD_MAX_NAME
],
407 /* Keyword from file */
409 /* Option from file */
411 /* Human-readable text from file */
412 *string
, /* Code/text from file */
413 *sptr
, /* Pointer into string */
414 *nameptr
, /* Pointer into name */
415 *temp
, /* Temporary string pointer */
416 **tempfonts
; /* Temporary fonts pointer */
417 float order
; /* Order dependency number */
418 ppd_section_t section
; /* Order dependency section */
419 ppd_profile_t
*profile
; /* Pointer to color profile */
420 char **filter
; /* Pointer to filter */
421 cups_lang_t
*language
; /* Default language */
422 struct lconv
*loc
; /* Locale data */
423 int ui_keyword
; /* Is this line a UI keyword? */
424 _cups_globals_t
*cg
= _cupsGlobals();
426 static const char * const ui_keywords
[] =
428 #ifdef CUPS_USE_FULL_UI_KEYWORDS_LIST
430 * Adobe defines some 41 keywords as "UI", meaning that they are
431 * user interface elements and that they should be treated as such
432 * even if the PPD creator doesn't use Open/CloseUI around them.
434 * Since this can cause previously invisible options to appear and
435 * confuse users, the default is to only treat the PageSize and
436 * PageRegion keywords this way.
438 /* Boolean keywords */
448 /* PickOne keywords */
461 "JCLFrameBufferSize",
482 #else /* !CUPS_USE_FULL_UI_KEYWORDS_LIST */
485 #endif /* CUPS_USE_FULL_UI_KEYWORDS_LIST */
490 * Default to "OK" status...
493 cg
->ppd_status
= PPD_OK
;
497 * Range check input...
502 cg
->ppd_status
= PPD_NULL_FILE
;
507 * Grab the first line and make sure it reads '*PPD-Adobe: "major.minor"'...
510 mask
= ppd_read(fp
, keyword
, name
, text
, &string
, 0, cg
);
512 DEBUG_printf(("mask=%x, keyword=\"%s\"...\n", mask
, keyword
));
515 strcmp(keyword
, "PPD-Adobe") ||
516 string
== NULL
|| string
[0] != '4')
519 * Either this is not a PPD file, or it is not a 4.x PPD file.
522 if (cg
->ppd_status
== PPD_OK
)
523 cg
->ppd_status
= PPD_MISSING_PPDADOBE4
;
530 DEBUG_printf(("ppdOpen: keyword = %s, string = %p\n", keyword
, string
));
535 * Allocate memory for the PPD file record...
538 if ((ppd
= calloc(1, sizeof(ppd_file_t
))) == NULL
)
540 cg
->ppd_status
= PPD_ALLOC_ERROR
;
545 ppd
->language_level
= 1;
546 ppd
->color_device
= 0;
547 ppd
->colorspace
= PPD_CS_GRAY
;
548 ppd
->landscape
= -90;
549 ppd
->coptions
= cupsArrayNew((cups_array_func_t
)ppd_compare_coptions
,
553 * Get the default language for the user...
556 language
= cupsLangDefault();
560 * Read lines from the PPD file and add them to the file record...
569 while ((mask
= ppd_read(fp
, keyword
, name
, text
, &string
, 1, cg
)) != 0)
572 printf("mask = %x, keyword = \"%s\"", mask
, keyword
);
575 printf(", name = \"%s\"", name
);
578 printf(", text = \"%s\"", text
);
582 if (strlen(string
) > 40)
583 printf(", string = %p", string
);
585 printf(", string = \"%s\"", string
);
591 if (strcmp(keyword
, "CloseUI") && strcmp(keyword
, "CloseGroup") &&
592 strcmp(keyword
, "CloseSubGroup") && strncmp(keyword
, "Default", 7) &&
593 strcmp(keyword
, "JCLCloseUI") && strcmp(keyword
, "JCLOpenUI") &&
594 strcmp(keyword
, "OpenUI") && strcmp(keyword
, "OpenGroup") &&
595 strcmp(keyword
, "OpenSubGroup") && string
== NULL
)
598 * Need a string value!
601 cg
->ppd_status
= PPD_MISSING_VALUE
;
607 * Certain main keywords (as defined by the PPD spec) may be used
608 * without the usual OpenUI/CloseUI stuff. Presumably this is just
609 * so that Adobe wouldn't completely break compatibility with PPD
610 * files prior to v4.0 of the spec, but it is hopelessly
611 * inconsistent... Catch these main keywords and automatically
612 * create the corresponding option, as needed...
618 * Previous line was a UI keyword...
625 if (option
== NULL
&&
626 (mask
& (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
)) ==
627 (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
))
629 for (i
= 0; i
< (int)(sizeof(ui_keywords
) / sizeof(ui_keywords
[0])); i
++)
630 if (!strcmp(keyword
, ui_keywords
[i
]))
633 if (i
< (int)(sizeof(ui_keywords
) / sizeof(ui_keywords
[0])))
636 * Create the option in the appropriate group...
641 DEBUG_printf(("**** FOUND ADOBE UI KEYWORD %s WITHOUT OPENUI!\n",
646 if ((group
= ppd_get_group(ppd
, "General", _("General"), cg
)) == NULL
)
649 DEBUG_printf(("Adding to group %s...\n", group
->text
));
650 option
= ppd_get_option(group
, keyword
);
654 option
= ppd_get_option(group
, keyword
);
658 cg
->ppd_status
= PPD_ALLOC_ERROR
;
664 * Now fill in the initial information for the option...
667 if (!strncmp(keyword
, "JCL", 3))
668 option
->section
= PPD_ORDER_JCL
;
670 option
->section
= PPD_ORDER_ANY
;
672 option
->order
= 10.0f
;
675 option
->ui
= PPD_UI_BOOLEAN
;
677 option
->ui
= PPD_UI_PICKONE
;
679 for (j
= 0; j
< ppd
->num_attrs
; j
++)
680 if (!strncmp(ppd
->attrs
[j
]->name
, "Default", 7) &&
681 !strcmp(ppd
->attrs
[j
]->name
+ 7, keyword
) &&
682 ppd
->attrs
[j
]->value
)
684 DEBUG_printf(("Setting Default%s to %s via attribute...\n",
685 option
->keyword
, ppd
->attrs
[j
]->value
));
686 strlcpy(option
->defchoice
, ppd
->attrs
[j
]->value
,
687 sizeof(option
->defchoice
));
691 if (!strcmp(keyword
, "PageSize"))
692 strlcpy(option
->text
, _("Media Size"), sizeof(option
->text
));
693 else if (!strcmp(keyword
, "MediaType"))
694 strlcpy(option
->text
, _("Media Type"), sizeof(option
->text
));
695 else if (!strcmp(keyword
, "InputSlot"))
696 strlcpy(option
->text
, _("Media Source"), sizeof(option
->text
));
697 else if (!strcmp(keyword
, "ColorModel"))
698 strlcpy(option
->text
, _("Output Mode"), sizeof(option
->text
));
699 else if (!strcmp(keyword
, "Resolution"))
700 strlcpy(option
->text
, _("Resolution"), sizeof(option
->text
));
702 strlcpy(option
->text
, keyword
, sizeof(option
->text
));
706 if (!strcmp(keyword
, "LanguageLevel"))
707 ppd
->language_level
= atoi(string
);
708 else if (!strcmp(keyword
, "LanguageEncoding"))
709 ppd
->lang_encoding
= string
;
710 else if (!strcmp(keyword
, "LanguageVersion"))
711 ppd
->lang_version
= string
;
712 else if (!strcmp(keyword
, "Manufacturer"))
713 ppd
->manufacturer
= string
;
714 else if (!strcmp(keyword
, "ModelName"))
715 ppd
->modelname
= string
;
716 else if (!strcmp(keyword
, "Protocols"))
717 ppd
->protocols
= string
;
718 else if (!strcmp(keyword
, "PCFileName"))
719 ppd
->pcfilename
= string
;
720 else if (!strcmp(keyword
, "NickName"))
721 ppd
->nickname
= string
;
722 else if (!strcmp(keyword
, "Product"))
723 ppd
->product
= string
;
724 else if (!strcmp(keyword
, "ShortNickName"))
725 ppd
->shortnickname
= string
;
726 else if (!strcmp(keyword
, "TTRasterizer"))
727 ppd
->ttrasterizer
= string
;
728 else if (!strcmp(keyword
, "JCLBegin"))
730 ppd
->jcl_begin
= strdup(string
);
731 ppd_decode(ppd
->jcl_begin
); /* Decode quoted string */
733 else if (!strcmp(keyword
, "JCLEnd"))
735 ppd
->jcl_end
= strdup(string
);
736 ppd_decode(ppd
->jcl_end
); /* Decode quoted string */
738 else if (!strcmp(keyword
, "JCLToPSInterpreter"))
740 ppd
->jcl_ps
= strdup(string
);
741 ppd_decode(ppd
->jcl_ps
); /* Decode quoted string */
743 else if (!strcmp(keyword
, "AccurateScreensSupport"))
744 ppd
->accurate_screens
= !strcmp(string
, "True");
745 else if (!strcmp(keyword
, "ColorDevice"))
746 ppd
->color_device
= !strcmp(string
, "True");
747 else if (!strcmp(keyword
, "ContoneOnly"))
748 ppd
->contone_only
= !strcmp(string
, "True");
749 else if (!strcmp(keyword
, "cupsFlipDuplex"))
750 ppd
->flip_duplex
= !strcmp(string
, "True");
751 else if (!strcmp(keyword
, "cupsManualCopies"))
752 ppd
->manual_copies
= !strcmp(string
, "True");
753 else if (!strcmp(keyword
, "cupsModelNumber"))
754 ppd
->model_number
= atoi(string
);
755 else if (!strcmp(keyword
, "cupsColorProfile"))
757 if (ppd
->num_profiles
== 0)
758 profile
= malloc(sizeof(ppd_profile_t
));
760 profile
= realloc(ppd
->profiles
, sizeof(ppd_profile_t
) *
761 (ppd
->num_profiles
+ 1));
763 ppd
->profiles
= profile
;
764 profile
+= ppd
->num_profiles
;
765 ppd
->num_profiles
++;
767 memset(profile
, 0, sizeof(ppd_profile_t
));
768 strlcpy(profile
->resolution
, name
, sizeof(profile
->resolution
));
769 strlcpy(profile
->media_type
, text
, sizeof(profile
->media_type
));
771 profile
->density
= _cupsStrScand(string
, &sptr
, loc
);
772 profile
->gamma
= _cupsStrScand(sptr
, &sptr
, loc
);
773 profile
->matrix
[0][0] = _cupsStrScand(sptr
, &sptr
, loc
);
774 profile
->matrix
[0][1] = _cupsStrScand(sptr
, &sptr
, loc
);
775 profile
->matrix
[0][2] = _cupsStrScand(sptr
, &sptr
, loc
);
776 profile
->matrix
[1][0] = _cupsStrScand(sptr
, &sptr
, loc
);
777 profile
->matrix
[1][1] = _cupsStrScand(sptr
, &sptr
, loc
);
778 profile
->matrix
[1][2] = _cupsStrScand(sptr
, &sptr
, loc
);
779 profile
->matrix
[2][0] = _cupsStrScand(sptr
, &sptr
, loc
);
780 profile
->matrix
[2][1] = _cupsStrScand(sptr
, &sptr
, loc
);
781 profile
->matrix
[2][2] = _cupsStrScand(sptr
, &sptr
, loc
);
783 else if (!strcmp(keyword
, "cupsFilter"))
785 if (ppd
->num_filters
== 0)
786 filter
= malloc(sizeof(char *));
788 filter
= realloc(ppd
->filters
, sizeof(char *) * (ppd
->num_filters
+ 1));
794 cg
->ppd_status
= PPD_ALLOC_ERROR
;
799 ppd
->filters
= filter
;
800 filter
+= ppd
->num_filters
;
804 * Copy filter string and prevent it from being freed below...
810 else if (!strcmp(keyword
, "Throughput"))
811 ppd
->throughput
= atoi(string
);
812 else if (!strcmp(keyword
, "Font"))
815 * Add this font to the list of available fonts...
818 if (ppd
->num_fonts
== 0)
819 tempfonts
= (char **)malloc(sizeof(char *));
821 tempfonts
= (char **)realloc(ppd
->fonts
,
822 sizeof(char *) * (ppd
->num_fonts
+ 1));
824 if (tempfonts
== NULL
)
826 cg
->ppd_status
= PPD_ALLOC_ERROR
;
831 ppd
->fonts
= tempfonts
;
832 ppd
->fonts
[ppd
->num_fonts
] = strdup(name
);
835 else if (!strncmp(keyword
, "ParamCustom", 11))
837 ppd_coption_t
*coption
; /* Custom option */
838 ppd_cparam_t
*cparam
; /* Custom parameter */
839 int corder
; /* Order number */
840 char ctype
[33], /* Data type */
841 cminimum
[65], /* Minimum value */
842 cmaximum
[65]; /* Maximum value */
846 * Get the custom option and parameter...
849 if ((coption
= ppd_get_coption(ppd
, keyword
+ 11)) == NULL
)
851 cg
->ppd_status
= PPD_ALLOC_ERROR
;
856 if ((cparam
= ppd_get_cparam(coption
, name
, text
)) == NULL
)
858 cg
->ppd_status
= PPD_ALLOC_ERROR
;
864 * Get the parameter data...
867 if (sscanf(string
, "%d%32s%64s%64s", &corder
, ctype
, cminimum
,
870 cg
->ppd_status
= PPD_BAD_CUSTOM_PARAM
;
875 cparam
->order
= corder
;
877 if (!strcmp(ctype
, "curve"))
879 cparam
->type
= PPD_CUSTOM_CURVE
;
880 cparam
->minimum
.custom_curve
= _cupsStrScand(cminimum
, NULL
, loc
);
881 cparam
->maximum
.custom_curve
= _cupsStrScand(cmaximum
, NULL
, loc
);
883 else if (!strcmp(ctype
, "int"))
885 cparam
->type
= PPD_CUSTOM_INT
;
886 cparam
->minimum
.custom_int
= atoi(cminimum
);
887 cparam
->maximum
.custom_int
= atoi(cmaximum
);
889 else if (!strcmp(ctype
, "invcurve"))
891 cparam
->type
= PPD_CUSTOM_INVCURVE
;
892 cparam
->minimum
.custom_invcurve
= _cupsStrScand(cminimum
, NULL
, loc
);
893 cparam
->maximum
.custom_invcurve
= _cupsStrScand(cmaximum
, NULL
, loc
);
895 else if (!strcmp(ctype
, "passcode"))
897 cparam
->type
= PPD_CUSTOM_PASSCODE
;
898 cparam
->minimum
.custom_passcode
= atoi(cminimum
);
899 cparam
->maximum
.custom_passcode
= atoi(cmaximum
);
901 else if (!strcmp(ctype
, "password"))
903 cparam
->type
= PPD_CUSTOM_PASSWORD
;
904 cparam
->minimum
.custom_password
= atoi(cminimum
);
905 cparam
->maximum
.custom_password
= atoi(cmaximum
);
907 else if (!strcmp(ctype
, "points"))
909 cparam
->type
= PPD_CUSTOM_POINTS
;
910 cparam
->minimum
.custom_points
= _cupsStrScand(cminimum
, NULL
, loc
);
911 cparam
->maximum
.custom_points
= _cupsStrScand(cmaximum
, NULL
, loc
);
913 else if (!strcmp(ctype
, "real"))
915 cparam
->type
= PPD_CUSTOM_REAL
;
916 cparam
->minimum
.custom_real
= _cupsStrScand(cminimum
, NULL
, loc
);
917 cparam
->maximum
.custom_real
= _cupsStrScand(cmaximum
, NULL
, loc
);
919 else if (!strcmp(ctype
, "string"))
921 cparam
->type
= PPD_CUSTOM_STRING
;
922 cparam
->minimum
.custom_string
= atoi(cminimum
);
923 cparam
->maximum
.custom_string
= atoi(cmaximum
);
927 cg
->ppd_status
= PPD_BAD_CUSTOM_PARAM
;
933 * Now special-case for CustomPageSize...
936 if (!strcmp(coption
->keyword
, "PageSize"))
938 if (!strcmp(name
, "Width"))
940 ppd
->custom_min
[0] = cparam
->minimum
.custom_points
;
941 ppd
->custom_max
[0] = cparam
->maximum
.custom_points
;
943 else if (!strcmp(name
, "Height"))
945 ppd
->custom_min
[1] = cparam
->minimum
.custom_points
;
946 ppd
->custom_max
[1] = cparam
->maximum
.custom_points
;
950 else if (!strcmp(keyword
, "HWMargins"))
952 for (i
= 0, sptr
= string
; i
< 4; i
++)
953 ppd
->custom_margins
[i
] = _cupsStrScand(sptr
, &sptr
, loc
);
955 else if (!strncmp(keyword
, "Custom", 6) && !strcmp(name
, "True"))
957 ppd_coption_t
*coption
; /* Custom option */
960 DEBUG_puts("Processing Custom option...");
963 * Get the option and custom option...
966 if ((option
= ppdFindOption(ppd
, keyword
+ 6)) == NULL
)
968 ppd_group_t
*gtemp
; /* Temporary group */
971 DEBUG_printf(("%s option not found for %s...\n", keyword
+ 6, keyword
));
973 if ((gtemp
= ppd_get_group(ppd
, "General", _("General"), cg
)) == NULL
)
975 DEBUG_puts("Unable to get general group!");
980 if ((option
= ppd_get_option(gtemp
, keyword
+ 6)) == NULL
)
982 DEBUG_printf(("Unable to get %s option!\n", keyword
+ 6));
984 cg
->ppd_status
= PPD_ALLOC_ERROR
;
990 if ((coption
= ppd_get_coption(ppd
, keyword
+ 6)) == NULL
)
992 cg
->ppd_status
= PPD_ALLOC_ERROR
;
998 * Add the "custom" option...
1001 if ((choice
= ppd_add_choice(option
, "Custom")) == NULL
)
1003 DEBUG_puts("Unable to add Custom choice!");
1005 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1010 strlcpy(choice
->text
, text
[0] ? text
: _("Custom"),
1011 sizeof(choice
->text
));
1013 choice
->code
= string
;
1014 string
= NULL
; /* Don't add as an attribute below */
1018 * Now process custom page sizes specially...
1021 if (!strcmp(keyword
, "CustomPageSize"))
1023 ppd
->variable_sizes
= 1;
1026 * Add a "Custom" page size entry...
1029 ppd_add_size(ppd
, "Custom");
1032 else if (!strcmp(keyword
, "LandscapeOrientation"))
1034 if (!strcmp(string
, "Minus90"))
1035 ppd
->landscape
= -90;
1036 else if (!strcmp(string
, "Plus90"))
1037 ppd
->landscape
= 90;
1039 else if (!strcmp(keyword
, "Emulators"))
1041 for (count
= 1, sptr
= string
; sptr
!= NULL
;)
1042 if ((sptr
= strchr(sptr
, ' ')) != NULL
)
1045 while (*sptr
== ' ')
1049 ppd
->num_emulations
= count
;
1050 ppd
->emulations
= calloc(count
, sizeof(ppd_emul_t
));
1052 for (i
= 0, sptr
= string
; i
< count
; i
++)
1054 for (nameptr
= ppd
->emulations
[i
].name
;
1055 *sptr
!= '\0' && *sptr
!= ' ';
1057 if (nameptr
< (ppd
->emulations
[i
].name
+ sizeof(ppd
->emulations
[i
].name
) - 1))
1062 while (*sptr
== ' ')
1066 else if (!strncmp(keyword
, "StartEmulator_", 14))
1070 for (i
= 0; i
< ppd
->num_emulations
; i
++)
1071 if (!strcmp(keyword
+ 14, ppd
->emulations
[i
].name
))
1073 ppd
->emulations
[i
].start
= string
;
1077 else if (!strncmp(keyword
, "StopEmulator_", 13))
1081 for (i
= 0; i
< ppd
->num_emulations
; i
++)
1082 if (!strcmp(keyword
+ 13, ppd
->emulations
[i
].name
))
1084 ppd
->emulations
[i
].stop
= string
;
1088 else if (!strcmp(keyword
, "JobPatchFile"))
1090 if (ppd
->patches
== NULL
)
1091 ppd
->patches
= strdup(string
);
1094 temp
= realloc(ppd
->patches
, strlen(ppd
->patches
) +
1095 strlen(string
) + 1);
1098 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1103 ppd
->patches
= temp
;
1105 strcpy(ppd
->patches
+ strlen(ppd
->patches
), string
);
1108 else if (!strcmp(keyword
, "OpenUI"))
1111 * Don't allow nesting of options...
1114 if (option
&& cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1116 cg
->ppd_status
= PPD_NESTED_OPEN_UI
;
1122 * Add an option record to the current sub-group, group, or file...
1126 _cups_strcpy(name
, name
+ 1); /* Eliminate leading asterisk */
1128 for (i
= (int)strlen(name
) - 1; i
> 0 && isspace(name
[i
] & 255); i
--)
1129 name
[i
] = '\0'; /* Eliminate trailing spaces */
1131 DEBUG_printf(("OpenUI of %s in group %s...\n", name
,
1132 group
? group
->text
: "(null)"));
1134 if (subgroup
!= NULL
)
1135 option
= ppd_get_option(subgroup
, name
);
1136 else if (group
== NULL
)
1138 if ((group
= ppd_get_group(ppd
, "General", _("General"), cg
)) == NULL
)
1141 DEBUG_printf(("Adding to group %s...\n", group
->text
));
1142 option
= ppd_get_option(group
, name
);
1146 option
= ppd_get_option(group
, name
);
1150 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1156 * Now fill in the initial information for the option...
1159 if (string
&& !strcmp(string
, "PickMany"))
1160 option
->ui
= PPD_UI_PICKMANY
;
1161 else if (string
&& !strcmp(string
, "Boolean"))
1162 option
->ui
= PPD_UI_BOOLEAN
;
1163 else if (string
&& !strcmp(string
, "PickOne"))
1164 option
->ui
= PPD_UI_PICKONE
;
1165 else if (cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1167 cg
->ppd_status
= PPD_BAD_OPEN_UI
;
1172 option
->ui
= PPD_UI_PICKONE
;
1174 for (j
= 0; j
< ppd
->num_attrs
; j
++)
1175 if (!strncmp(ppd
->attrs
[j
]->name
, "Default", 7) &&
1176 !strcmp(ppd
->attrs
[j
]->name
+ 7, name
) &&
1177 ppd
->attrs
[j
]->value
)
1179 DEBUG_printf(("Setting Default%s to %s via attribute...\n",
1180 option
->keyword
, ppd
->attrs
[j
]->value
));
1181 strlcpy(option
->defchoice
, ppd
->attrs
[j
]->value
,
1182 sizeof(option
->defchoice
));
1187 strlcpy(option
->text
, text
, sizeof(option
->text
));
1190 if (!strcmp(name
, "PageSize"))
1191 strlcpy(option
->text
, _("Media Size"), sizeof(option
->text
));
1192 else if (!strcmp(name
, "MediaType"))
1193 strlcpy(option
->text
, _("Media Type"), sizeof(option
->text
));
1194 else if (!strcmp(name
, "InputSlot"))
1195 strlcpy(option
->text
, _("Media Source"), sizeof(option
->text
));
1196 else if (!strcmp(name
, "ColorModel"))
1197 strlcpy(option
->text
, _("Output Mode"), sizeof(option
->text
));
1198 else if (!strcmp(name
, "Resolution"))
1199 strlcpy(option
->text
, _("Resolution"), sizeof(option
->text
));
1201 strlcpy(option
->text
, name
, sizeof(option
->text
));
1204 option
->section
= PPD_ORDER_ANY
;
1209 else if (!strcmp(keyword
, "JCLOpenUI"))
1212 * Don't allow nesting of options...
1215 if (option
&& cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1217 cg
->ppd_status
= PPD_NESTED_OPEN_UI
;
1223 * Find the JCL group, and add if needed...
1226 group
= ppd_get_group(ppd
, "JCL", _("JCL"), cg
);
1232 * Add an option record to the current JCLs...
1236 _cups_strcpy(name
, name
+ 1);
1238 option
= ppd_get_option(group
, name
);
1242 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1248 * Now fill in the initial information for the option...
1251 if (string
&& !strcmp(string
, "PickMany"))
1252 option
->ui
= PPD_UI_PICKMANY
;
1253 else if (string
&& !strcmp(string
, "Boolean"))
1254 option
->ui
= PPD_UI_BOOLEAN
;
1255 else if (string
&& !strcmp(string
, "PickOne"))
1256 option
->ui
= PPD_UI_PICKONE
;
1259 cg
->ppd_status
= PPD_BAD_OPEN_UI
;
1264 for (j
= 0; j
< ppd
->num_attrs
; j
++)
1265 if (!strncmp(ppd
->attrs
[j
]->name
, "Default", 7) &&
1266 !strcmp(ppd
->attrs
[j
]->name
+ 7, name
) &&
1267 ppd
->attrs
[j
]->value
)
1269 DEBUG_printf(("Setting Default%s to %s via attribute...\n",
1270 option
->keyword
, ppd
->attrs
[j
]->value
));
1271 strlcpy(option
->defchoice
, ppd
->attrs
[j
]->value
,
1272 sizeof(option
->defchoice
));
1276 strlcpy(option
->text
, text
, sizeof(option
->text
));
1278 option
->section
= PPD_ORDER_JCL
;
1284 else if (!strcmp(keyword
, "CloseUI") || !strcmp(keyword
, "JCLCloseUI"))
1291 else if (!strcmp(keyword
, "OpenGroup"))
1294 * Open a new group...
1299 cg
->ppd_status
= PPD_NESTED_OPEN_GROUP
;
1306 cg
->ppd_status
= PPD_BAD_OPEN_GROUP
;
1312 * Separate the group name from the text (name/text)...
1315 if ((sptr
= strchr(string
, '/')) != NULL
)
1321 * Fix up the text...
1327 * Find/add the group...
1330 group
= ppd_get_group(ppd
, string
, sptr
, cg
);
1338 else if (!strcmp(keyword
, "CloseGroup"))
1345 else if (!strcmp(keyword
, "OrderDependency") ||
1346 !strcmp(keyword
, "NonUIOrderDependency"))
1348 order
= _cupsStrScand(string
, &sptr
, loc
);
1350 if (!sptr
|| sscanf(sptr
, "%40s%40s", name
, keyword
) != 2)
1352 cg
->ppd_status
= PPD_BAD_ORDER_DEPENDENCY
;
1357 if (keyword
[0] == '*')
1358 _cups_strcpy(keyword
, keyword
+ 1);
1360 if (!strcmp(name
, "ExitServer"))
1361 section
= PPD_ORDER_EXIT
;
1362 else if (!strcmp(name
, "Prolog"))
1363 section
= PPD_ORDER_PROLOG
;
1364 else if (!strcmp(name
, "DocumentSetup"))
1365 section
= PPD_ORDER_DOCUMENT
;
1366 else if (!strcmp(name
, "PageSetup"))
1367 section
= PPD_ORDER_PAGE
;
1368 else if (!strcmp(name
, "JCLSetup"))
1369 section
= PPD_ORDER_JCL
;
1371 section
= PPD_ORDER_ANY
;
1379 * Only valid for Non-UI options...
1382 for (i
= ppd
->num_groups
, gtemp
= ppd
->groups
; i
> 0; i
--, gtemp
++)
1383 if (gtemp
->text
[0] == '\0')
1387 for (i
= 0; i
< gtemp
->num_options
; i
++)
1388 if (!strcmp(keyword
, gtemp
->options
[i
].keyword
))
1390 gtemp
->options
[i
].section
= section
;
1391 gtemp
->options
[i
].order
= order
;
1397 option
->section
= section
;
1398 option
->order
= order
;
1404 else if (!strncmp(keyword
, "Default", 7))
1410 * Drop UI text, if any, from value...
1413 if (strchr(string
, '/') != NULL
)
1414 *strchr(string
, '/') = '\0';
1417 * Assign the default value as appropriate...
1420 if (!strcmp(keyword
, "DefaultColorSpace"))
1423 * Set default colorspace...
1426 if (!strcmp(string
, "CMY"))
1427 ppd
->colorspace
= PPD_CS_CMY
;
1428 else if (!strcmp(string
, "CMYK"))
1429 ppd
->colorspace
= PPD_CS_CMYK
;
1430 else if (!strcmp(string
, "RGB"))
1431 ppd
->colorspace
= PPD_CS_RGB
;
1432 else if (!strcmp(string
, "RGBK"))
1433 ppd
->colorspace
= PPD_CS_RGBK
;
1434 else if (!strcmp(string
, "N"))
1435 ppd
->colorspace
= PPD_CS_N
;
1437 ppd
->colorspace
= PPD_CS_GRAY
;
1439 else if (option
&& !strcmp(keyword
+ 7, option
->keyword
))
1442 * Set the default as part of the current option...
1445 DEBUG_printf(("Setting %s to %s...\n", keyword
, string
));
1447 strlcpy(option
->defchoice
, string
, sizeof(option
->defchoice
));
1449 DEBUG_printf(("%s is now %s...\n", keyword
, option
->defchoice
));
1454 * Lookup option and set if it has been defined...
1457 ppd_option_t
*toption
; /* Temporary option */
1460 if ((toption
= ppdFindOption(ppd
, keyword
+ 7)) != NULL
)
1462 DEBUG_printf(("Setting %s to %s...\n", keyword
, string
));
1463 strlcpy(toption
->defchoice
, string
, sizeof(toption
->defchoice
));
1467 else if (!strcmp(keyword
, "UIConstraints") ||
1468 !strcmp(keyword
, "NonUIConstraints"))
1470 if (ppd
->num_consts
== 0)
1471 constraint
= calloc(1, sizeof(ppd_const_t
));
1473 constraint
= realloc(ppd
->consts
,
1474 (ppd
->num_consts
+ 1) * sizeof(ppd_const_t
));
1476 if (constraint
== NULL
)
1478 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1483 ppd
->consts
= constraint
;
1484 constraint
+= ppd
->num_consts
;
1487 switch (sscanf(string
, "%40s%40s%40s%40s", constraint
->option1
,
1488 constraint
->choice1
, constraint
->option2
,
1489 constraint
->choice2
))
1491 case 0 : /* Error */
1492 case 1 : /* Error */
1493 cg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1496 case 2 : /* Two options... */
1498 * The following strcpy's are safe, as optionN and
1499 * choiceN are all the same size (size defined by PPD spec...)
1502 if (constraint
->option1
[0] == '*')
1503 _cups_strcpy(constraint
->option1
, constraint
->option1
+ 1);
1505 if (constraint
->choice1
[0] == '*')
1506 _cups_strcpy(constraint
->option2
, constraint
->choice1
+ 1);
1508 _cups_strcpy(constraint
->option2
, constraint
->choice1
);
1510 constraint
->choice1
[0] = '\0';
1511 constraint
->choice2
[0] = '\0';
1514 case 3 : /* Two options, one choice... */
1516 * The following _cups_strcpy's are safe, as optionN and
1517 * choiceN are all the same size (size defined by PPD spec...)
1520 if (constraint
->option1
[0] == '*')
1521 _cups_strcpy(constraint
->option1
, constraint
->option1
+ 1);
1523 if (constraint
->choice1
[0] == '*')
1525 _cups_strcpy(constraint
->choice2
, constraint
->option2
);
1526 _cups_strcpy(constraint
->option2
, constraint
->choice1
+ 1);
1527 constraint
->choice1
[0] = '\0';
1531 if (constraint
->option2
[0] == '*')
1532 _cups_strcpy(constraint
->option2
, constraint
->option2
+ 1);
1534 constraint
->choice2
[0] = '\0';
1538 case 4 : /* Two options, two choices... */
1539 if (constraint
->option1
[0] == '*')
1540 _cups_strcpy(constraint
->option1
, constraint
->option1
+ 1);
1542 if (constraint
->option2
[0] == '*')
1543 _cups_strcpy(constraint
->option2
, constraint
->option2
+ 1);
1550 else if (!strcmp(keyword
, "PaperDimension"))
1552 if ((size
= ppdPageSize(ppd
, name
)) == NULL
)
1553 size
= ppd_add_size(ppd
, name
);
1558 * Unable to add or find size!
1561 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1566 size
->width
= _cupsStrScand(string
, &sptr
, loc
);
1567 size
->length
= _cupsStrScand(sptr
, NULL
, loc
);
1572 else if (!strcmp(keyword
, "ImageableArea"))
1574 if ((size
= ppdPageSize(ppd
, name
)) == NULL
)
1575 size
= ppd_add_size(ppd
, name
);
1580 * Unable to add or find size!
1583 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1588 size
->left
= _cupsStrScand(string
, &sptr
, loc
);
1589 size
->bottom
= _cupsStrScand(sptr
, &sptr
, loc
);
1590 size
->right
= _cupsStrScand(sptr
, &sptr
, loc
);
1591 size
->top
= _cupsStrScand(sptr
, NULL
, loc
);
1596 else if (option
!= NULL
&&
1597 (mask
& (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
)) ==
1598 (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
) &&
1599 !strcmp(keyword
, option
->keyword
))
1601 DEBUG_printf(("group = %p, subgroup = %p\n", group
, subgroup
));
1603 if (!strcmp(keyword
, "PageSize"))
1606 * Add a page size...
1609 if (ppdPageSize(ppd
, name
) == NULL
)
1610 ppd_add_size(ppd
, name
);
1614 * Add the option choice...
1617 choice
= ppd_add_choice(option
, name
);
1619 if (mask
& PPD_TEXT
)
1620 strlcpy(choice
->text
, text
, sizeof(choice
->text
));
1621 else if (!strcmp(name
, "True"))
1622 strcpy(choice
->text
, _("Yes"));
1623 else if (!strcmp(name
, "False"))
1624 strcpy(choice
->text
, _("No"));
1626 strlcpy(choice
->text
, name
, sizeof(choice
->text
));
1628 if (option
->section
== PPD_ORDER_JCL
)
1629 ppd_decode(string
); /* Decode quoted string */
1631 choice
->code
= string
;
1632 string
= NULL
; /* Don't add as an attribute below */
1636 * Add remaining lines with keywords and string values as attributes...
1640 (mask
& (PPD_KEYWORD
| PPD_STRING
)) == (PPD_KEYWORD
| PPD_STRING
))
1641 ppd_add_attr(ppd
, keyword
, name
, text
, string
);
1647 * Reset language preferences...
1650 cupsLangFree(language
);
1654 printf("Premature EOF at %lu...\n", (unsigned long)ftell(fp
));
1657 if (cg
->ppd_status
!= PPD_OK
)
1660 * Had an error reading the PPD file, cannot continue!
1669 * Create the sorted options array and set the option back-pointer for
1670 * each choice and custom option...
1673 ppd
->options
= cupsArrayNew((cups_array_func_t
)ppd_compare_options
, NULL
);
1675 for (i
= ppd
->num_groups
, group
= ppd
->groups
;
1679 for (j
= group
->num_options
, option
= group
->options
;
1683 ppd_coption_t
*coption
; /* Custom option */
1686 cupsArrayAdd(ppd
->options
, option
);
1688 for (k
= 0; k
< option
->num_choices
; k
++)
1689 option
->choices
[k
].option
= option
;
1691 if ((coption
= ppdFindCustomOption(ppd
, option
->keyword
)) != NULL
)
1692 coption
->option
= option
;
1697 * Return the PPD file structure...
1703 * Common exit point for errors to save code size...
1712 cupsLangFree(language
);
1719 * 'ppdOpenFd()' - Read a PPD file into memory.
1722 ppd_file_t
* /* O - PPD file record */
1723 ppdOpenFd(int fd
) /* I - File to read from */
1725 cups_file_t
*fp
; /* CUPS file pointer */
1726 ppd_file_t
*ppd
; /* PPD file record */
1727 _cups_globals_t
*cg
= _cupsGlobals();
1732 * Set the line number to 0...
1738 * Range check input...
1743 cg
->ppd_status
= PPD_NULL_FILE
;
1749 * Try to open the file and parse it...
1752 if ((fp
= cupsFileOpenFd(fd
, "r")) != NULL
)
1760 cg
->ppd_status
= PPD_FILE_OPEN_ERROR
;
1769 * 'ppdOpenFile()' - Read a PPD file into memory.
1772 ppd_file_t
* /* O - PPD file record */
1773 ppdOpenFile(const char *filename
) /* I - File to read from */
1775 cups_file_t
*fp
; /* File pointer */
1776 ppd_file_t
*ppd
; /* PPD file record */
1777 _cups_globals_t
*cg
= _cupsGlobals();
1782 * Set the line number to 0...
1788 * Range check input...
1791 if (filename
== NULL
)
1793 cg
->ppd_status
= PPD_NULL_FILE
;
1799 * Try to open the file and parse it...
1802 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
1810 cg
->ppd_status
= PPD_FILE_OPEN_ERROR
;
1819 * 'ppdSetConformance()' - Set the conformance level for PPD files.
1821 * @since CUPS 1.1.20@
1825 ppdSetConformance(ppd_conform_t c
) /* I - Conformance level */
1827 _cups_globals_t
*cg
= _cupsGlobals();
1831 cg
->ppd_conform
= c
;
1836 * 'ppd_add_attr()' - Add an attribute to the PPD data.
1839 static ppd_attr_t
* /* O - New attribute */
1840 ppd_add_attr(ppd_file_t
*ppd
, /* I - PPD file data */
1841 const char *name
, /* I - Attribute name */
1842 const char *spec
, /* I - Specifier string, if any */
1843 const char *text
, /* I - Text string, if any */
1844 const char *value
) /* I - Value of attribute */
1846 ppd_attr_t
**ptr
, /* New array */
1847 *temp
; /* New attribute */
1851 * Range check input...
1854 if (ppd
== NULL
|| name
== NULL
|| spec
== NULL
)
1858 * Create the array as needed...
1861 if (!ppd
->sorted_attrs
)
1862 ppd
->sorted_attrs
= cupsArrayNew((cups_array_func_t
)ppd_compare_attrs
,
1866 * Allocate memory for the new attribute...
1869 if (ppd
->num_attrs
== 0)
1870 ptr
= malloc(sizeof(ppd_attr_t
*));
1872 ptr
= realloc(ppd
->attrs
, (ppd
->num_attrs
+ 1) * sizeof(ppd_attr_t
*));
1878 ptr
+= ppd
->num_attrs
;
1880 if ((temp
= calloc(1, sizeof(ppd_attr_t
))) == NULL
)
1891 strlcpy(temp
->name
, name
, sizeof(temp
->name
));
1892 strlcpy(temp
->spec
, spec
, sizeof(temp
->spec
));
1893 strlcpy(temp
->text
, text
, sizeof(temp
->text
));
1894 temp
->value
= (char *)value
;
1897 * Add the attribute to the sorted array...
1900 cupsArrayAdd(ppd
->sorted_attrs
, temp
);
1903 * Return the attribute...
1911 * 'ppd_add_choice()' - Add a choice to an option.
1914 static ppd_choice_t
* /* O - Named choice */
1915 ppd_add_choice(ppd_option_t
*option
, /* I - Option */
1916 const char *name
) /* I - Name of choice */
1918 ppd_choice_t
*choice
; /* Choice */
1921 if (option
->num_choices
== 0)
1922 choice
= malloc(sizeof(ppd_choice_t
));
1924 choice
= realloc(option
->choices
,
1925 sizeof(ppd_choice_t
) * (option
->num_choices
+ 1));
1930 option
->choices
= choice
;
1931 choice
+= option
->num_choices
;
1932 option
->num_choices
++;
1934 memset(choice
, 0, sizeof(ppd_choice_t
));
1935 strlcpy(choice
->choice
, name
, sizeof(choice
->choice
));
1942 * 'ppd_add_size()' - Add a page size.
1945 static ppd_size_t
* /* O - Named size */
1946 ppd_add_size(ppd_file_t
*ppd
, /* I - PPD file */
1947 const char *name
) /* I - Name of size */
1949 ppd_size_t
*size
; /* Size */
1952 if (ppd
->num_sizes
== 0)
1953 size
= malloc(sizeof(ppd_size_t
));
1955 size
= realloc(ppd
->sizes
, sizeof(ppd_size_t
) * (ppd
->num_sizes
+ 1));
1961 size
+= ppd
->num_sizes
;
1964 memset(size
, 0, sizeof(ppd_size_t
));
1965 strlcpy(size
->name
, name
, sizeof(size
->name
));
1972 * 'ppd_compare_attrs()' - Compare two attributes.
1975 static int /* O - Result of comparison */
1976 ppd_compare_attrs(ppd_attr_t
*a
, /* I - First attribute */
1977 ppd_attr_t
*b
) /* I - Second attribute */
1979 int ret
; /* Result of comparison */
1982 if ((ret
= strcasecmp(a
->name
, b
->name
)) != 0)
1984 else if (a
->spec
[0] && b
->spec
[0])
1985 return (strcasecmp(a
->spec
, b
->spec
));
1992 * 'ppd_compare_coptions()' - Compare two custom options.
1995 static int /* O - Result of comparison */
1996 ppd_compare_coptions(ppd_coption_t
*a
, /* I - First option */
1997 ppd_coption_t
*b
) /* I - Second option */
1999 return (strcasecmp(a
->keyword
, b
->keyword
));
2004 * 'ppd_compare_cparams()' - Compare two custom parameters.
2007 static int /* O - Result of comparison */
2008 ppd_compare_cparams(ppd_cparam_t
*a
, /* I - First parameter */
2009 ppd_cparam_t
*b
) /* I - Second parameter */
2011 return (strcasecmp(a
->name
, b
->name
));
2016 * 'ppd_compare_options()' - Compare two options.
2019 static int /* O - Result of comparison */
2020 ppd_compare_options(ppd_option_t
*a
, /* I - First option */
2021 ppd_option_t
*b
) /* I - Second option */
2023 return (strcasecmp(a
->keyword
, b
->keyword
));
2028 * 'ppd_decode()' - Decode a string value...
2031 static int /* O - Length of decoded string */
2032 ppd_decode(char *string
) /* I - String to decode */
2034 char *inptr
, /* Input pointer */
2035 *outptr
; /* Output pointer */
2041 while (*inptr
!= '\0')
2042 if (*inptr
== '<' && isxdigit(inptr
[1] & 255))
2045 * Convert hex to 8-bit values...
2049 while (isxdigit(*inptr
& 255))
2051 if (isalpha(*inptr
))
2052 *outptr
= (tolower(*inptr
) - 'a' + 10) << 4;
2054 *outptr
= (*inptr
- '0') << 4;
2058 if (!isxdigit(*inptr
& 255))
2061 if (isalpha(*inptr
))
2062 *outptr
|= tolower(*inptr
) - 'a' + 10;
2064 *outptr
|= *inptr
- '0';
2070 while (*inptr
!= '>' && *inptr
!= '\0')
2072 while (*inptr
== '>')
2076 *outptr
++ = *inptr
++;
2080 return ((int)(outptr
- string
));
2085 * 'ppd_free_group()' - Free a single UI group.
2089 ppd_free_group(ppd_group_t
*group
) /* I - Group to free */
2091 int i
; /* Looping var */
2092 ppd_option_t
*option
; /* Current option */
2093 ppd_group_t
*subgroup
; /* Current sub-group */
2096 if (group
->num_options
> 0)
2098 for (i
= group
->num_options
, option
= group
->options
;
2101 ppd_free_option(option
);
2103 ppd_free(group
->options
);
2106 if (group
->num_subgroups
> 0)
2108 for (i
= group
->num_subgroups
, subgroup
= group
->subgroups
;
2111 ppd_free_group(subgroup
);
2113 ppd_free(group
->subgroups
);
2119 * 'ppd_free_option()' - Free a single option.
2123 ppd_free_option(ppd_option_t
*option
) /* I - Option to free */
2125 int i
; /* Looping var */
2126 ppd_choice_t
*choice
; /* Current choice */
2129 if (option
->num_choices
> 0)
2131 for (i
= option
->num_choices
, choice
= option
->choices
;
2135 ppd_free(choice
->code
);
2138 ppd_free(option
->choices
);
2144 * 'ppd_get_coption()' - Get a custom option record.
2147 static ppd_coption_t
* /* O - Custom option... */
2148 ppd_get_coption(ppd_file_t
*ppd
, /* I - PPD file */
2149 const char *name
) /* I - Name of option */
2151 ppd_coption_t
*copt
; /* New custom option */
2155 * See if the option already exists...
2158 if ((copt
= ppdFindCustomOption(ppd
, name
)) != NULL
)
2162 * Not found, so create the custom option record...
2165 if ((copt
= calloc(1, sizeof(ppd_coption_t
))) == NULL
)
2168 strlcpy(copt
->keyword
, name
, sizeof(copt
->keyword
));
2170 copt
->params
= cupsArrayNew((cups_array_func_t
)ppd_compare_cparams
, NULL
);
2172 cupsArrayAdd(ppd
->coptions
, copt
);
2175 * Return the new record...
2183 * 'ppd_get_cparam()' - Get a custom parameter record.
2186 static ppd_cparam_t
* /* O - Extended option... */
2187 ppd_get_cparam(ppd_coption_t
*opt
, /* I - PPD file */
2188 const char *param
, /* I - Name of parameter */
2189 const char *text
) /* I - Human-readable text */
2191 ppd_cparam_t
*cparam
; /* New custom parameter */
2195 * See if the parameter already exists...
2198 if ((cparam
= ppdFindCustomParam(opt
, param
)) != NULL
)
2202 * Not found, so create the custom parameter record...
2205 if ((cparam
= calloc(1, sizeof(ppd_cparam_t
))) == NULL
)
2208 strlcpy(cparam
->name
, param
, sizeof(cparam
->name
));
2209 strlcpy(cparam
->text
, text
, sizeof(cparam
->text
));
2212 * Add this record to the array...
2215 cupsArrayAdd(opt
->params
, cparam
);
2218 * Return the new record...
2226 * 'ppd_get_group()' - Find or create the named group as needed.
2229 static ppd_group_t
* /* O - Named group */
2230 ppd_get_group(ppd_file_t
*ppd
, /* I - PPD file */
2231 const char *name
, /* I - Name of group */
2232 const char *text
, /* I - Text for group */
2233 _cups_globals_t
*cg
) /* I - Global data */
2235 int i
; /* Looping var */
2236 ppd_group_t
*group
; /* Group */
2239 DEBUG_printf(("ppd_get_group(ppd=%p, name=\"%s\", text=\"%s\", cg=%p)\n",
2240 ppd
, name
, text
, cg
));
2242 for (i
= ppd
->num_groups
, group
= ppd
->groups
; i
> 0; i
--, group
++)
2243 if (!strcmp(group
->name
, name
))
2248 DEBUG_printf(("Adding group %s...\n", name
));
2250 if (cg
->ppd_conform
== PPD_CONFORM_STRICT
&& strlen(text
) >= sizeof(group
->text
))
2252 cg
->ppd_status
= PPD_ILLEGAL_TRANSLATION
;
2257 if (ppd
->num_groups
== 0)
2258 group
= malloc(sizeof(ppd_group_t
));
2260 group
= realloc(ppd
->groups
,
2261 (ppd
->num_groups
+ 1) * sizeof(ppd_group_t
));
2265 cg
->ppd_status
= PPD_ALLOC_ERROR
;
2270 ppd
->groups
= group
;
2271 group
+= ppd
->num_groups
;
2274 memset(group
, 0, sizeof(ppd_group_t
));
2275 strlcpy(group
->name
, name
, sizeof(group
->name
));
2276 strlcpy(group
->text
, text
, sizeof(group
->text
));
2284 * 'ppd_get_option()' - Find or create the named option as needed.
2287 static ppd_option_t
* /* O - Named option */
2288 ppd_get_option(ppd_group_t
*group
, /* I - Group */
2289 const char *name
) /* I - Name of option */
2291 int i
; /* Looping var */
2292 ppd_option_t
*option
; /* Option */
2295 DEBUG_printf(("ppd_get_option(group=%p(\"%s\"), name=\"%s\")\n",
2296 group
, group
->name
, name
));
2298 for (i
= group
->num_options
, option
= group
->options
; i
> 0; i
--, option
++)
2299 if (!strcmp(option
->keyword
, name
))
2304 if (group
->num_options
== 0)
2305 option
= malloc(sizeof(ppd_option_t
));
2307 option
= realloc(group
->options
,
2308 (group
->num_options
+ 1) * sizeof(ppd_option_t
));
2313 group
->options
= option
;
2314 option
+= group
->num_options
;
2315 group
->num_options
++;
2317 memset(option
, 0, sizeof(ppd_option_t
));
2318 strlcpy(option
->keyword
, name
, sizeof(option
->keyword
));
2326 * 'ppd_read()' - Read a line from a PPD file, skipping comment lines as
2330 static int /* O - Bitmask of fields read */
2331 ppd_read(cups_file_t
*fp
, /* I - File to read from */
2332 char *keyword
, /* O - Keyword from line */
2333 char *option
, /* O - Option from line */
2334 char *text
, /* O - Human-readable text from line */
2335 char **string
, /* O - Code/string data */
2336 int ignoreblank
, /* I - Ignore blank lines? */
2337 _cups_globals_t
*cg
) /* I - Global data */
2339 int ch
, /* Character from file */
2340 col
, /* Column in line */
2341 colon
, /* Colon seen? */
2342 endquote
, /* Waiting for an end quote */
2343 mask
, /* Mask to be returned */
2344 startline
, /* Start line */
2345 textlen
; /* Length of text */
2346 char *keyptr
, /* Keyword pointer */
2347 *optptr
, /* Option pointer */
2348 *textptr
, /* Text pointer */
2349 *strptr
, /* Pointer into string */
2350 *lineptr
, /* Current position in line buffer */
2351 *line
; /* Line buffer */
2352 int linesize
; /* Current size of line buffer */
2355 * Range check everything...
2358 if (!fp
|| !keyword
|| !option
|| !text
|| !string
)
2362 * Now loop until we have a valid line...
2367 startline
= cg
->ppd_line
+ 1;
2369 line
= malloc(linesize
);
2384 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
2386 if (lineptr
>= (line
+ linesize
- 1))
2389 * Expand the line buffer...
2392 char *temp
; /* Temporary line pointer */
2396 if (linesize
> 262144)
2399 * Don't allow lines longer than 256k!
2402 cg
->ppd_line
= startline
;
2403 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2410 temp
= realloc(line
, linesize
);
2413 cg
->ppd_line
= startline
;
2414 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2421 lineptr
= temp
+ (lineptr
- line
);
2425 if (ch
== '\r' || ch
== '\n')
2428 * Line feed or carriage return...
2437 * Check for a trailing line feed...
2440 if ((ch
= cupsFilePeekChar(fp
)) == EOF
)
2447 cupsFileGetChar(fp
);
2450 if (lineptr
== line
&& ignoreblank
)
2451 continue; /* Skip blank lines */
2455 if (!endquote
) /* Continue for multi-line text */
2460 else if (ch
< ' ' && ch
!= '\t' && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2463 * Other control characters...
2466 cg
->ppd_line
= startline
;
2467 cg
->ppd_status
= PPD_ILLEGAL_CHARACTER
;
2473 else if (ch
!= 0x1a)
2476 * Any other character...
2482 if (col
> (PPD_MAX_LINE
- 1))
2485 * Line is too long...
2488 cg
->ppd_line
= startline
;
2489 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2496 if (ch
== ':' && strncmp(line
, "*%", 2) != 0)
2499 if (ch
== '\"' && colon
)
2500 endquote
= !endquote
;
2507 * Didn't finish this quoted string...
2510 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
2513 else if (ch
== '\r' || ch
== '\n')
2521 * Check for a trailing line feed...
2524 if ((ch
= cupsFilePeekChar(fp
)) == EOF
)
2527 cupsFileGetChar(fp
);
2532 else if (ch
< ' ' && ch
!= '\t' && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2535 * Other control characters...
2538 cg
->ppd_line
= startline
;
2539 cg
->ppd_status
= PPD_ILLEGAL_CHARACTER
;
2545 else if (ch
!= 0x1a)
2549 if (col
> (PPD_MAX_LINE
- 1))
2552 * Line is too long...
2555 cg
->ppd_line
= startline
;
2556 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2568 * Didn't finish this line...
2571 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
2572 if (ch
== '\r' || ch
== '\n')
2575 * Line feed or carriage return...
2584 * Check for a trailing line feed...
2587 if ((ch
= cupsFilePeekChar(fp
)) == EOF
)
2590 cupsFileGetChar(fp
);
2595 else if (ch
< ' ' && ch
!= '\t' && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2598 * Other control characters...
2601 cg
->ppd_line
= startline
;
2602 cg
->ppd_status
= PPD_ILLEGAL_CHARACTER
;
2608 else if (ch
!= 0x1a)
2612 if (col
> (PPD_MAX_LINE
- 1))
2615 * Line is too long...
2618 cg
->ppd_line
= startline
;
2619 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2628 if (lineptr
> line
&& lineptr
[-1] == '\n')
2633 DEBUG_printf(("LINE = \"%s\"\n", line
));
2636 * The dynamically created PPDs for older style Mac OS X
2637 * drivers include a large blob of data inserted as comments
2638 * at the end of the file. As an optimization we can stop
2639 * reading the PPD when we get to the start of this data.
2642 if (!strcmp(line
, "*%APLWORKSET START"))
2648 if (ch
== EOF
&& lineptr
== line
)
2666 if ((!line
[0] || /* Blank line */
2667 !strncmp(line
, "*%", 2) || /* Comment line */
2668 !strcmp(line
, "*End")) && /* End of multi-line string */
2669 ignoreblank
) /* Ignore these? */
2671 startline
= cg
->ppd_line
+ 1;
2675 if (!strcmp(line
, "*")) /* (Bad) comment line */
2677 if (cg
->ppd_conform
== PPD_CONFORM_RELAXED
)
2679 startline
= cg
->ppd_line
+ 1;
2684 cg
->ppd_line
= startline
;
2685 cg
->ppd_status
= PPD_ILLEGAL_MAIN_KEYWORD
;
2692 if (line
[0] != '*') /* All lines start with an asterisk */
2695 * Allow lines consisting of just whitespace...
2698 for (lineptr
= line
; *lineptr
; lineptr
++)
2699 if (!isspace(*lineptr
& 255))
2704 cg
->ppd_status
= PPD_MISSING_ASTERISK
;
2708 else if (ignoreblank
)
2723 while (*lineptr
!= '\0' && *lineptr
!= ':' && !isspace(*lineptr
& 255))
2725 if (*lineptr
<= ' ' || *lineptr
> 126 || *lineptr
== '/' ||
2726 (keyptr
- keyword
) >= (PPD_MAX_NAME
- 1))
2728 cg
->ppd_status
= PPD_ILLEGAL_MAIN_KEYWORD
;
2733 *keyptr
++ = *lineptr
++;
2738 if (!strcmp(keyword
, "End"))
2741 mask
|= PPD_KEYWORD
;
2743 /* DEBUG_printf(("keyword = \"%s\", lineptr = \"%s\"\n", keyword, lineptr));*/
2745 if (isspace(*lineptr
& 255))
2748 * Get an option name...
2751 while (isspace(*lineptr
& 255))
2756 while (*lineptr
!= '\0' && !isspace(*lineptr
& 255) && *lineptr
!= ':' &&
2759 if (*lineptr
<= ' ' || *lineptr
> 126 ||
2760 (optptr
- option
) >= (PPD_MAX_NAME
- 1))
2762 cg
->ppd_status
= PPD_ILLEGAL_OPTION_KEYWORD
;
2767 *optptr
++ = *lineptr
++;
2772 if (isspace(*lineptr
& 255) && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2774 cg
->ppd_status
= PPD_ILLEGAL_WHITESPACE
;
2779 while (isspace(*lineptr
& 255))
2784 /* DEBUG_printf(("option = \"%s\", lineptr = \"%s\"\n", option, lineptr));*/
2786 if (*lineptr
== '/')
2789 * Get human-readable text...
2796 while (*lineptr
!= '\0' && *lineptr
!= '\n' && *lineptr
!= ':')
2798 if (((unsigned char)*lineptr
< ' ' && *lineptr
!= '\t') ||
2799 (textptr
- text
) >= (PPD_MAX_LINE
- 1))
2801 cg
->ppd_status
= PPD_ILLEGAL_TRANSLATION
;
2806 *textptr
++ = *lineptr
++;
2810 textlen
= ppd_decode(text
);
2812 if (textlen
> PPD_MAX_TEXT
&& cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2814 cg
->ppd_status
= PPD_ILLEGAL_TRANSLATION
;
2822 /* DEBUG_printf(("text = \"%s\", lineptr = \"%s\"\n", text, lineptr));*/
2825 if (isspace(*lineptr
& 255) && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2827 cg
->ppd_status
= PPD_ILLEGAL_WHITESPACE
;
2832 while (isspace(*lineptr
& 255))
2835 if (*lineptr
== ':')
2838 * Get string after triming leading and trailing whitespace...
2842 while (isspace(*lineptr
& 255))
2845 strptr
= lineptr
+ strlen(lineptr
) - 1;
2846 while (strptr
>= lineptr
&& isspace(*strptr
& 255))
2849 if (*strptr
== '\"')
2852 * Quoted string by itself...
2855 *string
= malloc(strlen(lineptr
) + 1);
2859 for (; *lineptr
!= '\0'; lineptr
++)
2860 if (*lineptr
!= '\"')
2861 *strptr
++ = *lineptr
;
2866 *string
= strdup(lineptr
);
2868 /* DEBUG_printf(("string = \"%s\", lineptr = \"%s\"\n", *string, lineptr));*/
2882 * End of "$Id: ppd.c 5238 2006-03-07 04:41:42Z mike $".