summaryrefslogtreecommitdiff
path: root/ppdc/ppdmerge.cxx
blob: 397f1c3baa7d08d548eb89e48fba7e516c5c641d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
//
// PPD file merge utility for the CUPS PPD Compiler.
//
// Copyright 2007-2014 by Apple Inc.
// Copyright 2002-2007 by Easy Software Products.
//
// These coded instructions, statements, and computer programs are the
// property of Apple Inc. and are protected by Federal copyright
// law.  Distribution and use rights are outlined in the file "LICENSE.txt"
// which should have been included with this file.  If this file is
// file is missing or damaged, see the license at "http://www.cups.org/".
//

//
// Include necessary headers...
//

#include <cups/cups-private.h>
#include <cups/ppd-private.h>
#include <cups/array.h>


//
// Local functions...
//

static const char	*ppd_locale(ppd_file_t *ppd);
static void		usage(void);


//
// 'main()' - Main entry for the PPD merge utility.
//

int					// O - Exit status
main(int  argc,				// I - Number of command-line arguments
     char *argv[])			// I - Command-line arguments
{
  int		i;			// Looping var
  char		*opt;			// Current option
  ppd_file_t	*ppd;			// PPD file
  cups_array_t	*ppds;			// Array of PPD files
  const char	*inname,		// First input filename
		*outname;		// Output filename (if any)
  cups_file_t	*infile,		// Input file
		*outfile;		// Output file
  cups_array_t	*languages;		// Languages in file
  const char	*locale;		// Current locale
  char		line[1024];		// Line from file


  _cupsSetLocale(argv);

  // Scan the command-line...
  inname    = NULL;
  outname   = NULL;
  outfile   = NULL;
  languages = NULL;
  ppds      = cupsArrayNew(NULL, NULL);

  for (i = 1; i < argc; i ++)
    if (argv[i][0] == '-')
    {
      for (opt = argv[i] + 1; *opt; opt ++)
        switch (*opt)
	{
	  case 'o' :			// Output file
              if (outname)
	        usage();

	      i ++;
	      if (i >= argc)
        	usage();

	      outname = argv[i];
	      break;

	  default :			// Unknown
	      usage();
	      break;
        }
    }
    else
    {
      // Open and load the PPD file...
      if ((infile = cupsFileOpen(argv[i], "r")) == NULL)
      {
        _cupsLangPrintf(stderr, _("%s: Unable to open %s: %s"), "ppdmerge",
	                argv[i], strerror(errno));
	return (1);
      }

      // Open the PPD file...
      if ((ppd = ppdOpen2(infile)) == NULL)
      {
        ppd_status_t	status;		// PPD open status
	int		curline,	// Current line
			linenum;	// Line number


        status = ppdLastError(&linenum);

	_cupsLangPrintf(stderr,
	                _("%s: Unable to open PPD file: %s on line %d."),
	                "ppdmerge", ppdErrorString(status), linenum);
        cupsFileRewind(infile);

        line[0] = '\0';
	curline = 0;

        while (cupsFileGets(infile, line, sizeof(line)))
	{
	  curline ++;
	  if (curline >= linenum)
	    break;
	}

	_cupsLangPrintf(stderr, "%d: %s", linenum, line);

        cupsFileClose(infile);
	return (1);
      }

      // Figure out the locale...
      if ((locale = ppd_locale(ppd)) == NULL)
      {
        _cupsLangPrintf(stderr,
	                _("ppdmerge: Bad LanguageVersion \"%s\" in %s."),
			ppd->lang_version, argv[i]);
        cupsFileClose(infile);
	ppdClose(ppd);
	return (1);
      }

      if (!strcmp(locale, "en") && !inname && !outfile)
      {
        // Set the English PPD's filename...
	inname    = argv[i];
	languages = _ppdGetLanguages(ppd);

        if (outname && !strcmp(inname, outname))
	{
	  // Rename input filename so that we don't overwrite it...
	  char bckname[1024];		// Backup filename


	  snprintf(bckname, sizeof(bckname), "%s.bck", inname);

	  if (rename(inname, bckname))
	  {
	    _cupsLangPrintf(stderr,
	                    _("ppdmerge: Unable to backup %s to %s - %s"),
			    inname, bckname, strerror(errno));
	    return (1);
	  }

	  inname = bckname;
	}
      }
      else if (strcmp(locale, "en"))
      {
	// Save this PPD for later processing...
        cupsArrayAdd(ppds, ppd);
      }
      else
      {
        // Don't need this PPD...
	_cupsLangPrintf(stderr, _("ppdmerge: Ignoring PPD file %s."),
	                argv[i]);
        ppdClose(ppd);
      }

      // Close and move on...
      cupsFileClose(infile);
    }

  // If no PPDs have been loaded, display the program usage message.
  if (!inname)
    usage();

  // Loop through the PPD files we loaded to generate a new language list...
  if (!languages)
    languages = cupsArrayNew((cups_array_func_t)strcmp, NULL);

  for (ppd = (ppd_file_t *)cupsArrayFirst(ppds);
       ppd;
       ppd = (ppd_file_t *)cupsArrayNext(ppds))
  {
    locale = ppd_locale(ppd);

    if (cupsArrayFind(languages, (void *)locale))
    {
      // Already have this language, remove the PPD from the list.
      ppdClose(ppd);
      cupsArrayRemove(ppds, ppd);
    }
    else
      cupsArrayAdd(languages, (void *)locale);
  }

  // Copy the English PPD starting with a cupsLanguages line...
  infile = cupsFileOpen(inname, "r");

  if (outname)
  {
    const char *ext = strrchr(outname, '.');
    if (ext && !strcmp(ext, ".gz"))
      outfile = cupsFileOpen(outname, "w9");
    else
      outfile = cupsFileOpen(outname, "w");
  }
  else
    outfile = cupsFileStdout();

  cupsFileGets(infile, line, sizeof(line));
  cupsFilePrintf(outfile, "%s\n", line);
  if ((locale = (char *)cupsArrayFirst(languages)) != NULL)
  {
    cupsFilePrintf(outfile, "*cupsLanguages: \"%s", locale);
    while ((locale = (char *)cupsArrayNext(languages)) != NULL)
      cupsFilePrintf(outfile, " %s", locale);
    cupsFilePuts(outfile, "\"\n");
  }

  while (cupsFileGets(infile, line, sizeof(line)))
  {
    if (strncmp(line, "*cupsLanguages:", 15))
      cupsFilePrintf(outfile, "%s\n", line);
  }

  // Loop through the other PPD files we loaded to provide the translations...
  for (ppd = (ppd_file_t *)cupsArrayFirst(ppds);
       ppd;
       ppd = (ppd_file_t *)cupsArrayNext(ppds))
  {
    // Output all of the UI text for this language...
    int			j, k, l;	// Looping vars
    ppd_group_t		*g;		// Option group
    ppd_option_t	*o;		// Option
    ppd_choice_t	*c;		// Choice
    ppd_coption_t	*co;		// Custom option
    ppd_cparam_t	*cp;		// Custom parameter
    ppd_attr_t		*attr;		// PPD attribute

    locale = ppd_locale(ppd);

    cupsFilePrintf(outfile, "*%% %s localization\n", ppd->lang_version);
    cupsFilePrintf(outfile, "*%s.Translation ModelName/%s: \"\"\n", locale,
		   ppd->modelname);

    for (j = ppd->num_groups, g = ppd->groups; j > 0; j --, g ++)
    {
      cupsFilePrintf(outfile, "*%s.Translation %s/%s: \"\"\n", locale,
		     g->name, g->text);

      for (k = g->num_options, o = g->options; k > 0; k --, o ++)
      {
	cupsFilePrintf(outfile, "*%s.Translation %s/%s: \"\"\n", locale,
		       o->keyword, o->text);

	for (l = o->num_choices, c = o->choices; l > 0; l --, c ++)
	  cupsFilePrintf(outfile, "*%s.%s %s/%s: \"\"\n", locale,
			 o->keyword, c->choice, c->text);

	if ((co = ppdFindCustomOption(ppd, o->keyword)) != NULL)
	{
	  snprintf(line, sizeof(line), "Custom%s", o->keyword);
	  attr = ppdFindAttr(ppd, line, "True");
	  cupsFilePrintf(outfile, "*%s.Custom%s True/%s: \"\"\n", locale,
			 o->keyword, attr->text);
	  for (cp = ppdFirstCustomParam(co); cp; cp = ppdNextCustomParam(co))
	    cupsFilePrintf(outfile, "*%s.ParamCustom%s %s/%s: \"\"\n", locale,
			   o->keyword, cp->name, cp->text);
	}
      }
    }

    ppdClose(ppd);
  }

  cupsArrayDelete(ppds);

  cupsFileClose(outfile);

  // Return with no errors.
  return (0);
}


