17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
237c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
287c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include "stdio.h"
347c478bd9Sstevel@tonic-gate #include "string.h"
357c478bd9Sstevel@tonic-gate #include "stdlib.h"
367c478bd9Sstevel@tonic-gate #include "unistd.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include "lp.h"
397c478bd9Sstevel@tonic-gate #include "filters.h"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate static void		q_print ( int , char * );
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate static char *fw_zDblQte (char *zBuf);
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /**
467c478bd9Sstevel@tonic-gate  ** dumpfilters() - WRITE FILTERS FROM INTERNAL STRUCTURE TO FILTER TABLE
477c478bd9Sstevel@tonic-gate  **/
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate int
dumpfilters(char * file)507c478bd9Sstevel@tonic-gate dumpfilters(char *file)
517c478bd9Sstevel@tonic-gate {
527c478bd9Sstevel@tonic-gate 	register _FILTER	*pf;
537c478bd9Sstevel@tonic-gate 	register TEMPLATE	*pt;
547c478bd9Sstevel@tonic-gate 	register TYPE		*pty;
557c478bd9Sstevel@tonic-gate 	register char		*p,
567c478bd9Sstevel@tonic-gate 				*sep;
577c478bd9Sstevel@tonic-gate 	register int		fld;
587c478bd9Sstevel@tonic-gate 	int fd;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	if ((fd = open_filtertable(file, "w")) < 0)
617c478bd9Sstevel@tonic-gate 		return (-1);
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	printlist_setup ("", "", LP_SEP, "");
647c478bd9Sstevel@tonic-gate 	if (filters) for (pf = filters; pf->name; pf++) {
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 		for (fld = 0; fld < FL_MAX; fld++) switch (fld) {
677c478bd9Sstevel@tonic-gate 		case FL_IGN:
687c478bd9Sstevel@tonic-gate 			break;
697c478bd9Sstevel@tonic-gate 		case FL_NAME:
707c478bd9Sstevel@tonic-gate 			p = pf->name;
717c478bd9Sstevel@tonic-gate 			goto String;
727c478bd9Sstevel@tonic-gate 		case FL_CMD:
737c478bd9Sstevel@tonic-gate 			if ((p = fw_zDblQte (pf->command)) != NULL) {
747c478bd9Sstevel@tonic-gate 				(void)fdprintf (fd, "%s%s", FL_SEP, p);
757c478bd9Sstevel@tonic-gate 				free (p);
767c478bd9Sstevel@tonic-gate 				break;
777c478bd9Sstevel@tonic-gate 			}
787c478bd9Sstevel@tonic-gate 			/* zDblQte failed so go without quotes */
797c478bd9Sstevel@tonic-gate 			p = pf->command;
807c478bd9Sstevel@tonic-gate String:			(void)fdprintf (fd, "%s%s", FL_SEP, (p? p : ""));
817c478bd9Sstevel@tonic-gate 			break;
827c478bd9Sstevel@tonic-gate 		case FL_TYPE:
837c478bd9Sstevel@tonic-gate 			(void)fdprintf(fd, "%s%s", FL_SEP,
847c478bd9Sstevel@tonic-gate 				(pf->type == fl_fast? FL_FAST : FL_SLOW));
857c478bd9Sstevel@tonic-gate 			break;
867c478bd9Sstevel@tonic-gate 		case FL_PTYPS:
877c478bd9Sstevel@tonic-gate 			pty = pf->printer_types;
887c478bd9Sstevel@tonic-gate 			goto Types;
897c478bd9Sstevel@tonic-gate 		case FL_ITYPS:
907c478bd9Sstevel@tonic-gate 			pty = pf->input_types;
917c478bd9Sstevel@tonic-gate 			goto Types;
927c478bd9Sstevel@tonic-gate 		case FL_OTYPS:
937c478bd9Sstevel@tonic-gate 			pty = pf->output_types;
947c478bd9Sstevel@tonic-gate Types:			(void)fdprintf(fd, "%s", FL_SEP);
957c478bd9Sstevel@tonic-gate 			sep = "";
967c478bd9Sstevel@tonic-gate 			if (pty) {
977c478bd9Sstevel@tonic-gate 				for (; pty->name; pty++) {
987c478bd9Sstevel@tonic-gate 					(void)fdprintf(fd, "%s%s", sep,
997c478bd9Sstevel@tonic-gate 						pty->name);
1007c478bd9Sstevel@tonic-gate 					sep = ",";
1017c478bd9Sstevel@tonic-gate 				}
1027c478bd9Sstevel@tonic-gate 			} else
1037c478bd9Sstevel@tonic-gate 				(void)fdprintf(fd, "%s", NAME_ANY);
1047c478bd9Sstevel@tonic-gate 			break;
1057c478bd9Sstevel@tonic-gate 		case FL_PRTRS:
1067c478bd9Sstevel@tonic-gate 			(void)fdprintf(fd, "%s", FL_SEP);
1077c478bd9Sstevel@tonic-gate 			if (pf->printers)
1087c478bd9Sstevel@tonic-gate 				fdprintlist (fd, pf->printers);
1097c478bd9Sstevel@tonic-gate 			else
1107c478bd9Sstevel@tonic-gate 				(void)fdprintf(fd, "%s", NAME_ANY);
1117c478bd9Sstevel@tonic-gate 			break;
1127c478bd9Sstevel@tonic-gate 		case FL_TMPS:
1137c478bd9Sstevel@tonic-gate 			(void)fdprintf(fd, "%s", FL_SEP);
1147c478bd9Sstevel@tonic-gate 			sep = "";
1157c478bd9Sstevel@tonic-gate 			if ((pt = pf->templates))
1167c478bd9Sstevel@tonic-gate 				for(; pt->keyword; pt++) {
1177c478bd9Sstevel@tonic-gate 					(void)fdprintf(fd, "%s%s ", sep,
1187c478bd9Sstevel@tonic-gate 						pt->keyword);
1197c478bd9Sstevel@tonic-gate 					q_print(fd, pt->pattern);
1207c478bd9Sstevel@tonic-gate 					(void)fdprintf(fd, " = ");
1217c478bd9Sstevel@tonic-gate 					q_print(fd, pt->result);
1227c478bd9Sstevel@tonic-gate 					sep = ",";
1237c478bd9Sstevel@tonic-gate 				}
1247c478bd9Sstevel@tonic-gate 			break;
1257c478bd9Sstevel@tonic-gate 		}
1267c478bd9Sstevel@tonic-gate 		(void)fdprintf(fd, FL_END);
1277c478bd9Sstevel@tonic-gate 	}
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	close(fd);
1307c478bd9Sstevel@tonic-gate 	return (0);
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate /**
1347c478bd9Sstevel@tonic-gate  ** q_print() - PRINT STRING, QUOTING SEPARATOR CHARACTERS
1357c478bd9Sstevel@tonic-gate  **/
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate static void
q_print(int fd,char * str)1387c478bd9Sstevel@tonic-gate q_print(int fd, char *str)
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate 	/*
1417c478bd9Sstevel@tonic-gate 	 * There are four reasons to quote a character: It is
1427c478bd9Sstevel@tonic-gate 	 * a quote (backslash) character, it is a field separator,
1437c478bd9Sstevel@tonic-gate 	 * it is a list separator, or it is a template separator.
1447c478bd9Sstevel@tonic-gate 	 * "loadfilters()" strips the quote (backslash), but not
1457c478bd9Sstevel@tonic-gate 	 * in one place.
1467c478bd9Sstevel@tonic-gate 	 */
1477c478bd9Sstevel@tonic-gate 	if (!str)
1487c478bd9Sstevel@tonic-gate 		return;
1497c478bd9Sstevel@tonic-gate 	while (*str) {
1507c478bd9Sstevel@tonic-gate 		if (
1517c478bd9Sstevel@tonic-gate 			*str == '\\'		/* quote reason #1 */
1527c478bd9Sstevel@tonic-gate 		     || strchr(FL_SEP, *str)	/* quote reason #2 */
1537c478bd9Sstevel@tonic-gate 		     || strchr(LP_SEP, *str)	/* quote reason #3 */
1547c478bd9Sstevel@tonic-gate 		     || strchr("=", *str)	/* quote reason #4 */
1557c478bd9Sstevel@tonic-gate 		)
1567c478bd9Sstevel@tonic-gate 			fdputc ('\\', fd);
1577c478bd9Sstevel@tonic-gate 		fdputc (*str, fd);
1587c478bd9Sstevel@tonic-gate 		str++;
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 	return;
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate /*********************************************************
1647c478bd9Sstevel@tonic-gate 
165*2a8bcb4eSToomas Soome 	fw_zDblQte
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	Duplicates the given string allocating memory
1687c478bd9Sstevel@tonic-gate 	using malloc.
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	Double quotes are used to encase the string
1717c478bd9Sstevel@tonic-gate 	and a backslash s put infront of any embedded
172*2a8bcb4eSToomas Soome 	quotes.
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	returns a pointer to the string provided.
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	It the function runs out of memory it returns
1777c478bd9Sstevel@tonic-gate 	NULL.
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate */
fw_zDblQte(char * zBuf)1817c478bd9Sstevel@tonic-gate static char *fw_zDblQte (char *zBuf)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	char *zT;
1847c478bd9Sstevel@tonic-gate 	int i;
1857c478bd9Sstevel@tonic-gate 	int j;
1867c478bd9Sstevel@tonic-gate 	int iNewSize;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	/* count the embedded double quotes */
1897c478bd9Sstevel@tonic-gate 	for (i = j = 0; zBuf[i]; i++) {
1907c478bd9Sstevel@tonic-gate 		if (zBuf[i] == '"') {
1917c478bd9Sstevel@tonic-gate 			j++;
1927c478bd9Sstevel@tonic-gate 		}
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 
195*2a8bcb4eSToomas Soome 	/*
196*2a8bcb4eSToomas Soome 		Allocate a new buffer
1977c478bd9Sstevel@tonic-gate 		add 3 extra bytes for:
1987c478bd9Sstevel@tonic-gate 			the new leading double quote
1997c478bd9Sstevel@tonic-gate 			the new trailing double quote
2007c478bd9Sstevel@tonic-gate 			and the NULL
2017c478bd9Sstevel@tonic-gate 		add an extra byte for each embedded double quote
2027c478bd9Sstevel@tonic-gate 	*/
2037c478bd9Sstevel@tonic-gate 	iNewSize = (strlen (zBuf) + 3 + j);
2047c478bd9Sstevel@tonic-gate 	if ((zT = malloc (iNewSize)) == NULL) {
2057c478bd9Sstevel@tonic-gate 		return (NULL); /* buffer overflow */
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	j = 0;
2097c478bd9Sstevel@tonic-gate 	zT[j++] = '"'; /* start with a leading double quote */
2107c478bd9Sstevel@tonic-gate 	for (i = 0; zBuf[i]; i++) {
2117c478bd9Sstevel@tonic-gate 		if (zBuf[i] == '"') {
2127c478bd9Sstevel@tonic-gate 			zT[j++] = '\\';
213*2a8bcb4eSToomas Soome 		}
2147c478bd9Sstevel@tonic-gate 		zT[j++] = zBuf[i];
2157c478bd9Sstevel@tonic-gate 	}
216*2a8bcb4eSToomas Soome 
2177c478bd9Sstevel@tonic-gate 	zT[j++] = '"'; /* add a trailing double quote */
2187c478bd9Sstevel@tonic-gate 	zT[j] = '\0';
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	return (zT);
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
223