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