home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume3 / nobs / nobs.c < prev   
C/C++ Source or Header  |  1989-02-03  |  2KB  |  85 lines

  1. /* nobs.c - Simple backspace filter - 1.1 */
  2.  
  3. /*
  4. ** This program will take lines containing overstrike pragmas of
  5. ** the form <char><bs><char> and convert them to individual lines of
  6. ** output with a return but no line feed between them.
  7. ** Useful in filtering nroff output to line printers that cannot
  8. ** back space (or on which back spaces are expensive).
  9. */
  10.  
  11. /*
  12. ** Author:
  13. **   Chad R. Larson            This program is placed in the
  14. **   DCF, Inc.                Public Domain.  You may do with
  15. **   14623 North 49th Place        it as you please.
  16. **   Scottsdale, AZ 85254
  17. */
  18.  
  19. #include <stdio.h>
  20.  
  21. #define    LINESIZE    512        /* maximum line length */
  22. #define MAXOVER        8        /* maximum number of overstrikes */
  23.  
  24. /* forward references */
  25. void exit();
  26. char *memset();
  27.  
  28. static char input[LINESIZE];        /* input line buffer */
  29. static char output[MAXOVER][LINESIZE];    /* output line buffers */
  30.  
  31. void main()
  32. {
  33.     int        line;        /* output buffer array index */
  34.     int        in_dex;        /* offset into input buffer */
  35.     int        out_dex;    /* offset into output buffer */
  36.     int        line_count;    /* number of output lines */
  37.     int        strip;        /* trailing space strip index */
  38.     char    chr;        /* single character storage */
  39.  
  40.     /* loop through the input lines */
  41.     while ( fgets(input, LINESIZE, stdin) != (char *)NULL ) {
  42.  
  43.     /* init output buffers to blanks */
  44.     memset( output, ' ', sizeof(output) );
  45.  
  46.     /* slide through input line, dropping bs chars */
  47.     out_dex = -1;        /* reset array pointers */
  48.     in_dex = 0;
  49.     line = 0;
  50.     line_count = 0;
  51.  
  52.     while ( ( chr = input[in_dex++] ) && chr != '\n' ) {
  53.         if (chr != '\b') {
  54.         line = 0;            /* back to main line */
  55.         output[line][++out_dex] = chr;    /* stuff the character */
  56.         } else {    /* got backspace */
  57.         ++line;            /* select output buffer */
  58.         if (line == MAXOVER) {
  59.             fprintf(stderr, "Too many overstrikes!\n");
  60.             exit(1);
  61.         }
  62.         output[line][out_dex] = input[in_dex++];
  63.         line_count = (line_count < line) ? line : line_count;
  64.         }
  65.     } /* end of input line */
  66.  
  67.     /* print the output buffers */
  68.     for (line = 0; line <= line_count; line++) {
  69.         strip = out_dex;
  70.         while (output[line][strip] == ' ')    /* strip trailing spaces */
  71.         --strip;
  72.         ++strip;                /* point past string end */
  73.         if (line < line_count)        /* new line or return? */
  74.         output[line][strip] = '\r';
  75.         else
  76.         output[line][strip] = '\n';
  77.         output[line][++strip] = '\0';    /* terminate string */
  78.         fputs (output[line], stdout);    /* print it */
  79.     }
  80.  
  81.     } /* end of file */
  82.     exit(0);
  83.  
  84. } /* end of main */
  85.