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 $
46 static int eflags
, fold
, foldwidth
= 80, none
, markeol
;
50 static const char *extra
= "";
52 static void process(FILE *);
55 main(int argc
, char *argv
[])
61 while ((ch
= getopt(argc
, argv
, "abcde:F:fhlMmNnoSstw")) != -1)
67 eflags
|= VIS_NOSLASH
;
81 if ((foldwidth
= atoi(optarg
)) < 5) {
82 errx(1, "can't fold lines to less than 5 cols");
87 fold
++; /* fold output lines to 80 cols */
88 break; /* using hidden newline */
90 eflags
|= VIS_HTTPSTYLE
;
93 markeol
++; /* mark end of line with \$ */
99 eflags
|= VIS_MIMESTYLE
;
104 eflags
|= VIS_NOLOCALE
;
127 "usage: %s [-abcfhlMmNnoSstw] [-e extra]"
128 " [-F foldwidth] [file ...]\n", getprogname());
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");
143 if ((fp
= fopen(*argv
, "r")) != NULL
) {
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
[2 * MB_LEN_MAX
+ 1]; /* max space for 2 wchars */
165 char buff
[4 * MB_LEN_MAX
+ 1]; /* max encoding length for one char */
166 int mbilen
, cerr
= 0, raerr
= 0;
169 * The input stream is considered to be multibyte characters.
170 * The input loop will read this data inputing one character,
171 * possibly multiple bytes, at a time and converting each to
172 * a wide character wchar_t.
174 * The vis(3) functions, however, require single either bytes
175 * or a multibyte string as their arguments. So we convert
176 * our input wchar_t and the following look-ahead wchar_t to
177 * a multibyte string for processing by vis(3).
180 /* Read one multibyte character, store as wchar_t */
182 if (c
== WEOF
&& errno
== EILSEQ
) {
183 /* Error in multibyte data. Read one byte. */
184 c
= (wint_t)getc(fp
);
188 /* Clear multibyte input buffer. */
189 memset(mbibuff
, 0, sizeof(mbibuff
));
190 /* Read-ahead next multibyte character. */
193 if (cerr
|| (rachar
== WEOF
&& errno
== EILSEQ
)) {
194 /* Error in multibyte data. Read one byte. */
195 rachar
= (wint_t)getc(fp
);
199 /* Handle -n flag. */
205 } else if (markeol
&& c
== '\n') {
206 /* Handle -l flag. */
208 if ((eflags
& VIS_NOSLASH
) == 0)
215 * Convert character using vis(3) library.
216 * At this point we will process one character.
217 * But we must pass the vis(3) library this
218 * character plus the next one because the next
219 * one is used as a look-ahead to decide how to
220 * encode this one under certain circumstances.
222 * Since our characters may be multibyte, e.g.,
223 * in the UTF-8 locale, we cannot use vis() and
224 * svis() which require byte input, so we must
225 * create a multibyte string and use strvisx().
227 /* Treat EOF as a NUL char. */
232 * If we hit a multibyte conversion error above,
233 * insert byte directly into string buff because
234 * wctomb() will fail. Else convert wchar_t to
235 * multibyte using wctomb().
241 mbilen
= wctomb(mbibuff
, c
);
242 /* Same for look-ahead character. */
244 mbibuff
[mbilen
] = (char)c1
;
246 wctomb(mbibuff
+ mbilen
, c1
);
247 /* Perform encoding on just first character. */
248 strsenvisx(buff
, 4 * MB_LEN_MAX
, mbibuff
,
249 1, eflags
, extra
, &cerr
);
256 printf("<%02d,", col
);
258 col
= foldit(cp
, col
, foldwidth
, eflags
);
261 printf("%02d>", col
);
271 * terminate partial line with a hidden newline
273 if (fold
&& *(cp
- 1) != '\n')
274 printf(eflags
& VIS_MIMESTYLE
? "=\n" : "\\\n");