2 * "$Id: ppd.c 5200 2006-02-28 00:10:32Z mike $"
4 * PPD file routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * PostScript is a trademark of Adobe Systems, Inc.
26 * This code and any derivative of it may be used and distributed
27 * freely under the terms of the GNU General Public License when
28 * used with GNU Ghostscript or its derivatives. Use of the code
29 * (or any derivative of it) with software other than GNU
30 * GhostScript (or its derivatives) is governed by the CUPS license
33 * This file is subject to the Apple OS-Developed Software exception.
37 * ppdClose() - Free all memory used by the PPD file.
38 * ppdErrorString() - Returns the text assocated with a status.
39 * ppdLastError() - Return the status from the last ppdOpen*().
40 * ppdOpen() - Read a PPD file into memory.
41 * ppdOpen2() - Read a PPD file into memory.
42 * ppdOpenFd() - Read a PPD file into memory.
43 * ppdOpenFile() - Read a PPD file into memory.
44 * ppdSetConformance() - Set the conformance level for PPD files.
45 * ppd_add_attr() - Add an attribute to the PPD data.
46 * ppd_add_choice() - Add a choice to an option.
47 * ppd_add_size() - Add a page size.
48 * ppd_compare_attrs() - Compare two attributes.
49 * ppd_compare_coptions() - Compare two custom options.
50 * ppd_compare_cparams() - Compare two custom parameters.
51 * ppd_compare_options() - Compare two options.
52 * ppd_decode() - Decode a string value...
53 * ppd_free_group() - Free a single UI group.
54 * ppd_free_option() - Free a single option.
55 * ppd_get_coption() - Get a custom option record.
56 * ppd_get_cparam() - Get a custom parameter record.
57 * ppd_get_group() - Find or create the named group as needed.
58 * ppd_get_option() - Find or create the named option as needed.
59 * ppd_read() - Read a line from a PPD file, skipping comment
64 * Include necessary headers.
76 #if defined(WIN32) || defined(__EMX__)
77 # define READ_BINARY "rb" /* Open a binary file for reading */
78 # define WRITE_BINARY "wb" /* Open a binary file for writing */
80 # define READ_BINARY "r" /* Open a binary file for reading */
81 # define WRITE_BINARY "w" /* Open a binary file for writing */
82 #endif /* WIN32 || __EMX__ */
84 #define ppd_free(p) if (p) free(p) /* Safe free macro */
86 #define PPD_KEYWORD 1 /* Line contained a keyword */
87 #define PPD_OPTION 2 /* Line contained an option name */
88 #define PPD_TEXT 4 /* Line contained human-readable text */
89 #define PPD_STRING 8 /* Line contained a string or code */
96 static ppd_attr_t
*ppd_add_attr(ppd_file_t
*ppd
, const char *name
,
97 const char *spec
, const char *text
,
99 static ppd_choice_t
*ppd_add_choice(ppd_option_t
*option
, const char *name
);
100 static ppd_size_t
*ppd_add_size(ppd_file_t
*ppd
, const char *name
);
101 static int ppd_compare_attrs(ppd_attr_t
*a
, ppd_attr_t
*b
);
102 static int ppd_compare_coptions(ppd_coption_t
*a
,
104 static int ppd_compare_cparams(ppd_cparam_t
*a
, ppd_cparam_t
*b
);
105 static int ppd_compare_options(ppd_option_t
*a
, ppd_option_t
*b
);
106 static int ppd_decode(char *string
);
107 static void ppd_free_group(ppd_group_t
*group
);
108 static void ppd_free_option(ppd_option_t
*option
);
109 static ppd_coption_t
*ppd_get_coption(ppd_file_t
*ppd
, const char *name
);
110 static ppd_cparam_t
*ppd_get_cparam(ppd_coption_t
*opt
,
113 static ppd_group_t
*ppd_get_group(ppd_file_t
*ppd
, const char *name
,
114 const char *text
, _cups_globals_t
*cg
);
115 static ppd_option_t
*ppd_get_option(ppd_group_t
*group
, const char *name
);
116 static int ppd_read(cups_file_t
*fp
, char *keyword
, char *option
,
117 char *text
, char **string
, int ignoreblank
,
118 _cups_globals_t
*cg
);
122 * 'ppdClose()' - Free all memory used by the PPD file.
126 ppdClose(ppd_file_t
*ppd
) /* I - PPD file record */
128 int i
; /* Looping var */
129 ppd_emul_t
*emul
; /* Current emulation */
130 ppd_group_t
*group
; /* Current group */
131 char **font
; /* Current font */
132 char **filter
; /* Current filter */
133 ppd_attr_t
**attr
; /* Current attribute */
134 ppd_coption_t
*coption
; /* Current custom option */
135 ppd_cparam_t
*cparam
; /* Current custom parameter */
139 * Range check arguments...
146 * Free all strings at the top level...
149 ppd_free(ppd
->patches
);
150 ppd_free(ppd
->jcl_begin
);
151 ppd_free(ppd
->jcl_end
);
152 ppd_free(ppd
->jcl_ps
);
155 * Free any emulations...
158 if (ppd
->num_emulations
> 0)
160 for (i
= ppd
->num_emulations
, emul
= ppd
->emulations
; i
> 0; i
--, emul
++)
162 ppd_free(emul
->start
);
163 ppd_free(emul
->stop
);
166 ppd_free(ppd
->emulations
);
170 * Free any UI groups, subgroups, and options...
173 if (ppd
->num_groups
> 0)
175 for (i
= ppd
->num_groups
, group
= ppd
->groups
; i
> 0; i
--, group
++)
176 ppd_free_group(group
);
178 ppd_free(ppd
->groups
);
181 cupsArrayDelete(ppd
->options
);
184 * Free any page sizes...
187 if (ppd
->num_sizes
> 0)
188 ppd_free(ppd
->sizes
);
191 * Free any constraints...
194 if (ppd
->num_consts
> 0)
195 ppd_free(ppd
->consts
);
198 * Free any filters...
201 if (ppd
->num_filters
> 0)
203 for (i
= ppd
->num_filters
, filter
= ppd
->filters
; i
> 0; i
--, filter
++)
208 ppd_free(ppd
->filters
);
215 if (ppd
->num_fonts
> 0)
217 for (i
= ppd
->num_fonts
, font
= ppd
->fonts
; i
> 0; i
--, font
++)
222 ppd_free(ppd
->fonts
);
226 * Free any profiles...
229 if (ppd
->num_profiles
> 0)
230 ppd_free(ppd
->profiles
);
233 * Free any attributes...
236 if (ppd
->num_attrs
> 0)
238 for (i
= ppd
->num_attrs
, attr
= ppd
->attrs
; i
> 0; i
--, attr
++)
240 ppd_free((*attr
)->value
);
244 ppd_free(ppd
->attrs
);
247 cupsArrayDelete(ppd
->sorted_attrs
);
250 * Free custom options...
253 for (coption
= (ppd_coption_t
*)cupsArrayFirst(ppd
->coptions
);
255 coption
= (ppd_coption_t
*)cupsArrayNext(ppd
->coptions
))
257 for (cparam
= (ppd_cparam_t
*)cupsArrayFirst(coption
->params
);
259 cparam
= (ppd_cparam_t
*)cupsArrayNext(coption
->params
))
261 switch (cparam
->type
)
263 case PPD_CUSTOM_PASSCODE
:
264 case PPD_CUSTOM_PASSWORD
:
265 case PPD_CUSTOM_STRING
:
266 ppd_free(cparam
->current
.custom_string
);
276 cupsArrayDelete(coption
->params
);
281 cupsArrayDelete(ppd
->coptions
);
284 * Free the whole record...
292 * 'ppdErrorString()' - Returns the text assocated with a status.
294 * @since CUPS 1.1.19@
297 const char * /* O - Status string */
298 ppdErrorString(ppd_status_t status
) /* I - PPD status */
300 static const char * const messages
[] =/* Status messages */
303 _("Unable to open PPD file"),
304 _("NULL PPD file pointer"),
305 _("Memory allocation error"),
306 _("Missing PPD-Adobe-4.x header"),
307 _("Missing value string"),
310 _("OpenGroup without a CloseGroup first"),
311 _("Bad OpenUI/JCLOpenUI"),
312 _("OpenUI/JCLOpenUI without a CloseUI/JCLCloseUI first"),
313 _("Bad OrderDependency"),
314 _("Bad UIConstraints"),
315 _("Missing asterisk in column 1"),
316 _("Line longer than the maximum allowed (255 characters)"),
317 _("Illegal control character"),
318 _("Illegal main keyword string"),
319 _("Illegal option keyword string"),
320 _("Illegal translation string"),
321 _("Illegal whitespace character"),
322 _("Bad custom parameter")
326 if (status
< PPD_OK
|| status
> PPD_ILLEGAL_WHITESPACE
)
327 return (_cupsLangString(cupsLangDefault(), _("Unknown")));
329 return (_cupsLangString(cupsLangDefault(), messages
[status
]));
334 * 'ppdLastError()' - Return the status from the last ppdOpen*().
336 * @since CUPS 1.1.19@
339 ppd_status_t
/* O - Status code */
340 ppdLastError(int *line
) /* O - Line number */
342 _cups_globals_t
*cg
= _cupsGlobals();
347 *line
= cg
->ppd_line
;
349 return (cg
->ppd_status
);
354 * 'ppdOpen()' - Read a PPD file into memory.
357 ppd_file_t
* /* O - PPD file record */
358 ppdOpen(FILE *fp
) /* I - File to read from */
360 ppd_file_t
*ppd
; /* PPD file record */
361 cups_file_t
*cf
; /* CUPS file */
365 * Reopen the stdio file as a CUPS file...
368 if ((cf
= cupsFileOpenFd(fileno(fp
), "r")) == NULL
)
372 * Load the PPD file using the newer API...
378 * Close the CUPS file and return the PPD...
388 * 'ppdOpen2()' - Read a PPD file into memory.
393 ppd_file_t
* /* O - PPD file record */
394 ppdOpen2(cups_file_t
*fp
) /* I - File to read from */
396 char *oldlocale
; /* Old locale settings */
397 int i
, j
, k
; /* Looping vars */
398 int count
; /* Temporary count */
399 ppd_file_t
*ppd
; /* PPD file record */
400 ppd_group_t
*group
, /* Current group */
401 *subgroup
; /* Current sub-group */
402 ppd_option_t
*option
; /* Current option */
403 ppd_choice_t
*choice
; /* Current choice */
404 ppd_const_t
*constraint
; /* Current constraint */
405 ppd_size_t
*size
; /* Current page size */
406 int mask
; /* Line data mask */
407 char keyword
[PPD_MAX_NAME
],
408 /* Keyword from file */
410 /* Option from file */
412 /* Human-readable text from file */
413 *string
, /* Code/text from file */
414 *sptr
, /* Pointer into string */
415 *nameptr
, /* Pointer into name */
416 *temp
, /* Temporary string pointer */
417 **tempfonts
; /* Temporary fonts pointer */
418 float order
; /* Order dependency number */
419 ppd_section_t section
; /* Order dependency section */
420 ppd_profile_t
*profile
; /* Pointer to color profile */
421 char **filter
; /* Pointer to filter */
422 cups_lang_t
*language
; /* Default language */
423 int ui_keyword
; /* Is this line a UI keyword? */
424 _cups_globals_t
*cg
= _cupsGlobals();
426 static const char * const ui_keywords
[] =
428 #ifdef CUPS_USE_FULL_UI_KEYWORDS_LIST
430 * Adobe defines some 41 keywords as "UI", meaning that they are
431 * user interface elements and that they should be treated as such
432 * even if the PPD creator doesn't use Open/CloseUI around them.
434 * Since this can cause previously invisible options to appear and
435 * confuse users, the default is to only treat the PageSize and
436 * PageRegion keywords this way.
438 /* Boolean keywords */
448 /* PickOne keywords */
461 "JCLFrameBufferSize",
482 #else /* !CUPS_USE_FULL_UI_KEYWORDS_LIST */
485 #endif /* CUPS_USE_FULL_UI_KEYWORDS_LIST */
490 * Default to "OK" status...
493 cg
->ppd_status
= PPD_OK
;
497 * Range check input...
502 cg
->ppd_status
= PPD_NULL_FILE
;
507 * Grab the first line and make sure it reads '*PPD-Adobe: "major.minor"'...
510 mask
= ppd_read(fp
, keyword
, name
, text
, &string
, 0, cg
);
512 DEBUG_printf(("mask=%x, keyword=\"%s\"...\n", mask
, keyword
));
515 strcmp(keyword
, "PPD-Adobe") ||
516 string
== NULL
|| string
[0] != '4')
519 * Either this is not a PPD file, or it is not a 4.x PPD file.
522 if (cg
->ppd_status
== PPD_OK
)
523 cg
->ppd_status
= PPD_MISSING_PPDADOBE4
;
530 DEBUG_printf(("ppdOpen: keyword = %s, string = %p\n", keyword
, string
));
535 * Allocate memory for the PPD file record...
538 if ((ppd
= calloc(1, sizeof(ppd_file_t
))) == NULL
)
540 cg
->ppd_status
= PPD_ALLOC_ERROR
;
545 ppd
->language_level
= 1;
546 ppd
->color_device
= 0;
547 ppd
->colorspace
= PPD_CS_GRAY
;
548 ppd
->landscape
= -90;
549 ppd
->coptions
= cupsArrayNew((cups_array_func_t
)ppd_compare_coptions
,
553 * Get the default language for the user...
556 language
= cupsLangDefault();
559 oldlocale
= _cupsSaveLocale(LC_NUMERIC
, "C");
561 oldlocale
= _cupsSaveLocale(LC_ALL
, "C");
562 #endif /* LC_NUMERIC */
565 * Read lines from the PPD file and add them to the file record...
574 while ((mask
= ppd_read(fp
, keyword
, name
, text
, &string
, 1, cg
)) != 0)
577 printf("mask = %x, keyword = \"%s\"", mask
, keyword
);
580 printf(", name = \"%s\"", name
);
583 printf(", text = \"%s\"", text
);
587 if (strlen(string
) > 40)
588 printf(", string = %p", string
);
590 printf(", string = \"%s\"", string
);
596 if (strcmp(keyword
, "CloseUI") && strcmp(keyword
, "CloseGroup") &&
597 strcmp(keyword
, "CloseSubGroup") && strncmp(keyword
, "Default", 7) &&
598 strcmp(keyword
, "JCLCloseUI") && strcmp(keyword
, "JCLOpenUI") &&
599 strcmp(keyword
, "OpenUI") && strcmp(keyword
, "OpenGroup") &&
600 strcmp(keyword
, "OpenSubGroup") && string
== NULL
)
603 * Need a string value!
606 cg
->ppd_status
= PPD_MISSING_VALUE
;
612 * Certain main keywords (as defined by the PPD spec) may be used
613 * without the usual OpenUI/CloseUI stuff. Presumably this is just
614 * so that Adobe wouldn't completely break compatibility with PPD
615 * files prior to v4.0 of the spec, but it is hopelessly
616 * inconsistent... Catch these main keywords and automatically
617 * create the corresponding option, as needed...
623 * Previous line was a UI keyword...
630 if (option
== NULL
&&
631 (mask
& (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
)) ==
632 (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
))
634 for (i
= 0; i
< (int)(sizeof(ui_keywords
) / sizeof(ui_keywords
[0])); i
++)
635 if (!strcmp(keyword
, ui_keywords
[i
]))
638 if (i
< (int)(sizeof(ui_keywords
) / sizeof(ui_keywords
[0])))
641 * Create the option in the appropriate group...
646 DEBUG_printf(("**** FOUND ADOBE UI KEYWORD %s WITHOUT OPENUI!\n",
651 if ((group
= ppd_get_group(ppd
, "General", _("General"), cg
)) == NULL
)
654 DEBUG_printf(("Adding to group %s...\n", group
->text
));
655 option
= ppd_get_option(group
, keyword
);
659 option
= ppd_get_option(group
, keyword
);
663 cg
->ppd_status
= PPD_ALLOC_ERROR
;
669 * Now fill in the initial information for the option...
672 if (!strncmp(keyword
, "JCL", 3))
673 option
->section
= PPD_ORDER_JCL
;
675 option
->section
= PPD_ORDER_ANY
;
677 option
->order
= 10.0f
;
680 option
->ui
= PPD_UI_BOOLEAN
;
682 option
->ui
= PPD_UI_PICKONE
;
684 for (j
= 0; j
< ppd
->num_attrs
; j
++)
685 if (!strncmp(ppd
->attrs
[j
]->name
, "Default", 7) &&
686 !strcmp(ppd
->attrs
[j
]->name
+ 7, keyword
) &&
687 ppd
->attrs
[j
]->value
)
689 DEBUG_printf(("Setting Default%s to %s via attribute...\n",
690 option
->keyword
, ppd
->attrs
[j
]->value
));
691 strlcpy(option
->defchoice
, ppd
->attrs
[j
]->value
,
692 sizeof(option
->defchoice
));
696 if (!strcmp(keyword
, "PageSize"))
697 strlcpy(option
->text
, _("Media Size"), sizeof(option
->text
));
698 else if (!strcmp(keyword
, "MediaType"))
699 strlcpy(option
->text
, _("Media Type"), sizeof(option
->text
));
700 else if (!strcmp(keyword
, "InputSlot"))
701 strlcpy(option
->text
, _("Media Source"), sizeof(option
->text
));
702 else if (!strcmp(keyword
, "ColorModel"))
703 strlcpy(option
->text
, _("Output Mode"), sizeof(option
->text
));
704 else if (!strcmp(keyword
, "Resolution"))
705 strlcpy(option
->text
, _("Resolution"), sizeof(option
->text
));
707 strlcpy(option
->text
, keyword
, sizeof(option
->text
));
711 if (!strcmp(keyword
, "LanguageLevel"))
712 ppd
->language_level
= atoi(string
);
713 else if (!strcmp(keyword
, "LanguageEncoding"))
714 ppd
->lang_encoding
= string
;
715 else if (!strcmp(keyword
, "LanguageVersion"))
716 ppd
->lang_version
= string
;
717 else if (!strcmp(keyword
, "Manufacturer"))
718 ppd
->manufacturer
= string
;
719 else if (!strcmp(keyword
, "ModelName"))
720 ppd
->modelname
= string
;
721 else if (!strcmp(keyword
, "Protocols"))
722 ppd
->protocols
= string
;
723 else if (!strcmp(keyword
, "PCFileName"))
724 ppd
->pcfilename
= string
;
725 else if (!strcmp(keyword
, "NickName"))
726 ppd
->nickname
= string
;
727 else if (!strcmp(keyword
, "Product"))
728 ppd
->product
= string
;
729 else if (!strcmp(keyword
, "ShortNickName"))
730 ppd
->shortnickname
= string
;
731 else if (!strcmp(keyword
, "TTRasterizer"))
732 ppd
->ttrasterizer
= string
;
733 else if (!strcmp(keyword
, "JCLBegin"))
735 ppd
->jcl_begin
= strdup(string
);
736 ppd_decode(ppd
->jcl_begin
); /* Decode quoted string */
738 else if (!strcmp(keyword
, "JCLEnd"))
740 ppd
->jcl_end
= strdup(string
);
741 ppd_decode(ppd
->jcl_end
); /* Decode quoted string */
743 else if (!strcmp(keyword
, "JCLToPSInterpreter"))
745 ppd
->jcl_ps
= strdup(string
);
746 ppd_decode(ppd
->jcl_ps
); /* Decode quoted string */
748 else if (!strcmp(keyword
, "AccurateScreensSupport"))
749 ppd
->accurate_screens
= !strcmp(string
, "True");
750 else if (!strcmp(keyword
, "ColorDevice"))
751 ppd
->color_device
= !strcmp(string
, "True");
752 else if (!strcmp(keyword
, "ContoneOnly"))
753 ppd
->contone_only
= !strcmp(string
, "True");
754 else if (!strcmp(keyword
, "cupsFlipDuplex"))
755 ppd
->flip_duplex
= !strcmp(string
, "True");
756 else if (!strcmp(keyword
, "cupsManualCopies"))
757 ppd
->manual_copies
= !strcmp(string
, "True");
758 else if (!strcmp(keyword
, "cupsModelNumber"))
759 ppd
->model_number
= atoi(string
);
760 else if (!strcmp(keyword
, "cupsColorProfile"))
762 if (ppd
->num_profiles
== 0)
763 profile
= malloc(sizeof(ppd_profile_t
));
765 profile
= realloc(ppd
->profiles
, sizeof(ppd_profile_t
) *
766 (ppd
->num_profiles
+ 1));
768 ppd
->profiles
= profile
;
769 profile
+= ppd
->num_profiles
;
770 ppd
->num_profiles
++;
772 memset(profile
, 0, sizeof(ppd_profile_t
));
773 strlcpy(profile
->resolution
, name
, sizeof(profile
->resolution
));
774 strlcpy(profile
->media_type
, text
, sizeof(profile
->media_type
));
775 sscanf(string
, "%f%f%f%f%f%f%f%f%f%f%f", &(profile
->density
),
777 profile
->matrix
[0] + 0, profile
->matrix
[0] + 1,
778 profile
->matrix
[0] + 2, profile
->matrix
[1] + 0,
779 profile
->matrix
[1] + 1, profile
->matrix
[1] + 2,
780 profile
->matrix
[2] + 0, profile
->matrix
[2] + 1,
781 profile
->matrix
[2] + 2);
783 else if (!strcmp(keyword
, "cupsFilter"))
785 if (ppd
->num_filters
== 0)
786 filter
= malloc(sizeof(char *));
788 filter
= realloc(ppd
->filters
, sizeof(char *) * (ppd
->num_filters
+ 1));
794 cg
->ppd_status
= PPD_ALLOC_ERROR
;
799 ppd
->filters
= filter
;
800 filter
+= ppd
->num_filters
;
804 * Copy filter string and prevent it from being freed below...
810 else if (!strcmp(keyword
, "Throughput"))
811 ppd
->throughput
= atoi(string
);
812 else if (!strcmp(keyword
, "Font"))
815 * Add this font to the list of available fonts...
818 if (ppd
->num_fonts
== 0)
819 tempfonts
= (char **)malloc(sizeof(char *));
821 tempfonts
= (char **)realloc(ppd
->fonts
,
822 sizeof(char *) * (ppd
->num_fonts
+ 1));
824 if (tempfonts
== NULL
)
826 cg
->ppd_status
= PPD_ALLOC_ERROR
;
831 ppd
->fonts
= tempfonts
;
832 ppd
->fonts
[ppd
->num_fonts
] = strdup(name
);
835 else if (!strncmp(keyword
, "ParamCustom", 11))
837 ppd_coption_t
*coption
; /* Custom option */
838 ppd_cparam_t
*cparam
; /* Custom parameter */
839 int corder
; /* Order number */
840 char ctype
[33], /* Data type */
841 cminimum
[65], /* Minimum value */
842 cmaximum
[65]; /* Maximum value */
846 * Get the custom option and parameter...
849 if ((coption
= ppd_get_coption(ppd
, keyword
+ 11)) == NULL
)
851 cg
->ppd_status
= PPD_ALLOC_ERROR
;
856 if ((cparam
= ppd_get_cparam(coption
, name
, text
)) == NULL
)
858 cg
->ppd_status
= PPD_ALLOC_ERROR
;
864 * Get the parameter data...
867 if (sscanf(string
, "%d%32s%64s%64s", &corder
, ctype
, cminimum
,
870 cg
->ppd_status
= PPD_BAD_CUSTOM_PARAM
;
875 cparam
->order
= corder
;
877 if (!strcmp(ctype
, "curve"))
879 cparam
->type
= PPD_CUSTOM_CURVE
;
880 cparam
->minimum
.custom_curve
= atof(cminimum
);
881 cparam
->maximum
.custom_curve
= atof(cmaximum
);
883 else if (!strcmp(ctype
, "int"))
885 cparam
->type
= PPD_CUSTOM_INT
;
886 cparam
->minimum
.custom_int
= atoi(cminimum
);
887 cparam
->maximum
.custom_int
= atoi(cmaximum
);
889 else if (!strcmp(ctype
, "invcurve"))
891 cparam
->type
= PPD_CUSTOM_INVCURVE
;
892 cparam
->minimum
.custom_invcurve
= atof(cminimum
);
893 cparam
->maximum
.custom_invcurve
= atof(cmaximum
);
895 else if (!strcmp(ctype
, "passcode"))
897 cparam
->type
= PPD_CUSTOM_PASSCODE
;
898 cparam
->minimum
.custom_passcode
= atoi(cminimum
);
899 cparam
->maximum
.custom_passcode
= atoi(cmaximum
);
901 else if (!strcmp(ctype
, "password"))
903 cparam
->type
= PPD_CUSTOM_PASSWORD
;
904 cparam
->minimum
.custom_password
= atoi(cminimum
);
905 cparam
->maximum
.custom_password
= atoi(cmaximum
);
907 else if (!strcmp(ctype
, "points"))
909 cparam
->type
= PPD_CUSTOM_POINTS
;
910 cparam
->minimum
.custom_points
= atof(cminimum
);
911 cparam
->maximum
.custom_points
= atof(cmaximum
);
913 else if (!strcmp(ctype
, "real"))
915 cparam
->type
= PPD_CUSTOM_REAL
;
916 cparam
->minimum
.custom_real
= atof(cminimum
);
917 cparam
->maximum
.custom_real
= atof(cmaximum
);
919 else if (!strcmp(ctype
, "string"))
921 cparam
->type
= PPD_CUSTOM_STRING
;
922 cparam
->minimum
.custom_string
= atoi(cminimum
);
923 cparam
->maximum
.custom_string
= atoi(cmaximum
);
927 cg
->ppd_status
= PPD_BAD_CUSTOM_PARAM
;
933 * Now special-case for CustomPageSize...
936 if (!strcmp(coption
->keyword
, "PageSize"))
938 if (!strcmp(name
, "Width"))
940 ppd
->custom_min
[0] = cparam
->minimum
.custom_points
;
941 ppd
->custom_max
[0] = cparam
->maximum
.custom_points
;
943 else if (!strcmp(name
, "Height"))
945 ppd
->custom_min
[1] = cparam
->minimum
.custom_points
;
946 ppd
->custom_max
[1] = cparam
->maximum
.custom_points
;
950 else if (!strcmp(keyword
, "HWMargins"))
951 sscanf(string
, "%f%f%f%f", ppd
->custom_margins
+ 0,
952 ppd
->custom_margins
+ 1, ppd
->custom_margins
+ 2,
953 ppd
->custom_margins
+ 3);
954 else if (!strncmp(keyword
, "Custom", 6) && !strcmp(name
, "True"))
956 ppd_coption_t
*coption
; /* Custom option */
959 DEBUG_puts("Processing Custom option...");
962 * Get the option and custom option...
965 if ((option
= ppdFindOption(ppd
, keyword
+ 6)) == NULL
)
967 ppd_group_t
*gtemp
; /* Temporary group */
970 DEBUG_printf(("%s option not found for %s...\n", keyword
+ 6, keyword
));
972 if ((gtemp
= ppd_get_group(ppd
, "General", _("General"), cg
)) == NULL
)
974 DEBUG_puts("Unable to get general group!");
979 if ((option
= ppd_get_option(gtemp
, keyword
+ 6)) == NULL
)
981 DEBUG_printf(("Unable to get %s option!\n", keyword
+ 6));
983 cg
->ppd_status
= PPD_ALLOC_ERROR
;
989 if ((coption
= ppd_get_coption(ppd
, keyword
+ 6)) == NULL
)
991 cg
->ppd_status
= PPD_ALLOC_ERROR
;
997 * Add the "custom" option...
1000 if ((choice
= ppd_add_choice(option
, "Custom")) == NULL
)
1002 DEBUG_puts("Unable to add Custom choice!");
1004 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1009 strlcpy(choice
->text
, text
[0] ? text
: _("Custom"),
1010 sizeof(choice
->text
));
1012 choice
->code
= string
;
1013 string
= NULL
; /* Don't add as an attribute below */
1017 * Now process custom page sizes specially...
1020 if (!strcmp(keyword
, "CustomPageSize"))
1022 ppd
->variable_sizes
= 1;
1025 * Add a "Custom" page size entry...
1028 ppd_add_size(ppd
, "Custom");
1031 else if (!strcmp(keyword
, "LandscapeOrientation"))
1033 if (!strcmp(string
, "Minus90"))
1034 ppd
->landscape
= -90;
1035 else if (!strcmp(string
, "Plus90"))
1036 ppd
->landscape
= 90;
1038 else if (!strcmp(keyword
, "Emulators"))
1040 for (count
= 1, sptr
= string
; sptr
!= NULL
;)
1041 if ((sptr
= strchr(sptr
, ' ')) != NULL
)
1044 while (*sptr
== ' ')
1048 ppd
->num_emulations
= count
;
1049 ppd
->emulations
= calloc(count
, sizeof(ppd_emul_t
));
1051 for (i
= 0, sptr
= string
; i
< count
; i
++)
1053 for (nameptr
= ppd
->emulations
[i
].name
;
1054 *sptr
!= '\0' && *sptr
!= ' ';
1056 if (nameptr
< (ppd
->emulations
[i
].name
+ sizeof(ppd
->emulations
[i
].name
) - 1))
1061 while (*sptr
== ' ')
1065 else if (!strncmp(keyword
, "StartEmulator_", 14))
1069 for (i
= 0; i
< ppd
->num_emulations
; i
++)
1070 if (!strcmp(keyword
+ 14, ppd
->emulations
[i
].name
))
1072 ppd
->emulations
[i
].start
= string
;
1076 else if (!strncmp(keyword
, "StopEmulator_", 13))
1080 for (i
= 0; i
< ppd
->num_emulations
; i
++)
1081 if (!strcmp(keyword
+ 13, ppd
->emulations
[i
].name
))
1083 ppd
->emulations
[i
].stop
= string
;
1087 else if (!strcmp(keyword
, "JobPatchFile"))
1089 if (ppd
->patches
== NULL
)
1090 ppd
->patches
= strdup(string
);
1093 temp
= realloc(ppd
->patches
, strlen(ppd
->patches
) +
1094 strlen(string
) + 1);
1097 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1102 ppd
->patches
= temp
;
1104 strcpy(ppd
->patches
+ strlen(ppd
->patches
), string
);
1107 else if (!strcmp(keyword
, "OpenUI"))
1110 * Don't allow nesting of options...
1113 if (option
&& cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1115 cg
->ppd_status
= PPD_NESTED_OPEN_UI
;
1121 * Add an option record to the current sub-group, group, or file...
1125 _cups_strcpy(name
, name
+ 1); /* Eliminate leading asterisk */
1127 for (i
= (int)strlen(name
) - 1; i
> 0 && isspace(name
[i
] & 255); i
--)
1128 name
[i
] = '\0'; /* Eliminate trailing spaces */
1130 DEBUG_printf(("OpenUI of %s in group %s...\n", name
,
1131 group
? group
->text
: "(null)"));
1133 if (subgroup
!= NULL
)
1134 option
= ppd_get_option(subgroup
, name
);
1135 else if (group
== NULL
)
1137 if ((group
= ppd_get_group(ppd
, "General", _("General"), cg
)) == NULL
)
1140 DEBUG_printf(("Adding to group %s...\n", group
->text
));
1141 option
= ppd_get_option(group
, name
);
1145 option
= ppd_get_option(group
, name
);
1149 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1155 * Now fill in the initial information for the option...
1158 if (string
&& !strcmp(string
, "PickMany"))
1159 option
->ui
= PPD_UI_PICKMANY
;
1160 else if (string
&& !strcmp(string
, "Boolean"))
1161 option
->ui
= PPD_UI_BOOLEAN
;
1162 else if (string
&& !strcmp(string
, "PickOne"))
1163 option
->ui
= PPD_UI_PICKONE
;
1164 else if (cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1166 cg
->ppd_status
= PPD_BAD_OPEN_UI
;
1171 option
->ui
= PPD_UI_PICKONE
;
1173 for (j
= 0; j
< ppd
->num_attrs
; j
++)
1174 if (!strncmp(ppd
->attrs
[j
]->name
, "Default", 7) &&
1175 !strcmp(ppd
->attrs
[j
]->name
+ 7, name
) &&
1176 ppd
->attrs
[j
]->value
)
1178 DEBUG_printf(("Setting Default%s to %s via attribute...\n",
1179 option
->keyword
, ppd
->attrs
[j
]->value
));
1180 strlcpy(option
->defchoice
, ppd
->attrs
[j
]->value
,
1181 sizeof(option
->defchoice
));
1186 strlcpy(option
->text
, text
, sizeof(option
->text
));
1189 if (!strcmp(name
, "PageSize"))
1190 strlcpy(option
->text
, _("Media Size"), sizeof(option
->text
));
1191 else if (!strcmp(name
, "MediaType"))
1192 strlcpy(option
->text
, _("Media Type"), sizeof(option
->text
));
1193 else if (!strcmp(name
, "InputSlot"))
1194 strlcpy(option
->text
, _("Media Source"), sizeof(option
->text
));
1195 else if (!strcmp(name
, "ColorModel"))
1196 strlcpy(option
->text
, _("Output Mode"), sizeof(option
->text
));
1197 else if (!strcmp(name
, "Resolution"))
1198 strlcpy(option
->text
, _("Resolution"), sizeof(option
->text
));
1200 strlcpy(option
->text
, name
, sizeof(option
->text
));
1203 option
->section
= PPD_ORDER_ANY
;
1208 else if (!strcmp(keyword
, "JCLOpenUI"))
1211 * Don't allow nesting of options...
1214 if (option
&& cg
->ppd_conform
== PPD_CONFORM_STRICT
)
1216 cg
->ppd_status
= PPD_NESTED_OPEN_UI
;
1222 * Find the JCL group, and add if needed...
1225 group
= ppd_get_group(ppd
, "JCL", _("JCL"), cg
);
1231 * Add an option record to the current JCLs...
1235 _cups_strcpy(name
, name
+ 1);
1237 option
= ppd_get_option(group
, name
);
1241 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1247 * Now fill in the initial information for the option...
1250 if (string
&& !strcmp(string
, "PickMany"))
1251 option
->ui
= PPD_UI_PICKMANY
;
1252 else if (string
&& !strcmp(string
, "Boolean"))
1253 option
->ui
= PPD_UI_BOOLEAN
;
1254 else if (string
&& !strcmp(string
, "PickOne"))
1255 option
->ui
= PPD_UI_PICKONE
;
1258 cg
->ppd_status
= PPD_BAD_OPEN_UI
;
1263 for (j
= 0; j
< ppd
->num_attrs
; j
++)
1264 if (!strncmp(ppd
->attrs
[j
]->name
, "Default", 7) &&
1265 !strcmp(ppd
->attrs
[j
]->name
+ 7, name
) &&
1266 ppd
->attrs
[j
]->value
)
1268 DEBUG_printf(("Setting Default%s to %s via attribute...\n",
1269 option
->keyword
, ppd
->attrs
[j
]->value
));
1270 strlcpy(option
->defchoice
, ppd
->attrs
[j
]->value
,
1271 sizeof(option
->defchoice
));
1275 strlcpy(option
->text
, text
, sizeof(option
->text
));
1277 option
->section
= PPD_ORDER_JCL
;
1283 else if (!strcmp(keyword
, "CloseUI") || !strcmp(keyword
, "JCLCloseUI"))
1290 else if (!strcmp(keyword
, "OpenGroup"))
1293 * Open a new group...
1298 cg
->ppd_status
= PPD_NESTED_OPEN_GROUP
;
1305 cg
->ppd_status
= PPD_BAD_OPEN_GROUP
;
1311 * Separate the group name from the text (name/text)...
1314 if ((sptr
= strchr(string
, '/')) != NULL
)
1320 * Fix up the text...
1326 * Find/add the group...
1329 group
= ppd_get_group(ppd
, string
, sptr
, cg
);
1337 else if (!strcmp(keyword
, "CloseGroup"))
1344 else if (!strcmp(keyword
, "OrderDependency") ||
1345 !strcmp(keyword
, "NonUIOrderDependency"))
1347 if (sscanf(string
, "%f%40s%40s", &order
, name
, keyword
) != 3)
1349 cg
->ppd_status
= PPD_BAD_ORDER_DEPENDENCY
;
1354 if (keyword
[0] == '*')
1355 _cups_strcpy(keyword
, keyword
+ 1);
1357 if (!strcmp(name
, "ExitServer"))
1358 section
= PPD_ORDER_EXIT
;
1359 else if (!strcmp(name
, "Prolog"))
1360 section
= PPD_ORDER_PROLOG
;
1361 else if (!strcmp(name
, "DocumentSetup"))
1362 section
= PPD_ORDER_DOCUMENT
;
1363 else if (!strcmp(name
, "PageSetup"))
1364 section
= PPD_ORDER_PAGE
;
1365 else if (!strcmp(name
, "JCLSetup"))
1366 section
= PPD_ORDER_JCL
;
1368 section
= PPD_ORDER_ANY
;
1376 * Only valid for Non-UI options...
1379 for (i
= ppd
->num_groups
, gtemp
= ppd
->groups
; i
> 0; i
--, gtemp
++)
1380 if (gtemp
->text
[0] == '\0')
1384 for (i
= 0; i
< gtemp
->num_options
; i
++)
1385 if (!strcmp(keyword
, gtemp
->options
[i
].keyword
))
1387 gtemp
->options
[i
].section
= section
;
1388 gtemp
->options
[i
].order
= order
;
1394 option
->section
= section
;
1395 option
->order
= order
;
1401 else if (!strncmp(keyword
, "Default", 7))
1407 * Drop UI text, if any, from value...
1410 if (strchr(string
, '/') != NULL
)
1411 *strchr(string
, '/') = '\0';
1414 * Assign the default value as appropriate...
1417 if (!strcmp(keyword
, "DefaultColorSpace"))
1420 * Set default colorspace...
1423 if (!strcmp(string
, "CMY"))
1424 ppd
->colorspace
= PPD_CS_CMY
;
1425 else if (!strcmp(string
, "CMYK"))
1426 ppd
->colorspace
= PPD_CS_CMYK
;
1427 else if (!strcmp(string
, "RGB"))
1428 ppd
->colorspace
= PPD_CS_RGB
;
1429 else if (!strcmp(string
, "RGBK"))
1430 ppd
->colorspace
= PPD_CS_RGBK
;
1431 else if (!strcmp(string
, "N"))
1432 ppd
->colorspace
= PPD_CS_N
;
1434 ppd
->colorspace
= PPD_CS_GRAY
;
1436 else if (option
&& !strcmp(keyword
+ 7, option
->keyword
))
1439 * Set the default as part of the current option...
1442 DEBUG_printf(("Setting %s to %s...\n", keyword
, string
));
1444 strlcpy(option
->defchoice
, string
, sizeof(option
->defchoice
));
1446 DEBUG_printf(("%s is now %s...\n", keyword
, option
->defchoice
));
1451 * Lookup option and set if it has been defined...
1454 ppd_option_t
*toption
; /* Temporary option */
1457 if ((toption
= ppdFindOption(ppd
, keyword
+ 7)) != NULL
)
1459 DEBUG_printf(("Setting %s to %s...\n", keyword
, string
));
1460 strlcpy(toption
->defchoice
, string
, sizeof(toption
->defchoice
));
1464 else if (!strcmp(keyword
, "UIConstraints") ||
1465 !strcmp(keyword
, "NonUIConstraints"))
1467 if (ppd
->num_consts
== 0)
1468 constraint
= calloc(1, sizeof(ppd_const_t
));
1470 constraint
= realloc(ppd
->consts
,
1471 (ppd
->num_consts
+ 1) * sizeof(ppd_const_t
));
1473 if (constraint
== NULL
)
1475 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1480 ppd
->consts
= constraint
;
1481 constraint
+= ppd
->num_consts
;
1484 switch (sscanf(string
, "%40s%40s%40s%40s", constraint
->option1
,
1485 constraint
->choice1
, constraint
->option2
,
1486 constraint
->choice2
))
1488 case 0 : /* Error */
1489 case 1 : /* Error */
1490 cg
->ppd_status
= PPD_BAD_UI_CONSTRAINTS
;
1493 case 2 : /* Two options... */
1495 * The following strcpy's are safe, as optionN and
1496 * choiceN are all the same size (size defined by PPD spec...)
1499 if (constraint
->option1
[0] == '*')
1500 _cups_strcpy(constraint
->option1
, constraint
->option1
+ 1);
1502 if (constraint
->choice1
[0] == '*')
1503 _cups_strcpy(constraint
->option2
, constraint
->choice1
+ 1);
1505 _cups_strcpy(constraint
->option2
, constraint
->choice1
);
1507 constraint
->choice1
[0] = '\0';
1508 constraint
->choice2
[0] = '\0';
1511 case 3 : /* Two options, one choice... */
1513 * The following _cups_strcpy's are safe, as optionN and
1514 * choiceN are all the same size (size defined by PPD spec...)
1517 if (constraint
->option1
[0] == '*')
1518 _cups_strcpy(constraint
->option1
, constraint
->option1
+ 1);
1520 if (constraint
->choice1
[0] == '*')
1522 _cups_strcpy(constraint
->choice2
, constraint
->option2
);
1523 _cups_strcpy(constraint
->option2
, constraint
->choice1
+ 1);
1524 constraint
->choice1
[0] = '\0';
1528 if (constraint
->option2
[0] == '*')
1529 _cups_strcpy(constraint
->option2
, constraint
->option2
+ 1);
1531 constraint
->choice2
[0] = '\0';
1535 case 4 : /* Two options, two choices... */
1536 if (constraint
->option1
[0] == '*')
1537 _cups_strcpy(constraint
->option1
, constraint
->option1
+ 1);
1539 if (constraint
->option2
[0] == '*')
1540 _cups_strcpy(constraint
->option2
, constraint
->option2
+ 1);
1547 else if (!strcmp(keyword
, "PaperDimension"))
1549 if ((size
= ppdPageSize(ppd
, name
)) == NULL
)
1550 size
= ppd_add_size(ppd
, name
);
1555 * Unable to add or find size!
1558 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1563 sscanf(string
, "%f%f", &(size
->width
), &(size
->length
));
1568 else if (!strcmp(keyword
, "ImageableArea"))
1570 if ((size
= ppdPageSize(ppd
, name
)) == NULL
)
1571 size
= ppd_add_size(ppd
, name
);
1576 * Unable to add or find size!
1579 cg
->ppd_status
= PPD_ALLOC_ERROR
;
1584 sscanf(string
, "%f%f%f%f", &(size
->left
), &(size
->bottom
),
1585 &(size
->right
), &(size
->top
));
1590 else if (option
!= NULL
&&
1591 (mask
& (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
)) ==
1592 (PPD_KEYWORD
| PPD_OPTION
| PPD_STRING
) &&
1593 !strcmp(keyword
, option
->keyword
))
1595 DEBUG_printf(("group = %p, subgroup = %p\n", group
, subgroup
));
1597 if (!strcmp(keyword
, "PageSize"))
1600 * Add a page size...
1603 if (ppdPageSize(ppd
, name
) == NULL
)
1604 ppd_add_size(ppd
, name
);
1608 * Add the option choice...
1611 choice
= ppd_add_choice(option
, name
);
1613 if (mask
& PPD_TEXT
)
1614 strlcpy(choice
->text
, text
, sizeof(choice
->text
));
1615 else if (!strcmp(name
, "True"))
1616 strcpy(choice
->text
, _("Yes"));
1617 else if (!strcmp(name
, "False"))
1618 strcpy(choice
->text
, _("No"));
1620 strlcpy(choice
->text
, name
, sizeof(choice
->text
));
1622 if (option
->section
== PPD_ORDER_JCL
)
1623 ppd_decode(string
); /* Decode quoted string */
1625 choice
->code
= string
;
1626 string
= NULL
; /* Don't add as an attribute below */
1630 * Add remaining lines with keywords and string values as attributes...
1634 (mask
& (PPD_KEYWORD
| PPD_STRING
)) == (PPD_KEYWORD
| PPD_STRING
))
1635 ppd_add_attr(ppd
, keyword
, name
, text
, string
);
1641 * Reset language preferences...
1644 cupsLangFree(language
);
1647 _cupsRestoreLocale(LC_NUMERIC
, oldlocale
);
1649 _cupsRestoreLocale(LC_ALL
, oldlocale
);
1650 #endif /* LC_NUMERIC */
1654 printf("Premature EOF at %lu...\n", (unsigned long)ftell(fp
));
1657 if (cg
->ppd_status
!= PPD_OK
)
1660 * Had an error reading the PPD file, cannot continue!
1669 * Create the sorted options array and set the option back-pointer for
1670 * each choice and custom option...
1673 ppd
->options
= cupsArrayNew((cups_array_func_t
)ppd_compare_options
, NULL
);
1675 for (i
= ppd
->num_groups
, group
= ppd
->groups
;
1679 for (j
= group
->num_options
, option
= group
->options
;
1683 ppd_coption_t
*coption
; /* Custom option */
1686 cupsArrayAdd(ppd
->options
, option
);
1688 for (k
= 0; k
< option
->num_choices
; k
++)
1689 option
->choices
[k
].option
= option
;
1691 if ((coption
= ppdFindCustomOption(ppd
, option
->keyword
)) != NULL
)
1692 coption
->option
= option
;
1697 * Return the PPD file structure...
1703 * Common exit point for errors to save code size...
1712 cupsLangFree(language
);
1715 _cupsRestoreLocale(LC_NUMERIC
, oldlocale
);
1717 _cupsRestoreLocale(LC_ALL
, oldlocale
);
1718 #endif /* LC_NUMERIC */
1725 * 'ppdOpenFd()' - Read a PPD file into memory.
1728 ppd_file_t
* /* O - PPD file record */
1729 ppdOpenFd(int fd
) /* I - File to read from */
1731 cups_file_t
*fp
; /* CUPS file pointer */
1732 ppd_file_t
*ppd
; /* PPD file record */
1733 _cups_globals_t
*cg
= _cupsGlobals();
1738 * Set the line number to 0...
1744 * Range check input...
1749 cg
->ppd_status
= PPD_NULL_FILE
;
1755 * Try to open the file and parse it...
1758 if ((fp
= cupsFileOpenFd(fd
, "r")) != NULL
)
1766 cg
->ppd_status
= PPD_FILE_OPEN_ERROR
;
1775 * 'ppdOpenFile()' - Read a PPD file into memory.
1778 ppd_file_t
* /* O - PPD file record */
1779 ppdOpenFile(const char *filename
) /* I - File to read from */
1781 cups_file_t
*fp
; /* File pointer */
1782 ppd_file_t
*ppd
; /* PPD file record */
1783 _cups_globals_t
*cg
= _cupsGlobals();
1788 * Set the line number to 0...
1794 * Range check input...
1797 if (filename
== NULL
)
1799 cg
->ppd_status
= PPD_NULL_FILE
;
1805 * Try to open the file and parse it...
1808 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
1816 cg
->ppd_status
= PPD_FILE_OPEN_ERROR
;
1825 * 'ppdSetConformance()' - Set the conformance level for PPD files.
1827 * @since CUPS 1.1.20@
1831 ppdSetConformance(ppd_conform_t c
) /* I - Conformance level */
1833 _cups_globals_t
*cg
= _cupsGlobals();
1837 cg
->ppd_conform
= c
;
1842 * 'ppd_add_attr()' - Add an attribute to the PPD data.
1845 static ppd_attr_t
* /* O - New attribute */
1846 ppd_add_attr(ppd_file_t
*ppd
, /* I - PPD file data */
1847 const char *name
, /* I - Attribute name */
1848 const char *spec
, /* I - Specifier string, if any */
1849 const char *text
, /* I - Text string, if any */
1850 const char *value
) /* I - Value of attribute */
1852 ppd_attr_t
**ptr
, /* New array */
1853 *temp
; /* New attribute */
1857 * Range check input...
1860 if (ppd
== NULL
|| name
== NULL
|| spec
== NULL
)
1864 * Create the array as needed...
1867 if (!ppd
->sorted_attrs
)
1868 ppd
->sorted_attrs
= cupsArrayNew((cups_array_func_t
)ppd_compare_attrs
,
1872 * Allocate memory for the new attribute...
1875 if (ppd
->num_attrs
== 0)
1876 ptr
= malloc(sizeof(ppd_attr_t
*));
1878 ptr
= realloc(ppd
->attrs
, (ppd
->num_attrs
+ 1) * sizeof(ppd_attr_t
*));
1884 ptr
+= ppd
->num_attrs
;
1886 if ((temp
= calloc(1, sizeof(ppd_attr_t
))) == NULL
)
1897 strlcpy(temp
->name
, name
, sizeof(temp
->name
));
1898 strlcpy(temp
->spec
, spec
, sizeof(temp
->spec
));
1899 strlcpy(temp
->text
, text
, sizeof(temp
->text
));
1900 temp
->value
= (char *)value
;
1903 * Add the attribute to the sorted array...
1906 cupsArrayAdd(ppd
->sorted_attrs
, temp
);
1909 * Return the attribute...
1917 * 'ppd_add_choice()' - Add a choice to an option.
1920 static ppd_choice_t
* /* O - Named choice */
1921 ppd_add_choice(ppd_option_t
*option
, /* I - Option */
1922 const char *name
) /* I - Name of choice */
1924 ppd_choice_t
*choice
; /* Choice */
1927 if (option
->num_choices
== 0)
1928 choice
= malloc(sizeof(ppd_choice_t
));
1930 choice
= realloc(option
->choices
,
1931 sizeof(ppd_choice_t
) * (option
->num_choices
+ 1));
1936 option
->choices
= choice
;
1937 choice
+= option
->num_choices
;
1938 option
->num_choices
++;
1940 memset(choice
, 0, sizeof(ppd_choice_t
));
1941 strlcpy(choice
->choice
, name
, sizeof(choice
->choice
));
1948 * 'ppd_add_size()' - Add a page size.
1951 static ppd_size_t
* /* O - Named size */
1952 ppd_add_size(ppd_file_t
*ppd
, /* I - PPD file */
1953 const char *name
) /* I - Name of size */
1955 ppd_size_t
*size
; /* Size */
1958 if (ppd
->num_sizes
== 0)
1959 size
= malloc(sizeof(ppd_size_t
));
1961 size
= realloc(ppd
->sizes
, sizeof(ppd_size_t
) * (ppd
->num_sizes
+ 1));
1967 size
+= ppd
->num_sizes
;
1970 memset(size
, 0, sizeof(ppd_size_t
));
1971 strlcpy(size
->name
, name
, sizeof(size
->name
));
1978 * 'ppd_compare_attrs()' - Compare two attributes.
1981 static int /* O - Result of comparison */
1982 ppd_compare_attrs(ppd_attr_t
*a
, /* I - First attribute */
1983 ppd_attr_t
*b
) /* I - Second attribute */
1985 int ret
; /* Result of comparison */
1988 if ((ret
= strcasecmp(a
->name
, b
->name
)) != 0)
1990 else if (a
->spec
[0] && b
->spec
[0])
1991 return (strcasecmp(a
->spec
, b
->spec
));
1998 * 'ppd_compare_coptions()' - Compare two custom options.
2001 static int /* O - Result of comparison */
2002 ppd_compare_coptions(ppd_coption_t
*a
, /* I - First option */
2003 ppd_coption_t
*b
) /* I - Second option */
2005 return (strcasecmp(a
->keyword
, b
->keyword
));
2010 * 'ppd_compare_cparams()' - Compare two custom parameters.
2013 static int /* O - Result of comparison */
2014 ppd_compare_cparams(ppd_cparam_t
*a
, /* I - First parameter */
2015 ppd_cparam_t
*b
) /* I - Second parameter */
2017 return (strcasecmp(a
->name
, b
->name
));
2022 * 'ppd_compare_options()' - Compare two options.
2025 static int /* O - Result of comparison */
2026 ppd_compare_options(ppd_option_t
*a
, /* I - First option */
2027 ppd_option_t
*b
) /* I - Second option */
2029 return (strcasecmp(a
->keyword
, b
->keyword
));
2034 * 'ppd_decode()' - Decode a string value...
2037 static int /* O - Length of decoded string */
2038 ppd_decode(char *string
) /* I - String to decode */
2040 char *inptr
, /* Input pointer */
2041 *outptr
; /* Output pointer */
2047 while (*inptr
!= '\0')
2048 if (*inptr
== '<' && isxdigit(inptr
[1] & 255))
2051 * Convert hex to 8-bit values...
2055 while (isxdigit(*inptr
& 255))
2057 if (isalpha(*inptr
))
2058 *outptr
= (tolower(*inptr
) - 'a' + 10) << 4;
2060 *outptr
= (*inptr
- '0') << 4;
2064 if (!isxdigit(*inptr
& 255))
2067 if (isalpha(*inptr
))
2068 *outptr
|= tolower(*inptr
) - 'a' + 10;
2070 *outptr
|= *inptr
- '0';
2076 while (*inptr
!= '>' && *inptr
!= '\0')
2078 while (*inptr
== '>')
2082 *outptr
++ = *inptr
++;
2086 return ((int)(outptr
- string
));
2091 * 'ppd_free_group()' - Free a single UI group.
2095 ppd_free_group(ppd_group_t
*group
) /* I - Group to free */
2097 int i
; /* Looping var */
2098 ppd_option_t
*option
; /* Current option */
2099 ppd_group_t
*subgroup
; /* Current sub-group */
2102 if (group
->num_options
> 0)
2104 for (i
= group
->num_options
, option
= group
->options
;
2107 ppd_free_option(option
);
2109 ppd_free(group
->options
);
2112 if (group
->num_subgroups
> 0)
2114 for (i
= group
->num_subgroups
, subgroup
= group
->subgroups
;
2117 ppd_free_group(subgroup
);
2119 ppd_free(group
->subgroups
);
2125 * 'ppd_free_option()' - Free a single option.
2129 ppd_free_option(ppd_option_t
*option
) /* I - Option to free */
2131 int i
; /* Looping var */
2132 ppd_choice_t
*choice
; /* Current choice */
2135 if (option
->num_choices
> 0)
2137 for (i
= option
->num_choices
, choice
= option
->choices
;
2141 ppd_free(choice
->code
);
2144 ppd_free(option
->choices
);
2150 * 'ppd_get_coption()' - Get a custom option record.
2153 static ppd_coption_t
* /* O - Custom option... */
2154 ppd_get_coption(ppd_file_t
*ppd
, /* I - PPD file */
2155 const char *name
) /* I - Name of option */
2157 ppd_coption_t
*copt
; /* New custom option */
2161 * See if the option already exists...
2164 if ((copt
= ppdFindCustomOption(ppd
, name
)) != NULL
)
2168 * Not found, so create the custom option record...
2171 if ((copt
= calloc(1, sizeof(ppd_coption_t
))) == NULL
)
2174 strlcpy(copt
->keyword
, name
, sizeof(copt
->keyword
));
2176 copt
->params
= cupsArrayNew((cups_array_func_t
)ppd_compare_cparams
, NULL
);
2178 cupsArrayAdd(ppd
->coptions
, copt
);
2181 * Return the new record...
2189 * 'ppd_get_cparam()' - Get a custom parameter record.
2192 static ppd_cparam_t
* /* O - Extended option... */
2193 ppd_get_cparam(ppd_coption_t
*opt
, /* I - PPD file */
2194 const char *param
, /* I - Name of parameter */
2195 const char *text
) /* I - Human-readable text */
2197 ppd_cparam_t
*cparam
; /* New custom parameter */
2201 * See if the parameter already exists...
2204 if ((cparam
= ppdFindCustomParam(opt
, param
)) != NULL
)
2208 * Not found, so create the custom parameter record...
2211 if ((cparam
= calloc(1, sizeof(ppd_cparam_t
))) == NULL
)
2214 strlcpy(cparam
->name
, param
, sizeof(cparam
->name
));
2215 strlcpy(cparam
->text
, text
, sizeof(cparam
->text
));
2218 * Add this record to the array...
2221 cupsArrayAdd(opt
->params
, cparam
);
2224 * Return the new record...
2232 * 'ppd_get_group()' - Find or create the named group as needed.
2235 static ppd_group_t
* /* O - Named group */
2236 ppd_get_group(ppd_file_t
*ppd
, /* I - PPD file */
2237 const char *name
, /* I - Name of group */
2238 const char *text
, /* I - Text for group */
2239 _cups_globals_t
*cg
) /* I - Global data */
2241 int i
; /* Looping var */
2242 ppd_group_t
*group
; /* Group */
2245 DEBUG_printf(("ppd_get_group(ppd=%p, name=\"%s\", text=\"%s\", cg=%p)\n",
2246 ppd
, name
, text
, cg
));
2248 for (i
= ppd
->num_groups
, group
= ppd
->groups
; i
> 0; i
--, group
++)
2249 if (!strcmp(group
->name
, name
))
2254 DEBUG_printf(("Adding group %s...\n", name
));
2256 if (cg
->ppd_conform
== PPD_CONFORM_STRICT
&& strlen(text
) >= sizeof(group
->text
))
2258 cg
->ppd_status
= PPD_ILLEGAL_TRANSLATION
;
2263 if (ppd
->num_groups
== 0)
2264 group
= malloc(sizeof(ppd_group_t
));
2266 group
= realloc(ppd
->groups
,
2267 (ppd
->num_groups
+ 1) * sizeof(ppd_group_t
));
2271 cg
->ppd_status
= PPD_ALLOC_ERROR
;
2276 ppd
->groups
= group
;
2277 group
+= ppd
->num_groups
;
2280 memset(group
, 0, sizeof(ppd_group_t
));
2281 strlcpy(group
->name
, name
, sizeof(group
->name
));
2282 strlcpy(group
->text
, text
, sizeof(group
->text
));
2290 * 'ppd_get_option()' - Find or create the named option as needed.
2293 static ppd_option_t
* /* O - Named option */
2294 ppd_get_option(ppd_group_t
*group
, /* I - Group */
2295 const char *name
) /* I - Name of option */
2297 int i
; /* Looping var */
2298 ppd_option_t
*option
; /* Option */
2301 DEBUG_printf(("ppd_get_option(group=%p(\"%s\"), name=\"%s\")\n",
2302 group
, group
->name
, name
));
2304 for (i
= group
->num_options
, option
= group
->options
; i
> 0; i
--, option
++)
2305 if (!strcmp(option
->keyword
, name
))
2310 if (group
->num_options
== 0)
2311 option
= malloc(sizeof(ppd_option_t
));
2313 option
= realloc(group
->options
,
2314 (group
->num_options
+ 1) * sizeof(ppd_option_t
));
2319 group
->options
= option
;
2320 option
+= group
->num_options
;
2321 group
->num_options
++;
2323 memset(option
, 0, sizeof(ppd_option_t
));
2324 strlcpy(option
->keyword
, name
, sizeof(option
->keyword
));
2332 * 'ppd_read()' - Read a line from a PPD file, skipping comment lines as
2336 static int /* O - Bitmask of fields read */
2337 ppd_read(cups_file_t
*fp
, /* I - File to read from */
2338 char *keyword
, /* O - Keyword from line */
2339 char *option
, /* O - Option from line */
2340 char *text
, /* O - Human-readable text from line */
2341 char **string
, /* O - Code/string data */
2342 int ignoreblank
, /* I - Ignore blank lines? */
2343 _cups_globals_t
*cg
) /* I - Global data */
2345 int ch
, /* Character from file */
2346 col
, /* Column in line */
2347 colon
, /* Colon seen? */
2348 endquote
, /* Waiting for an end quote */
2349 mask
, /* Mask to be returned */
2350 startline
, /* Start line */
2351 textlen
; /* Length of text */
2352 char *keyptr
, /* Keyword pointer */
2353 *optptr
, /* Option pointer */
2354 *textptr
, /* Text pointer */
2355 *strptr
, /* Pointer into string */
2356 *lineptr
, /* Current position in line buffer */
2357 *line
; /* Line buffer */
2358 int linesize
; /* Current size of line buffer */
2361 * Range check everything...
2364 if (!fp
|| !keyword
|| !option
|| !text
|| !string
)
2368 * Now loop until we have a valid line...
2373 startline
= cg
->ppd_line
+ 1;
2375 line
= malloc(linesize
);
2390 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
2392 if (lineptr
>= (line
+ linesize
- 1))
2395 * Expand the line buffer...
2398 char *temp
; /* Temporary line pointer */
2402 if (linesize
> 262144)
2405 * Don't allow lines longer than 256k!
2408 cg
->ppd_line
= startline
;
2409 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2416 temp
= realloc(line
, linesize
);
2419 cg
->ppd_line
= startline
;
2420 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2427 lineptr
= temp
+ (lineptr
- line
);
2431 if (ch
== '\r' || ch
== '\n')
2434 * Line feed or carriage return...
2443 * Check for a trailing line feed...
2446 if ((ch
= cupsFilePeekChar(fp
)) == EOF
)
2453 cupsFileGetChar(fp
);
2456 if (lineptr
== line
&& ignoreblank
)
2457 continue; /* Skip blank lines */
2461 if (!endquote
) /* Continue for multi-line text */
2466 else if (ch
< ' ' && ch
!= '\t' && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2469 * Other control characters...
2472 cg
->ppd_line
= startline
;
2473 cg
->ppd_status
= PPD_ILLEGAL_CHARACTER
;
2479 else if (ch
!= 0x1a)
2482 * Any other character...
2488 if (col
> (PPD_MAX_LINE
- 1))
2491 * Line is too long...
2494 cg
->ppd_line
= startline
;
2495 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2502 if (ch
== ':' && strncmp(line
, "*%", 2) != 0)
2505 if (ch
== '\"' && colon
)
2506 endquote
= !endquote
;
2513 * Didn't finish this quoted string...
2516 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
2519 else if (ch
== '\r' || ch
== '\n')
2527 * Check for a trailing line feed...
2530 if ((ch
= cupsFilePeekChar(fp
)) == EOF
)
2533 cupsFileGetChar(fp
);
2538 else if (ch
< ' ' && ch
!= '\t' && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2541 * Other control characters...
2544 cg
->ppd_line
= startline
;
2545 cg
->ppd_status
= PPD_ILLEGAL_CHARACTER
;
2551 else if (ch
!= 0x1a)
2555 if (col
> (PPD_MAX_LINE
- 1))
2558 * Line is too long...
2561 cg
->ppd_line
= startline
;
2562 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2574 * Didn't finish this line...
2577 while ((ch
= cupsFileGetChar(fp
)) != EOF
)
2578 if (ch
== '\r' || ch
== '\n')
2581 * Line feed or carriage return...
2590 * Check for a trailing line feed...
2593 if ((ch
= cupsFilePeekChar(fp
)) == EOF
)
2596 cupsFileGetChar(fp
);
2601 else if (ch
< ' ' && ch
!= '\t' && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2604 * Other control characters...
2607 cg
->ppd_line
= startline
;
2608 cg
->ppd_status
= PPD_ILLEGAL_CHARACTER
;
2614 else if (ch
!= 0x1a)
2618 if (col
> (PPD_MAX_LINE
- 1))
2621 * Line is too long...
2624 cg
->ppd_line
= startline
;
2625 cg
->ppd_status
= PPD_LINE_TOO_LONG
;
2634 if (lineptr
> line
&& lineptr
[-1] == '\n')
2639 DEBUG_printf(("LINE = \"%s\"\n", line
));
2642 * The dynamically created PPDs for older style Mac OS X
2643 * drivers include a large blob of data inserted as comments
2644 * at the end of the file. As an optimization we can stop
2645 * reading the PPD when we get to the start of this data.
2648 if (!strcmp(line
, "*%APLWORKSET START"))
2654 if (ch
== EOF
&& lineptr
== line
)
2672 if ((!line
[0] || /* Blank line */
2673 !strncmp(line
, "*%", 2) || /* Comment line */
2674 !strcmp(line
, "*End")) && /* End of multi-line string */
2675 ignoreblank
) /* Ignore these? */
2677 startline
= cg
->ppd_line
+ 1;
2681 if (!strcmp(line
, "*")) /* (Bad) comment line */
2683 if (cg
->ppd_conform
== PPD_CONFORM_RELAXED
)
2685 startline
= cg
->ppd_line
+ 1;
2690 cg
->ppd_line
= startline
;
2691 cg
->ppd_status
= PPD_ILLEGAL_MAIN_KEYWORD
;
2698 if (line
[0] != '*') /* All lines start with an asterisk */
2701 * Allow lines consisting of just whitespace...
2704 for (lineptr
= line
; *lineptr
; lineptr
++)
2705 if (!isspace(*lineptr
& 255))
2710 cg
->ppd_status
= PPD_MISSING_ASTERISK
;
2714 else if (ignoreblank
)
2729 while (*lineptr
!= '\0' && *lineptr
!= ':' && !isspace(*lineptr
& 255))
2731 if (*lineptr
<= ' ' || *lineptr
> 126 || *lineptr
== '/' ||
2732 (keyptr
- keyword
) >= (PPD_MAX_NAME
- 1))
2734 cg
->ppd_status
= PPD_ILLEGAL_MAIN_KEYWORD
;
2739 *keyptr
++ = *lineptr
++;
2744 if (!strcmp(keyword
, "End"))
2747 mask
|= PPD_KEYWORD
;
2749 /* DEBUG_printf(("keyword = \"%s\", lineptr = \"%s\"\n", keyword, lineptr));*/
2751 if (isspace(*lineptr
& 255))
2754 * Get an option name...
2757 while (isspace(*lineptr
& 255))
2762 while (*lineptr
!= '\0' && !isspace(*lineptr
& 255) && *lineptr
!= ':' &&
2765 if (*lineptr
<= ' ' || *lineptr
> 126 ||
2766 (optptr
- option
) >= (PPD_MAX_NAME
- 1))
2768 cg
->ppd_status
= PPD_ILLEGAL_OPTION_KEYWORD
;
2773 *optptr
++ = *lineptr
++;
2778 if (isspace(*lineptr
& 255) && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2780 cg
->ppd_status
= PPD_ILLEGAL_WHITESPACE
;
2785 while (isspace(*lineptr
& 255))
2790 /* DEBUG_printf(("option = \"%s\", lineptr = \"%s\"\n", option, lineptr));*/
2792 if (*lineptr
== '/')
2795 * Get human-readable text...
2802 while (*lineptr
!= '\0' && *lineptr
!= '\n' && *lineptr
!= ':')
2804 if (((unsigned char)*lineptr
< ' ' && *lineptr
!= '\t') ||
2805 (textptr
- text
) >= (PPD_MAX_LINE
- 1))
2807 cg
->ppd_status
= PPD_ILLEGAL_TRANSLATION
;
2812 *textptr
++ = *lineptr
++;
2816 textlen
= ppd_decode(text
);
2818 if (textlen
> PPD_MAX_TEXT
&& cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2820 cg
->ppd_status
= PPD_ILLEGAL_TRANSLATION
;
2828 /* DEBUG_printf(("text = \"%s\", lineptr = \"%s\"\n", text, lineptr));*/
2831 if (isspace(*lineptr
& 255) && cg
->ppd_conform
== PPD_CONFORM_STRICT
)
2833 cg
->ppd_status
= PPD_ILLEGAL_WHITESPACE
;
2838 while (isspace(*lineptr
& 255))
2841 if (*lineptr
== ':')
2844 * Get string after triming leading and trailing whitespace...
2848 while (isspace(*lineptr
& 255))
2851 strptr
= lineptr
+ strlen(lineptr
) - 1;
2852 while (strptr
>= lineptr
&& isspace(*strptr
& 255))
2855 if (*strptr
== '\"')
2858 * Quoted string by itself...
2861 *string
= malloc(strlen(lineptr
) + 1);
2865 for (; *lineptr
!= '\0'; lineptr
++)
2866 if (*lineptr
!= '\"')
2867 *strptr
++ = *lineptr
;
2872 *string
= strdup(lineptr
);
2874 /* DEBUG_printf(("string = \"%s\", lineptr = \"%s\"\n", *string, lineptr));*/
2888 * End of "$Id: ppd.c 5200 2006-02-28 00:10:32Z mike $".