home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 1 / GoldFishApril1994_CD2.img / d4xx / d473 / cnewssrc / cnews_src.lzh / relay / hdrmunge.c < prev    next >
C/C++ Source or Header  |  1990-05-28  |  9KB  |  290 lines

  1. /* :ts=4
  2.  * Usenet header modification & generation
  3.  *
  4.  * Ideally, headers should never be modified; message text, including
  5.  * headers, should be passed untouched.  Path: and Xref: demand that this
  6.  * rule be violated, and we delete huge obsolete headers to save space.
  7.  *
  8.  * Delete obsolete & large headers and Xref (can't be right),
  9.  * as headers are read.
  10.  * Recognise Newsgroups: and if more than one group, generate Xref: &
  11.  * leave holes for the article numbers - fileart will fill them in.
  12.  * (Make rn look in history instead?)
  13.  *
  14.  * Pile up headers into a static buffer until end of buffer (i.e. next
  15.  * line might not fit) [checked in hdrsave()], end of headers, end of
  16.  * file, or byte count is exhausted [checked in cparttofp].  Then write
  17.  * them to disk.  Prepend hostname! to Path: value, during output.
  18.  *
  19.  *    $Log$
  20.  */
  21.  
  22. #include <stdio.h>
  23. #ifdef unix
  24. # include <sys/types.h>
  25. #endif /* unix */
  26. #include "libc.h"
  27. #include "news.h"
  28. #include "case.h"
  29. #include "fileart.h"
  30. #include "headers.h"
  31. #include "article.h"
  32. #include "hdrint.h"
  33. #include "msgs.h"
  34.  
  35. /*
  36.  * HDRMEMSIZ is the length of a header-stashing buffer, which is used
  37.  * only during article-header copying.
  38.  * HDRMEMSIZ can be too small if memory is tight & will only hurt performance.
  39.  * Derivation: 630 bytes is a large header (after discarding *-Version:, etc.).
  40.  */
  41. #ifdef AMIGA
  42. # define HDRMEMSIZ 16384    /* This (helps) avoid the link/unlink problem */
  43. #else /* !AMIGA */
  44. # ifndef HDRMEMSIZ
  45. #  ifdef SMALLMEM
  46. #   define HDRMEMSIZ 630
  47. #  else
  48. #   define HDRMEMSIZ 8192        /* # bytes for saving headers in core */
  49. #  endif /* SMALLMEM */
  50. # endif    /* HDRMEMSIZ */
  51. #endif /* AMIGA */
  52.  
  53. /* private */
  54. static char **hptrs = NULL;    /* saved-headers-ptrs array; allocated once */
  55.  
  56. /* forwards */
  57. FORWARD void emithdr(), hdrsave();
  58.  
  59. /*
  60.  * Generate an Xref: header from art->a_files.
  61.  * Turn slashes in art->a_files into colons in Xref:.
  62.  */
  63. void
  64. emitxref(art)
  65. register struct article *art;
  66. {
  67.     register char *slashp, *xrefs;
  68.  
  69.     if (!art->a_xref) {
  70.         art->a_xref = YES;
  71.         xrefs = strsave(art->a_files);
  72.         for (slashp = xrefs; (slashp = strchr(slashp, FNDELIM)) != NULL; )
  73.             *slashp++ = ':';
  74.         if (fprintf(art->a_artf, "%s %s %s\n",
  75.             xrefhdr.hdrnm, hostname(), xrefs) == EOF)
  76.             fulldisk(art, spoolnm(art));
  77.         free(xrefs);
  78.     }
  79. }
  80.  
  81. /*
  82.  * --- header copying starts here ---
  83.  */
  84.  
  85. /*
  86.  * Copy headers and delete or modify a few.  Assumes hdrparse has been called.
  87.  * Delete obsolete & large headers and Xref.
  88.  * Pile up other headers for later output (Path: is changed during output).
  89.  *
  90.  * art->a_artf may be NULL, and may get set by hdrsave.
  91.  */
  92. STATIC void
  93. hdrmunge(art, buffer, hdrlen, hdrlst)
  94. register struct article *art;
  95. register char *buffer;
  96. int hdrlen;            /* optimisation only */
  97. hdrlist hdrlst;            /* headers of negative utility: snuff 'em */
  98. {
  99.     register struct hdrdef **vhp;
  100.  
  101.     if (headdebug)
  102.         (void) fputs(buffer, stderr);
  103.     for (vhp = hdrlst; *vhp != NULL; vhp++) {
  104.         register CONST char *hdrnm = (*vhp)->hdrnm;
  105.  
  106.         if (CISTREQN(buffer, hdrnm, (int)(*vhp)->hdrlen))
  107.             return;            /* don't save this header */
  108.     }
  109.     hdrsave(art, buffer, hdrlen);
  110. }
  111.  
  112. /*
  113.  * If headers already dumped, just write to art->a_artf.
  114.  * Else if there is room, stash "hdr" away until end of headers is seen
  115.  * (could just wait until Newsgroups: and Control: are seen, if seen)
  116.  * or there is no room left in the header buffer, then open the first
  117.  * article link (on art->a_artf) and dump the saved headers and the current
  118.  * header to it.
  119.  *
  120.  * Copy into art->a_haccum (in future, could read in directly,
  121.  * iff copying is high on the profile).
  122.  *
  123.  * hdrstore is static because it is used repeatedly, it only makes sense
  124.  * to have one active at a time, and there is no memory saving in allocating
  125.  * and deallocating it, particularly since copyart's (header) buffer must
  126.  * coexist with hdrstore.
  127.  */
  128. STATIC void
  129. hdrsave(art, hdr, hdrlen)
  130. register struct article *art;
  131. char *hdr;
  132. register int hdrlen;            /* optimisation only */
  133. {
  134.     if (art->a_artf != NULL) {
  135.         emithdr(art, hdr, hdrlen);
  136.         return;
  137.     }
  138.     if (art->a_haccum == NULL) {
  139.         static char hdrstore[HDRMEMSIZ];
  140.  
  141.         art->a_haccum = hdrstore;
  142.         art->a_haccum[0] = '\0';
  143.         art->a_hnext = art->a_haccum;
  144.         art->a_hbytesleft = HDRMEMSIZ;
  145.     }
  146.     if (art->a_hbytesleft > hdrlen) {
  147.         /* add new ptr.-to-this-header to tail of saved-hdr-ptr.-list */
  148.         if (art->a_hptrs == NULL) {
  149.             art->a_hpused = 0;
  150.             art->a_hpalloced = MINSHPTRS;
  151.             if (hptrs == NULL)    /* once only */
  152.                 hptrs = (char **) nemalloc((unsigned)
  153.                     (art->a_hpalloced * sizeof(char *)));
  154.             art->a_hptrs = hptrs;
  155.         }
  156.         while (art->a_hpused >= art->a_hpalloced) {
  157.             art->a_hpalloced += MINSHPTRS;
  158.             art->a_hptrs = hptrs = (char **)
  159.                 realloc((char *)art->a_hptrs, (unsigned)
  160.                 (art->a_hpalloced * sizeof(char *)));
  161.             if (art->a_hptrs == NULL)
  162.                 errunlock("out of memory (for art->a_hptrs)", "");
  163.         }
  164.         art->a_hptrs[art->a_hpused++] = art->a_hnext;
  165.  
  166.         /* (void) strcat(art->a_haccum, hdr); */
  167.         (void) strcpy(art->a_hnext, hdr);
  168.         art->a_hnext += hdrlen;        /* points at NUL byte */
  169.         art->a_hbytesleft -= hdrlen;
  170.     } else {
  171.         hdrdump(art, NOTALLHDRS);    /* don't file */
  172.         if (art->a_artf != NULL)
  173.             emithdr(art, hdr, hdrlen);
  174.     }
  175. }
  176.  
  177. /*
  178.  * Change Path: while writing it out, just dump other headers (hdr) verbatim.
  179.  */
  180. STATIC void
  181. emithdr(art, hdr, hdrlen)
  182. register struct article *art;
  183. char *hdr;
  184. register int hdrlen;
  185. {
  186.     if (CISTREQN(hdr, pathhdr.hdrnm, (int)pathhdr.hdrlen)) {
  187.         register char *oldpath, *hostnm = hostname();
  188.  
  189.         oldpath = skipsp(&hdr[pathhdr.hdrlen]);
  190.         /*
  191.          * V7 f?printf return 0 or EOF, not a byte count, so it is
  192.          * not portable to use fprintf's return value as a byte count.
  193.          */
  194.         if (fprintf(art->a_artf, "%s %s!", pathhdr.hdrnm, hostnm) ==
  195.             EOF || fputs(oldpath, art->a_artf) == EOF)
  196.             fulldisk(art, spoolnm(art));
  197.         else {
  198.             static unsigned hostlen = 0;
  199.  
  200.             if (hostlen == 0)
  201.                 hostlen = strlen(hostnm);
  202.             art->a_charswritten += (pathhdr.hdrlen + 1 /* strlen(" ") */ +
  203.                 hostlen + 1 /* strlen("!") */ + strlen(oldpath));
  204.         }
  205.     } else {
  206.         if (fwrite(hdr, hdrlen, 1, art->a_artf) != 1)
  207.             fulldisk(art, spoolnm(art));
  208.         else
  209.             art->a_charswritten += hdrlen;
  210.     }
  211. }
  212.  
  213. /*
  214.  * Write out saved headers after opening on art->a_artf either a temporary
  215.  * file (using mktemp(3)) or the first article link, based on art->h.h_ngs &
  216.  * nxtartnum(); set a_tmpf to which ever name is opened.
  217.  * Modify Path: value on the way.
  218.  *
  219.  * If all headers were seen, then open the first link, link to the rest,
  220.  * and generate Xref:, else open a temporary name and write the article
  221.  * there (it will get filed later by hdrdump(...,ALLHDRS) or in insart()).
  222.  */
  223. void
  224. hdrdump(art, allhdrsseen)
  225. register struct article *art;
  226. boolean allhdrsseen;        /* all headers seen & hdrdeflt() called? */
  227. {
  228.     if (art->a_filed)
  229.         return;
  230.     if (allhdrsseen)
  231.         fileart(art);            /* set a_tmpf */
  232.     else if (art->a_artf == NULL) {
  233.         nnfree(&art->a_tmpf);
  234.         art->a_tmpf = strsave(SPOOLTMP);
  235.         (void) mktemp(art->a_tmpf);
  236.         art->a_unlink = YES;
  237.         art->a_artf = fopenwclex(art->a_tmpf, "w");
  238.         if (art->a_artf == NULL)
  239.             art->a_status |= ST_DROPPED;
  240.     }
  241.     if (art->a_artf != NULL &&
  242.         art->a_haccum != NULL && art->a_haccum[0] != '\0') {
  243.         register int i;
  244.         register char **pp;
  245.         register char *nxtln;
  246.         register int saved;
  247.  
  248.         for (i = 0, pp = art->a_hptrs; i < art->a_hpused; ++i, ++pp) {
  249.             if (i >= art->a_hpused-1)    /* last in-core hdr? */
  250.                 nxtln = art->a_hnext;
  251.             else
  252.                 nxtln = pp[1];
  253.             saved = *nxtln;
  254.             *nxtln = '\0';
  255.             emithdr(art, *pp, nxtln - *pp);
  256.                  *nxtln = saved;            /* restore */
  257.         }
  258.         /* art->a_haccum could be freed and zeroed here, if malloced */
  259.         art->a_haccum[0] = '\0';    /* paranoia */
  260.         art->a_hnext = art->a_haccum;    /* for next article */
  261.         art->a_hpused = 0;        /* trash saved-header-ptr-list */
  262.         /* reduce saved-header-ptr-list to original size */
  263.         if (art->a_hpalloced > MINSHPTRS) {
  264.             art->a_hpalloced = MINSHPTRS;
  265.             art->a_hptrs = hptrs = (char **)
  266.                 realloc((char *)art->a_hptrs, (unsigned)
  267.                 (art->a_hpalloced * sizeof(char *)));
  268.             if (hptrs == NULL)
  269.                 errunlock("can't free a_hptrs memory", "");
  270.         }
  271.     }
  272. }
  273.  
  274. /*
  275.  * hdrparse remembers this header if it's interesting.
  276.  * hdrmunge needs art->h.h_ngs to be set, so it is called second.
  277.  * hdrmunge saves or writes this header, unless it's deemed a waste of bytes.
  278.  * hdrmunge may call hdrdump(art, NOTALLHDRS).
  279.  * hdrdump counts art->a_charswritten.
  280.  */
  281. void
  282. hdrdigest(art, line, hdrlen)
  283. register struct article *art;
  284. register char *line;
  285. int hdrlen;
  286. {
  287.     hdrparse(&art->h, line, parsehdrs);
  288.     hdrmunge(art, line, hdrlen, hdrvilest);
  289. }
  290.