-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfoomatic-configure.in
4506 lines (4079 loc) · 134 KB
/
foomatic-configure.in
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!@PERL@ -w
use strict; # -*- perl -*-
# This is foomatic-configure, a program to establish and configure
# print queues, drivers, spoolers, etc using the foomatic database and
# companion filters.
# It also comprises half of a programatic API for user tools: you can
# learn and control everything about the static properties of print
# queues here. With the sister program foomatic-printjob, you can do
# everything related to print queue dynamic state: submit jobs, and
# query, cancel, reorder, and redirect them.
use Foomatic::Defaults;
use Foomatic::DB;
use Data::Dumper;
# Connect syntax:
#
# This differs a tad from CUPS's, partly because everything is
# supposed to be a file, and CUPS doesn't entirely reflect that.
# But I'm not really very particular...
#
# If a certain URI is not supported by all the spoolers, the spoolers
# which support it are listed in parantheses, "direct" means direct,
# spooler-less printing.
#
# usb:/path/device # Local USB printer
# usb://make/model?serial=xxx # Printer-bound USB connection (CUPS)
# parallel:/path/device # Local parallel printer
# serial:/path/device # Local serial printer
# file:/path/file # includes usb, lp, named pipes, other
# hp:/bus/model?serial=xxx # HPLIP print queue (hpinkjet.sf.net)
# hpfax:/bus/model?serial=xxx # HPLIP fax queue (hpinkjet.sf.net)
# ptal:/provider:bus:name # HPOJ MLC protocol (hpoj.sf.net,obsolete)
# mtink:/path/device # Epson inkjet through mtink daemon
# # (for ink level monitoring when printing,
# # http://xwtools.automatix.de/)
# lpd://host/queue # LPD protocol
# lpd://host # LPD protocol (default queue, CUPS only)
# socket://host:port # TCP aka appsocket
# socket://host # TCP aka appsocket (port 9100)
# ncp://user:pass@host/queue # Netware (LPD, LPRng, direct)
# smb://user:pass@wgrp/host/queue # Windows (CUPS, PPR, LPD, LPRng, direct)
# stdout # Standard output (direct)
# postpipe:"<command line>" # Free-formed backend command line
# # (LPD, LPRng, direct)
#
# Read out the program name with which we were called, but discard the path
$0 =~ m!/([^/]+)\s*$!;
my $progname = ($1 || $0);
my $debug = 0;
# We use the library Getopt::Long here, so that we can have more than
# one "-o" option on one command line.
my($opt_q, $opt_f, $opt_w, $opt_n, $opt_N, $opt_L, $opt_ppd,
$opt_d, $opt_p, $opt_s, $opt_C, $opt_R, $opt_D, $opt_Q, $opt_P,
$opt_O, $opt_X, $opt_c, @opt_o, $opt_r, $opt_dd, $opt_nodd,
$opt_att, $opt_delay, $opt_h);
use Getopt::Long;
Getopt::Long::Configure("no_ignore_case");
GetOptions("q" => \$opt_q, # Quiet, non-interactive operation
"f" => \$opt_f, # Force rebuild of PPD from database
"w" => \$opt_w, # Cut GUI strings in the PPD to 39
# characters (for CUPS Windows driver)
"n=s" => \$opt_n, # queue Name
"N=s" => \$opt_N, # human-readable Name (Model,
# Description)
"L=s" => \$opt_L, # Location
"ppd=s" => \$opt_ppd, # PPD file
"d=s" => \$opt_d, # Driver
"p=s" => \$opt_p, # Printer
"s=s" => \$opt_s, # Spooler
"C" => \$opt_C, # Copy queue
"R" => \$opt_R, # Remove queue
"D" => \$opt_D, # set Default queue
"Q" => \$opt_Q, # Query queue info
"P" => \$opt_P, # Perl queue/printer/driver info output
"O" => \$opt_O, # get printer support Overview
"X" => \$opt_X, # query XML printer/driver/combo info
"c=s" => \$opt_c, # printer Connection type
"o=s" => \@opt_o, # default printing Options
"r" => \$opt_r, # list Remote queues
"backend-dont-disable=s" => \$opt_dd, # Do not disable CUPS
# backends
"backend-attempts=s" => \$opt_att, # Try that often when backend
# fails
"backend-delay=s" => \$opt_delay, # Delay in seconds between
# retries of failed backend
"h" => \$opt_h, # Help!
"help"=> \$opt_h) || help();
help() if $opt_h;
my $db = new Foomatic::DB;
overview() if $opt_O;
get_xml() if $opt_X;
my $force = ($opt_f ? 1 : 0);
my $shortgui = ($opt_w ? 1 : 0);
my $in_config = {'queue' => $opt_n,
'desc' => $opt_N,
'loc' => $opt_L,
'ppdfile' => $opt_ppd,
'driver' => $opt_d,
'printer' => $opt_p,
'spooler' => $opt_s,
'connect' => $opt_c,
'options' => \@opt_o,
'force' => $force,
'shortgui' => $shortgui,
'dd' => $opt_dd,
'att' => $opt_att,
'delay' => $opt_delay,
'foomatic' => 1};
# If description and location contain only whitespace, use an empty string
# instead
if ((defined($in_config->{'desc'})) && ($in_config->{'desc'} =~ m!^\s*$!)) {
$in_config->{'desc'} = "";
}
if ((defined($in_config->{'loc'})) && ($in_config->{'loc'} =~ m!^\s*$!)) {
$in_config->{'loc'} = "";
}
my $action = ($opt_R ? 'delete' : 'configure');
$action = ($opt_D ? 'default' : $action);
$action = ($opt_Q ? 'query' : $action);
$action = ($opt_P ? 'query' : $action);
my $procs = { 'lpd' => { 'delete' => \&delete_lpd,
'configure' => \&setup_lpd,
'default' => \&default_lpd,
'query' => \&query_lpd },
'lprng'=>{ 'delete' => \&delete_lpd,
'query' => \&query_lpd,
'default' => \&default_lprng,
'configure' => \&setup_lpd },
'cups' =>{ 'delete' => \&delete_cups,
'query' => \&query_cups,
'default' => \&default_cups,
'configure' => \&setup_cups },
'pdq' =>{ 'delete' => \&delete_pdq,
'query' => \&query_pdq,
'default' => \&default_pdq,
'configure' => \&setup_pdq },
'ppr' =>{ 'delete' => \&delete_ppr,
'query' => \&query_ppr,
'default' => \&default_ppr,
'configure' => \&setup_ppr },
'direct'=>{'delete' => \&delete_direct,
'query' => \&query_direct,
'default' => \&default_direct,
'configure' => \&setup_direct } };
if (!($opt_Q or $opt_P or defined($in_config->{'queue'}))) {
# No queue manipulation without knowing the name of the queue
print STDERR "You must specify a queue name with -n!\n";
help();
exit 1;
}
if (!defined($in_config->{'spooler'})) {
my $takenfromconfigfile = 0;
# Personal default spooler
my $s;
if (($> != 0) && (-f "$ENV{'HOME'}/.defaultspooler")) {
$s = `cat $ENV{'HOME'}/.defaultspooler`;
chomp $s;
$takenfromconfigfile = 1;
}
# System default spooler
if ((!defined($s)) && (-f "$sysdeps->{'foo-etc'}/defaultspooler")) {
$s = `cat $sysdeps->{'foo-etc'}/defaultspooler`;
chomp $s;
$takenfromconfigfile = 1;
}
if (!defined($s)) {
$s = detect_spooler();
}
die "Unable to identify spooler, please specify with -s\n"
unless $s;
if ((!$opt_q) && (!$takenfromconfigfile)) {
print STDERR "You appear to be using $s. Correct? ";
my $yn = <STDIN>;
die "\n" if ($yn !~ m!^y!i);
}
$in_config->{'spooler'} = $s;
}
if ($in_config->{'printer'}) {
# If the user supplies an old numerical printer ID, translate it to
# a new clear-text ID
$in_config->{'printer'} =
Foomatic::DB::translate_printer_id($in_config->{'printer'});
}
# Call proper proc
&{$procs->{$in_config->{'spooler'}}{$action}}($in_config);
exit(0);
# Common parts for queue creation/modification functions
sub getoldqueuedata {
my ($config, $reconf) = @_;
my ($sourcespooler, $sourcequeue, $olddatablob, $beh);
# Copy a queue
if ($opt_C) {
if ($#ARGV == 0) { # 1 argument -> queue from same spooler
$sourcespooler = $config->{'spooler'};
$sourcequeue = $ARGV[0];
} elsif ($#ARGV == 1) { # 2 arguments -> queue from given spooler
$sourcespooler = $ARGV[0];
$sourcequeue = $ARGV[1];
} else {
die "Unsufficient options to copy a queue, " .
"try \"$progname -h\"!\n";
}
# Read data from source queue
if (!($olddatablob = load_datablob($sourcespooler, $sourcequeue))) {
# It is not possible to copy the given source queue
die "The source queue $sourcequeue does not exist " .
"or is corrupted!\n";
}
# PPD file of the source queue, if it exists, and if the user
# does not insist on using another PPD file, we must copy it
my $sourceppd = $olddatablob->{'ppdfile'};
if ((-r $sourceppd) && (!$config->{'ppdfile'})) {
$config->{'ppdfile'} = $sourceppd;
}
# Stuff data into the $config structure, all items must be defined,
# so that an old queue gets overwritten
if ($olddatablob->{'queuedata'}) {
my $i;
for $i (('desc', 'loc', 'printer', 'driver', 'connect',
'ppdfile', 'dd', 'att', 'delay')) {
if (!defined($config->{$i})) {
if ($olddatablob->{'queuedata'}{$i}){
$config->{$i} = $olddatablob->{'queuedata'}{$i};
} elsif ($i eq 'dd') {
$config->{$i} = 0;
} elsif ($i eq 'att') {
$config->{$i} = 1;
} elsif ($i eq 'delay') {
$config->{$i} = 30;
} else {
$config->{$i} = "";
}
}
}
# Check consistency of the printer/driver settings
if ((($config->{'driver'} eq "") ||
($config->{'driver'} eq "raw") || # No new driver, printer,
($config->{'printer'} eq "")) && # PPD file
($config->{'ppdfile'} eq "") &&
((!defined($olddatablob->{'args'})) || # No existing options
($#{$olddatablob->{'args'}} < 0))) { # -> source queue raw
$config->{'driver'} = "raw";
$config->{'printer'} = undef;
}
# We do not need the queue data block any more
delete($olddatablob->{'queuedata'});
} else {
# No Foomatic/PPD data
$olddatablob = undef;
}
} else {
# Load the datablob of the former configuration
if ($reconf) {
if ($olddatablob = load_datablob($config->{'spooler'},
$config->{'queue'})) {
# If the user has supplied only a printer or only a driver
# fill in the second of the two fields in $config
if ((!$config->{'ppdfile'}) &&
($olddatablob->{'queuedata'}{'foomatic'})) {
if ((!$config->{'driver'}) && ($config->{'printer'})) {
$config->{'driver'} = $olddatablob->{'driver'};
}
if ((!$config->{'printer'}) && ($config->{'driver'})) {
$config->{'printer'} = $olddatablob->{'id'};
}
}
# Extract URI and backend error handling data
if ($config->{'spooler'} eq "cups") {
$beh->{'uri'} = $olddatablob->{'queuedata'}{'connect'};
$beh->{'dd'} = $olddatablob->{'queuedata'}{'dd'};
$beh->{'att'} = $olddatablob->{'queuedata'}{'att'};
$beh->{'delay'} = $olddatablob->{'queuedata'}{'delay'};
}
# We do not need the queue data block here
delete($olddatablob->{'queuedata'});
} else {
$olddatablob = undef;
}
}
}
# If the user does not supply info about his printer and/or driver
# and the queue did not exist before we assume that he wants to set up a
# raw queue. To make a raw queue out of a formerly filtered one, one
# has to use the driver name "raw".
$config->{'driver'} = "" if not defined $config->{'driver'};
$config->{'printer'} = "" if not defined $config->{'printer'};
$config->{'ppdfile'} = "" if not defined $config->{'ppdfile'};
my $nodriver = (((!$config->{'driver'}) && (!$config->{'printer'}) &&
(!$config->{'ppdfile'})) ||
($config->{'driver'} eq "raw"));
# Set to 1 when we retrieve a data set from the Foomatic database
my $newfoomaticdata = 0;
if ($nodriver) {
if ($olddatablob) {
if ($config->{'driver'} ne "raw") {
# We couldn't determine a certain driver, probably we had a
# native PostScript PPD file
$db->{'dat'} = $olddatablob;
} else {
# For a raw queue overtake at least the $postpipe
if (defined($olddatablob->{'postpipe'})) {
$db->{'dat'}{'postpipe'} = $olddatablob->{'postpipe'};
}
}
}
} elsif ($config->{'ppdfile'}) {
if (! -r $config->{'ppdfile'}) {
die "The PPD file \'$config->{'ppdfile'}\' does not exist or is " .
"readable.\n";
}
# Load the data from the PPD file
$db->getdatfromppd($config->{'ppdfile'});
# Overtake the former default settings
if ($olddatablob) {overtake_defaults($olddatablob)};
# Overtake the former $postpipe
if (defined($olddatablob->{'postpipe'})) {
$db->{'dat'}{'postpipe'} = $olddatablob->{'postpipe'};
}
} else {
if (($olddatablob) &&
($olddatablob->{'driver'} eq $config->{'driver'}) &&
($olddatablob->{'id'} eq $config->{'printer'}) &&
(!$config->{'force'})) {
# Overtake data from the former configuration
$db->{'dat'} = $olddatablob;
} else {
# Retrieve data from the Foomatic database
if (!$config->{'driver'}) {
die "You also need to specify a driver with \"-d\"!\n";
}
if (!$config->{'printer'}) {
die "You also need to specify a printer with \"-p\"!\n";
}
# The printer is supported by the chosen driver? If yes, load
# its data
my $possible = $db->getdat($config->{'driver'},
$config->{'printer'});
die "That printer and driver combination is not possible.\n"
if (!$possible);
die "There is neither a custom PPD file nor the driver database entry contains sufficient data to build a PPD file.\n"
if (!$db->{'dat'}{'cmd'}) && (!$db->{'dat'}{'ppdfile'});
$newfoomaticdata = 1;
# Overtake the former default settings
if ($olddatablob) {overtake_defaults($olddatablob)};
# Overtake the former $postpipe
if (defined($olddatablob->{'postpipe'})) {
$db->{'dat'}{'postpipe'} = $olddatablob->{'postpipe'};
}
}
}
# When we have no arguments in the current configuration, we must have
# a raw queue
my $rawqueue = ((!defined($db->{'dat'}{'args'})) ||
($#{$db->{'dat'}{'args'}} < 0));
# Set the default printing options supplied on the command line
if (!$rawqueue) {
set_default_options($config, $db->{'dat'});
}
# Printer model name (for comment field of the queue configuration)
my ($make, $model, $makemodel);
if (defined($db->{'dat'})) {
$make = $db->{'dat'}{'make'};
$model = $db->{'dat'}{'model'};
$makemodel = $db->{'dat'}{'makemodel'};
if (($make) && ($model)) {
$makemodel = "$make $model";
}
}
return ($rawqueue, $newfoomaticdata, $makemodel,
($config->{'spooler'} eq "cups" ? $beh : ()));
}
#fix to work on Ubuntu, where cupd runs not as root, but as cupsys user
#like system ("chown cupsys $ppdfile"), but
#changeowner function changes owner only if user exists on system
sub changeowner {
my ($username, $file) = @_;
my ($uid,$gid) = (-1, -1);
my $l;
$l = getpwnam($username); $uid = $l if defined($l);
$l = getgrnam($username); $gid = $l if defined($l);
chown $uid, $gid, $file;
}
sub writeppdfile {
my ($config, $ppdfile, $rawqueue, $newfoomaticdata) = @_;
# Save old $ppdfile, if any
system("cp -f \'$ppdfile\' \'$ppdfile.old\'")
if (-f $ppdfile);
if ($rawqueue) {
# Raw queue with $postpipe, use a "PPD" only containing the
# $postpipe (LPRng, LPD, and no spooler only)
if (((defined $db->{'dat'}{'postpipe'} && $db->{'dat'}{'postpipe'} ne "") &&
(($config->{'spooler'} eq 'lprng') ||
($config->{'spooler'} eq 'lpd'))) ||
($config->{'spooler'} eq 'direct')) {
open PPDFILE, "> $ppdfile" or die "Cannot write \'$ppdfile\'!\n";
print PPDFILE "*PPD-Adobe: \"4.3\"\n*%\n";
print PPDFILE "*% This is a raw (driverless/unfiltered) " .
"queue, this PPD file only carries\n" .
"*% the postpipe.\n*%\n";
close PPDFILE;
$db->ppdsetdefaults($ppdfile);
chmod 0644, $ppdfile;
#fix to work on Ubuntu, where cupd runs not as root, but as cupsys user
#system ("chown cupsys $ppdfile");
#changeowner function changes owner only if user exists on system
changeowner("cupsys", $ppdfile);
} else {
if (-f $ppdfile) {
unlink "$ppdfile" or die "Cannot delete \'$ppdfile\'!\n";
}
}
} else {
if ($config->{'ppdfile'}) {
# Copy in the PPD file specified on the command line
if ($config->{'ppdfile'} !~ /\.gz$/i) {
# Uncompressed PPD file
system("cp -f \'$config->{'ppdfile'}\' \'$ppdfile\'") and
die "Cannot copy \'$config->{'ppdfile'}\' to \'$ppdfile\'!\n";
} else {
# Compressed PPD file
system("$sysdeps->{'gzip'} -dc " .
"\'$config->{'ppdfile'}\' > " .
"\'$ppdfile\'") and
die "Cannot copy \'$config->{'ppdfile'}\' to \'$ppdfile\'!\n";
}
# Set default option settings and $postpipe
$db->ppdsetdefaults($ppdfile);
} elsif ($newfoomaticdata) {
# Generate the PPD file from the Foomatic database
open PPDFILE, "> $ppdfile" or die "Cannot write \'$ppdfile\'!\n";
print PPDFILE $db->getppd($config->{'shortgui'});
close PPDFILE;
} else {
# Keep the previous PPD file, only set the options and the
# $postpipe
$db->ppdsetdefaults($ppdfile);
}
# Correct the permissions of the PPD file
chmod 0644, $ppdfile;
#fix to work on Ubuntu, where cupd runs not as root, but as cupsys user
#system ("chown cupsys $ppdfile");
#changeowner function changes owner only if user exists on system
changeowner("cupsys", $ppdfile);
}
}
### Queue manipulation functions for both LPD and LPRng
sub setup_lpd {
my ($config) = $_[0];
# Read the previous /etc/printcap
my $pcap = load_lpd_printcap();
my ($ppdfile, $entry, $reconf, $p);
for $p (@{$pcap}) {
if ($p->{'names'}[0] eq $config->{'queue'}) {
$entry = $p;
$reconf = 1;
print "Reconfigure of ", Dumper($p) if $debug;
last;
}
}
# PPD file name
$ppdfile = sprintf('%s/lpd/%s.ppd',
$sysdeps->{'foo-etc'},
$config->{'queue'}) if !$ppdfile;
# Get the data from the former queue if we reconfigure or copy a queue
# do also some checking of the user-supplied parameters
my ($rawqueue, $newfoomaticdata, $makemodel) =
getoldqueuedata($config, $reconf);
# Set the printer queue name line in /etc/printcap
if (!$reconf) {
if (!$rawqueue) {
$entry->{'names'}[0] = $config->{'queue'};
$entry->{'names'}[1] = $config->{'desc'};
$entry->{'names'}[2] = "$makemodel";
$entry->{'names'}[3] = $config->{'loc'};
} else {
$entry->{'names'}[0] = $config->{'queue'};
$entry->{'names'}[1] = $config->{'desc'};
$entry->{'names'}[2] = "Raw queue";
$entry->{'names'}[3] = $config->{'loc'};
}
} else {
if (!$rawqueue) {
$entry->{'names'}[2] = "$makemodel";
} else {
if (($entry->{'names'}[2] eq "Raw queue") ||
($config->{'driver'} eq "raw")) {
$rawqueue = 1;
$entry->{'names'}[2] = "Raw queue";
}
}
if (defined($config->{'desc'})) {
$entry->{'names'}[1] = $config->{'desc'};
}
if (defined($config->{'loc'})) {
$entry->{'names'}[3] = $config->{'loc'};
}
}
# These lines are always in /etc/printcap
$entry->{'str'}{'sd'} = sprintf('%s/%s',
$sysdeps->{'lpd-dir'},
$config->{'queue'});
$entry->{'str'}{'lf'} = $sysdeps->{'lpd-log'};
$entry->{'num'}{'mx'} = '0';
$entry->{'bool'}{'sh'} = 1;
# Lines depending on the printer/spooler
if (!$rawqueue) {
if ($config->{'spooler'} eq "lpd") {
$entry->{'str'}{'ppdfile'} = $ppdfile; # For the GPR printing GUI
delete $entry->{'str'}{'ppd'};
$entry->{'str'}{'if'} = $sysdeps->{'foomatic-rip'};
$entry->{'str'}{'af'} = $ppdfile;
delete $entry->{'bool'}{'force_localhost'};
delete $entry->{'str'}{'filter_options'};
} elsif ($config->{'spooler'} eq "lprng") {
$entry->{'str'}{'ppd'} = $ppdfile; # for LPRng PPD support
$entry->{'str'}{'if'} = $sysdeps->{'foomatic-rip'};
$entry->{'bool'}{'force_localhost'} = 1;
delete $entry->{'str'}{'ppdfile'};
delete $entry->{'str'}{'af'};
delete $entry->{'str'}{'filter_options'};
} else {
die "The spooler $config->{'spooler'} is not supported " .
"by this function!\n";
}
} else {
delete $entry->{'str'}{'if'};
delete $entry->{'str'}{'af'};
delete $entry->{'str'}{'filter_options'};
delete $entry->{'str'}{'ppd'};
if ($config->{'spooler'} eq "lpd") {
delete $entry->{'bool'}{'force_localhost'};
} elsif ($config->{'spooler'} eq "lprng") {
$entry->{'bool'}{'force_localhost'} = 1;
} else {
die "The spooler $config->{'spooler'} is not supported " .
"by this function!\n";
}
}
# If printing job has to be passed through a special program, put the
# command line into $postpipe (for example for Socket, Samba, ...)
my $postpipe = "";
if ((!$reconf) or ($config->{'connect'})) {
# Set up connection type
# Remove "rm" and "rp" tags to avoid problems when overwriting a
# raw queue
delete $entry->{'str'}{'rm'};
delete $entry->{'str'}{'rp'};
# All URIs ("-c" option) have the same syntax as URIs in CUPS ("-v"
# option of "lpadmin").
my $file;
if ($config->{'connect'} =~ m!^(file|usb|parallel|serial):(.*)!) {
# Local printer or printing to a file
$file = $2;
if ($config->{'connect'} =~ m!^usb://!) {
# Queue with printer-bound USB URI transferred from CUPS,
# as LPD/LPRng does not support these URIs, translate it
# back to a standard USB device URI
$file = cups_usb_printer_uri_to_device_uri($file);
}
if (! -e $file) {
warn "The device or file $file doesn't exist? " .
"Working anyway.\n";
}
if (($file =~ m!^$sysdeps->{'ptal-pipes'}/(.+)$!) ||
($file =~ m!^/dev/ptal-printd/(.+)$!) ||
($file =~ m!^/var/run/ptal-printd/(.+)$!)) {
# Translate URI for ptal-printd to postpipe using the
# "ptal-connect" command
my $devname = $1;
$devname =~ s/_/:/;
$devname =~ s/_/:/;
$postpipe = "$sysdeps->{'ptal-connect'} $devname -print";
$entry->{'str'}{'lp'} = "/dev/null";
} else {
$entry->{'str'}{'lp'} = $file;
}
} elsif ($config->{'connect'} =~ m!^ptal://?([^/].*)$!) {
# HPOJ MLC protocol
my $devname = $1;
$postpipe = "$sysdeps->{'ptal-connect'} $devname -print";
$entry->{'str'}{'lp'} = "/dev/null";
} elsif ($config->{'connect'} =~ m!^mtink:/(.+)$!) {
# Printing through "mtinkd"
$entry->{'str'}{'lp'} = "$sysdeps->{'mtink-pipes'}/$1";
} elsif ($config->{'connect'} =~ m!^lpd://([^/]+)/([^/]+)$!) {
# Remote LPD
my $remhost = $1;
my $remqueue = $2;
if (($rawqueue) && ($config->{'spooler'} eq "lpd")) {
$entry->{'str'}{'rm'} = $remhost;
$entry->{'str'}{'rp'} = $remqueue;
delete $entry->{'str'}{'lp'};
} elsif( ($config->{'spooler'} eq "lprng")) {
delete $entry->{'str'}{'rm'};
delete $entry->{'str'}{'rp'};
$entry->{'str'}{'lp'} = "$remqueue\@$remhost";
} else {
# classic LPD does not support sending jobs to a server with the
# "rm" and "rp" tags in /etc/printcap and filtering it
# before ("if" tag). So when we do not set up a raw queue,
# we do not
#
# $entry->{'str'}{'rm'} = $remhost;
# $entry->{'str'}{'rp'} = $remqueue;
#
# but use "rlpr" in a $postpipe. Note that "rlpr" prints a
# banner page by default, "-h" suppresses it. "rlpr" must
# be SUID "root".
$postpipe = "$sysdeps->{'rlpr'} -q -h -P " .
"$remqueue\@$remhost";
$entry->{'str'}{'lp'} = "/dev/null";
}
} elsif ($config->{'connect'} =~
m!^socket://([^/:]+):([0-9]+)/?$!) {
# Socket (AppSocket/HP JetDirect)
my $remhost = $1;
my $remport = $2;
if( ($config->{'spooler'} eq "lprng")) {
$entry->{'str'}{'lp'} = "$remhost\%$remport";
} else {
$postpipe = "$sysdeps->{'nc'} -w 1 $remhost $remport";
$entry->{'str'}{'lp'} = "/dev/null";
}
} elsif ($config->{'connect'} =~ m!^smb://(.*)$!) {
# SMB (Printer on Windows server)
my $parameters = $1;
# Get the user's login and password from the URI
my $smbuser = "";
my $smbpassword = "";
if ($parameters =~ m!([^@]*)@([^@]+)!) {
my $login = $1;
$parameters = $2;
if ($login =~ m!([^:]*):([^:]*)!) {
$smbuser = $1;
$smbpassword = $2;
} else {
$smbuser = $login;
$smbpassword = "";
}
} else {
$smbuser = "GUEST";
$smbpassword = "";
}
# Get the workgroup, server, and share name
my $workgroup = "";
my $smbserver = "";
my $smbshare = "";
if ($parameters =~ m!([^/]*)/([^/]+)/([^/]+)$!) {
$workgroup = $1;
$smbserver = $2;
$smbshare = $3;
} elsif ($parameters =~ m!([^/]+)/([^/]+)$!) {
$workgroup = "";
$smbserver = $1;
$smbshare = $2;
} else {
die "The \"smb://\" URI must at least contain the " .
"server name and the share name!\n";
}
# Set up the command line for printing on the SMB server
$postpipe = "$sysdeps->{'smbclient'} '//$smbserver/$smbshare'";
if ($smbpassword ne "") {
warn("WARNING: smbclient password is visible in PPD file\n");
$postpipe .= " '$smbpassword'";
}
if ($smbuser ne "") {$postpipe .= " -U '$smbuser'";}
if ($workgroup ne "") {$postpipe .= " -W '$workgroup'";}
$postpipe .= " -N -P -c 'print -' ";
$entry->{'str'}{'lp'} = "/dev/null";
} elsif ($config->{'connect'} =~ m!^ncp://(.*)$!) {
my $parameters = $1;
# Get the user's login and password from the URI
my $ncpuser = "";
my $ncppassword = "";
if ($parameters =~ m!([^@]*)@([^@]+)!) {
my $login = $1;
$parameters = $2;
if ($login =~ m!([^:]*):([^:]*)!) {
$ncpuser = $1;
$ncppassword = $2;
} else {
$ncpuser = $login;
$ncppassword = "";
}
} else {
$ncpuser = "";
$ncppassword = "";
}
# Get the server and share name
my $ncpserver = "";
my $ncpqueue = "";
if ($parameters =~ m!([^/]+)/([^/]+)$!) {
$ncpserver = $1;
$ncpqueue = $2;
} else {
die "The \"ncp://\" URI must at least contain the " .
"server name and the queue name!\n";
}
# Set up the command line for printing on the Netware server
$postpipe = "$sysdeps->{'nprint'} -S $ncpserver";
if ($ncpuser ne "") {
$postpipe .= " -U $ncpuser";
if ($ncppassword ne "") {
warn("WARNING: ncp password is visible in PPD file\n");
$postpipe .= " -P $ncppassword";
} else {
$postpipe .= " -n";
}
}
$postpipe .= " -q $ncpqueue -N - 2>/dev/null";
$entry->{'str'}{'lp'} = "/dev/null";
} elsif ($config->{'connect'} =~ m!^postpipe:(.*)$!) {
# Pipe output into a command
$postpipe = $1;
$entry->{'str'}{'lp'} = "/dev/null";
} elsif ($config->{'connect'}) {
$entry->{'str'}{'lp'} = '/dev/null';
die ("The URI \"$config->{'connect'}\" is not supported " .
"for LPD/LPRng or you have\nmistyped.\n");
} else {
print STDERR "You must specify a connection with -c.\n";
help();
exit(1);
}
# Put $postpipe into the data structure, so that it will be
# inserted into the PPD file
if ($postpipe ne "") {
$postpipe = "| $postpipe";
$db->{'dat'}{'postpipe'} = $postpipe;
} else {
undef $db->{'dat'}{'postpipe'};
}
} else {
# Keep previous connection type
# Use previous $postpipe
if (defined($db->{'dat'}{'postpipe'})) {
$postpipe = $db->{'dat'}{'postpipe'};
}
}
# When we have a $postpipe we never write to a device
if ($postpipe ne "") {
$entry->{'str'}{'lp'} = '/dev/null';
if ($config->{'spooler'} eq "lpd") {
$entry->{'str'}{'if'} = $sysdeps->{'foomatic-rip'};
$entry->{'str'}{'af'} = $ppdfile;
} elsif ($config->{'spooler'} eq "lprng") {
$entry->{'str'}{'if'} = $sysdeps->{'foomatic-rip'};
$entry->{'str'}{'ppd'} = $ppdfile;
$entry->{'bool'}{'force_localhost'} = 1;
} else {
die "The spooler $config->{'spooler'} is not supported " .
"by this function!\n";
}
}
# Various file setup
mkdir $sysdeps->{'foo-etc'}, 0755;
mkdir "$sysdeps->{'foo-etc'}/lpd", 0755;
mkdir $entry->{'str'}{'sd'}, 0755;
# Lead with a blank line for new entries
push (@{$entry->{'comments'}}, "\n")
if (!$reconf);
# Put in a useful comment for both new and old entries
push (@{$entry->{'comments'}},
sprintf ("\# Entry edited %s by $progname.",
scalar(localtime(time))),
"\# Additional configuration atop $ppdfile");
# Add to the printcap if a new entry
if (!$reconf) {
push(@{$pcap}, $entry);
}
# Generate/write te PPD file
writeppdfile($config, $ppdfile, $rawqueue, $newfoomaticdata);
# Make sure that /var/spool/lp-errs exists
system "touch $sysdeps->{'lpd-log'}";
chmod 0600, $sysdeps->{'lpd-log'};
my ($lpuid, $lpgid) = (-1, -1);
my $l;
$l = getpwnam("lp"); $lpuid = $l if defined($l);
$l = getgrnam("lp"); $lpgid = $l if defined($l);
chown $lpuid, $lpgid, $sysdeps->{'lpd-log'};
# Write back /etc/printcap
my $printcap = $sysdeps->{'lpd-pcap'};
rename $printcap, "$printcap.old" or die "Cannot backup $printcap!\n";
open PRINTCAP, "> $printcap" or die "Cannot write $printcap!\n";
print PRINTCAP dump_lpd_printcap($config, $pcap);
close PRINTCAP;
chmod 0644, $printcap;
# In case of LPRng, give SIGHUP to the daemon, LPRng needs this to
# recognize a new queue
if ($config->{'spooler'} eq "lprng") {
# first check configuration
system("$sysdeps->{'lprng-checkpc'} -f > /dev/null 2>&1");
# now signal to use it
system("$sysdeps->{'lpd-lpc'} reread > /dev/null 2>&1");
}
return 1;
}
sub default_lpd {
my ($config) = $_[0];
my $name = $config->{'queue'};
my $pcap = load_lpd_printcap();
# Add the alias "lp" to the /etc/printcap entry to make LPD considering
# the chosen printer as default printer
# Some stuff for renaming a queue named "lp"
my $nppdfile = undef;
my $newname = undef;
my $rawqueue = 0;
my @newcap;
for (@{$pcap}) {
my $p = $_;
if ($p->{'names'}[0] eq $name) {
$p->{'names'}[4] = 'lp';
} else {
# Rename a printer whose first name is 'lp'
if ($p->{'names'}[0] eq 'lp') {
# Do we have a raw queue?
if ((!defined($p->{'str'}{'if'})) ||
($p->{'str'}{'if'} ne $sysdeps->{'foomatic-rip'})) {
$rawqueue = 1;
}
# Search for a free name
my $i = 0;
my $namefound = 0;
while(!$namefound) {
my $pp;
my $nameinuse = 0;
for $pp (@{$pcap}) {
if (defined($pp->{'names'})) {
my $n;
for $n (@{$pp->{'names'}}) {
if ($n eq "lp$i") {
$nameinuse = 1;
last;
}
}
if ($nameinuse) {
$i++;
last;
}
}
}
$namefound = 1 - $nameinuse;
}
$newname = "lp$i";
# Old PPD file name
my $ppdfile = sprintf('%s/lpd/lp.ppd',
$sysdeps->{'foo-etc'});
# New PPD file name
my $nppdfile = sprintf('%s/lpd/%s.ppd',
$sysdeps->{'foo-etc'},
$newname);
# Rename the printer
$p->{'names'}[0] = $newname;
my $oldspooldir = $p->{'str'}{'sd'};
$p->{'str'}{'sd'} = sprintf('%s/%s',
$sysdeps->{'lpd-dir'},
$newname);
if ($p->{'str'}{'af'} =~ /\.ppd$/) {
$p->{'str'}{'af'} = $nppdfile;
}
# Rename old $ppdfile, if any
rename $ppdfile, $nppdfile
if (-f $ppdfile);
# Rename the spool directory
rename $oldspooldir, $p->{'str'}{'sd'}
if (-d $oldspooldir);
# Put out warning
warn("WARNING: Printer \"lp\" renamed to \"$newname\".\n");
}
# Remove 'lp' as alias name
my $n;
for $n (@{$p->{'names'}}) {
if ($n eq 'lp') {
$n = '';
}
}
}
push (@newcap, $p);
}
my @newprintcap = dump_lpd_printcap($config, \@newcap);
my $printcap = $sysdeps->{'lpd-pcap'};
rename $printcap, "$printcap.old" or die "Cannot backup $printcap!\n";
open PRINTCAP, "> $printcap" or die "Cannot write $printcap!\n";
print PRINTCAP @newprintcap;
close PRINTCAP;
chmod 0644, $printcap;
return 1;
}
sub default_lprng {
my ($config) = $_[0];
my $name = $config->{'queue'};
my $pcap = load_lpd_printcap();
# Move the /etc/printcap entry for the chosen printer to the first place
# so that LPRng considers it as the default printer
my @newcap;
for (@{$pcap}) {
push (@newcap, $_)
if ($_->{'names'}[0] eq $name);
}
for (@{$pcap}) {
push (@newcap, $_)
unless ($_->{'names'}[0] eq $name);
}
my @newprintcap = dump_lpd_printcap($config, \@newcap);
my $printcap = $sysdeps->{'lpd-pcap'};
rename $printcap, "$printcap.old" or die "Cannot backup $printcap!\n";
open PRINTCAP, "> $printcap" or die "Cannot write $printcap!\n";
print PRINTCAP @newprintcap;