]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
bc44d920 | 2 | * "$Id: hpgl-input.c 6649 2007-07-11 21:46:42Z mike $" |
ef416fc2 | 3 | * |
4 | * HP-GL/2 input processing for the Common UNIX Printing System (CUPS). | |
5 | * | |
bc44d920 | 6 | * Copyright 2007 by Apple Inc. |
b423cd4c | 7 | * Copyright 1993-2006 by Easy Software Products. |
ef416fc2 | 8 | * |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 10 | * property of Apple Inc. and are protected by Federal copyright |
11 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
12 | * which should have been included with this file. If this file is | |
13 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
ef416fc2 | 14 | * |
15 | * This file is subject to the Apple OS-Developed Software exception. | |
16 | * | |
17 | * Contents: | |
18 | * | |
19 | * ParseCommand() - Parse an HPGL/2 command. | |
20 | * FreeParameters() - Free all string parameter values. | |
21 | */ | |
22 | ||
23 | /* | |
24 | * Include necessary headers... | |
25 | */ | |
26 | ||
27 | #include "hpgltops.h" | |
28 | #include <ctype.h> | |
c0e1af83 | 29 | #include <cups/i18n.h> |
ef416fc2 | 30 | |
31 | #define MAX_PARAMS 16384 | |
32 | ||
33 | ||
34 | /* | |
35 | * 'ParseCommand()' - Parse an HPGL/2 command. | |
36 | * | |
37 | * Returns the number of parameters seen or -1 on EOF. | |
38 | */ | |
39 | ||
40 | int /* O - -1 on EOF, # params otherwise */ | |
41 | ParseCommand(FILE *fp, /* I - File to read from */ | |
42 | char *name, /* O - Name of command */ | |
43 | param_t **params) /* O - Parameter list */ | |
44 | { | |
45 | int num_params, /* Number of parameters seen */ | |
46 | ch, /* Current char */ | |
47 | done, /* Non-zero when the current command is read */ | |
48 | i; /* Looping var */ | |
49 | char buf[262144], /* String buffer */ | |
50 | *bufptr; /* Pointer into buffer */ | |
51 | static param_t p[MAX_PARAMS]; /* Parameter buffer */ | |
52 | ||
53 | ||
54 | num_params = 0; | |
55 | done = 0; | |
56 | ||
57 | do | |
58 | { | |
59 | while ((ch = getc(fp)) != EOF) | |
60 | if (strchr(" \t\r\n,;", ch) == NULL) | |
61 | break; | |
62 | ||
63 | if (ch == EOF) | |
64 | { | |
65 | return (-1); | |
66 | } | |
67 | ||
68 | if (ch == 0x1b) | |
69 | switch (getc(fp)) | |
70 | { | |
71 | case '.' : /* HP-GL/2 job control */ | |
72 | i = getc(fp); | |
73 | ||
74 | if (strchr(")Z", i) != NULL) | |
75 | { | |
76 | /* | |
77 | * 'Printer Off' command - look for next 'Printer On' command... | |
78 | */ | |
79 | ||
80 | for (;;) | |
81 | { | |
82 | while ((i = getc(fp)) != EOF && i != 0x1b); | |
83 | ||
84 | if (i == EOF) | |
85 | return (-1); | |
86 | ||
87 | if (getc(fp) != '.') | |
88 | continue; | |
89 | ||
90 | if ((i = getc(fp)) == '(' || | |
91 | i == 'Y') | |
92 | break; | |
93 | } | |
94 | } | |
95 | else if (strchr("@HIMNTI\003", i) != NULL) | |
96 | { | |
97 | while ((i = getc(fp)) != EOF && i != ':'); | |
98 | } | |
99 | break; | |
100 | ||
101 | case '%' : /* PJL command? */ | |
102 | if ((i = getc(fp)) == '-') | |
103 | if ((i = getc(fp)) == '1') | |
104 | if ((i = getc(fp)) == '2') | |
105 | { | |
106 | /* | |
107 | * Yes, dump everything up to the "ENTER LANGUAGE" line... | |
108 | */ | |
109 | ||
110 | while (fgets(buf, sizeof(buf), fp) != NULL) | |
111 | if (strstr(buf, "ENTER") && strstr(buf, "LANGUAGE")) | |
112 | break; | |
113 | break; | |
114 | } | |
115 | ||
116 | ungetc(i, fp); | |
117 | ||
118 | default : /* HP RTL/PCL control */ | |
119 | while ((i = getc(fp)) != EOF && !isupper(i & 255)); | |
b423cd4c | 120 | |
121 | if (i == EOF) | |
122 | return (-1); | |
ef416fc2 | 123 | break; |
124 | } | |
125 | } while (ch < ' '); | |
126 | ||
127 | name[0] = ch; | |
128 | name[1] = getc(fp); | |
129 | name[2] = '\0'; | |
130 | ||
b423cd4c | 131 | if (name[1] < ' ') |
132 | { | |
133 | /* | |
134 | * If we get here, then more than likely we are faced with a raw PCL | |
135 | * file which we can't handle - abort! | |
136 | */ | |
137 | ||
c0e1af83 | 138 | fputs(_("ERROR: Invalid HP-GL/2 command seen, unable to print file!\n"), |
b423cd4c | 139 | stderr); |
140 | return (-1); | |
141 | } | |
142 | ||
143 | if (!strcasecmp(name, "LB")) | |
ef416fc2 | 144 | { |
145 | bufptr = buf; | |
146 | while ((ch = getc(fp)) != StringTerminator) | |
147 | if (bufptr < (buf + sizeof(buf) - 1)) | |
148 | *bufptr++ = ch; | |
149 | *bufptr = '\0'; | |
150 | ||
151 | p[num_params].type = PARAM_STRING; | |
152 | p[num_params].value.string = strdup(buf); | |
153 | num_params ++; | |
154 | } | |
b423cd4c | 155 | else if (!strcasecmp(name, "SM")) |
ef416fc2 | 156 | { |
157 | buf[0] = getc(fp); | |
158 | buf[1] = '\0'; | |
159 | p[num_params].type = PARAM_STRING; | |
160 | p[num_params].value.string = strdup(buf); | |
161 | num_params ++; | |
162 | } | |
b423cd4c | 163 | else if (!strcasecmp(name, "DT")) |
ef416fc2 | 164 | { |
165 | if ((buf[0] = getc(fp)) != ';') | |
166 | { | |
167 | buf[1] = '\0'; | |
168 | p[num_params].type = PARAM_STRING; | |
169 | p[num_params].value.string = strdup(buf); | |
170 | num_params ++; | |
171 | } | |
172 | } | |
b423cd4c | 173 | else if (!strcasecmp(name, "PE")) |
ef416fc2 | 174 | { |
175 | bufptr = buf; | |
176 | while ((ch = getc(fp)) != ';') | |
bc44d920 | 177 | if (ch == EOF) |
178 | break; | |
179 | else if (bufptr < (buf + sizeof(buf) - 1)) | |
ef416fc2 | 180 | *bufptr++ = ch; |
181 | *bufptr = '\0'; | |
182 | ||
183 | p[num_params].type = PARAM_STRING; | |
184 | p[num_params].value.string = strdup(buf); | |
185 | num_params ++; | |
186 | } | |
187 | ||
188 | while (!done) | |
189 | switch (ch = getc(fp)) | |
190 | { | |
bc44d920 | 191 | case EOF : |
192 | done = 1; | |
193 | break; | |
194 | ||
ef416fc2 | 195 | case ',' : |
196 | case ' ' : | |
197 | case '\n' : | |
198 | case '\r' : | |
199 | case '\t' : | |
200 | break; | |
201 | ||
202 | case '\"' : | |
203 | fscanf(fp, "%262143[^\"]\"", buf); | |
204 | if (num_params < MAX_PARAMS) | |
205 | { | |
206 | p[num_params].type = PARAM_STRING; | |
207 | p[num_params].value.string = strdup(buf); | |
208 | num_params ++; | |
209 | }; | |
210 | break; | |
211 | ||
212 | case '-' : | |
213 | case '+' : | |
214 | ungetc(ch, fp); | |
215 | fscanf(fp, "%f", &(p[num_params].value.number)); | |
216 | if (num_params < MAX_PARAMS) | |
217 | { | |
218 | p[num_params].type = PARAM_RELATIVE; | |
219 | num_params ++; | |
220 | } | |
221 | break; | |
222 | case '0' : | |
223 | case '1' : | |
224 | case '2' : | |
225 | case '3' : | |
226 | case '4' : | |
227 | case '5' : | |
228 | case '6' : | |
229 | case '7' : | |
230 | case '8' : | |
231 | case '9' : | |
232 | case '.' : | |
233 | ungetc(ch, fp); | |
234 | fscanf(fp, "%f", &(p[num_params].value.number)); | |
235 | if (num_params < MAX_PARAMS) | |
236 | { | |
237 | p[num_params].type = PARAM_ABSOLUTE; | |
238 | num_params ++; | |
239 | } | |
240 | break; | |
241 | default : | |
242 | ungetc(ch, fp); | |
243 | done = 1; | |
244 | break; | |
245 | } | |
246 | ||
247 | *params = p; | |
248 | return (num_params); | |
249 | } | |
250 | ||
251 | ||
252 | /* | |
253 | * 'FreeParameters()' - Free all string parameter values. | |
254 | */ | |
255 | ||
256 | void | |
257 | FreeParameters(int num_params, /* I - Number of parameters */ | |
258 | param_t *params) /* I - Parameter values */ | |
259 | { | |
260 | int i; /* Looping var */ | |
261 | ||
262 | ||
263 | for (i = 0; i < num_params; i ++) | |
264 | if (params[i].type == PARAM_STRING) | |
265 | free(params[i].value.string); | |
266 | } | |
267 | ||
268 | ||
269 | /* | |
bc44d920 | 270 | * End of "$Id: hpgl-input.c 6649 2007-07-11 21:46:42Z mike $". |
ef416fc2 | 271 | */ |