xref: /csrg-svn/libexec/bugfiler/gethead.c (revision 46667)
1 /*
2  * Copyright (c) 1986, 1987 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)gethead.c	5.9 (Berkeley) 02/25/91";
10 #endif /* not lint */
11 
12 #include <sys/param.h>
13 #include <sys/stat.h>
14 #include <dirent.h>
15 #include <unistd.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include "pathnames.h"
20 #include "bug.h"
21 
22 static int	chk1(), pbuf();
23 
24 #define ENT(X)	sizeof(X) - 1, X
25 HEADER	mailhead[] = {				/* mail headers */
26 	{ NO, YES,  NULL, ENT("Date:"), },
27 	{ NO,  NO,  NULL, ENT("From "), },
28 	{ NO, YES,  NULL, ENT("From:"), },
29 	{ NO,  NO,  chk1, ENT("Index:"), },
30 	{ NO, YES,  NULL, ENT("Message-Id:"), },
31 	{ NO, YES,  NULL, ENT("Reply-To:"), },
32 	{ NO, YES,  NULL, ENT("Return-Path:"), },
33 	{ NO,  NO,  pbuf, ENT("Subject:"), },
34 	{ NO, YES,  NULL, ENT("To:"), },
35 	{ NO,  NO,  NULL, ENT("Apparently-To:"), },
36 	{ ERR, }
37 };
38 
39 FILE	*dfp;				/* distf file pointer */
40 char	dir[MAXNAMLEN],			/* subject and folder */
41 	folder[MAXNAMLEN];
42 
43 /*
44  * gethead --
45  *	read mail and bug headers from bug report, construct redist headers
46  */
47 gethead(redist)
48 	int	redist;
49 {
50 	register HEADER	*hp;		/* mail header pointer */
51 
52 	if (redist) {
53 		int	fd;
54 		char	*distf;
55 
56 		distf = _PATH_TMP;
57 		if (!(fd = mkstemp(distf)) || !(dfp = fdopen(fd, "w+")))
58 			error("can't create redistribution file %s.", distf);
59 		/* disappear after last reference is closed */
60 		(void)unlink(distf);
61 	}
62 	if (!freopen(tmpname, "r", stdin))
63 		error("can't read temporary bug file %s.", tmpname);
64 
65 	while (fgets(bfr, sizeof(bfr), stdin)) {
66 		for (hp = mailhead; hp->found != ERR; ++hp)
67 			if (!hp->found)
68 				if (!strncmp(hp->tag, bfr, hp->len)) {
69 					if (hp->valid && !((*(hp->valid))(bfr)))
70 						break;
71 					if (!(hp->line = malloc((u_int)(strlen(bfr) + 1))))
72 						error("malloc failed.", CHN);
73 					(void)strcpy(hp->line, bfr);
74 					hp->found = YES;
75 					break;
76 				}
77 		if ((hp->found == ERR || hp->redist) && redist)
78 			fputs(bfr, dfp);
79 	}
80 
81 	if (!mailhead[INDX_TAG].found)
82 		error("no readable \"Index:\" header in bug report.", CHN);
83 }
84 
85 /*
86  * chk1 --
87  *	parse the "Index:" line into folder and directory
88  */
89 static
90 chk1(line)
91 	char	*line;
92 {
93 	register char	*C;		/* tmp pointer */
94 	struct stat	sbuf;		/* existence check */
95 	char	*index();
96 
97 	if (sscanf(line, " Index: %s %s ", folder, dir) != 2)
98 		return(NO);
99 	if (C = index(folder, '/')) {	/* deal with "bin/from.c" */
100 		if (C == folder)
101 			return(NO);
102 		*C = EOS;
103 	}
104 	if (stat(dir, &sbuf) || (sbuf.st_mode & S_IFMT) != S_IFDIR)
105 		return(NO);
106 	(void)pbuf(line);
107 	return(YES);
108 }
109 
110 /*
111  * pbuf --
112  *	kludge so that summary file looks pretty
113  */
114 static
115 pbuf(line)
116 	char	*line;
117 {
118 	register char	*rp,			/* tmp pointers */
119 			*wp;
120 
121 	for (rp = line; *rp == ' ' || *rp == '\t'; ++rp);
122 	for (wp = line; *rp; ++wp) {
123 		if ((*wp = *rp++) != ' ' && *wp != '\t')
124 			continue;
125 		*wp = ' ';
126 		while (*rp == ' ' || *rp == '\t')
127 			++rp;
128 	}
129 	if (wp[-1] == ' ')			/* wp can't == line */
130 		--wp;
131 	*wp = EOS;
132 	return(YES);
133 }
134