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