xref: /csrg-svn/libexec/bugfiler/gethead.c (revision 33416)
130162Sbostic /*
2*33416Sbostic  * Copyright (c) 1986, 1987 Regents of the University of California.
3*33416Sbostic  * All rights reserved.
4*33416Sbostic  *
5*33416Sbostic  * Redistribution and use in source and binary forms are permitted
6*33416Sbostic  * provided that this notice is preserved and that due credit is given
7*33416Sbostic  * to the University of California at Berkeley. The name of the University
8*33416Sbostic  * may not be used to endorse or promote products derived from this
9*33416Sbostic  * software without specific prior written permission. This software
10*33416Sbostic  * is provided ``as is'' without express or implied warranty.
1130162Sbostic  */
1230162Sbostic 
1330162Sbostic #ifndef lint
14*33416Sbostic static char sccsid[] = "@(#)gethead.c	5.5 (Berkeley) 02/01/88";
15*33416Sbostic #endif /* not lint */
1630162Sbostic 
1730162Sbostic #include <bug.h>
1830162Sbostic #include <sys/stat.h>
1930162Sbostic #include <stdio.h>
2030162Sbostic 
2131011Sbostic static int	chk1(), pbuf();
2230162Sbostic 
2330162Sbostic #define ENT(X)	sizeof(X) - 1, X
2430162Sbostic HEADER	mailhead[] = {				/* mail headers */
2530162Sbostic 	{ NO, YES,  NULL, ENT("Date:"), },
2630162Sbostic 	{ NO,  NO,  NULL, ENT("From "), },
2730162Sbostic 	{ NO, YES,  NULL, ENT("From:"), },
2830162Sbostic 	{ NO,  NO,  chk1, ENT("Index:"), },
2930162Sbostic 	{ NO, YES,  NULL, ENT("Message-Id:"), },
3032093Skarels 	{ NO, YES,  NULL, ENT("Reply-To:"), },
3132093Skarels 	{ NO, YES,  NULL, ENT("Return-Path:"), },
3231011Sbostic 	{ NO,  NO,  pbuf, ENT("Subject:"), },
3332093Skarels 	{ NO, YES,  NULL, ENT("To:"), },
3432093Skarels 	{ NO,  NO,  NULL, ENT("Apparently-To:"), },
3530162Sbostic 	{ ERR, }
3630162Sbostic };
3730162Sbostic 
3830890Sbostic FILE	*dfp;				/* distf file pointer */
3930890Sbostic char	dir[MAXNAMLEN],			/* subject and folder */
4030162Sbostic 	folder[MAXNAMLEN];
4130162Sbostic 
4230162Sbostic /*
4330162Sbostic  * gethead --
4430162Sbostic  *	read mail and bug headers from bug report, construct redist headers
4530162Sbostic  */
4630890Sbostic gethead(redist)
4730890Sbostic 	int	redist;
4830162Sbostic {
4930162Sbostic 	register HEADER	*hp;		/* mail header pointer */
5030890Sbostic 	char	*strcpy(), *malloc();
5130162Sbostic 
5230890Sbostic 	if (redist) {
5330890Sbostic 		int	fd;
5430890Sbostic 		char	*distf;
5530162Sbostic 
5630890Sbostic 		distf = "/tmp/BUG_XXXXXX";
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);
6130890Sbostic 	}
6230890Sbostic 	if (!freopen(tmpname, "r", stdin))
6330890Sbostic 		error("can't read temporary bug file %s.", tmpname);
6430890Sbostic 
6530890Sbostic 	while (fgets(bfr, sizeof(bfr), stdin)) {
6630890Sbostic 		for (hp = mailhead; hp->found != ERR; ++hp)
6730162Sbostic 			if (!hp->found)
6830890Sbostic 				if (!strncmp(hp->tag, bfr, hp->len)) {
6930162Sbostic 					if (hp->valid && !((*(hp->valid))(bfr)))
7030162Sbostic 						break;
7130162Sbostic 					if (!(hp->line = malloc((u_int)(strlen(bfr) + 1))))
7230890Sbostic 						error("malloc failed.", CHN);
7330890Sbostic 					(void)strcpy(hp->line, bfr);
7430162Sbostic 					hp->found = YES;
7530162Sbostic 					break;
7630162Sbostic 				}
7730890Sbostic 		if ((hp->found == ERR || hp->redist) && redist)
7830890Sbostic 			fputs(bfr, dfp);
7930162Sbostic 	}
8030162Sbostic 
8130162Sbostic 	if (!mailhead[INDX_TAG].found)
8230890Sbostic 		error("no readable \"Index:\" header in bug report.", CHN);
8330162Sbostic }
8430162Sbostic 
8530162Sbostic /*
8630162Sbostic  * chk1 --
8730162Sbostic  *	parse the "Index:" line into folder and directory
8830162Sbostic  */
8930162Sbostic static
9030162Sbostic chk1(line)
9130890Sbostic 	char	*line;
9230162Sbostic {
9330162Sbostic 	register char	*C;		/* tmp pointer */
9430162Sbostic 	struct stat	sbuf;		/* existence check */
9530162Sbostic 	char	*index();
9630162Sbostic 
9730890Sbostic 	if (sscanf(line, " Index: %s %s ", folder, dir) != 2)
9830162Sbostic 		return(NO);
9930890Sbostic 	if (C = index(folder, '/')) {	/* deal with "bin/from.c" */
10030162Sbostic 		if (C == folder)
10130162Sbostic 			return(NO);
10230162Sbostic 		*C = EOS;
10330162Sbostic 	}
10430890Sbostic 	if (stat(dir, &sbuf) || (sbuf.st_mode & S_IFMT) != S_IFDIR)
10530162Sbostic 		return(NO);
10631011Sbostic 	(void)pbuf(line);
10730162Sbostic 	return(YES);
10830162Sbostic }
10931011Sbostic 
11031011Sbostic /*
11131011Sbostic  * pbuf --
11231011Sbostic  *	kludge so that summary file looks pretty
11331011Sbostic  */
11431011Sbostic static
11531011Sbostic pbuf(line)
11631011Sbostic 	char	*line;
11731011Sbostic {
11831011Sbostic 	register char	*rp,			/* tmp pointers */
11931011Sbostic 			*wp;
12031011Sbostic 
12131011Sbostic 	for (rp = line; *rp == ' ' || *rp == '\t'; ++rp);
12231011Sbostic 	for (wp = line; *rp; ++wp) {
12331011Sbostic 		if ((*wp = *rp++) != ' ' && *wp != '\t')
12431011Sbostic 			continue;
12531011Sbostic 		*wp = ' ';
12631011Sbostic 		while (*rp == ' ' || *rp == '\t')
12731011Sbostic 			++rp;
12831011Sbostic 	}
12931011Sbostic 	if (wp[-1] == ' ')			/* wp can't == line */
13031011Sbostic 		--wp;
13131011Sbostic 	*wp = EOS;
13231011Sbostic 	return(YES);
13331011Sbostic }
134