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 * _ppd_attr_compare() - Compare two attributes.
38 * ppdClose() - Free all memory used by the PPD file.
39 * ppdErrorString() - Returns the text assocated with a status.
40 * ppdLastError() - Return the status from the last ppdOpen*().
41 * ppdOpen() - 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_coptions() - Compare two custom options.
49 * ppd_compare_cparams() - Compare two custom parameters.
50 * ppd_compare_options() - Compare two options.
51 * ppd_decode() - Decode a string value...
52 * ppd_free_group() - Free a single UI group.
53 * ppd_free_option() - Free a single option.
54 * ppd_get_coption() - Get a custom option record.
55 * ppd_get_cparam() - Get a custom parameter record.
56 * ppd_get_group() - Find or create the named group as needed.
57 * ppd_get_option() - Find or create the named option as needed.
58 * ppd_read() - Read a line from a PPD file, skipping comment
63 * Include necessary headers.
75 #if defined(WIN32) || defined(__EMX__)
76 # define READ_BINARY "rb" /* Open a binary file for reading */
77 # define WRITE_BINARY "wb" /* Open a binary file for writing */
79 # define READ_BINARY "r" /* Open a binary file for reading */
80 # define WRITE_BINARY "w" /* Open a binary file for writing */
81 #endif /* WIN32 || __EMX__ */
83 #define ppd_free(p) if (p) free(p) /* Safe free macro */
85 #define PPD_KEYWORD 1 /* Line contained a keyword */
86 #define PPD_OPTION 2 /* Line contained an option name */
87 #define PPD_TEXT 4 /* Line contained human-readable text */
88 #define PPD_STRING 8 /* Line contained a string or code */
95 static ppd_attr_t
*ppd_add_attr(ppd_file_t
*ppd
, const char *name
,
96 const char *spec
, const char *text
,
98 static ppd_choice_t
*ppd_add_choice(ppd_option_t
*option
, const char *name
);
99 static ppd_size_t
*ppd_add_size(ppd_file_t
*ppd
, const char *name
);
100 static int ppd_compare_coptions(ppd_coption_t
*a
, ppd_coption_t
*b
);
101 static int ppd_compare_cparams(ppd_cparam_t
*a
, ppd_cparam_t
*b
);
102 static int ppd_compare_options(ppd_option_t
*a
, ppd_option_t
*b
);
103 static int ppd_decode(char *string
);
104 static void ppd_free_group(ppd_group_t
*group
);
105 static void ppd_free_option(ppd_option_t
*option
);
106 static ppd_coption_t
*ppd_get_coption(ppd_file_t
*ppd
, const char *name
);
107 static ppd_cparam_t
*ppd_get_cparam(ppd_coption_t
*opt
,
110 static ppd_group_t
*ppd_get_group(ppd_file_t
*ppd
, const char *name
,
111 const char *text
, _cups_globals_t
*cg
);
112 static ppd_option_t
*ppd_get_option(ppd_group_t
*group
, const char *name
);
113 static int ppd_read(cups_file_t
*fp
, char *keyword
, char *option
,
114 char *text
, char **string
, int ignoreblank
,
115 _cups_globals_t
*cg
);
119 * '_ppd_attr_compare()' - Compare two attributes.
122 int /* O - Result of comparison */
123 _ppd_attr_compare(ppd_attr_t
**a
, /* I - First attribute */
124 ppd_attr_t
**b
) /* I - Second attribute */
126 int ret
; /* Result of comparison */
129 if ((ret
= strcasecmp((*a
)->name
, (*b
)->name
)) != 0)
131 else if ((*a
)->spec
[0] && (*b
)->spec
[0])
132 return (strcasecmp((*a
)->spec
, (*b
)->spec
));
139 * 'ppdClose()' - Free all memory used by the PPD file.
143 ppdClose(ppd_file_t
*ppd
) /* I - PPD file record */
145 int i
; /* Looping var */
146 ppd_emul_t
*emul
; /* Current emulation */
147 ppd_group_t
*group
; /* Current group */
148 char **font
; /* Current font */
149 char **filter
; /* Current filter */
150 ppd_attr_t
**attr
; /* Current attribute */
151 ppd_coption_t
*coption
; /* Current custom option */
152 ppd_cparam_t
*cparam
; /* Current custom parameter */
156 * Range check arguments...
163 * Free all strings at the top level...
166 ppd_free(ppd
->patches
);
167 ppd_free(ppd
->jcl_begin
);
168 ppd_free(ppd
->jcl_end
);
169 ppd_free(ppd
->jcl_ps
);
172 * Free any emulations...
175 if (ppd
->num_emulations
> 0)
177 for (i
= ppd
->num_emulations
, emul
= ppd
->emulations
; i
> 0; i
--, emul
++)
179 ppd_free(emul
->start
);
180 ppd_free(emul
->stop
);
183 ppd_free(ppd
->emulations
);
187 * Free any UI groups, subgroups, and options...
190 if (ppd
->num_groups
> 0)
192 for (i
= ppd
->num_groups
, group
= ppd
->groups
; i
> 0; i
--, group
++)
193 ppd_free_group(group
);
195 ppd_free(ppd
->groups
);
198 cupsArrayDelete(ppd
->options
);
201 * Free any page sizes...
204 if (ppd
->num_sizes
> 0)
205 ppd_free(ppd
->sizes
);
208 * Free any constraints...
211 if (ppd
->num_consts
> 0)
212 ppd_free(ppd
->consts
);
215 * Free any filters...
218 if (ppd
->num_filters
> 0)
220 for (i
= ppd
->num_filters
, filter
= ppd
->filters
; i
> 0; i
--, filter
++)
225 ppd_free(ppd
->filters
);
232 if (ppd
->num_fonts
> 0)
234 for (i
= ppd
->num_fonts
, font
= ppd
->fonts
; i
> 0; i
--, font
++)
239 ppd_free(ppd
->fonts
);
243 * Free any profiles...
246 if (ppd
->num_profiles
> 0)
247 ppd_free(ppd
->profiles
);
250 * Free any attributes...
253 if (ppd
->num_attrs
> 0)
255 for (i
= ppd
->num_attrs
, attr
= ppd
->attrs
; i
> 0; i
--, attr
++)
257 ppd_free((*attr
)->value
);
261 ppd_free(ppd
->attrs
);
265 * Free custom options...
268 for (coption
= (ppd_coption_t
*)cupsArrayFirst(ppd
->coptions
);
270 coption
= (ppd_coption_t
*)cupsArrayNext(ppd
->coptions
))
272 for (cparam
= (ppd_cparam_t
*)cupsArrayFirst(coption
->params
);
274 cparam
= (ppd_cparam_t
*)cupsArrayNext(coption
->params
))
276 switch (cparam
->type
)
278 case PPD_CUSTOM_PASSCODE
:
279 case PPD_CUSTOM_PASSWORD
:
280 case PPD_CUSTOM_STRING
:
281 ppd_free(cparam
->current
.custom_string
);
282 ppd_free(cparam
->minimum
.custom_string
);
283 ppd_free(cparam
->maximum
.custom_string
);
293 cupsArrayDelete(coption
->params
);
298 cupsArrayDelete(ppd
->coptions
);
301 * Free the whole record...
309 * 'ppdErrorString()' - Returns the text assocated with a status.
311 * @since CUPS 1.1.19@
314 const char * /* O - Status string */
315 ppdErrorString(ppd_status_t status
) /* I - PPD status */
317 static const char * const messages
[] =/* Status messages */
320 _("Unable to open PPD file"),
321 _("NULL PPD file pointer"),
322 _("Memory allocation error"),
323 _("Missing PPD-Adobe-4.x header"),
324 _("Missing value string"),
327 _("OpenGroup without a CloseGroup first"),
328 _("Bad OpenUI/JCLOpenUI"),
329 _("OpenUI/JCLOpenUI without a CloseUI/JCLCloseUI first"),
330 _("Bad OrderDependency"),
331 _("Bad UIConstraints"),
332 _("Missing asterisk in column 1"),
333 _("Line longer than the maximum allowed (255 characters)"),
334 _("Illegal control character"),
335 _("Illegal main keyword string"),
336 _("Illegal option keyword string"),
337 _("Illegal translation string"),
338 _("Illegal whitespace character"),
339 _("Bad custom parameter")
343 if (status
< PPD_OK
|| status
> PPD_ILLEGAL_WHITESPACE
)
344 return (_cupsLangString(cupsLangDefault(), _("Unknown")));
346 return (_cupsLangString(cupsLangDefault(), messages
[status
]));
351 * 'ppdLastError()' - Return the status from the last ppdOpen*().
353 * @since CUPS 1.1.19@
356 ppd_status_t
/* O - Status code */
357 ppdLastError(int *line
) /* O - Line number */
359 _cups_globals_t
*cg
= _cupsGlobals();
364 *line
= cg
->ppd_line
;
366 return (cg
->ppd_status
);
371 * 'ppdOpen()' - Read a PPD file into memory.
374 ppd_file_t
* /* O - PPD file record */
375 ppdOpen(FILE *fp
) /* I - File to read from */
377 ppd_file_t
*ppd
; /* PPD file record */
378 cups_file_t
*cf
; /* CUPS file */
382 * Reopen the stdio file as a CUPS file...
385 if ((cf
= cupsFileOpenFd(fileno(fp
), "r")) == NULL
)
389 * Load the PPD file using the newer API...
395 * Close the CUPS file and return the PPD...
405 * 'ppdOpen2()' - Read a PPD file into memory.
410 ppd_file_t
* /* O - PPD file record */
411 ppdOpen2(cups_file_t
*fp
) /* I - File to read from */
413 char *oldlocale
; /* Old locale settings */
414 int i
, j
, k
; /* Looping vars */
415 int count
; /* Temporary count */
416 ppd_file_t
*ppd
; /* PPD file record */
417 ppd_group_t
*group
, /* Current group */
418 *subgroup
; /* Current sub-group */
419 ppd_option_t
*option
; /* Current option */
420 ppd_choice_t
*choice
; /* Current choice */
421 ppd_const_t
*constraint
; /* Current constraint */
422 ppd_size_t
*size
; /* Current page size */
423 int mask
; /* Line data mask */
424 char keyword
[PPD_MAX_NAME
],
425 /* Keyword from file */
427 /* Option from file */
429 /* Human-readable text from file */
430 *string
, /* Code/text from file */
431 *sptr
, /* Pointer into string */
432 *nameptr
, /* Pointer into name */
433 *temp
, /* Temporary string pointer */
434 **tempfonts
; /* Temporary fonts pointer */
435 float order
; /* Order dependency number */
436 ppd_section_t section
; /* Order dependency section */
437 ppd_profile_t
*profile
; /* Pointer to color profile */
438 char **filter
; /* Pointer to filter */
439 cups_lang_t
*language
; /* Default language */
440 int ui_keyword
; /* Is this line a UI keyword? */
441 _cups_globals_t
*cg
= _cupsGlobals();
443 static const char * const ui_keywords
[] =
445 /* Boolean keywords */
455 /* PickOne keywords */
468 "JCLFrameBufferSize",
493 * Default to "OK" status...
496 cg
->ppd_status
= PPD_OK
;
500 * Range check input...
505 cg
->ppd_status
= PPD_NULL_FILE
;
510 * Grab the first line and make sure it reads '*PPD-Adobe: "major.minor"'...
513 mask
= ppd_read(fp
, keyword
, name
, text
, &string
, 0, cg
);
515 DEBUG_printf(("mask=%x, keyword=\"%s\"...\n", mask
, keyword
));
518 strcmp(keyword
, "PPD-Adobe") ||
519 string
== NULL
|| string
[0] != '4')
522 * Either this is not a PPD file, or it is not a 4.x PPD file.
525 if (cg
->ppd_status
== PPD_OK
)
526 cg
->ppd_status
= PPD_MISSING_PPDADOBE4
;
533 DEBUG_printf(("ppdOpen: keyword = %s, string = %p\n", keyword
, string
));
538 * Allocate memory for the PPD file record...
541 if ((ppd
= calloc(1, sizeof(ppd_file_t
))) == NULL
)
543 cg
->ppd_status
= PPD_ALLOC_ERROR
;
548 ppd
->language_level
= 1;
549 ppd
->color_device
= 0;
550 ppd
->colorspace
= PPD_CS_GRAY
;
551 ppd
->landscape
= -90;
552 ppd
->coptions
= cupsArrayNew((cups_array_func_t
)ppd_compare_coptions
,
556 * Get the default language for the user...
559 language
= cupsLangDefault();
562 oldlocale
= _cupsSaveLocale(LC_NUMERIC
, "C");
564 oldlocale
= _cupsSaveLocale(LC_ALL
, "C");
565 #endif /* LC_NUMERIC */
568 * Read lines from the PPD file and add them to the file record...
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 (strcmp(keyword
, "Collate") && strcmp(keyword
, "Duplex") &&
655 strcmp(keyword
, "InputSlot") && strcmp(keyword
, "ManualFeed") &&
656 strcmp(keyword
, "MediaType") && strcmp(keyword
, "MediaColor") &&
657 strcmp(keyword
, "MediaWeight") && strcmp(keyword
, "OutputBin") &&
658 strcmp(keyword
, "OutputMode") && strcmp(keyword
, "OutputOrder") &&
659 strcmp(keyword
, "PageSize") && strcmp(keyword
, "PageRegion"))
660 group
= ppd_get_group(ppd
, "Extra", _("Extra"), cg
);
662 group
= ppd_get_group(ppd
, "General", _("General"), cg
);
667 DEBUG_printf(("Adding to group %s...\n", group
->text
));
668 option
= ppd_get_option(group
, keyword
);
672 option
= ppd_get_option(group
, keyword
);
676 cg
->ppd_status
= PPD_ALLOC_ERROR
;
682 * Now fill in the initial information for the option...
685 if (!strncmp(keyword
, "JCL", 3))
686 option
->section
= PPD_ORDER_JCL
;
688 option
->section
= PPD_ORDER_ANY
;
690 option
->order
= 10.0f
;
693 option
->ui
= PPD_UI_BOOLEAN
;
695 option
->ui
= PPD_UI_PICKONE
;
697 for (j
= 0; j
< ppd
->num_attrs
; j
++)
698 if (!strncmp(ppd
->attrs
[j
]->name
, "Default", 7) &&
699 !strcmp(ppd
->attrs
[j
]->name
+ 7, keyword
) &&
700 ppd
->attrs
[j
]->value
)
702 DEBUG_printf(("Setting Default%s to %s via attribute...\n",
703 option
->keyword
, ppd
->attrs
[j
]->value
));
704 strlcpy(option
->defchoice
, ppd
->attrs
[j
]->value
,
705 sizeof(option
->defchoice
));
709 if (!strcmp(keyword
, "PageSize"))
710 strlcpy(option
->text
, _("Media Size"), sizeof(option
->text
));
711 else if (!strcmp(keyword
, "MediaType"))
712 strlcpy(option
->text
, _("Media Type"), sizeof(option
->text
));
713 else if (!strcmp(keyword
, "InputSlot"))
714 strlcpy(option
->text
, _("Media Source"), sizeof(option
->text
));
715 else if (!strcmp(keyword
, "ColorModel"))
716 strlcpy(option
->text
, _("Output Mode"), sizeof(option
->text
));
717 else if (!strcmp(keyword
, "Resolution"))
718 strlcpy(option
->text
, _("Resolution"), sizeof(option
->text
));
720 strlcpy(option
->text
, keyword
, sizeof(option
->text
));
724 if (!strcmp(keyword
, "LanguageLevel"))
725 ppd
->language_level
= atoi(string
);
726 else if (!strcmp(keyword
, "LanguageEncoding"))
727 ppd
->lang_encoding
= string
;
728 else if (!strcmp(keyword
, "LanguageVersion"))
729 ppd
->lang_version
= string
;
730 else if (!strcmp(keyword
, "Manufacturer"))
731 ppd
->manufacturer
= string
;
732 else if (!strcmp(keyword
, "ModelName"))
733 ppd
->modelname
= string
;
734 else if (!strcmp(keyword
, "Protocols"))
735 ppd
->protocols
= string
;
736 else if (!strcmp(keyword
, "PCFileName"))
737 ppd
->pcfilename
= string
;
738 else if (!strcmp(keyword
, "NickName"))
739 ppd
->nickname
= string
;
740 else if (!strcmp(keyword
, "Product"))
741 ppd
->product
= string
;
742 else if (!strcmp(keyword
, "ShortNickName"))
743 ppd
->shortnickname
= string
;
744 else if (!strcmp(keyword
, "TTRasterizer"))
745 ppd
->ttrasterizer
= string
;
746 else if (!strcmp(keyword
, "JCLBegin"))
748 ppd
->jcl_begin
= strdup(string
);
749 ppd_decode(ppd
->jcl_begin
); /* Decode quoted string */
751 else if (!strcmp(keyword
, "JCLEnd"))
753 ppd
->jcl_end
= strdup(string
);
754 ppd_decode(ppd
->jcl_end
); /* Decode quoted string */
756 else if (!strcmp(keyword
, "JCLToPSInterpreter"))
758 ppd
->jcl_ps
= strdup(string
);
759 ppd_decode(ppd
->jcl_ps
); /* Decode quoted string */
761 else if (!strcmp(keyword
, "AccurateScreensSupport"))
762 ppd
->accurate_screens
= !strcmp(string
, "True");
763 else if (!strcmp(keyword
, "ColorDevice"))
764 ppd
->color_device
= !strcmp(string
, "True");
765 else if (!strcmp(keyword
, "ContoneOnly"))
766 ppd
->contone_only
= !strcmp(string
, "True");
767 else if (!strcmp(keyword
, "cupsFlipDuplex"))
768 ppd
->flip_duplex
= !strcmp(string
, "True");
769 else if (!strcmp(keyword
, "cupsManualCopies"))
770 ppd
->manual_copies
= !strcmp(string
, "True");
771 else if (!strcmp(keyword
, "cupsModelNumber"))
772 ppd
->model_number
= atoi(string
);
773 else if (!strcmp(keyword
, "cupsColorProfile"))
775 if (ppd
->num_profiles
== 0)
776 profile
= malloc(sizeof(ppd_profile_t
));
778 profile
= realloc(ppd
->profiles
, sizeof(ppd_profile_t
) *
779 (ppd
->num_profiles
+ 1));
781 ppd
->profiles
= profile
;
782 profile
+= ppd
->num_profiles
;
783 ppd
->num_profiles
++;
785 memset(profile
, 0, sizeof(ppd_profile_t
));
786 strlcpy(profile
->resolution
, name
, sizeof(profile
->resolution
));
787 strlcpy(profile
->media_type
, text
, sizeof(profile
->media_type
));
788 sscanf(string
, "%f%f%f%f%f%f%f%f%f%f%f", &(profile
->density
),
790 profile
->matrix
[0] + 0, profile
->matrix
[0] + 1,
791 profile
->matrix
[0] + 2, profile
->matrix
[1] + 0,
792 profile
->matrix
[1] + 1, profile
->matrix
[1] + 2,
793 profile
->matrix
[2] + 0, profile
->matrix
[2] + 1,
794 profile
->matrix
[2] + 2);
796 else if (!strcmp(keyword
, "cupsFilter"))
798 if (ppd
->num_filters
== 0)
799 filter
= malloc(sizeof(char *));
801 filter
= realloc(ppd
->filters
, sizeof(char *) * (ppd
->num_filters
+ 1));
807 cg
->ppd_status
= PPD_ALLOC_ERROR
;
812 ppd
->filters
= filter
;
813 filter
+= ppd
->num_filters
;
817 * Copy filter string and prevent it from being freed below...
823 else if (!strcmp(keyword
, "Throughput"))
824 ppd
->throughput
= atoi(string
);
825 else if (!strcmp(keyword
, "Font"))
828 * Add this font to the list of available fonts...
831 if (ppd
->num_fonts
== 0)
832 tempfonts
= (char **)malloc(sizeof(char *));
834 tempfonts
= (char **)realloc(ppd
->fonts
,
835 sizeof(char *) * (ppd
->num_fonts
+ 1));
837 if (tempfonts
== NULL
)
839 cg
->ppd_status
= PPD_ALLOC_ERROR
;
844 ppd
->fonts
= tempfonts
;
845 ppd
->fonts
[ppd
->num_fonts
] = strdup(name
);
849 else if (!strcmp(keyword
, "ParamCustomPageSize"))
851 if (!strcmp(name
, "Width"))
852 sscanf(string
, "%*s%*s%f%f", ppd
->custom_min
+ 0,
853 ppd
->custom_max
+ 0);
854 else if (!strcmp(name
, "Height"))
855 sscanf(string
, "%*s%*s%f%f", ppd
->custom_min
+ 1,
856 ppd
->custom_max
+ 1);
859 else if (!strncmp(keyword
, "ParamCustom", 11))
861 ppd_coption_t
*coption
; /* Custom option */
862 ppd_cparam_t
*cparam
; /* Custom parameter */
863 int corder
; /* Order number */
864 char ctype
[33], /* Data type */
865 cminimum
[65], /* Minimum value */
866 cmaximum
[65]; /* Maximum value */
870 * Get the custom option and parameter...
873 if ((coption
= ppd_get_coption(ppd
, keyword
+ 11)) == NULL
)
875 cg
->ppd_status
= PPD_ALLOC_ERROR
;
880 if ((cparam
= ppd_get_cparam(coption
, name
, text
)) == NULL
)
882 cg
->ppd_status
= PPD_ALLOC_ERROR
;
888 * Get the parameter data...
891 if (sscanf(string
, "%d%32s%64s%64s", &corder
, ctype
, cminimum
,
894 cg
->ppd_status
= PPD_BAD_CUSTOM_PARAM
;
899 cparam
->order
= corder
;
901 if (!strcmp(ctype
, "curve"))
903 cparam
->type
= PPD_CUSTOM_CURVE
;
904 cparam
->minimum
.custom_curve
= atof(cminimum
);
905 cparam
->maximum
.custom_curve
= atof(cmaximum
);
907 else if (!strcmp(ctype
, "int"))
909 cparam
->type
= PPD_CUSTOM_INT
;
910 cparam
->minimum
.custom_int
= atoi(cminimum
);
911 cparam
->maximum
.custom_int
= atoi(cmaximum
);
913 else if (!strcmp(ctype
, "invcurve"))
915 cparam
->type
= PPD_CUSTOM_INVCURVE
;
916 cparam
->minimum
.custom_invcurve
= atof(cminimum
);
917 cparam
->maximum
.custom_invcurve
= atof(cmaximum
);
919 else if (!strcmp(ctype
, "passcode"))
921 cparam
->type
= PPD_CUSTOM_PASSCODE
;
922 cparam
->minimum
.custom_passcode
= strdup(cminimum
);
923 cparam
->maximum
.custom_passcode
= strdup(cmaximum
);
925 else if (!strcmp(ctype
, "password"))
927 cparam
->type
= PPD_CUSTOM_PASSWORD
;
928 cparam
->minimum
.custom_password
= strdup(cminimum
);
929 cparam
->maximum
.custom_password
= strdup(cmaximum
);
931 else if (!strcmp(ctype
, "points"))
933 cparam
->type
= PPD_CUSTOM_POINTS
;
934 cparam
->minimum
.custom_points
= atof(cminimum
);
935 cparam
->maximum
.custom_points
= atof(cmaximum
);
937 else if (!strcmp(ctype
, "real"))
939 cparam
->type
= PPD_CUSTOM_REAL
;
940 cparam
->minimum
.custom_real
= atof(cminimum
);
941 cparam
->maximum
.custom_real
= atof(cmaximum
);
943 else if (!strcmp(ctype
, "string"))
945 cparam
->type
= PPD_CUSTOM_STRING
;
946 cparam
->minimum
.custom_string
= strdup(cminimum
);
947 cparam
->maximum
.custom_string
= strdup(cmaximum
);
951 cg
->ppd_status
= PPD_BAD_CUSTOM_PARAM
;
957 * Now special-case for CustomPageSize...
960 if (!strcmp(coption
->keyword
, "PageSize"))
962 if (!strcmp(name
, "Width"))
964 ppd
->custom_min
[0] = cparam
->minimum
.custom_points
;
965 ppd
->custom_max
[0] = cparam
->maximum
.custom_points
;
967 else if (!strcmp(name
, "Height"))
969 ppd
->custom_min
[1] = cparam
->minimum
.custom_points
;
970 ppd
->custom_max
[1] = cparam
->maximum
.custom_points
;
974 else if (!strcmp(keyword
, "HWMargins"))
975 sscanf(string
, "%f%f%f%f", ppd
->custom_margins
+ 0,
976 ppd
->custom_margins
+ 1, ppd
->custom_margins
+ 2,
977 ppd
->custom_margins
+ 3);
978 else if (!strncmp(keyword
, "Custom", 6) && !strcmp(name
, "True"))
980 ppd_coption_t
*coption
; /* Custom option */
983 DEBUG_puts("Processing Custom option...");
986 * Get the option and custom option...
989 if ((option
= ppdFindOption(ppd
, keyword
+ 6)) == NULL
)
991 ppd_group_t
*gtemp
; /* Temporary group */
994 DEBUG_printf(("%s option not found for %s...\n", keyword
+ 6, keyword
));
996 if ((gtemp
= ppd_get_group(ppd
, "General", _("General"), cg
)) == NULL
)
998 DEBUG_puts("Unable to get general group!");
1003 if ((option
= ppd_get_option(gtemp
, keyword
+ 6)) == NULL
)
1005 DEBUG_printf(("Unable to get %s option!\n", keyword
+ 6));
1007 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1013 if ((coption
= ppd_get_coption(ppd
, keyword
+ 6)) == NULL
)
1015 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1021 * Add the "custom" option...
1024 if ((choice
= ppd_add_choice(option
, "Custom")) == NULL
)
1026 DEBUG_puts("Unable to add Custom choice!");
1028 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1033 strlcpy(choice
->text
, text
[0] ? text
: _("Custom"),
1034 sizeof(choice
->text
));
1036 choice
->code
= string
;
1037 string
= NULL
; /* Don't add as an attribute below */
1041 * Now process custom page sizes specially...
1044 if (!strcmp(keyword
, "CustomPageSize"))
1046 ppd
->variable_sizes
= 1;
1049 * Add a "Custom" page size entry...
1052 ppd_add_size(ppd
, "Custom");
1055 else if (!strcmp(keyword
, "LandscapeOrientation"))
1057 if (!strcmp(string
, "Minus90"))
1058 ppd
->landscape
= -90;
1059 else if (!strcmp(string
, "Plus90"))
1060 ppd
->landscape
= 90;
1062 else if (!strcmp(keyword
, "Emulators"))
1064 for (count
= 1, sptr
= string
; sptr
!= NULL
;)
1065 if ((sptr
= strchr(sptr
, ' ')) != NULL
)
1068 while (*sptr
== ' ')
1072 ppd
->num_emulations
= count
;
1073 ppd
->emulations
= calloc(count
, sizeof(ppd_emul_t
));
1075 for (i
= 0, sptr
= string
; i
< count
; i
++)
1077 for (nameptr
= ppd
->emulations
[i
].name
;
1078 *sptr
!= '\0' && *sptr
!= ' ';
1080 if (nameptr
< (ppd
->emulations
[i
].name
+ sizeof(ppd
->emulations
[i
].name
) - 1))
1085 while (*sptr
== ' ')
1089 else if (!strncmp(keyword
, "StartEmulator_", 14))
1093 for (i
= 0; i
< ppd
->num_emulations
; i
++)
1094 if (!strcmp(keyword
+ 14, ppd
->emulations
[i
].name
))
1096 ppd
->emulations
[i
].start
= string
;
1100 else if (!strncmp(keyword
, "StopEmulator_", 13))
1104 for (i
= 0; i
< ppd
->num_emulations
; i
++)
1105 if (!strcmp(keyword
+ 13, ppd
->emulations
[i
].name
))
1107 ppd
->emulations
[i
].stop
= string
;
1111 else if (!strcmp(keyword
, "JobPatchFile"))
1113 if (ppd
->patches
== NULL
)
1114 ppd
->patches
= strdup(string
);
1117 temp
= realloc(ppd
->patches
, strlen(ppd
->patches
) +
1118 strlen(string
) + 1);
1121 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1126 ppd
->patches
= temp
;
1128 strcpy(ppd
->patches
+ strlen(ppd
->patches
), string
);
1131 else if (!strcmp(keyword
, "OpenUI"))
1134 * Don't allow nesting of options...
1137 if (option
&& cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1139 cg
->ppd_status
= PPD_NESTED_OPEN_UI
;
1145 * Add an option record to the current sub-group, group, or file...
1149 _cups_strcpy(name
, name
+ 1); /* Eliminate leading asterisk */
1151 for (i
= (int)strlen(name
) - 1; i
> 0 && isspace(name
[i
] & 255); i
--)
1152 name
[i
] = '\0'; /* Eliminate trailing spaces */
1154 DEBUG_printf(("OpenUI of %s in group %s...\n", name
,
1155 group
? group
->text
: "(null)"));
1157 if (subgroup
!= NULL
)
1158 option
= ppd_get_option(subgroup
, name
);
1159 else if (group
== NULL
)
1161 if (strcmp(name
, "Collate") && strcmp(name
, "Duplex") &&
1162 strcmp(name
, "InputSlot") && strcmp(name
, "ManualFeed") &&
1163 strcmp(name
, "MediaType") && strcmp(name
, "MediaColor") &&
1164 strcmp(name
, "MediaWeight") && strcmp(name
, "OutputBin") &&
1165 strcmp(name
, "OutputMode") && strcmp(name
, "OutputOrder") &&
1166 strcmp(name
, "PageSize") && strcmp(name
, "PageRegion"))
1167 group
= ppd_get_group(ppd
, "Extra", _("Extra"), cg
);
1169 group
= ppd_get_group(ppd
, "General", _("General"), cg
);
1174 DEBUG_printf(("Adding to group %s...\n", group
->text
));
1175 option
= ppd_get_option(group
, name
);
1179 option
= ppd_get_option(group
, name
);
1183 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1189 * Now fill in the initial information for the option...
1192 if (string
&& !strcmp(string
, "PickMany"))
1193 option
->ui
= PPD_UI_PICKMANY
;
1194 else if (string
&& !strcmp(string
, "Boolean"))
1195 option
->ui
= PPD_UI_BOOLEAN
;
1196 else if (string
&& !strcmp(string
, "PickOne"))
1197 option
->ui
= PPD_UI_PICKONE
;
1198 else if (cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1200 cg
->ppd_status
= PPD_BAD_OPEN_UI
;
1205 option
->ui
= PPD_UI_PICKONE
;
1207 for (j
= 0; j
< ppd
->num_attrs
; j
++)
1208 if (!strncmp(ppd
->attrs
[j
]->name
, "Default", 7) &&
1209 !strcmp(ppd
->attrs
[j
]->name
+ 7, name
) &&
1210 ppd
->attrs
[j
]->value
)
1212 DEBUG_printf(("Setting Default%s to %s via attribute...\n",
1213 option
->keyword
, ppd
->attrs
[j
]->value
));
1214 strlcpy(option
->defchoice
, ppd
->attrs
[j
]->value
,
1215 sizeof(option
->defchoice
));
1220 strlcpy(option
->text
, text
, sizeof(option
->text
));
1223 if (!strcmp(name
, "PageSize"))
1224 strlcpy(option
->text
, _("Media Size"), sizeof(option
->text
));
1225 else if (!strcmp(name
, "MediaType"))
1226 strlcpy(option
->text
, _("Media Type"), sizeof(option
->text
));
1227 else if (!strcmp(name
, "InputSlot"))
1228 strlcpy(option
->text
, _("Media Source"), sizeof(option
->text
));
1229 else if (!strcmp(name
, "ColorModel"))
1230 strlcpy(option
->text
, _("Output Mode"), sizeof(option
->text
));
1231 else if (!strcmp(name
, "Resolution"))
1232 strlcpy(option
->text
, _("Resolution"), sizeof(option
->text
));
1234 strlcpy(option
->text
, name
, sizeof(option
->text
));
1237 option
->section
= PPD_ORDER_ANY
;
1242 else if (!strcmp(keyword
, "JCLOpenUI"))
1245 * Don't allow nesting of options...
1248 if (option
&& cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1250 cg
->ppd_status
= PPD_NESTED_OPEN_UI
;
1256 * Find the JCL group, and add if needed...
1259 group
= ppd_get_group(ppd
, "JCL", _("JCL"), cg
);
1265 * Add an option record to the current JCLs...
1269 _cups_strcpy(name
, name
+ 1);
1271 option
= ppd_get_option(group
, name
);
1275 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1281 * Now fill in the initial information for the option...
1284 if (string
&& !strcmp(string
, "PickMany"))
1285 option
->ui
= PPD_UI_PICKMANY
;
1286 else if (string
&& !strcmp(string
, "Boolean"))
1287 option
->ui
= PPD_UI_BOOLEAN
;
1288 else if (string
&& !strcmp(string
, "PickOne"))
1289 option
->ui
= PPD_UI_PICKONE
;
1292 cg
->ppd_status
= PPD_BAD_OPEN_UI
;
1297 for (j
= 0; j
< ppd
->num_attrs
; j
++)
1298 if (!strncmp(ppd
->attrs
[j
]->name
, "Default", 7) &&
1299 !strcmp(ppd
->attrs
[j
]->name
+ 7, name
) &&
1300 ppd
->attrs
[j
]->value
)
1302 DEBUG_printf(("Setting Default%s to %s via attribute...\n",
1303 option
->keyword
, ppd
->attrs
[j
]->value
));
1304 strlcpy(option
->defchoice
, ppd
->attrs
[j
]->value
,
1305 sizeof(option
->defchoice
));
1309 strlcpy(option
->text
, text
, sizeof(option
->text
));
1311 option
->section
= PPD_ORDER_JCL
;
1317 else if (!strcmp(keyword
, "CloseUI") || !strcmp(keyword
, "JCLCloseUI"))
1324 else if (!strcmp(keyword
, "OpenGroup"))
1327 * Open a new group...
1332 cg
->ppd_status
= PPD_NESTED_OPEN_GROUP
;
1339 cg
->ppd_status
= PPD_BAD_OPEN_GROUP
;
1345 * Separate the group name from the text (name/text)...
1348 if ((sptr
= strchr(string
, '/')) != NULL
)
1354 * Fix up the text...
1360 * Find/add the group...
1363 group
= ppd_get_group(ppd
, string
, sptr
, cg
);
1371 else if (!strcmp(keyword
, "CloseGroup"))
1378 else if (!strcmp(keyword
, "OrderDependency") ||
1379 !strcmp(keyword
, "NonUIOrderDependency"))
1381 if (sscanf(string
, "%f%40s%40s", &order
, name
, keyword
) != 3)
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 sscanf(string
, "%f%f", &(size
->width
), &(size
->length
));
1602 else if (!strcmp(keyword
, "ImageableArea"))
1604 if ((size
= ppdPageSize(ppd
, name
)) == NULL
)
1605 size
= ppd_add_size(ppd
, name
);
1610 * Unable to add or find size!
1613 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1618 sscanf(string
, "%f%f%f%f", &(size
->left
), &(size
->bottom
),
1619 &(size
->right
), &(size
->top
));
1624 else if (option
!= NULL
&&
1625 (mask
& (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
)) ==
1626 (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
) &&
1627 !strcmp(keyword
, option
->keyword
))
1629 DEBUG_printf(("group = %p, subgroup = %p\n", group
, subgroup
));
1631 if (!strcmp(keyword
, "PageSize"))
1634 * Add a page size...
1637 if (ppdPageSize(ppd
, name
) == NULL
)
1638 ppd_add_size(ppd
, name
);
1642 * Add the option choice...
1645 choice
= ppd_add_choice(option
, name
);
1647 if (mask
& PPD_TEXT
)
1648 strlcpy(choice
->text
, text
, sizeof(choice
->text
));
1649 else if (!strcmp(name
, "True"))
1650 strcpy(choice
->text
, _("Yes"));
1651 else if (!strcmp(name
, "False"))
1652 strcpy(choice
->text
, _("No"));
1654 strlcpy(choice
->text
, name
, sizeof(choice
->text
));
1656 if (option
->section
== PPD_ORDER_JCL
)
1657 ppd_decode(string
); /* Decode quoted string */
1659 choice
->code
= string
;
1660 string
= NULL
; /* Don't add as an attribute below */
1664 * Add remaining lines with keywords and string values as attributes...
1668 (mask
& (PPD_KEYWORD
| PPD_STRING
)) == (PPD_KEYWORD
| PPD_STRING
))
1669 ppd_add_attr(ppd
, keyword
, name
, text
, string
);
1675 * Reset language preferences...
1678 cupsLangFree(language
);
1681 _cupsRestoreLocale(LC_NUMERIC
, oldlocale
);
1683 _cupsRestoreLocale(LC_ALL
, oldlocale
);
1684 #endif /* LC_NUMERIC */
1688 printf("Premature EOF at %lu...\n", (unsigned long)ftell(fp
));
1691 if (cg
->ppd_status
!= PPD_OK
)
1694 * Had an error reading the PPD file, cannot continue!
1703 * Create the sorted options array and set the option back-pointer for
1704 * each choice and custom option...
1707 ppd
->options
= cupsArrayNew((cups_array_func_t
)ppd_compare_options
, NULL
);
1709 for (i
= ppd
->num_groups
, group
= ppd
->groups
;
1713 for (j
= group
->num_options
, option
= group
->options
;
1717 ppd_coption_t
*coption
; /* Custom option */
1720 cupsArrayAdd(ppd
->options
, option
);
1722 for (k
= 0; k
< option
->num_choices
; k
++)
1723 option
->choices
[k
].option
= option
;
1725 if ((coption
= ppdFindCustomOption(ppd
, option
->keyword
)) != NULL
)
1726 coption
->option
= option
;
1731 * Sort the attributes...
1734 if (ppd
->num_attrs
> 1)
1735 qsort(ppd
->attrs
, ppd
->num_attrs
, sizeof(ppd_attr_t
*),
1736 (int (*)(const void *, const void *))_ppd_attr_compare
);
1739 * Return the PPD file structure...
1745 * Common exit point for errors to save code size...
1754 cupsLangFree(language
);
1757 _cupsRestoreLocale(LC_NUMERIC
, oldlocale
);
1759 _cupsRestoreLocale(LC_ALL
, oldlocale
);
1760 #endif /* LC_NUMERIC */
1767 * 'ppdOpenFd()' - Read a PPD file into memory.
1770 ppd_file_t
* /* O - PPD file record */
1771 ppdOpenFd(int fd
) /* I - File to read from */
1773 cups_file_t
*fp
; /* CUPS file pointer */
1774 ppd_file_t
*ppd
; /* PPD file record */
1775 _cups_globals_t
*cg
= _cupsGlobals();
1780 * Set the line number to 0...
1786 * Range check input...
1791 cg
->ppd_status
= PPD_NULL_FILE
;
1797 * Try to open the file and parse it...
1800 if ((fp
= cupsFileOpenFd(fd
, "r")) != NULL
)
1808 cg
->ppd_status
= PPD_FILE_OPEN_ERROR
;
1817 * 'ppdOpenFile()' - Read a PPD file into memory.
1820 ppd_file_t
* /* O - PPD file record */
1821 ppdOpenFile(const char *filename
) /* I - File to read from */
1823 cups_file_t
*fp
; /* File pointer */
1824 ppd_file_t
*ppd
; /* PPD file record */
1825 _cups_globals_t
*cg
= _cupsGlobals();
1830 * Set the line number to 0...
1836 * Range check input...
1839 if (filename
== NULL
)
1841 cg
->ppd_status
= PPD_NULL_FILE
;
1847 * Try to open the file and parse it...
1850 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
1858 cg
->ppd_status
= PPD_FILE_OPEN_ERROR
;
1867 * 'ppdSetConformance()' - Set the conformance level for PPD files.
1869 * @since CUPS 1.1.20@
1873 ppdSetConformance(ppd_conform_t c
) /* I - Conformance level */
1875 _cups_globals_t
*cg
= _cupsGlobals();
1879 cg
->ppd_conform
= c
;
1884 * 'ppd_add_attr()' - Add an attribute to the PPD data.
1887 static ppd_attr_t
* /* O - New attribute */
1888 ppd_add_attr(ppd_file_t
*ppd
, /* I - PPD file data */
1889 const char *name
, /* I - Attribute name */
1890 const char *spec
, /* I - Specifier string, if any */
1891 const char *text
, /* I - Text string, if any */
1892 const char *value
) /* I - Value of attribute */
1894 ppd_attr_t
**ptr
, /* New array */
1895 *temp
; /* New attribute */
1899 * Range check input...
1902 if (ppd
== NULL
|| name
== NULL
|| spec
== NULL
)
1906 * Allocate memory for the new attribute...
1909 if (ppd
->num_attrs
== 0)
1910 ptr
= malloc(sizeof(ppd_attr_t
*));
1912 ptr
= realloc(ppd
->attrs
, (ppd
->num_attrs
+ 1) * sizeof(ppd_attr_t
*));
1918 ptr
+= ppd
->num_attrs
;
1920 if ((temp
= calloc(1, sizeof(ppd_attr_t
))) == NULL
)
1931 strlcpy(temp
->name
, name
, sizeof(temp
->name
));
1932 strlcpy(temp
->spec
, spec
, sizeof(temp
->spec
));
1933 strlcpy(temp
->text
, text
, sizeof(temp
->text
));
1934 temp
->value
= (char *)value
;
1937 * Return the attribute...
1945 * 'ppd_add_choice()' - Add a choice to an option.
1948 static ppd_choice_t
* /* O - Named choice */
1949 ppd_add_choice(ppd_option_t
*option
, /* I - Option */
1950 const char *name
) /* I - Name of choice */
1952 ppd_choice_t
*choice
; /* Choice */
1955 if (option
->num_choices
== 0)
1956 choice
= malloc(sizeof(ppd_choice_t
));
1958 choice
= realloc(option
->choices
,
1959 sizeof(ppd_choice_t
) * (option
->num_choices
+ 1));
1964 option
->choices
= choice
;
1965 choice
+= option
->num_choices
;
1966 option
->num_choices
++;
1968 memset(choice
, 0, sizeof(ppd_choice_t
));
1969 strlcpy(choice
->choice
, name
, sizeof(choice
->choice
));
1976 * 'ppd_add_size()' - Add a page size.
1979 static ppd_size_t
* /* O - Named size */
1980 ppd_add_size(ppd_file_t
*ppd
, /* I - PPD file */
1981 const char *name
) /* I - Name of size */
1983 ppd_size_t
*size
; /* Size */
1986 if (ppd
->num_sizes
== 0)
1987 size
= malloc(sizeof(ppd_size_t
));
1989 size
= realloc(ppd
->sizes
, sizeof(ppd_size_t
) * (ppd
->num_sizes
+ 1));
1995 size
+= ppd
->num_sizes
;
1998 memset(size
, 0, sizeof(ppd_size_t
));
1999 strlcpy(size
->name
, name
, sizeof(size
->name
));
2006 * 'ppd_compare_coptions()' - Compare two custom options.
2009 static int /* O - Result of comparison */
2010 ppd_compare_coptions(ppd_coption_t
*a
, /* I - First option */
2011 ppd_coption_t
*b
) /* I - Second option */
2013 return (strcasecmp(a
->keyword
, b
->keyword
));
2018 * 'ppd_compare_cparams()' - Compare two custom parameters.
2021 static int /* O - Result of comparison */
2022 ppd_compare_cparams(ppd_cparam_t
*a
, /* I - First parameter */
2023 ppd_cparam_t
*b
) /* I - Second parameter */
2025 return (strcasecmp(a
->name
, b
->name
));
2030 * 'ppd_compare_options()' - Compare two options.
2033 static int /* O - Result of comparison */
2034 ppd_compare_options(ppd_option_t
*a
, /* I - First option */
2035 ppd_option_t
*b
) /* I - Second option */
2037 return (strcasecmp(a
->keyword
, b
->keyword
));
2042 * 'ppd_decode()' - Decode a string value...
2045 static int /* O - Length of decoded string */
2046 ppd_decode(char *string
) /* I - String to decode */
2048 char *inptr
, /* Input pointer */
2049 *outptr
; /* Output pointer */
2055 while (*inptr
!= '\0')
2056 if (*inptr
== '<' && isxdigit(inptr
[1] & 255))
2059 * Convert hex to 8-bit values...
2063 while (isxdigit(*inptr
& 255))
2065 if (isalpha(*inptr
))
2066 *outptr
= (tolower(*inptr
) - 'a' + 10) << 4;
2068 *outptr
= (*inptr
- '0') << 4;
2072 if (!isxdigit(*inptr
& 255))
2075 if (isalpha(*inptr
))
2076 *outptr
|= tolower(*inptr
) - 'a' + 10;
2078 *outptr
|= *inptr
- '0';
2084 while (*inptr
!= '>' && *inptr
!= '\0')
2086 while (*inptr
== '>')
2090 *outptr
++ = *inptr
++;
2094 return ((int)(outptr
- string
));
2099 * 'ppd_free_group()' - Free a single UI group.
2103 ppd_free_group(ppd_group_t
*group
) /* I - Group to free */
2105 int i
; /* Looping var */
2106 ppd_option_t
*option
; /* Current option */
2107 ppd_group_t
*subgroup
; /* Current sub-group */
2110 if (group
->num_options
> 0)
2112 for (i
= group
->num_options
, option
= group
->options
;
2115 ppd_free_option(option
);
2117 ppd_free(group
->options
);
2120 if (group
->num_subgroups
> 0)
2122 for (i
= group
->num_subgroups
, subgroup
= group
->subgroups
;
2125 ppd_free_group(subgroup
);
2127 ppd_free(group
->subgroups
);
2133 * 'ppd_free_option()' - Free a single option.
2137 ppd_free_option(ppd_option_t
*option
) /* I - Option to free */
2139 int i
; /* Looping var */
2140 ppd_choice_t
*choice
; /* Current choice */
2143 if (option
->num_choices
> 0)
2145 for (i
= option
->num_choices
, choice
= option
->choices
;
2149 ppd_free(choice
->code
);
2152 ppd_free(option
->choices
);
2158 * 'ppd_get_coption()' - Get a custom option record.
2161 static ppd_coption_t
* /* O - Custom option... */
2162 ppd_get_coption(ppd_file_t
*ppd
, /* I - PPD file */
2163 const char *name
) /* I - Name of option */
2165 ppd_coption_t
*copt
; /* New custom option */
2169 * See if the option already exists...
2172 if ((copt
= ppdFindCustomOption(ppd
, name
)) != NULL
)
2176 * Not found, so create the custom option record...
2179 if ((copt
= calloc(1, sizeof(ppd_coption_t
))) == NULL
)
2182 strlcpy(copt
->keyword
, name
, sizeof(copt
->keyword
));
2184 copt
->params
= cupsArrayNew((cups_array_func_t
)ppd_compare_cparams
, NULL
);
2186 cupsArrayAdd(ppd
->coptions
, copt
);
2189 * Return the new record...
2197 * 'ppd_get_cparam()' - Get a custom parameter record.
2200 static ppd_cparam_t
* /* O - Extended option... */
2201 ppd_get_cparam(ppd_coption_t
*opt
, /* I - PPD file */
2202 const char *param
, /* I - Name of parameter */
2203 const char *text
) /* I - Human-readable text */
2205 ppd_cparam_t
*cparam
; /* New custom parameter */
2209 * See if the parameter already exists...
2212 if ((cparam
= ppdFindCustomParam(opt
, param
)) != NULL
)
2216 * Not found, so create the custom parameter record...
2219 if ((cparam
= calloc(1, sizeof(ppd_cparam_t
))) == NULL
)
2222 strlcpy(cparam
->name
, param
, sizeof(cparam
->name
));
2223 strlcpy(cparam
->text
, text
, sizeof(cparam
->text
));
2226 * Add this record to the array...
2229 cupsArrayAdd(opt
->params
, cparam
);
2232 * Return the new record...
2240 * 'ppd_get_group()' - Find or create the named group as needed.
2243 static ppd_group_t
* /* O - Named group */
2244 ppd_get_group(ppd_file_t
*ppd
, /* I - PPD file */
2245 const char *name
, /* I - Name of group */
2246 const char *text
, /* I - Text for group */
2247 _cups_globals_t
*cg
) /* I - Global data */
2249 int i
; /* Looping var */
2250 ppd_group_t
*group
; /* Group */
2253 DEBUG_printf(("ppd_get_group(ppd=%p, name=\"%s\", text=\"%s\", cg=%p)\n",
2254 ppd
, name
, text
, cg
));
2256 for (i
= ppd
->num_groups
, group
= ppd
->groups
; i
> 0; i
--, group
++)
2257 if (!strcmp(group
->name
, name
))
2262 DEBUG_printf(("Adding group %s...\n", name
));
2264 if (cg
->ppd_conform
== PPD_CONFORM_STRICT
&& strlen(text
) >= sizeof(group
->text
))
2266 cg
->ppd_status
= PPD_ILLEGAL_TRANSLATION
;
2271 if (ppd
->num_groups
== 0)
2272 group
= malloc(sizeof(ppd_group_t
));
2274 group
= realloc(ppd
->groups
,
2275 (ppd
->num_groups
+ 1) * sizeof(ppd_group_t
));
2279 cg
->ppd_status
= PPD_ALLOC_ERROR
;
2284 ppd
->groups
= group
;
2285 group
+= ppd
->num_groups
;
2288 memset(group
, 0, sizeof(ppd_group_t
));
2289 strlcpy(group
->name
, name
, sizeof(group
->name
));
2290 strlcpy(group
->text
, text
, sizeof(group
->text
));
2298 * 'ppd_get_option()' - Find or create the named option as needed.
2301 static ppd_option_t
* /* O - Named option */
2302 ppd_get_option(ppd_group_t
*group
, /* I - Group */
2303 const char *name
) /* I - Name of option */
2305 int i
; /* Looping var */
2306 ppd_option_t
*option
; /* Option */
2309 DEBUG_printf(("ppd_get_option(group=%p(\"%s\"), name=\"%s\")\n",
2310 group
, group
->name
, name
));
2312 for (i
= group
->num_options
, option
= group
->options
; i
> 0; i
--, option
++)
2313 if (!strcmp(option
->keyword
, name
))
2318 if (group
->num_options
== 0)
2319 option
= malloc(sizeof(ppd_option_t
));
2321 option
= realloc(group
->options
,
2322 (group
->num_options
+ 1) * sizeof(ppd_option_t
));
2327 group
->options
= option
;
2328 option
+= group
->num_options
;
2329 group
->num_options
++;
2331 memset(option
, 0, sizeof(ppd_option_t
));
2332 strlcpy(option
->keyword
, name
, sizeof(option
->keyword
));
2340 * 'ppd_read()' - Read a line from a PPD file, skipping comment lines as
2344 static int /* O - Bitmask of fields read */
2345 ppd_read(cups_file_t
*fp
, /* I - File to read from */
2346 char *keyword
, /* O - Keyword from line */
2347 char *option
, /* O - Option from line */
2348 char *text
, /* O - Human-readable text from line */
2349 char **string
, /* O - Code/string data */
2350 int ignoreblank
, /* I - Ignore blank lines? */
2351 _cups_globals_t
*cg
) /* I - Global data */
2353 int ch
, /* Character from file */
2354 col
, /* Column in line */
2355 colon
, /* Colon seen? */
2356 endquote
, /* Waiting for an end quote */
2357 mask
, /* Mask to be returned */
2358 startline
, /* Start line */
2359 textlen
; /* Length of text */
2360 char *keyptr
, /* Keyword pointer */
2361 *optptr
, /* Option pointer */
2362 *textptr
, /* Text pointer */
2363 *strptr
, /* Pointer into string */
2364 *lineptr
, /* Current position in line buffer */
2365 *line
; /* Line buffer */
2366 int linesize
; /* Current size of line buffer */
2369 * Range check everything...
2372 if (!fp
|| !keyword
|| !option
|| !text
|| !string
)
2376 * Now loop until we have a valid line...
2381 startline
= cg
->ppd_line
+ 1;
2383 line
= malloc(linesize
);
2398 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
2400 if (lineptr
>= (line
+ linesize
- 1))
2403 * Expand the line buffer...
2406 char *temp
; /* Temporary line pointer */
2410 if (linesize
> 262144)
2413 * Don't allow lines longer than 256k!
2416 cg
->ppd_line
= startline
;
2417 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2424 temp
= realloc(line
, linesize
);
2427 cg
->ppd_line
= startline
;
2428 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2435 lineptr
= temp
+ (lineptr
- line
);
2439 if (ch
== '\r' || ch
== '\n')
2442 * Line feed or carriage return...
2451 * Check for a trailing line feed...
2454 if ((ch
= cupsFilePeekChar(fp
)) == EOF
)
2461 cupsFileGetChar(fp
);
2464 if (lineptr
== line
&& ignoreblank
)
2465 continue; /* Skip blank lines */
2469 if (!endquote
) /* Continue for multi-line text */
2474 else if (ch
< ' ' && ch
!= '\t' && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2477 * Other control characters...
2480 cg
->ppd_line
= startline
;
2481 cg
->ppd_status
= PPD_ILLEGAL_CHARACTER
;
2487 else if (ch
!= 0x1a)
2490 * Any other character...
2496 if (col
> (PPD_MAX_LINE
- 1))
2499 * Line is too long...
2502 cg
->ppd_line
= startline
;
2503 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2510 if (ch
== ':' && strncmp(line
, "*%", 2) != 0)
2513 if (ch
== '\"' && colon
)
2514 endquote
= !endquote
;
2521 * Didn't finish this quoted string...
2524 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
2527 else if (ch
== '\r' || ch
== '\n')
2535 * Check for a trailing line feed...
2538 if ((ch
= cupsFilePeekChar(fp
)) == EOF
)
2541 cupsFileGetChar(fp
);
2546 else if (ch
< ' ' && ch
!= '\t' && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2549 * Other control characters...
2552 cg
->ppd_line
= startline
;
2553 cg
->ppd_status
= PPD_ILLEGAL_CHARACTER
;
2559 else if (ch
!= 0x1a)
2563 if (col
> (PPD_MAX_LINE
- 1))
2566 * Line is too long...
2569 cg
->ppd_line
= startline
;
2570 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2582 * Didn't finish this line...
2585 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
2586 if (ch
== '\r' || ch
== '\n')
2589 * Line feed or carriage return...
2598 * Check for a trailing line feed...
2601 if ((ch
= cupsFilePeekChar(fp
)) == EOF
)
2604 cupsFileGetChar(fp
);
2609 else if (ch
< ' ' && ch
!= '\t' && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2612 * Other control characters...
2615 cg
->ppd_line
= startline
;
2616 cg
->ppd_status
= PPD_ILLEGAL_CHARACTER
;
2622 else if (ch
!= 0x1a)
2626 if (col
> (PPD_MAX_LINE
- 1))
2629 * Line is too long...
2632 cg
->ppd_line
= startline
;
2633 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2642 if (lineptr
> line
&& lineptr
[-1] == '\n')
2647 DEBUG_printf(("LINE = \"%s\"\n", line
));
2649 if (ch
== EOF
&& lineptr
== line
)
2667 if ((!line
[0] || /* Blank line */
2668 !strncmp(line
, "*%", 2) || /* Comment line */
2669 !strcmp(line
, "*End")) && /* End of multi-line string */
2670 ignoreblank
) /* Ignore these? */
2672 startline
= cg
->ppd_line
+ 1;
2676 if (!strcmp(line
, "*")) /* (Bad) comment line */
2678 if (cg
->ppd_conform
== PPD_CONFORM_RELAXED
)
2680 startline
= cg
->ppd_line
+ 1;
2685 cg
->ppd_line
= startline
;
2686 cg
->ppd_status
= PPD_ILLEGAL_MAIN_KEYWORD
;
2693 if (line
[0] != '*') /* All lines start with an asterisk */
2696 * Allow lines consisting of just whitespace...
2699 for (lineptr
= line
; *lineptr
; lineptr
++)
2700 if (!isspace(*lineptr
& 255))
2705 cg
->ppd_status
= PPD_MISSING_ASTERISK
;
2709 else if (ignoreblank
)
2724 while (*lineptr
!= '\0' && *lineptr
!= ':' && !isspace(*lineptr
& 255))
2726 if (*lineptr
<= ' ' || *lineptr
> 126 || *lineptr
== '/' ||
2727 (keyptr
- keyword
) >= (PPD_MAX_NAME
- 1))
2729 cg
->ppd_status
= PPD_ILLEGAL_MAIN_KEYWORD
;
2734 *keyptr
++ = *lineptr
++;
2739 if (!strcmp(keyword
, "End"))
2742 mask
|= PPD_KEYWORD
;
2744 /* DEBUG_printf(("keyword = \"%s\", lineptr = \"%s\"\n", keyword, lineptr));*/
2746 if (isspace(*lineptr
& 255))
2749 * Get an option name...
2752 while (isspace(*lineptr
& 255))
2757 while (*lineptr
!= '\0' && !isspace(*lineptr
& 255) && *lineptr
!= ':' &&
2760 if (*lineptr
<= ' ' || *lineptr
> 126 ||
2761 (optptr
- option
) >= (PPD_MAX_NAME
- 1))
2763 cg
->ppd_status
= PPD_ILLEGAL_OPTION_KEYWORD
;
2768 *optptr
++ = *lineptr
++;
2773 if (isspace(*lineptr
& 255) && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2775 cg
->ppd_status
= PPD_ILLEGAL_WHITESPACE
;
2780 while (isspace(*lineptr
& 255))
2785 /* DEBUG_printf(("option = \"%s\", lineptr = \"%s\"\n", option, lineptr));*/
2787 if (*lineptr
== '/')
2790 * Get human-readable text...
2797 while (*lineptr
!= '\0' && *lineptr
!= '\n' && *lineptr
!= ':')
2799 if (((unsigned char)*lineptr
< ' ' && *lineptr
!= '\t') ||
2800 (textptr
- text
) >= (PPD_MAX_LINE
- 1))
2802 cg
->ppd_status
= PPD_ILLEGAL_TRANSLATION
;
2807 *textptr
++ = *lineptr
++;
2811 textlen
= ppd_decode(text
);
2813 if (textlen
> PPD_MAX_TEXT
&& cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2815 cg
->ppd_status
= PPD_ILLEGAL_TRANSLATION
;
2823 /* DEBUG_printf(("text = \"%s\", lineptr = \"%s\"\n", text, lineptr));*/
2826 if (isspace(*lineptr
& 255) && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2828 cg
->ppd_status
= PPD_ILLEGAL_WHITESPACE
;
2833 while (isspace(*lineptr
& 255))
2836 if (*lineptr
== ':')
2839 * Get string after triming leading and trailing whitespace...
2843 while (isspace(*lineptr
& 255))
2846 strptr
= lineptr
+ strlen(lineptr
) - 1;
2847 while (strptr
>= lineptr
&& isspace(*strptr
& 255))
2850 if (*strptr
== '\"')
2853 * Quoted string by itself...
2856 *string
= malloc(strlen(lineptr
) + 1);
2860 for (; *lineptr
!= '\0'; lineptr
++)
2861 if (*lineptr
!= '\"')
2862 *strptr
++ = *lineptr
;
2867 *string
= strdup(lineptr
);
2869 /* DEBUG_printf(("string = \"%s\", lineptr = \"%s\"\n", *string, lineptr));*/