1 /*********************************************************************** 2 * * 3 * This software is part of the ast package * 4 * Copyright (c) 1985-2007 AT&T Knowledge Ventures * 5 * and is licensed under the * 6 * Common Public License, Version 1.0 * 7 * by AT&T Knowledge Ventures * 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 char *tls[2], **ls; /* for %..[separ]s */ 114 char* t_str; /* stuff between () */ 115 ssize_t n_str; /* its length */ 116 117 Argv_t argv; /* for extf to return value */ 118 Sffmt_t *ft; /* format environment */ 119 Fmt_t *fm, *fmstk; /* stack contexts */ 120 121 char* oform; /* original format string */ 122 va_list oargs; /* original arg list */ 123 Fmtpos_t* fp; /* arg position list */ 124 int argp, argn; /* arg position and number */ 125 126 #define SLACK 1024 127 char buf[SF_MAXDIGITS+SLACK], tmp[SF_MAXDIGITS+1], data[SF_GRAIN]; 128 int decimal = 0, thousand = 0; 129 130 #if _has_multibyte 131 wchar_t* wsp; 132 SFMBDCL(fmbs) /* state of format string */ 133 SFMBDCL(mbs) /* state of some string */ 134 #ifdef mbwidth 135 char* osp; 136 int n_w, sf_wcwidth; 137 #endif 138 #endif 139 140 /* local io system */ 141 int o, n_output; 142 #define SMputc(f,c) { if((o = SFFLSBUF(f,c)) >= 0 ) n_output += 1; \ 143 else { SFBUF(f); goto done; } \ 144 } 145 #define SMnputc(f,c,n) { if((o = SFNPUTC(f,c,n)) > 0 ) n_output += 1; \ 146 if(o != n) { SFBUF(f); goto done; } \ 147 } 148 #define SMwrite(f,s,n) { if((o = SFWRITE(f,(Void_t*)s,n)) > 0 ) n_output += o; \ 149 if(o != n) { SFBUF(f); goto done; } \ 150 } 151 #if _sffmt_small /* these macros are made smaller at some performance cost */ 152 #define SFBUF(f) 153 #define SFINIT(f) (n_output = 0) 154 #define SFEND(f) 155 #define SFputc(f,c) SMputc(f,c) 156 #define SFnputc(f,c,n) SMnputc(f,c,n) 157 #define SFwrite(f,s,n) SMwrite(f,s,n) 158 #else 159 uchar *d, *endd; 160 #define SFBUF(f) (d = f->next, endd = f->endb) 161 #define SFINIT(f) (SFBUF(f), n_output = 0) 162 #define SFEND(f) ((n_output += d - f->next), (f->next = d)) 163 #define SFputc(f,c) { if(d < endd) { *d++ = (uchar)c; } \ 164 else { SFEND(f); SMputc(f,c); SFBUF(f); } \ 165 } 166 #define SFnputc(f,c,n) { if(d+n <= endd) { while(n--) *d++ = (uchar)(c); } \ 167 else { SFEND(f); SMnputc(f,c,n); SFBUF(f); } \ 168 } 169 #define SFwrite(f,s,n) { if(d+n <= endd) { while(n--) *d++ = (uchar)(*s++); } \ 170 else { SFEND(f); SMwrite(f,s,n); SFBUF(f); } \ 171 } 172 #endif /* _sffmt_small */ 173 174 SFCVINIT(); /* initialize conversion tables */ 175 176 SFMTXSTART(f,-1); 177 178 if(!form) 179 SFMTXRETURN(f, -1); 180 181 /* make sure stream is in write mode and buffer is not NULL */ 182 if(f->mode != SF_WRITE && _sfmode(f,SF_WRITE,0) < 0) 183 SFMTXRETURN(f, -1); 184 185 SFLOCK(f,0); 186 187 if(!f->data && !(f->flags&SF_STRING)) 188 { f->data = f->next = (uchar*)data; 189 f->endb = f->data+sizeof(data); 190 } 191 SFINIT(f); 192 #if _has_multibyte && defined(mbwidth) 193 sf_wcwidth = f->flags & SF_WCWIDTH; 194 #endif 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_LONG; 666 case 's': 667 if(base >= 0) /* list of strings */ 668 { if(!(ls = argv.sp) || !ls[0]) 669 continue; 670 } 671 else 672 { if(!(sp = argv.s)) 673 { sp = "(null)"; 674 flags &= ~SFFMT_LONG; 675 } 676 #if _PACKAGE_ast 677 str_cvt: 678 if(scale) 679 { size = base = -1; 680 flags &= ~SFFMT_LONG; 681 } 682 #endif 683 ls = tls; tls[0] = sp; 684 } 685 for(sp = *ls;;) 686 { /* v: number of bytes w: print width of those v bytes */ 687 #if _has_multibyte 688 if(flags & SFFMT_LONG) 689 { v = 0; 690 #ifdef mbwidth 691 w = 0; 692 #endif 693 SFMBCLR(&mbs); 694 for(n = 0, wsp = (wchar_t*)sp;; ++wsp, ++n) 695 { if((size >= 0 && n >= size) || 696 (size < 0 && *wsp == 0) ) 697 break; 698 if((n_s = wcrtomb(buf, *wsp, &mbs)) <= 0) 699 break; 700 #ifdef mbwidth 701 if(sf_wcwidth ) 702 { n_w = mbwidth(*wsp); 703 if(precis >= 0 && (w+n_w) > precis ) 704 break; 705 w += n_w; 706 } 707 else 708 #endif 709 if(precis >= 0 && (v+n_s) > precis ) 710 break; 711 v += n_s; 712 } 713 #ifdef mbwidth 714 if(!sf_wcwidth ) 715 w = v; 716 #endif 717 } 718 #if defined(mbwide) && defined(mbchar) && defined(mbwidth) 719 else if (mbwide()) 720 { w = 0; 721 SFMBCLR(&mbs); 722 ssp = sp; 723 for(;;) 724 { if((size >= 0 && w >= size) || 725 (size < 0 && *ssp == 0) ) 726 break; 727 osp = ssp; 728 n = mbchar(osp); 729 n_w = sf_wcwidth ? mbwidth(n) : 1; 730 if(precis >= 0 && (w+n_w) > precis ) 731 break; 732 w += n_w; 733 ssp = osp; 734 } 735 v = ssp - sp; 736 } 737 #endif 738 else 739 #endif 740 { if((v = size) < 0) 741 for(v = 0; sp[v]; ++v) 742 if(v == precis) 743 break; 744 if(precis >= 0 && v > precis) 745 v = precis; 746 w = v; 747 } 748 749 if((n = width - w) > 0 && !(flags&SFFMT_LEFT) ) 750 { if(flags&SFFMT_CENTER) 751 { n -= (k = n/2); 752 SFnputc(f, ' ', k); 753 } 754 else 755 { 756 SFnputc(f, ' ', n); 757 n = 0; 758 } 759 } 760 #if _has_multibyte 761 if(flags & SFFMT_LONG) 762 { SFMBCLR(&mbs); 763 for(wsp = (wchar_t*)sp; w > 0; ++wsp, --w) 764 { if((n_s = wcrtomb(buf, *wsp, &mbs)) <= 0) 765 break; 766 sp = buf; SFwrite(f, sp, n_s); 767 } 768 } 769 else 770 #endif 771 { SFwrite(f,sp,v); } 772 if(n > 0) 773 { SFnputc(f,' ',n); } 774 if(!(sp = *++ls)) 775 break; 776 else if(base > 0) 777 { SFputc(f,base); } 778 } 779 continue; 780 781 case 'C': 782 flags = (flags & ~SFFMT_TYPES) | SFFMT_LONG; 783 case 'c': 784 if(precis <= 0) /* # of times to repeat a character */ 785 precis = 1; 786 #if _has_multibyte 787 if(flags & SFFMT_LONG) 788 { if(base >= 0) 789 { if(!(wsp = (wchar_t*)argv.s) ) 790 continue; 791 for(size = 0; wsp[size]; ++size) 792 ; 793 } 794 else 795 { wsp = &argv.wc; 796 size = 1; 797 } 798 } 799 else 800 #endif 801 { if(base >= 0) 802 { if(!(sp = argv.s) ) 803 continue; 804 size = strlen(sp); 805 } 806 else 807 { argv.c = (char)(argv.i); 808 sp = &argv.c; 809 size = 1; 810 } 811 } 812 813 while(size > 0) 814 { 815 #if _has_multibyte 816 if(flags&SFFMT_LONG) 817 { SFMBCLR(&mbs); 818 if((n_s = wcrtomb(buf, *wsp++, &mbs)) <= 0) 819 break; 820 #ifdef mbwidth 821 if(sf_wcwidth) 822 n_s = mbwidth(*(wsp - 1)); 823 #endif 824 n = width - precis*n_s; /* padding amount */ 825 } 826 else 827 #endif 828 if(flags&SFFMT_ALTER) 829 { n_s = chr2str(buf, *sp++); 830 n = width - precis*n_s; 831 } 832 else 833 { fmt = *sp++; 834 n = width - precis; 835 } 836 837 if(n > 0 && !(flags&SFFMT_LEFT) ) 838 { if(flags&SFFMT_CENTER) 839 { n -= (k = n/2); 840 SFnputc(f, ' ', k); 841 } 842 else 843 { SFnputc(f, ' ', n); 844 n = 0; 845 } 846 } 847 848 v = precis; /* need this because SFnputc may clear it */ 849 #if _has_multibyte 850 if(flags&SFFMT_LONG) 851 { for(; v > 0; --v) 852 { ssp = buf; k = n_s; SFwrite(f,ssp,k); } 853 } 854 else 855 #endif 856 if(flags&SFFMT_ALTER) 857 { for(; v > 0; --v) 858 { ssp = buf; k = n_s; SFwrite(f,ssp,k); } 859 } 860 else 861 { SFnputc(f, fmt, v); 862 } 863 864 if(n > 0) 865 { SFnputc(f,' ',n); }; 866 867 if((size -= 1) > 0 && base > 0) 868 { SFputc(f,base); } 869 } 870 continue; 871 872 case 'n': /* return current output length */ 873 SFEND(f); 874 #if !_ast_intmax_long 875 if(size == sizeof(Sflong_t) ) 876 *((Sflong_t*)argv.vp) = (Sflong_t)n_output; 877 else 878 #endif 879 if(size == sizeof(long)) 880 *((long*)argv.vp) = (long)n_output; 881 else if(size == sizeof(short) ) 882 *((short*)argv.vp) = (short)n_output; 883 else if(size == sizeof(uchar) ) 884 *((uchar*)argv.vp) = (uchar)n_output; 885 else *((int*)argv.vp) = (int)n_output; 886 887 continue; 888 889 case 'p': /* pointer value */ 890 fmt = 'x'; 891 base = 16; n_s = 15; n = 4; 892 flags = (flags&~(SFFMT_SIGN|SFFMT_BLANK|SFFMT_ZERO))|SFFMT_ALTER; 893 #if _more_void_int 894 lv = (Sflong_t)((Sfulong_t)argv.vp); 895 goto long_cvt; 896 #else 897 v = (int)((uint)argv.vp); 898 goto int_cvt; 899 #endif 900 case 'o': 901 base = 8; n_s = 7; n = 3; 902 flags &= ~(SFFMT_SIGN|SFFMT_BLANK); 903 goto int_arg; 904 case 'X': 905 ssp = "0123456789ABCDEF"; 906 case 'x': 907 base = 16; n_s = 15; n = 4; 908 flags &= ~(SFFMT_SIGN|SFFMT_BLANK); 909 goto int_arg; 910 case 'i': 911 #if _PACKAGE_ast 912 if((flags&SFFMT_ALTER) && base < 0) 913 { flags &= ~SFFMT_ALTER; 914 scale = 1024; 915 } 916 #endif 917 fmt = 'd'; 918 goto d_format; 919 case 'u': 920 flags &= ~(SFFMT_SIGN|SFFMT_BLANK); 921 case 'd': 922 d_format: 923 #if _PACKAGE_ast 924 if((flags&SFFMT_ALTER) && base < 0) 925 { flags &= ~SFFMT_ALTER; 926 scale = 1000; 927 } 928 #endif 929 if(base < 2 || base > SF_RADIX) 930 base = 10; 931 if((base&(n_s = base-1)) == 0) 932 { if(base < 8) 933 n = base < 4 ? 1 : 2; 934 else if(base < 32) 935 n = base < 16 ? 3 : 4; 936 else n = base < 64 ? 5 : 6; 937 } 938 else n_s = base == 10 ? -1 : 0; 939 940 int_arg: 941 #if !_ast_intmax_long || _more_long_int || _more_void_int 942 if(size == sizeof(Sflong_t)) 943 { lv = argv.ll; 944 goto long_cvt; 945 } 946 else if(sizeof(long) < sizeof(Sflong_t) && size == sizeof(long)) 947 { if(fmt == 'd') 948 lv = (Sflong_t)argv.l; 949 else lv = (Sflong_t)argv.ul; 950 long_cvt: 951 #if _PACKAGE_ast 952 if(scale) 953 { sp = fmtscale(lv, scale); 954 goto str_cvt; 955 } 956 #endif 957 if(lv == 0 && precis == 0) 958 break; 959 if(lv < 0 && fmt == 'd' ) 960 { flags |= SFFMT_MINUS; 961 if(lv == HIGHBITL) /* avoid overflow */ 962 { lv = (Sflong_t)(HIGHBITL/base); 963 *--sp = _Sfdigits[HIGHBITL - 964 ((Sfulong_t)lv)*base]; 965 } 966 else lv = -lv; 967 } 968 if(n_s < 0) /* base 10 */ 969 { Sflong_t nv; 970 sfucvt(lv,sp,nv,ssp,Sflong_t,Sfulong_t); 971 } 972 else if(n_s > 0) /* base power-of-2 */ 973 { do 974 { *--sp = ssp[lv&n_s]; 975 } while((lv = ((Sfulong_t)lv) >> n) ); 976 } 977 else /* general base */ 978 { do 979 { *--sp = ssp[((Sfulong_t)lv)%base]; 980 } while((lv = ((Sfulong_t)lv)/base) ); 981 } 982 } else 983 #endif 984 if(sizeof(short) < sizeof(int) && size == sizeof(short) ) 985 { if(fmt == 'd') 986 v = (int)((short)argv.i); 987 else v = (int)((ushort)argv.i); 988 goto int_cvt; 989 } 990 else if(size == sizeof(char)) 991 { if(fmt != 'd') 992 v = (int)((uchar)argv.i); 993 else 994 { 995 #if _key_signed 996 v = (int)((signed char)argv.i); 997 #else 998 if(argv.i < 0) 999 v = -((int)((char)(-argv.i))); 1000 else v = ((int)((char)( argv.i))); 1001 #endif 1002 } 1003 goto int_cvt; 1004 } 1005 else 1006 { v = argv.i; 1007 int_cvt: 1008 #if _PACKAGE_ast 1009 if(scale) 1010 { sp = fmtscale(v, scale); 1011 goto str_cvt; 1012 } 1013 #endif 1014 if(v == 0 && precis == 0) 1015 break; 1016 if(v < 0 && fmt == 'd' ) 1017 { flags |= SFFMT_MINUS; 1018 if(v == HIGHBITI) /* avoid overflow */ 1019 { v = (int)(HIGHBITI/base); 1020 *--sp = _Sfdigits[HIGHBITI - 1021 ((uint)v)*base]; 1022 } 1023 else v = -v; 1024 } 1025 if(n_s < 0) /* base 10 */ 1026 { sfucvt(v,sp,n,ssp,int,uint); 1027 } 1028 else if(n_s > 0) /* base power-of-2 */ 1029 { do 1030 { *--sp = ssp[v&n_s]; 1031 } while((v = ((uint)v) >> n) ); 1032 } 1033 else /* n_s == 0, general base */ 1034 { do 1035 { *--sp = ssp[((uint)v)%base]; 1036 } while((v = ((uint)v)/base) ); 1037 } 1038 } 1039 1040 if(n_s < 0 && (flags&SFFMT_THOUSAND) && (n = endsp-sp) > 3) 1041 { if((n %= 3) == 0) 1042 n = 3; 1043 for(ep = buf+SLACK, endep = ep + n; ; ) 1044 { while(ep < endep) 1045 *ep++ = *sp++; 1046 if(sp == endsp) 1047 break; 1048 if(sp <= endsp-3) 1049 *ep++ = thousand; 1050 endep = ep+3; 1051 } 1052 sp = buf+SLACK; 1053 endsp = ep; 1054 } 1055 1056 /* zero padding for precision if have room in buffer */ 1057 if(precis > 0 && (precis -= (endsp-sp)) < (sp-buf)-64) 1058 while(precis-- > 0) 1059 *--sp = '0'; 1060 1061 if(flags&SFFMT_ALTER) /* prefix */ 1062 { if(fmt == 'o') 1063 { if(*sp != '0') 1064 *--sp = '0'; 1065 } 1066 else 1067 { if(width > 0 && (flags&SFFMT_ZERO)) 1068 { /* do 0 padding first */ 1069 if(fmt == 'x' || fmt == 'X') 1070 n = 0; 1071 else if(dot < 2) 1072 n = width; 1073 else n = base < 10 ? 2 : 3; 1074 n += (flags&(SFFMT_MINUS|SFFMT_SIGN)) ? 1075 1 : 0; 1076 n = width - (n + (endsp-sp)); 1077 while(n-- > 0) 1078 *--sp = '0'; 1079 } 1080 if(fmt == 'x' || fmt == 'X') 1081 { *--sp = (char)fmt; 1082 *--sp = '0'; 1083 } 1084 else if(dot >= 2) 1085 { /* base#value notation */ 1086 *--sp = '#'; 1087 if(base < 10) 1088 *--sp = (char)('0'+base); 1089 else 1090 { *--sp = _Sfdec[(base <<= 1)+1]; 1091 *--sp = _Sfdec[base]; 1092 } 1093 } 1094 } 1095 } 1096 1097 break; 1098 1099 case 'g': case 'G': /* these ultimately become %e or %f */ 1100 case 'a': case 'A': 1101 case 'e': case 'E': 1102 case 'f': case 'F': 1103 #if !_ast_fltmax_double 1104 if(size == sizeof(Sfdouble_t) ) 1105 { v = SFFMT_LDOUBLE; 1106 dval = argv.ld; 1107 } 1108 else 1109 #endif 1110 { v = 0; 1111 dval = argv.d; 1112 } 1113 1114 if(fmt == 'e' || fmt == 'E' && (v |= SFFMT_UPPER)) 1115 { v |= SFFMT_EFORMAT; 1116 n = (precis = precis < 0 ? FPRECIS : precis)+1; 1117 ep = _sfcvt(dval,tmp+1,sizeof(tmp)-1, min(n,SF_FDIGITS), 1118 &decpt, &sign, &n_s, v); 1119 goto e_format; 1120 } 1121 else if(fmt == 'f' || fmt == 'F' && (v |= SFFMT_UPPER)) 1122 { precis = precis < 0 ? FPRECIS : precis; 1123 ep = _sfcvt(dval,tmp+1,sizeof(tmp)-1, min(precis,SF_FDIGITS), 1124 &decpt, &sign, &n_s, v); 1125 goto f_format; 1126 } 1127 else if(fmt == 'a' || fmt == 'A' && (v |= SFFMT_UPPER)) 1128 { v |= SFFMT_AFORMAT; 1129 if(precis < 0) 1130 { if(v & SFFMT_LDOUBLE) 1131 precis = 2*(sizeof(Sfdouble_t) - 2); 1132 else precis = 2*(sizeof(double) - 2); 1133 } 1134 n = precis + 1; 1135 ep = _sfcvt(dval,tmp+1,sizeof(tmp)-1, min(n,SF_FDIGITS), 1136 &decpt, &sign, &n_s, v); 1137 1138 sp = endsp = buf+1; /* reserve space for sign */ 1139 *endsp++ = '0'; 1140 *endsp++ = fmt == 'a' ? 'x' : 'X'; 1141 if (!isxdigit(*ep)) 1142 goto infinite; 1143 goto a_format; 1144 } 1145 else /* 'g' or 'G' format */ 1146 { precis = precis < 0 ? FPRECIS : precis == 0 ? 1 : precis; 1147 if(fmt == 'G') 1148 v |= SFFMT_UPPER; 1149 v |= SFFMT_EFORMAT; 1150 ep = _sfcvt(dval,tmp+1,sizeof(tmp)-1, min(precis,SF_FDIGITS), 1151 &decpt, &sign, &n_s, v); 1152 if(dval == 0.) 1153 decpt = 1; 1154 else if(*ep == 'I') 1155 goto infinite; 1156 1157 if(!(flags&SFFMT_ALTER)) 1158 { /* zap trailing 0s */ 1159 if((n = n_s) > precis) 1160 n = precis; 1161 while((n -= 1) >= 1 && ep[n] == '0') 1162 ; 1163 n += 1; 1164 } 1165 else n = precis; 1166 1167 if(decpt < -3 || decpt > precis) 1168 { precis = n-1; 1169 goto e_format; 1170 } 1171 else 1172 { precis = n - decpt; 1173 goto f_format; 1174 } 1175 } 1176 1177 e_format: /* build the x.yyyy string */ 1178 if(isalpha(*ep)) 1179 goto infinite; 1180 sp = endsp = buf+1; /* reserve space for sign */ 1181 a_format: 1182 *endsp++ = *ep ? *ep++ : '0'; 1183 1184 SFSETLOCALE(&decimal,&thousand); 1185 if(precis > 0 || (flags&SFFMT_ALTER)) 1186 *endsp++ = decimal; 1187 ssp = endsp; 1188 endep = ep+precis; 1189 while((*endsp++ = *ep++) && ep <= endep) 1190 ; 1191 precis -= (endsp -= 1) - ssp; 1192 1193 /* build the exponent */ 1194 ep = endep = buf+(sizeof(buf)-1); 1195 if(dval != 0.) 1196 { if((n = decpt - 1) < 0) 1197 n = -n; 1198 while(n > 9) 1199 { v = n; n /= 10; 1200 *--ep = (char)('0' + (v - n*10)); 1201 } 1202 } 1203 else n = 0; 1204 *--ep = (char)('0' + n); 1205 if(endep-ep <= 1) /* at least 2 digits */ 1206 *--ep = '0'; 1207 1208 /* the e/Exponent separator and sign */ 1209 *--ep = (decpt > 0 || dval == 0.) ? '+' : '-'; 1210 *--ep = fmt == 'a' ? 'p' : fmt == 'A' ? 'P' : 1211 isupper(fmt) ? 'E' : 'e'; 1212 1213 goto end_aefg; 1214 1215 f_format: /* data before the decimal point */ 1216 if(isalpha(*ep)) 1217 { 1218 infinite: 1219 flags &= ~SFFMT_ZERO; 1220 endsp = (sp = ep)+sfslen(); 1221 ep = endep; 1222 precis = 0; 1223 goto end_aefg; 1224 } 1225 1226 SFSETLOCALE(&decimal,&thousand); 1227 endsp = sp = buf+1; /* save a space for sign */ 1228 endep = ep+decpt; 1229 if(decpt > 3 && (flags&SFFMT_THOUSAND) ) 1230 { if((n = decpt%3) == 0) 1231 n = 3; 1232 while(ep < endep && (*endsp++ = *ep++) ) 1233 { if(--n == 0 && (ep <= endep-3) ) 1234 { *endsp++ = thousand; 1235 n = 3; 1236 } 1237 } 1238 } 1239 else 1240 { while(ep < endep && (*endsp++ = *ep++)) 1241 ; 1242 } 1243 if(endsp == sp) 1244 *endsp++ = '0'; 1245 1246 if(precis > 0 || (flags&SFFMT_ALTER)) 1247 *endsp++ = decimal; 1248 1249 if((n = -decpt) > 0) 1250 { /* output zeros for negative exponent */ 1251 ssp = endsp + min(n,precis); 1252 precis -= n; 1253 while(endsp < ssp) 1254 *endsp++ = '0'; 1255 } 1256 1257 ssp = endsp; 1258 endep = ep+precis; 1259 while((*endsp++ = *ep++) && ep <= endep) 1260 ; 1261 precis -= (endsp -= 1) - ssp; 1262 ep = endep; 1263 end_aefg: 1264 flags |= SFFMT_FLOAT; 1265 if(sign) 1266 flags |= SFFMT_MINUS; 1267 break; 1268 } 1269 1270 if(flags == 0 && width <= 0) 1271 goto do_output; 1272 1273 if(flags&SFFMT_PREFIX) 1274 fmt = (flags&SFFMT_MINUS) ? '-' : (flags&SFFMT_SIGN) ? '+' : ' '; 1275 1276 n = (endsp-sp) + (endep-ep) + (precis <= 0 ? 0 : precis) + 1277 ((flags&SFFMT_PREFIX) ? 1 : 0); 1278 if((v = width-n) <= 0) 1279 v = 0; 1280 else if(!(flags&SFFMT_ZERO)) /* right padding */ 1281 { if(flags&SFFMT_LEFT) 1282 v = -v; 1283 else if(flags&SFFMT_PREFIX) /* blank padding, output prefix now */ 1284 { *--sp = fmt; 1285 flags &= ~SFFMT_PREFIX; 1286 } 1287 } 1288 1289 if(flags&SFFMT_PREFIX) /* put out the prefix */ 1290 { SFputc(f,fmt); 1291 if(fmt != ' ') 1292 flags |= SFFMT_ZERO; 1293 } 1294 1295 if((n = v) > 0) /* left padding */ 1296 { v = (flags&SFFMT_ZERO) ? '0' : ' '; 1297 SFnputc(f,v,n); 1298 } 1299 1300 if((n = precis) > 0 && !(flags&SFFMT_FLOAT)) 1301 { /* padding for integer precision */ 1302 SFnputc(f,'0',n); 1303 precis = 0; 1304 } 1305 1306 do_output: 1307 if((n = endsp-sp) > 0) 1308 SFwrite(f,sp,n); 1309 1310 if(flags&(SFFMT_FLOAT|SFFMT_LEFT)) 1311 { /* SFFMT_FLOAT: right padding for float precision */ 1312 if((n = precis) > 0) 1313 SFnputc(f,'0',n); 1314 1315 /* SFFMT_FLOAT: the exponent of %eE */ 1316 if((n = endep - (sp = ep)) > 0) 1317 SFwrite(f,sp,n); 1318 1319 /* SFFMT_LEFT: right padding */ 1320 if((n = -v) > 0) 1321 { SFnputc(f,' ',n); } 1322 } 1323 } 1324 1325 pop_fmt: 1326 if(fp) 1327 { free(fp); 1328 fp = NIL(Fmtpos_t*); 1329 } 1330 while((fm = fmstk) ) /* pop the format stack and continue */ 1331 { if(fm->eventf) 1332 { if(!form || !form[0]) 1333 (*fm->eventf)(f,SF_FINAL,NIL(Void_t*),ft); 1334 else if((*fm->eventf)(f,SF_DPOP,(Void_t*)form,ft) < 0) 1335 goto loop_fmt; 1336 } 1337 1338 fmstk = fm->next; 1339 if((form = fm->form) ) 1340 { SFMBCPY(&fmbs,&fm->mbs); 1341 va_copy(args, fm->args); 1342 oform = fm->oform; 1343 va_copy(oargs,fm->oargs); 1344 argn = fm->argn; 1345 fp = fm->fp; 1346 } 1347 ft = fm->ft; 1348 free(fm); 1349 if(form && form[0]) 1350 goto loop_fmt; 1351 } 1352 1353 done: 1354 if(fp) 1355 free(fp); 1356 while((fm = fmstk) ) 1357 { if(fm->eventf) 1358 (*fm->eventf)(f,SF_FINAL,NIL(Void_t*),fm->ft); 1359 fmstk = fm->next; 1360 free(fm); 1361 } 1362 1363 SFEND(f); 1364 1365 n = f->next - f->data; 1366 if((sp = (char*)f->data) == data) 1367 f->endw = f->endr = f->endb = f->data = NIL(uchar*); 1368 f->next = f->data; 1369 1370 if((((flags = f->flags)&SF_SHARE) && !(flags&SF_PUBLIC) ) || 1371 (n > 0 && (sp == data || (flags&SF_LINE) ) ) ) 1372 (void)SFWRITE(f,(Void_t*)sp,n); 1373 else f->next += n; 1374 1375 SFOPEN(f,0); 1376 SFMTXRETURN(f, n_output); 1377 } 1378