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