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