]>
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-2010 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 // main() - Main entry for the PPD compiler.
18 // usage() - Show usage and exit.
22 // Include necessary headers...
25 #include "ppdc-private.h"
28 #include <sys/types.h>
35 static void usage(void);
39 // 'main()' - Main entry for the PPD compiler.
42 int // O - Exit status
43 main(int argc
, // I - Number of command-line arguments
44 char *argv
[]) // I - Command-line arguments
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
54 *outname
, // Output filename
58 // Lowercase pcfilename
59 filename
[1024]; // PPD filename
61 do_test
, // Test PPD files
62 use_model_name
, // Use ModelName for filename
64 ppdcLineEnding le
; // Line ending to use
65 ppdcArray
*locales
; // List of locales
66 cups_array_t
*filenames
; // List of generated filenames
71 // Scan the command-line...
78 src
= new ppdcSource();
81 filenames
= cupsArrayNew((cups_array_func_t
)strcasecmp
, NULL
);
83 for (i
= 1; i
< argc
; i
++)
84 if (argv
[i
][0] == '-')
86 for (opt
= argv
[i
] + 1; *opt
; opt
++)
89 case 'D' : // Define variable
94 if ((value
= strchr(argv
[i
], '=')) != NULL
)
98 src
->set_variable(argv
[i
], value
);
101 src
->set_variable(argv
[i
], "1");
104 case 'I' : // Include directory...
110 _cupsLangPrintf(stdout
,
111 _("ppdc: Adding include directory \"%s\"...\n"),
114 ppdcSource::add_include(argv
[i
]);
117 case 'c' : // Message catalog...
123 _cupsLangPrintf(stdout
,
124 _("ppdc: Loading messages from \"%s\"...\n"),
128 catalog
= new ppdcCatalog("en");
130 if (catalog
->load_messages(argv
[i
]))
132 _cupsLangPrintf(stderr
,
133 _("ppdc: Unable to load localization file "
134 "\"%s\" - %s\n"), argv
[i
], strerror(errno
));
139 case 'd' : // Output directory...
145 _cupsLangPrintf(stdout
,
146 _("ppdc: Writing PPD files to directory "
147 "\"%s\"...\n"), argv
[i
]);
152 case 'l' : // Language(s)...
157 if (strchr(argv
[i
], ','))
159 // Comma-delimited list of languages...
160 char temp
[1024], // Copy of language list
161 *start
, // Start of current locale name
162 *end
; // End of current locale name
165 locales
= new ppdcArray();
167 strlcpy(temp
, argv
[i
], sizeof(temp
));
168 for (start
= temp
; *start
; start
= end
)
170 if ((end
= strchr(start
, ',')) != NULL
)
173 end
= start
+ strlen(start
);
176 locales
->add(new ppdcString(start
));
182 _cupsLangPrintf(stdout
,
183 _("ppdc: Loading messages for locale "
184 "\"%s\"...\n"), argv
[i
]);
189 catalog
= new ppdcCatalog(argv
[i
]);
191 if (catalog
->messages
->count
== 0)
193 _cupsLangPrintf(stderr
,
194 _("ppdc: Unable to find localization for "
195 "\"%s\" - %s\n"), argv
[i
], strerror(errno
));
201 case 'm' : // Use ModelName for filename
205 case 't' : // Test PPDs instead of generating them
209 case 'v' : // Be verbose...
213 case 'z' : // Compress files...
217 case '-' : // --option
218 if (!strcmp(opt
, "-lf"))
221 opt
+= strlen(opt
) - 1;
224 else if (!strcmp(opt
, "-cr"))
227 opt
+= strlen(opt
) - 1;
230 else if (!strcmp(opt
, "-crlf"))
233 opt
+= strlen(opt
) - 1;
244 // Open and load the driver info file...
246 _cupsLangPrintf(stdout
,
247 _("ppdc: Loading driver information file \"%s\"...\n"),
250 src
->read_file(argv
[i
]);
254 if (src
->drivers
->count
> 0)
256 // Create the output directory...
257 if (mkdir(outdir
, 0777))
261 _cupsLangPrintf(stderr
,
262 _("ppdc: Unable to create output directory %s: %s\n"),
263 outdir
, strerror(errno
));
268 // Write PPD files...
269 for (d
= (ppdcDriver
*)src
->drivers
->first();
271 d
= (ppdcDriver
*)src
->drivers
->next())
275 // Test the PPD file for this driver...
276 int pid
, // Process ID
277 fds
[2]; // Pipe file descriptors
282 _cupsLangPrintf(stderr
,
283 _("ppdc: Unable to create output pipes: %s\n"),
288 if ((pid
= fork()) == 0)
290 // Child process comes here...
296 execlp("cupstestppd", "cupstestppd", "-", (char *)0);
298 _cupsLangPrintf(stderr
,
299 _("ppdc: Unable to execute cupstestppd: %s\n"),
305 _cupsLangPrintf(stderr
,
306 _("ppdc: Unable to execute cupstestppd: %s\n"),
312 fp
= cupsFileOpenFd(fds
[1], "w");
316 // Write the PPD file for this driver...
319 if (!strncasecmp(d
->model_name
->value
, d
->manufacturer
->value
,
320 strlen(d
->manufacturer
->value
)))
322 // Model name already starts with the manufacturer...
323 outname
= d
->model_name
->value
;
327 // Add manufacturer to the front of the model name...
328 snprintf(make_model
, sizeof(make_model
), "%s %s",
329 d
->manufacturer
->value
, d
->model_name
->value
);
330 outname
= make_model
;
333 else if (d
->file_name
)
334 outname
= d
->file_name
->value
;
336 outname
= d
->pc_file_name
->value
;
338 if (strstr(outname
, ".PPD"))
340 // Convert PCFileName to lowercase...
342 outname
[j
] && j
< (int)(sizeof(pcfilename
) - 1);
344 pcfilename
[j
] = tolower(outname
[j
] & 255);
346 pcfilename
[j
] = '\0';
350 // Leave PCFileName as-is...
351 strlcpy(pcfilename
, outname
, sizeof(pcfilename
));
354 // Open the PPD file for writing...
356 snprintf(filename
, sizeof(filename
), "%s/%s.gz", outdir
, pcfilename
);
358 snprintf(filename
, sizeof(filename
), "%s/%s", outdir
, pcfilename
);
360 if (cupsArrayFind(filenames
, filename
))
361 _cupsLangPrintf(stderr
,
362 _("ppdc: Warning - overlapping filename \"%s\".\n"),
365 cupsArrayAdd(filenames
, strdup(filename
));
367 fp
= cupsFileOpen(filename
, comp
? "w9" : "w");
370 _cupsLangPrintf(stderr
,
371 _("ppdc: Unable to create PPD file \"%s\" - %s.\n"),
372 filename
, strerror(errno
));
377 _cupsLangPrintf(stdout
, _("ppdc: Writing %s...\n"), filename
);
381 * Write the PPD file...
384 ppdcArray
*templocales
= locales
;
388 templocales
= new ppdcArray();
389 for (ppdcCatalog
*tempcatalog
= (ppdcCatalog
*)src
->po_files
->first();
391 tempcatalog
= (ppdcCatalog
*)src
->po_files
->next())
393 tempcatalog
->locale
->retain();
394 templocales
->add(tempcatalog
->locale
);
398 if (d
->write_ppd_file(fp
, catalog
, templocales
, src
, le
))
404 if (templocales
!= locales
)
405 templocales
->release();
413 // Delete the printer driver information...
416 // Message catalog...
420 // Return with no errors.
426 // 'usage()' - Show usage and exit.
432 _cupsLangPuts(stdout
,
433 _("Usage: ppdc [options] filename.drv [ ... filenameN.drv ]\n"
435 " -D name=value Set named variable to value.\n"
436 " -I include-dir Add include directory to search "
438 " -c catalog.po Load the specified message catalog.\n"
439 " -d output-dir Specify the output directory.\n"
440 " -l lang[,lang,...] Specify the output language(s) "
442 " -m Use the ModelName value as the "
444 " -t Test PPDs instead of generating "
446 " -v Be verbose (more v's for more "
448 " -z Compress PPD files using GNU zip.\n"
449 " --cr End lines with CR (Mac OS 9).\n"
450 " --crlf End lines with CR + LF (Windows).\n"
451 " --lf End lines with LF (UNIX/Linux/Mac "