This file is indexed.

/usr/bin/foomatic-printjob is in foomatic-db-engine 4.0.12-2+b1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
#!/usr/bin/perl
# -*- perl -*-
# This is foomatic-printjob, a program to print and manage printing
# jobs with the same commands independent whether the spooler is CUPS,
# LPD, LPRng, or PDQ.

# It also comprises half of a programattic API for user tools: you can
# learn and control everything about the properties of printing jobs
# here. With the sister program foomatic-configure, you can do
# everything related to print queue static state: install, modify,
# remove queues, query queue, printer, and driver info.

use Foomatic::Defaults;
use Foomatic::DB;

# Read out the program name with which we were called, but discard the path

$0 =~ m!/([^/]+)\s*$!;
$progname = $1;

# We use the library Getopt::Long here, so that we can have more than one "-o"
# option on one command line.

use Getopt::Long;
Getopt::Long::Configure("no_ignore_case", "pass_through");
GetOptions("P=s" => \$opt_P,         # which queue (Printer)?
	   "d=s" => \$opt_d,         # which queue (Destination)?
	   "s=s" => \$opt_s,         # which Spooler?
	   "o=s" => \@opt_o,         # printing Options
	   "Q"   => \$opt_Q,         # Query jobs in queue
	   "R"   => \$opt_R,         # Remove job(s)
	   "C"   => \$opt_C,         # Control job(s)/queue(s)
	   "S"   => \$opt_S,         # set default Spooler
	   "h"   => \$opt_h);        # Help!

help() if ($opt_h && !$opt_P);

my $in_config = {'queue'   => $opt_P,
		 'options' => \@opt_o,
		 'spooler' => $opt_s};

# Default action: Printing
my $action = 'print';

# Determine the action by the name how we were called
if ($progname =~ m!^lpc!) { # 'lpc*' ==> control 
    $action = 'control';
} elsif ($progname =~ m!^lprm!) { # 'lprm*' ==> remove jobs 
    $action = 'remove';
} elsif ($progname =~ m!^lpq!) { # 'lpq*' ==> list jobs 
    $action = 'query';
} elsif (($progname =~ m!^lpr!) || ($progname =~ m!^lp!)) { 
    # 'lpr*', 'lp*' ==> print 
    $action = 'print';
}

# Determine the action by a command line option
$action = ($opt_R ? 'remove' : $action);
$action = ($opt_Q ? 'query' : $action);
$action = ($opt_C ? 'control' : $action);

my $procs = { 'lpd' => { 'print'    => \&print_lpd,
			 'query'    => \&query_lpd,
		         'remove'   => \&remove_lpd,
		         'control'  => \&control_lpd },
	      'lprng'=>{ 'print'    => \&print_lprng,
			 'query'    => \&query_lprng,
			 'remove'   => \&remove_lprng,
			 'control'  => \&control_lpd },
	      'cups' =>{ 'print'    => \&print_cups,
			 'query'    => \&query_cups,
			 'remove'   => \&remove_cups,
			 'control'  => \&control_cups },
	      'pdq'  =>{ 'print'    => \&print_pdq,
			 'query'    => \&query_pdq,
			 'remove'   => \&remove_pdq,
			 'control'  => \&control_pdq } };

if (!(defined($in_config->{'queue'}))) {
    # No job handling without knowing the name of the queue
    # PRINTER environment variable
    if (defined($opt_d)) {
	$in_config->{'queue'} = $opt_d;
    } elsif (defined($ENV{PRINTER})) {
	$in_config->{'queue'} = $ENV{PRINTER};
    } else {
	# Use spoolers default
    }
}

if (!defined($in_config->{'spooler'})) {

    # Personal default spooler
    if (($> != 0) && (-f "$ENV{'HOME'}/.defaultspooler")) {
	$s = `cat $ENV{'HOME'}/.defaultspooler`;
	chomp $s;
    }
    
    # System default spooler
    if ((!defined($s)) && (-f "$sysdeps->{'foo-etc'}/defaultspooler")) {
	$s = `cat $sysdeps->{'foo-etc'}/defaultspooler`;
	chomp $s;
    }
    
    if (!defined($s)) {
	$s = detect_spooler();
    }

    die "Unable to identify spooler, please specify one with \"-s\"!\n"
	unless $s;

    if (defined($opt_i)) {
	print STDERR "You appear to be using $s.  Correct? ";
	my $yn = <STDIN>;
	die "\n" if ($yn !~ m!^y!i);
    }

    $in_config->{'spooler'} = $s;
}

if (defined($opt_S)) {
    if ($> == 0) { # Program invoked as "root"?
	# Set system default spooler
	open DEFAULTFILE, "> $sysdeps->{'foo-etc'}/defaultspooler" ||
	    die "Cannot write $sysdeps->{'foo-etc'}/defaultspooler!\n";
	print DEFAULTFILE "$in_config->{'spooler'}\n";
	close DEFAULTFILE;
	exit 0;
    } else {
	# Set personal default spooler
	open DEFAULTFILE, "> $ENV{'HOME'}/.defaultspooler" ||
	    die "Cannot write $ENV{'HOME'}/.defaultspooler!\n";
	print DEFAULTFILE "$in_config->{'spooler'}\n";
	close DEFAULTFILE;
	exit 0;
    }
}

