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
|
#!@PERL@
use English;
use IO::Socket;
my $JSUCC = 0;
my $JABORT = 33;
my $JNOSPOOL = 38;
my $JNOPRINT = 39;
my $debug = 0;
my $optind;
# pull out the options
my($key,$value,$opt,$long,$opt_c);
for( $i = 0; $i < @ARGV; ++$i ){
$opt = $ARGV[$i];
print STDERR "XX opt= $opt\n" if $debug;
if( $opt eq '-c' ){
$opt_c = 1;
} elsif( ($key, $value) = ($opt =~ /^-(.)(.*)/) ){
if( $value eq "" ){
$value = $ARGV[++$i];
}
${"opt_$key"} = $value;
print STDERR "XX opt_$key = " . ${"opt_$key"} . "\n" if $debug;
} else {
$optind = $i;
last;
}
print STDERR "XX opt_P = $opt_P\n" if $debug;
}
$long = 0; # short
if( defined($opt_T) ){
print STDERR "XX CHECK_REMOTE opt_T=$opt_T\n" if $debug;
if( $opt_T =~ /debug/ ){
$debug = 1;
}
if( $opt_T =~ /short/ ){
$long = 1;
}
if( $opt_T =~ /long/ ){
$long = 0;
}
}
print STDERR "XX CHECK_REMOTE opt_P=$opt_P\n" if $debug;
print STDERR "XX CHECK_REMOTE " . join(" ",@ARGV) . "\n" if $debug;
if( !defined($opt_P) ){
print STDERR "$0: no -P value\n";
exit( $JABORT );
}
$printer = $opt_P;
while( checkstatus( $printer, $long ) ){
print STDERR "XX CHECK_REMOTE sleeping\n" if $debug;
sleep(10);
}
exit $JSUCC;
sub checkstatus {
my ($printer,$long) = @_;
my ($remote,$port);
my ($count, $socket, $line);
if( $long ){
$long = 4;
} else {
$long = 3;
}
if( $printer =~ /@/ ){
($printer,$remote) = $printer =~ m/(.*)@(.*)/;
}
$remote="localhost" unless $remote;
if( $remote =~ /%/ ){
($remote,$port) = $remote =~ m/(.*)%(.*)/;
}
$port = 515 unless $port;
print STDERR "XX CHECK_REMOTE remote='$remote', port='$port', pr='$printer', op='$long'\n" if $debug;
$socket = getconnection( $remote, $port );
$count = -1;
# send the command
printf $socket "%c%s\n", $long, $printer;
while ( defined( $line = <$socket>) && $count < 0 ){
chomp $line;
print STDERR "XX CHECKREMOTE '$line'\n" if $debug;
if( $line =~ /printing disa/ ){
print STDERR "XX CHECKREMOTE printing disable\n" if $debug;
exit $JNOPRINT;
} elsif( $line =~ /spooling disa/ ){
print STDERR "XX CHECKREMOTE printing disable\n" if $debug;
exit $JNOSPOOL;
} elsif( $line =~ /([0-9]*)\s+job.?$/ ){
$count = $1;
print STDERR "XX CHECKREMOTE $count jobs\n" if $debug;
}
}
close $socket;
if( $count < 0 ){
print STDERR "CHECKREMOTE cannot decode status\n";
exit $JABORT;
}
return $count;
}
sub getconnection {
my ($remote,$port) = @_;
my ($socket);
print STDERR "XX CHECK_REMOTE remote='$remote', port=$port\n" if $debug;
$socket = IO::Socket::INET->new(
Proto => "tcp",
PeerAddr => $remote,
PeerPort => $port,
);
if( !$socket ){
print STDERR "CHECK_REMOTE IO::Socket::INET failed - $!\n";
exit $JABORT;
}
$socket->autoflush(1);
$socket;
}
|