1 /* $OpenBSD: subr_prf.c,v 1.91 2017/04/30 16:45:46 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/file.h> 49 #include <sys/tty.h> 50 #include <sys/tprintf.h> 51 #include <sys/syslog.h> 52 #include <sys/malloc.h> 53 #include <sys/pool.h> 54 #include <sys/mutex.h> 55 56 #include <dev/cons.h> 57 58 /* 59 * note that stdarg.h and the ansi style va_start macro is used for both 60 * ansi and traditional c compilers. 61 */ 62 #include <sys/stdarg.h> 63 64 #ifdef DDB 65 #include <ddb/db_output.h> /* db_printf, db_putchar prototypes */ 66 #include <ddb/db_var.h> /* db_log, db_radix */ 67 #endif 68 69 70 /* 71 * defines 72 */ 73 74 /* flags for kprintf */ 75 #define TOCONS 0x01 /* to the console */ 76 #define TOTTY 0x02 /* to the process' tty */ 77 #define TOLOG 0x04 /* to the kernel message buffer */ 78 #define TOBUFONLY 0x08 /* to the buffer (only) [for snprintf] */ 79 #define TODDB 0x10 /* to ddb console */ 80 #define TOCOUNT 0x20 /* act like [v]snprintf */ 81 82 /* max size buffer kprintf needs to print quad_t [size in base 8 + \0] */ 83 #define KPRINTF_BUFSIZE (sizeof(quad_t) * NBBY / 3 + 2) 84 85 86 /* 87 * local prototypes 88 */ 89 90 int kprintf(const char *, int, void *, char *, va_list); 91 void kputchar(int, int, struct tty *); 92 93 struct mutex kprintf_mutex = 94 MUTEX_INITIALIZER_FLAGS(IPL_HIGH, "kprintf", MTX_NOWITNESS); 95 96 /* 97 * globals 98 */ 99 100 extern int log_open; /* subr_log: is /dev/klog open? */ 101 const char *panicstr; /* arg to first call to panic (used as a flag 102 to indicate that panic has already been called). */ 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 122 /* 123 * flag to indicate if we are currently in ddb (on some processor) 124 */ 125 int db_is_active; 126 #endif 127 128 /* 129 * panic on spl assertion failure? 130 */ 131 int splassert_ctl = 1; 132 133 /* 134 * v_putc: routine to putc on virtual console 135 * 136 * the v_putc pointer can be used to redirect the console cnputc elsewhere 137 * [e.g. to a "virtual console"]. 138 */ 139 140 void (*v_putc)(int) = cnputc; /* start with cnputc (normal cons) */ 141 142 143 /* 144 * functions 145 */ 146 147 /* 148 * Partial support (the failure case) of the assertion facility 149 * commonly found in userland. 150 */ 151 void 152 __assert(const char *t, const char *f, int l, const char *e) 153 { 154 155 panic(__KASSERTSTR, t, e, f, l); 156 } 157 158 /* 159 * tablefull: warn that a system table is full 160 */ 161 162 void 163 tablefull(const char *tab) 164 { 165 log(LOG_ERR, "%s: table is full\n", tab); 166 } 167 168 /* 169 * panic: handle an unresolvable fatal error 170 * 171 * prints "panic: <message>" and reboots. if called twice (i.e. recursive 172 * call) we avoid trying to sync the disk and just reboot (to avoid 173 * recursive panics). 174 */ 175 176 void 177 panic(const char *fmt, ...) 178 { 179 static char panicbuf[512]; 180 int bootopt; 181 va_list ap; 182 183 /* do not trigger assertions, we know that we are inconsistent */ 184 splassert_ctl = 0; 185 186 bootopt = RB_AUTOBOOT | RB_DUMP; 187 va_start(ap, fmt); 188 if (panicstr) 189 bootopt |= RB_NOSYNC; 190 else { 191 vsnprintf(panicbuf, sizeof panicbuf, fmt, ap); 192 panicstr = panicbuf; 193 } 194 va_end(ap); 195 196 printf("panic: "); 197 va_start(ap, fmt); 198 vprintf(fmt, ap); 199 printf("\n"); 200 va_end(ap); 201 202 #ifdef DDB 203 if (db_panic) 204 db_enter(); 205 else 206 db_stack_dump(); 207 #endif 208 reboot(bootopt); 209 /* NOTREACHED */ 210 } 211 212 /* 213 * We print only the function name. The file name is usually very long and 214 * would eat tons of space in the kernel. 215 */ 216 void 217 splassert_fail(int wantipl, int haveipl, const char *func) 218 { 219 220 printf("splassert: %s: want %d have %d\n", func, wantipl, haveipl); 221 switch (splassert_ctl) { 222 case 1: 223 break; 224 case 2: 225 #ifdef DDB 226 db_stack_dump(); 227 #endif 228 break; 229 case 3: 230 #ifdef DDB 231 db_stack_dump(); 232 db_enter(); 233 #endif 234 break; 235 default: 236 panic("spl assertion failure in %s", func); 237 } 238 } 239 240 /* 241 * kernel logging functions: log, logpri, addlog 242 */ 243 244 /* 245 * log: write to the log buffer 246 * 247 * => will not sleep [so safe to call from interrupt] 248 * => will log to console if /dev/klog isn't open 249 */ 250 251 void 252 log(int level, const char *fmt, ...) 253 { 254 int s; 255 va_list ap; 256 257 s = splhigh(); 258 logpri(level); /* log the level first */ 259 va_start(ap, fmt); 260 kprintf(fmt, TOLOG, NULL, NULL, ap); 261 va_end(ap); 262 splx(s); 263 if (!log_open) { 264 va_start(ap, fmt); 265 kprintf(fmt, TOCONS, NULL, NULL, ap); 266 va_end(ap); 267 } 268 logwakeup(); /* wake up anyone waiting for log msgs */ 269 } 270 271 /* 272 * logpri: log the priority level to the klog 273 */ 274 275 void 276 logpri(int level) 277 { 278 char *p; 279 char snbuf[KPRINTF_BUFSIZE]; 280 281 kputchar('<', TOLOG, NULL); 282 snprintf(snbuf, sizeof snbuf, "%d", level); 283 for (p = snbuf ; *p ; p++) 284 kputchar(*p, TOLOG, NULL); 285 kputchar('>', TOLOG, NULL); 286 } 287 288 /* 289 * addlog: add info to previous log message 290 */ 291 292 int 293 addlog(const char *fmt, ...) 294 { 295 int s; 296 va_list ap; 297 298 s = splhigh(); 299 va_start(ap, fmt); 300 kprintf(fmt, TOLOG, NULL, NULL, ap); 301 va_end(ap); 302 splx(s); 303 if (!log_open) { 304 va_start(ap, fmt); 305 kprintf(fmt, TOCONS, NULL, NULL, ap); 306 va_end(ap); 307 } 308 logwakeup(); 309 return(0); 310 } 311 312 313 /* 314 * kputchar: print a single character on console or user terminal. 315 * 316 * => if console, then the last MSGBUFS chars are saved in msgbuf 317 * for inspection later (e.g. dmesg/syslog) 318 */ 319 void 320 kputchar(int c, int flags, struct tty *tp) 321 { 322 extern int msgbufmapped; 323 int ddb_active = 0; 324 325 #ifdef DDB 326 ddb_active = db_is_active; 327 #endif 328 329 if (panicstr) 330 constty = NULL; 331 332 if ((flags & TOCONS) && tp == NULL && constty && !ddb_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 || ddb_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 mtx_enter(&kprintf_mutex); 504 505 va_start(ap, fmt); 506 retval = kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap); 507 va_end(ap); 508 if (!panicstr) 509 logwakeup(); 510 511 mtx_leave(&kprintf_mutex); 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 528 retval = kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap); 529 if (!panicstr) 530 logwakeup(); 531 532 mtx_leave(&kprintf_mutex); 533 534 return (retval); 535 } 536 537 /* 538 * snprintf: print a message to a buffer 539 */ 540 int 541 snprintf(char *buf, size_t size, const char *fmt, ...) 542 { 543 int retval; 544 va_list ap; 545 char *p; 546 547 p = buf + size - 1; 548 if (size < 1) 549 p = buf; 550 va_start(ap, fmt); 551 retval = kprintf(fmt, TOBUFONLY | TOCOUNT, &p, buf, ap); 552 va_end(ap); 553 if (size > 0) 554 *(p) = 0; /* null terminate */ 555 return(retval); 556 } 557 558 /* 559 * vsnprintf: print a message to a buffer [already have va_alist] 560 */ 561 int 562 vsnprintf(char *buf, size_t size, const char *fmt, va_list ap) 563 { 564 int retval; 565 char *p; 566 567 p = buf + size - 1; 568 if (size < 1) 569 p = buf; 570 retval = kprintf(fmt, TOBUFONLY | TOCOUNT, &p, buf, ap); 571 if (size > 0) 572 *(p) = 0; /* null terminate */ 573 return(retval); 574 } 575 576 /* 577 * kprintf: scaled down version of printf(3). 578 * 579 * this version based on vfprintf() from libc which was derived from 580 * software contributed to Berkeley by Chris Torek. 581 * 582 * The additional format %b is supported to decode error registers. 583 * Its usage is: 584 * 585 * printf("reg=%b\n", regval, "<base><arg>*"); 586 * 587 * where <base> is the output base expressed as a control character, e.g. 588 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters, 589 * the first of which gives the bit number to be inspected (origin 1), and 590 * the next characters (up to a control character, i.e. a character <= 32), 591 * give the name of the register. Thus: 592 * 593 * kprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n"); 594 * 595 * would produce output: 596 * 597 * reg=3<BITTWO,BITONE> 598 * 599 * To support larger integers (> 32 bits), %b formatting will also accept 600 * control characters in the region 0x80 - 0xff. 0x80 refers to bit 0, 601 * 0x81 refers to bit 1, and so on. The equivalent string to the above is: 602 * 603 * kprintf("reg=%b\n", 3, "\10\201BITTWO\200BITONE\n"); 604 * 605 * and would produce the same output. 606 * 607 * Like the rest of printf, %b can be prefixed to handle various size 608 * modifiers, eg. %b is for "int", %lb is for "long", and %llb supports 609 * "long long". 610 * 611 * This code is large and complicated... 612 */ 613 614 /* 615 * macros for converting digits to letters and vice versa 616 */ 617 #define to_digit(c) ((c) - '0') 618 #define is_digit(c) ((unsigned)to_digit(c) <= 9) 619 #define to_char(n) ((n) + '0') 620 621 /* 622 * flags used during conversion. 623 */ 624 #define ALT 0x001 /* alternate form */ 625 #define HEXPREFIX 0x002 /* add 0x or 0X prefix */ 626 #define LADJUST 0x004 /* left adjustment */ 627 #define LONGDBL 0x008 /* long double; unimplemented */ 628 #define LONGINT 0x010 /* long integer */ 629 #define QUADINT 0x020 /* quad integer */ 630 #define SHORTINT 0x040 /* short integer */ 631 #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */ 632 #define FPT 0x100 /* Floating point number */ 633 #define SIZEINT 0x200 /* (signed) size_t */ 634 635 /* 636 * To extend shorts properly, we need both signed and unsigned 637 * argument extraction methods. 638 */ 639 #define SARG() \ 640 (flags&QUADINT ? va_arg(ap, quad_t) : \ 641 flags&LONGINT ? va_arg(ap, long) : \ 642 flags&SIZEINT ? va_arg(ap, ssize_t) : \ 643 flags&SHORTINT ? (long)(short)va_arg(ap, int) : \ 644 (long)va_arg(ap, int)) 645 #define UARG() \ 646 (flags&QUADINT ? va_arg(ap, u_quad_t) : \ 647 flags&LONGINT ? va_arg(ap, u_long) : \ 648 flags&SIZEINT ? va_arg(ap, size_t) : \ 649 flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \ 650 (u_long)va_arg(ap, u_int)) 651 652 #define KPRINTF_PUTCHAR(C) do { \ 653 int chr = (C); \ 654 ret += 1; \ 655 if (oflags & TOBUFONLY) { \ 656 if ((vp != NULL) && (sbuf == tailp)) { \ 657 if (!(oflags & TOCOUNT)) \ 658 goto overflow; \ 659 } else \ 660 *sbuf++ = chr; \ 661 } else { \ 662 kputchar(chr, oflags, (struct tty *)vp); \ 663 } \ 664 } while(0) 665 666 int 667 kprintf(const char *fmt0, int oflags, void *vp, char *sbuf, va_list ap) 668 { 669 char *fmt; /* format string */ 670 int ch; /* character from fmt */ 671 int n; /* handy integer (short term usage) */ 672 char *cp = NULL; /* handy char pointer (short term usage) */ 673 int flags; /* flags as above */ 674 int ret; /* return value accumulator */ 675 int width; /* width from format (%8d), or 0 */ 676 int prec; /* precision from format (%.3d), or -1 */ 677 char sign; /* sign prefix (' ', '+', '-', or \0) */ 678 679 u_quad_t _uquad; /* integer arguments %[diouxX] */ 680 enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */ 681 int dprec; /* a copy of prec if [diouxX], 0 otherwise */ 682 int realsz; /* field size expanded by dprec */ 683 int size = 0; /* size of converted field or string */ 684 char *xdigs = NULL; /* digits for [xX] conversion */ 685 char buf[KPRINTF_BUFSIZE]; /* space for %c, %[diouxX] */ 686 char *tailp = NULL; /* tail pointer for snprintf */ 687 688 if ((oflags & TOBUFONLY) && (vp != NULL)) 689 tailp = *(char **)vp; 690 691 fmt = (char *)fmt0; 692 ret = 0; 693 694 /* 695 * Scan the format for conversions (`%' character). 696 */ 697 for (;;) { 698 while (*fmt != '%' && *fmt) { 699 KPRINTF_PUTCHAR(*fmt++); 700 } 701 if (*fmt == 0) 702 goto done; 703 704 fmt++; /* skip over '%' */ 705 706 flags = 0; 707 dprec = 0; 708 width = 0; 709 prec = -1; 710 sign = '\0'; 711 712 rflag: ch = *fmt++; 713 reswitch: switch (ch) { 714 /* XXX: non-standard '%b' format */ 715 case 'b': { 716 char *b, *z; 717 int tmp; 718 _uquad = UARG(); 719 b = va_arg(ap, char *); 720 if (*b == 8) 721 snprintf(buf, sizeof buf, "%llo", _uquad); 722 else if (*b == 10) 723 snprintf(buf, sizeof buf, "%lld", _uquad); 724 else if (*b == 16) 725 snprintf(buf, sizeof buf, "%llx", _uquad); 726 else 727 break; 728 b++; 729 730 z = buf; 731 while (*z) { 732 KPRINTF_PUTCHAR(*z++); 733 } 734 735 if (_uquad) { 736 tmp = 0; 737 while ((n = *b++) != 0) { 738 if (n & 0x80) 739 n &= 0x7f; 740 else if (n <= ' ') 741 n = n - 1; 742 if (_uquad & (1LL << n)) { 743 KPRINTF_PUTCHAR(tmp ? ',':'<'); 744 while (*b > ' ' && 745 (*b & 0x80) == 0) { 746 KPRINTF_PUTCHAR(*b); 747 b++; 748 } 749 tmp = 1; 750 } else { 751 while (*b > ' ' && 752 (*b & 0x80) == 0) 753 b++; 754 } 755 } 756 if (tmp) { 757 KPRINTF_PUTCHAR('>'); 758 } 759 } 760 continue; /* no output */ 761 } 762 763 case ' ': 764 /* 765 * ``If the space and + flags both appear, the space 766 * flag will be ignored.'' 767 * -- ANSI X3J11 768 */ 769 if (!sign) 770 sign = ' '; 771 goto rflag; 772 case '#': 773 flags |= ALT; 774 goto rflag; 775 case '*': 776 /* 777 * ``A negative field width argument is taken as a 778 * - flag followed by a positive field width.'' 779 * -- ANSI X3J11 780 * They don't exclude field widths read from args. 781 */ 782 if ((width = va_arg(ap, int)) >= 0) 783 goto rflag; 784 width = -width; 785 /* FALLTHROUGH */ 786 case '-': 787 flags |= LADJUST; 788 goto rflag; 789 case '+': 790 sign = '+'; 791 goto rflag; 792 case '.': 793 if ((ch = *fmt++) == '*') { 794 n = va_arg(ap, int); 795 prec = n < 0 ? -1 : n; 796 goto rflag; 797 } 798 n = 0; 799 while (is_digit(ch)) { 800 n = 10 * n + to_digit(ch); 801 ch = *fmt++; 802 } 803 prec = n < 0 ? -1 : n; 804 goto reswitch; 805 case '0': 806 /* 807 * ``Note that 0 is taken as a flag, not as the 808 * beginning of a field width.'' 809 * -- ANSI X3J11 810 */ 811 flags |= ZEROPAD; 812 goto rflag; 813 case '1': case '2': case '3': case '4': 814 case '5': case '6': case '7': case '8': case '9': 815 n = 0; 816 do { 817 n = 10 * n + to_digit(ch); 818 ch = *fmt++; 819 } while (is_digit(ch)); 820 width = n; 821 goto reswitch; 822 case 'h': 823 flags |= SHORTINT; 824 goto rflag; 825 case 'l': 826 if (*fmt == 'l') { 827 fmt++; 828 flags |= QUADINT; 829 } else { 830 flags |= LONGINT; 831 } 832 goto rflag; 833 case 'q': 834 flags |= QUADINT; 835 goto rflag; 836 case 'z': 837 flags |= SIZEINT; 838 goto rflag; 839 case 'c': 840 *(cp = buf) = va_arg(ap, int); 841 size = 1; 842 sign = '\0'; 843 break; 844 case 't': 845 /* ptrdiff_t */ 846 /* FALLTHROUGH */ 847 case 'D': 848 flags |= LONGINT; 849 /*FALLTHROUGH*/ 850 case 'd': 851 case 'i': 852 _uquad = SARG(); 853 if ((quad_t)_uquad < 0) { 854 _uquad = -_uquad; 855 sign = '-'; 856 } 857 base = DEC; 858 goto number; 859 case 'n': 860 /* %n is unsupported in the kernel; just skip it */ 861 if (flags & QUADINT) 862 (void)va_arg(ap, quad_t *); 863 else if (flags & LONGINT) 864 (void)va_arg(ap, long *); 865 else if (flags & SHORTINT) 866 (void)va_arg(ap, short *); 867 else if (flags & SIZEINT) 868 (void)va_arg(ap, ssize_t *); 869 else 870 (void)va_arg(ap, int *); 871 continue; /* no output */ 872 case 'O': 873 flags |= LONGINT; 874 /*FALLTHROUGH*/ 875 case 'o': 876 _uquad = UARG(); 877 base = OCT; 878 goto nosign; 879 case 'p': 880 /* 881 * ``The argument shall be a pointer to void. The 882 * value of the pointer is converted to a sequence 883 * of printable characters, in an implementation- 884 * defined manner.'' 885 * -- ANSI X3J11 886 */ 887 _uquad = (u_long)va_arg(ap, void *); 888 base = HEX; 889 xdigs = "0123456789abcdef"; 890 flags |= HEXPREFIX; 891 ch = 'x'; 892 goto nosign; 893 case 's': 894 if ((cp = va_arg(ap, char *)) == NULL) 895 cp = "(null)"; 896 if (prec >= 0) { 897 /* 898 * can't use strlen; can only look for the 899 * NUL in the first `prec' characters, and 900 * strlen() will go further. 901 */ 902 char *p = memchr(cp, 0, prec); 903 904 if (p != NULL) { 905 size = p - cp; 906 if (size > prec) 907 size = prec; 908 } else 909 size = prec; 910 } else 911 size = strlen(cp); 912 sign = '\0'; 913 break; 914 case 'U': 915 flags |= LONGINT; 916 /*FALLTHROUGH*/ 917 case 'u': 918 _uquad = UARG(); 919 base = DEC; 920 goto nosign; 921 case 'X': 922 xdigs = "0123456789ABCDEF"; 923 goto hex; 924 case 'x': 925 xdigs = "0123456789abcdef"; 926 hex: _uquad = UARG(); 927 base = HEX; 928 /* leading 0x/X only if non-zero */ 929 if (flags & ALT && _uquad != 0) 930 flags |= HEXPREFIX; 931 932 /* unsigned conversions */ 933 nosign: sign = '\0'; 934 /* 935 * ``... diouXx conversions ... if a precision is 936 * specified, the 0 flag will be ignored.'' 937 * -- ANSI X3J11 938 */ 939 number: if ((dprec = prec) >= 0) 940 flags &= ~ZEROPAD; 941 942 /* 943 * ``The result of converting a zero value with an 944 * explicit precision of zero is no characters.'' 945 * -- ANSI X3J11 946 */ 947 cp = buf + KPRINTF_BUFSIZE; 948 if (_uquad != 0 || prec != 0) { 949 /* 950 * Unsigned mod is hard, and unsigned mod 951 * by a constant is easier than that by 952 * a variable; hence this switch. 953 */ 954 switch (base) { 955 case OCT: 956 do { 957 *--cp = to_char(_uquad & 7); 958 _uquad >>= 3; 959 } while (_uquad); 960 /* handle octal leading 0 */ 961 if (flags & ALT && *cp != '0') 962 *--cp = '0'; 963 break; 964 965 case DEC: 966 /* many numbers are 1 digit */ 967 while (_uquad >= 10) { 968 *--cp = to_char(_uquad % 10); 969 _uquad /= 10; 970 } 971 *--cp = to_char(_uquad); 972 break; 973 974 case HEX: 975 do { 976 *--cp = xdigs[_uquad & 15]; 977 _uquad >>= 4; 978 } while (_uquad); 979 break; 980 981 default: 982 cp = "bug in kprintf: bad base"; 983 size = strlen(cp); 984 goto skipsize; 985 } 986 } 987 size = buf + KPRINTF_BUFSIZE - cp; 988 skipsize: 989 break; 990 default: /* "%?" prints ?, unless ? is NUL */ 991 if (ch == '\0') 992 goto done; 993 /* pretend it was %c with argument ch */ 994 cp = buf; 995 *cp = ch; 996 size = 1; 997 sign = '\0'; 998 break; 999 } 1000 1001 /* 1002 * All reasonable formats wind up here. At this point, `cp' 1003 * points to a string which (if not flags&LADJUST) should be 1004 * padded out to `width' places. If flags&ZEROPAD, it should 1005 * first be prefixed by any sign or other prefix; otherwise, 1006 * it should be blank padded before the prefix is emitted. 1007 * After any left-hand padding and prefixing, emit zeroes 1008 * required by a decimal [diouxX] precision, then print the 1009 * string proper, then emit zeroes required by any leftover 1010 * floating precision; finally, if LADJUST, pad with blanks. 1011 * 1012 * Compute actual size, so we know how much to pad. 1013 * size excludes decimal prec; realsz includes it. 1014 */ 1015 realsz = dprec > size ? dprec : size; 1016 if (sign) 1017 realsz++; 1018 else if (flags & HEXPREFIX) 1019 realsz+= 2; 1020 1021 /* right-adjusting blank padding */ 1022 if ((flags & (LADJUST|ZEROPAD)) == 0) { 1023 n = width - realsz; 1024 while (n-- > 0) 1025 KPRINTF_PUTCHAR(' '); 1026 } 1027 1028 /* prefix */ 1029 if (sign) { 1030 KPRINTF_PUTCHAR(sign); 1031 } else if (flags & HEXPREFIX) { 1032 KPRINTF_PUTCHAR('0'); 1033 KPRINTF_PUTCHAR(ch); 1034 } 1035 1036 /* right-adjusting zero padding */ 1037 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) { 1038 n = width - realsz; 1039 while (n-- > 0) 1040 KPRINTF_PUTCHAR('0'); 1041 } 1042 1043 /* leading zeroes from decimal precision */ 1044 n = dprec - size; 1045 while (n-- > 0) 1046 KPRINTF_PUTCHAR('0'); 1047 1048 /* the string or number proper */ 1049 while (size--) 1050 KPRINTF_PUTCHAR(*cp++); 1051 /* left-adjusting padding (always blank) */ 1052 if (flags & LADJUST) { 1053 n = width - realsz; 1054 while (n-- > 0) 1055 KPRINTF_PUTCHAR(' '); 1056 } 1057 } 1058 1059 done: 1060 if ((oflags & TOBUFONLY) && (vp != NULL)) 1061 *(char **)vp = sbuf; 1062 overflow: 1063 return (ret); 1064 /* NOTREACHED */ 1065 } 1066 1067 #if __GNUC_PREREQ__(2,96) 1068 /* 1069 * XXX - these functions shouldn't be in the kernel, but gcc 3.X feels like 1070 * translating some printf calls to puts and since it doesn't seem 1071 * possible to just turn off parts of those optimizations (some of 1072 * them are really useful), we have to provide a dummy puts and putchar 1073 * that are wrappers around printf. 1074 */ 1075 int puts(const char *); 1076 int putchar(int c); 1077 1078 int 1079 puts(const char *str) 1080 { 1081 printf("%s\n", str); 1082 1083 return (0); 1084 } 1085 1086 int 1087 putchar(int c) 1088 { 1089 printf("%c", c); 1090 1091 return (c); 1092 } 1093 1094 1095 #endif 1096