home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Unix / CNews / Source / explode / explode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-19  |  2.9 KB  |  149 lines

  1. /*
  2.  * explode - read relaynews master batch file & write all the real batch files.
  3.  * for UUNET, the C version needs to fnlock all descriptors
  4.  */
  5.  
  6. /*
  7. BEGIN    { path = "error"; size = "error"; msgid = "error" }
  8. /^($|#)/    { next }    # comment
  9. NF == 3 && /^</    {            # start a new article
  10.     msgid = $1
  11.     path = $2
  12.     size = $3
  13.     next
  14. }
  15. NF == 2 && /^F/    { print path >>$2; next }        # B format
  16. NF == 2 && /^f/    { print path, size >>$2; next }        # C format
  17. NF == 2 && /^n/    { print path, msgid >>$2; next }    # NNTP format
  18. NF == 2 && /^I/    { print msgid >>$2; next }        # ihave/sendme format
  19.  
  20.     { print "bad input on line", NR, ": " $0 | "cat >&2" }
  21.  */
  22.  
  23. #include <stdio.h>
  24. #include <ctype.h>
  25. #include <errno.h>
  26. #include <sys/types.h>
  27. #include "libc.h"
  28. #include "news.h"
  29. #include "fgetmfs.h"
  30. #include "trbatch.h"
  31.  
  32. /* imports */
  33. extern int optind;
  34. extern char *optarg;
  35. extern FILE *efopen();
  36.  
  37. /* exports */
  38. char *progname = "";
  39. int debug;
  40.  
  41. /*
  42.  * main - parse arguments and handle options
  43.  */
  44. main(argc, argv)
  45. int argc;
  46. char *argv[];
  47. {
  48.     int c, errflg = 0;
  49.  
  50.     if (argc > 0)
  51.         progname = argv[0];
  52.     while ((c = getopt(argc, argv, "d")) != EOF)
  53.         switch (c) {
  54.         case 'd':
  55.             ++debug;
  56.             break;
  57.         default:
  58.             errflg++;
  59.             break;
  60.         }
  61.     if (errflg) {
  62.         (void) fprintf(stderr, "usage: %s [-d] [file]...\n", progname);
  63.         exit(2);
  64.     }
  65.  
  66.     morefds();
  67.     if (optind >= argc)
  68.         process(stdin, "stdin");
  69.     else
  70.         for (; optind < argc; optind++)
  71.             if (STREQ(argv[optind], "-"))
  72.                 process(stdin, "-");
  73.             else {
  74.                 FILE *in = efopen(argv[optind], "r");
  75.  
  76.                 process(in, argv[optind]);
  77.                 (void) fclose(in);
  78.             }
  79.     (void) bfrealclose();        /* paranoia */
  80.     exit(0);
  81. }
  82.  
  83. /*
  84.  * process - process input file
  85.  */
  86. process(in, inname)
  87. FILE *in;
  88. char *inname;
  89. {
  90.     register char *line, *name;
  91.     register struct batchfile *bf;
  92.     register long lsize = 0;
  93.     char *fields[3];
  94.     char *msgid = NULL, *path = NULL, *size = NULL;
  95.  
  96. #ifdef UUNET
  97.     if (!fnlockfile(in))
  98.         error("can't lock %s", inname);
  99. #endif
  100.     while ((line = fgetms(in)) != NULL) {
  101.         switch (*line) {
  102.         case '#':
  103.         case '\n':
  104.             break;            /* ignore comments */
  105.         case '<':            /* new article */
  106.             trim(line);
  107.             if (split(line, fields, 3, "") != 3) {
  108.                 errno = 0;
  109.                 warning("malformed article line: %s", line);
  110.             } else {
  111.                 msgid = strsave(fields[0]);
  112.                 path = strsave(fields[1]);
  113.                 size = strsave(fields[2]);
  114.                 lsize = atol(size);
  115.             }
  116.             if (debug)
  117.                 (void) fprintf(stderr,
  118.                            "%s: new article %s %s %s\n",
  119.                            progname, msgid, path, size);
  120.             break;
  121.         case 'F':
  122.         case 'f':
  123.         case 'n':
  124.         case 'I':
  125.             trim(line);
  126.             if (msgid == NULL) {
  127.                 errno = 0;
  128.                 warning(
  129.                 "batch file line before article line: %s",
  130.                     line);
  131.             } else {
  132.                 for (name = line+1; isascii(*name) &&
  133.                     isspace(*name); name++)
  134.                     ;
  135.                 bf = bfopen(name);
  136.                 if (!bfappend(bf, *line, name, path, msgid,
  137.                     lsize))
  138.                     error("error writing %s", name);
  139.             }
  140.             break;
  141.         default:
  142.             errno = 0;
  143.             warning("bad input line: %s", line);
  144.             break;
  145.         }
  146.         free(line);
  147.     }
  148. }
  149.