1*12740Sralph /*	displayq.c	4.4	83/05/27	*/
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");
8712531Sralph 		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");
108*12740Sralph 	if (stat(LO, &statb) >= 0 && (statb.st_mode & 010)) {
10912113Sralph 		if (sendtorem)
11012113Sralph 			printf("\n%s: ", host);
111*12740Sralph 		printf("Warning: %s queue is turned off\n", printer);
112*12740Sralph 	}
113*12740Sralph 	if (nitems == 0) {
114*12740Sralph 		if (!sendtorem)
115*12740Sralph 			printf("no entries\n");
11612113Sralph 		return(0);
11712113Sralph 	}
11812113Sralph 	fp = fopen(LO, "r");
11912113Sralph 	if (fp == NULL || flock(fileno(fp), FSHLOCK|FNBLOCK) == 0) {
12012113Sralph 		if (fp != NULL)
12112113Sralph 			fclose(fp);
12212113Sralph 		garbage = nitems;
123*12740Sralph 		if (sendtorem)
124*12740Sralph 			printf("\n%s: ", host);
12512434Sralph 		if (stat(LO, &statb) >= 0 && (statb.st_mode & 0100))
12612434Sralph 			printf("Warning: %s is down\n", printer);
12712113Sralph 		else
12812434Sralph 			printf("Warning: no daemon present\n");
12912113Sralph 	} else {
13012113Sralph 		register char *cp = current;
13112113Sralph 
13212113Sralph 		/* skip daemon pid */
13312113Sralph 		while ((*cp = getc(fp)) != EOF && *cp != '\n');
13412113Sralph 		/* read current file name */
13512113Sralph 		while ((*cp = getc(fp)) != EOF && *cp != '\n')
13612113Sralph 			cp++;
13712113Sralph 		*cp = '\0';
13812113Sralph 		fclose(fp);
13912434Sralph 		/*
14012434Sralph 		 * Print the status file to show what the daemon is doing.
14112434Sralph 		 */
14212434Sralph 		if (sendtorem)
14312434Sralph 			printf("\n%s: ", host);
14412434Sralph 		if ((fd = open(ST, FRDONLY|FSHLOCK)) >= 0) {
14512434Sralph 			while ((i = read(fd, line, sizeof(line))) > 0)
14612434Sralph 				(void) fwrite(line, 1, i, stdout);
14712434Sralph 			(void) close(fd);
14812434Sralph 		} else
14912434Sralph 			putchar('\n');
15012113Sralph 	}
15112113Sralph 	/*
15212113Sralph 	 * Now, examine the control files and print out the jobs to
15312113Sralph 	 * be done for each user.
15412113Sralph 	 */
15512113Sralph 	if (!lflag)
15612113Sralph 		header();
15712113Sralph 	for (i = 0; i < nitems; i++) {
15812113Sralph 		q = queue[i];
15912113Sralph 		inform(q->q_name);
16012113Sralph 		free(q);
16112113Sralph 	}
16212113Sralph 	free(queue);
16312113Sralph 	return(nitems-garbage);
16412113Sralph }
16512113Sralph 
16612113Sralph /*
16712113Sralph  * Print the header for the short listing format
16812113Sralph  */
16912113Sralph header()
17012113Sralph {
17112113Sralph 	printf(head0);
17212113Sralph 	col = strlen(head0)+1;
17312113Sralph 	blankfill(SIZCOL);
17412113Sralph 	printf(head1);
17512113Sralph }
17612113Sralph 
17712113Sralph inform(cf)
17812113Sralph 	char *cf;
17912113Sralph {
18012113Sralph 	register int j, k;
18112113Sralph 	register char *cp;
18212113Sralph 	FILE *cfp;
18312113Sralph 
18412113Sralph 	/*
18512113Sralph 	 * There's a chance the control file has gone away
18612113Sralph 	 * in the meantime; if this is the case just keep going
18712113Sralph 	 */
18812113Sralph 	if ((cfp = fopen(cf, "r")) == NULL)
18912113Sralph 		return;
19012113Sralph 
19112113Sralph 	if (rank < 0)
19212113Sralph 		rank = 0;
19312113Sralph 	if (sendtorem || garbage || strcmp(cf, current))
19412113Sralph 		rank++;
19512113Sralph 	j = 0;
19612113Sralph 	while (getline(cfp)) {
19712113Sralph 		switch (line[0]) {
19812113Sralph 		case 'P': /* Was this file specified in the user's list? */
19912113Sralph 			if (!inlist(line+1, cf)) {
20012113Sralph 				fclose(cfp);
20112113Sralph 				return;
20212113Sralph 			}
20312113Sralph 			if (lflag) {
20412113Sralph 				printf("\n%s: ", line+1);
20512113Sralph 				col = strlen(line+1) + 2;
20612113Sralph 				prank(rank);
20712113Sralph 				blankfill(JOBCOL);
20812113Sralph 				printf(" [job %s]\n", cf+3);
20912113Sralph 			} else {
21012113Sralph 				col = 0;
21112113Sralph 				prank(rank);
21212113Sralph 				blankfill(OWNCOL);
21312113Sralph 				printf("%-10s %-3d  ", line+1, atoi(cf+3));
21412113Sralph 				col += 16;
21512113Sralph 				first = 1;
21612113Sralph 			}
21712113Sralph 			continue;
21812113Sralph 		default: /* some format specifer and file name? */
21912113Sralph 			if (line[0] < 'a' || line[0] > 'z')
22012113Sralph 				continue;
22112113Sralph 			if (j == 0 || strcmp(file, line+1) != 0)
22212113Sralph 				strcpy(file, line+1);
22312113Sralph 			j++;
22412113Sralph 			continue;
22512113Sralph 		case 'N':
22612113Sralph 			show(line+1, file, j);
22712113Sralph 			file[0] = '\0';
22812113Sralph 			j = 0;
22912113Sralph 		}
23012113Sralph 	}
23112113Sralph 	fclose(cfp);
23212113Sralph 	if (!lflag) {
23312113Sralph 		blankfill(SIZCOL);
23412113Sralph 		printf("%D bytes\n", totsize);
23512113Sralph 		totsize = 0;
23612113Sralph 	}
23712113Sralph }
23812113Sralph 
23912113Sralph inlist(name, file)
24012113Sralph 	char *name, *file;
24112113Sralph {
24212113Sralph 	register int *r, n;
24312113Sralph 	register char **u, *cp;
24412113Sralph 
24512113Sralph 	if (users == 0 && requests == 0)
24612113Sralph 		return(1);
24712113Sralph 	/*
24812113Sralph 	 * Check to see if it's in the user list
24912113Sralph 	 */
25012113Sralph 	for (u = user; u < &user[users]; u++)
25112113Sralph 		if (!strcmp(*u, name))
25212113Sralph 			return(1);
25312113Sralph 	/*
25412113Sralph 	 * Check the request list
25512113Sralph 	 */
25612113Sralph 	for (n = 0, cp = file+3; isdigit(*cp); )
25712113Sralph 		n = n * 10 + (*cp++ - '0');
25812113Sralph 	for (r = requ; r < &requ[requests]; r++)
25912113Sralph 		if (*r == n && !strcmp(cp, from))
26012113Sralph 			return(1);
26112113Sralph 	return(0);
26212113Sralph }
26312113Sralph 
26412113Sralph show(nfile, file, copies)
26512113Sralph 	register char *nfile, *file;
26612113Sralph {
26712113Sralph 	if (strcmp(nfile, " ") == 0)
26812113Sralph 		nfile = "(standard input)";
26912113Sralph 	if (lflag)
27012113Sralph 		ldump(nfile, file, copies);
27112113Sralph 	else
27212113Sralph 		dump(nfile, file, copies);
27312113Sralph }
27412113Sralph 
27512113Sralph /*
27612113Sralph  * Fill the line with blanks to the specified column
27712113Sralph  */
27812113Sralph blankfill(n)
27912113Sralph 	register int n;
28012113Sralph {
28112113Sralph 	while (col++ < n)
28212113Sralph 		putchar(' ');
28312113Sralph }
28412113Sralph 
28512113Sralph /*
28612113Sralph  * Give the abbreviated dump of the file names
28712113Sralph  */
28812113Sralph dump(nfile, file, copies)
28912113Sralph 	char *nfile, *file;
29012113Sralph {
29112113Sralph 	register short n, fill;
29212113Sralph 	struct stat lbuf;
29312113Sralph 
29412113Sralph 	/*
29512113Sralph 	 * Print as many files as will fit
29612113Sralph 	 *  (leaving room for the total size)
29712113Sralph 	 */
29812113Sralph 	 fill = first ? 0 : 2;	/* fill space for ``, '' */
29912113Sralph 	 if (((n = strlen(nfile)) + col + fill) >= SIZCOL-4) {
30012113Sralph 		if (col < SIZCOL) {
30112113Sralph 			printf(" ..."), col += 4;
30212113Sralph 			blankfill(SIZCOL);
30312113Sralph 		}
30412113Sralph 	} else {
30512113Sralph 		if (first)
30612113Sralph 			first = 0;
30712113Sralph 		else
30812113Sralph 			printf(", ");
30912113Sralph 		printf("%s", nfile);
31012113Sralph 		col += n+fill;
31112113Sralph 	}
31212113Sralph 	if (*file && !stat(file, &lbuf))
31312113Sralph 		totsize += copies * lbuf.st_size;
31412113Sralph }
31512113Sralph 
31612113Sralph /*
31712113Sralph  * Print the long info about the file
31812113Sralph  */
31912113Sralph ldump(nfile, file, copies)
32012113Sralph 	char *nfile, *file;
32112113Sralph {
32212113Sralph 	struct stat lbuf;
32312113Sralph 
32412113Sralph 	putchar('\t');
32512113Sralph 	if (copies > 1)
32612113Sralph 		printf("%-2d copies of %-19s", copies, nfile);
32712113Sralph 	else
32812113Sralph 		printf("%-32s", nfile);
32912113Sralph 	if (*file && !stat(file, &lbuf))
33012113Sralph 		printf(" %D bytes", lbuf.st_size);
33112113Sralph 	else
33212113Sralph 		printf(" ??? bytes");
33312113Sralph 	putchar('\n');
33412113Sralph }
33512113Sralph 
33612113Sralph /*
33712113Sralph  * Print the job's rank in the queue,
33812113Sralph  *   update col for screen management
33912113Sralph  */
34012113Sralph prank(n)
34112113Sralph {
34212113Sralph 	char line[100];
34312113Sralph 	static char *r[] = {
34412113Sralph 		"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"
34512113Sralph 	};
34612113Sralph 
34712113Sralph 	if (n == 0) {
34812113Sralph 		printf("active");
34912113Sralph 		col += 6;
35012113Sralph 		return;
35112113Sralph 	}
35212113Sralph 	if ((n/10) == 1)
35312113Sralph 		(void) sprintf(line, "%dth", n);
35412113Sralph 	else
35512113Sralph 		(void) sprintf(line, "%d%s", n, r[n%10]);
35612113Sralph 	col += strlen(line);
35712113Sralph 	printf("%s", line);
35812113Sralph }
359