xref: /openbsd/sys/kern/subr_prf.c (revision 3d8817e4)
1 /*	$OpenBSD: subr_prf.c,v 1.76 2011/04/03 16:46:19 drahn 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 #include <sys/mutex.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 #include <ddb/db_var.h>		/* db_log, db_radix */
72 #endif
73 
74 
75 /*
76  * defines
77  */
78 
79 /* flags for kprintf */
80 #define TOCONS		0x01	/* to the console */
81 #define TOTTY		0x02	/* to the process' tty */
82 #define TOLOG		0x04	/* to the kernel message buffer */
83 #define TOBUFONLY	0x08	/* to the buffer (only) [for snprintf] */
84 #define TODDB		0x10	/* to ddb console */
85 #define TOCOUNT		0x20	/* act like [v]snprintf */
86 
87 /* max size buffer kprintf needs to print quad_t [size in base 8 + \0] */
88 #define KPRINTF_BUFSIZE		(sizeof(quad_t) * NBBY / 3 + 2)
89 
90 
91 /*
92  * local prototypes
93  */
94 
95 int	 kprintf(const char *, int, void *, char *, va_list);
96 void	 kputchar(int, int, struct tty *);
97 
98 struct mutex kprintf_mutex = MUTEX_INITIALIZER(IPL_HIGH);
99 
100 /*
101  * globals
102  */
103 
104 extern struct	tty *constty;	/* pointer to console "window" tty */
105 extern	int log_open;	/* subr_log: is /dev/klog open? */
106 const	char *panicstr; /* arg to first call to panic (used as a flag
107 			   to indicate that panic has already been called). */
108 #ifdef DDB
109 /*
110  * Enter ddb on panic.
111  */
112 int	db_panic = 1;
113 
114 /*
115  * db_console controls if we can be able to enter ddb by a special key
116  * combination (machine dependent).
117  * If DDB_SAFE_CONSOLE is defined in the kernel configuration it allows
118  * to break into console during boot. It's _really_ useful when debugging
119  * some things in the kernel that can cause init(8) to crash.
120  */
121 #ifdef DDB_SAFE_CONSOLE
122 int	db_console = 1;
123 #else
124 int	db_console = 0;
125 #endif
126 
127 /*
128  * flag to indicate if we are currently in ddb (on some processor)
129  */
130 int db_is_active;
131 #endif
132 
133 /*
134  * panic on spl assertion failure?
135  */
136 int splassert_ctl = 1;
137 
138 /*
139  * v_putc: routine to putc on virtual console
140  *
141  * the v_putc pointer can be used to redirect the console cnputc elsewhere
142  * [e.g. to a "virtual console"].
143  */
144 
145 void (*v_putc)(int) = cnputc;	/* start with cnputc (normal cons) */
146 
147 
148 /*
149  * functions
150  */
151 
152 /*
153  *	Partial support (the failure case) of the assertion facility
154  *	commonly found in userland.
155  */
156 void
157 __assert(const char *t, const char *f, int l, const char *e)
158 {
159 
160 	panic("kernel %sassertion \"%s\" failed: file \"%s\", line %d",
161 		t, e, f, l);
162 }
163 
164 /*
165  * tablefull: warn that a system table is full
166  */
167 
168 void
169 tablefull(const char *tab)
170 {
171 	log(LOG_ERR, "%s: table is full\n", tab);
172 }
173 
174 /*
175  * panic: handle an unresolvable fatal error
176  *
177  * prints "panic: <message>" and reboots.   if called twice (i.e. recursive
178  * call) we avoid trying to sync the disk and just reboot (to avoid
179  * recursive panics).
180  */
181 
182 void
183 panic(const char *fmt, ...)
184 {
185 	static char panicbuf[512];
186 	int bootopt;
187 	va_list ap;
188 
189 	bootopt = RB_AUTOBOOT | RB_DUMP;
190 	va_start(ap, fmt);
191 	if (panicstr)
192 		bootopt |= RB_NOSYNC;
193 	else {
194 		vsnprintf(panicbuf, sizeof panicbuf, fmt, ap);
195 		panicstr = panicbuf;
196 	}
197 	va_end(ap);
198 
199 	printf("panic: ");
200 	va_start(ap, fmt);
201 	vprintf(fmt, ap);
202 	printf("\n");
203 	va_end(ap);
204 
205 #ifdef KGDB
206 	kgdb_panic();
207 #endif
208 #ifdef KADB
209 	if (boothowto & RB_KDB)
210 		kdbpanic();
211 #endif
212 #ifdef DDB
213 	if (db_panic)
214 		Debugger();
215 	else
216 		db_stack_dump();
217 #endif
218 	boot(bootopt);
219 }
220 
221 /*
222  * We print only the function name. The file name is usually very long and
223  * would eat tons of space in the kernel.
224  */
225 void
226 splassert_fail(int wantipl, int haveipl, const char *func)
227 {
228 
229 	printf("splassert: %s: want %d have %d\n", func, wantipl, haveipl);
230 	switch (splassert_ctl) {
231 	case 1:
232 		break;
233 	case 2:
234 #ifdef DDB
235 		db_stack_dump();
236 #endif
237 		break;
238 	case 3:
239 #ifdef DDB
240 		db_stack_dump();
241 		Debugger();
242 #endif
243 		break;
244 	default:
245 		panic("spl assertion failure in %s", func);
246 	}
247 }
248 
249 /*
250  * kernel logging functions: log, logpri, addlog
251  */
252 
253 /*
254  * log: write to the log buffer
255  *
256  * => will not sleep [so safe to call from interrupt]
257  * => will log to console if /dev/klog isn't open
258  */
259 
260 void
261 log(int level, const char *fmt, ...)
262 {
263 	int s;
264 	va_list ap;
265 
266 	s = splhigh();
267 	logpri(level);		/* log the level first */
268 	va_start(ap, fmt);
269 	kprintf(fmt, TOLOG, NULL, NULL, ap);
270 	va_end(ap);
271 	splx(s);
272 	if (!log_open) {
273 		va_start(ap, fmt);
274 		kprintf(fmt, TOCONS, NULL, NULL, ap);
275 		va_end(ap);
276 	}
277 	logwakeup();		/* wake up anyone waiting for log msgs */
278 }
279 
280 /*
281  * logpri: log the priority level to the klog
282  */
283 
284 void
285 logpri(int level)
286 {
287 	char *p;
288 	char snbuf[KPRINTF_BUFSIZE];
289 
290 	kputchar('<', TOLOG, NULL);
291 	snprintf(snbuf, sizeof snbuf, "%d", level);
292 	for (p = snbuf ; *p ; p++)
293 		kputchar(*p, TOLOG, NULL);
294 	kputchar('>', TOLOG, NULL);
295 }
296 
297 /*
298  * addlog: add info to previous log message
299  */
300 
301 int
302 addlog(const char *fmt, ...)
303 {
304 	int s;
305 	va_list ap;
306 
307 	s = splhigh();
308 	va_start(ap, fmt);
309 	kprintf(fmt, TOLOG, NULL, NULL, ap);
310 	va_end(ap);
311 	splx(s);
312 	if (!log_open) {
313 		va_start(ap, fmt);
314 		kprintf(fmt, TOCONS, NULL, NULL, ap);
315 		va_end(ap);
316 	}
317 	logwakeup();
318 	return(0);
319 }
320 
321 
322 /*
323  * kputchar: print a single character on console or user terminal.
324  *
325  * => if console, then the last MSGBUFS chars are saved in msgbuf
326  *	for inspection later (e.g. dmesg/syslog)
327  */
328 void
329 kputchar(int c, int flags, struct tty *tp)
330 {
331 	extern int msgbufmapped;
332 	int ddb_active = 0;
333 
334 #ifdef DDB
335 	ddb_active = db_is_active;
336 #endif
337 
338 	if (panicstr)
339 		constty = NULL;
340 
341 	if ((flags & TOCONS) && tp == NULL && constty && !ddb_active) {
342 		tp = constty;
343 		flags |= TOTTY;
344 	}
345 	if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 &&
346 	    (flags & TOCONS) && tp == constty)
347 		constty = NULL;
348 	if ((flags & TOLOG) &&
349 	    c != '\0' && c != '\r' && c != 0177 && msgbufmapped)
350 		msgbuf_putchar(c);
351 	if ((flags & TOCONS) && (constty == NULL || ddb_active) && c != '\0')
352 		(*v_putc)(c);
353 #ifdef DDB
354 	if (flags & TODDB)
355 		db_putchar(c);
356 #endif
357 }
358 
359 
360 /*
361  * uprintf: print to the controlling tty of the current process
362  *
363  * => we may block if the tty queue is full
364  * => no message is printed if the queue doesn't clear in a reasonable
365  *	time
366  */
367 
368 void
369 uprintf(const char *fmt, ...)
370 {
371 	struct process *pr = curproc->p_p;
372 	va_list ap;
373 
374 	if (pr->ps_flags & PS_CONTROLT && pr->ps_session->s_ttyvp) {
375 		va_start(ap, fmt);
376 		kprintf(fmt, TOTTY, pr->ps_session->s_ttyp, NULL, ap);
377 		va_end(ap);
378 	}
379 }
380 
381 #if defined(NFSSERVER) || defined(NFSCLIENT)
382 
383 /*
384  * tprintf functions: used to send messages to a specific process
385  *
386  * usage:
387  *   get a tpr_t handle on a process "p" by using "tprintf_open(p)"
388  *   use the handle when calling "tprintf"
389  *   when done, do a "tprintf_close" to drop the handle
390  */
391 
392 /*
393  * tprintf_open: get a tprintf handle on a process "p"
394  * XXX change s/proc/process
395  *
396  * => returns NULL if process can't be printed to
397  */
398 
399 tpr_t
400 tprintf_open(struct proc *p)
401 {
402 	struct process *pr = p->p_p;
403 
404 	if (pr->ps_flags & PS_CONTROLT && pr->ps_session->s_ttyvp) {
405 		SESSHOLD(pr->ps_session);
406 		return ((tpr_t)pr->ps_session);
407 	}
408 	return ((tpr_t) NULL);
409 }
410 
411 /*
412  * tprintf_close: dispose of a tprintf handle obtained with tprintf_open
413  */
414 
415 void
416 tprintf_close(tpr_t sess)
417 {
418 
419 	if (sess)
420 		SESSRELE((struct session *) sess);
421 }
422 
423 /*
424  * tprintf: given tprintf handle to a process [obtained with tprintf_open],
425  * send a message to the controlling tty for that process.
426  *
427  * => also sends message to /dev/klog
428  */
429 void
430 tprintf(tpr_t tpr, const char *fmt, ...)
431 {
432 	struct session *sess = (struct session *)tpr;
433 	struct tty *tp = NULL;
434 	int flags = TOLOG;
435 	va_list ap;
436 
437 	logpri(LOG_INFO);
438 	if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
439 		flags |= TOTTY;
440 		tp = sess->s_ttyp;
441 	}
442 	va_start(ap, fmt);
443 	kprintf(fmt, flags, tp, NULL, ap);
444 	va_end(ap);
445 	logwakeup();
446 }
447 
448 #endif	/* NFSSERVER || NFSCLIENT */
449 
450 
451 /*
452  * ttyprintf: send a message to a specific tty
453  *
454  * => should be used only by tty driver or anything that knows the
455  *	underlying tty will not be revoked(2)'d away.  [otherwise,
456  *	use tprintf]
457  */
458 void
459 ttyprintf(struct tty *tp, const char *fmt, ...)
460 {
461 	va_list ap;
462 
463 	va_start(ap, fmt);
464 	kprintf(fmt, TOTTY, tp, NULL, ap);
465 	va_end(ap);
466 }
467 
468 #ifdef DDB
469 
470 /*
471  * db_printf: printf for DDB (via db_putchar)
472  */
473 
474 int
475 db_printf(const char *fmt, ...)
476 {
477 	va_list ap;
478 	int flags, retval;
479 
480 	flags = TODDB;
481 	if (db_log)
482 		flags |= TOLOG;
483 	va_start(ap, fmt);
484 	retval = kprintf(fmt, flags, NULL, NULL, ap);
485 	va_end(ap);
486 	return(retval);
487 }
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 'D':
847 			flags |= LONGINT;
848 			/*FALLTHROUGH*/
849 		case 'd':
850 		case 'i':
851 			_uquad = SARG();
852 			if ((quad_t)_uquad < 0) {
853 				_uquad = -_uquad;
854 				sign = '-';
855 			}
856 			base = DEC;
857 			goto number;
858 		case 'n':
859 			if (flags & QUADINT)
860 				*va_arg(ap, quad_t *) = ret;
861 			else if (flags & LONGINT)
862 				*va_arg(ap, long *) = ret;
863 			else if (flags & SHORTINT)
864 				*va_arg(ap, short *) = ret;
865 			else if (flags & SIZEINT)
866 				*va_arg(ap, ssize_t *) = ret;
867 			else
868 				*va_arg(ap, int *) = ret;
869 			continue;	/* no output */
870 		case 'O':
871 			flags |= LONGINT;
872 			/*FALLTHROUGH*/
873 		case 'o':
874 			_uquad = UARG();
875 			base = OCT;
876 			goto nosign;
877 		case 'p':
878 			/*
879 			 * ``The argument shall be a pointer to void.  The
880 			 * value of the pointer is converted to a sequence
881 			 * of printable characters, in an implementation-
882 			 * defined manner.''
883 			 *	-- ANSI X3J11
884 			 */
885 			/* NOSTRICT */
886 			_uquad = (u_long)va_arg(ap, void *);
887 			base = HEX;
888 			xdigs = "0123456789abcdef";
889 			flags |= HEXPREFIX;
890 			ch = 'x';
891 			goto nosign;
892 		case 's':
893 			if ((cp = va_arg(ap, char *)) == NULL)
894 				cp = "(null)";
895 			if (prec >= 0) {
896 				/*
897 				 * can't use strlen; can only look for the
898 				 * NUL in the first `prec' characters, and
899 				 * strlen() will go further.
900 				 */
901 				char *p = memchr(cp, 0, prec);
902 
903 				if (p != NULL) {
904 					size = p - cp;
905 					if (size > prec)
906 						size = prec;
907 				} else
908 					size = prec;
909 			} else
910 				size = strlen(cp);
911 			sign = '\0';
912 			break;
913 		case 'U':
914 			flags |= LONGINT;
915 			/*FALLTHROUGH*/
916 		case 'u':
917 			_uquad = UARG();
918 			base = DEC;
919 			goto nosign;
920 		case 'X':
921 			xdigs = "0123456789ABCDEF";
922 			goto hex;
923 		case 'x':
924 			xdigs = "0123456789abcdef";
925 hex:			_uquad = UARG();
926 			base = HEX;
927 			/* leading 0x/X only if non-zero */
928 			if (flags & ALT && _uquad != 0)
929 				flags |= HEXPREFIX;
930 
931 			/* unsigned conversions */
932 nosign:			sign = '\0';
933 			/*
934 			 * ``... diouXx conversions ... if a precision is
935 			 * specified, the 0 flag will be ignored.''
936 			 *	-- ANSI X3J11
937 			 */
938 number:			if ((dprec = prec) >= 0)
939 				flags &= ~ZEROPAD;
940 
941 			/*
942 			 * ``The result of converting a zero value with an
943 			 * explicit precision of zero is no characters.''
944 			 *	-- ANSI X3J11
945 			 */
946 			cp = buf + KPRINTF_BUFSIZE;
947 			if (_uquad != 0 || prec != 0) {
948 				/*
949 				 * Unsigned mod is hard, and unsigned mod
950 				 * by a constant is easier than that by
951 				 * a variable; hence this switch.
952 				 */
953 				switch (base) {
954 				case OCT:
955 					do {
956 						*--cp = to_char(_uquad & 7);
957 						_uquad >>= 3;
958 					} while (_uquad);
959 					/* handle octal leading 0 */
960 					if (flags & ALT && *cp != '0')
961 						*--cp = '0';
962 					break;
963 
964 				case DEC:
965 					/* many numbers are 1 digit */
966 					while (_uquad >= 10) {
967 						*--cp = to_char(_uquad % 10);
968 						_uquad /= 10;
969 					}
970 					*--cp = to_char(_uquad);
971 					break;
972 
973 				case HEX:
974 					do {
975 						*--cp = xdigs[_uquad & 15];
976 						_uquad >>= 4;
977 					} while (_uquad);
978 					break;
979 
980 				default:
981 					cp = "bug in kprintf: bad base";
982 					size = strlen(cp);
983 					goto skipsize;
984 				}
985 			}
986 			size = buf + KPRINTF_BUFSIZE - cp;
987 		skipsize:
988 			break;
989 		default:	/* "%?" prints ?, unless ? is NUL */
990 			if (ch == '\0')
991 				goto done;
992 			/* pretend it was %c with argument ch */
993 			cp = buf;
994 			*cp = ch;
995 			size = 1;
996 			sign = '\0';
997 			break;
998 		}
999 
1000 		/*
1001 		 * All reasonable formats wind up here.  At this point, `cp'
1002 		 * points to a string which (if not flags&LADJUST) should be
1003 		 * padded out to `width' places.  If flags&ZEROPAD, it should
1004 		 * first be prefixed by any sign or other prefix; otherwise,
1005 		 * it should be blank padded before the prefix is emitted.
1006 		 * After any left-hand padding and prefixing, emit zeroes
1007 		 * required by a decimal [diouxX] precision, then print the
1008 		 * string proper, then emit zeroes required by any leftover
1009 		 * floating precision; finally, if LADJUST, pad with blanks.
1010 		 *
1011 		 * Compute actual size, so we know how much to pad.
1012 		 * size excludes decimal prec; realsz includes it.
1013 		 */
1014 		realsz = dprec > size ? dprec : size;
1015 		if (sign)
1016 			realsz++;
1017 		else if (flags & HEXPREFIX)
1018 			realsz+= 2;
1019 
1020 		/* right-adjusting blank padding */
1021 		if ((flags & (LADJUST|ZEROPAD)) == 0) {
1022 			n = width - realsz;
1023 			while (n-- > 0)
1024 				KPRINTF_PUTCHAR(' ');
1025 		}
1026 
1027 		/* prefix */
1028 		if (sign) {
1029 			KPRINTF_PUTCHAR(sign);
1030 		} else if (flags & HEXPREFIX) {
1031 			KPRINTF_PUTCHAR('0');
1032 			KPRINTF_PUTCHAR(ch);
1033 		}
1034 
1035 		/* right-adjusting zero padding */
1036 		if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) {
1037 			n = width - realsz;
1038 			while (n-- > 0)
1039 				KPRINTF_PUTCHAR('0');
1040 		}
1041 
1042 		/* leading zeroes from decimal precision */
1043 		n = dprec - size;
1044 		while (n-- > 0)
1045 			KPRINTF_PUTCHAR('0');
1046 
1047 		/* the string or number proper */
1048 		while (size--)
1049 			KPRINTF_PUTCHAR(*cp++);
1050 		/* left-adjusting padding (always blank) */
1051 		if (flags & LADJUST) {
1052 			n = width - realsz;
1053 			while (n-- > 0)
1054 				KPRINTF_PUTCHAR(' ');
1055 		}
1056 	}
1057 
1058 done:
1059 	if ((oflags & TOBUFONLY) && (vp != NULL))
1060 		*(char **)vp = sbuf;
1061 overflow:
1062 	return (ret);
1063 	/* NOTREACHED */
1064 }
1065 
1066 #if __GNUC_PREREQ__(2,96)
1067 /*
1068  * XXX - these functions shouldn't be in the kernel, but gcc 3.X feels like
1069  *       translating some printf calls to puts and since it doesn't seem
1070  *       possible to just turn off parts of those optimizations (some of
1071  *       them are really useful), we have to provide a dummy puts and putchar
1072  *	 that are wrappers around printf.
1073  */
1074 int	puts(const char *);
1075 int	putchar(int c);
1076 
1077 int
1078 puts(const char *str)
1079 {
1080 	printf("%s\n", str);
1081 
1082 	return (0);
1083 }
1084 
1085 int
1086 putchar(int c)
1087 {
1088 	printf("%c", c);
1089 
1090 	return (c);
1091 }
1092 
1093 
1094 #endif
1095