1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
|
/* @(#)format.c 1.29 98/03/31 Copyright 1985 J. Schilling */
/*
* format
* common code for printf fprintf & sprintf
*
* allows recursive printf with "%r", used in:
* error, comerr, comerrno, errmsg, errmsgno and the like
*
* Copyright (c) 1985 J. Schilling
*/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <mconfig.h>
#include <vadefs.h>
#include <strdefs.h>
#include <stdxlib.h>
#ifndef HAVE_STDLIB_H
extern char *gcvt __PR((double, int, char *));
#endif
#include <standard.h>
/*
* Some CPU's (e.g. PDP-11) cannot do logical shifts.
* They use rotate instead. Masking the low bits before,
* makes rotate work too.
*/
#define allmask(t) ((unsigned t)~((unsigned t)0))
#define lowmask(t, x) ((unsigned t)~((unsigned t)((1 << (x))-1) ))
#define rshiftmask(t, s)((allmask(t) & lowmask(t, s)) >> (s))
#define CHARMASK makemask(char)
#define SHORTMASK makemask(short)
#define INTMASK makemask(int)
#define LONGMASK makemask(long)
#ifdef DIVLBYS
extern long divlbys();
extern long modlbys();
#else
#define divlbys(val, base) ((val)/(base))
#define modlbys(val, base) ((val)%(base))
#endif
/*
* We use macros here to avoid the need to link to the international
* character routines.
* We don't need internationalization for our purpose.
*/
#define is_dig(c) (((c) >= '0') && ((c) <= '9'))
#define is_cap(c) ((c) >= 'A' && (c) <= 'Z')
#define to_cap(c) (is_cap(c) ? c : c - 'a' + 'A')
#define cap_ty(c) (is_cap(c) ? 'L' : 'I')
typedef unsigned int Uint;
typedef unsigned short Ushort;
typedef unsigned long Ulong;
#ifdef HAVE_LONGLONG
typedef unsigned long long Ullong;
#endif
typedef struct f_args {
void (*outf) __PR((char, long)); /* Func from format(fun, arg) */
long farg; /* Arg from format (fun, arg) */
int minusflag;
int flags;
int fldwidth;
int signific;
int lzero;
char *buf;
char *bufp;
char fillc;
char *prefix;
int prefixlen;
} f_args;
#define MINUSFLG 1 /* '-' flag */
#define PLUSFLG 2 /* '+' flag */
#define SPACEFLG 4 /* ' ' flag */
#define HASHFLG 8 /* '#' flag */
LOCAL void prnum __PR((Ulong, unsigned, f_args *));
LOCAL void prdnum __PR((Ulong, f_args *));
LOCAL void pronum __PR((Ulong, f_args *));
LOCAL void prxnum __PR((Ulong, f_args *));
LOCAL void prXnum __PR((Ulong, f_args *));
LOCAL int prbuf __PR((const char *, f_args *));
LOCAL int prc __PR((char, f_args *));
LOCAL int prstring __PR((const char *, f_args *));
#ifdef PROTOTYPES
EXPORT int format( void (*fun)(char, long),
long farg,
const char *fmt,
va_list args)
#else
EXPORT int format(fun, farg, fmt, args)
register void (*fun)();
register long farg;
register char *fmt;
va_list args;
#endif
{
char buf[512];
const char *sfmt;
register int unsflag;
register long val;
register char type;
register char mode;
register char c;
int count;
int i;
short sh;
const char *str;
double dval;
Ulong res;
char *rfmt;
f_args fa;
fa.outf = fun;
fa.farg = farg;
count = 0;
/*
* Main loop over the format string.
* Increment and check for end of string is made here.
*/
for(; *fmt != '\0'; fmt++) {
c = *fmt;
while (c != '%') {
if (c == '\0')
return (count);
(*fun)(c, farg);
c = *(++fmt);
count++;
}
/*
* We reached a '%' sign.
*/
buf[0] = '\0';
fa.buf = fa.bufp = buf;
fa.minusflag = 0;
fa.flags = 0;
fa.fldwidth = 0;
fa.signific = -1;
fa.lzero = 0;
fa.fillc = ' ';
fa.prefixlen = 0;
sfmt = fmt;
unsflag = FALSE;
newflag:
switch (*(++fmt)) {
case '+':
fa.flags |= PLUSFLG;
goto newflag;
case '-':
fa.minusflag++;
goto newflag;
case ' ':
/*
* If the space and the + flag are present,
* the space flag will be ignored.
*/
fa.flags |= SPACEFLG;
goto newflag;
case '#':
fa.flags |= HASHFLG;
goto newflag;
case '0':
/*
* '0' is a flag.
*/
fa.fillc = '0';
goto newflag;
}
if (*fmt == '*') {
fmt++;
fa.fldwidth = va_arg(args, int);
/*
* A negative fieldwith is a minus flag with a
* positive fieldwidth.
*/
if (fa.fldwidth < 0) {
fa.fldwidth = -fa.fldwidth;
/* fa.minusflag ^= 1;*/
fa.minusflag = 1;
}
} else while (c = *fmt, is_dig(c)) {
fa.fldwidth *= 10;
fa.fldwidth += c - '0';
fmt++;
}
if (*fmt == '.') {
fmt++;
fa.signific = 0;
if (*fmt == '*') {
fmt++;
fa.signific = va_arg(args, int);
if (fa.signific < 0)
fa.signific = 0;
} else while (c = *fmt, is_dig(c)) {
fa.signific *= 10;
fa.signific += c - '0';
fmt++;
}
}
if (strchr("UCSIL", *fmt)) {
/*
* Enhancements to K&R and ANSI:
*
* got a type specifyer
*/
if (*fmt == 'U') {
fmt++;
unsflag = TRUE;
}
if (!strchr("CSILZODX", *fmt)) {
/*
* Got only 'U'nsigned specifyer,
* use default type and mode.
*/
type = 'I';
mode = 'D';
fmt--;
} else if (!strchr("CSIL", *fmt)) {
/*
* no type, use default
*/
type = 'I';
mode = *fmt;
} else {
/*
* got CSIL
*/
type = *fmt++;
if (!strchr("ZODX", mode = *fmt)) {
fmt--;
mode = 'D';/* default mode */
}
}
} else switch(*fmt) {
case 'h':
type = 'S'; /* convert to type */
goto getmode;
case 'l':
type = 'L'; /* convert to type */
getmode:
if (!strchr("udioxX", *(++fmt))) {
fmt--;
mode = 'D';
} else {
mode = *fmt;
if (mode != 'x')
mode = to_cap(mode);
if (mode == 'U')
unsflag = TRUE;
else if (mode == 'I') /*XXX */
mode = 'D';
}
break;
case 'x':
mode = 'x';
goto havemode;
case 'u':
unsflag = TRUE;
case 'o': case 'O':
case 'd': case 'D':
case 'i': case 'I':
case 'p':
case 'X':
case 'z': case 'Z':
mode = to_cap(*fmt);
havemode:
type = cap_ty(*fmt);
if (mode == 'I') /*XXX kann entfallen*/
mode = 'D'; /*wenn besseres uflg*/
break;
case '%':
count += prc('%', &fa);
continue;
case ' ':
count += prbuf("", &fa);
continue;
case 'c':
c = va_arg(args, int);
count += prc(c, &fa);
continue;
case 's':
str = va_arg(args, char *);
count += prstring(str, &fa);
continue;
case 'b':
str = va_arg(args, char *);
fa.signific = va_arg(args, int);
count += prstring(str, &fa);
continue;
#ifndef NO_FLOATINGPOINT
case 'e':
if (fa.signific == -1)
fa.signific = 6;
dval = va_arg(args, double);
ftoes(buf, dval, 0, fa.signific);
count += prbuf(buf, &fa);
continue;
case 'f':
if (fa.signific == -1)
fa.signific = 6;
dval = va_arg(args, double);
ftofs(buf, dval, 0, fa.signific);
count += prbuf(buf, &fa);
continue;
case 'g':
if (fa.signific == -1)
fa.signific = 6;
if (fa.signific == 0)
fa.signific = 1;
dval = va_arg(args, double);
gcvt(dval, fa.signific, buf);
count += prbuf(buf, &fa);
continue;
#else
# ifdef USE_FLOATINGARGS
case 'e':
case 'f':
case 'g':
dval = va_arg(args, double);
continue;
# endif
#endif
case 'r': /* recursive printf */
case 'R': /* recursive printf */
rfmt = va_arg(args, char *);
/*
* I don't know any portable way to get an arbitrary
* C object from a var arg list so I use a
* system-specific routine __va_arg_list() that knows
* if 'va_list' is an array. You will not be able to
* assign the value of __va_arg_list() but it works
* to be used as an argument of a function.
* It is a requirement for recursive printf to be able
* to use this function argument. If your system
* defines va_list to be an array you need to know this
* via autoconf or another mechanism.
* It would be nice to have something like
* __va_arg_list() in stdarg.h
*/
count += format(fun, farg, rfmt, __va_arg_list(args));
continue;
case 'n':
{
int *ip = va_arg(args, int *);
*ip = count;
}
continue;
default: /* Unknown '%' format */
sfmt++; /* Dont't print '%' */
count += fmt - sfmt;
while (sfmt < fmt)
(*fun)(*(sfmt++), farg);
if (*fmt == '\0') {
fmt--;
continue;
} else {
(*fun)(*fmt, farg);
count++;
continue;
}
}
/*
* print numbers:
* first prepare type 'C'har, 'S'hort, 'I'nt, or 'L'ong
*/
switch(type) {
case 'C':
c = va_arg(args, int);
val = c; /* extend sign here */
if (unsflag || mode != 'D')
#ifdef DO_MASK
val &= CHARMASK;
#else
val = (unsigned char)val;
#endif
break;
case 'S':
sh = va_arg(args, int);
val = sh; /* extend sign here */
if (unsflag || mode != 'D')
#ifdef DO_MASK
val &= SHORTMASK;
#else
val = (unsigned short)val;
#endif
break;
case 'I':
default:
i = va_arg(args, int);
val = i; /* extend sign here */
if (unsflag || mode != 'D')
#ifdef DO_MASK
val &= INTMASK;
#else
val = (unsigned int)val;
#endif
break;
case 'P':
case 'L':
val = va_arg(args, long);
break;
}
/*
* Final print out, take care of mode:
* mode is one of: 'O'ctal, 'D'ecimal, or he'X'
* oder 'Z'weierdarstellung.
*/
fa.bufp = &buf[sizeof(buf)-1];
*--fa.bufp = '\0';
if (val == 0 && mode != 'D') {
printzero:
/*
* Printing '0' with fieldwidth 0 results in no chars.
*/
fa.lzero = -1;
if (fa.signific >= 0)
fa.fillc = ' ';
count += prstring("0", &fa);
continue;
} else switch(mode) {
case 'D':
if (!unsflag && val < 0) {
fa.prefix = "-";
fa.prefixlen = 1;
val = -val;
} else if (fa.flags & PLUSFLG) {
fa.prefix = "+";
fa.prefixlen = 1;
} else if (fa.flags & SPACEFLG) {
fa.prefix = " ";
fa.prefixlen = 1;
}
if (val == 0)
goto printzero;
case 'U':
/* output a long unsigned decimal number */
prdnum(val, &fa);
break;
case 'O':
/* output a long octal number */
if (fa.flags & HASHFLG) {
fa.prefix = "0";
fa.prefixlen = 1;
}
pronum(val & 07, &fa);
if ((res = (val>>3) & rshiftmask(long, 3)) != 0)
pronum(res, &fa);
break;
case 'p':
case 'x':
/* output a hex long */
if (fa.flags & HASHFLG) {
fa.prefix = "0x";
fa.prefixlen = 2;
}
prxnum(val & 0xF, &fa);
if ((res = (val>>4) & rshiftmask(long, 4)) != 0)
prxnum(res, &fa);
break;
case 'P':
case 'X':
/* output a hex long */
if (fa.flags & HASHFLG) {
fa.prefix = "0X";
fa.prefixlen = 2;
}
prXnum(val & 0xF, &fa);
if ((res = (val>>4) & rshiftmask(long, 4)) != 0)
prXnum(res, &fa);
break;
case 'Z':
/* output a binary long */
prnum(val & 0x1, 2, &fa);
if ((res = (val>>1) & rshiftmask(long, 1)) != 0)
prnum(res, 2, &fa);
}
fa.lzero = -1;
/*
* If a precision (fielwidth) is specified
* on diouXx conversions, the '0' flag is ignored.
*/
if (fa.signific >= 0)
fa.fillc = ' ';
count += prbuf(fa.bufp, &fa);
}
return (count);
}
/*
* Routines to print (not negative) numbers in an arbitrary base
*/
LOCAL unsigned char dtab[] = "0123456789abcdef";
LOCAL unsigned char udtab[] = "0123456789ABCDEF";
LOCAL void prnum(val, base, fa)
register Ulong val;
register unsigned base;
f_args *fa;
{
register char *p = fa->bufp;
do {
*--p = dtab[modlbys(val, base)];
val = divlbys(val, base);
} while (val > 0);
fa->bufp = p;
}
LOCAL void prdnum(val, fa)
register Ulong val;
f_args *fa;
{
register char *p = fa->bufp;
do {
*--p = dtab[modlbys(val, (unsigned)10)];
val = divlbys(val, (unsigned)10);
} while (val > 0);
fa->bufp = p;
}
/*
* We may need to use division here too (PDP-11, non two's complement ...)
*/
LOCAL void pronum(val, fa)
register Ulong val;
f_args *fa;
{
register char *p = fa->bufp;
do {
*--p = dtab[val & 7];
val >>= 3;
} while (val > 0);
fa->bufp = p;
}
LOCAL void prxnum(val, fa)
register Ulong val;
f_args *fa;
{
register char *p = fa->bufp;
do {
*--p = dtab[val & 15];
val >>= 4;
} while (val > 0);
fa->bufp = p;
}
LOCAL void prXnum(val, fa)
register Ulong val;
f_args *fa;
{
register char *p = fa->bufp;
do {
*--p = udtab[val & 15];
val >>= 4;
} while (val > 0);
fa->bufp = p;
}
/*
* Final buffer print out routine.
*/
LOCAL int prbuf(s, fa)
register const char *s;
f_args *fa;
{
register int diff;
register int rfillc;
register long arg = fa->farg;
register void (*fun) __PR((char, long)) = fa->outf;
register int count;
register int lzero = 0;
count = strlen(s);
if (fa->lzero < 0 && count < fa->signific)
lzero = fa->signific - count;
diff = fa->fldwidth - lzero - count - fa->prefixlen;
count += lzero;
if (diff > 0)
count += diff;
if (fa->prefixlen && fa->fillc != ' ') {
while (*fa->prefix != '\0')
(*fun)(*fa->prefix++, arg);
}
if (!fa->minusflag) {
rfillc = fa->fillc;
while (--diff >= 0)
(*fun)(rfillc, arg);
}
if (fa->prefixlen && fa->fillc == ' ') {
while (*fa->prefix != '\0')
(*fun)(*fa->prefix++, arg);
}
if (lzero > 0) {
rfillc = '0';
while (--lzero >= 0)
(*fun)(rfillc, arg);
}
while (*s != '\0')
(*fun)(*s++, arg);
if (fa->minusflag) {
rfillc = ' ';
while (--diff >= 0)
(*fun)(rfillc, arg);
}
return (count);
}
/*
* Print out one char, allowing prc('\0')
* Similar to prbuf()
*/
#ifdef PROTOTYPES
LOCAL int prc(char c, f_args *fa)
#else
LOCAL int prc(c, fa)
char c;
f_args *fa;
#endif
{
register int diff;
register int rfillc;
register long arg = fa->farg;
register void (*fun) __PR((char, long)) = fa->outf;
register int count;
count = 1;
diff = fa->fldwidth - 1;
if (diff > 0)
count += diff;
if (!fa->minusflag) {
rfillc = fa->fillc;
while (--diff >= 0)
(*fun)(rfillc, arg);
}
(*fun)(c, arg);
if (fa->minusflag) {
rfillc = ' ';
while (--diff >= 0)
(*fun)(rfillc, arg);
}
return (count);
}
/*
* String output routine.
* If fa->signific is >= 0, it uses only fa->signific chars.
* If fa->signific is 0, print no characters.
*/
LOCAL int prstring(s, fa)
register const char *s;
f_args *fa;
{
register char *bp;
register int signific;
if (s == NULL)
return (prbuf("(NULL POINTER)", fa));
if (fa->signific < 0)
return (prbuf(s, fa));
bp = fa->buf;
signific = fa->signific;
while (--signific >= 0 && *s != '\0')
*bp++ = *s++;
*bp = '\0';
return (prbuf(fa->buf, fa));
}
|