File: prtmgmt

package info (click to toggle)
apsfilter 7.2.6-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,356 kB
  • sloc: sh: 452; ansic: 236; perl: 175; makefile: 124
file content (270 lines) | stat: -rwxr-xr-x 6,939 bytes parent folder | download | duplicates (9)
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
#! /usr/bin/perl
#
# Copyright (c) 2000 Andreas Klemm <andreas@apsfilter.org>. Neuss.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $ApsCVS: src/apsfilter/tools/prtmgmt,v 1.5 2000/12/10 13:54:23 andreas Exp $

use diagnostics;

# read variables
sub variables {

	$ENV{PATH} = "/bin:/usr/bin:/usr/local/bin";	# set sane PATH

	$config = "./prtmgmt.conf";	# config file for prtmgmt
	$printcap = "/etc/printcap";	# location of printcap

	$debug = 0;			# turn on debugging
	$verbose = 0;			# list printer aliases as well

	$printer = "";			# printer name
	$palias1 = "";			# printer alias 1
	$palias2 = "";			# printer alias 2

	$lp = "";			# lp - device name for output
	$rm = "";			# rm - machine name for remote printer
	$rp = "";			# rp - remote printer name

	$if = "";			# if - name of input filter script
	$sdbase = "";			# spooldir base
	$sd = "";			# sd - spool directory
	$lf = "";			# lf - error logging file name
	$af = "";			# af - name of accounting file

	$mx = "mx#";			# mx - maximum file size (BUFSIZ blocks)
	$sh = "sh";			# sh - suppress printing of header page
}

# read configuration
sub read_config {

	print "DEBUG: in read_config\n" if $debug;
	print "DEBUG: verbose = $verbose\n" if $debug;

	print "Reading $config...\n" if $verbose;
	open (C, $config) or &init_config;

	while(<C>) {
		print "debug: $_" if $debug;
		# strip CR
		chop;
		# ignore comments ...
		next if /^\s*$/;
        	next if /^\s*#/;
		# XXXXX
		print $_;
	}
	close C;
}

# create empty config_file
sub init_config {
	print "Creating config file...\n" if $verbose;

	open(C, "> $config") or die "$config: $!\n";
	print C <<EOF;
#
# $config - automatically generated by prtmgmt(8)
#

EOF
	close C;
}

# write config_file
sub write_config {
	print "Saving config file...\n" if $verbose;
	rename($config, "$config.bak");

	open(C, "> $config") or die "$config: $!\n";
	print C <<EOF;
#
# $config - automatically generated by prtmgmt(8)
#

EOF
	close C;
}

sub parse_arguments {
	local(@argv) = @_;
	local($usage);
	local($list_printer);

	&usage if $#argv < 0;

	while ($_ = $argv[0], /^-/) {
	    shift @argv;
	    last if /^--$/;
	    if    (/^--?(D|debug)$/) { $debug = 1 }
	    elsif (/^--?(h|help|\?)$/) { $usage = 1 }
	    elsif (/^--?(v|verbose|\?)$/) { $verbose = 1 }
	    elsif (/^--?(a|add|\?)$/) {
		$add_printer = 1; $printer = $argv[0]; shift @argv;
	    }
	    elsif (/^--?(d|\?)$/) {
		$deactivate_printer = 1; $printer = $argv[0]; shift @argv;
	    }
	    elsif (/^--?(l|list|\?)$/) { $list_printer = 1 }
	    elsif (/^--?(m|modify|\?)$/) {
		$modify_printer = 1; $printer = $argv[0]; shift @argv;
	    }
	    elsif (/^--?(r|remove|\?)$/) {
		$remove_printer = 1; $printer = $argv[0]; shift @argv;
	    }
	    elsif (/^--?(pa1|palias1|\?)$/) { $palias1 = $argv[0]; shift @argv }
	    elsif (/^--?(pa2|palias2|\?)$/) { $palias2 = $argv[0]; shift @argv }
	    else {
		print ("error: unknown commandline option\n");
		&usage;
	    }
	}

	# call functions
	&usage			if $usage;
	&add_printer		if $add_printer;
	&deactivate_printer	if $deactivate_printer;
	&list_printer		if $list_printer;
	&modify_printer		if $modify_printer;
	&remove_printer		if $remove_printer;
}

sub usage {
	warn <<USAGE;
usage: prtmgmt

    [-a|-add]     add printer         [-D|debug]    set debugging mode
    [-d]          deactivate printer  [-h|-help]    this help
    [-l|-list]    list printer        [-v|-verbose] increase verbosity
    [-m|-modify]  modify printer
    [-r|-remove]  remove printer      [-i|-init]    initialize some defaults

    for add/delete/modify operation:
    [-p|-printer]      printer name
    [-pa1|-palias1]    printer alias 1
    [-pa2|-palias2]    printer alias 2

printcap=$printcap sdbase=$sdbase sd=$sd
lf=$lf af=$af if=$if
lp=$lp rm=$rm rp=$rp
mx=$mx sh=$sh
USAGE
	exit 1;
}

# add printer in $printcap
sub add_printer {
	print "not yet implemented\n";
	exit 1;
}

# deactivate printer in $printcap
sub deactivate_printer {
	print "not yet implemented\n";
	exit 1;
}

# list name of printers in $printcap
sub list_printer {
	local($pname, $palias1, $palias2);

	print "DEBUG: in list_printer\n" if $debug;
	print "DEBUG: verbose = $verbose\n" if $debug;

	open(P, $printcap) || die "$printcap: $!\n";

	while (<P>) {
		print "debug: $_" if $debug;
		# strip CR
		chop;
		# ignore comments ...
		next if /^\s*$/;
        	next if /^\s*#/;
 		# ... and continued printer configuration lines
        	next if /^\s*:/;
		# read printer name
		($pname,$palias1,$palias2) = (split(/\|/, $_));
		($palias1) = (split(/:/, $palias1));
		($palias2) = (split(/:/, $palias2));
		if ($verbose) {
			print "$pname:$palias1:$palias2\n"
		} else {
			print "$pname\n";
		}
	}
	close P;
}

# modify printer in $printcap
sub modify_printer {
	print "not yet implemented\n";
	exit 1;
}

# remove printer from $printcap
sub remove_printer {
	print "not yet implemented\n";
	exit 1;
}

sub confirm_yn {
	local($message, $confirm) = @_;
	local($yes) = '^(yes|YES|y|Y)$';
	local($no) = '^(no|NO|n|N)$';
	local($read);

	if ($confirm && ($confirm =~ "$yes" || $confirm == 1)) {
		$confirm = "y";
	} else {
		$confirm = "n";
	}
	print "$message (y/n) [$confirm]: ";
	chop($read = <STDIN>);
	$read =~ s/^\s*//;
	$read =~ s/\s*$//;
	return 1 unless $read;

	if (($confirm eq "y" && $read =~ "$yes") ||
	    ($confirm eq "n" && $read =~ "$no")) {
		return 1;
	}

	if ($read !~ "$yes" && $read !~ "$no") {
		warn "Wrong value. Enter again!\a\n";
		return &confirm_yn($message, $confirm);
	}
	return 0;
}

################
# main
#

&variables;				# initialize variables
&read_config;				# read configuration
&parse_arguments(@ARGV);		# parse arguments
&write_config;				# write configuration

exit 0