Debian Codegraph @

contrib/pcl3/src/pclscan.c

raw
1 /******************************************************************************
2 File: $Id: pclscan.c,v 1.8 2000-10-22 11:05:34+02 Martin Rel $
3 Contents: PCL scanner
4 Author: Martin Lottermoser, Greifswaldstrasse 28, 38124 Braunschweig,
5 Germany, e-mail: [email protected]
6
7 *******************************************************************************
8 * *
9 * Copyright (C) 1999, 2000 by Martin Lottermoser *
10 * All rights reserved *
11 * *
12 ******************************************************************************/
13
14 /*****************************************************************************/
15
16 #ifndef _XOPEN_SOURCE
17 #define _XOPEN_SOURCE 500
18 #endif
19 #ifndef _XOPEN_SOURCE_EXTENDED
20 #define _XOPEN_SOURCE_EXTENDED 1
21 #endif
22
23 /* Standard headers */
24 #include <errno.h>
25 #include <nl_types.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 /* Special headers */
31 #include "pclscan.h"
32
33 /*****************************************************************************/
34
35 static nl_catd catd = (nl_catd)-1; /* NLS catalogue descriptor */
36
37 static void message(int id, const char *s)
38 {
39 if (catd != (nl_catd)-1) s = catgets(catd, 1, id, s);
40 /* The check might not be necessary (X/Open requires catgets() to return 's'
41 if the call is unsuccessful for *any* reason and the possible errors
42 include EBADF for an invalid 'catd'), but one never knows. */
43
44 fputs("? pclscan: ", stderr);
45 fprintf(stderr, s);
46 fputc('\n', stderr);
47
48 return;
49 }
50
51 /*****************************************************************************/
52
53 static const pcl_Octet ESC = '\x1B';
54
55 /******************************************************************************
56
57 Function: pcl_is_control_code
58
59 This function returns a non-zero value iff 'c', taken to be an unsigned char
60 converted to 'int', is a PCL control code.
61
62 ******************************************************************************/
63
64 int pcl_is_control_code(int c)
65 {
66 return
67 c <= ' ' && (
68 c == '\0' ||
69 /* TRG500 does not mention NUL as a control code. */
70 c == '\b' || c == '\t' || c == '\n' || c == '\f' || c == '\r' ||
71 c == '\x0E' /* Shift Out (SO) */ ||
72 c == '\x0F' /* Shift In (SI) */ ||
73 c == '\x11' /* Device Control 1 (DC1) */ ||
74 c == '\x13' /* Device Control 3 (DC3) */ ||
75 c == ESC ||
76 c == ' ');
77 }
78
79 /*****************************************************************************/
80
81 static int cmp_strings(const void *a, const void *b)
82 {
83 return strncmp((const char *)a, (const char *)b, 3);
84 }
85
86 static int default_interpreter(gp_file *in, const pcl_Command *cmd)
87 {
88 /* Skip over arguments for those commands which are known to have them */
89 if (cmd->kind >= 3) {
90 if (strncmp((const char *)cmd->chars, "%-X", 3) == 0 && cmd->i == 12345) {
91 /* Universal Exit Language (UEL)/Start of PJL */
92 int c;
93
94 do {
95 c = fgetc(in);
96 if (c != '@') break;
97 do c = fgetc(in); while (c != EOF && c != '\n');
98 } while (c != EOF);
99 if (c != EOF) ungetc(c, in);
100 }
101 else if (cmd->i > 0) {
102 static const char with_args[][4] = {
103 /* Must be sorted with respect to cmp_strings() */
104 "&bW",
105 "&iW",
106 "&pX", /* Transparent Print Mode */
107 "(sW", /* Download Character */
108 ")sW", /* Create Font */
109 "*bV", /* Transfer Raster Graphics Data by Plane */
110 "*bW", /* Transfer Raster Graphics Data by Row */
111 "*dW", /* Palette Configuration */
112 "*gW", /* Configure Raster Data */
113 "*oW"};
114
115 if (bsearch(cmd->chars, with_args,
116 sizeof(with_args)/sizeof(with_args[0]), sizeof(with_args[0]),
117 &cmp_strings) != NULL) {
118 int j;
119
120 j = cmd->i;
121 while (j > 0 && fgetc(in) != EOF) j--;
122 if (j > 0) {
123 message(1, "Premature EOF on input.");
124 return -1;
125 }
126 }
127 }
128 }
129 else if (cmd->kind == 2 && cmd->chars[0] == 'Y') {
130 /* Display Functions Mode ON */
131 int c;
132
133 /* Read until EOF or Display Functions Mode OFF */
134 do {
135 do c = fgetc(in); while (c != EOF && c != ESC);
136 if (c != EOF) c = fgetc(in);
137 } while (c != EOF && c != 'Z');
138
139 if (c != 'Z') {
140 message(1, "Premature EOF on input.");
141 return -1;
142 }
143 }
144
145 return 0;
146 }
147
148 /*****************************************************************************/
149
150 static int default_handler(gp_file *in)
151 {
152 int c;
153
154 do {
155 c = fgetc(in);
156 } while (c != EOF && !pcl_is_control_code(c));
157 if (c != EOF) ungetc(c, in);
158
159 return 0;
160 }
161
162 /******************************************************************************
163
164 Function: pcl_scan
165
166 This function scans a PCL file, separating it into printer commands and
167 unknown data, and calling the functions '(*interpreter)()' and '(*handler)()'
168 with these parts, respectively.
169
170 'in' must have been opened as a binary file and for reading.
171 'interpreter' or 'handler' may be null in which case default routines are
172 used.
173 If a non-NULL function pointer is provided for 'interpreter' or 'handler',
174 the function will be passed the value of 'idata' or 'hdata', respectively.
175
176 This function returns zero on success and a negative value otherwise.
177 If one of the interpreter or data handler functions returns a negative value,
178 processing will stop and the value will be returned as the return value of
179 pcl_scan().
180
181 ******************************************************************************/
182
183 int pcl_scan(gp_file *in, pcl_CommandInterpreter interpreter, void *idata,
184 pcl_UnknownDataHandler handler, void *hdata)
185 {
186 int
187 c,
188 rc = 0;
189
190 /* Open catalogue descriptor */
191 catd = catopen("pcltools", 0);
192 if (catd == (nl_catd)(-1) && errno != ENOENT)
193 /* A system returning ENOENT if no catalogue is available is for example
194 Sun Solaris 2.5. */
195 fprintf(stderr,
196 "?-W pclscan: Error trying to open message catalogue: %s.\n",
197 strerror(errno));
198
199 /* Loop over input */
200 while (rc >= 0 && (c = fgetc(in)) != EOF) {
201 if (c == ESC) {
202 pcl_Command command;
203
204 if ((c = fgetc(in)) == EOF) {
205 rc = -1;
206 message(1, "Premature EOF on input.");
207 break;
208 }
209 command.chars[0] = c;
210 if (48 <= c && c <= 126) {
211 /* Two-character escape sequence */
212 command.kind = 2;
213 if (interpreter == NULL ||
214 (rc = (*interpreter)(in, &command, idata)) > 0)
215 rc = default_interpreter(in, &command);
216 }
217 else {
218 int continued;
219
220 /* Parameterized escape sequence or garbage. The character we've just
221 read is the "parameterized character" and it should be in the range
222 33-47. */
223
224 /* Now the group character (should be in the range 96-126, but HP uses
225 sometimes at least '-', which has the value 45) */
226 if ((c = fgetc(in)) == EOF) {
227 rc = -1;
228 message(1, "Premature EOF on input.");
229 break;
230 }
231 command.chars[1] = c;
232
233 continued = 0;
234 do {
235 /* Now for the value */
236 if ((c = fgetc(in)) == EOF) {
237 rc = -1;
238 message(1, "Premature EOF on input.");
239 break;
240 }
241 if (c == '+' || c == '-' || '0' <= c && c <= '9') {
242 if (c == '+' || c == '-') command.prefix = c;
243 else command.prefix = ' ';
244 ungetc(c, in);
245 if (fscanf(in, "%d", &command.i) != 1) {
246 rc = -1;
247 message(2, "Syntax error in value field.");
248 break;
249 }
250
251 /* Decimal point? */
252 if ((c = fgetc(in)) == EOF) {
253 rc = -1;
254 message(1, "Premature EOF on input.");
255 break;
256 }
257 command.scale = 0;
258 command.fraction = 0;
259 if (c == '.') {
260 command.scale = 1;
261 while ((c = fgetc(in)) != EOF && '0' <= c && c <= '9') {
262 command.fraction = command.fraction*10 + (c - '0');
263 command.scale *= 10;
264 }
265 if (c == EOF) {
266 rc = -1;
267 message(1, "Premature EOF on input.");
268 break;
269 }
270 if (command.prefix == '-') command.fraction = -command.fraction;
271 }
272 }
273 else {
274 command.prefix = '\0'; /* no value given */
275 command.i = 0;
276 }
277
278 /* Final character */
279 if (96 <= c && c <= 126) {
280 /* Parameter character */
281 command.chars[2] = c - ('a' - 'A');
282 command.kind = (continued? 6: 4);
283 continued = 1;
284 }
285 else {
286 /* Termination character (should be in the range 64-94) */
287 command.chars[2] = c;
288 command.kind = (continued? 5: 3);
289 continued = 0;
290 }
291
292 if (interpreter == NULL ||
293 (rc = (*interpreter)(in, &command, idata)) > 0)
294 rc = default_interpreter(in, &command);
295 } while (rc == 0 && continued);
296 }
297 }
298 else if (pcl_is_control_code(c)) {
299 pcl_Command command;
300
301 command.chars[0] = c;
302 command.kind = 1;
303 command.i = 1; /* number of occurrences */
304
305 while ((c = fgetc(in)) != EOF && c == command.chars[0]) command.i++;
306 if (c != EOF) ungetc(c, in);
307
308 if (interpreter == NULL ||
309 (rc = (*interpreter)(in, &command, idata)) > 0)
310 rc = default_interpreter(in, &command);
311 }
312 else {
313 ungetc(c, in);
314 if (handler == NULL || (rc = (*handler)(in, hdata)) > 0)
315 rc = default_handler(in);
316 }
317 }
318
319 /* Close catalogue descriptor */
320 if (catd != (nl_catd)-1) catclose(catd);
321
322 return rc;
323 }