xref: /csrg-svn/libexec/bugfiler/gethead.c (revision 30162)
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
8 static char sccsid[] = "@(#)gethead.c	5.1 (Berkeley) 86/11/25";
9 #endif not lint
10 
11 #include <bug.h>
12 #include <sys/stat.h>
13 #include <sys/dir.h>
14 #include <stdio.h>
15 
16 static int	chk1();
17 
18 #define ENT(X)	sizeof(X) - 1, X
19 HEADER	mailhead[] = {				/* mail headers */
20 	{ NO, YES,  NULL, ENT("Date:"), },
21 	{ NO,  NO,  NULL, ENT("From "), },
22 	{ NO, YES,  NULL, ENT("From:"), },
23 	{ NO,  NO,  chk1, ENT("Index:"), },
24 	{ NO, YES,  NULL, ENT("Message-Id:"), },
25 	{ NO,  NO,  NULL, ENT("Reply-To:"), },
26 	{ NO,  NO,  NULL, ENT("Return-Path:"), },
27 	{ NO,  NO,  NULL, ENT("Subject:"), },
28 	{ NO,  NO,  NULL, ENT("To:"), },
29 	{ ERR, }
30 };
31 
32 extern short	do_redist,		/* if redistributing report */
33 		made_dist;		/* if dist file needs removing */
34 extern char	tmpname[];		/* temp bug report file */
35 
36 char	*distf = TMP_FILE,		/* redist temp file */
37 	dir[MAXNAMLEN],			/* subject and folder */
38 	folder[MAXNAMLEN];
39 
40 /*
41  * gethead --
42  *	read mail and bug headers from bug report, construct redist headers
43  */
44 gethead()
45 {
46 	register HEADER	*hp;		/* mail header pointer */
47 	register FILE	*dfp;		/* distf file pointer */
48 	char	*strcpy(), *malloc(), *mktemp();
49 
50 	if (do_redist && (!mktemp(distf) || !(dfp = fopen(distf,"w"))))
51 		error("unable to create redistribution file %s.",distf);
52 	made_dist = YES;
53 	if (!freopen(tmpname,"r",stdin))
54 		error("unable to read temporary bug file %s.",tmpname);
55 
56 	while (fgets(bfr,sizeof(bfr),stdin)) {
57 		for (hp = mailhead;hp->found != ERR;++hp)
58 			if (!hp->found)
59 				if (!strncmp(hp->tag,bfr,hp->len)) {
60 					if (hp->valid && !((*(hp->valid))(bfr)))
61 						break;
62 					if (!(hp->line = malloc((u_int)(strlen(bfr) + 1))))
63 						error("unable to allocate space for header search.",CHN);
64 					strcpy(hp->line,bfr);
65 					hp->found = YES;
66 					break;
67 				}
68 		if ((hp->found == ERR || hp->redist) && do_redist)
69 			fputs(bfr,dfp);
70 	}
71 
72 	if (!mailhead[INDX_TAG].found)
73 		error("no readable \"Index:\" header in bug report.",CHN);
74 	if (do_redist)
75 		fclose(dfp);
76 }
77 
78 /*
79  * chk1 --
80  *	parse the "Index:" line into folder and directory
81  */
82 static
83 chk1(line)
84 char	*line;
85 {
86 	register char	*C;		/* tmp pointer */
87 	struct stat	sbuf;		/* existence check */
88 	char	*index();
89 
90 	if (sscanf(line," Index: %s %s ",folder,dir) != 2)
91 		return(NO);
92 
93 	/* backward compatible, deal with "bin/from.c" */
94 	if (C = index(folder,'/')) {
95 		if (C == folder)
96 			return(NO);
97 		*C = EOS;
98 	}
99 
100 	if (stat(dir,&sbuf) || (sbuf.st_mode & S_IFMT) != S_IFDIR)
101 		return(NO);
102 	return(YES);
103 }
104