# Exception...
help_options($in_config) if ($opt_h);

# Call proper proc
exit &{$procs->{$in_config->{'spooler'}}{$action}}($in_config);

### Printing/Job manipulation functions for LPD

sub print_lpd {
    my ($config) = $_[0];

    #sysdeps->{'lpd-lpr'} = "/home/test/lpr-0.71/lpr/lpr";

    # Auto-detect whether the "lpr" executable is the VA-Linux version or not
    my $valinuxlpr = 
	!(system "strings $sysdeps->{'lpd-lpr'} | grep option > /dev/null");

    # Printing command
    my $commandline = "$sysdeps->{'lpd-lpr'}";

    # Add the printer queue argument
    if (defined($config->{'queue'})) {
	$commandline .= " -P $config->{'queue'}";
    }

    # Add the driver-specific options supplied by the user, if any
    # For the VA-Linux implementation of "lpr" (gnulpr) options are passed
    # with '-o option=value -o switch', for the BSD implementation they are
    # passe with '-J"option=value switch"'.
    if ($valinuxlpr) {
	# VA-Linux/gnulpr
	if ($#{$config->{'options'}} >= 0) {
	    for (@{$config->{'options'}}) {
		$commandline .= " -o $_";
	    }
	}
    } else {
	# BSD
	if ($#{$config->{'options'}} >= 0) {
	    $commandline .= " -J\"";
	    for (@{$config->{'options'}}) {
		$commandline .= "$_ ";
	    }
	    $commandline .= "\"";
	}
    }

    # Add the remaining command line arguments, they are the names of
    # the files to print and also spooler-specific options
    $commandline .= " @ARGV";

    # Do it!

    #print "$commandline\n";
    return (system $commandline) >> 8;

}

sub query_lpd {
    my ($config) = $_[0];

    # standard lpq, emulate -a of lpq-cups
    # Read additional options
    GetOptions("a"   => \$opt_a);        # List jobs on all printers
    
    if (defined($opt_a)) {
	# Get all printer queues
	open QUEUELIST, "$sysdeps->{'lpd-lpc'} status 2>&1 | grep \":\$\" | ";
	my @queuelist = <QUEUELIST>;
	close QUEUELIST;
	# List the jobs on all the queues
	for (@queuelist) {
	    my $queue = $_;
	    chomp $queue;
	    print "$queue\n";
	    $queue =~ s/:$//;
	    my $result = (system "$sysdeps->{'lpd-lpq'} -P $queue @ARGV") >> 8;
	    if ($result != 0) {return $result};
	}
    } else {
	# List the jobs on the specified queue
	my $queue = "";
	if (defined($config->{'queue'})) {
	    $queue = " -P $config->{'queue'}";
	}
	return (system "$sysdeps->{'lpd-lpq'}$queue @ARGV") >> 8;
    }
}

sub remove_lpd {
    my ($config) = $_[0];
    
    # Remove a job with the standard "lprm" command

    # Removing command
    my $commandline = "$sysdeps->{'lpd-lprm'}";

    # Add the printer queue argument
    if (defined($config->{'queue'})) {
	$commandline .= " -P $config->{'queue'}";
    }

    # Add the remaining command line arguments, they are the numbers
    # of the jobs to kill, the users whose jos to remove and also
    # spooler-specific options

    $commandline .= " @ARGV";

    # Do it!

    #print "$commandline\n";
    return (system $commandline) >> 8;

}

sub control_lpd {
    my ($config) = $_[0];
    
    # Control the printing system with the standard "lpc" command

    # Control command
    my $commandline = "$sysdeps->{'lpd-lpc'}";

    # Add the remaining command line arguments, they are the control command
    # with its arguments

    $commandline .= " @ARGV";

    # Do it!

    #print "$commandline\n";
    return (system $commandline) >> 8;

}

### Printing/Job manipulation functions for LPRng

sub print_lprng {
    my ($config) = $_[0];

    # Printing command
    my $commandline = "$sysdeps->{'lpd-lpr'}";

    # Add the printer queue argument
    if (defined($config->{'queue'})) {
	$commandline .= " -P $config->{'queue'}";
    }

    # Add the driver-specific options supplied by the user, if any
    if ($#{$config->{'options'}} >= 0) {
	for (@{$config->{'options'}}) {
	    $commandline .= " -Z $_";
	}
    }

    # Add the remaining command line arguments, they are the names of
    # the files to print and also spooler-specific options
    $commandline .= " @ARGV";

    # Do it!

    #print "$commandline\n";
    return (system $commandline) >> 8;

}

