1 /* $NetBSD: vis.c,v 1.22 2013/02/20 17:04:45 christos Exp $ */
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
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
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)vis.c 8.1 (Berkeley) 6/6/93";
42 __RCSID("$NetBSD: vis.c,v 1.22 2013/02/20 17:04:45 christos Exp $");
58 static int eflags
, fold
, foldwidth
= 80, none
, markeol
;
62 static const char *extra
= "";
64 static void process(FILE *);
67 main(int argc
, char *argv
[])
73 while ((ch
= getopt(argc
, argv
, "bcde:F:fhlmnostw")) != -1)
76 eflags
|= VIS_NOSLASH
;
90 if ((foldwidth
= atoi(optarg
)) < 5) {
91 errx(1, "can't fold lines to less than 5 cols");
97 fold
++; /* fold output lines to 80 cols */
98 break; /* using hidden newline */
100 eflags
|= VIS_HTTPSTYLE
;
103 markeol
++; /* mark end of line with \$ */
106 eflags
|= VIS_MIMESTYLE
;
127 (void)fprintf(stderr
,
128 "Usage: %s [-bcfhlmnostw] [-e extra]"
129 " [-F foldwidth] [file ...]\n", getprogname());
133 if ((eflags
& (VIS_HTTPSTYLE
|VIS_MIMESTYLE
)) ==
134 (VIS_HTTPSTYLE
|VIS_MIMESTYLE
))
135 errx(1, "Can't specify -m and -h at the same time");
144 if ((fp
= fopen(*argv
, "r")) != NULL
) {
162 static char nul
[] = "\0";
163 char *cp
= nul
+ 1; /* so *(cp-1) starts out != '\n' */
164 wint_t c
, c1
, rachar
;
165 char mbibuff
[2 * MB_LEN_MAX
+ 1]; /* max space for 2 wchars */
166 char buff
[4 * MB_LEN_MAX
+ 1]; /* max encoding length for one char */
167 int mbilen
, cerr
= 0, raerr
= 0;
170 * The input stream is considered to be multibyte characters.
171 * The input loop will read this data inputing one character,
172 * possibly multiple bytes, at a time and converting each to
173 * a wide character wchar_t.
175 * The vis(3) functions, however, require single either bytes
176 * or a multibyte string as their arguments. So we convert
177 * our input wchar_t and the following look-ahead wchar_t to
178 * a multibyte string for processing by vis(3).
181 /* Read one multibyte character, store as wchar_t */
183 if (c
== WEOF
&& errno
== EILSEQ
) {
184 /* Error in multibyte data. Read one byte. */
185 c
= (wint_t)getc(fp
);
189 /* Clear multibyte input buffer. */
190 memset(mbibuff
, 0, sizeof(mbibuff
));
191 /* Read-ahead next multibyte character. */
194 if (cerr
|| (rachar
== WEOF
&& errno
== EILSEQ
)) {
195 /* Error in multibyte data. Read one byte. */
196 rachar
= (wint_t)getc(fp
);
200 /* Handle -n flag. */
206 } else if (markeol
&& c
== '\n') {
207 /* Handle -l flag. */
209 if ((eflags
& VIS_NOSLASH
) == 0)
216 * Convert character using vis(3) library.
217 * At this point we will process one character.
218 * But we must pass the vis(3) library this
219 * character plus the next one because the next
220 * one is used as a look-ahead to decide how to
221 * encode this one under certain circumstances.
223 * Since our characters may be multibyte, e.g.,
224 * in the UTF-8 locale, we cannot use vis() and
225 * svis() which require byte input, so we must
226 * create a multibyte string and use strvisx().
228 /* Treat EOF as a NUL char. */
233 * If we hit a multibyte conversion error above,
234 * insert byte directly into string buff because
235 * wctomb() will fail. Else convert wchar_t to
236 * multibyte using wctomb().
242 mbilen
= wctomb(mbibuff
, c
);
243 /* Same for look-ahead character. */
245 mbibuff
[mbilen
] = (char)c1
;
247 wctomb(mbibuff
+ mbilen
, c1
);
248 /* Perform encoding on just first character. */
249 (void) strsenvisx(buff
, 4 * MB_LEN_MAX
, mbibuff
,
250 1, eflags
, extra
, &cerr
);
257 (void)printf("<%02d,", col
);
259 col
= foldit(cp
, col
, foldwidth
, eflags
);
262 (void)printf("%02d>", col
);
272 * terminate partial line with a hidden newline
274 if (fold
&& *(cp
- 1) != '\n')
275 (void)printf(eflags
& VIS_MIMESTYLE
? "=\n" : "\\\n");