137da2899SCharles.Forsyth /*
237da2899SCharles.Forsyth * The authors of this software are Rob Pike and Ken Thompson.
337da2899SCharles.Forsyth * Copyright (c) 2002 by Lucent Technologies.
437da2899SCharles.Forsyth * Permission to use, copy, modify, and distribute this software for any
537da2899SCharles.Forsyth * purpose without fee is hereby granted, provided that this entire notice
637da2899SCharles.Forsyth * is included in all copies of any software which is or includes a copy
737da2899SCharles.Forsyth * or modification of this software and in all copies of the supporting
837da2899SCharles.Forsyth * documentation for such software.
937da2899SCharles.Forsyth * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
1037da2899SCharles.Forsyth * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE ANY
1137da2899SCharles.Forsyth * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
1237da2899SCharles.Forsyth * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
1337da2899SCharles.Forsyth */
1437da2899SCharles.Forsyth #include "lib9.h"
1537da2899SCharles.Forsyth #include "fmtdef.h"
1637da2899SCharles.Forsyth
1737da2899SCharles.Forsyth static int
fmtStrFlush(Fmt * f)1837da2899SCharles.Forsyth fmtStrFlush(Fmt *f)
1937da2899SCharles.Forsyth {
2037da2899SCharles.Forsyth char *s;
2137da2899SCharles.Forsyth int n;
2237da2899SCharles.Forsyth
23*3cd4f1d1SCharles Forsyth if(f->start == nil)
24*3cd4f1d1SCharles Forsyth return 0;
2537da2899SCharles.Forsyth n = (int)f->farg;
2637da2899SCharles.Forsyth n += 256;
2737da2899SCharles.Forsyth f->farg = (void*)n;
2837da2899SCharles.Forsyth s = f->start;
2937da2899SCharles.Forsyth f->start = realloc(s, n);
3037da2899SCharles.Forsyth if(f->start == nil){
3137da2899SCharles.Forsyth free(s);
3237da2899SCharles.Forsyth f->to = nil;
3337da2899SCharles.Forsyth f->stop = nil;
3437da2899SCharles.Forsyth return 0;
3537da2899SCharles.Forsyth }
3637da2899SCharles.Forsyth f->to = (char*)f->start + ((char*)f->to - s);
3737da2899SCharles.Forsyth f->stop = (char*)f->start + n - 1;
3837da2899SCharles.Forsyth return 1;
3937da2899SCharles.Forsyth }
4037da2899SCharles.Forsyth
4137da2899SCharles.Forsyth int
fmtstrinit(Fmt * f)4237da2899SCharles.Forsyth fmtstrinit(Fmt *f)
4337da2899SCharles.Forsyth {
4437da2899SCharles.Forsyth int n;
4537da2899SCharles.Forsyth
46*3cd4f1d1SCharles Forsyth memset(f, 0, sizeof(*f));
4737da2899SCharles.Forsyth n = 32;
4837da2899SCharles.Forsyth f->start = malloc(n);
4937da2899SCharles.Forsyth if(f->start == nil)
5037da2899SCharles.Forsyth return -1;
5137da2899SCharles.Forsyth f->to = f->start;
5237da2899SCharles.Forsyth f->stop = (char*)f->start + n - 1;
5337da2899SCharles.Forsyth f->flush = fmtStrFlush;
5437da2899SCharles.Forsyth f->farg = (void*)n;
5537da2899SCharles.Forsyth f->nfmt = 0;
5637da2899SCharles.Forsyth return 0;
5737da2899SCharles.Forsyth }
5837da2899SCharles.Forsyth
5937da2899SCharles.Forsyth /*
6037da2899SCharles.Forsyth * print into an allocated string buffer
6137da2899SCharles.Forsyth */
6237da2899SCharles.Forsyth char*
vsmprint(char * fmt,va_list args)6337da2899SCharles.Forsyth vsmprint(char *fmt, va_list args)
6437da2899SCharles.Forsyth {
6537da2899SCharles.Forsyth Fmt f;
6637da2899SCharles.Forsyth int n;
6737da2899SCharles.Forsyth
6837da2899SCharles.Forsyth if(fmtstrinit(&f) < 0)
6937da2899SCharles.Forsyth return nil;
70*3cd4f1d1SCharles Forsyth va_copy(f.args, args);
7137da2899SCharles.Forsyth n = dofmt(&f, fmt);
72*3cd4f1d1SCharles Forsyth va_end(f.args);
73*3cd4f1d1SCharles Forsyth if(n < 0){
74*3cd4f1d1SCharles Forsyth free(f.start);
75*3cd4f1d1SCharles Forsyth f.start = nil;
7637da2899SCharles.Forsyth return nil;
77*3cd4f1d1SCharles Forsyth }
78*3cd4f1d1SCharles Forsyth return fmtstrflush(&f);
7937da2899SCharles.Forsyth }
80