xref: /csrg-svn/usr.sbin/lpr/lpq/lpq.c (revision 13953)
1*13953Ssam #ifndef lint
2*13953Ssam static char sccsid[] = "@(#)lpq.c	4.5 (Berkeley) 07/17/83";
3*13953Ssam #endif
4*13953Ssam 
512108Sralph /*
612108Sralph  * Spool Queue examination program
712108Sralph  *
812108Sralph  * lpq [+[n]] [-Pprinter] [user...] [job...]
912108Sralph  *
1012108Sralph  * + means continually scan queue until empty
1112108Sralph  * -P used to identify printer as per lpr/lprm
1212108Sralph  */
1312108Sralph 
1412108Sralph #include "lp.h"
1512108Sralph 
1612108Sralph char	*user[MAXUSERS];	/* users to process */
1712108Sralph int	users;			/* # of users in user array */
1812108Sralph int	requ[MAXREQUESTS];	/* job number of spool entries */
1912108Sralph int	requests;		/* # of spool requests */
2012108Sralph 
2112876Sralph static int	repeat;		/* + flag indicator */
2212876Sralph static int	slptime = 30;	/* pause between screen refereshes */
2312876Sralph static int	lflag;		/* long output option */
2412108Sralph 
2512108Sralph /*
2612108Sralph  * Termcap stuff for fancy display
2712108Sralph  */
2812108Sralph #ifdef TERMCAP
2912108Sralph struct sgttyb sbuf;
3012876Sralph static unsigned ospeed;
3112876Sralph static int	dumb;		/* whether to use capabilities */
3212876Sralph static char	PC;		/* pad character for output */
3312876Sralph static char	*UP;		/* up one line */
3412876Sralph static char	*BC;		/* backspace character, other than \b */
3512876Sralph static char	*CM;		/* cursor motion */
3612876Sralph static char	*CL;		/* clear display */
3712876Sralph static char	*TI;		/* terminal init for CM */
3812876Sralph static char	*TE;		/* terminal clear for CM */
3912876Sralph static char	*SO;		/* stand out start */
4012876Sralph static char	*SE;		/* stand out end */
4112108Sralph 
4212108Sralph char	*tgetstr();
4312108Sralph int	putch();		/* for tputs' */
4412108Sralph #endif
4512108Sralph 
4612108Sralph main(argc, argv)
4712108Sralph 	char *argv[];
4812108Sralph {
4912108Sralph 	register char *arg;
5012108Sralph 	register int n;
5112108Sralph 
5212431Sralph 	name = argv[0];
5312431Sralph 	gethostname(host, sizeof(host));
5412431Sralph 
5512108Sralph 	while (--argc) {
5612108Sralph 		if ((arg = *++argv)[0] == '+') {
5712108Sralph 			if (arg[1] != '\0')
5812108Sralph 				if ((n = atoi(&arg[1])) > 0)
5912108Sralph 					slptime = n;
6012108Sralph 			repeat++;
6112108Sralph 		} else if (arg[0] == '-')
6212108Sralph 			switch (arg[1]) {
6312108Sralph 			case 'P':		/* printer name */
6412730Sralph 				if (arg[2])
6512730Sralph 					printer = &arg[2];
6612730Sralph 				else if (argc > 1) {
6712730Sralph 					argc--;
6812730Sralph 					printer = *++argv;
6912730Sralph 				}
7012108Sralph 				break;
7112108Sralph 
7212108Sralph 			case 'l':		/* long output */
7312108Sralph 				lflag++;
7412108Sralph 				break;
7512108Sralph 
7612108Sralph 			default:
7712108Sralph 				usage();
7812108Sralph 		} else {
7912108Sralph 			if (isdigit(arg[0])) {
8012108Sralph 				if (requests >= MAXREQUESTS)
8112108Sralph 					fatal("too many requests");
8212108Sralph 				requ[requests++] = atoi(arg);
8312108Sralph 			} else {
8412108Sralph 				if (users >= MAXUSERS)
8512108Sralph 					fatal("too many users");
8612108Sralph 				user[users++] = arg;
8712108Sralph 			}
8812108Sralph 		}
8912108Sralph 	}
9012108Sralph 	if (printer == NULL && (printer = getenv("PRINTER")) == NULL)
9112108Sralph 		printer = DEFLP;
9212108Sralph #ifdef TERMCAP
9312108Sralph 	dumb = termcap();
9412108Sralph #endif
9512108Sralph 
9612108Sralph 	if (repeat) {
9712108Sralph #ifdef TERMCAP
9812108Sralph 		if (TI)
9912108Sralph 			tputs(TI, 0, putch);
10012108Sralph #endif
10112108Sralph 		do {
10212108Sralph #ifdef TERMCAP
10312108Sralph 			if (!dumb) {
10412108Sralph 				tputs(CL, 0, putch);
10512108Sralph 				tputs(tgoto(CM, 0, 0), 0, putch);
10612108Sralph 			}
10712108Sralph #endif
10812108Sralph 			if ((n = displayq(lflag)) > 0)
10912108Sralph 				sleep(slptime);
11012108Sralph 		} while (n > 0);
11112108Sralph #ifdef TERMCAP
11212108Sralph 		if (!dumb) {
11312108Sralph 			standout(stdout, "Hit return to continue");
11412108Sralph 			while (getchar() != '\n');
11512108Sralph 			if (TE)
11612108Sralph 				tputs(TE, 0, putch);
11712108Sralph 		}
11812108Sralph #endif
11912108Sralph 	} else
12012108Sralph 		displayq(lflag);
12112108Sralph }
12212108Sralph 
12312876Sralph static
12412108Sralph usage()
12512108Sralph {
12612108Sralph 	printf("usage: lpq [-Pprinter] [-l] [+[n]] [user...] [job...]\n");
12712108Sralph 	exit(1);
12812108Sralph }
12912108Sralph 
13012108Sralph /*
13112108Sralph  * If we have the capability, print this in standout mode
13212108Sralph  */
13312876Sralph static
13412108Sralph standout(f, s, a1, a2)
13512108Sralph 	FILE *f;
13612108Sralph 	char *s;
13712108Sralph {
13812108Sralph #ifdef TERMCAP
13912108Sralph 	if (SO)
14012108Sralph 		tputs(SO, 0, putch);
14112108Sralph 	fprintf(f, s, a1, a2);
14212108Sralph 	if (SO && SE)
14312108Sralph 		tputs(SE, 0, putch);
14412108Sralph #else
14512108Sralph 	fprintf(f, s, a1, a2);
14612108Sralph #endif
14712108Sralph }
14812108Sralph 
14912108Sralph #ifdef TERMCAP
15012876Sralph static char *
15112108Sralph capstrings[] = {
15212108Sralph 	"bc", "cl", "cm", "so", "se", "ti", "te", "up",
15312108Sralph 	0
15412108Sralph };
15512108Sralph 
15612876Sralph static char **
15712108Sralph caps[] = {
15812108Sralph 	&BC, &CL, &CM, &SO, &SE, &TI, &TE, &UP,
15912108Sralph };
16012108Sralph 
16112108Sralph /*
16212108Sralph  * All we need from termcap is to clear screen and
16312108Sralph  *   position cursor at the top; if these aren't available
16412108Sralph  *   we say the terminal is dumb and let things scroll
16512108Sralph  */
16612876Sralph static
16712108Sralph termcap()
16812108Sralph {
16912108Sralph 	char *term, tbuf[BUFSIZ];
17012108Sralph 	static char buf[BUFSIZ/2];
17112108Sralph 	register short columns;
17212108Sralph 	char *bp = buf;
17312108Sralph 	register char **p, ***q, *cp;
17412108Sralph 
17512108Sralph 	ioctl(0, TIOCGETP, (char *)&sbuf);
17612108Sralph 	ospeed = sbuf.sg_ospeed;
17712108Sralph 	if ((term = getenv("TERM")) != NULL && tgetent(tbuf, term) > 0) {
17812108Sralph 		for (p = capstrings, q = caps; *p != NULL; p++, q++)
17912108Sralph 			**q = tgetstr(*p, &bp);
18012108Sralph 		if ((cp = tgetstr("pc", &bp)) != NULL)
18112108Sralph 			PC = *cp;
18212108Sralph 	}
18312108Sralph 	return(CL == NULL || CM == NULL);
18412108Sralph }
18512108Sralph 
18612108Sralph /*
18712108Sralph  * Putchar writearound for tputs
18812108Sralph  */
18912876Sralph static
19012108Sralph putch(c)
19112108Sralph 	char c;
19212108Sralph {
19312108Sralph 	putchar(c);
19412108Sralph }
19512108Sralph #endif
196