summaryrefslogtreecommitdiff
path: root/ppdc/ppdc-import.cxx
blob: d41cbb982204afe3b06c8d5eb06900ca11452da0 (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
//
// "$Id: ppdc-import.cxx 3275 2011-05-20 07:26:13Z msweet $"
//
//   PPD file import methods for the CUPS PPD Compiler.
//
//   Copyright 2007-2011 by Apple Inc.
//   Copyright 2002-2006 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/".
//
// Contents:
//
//   ppdcSource::import_ppd() - Import a PPD file.
//   ppd_gets()               - Get a line from a PPD file.
//

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

#include "ppdc-private.h"
#include <cups/ppd.h>


//
// 'ppdcSource::import_ppd()' - Import a PPD file.
//

int					// O - 1 on success, 0 on failure
ppdcSource::import_ppd(const char *f)	// I - Filename
{
  int		i, j, k;		// Looping vars
  cups_file_t	*fp;			// File
  char		line[256],		// Comment line
		*ptr;			// Pointer into line
  int		cost;			// Cost for filter
  ppd_file_t	*ppd;			// PPD file data
  ppd_group_t	*group;			// PPD group
  ppd_option_t	*option;		// PPD option
  ppd_choice_t	*choice;		// PPD choice
  ppd_attr_t	*attr;			// PPD attribute
  ppd_const_t	*constraint;		// PPD UI constraint
  ppd_const_t	*constraint2;		// Temp PPD UI constraint
  ppd_size_t	*size;			// PPD page size
  ppdcDriver	*driver;		// Driver
  ppdcFilter	*filter;		// Current filter
  ppdcFont	*font;			// Font
  ppdcGroup	*cgroup;		// UI group
  ppdcOption	*coption;		// UI option
  ppdcChoice	*cchoice;		// UI choice
  ppdcConstraint *cconstraint;		// UI constraint
  ppdcMediaSize	*csize;			// Media size


  // Try opening the PPD file...
  if ((ppd = ppdOpenFile(f)) == NULL)
    return (0);

  // All PPD files need a PCFileName attribute...
  if (!ppd->pcfilename)
  {
    ppdClose(ppd);
    return (0);
  }

  // See if the driver has already been imported...
  if ((driver = find_driver(ppd->pcfilename)) == NULL)
  {
    // Create a new PPD file...
    if ((fp = cupsFileOpen(f, "r")) == NULL)
    {
      ppdClose(ppd);
      return (0);
    }

    driver       = new ppdcDriver();
    driver->type = PPDC_DRIVER_PS;

    drivers->add(driver);

    // Read the initial comments from the PPD file and use them as the
    // copyright/license text...
    cupsFileGets(fp, line, sizeof(line));
					// Skip *PPD-Adobe-M.m

    while (cupsFileGets(fp, line, sizeof(line)))
      if (strncmp(line, "*%", 2))
        break;
      else if (strncmp(line, "*%%%% ", 6))
      {
        for (ptr = line + 2; isspace(*ptr); ptr ++);

        driver->add_copyright(ptr);
      }

    cupsFileClose(fp);

    // Then add the stuff from the PPD file...
    if (ppd->modelname && ppd->manufacturer &&
        !_cups_strncasecmp(ppd->modelname, ppd->manufacturer,
                     strlen(ppd->manufacturer)))
    {
      ptr = ppd->modelname + strlen(ppd->manufacturer);

      while (isspace(*ptr))
        ptr ++;
    }
    else
      ptr = ppd->modelname;

    if (ppd->nickname)
      driver->add_attr(new ppdcAttr("NickName", NULL, NULL, ppd->nickname));

    if (ppd->shortnickname)
      driver->add_attr(new ppdcAttr("ShortNickName", NULL, NULL,
                                    ppd->shortnickname));

    driver->manufacturer        = new ppdcString(ppd->manufacturer);
    driver->model_name          = new ppdcString(ptr);
    driver->pc_file_name        = new ppdcString(ppd->pcfilename);
    attr = ppdFindAttr(ppd, "FileVersion", NULL);
    driver->version             = new ppdcString(attr ? attr->value : NULL);
    driver->model_number        = ppd->model_number;
    driver->manual_copies       = ppd->manual_copies;
    driver->color_device        = ppd->color_device;
    driver->throughput          = ppd->throughput;
    driver->variable_paper_size = ppd->variable_sizes;
    driver->max_width           = ppd->custom_max[0];
    driver->max_length          = ppd->custom_max[1];
    driver->min_width           = ppd->custom_min[0];
    driver->min_length          = ppd->custom_min[1];
    driver->left_margin         = ppd->custom_margins[0];
    driver->bottom_margin       = ppd->custom_margins[1];
    driver->right_margin        = ppd->custom_margins[2];
    driver->top_margin          = ppd->custom_margins[3];

    for (i = 0; i < ppd->num_filters; i ++)
    {
      strlcpy(line, ppd->filters[i], sizeof(line));

      for (ptr = line; *ptr; ptr ++)
        if (isspace(*ptr & 255))
	  break;
      *ptr++ = '\0';

      cost = strtol(ptr, &ptr, 10);

      while (isspace(*ptr & 255))
        ptr ++;

      filter = new ppdcFilter(line, ptr, cost);
      driver->add_filter(filter);
    }

    attr = ppdFindAttr(ppd, "DefaultFont", NULL);
    driver->default_font  = new ppdcString(attr ? attr->value : NULL);

    // Collect media sizes...
    ppd_option_t	*region_option,		// PageRegion option
			*size_option;		// PageSize option
    ppd_choice_t	*region_choice,		// PageRegion choice
			*size_choice;		// PageSize choice

    region_option = ppdFindOption(ppd, "PageRegion");
    size_option   = ppdFindOption(ppd, "PageSize");

    for (i = ppd->num_sizes, size = ppd->sizes; i > 0; i --, size ++)
    {
      // Don't do custom size here...
      if (!_cups_strcasecmp(size->name, "Custom"))
        continue;

      // Get the code for the PageSize and PageRegion options...
      region_choice = ppdFindChoice(region_option, size->name);
      size_choice   = ppdFindChoice(size_option, size->name);

      // Create a new media size record and add it to the driver...
      csize = new ppdcMediaSize(size->name, size_choice->text, size->width,
                                size->length, size->left, size->bottom,
				size->width - size->right,
				size->length - size->top,
				size_choice->code, region_choice->code);

       driver->add_size(csize);

       if (!_cups_strcasecmp(size_option->defchoice, size->name))
         driver->set_default_size(csize);
    }

    // Now all of the options...
    for (i = ppd->num_groups, group = ppd->groups; i > 0; i --, group ++)
    {
      cgroup = new ppdcGroup(group->name, group->text);
      driver->add_group(cgroup);

      for (j = group->num_options, option = group->options; j > 0; j --, option ++)
      {
        if (!strcmp(option->keyword, "PageSize") || !strcmp(option->keyword, "PageRegion"))
          continue;

        coption = new ppdcOption((ppdcOptType)option->ui, option->keyword,
	                         option->text, (ppdcOptSection)option->section,
				 option->order);
        cgroup->add_option(coption);

        for (k = option->num_choices, choice = option->choices; k > 0; k --, choice ++)
        {
	  if (!strcmp(choice->choice, "Custom"))
	    continue;

          cchoice = new ppdcChoice(choice->choice, choice->text, choice->code);
          coption->add_choice(cchoice);

          if (!_cups_strcasecmp(option->defchoice, choice->choice))
            coption->set_defchoice(cchoice);
        }
      }
    }

    // Now the constraints...
    for (i = ppd->num_consts, constraint = ppd->consts;
         i > 0;
	 i --, constraint ++)
    {
      // Look for mirrored constraints...
      for (j = i - 1, constraint2 = constraint + 1;
           j > 0;
	   j --, constraint2 ++)
	if (!strcmp(constraint->option1, constraint2->option2) &&
	    !strcmp(constraint->choice1, constraint2->choice2) &&
	    !strcmp(constraint->option2, constraint2->option1) &&
	    !strcmp(constraint->choice2, constraint2->choice1))
          break;

      if (j)
        continue;

      cconstraint = new ppdcConstraint(constraint->option2, constraint->choice2,
                                       constraint->option1, constraint->choice1);
      driver->add_constraint(cconstraint);
    }

    for (i = 0; i < ppd->num_attrs; i ++)
    {
      attr = ppd->attrs[i];

      if (!strcmp(attr->name, "Font"))
      {
        // Font...
	char		encoding[256],	// Encoding string
			version[256],	// Version string
			charset[256],	// Charset string
			status[256];	// Status string
	ppdcFontStatus	fstatus;	// Status enumeration


        if (sscanf(attr->value, "%s%*[^\"]\"%[^\"]\"%s%s", encoding, version,
	           charset, status) != 4)
	{
	  _cupsLangPrintf(stderr, _("ppdc: Bad font attribute: %s"),
	                  attr->value);
	  continue;
	}

        if (!strcmp(status, "ROM"))
	  fstatus = PPDC_FONT_ROM;
	else
	  fstatus = PPDC_FONT_DISK;

        font = new ppdcFont(attr->spec, encoding, version, charset, fstatus);

	driver->add_font(font);
      }
      else if (!strcmp(attr->name, "CustomPageSize"))
      {
        driver->set_custom_size_code(attr->value);
      }
      else if ((strncmp(attr->name, "Default", 7) ||
        	!strcmp(attr->name, "DefaultColorSpace")) &&
	       strcmp(attr->name, "ColorDevice") &&
	       strcmp(attr->name, "Manufacturer") &&
	       strcmp(attr->name, "ModelName") &&
	       strcmp(attr->name, "MaxMediaHeight") &&
	       strcmp(attr->name, "MaxMediaWidth") &&
	       strcmp(attr->name, "NickName") &&
	       strcmp(attr->name, "ParamCustomPageSize") &&
	       strcmp(attr->name, "ShortNickName") &&
	       strcmp(attr->name, "Throughput") &&
	       strcmp(attr->name, "PCFileName") &&
	       strcmp(attr->name, "FileVersion") &&
	       strcmp(attr->name, "FormatVersion") &&
	       strcmp(attr->name, "HWMargins") &&
	       strcmp(attr->name, "VariablePaperSize") &&
	       strcmp(attr->name, "LanguageEncoding") &&
	       strcmp(attr->name, "LanguageVersion") &&
	       strcmp(attr->name, "cupsFilter") &&
	       strcmp(attr->name, "cupsFlipDuplex") &&
	       strcmp(attr->name, "cupsLanguages") &&
	       strcmp(attr->name, "cupsManualCopies") &&
	       strcmp(attr->name, "cupsModelNumber") &&
	       strcmp(attr->name, "cupsVersion"))
      {
        if ((ptr = strchr(attr->name, '.')) != NULL &&
	    ((ptr - attr->name) == 2 || (ptr - attr->name) == 5))
	{
	  // Might be a localization attribute; test further...
	  if (isalpha(attr->name[0] & 255) &&
	      isalpha(attr->name[1] & 255) &&
	      (attr->name[2] == '.' ||
	       (attr->name[2] == '_' && isalpha(attr->name[3] & 255) &&
	        isalpha(attr->name[4] & 255))))
            continue;
	}

        // Attribute...
        driver->add_attr(new ppdcAttr(attr->name, attr->spec, attr->text,
	                              attr->value));
      }
      else if (!strncmp(attr->name, "Default", 7) &&
               !ppdFindOption(ppd, attr->name + 7) &&
	       strcmp(attr->name, "DefaultFont") &&
	       strcmp(attr->name, "DefaultImageableArea") &&
	       strcmp(attr->name, "DefaultPaperDimension") &&
	       strcmp(attr->name, "DefaultFont"))
      {
        // Default attribute...
        driver->add_attr(new ppdcAttr(attr->name, attr->spec, attr->text,
	                              attr->value));
      }
    }
  }

  return (1);
}


//
// End of "$Id: ppdc-import.cxx 3275 2011-05-20 07:26:13Z msweet $".
//