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