home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / libexec / bugfiler / gethead.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-12  |  4.6 KB  |  161 lines

  1. /*
  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, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char sccsid[] = "@(#)gethead.c    5.10 (Berkeley) 3/7/91";
  36. #endif /* not lint */
  37.  
  38. #include <sys/param.h>
  39. #include <sys/stat.h>
  40. #include <dirent.h>
  41. #include <unistd.h>
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include "pathnames.h"
  46. #include "bug.h"
  47.  
  48. static int    chk1(), pbuf();
  49.  
  50. #define ENT(X)    sizeof(X) - 1, X
  51. HEADER    mailhead[] = {                /* mail headers */
  52.     { NO, YES,  NULL, ENT("Date:"), },
  53.     { NO,  NO,  NULL, ENT("From "), },
  54.     { NO, YES,  NULL, ENT("From:"), },
  55.     { NO,  NO,  chk1, ENT("Index:"), },
  56.     { NO, YES,  NULL, ENT("Message-Id:"), },
  57.     { NO, YES,  NULL, ENT("Reply-To:"), },
  58.     { NO, YES,  NULL, ENT("Return-Path:"), },
  59.     { NO,  NO,  pbuf, ENT("Subject:"), },
  60.     { NO, YES,  NULL, ENT("To:"), },
  61.     { NO,  NO,  NULL, ENT("Apparently-To:"), },
  62.     { ERR, }
  63. };
  64.  
  65. FILE    *dfp;                /* distf file pointer */
  66. char    dir[MAXNAMLEN],            /* subject and folder */
  67.     folder[MAXNAMLEN];
  68.  
  69. /*
  70.  * gethead --
  71.  *    read mail and bug headers from bug report, construct redist headers
  72.  */
  73. gethead(redist)
  74.     int    redist;
  75. {
  76.     register HEADER    *hp;        /* mail header pointer */
  77.  
  78.     if (redist) {
  79.         int    fd;
  80.         char    *distf;
  81.  
  82.         distf = strdup(_PATH_TMP);
  83.         if (!(fd = mkstemp(distf)) || !(dfp = fdopen(fd, "w+")))
  84.             error("can't create redistribution file %s.", distf);
  85.         /* disappear after last reference is closed */
  86.         (void)unlink(distf);
  87.         free(distf);
  88.     }
  89.     if (!freopen(tmpname, "r", stdin))
  90.         error("can't read temporary bug file %s.", tmpname);
  91.  
  92.     while (fgets(bfr, sizeof(bfr), stdin)) {
  93.         for (hp = mailhead; hp->found != ERR; ++hp)
  94.             if (!hp->found)
  95.                 if (!strncmp(hp->tag, bfr, hp->len)) {
  96.                     if (hp->valid && !((*(hp->valid))(bfr)))
  97.                         break;
  98.                     if (!(hp->line = malloc((u_int)(strlen(bfr) + 1))))
  99.                         error("malloc failed.", CHN);
  100.                     (void)strcpy(hp->line, bfr);
  101.                     hp->found = YES;
  102.                     break;
  103.                 }
  104.         if ((hp->found == ERR || hp->redist) && redist)
  105.             fputs(bfr, dfp);
  106.     }
  107.  
  108.     if (!mailhead[INDX_TAG].found)
  109.         error("no readable \"Index:\" header in bug report.", CHN);
  110. }
  111.  
  112. /*
  113.  * chk1 --
  114.  *    parse the "Index:" line into folder and directory
  115.  */
  116. static
  117. chk1(line)
  118.     char    *line;
  119. {
  120.     register char    *C;        /* tmp pointer */
  121.     struct stat    sbuf;        /* existence check */
  122.     char    *index();
  123.  
  124.     if (sscanf(line, " Index: %s %s ", folder, dir) != 2)
  125.         return(NO);
  126.     if (C = index(folder, '/')) {    /* deal with "bin/from.c" */
  127.         if (C == folder)
  128.             return(NO);
  129.         *C = EOS;
  130.     }
  131.     if (stat(dir, &sbuf) || (sbuf.st_mode & S_IFMT) != S_IFDIR)
  132.         return(NO);
  133.     (void)pbuf(line);
  134.     return(YES);
  135. }
  136.  
  137. /*
  138.  * pbuf --
  139.  *    kludge so that summary file looks pretty
  140.  */
  141. static
  142. pbuf(line)
  143.     char    *line;
  144. {
  145.     register char    *rp,            /* tmp pointers */
  146.             *wp;
  147.  
  148.     for (rp = line; *rp == ' ' || *rp == '\t'; ++rp);
  149.     for (wp = line; *rp; ++wp) {
  150.         if ((*wp = *rp++) != ' ' && *wp != '\t')
  151.             continue;
  152.         *wp = ' ';
  153.         while (*rp == ' ' || *rp == '\t')
  154.             ++rp;
  155.     }
  156.     if (wp[-1] == ' ')            /* wp can't == line */
  157.         --wp;
  158.     *wp = EOS;
  159.     return(YES);
  160. }
  161.