//
// 'ppd_locale()' - Return the locale associated with a PPD file.
//

static const char *			// O - Locale string
ppd_locale(ppd_file_t *ppd)		// I - PPD file
{
  int		i;			// Looping var
  size_t	vlen;			// Length of LanguageVersion string
  static char	locale[255];		// Locale string
  static struct				// LanguageVersion translation table
  {
    const char	*version,		// LanguageVersion string */
		*language;		// Language code */
  }		languages[] =
  {
    { "chinese",		"zh" },
    { "czech",			"cs" },
    { "danish",			"da" },
    { "dutch",			"nl" },
    { "english",		"en" },
    { "finnish",		"fi" },
    { "french",			"fr" },
    { "german",			"de" },
    { "greek",			"el" },
    { "hungarian",		"hu" },
    { "italian",		"it" },
    { "japanese",		"ja" },
    { "korean",			"ko" },
    { "norwegian",		"no" },
    { "polish",			"pl" },
    { "portuguese",		"pt" },
    { "russian",		"ru" },
    { "simplified chinese",	"zh_CN" },
    { "slovak",			"sk" },
    { "spanish",		"es" },
    { "swedish",		"sv" },
    { "traditional chinese",	"zh_TW" },
    { "turkish",		"tr" }
  };


  for (i = 0; i < (int)(sizeof(languages) / sizeof(languages[0])); i ++)
  {
    vlen = strlen(languages[i].version);

    if (!_cups_strncasecmp(ppd->lang_version, languages[i].version, vlen))
    {
      if (ppd->lang_version[vlen] == '-' ||
          ppd->lang_version[vlen] == '_')
        snprintf(locale, sizeof(locale), "%s_%s", languages[i].language,
	         ppd->lang_version + vlen + 1);
      else
        strlcpy(locale, languages[i].language, sizeof(locale));

      return (locale);
    }
  }

  return (NULL);
}

//
// 'usage()' - Show usage and exit.
//

static void
usage(void)
{
  _cupsLangPuts(stdout, _("Usage: ppdmerge [options] filename.ppd [ ... "
                          "filenameN.ppd ]"));
  _cupsLangPuts(stdout, _("Options:"));
  _cupsLangPuts(stdout, _("  -o filename.ppd[.gz]    Set output file "
                          "(otherwise stdout)."));

  exit(1);
}