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