xref: /original-bsd/libexec/bugfiler/gethead.c (revision e59fb703)
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.10 (Berkeley) 03/07/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 = strdup(_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 		free(distf);
62 	}
63 	if (!freopen(tmpname, "r", stdin))
64 		error("can't read temporary bug file %s.", tmpname);
65 
66 	while (fgets(bfr, sizeof(bfr), stdin)) {
67 		for (hp = mailhead; hp->found != ERR; ++hp)
68 			if (!hp->found)
69 				if (!strncmp(hp->tag, bfr, hp->len)) {
70 					if (hp->valid && !((*(hp->valid))(bfr)))
71 						break;
72 					if (!(hp->line = malloc((u_int)(strlen(bfr) + 1))))
73 						error("malloc failed.", CHN);
74 					(void)strcpy(hp->line, bfr);
75 					hp->found = YES;
76 					break;
77 				}
78 		if ((hp->found == ERR || hp->redist) && redist)
79 			fputs(bfr, dfp);
80 	}
81 
82 	if (!mailhead[INDX_TAG].found)
83 		error("no readable \"Index:\" header in bug report.", CHN);
84 }
85 
86 /*
87  * chk1 --
88  *	parse the "Index:" line into folder and directory
89  */
90 static
91 chk1(line)
92 	char	*line;
93 {
94 	register char	*C;		/* tmp pointer */
95 	struct stat	sbuf;		/* existence check */
96 	char	*index();
97 
98 	if (sscanf(line, " Index: %s %s ", folder, dir) != 2)
99 		return(NO);
100 	if (C = index(folder, '/')) {	/* deal with "bin/from.c" */
101 		if (C == folder)
102 			return(NO);
103 		*C = EOS;
104 	}
105 	if (stat(dir, &sbuf) || (sbuf.st_mode & S_IFMT) != S_IFDIR)
106 		return(NO);
107 	(void)pbuf(line);
108 	return(YES);
109 }
110 
111 /*
112  * pbuf --
113  *	kludge so that summary file looks pretty
114  */
115 static
116 pbuf(line)
117 	char	*line;
118 {
119 	register char	*rp,			/* tmp pointers */
120 			*wp;
121 
122 	for (rp = line; *rp == ' ' || *rp == '\t'; ++rp);
123 	for (wp = line; *rp; ++wp) {
124 		if ((*wp = *rp++) != ' ' && *wp != '\t')
125 			continue;
126 		*wp = ' ';
127 		while (*rp == ' ' || *rp == '\t')
128 			++rp;
129 	}
130 	if (wp[-1] == ' ')			/* wp can't == line */
131 		--wp;
132 	*wp = EOS;
133 	return(YES);
134 }
135