2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * From: @(#)common.c 8.5 (Berkeley) 4/28/95
40 * $FreeBSD: src/usr.sbin/lpr/common_source/printcap.c,v 1.6.2.3 2002/04/28 23:29:10 gad Exp $
41 * $DragonFly: src/usr.sbin/lpr/common_source/printcap.c,v 1.3 2004/03/22 22:32:50 cpressey Exp $
50 #include <sys/param.h> /* required for lp.h, but not used here */
51 #include <sys/dirent.h> /* ditto */
54 #include "pathnames.h"
57 * Routines and data used in processing the printcap file.
59 static char *printcapdb[2] = { _PATH_PRINTCAP, 0 }; /* list for cget* */
61 static char *capdb_canonical_name(const char *_bp);
62 static int capdb_getaltlog(char *_bp, const char *_shrt,
64 static int capdb_getaltnum(char *_bp, const char *_shrt,
65 const char *_lng, long _dflt, long *_result);
66 static int capdb_getaltstr(char *_bp, const char *_shrt,
67 const char *lng, const char *_dflt, char **_result);
68 static int getprintcap_int(char *_bp, struct printer *_pp);
71 * Change the name of the printcap file. Used by chkprintcap(8),
72 * but could be used by other members of the suite with appropriate
76 setprintcap(char *newfile)
78 printcapdb[0] = newfile;
82 * Read the printcap database for printer `printer' into the
83 * struct printer pointed by `pp'. Return values are as for
84 * cgetent(3): -1 means we could not find what we wanted, -2
85 * means a system error occurred (and errno is set), -3 if a
86 * reference (`tc=') loop was detected, and 0 means success.
88 * Copied from lpr; should add additional capabilities as they
89 * are required by the other programs in the suite so that
90 * printcap-reading is consistent across the entire family.
93 getprintcap(const char *printer, struct printer *pp)
100 * A bug in the declaration of cgetent(3) means that we have
101 * to hide the constness of its third argument.
103 XXX = (char *)printer;
104 if ((status = cgetent(&bp, printcapdb, XXX)) < 0)
106 status = getprintcap_int(bp, pp);
112 * Map the status values returned by cgetfirst/cgetnext into those
113 * used by cgetent, returning truth if there are more records to
114 * examine. This points out what is arguably a bug in the cget*
115 * interface (or at least a nasty wart).
118 firstnextmap(int *status)
141 * Scan through the database of printers using cgetfirst/cgetnext.
142 * Return false of error or end-of-database; else true.
145 firstprinter(struct printer *pp, int *error)
151 status = cgetfirst(&bp, printcapdb);
152 if (firstnextmap(&status) == 0) {
159 status = getprintcap_int(bp, pp);
167 nextprinter(struct printer *pp, int *error)
173 status = cgetnext(&bp, printcapdb);
174 if (firstnextmap(&status) == 0) {
181 status = getprintcap_int(bp, pp);
195 * This must match the order of declaration of enum filter in lp.h.
197 static const char *filters[] = {
198 "cf", "df", "gf", "if", "nf", "of", "rf", "tf", "vf"
201 static const char *longfilters[] = {
202 "filt.cifplot", "filt.dvi", "filt.plot", "filt.input", "filt.ditroff",
203 "filt.output", "filt.fortran", "filt.troff", "filt.raster"
207 * Internal routine for both getprintcap() and nextprinter().
208 * Actually parse the printcap entry using cget* functions.
209 * Also attempt to figure out the canonical name of the printer
210 * and store a malloced copy of it in pp->printer.
213 getprintcap_int(char *bp, struct printer *pp)
215 enum lpd_filters filt;
219 if ((pp->printer = capdb_canonical_name(bp)) == 0)
220 return PCAPERR_OSERR;
222 #define CHK(x) do {if ((x) == PCAPERR_OSERR) return PCAPERR_OSERR;}while(0)
223 CHK(capdb_getaltstr(bp, "af", "acct.file", 0, &pp->acct_file));
224 CHK(capdb_getaltnum(bp, "br", "tty.rate", 0, &pp->baud_rate));
225 CHK(capdb_getaltnum(bp, "ct", "remote.timeout", DEFTIMEOUT,
227 CHK(capdb_getaltnum(bp, "du", "daemon.user", DEFUID,
229 CHK(capdb_getaltstr(bp, "ff", "job.formfeed", DEFFF, &pp->form_feed));
230 CHK(capdb_getaltstr(bp, "lf", "spool.log", _PATH_CONSOLE,
232 CHK(capdb_getaltstr(bp, "lo", "spool.lock", DEFLOCK, &pp->lock_file));
233 CHK(capdb_getaltstr(bp, "lp", "tty.device", _PATH_DEFDEVLP, &pp->lp));
234 CHK(capdb_getaltnum(bp, "mc", "max.copies", DEFMAXCOPIES,
236 CHK(capdb_getaltstr(bp, "ms", "tty.mode", 0, &pp->mode_set));
237 CHK(capdb_getaltnum(bp, "mx", "max.blocks", DEFMX, &pp->max_blocks));
238 CHK(capdb_getaltnum(bp, "pc", "acct.price", 0, &pp->price100));
239 CHK(capdb_getaltnum(bp, "pl", "page.length", DEFLENGTH,
241 CHK(capdb_getaltnum(bp, "pw", "page.width", DEFWIDTH,
243 CHK(capdb_getaltnum(bp, "px", "page.pwidth", 0, &pp->page_pwidth));
244 CHK(capdb_getaltnum(bp, "py", "page.plength", 0, &pp->page_plength));
245 CHK(capdb_getaltstr(bp, "rg", "daemon.restrictgrp", 0,
247 CHK(capdb_getaltstr(bp, "rm", "remote.host", 0, &pp->remote_host));
248 CHK(capdb_getaltstr(bp, "rp", "remote.queue", DEFLP,
250 CHK(capdb_getaltstr(bp, "sd", "spool.dir", _PATH_DEFSPOOL,
252 CHK(capdb_getaltstr(bp, "sr", "stat.recv", 0, &pp->stat_recv));
253 CHK(capdb_getaltstr(bp, "ss", "stat.send", 0, &pp->stat_send));
254 CHK(capdb_getaltstr(bp, "st", "spool.status", DEFSTAT,
256 CHK(capdb_getaltstr(bp, "tr", "job.trailer", 0, &pp->trailer));
258 pp->resend_copies = capdb_getaltlog(bp, "rc", "remote.resend_copies");
259 pp->restricted = capdb_getaltlog(bp, "rs", "daemon.restricted");
260 pp->short_banner = capdb_getaltlog(bp, "sb", "banner.short");
261 pp->no_copies = capdb_getaltlog(bp, "sc", "job.no_copies");
262 pp->no_formfeed = capdb_getaltlog(bp, "sf", "job.no_formfeed");
263 pp->no_header = capdb_getaltlog(bp, "sh", "banner.disable");
264 pp->header_last = capdb_getaltlog(bp, "hl", "banner.last");
265 pp->rw = capdb_getaltlog(bp, "rw", "tty.rw");
266 pp->tof = !capdb_getaltlog(bp, "fo", "job.topofform");
269 * Decide if the remote printer name matches the local printer name.
270 * If no name is given then we assume they mean them to match.
271 * If a name is given see if the rp_name is one of the names for
274 pp->rp_matches_local = 1;
275 CHK((error = capdb_getaltstr(bp, "rp", "remote.queue", 0, &rp_name)));
276 if (error != PCAPERR_NOTFOUND && rp_name != NULL) {
277 if (cgetmatch(bp,rp_name) != 0)
278 pp->rp_matches_local = 0;
285 for (filt = 0; filt < LPF_COUNT; filt++) {
286 CHK(capdb_getaltstr(bp, filters[filt], longfilters[filt], 0,
287 &pp->filters[filt]));
294 * Decode the error codes returned by cgetent() using the names we
295 * made up for them from "lp.h".
296 * This would have been much better done with Common Error, >sigh<.
297 * Perhaps this can be fixed in the next incarnation of cget*.
304 return "unresolved tc= expansion";
305 case PCAPERR_SUCCESS:
307 case PCAPERR_NOTFOUND:
308 return "printer not found";
310 return strerror(errno);
312 return "loop detected in tc= expansion";
314 return "unknown printcap error";
319 * Initialize a `struct printer' to contain values harmless to
320 * the other routines in liblpr.
323 init_printer(struct printer *pp)
325 static struct printer zero;
330 * Free the dynamically-allocated strings in a `struct printer'.
334 free_printer(struct printer *pp)
336 enum lpd_filters filt;
337 #define cfree(x) do { if (x) free(x); } while(0)
339 cfree(pp->acct_file);
340 for (filt = 0; filt < LPF_COUNT; filt++)
341 cfree(pp->filters[filt]);
342 cfree(pp->form_feed);
344 cfree(pp->lock_file);
346 cfree(pp->restrict_grp);
347 cfree(pp->remote_host);
348 cfree(pp->remote_queue);
349 cfree(pp->spool_dir);
350 cfree(pp->stat_recv);
351 cfree(pp->stat_send);
352 cfree(pp->status_file);
360 * The following routines are part of what would be a sensible library
361 * interface to capability databases. Maybe someday this will become
366 * It provides similar functionality to cgetstr(),
367 * except that it provides for both a long and a short
368 * capability name and allows for a default to be specified.
371 capdb_getaltstr(char *bp, const char *shrt, const char *lng,
372 const char *dflt, char **result)
376 status = cgetstr(bp, (char *)/*XXX*/lng, result);
377 if (status >= 0 || status == PCAPERR_OSERR)
379 status = cgetstr(bp, (char *)/*XXX*/shrt, result);
380 if (status >= 0 || status == PCAPERR_OSERR)
383 *result = strdup(dflt);
385 return PCAPERR_OSERR;
386 return strlen(*result);
388 return PCAPERR_NOTFOUND;
392 * The same, only for integers.
395 capdb_getaltnum(char *bp, const char *shrt, const char *lng, long dflt,
400 status = cgetnum(bp, (char *)/*XXX*/lng, result);
403 status = cgetnum(bp, (char *)/*XXX*/shrt, result);
411 * Likewise for logical values. There's no need for a default parameter
412 * because the default is always false.
415 capdb_getaltlog(char *bp, const char *shrt, const char *lng)
417 if (cgetcap(bp, (char *)/*XXX*/lng, ':'))
419 if (cgetcap(bp, (char *)/*XXX*/shrt, ':'))
425 * Also should be a part of a better cget* library.
426 * Given a capdb entry, attempt to figure out what its canonical name
427 * is, and return a malloced copy of it. The canonical name is
428 * considered to be the first one listed.
431 capdb_canonical_name(const char *bp)
436 nameend = strpbrk(bp, "|:");
439 if ((retval = malloc(nameend - bp + 1)) != 0) {
441 strncat(retval, bp, nameend - bp);