xref: /minix3/minix/commands/cawf/bsfilt.c (revision b5e2faaaaf60a8b9a02f8d72f64caa56a87eb312)
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 
46 #define MAXLL	2048			/* ridiculous maximum line length */
47 
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 */
54 
55 void Putchar(int ch);
56 
57 int main(int argc, char *argv[]) {
58 	int ax = 1;			/* argument index */
59 	unsigned char c;		/* character buffer */
60 	FILE *fs;			/* file stream */
61 	int nf = 0;			/* number of files processed */
62 	unsigned char pc;		/* previous character */
63 	int under = 0;                  /* underline */
64 /*
65  * Save program name.
66  */
67 	if ((Pname = strrchr(argv[0], '/')) != NULL)
68 		Pname++;
69 	else if ((Pname = strrchr(argv[0], '\\')) != NULL)
70 		Pname++;
71 	else
72 		Pname = argv[0];
73 /*
74  * Process options.
75  */
76 	if (argc > 1 && argv[1][0] == '-') {
77 		switch (argv[1][1]) {
78 	/*
79 	 * "-U" - underline with dashes.
80 	 */
81 		case 'U':
82 			Dash = 0;
83 			under = 1;
84 			break;
85 	/*
86 	 * "-" - do no  underlining at all.
87 	 */
88 		case '\0':
89 			Dash = under = 0;
90 			break;
91 		default:
92 			(void) fprintf(stderr,
93 				"%s usage: [-] [-U] [file]\n", Pname);
94 			exit(1);
95 		}
96 		ax++;
97 	}
98 /*
99  * Process files.  Read standard input if no files names.
100  */
101 
102 	while (ax < argc || nf == 0) {
103 		if (ax >= argc)
104 			fs = stdin;
105 		else {
106 #ifdef	UNIX
107 			if ((fs = fopen(argv[ax], "r")) == NULL)
108 #else
109 			if ((fs = fopen(argv[ax], "rt")) == NULL)
110 #endif
111 			{
112 				(void) fprintf(stderr, "%s: can't open %s\n",
113 					Pname, argv[ax]);
114 				exit(1);
115 			}
116 			ax++;
117 		}
118 		nf++;
119 	/*
120 	 * Read input a character at a time.
121 	 */
122 		for (pc = '\0';;) {
123 			c = (unsigned char)fgetc(fs);
124 			if (feof(fs))
125 				break;
126 			switch(c) {
127 
128 			case '\n':
129 				if (pc)
130 					Putchar((int)pc);
131 				Putchar('\n');
132 				pc = '\0';
133 				break;
134 
135 			case '\b':
136 				if (pc == '_') {
137 					if (under) {
138 						putchar(pc);
139 						putchar('\b');
140 					} else if (Dash)
141 						Dp = 1;
142 				}
143 				pc = '\0';
144 				break;
145 
146 			default:
147 				if (pc)
148 					Putchar((int)pc);
149 				pc = c;
150 			}
151 		}
152 		if (pc) {
153 			Putchar((int)pc);
154 			Putchar((int)'\n');
155 		}
156 	}
157 	exit(0);
158 }
159 
160 
161 /*
162  * Putchar(ch) - put a character with possible underlining
163  */
164 
165 void Putchar(int ch) {
166 	int i;					/* temporary index */
167 
168 	if ((unsigned char)ch == '\n') {
169 /*
170  * Handle end of line.
171  */
172 		putchar('\n');
173 		if (Ulx) {
174 			while (Ulx && Ulb[Ulx-1] == ' ')
175 				Ulx--;
176 			if (Ulx) {
177 				for (i = 0; i < Ulx; i++)
178 					putchar(Ulb[i]);
179 				putchar('\n');
180 			}
181 		}
182 		Dp = Ulx = 0;
183 		Lc++;
184 		return;
185 	}
186 /*
187  * Put "normal" character.
188  */
189 	putchar((unsigned char)ch);
190 	if (Dash) {
191 
192 	/*
193 	 * Handle dash-type underlining.
194 	 */
195 		if (Ulx >= MAXLL) {
196 			(void) fprintf(stderr,
197 				"%s: underline for line %d > %d characters\n",
198 				Pname, Lc, MAXLL);
199 			exit(1);
200 		}
201 		Ulb[Ulx++] = Dp ? '-' : ' ';
202 		Dp = 0;
203 	}
204 }
205