File: magicfilterconfig

package info (click to toggle)
magicfilter 1.2-63
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,280 kB
  • ctags: 142
  • sloc: ansic: 1,292; makefile: 289; perl: 220; sh: 193
file content (305 lines) | stat: -rw-r--r-- 7,664 bytes parent folder | download | duplicates (6)
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
#!/usr/bin/perl -w
# -*- cperl -*-

# Public Domain.
# David Frey

use strict;
no strict "vars";

my $printcap="/etc/printcap";
my $filterdir="/etc/magicfilter";
my $force=0;
my $m=0;

if ($#ARGV >= 0) {
  $force=($ARGV[0] eq "--force"); if ($force) { $m++; }
}

if ($m <= $#ARGV) { $printcap=$ARGV[$m]; }

sub input()
{
  my($message)=$_[0];
  my($defanswer)=$_[1];
  my($reply);

  printf "%-45s [%-15s]: ",$message,$defanswer;
  $reply=<STDIN>; chop($reply); if ($reply eq "") { $reply=$defanswer; }

  return $reply;
}

sub yninput()
{
  my($message)=$_[0];
  my($defanswer)=$_[1];
  my($reply);

  printf "%s? (y/n) [%s]: ",$message,$defanswer;
  $reply=<STDIN>; chop($reply); if ($reply eq "") { $reply=$defanswer; }

  return $reply;
}

sub readfilters()
{
  my($i);

  if ( ! -e $filterdir ) {
    print<<EOT;

The magicfilter package is wrongly installed; can\'t find $filterdir.

EOT

    die "'$filterdir' missing, stopped";
  }

  open(FILTERS, "ls $filterdir/*-filter|sort|") || die "Can't find $filters.";
  $i=0;
  while (<FILTERS>) {
    chop;
    s/$filterdir\/(.*)-filter/$1/g;
    $filters[$i]=$_; $i++;
  }
  close(FILTERS);

  foreach $entry (@filters) { $entry =~ s/-filter//g; }
  return sort(@filters);
}

@filters=&readfilters;

$ok="no"; $m=-1; $d=0;
while ($ok !~ /y(es)?/) {
  print<<EOT;

You will have to supply the following items for each printer to be installed
(example values in parentheses):

 - its short name and its full name            (hpjlet4, HP LaserJet 4)
 - the device where the printer is attached    (/dev/lp0)
 - and the input filter you want to use        (ljet4l)

EOT

  $p=-1;
  do {
    $p++; $m++; $full[$m+1]='done';

    if ($p == 0) { $full[$p]='HP Laserjet 4L'; }
    else         { $full[$p]='done'; }

    $short[$p]='hplj4l';
    $dev[$p]=sprintf("/dev/lp%d",$p+1); $ifilter[$p]='ljet4l';
    $lpflags[$p]=""; $remflags[$p]=""; $serflags[$p]="";

    printf "Printer #%d:\n", $p+1;
    printf "------------\n";

    $full[$p]=&input("Full name, `done' when done", $full[$p]);

    if ($full[$p] !~ /done\s*/) {
      $short[$p]=&input("Short name, name of the spool directory",$short[$p]);
      $dev[$p]  =&input("Full device path (or remote machine name)",
			$dev[$p]);

      if ($#filters == 0) {
        print "There aren't any filters."; $full[$p]='done';
      } else {
	print<<EOT;

The following filters are available in /etc/magicfilter.

You can read the comments at the top of each file to help decide which
filter would best suit your needs.

The filters concentrate on the printer language. If you know, that your
printer uses a given printer language (e.g. ESC/P2 or PCL5) and your
printer is not listed below, you can use another filter which uses same
language.

Read the file `/usr/share/doc/gs-gpl/Devices.htm' from the GhostScript package
and have your printer manual handy if you want to change a filter.

EOT
	$l=0;
	foreach $filter (@filters) {
	  $l += (length($filter)+1);
	  if ($l > 72-length($filter)) { print "\n"; $l=0; }
	  print "$filter ";
	}
        print "\n\n";

        $ifilter[$p]=&input("The input filter to be used", $ifilter[$p]);

        if ($dev[$p] !~ /\/dev\//) {
	  $remflags[$p]=":rm=$dev[$p]:rp=$short[$p]:lpr_bounce"; $dev[$p]="";
	} elsif ($dev[$p] =~ /\/dev\/ttyS/) {
	  $serflags[$p]=":bf=2400:fc=0:fs=1";
	} else {
          $lpflags[$p]="lp=$dev[$p]:";
        }

        $spooldir[$p]="/var/spool/lpd/$short[$p]";

        print "\n";
      }
    }
  } while ($full[$p] !~ /done\s*/);

  if ($p > 0) {
    if ($p == 1) { $d=0; }
    do {
      format top=

                       Printer configuration

Default printer (alias `lp') is marked with an asterisk.

Printer name  Short name Spool dir            Device     Input filter
-------------------------------------------------------------------------------
.

    format STDOUT=
@<<<<<<<<<<<< @<<<<<<<<< @<<<<<<<<<<<<<<<<<<< @<<<<<<<<< @<<<<<<<<<<<<<<<<<<@<<
$full,        $short,    $spooldir,           $dev,      $ifilter,          $a
.

      for($i=0;$i<$p;$i++) {
        $full=$full[$i]; $short=$short[$i];
        $spooldir=$spooldir[$i]; $dev=$dev[$i]; $ifilter=$ifilter[$i];
	$a= ($remflags[$i] ne "") ? "R" : "";
        $a= $a . (($i == $d) ? "*" : "");

        write;
      }
      $ans="n"; $-=0;

      print "\n";
      if ($p > 1) {
        $ans=&yninput("Do you want to change the default printer","n");
        if ($ans =~ /y(es)?/) {
          $def=&input("New default printer (type its short name)","");
          $d=0;
          while (($d < $p) && ($short[$d] !~ /$def/)) { $d++; }
          if ($d == $p) { $d = 0; }
	  $short[$d]=$def;
        }
      }
    } while ($ans =~ /y(es)?/);
    $ok=&yninput("Is this ok","y");
  } else {
    $ok="yes";
  }
}

print "\n";
if ($p > 0) {
  if ( -x "/usr/bin/paperconf" ) {
    open(PAPERSIZE, "/usr/bin/paperconf -N|");
    $papersize=<PAPERSIZE>; chop($papersize);
    close(PAPERSIZE);
  }

  $pagelength=66;
  if ($papersize =~ /A4/) { $pagelength=72; }
  print "Paper size is $papersize, page length is $pagelength lines.\n";

  system("/etc/init.d/lpd stop  || /etc/init.d/lprng stop");

  my $printcaporig="$printcap.orig";
  if ( -r "$printcaporig" ) {
    unlink($printcaporig);
  }
  if ( -r "$printcap" ) {
    rename $printcap,$printcaporig;

    print "Renamed original $printcap to $printcaporig.\n";
  }

  open(PRINTCAP,">$printcap") || die "Can't write $printcap: $!";
  print PRINTCAP <<EOH;
#
# Copyright (c) 1983 Regents of the University of California.
# All rights reserved.
#
# Redistribution and use in source and binary forms are permitted
# provided that this notice is preserved and that due credit is given
# to the University of California at Berkeley. The name of the University
# may not be used to endorse or promote products derived from this
# software without specific prior written permission. This software
# is provided ``as is'' without express or implied warranty.
#
#	@(#)etc.printcap	5.2 (Berkeley) 5/5/88
#
# This file was generated by $0.
#
EOH

  $short[$d]="lp|" . $short[$d];
  for($i=0;$i<$p;$i++) {
    print PRINTCAP <<EOE;
$short[$i]|$full[$i]:\\
\t:$lpflags[$i]sd=$spooldir[$i]$remflags[$i]$serflags[$i]:\\
\t:sh:pw\#80:pl\#${pagelength}:px\#1440:mx\#0:\\
\t:if=$filterdir/$ifilter[$i]-filter:\\
\t:af=/var/log/lp-acct:lf=/var/log/lp-errs:
EOE
  }
  close(PRINTCAP);
  chmod 0644,$printcap;

  for($i=0;$i<$p;$i++) {
    $lpgrp=(getpwnam("lp"))[2];
    if ( ! -e $spooldir[$i] ) {
      umask 002; mkdir($spooldir[$i],02775); chown(0,$lpgrp,$spooldir[$i]);
    }
  }

  print<<EOT;
$printcap generated.

You\'ll probably want to finetune your newly created $printcap file.
Read the printcap(5), lpr(1), lpq(1), lprm(1) and lpc(1) manual pages
and the PRINTING-HOWTO before doing this.

EOT
  if (grep(/Stylus/, @ifilter)) {
    print<<EOT;
The file README-StylusColor explains the fine points about the Epson
Stylii Color family of printers.

EOT
  }
  if ($#remflags != $p) {
    print<<EOT;
Some printcap entries are for local printers. I therefore suggest to check
the suggests line in order to have all the necessary programs installed for
filtering (this is printer dependant, e.g. PostScript printers typically need
enscript to convert ASCII text to PostScript).

EOT
  }

  if (grep(/Deskjet/, @ifilter)) {
    print<<EOT;
Please install the djtools package.
If your deskjet does not work with your supplied driver try the pcl3 driver
or vice versa.

EOT
  }

  system("/etc/init.d/lpd start || /etc/init.d/lprng start");
}

my $display=$ENV{"DISPLAY"} || "";

if ( $display ne "" ) {
  $dummy="";
  print "Press <ENTER> to continue."; $dummy=<STDIN>;
}

exit 0;