]>
git.ipfire.org Git - thirdparty/cups.git/blob - ppdc/ppdc.cxx
2 // PPD file compiler main entry for the CUPS PPD Compiler.
4 // Copyright 2007-2014 by Apple Inc.
5 // Copyright 2002-2007 by Easy Software Products.
7 // Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 // Include necessary headers...
14 #include "ppdc-private.h"
17 #include <sys/types.h>
24 static void usage(void) _CUPS_NORETURN
;
28 // 'main()' - Main entry for the PPD compiler.
31 int // O - Exit status
32 main(int argc
, // I - Number of command-line arguments
33 char *argv
[]) // I - Command-line arguments
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
47 // Lowercase pcfilename
48 filename
[1024]; // PPD filename
50 do_test
, // Test PPD files
51 single_language
,// Generate single-language files
52 use_model_name
, // Use ModelName for filename
54 ppdcLineEnding le
; // Line ending to use
55 ppdcArray
*locales
; // List of locales
56 cups_array_t
*filenames
; // List of generated filenames
61 // Scan the command-line...
69 src
= new ppdcSource();
72 filenames
= cupsArrayNew((cups_array_func_t
)_cups_strcasecmp
, NULL
);
74 for (i
= 1; i
< argc
; i
++)
75 if (argv
[i
][0] == '-')
77 for (opt
= argv
[i
] + 1; *opt
; opt
++)
80 case 'D' : // Define variable
85 if ((value
= strchr(argv
[i
], '=')) != NULL
)
89 src
->set_variable(argv
[i
], value
);
92 src
->set_variable(argv
[i
], "1");
95 case 'I' : // Include directory...
101 _cupsLangPrintf(stdout
,
102 _("ppdc: Adding include directory \"%s\"."),
105 ppdcSource::add_include(argv
[i
]);
108 case 'c' : // Message catalog...
114 _cupsLangPrintf(stdout
,
115 _("ppdc: Loading messages from \"%s\"."),
119 catalog
= new ppdcCatalog("en");
121 if (catalog
->load_messages(argv
[i
]))
123 _cupsLangPrintf(stderr
,
124 _("ppdc: Unable to load localization file "
125 "\"%s\" - %s"), argv
[i
], strerror(errno
));
130 case 'd' : // Output directory...
136 _cupsLangPrintf(stdout
,
137 _("ppdc: Writing PPD files to directory "
138 "\"%s\"."), argv
[i
]);
143 case 'l' : // Language(s)...
148 if (strchr(argv
[i
], ','))
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
156 locales
= new ppdcArray();
158 strlcpy(temp
, argv
[i
], sizeof(temp
));
159 for (start
= temp
; *start
; start
= end
)
161 if ((end
= strchr(start
, ',')) != NULL
)
164 end
= start
+ strlen(start
);
167 locales
->add(new ppdcString(start
));
175 _cupsLangPrintf(stdout
,
176 _("ppdc: Loading messages for locale "
177 "\"%s\"."), argv
[i
]);
182 catalog
= new ppdcCatalog(argv
[i
]);
184 if (catalog
->messages
->count
== 0 && strcmp(argv
[i
], "en"))
186 _cupsLangPrintf(stderr
,
187 _("ppdc: Unable to find localization for "
188 "\"%s\" - %s"), argv
[i
], strerror(errno
));
194 case 'm' : // Use ModelName for filename
198 case 't' : // Test PPDs instead of generating them
202 case 'v' : // Be verbose...
206 case 'z' : // Compress files...
210 case '-' : // --option
211 if (!strcmp(opt
, "-lf"))
214 opt
+= strlen(opt
) - 1;
217 else if (!strcmp(opt
, "-cr"))
220 opt
+= strlen(opt
) - 1;
223 else if (!strcmp(opt
, "-crlf"))
226 opt
+= strlen(opt
) - 1;
236 // Open and load the driver info file...
238 _cupsLangPrintf(stdout
,
239 _("ppdc: Loading driver information file \"%s\"."),
242 src
->read_file(argv
[i
]);
246 if (src
->drivers
->count
> 0)
248 // Create the output directory...
249 if (mkdir(outdir
, 0777))
253 _cupsLangPrintf(stderr
,
254 _("ppdc: Unable to create output directory %s: %s"),
255 outdir
, strerror(errno
));
260 // Write PPD files...
261 for (d
= (ppdcDriver
*)src
->drivers
->first();
263 d
= (ppdcDriver
*)src
->drivers
->next())
267 // Test the PPD file for this driver...
268 int pid
, // Process ID
269 fds
[2]; // Pipe file descriptors
274 _cupsLangPrintf(stderr
,
275 _("ppdc: Unable to create output pipes: %s"),
280 if ((pid
= fork()) == 0)
282 // Child process comes here...
288 execlp("cupstestppd", "cupstestppd", "-", (char *)0);
290 _cupsLangPrintf(stderr
,
291 _("ppdc: Unable to execute cupstestppd: %s"),
297 _cupsLangPrintf(stderr
, _("ppdc: Unable to execute cupstestppd: %s"),
303 fp
= cupsFileOpenFd(fds
[1], "w");
307 // Write the PPD file for this driver...
310 if (!_cups_strncasecmp(d
->model_name
->value
, d
->manufacturer
->value
,
311 strlen(d
->manufacturer
->value
)))
313 // Model name already starts with the manufacturer...
314 outname
= d
->model_name
->value
;
318 // Add manufacturer to the front of the model name...
319 snprintf(make_model
, sizeof(make_model
), "%s %s",
320 d
->manufacturer
->value
, d
->model_name
->value
);
321 outname
= make_model
;
324 else if (d
->file_name
)
325 outname
= d
->file_name
->value
;
327 outname
= d
->pc_file_name
->value
;
329 if (strstr(outname
, ".PPD"))
331 // Convert PCFileName to lowercase...
333 outname
[j
] && j
< (int)(sizeof(pcfilename
) - 1);
335 pcfilename
[j
] = (char)tolower(outname
[j
] & 255);
337 pcfilename
[j
] = '\0';
341 // Leave PCFileName as-is...
342 strlcpy(pcfilename
, outname
, sizeof(pcfilename
));
345 // Open the PPD file for writing...
347 snprintf(filename
, sizeof(filename
), "%s/%s.gz", outdir
, pcfilename
);
349 snprintf(filename
, sizeof(filename
), "%s/%s", outdir
, pcfilename
);
351 if (cupsArrayFind(filenames
, filename
))
352 _cupsLangPrintf(stderr
,
353 _("ppdc: Warning - overlapping filename \"%s\"."),
356 cupsArrayAdd(filenames
, strdup(filename
));
358 fp
= cupsFileOpen(filename
, comp
? "w9" : "w");
361 _cupsLangPrintf(stderr
,
362 _("ppdc: Unable to create PPD file \"%s\" - %s."),
363 filename
, strerror(errno
));
368 _cupsLangPrintf(stdout
, _("ppdc: Writing %s."), filename
);
372 * Write the PPD file...
375 ppdcArray
*templocales
= locales
;
377 if (!templocales
&& !single_language
)
379 templocales
= new ppdcArray();
380 for (ppdcCatalog
*tempcatalog
= (ppdcCatalog
*)src
->po_files
->first();
382 tempcatalog
= (ppdcCatalog
*)src
->po_files
->next())
384 tempcatalog
->locale
->retain();
385 templocales
->add(tempcatalog
->locale
);
389 if (d
->write_ppd_file(fp
, catalog
, templocales
, src
, le
))
395 if (templocales
&& templocales
!= locales
)
396 templocales
->release();
404 // Delete the printer driver information...
407 // Message catalog...
411 // Return with no errors.
417 // 'usage()' - Show usage and exit.
423 _cupsLangPuts(stdout
, _("Usage: ppdc [options] filename.drv [ ... "
425 _cupsLangPuts(stdout
, _("Options:"));
426 _cupsLangPuts(stdout
, _(" -D name=value Set named variable to "
428 _cupsLangPuts(stdout
, _(" -I include-dir Add include directory to "
430 _cupsLangPuts(stdout
, _(" -c catalog.po Load the specified "
431 "message catalog."));
432 _cupsLangPuts(stdout
, _(" -d output-dir Specify the output "
434 _cupsLangPuts(stdout
, _(" -l lang[,lang,...] Specify the output "
435 "language(s) (locale)."));
436 _cupsLangPuts(stdout
, _(" -m Use the ModelName value "
437 "as the filename."));
438 _cupsLangPuts(stdout
, _(" -t Test PPDs instead of "
439 "generating them."));
440 _cupsLangPuts(stdout
, _(" -v Be verbose."));
441 _cupsLangPuts(stdout
, _(" -z Compress PPD files using "
443 _cupsLangPuts(stdout
, _(" --cr End lines with CR (Mac "
445 _cupsLangPuts(stdout
, _(" --crlf End lines with CR + LF "
447 _cupsLangPuts(stdout
, _(" --lf End lines with LF "
448 "(UNIX/Linux/macOS)."));