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