subr_prf.c revision 1.49 1 1.49 jonathan /* $NetBSD: subr_prf.c,v 1.49 1998/07/04 22:18:52 jonathan 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.45 chuck * underlying tty will not be revoked(2)'d away. [otherwise,
441 1.45 chuck * 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.37 thorpej bitmask_snprintf(ul, p, buf, buflen)
565 1.37 thorpej u_long ul;
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.37 thorpej register int n;
573 1.45 chuck int tmp;
574 1.45 chuck char snbuf[KPRINTF_BUFSIZE];
575 1.37 thorpej
576 1.37 thorpej bp = buf;
577 1.37 thorpej bzero(buf, 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.45 chuck if (*p == 8)
592 1.45 chuck sprintf(snbuf,"%lo", ul);
593 1.45 chuck else if (*p == 10)
594 1.45 chuck sprintf(snbuf, "%ld", ul);
595 1.45 chuck else if (*p == 16)
596 1.45 chuck sprintf(snbuf, "%lx", ul);
597 1.45 chuck else
598 1.45 chuck return(buf); /* punt if not oct, dec, or hex */
599 1.45 chuck p++;
600 1.45 chuck
601 1.45 chuck for (q = snbuf ; *q ; q++) {
602 1.45 chuck *bp++ = *q;
603 1.37 thorpej left--;
604 1.37 thorpej }
605 1.37 thorpej
606 1.37 thorpej /*
607 1.37 thorpej * If the value we printed was 0, or if we don't have room for
608 1.37 thorpej * "<x>", we're done.
609 1.37 thorpej */
610 1.37 thorpej if (ul == 0 || left < 3)
611 1.37 thorpej return (buf);
612 1.37 thorpej
613 1.37 thorpej #define PUTBYTE(b, c, l) \
614 1.37 thorpej *(b)++ = (c); \
615 1.37 thorpej if (--(l) == 0) \
616 1.37 thorpej goto out;
617 1.37 thorpej
618 1.37 thorpej for (tmp = 0; (n = *p++) != 0;) {
619 1.37 thorpej if (ul & (1 << (n - 1))) {
620 1.37 thorpej PUTBYTE(bp, tmp ? ',' : '<', left);
621 1.37 thorpej for (; (n = *p) > ' '; ++p) {
622 1.37 thorpej PUTBYTE(bp, n, left);
623 1.37 thorpej }
624 1.37 thorpej tmp = 1;
625 1.37 thorpej } else
626 1.37 thorpej for (; *p > ' '; ++p)
627 1.37 thorpej continue;
628 1.37 thorpej }
629 1.37 thorpej if (tmp)
630 1.37 thorpej *bp = '>';
631 1.37 thorpej
632 1.37 thorpej #undef PUTBYTE
633 1.37 thorpej
634 1.37 thorpej out:
635 1.37 thorpej return (buf);
636 1.45 chuck }
637 1.45 chuck
638 1.45 chuck /*
639 1.45 chuck * kprintf: scaled down version of printf(3).
640 1.45 chuck *
641 1.45 chuck * this version based on vfprintf() from libc which was derived from
642 1.45 chuck * software contributed to Berkeley by Chris Torek.
643 1.45 chuck *
644 1.45 chuck * Two additional formats:
645 1.45 chuck *
646 1.45 chuck * The format %b is supported to decode error registers.
647 1.45 chuck * Its usage is:
648 1.45 chuck *
649 1.45 chuck * printf("reg=%b\n", regval, "<base><arg>*");
650 1.45 chuck *
651 1.45 chuck * where <base> is the output base expressed as a control character, e.g.
652 1.45 chuck * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
653 1.45 chuck * the first of which gives the bit number to be inspected (origin 1), and
654 1.45 chuck * the next characters (up to a control character, i.e. a character <= 32),
655 1.45 chuck * give the name of the register. Thus:
656 1.45 chuck *
657 1.45 chuck * kprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
658 1.45 chuck *
659 1.45 chuck * would produce output:
660 1.45 chuck *
661 1.45 chuck * reg=3<BITTWO,BITONE>
662 1.45 chuck *
663 1.45 chuck * The format %: passes an additional format string and argument list
664 1.45 chuck * recursively. Its usage is:
665 1.45 chuck *
666 1.45 chuck * fn(char *fmt, ...)
667 1.45 chuck * {
668 1.45 chuck * va_list ap;
669 1.45 chuck * va_start(ap, fmt);
670 1.45 chuck * printf("prefix: %: suffix\n", fmt, ap);
671 1.45 chuck * va_end(ap);
672 1.45 chuck * }
673 1.45 chuck *
674 1.45 chuck * this is the actual printf innards
675 1.45 chuck *
676 1.45 chuck * This code is large and complicated...
677 1.45 chuck */
678 1.45 chuck
679 1.45 chuck /*
680 1.45 chuck * macros for converting digits to letters and vice versa
681 1.45 chuck */
682 1.45 chuck #define to_digit(c) ((c) - '0')
683 1.45 chuck #define is_digit(c) ((unsigned)to_digit(c) <= 9)
684 1.45 chuck #define to_char(n) ((n) + '0')
685 1.45 chuck
686 1.45 chuck /*
687 1.45 chuck * flags used during conversion.
688 1.45 chuck */
689 1.45 chuck #define ALT 0x001 /* alternate form */
690 1.45 chuck #define HEXPREFIX 0x002 /* add 0x or 0X prefix */
691 1.45 chuck #define LADJUST 0x004 /* left adjustment */
692 1.45 chuck #define LONGDBL 0x008 /* long double; unimplemented */
693 1.45 chuck #define LONGINT 0x010 /* long integer */
694 1.45 chuck #define QUADINT 0x020 /* quad integer */
695 1.45 chuck #define SHORTINT 0x040 /* short integer */
696 1.45 chuck #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */
697 1.45 chuck #define FPT 0x100 /* Floating point number */
698 1.45 chuck
699 1.45 chuck /*
700 1.45 chuck * To extend shorts properly, we need both signed and unsigned
701 1.45 chuck * argument extraction methods.
702 1.45 chuck */
703 1.45 chuck #define SARG() \
704 1.45 chuck (flags&QUADINT ? va_arg(ap, quad_t) : \
705 1.45 chuck flags&LONGINT ? va_arg(ap, long) : \
706 1.45 chuck flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
707 1.45 chuck (long)va_arg(ap, int))
708 1.45 chuck #define UARG() \
709 1.45 chuck (flags&QUADINT ? va_arg(ap, u_quad_t) : \
710 1.45 chuck flags&LONGINT ? va_arg(ap, u_long) : \
711 1.45 chuck flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
712 1.45 chuck (u_long)va_arg(ap, u_int))
713 1.45 chuck
714 1.45 chuck #define KPRINTF_PUTCHAR(C) \
715 1.45 chuck (oflags == TOBUFONLY) ? *sbuf++ = (C) : putchar((C), oflags, tp);
716 1.45 chuck
717 1.45 chuck int
718 1.45 chuck kprintf(fmt0, oflags, tp, sbuf, ap)
719 1.45 chuck const char *fmt0;
720 1.45 chuck int oflags;
721 1.45 chuck struct tty *tp;
722 1.45 chuck char *sbuf;
723 1.45 chuck va_list ap;
724 1.45 chuck {
725 1.45 chuck char *fmt; /* format string */
726 1.45 chuck int ch; /* character from fmt */
727 1.45 chuck int n; /* handy integer (short term usage) */
728 1.45 chuck char *cp; /* handy char pointer (short term usage) */
729 1.45 chuck int flags; /* flags as above */
730 1.45 chuck int ret; /* return value accumulator */
731 1.45 chuck int width; /* width from format (%8d), or 0 */
732 1.45 chuck int prec; /* precision from format (%.3d), or -1 */
733 1.45 chuck char sign; /* sign prefix (' ', '+', '-', or \0) */
734 1.45 chuck
735 1.45 chuck u_quad_t _uquad; /* integer arguments %[diouxX] */
736 1.45 chuck enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
737 1.45 chuck int dprec; /* a copy of prec if [diouxX], 0 otherwise */
738 1.45 chuck int realsz; /* field size expanded by dprec */
739 1.45 chuck int size; /* size of converted field or string */
740 1.45 chuck char *xdigs; /* digits for [xX] conversion */
741 1.45 chuck char buf[KPRINTF_BUFSIZE]; /* space for %c, %[diouxX] */
742 1.45 chuck
743 1.45 chuck cp = NULL; /* XXX: shutup gcc */
744 1.45 chuck size = 0; /* XXX: shutup gcc */
745 1.45 chuck
746 1.45 chuck fmt = (char *)fmt0;
747 1.45 chuck ret = 0;
748 1.45 chuck
749 1.45 chuck xdigs = NULL; /* XXX: shut up gcc warning */
750 1.45 chuck
751 1.45 chuck /*
752 1.45 chuck * Scan the format for conversions (`%' character).
753 1.45 chuck */
754 1.45 chuck for (;;) {
755 1.45 chuck while (*fmt != '%' && *fmt) {
756 1.45 chuck KPRINTF_PUTCHAR(*fmt++);
757 1.45 chuck ret++;
758 1.45 chuck }
759 1.45 chuck if (*fmt == 0)
760 1.45 chuck goto done;
761 1.45 chuck
762 1.45 chuck fmt++; /* skip over '%' */
763 1.45 chuck
764 1.45 chuck flags = 0;
765 1.45 chuck dprec = 0;
766 1.45 chuck width = 0;
767 1.45 chuck prec = -1;
768 1.45 chuck sign = '\0';
769 1.45 chuck
770 1.45 chuck rflag: ch = *fmt++;
771 1.45 chuck reswitch: switch (ch) {
772 1.45 chuck /* XXX: non-standard '%:' format */
773 1.45 chuck #ifndef __powerpc__
774 1.45 chuck case ':':
775 1.45 chuck if (oflags != TOBUFONLY) {
776 1.45 chuck cp = va_arg(ap, char *);
777 1.45 chuck kprintf(cp, oflags, tp,
778 1.45 chuck NULL, va_arg(ap, va_list));
779 1.45 chuck }
780 1.45 chuck continue; /* no output */
781 1.45 chuck #endif
782 1.45 chuck /* XXX: non-standard '%b' format */
783 1.45 chuck case 'b': {
784 1.45 chuck char *b, *z;
785 1.45 chuck int tmp;
786 1.45 chuck _uquad = va_arg(ap, int);
787 1.45 chuck b = va_arg(ap, char *);
788 1.45 chuck if (*b == 8)
789 1.45 chuck sprintf(buf, "%qo", _uquad);
790 1.45 chuck else if (*b == 10)
791 1.45 chuck sprintf(buf, "%qd", _uquad);
792 1.45 chuck else if (*b == 16)
793 1.45 chuck sprintf(buf, "%qx", _uquad);
794 1.45 chuck else
795 1.45 chuck break;
796 1.45 chuck b++;
797 1.45 chuck
798 1.45 chuck z = buf;
799 1.45 chuck while (*z) {
800 1.45 chuck KPRINTF_PUTCHAR(*z++);
801 1.45 chuck ret++;
802 1.45 chuck }
803 1.45 chuck
804 1.45 chuck if (_uquad) {
805 1.45 chuck tmp = 0;
806 1.45 chuck while ((n = *b++) != 0) {
807 1.45 chuck if (_uquad & (1 << (n - 1))) {
808 1.45 chuck KPRINTF_PUTCHAR(tmp ? ',':'<');
809 1.45 chuck ret++;
810 1.45 chuck while ((n = *b) > ' ') {
811 1.45 chuck KPRINTF_PUTCHAR(n);
812 1.45 chuck ret++;
813 1.45 chuck b++;
814 1.45 chuck }
815 1.45 chuck tmp = 1;
816 1.45 chuck } else {
817 1.45 chuck while(*b > ' ')
818 1.45 chuck b++;
819 1.45 chuck }
820 1.45 chuck }
821 1.45 chuck if (tmp) {
822 1.45 chuck KPRINTF_PUTCHAR('>');
823 1.45 chuck ret++;
824 1.45 chuck }
825 1.45 chuck }
826 1.45 chuck continue; /* no output */
827 1.45 chuck }
828 1.45 chuck
829 1.45 chuck #ifdef DDB
830 1.45 chuck /* XXX: non-standard '%r' format (print int in db_radix) */
831 1.45 chuck case 'r':
832 1.45 chuck if ((oflags & TODDB) == 0)
833 1.45 chuck goto default_case;
834 1.45 chuck
835 1.45 chuck if (db_radix == 16)
836 1.45 chuck goto case_z; /* signed hex */
837 1.45 chuck _uquad = SARG();
838 1.45 chuck if ((quad_t)_uquad < 0) {
839 1.45 chuck _uquad = -_uquad;
840 1.45 chuck sign = '-';
841 1.45 chuck }
842 1.45 chuck base = (db_radix == 8) ? OCT : DEC;
843 1.45 chuck goto number;
844 1.45 chuck
845 1.45 chuck
846 1.45 chuck /* XXX: non-standard '%z' format ("signed hex", a "hex %i")*/
847 1.45 chuck case 'z':
848 1.45 chuck case_z:
849 1.45 chuck if ((oflags & TODDB) == 0)
850 1.45 chuck goto default_case;
851 1.45 chuck
852 1.45 chuck xdigs = "0123456789abcdef";
853 1.45 chuck ch = 'x'; /* the 'x' in '0x' (below) */
854 1.45 chuck _uquad = SARG();
855 1.45 chuck base = HEX;
856 1.45 chuck /* leading 0x/X only if non-zero */
857 1.45 chuck if (flags & ALT && _uquad != 0)
858 1.45 chuck flags |= HEXPREFIX;
859 1.45 chuck if ((quad_t)_uquad < 0) {
860 1.45 chuck _uquad = -_uquad;
861 1.45 chuck sign = '-';
862 1.45 chuck }
863 1.45 chuck goto number;
864 1.45 chuck #endif
865 1.45 chuck
866 1.45 chuck case ' ':
867 1.45 chuck /*
868 1.45 chuck * ``If the space and + flags both appear, the space
869 1.45 chuck * flag will be ignored.''
870 1.45 chuck * -- ANSI X3J11
871 1.45 chuck */
872 1.45 chuck if (!sign)
873 1.45 chuck sign = ' ';
874 1.45 chuck goto rflag;
875 1.45 chuck case '#':
876 1.45 chuck flags |= ALT;
877 1.45 chuck goto rflag;
878 1.45 chuck case '*':
879 1.45 chuck /*
880 1.45 chuck * ``A negative field width argument is taken as a
881 1.45 chuck * - flag followed by a positive field width.''
882 1.45 chuck * -- ANSI X3J11
883 1.45 chuck * They don't exclude field widths read from args.
884 1.45 chuck */
885 1.45 chuck if ((width = va_arg(ap, int)) >= 0)
886 1.45 chuck goto rflag;
887 1.45 chuck width = -width;
888 1.45 chuck /* FALLTHROUGH */
889 1.45 chuck case '-':
890 1.45 chuck flags |= LADJUST;
891 1.45 chuck goto rflag;
892 1.45 chuck case '+':
893 1.45 chuck sign = '+';
894 1.45 chuck goto rflag;
895 1.45 chuck case '.':
896 1.45 chuck if ((ch = *fmt++) == '*') {
897 1.45 chuck n = va_arg(ap, int);
898 1.45 chuck prec = n < 0 ? -1 : n;
899 1.45 chuck goto rflag;
900 1.45 chuck }
901 1.45 chuck n = 0;
902 1.45 chuck while (is_digit(ch)) {
903 1.45 chuck n = 10 * n + to_digit(ch);
904 1.45 chuck ch = *fmt++;
905 1.45 chuck }
906 1.45 chuck prec = n < 0 ? -1 : n;
907 1.45 chuck goto reswitch;
908 1.45 chuck case '0':
909 1.45 chuck /*
910 1.45 chuck * ``Note that 0 is taken as a flag, not as the
911 1.45 chuck * beginning of a field width.''
912 1.45 chuck * -- ANSI X3J11
913 1.45 chuck */
914 1.45 chuck flags |= ZEROPAD;
915 1.45 chuck goto rflag;
916 1.45 chuck case '1': case '2': case '3': case '4':
917 1.45 chuck case '5': case '6': case '7': case '8': case '9':
918 1.45 chuck n = 0;
919 1.45 chuck do {
920 1.45 chuck n = 10 * n + to_digit(ch);
921 1.45 chuck ch = *fmt++;
922 1.45 chuck } while (is_digit(ch));
923 1.45 chuck width = n;
924 1.45 chuck goto reswitch;
925 1.45 chuck case 'h':
926 1.45 chuck flags |= SHORTINT;
927 1.45 chuck goto rflag;
928 1.45 chuck case 'l':
929 1.45 chuck if (*fmt == 'l') {
930 1.45 chuck fmt++;
931 1.45 chuck flags |= QUADINT;
932 1.45 chuck } else {
933 1.45 chuck flags |= LONGINT;
934 1.45 chuck }
935 1.45 chuck goto rflag;
936 1.45 chuck case 'q':
937 1.45 chuck flags |= QUADINT;
938 1.45 chuck goto rflag;
939 1.45 chuck case 'c':
940 1.45 chuck *(cp = buf) = va_arg(ap, int);
941 1.45 chuck size = 1;
942 1.45 chuck sign = '\0';
943 1.45 chuck break;
944 1.45 chuck case 'D':
945 1.45 chuck flags |= LONGINT;
946 1.45 chuck /*FALLTHROUGH*/
947 1.45 chuck case 'd':
948 1.45 chuck case 'i':
949 1.45 chuck _uquad = SARG();
950 1.45 chuck if ((quad_t)_uquad < 0) {
951 1.45 chuck _uquad = -_uquad;
952 1.45 chuck sign = '-';
953 1.45 chuck }
954 1.45 chuck base = DEC;
955 1.45 chuck goto number;
956 1.45 chuck case 'n':
957 1.45 chuck #ifdef DDB
958 1.45 chuck /* XXX: non-standard '%n' format */
959 1.45 chuck /*
960 1.45 chuck * XXX: HACK! DDB wants '%n' to be a '%u' printed
961 1.45 chuck * in db_radix format. this should die since '%n'
962 1.45 chuck * is already defined in standard printf to write
963 1.45 chuck * the number of chars printed so far to the arg (which
964 1.45 chuck * should be a pointer.
965 1.45 chuck */
966 1.45 chuck if (oflags & TODDB) {
967 1.45 chuck if (db_radix == 16)
968 1.45 chuck ch = 'x'; /* convert to %x */
969 1.45 chuck else if (db_radix == 8)
970 1.45 chuck ch = 'o'; /* convert to %o */
971 1.45 chuck else
972 1.45 chuck ch = 'u'; /* convert to %u */
973 1.45 chuck
974 1.45 chuck /* ... and start again */
975 1.45 chuck goto reswitch;
976 1.45 chuck }
977 1.45 chuck
978 1.45 chuck #endif
979 1.45 chuck if (flags & QUADINT)
980 1.45 chuck *va_arg(ap, quad_t *) = ret;
981 1.45 chuck else if (flags & LONGINT)
982 1.45 chuck *va_arg(ap, long *) = ret;
983 1.45 chuck else if (flags & SHORTINT)
984 1.45 chuck *va_arg(ap, short *) = ret;
985 1.45 chuck else
986 1.45 chuck *va_arg(ap, int *) = ret;
987 1.45 chuck continue; /* no output */
988 1.45 chuck case 'O':
989 1.45 chuck flags |= LONGINT;
990 1.45 chuck /*FALLTHROUGH*/
991 1.45 chuck case 'o':
992 1.45 chuck _uquad = UARG();
993 1.45 chuck base = OCT;
994 1.45 chuck goto nosign;
995 1.45 chuck case 'p':
996 1.45 chuck /*
997 1.45 chuck * ``The argument shall be a pointer to void. The
998 1.45 chuck * value of the pointer is converted to a sequence
999 1.45 chuck * of printable characters, in an implementation-
1000 1.45 chuck * defined manner.''
1001 1.45 chuck * -- ANSI X3J11
1002 1.45 chuck */
1003 1.45 chuck /* NOSTRICT */
1004 1.45 chuck _uquad = (u_long)va_arg(ap, void *);
1005 1.45 chuck base = HEX;
1006 1.45 chuck xdigs = "0123456789abcdef";
1007 1.45 chuck flags |= HEXPREFIX;
1008 1.45 chuck ch = 'x';
1009 1.45 chuck goto nosign;
1010 1.45 chuck case 's':
1011 1.45 chuck if ((cp = va_arg(ap, char *)) == NULL)
1012 1.45 chuck cp = "(null)";
1013 1.45 chuck if (prec >= 0) {
1014 1.45 chuck /*
1015 1.45 chuck * can't use strlen; can only look for the
1016 1.45 chuck * NUL in the first `prec' characters, and
1017 1.45 chuck * strlen() will go further.
1018 1.45 chuck */
1019 1.45 chuck char *p = memchr(cp, 0, prec);
1020 1.45 chuck
1021 1.45 chuck if (p != NULL) {
1022 1.45 chuck size = p - cp;
1023 1.45 chuck if (size > prec)
1024 1.45 chuck size = prec;
1025 1.45 chuck } else
1026 1.45 chuck size = prec;
1027 1.45 chuck } else
1028 1.45 chuck size = strlen(cp);
1029 1.45 chuck sign = '\0';
1030 1.45 chuck break;
1031 1.45 chuck case 'U':
1032 1.45 chuck flags |= LONGINT;
1033 1.45 chuck /*FALLTHROUGH*/
1034 1.45 chuck case 'u':
1035 1.45 chuck _uquad = UARG();
1036 1.45 chuck base = DEC;
1037 1.45 chuck goto nosign;
1038 1.45 chuck case 'X':
1039 1.45 chuck xdigs = "0123456789ABCDEF";
1040 1.45 chuck goto hex;
1041 1.45 chuck case 'x':
1042 1.45 chuck xdigs = "0123456789abcdef";
1043 1.45 chuck hex: _uquad = UARG();
1044 1.45 chuck base = HEX;
1045 1.45 chuck /* leading 0x/X only if non-zero */
1046 1.45 chuck if (flags & ALT && _uquad != 0)
1047 1.45 chuck flags |= HEXPREFIX;
1048 1.45 chuck
1049 1.45 chuck /* unsigned conversions */
1050 1.45 chuck nosign: sign = '\0';
1051 1.45 chuck /*
1052 1.45 chuck * ``... diouXx conversions ... if a precision is
1053 1.45 chuck * specified, the 0 flag will be ignored.''
1054 1.45 chuck * -- ANSI X3J11
1055 1.45 chuck */
1056 1.45 chuck number: if ((dprec = prec) >= 0)
1057 1.45 chuck flags &= ~ZEROPAD;
1058 1.45 chuck
1059 1.45 chuck /*
1060 1.45 chuck * ``The result of converting a zero value with an
1061 1.45 chuck * explicit precision of zero is no characters.''
1062 1.45 chuck * -- ANSI X3J11
1063 1.45 chuck */
1064 1.45 chuck cp = buf + KPRINTF_BUFSIZE;
1065 1.45 chuck if (_uquad != 0 || prec != 0) {
1066 1.45 chuck /*
1067 1.45 chuck * Unsigned mod is hard, and unsigned mod
1068 1.45 chuck * by a constant is easier than that by
1069 1.45 chuck * a variable; hence this switch.
1070 1.45 chuck */
1071 1.45 chuck switch (base) {
1072 1.45 chuck case OCT:
1073 1.45 chuck do {
1074 1.45 chuck *--cp = to_char(_uquad & 7);
1075 1.45 chuck _uquad >>= 3;
1076 1.45 chuck } while (_uquad);
1077 1.45 chuck /* handle octal leading 0 */
1078 1.45 chuck if (flags & ALT && *cp != '0')
1079 1.45 chuck *--cp = '0';
1080 1.45 chuck break;
1081 1.45 chuck
1082 1.45 chuck case DEC:
1083 1.45 chuck /* many numbers are 1 digit */
1084 1.45 chuck while (_uquad >= 10) {
1085 1.45 chuck *--cp = to_char(_uquad % 10);
1086 1.45 chuck _uquad /= 10;
1087 1.45 chuck }
1088 1.45 chuck *--cp = to_char(_uquad);
1089 1.45 chuck break;
1090 1.45 chuck
1091 1.45 chuck case HEX:
1092 1.45 chuck do {
1093 1.45 chuck *--cp = xdigs[_uquad & 15];
1094 1.45 chuck _uquad >>= 4;
1095 1.45 chuck } while (_uquad);
1096 1.45 chuck break;
1097 1.45 chuck
1098 1.45 chuck default:
1099 1.45 chuck cp = "bug in kprintf: bad base";
1100 1.45 chuck size = strlen(cp);
1101 1.45 chuck goto skipsize;
1102 1.45 chuck }
1103 1.45 chuck }
1104 1.45 chuck size = buf + KPRINTF_BUFSIZE - cp;
1105 1.45 chuck skipsize:
1106 1.45 chuck break;
1107 1.45 chuck default: /* "%?" prints ?, unless ? is NUL */
1108 1.45 chuck #ifdef DDB
1109 1.45 chuck default_case: /* DDB */
1110 1.45 chuck #endif
1111 1.45 chuck if (ch == '\0')
1112 1.45 chuck goto done;
1113 1.45 chuck /* pretend it was %c with argument ch */
1114 1.45 chuck cp = buf;
1115 1.45 chuck *cp = ch;
1116 1.45 chuck size = 1;
1117 1.45 chuck sign = '\0';
1118 1.45 chuck break;
1119 1.45 chuck }
1120 1.45 chuck
1121 1.45 chuck /*
1122 1.45 chuck * All reasonable formats wind up here. At this point, `cp'
1123 1.45 chuck * points to a string which (if not flags&LADJUST) should be
1124 1.45 chuck * padded out to `width' places. If flags&ZEROPAD, it should
1125 1.45 chuck * first be prefixed by any sign or other prefix; otherwise,
1126 1.45 chuck * it should be blank padded before the prefix is emitted.
1127 1.45 chuck * After any left-hand padding and prefixing, emit zeroes
1128 1.45 chuck * required by a decimal [diouxX] precision, then print the
1129 1.45 chuck * string proper, then emit zeroes required by any leftover
1130 1.45 chuck * floating precision; finally, if LADJUST, pad with blanks.
1131 1.45 chuck *
1132 1.45 chuck * Compute actual size, so we know how much to pad.
1133 1.45 chuck * size excludes decimal prec; realsz includes it.
1134 1.45 chuck */
1135 1.45 chuck realsz = dprec > size ? dprec : size;
1136 1.45 chuck if (sign)
1137 1.45 chuck realsz++;
1138 1.45 chuck else if (flags & HEXPREFIX)
1139 1.45 chuck realsz+= 2;
1140 1.45 chuck
1141 1.45 chuck /* right-adjusting blank padding */
1142 1.45 chuck if ((flags & (LADJUST|ZEROPAD)) == 0) {
1143 1.45 chuck n = width - realsz;
1144 1.45 chuck while (n-- > 0)
1145 1.45 chuck KPRINTF_PUTCHAR(' ');
1146 1.45 chuck }
1147 1.45 chuck
1148 1.45 chuck /* prefix */
1149 1.45 chuck if (sign) {
1150 1.45 chuck KPRINTF_PUTCHAR(sign);
1151 1.45 chuck } else if (flags & HEXPREFIX) {
1152 1.45 chuck KPRINTF_PUTCHAR('0');
1153 1.45 chuck KPRINTF_PUTCHAR(ch);
1154 1.45 chuck }
1155 1.45 chuck
1156 1.45 chuck /* right-adjusting zero padding */
1157 1.45 chuck if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) {
1158 1.45 chuck n = width - realsz;
1159 1.45 chuck while (n-- > 0)
1160 1.45 chuck KPRINTF_PUTCHAR('0');
1161 1.45 chuck }
1162 1.45 chuck
1163 1.45 chuck /* leading zeroes from decimal precision */
1164 1.45 chuck n = dprec - size;
1165 1.45 chuck while (n-- > 0)
1166 1.45 chuck KPRINTF_PUTCHAR('0');
1167 1.45 chuck
1168 1.45 chuck /* the string or number proper */
1169 1.45 chuck while (size--)
1170 1.45 chuck KPRINTF_PUTCHAR(*cp++);
1171 1.45 chuck /* left-adjusting padding (always blank) */
1172 1.45 chuck if (flags & LADJUST) {
1173 1.45 chuck n = width - realsz;
1174 1.45 chuck while (n-- > 0)
1175 1.45 chuck KPRINTF_PUTCHAR(' ');
1176 1.45 chuck }
1177 1.45 chuck
1178 1.45 chuck /* finally, adjust ret */
1179 1.45 chuck ret += width > realsz ? width : realsz;
1180 1.45 chuck
1181 1.45 chuck }
1182 1.45 chuck done:
1183 1.45 chuck return (ret);
1184 1.45 chuck /* NOTREACHED */
1185 1.12 cgd }
1186