xref: /plan9-contrib/sys/src/cmd/unix/drawterm/libc/dorfmt.c (revision 0d601874851962e88c6c60fdd2e637bba04e13c2)
1 /*
2  * The authors of this software are Rob Pike and Ken Thompson.
3  *              Copyright (c) 2002 by Lucent Technologies.
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose without fee is hereby granted, provided that this entire notice
6  * is included in all copies of any software which is or includes a copy
7  * or modification of this software and in all copies of the supporting
8  * documentation for such software.
9  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
10  * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE
11  * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
12  * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
13  */
14 #include <u.h>
15 #include <libc.h>
16 #include "fmtdef.h"
17 
18 /* format the output into f->to and return the number of characters fmted  */
19 
20 int
21 dorfmt(Fmt *f, const Rune *fmt)
22 {
23 	Rune *rt, *rs;
24 	int r;
25 	char *t, *s;
26 	int nfmt;
27 
28 	nfmt = f->nfmt;
29 	for(;;){
30 		if(f->runes){
31 			rt = f->to;
32 			rs = f->stop;
33 			while((r = *fmt++) && r != '%'){
34 				FMTRCHAR(f, rt, rs, r);
35 			}
36 			f->nfmt += rt - (Rune *)f->to;
37 			f->to = rt;
38 			if(!r)
39 				return f->nfmt - nfmt;
40 			f->stop = rs;
41 		}else{
42 			t = f->to;
43 			s = f->stop;
44 			while((r = *fmt++) && r != '%'){
45 				FMTRUNE(f, t, f->stop, r);
46 			}
47 			f->nfmt += t - (char *)f->to;
48 			f->to = t;
49 			if(!r)
50 				return f->nfmt - nfmt;
51 			f->stop = s;
52 		}
53 
54 		fmt = __fmtdispatch(f, (Rune*)fmt, 1);
55 		if(fmt == nil)
56 			return -1;
57 	}
58 	return 0;		/* not reached */
59 }
60