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