]> 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/*
2 * "$Id: hpgl-input.c 4494 2005-02-18 02:18:11Z mike $"
3 *
4 * HP-GL/2 input processing for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1993-2005 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
48int /* O - -1 on EOF, # params otherwise */
49ParseCommand(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 break;
129 }
130 } while (ch < ' ');
131
132 name[0] = ch;
133 name[1] = getc(fp);
134 name[2] = '\0';
135
136 if (strcasecmp(name, "LB") == 0)
137 {
138 bufptr = buf;
139 while ((ch = getc(fp)) != StringTerminator)
140 if (bufptr < (buf + sizeof(buf) - 1))
141 *bufptr++ = ch;
142 *bufptr = '\0';
143
144 p[num_params].type = PARAM_STRING;
145 p[num_params].value.string = strdup(buf);
146 num_params ++;
147 }
148 else if (strcasecmp(name, "SM") == 0)
149 {
150 buf[0] = getc(fp);
151 buf[1] = '\0';
152 p[num_params].type = PARAM_STRING;
153 p[num_params].value.string = strdup(buf);
154 num_params ++;
155 }
156 else if (strcasecmp(name, "DT") == 0)
157 {
158 if ((buf[0] = getc(fp)) != ';')
159 {
160 buf[1] = '\0';
161 p[num_params].type = PARAM_STRING;
162 p[num_params].value.string = strdup(buf);
163 num_params ++;
164 }
165 }
166 else if (strcasecmp(name, "PE") == 0)
167 {
168 bufptr = buf;
169 while ((ch = getc(fp)) != ';')
170 if (bufptr < (buf + sizeof(buf) - 1))
171 *bufptr++ = ch;
172 *bufptr = '\0';
173
174 p[num_params].type = PARAM_STRING;
175 p[num_params].value.string = strdup(buf);
176 num_params ++;
177 }
178
179 while (!done)
180 switch (ch = getc(fp))
181 {
182 case ',' :
183 case ' ' :
184 case '\n' :
185 case '\r' :
186 case '\t' :
187 break;
188
189 case '\"' :
190 fscanf(fp, "%262143[^\"]\"", buf);
191 if (num_params < MAX_PARAMS)
192 {
193 p[num_params].type = PARAM_STRING;
194 p[num_params].value.string = strdup(buf);
195 num_params ++;
196 };
197 break;
198
199 case '-' :
200 case '+' :
201 ungetc(ch, fp);
202 fscanf(fp, "%f", &(p[num_params].value.number));
203 if (num_params < MAX_PARAMS)
204 {
205 p[num_params].type = PARAM_RELATIVE;
206 num_params ++;
207 }
208 break;
209 case '0' :
210 case '1' :
211 case '2' :
212 case '3' :
213 case '4' :
214 case '5' :
215 case '6' :
216 case '7' :
217 case '8' :
218 case '9' :
219 case '.' :
220 ungetc(ch, fp);
221 fscanf(fp, "%f", &(p[num_params].value.number));
222 if (num_params < MAX_PARAMS)
223 {
224 p[num_params].type = PARAM_ABSOLUTE;
225 num_params ++;
226 }
227 break;
228 default :
229 ungetc(ch, fp);
230 done = 1;
231 break;
232 }
233
234 *params = p;
235 return (num_params);
236}
237
238
239/*
240 * 'FreeParameters()' - Free all string parameter values.
241 */
242
243void
244FreeParameters(int num_params, /* I - Number of parameters */
245 param_t *params) /* I - Parameter values */
246{
247 int i; /* Looping var */
248
249
250 for (i = 0; i < num_params; i ++)
251 if (params[i].type == PARAM_STRING)
252 free(params[i].value.string);
253}
254
255
256/*
257 * End of "$Id: hpgl-input.c 4494 2005-02-18 02:18:11Z mike $".
258 */