1*13952Ssam #ifndef lint
2*13952Ssam static char sccsid[] = "@(#)displayq.c	4.9 (Berkeley) 07/17/83";
3*13952Ssam #endif
4*13952Ssam 
512113Sralph /*
612113Sralph  * Routines to display the state of the queue.
712113Sralph  */
812113Sralph 
912113Sralph #include "lp.h"
1012113Sralph 
1113170Sralph #define JOBCOL	40		/* column for job # in -l format */
1213170Sralph #define OWNCOL	7		/* start of Owner column in normal */
1313170Sralph #define SIZCOL	62		/* start of Size column in normal */
1412113Sralph 
1512113Sralph /*
1612113Sralph  * Stuff for handling job specifications
1712113Sralph  */
1812113Sralph extern char	*user[];	/* users to process */
1912113Sralph extern int	users;		/* # of users in user array */
2012113Sralph extern int	requ[];		/* job number of spool entries */
2112113Sralph extern int	requests;	/* # of spool requests */
2212113Sralph 
2312878Sralph static int	lflag;		/* long output option */
2412878Sralph static char	current[40];	/* current file being printed */
2512878Sralph static int	garbage;	/* # of garbage cf files */
2612878Sralph static int	rank;		/* order to be printed (-1=none, 0=active) */
2712878Sralph static long	totsize;	/* total print job size in bytes */
2812878Sralph static int	first;		/* first file in ``files'' column? */
2912878Sralph static int	col;		/* column on screen */
3012878Sralph static int	sendtorem;	/* are we sending to a remote? */
3112878Sralph static char	file[132];	/* print file name */
3212113Sralph 
3312878Sralph static char	*head0 = "Rank   Owner      Job  Files";
3412878Sralph static char	*head1 = "Total Size\n";
3512113Sralph 
3612113Sralph /*
3712113Sralph  * Display the current state of the queue. Format = 1 if long format.
3812113Sralph  */
3912113Sralph displayq(format)
4012113Sralph 	int format;
4112113Sralph {
4212113Sralph 	register struct queue *q;
4312113Sralph 	register int i, nitems, fd;
4412113Sralph 	struct queue **queue;
4512113Sralph 	struct stat statb;
4612113Sralph 	FILE *fp;
4712113Sralph 
4812113Sralph 	lflag = format;
4912113Sralph 	totsize = 0;
5012113Sralph 	rank = -1;
5112113Sralph 
5212113Sralph 	if ((i = pgetent(line, printer)) < 0)
5312113Sralph 		fatal("cannot open printer description file");
5412113Sralph 	else if (i == 0)
5512113Sralph 		fatal("unknown printer");
5612113Sralph 	if ((LP = pgetstr("lp", &bp)) == NULL)
5712113Sralph 		LP = DEFDEVLP;
5812113Sralph 	if ((RP = pgetstr("rp", &bp)) == NULL)
5912434Sralph 		RP = DEFLP;
6012113Sralph 	if ((SD = pgetstr("sd", &bp)) == NULL)
6112113Sralph 		SD = DEFSPOOL;
6212113Sralph 	if ((LO = pgetstr("lo", &bp)) == NULL)
6312113Sralph 		LO = DEFLOCK;
6412113Sralph 	if ((ST = pgetstr("st", &bp)) == NULL)
6512113Sralph 		ST = DEFSTAT;
6612113Sralph 	RM = pgetstr("rm", &bp);
6712113Sralph 
6812113Sralph 	/*
6912113Sralph 	 * If there is no local printer, then print the queue on
7012113Sralph 	 * the remote machine and then what's in the queue here.
7112113Sralph 	 * Note that a file in transit may not show up in either queue.
7212113Sralph 	 */
7312113Sralph 	if (*LP == '\0') {
7412113Sralph 		register char *cp;
7512113Sralph 		char c;
7612113Sralph 
7712113Sralph 		sendtorem++;
7812113Sralph 		(void) sprintf(line, "%c%s", format + '\3', RP);
7912113Sralph 		cp = line;
8012113Sralph 		for (i = 0; i < requests; i++) {
8112113Sralph 			cp += strlen(cp);
8212113Sralph 			(void) sprintf(cp, " %d", requ[i]);
8312113Sralph 		}
8412113Sralph 		for (i = 0; i < users; i++) {
8512113Sralph 			cp += strlen(cp);
8612113Sralph 			*cp++ = ' ';
8712113Sralph 			strcpy(cp, user[i]);
8812113Sralph 		}
8912113Sralph 		strcat(line, "\n");
9012531Sralph 		fd = getport(RM);
9112113Sralph 		if (fd < 0) {
9212113Sralph 			if (from != host)
9312113Sralph 				printf("%s: ", host);
9412113Sralph 			printf("connection to %s is down\n", RM);
9512113Sralph 		} else {
9612113Sralph 			i = strlen(line);
9712113Sralph 			if (write(fd, line, i) != i)
9812113Sralph 				fatal("Lost connection");
9912113Sralph 			while ((i = read(fd, line, sizeof(line))) > 0)
10012113Sralph 				(void) fwrite(line, 1, i, stdout);
10112113Sralph 			(void) close(fd);
10212113Sralph 		}
10312113Sralph 	}
10412113Sralph 	/*
10512113Sralph 	 * Find all the control files in the spooling directory
10612113Sralph 	 */
10712113Sralph 	if (chdir(SD) < 0)
10812113Sralph 		fatal("cannot chdir to spooling directory");
10912113Sralph 	if ((nitems = getq(&queue)) < 0)
11012113Sralph 		fatal("cannot examine spooling area\n");
11112740Sralph 	if (stat(LO, &statb) >= 0 && (statb.st_mode & 010)) {
11212113Sralph 		if (sendtorem)
11312113Sralph 			printf("\n%s: ", host);
11412740Sralph 		printf("Warning: %s queue is turned off\n", printer);
11512740Sralph 	}
11612740Sralph 	if (nitems == 0) {
11712740Sralph 		if (!sendtorem)
11812740Sralph 			printf("no entries\n");
11912113Sralph 		return(0);
12012113Sralph 	}
12112113Sralph 	fp = fopen(LO, "r");
12213441Sralph 	if (fp == NULL)
12313441Sralph 		warn();
12413441Sralph 	else {
12513441Sralph 		register char *cp;
12612113Sralph 
12713441Sralph 		/* get daemon pid */
12813441Sralph 		cp = current;
12912113Sralph 		while ((*cp = getc(fp)) != EOF && *cp != '\n')
13012113Sralph 			cp++;
13112113Sralph 		*cp = '\0';
13213441Sralph 		i = atoi(current);
13313441Sralph 		if (kill(i, 0) < 0)
13413441Sralph 			warn();
13513441Sralph 		else {
13613441Sralph 			/* read current file name */
13713441Sralph 			cp = current;
13813441Sralph 			while ((*cp = getc(fp)) != EOF && *cp != '\n')
13913441Sralph 				cp++;
14013441Sralph 			*cp = '\0';
14113441Sralph 			/*
14213441Sralph 			 * Print the status file.
14313441Sralph 			 */
14413441Sralph 			if (sendtorem)
14513441Sralph 				printf("\n%s: ", host);
14613441Sralph 			fd = open(ST, O_RDONLY);
14713441Sralph 			if (fd >= 0) {
14813441Sralph 				(void) flock(fd, LOCK_SH);
14913441Sralph 				while ((i = read(fd, line, sizeof(line))) > 0)
15013441Sralph 					(void) fwrite(line, 1, i, stdout);
15113441Sralph 				(void) close(fd);	/* unlocks as well */
15213441Sralph 			} else
15313441Sralph 				putchar('\n');
15413441Sralph 		}
15513441Sralph 		(void) fclose(fp);
15612113Sralph 	}
15712113Sralph 	/*
15812113Sralph 	 * Now, examine the control files and print out the jobs to
15912113Sralph 	 * be done for each user.
16012113Sralph 	 */
16112113Sralph 	if (!lflag)
16212113Sralph 		header();
16312113Sralph 	for (i = 0; i < nitems; i++) {
16412113Sralph 		q = queue[i];
16512113Sralph 		inform(q->q_name);
16612113Sralph 		free(q);
16712113Sralph 	}
16812113Sralph 	free(queue);
16912113Sralph 	return(nitems-garbage);
17012113Sralph }
17112113Sralph 
17212113Sralph /*
17313441Sralph  * Print a warning message if there is no daemon present.
17413441Sralph  */
17513441Sralph warn()
17613441Sralph {
17713441Sralph 	struct stat statb;
17813441Sralph 
17913441Sralph 	if (sendtorem)
18013441Sralph 		printf("\n%s: ", host);
18113441Sralph 	if (stat(LO, &statb) >= 0 && (statb.st_mode & 0100))
18213441Sralph 		printf("Warning: %s is down\n", printer);
18313441Sralph 	else
18413441Sralph 		printf("Warning: no daemon present\n");
18513441Sralph 	current[0] = '\0';
18613441Sralph }
18713441Sralph 
18813441Sralph /*
18912113Sralph  * Print the header for the short listing format
19012113Sralph  */
19112878Sralph static
19212113Sralph header()
19312113Sralph {
19412113Sralph 	printf(head0);
19512113Sralph 	col = strlen(head0)+1;
19612113Sralph 	blankfill(SIZCOL);
19712113Sralph 	printf(head1);
19812113Sralph }
19912113Sralph 
20012878Sralph static
20112113Sralph inform(cf)
20212113Sralph 	char *cf;
20312113Sralph {
20412113Sralph 	register int j, k;
20512113Sralph 	register char *cp;
20612113Sralph 	FILE *cfp;
20712113Sralph 
20812113Sralph 	/*
20912113Sralph 	 * There's a chance the control file has gone away
21012113Sralph 	 * in the meantime; if this is the case just keep going
21112113Sralph 	 */
21212113Sralph 	if ((cfp = fopen(cf, "r")) == NULL)
21312113Sralph 		return;
21412113Sralph 
21512113Sralph 	if (rank < 0)
21612113Sralph 		rank = 0;
21712113Sralph 	if (sendtorem || garbage || strcmp(cf, current))
21812113Sralph 		rank++;
21912113Sralph 	j = 0;
22012113Sralph 	while (getline(cfp)) {
22112113Sralph 		switch (line[0]) {
22212113Sralph 		case 'P': /* Was this file specified in the user's list? */
22312113Sralph 			if (!inlist(line+1, cf)) {
22412113Sralph 				fclose(cfp);
22512113Sralph 				return;
22612113Sralph 			}
22712113Sralph 			if (lflag) {
22812113Sralph 				printf("\n%s: ", line+1);
22912113Sralph 				col = strlen(line+1) + 2;
23012113Sralph 				prank(rank);
23112113Sralph 				blankfill(JOBCOL);
23212113Sralph 				printf(" [job %s]\n", cf+3);
23312113Sralph 			} else {
23412113Sralph 				col = 0;
23512113Sralph 				prank(rank);
23612113Sralph 				blankfill(OWNCOL);
23712113Sralph 				printf("%-10s %-3d  ", line+1, atoi(cf+3));
23812113Sralph 				col += 16;
23912113Sralph 				first = 1;
24012113Sralph 			}
24112113Sralph 			continue;
24212113Sralph 		default: /* some format specifer and file name? */
24312113Sralph 			if (line[0] < 'a' || line[0] > 'z')
24412113Sralph 				continue;
24512113Sralph 			if (j == 0 || strcmp(file, line+1) != 0)
24612113Sralph 				strcpy(file, line+1);
24712113Sralph 			j++;
24812113Sralph 			continue;
24912113Sralph 		case 'N':
25012113Sralph 			show(line+1, file, j);
25112113Sralph 			file[0] = '\0';
25212113Sralph 			j = 0;
25312113Sralph 		}
25412113Sralph 	}
25512113Sralph 	fclose(cfp);
25612113Sralph 	if (!lflag) {
25712113Sralph 		blankfill(SIZCOL);
25812113Sralph 		printf("%D bytes\n", totsize);
25912113Sralph 		totsize = 0;
26012113Sralph 	}
26112113Sralph }
26212113Sralph 
26312878Sralph static
26412113Sralph inlist(name, file)
26512113Sralph 	char *name, *file;
26612113Sralph {
26712113Sralph 	register int *r, n;
26812113Sralph 	register char **u, *cp;
26912113Sralph 
27012113Sralph 	if (users == 0 && requests == 0)
27112113Sralph 		return(1);
27212113Sralph 	/*
27312113Sralph 	 * Check to see if it's in the user list
27412113Sralph 	 */
27512113Sralph 	for (u = user; u < &user[users]; u++)
27612113Sralph 		if (!strcmp(*u, name))
27712113Sralph 			return(1);
27812113Sralph 	/*
27912113Sralph 	 * Check the request list
28012113Sralph 	 */
28112113Sralph 	for (n = 0, cp = file+3; isdigit(*cp); )
28212113Sralph 		n = n * 10 + (*cp++ - '0');
28312113Sralph 	for (r = requ; r < &requ[requests]; r++)
28412113Sralph 		if (*r == n && !strcmp(cp, from))
28512113Sralph 			return(1);
28612113Sralph 	return(0);
28712113Sralph }
28812113Sralph 
28912878Sralph static
29012113Sralph show(nfile, file, copies)
29112113Sralph 	register char *nfile, *file;
29212113Sralph {
29312113Sralph 	if (strcmp(nfile, " ") == 0)
29412113Sralph 		nfile = "(standard input)";
29512113Sralph 	if (lflag)
29612113Sralph 		ldump(nfile, file, copies);
29712113Sralph 	else
29812113Sralph 		dump(nfile, file, copies);
29912113Sralph }
30012113Sralph 
30112113Sralph /*
30212113Sralph  * Fill the line with blanks to the specified column
30312113Sralph  */
30412878Sralph static
30512113Sralph blankfill(n)
30612113Sralph 	register int n;
30712113Sralph {
30812113Sralph 	while (col++ < n)
30912113Sralph 		putchar(' ');
31012113Sralph }
31112113Sralph 
31212113Sralph /*
31312113Sralph  * Give the abbreviated dump of the file names
31412113Sralph  */
31512878Sralph static
31612113Sralph dump(nfile, file, copies)
31712113Sralph 	char *nfile, *file;
31812113Sralph {
31912113Sralph 	register short n, fill;
32012113Sralph 	struct stat lbuf;
32112113Sralph 
32212113Sralph 	/*
32312113Sralph 	 * Print as many files as will fit
32412113Sralph 	 *  (leaving room for the total size)
32512113Sralph 	 */
32612113Sralph 	 fill = first ? 0 : 2;	/* fill space for ``, '' */
32712113Sralph 	 if (((n = strlen(nfile)) + col + fill) >= SIZCOL-4) {
32812113Sralph 		if (col < SIZCOL) {
32912113Sralph 			printf(" ..."), col += 4;
33012113Sralph 			blankfill(SIZCOL);
33112113Sralph 		}
33212113Sralph 	} else {
33312113Sralph 		if (first)
33412113Sralph 			first = 0;
33512113Sralph 		else
33612113Sralph 			printf(", ");
33712113Sralph 		printf("%s", nfile);
33812113Sralph 		col += n+fill;
33912113Sralph 	}
34012113Sralph 	if (*file && !stat(file, &lbuf))
34112113Sralph 		totsize += copies * lbuf.st_size;
34212113Sralph }
34312113Sralph 
34412113Sralph /*
34512113Sralph  * Print the long info about the file
34612113Sralph  */
34712878Sralph static
34812113Sralph ldump(nfile, file, copies)
34912113Sralph 	char *nfile, *file;
35012113Sralph {
35112113Sralph 	struct stat lbuf;
35212113Sralph 
35312113Sralph 	putchar('\t');
35412113Sralph 	if (copies > 1)
35512113Sralph 		printf("%-2d copies of %-19s", copies, nfile);
35612113Sralph 	else
35712113Sralph 		printf("%-32s", nfile);
35812113Sralph 	if (*file && !stat(file, &lbuf))
35912113Sralph 		printf(" %D bytes", lbuf.st_size);
36012113Sralph 	else
36112113Sralph 		printf(" ??? bytes");
36212113Sralph 	putchar('\n');
36312113Sralph }
36412113Sralph 
36512113Sralph /*
36612113Sralph  * Print the job's rank in the queue,
36712113Sralph  *   update col for screen management
36812113Sralph  */
36912878Sralph static
37012113Sralph prank(n)
37112113Sralph {
37212113Sralph 	char line[100];
37312113Sralph 	static char *r[] = {
37412113Sralph 		"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"
37512113Sralph 	};
37612113Sralph 
37712113Sralph 	if (n == 0) {
37812113Sralph 		printf("active");
37912113Sralph 		col += 6;
38012113Sralph 		return;
38112113Sralph 	}
38212113Sralph 	if ((n/10) == 1)
38312113Sralph 		(void) sprintf(line, "%dth", n);
38412113Sralph 	else
38512113Sralph 		(void) sprintf(line, "%d%s", n, r[n%10]);
38612113Sralph 	col += strlen(line);
38712113Sralph 	printf("%s", line);
38812113Sralph }
389