timport changes from plan 9 - plan9port - [fork] Plan 9 from user space
 (HTM) git clone git://src.adamsgaard.dk/plan9port
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit df121a0027e6dc08abe6fc907c9af2593a263c17
 (DIR) parent a76c0fd4a9a9a9a64893db614dd146a5819a6e0c
 (HTM) Author: rsc <devnull@localhost>
       Date:   Sun, 26 Dec 2004 23:07:37 +0000
       
       import changes from plan 9
       
       Diffstat:
         M src/lib9/fmt/dofmt.c                |       2 --
         M src/lib9/fmt/fltfmt.c               |      33 -------------------------------
         M src/lib9/fmt/fmt.c                  |       6 ++++++
         M src/lib9/fmt/fmtdef.h               |       9 +++++++++
         M src/lib9/fmt/fmtlock.c              |       2 --
         M src/lib9/fmt/fmtprint.c             |       8 ++++----
         M src/lib9/fmt/fmtquote.c             |      10 ++++++----
         M src/lib9/fmt/fmtstr.c               |      52 ++-----------------------------
         M src/lib9/fmt/fmtvprint.c            |      11 ++++++-----
         M src/lib9/fmt/runefmtstr.c           |      52 ++-----------------------------
         M src/lib9/fmt/runevseprint.c         |       4 ++--
         M src/lib9/fmt/runevsmprint.c         |      65 +++++++++++++++++++++++++++++--
         M src/lib9/fmt/runevsnprint.c         |       4 ++--
         M src/lib9/fmt/test.c                 |       5 ++++-
         M src/lib9/fmt/vfprint.c              |       4 ++--
         M src/lib9/fmt/vseprint.c             |       4 ++--
         M src/lib9/fmt/vsmprint.c             |      63 +++++++++++++++++++++++++++++--
         M src/lib9/fmt/vsnprint.c             |       4 ++--
       
       18 files changed, 171 insertions(+), 167 deletions(-)
       ---
 (DIR) diff --git a/src/lib9/fmt/dofmt.c b/src/lib9/fmt/dofmt.c
       t@@ -526,11 +526,9 @@ __flagfmt(Fmt *f)
                                f->flags |= FmtByte;
                        f->flags |= FmtShort;
                        break;
       -#ifndef PLAN9PORT
                case 'L':
                        f->flags |= FmtLDouble;
                        break;
       -#endif
                case 'l':
                        if(f->flags & FmtLong)
                                f->flags |= FmtVLong;
 (DIR) diff --git a/src/lib9/fmt/fltfmt.c b/src/lib9/fmt/fltfmt.c
       t@@ -371,39 +371,6 @@ found:
        }
        
        static int
       -fmtzdotpad(Fmt *f, int n, int pt)
       -{
       -        char *t, *s;
       -        int i;
       -        Rune *rt, *rs;
       -
       -        if(f->runes){
       -                rt = (Rune*)f->to;
       -                rs = (Rune*)f->stop;
       -                for(i = 0; i < n; i++){
       -                        if(i == pt){
       -                                FMTRCHAR(f, rt, rs, '.');
       -                        }
       -                        FMTRCHAR(f, rt, rs, '0');
       -                }
       -                f->nfmt += rt - (Rune*)f->to;
       -                f->to = rt;
       -        }else{
       -                t = (char*)f->to;
       -                s = (char*)f->stop;
       -                for(i = 0; i < n; i++){
       -                        if(i == pt){
       -                                FMTCHAR(f, t, s, '.');
       -                        }
       -                        FMTCHAR(f, t, s, '0');
       -                }
       -                f->nfmt += t - (char *)f->to;
       -                f->to = t;
       -        }
       -        return 0;
       -}
       -
       -static int
        floatfmt(Fmt *fmt, double f)
        {
                char s[FDIGIT+10];
 (DIR) diff --git a/src/lib9/fmt/fmt.c b/src/lib9/fmt/fmt.c
       t@@ -45,9 +45,13 @@ static Convfmt knownfmt[] = {
                '-',        __flagfmt,
                'C',        __runefmt,        /* Plan 9 addition */
                'E',        __efgfmt,
       +#ifndef PLAN9PORT
                'F',        __efgfmt,        /* ANSI only */
       +#endif
                'G',        __efgfmt,
       +#ifndef PLAN9PORT
                'L',        __flagfmt,        /* ANSI only */
       +#endif
                'S',        __runesfmt,        /* Plan 9 addition */
                'X',        __ifmt,
                'b',        __ifmt,                /* Plan 9 addition */
       t@@ -57,7 +61,9 @@ static Convfmt knownfmt[] = {
                'f',        __efgfmt,
                'g',        __efgfmt,
                'h',        __flagfmt,
       +#ifndef PLAN9PORT
                'i',        __ifmt,                /* ANSI only */
       +#endif
                'l',        __flagfmt,
                'n',        __countfmt,
                'o',        __ifmt,
 (DIR) diff --git a/src/lib9/fmt/fmtdef.h b/src/lib9/fmt/fmtdef.h
       t@@ -105,3 +105,12 @@ int          __strfmt(Fmt *f);
                        t += runetochar(t, &_rune);\
                }\
                }while(0)
       +
       +#ifdef va_copy
       +#        define VA_COPY(a,b) va_copy(a,b)
       +#        define VA_END(a) va_end(a)
       +#else
       +#        define VA_COPY(a,b) (a) = (b)
       +#        define VA_END(a)
       +#endif
       +
 (DIR) diff --git a/src/lib9/fmt/fmtlock.c b/src/lib9/fmt/fmtlock.c
       t@@ -19,11 +19,9 @@
        void
        __fmtlock(void)
        {
       -        ;
        }
        
        void
        __fmtunlock(void)
        {
       -        ;
        }
 (DIR) diff --git a/src/lib9/fmt/fmtprint.c b/src/lib9/fmt/fmtprint.c
       t@@ -17,7 +17,6 @@
        #include "fmt.h"
        #include "fmtdef.h"
        
       -
        /*
         * format a string into the output buffer
         * designed for formats which themselves call fmt,
       t@@ -32,15 +31,16 @@ fmtprint(Fmt *f, char *fmt, ...)
                f->flags = 0;
                f->width = 0;
                f->prec = 0;
       -        va_copy(va, f->args);
       +        VA_COPY(va, f->args);
       +        VA_END(f->args);
                va_start(f->args, fmt);
                n = dofmt(f, fmt);
                va_end(f->args);
                f->flags = 0;
                f->width = 0;
                f->prec = 0;
       -        va_copy(f->args,va);
       -        va_end(va);
       +        VA_COPY(f->args,va);
       +        VA_END(va);
                if(n >= 0)
                        return 0;
                return n;
 (DIR) diff --git a/src/lib9/fmt/fmtquote.c b/src/lib9/fmt/fmtquote.c
       t@@ -48,7 +48,7 @@ __quotesetup(char *s, Rune *r, int nin, int nout, Quoteinfo *q, int sharp, int r
                        q->nbytesout = 2;
                        q->nrunesout = 2;
                }
       -        for(; nin!=0; nin-=w){
       +        for(; nin!=0; nin--){
                        if(s)
                                w = chartorune(&c, s);
                        else{
       t@@ -183,12 +183,14 @@ qstrfmt(char *sin, Rune *rin, Quoteinfo *q, Fmt *f)
        int
        __quotestrfmt(int runesin, Fmt *f)
        {
       -        int outlen;
       +        int nin, outlen;
                Rune *r;
                char *s;
                Quoteinfo q;
        
       -        f->flags &= ~FmtPrec;        /* ignored for %q %Q, so disable for %s %S in easy case */
       +        nin = -1;
       +        if(f->flags&FmtPrec)
       +                nin = f->prec;
                if(runesin){
                        r = va_arg(f->args, Rune *);
                        s = nil;
       t@@ -206,7 +208,7 @@ __quotestrfmt(int runesin, Fmt *f)
                else
                        outlen = (char*)f->stop - (char*)f->to;
        
       -        __quotesetup(s, r, -1, outlen, &q, f->flags&FmtSharp, f->runes);
       +        __quotesetup(s, r, nin, outlen, &q, f->flags&FmtSharp, f->runes);
        //print("bytes in %d bytes out %d runes in %d runesout %d\n", q.nbytesin, q.nbytesout, q.nrunesin, q.nrunesout);
        
                if(runesin){
 (DIR) diff --git a/src/lib9/fmt/fmtstr.c b/src/lib9/fmt/fmtstr.c
       t@@ -11,63 +11,17 @@
         * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
         * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
         */
       -/*
       - * Plan 9 port version must include libc.h in order to 
       - * get Plan 9 debugging malloc, which sometimes returns
       - * different pointers than the standard malloc. 
       - */
       -#ifdef PLAN9PORT
       -#include <u.h>
       -#include <libc.h>
       -#else
        #include <stdlib.h>
       +#include <stdarg.h>
        #include "plan9.h"
        #include "fmt.h"
        #include "fmtdef.h"
       -#endif
       -
       -static int
       -fmtStrFlush(Fmt *f)
       -{
       -        char *s;
       -        int n;
       -
       -        n = (int)f->farg;
       -        n += 256;
       -        f->farg = (void*)n;
       -        s = (char*)f->start;
       -        f->start = realloc(s, n);
       -        if(f->start == nil){
       -                f->start = s;
       -                return 0;
       -        }
       -        f->to = (char*)f->start + ((char*)f->to - s);
       -        f->stop = (char*)f->start + n - 1;
       -        return 1;
       -}
       -
       -int
       -fmtstrinit(Fmt *f)
       -{
       -        int n;
       -
       -        f->runes = 0;
       -        n = 32;
       -        f->start = malloc(n);
       -        if(f->start == nil)
       -                return -1;
       -        f->to = f->start;
       -        f->stop = (char*)f->start + n - 1;
       -        f->flush = fmtStrFlush;
       -        f->farg = (void*)n;
       -        f->nfmt = 0;
       -        return 0;
       -}
        
        char*
        fmtstrflush(Fmt *f)
        {
       +        if(f->start == nil)
       +                return nil;
                *(char*)f->to = '\0';
       -        f->to = f->start;
                return (char*)f->start;
        }
 (DIR) diff --git a/src/lib9/fmt/fmtvprint.c b/src/lib9/fmt/fmtvprint.c
       t@@ -32,15 +32,16 @@ fmtvprint(Fmt *f, char *fmt, va_list args)
                f->flags = 0;
                f->width = 0;
                f->prec = 0;
       -        va_copy(va,f->args);
       -        va_copy(f->args,args);
       +        VA_COPY(va,f->args);
       +        VA_END(f->args);
       +        VA_COPY(f->args,args);
                n = dofmt(f, fmt);
                f->flags = 0;
                f->width = 0;
                f->prec = 0;
       -        va_end(f->args);
       -        va_copy(f->args,va);
       -        va_end(va);
       +        VA_END(f->args);
       +        VA_COPY(f->args,va);
       +        VA_END(va);
                if(n >= 0)
                        return 0;
                return n;
 (DIR) diff --git a/src/lib9/fmt/runefmtstr.c b/src/lib9/fmt/runefmtstr.c
       t@@ -11,63 +11,17 @@
         * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
         * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
         */
       -/*
       - * Plan 9 port version must include libc.h in order to 
       - * get Plan 9 debugging malloc, which sometimes returns
       - * different pointers than the standard malloc. 
       - */
       -#ifdef PLAN9PORT
       -#include <u.h>
       -#include <libc.h>
       -#else
       +#include <stdarg.h>
        #include <stdlib.h>
        #include "plan9.h"
        #include "fmt.h"
        #include "fmtdef.h"
       -#endif
       -
       -static int
       -runeFmtStrFlush(Fmt *f)
       -{
       -        Rune *s;
       -        int n;
       -
       -        n = (int)f->farg;
       -        n += 256;
       -        f->farg = (void*)n;
       -        s = (Rune*)f->start;
       -        f->start = realloc(s, sizeof(Rune)*n);
       -        if(f->start == nil){
       -                f->start = s;
       -                return 0;
       -        }
       -        f->to = (Rune*)f->start + ((Rune*)f->to - s);
       -        f->stop = (Rune*)f->start + n - 1;
       -        return 1;
       -}
       -
       -int
       -runefmtstrinit(Fmt *f)
       -{
       -        int n;
       -
       -        f->runes = 1;
       -        n = 32;
       -        f->start = malloc(sizeof(Rune)*n);
       -        if(f->start == nil)
       -                return -1;
       -        f->to = f->start;
       -        f->stop = (Rune*)f->start + n - 1;
       -        f->flush = runeFmtStrFlush;
       -        f->farg = (void*)n;
       -        f->nfmt = 0;
       -        return 0;
       -}
        
        Rune*
        runefmtstrflush(Fmt *f)
        {
       +        if(f->start == nil)
       +                return nil;
                *(Rune*)f->to = '\0';
       -        f->to = f->start;
                return f->start;
        }
 (DIR) diff --git a/src/lib9/fmt/runevseprint.c b/src/lib9/fmt/runevseprint.c
       t@@ -31,9 +31,9 @@ runevseprint(Rune *buf, Rune *e, char *fmt, va_list args)
                f.flush = nil;
                f.farg = nil;
                f.nfmt = 0;
       -        va_copy(f.args,args);
       +        VA_COPY(f.args,args);
                dofmt(&f, fmt);
       -        va_end(f.args);
       +        VA_END(f.args);
                *(Rune*)f.to = '\0';
                return (Rune*)f.to;
        }
 (DIR) diff --git a/src/lib9/fmt/runevsmprint.c b/src/lib9/fmt/runevsmprint.c
       t@@ -11,11 +11,64 @@
         * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
         * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
         */
       -#include <stdarg.h>
       +/*
       + * Plan 9 port version must include libc.h in order to 
       + * get Plan 9 debugging malloc, which sometimes returns
       + * different pointers than the standard malloc. 
       + */
       +#ifdef PLAN9PORT
       +#include <u.h>
       +#include <libc.h>
       +#else
        #include <stdlib.h>
        #include "plan9.h"
        #include "fmt.h"
        #include "fmtdef.h"
       +#endif
       +
       +static int
       +runeFmtStrFlush(Fmt *f)
       +{
       +        Rune *s;
       +        int n;
       +
       +        if(f->start == nil)
       +                return 0;
       +        n = (int)f->farg;
       +        n *= 2;
       +        s = (Rune*)f->start;
       +        f->start = realloc(s, sizeof(Rune)*n);
       +        if(f->start == nil){
       +                f->farg = nil;
       +                f->to = nil;
       +                f->stop = nil;
       +                free(s);
       +                return 0;
       +        }
       +        f->farg = (void*)n;
       +        f->to = (Rune*)f->start + ((Rune*)f->to - s);
       +        f->stop = (Rune*)f->start + n - 1;
       +        return 1;
       +}
       +
       +int
       +runefmtstrinit(Fmt *f)
       +{
       +        int n;
       +
       +        memset(f, 0, sizeof *f);
       +        f->runes = 1;
       +        n = 32;
       +        f->start = malloc(sizeof(Rune)*n);
       +        if(f->start == nil)
       +                return -1;
       +        f->to = f->start;
       +        f->stop = (Rune*)f->start + n - 1;
       +        f->flush = runeFmtStrFlush;
       +        f->farg = (void*)n;
       +        f->nfmt = 0;
       +        return 0;
       +}
        
        /*
         * print into an allocated string buffer
       t@@ -28,11 +81,15 @@ runevsmprint(char *fmt, va_list args)
        
                if(runefmtstrinit(&f) < 0)
                        return nil;
       -        va_copy(f.args,args);
       +        VA_COPY(f.args,args);
                n = dofmt(&f, fmt);
       -        va_end(f.args);
       -        if(n < 0)
       +        VA_END(f.args);
       +        if(f.start == nil)
       +                return nil;
       +        if(n < 0){
       +                free(f.start);
                        return nil;
       +        }
                *(Rune*)f.to = '\0';
                return (Rune*)f.start;
        }
 (DIR) diff --git a/src/lib9/fmt/runevsnprint.c b/src/lib9/fmt/runevsnprint.c
       t@@ -31,9 +31,9 @@ runevsnprint(Rune *buf, int len, char *fmt, va_list args)
                f.flush = nil;
                f.farg = nil;
                f.nfmt = 0;
       -        va_copy(f.args,args);
       +        VA_COPY(f.args,args);
                dofmt(&f, fmt);
       -        va_end(f.args);
       +        VA_END(f.args);
                *(Rune*)f.to = '\0';
                return (Rune*)f.to - buf;
        }
 (DIR) diff --git a/src/lib9/fmt/test.c b/src/lib9/fmt/test.c
       t@@ -11,6 +11,7 @@
         * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
         * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
         */
       +#include <stdio.h>
        #include <stdarg.h>
        #include <utf.h>
        #include "plan9.h"
       t@@ -32,10 +33,12 @@ main(int argc, char *argv[])
                print("e: %e %e %e\n", 3.14159, 3.14159e10, 3.14159e-10);
                print("f: %f %f %f\n", 3.14159, 3.14159e10, 3.14159e-10);
                print("smiley: %C\n", (Rune)0x263a);
       -        print("%g %.18\n", 2e25, 2e25);
       +        print("%g %.18g\n", 2e25, 2e25);
                print("%2.18g\n", 1.0);
       +        print("%2.18f\n", 1.0);
                print("%f\n", 3.1415927/4);
                print("%d\n", 23);
                print("%i\n", 23);
       +        print("%0.10d\n", 12345);
                return 0;
        }
 (DIR) diff --git a/src/lib9/fmt/vfprint.c b/src/lib9/fmt/vfprint.c
       t@@ -24,9 +24,9 @@ vfprint(int fd, char *fmt, va_list args)
                int n;
        
                fmtfdinit(&f, fd, buf, sizeof(buf));
       -        va_copy(f.args,args);
       +        VA_COPY(f.args,args);
                n = dofmt(&f, fmt);
       -        va_end(f.args);
       +        VA_END(f.args);
                if(n > 0 && __fmtFdFlush(&f) == 0)
                        return -1;
                return n;
 (DIR) diff --git a/src/lib9/fmt/vseprint.c b/src/lib9/fmt/vseprint.c
       t@@ -30,9 +30,9 @@ vseprint(char *buf, char *e, char *fmt, va_list args)
                f.flush = 0;
                f.farg = nil;
                f.nfmt = 0;
       -        va_copy(f.args,args);
       +        VA_COPY(f.args,args);
                dofmt(&f, fmt);
       -        va_end(f.args);
       +        VA_END(f.args);
                *(char*)f.to = '\0';
                return (char*)f.to;
        }
 (DIR) diff --git a/src/lib9/fmt/vsmprint.c b/src/lib9/fmt/vsmprint.c
       t@@ -11,11 +11,64 @@
         * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
         * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
         */
       +/*
       + * Plan 9 port version must include libc.h in order to 
       + * get Plan 9 debugging malloc, which sometimes returns
       + * different pointers than the standard malloc. 
       + */
       +#ifdef PLAN9PORT
       +#include <u.h>
       +#include <libc.h>
       +#else
        #include <stdlib.h>
       -#include <stdarg.h>
        #include "plan9.h"
        #include "fmt.h"
        #include "fmtdef.h"
       +#endif
       +
       +static int
       +fmtStrFlush(Fmt *f)
       +{
       +        char *s;
       +        int n;
       +
       +        if(f->start == nil)
       +                return 0;
       +        n = (int)f->farg;
       +        n *= 2;
       +        s = (char*)f->start;
       +        f->start = realloc(s, n);
       +        if(f->start == nil){
       +                f->farg = nil;
       +                f->to = nil;
       +                f->stop = nil;
       +                free(s);
       +                return 0;
       +        }
       +        f->farg = (void*)n;
       +        f->to = (char*)f->start + ((char*)f->to - s);
       +        f->stop = (char*)f->start + n - 1;
       +        return 1;
       +}
       +
       +int
       +fmtstrinit(Fmt *f)
       +{
       +        int n;
       +
       +        memset(f, 0, sizeof *f);
       +        f->runes = 0;
       +        n = 32;
       +        f->start = malloc(n);
       +        if(f->start == nil)
       +                return -1;
       +        f->to = f->start;
       +        f->stop = (char*)f->start + n - 1;
       +        f->flush = fmtStrFlush;
       +        f->farg = (void*)n;
       +        f->nfmt = 0;
       +        return 0;
       +}
        
        /*
         * print into an allocated string buffer
       t@@ -28,10 +81,12 @@ vsmprint(char *fmt, va_list args)
        
                if(fmtstrinit(&f) < 0)
                        return nil;
       -        va_copy(f.args,args);
       +        VA_COPY(f.args,args);
                n = dofmt(&f, fmt);
       -        va_end(f.args);
       -        if(n < 0)
       +        VA_END(f.args);
       +        if(n < 0){
       +                free(f.start);
                        return nil;
       +        }
                return fmtstrflush(&f);
        }
 (DIR) diff --git a/src/lib9/fmt/vsnprint.c b/src/lib9/fmt/vsnprint.c
       t@@ -31,9 +31,9 @@ vsnprint(char *buf, int len, char *fmt, va_list args)
                f.flush = 0;
                f.farg = nil;
                f.nfmt = 0;
       -        va_copy(f.args,args);
       +        VA_COPY(f.args,args);
                dofmt(&f, fmt);
       -        va_end(f.args);
       +        VA_END(f.args);
                *(char*)f.to = '\0';
                return (char*)f.to - buf;
        }