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