This file is indexed.

/usr/share/perl5/Foomatic/PPD.pm is in foomatic-db-engine 4.0.8-2ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package Foomatic::PPD;

use Foomatic::UIElem;

my @Sections = qw(JCLBegin 
		  JCLSetup
		  JCLToPSInterpreter
		  ExitServer
		  Prolog
		  DocumentSetup
		  PageSetup);

sub new {
    my ($type, $filename, $poid) = @_;

    open PPD, "$filename" or die;

    my %ppd;

    $ppd{'filename'} = $filename;
    my ($opt, $optname);
    my ($choice,$choiceverb,$snippet);
    my $l;
    for $l (<PPD>) {
	
	# skip comments
	next if ($l =~ m!^*%!);
	
	# skip blank lines
	next if ($l =~ m!^\s*$!);
	
	if (defined($snippet)) {
	    $snippet = "$snippet$l";
	    if ($snippet =~ s!\"\s*$!!) {
		$opt->add_option($choice, $choiceverb, $snippet);
		$snippet = undef;
	    }
	} elsif (defined($opt)) {      
	    # In mid-parse of a UI option clause
	    if ($l =~ m!^\*CloseUI: \*$optname!) {
		# close up the $opt object
		push(@{$ppd{'options'}}, $opt);
		$optname = $opt = undef;
	    } elsif ($l =~ m!^\*OrderDependency: ([\.\d]+) (AnySetup|JCLSetup|PageSetup|DocumentSetup|Prolog|ExitServer) \*$optname!) {
		my ($order, $section) = ($1, $2);
		$opt->set('order_real'=>$order);
		$opt->set('order_section'=>$section);
		$opt->set('order_keyword'=>$optname);
	    } elsif ($l =~ m!^\*Default$optname:\s+([0-9\&A-Za-z]+)!) {
		$opt->set('default'=>$1);
	    } elsif ($l =~ m!^\*$optname ([\d\&A-Za-z]+)(\/([^:]+))?:\s*\"([^\"]*)(\")?!) {
		if ($5 eq '"') {
		    $opt->add_option($1,$3?$3:$1,$4);
		} else {
		    $snippet = $4;
		    $choice = $1;
		    $choiceverb = $3?$3:$1;
		}
	    }
	    
	} else {
	    # not in UI clause parsing
	    if ($l =~ m!^\*NickName:\s*\"(.+)\"!) {
		$ppd{'NickName'} = $1;
	    } elsif ($l =~ m!^\*ModelName:\s*\"(.+)\"!) {
		$ppd{'ModelName'} = $1;
	    } elsif ($l =~ m!^\*Manufacturer:\s*\"(.+)\"!) {
		$ppd{'Manufacturer'} = $1;
	    } elsif ($l =~ m!^\*OpenUI \*([0-9\&A-Za-z]+)(\/(.+))?:\s*(Boolean|PickOne|PickMany)!) {
		my ($name, $label, $type) = ($1, $3, $4);
		# make new $opt object
		$optname = $name;
		$opt = new Foomatic::UIElem ('name'=>$optname,
					     'type'=>$type,
					     'label'=>($label?$label:$name));
	    } 
	    
	    # yadda...
	    
	} 

    }
    
    close PPD;

    my $this = bless \%ppd;
    $this->sort_options();
    $this->{'printer_id'} = $poid;

    return $this;
}

sub sort_options {
    my $this = $_[0];

    # First, sort all the options into sections
    my %sections;
    my $o;
    while (defined($o=pop(@{$this->{'options'}}))) {
	my $sec = $o->{'order_section'};
	push(@{$sections{($sec ? $sec : 'Queries')}}, $o);
    }
    $this->{'options'} = undef;

    # Put AnySetup stuff in DocumentSetup (or could be PageSetup)
    push(@{$sections{'DocumentSetup'}}, @{$sections{'AnySetup'}});
    @{$sections{'AnySetup'}} = ();

    # Now sort each section by orderdep number
    my $k;
    for $k (keys(%sections)) {
	my @sorted = sort {$a->{'order_real'} 
			   <=> $b->{'order_real'}}   @{$sections{$k}};
	$sections{$k} = \@sorted;
    }

    $this->{'sections'} = \%sections;
}


