Handle hostnames with upper-case letters
[webmin.git] / lpadmin / old-caldera-driver.pl
1 # caldera-driver.pl
2 # Functions for printer drivers as generated by COAS
3
4 %paper_sizes = ( 'a4', 'A4',
5                  'a3', 'A3',
6                  'a5', 'A5',
7                  'letter', 'US Letter',
8                  'legal', 'Legal',
9                  'ledger', 'Ledger' );
10 $driver_dir = "/etc/sysconfig/printers";
11 $base_driver = "$module_root_directory/base_coas_driver";
12 $webmin_windows_driver = 1;
13
14 # is_windows_driver(path)
15 # Returns the server, share, username, password, workgroup, program
16 # if path is a webmin windows driver
17 sub is_windows_driver
18 {
19 return &is_webmin_windows_driver(@_);
20 }
21
22 # is_driver(path)
23 # Returns the driver name and dpi if some path is a webmin driver, or undef
24 sub is_driver
25 {
26 if (!$_[0]) {
27         return { 'mode' => 0,
28                  'desc' => 'None' };
29         }
30 open(DRV, $_[0]);
31 local @lines = <DRV>;
32 close(DRV);
33 if ($lines[1] =~ /^source ($driver_dir\/\S+)/) {
34         # Looks like a caldera driver! Read the sysconfig file
35         local %conf;
36         &read_env_file($1, \%conf);
37         if ($conf{'GSDEVICE'} eq 'NET' || $conf{'GSDEVICE'} eq 'RAW') {
38                 # Driver isn't even used
39                 return { 'mode' => 0,
40                          'desc' => 'None' };
41                 }
42         elsif ($conf{'GSDEVICE'} eq 'uniprint') {
43                 # Uniprint driver
44                 foreach $u (&list_uniprint()) {
45                         $desc = $u->[1] if ($u->[0] eq $conf{'UPP'});
46                         }
47                 $desc =~ s/,.*$//g;
48                 return { 'mode' => 3,
49                          'upp' => $conf{'UPP'},
50                          'paper' => $conf{'PAPERSIZE'},
51                          'double' => lc($conf{'DOUBLEPAGE'}),
52                          'eof' => lc($conf{'SENDEOF'}),
53                          'desc' => $desc ? $desc : $conf{'UPP'} };
54                 }
55         else {
56                 # A caldera printer driver
57                 open(COAS, $config{'coas_printers'});
58                 local $plist = &parse_coas(COAS);
59                 close(COAS);
60                 local ($desc, $p);
61                 foreach $p (values %$plist) {
62                         $desc = $p->{'description'}
63                                 if ($p->{'type'}->{'0'} eq $conf{'GSDEVICE'} &&
64                                     !$desc);
65                         }
66                 return { 'mode' => 1,
67                          'gsdevice' => $conf{'GSDEVICE'},
68                          'gsname' => $conf{'GSNAME'},
69                          'res' => $conf{'RESOLUTION'},
70                          'paper' => $conf{'PAPERSIZE'},
71                          'eof' => lc($conf{'SENDEOF'}),
72                          'double' => lc($conf{'DOUBLEPAGE'}),
73                          'gsopts' => $conf{'GSOPTS'},
74                          'desc' => $conf{'GSNAME'} ? $conf{'GSNAME'} : $desc };
75                 }
76         }
77 else {
78         # A driver of some kind, but not caldera's
79         return { 'mode' => 2,
80                  'file' => $_[0],
81                  'desc' => $_[0] };
82         }
83 }
84
85 # create_windows_driver(&printer, &driver)
86 sub create_windows_driver
87 {
88 return &create_webmin_windows_driver(@_);
89 }
90
91 # create_driver(&printer, &driver)
92 sub create_driver
93 {
94 if ($_[1]->{'mode'} == 0) {
95         return undef;
96         }
97 elsif ($_[1]->{'mode'} == 2) {
98         return $_[1]->{'file'};
99         }
100 else {
101         # Create or update the parameters file
102         local %conf;
103         &read_env_file("$driver_dir/$_[0]->{'name'}", \%conf);
104         $conf{'GSDEVICE'} = $_[1]->{'gsdevice'};
105         $conf{'GSNAME'} = $_[1]->{'gsname'};
106         $conf{'NAME'} = $_[0]->{'name'};
107         $conf{'RESOLUTION'} = $_[1]->{'res'};
108         $conf{'PAPERSIZE'} = $_[1]->{'paper'};
109         $conf{'DESC'} = $_[0]->{'desc'};
110         $conf{'SENDEOF'} = $_[1]->{'eof'};
111         $conf{'DOUBLEPAGE'} = $_[1]->{'double'};
112         $conf{'GSOPTS'} = $_[1]->{'gsopts'};
113         $conf{'UPP'} = $_[1]->{'upp'};
114         &write_env_file("$driver_dir/$_[0]->{'name'}", \%conf);
115
116         # Create the standard driver program
117         open(DRIVER, $base_driver);
118         local @lines = <DRIVER>;
119         close(DRIVER);
120         $lines[1] = "source $driver_dir/$_[0]->{'name'}\n";
121         mkdir("$config{'spool_dir'}/$_[0]->{'name'}", 0755);
122         local $drv = "$config{'spool_dir'}/$_[0]->{'name'}/printfilter";
123         open(DRV, ">$drv");
124         print DRV @lines;
125         close(DRV);
126         return $drv;
127         }
128 }
129
130 # delete_driver(name)
131 sub delete_driver
132 {
133 &delete_webmin_driver($_[0]);
134 unlink("$driver_dir/$_[0]");
135 }
136
137 # driver_input(&printer, &driver)
138 sub driver_input
139 {
140 local $mode = $_[0]->{'rhost'} ? 0 : $_[1]->{'mode'};
141 printf "<tr> <td><input type=radio name=mode value=0 %s> %s</td>\n",
142         $mode == 0 ? 'checked' : '', $text{'caldera_none'};
143 print "<td>($text{'caldera_nonemsg'})</td> </tr>\n";
144 printf "<tr> <td><input type=radio name=mode value=2 %s> %s</td>",
145         $mode == 2 ? 'checked' : '', $text{'caldera_prog'};
146 printf "<td><input name=program size=40 value='%s'></td> </tr>\n",
147         $mode == 2 ? $_[0]->{'iface'} : '';
148
149 # Normal driver options
150 printf "<tr> <td valign=top><input type=radio name=mode value=1 %s> %s</td>\n",
151         $mode == 1 ? 'checked' : '', $text{'caldera_coas'};
152 print "<td><table width=100%>\n";
153
154 print "<tr> <td valign=top><b>$text{'caldera_printer'}</b></td>\n";
155 print "<td colspan=3><select size=5 name=gsdevice onChange='setres(0)'>\n";
156 open(COAS, $config{'coas_printers'});
157 local $plist = &parse_coas(COAS);
158 close(COAS);
159 local ($i, $j, $p, $k, $found, $select_res);
160 foreach $p (values %$plist) {
161         if ($p->{'description'} eq $_[1]->{'gsname'} &&
162             $p->{'type'}->{'0'} ne $_[1]->{'gsdevice'}) {
163                 # COAS has changed the device
164                 $_[1]->{'gsname'} = undef;
165                 }
166         }
167 foreach $k (sort { $a <=> $b } keys %$plist) {
168         $p = $plist->{$k};
169         next if ($p->{'type'}->{'0'} =~ /NET|RAW/);
170         local @thisres = values %{$p->{'resolution'}};
171         local $got = ($_[1]->{'gsname'} eq $p->{'description'} &&
172                       $_[1]->{'gsdevice'} eq $p->{'type'}->{'0'}) ||
173                      (!$_[1]->{'gsname'} && !$found &&
174                       $_[1]->{'gsdevice'} eq $p->{'type'}->{'0'});
175         printf "<option %s value='%s'>%s\n",
176                 $got ? 'selected' : '',
177                 $p->{'description'}.";".join(";", @thisres),
178                 $p->{'description'};
179         $found++ if ($got);
180         $select_res = &indexof($_[1]->{'res'}, @thisres) if ($got);
181         map { $gotres{$_}++ } @thisres;
182         }
183 print "</select><select name=res size=5>\n";
184 foreach $r (sort { $a <=> $b} keys %gotres) {
185         printf "<option %s>%s\n",
186                 $_[1]->{'res'} eq $r ? 'selected' : '', $r;
187         }
188 print "</select></td> </tr>\n";
189
190 print "<tr> <td><b>$text{'caldera_eof'}</b></td>\n";
191 printf "<td><input type=radio name=eof value=true %s> $text{'yes'}\n",
192         $_[1]->{'eof'} eq 'true' ? 'checked' : '';
193 printf "<input type=radio name=eof value=false %s> $text{'no'}</td>\n",
194         $_[1]->{'eof'} eq 'true' ? '' : 'checked';
195
196 print "<td><b>$text{'caldera_paper'}</b></td> <td><select name=paper>\n";
197 foreach $p (sort { $a cmp $b } keys %paper_sizes) {
198         printf "<option value='%s' %s>%s\n",
199                 $p, $_[1]->{'paper'} eq $p ? 'selected' : '',
200                 $paper_sizes{$p};
201         }
202 print "</select></td> </tr>\n";
203
204 print "<tr> <td><b>$text{'caldera_double'}</b></td>\n";
205 printf "<td><input type=radio name=double value=true %s> $text{'yes'}\n",
206         $_[1]->{'double'} eq 'true' ? 'checked' : '';
207 printf "<input type=radio name=double value=false %s> $text{'no'}</td>\n",
208         $_[1]->{'double'} eq 'true' ? '' : 'checked';
209
210 print "<td><b>$text{'caldera_gsopts'}</b></td>\n";
211 printf "<td><input name=gsopts size=30 value='%s'></td> </tr>\n",
212         $_[1]->{'gsopts'};
213
214 print "</table></td></tr>\n";
215
216 # Uniprint driver options
217 printf "<tr> <td valign=top><input type=radio name=mode value=3 %s> %s</td>\n",
218         $mode == 3 ? 'checked' : '', $text{'caldera_uniprint'};
219 print "<td><table width=100%>\n";
220
221 print "<tr> <td valign=top><b>$text{'caldera_printer'}</b></td>\n";
222 print "<td colspan=3><select name=uniprint size=5>\n";
223 foreach $u (&list_uniprint()) {
224         printf "<option value=%s %s>%s\n",
225                 $u->[0], $u->[0] eq $_[1]->{'upp'} ? 'selected' : '', $u->[1];
226         }
227 closedir(DIR);
228 print "</select></td> </tr>\n";
229
230 print "<tr> <td><b>$text{'caldera_eof'}</b></td>\n";
231 printf "<td><input type=radio name=ueof value=true %s> $text{'yes'}\n",
232         $_[1]->{'eof'} eq 'true' ? 'checked' : '';
233 printf "<input type=radio name=ueof value=false %s> $text{'no'}</td>\n",
234         $_[1]->{'eof'} eq 'true' ? '' : 'checked';
235
236 print "<td><b>$text{'caldera_paper'}</b></td> <td><select name=upaper>\n";
237 foreach $p (sort { $a cmp $b } keys %paper_sizes) {
238         printf "<option value='%s' %s>%s\n",
239                 $p, $_[1]->{'paper'} eq $p ? 'selected' : '',
240                 $paper_sizes{$p};
241         }
242 print "</select></td> </tr>\n";
243
244 print "<tr> <td><b>$text{'caldera_double'}</b></td>\n";
245 printf "<td><input type=radio name=udouble value=true %s> $text{'yes'}\n",
246         $_[1]->{'double'} eq 'true' ? 'checked' : '';
247 printf "<input type=radio name=udouble value=false %s> $text{'no'}</td>\n",
248         $_[1]->{'double'} eq 'true' ? '' : 'checked';
249
250 print "</table></td></tr>\n";
251
252 return <<EOF;
253 <script>
254 function setres(sel)
255 {
256 var idx = document.forms[0].gsdevice.selectedIndex;
257 var v = new String(document.forms[0].gsdevice.options[idx].value);
258 var vv = v.split(";");
259 var res = document.forms[0].res;
260 res.length = 0;
261 for(var i=1; i<vv.length; i++) {
262         res.options[i-1] = new Option(vv[i], vv[i]);
263         }
264 if (res.length > 0) {
265         res.options[sel].selected = true;
266         }
267 }
268 setres($select_res);
269 </script>
270 EOF
271 }
272
273 # parse_driver()
274 # Parse driver selection from %in and return a driver structure
275 sub parse_driver
276 {
277 if ($in{'mode'} == 0) {
278         return { 'mode' => 0 };
279         }
280 elsif ($in{'mode'} == 2) {
281         $in{'program'} =~ /^(\S+)/ && -x $1 ||
282                 &error(&text('caldera_eprog', $in{'program'}));
283         return { 'mode' => 2,
284                  'file' => $in{'program'} };
285         }
286 elsif ($in{'mode'} == 1) {
287         # Normal ghostscript driver
288         open(COAS, $config{'coas_printers'});
289         local $plist = &parse_coas(COAS);
290         close(COAS);
291         $in{'gsdevice'} || &error($text{'caldera_edriver'});
292         $in{'gsdevice'} =~ s/;(.*)$//;
293         local ($p, $gsdevice);
294         foreach $p (values %$plist) {
295                 $gsdevice = $p->{'type'}->{'0'}
296                         if ($p->{'description'} eq $in{'gsdevice'});
297                 }
298         $gsdevice eq 'PostScript' || $in{'res'} ||
299                 &error($text{'caldera_eres'});
300         return { 'mode' => 1,
301                  'gsdevice' => $gsdevice,
302                  'gsname' => $in{'gsdevice'},
303                  'res' => $in{'res'},
304                  'paper' => $in{'paper'},
305                  'eof' => $in{'eof'},
306                  'double' => $in{'double'},
307                  'gsopts' => $in{'gsopts'} };
308         }
309 else {
310         # Uniprint ghostscript driver
311         $in{'uniprint'} || &error($text{'caldera_edriver'});
312         return { 'mode' => 3,
313                  'gsdevice' => 'uniprint',
314                  'upp' => $in{'uniprint'},
315                  'paper' => $in{'upaper'},
316                  'eof' => $in{'ueof'},
317                  'double' => $in{'udouble'} };
318         }
319 }
320
321 # parse_coas(handle)
322 sub parse_coas
323 {
324 local $h = $_[0];
325 local (%rv, $_);
326 while(<$h>) {
327         s/#.*$//g;
328         s/\r|\n//g;
329         if (/^\s*(\S+)\s+{/) {
330                 # start of a section
331                 local $k = $1;
332                 $rv{$k} = &parse_coas($h);
333                 }
334         elsif (/^\s*}/) {
335                 # end of a section
336                 last;
337                 }
338         elsif (/^\s*(\S+)\s+(.*)/) {
339                 # a value in a section
340                 $rv{$1} = $2;
341                 }
342         }
343 return \%rv;
344 }
345
346 1;
347