home *** CD-ROM | disk | FTP | other *** search
- /* nobs.c - Simple backspace filter - 1.1 */
-
- /*
- ** This program will take lines containing overstrike pragmas of
- ** the form <char><bs><char> and convert them to individual lines of
- ** output with a return but no line feed between them.
- ** Useful in filtering nroff output to line printers that cannot
- ** back space (or on which back spaces are expensive).
- */
-
- /*
- ** Author:
- ** Chad R. Larson This program is placed in the
- ** DCF, Inc. Public Domain. You may do with
- ** 14623 North 49th Place it as you please.
- ** Scottsdale, AZ 85254
- */
-
- #include <stdio.h>
-
- #define LINESIZE 512 /* maximum line length */
- #define MAXOVER 8 /* maximum number of overstrikes */
-
- /* forward references */
- void exit();
- char *memset();
-
- static char input[LINESIZE]; /* input line buffer */
- static char output[MAXOVER][LINESIZE]; /* output line buffers */
-
- void main()
- {
- int line; /* output buffer array index */
- int in_dex; /* offset into input buffer */
- int out_dex; /* offset into output buffer */
- int line_count; /* number of output lines */
- int strip; /* trailing space strip index */
- char chr; /* single character storage */
-
- /* loop through the input lines */
- while ( fgets(input, LINESIZE, stdin) != (char *)NULL ) {
-
- /* init output buffers to blanks */
- memset( output, ' ', sizeof(output) );
-
- /* slide through input line, dropping bs chars */
- out_dex = -1; /* reset array pointers */
- in_dex = 0;
- line = 0;
- line_count = 0;
-
- while ( ( chr = input[in_dex++] ) && chr != '\n' ) {
- if (chr != '\b') {
- line = 0; /* back to main line */
- output[line][++out_dex] = chr; /* stuff the character */
- } else { /* got backspace */
- ++line; /* select output buffer */
- if (line == MAXOVER) {
- fprintf(stderr, "Too many overstrikes!\n");
- exit(1);
- }
- output[line][out_dex] = input[in_dex++];
- line_count = (line_count < line) ? line : line_count;
- }
- } /* end of input line */
-
- /* print the output buffers */
- for (line = 0; line <= line_count; line++) {
- strip = out_dex;
- while (output[line][strip] == ' ') /* strip trailing spaces */
- --strip;
- ++strip; /* point past string end */
- if (line < line_count) /* new line or return? */
- output[line][strip] = '\r';
- else
- output[line][strip] = '\n';
- output[line][++strip] = '\0'; /* terminate string */
- fputs (output[line], stdout); /* print it */
- }
-
- } /* end of file */
- exit(0);
-
- } /* end of main */
-