sub query_lprng {
    my ($config) = $_[0];

    # We filter the output of lpq and rearrange it to have the same format
    # as of LPD and CUPS.

    GetOptions("l"   => \$opt_l);       # Long, more verbose output

    # List the jobs on the specified queue
    my $queue = "";
    if (defined($config->{'queue'})) {
	$queue = " -P $config->{'queue'}";
    }
    open LPQOUTPUT, "$sysdeps->{'lpd-lpq'}$queue @ARGV |" || return 1;
    my @lpqoutput = <LPQOUTPUT>;
    close LPQOUTPUT;
    # Filter the output
    for $line (@lpqoutput) {
	chomp $line;
	if ($line =~ m!^\s*(\S+)\s+([^@\s]+)@[^@\+\s]+\+[0-9]+\s+\S+\s+([0-9]+)\s+(\S+)\s+([0-9]+)\s+[0-9:]+\s*$!) {
	    my ($rank, $owner, $jobid, $file, $size) = ($1, $2, $3, $4, $5);
	    if (defined($opt_l)) {
		my $owner_rank = "$owner: $rank";
		if (length($owner_rank) > 40) {
		    $owner_rank = substr($owner_rank, 0, 40);
		}
		if (length($file) > 40) {$file = substr($file, 0, 40);}
		print sprintf("\n%-40s [job %d]\n\t%-40s %d bytes\n",
			      $owner_rank, $jobid, $file, $size);
	    } else {
		if (length($rank) > 6) {$rank = substr($rank, 0, 6)};
		if (length($owner) > 8) {$owner = substr($owner, 0, 8)};
		if (length($file) > 37) {$file = substr($file, 0, 37)};
		print sprintf("%-6s %-8s % 6d %-37s %d bytes\n",
			      $rank, $owner, $jobid, $file, $size);
	    }
	} elsif ($line =~ m!\s*Rank\s+Owner!) {
	    if (!defined($opt_l)) {
		print "Rank   Owner       Job File(s)                               Total Size\n";
	    }
	} else {
	    print("$line\n");
	}
    }
}

