xref: /csrg-svn/libexec/bugfiler/gethead.c (revision 42663)
130162Sbostic /*
233416Sbostic  * Copyright (c) 1986, 1987 Regents of the University of California.
333416Sbostic  * All rights reserved.
433416Sbostic  *
5*42663Sbostic  * %sccs.include.redist.c%
630162Sbostic  */
730162Sbostic 
830162Sbostic #ifndef lint
9*42663Sbostic static char sccsid[] = "@(#)gethead.c	5.8 (Berkeley) 06/01/90";
1033416Sbostic #endif /* not lint */
1130162Sbostic 
1230162Sbostic #include <bug.h>
1330162Sbostic #include <sys/stat.h>
1430162Sbostic #include <stdio.h>
1537887Sbostic #include "pathnames.h"
1630162Sbostic 
1731011Sbostic static int	chk1(), pbuf();
1830162Sbostic 
1930162Sbostic #define ENT(X)	sizeof(X) - 1, X
2030162Sbostic HEADER	mailhead[] = {				/* mail headers */
2130162Sbostic 	{ NO, YES,  NULL, ENT("Date:"), },
2230162Sbostic 	{ NO,  NO,  NULL, ENT("From "), },
2330162Sbostic 	{ NO, YES,  NULL, ENT("From:"), },
2430162Sbostic 	{ NO,  NO,  chk1, ENT("Index:"), },
2530162Sbostic 	{ NO, YES,  NULL, ENT("Message-Id:"), },
2632093Skarels 	{ NO, YES,  NULL, ENT("Reply-To:"), },
2732093Skarels 	{ NO, YES,  NULL, ENT("Return-Path:"), },
2831011Sbostic 	{ NO,  NO,  pbuf, ENT("Subject:"), },
2932093Skarels 	{ NO, YES,  NULL, ENT("To:"), },
3032093Skarels 	{ NO,  NO,  NULL, ENT("Apparently-To:"), },
3130162Sbostic 	{ ERR, }
3230162Sbostic };
3330162Sbostic 
3430890Sbostic FILE	*dfp;				/* distf file pointer */
3530890Sbostic char	dir[MAXNAMLEN],			/* subject and folder */
3630162Sbostic 	folder[MAXNAMLEN];
3730162Sbostic 
3830162Sbostic /*
3930162Sbostic  * gethead --
4030162Sbostic  *	read mail and bug headers from bug report, construct redist headers
4130162Sbostic  */
4230890Sbostic gethead(redist)
4330890Sbostic 	int	redist;
4430162Sbostic {
4530162Sbostic 	register HEADER	*hp;		/* mail header pointer */
4630890Sbostic 	char	*strcpy(), *malloc();
4730162Sbostic 
4830890Sbostic 	if (redist) {
4930890Sbostic 		int	fd;
5030890Sbostic 		char	*distf;
5130162Sbostic 
5237887Sbostic 		distf = _PATH_TMP;
5330890Sbostic 		if (!(fd = mkstemp(distf)) || !(dfp = fdopen(fd, "w+")))
5430890Sbostic 			error("can't create redistribution file %s.", distf);
5530890Sbostic 		/* disappear after last reference is closed */
5630890Sbostic 		(void)unlink(distf);
5730890Sbostic 	}
5830890Sbostic 	if (!freopen(tmpname, "r", stdin))
5930890Sbostic 		error("can't read temporary bug file %s.", tmpname);
6030890Sbostic 
6130890Sbostic 	while (fgets(bfr, sizeof(bfr), stdin)) {
6230890Sbostic 		for (hp = mailhead; hp->found != ERR; ++hp)
6330162Sbostic 			if (!hp->found)
6430890Sbostic 				if (!strncmp(hp->tag, bfr, hp->len)) {
6530162Sbostic 					if (hp->valid && !((*(hp->valid))(bfr)))
6630162Sbostic 						break;
6730162Sbostic 					if (!(hp->line = malloc((u_int)(strlen(bfr) + 1))))
6830890Sbostic 						error("malloc failed.", CHN);
6930890Sbostic 					(void)strcpy(hp->line, bfr);
7030162Sbostic 					hp->found = YES;
7130162Sbostic 					break;
7230162Sbostic 				}
7330890Sbostic 		if ((hp->found == ERR || hp->redist) && redist)
7430890Sbostic 			fputs(bfr, dfp);
7530162Sbostic 	}
7630162Sbostic 
7730162Sbostic 	if (!mailhead[INDX_TAG].found)
7830890Sbostic 		error("no readable \"Index:\" header in bug report.", CHN);
7930162Sbostic }
8030162Sbostic 
8130162Sbostic /*
8230162Sbostic  * chk1 --
8330162Sbostic  *	parse the "Index:" line into folder and directory
8430162Sbostic  */
8530162Sbostic static
8630162Sbostic chk1(line)
8730890Sbostic 	char	*line;
8830162Sbostic {
8930162Sbostic 	register char	*C;		/* tmp pointer */
9030162Sbostic 	struct stat	sbuf;		/* existence check */
9130162Sbostic 	char	*index();
9230162Sbostic 
9330890Sbostic 	if (sscanf(line, " Index: %s %s ", folder, dir) != 2)
9430162Sbostic 		return(NO);
9530890Sbostic 	if (C = index(folder, '/')) {	/* deal with "bin/from.c" */
9630162Sbostic 		if (C == folder)
9730162Sbostic 			return(NO);
9830162Sbostic 		*C = EOS;
9930162Sbostic 	}
10030890Sbostic 	if (stat(dir, &sbuf) || (sbuf.st_mode & S_IFMT) != S_IFDIR)
10130162Sbostic 		return(NO);
10231011Sbostic 	(void)pbuf(line);
10330162Sbostic 	return(YES);
10430162Sbostic }
10531011Sbostic 
10631011Sbostic /*
10731011Sbostic  * pbuf --
10831011Sbostic  *	kludge so that summary file looks pretty
10931011Sbostic  */
11031011Sbostic static
11131011Sbostic pbuf(line)
11231011Sbostic 	char	*line;
11331011Sbostic {
11431011Sbostic 	register char	*rp,			/* tmp pointers */
11531011Sbostic 			*wp;
11631011Sbostic 
11731011Sbostic 	for (rp = line; *rp == ' ' || *rp == '\t'; ++rp);
11831011Sbostic 	for (wp = line; *rp; ++wp) {
11931011Sbostic 		if ((*wp = *rp++) != ' ' && *wp != '\t')
12031011Sbostic 			continue;
12131011Sbostic 		*wp = ' ';
12231011Sbostic 		while (*rp == ' ' || *rp == '\t')
12331011Sbostic 			++rp;
12431011Sbostic 	}
12531011Sbostic 	if (wp[-1] == ' ')			/* wp can't == line */
12631011Sbostic 		--wp;
12731011Sbostic 	*wp = EOS;
12831011Sbostic 	return(YES);
12931011Sbostic }
130