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 /*
237c478bd9Sstevel@tonic-gate  * Copyright 1996 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*48bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include "string.h"
347c478bd9Sstevel@tonic-gate #include "limits.h"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include "lpsched.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include "validate.h"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * Transform consecutive "LP_SEP" characters to a single comma and n-1 LP_SEP;
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  * BUT we'll leave LP_SEP inside single quotes alone!
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  * This is to allow the following case (and the like) to work correctly:
46*48bbca81SDaniel Hoffman  *	prtitle='standard input'
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate void
transform_WS_to_SEP(char * cp)507c478bd9Sstevel@tonic-gate transform_WS_to_SEP(char *cp)
517c478bd9Sstevel@tonic-gate {	char *p;
527c478bd9Sstevel@tonic-gate 	int inside_quote = 0;
537c478bd9Sstevel@tonic-gate 	int done_one_already = 0;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 	for (p = cp; *p != '\0'; p++) {
567c478bd9Sstevel@tonic-gate 		if (*p == '\'') {
577c478bd9Sstevel@tonic-gate 			inside_quote = (inside_quote + 1) % 2;
587c478bd9Sstevel@tonic-gate 			continue;
597c478bd9Sstevel@tonic-gate 		}
607c478bd9Sstevel@tonic-gate 		if (inside_quote)
617c478bd9Sstevel@tonic-gate 			continue;
627c478bd9Sstevel@tonic-gate 		if (*p == ' ') {
637c478bd9Sstevel@tonic-gate 			if (!done_one_already) {
647c478bd9Sstevel@tonic-gate 				*p = ',';
657c478bd9Sstevel@tonic-gate 				done_one_already = 1;
667c478bd9Sstevel@tonic-gate 			} else {
677c478bd9Sstevel@tonic-gate 				/* multiple LP_WS into one LP_SEP */
687c478bd9Sstevel@tonic-gate 			}
697c478bd9Sstevel@tonic-gate 		} else {
707c478bd9Sstevel@tonic-gate 			done_one_already = 0;
717c478bd9Sstevel@tonic-gate 		}
727c478bd9Sstevel@tonic-gate 	}
737c478bd9Sstevel@tonic-gate }
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /**
767c478bd9Sstevel@tonic-gate  ** pickfilter() - SEE IF WE CAN FIND A FILTER FOR THIS REQUEST
777c478bd9Sstevel@tonic-gate  **/
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate int
pickfilter(RSTATUS * prs,CANDIDATE * pc,FSTATUS * pfs)807c478bd9Sstevel@tonic-gate pickfilter(RSTATUS *prs, CANDIDATE *pc, FSTATUS *pfs)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	register char **	pp;
837c478bd9Sstevel@tonic-gate 	register char **	pl;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	register PSTATUS *	pps		= pc->pps;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	char *			pipes[2]	= { 0 , 0 };
887c478bd9Sstevel@tonic-gate 	char *			cp;
897c478bd9Sstevel@tonic-gate 	char *			output_type;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	char **			modes		= 0;
927c478bd9Sstevel@tonic-gate 	char **			parms		= 0;
937c478bd9Sstevel@tonic-gate 	char **			valid_printer_types;
947c478bd9Sstevel@tonic-gate 	char **			p_cpi		= 0;
957c478bd9Sstevel@tonic-gate 	char **			p_lpi		= 0;
967c478bd9Sstevel@tonic-gate 	char **			p_pwid		= 0;
977c478bd9Sstevel@tonic-gate 	char **			p_plen		= 0;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	FILTERTYPE		ret		= fl_none;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	int			got_cpi		= 0;
1027c478bd9Sstevel@tonic-gate 	int			got_lpi		= 0;
1037c478bd9Sstevel@tonic-gate 	int			got_plen	= 0;
1047c478bd9Sstevel@tonic-gate 	int			got_pwid	= 0;
1057c478bd9Sstevel@tonic-gate 	int			must_have_filter= 0;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	unsigned long		chk;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	/* fix for bugid 1097387	*/
1117c478bd9Sstevel@tonic-gate 	output_type = (char *) NULL;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	/*
1147c478bd9Sstevel@tonic-gate 	 * The bulk of the code below is building a parameter list "parms"
1157c478bd9Sstevel@tonic-gate 	 * to send to "insfilter()".
1167c478bd9Sstevel@tonic-gate 	 */
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	if (prs->request->modes) {
1197c478bd9Sstevel@tonic-gate 		cp = Strdup(prs->request->modes);
1207c478bd9Sstevel@tonic-gate 		transform_WS_to_SEP(cp);
1217c478bd9Sstevel@tonic-gate 		modes = getlist(cp, "", LP_SEP);
1227c478bd9Sstevel@tonic-gate 		Free (cp);
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	pp = parms = (char **)Malloc(
1267c478bd9Sstevel@tonic-gate 		2 * (NPARM_SPEC + lenlist(modes) + 1) * sizeof(char *)
1277c478bd9Sstevel@tonic-gate 	);
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	/*
1307c478bd9Sstevel@tonic-gate 	 * Add to the parameter list the appropriate cpi/lpi/etc.
1317c478bd9Sstevel@tonic-gate 	 * characteristics (aka ``stuff'') that will be used for
1327c478bd9Sstevel@tonic-gate 	 * this job. The printer defaults are questionable.
1337c478bd9Sstevel@tonic-gate 	 * Take this opportunity to also save the ``stuff'' in
1347c478bd9Sstevel@tonic-gate 	 * the request structure.
1357c478bd9Sstevel@tonic-gate 	 */
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	unload_str (&(prs->cpi));
1387c478bd9Sstevel@tonic-gate 	unload_str (&(prs->lpi));
1397c478bd9Sstevel@tonic-gate 	unload_str (&(prs->plen));
1407c478bd9Sstevel@tonic-gate 	unload_str (&(prs->pwid));
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	/*
1437c478bd9Sstevel@tonic-gate 	 * If a form is involved, pick up its page size and print
1447c478bd9Sstevel@tonic-gate 	 * spacing requirements.
1457c478bd9Sstevel@tonic-gate 	 */
1467c478bd9Sstevel@tonic-gate 	if (pfs) {
1477c478bd9Sstevel@tonic-gate 		if (pfs->cpi) {
1487c478bd9Sstevel@tonic-gate 			*pp++ = PARM_CPI;
1497c478bd9Sstevel@tonic-gate 			*pp++ = prs->cpi = pfs->cpi;
1507c478bd9Sstevel@tonic-gate 			got_cpi = 1;
1517c478bd9Sstevel@tonic-gate 		}
1527c478bd9Sstevel@tonic-gate 		if (pfs->lpi) {
1537c478bd9Sstevel@tonic-gate 			*pp++ = PARM_LPI;
1547c478bd9Sstevel@tonic-gate 			*pp++ = prs->lpi = pfs->lpi;
1557c478bd9Sstevel@tonic-gate 			got_lpi = 1;
1567c478bd9Sstevel@tonic-gate 		}
1577c478bd9Sstevel@tonic-gate 		if (pfs->plen) {
1587c478bd9Sstevel@tonic-gate 			*pp++ = PARM_LENGTH;
1597c478bd9Sstevel@tonic-gate 			*pp++ = prs->plen = pfs->plen;
1607c478bd9Sstevel@tonic-gate 			got_plen = 1;
1617c478bd9Sstevel@tonic-gate 		}
1627c478bd9Sstevel@tonic-gate 		if (pfs->pwid) {
1637c478bd9Sstevel@tonic-gate 			*pp++ = PARM_WIDTH;
1647c478bd9Sstevel@tonic-gate 			*pp++ = prs->pwid = pfs->pwid;
1657c478bd9Sstevel@tonic-gate 			got_pwid = 1;
1667c478bd9Sstevel@tonic-gate 		}
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	/*
1697c478bd9Sstevel@tonic-gate 	 * If no form is involved, pick up whatever page size and print
1707c478bd9Sstevel@tonic-gate 	 * spacing requirements were given by the user.
1717c478bd9Sstevel@tonic-gate 	 */
1727c478bd9Sstevel@tonic-gate 	} else {
1737c478bd9Sstevel@tonic-gate 		if (o_cpi) {
1747c478bd9Sstevel@tonic-gate 			*pp++ = PARM_CPI;
1757c478bd9Sstevel@tonic-gate 			*pp++ = prs->cpi = o_cpi;
1767c478bd9Sstevel@tonic-gate 			got_cpi = 1;
1777c478bd9Sstevel@tonic-gate 		}
1787c478bd9Sstevel@tonic-gate 		if (o_lpi) {
1797c478bd9Sstevel@tonic-gate 			*pp++ = PARM_LPI;
1807c478bd9Sstevel@tonic-gate 			*pp++ = prs->lpi = o_lpi;
1817c478bd9Sstevel@tonic-gate 			got_lpi = 1;
1827c478bd9Sstevel@tonic-gate 		}
1837c478bd9Sstevel@tonic-gate 		if (o_length) {
1847c478bd9Sstevel@tonic-gate 			*pp++ = PARM_LENGTH;
1857c478bd9Sstevel@tonic-gate 			*pp++ = prs->plen = o_length;
1867c478bd9Sstevel@tonic-gate 			got_plen = 1;
1877c478bd9Sstevel@tonic-gate 		}
1887c478bd9Sstevel@tonic-gate 		if (o_width) {
1897c478bd9Sstevel@tonic-gate 			*pp++ = PARM_WIDTH;
1907c478bd9Sstevel@tonic-gate 			*pp++ = prs->pwid = o_width;
1917c478bd9Sstevel@tonic-gate 			got_pwid = 1;
1927c478bd9Sstevel@tonic-gate 		}
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	/*
1967c478bd9Sstevel@tonic-gate 	 * Pick up whatever page size and print spacing requirements
1977c478bd9Sstevel@tonic-gate 	 * haven't been specified yet from the printer defaults.
1987c478bd9Sstevel@tonic-gate 	 *
1997c478bd9Sstevel@tonic-gate 	 * Note: The following cpi/lpi/etc are guaranteed to work
2007c478bd9Sstevel@tonic-gate 	 * for at least one type of the printer at hand, but not
2017c478bd9Sstevel@tonic-gate 	 * necessarily all types. Once we pick a type that works
2027c478bd9Sstevel@tonic-gate 	 * we'll verify that the cpi/lpi/etc stuff works, too.
2037c478bd9Sstevel@tonic-gate 	 * The code that does that assumes that we do the following last,
2047c478bd9Sstevel@tonic-gate 	 * after picking up the form and/or user stuff. If this changes,
2057c478bd9Sstevel@tonic-gate 	 * then the later code will have to be changed, too.
2067c478bd9Sstevel@tonic-gate 	 */
2077c478bd9Sstevel@tonic-gate 	if (!got_cpi && pps->cpi) {
2087c478bd9Sstevel@tonic-gate 		*pp++ = PARM_CPI;
2097c478bd9Sstevel@tonic-gate 		*(p_cpi = pp++) = prs->cpi = pps->cpi;
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate 	if (!got_lpi && pps->lpi) {
2127c478bd9Sstevel@tonic-gate 		*pp++ = PARM_LPI;
2137c478bd9Sstevel@tonic-gate 		*(p_lpi = pp++) = prs->lpi = pps->lpi;
2147c478bd9Sstevel@tonic-gate 	}
2157c478bd9Sstevel@tonic-gate 	if (!got_plen && pps->plen) {
2167c478bd9Sstevel@tonic-gate 		*pp++ = PARM_LENGTH;
2177c478bd9Sstevel@tonic-gate 		*(p_plen = pp++) = prs->plen = pps->plen;
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate 	if (!got_pwid && pps->pwid) {
2207c478bd9Sstevel@tonic-gate 		*pp++ = PARM_WIDTH;
2217c478bd9Sstevel@tonic-gate 		*(p_pwid = pp++) = prs->pwid = pps->pwid;
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	/*
2257c478bd9Sstevel@tonic-gate 	 * Pick up the number of pages, character set (the form's
2267c478bd9Sstevel@tonic-gate 	 * or the user's), the form name, the number of copies,
2277c478bd9Sstevel@tonic-gate 	 * and the modes.
2287c478bd9Sstevel@tonic-gate 	 */
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	if (prs->request->pages) {
2317c478bd9Sstevel@tonic-gate 		*pp++ = PARM_PAGES;
2327c478bd9Sstevel@tonic-gate 		*pp++ = prs->request->pages;
2337c478bd9Sstevel@tonic-gate 		must_have_filter = 1;
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	if (prs->request->charset) {
2377c478bd9Sstevel@tonic-gate 		*pp++ = PARM_CHARSET;
2387c478bd9Sstevel@tonic-gate 		*pp++ = prs->request->charset;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	} else if (pfs && pfs->form->chset) {
2417c478bd9Sstevel@tonic-gate 		*pp++ = PARM_CHARSET;
2427c478bd9Sstevel@tonic-gate 		*pp++ = pfs->form->chset;
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	if (prs->request->form) {
2467c478bd9Sstevel@tonic-gate 		*pp++ = PARM_FORM;
2477c478bd9Sstevel@tonic-gate 		*pp++ = prs->request->form;
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	if (prs->request->copies > 1) {
2517c478bd9Sstevel@tonic-gate 		*pp++ = PARM_COPIES;
2527c478bd9Sstevel@tonic-gate 		sprintf ((*pp++ = BIGGEST_NUMBER_S), "%d", prs->request->copies);
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	if (modes) {
2567c478bd9Sstevel@tonic-gate 		for (pl = modes; *pl; pl++) {
2577c478bd9Sstevel@tonic-gate 			*pp++ = PARM_MODES;
2587c478bd9Sstevel@tonic-gate 			*pp++ = *pl;
2597c478bd9Sstevel@tonic-gate 		}
2607c478bd9Sstevel@tonic-gate 		must_have_filter = 1;
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	*pp = 0;	/* null terminated list! */
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	/*
2677c478bd9Sstevel@tonic-gate 	 * If the printer type(s) are not ``unknown'', then include
2687c478bd9Sstevel@tonic-gate 	 * them as possible ``output'' type(s) to match
2697c478bd9Sstevel@tonic-gate 	 * with the user's input type (directly, or through a filter).
2707c478bd9Sstevel@tonic-gate 	 */
2717c478bd9Sstevel@tonic-gate 	if (!STREQU(*(pps->printer->printer_types), NAME_UNKNOWN))
2727c478bd9Sstevel@tonic-gate 		valid_printer_types = pc->printer_types;
2737c478bd9Sstevel@tonic-gate 	else {
2747c478bd9Sstevel@tonic-gate 		valid_printer_types = 0;
2757c478bd9Sstevel@tonic-gate 		must_have_filter = 0;
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	pc->fast = 0;
2797c478bd9Sstevel@tonic-gate 	pc->slow = 0;
2807c478bd9Sstevel@tonic-gate 	pc->output_type = 0;
2817c478bd9Sstevel@tonic-gate 	pc->flags = 0;
2827c478bd9Sstevel@tonic-gate 	ret = fl_none;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	/*
2857c478bd9Sstevel@tonic-gate 	 * If we don't really need a filter and the types match,
2867c478bd9Sstevel@tonic-gate 	 * then that's good enough. Some ``broadly defined''
2877c478bd9Sstevel@tonic-gate 	 * filters might match our needs, but if the printer
2887c478bd9Sstevel@tonic-gate 	 * can do what we need, then why pull in a filter?
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	 * Besides, Section 3.40 in the requirements imply
2937c478bd9Sstevel@tonic-gate 	 * that we don't use a filter if the printer can handle
2947c478bd9Sstevel@tonic-gate 	 * the file.
2957c478bd9Sstevel@tonic-gate 	 */
2967c478bd9Sstevel@tonic-gate 	if (!must_have_filter ) {
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 		if (
2997c478bd9Sstevel@tonic-gate 			valid_printer_types
3007c478bd9Sstevel@tonic-gate 		     && searchlist_with_terminfo(
3017c478bd9Sstevel@tonic-gate 				prs->request->input_type,
3027c478bd9Sstevel@tonic-gate 				valid_printer_types
3037c478bd9Sstevel@tonic-gate 			)
3047c478bd9Sstevel@tonic-gate 		) {
3057c478bd9Sstevel@tonic-gate 			ret = fl_both;	/* not really, but ok */
3067c478bd9Sstevel@tonic-gate 			pc->printer_type = Strdup(prs->request->input_type);
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 		} else if (
3097c478bd9Sstevel@tonic-gate 			pps->printer->input_types
3107c478bd9Sstevel@tonic-gate 		     && searchlist_with_terminfo(
3117c478bd9Sstevel@tonic-gate 				prs->request->input_type,
3127c478bd9Sstevel@tonic-gate 				pps->printer->input_types
3137c478bd9Sstevel@tonic-gate 			)
3147c478bd9Sstevel@tonic-gate 		) {
3157c478bd9Sstevel@tonic-gate 			ret = fl_both;	/* not really, but ok */
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 			/*
3187c478bd9Sstevel@tonic-gate 			 * (1) There is one printer type, might even
3197c478bd9Sstevel@tonic-gate 			 *     be ``unknown'';
3207c478bd9Sstevel@tonic-gate 			 * (2) There are several printer types, but that
3217c478bd9Sstevel@tonic-gate 			 *     means only one input type, ``simple'',
3227c478bd9Sstevel@tonic-gate 			 *     which any of the printer types can handle.
3237c478bd9Sstevel@tonic-gate 			 */
3247c478bd9Sstevel@tonic-gate 			pc->printer_type = Strdup(*(pc->printer_types));
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 		}
3277c478bd9Sstevel@tonic-gate 	}
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	/*
3307c478bd9Sstevel@tonic-gate 	 * Don't try using a filter if the user doesn't want
331*48bbca81SDaniel Hoffman 	 * a filter to be used! They would rather see an
3327c478bd9Sstevel@tonic-gate 	 * error message than (heaven forbid!) a filter being
3337c478bd9Sstevel@tonic-gate 	 * used.
3347c478bd9Sstevel@tonic-gate 	 */
3357c478bd9Sstevel@tonic-gate 	if (ret == fl_none && !(prs->request->actions & ACT_RAW)) {
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 		/*
3387c478bd9Sstevel@tonic-gate 		 * For each printer type, and each input type the printer
3397c478bd9Sstevel@tonic-gate 		 * accepts, see if we have a filter that matches the
3407c478bd9Sstevel@tonic-gate 		 * request to the printer. Each time we try, save the
3417c478bd9Sstevel@tonic-gate 		 * output type we use in case of success; we just might
3427c478bd9Sstevel@tonic-gate 		 * need that value later....
3437c478bd9Sstevel@tonic-gate 		 */
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 		for (
3467c478bd9Sstevel@tonic-gate 			pl = valid_printer_types;
3477c478bd9Sstevel@tonic-gate 			pl && *pl && ret == fl_none;
3487c478bd9Sstevel@tonic-gate 			pl++
3497c478bd9Sstevel@tonic-gate 		)
3507c478bd9Sstevel@tonic-gate 			ret = insfilter(
3517c478bd9Sstevel@tonic-gate 				pipes,
3527c478bd9Sstevel@tonic-gate 				prs->request->input_type,
3537c478bd9Sstevel@tonic-gate 				(output_type = *pl),
3547c478bd9Sstevel@tonic-gate 				*pl,
3557c478bd9Sstevel@tonic-gate 				pps->printer->name,
3567c478bd9Sstevel@tonic-gate 				parms,
3577c478bd9Sstevel@tonic-gate 				&(pc->flags)
3587c478bd9Sstevel@tonic-gate 			);
3597c478bd9Sstevel@tonic-gate 		if (ret != fl_none)
3607c478bd9Sstevel@tonic-gate 			pc->printer_type = Strdup(*pl);
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 		for (
3637c478bd9Sstevel@tonic-gate 			pl = pps->printer->input_types;
3647c478bd9Sstevel@tonic-gate 			pl && *pl && ret == fl_none;
3657c478bd9Sstevel@tonic-gate 			pl++
3667c478bd9Sstevel@tonic-gate 		)
3677c478bd9Sstevel@tonic-gate 			/*
3687c478bd9Sstevel@tonic-gate 			 * Don't waste time with check we've already made.
3697c478bd9Sstevel@tonic-gate 			 */
3707c478bd9Sstevel@tonic-gate 			if ((must_have_filter == 1) ||
3717c478bd9Sstevel@tonic-gate 				!valid_printer_types
3727c478bd9Sstevel@tonic-gate 			     || !searchlist(*pl, valid_printer_types)
3737c478bd9Sstevel@tonic-gate 			)
3747c478bd9Sstevel@tonic-gate 				/*
3757c478bd9Sstevel@tonic-gate 				 * Either we have one (or less) printer
3767c478bd9Sstevel@tonic-gate 				 * types and many input types, or we have
3777c478bd9Sstevel@tonic-gate 				 * one input type, ``simple''; regardless,
3787c478bd9Sstevel@tonic-gate 				 * using the first printer type is OK.
3797c478bd9Sstevel@tonic-gate 				 */
3807c478bd9Sstevel@tonic-gate 				ret = insfilter(
3817c478bd9Sstevel@tonic-gate 					pipes,
3827c478bd9Sstevel@tonic-gate 					prs->request->input_type,
3837c478bd9Sstevel@tonic-gate 					(output_type = *pl),
3847c478bd9Sstevel@tonic-gate 					*(pc->printer_types),
3857c478bd9Sstevel@tonic-gate 					pps->printer->name,
3867c478bd9Sstevel@tonic-gate 					parms,
3877c478bd9Sstevel@tonic-gate 					&(pc->flags)
3887c478bd9Sstevel@tonic-gate 				);
3897c478bd9Sstevel@tonic-gate 		if (ret != fl_none)
3907c478bd9Sstevel@tonic-gate 			pc->printer_type = Strdup(*(pc->printer_types));
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	}
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	/*
3957c478bd9Sstevel@tonic-gate 	 * If we were successful, check that the printer type
3967c478bd9Sstevel@tonic-gate 	 * we picked can handle the PRINTER'S cpi/lpi/etc. defaults.
3977c478bd9Sstevel@tonic-gate 	 * (We know that ALL the printer's types can handle the stuff
3987c478bd9Sstevel@tonic-gate 	 * the user gave or the stuff in the form.)
3997c478bd9Sstevel@tonic-gate 	 * Each printer's default that doesn't pass muster gets dropped.
4007c478bd9Sstevel@tonic-gate 	 * This may mean re-instantiating the filter(s) (if any).
4017c478bd9Sstevel@tonic-gate 	 */
4027c478bd9Sstevel@tonic-gate 	if (ret != fl_none && (p_cpi || p_lpi || p_pwid || p_plen)) {
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate #define	NZ(X)	((X)? *(X) : (char *)0)
4057c478bd9Sstevel@tonic-gate 		chk = chkprinter(
4067c478bd9Sstevel@tonic-gate 			pc->printer_type,
4077c478bd9Sstevel@tonic-gate 			NZ(p_cpi),
4087c478bd9Sstevel@tonic-gate 			NZ(p_lpi),
4097c478bd9Sstevel@tonic-gate 			NZ(p_plen),
4107c478bd9Sstevel@tonic-gate 			NZ(p_pwid),
4117c478bd9Sstevel@tonic-gate 			(char *)0
4127c478bd9Sstevel@tonic-gate 		);
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 		if (chk) {
4157c478bd9Sstevel@tonic-gate 			register char **	_pp;
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 			char *			hole	= "";
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 			/*
4217c478bd9Sstevel@tonic-gate 			 * Remove the offending printer defaults from the
4227c478bd9Sstevel@tonic-gate 			 * request list of cpi/lpi/etc. stuff, and punch
4237c478bd9Sstevel@tonic-gate 			 * (non-null!) holes in the parameter list.
4247c478bd9Sstevel@tonic-gate 			 */
4257c478bd9Sstevel@tonic-gate #define DROP(P,R)	if (P) {P[-1] = P[0] = hole; R = 0;} else/*EMPTY*/
4267c478bd9Sstevel@tonic-gate 			if (chk & PCK_CPI)	DROP (p_cpi, prs->cpi);
4277c478bd9Sstevel@tonic-gate 			if (chk & PCK_LPI)	DROP (p_lpi, prs->lpi);
4287c478bd9Sstevel@tonic-gate 			if (chk & PCK_WIDTH)	DROP (p_pwid, prs->pwid);
4297c478bd9Sstevel@tonic-gate 			if (chk & PCK_LENGTH)	DROP (p_plen, prs->plen);
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 			/*
4327c478bd9Sstevel@tonic-gate 			 * If there are filters, we have to re-instantiate
4337c478bd9Sstevel@tonic-gate 			 * them. (Can't check "ret" here, because it may
4347c478bd9Sstevel@tonic-gate 			 * be misleading.)
4357c478bd9Sstevel@tonic-gate 			 */
4367c478bd9Sstevel@tonic-gate 			if (pipes[0] || pipes[1]) {
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 				/*
4397c478bd9Sstevel@tonic-gate 				 * First, close up the gaps we punched in
4407c478bd9Sstevel@tonic-gate 				 * the parameter list.
4417c478bd9Sstevel@tonic-gate 				 */
4427c478bd9Sstevel@tonic-gate 				for (pp = _pp = parms; *pp; pp++)
4437c478bd9Sstevel@tonic-gate 					if (*pp != hole)
4447c478bd9Sstevel@tonic-gate 						*_pp++ = *pp;
4457c478bd9Sstevel@tonic-gate 				*_pp = 0;
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 				/*
4487c478bd9Sstevel@tonic-gate 				 * Re-instantiate the filter(s). This
4497c478bd9Sstevel@tonic-gate 				 * CAN'T fail, because it is not mandatory
4507c478bd9Sstevel@tonic-gate 				 * that filters handle cpi/lpi/etc. stuff.
4517c478bd9Sstevel@tonic-gate 				 */
4527c478bd9Sstevel@tonic-gate 				ret = insfilter(
4537c478bd9Sstevel@tonic-gate 					pipes,
4547c478bd9Sstevel@tonic-gate 					prs->request->input_type,
4557c478bd9Sstevel@tonic-gate 					output_type,
4567c478bd9Sstevel@tonic-gate 					pc->printer_type,
4577c478bd9Sstevel@tonic-gate 					pps->printer->name,
4587c478bd9Sstevel@tonic-gate 					parms,
4597c478bd9Sstevel@tonic-gate 					&(pc->flags)
4607c478bd9Sstevel@tonic-gate 				);
4617c478bd9Sstevel@tonic-gate 			}
4627c478bd9Sstevel@tonic-gate 		}
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	/*
4667c478bd9Sstevel@tonic-gate 	 * Save the filters, if any. Note: although "ret" can be
4677c478bd9Sstevel@tonic-gate 	 * misleading, i.e. set to "fl_both" when there really aren't
4687c478bd9Sstevel@tonic-gate 	 * any filters, the declaration of "pipes" ensured they'd be
4697c478bd9Sstevel@tonic-gate 	 * zero if not set.
4707c478bd9Sstevel@tonic-gate 	 */
4717c478bd9Sstevel@tonic-gate 	if (ret == fl_both || ret == fl_slow)
4727c478bd9Sstevel@tonic-gate 		pc->slow = pipes[0];
4737c478bd9Sstevel@tonic-gate 	if (ret == fl_both || ret == fl_fast)
4747c478bd9Sstevel@tonic-gate 		pc->fast = pipes[1];
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	if (ret != fl_none)
4777c478bd9Sstevel@tonic-gate 		pc->output_type = Strdup (output_type);
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	/*
4807c478bd9Sstevel@tonic-gate 	 * Wait until now to allocate storage for the cpi/etc.
4817c478bd9Sstevel@tonic-gate 	 * stuff, to make life easier above.
4827c478bd9Sstevel@tonic-gate 	 */
4837c478bd9Sstevel@tonic-gate 	if (prs->cpi)	prs->cpi = Strdup(prs->cpi);
4847c478bd9Sstevel@tonic-gate 	if (prs->lpi)	prs->lpi = Strdup(prs->lpi);
4857c478bd9Sstevel@tonic-gate 	if (prs->plen)	prs->plen = Strdup(prs->plen);
4867c478bd9Sstevel@tonic-gate 	if (prs->pwid)	prs->pwid = Strdup(prs->pwid);
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	if (parms)
4907c478bd9Sstevel@tonic-gate 		Free ((char *)parms);
4917c478bd9Sstevel@tonic-gate 	if (modes)
4927c478bd9Sstevel@tonic-gate 		freelist (modes);
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	return ((ret != fl_none));
4957c478bd9Sstevel@tonic-gate }
496