dorfmt.c (1673B)
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 <stdarg.h> 15 #include <string.h> 16 #include "plan9.h" 17 #include "fmt.h" 18 #include "fmtdef.h" 19 20 /* format the output into f->to and return the number of characters fmted */ 21 22 /* BUG: THIS FILE IS NOT UPDATED TO THE NEW SPEC */ 23 int 24 dorfmt(Fmt *f, const Rune *fmt) 25 { 26 Rune *rt, *rs; 27 int r; 28 char *t, *s; 29 int nfmt; 30 31 nfmt = f->nfmt; 32 for(;;){ 33 if(f->runes){ 34 rt = (Rune*)f->to; 35 rs = (Rune*)f->stop; 36 while((r = *fmt++) && r != '%'){ 37 FMTRCHAR(f, rt, rs, r); 38 } 39 f->nfmt += rt - (Rune *)f->to; 40 f->to = rt; 41 if(!r) 42 return f->nfmt - nfmt; 43 f->stop = rs; 44 }else{ 45 t = (char*)f->to; 46 s = (char*)f->stop; 47 while((r = *fmt++) && r != '%'){ 48 FMTRUNE(f, t, f->stop, r); 49 } 50 f->nfmt += t - (char *)f->to; 51 f->to = t; 52 if(!r) 53 return f->nfmt - nfmt; 54 f->stop = s; 55 } 56 57 fmt = (Rune*)__fmtdispatch(f, (Rune*)fmt, 1); 58 if(fmt == nil) 59 return -1; 60 } 61 return 0; /* not reached */ 62 }