merge common code from dmf and dmz drivers
[unix-history] / usr / src / usr.sbin / lpr / lpq / lpq.c
CommitLineData
d0aeaf5a
DF
1/*
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
7#ifndef lint
8char copyright[] =
9"@(#) Copyright (c) 1983 Regents of the University of California.\n\
10 All rights reserved.\n";
11#endif not lint
12
5f84f8f0 13#ifndef lint
126fc76f 14static char sccsid[] = "@(#)lpq.c 5.2 (Berkeley) %G%";
d0aeaf5a 15#endif not lint
5f84f8f0 16
dd8e3649
RC
17/*
18 * Spool Queue examination program
19 *
20 * lpq [+[n]] [-Pprinter] [user...] [job...]
21 *
22 * + means continually scan queue until empty
23 * -P used to identify printer as per lpr/lprm
24 */
25
26#include "lp.h"
27
28char *user[MAXUSERS]; /* users to process */
29int users; /* # of users in user array */
30int requ[MAXREQUESTS]; /* job number of spool entries */
31int requests; /* # of spool requests */
32
54266d1f
RC
33static int repeat; /* + flag indicator */
34static int slptime = 30; /* pause between screen refereshes */
35static int lflag; /* long output option */
dd8e3649
RC
36
37/*
38 * Termcap stuff for fancy display
39 */
40#ifdef TERMCAP
41struct sgttyb sbuf;
54266d1f
RC
42static unsigned ospeed;
43static int dumb; /* whether to use capabilities */
44static char PC; /* pad character for output */
45static char *UP; /* up one line */
46static char *BC; /* backspace character, other than \b */
47static char *CM; /* cursor motion */
48static char *CL; /* clear display */
49static char *TI; /* terminal init for CM */
50static char *TE; /* terminal clear for CM */
51static char *SO; /* stand out start */
52static char *SE; /* stand out end */
dd8e3649
RC
53
54char *tgetstr();
55int putch(); /* for tputs' */
56#endif
57
58main(argc, argv)
59 char *argv[];
60{
61 register char *arg;
62 register int n;
63
4d4caa50
RC
64 name = argv[0];
65 gethostname(host, sizeof(host));
126fc76f 66 openlog("lpd", 0, LOG_LPR);
4d4caa50 67
dd8e3649
RC
68 while (--argc) {
69 if ((arg = *++argv)[0] == '+') {
70 if (arg[1] != '\0')
71 if ((n = atoi(&arg[1])) > 0)
72 slptime = n;
73 repeat++;
74 } else if (arg[0] == '-')
75 switch (arg[1]) {
76 case 'P': /* printer name */
e5be50e6
RC
77 if (arg[2])
78 printer = &arg[2];
79 else if (argc > 1) {
80 argc--;
81 printer = *++argv;
82 }
dd8e3649
RC
83 break;
84
85 case 'l': /* long output */
86 lflag++;
87 break;
88
89 default:
90 usage();
91 } else {
92 if (isdigit(arg[0])) {
93 if (requests >= MAXREQUESTS)
94 fatal("too many requests");
95 requ[requests++] = atoi(arg);
96 } else {
97 if (users >= MAXUSERS)
98 fatal("too many users");
99 user[users++] = arg;
100 }
101 }
102 }
103 if (printer == NULL && (printer = getenv("PRINTER")) == NULL)
104 printer = DEFLP;
dd8e3649
RC
105#ifdef TERMCAP
106 dumb = termcap();
107#endif
108
109 if (repeat) {
110#ifdef TERMCAP
111 if (TI)
112 tputs(TI, 0, putch);
113#endif
114 do {
115#ifdef TERMCAP
116 if (!dumb) {
117 tputs(CL, 0, putch);
118 tputs(tgoto(CM, 0, 0), 0, putch);
119 }
120#endif
121 if ((n = displayq(lflag)) > 0)
122 sleep(slptime);
123 } while (n > 0);
124#ifdef TERMCAP
125 if (!dumb) {
126 standout(stdout, "Hit return to continue");
127 while (getchar() != '\n');
128 if (TE)
129 tputs(TE, 0, putch);
130 }
131#endif
132 } else
133 displayq(lflag);
1e7e612e 134 exit(0);
dd8e3649
RC
135}
136
54266d1f 137static
dd8e3649
RC
138usage()
139{
140 printf("usage: lpq [-Pprinter] [-l] [+[n]] [user...] [job...]\n");
141 exit(1);
142}
143
144/*
145 * If we have the capability, print this in standout mode
146 */
54266d1f 147static
dd8e3649
RC
148standout(f, s, a1, a2)
149 FILE *f;
150 char *s;
151{
152#ifdef TERMCAP
153 if (SO)
154 tputs(SO, 0, putch);
155 fprintf(f, s, a1, a2);
156 if (SO && SE)
157 tputs(SE, 0, putch);
158#else
159 fprintf(f, s, a1, a2);
160#endif
161}
162
163#ifdef TERMCAP
54266d1f 164static char *
dd8e3649
RC
165capstrings[] = {
166 "bc", "cl", "cm", "so", "se", "ti", "te", "up",
167 0
168};
169
54266d1f 170static char **
dd8e3649
RC
171caps[] = {
172 &BC, &CL, &CM, &SO, &SE, &TI, &TE, &UP,
173};
174
175/*
176 * All we need from termcap is to clear screen and
177 * position cursor at the top; if these aren't available
178 * we say the terminal is dumb and let things scroll
179 */
54266d1f 180static
dd8e3649
RC
181termcap()
182{
183 char *term, tbuf[BUFSIZ];
184 static char buf[BUFSIZ/2];
185 register short columns;
186 char *bp = buf;
187 register char **p, ***q, *cp;
dd8e3649
RC
188
189 ioctl(0, TIOCGETP, (char *)&sbuf);
190 ospeed = sbuf.sg_ospeed;
191 if ((term = getenv("TERM")) != NULL && tgetent(tbuf, term) > 0) {
192 for (p = capstrings, q = caps; *p != NULL; p++, q++)
193 **q = tgetstr(*p, &bp);
194 if ((cp = tgetstr("pc", &bp)) != NULL)
195 PC = *cp;
dd8e3649
RC
196 }
197 return(CL == NULL || CM == NULL);
198}
199
200/*
201 * Putchar writearound for tputs
202 */
54266d1f 203static
dd8e3649
RC
204putch(c)
205 char c;
206{
207 putchar(c);
208}
209#endif