Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/vis do the encoding character by character instead o...



details:   https://anonhg.NetBSD.org/src/rev/347102f2ec77
branches:  trunk
changeset: 784939:347102f2ec77
user:      christos <christos%NetBSD.org@localhost>
date:      Thu Feb 14 14:00:00 2013 +0000

description:
do the encoding character by character instead of failing on encoding mismatch

diffstat:

 usr.bin/vis/vis.1 |   7 +------
 usr.bin/vis/vis.c |  30 +++++++++++++++++++++---------
 2 files changed, 22 insertions(+), 15 deletions(-)

diffs (94 lines):

diff -r 17d1423c85e2 -r 347102f2ec77 usr.bin/vis/vis.1
--- a/usr.bin/vis/vis.1 Thu Feb 14 13:57:53 2013 +0000
+++ b/usr.bin/vis/vis.1 Thu Feb 14 14:00:00 2013 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: vis.1,v 1.16 2013/02/14 08:56:59 wiz Exp $
+.\"    $NetBSD: vis.1,v 1.17 2013/02/14 14:00:00 christos Exp $
 .\"
 .\" Copyright (c) 1989, 1991, 1993, 1994
 .\"    The Regents of the University of California.  All rights reserved.
@@ -149,11 +149,6 @@
 If the locales of the data and the conversion are mismatched, multibyte
 character recognition may fail and encoding will be performed byte-by-byte
 instead.
-The result of encoding using
-.Nm
-followed by decoding using
-.Xr unvis 3
-is unlikely to return the same input data in this case.
 .Sh ENVIRONMENT
 .Bl -tag -width ".Ev LC_CTYPE"
 .It Ev LC_CTYPE
diff -r 17d1423c85e2 -r 347102f2ec77 usr.bin/vis/vis.c
--- a/usr.bin/vis/vis.c Thu Feb 14 13:57:53 2013 +0000
+++ b/usr.bin/vis/vis.c Thu Feb 14 14:00:00 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: vis.c,v 1.19 2013/02/13 22:28:41 christos Exp $        */
+/*     $NetBSD: vis.c,v 1.20 2013/02/14 14:00:01 christos Exp $        */
 
 /*-
  * Copyright (c) 1989, 1993
@@ -39,7 +39,7 @@
 #if 0
 static char sccsid[] = "@(#)vis.c      8.1 (Berkeley) 6/6/93";
 #endif
-__RCSID("$NetBSD: vis.c,v 1.19 2013/02/13 22:28:41 christos Exp $");
+__RCSID("$NetBSD: vis.c,v 1.20 2013/02/14 14:00:01 christos Exp $");
 #endif /* not lint */
 
 #include <stdio.h>
@@ -161,17 +161,21 @@
        static char nul[] = "\0";
        char *cp = nul + 1;     /* so *(cp-1) starts out != '\n' */
        wint_t c, c1, rachar; 
-       wchar_t ibuff[3]; /* room for c + rachar + NUL */
        char mbibuff[13]; /* ((sizeof(ibuff) - 1) * MB_LEN_MAX) + NUL */
        char buff[5]; /* max vis-encoding length for one char + NUL */
+       int mbilen, cerr = 0, raerr = 0;
        
        c = getwc(fp);
-       if (c == WEOF && errno == EILSEQ)
+       if (c == WEOF && errno == EILSEQ) {
                c = (wint_t)getc(fp);
+               cerr = 1;
+       }
        while (c != WEOF) {
                rachar = getwc(fp);
-               if (rachar == WEOF && errno == EILSEQ)
+               if (rachar == WEOF && errno == EILSEQ) {
                        rachar = (wint_t)getc(fp);
+                       raerr = 1;
+               }
                if (none) {
                        cp = buff;
                        *cp++ = c;
@@ -189,10 +193,16 @@
                        c1 = rachar;
                        if (c1 == WEOF)
                                c1 = L'\0';
-                       swprintf(ibuff, 3, L"%lc%lc", c, c1);
-                       wcstombs(mbibuff, ibuff,
-                           (wcslen(ibuff) * MB_LEN_MAX) + 1);
-                       (void) strsvisx(buff, mbibuff, 1, eflags, extra);
+                       if (cerr) {
+                               *mbibuff = c;
+                               mbilen = 1;
+                       } else
+                               mbilen = wctomb(mbibuff, c);
+                       if (raerr)
+                               mbibuff[mbilen] = c1;
+                       else
+                               wctomb(mbibuff + mbilen, c1);
+                       (void)strsvisx(buff, mbibuff, mbilen, eflags, extra);
                }
 
                cp = buff;
@@ -211,6 +221,8 @@
                        (void)putchar(*cp);
                } while (*++cp);
                c = rachar;
+               cerr = raerr;
+               raerr = 0;
        }
        /*
         * terminate partial line with a hidden newline



Home | Main Index | Thread Index | Old Index