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