]>
git.ipfire.org Git - thirdparty/cups.git/blob - ppdc/ppdc-import.cxx
4 // PPD file import methods for the CUPS PPD Compiler.
6 // Copyright 2007-2008 by Apple Inc.
7 // Copyright 2002-2006 by Easy Software Products.
9 // These coded instructions, statements, and computer programs are the
10 // property of Apple Inc. and are protected by Federal copyright
11 // law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 // which should have been included with this file. If this file is
13 // file is missing or damaged, see the license at "http://www.cups.org/".
17 // ppdcSource::import_ppd() - Import a PPD file.
18 // ppd_gets() - Get a line from a PPD file.
22 // Include necessary headers...
25 #include "ppdc-private.h"
27 #include <cups/i18n.h>
31 // 'ppdcSource::import_ppd()' - Import a PPD file.
34 int // O - 1 on success, 0 on failure
35 ppdcSource::import_ppd(const char *f
) // I - Filename
37 int i
, j
, k
; // Looping vars
38 cups_file_t
*fp
; // File
39 char line
[256], // Comment line
40 *ptr
; // Pointer into line
41 ppd_file_t
*ppd
; // PPD file data
42 ppd_group_t
*group
; // PPD group
43 ppd_option_t
*option
; // PPD option
44 ppd_choice_t
*choice
; // PPD choice
45 ppd_attr_t
*attr
; // PPD attribute
46 ppd_const_t
*constraint
; // PPD UI constraint
47 ppd_const_t
*constraint2
; // Temp PPD UI constraint
48 ppd_size_t
*size
; // PPD page size
49 ppdcDriver
*driver
; // Driver
50 ppdcFont
*font
; // Font
51 ppdcGroup
*cgroup
; // UI group
52 ppdcOption
*coption
; // UI option
53 ppdcChoice
*cchoice
; // UI choice
54 ppdcConstraint
*cconstraint
; // UI constraint
55 ppdcMediaSize
*csize
; // Media size
58 // Try opening the PPD file...
59 if ((ppd
= ppdOpenFile(f
)) == NULL
)
62 // All PPD files need a PCFileName attribute...
69 // See if the driver has already been imported...
70 if ((driver
= find_driver(ppd
->pcfilename
)) == NULL
)
72 // Create a new PPD file...
73 if ((fp
= cupsFileOpen(f
, "r")) == NULL
)
79 driver
= new ppdcDriver();
80 driver
->type
= PPDC_DRIVER_PS
;
84 // Read the initial comments from the PPD file and use them as the
85 // copyright/license text...
86 cupsFileGets(fp
, line
, sizeof(line
));
87 // Skip *PPD-Adobe-M.m
89 while (cupsFileGets(fp
, line
, sizeof(line
)))
90 if (strncmp(line
, "*%", 2))
94 for (ptr
= line
+ 2; isspace(*ptr
); ptr
++);
96 driver
->add_copyright(ptr
);
101 // Then add the stuff from the PPD file...
102 if (ppd
->modelname
&& ppd
->manufacturer
&&
103 !strncasecmp(ppd
->modelname
, ppd
->manufacturer
,
104 strlen(ppd
->manufacturer
)))
106 ptr
= ppd
->modelname
+ strlen(ppd
->manufacturer
);
108 while (isspace(*ptr
))
112 ptr
= ppd
->modelname
;
114 driver
->manufacturer
= new ppdcString(ppd
->manufacturer
);
115 driver
->model_name
= new ppdcString(ptr
);
116 driver
->pc_file_name
= new ppdcString(ppd
->pcfilename
);
117 attr
= ppdFindAttr(ppd
, "FileVersion", NULL
);
118 driver
->version
= new ppdcString(attr
? attr
->value
: NULL
);
119 driver
->model_number
= ppd
->model_number
;
120 driver
->manual_copies
= ppd
->manual_copies
;
121 driver
->color_device
= ppd
->color_device
;
122 driver
->throughput
= ppd
->throughput
;
124 attr
= ppdFindAttr(ppd
, "DefaultFont", NULL
);
125 driver
->default_font
= new ppdcString(attr
? attr
->value
: NULL
);
127 // Collect media sizes...
128 ppd_option_t
*region_option
, // PageRegion option
129 *size_option
; // PageSize option
130 ppd_choice_t
*region_choice
, // PageRegion choice
131 *size_choice
; // PageSize choice
133 region_option
= ppdFindOption(ppd
, "PageRegion");
134 size_option
= ppdFindOption(ppd
, "PageSize");
136 for (i
= ppd
->num_sizes
, size
= ppd
->sizes
; i
> 0; i
--, size
++)
138 // Don't do custom size here...
139 if (!strcasecmp(size
->name
, "Custom"))
142 // Get the code for the PageSize and PageRegion options...
143 region_choice
= ppdFindChoice(region_option
, size
->name
);
144 size_choice
= ppdFindChoice(size_option
, size
->name
);
146 // Create a new media size record and add it to the driver...
147 csize
= new ppdcMediaSize(size
->name
, size_choice
->text
, size
->width
,
148 size
->length
, size
->left
, size
->bottom
,
149 size
->width
- size
->right
,
150 size
->length
- size
->top
,
151 size_choice
->code
, region_choice
->code
);
153 driver
->add_size(csize
);
155 if (!strcasecmp(size_option
->defchoice
, size
->name
))
156 driver
->set_default_size(csize
);
159 // Now all of the options...
160 for (i
= ppd
->num_groups
, group
= ppd
->groups
; i
> 0; i
--, group
++)
162 cgroup
= new ppdcGroup(group
->name
, group
->text
);
163 driver
->add_group(cgroup
);
165 for (j
= group
->num_options
, option
= group
->options
; j
> 0; j
--, option
++)
167 if (!strcmp(option
->keyword
, "PageSize") || !strcmp(option
->keyword
, "PageRegion"))
170 coption
= new ppdcOption((ppdcOptType
)option
->ui
, option
->keyword
,
171 option
->text
, (ppdcOptSection
)option
->section
,
173 cgroup
->add_option(coption
);
175 for (k
= option
->num_choices
, choice
= option
->choices
; k
> 0; k
--, choice
++)
177 cchoice
= new ppdcChoice(choice
->choice
, choice
->text
, choice
->code
);
178 coption
->add_choice(cchoice
);
180 if (!strcasecmp(option
->defchoice
, choice
->choice
))
181 coption
->set_defchoice(cchoice
);
186 // Now the constraints...
187 for (i
= ppd
->num_consts
, constraint
= ppd
->consts
;
191 for (j
= i
- 1, constraint2
= constraint
;
193 j
--, constraint2
++)
194 if (constraint
!= constraint2
&&
195 !strcmp(constraint
->option1
, constraint2
->option2
) &&
196 (constraint
->choice1
== constraint2
->choice2
||
197 (constraint
->choice1
&& constraint2
->choice2
&&
198 !strcmp(constraint
->choice1
, constraint2
->choice2
))) &&
199 !strcmp(constraint
->option2
, constraint2
->option1
) &&
200 (constraint
->choice2
== constraint2
->choice1
||
201 (constraint
->choice2
&& constraint2
->choice1
&&
202 !strcmp(constraint
->choice2
, constraint2
->choice1
))))
208 cconstraint
= new ppdcConstraint(constraint
->option1
, constraint
->choice1
,
209 constraint
->option2
, constraint
->choice2
);
210 driver
->add_constraint(cconstraint
);
213 for (i
= 0; i
< ppd
->num_attrs
; i
++)
215 attr
= ppd
->attrs
[i
];
217 if (!strcmp(attr
->name
, "Font"))
220 char encoding
[256], // Encoding string
221 version
[256], // Version string
222 charset
[256], // Charset string
223 status
[256]; // Status string
224 ppdcFontStatus fstatus
; // Status enumeration
227 if (sscanf(attr
->value
, "%s%*[^\"]\"%[^\"]\"%s%s", encoding
, version
,
228 charset
, status
) != 4)
230 _cupsLangPrintf(stderr
, _("Bad font attribute: %s\n"), attr
->value
);
234 if (!strcmp(status
, "ROM"))
235 fstatus
= PPDC_FONT_ROM
;
237 fstatus
= PPDC_FONT_DISK
;
239 font
= new ppdcFont(attr
->spec
, encoding
, version
, charset
, fstatus
);
241 driver
->add_font(font
);
243 else if ((strncmp(attr
->name
, "Default", 7) ||
244 !strcmp(attr
->name
, "DefaultColorSpace")) &&
245 strcmp(attr
->name
, "ColorDevice") &&
246 strcmp(attr
->name
, "Manufacturer") &&
247 strcmp(attr
->name
, "ModelName") &&
248 strcmp(attr
->name
, "MaxMediaHeight") &&
249 strcmp(attr
->name
, "MaxMediaWidth") &&
250 strcmp(attr
->name
, "NickName") &&
251 strcmp(attr
->name
, "ShortNickName") &&
252 strcmp(attr
->name
, "Throughput") &&
253 strcmp(attr
->name
, "PCFileName") &&
254 strcmp(attr
->name
, "FileVersion") &&
255 strcmp(attr
->name
, "FormatVersion") &&
256 strcmp(attr
->name
, "VariablePaperSize") &&
257 strcmp(attr
->name
, "LanguageEncoding") &&
258 strcmp(attr
->name
, "LanguageVersion"))
261 driver
->add_attr(new ppdcAttr(attr
->name
, attr
->spec
, attr
->text
,
264 else if (!strncmp(attr
->name
, "Default", 7) &&
265 !ppdFindOption(ppd
, attr
->name
+ 7) &&
266 strcmp(attr
->name
, "DefaultFont") &&
267 strcmp(attr
->name
, "DefaultImageableArea") &&
268 strcmp(attr
->name
, "DefaultPaperDimension") &&
269 strcmp(attr
->name
, "DefaultFont"))
271 // Default attribute...
272 driver
->add_attr(new ppdcAttr(attr
->name
, attr
->spec
, attr
->text
,