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