1433d6423SLionel Sambuc /*
2433d6423SLionel Sambuc * bsfilt.c - a colcrt-like processor for cawf(1)
3433d6423SLionel Sambuc */
4433d6423SLionel Sambuc
5433d6423SLionel Sambuc /*
6433d6423SLionel Sambuc * Copyright (c) 1991 Purdue University Research Foundation,
7433d6423SLionel Sambuc * West Lafayette, Indiana 47907. All rights reserved.
8433d6423SLionel Sambuc *
9433d6423SLionel Sambuc * Written by Victor A. Abell <abe@mace.cc.purdue.edu>, Purdue
10433d6423SLionel Sambuc * University Computing Center. Not derived from licensed software;
11433d6423SLionel Sambuc * derived from awf(1) by Henry Spencer of the University of Toronto.
12433d6423SLionel Sambuc *
13433d6423SLionel Sambuc * Permission is granted to anyone to use this software for any
14433d6423SLionel Sambuc * purpose on any computer system, and to alter it and redistribute
15433d6423SLionel Sambuc * it freely, subject to the following restrictions:
16433d6423SLionel Sambuc *
17433d6423SLionel Sambuc * 1. The author is not responsible for any consequences of use of
18433d6423SLionel Sambuc * this software, even if they arise from flaws in it.
19433d6423SLionel Sambuc *
20433d6423SLionel Sambuc * 2. The origin of this software must not be misrepresented, either
21433d6423SLionel Sambuc * by explicit claim or by omission. Credits must appear in the
22433d6423SLionel Sambuc * documentation.
23433d6423SLionel Sambuc *
24433d6423SLionel Sambuc * 3. Altered versions must be plainly marked as such, and must not
25433d6423SLionel Sambuc * be misrepresented as being the original software. Credits must
26433d6423SLionel Sambuc * appear in the documentation.
27433d6423SLionel Sambuc *
28433d6423SLionel Sambuc * 4. This notice may not be removed or altered.
29433d6423SLionel Sambuc */
30433d6423SLionel Sambuc
31*d0055759SDavid van Moolenbroek #include <stdlib.h>
32433d6423SLionel Sambuc #include <stdio.h>
33433d6423SLionel Sambuc
34433d6423SLionel Sambuc #ifdef UNIX
35433d6423SLionel Sambuc # ifdef USG
36433d6423SLionel Sambuc #include <string.h>
37433d6423SLionel Sambuc # else /* not USG */
38433d6423SLionel Sambuc #include <strings.h>
39433d6423SLionel Sambuc # endif /* USG */
40433d6423SLionel Sambuc #else /* not UNIX */
41433d6423SLionel Sambuc #include <string.h>
42433d6423SLionel Sambuc #endif /* UNIX */
43433d6423SLionel Sambuc
44433d6423SLionel Sambuc #include <sys/types.h>
45433d6423SLionel Sambuc
46433d6423SLionel Sambuc
47433d6423SLionel Sambuc #define MAXLL 2048 /* ridiculous maximum line length */
48433d6423SLionel Sambuc
49433d6423SLionel Sambuc int Dash = 1; /* underline with dashes */
50433d6423SLionel Sambuc int Dp = 0; /* dash pending */
51433d6423SLionel Sambuc int Lc = 0; /* line count */
52433d6423SLionel Sambuc char *Pname; /* program name */
53433d6423SLionel Sambuc unsigned char Ulb[MAXLL]; /* underline buffer */
54433d6423SLionel Sambuc int Ulx = 0; /* underline buffer index */
55433d6423SLionel Sambuc
56433d6423SLionel Sambuc void Putchar(int ch);
57433d6423SLionel Sambuc
main(int argc,char * argv[])58d9494baaSJacob Adams int main(int argc, char *argv[]) {
59433d6423SLionel Sambuc int ax = 1; /* argument index */
60433d6423SLionel Sambuc unsigned char c; /* character buffer */
61433d6423SLionel Sambuc FILE *fs; /* file stream */
62433d6423SLionel Sambuc int nf = 0; /* number of files processed */
63433d6423SLionel Sambuc unsigned char pc; /* previous character */
64433d6423SLionel Sambuc int under = 0; /* underline */
65433d6423SLionel Sambuc /*
66433d6423SLionel Sambuc * Save program name.
67433d6423SLionel Sambuc */
68433d6423SLionel Sambuc if ((Pname = strrchr(argv[0], '/')) != NULL)
69433d6423SLionel Sambuc Pname++;
70433d6423SLionel Sambuc else if ((Pname = strrchr(argv[0], '\\')) != NULL)
71433d6423SLionel Sambuc Pname++;
72433d6423SLionel Sambuc else
73433d6423SLionel Sambuc Pname = argv[0];
74433d6423SLionel Sambuc /*
75433d6423SLionel Sambuc * Process options.
76433d6423SLionel Sambuc */
77433d6423SLionel Sambuc if (argc > 1 && argv[1][0] == '-') {
78433d6423SLionel Sambuc switch (argv[1][1]) {
79433d6423SLionel Sambuc /*
80433d6423SLionel Sambuc * "-U" - underline with dashes.
81433d6423SLionel Sambuc */
82433d6423SLionel Sambuc case 'U':
83433d6423SLionel Sambuc Dash = 0;
84433d6423SLionel Sambuc under = 1;
85433d6423SLionel Sambuc break;
86433d6423SLionel Sambuc /*
87433d6423SLionel Sambuc * "-" - do no underlining at all.
88433d6423SLionel Sambuc */
89433d6423SLionel Sambuc case '\0':
90433d6423SLionel Sambuc Dash = under = 0;
91433d6423SLionel Sambuc break;
92433d6423SLionel Sambuc default:
93433d6423SLionel Sambuc (void) fprintf(stderr,
94433d6423SLionel Sambuc "%s usage: [-] [-U] [file]\n", Pname);
95433d6423SLionel Sambuc exit(1);
96433d6423SLionel Sambuc }
97433d6423SLionel Sambuc ax++;
98433d6423SLionel Sambuc }
99433d6423SLionel Sambuc /*
100433d6423SLionel Sambuc * Process files. Read standard input if no files names.
101433d6423SLionel Sambuc */
102433d6423SLionel Sambuc
103433d6423SLionel Sambuc while (ax < argc || nf == 0) {
104433d6423SLionel Sambuc if (ax >= argc)
105433d6423SLionel Sambuc fs = stdin;
106433d6423SLionel Sambuc else {
107433d6423SLionel Sambuc #ifdef UNIX
108433d6423SLionel Sambuc if ((fs = fopen(argv[ax], "r")) == NULL)
109433d6423SLionel Sambuc #else
110433d6423SLionel Sambuc if ((fs = fopen(argv[ax], "rt")) == NULL)
111433d6423SLionel Sambuc #endif
112433d6423SLionel Sambuc {
113433d6423SLionel Sambuc (void) fprintf(stderr, "%s: can't open %s\n",
114433d6423SLionel Sambuc Pname, argv[ax]);
115433d6423SLionel Sambuc exit(1);
116433d6423SLionel Sambuc }
117433d6423SLionel Sambuc ax++;
118433d6423SLionel Sambuc }
119433d6423SLionel Sambuc nf++;
120433d6423SLionel Sambuc /*
121433d6423SLionel Sambuc * Read input a character at a time.
122433d6423SLionel Sambuc */
123433d6423SLionel Sambuc for (pc = '\0';;) {
124433d6423SLionel Sambuc c = (unsigned char)fgetc(fs);
125433d6423SLionel Sambuc if (feof(fs))
126433d6423SLionel Sambuc break;
127433d6423SLionel Sambuc switch(c) {
128433d6423SLionel Sambuc
129433d6423SLionel Sambuc case '\n':
130433d6423SLionel Sambuc if (pc)
131433d6423SLionel Sambuc Putchar((int)pc);
132433d6423SLionel Sambuc Putchar('\n');
133433d6423SLionel Sambuc pc = '\0';
134433d6423SLionel Sambuc break;
135433d6423SLionel Sambuc
136433d6423SLionel Sambuc case '\b':
137433d6423SLionel Sambuc if (pc == '_') {
138433d6423SLionel Sambuc if (under) {
139433d6423SLionel Sambuc putchar(pc);
140433d6423SLionel Sambuc putchar('\b');
141433d6423SLionel Sambuc } else if (Dash)
142433d6423SLionel Sambuc Dp = 1;
143433d6423SLionel Sambuc }
144433d6423SLionel Sambuc pc = '\0';
145433d6423SLionel Sambuc break;
146433d6423SLionel Sambuc
147433d6423SLionel Sambuc default:
148433d6423SLionel Sambuc if (pc)
149433d6423SLionel Sambuc Putchar((int)pc);
150433d6423SLionel Sambuc pc = c;
151433d6423SLionel Sambuc }
152433d6423SLionel Sambuc }
153433d6423SLionel Sambuc if (pc) {
154433d6423SLionel Sambuc Putchar((int)pc);
155433d6423SLionel Sambuc Putchar((int)'\n');
156433d6423SLionel Sambuc }
157433d6423SLionel Sambuc }
158433d6423SLionel Sambuc exit(0);
159433d6423SLionel Sambuc }
160433d6423SLionel Sambuc
161433d6423SLionel Sambuc
162433d6423SLionel Sambuc /*
163433d6423SLionel Sambuc * Putchar(ch) - put a character with possible underlining
164433d6423SLionel Sambuc */
165433d6423SLionel Sambuc
Putchar(int ch)166d9494baaSJacob Adams void Putchar(int ch) {
167433d6423SLionel Sambuc int i; /* temporary index */
168433d6423SLionel Sambuc
169433d6423SLionel Sambuc if ((unsigned char)ch == '\n') {
170433d6423SLionel Sambuc /*
171433d6423SLionel Sambuc * Handle end of line.
172433d6423SLionel Sambuc */
173433d6423SLionel Sambuc putchar('\n');
174433d6423SLionel Sambuc if (Ulx) {
175433d6423SLionel Sambuc while (Ulx && Ulb[Ulx-1] == ' ')
176433d6423SLionel Sambuc Ulx--;
177433d6423SLionel Sambuc if (Ulx) {
178433d6423SLionel Sambuc for (i = 0; i < Ulx; i++)
179433d6423SLionel Sambuc putchar(Ulb[i]);
180433d6423SLionel Sambuc putchar('\n');
181433d6423SLionel Sambuc }
182433d6423SLionel Sambuc }
183433d6423SLionel Sambuc Dp = Ulx = 0;
184433d6423SLionel Sambuc Lc++;
185433d6423SLionel Sambuc return;
186433d6423SLionel Sambuc }
187433d6423SLionel Sambuc /*
188433d6423SLionel Sambuc * Put "normal" character.
189433d6423SLionel Sambuc */
190433d6423SLionel Sambuc putchar((unsigned char)ch);
191433d6423SLionel Sambuc if (Dash) {
192433d6423SLionel Sambuc
193433d6423SLionel Sambuc /*
194433d6423SLionel Sambuc * Handle dash-type underlining.
195433d6423SLionel Sambuc */
196433d6423SLionel Sambuc if (Ulx >= MAXLL) {
197433d6423SLionel Sambuc (void) fprintf(stderr,
198433d6423SLionel Sambuc "%s: underline for line %d > %d characters\n",
199433d6423SLionel Sambuc Pname, Lc, MAXLL);
200433d6423SLionel Sambuc exit(1);
201433d6423SLionel Sambuc }
202433d6423SLionel Sambuc Ulb[Ulx++] = Dp ? '-' : ' ';
203433d6423SLionel Sambuc Dp = 0;
204433d6423SLionel Sambuc }
205433d6423SLionel Sambuc }
206