Home | History | Annotate | Line # | Download | only in kern
subr_prf.c revision 1.37
      1  1.37   thorpej /*	$NetBSD: subr_prf.c,v 1.37 1996/11/13 06:06:05 thorpej Exp $	*/
      2  1.15       cgd 
      3  1.12       cgd /*-
      4  1.12       cgd  * Copyright (c) 1986, 1988, 1991, 1993
      5  1.12       cgd  *	The Regents of the University of California.  All rights reserved.
      6  1.12       cgd  * (c) UNIX System Laboratories, Inc.
      7  1.12       cgd  * All or some portions of this file are derived from material licensed
      8  1.12       cgd  * to the University of California by American Telephone and Telegraph
      9  1.12       cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  1.12       cgd  * the permission of UNIX System Laboratories, Inc.
     11  1.12       cgd  *
     12  1.12       cgd  * Redistribution and use in source and binary forms, with or without
     13  1.12       cgd  * modification, are permitted provided that the following conditions
     14  1.12       cgd  * are met:
     15  1.12       cgd  * 1. Redistributions of source code must retain the above copyright
     16  1.12       cgd  *    notice, this list of conditions and the following disclaimer.
     17  1.12       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     18  1.12       cgd  *    notice, this list of conditions and the following disclaimer in the
     19  1.12       cgd  *    documentation and/or other materials provided with the distribution.
     20  1.12       cgd  * 3. All advertising materials mentioning features or use of this software
     21  1.12       cgd  *    must display the following acknowledgement:
     22  1.12       cgd  *	This product includes software developed by the University of
     23  1.12       cgd  *	California, Berkeley and its contributors.
     24  1.12       cgd  * 4. Neither the name of the University nor the names of its contributors
     25  1.12       cgd  *    may be used to endorse or promote products derived from this software
     26  1.12       cgd  *    without specific prior written permission.
     27  1.12       cgd  *
     28  1.12       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  1.12       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  1.12       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  1.12       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  1.12       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  1.12       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  1.12       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  1.12       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  1.12       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  1.12       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  1.12       cgd  * SUCH DAMAGE.
     39  1.12       cgd  *
     40  1.15       cgd  *	@(#)subr_prf.c	8.3 (Berkeley) 1/21/94
     41  1.12       cgd  */
     42  1.12       cgd 
     43  1.12       cgd #include <sys/param.h>
     44  1.12       cgd #include <sys/systm.h>
     45  1.12       cgd #include <sys/buf.h>
     46  1.12       cgd #include <sys/conf.h>
     47  1.12       cgd #include <sys/reboot.h>
     48  1.12       cgd #include <sys/msgbuf.h>
     49  1.12       cgd #include <sys/proc.h>
     50  1.12       cgd #include <sys/ioctl.h>
     51  1.12       cgd #include <sys/vnode.h>
     52  1.12       cgd #include <sys/file.h>
     53  1.12       cgd #include <sys/tty.h>
     54  1.12       cgd #include <sys/tprintf.h>
     55  1.12       cgd #include <sys/syslog.h>
     56  1.12       cgd #include <sys/malloc.h>
     57  1.12       cgd 
     58  1.20  christos #include <dev/cons.h>
     59  1.20  christos 
     60  1.12       cgd /*
     61  1.12       cgd  * Note that stdarg.h and the ANSI style va_start macro is used for both
     62  1.12       cgd  * ANSI and traditional C compilers.
     63  1.35       gwr  * XXX: This requires that stdarg.h defines: va_alist, va_dcl
     64  1.12       cgd  */
     65  1.12       cgd #include <machine/stdarg.h>
     66  1.12       cgd 
     67  1.32        ws #include "ipkdb.h"
     68  1.28        ws 
     69  1.12       cgd #ifdef KADB
     70  1.12       cgd #include <machine/kdbparam.h>
     71  1.22  christos #endif
     72  1.22  christos #ifdef KGDB
     73  1.22  christos #include <machine/cpu.h>
     74  1.12       cgd #endif
     75  1.20  christos 
     76  1.12       cgd #define TOCONS	0x01
     77  1.12       cgd #define TOTTY	0x02
     78  1.12       cgd #define TOLOG	0x04
     79  1.12       cgd 
     80  1.37   thorpej /*
     81  1.37   thorpej  * This is the size of the buffer that should be passed to ksnprintn().
     82  1.37   thorpej  * It's the length of a long in base 8, plus NULL.
     83  1.37   thorpej  */
     84  1.37   thorpej #define KSNPRINTN_BUFSIZE	(sizeof(long) * NBBY / 3 + 2)
     85  1.37   thorpej 
     86  1.12       cgd struct	tty *constty;			/* pointer to console "window" tty */
     87  1.12       cgd 
     88  1.20  christos void	(*v_putc) __P((int)) = cnputc;	/* routine to putc on virtual console */
     89  1.12       cgd 
     90  1.20  christos static void putchar __P((int, int, struct tty *));
     91  1.37   thorpej static char *ksnprintn __P((u_long, int, int *, char *, size_t));
     92  1.30  christos void kprintf __P((const char *, int, struct tty *, va_list));
     93  1.12       cgd 
     94  1.14       cgd int consintr = 1;			/* Ok to handle console interrupts? */
     95  1.12       cgd 
     96  1.12       cgd /*
     97  1.14       cgd  * Variable panicstr contains argument to first call to panic; used as flag
     98  1.14       cgd  * to indicate that the kernel has already called panic.
     99  1.12       cgd  */
    100  1.14       cgd const char *panicstr;
    101  1.12       cgd 
    102  1.12       cgd /*
    103  1.12       cgd  * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
    104  1.12       cgd  * and then reboots.  If we are called twice, then we avoid trying to sync
    105  1.12       cgd  * the disks as this often leads to recursive panics.
    106  1.12       cgd  */
    107  1.14       cgd void
    108  1.12       cgd #ifdef __STDC__
    109  1.12       cgd panic(const char *fmt, ...)
    110  1.12       cgd #else
    111  1.14       cgd panic(fmt, va_alist)
    112  1.12       cgd 	char *fmt;
    113  1.20  christos 	va_dcl
    114  1.12       cgd #endif
    115  1.12       cgd {
    116  1.14       cgd 	int bootopt;
    117  1.12       cgd 	va_list ap;
    118  1.12       cgd 
    119  1.14       cgd 	bootopt = RB_AUTOBOOT | RB_DUMP;
    120  1.12       cgd 	if (panicstr)
    121  1.12       cgd 		bootopt |= RB_NOSYNC;
    122  1.12       cgd 	else
    123  1.12       cgd 		panicstr = fmt;
    124  1.12       cgd 
    125  1.12       cgd 	va_start(ap, fmt);
    126  1.31       cgd 	printf("panic: %:\n", fmt, ap);
    127  1.12       cgd 	va_end(ap);
    128  1.12       cgd 
    129  1.32        ws #if NIPKDB > 0
    130  1.32        ws 	ipkdb_panic();
    131  1.28        ws #endif
    132  1.12       cgd #ifdef KGDB
    133  1.12       cgd 	kgdb_panic();
    134  1.12       cgd #endif
    135  1.12       cgd #ifdef KADB
    136  1.14       cgd 	if (boothowto & RB_KDB)
    137  1.14       cgd 		kdbpanic();
    138  1.12       cgd #endif
    139  1.12       cgd #ifdef DDB
    140  1.12       cgd 	Debugger();
    141  1.12       cgd #endif
    142  1.26       mrg 	boot(bootopt, NULL);
    143  1.12       cgd }
    144  1.12       cgd 
    145  1.12       cgd /*
    146  1.12       cgd  * Warn that a system table is full.
    147  1.12       cgd  */
    148  1.12       cgd void
    149  1.12       cgd tablefull(tab)
    150  1.12       cgd 	const char *tab;
    151  1.12       cgd {
    152  1.12       cgd 
    153  1.12       cgd 	log(LOG_ERR, "%s: table is full\n", tab);
    154  1.12       cgd }
    155  1.12       cgd 
    156  1.12       cgd /*
    157  1.12       cgd  * Uprintf prints to the controlling terminal for the current process.
    158  1.12       cgd  * It may block if the tty queue is overfull.  No message is printed if
    159  1.12       cgd  * the queue does not clear in a reasonable time.
    160  1.12       cgd  */
    161  1.12       cgd void
    162  1.12       cgd #ifdef __STDC__
    163  1.12       cgd uprintf(const char *fmt, ...)
    164  1.12       cgd #else
    165  1.14       cgd uprintf(fmt, va_alist)
    166  1.12       cgd 	char *fmt;
    167  1.20  christos 	va_dcl
    168  1.12       cgd #endif
    169  1.12       cgd {
    170  1.12       cgd 	register struct proc *p = curproc;
    171  1.12       cgd 	va_list ap;
    172  1.12       cgd 
    173  1.12       cgd 	if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
    174  1.12       cgd 		va_start(ap, fmt);
    175  1.30  christos 		kprintf(fmt, TOTTY, p->p_session->s_ttyp, ap);
    176  1.12       cgd 		va_end(ap);
    177  1.12       cgd 	}
    178  1.12       cgd }
    179  1.12       cgd 
    180  1.12       cgd tpr_t
    181  1.12       cgd tprintf_open(p)
    182  1.12       cgd 	register struct proc *p;
    183  1.12       cgd {
    184  1.12       cgd 
    185  1.12       cgd 	if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
    186  1.12       cgd 		SESSHOLD(p->p_session);
    187  1.12       cgd 		return ((tpr_t) p->p_session);
    188  1.12       cgd 	}
    189  1.12       cgd 	return ((tpr_t) NULL);
    190  1.12       cgd }
    191  1.12       cgd 
    192  1.12       cgd void
    193  1.12       cgd tprintf_close(sess)
    194  1.12       cgd 	tpr_t sess;
    195  1.12       cgd {
    196  1.12       cgd 
    197  1.12       cgd 	if (sess)
    198  1.12       cgd 		SESSRELE((struct session *) sess);
    199  1.12       cgd }
    200  1.12       cgd 
    201  1.12       cgd /*
    202  1.12       cgd  * tprintf prints on the controlling terminal associated
    203  1.12       cgd  * with the given session.
    204  1.12       cgd  */
    205  1.12       cgd void
    206  1.12       cgd #ifdef __STDC__
    207  1.12       cgd tprintf(tpr_t tpr, const char *fmt, ...)
    208  1.12       cgd #else
    209  1.14       cgd tprintf(tpr, fmt, va_alist)
    210  1.12       cgd 	tpr_t tpr;
    211  1.12       cgd 	char *fmt;
    212  1.20  christos 	va_dcl
    213  1.12       cgd #endif
    214  1.12       cgd {
    215  1.12       cgd 	register struct session *sess = (struct session *)tpr;
    216  1.12       cgd 	struct tty *tp = NULL;
    217  1.12       cgd 	int flags = TOLOG;
    218  1.12       cgd 	va_list ap;
    219  1.12       cgd 
    220  1.12       cgd 	logpri(LOG_INFO);
    221  1.12       cgd 	if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
    222  1.12       cgd 		flags |= TOTTY;
    223  1.12       cgd 		tp = sess->s_ttyp;
    224  1.12       cgd 	}
    225  1.12       cgd 	va_start(ap, fmt);
    226  1.30  christos 	kprintf(fmt, flags, tp, ap);
    227  1.12       cgd 	va_end(ap);
    228  1.12       cgd 	logwakeup();
    229  1.12       cgd }
    230  1.12       cgd 
    231  1.12       cgd /*
    232  1.12       cgd  * Ttyprintf displays a message on a tty; it should be used only by
    233  1.12       cgd  * the tty driver, or anything that knows the underlying tty will not
    234  1.12       cgd  * be revoke(2)'d away.  Other callers should use tprintf.
    235  1.12       cgd  */
    236  1.12       cgd void
    237  1.12       cgd #ifdef __STDC__
    238  1.12       cgd ttyprintf(struct tty *tp, const char *fmt, ...)
    239  1.12       cgd #else
    240  1.14       cgd ttyprintf(tp, fmt, va_alist)
    241  1.12       cgd 	struct tty *tp;
    242  1.12       cgd 	char *fmt;
    243  1.20  christos 	va_dcl
    244  1.12       cgd #endif
    245  1.12       cgd {
    246  1.12       cgd 	va_list ap;
    247  1.12       cgd 
    248  1.12       cgd 	va_start(ap, fmt);
    249  1.30  christos 	kprintf(fmt, TOTTY, tp, ap);
    250  1.12       cgd 	va_end(ap);
    251  1.12       cgd }
    252  1.12       cgd 
    253  1.12       cgd extern	int log_open;
    254  1.12       cgd 
    255  1.12       cgd /*
    256  1.12       cgd  * Log writes to the log buffer, and guarantees not to sleep (so can be
    257  1.12       cgd  * called by interrupt routines).  If there is no process reading the
    258  1.12       cgd  * log yet, it writes to the console also.
    259  1.12       cgd  */
    260  1.12       cgd void
    261  1.12       cgd #ifdef __STDC__
    262  1.12       cgd log(int level, const char *fmt, ...)
    263  1.12       cgd #else
    264  1.14       cgd log(level, fmt, va_alist)
    265  1.12       cgd 	int level;
    266  1.12       cgd 	char *fmt;
    267  1.20  christos 	va_dcl
    268  1.12       cgd #endif
    269  1.12       cgd {
    270  1.12       cgd 	register int s;
    271  1.12       cgd 	va_list ap;
    272  1.12       cgd 
    273  1.12       cgd 	s = splhigh();
    274  1.12       cgd 	logpri(level);
    275  1.12       cgd 	va_start(ap, fmt);
    276  1.30  christos 	kprintf(fmt, TOLOG, NULL, ap);
    277  1.12       cgd 	splx(s);
    278  1.12       cgd 	va_end(ap);
    279  1.12       cgd 	if (!log_open) {
    280  1.12       cgd 		va_start(ap, fmt);
    281  1.30  christos 		kprintf(fmt, TOCONS, NULL, ap);
    282  1.12       cgd 		va_end(ap);
    283  1.12       cgd 	}
    284  1.12       cgd 	logwakeup();
    285  1.12       cgd }
    286  1.12       cgd 
    287  1.13   mycroft void
    288  1.12       cgd logpri(level)
    289  1.12       cgd 	int level;
    290  1.12       cgd {
    291  1.12       cgd 	register int ch;
    292  1.12       cgd 	register char *p;
    293  1.37   thorpej 	char snbuf[KSNPRINTN_BUFSIZE];
    294  1.12       cgd 
    295  1.12       cgd 	putchar('<', TOLOG, NULL);
    296  1.37   thorpej 	for (p = ksnprintn((u_long)level, 10, NULL, snbuf, sizeof(snbuf));
    297  1.37   thorpej 	    (ch = *p--) != 0;)
    298  1.12       cgd 		putchar(ch, TOLOG, NULL);
    299  1.12       cgd 	putchar('>', TOLOG, NULL);
    300  1.12       cgd }
    301  1.12       cgd 
    302  1.12       cgd void
    303  1.12       cgd #ifdef __STDC__
    304  1.12       cgd addlog(const char *fmt, ...)
    305  1.12       cgd #else
    306  1.14       cgd addlog(fmt, va_alist)
    307  1.12       cgd 	char *fmt;
    308  1.20  christos 	va_dcl
    309  1.12       cgd #endif
    310  1.12       cgd {
    311  1.12       cgd 	register int s;
    312  1.12       cgd 	va_list ap;
    313  1.12       cgd 
    314  1.12       cgd 	s = splhigh();
    315  1.12       cgd 	va_start(ap, fmt);
    316  1.30  christos 	kprintf(fmt, TOLOG, NULL, ap);
    317  1.12       cgd 	splx(s);
    318  1.12       cgd 	va_end(ap);
    319  1.12       cgd 	if (!log_open) {
    320  1.12       cgd 		va_start(ap, fmt);
    321  1.30  christos 		kprintf(fmt, TOCONS, NULL, ap);
    322  1.12       cgd 		va_end(ap);
    323  1.12       cgd 	}
    324  1.12       cgd 	logwakeup();
    325  1.12       cgd }
    326  1.12       cgd 
    327  1.12       cgd void
    328  1.12       cgd #ifdef __STDC__
    329  1.30  christos printf(const char *fmt, ...)
    330  1.12       cgd #else
    331  1.30  christos printf(fmt, va_alist)
    332  1.12       cgd 	char *fmt;
    333  1.20  christos 	va_dcl
    334  1.12       cgd #endif
    335  1.12       cgd {
    336  1.12       cgd 	va_list ap;
    337  1.12       cgd 	register int savintr;
    338  1.12       cgd 
    339  1.12       cgd 	savintr = consintr;		/* disable interrupts */
    340  1.12       cgd 	consintr = 0;
    341  1.12       cgd 	va_start(ap, fmt);
    342  1.30  christos 	kprintf(fmt, TOCONS | TOLOG, NULL, ap);
    343  1.12       cgd 	va_end(ap);
    344  1.12       cgd 	if (!panicstr)
    345  1.12       cgd 		logwakeup();
    346  1.12       cgd 	consintr = savintr;		/* reenable interrupts */
    347  1.12       cgd }
    348  1.12       cgd 
    349  1.12       cgd /*
    350  1.12       cgd  * Scaled down version of printf(3).
    351  1.12       cgd  *
    352  1.12       cgd  * Two additional formats:
    353  1.12       cgd  *
    354  1.12       cgd  * The format %b is supported to decode error registers.
    355  1.12       cgd  * Its usage is:
    356  1.12       cgd  *
    357  1.30  christos  *	printf("reg=%b\n", regval, "<base><arg>*");
    358  1.12       cgd  *
    359  1.12       cgd  * where <base> is the output base expressed as a control character, e.g.
    360  1.12       cgd  * \10 gives octal; \20 gives hex.  Each arg is a sequence of characters,
    361  1.12       cgd  * the first of which gives the bit number to be inspected (origin 1), and
    362  1.12       cgd  * the next characters (up to a control character, i.e. a character <= 32),
    363  1.12       cgd  * give the name of the register.  Thus:
    364  1.12       cgd  *
    365  1.30  christos  *	kprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
    366  1.12       cgd  *
    367  1.12       cgd  * would produce output:
    368  1.12       cgd  *
    369  1.12       cgd  *	reg=3<BITTWO,BITONE>
    370  1.12       cgd  *
    371  1.24  christos  * The format %: passes an additional format string and argument list
    372  1.14       cgd  * recursively.  Its usage is:
    373  1.12       cgd  *
    374  1.14       cgd  * fn(char *fmt, ...)
    375  1.12       cgd  * {
    376  1.12       cgd  *	va_list ap;
    377  1.12       cgd  *	va_start(ap, fmt);
    378  1.30  christos  *	printf("prefix: %: suffix\n", fmt, ap);
    379  1.12       cgd  *	va_end(ap);
    380  1.14       cgd  * }
    381  1.12       cgd  *
    382  1.12       cgd  * Space or zero padding and a field width are supported for the numeric
    383  1.12       cgd  * formats only.
    384  1.12       cgd  */
    385  1.12       cgd void
    386  1.30  christos kprintf(fmt, flags, tp, ap)
    387  1.12       cgd 	register const char *fmt;
    388  1.12       cgd 	int flags;
    389  1.12       cgd 	struct tty *tp;
    390  1.12       cgd 	va_list ap;
    391  1.12       cgd {
    392  1.14       cgd 	register char *p, *q;
    393  1.12       cgd 	register int ch, n;
    394  1.12       cgd 	u_long ul;
    395  1.12       cgd 	int base, lflag, tmp, width;
    396  1.37   thorpej 	char padc, snbuf[KSNPRINTN_BUFSIZE];
    397  1.12       cgd 
    398  1.12       cgd 	for (;;) {
    399  1.12       cgd 		padc = ' ';
    400  1.12       cgd 		width = 0;
    401  1.33       cgd 		while ((ch = *(const u_char *)fmt++) != '%') {
    402  1.12       cgd 			if (ch == '\0')
    403  1.12       cgd 				return;
    404  1.12       cgd 			putchar(ch, flags, tp);
    405  1.12       cgd 		}
    406  1.12       cgd 		lflag = 0;
    407  1.33       cgd reswitch:	switch (ch = *(const u_char *)fmt++) {
    408  1.12       cgd 		case '0':
    409  1.27  christos 		case '.':
    410  1.12       cgd 			padc = '0';
    411  1.12       cgd 			goto reswitch;
    412  1.12       cgd 		case '1': case '2': case '3': case '4':
    413  1.12       cgd 		case '5': case '6': case '7': case '8': case '9':
    414  1.12       cgd 			for (width = 0;; ++fmt) {
    415  1.12       cgd 				width = width * 10 + ch - '0';
    416  1.12       cgd 				ch = *fmt;
    417  1.12       cgd 				if (ch < '0' || ch > '9')
    418  1.12       cgd 					break;
    419  1.12       cgd 			}
    420  1.12       cgd 			goto reswitch;
    421  1.12       cgd 		case 'l':
    422  1.12       cgd 			lflag = 1;
    423  1.12       cgd 			goto reswitch;
    424  1.12       cgd 		case 'b':
    425  1.12       cgd 			ul = va_arg(ap, int);
    426  1.12       cgd 			p = va_arg(ap, char *);
    427  1.37   thorpej 			for (q = ksnprintn(ul, *p++, NULL, snbuf,
    428  1.37   thorpej 			    sizeof(snbuf)); (ch = *q--) != 0;)
    429  1.12       cgd 				putchar(ch, flags, tp);
    430  1.12       cgd 
    431  1.12       cgd 			if (!ul)
    432  1.12       cgd 				break;
    433  1.12       cgd 
    434  1.20  christos 			for (tmp = 0; (n = *p++) != 0;) {
    435  1.12       cgd 				if (ul & (1 << (n - 1))) {
    436  1.12       cgd 					putchar(tmp ? ',' : '<', flags, tp);
    437  1.12       cgd 					for (; (n = *p) > ' '; ++p)
    438  1.12       cgd 						putchar(n, flags, tp);
    439  1.12       cgd 					tmp = 1;
    440  1.12       cgd 				} else
    441  1.14       cgd 					for (; *p > ' '; ++p)
    442  1.14       cgd 						continue;
    443  1.12       cgd 			}
    444  1.12       cgd 			if (tmp)
    445  1.12       cgd 				putchar('>', flags, tp);
    446  1.12       cgd 			break;
    447  1.12       cgd 		case 'c':
    448  1.12       cgd 			putchar(va_arg(ap, int), flags, tp);
    449  1.12       cgd 			break;
    450  1.24  christos 		case ':':
    451  1.12       cgd 			p = va_arg(ap, char *);
    452  1.30  christos 			kprintf(p, flags, tp, va_arg(ap, va_list));
    453  1.12       cgd 			break;
    454  1.12       cgd 		case 's':
    455  1.16   mycroft 			if ((p = va_arg(ap, char *)) == NULL)
    456  1.16   mycroft 				p = "(null)";
    457  1.20  christos 			while ((ch = *p++) != 0)
    458  1.12       cgd 				putchar(ch, flags, tp);
    459  1.12       cgd 			break;
    460  1.12       cgd 		case 'd':
    461  1.12       cgd 			ul = lflag ? va_arg(ap, long) : va_arg(ap, int);
    462  1.12       cgd 			if ((long)ul < 0) {
    463  1.12       cgd 				putchar('-', flags, tp);
    464  1.12       cgd 				ul = -(long)ul;
    465  1.12       cgd 			}
    466  1.12       cgd 			base = 10;
    467  1.12       cgd 			goto number;
    468  1.12       cgd 		case 'o':
    469  1.12       cgd 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
    470  1.12       cgd 			base = 8;
    471  1.12       cgd 			goto number;
    472  1.12       cgd 		case 'u':
    473  1.12       cgd 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
    474  1.12       cgd 			base = 10;
    475  1.12       cgd 			goto number;
    476  1.17   mycroft 		case 'p':
    477  1.17   mycroft 			putchar('0', flags, tp);
    478  1.17   mycroft 			putchar('x', flags, tp);
    479  1.18   mycroft 			ul = (u_long)va_arg(ap, void *);
    480  1.18   mycroft 			base = 16;
    481  1.18   mycroft 			goto number;
    482  1.12       cgd 		case 'x':
    483  1.12       cgd 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
    484  1.12       cgd 			base = 16;
    485  1.37   thorpej number:			p = ksnprintn(ul, base, &tmp, snbuf, sizeof(snbuf));
    486  1.12       cgd 			if (width && (width -= tmp) > 0)
    487  1.12       cgd 				while (width--)
    488  1.12       cgd 					putchar(padc, flags, tp);
    489  1.20  christos 			while ((ch = *p--) != 0)
    490  1.12       cgd 				putchar(ch, flags, tp);
    491  1.12       cgd 			break;
    492  1.12       cgd 		default:
    493  1.12       cgd 			putchar('%', flags, tp);
    494  1.12       cgd 			if (lflag)
    495  1.12       cgd 				putchar('l', flags, tp);
    496  1.12       cgd 			/* FALLTHROUGH */
    497  1.12       cgd 		case '%':
    498  1.12       cgd 			putchar(ch, flags, tp);
    499  1.12       cgd 		}
    500  1.12       cgd 	}
    501  1.12       cgd }
    502  1.12       cgd 
    503  1.12       cgd /*
    504  1.12       cgd  * Print a character on console or users terminal.  If destination is
    505  1.12       cgd  * the console then the last MSGBUFS characters are saved in msgbuf for
    506  1.12       cgd  * inspection later.
    507  1.12       cgd  */
    508  1.12       cgd static void
    509  1.12       cgd putchar(c, flags, tp)
    510  1.12       cgd 	register int c;
    511  1.12       cgd 	int flags;
    512  1.12       cgd 	struct tty *tp;
    513  1.12       cgd {
    514  1.14       cgd 	extern int msgbufmapped;
    515  1.12       cgd 	register struct msgbuf *mbp;
    516  1.12       cgd 
    517  1.12       cgd 	if (panicstr)
    518  1.12       cgd 		constty = NULL;
    519  1.12       cgd 	if ((flags & TOCONS) && tp == NULL && constty) {
    520  1.12       cgd 		tp = constty;
    521  1.12       cgd 		flags |= TOTTY;
    522  1.12       cgd 	}
    523  1.12       cgd 	if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 &&
    524  1.12       cgd 	    (flags & TOCONS) && tp == constty)
    525  1.12       cgd 		constty = NULL;
    526  1.12       cgd 	if ((flags & TOLOG) &&
    527  1.12       cgd 	    c != '\0' && c != '\r' && c != 0177 && msgbufmapped) {
    528  1.12       cgd 		mbp = msgbufp;
    529  1.12       cgd 		if (mbp->msg_magic != MSG_MAGIC) {
    530  1.12       cgd 			bzero((caddr_t)mbp, sizeof(*mbp));
    531  1.12       cgd 			mbp->msg_magic = MSG_MAGIC;
    532  1.12       cgd 		}
    533  1.12       cgd 		mbp->msg_bufc[mbp->msg_bufx++] = c;
    534  1.12       cgd 		if (mbp->msg_bufx < 0 || mbp->msg_bufx >= MSG_BSIZE)
    535  1.12       cgd 			mbp->msg_bufx = 0;
    536  1.12       cgd 	}
    537  1.12       cgd 	if ((flags & TOCONS) && constty == NULL && c != '\0')
    538  1.12       cgd 		(*v_putc)(c);
    539  1.12       cgd }
    540  1.12       cgd 
    541  1.12       cgd /*
    542  1.12       cgd  * Scaled down version of sprintf(3).
    543  1.12       cgd  */
    544  1.20  christos int
    545  1.12       cgd #ifdef __STDC__
    546  1.30  christos sprintf(char *buf, const char *cfmt, ...)
    547  1.12       cgd #else
    548  1.30  christos sprintf(buf, cfmt, va_alist)
    549  1.34       gwr 	char *buf;
    550  1.36       gwr 	const char *cfmt;
    551  1.20  christos 	va_dcl
    552  1.12       cgd #endif
    553  1.12       cgd {
    554  1.36       gwr 	register const char *fmt = cfmt;
    555  1.12       cgd 	register char *p, *bp;
    556  1.12       cgd 	register int ch, base;
    557  1.12       cgd 	u_long ul;
    558  1.19       cgd 	int lflag, tmp, width;
    559  1.12       cgd 	va_list ap;
    560  1.37   thorpej 	char padc, snbuf[KSNPRINTN_BUFSIZE];
    561  1.12       cgd 
    562  1.12       cgd 	va_start(ap, cfmt);
    563  1.12       cgd 	for (bp = buf; ; ) {
    564  1.19       cgd 		padc = ' ';
    565  1.19       cgd 		width = 0;
    566  1.33       cgd 		while ((ch = *(const u_char *)fmt++) != '%')
    567  1.12       cgd 			if ((*bp++ = ch) == '\0')
    568  1.12       cgd 				return ((bp - buf) - 1);
    569  1.12       cgd 
    570  1.12       cgd 		lflag = 0;
    571  1.33       cgd reswitch:	switch (ch = *(const u_char *)fmt++) {
    572  1.19       cgd 		case '0':
    573  1.19       cgd 			padc = '0';
    574  1.19       cgd 			goto reswitch;
    575  1.19       cgd 		case '1': case '2': case '3': case '4':
    576  1.19       cgd 		case '5': case '6': case '7': case '8': case '9':
    577  1.19       cgd 			for (width = 0;; ++fmt) {
    578  1.19       cgd 				width = width * 10 + ch - '0';
    579  1.19       cgd 				ch = *fmt;
    580  1.19       cgd 				if (ch < '0' || ch > '9')
    581  1.19       cgd 					break;
    582  1.19       cgd 			}
    583  1.19       cgd 			goto reswitch;
    584  1.12       cgd 		case 'l':
    585  1.12       cgd 			lflag = 1;
    586  1.12       cgd 			goto reswitch;
    587  1.19       cgd 		/* case 'b': ... break; XXX */
    588  1.12       cgd 		case 'c':
    589  1.12       cgd 			*bp++ = va_arg(ap, int);
    590  1.12       cgd 			break;
    591  1.19       cgd 		/* case 'r': ... break; XXX */
    592  1.12       cgd 		case 's':
    593  1.12       cgd 			p = va_arg(ap, char *);
    594  1.20  christos 			while ((*bp++ = *p++) != 0)
    595  1.14       cgd 				continue;
    596  1.12       cgd 			--bp;
    597  1.12       cgd 			break;
    598  1.12       cgd 		case 'd':
    599  1.12       cgd 			ul = lflag ? va_arg(ap, long) : va_arg(ap, int);
    600  1.12       cgd 			if ((long)ul < 0) {
    601  1.12       cgd 				*bp++ = '-';
    602  1.12       cgd 				ul = -(long)ul;
    603  1.12       cgd 			}
    604  1.12       cgd 			base = 10;
    605  1.12       cgd 			goto number;
    606  1.12       cgd 			break;
    607  1.12       cgd 		case 'o':
    608  1.12       cgd 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
    609  1.12       cgd 			base = 8;
    610  1.12       cgd 			goto number;
    611  1.12       cgd 			break;
    612  1.12       cgd 		case 'u':
    613  1.12       cgd 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
    614  1.12       cgd 			base = 10;
    615  1.12       cgd 			goto number;
    616  1.12       cgd 			break;
    617  1.19       cgd 		case 'p':
    618  1.19       cgd 			*bp++ = '0';
    619  1.19       cgd 			*bp++ = 'x';
    620  1.19       cgd 			ul = (u_long)va_arg(ap, void *);
    621  1.19       cgd 			base = 16;
    622  1.19       cgd 			goto number;
    623  1.12       cgd 		case 'x':
    624  1.12       cgd 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
    625  1.12       cgd 			base = 16;
    626  1.37   thorpej number:			p = ksnprintn(ul, base, &tmp, snbuf, sizeof(snbuf));
    627  1.19       cgd 			if (width && (width -= tmp) > 0)
    628  1.19       cgd 				while (width--)
    629  1.19       cgd 					*bp++ = padc;
    630  1.20  christos 			while ((ch = *p--) != 0)
    631  1.12       cgd 				*bp++ = ch;
    632  1.12       cgd 			break;
    633  1.12       cgd 		default:
    634  1.12       cgd 			*bp++ = '%';
    635  1.12       cgd 			if (lflag)
    636  1.12       cgd 				*bp++ = 'l';
    637  1.12       cgd 			/* FALLTHROUGH */
    638  1.12       cgd 		case '%':
    639  1.12       cgd 			*bp++ = ch;
    640  1.12       cgd 		}
    641  1.12       cgd 	}
    642  1.12       cgd 	va_end(ap);
    643  1.12       cgd }
    644  1.12       cgd 
    645  1.12       cgd /*
    646  1.12       cgd  * Put a number (base <= 16) in a buffer in reverse order; return an
    647  1.12       cgd  * optional length and a pointer to the NULL terminated (preceded?)
    648  1.12       cgd  * buffer.
    649  1.12       cgd  */
    650  1.12       cgd static char *
    651  1.37   thorpej ksnprintn(ul, base, lenp, buf, buflen)
    652  1.12       cgd 	register u_long ul;
    653  1.12       cgd 	register int base, *lenp;
    654  1.37   thorpej 	char *buf;
    655  1.37   thorpej 	size_t buflen;
    656  1.37   thorpej {
    657  1.12       cgd 	register char *p;
    658  1.12       cgd 
    659  1.12       cgd 	p = buf;
    660  1.37   thorpej 	*p = '\0';			/* ensure NULL `termination' */
    661  1.37   thorpej 
    662  1.37   thorpej 	/*
    663  1.37   thorpej 	 * Don't even bother of the buffer's not big enough.  No
    664  1.37   thorpej 	 * value at all is better than a wrong value, and we
    665  1.37   thorpej 	 * have a lot of control over the buffer that's passed
    666  1.37   thorpej 	 * to this function, since it's not exported.
    667  1.37   thorpej 	 */
    668  1.37   thorpej 	if (buflen < KSNPRINTN_BUFSIZE)
    669  1.37   thorpej 		return (p);
    670  1.37   thorpej 
    671  1.12       cgd 	do {
    672  1.12       cgd 		*++p = "0123456789abcdef"[ul % base];
    673  1.12       cgd 	} while (ul /= base);
    674  1.12       cgd 	if (lenp)
    675  1.12       cgd 		*lenp = p - buf;
    676  1.12       cgd 	return (p);
    677  1.37   thorpej }
    678  1.37   thorpej 
    679  1.37   thorpej /*
    680  1.37   thorpej  * Print a bitmask into the provided buffer, and return a pointer
    681  1.37   thorpej  * to that buffer.
    682  1.37   thorpej  */
    683  1.37   thorpej char *
    684  1.37   thorpej bitmask_snprintf(ul, p, buf, buflen)
    685  1.37   thorpej 	u_long ul;
    686  1.37   thorpej 	const char *p;
    687  1.37   thorpej 	char *buf;
    688  1.37   thorpej 	size_t buflen;
    689  1.37   thorpej {
    690  1.37   thorpej 	char *bp, *q;
    691  1.37   thorpej 	size_t left;
    692  1.37   thorpej 	register int n;
    693  1.37   thorpej 	int ch, tmp;
    694  1.37   thorpej 	char snbuf[KSNPRINTN_BUFSIZE];
    695  1.37   thorpej 
    696  1.37   thorpej 	bp = buf;
    697  1.37   thorpej 	bzero(buf, buflen);
    698  1.37   thorpej 
    699  1.37   thorpej 	/*
    700  1.37   thorpej 	 * Always leave room for the trailing NULL.
    701  1.37   thorpej 	 */
    702  1.37   thorpej 	left = buflen - 1;
    703  1.37   thorpej 
    704  1.37   thorpej 	/*
    705  1.37   thorpej 	 * Print the value into the buffer.  Abort if there's not
    706  1.37   thorpej 	 * enough room.
    707  1.37   thorpej 	 */
    708  1.37   thorpej 	if (buflen < KSNPRINTN_BUFSIZE)
    709  1.37   thorpej 		return (buf);
    710  1.37   thorpej 
    711  1.37   thorpej 	for (q = ksnprintn(ul, *p++, NULL, snbuf, sizeof(snbuf));
    712  1.37   thorpej 	    (ch = *q--) != 0;) {
    713  1.37   thorpej 		*bp++ = ch;
    714  1.37   thorpej 		left--;
    715  1.37   thorpej 	}
    716  1.37   thorpej 
    717  1.37   thorpej 	/*
    718  1.37   thorpej 	 * If the value we printed was 0, or if we don't have room for
    719  1.37   thorpej 	 * "<x>", we're done.
    720  1.37   thorpej 	 */
    721  1.37   thorpej 	if (ul == 0 || left < 3)
    722  1.37   thorpej 		return (buf);
    723  1.37   thorpej 
    724  1.37   thorpej #define PUTBYTE(b, c, l)	\
    725  1.37   thorpej 	*(b)++ = (c);		\
    726  1.37   thorpej 	if (--(l) == 0)		\
    727  1.37   thorpej 		goto out;
    728  1.37   thorpej 
    729  1.37   thorpej 	for (tmp = 0; (n = *p++) != 0;) {
    730  1.37   thorpej 		if (ul & (1 << (n - 1))) {
    731  1.37   thorpej 			PUTBYTE(bp, tmp ? ',' : '<', left);
    732  1.37   thorpej 				for (; (n = *p) > ' '; ++p) {
    733  1.37   thorpej 					PUTBYTE(bp, n, left);
    734  1.37   thorpej 				}
    735  1.37   thorpej 				tmp = 1;
    736  1.37   thorpej 		} else
    737  1.37   thorpej 			for (; *p > ' '; ++p)
    738  1.37   thorpej 				continue;
    739  1.37   thorpej 	}
    740  1.37   thorpej 	if (tmp)
    741  1.37   thorpej 		*bp = '>';
    742  1.37   thorpej 
    743  1.37   thorpej #undef PUTBYTE
    744  1.37   thorpej 
    745  1.37   thorpej  out:
    746  1.37   thorpej 	return (buf);
    747  1.12       cgd }
    748