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 39ed71c342SPoul-Henning Kamp * $Id: subr_prf.c,v 1.24 1996/01/19 11:38:18 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); 110df8bae1dSRodney W. Grimes printf("panic: %r\n", fmt, ap); 111df8bae1dSRodney W. Grimes va_end(ap); 112df8bae1dSRodney W. Grimes 113df8bae1dSRodney W. Grimes #ifdef KGDB 114df8bae1dSRodney W. Grimes kgdb_panic(); 115df8bae1dSRodney W. Grimes #endif 116df8bae1dSRodney W. Grimes #ifdef KADB 117df8bae1dSRodney W. Grimes if (boothowto & RB_KDB) 118df8bae1dSRodney W. Grimes kdbpanic(); 119df8bae1dSRodney W. Grimes #endif 1208a129caeSDavid Greenman #ifdef DDB 12126f9a767SRodney W. Grimes Debugger ("panic"); 12226f9a767SRodney W. Grimes #endif 123df8bae1dSRodney W. Grimes boot(bootopt); 124df8bae1dSRodney W. Grimes } 125df8bae1dSRodney W. Grimes 126df8bae1dSRodney W. Grimes /* 127df8bae1dSRodney W. Grimes * Warn that a system table is full. 128df8bae1dSRodney W. Grimes */ 129df8bae1dSRodney W. Grimes void 130df8bae1dSRodney W. Grimes tablefull(tab) 131df8bae1dSRodney W. Grimes const char *tab; 132df8bae1dSRodney W. Grimes { 133df8bae1dSRodney W. Grimes 134df8bae1dSRodney W. Grimes log(LOG_ERR, "%s: table is full\n", tab); 135df8bae1dSRodney W. Grimes } 136df8bae1dSRodney W. Grimes 137df8bae1dSRodney W. Grimes /* 138df8bae1dSRodney W. Grimes * Uprintf prints to the controlling terminal for the current process. 139df8bae1dSRodney W. Grimes * It may block if the tty queue is overfull. No message is printed if 140df8bae1dSRodney W. Grimes * the queue does not clear in a reasonable time. 141df8bae1dSRodney W. Grimes */ 142df8bae1dSRodney W. Grimes void 143df8bae1dSRodney W. Grimes uprintf(const char *fmt, ...) 144df8bae1dSRodney W. Grimes { 145791d77e0SPoul-Henning Kamp struct proc *p = curproc; 146df8bae1dSRodney W. Grimes va_list ap; 147791d77e0SPoul-Henning Kamp struct putchar_arg pca; 148df8bae1dSRodney W. Grimes 149df8bae1dSRodney W. Grimes if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) { 150df8bae1dSRodney W. Grimes va_start(ap, fmt); 151791d77e0SPoul-Henning Kamp pca.tty = p->p_session->s_ttyp; 152791d77e0SPoul-Henning Kamp pca.flags = TOTTY; 153791d77e0SPoul-Henning Kamp kvprintf(fmt, putchar, &pca, 10, ap); 154df8bae1dSRodney W. Grimes va_end(ap); 155df8bae1dSRodney W. Grimes } 156df8bae1dSRodney W. Grimes } 157df8bae1dSRodney W. Grimes 158df8bae1dSRodney W. Grimes tpr_t 159df8bae1dSRodney W. Grimes tprintf_open(p) 160df8bae1dSRodney W. Grimes register struct proc *p; 161df8bae1dSRodney W. Grimes { 162df8bae1dSRodney W. Grimes 163df8bae1dSRodney W. Grimes if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) { 164df8bae1dSRodney W. Grimes SESSHOLD(p->p_session); 165df8bae1dSRodney W. Grimes return ((tpr_t) p->p_session); 166df8bae1dSRodney W. Grimes } 167df8bae1dSRodney W. Grimes return ((tpr_t) NULL); 168df8bae1dSRodney W. Grimes } 169df8bae1dSRodney W. Grimes 170df8bae1dSRodney W. Grimes void 171df8bae1dSRodney W. Grimes tprintf_close(sess) 172df8bae1dSRodney W. Grimes tpr_t sess; 173df8bae1dSRodney W. Grimes { 174df8bae1dSRodney W. Grimes 175df8bae1dSRodney W. Grimes if (sess) 176df8bae1dSRodney W. Grimes SESSRELE((struct session *) sess); 177df8bae1dSRodney W. Grimes } 178df8bae1dSRodney W. Grimes 179df8bae1dSRodney W. Grimes /* 180df8bae1dSRodney W. Grimes * tprintf prints on the controlling terminal associated 181df8bae1dSRodney W. Grimes * with the given session. 182df8bae1dSRodney W. Grimes */ 183df8bae1dSRodney W. Grimes void 184df8bae1dSRodney W. Grimes tprintf(tpr_t tpr, const char *fmt, ...) 185df8bae1dSRodney W. Grimes { 186df8bae1dSRodney W. Grimes register struct session *sess = (struct session *)tpr; 187df8bae1dSRodney W. Grimes struct tty *tp = NULL; 188df8bae1dSRodney W. Grimes int flags = TOLOG; 189df8bae1dSRodney W. Grimes va_list ap; 190791d77e0SPoul-Henning Kamp struct putchar_arg pca; 191df8bae1dSRodney W. Grimes 192df8bae1dSRodney W. Grimes logpri(LOG_INFO); 193df8bae1dSRodney W. Grimes if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) { 194df8bae1dSRodney W. Grimes flags |= TOTTY; 195df8bae1dSRodney W. Grimes tp = sess->s_ttyp; 196df8bae1dSRodney W. Grimes } 197df8bae1dSRodney W. Grimes va_start(ap, fmt); 198791d77e0SPoul-Henning Kamp pca.tty = tp; 199791d77e0SPoul-Henning Kamp pca.flags = flags; 200791d77e0SPoul-Henning Kamp kvprintf(fmt, putchar, &pca, 10, ap); 201df8bae1dSRodney W. Grimes va_end(ap); 202df8bae1dSRodney W. Grimes logwakeup(); 203df8bae1dSRodney W. Grimes } 204df8bae1dSRodney W. Grimes 205df8bae1dSRodney W. Grimes /* 206df8bae1dSRodney W. Grimes * Ttyprintf displays a message on a tty; it should be used only by 207df8bae1dSRodney W. Grimes * the tty driver, or anything that knows the underlying tty will not 208df8bae1dSRodney W. Grimes * be revoke(2)'d away. Other callers should use tprintf. 209df8bae1dSRodney W. Grimes */ 210df8bae1dSRodney W. Grimes void 211df8bae1dSRodney W. Grimes ttyprintf(struct tty *tp, const char *fmt, ...) 212df8bae1dSRodney W. Grimes { 213df8bae1dSRodney W. Grimes va_list ap; 214791d77e0SPoul-Henning Kamp struct putchar_arg pca; 215df8bae1dSRodney W. Grimes va_start(ap, fmt); 216791d77e0SPoul-Henning Kamp pca.tty = tp; 217791d77e0SPoul-Henning Kamp pca.flags = TOTTY; 218791d77e0SPoul-Henning Kamp kvprintf(fmt, putchar, &pca, 10, ap); 219df8bae1dSRodney W. Grimes va_end(ap); 220df8bae1dSRodney W. Grimes } 221df8bae1dSRodney W. Grimes 222df8bae1dSRodney W. Grimes extern int log_open; 223df8bae1dSRodney W. Grimes 224df8bae1dSRodney W. Grimes /* 225df8bae1dSRodney W. Grimes * Log writes to the log buffer, and guarantees not to sleep (so can be 226df8bae1dSRodney W. Grimes * called by interrupt routines). If there is no process reading the 227df8bae1dSRodney W. Grimes * log yet, it writes to the console also. 228df8bae1dSRodney W. Grimes */ 229df8bae1dSRodney W. Grimes void 230df8bae1dSRodney W. Grimes log(int level, const char *fmt, ...) 231df8bae1dSRodney W. Grimes { 232df8bae1dSRodney W. Grimes register int s; 233df8bae1dSRodney W. Grimes va_list ap; 234df8bae1dSRodney W. Grimes 235df8bae1dSRodney W. Grimes s = splhigh(); 236df8bae1dSRodney W. Grimes logpri(level); 237df8bae1dSRodney W. Grimes va_start(ap, fmt); 238791d77e0SPoul-Henning Kamp 239791d77e0SPoul-Henning Kamp kvprintf(fmt, msglogchar, NULL, 10, ap); 240df8bae1dSRodney W. Grimes va_end(ap); 241791d77e0SPoul-Henning Kamp 242791d77e0SPoul-Henning Kamp splx(s); 243df8bae1dSRodney W. Grimes if (!log_open) { 244791d77e0SPoul-Henning Kamp struct putchar_arg pca; 245df8bae1dSRodney W. Grimes va_start(ap, fmt); 246791d77e0SPoul-Henning Kamp pca.tty = NULL; 247791d77e0SPoul-Henning Kamp pca.flags = TOCONS; 248791d77e0SPoul-Henning Kamp kvprintf(fmt, putchar, &pca, 10, ap); 249df8bae1dSRodney W. Grimes va_end(ap); 250df8bae1dSRodney W. Grimes } 251df8bae1dSRodney W. Grimes logwakeup(); 252df8bae1dSRodney W. Grimes } 253df8bae1dSRodney W. Grimes 25487b6de2bSPoul-Henning Kamp static void 255df8bae1dSRodney W. Grimes logpri(level) 256df8bae1dSRodney W. Grimes int level; 257df8bae1dSRodney W. Grimes { 258df8bae1dSRodney W. Grimes register char *p; 259df8bae1dSRodney W. Grimes 260791d77e0SPoul-Henning Kamp msglogchar('<', NULL); 261797f2d22SPoul-Henning Kamp for (p = ksprintn((u_long)level, 10, NULL); *p;) 262791d77e0SPoul-Henning Kamp msglogchar(*p--, NULL); 263791d77e0SPoul-Henning Kamp msglogchar('>', NULL); 264df8bae1dSRodney W. Grimes } 265df8bae1dSRodney W. Grimes 266df8bae1dSRodney W. Grimes void 267df8bae1dSRodney W. Grimes addlog(const char *fmt, ...) 268df8bae1dSRodney W. Grimes { 269df8bae1dSRodney W. Grimes register int s; 270df8bae1dSRodney W. Grimes va_list ap; 271df8bae1dSRodney W. Grimes 272df8bae1dSRodney W. Grimes s = splhigh(); 273df8bae1dSRodney W. Grimes va_start(ap, fmt); 274791d77e0SPoul-Henning Kamp kvprintf(fmt, msglogchar, NULL, 10, ap); 275df8bae1dSRodney W. Grimes splx(s); 276df8bae1dSRodney W. Grimes va_end(ap); 277df8bae1dSRodney W. Grimes if (!log_open) { 278791d77e0SPoul-Henning Kamp struct putchar_arg pca; 279df8bae1dSRodney W. Grimes va_start(ap, fmt); 280791d77e0SPoul-Henning Kamp pca.tty = NULL; 281791d77e0SPoul-Henning Kamp pca.flags = TOCONS; 282791d77e0SPoul-Henning Kamp kvprintf(fmt, putchar, &pca, 10, ap); 283df8bae1dSRodney W. Grimes va_end(ap); 284df8bae1dSRodney W. Grimes } 285df8bae1dSRodney W. Grimes logwakeup(); 286df8bae1dSRodney W. Grimes } 287df8bae1dSRodney W. Grimes 288df8bae1dSRodney W. Grimes void 289df8bae1dSRodney W. Grimes printf(const char *fmt, ...) 290df8bae1dSRodney W. Grimes { 291df8bae1dSRodney W. Grimes va_list ap; 292df8bae1dSRodney W. Grimes register int savintr; 293791d77e0SPoul-Henning Kamp struct putchar_arg pca; 294df8bae1dSRodney W. Grimes 295df8bae1dSRodney W. Grimes savintr = consintr; /* disable interrupts */ 296df8bae1dSRodney W. Grimes consintr = 0; 297df8bae1dSRodney W. Grimes va_start(ap, fmt); 298791d77e0SPoul-Henning Kamp pca.tty = NULL; 299791d77e0SPoul-Henning Kamp pca.flags = TOCONS | TOLOG; 300791d77e0SPoul-Henning Kamp kvprintf(fmt, putchar, &pca, 10, ap); 301df8bae1dSRodney W. Grimes va_end(ap); 302df8bae1dSRodney W. Grimes if (!panicstr) 303df8bae1dSRodney W. Grimes logwakeup(); 304df8bae1dSRodney W. Grimes consintr = savintr; /* reenable interrupts */ 305df8bae1dSRodney W. Grimes } 306df8bae1dSRodney W. Grimes 307791d77e0SPoul-Henning Kamp void 308791d77e0SPoul-Henning Kamp vprintf(const char *fmt, va_list ap) 309791d77e0SPoul-Henning Kamp { 310791d77e0SPoul-Henning Kamp register int savintr; 311791d77e0SPoul-Henning Kamp struct putchar_arg pca; 312791d77e0SPoul-Henning Kamp 313791d77e0SPoul-Henning Kamp savintr = consintr; /* disable interrupts */ 314791d77e0SPoul-Henning Kamp consintr = 0; 315791d77e0SPoul-Henning Kamp pca.tty = NULL; 316791d77e0SPoul-Henning Kamp pca.flags = TOCONS | TOLOG; 317791d77e0SPoul-Henning Kamp kvprintf(fmt, putchar, &pca, 10, ap); 318791d77e0SPoul-Henning Kamp if (!panicstr) 319791d77e0SPoul-Henning Kamp logwakeup(); 320791d77e0SPoul-Henning Kamp consintr = savintr; /* reenable interrupts */ 321791d77e0SPoul-Henning Kamp } 322791d77e0SPoul-Henning Kamp 323791d77e0SPoul-Henning Kamp /* 324791d77e0SPoul-Henning Kamp * Print a character on console or users terminal. If destination is 325791d77e0SPoul-Henning Kamp * the console then the last MSGBUFS characters are saved in msgbuf for 326791d77e0SPoul-Henning Kamp * inspection later. 327791d77e0SPoul-Henning Kamp */ 328791d77e0SPoul-Henning Kamp static void 329791d77e0SPoul-Henning Kamp putchar(int c, void *arg) 330791d77e0SPoul-Henning Kamp { 331791d77e0SPoul-Henning Kamp struct putchar_arg *ap = (struct putchar_arg*) arg; 332791d77e0SPoul-Henning Kamp int flags = ap->flags; 333791d77e0SPoul-Henning Kamp struct tty *tp = ap->tty; 334791d77e0SPoul-Henning Kamp if (panicstr) 335791d77e0SPoul-Henning Kamp constty = NULL; 336791d77e0SPoul-Henning Kamp if ((flags & TOCONS) && tp == NULL && constty) { 337791d77e0SPoul-Henning Kamp tp = constty; 338791d77e0SPoul-Henning Kamp flags |= TOTTY; 339791d77e0SPoul-Henning Kamp } 340791d77e0SPoul-Henning Kamp if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 && 341791d77e0SPoul-Henning Kamp (flags & TOCONS) && tp == constty) 342791d77e0SPoul-Henning Kamp constty = NULL; 343791d77e0SPoul-Henning Kamp if ((flags & TOLOG)) 344791d77e0SPoul-Henning Kamp msglogchar(c, NULL); 345791d77e0SPoul-Henning Kamp if ((flags & TOCONS) && constty == NULL && c != '\0') 346791d77e0SPoul-Henning Kamp (*v_putc)(c); 347791d77e0SPoul-Henning Kamp } 348791d77e0SPoul-Henning Kamp 349791d77e0SPoul-Henning Kamp /* 350791d77e0SPoul-Henning Kamp * Scaled down version of sprintf(3). 351791d77e0SPoul-Henning Kamp */ 352791d77e0SPoul-Henning Kamp int 353791d77e0SPoul-Henning Kamp sprintf(char *buf, const char *cfmt, ...) 354791d77e0SPoul-Henning Kamp { 355791d77e0SPoul-Henning Kamp int retval; 356791d77e0SPoul-Henning Kamp va_list ap; 357791d77e0SPoul-Henning Kamp 358791d77e0SPoul-Henning Kamp va_start(ap, cfmt); 359791d77e0SPoul-Henning Kamp retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap); 360fe96d47dSPoul-Henning Kamp buf[retval] = '\0'; 361791d77e0SPoul-Henning Kamp va_end(ap); 362791d77e0SPoul-Henning Kamp return retval; 363791d77e0SPoul-Henning Kamp } 364791d77e0SPoul-Henning Kamp 365791d77e0SPoul-Henning Kamp /* 366791d77e0SPoul-Henning Kamp * Put a number (base <= 16) in a buffer in reverse order; return an 367791d77e0SPoul-Henning Kamp * optional length and a pointer to the NULL terminated (preceded?) 368791d77e0SPoul-Henning Kamp * buffer. 369791d77e0SPoul-Henning Kamp */ 370791d77e0SPoul-Henning Kamp static char * 371791d77e0SPoul-Henning Kamp ksprintn(ul, base, lenp) 372791d77e0SPoul-Henning Kamp register u_long ul; 373791d77e0SPoul-Henning Kamp register int base, *lenp; 374791d77e0SPoul-Henning Kamp { /* A long in base 8, plus NULL. */ 375791d77e0SPoul-Henning Kamp static char buf[sizeof(long) * NBBY / 3 + 2]; 376791d77e0SPoul-Henning Kamp register char *p; 377791d77e0SPoul-Henning Kamp 378791d77e0SPoul-Henning Kamp p = buf; 379791d77e0SPoul-Henning Kamp do { 380791d77e0SPoul-Henning Kamp *++p = hex2ascii(ul % base); 381791d77e0SPoul-Henning Kamp } while (ul /= base); 382791d77e0SPoul-Henning Kamp if (lenp) 383791d77e0SPoul-Henning Kamp *lenp = p - buf; 384791d77e0SPoul-Henning Kamp return (p); 385791d77e0SPoul-Henning Kamp } 386791d77e0SPoul-Henning Kamp 387df8bae1dSRodney W. Grimes /* 388df8bae1dSRodney W. Grimes * Scaled down version of printf(3). 389df8bae1dSRodney W. Grimes * 390df8bae1dSRodney W. Grimes * Two additional formats: 391df8bae1dSRodney W. Grimes * 392df8bae1dSRodney W. Grimes * The format %b is supported to decode error registers. 393df8bae1dSRodney W. Grimes * Its usage is: 394df8bae1dSRodney W. Grimes * 395df8bae1dSRodney W. Grimes * printf("reg=%b\n", regval, "<base><arg>*"); 396df8bae1dSRodney W. Grimes * 397df8bae1dSRodney W. Grimes * where <base> is the output base expressed as a control character, e.g. 398df8bae1dSRodney W. Grimes * \10 gives octal; \20 gives hex. Each arg is a sequence of characters, 399df8bae1dSRodney W. Grimes * the first of which gives the bit number to be inspected (origin 1), and 400df8bae1dSRodney W. Grimes * the next characters (up to a control character, i.e. a character <= 32), 401df8bae1dSRodney W. Grimes * give the name of the register. Thus: 402df8bae1dSRodney W. Grimes * 403df8bae1dSRodney W. Grimes * kprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n"); 404df8bae1dSRodney W. Grimes * 405df8bae1dSRodney W. Grimes * would produce output: 406df8bae1dSRodney W. Grimes * 407df8bae1dSRodney W. Grimes * reg=3<BITTWO,BITONE> 408df8bae1dSRodney W. Grimes * 409df8bae1dSRodney W. Grimes * The format %r passes an additional format string and argument list 410df8bae1dSRodney W. Grimes * recursively. Its usage is: 411df8bae1dSRodney W. Grimes * 412df8bae1dSRodney W. Grimes * fn(char *fmt, ...) 413df8bae1dSRodney W. Grimes * { 414df8bae1dSRodney W. Grimes * va_list ap; 415df8bae1dSRodney W. Grimes * va_start(ap, fmt); 416df8bae1dSRodney W. Grimes * printf("prefix: %r: suffix\n", fmt, ap); 417df8bae1dSRodney W. Grimes * va_end(ap); 418df8bae1dSRodney W. Grimes * } 419df8bae1dSRodney W. Grimes * 420df8bae1dSRodney W. Grimes * Space or zero padding and a field width are supported for the numeric 421df8bae1dSRodney W. Grimes * formats only. 422df8bae1dSRodney W. Grimes */ 423791d77e0SPoul-Henning Kamp int 424791d77e0SPoul-Henning Kamp kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap) 425df8bae1dSRodney W. Grimes { 426791d77e0SPoul-Henning Kamp #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; } 427791d77e0SPoul-Henning Kamp char *p, *q, *d; 428791d77e0SPoul-Henning Kamp int ch, n; 429df8bae1dSRodney W. Grimes u_long ul; 4304830092aSPoul-Henning Kamp int base, lflag, tmp, width, ladjust, sharpflag, neg, sign, dot; 4314f20e4b1SPoul-Henning Kamp int dwidth; 432df8bae1dSRodney W. Grimes char padc; 433791d77e0SPoul-Henning Kamp int retval = 0; 434791d77e0SPoul-Henning Kamp 435fe96d47dSPoul-Henning Kamp if (!func) 436791d77e0SPoul-Henning Kamp d = (char *) arg; 437791d77e0SPoul-Henning Kamp else 438fe96d47dSPoul-Henning Kamp d = NULL; 4398f5067baSDavid Greenman 4408f5067baSDavid Greenman if (fmt == NULL) 4412336b9d7SBruce Evans fmt = "(fmt null)\n"; 442df8bae1dSRodney W. Grimes for (;;) { 443df8bae1dSRodney W. Grimes padc = ' '; 444df8bae1dSRodney W. Grimes width = 0; 445df8bae1dSRodney W. Grimes while ((ch = *(u_char *)fmt++) != '%') { 446df8bae1dSRodney W. Grimes if (ch == '\0') 447791d77e0SPoul-Henning Kamp return retval; 448791d77e0SPoul-Henning Kamp PCHAR(ch); 449df8bae1dSRodney W. Grimes } 4504f20e4b1SPoul-Henning Kamp lflag = 0; ladjust = 0; sharpflag = 0; neg = 0; 4514f20e4b1SPoul-Henning Kamp sign = 0; dot = 0; dwidth = 0; 452df8bae1dSRodney W. Grimes reswitch: switch (ch = *(u_char *)fmt++) { 4534830092aSPoul-Henning Kamp case '.': 4544830092aSPoul-Henning Kamp dot = 1; 4554830092aSPoul-Henning Kamp goto reswitch; 456791d77e0SPoul-Henning Kamp case '#': 457791d77e0SPoul-Henning Kamp sharpflag = 1; 458791d77e0SPoul-Henning Kamp goto reswitch; 459791d77e0SPoul-Henning Kamp case '+': 460791d77e0SPoul-Henning Kamp sign = 1; 461791d77e0SPoul-Henning Kamp goto reswitch; 462791d77e0SPoul-Henning Kamp case '-': 463791d77e0SPoul-Henning Kamp ladjust = 1; 464791d77e0SPoul-Henning Kamp goto reswitch; 465791d77e0SPoul-Henning Kamp case '%': 466791d77e0SPoul-Henning Kamp PCHAR(ch); 467791d77e0SPoul-Henning Kamp break; 468791d77e0SPoul-Henning Kamp case '*': 469ed71c342SPoul-Henning Kamp if (!dot) { 470791d77e0SPoul-Henning Kamp width = va_arg(ap, int); 471791d77e0SPoul-Henning Kamp if (width < 0) { 472791d77e0SPoul-Henning Kamp ladjust = !ladjust; 473791d77e0SPoul-Henning Kamp width = -width; 474791d77e0SPoul-Henning Kamp } 475ed71c342SPoul-Henning Kamp } else { 476ed71c342SPoul-Henning Kamp dwidth = va_arg(ap, int); 477ed71c342SPoul-Henning Kamp } 478791d77e0SPoul-Henning Kamp goto reswitch; 479df8bae1dSRodney W. Grimes case '0': 4804f20e4b1SPoul-Henning Kamp if (!dot) { 481df8bae1dSRodney W. Grimes padc = '0'; 482df8bae1dSRodney W. Grimes goto reswitch; 4834f20e4b1SPoul-Henning Kamp } 484df8bae1dSRodney W. Grimes case '1': case '2': case '3': case '4': 485df8bae1dSRodney W. Grimes case '5': case '6': case '7': case '8': case '9': 4864f20e4b1SPoul-Henning Kamp for (n = 0;; ++fmt) { 4874f20e4b1SPoul-Henning Kamp n = n * 10 + ch - '0'; 488df8bae1dSRodney W. Grimes ch = *fmt; 489df8bae1dSRodney W. Grimes if (ch < '0' || ch > '9') 490df8bae1dSRodney W. Grimes break; 491df8bae1dSRodney W. Grimes } 4924f20e4b1SPoul-Henning Kamp if (dot) 4934f20e4b1SPoul-Henning Kamp dwidth = n; 4944f20e4b1SPoul-Henning Kamp else 4954f20e4b1SPoul-Henning Kamp width = n; 496df8bae1dSRodney W. Grimes goto reswitch; 497df8bae1dSRodney W. Grimes case 'b': 498df8bae1dSRodney W. Grimes ul = va_arg(ap, int); 499df8bae1dSRodney W. Grimes p = va_arg(ap, char *); 500797f2d22SPoul-Henning Kamp for (q = ksprintn(ul, *p++, NULL); *q;) 501791d77e0SPoul-Henning Kamp PCHAR(*q--); 502df8bae1dSRodney W. Grimes 503df8bae1dSRodney W. Grimes if (!ul) 504df8bae1dSRodney W. Grimes break; 505df8bae1dSRodney W. Grimes 506797f2d22SPoul-Henning Kamp for (tmp = 0; *p;) { 507797f2d22SPoul-Henning Kamp n = *p++; 508df8bae1dSRodney W. Grimes if (ul & (1 << (n - 1))) { 509791d77e0SPoul-Henning Kamp PCHAR(tmp ? ',' : '<'); 510df8bae1dSRodney W. Grimes for (; (n = *p) > ' '; ++p) 511791d77e0SPoul-Henning Kamp PCHAR(n); 512df8bae1dSRodney W. Grimes tmp = 1; 513df8bae1dSRodney W. Grimes } else 514df8bae1dSRodney W. Grimes for (; *p > ' '; ++p) 515df8bae1dSRodney W. Grimes continue; 516df8bae1dSRodney W. Grimes } 517df8bae1dSRodney W. Grimes if (tmp) 518791d77e0SPoul-Henning Kamp PCHAR('>'); 519df8bae1dSRodney W. Grimes break; 520df8bae1dSRodney W. Grimes case 'c': 521791d77e0SPoul-Henning Kamp PCHAR(va_arg(ap, int)); 522df8bae1dSRodney W. Grimes break; 523df8bae1dSRodney W. Grimes case 'd': 524df8bae1dSRodney W. Grimes ul = lflag ? va_arg(ap, long) : va_arg(ap, int); 525791d77e0SPoul-Henning Kamp sign = 1; 526df8bae1dSRodney W. Grimes base = 10; 527df8bae1dSRodney W. Grimes goto number; 528791d77e0SPoul-Henning Kamp case 'l': 529791d77e0SPoul-Henning Kamp lflag = 1; 530791d77e0SPoul-Henning Kamp goto reswitch; 531791d77e0SPoul-Henning Kamp case 'n': 532791d77e0SPoul-Henning Kamp ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int); 533791d77e0SPoul-Henning Kamp base = radix; 534791d77e0SPoul-Henning Kamp goto number; 535df8bae1dSRodney W. Grimes case 'o': 536df8bae1dSRodney W. Grimes ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int); 537df8bae1dSRodney W. Grimes base = 8; 538df8bae1dSRodney W. Grimes goto number; 53912d17f65SPoul-Henning Kamp case 'p': 54012d17f65SPoul-Henning Kamp ul = (u_long)va_arg(ap, void *); 54112d17f65SPoul-Henning Kamp base = 16; 542791d77e0SPoul-Henning Kamp PCHAR('0'); 543791d77e0SPoul-Henning Kamp PCHAR('x'); 54412d17f65SPoul-Henning Kamp goto number; 545791d77e0SPoul-Henning Kamp case 'r': 546791d77e0SPoul-Henning Kamp p = va_arg(ap, char *); 547fe96d47dSPoul-Henning Kamp if (!func) { 548fe96d47dSPoul-Henning Kamp n = kvprintf(p, func, d, radix, ap); 549791d77e0SPoul-Henning Kamp d += n; 550fe96d47dSPoul-Henning Kamp } else { 551fe96d47dSPoul-Henning Kamp n = kvprintf(p, func, arg, radix, ap); 552fe96d47dSPoul-Henning Kamp } 553791d77e0SPoul-Henning Kamp retval += n; 554791d77e0SPoul-Henning Kamp break; 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