]> git.ipfire.org Git - thirdparty/cups.git/blob - filter/hpgl-input.c
Merge changes from CUPS 1.5svn-r8950.
[thirdparty/cups.git] / filter / hpgl-input.c
1 /*
2 * "$Id: hpgl-input.c 7219 2008-01-14 22:00:02Z mike $"
3 *
4 * HP-GL/2 input processing for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007-2009 by Apple Inc.
7 * Copyright 1993-2006 by Easy Software Products.
8 *
9 * These coded instructions, statements, and computer programs are the
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/".
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>
29 #include <cups/i18n.h>
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 float temp; /* Temporary parameter value */
52 static param_t p[MAX_PARAMS]; /* Parameter buffer */
53
54
55 num_params = 0;
56 done = 0;
57
58 do
59 {
60 while ((ch = getc(fp)) != EOF)
61 if (strchr(" \t\r\n,;", ch) == NULL)
62 break;
63
64 if (ch == EOF)
65 {
66 return (-1);
67 }
68
69 if (ch == 0x1b)
70 switch (getc(fp))
71 {
72 case '.' : /* HP-GL/2 job control */
73 i = getc(fp);
74
75 if (strchr(")Z", i) != NULL)
76 {
77 /*
78 * 'Printer Off' command - look for next 'Printer On' command...
79 */
80
81 for (;;)
82 {
83 while ((i = getc(fp)) != EOF && i != 0x1b);
84
85 if (i == EOF)
86 return (-1);
87
88 if (getc(fp) != '.')
89 continue;
90
91 if ((i = getc(fp)) == '(' ||
92 i == 'Y')
93 break;
94 }
95 }
96 else if (strchr("@HIMNTI\003", i) != NULL)
97 {
98 while ((i = getc(fp)) != EOF && i != ':');
99 }
100 break;
101
102 case '%' : /* PJL command? */
103 if ((i = getc(fp)) == '-')
104 if ((i = getc(fp)) == '1')
105 if ((i = getc(fp)) == '2')
106 {
107 /*
108 * Yes, dump everything up to the "ENTER LANGUAGE" line...
109 */
110
111 while (fgets(buf, sizeof(buf), fp) != NULL)
112 if (strstr(buf, "ENTER") && strstr(buf, "LANGUAGE"))
113 break;
114 break;
115 }
116
117 ungetc(i, fp);
118
119 default : /* HP RTL/PCL control */
120 while ((i = getc(fp)) != EOF && !isupper(i & 255));
121
122 if (i == EOF)
123 return (-1);
124 break;
125 }
126 } while (ch < ' ');
127
128 name[0] = ch;
129 name[1] = getc(fp);
130 name[2] = '\0';
131
132 if (name[1] < ' ')
133 {
134 /*
135 * If we get here, then more than likely we are faced with a raw PCL
136 * file which we can't handle - abort!
137 */
138
139 fputs(_("ERROR: Invalid HP-GL/2 command seen, unable to print file\n"),
140 stderr);
141 return (-1);
142 }
143
144 if (!strcasecmp(name, "LB"))
145 {
146 bufptr = buf;
147 while ((ch = getc(fp)) != StringTerminator && ch != EOF)
148 if (bufptr < (buf + sizeof(buf) - 1))
149 *bufptr++ = ch;
150 *bufptr = '\0';
151
152 p[num_params].type = PARAM_STRING;
153 p[num_params].value.string = strdup(buf);
154 num_params ++;
155 }
156 else if (!strcasecmp(name, "SM"))
157 {
158 buf[0] = getc(fp);
159 buf[1] = '\0';
160 p[num_params].type = PARAM_STRING;
161 p[num_params].value.string = strdup(buf);
162 num_params ++;
163 }
164 else if (!strcasecmp(name, "DT"))
165 {
166 if ((buf[0] = getc(fp)) != ';')
167 {
168 buf[1] = '\0';
169 p[num_params].type = PARAM_STRING;
170 p[num_params].value.string = strdup(buf);
171 num_params ++;
172 }
173 }
174 else if (!strcasecmp(name, "PE"))
175 {
176 bufptr = buf;
177 while ((ch = getc(fp)) != ';')
178 if (ch == EOF)
179 break;
180 else if (bufptr < (buf + sizeof(buf) - 1))
181 *bufptr++ = ch;
182 *bufptr = '\0';
183
184 p[num_params].type = PARAM_STRING;
185 p[num_params].value.string = strdup(buf);
186 num_params ++;
187 }
188
189 while (!done)
190 switch (ch = getc(fp))
191 {
192 case EOF :
193 done = 1;
194 break;
195
196 case ',' :
197 case ' ' :
198 case '\n' :
199 case '\r' :
200 case '\t' :
201 break;
202
203 case '\"' :
204 fscanf(fp, "%262143[^\"]\"", buf);
205 if (num_params < MAX_PARAMS)
206 {
207 p[num_params].type = PARAM_STRING;
208 p[num_params].value.string = strdup(buf);
209 num_params ++;
210 };
211 break;
212
213 case '-' :
214 case '+' :
215 ungetc(ch, fp);
216 if (fscanf(fp, "%f", &temp) == 1 && num_params < MAX_PARAMS)
217 {
218 p[num_params].type = PARAM_RELATIVE;
219 p[num_params].value.number = temp;
220 num_params ++;
221 }
222 break;
223 case '0' :
224 case '1' :
225 case '2' :
226 case '3' :
227 case '4' :
228 case '5' :
229 case '6' :
230 case '7' :
231 case '8' :
232 case '9' :
233 case '.' :
234 ungetc(ch, fp);
235 if (fscanf(fp, "%f", &temp) == 1 && num_params < MAX_PARAMS)
236 {
237 p[num_params].type = PARAM_ABSOLUTE;
238 p[num_params].value.number = temp;
239 num_params ++;
240 }
241 break;
242 default :
243 ungetc(ch, fp);
244 done = 1;
245 break;
246 }
247
248 *params = p;
249 return (num_params);
250 }
251
252
253 /*
254 * 'FreeParameters()' - Free all string parameter values.
255 */
256
257 void
258 FreeParameters(int num_params, /* I - Number of parameters */
259 param_t *params) /* I - Parameter values */
260 {
261 int i; /* Looping var */
262
263
264 for (i = 0; i < num_params; i ++)
265 if (params[i].type == PARAM_STRING)
266 free(params[i].value.string);
267 }
268
269
270 /*
271 * End of "$Id: hpgl-input.c 7219 2008-01-14 22:00:02Z mike $".
272 */