xref: /freebsd/sys/kern/subr_prf.c (revision 120f07833429b4591b35c937b643de8a7968a7d6)
1df8bae1dSRodney W. Grimes /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1986, 1988, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
5df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
6df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
7df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
19df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
20df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
21df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
22df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
23df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
24df8bae1dSRodney W. Grimes  *    without specific prior written permission.
25df8bae1dSRodney W. Grimes  *
26df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  *	@(#)subr_prf.c	8.3 (Berkeley) 1/21/94
39120f0783SPoul-Henning Kamp  * $Id: subr_prf.c,v 1.26 1996/01/22 13:21:33 phk Exp $
40df8bae1dSRodney W. Grimes  */
41df8bae1dSRodney W. Grimes 
420e41ee30SGarrett Wollman #include "opt_ddb.h"
430e41ee30SGarrett Wollman 
44df8bae1dSRodney W. Grimes #include <sys/param.h>
45df8bae1dSRodney W. Grimes #include <sys/systm.h>
46df8bae1dSRodney W. Grimes #include <sys/reboot.h>
47df8bae1dSRodney W. Grimes #include <sys/msgbuf.h>
48df8bae1dSRodney W. Grimes #include <sys/proc.h>
49df8bae1dSRodney W. Grimes #include <sys/vnode.h>
50df8bae1dSRodney W. Grimes #include <sys/tty.h>
51df8bae1dSRodney W. Grimes #include <sys/tprintf.h>
52df8bae1dSRodney W. Grimes #include <sys/syslog.h>
53df8bae1dSRodney W. Grimes #include <sys/malloc.h>
544fb0b0deSJoerg Wunsch #include <machine/cons.h>
55df8bae1dSRodney W. Grimes 
56df8bae1dSRodney W. Grimes /*
57df8bae1dSRodney W. Grimes  * Note that stdarg.h and the ANSI style va_start macro is used for both
58df8bae1dSRodney W. Grimes  * ANSI and traditional C compilers.
59df8bae1dSRodney W. Grimes  */
60df8bae1dSRodney W. Grimes #include <machine/stdarg.h>
61df8bae1dSRodney W. Grimes 
62df8bae1dSRodney W. Grimes #ifdef KADB
63df8bae1dSRodney W. Grimes #include <machine/kdbparam.h>
64df8bae1dSRodney W. Grimes #endif
65df8bae1dSRodney W. Grimes 
664fb0b0deSJoerg Wunsch 
67df8bae1dSRodney W. Grimes #define TOCONS	0x01
68df8bae1dSRodney W. Grimes #define TOTTY	0x02
69df8bae1dSRodney W. Grimes #define TOLOG	0x04
70df8bae1dSRodney W. Grimes 
71df8bae1dSRodney W. Grimes struct	tty *constty;			/* pointer to console "window" tty */
72df8bae1dSRodney W. Grimes 
7387b6de2bSPoul-Henning Kamp static void (*v_putc)(int) = cnputc;	/* routine to putc on virtual console */
74df8bae1dSRodney W. Grimes 
7587b6de2bSPoul-Henning Kamp static void  logpri __P((int level));
76791d77e0SPoul-Henning Kamp static void  msglogchar(int c, void *dummyarg);
77791d77e0SPoul-Henning Kamp struct putchar_arg {int flags; struct tty *tty; };
78791d77e0SPoul-Henning Kamp static void  putchar __P((int ch, void *arg));
79df8bae1dSRodney W. Grimes static char *ksprintn __P((u_long num, int base, int *len));
80df8bae1dSRodney W. Grimes 
8187b6de2bSPoul-Henning Kamp static int consintr = 1;		/* Ok to handle console interrupts? */
82df8bae1dSRodney W. Grimes 
83df8bae1dSRodney W. Grimes /*
84df8bae1dSRodney W. Grimes  * Variable panicstr contains argument to first call to panic; used as flag
85df8bae1dSRodney W. Grimes  * to indicate that the kernel has already called panic.
86df8bae1dSRodney W. Grimes  */
87df8bae1dSRodney W. Grimes const char *panicstr;
88df8bae1dSRodney W. Grimes 
89df8bae1dSRodney W. Grimes /*
90df8bae1dSRodney W. Grimes  * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
91df8bae1dSRodney W. Grimes  * and then reboots.  If we are called twice, then we avoid trying to sync
92df8bae1dSRodney W. Grimes  * the disks as this often leads to recursive panics.
93df8bae1dSRodney W. Grimes  */
94df8bae1dSRodney W. Grimes #ifdef __GNUC__
950e427608SDavid Greenman __dead			/* panic() does not return */
96df8bae1dSRodney W. Grimes #endif
97df8bae1dSRodney W. Grimes void
98df8bae1dSRodney W. Grimes panic(const char *fmt, ...)
99df8bae1dSRodney W. Grimes {
100df8bae1dSRodney W. Grimes 	int bootopt;
101df8bae1dSRodney W. Grimes 	va_list ap;
102df8bae1dSRodney W. Grimes 
103df8bae1dSRodney W. Grimes 	bootopt = RB_AUTOBOOT | RB_DUMP;
104df8bae1dSRodney W. Grimes 	if (panicstr)
105df8bae1dSRodney W. Grimes 		bootopt |= RB_NOSYNC;
106df8bae1dSRodney W. Grimes 	else
107df8bae1dSRodney W. Grimes 		panicstr = fmt;
108df8bae1dSRodney W. Grimes 
109df8bae1dSRodney W. Grimes 	va_start(ap, fmt);
110120f0783SPoul-Henning Kamp 	printf("panic: ");
111120f0783SPoul-Henning Kamp 	vprintf(fmt, ap);
112df8bae1dSRodney W. Grimes 	va_end(ap);
113df8bae1dSRodney W. Grimes 
114df8bae1dSRodney W. Grimes #ifdef KGDB
115df8bae1dSRodney W. Grimes 	kgdb_panic();
116df8bae1dSRodney W. Grimes #endif
117df8bae1dSRodney W. Grimes #ifdef KADB
118df8bae1dSRodney W. Grimes 	if (boothowto & RB_KDB)
119df8bae1dSRodney W. Grimes 		kdbpanic();
120df8bae1dSRodney W. Grimes #endif
1218a129caeSDavid Greenman #ifdef DDB
12226f9a767SRodney W. Grimes 	Debugger ("panic");
12326f9a767SRodney W. Grimes #endif
124df8bae1dSRodney W. Grimes 	boot(bootopt);
125df8bae1dSRodney W. Grimes }
126df8bae1dSRodney W. Grimes 
127df8bae1dSRodney W. Grimes /*
128df8bae1dSRodney W. Grimes  * Warn that a system table is full.
129df8bae1dSRodney W. Grimes  */
130df8bae1dSRodney W. Grimes void
131df8bae1dSRodney W. Grimes tablefull(tab)
132df8bae1dSRodney W. Grimes 	const char *tab;
133df8bae1dSRodney W. Grimes {
134df8bae1dSRodney W. Grimes 
135df8bae1dSRodney W. Grimes 	log(LOG_ERR, "%s: table is full\n", tab);
136df8bae1dSRodney W. Grimes }
137df8bae1dSRodney W. Grimes 
138df8bae1dSRodney W. Grimes /*
139df8bae1dSRodney W. Grimes  * Uprintf prints to the controlling terminal for the current process.
140df8bae1dSRodney W. Grimes  * It may block if the tty queue is overfull.  No message is printed if
141df8bae1dSRodney W. Grimes  * the queue does not clear in a reasonable time.
142df8bae1dSRodney W. Grimes  */
143df8bae1dSRodney W. Grimes void
144df8bae1dSRodney W. Grimes uprintf(const char *fmt, ...)
145df8bae1dSRodney W. Grimes {
146791d77e0SPoul-Henning Kamp 	struct proc *p = curproc;
147df8bae1dSRodney W. Grimes 	va_list ap;
148791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
149df8bae1dSRodney W. Grimes 
150df8bae1dSRodney W. Grimes 	if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
151df8bae1dSRodney W. Grimes 		va_start(ap, fmt);
152791d77e0SPoul-Henning Kamp 		pca.tty = p->p_session->s_ttyp;
153791d77e0SPoul-Henning Kamp 		pca.flags = TOTTY;
154791d77e0SPoul-Henning Kamp 		kvprintf(fmt, putchar, &pca, 10, ap);
155df8bae1dSRodney W. Grimes 		va_end(ap);
156df8bae1dSRodney W. Grimes 	}
157df8bae1dSRodney W. Grimes }
158df8bae1dSRodney W. Grimes 
159df8bae1dSRodney W. Grimes tpr_t
160df8bae1dSRodney W. Grimes tprintf_open(p)
161df8bae1dSRodney W. Grimes 	register struct proc *p;
162df8bae1dSRodney W. Grimes {
163df8bae1dSRodney W. Grimes 
164df8bae1dSRodney W. Grimes 	if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
165df8bae1dSRodney W. Grimes 		SESSHOLD(p->p_session);
166df8bae1dSRodney W. Grimes 		return ((tpr_t) p->p_session);
167df8bae1dSRodney W. Grimes 	}
168df8bae1dSRodney W. Grimes 	return ((tpr_t) NULL);
169df8bae1dSRodney W. Grimes }
170df8bae1dSRodney W. Grimes 
171df8bae1dSRodney W. Grimes void
172df8bae1dSRodney W. Grimes tprintf_close(sess)
173df8bae1dSRodney W. Grimes 	tpr_t sess;
174df8bae1dSRodney W. Grimes {
175df8bae1dSRodney W. Grimes 
176df8bae1dSRodney W. Grimes 	if (sess)
177df8bae1dSRodney W. Grimes 		SESSRELE((struct session *) sess);
178df8bae1dSRodney W. Grimes }
179df8bae1dSRodney W. Grimes 
180df8bae1dSRodney W. Grimes /*
181df8bae1dSRodney W. Grimes  * tprintf prints on the controlling terminal associated
182df8bae1dSRodney W. Grimes  * with the given session.
183df8bae1dSRodney W. Grimes  */
184df8bae1dSRodney W. Grimes void
185df8bae1dSRodney W. Grimes tprintf(tpr_t tpr, const char *fmt, ...)
186df8bae1dSRodney W. Grimes {
187df8bae1dSRodney W. Grimes 	register struct session *sess = (struct session *)tpr;
188df8bae1dSRodney W. Grimes 	struct tty *tp = NULL;
189df8bae1dSRodney W. Grimes 	int flags = TOLOG;
190df8bae1dSRodney W. Grimes 	va_list ap;
191791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
192df8bae1dSRodney W. Grimes 
193df8bae1dSRodney W. Grimes 	logpri(LOG_INFO);
194df8bae1dSRodney W. Grimes 	if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
195df8bae1dSRodney W. Grimes 		flags |= TOTTY;
196df8bae1dSRodney W. Grimes 		tp = sess->s_ttyp;
197df8bae1dSRodney W. Grimes 	}
198df8bae1dSRodney W. Grimes 	va_start(ap, fmt);
199791d77e0SPoul-Henning Kamp 	pca.tty = tp;
200791d77e0SPoul-Henning Kamp 	pca.flags = flags;
201791d77e0SPoul-Henning Kamp 	kvprintf(fmt, putchar, &pca, 10, ap);
202df8bae1dSRodney W. Grimes 	va_end(ap);
203df8bae1dSRodney W. Grimes 	logwakeup();
204df8bae1dSRodney W. Grimes }
205df8bae1dSRodney W. Grimes 
206df8bae1dSRodney W. Grimes /*
207df8bae1dSRodney W. Grimes  * Ttyprintf displays a message on a tty; it should be used only by
208df8bae1dSRodney W. Grimes  * the tty driver, or anything that knows the underlying tty will not
209df8bae1dSRodney W. Grimes  * be revoke(2)'d away.  Other callers should use tprintf.
210df8bae1dSRodney W. Grimes  */
211df8bae1dSRodney W. Grimes void
212df8bae1dSRodney W. Grimes ttyprintf(struct tty *tp, const char *fmt, ...)
213df8bae1dSRodney W. Grimes {
214df8bae1dSRodney W. Grimes 	va_list ap;
215791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
216df8bae1dSRodney W. Grimes 	va_start(ap, fmt);
217791d77e0SPoul-Henning Kamp 	pca.tty = tp;
218791d77e0SPoul-Henning Kamp 	pca.flags = TOTTY;
219791d77e0SPoul-Henning Kamp 	kvprintf(fmt, putchar, &pca, 10, ap);
220df8bae1dSRodney W. Grimes 	va_end(ap);
221df8bae1dSRodney W. Grimes }
222df8bae1dSRodney W. Grimes 
223df8bae1dSRodney W. Grimes extern	int log_open;
224df8bae1dSRodney W. Grimes 
225df8bae1dSRodney W. Grimes /*
226df8bae1dSRodney W. Grimes  * Log writes to the log buffer, and guarantees not to sleep (so can be
227df8bae1dSRodney W. Grimes  * called by interrupt routines).  If there is no process reading the
228df8bae1dSRodney W. Grimes  * log yet, it writes to the console also.
229df8bae1dSRodney W. Grimes  */
230df8bae1dSRodney W. Grimes void
231df8bae1dSRodney W. Grimes log(int level, const char *fmt, ...)
232df8bae1dSRodney W. Grimes {
233df8bae1dSRodney W. Grimes 	register int s;
234df8bae1dSRodney W. Grimes 	va_list ap;
235df8bae1dSRodney W. Grimes 
236df8bae1dSRodney W. Grimes 	s = splhigh();
237df8bae1dSRodney W. Grimes 	logpri(level);
238df8bae1dSRodney W. Grimes 	va_start(ap, fmt);
239791d77e0SPoul-Henning Kamp 
240791d77e0SPoul-Henning Kamp 	kvprintf(fmt, msglogchar, NULL, 10, ap);
241df8bae1dSRodney W. Grimes 	va_end(ap);
242791d77e0SPoul-Henning Kamp 
243791d77e0SPoul-Henning Kamp 	splx(s);
244df8bae1dSRodney W. Grimes 	if (!log_open) {
245791d77e0SPoul-Henning Kamp 		struct putchar_arg pca;
246df8bae1dSRodney W. Grimes 		va_start(ap, fmt);
247791d77e0SPoul-Henning Kamp 		pca.tty = NULL;
248791d77e0SPoul-Henning Kamp 		pca.flags = TOCONS;
249791d77e0SPoul-Henning Kamp 		kvprintf(fmt, putchar, &pca, 10, ap);
250df8bae1dSRodney W. Grimes 		va_end(ap);
251df8bae1dSRodney W. Grimes 	}
252df8bae1dSRodney W. Grimes 	logwakeup();
253df8bae1dSRodney W. Grimes }
254df8bae1dSRodney W. Grimes 
25587b6de2bSPoul-Henning Kamp static void
256df8bae1dSRodney W. Grimes logpri(level)
257df8bae1dSRodney W. Grimes 	int level;
258df8bae1dSRodney W. Grimes {
259df8bae1dSRodney W. Grimes 	register char *p;
260df8bae1dSRodney W. Grimes 
261791d77e0SPoul-Henning Kamp 	msglogchar('<', NULL);
262797f2d22SPoul-Henning Kamp 	for (p = ksprintn((u_long)level, 10, NULL); *p;)
263791d77e0SPoul-Henning Kamp 		msglogchar(*p--, NULL);
264791d77e0SPoul-Henning Kamp 	msglogchar('>', NULL);
265df8bae1dSRodney W. Grimes }
266df8bae1dSRodney W. Grimes 
267df8bae1dSRodney W. Grimes void
268df8bae1dSRodney W. Grimes addlog(const char *fmt, ...)
269df8bae1dSRodney W. Grimes {
270df8bae1dSRodney W. Grimes 	register int s;
271df8bae1dSRodney W. Grimes 	va_list ap;
272df8bae1dSRodney W. Grimes 
273df8bae1dSRodney W. Grimes 	s = splhigh();
274df8bae1dSRodney W. Grimes 	va_start(ap, fmt);
275791d77e0SPoul-Henning Kamp 	kvprintf(fmt, msglogchar, NULL, 10, ap);
276df8bae1dSRodney W. Grimes 	splx(s);
277df8bae1dSRodney W. Grimes 	va_end(ap);
278df8bae1dSRodney W. Grimes 	if (!log_open) {
279791d77e0SPoul-Henning Kamp 		struct putchar_arg pca;
280df8bae1dSRodney W. Grimes 		va_start(ap, fmt);
281791d77e0SPoul-Henning Kamp 		pca.tty = NULL;
282791d77e0SPoul-Henning Kamp 		pca.flags = TOCONS;
283791d77e0SPoul-Henning Kamp 		kvprintf(fmt, putchar, &pca, 10, ap);
284df8bae1dSRodney W. Grimes 		va_end(ap);
285df8bae1dSRodney W. Grimes 	}
286df8bae1dSRodney W. Grimes 	logwakeup();
287df8bae1dSRodney W. Grimes }
288df8bae1dSRodney W. Grimes 
289df8bae1dSRodney W. Grimes void
290df8bae1dSRodney W. Grimes printf(const char *fmt, ...)
291df8bae1dSRodney W. Grimes {
292df8bae1dSRodney W. Grimes 	va_list ap;
293df8bae1dSRodney W. Grimes 	register int savintr;
294791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
295df8bae1dSRodney W. Grimes 
296df8bae1dSRodney W. Grimes 	savintr = consintr;		/* disable interrupts */
297df8bae1dSRodney W. Grimes 	consintr = 0;
298df8bae1dSRodney W. Grimes 	va_start(ap, fmt);
299791d77e0SPoul-Henning Kamp 	pca.tty = NULL;
300791d77e0SPoul-Henning Kamp 	pca.flags = TOCONS | TOLOG;
301791d77e0SPoul-Henning Kamp 	kvprintf(fmt, putchar, &pca, 10, ap);
302df8bae1dSRodney W. Grimes 	va_end(ap);
303df8bae1dSRodney W. Grimes 	if (!panicstr)
304df8bae1dSRodney W. Grimes 		logwakeup();
305df8bae1dSRodney W. Grimes 	consintr = savintr;		/* reenable interrupts */
306df8bae1dSRodney W. Grimes }
307df8bae1dSRodney W. Grimes 
308791d77e0SPoul-Henning Kamp void
309791d77e0SPoul-Henning Kamp vprintf(const char *fmt, va_list ap)
310791d77e0SPoul-Henning Kamp {
311791d77e0SPoul-Henning Kamp 	register int savintr;
312791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
313791d77e0SPoul-Henning Kamp 
314791d77e0SPoul-Henning Kamp 	savintr = consintr;		/* disable interrupts */
315791d77e0SPoul-Henning Kamp 	consintr = 0;
316791d77e0SPoul-Henning Kamp 	pca.tty = NULL;
317791d77e0SPoul-Henning Kamp 	pca.flags = TOCONS | TOLOG;
318791d77e0SPoul-Henning Kamp 	kvprintf(fmt, putchar, &pca, 10, ap);
319791d77e0SPoul-Henning Kamp 	if (!panicstr)
320791d77e0SPoul-Henning Kamp 		logwakeup();
321791d77e0SPoul-Henning Kamp 	consintr = savintr;		/* reenable interrupts */
322791d77e0SPoul-Henning Kamp }
323791d77e0SPoul-Henning Kamp 
324791d77e0SPoul-Henning Kamp /*
325791d77e0SPoul-Henning Kamp  * Print a character on console or users terminal.  If destination is
326791d77e0SPoul-Henning Kamp  * the console then the last MSGBUFS characters are saved in msgbuf for
327791d77e0SPoul-Henning Kamp  * inspection later.
328791d77e0SPoul-Henning Kamp  */
329791d77e0SPoul-Henning Kamp static void
330791d77e0SPoul-Henning Kamp putchar(int c, void *arg)
331791d77e0SPoul-Henning Kamp {
332791d77e0SPoul-Henning Kamp 	struct putchar_arg *ap = (struct putchar_arg*) arg;
333791d77e0SPoul-Henning Kamp 	int flags = ap->flags;
334791d77e0SPoul-Henning Kamp 	struct tty *tp = ap->tty;
335791d77e0SPoul-Henning Kamp 	if (panicstr)
336791d77e0SPoul-Henning Kamp 		constty = NULL;
337791d77e0SPoul-Henning Kamp 	if ((flags & TOCONS) && tp == NULL && constty) {
338791d77e0SPoul-Henning Kamp 		tp = constty;
339791d77e0SPoul-Henning Kamp 		flags |= TOTTY;
340791d77e0SPoul-Henning Kamp 	}
341791d77e0SPoul-Henning Kamp 	if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 &&
342791d77e0SPoul-Henning Kamp 	    (flags & TOCONS) && tp == constty)
343791d77e0SPoul-Henning Kamp 		constty = NULL;
344791d77e0SPoul-Henning Kamp 	if ((flags & TOLOG))
345791d77e0SPoul-Henning Kamp 		msglogchar(c, NULL);
346791d77e0SPoul-Henning Kamp 	if ((flags & TOCONS) && constty == NULL && c != '\0')
347791d77e0SPoul-Henning Kamp 		(*v_putc)(c);
348791d77e0SPoul-Henning Kamp }
349791d77e0SPoul-Henning Kamp 
350791d77e0SPoul-Henning Kamp /*
351791d77e0SPoul-Henning Kamp  * Scaled down version of sprintf(3).
352791d77e0SPoul-Henning Kamp  */
353791d77e0SPoul-Henning Kamp int
354791d77e0SPoul-Henning Kamp sprintf(char *buf, const char *cfmt, ...)
355791d77e0SPoul-Henning Kamp {
356791d77e0SPoul-Henning Kamp 	int retval;
357791d77e0SPoul-Henning Kamp 	va_list ap;
358791d77e0SPoul-Henning Kamp 
359791d77e0SPoul-Henning Kamp 	va_start(ap, cfmt);
360791d77e0SPoul-Henning Kamp 	retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
361fe96d47dSPoul-Henning Kamp 	buf[retval] = '\0';
362791d77e0SPoul-Henning Kamp 	va_end(ap);
363791d77e0SPoul-Henning Kamp 	return retval;
364791d77e0SPoul-Henning Kamp }
365791d77e0SPoul-Henning Kamp 
366791d77e0SPoul-Henning Kamp /*
367791d77e0SPoul-Henning Kamp  * Put a number (base <= 16) in a buffer in reverse order; return an
368791d77e0SPoul-Henning Kamp  * optional length and a pointer to the NULL terminated (preceded?)
369791d77e0SPoul-Henning Kamp  * buffer.
370791d77e0SPoul-Henning Kamp  */
371791d77e0SPoul-Henning Kamp static char *
372791d77e0SPoul-Henning Kamp ksprintn(ul, base, lenp)
373791d77e0SPoul-Henning Kamp 	register u_long ul;
374791d77e0SPoul-Henning Kamp 	register int base, *lenp;
375791d77e0SPoul-Henning Kamp {					/* A long in base 8, plus NULL. */
376791d77e0SPoul-Henning Kamp 	static char buf[sizeof(long) * NBBY / 3 + 2];
377791d77e0SPoul-Henning Kamp 	register char *p;
378791d77e0SPoul-Henning Kamp 
379791d77e0SPoul-Henning Kamp 	p = buf;
380791d77e0SPoul-Henning Kamp 	do {
381791d77e0SPoul-Henning Kamp 		*++p = hex2ascii(ul % base);
382791d77e0SPoul-Henning Kamp 	} while (ul /= base);
383791d77e0SPoul-Henning Kamp 	if (lenp)
384791d77e0SPoul-Henning Kamp 		*lenp = p - buf;
385791d77e0SPoul-Henning Kamp 	return (p);
386791d77e0SPoul-Henning Kamp }
387791d77e0SPoul-Henning Kamp 
388df8bae1dSRodney W. Grimes /*
389df8bae1dSRodney W. Grimes  * Scaled down version of printf(3).
390df8bae1dSRodney W. Grimes  *
391df8bae1dSRodney W. Grimes  * Two additional formats:
392df8bae1dSRodney W. Grimes  *
393df8bae1dSRodney W. Grimes  * The format %b is supported to decode error registers.
394df8bae1dSRodney W. Grimes  * Its usage is:
395df8bae1dSRodney W. Grimes  *
396df8bae1dSRodney W. Grimes  *	printf("reg=%b\n", regval, "<base><arg>*");
397df8bae1dSRodney W. Grimes  *
398df8bae1dSRodney W. Grimes  * where <base> is the output base expressed as a control character, e.g.
399df8bae1dSRodney W. Grimes  * \10 gives octal; \20 gives hex.  Each arg is a sequence of characters,
400df8bae1dSRodney W. Grimes  * the first of which gives the bit number to be inspected (origin 1), and
401df8bae1dSRodney W. Grimes  * the next characters (up to a control character, i.e. a character <= 32),
402df8bae1dSRodney W. Grimes  * give the name of the register.  Thus:
403df8bae1dSRodney W. Grimes  *
404df8bae1dSRodney W. Grimes  *	kprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
405df8bae1dSRodney W. Grimes  *
406df8bae1dSRodney W. Grimes  * would produce output:
407df8bae1dSRodney W. Grimes  *
408df8bae1dSRodney W. Grimes  *	reg=3<BITTWO,BITONE>
409df8bae1dSRodney W. Grimes  *
410120f0783SPoul-Henning Kamp  * XXX:  %D  -- Hexdump, takes pointer and separator string:
411120f0783SPoul-Henning Kamp  *		("%6D", ptr, ":")   -> XX:XX:XX:XX:XX:XX
412120f0783SPoul-Henning Kamp  *		("%*D", len, ptr, " " -> XX XX XX XX ...
413df8bae1dSRodney W. Grimes  */
414791d77e0SPoul-Henning Kamp int
415791d77e0SPoul-Henning Kamp kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap)
416df8bae1dSRodney W. Grimes {
417791d77e0SPoul-Henning Kamp #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
418791d77e0SPoul-Henning Kamp 	char *p, *q, *d;
419120f0783SPoul-Henning Kamp 	u_char *up;
420791d77e0SPoul-Henning Kamp 	int ch, n;
421df8bae1dSRodney W. Grimes 	u_long ul;
4224830092aSPoul-Henning Kamp 	int base, lflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
4234f20e4b1SPoul-Henning Kamp 	int dwidth;
424df8bae1dSRodney W. Grimes 	char padc;
425791d77e0SPoul-Henning Kamp 	int retval = 0;
426791d77e0SPoul-Henning Kamp 
427fe96d47dSPoul-Henning Kamp 	if (!func)
428791d77e0SPoul-Henning Kamp 		d = (char *) arg;
429791d77e0SPoul-Henning Kamp 	else
430fe96d47dSPoul-Henning Kamp 		d = NULL;
4318f5067baSDavid Greenman 
4328f5067baSDavid Greenman 	if (fmt == NULL)
4332336b9d7SBruce Evans 		fmt = "(fmt null)\n";
434b4b2f81eSPoul-Henning Kamp 
435120f0783SPoul-Henning Kamp 	if (radix < 2 || radix > 36)
436b4b2f81eSPoul-Henning Kamp 		radix = 10;
437b4b2f81eSPoul-Henning Kamp 
438df8bae1dSRodney W. Grimes 	for (;;) {
439df8bae1dSRodney W. Grimes 		padc = ' ';
440df8bae1dSRodney W. Grimes 		width = 0;
441df8bae1dSRodney W. Grimes 		while ((ch = *(u_char *)fmt++) != '%') {
442df8bae1dSRodney W. Grimes 			if (ch == '\0')
443791d77e0SPoul-Henning Kamp 				return retval;
444791d77e0SPoul-Henning Kamp 			PCHAR(ch);
445df8bae1dSRodney W. Grimes 		}
4464f20e4b1SPoul-Henning Kamp 		lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
4474f20e4b1SPoul-Henning Kamp 		sign = 0; dot = 0; dwidth = 0;
448df8bae1dSRodney W. Grimes reswitch:	switch (ch = *(u_char *)fmt++) {
4494830092aSPoul-Henning Kamp 		case '.':
4504830092aSPoul-Henning Kamp 			dot = 1;
4514830092aSPoul-Henning Kamp 			goto reswitch;
452791d77e0SPoul-Henning Kamp 		case '#':
453791d77e0SPoul-Henning Kamp 			sharpflag = 1;
454791d77e0SPoul-Henning Kamp 			goto reswitch;
455791d77e0SPoul-Henning Kamp 		case '+':
456791d77e0SPoul-Henning Kamp 			sign = 1;
457791d77e0SPoul-Henning Kamp 			goto reswitch;
458791d77e0SPoul-Henning Kamp 		case '-':
459791d77e0SPoul-Henning Kamp 			ladjust = 1;
460791d77e0SPoul-Henning Kamp 			goto reswitch;
461791d77e0SPoul-Henning Kamp 		case '%':
462791d77e0SPoul-Henning Kamp 			PCHAR(ch);
463791d77e0SPoul-Henning Kamp 			break;
464791d77e0SPoul-Henning Kamp 		case '*':
465ed71c342SPoul-Henning Kamp 			if (!dot) {
466791d77e0SPoul-Henning Kamp 				width = va_arg(ap, int);
467791d77e0SPoul-Henning Kamp 				if (width < 0) {
468791d77e0SPoul-Henning Kamp 					ladjust = !ladjust;
469791d77e0SPoul-Henning Kamp 					width = -width;
470791d77e0SPoul-Henning Kamp 				}
471ed71c342SPoul-Henning Kamp 			} else {
472ed71c342SPoul-Henning Kamp 				dwidth = va_arg(ap, int);
473ed71c342SPoul-Henning Kamp 			}
474791d77e0SPoul-Henning Kamp 			goto reswitch;
475df8bae1dSRodney W. Grimes 		case '0':
4764f20e4b1SPoul-Henning Kamp 			if (!dot) {
477df8bae1dSRodney W. Grimes 				padc = '0';
478df8bae1dSRodney W. Grimes 				goto reswitch;
4794f20e4b1SPoul-Henning Kamp 			}
480df8bae1dSRodney W. Grimes 		case '1': case '2': case '3': case '4':
481df8bae1dSRodney W. Grimes 		case '5': case '6': case '7': case '8': case '9':
4824f20e4b1SPoul-Henning Kamp 				for (n = 0;; ++fmt) {
4834f20e4b1SPoul-Henning Kamp 					n = n * 10 + ch - '0';
484df8bae1dSRodney W. Grimes 					ch = *fmt;
485df8bae1dSRodney W. Grimes 					if (ch < '0' || ch > '9')
486df8bae1dSRodney W. Grimes 						break;
487df8bae1dSRodney W. Grimes 				}
4884f20e4b1SPoul-Henning Kamp 			if (dot)
4894f20e4b1SPoul-Henning Kamp 				dwidth = n;
4904f20e4b1SPoul-Henning Kamp 			else
4914f20e4b1SPoul-Henning Kamp 				width = n;
492df8bae1dSRodney W. Grimes 			goto reswitch;
493df8bae1dSRodney W. Grimes 		case 'b':
494df8bae1dSRodney W. Grimes 			ul = va_arg(ap, int);
495df8bae1dSRodney W. Grimes 			p = va_arg(ap, char *);
496797f2d22SPoul-Henning Kamp 			for (q = ksprintn(ul, *p++, NULL); *q;)
497791d77e0SPoul-Henning Kamp 				PCHAR(*q--);
498df8bae1dSRodney W. Grimes 
499df8bae1dSRodney W. Grimes 			if (!ul)
500df8bae1dSRodney W. Grimes 				break;
501df8bae1dSRodney W. Grimes 
502797f2d22SPoul-Henning Kamp 			for (tmp = 0; *p;) {
503797f2d22SPoul-Henning Kamp 				n = *p++;
504df8bae1dSRodney W. Grimes 				if (ul & (1 << (n - 1))) {
505791d77e0SPoul-Henning Kamp 					PCHAR(tmp ? ',' : '<');
506df8bae1dSRodney W. Grimes 					for (; (n = *p) > ' '; ++p)
507791d77e0SPoul-Henning Kamp 						PCHAR(n);
508df8bae1dSRodney W. Grimes 					tmp = 1;
509df8bae1dSRodney W. Grimes 				} else
510df8bae1dSRodney W. Grimes 					for (; *p > ' '; ++p)
511df8bae1dSRodney W. Grimes 						continue;
512df8bae1dSRodney W. Grimes 			}
513df8bae1dSRodney W. Grimes 			if (tmp)
514791d77e0SPoul-Henning Kamp 				PCHAR('>');
515df8bae1dSRodney W. Grimes 			break;
516df8bae1dSRodney W. Grimes 		case 'c':
517791d77e0SPoul-Henning Kamp 			PCHAR(va_arg(ap, int));
518df8bae1dSRodney W. Grimes 			break;
519120f0783SPoul-Henning Kamp 		case 'D':
520120f0783SPoul-Henning Kamp 			up = va_arg(ap, u_char *);
521120f0783SPoul-Henning Kamp 			p = va_arg(ap, char *);
522120f0783SPoul-Henning Kamp 			if (!width)
523120f0783SPoul-Henning Kamp 				width = 16;
524120f0783SPoul-Henning Kamp 			while(width--) {
525120f0783SPoul-Henning Kamp 				PCHAR(hex2ascii(*up >> 4));
526120f0783SPoul-Henning Kamp 				PCHAR(hex2ascii(*up & 0x0f));
527120f0783SPoul-Henning Kamp 				up++;
528120f0783SPoul-Henning Kamp 				if (width)
529120f0783SPoul-Henning Kamp 					for (q=p;*q;q++)
530120f0783SPoul-Henning Kamp 						PCHAR(*q);
531120f0783SPoul-Henning Kamp 			}
532120f0783SPoul-Henning Kamp 			break;
533df8bae1dSRodney W. Grimes 		case 'd':
534df8bae1dSRodney W. Grimes 			ul = lflag ? va_arg(ap, long) : va_arg(ap, int);
535791d77e0SPoul-Henning Kamp 			sign = 1;
536df8bae1dSRodney W. Grimes 			base = 10;
537df8bae1dSRodney W. Grimes 			goto number;
538791d77e0SPoul-Henning Kamp 		case 'l':
539791d77e0SPoul-Henning Kamp 			lflag = 1;
540791d77e0SPoul-Henning Kamp 			goto reswitch;
541791d77e0SPoul-Henning Kamp 		case 'n':
542791d77e0SPoul-Henning Kamp 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
543791d77e0SPoul-Henning Kamp 			base = radix;
544791d77e0SPoul-Henning Kamp 			goto number;
545df8bae1dSRodney W. Grimes 		case 'o':
546df8bae1dSRodney W. Grimes 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
547df8bae1dSRodney W. Grimes 			base = 8;
548df8bae1dSRodney W. Grimes 			goto number;
54912d17f65SPoul-Henning Kamp 		case 'p':
55012d17f65SPoul-Henning Kamp 			ul = (u_long)va_arg(ap, void *);
55112d17f65SPoul-Henning Kamp 			base = 16;
552791d77e0SPoul-Henning Kamp 			PCHAR('0');
553791d77e0SPoul-Henning Kamp 			PCHAR('x');
55412d17f65SPoul-Henning Kamp 			goto number;
555791d77e0SPoul-Henning Kamp 		case 's':
556791d77e0SPoul-Henning Kamp 			p = va_arg(ap, char *);
557791d77e0SPoul-Henning Kamp 			if (p == NULL)
558791d77e0SPoul-Henning Kamp 				p = "(null)";
5594830092aSPoul-Henning Kamp 			if (!dot)
5604830092aSPoul-Henning Kamp 				n = strlen (p);
5614830092aSPoul-Henning Kamp 			else
5624f20e4b1SPoul-Henning Kamp 				for (n = 0; n < dwidth && p[n]; n++)
5634830092aSPoul-Henning Kamp 					continue;
5644f20e4b1SPoul-Henning Kamp 
5654830092aSPoul-Henning Kamp 			width -= n;
5664f20e4b1SPoul-Henning Kamp 
567791d77e0SPoul-Henning Kamp 			if (!ladjust && width > 0)
568791d77e0SPoul-Henning Kamp 				while (width--)
569791d77e0SPoul-Henning Kamp 					PCHAR(padc);
5704830092aSPoul-Henning Kamp 			while (n--)
571791d77e0SPoul-Henning Kamp 				PCHAR(*p++);
572791d77e0SPoul-Henning Kamp 			if (ladjust && width > 0)
573791d77e0SPoul-Henning Kamp 				while (width--)
574791d77e0SPoul-Henning Kamp 					PCHAR(padc);
575791d77e0SPoul-Henning Kamp 			break;
576f53dbe97SBruce Evans 		case 'u':
577f53dbe97SBruce Evans 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
578f53dbe97SBruce Evans 			base = 10;
579f53dbe97SBruce Evans 			goto number;
580df8bae1dSRodney W. Grimes 		case 'x':
581df8bae1dSRodney W. Grimes 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
582df8bae1dSRodney W. Grimes 			base = 16;
583791d77e0SPoul-Henning Kamp number:			if (sign && (long)ul < 0L) {
584791d77e0SPoul-Henning Kamp 				neg = 1;
585791d77e0SPoul-Henning Kamp 				ul = -(long)ul;
586791d77e0SPoul-Henning Kamp 			}
587791d77e0SPoul-Henning Kamp 			p = ksprintn(ul, base, &tmp);
588791d77e0SPoul-Henning Kamp 			if (sharpflag && ul != 0) {
589791d77e0SPoul-Henning Kamp 				if (base == 8)
590791d77e0SPoul-Henning Kamp 					tmp++;
591791d77e0SPoul-Henning Kamp 				else if (base == 16)
592791d77e0SPoul-Henning Kamp 					tmp += 2;
593791d77e0SPoul-Henning Kamp 			}
594791d77e0SPoul-Henning Kamp 			if (neg)
595791d77e0SPoul-Henning Kamp 				tmp++;
596791d77e0SPoul-Henning Kamp 
597791d77e0SPoul-Henning Kamp 			if (!ladjust && width && (width -= tmp) > 0)
598df8bae1dSRodney W. Grimes 				while (width--)
599791d77e0SPoul-Henning Kamp 					PCHAR(padc);
600791d77e0SPoul-Henning Kamp 			if (neg)
601791d77e0SPoul-Henning Kamp 				PCHAR('-');
602791d77e0SPoul-Henning Kamp 			if (sharpflag && ul != 0) {
603791d77e0SPoul-Henning Kamp 				if (base == 8) {
604791d77e0SPoul-Henning Kamp 					PCHAR('0');
605791d77e0SPoul-Henning Kamp 				} else if (base == 16) {
606791d77e0SPoul-Henning Kamp 					PCHAR('0');
607791d77e0SPoul-Henning Kamp 					PCHAR('x');
608791d77e0SPoul-Henning Kamp 				}
609791d77e0SPoul-Henning Kamp 			}
610791d77e0SPoul-Henning Kamp 
611797f2d22SPoul-Henning Kamp 			while (*p)
612791d77e0SPoul-Henning Kamp 				PCHAR(*p--);
613791d77e0SPoul-Henning Kamp 
614791d77e0SPoul-Henning Kamp 			if (ladjust && width && (width -= tmp) > 0)
615791d77e0SPoul-Henning Kamp 				while (width--)
616791d77e0SPoul-Henning Kamp 					PCHAR(padc);
617791d77e0SPoul-Henning Kamp 
618df8bae1dSRodney W. Grimes 			break;
619df8bae1dSRodney W. Grimes 		default:
620791d77e0SPoul-Henning Kamp 			PCHAR('%');
621df8bae1dSRodney W. Grimes 			if (lflag)
622791d77e0SPoul-Henning Kamp 				PCHAR('l');
623791d77e0SPoul-Henning Kamp 			PCHAR(ch);
624791d77e0SPoul-Henning Kamp 			break;
625df8bae1dSRodney W. Grimes 		}
626df8bae1dSRodney W. Grimes 	}
627791d77e0SPoul-Henning Kamp #undef PCHAR
628df8bae1dSRodney W. Grimes }
629df8bae1dSRodney W. Grimes 
630df8bae1dSRodney W. Grimes /*
631791d77e0SPoul-Henning Kamp  * Put character in log buffer.
632df8bae1dSRodney W. Grimes  */
633df8bae1dSRodney W. Grimes static void
634791d77e0SPoul-Henning Kamp msglogchar(int c, void *dummyarg)
635df8bae1dSRodney W. Grimes {
636791d77e0SPoul-Henning Kamp 	struct msgbuf *mbp;
637df8bae1dSRodney W. Grimes 
638791d77e0SPoul-Henning Kamp 	if (c != '\0' && c != '\r' && c != 0177 && msgbufmapped) {
639df8bae1dSRodney W. Grimes 		mbp = msgbufp;
6406c8897cfSDavid Greenman 		if (mbp->msg_magic != MSG_MAGIC ||
6416c8897cfSDavid Greenman 		    mbp->msg_bufx >= MSG_BSIZE ||
6426c8897cfSDavid Greenman 		    mbp->msg_bufr >= MSG_BSIZE) {
6436c8897cfSDavid Greenman 			bzero(mbp, sizeof(struct msgbuf));
644df8bae1dSRodney W. Grimes 			mbp->msg_magic = MSG_MAGIC;
645df8bae1dSRodney W. Grimes 		}
646df8bae1dSRodney W. Grimes 		mbp->msg_bufc[mbp->msg_bufx++] = c;
647b4224c9cSDavid Greenman 		if (mbp->msg_bufx >= MSG_BSIZE)
648df8bae1dSRodney W. Grimes 			mbp->msg_bufx = 0;
64902d5c7b1SDavid Greenman 		/* If the buffer is full, keep the most recent data. */
650a3f4faceSBruce Evans 		if (mbp->msg_bufr == mbp->msg_bufx) {
65102d5c7b1SDavid Greenman 			if (++mbp->msg_bufr >= MSG_BSIZE)
652a3f4faceSBruce Evans 				mbp->msg_bufr = 0;
653a3f4faceSBruce Evans 		}
654df8bae1dSRodney W. Grimes 	}
655df8bae1dSRodney W. Grimes }
656