xref: /csrg-svn/libexec/bugfiler/gethead.c (revision 30890)
1 /*
2  * Copyright (c) 1986 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  */
6 
7 #ifndef lint
8 static char sccsid[] = "@(#)gethead.c	5.2 (Berkeley) 87/04/11";
9 #endif not lint
10 
11 #include <bug.h>
12 #include <sys/stat.h>
13 #include <stdio.h>
14 
15 static int	chk1();
16 
17 #define ENT(X)	sizeof(X) - 1, X
18 HEADER	mailhead[] = {				/* mail headers */
19 	{ NO, YES,  NULL, ENT("Date:"), },
20 	{ NO,  NO,  NULL, ENT("From "), },
21 	{ NO, YES,  NULL, ENT("From:"), },
22 	{ NO,  NO,  chk1, ENT("Index:"), },
23 	{ NO, YES,  NULL, ENT("Message-Id:"), },
24 	{ NO,  NO,  NULL, ENT("Reply-To:"), },
25 	{ NO,  NO,  NULL, ENT("Return-Path:"), },
26 	{ NO,  NO,  NULL, ENT("Subject:"), },
27 	{ NO,  NO,  NULL, ENT("To:"), },
28 	{ ERR, }
29 };
30 
31 FILE	*dfp;				/* distf file pointer */
32 char	dir[MAXNAMLEN],			/* subject and folder */
33 	folder[MAXNAMLEN];
34 
35 /*
36  * gethead --
37  *	read mail and bug headers from bug report, construct redist headers
38  */
39 gethead(redist)
40 	int	redist;
41 {
42 	register HEADER	*hp;		/* mail header pointer */
43 	char	*strcpy(), *malloc();
44 
45 	if (redist) {
46 		int	fd;
47 		char	*distf;
48 
49 		distf = "/tmp/BUG_XXXXXX";
50 		if (!(fd = mkstemp(distf)) || !(dfp = fdopen(fd, "w+")))
51 			error("can't create redistribution file %s.", distf);
52 		/* disappear after last reference is closed */
53 		(void)unlink(distf);
54 	}
55 	if (!freopen(tmpname, "r", stdin))
56 		error("can't read temporary bug file %s.", tmpname);
57 
58 	while (fgets(bfr, sizeof(bfr), stdin)) {
59 		for (hp = mailhead; hp->found != ERR; ++hp)
60 			if (!hp->found)
61 				if (!strncmp(hp->tag, bfr, hp->len)) {
62 					if (hp->valid && !((*(hp->valid))(bfr)))
63 						break;
64 					if (!(hp->line = malloc((u_int)(strlen(bfr) + 1))))
65 						error("malloc failed.", CHN);
66 					(void)strcpy(hp->line, bfr);
67 					hp->found = YES;
68 					break;
69 				}
70 		if ((hp->found == ERR || hp->redist) && redist)
71 			fputs(bfr, dfp);
72 	}
73 
74 	if (!mailhead[INDX_TAG].found)
75 		error("no readable \"Index:\" header in bug report.", CHN);
76 }
77 
78 /*
79  * chk1 --
80  *	parse the "Index:" line into folder and directory
81  */
82 static
83 chk1(line)
84 	char	*line;
85 {
86 	register char	*C;		/* tmp pointer */
87 	struct stat	sbuf;		/* existence check */
88 	char	*index();
89 
90 	if (sscanf(line, " Index: %s %s ", folder, dir) != 2)
91 		return(NO);
92 	if (C = index(folder, '/')) {	/* deal with "bin/from.c" */
93 		if (C == folder)
94 			return(NO);
95 		*C = EOS;
96 	}
97 	if (stat(dir, &sbuf) || (sbuf.st_mode & S_IFMT) != S_IFDIR)
98 		return(NO);
99 	return(YES);
100 }
101