]>
git.ipfire.org Git - thirdparty/cups.git/blob - ppdc/ppdmerge.cxx
4 // PPD file merge utility 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 merge utility.
18 // ppd_locale() - Return the locale associated with a PPD file.
19 // usage() - Show usage and exit.
23 // Include necessary headers...
26 #include <cups/cups-private.h>
27 #include <cups/ppd-private.h>
28 #include <cups/array.h>
35 static const char *ppd_locale(ppd_file_t
*ppd
);
36 static void usage(void);
40 // 'main()' - Main entry for the PPD merge utility.
43 int // O - Exit status
44 main(int argc
, // I - Number of command-line arguments
45 char *argv
[]) // I - Command-line arguments
48 char *opt
; // Current option
49 ppd_file_t
*ppd
; // PPD file
50 cups_array_t
*ppds
; // Array of PPD files
51 const char *inname
, // First input filename
52 *outname
; // Output filename (if any)
53 cups_file_t
*infile
, // Input file
54 *outfile
; // Output file
55 cups_array_t
*languages
; // Languages in file
56 const char *locale
; // Current locale
57 char line
[1024]; // Line from file
62 // Scan the command-line...
67 ppds
= cupsArrayNew(NULL
, NULL
);
69 for (i
= 1; i
< argc
; i
++)
70 if (argv
[i
][0] == '-')
72 for (opt
= argv
[i
] + 1; *opt
; opt
++)
75 case 'o' : // Output file
93 // Open and load the PPD file...
94 if ((infile
= cupsFileOpen(argv
[i
], "r")) == NULL
)
96 _cupsLangPrintf(stderr
, _("%s: Unable to open %s: %s\n"), "ppdmerge",
97 argv
[i
], strerror(errno
));
101 // Open the PPD file...
102 if ((ppd
= ppdOpen2(infile
)) == NULL
)
104 ppd_status_t status
; // PPD open status
105 int linenum
; // Line number
108 status
= ppdLastError(&linenum
);
110 _cupsLangPrintf(stderr
, _("%s: Unable to open PPD file: %s on line %d.\n"),
111 "ppdmerge", ppdErrorString(status
), linenum
);
113 _cupsLangPrintf(stderr
, "%d: ", linenum
);
114 cupsFileRewind(infile
);
118 while (cupsFileGets(infile
, line
, sizeof(line
)))
125 _cupsLangPrintf(stderr
, "%s\n", line
);
127 cupsFileClose(infile
);
131 // Figure out the locale...
132 if ((locale
= ppd_locale(ppd
)) == NULL
)
134 _cupsLangPrintf(stderr
,
135 _("ppdmerge: Bad LanguageVersion \"%s\" in %s\n"),
136 ppd
->lang_version
, argv
[i
]);
137 cupsFileClose(infile
);
142 if (!strcmp(locale
, "en") && !inname
&& !outfile
)
144 // Set the English PPD's filename...
146 languages
= _ppdGetLanguages(ppd
);
148 if (outname
&& !strcmp(inname
, outname
))
150 // Rename input filename so that we don't overwrite it...
151 char bckname
[1024]; // Backup filename
154 snprintf(bckname
, sizeof(bckname
), "%s.bck", inname
);
156 if (rename(inname
, bckname
))
158 _cupsLangPrintf(stderr
,
159 _("ppdmerge: Unable to backup %s to %s- %s\n"),
160 inname
, bckname
, strerror(errno
));
167 else if (strcmp(locale
, "en"))
169 // Save this PPD for later processing...
170 cupsArrayAdd(ppds
, ppd
);
174 // Don't need this PPD...
175 _cupsLangPrintf(stderr
, _("ppdmerge: Ignoring PPD file %s...\n"),
180 // Close and move on...
181 cupsFileClose(infile
);
184 // If no PPDs have been loaded, display the program usage message.
188 // Loop through the PPD files we loaded to generate a new language list...
190 languages
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
192 for (ppd
= (ppd_file_t
*)cupsArrayFirst(ppds
);
194 ppd
= (ppd_file_t
*)cupsArrayNext(ppds
))
196 locale
= ppd_locale(ppd
);
198 if (cupsArrayFind(languages
, (void *)locale
))
200 // Already have this language, remove the PPD from the list.
202 cupsArrayRemove(ppds
, ppd
);
205 cupsArrayAdd(languages
, (void *)locale
);
208 // Copy the English PPD starting with a cupsLanguages line...
209 infile
= cupsFileOpen(inname
, "r");
213 const char *ext
= strrchr(outname
, '.');
214 if (ext
&& !strcmp(ext
, ".gz"))
215 outfile
= cupsFileOpen(outname
, "w9");
217 outfile
= cupsFileOpen(outname
, "w");
220 outfile
= cupsFileStdout();
222 cupsFileGets(infile
, line
, sizeof(line
));
223 cupsFilePrintf(outfile
, "%s\n", line
);
224 if ((locale
= (char *)cupsArrayFirst(languages
)) != NULL
)
226 cupsFilePrintf(outfile
, "*cupsLanguages: \"%s", locale
);
227 while ((locale
= (char *)cupsArrayNext(languages
)) != NULL
)
228 cupsFilePrintf(outfile
, " %s", locale
);
229 cupsFilePuts(outfile
, "\"\n");
232 while (cupsFileGets(infile
, line
, sizeof(line
)))
234 if (strncmp(line
, "*cupsLanguages:", 15))
235 cupsFilePrintf(outfile
, "%s\n", line
);
238 // Loop through the other PPD files we loaded to provide the translations...
239 for (ppd
= (ppd_file_t
*)cupsArrayFirst(ppds
);
241 ppd
= (ppd_file_t
*)cupsArrayNext(ppds
))
243 // Output all of the UI text for this language...
244 int j
, k
, l
; // Looping vars
245 ppd_group_t
*g
; // Option group
246 ppd_option_t
*o
; // Option
247 ppd_choice_t
*c
; // Choice
248 ppd_coption_t
*co
; // Custom option
249 ppd_cparam_t
*cp
; // Custom parameter
250 ppd_attr_t
*attr
; // PPD attribute
252 locale
= ppd_locale(ppd
);
254 cupsFilePrintf(outfile
, "*%% %s localization\n", ppd
->lang_version
);
255 cupsFilePrintf(outfile
, "*%s.Translation ModelName/%s: \"\"\n", locale
,
258 for (j
= ppd
->num_groups
, g
= ppd
->groups
; j
> 0; j
--, g
++)
260 cupsFilePrintf(outfile
, "*%s.Translation %s/%s: \"\"\n", locale
,
263 for (k
= g
->num_options
, o
= g
->options
; k
> 0; k
--, o
++)
265 cupsFilePrintf(outfile
, "*%s.Translation %s/%s: \"\"\n", locale
,
266 o
->keyword
, o
->text
);
268 for (l
= o
->num_choices
, c
= o
->choices
; l
> 0; l
--, c
++)
269 cupsFilePrintf(outfile
, "*%s.%s %s/%s: \"\"\n", locale
,
270 o
->keyword
, c
->choice
, c
->text
);
272 if ((co
= ppdFindCustomOption(ppd
, o
->keyword
)) != NULL
)
274 snprintf(line
, sizeof(line
), "Custom%s", o
->keyword
);
275 attr
= ppdFindAttr(ppd
, line
, "True");
276 cupsFilePrintf(outfile
, "*%s.Custom%s True/%s: \"\"\n", locale
,
277 o
->keyword
, attr
->text
);
278 for (cp
= ppdFirstCustomParam(co
); cp
; cp
= ppdNextCustomParam(co
))
279 cupsFilePrintf(outfile
, "*%s.ParamCustom%s %s/%s: \"\"\n", locale
,
280 o
->keyword
, cp
->name
, cp
->text
);
288 cupsArrayDelete(ppds
);
290 cupsFileClose(outfile
);
292 // Return with no errors.
298 // 'ppd_locale()' - Return the locale associated with a PPD file.
301 static const char * // O - Locale string
302 ppd_locale(ppd_file_t
*ppd
) // I - PPD file
304 int i
, // Looping var
305 vlen
; // Length of LanguageVersion string
306 static char locale
[255]; // Locale string
307 static struct // LanguageVersion translation table
309 const char *version
, // LanguageVersion string */
310 *language
; // Language code */
322 { "hungarian", "hu" },
324 { "japanese", "ja" },
326 { "norwegian", "no" },
328 { "portuguese", "pt" },
330 { "simplified chinese", "zh_CN" },
334 { "traditional chinese", "zh_TW" },
339 for (i
= 0; i
< (int)(sizeof(languages
) / sizeof(languages
[0])); i
++)
341 vlen
= strlen(languages
[i
].version
);
343 if (!strncasecmp(ppd
->lang_version
, languages
[i
].version
, vlen
))
345 if (ppd
->lang_version
[vlen
] == '-' ||
346 ppd
->lang_version
[vlen
] == '_')
347 snprintf(locale
, sizeof(locale
), "%s_%s", languages
[i
].language
,
348 ppd
->lang_version
+ vlen
+ 1);
350 strlcpy(locale
, languages
[i
].language
, sizeof(locale
));
360 // 'usage()' - Show usage and exit.
366 _cupsLangPuts(stdout
,
367 _("Usage: ppdmerge [options] filename.ppd "
368 "[ ... filenameN.ppd ]\n"
370 " -o filename.ppd[.gz]\n"));