vis.c revision 1.18 1 /* $NetBSD: vis.c,v 1.18 2013/02/13 22:24:48 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.18 2013/02/13 22:24:48 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 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 wchar_t ibuff[3]; /* room for c + rachar + NUL */
165 char mbibuff[13]; /* ((sizeof(ibuff) - 1) * MB_LEN_MAX) + NUL */
166 char buff[5]; /* max vis-encoding length for one char + NUL */
167
168 c = getwc(fp);
169 if (c == WEOF && errno == EILSEQ)
170 c = (wint_t)getc(fp);
171 while (c != WEOF) {
172 rachar = getwc(fp);
173 if (rachar == WEOF && errno == EILSEQ)
174 rachar = (wint_t)getc(fp);
175 if (none) {
176 cp = buff;
177 *cp++ = c;
178 if (c == '\\')
179 *cp++ = '\\';
180 *cp = '\0';
181 } else if (markeol && c == '\n') {
182 cp = buff;
183 if ((eflags & VIS_NOSLASH) == 0)
184 *cp++ = '\\';
185 *cp++ = '$';
186 *cp++ = '\n';
187 *cp = '\0';
188 } else {
189 c1 = rachar;
190 if (c1 == WEOF)
191 c1 = L'\0';
192 swprintf(ibuff, 3, L"%lc%lc", c, c1);
193 wcstombs(mbibuff, ibuff,
194 (wcslen(ibuff) * MB_LEN_MAX) + 1);
195 (void) strsvisx(buff, mbibuff, 1, eflags, extra);
196 }
197
198 cp = buff;
199 if (fold) {
200 #ifdef DEBUG
201 if (debug)
202 (void)printf("<%02d,", col);
203 #endif
204 col = foldit(cp, col, foldwidth, eflags);
205 #ifdef DEBUG
206 if (debug)
207 (void)printf("%02d>", col);
208 #endif
209 }
210 do {
211 (void)putchar(*cp);
212 } while (*++cp);
213 c = rachar;
214 }
215 /*
216 * terminate partial line with a hidden newline
217 */
218 if (fold && *(cp - 1) != '\n')
219 (void)printf(eflags & VIS_MIMESTYLE ? "=\n" : "\\\n");
220 }
221