subr_prf.c revision 1.42 1 1.42 thorpej /* $NetBSD: subr_prf.c,v 1.42 1997/06/26 00:43:10 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.42 thorpej #include <sys/kgdb.h>
74 1.22 christos #include <machine/cpu.h>
75 1.12 cgd #endif
76 1.20 christos
77 1.12 cgd #define TOCONS 0x01
78 1.12 cgd #define TOTTY 0x02
79 1.12 cgd #define TOLOG 0x04
80 1.12 cgd
81 1.37 thorpej /*
82 1.37 thorpej * This is the size of the buffer that should be passed to ksnprintn().
83 1.37 thorpej * It's the length of a long in base 8, plus NULL.
84 1.37 thorpej */
85 1.41 drochner #define KSNPRINTN_BUFSIZE (sizeof(quad_t) * NBBY / 3 + 2)
86 1.37 thorpej
87 1.12 cgd struct tty *constty; /* pointer to console "window" tty */
88 1.12 cgd
89 1.20 christos void (*v_putc) __P((int)) = cnputc; /* routine to putc on virtual console */
90 1.12 cgd
91 1.20 christos static void putchar __P((int, int, struct tty *));
92 1.41 drochner static char *ksnprintn __P((u_quad_t, int, int *, char *, size_t));
93 1.30 christos void kprintf __P((const char *, int, struct tty *, va_list));
94 1.12 cgd
95 1.14 cgd int consintr = 1; /* Ok to handle console interrupts? */
96 1.12 cgd
97 1.12 cgd /*
98 1.14 cgd * Variable panicstr contains argument to first call to panic; used as flag
99 1.14 cgd * to indicate that the kernel has already called panic.
100 1.12 cgd */
101 1.14 cgd const char *panicstr;
102 1.12 cgd
103 1.12 cgd /*
104 1.12 cgd * Panic is called on unresolvable fatal errors. It prints "panic: mesg",
105 1.12 cgd * and then reboots. If we are called twice, then we avoid trying to sync
106 1.12 cgd * the disks as this often leads to recursive panics.
107 1.12 cgd */
108 1.14 cgd void
109 1.12 cgd #ifdef __STDC__
110 1.12 cgd panic(const char *fmt, ...)
111 1.12 cgd #else
112 1.14 cgd panic(fmt, va_alist)
113 1.12 cgd char *fmt;
114 1.20 christos va_dcl
115 1.12 cgd #endif
116 1.12 cgd {
117 1.14 cgd int bootopt;
118 1.12 cgd va_list ap;
119 1.12 cgd
120 1.14 cgd bootopt = RB_AUTOBOOT | RB_DUMP;
121 1.12 cgd if (panicstr)
122 1.12 cgd bootopt |= RB_NOSYNC;
123 1.12 cgd else
124 1.12 cgd panicstr = fmt;
125 1.12 cgd
126 1.12 cgd va_start(ap, fmt);
127 1.40 thorpej #ifdef __powerpc__ /* XXX */
128 1.40 thorpej printf("panic: "); /* XXX */
129 1.40 thorpej vprintf(fmt, ap); /* XXX */
130 1.40 thorpej printf("\n"); /* XXX */
131 1.40 thorpej #else /* XXX */
132 1.40 thorpej printf("panic: %:\n", fmt, ap); /* XXX */
133 1.40 thorpej #endif /* XXX */
134 1.12 cgd va_end(ap);
135 1.12 cgd
136 1.32 ws #if NIPKDB > 0
137 1.32 ws ipkdb_panic();
138 1.28 ws #endif
139 1.12 cgd #ifdef KGDB
140 1.12 cgd kgdb_panic();
141 1.12 cgd #endif
142 1.12 cgd #ifdef KADB
143 1.14 cgd if (boothowto & RB_KDB)
144 1.14 cgd kdbpanic();
145 1.12 cgd #endif
146 1.12 cgd #ifdef DDB
147 1.38 thorpej if (db_onpanic)
148 1.38 thorpej Debugger();
149 1.12 cgd #endif
150 1.39 gwr cpu_reboot(bootopt, NULL);
151 1.12 cgd }
152 1.12 cgd
153 1.12 cgd /*
154 1.12 cgd * Warn that a system table is full.
155 1.12 cgd */
156 1.12 cgd void
157 1.12 cgd tablefull(tab)
158 1.12 cgd const char *tab;
159 1.12 cgd {
160 1.12 cgd
161 1.12 cgd log(LOG_ERR, "%s: table is full\n", tab);
162 1.12 cgd }
163 1.12 cgd
164 1.12 cgd /*
165 1.12 cgd * Uprintf prints to the controlling terminal for the current process.
166 1.12 cgd * It may block if the tty queue is overfull. No message is printed if
167 1.12 cgd * the queue does not clear in a reasonable time.
168 1.12 cgd */
169 1.12 cgd void
170 1.12 cgd #ifdef __STDC__
171 1.12 cgd uprintf(const char *fmt, ...)
172 1.12 cgd #else
173 1.14 cgd uprintf(fmt, va_alist)
174 1.12 cgd char *fmt;
175 1.20 christos va_dcl
176 1.12 cgd #endif
177 1.12 cgd {
178 1.12 cgd register struct proc *p = curproc;
179 1.12 cgd va_list ap;
180 1.12 cgd
181 1.12 cgd if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
182 1.12 cgd va_start(ap, fmt);
183 1.30 christos kprintf(fmt, TOTTY, p->p_session->s_ttyp, ap);
184 1.12 cgd va_end(ap);
185 1.12 cgd }
186 1.12 cgd }
187 1.12 cgd
188 1.12 cgd tpr_t
189 1.12 cgd tprintf_open(p)
190 1.12 cgd register struct proc *p;
191 1.12 cgd {
192 1.12 cgd
193 1.12 cgd if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
194 1.12 cgd SESSHOLD(p->p_session);
195 1.12 cgd return ((tpr_t) p->p_session);
196 1.12 cgd }
197 1.12 cgd return ((tpr_t) NULL);
198 1.12 cgd }
199 1.12 cgd
200 1.12 cgd void
201 1.12 cgd tprintf_close(sess)
202 1.12 cgd tpr_t sess;
203 1.12 cgd {
204 1.12 cgd
205 1.12 cgd if (sess)
206 1.12 cgd SESSRELE((struct session *) sess);
207 1.12 cgd }
208 1.12 cgd
209 1.12 cgd /*
210 1.12 cgd * tprintf prints on the controlling terminal associated
211 1.12 cgd * with the given session.
212 1.12 cgd */
213 1.12 cgd void
214 1.12 cgd #ifdef __STDC__
215 1.12 cgd tprintf(tpr_t tpr, const char *fmt, ...)
216 1.12 cgd #else
217 1.14 cgd tprintf(tpr, fmt, va_alist)
218 1.12 cgd tpr_t tpr;
219 1.12 cgd char *fmt;
220 1.20 christos va_dcl
221 1.12 cgd #endif
222 1.12 cgd {
223 1.12 cgd register struct session *sess = (struct session *)tpr;
224 1.12 cgd struct tty *tp = NULL;
225 1.12 cgd int flags = TOLOG;
226 1.12 cgd va_list ap;
227 1.12 cgd
228 1.12 cgd logpri(LOG_INFO);
229 1.12 cgd if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
230 1.12 cgd flags |= TOTTY;
231 1.12 cgd tp = sess->s_ttyp;
232 1.12 cgd }
233 1.12 cgd va_start(ap, fmt);
234 1.30 christos kprintf(fmt, flags, tp, ap);
235 1.12 cgd va_end(ap);
236 1.12 cgd logwakeup();
237 1.12 cgd }
238 1.12 cgd
239 1.12 cgd /*
240 1.12 cgd * Ttyprintf displays a message on a tty; it should be used only by
241 1.12 cgd * the tty driver, or anything that knows the underlying tty will not
242 1.12 cgd * be revoke(2)'d away. Other callers should use tprintf.
243 1.12 cgd */
244 1.12 cgd void
245 1.12 cgd #ifdef __STDC__
246 1.12 cgd ttyprintf(struct tty *tp, const char *fmt, ...)
247 1.12 cgd #else
248 1.14 cgd ttyprintf(tp, fmt, va_alist)
249 1.12 cgd struct tty *tp;
250 1.12 cgd char *fmt;
251 1.20 christos va_dcl
252 1.12 cgd #endif
253 1.12 cgd {
254 1.12 cgd va_list ap;
255 1.12 cgd
256 1.12 cgd va_start(ap, fmt);
257 1.30 christos kprintf(fmt, TOTTY, tp, ap);
258 1.12 cgd va_end(ap);
259 1.12 cgd }
260 1.12 cgd
261 1.12 cgd extern int log_open;
262 1.12 cgd
263 1.12 cgd /*
264 1.12 cgd * Log writes to the log buffer, and guarantees not to sleep (so can be
265 1.12 cgd * called by interrupt routines). If there is no process reading the
266 1.12 cgd * log yet, it writes to the console also.
267 1.12 cgd */
268 1.12 cgd void
269 1.12 cgd #ifdef __STDC__
270 1.12 cgd log(int level, const char *fmt, ...)
271 1.12 cgd #else
272 1.14 cgd log(level, fmt, va_alist)
273 1.12 cgd int level;
274 1.12 cgd char *fmt;
275 1.20 christos va_dcl
276 1.12 cgd #endif
277 1.12 cgd {
278 1.12 cgd register int s;
279 1.12 cgd va_list ap;
280 1.12 cgd
281 1.12 cgd s = splhigh();
282 1.12 cgd logpri(level);
283 1.12 cgd va_start(ap, fmt);
284 1.30 christos kprintf(fmt, TOLOG, NULL, ap);
285 1.12 cgd splx(s);
286 1.12 cgd va_end(ap);
287 1.12 cgd if (!log_open) {
288 1.12 cgd va_start(ap, fmt);
289 1.30 christos kprintf(fmt, TOCONS, NULL, ap);
290 1.12 cgd va_end(ap);
291 1.12 cgd }
292 1.12 cgd logwakeup();
293 1.12 cgd }
294 1.12 cgd
295 1.13 mycroft void
296 1.12 cgd logpri(level)
297 1.12 cgd int level;
298 1.12 cgd {
299 1.12 cgd register int ch;
300 1.12 cgd register char *p;
301 1.37 thorpej char snbuf[KSNPRINTN_BUFSIZE];
302 1.12 cgd
303 1.12 cgd putchar('<', TOLOG, NULL);
304 1.37 thorpej for (p = ksnprintn((u_long)level, 10, NULL, snbuf, sizeof(snbuf));
305 1.37 thorpej (ch = *p--) != 0;)
306 1.12 cgd putchar(ch, TOLOG, NULL);
307 1.12 cgd putchar('>', TOLOG, NULL);
308 1.12 cgd }
309 1.12 cgd
310 1.12 cgd void
311 1.12 cgd #ifdef __STDC__
312 1.12 cgd addlog(const char *fmt, ...)
313 1.12 cgd #else
314 1.14 cgd addlog(fmt, va_alist)
315 1.12 cgd char *fmt;
316 1.20 christos va_dcl
317 1.12 cgd #endif
318 1.12 cgd {
319 1.12 cgd register int s;
320 1.12 cgd va_list ap;
321 1.12 cgd
322 1.12 cgd s = splhigh();
323 1.12 cgd va_start(ap, fmt);
324 1.30 christos kprintf(fmt, TOLOG, NULL, ap);
325 1.12 cgd splx(s);
326 1.12 cgd va_end(ap);
327 1.12 cgd if (!log_open) {
328 1.12 cgd va_start(ap, fmt);
329 1.30 christos kprintf(fmt, TOCONS, NULL, ap);
330 1.12 cgd va_end(ap);
331 1.12 cgd }
332 1.12 cgd logwakeup();
333 1.12 cgd }
334 1.12 cgd
335 1.12 cgd void
336 1.12 cgd #ifdef __STDC__
337 1.30 christos printf(const char *fmt, ...)
338 1.12 cgd #else
339 1.30 christos printf(fmt, va_alist)
340 1.12 cgd char *fmt;
341 1.20 christos va_dcl
342 1.12 cgd #endif
343 1.12 cgd {
344 1.12 cgd va_list ap;
345 1.12 cgd register int savintr;
346 1.12 cgd
347 1.12 cgd savintr = consintr; /* disable interrupts */
348 1.12 cgd consintr = 0;
349 1.12 cgd va_start(ap, fmt);
350 1.30 christos kprintf(fmt, TOCONS | TOLOG, NULL, ap);
351 1.12 cgd va_end(ap);
352 1.12 cgd if (!panicstr)
353 1.12 cgd logwakeup();
354 1.12 cgd consintr = savintr; /* reenable interrupts */
355 1.12 cgd }
356 1.12 cgd
357 1.40 thorpej #ifdef __powerpc__ /* XXX XXX XXX */
358 1.40 thorpej void
359 1.40 thorpej vprintf(fmt, ap)
360 1.40 thorpej const char *fmt;
361 1.40 thorpej va_list ap;
362 1.40 thorpej {
363 1.40 thorpej register int savintr;
364 1.40 thorpej
365 1.40 thorpej savintr = consintr; /* disable interrupts */
366 1.40 thorpej consintr = 0;
367 1.40 thorpej kprintf(fmt, TOCONS | TOLOG, NULL, ap);
368 1.40 thorpej if (!panicstr)
369 1.40 thorpej logwakeup();
370 1.40 thorpej consintr = savintr; /* reenable interrupts */
371 1.40 thorpej }
372 1.40 thorpej #endif /* __powerpc__ */ /* XXX XXX XXX */
373 1.40 thorpej
374 1.12 cgd /*
375 1.12 cgd * Scaled down version of printf(3).
376 1.12 cgd *
377 1.12 cgd * Two additional formats:
378 1.12 cgd *
379 1.12 cgd * The format %b is supported to decode error registers.
380 1.12 cgd * Its usage is:
381 1.12 cgd *
382 1.30 christos * printf("reg=%b\n", regval, "<base><arg>*");
383 1.12 cgd *
384 1.12 cgd * where <base> is the output base expressed as a control character, e.g.
385 1.12 cgd * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
386 1.12 cgd * the first of which gives the bit number to be inspected (origin 1), and
387 1.12 cgd * the next characters (up to a control character, i.e. a character <= 32),
388 1.12 cgd * give the name of the register. Thus:
389 1.12 cgd *
390 1.30 christos * kprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
391 1.12 cgd *
392 1.12 cgd * would produce output:
393 1.12 cgd *
394 1.12 cgd * reg=3<BITTWO,BITONE>
395 1.12 cgd *
396 1.24 christos * The format %: passes an additional format string and argument list
397 1.14 cgd * recursively. Its usage is:
398 1.12 cgd *
399 1.14 cgd * fn(char *fmt, ...)
400 1.12 cgd * {
401 1.12 cgd * va_list ap;
402 1.12 cgd * va_start(ap, fmt);
403 1.30 christos * printf("prefix: %: suffix\n", fmt, ap);
404 1.12 cgd * va_end(ap);
405 1.14 cgd * }
406 1.12 cgd *
407 1.12 cgd * Space or zero padding and a field width are supported for the numeric
408 1.12 cgd * formats only.
409 1.12 cgd */
410 1.41 drochner #define LONGINT 0x010 /* long integer */
411 1.41 drochner #define QUADINT 0x020 /* quad integer */
412 1.41 drochner #define SARG() \
413 1.41 drochner (flags&QUADINT ? va_arg(ap, quad_t) : \
414 1.41 drochner flags&LONGINT ? va_arg(ap, long) : \
415 1.41 drochner (long)va_arg(ap, int))
416 1.41 drochner #define UARG() \
417 1.41 drochner (flags&QUADINT ? va_arg(ap, u_quad_t) : \
418 1.41 drochner flags&LONGINT ? va_arg(ap, u_long) : \
419 1.41 drochner (u_long)va_arg(ap, u_int))
420 1.12 cgd void
421 1.41 drochner kprintf(fmt, oflags, tp, ap)
422 1.12 cgd register const char *fmt;
423 1.41 drochner int oflags;
424 1.12 cgd struct tty *tp;
425 1.12 cgd va_list ap;
426 1.12 cgd {
427 1.14 cgd register char *p, *q;
428 1.12 cgd register int ch, n;
429 1.41 drochner u_quad_t ul;
430 1.41 drochner int base, flags, tmp, width;
431 1.37 thorpej char padc, snbuf[KSNPRINTN_BUFSIZE];
432 1.12 cgd
433 1.12 cgd for (;;) {
434 1.12 cgd padc = ' ';
435 1.12 cgd width = 0;
436 1.33 cgd while ((ch = *(const u_char *)fmt++) != '%') {
437 1.12 cgd if (ch == '\0')
438 1.12 cgd return;
439 1.41 drochner putchar(ch, oflags, tp);
440 1.12 cgd }
441 1.41 drochner flags = 0;
442 1.33 cgd reswitch: switch (ch = *(const u_char *)fmt++) {
443 1.12 cgd case '0':
444 1.27 christos case '.':
445 1.12 cgd padc = '0';
446 1.12 cgd goto reswitch;
447 1.12 cgd case '1': case '2': case '3': case '4':
448 1.12 cgd case '5': case '6': case '7': case '8': case '9':
449 1.12 cgd for (width = 0;; ++fmt) {
450 1.12 cgd width = width * 10 + ch - '0';
451 1.12 cgd ch = *fmt;
452 1.12 cgd if (ch < '0' || ch > '9')
453 1.12 cgd break;
454 1.12 cgd }
455 1.12 cgd goto reswitch;
456 1.12 cgd case 'l':
457 1.41 drochner flags |= LONGINT;
458 1.41 drochner goto reswitch;
459 1.41 drochner case 'q':
460 1.41 drochner flags |= QUADINT;
461 1.12 cgd goto reswitch;
462 1.12 cgd case 'b':
463 1.12 cgd ul = va_arg(ap, int);
464 1.12 cgd p = va_arg(ap, char *);
465 1.37 thorpej for (q = ksnprintn(ul, *p++, NULL, snbuf,
466 1.37 thorpej sizeof(snbuf)); (ch = *q--) != 0;)
467 1.41 drochner putchar(ch, oflags, tp);
468 1.12 cgd
469 1.12 cgd if (!ul)
470 1.12 cgd break;
471 1.12 cgd
472 1.20 christos for (tmp = 0; (n = *p++) != 0;) {
473 1.12 cgd if (ul & (1 << (n - 1))) {
474 1.41 drochner putchar(tmp ? ',' : '<', oflags, tp);
475 1.12 cgd for (; (n = *p) > ' '; ++p)
476 1.41 drochner putchar(n, oflags, tp);
477 1.12 cgd tmp = 1;
478 1.12 cgd } else
479 1.14 cgd for (; *p > ' '; ++p)
480 1.14 cgd continue;
481 1.12 cgd }
482 1.12 cgd if (tmp)
483 1.41 drochner putchar('>', oflags, tp);
484 1.12 cgd break;
485 1.12 cgd case 'c':
486 1.41 drochner putchar(va_arg(ap, int), oflags, tp);
487 1.12 cgd break;
488 1.40 thorpej #ifndef __powerpc__ /* XXX XXX XXX */
489 1.24 christos case ':':
490 1.12 cgd p = va_arg(ap, char *);
491 1.41 drochner kprintf(p, oflags, tp, va_arg(ap, va_list));
492 1.12 cgd break;
493 1.40 thorpej #endif /* __powerpc__ */ /* XXX XXX XXX */
494 1.12 cgd case 's':
495 1.16 mycroft if ((p = va_arg(ap, char *)) == NULL)
496 1.16 mycroft p = "(null)";
497 1.20 christos while ((ch = *p++) != 0)
498 1.41 drochner putchar(ch, oflags, tp);
499 1.12 cgd break;
500 1.12 cgd case 'd':
501 1.41 drochner ul = SARG();
502 1.41 drochner if ((quad_t)ul < 0) {
503 1.41 drochner putchar('-', oflags, tp);
504 1.41 drochner ul = -ul;
505 1.12 cgd }
506 1.12 cgd base = 10;
507 1.12 cgd goto number;
508 1.12 cgd case 'o':
509 1.41 drochner ul = UARG();
510 1.12 cgd base = 8;
511 1.12 cgd goto number;
512 1.12 cgd case 'u':
513 1.41 drochner ul = UARG();
514 1.12 cgd base = 10;
515 1.12 cgd goto number;
516 1.17 mycroft case 'p':
517 1.41 drochner putchar('0', oflags, tp);
518 1.41 drochner putchar('x', oflags, tp);
519 1.18 mycroft ul = (u_long)va_arg(ap, void *);
520 1.18 mycroft base = 16;
521 1.18 mycroft goto number;
522 1.12 cgd case 'x':
523 1.41 drochner ul = UARG();
524 1.12 cgd base = 16;
525 1.37 thorpej number: p = ksnprintn(ul, base, &tmp, snbuf, sizeof(snbuf));
526 1.12 cgd if (width && (width -= tmp) > 0)
527 1.12 cgd while (width--)
528 1.41 drochner putchar(padc, oflags, tp);
529 1.20 christos while ((ch = *p--) != 0)
530 1.41 drochner putchar(ch, oflags, tp);
531 1.12 cgd break;
532 1.12 cgd default:
533 1.41 drochner putchar('%', oflags, tp);
534 1.41 drochner /* flags??? */
535 1.12 cgd /* FALLTHROUGH */
536 1.12 cgd case '%':
537 1.41 drochner putchar(ch, oflags, tp);
538 1.12 cgd }
539 1.12 cgd }
540 1.12 cgd }
541 1.12 cgd
542 1.12 cgd /*
543 1.12 cgd * Print a character on console or users terminal. If destination is
544 1.12 cgd * the console then the last MSGBUFS characters are saved in msgbuf for
545 1.12 cgd * inspection later.
546 1.12 cgd */
547 1.12 cgd static void
548 1.12 cgd putchar(c, flags, tp)
549 1.12 cgd register int c;
550 1.12 cgd int flags;
551 1.12 cgd struct tty *tp;
552 1.12 cgd {
553 1.14 cgd extern int msgbufmapped;
554 1.12 cgd register struct msgbuf *mbp;
555 1.12 cgd
556 1.12 cgd if (panicstr)
557 1.12 cgd constty = NULL;
558 1.12 cgd if ((flags & TOCONS) && tp == NULL && constty) {
559 1.12 cgd tp = constty;
560 1.12 cgd flags |= TOTTY;
561 1.12 cgd }
562 1.12 cgd if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 &&
563 1.12 cgd (flags & TOCONS) && tp == constty)
564 1.12 cgd constty = NULL;
565 1.12 cgd if ((flags & TOLOG) &&
566 1.12 cgd c != '\0' && c != '\r' && c != 0177 && msgbufmapped) {
567 1.12 cgd mbp = msgbufp;
568 1.12 cgd if (mbp->msg_magic != MSG_MAGIC) {
569 1.12 cgd bzero((caddr_t)mbp, sizeof(*mbp));
570 1.12 cgd mbp->msg_magic = MSG_MAGIC;
571 1.12 cgd }
572 1.12 cgd mbp->msg_bufc[mbp->msg_bufx++] = c;
573 1.12 cgd if (mbp->msg_bufx < 0 || mbp->msg_bufx >= MSG_BSIZE)
574 1.12 cgd mbp->msg_bufx = 0;
575 1.12 cgd }
576 1.12 cgd if ((flags & TOCONS) && constty == NULL && c != '\0')
577 1.12 cgd (*v_putc)(c);
578 1.12 cgd }
579 1.12 cgd
580 1.12 cgd /*
581 1.12 cgd * Scaled down version of sprintf(3).
582 1.12 cgd */
583 1.20 christos int
584 1.12 cgd #ifdef __STDC__
585 1.30 christos sprintf(char *buf, const char *cfmt, ...)
586 1.12 cgd #else
587 1.30 christos sprintf(buf, cfmt, va_alist)
588 1.34 gwr char *buf;
589 1.36 gwr const char *cfmt;
590 1.20 christos va_dcl
591 1.12 cgd #endif
592 1.12 cgd {
593 1.36 gwr register const char *fmt = cfmt;
594 1.12 cgd register char *p, *bp;
595 1.12 cgd register int ch, base;
596 1.41 drochner u_quad_t ul;
597 1.41 drochner int flags, tmp, width;
598 1.12 cgd va_list ap;
599 1.37 thorpej char padc, snbuf[KSNPRINTN_BUFSIZE];
600 1.12 cgd
601 1.12 cgd va_start(ap, cfmt);
602 1.12 cgd for (bp = buf; ; ) {
603 1.19 cgd padc = ' ';
604 1.19 cgd width = 0;
605 1.33 cgd while ((ch = *(const u_char *)fmt++) != '%')
606 1.12 cgd if ((*bp++ = ch) == '\0')
607 1.12 cgd return ((bp - buf) - 1);
608 1.12 cgd
609 1.41 drochner flags = 0;
610 1.33 cgd reswitch: switch (ch = *(const u_char *)fmt++) {
611 1.19 cgd case '0':
612 1.19 cgd padc = '0';
613 1.19 cgd goto reswitch;
614 1.19 cgd case '1': case '2': case '3': case '4':
615 1.19 cgd case '5': case '6': case '7': case '8': case '9':
616 1.19 cgd for (width = 0;; ++fmt) {
617 1.19 cgd width = width * 10 + ch - '0';
618 1.19 cgd ch = *fmt;
619 1.19 cgd if (ch < '0' || ch > '9')
620 1.19 cgd break;
621 1.19 cgd }
622 1.19 cgd goto reswitch;
623 1.12 cgd case 'l':
624 1.41 drochner flags |= LONGINT;
625 1.41 drochner goto reswitch;
626 1.41 drochner case 'q':
627 1.41 drochner flags |= QUADINT;
628 1.12 cgd goto reswitch;
629 1.19 cgd /* case 'b': ... break; XXX */
630 1.12 cgd case 'c':
631 1.12 cgd *bp++ = va_arg(ap, int);
632 1.12 cgd break;
633 1.19 cgd /* case 'r': ... break; XXX */
634 1.12 cgd case 's':
635 1.12 cgd p = va_arg(ap, char *);
636 1.20 christos while ((*bp++ = *p++) != 0)
637 1.14 cgd continue;
638 1.12 cgd --bp;
639 1.12 cgd break;
640 1.12 cgd case 'd':
641 1.41 drochner ul = SARG();
642 1.41 drochner if ((quad_t)ul < 0) {
643 1.12 cgd *bp++ = '-';
644 1.41 drochner ul = -ul;
645 1.12 cgd }
646 1.12 cgd base = 10;
647 1.12 cgd goto number;
648 1.12 cgd case 'o':
649 1.41 drochner ul = UARG();
650 1.12 cgd base = 8;
651 1.12 cgd goto number;
652 1.12 cgd case 'u':
653 1.41 drochner ul = UARG();
654 1.12 cgd base = 10;
655 1.12 cgd goto number;
656 1.19 cgd case 'p':
657 1.19 cgd *bp++ = '0';
658 1.19 cgd *bp++ = 'x';
659 1.19 cgd ul = (u_long)va_arg(ap, void *);
660 1.19 cgd base = 16;
661 1.19 cgd goto number;
662 1.12 cgd case 'x':
663 1.41 drochner ul = UARG();
664 1.12 cgd base = 16;
665 1.37 thorpej number: p = ksnprintn(ul, base, &tmp, snbuf, sizeof(snbuf));
666 1.19 cgd if (width && (width -= tmp) > 0)
667 1.19 cgd while (width--)
668 1.19 cgd *bp++ = padc;
669 1.20 christos while ((ch = *p--) != 0)
670 1.12 cgd *bp++ = ch;
671 1.12 cgd break;
672 1.12 cgd default:
673 1.12 cgd *bp++ = '%';
674 1.41 drochner /* flags??? */
675 1.12 cgd /* FALLTHROUGH */
676 1.12 cgd case '%':
677 1.12 cgd *bp++ = ch;
678 1.12 cgd }
679 1.12 cgd }
680 1.12 cgd va_end(ap);
681 1.12 cgd }
682 1.12 cgd
683 1.12 cgd /*
684 1.12 cgd * Put a number (base <= 16) in a buffer in reverse order; return an
685 1.12 cgd * optional length and a pointer to the NULL terminated (preceded?)
686 1.12 cgd * buffer.
687 1.12 cgd */
688 1.12 cgd static char *
689 1.37 thorpej ksnprintn(ul, base, lenp, buf, buflen)
690 1.41 drochner register u_quad_t ul;
691 1.12 cgd register int base, *lenp;
692 1.37 thorpej char *buf;
693 1.37 thorpej size_t buflen;
694 1.37 thorpej {
695 1.12 cgd register char *p;
696 1.12 cgd
697 1.12 cgd p = buf;
698 1.37 thorpej *p = '\0'; /* ensure NULL `termination' */
699 1.37 thorpej
700 1.37 thorpej /*
701 1.37 thorpej * Don't even bother of the buffer's not big enough. No
702 1.37 thorpej * value at all is better than a wrong value, and we
703 1.37 thorpej * have a lot of control over the buffer that's passed
704 1.37 thorpej * to this function, since it's not exported.
705 1.37 thorpej */
706 1.37 thorpej if (buflen < KSNPRINTN_BUFSIZE)
707 1.37 thorpej return (p);
708 1.37 thorpej
709 1.12 cgd do {
710 1.12 cgd *++p = "0123456789abcdef"[ul % base];
711 1.12 cgd } while (ul /= base);
712 1.12 cgd if (lenp)
713 1.12 cgd *lenp = p - buf;
714 1.12 cgd return (p);
715 1.37 thorpej }
716 1.37 thorpej
717 1.37 thorpej /*
718 1.37 thorpej * Print a bitmask into the provided buffer, and return a pointer
719 1.37 thorpej * to that buffer.
720 1.37 thorpej */
721 1.37 thorpej char *
722 1.37 thorpej bitmask_snprintf(ul, p, buf, buflen)
723 1.37 thorpej u_long ul;
724 1.37 thorpej const char *p;
725 1.37 thorpej char *buf;
726 1.37 thorpej size_t buflen;
727 1.37 thorpej {
728 1.37 thorpej char *bp, *q;
729 1.37 thorpej size_t left;
730 1.37 thorpej register int n;
731 1.37 thorpej int ch, tmp;
732 1.37 thorpej char snbuf[KSNPRINTN_BUFSIZE];
733 1.37 thorpej
734 1.37 thorpej bp = buf;
735 1.37 thorpej bzero(buf, buflen);
736 1.37 thorpej
737 1.37 thorpej /*
738 1.37 thorpej * Always leave room for the trailing NULL.
739 1.37 thorpej */
740 1.37 thorpej left = buflen - 1;
741 1.37 thorpej
742 1.37 thorpej /*
743 1.37 thorpej * Print the value into the buffer. Abort if there's not
744 1.37 thorpej * enough room.
745 1.37 thorpej */
746 1.37 thorpej if (buflen < KSNPRINTN_BUFSIZE)
747 1.37 thorpej return (buf);
748 1.37 thorpej
749 1.37 thorpej for (q = ksnprintn(ul, *p++, NULL, snbuf, sizeof(snbuf));
750 1.37 thorpej (ch = *q--) != 0;) {
751 1.37 thorpej *bp++ = ch;
752 1.37 thorpej left--;
753 1.37 thorpej }
754 1.37 thorpej
755 1.37 thorpej /*
756 1.37 thorpej * If the value we printed was 0, or if we don't have room for
757 1.37 thorpej * "<x>", we're done.
758 1.37 thorpej */
759 1.37 thorpej if (ul == 0 || left < 3)
760 1.37 thorpej return (buf);
761 1.37 thorpej
762 1.37 thorpej #define PUTBYTE(b, c, l) \
763 1.37 thorpej *(b)++ = (c); \
764 1.37 thorpej if (--(l) == 0) \
765 1.37 thorpej goto out;
766 1.37 thorpej
767 1.37 thorpej for (tmp = 0; (n = *p++) != 0;) {
768 1.37 thorpej if (ul & (1 << (n - 1))) {
769 1.37 thorpej PUTBYTE(bp, tmp ? ',' : '<', left);
770 1.37 thorpej for (; (n = *p) > ' '; ++p) {
771 1.37 thorpej PUTBYTE(bp, n, left);
772 1.37 thorpej }
773 1.37 thorpej tmp = 1;
774 1.37 thorpej } else
775 1.37 thorpej for (; *p > ' '; ++p)
776 1.37 thorpej continue;
777 1.37 thorpej }
778 1.37 thorpej if (tmp)
779 1.37 thorpej *bp = '>';
780 1.37 thorpej
781 1.37 thorpej #undef PUTBYTE
782 1.37 thorpej
783 1.37 thorpej out:
784 1.37 thorpej return (buf);
785 1.12 cgd }
786