Debian Codegraph @

UTILS/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\n" .
95 "\@PJL INFO VARIABLES\n" .
96 "\@PJL INFO ID\n" .
97 "\@PJL INFO CONFIG\n" .
98 "\@PJL ENTER LANGUAGE = PCL\n" .
99 "\033%-12345X"
100 or die "write to socket failed";
101 my $s = IO::Select->new();
102 my $in = "";
103 my $input;
104 while(1){
105 $s->add($socket);
106 print "starting wait, timeout $timeout\n" if $debug;
107 my @ready = $s->can_read($timeout);
108 print "ready '" . scalar @ready . "'\n" if $debug;
109 last if( @ready == 0 );
110 my $len = sysread $socket, $input, 1024;
111 print "Read $len\n" if $debug > 1;
112 last if not $len;
113 $input =~ s/[\015\014]//g;
114 print " INPUT '$input'\n" if $debug > 1;
115 $in .= $input;
116 }
117 close($socket);
118 @lines = map( "$_\n", split("\n",$in ));
119 } else {
120 @lines = <> or die "No input";
121 }
122 print "Input " . join("", @lines) . "\n" if $debug;
123
124 if( $verbose ){
125 print join("", @lines);
126 exit(0);
127 }
128
129 $inbody = 0;
130 $outputline = "";
131 foreach (@lines){
132 chomp;
133 s/\015//;
134 $line = $_;
135 print "# INPUT $_ \n" if $debug;
136 if( /^\@PJL/ ){
137 if( /^\@PJL\s+INFO\s+VARIABLES/ ){
138 $inbody = 1;
139 } else {
140 $inbody = 0;
141 }
142 }
143 if( not $inbody and $outputline ){
144 print STDERR "# Unexpected end of information\n";
145 print $outputline . "\n";
146 exit(1);
147 }
148 next if( not $inbody );
149 if ($line =~ m/^\s*([\w\s:]*)\s*=\"?([\w\.]*)\"?\s*\[([0-9]*)\s*(\w*)\s*(\w*)\]/) {
150 # Option header
151 if( $outputline ){
152 print STDERR "# Incomplete option '$outputline'\n";
153 exit(1);
154 }
155 my $typestr;
156 my $modifierstr;
157 ($option, $default, $numchoices, $typestr, $modifierstr) =
158 ($1, $2, $3, $4, $5);
159 print "# Option: '$option', Default '$default', Numchoices '$numchoices', Typestr '$typestr', Modifier '$modifierstr'\n"
160 if $debug;
161 if ($modifierstr eq "READONLY") { # Ignore READONLY options
162 $numchoices = -2;
163 print "# WARNING: Read-only option '$option', ignored!\n";
164 next;
165 }
166 $choicesfound = 0;
167 @choices = ();
168 my $optstr = $option;
169 $optstr =~ s/\W/_/g;
170 $outputline = $optstr . "=";
171 if( $optstr ne $option ){
172 $optstr = "($option)";
173 $optstr =~ s/\s/+/g;
174 $outputline .= $optstr;
175 }
176 if (($modifierstr eq "STRING") || ($typestr eq "STRING")) {
177 $outputline .= "STRING;";
178 print $outputline . "\n";
179 $outputline = "";
180 $numchoices = -2;
181 next;
182 }
183 if ($numchoices == 1 && $typestr eq "ENUMERATED") { # Ignore enumerated with 1 choice
184 $numchoices = -2;
185 print "# WARNING: enumerated option '$option' with one choice, ignored!\n";
186 $outputline = "";
187 $numchoices = -2;
188 next;
189 }
190 $outputline .= "$typestr;";
191 } else {
192 $choicesfound = @choices;
193 print "# option: '$line', numchoices '$numchoices', found '$choicesfound'\n" if $debug>1;
194 next if( $numchoices < 0 );
195 $line =~ s/\s//g;
196 next if( $line eq "" );
197 if( not $numchoices or ($numchoices <= $choicesfound) ){
198 print "# Unexpected option '$line'\n";
199 next;
200 }
201 push @choices, $line;
202 if( @choices == $numchoices ){
203 print $outputline . join(",",@choices). "\n";
204 $outputline = "";
205 }
206 }
207 }
208 if( $outputline ){
209 print STDERR "# Unexpected EOF\n";
210 print $outputline . "\n";
211 exit(1);
212 }