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