1 /* $OpenBSD: subr_prf.c,v 1.94 2018/03/20 15:45:32 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 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 if (panicstr || db_active) 220 return; 221 222 printf("splassert: %s: want %d have %d\n", func, wantipl, haveipl); 223 switch (splassert_ctl) { 224 case 1: 225 break; 226 case 2: 227 #ifdef DDB 228 db_stack_dump(); 229 #endif 230 break; 231 case 3: 232 #ifdef DDB 233 db_stack_dump(); 234 db_enter(); 235 #endif 236 break; 237 default: 238 panic("spl assertion failure in %s", func); 239 } 240 } 241 242 /* 243 * kernel logging functions: log, logpri, addlog 244 */ 245 246 /* 247 * log: write to the log buffer 248 * 249 * => will not sleep [so safe to call from interrupt] 250 * => will log to console if /dev/klog isn't open 251 */ 252 253 void 254 log(int level, const char *fmt, ...) 255 { 256 int s; 257 va_list ap; 258 259 s = splhigh(); 260 logpri(level); /* log the level first */ 261 va_start(ap, fmt); 262 kprintf(fmt, TOLOG, NULL, NULL, ap); 263 va_end(ap); 264 splx(s); 265 if (!log_open) { 266 va_start(ap, fmt); 267 kprintf(fmt, TOCONS, NULL, NULL, ap); 268 va_end(ap); 269 } 270 logwakeup(); /* wake up anyone waiting for log msgs */ 271 } 272 273 /* 274 * logpri: log the priority level to the klog 275 */ 276 277 void 278 logpri(int level) 279 { 280 char *p; 281 char snbuf[KPRINTF_BUFSIZE]; 282 283 kputchar('<', TOLOG, NULL); 284 snprintf(snbuf, sizeof snbuf, "%d", level); 285 for (p = snbuf ; *p ; p++) 286 kputchar(*p, TOLOG, NULL); 287 kputchar('>', TOLOG, NULL); 288 } 289 290 /* 291 * addlog: add info to previous log message 292 */ 293 294 int 295 addlog(const char *fmt, ...) 296 { 297 int s; 298 va_list ap; 299 300 s = splhigh(); 301 va_start(ap, fmt); 302 kprintf(fmt, TOLOG, NULL, NULL, ap); 303 va_end(ap); 304 splx(s); 305 if (!log_open) { 306 va_start(ap, fmt); 307 kprintf(fmt, TOCONS, NULL, NULL, ap); 308 va_end(ap); 309 } 310 logwakeup(); 311 return(0); 312 } 313 314 315 /* 316 * kputchar: print a single character on console or user terminal. 317 * 318 * => if console, then the last MSGBUFS chars are saved in msgbuf 319 * for inspection later (e.g. dmesg/syslog) 320 */ 321 void 322 kputchar(int c, int flags, struct tty *tp) 323 { 324 extern int msgbufmapped; 325 int ddb_active = 0; 326 327 #ifdef DDB 328 ddb_active = db_is_active; 329 #endif 330 331 if (panicstr) 332 constty = NULL; 333 334 if ((flags & TOCONS) && tp == NULL && constty && !ddb_active) { 335 tp = constty; 336 flags |= TOTTY; 337 } 338 if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 && 339 (flags & TOCONS) && tp == constty) 340 constty = NULL; 341 if ((flags & TOLOG) && 342 c != '\0' && c != '\r' && c != 0177 && msgbufmapped) 343 msgbuf_putchar(msgbufp, c); 344 if ((flags & TOCONS) && (constty == NULL || ddb_active) && c != '\0') 345 (*v_putc)(c); 346 #ifdef DDB 347 if (flags & TODDB) 348 db_putchar(c); 349 #endif 350 } 351 352 353 /* 354 * uprintf: print to the controlling tty of the current process 355 * 356 * => we may block if the tty queue is full 357 * => no message is printed if the queue doesn't clear in a reasonable 358 * time 359 */ 360 361 void 362 uprintf(const char *fmt, ...) 363 { 364 struct process *pr = curproc->p_p; 365 va_list ap; 366 367 if (pr->ps_flags & PS_CONTROLT && pr->ps_session->s_ttyvp) { 368 va_start(ap, fmt); 369 kprintf(fmt, TOTTY, pr->ps_session->s_ttyp, NULL, ap); 370 va_end(ap); 371 } 372 } 373 374 #if defined(NFSSERVER) || defined(NFSCLIENT) 375 376 /* 377 * tprintf functions: used to send messages to a specific process 378 * 379 * usage: 380 * get a tpr_t handle on a process "p" by using "tprintf_open(p)" 381 * use the handle when calling "tprintf" 382 * when done, do a "tprintf_close" to drop the handle 383 */ 384 385 /* 386 * tprintf_open: get a tprintf handle on a process "p" 387 * XXX change s/proc/process 388 * 389 * => returns NULL if process can't be printed to 390 */ 391 392 tpr_t 393 tprintf_open(struct proc *p) 394 { 395 struct process *pr = p->p_p; 396 397 if (pr->ps_flags & PS_CONTROLT && pr->ps_session->s_ttyvp) { 398 SESSHOLD(pr->ps_session); 399 return ((tpr_t)pr->ps_session); 400 } 401 return ((tpr_t) NULL); 402 } 403 404 /* 405 * tprintf_close: dispose of a tprintf handle obtained with tprintf_open 406 */ 407 408 void 409 tprintf_close(tpr_t sess) 410 { 411 412 if (sess) 413 SESSRELE((struct session *) sess); 414 } 415 416 /* 417 * tprintf: given tprintf handle to a process [obtained with tprintf_open], 418 * send a message to the controlling tty for that process. 419 * 420 * => also sends message to /dev/klog 421 */ 422 void 423 tprintf(tpr_t tpr, const char *fmt, ...) 424 { 425 struct session *sess = (struct session *)tpr; 426 struct tty *tp = NULL; 427 int flags = TOLOG; 428 va_list ap; 429 430 logpri(LOG_INFO); 431 if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) { 432 flags |= TOTTY; 433 tp = sess->s_ttyp; 434 } 435 va_start(ap, fmt); 436 kprintf(fmt, flags, tp, NULL, ap); 437 va_end(ap); 438 logwakeup(); 439 } 440 441 #endif /* NFSSERVER || NFSCLIENT */ 442 443 444 /* 445 * ttyprintf: send a message to a specific tty 446 * 447 * => should be used only by tty driver or anything that knows the 448 * underlying tty will not be revoked(2)'d away. [otherwise, 449 * use tprintf] 450 */ 451 void 452 ttyprintf(struct tty *tp, const char *fmt, ...) 453 { 454 va_list ap; 455 456 va_start(ap, fmt); 457 kprintf(fmt, TOTTY, tp, NULL, ap); 458 va_end(ap); 459 } 460 461 #ifdef DDB 462 463 /* 464 * db_printf: printf for DDB (via db_putchar) 465 */ 466 467 int 468 db_printf(const char *fmt, ...) 469 { 470 va_list ap; 471 int retval; 472 473 va_start(ap, fmt); 474 retval = db_vprintf(fmt, ap); 475 va_end(ap); 476 return(retval); 477 } 478 479 int 480 db_vprintf(const char *fmt, va_list ap) 481 { 482 int flags; 483 484 flags = TODDB; 485 if (db_log) 486 flags |= TOLOG; 487 return (kprintf(fmt, flags, NULL, NULL, ap)); 488 } 489 #endif /* DDB */ 490 491 492 /* 493 * normal kernel printf functions: printf, vprintf, snprintf 494 */ 495 496 /* 497 * printf: print a message to the console and the log 498 */ 499 int 500 printf(const char *fmt, ...) 501 { 502 va_list ap; 503 int retval; 504 505 mtx_enter(&kprintf_mutex); 506 507 va_start(ap, fmt); 508 retval = kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap); 509 va_end(ap); 510 if (!panicstr) 511 logwakeup(); 512 513 mtx_leave(&kprintf_mutex); 514 515 return(retval); 516 } 517 518 /* 519 * vprintf: print a message to the console and the log [already have a 520 * va_list] 521 */ 522 523 int 524 vprintf(const char *fmt, va_list ap) 525 { 526 int retval; 527 528 mtx_enter(&kprintf_mutex); 529 530 retval = kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap); 531 if (!panicstr) 532 logwakeup(); 533 534 mtx_leave(&kprintf_mutex); 535 536 return (retval); 537 } 538 539 /* 540 * snprintf: print a message to a buffer 541 */ 542 int 543 snprintf(char *buf, size_t size, const char *fmt, ...) 544 { 545 int retval; 546 va_list ap; 547 char *p; 548 549 p = buf + size - 1; 550 if (size < 1) 551 p = buf; 552 va_start(ap, fmt); 553 retval = kprintf(fmt, TOBUFONLY | TOCOUNT, &p, buf, ap); 554 va_end(ap); 555 if (size > 0) 556 *(p) = 0; /* null terminate */ 557 return(retval); 558 } 559 560 /* 561 * vsnprintf: print a message to a buffer [already have va_alist] 562 */ 563 int 564 vsnprintf(char *buf, size_t size, const char *fmt, va_list ap) 565 { 566 int retval; 567 char *p; 568 569 p = buf + size - 1; 570 if (size < 1) 571 p = buf; 572 retval = kprintf(fmt, TOBUFONLY | TOCOUNT, &p, buf, ap); 573 if (size > 0) 574 *(p) = 0; /* null terminate */ 575 return(retval); 576 } 577 578 /* 579 * kprintf: scaled down version of printf(3). 580 * 581 * this version based on vfprintf() from libc which was derived from 582 * software contributed to Berkeley by Chris Torek. 583 * 584 * The additional format %b is supported to decode error registers. 585 * Its usage is: 586 * 587 * printf("reg=%b\n", regval, "<base><arg>*"); 588 * 589 * where <base> is the output base expressed as a control character, e.g. 590 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters, 591 * the first of which gives the bit number to be inspected (origin 1), and 592 * the next characters (up to a control character, i.e. a character <= 32), 593 * give the name of the register. Thus: 594 * 595 * kprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n"); 596 * 597 * would produce output: 598 * 599 * reg=3<BITTWO,BITONE> 600 * 601 * To support larger integers (> 32 bits), %b formatting will also accept 602 * control characters in the region 0x80 - 0xff. 0x80 refers to bit 0, 603 * 0x81 refers to bit 1, and so on. The equivalent string to the above is: 604 * 605 * kprintf("reg=%b\n", 3, "\10\201BITTWO\200BITONE\n"); 606 * 607 * and would produce the same output. 608 * 609 * Like the rest of printf, %b can be prefixed to handle various size 610 * modifiers, eg. %b is for "int", %lb is for "long", and %llb supports 611 * "long long". 612 * 613 * This code is large and complicated... 614 */ 615 616 /* 617 * macros for converting digits to letters and vice versa 618 */ 619 #define to_digit(c) ((c) - '0') 620 #define is_digit(c) ((unsigned)to_digit(c) <= 9) 621 #define to_char(n) ((n) + '0') 622 623 /* 624 * flags used during conversion. 625 */ 626 #define ALT 0x001 /* alternate form */ 627 #define HEXPREFIX 0x002 /* add 0x or 0X prefix */ 628 #define LADJUST 0x004 /* left adjustment */ 629 #define LONGDBL 0x008 /* long double; unimplemented */ 630 #define LONGINT 0x010 /* long integer */ 631 #define QUADINT 0x020 /* quad integer */ 632 #define SHORTINT 0x040 /* short integer */ 633 #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */ 634 #define FPT 0x100 /* Floating point number */ 635 #define SIZEINT 0x200 /* (signed) size_t */ 636 637 /* 638 * To extend shorts properly, we need both signed and unsigned 639 * argument extraction methods. 640 */ 641 #define SARG() \ 642 (flags&QUADINT ? va_arg(ap, quad_t) : \ 643 flags&LONGINT ? va_arg(ap, long) : \ 644 flags&SIZEINT ? va_arg(ap, ssize_t) : \ 645 flags&SHORTINT ? (long)(short)va_arg(ap, int) : \ 646 (long)va_arg(ap, int)) 647 #define UARG() \ 648 (flags&QUADINT ? va_arg(ap, u_quad_t) : \ 649 flags&LONGINT ? va_arg(ap, u_long) : \ 650 flags&SIZEINT ? va_arg(ap, size_t) : \ 651 flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \ 652 (u_long)va_arg(ap, u_int)) 653 654 #define KPRINTF_PUTCHAR(C) do { \ 655 int chr = (C); \ 656 ret += 1; \ 657 if (oflags & TOBUFONLY) { \ 658 if ((vp != NULL) && (sbuf == tailp)) { \ 659 if (!(oflags & TOCOUNT)) \ 660 goto overflow; \ 661 } else \ 662 *sbuf++ = chr; \ 663 } else { \ 664 kputchar(chr, oflags, (struct tty *)vp); \ 665 } \ 666 } while(0) 667 668 int 669 kprintf(const char *fmt0, int oflags, void *vp, char *sbuf, va_list ap) 670 { 671 char *fmt; /* format string */ 672 int ch; /* character from fmt */ 673 int n; /* handy integer (short term usage) */ 674 char *cp = NULL; /* handy char pointer (short term usage) */ 675 int flags; /* flags as above */ 676 int ret; /* return value accumulator */ 677 int width; /* width from format (%8d), or 0 */ 678 int prec; /* precision from format (%.3d), or -1 */ 679 char sign; /* sign prefix (' ', '+', '-', or \0) */ 680 681 u_quad_t _uquad; /* integer arguments %[diouxX] */ 682 enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */ 683 int dprec; /* a copy of prec if [diouxX], 0 otherwise */ 684 int realsz; /* field size expanded by dprec */ 685 int size = 0; /* size of converted field or string */ 686 char *xdigs = NULL; /* digits for [xX] conversion */ 687 char buf[KPRINTF_BUFSIZE]; /* space for %c, %[diouxX] */ 688 char *tailp = NULL; /* tail pointer for snprintf */ 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