sub pdq_options {
    my $this = $_[0];

    my @opts;

    my $k;
    for $k (@Sections) {
	my $o;
	for $o (@{$this->{'sections'}->{$k}}) {
	    my ($name, $label, $type, $default) = 
		($o->{'name'},$o->{'label'},$o->{'type'},$o->{'default'});

	    if ($type eq 'PickOne' 
		or $type eq 'Boolean'
		or $type eq 'PickMany') {
		push(@opts,
		     "  option {\n",
		     "    var = \"$name\"\n",
		     "    desc = \"$label\"\n",
		     "    default_choice \"$default\"\n");

		my $c;
		for $c (@{$o->{'options'}}) {
		    my ($label, $option) = ($c->{'label'}, $c->{'option'});
		    push(@opts,
			 "    choice \"$name:$option\" {\n",
			 "      value = \"$option\"\n",
			 "      desc = \"$label\"\n",
			 "    }\n");
		}

		push(@opts,
		     "  }\n");
	    }

	    $optnum++;
	}
    }

    return @opts;
}

sub _tag {
    my ($t, @v) = @_;

    return '' if !defined(@v);

    if (0) {
	$v =~ s!\&!\&amp\;!g;
	$v =~ s!\<!\&lt\;!g;
    }

    return "<$t>" . join('',@v) . "</$t>\n";
}

sub foo_options {
    my ($this) = (@_);

    # We build a list of option xml objects
    my @options;

    # All of them need to get sandwiched between
    # %!PS
    # statusdict begin
    #   opt1
    #   opt2
    #   ....
    # end

    # For each section in the order defined by Adobe, and for each
    # option in the order defined by the PPD, we emit shell code to
    # generate the proper snippet-o-postscript into the backend
    # filter's output.

    # TODO: some sections go in the middle of the document!  E-gad!
    # TODO: JCL incantations - these aren't even parsed from the PPD yet.

    my $prn = $this->{'printer_id'};
    my $filename = $this->{'filename'};
    
    my $k;
    # AnySetup has already been moved to DocumentSetup.
    # We don't do the JCL stuff yet.
    for $k (qw(Prolog DocumentSetup PageSetup)) {
	my $o;
	my $optidx = 0;
	for $o (@{$this->{'sections'}->{$k}}) {

	    my (@opt) = ();

	    # We only do PickOne and Boolean (no PickMany)
	    if ($o->{'type'} ne 'PickOne'
		and $o->{'type'} ne 'Boolean') {
		print STDERR ("Skipping option ", $o->{'name'}, 
			      " because it is a ", $o->{'type'}, "\n");
		next;
	    }

	    # Skip "PageRegion", it is the same as "PageSize"
	    next if ($o->{'name'} eq "PageRegion");

	    my ($var,$order) = @$o{'name','order_real'};

	    # Find index of default option
	    my $c;
	    my $scanindex = 1;
	    my $defaultindex = 1; # assume first
	    for $c (@{$o->{'options'}}) {
		my $v = $c->{'option'};
		
		if ($o->{'default'} eq $v) {
		    $defaultindex = $scanindex;
		    last;
		}
		
		$scanindex++;
	    }


	    push (@opt, ("<option type='enum' id='ppd-$prn-$var'>\n",
			 "<!-- option from section $k in $filename -->\n",
			 _tag('arg_longname',
			      _tag('en', xml_esc($o->{'label'}))),
			 _tag('arg_shortname',
			      _tag('en', xml_esc($var))),
			 _tag('arg_execution',
			      _tag('arg_order', 500 + $order),
			      _tag('arg_spot', 'A'),
			      "<arg_postscript section='$k' />\n",
			      _tag('arg_proto', '%s')),
			 _tag('constraints',
			      ("<constraint sense='true'>\n",
			       _tag('driver', 'ppd'),
			       _tag('printer', "printer/$prn"),
			       _tag('arg_defval', "ppd-$prn-$var-$defaultindex"),
			       "</constraint>\n"))));
	    
	    my $choiceidx=0;
	    my @evals;
	    for $c (@{$o->{'options'}}) {
		my $v = $c->{'option'};
		my $snippet = $c->{'snippet'};
		$choiceidx++;
		
		if (defined($snippet)) {
		    
		    push (@evals,
			  ("<enum_val id='ppd-$prn-$var-$choiceidx'>\n",
			   _tag('ev_longname', 
				_tag('en', xml_esc($c->{'label'}))),
			   _tag('ev_shortname',
				_tag('en', xml_esc($c->{'option'}))),
			   ($snippet ? _tag('ev_driverval', xml_esc($snippet))
			    : "<ev_driverval></ev_driverval>\n"),
			   "</enum_val>\n"));
		    
		    # TODO: We should also handle <##> hex numbers in snippets!
		}
	    }

	    # If there are choices, put them in
	    if (scalar(@evals)) {
		push(@opt,
		     _tag('enum_vals', join('',@evals)));
	    
		push (@opt, "</option>\n");

		# Note that we skip the whole thing if there are no choices!
		push (@options, { 'id' => "ppd-$prn-$var",
				  'xml' => \@opt } );
	    }
	}
    }
    
    return @options;

}	

