diff options
author | ache <ache@FreeBSD.org> | 2016-09-22 16:46:59 +0000 |
---|---|---|
committer | ache <ache@FreeBSD.org> | 2016-09-22 16:46:59 +0000 |
commit | 815c47c03bec30e97fcaccf870c39b17c7c01a55 (patch) | |
tree | b62f62bb7e5fa274ff25057d54303fdfd7968b1a | |
parent | 4e88981ba96e1935778ecbba00e5789c2fa7e1f0 (diff) | |
download | FreeBSD-src-815c47c03bec30e97fcaccf870c39b17c7c01a55.zip FreeBSD-src-815c47c03bec30e97fcaccf870c39b17c7c01a55.tar.gz |
MFC r305841
Implement multibyte encoding support for -v with fallback
-rw-r--r-- | bin/cat/cat.c | 37 |
1 files changed, 31 insertions, 6 deletions
diff --git a/bin/cat/cat.c b/bin/cat/cat.c index 0daac7c..092d0b0 100644 --- a/bin/cat/cat.c +++ b/bin/cat/cat.c @@ -63,6 +63,8 @@ __FBSDID("$FreeBSD$"); #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <wchar.h> +#include <wctype.h> static int bflag, eflag, lflag, nflag, sflag, tflag, vflag; static int rval; @@ -205,6 +207,7 @@ static void cook_cat(FILE *fp) { int ch, gobble, line, prev; + wint_t wch; /* Reset EOF condition on stdin. */ if (fp == stdin && feof(stdin)) @@ -237,18 +240,40 @@ cook_cat(FILE *fp) continue; } } else if (vflag) { - if (!isascii(ch) && !isprint(ch)) { + (void)ungetc(ch, fp); + /* + * Our getwc(3) doesn't change file position + * on error. + */ + if ((wch = getwc(fp)) == WEOF) { + if (ferror(fp) && errno == EILSEQ) { + clearerr(fp); + /* Resync attempt. */ + memset(&fp->_mbstate, 0, sizeof(mbstate_t)); + if ((ch = getc(fp)) == EOF) + break; + wch = ch; + goto ilseq; + } else + break; + } + if (!iswascii(wch) && !iswprint(wch)) { +ilseq: if (putchar('M') == EOF || putchar('-') == EOF) break; - ch = toascii(ch); + wch = toascii(wch); } - if (iscntrl(ch)) { - if (putchar('^') == EOF || - putchar(ch == '\177' ? '?' : - ch | 0100) == EOF) + if (iswcntrl(wch)) { + ch = toascii(wch); + ch = (ch == '\177') ? '?' : (ch | 0100); + if (putchar('^') == EOF || putchar(ch) == EOF) break; continue; } + if (putwchar(wch) == WEOF) + break; + ch = -1; + continue; } if (putchar(ch) == EOF) break; |