xref: /openbsd-src/sys/kern/subr_prf.c (revision e2a1b4748ac00cfe1e64a346f850b3c670166aef)
1 /*	$OpenBSD: subr_prf.c,v 1.58 2004/01/03 14:08:53 espie 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 <sys/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  * This code is large and complicated...
605  */
606 
607 /*
608  * macros for converting digits to letters and vice versa
609  */
610 #define	to_digit(c)	((c) - '0')
611 #define is_digit(c)	((unsigned)to_digit(c) <= 9)
612 #define	to_char(n)	((n) + '0')
613 
614 /*
615  * flags used during conversion.
616  */
617 #define	ALT		0x001		/* alternate form */
618 #define	HEXPREFIX	0x002		/* add 0x or 0X prefix */
619 #define	LADJUST		0x004		/* left adjustment */
620 #define	LONGDBL		0x008		/* long double; unimplemented */
621 #define	LONGINT		0x010		/* long integer */
622 #define	QUADINT		0x020		/* quad integer */
623 #define	SHORTINT	0x040		/* short integer */
624 #define	ZEROPAD		0x080		/* zero (as opposed to blank) pad */
625 #define FPT		0x100		/* Floating point number */
626 
627 	/*
628 	 * To extend shorts properly, we need both signed and unsigned
629 	 * argument extraction methods.
630 	 */
631 #define	SARG() \
632 	(flags&QUADINT ? va_arg(ap, quad_t) : \
633 	    flags&LONGINT ? va_arg(ap, long) : \
634 	    flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
635 	    (long)va_arg(ap, int))
636 #define	UARG() \
637 	(flags&QUADINT ? va_arg(ap, u_quad_t) : \
638 	    flags&LONGINT ? va_arg(ap, u_long) : \
639 	    flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
640 	    (u_long)va_arg(ap, u_int))
641 
642 #define KPRINTF_PUTCHAR(C) do {					\
643 	int chr = (C);							\
644 	ret += 1;							\
645 	if (oflags & TOBUFONLY) {					\
646 		if ((vp != NULL) && (sbuf == tailp)) {			\
647 			if (!(oflags & TOCOUNT))				\
648 				goto overflow;				\
649 		} else							\
650 			*sbuf++ = chr;					\
651 	} else {							\
652 		kputchar(chr, oflags, (struct tty *)vp);			\
653 	}								\
654 } while(0)
655 
656 int
657 kprintf(const char *fmt0, int oflags, void *vp, char *sbuf, va_list ap)
658 {
659 	char *fmt;		/* format string */
660 	int ch;			/* character from fmt */
661 	int n;			/* handy integer (short term usage) */
662 	char *cp = NULL;	/* handy char pointer (short term usage) */
663 	int flags;		/* flags as above */
664 	int ret;		/* return value accumulator */
665 	int width;		/* width from format (%8d), or 0 */
666 	int prec;		/* precision from format (%.3d), or -1 */
667 	char sign;		/* sign prefix (' ', '+', '-', or \0) */
668 
669 	u_quad_t _uquad;	/* integer arguments %[diouxX] */
670 	enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
671 	int dprec;		/* a copy of prec if [diouxX], 0 otherwise */
672 	int realsz;		/* field size expanded by dprec */
673 	int size = 0;		/* size of converted field or string */
674 	char *xdigs = NULL;	/* digits for [xX] conversion */
675 	char buf[KPRINTF_BUFSIZE]; /* space for %c, %[diouxX] */
676 	char *tailp = NULL;	/* tail pointer for snprintf */
677 
678 	if ((oflags & TOBUFONLY) && (vp != NULL))
679 		tailp = *(char **)vp;
680 
681 	fmt = (char *)fmt0;
682 	ret = 0;
683 
684 	/*
685 	 * Scan the format for conversions (`%' character).
686 	 */
687 	for (;;) {
688 		while (*fmt != '%' && *fmt) {
689 			KPRINTF_PUTCHAR(*fmt++);
690 		}
691 		if (*fmt == 0)
692 			goto done;
693 
694 		fmt++;		/* skip over '%' */
695 
696 		flags = 0;
697 		dprec = 0;
698 		width = 0;
699 		prec = -1;
700 		sign = '\0';
701 
702 rflag:		ch = *fmt++;
703 reswitch:	switch (ch) {
704 		/* XXX: non-standard '%b' format */
705 		case 'b': {
706 			char *b, *z;
707 			int tmp;
708 			_uquad = va_arg(ap, u_int);
709 			b = va_arg(ap, char *);
710 			if (*b == 8)
711 				snprintf(buf, sizeof buf, "%llo", _uquad);
712 			else if (*b == 10)
713 				snprintf(buf, sizeof buf, "%lld", _uquad);
714 			else if (*b == 16)
715 				snprintf(buf, sizeof buf, "%llx", _uquad);
716 			else
717 				break;
718 			b++;
719 
720 			z = buf;
721 			while (*z) {
722 				KPRINTF_PUTCHAR(*z++);
723 			}
724 
725 			if (_uquad) {
726 				tmp = 0;
727 				while ((n = *b++) != 0) {
728 					if (_uquad & (1 << (n - 1))) {
729 						KPRINTF_PUTCHAR(tmp ? ',':'<');
730 						while ((n = *b) > ' ') {
731 							KPRINTF_PUTCHAR(n);
732 							b++;
733 						}
734 						tmp = 1;
735 					} else {
736 						while(*b > ' ')
737 							b++;
738 					}
739 				}
740 				if (tmp) {
741 					KPRINTF_PUTCHAR('>');
742 				}
743 			}
744 			continue;	/* no output */
745 		}
746 
747 #ifdef DDB
748 		/* XXX: non-standard '%r' format (print int in db_radix) */
749 		case 'r':
750 			if ((oflags & TODDB) == 0)
751 				goto default_case;
752 
753 			if (db_radix == 16)
754 				goto case_z;	/* signed hex */
755 			_uquad = SARG();
756 			if ((quad_t)_uquad < 0) {
757 				_uquad = -_uquad;
758 				sign = '-';
759 			}
760 			base = (db_radix == 8) ? OCT : DEC;
761 			goto number;
762 
763 
764 		/* XXX: non-standard '%z' format ("signed hex", a "hex %i")*/
765 		case 'z':
766 		case_z:
767 			if ((oflags & TODDB) == 0)
768 				goto default_case;
769 
770 			xdigs = "0123456789abcdef";
771 			ch = 'x';	/* the 'x' in '0x' (below) */
772 			_uquad = SARG();
773 			base = HEX;
774 			/* leading 0x/X only if non-zero */
775 			if (flags & ALT && _uquad != 0)
776 				flags |= HEXPREFIX;
777 			if ((quad_t)_uquad < 0) {
778 				_uquad = -_uquad;
779 				sign = '-';
780 			}
781 			goto number;
782 #endif
783 
784 		case ' ':
785 			/*
786 			 * ``If the space and + flags both appear, the space
787 			 * flag will be ignored.''
788 			 *	-- ANSI X3J11
789 			 */
790 			if (!sign)
791 				sign = ' ';
792 			goto rflag;
793 		case '#':
794 			flags |= ALT;
795 			goto rflag;
796 		case '*':
797 			/*
798 			 * ``A negative field width argument is taken as a
799 			 * - flag followed by a positive field width.''
800 			 *	-- ANSI X3J11
801 			 * They don't exclude field widths read from args.
802 			 */
803 			if ((width = va_arg(ap, int)) >= 0)
804 				goto rflag;
805 			width = -width;
806 			/* FALLTHROUGH */
807 		case '-':
808 			flags |= LADJUST;
809 			goto rflag;
810 		case '+':
811 			sign = '+';
812 			goto rflag;
813 		case '.':
814 			if ((ch = *fmt++) == '*') {
815 				n = va_arg(ap, int);
816 				prec = n < 0 ? -1 : n;
817 				goto rflag;
818 			}
819 			n = 0;
820 			while (is_digit(ch)) {
821 				n = 10 * n + to_digit(ch);
822 				ch = *fmt++;
823 			}
824 			prec = n < 0 ? -1 : n;
825 			goto reswitch;
826 		case '0':
827 			/*
828 			 * ``Note that 0 is taken as a flag, not as the
829 			 * beginning of a field width.''
830 			 *	-- ANSI X3J11
831 			 */
832 			flags |= ZEROPAD;
833 			goto rflag;
834 		case '1': case '2': case '3': case '4':
835 		case '5': case '6': case '7': case '8': case '9':
836 			n = 0;
837 			do {
838 				n = 10 * n + to_digit(ch);
839 				ch = *fmt++;
840 			} while (is_digit(ch));
841 			width = n;
842 			goto reswitch;
843 		case 'h':
844 			flags |= SHORTINT;
845 			goto rflag;
846 		case 'l':
847 			if (*fmt == 'l') {
848 				fmt++;
849 				flags |= QUADINT;
850 			} else {
851 				flags |= LONGINT;
852 			}
853 			goto rflag;
854 		case 'q':
855 			flags |= QUADINT;
856 			goto rflag;
857 		case 'c':
858 			*(cp = buf) = va_arg(ap, int);
859 			size = 1;
860 			sign = '\0';
861 			break;
862 		case 'D':
863 			flags |= LONGINT;
864 			/*FALLTHROUGH*/
865 		case 'd':
866 		case 'i':
867 			_uquad = SARG();
868 			if ((quad_t)_uquad < 0) {
869 				_uquad = -_uquad;
870 				sign = '-';
871 			}
872 			base = DEC;
873 			goto number;
874 		case 'n':
875 #ifdef DDB
876 		/* XXX: non-standard '%n' format */
877 		/*
878 		 * XXX: HACK!   DDB wants '%n' to be a '%u' printed
879 		 * in db_radix format.   this should die since '%n'
880 		 * is already defined in standard printf to write
881 		 * the number of chars printed so far to the arg (which
882 		 * should be a pointer.
883 		 */
884 			if (oflags & TODDB) {
885 				if (db_radix == 16)
886 					ch = 'x';	/* convert to %x */
887 				else if (db_radix == 8)
888 					ch = 'o';	/* convert to %o */
889 				else
890 					ch = 'u';	/* convert to %u */
891 
892 				/* ... and start again */
893 				goto reswitch;
894 			}
895 
896 #endif
897 			if (flags & QUADINT)
898 				*va_arg(ap, quad_t *) = ret;
899 			else if (flags & LONGINT)
900 				*va_arg(ap, long *) = ret;
901 			else if (flags & SHORTINT)
902 				*va_arg(ap, short *) = ret;
903 			else
904 				*va_arg(ap, int *) = ret;
905 			continue;	/* no output */
906 		case 'O':
907 			flags |= LONGINT;
908 			/*FALLTHROUGH*/
909 		case 'o':
910 			_uquad = UARG();
911 			base = OCT;
912 			goto nosign;
913 		case 'p':
914 			/*
915 			 * ``The argument shall be a pointer to void.  The
916 			 * value of the pointer is converted to a sequence
917 			 * of printable characters, in an implementation-
918 			 * defined manner.''
919 			 *	-- ANSI X3J11
920 			 */
921 			/* NOSTRICT */
922 			_uquad = (u_long)va_arg(ap, void *);
923 			base = HEX;
924 			xdigs = "0123456789abcdef";
925 			flags |= HEXPREFIX;
926 			ch = 'x';
927 			goto nosign;
928 		case 's':
929 			if ((cp = va_arg(ap, char *)) == NULL)
930 				cp = "(null)";
931 			if (prec >= 0) {
932 				/*
933 				 * can't use strlen; can only look for the
934 				 * NUL in the first `prec' characters, and
935 				 * strlen() will go further.
936 				 */
937 				char *p = memchr(cp, 0, prec);
938 
939 				if (p != NULL) {
940 					size = p - cp;
941 					if (size > prec)
942 						size = prec;
943 				} else
944 					size = prec;
945 			} else
946 				size = strlen(cp);
947 			sign = '\0';
948 			break;
949 		case 'U':
950 			flags |= LONGINT;
951 			/*FALLTHROUGH*/
952 		case 'u':
953 			_uquad = UARG();
954 			base = DEC;
955 			goto nosign;
956 		case 'X':
957 			xdigs = "0123456789ABCDEF";
958 			goto hex;
959 		case 'x':
960 			xdigs = "0123456789abcdef";
961 hex:			_uquad = UARG();
962 			base = HEX;
963 			/* leading 0x/X only if non-zero */
964 			if (flags & ALT && _uquad != 0)
965 				flags |= HEXPREFIX;
966 
967 			/* unsigned conversions */
968 nosign:			sign = '\0';
969 			/*
970 			 * ``... diouXx conversions ... if a precision is
971 			 * specified, the 0 flag will be ignored.''
972 			 *	-- ANSI X3J11
973 			 */
974 number:			if ((dprec = prec) >= 0)
975 				flags &= ~ZEROPAD;
976 
977 			/*
978 			 * ``The result of converting a zero value with an
979 			 * explicit precision of zero is no characters.''
980 			 *	-- ANSI X3J11
981 			 */
982 			cp = buf + KPRINTF_BUFSIZE;
983 			if (_uquad != 0 || prec != 0) {
984 				/*
985 				 * Unsigned mod is hard, and unsigned mod
986 				 * by a constant is easier than that by
987 				 * a variable; hence this switch.
988 				 */
989 				switch (base) {
990 				case OCT:
991 					do {
992 						*--cp = to_char(_uquad & 7);
993 						_uquad >>= 3;
994 					} while (_uquad);
995 					/* handle octal leading 0 */
996 					if (flags & ALT && *cp != '0')
997 						*--cp = '0';
998 					break;
999 
1000 				case DEC:
1001 					/* many numbers are 1 digit */
1002 					while (_uquad >= 10) {
1003 						*--cp = to_char(_uquad % 10);
1004 						_uquad /= 10;
1005 					}
1006 					*--cp = to_char(_uquad);
1007 					break;
1008 
1009 				case HEX:
1010 					do {
1011 						*--cp = xdigs[_uquad & 15];
1012 						_uquad >>= 4;
1013 					} while (_uquad);
1014 					break;
1015 
1016 				default:
1017 					cp = "bug in kprintf: bad base";
1018 					size = strlen(cp);
1019 					goto skipsize;
1020 				}
1021 			}
1022 			size = buf + KPRINTF_BUFSIZE - cp;
1023 		skipsize:
1024 			break;
1025 		default:	/* "%?" prints ?, unless ? is NUL */
1026 #ifdef DDB
1027 		default_case:	/* DDB */
1028 #endif
1029 			if (ch == '\0')
1030 				goto done;
1031 			/* pretend it was %c with argument ch */
1032 			cp = buf;
1033 			*cp = ch;
1034 			size = 1;
1035 			sign = '\0';
1036 			break;
1037 		}
1038 
1039 		/*
1040 		 * All reasonable formats wind up here.  At this point, `cp'
1041 		 * points to a string which (if not flags&LADJUST) should be
1042 		 * padded out to `width' places.  If flags&ZEROPAD, it should
1043 		 * first be prefixed by any sign or other prefix; otherwise,
1044 		 * it should be blank padded before the prefix is emitted.
1045 		 * After any left-hand padding and prefixing, emit zeroes
1046 		 * required by a decimal [diouxX] precision, then print the
1047 		 * string proper, then emit zeroes required by any leftover
1048 		 * floating precision; finally, if LADJUST, pad with blanks.
1049 		 *
1050 		 * Compute actual size, so we know how much to pad.
1051 		 * size excludes decimal prec; realsz includes it.
1052 		 */
1053 		realsz = dprec > size ? dprec : size;
1054 		if (sign)
1055 			realsz++;
1056 		else if (flags & HEXPREFIX)
1057 			realsz+= 2;
1058 
1059 		/* right-adjusting blank padding */
1060 		if ((flags & (LADJUST|ZEROPAD)) == 0) {
1061 			n = width - realsz;
1062 			while (n-- > 0)
1063 				KPRINTF_PUTCHAR(' ');
1064 		}
1065 
1066 		/* prefix */
1067 		if (sign) {
1068 			KPRINTF_PUTCHAR(sign);
1069 		} else if (flags & HEXPREFIX) {
1070 			KPRINTF_PUTCHAR('0');
1071 			KPRINTF_PUTCHAR(ch);
1072 		}
1073 
1074 		/* right-adjusting zero padding */
1075 		if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) {
1076 			n = width - realsz;
1077 			while (n-- > 0)
1078 				KPRINTF_PUTCHAR('0');
1079 		}
1080 
1081 		/* leading zeroes from decimal precision */
1082 		n = dprec - size;
1083 		while (n-- > 0)
1084 			KPRINTF_PUTCHAR('0');
1085 
1086 		/* the string or number proper */
1087 		while (size--)
1088 			KPRINTF_PUTCHAR(*cp++);
1089 		/* left-adjusting padding (always blank) */
1090 		if (flags & LADJUST) {
1091 			n = width - realsz;
1092 			while (n-- > 0)
1093 				KPRINTF_PUTCHAR(' ');
1094 		}
1095 	}
1096 
1097 done:
1098 	if ((oflags & TOBUFONLY) && (vp != NULL))
1099 		*(char **)vp = sbuf;
1100 overflow:
1101 	return (ret);
1102 	/* NOTREACHED */
1103 }
1104 
1105 #if __GNUC_PREREQ__(2,96)
1106 /*
1107  * XXX - these functions shouldn't be in the kernel, but gcc 3.X feels like
1108  *       translating some printf calls to puts and since it doesn't seem
1109  *       possible to just turn off parts of those optimizations (some of
1110  *       them are really useful, we have to provide a dummy puts and putchar
1111  *	 that are wrappers around printf.
1112  */
1113 int	puts(const char *);
1114 int	putchar(int c);
1115 
1116 int
1117 puts(const char *str)
1118 {
1119 	printf("%s\n", str);
1120 
1121 	return (0);
1122 }
1123 
1124 int
1125 putchar(int c)
1126 {
1127 	printf("%c", c);
1128 
1129 	return (c);
1130 }
1131 
1132 
1133 #endif
1134