BSD 4_4_Lite2 release
[unix-history] / usr / src / usr.sbin / lpr / common_source / printcap.c
index a1d259f..cbbaef1 100644 (file)
@@ -1,16 +1,49 @@
 /*
 /*
- * Copyright (c) 1983 Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1983, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  *
- * %sccs.include.redist.c%
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)printcap.c 5.7 (Berkeley) %G%";
+static char sccsid[] = "@(#)printcap.c 8.2 (Berkeley) 4/28/95";
 #endif /* not lint */
 
 #endif /* not lint */
 
-#include <ctype.h>
+#include <sys/param.h>
+
+#include <fcntl.h>
+#include <dirent.h>
+#include <unistd.h>
 #include <stdio.h>
 #include <stdio.h>
+#include <ctype.h>
+#include <string.h>
+#include "lp.h"
 #include "pathnames.h"
 
 #ifndef BUFSIZ
 #include "pathnames.h"
 
 #ifndef BUFSIZ
@@ -18,6 +51,87 @@ static char sccsid[] = "@(#)printcap.c       5.7 (Berkeley) %G%";
 #endif
 #define MAXHOP 32      /* max number of tc= indirections */
 
 #endif
 #define MAXHOP 32      /* max number of tc= indirections */
 
+/*
+ * getcap-style interface for the old printcap routines.
+ *
+ * !!!USE THIS INTERFACE ONLY IF YOU DON'T HAVE THE REAL GETCAP!!!
+ */
+
+static char *pbp;              /* pointer into pbuf for pgetstr() */
+static char pbuf[BUFSIZ];      /* buffer for capability strings */
+extern char line[];            /* buffer for printcap entries */
+
+int
+cgetnext(bp, db_array)
+        register char **bp;
+       char **db_array;
+{
+       int ret;
+       char *strdup();
+
+       pbp = pbuf;
+       ret = getprent(line);
+       *bp = strdup(line);
+       return (ret);
+}
+
+int
+cgetent(bp, db_array, name)
+       char **bp, **db_array, *name;
+{
+       int i;
+
+       *bp = line;
+       pbp = pbuf;
+       i = pgetent(*bp, name);
+       if (i < 0)
+               return (-2);
+       else if (i == 0)
+               return (-1);
+       else
+               return (0);
+}
+
+char *
+cgetcap(buf, cap, type)
+       char *buf, *cap;
+       int type;
+{
+       return ((char *) pgetflag(cap));
+}
+
+int
+cgetstr(buf, cap, str)
+       char *buf, *cap;
+       char **str;
+{
+       char *pgetstr __P((char *, char **));
+
+       if (pbp >= pbuf+BUFSIZ) {
+               write(2, "Capability string buffer overflow\n", 34);
+               return (-1);
+       }
+       return ((*str = pgetstr(cap, &pbp)) == NULL ? -1 : 0);
+}
+
+int
+cgetnum(buf, cap, num)
+       char *buf, *cap;
+       long *num;
+{
+       return ((*num = pgetnum(cap)) < 0 ? -1 : 0);
+}
+
+int
+cgetclose()
+{
+       void endprent __P((void));
+
+       endprent();
+       return (0);
+}
+
+
 /*
  * termcap - routines for dealing with the terminal capability data base
  *
 /*
  * termcap - routines for dealing with the terminal capability data base
  *
@@ -50,15 +164,17 @@ static char sccsid[] = "@(#)printcap.c     5.7 (Berkeley) %G%";
 static FILE *pfp = NULL;       /* printcap data base file pointer */
 static char *tbuf;
 static int hopcount;           /* detect infinite loops in termcap, init 0 */
 static FILE *pfp = NULL;       /* printcap data base file pointer */
 static char *tbuf;
 static int hopcount;           /* detect infinite loops in termcap, init 0 */
-char   *tskip();
-char   *tgetstr();
-char   *tdecode();
-char   *getenv();
+static int tf;
+
+char *tgetstr __P((char *, char **));
+static char *tskip __P((char *));
+static char *tdecode __P((char *, char **));
 
 /*
  * Similar to tgetent except it returns the next enrty instead of
  * doing a lookup.
  */
 
 /*
  * Similar to tgetent except it returns the next enrty instead of
  * doing a lookup.
  */
+int
 getprent(bp)
        register char *bp;
 {
 getprent(bp)
        register char *bp;
 {
@@ -100,10 +216,21 @@ getprent(bp)
        }
 }
 
        }
 }
 
