xref: /netbsd-src/usr.bin/vis/vis.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /*	$NetBSD: vis.c,v 1.7 2002/12/23 01:45:54 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.7 2002/12/23 01:45:54 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 char *extra;
58 
59 int foldit __P((char *, int, int));
60 int main __P((int, char **));
61 void process __P((FILE *, char *));
62 
63 int
64 main(argc, argv)
65 	int argc;
66 	char *argv[];
67 {
68 	FILE *fp;
69 	int ch;
70 	int rval;
71 
72 	while ((ch = getopt(argc, argv, "nwctsobe:fF:ld")) != -1)
73 		switch((char)ch) {
74 		case 'n':
75 			none++;
76 			break;
77 		case 'w':
78 			eflags |= VIS_WHITE;
79 			break;
80 		case 'c':
81 			eflags |= VIS_CSTYLE;
82 			break;
83 		case 't':
84 			eflags |= VIS_TAB;
85 			break;
86 		case 's':
87 			eflags |= VIS_SAFE;
88 			break;
89 		case 'o':
90 			eflags |= VIS_OCTAL;
91 			break;
92 		case 'b':
93 			eflags |= VIS_NOSLASH;
94 			break;
95 		case 'e':
96 			extra = optarg;
97 			break;
98 		case 'F':
99 			if ((foldwidth = atoi(optarg))<5) {
100 				errx(1, "can't fold lines to less than 5 cols");
101 				/* NOTREACHED */
102 			}
103 			/*FALLTHROUGH*/
104 		case 'f':
105 			fold++;		/* fold output lines to 80 cols */
106 			break;		/* using hidden newline */
107 		case 'l':
108 			markeol++;	/* mark end of line with \$ */
109 			break;
110 #ifdef DEBUG
111 		case 'd':
112 			debug++;
113 			break;
114 #endif
115 		case '?':
116 		default:
117 			fprintf(stderr,
118 		    "Usage: vis [-nwctsobf] [-e extra] [-F foldwidth]\n");
119 			exit(1);
120 		}
121 	argc -= optind;
122 	argv += optind;
123 
124 	rval = 0;
125 
126 	if (*argv)
127 		while (*argv) {
128 			if ((fp=fopen(*argv, "r")) != NULL) {
129 				process(fp, *argv);
130 				fclose(fp);
131 			} else {
132 				warn("%s", *argv);
133 				rval = 1;
134 			}
135 			argv++;
136 		}
137 	else
138 		process(stdin, "<stdin>");
139 	exit(rval);
140 }
141 
142 void
143 process(fp, filename)
144 	FILE *fp;
145 	char *filename;
146 {
147 	static int col = 0;
148 	char *cp = "\0"+1;	/* so *(cp-1) starts out != '\n' */
149 	int c, rachar;
150 	char buff[5];
151 
152 	c = getc(fp);
153 	while (c != EOF) {
154 		rachar = getc(fp);
155 		if (none) {
156 			cp = buff;
157 			*cp++ = c;
158 			if (c == '\\')
159 				*cp++ = '\\';
160 			*cp = '\0';
161 		} else if (markeol && c == '\n') {
162 			cp = buff;
163 			if ((eflags & VIS_NOSLASH) == 0)
164 				*cp++ = '\\';
165 			*cp++ = '$';
166 			*cp++ = '\n';
167 			*cp = '\0';
168 		} else if (extra)
169 			(void) svis(buff, (char)c, eflags, (char)rachar, extra);
170 		else
171 			(void) vis(buff, (char)c, eflags, (char)rachar);
172 
173 		cp = buff;
174 		if (fold) {
175 #ifdef DEBUG
176 			if (debug)
177 				printf("<%02d,", col);
178 #endif
179 			col = foldit(cp, col, foldwidth);
180 #ifdef DEBUG
181 			if (debug)
182 				printf("%02d>", col);
183 #endif
184 		}
185 		do {
186 			putchar(*cp);
187 		} while (*++cp);
188 		c = rachar;
189 	}
190 	/*
191 	 * terminate partial line with a hidden newline
192 	 */
193 	if (fold && *(cp-1) != '\n')
194 		printf("\\\n");
195 }
196