xref: /csrg-svn/usr.sbin/lpr/lpq/lpq.c (revision 12730)
1*12730Sralph /*	lpq.c	4.3	83/05/26	*/
212108Sralph /*
312108Sralph  * Spool Queue examination program
412108Sralph  *
512108Sralph  * lpq [+[n]] [-Pprinter] [user...] [job...]
612108Sralph  *
712108Sralph  * + means continually scan queue until empty
812108Sralph  * -P used to identify printer as per lpr/lprm
912108Sralph  */
1012108Sralph 
1112108Sralph #include "lp.h"
1212108Sralph 
1312108Sralph char	*user[MAXUSERS];	/* users to process */
1412108Sralph int	users;			/* # of users in user array */
1512108Sralph int	requ[MAXREQUESTS];	/* job number of spool entries */
1612108Sralph int	requests;		/* # of spool requests */
1712108Sralph 
1812108Sralph int	repeat;			/* + flag indicator */
1912108Sralph int	slptime = 30;		/* pause between screen refereshes */
2012108Sralph int	lflag;			/* long output option */
2112108Sralph 
2212108Sralph /*
2312108Sralph  * Termcap stuff for fancy display
2412108Sralph  */
2512108Sralph #ifdef TERMCAP
2612108Sralph struct sgttyb sbuf;
2712108Sralph unsigned ospeed;
2812108Sralph int	dumb;			/* whether to use capabilities */
2912108Sralph char	PC;			/* pad character for output */
3012108Sralph char	*UP;			/* up one line */
3112108Sralph char	*BC;			/* backspace character, other than \b */
3212108Sralph char	*CM;			/* cursor motion */
3312108Sralph char	*CL;			/* clear display */
3412108Sralph char	*TI;			/* terminal init for CM */
3512108Sralph char	*TE;			/* terminal clear for CM */
3612108Sralph char	*SO;			/* stand out start */
3712108Sralph char	*SE;			/* stand out end */
3812108Sralph 
3912108Sralph char	*tgetstr();
4012108Sralph int	putch();		/* for tputs' */
4112108Sralph #endif
4212108Sralph 
4312108Sralph main(argc, argv)
4412108Sralph 	char *argv[];
4512108Sralph {
4612108Sralph 	register char *arg;
4712108Sralph 	register int n;
4812108Sralph 
4912431Sralph 	name = argv[0];
5012431Sralph 	gethostname(host, sizeof(host));
5112431Sralph 
5212108Sralph 	while (--argc) {
5312108Sralph 		if ((arg = *++argv)[0] == '+') {
5412108Sralph 			if (arg[1] != '\0')
5512108Sralph 				if ((n = atoi(&arg[1])) > 0)
5612108Sralph 					slptime = n;
5712108Sralph 			repeat++;
5812108Sralph 		} else if (arg[0] == '-')
5912108Sralph 			switch (arg[1]) {
6012108Sralph 			case 'P':		/* printer name */
61*12730Sralph 				if (arg[2])
62*12730Sralph 					printer = &arg[2];
63*12730Sralph 				else if (argc > 1) {
64*12730Sralph 					argc--;
65*12730Sralph 					printer = *++argv;
66*12730Sralph 				}
6712108Sralph 				break;
6812108Sralph 
6912108Sralph 			case 'l':		/* long output */
7012108Sralph 				lflag++;
7112108Sralph 				break;
7212108Sralph 
7312108Sralph 			default:
7412108Sralph 				usage();
7512108Sralph 		} else {
7612108Sralph 			if (isdigit(arg[0])) {
7712108Sralph 				if (requests >= MAXREQUESTS)
7812108Sralph 					fatal("too many requests");
7912108Sralph 				requ[requests++] = atoi(arg);
8012108Sralph 			} else {
8112108Sralph 				if (users >= MAXUSERS)
8212108Sralph 					fatal("too many users");
8312108Sralph 				user[users++] = arg;
8412108Sralph 			}
8512108Sralph 		}
8612108Sralph 	}
8712108Sralph 	if (printer == NULL && (printer = getenv("PRINTER")) == NULL)
8812108Sralph 		printer = DEFLP;
8912108Sralph #ifdef TERMCAP
9012108Sralph 	dumb = termcap();
9112108Sralph #endif
9212108Sralph 
9312108Sralph 	if (repeat) {
9412108Sralph #ifdef TERMCAP
9512108Sralph 		if (TI)
9612108Sralph 			tputs(TI, 0, putch);
9712108Sralph #endif
9812108Sralph 		do {
9912108Sralph #ifdef TERMCAP
10012108Sralph 			if (!dumb) {
10112108Sralph 				tputs(CL, 0, putch);
10212108Sralph 				tputs(tgoto(CM, 0, 0), 0, putch);
10312108Sralph 			}
10412108Sralph #endif
10512108Sralph 			if ((n = displayq(lflag)) > 0)
10612108Sralph 				sleep(slptime);
10712108Sralph 		} while (n > 0);
10812108Sralph #ifdef TERMCAP
10912108Sralph 		if (!dumb) {
11012108Sralph 			standout(stdout, "Hit return to continue");
11112108Sralph 			while (getchar() != '\n');
11212108Sralph 			if (TE)
11312108Sralph 				tputs(TE, 0, putch);
11412108Sralph 		}
11512108Sralph #endif
11612108Sralph 	} else
11712108Sralph 		displayq(lflag);
11812108Sralph }
11912108Sralph 
12012108Sralph usage()
12112108Sralph {
12212108Sralph 	printf("usage: lpq [-Pprinter] [-l] [+[n]] [user...] [job...]\n");
12312108Sralph 	exit(1);
12412108Sralph }
12512108Sralph 
12612108Sralph /*
12712108Sralph  * If we have the capability, print this in standout mode
12812108Sralph  */
12912108Sralph standout(f, s, a1, a2)
13012108Sralph 	FILE *f;
13112108Sralph 	char *s;
13212108Sralph {
13312108Sralph #ifdef TERMCAP
13412108Sralph 	if (SO)
13512108Sralph 		tputs(SO, 0, putch);
13612108Sralph 	fprintf(f, s, a1, a2);
13712108Sralph 	if (SO && SE)
13812108Sralph 		tputs(SE, 0, putch);
13912108Sralph #else
14012108Sralph 	fprintf(f, s, a1, a2);
14112108Sralph #endif
14212108Sralph }
14312108Sralph 
14412108Sralph #ifdef TERMCAP
14512108Sralph char *
14612108Sralph capstrings[] = {
14712108Sralph 	"bc", "cl", "cm", "so", "se", "ti", "te", "up",
14812108Sralph 	0
14912108Sralph };
15012108Sralph 
15112108Sralph char **
15212108Sralph caps[] = {
15312108Sralph 	&BC, &CL, &CM, &SO, &SE, &TI, &TE, &UP,
15412108Sralph };
15512108Sralph 
15612108Sralph /*
15712108Sralph  * All we need from termcap is to clear screen and
15812108Sralph  *   position cursor at the top; if these aren't available
15912108Sralph  *   we say the terminal is dumb and let things scroll
16012108Sralph  */
16112108Sralph termcap()
16212108Sralph {
16312108Sralph 	char *term, tbuf[BUFSIZ];
16412108Sralph 	static char buf[BUFSIZ/2];
16512108Sralph 	register short columns;
16612108Sralph 	char *bp = buf;
16712108Sralph 	register char **p, ***q, *cp;
16812108Sralph 	extern int SIZCOL;
16912108Sralph 
17012108Sralph 	ioctl(0, TIOCGETP, (char *)&sbuf);
17112108Sralph 	ospeed = sbuf.sg_ospeed;
17212108Sralph 	if ((term = getenv("TERM")) != NULL && tgetent(tbuf, term) > 0) {
17312108Sralph 		for (p = capstrings, q = caps; *p != NULL; p++, q++)
17412108Sralph 			**q = tgetstr(*p, &bp);
17512108Sralph 		if ((cp = tgetstr("pc", &bp)) != NULL)
17612108Sralph 			PC = *cp;
17712108Sralph 		if ((columns = tgetnum("co")) >= 0)
17812108Sralph 			SIZCOL = columns-18;
17912108Sralph 	}
18012108Sralph 	return(CL == NULL || CM == NULL);
18112108Sralph }
18212108Sralph 
18312108Sralph /*
18412108Sralph  * Putchar writearound for tputs
18512108Sralph  */
18612108Sralph putch(c)
18712108Sralph 	char c;
18812108Sralph {
18912108Sralph 	putchar(c);
19012108Sralph }
19112108Sralph #endif
192