subr_prf.c revision 1.52 1 1.52 mrg /* $NetBSD: subr_prf.c,v 1.52 1998/08/31 02:47:00 mrg 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.48 fvdl * @(#)subr_prf.c 8.4 (Berkeley) 5/4/95
41 1.12 cgd */
42 1.49 jonathan #include "opt_ddb.h"
43 1.49 jonathan #include "ipkdb.h"
44 1.12 cgd
45 1.12 cgd #include <sys/param.h>
46 1.12 cgd #include <sys/systm.h>
47 1.12 cgd #include <sys/buf.h>
48 1.12 cgd #include <sys/conf.h>
49 1.12 cgd #include <sys/reboot.h>
50 1.12 cgd #include <sys/msgbuf.h>
51 1.12 cgd #include <sys/proc.h>
52 1.12 cgd #include <sys/ioctl.h>
53 1.12 cgd #include <sys/vnode.h>
54 1.12 cgd #include <sys/file.h>
55 1.12 cgd #include <sys/tty.h>
56 1.12 cgd #include <sys/tprintf.h>
57 1.12 cgd #include <sys/syslog.h>
58 1.12 cgd #include <sys/malloc.h>
59 1.12 cgd
60 1.20 christos #include <dev/cons.h>
61 1.20 christos
62 1.49 jonathan #ifdef DDB
63 1.49 jonathan #include <ddb/ddbvar.h>
64 1.49 jonathan #endif
65 1.49 jonathan
66 1.49 jonathan
67 1.12 cgd /*
68 1.45 chuck * note that stdarg.h and the ansi style va_start macro is used for both
69 1.45 chuck * ansi and traditional c complers.
70 1.45 chuck * XXX: this requires that stdarg.h define: va_alist and va_dcl
71 1.12 cgd */
72 1.12 cgd #include <machine/stdarg.h>
73 1.12 cgd
74 1.28 ws
75 1.22 christos #ifdef KGDB
76 1.42 thorpej #include <sys/kgdb.h>
77 1.22 christos #include <machine/cpu.h>
78 1.12 cgd #endif
79 1.45 chuck #ifdef DDB
80 1.45 chuck #include <ddb/db_output.h> /* db_printf, db_putchar prototypes */
81 1.45 chuck extern int db_radix; /* XXX: for non-standard '%r' format */
82 1.45 chuck #endif
83 1.45 chuck
84 1.45 chuck
85 1.45 chuck /*
86 1.45 chuck * defines
87 1.45 chuck */
88 1.45 chuck
89 1.45 chuck /* flags for kprintf */
90 1.45 chuck #define TOCONS 0x01 /* to the console */
91 1.45 chuck #define TOTTY 0x02 /* to the process' tty */
92 1.45 chuck #define TOLOG 0x04 /* to the kernel message buffer */
93 1.45 chuck #define TOBUFONLY 0x08 /* to the buffer (only) [for sprintf] */
94 1.45 chuck #define TODDB 0x10 /* to ddb console */
95 1.45 chuck
96 1.45 chuck /* max size buffer kprintf needs to print quad_t [size in base 8 + \0] */
97 1.45 chuck #define KPRINTF_BUFSIZE (sizeof(quad_t) * NBBY / 3 + 2)
98 1.45 chuck
99 1.45 chuck
100 1.45 chuck /*
101 1.45 chuck * local prototypes
102 1.45 chuck */
103 1.45 chuck
104 1.45 chuck static int kprintf __P((const char *, int, struct tty *,
105 1.45 chuck char *, va_list));
106 1.45 chuck static void putchar __P((int, int, struct tty *));
107 1.20 christos
108 1.12 cgd
109 1.37 thorpej /*
110 1.45 chuck * globals
111 1.37 thorpej */
112 1.37 thorpej
113 1.45 chuck struct tty *constty; /* pointer to console "window" tty */
114 1.45 chuck int consintr = 1; /* ok to handle console interrupts? */
115 1.45 chuck extern int log_open; /* subr_log: is /dev/klog open? */
116 1.45 chuck const char *panicstr; /* arg to first call to panic (used as a flag
117 1.45 chuck to indicate that panic has already been called). */
118 1.45 chuck
119 1.45 chuck /*
120 1.45 chuck * v_putc: routine to putc on virtual console
121 1.45 chuck *
122 1.45 chuck * the v_putc pointer can be used to redirect the console cnputc elsewhere
123 1.45 chuck * [e.g. to a "virtual console"].
124 1.45 chuck */
125 1.12 cgd
126 1.45 chuck void (*v_putc) __P((int)) = cnputc; /* start with cnputc (normal cons) */
127 1.12 cgd
128 1.12 cgd
129 1.45 chuck /*
130 1.45 chuck * functions
131 1.45 chuck */
132 1.12 cgd
133 1.12 cgd /*
134 1.45 chuck * tablefull: warn that a system table is full
135 1.12 cgd */
136 1.45 chuck
137 1.45 chuck void
138 1.45 chuck tablefull(tab)
139 1.45 chuck const char *tab;
140 1.45 chuck {
141 1.45 chuck log(LOG_ERR, "%s: table is full\n", tab);
142 1.45 chuck }
143 1.12 cgd
144 1.12 cgd /*
145 1.45 chuck * panic: handle an unresolvable fatal error
146 1.45 chuck *
147 1.45 chuck * prints "panic: <message>" and reboots. if called twice (i.e. recursive
148 1.45 chuck * call) we avoid trying to sync the disk and just reboot (to avoid
149 1.45 chuck * recursive panics).
150 1.12 cgd */
151 1.45 chuck
152 1.14 cgd void
153 1.12 cgd #ifdef __STDC__
154 1.12 cgd panic(const char *fmt, ...)
155 1.12 cgd #else
156 1.14 cgd panic(fmt, va_alist)
157 1.12 cgd char *fmt;
158 1.20 christos va_dcl
159 1.12 cgd #endif
160 1.12 cgd {
161 1.14 cgd int bootopt;
162 1.12 cgd va_list ap;
163 1.12 cgd
164 1.14 cgd bootopt = RB_AUTOBOOT | RB_DUMP;
165 1.12 cgd if (panicstr)
166 1.12 cgd bootopt |= RB_NOSYNC;
167 1.12 cgd else
168 1.12 cgd panicstr = fmt;
169 1.12 cgd
170 1.12 cgd va_start(ap, fmt);
171 1.45 chuck printf("panic: ");
172 1.45 chuck vprintf(fmt, ap);
173 1.45 chuck printf("\n");
174 1.12 cgd va_end(ap);
175 1.12 cgd
176 1.32 ws #if NIPKDB > 0
177 1.32 ws ipkdb_panic();
178 1.28 ws #endif
179 1.12 cgd #ifdef KGDB
180 1.12 cgd kgdb_panic();
181 1.12 cgd #endif
182 1.12 cgd #ifdef KADB
183 1.14 cgd if (boothowto & RB_KDB)
184 1.14 cgd kdbpanic();
185 1.12 cgd #endif
186 1.12 cgd #ifdef DDB
187 1.38 thorpej if (db_onpanic)
188 1.38 thorpej Debugger();
189 1.12 cgd #endif
190 1.39 gwr cpu_reboot(bootopt, NULL);
191 1.12 cgd }
192 1.12 cgd
193 1.12 cgd /*
194 1.45 chuck * kernel logging functions: log, logpri, addlog
195 1.45 chuck */
196 1.45 chuck
197 1.45 chuck /*
198 1.45 chuck * log: write to the log buffer
199 1.45 chuck *
200 1.45 chuck * => will not sleep [so safe to call from interrupt]
201 1.45 chuck * => will log to console if /dev/klog isn't open
202 1.45 chuck */
203 1.45 chuck
204 1.45 chuck void
205 1.45 chuck #ifdef __STDC__
206 1.45 chuck log(int level, const char *fmt, ...)
207 1.45 chuck #else
208 1.45 chuck log(level, fmt, va_alist)
209 1.45 chuck int level;
210 1.45 chuck char *fmt;
211 1.45 chuck va_dcl
212 1.45 chuck #endif
213 1.45 chuck {
214 1.45 chuck register int s;
215 1.45 chuck va_list ap;
216 1.45 chuck
217 1.45 chuck s = splhigh();
218 1.45 chuck logpri(level); /* log the level first */
219 1.45 chuck va_start(ap, fmt);
220 1.45 chuck kprintf(fmt, TOLOG, NULL, NULL, ap);
221 1.45 chuck splx(s);
222 1.45 chuck va_end(ap);
223 1.45 chuck if (!log_open) {
224 1.45 chuck va_start(ap, fmt);
225 1.45 chuck kprintf(fmt, TOCONS, NULL, NULL, ap);
226 1.45 chuck va_end(ap);
227 1.45 chuck }
228 1.45 chuck logwakeup(); /* wake up anyone waiting for log msgs */
229 1.45 chuck }
230 1.45 chuck
231 1.45 chuck /*
232 1.45 chuck * logpri: log the priority level to the klog
233 1.45 chuck */
234 1.45 chuck
235 1.45 chuck void /* XXXCDC: should be static? */
236 1.45 chuck logpri(level)
237 1.45 chuck int level;
238 1.45 chuck {
239 1.45 chuck char *p;
240 1.45 chuck char snbuf[KPRINTF_BUFSIZE];
241 1.45 chuck
242 1.45 chuck putchar('<', TOLOG, NULL);
243 1.45 chuck sprintf(snbuf, "%d", level);
244 1.45 chuck for (p = snbuf ; *p ; p++)
245 1.45 chuck putchar(*p, TOLOG, NULL);
246 1.45 chuck putchar('>', TOLOG, NULL);
247 1.45 chuck }
248 1.45 chuck
249 1.45 chuck /*
250 1.45 chuck * addlog: add info to previous log message
251 1.12 cgd */
252 1.45 chuck
253 1.12 cgd void
254 1.45 chuck #ifdef __STDC__
255 1.45 chuck addlog(const char *fmt, ...)
256 1.45 chuck #else
257 1.45 chuck addlog(fmt, va_alist)
258 1.45 chuck char *fmt;
259 1.45 chuck va_dcl
260 1.45 chuck #endif
261 1.45 chuck {
262 1.45 chuck register int s;
263 1.45 chuck va_list ap;
264 1.45 chuck
265 1.45 chuck s = splhigh();
266 1.45 chuck va_start(ap, fmt);
267 1.45 chuck kprintf(fmt, TOLOG, NULL, NULL, ap);
268 1.45 chuck splx(s);
269 1.45 chuck va_end(ap);
270 1.45 chuck if (!log_open) {
271 1.45 chuck va_start(ap, fmt);
272 1.45 chuck kprintf(fmt, TOCONS, NULL, NULL, ap);
273 1.45 chuck va_end(ap);
274 1.45 chuck }
275 1.45 chuck logwakeup();
276 1.45 chuck }
277 1.45 chuck
278 1.45 chuck
279 1.45 chuck /*
280 1.45 chuck * putchar: print a single character on console or user terminal.
281 1.45 chuck *
282 1.45 chuck * => if console, then the last MSGBUFS chars are saved in msgbuf
283 1.45 chuck * for inspection later (e.g. dmesg/syslog)
284 1.45 chuck */
285 1.45 chuck static void
286 1.45 chuck putchar(c, flags, tp)
287 1.45 chuck register int c;
288 1.45 chuck int flags;
289 1.45 chuck struct tty *tp;
290 1.12 cgd {
291 1.45 chuck register struct kern_msgbuf *mbp;
292 1.12 cgd
293 1.45 chuck if (panicstr)
294 1.45 chuck constty = NULL;
295 1.45 chuck if ((flags & TOCONS) && tp == NULL && constty) {
296 1.45 chuck tp = constty;
297 1.45 chuck flags |= TOTTY;
298 1.45 chuck }
299 1.45 chuck if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 &&
300 1.45 chuck (flags & TOCONS) && tp == constty)
301 1.45 chuck constty = NULL;
302 1.45 chuck if ((flags & TOLOG) &&
303 1.45 chuck c != '\0' && c != '\r' && c != 0177 && msgbufenabled) {
304 1.45 chuck mbp = msgbufp;
305 1.45 chuck if (mbp->msg_magic != MSG_MAGIC) {
306 1.45 chuck /*
307 1.45 chuck * Arguably should panic or somehow notify the
308 1.45 chuck * user... but how? Panic may be too drastic,
309 1.45 chuck * and would obliterate the message being kicked
310 1.45 chuck * out (maybe a panic itself), and printf
311 1.45 chuck * would invoke us recursively. Silently punt
312 1.45 chuck * for now. If syslog is running, it should
313 1.45 chuck * notice.
314 1.45 chuck */
315 1.45 chuck msgbufenabled = 0;
316 1.45 chuck } else {
317 1.45 chuck mbp->msg_bufc[mbp->msg_bufx++] = c;
318 1.45 chuck if (mbp->msg_bufx < 0 || mbp->msg_bufx >= mbp->msg_bufs)
319 1.45 chuck mbp->msg_bufx = 0;
320 1.48 fvdl /* If the buffer is full, keep the most recent data. */
321 1.48 fvdl if (mbp->msg_bufr == mbp->msg_bufx) {
322 1.48 fvdl if (++mbp->msg_bufr >= mbp->msg_bufs)
323 1.48 fvdl mbp->msg_bufr = 0;
324 1.48 fvdl }
325 1.45 chuck }
326 1.45 chuck }
327 1.45 chuck if ((flags & TOCONS) && constty == NULL && c != '\0')
328 1.45 chuck (*v_putc)(c);
329 1.45 chuck #ifdef DDB
330 1.45 chuck if (flags & TODDB)
331 1.45 chuck db_putchar(c);
332 1.45 chuck #endif
333 1.12 cgd }
334 1.12 cgd
335 1.45 chuck
336 1.12 cgd /*
337 1.45 chuck * uprintf: print to the controlling tty of the current process
338 1.45 chuck *
339 1.45 chuck * => we may block if the tty queue is full
340 1.45 chuck * => no message is printed if the queue doesn't clear in a reasonable
341 1.45 chuck * time
342 1.12 cgd */
343 1.45 chuck
344 1.12 cgd void
345 1.12 cgd #ifdef __STDC__
346 1.12 cgd uprintf(const char *fmt, ...)
347 1.12 cgd #else
348 1.14 cgd uprintf(fmt, va_alist)
349 1.12 cgd char *fmt;
350 1.20 christos va_dcl
351 1.12 cgd #endif
352 1.12 cgd {
353 1.12 cgd register struct proc *p = curproc;
354 1.12 cgd va_list ap;
355 1.12 cgd
356 1.12 cgd if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
357 1.12 cgd va_start(ap, fmt);
358 1.45 chuck kprintf(fmt, TOTTY, p->p_session->s_ttyp, NULL, ap);
359 1.12 cgd va_end(ap);
360 1.12 cgd }
361 1.12 cgd }
362 1.12 cgd
363 1.45 chuck /*
364 1.45 chuck * tprintf functions: used to send messages to a specific process
365 1.45 chuck *
366 1.45 chuck * usage:
367 1.45 chuck * get a tpr_t handle on a process "p" by using "tprintf_open(p)"
368 1.45 chuck * use the handle when calling "tprintf"
369 1.45 chuck * when done, do a "tprintf_close" to drop the handle
370 1.45 chuck */
371 1.45 chuck
372 1.45 chuck /*
373 1.45 chuck * tprintf_open: get a tprintf handle on a process "p"
374 1.45 chuck *
375 1.45 chuck * => returns NULL if process can't be printed to
376 1.45 chuck */
377 1.45 chuck
378 1.12 cgd tpr_t
379 1.12 cgd tprintf_open(p)
380 1.12 cgd register struct proc *p;
381 1.12 cgd {
382 1.12 cgd
383 1.12 cgd if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
384 1.12 cgd SESSHOLD(p->p_session);
385 1.12 cgd return ((tpr_t) p->p_session);
386 1.12 cgd }
387 1.12 cgd return ((tpr_t) NULL);
388 1.12 cgd }
389 1.12 cgd
390 1.45 chuck /*
391 1.45 chuck * tprintf_close: dispose of a tprintf handle obtained with tprintf_open
392 1.45 chuck */
393 1.45 chuck
394 1.12 cgd void
395 1.12 cgd tprintf_close(sess)
396 1.12 cgd tpr_t sess;
397 1.12 cgd {
398 1.12 cgd
399 1.12 cgd if (sess)
400 1.12 cgd SESSRELE((struct session *) sess);
401 1.12 cgd }
402 1.12 cgd
403 1.12 cgd /*
404 1.45 chuck * tprintf: given tprintf handle to a process [obtained with tprintf_open],
405 1.45 chuck * send a message to the controlling tty for that process.
406 1.45 chuck *
407 1.45 chuck * => also sends message to /dev/klog
408 1.12 cgd */
409 1.12 cgd void
410 1.12 cgd #ifdef __STDC__
411 1.12 cgd tprintf(tpr_t tpr, const char *fmt, ...)
412 1.12 cgd #else
413 1.14 cgd tprintf(tpr, fmt, va_alist)
414 1.12 cgd tpr_t tpr;
415 1.12 cgd char *fmt;
416 1.20 christos va_dcl
417 1.12 cgd #endif
418 1.12 cgd {
419 1.12 cgd register struct session *sess = (struct session *)tpr;
420 1.12 cgd struct tty *tp = NULL;
421 1.12 cgd int flags = TOLOG;
422 1.12 cgd va_list ap;
423 1.12 cgd
424 1.12 cgd logpri(LOG_INFO);
425 1.12 cgd if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
426 1.12 cgd flags |= TOTTY;
427 1.12 cgd tp = sess->s_ttyp;
428 1.12 cgd }
429 1.12 cgd va_start(ap, fmt);
430 1.45 chuck kprintf(fmt, flags, tp, NULL, ap);
431 1.12 cgd va_end(ap);
432 1.12 cgd logwakeup();
433 1.12 cgd }
434 1.12 cgd
435 1.45 chuck
436 1.12 cgd /*
437 1.45 chuck * ttyprintf: send a message to a specific tty
438 1.45 chuck *
439 1.45 chuck * => should be used only by tty driver or anything that knows the
440 1.52 mrg * underlying tty will not be revoked(2)'d away. [otherwise,
441 1.52 mrg * use tprintf]
442 1.12 cgd */
443 1.12 cgd void
444 1.12 cgd #ifdef __STDC__
445 1.12 cgd ttyprintf(struct tty *tp, const char *fmt, ...)
446 1.12 cgd #else
447 1.14 cgd ttyprintf(tp, fmt, va_alist)
448 1.12 cgd struct tty *tp;
449 1.12 cgd char *fmt;
450 1.20 christos va_dcl
451 1.12 cgd #endif
452 1.12 cgd {
453 1.12 cgd va_list ap;
454 1.12 cgd
455 1.12 cgd va_start(ap, fmt);
456 1.45 chuck kprintf(fmt, TOTTY, tp, NULL, ap);
457 1.12 cgd va_end(ap);
458 1.12 cgd }
459 1.12 cgd
460 1.45 chuck #ifdef DDB
461 1.12 cgd
462 1.12 cgd /*
463 1.45 chuck * db_printf: printf for DDB (via db_putchar)
464 1.12 cgd */
465 1.45 chuck
466 1.12 cgd void
467 1.12 cgd #ifdef __STDC__
468 1.45 chuck db_printf(const char *fmt, ...)
469 1.12 cgd #else
470 1.45 chuck db_printf(fmt, va_alist)
471 1.12 cgd char *fmt;
472 1.20 christos va_dcl
473 1.12 cgd #endif
474 1.12 cgd {
475 1.12 cgd va_list ap;
476 1.12 cgd
477 1.12 cgd va_start(ap, fmt);
478 1.45 chuck kprintf(fmt, TODDB, NULL, NULL, ap);
479 1.12 cgd va_end(ap);
480 1.12 cgd }
481 1.12 cgd
482 1.45 chuck #endif /* DDB */
483 1.12 cgd
484 1.12 cgd
485 1.45 chuck /*
486 1.45 chuck * normal kernel printf functions: printf, vprintf, sprintf
487 1.45 chuck */
488 1.12 cgd
489 1.45 chuck /*
490 1.45 chuck * printf: print a message to the console and the log
491 1.45 chuck */
492 1.12 cgd void
493 1.12 cgd #ifdef __STDC__
494 1.30 christos printf(const char *fmt, ...)
495 1.12 cgd #else
496 1.30 christos printf(fmt, va_alist)
497 1.12 cgd char *fmt;
498 1.20 christos va_dcl
499 1.12 cgd #endif
500 1.12 cgd {
501 1.12 cgd va_list ap;
502 1.45 chuck int savintr;
503 1.12 cgd
504 1.12 cgd savintr = consintr; /* disable interrupts */
505 1.12 cgd consintr = 0;
506 1.12 cgd va_start(ap, fmt);
507 1.45 chuck kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap);
508 1.12 cgd va_end(ap);
509 1.12 cgd if (!panicstr)
510 1.12 cgd logwakeup();
511 1.12 cgd consintr = savintr; /* reenable interrupts */
512 1.12 cgd }
513 1.12 cgd
514 1.45 chuck /*
515 1.45 chuck * vprintf: print a message to the console and the log [already have
516 1.45 chuck * va_alist]
517 1.45 chuck */
518 1.45 chuck
519 1.40 thorpej void
520 1.40 thorpej vprintf(fmt, ap)
521 1.40 thorpej const char *fmt;
522 1.40 thorpej va_list ap;
523 1.40 thorpej {
524 1.45 chuck int savintr;
525 1.40 thorpej
526 1.40 thorpej savintr = consintr; /* disable interrupts */
527 1.40 thorpej consintr = 0;
528 1.45 chuck kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap);
529 1.40 thorpej if (!panicstr)
530 1.40 thorpej logwakeup();
531 1.40 thorpej consintr = savintr; /* reenable interrupts */
532 1.40 thorpej }
533 1.40 thorpej
534 1.12 cgd /*
535 1.45 chuck * sprintf: print a message to a buffer
536 1.12 cgd */
537 1.45 chuck int
538 1.45 chuck #ifdef __STDC__
539 1.45 chuck sprintf(char *buf, const char *fmt, ...)
540 1.45 chuck #else
541 1.45 chuck sprintf(buf, fmt, va_alist)
542 1.45 chuck char *buf;
543 1.45 chuck const char *cfmt;
544 1.45 chuck va_dcl
545 1.45 chuck #endif
546 1.45 chuck {
547 1.45 chuck int retval;
548 1.12 cgd va_list ap;
549 1.12 cgd
550 1.45 chuck va_start(ap, fmt);
551 1.45 chuck retval = kprintf(fmt, TOBUFONLY, NULL, buf, ap);
552 1.12 cgd va_end(ap);
553 1.45 chuck *(buf + retval) = 0; /* null terminate */
554 1.45 chuck return(retval);
555 1.12 cgd }
556 1.12 cgd
557 1.12 cgd /*
558 1.45 chuck * bitmask_snprintf: print a kernel-printf "%b" message to a buffer
559 1.45 chuck *
560 1.45 chuck * => returns pointer to the buffer
561 1.45 chuck * => XXX: useful vs. kernel %b?
562 1.37 thorpej */
563 1.37 thorpej char *
564 1.51 pk bitmask_snprintf(val, p, buf, buflen)
565 1.51 pk u_quad_t val;
566 1.37 thorpej const char *p;
567 1.37 thorpej char *buf;
568 1.37 thorpej size_t buflen;
569 1.37 thorpej {
570 1.37 thorpej char *bp, *q;
571 1.37 thorpej size_t left;
572 1.51 pk char *sbase, snbuf[KPRINTF_BUFSIZE];
573 1.51 pk int base, bit, ch, len, sep;
574 1.51 pk u_quad_t field;
575 1.37 thorpej
576 1.37 thorpej bp = buf;
577 1.50 perry memset(buf, 0, buflen);
578 1.37 thorpej
579 1.37 thorpej /*
580 1.37 thorpej * Always leave room for the trailing NULL.
581 1.37 thorpej */
582 1.37 thorpej left = buflen - 1;
583 1.37 thorpej
584 1.37 thorpej /*
585 1.37 thorpej * Print the value into the buffer. Abort if there's not
586 1.37 thorpej * enough room.
587 1.37 thorpej */
588 1.45 chuck if (buflen < KPRINTF_BUFSIZE)
589 1.37 thorpej return (buf);
590 1.37 thorpej
591 1.51 pk ch = *p++;
592 1.51 pk base = ch != '\177' ? ch : *p++;
593 1.51 pk sbase = base == 8 ? "%qo" : base == 10 ? "%qd" : base == 16 ? "%qx" : 0;
594 1.51 pk if (sbase == 0)
595 1.51 pk return (buf); /* punt if not oct, dec, or hex */
596 1.45 chuck
597 1.51 pk sprintf(snbuf, sbase, val);
598 1.45 chuck for (q = snbuf ; *q ; q++) {
599 1.45 chuck *bp++ = *q;
600 1.37 thorpej left--;
601 1.37 thorpej }
602 1.37 thorpej
603 1.37 thorpej /*
604 1.37 thorpej * If the value we printed was 0, or if we don't have room for
605 1.37 thorpej * "<x>", we're done.
606 1.37 thorpej */
607 1.51 pk if (val == 0 || left < 3)
608 1.37 thorpej return (buf);
609 1.37 thorpej
610 1.37 thorpej #define PUTBYTE(b, c, l) \
611 1.37 thorpej *(b)++ = (c); \
612 1.37 thorpej if (--(l) == 0) \
613 1.37 thorpej goto out;
614 1.51 pk #define PUTSTR(b, p, l) do { \
615 1.51 pk int c; \
616 1.51 pk while ((c = *(p)++) != 0) { \
617 1.51 pk *(b)++ = c; \
618 1.51 pk if (--(l) == 0) \
619 1.51 pk goto out; \
620 1.51 pk } \
621 1.51 pk (p)++; \
622 1.51 pk } while (0)
623 1.37 thorpej
624 1.51 pk /*
625 1.51 pk * Chris Torek's new style %b format is identified by a leading \177
626 1.51 pk */
627 1.51 pk sep = '<';
628 1.51 pk if (ch != '\177') {
629 1.51 pk /* old (standard) %b format. */
630 1.51 pk for (;(bit = *p++) != 0;) {
631 1.51 pk if (val & (1 << (bit - 1))) {
632 1.51 pk PUTBYTE(bp, sep, left);
633 1.51 pk for (; (ch = *p) > ' '; ++p) {
634 1.51 pk PUTBYTE(bp, ch, left);
635 1.37 thorpej }
636 1.51 pk sep = ',';
637 1.51 pk } else
638 1.51 pk for (; *p > ' '; ++p)
639 1.51 pk continue;
640 1.51 pk }
641 1.51 pk } else {
642 1.51 pk /* new quad-capable %b format; also does fields. */
643 1.51 pk field = val;
644 1.51 pk while ((ch = *p++) != '\0') {
645 1.51 pk bit = *p++; /* now 0-origin */
646 1.51 pk switch (ch) {
647 1.51 pk case 'b':
648 1.51 pk if (((u_int)(val >> bit) & 1) == 0)
649 1.51 pk goto skip;
650 1.51 pk PUTBYTE(bp, sep, left);
651 1.51 pk PUTSTR(bp, p, left);
652 1.51 pk sep = ',';
653 1.51 pk break;
654 1.51 pk case 'f':
655 1.51 pk case 'F':
656 1.51 pk len = *p++; /* field length */
657 1.51 pk field = (val >> bit) & ((1ULL << len) - 1);
658 1.51 pk if (ch == 'F') /* just extract */
659 1.51 pk break;
660 1.51 pk PUTBYTE(bp, sep, left);
661 1.51 pk sep = ',';
662 1.51 pk PUTSTR(bp, p, left);
663 1.51 pk PUTBYTE(bp, '=', left);
664 1.51 pk sprintf(snbuf, sbase, field);
665 1.51 pk q = snbuf; PUTSTR(bp, q, left);
666 1.51 pk break;
667 1.51 pk case '=':
668 1.51 pk case ':':
669 1.51 pk /*
670 1.51 pk * Here "bit" is actually a value instead,
671 1.51 pk * to be compared against the last field.
672 1.51 pk * This only works for values in [0..255],
673 1.51 pk * of course.
674 1.51 pk */
675 1.51 pk if ((int)field != bit)
676 1.51 pk goto skip;
677 1.51 pk if (ch == '=')
678 1.51 pk PUTBYTE(bp, '=', left);
679 1.51 pk PUTSTR(bp, p, left);
680 1.51 pk break;
681 1.51 pk default:
682 1.51 pk skip:
683 1.51 pk while (*p++ != '\0')
684 1.51 pk continue;
685 1.51 pk break;
686 1.51 pk }
687 1.51 pk }
688 1.37 thorpej }
689 1.51 pk if (sep != '<')
690 1.51 pk PUTBYTE(bp, '>', left);
691 1.51 pk
692 1.51 pk out:
693 1.51 pk return (buf);
694 1.37 thorpej
695 1.37 thorpej #undef PUTBYTE
696 1.51 pk #undef PUTSTR
697 1.45 chuck }
698 1.45 chuck
699 1.45 chuck /*
700 1.45 chuck * kprintf: scaled down version of printf(3).
701 1.45 chuck *
702 1.45 chuck * this version based on vfprintf() from libc which was derived from
703 1.45 chuck * software contributed to Berkeley by Chris Torek.
704 1.45 chuck *
705 1.45 chuck * Two additional formats:
706 1.45 chuck *
707 1.45 chuck * The format %b is supported to decode error registers.
708 1.45 chuck * Its usage is:
709 1.45 chuck *
710 1.45 chuck * printf("reg=%b\n", regval, "<base><arg>*");
711 1.45 chuck *
712 1.45 chuck * where <base> is the output base expressed as a control character, e.g.
713 1.45 chuck * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
714 1.45 chuck * the first of which gives the bit number to be inspected (origin 1), and
715 1.45 chuck * the next characters (up to a control character, i.e. a character <= 32),
716 1.45 chuck * give the name of the register. Thus:
717 1.45 chuck *
718 1.45 chuck * kprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
719 1.45 chuck *
720 1.45 chuck * would produce output:
721 1.45 chuck *
722 1.45 chuck * reg=3<BITTWO,BITONE>
723 1.45 chuck *
724 1.45 chuck * The format %: passes an additional format string and argument list
725 1.45 chuck * recursively. Its usage is:
726 1.45 chuck *
727 1.45 chuck * fn(char *fmt, ...)
728 1.45 chuck * {
729 1.45 chuck * va_list ap;
730 1.45 chuck * va_start(ap, fmt);
731 1.45 chuck * printf("prefix: %: suffix\n", fmt, ap);
732 1.45 chuck * va_end(ap);
733 1.45 chuck * }
734 1.45 chuck *
735 1.45 chuck * this is the actual printf innards
736 1.45 chuck *
737 1.45 chuck * This code is large and complicated...
738 1.45 chuck */
739 1.45 chuck
740 1.45 chuck /*
741 1.45 chuck * macros for converting digits to letters and vice versa
742 1.45 chuck */
743 1.45 chuck #define to_digit(c) ((c) - '0')
744 1.45 chuck #define is_digit(c) ((unsigned)to_digit(c) <= 9)
745 1.45 chuck #define to_char(n) ((n) + '0')
746 1.45 chuck
747 1.45 chuck /*
748 1.45 chuck * flags used during conversion.
749 1.45 chuck */
750 1.45 chuck #define ALT 0x001 /* alternate form */
751 1.45 chuck #define HEXPREFIX 0x002 /* add 0x or 0X prefix */
752 1.45 chuck #define LADJUST 0x004 /* left adjustment */
753 1.45 chuck #define LONGDBL 0x008 /* long double; unimplemented */
754 1.45 chuck #define LONGINT 0x010 /* long integer */
755 1.45 chuck #define QUADINT 0x020 /* quad integer */
756 1.45 chuck #define SHORTINT 0x040 /* short integer */
757 1.45 chuck #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */
758 1.45 chuck #define FPT 0x100 /* Floating point number */
759 1.45 chuck
760 1.45 chuck /*
761 1.45 chuck * To extend shorts properly, we need both signed and unsigned
762 1.45 chuck * argument extraction methods.
763 1.45 chuck */
764 1.45 chuck #define SARG() \
765 1.45 chuck (flags&QUADINT ? va_arg(ap, quad_t) : \
766 1.45 chuck flags&LONGINT ? va_arg(ap, long) : \
767 1.45 chuck flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
768 1.45 chuck (long)va_arg(ap, int))
769 1.45 chuck #define UARG() \
770 1.45 chuck (flags&QUADINT ? va_arg(ap, u_quad_t) : \
771 1.45 chuck flags&LONGINT ? va_arg(ap, u_long) : \
772 1.45 chuck flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
773 1.45 chuck (u_long)va_arg(ap, u_int))
774 1.45 chuck
775 1.45 chuck #define KPRINTF_PUTCHAR(C) \
776 1.45 chuck (oflags == TOBUFONLY) ? *sbuf++ = (C) : putchar((C), oflags, tp);
777 1.45 chuck
778 1.45 chuck int
779 1.45 chuck kprintf(fmt0, oflags, tp, sbuf, ap)
780 1.45 chuck const char *fmt0;
781 1.45 chuck int oflags;
782 1.45 chuck struct tty *tp;
783 1.45 chuck char *sbuf;
784 1.45 chuck va_list ap;
785 1.45 chuck {
786 1.45 chuck char *fmt; /* format string */
787 1.45 chuck int ch; /* character from fmt */
788 1.45 chuck int n; /* handy integer (short term usage) */
789 1.45 chuck char *cp; /* handy char pointer (short term usage) */
790 1.45 chuck int flags; /* flags as above */
791 1.45 chuck int ret; /* return value accumulator */
792 1.45 chuck int width; /* width from format (%8d), or 0 */
793 1.45 chuck int prec; /* precision from format (%.3d), or -1 */
794 1.45 chuck char sign; /* sign prefix (' ', '+', '-', or \0) */
795 1.45 chuck
796 1.45 chuck u_quad_t _uquad; /* integer arguments %[diouxX] */
797 1.45 chuck enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
798 1.45 chuck int dprec; /* a copy of prec if [diouxX], 0 otherwise */
799 1.45 chuck int realsz; /* field size expanded by dprec */
800 1.45 chuck int size; /* size of converted field or string */
801 1.45 chuck char *xdigs; /* digits for [xX] conversion */
802 1.45 chuck char buf[KPRINTF_BUFSIZE]; /* space for %c, %[diouxX] */
803 1.45 chuck
804 1.45 chuck cp = NULL; /* XXX: shutup gcc */
805 1.45 chuck size = 0; /* XXX: shutup gcc */
806 1.45 chuck
807 1.45 chuck fmt = (char *)fmt0;
808 1.45 chuck ret = 0;
809 1.45 chuck
810 1.45 chuck xdigs = NULL; /* XXX: shut up gcc warning */
811 1.45 chuck
812 1.45 chuck /*
813 1.45 chuck * Scan the format for conversions (`%' character).
814 1.45 chuck */
815 1.45 chuck for (;;) {
816 1.45 chuck while (*fmt != '%' && *fmt) {
817 1.45 chuck KPRINTF_PUTCHAR(*fmt++);
818 1.45 chuck ret++;
819 1.45 chuck }
820 1.45 chuck if (*fmt == 0)
821 1.45 chuck goto done;
822 1.45 chuck
823 1.45 chuck fmt++; /* skip over '%' */
824 1.45 chuck
825 1.45 chuck flags = 0;
826 1.45 chuck dprec = 0;
827 1.45 chuck width = 0;
828 1.45 chuck prec = -1;
829 1.45 chuck sign = '\0';
830 1.45 chuck
831 1.45 chuck rflag: ch = *fmt++;
832 1.45 chuck reswitch: switch (ch) {
833 1.45 chuck /* XXX: non-standard '%:' format */
834 1.45 chuck #ifndef __powerpc__
835 1.45 chuck case ':':
836 1.45 chuck if (oflags != TOBUFONLY) {
837 1.45 chuck cp = va_arg(ap, char *);
838 1.45 chuck kprintf(cp, oflags, tp,
839 1.45 chuck NULL, va_arg(ap, va_list));
840 1.45 chuck }
841 1.45 chuck continue; /* no output */
842 1.45 chuck #endif
843 1.45 chuck /* XXX: non-standard '%b' format */
844 1.45 chuck case 'b': {
845 1.45 chuck char *b, *z;
846 1.45 chuck int tmp;
847 1.45 chuck _uquad = va_arg(ap, int);
848 1.45 chuck b = va_arg(ap, char *);
849 1.45 chuck if (*b == 8)
850 1.45 chuck sprintf(buf, "%qo", _uquad);
851 1.45 chuck else if (*b == 10)
852 1.45 chuck sprintf(buf, "%qd", _uquad);
853 1.45 chuck else if (*b == 16)
854 1.45 chuck sprintf(buf, "%qx", _uquad);
855 1.45 chuck else
856 1.45 chuck break;
857 1.45 chuck b++;
858 1.45 chuck
859 1.45 chuck z = buf;
860 1.45 chuck while (*z) {
861 1.45 chuck KPRINTF_PUTCHAR(*z++);
862 1.45 chuck ret++;
863 1.45 chuck }
864 1.45 chuck
865 1.45 chuck if (_uquad) {
866 1.45 chuck tmp = 0;
867 1.45 chuck while ((n = *b++) != 0) {
868 1.45 chuck if (_uquad & (1 << (n - 1))) {
869 1.45 chuck KPRINTF_PUTCHAR(tmp ? ',':'<');
870 1.45 chuck ret++;
871 1.45 chuck while ((n = *b) > ' ') {
872 1.45 chuck KPRINTF_PUTCHAR(n);
873 1.45 chuck ret++;
874 1.45 chuck b++;
875 1.45 chuck }
876 1.45 chuck tmp = 1;
877 1.45 chuck } else {
878 1.45 chuck while(*b > ' ')
879 1.45 chuck b++;
880 1.45 chuck }
881 1.45 chuck }
882 1.45 chuck if (tmp) {
883 1.45 chuck KPRINTF_PUTCHAR('>');
884 1.45 chuck ret++;
885 1.45 chuck }
886 1.45 chuck }
887 1.45 chuck continue; /* no output */
888 1.45 chuck }
889 1.45 chuck
890 1.45 chuck #ifdef DDB
891 1.45 chuck /* XXX: non-standard '%r' format (print int in db_radix) */
892 1.45 chuck case 'r':
893 1.45 chuck if ((oflags & TODDB) == 0)
894 1.45 chuck goto default_case;
895 1.45 chuck
896 1.45 chuck if (db_radix == 16)
897 1.45 chuck goto case_z; /* signed hex */
898 1.45 chuck _uquad = SARG();
899 1.45 chuck if ((quad_t)_uquad < 0) {
900 1.45 chuck _uquad = -_uquad;
901 1.45 chuck sign = '-';
902 1.45 chuck }
903 1.45 chuck base = (db_radix == 8) ? OCT : DEC;
904 1.45 chuck goto number;
905 1.45 chuck
906 1.45 chuck
907 1.45 chuck /* XXX: non-standard '%z' format ("signed hex", a "hex %i")*/
908 1.45 chuck case 'z':
909 1.45 chuck case_z:
910 1.45 chuck if ((oflags & TODDB) == 0)
911 1.45 chuck goto default_case;
912 1.45 chuck
913 1.45 chuck xdigs = "0123456789abcdef";
914 1.45 chuck ch = 'x'; /* the 'x' in '0x' (below) */
915 1.45 chuck _uquad = SARG();
916 1.45 chuck base = HEX;
917 1.45 chuck /* leading 0x/X only if non-zero */
918 1.45 chuck if (flags & ALT && _uquad != 0)
919 1.45 chuck flags |= HEXPREFIX;
920 1.45 chuck if ((quad_t)_uquad < 0) {
921 1.45 chuck _uquad = -_uquad;
922 1.45 chuck sign = '-';
923 1.45 chuck }
924 1.45 chuck goto number;
925 1.45 chuck #endif
926 1.45 chuck
927 1.45 chuck case ' ':
928 1.45 chuck /*
929 1.45 chuck * ``If the space and + flags both appear, the space
930 1.45 chuck * flag will be ignored.''
931 1.45 chuck * -- ANSI X3J11
932 1.45 chuck */
933 1.45 chuck if (!sign)
934 1.45 chuck sign = ' ';
935 1.45 chuck goto rflag;
936 1.45 chuck case '#':
937 1.45 chuck flags |= ALT;
938 1.45 chuck goto rflag;
939 1.45 chuck case '*':
940 1.45 chuck /*
941 1.45 chuck * ``A negative field width argument is taken as a
942 1.45 chuck * - flag followed by a positive field width.''
943 1.45 chuck * -- ANSI X3J11
944 1.45 chuck * They don't exclude field widths read from args.
945 1.45 chuck */
946 1.45 chuck if ((width = va_arg(ap, int)) >= 0)
947 1.45 chuck goto rflag;
948 1.45 chuck width = -width;
949 1.45 chuck /* FALLTHROUGH */
950 1.45 chuck case '-':
951 1.45 chuck flags |= LADJUST;
952 1.45 chuck goto rflag;
953 1.45 chuck case '+':
954 1.45 chuck sign = '+';
955 1.45 chuck goto rflag;
956 1.45 chuck case '.':
957 1.45 chuck if ((ch = *fmt++) == '*') {
958 1.45 chuck n = va_arg(ap, int);
959 1.45 chuck prec = n < 0 ? -1 : n;
960 1.45 chuck goto rflag;
961 1.45 chuck }
962 1.45 chuck n = 0;
963 1.45 chuck while (is_digit(ch)) {
964 1.45 chuck n = 10 * n + to_digit(ch);
965 1.45 chuck ch = *fmt++;
966 1.45 chuck }
967 1.45 chuck prec = n < 0 ? -1 : n;
968 1.45 chuck goto reswitch;
969 1.45 chuck case '0':
970 1.45 chuck /*
971 1.45 chuck * ``Note that 0 is taken as a flag, not as the
972 1.45 chuck * beginning of a field width.''
973 1.45 chuck * -- ANSI X3J11
974 1.45 chuck */
975 1.45 chuck flags |= ZEROPAD;
976 1.45 chuck goto rflag;
977 1.45 chuck case '1': case '2': case '3': case '4':
978 1.45 chuck case '5': case '6': case '7': case '8': case '9':
979 1.45 chuck n = 0;
980 1.45 chuck do {
981 1.45 chuck n = 10 * n + to_digit(ch);
982 1.45 chuck ch = *fmt++;
983 1.45 chuck } while (is_digit(ch));
984 1.45 chuck width = n;
985 1.45 chuck goto reswitch;
986 1.45 chuck case 'h':
987 1.45 chuck flags |= SHORTINT;
988 1.45 chuck goto rflag;
989 1.45 chuck case 'l':
990 1.45 chuck if (*fmt == 'l') {
991 1.45 chuck fmt++;
992 1.45 chuck flags |= QUADINT;
993 1.45 chuck } else {
994 1.45 chuck flags |= LONGINT;
995 1.45 chuck }
996 1.45 chuck goto rflag;
997 1.45 chuck case 'q':
998 1.45 chuck flags |= QUADINT;
999 1.45 chuck goto rflag;
1000 1.45 chuck case 'c':
1001 1.45 chuck *(cp = buf) = va_arg(ap, int);
1002 1.45 chuck size = 1;
1003 1.45 chuck sign = '\0';
1004 1.45 chuck break;
1005 1.45 chuck case 'D':
1006 1.45 chuck flags |= LONGINT;
1007 1.45 chuck /*FALLTHROUGH*/
1008 1.45 chuck case 'd':
1009 1.45 chuck case 'i':
1010 1.45 chuck _uquad = SARG();
1011 1.45 chuck if ((quad_t)_uquad < 0) {
1012 1.45 chuck _uquad = -_uquad;
1013 1.45 chuck sign = '-';
1014 1.45 chuck }
1015 1.45 chuck base = DEC;
1016 1.45 chuck goto number;
1017 1.45 chuck case 'n':
1018 1.45 chuck #ifdef DDB
1019 1.45 chuck /* XXX: non-standard '%n' format */
1020 1.45 chuck /*
1021 1.45 chuck * XXX: HACK! DDB wants '%n' to be a '%u' printed
1022 1.45 chuck * in db_radix format. this should die since '%n'
1023 1.45 chuck * is already defined in standard printf to write
1024 1.45 chuck * the number of chars printed so far to the arg (which
1025 1.45 chuck * should be a pointer.
1026 1.45 chuck */
1027 1.45 chuck if (oflags & TODDB) {
1028 1.45 chuck if (db_radix == 16)
1029 1.45 chuck ch = 'x'; /* convert to %x */
1030 1.45 chuck else if (db_radix == 8)
1031 1.45 chuck ch = 'o'; /* convert to %o */
1032 1.45 chuck else
1033 1.45 chuck ch = 'u'; /* convert to %u */
1034 1.45 chuck
1035 1.45 chuck /* ... and start again */
1036 1.45 chuck goto reswitch;
1037 1.45 chuck }
1038 1.45 chuck
1039 1.45 chuck #endif
1040 1.45 chuck if (flags & QUADINT)
1041 1.45 chuck *va_arg(ap, quad_t *) = ret;
1042 1.45 chuck else if (flags & LONGINT)
1043 1.45 chuck *va_arg(ap, long *) = ret;
1044 1.45 chuck else if (flags & SHORTINT)
1045 1.45 chuck *va_arg(ap, short *) = ret;
1046 1.45 chuck else
1047 1.45 chuck *va_arg(ap, int *) = ret;
1048 1.45 chuck continue; /* no output */
1049 1.45 chuck case 'O':
1050 1.45 chuck flags |= LONGINT;
1051 1.45 chuck /*FALLTHROUGH*/
1052 1.45 chuck case 'o':
1053 1.45 chuck _uquad = UARG();
1054 1.45 chuck base = OCT;
1055 1.45 chuck goto nosign;
1056 1.45 chuck case 'p':
1057 1.45 chuck /*
1058 1.45 chuck * ``The argument shall be a pointer to void. The
1059 1.45 chuck * value of the pointer is converted to a sequence
1060 1.45 chuck * of printable characters, in an implementation-
1061 1.45 chuck * defined manner.''
1062 1.45 chuck * -- ANSI X3J11
1063 1.45 chuck */
1064 1.45 chuck /* NOSTRICT */
1065 1.45 chuck _uquad = (u_long)va_arg(ap, void *);
1066 1.45 chuck base = HEX;
1067 1.45 chuck xdigs = "0123456789abcdef";
1068 1.45 chuck flags |= HEXPREFIX;
1069 1.45 chuck ch = 'x';
1070 1.45 chuck goto nosign;
1071 1.45 chuck case 's':
1072 1.45 chuck if ((cp = va_arg(ap, char *)) == NULL)
1073 1.45 chuck cp = "(null)";
1074 1.45 chuck if (prec >= 0) {
1075 1.45 chuck /*
1076 1.45 chuck * can't use strlen; can only look for the
1077 1.45 chuck * NUL in the first `prec' characters, and
1078 1.45 chuck * strlen() will go further.
1079 1.45 chuck */
1080 1.45 chuck char *p = memchr(cp, 0, prec);
1081 1.45 chuck
1082 1.45 chuck if (p != NULL) {
1083 1.45 chuck size = p - cp;
1084 1.45 chuck if (size > prec)
1085 1.45 chuck size = prec;
1086 1.45 chuck } else
1087 1.45 chuck size = prec;
1088 1.45 chuck } else
1089 1.45 chuck size = strlen(cp);
1090 1.45 chuck sign = '\0';
1091 1.45 chuck break;
1092 1.45 chuck case 'U':
1093 1.45 chuck flags |= LONGINT;
1094 1.45 chuck /*FALLTHROUGH*/
1095 1.45 chuck case 'u':
1096 1.45 chuck _uquad = UARG();
1097 1.45 chuck base = DEC;
1098 1.45 chuck goto nosign;
1099 1.45 chuck case 'X':
1100 1.45 chuck xdigs = "0123456789ABCDEF";
1101 1.45 chuck goto hex;
1102 1.45 chuck case 'x':
1103 1.45 chuck xdigs = "0123456789abcdef";
1104 1.45 chuck hex: _uquad = UARG();
1105 1.45 chuck base = HEX;
1106 1.45 chuck /* leading 0x/X only if non-zero */
1107 1.45 chuck if (flags & ALT && _uquad != 0)
1108 1.45 chuck flags |= HEXPREFIX;
1109 1.45 chuck
1110 1.45 chuck /* unsigned conversions */
1111 1.45 chuck nosign: sign = '\0';
1112 1.45 chuck /*
1113 1.45 chuck * ``... diouXx conversions ... if a precision is
1114 1.45 chuck * specified, the 0 flag will be ignored.''
1115 1.45 chuck * -- ANSI X3J11
1116 1.45 chuck */
1117 1.45 chuck number: if ((dprec = prec) >= 0)
1118 1.45 chuck flags &= ~ZEROPAD;
1119 1.45 chuck
1120 1.45 chuck /*
1121 1.45 chuck * ``The result of converting a zero value with an
1122 1.45 chuck * explicit precision of zero is no characters.''
1123 1.45 chuck * -- ANSI X3J11
1124 1.45 chuck */
1125 1.45 chuck cp = buf + KPRINTF_BUFSIZE;
1126 1.45 chuck if (_uquad != 0 || prec != 0) {
1127 1.45 chuck /*
1128 1.45 chuck * Unsigned mod is hard, and unsigned mod
1129 1.45 chuck * by a constant is easier than that by
1130 1.45 chuck * a variable; hence this switch.
1131 1.45 chuck */
1132 1.45 chuck switch (base) {
1133 1.45 chuck case OCT:
1134 1.45 chuck do {
1135 1.45 chuck *--cp = to_char(_uquad & 7);
1136 1.45 chuck _uquad >>= 3;
1137 1.45 chuck } while (_uquad);
1138 1.45 chuck /* handle octal leading 0 */
1139 1.45 chuck if (flags & ALT && *cp != '0')
1140 1.45 chuck *--cp = '0';
1141 1.45 chuck break;
1142 1.45 chuck
1143 1.45 chuck case DEC:
1144 1.45 chuck /* many numbers are 1 digit */
1145 1.45 chuck while (_uquad >= 10) {
1146 1.45 chuck *--cp = to_char(_uquad % 10);
1147 1.45 chuck _uquad /= 10;
1148 1.45 chuck }
1149 1.45 chuck *--cp = to_char(_uquad);
1150 1.45 chuck break;
1151 1.45 chuck
1152 1.45 chuck case HEX:
1153 1.45 chuck do {
1154 1.45 chuck *--cp = xdigs[_uquad & 15];
1155 1.45 chuck _uquad >>= 4;
1156 1.45 chuck } while (_uquad);
1157 1.45 chuck break;
1158 1.45 chuck
1159 1.45 chuck default:
1160 1.45 chuck cp = "bug in kprintf: bad base";
1161 1.45 chuck size = strlen(cp);
1162 1.45 chuck goto skipsize;
1163 1.45 chuck }
1164 1.45 chuck }
1165 1.45 chuck size = buf + KPRINTF_BUFSIZE - cp;
1166 1.45 chuck skipsize:
1167 1.45 chuck break;
1168 1.45 chuck default: /* "%?" prints ?, unless ? is NUL */
1169 1.45 chuck #ifdef DDB
1170 1.45 chuck default_case: /* DDB */
1171 1.45 chuck #endif
1172 1.45 chuck if (ch == '\0')
1173 1.45 chuck goto done;
1174 1.45 chuck /* pretend it was %c with argument ch */
1175 1.45 chuck cp = buf;
1176 1.45 chuck *cp = ch;
1177 1.45 chuck size = 1;
1178 1.45 chuck sign = '\0';
1179 1.45 chuck break;
1180 1.45 chuck }
1181 1.45 chuck
1182 1.45 chuck /*
1183 1.45 chuck * All reasonable formats wind up here. At this point, `cp'
1184 1.45 chuck * points to a string which (if not flags&LADJUST) should be
1185 1.45 chuck * padded out to `width' places. If flags&ZEROPAD, it should
1186 1.45 chuck * first be prefixed by any sign or other prefix; otherwise,
1187 1.45 chuck * it should be blank padded before the prefix is emitted.
1188 1.45 chuck * After any left-hand padding and prefixing, emit zeroes
1189 1.45 chuck * required by a decimal [diouxX] precision, then print the
1190 1.45 chuck * string proper, then emit zeroes required by any leftover
1191 1.45 chuck * floating precision; finally, if LADJUST, pad with blanks.
1192 1.45 chuck *
1193 1.45 chuck * Compute actual size, so we know how much to pad.
1194 1.45 chuck * size excludes decimal prec; realsz includes it.
1195 1.45 chuck */
1196 1.45 chuck realsz = dprec > size ? dprec : size;
1197 1.45 chuck if (sign)
1198 1.45 chuck realsz++;
1199 1.45 chuck else if (flags & HEXPREFIX)
1200 1.45 chuck realsz+= 2;
1201 1.45 chuck
1202 1.45 chuck /* right-adjusting blank padding */
1203 1.45 chuck if ((flags & (LADJUST|ZEROPAD)) == 0) {
1204 1.45 chuck n = width - realsz;
1205 1.45 chuck while (n-- > 0)
1206 1.45 chuck KPRINTF_PUTCHAR(' ');
1207 1.45 chuck }
1208 1.45 chuck
1209 1.45 chuck /* prefix */
1210 1.45 chuck if (sign) {
1211 1.45 chuck KPRINTF_PUTCHAR(sign);
1212 1.45 chuck } else if (flags & HEXPREFIX) {
1213 1.45 chuck KPRINTF_PUTCHAR('0');
1214 1.45 chuck KPRINTF_PUTCHAR(ch);
1215 1.45 chuck }
1216 1.45 chuck
1217 1.45 chuck /* right-adjusting zero padding */
1218 1.45 chuck if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) {
1219 1.45 chuck n = width - realsz;
1220 1.45 chuck while (n-- > 0)
1221 1.45 chuck KPRINTF_PUTCHAR('0');
1222 1.45 chuck }
1223 1.45 chuck
1224 1.45 chuck /* leading zeroes from decimal precision */
1225 1.45 chuck n = dprec - size;
1226 1.45 chuck while (n-- > 0)
1227 1.45 chuck KPRINTF_PUTCHAR('0');
1228 1.45 chuck
1229 1.45 chuck /* the string or number proper */
1230 1.45 chuck while (size--)
1231 1.45 chuck KPRINTF_PUTCHAR(*cp++);
1232 1.45 chuck /* left-adjusting padding (always blank) */
1233 1.45 chuck if (flags & LADJUST) {
1234 1.45 chuck n = width - realsz;
1235 1.45 chuck while (n-- > 0)
1236 1.45 chuck KPRINTF_PUTCHAR(' ');
1237 1.45 chuck }
1238 1.45 chuck
1239 1.45 chuck /* finally, adjust ret */
1240 1.45 chuck ret += width > realsz ? width : realsz;
1241 1.45 chuck
1242 1.45 chuck }
1243 1.45 chuck done:
1244 1.45 chuck return (ret);
1245 1.45 chuck /* NOTREACHED */
1246 1.12 cgd }
1247