]>
git.ipfire.org Git - thirdparty/cups.git/blob - ppdc/ppdmerge.cxx
397f1c3baa7d08d548eb89e48fba7e516c5c641d
2 // PPD file merge utility for the CUPS PPD Compiler.
4 // Copyright 2007-2014 by Apple Inc.
5 // Copyright 2002-2007 by Easy Software Products.
7 // These coded instructions, statements, and computer programs are the
8 // property of Apple Inc. and are protected by Federal copyright
9 // law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 // which should have been included with this file. If this file is
11 // file is missing or damaged, see the license at "http://www.cups.org/".
15 // Include necessary headers...
18 #include <cups/cups-private.h>
19 #include <cups/ppd-private.h>
20 #include <cups/array.h>
27 static const char *ppd_locale(ppd_file_t
*ppd
);
28 static void usage(void);
32 // 'main()' - Main entry for the PPD merge utility.
35 int // O - Exit status
36 main(int argc
, // I - Number of command-line arguments
37 char *argv
[]) // I - Command-line arguments
40 char *opt
; // Current option
41 ppd_file_t
*ppd
; // PPD file
42 cups_array_t
*ppds
; // Array of PPD files
43 const char *inname
, // First input filename
44 *outname
; // Output filename (if any)
45 cups_file_t
*infile
, // Input file
46 *outfile
; // Output file
47 cups_array_t
*languages
; // Languages in file
48 const char *locale
; // Current locale
49 char line
[1024]; // Line from file
54 // Scan the command-line...
59 ppds
= cupsArrayNew(NULL
, NULL
);
61 for (i
= 1; i
< argc
; i
++)
62 if (argv
[i
][0] == '-')
64 for (opt
= argv
[i
] + 1; *opt
; opt
++)
67 case 'o' : // Output file
85 // Open and load the PPD file...
86 if ((infile
= cupsFileOpen(argv
[i
], "r")) == NULL
)
88 _cupsLangPrintf(stderr
, _("%s: Unable to open %s: %s"), "ppdmerge",
89 argv
[i
], strerror(errno
));
93 // Open the PPD file...
94 if ((ppd
= ppdOpen2(infile
)) == NULL
)
96 ppd_status_t status
; // PPD open status
97 int curline
, // Current line
98 linenum
; // Line number
101 status
= ppdLastError(&linenum
);
103 _cupsLangPrintf(stderr
,
104 _("%s: Unable to open PPD file: %s on line %d."),
105 "ppdmerge", ppdErrorString(status
), linenum
);
106 cupsFileRewind(infile
);
111 while (cupsFileGets(infile
, line
, sizeof(line
)))
114 if (curline
>= linenum
)
118 _cupsLangPrintf(stderr
, "%d: %s", linenum
, line
);
120 cupsFileClose(infile
);
124 // Figure out the locale...
125 if ((locale
= ppd_locale(ppd
)) == NULL
)
127 _cupsLangPrintf(stderr
,
128 _("ppdmerge: Bad LanguageVersion \"%s\" in %s."),
129 ppd
->lang_version
, argv
[i
]);
130 cupsFileClose(infile
);
135 if (!strcmp(locale
, "en") && !inname
&& !outfile
)
137 // Set the English PPD's filename...
139 languages
= _ppdGetLanguages(ppd
);
141 if (outname
&& !strcmp(inname
, outname
))
143 // Rename input filename so that we don't overwrite it...
144 char bckname
[1024]; // Backup filename
147 snprintf(bckname
, sizeof(bckname
), "%s.bck", inname
);
149 if (rename(inname
, bckname
))
151 _cupsLangPrintf(stderr
,
152 _("ppdmerge: Unable to backup %s to %s - %s"),
153 inname
, bckname
, strerror(errno
));
160 else if (strcmp(locale
, "en"))
162 // Save this PPD for later processing...
163 cupsArrayAdd(ppds
, ppd
);
167 // Don't need this PPD...
168 _cupsLangPrintf(stderr
, _("ppdmerge: Ignoring PPD file %s."),
173 // Close and move on...
174 cupsFileClose(infile
);
177 // If no PPDs have been loaded, display the program usage message.
181 // Loop through the PPD files we loaded to generate a new language list...
183 languages
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
185 for (ppd
= (ppd_file_t
*)cupsArrayFirst(ppds
);
187 ppd
= (ppd_file_t
*)cupsArrayNext(ppds
))
189 locale
= ppd_locale(ppd
);
191 if (cupsArrayFind(languages
, (void *)locale
))
193 // Already have this language, remove the PPD from the list.
195 cupsArrayRemove(ppds
, ppd
);
198 cupsArrayAdd(languages
, (void *)locale
);
201 // Copy the English PPD starting with a cupsLanguages line...
202 infile
= cupsFileOpen(inname
, "r");
206 const char *ext
= strrchr(outname
, '.');
207 if (ext
&& !strcmp(ext
, ".gz"))
208 outfile
= cupsFileOpen(outname
, "w9");
210 outfile
= cupsFileOpen(outname
, "w");
213 outfile
= cupsFileStdout();
215 cupsFileGets(infile
, line
, sizeof(line
));
216 cupsFilePrintf(outfile
, "%s\n", line
);
217 if ((locale
= (char *)cupsArrayFirst(languages
)) != NULL
)
219 cupsFilePrintf(outfile
, "*cupsLanguages: \"%s", locale
);
220 while ((locale
= (char *)cupsArrayNext(languages
)) != NULL
)
221 cupsFilePrintf(outfile
, " %s", locale
);
222 cupsFilePuts(outfile
, "\"\n");
225 while (cupsFileGets(infile
, line
, sizeof(line
)))
227 if (strncmp(line
, "*cupsLanguages:", 15))
228 cupsFilePrintf(outfile
, "%s\n", line
);
231 // Loop through the other PPD files we loaded to provide the translations...
232 for (ppd
= (ppd_file_t
*)cupsArrayFirst(ppds
);
234 ppd
= (ppd_file_t
*)cupsArrayNext(ppds
))
236 // Output all of the UI text for this language...
237 int j
, k
, l
; // Looping vars
238 ppd_group_t
*g
; // Option group
239 ppd_option_t
*o
; // Option
240 ppd_choice_t
*c
; // Choice
241 ppd_coption_t
*co
; // Custom option
242 ppd_cparam_t
*cp
; // Custom parameter
243 ppd_attr_t
*attr
; // PPD attribute
245 locale
= ppd_locale(ppd
);
247 cupsFilePrintf(outfile
, "*%% %s localization\n", ppd
->lang_version
);
248 cupsFilePrintf(outfile
, "*%s.Translation ModelName/%s: \"\"\n", locale
,
251 for (j
= ppd
->num_groups
, g
= ppd
->groups
; j
> 0; j
--, g
++)
253 cupsFilePrintf(outfile
, "*%s.Translation %s/%s: \"\"\n", locale
,
256 for (k
= g
->num_options
, o
= g
->options
; k
> 0; k
--, o
++)
258 cupsFilePrintf(outfile
, "*%s.Translation %s/%s: \"\"\n", locale
,
259 o
->keyword
, o
->text
);
261 for (l
= o
->num_choices
, c
= o
->choices
; l
> 0; l
--, c
++)
262 cupsFilePrintf(outfile
, "*%s.%s %s/%s: \"\"\n", locale
,
263 o
->keyword
, c
->choice
, c
->text
);
265 if ((co
= ppdFindCustomOption(ppd
, o
->keyword
)) != NULL
)
267 snprintf(line
, sizeof(line
), "Custom%s", o
->keyword
);
268 attr
= ppdFindAttr(ppd
, line
, "True");
269 cupsFilePrintf(outfile
, "*%s.Custom%s True/%s: \"\"\n", locale
,
270 o
->keyword
, attr
->text
);
271 for (cp
= ppdFirstCustomParam(co
); cp
; cp
= ppdNextCustomParam(co
))
272 cupsFilePrintf(outfile
, "*%s.ParamCustom%s %s/%s: \"\"\n", locale
,
273 o
->keyword
, cp
->name
, cp
->text
);
281 cupsArrayDelete(ppds
);
283 cupsFileClose(outfile
);
285 // Return with no errors.
291 // 'ppd_locale()' - Return the locale associated with a PPD file.
294 static const char * // O - Locale string
295 ppd_locale(ppd_file_t
*ppd
) // I - PPD file
297 int i
; // Looping var
298 size_t vlen
; // Length of LanguageVersion string
299 static char locale
[255]; // Locale string
300 static struct // LanguageVersion translation table
302 const char *version
, // LanguageVersion string */
303 *language
; // Language code */
315 { "hungarian", "hu" },
317 { "japanese", "ja" },
319 { "norwegian", "no" },
321 { "portuguese", "pt" },
323 { "simplified chinese", "zh_CN" },
327 { "traditional chinese", "zh_TW" },
332 for (i
= 0; i
< (int)(sizeof(languages
) / sizeof(languages
[0])); i
++)
334 vlen
= strlen(languages
[i
].version
);
336 if (!_cups_strncasecmp(ppd
->lang_version
, languages
[i
].version
, vlen
))
338 if (ppd
->lang_version
[vlen
] == '-' ||
339 ppd
->lang_version
[vlen
] == '_')
340 snprintf(locale
, sizeof(locale
), "%s_%s", languages
[i
].language
,
341 ppd
->lang_version
+ vlen
+ 1);
343 strlcpy(locale
, languages
[i
].language
, sizeof(locale
));
353 // 'usage()' - Show usage and exit.
359 _cupsLangPuts(stdout
, _("Usage: ppdmerge [options] filename.ppd [ ... "
361 _cupsLangPuts(stdout
, _("Options:"));
362 _cupsLangPuts(stdout
, _(" -o filename.ppd[.gz] Set output file "
363 "(otherwise stdout)."));