1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright 1990 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29
30
31 #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.13 */
32 /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
33
34 #include "string.h"
35 #include "errno.h"
36 #include "stdlib.h"
37 #include "regexpr.h"
38
39 #include "lp.h"
40 #include "filters.h"
41
42 static char *keyword_list[] = {
43 PARM_INPUT,
44 PARM_OUTPUT,
45 PARM_TERM,
46 PARM_PRINTER,
47 PARM_CPI,
48 PARM_LPI,
49 PARM_LENGTH,
50 PARM_WIDTH,
51 PARM_PAGES,
52 PARM_CHARSET,
53 PARM_FORM,
54 PARM_COPIES,
55 PARM_MODES,
56 0
57 };
58
59 #if defined(__STDC__)
60 static char * q_strchr ( char * , char );
61 static char * q_strdup ( char * );
62 #else
63 static char *q_strchr(),
64 *q_strdup();
65 #endif
66
67 /**
68 ** s_to_filtertype() - CONVERT (char *) TO (FILTERTYPE)
69 **/
70
71 FILTERTYPE
72 #if defined(__STDC__)
s_to_filtertype(char * str)73 s_to_filtertype (
74 char * str
75 )
76 #else
77 s_to_filtertype (str)
78 char *str;
79 #endif
80 {
81 /*
82 * The default type, if none is given, is ``slow''.
83 */
84 if (STREQU(str, FL_FAST))
85 return (fl_fast);
86 else
87 return (fl_slow);
88 }
89
90 /**
91 ** s_to_type() - CONVERT (char *) TO (TYPE)
92 **/
93
94 TYPE
95 #if defined(__STDC__)
s_to_type(char * str)96 s_to_type (
97 char * str
98 )
99 #else
100 s_to_type (str)
101 register char *str;
102 #endif
103 {
104 TYPE ret;
105
106 if ((ret.name = Strdup(str)))
107 ret.info = isterminfo(str);
108 return (ret);
109 }
110
111 /**
112 ** s_to_template() - CONVERT (char *) TO (TEMPLATE)
113 **/
114
115 TEMPLATE
116 #if defined(__STDC__)
s_to_template(char * str)117 s_to_template (
118 char * str
119 )
120 #else
121 s_to_template (str)
122 register char *str;
123 #endif
124 {
125 TEMPLATE ret;
126
127 register char *p,
128 c;
129
130
131 if (!*(str += strspn(str, " "))) {
132 lp_errno = LP_ETEMPLATE;
133 ret.keyword = 0;
134 goto Done;
135 }
136
137 if (!(p = strchr(str, ' '))) {
138 lp_errno = LP_EPATTERN;
139 ret.keyword = 0;
140 goto Done;
141 }
142
143 c = *p;
144 *p = 0;
145 ret.keyword = Strdup(str);
146 *p = c;
147
148 if (!ret.keyword) {
149 lp_errno = LP_ENOMEM;
150 goto Done;
151 }
152 if (!searchlist(ret.keyword, keyword_list)) {
153 lp_errno = LP_EKEYWORD;
154 ret.keyword = 0;
155 goto Done;
156 }
157
158 str = p + strspn(p, " ");
159 if (!(p = q_strchr(str, '='))) {
160 lp_errno = LP_ERESULT;
161 ret.keyword = 0;
162 goto Done;
163 }
164 while (p[-1] == ' ' && p > str)
165 p--;
166
167 c = *p;
168 *p = 0;
169 ret.pattern = q_strdup(str);
170 *p = c;
171
172 if (!ret.pattern) {
173 lp_errno = LP_ENOMEM;
174 ret.keyword = 0;
175 goto Done;
176 }
177
178 if (!*ret.pattern) {
179 lp_errno = LP_EPATTERN;
180 ret.keyword = 0;
181 goto Done;
182 }
183
184 if (!(ret.re = compile(ret.pattern, (char *)0, (char *)0))) {
185 lp_errno = LP_EREGEX;
186 ret.keyword = 0;
187 goto Done;
188 }
189 ret.nbra = nbra;
190
191 if (!*(str = p + strspn(p, " ="))) {
192 lp_errno = LP_ERESULT;
193 ret.keyword = 0;
194 goto Done;
195 }
196 ret.result = q_strdup(str);
197 if (!ret.result) {
198 lp_errno = LP_ENOMEM;
199 ret.keyword = 0;
200 }
201
202 Done: return (ret);
203 }
204
205 /**
206 ** sl_to_typel() - CONVERT (char **) LIST TO (TYPE *) LIST
207 **/
208
209 TYPE *
210 #if defined(__STDC__)
sl_to_typel(char ** src)211 sl_to_typel (
212 char ** src
213 )
214 #else
215 sl_to_typel (src)
216 char **src;
217 #endif
218 {
219 register TYPE *dst;
220
221 register int nitems,
222 n;
223
224 if (!src || !*src)
225 return (0);
226
227 for (nitems = 0; src[nitems]; nitems++)
228 ;
229
230 if (!(dst = (TYPE *)Malloc((nitems + 1) * sizeof(TYPE)))) {
231 errno = ENOMEM;
232 return (0);
233 }
234
235 for (n = 0; n < nitems; n++)
236 dst[n] = s_to_type(src[n]);
237 dst[nitems].name = 0;
238
239 return (dst);
240 }
241
242 /**
243 ** sl_to_templatel() - DUPLICATE A (char **) LIST AS (TEMPLATE *) LIST
244 **/
245
246 TEMPLATE *
247 #if defined(__STDC__)
sl_to_templatel(char ** src)248 sl_to_templatel (
249 char ** src
250 )
251 #else
252 sl_to_templatel (src)
253 register char **src;
254 #endif
255 {
256 register TEMPLATE *dst;
257
258 register int nitems,
259 n;
260
261 if (!src || !*src)
262 return (0);
263
264 for (nitems = 0; src[nitems]; nitems++)
265 ;
266
267 if (!(dst = (TEMPLATE *)Malloc((nitems + 1) * sizeof(TEMPLATE)))){
268 errno = ENOMEM;
269 return (0);
270 }
271
272 for (n = 0; n < nitems; n++) {
273 dst[n] = s_to_template(src[n]);
274 if (dst[n].keyword == 0) {
275 freetempl (dst);
276 return (0);
277 }
278 }
279 dst[nitems].keyword = 0;
280
281 return (dst);
282 }
283
284 /**
285 ** type_to_s() - CONVERT (TYPE) TO (char *)
286 **/
287
288 char *
289 #if defined(__STDC__)
type_to_s(TYPE t)290 type_to_s (
291 TYPE t
292 )
293 #else
294 type_to_s (t)
295 TYPE t;
296 #endif
297 {
298 return (Strdup(t.name));
299 }
300
301 /**
302 ** template_to_s() - CONVERT (TEMPLATE) TO (char *)
303 **/
304
305 char *
306 #if defined(__STDC__)
template_to_s(TEMPLATE t)307 template_to_s (
308 TEMPLATE t
309 )
310 #else
311 template_to_s (t)
312 TEMPLATE t;
313 #endif
314 {
315 register char *ret,
316 *p,
317 *r;
318
319 register size_t len;
320
321
322 len = strlen(t.keyword) + 1;
323 for (p = t.pattern; *p; p++) {
324 if (*p == '=')
325 len++;
326 len++;
327 }
328 len += 3 + strlen(t.result);
329
330 ret = Malloc(len + 1);
331 if (!ret) {
332 errno = ENOMEM;
333 return (0);
334 }
335
336 r = ret;
337 for (p = t.keyword; *p; )
338 *r++ = *p++;
339 *r++ = ' ';
340 for (p = t.pattern; *p; ) {
341 if (*p == '=')
342 *r++ = '\\';
343 *r++ = *p++;
344 }
345 *r++ = ' ';
346 *r++ = '=';
347 *r++ = ' ';
348 for (p = t.result; *p; )
349 *r++ = *p++;
350 *r = 0;
351
352 return (ret);
353 }
354
355 /**
356 ** typel_to_sl() - DUPLICATE (TYPE *) LIST AS (char **) LIST
357 **/
358
359 char **
360 #if defined(__STDC__)
typel_to_sl(TYPE * src)361 typel_to_sl (
362 TYPE * src
363 )
364 #else
365 typel_to_sl (src)
366 TYPE *src;
367 #endif
368 {
369 register char **dst;
370
371 register size_t nitems;
372
373 register int n;
374
375
376 if (!src || !src->name)
377 return (0);
378
379 for (nitems = 0; src[nitems].name; nitems++)
380 ;
381
382 if (!(dst = (char **)Malloc((nitems + 1) * sizeof(char *)))) {
383 errno = ENOMEM;
384 return (0);
385 }
386
387 for (n = 0; n < nitems; n++)
388 dst[n] = type_to_s(src[n]);
389 dst[nitems] = 0;
390
391 return (dst);
392 }
393
394 /**
395 ** templatel_to_sl() - DUPLICATE A (TEMPLATE *) LIST AS (char **) LIST
396 **/
397
398 char **
399 #if defined(__STDC__)
templatel_to_sl(TEMPLATE * src)400 templatel_to_sl (
401 TEMPLATE * src
402 )
403 #else
404 templatel_to_sl (src)
405 register TEMPLATE *src;
406 #endif
407 {
408 register char **dst;
409
410 register size_t nitems;
411
412 register int n;
413
414
415 if (!src || !src->keyword)
416 return (0);
417
418 for (nitems = 0; src[nitems].keyword; nitems++)
419 ;
420
421 if (!(dst = (char **)Malloc((nitems + 1) * sizeof(char *)))) {
422 errno = ENOMEM;
423 return (0);
424 }
425
426 for (n = 0; n < nitems; n++)
427 dst[n] = template_to_s(src[n]);
428 dst[nitems] = 0;
429
430 return (dst);
431 }
432
433 /**
434 ** q_strpbrk() - strpbrk() WITH BACKSLASH QUOTING
435 ** q_strdup() - strdup() WITH BACKSLASHES OMITTED
436 **/
437
438 static char *
439 #if defined(__STDC__)
q_strchr(char * sp,char c)440 q_strchr (
441 char * sp,
442 char c
443 )
444 #else
445 q_strchr (sp, c)
446 register char *sp,
447 c;
448 #endif
449 {
450 do {
451 if (*sp == '\\' && sp[1])
452 sp += 2;
453 if (*sp == c)
454 return (sp);
455 } while (*sp++);
456 return (0);
457 }
458
459 static char *
460 #if defined(__STDC__)
q_strdup(char * str)461 q_strdup (
462 char * str
463 )
464 #else
465 q_strdup (str)
466 char *str;
467 #endif
468 {
469 char *ret;
470
471 register char *p,
472 *q;
473
474 register int len = 0;
475
476
477 for (p = str; *p; p++) {
478 if (*p == '\\' && p[1] == '=')
479 p++;
480 len++;
481 }
482
483 if (!(ret = q = Malloc(len + 1)))
484 return (0);
485
486 for (p = str; *p; p++) {
487 if (*p == '\\' && p[1] == '=')
488 p++;
489 *q++ = *p;
490 }
491 *q = 0;
492
493 return (ret);
494 }
495