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