xref: /freebsd/usr.sbin/lpr/common_source/printcap.c (revision 4d65a7c6951cea0333f1a0c1b32c38489cdfa6c5)
1df57947fSPedro F. Giffuni /*-
2df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
3df57947fSPedro F. Giffuni  *
44a1a0dbeSGarrett Wollman  * Copyright (c) 1983, 1993
54a1a0dbeSGarrett Wollman  *	The Regents of the University of California.  All rights reserved.
64a1a0dbeSGarrett Wollman  * (c) UNIX System Laboratories, Inc.
74a1a0dbeSGarrett Wollman  * All or some portions of this file are derived from material licensed
84a1a0dbeSGarrett Wollman  * to the University of California by American Telephone and Telegraph
94a1a0dbeSGarrett Wollman  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
104a1a0dbeSGarrett Wollman  * the permission of UNIX System Laboratories, Inc.
114a1a0dbeSGarrett Wollman  *
124a1a0dbeSGarrett Wollman  * Redistribution and use in source and binary forms, with or without
134a1a0dbeSGarrett Wollman  * modification, are permitted provided that the following conditions
144a1a0dbeSGarrett Wollman  * are met:
154a1a0dbeSGarrett Wollman  * 1. Redistributions of source code must retain the above copyright
164a1a0dbeSGarrett Wollman  *    notice, this list of conditions and the following disclaimer.
174a1a0dbeSGarrett Wollman  * 2. Redistributions in binary form must reproduce the above copyright
184a1a0dbeSGarrett Wollman  *    notice, this list of conditions and the following disclaimer in the
194a1a0dbeSGarrett Wollman  *    documentation and/or other materials provided with the distribution.
204a1a0dbeSGarrett Wollman  * 3. All advertising materials mentioning features or use of this software
214a1a0dbeSGarrett Wollman  *    must display the following acknowledgement:
224a1a0dbeSGarrett Wollman  *	This product includes software developed by the University of
234a1a0dbeSGarrett Wollman  *	California, Berkeley and its contributors.
244a1a0dbeSGarrett Wollman  * 4. Neither the name of the University nor the names of its contributors
254a1a0dbeSGarrett Wollman  *    may be used to endorse or promote products derived from this software
264a1a0dbeSGarrett Wollman  *    without specific prior written permission.
274a1a0dbeSGarrett Wollman  *
284a1a0dbeSGarrett Wollman  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
294a1a0dbeSGarrett Wollman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
304a1a0dbeSGarrett Wollman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
314a1a0dbeSGarrett Wollman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
324a1a0dbeSGarrett Wollman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
334a1a0dbeSGarrett Wollman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
344a1a0dbeSGarrett Wollman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
354a1a0dbeSGarrett Wollman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
364a1a0dbeSGarrett Wollman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
374a1a0dbeSGarrett Wollman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
384a1a0dbeSGarrett Wollman  * SUCH DAMAGE.
394a1a0dbeSGarrett Wollman  */
404a1a0dbeSGarrett Wollman 
411f589b47SGarance A Drosehn #include "lp.cdefs.h"		/* A cross-platform version of <sys/cdefs.h> */
424a1a0dbeSGarrett Wollman #include <errno.h>
434a1a0dbeSGarrett Wollman #include <stdio.h>
444a1a0dbeSGarrett Wollman #include <stdlib.h>
454a1a0dbeSGarrett Wollman #include <string.h>
464a1a0dbeSGarrett Wollman #include <unistd.h>
474a1a0dbeSGarrett Wollman 
484a1a0dbeSGarrett Wollman #include <sys/param.h>		/* required for lp.h, but not used here */
494a1a0dbeSGarrett Wollman #include <sys/dirent.h>		/* ditto */
504a1a0dbeSGarrett Wollman #include "lp.h"
514a1a0dbeSGarrett Wollman #include "lp.local.h"
524a1a0dbeSGarrett Wollman #include "pathnames.h"
534a1a0dbeSGarrett Wollman 
544a1a0dbeSGarrett Wollman /*
554a1a0dbeSGarrett Wollman  * Routines and data used in processing the printcap file.
564a1a0dbeSGarrett Wollman  */
57*a6e9cd25SEnji Cooper static char	*printcapdb[] = { __DECONST(char *, _PATH_PRINTCAP), NULL };
584a1a0dbeSGarrett Wollman 
59ba7a1ad7SGarance A Drosehn static char 	*capdb_canonical_name(const char *_bp);
60ba7a1ad7SGarance A Drosehn static int	 capdb_getaltlog(char *_bp, const char *_shrt,
61ba7a1ad7SGarance A Drosehn 		    const char *_lng);
62ba7a1ad7SGarance A Drosehn static int	 capdb_getaltnum(char *_bp, const char *_shrt,
63ba7a1ad7SGarance A Drosehn 		    const char *_lng, long _dflt, long *_result);
64ba7a1ad7SGarance A Drosehn static int	 capdb_getaltstr(char *_bp, const char *_shrt,
65ba7a1ad7SGarance A Drosehn 		    const char *lng, const char *_dflt, char **_result);
66ba7a1ad7SGarance A Drosehn static int	 getprintcap_int(char *_bp, struct printer *_pp);
674a1a0dbeSGarrett Wollman 
684a1a0dbeSGarrett Wollman /*
694a1a0dbeSGarrett Wollman  * Change the name of the printcap file.  Used by chkprintcap(8),
704a1a0dbeSGarrett Wollman  * but could be used by other members of the suite with appropriate
714a1a0dbeSGarrett Wollman  * security measures.
724a1a0dbeSGarrett Wollman  */
734a1a0dbeSGarrett Wollman void
setprintcap(char * newfile)744a1a0dbeSGarrett Wollman setprintcap(char *newfile)
754a1a0dbeSGarrett Wollman {
764a1a0dbeSGarrett Wollman 	printcapdb[0] = newfile;
774a1a0dbeSGarrett Wollman }
784a1a0dbeSGarrett Wollman 
794a1a0dbeSGarrett Wollman /*
804a1a0dbeSGarrett Wollman  * Read the printcap database for printer `printer' into the
814a1a0dbeSGarrett Wollman  * struct printer pointed by `pp'.  Return values are as for
824a1a0dbeSGarrett Wollman  * cgetent(3): -1 means we could not find what we wanted, -2
834a1a0dbeSGarrett Wollman  * means a system error occurred (and errno is set), -3 if a
844a1a0dbeSGarrett Wollman  * reference (`tc=') loop was detected, and 0 means success.
854a1a0dbeSGarrett Wollman  *
864a1a0dbeSGarrett Wollman  * Copied from lpr; should add additional capabilities as they
874a1a0dbeSGarrett Wollman  * are required by the other programs in the suite so that
884a1a0dbeSGarrett Wollman  * printcap-reading is consistent across the entire family.
894a1a0dbeSGarrett Wollman  */
904a1a0dbeSGarrett Wollman int
getprintcap(const char * printer,struct printer * pp)914a1a0dbeSGarrett Wollman getprintcap(const char *printer, struct printer *pp)
924a1a0dbeSGarrett Wollman {
934a1a0dbeSGarrett Wollman 	int status;
944a1a0dbeSGarrett Wollman 	char *bp;
954a1a0dbeSGarrett Wollman 
96*a6e9cd25SEnji Cooper 	if ((status = cgetent(&bp, printcapdb, printer)) < 0)
974a1a0dbeSGarrett Wollman 		return status;
984a1a0dbeSGarrett Wollman 	status = getprintcap_int(bp, pp);
994a1a0dbeSGarrett Wollman 	free(bp);
1004a1a0dbeSGarrett Wollman 	return status;
1014a1a0dbeSGarrett Wollman }
1024a1a0dbeSGarrett Wollman 
1034a1a0dbeSGarrett Wollman /*
1044a1a0dbeSGarrett Wollman  * Map the status values returned by cgetfirst/cgetnext into those
1054a1a0dbeSGarrett Wollman  * used by cgetent, returning truth if there are more records to
1064a1a0dbeSGarrett Wollman  * examine.  This points out what is arguably a bug in the cget*
1074a1a0dbeSGarrett Wollman  * interface (or at least a nasty wart).
1084a1a0dbeSGarrett Wollman  */
1094a1a0dbeSGarrett Wollman static int
firstnextmap(int * status)1104a1a0dbeSGarrett Wollman firstnextmap(int *status)
1114a1a0dbeSGarrett Wollman {
1124a1a0dbeSGarrett Wollman 	switch (*status) {
1134a1a0dbeSGarrett Wollman 	case 0:
1144a1a0dbeSGarrett Wollman 		return 0;
1154a1a0dbeSGarrett Wollman 	case 1:
1164a1a0dbeSGarrett Wollman 		*status = 0;
1174a1a0dbeSGarrett Wollman 		return 1;
1184a1a0dbeSGarrett Wollman 	case 2:
1194a1a0dbeSGarrett Wollman 		*status = 1;
1204a1a0dbeSGarrett Wollman 		return 1;
1214a1a0dbeSGarrett Wollman 	case -1:
1224a1a0dbeSGarrett Wollman 		*status = -2;
1234a1a0dbeSGarrett Wollman 		return 0;
1244a1a0dbeSGarrett Wollman 	case -2:
1254a1a0dbeSGarrett Wollman 		*status = -3;
1264a1a0dbeSGarrett Wollman 		return 1;
1274a1a0dbeSGarrett Wollman 	default:
1284a1a0dbeSGarrett Wollman 		return 0;
1294a1a0dbeSGarrett Wollman 	}
1304a1a0dbeSGarrett Wollman }
1314a1a0dbeSGarrett Wollman 
1324a1a0dbeSGarrett Wollman /*
1334a1a0dbeSGarrett Wollman  * Scan through the database of printers using cgetfirst/cgetnext.
1344a1a0dbeSGarrett Wollman  * Return false of error or end-of-database; else true.
1354a1a0dbeSGarrett Wollman  */
1364a1a0dbeSGarrett Wollman int
firstprinter(struct printer * pp,int * error)1374a1a0dbeSGarrett Wollman firstprinter(struct printer *pp, int *error)
1384a1a0dbeSGarrett Wollman {
1394a1a0dbeSGarrett Wollman 	int status;
1404a1a0dbeSGarrett Wollman 	char *bp;
1414a1a0dbeSGarrett Wollman 
1424a1a0dbeSGarrett Wollman 	init_printer(pp);
1434a1a0dbeSGarrett Wollman 	status = cgetfirst(&bp, printcapdb);
1444a1a0dbeSGarrett Wollman 	if (firstnextmap(&status) == 0) {
1454a1a0dbeSGarrett Wollman 		if (error)
1464a1a0dbeSGarrett Wollman 			*error = status;
1474a1a0dbeSGarrett Wollman 		return 0;
1484a1a0dbeSGarrett Wollman 	}
1494a1a0dbeSGarrett Wollman 	if (error)
1504a1a0dbeSGarrett Wollman 		*error = status;
1514a1a0dbeSGarrett Wollman 	status = getprintcap_int(bp, pp);
1524a1a0dbeSGarrett Wollman 	free(bp);
1534a1a0dbeSGarrett Wollman 	if (error && status)
1544a1a0dbeSGarrett Wollman 		*error = status;
1554a1a0dbeSGarrett Wollman 	return 1;
1564a1a0dbeSGarrett Wollman }
1574a1a0dbeSGarrett Wollman 
1584a1a0dbeSGarrett Wollman int
nextprinter(struct printer * pp,int * error)1594a1a0dbeSGarrett Wollman nextprinter(struct printer *pp, int *error)
1604a1a0dbeSGarrett Wollman {
1614a1a0dbeSGarrett Wollman 	int status;
1624a1a0dbeSGarrett Wollman 	char *bp;
1634a1a0dbeSGarrett Wollman 
1644a1a0dbeSGarrett Wollman 	free_printer(pp);
1654a1a0dbeSGarrett Wollman 	status = cgetnext(&bp, printcapdb);
1664a1a0dbeSGarrett Wollman 	if (firstnextmap(&status) == 0) {
1674a1a0dbeSGarrett Wollman 		if (error)
1684a1a0dbeSGarrett Wollman 			*error = status;
1694a1a0dbeSGarrett Wollman 		return 0;
1704a1a0dbeSGarrett Wollman 	}
1714a1a0dbeSGarrett Wollman 	if (error)
1724a1a0dbeSGarrett Wollman 		*error = status;
1734a1a0dbeSGarrett Wollman 	status = getprintcap_int(bp, pp);
1744a1a0dbeSGarrett Wollman 	free(bp);
1754a1a0dbeSGarrett Wollman 	if (error && status)
1764a1a0dbeSGarrett Wollman 		*error = status;
1774a1a0dbeSGarrett Wollman 	return 1;
1784a1a0dbeSGarrett Wollman }
1794a1a0dbeSGarrett Wollman 
1804a1a0dbeSGarrett Wollman void
lastprinter(void)1814a1a0dbeSGarrett Wollman lastprinter(void)
1824a1a0dbeSGarrett Wollman {
1834a1a0dbeSGarrett Wollman 	cgetclose();
1844a1a0dbeSGarrett Wollman }
1854a1a0dbeSGarrett Wollman 
1864a1a0dbeSGarrett Wollman /*
1874a1a0dbeSGarrett Wollman  * This must match the order of declaration of enum filter in lp.h.
1884a1a0dbeSGarrett Wollman  */
1894a1a0dbeSGarrett Wollman static const char *filters[] = {
1904a1a0dbeSGarrett Wollman 	"cf", "df", "gf", "if", "nf", "of", "rf", "tf", "vf"
1914a1a0dbeSGarrett Wollman };
1924a1a0dbeSGarrett Wollman 
1934a1a0dbeSGarrett Wollman static const char *longfilters[] = {
1944a1a0dbeSGarrett Wollman 	"filt.cifplot", "filt.dvi", "filt.plot", "filt.input", "filt.ditroff",
1954a1a0dbeSGarrett Wollman 	"filt.output", "filt.fortran", "filt.troff", "filt.raster"
1964a1a0dbeSGarrett Wollman };
1974a1a0dbeSGarrett Wollman 
1984a1a0dbeSGarrett Wollman /*
1994a1a0dbeSGarrett Wollman  * Internal routine for both getprintcap() and nextprinter().
2004a1a0dbeSGarrett Wollman  * Actually parse the printcap entry using cget* functions.
2014a1a0dbeSGarrett Wollman  * Also attempt to figure out the canonical name of the printer
2024a1a0dbeSGarrett Wollman  * and store a malloced copy of it in pp->printer.
2034a1a0dbeSGarrett Wollman  */
2044a1a0dbeSGarrett Wollman static int
getprintcap_int(char * bp,struct printer * pp)205ba7a1ad7SGarance A Drosehn getprintcap_int(char *bp, struct printer *pp)
2064a1a0dbeSGarrett Wollman {
2074a1a0dbeSGarrett Wollman 	enum lpd_filters filt;
208d5483ddfSJordan K. Hubbard 	char *rp_name;
209d5483ddfSJordan K. Hubbard 	int error;
2104a1a0dbeSGarrett Wollman 
211bc17d12dSPedro F. Giffuni 	if ((pp->printer = capdb_canonical_name(bp)) == NULL)
2124a1a0dbeSGarrett Wollman 		return PCAPERR_OSERR;
2134a1a0dbeSGarrett Wollman 
2144a1a0dbeSGarrett Wollman #define CHK(x) do {if ((x) == PCAPERR_OSERR) return PCAPERR_OSERR;}while(0)
2154a1a0dbeSGarrett Wollman 	CHK(capdb_getaltstr(bp, "af", "acct.file", 0, &pp->acct_file));
2164a1a0dbeSGarrett Wollman 	CHK(capdb_getaltnum(bp, "br", "tty.rate", 0, &pp->baud_rate));
2174a1a0dbeSGarrett Wollman 	CHK(capdb_getaltnum(bp, "ct", "remote.timeout", DEFTIMEOUT,
2184a1a0dbeSGarrett Wollman 			    &pp->conn_timeout));
2194a1a0dbeSGarrett Wollman 	CHK(capdb_getaltnum(bp, "du", "daemon.user", DEFUID,
2204a1a0dbeSGarrett Wollman 			    &pp->daemon_user));
2214a1a0dbeSGarrett Wollman 	CHK(capdb_getaltstr(bp, "ff", "job.formfeed", DEFFF, &pp->form_feed));
2224a1a0dbeSGarrett Wollman 	CHK(capdb_getaltstr(bp, "lf", "spool.log", _PATH_CONSOLE,
2234a1a0dbeSGarrett Wollman 			    &pp->log_file));
2244a1a0dbeSGarrett Wollman 	CHK(capdb_getaltstr(bp, "lo", "spool.lock", DEFLOCK, &pp->lock_file));
2254a1a0dbeSGarrett Wollman 	CHK(capdb_getaltstr(bp, "lp", "tty.device", _PATH_DEFDEVLP, &pp->lp));
2264a1a0dbeSGarrett Wollman 	CHK(capdb_getaltnum(bp, "mc", "max.copies", DEFMAXCOPIES,
2274a1a0dbeSGarrett Wollman 			    &pp->max_copies));
2284a1a0dbeSGarrett Wollman 	CHK(capdb_getaltstr(bp, "ms", "tty.mode", 0, &pp->mode_set));
2294a1a0dbeSGarrett Wollman 	CHK(capdb_getaltnum(bp, "mx", "max.blocks", DEFMX, &pp->max_blocks));
2304a1a0dbeSGarrett Wollman 	CHK(capdb_getaltnum(bp, "pc", "acct.price", 0, &pp->price100));
2314a1a0dbeSGarrett Wollman 	CHK(capdb_getaltnum(bp, "pl", "page.length", DEFLENGTH,
2324a1a0dbeSGarrett Wollman 			    &pp->page_length));
2334a1a0dbeSGarrett Wollman 	CHK(capdb_getaltnum(bp, "pw", "page.width", DEFWIDTH,
2344a1a0dbeSGarrett Wollman 			    &pp->page_width));
2354a1a0dbeSGarrett Wollman 	CHK(capdb_getaltnum(bp, "px", "page.pwidth", 0, &pp->page_pwidth));
2364a1a0dbeSGarrett Wollman 	CHK(capdb_getaltnum(bp, "py", "page.plength", 0, &pp->page_plength));
2374a1a0dbeSGarrett Wollman 	CHK(capdb_getaltstr(bp, "rg", "daemon.restrictgrp", 0,
2384a1a0dbeSGarrett Wollman 			    &pp->restrict_grp));
2394a1a0dbeSGarrett Wollman 	CHK(capdb_getaltstr(bp, "rm", "remote.host", 0, &pp->remote_host));
2404a1a0dbeSGarrett Wollman 	CHK(capdb_getaltstr(bp, "rp", "remote.queue", DEFLP,
2414a1a0dbeSGarrett Wollman 			    &pp->remote_queue));
2424a1a0dbeSGarrett Wollman 	CHK(capdb_getaltstr(bp, "sd", "spool.dir", _PATH_DEFSPOOL,
2434a1a0dbeSGarrett Wollman 			    &pp->spool_dir));
2446522ebecSGarance A Drosehn 	CHK(capdb_getaltstr(bp, "sr", "stat.recv", 0, &pp->stat_recv));
2456522ebecSGarance A Drosehn 	CHK(capdb_getaltstr(bp, "ss", "stat.send", 0, &pp->stat_send));
2464a1a0dbeSGarrett Wollman 	CHK(capdb_getaltstr(bp, "st", "spool.status", DEFSTAT,
2474a1a0dbeSGarrett Wollman 			    &pp->status_file));
2484a1a0dbeSGarrett Wollman 	CHK(capdb_getaltstr(bp, "tr", "job.trailer", 0, &pp->trailer));
2494a1a0dbeSGarrett Wollman 
25098f6cc54SGarance A Drosehn 	pp->resend_copies = capdb_getaltlog(bp, "rc", "remote.resend_copies");
2514a1a0dbeSGarrett Wollman 	pp->restricted = capdb_getaltlog(bp, "rs", "daemon.restricted");
2524a1a0dbeSGarrett Wollman 	pp->short_banner = capdb_getaltlog(bp, "sb", "banner.short");
2534a1a0dbeSGarrett Wollman 	pp->no_copies = capdb_getaltlog(bp, "sc", "job.no_copies");
2544a1a0dbeSGarrett Wollman 	pp->no_formfeed = capdb_getaltlog(bp, "sf", "job.no_formfeed");
2554a1a0dbeSGarrett Wollman 	pp->no_header = capdb_getaltlog(bp, "sh", "banner.disable");
2564a1a0dbeSGarrett Wollman 	pp->header_last = capdb_getaltlog(bp, "hl", "banner.last");
2574a1a0dbeSGarrett Wollman 	pp->rw = capdb_getaltlog(bp, "rw", "tty.rw");
2588ff467c6SGarrett Wollman 	pp->tof = !capdb_getaltlog(bp, "fo", "job.topofform");
2594a1a0dbeSGarrett Wollman 
2604a1a0dbeSGarrett Wollman 	/*
261d5483ddfSJordan K. Hubbard 	 * Decide if the remote printer name matches the local printer name.
262d5483ddfSJordan K. Hubbard 	 * If no name is given then we assume they mean them to match.
263d5483ddfSJordan K. Hubbard 	 * If a name is given see if the rp_name is one of the names for
264d5483ddfSJordan K. Hubbard 	 * this printer.
265d5483ddfSJordan K. Hubbard 	 */
266d5483ddfSJordan K. Hubbard 	pp->rp_matches_local = 1;
267d5483ddfSJordan K. Hubbard 	CHK((error = capdb_getaltstr(bp, "rp", "remote.queue", 0, &rp_name)));
268d5483ddfSJordan K. Hubbard 	if (error != PCAPERR_NOTFOUND && rp_name != NULL) {
269d5483ddfSJordan K. Hubbard 		if (cgetmatch(bp,rp_name) != 0)
270d5483ddfSJordan K. Hubbard 			pp->rp_matches_local = 0;
271d5483ddfSJordan K. Hubbard 		free(rp_name);
272d5483ddfSJordan K. Hubbard 	}
273d5483ddfSJordan K. Hubbard 
274d5483ddfSJordan K. Hubbard 	/*
2754a1a0dbeSGarrett Wollman 	 * Filters:
2764a1a0dbeSGarrett Wollman 	 */
2774a1a0dbeSGarrett Wollman 	for (filt = 0; filt < LPF_COUNT; filt++) {
2784a1a0dbeSGarrett Wollman 		CHK(capdb_getaltstr(bp, filters[filt], longfilters[filt], 0,
2794a1a0dbeSGarrett Wollman 				    &pp->filters[filt]));
2804a1a0dbeSGarrett Wollman 	}
2814a1a0dbeSGarrett Wollman 
2824a1a0dbeSGarrett Wollman 	return 0;
2834a1a0dbeSGarrett Wollman }
2844a1a0dbeSGarrett Wollman 
2854a1a0dbeSGarrett Wollman /*
2864a1a0dbeSGarrett Wollman  * Decode the error codes returned by cgetent() using the names we
2874a1a0dbeSGarrett Wollman  * made up for them from "lp.h".
2884a1a0dbeSGarrett Wollman  * This would have been much better done with Common Error, >sigh<.
2894a1a0dbeSGarrett Wollman  * Perhaps this can be fixed in the next incarnation of cget*.
2904a1a0dbeSGarrett Wollman  */
2914a1a0dbeSGarrett Wollman const char *
pcaperr(int error)2924a1a0dbeSGarrett Wollman pcaperr(int error)
2934a1a0dbeSGarrett Wollman {
2944a1a0dbeSGarrett Wollman 	switch(error) {
2954a1a0dbeSGarrett Wollman 	case PCAPERR_TCOPEN:
2964a1a0dbeSGarrett Wollman 		return "unresolved tc= expansion";
2974a1a0dbeSGarrett Wollman 	case PCAPERR_SUCCESS:
2984a1a0dbeSGarrett Wollman 		return "no error";
2994a1a0dbeSGarrett Wollman 	case PCAPERR_NOTFOUND:
3004a1a0dbeSGarrett Wollman 		return "printer not found";
3014a1a0dbeSGarrett Wollman 	case PCAPERR_OSERR:
3024a1a0dbeSGarrett Wollman 		return strerror(errno);
3034a1a0dbeSGarrett Wollman 	case PCAPERR_TCLOOP:
3044a1a0dbeSGarrett Wollman 		return "loop detected in tc= expansion";
3054a1a0dbeSGarrett Wollman 	default:
3064a1a0dbeSGarrett Wollman 		return "unknown printcap error";
3074a1a0dbeSGarrett Wollman 	}
3084a1a0dbeSGarrett Wollman }
3094a1a0dbeSGarrett Wollman 
3104a1a0dbeSGarrett Wollman /*
3114a1a0dbeSGarrett Wollman  * Initialize a `struct printer' to contain values harmless to
3124a1a0dbeSGarrett Wollman  * the other routines in liblpr.
3134a1a0dbeSGarrett Wollman  */
3144a1a0dbeSGarrett Wollman void
init_printer(struct printer * pp)3154a1a0dbeSGarrett Wollman init_printer(struct printer *pp)
3164a1a0dbeSGarrett Wollman {
3174a1a0dbeSGarrett Wollman 	static struct printer zero;
3184a1a0dbeSGarrett Wollman 	*pp = zero;
3194a1a0dbeSGarrett Wollman }
3204a1a0dbeSGarrett Wollman 
3214a1a0dbeSGarrett Wollman /*
3224a1a0dbeSGarrett Wollman  * Free the dynamically-allocated strings in a `struct printer'.
3234a1a0dbeSGarrett Wollman  * Idempotent.
3244a1a0dbeSGarrett Wollman  */
3254a1a0dbeSGarrett Wollman void
free_printer(struct printer * pp)3264a1a0dbeSGarrett Wollman free_printer(struct printer *pp)
3274a1a0dbeSGarrett Wollman {
3284a1a0dbeSGarrett Wollman 	enum lpd_filters filt;
3294a1a0dbeSGarrett Wollman #define	cfree(x)	do { if (x) free(x); } while(0)
3304a1a0dbeSGarrett Wollman 	cfree(pp->printer);
3314a1a0dbeSGarrett Wollman 	cfree(pp->acct_file);
3324a1a0dbeSGarrett Wollman 	for (filt = 0; filt < LPF_COUNT; filt++)
3334a1a0dbeSGarrett Wollman 		cfree(pp->filters[filt]);
3344a1a0dbeSGarrett Wollman 	cfree(pp->form_feed);
3354a1a0dbeSGarrett Wollman 	cfree(pp->log_file);
3364a1a0dbeSGarrett Wollman 	cfree(pp->lock_file);
3374a1a0dbeSGarrett Wollman 	cfree(pp->lp);
3384a1a0dbeSGarrett Wollman 	cfree(pp->restrict_grp);
3394a1a0dbeSGarrett Wollman 	cfree(pp->remote_host);
3404a1a0dbeSGarrett Wollman 	cfree(pp->remote_queue);
3414a1a0dbeSGarrett Wollman 	cfree(pp->spool_dir);
3426522ebecSGarance A Drosehn 	cfree(pp->stat_recv);
3436522ebecSGarance A Drosehn 	cfree(pp->stat_send);
3444a1a0dbeSGarrett Wollman 	cfree(pp->status_file);
3454a1a0dbeSGarrett Wollman 	cfree(pp->trailer);
3464a1a0dbeSGarrett Wollman 	cfree(pp->mode_set);
3474a1a0dbeSGarrett Wollman 
3484a1a0dbeSGarrett Wollman 	init_printer(pp);
3494a1a0dbeSGarrett Wollman }
3504a1a0dbeSGarrett Wollman 
3514a1a0dbeSGarrett Wollman 
3524a1a0dbeSGarrett Wollman /*
3534a1a0dbeSGarrett Wollman  * The following routines are part of what would be a sensible library
3544a1a0dbeSGarrett Wollman  * interface to capability databases.  Maybe someday this will become
3554a1a0dbeSGarrett Wollman  * the default.
3564a1a0dbeSGarrett Wollman  */
3574a1a0dbeSGarrett Wollman 
3584a1a0dbeSGarrett Wollman /*
3594a1a0dbeSGarrett Wollman  * It provides similar functionality to cgetstr(),
3604a1a0dbeSGarrett Wollman  * except that it provides for both a long and a short
3614a1a0dbeSGarrett Wollman  * capability name and allows for a default to be specified.
3624a1a0dbeSGarrett Wollman  */
3634a1a0dbeSGarrett Wollman static int
capdb_getaltstr(char * bp,const char * shrt,const char * lng,const char * dflt,char ** result)3644a1a0dbeSGarrett Wollman capdb_getaltstr(char *bp, const char *shrt, const char *lng,
3654a1a0dbeSGarrett Wollman     const char *dflt, char **result)
3664a1a0dbeSGarrett Wollman {
3674a1a0dbeSGarrett Wollman 	int status;
3684a1a0dbeSGarrett Wollman 
369*a6e9cd25SEnji Cooper 	status = cgetstr(bp, lng, result);
3704a1a0dbeSGarrett Wollman 	if (status >= 0 || status == PCAPERR_OSERR)
3714a1a0dbeSGarrett Wollman 		return status;
372*a6e9cd25SEnji Cooper 	status = cgetstr(bp, shrt, result);
3734a1a0dbeSGarrett Wollman 	if (status >= 0 || status == PCAPERR_OSERR)
3744a1a0dbeSGarrett Wollman 		return status;
3754a1a0dbeSGarrett Wollman 	if (dflt) {
3764a1a0dbeSGarrett Wollman 		*result = strdup(dflt);
377bc17d12dSPedro F. Giffuni 		if (*result == NULL)
3784a1a0dbeSGarrett Wollman 			return PCAPERR_OSERR;
3794a1a0dbeSGarrett Wollman 		return strlen(*result);
3804a1a0dbeSGarrett Wollman 	}
3814a1a0dbeSGarrett Wollman 	return PCAPERR_NOTFOUND;
3824a1a0dbeSGarrett Wollman }
3834a1a0dbeSGarrett Wollman 
3844a1a0dbeSGarrett Wollman /*
3854a1a0dbeSGarrett Wollman  * The same, only for integers.
3864a1a0dbeSGarrett Wollman  */
3874a1a0dbeSGarrett Wollman static int
capdb_getaltnum(char * bp,const char * shrt,const char * lng,long dflt,long * result)3884a1a0dbeSGarrett Wollman capdb_getaltnum(char *bp, const char *shrt, const char *lng, long dflt,
3894a1a0dbeSGarrett Wollman     long *result)
3904a1a0dbeSGarrett Wollman {
3914a1a0dbeSGarrett Wollman 	int status;
3924a1a0dbeSGarrett Wollman 
393*a6e9cd25SEnji Cooper 	status = cgetnum(bp, lng, result);
3944a1a0dbeSGarrett Wollman 	if (status >= 0)
3954a1a0dbeSGarrett Wollman 		return status;
396*a6e9cd25SEnji Cooper 	status = cgetnum(bp, shrt, result);
3974a1a0dbeSGarrett Wollman 	if (status >= 0)
3984a1a0dbeSGarrett Wollman 		return status;
3994a1a0dbeSGarrett Wollman 	*result = dflt;
4004a1a0dbeSGarrett Wollman 	return 0;
4014a1a0dbeSGarrett Wollman }
4024a1a0dbeSGarrett Wollman 
4034a1a0dbeSGarrett Wollman /*
4044a1a0dbeSGarrett Wollman  * Likewise for logical values.  There's no need for a default parameter
4054a1a0dbeSGarrett Wollman  * because the default is always false.
4064a1a0dbeSGarrett Wollman  */
4074a1a0dbeSGarrett Wollman static int
capdb_getaltlog(char * bp,const char * shrt,const char * lng)4084a1a0dbeSGarrett Wollman capdb_getaltlog(char *bp, const char *shrt, const char *lng)
4094a1a0dbeSGarrett Wollman {
410*a6e9cd25SEnji Cooper 	if (cgetcap(bp, lng, ':'))
4114a1a0dbeSGarrett Wollman 		return 1;
412*a6e9cd25SEnji Cooper 	if (cgetcap(bp, shrt, ':'))
4134a1a0dbeSGarrett Wollman 		return 1;
4144a1a0dbeSGarrett Wollman 	return 0;
4154a1a0dbeSGarrett Wollman }
4164a1a0dbeSGarrett Wollman 
4174a1a0dbeSGarrett Wollman /*
4184a1a0dbeSGarrett Wollman  * Also should be a part of a better cget* library.
4194a1a0dbeSGarrett Wollman  * Given a capdb entry, attempt to figure out what its canonical name
4204a1a0dbeSGarrett Wollman  * is, and return a malloced copy of it.  The canonical name is
4214a1a0dbeSGarrett Wollman  * considered to be the first one listed.
4224a1a0dbeSGarrett Wollman  */
4234a1a0dbeSGarrett Wollman static char *
capdb_canonical_name(const char * bp)4244a1a0dbeSGarrett Wollman capdb_canonical_name(const char *bp)
4254a1a0dbeSGarrett Wollman {
4264a1a0dbeSGarrett Wollman 	char *retval;
4274a1a0dbeSGarrett Wollman 	const char *nameend;
4284a1a0dbeSGarrett Wollman 
4294a1a0dbeSGarrett Wollman 	nameend = strpbrk(bp, "|:");
430bc17d12dSPedro F. Giffuni 	if (nameend == NULL)
4314a1a0dbeSGarrett Wollman 		nameend = bp + 1;
432bc17d12dSPedro F. Giffuni 	if ((retval = malloc(nameend - bp + 1)) != NULL) {
4334a1a0dbeSGarrett Wollman 		retval[0] = '\0';
4344a1a0dbeSGarrett Wollman 		strncat(retval, bp, nameend - bp);
4354a1a0dbeSGarrett Wollman 	}
4364a1a0dbeSGarrett Wollman 	return retval;
4374a1a0dbeSGarrett Wollman }
4384a1a0dbeSGarrett Wollman 
4394a1a0dbeSGarrett Wollman 
440