home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1997 March / Simtel-MSDOS-Mar1997-CD2.iso / 00_start / uucat.c < prev    next >
C/C++ Source or Header  |  1996-02-23  |  2KB  |  145 lines

  1.  
  2. /* uucat.c */
  3.  
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. #define TRUE    1
  8. #define FALSE    0
  9.  
  10. #define LENGTH    150
  11.  
  12. extern void uuread();
  13.  
  14.  
  15. void main(argc, argv)
  16. int argc;
  17. char *argv[];
  18. {
  19.   int error, argno;
  20.   FILE *infile;
  21.  
  22.   if (argc < 2)
  23.     uuread(stdin);
  24.   else
  25.   {
  26.     error = FALSE;
  27.     for (argno = 1; !error && argno < argc; argno++)
  28.     {
  29.       if ((infile = fopen(argv[argno], "r")) == NULL)
  30.       {
  31.     error = TRUE;
  32.     fprintf(stderr, "uucat: Can't open '%s' for input.\n", argv[argno]);
  33.       }
  34.       else
  35.       {
  36.     uuread(infile);
  37.     fclose(infile);
  38.       }
  39.     }
  40.   }
  41.  
  42.   exit(0);
  43. }
  44.  
  45.  
  46. void uuread(infile)
  47. FILE *infile;
  48. {
  49.   char *s2, *s1, *s0, *tmp_s;
  50.   int length;
  51.   static char s[3 * (LENGTH + 1)];
  52.   static int echo_on = FALSE, started = FALSE, just_finished = FALSE;
  53.   static int line_length = 0, lines_to_go = 0;
  54.  
  55.   s0 = s;
  56.   s1 = s0 + (LENGTH + 1);
  57.   s2 = s1 + (LENGTH + 1);
  58.  
  59.   s0[0] = s1[0] = s2[0] = '\0';  /* Clear strings */
  60.  
  61.   while (fgets(s0, LENGTH, infile) != NULL)
  62.   {
  63.     s0[LENGTH] = '\0';  /* Make sure string is terminated */
  64.  
  65.     if (just_finished)
  66.     {
  67.       if (strncmp(s0, "size ", 5) == 0)
  68.       {
  69.     fputs(s0, stdout);
  70.     s0[0] = '\0';
  71.       }
  72.       just_finished = FALSE;
  73.     }
  74.  
  75.     if (!started)
  76.     {
  77.       if (strncmp(s0, "begin ", 6) == 0)
  78.       {
  79.     started = echo_on = TRUE;
  80.     line_length = 0;
  81.     lines_to_go = 0;
  82.       }
  83.     }
  84.     else  /* started */
  85.     {
  86.       length = strlen(s0);
  87.       if (line_length == 0)
  88.     line_length = length;
  89.  
  90.       if (echo_on)
  91.       {
  92.     lines_to_go = 0;
  93.     if (s0[0] != 'M' || length != line_length)
  94.     {
  95.       echo_on = FALSE;
  96.       lines_to_go = 2;  /* Lines to go before 'end' is expected */
  97.       if (s0[0] == ' ' || s0[0] == '`')
  98.         lines_to_go = 1;
  99.     }
  100.       }
  101.       else  /* !echo_on */
  102.       {
  103.     if (s0[0] == 'M' && length == line_length)
  104.       echo_on = TRUE;
  105.     else if (lines_to_go > 0)
  106.     {
  107.       if (lines_to_go == 2)
  108.       {
  109.         if (s0[0] == ' ' || s0[0] == '`')
  110.           lines_to_go = 1;
  111.         else
  112.           lines_to_go = 0;  /* Unexpected line, so break off */
  113.       }
  114.       else if (lines_to_go == 1)
  115.       {
  116.         if (strcmp(s0, "end\n") == 0)
  117.         {
  118.           fputs(s2, stdout);
  119.           fputs(s1, stdout);
  120.           fputs(s0, stdout);
  121.           lines_to_go = 0;  /* Done. Break off */
  122.           just_finished = TRUE;
  123.           started = FALSE;
  124.         }
  125.         else
  126.           lines_to_go = 0;  /* Unexpected line, so break off */
  127.       }
  128.     }
  129.       }
  130.     }
  131.  
  132.     if (echo_on)
  133.     {
  134.       fputs(s0, stdout);
  135.       s0[0] = '\0';
  136.     }
  137.  
  138.     tmp_s = s2;
  139.     s2 = s1;
  140.     s1 = s0;
  141.     s0 = tmp_s;
  142.   }
  143. }
  144.  
  145.