2 91c13e54 2004-02-29 devnull * The authors of this software are Rob Pike and Ken Thompson.
3 91c13e54 2004-02-29 devnull * Copyright (c) 2002 by Lucent Technologies.
4 91c13e54 2004-02-29 devnull * Permission to use, copy, modify, and distribute this software for any
5 91c13e54 2004-02-29 devnull * purpose without fee is hereby granted, provided that this entire notice
6 91c13e54 2004-02-29 devnull * is included in all copies of any software which is or includes a copy
7 91c13e54 2004-02-29 devnull * or modification of this software and in all copies of the supporting
8 91c13e54 2004-02-29 devnull * documentation for such software.
9 91c13e54 2004-02-29 devnull * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
10 c90dd38f 2004-12-29 devnull * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE
11 c90dd38f 2004-12-29 devnull * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
12 91c13e54 2004-02-29 devnull * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
15 df121a00 2004-12-26 devnull * Plan 9 port version must include libc.h in order to
16 df121a00 2004-12-26 devnull * get Plan 9 debugging malloc, which sometimes returns
17 df121a00 2004-12-26 devnull * different pointers than the standard malloc.
19 df121a00 2004-12-26 devnull #ifdef PLAN9PORT
20 df121a00 2004-12-26 devnull #include <u.h>
21 df121a00 2004-12-26 devnull #include <libc.h>
22 eb7f99c5 2004-12-27 devnull #include "fmtdef.h"
24 91c13e54 2004-02-29 devnull #include <stdlib.h>
25 78e51a8c 2005-01-14 devnull #include <string.h>
26 e5aa96ac 2004-12-26 devnull #include "plan9.h"
27 91c13e54 2004-02-29 devnull #include "fmt.h"
28 91c13e54 2004-02-29 devnull #include "fmtdef.h"
31 df121a00 2004-12-26 devnull static int
32 df121a00 2004-12-26 devnull runeFmtStrFlush(Fmt *f)
37 df121a00 2004-12-26 devnull if(f->start == nil)
38 df121a00 2004-12-26 devnull return 0;
39 df121a00 2004-12-26 devnull n = (int)f->farg;
41 df121a00 2004-12-26 devnull s = (Rune*)f->start;
42 df121a00 2004-12-26 devnull f->start = realloc(s, sizeof(Rune)*n);
43 df121a00 2004-12-26 devnull if(f->start == nil){
44 df121a00 2004-12-26 devnull f->farg = nil;
45 df121a00 2004-12-26 devnull f->to = nil;
46 df121a00 2004-12-26 devnull f->stop = nil;
48 df121a00 2004-12-26 devnull return 0;
50 df121a00 2004-12-26 devnull f->farg = (void*)n;
51 df121a00 2004-12-26 devnull f->to = (Rune*)f->start + ((Rune*)f->to - s);
52 df121a00 2004-12-26 devnull f->stop = (Rune*)f->start + n - 1;
53 df121a00 2004-12-26 devnull return 1;
57 df121a00 2004-12-26 devnull runefmtstrinit(Fmt *f)
61 df121a00 2004-12-26 devnull memset(f, 0, sizeof *f);
62 df121a00 2004-12-26 devnull f->runes = 1;
64 df121a00 2004-12-26 devnull f->start = malloc(sizeof(Rune)*n);
65 df121a00 2004-12-26 devnull if(f->start == nil)
66 df121a00 2004-12-26 devnull return -1;
67 df121a00 2004-12-26 devnull f->to = f->start;
68 df121a00 2004-12-26 devnull f->stop = (Rune*)f->start + n - 1;
69 df121a00 2004-12-26 devnull f->flush = runeFmtStrFlush;
70 df121a00 2004-12-26 devnull f->farg = (void*)n;
71 df121a00 2004-12-26 devnull f->nfmt = 0;
72 df121a00 2004-12-26 devnull return 0;
76 91c13e54 2004-02-29 devnull * print into an allocated string buffer
79 91c13e54 2004-02-29 devnull runevsmprint(char *fmt, va_list args)
84 91c13e54 2004-02-29 devnull if(runefmtstrinit(&f) < 0)
85 91c13e54 2004-02-29 devnull return nil;
86 df121a00 2004-12-26 devnull VA_COPY(f.args,args);
87 91c13e54 2004-02-29 devnull n = dofmt(&f, fmt);
88 df121a00 2004-12-26 devnull VA_END(f.args);
89 df121a00 2004-12-26 devnull if(f.start == nil)
90 91c13e54 2004-02-29 devnull return nil;
91 df121a00 2004-12-26 devnull if(n < 0){
92 df121a00 2004-12-26 devnull free(f.start);
93 df121a00 2004-12-26 devnull return nil;
95 91c13e54 2004-02-29 devnull *(Rune*)f.to = '\0';
96 91c13e54 2004-02-29 devnull return (Rune*)f.start;