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