1 /** $MirOS: src/sys/kern/subr_prf.c,v 1.6 2014/02/09 23:20:02 tg Exp $ */
2 /* $OpenBSD: subr_prf.c,v 1.61 2005/04/14 21:58:50 krw Exp $ */
3 /* $NetBSD: subr_prf.c,v 1.45 1997/10/24 18:14:25 chuck Exp $ */
4
5 /*-
6 * Copyright (c) 1986, 1988, 1991, 1993
7 * The Regents of the University of California. All rights reserved.
8 * (c) UNIX System Laboratories, Inc.
9 * All or some portions of this file are derived from material licensed
10 * to the University of California by American Telephone and Telegraph
11 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
12 * the permission of UNIX System Laboratories, Inc.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94
39 */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/buf.h>
44 #include <sys/conf.h>
45 #include <sys/reboot.h>
46 #include <sys/msgbuf.h>
47 #include <sys/proc.h>
48 #include <sys/ioctl.h>
49 #include <sys/vnode.h>
50 #include <sys/file.h>
51 #include <sys/tty.h>
52 #include <sys/tprintf.h>
53 #include <sys/syslog.h>
54 #include <sys/malloc.h>
55 #include <sys/pool.h>
56
57 #include <dev/cons.h>
58
59 /*
60 * note that stdarg.h and the ansi style va_start macro is used for both
61 * ansi and traditional c compilers.
62 */
63 #include <sys/stdarg.h>
64
65 #ifdef KGDB
66 #include <sys/kgdb.h>
67 #include <machine/cpu.h>
68 #endif
69 #ifdef DDB
70 #include <ddb/db_output.h> /* db_printf, db_putchar prototypes */
71 extern int db_radix; /* XXX: for non-standard '%r' format */
72 #endif
73 #if defined(UVM_SWAP_ENCRYPT)
74 extern int uvm_doswapencrypt;
75 #endif
76
77
78 /*
79 * defines
80 */
81
82 /* flags for kprintf */
83 #define TOCONS 0x01 /* to the console */
84 #define TOTTY 0x02 /* to the process' tty */
85 #define TOLOG 0x04 /* to the kernel message buffer */
86 #define TOBUFONLY 0x08 /* to the buffer (only) [for sprintf] */
87 #define TODDB 0x10 /* to ddb console */
88 #define TOCOUNT 0x20 /* act like [v]snprintf */
89
90 /* max size buffer kprintf needs to print quad_t [size in base 8 + \0] */
91 #define KPRINTF_BUFSIZE (sizeof(quad_t) * NBBY / 3 + 2)
92
93
94 /*
95 * local prototypes
96 */
97
98 int kprintf(const char *, int, void *, char *, va_list);
99 void kputchar(int, int, struct tty *);
100
101 /*
102 * globals
103 */
104
105 extern struct tty *constty; /* pointer to console "window" tty */
106 int consintr = 1; /* ok to handle console interrupts? */
107 extern int log_open; /* subr_log: is /dev/klog open? */
108 const char *panicstr; /* arg to first call to panic (used as a flag
109 to indicate that panic has already been called). */
110 void (*panic_hook)(void) = NULL;
111 void (*panic_hook_display)(void) = NULL;
112 #ifdef DDB
113 /*
114 * Enter ddb on panic.
115 */
116 int db_panic = 1;
117
118 /*
119 * db_console controls if we can be able to enter ddb by a special key
120 * combination (machine dependent).
121 * If DDB_SAFE_CONSOLE is defined in the kernel configuration it allows
122 * to break into console during boot. It's _really_ useful when debugging
123 * some things in the kernel that can cause init(8) to crash.
124 */
125 #ifdef DDB_SAFE_CONSOLE
126 int db_console = 1;
127 #else
128 int db_console = 0;
129 #endif
130 #endif
131
132 /*
133 * panic on spl assertion failure?
134 */
135 int splassert_ctl = 0;
136
137 /*
138 * v_putc: routine to putc on virtual console
139 *
140 * the v_putc pointer can be used to redirect the console cnputc elsewhere
141 * [e.g. to a "virtual console"].
142 */
143
144 void (*v_putc)(int) = cnputc; /* start with cnputc (normal cons) */
145
146
147 /*
148 * functions
149 */
150
151 /*
152 * Partial support (the failure case) of the assertion facility
153 * commonly found in userland.
154 */
155 void
__assert(t,f,l,e)156 __assert(t, f, l, e)
157 const char *t, *f, *e;
158 int l;
159 {
160
161 panic("kernel %sassertion \"%s\" failed: file \"%s\", line %d",
162 t, e, f, l);
163 }
164
165 /*
166 * tablefull: warn that a system table is full
167 */
168
169 void
tablefull(tab)170 tablefull(tab)
171 const char *tab;
172 {
173 log(LOG_ERR, "%s: table is full\n", tab);
174 }
175
176 /*
177 * panic: handle an unresolvable fatal error
178 *
179 * prints "panic: <message>" and reboots. if called twice (i.e. recursive
180 * call) we avoid trying to sync the disk and just reboot (to avoid
181 * recursive panics).
182 * A stripped version of this code is at ddb/db_usrreq.c - keep it in sync.
183 */
184
185 void
panic(const char * fmt,...)186 panic(const char *fmt, ...)
187 {
188 static char panicbuf[512];
189 int bootopt;
190 va_list ap;
191
192 bootopt = RB_AUTOBOOT | RB_DUMP;
193 #if defined(UVM_SWAP_ENCRYPT)
194 if (uvm_doswapencrypt)
195 bootopt &= ~RB_DUMP;
196 #endif
197 va_start(ap, fmt);
198 if (panicstr)
199 bootopt |= RB_NOSYNC;
200 else {
201 vsnprintf(panicbuf, sizeof panicbuf, fmt, ap);
202 panicstr = panicbuf;
203 }
204 va_end(ap);
205
206 if (panic_hook) {
207 void (*tmp_panic_hook)(void) = panic_hook;
208
209 panic_hook = NULL;
210 tmp_panic_hook();
211 }
212
213 printf("panic: ");
214 va_start(ap, fmt);
215 vprintf(fmt, ap);
216 printf("\n");
217 va_end(ap);
218
219 #ifdef KGDB
220 kgdb_panic();
221 #endif
222 #ifdef KADB
223 if (boothowto & RB_KDB)
224 kdbpanic();
225 #endif
226 #ifdef DDB
227 if (db_panic)
228 Debugger();
229 else
230 db_stack_dump();
231 #endif
232 boot(bootopt);
233 }
234
235 /*
236 * We print only the function name. The file name is usually very long and
237 * would eat tons of space in the kernel.
238 */
239 void
splassert_fail(int wantipl,int haveipl,const char * func)240 splassert_fail(int wantipl, int haveipl, const char *func)
241 {
242
243 printf("splassert: %s: want %d have %d\n", func, wantipl, haveipl);
244 switch (splassert_ctl) {
245 case 1:
246 break;
247 case 2:
248 #ifdef DDB
249 db_stack_dump();
250 #endif
251 break;
252 case 3:
253 #ifdef DDB
254 db_stack_dump();
255 Debugger();
256 #endif
257 break;
258 default:
259 panic("spl assertion failure in %s", func);
260 }
261 }
262
263 /*
264 * kernel logging functions: log, logpri, addlog
265 */
266
267 /*
268 * log: write to the log buffer
269 *
270 * => will not sleep [so safe to call from interrupt]
271 * => will log to console if /dev/klog isn't open
272 */
273
274 void
log(int level,const char * fmt,...)275 log(int level, const char *fmt, ...)
276 {
277 int s;
278 va_list ap;
279
280 s = splhigh();
281 logpri(level); /* log the level first */
282 va_start(ap, fmt);
283 kprintf(fmt, TOLOG, NULL, NULL, ap);
284 va_end(ap);
285 splx(s);
286 if (!log_open) {
287 va_start(ap, fmt);
288 kprintf(fmt, TOCONS, NULL, NULL, ap);
289 va_end(ap);
290 }
291 logwakeup(); /* wake up anyone waiting for log msgs */
292 }
293
294 /*
295 * logpri: log the priority level to the klog
296 */
297
298 void
logpri(int level)299 logpri(int level)
300 {
301 char *p;
302 char snbuf[KPRINTF_BUFSIZE];
303
304 kputchar('<', TOLOG, NULL);
305 snprintf(snbuf, sizeof snbuf, "%d", level);
306 for (p = snbuf ; *p ; p++)
307 kputchar(*p, TOLOG, NULL);
308 kputchar('>', TOLOG, NULL);
309 }
310
311 /*
312 * addlog: add info to previous log message
313 */
314
315 int
addlog(const char * fmt,...)316 addlog(const char *fmt, ...)
317 {
318 int s;
319 va_list ap;
320
321 s = splhigh();
322 va_start(ap, fmt);
323 kprintf(fmt, TOLOG, NULL, NULL, ap);
324 va_end(ap);
325 splx(s);
326 if (!log_open) {
327 va_start(ap, fmt);
328 kprintf(fmt, TOCONS, NULL, NULL, ap);
329 va_end(ap);
330 }
331 logwakeup();
332 return(0);
333 }
334
335
336 /*
337 * kputchar: print a single character on console or user terminal.
338 *
339 * => if console, then the last MSGBUFS chars are saved in msgbuf
340 * for inspection later (e.g. dmesg/syslog)
341 */
342 void
kputchar(int c,int flags,struct tty * tp)343 kputchar(int c, int flags, struct tty *tp)
344 {
345 extern int msgbufmapped;
346
347 if (panicstr)
348 constty = NULL;
349 if ((flags & TOCONS) && tp == NULL && constty) {
350 tp = constty;
351 flags |= TOTTY;
352 }
353 if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 &&
354 (flags & TOCONS) && tp == constty)
355 constty = NULL;
356 if ((flags & TOLOG) &&
357 c != '\0' && c != '\r' && c != 0177 && msgbufmapped)
358 msgbuf_putchar(c);
359 if ((flags & TOCONS) && constty == NULL && c != '\0')
360 (*v_putc)(c);
361 #ifdef DDB
362 if (flags & TODDB)
363 db_putchar(c);
364 #endif
365 }
366
367
368 /*
369 * uprintf: print to the controlling tty of the current process
370 *
371 * => we may block if the tty queue is full
372 * => no message is printed if the queue doesn't clear in a reasonable
373 * time
374 */
375
376 void
uprintf(const char * fmt,...)377 uprintf(const char *fmt, ...)
378 {
379 struct proc *p = curproc;
380 va_list ap;
381
382 if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
383 va_start(ap, fmt);
384 kprintf(fmt, TOTTY, p->p_session->s_ttyp, NULL, ap);
385 va_end(ap);
386 }
387 }
388
389 /*
390 * tprintf functions: used to send messages to a specific process
391 *
392 * usage:
393 * get a tpr_t handle on a process "p" by using "tprintf_open(p)"
394 * use the handle when calling "tprintf"
395 * when done, do a "tprintf_close" to drop the handle
396 */
397
398 /*
399 * tprintf_open: get a tprintf handle on a process "p"
400 *
401 * => returns NULL if process can't be printed to
402 */
403
404 tpr_t
tprintf_open(struct proc * p)405 tprintf_open(struct proc *p)
406 {
407
408 if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
409 SESSHOLD(p->p_session);
410 return ((tpr_t) p->p_session);
411 }
412 return ((tpr_t) NULL);
413 }
414
415 /*
416 * tprintf_close: dispose of a tprintf handle obtained with tprintf_open
417 */
418
419 void
tprintf_close(tpr_t sess)420 tprintf_close(tpr_t sess)
421 {
422
423 if (sess)
424 SESSRELE((struct session *) sess);
425 }
426
427 /*
428 * tprintf: given tprintf handle to a process [obtained with tprintf_open],
429 * send a message to the controlling tty for that process.
430 *
431 * => also sends message to /dev/klog
432 */
433 void
tprintf(tpr_t tpr,const char * fmt,...)434 tprintf(tpr_t tpr, const char *fmt, ...)
435 {
436 struct session *sess = (struct session *)tpr;
437 struct tty *tp = NULL;
438 int flags = TOLOG;
439 va_list ap;
440
441 logpri(LOG_INFO);
442 if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
443 flags |= TOTTY;
444 tp = sess->s_ttyp;
445 }
446 va_start(ap, fmt);
447 kprintf(fmt, flags, tp, NULL, ap);
448 va_end(ap);
449 logwakeup();
450 }
451
452
453 /*
454 * ttyprintf: send a message to a specific tty
455 *
456 * => should be used only by tty driver or anything that knows the
457 * underlying tty will not be revoked(2)'d away. [otherwise,
458 * use tprintf]
459 */
460 void
ttyprintf(struct tty * tp,const char * fmt,...)461 ttyprintf(struct tty *tp, const char *fmt, ...)
462 {
463 va_list ap;
464
465 va_start(ap, fmt);
466 kprintf(fmt, TOTTY, tp, NULL, ap);
467 va_end(ap);
468 }
469
470 #ifdef DDB
471
472 /*
473 * db_printf: printf for DDB (via db_putchar)
474 */
475
476 int
db_printf(const char * fmt,...)477 db_printf(const char *fmt, ...)
478 {
479 va_list ap;
480 int flags, retval;
481
482 flags = TODDB;
483 if (db_log)
484 flags |= TOLOG;
485 va_start(ap, fmt);
486 retval = kprintf(fmt, flags, NULL, NULL, ap);
487 va_end(ap);
488 return(retval);
489 }
490
491 #endif /* DDB */
492
493
494 /*
495 * normal kernel printf functions: printf, vprintf, sprintf
496 */
497
498 /*
499 * printf: print a message to the console and the log
500 */
501 int
printf(const char * fmt,...)502 printf(const char *fmt, ...)
503 {
504 va_list ap;
505 int savintr, retval;
506
507 savintr = consintr; /* disable interrupts */
508 consintr = 0;
509 va_start(ap, fmt);
510 retval = kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap);
511 va_end(ap);
512 if (!panicstr)
513 logwakeup();
514 consintr = savintr; /* reenable interrupts */
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
vprintf(const char * fmt,va_list ap)524 vprintf(const char *fmt, va_list ap)
525 {
526 int savintr, retval;
527
528 savintr = consintr; /* disable interrupts */
529 consintr = 0;
530 retval = kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap);
531 if (!panicstr)
532 logwakeup();
533 consintr = savintr; /* reenable interrupts */
534 return (retval);
535 }
536
537 /*
538 * snprintf: print a message to a buffer
539 */
540 int
snprintf(char * buf,size_t size,const char * fmt,...)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
vsnprintf(char * buf,size_t size,const char * fmt,va_list ap)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 * Two additional formats:
583 *
584 * The 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 * This code is large and complicated...
602 */
603
604 /*
605 * macros for converting digits to letters and vice versa
606 */
607 #define to_digit(c) ((c) - '0')
608 #define is_digit(c) ((unsigned)to_digit(c) <= 9)
609 #define to_char(n) ((n) + '0')
610
611 /*
612 * flags used during conversion.
613 */
614 #define ALT 0x001 /* alternate form */
615 #define HEXPREFIX 0x002 /* add 0x or 0X prefix */
616 #define LADJUST 0x004 /* left adjustment */
617 /*#define LONGDBL 0x008 * long double; unimplemented */
618 #define LONGINT 0x010 /* long integer */
619 #define QUADINT 0x020 /* quad integer */
620 #define SHORTINT 0x040 /* short integer */
621 #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */
622 #define FPT 0x100 /* Floating point number */
623
624 /*
625 * To extend shorts properly, we need both signed and unsigned
626 * argument extraction methods.
627 */
628 #define SARG() \
629 (flags&QUADINT ? va_arg(ap, quad_t) : \
630 flags&LONGINT ? va_arg(ap, long) : \
631 flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
632 (long)va_arg(ap, int))
633 #define UARG() \
634 (flags&QUADINT ? va_arg(ap, u_quad_t) : \
635 flags&LONGINT ? va_arg(ap, u_long) : \
636 flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
637 (u_long)va_arg(ap, u_int))
638
639 #define KPRINTF_PUTCHAR(C) do { \
640 int chr = (C); \
641 ret += 1; \
642 if (oflags & TOBUFONLY) { \
643 if ((vp != NULL) && (sbuf == tailp)) { \
644 if (!(oflags & TOCOUNT)) \
645 goto overflow; \
646 } else \
647 *sbuf++ = chr; \
648 } else { \
649 kputchar(chr, oflags, (struct tty *)vp); \
650 } \
651 } while(0)
652
653 int
kprintf(const char * fmt0,int oflags,void * vp,char * sbuf,va_list ap)654 kprintf(const char *fmt0, int oflags, void *vp, char *sbuf, va_list ap)
655 {
656 char *fmt; /* format string */
657 int ch; /* character from fmt */
658 int n; /* handy integer (short term usage) */
659 char *cp = NULL; /* handy char pointer (short term usage) */
660 int flags; /* flags as above */
661 int ret; /* return value accumulator */
662 int width; /* width from format (%8d), or 0 */
663 int prec; /* precision from format (%.3d), or -1 */
664 char sign; /* sign prefix (' ', '+', '-', or \0) */
665
666 u_quad_t _uquad; /* integer arguments %[diouxX] */
667 enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
668 int dprec; /* a copy of prec if [diouxX], 0 otherwise */
669 int realsz; /* field size expanded by dprec */
670 int size = 0; /* size of converted field or string */
671 char *xdigs = NULL; /* digits for [xX] conversion */
672 char buf[KPRINTF_BUFSIZE]; /* space for %c, %[diouxX] */
673 char *tailp = NULL; /* tail pointer for snprintf */
674
675 if ((oflags & TOBUFONLY) && (vp != NULL))
676 tailp = *(char **)vp;
677
678 fmt = (char *)fmt0;
679 ret = 0;
680
681 /*
682 * Scan the format for conversions (`%' character).
683 */
684 for (;;) {
685 while (*fmt != '%' && *fmt) {
686 KPRINTF_PUTCHAR(*fmt++);
687 }
688 if (*fmt == 0)
689 goto done;
690
691 fmt++; /* skip over '%' */
692
693 flags = 0;
694 dprec = 0;
695 width = 0;
696 prec = -1;
697 sign = '\0';
698
699 rflag: ch = *fmt++;
700 reswitch: switch (ch) {
701 /* XXX: non-standard '%b' format */
702 case 'b': {
703 char *b, *z;
704 int tmp;
705 _uquad = va_arg(ap, u_int);
706 b = va_arg(ap, char *);
707 if (*b == 8)
708 snprintf(buf, sizeof buf, "%llo", _uquad);
709 else if (*b == 10)
710 snprintf(buf, sizeof buf, "%lld", _uquad);
711 else if (*b == 16)
712 snprintf(buf, sizeof buf, "%llx", _uquad);
713 else
714 break;
715 b++;
716
717 z = buf;
718 while (*z) {
719 KPRINTF_PUTCHAR(*z++);
720 }
721
722 if (_uquad) {
723 tmp = 0;
724 while ((n = *b++) != 0) {
725 if (_uquad & (1 << (n - 1))) {
726 KPRINTF_PUTCHAR(tmp ? ',':'<');
727 while ((n = *b) > ' ') {
728 KPRINTF_PUTCHAR(n);
729 b++;
730 }
731 tmp = 1;
732 } else {
733 while(*b > ' ')
734 b++;
735 }
736 }
737 if (tmp) {
738 KPRINTF_PUTCHAR('>');
739 }
740 }
741 continue; /* no output */
742 }
743
744 #ifdef DDB
745 /* XXX: non-standard '%r' format (print int in db_radix) */
746 case 'r':
747 if ((oflags & TODDB) == 0)
748 goto default_case;
749
750 if (db_radix == 16)
751 goto case_z; /* signed hex */
752 _uquad = SARG();
753 if ((quad_t)_uquad < 0) {
754 _uquad = -_uquad;
755 sign = '-';
756 }
757 base = (db_radix == 8) ? OCT : DEC;
758 goto number;
759
760
761 /* XXX: non-standard '%z' format ("signed hex", a "hex %i")*/
762 case 'z':
763 case_z:
764 if ((oflags & TODDB) == 0)
765 goto default_case;
766
767 xdigs = "0123456789abcdef";
768 ch = 'x'; /* the 'x' in '0x' (below) */
769 _uquad = SARG();
770 base = HEX;
771 /* leading 0x/X only if non-zero */
772 if (flags & ALT && _uquad != 0)
773 flags |= HEXPREFIX;
774 if ((quad_t)_uquad < 0) {
775 _uquad = -_uquad;
776 sign = '-';
777 }
778 goto number;
779 #endif
780
781 case ' ':
782 /*
783 * ``If the space and + flags both appear, the space
784 * flag will be ignored.''
785 * -- ANSI X3J11
786 */
787 if (!sign)
788 sign = ' ';
789 goto rflag;
790 case '#':
791 flags |= ALT;
792 goto rflag;
793 case '*':
794 /*
795 * ``A negative field width argument is taken as a
796 * - flag followed by a positive field width.''
797 * -- ANSI X3J11
798 * They don't exclude field widths read from args.
799 */
800 if ((width = va_arg(ap, int)) >= 0)
801 goto rflag;
802 width = -width;
803 /* FALLTHROUGH */
804 case '-':
805 flags |= LADJUST;
806 goto rflag;
807 case '+':
808 sign = '+';
809 goto rflag;
810 case '.':
811 if ((ch = *fmt++) == '*') {
812 n = va_arg(ap, int);
813 prec = n < 0 ? -1 : n;
814 goto rflag;
815 }
816 n = 0;
817 while (is_digit(ch)) {
818 n = 10 * n + to_digit(ch);
819 ch = *fmt++;
820 }
821 prec = n < 0 ? -1 : n;
822 goto reswitch;
823 case '0':
824 /*
825 * ``Note that 0 is taken as a flag, not as the
826 * beginning of a field width.''
827 * -- ANSI X3J11
828 */
829 flags |= ZEROPAD;
830 goto rflag;
831 case '1': case '2': case '3': case '4':
832 case '5': case '6': case '7': case '8': case '9':
833 n = 0;
834 do {
835 n = 10 * n + to_digit(ch);
836 ch = *fmt++;
837 } while (is_digit(ch));
838 width = n;
839 goto reswitch;
840 case 'h':
841 flags |= SHORTINT;
842 goto rflag;
843 case 'l':
844 if (*fmt == 'l') {
845 fmt++;
846 flags |= QUADINT;
847 } else {
848 flags |= LONGINT;
849 }
850 goto rflag;
851 case 'q':
852 flags |= QUADINT;
853 goto rflag;
854 case 'c':
855 *(cp = buf) = va_arg(ap, int);
856 size = 1;
857 sign = '\0';
858 break;
859 case 'D':
860 flags |= LONGINT;
861 /*FALLTHROUGH*/
862 case 'd':
863 case 'i':
864 _uquad = SARG();
865 if ((quad_t)_uquad < 0) {
866 _uquad = -_uquad;
867 sign = '-';
868 }
869 base = DEC;
870 goto number;
871 case 'n':
872 #ifdef DDB
873 /* XXX: non-standard '%n' format */
874 /*
875 * XXX: HACK! DDB wants '%n' to be a '%u' printed
876 * in db_radix format. this should die since '%n'
877 * is already defined in standard printf to write
878 * the number of chars printed so far to the arg (which
879 * should be a pointer.
880 */
881 if (oflags & TODDB) {
882 if (db_radix == 16)
883 ch = 'x'; /* convert to %x */
884 else if (db_radix == 8)
885 ch = 'o'; /* convert to %o */
886 else
887 ch = 'u'; /* convert to %u */
888
889 /* ... and start again */
890 goto reswitch;
891 }
892
893 #endif
894 if (flags & QUADINT)
895 *va_arg(ap, quad_t *) = ret;
896 else if (flags & LONGINT)
897 *va_arg(ap, long *) = ret;
898 else if (flags & SHORTINT)
899 *va_arg(ap, short *) = ret;
900 else
901 *va_arg(ap, int *) = ret;
902 continue; /* no output */
903 case 'O':
904 flags |= LONGINT;
905 /*FALLTHROUGH*/
906 case 'o':
907 _uquad = UARG();
908 base = OCT;
909 goto nosign;
910 case 'p':
911 /*
912 * ``The argument shall be a pointer to void. The
913 * value of the pointer is converted to a sequence
914 * of printable characters, in an implementation-
915 * defined manner.''
916 * -- ANSI X3J11
917 */
918 /* NOSTRICT */
919 _uquad = (u_long)va_arg(ap, void *);
920 base = HEX;
921 xdigs = "0123456789abcdef";
922 flags |= HEXPREFIX;
923 ch = 'x';
924 goto nosign;
925 case 's':
926 if ((cp = va_arg(ap, char *)) == NULL)
927 cp = "(null)";
928 if (prec >= 0) {
929 /*
930 * can't use strlen; can only look for the
931 * NUL in the first `prec' characters, and
932 * strlen() will go further.
933 */
934 char *p = memchr(cp, 0, prec);
935
936 if (p != NULL) {
937 size = p - cp;
938 if (size > prec)
939 size = prec;
940 } else
941 size = prec;
942 } else
943 size = strlen(cp);
944 sign = '\0';
945 break;
946 case 'U':
947 flags |= LONGINT;
948 /*FALLTHROUGH*/
949 case 'u':
950 _uquad = UARG();
951 base = DEC;
952 goto nosign;
953 case 'X':
954 xdigs = "0123456789ABCDEF";
955 goto hex;
956 case 'x':
957 xdigs = "0123456789abcdef";
958 hex: _uquad = UARG();
959 base = HEX;
960 /* leading 0x/X only if non-zero */
961 if (flags & ALT && _uquad != 0)
962 flags |= HEXPREFIX;
963
964 /* unsigned conversions */
965 nosign: sign = '\0';
966 /*
967 * ``... diouXx conversions ... if a precision is
968 * specified, the 0 flag will be ignored.''
969 * -- ANSI X3J11
970 */
971 number: if ((dprec = prec) >= 0)
972 flags &= ~ZEROPAD;
973
974 /*
975 * ``The result of converting a zero value with an
976 * explicit precision of zero is no characters.''
977 * -- ANSI X3J11
978 */
979 cp = buf + KPRINTF_BUFSIZE;
980 if (_uquad != 0 || prec != 0) {
981 /*
982 * Unsigned mod is hard, and unsigned mod
983 * by a constant is easier than that by
984 * a variable; hence this switch.
985 */
986 switch (base) {
987 case OCT:
988 do {
989 *--cp = to_char(_uquad & 7);
990 _uquad >>= 3;
991 } while (_uquad);
992 /* handle octal leading 0 */
993 if (flags & ALT && *cp != '0')
994 *--cp = '0';
995 break;
996
997 case DEC:
998 /* many numbers are 1 digit */
999 while (_uquad >= 10) {
1000 *--cp = to_char(_uquad % 10);
1001 _uquad /= 10;
1002 }
1003 *--cp = to_char(_uquad);
1004 break;
1005
1006 case HEX:
1007 do {
1008 *--cp = xdigs[_uquad & 15];
1009 _uquad >>= 4;
1010 } while (_uquad);
1011 break;
1012
1013 default:
1014 cp = "bug in kprintf: bad base";
1015 size = strlen(cp);
1016 goto skipsize;
1017 }
1018 }
1019 size = buf + KPRINTF_BUFSIZE - cp;
1020 skipsize:
1021 break;
1022 default: /* "%?" prints ?, unless ? is NUL */
1023 #ifdef DDB
1024 default_case: /* DDB */
1025 #endif
1026 if (ch == '\0')
1027 goto done;
1028 /* pretend it was %c with argument ch */
1029 cp = buf;
1030 *cp = ch;
1031 size = 1;
1032 sign = '\0';
1033 break;
1034 }
1035
1036 /*
1037 * All reasonable formats wind up here. At this point, `cp'
1038 * points to a string which (if not flags&LADJUST) should be
1039 * padded out to `width' places. If flags&ZEROPAD, it should
1040 * first be prefixed by any sign or other prefix; otherwise,
1041 * it should be blank padded before the prefix is emitted.
1042 * After any left-hand padding and prefixing, emit zeroes
1043 * required by a decimal [diouxX] precision, then print the
1044 * string proper, then emit zeroes required by any leftover
1045 * floating precision; finally, if LADJUST, pad with blanks.
1046 *
1047 * Compute actual size, so we know how much to pad.
1048 * size excludes decimal prec; realsz includes it.
1049 */
1050 realsz = dprec > size ? dprec : size;
1051 if (sign)
1052 realsz++;
1053 else if (flags & HEXPREFIX)
1054 realsz+= 2;
1055
1056 /* right-adjusting blank padding */
1057 if ((flags & (LADJUST|ZEROPAD)) == 0) {
1058 n = width - realsz;
1059 while (n-- > 0)
1060 KPRINTF_PUTCHAR(' ');
1061 }
1062
1063 /* prefix */
1064 if (sign) {
1065 KPRINTF_PUTCHAR(sign);
1066 } else if (flags & HEXPREFIX) {
1067 KPRINTF_PUTCHAR('0');
1068 KPRINTF_PUTCHAR(ch);
1069 }
1070
1071 /* right-adjusting zero padding */
1072 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) {
1073 n = width - realsz;
1074 while (n-- > 0)
1075 KPRINTF_PUTCHAR('0');
1076 }
1077
1078 /* leading zeroes from decimal precision */
1079 n = dprec - size;
1080 while (n-- > 0)
1081 KPRINTF_PUTCHAR('0');
1082
1083 /* the string or number proper */
1084 while (size--)
1085 KPRINTF_PUTCHAR(*cp++);
1086 /* left-adjusting padding (always blank) */
1087 if (flags & LADJUST) {
1088 n = width - realsz;
1089 while (n-- > 0)
1090 KPRINTF_PUTCHAR(' ');
1091 }
1092 }
1093
1094 done:
1095 if ((oflags & TOBUFONLY) && (vp != NULL))
1096 *(char **)vp = sbuf;
1097 overflow:
1098 return (ret);
1099 /* NOTREACHED */
1100 }
1101