2006-01-17 16:22:03 +03:00
|
|
|
#include <u.h>
|
|
|
|
#include <libc.h>
|
2005-08-08 16:50:13 +04:00
|
|
|
#include "fmtdef.h"
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
Maxfmt = 64
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct Convfmt Convfmt;
|
|
|
|
struct Convfmt
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
volatile Fmts fmt; /* for spin lock in fmtfmt; avoids race due to write order */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
2006-01-17 15:37:52 +03:00
|
|
|
/* lock by calling __fmtlock, __fmtunlock */
|
2005-08-08 16:50:13 +04:00
|
|
|
int nfmt;
|
|
|
|
Convfmt fmt[Maxfmt];
|
|
|
|
} fmtalloc;
|
|
|
|
|
|
|
|
static Convfmt knownfmt[] = {
|
2006-01-17 15:37:52 +03:00
|
|
|
' ', __flagfmt,
|
|
|
|
'#', __flagfmt,
|
|
|
|
'%', __percentfmt,
|
|
|
|
'+', __flagfmt,
|
|
|
|
',', __flagfmt,
|
|
|
|
'-', __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 */
|
|
|
|
'c', __charfmt,
|
|
|
|
'd', __ifmt,
|
|
|
|
'e', __efgfmt,
|
|
|
|
'f', __efgfmt,
|
|
|
|
'g', __efgfmt,
|
|
|
|
'h', __flagfmt,
|
|
|
|
#ifndef PLAN9PORT
|
|
|
|
'i', __ifmt, /* ANSI only */
|
|
|
|
#endif
|
|
|
|
'l', __flagfmt,
|
|
|
|
'n', __countfmt,
|
|
|
|
'o', __ifmt,
|
|
|
|
'p', __ifmt,
|
|
|
|
'r', __errfmt,
|
|
|
|
's', __strfmt,
|
|
|
|
#ifdef PLAN9PORT
|
|
|
|
'u', __flagfmt,
|
|
|
|
#else
|
|
|
|
'u', __ifmt,
|
|
|
|
#endif
|
|
|
|
'x', __ifmt,
|
2006-05-21 21:02:05 +04:00
|
|
|
0, 0,
|
2005-08-08 16:50:13 +04:00
|
|
|
};
|
|
|
|
|
2006-01-17 15:37:52 +03:00
|
|
|
|
|
|
|
int (*fmtdoquote)(int);
|
2005-08-08 16:50:13 +04:00
|
|
|
|
|
|
|
/*
|
2006-01-17 15:37:52 +03:00
|
|
|
* __fmtlock() must be set
|
2005-08-08 16:50:13 +04:00
|
|
|
*/
|
|
|
|
static int
|
2006-01-17 15:37:52 +03:00
|
|
|
__fmtinstall(int c, Fmts f)
|
2005-08-08 16:50:13 +04:00
|
|
|
{
|
|
|
|
Convfmt *p, *ep;
|
|
|
|
|
|
|
|
if(c<=0 || c>=65536)
|
|
|
|
return -1;
|
|
|
|
if(!f)
|
2006-01-17 15:37:52 +03:00
|
|
|
f = __badfmt;
|
2005-08-08 16:50:13 +04:00
|
|
|
|
|
|
|
ep = &fmtalloc.fmt[fmtalloc.nfmt];
|
|
|
|
for(p=fmtalloc.fmt; p<ep; p++)
|
|
|
|
if(p->c == c)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if(p == &fmtalloc.fmt[Maxfmt])
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
p->fmt = f;
|
|
|
|
if(p == ep){ /* installing a new format character */
|
|
|
|
fmtalloc.nfmt++;
|
|
|
|
p->c = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-01-17 15:37:52 +03:00
|
|
|
fmtinstall(int c, int (*f)(Fmt*))
|
2005-08-08 16:50:13 +04:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2006-01-17 15:37:52 +03:00
|
|
|
__fmtlock();
|
|
|
|
ret = __fmtinstall(c, f);
|
|
|
|
__fmtunlock();
|
2005-08-08 16:50:13 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Fmts
|
|
|
|
fmtfmt(int c)
|
|
|
|
{
|
|
|
|
Convfmt *p, *ep;
|
|
|
|
|
|
|
|
ep = &fmtalloc.fmt[fmtalloc.nfmt];
|
|
|
|
for(p=fmtalloc.fmt; p<ep; p++)
|
|
|
|
if(p->c == c){
|
2006-05-21 21:02:05 +04:00
|
|
|
while(p->fmt == 0) /* loop until value is updated */
|
2005-08-08 16:50:13 +04:00
|
|
|
;
|
|
|
|
return p->fmt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* is this a predefined format char? */
|
2006-01-17 15:37:52 +03:00
|
|
|
__fmtlock();
|
2005-08-08 16:50:13 +04:00
|
|
|
for(p=knownfmt; p->c; p++)
|
|
|
|
if(p->c == c){
|
2006-01-17 15:37:52 +03:00
|
|
|
__fmtinstall(p->c, p->fmt);
|
|
|
|
__fmtunlock();
|
2005-08-08 16:50:13 +04:00
|
|
|
return p->fmt;
|
|
|
|
}
|
2006-01-17 15:37:52 +03:00
|
|
|
__fmtunlock();
|
2005-08-08 16:50:13 +04:00
|
|
|
|
2006-01-17 15:37:52 +03:00
|
|
|
return __badfmt;
|
2005-08-08 16:50:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
2006-01-17 15:37:52 +03:00
|
|
|
__fmtdispatch(Fmt *f, void *fmt, int isrunes)
|
2005-08-08 16:50:13 +04:00
|
|
|
{
|
|
|
|
Rune rune, r;
|
|
|
|
int i, n;
|
|
|
|
|
|
|
|
f->flags = 0;
|
|
|
|
f->width = f->prec = 0;
|
|
|
|
|
|
|
|
for(;;){
|
|
|
|
if(isrunes){
|
|
|
|
r = *(Rune*)fmt;
|
|
|
|
fmt = (Rune*)fmt + 1;
|
|
|
|
}else{
|
2006-01-17 15:37:52 +03:00
|
|
|
fmt = (char*)fmt + chartorune(&rune, (char*)fmt);
|
2005-08-08 16:50:13 +04:00
|
|
|
r = rune;
|
|
|
|
}
|
|
|
|
f->r = r;
|
|
|
|
switch(r){
|
|
|
|
case '\0':
|
|
|
|
return nil;
|
|
|
|
case '.':
|
|
|
|
f->flags |= FmtWidth|FmtPrec;
|
|
|
|
continue;
|
|
|
|
case '0':
|
|
|
|
if(!(f->flags & FmtWidth)){
|
|
|
|
f->flags |= FmtZero;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* fall through */
|
|
|
|
case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
|
|
|
i = 0;
|
|
|
|
while(r >= '0' && r <= '9'){
|
|
|
|
i = i * 10 + r - '0';
|
|
|
|
if(isrunes){
|
|
|
|
r = *(Rune*)fmt;
|
|
|
|
fmt = (Rune*)fmt + 1;
|
|
|
|
}else{
|
|
|
|
r = *(char*)fmt;
|
|
|
|
fmt = (char*)fmt + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(isrunes)
|
|
|
|
fmt = (Rune*)fmt - 1;
|
|
|
|
else
|
|
|
|
fmt = (char*)fmt - 1;
|
|
|
|
numflag:
|
|
|
|
if(f->flags & FmtWidth){
|
|
|
|
f->flags |= FmtPrec;
|
|
|
|
f->prec = i;
|
|
|
|
}else{
|
|
|
|
f->flags |= FmtWidth;
|
|
|
|
f->width = i;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
case '*':
|
|
|
|
i = va_arg(f->args, int);
|
|
|
|
if(i < 0){
|
2006-01-17 15:37:52 +03:00
|
|
|
/*
|
|
|
|
* negative precision =>
|
|
|
|
* ignore the precision.
|
|
|
|
*/
|
|
|
|
if(f->flags & FmtPrec){
|
|
|
|
f->flags &= ~FmtPrec;
|
|
|
|
f->prec = 0;
|
|
|
|
continue;
|
|
|
|
}
|
2005-08-08 16:50:13 +04:00
|
|
|
i = -i;
|
|
|
|
f->flags |= FmtLeft;
|
|
|
|
}
|
|
|
|
goto numflag;
|
|
|
|
}
|
|
|
|
n = (*fmtfmt(r))(f);
|
|
|
|
if(n < 0)
|
|
|
|
return nil;
|
|
|
|
if(n == 0)
|
|
|
|
return fmt;
|
|
|
|
}
|
|
|
|
}
|