xref: /csrg-svn/libexec/bugfiler/gethead.c (revision 32093)
130162Sbostic /*
230162Sbostic  * Copyright (c) 1986 Regents of the University of California.
330162Sbostic  * All rights reserved.  The Berkeley software License Agreement
430162Sbostic  * specifies the terms and conditions for redistribution.
530162Sbostic  */
630162Sbostic 
730162Sbostic #ifndef lint
8*32093Skarels static char sccsid[] = "@(#)gethead.c	5.4 (Berkeley) 87/09/01";
930162Sbostic #endif not lint
1030162Sbostic 
1130162Sbostic #include <bug.h>
1230162Sbostic #include <sys/stat.h>
1330162Sbostic #include <stdio.h>
1430162Sbostic 
1531011Sbostic static int	chk1(), pbuf();
1630162Sbostic 
1730162Sbostic #define ENT(X)	sizeof(X) - 1, X
1830162Sbostic HEADER	mailhead[] = {				/* mail headers */
1930162Sbostic 	{ NO, YES,  NULL, ENT("Date:"), },
2030162Sbostic 	{ NO,  NO,  NULL, ENT("From "), },
2130162Sbostic 	{ NO, YES,  NULL, ENT("From:"), },
2230162Sbostic 	{ NO,  NO,  chk1, ENT("Index:"), },
2330162Sbostic 	{ NO, YES,  NULL, ENT("Message-Id:"), },
24*32093Skarels 	{ NO, YES,  NULL, ENT("Reply-To:"), },
25*32093Skarels 	{ NO, YES,  NULL, ENT("Return-Path:"), },
2631011Sbostic 	{ NO,  NO,  pbuf, ENT("Subject:"), },
27*32093Skarels 	{ NO, YES,  NULL, ENT("To:"), },
28*32093Skarels 	{ NO,  NO,  NULL, ENT("Apparently-To:"), },
2930162Sbostic 	{ ERR, }
3030162Sbostic };
3130162Sbostic 
3230890Sbostic FILE	*dfp;				/* distf file pointer */
3330890Sbostic char	dir[MAXNAMLEN],			/* subject and folder */
3430162Sbostic 	folder[MAXNAMLEN];
3530162Sbostic 
3630162Sbostic /*
3730162Sbostic  * gethead --
3830162Sbostic  *	read mail and bug headers from bug report, construct redist headers
3930162Sbostic  */
4030890Sbostic gethead(redist)
4130890Sbostic 	int	redist;
4230162Sbostic {
4330162Sbostic 	register HEADER	*hp;		/* mail header pointer */
4430890Sbostic 	char	*strcpy(), *malloc();
4530162Sbostic 
4630890Sbostic 	if (redist) {
4730890Sbostic 		int	fd;
4830890Sbostic 		char	*distf;
4930162Sbostic 
5030890Sbostic 		distf = "/tmp/BUG_XXXXXX";
5130890Sbostic 		if (!(fd = mkstemp(distf)) || !(dfp = fdopen(fd, "w+")))
5230890Sbostic 			error("can't create redistribution file %s.", distf);
5330890Sbostic 		/* disappear after last reference is closed */
5430890Sbostic 		(void)unlink(distf);
5530890Sbostic 	}
5630890Sbostic 	if (!freopen(tmpname, "r", stdin))
5730890Sbostic 		error("can't read temporary bug file %s.", tmpname);
5830890Sbostic 
5930890Sbostic 	while (fgets(bfr, sizeof(bfr), stdin)) {
6030890Sbostic 		for (hp = mailhead; hp->found != ERR; ++hp)
6130162Sbostic 			if (!hp->found)
6230890Sbostic 				if (!strncmp(hp->tag, bfr, hp->len)) {
6330162Sbostic 					if (hp->valid && !((*(hp->valid))(bfr)))
6430162Sbostic 						break;
6530162Sbostic 					if (!(hp->line = malloc((u_int)(strlen(bfr) + 1))))
6630890Sbostic 						error("malloc failed.", CHN);
6730890Sbostic 					(void)strcpy(hp->line, bfr);
6830162Sbostic 					hp->found = YES;
6930162Sbostic 					break;
7030162Sbostic 				}
7130890Sbostic 		if ((hp->found == ERR || hp->redist) && redist)
7230890Sbostic 			fputs(bfr, dfp);
7330162Sbostic 	}
7430162Sbostic 
7530162Sbostic 	if (!mailhead[INDX_TAG].found)
7630890Sbostic 		error("no readable \"Index:\" header in bug report.", CHN);
7730162Sbostic }
7830162Sbostic 
7930162Sbostic /*
8030162Sbostic  * chk1 --
8130162Sbostic  *	parse the "Index:" line into folder and directory
8230162Sbostic  */
8330162Sbostic static
8430162Sbostic chk1(line)
8530890Sbostic 	char	*line;
8630162Sbostic {
8730162Sbostic 	register char	*C;		/* tmp pointer */
8830162Sbostic 	struct stat	sbuf;		/* existence check */
8930162Sbostic 	char	*index();
9030162Sbostic 
9130890Sbostic 	if (sscanf(line, " Index: %s %s ", folder, dir) != 2)
9230162Sbostic 		return(NO);
9330890Sbostic 	if (C = index(folder, '/')) {	/* deal with "bin/from.c" */
9430162Sbostic 		if (C == folder)
9530162Sbostic 			return(NO);
9630162Sbostic 		*C = EOS;
9730162Sbostic 	}
9830890Sbostic 	if (stat(dir, &sbuf) || (sbuf.st_mode & S_IFMT) != S_IFDIR)
9930162Sbostic 		return(NO);
10031011Sbostic 	(void)pbuf(line);
10130162Sbostic 	return(YES);
10230162Sbostic }
10331011Sbostic 
10431011Sbostic /*
10531011Sbostic  * pbuf --
10631011Sbostic  *	kludge so that summary file looks pretty
10731011Sbostic  */
10831011Sbostic static
10931011Sbostic pbuf(line)
11031011Sbostic 	char	*line;
11131011Sbostic {
11231011Sbostic 	register char	*rp,			/* tmp pointers */
11331011Sbostic 			*wp;
11431011Sbostic 
11531011Sbostic 	for (rp = line; *rp == ' ' || *rp == '\t'; ++rp);
11631011Sbostic 	for (wp = line; *rp; ++wp) {
11731011Sbostic 		if ((*wp = *rp++) != ' ' && *wp != '\t')
11831011Sbostic 			continue;
11931011Sbostic 		*wp = ' ';
12031011Sbostic 		while (*rp == ' ' || *rp == '\t')
12131011Sbostic 			++rp;
12231011Sbostic 	}
12331011Sbostic 	if (wp[-1] == ' ')			/* wp can't == line */
12431011Sbostic 		--wp;
12531011Sbostic 	*wp = EOS;
12631011Sbostic 	return(YES);
12731011Sbostic }
128