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