xref: /csrg-svn/libexec/bugfiler/gethead.c (revision 33416)
1 /*
2  * Copyright (c) 1986, 1987 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)gethead.c	5.5 (Berkeley) 02/01/88";
15 #endif /* not lint */
16 
17 #include <bug.h>
18 #include <sys/stat.h>
19 #include <stdio.h>
20 
21 static int	chk1(), pbuf();
22 
23 #define ENT(X)	sizeof(X) - 1, X
24 HEADER	mailhead[] = {				/* mail headers */
25 	{ NO, YES,  NULL, ENT("Date:"), },
26 	{ NO,  NO,  NULL, ENT("From "), },
27 	{ NO, YES,  NULL, ENT("From:"), },
28 	{ NO,  NO,  chk1, ENT("Index:"), },
29 	{ NO, YES,  NULL, ENT("Message-Id:"), },
30 	{ NO, YES,  NULL, ENT("Reply-To:"), },
31 	{ NO, YES,  NULL, ENT("Return-Path:"), },
32 	{ NO,  NO,  pbuf, ENT("Subject:"), },
33 	{ NO, YES,  NULL, ENT("To:"), },
34 	{ NO,  NO,  NULL, ENT("Apparently-To:"), },
35 	{ ERR, }
36 };
37 
38 FILE	*dfp;				/* distf file pointer */
39 char	dir[MAXNAMLEN],			/* subject and folder */
40 	folder[MAXNAMLEN];
41 
42 /*
43  * gethead --
44  *	read mail and bug headers from bug report, construct redist headers
45  */
46 gethead(redist)
47 	int	redist;
48 {
49 	register HEADER	*hp;		/* mail header pointer */
50 	char	*strcpy(), *malloc();
51 
52 	if (redist) {
53 		int	fd;
54 		char	*distf;
55 
56 		distf = "/tmp/BUG_XXXXXX";
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