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