1 /* $OpenBSD: subr_prf.c,v 1.99 2019/07/20 23:06:51 mpi Exp $ */ 2 /* $NetBSD: subr_prf.c,v 1.45 1997/10/24 18:14:25 chuck Exp $ */ 3 4 /*- 5 * Copyright (c) 1986, 1988, 1991, 1993 6 * The Regents of the University of California. All rights reserved. 7 * (c) UNIX System Laboratories, Inc. 8 * All or some portions of this file are derived from material licensed 9 * to the University of California by American Telephone and Telegraph 10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 11 * the permission of UNIX System Laboratories, Inc. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94 38 */ 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/conf.h> 43 #include <sys/reboot.h> 44 #include <sys/msgbuf.h> 45 #include <sys/proc.h> 46 #include <sys/ioctl.h> 47 #include <sys/vnode.h> 48 #include <sys/tty.h> 49 #include <sys/tprintf.h> 50 #include <sys/syslog.h> 51 #include <sys/malloc.h> 52 #include <sys/pool.h> 53 #include <sys/mutex.h> 54 55 #include <dev/cons.h> 56 57 /* 58 * note that stdarg.h and the ansi style va_start macro is used for both 59 * ansi and traditional c compilers. 60 */ 61 #include <sys/stdarg.h> 62 63 #ifdef DDB 64 #include <ddb/db_output.h> /* db_printf, db_putchar prototypes */ 65 #include <ddb/db_var.h> /* db_log, db_radix */ 66 #endif 67 68 69 /* 70 * defines 71 */ 72 73 /* flags for kprintf */ 74 #define TOCONS 0x01 /* to the console */ 75 #define TOTTY 0x02 /* to the process' tty */ 76 #define TOLOG 0x04 /* to the kernel message buffer */ 77 #define TOBUFONLY 0x08 /* to the buffer (only) [for snprintf] */ 78 #define TODDB 0x10 /* to ddb console */ 79 #define TOCOUNT 0x20 /* act like [v]snprintf */ 80 81 /* max size buffer kprintf needs to print quad_t [size in base 8 + \0] */ 82 #define KPRINTF_BUFSIZE (sizeof(quad_t) * NBBY / 3 + 2) 83 84 85 /* 86 * local prototypes 87 */ 88 89 int kprintf(const char *, int, void *, char *, va_list); 90 void kputchar(int, int, struct tty *); 91 92 struct mutex kprintf_mutex = 93 MUTEX_INITIALIZER_FLAGS(IPL_HIGH, "kprintf", MTX_NOWITNESS); 94 95 /* 96 * globals 97 */ 98 99 extern int log_open; /* subr_log: is /dev/klog open? */ 100 const char *panicstr; /* arg to first call to panic (used as a flag 101 to indicate that panic has already been called). */ 102 const char *faultstr; /* page fault string */ 103 #ifdef DDB 104 /* 105 * Enter ddb on panic. 106 */ 107 int db_panic = 1; 108 109 /* 110 * db_console controls if we can be able to enter ddb by a special key 111 * combination (machine dependent). 112 * If DDB_SAFE_CONSOLE is defined in the kernel configuration it allows 113 * to break into console during boot. It's _really_ useful when debugging 114 * some things in the kernel that can cause init(8) to crash. 115 */ 116 #ifdef DDB_SAFE_CONSOLE 117 int db_console = 1; 118 #else 119 int db_console = 0; 120 #endif 121 #endif 122 123 /* 124 * panic on spl assertion failure? 125 */ 126 #ifdef SPLASSERT_WATCH 127 int splassert_ctl = 3; 128 #else 129 int splassert_ctl = 1; 130 #endif 131 132 /* 133 * v_putc: routine to putc on virtual console 134 * 135 * the v_putc pointer can be used to redirect the console cnputc elsewhere 136 * [e.g. to a "virtual console"]. 137 */ 138 139 void (*v_putc)(int) = cnputc; /* start with cnputc (normal cons) */ 140 141 142 /* 143 * functions 144 */ 145 146 /* 147 * Partial support (the failure case) of the assertion facility 148 * commonly found in userland. 149 */ 150 void 151 __assert(const char *t, const char *f, int l, const char *e) 152 { 153 154 panic(__KASSERTSTR, t, e, f, l); 155 } 156 157 /* 158 * tablefull: warn that a system table is full 159 */ 160 161 void 162 tablefull(const char *tab) 163 { 164 log(LOG_ERR, "%s: table is full\n", tab); 165 } 166 167 /* 168 * panic: handle an unresolvable fatal error 169 * 170 * prints "panic: <message>" and reboots. if called twice (i.e. recursive 171 * call) we avoid trying to sync the disk and just reboot (to avoid 172 * recursive panics). 173 */ 174 175 void 176 panic(const char *fmt, ...) 177 { 178 static char panicbuf[512]; 179 int bootopt; 180 va_list ap; 181 182 /* do not trigger assertions, we know that we are inconsistent */ 183 splassert_ctl = 0; 184 185 bootopt = RB_AUTOBOOT | RB_DUMP; 186 va_start(ap, fmt); 187 if (panicstr) 188 bootopt |= RB_NOSYNC; 189 else { 190 vsnprintf(panicbuf, sizeof panicbuf, fmt, ap); 191 panicstr = panicbuf; 192 } 193 va_end(ap); 194 195 printf("panic: "); 196 va_start(ap, fmt); 197 vprintf(fmt, ap); 198 printf("\n"); 199 va_end(ap); 200 201 #ifdef DDB 202 if (db_panic) 203 db_enter(); 204 else 205 db_stack_dump(); 206 #endif 207 reboot(bootopt); 208 /* NOTREACHED */ 209 } 210 211 /* 212 * We print only the function name. The file name is usually very long and 213 * would eat tons of space in the kernel. 214 */ 215 void 216 splassert_fail(int wantipl, int haveipl, const char *func) 217 { 218 if (panicstr || db_active) 219 return; 220 221 printf("splassert: %s: want %d have %d\n", func, wantipl, haveipl); 222 switch (splassert_ctl) { 223 case 1: 224 break; 225 case 2: 226 #ifdef DDB 227 db_stack_dump(); 228 #endif 229 break; 230 case 3: 231 #ifdef DDB 232 db_stack_dump(); 233 db_enter(); 234 #endif 235 break; 236 default: 237 panic("spl assertion failure in %s", func); 238 } 239 } 240 241 /* 242 * kernel logging functions: log, logpri, addlog 243 */ 244 245 /* 246 * log: write to the log buffer 247 * 248 * => will not sleep [so safe to call from interrupt] 249 * => will log to console if /dev/klog isn't open 250 */ 251 252 void 253 log(int level, const char *fmt, ...) 254 { 255 int s; 256 va_list ap; 257 258 s = splhigh(); 259 logpri(level); /* log the level first */ 260 va_start(ap, fmt); 261 kprintf(fmt, TOLOG, NULL, NULL, ap); 262 va_end(ap); 263 splx(s); 264 if (!log_open) { 265 va_start(ap, fmt); 266 mtx_enter(&kprintf_mutex); 267 kprintf(fmt, TOCONS, NULL, NULL, ap); 268 mtx_leave(&kprintf_mutex); 269 va_end(ap); 270 } 271 logwakeup(); /* wake up anyone waiting for log msgs */ 272 } 273 274 /* 275 * logpri: log the priority level to the klog 276 */ 277 278 void 279 logpri(int level) 280 { 281 char *p; 282 char snbuf[KPRINTF_BUFSIZE]; 283 284 kputchar('<', TOLOG, NULL); 285 snprintf(snbuf, sizeof snbuf, "%d", level); 286 for (p = snbuf ; *p ; p++) 287 kputchar(*p, TOLOG, NULL); 288 kputchar('>', TOLOG, NULL); 289 } 290 291 /* 292 * addlog: add info to previous log message 293 */ 294 295 int 296 addlog(const char *fmt, ...) 297 { 298 int s; 299 va_list ap; 300 301 s = splhigh(); 302 va_start(ap, fmt); 303 kprintf(fmt, TOLOG, NULL, NULL, ap); 304 va_end(ap); 305 splx(s); 306 if (!log_open) { 307 va_start(ap, fmt); 308 mtx_enter(&kprintf_mutex); 309 kprintf(fmt, TOCONS, NULL, NULL, ap); 310 mtx_leave(&kprintf_mutex); 311 va_end(ap); 312 } 313 logwakeup(); 314 return(0); 315 } 316 317 318 /* 319 * kputchar: print a single character on console or user terminal. 320 * 321 * => if console, then the last MSGBUFS chars are saved in msgbuf 322 * for inspection later (e.g. dmesg/syslog) 323 */ 324 void 325 kputchar(int c, int flags, struct tty *tp) 326 { 327 extern int msgbufmapped; 328 329 if (panicstr) 330 constty = NULL; 331 332 if ((flags & TOCONS) && tp == NULL && constty != NULL && !db_active) { 333 tp = constty; 334 flags |= TOTTY; 335 } 336 if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 && 337 (flags & TOCONS) && tp == constty) 338 constty = NULL; 339 if ((flags & TOLOG) && 340 c != '\0' && c != '\r' && c != 0177 && msgbufmapped) 341 msgbuf_putchar(msgbufp, c); 342 if ((flags & TOCONS) && (constty == NULL || db_active) && c != '\0') 343 (*v_putc)(c); 344 #ifdef DDB 345 if (flags & TODDB) 346 db_putchar(c); 347 #endif 348 } 349 350 351 /* 352 * uprintf: print to the controlling tty of the current process 353 * 354 * => we may block if the tty queue is full 355 * => no message is printed if the queue doesn't clear in a reasonable 356 * time 357 */ 358 359 void 360 uprintf(const char *fmt, ...) 361 { 362 struct process *pr = curproc->p_p; 363 va_list ap; 364 365 if (pr->ps_flags & PS_CONTROLT && pr->ps_session->s_ttyvp) { 366 va_start(ap, fmt); 367 kprintf(fmt, TOTTY, pr->ps_session->s_ttyp, NULL, ap); 368 va_end(ap); 369 } 370 } 371 372 #if defined(NFSSERVER) || defined(NFSCLIENT) 373 374 /* 375 * tprintf functions: used to send messages to a specific process 376 * 377 * usage: 378 * get a tpr_t handle on a process "p" by using "tprintf_open(p)" 379 * use the handle when calling "tprintf" 380 * when done, do a "tprintf_close" to drop the handle 381 */ 382 383 /* 384 * tprintf_open: get a tprintf handle on a process "p" 385 * XXX change s/proc/process 386 * 387 * => returns NULL if process can't be printed to 388 */ 389 390 tpr_t 391 tprintf_open(struct proc *p) 392 { 393 struct process *pr = p->p_p; 394 395 if (pr->ps_flags & PS_CONTROLT && pr->ps_session->s_ttyvp) { 396 SESSHOLD(pr->ps_session); 397 return ((tpr_t)pr->ps_session); 398 } 399 return ((tpr_t) NULL); 400 } 401 402 /* 403 * tprintf_close: dispose of a tprintf handle obtained with tprintf_open 404 */ 405 406 void 407 tprintf_close(tpr_t sess) 408 { 409 410 if (sess) 411 SESSRELE((struct session *) sess); 412 } 413 414 /* 415 * tprintf: given tprintf handle to a process [obtained with tprintf_open], 416 * send a message to the controlling tty for that process. 417 * 418 * => also sends message to /dev/klog 419 */ 420 void 421 tprintf(tpr_t tpr, const char *fmt, ...) 422 { 423 struct session *sess = (struct session *)tpr; 424 struct tty *tp = NULL; 425 int flags = TOLOG; 426 va_list ap; 427 428 logpri(LOG_INFO); 429 if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) { 430 flags |= TOTTY; 431 tp = sess->s_ttyp; 432 } 433 va_start(ap, fmt); 434 kprintf(fmt, flags, tp, NULL, ap); 435 va_end(ap); 436 logwakeup(); 437 } 438 439 #endif /* NFSSERVER || NFSCLIENT */ 440 441 442 /* 443 * ttyprintf: send a message to a specific tty 444 * 445 * => should be used only by tty driver or anything that knows the 446 * underlying tty will not be revoked(2)'d away. [otherwise, 447 * use tprintf] 448 */ 449 void 450 ttyprintf(struct tty *tp, const char *fmt, ...) 451 { 452 va_list ap; 453 454 va_start(ap, fmt); 455 kprintf(fmt, TOTTY, tp, NULL, ap); 456 va_end(ap); 457 } 458 459 #ifdef DDB 460 461 /* 462 * db_printf: printf for DDB (via db_putchar) 463 */ 464 465 int 466 db_printf(const char *fmt, ...) 467 { 468 va_list ap; 469 int retval; 470 471 va_start(ap, fmt); 472 retval = db_vprintf(fmt, ap); 473 va_end(ap); 474 return(retval); 475 } 476 477 int 478 db_vprintf(const char *fmt, va_list ap) 479 { 480 int flags; 481 482 flags = TODDB; 483 if (db_log) 484 flags |= TOLOG; 485 return (kprintf(fmt, flags, NULL, NULL, ap)); 486 } 487 #endif /* DDB */ 488 489 490 /* 491 * normal kernel printf functions: printf, vprintf, snprintf 492 */ 493 494 /* 495 * printf: print a message to the console and the log 496 */ 497 int 498 printf(const char *fmt, ...) 499 { 500 va_list ap; 501 int retval; 502 503 504 va_start(ap, fmt); 505 mtx_enter(&kprintf_mutex); 506 retval = kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap); 507 mtx_leave(&kprintf_mutex); 508 va_end(ap); 509 if (!panicstr) 510 logwakeup(); 511 512 513 return(retval); 514 } 515 516 /* 517 * vprintf: print a message to the console and the log [already have a 518 * va_list] 519 */ 520 521 int 522 vprintf(const char *fmt, va_list ap) 523 { 524 int retval; 525 526 mtx_enter(&kprintf_mutex); 527 retval = kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap); 528 mtx_leave(&kprintf_mutex); 529 if (!panicstr) 530 logwakeup(); 531 532 533 return (retval); 534 } 535 536 /* 537 * snprintf: print a message to a buffer 538 */ 539 int 540 snprintf(char *buf, size_t size, const char *fmt, ...) 541 { 542 int retval; 543 va_list ap; 544 char *p; 545 546 p = buf + size - 1; 547 if (size < 1) 548 p = buf; 549 va_start(ap, fmt); 550 retval = kprintf(fmt, TOBUFONLY | TOCOUNT, &p, buf, ap); 551 va_end(ap); 552 if (size > 0) 553 *(p) = 0; /* null terminate */ 554 return(retval); 555 } 556 557 /* 558 * vsnprintf: print a message to a buffer [already have va_alist] 559 */ 560 int 561 vsnprintf(char *buf, size_t size, const char *fmt, va_list ap) 562 { 563 int retval; 564 char *p; 565 566 p = buf + size - 1; 567 if (size < 1) 568 p = buf; 569 retval = kprintf(fmt, TOBUFONLY | TOCOUNT, &p, buf, ap); 570 if (size > 0) 571 *(p) = 0; /* null terminate */ 572 return(retval); 573 } 574 575 /* 576 * kprintf: scaled down version of printf(3). 577 * 578 * this version based on vfprintf() from libc which was derived from 579 * software contributed to Berkeley by Chris Torek. 580 * 581 * The additional format %b is supported to decode error registers. 582 * Its usage is: 583 * 584 * printf("reg=%b\n", regval, "<base><arg>*"); 585 * 586 * where <base> is the output base expressed as a control character, e.g. 587 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters, 588 * the first of which gives the bit number to be inspected (origin 1), and 589 * the next characters (up to a control character, i.e. a character <= 32), 590 * give the name of the register. Thus: 591 * 592 * kprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n"); 593 * 594 * would produce output: 595 * 596 * reg=3<BITTWO,BITONE> 597 * 598 * To support larger integers (> 32 bits), %b formatting will also accept 599 * control characters in the region 0x80 - 0xff. 0x80 refers to bit 0, 600 * 0x81 refers to bit 1, and so on. The equivalent string to the above is: 601 * 602 * kprintf("reg=%b\n", 3, "\10\201BITTWO\200BITONE\n"); 603 * 604 * and would produce the same output. 605 * 606 * Like the rest of printf, %b can be prefixed to handle various size 607 * modifiers, eg. %b is for "int", %lb is for "long", and %llb supports 608 * "long long". 609 * 610 * This code is large and complicated... 611 */ 612 613 /* 614 * macros for converting digits to letters and vice versa 615 */ 616 #define to_digit(c) ((c) - '0') 617 #define is_digit(c) ((unsigned)to_digit(c) <= 9) 618 #define to_char(n) ((n) + '0') 619 620 /* 621 * flags used during conversion. 622 */ 623 #define ALT 0x001 /* alternate form */ 624 #define HEXPREFIX 0x002 /* add 0x or 0X prefix */ 625 #define LADJUST 0x004 /* left adjustment */ 626 #define LONGDBL 0x008 /* long double; unimplemented */ 627 #define LONGINT 0x010 /* long integer */ 628 #define QUADINT 0x020 /* quad integer */ 629 #define SHORTINT 0x040 /* short integer */ 630 #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */ 631 #define FPT 0x100 /* Floating point number */ 632 #define SIZEINT 0x200 /* (signed) size_t */ 633 634 /* 635 * To extend shorts properly, we need both signed and unsigned 636 * argument extraction methods. 637 */ 638 #define SARG() \ 639 (flags&QUADINT ? va_arg(ap, quad_t) : \ 640 flags&LONGINT ? va_arg(ap, long) : \ 641 flags&SIZEINT ? va_arg(ap, ssize_t) : \ 642 flags&SHORTINT ? (long)(short)va_arg(ap, int) : \ 643 (long)va_arg(ap, int)) 644 #define UARG() \ 645 (flags&QUADINT ? va_arg(ap, u_quad_t) : \ 646 flags&LONGINT ? va_arg(ap, u_long) : \ 647 flags&SIZEINT ? va_arg(ap, size_t) : \ 648 flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \ 649 (u_long)va_arg(ap, u_int)) 650 651 #define KPRINTF_PUTCHAR(C) do { \ 652 int chr = (C); \ 653 ret += 1; \ 654 if (oflags & TOBUFONLY) { \ 655 if ((vp != NULL) && (sbuf == tailp)) { \ 656 if (!(oflags & TOCOUNT)) \ 657 goto overflow; \ 658 } else \ 659 *sbuf++ = chr; \ 660 } else { \ 661 kputchar(chr, oflags, (struct tty *)vp); \ 662 } \ 663 } while(0) 664 665 int 666 kprintf(const char *fmt0, int oflags, void *vp, char *sbuf, va_list ap) 667 { 668 char *fmt; /* format string */ 669 int ch; /* character from fmt */ 670 int n; /* handy integer (short term usage) */ 671 char *cp = NULL; /* handy char pointer (short term usage) */ 672 int flags; /* flags as above */ 673 int ret; /* return value accumulator */ 674 int width; /* width from format (%8d), or 0 */ 675 int prec; /* precision from format (%.3d), or -1 */ 676 char sign; /* sign prefix (' ', '+', '-', or \0) */ 677 678 u_quad_t _uquad; /* integer arguments %[diouxX] */ 679 enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */ 680 int dprec; /* a copy of prec if [diouxX], 0 otherwise */ 681 int realsz; /* field size expanded by dprec */ 682 int size = 0; /* size of converted field or string */ 683 char *xdigs = NULL; /* digits for [xX] conversion */ 684 char buf[KPRINTF_BUFSIZE]; /* space for %c, %[diouxX] */ 685 char *tailp = NULL; /* tail pointer for snprintf */ 686 687 if (oflags & TOCONS) 688 MUTEX_ASSERT_LOCKED(&kprintf_mutex); 689 690 if ((oflags & TOBUFONLY) && (vp != NULL)) 691 tailp = *(char **)vp; 692 693 fmt = (char *)fmt0; 694 ret = 0; 695 696 /* 697 * Scan the format for conversions (`%' character). 698 */ 699 for (;;) { 700 while (*fmt != '%' && *fmt) { 701 KPRINTF_PUTCHAR(*fmt++); 702 } 703 if (*fmt == 0) 704 goto done; 705 706 fmt++; /* skip over '%' */ 707 708 flags = 0; 709 dprec = 0; 710 width = 0; 711 prec = -1; 712 sign = '\0'; 713 714 rflag: ch = *fmt++; 715 reswitch: switch (ch) { 716 /* XXX: non-standard '%b' format */ 717 case 'b': { 718 char *b, *z; 719 int tmp; 720 _uquad = UARG(); 721 b = va_arg(ap, char *); 722 if (*b == 8) 723 snprintf(buf, sizeof buf, "%llo", _uquad); 724 else if (*b == 10) 725 snprintf(buf, sizeof buf, "%lld", _uquad); 726 else if (*b == 16) 727 snprintf(buf, sizeof buf, "%llx", _uquad); 728 else 729 break; 730 b++; 731 732 z = buf; 733 while (*z) { 734 KPRINTF_PUTCHAR(*z++); 735 } 736 737 if (_uquad) { 738 tmp = 0; 739 while ((n = *b++) != 0) { 740 if (n & 0x80) 741 n &= 0x7f; 742 else if (n <= ' ') 743 n = n - 1; 744 if (_uquad & (1LL << n)) { 745 KPRINTF_PUTCHAR(tmp ? ',':'<'); 746 while (*b > ' ' && 747 (*b & 0x80) == 0) { 748 KPRINTF_PUTCHAR(*b); 749 b++; 750 } 751 tmp = 1; 752 } else { 753 while (*b > ' ' && 754 (*b & 0x80) == 0) 755 b++; 756 } 757 } 758 if (tmp) { 759 KPRINTF_PUTCHAR('>'); 760 } 761 } 762 continue; /* no output */ 763 } 764 765 case ' ': 766 /* 767 * ``If the space and + flags both appear, the space 768 * flag will be ignored.'' 769 * -- ANSI X3J11 770 */ 771 if (!sign) 772 sign = ' '; 773 goto rflag; 774 case '#': 775 flags |= ALT; 776 goto rflag; 777 case '*': 778 /* 779 * ``A negative field width argument is taken as a 780 * - flag followed by a positive field width.'' 781 * -- ANSI X3J11 782 * They don't exclude field widths read from args. 783 */ 784 if ((width = va_arg(ap, int)) >= 0) 785 goto rflag; 786 width = -width; 787 /* FALLTHROUGH */ 788 case '-': 789 flags |= LADJUST; 790 goto rflag; 791 case '+': 792 sign = '+'; 793 goto rflag; 794 case '.': 795 if ((ch = *fmt++) == '*') { 796 n = va_arg(ap, int); 797 prec = n < 0 ? -1 : n; 798 goto rflag; 799 } 800 n = 0; 801 while (is_digit(ch)) { 802 n = 10 * n + to_digit(ch); 803 ch = *fmt++; 804 } 805 prec = n < 0 ? -1 : n; 806 goto reswitch; 807 case '0': 808 /* 809 * ``Note that 0 is taken as a flag, not as the 810 * beginning of a field width.'' 811 * -- ANSI X3J11 812 */ 813 flags |= ZEROPAD; 814 goto rflag; 815 case '1': case '2': case '3': case '4': 816 case '5': case '6': case '7': case '8': case '9': 817 n = 0; 818 do { 819 n = 10 * n + to_digit(ch); 820 ch = *fmt++; 821 } while (is_digit(ch)); 822 width = n; 823 goto reswitch; 824 case 'h': 825 flags |= SHORTINT; 826 goto rflag; 827 case 'l': 828 if (*fmt == 'l') { 829 fmt++; 830 flags |= QUADINT; 831 } else { 832 flags |= LONGINT; 833 } 834 goto rflag; 835 case 'q': 836 flags |= QUADINT; 837 goto rflag; 838 case 'z': 839 flags |= SIZEINT; 840 goto rflag; 841 case 'c': 842 *(cp = buf) = va_arg(ap, int); 843 size = 1; 844 sign = '\0'; 845 break; 846 case 't': 847 /* ptrdiff_t */ 848 /* FALLTHROUGH */ 849 case 'D': 850 flags |= LONGINT; 851 /*FALLTHROUGH*/ 852 case 'd': 853 case 'i': 854 _uquad = SARG(); 855 if ((quad_t)_uquad < 0) { 856 _uquad = -_uquad; 857 sign = '-'; 858 } 859 base = DEC; 860 goto number; 861 case 'n': 862 /* %n is unsupported in the kernel; just skip it */ 863 if (flags & QUADINT) 864 (void)va_arg(ap, quad_t *); 865 else if (flags & LONGINT) 866 (void)va_arg(ap, long *); 867 else if (flags & SHORTINT) 868 (void)va_arg(ap, short *); 869 else if (flags & SIZEINT) 870 (void)va_arg(ap, ssize_t *); 871 else 872 (void)va_arg(ap, int *); 873 continue; /* no output */ 874 case 'O': 875 flags |= LONGINT; 876 /*FALLTHROUGH*/ 877 case 'o': 878 _uquad = UARG(); 879 base = OCT; 880 goto nosign; 881 case 'p': 882 /* 883 * ``The argument shall be a pointer to void. The 884 * value of the pointer is converted to a sequence 885 * of printable characters, in an implementation- 886 * defined manner.'' 887 * -- ANSI X3J11 888 */ 889 _uquad = (u_long)va_arg(ap, void *); 890 base = HEX; 891 xdigs = "0123456789abcdef"; 892 flags |= HEXPREFIX; 893 ch = 'x'; 894 goto nosign; 895 case 's': 896 if ((cp = va_arg(ap, char *)) == NULL) 897 cp = "(null)"; 898 if (prec >= 0) { 899 /* 900 * can't use strlen; can only look for the 901 * NUL in the first `prec' characters, and 902 * strlen() will go further. 903 */ 904 char *p = memchr(cp, 0, prec); 905 906 if (p != NULL) { 907 size = p - cp; 908 if (size > prec) 909 size = prec; 910 } else 911 size = prec; 912 } else 913 size = strlen(cp); 914 sign = '\0'; 915 break; 916 case 'U': 917 flags |= LONGINT; 918 /*FALLTHROUGH*/ 919 case 'u': 920 _uquad = UARG(); 921 base = DEC; 922 goto nosign; 923 case 'X': 924 xdigs = "0123456789ABCDEF"; 925 goto hex; 926 case 'x': 927 xdigs = "0123456789abcdef"; 928 hex: _uquad = UARG(); 929 base = HEX; 930 /* leading 0x/X only if non-zero */ 931 if (flags & ALT && _uquad != 0) 932 flags |= HEXPREFIX; 933 934 /* unsigned conversions */ 935 nosign: sign = '\0'; 936 /* 937 * ``... diouXx conversions ... if a precision is 938 * specified, the 0 flag will be ignored.'' 939 * -- ANSI X3J11 940 */ 941 number: if ((dprec = prec) >= 0) 942 flags &= ~ZEROPAD; 943 944 /* 945 * ``The result of converting a zero value with an 946 * explicit precision of zero is no characters.'' 947 * -- ANSI X3J11 948 */ 949 cp = buf + KPRINTF_BUFSIZE; 950 if (_uquad != 0 || prec != 0) { 951 /* 952 * Unsigned mod is hard, and unsigned mod 953 * by a constant is easier than that by 954 * a variable; hence this switch. 955 */ 956 switch (base) { 957 case OCT: 958 do { 959 *--cp = to_char(_uquad & 7); 960 _uquad >>= 3; 961 } while (_uquad); 962 /* handle octal leading 0 */ 963 if (flags & ALT && *cp != '0') 964 *--cp = '0'; 965 break; 966 967 case DEC: 968 /* many numbers are 1 digit */ 969 while (_uquad >= 10) { 970 *--cp = to_char(_uquad % 10); 971 _uquad /= 10; 972 } 973 *--cp = to_char(_uquad); 974 break; 975 976 case HEX: 977 do { 978 *--cp = xdigs[_uquad & 15]; 979 _uquad >>= 4; 980 } while (_uquad); 981 break; 982 983 default: 984 cp = "bug in kprintf: bad base"; 985 size = strlen(cp); 986 goto skipsize; 987 } 988 } 989 size = buf + KPRINTF_BUFSIZE - cp; 990 skipsize: 991 break; 992 default: /* "%?" prints ?, unless ? is NUL */ 993 if (ch == '\0') 994 goto done; 995 /* pretend it was %c with argument ch */ 996 cp = buf; 997 *cp = ch; 998 size = 1; 999 sign = '\0'; 1000 break; 1001 } 1002 1003 /* 1004 * All reasonable formats wind up here. At this point, `cp' 1005 * points to a string which (if not flags&LADJUST) should be 1006 * padded out to `width' places. If flags&ZEROPAD, it should 1007 * first be prefixed by any sign or other prefix; otherwise, 1008 * it should be blank padded before the prefix is emitted. 1009 * After any left-hand padding and prefixing, emit zeroes 1010 * required by a decimal [diouxX] precision, then print the 1011 * string proper, then emit zeroes required by any leftover 1012 * floating precision; finally, if LADJUST, pad with blanks. 1013 * 1014 * Compute actual size, so we know how much to pad. 1015 * size excludes decimal prec; realsz includes it. 1016 */ 1017 realsz = dprec > size ? dprec : size; 1018 if (sign) 1019 realsz++; 1020 else if (flags & HEXPREFIX) 1021 realsz+= 2; 1022 1023 /* right-adjusting blank padding */ 1024 if ((flags & (LADJUST|ZEROPAD)) == 0) { 1025 n = width - realsz; 1026 while (n-- > 0) 1027 KPRINTF_PUTCHAR(' '); 1028 } 1029 1030 /* prefix */ 1031 if (sign) { 1032 KPRINTF_PUTCHAR(sign); 1033 } else if (flags & HEXPREFIX) { 1034 KPRINTF_PUTCHAR('0'); 1035 KPRINTF_PUTCHAR(ch); 1036 } 1037 1038 /* right-adjusting zero padding */ 1039 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) { 1040 n = width - realsz; 1041 while (n-- > 0) 1042 KPRINTF_PUTCHAR('0'); 1043 } 1044 1045 /* leading zeroes from decimal precision */ 1046 n = dprec - size; 1047 while (n-- > 0) 1048 KPRINTF_PUTCHAR('0'); 1049 1050 /* the string or number proper */ 1051 while (size--) 1052 KPRINTF_PUTCHAR(*cp++); 1053 /* left-adjusting padding (always blank) */ 1054 if (flags & LADJUST) { 1055 n = width - realsz; 1056 while (n-- > 0) 1057 KPRINTF_PUTCHAR(' '); 1058 } 1059 } 1060 1061 done: 1062 if ((oflags & TOBUFONLY) && (vp != NULL)) 1063 *(char **)vp = sbuf; 1064 overflow: 1065 return (ret); 1066 /* NOTREACHED */ 1067 } 1068 1069 #if __GNUC_PREREQ__(2,96) 1070 /* 1071 * XXX - these functions shouldn't be in the kernel, but gcc 3.X feels like 1072 * translating some printf calls to puts and since it doesn't seem 1073 * possible to just turn off parts of those optimizations (some of 1074 * them are really useful), we have to provide a dummy puts and putchar 1075 * that are wrappers around printf. 1076 */ 1077 int puts(const char *); 1078 int putchar(int c); 1079 1080 int 1081 puts(const char *str) 1082 { 1083 printf("%s\n", str); 1084 1085 return (0); 1086 } 1087 1088 int 1089 putchar(int c) 1090 { 1091 printf("%c", c); 1092 1093 return (c); 1094 } 1095 1096 1097 #endif 1098