]>
git.ipfire.org Git - thirdparty/cups.git/blob - ppdc/ppdc.cxx
4 // PPD file compiler main entry for the CUPS PPD Compiler.
6 // Copyright 2007-2014 by Apple Inc.
7 // Copyright 2002-2007 by Easy Software Products.
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/".
17 // Include necessary headers...
20 #include "ppdc-private.h"
23 #include <sys/types.h>
30 static void usage(void);
34 // 'main()' - Main entry for the PPD compiler.
37 int // O - Exit status
38 main(int argc
, // I - Number of command-line arguments
39 char *argv
[]) // I - Command-line arguments
41 int i
, j
; // Looping vars
42 ppdcCatalog
*catalog
; // Message catalog
43 const char *outdir
; // Output directory
44 ppdcSource
*src
; // PPD source file data
45 ppdcDriver
*d
; // Current driver
46 cups_file_t
*fp
; // PPD file
47 char *opt
, // Current option
48 *value
, // Value in option
49 *outname
, // Output filename
53 // Lowercase pcfilename
54 filename
[1024]; // PPD filename
56 do_test
, // Test PPD files
57 single_language
,// Generate single-language files
58 use_model_name
, // Use ModelName for filename
60 ppdcLineEnding le
; // Line ending to use
61 ppdcArray
*locales
; // List of locales
62 cups_array_t
*filenames
; // List of generated filenames
67 // Scan the command-line...
75 src
= new ppdcSource();
78 filenames
= cupsArrayNew((cups_array_func_t
)_cups_strcasecmp
, NULL
);
80 for (i
= 1; i
< argc
; i
++)
81 if (argv
[i
][0] == '-')
83 for (opt
= argv
[i
] + 1; *opt
; opt
++)
86 case 'D' : // Define variable
91 if ((value
= strchr(argv
[i
], '=')) != NULL
)
95 src
->set_variable(argv
[i
], value
);
98 src
->set_variable(argv
[i
], "1");
101 case 'I' : // Include directory...
107 _cupsLangPrintf(stdout
,
108 _("ppdc: Adding include directory \"%s\"."),
111 ppdcSource::add_include(argv
[i
]);
114 case 'c' : // Message catalog...
120 _cupsLangPrintf(stdout
,
121 _("ppdc: Loading messages from \"%s\"."),
125 catalog
= new ppdcCatalog("en");
127 if (catalog
->load_messages(argv
[i
]))
129 _cupsLangPrintf(stderr
,
130 _("ppdc: Unable to load localization file "
131 "\"%s\" - %s"), argv
[i
], strerror(errno
));
136 case 'd' : // Output directory...
142 _cupsLangPrintf(stdout
,
143 _("ppdc: Writing PPD files to directory "
144 "\"%s\"."), argv
[i
]);
149 case 'l' : // Language(s)...
154 if (strchr(argv
[i
], ','))
156 // Comma-delimited list of languages...
157 char temp
[1024], // Copy of language list
158 *start
, // Start of current locale name
159 *end
; // End of current locale name
162 locales
= new ppdcArray();
164 strlcpy(temp
, argv
[i
], sizeof(temp
));
165 for (start
= temp
; *start
; start
= end
)
167 if ((end
= strchr(start
, ',')) != NULL
)
170 end
= start
+ strlen(start
);
173 locales
->add(new ppdcString(start
));
181 _cupsLangPrintf(stdout
,
182 _("ppdc: Loading messages for locale "
183 "\"%s\"."), argv
[i
]);
188 catalog
= new ppdcCatalog(argv
[i
]);
190 if (catalog
->messages
->count
== 0 && strcmp(argv
[i
], "en"))
192 _cupsLangPrintf(stderr
,
193 _("ppdc: Unable to find localization for "
194 "\"%s\" - %s"), argv
[i
], strerror(errno
));
200 case 'm' : // Use ModelName for filename
204 case 't' : // Test PPDs instead of generating them
208 case 'v' : // Be verbose...
212 case 'z' : // Compress files...
216 case '-' : // --option
217 if (!strcmp(opt
, "-lf"))
220 opt
+= strlen(opt
) - 1;
223 else if (!strcmp(opt
, "-cr"))
226 opt
+= strlen(opt
) - 1;
229 else if (!strcmp(opt
, "-crlf"))
232 opt
+= strlen(opt
) - 1;
243 // Open and load the driver info file...
245 _cupsLangPrintf(stdout
,
246 _("ppdc: Loading driver information file \"%s\"."),
249 src
->read_file(argv
[i
]);
253 if (src
->drivers
->count
> 0)
255 // Create the output directory...
256 if (mkdir(outdir
, 0777))
260 _cupsLangPrintf(stderr
,
261 _("ppdc: Unable to create output directory %s: %s"),
262 outdir
, strerror(errno
));
267 // Write PPD files...
268 for (d
= (ppdcDriver
*)src
->drivers
->first();
270 d
= (ppdcDriver
*)src
->drivers
->next())
274 // Test the PPD file for this driver...
275 int pid
, // Process ID
276 fds
[2]; // Pipe file descriptors
281 _cupsLangPrintf(stderr
,
282 _("ppdc: Unable to create output pipes: %s"),
287 if ((pid
= fork()) == 0)
289 // Child process comes here...
295 execlp("cupstestppd", "cupstestppd", "-", (char *)0);
297 _cupsLangPrintf(stderr
,
298 _("ppdc: Unable to execute cupstestppd: %s"),
304 _cupsLangPrintf(stderr
, _("ppdc: Unable to execute cupstestppd: %s"),
310 fp
= cupsFileOpenFd(fds
[1], "w");
314 // Write the PPD file for this driver...
317 if (!_cups_strncasecmp(d
->model_name
->value
, d
->manufacturer
->value
,
318 strlen(d
->manufacturer
->value
)))
320 // Model name already starts with the manufacturer...
321 outname
= d
->model_name
->value
;
325 // Add manufacturer to the front of the model name...
326 snprintf(make_model
, sizeof(make_model
), "%s %s",
327 d
->manufacturer
->value
, d
->model_name
->value
);
328 outname
= make_model
;
331 else if (d
->file_name
)
332 outname
= d
->file_name
->value
;
334 outname
= d
->pc_file_name
->value
;
336 if (strstr(outname
, ".PPD"))
338 // Convert PCFileName to lowercase...
340 outname
[j
] && j
< (int)(sizeof(pcfilename
) - 1);
342 pcfilename
[j
] = (char)tolower(outname
[j
] & 255);
344 pcfilename
[j
] = '\0';
348 // Leave PCFileName as-is...
349 strlcpy(pcfilename
, outname
, sizeof(pcfilename
));
352 // Open the PPD file for writing...
354 snprintf(filename
, sizeof(filename
), "%s/%s.gz", outdir
, pcfilename
);
356 snprintf(filename
, sizeof(filename
), "%s/%s", outdir
, pcfilename
);
358 if (cupsArrayFind(filenames
, filename
))
359 _cupsLangPrintf(stderr
,
360 _("ppdc: Warning - overlapping filename \"%s\"."),
363 cupsArrayAdd(filenames
, strdup(filename
));
365 fp
= cupsFileOpen(filename
, comp
? "w9" : "w");
368 _cupsLangPrintf(stderr
,
369 _("ppdc: Unable to create PPD file \"%s\" - %s."),
370 filename
, strerror(errno
));
375 _cupsLangPrintf(stdout
, _("ppdc: Writing %s."), filename
);
379 * Write the PPD file...
382 ppdcArray
*templocales
= locales
;
384 if (!templocales
&& !single_language
)
386 templocales
= new ppdcArray();
387 for (ppdcCatalog
*tempcatalog
= (ppdcCatalog
*)src
->po_files
->first();
389 tempcatalog
= (ppdcCatalog
*)src
->po_files
->next())
391 tempcatalog
->locale
->retain();
392 templocales
->add(tempcatalog
->locale
);
396 if (d
->write_ppd_file(fp
, catalog
, templocales
, src
, le
))
402 if (templocales
!= locales
)
403 templocales
->release();
411 // Delete the printer driver information...
414 // Message catalog...
418 // Return with no errors.
424 // 'usage()' - Show usage and exit.
430 _cupsLangPuts(stdout
, _("Usage: ppdc [options] filename.drv [ ... "
432 _cupsLangPuts(stdout
, _("Options:"));
433 _cupsLangPuts(stdout
, _(" -D name=value Set named variable to "
435 _cupsLangPuts(stdout
, _(" -I include-dir Add include directory to "
437 _cupsLangPuts(stdout
, _(" -c catalog.po Load the specified "
438 "message catalog."));
439 _cupsLangPuts(stdout
, _(" -d output-dir Specify the output "
441 _cupsLangPuts(stdout
, _(" -l lang[,lang,...] Specify the output "
442 "language(s) (locale)."));
443 _cupsLangPuts(stdout
, _(" -m Use the ModelName value "
444 "as the filename."));
445 _cupsLangPuts(stdout
, _(" -t Test PPDs instead of "
446 "generating them."));
447 _cupsLangPuts(stdout
, _(" -v Be verbose."));
448 _cupsLangPuts(stdout
, _(" -z Compress PPD files using "
450 _cupsLangPuts(stdout
, _(" --cr End lines with CR (Mac "
452 _cupsLangPuts(stdout
, _(" --crlf End lines with CR + LF "
454 _cupsLangPuts(stdout
, _(" --lf End lines with LF "
455 "(UNIX/Linux/OS X)."));