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 394830092aSPoul-Henning Kamp * $Id: subr_prf.c,v 1.21 1996/01/15 22:40:43 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); 360791d77e0SPoul-Henning Kamp va_end(ap); 361791d77e0SPoul-Henning Kamp return retval; 362791d77e0SPoul-Henning Kamp } 363791d77e0SPoul-Henning Kamp 364791d77e0SPoul-Henning Kamp /* 365791d77e0SPoul-Henning Kamp * Put a number (base <= 16) in a buffer in reverse order; return an 366791d77e0SPoul-Henning Kamp * optional length and a pointer to the NULL terminated (preceded?) 367791d77e0SPoul-Henning Kamp * buffer. 368791d77e0SPoul-Henning Kamp */ 369791d77e0SPoul-Henning Kamp static char * 370791d77e0SPoul-Henning Kamp ksprintn(ul, base, lenp) 371791d77e0SPoul-Henning Kamp register u_long ul; 372791d77e0SPoul-Henning Kamp register int base, *lenp; 373791d77e0SPoul-Henning Kamp { /* A long in base 8, plus NULL. */ 374791d77e0SPoul-Henning Kamp static char buf[sizeof(long) * NBBY / 3 + 2]; 375791d77e0SPoul-Henning Kamp register char *p; 376791d77e0SPoul-Henning Kamp 377791d77e0SPoul-Henning Kamp p = buf; 378791d77e0SPoul-Henning Kamp do { 379791d77e0SPoul-Henning Kamp *++p = hex2ascii(ul % base); 380791d77e0SPoul-Henning Kamp } while (ul /= base); 381791d77e0SPoul-Henning Kamp if (lenp) 382791d77e0SPoul-Henning Kamp *lenp = p - buf; 383791d77e0SPoul-Henning Kamp return (p); 384791d77e0SPoul-Henning Kamp } 385791d77e0SPoul-Henning Kamp 386df8bae1dSRodney W. Grimes /* 387df8bae1dSRodney W. Grimes * Scaled down version of printf(3). 388df8bae1dSRodney W. Grimes * 389df8bae1dSRodney W. Grimes * Two additional formats: 390df8bae1dSRodney W. Grimes * 391df8bae1dSRodney W. Grimes * The format %b is supported to decode error registers. 392df8bae1dSRodney W. Grimes * Its usage is: 393df8bae1dSRodney W. Grimes * 394df8bae1dSRodney W. Grimes * printf("reg=%b\n", regval, "<base><arg>*"); 395df8bae1dSRodney W. Grimes * 396df8bae1dSRodney W. Grimes * where <base> is the output base expressed as a control character, e.g. 397df8bae1dSRodney W. Grimes * \10 gives octal; \20 gives hex. Each arg is a sequence of characters, 398df8bae1dSRodney W. Grimes * the first of which gives the bit number to be inspected (origin 1), and 399df8bae1dSRodney W. Grimes * the next characters (up to a control character, i.e. a character <= 32), 400df8bae1dSRodney W. Grimes * give the name of the register. Thus: 401df8bae1dSRodney W. Grimes * 402df8bae1dSRodney W. Grimes * kprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n"); 403df8bae1dSRodney W. Grimes * 404df8bae1dSRodney W. Grimes * would produce output: 405df8bae1dSRodney W. Grimes * 406df8bae1dSRodney W. Grimes * reg=3<BITTWO,BITONE> 407df8bae1dSRodney W. Grimes * 408df8bae1dSRodney W. Grimes * The format %r passes an additional format string and argument list 409df8bae1dSRodney W. Grimes * recursively. Its usage is: 410df8bae1dSRodney W. Grimes * 411df8bae1dSRodney W. Grimes * fn(char *fmt, ...) 412df8bae1dSRodney W. Grimes * { 413df8bae1dSRodney W. Grimes * va_list ap; 414df8bae1dSRodney W. Grimes * va_start(ap, fmt); 415df8bae1dSRodney W. Grimes * printf("prefix: %r: suffix\n", fmt, ap); 416df8bae1dSRodney W. Grimes * va_end(ap); 417df8bae1dSRodney W. Grimes * } 418df8bae1dSRodney W. Grimes * 419df8bae1dSRodney W. Grimes * Space or zero padding and a field width are supported for the numeric 420df8bae1dSRodney W. Grimes * formats only. 421df8bae1dSRodney W. Grimes */ 422791d77e0SPoul-Henning Kamp int 423791d77e0SPoul-Henning Kamp kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap) 424df8bae1dSRodney W. Grimes { 425791d77e0SPoul-Henning Kamp #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; } 426791d77e0SPoul-Henning Kamp char *p, *q, *d; 427791d77e0SPoul-Henning Kamp int ch, n; 428df8bae1dSRodney W. Grimes u_long ul; 4294830092aSPoul-Henning Kamp int base, lflag, tmp, width, ladjust, sharpflag, neg, sign, dot; 430df8bae1dSRodney W. Grimes char padc; 431791d77e0SPoul-Henning Kamp int retval = 0; 432791d77e0SPoul-Henning Kamp 433791d77e0SPoul-Henning Kamp if (func == NULL) 434791d77e0SPoul-Henning Kamp d = (char *) arg; 435791d77e0SPoul-Henning Kamp else 436791d77e0SPoul-Henning Kamp d = 0; 4378f5067baSDavid Greenman 4388f5067baSDavid Greenman if (fmt == NULL) 4392336b9d7SBruce Evans fmt = "(fmt null)\n"; 440df8bae1dSRodney W. Grimes for (;;) { 441df8bae1dSRodney W. Grimes padc = ' '; 442df8bae1dSRodney W. Grimes width = 0; 443df8bae1dSRodney W. Grimes while ((ch = *(u_char *)fmt++) != '%') { 444df8bae1dSRodney W. Grimes if (ch == '\0') 445791d77e0SPoul-Henning Kamp return retval; 446791d77e0SPoul-Henning Kamp PCHAR(ch); 447df8bae1dSRodney W. Grimes } 448df8bae1dSRodney W. Grimes lflag = 0; 449791d77e0SPoul-Henning Kamp ladjust = 0; 450791d77e0SPoul-Henning Kamp sharpflag = 0; 451791d77e0SPoul-Henning Kamp neg = 0; 452791d77e0SPoul-Henning Kamp sign = 0; 4534830092aSPoul-Henning Kamp dot = 0; 454df8bae1dSRodney W. Grimes reswitch: switch (ch = *(u_char *)fmt++) { 4554830092aSPoul-Henning Kamp case '.': 4564830092aSPoul-Henning Kamp dot = 1; 4574830092aSPoul-Henning Kamp goto reswitch; 458791d77e0SPoul-Henning Kamp case '#': 459791d77e0SPoul-Henning Kamp sharpflag = 1; 460791d77e0SPoul-Henning Kamp goto reswitch; 461791d77e0SPoul-Henning Kamp case '+': 462791d77e0SPoul-Henning Kamp sign = 1; 463791d77e0SPoul-Henning Kamp goto reswitch; 464791d77e0SPoul-Henning Kamp case '-': 465791d77e0SPoul-Henning Kamp ladjust = 1; 466791d77e0SPoul-Henning Kamp goto reswitch; 467791d77e0SPoul-Henning Kamp case '%': 468791d77e0SPoul-Henning Kamp PCHAR(ch); 469791d77e0SPoul-Henning Kamp break; 470791d77e0SPoul-Henning Kamp case '*': 471791d77e0SPoul-Henning Kamp width = va_arg(ap, int); 472791d77e0SPoul-Henning Kamp if (width < 0) { 473791d77e0SPoul-Henning Kamp ladjust = !ladjust; 474791d77e0SPoul-Henning Kamp width = -width; 475791d77e0SPoul-Henning Kamp } 476791d77e0SPoul-Henning Kamp goto reswitch; 477df8bae1dSRodney W. Grimes case '0': 478df8bae1dSRodney W. Grimes padc = '0'; 479df8bae1dSRodney W. Grimes goto reswitch; 480df8bae1dSRodney W. Grimes case '1': case '2': case '3': case '4': 481df8bae1dSRodney W. Grimes case '5': case '6': case '7': case '8': case '9': 482df8bae1dSRodney W. Grimes for (width = 0;; ++fmt) { 483df8bae1dSRodney W. Grimes width = width * 10 + ch - '0'; 484df8bae1dSRodney W. Grimes ch = *fmt; 485df8bae1dSRodney W. Grimes if (ch < '0' || ch > '9') 486df8bae1dSRodney W. Grimes break; 487df8bae1dSRodney W. Grimes } 488df8bae1dSRodney W. Grimes goto reswitch; 489df8bae1dSRodney W. Grimes case 'b': 490df8bae1dSRodney W. Grimes ul = va_arg(ap, int); 491df8bae1dSRodney W. Grimes p = va_arg(ap, char *); 492797f2d22SPoul-Henning Kamp for (q = ksprintn(ul, *p++, NULL); *q;) 493791d77e0SPoul-Henning Kamp PCHAR(*q--); 494df8bae1dSRodney W. Grimes 495df8bae1dSRodney W. Grimes if (!ul) 496df8bae1dSRodney W. Grimes break; 497df8bae1dSRodney W. Grimes 498797f2d22SPoul-Henning Kamp for (tmp = 0; *p;) { 499797f2d22SPoul-Henning Kamp n = *p++; 500df8bae1dSRodney W. Grimes if (ul & (1 << (n - 1))) { 501791d77e0SPoul-Henning Kamp PCHAR(tmp ? ',' : '<'); 502df8bae1dSRodney W. Grimes for (; (n = *p) > ' '; ++p) 503791d77e0SPoul-Henning Kamp PCHAR(n); 504df8bae1dSRodney W. Grimes tmp = 1; 505df8bae1dSRodney W. Grimes } else 506df8bae1dSRodney W. Grimes for (; *p > ' '; ++p) 507df8bae1dSRodney W. Grimes continue; 508df8bae1dSRodney W. Grimes } 509df8bae1dSRodney W. Grimes if (tmp) 510791d77e0SPoul-Henning Kamp PCHAR('>'); 511df8bae1dSRodney W. Grimes break; 512df8bae1dSRodney W. Grimes case 'c': 513791d77e0SPoul-Henning Kamp PCHAR(va_arg(ap, int)); 514df8bae1dSRodney W. Grimes break; 515df8bae1dSRodney W. Grimes case 'd': 516df8bae1dSRodney W. Grimes ul = lflag ? va_arg(ap, long) : va_arg(ap, int); 517791d77e0SPoul-Henning Kamp sign = 1; 518df8bae1dSRodney W. Grimes base = 10; 519df8bae1dSRodney W. Grimes goto number; 520791d77e0SPoul-Henning Kamp case 'l': 521791d77e0SPoul-Henning Kamp lflag = 1; 522791d77e0SPoul-Henning Kamp goto reswitch; 523791d77e0SPoul-Henning Kamp case 'n': 524791d77e0SPoul-Henning Kamp ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int); 525791d77e0SPoul-Henning Kamp base = radix; 526791d77e0SPoul-Henning Kamp goto number; 527df8bae1dSRodney W. Grimes case 'o': 528df8bae1dSRodney W. Grimes ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int); 529df8bae1dSRodney W. Grimes base = 8; 530df8bae1dSRodney W. Grimes goto number; 53112d17f65SPoul-Henning Kamp case 'p': 53212d17f65SPoul-Henning Kamp ul = (u_long)va_arg(ap, void *); 53312d17f65SPoul-Henning Kamp base = 16; 534791d77e0SPoul-Henning Kamp PCHAR('0'); 535791d77e0SPoul-Henning Kamp PCHAR('x'); 53612d17f65SPoul-Henning Kamp goto number; 537791d77e0SPoul-Henning Kamp case 'r': 538791d77e0SPoul-Henning Kamp p = va_arg(ap, char *); 539791d77e0SPoul-Henning Kamp n = kvprintf(p, func, arg, radix, ap); 540791d77e0SPoul-Henning Kamp if (!func) 541791d77e0SPoul-Henning Kamp d += n; 542791d77e0SPoul-Henning Kamp retval += n; 543791d77e0SPoul-Henning Kamp break; 544791d77e0SPoul-Henning Kamp case 's': 545791d77e0SPoul-Henning Kamp p = va_arg(ap, char *); 546791d77e0SPoul-Henning Kamp if (p == NULL) 547791d77e0SPoul-Henning Kamp p = "(null)"; 5484830092aSPoul-Henning Kamp if (!dot) 5494830092aSPoul-Henning Kamp n = strlen (p); 5504830092aSPoul-Henning Kamp else 5514830092aSPoul-Henning Kamp for (n = 0; n < width && p[n]; n++) 5524830092aSPoul-Henning Kamp continue; 5534830092aSPoul-Henning Kamp width -= n; 554791d77e0SPoul-Henning Kamp if (!ladjust && width > 0) 555791d77e0SPoul-Henning Kamp while (width--) 556791d77e0SPoul-Henning Kamp PCHAR(padc); 5574830092aSPoul-Henning Kamp while (n--) 558791d77e0SPoul-Henning Kamp PCHAR(*p++); 559791d77e0SPoul-Henning Kamp if (ladjust && width > 0) 560791d77e0SPoul-Henning Kamp while (width--) 561791d77e0SPoul-Henning Kamp PCHAR(padc); 562791d77e0SPoul-Henning Kamp break; 563f53dbe97SBruce Evans case 'u': 564f53dbe97SBruce Evans ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int); 565f53dbe97SBruce Evans base = 10; 566f53dbe97SBruce Evans goto number; 567df8bae1dSRodney W. Grimes case 'x': 568df8bae1dSRodney W. Grimes ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int); 569df8bae1dSRodney W. Grimes base = 16; 570791d77e0SPoul-Henning Kamp number: if (sign && (long)ul < 0L) { 571791d77e0SPoul-Henning Kamp neg = 1; 572791d77e0SPoul-Henning Kamp ul = -(long)ul; 573791d77e0SPoul-Henning Kamp } 574791d77e0SPoul-Henning Kamp p = ksprintn(ul, base, &tmp); 575791d77e0SPoul-Henning Kamp if (sharpflag && ul != 0) { 576791d77e0SPoul-Henning Kamp if (base == 8) 577791d77e0SPoul-Henning Kamp tmp++; 578791d77e0SPoul-Henning Kamp else if (base == 16) 579791d77e0SPoul-Henning Kamp tmp += 2; 580791d77e0SPoul-Henning Kamp } 581791d77e0SPoul-Henning Kamp if (neg) 582791d77e0SPoul-Henning Kamp tmp++; 583791d77e0SPoul-Henning Kamp 584791d77e0SPoul-Henning Kamp if (!ladjust && width && (width -= tmp) > 0) 585df8bae1dSRodney W. Grimes while (width--) 586791d77e0SPoul-Henning Kamp PCHAR(padc); 587791d77e0SPoul-Henning Kamp if (neg) 588791d77e0SPoul-Henning Kamp PCHAR('-'); 589791d77e0SPoul-Henning Kamp if (sharpflag && ul != 0) { 590791d77e0SPoul-Henning Kamp if (base == 8) { 591791d77e0SPoul-Henning Kamp PCHAR('0'); 592791d77e0SPoul-Henning Kamp } else if (base == 16) { 593791d77e0SPoul-Henning Kamp PCHAR('0'); 594791d77e0SPoul-Henning Kamp PCHAR('x'); 595791d77e0SPoul-Henning Kamp } 596791d77e0SPoul-Henning Kamp } 597791d77e0SPoul-Henning Kamp 598797f2d22SPoul-Henning Kamp while (*p) 599791d77e0SPoul-Henning Kamp PCHAR(*p--); 600791d77e0SPoul-Henning Kamp 601791d77e0SPoul-Henning Kamp if (ladjust && width && (width -= tmp) > 0) 602791d77e0SPoul-Henning Kamp while (width--) 603791d77e0SPoul-Henning Kamp PCHAR(padc); 604791d77e0SPoul-Henning Kamp 605df8bae1dSRodney W. Grimes break; 606df8bae1dSRodney W. Grimes default: 607791d77e0SPoul-Henning Kamp PCHAR('%'); 608df8bae1dSRodney W. Grimes if (lflag) 609791d77e0SPoul-Henning Kamp PCHAR('l'); 610791d77e0SPoul-Henning Kamp PCHAR(ch); 611791d77e0SPoul-Henning Kamp break; 612df8bae1dSRodney W. Grimes } 613df8bae1dSRodney W. Grimes } 614791d77e0SPoul-Henning Kamp #undef PCHAR 615df8bae1dSRodney W. Grimes } 616df8bae1dSRodney W. Grimes 617df8bae1dSRodney W. Grimes /* 618791d77e0SPoul-Henning Kamp * Put character in log buffer. 619df8bae1dSRodney W. Grimes */ 620df8bae1dSRodney W. Grimes static void 621791d77e0SPoul-Henning Kamp msglogchar(int c, void *dummyarg) 622df8bae1dSRodney W. Grimes { 623791d77e0SPoul-Henning Kamp struct msgbuf *mbp; 624df8bae1dSRodney W. Grimes 625791d77e0SPoul-Henning Kamp if (c != '\0' && c != '\r' && c != 0177 && msgbufmapped) { 626df8bae1dSRodney W. Grimes mbp = msgbufp; 6276c8897cfSDavid Greenman if (mbp->msg_magic != MSG_MAGIC || 6286c8897cfSDavid Greenman mbp->msg_bufx >= MSG_BSIZE || 6296c8897cfSDavid Greenman mbp->msg_bufr >= MSG_BSIZE) { 6306c8897cfSDavid Greenman bzero(mbp, sizeof(struct msgbuf)); 631df8bae1dSRodney W. Grimes mbp->msg_magic = MSG_MAGIC; 632df8bae1dSRodney W. Grimes } 633df8bae1dSRodney W. Grimes mbp->msg_bufc[mbp->msg_bufx++] = c; 634b4224c9cSDavid Greenman if (mbp->msg_bufx >= MSG_BSIZE) 635df8bae1dSRodney W. Grimes mbp->msg_bufx = 0; 63602d5c7b1SDavid Greenman /* If the buffer is full, keep the most recent data. */ 637a3f4faceSBruce Evans if (mbp->msg_bufr == mbp->msg_bufx) { 63802d5c7b1SDavid Greenman if (++mbp->msg_bufr >= MSG_BSIZE) 639a3f4faceSBruce Evans mbp->msg_bufr = 0; 640a3f4faceSBruce Evans } 641df8bae1dSRodney W. Grimes } 642df8bae1dSRodney W. Grimes } 643