Debian Codegraph @

extract_pjl

raw
1 #!/usr/bin/perl
2 use strict;
3 use FileHandle;
4 use Getopt::Std;
5 use IO::Select;
6 use IO::Socket::INET;
7
8 sub usage(){
9 print <<EOF;
10 use: $0 [-h host][-p port][-t timeout][pjlsourcefiles]
11 Read the specified input files
12 and produce a PJL summary
13 Or send a PJL sequence to the remote host and port.
14
15 $0 -h host [-p port][-t timeout][-v]
16 $0 [pjlsourcefiles]
17
18 -h host - remote host
19 -p port - port
20 -t timeout - timeout for no input
21 -v - just show data from remote host
22
23 PJL commands to request the printer information
24 \\033%-12345X
25 \@PJL
26 \@PJL INFO VARIABLES
27 \@PJL INFO ID
28 \@PJL INFO CONFIG
29 \\033%-12345X
30
31 If there is no additional information after timeout seconds
32 the connection is closed.
33
34 PJL Infomration has format:
35 \@PJL INFO VARIABLES
36 COPIES=1 [2 RANGE]
37 1
38 999
39 PAPER=LETTER [19 ENUMERATED]
40 LETTER
41 LETTERR
42 LEGAL
43 FOOLSCAP
44 INVOICE
45 ...
46 \@PJL ... < end of section unless it is \@PJL INFO VARIABLES
47
48 We produce:
49 pjloption=(fixedname)STRING
50 pjloption=(fixedname)RANGE;low,high
51 pjloption=(fixedname)ENUMERATED;v1,v2,...
52
53 From the returned information
54 EOF
55 exit(1);
56 }
57
58 $| = 1;
59
60 my(@lines,$line,$inbody,$outputline,$port, $host,$timeout,$verbose);
61 my $inoptionlisting = 0; # 1 if we are after a line "\@PJL INFO VARIABLES"
62 # and no other "\@PJL ..." line appeared
63 my $option = ""; # Name of current option
64 my $optiontype = -1; # 0: enum, 1: int, 2: float, -1: not found yet
65 my $numchoices = -1; # Number of choices, -1: not found yet
66 my $default = ""; # Default setting
67 my @choices = (); # List of possible choices
68 my $choicesfound = 0; # How many choices were already found
69
70 my $opt = {};
71 getopts("vdDp:h:t:",$opt) || usage();
72
73 $port = 9100;
74 $timeout = 3;
75 $verbose = 0;
76
77 my $debug = 0;
78 $debug = 1 if $opt->{d};
79 $debug = 2 if $opt->{D};
80 $host = $opt->{h} if $opt->{h};
81 $port = $opt->{p} if $opt->{p};
82 $timeout = $opt->{t} if $opt->{t};
83 $verbose = 1 if $opt->{v};
84
85 if( $host ){
86 my $socket = IO::Socket::INET->new(
87 PeerAddr=>$host,
88 PeerPort=>$port,
89 Proto=>"tcp",
90 Type=>SOCK_STREAM)
91 or die "could not connect to $host, port $port";
92 print $socket
93 "\033%-12345X" .
94 "\@PJL\r\n" .
95 "\@PJL INFO VARIABLES\r\n" .
96 "\@PJL INFO ID\r\n" .
97 "\@PJL INFO CONFIG\r\n" .
98 "\033%-12345X"
99 or die "write to socket failed";
100 my $s = IO::Select->new();
101 my $in = "";
102 my $input;
103 while(1){
104 $s->add($socket);
105 print "starting wait, timeout $timeout\n" if $debug;
106 my @ready = $s->can_read($timeout);
107 print "ready '" . scalar @ready . "'\n" if $debug;
108 last if( @ready == 0 );
109 my $len = sysread $socket, $input, 1024;
110 print "Read $len\n" if $debug > 1;
111 last if not $len;
112 $input =~ s/[\015\014]//g;
113 print " INPUT '$input'\n" if $debug > 1;
114 $in .= $input;
115 }
116 close($socket);
117 @lines = map( "$_\n", split("\n",$in ));
118 } else {
119 @lines = <> or die "No input";
120 }
121 print "Input " . join("", @lines) . "\n" if $debug;
122
123 if( $verbose ){
124 print join("", @lines);
125 exit(0);
126 }
127
128 $inbody = 0;
129 $outputline = "";
130 foreach (@lines){
131 chomp;
132 s/\015//;
133 $line = $_;
134 print "# INPUT $_ \n" if $debug;
135 if( /^\@PJL/ ){
136 if( /^\@PJL\s+INFO\s+VARIABLES/ ){
137 $inbody = 1;
138 } else {
139 $inbody = 0;
140 }
141 }
142 if( not $inbody and $outputline ){
143 print STDERR "# Unexpected end of information\n";
144 print $outputline . "\n";
145 exit(1);
146 }
147 next if( not $inbody );
148 if ($line =~ m/^\s*([\w\s:]*)\s*=\"?([\w\.]*)\"?\s*\[([0-9]*)\s*(\w*)\s*(\w*)\]/) {
149 # Option header
150 if( $outputline ){
151 print STDERR "# Incomplete option '$outputline'\n";
152 exit(1);
153 }
154 my $typestr;
155 my $modifierstr;
156 ($option, $default, $numchoices, $typestr, $modifierstr) =
157 ($1, $2, $3, $4, $5);
158 print "# Option: '$option', Default '$default', Numchoices '$numchoices', Typestr '$typestr', Modifier '$modifierstr'\n"
159 if $debug;
160 if ($modifierstr eq "READONLY") { # Ignore READONLY options
161 $numchoices = -2;
162 print "# WARNING: Read-only option '$option', ignored!\n";
163 next;
164 }
165 $choicesfound = 0;
166 @choices = ();
167 my $optstr = $option;
168 $optstr =~ s/\W/_/g;
169 $outputline = $optstr . "=";
170 if( $optstr ne $option ){
171 $optstr = "($option)";
172 $optstr =~ s/\s/+/g;
173 $outputline .= $optstr;
174 }
175 if (($modifierstr eq "STRING") || ($typestr eq "STRING")) {
176 $outputline .= "STRING;";
177 print $outputline . "\n";
178 $outputline = "";
179 $numchoices = -2;
180 next;
181 }
182 if ($numchoices == 1 && $typestr eq "ENUMERATED") { # Ignore enumerated with 1 choice
183 $numchoices = -2;
184 print "# WARNING: enumerated option '$option' with one choice, ignored!\n";
185 $outputline = "";
186 $numchoices = -2;
187 next;
188 }
189 $outputline .= "$typestr;";
190 } else {
191 $choicesfound = @choices;
192 print "# option: '$line', numchoices '$numchoices', found '$choicesfound'\n" if $debug>1;
193 next if( $numchoices < 0 );
194 $line =~ s/\s//g;
195 next if( $line eq "" );
196 if( not $numchoices or ($numchoices <= $choicesfound) ){
197 print "# Unexpected option '$line'\n";
198 next;
199 }
200 push @choices, $line;
201 if( @choices == $numchoices ){
202 print $outputline . join(",",@choices). "\n";
203 $outputline = "";
204 }
205 }
206 }
207 if( $outputline ){
208 print STDERR "# Unexpected EOF\n";
209 print $outputline . "\n";
210 exit(1);
211 }