Switch to using wchar_t instead of Rune. - sam - An updated version of the sam text editor.
 (HTM) git clone git://vernunftzentrum.de/sam.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) LICENSE
       ---
 (DIR) commit 5482032dce9b0f38b192198665de4c9beb14b52e
 (DIR) parent df3e7c46eae5620b6f3ab7fb5e3cdf02edeae541
 (HTM) Author: Rob King <jking@deadpixi.com>
       Date:   Mon,  3 Oct 2016 21:53:36 -0500
       
       Switch to using wchar_t instead of Rune.
       
       Diffstat:
         include/libc.h                      |       2 +-
         include/u.h                         |       1 +
         sam/io.c                            |      47 +++++++++----------------------
         sam/mesg.c                          |      96 ++++++++++++++++----------------
         sam/mesg.h                          |       2 +-
         sam/string.c                        |      16 ++++++++--------
       
       6 files changed, 73 insertions(+), 91 deletions(-)
       ---
 (DIR) diff --git a/include/libc.h b/include/libc.h
       @@ -6,7 +6,7 @@
        #include <u.h>
        
        #define utflen(s)        (mbstowcs(NULL, (s), 0))
       -#define fullrune(s, n)   (mbtowc(NULL, (s), (n))) /* >0 */
       +#define fullrune(s, n)   (mbtowc(NULL, (s), (n)) >= 0)
        #define runetochar(s, r) (wctomb((s), (r)))
        #define runelen(r)       (wctomb(NULL, (r)))
        
 (DIR) diff --git a/include/u.h b/include/u.h
       @@ -1,4 +1,5 @@
        #include <fcntl.h>
       +#include <inttypes.h>
        #include <limits.h>
        #include <locale.h>
        #include <stdbool.h>
 (DIR) diff --git a/sam/io.c b/sam/io.c
       @@ -73,47 +73,28 @@ writef(File *f)
        Posn
        readio(File *f, int *nulls, int setdate)
        {
       -    int n, b, w;
       -    wchar_t *r;
       -    Posn nt;
       +    size_t n = 0;
       +    size_t nt = 0;
            Posn p = addr.r.p2;
            uint64_t dev, qid;
            int64_t mtime;
            char buf[BLOCKSIZE+1] = {0};
       -    char *s = NULL;
       +    const char *bp = buf;
       +    wchar_t wbuf[BLOCKSIZE * MB_LEN_MAX + 1] = {0};
       +    mbstate_t ps = {0};
        
            *nulls = FALSE;
       -    b = 0;
       -    for (nt = 0; (n = read(io, buf + b, BLOCKSIZE - b)) > 0; nt += (r - genbuf)){
       -        n += b;
       -        b = 0;
       -        r = genbuf;
       -        s = buf;
       -        while (n > 0){
       -            if (fullrune(s, n)){
       -                w = chartorune(r, s);
       -                n -= w;
       -                s += w;
       -                continue;
       -            } else{
       -                if (*r)
       -                    *r++ = *s++;
       -                else
       -                    *nulls = true;
       -                --n;
       -            }
       -            b = n;
       -            memmove(buf, s, b);
       -            break;
       -        }
       -        Finsert(f, tmprstr(genbuf, r-genbuf), p);
       -    }
        
       -    if (b)
       -        *nulls = TRUE;
       +    n = read(io, buf, BLOCKSIZE);
       +    while (n > 0){
       +        size_t w = mbsrtowcs(wbuf, &bp, BLOCKSIZE, &ps);
       +        Finsert(f, tmprstr(wbuf, w), p);
        
       -    if (*nulls)
       -        warn(Wnulls);
       +        memset(buf, 0, sizeof(buf));
       +        nt += n;
       +        n = read(io, buf, BLOCKSIZE);
       +        bp = buf;
       +    }
        
            if (setdate){
                if (statfd(io, &dev, &qid, &mtime, 0, 0) > 0){
 (DIR) diff --git a/sam/mesg.c b/sam/mesg.c
       @@ -21,57 +21,57 @@ void    setgenstr(File*, Posn, Posn);
        
        #ifdef DEBUG
        char *hname[] = {
       -    [Hversion]  "Hversion",
       -    [Hbindname] "Hbindname",
       -    [Hcurrent]  "Hcurrent",
       -    [Hnewname]  "Hnewname",
       -    [Hmovname]  "Hmovname",
       -    [Hgrow]     "Hgrow",
       -    [Hcheck0]   "Hcheck0",
       -    [Hcheck]    "Hcheck",
       -    [Hunlock]   "Hunlock",
       -    [Hdata]     "Hdata",
       -    [Horigin]   "Horigin",
       -    [Hunlockfile]   "Hunlockfile",
       -    [Hsetdot]   "Hsetdot",
       -    [Hgrowdata] "Hgrowdata",
       -    [Hmoveto]   "Hmoveto",
       -    [Hclean]    "Hclean",
       -    [Hdirty]    "Hdirty",
       -    [Hcut]      "Hcut",
       -    [Hsetpat]   "Hsetpat",
       -    [Hdelname]  "Hdelname",
       -    [Hclose]    "Hclose",
       -    [Hsetsnarf] "Hsetsnarf",
       -    [Hsnarflen] "Hsnarflen",
       -    [Hack]      "Hack",
       -    [Hextcmd]   "Hextcmd",
       -    [Hexit]     "Hexit",
       +    [Hversion] =  "Hversion",
       +    [Hbindname] = "Hbindname",
       +    [Hcurrent] =  "Hcurrent",
       +    [Hnewname] =  "Hnewname",
       +    [Hmovname] =  "Hmovname",
       +    [Hgrow] =     "Hgrow",
       +    [Hcheck0] =   "Hcheck0",
       +    [Hcheck] =    "Hcheck",
       +    [Hunlock] =   "Hunlock",
       +    [Hdata] =     "Hdata",
       +    [Horigin] =   "Horigin",
       +    [Hunlockfile] =   "Hunlockfile",
       +    [Hsetdot] =   "Hsetdot",
       +    [Hgrowdata] = "Hgrowdata",
       +    [Hmoveto] =   "Hmoveto",
       +    [Hclean] =    "Hclean",
       +    [Hdirty] =    "Hdirty",
       +    [Hcut] =      "Hcut",
       +    [Hsetpat] =   "Hsetpat",
       +    [Hdelname] =  "Hdelname",
       +    [Hclose] =    "Hclose",
       +    [Hsetsnarf] = "Hsetsnarf",
       +    [Hsnarflen] = "Hsnarflen",
       +    [Hack] =      "Hack",
       +    [Hextcmd] =   "Hextcmd",
       +    [Hexit] =     "Hexit",
        };
        
        char *tname[] = {
       -    [Tversion]  "Tversion",
       -    [Tstartcmdfile] "Tstartcmdfile",
       -    [Tcheck]    "Tcheck",
       -    [Trequest]  "Trequest",
       -    [Torigin]   "Torigin",
       -    [Tstartfile]    "Tstartfile",
       -    [Tworkfile] "Tworkfile",
       -    [Ttype]     "Ttype",
       -    [Tcut]      "Tcut",
       -    [Tpaste]    "Tpaste",
       -    [Tsnarf]    "Tsnarf",
       -    [Tstartnewfile] "Tstartnewfile",
       -    [Twrite]    "Twrite",
       -    [Tclose]    "Tclose",
       -    [Tlook]     "Tlook",
       -    [Tsearch]   "Tsearch",
       -    [Tsend]     "Tsend",
       -    [Tdclick]   "Tdclick",
       -    [Tstartsnarf]   "Tstartsnarf",
       -    [Tsetsnarf] "Tsetsnarf",
       -    [Tack]      "Tack",
       -    [Texit]     "Texit",
       +    [Tversion] =  "Tversion",
       +    [Tstartcmdfile] = "Tstartcmdfile",
       +    [Tcheck] =    "Tcheck",
       +    [Trequest] =  "Trequest",
       +    [Torigin] =   "Torigin",
       +    [Tstartfile] =    "Tstartfile",
       +    [Tworkfile] = "Tworkfile",
       +    [Ttype] =     "Ttype",
       +    [Tcut] =      "Tcut",
       +    [Tpaste] =    "Tpaste",
       +    [Tsnarf] =    "Tsnarf",
       +    [Tstartnewfile] = "Tstartnewfile",
       +    [Twrite] =    "Twrite",
       +    [Tclose] =    "Tclose",
       +    [Tlook] =     "Tlook",
       +    [Tsearch] =   "Tsearch",
       +    [Tsend] =     "Tsend",
       +    [Tdclick] =   "Tdclick",
       +    [Tstartsnarf] =   "Tstartsnarf",
       +    [Tsetsnarf] = "Tsetsnarf",
       +    [Tack] =      "Tack",
       +    [Texit] =     "Texit",
        };
        
        void
 (DIR) diff --git a/sam/mesg.h b/sam/mesg.h
       @@ -1,5 +1,5 @@
        /* Copyright (c) 1998 Lucent Technologies - All rights reserved. */
       -#define VERSION 16092
       +#define VERSION 16091
        
        #define TBLOCKSIZE 512                           /* largest piece of text sent to terminal */
        #define DATASIZE  (MB_LEN_MAX * TBLOCKSIZE + 30) /* ... including protocol header stuff */
 (DIR) diff --git a/sam/string.c b/sam/string.c
       @@ -41,9 +41,9 @@ Strzero(String *p)
        void
        Strdupl(String *p, wchar_t *s) /* copies the null */
        {
       -    p->n = wcslen(s) + 1;
       -    Strinsure(p, p->n);
       -    memmove(p->s, s, p->n*RUNESIZE);
       +    p->n = wcslen(s);
       +    Strinsure(p, p->n + 1);
       +    wmemmove(p->s, s, p->n);
        }
        
        void
       @@ -51,7 +51,7 @@ Strduplstr(String *p, String *q)    /* will copy the null if there's one there *
        {
            Strinsure(p, q->n);
            p->n = q->n;
       -    memmove(p->s, q->s, q->n*RUNESIZE);
       +    wmemmove(p->s, q->s, q->n);
        }
        
        void
       @@ -78,15 +78,15 @@ void
        Strinsert(String *p, String *q, Posn p0)
        {
            Strinsure(p, p->n+q->n);
       -    memmove(p->s+p0+q->n, p->s+p0, (p->n-p0)*RUNESIZE);
       -    memmove(p->s+p0, q->s, q->n*RUNESIZE);
       +    wmemmove(p->s + p0 + q->n, p->s + p0, p->n - p0);
       +    wmemmove(p->s + p0, q->s, q->n);
            p->n += q->n;
        }
        
        void
        Strdelete(String *p, Posn p1, Posn p2)
        {
       -    memmove(p->s+p1, p->s+p2, (p->n-p2)*RUNESIZE);
       +    wmemmove(p->s + p1, p->s + p2, p->n - p2);
            p->n -= p2-p1;
        }
        
       @@ -105,9 +105,9 @@ Strtoc(String *s)
        
            memset(ws, 0, sizeof(ws));
            memset(c, 0, l + 1);
       +    wmemcpy(ws, s->s, s->n);
            ws[s->n] = 0;
        
       -    swprintf(ws, s->n, L"%ls", s->s);
            if (wcstombs(c, ws, l) == (size_t)-1)
                panic("encoding 1");