[9] | 1 | /*
|
---|
| 2 | * bsfilt.c - a colcrt-like processor for cawf(1)
|
---|
| 3 | */
|
---|
| 4 |
|
---|
| 5 | /*
|
---|
| 6 | * Copyright (c) 1991 Purdue University Research Foundation,
|
---|
| 7 | * West Lafayette, Indiana 47907. All rights reserved.
|
---|
| 8 | *
|
---|
| 9 | * Written by Victor A. Abell <abe@mace.cc.purdue.edu>, Purdue
|
---|
| 10 | * University Computing Center. Not derived from licensed software;
|
---|
| 11 | * derived from awf(1) by Henry Spencer of the University of Toronto.
|
---|
| 12 | *
|
---|
| 13 | * Permission is granted to anyone to use this software for any
|
---|
| 14 | * purpose on any computer system, and to alter it and redistribute
|
---|
| 15 | * it freely, subject to the following restrictions:
|
---|
| 16 | *
|
---|
| 17 | * 1. The author is not responsible for any consequences of use of
|
---|
| 18 | * this software, even if they arise from flaws in it.
|
---|
| 19 | *
|
---|
| 20 | * 2. The origin of this software must not be misrepresented, either
|
---|
| 21 | * by explicit claim or by omission. Credits must appear in the
|
---|
| 22 | * documentation.
|
---|
| 23 | *
|
---|
| 24 | * 3. Altered versions must be plainly marked as such, and must not
|
---|
| 25 | * be misrepresented as being the original software. Credits must
|
---|
| 26 | * appear in the documentation.
|
---|
| 27 | *
|
---|
| 28 | * 4. This notice may not be removed or altered.
|
---|
| 29 | */
|
---|
| 30 |
|
---|
| 31 | #include <stdio.h>
|
---|
| 32 |
|
---|
| 33 | #ifdef UNIX
|
---|
| 34 | # ifdef USG
|
---|
| 35 | #include <string.h>
|
---|
| 36 | # else /* not USG */
|
---|
| 37 | #include <strings.h>
|
---|
| 38 | # endif /* USG */
|
---|
| 39 | #else /* not UNIX */
|
---|
| 40 | #include <string.h>
|
---|
| 41 | #endif /* UNIX */
|
---|
| 42 |
|
---|
| 43 | #include <sys/types.h>
|
---|
| 44 |
|
---|
| 45 | #include "ansi.h"
|
---|
| 46 |
|
---|
| 47 | #define MAXLL 2048 /* ridiculous maximum line length */
|
---|
| 48 |
|
---|
| 49 | int Dash = 1; /* underline with dashes */
|
---|
| 50 | int Dp = 0; /* dash pending */
|
---|
| 51 | int Lc = 0; /* line count */
|
---|
| 52 | char *Pname; /* program name */
|
---|
| 53 | unsigned char Ulb[MAXLL]; /* underline buffer */
|
---|
| 54 | int Ulx = 0; /* underline buffer index */
|
---|
| 55 |
|
---|
| 56 | _PROTOTYPE(void Putchar,(int ch));
|
---|
| 57 |
|
---|
| 58 | main(argc, argv)
|
---|
| 59 | int argc;
|
---|
| 60 | char *argv[];
|
---|
| 61 | {
|
---|
| 62 | int ax = 1; /* argument index */
|
---|
| 63 | unsigned char c; /* character buffer */
|
---|
| 64 | FILE *fs; /* file stream */
|
---|
| 65 | int nf = 0; /* number of files processed */
|
---|
| 66 | unsigned char pc; /* previous character */
|
---|
| 67 | int under = 0; /* underline */
|
---|
| 68 | /*
|
---|
| 69 | * Save program name.
|
---|
| 70 | */
|
---|
| 71 | if ((Pname = strrchr(argv[0], '/')) != NULL)
|
---|
| 72 | Pname++;
|
---|
| 73 | else if ((Pname = strrchr(argv[0], '\\')) != NULL)
|
---|
| 74 | Pname++;
|
---|
| 75 | else
|
---|
| 76 | Pname = argv[0];
|
---|
| 77 | /*
|
---|
| 78 | * Process options.
|
---|
| 79 | */
|
---|
| 80 | if (argc > 1 && argv[1][0] == '-') {
|
---|
| 81 | switch (argv[1][1]) {
|
---|
| 82 | /*
|
---|
| 83 | * "-U" - underline with dashes.
|
---|
| 84 | */
|
---|
| 85 | case 'U':
|
---|
| 86 | Dash = 0;
|
---|
| 87 | under = 1;
|
---|
| 88 | break;
|
---|
| 89 | /*
|
---|
| 90 | * "-" - do no underlining at all.
|
---|
| 91 | */
|
---|
| 92 | case '\0':
|
---|
| 93 | Dash = under = 0;
|
---|
| 94 | break;
|
---|
| 95 | default:
|
---|
| 96 | (void) fprintf(stderr,
|
---|
| 97 | "%s usage: [-] [-U] [file]\n", Pname);
|
---|
| 98 | exit(1);
|
---|
| 99 | }
|
---|
| 100 | ax++;
|
---|
| 101 | }
|
---|
| 102 | /*
|
---|
| 103 | * Process files. Read standard input if no files names.
|
---|
| 104 | */
|
---|
| 105 |
|
---|
| 106 | while (ax < argc || nf == 0) {
|
---|
| 107 | if (ax >= argc)
|
---|
| 108 | fs = stdin;
|
---|
| 109 | else {
|
---|
| 110 | #ifdef UNIX
|
---|
| 111 | if ((fs = fopen(argv[ax], "r")) == NULL)
|
---|
| 112 | #else
|
---|
| 113 | if ((fs = fopen(argv[ax], "rt")) == NULL)
|
---|
| 114 | #endif
|
---|
| 115 | {
|
---|
| 116 | (void) fprintf(stderr, "%s: can't open %s\n",
|
---|
| 117 | Pname, argv[ax]);
|
---|
| 118 | exit(1);
|
---|
| 119 | }
|
---|
| 120 | ax++;
|
---|
| 121 | }
|
---|
| 122 | nf++;
|
---|
| 123 | /*
|
---|
| 124 | * Read input a character at a time.
|
---|
| 125 | */
|
---|
| 126 | for (pc = '\0';;) {
|
---|
| 127 | c = (unsigned char)fgetc(fs);
|
---|
| 128 | if (feof(fs))
|
---|
| 129 | break;
|
---|
| 130 | switch(c) {
|
---|
| 131 |
|
---|
| 132 | case '\n':
|
---|
| 133 | if (pc)
|
---|
| 134 | Putchar((int)pc);
|
---|
| 135 | Putchar('\n');
|
---|
| 136 | pc = '\0';
|
---|
| 137 | break;
|
---|
| 138 |
|
---|
| 139 | case '\b':
|
---|
| 140 | if (pc == '_') {
|
---|
| 141 | if (under) {
|
---|
| 142 | putchar(pc);
|
---|
| 143 | putchar('\b');
|
---|
| 144 | } else if (Dash)
|
---|
| 145 | Dp = 1;
|
---|
| 146 | }
|
---|
| 147 | pc = '\0';
|
---|
| 148 | break;
|
---|
| 149 |
|
---|
| 150 | default:
|
---|
| 151 | if (pc)
|
---|
| 152 | Putchar((int)pc);
|
---|
| 153 | pc = c;
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 | if (pc) {
|
---|
| 157 | Putchar((int)pc);
|
---|
| 158 | Putchar((int)'\n');
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
| 161 | exit(0);
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 |
|
---|
| 165 | /*
|
---|
| 166 | * Putchar(ch) - put a character with possible underlining
|
---|
| 167 | */
|
---|
| 168 |
|
---|
| 169 | void
|
---|
| 170 | Putchar(ch)
|
---|
| 171 | int ch;
|
---|
| 172 | {
|
---|
| 173 | int i; /* temporary index */
|
---|
| 174 |
|
---|
| 175 | if ((unsigned char)ch == '\n') {
|
---|
| 176 | /*
|
---|
| 177 | * Handle end of line.
|
---|
| 178 | */
|
---|
| 179 | putchar('\n');
|
---|
| 180 | if (Ulx) {
|
---|
| 181 | while (Ulx && Ulb[Ulx-1] == ' ')
|
---|
| 182 | Ulx--;
|
---|
| 183 | if (Ulx) {
|
---|
| 184 | for (i = 0; i < Ulx; i++)
|
---|
| 185 | putchar(Ulb[i]);
|
---|
| 186 | putchar('\n');
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
| 189 | Dp = Ulx = 0;
|
---|
| 190 | Lc++;
|
---|
| 191 | return;
|
---|
| 192 | }
|
---|
| 193 | /*
|
---|
| 194 | * Put "normal" character.
|
---|
| 195 | */
|
---|
| 196 | putchar((unsigned char)ch);
|
---|
| 197 | if (Dash) {
|
---|
| 198 |
|
---|
| 199 | /*
|
---|
| 200 | * Handle dash-type underlining.
|
---|
| 201 | */
|
---|
| 202 | if (Ulx >= MAXLL) {
|
---|
| 203 | (void) fprintf(stderr,
|
---|
| 204 | "%s: underline for line %d > %d characters\n",
|
---|
| 205 | Pname, Lc, MAXLL);
|
---|
| 206 | exit(1);
|
---|
| 207 | }
|
---|
| 208 | Ulb[Ulx++] = Dp ? '-' : ' ';
|
---|
| 209 | Dp = 0;
|
---|
| 210 | }
|
---|
| 211 | }
|
---|