1 /* 2 * Copyright (c) 2000-2001, 2004 Sendmail, Inc. and its suppliers. 3 * All rights reserved. 4 * Copyright (c) 1990 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Chris Torek. 9 * 10 * By using this file, you agree to the terms and conditions set 11 * forth in the LICENSE file which can be found at the top level of 12 * the sendmail distribution. 13 */ 14 15 #pragma ident "%Z%%M% %I% %E% SMI" 16 17 #include <sm/gen.h> 18 SM_IDSTR(id, "@(#)$Id: vfprintf.c,v 1.53 2004/08/03 20:54:49 ca Exp $") 19 20 /* 21 ** Overall: 22 ** Actual printing innards. 23 ** This code is large and complicated... 24 */ 25 26 #include <sys/types.h> 27 #include <stdlib.h> 28 #include <string.h> 29 #include <errno.h> 30 #include <sm/config.h> 31 #include <sm/varargs.h> 32 #include <sm/io.h> 33 #include <sm/heap.h> 34 #include <sm/conf.h> 35 #include "local.h" 36 #include "fvwrite.h" 37 38 static int sm_bprintf __P((SM_FILE_T *, const char *, va_list)); 39 static void sm_find_arguments __P((const char *, va_list , va_list **)); 40 static void sm_grow_type_table_x __P((unsigned char **, int *)); 41 static int sm_print __P((SM_FILE_T *, int, struct sm_uio *)); 42 43 /* 44 ** SM_PRINT -- print/flush to the file 45 ** 46 ** Flush out all the vectors defined by the given uio, 47 ** then reset it so that it can be reused. 48 ** 49 ** Parameters: 50 ** fp -- file pointer 51 ** timeout -- time to complete operation (milliseconds) 52 ** uio -- vector list of memory locations of data for printing 53 ** 54 ** Results: 55 ** Success: 0 (zero) 56 ** Failure: 57 */ 58 59 static int 60 sm_print(fp, timeout, uio) 61 SM_FILE_T *fp; 62 int timeout; 63 register struct sm_uio *uio; 64 { 65 register int err; 66 67 if (uio->uio_resid == 0) 68 { 69 uio->uio_iovcnt = 0; 70 return 0; 71 } 72 err = sm_fvwrite(fp, timeout, uio); 73 uio->uio_resid = 0; 74 uio->uio_iovcnt = 0; 75 return err; 76 } 77 78 /* 79 ** SM_BPRINTF -- allow formating to an unbuffered file. 80 ** 81 ** Helper function for `fprintf to unbuffered unix file': creates a 82 ** temporary buffer (via a "fake" file pointer). 83 ** We only work on write-only files; this avoids 84 ** worries about ungetc buffers and so forth. 85 ** 86 ** Parameters: 87 ** fp -- the file to send the o/p to 88 ** fmt -- format instructions for the o/p 89 ** ap -- vectors of data units used for formating 90 ** 91 ** Results: 92 ** Failure: SM_IO_EOF and errno set 93 ** Success: number of data units used in the formating 94 ** 95 ** Side effects: 96 ** formatted o/p can be SM_IO_BUFSIZ length maximum 97 */ 98 99 static int 100 sm_bprintf(fp, fmt, ap) 101 SM_FILE_T *fp; 102 const char *fmt; 103 SM_VA_LOCAL_DECL 104 { 105 int ret; 106 SM_FILE_T fake; 107 unsigned char buf[SM_IO_BUFSIZ]; 108 extern const char SmFileMagic[]; 109 110 /* copy the important variables */ 111 fake.sm_magic = SmFileMagic; 112 fake.f_timeout = SM_TIME_FOREVER; 113 fake.f_timeoutstate = SM_TIME_BLOCK; 114 fake.f_flags = fp->f_flags & ~SMNBF; 115 fake.f_file = fp->f_file; 116 fake.f_cookie = fp->f_cookie; 117 fake.f_write = fp->f_write; 118 fake.f_close = NULL; 119 fake.f_open = NULL; 120 fake.f_read = NULL; 121 fake.f_seek = NULL; 122 fake.f_setinfo = fake.f_getinfo = NULL; 123 fake.f_type = "sm_bprintf:fake"; 124 125 /* set up the buffer */ 126 fake.f_bf.smb_base = fake.f_p = buf; 127 fake.f_bf.smb_size = fake.f_w = sizeof(buf); 128 fake.f_lbfsize = 0; /* not actually used, but Just In Case */ 129 130 /* do the work, then copy any error status */ 131 ret = sm_io_vfprintf(&fake, SM_TIME_FOREVER, fmt, ap); 132 if (ret >= 0 && sm_io_flush(&fake, SM_TIME_FOREVER)) 133 ret = SM_IO_EOF; /* errno set by sm_io_flush */ 134 if (fake.f_flags & SMERR) 135 fp->f_flags |= SMERR; 136 return ret; 137 } 138 139 140 #define BUF 40 141 142 #define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */ 143 144 145 /* Macros for converting digits to letters and vice versa */ 146 #define to_digit(c) ((c) - '0') 147 #define is_digit(c) ((unsigned) to_digit(c) <= 9) 148 #define to_char(n) ((char) (n) + '0') 149 150 /* Flags used during conversion. */ 151 #define ALT 0x001 /* alternate form */ 152 #define HEXPREFIX 0x002 /* add 0x or 0X prefix */ 153 #define LADJUST 0x004 /* left adjustment */ 154 #define LONGINT 0x010 /* long integer */ 155 #define QUADINT 0x020 /* quad integer */ 156 #define SHORTINT 0x040 /* short integer */ 157 #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */ 158 #define FPT 0x100 /* Floating point number */ 159 160 /* 161 ** SM_IO_VPRINTF -- performs actual formating for o/p 162 ** 163 ** Parameters: 164 ** fp -- file pointer for o/p 165 ** timeout -- time to complete the print 166 ** fmt0 -- formating directives 167 ** ap -- vectors with data units for formating 168 ** 169 ** Results: 170 ** Success: number of data units used for formatting 171 ** Failure: SM_IO_EOF and sets errno 172 */ 173 174 int 175 sm_io_vfprintf(fp, timeout, fmt0, ap) 176 SM_FILE_T *fp; 177 int timeout; 178 const char *fmt0; 179 SM_VA_LOCAL_DECL 180 { 181 register char *fmt; /* format string */ 182 register int ch; /* character from fmt */ 183 register int n, m, n2; /* handy integers (short term usage) */ 184 register char *cp; /* handy char pointer (short term usage) */ 185 register struct sm_iov *iovp;/* for PRINT macro */ 186 register int flags; /* flags as above */ 187 int ret; /* return value accumulator */ 188 int width; /* width from format (%8d), or 0 */ 189 int prec; /* precision from format (%.3d), or -1 */ 190 char sign; /* sign prefix (' ', '+', '-', or \0) */ 191 wchar_t wc; 192 ULONGLONG_T _uquad; /* integer arguments %[diouxX] */ 193 enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */ 194 int dprec; /* a copy of prec if [diouxX], 0 otherwise */ 195 int realsz; /* field size expanded by dprec */ 196 int size; /* size of converted field or string */ 197 char *xdigs="0123456789abcdef"; /* digits for [xX] conversion */ 198 #define NIOV 8 199 struct sm_uio uio; /* output information: summary */ 200 struct sm_iov iov[NIOV];/* ... and individual io vectors */ 201 char buf[BUF]; /* space for %c, %[diouxX], %[eEfgG] */ 202 char ox[2]; /* space for 0x hex-prefix */ 203 va_list *argtable; /* args, built due to positional arg */ 204 va_list statargtable[STATIC_ARG_TBL_SIZE]; 205 int nextarg; /* 1-based argument index */ 206 va_list orgap; /* original argument pointer */ 207 208 /* 209 ** Choose PADSIZE to trade efficiency vs. size. If larger printf 210 ** fields occur frequently, increase PADSIZE and make the initialisers 211 ** below longer. 212 */ 213 #define PADSIZE 16 /* pad chunk size */ 214 static char blanks[PADSIZE] = 215 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '}; 216 static char zeroes[PADSIZE] = 217 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'}; 218 219 /* 220 ** BEWARE, these `goto error' on error, and PAD uses `n'. 221 */ 222 #define PRINT(ptr, len) do { \ 223 iovp->iov_base = (ptr); \ 224 iovp->iov_len = (len); \ 225 uio.uio_resid += (len); \ 226 iovp++; \ 227 if (++uio.uio_iovcnt >= NIOV) \ 228 { \ 229 if (sm_print(fp, timeout, &uio)) \ 230 goto error; \ 231 iovp = iov; \ 232 } \ 233 } while (0) 234 #define PAD(howmany, with) do \ 235 { \ 236 if ((n = (howmany)) > 0) \ 237 { \ 238 while (n > PADSIZE) { \ 239 PRINT(with, PADSIZE); \ 240 n -= PADSIZE; \ 241 } \ 242 PRINT(with, n); \ 243 } \ 244 } while (0) 245 #define FLUSH() do \ 246 { \ 247 if (uio.uio_resid && sm_print(fp, timeout, &uio)) \ 248 goto error; \ 249 uio.uio_iovcnt = 0; \ 250 iovp = iov; \ 251 } while (0) 252 253 /* 254 ** To extend shorts properly, we need both signed and unsigned 255 ** argument extraction methods. 256 */ 257 #define SARG() \ 258 (flags&QUADINT ? SM_VA_ARG(ap, LONGLONG_T) : \ 259 flags&LONGINT ? GETARG(long) : \ 260 flags&SHORTINT ? (long) (short) GETARG(int) : \ 261 (long) GETARG(int)) 262 #define UARG() \ 263 (flags&QUADINT ? SM_VA_ARG(ap, ULONGLONG_T) : \ 264 flags&LONGINT ? GETARG(unsigned long) : \ 265 flags&SHORTINT ? (unsigned long) (unsigned short) GETARG(int) : \ 266 (unsigned long) GETARG(unsigned int)) 267 268 /* 269 ** Get * arguments, including the form *nn$. Preserve the nextarg 270 ** that the argument can be gotten once the type is determined. 271 */ 272 #define GETASTER(val) \ 273 n2 = 0; \ 274 cp = fmt; \ 275 while (is_digit(*cp)) \ 276 { \ 277 n2 = 10 * n2 + to_digit(*cp); \ 278 cp++; \ 279 } \ 280 if (*cp == '$') \ 281 { \ 282 int hold = nextarg; \ 283 if (argtable == NULL) \ 284 { \ 285 argtable = statargtable; \ 286 sm_find_arguments(fmt0, orgap, &argtable); \ 287 } \ 288 nextarg = n2; \ 289 val = GETARG(int); \ 290 nextarg = hold; \ 291 fmt = ++cp; \ 292 } \ 293 else \ 294 { \ 295 val = GETARG(int); \ 296 } 297 298 /* 299 ** Get the argument indexed by nextarg. If the argument table is 300 ** built, use it to get the argument. If its not, get the next 301 ** argument (and arguments must be gotten sequentially). 302 */ 303 304 #if SM_VA_STD 305 # define GETARG(type) \ 306 (((argtable != NULL) ? (void) (ap = argtable[nextarg]) : (void) 0), \ 307 nextarg++, SM_VA_ARG(ap, type)) 308 #else /* SM_VA_STD */ 309 # define GETARG(type) \ 310 ((argtable != NULL) ? (*((type*)(argtable[nextarg++]))) : \ 311 (nextarg++, SM_VA_ARG(ap, type))) 312 #endif /* SM_VA_STD */ 313 314 /* sorry, fprintf(read_only_file, "") returns SM_IO_EOF, not 0 */ 315 if (cantwrite(fp)) 316 { 317 errno = EBADF; 318 return SM_IO_EOF; 319 } 320 321 /* optimise fprintf(stderr) (and other unbuffered Unix files) */ 322 if ((fp->f_flags & (SMNBF|SMWR|SMRW)) == (SMNBF|SMWR) && 323 fp->f_file >= 0) 324 return sm_bprintf(fp, fmt0, ap); 325 326 fmt = (char *) fmt0; 327 argtable = NULL; 328 nextarg = 1; 329 SM_VA_COPY(orgap, ap); 330 uio.uio_iov = iovp = iov; 331 uio.uio_resid = 0; 332 uio.uio_iovcnt = 0; 333 ret = 0; 334 335 /* Scan the format for conversions (`%' character). */ 336 for (;;) 337 { 338 cp = fmt; 339 n = 0; 340 while ((wc = *fmt) != '\0') 341 { 342 if (wc == '%') 343 { 344 n = 1; 345 break; 346 } 347 fmt++; 348 } 349 if ((m = fmt - cp) != 0) 350 { 351 PRINT(cp, m); 352 ret += m; 353 } 354 if (n <= 0) 355 goto done; 356 fmt++; /* skip over '%' */ 357 358 flags = 0; 359 dprec = 0; 360 width = 0; 361 prec = -1; 362 sign = '\0'; 363 364 rflag: ch = *fmt++; 365 reswitch: switch (ch) 366 { 367 case ' ': 368 369 /* 370 ** ``If the space and + flags both appear, the space 371 ** flag will be ignored.'' 372 ** -- ANSI X3J11 373 */ 374 375 if (!sign) 376 sign = ' '; 377 goto rflag; 378 case '#': 379 flags |= ALT; 380 goto rflag; 381 case '*': 382 383 /* 384 ** ``A negative field width argument is taken as a 385 ** - flag followed by a positive field width.'' 386 ** -- ANSI X3J11 387 ** They don't exclude field widths read from args. 388 */ 389 390 GETASTER(width); 391 if (width >= 0) 392 goto rflag; 393 width = -width; 394 /* FALLTHROUGH */ 395 case '-': 396 flags |= LADJUST; 397 goto rflag; 398 case '+': 399 sign = '+'; 400 goto rflag; 401 case '.': 402 if ((ch = *fmt++) == '*') 403 { 404 GETASTER(n); 405 prec = n < 0 ? -1 : n; 406 goto rflag; 407 } 408 n = 0; 409 while (is_digit(ch)) 410 { 411 n = 10 * n + to_digit(ch); 412 ch = *fmt++; 413 } 414 if (ch == '$') 415 { 416 nextarg = n; 417 if (argtable == NULL) 418 { 419 argtable = statargtable; 420 sm_find_arguments(fmt0, orgap, 421 &argtable); 422 } 423 goto rflag; 424 } 425 prec = n < 0 ? -1 : n; 426 goto reswitch; 427 case '0': 428 429 /* 430 ** ``Note that 0 is taken as a flag, not as the 431 ** beginning of a field width.'' 432 ** -- ANSI X3J11 433 */ 434 435 flags |= ZEROPAD; 436 goto rflag; 437 case '1': case '2': case '3': case '4': 438 case '5': case '6': case '7': case '8': case '9': 439 n = 0; 440 do 441 { 442 n = 10 * n + to_digit(ch); 443 ch = *fmt++; 444 } while (is_digit(ch)); 445 if (ch == '$') 446 { 447 nextarg = n; 448 if (argtable == NULL) 449 { 450 argtable = statargtable; 451 sm_find_arguments(fmt0, orgap, 452 &argtable); 453 } 454 goto rflag; 455 } 456 width = n; 457 goto reswitch; 458 case 'h': 459 flags |= SHORTINT; 460 goto rflag; 461 case 'l': 462 if (*fmt == 'l') 463 { 464 fmt++; 465 flags |= QUADINT; 466 } 467 else 468 { 469 flags |= LONGINT; 470 } 471 goto rflag; 472 case 'q': 473 flags |= QUADINT; 474 goto rflag; 475 case 'c': 476 *(cp = buf) = GETARG(int); 477 size = 1; 478 sign = '\0'; 479 break; 480 case 'D': 481 flags |= LONGINT; 482 /*FALLTHROUGH*/ 483 case 'd': 484 case 'i': 485 _uquad = SARG(); 486 if ((LONGLONG_T) _uquad < 0) 487 { 488 _uquad = -(LONGLONG_T) _uquad; 489 sign = '-'; 490 } 491 base = DEC; 492 goto number; 493 case 'e': 494 case 'E': 495 case 'f': 496 case 'g': 497 case 'G': 498 { 499 double val; 500 char *p; 501 char fmt[16]; 502 char out[150]; 503 size_t len; 504 505 /* 506 ** This code implements floating point output 507 ** in the most portable manner possible, 508 ** relying only on 'sprintf' as defined by 509 ** the 1989 ANSI C standard. 510 ** We silently cap width and precision 511 ** at 120, to avoid buffer overflow. 512 */ 513 514 val = GETARG(double); 515 516 p = fmt; 517 *p++ = '%'; 518 if (sign) 519 *p++ = sign; 520 if (flags & ALT) 521 *p++ = '#'; 522 if (flags & LADJUST) 523 *p++ = '-'; 524 if (flags & ZEROPAD) 525 *p++ = '0'; 526 *p++ = '*'; 527 if (prec >= 0) 528 { 529 *p++ = '.'; 530 *p++ = '*'; 531 } 532 *p++ = ch; 533 *p = '\0'; 534 535 if (width > 120) 536 width = 120; 537 if (prec > 120) 538 prec = 120; 539 if (prec >= 0) 540 sprintf(out, fmt, width, prec, val); 541 else 542 sprintf(out, fmt, width, val); 543 len = strlen(out); 544 PRINT(out, len); 545 FLUSH(); 546 continue; 547 } 548 case 'n': 549 if (flags & QUADINT) 550 *GETARG(LONGLONG_T *) = ret; 551 else if (flags & LONGINT) 552 *GETARG(long *) = ret; 553 else if (flags & SHORTINT) 554 *GETARG(short *) = ret; 555 else 556 *GETARG(int *) = ret; 557 continue; /* no output */ 558 case 'O': 559 flags |= LONGINT; 560 /*FALLTHROUGH*/ 561 case 'o': 562 _uquad = UARG(); 563 base = OCT; 564 goto nosign; 565 case 'p': 566 567 /* 568 ** ``The argument shall be a pointer to void. The 569 ** value of the pointer is converted to a sequence 570 ** of printable characters, in an implementation- 571 ** defined manner.'' 572 ** -- ANSI X3J11 573 */ 574 575 /* NOSTRICT */ 576 { 577 union 578 { 579 void *p; 580 ULONGLONG_T ll; 581 unsigned long l; 582 unsigned i; 583 } u; 584 u.p = GETARG(void *); 585 if (sizeof(void *) == sizeof(ULONGLONG_T)) 586 _uquad = u.ll; 587 else if (sizeof(void *) == sizeof(long)) 588 _uquad = u.l; 589 else 590 _uquad = u.i; 591 } 592 base = HEX; 593 xdigs = "0123456789abcdef"; 594 flags |= HEXPREFIX; 595 ch = 'x'; 596 goto nosign; 597 case 's': 598 if ((cp = GETARG(char *)) == NULL) 599 cp = "(null)"; 600 if (prec >= 0) 601 { 602 /* 603 ** can't use strlen; can only look for the 604 ** NUL in the first `prec' characters, and 605 ** strlen() will go further. 606 */ 607 608 char *p = memchr(cp, 0, prec); 609 610 if (p != NULL) 611 { 612 size = p - cp; 613 if (size > prec) 614 size = prec; 615 } 616 else 617 size = prec; 618 } 619 else 620 size = strlen(cp); 621 sign = '\0'; 622 break; 623 case 'U': 624 flags |= LONGINT; 625 /*FALLTHROUGH*/ 626 case 'u': 627 _uquad = UARG(); 628 base = DEC; 629 goto nosign; 630 case 'X': 631 xdigs = "0123456789ABCDEF"; 632 goto hex; 633 case 'x': 634 xdigs = "0123456789abcdef"; 635 hex: _uquad = UARG(); 636 base = HEX; 637 /* leading 0x/X only if non-zero */ 638 if (flags & ALT && _uquad != 0) 639 flags |= HEXPREFIX; 640 641 /* unsigned conversions */ 642 nosign: sign = '\0'; 643 644 /* 645 ** ``... diouXx conversions ... if a precision is 646 ** specified, the 0 flag will be ignored.'' 647 ** -- ANSI X3J11 648 */ 649 650 number: if ((dprec = prec) >= 0) 651 flags &= ~ZEROPAD; 652 653 /* 654 ** ``The result of converting a zero value with an 655 ** explicit precision of zero is no characters.'' 656 ** -- ANSI X3J11 657 */ 658 659 cp = buf + BUF; 660 if (_uquad != 0 || prec != 0) 661 { 662 /* 663 ** Unsigned mod is hard, and unsigned mod 664 ** by a constant is easier than that by 665 ** a variable; hence this switch. 666 */ 667 668 switch (base) 669 { 670 case OCT: 671 do 672 { 673 *--cp = to_char(_uquad & 7); 674 _uquad >>= 3; 675 } while (_uquad); 676 /* handle octal leading 0 */ 677 if (flags & ALT && *cp != '0') 678 *--cp = '0'; 679 break; 680 681 case DEC: 682 /* many numbers are 1 digit */ 683 while (_uquad >= 10) 684 { 685 *--cp = to_char(_uquad % 10); 686 _uquad /= 10; 687 } 688 *--cp = to_char(_uquad); 689 break; 690 691 case HEX: 692 do 693 { 694 *--cp = xdigs[_uquad & 15]; 695 _uquad >>= 4; 696 } while (_uquad); 697 break; 698 699 default: 700 cp = "bug in sm_io_vfprintf: bad base"; 701 size = strlen(cp); 702 goto skipsize; 703 } 704 } 705 size = buf + BUF - cp; 706 skipsize: 707 break; 708 default: /* "%?" prints ?, unless ? is NUL */ 709 if (ch == '\0') 710 goto done; 711 /* pretend it was %c with argument ch */ 712 cp = buf; 713 *cp = ch; 714 size = 1; 715 sign = '\0'; 716 break; 717 } 718 719 /* 720 ** All reasonable formats wind up here. At this point, `cp' 721 ** points to a string which (if not flags&LADJUST) should be 722 ** padded out to `width' places. If flags&ZEROPAD, it should 723 ** first be prefixed by any sign or other prefix; otherwise, 724 ** it should be blank padded before the prefix is emitted. 725 ** After any left-hand padding and prefixing, emit zeroes 726 ** required by a decimal [diouxX] precision, then print the 727 ** string proper, then emit zeroes required by any leftover 728 ** floating precision; finally, if LADJUST, pad with blanks. 729 ** 730 ** Compute actual size, so we know how much to pad. 731 ** size excludes decimal prec; realsz includes it. 732 */ 733 734 realsz = dprec > size ? dprec : size; 735 if (sign) 736 realsz++; 737 else if (flags & HEXPREFIX) 738 realsz+= 2; 739 740 /* right-adjusting blank padding */ 741 if ((flags & (LADJUST|ZEROPAD)) == 0) 742 PAD(width - realsz, blanks); 743 744 /* prefix */ 745 if (sign) 746 { 747 PRINT(&sign, 1); 748 } 749 else if (flags & HEXPREFIX) 750 { 751 ox[0] = '0'; 752 ox[1] = ch; 753 PRINT(ox, 2); 754 } 755 756 /* right-adjusting zero padding */ 757 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) 758 PAD(width - realsz, zeroes); 759 760 /* leading zeroes from decimal precision */ 761 PAD(dprec - size, zeroes); 762 763 /* the string or number proper */ 764 PRINT(cp, size); 765 /* left-adjusting padding (always blank) */ 766 if (flags & LADJUST) 767 PAD(width - realsz, blanks); 768 769 /* finally, adjust ret */ 770 ret += width > realsz ? width : realsz; 771 772 FLUSH(); /* copy out the I/O vectors */ 773 } 774 done: 775 FLUSH(); 776 error: 777 if ((argtable != NULL) && (argtable != statargtable)) 778 sm_free(argtable); 779 return sm_error(fp) ? SM_IO_EOF : ret; 780 /* NOTREACHED */ 781 } 782 783 /* Type ids for argument type table. */ 784 #define T_UNUSED 0 785 #define T_SHORT 1 786 #define T_U_SHORT 2 787 #define TP_SHORT 3 788 #define T_INT 4 789 #define T_U_INT 5 790 #define TP_INT 6 791 #define T_LONG 7 792 #define T_U_LONG 8 793 #define TP_LONG 9 794 #define T_QUAD 10 795 #define T_U_QUAD 11 796 #define TP_QUAD 12 797 #define T_DOUBLE 13 798 #define TP_CHAR 15 799 #define TP_VOID 16 800 801 /* 802 ** SM_FIND_ARGUMENTS -- find all args when a positional parameter is found. 803 ** 804 ** Find all arguments when a positional parameter is encountered. Returns a 805 ** table, indexed by argument number, of pointers to each arguments. The 806 ** initial argument table should be an array of STATIC_ARG_TBL_SIZE entries. 807 ** It will be replaced with a malloc-ed one if it overflows. 808 ** 809 ** Parameters: 810 ** fmt0 -- formating directives 811 ** ap -- vector list of data unit for formating consumption 812 ** argtable -- an indexable table (returned) of 'ap' 813 ** 814 ** Results: 815 ** none. 816 */ 817 818 static void 819 sm_find_arguments(fmt0, ap, argtable) 820 const char *fmt0; 821 SM_VA_LOCAL_DECL 822 va_list **argtable; 823 { 824 register char *fmt; /* format string */ 825 register int ch; /* character from fmt */ 826 register int n, n2; /* handy integer (short term usage) */ 827 register char *cp; /* handy char pointer (short term usage) */ 828 register int flags; /* flags as above */ 829 unsigned char *typetable; /* table of types */ 830 unsigned char stattypetable[STATIC_ARG_TBL_SIZE]; 831 int tablesize; /* current size of type table */ 832 int tablemax; /* largest used index in table */ 833 int nextarg; /* 1-based argument index */ 834 835 /* Add an argument type to the table, expanding if necessary. */ 836 #define ADDTYPE(type) \ 837 ((nextarg >= tablesize) ? \ 838 (sm_grow_type_table_x(&typetable, &tablesize), 0) : 0, \ 839 typetable[nextarg++] = type, \ 840 (nextarg > tablemax) ? tablemax = nextarg : 0) 841 842 #define ADDSARG() \ 843 ((flags & LONGINT) ? ADDTYPE(T_LONG) : \ 844 ((flags & SHORTINT) ? ADDTYPE(T_SHORT) : ADDTYPE(T_INT))) 845 846 #define ADDUARG() \ 847 ((flags & LONGINT) ? ADDTYPE(T_U_LONG) : \ 848 ((flags & SHORTINT) ? ADDTYPE(T_U_SHORT) : ADDTYPE(T_U_INT))) 849 850 /* Add * arguments to the type array. */ 851 #define ADDASTER() \ 852 n2 = 0; \ 853 cp = fmt; \ 854 while (is_digit(*cp)) \ 855 { \ 856 n2 = 10 * n2 + to_digit(*cp); \ 857 cp++; \ 858 } \ 859 if (*cp == '$') \ 860 { \ 861 int hold = nextarg; \ 862 nextarg = n2; \ 863 ADDTYPE (T_INT); \ 864 nextarg = hold; \ 865 fmt = ++cp; \ 866 } \ 867 else \ 868 { \ 869 ADDTYPE (T_INT); \ 870 } 871 fmt = (char *) fmt0; 872 typetable = stattypetable; 873 tablesize = STATIC_ARG_TBL_SIZE; 874 tablemax = 0; 875 nextarg = 1; 876 (void) memset(typetable, T_UNUSED, STATIC_ARG_TBL_SIZE); 877 878 /* Scan the format for conversions (`%' character). */ 879 for (;;) 880 { 881 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++) 882 /* void */; 883 if (ch == '\0') 884 goto done; 885 fmt++; /* skip over '%' */ 886 887 flags = 0; 888 889 rflag: ch = *fmt++; 890 reswitch: switch (ch) 891 { 892 case ' ': 893 case '#': 894 goto rflag; 895 case '*': 896 ADDASTER(); 897 goto rflag; 898 case '-': 899 case '+': 900 goto rflag; 901 case '.': 902 if ((ch = *fmt++) == '*') 903 { 904 ADDASTER(); 905 goto rflag; 906 } 907 while (is_digit(ch)) 908 { 909 ch = *fmt++; 910 } 911 goto reswitch; 912 case '0': 913 goto rflag; 914 case '1': case '2': case '3': case '4': 915 case '5': case '6': case '7': case '8': case '9': 916 n = 0; 917 do 918 { 919 n = 10 * n + to_digit(ch); 920 ch = *fmt++; 921 } while (is_digit(ch)); 922 if (ch == '$') 923 { 924 nextarg = n; 925 goto rflag; 926 } 927 goto reswitch; 928 case 'h': 929 flags |= SHORTINT; 930 goto rflag; 931 case 'l': 932 flags |= LONGINT; 933 goto rflag; 934 case 'q': 935 flags |= QUADINT; 936 goto rflag; 937 case 'c': 938 ADDTYPE(T_INT); 939 break; 940 case 'D': 941 flags |= LONGINT; 942 /*FALLTHROUGH*/ 943 case 'd': 944 case 'i': 945 if (flags & QUADINT) 946 { 947 ADDTYPE(T_QUAD); 948 } 949 else 950 { 951 ADDSARG(); 952 } 953 break; 954 case 'e': 955 case 'E': 956 case 'f': 957 case 'g': 958 case 'G': 959 ADDTYPE(T_DOUBLE); 960 break; 961 case 'n': 962 if (flags & QUADINT) 963 ADDTYPE(TP_QUAD); 964 else if (flags & LONGINT) 965 ADDTYPE(TP_LONG); 966 else if (flags & SHORTINT) 967 ADDTYPE(TP_SHORT); 968 else 969 ADDTYPE(TP_INT); 970 continue; /* no output */ 971 case 'O': 972 flags |= LONGINT; 973 /*FALLTHROUGH*/ 974 case 'o': 975 if (flags & QUADINT) 976 ADDTYPE(T_U_QUAD); 977 else 978 ADDUARG(); 979 break; 980 case 'p': 981 ADDTYPE(TP_VOID); 982 break; 983 case 's': 984 ADDTYPE(TP_CHAR); 985 break; 986 case 'U': 987 flags |= LONGINT; 988 /*FALLTHROUGH*/ 989 case 'u': 990 if (flags & QUADINT) 991 ADDTYPE(T_U_QUAD); 992 else 993 ADDUARG(); 994 break; 995 case 'X': 996 case 'x': 997 if (flags & QUADINT) 998 ADDTYPE(T_U_QUAD); 999 else 1000 ADDUARG(); 1001 break; 1002 default: /* "%?" prints ?, unless ? is NUL */ 1003 if (ch == '\0') 1004 goto done; 1005 break; 1006 } 1007 } 1008 done: 1009 /* Build the argument table. */ 1010 if (tablemax >= STATIC_ARG_TBL_SIZE) 1011 { 1012 *argtable = (va_list *) 1013 sm_malloc(sizeof(va_list) * (tablemax + 1)); 1014 } 1015 1016 for (n = 1; n <= tablemax; n++) 1017 { 1018 SM_VA_COPY((*argtable)[n], ap); 1019 switch (typetable [n]) 1020 { 1021 case T_UNUSED: 1022 (void) SM_VA_ARG(ap, int); 1023 break; 1024 case T_SHORT: 1025 (void) SM_VA_ARG(ap, int); 1026 break; 1027 case T_U_SHORT: 1028 (void) SM_VA_ARG(ap, int); 1029 break; 1030 case TP_SHORT: 1031 (void) SM_VA_ARG(ap, short *); 1032 break; 1033 case T_INT: 1034 (void) SM_VA_ARG(ap, int); 1035 break; 1036 case T_U_INT: 1037 (void) SM_VA_ARG(ap, unsigned int); 1038 break; 1039 case TP_INT: 1040 (void) SM_VA_ARG(ap, int *); 1041 break; 1042 case T_LONG: 1043 (void) SM_VA_ARG(ap, long); 1044 break; 1045 case T_U_LONG: 1046 (void) SM_VA_ARG(ap, unsigned long); 1047 break; 1048 case TP_LONG: 1049 (void) SM_VA_ARG(ap, long *); 1050 break; 1051 case T_QUAD: 1052 (void) SM_VA_ARG(ap, LONGLONG_T); 1053 break; 1054 case T_U_QUAD: 1055 (void) SM_VA_ARG(ap, ULONGLONG_T); 1056 break; 1057 case TP_QUAD: 1058 (void) SM_VA_ARG(ap, LONGLONG_T *); 1059 break; 1060 case T_DOUBLE: 1061 (void) SM_VA_ARG(ap, double); 1062 break; 1063 case TP_CHAR: 1064 (void) SM_VA_ARG(ap, char *); 1065 break; 1066 case TP_VOID: 1067 (void) SM_VA_ARG(ap, void *); 1068 break; 1069 } 1070 } 1071 1072 if ((typetable != NULL) && (typetable != stattypetable)) 1073 sm_free(typetable); 1074 } 1075 1076 /* 1077 ** SM_GROW_TYPE_TABLE -- Increase the size of the type table. 1078 ** 1079 ** Parameters: 1080 ** tabletype -- type of table to grow 1081 ** tablesize -- requested new table size 1082 ** 1083 ** Results: 1084 ** Raises an exception if can't allocate memory. 1085 */ 1086 1087 static void 1088 sm_grow_type_table_x(typetable, tablesize) 1089 unsigned char **typetable; 1090 int *tablesize; 1091 { 1092 unsigned char *oldtable = *typetable; 1093 int newsize = *tablesize * 2; 1094 1095 if (*tablesize == STATIC_ARG_TBL_SIZE) 1096 { 1097 *typetable = (unsigned char *) sm_malloc_x(sizeof(unsigned char) 1098 * newsize); 1099 (void) memmove(*typetable, oldtable, *tablesize); 1100 } 1101 else 1102 { 1103 *typetable = (unsigned char *) sm_realloc_x(typetable, 1104 sizeof(unsigned char) * newsize); 1105 } 1106 (void) memset(&typetable [*tablesize], T_UNUSED, 1107 (newsize - *tablesize)); 1108 1109 *tablesize = newsize; 1110 } 1111