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