xref: /csrg-svn/libexec/bugfiler/gethead.c (revision 34910)
130162Sbostic /*
233416Sbostic  * Copyright (c) 1986, 1987 Regents of the University of California.
333416Sbostic  * All rights reserved.
433416Sbostic  *
533416Sbostic  * Redistribution and use in source and binary forms are permitted
6*34910Sbostic  * provided that the above copyright notice and this paragraph are
7*34910Sbostic  * duplicated in all such forms and that any documentation,
8*34910Sbostic  * advertising materials, and other materials related to such
9*34910Sbostic  * distribution and use acknowledge that the software was developed
10*34910Sbostic  * by the University of California, Berkeley.  The name of the
11*34910Sbostic  * University may not be used to endorse or promote products derived
12*34910Sbostic  * from this software without specific prior written permission.
13*34910Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*34910Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*34910Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1630162Sbostic  */
1730162Sbostic 
1830162Sbostic #ifndef lint
19*34910Sbostic static char sccsid[] = "@(#)gethead.c	5.6 (Berkeley) 06/29/88";
2033416Sbostic #endif /* not lint */
2130162Sbostic 
2230162Sbostic #include <bug.h>
2330162Sbostic #include <sys/stat.h>
2430162Sbostic #include <stdio.h>
2530162Sbostic 
2631011Sbostic static int	chk1(), pbuf();
2730162Sbostic 
2830162Sbostic #define ENT(X)	sizeof(X) - 1, X
2930162Sbostic HEADER	mailhead[] = {				/* mail headers */
3030162Sbostic 	{ NO, YES,  NULL, ENT("Date:"), },
3130162Sbostic 	{ NO,  NO,  NULL, ENT("From "), },
3230162Sbostic 	{ NO, YES,  NULL, ENT("From:"), },
3330162Sbostic 	{ NO,  NO,  chk1, ENT("Index:"), },
3430162Sbostic 	{ NO, YES,  NULL, ENT("Message-Id:"), },
3532093Skarels 	{ NO, YES,  NULL, ENT("Reply-To:"), },
3632093Skarels 	{ NO, YES,  NULL, ENT("Return-Path:"), },
3731011Sbostic 	{ NO,  NO,  pbuf, ENT("Subject:"), },
3832093Skarels 	{ NO, YES,  NULL, ENT("To:"), },
3932093Skarels 	{ NO,  NO,  NULL, ENT("Apparently-To:"), },
4030162Sbostic 	{ ERR, }
4130162Sbostic };
4230162Sbostic 
4330890Sbostic FILE	*dfp;				/* distf file pointer */
4430890Sbostic char	dir[MAXNAMLEN],			/* subject and folder */
4530162Sbostic 	folder[MAXNAMLEN];
4630162Sbostic 
4730162Sbostic /*
4830162Sbostic  * gethead --
4930162Sbostic  *	read mail and bug headers from bug report, construct redist headers
5030162Sbostic  */
5130890Sbostic gethead(redist)
5230890Sbostic 	int	redist;
5330162Sbostic {
5430162Sbostic 	register HEADER	*hp;		/* mail header pointer */
5530890Sbostic 	char	*strcpy(), *malloc();
5630162Sbostic 
5730890Sbostic 	if (redist) {
5830890Sbostic 		int	fd;
5930890Sbostic 		char	*distf;
6030162Sbostic 
6130890Sbostic 		distf = "/tmp/BUG_XXXXXX";
6230890Sbostic 		if (!(fd = mkstemp(distf)) || !(dfp = fdopen(fd, "w+")))
6330890Sbostic 			error("can't create redistribution file %s.", distf);
6430890Sbostic 		/* disappear after last reference is closed */
6530890Sbostic 		(void)unlink(distf);
6630890Sbostic 	}
6730890Sbostic 	if (!freopen(tmpname, "r", stdin))
6830890Sbostic 		error("can't read temporary bug file %s.", tmpname);
6930890Sbostic 
7030890Sbostic 	while (fgets(bfr, sizeof(bfr), stdin)) {
7130890Sbostic 		for (hp = mailhead; hp->found != ERR; ++hp)
7230162Sbostic 			if (!hp->found)
7330890Sbostic 				if (!strncmp(hp->tag, bfr, hp->len)) {
7430162Sbostic 					if (hp->valid && !((*(hp->valid))(bfr)))
7530162Sbostic 						break;
7630162Sbostic 					if (!(hp->line = malloc((u_int)(strlen(bfr) + 1))))
7730890Sbostic 						error("malloc failed.", CHN);
7830890Sbostic 					(void)strcpy(hp->line, bfr);
7930162Sbostic 					hp->found = YES;
8030162Sbostic 					break;
8130162Sbostic 				}
8230890Sbostic 		if ((hp->found == ERR || hp->redist) && redist)
8330890Sbostic 			fputs(bfr, dfp);
8430162Sbostic 	}
8530162Sbostic 
8630162Sbostic 	if (!mailhead[INDX_TAG].found)
8730890Sbostic 		error("no readable \"Index:\" header in bug report.", CHN);
8830162Sbostic }
8930162Sbostic 
9030162Sbostic /*
9130162Sbostic  * chk1 --
9230162Sbostic  *	parse the "Index:" line into folder and directory
9330162Sbostic  */
9430162Sbostic static
9530162Sbostic chk1(line)
9630890Sbostic 	char	*line;
9730162Sbostic {
9830162Sbostic 	register char	*C;		/* tmp pointer */
9930162Sbostic 	struct stat	sbuf;		/* existence check */
10030162Sbostic 	char	*index();
10130162Sbostic 
10230890Sbostic 	if (sscanf(line, " Index: %s %s ", folder, dir) != 2)
10330162Sbostic 		return(NO);
10430890Sbostic 	if (C = index(folder, '/')) {	/* deal with "bin/from.c" */
10530162Sbostic 		if (C == folder)
10630162Sbostic 			return(NO);
10730162Sbostic 		*C = EOS;
10830162Sbostic 	}
10930890Sbostic 	if (stat(dir, &sbuf) || (sbuf.st_mode & S_IFMT) != S_IFDIR)
11030162Sbostic 		return(NO);
11131011Sbostic 	(void)pbuf(line);
11230162Sbostic 	return(YES);
11330162Sbostic }
11431011Sbostic 
11531011Sbostic /*
11631011Sbostic  * pbuf --
11731011Sbostic  *	kludge so that summary file looks pretty
11831011Sbostic  */
11931011Sbostic static
12031011Sbostic pbuf(line)
12131011Sbostic 	char	*line;
12231011Sbostic {
12331011Sbostic 	register char	*rp,			/* tmp pointers */
12431011Sbostic 			*wp;
12531011Sbostic 
12631011Sbostic 	for (rp = line; *rp == ' ' || *rp == '\t'; ++rp);
12731011Sbostic 	for (wp = line; *rp; ++wp) {
12831011Sbostic 		if ((*wp = *rp++) != ' ' && *wp != '\t')
12931011Sbostic 			continue;
13031011Sbostic 		*wp = ' ';
13131011Sbostic 		while (*rp == ' ' || *rp == '\t')
13231011Sbostic 			++rp;
13331011Sbostic 	}
13431011Sbostic 	if (wp[-1] == ' ')			/* wp can't == line */
13531011Sbostic 		--wp;
13631011Sbostic 	*wp = EOS;
13731011Sbostic 	return(YES);
13831011Sbostic }
139