xref: /csrg-svn/libexec/bugfiler/gethead.c (revision 47077)
130162Sbostic /*
233416Sbostic  * Copyright (c) 1986, 1987 Regents of the University of California.
333416Sbostic  * All rights reserved.
433416Sbostic  *
542663Sbostic  * %sccs.include.redist.c%
630162Sbostic  */
730162Sbostic 
830162Sbostic #ifndef lint
9*47077Sdonn static char sccsid[] = "@(#)gethead.c	5.10 (Berkeley) 03/07/91";
1033416Sbostic #endif /* not lint */
1130162Sbostic 
1246667Sbostic #include <sys/param.h>
1330162Sbostic #include <sys/stat.h>
1446667Sbostic #include <dirent.h>
1546667Sbostic #include <unistd.h>
1630162Sbostic #include <stdio.h>
1746667Sbostic #include <stdlib.h>
1846667Sbostic #include <string.h>
1937887Sbostic #include "pathnames.h"
2046667Sbostic #include "bug.h"
2130162Sbostic 
2231011Sbostic static int	chk1(), pbuf();
2330162Sbostic 
2430162Sbostic #define ENT(X)	sizeof(X) - 1, X
2530162Sbostic HEADER	mailhead[] = {				/* mail headers */
2630162Sbostic 	{ NO, YES,  NULL, ENT("Date:"), },
2730162Sbostic 	{ NO,  NO,  NULL, ENT("From "), },
2830162Sbostic 	{ NO, YES,  NULL, ENT("From:"), },
2930162Sbostic 	{ NO,  NO,  chk1, ENT("Index:"), },
3030162Sbostic 	{ NO, YES,  NULL, ENT("Message-Id:"), },
3132093Skarels 	{ NO, YES,  NULL, ENT("Reply-To:"), },
3232093Skarels 	{ NO, YES,  NULL, ENT("Return-Path:"), },
3331011Sbostic 	{ NO,  NO,  pbuf, ENT("Subject:"), },
3432093Skarels 	{ NO, YES,  NULL, ENT("To:"), },
3532093Skarels 	{ NO,  NO,  NULL, ENT("Apparently-To:"), },
3630162Sbostic 	{ ERR, }
3730162Sbostic };
3830162Sbostic 
3930890Sbostic FILE	*dfp;				/* distf file pointer */
4030890Sbostic char	dir[MAXNAMLEN],			/* subject and folder */
4130162Sbostic 	folder[MAXNAMLEN];
4230162Sbostic 
4330162Sbostic /*
4430162Sbostic  * gethead --
4530162Sbostic  *	read mail and bug headers from bug report, construct redist headers
4630162Sbostic  */
4730890Sbostic gethead(redist)
4830890Sbostic 	int	redist;
4930162Sbostic {
5030162Sbostic 	register HEADER	*hp;		/* mail header pointer */
5130162Sbostic 
5230890Sbostic 	if (redist) {
5330890Sbostic 		int	fd;
5430890Sbostic 		char	*distf;
5530162Sbostic 
56*47077Sdonn 		distf = strdup(_PATH_TMP);
5730890Sbostic 		if (!(fd = mkstemp(distf)) || !(dfp = fdopen(fd, "w+")))
5830890Sbostic 			error("can't create redistribution file %s.", distf);
5930890Sbostic 		/* disappear after last reference is closed */
6030890Sbostic 		(void)unlink(distf);
61*47077Sdonn 		free(distf);
6230890Sbostic 	}
6330890Sbostic 	if (!freopen(tmpname, "r", stdin))
6430890Sbostic 		error("can't read temporary bug file %s.", tmpname);
6530890Sbostic 
6630890Sbostic 	while (fgets(bfr, sizeof(bfr), stdin)) {
6730890Sbostic 		for (hp = mailhead; hp->found != ERR; ++hp)
6830162Sbostic 			if (!hp->found)
6930890Sbostic 				if (!strncmp(hp->tag, bfr, hp->len)) {
7030162Sbostic 					if (hp->valid && !((*(hp->valid))(bfr)))
7130162Sbostic 						break;
7230162Sbostic 					if (!(hp->line = malloc((u_int)(strlen(bfr) + 1))))
7330890Sbostic 						error("malloc failed.", CHN);
7430890Sbostic 					(void)strcpy(hp->line, bfr);
7530162Sbostic 					hp->found = YES;
7630162Sbostic 					break;
7730162Sbostic 				}
7830890Sbostic 		if ((hp->found == ERR || hp->redist) && redist)
7930890Sbostic 			fputs(bfr, dfp);
8030162Sbostic 	}
8130162Sbostic 
8230162Sbostic 	if (!mailhead[INDX_TAG].found)
8330890Sbostic 		error("no readable \"Index:\" header in bug report.", CHN);
8430162Sbostic }
8530162Sbostic 
8630162Sbostic /*
8730162Sbostic  * chk1 --
8830162Sbostic  *	parse the "Index:" line into folder and directory
8930162Sbostic  */
9030162Sbostic static
9130162Sbostic chk1(line)
9230890Sbostic 	char	*line;
9330162Sbostic {
9430162Sbostic 	register char	*C;		/* tmp pointer */
9530162Sbostic 	struct stat	sbuf;		/* existence check */
9630162Sbostic 	char	*index();
9730162Sbostic 
9830890Sbostic 	if (sscanf(line, " Index: %s %s ", folder, dir) != 2)
9930162Sbostic 		return(NO);
10030890Sbostic 	if (C = index(folder, '/')) {	/* deal with "bin/from.c" */
10130162Sbostic 		if (C == folder)
10230162Sbostic 			return(NO);
10330162Sbostic 		*C = EOS;
10430162Sbostic 	}
10530890Sbostic 	if (stat(dir, &sbuf) || (sbuf.st_mode & S_IFMT) != S_IFDIR)
10630162Sbostic 		return(NO);
10731011Sbostic 	(void)pbuf(line);
10830162Sbostic 	return(YES);
10930162Sbostic }
11031011Sbostic 
11131011Sbostic /*
11231011Sbostic  * pbuf --
11331011Sbostic  *	kludge so that summary file looks pretty
11431011Sbostic  */
11531011Sbostic static
11631011Sbostic pbuf(line)
11731011Sbostic 	char	*line;
11831011Sbostic {
11931011Sbostic 	register char	*rp,			/* tmp pointers */
12031011Sbostic 			*wp;
12131011Sbostic 
12231011Sbostic 	for (rp = line; *rp == ' ' || *rp == '\t'; ++rp);
12331011Sbostic 	for (wp = line; *rp; ++wp) {
12431011Sbostic 		if ((*wp = *rp++) != ' ' && *wp != '\t')
12531011Sbostic 			continue;
12631011Sbostic 		*wp = ' ';
12731011Sbostic 		while (*rp == ' ' || *rp == '\t')
12831011Sbostic 			++rp;
12931011Sbostic 	}
13031011Sbostic 	if (wp[-1] == ' ')			/* wp can't == line */
13131011Sbostic 		--wp;
13231011Sbostic 	*wp = EOS;
13331011Sbostic 	return(YES);
13431011Sbostic }
135