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