aboutsummaryrefslogtreecommitdiff
path: root/src/lib9
diff options
context:
space:
mode:
authorrsc <devnull@localhost>2004-12-26 23:07:37 +0000
committerrsc <devnull@localhost>2004-12-26 23:07:37 +0000
commitdf121a0027e6dc08abe6fc907c9af2593a263c17 (patch)
tree46d2da9546978f65073bab77217e3ce5d2dff544 /src/lib9
parenta76c0fd4a9a9a9a64893db614dd146a5819a6e0c (diff)
downloadplan9port-df121a0027e6dc08abe6fc907c9af2593a263c17.tar.gz
plan9port-df121a0027e6dc08abe6fc907c9af2593a263c17.tar.bz2
plan9port-df121a0027e6dc08abe6fc907c9af2593a263c17.zip
import changes from plan 9
Diffstat (limited to 'src/lib9')
-rw-r--r--src/lib9/fmt/dofmt.c2
-rw-r--r--src/lib9/fmt/fltfmt.c33
-rw-r--r--src/lib9/fmt/fmt.c6
-rw-r--r--src/lib9/fmt/fmtdef.h9
-rw-r--r--src/lib9/fmt/fmtlock.c2
-rw-r--r--src/lib9/fmt/fmtprint.c8
-rw-r--r--src/lib9/fmt/fmtquote.c10
-rw-r--r--src/lib9/fmt/fmtstr.c52
-rw-r--r--src/lib9/fmt/fmtvprint.c11
-rw-r--r--src/lib9/fmt/runefmtstr.c52
-rw-r--r--src/lib9/fmt/runevseprint.c4
-rw-r--r--src/lib9/fmt/runevsmprint.c65
-rw-r--r--src/lib9/fmt/runevsnprint.c4
-rw-r--r--src/lib9/fmt/test.c5
-rw-r--r--src/lib9/fmt/vfprint.c4
-rw-r--r--src/lib9/fmt/vseprint.c4
-rw-r--r--src/lib9/fmt/vsmprint.c63
-rw-r--r--src/lib9/fmt/vsnprint.c4
18 files changed, 171 insertions, 167 deletions
diff --git a/src/lib9/fmt/dofmt.c b/src/lib9/fmt/dofmt.c
index 824253bc..1db97738 100644
--- a/src/lib9/fmt/dofmt.c
+++ b/src/lib9/fmt/dofmt.c
@@ -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;
diff --git a/src/lib9/fmt/fltfmt.c b/src/lib9/fmt/fltfmt.c
index 90b86c10..57839ce9 100644
--- a/src/lib9/fmt/fltfmt.c
+++ b/src/lib9/fmt/fltfmt.c
@@ -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];
diff --git a/src/lib9/fmt/fmt.c b/src/lib9/fmt/fmt.c
index f9d8328d..f785d48e 100644
--- a/src/lib9/fmt/fmt.c
+++ b/src/lib9/fmt/fmt.c
@@ -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,
diff --git a/src/lib9/fmt/fmtdef.h b/src/lib9/fmt/fmtdef.h
index bfb2aa97..5a63f9be 100644
--- a/src/lib9/fmt/fmtdef.h
+++ b/src/lib9/fmt/fmtdef.h
@@ -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
+
diff --git a/src/lib9/fmt/fmtlock.c b/src/lib9/fmt/fmtlock.c
index 7501ce41..0ab5a16c 100644
--- a/src/lib9/fmt/fmtlock.c
+++ b/src/lib9/fmt/fmtlock.c
@@ -19,11 +19,9 @@
void
__fmtlock(void)
{
- ;
}
void
__fmtunlock(void)
{
- ;
}
diff --git a/src/lib9/fmt/fmtprint.c b/src/lib9/fmt/fmtprint.c
index 7044d5bd..84a74c8a 100644
--- a/src/lib9/fmt/fmtprint.c
+++ b/src/lib9/fmt/fmtprint.c
@@ -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;
diff --git a/src/lib9/fmt/fmtquote.c b/src/lib9/fmt/fmtquote.c
index b8f00c18..7d659a5a 100644
--- a/src/lib9/fmt/fmtquote.c
+++ b/src/lib9/fmt/fmtquote.c
@@ -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){
diff --git a/src/lib9/fmt/fmtstr.c b/src/lib9/fmt/fmtstr.c
index b4b68185..8568b5d5 100644
--- a/src/lib9/fmt/fmtstr.c
+++ b/src/lib9/fmt/fmtstr.c
@@ -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;
}
diff --git a/src/lib9/fmt/fmtvprint.c b/src/lib9/fmt/fmtvprint.c
index d1176a50..013314c2 100644
--- a/src/lib9/fmt/fmtvprint.c
+++ b/src/lib9/fmt/fmtvprint.c
@@ -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;
diff --git a/src/lib9/fmt/runefmtstr.c b/src/lib9/fmt/runefmtstr.c
index 53376395..b7ff3b5a 100644
--- a/src/lib9/fmt/runefmtstr.c
+++ b/src/lib9/fmt/runefmtstr.c
@@ -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;
}
diff --git a/src/lib9/fmt/runevseprint.c b/src/lib9/fmt/runevseprint.c
index 72f7e1b0..1e69f1b4 100644
--- a/src/lib9/fmt/runevseprint.c
+++ b/src/lib9/fmt/runevseprint.c
@@ -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;
}
diff --git a/src/lib9/fmt/runevsmprint.c b/src/lib9/fmt/runevsmprint.c
index 988e5c02..d1b25f30 100644
--- a/src/lib9/fmt/runevsmprint.c
+++ b/src/lib9/fmt/runevsmprint.c
@@ -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;
}
diff --git a/src/lib9/fmt/runevsnprint.c b/src/lib9/fmt/runevsnprint.c
index 8aff7345..120f5168 100644
--- a/src/lib9/fmt/runevsnprint.c
+++ b/src/lib9/fmt/runevsnprint.c
@@ -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;
}
diff --git a/src/lib9/fmt/test.c b/src/lib9/fmt/test.c
index 650f0f55..d66377ed 100644
--- a/src/lib9/fmt/test.c
+++ b/src/lib9/fmt/test.c
@@ -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;
}
diff --git a/src/lib9/fmt/vfprint.c b/src/lib9/fmt/vfprint.c
index 1df47477..3b026202 100644
--- a/src/lib9/fmt/vfprint.c
+++ b/src/lib9/fmt/vfprint.c
@@ -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;
diff --git a/src/lib9/fmt/vseprint.c b/src/lib9/fmt/vseprint.c
index 9eb674ac..c7c41ab4 100644
--- a/src/lib9/fmt/vseprint.c
+++ b/src/lib9/fmt/vseprint.c
@@ -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;
}
diff --git a/src/lib9/fmt/vsmprint.c b/src/lib9/fmt/vsmprint.c
index 871531ee..b6cf187c 100644
--- a/src/lib9/fmt/vsmprint.c
+++ b/src/lib9/fmt/vsmprint.c
@@ -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);
}
diff --git a/src/lib9/fmt/vsnprint.c b/src/lib9/fmt/vsnprint.c
index ec05d48a..8061f0d7 100644
--- a/src/lib9/fmt/vsnprint.c
+++ b/src/lib9/fmt/vsnprint.c
@@ -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;
}