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
))
280 cupsArrayDelete(ppd
->coptions
);
283 * Free the whole record...
291 * 'ppdErrorString()' - Returns the text assocated with a status.
293 * @since CUPS 1.1.19@
296 const char * /* O - Status string */
297 ppdErrorString(ppd_status_t status
) /* I - PPD status */
299 static const char * const messages
[] =/* Status messages */
302 _("Unable to open PPD file"),
303 _("NULL PPD file pointer"),
304 _("Memory allocation error"),
305 _("Missing PPD-Adobe-4.x header"),
306 _("Missing value string"),
309 _("OpenGroup without a CloseGroup first"),
310 _("Bad OpenUI/JCLOpenUI"),
311 _("OpenUI/JCLOpenUI without a CloseUI/JCLCloseUI first"),
312 _("Bad OrderDependency"),
313 _("Bad UIConstraints"),
314 _("Missing asterisk in column 1"),
315 _("Line longer than the maximum allowed (255 characters)"),
316 _("Illegal control character"),
317 _("Illegal main keyword string"),
318 _("Illegal option keyword string"),
319 _("Illegal translation string"),
320 _("Illegal whitespace character"),
321 _("Bad custom parameter")
325 if (status
< PPD_OK
|| status
> PPD_ILLEGAL_WHITESPACE
)
326 return (_cupsLangString(cupsLangDefault(), _("Unknown")));
328 return (_cupsLangString(cupsLangDefault(), messages
[status
]));
333 * 'ppdLastError()' - Return the status from the last ppdOpen*().
335 * @since CUPS 1.1.19@
338 ppd_status_t
/* O - Status code */
339 ppdLastError(int *line
) /* O - Line number */
341 _cups_globals_t
*cg
= _cupsGlobals();
346 *line
= cg
->ppd_line
;
348 return (cg
->ppd_status
);
353 * 'ppdOpen()' - Read a PPD file into memory.
356 ppd_file_t
* /* O - PPD file record */
357 ppdOpen(FILE *fp
) /* I - File to read from */
359 ppd_file_t
*ppd
; /* PPD file record */
360 cups_file_t
*cf
; /* CUPS file */
364 * Reopen the stdio file as a CUPS file...
367 if ((cf
= cupsFileOpenFd(fileno(fp
), "r")) == NULL
)
371 * Load the PPD file using the newer API...
377 * Close the CUPS file and return the PPD...
387 * 'ppdOpen2()' - Read a PPD file into memory.
392 ppd_file_t
* /* O - PPD file record */
393 ppdOpen2(cups_file_t
*fp
) /* I - File to read from */
395 char *oldlocale
; /* Old locale settings */
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 int ui_keyword
; /* Is this line a UI keyword? */
423 _cups_globals_t
*cg
= _cupsGlobals();
425 static const char * const ui_keywords
[] =
427 /* Boolean keywords */
437 /* PickOne keywords */
450 "JCLFrameBufferSize",
475 * Default to "OK" status...
478 cg
->ppd_status
= PPD_OK
;
482 * Range check input...
487 cg
->ppd_status
= PPD_NULL_FILE
;
492 * Grab the first line and make sure it reads '*PPD-Adobe: "major.minor"'...
495 mask
= ppd_read(fp
, keyword
, name
, text
, &string
, 0, cg
);
497 DEBUG_printf(("mask=%x, keyword=\"%s\"...\n", mask
, keyword
));
500 strcmp(keyword
, "PPD-Adobe") ||
501 string
== NULL
|| string
[0] != '4')
504 * Either this is not a PPD file, or it is not a 4.x PPD file.
507 if (cg
->ppd_status
== PPD_OK
)
508 cg
->ppd_status
= PPD_MISSING_PPDADOBE4
;
515 DEBUG_printf(("ppdOpen: keyword = %s, string = %p\n", keyword
, string
));
520 * Allocate memory for the PPD file record...
523 if ((ppd
= calloc(1, sizeof(ppd_file_t
))) == NULL
)
525 cg
->ppd_status
= PPD_ALLOC_ERROR
;
530 ppd
->language_level
= 1;
531 ppd
->color_device
= 0;
532 ppd
->colorspace
= PPD_CS_GRAY
;
533 ppd
->landscape
= -90;
534 ppd
->coptions
= cupsArrayNew((cups_array_func_t
)ppd_compare_coptions
,
538 * Get the default language for the user...
541 language
= cupsLangDefault();
544 oldlocale
= _cupsSaveLocale(LC_NUMERIC
, "C");
546 oldlocale
= _cupsSaveLocale(LC_ALL
, "C");
547 #endif /* LC_NUMERIC */
550 * Read lines from the PPD file and add them to the file record...
559 while ((mask
= ppd_read(fp
, keyword
, name
, text
, &string
, 1, cg
)) != 0)
562 printf("mask = %x, keyword = \"%s\"", mask
, keyword
);
565 printf(", name = \"%s\"", name
);
568 printf(", text = \"%s\"", text
);
572 if (strlen(string
) > 40)
573 printf(", string = %p", string
);
575 printf(", string = \"%s\"", string
);
581 if (strcmp(keyword
, "CloseUI") && strcmp(keyword
, "CloseGroup") &&
582 strcmp(keyword
, "CloseSubGroup") && strncmp(keyword
, "Default", 7) &&
583 strcmp(keyword
, "JCLCloseUI") && strcmp(keyword
, "JCLOpenUI") &&
584 strcmp(keyword
, "OpenUI") && strcmp(keyword
, "OpenGroup") &&
585 strcmp(keyword
, "OpenSubGroup") && string
== NULL
)
588 * Need a string value!
591 cg
->ppd_status
= PPD_MISSING_VALUE
;
597 * Certain main keywords (as defined by the PPD spec) may be used
598 * without the usual OpenUI/CloseUI stuff. Presumably this is just
599 * so that Adobe wouldn't completely break compatibility with PPD
600 * files prior to v4.0 of the spec, but it is hopelessly
601 * inconsistent... Catch these main keywords and automatically
602 * create the corresponding option, as needed...
608 * Previous line was a UI keyword...
615 if (option
== NULL
&&
616 (mask
& (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
)) ==
617 (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
))
619 for (i
= 0; i
< (int)(sizeof(ui_keywords
) / sizeof(ui_keywords
[0])); i
++)
620 if (!strcmp(keyword
, ui_keywords
[i
]))
623 if (i
< (int)(sizeof(ui_keywords
) / sizeof(ui_keywords
[0])))
626 * Create the option in the appropriate group...
631 DEBUG_printf(("**** FOUND ADOBE UI KEYWORD %s WITHOUT OPENUI!\n",
636 if (strcmp(keyword
, "Collate") && strcmp(keyword
, "Duplex") &&
637 strcmp(keyword
, "InputSlot") && strcmp(keyword
, "ManualFeed") &&
638 strcmp(keyword
, "MediaType") && strcmp(keyword
, "MediaColor") &&
639 strcmp(keyword
, "MediaWeight") && strcmp(keyword
, "OutputBin") &&
640 strcmp(keyword
, "OutputMode") && strcmp(keyword
, "OutputOrder") &&
641 strcmp(keyword
, "PageSize") && strcmp(keyword
, "PageRegion"))
642 group
= ppd_get_group(ppd
, "Extra", _("Extra"), cg
);
644 group
= ppd_get_group(ppd
, "General", _("General"), cg
);
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
));
770 sscanf(string
, "%f%f%f%f%f%f%f%f%f%f%f", &(profile
->density
),
772 profile
->matrix
[0] + 0, profile
->matrix
[0] + 1,
773 profile
->matrix
[0] + 2, profile
->matrix
[1] + 0,
774 profile
->matrix
[1] + 1, profile
->matrix
[1] + 2,
775 profile
->matrix
[2] + 0, profile
->matrix
[2] + 1,
776 profile
->matrix
[2] + 2);
778 else if (!strcmp(keyword
, "cupsFilter"))
780 if (ppd
->num_filters
== 0)
781 filter
= malloc(sizeof(char *));
783 filter
= realloc(ppd
->filters
, sizeof(char *) * (ppd
->num_filters
+ 1));
789 cg
->ppd_status
= PPD_ALLOC_ERROR
;
794 ppd
->filters
= filter
;
795 filter
+= ppd
->num_filters
;
799 * Copy filter string and prevent it from being freed below...
805 else if (!strcmp(keyword
, "Throughput"))
806 ppd
->throughput
= atoi(string
);
807 else if (!strcmp(keyword
, "Font"))
810 * Add this font to the list of available fonts...
813 if (ppd
->num_fonts
== 0)
814 tempfonts
= (char **)malloc(sizeof(char *));
816 tempfonts
= (char **)realloc(ppd
->fonts
,
817 sizeof(char *) * (ppd
->num_fonts
+ 1));
819 if (tempfonts
== NULL
)
821 cg
->ppd_status
= PPD_ALLOC_ERROR
;
826 ppd
->fonts
= tempfonts
;
827 ppd
->fonts
[ppd
->num_fonts
] = strdup(name
);
831 else if (!strcmp(keyword
, "ParamCustomPageSize"))
833 if (!strcmp(name
, "Width"))
834 sscanf(string
, "%*s%*s%f%f", ppd
->custom_min
+ 0,
835 ppd
->custom_max
+ 0);
836 else if (!strcmp(name
, "Height"))
837 sscanf(string
, "%*s%*s%f%f", ppd
->custom_min
+ 1,
838 ppd
->custom_max
+ 1);
841 else if (!strncmp(keyword
, "ParamCustom", 11))
843 ppd_coption_t
*coption
; /* Custom option */
844 ppd_cparam_t
*cparam
; /* Custom parameter */
845 int corder
; /* Order number */
846 char ctype
[33], /* Data type */
847 cminimum
[65], /* Minimum value */
848 cmaximum
[65]; /* Maximum value */
852 * Get the custom option and parameter...
855 if ((coption
= ppd_get_coption(ppd
, keyword
+ 11)) == NULL
)
857 cg
->ppd_status
= PPD_ALLOC_ERROR
;
862 if ((cparam
= ppd_get_cparam(coption
, name
, text
)) == NULL
)
864 cg
->ppd_status
= PPD_ALLOC_ERROR
;
870 * Get the parameter data...
873 if (sscanf(string
, "%d%32s%64s%64s", &corder
, ctype
, cminimum
,
876 cg
->ppd_status
= PPD_BAD_CUSTOM_PARAM
;
881 cparam
->order
= corder
;
883 if (!strcmp(ctype
, "curve"))
885 cparam
->type
= PPD_CUSTOM_CURVE
;
886 cparam
->minimum
.custom_curve
= atof(cminimum
);
887 cparam
->maximum
.custom_curve
= atof(cmaximum
);
889 else if (!strcmp(ctype
, "int"))
891 cparam
->type
= PPD_CUSTOM_INT
;
892 cparam
->minimum
.custom_int
= atoi(cminimum
);
893 cparam
->maximum
.custom_int
= atoi(cmaximum
);
895 else if (!strcmp(ctype
, "invcurve"))
897 cparam
->type
= PPD_CUSTOM_INVCURVE
;
898 cparam
->minimum
.custom_invcurve
= atof(cminimum
);
899 cparam
->maximum
.custom_invcurve
= atof(cmaximum
);
901 else if (!strcmp(ctype
, "passcode"))
903 cparam
->type
= PPD_CUSTOM_PASSCODE
;
904 cparam
->minimum
.custom_passcode
= strdup(cminimum
);
905 cparam
->maximum
.custom_passcode
= strdup(cmaximum
);
907 else if (!strcmp(ctype
, "password"))
909 cparam
->type
= PPD_CUSTOM_PASSWORD
;
910 cparam
->minimum
.custom_password
= strdup(cminimum
);
911 cparam
->maximum
.custom_password
= strdup(cmaximum
);
913 else if (!strcmp(ctype
, "points"))
915 cparam
->type
= PPD_CUSTOM_POINTS
;
916 cparam
->minimum
.custom_points
= atof(cminimum
);
917 cparam
->maximum
.custom_points
= atof(cmaximum
);
919 else if (!strcmp(ctype
, "real"))
921 cparam
->type
= PPD_CUSTOM_REAL
;
922 cparam
->minimum
.custom_real
= atof(cminimum
);
923 cparam
->maximum
.custom_real
= atof(cmaximum
);
925 else if (!strcmp(ctype
, "string"))
927 cparam
->type
= PPD_CUSTOM_STRING
;
928 cparam
->minimum
.custom_string
= strdup(cminimum
);
929 cparam
->maximum
.custom_string
= strdup(cmaximum
);
933 cg
->ppd_status
= PPD_BAD_CUSTOM_PARAM
;
939 * Now special-case for CustomPageSize...
942 if (!strcmp(coption
->keyword
, "PageSize"))
944 if (!strcmp(name
, "Width"))
946 ppd
->custom_min
[0] = cparam
->minimum
.custom_points
;
947 ppd
->custom_max
[0] = cparam
->maximum
.custom_points
;
949 else if (!strcmp(name
, "Height"))
951 ppd
->custom_min
[1] = cparam
->minimum
.custom_points
;
952 ppd
->custom_max
[1] = cparam
->maximum
.custom_points
;
956 else if (!strcmp(keyword
, "HWMargins"))
957 sscanf(string
, "%f%f%f%f", ppd
->custom_margins
+ 0,
958 ppd
->custom_margins
+ 1, ppd
->custom_margins
+ 2,
959 ppd
->custom_margins
+ 3);
960 else if (!strncmp(keyword
, "Custom", 6) && !strcmp(name
, "True"))
962 ppd_coption_t
*coption
; /* Custom option */
965 DEBUG_puts("Processing Custom option...");
968 * Get the option and custom option...
971 if ((option
= ppdFindOption(ppd
, keyword
+ 6)) == NULL
)
973 ppd_group_t
*gtemp
; /* Temporary group */
976 DEBUG_printf(("%s option not found for %s...\n", keyword
+ 6, keyword
));
978 if ((gtemp
= ppd_get_group(ppd
, "General", _("General"), cg
)) == NULL
)
980 DEBUG_puts("Unable to get general group!");
985 if ((option
= ppd_get_option(gtemp
, keyword
+ 6)) == NULL
)
987 DEBUG_printf(("Unable to get %s option!\n", keyword
+ 6));
989 cg
->ppd_status
= PPD_ALLOC_ERROR
;
995 if ((coption
= ppd_get_coption(ppd
, keyword
+ 6)) == NULL
)
997 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1003 * Add the "custom" option...
1006 if ((choice
= ppd_add_choice(option
, "Custom")) == NULL
)
1008 DEBUG_puts("Unable to add Custom choice!");
1010 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1015 strlcpy(choice
->text
, text
[0] ? text
: _("Custom"),
1016 sizeof(choice
->text
));
1018 choice
->code
= string
;
1019 string
= NULL
; /* Don't add as an attribute below */
1023 * Now process custom page sizes specially...
1026 if (!strcmp(keyword
, "CustomPageSize"))
1028 ppd
->variable_sizes
= 1;
1031 * Add a "Custom" page size entry...
1034 ppd_add_size(ppd
, "Custom");
1037 else if (!strcmp(keyword
, "LandscapeOrientation"))
1039 if (!strcmp(string
, "Minus90"))
1040 ppd
->landscape
= -90;
1041 else if (!strcmp(string
, "Plus90"))
1042 ppd
->landscape
= 90;
1044 else if (!strcmp(keyword
, "Emulators"))
1046 for (count
= 1, sptr
= string
; sptr
!= NULL
;)
1047 if ((sptr
= strchr(sptr
, ' ')) != NULL
)
1050 while (*sptr
== ' ')
1054 ppd
->num_emulations
= count
;
1055 ppd
->emulations
= calloc(count
, sizeof(ppd_emul_t
));
1057 for (i
= 0, sptr
= string
; i
< count
; i
++)
1059 for (nameptr
= ppd
->emulations
[i
].name
;
1060 *sptr
!= '\0' && *sptr
!= ' ';
1062 if (nameptr
< (ppd
->emulations
[i
].name
+ sizeof(ppd
->emulations
[i
].name
) - 1))
1067 while (*sptr
== ' ')
1071 else if (!strncmp(keyword
, "StartEmulator_", 14))
1075 for (i
= 0; i
< ppd
->num_emulations
; i
++)
1076 if (!strcmp(keyword
+ 14, ppd
->emulations
[i
].name
))
1078 ppd
->emulations
[i
].start
= string
;
1082 else if (!strncmp(keyword
, "StopEmulator_", 13))
1086 for (i
= 0; i
< ppd
->num_emulations
; i
++)
1087 if (!strcmp(keyword
+ 13, ppd
->emulations
[i
].name
))
1089 ppd
->emulations
[i
].stop
= string
;
1093 else if (!strcmp(keyword
, "JobPatchFile"))
1095 if (ppd
->patches
== NULL
)
1096 ppd
->patches
= strdup(string
);
1099 temp
= realloc(ppd
->patches
, strlen(ppd
->patches
) +
1100 strlen(string
) + 1);
1103 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1108 ppd
->patches
= temp
;
1110 strcpy(ppd
->patches
+ strlen(ppd
->patches
), string
);
1113 else if (!strcmp(keyword
, "OpenUI"))
1116 * Don't allow nesting of options...
1119 if (option
&& cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1121 cg
->ppd_status
= PPD_NESTED_OPEN_UI
;
1127 * Add an option record to the current sub-group, group, or file...
1131 _cups_strcpy(name
, name
+ 1); /* Eliminate leading asterisk */
1133 for (i
= (int)strlen(name
) - 1; i
> 0 && isspace(name
[i
] & 255); i
--)
1134 name
[i
] = '\0'; /* Eliminate trailing spaces */
1136 DEBUG_printf(("OpenUI of %s in group %s...\n", name
,
1137 group
? group
->text
: "(null)"));
1139 if (subgroup
!= NULL
)
1140 option
= ppd_get_option(subgroup
, name
);
1141 else if (group
== NULL
)
1143 if (strcmp(name
, "Collate") && strcmp(name
, "Duplex") &&
1144 strcmp(name
, "InputSlot") && strcmp(name
, "ManualFeed") &&
1145 strcmp(name
, "MediaType") && strcmp(name
, "MediaColor") &&
1146 strcmp(name
, "MediaWeight") && strcmp(name
, "OutputBin") &&
1147 strcmp(name
, "OutputMode") && strcmp(name
, "OutputOrder") &&
1148 strcmp(name
, "PageSize") && strcmp(name
, "PageRegion"))
1149 group
= ppd_get_group(ppd
, "Extra", _("Extra"), cg
);
1151 group
= ppd_get_group(ppd
, "General", _("General"), cg
);
1156 DEBUG_printf(("Adding to group %s...\n", group
->text
));
1157 option
= ppd_get_option(group
, name
);
1161 option
= ppd_get_option(group
, name
);
1165 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1171 * Now fill in the initial information for the option...
1174 if (string
&& !strcmp(string
, "PickMany"))
1175 option
->ui
= PPD_UI_PICKMANY
;
1176 else if (string
&& !strcmp(string
, "Boolean"))
1177 option
->ui
= PPD_UI_BOOLEAN
;
1178 else if (string
&& !strcmp(string
, "PickOne"))
1179 option
->ui
= PPD_UI_PICKONE
;
1180 else if (cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1182 cg
->ppd_status
= PPD_BAD_OPEN_UI
;
1187 option
->ui
= PPD_UI_PICKONE
;
1189 for (j
= 0; j
< ppd
->num_attrs
; j
++)
1190 if (!strncmp(ppd
->attrs
[j
]->name
, "Default", 7) &&
1191 !strcmp(ppd
->attrs
[j
]->name
+ 7, name
) &&
1192 ppd
->attrs
[j
]->value
)
1194 DEBUG_printf(("Setting Default%s to %s via attribute...\n",
1195 option
->keyword
, ppd
->attrs
[j
]->value
));
1196 strlcpy(option
->defchoice
, ppd
->attrs
[j
]->value
,
1197 sizeof(option
->defchoice
));
1202 strlcpy(option
->text
, text
, sizeof(option
->text
));
1205 if (!strcmp(name
, "PageSize"))
1206 strlcpy(option
->text
, _("Media Size"), sizeof(option
->text
));
1207 else if (!strcmp(name
, "MediaType"))
1208 strlcpy(option
->text
, _("Media Type"), sizeof(option
->text
));
1209 else if (!strcmp(name
, "InputSlot"))
1210 strlcpy(option
->text
, _("Media Source"), sizeof(option
->text
));
1211 else if (!strcmp(name
, "ColorModel"))
1212 strlcpy(option
->text
, _("Output Mode"), sizeof(option
->text
));
1213 else if (!strcmp(name
, "Resolution"))
1214 strlcpy(option
->text
, _("Resolution"), sizeof(option
->text
));
1216 strlcpy(option
->text
, name
, sizeof(option
->text
));
1219 option
->section
= PPD_ORDER_ANY
;
1224 else if (!strcmp(keyword
, "JCLOpenUI"))
1227 * Don't allow nesting of options...
1230 if (option
&& cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1232 cg
->ppd_status
= PPD_NESTED_OPEN_UI
;
1238 * Find the JCL group, and add if needed...
1241 group
= ppd_get_group(ppd
, "JCL", _("JCL"), cg
);
1247 * Add an option record to the current JCLs...
1251 _cups_strcpy(name
, name
+ 1);
1253 option
= ppd_get_option(group
, name
);
1257 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1263 * Now fill in the initial information for the option...
1266 if (string
&& !strcmp(string
, "PickMany"))
1267 option
->ui
= PPD_UI_PICKMANY
;
1268 else if (string
&& !strcmp(string
, "Boolean"))
1269 option
->ui
= PPD_UI_BOOLEAN
;
1270 else if (string
&& !strcmp(string
, "PickOne"))
1271 option
->ui
= PPD_UI_PICKONE
;
1274 cg
->ppd_status
= PPD_BAD_OPEN_UI
;
1279 for (j
= 0; j
< ppd
->num_attrs
; j
++)
1280 if (!strncmp(ppd
->attrs
[j
]->name
, "Default", 7) &&
1281 !strcmp(ppd
->attrs
[j
]->name
+ 7, name
) &&
1282 ppd
->attrs
[j
]->value
)
1284 DEBUG_printf(("Setting Default%s to %s via attribute...\n",
1285 option
->keyword
, ppd
->attrs
[j
]->value
));
1286 strlcpy(option
->defchoice
, ppd
->attrs
[j
]->value
,
1287 sizeof(option
->defchoice
));
1291 strlcpy(option
->text
, text
, sizeof(option
->text
));
1293 option
->section
= PPD_ORDER_JCL
;
1299 else if (!strcmp(keyword
, "CloseUI") || !strcmp(keyword
, "JCLCloseUI"))
1306 else if (!strcmp(keyword
, "OpenGroup"))
1309 * Open a new group...
1314 cg
->ppd_status
= PPD_NESTED_OPEN_GROUP
;
1321 cg
->ppd_status
= PPD_BAD_OPEN_GROUP
;
1327 * Separate the group name from the text (name/text)...
1330 if ((sptr
= strchr(string
, '/')) != NULL
)
1336 * Fix up the text...
1342 * Find/add the group...
1345 group
= ppd_get_group(ppd
, string
, sptr
, cg
);
1353 else if (!strcmp(keyword
, "CloseGroup"))
1360 else if (!strcmp(keyword
, "OrderDependency") ||
1361 !strcmp(keyword
, "NonUIOrderDependency"))
1363 if (sscanf(string
, "%f%40s%40s", &order
, name
, keyword
) != 3)
1365 cg
->ppd_status
= PPD_BAD_ORDER_DEPENDENCY
;
1370 if (keyword
[0] == '*')
1371 _cups_strcpy(keyword
, keyword
+ 1);
1373 if (!strcmp(name
, "ExitServer"))
1374 section
= PPD_ORDER_EXIT
;
1375 else if (!strcmp(name
, "Prolog"))
1376 section
= PPD_ORDER_PROLOG
;
1377 else if (!strcmp(name
, "DocumentSetup"))
1378 section
= PPD_ORDER_DOCUMENT
;
1379 else if (!strcmp(name
, "PageSetup"))
1380 section
= PPD_ORDER_PAGE
;
1381 else if (!strcmp(name
, "JCLSetup"))
1382 section
= PPD_ORDER_JCL
;
1384 section
= PPD_ORDER_ANY
;
1392 * Only valid for Non-UI options...
1395 for (i
= ppd
->num_groups
, gtemp
= ppd
->groups
; i
> 0; i
--, gtemp
++)
1396 if (gtemp
->text
[0] == '\0')
1400 for (i
= 0; i
< gtemp
->num_options
; i
++)
1401 if (!strcmp(keyword
, gtemp
->options
[i
].keyword
))
1403 gtemp
->options
[i
].section
= section
;
1404 gtemp
->options
[i
].order
= order
;
1410 option
->section
= section
;
1411 option
->order
= order
;
1417 else if (!strncmp(keyword
, "Default", 7))
1423 * Drop UI text, if any, from value...
1426 if (strchr(string
, '/') != NULL
)
1427 *strchr(string
, '/') = '\0';
1430 * Assign the default value as appropriate...
1433 if (!strcmp(keyword
, "DefaultColorSpace"))
1436 * Set default colorspace...
1439 if (!strcmp(string
, "CMY"))
1440 ppd
->colorspace
= PPD_CS_CMY
;
1441 else if (!strcmp(string
, "CMYK"))
1442 ppd
->colorspace
= PPD_CS_CMYK
;
1443 else if (!strcmp(string
, "RGB"))
1444 ppd
->colorspace
= PPD_CS_RGB
;
1445 else if (!strcmp(string
, "RGBK"))
1446 ppd
->colorspace
= PPD_CS_RGBK
;
1447 else if (!strcmp(string
, "N"))
1448 ppd
->colorspace
= PPD_CS_N
;
1450 ppd
->colorspace
= PPD_CS_GRAY
;
1452 else if (option
&& !strcmp(keyword
+ 7, option
->keyword
))
1455 * Set the default as part of the current option...
1458 DEBUG_printf(("Setting %s to %s...\n", keyword
, string
));
1460 strlcpy(option
->defchoice
, string
, sizeof(option
->defchoice
));
1462 DEBUG_printf(("%s is now %s...\n", keyword
, option
->defchoice
));
1467 * Lookup option and set if it has been defined...
1470 ppd_option_t
*toption
; /* Temporary option */
1473 if ((toption
= ppdFindOption(ppd
, keyword
+ 7)) != NULL
)
1475 DEBUG_printf(("Setting %s to %s...\n", keyword
, string
));
1476 strlcpy(toption
->defchoice
, string
, sizeof(toption
->defchoice
));
1480 else if (!strcmp(keyword
, "UIConstraints") ||
1481 !strcmp(keyword
, "NonUIConstraints"))
1483 if (ppd
->num_consts
== 0)
1484 constraint
= calloc(1, sizeof(ppd_const_t
));
1486 constraint
= realloc(ppd
->consts
,
1487 (ppd
->num_consts
+ 1) * sizeof(ppd_const_t
));
1489 if (constraint
== NULL
)
1491 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1496 ppd
->consts
= constraint
;
1497 constraint
+= ppd
->num_consts
;
1500 switch (sscanf(string
, "%40s%40s%40s%40s", constraint
->option1
,
1501 constraint
->choice1
, constraint
->option2
,
1502 constraint
->choice2
))
1504 case 0 : /* Error */
1505 case 1 : /* Error */
1506 cg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1509 case 2 : /* Two options... */
1511 * The following strcpy's are safe, as optionN and
1512 * choiceN are all the same size (size defined by PPD spec...)
1515 if (constraint
->option1
[0] == '*')
1516 _cups_strcpy(constraint
->option1
, constraint
->option1
+ 1);
1518 if (constraint
->choice1
[0] == '*')
1519 _cups_strcpy(constraint
->option2
, constraint
->choice1
+ 1);
1521 _cups_strcpy(constraint
->option2
, constraint
->choice1
);
1523 constraint
->choice1
[0] = '\0';
1524 constraint
->choice2
[0] = '\0';
1527 case 3 : /* Two options, one choice... */
1529 * The following _cups_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] == '*')
1538 _cups_strcpy(constraint
->choice2
, constraint
->option2
);
1539 _cups_strcpy(constraint
->option2
, constraint
->choice1
+ 1);
1540 constraint
->choice1
[0] = '\0';
1544 if (constraint
->option2
[0] == '*')
1545 _cups_strcpy(constraint
->option2
, constraint
->option2
+ 1);
1547 constraint
->choice2
[0] = '\0';
1551 case 4 : /* Two options, two choices... */
1552 if (constraint
->option1
[0] == '*')
1553 _cups_strcpy(constraint
->option1
, constraint
->option1
+ 1);
1555 if (constraint
->option2
[0] == '*')
1556 _cups_strcpy(constraint
->option2
, constraint
->option2
+ 1);
1563 else if (!strcmp(keyword
, "PaperDimension"))
1565 if ((size
= ppdPageSize(ppd
, name
)) == NULL
)
1566 size
= ppd_add_size(ppd
, name
);
1571 * Unable to add or find size!
1574 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1579 sscanf(string
, "%f%f", &(size
->width
), &(size
->length
));
1584 else if (!strcmp(keyword
, "ImageableArea"))
1586 if ((size
= ppdPageSize(ppd
, name
)) == NULL
)
1587 size
= ppd_add_size(ppd
, name
);
1592 * Unable to add or find size!
1595 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1600 sscanf(string
, "%f%f%f%f", &(size
->left
), &(size
->bottom
),
1601 &(size
->right
), &(size
->top
));
1606 else if (option
!= NULL
&&
1607 (mask
& (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
)) ==
1608 (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
) &&
1609 !strcmp(keyword
, option
->keyword
))
1611 DEBUG_printf(("group = %p, subgroup = %p\n", group
, subgroup
));
1613 if (!strcmp(keyword
, "PageSize"))
1616 * Add a page size...
1619 if (ppdPageSize(ppd
, name
) == NULL
)
1620 ppd_add_size(ppd
, name
);
1624 * Add the option choice...
1627 choice
= ppd_add_choice(option
, name
);
1629 if (mask
& PPD_TEXT
)
1630 strlcpy(choice
->text
, text
, sizeof(choice
->text
));
1631 else if (!strcmp(name
, "True"))
1632 strcpy(choice
->text
, _("Yes"));
1633 else if (!strcmp(name
, "False"))
1634 strcpy(choice
->text
, _("No"));
1636 strlcpy(choice
->text
, name
, sizeof(choice
->text
));
1638 if (option
->section
== PPD_ORDER_JCL
)
1639 ppd_decode(string
); /* Decode quoted string */
1641 choice
->code
= string
;
1642 string
= NULL
; /* Don't add as an attribute below */
1646 * Add remaining lines with keywords and string values as attributes...
1650 (mask
& (PPD_KEYWORD
| PPD_STRING
)) == (PPD_KEYWORD
| PPD_STRING
))
1651 ppd_add_attr(ppd
, keyword
, name
, text
, string
);
1657 * Reset language preferences...
1660 cupsLangFree(language
);
1663 _cupsRestoreLocale(LC_NUMERIC
, oldlocale
);
1665 _cupsRestoreLocale(LC_ALL
, oldlocale
);
1666 #endif /* LC_NUMERIC */
1670 printf("Premature EOF at %lu...\n", (unsigned long)ftell(fp
));
1673 if (cg
->ppd_status
!= PPD_OK
)
1676 * Had an error reading the PPD file, cannot continue!
1685 * Create the sorted options array and set the option back-pointer for
1686 * each choice and custom option...
1689 ppd
->options
= cupsArrayNew((cups_array_func_t
)ppd_compare_options
, NULL
);
1691 for (i
= ppd
->num_groups
, group
= ppd
->groups
;
1695 for (j
= group
->num_options
, option
= group
->options
;
1699 ppd_coption_t
*coption
; /* Custom option */
1702 cupsArrayAdd(ppd
->options
, option
);
1704 for (k
= 0; k
< option
->num_choices
; k
++)
1705 option
->choices
[k
].option
= option
;
1707 if ((coption
= ppdFindCustomOption(ppd
, option
->keyword
)) != NULL
)
1708 coption
->option
= option
;
1713 * Sort the attributes...
1716 if (ppd
->num_attrs
> 1)
1717 qsort(ppd
->attrs
, ppd
->num_attrs
, sizeof(ppd_attr_t
*),
1718 (int (*)(const void *, const void *))_ppd_attr_compare
);
1721 * Return the PPD file structure...
1727 * Common exit point for errors to save code size...
1736 cupsLangFree(language
);
1739 _cupsRestoreLocale(LC_NUMERIC
, oldlocale
);
1741 _cupsRestoreLocale(LC_ALL
, oldlocale
);
1742 #endif /* LC_NUMERIC */
1749 * 'ppdOpenFd()' - Read a PPD file into memory.
1752 ppd_file_t
* /* O - PPD file record */
1753 ppdOpenFd(int fd
) /* I - File to read from */
1755 cups_file_t
*fp
; /* CUPS file pointer */
1756 ppd_file_t
*ppd
; /* PPD file record */
1757 _cups_globals_t
*cg
= _cupsGlobals();
1762 * Set the line number to 0...
1768 * Range check input...
1773 cg
->ppd_status
= PPD_NULL_FILE
;
1779 * Try to open the file and parse it...
1782 if ((fp
= cupsFileOpenFd(fd
, "r")) != NULL
)
1790 cg
->ppd_status
= PPD_FILE_OPEN_ERROR
;
1799 * 'ppdOpenFile()' - Read a PPD file into memory.
1802 ppd_file_t
* /* O - PPD file record */
1803 ppdOpenFile(const char *filename
) /* I - File to read from */
1805 cups_file_t
*fp
; /* File pointer */
1806 ppd_file_t
*ppd
; /* PPD file record */
1807 _cups_globals_t
*cg
= _cupsGlobals();
1812 * Set the line number to 0...
1818 * Range check input...
1821 if (filename
== NULL
)
1823 cg
->ppd_status
= PPD_NULL_FILE
;
1829 * Try to open the file and parse it...
1832 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
1840 cg
->ppd_status
= PPD_FILE_OPEN_ERROR
;
1849 * 'ppdSetConformance()' - Set the conformance level for PPD files.
1851 * @since CUPS 1.1.20@
1855 ppdSetConformance(ppd_conform_t c
) /* I - Conformance level */
1857 _cups_globals_t
*cg
= _cupsGlobals();
1861 cg
->ppd_conform
= c
;
1866 * 'ppd_add_attr()' - Add an attribute to the PPD data.
1869 static ppd_attr_t
* /* O - New attribute */
1870 ppd_add_attr(ppd_file_t
*ppd
, /* I - PPD file data */
1871 const char *name
, /* I - Attribute name */
1872 const char *spec
, /* I - Specifier string, if any */
1873 const char *text
, /* I - Text string, if any */
1874 const char *value
) /* I - Value of attribute */
1876 ppd_attr_t
**ptr
, /* New array */
1877 *temp
; /* New attribute */
1881 * Range check input...
1884 if (ppd
== NULL
|| name
== NULL
|| spec
== NULL
)
1888 * Allocate memory for the new attribute...
1891 if (ppd
->num_attrs
== 0)
1892 ptr
= malloc(sizeof(ppd_attr_t
*));
1894 ptr
= realloc(ppd
->attrs
, (ppd
->num_attrs
+ 1) * sizeof(ppd_attr_t
*));
1900 ptr
+= ppd
->num_attrs
;
1902 if ((temp
= calloc(1, sizeof(ppd_attr_t
))) == NULL
)
1913 strlcpy(temp
->name
, name
, sizeof(temp
->name
));
1914 strlcpy(temp
->spec
, spec
, sizeof(temp
->spec
));
1915 strlcpy(temp
->text
, text
, sizeof(temp
->text
));
1916 temp
->value
= (char *)value
;
1919 * Return the attribute...
1927 * 'ppd_add_choice()' - Add a choice to an option.
1930 static ppd_choice_t
* /* O - Named choice */
1931 ppd_add_choice(ppd_option_t
*option
, /* I - Option */
1932 const char *name
) /* I - Name of choice */
1934 ppd_choice_t
*choice
; /* Choice */
1937 if (option
->num_choices
== 0)
1938 choice
= malloc(sizeof(ppd_choice_t
));
1940 choice
= realloc(option
->choices
,
1941 sizeof(ppd_choice_t
) * (option
->num_choices
+ 1));
1946 option
->choices
= choice
;
1947 choice
+= option
->num_choices
;
1948 option
->num_choices
++;
1950 memset(choice
, 0, sizeof(ppd_choice_t
));
1951 strlcpy(choice
->choice
, name
, sizeof(choice
->choice
));
1958 * 'ppd_add_size()' - Add a page size.
1961 static ppd_size_t
* /* O - Named size */
1962 ppd_add_size(ppd_file_t
*ppd
, /* I - PPD file */
1963 const char *name
) /* I - Name of size */
1965 ppd_size_t
*size
; /* Size */
1968 if (ppd
->num_sizes
== 0)
1969 size
= malloc(sizeof(ppd_size_t
));
1971 size
= realloc(ppd
->sizes
, sizeof(ppd_size_t
) * (ppd
->num_sizes
+ 1));
1977 size
+= ppd
->num_sizes
;
1980 memset(size
, 0, sizeof(ppd_size_t
));
1981 strlcpy(size
->name
, name
, sizeof(size
->name
));
1988 * 'ppd_compare_coptions()' - Compare two custom options.
1991 static int /* O - Result of comparison */
1992 ppd_compare_coptions(ppd_coption_t
*a
, /* I - First option */
1993 ppd_coption_t
*b
) /* I - Second option */
1995 return (strcasecmp(a
->keyword
, b
->keyword
));
2000 * 'ppd_compare_cparams()' - Compare two custom parameters.
2003 static int /* O - Result of comparison */
2004 ppd_compare_cparams(ppd_cparam_t
*a
, /* I - First parameter */
2005 ppd_cparam_t
*b
) /* I - Second parameter */
2007 return (strcasecmp(a
->name
, b
->name
));
2012 * 'ppd_compare_options()' - Compare two options.
2015 static int /* O - Result of comparison */
2016 ppd_compare_options(ppd_option_t
*a
, /* I - First option */
2017 ppd_option_t
*b
) /* I - Second option */
2019 return (strcasecmp(a
->keyword
, b
->keyword
));
2024 * 'ppd_decode()' - Decode a string value...
2027 static int /* O - Length of decoded string */
2028 ppd_decode(char *string
) /* I - String to decode */
2030 char *inptr
, /* Input pointer */
2031 *outptr
; /* Output pointer */
2037 while (*inptr
!= '\0')
2038 if (*inptr
== '<' && isxdigit(inptr
[1] & 255))
2041 * Convert hex to 8-bit values...
2045 while (isxdigit(*inptr
& 255))
2047 if (isalpha(*inptr
))
2048 *outptr
= (tolower(*inptr
) - 'a' + 10) << 4;
2050 *outptr
= (*inptr
- '0') << 4;
2054 if (!isxdigit(*inptr
& 255))
2057 if (isalpha(*inptr
))
2058 *outptr
|= tolower(*inptr
) - 'a' + 10;
2060 *outptr
|= *inptr
- '0';
2066 while (*inptr
!= '>' && *inptr
!= '\0')
2068 while (*inptr
== '>')
2072 *outptr
++ = *inptr
++;
2076 return ((int)(outptr
- string
));
2081 * 'ppd_free_group()' - Free a single UI group.
2085 ppd_free_group(ppd_group_t
*group
) /* I - Group to free */
2087 int i
; /* Looping var */
2088 ppd_option_t
*option
; /* Current option */
2089 ppd_group_t
*subgroup
; /* Current sub-group */
2092 if (group
->num_options
> 0)
2094 for (i
= group
->num_options
, option
= group
->options
;
2097 ppd_free_option(option
);
2099 ppd_free(group
->options
);
2102 if (group
->num_subgroups
> 0)
2104 for (i
= group
->num_subgroups
, subgroup
= group
->subgroups
;
2107 ppd_free_group(subgroup
);
2109 ppd_free(group
->subgroups
);
2115 * 'ppd_free_option()' - Free a single option.
2119 ppd_free_option(ppd_option_t
*option
) /* I - Option to free */
2121 int i
; /* Looping var */
2122 ppd_choice_t
*choice
; /* Current choice */
2125 if (option
->num_choices
> 0)
2127 for (i
= option
->num_choices
, choice
= option
->choices
;
2131 ppd_free(choice
->code
);
2134 ppd_free(option
->choices
);
2140 * 'ppd_get_coption()' - Get a custom option record.
2143 static ppd_coption_t
* /* O - Custom option... */
2144 ppd_get_coption(ppd_file_t
*ppd
, /* I - PPD file */
2145 const char *name
) /* I - Name of option */
2147 ppd_coption_t
*copt
; /* New custom option */
2151 * See if the option already exists...
2154 if ((copt
= ppdFindCustomOption(ppd
, name
)) != NULL
)
2158 * Not found, so create the custom option record...
2161 if ((copt
= calloc(1, sizeof(ppd_coption_t
))) == NULL
)
2164 strlcpy(copt
->keyword
, name
, sizeof(copt
->keyword
));
2166 copt
->params
= cupsArrayNew((cups_array_func_t
)ppd_compare_cparams
, NULL
);
2169 * Return the new record...
2177 * 'ppd_get_cparam()' - Get a custom parameter record.
2180 static ppd_cparam_t
* /* O - Extended option... */
2181 ppd_get_cparam(ppd_coption_t
*opt
, /* I - PPD file */
2182 const char *param
, /* I - Name of parameter */
2183 const char *text
) /* I - Human-readable text */
2185 ppd_cparam_t
*cparam
; /* New custom parameter */
2189 * See if the parameter already exists...
2192 if ((cparam
= ppdFindCustomParam(opt
, param
)) != NULL
)
2196 * Not found, so create the custom parameter record...
2199 if ((cparam
= calloc(1, sizeof(ppd_cparam_t
))) == NULL
)
2202 strlcpy(cparam
->name
, param
, sizeof(cparam
->name
));
2203 strlcpy(cparam
->text
, text
, sizeof(cparam
->text
));
2206 * Add this record to the array...
2209 cupsArrayAdd(opt
->params
, cparam
);
2212 * Return the new record...
2220 * 'ppd_get_group()' - Find or create the named group as needed.
2223 static ppd_group_t
* /* O - Named group */
2224 ppd_get_group(ppd_file_t
*ppd
, /* I - PPD file */
2225 const char *name
, /* I - Name of group */
2226 const char *text
, /* I - Text for group */
2227 _cups_globals_t
*cg
) /* I - Global data */
2229 int i
; /* Looping var */
2230 ppd_group_t
*group
; /* Group */
2233 DEBUG_printf(("ppd_get_group(ppd=%p, name=\"%s\", text=\"%s\", cg=%p)\n",
2234 ppd
, name
, text
, cg
));
2236 for (i
= ppd
->num_groups
, group
= ppd
->groups
; i
> 0; i
--, group
++)
2237 if (!strcmp(group
->name
, name
))
2242 DEBUG_printf(("Adding group %s...\n", name
));
2244 if (cg
->ppd_conform
== PPD_CONFORM_STRICT
&& strlen(text
) >= sizeof(group
->text
))
2246 cg
->ppd_status
= PPD_ILLEGAL_TRANSLATION
;
2251 if (ppd
->num_groups
== 0)
2252 group
= malloc(sizeof(ppd_group_t
));
2254 group
= realloc(ppd
->groups
,
2255 (ppd
->num_groups
+ 1) * sizeof(ppd_group_t
));
2259 cg
->ppd_status
= PPD_ALLOC_ERROR
;
2264 ppd
->groups
= group
;
2265 group
+= ppd
->num_groups
;
2268 memset(group
, 0, sizeof(ppd_group_t
));
2269 strlcpy(group
->name
, name
, sizeof(group
->name
));
2270 strlcpy(group
->text
, text
, sizeof(group
->text
));
2278 * 'ppd_get_option()' - Find or create the named option as needed.
2281 static ppd_option_t
* /* O - Named option */
2282 ppd_get_option(ppd_group_t
*group
, /* I - Group */
2283 const char *name
) /* I - Name of option */
2285 int i
; /* Looping var */
2286 ppd_option_t
*option
; /* Option */
2289 DEBUG_printf(("ppd_get_option(group=%p(\"%s\"), name=\"%s\")\n",
2290 group
, group
->name
, name
));
2292 for (i
= group
->num_options
, option
= group
->options
; i
> 0; i
--, option
++)
2293 if (!strcmp(option
->keyword
, name
))
2298 if (group
->num_options
== 0)
2299 option
= malloc(sizeof(ppd_option_t
));
2301 option
= realloc(group
->options
,
2302 (group
->num_options
+ 1) * sizeof(ppd_option_t
));
2307 group
->options
= option
;
2308 option
+= group
->num_options
;
2309 group
->num_options
++;
2311 memset(option
, 0, sizeof(ppd_option_t
));
2312 strlcpy(option
->keyword
, name
, sizeof(option
->keyword
));
2320 * 'ppd_read()' - Read a line from a PPD file, skipping comment lines as
2324 static int /* O - Bitmask of fields read */
2325 ppd_read(cups_file_t
*fp
, /* I - File to read from */
2326 char *keyword
, /* O - Keyword from line */
2327 char *option
, /* O - Option from line */
2328 char *text
, /* O - Human-readable text from line */
2329 char **string
, /* O - Code/string data */
2330 int ignoreblank
, /* I - Ignore blank lines? */
2331 _cups_globals_t
*cg
) /* I - Global data */
2333 int ch
, /* Character from file */
2334 col
, /* Column in line */
2335 colon
, /* Colon seen? */
2336 endquote
, /* Waiting for an end quote */
2337 mask
, /* Mask to be returned */
2338 startline
, /* Start line */
2339 textlen
; /* Length of text */
2340 char *keyptr
, /* Keyword pointer */
2341 *optptr
, /* Option pointer */
2342 *textptr
, /* Text pointer */
2343 *strptr
, /* Pointer into string */
2344 *lineptr
, /* Current position in line buffer */
2345 *line
; /* Line buffer */
2346 int linesize
; /* Current size of line buffer */
2349 * Range check everything...
2352 if (!fp
|| !keyword
|| !option
|| !text
|| !string
)
2356 * Now loop until we have a valid line...
2361 startline
= cg
->ppd_line
+ 1;
2363 line
= malloc(linesize
);
2378 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
2380 if (lineptr
>= (line
+ linesize
- 1))
2383 * Expand the line buffer...
2386 char *temp
; /* Temporary line pointer */
2390 if (linesize
> 262144)
2393 * Don't allow lines longer than 256k!
2396 cg
->ppd_line
= startline
;
2397 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2404 temp
= realloc(line
, linesize
);
2407 cg
->ppd_line
= startline
;
2408 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2415 lineptr
= temp
+ (lineptr
- line
);
2419 if (ch
== '\r' || ch
== '\n')
2422 * Line feed or carriage return...
2431 * Check for a trailing line feed...
2434 if ((ch
= cupsFilePeekChar(fp
)) == EOF
)
2437 cupsFileGetChar(fp
);
2440 if (lineptr
== line
&& ignoreblank
)
2441 continue; /* Skip blank lines */
2445 if (!endquote
) /* Continue for multi-line text */
2450 else if (ch
< ' ' && ch
!= '\t' && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2453 * Other control characters...
2456 cg
->ppd_line
= startline
;
2457 cg
->ppd_status
= PPD_ILLEGAL_CHARACTER
;
2463 else if (ch
!= 0x1a)
2466 * Any other character...
2472 if (col
> (PPD_MAX_LINE
- 1))
2475 * Line is too long...
2478 cg
->ppd_line
= startline
;
2479 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2486 if (ch
== ':' && strncmp(line
, "*%", 2) != 0)
2489 if (ch
== '\"' && colon
)
2490 endquote
= !endquote
;
2497 * Didn't finish this quoted string...
2500 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
2503 else if (ch
== '\r' || ch
== '\n')
2511 * Check for a trailing line feed...
2514 if ((ch
= cupsFilePeekChar(fp
)) == EOF
)
2517 cupsFileGetChar(fp
);
2522 else if (ch
< ' ' && ch
!= '\t' && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2525 * Other control characters...
2528 cg
->ppd_line
= startline
;
2529 cg
->ppd_status
= PPD_ILLEGAL_CHARACTER
;
2535 else if (ch
!= 0x1a)
2539 if (col
> (PPD_MAX_LINE
- 1))
2542 * Line is too long...
2545 cg
->ppd_line
= startline
;
2546 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2558 * Didn't finish this line...
2561 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
2562 if (ch
== '\r' || ch
== '\n')
2565 * Line feed or carriage return...
2574 * Check for a trailing line feed...
2577 if ((ch
= cupsFilePeekChar(fp
)) == EOF
)
2580 cupsFileGetChar(fp
);
2585 else if (ch
< ' ' && ch
!= '\t' && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2588 * Other control characters...
2591 cg
->ppd_line
= startline
;
2592 cg
->ppd_status
= PPD_ILLEGAL_CHARACTER
;
2598 else if (ch
!= 0x1a)
2602 if (col
> (PPD_MAX_LINE
- 1))
2605 * Line is too long...
2608 cg
->ppd_line
= startline
;
2609 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2618 if (lineptr
> line
&& lineptr
[-1] == '\n')
2623 DEBUG_printf(("LINE = \"%s\"\n", line
));
2625 if (ch
== EOF
&& lineptr
== line
)
2643 if ((!line
[0] || /* Blank line */
2644 !strncmp(line
, "*%", 2) || /* Comment line */
2645 !strcmp(line
, "*End")) && /* End of multi-line string */
2646 ignoreblank
) /* Ignore these? */
2648 startline
= cg
->ppd_line
+ 1;
2652 if (!strcmp(line
, "*")) /* (Bad) comment line */
2654 if (cg
->ppd_conform
== PPD_CONFORM_RELAXED
)
2656 startline
= cg
->ppd_line
+ 1;
2661 cg
->ppd_line
= startline
;
2662 cg
->ppd_status
= PPD_ILLEGAL_MAIN_KEYWORD
;
2669 if (line
[0] != '*') /* All lines start with an asterisk */
2671 if (cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2673 cg
->ppd_status
= PPD_MISSING_ASTERISK
;
2679 * Allow lines consisting of just whitespace...
2682 for (lineptr
= line
; *lineptr
; lineptr
++)
2683 if (!isspace(*lineptr
& 255))
2688 cg
->ppd_status
= PPD_MISSING_ASTERISK
;
2692 else if (ignoreblank
)
2707 while (*lineptr
!= '\0' && *lineptr
!= ':' && !isspace(*lineptr
& 255))
2709 if (*lineptr
<= ' ' || *lineptr
> 126 || *lineptr
== '/' ||
2710 (keyptr
- keyword
) >= (PPD_MAX_NAME
- 1))
2712 cg
->ppd_status
= PPD_ILLEGAL_MAIN_KEYWORD
;
2717 *keyptr
++ = *lineptr
++;
2722 if (!strcmp(keyword
, "End"))
2725 mask
|= PPD_KEYWORD
;
2727 /* DEBUG_printf(("keyword = \"%s\", lineptr = \"%s\"\n", keyword, lineptr));*/
2729 if (isspace(*lineptr
& 255))
2732 * Get an option name...
2735 while (isspace(*lineptr
& 255))
2740 while (*lineptr
!= '\0' && !isspace(*lineptr
& 255) && *lineptr
!= ':' &&
2743 if (*lineptr
<= ' ' || *lineptr
> 126 ||
2744 (optptr
- option
) >= (PPD_MAX_NAME
- 1))
2746 cg
->ppd_status
= PPD_ILLEGAL_OPTION_KEYWORD
;
2751 *optptr
++ = *lineptr
++;
2756 if (isspace(*lineptr
& 255) && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2758 cg
->ppd_status
= PPD_ILLEGAL_WHITESPACE
;
2763 while (isspace(*lineptr
& 255))
2768 /* DEBUG_printf(("option = \"%s\", lineptr = \"%s\"\n", option, lineptr));*/
2770 if (*lineptr
== '/')
2773 * Get human-readable text...
2780 while (*lineptr
!= '\0' && *lineptr
!= '\n' && *lineptr
!= ':')
2782 if (((unsigned char)*lineptr
< ' ' && *lineptr
!= '\t') ||
2783 (textptr
- text
) >= (PPD_MAX_LINE
- 1))
2785 cg
->ppd_status
= PPD_ILLEGAL_TRANSLATION
;
2790 *textptr
++ = *lineptr
++;
2794 textlen
= ppd_decode(text
);
2796 if (textlen
> PPD_MAX_TEXT
&& cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2798 cg
->ppd_status
= PPD_ILLEGAL_TRANSLATION
;
2806 /* DEBUG_printf(("text = \"%s\", lineptr = \"%s\"\n", text, lineptr));*/
2809 if (isspace(*lineptr
& 255) && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2811 cg
->ppd_status
= PPD_ILLEGAL_WHITESPACE
;
2816 while (isspace(*lineptr
& 255))
2819 if (*lineptr
== ':')
2822 * Get string after triming leading and trailing whitespace...
2826 while (isspace(*lineptr
& 255))
2829 strptr
= lineptr
+ strlen(lineptr
) - 1;
2830 while (strptr
>= lineptr
&& isspace(*strptr
& 255))
2833 if (*strptr
== '\"')
2836 * Quoted string by itself...
2839 *string
= malloc(strlen(lineptr
) + 1);
2843 for (; *lineptr
!= '\0'; lineptr
++)
2844 if (*lineptr
!= '\"')
2845 *strptr
++ = *lineptr
;
2850 *string
= strdup(lineptr
);
2852 /* DEBUG_printf(("string = \"%s\", lineptr = \"%s\"\n", *string, lineptr));*/