]>
git.ipfire.org Git - thirdparty/cups.git/blob - ppdc/ppdmerge.cxx
8c4863e11f2625513caeea022e0903e0a76954b6
4 // PPD file merge utility 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 <cups/cups-private.h>
21 #include <cups/ppd-private.h>
22 #include <cups/array.h>
29 static const char *ppd_locale(ppd_file_t
*ppd
);
30 static void usage(void);
34 // 'main()' - Main entry for the PPD merge utility.
37 int // O - Exit status
38 main(int argc
, // I - Number of command-line arguments
39 char *argv
[]) // I - Command-line arguments
42 char *opt
; // Current option
43 ppd_file_t
*ppd
; // PPD file
44 cups_array_t
*ppds
; // Array of PPD files
45 const char *inname
, // First input filename
46 *outname
; // Output filename (if any)
47 cups_file_t
*infile
, // Input file
48 *outfile
; // Output file
49 cups_array_t
*languages
; // Languages in file
50 const char *locale
; // Current locale
51 char line
[1024]; // Line from file
56 // Scan the command-line...
61 ppds
= cupsArrayNew(NULL
, NULL
);
63 for (i
= 1; i
< argc
; i
++)
64 if (argv
[i
][0] == '-')
66 for (opt
= argv
[i
] + 1; *opt
; opt
++)
69 case 'o' : // Output file
87 // Open and load the PPD file...
88 if ((infile
= cupsFileOpen(argv
[i
], "r")) == NULL
)
90 _cupsLangPrintf(stderr
, _("%s: Unable to open %s: %s"), "ppdmerge",
91 argv
[i
], strerror(errno
));
95 // Open the PPD file...
96 if ((ppd
= ppdOpen2(infile
)) == NULL
)
98 ppd_status_t status
; // PPD open status
99 int curline
, // Current line
100 linenum
; // Line number
103 status
= ppdLastError(&linenum
);
105 _cupsLangPrintf(stderr
,
106 _("%s: Unable to open PPD file: %s on line %d."),
107 "ppdmerge", ppdErrorString(status
), linenum
);
108 cupsFileRewind(infile
);
113 while (cupsFileGets(infile
, line
, sizeof(line
)))
116 if (curline
>= linenum
)
120 _cupsLangPrintf(stderr
, "%d: %s", linenum
, line
);
122 cupsFileClose(infile
);
126 // Figure out the locale...
127 if ((locale
= ppd_locale(ppd
)) == NULL
)
129 _cupsLangPrintf(stderr
,
130 _("ppdmerge: Bad LanguageVersion \"%s\" in %s."),
131 ppd
->lang_version
, argv
[i
]);
132 cupsFileClose(infile
);
137 if (!strcmp(locale
, "en") && !inname
&& !outfile
)
139 // Set the English PPD's filename...
141 languages
= _ppdGetLanguages(ppd
);
143 if (outname
&& !strcmp(inname
, outname
))
145 // Rename input filename so that we don't overwrite it...
146 char bckname
[1024]; // Backup filename
149 snprintf(bckname
, sizeof(bckname
), "%s.bck", inname
);
151 if (rename(inname
, bckname
))
153 _cupsLangPrintf(stderr
,
154 _("ppdmerge: Unable to backup %s to %s - %s"),
155 inname
, bckname
, strerror(errno
));
162 else if (strcmp(locale
, "en"))
164 // Save this PPD for later processing...
165 cupsArrayAdd(ppds
, ppd
);
169 // Don't need this PPD...
170 _cupsLangPrintf(stderr
, _("ppdmerge: Ignoring PPD file %s."),
175 // Close and move on...
176 cupsFileClose(infile
);
179 // If no PPDs have been loaded, display the program usage message.
183 // Loop through the PPD files we loaded to generate a new language list...
185 languages
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
187 for (ppd
= (ppd_file_t
*)cupsArrayFirst(ppds
);
189 ppd
= (ppd_file_t
*)cupsArrayNext(ppds
))
191 locale
= ppd_locale(ppd
);
193 if (cupsArrayFind(languages
, (void *)locale
))
195 // Already have this language, remove the PPD from the list.
197 cupsArrayRemove(ppds
, ppd
);
200 cupsArrayAdd(languages
, (void *)locale
);
203 // Copy the English PPD starting with a cupsLanguages line...
204 infile
= cupsFileOpen(inname
, "r");
208 const char *ext
= strrchr(outname
, '.');
209 if (ext
&& !strcmp(ext
, ".gz"))
210 outfile
= cupsFileOpen(outname
, "w9");
212 outfile
= cupsFileOpen(outname
, "w");
215 outfile
= cupsFileStdout();
217 cupsFileGets(infile
, line
, sizeof(line
));
218 cupsFilePrintf(outfile
, "%s\n", line
);
219 if ((locale
= (char *)cupsArrayFirst(languages
)) != NULL
)
221 cupsFilePrintf(outfile
, "*cupsLanguages: \"%s", locale
);
222 while ((locale
= (char *)cupsArrayNext(languages
)) != NULL
)
223 cupsFilePrintf(outfile
, " %s", locale
);
224 cupsFilePuts(outfile
, "\"\n");
227 while (cupsFileGets(infile
, line
, sizeof(line
)))
229 if (strncmp(line
, "*cupsLanguages:", 15))
230 cupsFilePrintf(outfile
, "%s\n", line
);
233 // Loop through the other PPD files we loaded to provide the translations...
234 for (ppd
= (ppd_file_t
*)cupsArrayFirst(ppds
);
236 ppd
= (ppd_file_t
*)cupsArrayNext(ppds
))
238 // Output all of the UI text for this language...
239 int j
, k
, l
; // Looping vars
240 ppd_group_t
*g
; // Option group
241 ppd_option_t
*o
; // Option
242 ppd_choice_t
*c
; // Choice
243 ppd_coption_t
*co
; // Custom option
244 ppd_cparam_t
*cp
; // Custom parameter
245 ppd_attr_t
*attr
; // PPD attribute
247 locale
= ppd_locale(ppd
);
249 cupsFilePrintf(outfile
, "*%% %s localization\n", ppd
->lang_version
);
250 cupsFilePrintf(outfile
, "*%s.Translation ModelName/%s: \"\"\n", locale
,
253 for (j
= ppd
->num_groups
, g
= ppd
->groups
; j
> 0; j
--, g
++)
255 cupsFilePrintf(outfile
, "*%s.Translation %s/%s: \"\"\n", locale
,
258 for (k
= g
->num_options
, o
= g
->options
; k
> 0; k
--, o
++)
260 cupsFilePrintf(outfile
, "*%s.Translation %s/%s: \"\"\n", locale
,
261 o
->keyword
, o
->text
);
263 for (l
= o
->num_choices
, c
= o
->choices
; l
> 0; l
--, c
++)
264 cupsFilePrintf(outfile
, "*%s.%s %s/%s: \"\"\n", locale
,
265 o
->keyword
, c
->choice
, c
->text
);
267 if ((co
= ppdFindCustomOption(ppd
, o
->keyword
)) != NULL
)
269 snprintf(line
, sizeof(line
), "Custom%s", o
->keyword
);
270 attr
= ppdFindAttr(ppd
, line
, "True");
271 cupsFilePrintf(outfile
, "*%s.Custom%s True/%s: \"\"\n", locale
,
272 o
->keyword
, attr
->text
);
273 for (cp
= ppdFirstCustomParam(co
); cp
; cp
= ppdNextCustomParam(co
))
274 cupsFilePrintf(outfile
, "*%s.ParamCustom%s %s/%s: \"\"\n", locale
,
275 o
->keyword
, cp
->name
, cp
->text
);
283 cupsArrayDelete(ppds
);
285 cupsFileClose(outfile
);
287 // Return with no errors.
293 // 'ppd_locale()' - Return the locale associated with a PPD file.
296 static const char * // O - Locale string
297 ppd_locale(ppd_file_t
*ppd
) // I - PPD file
299 int i
; // Looping var
300 size_t vlen
; // Length of LanguageVersion string
301 static char locale
[255]; // Locale string
302 static struct // LanguageVersion translation table
304 const char *version
, // LanguageVersion string */
305 *language
; // Language code */
317 { "hungarian", "hu" },
319 { "japanese", "ja" },
321 { "norwegian", "no" },
323 { "portuguese", "pt" },
325 { "simplified chinese", "zh_CN" },
329 { "traditional chinese", "zh_TW" },
334 for (i
= 0; i
< (int)(sizeof(languages
) / sizeof(languages
[0])); i
++)
336 vlen
= strlen(languages
[i
].version
);
338 if (!_cups_strncasecmp(ppd
->lang_version
, languages
[i
].version
, vlen
))
340 if (ppd
->lang_version
[vlen
] == '-' ||
341 ppd
->lang_version
[vlen
] == '_')
342 snprintf(locale
, sizeof(locale
), "%s_%s", languages
[i
].language
,
343 ppd
->lang_version
+ vlen
+ 1);
345 strlcpy(locale
, languages
[i
].language
, sizeof(locale
));
355 // 'usage()' - Show usage and exit.
361 _cupsLangPuts(stdout
, _("Usage: ppdmerge [options] filename.ppd [ ... "
363 _cupsLangPuts(stdout
, _("Options:"));
364 _cupsLangPuts(stdout
, _(" -o filename.ppd[.gz] Set output file "
365 "(otherwise stdout)."));