minor fixes for safecopy & safemap tests
[minix.git] / commands / cawf / bsfilt.c
blob214efda33fdb7d955b3cd73fbb9e86f0e3f26367
1 /*
2 * bsfilt.c - a colcrt-like processor for cawf(1)
3 */
5 /*
6 * Copyright (c) 1991 Purdue University Research Foundation,
7 * West Lafayette, Indiana 47907. All rights reserved.
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.
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:
17 * 1. The author is not responsible for any consequences of use of
18 * this software, even if they arise from flaws in it.
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.
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.
28 * 4. This notice may not be removed or altered.
31 #include <stdio.h>
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 */
43 #include <sys/types.h>
46 #define MAXLL 2048 /* ridiculous maximum line length */
48 int Dash = 1; /* underline with dashes */
49 int Dp = 0; /* dash pending */
50 int Lc = 0; /* line count */
51 char *Pname; /* program name */
52 unsigned char Ulb[MAXLL]; /* underline buffer */
53 int Ulx = 0; /* underline buffer index */
55 void Putchar(int ch);
57 main(argc, argv)
58 int argc;
59 char *argv[];
61 int ax = 1; /* argument index */
62 unsigned char c; /* character buffer */
63 FILE *fs; /* file stream */
64 int nf = 0; /* number of files processed */
65 unsigned char pc; /* previous character */
66 int under = 0; /* underline */
68 * Save program name.
70 if ((Pname = strrchr(argv[0], '/')) != NULL)
71 Pname++;
72 else if ((Pname = strrchr(argv[0], '\\')) != NULL)
73 Pname++;
74 else
75 Pname = argv[0];
77 * Process options.
79 if (argc > 1 && argv[1][0] == '-') {
80 switch (argv[1][1]) {
82 * "-U" - underline with dashes.
84 case 'U':
85 Dash = 0;
86 under = 1;
87 break;
89 * "-" - do no underlining at all.
91 case '\0':
92 Dash = under = 0;
93 break;
94 default:
95 (void) fprintf(stderr,
96 "%s usage: [-] [-U] [file]\n", Pname);
97 exit(1);
99 ax++;
102 * Process files. Read standard input if no files names.
105 while (ax < argc || nf == 0) {
106 if (ax >= argc)
107 fs = stdin;
108 else {
109 #ifdef UNIX
110 if ((fs = fopen(argv[ax], "r")) == NULL)
111 #else
112 if ((fs = fopen(argv[ax], "rt")) == NULL)
113 #endif
115 (void) fprintf(stderr, "%s: can't open %s\n",
116 Pname, argv[ax]);
117 exit(1);
119 ax++;
121 nf++;
123 * Read input a character at a time.
125 for (pc = '\0';;) {
126 c = (unsigned char)fgetc(fs);
127 if (feof(fs))
128 break;
129 switch(c) {
131 case '\n':
132 if (pc)
133 Putchar((int)pc);
134 Putchar('\n');
135 pc = '\0';
136 break;
138 case '\b':
139 if (pc == '_') {
140 if (under) {
141 putchar(pc);
142 putchar('\b');
143 } else if (Dash)
144 Dp = 1;
146 pc = '\0';
147 break;
149 default:
150 if (pc)
151 Putchar((int)pc);
152 pc = c;
155 if (pc) {
156 Putchar((int)pc);
157 Putchar((int)'\n');
160 exit(0);
165 * Putchar(ch) - put a character with possible underlining
168 void
169 Putchar(ch)
170 int ch;
172 int i; /* temporary index */
174 if ((unsigned char)ch == '\n') {
176 * Handle end of line.
178 putchar('\n');
179 if (Ulx) {
180 while (Ulx && Ulb[Ulx-1] == ' ')
181 Ulx--;
182 if (Ulx) {
183 for (i = 0; i < Ulx; i++)
184 putchar(Ulb[i]);
185 putchar('\n');
188 Dp = Ulx = 0;
189 Lc++;
190 return;
193 * Put "normal" character.
195 putchar((unsigned char)ch);
196 if (Dash) {
199 * Handle dash-type underlining.
201 if (Ulx >= MAXLL) {
202 (void) fprintf(stderr,
203 "%s: underline for line %d > %d characters\n",
204 Pname, Lc, MAXLL);
205 exit(1);
207 Ulb[Ulx++] = Dp ? '-' : ' ';
208 Dp = 0;