xref: /openbsd-src/usr.bin/vis/vis.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: vis.c,v 1.11 2007/03/15 23:17:20 jmc Exp $	*/
2 /*	$NetBSD: vis.c,v 1.4 1994/12/20 16:13:03 jtc Exp $	*/
3 
4 /*-
5  * Copyright (c) 1989, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef lint
34 static const char copyright[] =
35 "@(#) Copyright (c) 1989, 1993\n\
36 	The Regents of the University of California.  All rights reserved.\n";
37 #endif /* not lint */
38 
39 #ifndef lint
40 #if 0
41 static const char sccsid[] = "@(#)vis.c	8.1 (Berkeley) 6/6/93";
42 #endif
43 static const char rcsid[] = "$OpenBSD: vis.c,v 1.11 2007/03/15 23:17:20 jmc Exp $";
44 #endif /* not lint */
45 
46 #include <stdio.h>
47 #include <string.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50 #include <err.h>
51 #include <vis.h>
52 
53 int eflags, fold, foldwidth=80, none, markeol;
54 
55 #ifdef DEBUG
56 int debug;
57 #endif
58 
59 int foldit(char *, int, int);
60 void process(FILE *);
61 __dead void usage(void);
62 
63 int
64 main(int argc, char *argv[])
65 {
66 	FILE *fp;
67 	int ch;
68 
69 	while ((ch = getopt(argc, argv, "nwctsobfF:ld")) != -1)
70 		switch((char)ch) {
71 		case 'n':
72 			none++;
73 			break;
74 		case 'w':
75 			eflags |= VIS_WHITE;
76 			break;
77 		case 'c':
78 			eflags |= VIS_CSTYLE;
79 			break;
80 		case 't':
81 			eflags |= VIS_TAB;
82 			break;
83 		case 's':
84 			eflags |= VIS_SAFE;
85 			break;
86 		case 'o':
87 			eflags |= VIS_OCTAL;
88 			break;
89 		case 'b':
90 			eflags |= VIS_NOSLASH;
91 			break;
92 		case 'F':
93 			if ((foldwidth = atoi(optarg))<5) {
94 				errx(1, "can't fold lines to less than 5 cols");
95 				/* NOTREACHED */
96 			}
97 			/*FALLTHROUGH*/
98 		case 'f':
99 			fold++;		/* fold output lines to 80 cols */
100 			break;		/* using hidden newline */
101 		case 'l':
102 			markeol++;	/* mark end of line with \$ */
103 			break;
104 #ifdef DEBUG
105 		case 'd':
106 			debug++;
107 			break;
108 #endif
109 		case '?':
110 		default:
111 			usage();
112 		}
113 	argc -= optind;
114 	argv += optind;
115 
116 	if (*argv)
117 		while (*argv) {
118 			if ((fp=fopen(*argv, "r")) != NULL)
119 				process(fp);
120 			else
121 				warn("%s", *argv);
122 			argv++;
123 		}
124 	else
125 		process(stdin);
126 	exit(0);
127 }
128 
129 void
130 process(FILE *fp)
131 {
132 	static int col = 0;
133 	char *cp = "\0"+1;	/* so *(cp-1) starts out != '\n' */
134 	int c, rachar;
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 
180 __dead void
181 usage(void)
182 {
183 	extern char *__progname;
184 
185 	fprintf(stderr, "usage: %s [-bcflnostw] [-F foldwidth] [file ...]\n",
186 	    __progname);
187 	exit(1);
188 }
189