xref: /titanic_51/usr/src/lib/libast/common/sfio/sfvprintf.c (revision 3d5869cc80c26887ee2c78b0f35e58f574e97192)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2009 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)
268 								  (f,oform,oargs,ft,0)) )
269 								goto pop_fmt;
270 							n = FP_SET(n,argn);
271 						}
272 						else	n = FP_SET(-1,argn);
273 
274 						if(fp)
275 						{	t_str = fp[n].argv.s;
276 							n_str = fp[n].ft.size;
277 						}
278 						else if(ft && ft->extf )
279 						{	FMTSET(ft, form,args,
280 								LEFTP, 0, 0, 0,0,0,
281 								NIL(char*),0);
282 							n = (*ft->extf)
283 							      (f,(Void_t*)&argv,ft);
284 							if(n < 0)
285 								goto pop_fmt;
286 							if(!(ft->flags&SFFMT_VALUE) )
287 								goto t_arg;
288 							if((t_str = argv.s) &&
289 							   (n_str = (int)ft->size) < 0)
290 								n_str = strlen(t_str);
291 						}
292 						else
293 						{ t_arg:
294 							if((t_str = va_arg(args,char*)) )
295 								n_str = strlen(t_str);
296 						}
297 					}
298 					goto loop_flags;
299 				}
300 			}
301 
302 		case '-' :
303 			flags = (flags & ~(SFFMT_CENTER|SFFMT_ZERO)) | SFFMT_LEFT;
304 			goto loop_flags;
305 		case '0' :
306 			if(!(flags&(SFFMT_LEFT|SFFMT_CENTER)) )
307 				flags |= SFFMT_ZERO;
308 			goto loop_flags;
309 		case ' ' :
310 			if(!(flags&SFFMT_SIGN) )
311 				flags |= SFFMT_BLANK;
312 			goto loop_flags;
313 		case '+' :
314 			flags = (flags & ~SFFMT_BLANK) | SFFMT_SIGN;
315 			goto loop_flags;
316 		case '=' :
317 			flags = (flags & ~(SFFMT_LEFT|SFFMT_ZERO)) | SFFMT_CENTER;
318 			goto loop_flags;
319 		case '#' :
320 			flags |= SFFMT_ALTER;
321 			goto loop_flags;
322 		case QUOTE:
323 			SFSETLOCALE(&decimal,&thousand);
324 			if(thousand > 0)
325 				flags |= SFFMT_THOUSAND;
326 			goto loop_flags;
327 		case ',':
328 			SFSETLOCALE(&decimal,&thousand);
329 			if(thousand < 0)
330 				thousand = fmt;
331 			flags |= SFFMT_THOUSAND;
332 			goto loop_flags;
333 
334 		case '.' :
335 			dot += 1;
336 			if(dot == 1)
337 			{	/* so base can be defined without setting precis */
338 				if(*form != '.')
339 					precis = 0;
340 			}
341 			else if(dot == 2)
342 			{	base = 0; /* for %s,%c */
343 				v = form[0] == 'l' ? form[1] : form[0];
344 				if(v == 'c' || v == 'C' || v == 's' || v == 'S')
345 					goto loop_flags;
346 				if(*form && !isalnum(*form))
347 				{	v = form[1] == 'l' ? form[2] : form[1];
348 					if(v == 'c' || v == 'C' || v == 's' || v == 'S')
349 					{	if(*form == '*')
350 							goto do_star;
351 						else
352 						{	base = *form++;
353 							goto loop_flags;
354 						}
355 					}
356 				}
357 			}
358 
359 			if(isdigit(*form) )
360 			{	fmt = *form++;
361 				goto dot_size;
362 			}
363 			else if(*form != '*')
364 				goto loop_flags;
365 		do_star:
366 			form += 1; /* fall thru for '*' */
367 		case '*' :
368 			form = (*_Sffmtintf)(form,&n);
369 			if(*form == '$')
370 			{	form += 1;
371 				if(!fp && !(fp = (*_Sffmtposf)(f,oform,oargs,ft,0)) )
372 					goto pop_fmt;
373 				n = FP_SET(n,argn);
374 			}
375 			else	n = FP_SET(-1,argn);
376 
377 			if(fp)
378 				v = fp[n].argv.i;
379 			else if(ft && ft->extf)
380 			{	FMTSET(ft, form,args, '.',dot, 0, 0,0,0, NIL(char*), 0);
381 				if((*ft->extf)(f, (Void_t*)(&argv), ft) < 0)
382 					goto pop_fmt;
383 				fmt = ft->fmt;
384 				flags = (flags&~SFFMT_TYPES) | (ft->flags&SFFMT_TYPES);
385 				if(ft->flags&SFFMT_VALUE)
386 					v = argv.i;
387 				else	v = (dot <= 2) ? va_arg(args,int) : 0;
388 			}
389 			else	v = dot <= 2 ? va_arg(args,int) : 0;
390 			goto dot_set;
391 
392 		case '1' : case '2' : case '3' :
393 		case '4' : case '5' : case '6' :
394 		case '7' : case '8' : case '9' :
395 		dot_size :
396 			for(v = fmt - '0'; isdigit(*form); ++form)
397 				v = v*10 + (*form - '0');
398 			if(*form == '$')
399 			{	form += 1;
400 				if(!fp && !(fp = (*_Sffmtposf)(f,oform,oargs,ft,0)) )
401 					goto pop_fmt;
402 				argp = v-1;
403 				goto loop_flags;
404 			}
405 		dot_set :
406 			if(dot == 0)
407 			{	if((width = v) < 0)
408 				{	width = -width;
409 					flags = (flags & ~(SFFMT_CENTER|SFFMT_ZERO)) | SFFMT_LEFT;
410 				}
411 			}
412 			else if(dot == 1)
413 				precis = v;
414 			else if(dot == 2)
415 				base = v;
416 			goto loop_flags;
417 
418 		case 'I' : /* object length */
419 			size = -1; flags = (flags & ~SFFMT_TYPES) | SFFMT_IFLAG;
420 			if(isdigit(*form) )
421 			{	for(size = 0, n = *form; isdigit(n); n = *++form)
422 					size = size*10 + (n - '0');
423 			}
424 			else if(*form == '*')
425 			{	form = (*_Sffmtintf)(form+1,&n);
426 				if(*form == '$')
427 				{	form += 1;
428 					if(!fp &&
429 					   !(fp = (*_Sffmtposf)(f,oform,oargs,ft,0)))
430 						goto pop_fmt;
431 					n = FP_SET(n,argn);
432 				}
433 				else	n = FP_SET(-1,argn);
434 
435 				if(fp)	/* use position list */
436 					size = fp[n].argv.i;
437 				else if(ft && ft->extf)
438 				{	FMTSET(ft, form,args, 'I',sizeof(int), 0, 0,0,0,
439 						NIL(char*), 0);
440 					if((*ft->extf)(f, (Void_t*)(&argv), ft) < 0)
441 						goto pop_fmt;
442 					if(ft->flags&SFFMT_VALUE)
443 						size = argv.i;
444 					else	size = va_arg(args,int);
445 				}
446 				else	size = va_arg(args,int);
447 			}
448 			goto loop_flags;
449 
450 		case 'l' :
451 			size = -1; flags &= ~SFFMT_TYPES;
452 			if(*form == 'l')
453 			{	form += 1;
454 				flags |= SFFMT_LLONG;
455 			}
456 			else	flags |= SFFMT_LONG;
457 			goto loop_flags;
458 		case 'h' :
459 			size = -1; flags &= ~SFFMT_TYPES;
460 			if(*form == 'h')
461 			{	form += 1;
462 				flags |= SFFMT_SSHORT;
463 			}
464 			else	flags |= SFFMT_SHORT;
465 			goto loop_flags;
466 		case 'L' :
467 			size = -1; flags = (flags & ~SFFMT_TYPES) | SFFMT_LDOUBLE;
468 			goto loop_flags;
469 
470 		case 'j' :
471 			size = -1; flags = (flags&~SFFMT_TYPES) | SFFMT_JFLAG;
472 			goto loop_flags;
473 		case 'z' :
474 			size = -1; flags = (flags&~SFFMT_TYPES) | SFFMT_ZFLAG;
475 			goto loop_flags;
476 		case 't' :
477 			size = -1; flags = (flags&~SFFMT_TYPES) | SFFMT_TFLAG;
478 			goto loop_flags;
479 		default:
480 			break;
481 		}
482 
483 		/* set object size for scalars */
484 		if(flags & SFFMT_TYPES)
485 		{	if((_Sftype[fmt]&(SFFMT_INT|SFFMT_UINT)) || fmt == 'n')
486 			{	if(flags&SFFMT_LONG)
487 					size = sizeof(long);
488 				else if(flags&SFFMT_SHORT)
489 					size = sizeof(short);
490 				else if(flags&SFFMT_SSHORT)
491 					size = sizeof(char);
492 				else if(flags&SFFMT_TFLAG)
493 					size = sizeof(ptrdiff_t);
494 				else if(flags&SFFMT_ZFLAG)
495 					size = sizeof(size_t);
496 				else if(flags&(SFFMT_LLONG|SFFMT_JFLAG) )
497 					size = sizeof(Sflong_t);
498 				else if(flags&SFFMT_IFLAG)
499 				{	if(size <= 0 ||
500 					   size == sizeof(Sflong_t)*CHAR_BIT )
501 						size = sizeof(Sflong_t);
502 				}
503 				else if(size < 0)
504 					size = sizeof(int);
505 			}
506 			else if(_Sftype[fmt]&SFFMT_FLOAT)
507 			{	if(flags&SFFMT_LDOUBLE)
508 					size = sizeof(Sfdouble_t);
509 				else if(flags&(SFFMT_LONG|SFFMT_LLONG))
510 					size = sizeof(double);
511 				else if(flags&SFFMT_IFLAG)
512 				{	if(size <= 0)
513 						size = sizeof(Sfdouble_t);
514 				}
515 				else if(size < 0)
516 					size = sizeof(float);
517 			}
518 			else if(_Sftype[fmt]&SFFMT_CHAR)
519 			{
520 #if _has_multibyte
521 				if((flags&SFFMT_LONG) || fmt == 'C')
522 				{	size = sizeof(wchar_t) > sizeof(int) ?
523 						sizeof(wchar_t) : sizeof(int);
524 				} else
525 #endif
526 				if(size < 0)
527 					size = sizeof(int);
528 			}
529 		}
530 
531 		argp = FP_SET(argp,argn);
532 		if(fp)
533 		{	if(ft && ft->extf && fp[argp].ft.fmt != fp[argp].fmt)
534 				fmt = fp[argp].ft.fmt;
535 			argv = fp[argp].argv;
536 			size = fp[argp].ft.size;
537 		}
538 		else if(ft && ft->extf )	/* extended processing */
539 		{	FMTSET(ft, form,args, fmt, size,flags, width,precis,base,
540 				t_str,n_str);
541 			SFEND(f); SFOPEN(f,0);
542 			v = (*ft->extf)(f, (Void_t*)(&argv), ft);
543 			SFLOCK(f,0); SFBUF(f);
544 
545 			if(v < 0)	/* no further processing */
546 				goto pop_fmt;
547 			else if(v > 0)	/* extf output v bytes */
548 			{	n_output += v;
549 				continue;
550 			}
551 			else		/* extf did not output */
552 			{	FMTGET(ft, form,args, fmt, size,flags, width,precis,base);
553 
554 				if(!(ft->flags&SFFMT_VALUE))
555 					goto arg_list;
556 				else if(_Sftype[fmt]&(SFFMT_INT|SFFMT_UINT) )
557 				{	if(size == sizeof(short))
558 					{	if(_Sftype[fmt]&SFFMT_INT)
559 							argv.i = argv.h;
560 						else	argv.i = argv.uh;
561 					}
562 					else if(size == sizeof(char))
563 					{	if(_Sftype[fmt]&SFFMT_INT)
564 							argv.i = argv.c;
565 						else	argv.i = argv.uc;
566 					}
567 				}
568 				else if(_Sftype[fmt]&SFFMT_FLOAT )
569 				{	if(size == sizeof(float) )
570 						argv.d = argv.f;
571 				}
572 				else if(_Sftype[fmt]&SFFMT_CHAR)
573 				{	if(base < 0)
574 						argv.i = (int)argv.c;
575 				}
576 			}
577 		}
578 		else
579 		{ arg_list:
580 			switch(_Sftype[fmt])
581 			{ case SFFMT_INT:
582 			  case SFFMT_UINT:
583 #if !_ast_intmax_long
584 				if(size == sizeof(Sflong_t))
585 					argv.ll = va_arg(args, Sflong_t);
586 				else
587 #endif
588 				if(size == sizeof(long) )
589 					argv.l = va_arg(args, long);
590 				else	argv.i = va_arg(args, int);
591 				break;
592 			 case SFFMT_FLOAT:
593 #if !_ast_fltmax_double
594 				if(size == sizeof(Sfdouble_t))
595 					argv.ld = va_arg(args,Sfdouble_t);
596 				else
597 #endif
598 					argv.d  = va_arg(args,double);
599 				break;
600 			 case SFFMT_POINTER:
601 					argv.vp = va_arg(args,Void_t*);
602 				break;
603 			 case SFFMT_CHAR:
604 				if(base >= 0)
605 					argv.s = va_arg(args,char*);
606 #if _has_multibyte
607 				else if((flags & SFFMT_LONG) || fmt == 'C')
608 				{	if(sizeof(wchar_t) <= sizeof(uint) )
609 						argv.wc = (wchar_t)va_arg(args,uint);
610 					else	argv.wc = va_arg(args,wchar_t);
611 				}
612 #endif
613 				else	argv.i = va_arg(args,int);
614 				break;
615 			 default: /* unknown pattern */
616 				break;
617 			}
618 		}
619 
620 		switch(fmt)	/* PRINTF DIRECTIVES */
621 		{
622 		default :	/* unknown directive */
623 			form -= 1;
624 			argn -= 1;
625 			continue;
626 
627 		case '!' :	/* stacking a new environment */
628 			if(!fp)
629 				fp = (*_Sffmtposf)(f,oform,oargs,ft,0);
630 			else	goto pop_fmt;
631 
632 			if(!argv.ft)
633 				goto pop_fmt;
634 			if(!argv.ft->form && ft ) /* change extension functions */
635 			{	if(ft->eventf &&
636 				   (*ft->eventf)(f,SF_DPOP,(Void_t*)form,ft) < 0)
637 					continue;
638 				fmstk->ft = ft = argv.ft;
639 			}
640 			else			/* stack a new environment */
641 			{	if(!(fm = (Fmt_t*)malloc(sizeof(Fmt_t))) )
642 					goto done;
643 
644 				ft = fm->ft = argv.ft;
645 				SFMBSET(ft->mbs, &fmbs);
646 				if(ft->form)
647 				{	fm->form = (char*)form; SFMBCPY(&fm->mbs,&fmbs);
648 					va_copy(fm->args,args);
649 
650 					fm->oform = oform;
651 					va_copy(fm->oargs,oargs);
652 					fm->argn = argn;
653 					fm->fp = fp;
654 
655 					form = ft->form; SFMBCLR(ft->mbs);
656 					va_copy(args,ft->args);
657 					argn = -1;
658 					fp = NIL(Fmtpos_t*);
659 					oform = (char*)form;
660 					va_copy(oargs,args);
661 				}
662 				else	fm->form = NIL(char*);
663 
664 				fm->eventf = ft->eventf;
665 				fm->next = fmstk;
666 				fmstk = fm;
667 			}
668 			continue;
669 
670 		case 'S':
671 			flags = (flags & ~(SFFMT_TYPES|SFFMT_LDOUBLE)) | SFFMT_LONG;
672 		case 's':
673 #if _has_multibyte && defined(mbwidth)
674 			wc = (flags & SFFMT_LDOUBLE) && mbwide();
675 #endif
676 			if(base >= 0)	/* list of strings */
677 			{	if(!(ls = argv.sp) || !ls[0])
678 					continue;
679 			}
680 			else
681 			{	if(!(sp = argv.s))
682 				{	sp = "(null)";
683 					flags &= ~SFFMT_LONG;
684 				}
685 #if _PACKAGE_ast
686 		str_cvt:
687 				if(scale)
688 				{	size = base = -1;
689 					flags &= ~SFFMT_LONG;
690 				}
691 #endif
692 				ls = tls; tls[0] = sp;
693 			}
694 			for(sp = *ls;;)
695 			{	/* v: number of bytes  w: print width of those v bytes */
696 #if _has_multibyte
697 				if(flags & SFFMT_LONG)
698 				{	v = 0;
699 #ifdef mbwidth
700 					w = 0;
701 #endif
702 					SFMBCLR(&mbs);
703 					for(n = 0, wsp = (wchar_t*)sp;; ++wsp, ++n)
704 					{	if((size >= 0 && n >= size) ||
705 						   (size <  0 && *wsp == 0) )
706 							break;
707 						if((n_s = wcrtomb(buf, *wsp, &mbs)) <= 0)
708 							break;
709 #ifdef mbwidth
710 						if(wc)
711 						{	n_w = mbwidth(*wsp);
712 							if(precis >= 0 && (w+n_w) > precis )
713 								break;
714 							w += n_w;
715 						}
716 						else
717 #endif
718 						if(precis >= 0 && (v+n_s) > precis )
719 							break;
720 						v += n_s;
721 					}
722 #ifdef mbwidth
723 					if(!wc)
724 						w = v;
725 #endif
726 				}
727 #if _has_multibyte && defined(mbwidth)
728 				else if (wc)
729 				{	w = 0;
730 					SFMBCLR(&mbs);
731 					ssp = sp;
732 					for(;;)
733 					{	if((size >= 0 && w >= size) ||
734 						   (size <  0 && *ssp == 0) )
735 							break;
736 						osp = ssp;
737 						n = mbchar(osp);
738 						n_w = mbwidth(n);
739 						if(precis >= 0 && (w+n_w) > precis )
740 							break;
741 						w += n_w;
742 						ssp = osp;
743 					}
744 					v = ssp - sp;
745 				}
746 #endif
747 				else
748 #endif
749 				{	if((v = size) < 0)
750 						for(v = 0; sp[v]; ++v)
751 							if(v == precis)
752 								break;
753 					if(precis >= 0 && v > precis)
754 						v = precis;
755 					w = v;
756 				}
757 
758 				if((n = width - w) > 0 && !(flags&SFFMT_LEFT) )
759 				{	if(flags&SFFMT_CENTER)
760 					{	n -= (k = n/2);
761 						SFnputc(f, ' ', k);
762 					}
763 					else
764 					{
765 						SFnputc(f, ' ', n);
766 						n = 0;
767 					}
768 				}
769 #if _has_multibyte
770 				if(flags & SFFMT_LONG)
771 				{	SFMBCLR(&mbs);
772 					for(wsp = (wchar_t*)sp; w > 0; ++wsp, --w)
773 					{	if((n_s = wcrtomb(buf, *wsp, &mbs)) <= 0)
774 							break;
775 						sp = buf; SFwrite(f, sp, n_s);
776 					}
777 				}
778 				else
779 #endif
780 					{ SFwrite(f,sp,v); }
781 				if(n > 0)
782 					{ SFnputc(f,' ',n); }
783 				if(!(sp = *++ls))
784 					break;
785 				else if(base > 0)
786 					{ SFputc(f,base); }
787 			}
788 			continue;
789 
790 		case 'C':
791 			flags = (flags & ~(SFFMT_TYPES|SFFMT_LDOUBLE)) | SFFMT_LONG;
792 		case 'c':
793 #if _has_multibyte && defined(mbwidth)
794 			wc = (flags & SFFMT_LDOUBLE) && mbwide();
795 #endif
796 			if(precis <= 0) /* # of times to repeat a character */
797 				precis = 1;
798 #if _has_multibyte
799 			if(flags & SFFMT_LONG)
800 			{	if(base >= 0)
801 				{	if(!(wsp = (wchar_t*)argv.s) )
802 						continue;
803 					for(size = 0; wsp[size]; ++size)
804 						;
805 				}
806 				else
807 				{	wsp = &argv.wc;
808 					size = 1;
809 				}
810 			}
811 			else
812 #endif
813 			{	if(base >= 0)
814 				{	if(!(sp = argv.s) )
815 						continue;
816 					size = strlen(sp);
817 				}
818 				else
819 				{	argv.c = (char)(argv.i);
820 					sp = &argv.c;
821 					size = 1;
822 				}
823 			}
824 
825 			while(size > 0)
826 			{
827 #if _has_multibyte
828 				if(flags&SFFMT_LONG)
829 				{	SFMBCLR(&mbs);
830 					if((n_s = wcrtomb(buf, *wsp++, &mbs)) <= 0)
831 						break;
832 #ifdef mbwidth
833 					if(wc)
834 						n_s = mbwidth(*(wsp - 1));
835 #endif
836 					n = width - precis*n_s; /* padding amount */
837 				}
838 				else
839 #endif
840 				if(flags&SFFMT_ALTER)
841 				{	n_s = chr2str(buf, *sp++);
842 					n = width - precis*n_s;
843 				}
844 				else
845 				{	fmt = *sp++;
846 					n = width - precis;
847 				}
848 
849 				if(n > 0 && !(flags&SFFMT_LEFT) )
850 				{	if(flags&SFFMT_CENTER)
851 					{	n -= (k = n/2);
852 						SFnputc(f, ' ', k);
853 					}
854 					else
855 					{	SFnputc(f, ' ', n);
856 						n = 0;
857 					}
858 				}
859 
860 				v = precis; /* need this because SFnputc may clear it */
861 #if _has_multibyte
862 				if(flags&SFFMT_LONG)
863 				{	for(; v > 0; --v)
864 						{ ssp = buf; k = n_s; SFwrite(f,ssp,k); }
865 				}
866 				else
867 #endif
868 				if(flags&SFFMT_ALTER)
869 				{	for(; v > 0; --v)
870 						{ ssp = buf; k = n_s; SFwrite(f,ssp,k); }
871 				}
872 				else
873 				{	SFnputc(f, fmt, v);
874 				}
875 
876 				if(n > 0)
877 					{ SFnputc(f,' ',n); };
878 
879 				if((size -= 1) > 0 && base > 0)
880 					{ SFputc(f,base); }
881 			}
882 			continue;
883 
884 		case 'n':	/* return current output length */
885 			SFEND(f);
886 #if !_ast_intmax_long
887 			if(size == sizeof(Sflong_t) )
888 				*((Sflong_t*)argv.vp) = (Sflong_t)n_output;
889 			else
890 #endif
891 			if(size == sizeof(long))
892 				*((long*)argv.vp) = (long)n_output;
893 			else if(size == sizeof(short) )
894 				*((short*)argv.vp) = (short)n_output;
895 			else if(size == sizeof(uchar) )
896 				*((uchar*)argv.vp) = (uchar)n_output;
897 			else	*((int*)argv.vp) = (int)n_output;
898 
899 			continue;
900 
901 		case 'p':	/* pointer value */
902 			fmt = 'x';
903 			base = 16; n_s = 15; n = 4;
904 			flags = (flags&~(SFFMT_SIGN|SFFMT_BLANK|SFFMT_ZERO))|SFFMT_ALTER;
905 #if _more_void_int
906 			lv = (Sflong_t)((Sfulong_t)argv.vp);
907 			goto long_cvt;
908 #else
909 			v = (int)((uint)argv.vp);
910 			goto int_cvt;
911 #endif
912 		case 'o':
913 			base = 8; n_s = 7; n = 3;
914 			flags &= ~(SFFMT_SIGN|SFFMT_BLANK);
915 			goto int_arg;
916 		case 'X':
917 			ssp = "0123456789ABCDEF";
918 		case 'x':
919 			base = 16; n_s = 15; n = 4;
920 			flags &= ~(SFFMT_SIGN|SFFMT_BLANK);
921 			goto int_arg;
922 		case 'i':
923 #if _PACKAGE_ast
924 			if((flags&SFFMT_ALTER) && base < 0)
925 			{	flags &= ~SFFMT_ALTER;
926 				scale = 1024;
927 			}
928 #endif
929 			fmt = 'd';
930 			goto d_format;
931 		case 'u':
932 			flags &= ~(SFFMT_SIGN|SFFMT_BLANK);
933 		case 'd':
934 		d_format:
935 #if _PACKAGE_ast
936 			if((flags&SFFMT_ALTER) && base < 0)
937 			{	flags &= ~SFFMT_ALTER;
938 				scale = 1000;
939 			}
940 #endif
941 			if(base < 2 || base > SF_RADIX)
942 				base = 10;
943 			if((base&(n_s = base-1)) == 0)
944 			{	if(base < 8)
945 					n = base <  4 ? 1 : 2;
946 				else if(base < 32)
947 					n = base < 16 ? 3 : 4;
948 				else	n = base < 64 ? 5 : 6;
949 			}
950 			else	n_s = base == 10 ? -1 : 0;
951 
952 		int_arg:
953 #if !_ast_intmax_long ||  _more_long_int || _more_void_int
954 			if(size == sizeof(Sflong_t))
955 			{	lv = argv.ll;
956 				goto long_cvt;
957 			}
958 			else if(sizeof(long) < sizeof(Sflong_t) && size == sizeof(long))
959 			{	if(fmt == 'd')
960 					lv = (Sflong_t)argv.l;
961 				else	lv = (Sflong_t)argv.ul;
962 			long_cvt:
963 #if _PACKAGE_ast
964 				if(scale)
965 				{	sp = fmtscale(lv, scale);
966 #if _has_multibyte && defined(mbwidth)
967 					wc = 0;
968 #endif
969 					goto str_cvt;
970 				}
971 #endif
972 				if(lv == 0 && precis == 0)
973 					break;
974 				if(lv < 0 && fmt == 'd' )
975 				{	flags |= SFFMT_MINUS;
976 					if(lv == HIGHBITL) /* avoid overflow */
977 					{	lv = (Sflong_t)(HIGHBITL/base);
978 						*--sp = _Sfdigits[HIGHBITL -
979 								  ((Sfulong_t)lv)*base];
980 					}
981 					else	lv = -lv;
982 				}
983 				if(n_s < 0)	/* base 10 */
984 				{	Sflong_t	nv;
985 					sfucvt(lv,sp,nv,ssp,Sflong_t,Sfulong_t);
986 				}
987 				else if(n_s > 0) /* base power-of-2 */
988 				{	do
989 					{	*--sp = ssp[lv&n_s];
990 					} while((lv = ((Sfulong_t)lv) >> n) );
991 				}
992 				else		/* general base */
993 				{	do
994 					{	*--sp = ssp[((Sfulong_t)lv)%base];
995 					} while((lv = ((Sfulong_t)lv)/base) );
996 				}
997 			} else
998 #endif
999 			if(sizeof(short) < sizeof(int) && size == sizeof(short) )
1000 			{	if(fmt == 'd')
1001 					v = (int)((short)argv.i);
1002 				else	v = (int)((ushort)argv.i);
1003 				goto int_cvt;
1004 			}
1005 			else if(size == sizeof(char))
1006 			{	if(fmt != 'd')
1007 					v = (int)((uchar)argv.i);
1008 				else
1009 				{
1010 #if _key_signed
1011 					v = (int)((signed char)argv.i);
1012 #else
1013 					if(argv.i < 0)
1014 						v = -((int)((char)(-argv.i)));
1015 					else	v =  ((int)((char)( argv.i)));
1016 #endif
1017 				}
1018 				goto int_cvt;
1019 			}
1020 			else
1021 			{	v = argv.i;
1022 			int_cvt:
1023 #if _PACKAGE_ast
1024 				if(scale)
1025 				{	sp = fmtscale(v, scale);
1026 #if _has_multibyte && defined(mbwidth)
1027 					wc = 0;
1028 #endif
1029 					goto str_cvt;
1030 				}
1031 #endif
1032 				if(v == 0 && precis == 0)
1033 					break;
1034 				if(v < 0 && fmt == 'd' )
1035 				{	flags |= SFFMT_MINUS;
1036 					if(v == HIGHBITI) /* avoid overflow */
1037 					{	v = (int)(HIGHBITI/base);
1038 						*--sp = _Sfdigits[HIGHBITI -
1039 								  ((uint)v)*base];
1040 					}
1041 					else	v = -v;
1042 				}
1043 				if(n_s < 0)	/* base 10 */
1044 				{	sfucvt(v,sp,n,ssp,int,uint);
1045 				}
1046 				else if(n_s > 0) /* base power-of-2 */
1047 				{	do
1048 					{	*--sp = ssp[v&n_s];
1049 					} while((v = ((uint)v) >> n) );
1050 				}
1051 				else /* n_s == 0, general base */
1052 				{	do
1053 					{	*--sp = ssp[((uint)v)%base];
1054 					} while((v = ((uint)v)/base) );
1055 				}
1056 			}
1057 
1058 			if(n_s < 0 && (flags&SFFMT_THOUSAND) && (n = endsp-sp) > 3)
1059 			{	if((n %= 3) == 0)
1060 					n = 3;
1061  				for(ep = buf+SLACK, endep = ep + n; ; )
1062 				{	while(ep < endep)
1063 						*ep++ = *sp++;
1064 					if(sp == endsp)
1065 						break;
1066 					if(sp <= endsp-3)
1067 						*ep++ = thousand;
1068 					endep = ep+3;
1069 				}
1070 				sp = buf+SLACK;
1071 				endsp = ep;
1072 			}
1073 
1074 			/* zero padding for precision if have room in buffer */
1075 			if(precis > 0 && (precis -= (endsp-sp)) < (sp-buf)-64)
1076 				while(precis-- > 0)
1077 					*--sp = '0';
1078 
1079 			if(flags&SFFMT_ALTER) /* prefix */
1080 			{	if(fmt == 'o')
1081 				{	if(*sp != '0')
1082 						*--sp = '0';
1083 				}
1084 				else
1085 				{	if(width > 0 && (flags&SFFMT_ZERO))
1086 					{	/* do 0 padding first */
1087 						if(fmt == 'x' || fmt == 'X')
1088 							n = 0;
1089 						else if(dot < 2)
1090 							n = width;
1091 						else	n = base < 10 ? 2 : 3;
1092 						n += (flags&(SFFMT_MINUS|SFFMT_SIGN)) ?
1093 							1 : 0;
1094 						n = width - (n + (endsp-sp));
1095 						while(n-- > 0)
1096 							*--sp = '0';
1097 					}
1098 					if(fmt == 'x' || fmt == 'X')
1099 					{	*--sp = (char)fmt;
1100 						*--sp = '0';
1101 					}
1102 					else if(dot >= 2)
1103 					{	/* base#value notation */
1104 						*--sp = '#';
1105 						if(base < 10)
1106 							*--sp = (char)('0'+base);
1107 						else
1108 						{	*--sp = _Sfdec[(base <<= 1)+1];
1109 							*--sp = _Sfdec[base];
1110 						}
1111 					}
1112 				}
1113 			}
1114 
1115 			break;
1116 
1117 		case 'g': case 'G': /* these ultimately become %e or %f */
1118 		case 'a': case 'A':
1119 		case 'e': case 'E':
1120 		case 'f': case 'F':
1121 #if !_ast_fltmax_double
1122 			if(size == sizeof(Sfdouble_t) )
1123 			{	v = SFFMT_LDOUBLE;
1124 				valp = &argv.ld;
1125 				dval = argv.ld;
1126 			}
1127 			else
1128 #endif
1129 			{	v = 0;
1130 				valp = &argv.d;
1131 				dval = argv.d;
1132 			}
1133 
1134 			if(fmt == 'e' || fmt == 'E' && (v |= SFFMT_UPPER))
1135 			{	v |= SFFMT_EFORMAT;
1136 				n = (precis = precis < 0 ? FPRECIS : precis)+1;
1137 				ep = _sfcvt(valp,tmp+1,sizeof(tmp)-1, min(n,SF_FDIGITS),
1138 					    &decpt, &sign, &n_s, v);
1139 				goto e_format;
1140 			}
1141 			else if(fmt == 'f' || fmt == 'F' && (v |= SFFMT_UPPER))
1142 			{	precis = precis < 0 ? FPRECIS : precis;
1143 				ep = _sfcvt(valp,tmp+1,sizeof(tmp)-1, min(precis,SF_FDIGITS),
1144 					    &decpt, &sign, &n_s, v);
1145 				goto f_format;
1146 			}
1147 			else if(fmt == 'a' || fmt == 'A' && (v |= SFFMT_UPPER))
1148 			{	v |= SFFMT_AFORMAT;
1149 				if(precis < 0)
1150 				{	if(v & SFFMT_LDOUBLE)
1151 						precis = 2*(sizeof(Sfdouble_t) - 2);
1152 					else	precis = 2*(sizeof(double) - 2);
1153 				}
1154 				n = precis + 1;
1155 				ep = _sfcvt(valp,tmp+1,sizeof(tmp)-1, min(n,SF_FDIGITS),
1156 					    &decpt, &sign, &n_s, v);
1157 
1158 				sp = endsp = buf+1;	/* reserve space for sign */
1159 				*endsp++ = '0';
1160 				*endsp++ = fmt == 'a' ? 'x' : 'X';
1161 				if (!isxdigit(*ep))
1162 					goto infinite;
1163 				goto a_format;
1164 			}
1165 			else /* 'g' or 'G' format */
1166 			{	precis = precis < 0 ? FPRECIS : precis == 0 ? 1 : precis;
1167 				if(fmt == 'G')
1168 					v |= SFFMT_UPPER;
1169 				v |= SFFMT_EFORMAT;
1170 				ep = _sfcvt(valp,tmp+1,sizeof(tmp)-1, min(precis,SF_FDIGITS),
1171 					    &decpt, &sign, &n_s, v);
1172 				if(dval == 0.)
1173 					decpt = 1;
1174 				else if(*ep == 'I')
1175 					goto infinite;
1176 
1177 				if(!(flags&SFFMT_ALTER))
1178 				{	/* zap trailing 0s */
1179 					if((n = n_s) > precis)
1180 						n = precis;
1181 					while((n -= 1) >= 1 && ep[n] == '0')
1182 						;
1183 					n += 1;
1184 				}
1185 				else	n = precis;
1186 
1187 				if(decpt < -3 || decpt > precis)
1188 				{	precis = n-1;
1189 					goto e_format;
1190 				}
1191 				else
1192 				{	precis = n - decpt;
1193 					goto f_format;
1194 				}
1195 			}
1196 
1197 		e_format: /* build the x.yyyy string */
1198 			if(isalpha(*ep))
1199 				goto infinite;
1200 			sp = endsp = buf+1;	/* reserve space for sign */
1201 		a_format:
1202 			*endsp++ = *ep ? *ep++ : '0';
1203 
1204 			SFSETLOCALE(&decimal,&thousand);
1205 			if(precis > 0 || (flags&SFFMT_ALTER))
1206 				*endsp++ = decimal;
1207 			ssp = endsp;
1208 			endep = ep+precis;
1209 			while((*endsp++ = *ep++) && ep <= endep)
1210 				;
1211 			precis -= (endsp -= 1) - ssp;
1212 
1213 			/* build the exponent */
1214 			ep = endep = buf+(sizeof(buf)-1);
1215 			if(dval != 0.)
1216 			{	if((n = decpt - 1) < 0)
1217 					n = -n;
1218 				while(n > 9)
1219 				{	v = n; n /= 10;
1220 					*--ep = (char)('0' + (v - n*10));
1221 				}
1222 			}
1223 			else	n = 0;
1224 			*--ep = (char)('0' + n);
1225 			if(endep-ep <= 1)	/* at least 2 digits */
1226 				*--ep = '0';
1227 
1228 			/* the e/Exponent separator and sign */
1229 			*--ep = (decpt > 0 || dval == 0.) ? '+' : '-';
1230 			*--ep = fmt == 'a' ? 'p' : fmt == 'A' ? 'P' :
1231 				isupper(fmt) ? 'E' : 'e';
1232 
1233 			goto end_aefg;
1234 
1235 		f_format: /* data before the decimal point */
1236 			if(isalpha(*ep))
1237 			{
1238 			infinite:
1239 				flags &= ~SFFMT_ZERO;
1240 				endsp = (sp = ep)+sfslen();
1241 				ep = endep;
1242 				precis = 0;
1243 				goto end_aefg;
1244 			}
1245 
1246 			SFSETLOCALE(&decimal,&thousand);
1247 			endsp = sp = buf+1;	/* save a space for sign */
1248 			endep = ep+decpt;
1249 			if(decpt > 3 && (flags&SFFMT_THOUSAND) )
1250 			{	if((n = decpt%3) == 0)
1251 					n = 3;
1252 				while(ep < endep && (*endsp++ = *ep++) )
1253 				{	if(--n == 0 && (ep <= endep-3) )
1254 					{	*endsp++ = thousand;
1255 						n = 3;
1256 					}
1257 				}
1258 			}
1259 			else
1260 			{	while(ep < endep && (*endsp++ = *ep++))
1261 					;
1262 			}
1263 			if(endsp == sp)
1264 				*endsp++ = '0';
1265 
1266 			if(precis > 0 || (flags&SFFMT_ALTER))
1267 				*endsp++ = decimal;
1268 
1269 			if((n = -decpt) > 0)
1270 			{	/* output zeros for negative exponent */
1271 				ssp = endsp + min(n,precis);
1272 				precis -= n;
1273 				while(endsp < ssp)
1274 					*endsp++ = '0';
1275 			}
1276 
1277 			ssp = endsp;
1278 			endep = ep+precis;
1279 			while((*endsp++ = *ep++) && ep <= endep)
1280 				;
1281 			precis -= (endsp -= 1) - ssp;
1282 			ep = endep;
1283 		end_aefg:
1284 			flags |= SFFMT_FLOAT;
1285 			if(sign)
1286 				flags |= SFFMT_MINUS;
1287 			break;
1288 		}
1289 
1290 		if(flags == 0 && width <= 0)
1291 			goto do_output;
1292 
1293 		if(flags&SFFMT_PREFIX)
1294 			fmt = (flags&SFFMT_MINUS) ? '-' : (flags&SFFMT_SIGN) ? '+' : ' ';
1295 
1296 		n = (endsp-sp) + (endep-ep) + (precis <= 0 ? 0 : precis) +
1297 		    ((flags&SFFMT_PREFIX) ? 1 : 0);
1298 		if((v = width-n) <= 0)
1299 			v = 0;
1300 		else if(!(flags&SFFMT_ZERO)) /* right padding */
1301 		{	if(flags&SFFMT_LEFT)
1302 				v = -v;
1303 			else if(flags&SFFMT_PREFIX) /* blank padding, output prefix now */
1304 			{	*--sp = fmt;
1305 				flags &= ~SFFMT_PREFIX;
1306 			}
1307 		}
1308 
1309 		if(flags&SFFMT_PREFIX) /* put out the prefix */
1310 		{	SFputc(f,fmt);
1311 			if(fmt != ' ')
1312 				flags |= SFFMT_ZERO;
1313 		}
1314 
1315 		if((n = v) > 0) /* left padding */
1316 		{	v = (flags&SFFMT_ZERO) ? '0' : ' ';
1317 			SFnputc(f,v,n);
1318 		}
1319 
1320 		if((n = precis) > 0 && !(flags&SFFMT_FLOAT))
1321 		{	/* padding for integer precision */
1322 			SFnputc(f,'0',n);
1323 			precis = 0;
1324 		}
1325 
1326 	do_output:
1327 		if((n = endsp-sp) > 0)
1328 			SFwrite(f,sp,n);
1329 
1330 		if(flags&(SFFMT_FLOAT|SFFMT_LEFT))
1331 		{	/* SFFMT_FLOAT: right padding for float precision */
1332 			if((n = precis) > 0)
1333 				SFnputc(f,'0',n);
1334 
1335 			/* SFFMT_FLOAT: the exponent of %eE */
1336 			if((n = endep - (sp = ep)) > 0)
1337 				SFwrite(f,sp,n);
1338 
1339 			/* SFFMT_LEFT: right padding */
1340 			if((n = -v) > 0)
1341 				{ SFnputc(f,' ',n); }
1342 		}
1343 	}
1344 
1345 pop_fmt:
1346 	if(fp)
1347 	{	free(fp);
1348 		fp = NIL(Fmtpos_t*);
1349 	}
1350 	while((fm = fmstk) ) /* pop the format stack and continue */
1351 	{	if(fm->eventf)
1352 		{	if(!form || !form[0])
1353 				(*fm->eventf)(f,SF_FINAL,NIL(Void_t*),ft);
1354 			else if((*fm->eventf)(f,SF_DPOP,(Void_t*)form,ft) < 0)
1355 				goto loop_fmt;
1356 		}
1357 
1358 		fmstk = fm->next;
1359 		if((form = fm->form) )
1360 		{	SFMBCPY(&fmbs,&fm->mbs);
1361 			va_copy(args, fm->args);
1362 			oform = fm->oform;
1363 			va_copy(oargs,fm->oargs);
1364 			argn = fm->argn;
1365 			fp = fm->fp;
1366 		}
1367 		ft = fm->ft;
1368 		free(fm);
1369 		if(form && form[0])
1370 			goto loop_fmt;
1371 	}
1372 
1373 done:
1374 	if(fp)
1375 		free(fp);
1376 	while((fm = fmstk) )
1377 	{	if(fm->eventf)
1378 			(*fm->eventf)(f,SF_FINAL,NIL(Void_t*),fm->ft);
1379 		fmstk = fm->next;
1380 		free(fm);
1381 	}
1382 
1383 	SFEND(f);
1384 
1385 	n = f->next - f->data;
1386 	if((sp = (char*)f->data) == data)
1387 		f->endw = f->endr = f->endb = f->data = NIL(uchar*);
1388 	f->next = f->data;
1389 
1390 	if((((flags = f->flags)&SF_SHARE) && !(flags&SF_PUBLIC) ) ||
1391 	   (n > 0 && (sp == data || (flags&SF_LINE) ) ) )
1392 		(void)SFWRITE(f,(Void_t*)sp,n);
1393 	else	f->next += n;
1394 
1395 	SFOPEN(f,0);
1396 	SFMTXRETURN(f, n_output);
1397 }
1398