vis.c revision 1.20 1 /* $NetBSD: vis.c,v 1.20 2013/02/14 14:00:01 christos 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\
35 The Regents of the University of California. All rights reserved.");
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.20 2013/02/14 14:00:01 christos Exp $");
43 #endif /* not lint */
44
45 #include <stdio.h>
46 #include <string.h>
47 #include <stdlib.h>
48 #include <errno.h>
49 #include <wchar.h>
50 #include <limits.h>
51 #include <unistd.h>
52 #include <err.h>
53 #include <vis.h>
54
55 #include "extern.h"
56
57 static int eflags, fold, foldwidth = 80, none, markeol;
58 #ifdef DEBUG
59 int debug;
60 #endif
61 static const char *extra = "";
62
63 static void process(FILE *);
64
65 int
66 main(int argc, char *argv[])
67 {
68 FILE *fp;
69 int ch;
70 int rval;
71
72 while ((ch = getopt(argc, argv, "bcde:F:fhlmnostw")) != -1)
73 switch((char)ch) {
74 case 'b':
75 eflags |= VIS_NOSLASH;
76 break;
77 case 'c':
78 eflags |= VIS_CSTYLE;
79 break;
80 #ifdef DEBUG
81 case 'd':
82 debug++;
83 break;
84 #endif
85 case 'e':
86 extra = optarg;
87 break;
88 case 'F':
89 if ((foldwidth = atoi(optarg)) < 5) {
90 errx(1, "can't fold lines to less than 5 cols");
91 /* NOTREACHED */
92 }
93 markeol++;
94 break;
95 case 'f':
96 fold++; /* fold output lines to 80 cols */
97 break; /* using hidden newline */
98 case 'h':
99 eflags |= VIS_HTTPSTYLE;
100 break;
101 case 'l':
102 markeol++; /* mark end of line with \$ */
103 break;
104 case 'm':
105 eflags |= VIS_MIMESTYLE;
106 if (foldwidth == 80)
107 foldwidth = 76;
108 break;
109 case 'n':
110 none++;
111 break;
112 case 'o':
113 eflags |= VIS_OCTAL;
114 break;
115 case 's':
116 eflags |= VIS_SAFE;
117 break;
118 case 't':
119 eflags |= VIS_TAB;
120 break;
121 case 'w':
122 eflags |= VIS_WHITE;
123 break;
124 case '?':
125 default:
126 (void)fprintf(stderr,
127 "Usage: %s [-bcfhlmnostw] [-e extra]"
128 " [-F foldwidth] [file ...]\n", getprogname());
129 return 1;
130 }
131
132 if ((eflags & (VIS_HTTPSTYLE|VIS_MIMESTYLE)) ==
133 (VIS_HTTPSTYLE|VIS_MIMESTYLE))
134 errx(1, "Can't specify -m and -h at the same time");
135
136 argc -= optind;
137 argv += optind;
138
139 rval = 0;
140
141 if (*argv)
142 while (*argv) {
143 if ((fp = fopen(*argv, "r")) != NULL) {
144 process(fp);
145 (void)fclose(fp);
146 } else {
147 warn("%s", *argv);
148 rval = 1;
149 }
150 argv++;
151 }
152 else
153 process(stdin);
154 return rval;
155 }
156
157 static void
158 process(FILE *fp)
159 {
160 static int col = 0;
161 static char nul[] = "\0";
162 char *cp = nul + 1; /* so *(cp-1) starts out != '\n' */
163 wint_t c, c1, rachar;
164 char mbibuff[13]; /* ((sizeof(ibuff) - 1) * MB_LEN_MAX) + NUL */
165 char buff[5]; /* max vis-encoding length for one char + NUL */
166 int mbilen, cerr = 0, raerr = 0;
167
168 c = getwc(fp);
169 if (c == WEOF && errno == EILSEQ) {
170 c = (wint_t)getc(fp);
171 cerr = 1;
172 }
173 while (c != WEOF) {
174 rachar = getwc(fp);
175 if (rachar == WEOF && errno == EILSEQ) {
176 rachar = (wint_t)getc(fp);
177 raerr = 1;
178 }
179 if (none) {
180 cp = buff;
181 *cp++ = c;
182 if (c == '\\')
183 *cp++ = '\\';
184 *cp = '\0';
185 } else if (markeol && c == '\n') {
186 cp = buff;
187 if ((eflags & VIS_NOSLASH) == 0)
188 *cp++ = '\\';
189 *cp++ = '$';
190 *cp++ = '\n';
191 *cp = '\0';
192 } else {
193 c1 = rachar;
194 if (c1 == WEOF)
195 c1 = L'\0';
196 if (cerr) {
197 *mbibuff = c;
198 mbilen = 1;
199 } else
200 mbilen = wctomb(mbibuff, c);
201 if (raerr)
202 mbibuff[mbilen] = c1;
203 else
204 wctomb(mbibuff + mbilen, c1);
205 (void)strsvisx(buff, mbibuff, mbilen, eflags, extra);
206 }
207
208 cp = buff;
209 if (fold) {
210 #ifdef DEBUG
211 if (debug)
212 (void)printf("<%02d,", col);
213 #endif
214 col = foldit(cp, col, foldwidth, eflags);
215 #ifdef DEBUG
216 if (debug)
217 (void)printf("%02d>", col);
218 #endif
219 }
220 do {
221 (void)putchar(*cp);
222 } while (*++cp);
223 c = rachar;
224 cerr = raerr;
225 raerr = 0;
226 }
227 /*
228 * terminate partial line with a hidden newline
229 */
230 if (fold && *(cp - 1) != '\n')
231 (void)printf(eflags & VIS_MIMESTYLE ? "=\n" : "\\\n");
232 }
233