]> git.ipfire.org Git - thirdparty/cups.git/blob - ppdc/ppdc-import.cxx
Merge changes from CUPS 1.4svn-r8606.
[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 int cost; // Cost for filter
42 ppd_file_t *ppd; // PPD file data
43 ppd_group_t *group; // PPD group
44 ppd_option_t *option; // PPD option
45 ppd_choice_t *choice; // PPD choice
46 ppd_attr_t *attr; // PPD attribute
47 ppd_const_t *constraint; // PPD UI constraint
48 ppd_const_t *constraint2; // Temp PPD UI constraint
49 ppd_size_t *size; // PPD page size
50 ppdcDriver *driver; // Driver
51 ppdcFilter *filter; // Current filter
52 ppdcFont *font; // Font
53 ppdcGroup *cgroup; // UI group
54 ppdcOption *coption; // UI option
55 ppdcChoice *cchoice; // UI choice
56 ppdcConstraint *cconstraint; // UI constraint
57 ppdcMediaSize *csize; // Media size
58
59
60 // Try opening the PPD file...
61 if ((ppd = ppdOpenFile(f)) == NULL)
62 return (0);
63
64 // All PPD files need a PCFileName attribute...
65 if (!ppd->pcfilename)
66 {
67 ppdClose(ppd);
68 return (0);
69 }
70
71 // See if the driver has already been imported...
72 if ((driver = find_driver(ppd->pcfilename)) == NULL)
73 {
74 // Create a new PPD file...
75 if ((fp = cupsFileOpen(f, "r")) == NULL)
76 {
77 ppdClose(ppd);
78 return (0);
79 }
80
81 driver = new ppdcDriver();
82 driver->type = PPDC_DRIVER_PS;
83
84 drivers->add(driver);
85
86 // Read the initial comments from the PPD file and use them as the
87 // copyright/license text...
88 cupsFileGets(fp, line, sizeof(line));
89 // Skip *PPD-Adobe-M.m
90
91 while (cupsFileGets(fp, line, sizeof(line)))
92 if (strncmp(line, "*%", 2))
93 break;
94 else if (strncmp(line, "*%%%% ", 6))
95 {
96 for (ptr = line + 2; isspace(*ptr); ptr ++);
97
98 driver->add_copyright(ptr);
99 }
100
101 cupsFileClose(fp);
102
103 // Then add the stuff from the PPD file...
104 if (ppd->modelname && ppd->manufacturer &&
105 !strncasecmp(ppd->modelname, ppd->manufacturer,
106 strlen(ppd->manufacturer)))
107 {
108 ptr = ppd->modelname + strlen(ppd->manufacturer);
109
110 while (isspace(*ptr))
111 ptr ++;
112 }
113 else
114 ptr = ppd->modelname;
115
116 if (ppd->nickname)
117 driver->add_attr(new ppdcAttr("NickName", NULL, NULL, ppd->nickname));
118
119 if (ppd->shortnickname)
120 driver->add_attr(new ppdcAttr("ShortNickName", NULL, NULL,
121 ppd->shortnickname));
122
123 driver->manufacturer = new ppdcString(ppd->manufacturer);
124 driver->model_name = new ppdcString(ptr);
125 driver->pc_file_name = new ppdcString(ppd->pcfilename);
126 attr = ppdFindAttr(ppd, "FileVersion", NULL);
127 driver->version = new ppdcString(attr ? attr->value : NULL);
128 driver->model_number = ppd->model_number;
129 driver->manual_copies = ppd->manual_copies;
130 driver->color_device = ppd->color_device;
131 driver->throughput = ppd->throughput;
132 driver->variable_paper_size = ppd->variable_sizes;
133 driver->max_width = ppd->custom_max[0];
134 driver->max_length = ppd->custom_max[1];
135 driver->min_width = ppd->custom_min[0];
136 driver->min_length = ppd->custom_min[1];
137 driver->left_margin = ppd->custom_margins[0];
138 driver->bottom_margin = ppd->custom_margins[1];
139 driver->right_margin = ppd->custom_margins[2];
140 driver->top_margin = ppd->custom_margins[3];
141
142 for (i = 0; i < ppd->num_filters; i ++)
143 {
144 strlcpy(line, ppd->filters[i], sizeof(line));
145
146 for (ptr = line; *ptr; ptr ++)
147 if (isspace(*ptr & 255))
148 break;
149 *ptr++ = '\0';
150
151 cost = strtol(ptr, &ptr, 10);
152
153 while (isspace(*ptr & 255))
154 ptr ++;
155
156 filter = new ppdcFilter(line, ptr, cost);
157 driver->add_filter(filter);
158 }
159
160 attr = ppdFindAttr(ppd, "DefaultFont", NULL);
161 driver->default_font = new ppdcString(attr ? attr->value : NULL);
162
163 // Collect media sizes...
164 ppd_option_t *region_option, // PageRegion option
165 *size_option; // PageSize option
166 ppd_choice_t *region_choice, // PageRegion choice
167 *size_choice; // PageSize choice
168
169 region_option = ppdFindOption(ppd, "PageRegion");
170 size_option = ppdFindOption(ppd, "PageSize");
171
172 for (i = ppd->num_sizes, size = ppd->sizes; i > 0; i --, size ++)
173 {
174 // Don't do custom size here...
175 if (!strcasecmp(size->name, "Custom"))
176 continue;
177
178 // Get the code for the PageSize and PageRegion options...
179 region_choice = ppdFindChoice(region_option, size->name);
180 size_choice = ppdFindChoice(size_option, size->name);
181
182 // Create a new media size record and add it to the driver...
183 csize = new ppdcMediaSize(size->name, size_choice->text, size->width,
184 size->length, size->left, size->bottom,
185 size->width - size->right,
186 size->length - size->top,
187 size_choice->code, region_choice->code);
188
189 driver->add_size(csize);
190
191 if (!strcasecmp(size_option->defchoice, size->name))
192 driver->set_default_size(csize);
193 }
194
195 // Now all of the options...
196 for (i = ppd->num_groups, group = ppd->groups; i > 0; i --, group ++)
197 {
198 cgroup = new ppdcGroup(group->name, group->text);
199 driver->add_group(cgroup);
200
201 for (j = group->num_options, option = group->options; j > 0; j --, option ++)
202 {
203 if (!strcmp(option->keyword, "PageSize") || !strcmp(option->keyword, "PageRegion"))
204 continue;
205
206 coption = new ppdcOption((ppdcOptType)option->ui, option->keyword,
207 option->text, (ppdcOptSection)option->section,
208 option->order);
209 cgroup->add_option(coption);
210
211 for (k = option->num_choices, choice = option->choices; k > 0; k --, choice ++)
212 {
213 if (!strcmp(choice->choice, "Custom"))
214 continue;
215
216 cchoice = new ppdcChoice(choice->choice, choice->text, choice->code);
217 coption->add_choice(cchoice);
218
219 if (!strcasecmp(option->defchoice, choice->choice))
220 coption->set_defchoice(cchoice);
221 }
222 }
223 }
224
225 // Now the constraints...
226 for (i = ppd->num_consts, constraint = ppd->consts;
227 i > 0;
228 i --, constraint ++)
229 {
230 // Look for mirrored constraints...
231 for (j = i - 1, constraint2 = constraint + 1;
232 j > 0;
233 j --, constraint2 ++)
234 if (!strcmp(constraint->option1, constraint2->option2) &&
235 !strcmp(constraint->choice1, constraint2->choice2) &&
236 !strcmp(constraint->option2, constraint2->option1) &&
237 !strcmp(constraint->choice2, constraint2->choice1))
238 break;
239
240 if (j)
241 continue;
242
243 cconstraint = new ppdcConstraint(constraint->option2, constraint->choice2,
244 constraint->option1, constraint->choice1);
245 driver->add_constraint(cconstraint);
246 }
247
248 for (i = 0; i < ppd->num_attrs; i ++)
249 {
250 attr = ppd->attrs[i];
251
252 if (!strcmp(attr->name, "Font"))
253 {
254 // Font...
255 char encoding[256], // Encoding string
256 version[256], // Version string
257 charset[256], // Charset string
258 status[256]; // Status string
259 ppdcFontStatus fstatus; // Status enumeration
260
261
262 if (sscanf(attr->value, "%s%*[^\"]\"%[^\"]\"%s%s", encoding, version,
263 charset, status) != 4)
264 {
265 _cupsLangPrintf(stderr, _("Bad font attribute: %s\n"), attr->value);
266 continue;
267 }
268
269 if (!strcmp(status, "ROM"))
270 fstatus = PPDC_FONT_ROM;
271 else
272 fstatus = PPDC_FONT_DISK;
273
274 font = new ppdcFont(attr->spec, encoding, version, charset, fstatus);
275
276 driver->add_font(font);
277 }
278 else if (!strcmp(attr->name, "CustomPageSize"))
279 {
280 driver->set_custom_size_code(attr->value);
281 }
282 else if ((strncmp(attr->name, "Default", 7) ||
283 !strcmp(attr->name, "DefaultColorSpace")) &&
284 strcmp(attr->name, "ColorDevice") &&
285 strcmp(attr->name, "Manufacturer") &&
286 strcmp(attr->name, "ModelName") &&
287 strcmp(attr->name, "MaxMediaHeight") &&
288 strcmp(attr->name, "MaxMediaWidth") &&
289 strcmp(attr->name, "NickName") &&
290 strcmp(attr->name, "ParamCustomPageSize") &&
291 strcmp(attr->name, "ShortNickName") &&
292 strcmp(attr->name, "Throughput") &&
293 strcmp(attr->name, "PCFileName") &&
294 strcmp(attr->name, "FileVersion") &&
295 strcmp(attr->name, "FormatVersion") &&
296 strcmp(attr->name, "HWMargins") &&
297 strcmp(attr->name, "VariablePaperSize") &&
298 strcmp(attr->name, "LanguageEncoding") &&
299 strcmp(attr->name, "LanguageVersion") &&
300 strcmp(attr->name, "cupsFilter") &&
301 strcmp(attr->name, "cupsFlipDuplex") &&
302 strcmp(attr->name, "cupsLanguages") &&
303 strcmp(attr->name, "cupsManualCopies") &&
304 strcmp(attr->name, "cupsModelNumber") &&
305 strcmp(attr->name, "cupsVersion"))
306 {
307 if ((ptr = strchr(attr->name, '.')) != NULL &&
308 ((ptr - attr->name) == 2 || (ptr - attr->name) == 5))
309 {
310 // Might be a localization attribute; test further...
311 if (isalpha(attr->name[0] & 255) &&
312 isalpha(attr->name[1] & 255) &&
313 (attr->name[2] == '.' ||
314 (attr->name[2] == '_' && isalpha(attr->name[3] & 255) &&
315 isalpha(attr->name[4] & 255))))
316 continue;
317 }
318
319 // Attribute...
320 driver->add_attr(new ppdcAttr(attr->name, attr->spec, attr->text,
321 attr->value));
322 }
323 else if (!strncmp(attr->name, "Default", 7) &&
324 !ppdFindOption(ppd, attr->name + 7) &&
325 strcmp(attr->name, "DefaultFont") &&
326 strcmp(attr->name, "DefaultImageableArea") &&
327 strcmp(attr->name, "DefaultPaperDimension") &&
328 strcmp(attr->name, "DefaultFont"))
329 {
330 // Default attribute...
331 driver->add_attr(new ppdcAttr(attr->name, attr->spec, attr->text,
332 attr->value));
333 }
334 }
335 }
336
337 return (1);
338 }
339
340
341 //
342 // End of "$Id$".
343 //