xref: /openbsd-src/sys/kern/subr_prf.c (revision 0b7734b3d77bb9b21afec6f4621cae6c805dbd45)
1 /*	$OpenBSD: subr_prf.c,v 1.87 2016/05/17 23:43:47 bluhm Exp $	*/
2 /*	$NetBSD: subr_prf.c,v 1.45 1997/10/24 18:14:25 chuck Exp $	*/
3 
4 /*-
5  * Copyright (c) 1986, 1988, 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  * (c) UNIX System Laboratories, Inc.
8  * All or some portions of this file are derived from material licensed
9  * to the University of California by American Telephone and Telegraph
10  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11  * the permission of UNIX System Laboratories, Inc.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)subr_prf.c	8.3 (Berkeley) 1/21/94
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/conf.h>
43 #include <sys/reboot.h>
44 #include <sys/msgbuf.h>
45 #include <sys/proc.h>
46 #include <sys/ioctl.h>
47 #include <sys/vnode.h>
48 #include <sys/file.h>
49 #include <sys/tty.h>
50 #include <sys/tprintf.h>
51 #include <sys/syslog.h>
52 #include <sys/malloc.h>
53 #include <sys/pool.h>
54 #include <sys/mutex.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 #endif
67 #ifdef DDB
68 #include <ddb/db_output.h>	/* db_printf, db_putchar prototypes */
69 #include <ddb/db_var.h>		/* db_log, db_radix */
70 #endif
71 
72 
73 /*
74  * defines
75  */
76 
77 /* flags for kprintf */
78 #define TOCONS		0x01	/* to the console */
79 #define TOTTY		0x02	/* to the process' tty */
80 #define TOLOG		0x04	/* to the kernel message buffer */
81 #define TOBUFONLY	0x08	/* to the buffer (only) [for snprintf] */
82 #define TODDB		0x10	/* to ddb console */
83 #define TOCOUNT		0x20	/* act like [v]snprintf */
84 
85 /* max size buffer kprintf needs to print quad_t [size in base 8 + \0] */
86 #define KPRINTF_BUFSIZE		(sizeof(quad_t) * NBBY / 3 + 2)
87 
88 
89 /*
90  * local prototypes
91  */
92 
93 int	 kprintf(const char *, int, void *, char *, va_list);
94 void	 kputchar(int, int, struct tty *);
95 
96 struct mutex kprintf_mutex = MUTEX_INITIALIZER(IPL_HIGH);
97 
98 /*
99  * globals
100  */
101 
102 extern	int log_open;	/* subr_log: is /dev/klog open? */
103 const	char *panicstr; /* arg to first call to panic (used as a flag
104 			   to indicate that panic has already been called). */
105 #ifdef DDB
106 /*
107  * Enter ddb on panic.
108  */
109 int	db_panic = 1;
110 
111 /*
112  * db_console controls if we can be able to enter ddb by a special key
113  * combination (machine dependent).
114  * If DDB_SAFE_CONSOLE is defined in the kernel configuration it allows
115  * to break into console during boot. It's _really_ useful when debugging
116  * some things in the kernel that can cause init(8) to crash.
117  */
118 #ifdef DDB_SAFE_CONSOLE
119 int	db_console = 1;
120 #else
121 int	db_console = 0;
122 #endif
123 
124 /*
125  * flag to indicate if we are currently in ddb (on some processor)
126  */
127 int db_is_active;
128 #endif
129 
130 /*
131  * panic on spl assertion failure?
132  */
133 int splassert_ctl = 1;
134 
135 /*
136  * v_putc: routine to putc on virtual console
137  *
138  * the v_putc pointer can be used to redirect the console cnputc elsewhere
139  * [e.g. to a "virtual console"].
140  */
141 
142 void (*v_putc)(int) = cnputc;	/* start with cnputc (normal cons) */
143 
144 
145 /*
146  * functions
147  */
148 
149 /*
150  *	Partial support (the failure case) of the assertion facility
151  *	commonly found in userland.
152  */
153 void
154 __assert(const char *t, const char *f, int l, const char *e)
155 {
156 
157 	panic(__KASSERTSTR, t, e, f, l);
158 }
159 
160 /*
161  * tablefull: warn that a system table is full
162  */
163 
164 void
165 tablefull(const char *tab)
166 {
167 	log(LOG_ERR, "%s: table is full\n", tab);
168 }
169 
170 /*
171  * panic: handle an unresolvable fatal error
172  *
173  * prints "panic: <message>" and reboots.   if called twice (i.e. recursive
174  * call) we avoid trying to sync the disk and just reboot (to avoid
175  * recursive panics).
176  */
177 
178 void
179 panic(const char *fmt, ...)
180 {
181 	static char panicbuf[512];
182 	int bootopt;
183 	va_list ap;
184 
185 	/* do not trigger assertions, we know that we are inconsistent */
186 	splassert_ctl = 0;
187 
188 	bootopt = RB_AUTOBOOT | RB_DUMP;
189 	va_start(ap, fmt);
190 	if (panicstr)
191 		bootopt |= RB_NOSYNC;
192 	else {
193 		vsnprintf(panicbuf, sizeof panicbuf, fmt, ap);
194 		panicstr = panicbuf;
195 	}
196 	va_end(ap);
197 
198 	printf("panic: ");
199 	va_start(ap, fmt);
200 	vprintf(fmt, ap);
201 	printf("\n");
202 	va_end(ap);
203 
204 #ifdef KGDB
205 	kgdb_panic();
206 #endif
207 #ifdef KADB
208 	if (boothowto & RB_KDB)
209 		kdbpanic();
210 #endif
211 #ifdef DDB
212 	if (db_panic)
213 		Debugger();
214 	else
215 		db_stack_dump();
216 #endif
217 	reboot(bootopt);
218 	/* NOTREACHED */
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(msgbufp, 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 retval;
479 
480 	va_start(ap, fmt);
481 	retval = db_vprintf(fmt, ap);
482 	va_end(ap);
483 	return(retval);
484 }
485 
486 int
487 db_vprintf(const char *fmt, va_list ap)
488 {
489 	int flags;
490 
491 	flags = TODDB;
492 	if (db_log)
493 		flags |= TOLOG;
494 	return (kprintf(fmt, flags, NULL, NULL, ap));
495 }
496 #endif /* DDB */
497 
498 
499 /*
500  * normal kernel printf functions: printf, vprintf, snprintf
501  */
502 
503 /*
504  * printf: print a message to the console and the log
505  */
506 int
507 printf(const char *fmt, ...)
508 {
509 	va_list ap;
510 	int retval;
511 
512 	mtx_enter(&kprintf_mutex);
513 
514 	va_start(ap, fmt);
515 	retval = kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap);
516 	va_end(ap);
517 	if (!panicstr)
518 		logwakeup();
519 
520 	mtx_leave(&kprintf_mutex);
521 
522 	return(retval);
523 }
524 
525 /*
526  * vprintf: print a message to the console and the log [already have a
527  *	va_list]
528  */
529 
530 int
531 vprintf(const char *fmt, va_list ap)
532 {
533 	int retval;
534 
535 	mtx_enter(&kprintf_mutex);
536 
537 	retval = kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap);
538 	if (!panicstr)
539 		logwakeup();
540 
541 	mtx_leave(&kprintf_mutex);
542 
543 	return (retval);
544 }
545 
546 /*
547  * snprintf: print a message to a buffer
548  */
549 int
550 snprintf(char *buf, size_t size, const char *fmt, ...)
551 {
552 	int retval;
553 	va_list ap;
554 	char *p;
555 
556 	p = buf + size - 1;
557 	if (size < 1)
558 		p = buf;
559 	va_start(ap, fmt);
560 	retval = kprintf(fmt, TOBUFONLY | TOCOUNT, &p, buf, ap);
561 	va_end(ap);
562 	if (size > 0)
563 		*(p) = 0;	/* null terminate */
564 	return(retval);
565 }
566 
567 /*
568  * vsnprintf: print a message to a buffer [already have va_alist]
569  */
570 int
571 vsnprintf(char *buf, size_t size, const char *fmt, va_list ap)
572 {
573 	int retval;
574 	char *p;
575 
576 	p = buf + size - 1;
577 	if (size < 1)
578 		p = buf;
579 	retval = kprintf(fmt, TOBUFONLY | TOCOUNT, &p, buf, ap);
580 	if (size > 0)
581 		*(p) = 0;	/* null terminate */
582 	return(retval);
583 }
584 
585 /*
586  * kprintf: scaled down version of printf(3).
587  *
588  * this version based on vfprintf() from libc which was derived from
589  * software contributed to Berkeley by Chris Torek.
590  *
591  * The additional format %b is supported to decode error registers.
592  * Its usage is:
593  *
594  *	printf("reg=%b\n", regval, "<base><arg>*");
595  *
596  * where <base> is the output base expressed as a control character, e.g.
597  * \10 gives octal; \20 gives hex.  Each arg is a sequence of characters,
598  * the first of which gives the bit number to be inspected (origin 1), and
599  * the next characters (up to a control character, i.e. a character <= 32),
600  * give the name of the register.  Thus:
601  *
602  *	kprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
603  *
604  * would produce output:
605  *
606  *	reg=3<BITTWO,BITONE>
607  *
608  * To support larger integers (> 32 bits), %b formatting will also accept
609  * control characters in the region 0x80 - 0xff.  0x80 refers to bit 0,
610  * 0x81 refers to bit 1, and so on.  The equivalent string to the above is:
611  *
612  *	kprintf("reg=%b\n", 3, "\10\201BITTWO\200BITONE\n");
613  *
614  * and would produce the same output.
615  *
616  * Like the rest of printf, %b can be prefixed to handle various size
617  * modifiers, eg. %b is for "int", %lb is for "long", and %llb supports
618  * "long long".
619  *
620  * This code is large and complicated...
621  */
622 
623 /*
624  * macros for converting digits to letters and vice versa
625  */
626 #define	to_digit(c)	((c) - '0')
627 #define is_digit(c)	((unsigned)to_digit(c) <= 9)
628 #define	to_char(n)	((n) + '0')
629 
630 /*
631  * flags used during conversion.
632  */
633 #define	ALT		0x001		/* alternate form */
634 #define	HEXPREFIX	0x002		/* add 0x or 0X prefix */
635 #define	LADJUST		0x004		/* left adjustment */
636 #define	LONGDBL		0x008		/* long double; unimplemented */
637 #define	LONGINT		0x010		/* long integer */
638 #define	QUADINT		0x020		/* quad integer */
639 #define	SHORTINT	0x040		/* short integer */
640 #define	ZEROPAD		0x080		/* zero (as opposed to blank) pad */
641 #define FPT		0x100		/* Floating point number */
642 #define SIZEINT		0x200		/* (signed) size_t */
643 
644 	/*
645 	 * To extend shorts properly, we need both signed and unsigned
646 	 * argument extraction methods.
647 	 */
648 #define	SARG() \
649 	(flags&QUADINT ? va_arg(ap, quad_t) : \
650 	    flags&LONGINT ? va_arg(ap, long) : \
651 	    flags&SIZEINT ? va_arg(ap, ssize_t) : \
652 	    flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
653 	    (long)va_arg(ap, int))
654 #define	UARG() \
655 	(flags&QUADINT ? va_arg(ap, u_quad_t) : \
656 	    flags&LONGINT ? va_arg(ap, u_long) : \
657 	    flags&SIZEINT ? va_arg(ap, size_t) : \
658 	    flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
659 	    (u_long)va_arg(ap, u_int))
660 
661 #define KPRINTF_PUTCHAR(C) do {					\
662 	int chr = (C);							\
663 	ret += 1;							\
664 	if (oflags & TOBUFONLY) {					\
665 		if ((vp != NULL) && (sbuf == tailp)) {			\
666 			if (!(oflags & TOCOUNT))				\
667 				goto overflow;				\
668 		} else							\
669 			*sbuf++ = chr;					\
670 	} else {							\
671 		kputchar(chr, oflags, (struct tty *)vp);			\
672 	}								\
673 } while(0)
674 
675 int
676 kprintf(const char *fmt0, int oflags, void *vp, char *sbuf, va_list ap)
677 {
678 	char *fmt;		/* format string */
679 	int ch;			/* character from fmt */
680 	int n;			/* handy integer (short term usage) */
681 	char *cp = NULL;	/* handy char pointer (short term usage) */
682 	int flags;		/* flags as above */
683 	int ret;		/* return value accumulator */
684 	int width;		/* width from format (%8d), or 0 */
685 	int prec;		/* precision from format (%.3d), or -1 */
686 	char sign;		/* sign prefix (' ', '+', '-', or \0) */
687 
688 	u_quad_t _uquad;	/* integer arguments %[diouxX] */
689 	enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
690 	int dprec;		/* a copy of prec if [diouxX], 0 otherwise */
691 	int realsz;		/* field size expanded by dprec */
692 	int size = 0;		/* size of converted field or string */
693 	char *xdigs = NULL;	/* digits for [xX] conversion */
694 	char buf[KPRINTF_BUFSIZE]; /* space for %c, %[diouxX] */
695 	char *tailp = NULL;	/* tail pointer for snprintf */
696 
697 	if ((oflags & TOBUFONLY) && (vp != NULL))
698 		tailp = *(char **)vp;
699 
700 	fmt = (char *)fmt0;
701 	ret = 0;
702 
703 	/*
704 	 * Scan the format for conversions (`%' character).
705 	 */
706 	for (;;) {
707 		while (*fmt != '%' && *fmt) {
708 			KPRINTF_PUTCHAR(*fmt++);
709 		}
710 		if (*fmt == 0)
711 			goto done;
712 
713 		fmt++;		/* skip over '%' */
714 
715 		flags = 0;
716 		dprec = 0;
717 		width = 0;
718 		prec = -1;
719 		sign = '\0';
720 
721 rflag:		ch = *fmt++;
722 reswitch:	switch (ch) {
723 		/* XXX: non-standard '%b' format */
724 		case 'b': {
725 			char *b, *z;
726 			int tmp;
727 			_uquad = UARG();
728 			b = va_arg(ap, char *);
729 			if (*b == 8)
730 				snprintf(buf, sizeof buf, "%llo", _uquad);
731 			else if (*b == 10)
732 				snprintf(buf, sizeof buf, "%lld", _uquad);
733 			else if (*b == 16)
734 				snprintf(buf, sizeof buf, "%llx", _uquad);
735 			else
736 				break;
737 			b++;
738 
739 			z = buf;
740 			while (*z) {
741 				KPRINTF_PUTCHAR(*z++);
742 			}
743 
744 			if (_uquad) {
745 				tmp = 0;
746 				while ((n = *b++) != 0) {
747 					if (n & 0x80)
748 						n &= 0x7f;
749 					else if (n <= ' ')
750 						n = n - 1;
751 					if (_uquad & (1LL << n)) {
752 						KPRINTF_PUTCHAR(tmp ? ',':'<');
753 						while (*b > ' ' &&
754 						    (*b & 0x80) == 0) {
755 							KPRINTF_PUTCHAR(*b);
756 							b++;
757 						}
758 						tmp = 1;
759 					} else {
760 						while (*b > ' ' &&
761 						    (*b & 0x80) == 0)
762 							b++;
763 					}
764 				}
765 				if (tmp) {
766 					KPRINTF_PUTCHAR('>');
767 				}
768 			}
769 			continue;	/* no output */
770 		}
771 
772 		case ' ':
773 			/*
774 			 * ``If the space and + flags both appear, the space
775 			 * flag will be ignored.''
776 			 *	-- ANSI X3J11
777 			 */
778 			if (!sign)
779 				sign = ' ';
780 			goto rflag;
781 		case '#':
782 			flags |= ALT;
783 			goto rflag;
784 		case '*':
785 			/*
786 			 * ``A negative field width argument is taken as a
787 			 * - flag followed by a positive field width.''
788 			 *	-- ANSI X3J11
789 			 * They don't exclude field widths read from args.
790 			 */
791 			if ((width = va_arg(ap, int)) >= 0)
792 				goto rflag;
793 			width = -width;
794 			/* FALLTHROUGH */
795 		case '-':
796 			flags |= LADJUST;
797 			goto rflag;
798 		case '+':
799 			sign = '+';
800 			goto rflag;
801 		case '.':
802 			if ((ch = *fmt++) == '*') {
803 				n = va_arg(ap, int);
804 				prec = n < 0 ? -1 : n;
805 				goto rflag;
806 			}
807 			n = 0;
808 			while (is_digit(ch)) {
809 				n = 10 * n + to_digit(ch);
810 				ch = *fmt++;
811 			}
812 			prec = n < 0 ? -1 : n;
813 			goto reswitch;
814 		case '0':
815 			/*
816 			 * ``Note that 0 is taken as a flag, not as the
817 			 * beginning of a field width.''
818 			 *	-- ANSI X3J11
819 			 */
820 			flags |= ZEROPAD;
821 			goto rflag;
822 		case '1': case '2': case '3': case '4':
823 		case '5': case '6': case '7': case '8': case '9':
824 			n = 0;
825 			do {
826 				n = 10 * n + to_digit(ch);
827 				ch = *fmt++;
828 			} while (is_digit(ch));
829 			width = n;
830 			goto reswitch;
831 		case 'h':
832 			flags |= SHORTINT;
833 			goto rflag;
834 		case 'l':
835 			if (*fmt == 'l') {
836 				fmt++;
837 				flags |= QUADINT;
838 			} else {
839 				flags |= LONGINT;
840 			}
841 			goto rflag;
842 		case 'q':
843 			flags |= QUADINT;
844 			goto rflag;
845 		case 'z':
846 			flags |= SIZEINT;
847 			goto rflag;
848 		case 'c':
849 			*(cp = buf) = va_arg(ap, int);
850 			size = 1;
851 			sign = '\0';
852 			break;
853 		case 't':
854 			/* ptrdiff_t */
855 			/* FALLTHROUGH */
856 		case 'D':
857 			flags |= LONGINT;
858 			/*FALLTHROUGH*/
859 		case 'd':
860 		case 'i':
861 			_uquad = SARG();
862 			if ((quad_t)_uquad < 0) {
863 				_uquad = -_uquad;
864 				sign = '-';
865 			}
866 			base = DEC;
867 			goto number;
868 		case 'n':
869 			/* %n is unsupported in the kernel; just skip it */
870 			if (flags & QUADINT)
871 				(void)va_arg(ap, quad_t *);
872 			else if (flags & LONGINT)
873 				(void)va_arg(ap, long *);
874 			else if (flags & SHORTINT)
875 				(void)va_arg(ap, short *);
876 			else if (flags & SIZEINT)
877 				(void)va_arg(ap, ssize_t *);
878 			else
879 				(void)va_arg(ap, int *);
880 			continue;	/* no output */
881 		case 'O':
882 			flags |= LONGINT;
883 			/*FALLTHROUGH*/
884 		case 'o':
885 			_uquad = UARG();
886 			base = OCT;
887 			goto nosign;
888 		case 'p':
889 			/*
890 			 * ``The argument shall be a pointer to void.  The
891 			 * value of the pointer is converted to a sequence
892 			 * of printable characters, in an implementation-
893 			 * defined manner.''
894 			 *	-- ANSI X3J11
895 			 */
896 			_uquad = (u_long)va_arg(ap, void *);
897 			base = HEX;
898 			xdigs = "0123456789abcdef";
899 			flags |= HEXPREFIX;
900 			ch = 'x';
901 			goto nosign;
902 		case 's':
903 			if ((cp = va_arg(ap, char *)) == NULL)
904 				cp = "(null)";
905 			if (prec >= 0) {
906 				/*
907 				 * can't use strlen; can only look for the
908 				 * NUL in the first `prec' characters, and
909 				 * strlen() will go further.
910 				 */
911 				char *p = memchr(cp, 0, prec);
912 
913 				if (p != NULL) {
914 					size = p - cp;
915 					if (size > prec)
916 						size = prec;
917 				} else
918 					size = prec;
919 			} else
920 				size = strlen(cp);
921 			sign = '\0';
922 			break;
923 		case 'U':
924 			flags |= LONGINT;
925 			/*FALLTHROUGH*/
926 		case 'u':
927 			_uquad = UARG();
928 			base = DEC;
929 			goto nosign;
930 		case 'X':
931 			xdigs = "0123456789ABCDEF";
932 			goto hex;
933 		case 'x':
934 			xdigs = "0123456789abcdef";
935 hex:			_uquad = UARG();
936 			base = HEX;
937 			/* leading 0x/X only if non-zero */
938 			if (flags & ALT && _uquad != 0)
939 				flags |= HEXPREFIX;
940 
941 			/* unsigned conversions */
942 nosign:			sign = '\0';
943 			/*
944 			 * ``... diouXx conversions ... if a precision is
945 			 * specified, the 0 flag will be ignored.''
946 			 *	-- ANSI X3J11
947 			 */
948 number:			if ((dprec = prec) >= 0)
949 				flags &= ~ZEROPAD;
950 
951 			/*
952 			 * ``The result of converting a zero value with an
953 			 * explicit precision of zero is no characters.''
954 			 *	-- ANSI X3J11
955 			 */
956 			cp = buf + KPRINTF_BUFSIZE;
957 			if (_uquad != 0 || prec != 0) {
958 				/*
959 				 * Unsigned mod is hard, and unsigned mod
960 				 * by a constant is easier than that by
961 				 * a variable; hence this switch.
962 				 */
963 				switch (base) {
964 				case OCT:
965 					do {
966 						*--cp = to_char(_uquad & 7);
967 						_uquad >>= 3;
968 					} while (_uquad);
969 					/* handle octal leading 0 */
970 					if (flags & ALT && *cp != '0')
971 						*--cp = '0';
972 					break;
973 
974 				case DEC:
975 					/* many numbers are 1 digit */
976 					while (_uquad >= 10) {
977 						*--cp = to_char(_uquad % 10);
978 						_uquad /= 10;
979 					}
980 					*--cp = to_char(_uquad);
981 					break;
982 
983 				case HEX:
984 					do {
985 						*--cp = xdigs[_uquad & 15];
986 						_uquad >>= 4;
987 					} while (_uquad);
988 					break;
989 
990 				default:
991 					cp = "bug in kprintf: bad base";
992 					size = strlen(cp);
993 					goto skipsize;
994 				}
995 			}
996 			size = buf + KPRINTF_BUFSIZE - cp;
997 		skipsize:
998 			break;
999 		default:	/* "%?" prints ?, unless ? is NUL */
1000 			if (ch == '\0')
1001 				goto done;
1002 			/* pretend it was %c with argument ch */
1003 			cp = buf;
1004 			*cp = ch;
1005 			size = 1;
1006 			sign = '\0';
1007 			break;
1008 		}
1009 
1010 		/*
1011 		 * All reasonable formats wind up here.  At this point, `cp'
1012 		 * points to a string which (if not flags&LADJUST) should be
1013 		 * padded out to `width' places.  If flags&ZEROPAD, it should
1014 		 * first be prefixed by any sign or other prefix; otherwise,
1015 		 * it should be blank padded before the prefix is emitted.
1016 		 * After any left-hand padding and prefixing, emit zeroes
1017 		 * required by a decimal [diouxX] precision, then print the
1018 		 * string proper, then emit zeroes required by any leftover
1019 		 * floating precision; finally, if LADJUST, pad with blanks.
1020 		 *
1021 		 * Compute actual size, so we know how much to pad.
1022 		 * size excludes decimal prec; realsz includes it.
1023 		 */
1024 		realsz = dprec > size ? dprec : size;
1025 		if (sign)
1026 			realsz++;
1027 		else if (flags & HEXPREFIX)
1028 			realsz+= 2;
1029 
1030 		/* right-adjusting blank padding */
1031 		if ((flags & (LADJUST|ZEROPAD)) == 0) {
1032 			n = width - realsz;
1033 			while (n-- > 0)
1034 				KPRINTF_PUTCHAR(' ');
1035 		}
1036 
1037 		/* prefix */
1038 		if (sign) {
1039 			KPRINTF_PUTCHAR(sign);
1040 		} else if (flags & HEXPREFIX) {
1041 			KPRINTF_PUTCHAR('0');
1042 			KPRINTF_PUTCHAR(ch);
1043 		}
1044 
1045 		/* right-adjusting zero padding */
1046 		if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) {
1047 			n = width - realsz;
1048 			while (n-- > 0)
1049 				KPRINTF_PUTCHAR('0');
1050 		}
1051 
1052 		/* leading zeroes from decimal precision */
1053 		n = dprec - size;
1054 		while (n-- > 0)
1055 			KPRINTF_PUTCHAR('0');
1056 
1057 		/* the string or number proper */
1058 		while (size--)
1059 			KPRINTF_PUTCHAR(*cp++);
1060 		/* left-adjusting padding (always blank) */
1061 		if (flags & LADJUST) {
1062 			n = width - realsz;
1063 			while (n-- > 0)
1064 				KPRINTF_PUTCHAR(' ');
1065 		}
1066 	}
1067 
1068 done:
1069 	if ((oflags & TOBUFONLY) && (vp != NULL))
1070 		*(char **)vp = sbuf;
1071 overflow:
1072 	return (ret);
1073 	/* NOTREACHED */
1074 }
1075 
1076 #if __GNUC_PREREQ__(2,96)
1077 /*
1078  * XXX - these functions shouldn't be in the kernel, but gcc 3.X feels like
1079  *       translating some printf calls to puts and since it doesn't seem
1080  *       possible to just turn off parts of those optimizations (some of
1081  *       them are really useful), we have to provide a dummy puts and putchar
1082  *	 that are wrappers around printf.
1083  */
1084 int	puts(const char *);
1085 int	putchar(int c);
1086 
1087 int
1088 puts(const char *str)
1089 {
1090 	printf("%s\n", str);
1091 
1092 	return (0);
1093 }
1094 
1095 int
1096 putchar(int c)
1097 {
1098 	printf("%c", c);
1099 
1100 	return (c);
1101 }
1102 
1103 
1104 #endif
1105