]> git.ipfire.org Git - thirdparty/cups.git/blob - ppdc/ppdc-import.cxx
Merge changes from CUPS 1.4svn-r8492.
[thirdparty/cups.git] / ppdc / ppdc-import.cxx
1 //
2 // "$Id$"
3 //
4 // PPD file import methods for the CUPS PPD Compiler.
5 //
6 // Copyright 2007-2008 by Apple Inc.
7 // Copyright 2002-2006 by Easy Software Products.
8 //
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/".
14 //
15 // Contents:
16 //
17 // ppdcSource::import_ppd() - Import a PPD file.
18 // ppd_gets() - Get a line from a PPD file.
19 //
20
21 //
22 // Include necessary headers...
23 //
24
25 #include "ppdc-private.h"
26 #include <cups/ppd.h>
27 #include <cups/i18n.h>
28
29
30 //
31 // 'ppdcSource::import_ppd()' - Import a PPD file.
32 //
33
34 int // O - 1 on success, 0 on failure
35 ppdcSource::import_ppd(const char *f) // I - Filename
36 {
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
56
57
58 // Try opening the PPD file...
59 if ((ppd = ppdOpenFile(f)) == NULL)
60 return (0);
61
62 // All PPD files need a PCFileName attribute...
63 if (!ppd->pcfilename)
64 {
65 ppdClose(ppd);
66 return (0);
67 }
68
69 // See if the driver has already been imported...
70 if ((driver = find_driver(ppd->pcfilename)) == NULL)
71 {
72 // Create a new PPD file...
73 if ((fp = cupsFileOpen(f, "r")) == NULL)
74 {
75 ppdClose(ppd);
76 return (0);
77 }
78
79 driver = new ppdcDriver();
80 driver->type = PPDC_DRIVER_PS;
81
82 drivers->add(driver);
83
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
88
89 while (cupsFileGets(fp, line, sizeof(line)))
90 if (strncmp(line, "*%", 2))
91 break;
92 else
93 {
94 for (ptr = line + 2; isspace(*ptr); ptr ++);
95
96 driver->add_copyright(ptr);
97 }
98
99 cupsFileClose(fp);
100
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)))
105 {
106 ptr = ppd->modelname + strlen(ppd->manufacturer);
107
108 while (isspace(*ptr))
109 ptr ++;
110 }
111 else
112 ptr = ppd->modelname;
113
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;
123
124 attr = ppdFindAttr(ppd, "DefaultFont", NULL);
125 driver->default_font = new ppdcString(attr ? attr->value : NULL);
126
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
132
133 region_option = ppdFindOption(ppd, "PageRegion");
134 size_option = ppdFindOption(ppd, "PageSize");
135
136 for (i = ppd->num_sizes, size = ppd->sizes; i > 0; i --, size ++)
137 {
138 // Don't do custom size here...
139 if (!strcasecmp(size->name, "Custom"))
140 continue;
141
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);
145
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);
152
153 driver->add_size(csize);
154
155 if (!strcasecmp(size_option->defchoice, size->name))
156 driver->set_default_size(csize);
157 }
158
159 // Now all of the options...
160 for (i = ppd->num_groups, group = ppd->groups; i > 0; i --, group ++)
161 {
162 cgroup = new ppdcGroup(group->name, group->text);
163 driver->add_group(cgroup);
164
165 for (j = group->num_options, option = group->options; j > 0; j --, option ++)
166 {
167 if (!strcmp(option->keyword, "PageSize") || !strcmp(option->keyword, "PageRegion"))
168 continue;
169
170 coption = new ppdcOption((ppdcOptType)option->ui, option->keyword,
171 option->text, (ppdcOptSection)option->section,
172 option->order);
173 cgroup->add_option(coption);
174
175 for (k = option->num_choices, choice = option->choices; k > 0; k --, choice ++)
176 {
177 cchoice = new ppdcChoice(choice->choice, choice->text, choice->code);
178 coption->add_choice(cchoice);
179
180 if (!strcasecmp(option->defchoice, choice->choice))
181 coption->set_defchoice(cchoice);
182 }
183 }
184 }
185
186 // Now the constraints...
187 for (i = ppd->num_consts, constraint = ppd->consts;
188 i > 0;
189 i --, constraint ++)
190 {
191 for (j = i - 1, constraint2 = constraint;
192 j > 0;
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))))
203 break;
204
205 if (j)
206 continue;
207
208 cconstraint = new ppdcConstraint(constraint->option1, constraint->choice1,
209 constraint->option2, constraint->choice2);
210 driver->add_constraint(cconstraint);
211 }
212
213 for (i = 0; i < ppd->num_attrs; i ++)
214 {
215 attr = ppd->attrs[i];
216
217 if (!strcmp(attr->name, "Font"))
218 {
219 // 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
225
226
227 if (sscanf(attr->value, "%s%*[^\"]\"%[^\"]\"%s%s", encoding, version,
228 charset, status) != 4)
229 {
230 _cupsLangPrintf(stderr, _("Bad font attribute: %s\n"), attr->value);
231 continue;
232 }
233
234 if (!strcmp(status, "ROM"))
235 fstatus = PPDC_FONT_ROM;
236 else
237 fstatus = PPDC_FONT_DISK;
238
239 font = new ppdcFont(attr->spec, encoding, version, charset, fstatus);
240
241 driver->add_font(font);
242 }
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"))
259 {
260 // Attribute...
261 driver->add_attr(new ppdcAttr(attr->name, attr->spec, attr->text,
262 attr->value));
263 }
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"))
270 {
271 // Default attribute...
272 driver->add_attr(new ppdcAttr(attr->name, attr->spec, attr->text,
273 attr->value));
274 }
275 }
276 }
277
278 return (1);
279 }
280
281
282 //
283 // End of "$Id$".
284 //