]> git.ipfire.org Git - thirdparty/cups.git/blob - filter/hpgl-input.c
Import cups.org releases
[thirdparty/cups.git] / filter / hpgl-input.c
1 /*
2 * "$Id$"
3 *
4 * HP-GL/2 input processing for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1993-2004 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 return (-1);
73
74 if (ch == 0x1b)
75 switch (getc(fp))
76 {
77 case '.' : /* HP-GL/2 job control */
78 i = getc(fp);
79
80 if (strchr(")Z", i) != NULL)
81 {
82 /*
83 * 'Printer Off' command - look for next 'Printer On' command...
84 */
85
86 for (;;)
87 {
88 while ((i = getc(fp)) != EOF && i != 0x1b);
89
90 if (i == EOF)
91 return (-1);
92
93 if (getc(fp) != '.')
94 continue;
95
96 if ((i = getc(fp)) == '(' ||
97 i == 'Y')
98 break;
99 }
100 }
101 else if (strchr("@HIMNTI\003", i) != NULL)
102 {
103 while ((i = getc(fp)) != EOF && i != ':');
104 }
105 break;
106
107 case '%' : /* PJL command? */
108 if (getc(fp) == '-')
109 {
110 /*
111 * Yes, dump everything up to the "ENTER LANGUAGE" line...
112 */
113
114 while (fgets(buf, sizeof(buf), fp) != NULL)
115 if (strstr(buf, "ENTER") && strstr(buf, "LANGUAGE"))
116 break;
117 break;
118 }
119
120 default : /* HP RTL/PCL control */
121 while ((i = getc(fp)) != EOF && !isupper(i & 255));
122 break;
123 }
124 } while (ch < ' ');
125
126 name[0] = ch;
127 name[1] = getc(fp);
128 name[2] = '\0';
129
130 if (strcasecmp(name, "LB") == 0)
131 {
132 bufptr = buf;
133 while ((ch = getc(fp)) != StringTerminator)
134 if (bufptr < (buf + sizeof(buf) - 1))
135 *bufptr++ = ch;
136 *bufptr = '\0';
137
138 p[num_params].type = PARAM_STRING;
139 p[num_params].value.string = strdup(buf);
140 num_params ++;
141 }
142 else if (strcasecmp(name, "SM") == 0)
143 {
144 buf[0] = getc(fp);
145 buf[1] = '\0';
146 p[num_params].type = PARAM_STRING;
147 p[num_params].value.string = strdup(buf);
148 num_params ++;
149 }
150 else if (strcasecmp(name, "DT") == 0)
151 {
152 if ((buf[0] = getc(fp)) != ';')
153 {
154 buf[1] = '\0';
155 p[num_params].type = PARAM_STRING;
156 p[num_params].value.string = strdup(buf);
157 num_params ++;
158 }
159 }
160 else if (strcasecmp(name, "PE") == 0)
161 {
162 bufptr = buf;
163 while ((ch = getc(fp)) != ';')
164 if (bufptr < (buf + sizeof(buf) - 1))
165 *bufptr++ = ch;
166 *bufptr = '\0';
167
168 p[num_params].type = PARAM_STRING;
169 p[num_params].value.string = strdup(buf);
170 num_params ++;
171 }
172
173 while (!done)
174 switch (ch = getc(fp))
175 {
176 case ',' :
177 case ' ' :
178 case '\n' :
179 case '\r' :
180 case '\t' :
181 break;
182
183 case '\"' :
184 fscanf(fp, "%262143[^\"]\"", buf);
185 if (num_params < MAX_PARAMS)
186 {
187 p[num_params].type = PARAM_STRING;
188 p[num_params].value.string = strdup(buf);
189 num_params ++;
190 };
191 break;
192
193 case '-' :
194 case '+' :
195 ungetc(ch, fp);
196 fscanf(fp, "%f", &(p[num_params].value.number));
197 if (num_params < MAX_PARAMS)
198 {
199 p[num_params].type = PARAM_RELATIVE;
200 num_params ++;
201 }
202 break;
203 case '0' :
204 case '1' :
205 case '2' :
206 case '3' :
207 case '4' :
208 case '5' :
209 case '6' :
210 case '7' :
211 case '8' :
212 case '9' :
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_ABSOLUTE;
219 num_params ++;
220 }
221 break;
222 default :
223 ungetc(ch, fp);
224 done = 1;
225 break;
226 }
227
228 *params = p;
229 return (num_params);
230 }
231
232
233 /*
234 * 'FreeParameters()' - Free all string parameter values.
235 */
236
237 void
238 FreeParameters(int num_params, /* I - Number of parameters */
239 param_t *params) /* I - Parameter values */
240 {
241 int i; /* Looping var */
242
243
244 for (i = 0; i < num_params; i ++)
245 if (params[i].type == PARAM_STRING)
246 free(params[i].value.string);
247 }
248
249
250 /*
251 * End of "$Id$".
252 */