+void
 endprent()
 {
 endprent()
 {
-       if (pfp != NULL)
-               fclose(pfp);
+       if (pfp != NULL) {
+               /*
+                * Can't use fclose here because on POSIX-compliant
+                * systems, fclose() causes the file pointer of the
+                * underlying file descriptor (which is possibly shared
+                * with a parent process) to be adjusted, and this
+                * reeks havoc in the parent because it doesn't know
+                * the file pointer has changed.
+                */
+               (void) close(fileno(pfp));
+               pfp = NULL;
+       }
 }
 
 /*
 }
 
 /*
@@ -111,6 +238,7 @@ endprent()
  * from the termcap file.  Parse is very rudimentary;
  * we just notice escaped newlines.
  */
  * from the termcap file.  Parse is very rudimentary;
  * we just notice escaped newlines.
  */
+int
 tgetent(bp, name)
        char *bp, *name;
 {
 tgetent(bp, name)
        char *bp, *name;
 {
@@ -118,11 +246,8 @@ tgetent(bp, name)
        register int c;
        register int i = 0, cnt = 0;
        char ibuf[BUFSIZ];
        register int c;
        register int i = 0, cnt = 0;
        char ibuf[BUFSIZ];
-       char *cp2;
-       int tf;
 
        tbuf = bp;
 
        tbuf = bp;
-       tf = 0;
 #ifndef V6
        cp = getenv("TERMCAP");
        /*
 #ifndef V6
        cp = getenv("TERMCAP");
        /*
@@ -144,11 +269,9 @@ tgetent(bp, name)
                } else
                        tf = open(cp, 0);
        }
                } else
                        tf = open(cp, 0);
        }
+#endif
        if (tf==0)
                tf = open(_PATH_PRINTCAP, 0);
        if (tf==0)
                tf = open(_PATH_PRINTCAP, 0);
-#else
-       tf = open(_PATH_PRINTCAP, 0);
-#endif
        if (tf < 0)
                return (-1);
        for (;;) {
        if (tf < 0)
                return (-1);
        for (;;) {
@@ -158,6 +281,7 @@ tgetent(bp, name)
                                cnt = read(tf, ibuf, BUFSIZ);
                                if (cnt <= 0) {
                                        close(tf);
                                cnt = read(tf, ibuf, BUFSIZ);
                                if (cnt <= 0) {
                                        close(tf);
+                                       tf = 0;
                                        return (0);
                                }
                                i = 0;
                                        return (0);
                                }
                                i = 0;
@@ -182,8 +306,13 @@ tgetent(bp, name)
                 * The real work for the match.
                 */
                if (tnamatch(name)) {
                 * The real work for the match.
                 */
                if (tnamatch(name)) {
-                       close(tf);
-                       return(tnchktc());
+                       lseek(tf, 0L, 0);
+                       i = tnchktc();
+                       if (tf) {
+                               close(tf);
+                               tf = 0;
+                       }
+                       return(i);
                }
        }
 }
                }
        }
 }
@@ -195,6 +324,7 @@ tgetent(bp, name)
  * entries to say "like an HP2621 but doesn't turn on the labels".
  * Note that this works because of the left to right scan.
  */
  * entries to say "like an HP2621 but doesn't turn on the labels".
  * Note that this works because of the left to right scan.
  */
+int
 tnchktc()
 {
        register char *p, *q;
 tnchktc()
 {
        register char *p, *q;
@@ -242,6 +372,7 @@ tnchktc()
  * against each such name.  The normal : terminator after the last
  * name (before the first field) stops us.
  */
  * against each such name.  The normal : terminator after the last
  * name (before the first field) stops us.
  */
+int
 tnamatch(np)
        char *np;
 {
 tnamatch(np)
        char *np;
 {
@@ -288,6 +419,7 @@ tskip(bp)
  * a # character.  If the option is not found we return -1.
  * Note that we handle octal numbers beginning with 0.
  */
  * a # character.  If the option is not found we return -1.
  * Note that we handle octal numbers beginning with 0.
  */
+int
 tgetnum(id)
        char *id;
 {
 tgetnum(id)
        char *id;
 {
@@ -321,6 +453,7 @@ tgetnum(id)
  * of the buffer.  Return 1 if we find the option, or 0 if it is
  * not given.
  */
  * of the buffer.  Return 1 if we find the option, or 0 if it is
  * not given.
  */
+int
 tgetflag(id)
        char *id;
 {
 tgetflag(id)
        char *id;
 {