2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 static char sccsid[] = "@(#)printcap.c 8.2 (Berkeley) 4/28/95";
47 #include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */
54 #include <sys/param.h> /* required for lp.h, but not used here */
55 #include <sys/dirent.h> /* ditto */
58 #include "pathnames.h"
61 * Routines and data used in processing the printcap file.
63 static char *printcapdb[] = { __DECONST(char *, _PATH_PRINTCAP), NULL };
65 static char *capdb_canonical_name(const char *_bp);
66 static int capdb_getaltlog(char *_bp, const char *_shrt,
68 static int capdb_getaltnum(char *_bp, const char *_shrt,
69 const char *_lng, long _dflt, long *_result);
70 static int capdb_getaltstr(char *_bp, const char *_shrt,
71 const char *lng, const char *_dflt, char **_result);
72 static int getprintcap_int(char *_bp, struct printer *_pp);
75 * Change the name of the printcap file. Used by chkprintcap(8),
76 * but could be used by other members of the suite with appropriate
80 setprintcap(char *newfile)
82 printcapdb[0] = newfile;
86 * Read the printcap database for printer `printer' into the
87 * struct printer pointed by `pp'. Return values are as for
88 * cgetent(3): -1 means we could not find what we wanted, -2
89 * means a system error occurred (and errno is set), -3 if a
90 * reference (`tc=') loop was detected, and 0 means success.
92 * Copied from lpr; should add additional capabilities as they
93 * are required by the other programs in the suite so that
94 * printcap-reading is consistent across the entire family.
97 getprintcap(const char *printer, struct printer *pp)
102 if ((status = cgetent(&bp, printcapdb, printer)) < 0)
104 status = getprintcap_int(bp, pp);
110 * Map the status values returned by cgetfirst/cgetnext into those
111 * used by cgetent, returning truth if there are more records to
112 * examine. This points out what is arguably a bug in the cget*
113 * interface (or at least a nasty wart).
116 firstnextmap(int *status)
139 * Scan through the database of printers using cgetfirst/cgetnext.
140 * Return false of error or end-of-database; else true.
143 firstprinter(struct printer *pp, int *error)
149 status = cgetfirst(&bp, printcapdb);
150 if (firstnextmap(&status) == 0) {
157 status = getprintcap_int(bp, pp);
165 nextprinter(struct printer *pp, int *error)
171 status = cgetnext(&bp, printcapdb);
172 if (firstnextmap(&status) == 0) {
179 status = getprintcap_int(bp, pp);
193 * This must match the order of declaration of enum filter in lp.h.
195 static const char *filters[] = {
196 "cf", "df", "gf", "if", "nf", "of", "rf", "tf", "vf"
199 static const char *longfilters[] = {
200 "filt.cifplot", "filt.dvi", "filt.plot", "filt.input", "filt.ditroff",
201 "filt.output", "filt.fortran", "filt.troff", "filt.raster"
205 * Internal routine for both getprintcap() and nextprinter().
206 * Actually parse the printcap entry using cget* functions.
207 * Also attempt to figure out the canonical name of the printer
208 * and store a malloced copy of it in pp->printer.
211 getprintcap_int(char *bp, struct printer *pp)
213 enum lpd_filters filt;
217 if ((pp->printer = capdb_canonical_name(bp)) == NULL)
218 return PCAPERR_OSERR;
220 #define CHK(x) do {if ((x) == PCAPERR_OSERR) return PCAPERR_OSERR;}while(0)
221 CHK(capdb_getaltstr(bp, "af", "acct.file", 0, &pp->acct_file));
222 CHK(capdb_getaltnum(bp, "br", "tty.rate", 0, &pp->baud_rate));
223 CHK(capdb_getaltnum(bp, "ct", "remote.timeout", DEFTIMEOUT,
225 CHK(capdb_getaltnum(bp, "du", "daemon.user", DEFUID,
227 CHK(capdb_getaltstr(bp, "ff", "job.formfeed", DEFFF, &pp->form_feed));
228 CHK(capdb_getaltstr(bp, "lf", "spool.log", _PATH_CONSOLE,
230 CHK(capdb_getaltstr(bp, "lo", "spool.lock", DEFLOCK, &pp->lock_file));
231 CHK(capdb_getaltstr(bp, "lp", "tty.device", _PATH_DEFDEVLP, &pp->lp));
232 CHK(capdb_getaltnum(bp, "mc", "max.copies", DEFMAXCOPIES,
234 CHK(capdb_getaltstr(bp, "ms", "tty.mode", 0, &pp->mode_set));
235 CHK(capdb_getaltnum(bp, "mx", "max.blocks", DEFMX, &pp->max_blocks));
236 CHK(capdb_getaltnum(bp, "pc", "acct.price", 0, &pp->price100));
237 CHK(capdb_getaltnum(bp, "pl", "page.length", DEFLENGTH,
239 CHK(capdb_getaltnum(bp, "pw", "page.width", DEFWIDTH,
241 CHK(capdb_getaltnum(bp, "px", "page.pwidth", 0, &pp->page_pwidth));
242 CHK(capdb_getaltnum(bp, "py", "page.plength", 0, &pp->page_plength));
243 CHK(capdb_getaltstr(bp, "rg", "daemon.restrictgrp", 0,
245 CHK(capdb_getaltstr(bp, "rm", "remote.host", 0, &pp->remote_host));
246 CHK(capdb_getaltstr(bp, "rp", "remote.queue", DEFLP,
248 CHK(capdb_getaltstr(bp, "sd", "spool.dir", _PATH_DEFSPOOL,
250 CHK(capdb_getaltstr(bp, "sr", "stat.recv", 0, &pp->stat_recv));
251 CHK(capdb_getaltstr(bp, "ss", "stat.send", 0, &pp->stat_send));
252 CHK(capdb_getaltstr(bp, "st", "spool.status", DEFSTAT,
254 CHK(capdb_getaltstr(bp, "tr", "job.trailer", 0, &pp->trailer));
256 pp->resend_copies = capdb_getaltlog(bp, "rc", "remote.resend_copies");
257 pp->restricted = capdb_getaltlog(bp, "rs", "daemon.restricted");
258 pp->short_banner = capdb_getaltlog(bp, "sb", "banner.short");
259 pp->no_copies = capdb_getaltlog(bp, "sc", "job.no_copies");
260 pp->no_formfeed = capdb_getaltlog(bp, "sf", "job.no_formfeed");
261 pp->no_header = capdb_getaltlog(bp, "sh", "banner.disable");
262 pp->header_last = capdb_getaltlog(bp, "hl", "banner.last");
263 pp->rw = capdb_getaltlog(bp, "rw", "tty.rw");
264 pp->tof = !capdb_getaltlog(bp, "fo", "job.topofform");
267 * Decide if the remote printer name matches the local printer name.
268 * If no name is given then we assume they mean them to match.
269 * If a name is given see if the rp_name is one of the names for
272 pp->rp_matches_local = 1;
273 CHK((error = capdb_getaltstr(bp, "rp", "remote.queue", 0, &rp_name)));
274 if (error != PCAPERR_NOTFOUND && rp_name != NULL) {
275 if (cgetmatch(bp,rp_name) != 0)
276 pp->rp_matches_local = 0;
283 for (filt = 0; filt < LPF_COUNT; filt++) {
284 CHK(capdb_getaltstr(bp, filters[filt], longfilters[filt], 0,
285 &pp->filters[filt]));
292 * Decode the error codes returned by cgetent() using the names we
293 * made up for them from "lp.h".
294 * This would have been much better done with Common Error, >sigh<.
295 * Perhaps this can be fixed in the next incarnation of cget*.
302 return "unresolved tc= expansion";
303 case PCAPERR_SUCCESS:
305 case PCAPERR_NOTFOUND:
306 return "printer not found";
308 return strerror(errno);
310 return "loop detected in tc= expansion";
312 return "unknown printcap error";
317 * Initialize a `struct printer' to contain values harmless to
318 * the other routines in liblpr.
321 init_printer(struct printer *pp)
323 static struct printer zero;
328 * Free the dynamically-allocated strings in a `struct printer'.
332 free_printer(struct printer *pp)
334 enum lpd_filters filt;
335 #define cfree(x) do { if (x) free(x); } while(0)
337 cfree(pp->acct_file);
338 for (filt = 0; filt < LPF_COUNT; filt++)
339 cfree(pp->filters[filt]);
340 cfree(pp->form_feed);
342 cfree(pp->lock_file);
344 cfree(pp->restrict_grp);
345 cfree(pp->remote_host);
346 cfree(pp->remote_queue);
347 cfree(pp->spool_dir);
348 cfree(pp->stat_recv);
349 cfree(pp->stat_send);
350 cfree(pp->status_file);
359 * The following routines are part of what would be a sensible library
360 * interface to capability databases. Maybe someday this will become
365 * It provides similar functionality to cgetstr(),
366 * except that it provides for both a long and a short
367 * capability name and allows for a default to be specified.
370 capdb_getaltstr(char *bp, const char *shrt, const char *lng,
371 const char *dflt, char **result)
375 status = cgetstr(bp, lng, result);
376 if (status >= 0 || status == PCAPERR_OSERR)
378 status = cgetstr(bp, shrt, result);
379 if (status >= 0 || status == PCAPERR_OSERR)
382 *result = strdup(dflt);
384 return PCAPERR_OSERR;
385 return strlen(*result);
387 return PCAPERR_NOTFOUND;
391 * The same, only for integers.
394 capdb_getaltnum(char *bp, const char *shrt, const char *lng, long dflt,
399 status = cgetnum(bp, lng, result);
402 status = cgetnum(bp, shrt, result);
410 * Likewise for logical values. There's no need for a default parameter
411 * because the default is always false.
414 capdb_getaltlog(char *bp, const char *shrt, const char *lng)
416 if (cgetcap(bp, lng, ':'))
418 if (cgetcap(bp, shrt, ':'))
424 * Also should be a part of a better cget* library.
425 * Given a capdb entry, attempt to figure out what its canonical name
426 * is, and return a malloced copy of it. The canonical name is
427 * considered to be the first one listed.
430 capdb_canonical_name(const char *bp)
435 nameend = strpbrk(bp, "|:");
438 if ((retval = malloc(nameend - bp + 1)) != NULL) {
440 strncat(retval, bp, nameend - bp);