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