]> git.ipfire.org Git - thirdparty/cups.git/blob - ppdc/ppdc.cxx
Don't generate certificates that expire on Feb 29th (Issue #5643)
[thirdparty/cups.git] / ppdc / ppdc.cxx
1 //
2 // PPD file compiler main entry for the CUPS PPD Compiler.
3 //
4 // Copyright 2007-2014 by Apple Inc.
5 // Copyright 2002-2007 by Easy Software Products.
6 //
7 // Licensed under Apache License v2.0. See the file "LICENSE" for more information.
8 //
9
10 //
11 // Include necessary headers...
12 //
13
14 #include "ppdc-private.h"
15 #include <unistd.h>
16 #include <sys/stat.h>
17 #include <sys/types.h>
18
19
20 //
21 // Local functions...
22 //
23
24 static void usage(void);
25
26
27 //
28 // 'main()' - Main entry for the PPD compiler.
29 //
30
31 int // O - Exit status
32 main(int argc, // I - Number of command-line arguments
33 char *argv[]) // I - Command-line arguments
34 {
35 int i, j; // Looping vars
36 ppdcCatalog *catalog; // Message catalog
37 const char *outdir; // Output directory
38 ppdcSource *src; // PPD source file data
39 ppdcDriver *d; // Current driver
40 cups_file_t *fp; // PPD file
41 char *opt, // Current option
42 *value, // Value in option
43 *outname, // Output filename
44 make_model[1024],
45 // Make and model
46 pcfilename[1024],
47 // Lowercase pcfilename
48 filename[1024]; // PPD filename
49 int comp, // Compress
50 do_test, // Test PPD files
51 single_language,// Generate single-language files
52 use_model_name, // Use ModelName for filename
53 verbose; // Verbosity
54 ppdcLineEnding le; // Line ending to use
55 ppdcArray *locales; // List of locales
56 cups_array_t *filenames; // List of generated filenames
57
58
59 _cupsSetLocale(argv);
60
61 // Scan the command-line...
62 catalog = NULL;
63 comp = 0;
64 do_test = 0;
65 le = PPDC_LFONLY;
66 locales = NULL;
67 outdir = "ppd";
68 single_language = 0;
69 src = new ppdcSource();
70 use_model_name = 0;
71 verbose = 0;
72 filenames = cupsArrayNew((cups_array_func_t)_cups_strcasecmp, NULL);
73
74 for (i = 1; i < argc; i ++)
75 if (argv[i][0] == '-')
76 {
77 for (opt = argv[i] + 1; *opt; opt ++)
78 switch (*opt)
79 {
80 case 'D' : // Define variable
81 i ++;
82 if (i >= argc)
83 usage();
84
85 if ((value = strchr(argv[i], '=')) != NULL)
86 {
87 *value++ = '\0';
88
89 src->set_variable(argv[i], value);
90 }
91 else
92 src->set_variable(argv[i], "1");
93 break;
94
95 case 'I' : // Include directory...
96 i ++;
97 if (i >= argc)
98 usage();
99
100 if (verbose > 1)
101 _cupsLangPrintf(stdout,
102 _("ppdc: Adding include directory \"%s\"."),
103 argv[i]);
104
105 ppdcSource::add_include(argv[i]);
106 break;
107
108 case 'c' : // Message catalog...
109 i ++;
110 if (i >= argc)
111 usage();
112
113 if (verbose > 1)
114 _cupsLangPrintf(stdout,
115 _("ppdc: Loading messages from \"%s\"."),
116 argv[i]);
117
118 if (!catalog)
119 catalog = new ppdcCatalog("en");
120
121 if (catalog->load_messages(argv[i]))
122 {
123 _cupsLangPrintf(stderr,
124 _("ppdc: Unable to load localization file "
125 "\"%s\" - %s"), argv[i], strerror(errno));
126 return (1);
127 }
128 break;
129
130 case 'd' : // Output directory...
131 i ++;
132 if (i >= argc)
133 usage();
134
135 if (verbose > 1)
136 _cupsLangPrintf(stdout,
137 _("ppdc: Writing PPD files to directory "
138 "\"%s\"."), argv[i]);
139
140 outdir = argv[i];
141 break;
142
143 case 'l' : // Language(s)...
144 i ++;
145 if (i >= argc)
146 usage();
147
148 if (strchr(argv[i], ','))
149 {
150 // Comma-delimited list of languages...
151 char temp[1024], // Copy of language list
152 *start, // Start of current locale name
153 *end; // End of current locale name
154
155
156 locales = new ppdcArray();
157
158 strlcpy(temp, argv[i], sizeof(temp));
159 for (start = temp; *start; start = end)
160 {
161 if ((end = strchr(start, ',')) != NULL)
162 *end++ = '\0';
163 else
164 end = start + strlen(start);
165
166 if (end > start)
167 locales->add(new ppdcString(start));
168 }
169 }
170 else
171 {
172 single_language = 1;
173
174 if (verbose > 1)
175 _cupsLangPrintf(stdout,
176 _("ppdc: Loading messages for locale "
177 "\"%s\"."), argv[i]);
178
179 if (catalog)
180 catalog->release();
181
182 catalog = new ppdcCatalog(argv[i]);
183
184 if (catalog->messages->count == 0 && strcmp(argv[i], "en"))
185 {
186 _cupsLangPrintf(stderr,
187 _("ppdc: Unable to find localization for "
188 "\"%s\" - %s"), argv[i], strerror(errno));
189 return (1);
190 }
191 }
192 break;
193
194 case 'm' : // Use ModelName for filename
195 use_model_name = 1;
196 break;
197
198 case 't' : // Test PPDs instead of generating them
199 do_test = 1;
200 break;
201
202 case 'v' : // Be verbose...
203 verbose ++;
204 break;
205
206 case 'z' : // Compress files...
207 comp = 1;
208 break;
209
210 case '-' : // --option
211 if (!strcmp(opt, "-lf"))
212 {
213 le = PPDC_LFONLY;
214 opt += strlen(opt) - 1;
215 break;
216 }
217 else if (!strcmp(opt, "-cr"))
218 {
219 le = PPDC_CRONLY;
220 opt += strlen(opt) - 1;
221 break;
222 }
223 else if (!strcmp(opt, "-crlf"))
224 {
225 le = PPDC_CRLF;
226 opt += strlen(opt) - 1;
227 break;
228 }
229
230 default : // Unknown
231 usage();
232 break;
233 }
234 }
235 else
236 {
237 // Open and load the driver info file...
238 if (verbose > 1)
239 _cupsLangPrintf(stdout,
240 _("ppdc: Loading driver information file \"%s\"."),
241 argv[i]);
242
243 src->read_file(argv[i]);
244 }
245
246
247 if (src->drivers->count > 0)
248 {
249 // Create the output directory...
250 if (mkdir(outdir, 0777))
251 {
252 if (errno != EEXIST)
253 {
254 _cupsLangPrintf(stderr,
255 _("ppdc: Unable to create output directory %s: %s"),
256 outdir, strerror(errno));
257 return (1);
258 }
259 }
260
261 // Write PPD files...
262 for (d = (ppdcDriver *)src->drivers->first();
263 d;
264 d = (ppdcDriver *)src->drivers->next())
265 {
266 if (do_test)
267 {
268 // Test the PPD file for this driver...
269 int pid, // Process ID
270 fds[2]; // Pipe file descriptors
271
272
273 if (pipe(fds))
274 {
275 _cupsLangPrintf(stderr,
276 _("ppdc: Unable to create output pipes: %s"),
277 strerror(errno));
278 return (1);
279 }
280
281 if ((pid = fork()) == 0)
282 {
283 // Child process comes here...
284 dup2(fds[0], 0);
285
286 close(fds[0]);
287 close(fds[1]);
288
289 execlp("cupstestppd", "cupstestppd", "-", (char *)0);
290
291 _cupsLangPrintf(stderr,
292 _("ppdc: Unable to execute cupstestppd: %s"),
293 strerror(errno));
294 return (errno);
295 }
296 else if (pid < 0)
297 {
298 _cupsLangPrintf(stderr, _("ppdc: Unable to execute cupstestppd: %s"),
299 strerror(errno));
300 return (errno);
301 }
302
303 close(fds[0]);
304 fp = cupsFileOpenFd(fds[1], "w");
305 }
306 else
307 {
308 // Write the PPD file for this driver...
309 if (use_model_name)
310 {
311 if (!_cups_strncasecmp(d->model_name->value, d->manufacturer->value,
312 strlen(d->manufacturer->value)))
313 {
314 // Model name already starts with the manufacturer...
315 outname = d->model_name->value;
316 }
317 else
318 {
319 // Add manufacturer to the front of the model name...
320 snprintf(make_model, sizeof(make_model), "%s %s",
321 d->manufacturer->value, d->model_name->value);
322 outname = make_model;
323 }
324 }
325 else if (d->file_name)
326 outname = d->file_name->value;
327 else
328 outname = d->pc_file_name->value;
329
330 if (strstr(outname, ".PPD"))
331 {
332 // Convert PCFileName to lowercase...
333 for (j = 0;
334 outname[j] && j < (int)(sizeof(pcfilename) - 1);
335 j ++)
336 pcfilename[j] = (char)tolower(outname[j] & 255);
337
338 pcfilename[j] = '\0';
339 }
340 else
341 {
342 // Leave PCFileName as-is...
343 strlcpy(pcfilename, outname, sizeof(pcfilename));
344 }
345
346 // Open the PPD file for writing...
347 if (comp)
348 snprintf(filename, sizeof(filename), "%s/%s.gz", outdir, pcfilename);
349 else
350 snprintf(filename, sizeof(filename), "%s/%s", outdir, pcfilename);
351
352 if (cupsArrayFind(filenames, filename))
353 _cupsLangPrintf(stderr,
354 _("ppdc: Warning - overlapping filename \"%s\"."),
355 filename);
356 else
357 cupsArrayAdd(filenames, strdup(filename));
358
359 fp = cupsFileOpen(filename, comp ? "w9" : "w");
360 if (!fp)
361 {
362 _cupsLangPrintf(stderr,
363 _("ppdc: Unable to create PPD file \"%s\" - %s."),
364 filename, strerror(errno));
365 return (1);
366 }
367
368 if (verbose)
369 _cupsLangPrintf(stdout, _("ppdc: Writing %s."), filename);
370 }
371
372 /*
373 * Write the PPD file...
374 */
375
376 ppdcArray *templocales = locales;
377
378 if (!templocales && !single_language)
379 {
380 templocales = new ppdcArray();
381 for (ppdcCatalog *tempcatalog = (ppdcCatalog *)src->po_files->first();
382 tempcatalog;
383 tempcatalog = (ppdcCatalog *)src->po_files->next())
384 {
385 tempcatalog->locale->retain();
386 templocales->add(tempcatalog->locale);
387 }
388 }
389
390 if (d->write_ppd_file(fp, catalog, templocales, src, le))
391 {
392 cupsFileClose(fp);
393 return (1);
394 }
395
396 if (templocales != locales)
397 templocales->release();
398
399 cupsFileClose(fp);
400 }
401 }
402 else
403 usage();
404
405 // Delete the printer driver information...
406 src->release();
407
408 // Message catalog...
409 if (catalog)
410 catalog->release();
411
412 // Return with no errors.
413 return (0);
414 }
415
416
417 //
418 // 'usage()' - Show usage and exit.
419 //
420
421 static void
422 usage(void)
423 {
424 _cupsLangPuts(stdout, _("Usage: ppdc [options] filename.drv [ ... "
425 "filenameN.drv ]"));
426 _cupsLangPuts(stdout, _("Options:"));
427 _cupsLangPuts(stdout, _(" -D name=value Set named variable to "
428 "value."));
429 _cupsLangPuts(stdout, _(" -I include-dir Add include directory to "
430 "search path."));
431 _cupsLangPuts(stdout, _(" -c catalog.po Load the specified "
432 "message catalog."));
433 _cupsLangPuts(stdout, _(" -d output-dir Specify the output "
434 "directory."));
435 _cupsLangPuts(stdout, _(" -l lang[,lang,...] Specify the output "
436 "language(s) (locale)."));
437 _cupsLangPuts(stdout, _(" -m Use the ModelName value "
438 "as the filename."));
439 _cupsLangPuts(stdout, _(" -t Test PPDs instead of "
440 "generating them."));
441 _cupsLangPuts(stdout, _(" -v Be verbose."));
442 _cupsLangPuts(stdout, _(" -z Compress PPD files using "
443 "GNU zip."));
444 _cupsLangPuts(stdout, _(" --cr End lines with CR (Mac "
445 "OS 9)."));
446 _cupsLangPuts(stdout, _(" --crlf End lines with CR + LF "
447 "(Windows)."));
448 _cupsLangPuts(stdout, _(" --lf End lines with LF "
449 "(UNIX/Linux/macOS)."));
450
451 exit(1);
452 }