add tetris
[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 *
1343342a 5 * %sccs.include.redist.c%
07a70c1c
KB
6 */
7
8#ifndef lint
6e1d805a 9static char sccsid[] = "@(#)gethead.c 5.10 (Berkeley) %G%";
aec2eff2 10#endif /* not lint */
07a70c1c 11
d0ef4a0d 12#include <sys/param.h>
07a70c1c 13#include <sys/stat.h>
d0ef4a0d
KB
14#include <dirent.h>
15#include <unistd.h>
07a70c1c 16#include <stdio.h>
d0ef4a0d
KB
17#include <stdlib.h>
18#include <string.h>
435e8dff 19#include "pathnames.h"
d0ef4a0d 20#include "bug.h"
07a70c1c 21
5ea1bf0e 22static int chk1(), pbuf();
07a70c1c
KB
23
24#define ENT(X) sizeof(X) - 1, X
25HEADER mailhead[] = { /* mail headers */
26 { NO, YES, NULL, ENT("Date:"), },
27 { NO, NO, NULL, ENT("From "), },
28 { NO, YES, NULL, ENT("From:"), },
29 { NO, NO, chk1, ENT("Index:"), },
30 { NO, YES, NULL, ENT("Message-Id:"), },
24213599
MK
31 { NO, YES, NULL, ENT("Reply-To:"), },
32 { NO, YES, NULL, ENT("Return-Path:"), },
5ea1bf0e 33 { NO, NO, pbuf, ENT("Subject:"), },
24213599
MK
34 { NO, YES, NULL, ENT("To:"), },
35 { NO, NO, NULL, ENT("Apparently-To:"), },
07a70c1c
KB
36 { ERR, }
37};
38
d8336336
KB
39FILE *dfp; /* distf file pointer */
40char dir[MAXNAMLEN], /* subject and folder */
07a70c1c
KB
41 folder[MAXNAMLEN];
42
43/*
44 * gethead --
45 * read mail and bug headers from bug report, construct redist headers
46 */
d8336336
KB
47gethead(redist)
48 int redist;
07a70c1c
KB
49{
50 register HEADER *hp; /* mail header pointer */
d8336336
KB
51
52 if (redist) {
53 int fd;
54 char *distf;
07a70c1c 55
6e1d805a 56 distf = strdup(_PATH_TMP);
d8336336
KB
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);
6e1d805a 61 free(distf);
d8336336
KB
62 }
63 if (!freopen(tmpname, "r", stdin))
64 error("can't read temporary bug file %s.", tmpname);
07a70c1c 65
d8336336
KB
66 while (fgets(bfr, sizeof(bfr), stdin)) {
67 for (hp = mailhead; hp->found != ERR; ++hp)
07a70c1c 68 if (!hp->found)
d8336336 69 if (!strncmp(hp->tag, bfr, hp->len)) {
07a70c1c
KB
70 if (hp->valid && !((*(hp->valid))(bfr)))
71 break;
72 if (!(hp->line = malloc((u_int)(strlen(bfr) + 1))))
d8336336
KB
73 error("malloc failed.", CHN);
74 (void)strcpy(hp->line, bfr);
07a70c1c
KB
75 hp->found = YES;
76 break;
77 }
d8336336
KB
78 if ((hp->found == ERR || hp->redist) && redist)
79 fputs(bfr, dfp);
07a70c1c
KB
80 }
81
82 if (!mailhead[INDX_TAG].found)
d8336336 83 error("no readable \"Index:\" header in bug report.", CHN);
07a70c1c
KB
84}
85
86/*
87 * chk1 --
88 * parse the "Index:" line into folder and directory
89 */
90static
91chk1(line)
d8336336 92 char *line;
07a70c1c
KB
93{
94 register char *C; /* tmp pointer */
95 struct stat sbuf; /* existence check */
96 char *index();
97
d8336336 98 if (sscanf(line, " Index: %s %s ", folder, dir) != 2)
07a70c1c 99 return(NO);
d8336336 100 if (C = index(folder, '/')) { /* deal with "bin/from.c" */
07a70c1c
KB
101 if (C == folder)
102 return(NO);
103 *C = EOS;
104 }
d8336336 105 if (stat(dir, &sbuf) || (sbuf.st_mode & S_IFMT) != S_IFDIR)
07a70c1c 106 return(NO);
5ea1bf0e
KB
107 (void)pbuf(line);
108 return(YES);
109}
110
111/*
112 * pbuf --
113 * kludge so that summary file looks pretty
114 */
115static
116pbuf(line)
117 char *line;
118{
119 register char *rp, /* tmp pointers */
120 *wp;
121
122 for (rp = line; *rp == ' ' || *rp == '\t'; ++rp);
123 for (wp = line; *rp; ++wp) {
124 if ((*wp = *rp++) != ' ' && *wp != '\t')
125 continue;
126 *wp = ' ';
127 while (*rp == ' ' || *rp == '\t')
128 ++rp;
129 }
130 if (wp[-1] == ' ') /* wp can't == line */
131 --wp;
132 *wp = EOS;
07a70c1c
KB
133 return(YES);
134}