Remove building with NOCRYPTO option
[minix3.git] / usr.bin / vis / vis.c
blobf20cdfc5e10bde31fd138bff15a2ca9555b63abe
1 /* $NetBSD: vis.c,v 1.25 2015/05/24 19:42:39 christos Exp $ */
3 /*-
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
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.
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.
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 */
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.25 2015/05/24 19:42:39 christos Exp $");
43 #endif /* not lint */
45 #include <stdio.h>
46 #include <string.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <errno.h>
50 #include <wchar.h>
51 #include <limits.h>
52 #include <unistd.h>
53 #include <err.h>
54 #include <vis.h>
56 #include "extern.h"
58 static int eflags, fold, foldwidth = 80, none, markeol;
59 #ifdef DEBUG
60 int debug;
61 #endif
62 static const char *extra = "";
64 static void process(FILE *);
66 int
67 main(int argc, char *argv[])
69 FILE *fp;
70 int ch;
71 int rval;
73 while ((ch = getopt(argc, argv, "bcde:F:fhlMmNnoSstw")) != -1)
74 switch((char)ch) {
75 case 'b':
76 eflags |= VIS_NOSLASH;
77 break;
78 case 'c':
79 eflags |= VIS_CSTYLE;
80 break;
81 #ifdef DEBUG
82 case 'd':
83 debug++;
84 break;
85 #endif
86 case 'e':
87 extra = optarg;
88 break;
89 case 'F':
90 if ((foldwidth = atoi(optarg)) < 5) {
91 errx(1, "can't fold lines to less than 5 cols");
92 /* NOTREACHED */
94 markeol++;
95 break;
96 case 'f':
97 fold++; /* fold output lines to 80 cols */
98 break; /* using hidden newline */
99 case 'h':
100 eflags |= VIS_HTTPSTYLE;
101 break;
102 case 'l':
103 markeol++; /* mark end of line with \$ */
104 break;
105 case 'M':
106 eflags |= VIS_META;
107 break;
108 case 'm':
109 eflags |= VIS_MIMESTYLE;
110 if (foldwidth == 80)
111 foldwidth = 76;
112 break;
113 case 'N':
114 eflags |= VIS_NOLOCALE;
115 break;
116 case 'n':
117 none++;
118 break;
119 case 'o':
120 eflags |= VIS_OCTAL;
121 break;
122 case 'S':
123 eflags |= VIS_SHELL;
124 break;
125 case 's':
126 eflags |= VIS_SAFE;
127 break;
128 case 't':
129 eflags |= VIS_TAB;
130 break;
131 case 'w':
132 eflags |= VIS_WHITE;
133 break;
134 case '?':
135 default:
136 (void)fprintf(stderr,
137 "Usage: %s [-bcfhlMmNnoSstw] [-e extra]"
138 " [-F foldwidth] [file ...]\n", getprogname());
139 return 1;
142 if ((eflags & (VIS_HTTPSTYLE|VIS_MIMESTYLE)) ==
143 (VIS_HTTPSTYLE|VIS_MIMESTYLE))
144 errx(1, "Can't specify -m and -h at the same time");
146 argc -= optind;
147 argv += optind;
149 rval = 0;
151 if (*argv)
152 while (*argv) {
153 if ((fp = fopen(*argv, "r")) != NULL) {
154 process(fp);
155 (void)fclose(fp);
156 } else {
157 warn("%s", *argv);
158 rval = 1;
160 argv++;
162 else
163 process(stdin);
164 return rval;
167 static void
168 process(FILE *fp)
170 static int col = 0;
171 static char nul[] = "\0";
172 char *cp = nul + 1; /* so *(cp-1) starts out != '\n' */
173 wint_t c, c1, rachar;
174 char mbibuff[2 * MB_LEN_MAX + 1]; /* max space for 2 wchars */
175 char buff[4 * MB_LEN_MAX + 1]; /* max encoding length for one char */
176 int mbilen, cerr = 0, raerr = 0;
178 #if defined(__minix)
179 /* triggers a 'may be used uninitialized', when compiled with gcc,
180 * asserts off, and -Os. */
181 rachar = 0;
182 #endif /* defined(__minix) */
184 * The input stream is considered to be multibyte characters.
185 * The input loop will read this data inputing one character,
186 * possibly multiple bytes, at a time and converting each to
187 * a wide character wchar_t.
189 * The vis(3) functions, however, require single either bytes
190 * or a multibyte string as their arguments. So we convert
191 * our input wchar_t and the following look-ahead wchar_t to
192 * a multibyte string for processing by vis(3).
195 /* Read one multibyte character, store as wchar_t */
196 c = getwc(fp);
197 if (c == WEOF && errno == EILSEQ) {
198 /* Error in multibyte data. Read one byte. */
199 c = (wint_t)getc(fp);
200 cerr = 1;
202 while (c != WEOF) {
203 /* Clear multibyte input buffer. */
204 memset(mbibuff, 0, sizeof(mbibuff));
205 /* Read-ahead next multibyte character. */
206 if (!cerr)
207 rachar = getwc(fp);
208 if (cerr || (rachar == WEOF && errno == EILSEQ)) {
209 /* Error in multibyte data. Read one byte. */
210 rachar = (wint_t)getc(fp);
211 raerr = 1;
213 if (none) {
214 /* Handle -n flag. */
215 cp = buff;
216 *cp++ = c;
217 if (c == '\\')
218 *cp++ = '\\';
219 *cp = '\0';
220 } else if (markeol && c == '\n') {
221 /* Handle -l flag. */
222 cp = buff;
223 if ((eflags & VIS_NOSLASH) == 0)
224 *cp++ = '\\';
225 *cp++ = '$';
226 *cp++ = '\n';
227 *cp = '\0';
228 } else {
230 * Convert character using vis(3) library.
231 * At this point we will process one character.
232 * But we must pass the vis(3) library this
233 * character plus the next one because the next
234 * one is used as a look-ahead to decide how to
235 * encode this one under certain circumstances.
237 * Since our characters may be multibyte, e.g.,
238 * in the UTF-8 locale, we cannot use vis() and
239 * svis() which require byte input, so we must
240 * create a multibyte string and use strvisx().
242 /* Treat EOF as a NUL char. */
243 c1 = rachar;
244 if (c1 == WEOF)
245 c1 = L'\0';
247 * If we hit a multibyte conversion error above,
248 * insert byte directly into string buff because
249 * wctomb() will fail. Else convert wchar_t to
250 * multibyte using wctomb().
252 if (cerr) {
253 *mbibuff = (char)c;
254 mbilen = 1;
255 } else
256 mbilen = wctomb(mbibuff, c);
257 /* Same for look-ahead character. */
258 if (raerr)
259 mbibuff[mbilen] = (char)c1;
260 else
261 wctomb(mbibuff + mbilen, c1);
262 /* Perform encoding on just first character. */
263 (void) strsenvisx(buff, 4 * MB_LEN_MAX, mbibuff,
264 1, eflags, extra, &cerr);
267 cp = buff;
268 if (fold) {
269 #ifdef DEBUG
270 if (debug)
271 (void)printf("<%02d,", col);
272 #endif
273 col = foldit(cp, col, foldwidth, eflags);
274 #ifdef DEBUG
275 if (debug)
276 (void)printf("%02d>", col);
277 #endif
279 do {
280 (void)putchar(*cp);
281 } while (*++cp);
282 c = rachar;
283 cerr = raerr;
286 * terminate partial line with a hidden newline
288 if (fold && *(cp - 1) != '\n')
289 (void)printf(eflags & VIS_MIMESTYLE ? "=\n" : "\\\n");