forked from webmin/webmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreebsd-lib.pl
executable file
·291 lines (269 loc) · 7.26 KB
/
freebsd-lib.pl
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
# freebsd-lib.pl
# Functions for printcap-style printer management
# list_printers()
# Returns a list of known printer names
sub list_printers
{
local($l, @rv);
foreach $l (&list_printcap()) {
$l->{'name'} =~ /^([^\|]+)/;
push(@rv, $1);
}
return @rv;
}
# get_printer(name, [nostatus])
sub get_printer
{
local($l, %prn, @w, @n, $w, %cap, @jobs);
foreach $l (&list_printcap()) {
@n = split(/\|/, $l->{'name'});
if ($n[0] eq $_[0]) {
# found the printer.. get info from printcap
$prn{'name'} = $n[0];
if (@n > 2) { $prn{'alias'} = [ @n[1..$#n-1] ]; }
if (@n > 1) { $prn{'desc'} = $n[$#n]; }
$prn{'iface'} = $l->{'if'};
$prn{'banner'} = !defined($l->{'sh'});
$prn{'dev'} = $l->{'lp'};
$prn{'rhost'} = $l->{'rm'};
$prn{'rqueue'} = $l->{'rp'};
$prn{'msize'} = $l->{'mx'};
$prn{'ro'} = $l->{'file'} eq $config{'ro_printcap_file'};
if (!$_[1]) {
# call lpc to get status
local $esc = quotemeta($prn{'name'});
$out = &backquote_command("lpc status $esc 2>&1", 1);
$prn{'accepting'} = ($out =~ /queuing is enabled/);
$prn{'enabled'} = ($out =~ /printing is enabled/);
}
return \%prn;
}
}
return undef;
}
sub get_jobs
{
local @jobs;
local $esc = quotemeta($_[0]);
open(LPQ, "lpq -P$esc |");
while(<LPQ>) {
chop;
if (/^Rank\s+Owner\s+/) { $doneheader++; }
elsif ($doneheader && /^(\S+)\s+(\S+)\s+(\d+)\s+(.*\S)\s+(\d+)\s+(\S+)$/) {
local(%job);
$job{'id'} = $3;
$job{'user'} = $2;
$job{'size'} = $5;
$job{'file'} = $4;
$job{'printing'} = ($1 eq "active");
push(@jobs, \%job);
}
}
close(LPQ);
return @jobs;
}
sub printer_support
{
return $_[0] !~ /^(why|allow|default|ctype|riface|sysv|direct|rnoqueue|ipp)$/;
}
# create_printer(&details)
# Create a new printer in /etc/printcap
sub create_printer
{
local(%cap);
$cap{'sd'} = "$config{'spool_dir'}/$_[0]->{'name'}";
&lock_file($cap{'sd'});
mkdir($cap{'sd'}, 0755);
&unlock_file($cap{'sd'});
&lock_file($config{'printcap_file'});
local $lref = &read_file_lines($config{'printcap_file'});
push(@$lref, &make_printcap($_[0], \%cap));
&flush_file_lines($config{'printcap_file'});
&unlock_file($config{'printcap_file'});
&apply_status($_[0]);
}
# modify_printer(&details)
sub modify_printer
{
local(@old, $o, $old, @cap);
&lock_file($config{'printcap_file'});
@old = &list_printcap();
foreach $o (@old) {
$o->{'name'} =~ /^([^\|]+)/;
if ($1 eq $_[0]->{'name'}) {
# found current details
$old = $o;
last;
}
}
if (!$old) { &error("Printer '$_[0]->{'name'}' no longer exists"); }
local $lref = &read_file_lines($config{'printcap_file'});
splice(@$lref, $old->{'line'}, $old->{'eline'} - $old->{'line'} + 1,
&make_printcap($_[0], $old));
&flush_file_lines($config{'printcap_file'});
&unlock_file($config{'printcap_file'});
&apply_status($_[0]);
}
# delete_printer(name)
sub delete_printer
{
local(@old, $o, $old, @cap);
&lock_file($config{'printcap_file'});
@old = &list_printcap();
foreach $o (@old) {
$o->{'name'} =~ /^([^\|]+)/;
if ($1 eq $_[0]) {
# found current details
$old = $o;
last;
}
}
if (!$old) { &error("Printer '$_[0]' no longer exists"); }
local $lref = &read_file_lines($config{'printcap_file'});
splice(@$lref, $old->{'line'}, $old->{'eline'} - $old->{'line'} + 1);
&flush_file_lines($config{'printcap_file'});
&unlock_file($config{'printcap_file'});
}
# cancel_job(printer, job)
# Calls lprm to remove some job
sub cancel_job
{
local($out);
local $esc = quotemeta($_[0]);
local $iesc = quotemeta($_[1]);
$out = &backquote_logged("lprm -P$esc $iesc 2>&1");
if ($?) { &error("lprm failed : $out"); }
}
# make_printcap(&details, &old)
# Updates or creates a printcap line
sub make_printcap
{
local(%prn, %cap, $a, $rv, $c);
%prn = %{$_[0]}; %cap = %{$_[1]};
$cap{'if'} = $prn{'iface'} ? $prn{'iface'} : undef;
$cap{'sh'} = $prn{'banner'} ? undef : "";
$cap{'lp'} = $prn{'dev'} ? $prn{'dev'} : undef;
$cap{'rm'} = $prn{'rhost'} ? $prn{'rhost'} : undef;
$cap{'rp'} = $prn{'rqueue'} ? $prn{'rqueue'} : undef;
$cap{'mx'} = defined($prn{'msize'}) ? $prn{'msize'} : undef;
$rv = $prn{'name'};
foreach $a (@{$prn{'alias'}}) { $rv .= "|$a"; }
$rv .= "|$prn{'desc'}";
foreach $c (keys %cap) {
if (length($c) == 2 && defined($cap{$c})) {
if ($cap{$c} eq "") { $rv .= ":$c"; }
elsif ($cap{$c} =~ /^\d+$/) { $rv .= ":$c#$cap{$c}"; }
else { $rv .= ":$c=$cap{$c}"; }
}
}
$rv .= ":";
return $rv;
}
# list_printcap()
# Returns an array of associative arrays containing printcap fields
sub list_printcap
{
return @list_printcap_cache if (scalar(@list_printcap_cache));
local(@rv, @line, $line, $cont, $lnum, $i, %done, $capfile);
foreach $capfile ($config{'printcap_file'}, $config{'ro_printcap_file'}) {
next if (!$capfile || $done{$capfile}++);
open(CAP, "<".$capfile);
$lnum = 0;
while($line = <CAP>) {
$line =~ s/^#.*$//g; # remove comments
$line =~ s/\s+$//g; # remove trailing spaces
$line =~ s/^\s+//g; # remove leading spaces
if ($line =~ /\S/) {
$ncont = ($line =~ s/\\$//g);
if ($cont) {
$line[$#line] .= $line;
$eline[@line - 1] = $lnum;
}
else {
push(@line, $line);
$eline[@line - 1] = $sline[@line - 1] = $lnum;
}
$cont = $ncont;
}
$lnum++;
}
close(CAP);
for($i=0; $i<@line; $i++) {
local(%cap);
@w = split(/:+/, $line[$i]);
$cap{'name'} = $w[0];
$cap{'line'} = $sline[$i];
$cap{'eline'} = $eline[$i];
$cap{'file'} = $capfile;
foreach $w (@w[1..$#w]) {
if ($w =~ /^([A-z0-9]+)[=#](.*)$/) { $cap{$1} = $2; }
elsif ($w =~ /^([A-z0-9]+)$/) { $cap{$w} = ""; }
}
push(@rv, \%cap);
}
}
@list_printcap_cache = @rv;
return @rv;
}
# apply_status(&details)
# Calls lpc to enable or disable a printer.
# Restarting lpd doesn't seem to be necessary?
sub apply_status
{
local($out);
local $esc = quotemeta($_[0]->{'name'});
$out = &backquote_command("lpc status $esc 2>&1", 1);
if ($_[0]->{'enabled'} && $out !~ /printing is enabled/)
{ &backquote_logged("lpc up $esc"); }
elsif (!$_[0]->{'enabled'} && $out =~ /printing is enabled/)
{ &backquote_logged("lpc down $esc"); }
if ($_[0]->{'accepting'} && $out !~ /queuing is enabled/)
{ &backquote_logged("lpc enable $esc"); }
elsif (!$_[0]->{'accepting'} && $out =~ /queuing is enabled/)
{ &backquote_logged("lpc disable $esc"); }
}
# sched_running()
# Returns the pid if lpsched is running, 0 if not, -1 if cannot be stopped
sub sched_running
{
@pid = &find_byname("lpd");
if (@pid) { return $pid[0]; }
return 0;
}
# start_sched()
# Start lpsched
sub start_sched
{
local $out = &backquote_logged("lpd 2>&1");
if ($?) { &error("failed to start lpd : <tt>$out</tt>"); }
}
# stop_sched(pid)
# Stop the running lpsched process
sub stop_sched
{
local @pid = &find_byname("lpd");
&kill_logged('TERM', @pid) || &error("Failed to stop lpd : $!");
}
# print_command(printer, file)
# Returns the command to print some file on some printer
sub print_command
{
local $esc = quotemeta($_[0]);
local $fesc = quotemeta($_[1]);
return "lpr -P$esc $fesc";
}
# check_print_system()
sub check_print_system
{
&has_command("lpr") || return &text('freebsd_ecmd', "<tt>lpr</tt>");
-d $config{'spool_dir'} || return &text('freebsd_espool', "<tt>$config{'spool_dir'}</tt>");
return undef;
}
if ($gconfig{'os_type'} eq 'openbsd') {
@device_files = ("/dev/lpt0", "/dev/cua00", "/dev/cua01");
}
else {
@device_files = ("/dev/lpt0", "/dev/cuaa0", "/dev/cuaa1");
}
@device_names = ($text{'freebsd_paralel'}, &text('freebsd_serial', "1"),
&text('freebsd_serial', "2"));