import changes from plan 9

This commit is contained in:
rsc 2004-12-26 23:07:37 +00:00
parent a76c0fd4a9
commit df121a0027
18 changed files with 171 additions and 167 deletions

View file

@ -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;

View file

@ -370,39 +370,6 @@ found:
s2[d] = 0;
}
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)
{

View file

@ -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 */
@ -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,

View file

@ -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

View file

@ -19,11 +19,9 @@
void
__fmtlock(void)
{
;
}
void
__fmtunlock(void)
{
;
}

View file

@ -17,7 +17,6 @@
#include "fmt.h"
#include "fmtdef.h"
/*
* format a string into the output buffer
* designed for formats which themselves call fmt,
@ -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;

View file

@ -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{
@ -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;
@ -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){

View file

@ -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;
}

View file

@ -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;

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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
@ -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;
}

View file

@ -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;
}

View file

@ -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"
@ -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;
}

View file

@ -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;

View file

@ -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;
}

View file

@ -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
@ -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);
}

View file

@ -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;
}