xref: /netbsd-src/usr.bin/vis/vis.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: vis.c,v 1.5 1997/10/20 03:06:48 lukem Exp $	*/
2 
3 /*-
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
39 	The Regents of the University of California.  All rights reserved.\n");
40 #endif /* not lint */
41 
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)vis.c	8.1 (Berkeley) 6/6/93";
45 #endif
46 __RCSID("$NetBSD: vis.c,v 1.5 1997/10/20 03:06:48 lukem Exp $");
47 #endif /* not lint */
48 
49 #include <stdio.h>
50 #include <string.h>
51 #include <stdlib.h>
52 #include <unistd.h>
53 #include <err.h>
54 #include <vis.h>
55 
56 int eflags, fold, foldwidth=80, none, markeol, debug;
57 
58 int foldit __P((char *, int, int));
59 int main __P((int, char **));
60 void process __P((FILE *, char *));
61 
62 int
63 main(argc, argv)
64 	int argc;
65 	char *argv[];
66 {
67 	FILE *fp;
68 	int ch;
69 
70 	while ((ch = getopt(argc, argv, "nwctsobfF:ld")) != -1)
71 		switch((char)ch) {
72 		case 'n':
73 			none++;
74 			break;
75 		case 'w':
76 			eflags |= VIS_WHITE;
77 			break;
78 		case 'c':
79 			eflags |= VIS_CSTYLE;
80 			break;
81 		case 't':
82 			eflags |= VIS_TAB;
83 			break;
84 		case 's':
85 			eflags |= VIS_SAFE;
86 			break;
87 		case 'o':
88 			eflags |= VIS_OCTAL;
89 			break;
90 		case 'b':
91 			eflags |= VIS_NOSLASH;
92 			break;
93 		case 'F':
94 			if ((foldwidth = atoi(optarg))<5) {
95 				errx(1, "can't fold lines to less than 5 cols");
96 				/* NOTREACHED */
97 			}
98 			/*FALLTHROUGH*/
99 		case 'f':
100 			fold++;		/* fold output lines to 80 cols */
101 			break;		/* using hidden newline */
102 		case 'l':
103 			markeol++;	/* mark end of line with \$ */
104 			break;
105 #ifdef DEBUG
106 		case 'd':
107 			debug++;
108 			break;
109 #endif
110 		case '?':
111 		default:
112 			fprintf(stderr,
113 		"usage: vis [-nwctsobf] [-F foldwidth]\n");
114 			exit(1);
115 		}
116 	argc -= optind;
117 	argv += optind;
118 
119 	if (*argv)
120 		while (*argv) {
121 			if ((fp=fopen(*argv, "r")) != NULL)
122 				process(fp, *argv);
123 			else
124 				warn("%s", *argv);
125 			argv++;
126 		}
127 	else
128 		process(stdin, "<stdin>");
129 	exit(0);
130 }
131 
132 void
133 process(fp, filename)
134 	FILE *fp;
135 	char *filename;
136 {
137 	static int col = 0;
138 	char *cp = "\0"+1;	/* so *(cp-1) starts out != '\n' */
139 	int c, rachar;
140 	char buff[5];
141 
142 	c = getc(fp);
143 	while (c != EOF) {
144 		rachar = getc(fp);
145 		if (none) {
146 			cp = buff;
147 			*cp++ = c;
148 			if (c == '\\')
149 				*cp++ = '\\';
150 			*cp = '\0';
151 		} else if (markeol && c == '\n') {
152 			cp = buff;
153 			if ((eflags & VIS_NOSLASH) == 0)
154 				*cp++ = '\\';
155 			*cp++ = '$';
156 			*cp++ = '\n';
157 			*cp = '\0';
158 		} else
159 			(void) vis(buff, (char)c, eflags, (char)rachar);
160 
161 		cp = buff;
162 		if (fold) {
163 #ifdef DEBUG
164 			if (debug)
165 				printf("<%02d,", col);
166 #endif
167 			col = foldit(cp, col, foldwidth);
168 #ifdef DEBUG
169 			if (debug)
170 				printf("%02d>", col);
171 #endif
172 		}
173 		do {
174 			putchar(*cp);
175 		} while (*++cp);
176 		c = rachar;
177 	}
178 	/*
179 	 * terminate partial line with a hidden newline
180 	 */
181 	if (fold && *(cp-1) != '\n')
182 		printf("\\\n");
183 }
184