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