2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static char sccsid[] = "@(#)displayq.c 8.4 (Berkeley) 4/28/95";
36 #include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
51 #define psignal foil_gcc_psignal
52 #define sys_siglist foil_gcc_siglist
59 #include "pathnames.h"
62 * Routines to display the state of the queue.
64 #define JOBCOL 40 /* column for job # in -l format */
65 #define OWNCOL 7 /* start of Owner column in normal */
66 #define SIZCOL 62 /* start of Size column in normal */
69 * isprint() takes a parameter of 'int', but expect values in the range
70 * of unsigned char. Define a wrapper which takes a value of type 'char',
71 * whether signed or unsigned, and ensure it ends up in the right range.
73 #define isprintch(Anychar) isprint((u_char)(Anychar))
76 * Stuff for handling job specifications
78 static int col; /* column on screen */
79 static char current[MAXNAMLEN+1]; /* current file being printed */
80 static char file[MAXNAMLEN+1]; /* print file name */
81 static int first; /* first file in ``files'' column? */
82 static int garbage; /* # of garbage cf files */
83 static int lflag; /* long output option */
84 static int rank; /* order to be printed (-1=none, 0=active) */
85 static long totsize; /* total print job size in bytes */
87 static const char *head0 = "Rank Owner Job Files";
88 static const char *head1 = "Total Size\n";
90 static void alarmhandler(int _signo);
91 static void filtered_write(char *_obuffer, int _wlen, FILE *_wstream);
92 static void daemonwarn(const struct printer *_pp);
95 * Display the current state of the queue. Format = 1 if long format.
98 displayq(struct printer *pp, int format)
100 register struct jobqueue *q;
101 register int i, nitems, fd, ret;
103 struct jobqueue **queue;
106 void (*savealrm)(int);
112 if ((cp = checkremote(pp))) {
113 printf("Warning: %s\n", cp);
118 * Print out local queue
119 * Find all the control files in the spooling directory
122 if (chdir(pp->spool_dir) < 0)
123 fatal(pp, "cannot chdir to spooling directory: %s",
126 if ((nitems = getq(pp, &queue)) < 0)
127 fatal(pp, "cannot examine spooling area\n");
129 ret = stat(pp->lock_file, &statb);
132 if (statb.st_mode & LFM_PRINT_DIS) {
134 printf("%s: ", local_host);
135 printf("Warning: %s is down: ", pp->printer);
137 fd = open(pp->status_file, O_RDONLY|O_SHLOCK);
140 while ((i = read(fd, line, sizeof(line))) > 0)
141 (void) fwrite(line, 1, i, stdout);
142 (void) close(fd); /* unlocks as well */
146 if (statb.st_mode & LFM_QUEUE_DIS) {
148 printf("%s: ", local_host);
149 printf("Warning: %s queue is turned off\n",
156 fp = fopen(pp->lock_file, "r");
163 endp = cp + sizeof(current) - 1;
164 while ((i = getc(fp)) != EOF && i != '\n') {
180 /* read current file name */
182 endp = cp + sizeof(current) - 1;
183 while ((i = getc(fp)) != EOF && i != '\n') {
189 * Print the status file.
192 printf("%s: ", local_host);
194 fd = open(pp->status_file, O_RDONLY|O_SHLOCK);
197 while ((i = read(fd, line,
199 fwrite(line, 1, i, stdout);
200 close(fd); /* unlocks as well */
207 * Now, examine the control files and print out the jobs to
208 * be done for each user.
212 for (i = 0; i < nitems; i++) {
214 inform(pp, q->job_cfname);
226 * Print foreign queue
227 * Note that a file in transit may show up in either queue.
231 (void) snprintf(line, sizeof(line), "%c%s", format ? '\4' : '\3',
234 for (i = 0; i < requests && cp-line+10 < sizeof(line) - 1; i++) {
236 (void) sprintf(cp, " %d", requ[i]);
238 for (i = 0; i < users && cp - line + 1 + strlen(user[i]) <
239 sizeof(line) - 1; i++) {
242 (void) strcpy(cp, user[i]);
245 savealrm = signal(SIGALRM, alarmhandler);
246 alarm(pp->conn_timeout);
247 fd = getport(pp, pp->remote_host, 0);
249 (void)signal(SIGALRM, savealrm);
251 if (from_host != local_host)
252 printf("%s: ", local_host);
253 printf("connection to %s is down\n", pp->remote_host);
257 if (write(fd, line, i) != i)
258 fatal(pp, "Lost connection");
259 while ((i = read(fd, line, sizeof(line))) > 0)
260 filtered_write(line, i, stdout);
261 filtered_write(NULL, -1, stdout);
267 * The lpq-info read from remote hosts may contain unprintable characters,
268 * or carriage-returns instead of line-feeds. Clean those up before echoing
269 * the lpq-info line(s) to stdout. The info may also be missing any kind of
270 * end-of-line character. This also turns CRLF and LFCR into a plain LF.
272 * This routine may be called multiple times to process a single set of
273 * information, and after a set is finished this routine must be called
274 * one extra time with NULL specified as the buffer address.
277 filtered_write(char *wbuffer, int wlen, FILE *wstream)
279 static char lastchar, savedchar;
280 char *chkptr, *dest_end, *dest_ch, *nxtptr, *w_end;
282 char destbuf[BUFSIZ];
284 if (wbuffer == NULL) {
285 if (savedchar != '\0') {
286 if (savedchar == '\r')
288 fputc(savedchar, wstream);
289 lastchar = savedchar;
292 if (lastchar != '\0' && lastchar != '\n')
293 fputc('\n', wstream);
298 dest_ch = &destbuf[0];
299 dest_end = dest_ch + sizeof(destbuf);
301 w_end = wbuffer + wlen;
303 if (savedchar != '\0') {
309 while (chkptr < w_end) {
310 if (nxtptr < w_end) {
311 if ((*chkptr == '\r' && *nxtptr == '\n') ||
312 (*chkptr == '\n' && *nxtptr == '\r')) {
314 /* want to skip past that second character */
319 /* This is the last byte in the buffer given on this
320 * call, so check if it could be the first-byte of a
321 * significant two-byte sequence. If it is, then
322 * don't write it out now, but save for checking in
326 if (*chkptr == '\r' || *chkptr == '\n') {
333 #if 0 /* XXX - don't translate unprintable characters (yet) */
334 else if (*chkptr != '\t' && *chkptr != '\n' &&
339 *dest_ch++ = *chkptr;
344 if (dest_ch >= dest_end) {
345 destlen = dest_ch - &destbuf[0];
346 fwrite(destbuf, 1, destlen, wstream);
347 lastchar = destbuf[destlen - 1];
348 dest_ch = &destbuf[0];
351 destlen = dest_ch - &destbuf[0];
353 fwrite(destbuf, 1, destlen, wstream);
354 lastchar = destbuf[destlen - 1];
359 * Print a warning message if there is no daemon present.
362 daemonwarn(const struct printer *pp)
365 printf("%s: ", local_host);
366 puts("Warning: no daemon present");
371 * Print the header for the short listing format
377 col = strlen(head0)+1;
383 inform(const struct printer *pp, char *cf)
386 char savedname[MAXPATHLEN+1];
390 * There's a chance the control file has gone away
391 * in the meantime; if this is the case just keep going
394 if ((cfp = fopen(cf, "r")) == NULL)
400 if (pp->remote || garbage || strcmp(cf, current))
404 * The cf-file may include commands to print more than one datafile
405 * from the user. For each datafile, the cf-file contains at least
406 * one line which starts with some format-specifier ('a'-'z'), and
407 * a second line ('N'ame) which indicates the original name the user
408 * specified for that file. There can be multiple format-spec lines
409 * for a single Name-line, if the user requested multiple copies of
410 * that file. Standard lpr puts the format-spec line(s) before the
411 * Name-line, while lprNG puts the Name-line before the format-spec
412 * line(s). This section needs to handle the lines in either order.
417 jnum = calc_jobnum(cf, NULL);
418 while (getline(cfp)) {
420 case 'P': /* Was this file specified in the user's list? */
421 if (!inlist(line+1, cf)) {
426 printf("\n%s: ", line+1);
427 col = strlen(line+1) + 2;
430 printf(" [job %s]\n", cf+3);
435 printf("%-10s %-3d ", line+1, jnum);
440 default: /* some format specifer and file name? */
441 if (line[0] < 'a' || line[0] > 'z')
443 if (copycnt == 0 || strcmp(file, line+1) != 0) {
444 strlcpy(file, line + 1, sizeof(file));
448 * deliberately 'continue' to another getline(), so
449 * all format-spec lines for this datafile are read
450 * in and counted before calling show()
454 strlcpy(savedname, line + 1, sizeof(savedname));
457 if ((file[0] != '\0') && (savedname[0] != '\0')) {
458 show(savedname, file, copycnt);
465 /* check for a file which hasn't been shown yet */
466 if (file[0] != '\0') {
467 if (savedname[0] == '\0') {
468 /* a safeguard in case the N-ame line is missing */
469 strlcpy(savedname, file, sizeof(savedname));
471 show(savedname, file, copycnt);
475 printf("%ld bytes\n", totsize);
481 inlist(char *uname, char *cfile)
487 if (users == 0 && requests == 0)
490 * Check to see if it's in the user list
492 for (u = user; u < &user[users]; u++)
493 if (!strcmp(*u, uname))
496 * Check the request list
498 jnum = calc_jobnum(cfile, &cfhost);
499 for (r = requ; r < &requ[requests]; r++)
500 if (*r == jnum && !strcmp(cfhost, from_host))
506 show(const char *nfile, const char *datafile, int copies)
508 if (strcmp(nfile, " ") == 0)
509 nfile = "(standard input)";
511 ldump(nfile, datafile, copies);
513 dump(nfile, datafile, copies);
517 * Fill the line with blanks to the specified column
522 while (col++ < tocol)
527 * Give the abbreviated dump of the file names
530 dump(const char *nfile, const char *datafile, int copies)
533 const char etctmpl[] = ", ...";
534 char etc[sizeof(etctmpl)];
540 * Print as many filenames as will fit
541 * (leaving room for the 'total size' field)
543 fill = first ? 0 : 2; /* fill space for ``, '' */
544 nlen = strlen(nfile);
545 rem = SIZCOL - 1 - col;
546 if (nlen + fill > rem) {
548 /* print the right-most part of the name */
549 printf("...%s ", &nfile[3+nlen-rem]);
551 } else if (rem > 0) {
552 /* fit as much of the etc-string as we can */
554 if (rem > strlen(etctmpl))
555 remetc = strlen(etctmpl);
557 strncat(etc, etctmpl, remetc);
561 /* room for the last segment of this filename? */
562 lastsep = strrchr(nfile, '/');
563 if ((lastsep != NULL) && (rem > strlen(lastsep))) {
564 /* print the right-most part of this name */
565 printf("%s", lastsep);
566 col += strlen(lastsep);
568 /* do not pack any more names in here */
581 if (*datafile && !stat(datafile, &lbuf))
582 totsize += copies * lbuf.st_size;
587 * Print the long info about the file
590 ldump(const char *nfile, const char *datafile, int copies)
596 printf("%-2d copies of %-19s", copies, nfile);
598 printf("%-32s", nfile);
599 if (*datafile && !stat(datafile, &lbuf))
600 printf(" %qd bytes", (long long) lbuf.st_size);
602 printf(" ??? bytes");
607 * Print the job's rank in the queue,
608 * update col for screen management
614 static const char *r[] = {
615 "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"
624 (void)snprintf(rline, sizeof(rline), "%dth", n);
626 (void)snprintf(rline, sizeof(rline), "%d%s", n, r[n%10]);
627 col += strlen(rline);
632 alarmhandler(int signo __unused)
634 /* the signal is ignored */
635 /* (the '__unused' is just to avoid a compile-time warning) */