130162Sbostic /* 230162Sbostic * Copyright (c) 1986 Regents of the University of California. 330162Sbostic * All rights reserved. The Berkeley software License Agreement 430162Sbostic * specifies the terms and conditions for redistribution. 530162Sbostic */ 630162Sbostic 730162Sbostic #ifndef lint 8*31011Sbostic static char sccsid[] = "@(#)gethead.c 5.3 (Berkeley) 87/05/02"; 930162Sbostic #endif not lint 1030162Sbostic 1130162Sbostic #include <bug.h> 1230162Sbostic #include <sys/stat.h> 1330162Sbostic #include <stdio.h> 1430162Sbostic 15*31011Sbostic static int chk1(), pbuf(); 1630162Sbostic 1730162Sbostic #define ENT(X) sizeof(X) - 1, X 1830162Sbostic HEADER mailhead[] = { /* mail headers */ 1930162Sbostic { NO, YES, NULL, ENT("Date:"), }, 2030162Sbostic { NO, NO, NULL, ENT("From "), }, 2130162Sbostic { NO, YES, NULL, ENT("From:"), }, 2230162Sbostic { NO, NO, chk1, ENT("Index:"), }, 2330162Sbostic { NO, YES, NULL, ENT("Message-Id:"), }, 2430162Sbostic { NO, NO, NULL, ENT("Reply-To:"), }, 2530162Sbostic { NO, NO, NULL, ENT("Return-Path:"), }, 26*31011Sbostic { NO, NO, pbuf, ENT("Subject:"), }, 2730162Sbostic { NO, NO, NULL, ENT("To:"), }, 2830162Sbostic { ERR, } 2930162Sbostic }; 3030162Sbostic 3130890Sbostic FILE *dfp; /* distf file pointer */ 3230890Sbostic char dir[MAXNAMLEN], /* subject and folder */ 3330162Sbostic folder[MAXNAMLEN]; 3430162Sbostic 3530162Sbostic /* 3630162Sbostic * gethead -- 3730162Sbostic * read mail and bug headers from bug report, construct redist headers 3830162Sbostic */ 3930890Sbostic gethead(redist) 4030890Sbostic int redist; 4130162Sbostic { 4230162Sbostic register HEADER *hp; /* mail header pointer */ 4330890Sbostic char *strcpy(), *malloc(); 4430162Sbostic 4530890Sbostic if (redist) { 4630890Sbostic int fd; 4730890Sbostic char *distf; 4830162Sbostic 4930890Sbostic distf = "/tmp/BUG_XXXXXX"; 5030890Sbostic if (!(fd = mkstemp(distf)) || !(dfp = fdopen(fd, "w+"))) 5130890Sbostic error("can't create redistribution file %s.", distf); 5230890Sbostic /* disappear after last reference is closed */ 5330890Sbostic (void)unlink(distf); 5430890Sbostic } 5530890Sbostic if (!freopen(tmpname, "r", stdin)) 5630890Sbostic error("can't read temporary bug file %s.", tmpname); 5730890Sbostic 5830890Sbostic while (fgets(bfr, sizeof(bfr), stdin)) { 5930890Sbostic for (hp = mailhead; hp->found != ERR; ++hp) 6030162Sbostic if (!hp->found) 6130890Sbostic if (!strncmp(hp->tag, bfr, hp->len)) { 6230162Sbostic if (hp->valid && !((*(hp->valid))(bfr))) 6330162Sbostic break; 6430162Sbostic if (!(hp->line = malloc((u_int)(strlen(bfr) + 1)))) 6530890Sbostic error("malloc failed.", CHN); 6630890Sbostic (void)strcpy(hp->line, bfr); 6730162Sbostic hp->found = YES; 6830162Sbostic break; 6930162Sbostic } 7030890Sbostic if ((hp->found == ERR || hp->redist) && redist) 7130890Sbostic fputs(bfr, dfp); 7230162Sbostic } 7330162Sbostic 7430162Sbostic if (!mailhead[INDX_TAG].found) 7530890Sbostic error("no readable \"Index:\" header in bug report.", CHN); 7630162Sbostic } 7730162Sbostic 7830162Sbostic /* 7930162Sbostic * chk1 -- 8030162Sbostic * parse the "Index:" line into folder and directory 8130162Sbostic */ 8230162Sbostic static 8330162Sbostic chk1(line) 8430890Sbostic char *line; 8530162Sbostic { 8630162Sbostic register char *C; /* tmp pointer */ 8730162Sbostic struct stat sbuf; /* existence check */ 8830162Sbostic char *index(); 8930162Sbostic 9030890Sbostic if (sscanf(line, " Index: %s %s ", folder, dir) != 2) 9130162Sbostic return(NO); 9230890Sbostic if (C = index(folder, '/')) { /* deal with "bin/from.c" */ 9330162Sbostic if (C == folder) 9430162Sbostic return(NO); 9530162Sbostic *C = EOS; 9630162Sbostic } 9730890Sbostic if (stat(dir, &sbuf) || (sbuf.st_mode & S_IFMT) != S_IFDIR) 9830162Sbostic return(NO); 99*31011Sbostic (void)pbuf(line); 10030162Sbostic return(YES); 10130162Sbostic } 102*31011Sbostic 103*31011Sbostic /* 104*31011Sbostic * pbuf -- 105*31011Sbostic * kludge so that summary file looks pretty 106*31011Sbostic */ 107*31011Sbostic static 108*31011Sbostic pbuf(line) 109*31011Sbostic char *line; 110*31011Sbostic { 111*31011Sbostic register char *rp, /* tmp pointers */ 112*31011Sbostic *wp; 113*31011Sbostic 114*31011Sbostic for (rp = line; *rp == ' ' || *rp == '\t'; ++rp); 115*31011Sbostic for (wp = line; *rp; ++wp) { 116*31011Sbostic if ((*wp = *rp++) != ' ' && *wp != '\t') 117*31011Sbostic continue; 118*31011Sbostic *wp = ' '; 119*31011Sbostic while (*rp == ' ' || *rp == '\t') 120*31011Sbostic ++rp; 121*31011Sbostic } 122*31011Sbostic if (wp[-1] == ' ') /* wp can't == line */ 123*31011Sbostic --wp; 124*31011Sbostic *wp = EOS; 125*31011Sbostic return(YES); 126*31011Sbostic } 127