home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1993 May / SIMTEL_0593.ISO / msdos / txtutl / undigest.c < prev    next >
C/C++ Source or Header  |  1993-01-27  |  7KB  |  239 lines

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