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