File: magicfilterconfig

package info (click to toggle)
magicfilter 1.2-24
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 560 kB
  • ctags: 76
  • sloc: ansic: 1,280; perl: 230; makefile: 218; sh: 192
file content (306 lines) | stat: -rw-r--r-- 7,226 bytes parent folder | download
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
#!/usr/bin/perl -w

use strict; no strict "vars";

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

$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 "%-39s [%-20s]: ",$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|") || 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);
}

if ( -e $printcap && !$force) 
{
  print<<EOT;

You already have an $printcap file installed.
I assume that it is already correctly configured and won't overwrite it.
(if $printcap is not correctly configured, delete the file and re-run 
 $0 or run '$0 --force').

EOT
  exit 0;
} 

@filters=&readfilters;

$ok="no"; $m=-1; $d=0;
while ($ok !~ /ye*s*/)
{
  print<<EOT;
  
You will have to supply the following items for each printer to be installed
(example values in parentheses):

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

EOT

  $p=-1;
  do
  {
    $p++; $m++; $full[$m+1]='done';
    $full[$p]='HP Laserjet 4L'; $short[$p]='hplj4l'; $alias[$p]='lj';
    $dev[$p]=sprintf("lp%d",$p+1); $ifilter[$p]='ljet4m';

    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]);
      $alias[$p]=&input("Alias                                  ",$alias[$p]);
      $dev[$p]  =&input("Device (without leading /dev/)         ",$dev[$p]);

      if ($#filters == 0)
      {
        print "There aren't any filters."; $full[$p]='done';
      }
      else
      {
        print "\nThe following filters are available:\n\n";
	$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 ($ifilter[$p] =~ "dj*") {
          print "\n\nATTENTION: If you don't have the djtools ";
          print "installed, install them,\nor replace the last line of";
          print "your $filterdir/$ifilter[$p]-filter with:\n";
          print "default		text\n\n";
        }

        if ($dev[$p] !~ /dev/) { $dev[$p]="/dev/$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 Alias Spool dir            Device   Input filter
------------------------------------------------------------------------------
.

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

      for($i=0;$i<$p;$i++)
      {
        $full=$full[$i]; $short=$short[$i]; $alias=$alias[$i];
        $spooldir=$spooldir[$i]; $dev=$dev[$i]; $ifilter=$ifilter[$i];
        $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 alias)","");
          $d=0;
          while (($d < $p) && ($alias[$d] !~ /$def/)) { $d++; }
          if ($d == $p) { $d = 0; }
        }
      }
    }
    while ($ans eq "y");
    $ok=&yninput("Is this ok","y");
  }
  else
  {
    $ok="yes";
  } 
}

print "\n";
if ($p > 0)
{
  if ( ! -r $papersizefile )
  {
    $papersize=&input("What should the default papersize be?","A4");
    ($papersize) = $papersize =~ /(\w+)\s*/;
    open(PAPERSIZE, ">$papersizefile");
    print PAPERSIZE "$papersize\n";
    close(PAPERSIZE);
  }
  else
  {
    open(PAPERSIZE, "$papersizefile");
    $papersize=<PAPERSIZE>; chop($papersize);
    close(PAPERSIZE);
  }

  $pagelength=66;
  if ($papersize =~ /[Aa]4/) { $pagelength=72; }
  print "Papersize is $papersize, page length is $pagelength lines.\n";

  # Stop running daemons:
  if ( -x "/etc/init.d/lpd"   ) { system("/etc/init.d/lpd stop"); }
  if ( -x "/etc/init.d/lprng" ) { system("/etc/init.d/lprng stop"); }


  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

  $alias[$d]="lp|$alias[$d]";
  for($i=0;$i<$p;$i++)
  {
    print PRINTCAP <<"EOE";
$alias[$i]|$short[$i]|$full[$i]:\\
\t:lp=$dev[$i]:sd=$spooldir[$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);

  for($i=0;$i<$p;$i++)
  {
    $lpgrp=(getpwnam("lp"))[2];
    if ( ! -e $spooldir[$i] )
    {
      $ans=&yninput("Spool directory doesn't exist; create $spooldir[$i]","y");
      if ($ans eq "y") 
      { 
        umask 002; mkdir($spooldir[$i],02775); chown(0,$lpgrp,$spooldir[$i]);
        
      }
    }
  }

  print<<EOT;
$printcap generated.

You'll probable 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 ( -x "/etc/init.d/lpd"   ) { system("/etc/init.d/lpd start"); }
  if ( -x "/etc/init.d/lprng" ) { system("/etc/init.d/lprng start"); }
}
else
{
  print<<EOT;

Nothing to install.
You're going to write your $printcap file yourself.

EOT
}

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

if ( $display ne "" ) {
  $dummy="";
  print "Press a key to continue:"; $dummy=<STDIN>;
}

exit 0;