xref: /netbsd-src/usr.bin/vis/vis.c (revision 201fac6e3d8ac6a5503c4810b0378f73101d50c3)
1 /*	$NetBSD: vis.c,v 1.3 1994/11/17 07:56:01 jtc 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 #ifndef lint
37 static char copyright[] =
38 "@(#) 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 static char rcsid[] = "$NetBSD: vis.c,v 1.3 1994/11/17 07:56:01 jtc Exp $";
47 #endif /* not lint */
48 
49 #include <stdio.h>
50 #include <vis.h>
51 
52 int eflags, fold, foldwidth=80, none, markeol, debug;
53 
54 main(argc, argv)
55 	char *argv[];
56 {
57 	extern char *optarg;
58 	extern int optind;
59 	extern int errno;
60 	FILE *fp;
61 	int ch;
62 
63 	while ((ch = getopt(argc, argv, "nwctsobfF:ld")) != EOF)
64 		switch((char)ch) {
65 		case 'n':
66 			none++;
67 			break;
68 		case 'w':
69 			eflags |= VIS_WHITE;
70 			break;
71 		case 'c':
72 			eflags |= VIS_CSTYLE;
73 			break;
74 		case 't':
75 			eflags |= VIS_TAB;
76 			break;
77 		case 's':
78 			eflags |= VIS_SAFE;
79 			break;
80 		case 'o':
81 			eflags |= VIS_OCTAL;
82 			break;
83 		case 'b':
84 			eflags |= VIS_NOSLASH;
85 			break;
86 		case 'F':
87 			if ((foldwidth = atoi(optarg))<5) {
88 				fprintf(stderr,
89 				 "vis: can't fold lines to less than 5 cols\n");
90 				exit(1);
91 			}
92 			/*FALLTHROUGH*/
93 		case 'f':
94 			fold++;		/* fold output lines to 80 cols */
95 			break;		/* using hidden newline */
96 		case 'l':
97 			markeol++;	/* mark end of line with \$ */
98 			break;
99 #ifdef DEBUG
100 		case 'd':
101 			debug++;
102 			break;
103 #endif
104 		case '?':
105 		default:
106 			fprintf(stderr,
107 		"usage: vis [-nwctsobf] [-F foldwidth]\n");
108 			exit(1);
109 		}
110 	argc -= optind;
111 	argv += optind;
112 
113 	if (*argv)
114 		while (*argv) {
115 			if ((fp=fopen(*argv, "r")) != NULL)
116 				process(fp, *argv);
117 			else
118 				fprintf(stderr, "vis: %s: %s\n", *argv,
119 				    (char *)strerror(errno));
120 			argv++;
121 		}
122 	else
123 		process(stdin, "<stdin>");
124 	exit(0);
125 }
126 
127 process(fp, filename)
128 	FILE *fp;
129 	char *filename;
130 {
131 	static int col = 0;
132 	register char *cp = "\0"+1;	/* so *(cp-1) starts out != '\n' */
133 	register int c, rachar;
134 	register char nc;
135 	char buff[5];
136 
137 	c = getc(fp);
138 	while (c != EOF) {
139 		rachar = getc(fp);
140 		if (none) {
141 			cp = buff;
142 			*cp++ = c;
143 			if (c == '\\')
144 				*cp++ = '\\';
145 			*cp = '\0';
146 		} else if (markeol && c == '\n') {
147 			cp = buff;
148 			if ((eflags & VIS_NOSLASH) == 0)
149 				*cp++ = '\\';
150 			*cp++ = '$';
151 			*cp++ = '\n';
152 			*cp = '\0';
153 		} else
154 			(void) vis(buff, (char)c, eflags, (char)rachar);
155 
156 		cp = buff;
157 		if (fold) {
158 #ifdef DEBUG
159 			if (debug)
160 				printf("<%02d,", col);
161 #endif
162 			col = foldit(cp, col, foldwidth);
163 #ifdef DEBUG
164 			if (debug)
165 				printf("%02d>", col);
166 #endif
167 		}
168 		do {
169 			putchar(*cp);
170 		} while (*++cp);
171 		c = rachar;
172 	}
173 	/*
174 	 * terminate partial line with a hidden newline
175 	 */
176 	if (fold && *(cp-1) != '\n')
177 		printf("\\\n");
178 }
179