xref: /titanic_44/usr/src/lib/libast/common/sfio/sfvprintf.c (revision 8339b41da2395f0525c46ceedfeb01961893ec44)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #include	"sfhdr.h"
23 
24 /*	The engine for formatting data.
25 **	1. Argument positioning is done in sftable.c so any changes
26 **	   made here should be reflected in sftable.c as well.
27 **	2. For internationalization, Sfio only supports I/O of multibyte strings.
28 **	   However, this code does provide minimal support so that Stdio functions
29 **	   such as fwprintf/swprintf can be emulated (see stdvwprintf()).
30 **
31 **	Written by Kiem-Phong Vo.
32 */
33 #define HIGHBITI	(~((~((uint)0)) >> 1))
34 #define HIGHBITL	(~((~((Sfulong_t)0)) >> 1))
35 
36 #define SFFMT_PREFIX	(SFFMT_MINUS|SFFMT_SIGN|SFFMT_BLANK)
37 
38 #define FPRECIS		6	/* default precision for floats 	*/
39 
40 #if _PACKAGE_ast
41 #include <ccode.h>
42 #else
43 /* characters when using ebcdic or ascii */
44 #if _chr_ebcdic
45 #define CC_vt			013	/* vertical tab	*/
46 #define CC_esc			047	/* escape	*/
47 #define CC_bel			057	/* bell		*/
48 #else
49 #define CC_vt			013	/* vertical tab	*/
50 #define CC_esc			033	/* escape	*/
51 #define CC_bel			007	/* bell		*/
52 #endif /* _chr_ebcdic */
53 #endif /* _PACKAGE_ast */
54 
55 #if __STD_C
56 static int chr2str(char* buf, int v)
57 #else
58 static int chr2str(buf, v)
59 char*	buf;
60 int	v;
61 #endif
62 {
63 	if(isprint(v) && v != '\\')
64 	{	*buf++ = v;
65 		return 1;
66 	}
67 	else
68 	{	*buf++ = '\\';
69 		switch(v)
70 		{ case CC_bel:	*buf++ = 'a'; return 2;
71 		  case CC_vt:	*buf++ = 'v'; return 2;
72 		  case CC_esc:	*buf++ = 'E'; return 2;
73 		  case '\b':	*buf++ = 'b'; return 2;
74 		  case '\f':	*buf++ = 'f'; return 2;
75 		  case '\n':	*buf++ = 'n'; return 2;
76 		  case '\r':	*buf++ = 'r'; return 2;
77 		  case '\t':	*buf++ = 't'; return 2;
78 		  case '\\':	*buf++ = '\\'; return 2;
79 		  default:	*buf++ = '0' + ((v >> 6) & 03);
80 				*buf++ = '0' + ((v >> 3) & 07);
81 				*buf++ = '0' + ((v >> 0) & 07);
82 				return 4;
83 		}
84 	}
85 }
86 
87 /* On some platform(s), large functions are not compilable.
88 ** In such a case, the below macro should be defined non-zero so that
89 ** some in-lined macros will be made smaller, trading time for space.
90 */
91 #if !defined(_sffmt_small) && defined(_UTS)
92 #define _sffmt_small	1
93 #endif
94 
95 #if __STD_C
96 int sfvprintf(Sfio_t* f, const char* form, va_list args)
97 #else
98 int sfvprintf(f,form,args)
99 Sfio_t*	f;		/* file to print to	*/
100 char*	form;		/* format to use	*/
101 va_list	args;		/* arg list if !argf	*/
102 #endif
103 {
104 	int		n, v, w, k, n_s, base, fmt, flags;
105 	Sflong_t	lv;
106 	char		*sp, *ssp, *endsp, *ep, *endep;
107 	int		dot, width, precis, sign, decpt;
108 #if _PACKAGE_ast
109 	int		scale;
110 #endif
111 	ssize_t		size;
112 	Sfdouble_t	dval;
113 	Void_t*		valp;
114 	char		*tls[2], **ls;	/* for %..[separ]s		*/
115 	char*		t_str;		/* stuff between ()		*/
116 	ssize_t		n_str;		/* its length			*/
117 
118 	Argv_t		argv;		/* for extf to return value	*/
119 	Sffmt_t		*ft;		/* format environment		*/
120 	Fmt_t		*fm, *fmstk;	/* stack contexts		*/
121 
122 	char*		oform;		/* original format string	*/
123 	va_list		oargs;		/* original arg list		*/
124 	Fmtpos_t*	fp;		/* arg position list		*/
125 	int		argp, argn;	/* arg position and number	*/
126 
127 #define SLACK		1024
128 	char		buf[SF_MAXDIGITS+SLACK], tmp[SF_MAXDIGITS+1], data[SF_GRAIN];
129 	int		decimal = 0, thousand = 0;
130 
131 #if _has_multibyte
132 	wchar_t*	wsp;
133 	SFMBDCL(fmbs)			/* state of format string	*/
134 	SFMBDCL(mbs)			/* state of some string		*/
135 #ifdef mbwidth
136 	char*		osp;
137 	int		n_w, wc;
138 #endif
139 #endif
140 
141 	/* local io system */
142 	int		o, n_output;
143 #define SMputc(f,c)	{ if((o = SFFLSBUF(f,c)) >= 0 ) n_output += 1; \
144 			  else		{ SFBUF(f); goto done; } \
145 			}
146 #define SMnputc(f,c,n)	{ if((o = SFNPUTC(f,c,n)) > 0 ) n_output += 1; \
147 			  if(o != n)	{ SFBUF(f); goto done; } \
148 			}
149 #define SMwrite(f,s,n)	{ if((o = SFWRITE(f,(Void_t*)s,n)) > 0 ) n_output += o; \
150 			  if(o != n)	{ SFBUF(f); goto done; } \
151 			}
152 #if _sffmt_small /* these macros are made smaller at some performance cost */
153 #define SFBUF(f)
154 #define SFINIT(f)	(n_output = 0)
155 #define SFEND(f)
156 #define SFputc(f,c)	SMputc(f,c)
157 #define SFnputc(f,c,n)	SMnputc(f,c,n)
158 #define SFwrite(f,s,n)	SMwrite(f,s,n)
159 #else
160 	uchar	*d, *endd;
161 #define SFBUF(f)	(d = f->next, endd = f->endb)
162 #define SFINIT(f)	(SFBUF(f), n_output = 0)
163 #define SFEND(f)	((n_output += d - f->next), (f->next = d))
164 #define SFputc(f,c)	{ if(d < endd) 	  { *d++ = (uchar)c; } \
165 	  		  else		  { SFEND(f); SMputc(f,c); SFBUF(f); } \
166 	  		}
167 #define SFnputc(f,c,n)	{ if(d+n <= endd) { while(n--) *d++ = (uchar)(c); } \
168 			  else		  { SFEND(f); SMnputc(f,c,n); SFBUF(f); } \
169 			}
170 #define SFwrite(f,s,n)	{ if(d+n <= endd) { while(n--) *d++ = (uchar)(*s++); } \
171 			  else 		  { SFEND(f); SMwrite(f,s,n); SFBUF(f); } \
172 			}
173 #endif /* _sffmt_small */
174 
175 	SFMTXDECL(f);
176 
177 	SFCVINIT();	/* initialize conversion tables */
178 
179 	SFMTXENTER(f,-1);
180 
181 	if(!form)
182 		SFMTXRETURN(f, -1);
183 
184 	/* make sure stream is in write mode and buffer is not NULL */
185 	if(f->mode != SF_WRITE && _sfmode(f,SF_WRITE,0) < 0)
186 		SFMTXRETURN(f, -1);
187 
188 	SFLOCK(f,0);
189 
190 	if(!f->data && !(f->flags&SF_STRING))
191 	{	f->data = f->next = (uchar*)data;
192 		f->endb = f->data+sizeof(data);
193 	}
194 	SFINIT(f);
195 
196 	tls[1] = NIL(char*);
197 
198 	fmstk = NIL(Fmt_t*);
199 	ft = NIL(Sffmt_t*);
200 
201 	oform = (char*)form;
202 	va_copy(oargs,args);
203 	argn = -1;
204 	fp = NIL(Fmtpos_t*);
205 
206 loop_fmt :
207 	SFMBCLR(&fmbs); /* clear multibyte states to parse the format string */
208 	while((n = *form) )
209 	{	if(n != '%') /* collect the non-pattern chars */
210 		{	sp = (char*)form;
211 			do
212 			{	if((n = SFMBLEN(form, &fmbs)) <= 0)
213 				{	n = 1;
214 					SFMBCLR(&fmbs);
215 				}
216 			} while(*(form += n) && *form != '%');
217 
218 			n = form-sp;
219 			SFwrite(f,sp,n);
220 			continue;
221 		}
222 		else	form += 1;
223 
224 		flags = 0;
225 #if _PACKAGE_ast
226 		scale = 0;
227 #endif
228 		size = width = precis = base = n_s = argp = -1;
229 		ssp = _Sfdigits;
230 		endep = ep = NIL(char*);
231 		endsp = sp = buf+(sizeof(buf)-1);
232 		t_str = NIL(char*);
233 		n_str = dot = 0;
234 
235 	loop_flags:	/* LOOP FOR \0, %, FLAGS, WIDTH, PRECISION, BASE, TYPE */
236 		switch((fmt = *form++) )
237 		{
238 		case '\0':
239 			SFputc(f,'%');
240 			goto pop_fmt;
241 		case '%' :
242 			SFputc(f,'%');
243 			continue;
244 
245 		case LEFTP : /* get the type enclosed in balanced parens */
246 			t_str = (char*)form;
247 			for(v = 1;;)
248 			{	switch(*form++)
249 				{
250 				case 0 :	/* not balancable, retract */
251 					form = t_str;
252 					t_str = NIL(char*);
253 					n_str = 0;
254 					goto loop_flags;
255 				case LEFTP :	/* increasing nested level */
256 					v += 1;
257 					continue;
258 				case RIGHTP :	/* decreasing nested level */
259 					if((v -= 1) != 0)
260 						continue;
261 					if(*t_str != '*' )
262 						n_str = (form-1)-t_str;
263 					else
264 					{	t_str = (*_Sffmtintf)(t_str+1,&n);
265 						if(*t_str == '$')
266 						{	if(!fp &&
267 							   !(fp = (*_Sffmtposf)(f,oform,oargs,ft,0)) )
268 								goto pop_fmt;
269 							n = FP_SET(n,argn);
270 						}
271 						else	n = FP_SET(-1,argn);
272 
273 						if(fp)
274 						{	t_str = fp[n].argv.s;
275 							n_str = fp[n].ft.size;
276 						}
277 						else if(ft && ft->extf )
278 						{	FMTSET(ft, form,args,
279 								LEFTP, 0, 0, 0,0,0,
280 								NIL(char*),0);
281 							n = (*ft->extf)
282 							      (f,(Void_t*)&argv,ft);
283 							if(n < 0)
284 								goto pop_fmt;
285 							if(!(ft->flags&SFFMT_VALUE) )
286 								goto t_arg;
287 							if((t_str = argv.s) &&
288 							   (n_str = (int)ft->size) < 0)
289 								n_str = strlen(t_str);
290 						}
291 						else
292 						{ t_arg:
293 							if((t_str = va_arg(args,char*)) )
294 								n_str = strlen(t_str);
295 						}
296 					}
297 					goto loop_flags;
298 				}
299 			}
300 
301 		case '-' :
302 			flags = (flags & ~(SFFMT_CENTER|SFFMT_ZERO)) | SFFMT_LEFT;
303 			goto loop_flags;
304 		case '0' :
305 			if(!(flags&(SFFMT_LEFT|SFFMT_CENTER)) )
306 				flags |= SFFMT_ZERO;
307 			goto loop_flags;
308 		case ' ' :
309 			if(!(flags&SFFMT_SIGN) )
310 				flags |= SFFMT_BLANK;
311 			goto loop_flags;
312 		case '+' :
313 			flags = (flags & ~SFFMT_BLANK) | SFFMT_SIGN;
314 			goto loop_flags;
315 		case '=' :
316 			flags = (flags & ~(SFFMT_LEFT|SFFMT_ZERO)) | SFFMT_CENTER;
317 			goto loop_flags;
318 		case '#' :
319 			flags |= SFFMT_ALTER;
320 			goto loop_flags;
321 		case QUOTE:
322 			SFSETLOCALE(&decimal,&thousand);
323 			if(thousand > 0)
324 				flags |= SFFMT_THOUSAND;
325 			goto loop_flags;
326 		case ',':
327 			SFSETLOCALE(&decimal,&thousand);
328 			if(thousand < 0)
329 				thousand = fmt;
330 			flags |= SFFMT_THOUSAND;
331 			goto loop_flags;
332 
333 		case '.' :
334 			dot += 1;
335 			if(dot == 1)
336 			{	/* so base can be defined without setting precis */
337 				if(*form != '.')
338 					precis = 0;
339 			}
340 			else if(dot == 2)
341 			{	base = 0; /* for %s,%c */
342 				v = form[0] == 'l' ? form[1] : form[0];
343 				if(v == 'c' || v == 'C' || v == 's' || v == 'S')
344 					goto loop_flags;
345 				if(*form && !isalnum(*form))
346 				{	v = form[1] == 'l' ? form[2] : form[1];
347 					if(v == 'c' || v == 'C' || v == 's' || v == 'S')
348 					{	if(*form == '*')
349 							goto do_star;
350 						else
351 						{	base = *form++;
352 							goto loop_flags;
353 						}
354 					}
355 				}
356 			}
357 
358 			if(isdigit(*form) )
359 			{	fmt = *form++;
360 				goto dot_size;
361 			}
362 			else if(*form != '*')
363 				goto loop_flags;
364 		do_star:
365 			form += 1; /* fall thru for '*' */
366 		case '*' :
367 			form = (*_Sffmtintf)(form,&n);
368 			if(*form == '$')
369 			{	form += 1;
370 				if(!fp && !(fp = (*_Sffmtposf)(f,oform,oargs,ft,0)) )
371 					goto pop_fmt;
372 				n = FP_SET(n,argn);
373 			}
374 			else	n = FP_SET(-1,argn);
375 
376 			if(fp)
377 				v = fp[n].argv.i;
378 			else if(ft && ft->extf)
379 			{	FMTSET(ft, form,args, '.',dot, 0, 0,0,0, NIL(char*), 0);
380 				if((*ft->extf)(f, (Void_t*)(&argv), ft) < 0)
381 					goto pop_fmt;
382 				fmt = ft->fmt;
383 				flags = (flags&~SFFMT_TYPES) | (ft->flags&SFFMT_TYPES);
384 				if(ft->flags&SFFMT_VALUE)
385 					v = argv.i;
386 				else	v = (dot <= 2) ? va_arg(args,int) : 0;
387 			}
388 			else	v = dot <= 2 ? va_arg(args,int) : 0;
389 			goto dot_set;
390 
391 		case '1' : case '2' : case '3' :
392 		case '4' : case '5' : case '6' :
393 		case '7' : case '8' : case '9' :
394 		dot_size :
395 			for(v = fmt - '0'; isdigit(*form); ++form)
396 				v = v*10 + (*form - '0');
397 			if(*form == '$')
398 			{	form += 1;
399 				if(!fp && !(fp = (*_Sffmtposf)(f,oform,oargs,ft,0)) )
400 					goto pop_fmt;
401 				argp = v-1;
402 				goto loop_flags;
403 			}
404 		dot_set :
405 			if(dot == 0)
406 			{	if((width = v) < 0)
407 				{	width = -width;
408 					flags = (flags & ~(SFFMT_CENTER|SFFMT_ZERO)) | SFFMT_LEFT;
409 				}
410 			}
411 			else if(dot == 1)
412 				precis = v;
413 			else if(dot == 2)
414 				base = v;
415 			goto loop_flags;
416 
417 		case 'I' : /* object length */
418 			size = -1; flags = (flags & ~SFFMT_TYPES) | SFFMT_IFLAG;
419 			if(isdigit(*form) )
420 			{	for(size = 0, n = *form; isdigit(n); n = *++form)
421 					size = size*10 + (n - '0');
422 			}
423 			else if(*form == '*')
424 			{	form = (*_Sffmtintf)(form+1,&n);
425 				if(*form == '$')
426 				{	form += 1;
427 					if(!fp &&
428 					   !(fp = (*_Sffmtposf)(f,oform,oargs,ft,0)))
429 						goto pop_fmt;
430 					n = FP_SET(n,argn);
431 				}
432 				else	n = FP_SET(-1,argn);
433 
434 				if(fp)	/* use position list */
435 					size = fp[n].argv.i;
436 				else if(ft && ft->extf)
437 				{	FMTSET(ft, form,args, 'I',sizeof(int), 0, 0,0,0,
438 						NIL(char*), 0);
439 					if((*ft->extf)(f, (Void_t*)(&argv), ft) < 0)
440 						goto pop_fmt;
441 					if(ft->flags&SFFMT_VALUE)
442 						size = argv.i;
443 					else	size = va_arg(args,int);
444 				}
445 				else	size = va_arg(args,int);
446 			}
447 			goto loop_flags;
448 
449 		case 'l' :
450 			size = -1; flags &= ~SFFMT_TYPES;
451 			if(*form == 'l')
452 			{	form += 1;
453 				flags |= SFFMT_LLONG;
454 			}
455 			else	flags |= SFFMT_LONG;
456 			goto loop_flags;
457 		case 'h' :
458 			size = -1; flags &= ~SFFMT_TYPES;
459 			if(*form == 'h')
460 			{	form += 1;
461 				flags |= SFFMT_SSHORT;
462 			}
463 			else	flags |= SFFMT_SHORT;
464 			goto loop_flags;
465 		case 'L' :
466 			size = -1; flags = (flags & ~SFFMT_TYPES) | SFFMT_LDOUBLE;
467 			goto loop_flags;
468 
469 		case 'j' :
470 			size = -1; flags = (flags&~SFFMT_TYPES) | SFFMT_JFLAG;
471 			goto loop_flags;
472 		case 'z' :
473 			size = -1; flags = (flags&~SFFMT_TYPES) | SFFMT_ZFLAG;
474 			goto loop_flags;
475 		case 't' :
476 			size = -1; flags = (flags&~SFFMT_TYPES) | SFFMT_TFLAG;
477 			goto loop_flags;
478 		default:
479 			break;
480 		}
481 
482 		/* set object size for scalars */
483 		if(flags & SFFMT_TYPES)
484 		{	if((_Sftype[fmt]&(SFFMT_INT|SFFMT_UINT)) || fmt == 'n')
485 			{	if(flags&SFFMT_LONG)
486 					size = sizeof(long);
487 				else if(flags&SFFMT_SHORT)
488 					size = sizeof(short);
489 				else if(flags&SFFMT_SSHORT)
490 					size = sizeof(char);
491 				else if(flags&SFFMT_TFLAG)
492 					size = sizeof(ptrdiff_t);
493 				else if(flags&SFFMT_ZFLAG)
494 					size = sizeof(size_t);
495 				else if(flags&(SFFMT_LLONG|SFFMT_JFLAG) )
496 					size = sizeof(Sflong_t);
497 				else if(flags&SFFMT_IFLAG)
498 				{	if(size <= 0 ||
499 					   size == sizeof(Sflong_t)*CHAR_BIT )
500 						size = sizeof(Sflong_t);
501 				}
502 				else if(size < 0)
503 					size = sizeof(int);
504 			}
505 			else if(_Sftype[fmt]&SFFMT_FLOAT)
506 			{	if(flags&SFFMT_LDOUBLE)
507 					size = sizeof(Sfdouble_t);
508 				else if(flags&(SFFMT_LONG|SFFMT_LLONG))
509 					size = sizeof(double);
510 				else if(flags&SFFMT_IFLAG)
511 				{	if(size <= 0)
512 						size = sizeof(Sfdouble_t);
513 				}
514 				else if(size < 0)
515 					size = sizeof(float);
516 			}
517 			else if(_Sftype[fmt]&SFFMT_CHAR)
518 			{
519 #if _has_multibyte
520 				if((flags&SFFMT_LONG) || fmt == 'C')
521 				{	size = sizeof(wchar_t) > sizeof(int) ?
522 						sizeof(wchar_t) : sizeof(int);
523 				} else
524 #endif
525 				if(size < 0)
526 					size = sizeof(int);
527 			}
528 		}
529 
530 		argp = FP_SET(argp,argn);
531 		if(fp)
532 		{	if(ft && ft->extf && fp[argp].ft.fmt != fp[argp].fmt)
533 				fmt = fp[argp].ft.fmt;
534 			argv = fp[argp].argv;
535 			size = fp[argp].ft.size;
536 		}
537 		else if(ft && ft->extf )	/* extended processing */
538 		{	FMTSET(ft, form,args, fmt, size,flags, width,precis,base,
539 				t_str,n_str);
540 			SFEND(f); SFOPEN(f,0);
541 			v = (*ft->extf)(f, (Void_t*)(&argv), ft);
542 			SFLOCK(f,0); SFBUF(f);
543 
544 			if(v < 0)	/* no further processing */
545 				goto pop_fmt;
546 			else if(v > 0)	/* extf output v bytes */
547 			{	n_output += v;
548 				continue;
549 			}
550 			else		/* extf did not output */
551 			{	FMTGET(ft, form,args, fmt, size,flags, width,precis,base);
552 
553 				if(!(ft->flags&SFFMT_VALUE))
554 					goto arg_list;
555 				else if(_Sftype[fmt]&(SFFMT_INT|SFFMT_UINT) )
556 				{	if(size == sizeof(short))
557 					{	if(_Sftype[fmt]&SFFMT_INT)
558 							argv.i = argv.h;
559 						else	argv.i = argv.uh;
560 					}
561 					else if(size == sizeof(char))
562 					{	if(_Sftype[fmt]&SFFMT_INT)
563 							argv.i = argv.c;
564 						else	argv.i = argv.uc;
565 					}
566 				}
567 				else if(_Sftype[fmt]&SFFMT_FLOAT )
568 				{	if(size == sizeof(float) )
569 						argv.d = argv.f;
570 				}
571 				else if(_Sftype[fmt]&SFFMT_CHAR)
572 				{	if(base < 0)
573 						argv.i = (int)argv.c;
574 				}
575 			}
576 		}
577 		else
578 		{ arg_list:
579 			switch(_Sftype[fmt])
580 			{ case SFFMT_INT:
581 			  case SFFMT_UINT:
582 #if !_ast_intmax_long
583 				if(size == sizeof(Sflong_t))
584 					argv.ll = va_arg(args, Sflong_t);
585 				else
586 #endif
587 				if(size == sizeof(long) )
588 					argv.l = va_arg(args, long);
589 				else	argv.i = va_arg(args, int);
590 				break;
591 			 case SFFMT_FLOAT:
592 #if !_ast_fltmax_double
593 				if(size == sizeof(Sfdouble_t))
594 					argv.ld = va_arg(args,Sfdouble_t);
595 				else
596 #endif
597 					argv.d  = va_arg(args,double);
598 				break;
599 			 case SFFMT_POINTER:
600 					argv.vp = va_arg(args,Void_t*);
601 				break;
602 			 case SFFMT_CHAR:
603 				if(base >= 0)
604 					argv.s = va_arg(args,char*);
605 #if _has_multibyte
606 				else if((flags & SFFMT_LONG) || fmt == 'C')
607 				{	if(sizeof(wchar_t) <= sizeof(uint) )
608 						argv.wc = (wchar_t)va_arg(args,uint);
609 					else	argv.wc = va_arg(args,wchar_t);
610 				}
611 #endif
612 				else	argv.i = va_arg(args,int);
613 				break;
614 			 default: /* unknown pattern */
615 				break;
616 			}
617 		}
618 
619 		switch(fmt)	/* PRINTF DIRECTIVES */
620 		{
621 		default :	/* unknown directive */
622 			form -= 1;
623 			argn -= 1;
624 			continue;
625 
626 		case '!' :	/* stacking a new environment */
627 			if(!fp)
628 				fp = (*_Sffmtposf)(f,oform,oargs,ft,0);
629 			else	goto pop_fmt;
630 
631 			if(!argv.ft)
632 				goto pop_fmt;
633 			if(!argv.ft->form && ft ) /* change extension functions */
634 			{	if(ft->eventf &&
635 				   (*ft->eventf)(f,SF_DPOP,(Void_t*)form,ft) < 0)
636 					continue;
637 				fmstk->ft = ft = argv.ft;
638 			}
639 			else			/* stack a new environment */
640 			{	if(!(fm = (Fmt_t*)malloc(sizeof(Fmt_t))) )
641 					goto done;
642 
643 				ft = fm->ft = argv.ft;
644 				SFMBSET(ft->mbs, &fmbs);
645 				if(ft->form)
646 				{	fm->form = (char*)form; SFMBCPY(&fm->mbs,&fmbs);
647 					va_copy(fm->args,args);
648 
649 					fm->oform = oform;
650 					va_copy(fm->oargs,oargs);
651 					fm->argn = argn;
652 					fm->fp = fp;
653 
654 					form = ft->form; SFMBCLR(ft->mbs);
655 					va_copy(args,ft->args);
656 					argn = -1;
657 					fp = NIL(Fmtpos_t*);
658 					oform = (char*)form;
659 					va_copy(oargs,args);
660 				}
661 				else	fm->form = NIL(char*);
662 
663 				fm->eventf = ft->eventf;
664 				fm->next = fmstk;
665 				fmstk = fm;
666 			}
667 			continue;
668 
669 		case 'S':
670 			flags = (flags & ~(SFFMT_TYPES|SFFMT_LDOUBLE)) | SFFMT_LONG;
671 		case 's':
672 #if _has_multibyte && defined(mbwidth)
673 			wc = (flags & SFFMT_LDOUBLE) && mbwide();
674 #endif
675 			if(base >= 0)	/* list of strings */
676 			{	if(!(ls = argv.sp) || !ls[0])
677 					continue;
678 			}
679 			else
680 			{	if(!(sp = argv.s))
681 				{	sp = "(null)";
682 					flags &= ~SFFMT_LONG;
683 				}
684 #if _PACKAGE_ast
685 		str_cvt:
686 				if(scale)
687 				{	size = base = -1;
688 					flags &= ~SFFMT_LONG;
689 				}
690 #endif
691 				ls = tls; tls[0] = sp;
692 			}
693 			for(sp = *ls;;)
694 			{	/* v: number of bytes  w: print width of those v bytes */
695 #if _has_multibyte
696 				if(flags & SFFMT_LONG)
697 				{	v = 0;
698 #ifdef mbwidth
699 					w = 0;
700 #endif
701 					SFMBCLR(&mbs);
702 					for(n = 0, wsp = (wchar_t*)sp;; ++wsp, ++n)
703 					{	if((size >= 0 && n >= size) ||
704 						   (size <  0 && *wsp == 0) )
705 							break;
706 						if((n_s = wcrtomb(buf, *wsp, &mbs)) <= 0)
707 							break;
708 #ifdef mbwidth
709 						if(wc)
710 						{	n_w = mbwidth(*wsp);
711 							if(precis >= 0 && (w+n_w) > precis )
712 								break;
713 							w += n_w;
714 						}
715 						else
716 #endif
717 						if(precis >= 0 && (v+n_s) > precis )
718 							break;
719 						v += n_s;
720 					}
721 #ifdef mbwidth
722 					if(!wc)
723 						w = v;
724 #endif
725 				}
726 #if _has_multibyte && defined(mbwidth)
727 				else if (wc)
728 				{	w = 0;
729 					SFMBCLR(&mbs);
730 					ssp = sp;
731 					for(;;)
732 					{	if((size >= 0 && w >= size) ||
733 						   (size <  0 && *ssp == 0) )
734 							break;
735 						osp = ssp;
736 						n = mbchar(osp);
737 						n_w = mbwidth(n);
738 						if(precis >= 0 && (w+n_w) > precis )
739 							break;
740 						w += n_w;
741 						ssp = osp;
742 					}
743 					v = ssp - sp;
744 				}
745 #endif
746 				else
747 #endif
748 				{	if((v = size) < 0)
749 						for(v = 0; sp[v]; ++v)
750 							if(v == precis)
751 								break;
752 					if(precis >= 0 && v > precis)
753 						v = precis;
754 					w = v;
755 				}
756 
757 				if((n = width - w) > 0 && !(flags&SFFMT_LEFT) )
758 				{	if(flags&SFFMT_CENTER)
759 					{	n -= (k = n/2);
760 						SFnputc(f, ' ', k);
761 					}
762 					else
763 					{
764 						SFnputc(f, ' ', n);
765 						n = 0;
766 					}
767 				}
768 #if _has_multibyte
769 				if(flags & SFFMT_LONG)
770 				{	SFMBCLR(&mbs);
771 					for(wsp = (wchar_t*)sp; w > 0; ++wsp, --w)
772 					{	if((n_s = wcrtomb(buf, *wsp, &mbs)) <= 0)
773 							break;
774 						sp = buf; SFwrite(f, sp, n_s);
775 					}
776 				}
777 				else
778 #endif
779 					{ SFwrite(f,sp,v); }
780 				if(n > 0)
781 					{ SFnputc(f,' ',n); }
782 				if(!(sp = *++ls))
783 					break;
784 				else if(base > 0)
785 					{ SFputc(f,base); }
786 			}
787 			continue;
788 
789 		case 'C':
790 			flags = (flags & ~(SFFMT_TYPES|SFFMT_LDOUBLE)) | SFFMT_LONG;
791 		case 'c':
792 #if _has_multibyte && defined(mbwidth)
793 			wc = (flags & SFFMT_LDOUBLE) && mbwide();
794 #endif
795 			if(precis <= 0) /* # of times to repeat a character */
796 				precis = 1;
797 #if _has_multibyte
798 			if(flags & SFFMT_LONG)
799 			{	if(base >= 0)
800 				{	if(!(wsp = (wchar_t*)argv.s) )
801 						continue;
802 					for(size = 0; wsp[size]; ++size)
803 						;
804 				}
805 				else
806 				{	wsp = &argv.wc;
807 					size = 1;
808 				}
809 			}
810 			else
811 #endif
812 			{	if(base >= 0)
813 				{	if(!(sp = argv.s) )
814 						continue;
815 					size = strlen(sp);
816 				}
817 				else
818 				{	argv.c = (char)(argv.i);
819 					sp = &argv.c;
820 					size = 1;
821 				}
822 			}
823 
824 			while(size > 0)
825 			{
826 #if _has_multibyte
827 				if(flags&SFFMT_LONG)
828 				{	SFMBCLR(&mbs);
829 					if((n_s = wcrtomb(buf, *wsp++, &mbs)) <= 0)
830 						break;
831 #ifdef mbwidth
832 					if(wc)
833 						n_s = mbwidth(*(wsp - 1));
834 #endif
835 					n = width - precis*n_s; /* padding amount */
836 				}
837 				else
838 #endif
839 				if(flags&SFFMT_ALTER)
840 				{	n_s = chr2str(buf, *sp++);
841 					n = width - precis*n_s;
842 				}
843 				else
844 				{	fmt = *sp++;
845 					n = width - precis;
846 				}
847 
848 				if(n > 0 && !(flags&SFFMT_LEFT) )
849 				{	if(flags&SFFMT_CENTER)
850 					{	n -= (k = n/2);
851 						SFnputc(f, ' ', k);
852 					}
853 					else
854 					{	SFnputc(f, ' ', n);
855 						n = 0;
856 					}
857 				}
858 
859 				v = precis; /* need this because SFnputc may clear it */
860 #if _has_multibyte
861 				if(flags&SFFMT_LONG)
862 				{	for(; v > 0; --v)
863 						{ ssp = buf; k = n_s; SFwrite(f,ssp,k); }
864 				}
865 				else
866 #endif
867 				if(flags&SFFMT_ALTER)
868 				{	for(; v > 0; --v)
869 						{ ssp = buf; k = n_s; SFwrite(f,ssp,k); }
870 				}
871 				else
872 				{	SFnputc(f, fmt, v);
873 				}
874 
875 				if(n > 0)
876 					{ SFnputc(f,' ',n); };
877 
878 				if((size -= 1) > 0 && base > 0)
879 					{ SFputc(f,base); }
880 			}
881 			continue;
882 
883 		case 'n':	/* return current output length */
884 			SFEND(f);
885 #if !_ast_intmax_long
886 			if(size == sizeof(Sflong_t) )
887 				*((Sflong_t*)argv.vp) = (Sflong_t)n_output;
888 			else
889 #endif
890 			if(size == sizeof(long))
891 				*((long*)argv.vp) = (long)n_output;
892 			else if(size == sizeof(short) )
893 				*((short*)argv.vp) = (short)n_output;
894 			else if(size == sizeof(uchar) )
895 				*((uchar*)argv.vp) = (uchar)n_output;
896 			else	*((int*)argv.vp) = (int)n_output;
897 
898 			continue;
899 
900 		case 'p':	/* pointer value */
901 			fmt = 'x';
902 			base = 16; n_s = 15; n = 4;
903 			flags = (flags&~(SFFMT_SIGN|SFFMT_BLANK|SFFMT_ZERO))|SFFMT_ALTER;
904 #if _more_void_int
905 			lv = (Sflong_t)((Sfulong_t)argv.vp);
906 			goto long_cvt;
907 #else
908 			v = (int)((uint)argv.vp);
909 			goto int_cvt;
910 #endif
911 		case 'o':
912 			base = 8; n_s = 7; n = 3;
913 			flags &= ~(SFFMT_SIGN|SFFMT_BLANK);
914 			goto int_arg;
915 		case 'X':
916 			ssp = "0123456789ABCDEF";
917 		case 'x':
918 			base = 16; n_s = 15; n = 4;
919 			flags &= ~(SFFMT_SIGN|SFFMT_BLANK);
920 			goto int_arg;
921 		case 'i':
922 #if _PACKAGE_ast
923 			if((flags&SFFMT_ALTER) && base < 0)
924 			{	flags &= ~SFFMT_ALTER;
925 				scale = 1024;
926 			}
927 #endif
928 			fmt = 'd';
929 			goto d_format;
930 		case 'u':
931 			flags &= ~(SFFMT_SIGN|SFFMT_BLANK);
932 		case 'd':
933 		d_format:
934 #if _PACKAGE_ast
935 			if((flags&SFFMT_ALTER) && base < 0)
936 			{	flags &= ~SFFMT_ALTER;
937 				scale = 1000;
938 			}
939 #endif
940 			if(base < 2 || base > SF_RADIX)
941 				base = 10;
942 			if((base&(n_s = base-1)) == 0)
943 			{	if(base < 8)
944 					n = base <  4 ? 1 : 2;
945 				else if(base < 32)
946 					n = base < 16 ? 3 : 4;
947 				else	n = base < 64 ? 5 : 6;
948 			}
949 			else	n_s = base == 10 ? -1 : 0;
950 
951 		int_arg:
952 #if !_ast_intmax_long ||  _more_long_int || _more_void_int
953 			if(size == sizeof(Sflong_t))
954 			{	lv = argv.ll;
955 				goto long_cvt;
956 			}
957 			else if(sizeof(long) < sizeof(Sflong_t) && size == sizeof(long))
958 			{	if(fmt == 'd')
959 					lv = (Sflong_t)argv.l;
960 				else	lv = (Sflong_t)argv.ul;
961 			long_cvt:
962 #if _PACKAGE_ast
963 				if(scale)
964 				{	sp = fmtscale(lv, scale);
965 #if _has_multibyte && defined(mbwidth)
966 					wc = 0;
967 #endif
968 					goto str_cvt;
969 				}
970 #endif
971 				if(lv == 0 && precis == 0)
972 					break;
973 				if(lv < 0 && fmt == 'd' )
974 				{	flags |= SFFMT_MINUS;
975 					if(lv == HIGHBITL) /* avoid overflow */
976 					{	lv = (Sflong_t)(HIGHBITL/base);
977 						*--sp = _Sfdigits[HIGHBITL -
978 								  ((Sfulong_t)lv)*base];
979 					}
980 					else	lv = -lv;
981 				}
982 				if(n_s < 0)	/* base 10 */
983 				{	Sflong_t	nv;
984 					sfucvt(lv,sp,nv,ssp,Sflong_t,Sfulong_t);
985 				}
986 				else if(n_s > 0) /* base power-of-2 */
987 				{	do
988 					{	*--sp = ssp[lv&n_s];
989 					} while((lv = ((Sfulong_t)lv) >> n) );
990 				}
991 				else		/* general base */
992 				{	do
993 					{	*--sp = ssp[((Sfulong_t)lv)%base];
994 					} while((lv = ((Sfulong_t)lv)/base) );
995 				}
996 			} else
997 #endif
998 			if(sizeof(short) < sizeof(int) && size == sizeof(short) )
999 			{	if(fmt == 'd')
1000 					v = (int)((short)argv.i);
1001 				else	v = (int)((ushort)argv.i);
1002 				goto int_cvt;
1003 			}
1004 			else if(size == sizeof(char))
1005 			{	if(fmt != 'd')
1006 					v = (int)((uchar)argv.i);
1007 				else
1008 				{
1009 #if _key_signed
1010 					v = (int)((signed char)argv.i);
1011 #else
1012 					if(argv.i < 0)
1013 						v = -((int)((char)(-argv.i)));
1014 					else	v =  ((int)((char)( argv.i)));
1015 #endif
1016 				}
1017 				goto int_cvt;
1018 			}
1019 			else
1020 			{	v = argv.i;
1021 			int_cvt:
1022 #if _PACKAGE_ast
1023 				if(scale)
1024 				{	sp = fmtscale(v, scale);
1025 #if _has_multibyte && defined(mbwidth)
1026 					wc = 0;
1027 #endif
1028 					goto str_cvt;
1029 				}
1030 #endif
1031 				if(v == 0 && precis == 0)
1032 					break;
1033 				if(v < 0 && fmt == 'd' )
1034 				{	flags |= SFFMT_MINUS;
1035 					if(v == HIGHBITI) /* avoid overflow */
1036 					{	v = (int)(HIGHBITI/base);
1037 						*--sp = _Sfdigits[HIGHBITI -
1038 								  ((uint)v)*base];
1039 					}
1040 					else	v = -v;
1041 				}
1042 				if(n_s < 0)	/* base 10 */
1043 				{	sfucvt(v,sp,n,ssp,int,uint);
1044 				}
1045 				else if(n_s > 0) /* base power-of-2 */
1046 				{	do
1047 					{	*--sp = ssp[v&n_s];
1048 					} while((v = ((uint)v) >> n) );
1049 				}
1050 				else /* n_s == 0, general base */
1051 				{	do
1052 					{	*--sp = ssp[((uint)v)%base];
1053 					} while((v = ((uint)v)/base) );
1054 				}
1055 			}
1056 
1057 			if(n_s < 0 && (flags&SFFMT_THOUSAND) && (n = endsp-sp) > 3)
1058 			{	if((n %= 3) == 0)
1059 					n = 3;
1060  				for(ep = buf+SLACK, endep = ep + n; ; )
1061 				{	while(ep < endep)
1062 						*ep++ = *sp++;
1063 					if(sp == endsp)
1064 						break;
1065 					if(sp <= endsp-3)
1066 						*ep++ = thousand;
1067 					endep = ep+3;
1068 				}
1069 				sp = buf+SLACK;
1070 				endsp = ep;
1071 			}
1072 
1073 			/* zero padding for precision if have room in buffer */
1074 			if(precis > 0 && (precis -= (endsp-sp)) < (sp-buf)-64)
1075 				while(precis-- > 0)
1076 					*--sp = '0';
1077 
1078 			if(flags&SFFMT_ALTER) /* prefix */
1079 			{	if(fmt == 'o')
1080 				{	if(*sp != '0')
1081 						*--sp = '0';
1082 				}
1083 				else
1084 				{	if(width > 0 && (flags&SFFMT_ZERO))
1085 					{	/* do 0 padding first */
1086 						if(fmt == 'x' || fmt == 'X')
1087 							n = 0;
1088 						else if(dot < 2)
1089 							n = width;
1090 						else	n = base < 10 ? 2 : 3;
1091 						n += (flags&(SFFMT_MINUS|SFFMT_SIGN)) ?
1092 							1 : 0;
1093 						n = width - (n + (endsp-sp));
1094 						while(n-- > 0)
1095 							*--sp = '0';
1096 					}
1097 					if(fmt == 'x' || fmt == 'X')
1098 					{	*--sp = (char)fmt;
1099 						*--sp = '0';
1100 					}
1101 					else if(dot >= 2)
1102 					{	/* base#value notation */
1103 						*--sp = '#';
1104 						if(base < 10)
1105 							*--sp = (char)('0'+base);
1106 						else
1107 						{	*--sp = _Sfdec[(base <<= 1)+1];
1108 							*--sp = _Sfdec[base];
1109 						}
1110 					}
1111 				}
1112 			}
1113 
1114 			break;
1115 
1116 		case 'g': case 'G': /* these ultimately become %e or %f */
1117 		case 'a': case 'A':
1118 		case 'e': case 'E':
1119 		case 'f': case 'F':
1120 #if !_ast_fltmax_double
1121 			if(size == sizeof(Sfdouble_t) )
1122 			{	v = SFFMT_LDOUBLE;
1123 				valp = &argv.ld;
1124 				dval = argv.ld;
1125 			}
1126 			else
1127 #endif
1128 			{	v = 0;
1129 				valp = &argv.d;
1130 				dval = argv.d;
1131 			}
1132 
1133 			if(fmt == 'e' || fmt == 'E' && (v |= SFFMT_UPPER))
1134 			{	v |= SFFMT_EFORMAT;
1135 				n = (precis = precis < 0 ? FPRECIS : precis)+1;
1136 				ep = _sfcvt(valp,tmp+1,sizeof(tmp)-1, min(n,SF_FDIGITS),
1137 					    &decpt, &sign, &n_s, v);
1138 				goto e_format;
1139 			}
1140 			else if(fmt == 'f' || fmt == 'F' && (v |= SFFMT_UPPER))
1141 			{	precis = precis < 0 ? FPRECIS : precis;
1142 				ep = _sfcvt(valp,tmp+1,sizeof(tmp)-1, min(precis,SF_FDIGITS),
1143 					    &decpt, &sign, &n_s, v);
1144 				goto f_format;
1145 			}
1146 			else if(fmt == 'a' || fmt == 'A' && (v |= SFFMT_UPPER))
1147 			{	v |= SFFMT_AFORMAT;
1148 				if(precis < 0)
1149 				{	if(v & SFFMT_LDOUBLE)
1150 						precis = 2*(sizeof(Sfdouble_t) - 2);
1151 					else	precis = 2*(sizeof(double) - 2);
1152 				}
1153 				n = precis + 1;
1154 				ep = _sfcvt(valp,tmp+1,sizeof(tmp)-1, min(n,SF_FDIGITS),
1155 					    &decpt, &sign, &n_s, v);
1156 
1157 				sp = endsp = buf+1;	/* reserve space for sign */
1158 				*endsp++ = '0';
1159 				*endsp++ = fmt == 'a' ? 'x' : 'X';
1160 				if (!isxdigit(*ep))
1161 					goto infinite;
1162 				goto a_format;
1163 			}
1164 			else /* 'g' or 'G' format */
1165 			{	precis = precis < 0 ? FPRECIS : precis == 0 ? 1 : precis;
1166 				if(fmt == 'G')
1167 					v |= SFFMT_UPPER;
1168 				v |= SFFMT_EFORMAT;
1169 				ep = _sfcvt(valp,tmp+1,sizeof(tmp)-1, min(precis,SF_FDIGITS),
1170 					    &decpt, &sign, &n_s, v);
1171 				if(dval == 0.)
1172 					decpt = 1;
1173 				else if(*ep == 'I')
1174 					goto infinite;
1175 
1176 				if(!(flags&SFFMT_ALTER))
1177 				{	/* zap trailing 0s */
1178 					if((n = n_s) > precis)
1179 						n = precis;
1180 					while((n -= 1) >= 1 && ep[n] == '0')
1181 						;
1182 					n += 1;
1183 				}
1184 				else	n = precis;
1185 
1186 				if(decpt < -3 || decpt > precis)
1187 				{	precis = n-1;
1188 					goto e_format;
1189 				}
1190 				else
1191 				{	precis = n - decpt;
1192 					goto f_format;
1193 				}
1194 			}
1195 
1196 		e_format: /* build the x.yyyy string */
1197 			if(isalpha(*ep))
1198 				goto infinite;
1199 			sp = endsp = buf+1;	/* reserve space for sign */
1200 		a_format:
1201 			*endsp++ = *ep ? *ep++ : '0';
1202 
1203 			SFSETLOCALE(&decimal,&thousand);
1204 			if(precis > 0 || (flags&SFFMT_ALTER))
1205 				*endsp++ = decimal;
1206 			ssp = endsp;
1207 			endep = ep+precis;
1208 			while((*endsp++ = *ep++) && ep <= endep)
1209 				;
1210 			precis -= (endsp -= 1) - ssp;
1211 
1212 			/* build the exponent */
1213 			ep = endep = buf+(sizeof(buf)-1);
1214 			if(dval != 0.)
1215 			{	if((n = decpt - 1) < 0)
1216 					n = -n;
1217 				while(n > 9)
1218 				{	v = n; n /= 10;
1219 					*--ep = (char)('0' + (v - n*10));
1220 				}
1221 			}
1222 			else	n = 0;
1223 			*--ep = (char)('0' + n);
1224 			if(endep-ep <= 1)	/* at least 2 digits */
1225 				*--ep = '0';
1226 
1227 			/* the e/Exponent separator and sign */
1228 			*--ep = (decpt > 0 || dval == 0.) ? '+' : '-';
1229 			*--ep = fmt == 'a' ? 'p' : fmt == 'A' ? 'P' :
1230 				isupper(fmt) ? 'E' : 'e';
1231 
1232 			goto end_aefg;
1233 
1234 		f_format: /* data before the decimal point */
1235 			if(isalpha(*ep))
1236 			{
1237 			infinite:
1238 				flags &= ~SFFMT_ZERO;
1239 				endsp = (sp = ep)+sfslen();
1240 				ep = endep;
1241 				precis = 0;
1242 				goto end_aefg;
1243 			}
1244 
1245 			SFSETLOCALE(&decimal,&thousand);
1246 			endsp = sp = buf+1;	/* save a space for sign */
1247 			endep = ep+decpt;
1248 			if(decpt > 3 && (flags&SFFMT_THOUSAND) )
1249 			{	if((n = decpt%3) == 0)
1250 					n = 3;
1251 				while(ep < endep && (*endsp++ = *ep++) )
1252 				{	if(--n == 0 && (ep <= endep-3) )
1253 					{	*endsp++ = thousand;
1254 						n = 3;
1255 					}
1256 				}
1257 			}
1258 			else
1259 			{	while(ep < endep && (*endsp++ = *ep++))
1260 					;
1261 			}
1262 			if(endsp == sp)
1263 				*endsp++ = '0';
1264 
1265 			if(precis > 0 || (flags&SFFMT_ALTER))
1266 				*endsp++ = decimal;
1267 
1268 			if((n = -decpt) > 0)
1269 			{	/* output zeros for negative exponent */
1270 				ssp = endsp + min(n,precis);
1271 				precis -= n;
1272 				while(endsp < ssp)
1273 					*endsp++ = '0';
1274 			}
1275 
1276 			ssp = endsp;
1277 			endep = ep+precis;
1278 			while((*endsp++ = *ep++) && ep <= endep)
1279 				;
1280 			precis -= (endsp -= 1) - ssp;
1281 			ep = endep;
1282 		end_aefg:
1283 			flags |= SFFMT_FLOAT;
1284 			if(sign)
1285 				flags |= SFFMT_MINUS;
1286 			break;
1287 		}
1288 
1289 		if(flags == 0 && width <= 0)
1290 			goto do_output;
1291 
1292 		if(flags&SFFMT_PREFIX)
1293 			fmt = (flags&SFFMT_MINUS) ? '-' : (flags&SFFMT_SIGN) ? '+' : ' ';
1294 
1295 		n = (endsp-sp) + (endep-ep) + (precis <= 0 ? 0 : precis) +
1296 		    ((flags&SFFMT_PREFIX) ? 1 : 0);
1297 		if((v = width-n) <= 0)
1298 			v = 0;
1299 		else if(!(flags&SFFMT_ZERO)) /* right padding */
1300 		{	if(flags&SFFMT_LEFT)
1301 				v = -v;
1302 			else if(flags&SFFMT_PREFIX) /* blank padding, output prefix now */
1303 			{	*--sp = fmt;
1304 				flags &= ~SFFMT_PREFIX;
1305 			}
1306 		}
1307 
1308 		if(flags&SFFMT_PREFIX) /* put out the prefix */
1309 		{	SFputc(f,fmt);
1310 			if(fmt != ' ')
1311 				flags |= SFFMT_ZERO;
1312 		}
1313 
1314 		if((n = v) > 0) /* left padding */
1315 		{	v = (flags&SFFMT_ZERO) ? '0' : ' ';
1316 			SFnputc(f,v,n);
1317 		}
1318 
1319 		if((n = precis) > 0 && !(flags&SFFMT_FLOAT))
1320 		{	/* padding for integer precision */
1321 			SFnputc(f,'0',n);
1322 			precis = 0;
1323 		}
1324 
1325 	do_output:
1326 		if((n = endsp-sp) > 0)
1327 			SFwrite(f,sp,n);
1328 
1329 		if(flags&(SFFMT_FLOAT|SFFMT_LEFT))
1330 		{	/* SFFMT_FLOAT: right padding for float precision */
1331 			if((n = precis) > 0)
1332 				SFnputc(f,'0',n);
1333 
1334 			/* SFFMT_FLOAT: the exponent of %eE */
1335 			if((n = endep - (sp = ep)) > 0)
1336 				SFwrite(f,sp,n);
1337 
1338 			/* SFFMT_LEFT: right padding */
1339 			if((n = -v) > 0)
1340 				{ SFnputc(f,' ',n); }
1341 		}
1342 	}
1343 
1344 pop_fmt:
1345 	if(fp)
1346 	{	free(fp);
1347 		fp = NIL(Fmtpos_t*);
1348 	}
1349 	while((fm = fmstk) ) /* pop the format stack and continue */
1350 	{	if(fm->eventf)
1351 		{	if(!form || !form[0])
1352 				(*fm->eventf)(f,SF_FINAL,NIL(Void_t*),ft);
1353 			else if((*fm->eventf)(f,SF_DPOP,(Void_t*)form,ft) < 0)
1354 				goto loop_fmt;
1355 		}
1356 
1357 		fmstk = fm->next;
1358 		if((form = fm->form) )
1359 		{	SFMBCPY(&fmbs,&fm->mbs);
1360 			va_copy(args, fm->args);
1361 			oform = fm->oform;
1362 			va_copy(oargs,fm->oargs);
1363 			argn = fm->argn;
1364 			fp = fm->fp;
1365 		}
1366 		ft = fm->ft;
1367 		free(fm);
1368 		if(form && form[0])
1369 			goto loop_fmt;
1370 	}
1371 
1372 done:
1373 	if(fp)
1374 		free(fp);
1375 	while((fm = fmstk) )
1376 	{	if(fm->eventf)
1377 			(*fm->eventf)(f,SF_FINAL,NIL(Void_t*),fm->ft);
1378 		fmstk = fm->next;
1379 		free(fm);
1380 	}
1381 
1382 	SFEND(f);
1383 
1384 	n = f->next - f->data;
1385 	if((sp = (char*)f->data) == data)
1386 		f->endw = f->endr = f->endb = f->data = NIL(uchar*);
1387 	f->next = f->data;
1388 
1389 	if((((flags = f->flags)&SF_SHARE) && !(flags&SF_PUBLIC) ) ||
1390 	   (n > 0 && (sp == data || (flags&SF_LINE) ) ) )
1391 		(void)SFWRITE(f,(Void_t*)sp,n);
1392 	else	f->next += n;
1393 
1394 	SFOPEN(f,0);
1395 	SFMTXRETURN(f, n_output);
1396 }
1397