add special redist label ``all:''
[unix-history] / usr / src / libexec / bugfiler / gethead.c
CommitLineData
07a70c1c
KB
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
24213599 8static char sccsid[] = "@(#)gethead.c 5.4 (Berkeley) 87/09/01";
07a70c1c
KB
9#endif not lint
10
11#include <bug.h>
12#include <sys/stat.h>
07a70c1c
KB
13#include <stdio.h>
14
5ea1bf0e 15static int chk1(), pbuf();
07a70c1c
KB
16
17#define ENT(X) sizeof(X) - 1, X
18HEADER 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:"), },
24213599
MK
24 { NO, YES, NULL, ENT("Reply-To:"), },
25 { NO, YES, NULL, ENT("Return-Path:"), },
5ea1bf0e 26 { NO, NO, pbuf, ENT("Subject:"), },
24213599
MK
27 { NO, YES, NULL, ENT("To:"), },
28 { NO, NO, NULL, ENT("Apparently-To:"), },
07a70c1c
KB
29 { ERR, }
30};
31
d8336336
KB
32FILE *dfp; /* distf file pointer */
33char dir[MAXNAMLEN], /* subject and folder */
07a70c1c
KB
34 folder[MAXNAMLEN];
35
36/*
37 * gethead --
38 * read mail and bug headers from bug report, construct redist headers
39 */
d8336336
KB
40gethead(redist)
41 int redist;
07a70c1c
KB
42{
43 register HEADER *hp; /* mail header pointer */
d8336336
KB
44 char *strcpy(), *malloc();
45
46 if (redist) {
47 int fd;
48 char *distf;
07a70c1c 49
d8336336
KB
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);
07a70c1c 58
d8336336
KB
59 while (fgets(bfr, sizeof(bfr), stdin)) {
60 for (hp = mailhead; hp->found != ERR; ++hp)
07a70c1c 61 if (!hp->found)
d8336336 62 if (!strncmp(hp->tag, bfr, hp->len)) {
07a70c1c
KB
63 if (hp->valid && !((*(hp->valid))(bfr)))
64 break;
65 if (!(hp->line = malloc((u_int)(strlen(bfr) + 1))))
d8336336
KB
66 error("malloc failed.", CHN);
67 (void)strcpy(hp->line, bfr);
07a70c1c
KB
68 hp->found = YES;
69 break;
70 }
d8336336
KB
71 if ((hp->found == ERR || hp->redist) && redist)
72 fputs(bfr, dfp);
07a70c1c
KB
73 }
74
75 if (!mailhead[INDX_TAG].found)
d8336336 76 error("no readable \"Index:\" header in bug report.", CHN);
07a70c1c
KB
77}
78
79/*
80 * chk1 --
81 * parse the "Index:" line into folder and directory
82 */
83static
84chk1(line)
d8336336 85 char *line;
07a70c1c
KB
86{
87 register char *C; /* tmp pointer */
88 struct stat sbuf; /* existence check */
89 char *index();
90
d8336336 91 if (sscanf(line, " Index: %s %s ", folder, dir) != 2)
07a70c1c 92 return(NO);
d8336336 93 if (C = index(folder, '/')) { /* deal with "bin/from.c" */
07a70c1c
KB
94 if (C == folder)
95 return(NO);
96 *C = EOS;
97 }
d8336336 98 if (stat(dir, &sbuf) || (sbuf.st_mode & S_IFMT) != S_IFDIR)
07a70c1c 99 return(NO);
5ea1bf0e
KB
100 (void)pbuf(line);
101 return(YES);
102}
103
104/*
105 * pbuf --
106 * kludge so that summary file looks pretty
107 */
108static
109pbuf(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;
07a70c1c
KB
126 return(YES);
127}