home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1997 March / Simtel-MSDOS-Mar1997-CD1.iso / 00_start / undigest.c < prev    next >
C/C++ Source or Header  |  1996-07-09  |  8KB  |  302 lines

  1. /*
  2.     undigest - reformats a mailing list digest for use with "mail -f"
  3.  
  4.     Compile on Unix with this command:
  5.        cc -O undigest.c -o undigest
  6.  
  7.     If you wish to use the LONGNAME option use this command:
  8.        cc -O undigest.c -o undigest -DLONGNAME
  9.  
  10.     Modified 6/20/94 to handle a digest without a Unix From line.  kbp
  11.  
  12.     Modified 6/19/94 to add graceful exit on EOF errors.  kbp
  13.  
  14.     Modified 6/15/94 to add newline after the Preamble.  kbp
  15.  
  16.     Modified 1/10/94 to add compiling instructions.  kbp
  17.  
  18.     Modified 11/30/93 to increase lead from 90 to 1024 to allow
  19.     for headers with long lines.  kbp
  20.  
  21.     Modified 1/27/93 to increase temp def from 81 to 1024 to allow
  22.     for headers with long lines.  kbp
  23.  
  24.     Modified 12/5/92 to use the dashed break lines in the digest,
  25.     rather than the "Date:" lines of the member messages.  This
  26.     makes the program immune to differences in the order of the
  27.     headers in the member messages.  It also prevents false breaks
  28.     caused by messages included within member messages.  Renamed to
  29.     undigest.c to avoid confusion with the digest creator.  kbp
  30.  
  31.     Modified 11/16/90 to fix use of an uninitialized variable
  32.     (could cause a run time error on some machines)
  33.     and a variable set but not used to eliminate the warnings
  34.     from the Apollo c compiler.  jdb
  35.  
  36.     Modified 1/27/88 to handle more types of digests and
  37.     a compile-time option added (LONGNAME) which will cause
  38.     the default output file name to be <digest-name>.VOL.NUM
  39.     rather than the VOL.NUM form described below.
  40.  
  41.     This has been tested with the Info-IBM, Info-Kermit, and
  42.     Info-CPM Digest formats.
  43.  
  44.     This program should be called 'UNDIGEST' rather than 'DIGEST'
  45.     as it is below since there is a companion program 'digest' that
  46.     creates a Digest file from individual messages.
  47.  
  48.     The original documentation below has NOT been modified to
  49.     reflect these changes.
  50.  
  51.                     David Brown
  52.                     jdb@email.ncsc.navy.mil
  53. */
  54. /*
  55. DIGEST:  (version 3) November, 1984
  56.  
  57. NAME: 
  58.     Digest  - reformats the ailist digest for use with "mail -f"
  59.  
  60. SYNOPSIS:
  61.     digest file [file] 
  62.  
  63. DESCRIPTION:
  64.  
  65. digest takes the file name  given  in  first  argument  and  places  the
  66. reformatted  file  in  the  second argument, if given. If no output file
  67. (2nd argument) is given, the output will be placed into a  default  file
  68. whose  name  is of the form VOL.NUM where VOL and NUM are the volume and
  69. number of the ailist digest fed in (e.g. 2.144,etc.).
  70.  
  71. A few notes:
  72.     (1) if only one argument is given, it is assumed to be the
  73.         input file.  If no args are given, you get prompted for
  74.         for the input file, and the output is sent to the default
  75.         file construction.
  76.  
  77.     (2) This has been tested only for use with the specific ailist/human-
  78.         nets format now in use.  I will soon get around to adding
  79.         code to this program to handle other formats.  When I do,
  80.         I will send it along.
  81.  
  82.     (3) The input to this program must be A SINGLE AILIST 
  83.         OR HUMAN-NETS DIGEST.  If you have been stuffing all 
  84.         your ailist digests into a single file, running this pgm 
  85.         on that file will yield  incorrect and unpredictable results.  
  86.         The pgm is best  used to manage the incoming stuff.
  87.  
  88.     (4) the input file is left untouched (i.e. is not removed)
  89.  
  90.     (5) digest does not work with piped input (a bug, sorry).
  91.         This has meant for me that I stick the day's ailist digest
  92.         into a temp file when I receive it over "mail", and then
  93.         later "digest" this temp file to get it into a suitable
  94.         form for "mail -f".
  95.  
  96. BUGS:
  97.     If there are ailist sub-entries which do not have a DATE:
  98.     field in the header, they will be appended to the entry
  99.     prior.  
  100.  
  101. Any questions, suggestions or problems, etc. should be sent to 
  102.  
  103. douglas stumberger
  104. department of computer science
  105. 111 Cumington Street
  106. boston, ma. 02215
  107.  
  108. csnet: des@bostonu
  109. bitnet: csc10304@bostonu
  110.  
  111. */
  112.  
  113. #include <stdio.h>
  114.  
  115. /* the following defines the exact length of the break lines in the digest,
  116.    which are used to determine where the member messages begin and end. */
  117.  
  118. /* a line of 70 hyphens separates the Preamble from the rest of the digest. */
  119. char *endhead = "----------------------------------------------------------------------\n";
  120.  
  121. /* a line of 30 hyphens separates the messages in the digest. */
  122. char *keyline = "------------------------------\n";
  123.  
  124. #define DUMMY    "From dummy@line Wed Feb 29 12:12:12 GMT 1990"
  125.  
  126. main(argc,argv)
  127.     int argc; char *argv[] ;
  128. {
  129.     FILE *fpr, *fpw ;
  130.     char *lead, *leadone, *fromline, temp[1024], fname[81] ,
  131.         digest[81],vol[50],num[5] ;
  132.     register int done=0, nofrom=0, gl ;
  133.  
  134.     if (argc > 3) {
  135.         printf("Usage: %s file [file]\n",argv[0]) ;
  136.         exit(0);
  137.     }
  138.     if (argc == 1) { 
  139.         printf("What file is the digest in? > ") ;
  140.         scanf("%s",fname) ;
  141.     }
  142.     else 
  143.         strcpy(fname,argv[1]) ;
  144.     
  145.     if ((fpr = fopen(fname,"r")) == NULL) {
  146.         printf("%s: No such file\n",fname) ;
  147.         exit(0) ;
  148.     }
  149.  
  150. #ifdef DEBUG
  151.     printf(" input file name is <%s>\n",fname) ;
  152. #endif
  153.  
  154.     lead = (char *) calloc(1024,sizeof(char)) ;  
  155.     leadone = (char *) calloc(1024,sizeof(char)) ;  
  156.  
  157.     /* get the first line of file */
  158.     if (get_line(fpr,leadone) == EOF) {
  159.         fprintf(stderr, "\nundigest: premature EOF while reading Unix from line.\n");
  160.         fclose(fpr) ;
  161.         exit(1);
  162.     }
  163.  
  164. /* a dummy Unix from line in case it's missing */
  165.  
  166.     if (strncmp(leadone, "From ", 5) != 0) {
  167.         strcat(lead, DUMMY) ;
  168.         strcat(lead, "\n") ;
  169.         nofrom = 1 ;
  170.     }
  171.     else
  172.         strcat(lead, leadone) ;
  173.  
  174.     fromline = (char *) malloc(strlen(lead)+1) ;
  175.     strcpy(fromline,lead) ;
  176.  
  177.     if (argc != 3) {   /* no output file given - 
  178.                 find out vol/num for filename */
  179.  
  180.         while ((strcmp(lead,endhead)) && (!done)) {
  181.  
  182. #ifdef DEBUG
  183. printf("Scanning:%s",lead);
  184. #endif
  185.             sscanf(lead,"%s %s",digest,temp) ;
  186.             if (!strcmp(temp,"Digest")) {
  187. #ifdef DEBUG
  188. printf("\nFound a match\n");
  189. #endif
  190.                    sscanf(lead,"%*s %*s %*s %*s %*s %*s %*s %s %*c %*s %s",
  191.                    vol,num) ;
  192.                done++ ;
  193.             }
  194.     if (get_line(fpr,lead) == EOF) {
  195.         fprintf(stderr, "\nundigest: premature EOF while searching for digest name.\n");
  196.         fclose(fpr) ;
  197.         exit(1);
  198.     }
  199.         }
  200.  
  201.         strcat(digest,".") ;        
  202. #ifndef LONGNAME
  203.         digest[0]='\0';
  204. #endif
  205.         strcat(digest,vol) ;
  206.         strcat(digest,".") ;
  207.         strcat(digest,num) ;
  208.     }
  209.     else 
  210.         strcpy(digest,argv[2]) ;    /* output filename is third argument */
  211.  
  212.  
  213. #ifdef DEBUG
  214.         printf("output file is <%s>",digest) ;
  215. #endif
  216.  
  217.     fclose(fpr) ;            
  218.  
  219. #ifdef DEBUG
  220.         printf(" input file is <%s>\n",fname) ;
  221. #endif
  222.  
  223.     if ((fpr = fopen(fname,"r")) == NULL) {
  224.         printf("\nERROR: File will not rewind\n") ;
  225.         exit(0) ;
  226.     }
  227.  
  228.     if ((fpw = fopen(digest,"w")) == NULL) {
  229.         printf("\nERROR: Output File will not open\n") ;
  230.         exit(0) ;
  231.     }
  232.  
  233.     lead[0]='\0';
  234.  
  235.     /* copy the ailist header */
  236.     if (get_line(fpr,leadone) == EOF) {
  237.         fprintf(stderr, "\nundigest: premature EOF while reading message header.\n");
  238.         fprintf(fpw,"\n") ;
  239.         fclose(fpw) ;
  240.         fclose(fpr) ;
  241.         exit(1);
  242.     }
  243.  
  244.     if (nofrom != 0) {
  245.         strcat(lead, DUMMY) ;
  246.         strcat(lead, "\n") ;
  247.         strcat(lead, leadone) ;
  248.     }
  249.     else
  250.         strcat(lead, leadone) ;
  251.  
  252.     while (strcmp (lead,endhead)) {    /* i.e.  Law's message of the topics */
  253.         fprintf(fpw,"%s",lead) ;
  254.  
  255.     if (get_line(fpr,lead) == EOF) {
  256.         fprintf(stderr, "\nundigest: premature EOF while reading topics.\n");
  257.         fprintf(fpw,"\n") ;
  258.         fclose(fpw) ;
  259.         fclose(fpr) ;
  260.         exit(1);
  261.     }
  262.  
  263.     }
  264.  
  265.     fprintf(fpw,"\n%s",fromline) ;    /* add first break line after header */
  266.     fflush(fpw) ;
  267.     gl = get_line(fpr,lead) ;    /* gobble up blank line */
  268.     gl = get_line(fpr,lead) ;    /* do the body of the digest */
  269.  
  270.     while (gl != EOF) {
  271.  
  272.         if (!strcmp (lead,keyline))
  273.             {
  274.             fprintf(fpw,"%s",fromline) ;
  275.             gl = get_line(fpr,lead) ;
  276.             gl = get_line(fpr,lead) ;
  277.             }
  278.  
  279.         fprintf(fpw,"%s",lead) ;
  280.         gl = get_line(fpr,lead) ;
  281.     }
  282.  
  283.     fprintf(fpw,"\n") ;
  284.     fclose(fpw) ;
  285.     fclose(fpr) ;
  286.     printf("Re-formatted digest now in file <%s>\n",digest) ;
  287.     }    
  288.  
  289.  
  290. get_line (fp,s)
  291.     FILE *fp;    char *s;
  292. {
  293.     register int c=0,i=0 ;
  294.  
  295.     while ((c != '\n') && (c != EOF)) {
  296.         c = getc(fp) ;
  297.         *(s+i++) = c ;
  298.     }
  299.     *(s+i) = '\0' ;
  300.     return(c) ;
  301. }
  302.