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