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