]>
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);
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;
237 // Open and load the driver info file...
239 _cupsLangPrintf(stdout
,
240 _("ppdc: Loading driver information file \"%s\"."),
243 src
->read_file(argv
[i
]);
247 if (src
->drivers
->count
> 0)
249 // Create the output directory...
250 if (mkdir(outdir
, 0777))
254 _cupsLangPrintf(stderr
,
255 _("ppdc: Unable to create output directory %s: %s"),
256 outdir
, strerror(errno
));
261 // Write PPD files...
262 for (d
= (ppdcDriver
*)src
->drivers
->first();
264 d
= (ppdcDriver
*)src
->drivers
->next())
268 // Test the PPD file for this driver...
269 int pid
, // Process ID
270 fds
[2]; // Pipe file descriptors
275 _cupsLangPrintf(stderr
,
276 _("ppdc: Unable to create output pipes: %s"),
281 if ((pid
= fork()) == 0)
283 // Child process comes here...
289 execlp("cupstestppd", "cupstestppd", "-", (char *)0);
291 _cupsLangPrintf(stderr
,
292 _("ppdc: Unable to execute cupstestppd: %s"),
298 _cupsLangPrintf(stderr
, _("ppdc: Unable to execute cupstestppd: %s"),
304 fp
= cupsFileOpenFd(fds
[1], "w");
308 // Write the PPD file for this driver...
311 if (!_cups_strncasecmp(d
->model_name
->value
, d
->manufacturer
->value
,
312 strlen(d
->manufacturer
->value
)))
314 // Model name already starts with the manufacturer...
315 outname
= d
->model_name
->value
;
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
;
325 else if (d
->file_name
)
326 outname
= d
->file_name
->value
;
328 outname
= d
->pc_file_name
->value
;
330 if (strstr(outname
, ".PPD"))
332 // Convert PCFileName to lowercase...
334 outname
[j
] && j
< (int)(sizeof(pcfilename
) - 1);
336 pcfilename
[j
] = (char)tolower(outname
[j
] & 255);
338 pcfilename
[j
] = '\0';
342 // Leave PCFileName as-is...
343 strlcpy(pcfilename
, outname
, sizeof(pcfilename
));
346 // Open the PPD file for writing...
348 snprintf(filename
, sizeof(filename
), "%s/%s.gz", outdir
, pcfilename
);
350 snprintf(filename
, sizeof(filename
), "%s/%s", outdir
, pcfilename
);
352 if (cupsArrayFind(filenames
, filename
))
353 _cupsLangPrintf(stderr
,
354 _("ppdc: Warning - overlapping filename \"%s\"."),
357 cupsArrayAdd(filenames
, strdup(filename
));
359 fp
= cupsFileOpen(filename
, comp
? "w9" : "w");
362 _cupsLangPrintf(stderr
,
363 _("ppdc: Unable to create PPD file \"%s\" - %s."),
364 filename
, strerror(errno
));
369 _cupsLangPrintf(stdout
, _("ppdc: Writing %s."), filename
);
373 * Write the PPD file...
376 ppdcArray
*templocales
= locales
;
378 if (!templocales
&& !single_language
)
380 templocales
= new ppdcArray();
381 for (ppdcCatalog
*tempcatalog
= (ppdcCatalog
*)src
->po_files
->first();
383 tempcatalog
= (ppdcCatalog
*)src
->po_files
->next())
385 tempcatalog
->locale
->retain();
386 templocales
->add(tempcatalog
->locale
);
390 if (d
->write_ppd_file(fp
, catalog
, templocales
, src
, le
))
396 if (templocales
!= locales
)
397 templocales
->release();
405 // Delete the printer driver information...
408 // Message catalog...
412 // Return with no errors.
418 // 'usage()' - Show usage and exit.
424 _cupsLangPuts(stdout
, _("Usage: ppdc [options] filename.drv [ ... "
426 _cupsLangPuts(stdout
, _("Options:"));
427 _cupsLangPuts(stdout
, _(" -D name=value Set named variable to "
429 _cupsLangPuts(stdout
, _(" -I include-dir Add include directory to "
431 _cupsLangPuts(stdout
, _(" -c catalog.po Load the specified "
432 "message catalog."));
433 _cupsLangPuts(stdout
, _(" -d output-dir Specify the output "
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 "
444 _cupsLangPuts(stdout
, _(" --cr End lines with CR (Mac "
446 _cupsLangPuts(stdout
, _(" --crlf End lines with CR + LF "
448 _cupsLangPuts(stdout
, _(" --lf End lines with LF "
449 "(UNIX/Linux/macOS)."));