sub xml_esc {
    my ($in) = (@_);
    
    @chars = split(//,$in);
    $ascii = "";
    foreach (@chars) {
        if (ord ($_) > 127) { $_="?"; }
        $ascii .= $_;
    }
    $ascii =~ s!&!&amp;!g;
    $ascii =~ s!<!&lt;!g;
    $ascii =~ s!>!&gt;!g;

    return $ascii;
}

sub pdq_filter {
    my ($this) = $_[0];

    my @filt;

    push(@filt,
	 "  filter_exec {\n\n",
	 "    echo '%!PS' > \$OUTPUT\n",
	 "    echo 'statusdict begin' >> \$OUTPUT\n\n");


    # For each section in the order defined by Adobe, and for each
    # option in the order defined by the PPD, we emit shell code to
    # generate the proper snippet-o-postscript into the PDQ filter's
    # output.
    #
    # TODO: some sections go in the middle of the document!  E-gad!
    # TODO: JCL incantations - these aren't even parsed from the PPD yet.

    my $k;
    for $k (@Sections) {
	my $o;
	for $o (@{$this->{'sections'}->{$k}}) {
	    my ($var,$order) = @$o{'name','order_real'};
	    push (@filt,
		  "    # We put option $var in section $k order numer $order\n");
	    my $c;
	    my $first = 1;
	    for $c (@{$o->{'options'}}) {
		my $el = ($first ? '' : 'el');
		my $v = $c->{'option'};
		my $snippet = $c->{'snippet'};

		if ($snippet) {
		    my @sniplines = split("\n", $snippet);
		    push(@filt, 
			 "    ${el}if [ \"\$$var\" = \"$v\" ]; then\n");
		    for (@sniplines) {
			next if /^\s*$/;
			$_ =~ s!\'!\\\'!g; # escape single quotes
			# TODO: We should also handle <##> hex numbers!
			push(@filt,
			     "      echo \'$_\' >> \$OUTPUT;\n");
		    }

		    $first = 0;
		}
	    }
	    push (@filt, "    fi\n\n");
	}
    }

    push (@filt,
	  "    echo 'end' >> \$OUTPUT\n\n",
	  "    cat \$INPUT >> \$OUTPUT\n",
	  "  }\n");

    return @filt;
}

sub pdq_driver {
    my ($this) = @_;

    my $name = $this->get_name();
    my $driver = $name;

    $driver =~ s!^\s*!!;
    $driver =~ s!\s*$!!;
    $driver =~ s!(\s+)!\-!g;
    $driver = "$driver-0.1";

    my @drv;
    push (@drv,
	  "driver $driver {\n\n",
	  "  help \"This driver was automagically converted from the \n",
	  "        PPD file for the $name by ppdtopdq.\"\n\n",
	  $this->pdq_options(),
	  "\n",
	  "  language_driver ps {\n",
	  "    filetype_regx = \"postscript\"\n",
	  "  }\n\n",
	  $this->pdq_filter(),
	  "}\n");

    return @drv;
}

# get a nice pretty english name for this thing
sub get_name {
    my ($this) = @_;

    my ($mk,$md,$nk) = (@$this{'Manufacturer','ModelName','NickName'});

    my $name;
    if ($mk) { $name = "$mk"; }
    if ($md) { $name = "$name $md"; }
    elsif ($nk) { $name = "$name $nk"; }

    return $name;
}

1;