xref: /csrg-svn/usr.sbin/lpr/lpq/lpq.c (revision 22431)
1*22431Sdist /*
2*22431Sdist  * Copyright (c) 1983 Regents of the University of California.
3*22431Sdist  * All rights reserved.  The Berkeley software License Agreement
4*22431Sdist  * specifies the terms and conditions for redistribution.
5*22431Sdist  */
6*22431Sdist 
713953Ssam #ifndef lint
8*22431Sdist char copyright[] =
9*22431Sdist "@(#) Copyright (c) 1983 Regents of the University of California.\n\
10*22431Sdist  All rights reserved.\n";
11*22431Sdist #endif not lint
1213953Ssam 
13*22431Sdist #ifndef lint
14*22431Sdist static char sccsid[] = "@(#)lpq.c	5.1 (Berkeley) 06/06/85";
15*22431Sdist #endif not lint
16*22431Sdist 
1712108Sralph /*
1812108Sralph  * Spool Queue examination program
1912108Sralph  *
2012108Sralph  * lpq [+[n]] [-Pprinter] [user...] [job...]
2112108Sralph  *
2212108Sralph  * + means continually scan queue until empty
2312108Sralph  * -P used to identify printer as per lpr/lprm
2412108Sralph  */
2512108Sralph 
2612108Sralph #include "lp.h"
2712108Sralph 
2812108Sralph char	*user[MAXUSERS];	/* users to process */
2912108Sralph int	users;			/* # of users in user array */
3012108Sralph int	requ[MAXREQUESTS];	/* job number of spool entries */
3112108Sralph int	requests;		/* # of spool requests */
3212108Sralph 
3312876Sralph static int	repeat;		/* + flag indicator */
3412876Sralph static int	slptime = 30;	/* pause between screen refereshes */
3512876Sralph static int	lflag;		/* long output option */
3612108Sralph 
3712108Sralph /*
3812108Sralph  * Termcap stuff for fancy display
3912108Sralph  */
4012108Sralph #ifdef TERMCAP
4112108Sralph struct sgttyb sbuf;
4212876Sralph static unsigned ospeed;
4312876Sralph static int	dumb;		/* whether to use capabilities */
4412876Sralph static char	PC;		/* pad character for output */
4512876Sralph static char	*UP;		/* up one line */
4612876Sralph static char	*BC;		/* backspace character, other than \b */
4712876Sralph static char	*CM;		/* cursor motion */
4812876Sralph static char	*CL;		/* clear display */
4912876Sralph static char	*TI;		/* terminal init for CM */
5012876Sralph static char	*TE;		/* terminal clear for CM */
5112876Sralph static char	*SO;		/* stand out start */
5212876Sralph static char	*SE;		/* stand out end */
5312108Sralph 
5412108Sralph char	*tgetstr();
5512108Sralph int	putch();		/* for tputs' */
5612108Sralph #endif
5712108Sralph 
5812108Sralph main(argc, argv)
5912108Sralph 	char *argv[];
6012108Sralph {
6112108Sralph 	register char *arg;
6212108Sralph 	register int n;
6312108Sralph 
6412431Sralph 	name = argv[0];
6512431Sralph 	gethostname(host, sizeof(host));
6612431Sralph 
6712108Sralph 	while (--argc) {
6812108Sralph 		if ((arg = *++argv)[0] == '+') {
6912108Sralph 			if (arg[1] != '\0')
7012108Sralph 				if ((n = atoi(&arg[1])) > 0)
7112108Sralph 					slptime = n;
7212108Sralph 			repeat++;
7312108Sralph 		} else if (arg[0] == '-')
7412108Sralph 			switch (arg[1]) {
7512108Sralph 			case 'P':		/* printer name */
7612730Sralph 				if (arg[2])
7712730Sralph 					printer = &arg[2];
7812730Sralph 				else if (argc > 1) {
7912730Sralph 					argc--;
8012730Sralph 					printer = *++argv;
8112730Sralph 				}
8212108Sralph 				break;
8312108Sralph 
8412108Sralph 			case 'l':		/* long output */
8512108Sralph 				lflag++;
8612108Sralph 				break;
8712108Sralph 
8812108Sralph 			default:
8912108Sralph 				usage();
9012108Sralph 		} else {
9112108Sralph 			if (isdigit(arg[0])) {
9212108Sralph 				if (requests >= MAXREQUESTS)
9312108Sralph 					fatal("too many requests");
9412108Sralph 				requ[requests++] = atoi(arg);
9512108Sralph 			} else {
9612108Sralph 				if (users >= MAXUSERS)
9712108Sralph 					fatal("too many users");
9812108Sralph 				user[users++] = arg;
9912108Sralph 			}
10012108Sralph 		}
10112108Sralph 	}
10212108Sralph 	if (printer == NULL && (printer = getenv("PRINTER")) == NULL)
10312108Sralph 		printer = DEFLP;
10412108Sralph #ifdef TERMCAP
10512108Sralph 	dumb = termcap();
10612108Sralph #endif
10712108Sralph 
10812108Sralph 	if (repeat) {
10912108Sralph #ifdef TERMCAP
11012108Sralph 		if (TI)
11112108Sralph 			tputs(TI, 0, putch);
11212108Sralph #endif
11312108Sralph 		do {
11412108Sralph #ifdef TERMCAP
11512108Sralph 			if (!dumb) {
11612108Sralph 				tputs(CL, 0, putch);
11712108Sralph 				tputs(tgoto(CM, 0, 0), 0, putch);
11812108Sralph 			}
11912108Sralph #endif
12012108Sralph 			if ((n = displayq(lflag)) > 0)
12112108Sralph 				sleep(slptime);
12212108Sralph 		} while (n > 0);
12312108Sralph #ifdef TERMCAP
12412108Sralph 		if (!dumb) {
12512108Sralph 			standout(stdout, "Hit return to continue");
12612108Sralph 			while (getchar() != '\n');
12712108Sralph 			if (TE)
12812108Sralph 				tputs(TE, 0, putch);
12912108Sralph 		}
13012108Sralph #endif
13112108Sralph 	} else
13212108Sralph 		displayq(lflag);
13316103Sralph 	exit(0);
13412108Sralph }
13512108Sralph 
13612876Sralph static
13712108Sralph usage()
13812108Sralph {
13912108Sralph 	printf("usage: lpq [-Pprinter] [-l] [+[n]] [user...] [job...]\n");
14012108Sralph 	exit(1);
14112108Sralph }
14212108Sralph 
14312108Sralph /*
14412108Sralph  * If we have the capability, print this in standout mode
14512108Sralph  */
14612876Sralph static
14712108Sralph standout(f, s, a1, a2)
14812108Sralph 	FILE *f;
14912108Sralph 	char *s;
15012108Sralph {
15112108Sralph #ifdef TERMCAP
15212108Sralph 	if (SO)
15312108Sralph 		tputs(SO, 0, putch);
15412108Sralph 	fprintf(f, s, a1, a2);
15512108Sralph 	if (SO && SE)
15612108Sralph 		tputs(SE, 0, putch);
15712108Sralph #else
15812108Sralph 	fprintf(f, s, a1, a2);
15912108Sralph #endif
16012108Sralph }
16112108Sralph 
16212108Sralph #ifdef TERMCAP
16312876Sralph static char *
16412108Sralph capstrings[] = {
16512108Sralph 	"bc", "cl", "cm", "so", "se", "ti", "te", "up",
16612108Sralph 	0
16712108Sralph };
16812108Sralph 
16912876Sralph static char **
17012108Sralph caps[] = {
17112108Sralph 	&BC, &CL, &CM, &SO, &SE, &TI, &TE, &UP,
17212108Sralph };
17312108Sralph 
17412108Sralph /*
17512108Sralph  * All we need from termcap is to clear screen and
17612108Sralph  *   position cursor at the top; if these aren't available
17712108Sralph  *   we say the terminal is dumb and let things scroll
17812108Sralph  */
17912876Sralph static
18012108Sralph termcap()
18112108Sralph {
18212108Sralph 	char *term, tbuf[BUFSIZ];
18312108Sralph 	static char buf[BUFSIZ/2];
18412108Sralph 	register short columns;
18512108Sralph 	char *bp = buf;
18612108Sralph 	register char **p, ***q, *cp;
18712108Sralph 
18812108Sralph 	ioctl(0, TIOCGETP, (char *)&sbuf);
18912108Sralph 	ospeed = sbuf.sg_ospeed;
19012108Sralph 	if ((term = getenv("TERM")) != NULL && tgetent(tbuf, term) > 0) {
19112108Sralph 		for (p = capstrings, q = caps; *p != NULL; p++, q++)
19212108Sralph 			**q = tgetstr(*p, &bp);
19312108Sralph 		if ((cp = tgetstr("pc", &bp)) != NULL)
19412108Sralph 			PC = *cp;
19512108Sralph 	}
19612108Sralph 	return(CL == NULL || CM == NULL);
19712108Sralph }
19812108Sralph 
19912108Sralph /*
20012108Sralph  * Putchar writearound for tputs
20112108Sralph  */
20212876Sralph static
20312108Sralph putch(c)
20412108Sralph 	char c;
20512108Sralph {
20612108Sralph 	putchar(c);
20712108Sralph }
20812108Sralph #endif
209