1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include "stdio.h"
34*7c478bd9Sstevel@tonic-gate #include "string.h"
35*7c478bd9Sstevel@tonic-gate #include "stdlib.h"
36*7c478bd9Sstevel@tonic-gate #include "unistd.h"
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #include "lp.h"
39*7c478bd9Sstevel@tonic-gate #include "filters.h"
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate static void		q_print ( int , char * );
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate static char *fw_zDblQte (char *zBuf);
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate /**
46*7c478bd9Sstevel@tonic-gate  ** dumpfilters() - WRITE FILTERS FROM INTERNAL STRUCTURE TO FILTER TABLE
47*7c478bd9Sstevel@tonic-gate  **/
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate int
dumpfilters(char * file)50*7c478bd9Sstevel@tonic-gate dumpfilters(char *file)
51*7c478bd9Sstevel@tonic-gate {
52*7c478bd9Sstevel@tonic-gate 	register _FILTER	*pf;
53*7c478bd9Sstevel@tonic-gate 	register TEMPLATE	*pt;
54*7c478bd9Sstevel@tonic-gate 	register TYPE		*pty;
55*7c478bd9Sstevel@tonic-gate 	register char		*p,
56*7c478bd9Sstevel@tonic-gate 				*sep;
57*7c478bd9Sstevel@tonic-gate 	register int		fld;
58*7c478bd9Sstevel@tonic-gate 	int fd;
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate 	if ((fd = open_filtertable(file, "w")) < 0)
61*7c478bd9Sstevel@tonic-gate 		return (-1);
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	printlist_setup ("", "", LP_SEP, "");
64*7c478bd9Sstevel@tonic-gate 	if (filters) for (pf = filters; pf->name; pf++) {
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate 		for (fld = 0; fld < FL_MAX; fld++) switch (fld) {
67*7c478bd9Sstevel@tonic-gate 		case FL_IGN:
68*7c478bd9Sstevel@tonic-gate 			break;
69*7c478bd9Sstevel@tonic-gate 		case FL_NAME:
70*7c478bd9Sstevel@tonic-gate 			p = pf->name;
71*7c478bd9Sstevel@tonic-gate 			goto String;
72*7c478bd9Sstevel@tonic-gate 		case FL_CMD:
73*7c478bd9Sstevel@tonic-gate 			if ((p = fw_zDblQte (pf->command)) != NULL) {
74*7c478bd9Sstevel@tonic-gate 				(void)fdprintf (fd, "%s%s", FL_SEP, p);
75*7c478bd9Sstevel@tonic-gate 				free (p);
76*7c478bd9Sstevel@tonic-gate 				break;
77*7c478bd9Sstevel@tonic-gate 			}
78*7c478bd9Sstevel@tonic-gate 			/* zDblQte failed so go without quotes */
79*7c478bd9Sstevel@tonic-gate 			p = pf->command;
80*7c478bd9Sstevel@tonic-gate String:			(void)fdprintf (fd, "%s%s", FL_SEP, (p? p : ""));
81*7c478bd9Sstevel@tonic-gate 			break;
82*7c478bd9Sstevel@tonic-gate 		case FL_TYPE:
83*7c478bd9Sstevel@tonic-gate 			(void)fdprintf(fd, "%s%s", FL_SEP,
84*7c478bd9Sstevel@tonic-gate 				(pf->type == fl_fast? FL_FAST : FL_SLOW));
85*7c478bd9Sstevel@tonic-gate 			break;
86*7c478bd9Sstevel@tonic-gate 		case FL_PTYPS:
87*7c478bd9Sstevel@tonic-gate 			pty = pf->printer_types;
88*7c478bd9Sstevel@tonic-gate 			goto Types;
89*7c478bd9Sstevel@tonic-gate 		case FL_ITYPS:
90*7c478bd9Sstevel@tonic-gate 			pty = pf->input_types;
91*7c478bd9Sstevel@tonic-gate 			goto Types;
92*7c478bd9Sstevel@tonic-gate 		case FL_OTYPS:
93*7c478bd9Sstevel@tonic-gate 			pty = pf->output_types;
94*7c478bd9Sstevel@tonic-gate Types:			(void)fdprintf(fd, "%s", FL_SEP);
95*7c478bd9Sstevel@tonic-gate 			sep = "";
96*7c478bd9Sstevel@tonic-gate 			if (pty) {
97*7c478bd9Sstevel@tonic-gate 				for (; pty->name; pty++) {
98*7c478bd9Sstevel@tonic-gate 					(void)fdprintf(fd, "%s%s", sep,
99*7c478bd9Sstevel@tonic-gate 						pty->name);
100*7c478bd9Sstevel@tonic-gate 					sep = ",";
101*7c478bd9Sstevel@tonic-gate 				}
102*7c478bd9Sstevel@tonic-gate 			} else
103*7c478bd9Sstevel@tonic-gate 				(void)fdprintf(fd, "%s", NAME_ANY);
104*7c478bd9Sstevel@tonic-gate 			break;
105*7c478bd9Sstevel@tonic-gate 		case FL_PRTRS:
106*7c478bd9Sstevel@tonic-gate 			(void)fdprintf(fd, "%s", FL_SEP);
107*7c478bd9Sstevel@tonic-gate 			if (pf->printers)
108*7c478bd9Sstevel@tonic-gate 				fdprintlist (fd, pf->printers);
109*7c478bd9Sstevel@tonic-gate 			else
110*7c478bd9Sstevel@tonic-gate 				(void)fdprintf(fd, "%s", NAME_ANY);
111*7c478bd9Sstevel@tonic-gate 			break;
112*7c478bd9Sstevel@tonic-gate 		case FL_TMPS:
113*7c478bd9Sstevel@tonic-gate 			(void)fdprintf(fd, "%s", FL_SEP);
114*7c478bd9Sstevel@tonic-gate 			sep = "";
115*7c478bd9Sstevel@tonic-gate 			if ((pt = pf->templates))
116*7c478bd9Sstevel@tonic-gate 				for(; pt->keyword; pt++) {
117*7c478bd9Sstevel@tonic-gate 					(void)fdprintf(fd, "%s%s ", sep,
118*7c478bd9Sstevel@tonic-gate 						pt->keyword);
119*7c478bd9Sstevel@tonic-gate 					q_print(fd, pt->pattern);
120*7c478bd9Sstevel@tonic-gate 					(void)fdprintf(fd, " = ");
121*7c478bd9Sstevel@tonic-gate 					q_print(fd, pt->result);
122*7c478bd9Sstevel@tonic-gate 					sep = ",";
123*7c478bd9Sstevel@tonic-gate 				}
124*7c478bd9Sstevel@tonic-gate 			break;
125*7c478bd9Sstevel@tonic-gate 		}
126*7c478bd9Sstevel@tonic-gate 		(void)fdprintf(fd, FL_END);
127*7c478bd9Sstevel@tonic-gate 	}
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 	close(fd);
130*7c478bd9Sstevel@tonic-gate 	return (0);
131*7c478bd9Sstevel@tonic-gate }
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate /**
134*7c478bd9Sstevel@tonic-gate  ** q_print() - PRINT STRING, QUOTING SEPARATOR CHARACTERS
135*7c478bd9Sstevel@tonic-gate  **/
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate static void
q_print(int fd,char * str)138*7c478bd9Sstevel@tonic-gate q_print(int fd, char *str)
139*7c478bd9Sstevel@tonic-gate {
140*7c478bd9Sstevel@tonic-gate 	/*
141*7c478bd9Sstevel@tonic-gate 	 * There are four reasons to quote a character: It is
142*7c478bd9Sstevel@tonic-gate 	 * a quote (backslash) character, it is a field separator,
143*7c478bd9Sstevel@tonic-gate 	 * it is a list separator, or it is a template separator.
144*7c478bd9Sstevel@tonic-gate 	 * "loadfilters()" strips the quote (backslash), but not
145*7c478bd9Sstevel@tonic-gate 	 * in one place.
146*7c478bd9Sstevel@tonic-gate 	 */
147*7c478bd9Sstevel@tonic-gate 	if (!str)
148*7c478bd9Sstevel@tonic-gate 		return;
149*7c478bd9Sstevel@tonic-gate 	while (*str) {
150*7c478bd9Sstevel@tonic-gate 		if (
151*7c478bd9Sstevel@tonic-gate 			*str == '\\'		/* quote reason #1 */
152*7c478bd9Sstevel@tonic-gate 		     || strchr(FL_SEP, *str)	/* quote reason #2 */
153*7c478bd9Sstevel@tonic-gate 		     || strchr(LP_SEP, *str)	/* quote reason #3 */
154*7c478bd9Sstevel@tonic-gate 		     || strchr("=", *str)	/* quote reason #4 */
155*7c478bd9Sstevel@tonic-gate 		)
156*7c478bd9Sstevel@tonic-gate 			fdputc ('\\', fd);
157*7c478bd9Sstevel@tonic-gate 		fdputc (*str, fd);
158*7c478bd9Sstevel@tonic-gate 		str++;
159*7c478bd9Sstevel@tonic-gate 	}
160*7c478bd9Sstevel@tonic-gate 	return;
161*7c478bd9Sstevel@tonic-gate }
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate /*********************************************************
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 	fw_zDblQte
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate 	Duplicates the given string allocating memory
168*7c478bd9Sstevel@tonic-gate 	using malloc.
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 	Double quotes are used to encase the string
171*7c478bd9Sstevel@tonic-gate 	and a backslash s put infront of any embedded
172*7c478bd9Sstevel@tonic-gate 	quotes.
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	returns a pointer to the string provided.
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 	It the function runs out of memory it returns
177*7c478bd9Sstevel@tonic-gate 	NULL.
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate */
fw_zDblQte(char * zBuf)181*7c478bd9Sstevel@tonic-gate static char *fw_zDblQte (char *zBuf)
182*7c478bd9Sstevel@tonic-gate {
183*7c478bd9Sstevel@tonic-gate 	char *zT;
184*7c478bd9Sstevel@tonic-gate 	int i;
185*7c478bd9Sstevel@tonic-gate 	int j;
186*7c478bd9Sstevel@tonic-gate 	int iNewSize;
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 	/* count the embedded double quotes */
189*7c478bd9Sstevel@tonic-gate 	for (i = j = 0; zBuf[i]; i++) {
190*7c478bd9Sstevel@tonic-gate 		if (zBuf[i] == '"') {
191*7c478bd9Sstevel@tonic-gate 			j++;
192*7c478bd9Sstevel@tonic-gate 		}
193*7c478bd9Sstevel@tonic-gate 	}
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate 	/*
196*7c478bd9Sstevel@tonic-gate 		Allocate a new buffer
197*7c478bd9Sstevel@tonic-gate 		add 3 extra bytes for:
198*7c478bd9Sstevel@tonic-gate 			the new leading double quote
199*7c478bd9Sstevel@tonic-gate 			the new trailing double quote
200*7c478bd9Sstevel@tonic-gate 			and the NULL
201*7c478bd9Sstevel@tonic-gate 		add an extra byte for each embedded double quote
202*7c478bd9Sstevel@tonic-gate 	*/
203*7c478bd9Sstevel@tonic-gate 	iNewSize = (strlen (zBuf) + 3 + j);
204*7c478bd9Sstevel@tonic-gate 	if ((zT = malloc (iNewSize)) == NULL) {
205*7c478bd9Sstevel@tonic-gate 		return (NULL); /* buffer overflow */
206*7c478bd9Sstevel@tonic-gate 	}
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 	j = 0;
209*7c478bd9Sstevel@tonic-gate 	zT[j++] = '"'; /* start with a leading double quote */
210*7c478bd9Sstevel@tonic-gate 	for (i = 0; zBuf[i]; i++) {
211*7c478bd9Sstevel@tonic-gate 		if (zBuf[i] == '"') {
212*7c478bd9Sstevel@tonic-gate 			zT[j++] = '\\';
213*7c478bd9Sstevel@tonic-gate 		}
214*7c478bd9Sstevel@tonic-gate 		zT[j++] = zBuf[i];
215*7c478bd9Sstevel@tonic-gate 	}
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate 	zT[j++] = '"'; /* add a trailing double quote */
218*7c478bd9Sstevel@tonic-gate 	zT[j] = '\0';
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 	return (zT);
221*7c478bd9Sstevel@tonic-gate }
222*7c478bd9Sstevel@tonic-gate 
223