2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)vis.c 8.1 (Berkeley) 6/6/93
31 * $NetBSD: vis.c,v 1.25 2015/05/24 19:42:39 christos Exp $
47 static int eflags
, fold
, foldwidth
= 80, none
, markeol
;
51 static const char *extra
= "";
53 static void process(FILE *);
56 main(int argc
, char *argv
[])
62 while ((ch
= getopt(argc
, argv
, "bcde:F:fhlMmNnoSstw")) != -1)
65 eflags
|= VIS_NOSLASH
;
79 if ((foldwidth
= atoi(optarg
)) < 5) {
80 errx(1, "can't fold lines to less than 5 cols");
85 fold
++; /* fold output lines to 80 cols */
86 break; /* using hidden newline */
88 eflags
|= VIS_HTTPSTYLE
;
91 markeol
++; /* mark end of line with \$ */
97 eflags
|= VIS_MIMESTYLE
;
102 eflags
|= VIS_NOLOCALE
;
125 "usage: %s [-bcfhlMmNnoSstw] [-e extra]"
126 " [-F foldwidth] [file ...]\n", getprogname());
130 if ((eflags
& (VIS_HTTPSTYLE
|VIS_MIMESTYLE
)) ==
131 (VIS_HTTPSTYLE
|VIS_MIMESTYLE
))
132 errx(1, "Can't specify -m and -h at the same time");
141 if ((fp
= fopen(*argv
, "r")) != NULL
) {
159 static char nul
[] = "\0";
160 char *cp
= nul
+ 1; /* so *(cp-1) starts out != '\n' */
161 wint_t c
, c1
, rachar
;
162 char mbibuff
[2 * MB_LEN_MAX
+ 1]; /* max space for 2 wchars */
163 char buff
[4 * MB_LEN_MAX
+ 1]; /* max encoding length for one char */
164 int mbilen
, cerr
= 0, raerr
= 0;
167 * The input stream is considered to be multibyte characters.
168 * The input loop will read this data inputing one character,
169 * possibly multiple bytes, at a time and converting each to
170 * a wide character wchar_t.
172 * The vis(3) functions, however, require single either bytes
173 * or a multibyte string as their arguments. So we convert
174 * our input wchar_t and the following look-ahead wchar_t to
175 * a multibyte string for processing by vis(3).
178 /* Read one multibyte character, store as wchar_t */
180 if (c
== WEOF
&& errno
== EILSEQ
) {
181 /* Error in multibyte data. Read one byte. */
182 c
= (wint_t)getc(fp
);
186 /* Clear multibyte input buffer. */
187 memset(mbibuff
, 0, sizeof(mbibuff
));
188 /* Read-ahead next multibyte character. */
191 if (cerr
|| (rachar
== WEOF
&& errno
== EILSEQ
)) {
192 /* Error in multibyte data. Read one byte. */
193 rachar
= (wint_t)getc(fp
);
197 /* Handle -n flag. */
203 } else if (markeol
&& c
== '\n') {
204 /* Handle -l flag. */
206 if ((eflags
& VIS_NOSLASH
) == 0)
213 * Convert character using vis(3) library.
214 * At this point we will process one character.
215 * But we must pass the vis(3) library this
216 * character plus the next one because the next
217 * one is used as a look-ahead to decide how to
218 * encode this one under certain circumstances.
220 * Since our characters may be multibyte, e.g.,
221 * in the UTF-8 locale, we cannot use vis() and
222 * svis() which require byte input, so we must
223 * create a multibyte string and use strvisx().
225 /* Treat EOF as a NUL char. */
230 * If we hit a multibyte conversion error above,
231 * insert byte directly into string buff because
232 * wctomb() will fail. Else convert wchar_t to
233 * multibyte using wctomb().
239 mbilen
= wctomb(mbibuff
, c
);
240 /* Same for look-ahead character. */
242 mbibuff
[mbilen
] = (char)c1
;
244 wctomb(mbibuff
+ mbilen
, c1
);
245 /* Perform encoding on just first character. */
246 strsenvisx(buff
, 4 * MB_LEN_MAX
, mbibuff
,
247 1, eflags
, extra
, &cerr
);
254 printf("<%02d,", col
);
256 col
= foldit(cp
, col
, foldwidth
, eflags
);
259 printf("%02d>", col
);
269 * terminate partial line with a hidden newline
271 if (fold
&& *(cp
- 1) != '\n')
272 printf(eflags
& VIS_MIMESTYLE
? "=\n" : "\\\n");