xref: /csrg-svn/libexec/bugfiler/gethead.c (revision 37887)
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
634910Sbostic  * provided that the above copyright notice and this paragraph are
734910Sbostic  * duplicated in all such forms and that any documentation,
834910Sbostic  * advertising materials, and other materials related to such
934910Sbostic  * distribution and use acknowledge that the software was developed
1034910Sbostic  * by the University of California, Berkeley.  The name of the
1134910Sbostic  * University may not be used to endorse or promote products derived
1234910Sbostic  * from this software without specific prior written permission.
1334910Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434910Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534910Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1630162Sbostic  */
1730162Sbostic 
1830162Sbostic #ifndef lint
19*37887Sbostic static char sccsid[] = "@(#)gethead.c	5.7 (Berkeley) 05/11/89";
2033416Sbostic #endif /* not lint */
2130162Sbostic 
2230162Sbostic #include <bug.h>
2330162Sbostic #include <sys/stat.h>
2430162Sbostic #include <stdio.h>
25*37887Sbostic #include "pathnames.h"
2630162Sbostic 
2731011Sbostic static int	chk1(), pbuf();
2830162Sbostic 
2930162Sbostic #define ENT(X)	sizeof(X) - 1, X
3030162Sbostic HEADER	mailhead[] = {				/* mail headers */
3130162Sbostic 	{ NO, YES,  NULL, ENT("Date:"), },
3230162Sbostic 	{ NO,  NO,  NULL, ENT("From "), },
3330162Sbostic 	{ NO, YES,  NULL, ENT("From:"), },
3430162Sbostic 	{ NO,  NO,  chk1, ENT("Index:"), },
3530162Sbostic 	{ NO, YES,  NULL, ENT("Message-Id:"), },
3632093Skarels 	{ NO, YES,  NULL, ENT("Reply-To:"), },
3732093Skarels 	{ NO, YES,  NULL, ENT("Return-Path:"), },
3831011Sbostic 	{ NO,  NO,  pbuf, ENT("Subject:"), },
3932093Skarels 	{ NO, YES,  NULL, ENT("To:"), },
4032093Skarels 	{ NO,  NO,  NULL, ENT("Apparently-To:"), },
4130162Sbostic 	{ ERR, }
4230162Sbostic };
4330162Sbostic 
4430890Sbostic FILE	*dfp;				/* distf file pointer */
4530890Sbostic char	dir[MAXNAMLEN],			/* subject and folder */
4630162Sbostic 	folder[MAXNAMLEN];
4730162Sbostic 
4830162Sbostic /*
4930162Sbostic  * gethead --
5030162Sbostic  *	read mail and bug headers from bug report, construct redist headers
5130162Sbostic  */
5230890Sbostic gethead(redist)
5330890Sbostic 	int	redist;
5430162Sbostic {
5530162Sbostic 	register HEADER	*hp;		/* mail header pointer */
5630890Sbostic 	char	*strcpy(), *malloc();
5730162Sbostic 
5830890Sbostic 	if (redist) {
5930890Sbostic 		int	fd;
6030890Sbostic 		char	*distf;
6130162Sbostic 
62*37887Sbostic 		distf = _PATH_TMP;
6330890Sbostic 		if (!(fd = mkstemp(distf)) || !(dfp = fdopen(fd, "w+")))
6430890Sbostic 			error("can't create redistribution file %s.", distf);
6530890Sbostic 		/* disappear after last reference is closed */
6630890Sbostic 		(void)unlink(distf);
6730890Sbostic 	}
6830890Sbostic 	if (!freopen(tmpname, "r", stdin))
6930890Sbostic 		error("can't read temporary bug file %s.", tmpname);
7030890Sbostic 
7130890Sbostic 	while (fgets(bfr, sizeof(bfr), stdin)) {
7230890Sbostic 		for (hp = mailhead; hp->found != ERR; ++hp)
7330162Sbostic 			if (!hp->found)
7430890Sbostic 				if (!strncmp(hp->tag, bfr, hp->len)) {
7530162Sbostic 					if (hp->valid && !((*(hp->valid))(bfr)))
7630162Sbostic 						break;
7730162Sbostic 					if (!(hp->line = malloc((u_int)(strlen(bfr) + 1))))
7830890Sbostic 						error("malloc failed.", CHN);
7930890Sbostic 					(void)strcpy(hp->line, bfr);
8030162Sbostic 					hp->found = YES;
8130162Sbostic 					break;
8230162Sbostic 				}
8330890Sbostic 		if ((hp->found == ERR || hp->redist) && redist)
8430890Sbostic 			fputs(bfr, dfp);
8530162Sbostic 	}
8630162Sbostic 
8730162Sbostic 	if (!mailhead[INDX_TAG].found)
8830890Sbostic 		error("no readable \"Index:\" header in bug report.", CHN);
8930162Sbostic }
9030162Sbostic 
9130162Sbostic /*
9230162Sbostic  * chk1 --
9330162Sbostic  *	parse the "Index:" line into folder and directory
9430162Sbostic  */
9530162Sbostic static
9630162Sbostic chk1(line)
9730890Sbostic 	char	*line;
9830162Sbostic {
9930162Sbostic 	register char	*C;		/* tmp pointer */
10030162Sbostic 	struct stat	sbuf;		/* existence check */
10130162Sbostic 	char	*index();
10230162Sbostic 
10330890Sbostic 	if (sscanf(line, " Index: %s %s ", folder, dir) != 2)
10430162Sbostic 		return(NO);
10530890Sbostic 	if (C = index(folder, '/')) {	/* deal with "bin/from.c" */
10630162Sbostic 		if (C == folder)
10730162Sbostic 			return(NO);
10830162Sbostic 		*C = EOS;
10930162Sbostic 	}
11030890Sbostic 	if (stat(dir, &sbuf) || (sbuf.st_mode & S_IFMT) != S_IFDIR)
11130162Sbostic 		return(NO);
11231011Sbostic 	(void)pbuf(line);
11330162Sbostic 	return(YES);
11430162Sbostic }
11531011Sbostic 
11631011Sbostic /*
11731011Sbostic  * pbuf --
11831011Sbostic  *	kludge so that summary file looks pretty
11931011Sbostic  */
12031011Sbostic static
12131011Sbostic pbuf(line)
12231011Sbostic 	char	*line;
12331011Sbostic {
12431011Sbostic 	register char	*rp,			/* tmp pointers */
12531011Sbostic 			*wp;
12631011Sbostic 
12731011Sbostic 	for (rp = line; *rp == ' ' || *rp == '\t'; ++rp);
12831011Sbostic 	for (wp = line; *rp; ++wp) {
12931011Sbostic 		if ((*wp = *rp++) != ' ' && *wp != '\t')
13031011Sbostic 			continue;
13131011Sbostic 		*wp = ' ';
13231011Sbostic 		while (*rp == ' ' || *rp == '\t')
13331011Sbostic 			++rp;
13431011Sbostic 	}
13531011Sbostic 	if (wp[-1] == ' ')			/* wp can't == line */
13631011Sbostic 		--wp;
13731011Sbostic 	*wp = EOS;
13831011Sbostic 	return(YES);
13931011Sbostic }
140