1*12531Sralph /*	displayq.c	4.3	83/05/18	*/
212113Sralph /*
312113Sralph  * Routines to display the state of the queue.
412113Sralph  */
512113Sralph 
612113Sralph #include "lp.h"
712113Sralph 
812113Sralph #define	JOBCOL		40	/* column for job # in -l format */
912113Sralph #define OWNCOL		7	/* start of Owner column in normal */
1012113Sralph 
1112113Sralph /*
1212113Sralph  * Stuff for handling job specifications
1312113Sralph  */
1412113Sralph extern char	*user[];	/* users to process */
1512113Sralph extern int	users;		/* # of users in user array */
1612113Sralph extern int	requ[];		/* job number of spool entries */
1712113Sralph extern int	requests;	/* # of spool requests */
1812113Sralph 
1912113Sralph int	lflag;			/* long output option */
2012113Sralph char	current[40];		/* current file being printed */
2112113Sralph int	garbage;		/* # of garbage cf files */
2212113Sralph int	rank;			/* order to be printed (-1=none, 0=active) */
2312113Sralph long	totsize;		/* total print job size in bytes */
2412113Sralph int	first;			/* first file in ``files'' column? */
2512113Sralph int	col;			/* column on screen */
2612113Sralph int	SIZCOL = 62;		/* start of Size column in normal */
2712113Sralph int	sendtorem;		/* are we sending to a remote? */
2812113Sralph char	file[132];		/* print file name */
2912113Sralph 
3012113Sralph char	*head0 = "Rank   Owner      Job  Files";
3112113Sralph char	*head1 = "Total Size\n";
3212113Sralph 
3312113Sralph /*
3412113Sralph  * Display the current state of the queue. Format = 1 if long format.
3512113Sralph  */
3612113Sralph displayq(format)
3712113Sralph 	int format;
3812113Sralph {
3912113Sralph 	register struct queue *q;
4012113Sralph 	register int i, nitems, fd;
4112113Sralph 	struct queue **queue;
4212113Sralph 	struct stat statb;
4312113Sralph 	FILE *fp;
4412113Sralph 
4512113Sralph 	lflag = format;
4612113Sralph 	totsize = 0;
4712113Sralph 	rank = -1;
4812113Sralph 
4912113Sralph 	if ((i = pgetent(line, printer)) < 0)
5012113Sralph 		fatal("cannot open printer description file");
5112113Sralph 	else if (i == 0)
5212113Sralph 		fatal("unknown printer");
5312113Sralph 	if ((LP = pgetstr("lp", &bp)) == NULL)
5412113Sralph 		LP = DEFDEVLP;
5512113Sralph 	if ((RP = pgetstr("rp", &bp)) == NULL)
5612434Sralph 		RP = DEFLP;
5712113Sralph 	if ((SD = pgetstr("sd", &bp)) == NULL)
5812113Sralph 		SD = DEFSPOOL;
5912113Sralph 	if ((LO = pgetstr("lo", &bp)) == NULL)
6012113Sralph 		LO = DEFLOCK;
6112113Sralph 	if ((ST = pgetstr("st", &bp)) == NULL)
6212113Sralph 		ST = DEFSTAT;
6312113Sralph 	RM = pgetstr("rm", &bp);
6412113Sralph 
6512113Sralph 	/*
6612113Sralph 	 * If there is no local printer, then print the queue on
6712113Sralph 	 * the remote machine and then what's in the queue here.
6812113Sralph 	 * Note that a file in transit may not show up in either queue.
6912113Sralph 	 */
7012113Sralph 	if (*LP == '\0') {
7112113Sralph 		register char *cp;
7212113Sralph 		char c;
7312113Sralph 
7412113Sralph 		sendtorem++;
7512113Sralph 		(void) sprintf(line, "%c%s", format + '\3', RP);
7612113Sralph 		cp = line;
7712113Sralph 		for (i = 0; i < requests; i++) {
7812113Sralph 			cp += strlen(cp);
7912113Sralph 			(void) sprintf(cp, " %d", requ[i]);
8012113Sralph 		}
8112113Sralph 		for (i = 0; i < users; i++) {
8212113Sralph 			cp += strlen(cp);
8312113Sralph 			*cp++ = ' ';
8412113Sralph 			strcpy(cp, user[i]);
8512113Sralph 		}
8612113Sralph 		strcat(line, "\n");
87*12531Sralph 		fd = getport(RM);
8812113Sralph 		if (fd < 0) {
8912113Sralph 			if (from != host)
9012113Sralph 				printf("%s: ", host);
9112113Sralph 			printf("connection to %s is down\n", RM);
9212113Sralph 		} else {
9312113Sralph 			i = strlen(line);
9412113Sralph 			if (write(fd, line, i) != i)
9512113Sralph 				fatal("Lost connection");
9612113Sralph 			while ((i = read(fd, line, sizeof(line))) > 0)
9712113Sralph 				(void) fwrite(line, 1, i, stdout);
9812113Sralph 			(void) close(fd);
9912113Sralph 		}
10012113Sralph 	}
10112113Sralph 	/*
10212113Sralph 	 * Find all the control files in the spooling directory
10312113Sralph 	 */
10412113Sralph 	if (chdir(SD) < 0)
10512113Sralph 		fatal("cannot chdir to spooling directory");
10612113Sralph 	if ((nitems = getq(&queue)) < 0)
10712113Sralph 		fatal("cannot examine spooling area\n");
10812113Sralph 	if (nitems == 0) {
10912113Sralph 		if (sendtorem)
11012113Sralph 			printf("\n%s: ", host);
11112113Sralph 		printf("no entries\n");
11212113Sralph 		return(0);
11312113Sralph 	}
11412113Sralph 	fp = fopen(LO, "r");
11512113Sralph 	if (fp == NULL || flock(fileno(fp), FSHLOCK|FNBLOCK) == 0) {
11612113Sralph 		if (fp != NULL)
11712113Sralph 			fclose(fp);
11812113Sralph 		garbage = nitems;
11912434Sralph 		if (stat(LO, &statb) >= 0 && (statb.st_mode & 0100))
12012434Sralph 			printf("Warning: %s is down\n", printer);
12112113Sralph 		else
12212434Sralph 			printf("Warning: no daemon present\n");
12312113Sralph 	} else {
12412113Sralph 		register char *cp = current;
12512113Sralph 
12612113Sralph 		/* skip daemon pid */
12712113Sralph 		while ((*cp = getc(fp)) != EOF && *cp != '\n');
12812113Sralph 		/* read current file name */
12912113Sralph 		while ((*cp = getc(fp)) != EOF && *cp != '\n')
13012113Sralph 			cp++;
13112113Sralph 		*cp = '\0';
13212113Sralph 		fclose(fp);
13312434Sralph 		/*
13412434Sralph 		 * Print the status file to show what the daemon is doing.
13512434Sralph 		 */
13612434Sralph 		if (sendtorem)
13712434Sralph 			printf("\n%s: ", host);
13812434Sralph 		if ((fd = open(ST, FRDONLY|FSHLOCK)) >= 0) {
13912434Sralph 			while ((i = read(fd, line, sizeof(line))) > 0)
14012434Sralph 				(void) fwrite(line, 1, i, stdout);
14112434Sralph 			(void) close(fd);
14212434Sralph 		} else
14312434Sralph 			putchar('\n');
14412113Sralph 	}
14512113Sralph 	/*
14612113Sralph 	 * Now, examine the control files and print out the jobs to
14712113Sralph 	 * be done for each user.
14812113Sralph 	 */
14912113Sralph 	if (!lflag)
15012113Sralph 		header();
15112113Sralph 	for (i = 0; i < nitems; i++) {
15212113Sralph 		q = queue[i];
15312113Sralph 		inform(q->q_name);
15412113Sralph 		free(q);
15512113Sralph 	}
15612113Sralph 	free(queue);
15712113Sralph 	return(nitems-garbage);
15812113Sralph }
15912113Sralph 
16012113Sralph /*
16112113Sralph  * Print the header for the short listing format
16212113Sralph  */
16312113Sralph header()
16412113Sralph {
16512113Sralph 	printf(head0);
16612113Sralph 	col = strlen(head0)+1;
16712113Sralph 	blankfill(SIZCOL);
16812113Sralph 	printf(head1);
16912113Sralph }
17012113Sralph 
17112113Sralph inform(cf)
17212113Sralph 	char *cf;
17312113Sralph {
17412113Sralph 	register int j, k;
17512113Sralph 	register char *cp;
17612113Sralph 	FILE *cfp;
17712113Sralph 
17812113Sralph 	/*
17912113Sralph 	 * There's a chance the control file has gone away
18012113Sralph 	 * in the meantime; if this is the case just keep going
18112113Sralph 	 */
18212113Sralph 	if ((cfp = fopen(cf, "r")) == NULL)
18312113Sralph 		return;
18412113Sralph 
18512113Sralph 	if (rank < 0)
18612113Sralph 		rank = 0;
18712113Sralph 	if (sendtorem || garbage || strcmp(cf, current))
18812113Sralph 		rank++;
18912113Sralph 	j = 0;
19012113Sralph 	while (getline(cfp)) {
19112113Sralph 		switch (line[0]) {
19212113Sralph 		case 'P': /* Was this file specified in the user's list? */
19312113Sralph 			if (!inlist(line+1, cf)) {
19412113Sralph 				fclose(cfp);
19512113Sralph 				return;
19612113Sralph 			}
19712113Sralph 			if (lflag) {
19812113Sralph 				printf("\n%s: ", line+1);
19912113Sralph 				col = strlen(line+1) + 2;
20012113Sralph 				prank(rank);
20112113Sralph 				blankfill(JOBCOL);
20212113Sralph 				printf(" [job %s]\n", cf+3);
20312113Sralph 			} else {
20412113Sralph 				col = 0;
20512113Sralph 				prank(rank);
20612113Sralph 				blankfill(OWNCOL);
20712113Sralph 				printf("%-10s %-3d  ", line+1, atoi(cf+3));
20812113Sralph 				col += 16;
20912113Sralph 				first = 1;
21012113Sralph 			}
21112113Sralph 			continue;
21212113Sralph 		default: /* some format specifer and file name? */
21312113Sralph 			if (line[0] < 'a' || line[0] > 'z')
21412113Sralph 				continue;
21512113Sralph 			if (j == 0 || strcmp(file, line+1) != 0)
21612113Sralph 				strcpy(file, line+1);
21712113Sralph 			j++;
21812113Sralph 			continue;
21912113Sralph 		case 'N':
22012113Sralph 			show(line+1, file, j);
22112113Sralph 			file[0] = '\0';
22212113Sralph 			j = 0;
22312113Sralph 		}
22412113Sralph 	}
22512113Sralph 	fclose(cfp);
22612113Sralph 	if (!lflag) {
22712113Sralph 		blankfill(SIZCOL);
22812113Sralph 		printf("%D bytes\n", totsize);
22912113Sralph 		totsize = 0;
23012113Sralph 	}
23112113Sralph }
23212113Sralph 
23312113Sralph inlist(name, file)
23412113Sralph 	char *name, *file;
23512113Sralph {
23612113Sralph 	register int *r, n;
23712113Sralph 	register char **u, *cp;
23812113Sralph 
23912113Sralph 	if (users == 0 && requests == 0)
24012113Sralph 		return(1);
24112113Sralph 	/*
24212113Sralph 	 * Check to see if it's in the user list
24312113Sralph 	 */
24412113Sralph 	for (u = user; u < &user[users]; u++)
24512113Sralph 		if (!strcmp(*u, name))
24612113Sralph 			return(1);
24712113Sralph 	/*
24812113Sralph 	 * Check the request list
24912113Sralph 	 */
25012113Sralph 	for (n = 0, cp = file+3; isdigit(*cp); )
25112113Sralph 		n = n * 10 + (*cp++ - '0');
25212113Sralph 	for (r = requ; r < &requ[requests]; r++)
25312113Sralph 		if (*r == n && !strcmp(cp, from))
25412113Sralph 			return(1);
25512113Sralph 	return(0);
25612113Sralph }
25712113Sralph 
25812113Sralph show(nfile, file, copies)
25912113Sralph 	register char *nfile, *file;
26012113Sralph {
26112113Sralph 	if (strcmp(nfile, " ") == 0)
26212113Sralph 		nfile = "(standard input)";
26312113Sralph 	if (lflag)
26412113Sralph 		ldump(nfile, file, copies);
26512113Sralph 	else
26612113Sralph 		dump(nfile, file, copies);
26712113Sralph }
26812113Sralph 
26912113Sralph /*
27012113Sralph  * Fill the line with blanks to the specified column
27112113Sralph  */
27212113Sralph blankfill(n)
27312113Sralph 	register int n;
27412113Sralph {
27512113Sralph 	while (col++ < n)
27612113Sralph 		putchar(' ');
27712113Sralph }
27812113Sralph 
27912113Sralph /*
28012113Sralph  * Give the abbreviated dump of the file names
28112113Sralph  */
28212113Sralph dump(nfile, file, copies)
28312113Sralph 	char *nfile, *file;
28412113Sralph {
28512113Sralph 	register short n, fill;
28612113Sralph 	struct stat lbuf;
28712113Sralph 
28812113Sralph 	/*
28912113Sralph 	 * Print as many files as will fit
29012113Sralph 	 *  (leaving room for the total size)
29112113Sralph 	 */
29212113Sralph 	 fill = first ? 0 : 2;	/* fill space for ``, '' */
29312113Sralph 	 if (((n = strlen(nfile)) + col + fill) >= SIZCOL-4) {
29412113Sralph 		if (col < SIZCOL) {
29512113Sralph 			printf(" ..."), col += 4;
29612113Sralph 			blankfill(SIZCOL);
29712113Sralph 		}
29812113Sralph 	} else {
29912113Sralph 		if (first)
30012113Sralph 			first = 0;
30112113Sralph 		else
30212113Sralph 			printf(", ");
30312113Sralph 		printf("%s", nfile);
30412113Sralph 		col += n+fill;
30512113Sralph 	}
30612113Sralph 	if (*file && !stat(file, &lbuf))
30712113Sralph 		totsize += copies * lbuf.st_size;
30812113Sralph }
30912113Sralph 
31012113Sralph /*
31112113Sralph  * Print the long info about the file
31212113Sralph  */
31312113Sralph ldump(nfile, file, copies)
31412113Sralph 	char *nfile, *file;
31512113Sralph {
31612113Sralph 	struct stat lbuf;
31712113Sralph 
31812113Sralph 	putchar('\t');
31912113Sralph 	if (copies > 1)
32012113Sralph 		printf("%-2d copies of %-19s", copies, nfile);
32112113Sralph 	else
32212113Sralph 		printf("%-32s", nfile);
32312113Sralph 	if (*file && !stat(file, &lbuf))
32412113Sralph 		printf(" %D bytes", lbuf.st_size);
32512113Sralph 	else
32612113Sralph 		printf(" ??? bytes");
32712113Sralph 	putchar('\n');
32812113Sralph }
32912113Sralph 
33012113Sralph /*
33112113Sralph  * Print the job's rank in the queue,
33212113Sralph  *   update col for screen management
33312113Sralph  */
33412113Sralph prank(n)
33512113Sralph {
33612113Sralph 	char line[100];
33712113Sralph 	static char *r[] = {
33812113Sralph 		"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"
33912113Sralph 	};
34012113Sralph 
34112113Sralph 	if (n == 0) {
34212113Sralph 		printf("active");
34312113Sralph 		col += 6;
34412113Sralph 		return;
34512113Sralph 	}
34612113Sralph 	if ((n/10) == 1)
34712113Sralph 		(void) sprintf(line, "%dth", n);
34812113Sralph 	else
34912113Sralph 		(void) sprintf(line, "%d%s", n, r[n%10]);
35012113Sralph 	col += strlen(line);
35112113Sralph 	printf("%s", line);
35212113Sralph }
353