sub remove_lprng {
    my ($config) = $_[0];
    
    # Remove a job with the standard "lprm" command and emulate the "-"
    # option of the lprm command of BSD LPD

    # Removing command
    my $commandline = "$sysdeps->{'lpd-lprm'}";

    # Add the printer queue argument
    if (defined($config->{'queue'})) {
	$commandline .= " -P $config->{'queue'}";
    }

    # Replace the "-" option by the "all" option
    my $alljobs = "";
    for ($i = 0; ($i <= $#ARGV); $i++) {
	if ($ARGV[$i] =~ m!^\s*\-\s*$!) {
	    $alljobs = " all";
	    splice(@ARGV,$i,1);
	    $i--;
	}
    }
    $commandline .= $alljobs;

    # Add the remaining command line arguments, they are the numbers
    # of the jobs to kill, the users whose jos to remove and also
    # spooler-specific options

    $commandline .= " @ARGV";

    # Do it!

    #print "$commandline\n";
    return (system $commandline) >> 8;

}

sub control_lprng {

    # The lpc command of lprng is compatible to the one of LPD, it has only
    # many more commands. So we use the "control_lpd" function also for
    # lprng.

}

### Printing/Job manipulation functions for CUPS

sub print_cups {
    my ($config) = $_[0];

    # Printing command
    my $commandline = "$sysdeps->{'cups-lpr'}";

    # Add the printer queue argument
    if (defined($config->{'queue'})) {
	$commandline .= " -P $config->{'queue'}";
    }

    # Add the driver-specific options supplied by the user, if any
    if ($#{$config->{'options'}} >= 0) {
	for (@{$config->{'options'}}) {
	    $commandline .= " -o $_";
	}
    }

    # Add the remaining command line arguments, they are the names of
    # the files to print and also spooler-specific options
    $commandline .= " @ARGV";

    # Do it!

    #print "$commandline\n";
    return (system $commandline) >> 8;

}

sub query_cups {
    my ($config) = $_[0];

    # List the jobs on the specified queue
    my $queue = "";
    if (defined($config->{'queue'})) {
	$queue = " -P $config->{'queue'}";
    }
    return (system "$sysdeps->{'cups-lpq'}$queue @ARGV") >> 8;

}

sub remove_cups {
    my ($config) = $_[0];
    
    # Remove a job with the standard "lprm" command

    # Removing command
    my $commandline = "$sysdeps->{'cups-lprm'}";

    # Add the printer queue argument
    if (defined($config->{'queue'})) {
	$commandline .= " -P $config->{'queue'}";
    }

    # Add the remaining command line arguments, they are the numbers
    # of the jobs to kill, the users whose jos to remove and also
    # spooler-specific options

    $commandline .= " @ARGV";

    # Do it!

    #print "$commandline\n";
    return (system $commandline) >> 8;

}

sub control_cups {
    my ($config) = $_[0];

    # CUPS has no LPD/LPRng-compatible lpc command, so we must emulate
    # this functionality with the command line tools of CUPS.

    # The first command line argument (of the remaining ones) is the
    # control command (standard commands of lpc for LPD/LPRng)

    my $command = shift (@ARGV);

    if (!defined($command)) {
	die "You must supply a control command with the \"-C\" option!\n";
    } elsif (lc($command) eq "up") { # Turn on queue (queueing/printing)
	return (system "$sysdeps->{'cups-enable'} @ARGV; $sysdeps->{'cups-accept'} @ARGV") >> 8;
    } elsif (lc($command) eq "down") { # Turn off queue (queueing/printing)
	return (system "$sysdeps->{'cups-disable'} @ARGV; $sysdeps->{'cups-reject'} @ARGV") >> 8;
    } elsif (lc($command) eq "start") { # Turn on queue (printing)
	return (system "$sysdeps->{'cups-enable'} @ARGV") >> 8;
    } elsif (lc($command) eq "stop") { # Turn off queue (printing)
	return (system "$sysdeps->{'cups-disable'} @ARGV") >> 8;
    } elsif (lc($command) eq "enable") { # Accept new jobs
	return (system "$sysdeps->{'cups-accept'} @ARGV") >> 8;
    } elsif (lc($command) eq "disable") { # Reject new jobs
	return (system "$sysdeps->{'cups-reject'} @ARGV") >> 8;
    } elsif (lc($command) eq "move") { # Move jobs
	if (($#ARGV < 1) or ($#ARGV > 2)) {
	    die "Usage of the \"move\" control command:\n\n   move oldqueue [ jobID ] newqueue\n\n";
	}
	# The first argument is always the source printer
	my $fromqueue = shift (@ARGV);
	# The second argument is the job ID or the destination
	my $jobid = shift (@ARGV);
	# The third argument is the destination
	my $toqueue = shift (@ARGV);
	if (!defined($toqueue)) {
	    # No job ID given, move all jobs in the given queue
	    $toqueue = $jobid;
	    open LINES, "$sysdeps->{'cups-lpq'} -P $fromqueue |";
	    my @lines = <LINES>;
	    close LINES;
	    for (@lines) {
		if ($_ =~ m!^\s*\S+\s+\S+\s+([0-9]+)\s+!) {
		    system "$sysdeps->{'cups-lpmove'} $fromqueue-$1 $toqueue";
		}
	    }
	    return;
	} else {
	    # Treat the specified job
	    return (system "$sysdeps->{'cups-lpmove'} $fromqueue-$jobid $toqueue") >> 8;
	}
    } elsif ((lc($command) eq "hold") ||    # Hold job
	     (lc($command) eq "release") || # Resume job
	     (lc($command) eq "topq")) {    # Bring job to the top of the
	                                    # queue
	if (($#ARGV < 0) or ($#ARGV > 1)) {
	    die "Usage of the \"$command\" control command:\n\n   $command queue [ jobID ] \n\n";
	}
	# Clean up the command
	$command = lc($command);
	if ($command eq "release") {$command = "resume";} 
	if ($command eq "topq") {$command = "immediate";} 
	# The first argument is always the queue
	my $queue = shift (@ARGV);
	# The second argument is the job ID
	my $jobid = shift (@ARGV);
	if (!defined($jobid)) {
	    # No job ID given, treat all jobs in the given queue
	    open LINES, "$sysdeps->{'cups-lpq'} -P $queue |";
	    my @lines = <LINES>;
	    close LINES;
	    for (@lines) {
		if ($_ =~ m!^\s*\S+\s+\S+\s+([0-9]+)\s+!) {
		    system "$sysdeps->{'cups-lp'} -i $queue-$1 -H $command";
		}
	    }
	    return;
	} else {
	    # Treat the specified job
	    return (system "$sysdeps->{'cups-lp'} -i $queue-$jobid -H $command") >> 8;
	}
    } elsif (lc($command) eq "status") { # Queue status listing
	return (system "$sysdeps->{'cups-lpc'} status @ARGV") >> 8;
    } elsif (lc($command) eq "help") { # List the available commands
	print "The following control commands are available:\n\n";
	print "   up queue                : Turn on queue (queueing/printing)\n";
	print "   down queue              : Turn off queue (queueing/printing)\n";
	print "   start queue             : Turn on printing on queue\n";
	print "   stop queue              : Turn off printing on queue\n";
	print "   enable queue            : Make queue accepting new jobs\n";
	print "   disable queue           : Make queue rejecting new jobs\n";
	print "   move oldqueue [ jobid ] newqueue : \n";
	print "       Move job jobid in oldqueue to newqueue\n";
	print "       Move all jobs in oldqueue to newqueue when jobid not given\n";
	print "   hold queue [ jobid ]    : Hold job jobid or all jobs in queue\n";
	print "   release queue [ jobid ] : Release job jobid or all jobs in queue\n";
	print "   topq queue jobid        : Print job jobid in queue immediately\n";
	print "   status [ queue ]        : Status of queue or of all queues\n";
	print "   help                    : This help message\n\n";
    } else {
	die "Command \"$command\" not recognized!\n";
    }
	
}

### Printing/Job manipulation functions for PDQ

sub print_pdq {
    my ($config) = $_[0];

    # Printing command
    my $commandline = "$sysdeps->{'pdq-print'}";

    # Add the printer queue argument
    if (defined($config->{'queue'})) {
	$commandline .= " -P $config->{'queue'}";
    }

    # Add the driver-specific options supplied by the user, if any
    if ($#{$config->{'options'}} >= 0) {
	for (@{$config->{'options'}}) {
	    my $option = $_;
	    if ($option =~ m!^\s*([^=]+=[\+\-0-9\.]+)\s*$!) {
		# Foomatic treats numerical options as PDQ arguments ("-a"),
		# but there can be enumerated options with numbers as choices,
		# so we give the option in both styles. Since PDQ silently
		# ignores non-existent options, the wrong form of the option
		# will be ignored.
		$commandline .= " -aOPT_$1";
	    }
	    # Enumerated and boolean options are PDQ options ("-o"),
	    # the "=" has to be replaced by "_" to work with the
	    # PDQ-O-MATIC-generated configuration
	    $option =~ s/=/_/;  # Replace only the first "="
	    $commandline .= " -o$option";
	}
    }

    # The "-#" option for multiple copies is not supported by the print
    # command "pdq". So we launch "pdq" once per copy. Thw command line
    # will be modified appropriately directly before the printing command
    # is launched.
    # Note: '#' as option name is not supported by the Perl library
    # Getopt::Long.
    my $num_copies = 1;
    my $file_in_args = 0;
    my $i;
    for ($i = 0; ($i <= $#ARGV); $i++) {
	if ($ARGV[$i] =~ m!^\s*\-\#\s*([0-9]+)\s*$!) {
	    $num_copies = $1;
	    splice(@ARGV,$i,1);
	    $i--;
	} elsif ($ARGV[$i] =~ m!^\s*\-\#\s*$!) {
	    if ((defined $ARGV[$i+1]) && 
		($ARGV[$i+1] =~ m!^\s*([0-9]+)\s*$!)) {
		$num_copies = $1;
		splice(@ARGV,$i,2);
		$i--;
	    }
	} elsif ($ARGV[$i] =~ m!^\s*[^\-]+!) {
	    $file_in_args = 1;
	}
    }

    # Add the remaining command line arguments, they are the names of
    # the files to print and also spooler-specific options
    $commandline .= " @ARGV";

    # Do it!
    #print "$commandline\n"; return 0;

    if ($num_copies == 1) {
	return (system $commandline) >> 8;
    } else {
	if ($file_in_args == 0) {
	    # We print from standard input, so we must buffer it to be able
	    # to print multiple copies
	    my @job_contents = <STDIN>;
	    my $i;
	    for ($i = 0; $i < $num_copies; $i++) {
		open PIPE, "| $commandline" || 
		    die "Could not launch printing command!\n";
		print PIPE @job_contents;
		close PIPE;
	    }
	    return 0;
	} else {
	    # We print files
	    my $result = 0;
	    my $i;
	    for ($i = 0; $i < $num_copies; $i++) {
		$result = (system $commandline) >> 8;
		if ($result != 0) {return $result};
	    }
	    return 0;
	}
    }
}

sub query_pdq {
    my ($config) = $_[0];

    # PDQ has no possiblity to list the printing jobs from the command
    # line.  So we read the *.status files in ~/.printjobs and generate
    # the job entry lines from that information.

    # Read additional options
    GetOptions("a"   => \$opt_a,        # List jobs on all printers
	       "l"   => \$opt_l);       # Long, more verbose output

    # Make sure that a printer is specified when the "-a" option is not
    # given
    if ((!(defined($opt_a))) && (!(defined($config->{'queue'})))) {
	$config->{'queue'} = get_pdq_default_printer();
    }

    # If the user specified job numbers, list them. User names on the
    # command line do not make much sense, because under PDQ a user can
    # only see ones own jobs, they are supported here to do not break
    # front ends
    my $joblist = {};
    my $userlist = {};
    my $listalljobs = 1;
    my $listallusers = 1;
    my $i;
    for ($i = 0; ($i <= $#ARGV); $i++) {
	if ($ARGV[$i] =~ m!^\s*([0-9]+)\s*$!) {
	    my $job=$1;
	    # Fill up the number with zeros so that it has three digits
	    while (length($job) < 3) {$job = "0" . $job;}
	    $joblist->{$job} = 1;
	    $listalljobs = 0;
	    splice(@ARGV,$i,1);
	    $i--;
	} elsif ($ARGV[$i] =~ m!^\s*[^\-]+!) {
	    my $user=$ARGV[$i];
	    $userlist->{$user} = 1;
	    $listallusers = 0;
	    splice(@ARGV,$i,1);
	    $i--;
	} else {
	    die "Unknown option: $ARGV[$i]\n";
	}
    }

    # When we list only the jobs for a specific printer, display the
    # printer status at first. In PDQ the printer status cannot be
    # retrived from the command line, so we put a dummy line
    # "<printer> is ready".
    if (!(defined($opt_a))) {
	if (!pdq_check_printer($config->{'queue'})) {
	    die "$config->{'queue'}: unknown printer\n";
	}
	print "$config->{'queue'} is ready\n";
    }

    # Read in the names of all job status files in ~/.printjobs/
    my @jobnumbers = ();
    opendir PJDIR, "$ENV{'HOME'}/.printjobs" || 
	return 0;  # No ~/.printjobs/ directory ==> no jobs
    while ($filename = readdir(PJDIR)) {
	if ($filename =~ m!^([0-9][0-9][0-9]).status$!) {
	    push (@jobnumbers, $1);
	}
    }
    close PJDIR;
    # Sort the filenames in descending order to get the most recent jobs
    # listed at first
    @jobnumbers = sort {$b cmp $a} @jobnumbers;

    # Now list the jobs
    my $firstline = 1;
    for ($i = 0; $i <= $#jobnumbers; $i ++) {
	# Omit this job if job numbers are specified on the command line, but
	# not the one of this job
	next if (($listalljobs == 0) && 
		 (!(defined($joblist->{$jobnumbers[$i]}))));
	# Read the job status file
	next if !open JOBSTATUSFILE, 
	"< $ENV{'HOME'}/.printjobs/$jobnumbers[$i].status";
	my $jobstatusdata = join("", <JOBSTATUSFILE>);
	close JOBSTATUSFILE;
	# Extract the important fields from the file
	# Status:
	my $status = "";
	if ($jobstatusdata =~ m!^\s*status\s*\=\s*{([^{}]*)}\s*$!m) {
	    $status = $1;
	}
	# Omit this job when it has no status field or when the job is
	# already finished, cancelled, or aborted
	next if (($status eq "") || ($status =~ m!aborted!) || 
		 ($status =~ m!finished!) || ($status =~ m!cancelled!));
	# Avoid spaces in the status field, so that frontends can separate the
	# fields from the job list more easily.
	$status =~ s/\s//g;
	# Printer
	my $printer;
	if ($jobstatusdata =~ m!^\s*printer\s*\=\s*{([^{}]*)}\s*$!m) {
	    $printer = $1;
	}
	# Omit this job when we are querying only the jobs of another printer
	next if ((!(defined($opt_a))) && ($printer ne $config->{'queue'}));
	# Owner
	my $owner;
	if ($jobstatusdata =~ 
	m!^\s*env_driver\s*\=\s*{.*\"LOGNAME\"\s*=\s*\"([^\"]*)\".*}\s*$!m) {
	    $owner = $1;
	}
	# Omit this job if user names are specified on the command line, but
	# not the owner of this job
	next if (($listallusers == 0) && 
		 (!(defined($userlist->{$owner}))));
	# File
	my $file;
	if ($jobstatusdata =~ m!^\s*input_filename\s*\=\s*{([^{}]*)}\s*$!m) {
	    $file = $1;
	}
	# Size of job input file
	my $size;
	if (-f "$ENV{'HOME'}/.printjobs/$jobnumbers[$i].raw") {
	    $size = (stat("$ENV{'HOME'}/.printjobs/$jobnumbers[$i].raw"))[7];
	}

	# Now get the info nicely onto the screen
	my $outputline;
	if ($opt_l) {
	    # Long (3+ lines per job) mode
	    my $owner_status = "$owner: $status";
	    if (length($owner_status) > 40) {
		$owner_status = substr($owner_status, 0, 40);
	    }
	    if (length($file) > 40) {$file = substr($file, 0, 40);}
	    $outputline = sprintf("\n%-40s [job %d]\n\t%-40s %d bytes\n",
				  $owner_status, $jobnumbers[$i], $file,
				  $size);
	} else {
	    # Short (1 line per job) mode
	    if ($firstline == 1) {
		# headline
		print "Rank   Owner      Job  File(s)                               Total Size\n";
		$firstline = 0;
	    }
	    if (length($status) > 6) {$status = substr($status, 0, 6);}
	    if (length($owner) > 10) {$owner = substr($owner, 0, 10);}
	    if (length($file) > 37) {$file = substr($file, 0, 37);}
	    $outputline = sprintf("%-6s %-10s % 3d  %-37s %d bytes\n",
				  $status, $owner, $jobnumbers[$i], $file,
				  $size);
	}
	print $outputline;
    }

    # Say "no entries" if no job was listed
    if ($firstline == 1) {
	print "no entries\n";
    }
    
}

sub remove_pdq {
    my ($config) = $_[0];

    # PDQ has no possiblity to remove printing jobs from the command
    # line.  "xpdq" cancels jobs by "touch"ing <job id>.cancelled
    # files in ~/.printjobs and setting the permissions of these files
    # to 0600.

    # Make sure that a printer is specified when the "-a" option is not
    # given
    if (!(defined($config->{'queue'}))) {
	$config->{'queue'} = get_pdq_default_printer();
    }

    # If the user specified job numbers, list them. User names on the
    # command line do not make much sense, because under PDQ a user can
    # only see ones own jobs, they are supported here to do not break
    # front ends
    my $joblist = {};
    my $userlist = {};
    my $nojob = 1;
    my $nouser = 1;
    my $opt_alljobs = 0;
    my $i;
    for ($i = 0; ($i <= $#ARGV); $i++) {
	if ($ARGV[$i] =~ m!^\s*([0-9]+)\s*$!) {
	    my $job=$1;
	    # Fill up the number with zeros so that it has three digits
	    while (length($job) < 3) {$job = "0" . $job;}
	    $joblist->{$job} = 1;
	    $nojob = 0;
	    splice(@ARGV,$i,1);
	    $i--;
	} elsif ($ARGV[$i] =~ m!^\s*[^\-]+!) {
	    my $user=$ARGV[$i];
	    $userlist->{$user} = 1;
	    $nouser = 0;
	    splice(@ARGV,$i,1);
	    $i--;
	} elsif ($ARGV[$i] =~ m!^\s*\-\s*$!) {
	    $opt_alljobs = 1;
	    splice(@ARGV,$i,1);
	    $i--;
	} else {
	    die "Unknown option: $ARGV[$i]\n";
	}
    }

    # Does the chosen printer exist
    if (!pdq_check_printer($config->{'queue'})) {
	die "$config->{'queue'}: unknown printer\n";
    }

    # Read in the names of all job status files in ~/.printjobs/
    my @jobnumbers = ();
    opendir PJDIR, "$ENV{'HOME'}/.printjobs" || 
	return 0;  # No ~/.printjobs/ directory ==> no jobs
    while ($filename = readdir(PJDIR)) {
	if ($filename =~ m!^([0-9][0-9][0-9]).status$!) {
	    push (@jobnumbers, $1);
	}
    }
    close PJDIR;
    # Sort the filenames in descending order to get the most recent
    # (probably still waiting) jobs removed at first
    @jobnumbers = sort {$b cmp $a} @jobnumbers;

    # Now search the jobs to remove
    my $nothingremoved = 1;
    my $mostrecent = 1;
    for ($i = 0; $i <= $#jobnumbers; $i ++) {
	# Read the job status file
	next if !open JOBSTATUSFILE, 
	"< $ENV{'HOME'}/.printjobs/$jobnumbers[$i].status";
	my $jobstatusdata = join("", <JOBSTATUSFILE>);
	close JOBSTATUSFILE;
	# Extract the important fields from the file
	# Status:
	my $status = "";
	if ($jobstatusdata =~ m!^\s*status\s*\=\s*{([^{}]*)}\s*$!m) {
	    $status = $1;
	}
	# Omit this job when it is already finished, cancelled, or aborted
        # (then it cannot be killed any more)
	next if (($status eq "") || ($status =~ m!aborted!) || 
		 ($status =~ m!finished!) || ($status =~ m!cancelled!));
	# Printer
	my $printer;
	if ($jobstatusdata =~ m!^\s*printer\s*\=\s*{([^{}]*)}\s*$!m) {
	    $printer = $1;
	}
	# Omit this job when we want to remove jobs on another printer
	next if ((!(defined($opt_a))) && ($printer ne $config->{'queue'}));
	# Owner
	my $owner;
	if ($jobstatusdata =~ 
	m!^\s*env_driver\s*\=\s*{.*\"LOGNAME\"\s*=\s*\"([^\"]*)\".*}\s*$!m) {
	    $owner = $1;
	}

	# Kill the job when it is in the scope of jobs defined by the
	# command line
	if ((($nojob == 0) && (defined($joblist->{$jobnumbers[$i]}))) || 
	    (($nouser == 0) && (defined($userlist->{$owner}))) ||
	    (($opt_alljobs == 1) && ($ENV{'LOGNAME'} eq $owner)) ||
	    (($opt_alljobs == 1) && ($ENV{'LOGNAME'} eq "root")) ||
	    (($mostrecent == 1) && ($nojob == 1) && ($nouser == 1) && 
	     ($opt_alljobs == 0))) {
	    system("touch $ENV{'HOME'}/.printjobs/$jobnumbers[$i].cancelled; chmod 0600 $ENV{'HOME'}/.printjobs/$jobnumbers[$i].cancelled");
	    print STDERR "Cancel request for job $jobnumbers[$i] submitted!\n";
	    $nothingremoved = 0;
	}
	$mostrecent = 0;
    }

    # Say "No cancel request sent" if no job was killed
    if ($nothingremoved == 1) {
	print STDERR "no cancel request sent\n";
    }
}

sub control_pdq {

    # PDQ does not have functionality for enabling/disabling queues, 
    # holding/releasing/moving jobs, etc.

    die "Advanced queue/job manipulation functionality is not supported under PDQ!\n";

}

sub get_pdq_default_printer {

    # Read the help message of PDQ
    open PDQHELP, "pdq --help 2>&1 |";
    $pdqhelp = join ("", <PDQHELP>);
    close PDQHELP;

    # Search the "default" line
    if ($pdqhelp =~ m!default\s+printer.*\s+(\S+)\s*$!mg) {
	return $1;
    } else {
	die "No default printer defined, you have to specify a printer with \"-P\" or \"-d\"!\n";
    }

}

sub pdq_check_printer {
    my $printer = $_[0];

    # Read the help message of PDQ
    open PDQHELP, "pdq --help 2>&1 |";
    $pdqhelp = join ("", <PDQHELP>);
    close PDQHELP;

    # Search the appropriate printer entry
    return ($pdqhelp =~ m!^\s+$printer\s+\-\s+.*\s+\-\s*$!mg);

}

sub detect_spooler {
    # If tcp/localhost:631 opens, cups
    my $page = getpage('http://localhost:631/', 1);
    if ($page =~ m!CUPS!) {
	return 'cups';
    }

    # Else if /etc/printcap, some sort of lpd thing
    if (-f $sysdeps->{'lpd-pcap'}) {
	# If -f /etc/lpd.conf, lprng
	if (-f $sysdeps->{'lprng-conf'}) {
	    return 'lprng';
	} elsif (-x $sysdeps->{'lpd-bin'}) {
	    # There's a /usr/sbin/lpd
	    return 'lpd';
	}
    }

    # pdq executable in our path somewhere?
    for (split(':', $ENV{'PATH'})) {
	if (-x "$_/pdq") {
	    return 'pdq';
	}
    }

    return undef;
}

sub unimp {
    die "Sorry, $action for your spooler is unimplemented...\n";
}

sub help {

    my $action = 'all';
# Set up the help message depending on how we were called
    if ($progname =~ m!^lpc!) { # 'lpc*' ==> control 
	$action = 'control';
	print STDERR <<EOF;
Usage: $progname [ -s spooler ] [ -i ] command [ arguments ]
    or $progname -h
EOF
    } elsif ($progname =~ m!^lprm!) { # 'lprm*' ==> remove jobs 
	$action = 'remove';
	print STDERR <<EOF; 
Usage: $progname [ -s spooler ] [ -P queuename ] [ - ] [ -i ] [ jobid1 jobid2 ... ]
    or $progname -h
EOF
    } elsif ($progname =~ m!^lpq!) { # 'lpq*' ==> list jobs 
	$action = 'query';
	print STDERR <<EOF;
Usage: $progname [ -s spooler ] [ -P queuename ] [ -i ] [ -a ] [ user1 user2 ... ]
    or $progname -h
EOF
    } elsif (($progname =~ m!^lpr!) || ($progname =~ m!^lp!)) { # 'lpr*', 'lp*' ==> print 
	$action = 'print';
	print STDERR <<EOF;
Usage: $progname [ -s spooler ] [ -P queuename ] \
               [ -o option1=value1 -o option2 ... ] [ -i ] [ file1 file2 ... ]
    or $progname -S [ -s spooler ] [ -i ]
    or $progname -h [ -s spooler ] [ -P queuename ] [ -i ]
EOF
    } else { # name does not determine the action
	print STDERR <<EOF;
Usage: $progname [ -s spooler ] [ -P queuename ] \
               [ -o option1=value1 -o option2 ... ] [ -i ] \
	       [ file1 file2 ... ]
    or $progname -Q [ -s spooler ] [ -P queuename ] [ -i ] [ -a ] \
	       [ user1 user2 ... ]
    or $progname -R [ -s spooler ] [ -P queuename ] [ - ] [ -i ] \
	       [ jobid1 jobid2 ... ]
    or $progname -C [ -s spooler ] [ -i ] command [ arguments ]
    or $progname -S [ -s spooler ] [ -i ]
    or $progname -h [ -s spooler ] [ -P queuename ] [ -i ]
EOF
    }

     print STDERR <<EOF;

 -s spooler      Explicit spooler type (cups,lpd,lprng,pdq)
EOF

    if ($action ne 'control') {
	print STDERR <<EOF;
 -P queuename    Command should apply to this queue
EOF
    }

    if (($action eq 'print') || ($action eq 'all')) {
	print STDERR <<EOF;
 -o option=value Set option to value
 -o option       Set the switch option
 -\# n            Print n copies
 file1 file2 ... Files to be printed, when no file is given, standard input
                 will be printed
EOF
    }

    if ($action eq 'all') {
	print STDERR <<EOF;
 -Q              Query the jobs in a queue
EOF
    }

    if (($action eq 'query') || ($action eq 'all')) {
	print STDERR <<EOF;
 -a              Query the jobs in all queues
 user1 user2 ... Users whose jobs should be listed
EOF
    }

    if ($action eq 'all') {
	print STDERR <<EOF;
 -R              Remove a job from a queue
EOF
    }

    if (($action eq 'remove') || ($action eq 'all')) {
	print STDERR <<EOF;
 -               Remove all your jobs
 jobid1 jobid2   IDs of the jobs to be removed
EOF
    }

    if ($action eq 'all') {
	print STDERR <<EOF;
 -C              Execute control commands for queue/job manipulation
EOF
    }

    if (($action eq 'control') || ($action eq 'all')) {
	print STDERR <<EOF;
 command [ arguments ]  Control command for queue/job manipulation. The 
                 commands are the ones of the BSD "lpc" utility. Use
		 the control command "help" to get a list of supported 
                 commands. Note: the amount of commands varies with the
                 spooler, but the same commands given under different 
                 spoolers do the same thing.
EOF
    }

    print STDERR <<EOF;
 -i              Interactive mode: You will be asked if $progname
                 is in doubt about something. Otherwise $progname
		 uses auto-detection or quits with an error.
EOF

    if (($action eq 'print') || ($action eq 'all')) {
	print STDERR <<EOF;
 -S              Save the chosen spooler as the default spooler
 -h              Show this message or show a list of available options if a 
                 queue is specified

EOF
    } else {
	print STDERR <<EOF;
 -h              Show this message

EOF
    }

    exit 0;
}

# Help on printer-specific options
sub help_options {
    my ($config) = $_[0];
    
    # Is there an easier way to do this?
    eval `foomatic-configure -P -n $config->{'queue'} -s $config->{'spooler'}`;
    print "Available options for queue $config->{'queue'}:\n";
    
    foreach my $arg (@{$QUEUES[0]->{'args'}}) {
        next if $arg->{'hidden'};
        my @vals = ();

        print "  $arg->{'name'} : < ";
        foreach my $val (@{$arg->{'vals'}}) {
            push @vals, $val->{'value'};
        }
        print join(' | ', @vals) . " >\n";
    }

    exit 0;
}