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