subr_prf.c revision 1.46 1 1.46 ross /* $NetBSD: subr_prf.c,v 1.46 1997/11/15 09:27:33 ross Exp $ */
2 1.15 cgd
3 1.12 cgd /*-
4 1.12 cgd * Copyright (c) 1986, 1988, 1991, 1993
5 1.12 cgd * The Regents of the University of California. All rights reserved.
6 1.12 cgd * (c) UNIX System Laboratories, Inc.
7 1.12 cgd * All or some portions of this file are derived from material licensed
8 1.12 cgd * to the University of California by American Telephone and Telegraph
9 1.12 cgd * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 1.12 cgd * the permission of UNIX System Laboratories, Inc.
11 1.12 cgd *
12 1.12 cgd * Redistribution and use in source and binary forms, with or without
13 1.12 cgd * modification, are permitted provided that the following conditions
14 1.12 cgd * are met:
15 1.12 cgd * 1. Redistributions of source code must retain the above copyright
16 1.12 cgd * notice, this list of conditions and the following disclaimer.
17 1.12 cgd * 2. Redistributions in binary form must reproduce the above copyright
18 1.12 cgd * notice, this list of conditions and the following disclaimer in the
19 1.12 cgd * documentation and/or other materials provided with the distribution.
20 1.12 cgd * 3. All advertising materials mentioning features or use of this software
21 1.12 cgd * must display the following acknowledgement:
22 1.12 cgd * This product includes software developed by the University of
23 1.12 cgd * California, Berkeley and its contributors.
24 1.12 cgd * 4. Neither the name of the University nor the names of its contributors
25 1.12 cgd * may be used to endorse or promote products derived from this software
26 1.12 cgd * without specific prior written permission.
27 1.12 cgd *
28 1.12 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.12 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.12 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.12 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.12 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.12 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.12 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.12 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.12 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.12 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.12 cgd * SUCH DAMAGE.
39 1.12 cgd *
40 1.15 cgd * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94
41 1.12 cgd */
42 1.12 cgd
43 1.12 cgd #include <sys/param.h>
44 1.12 cgd #include <sys/systm.h>
45 1.12 cgd #include <sys/buf.h>
46 1.12 cgd #include <sys/conf.h>
47 1.12 cgd #include <sys/reboot.h>
48 1.12 cgd #include <sys/msgbuf.h>
49 1.12 cgd #include <sys/proc.h>
50 1.12 cgd #include <sys/ioctl.h>
51 1.12 cgd #include <sys/vnode.h>
52 1.12 cgd #include <sys/file.h>
53 1.12 cgd #include <sys/tty.h>
54 1.12 cgd #include <sys/tprintf.h>
55 1.12 cgd #include <sys/syslog.h>
56 1.12 cgd #include <sys/malloc.h>
57 1.12 cgd
58 1.20 christos #include <dev/cons.h>
59 1.20 christos
60 1.12 cgd /*
61 1.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.45 chuck }
315 1.45 chuck }
316 1.45 chuck if ((flags & TOCONS) && constty == NULL && c != '\0')
317 1.45 chuck (*v_putc)(c);
318 1.45 chuck #ifdef DDB
319 1.45 chuck if (flags & TODDB)
320 1.45 chuck db_putchar(c);
321 1.45 chuck #endif
322 1.12 cgd }
323 1.12 cgd
324 1.45 chuck
325 1.12 cgd /*
326 1.45 chuck * uprintf: print to the controlling tty of the current process
327 1.45 chuck *
328 1.45 chuck * => we may block if the tty queue is full
329 1.45 chuck * => no message is printed if the queue doesn't clear in a reasonable
330 1.45 chuck * time
331 1.12 cgd */
332 1.45 chuck
333 1.12 cgd void
334 1.12 cgd #ifdef __STDC__
335 1.12 cgd uprintf(const char *fmt, ...)
336 1.12 cgd #else
337 1.14 cgd uprintf(fmt, va_alist)
338 1.12 cgd char *fmt;
339 1.20 christos va_dcl
340 1.12 cgd #endif
341 1.12 cgd {
342 1.12 cgd register struct proc *p = curproc;
343 1.12 cgd va_list ap;
344 1.12 cgd
345 1.12 cgd if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
346 1.12 cgd va_start(ap, fmt);
347 1.45 chuck kprintf(fmt, TOTTY, p->p_session->s_ttyp, NULL, ap);
348 1.12 cgd va_end(ap);
349 1.12 cgd }
350 1.12 cgd }
351 1.12 cgd
352 1.45 chuck /*
353 1.45 chuck * tprintf functions: used to send messages to a specific process
354 1.45 chuck *
355 1.45 chuck * usage:
356 1.45 chuck * get a tpr_t handle on a process "p" by using "tprintf_open(p)"
357 1.45 chuck * use the handle when calling "tprintf"
358 1.45 chuck * when done, do a "tprintf_close" to drop the handle
359 1.45 chuck */
360 1.45 chuck
361 1.45 chuck /*
362 1.45 chuck * tprintf_open: get a tprintf handle on a process "p"
363 1.45 chuck *
364 1.45 chuck * => returns NULL if process can't be printed to
365 1.45 chuck */
366 1.45 chuck
367 1.12 cgd tpr_t
368 1.12 cgd tprintf_open(p)
369 1.12 cgd register struct proc *p;
370 1.12 cgd {
371 1.12 cgd
372 1.12 cgd if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
373 1.12 cgd SESSHOLD(p->p_session);
374 1.12 cgd return ((tpr_t) p->p_session);
375 1.12 cgd }
376 1.12 cgd return ((tpr_t) NULL);
377 1.12 cgd }
378 1.12 cgd
379 1.45 chuck /*
380 1.45 chuck * tprintf_close: dispose of a tprintf handle obtained with tprintf_open
381 1.45 chuck */
382 1.45 chuck
383 1.12 cgd void
384 1.12 cgd tprintf_close(sess)
385 1.12 cgd tpr_t sess;
386 1.12 cgd {
387 1.12 cgd
388 1.12 cgd if (sess)
389 1.12 cgd SESSRELE((struct session *) sess);
390 1.12 cgd }
391 1.12 cgd
392 1.12 cgd /*
393 1.45 chuck * tprintf: given tprintf handle to a process [obtained with tprintf_open],
394 1.45 chuck * send a message to the controlling tty for that process.
395 1.45 chuck *
396 1.45 chuck * => also sends message to /dev/klog
397 1.12 cgd */
398 1.12 cgd void
399 1.12 cgd #ifdef __STDC__
400 1.12 cgd tprintf(tpr_t tpr, const char *fmt, ...)
401 1.12 cgd #else
402 1.14 cgd tprintf(tpr, fmt, va_alist)
403 1.12 cgd tpr_t tpr;
404 1.12 cgd char *fmt;
405 1.20 christos va_dcl
406 1.12 cgd #endif
407 1.12 cgd {
408 1.12 cgd register struct session *sess = (struct session *)tpr;
409 1.12 cgd struct tty *tp = NULL;
410 1.12 cgd int flags = TOLOG;
411 1.12 cgd va_list ap;
412 1.12 cgd
413 1.12 cgd logpri(LOG_INFO);
414 1.12 cgd if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
415 1.12 cgd flags |= TOTTY;
416 1.12 cgd tp = sess->s_ttyp;
417 1.12 cgd }
418 1.12 cgd va_start(ap, fmt);
419 1.45 chuck kprintf(fmt, flags, tp, NULL, ap);
420 1.12 cgd va_end(ap);
421 1.12 cgd logwakeup();
422 1.12 cgd }
423 1.12 cgd
424 1.45 chuck
425 1.12 cgd /*
426 1.45 chuck * ttyprintf: send a message to a specific tty
427 1.45 chuck *
428 1.45 chuck * => should be used only by tty driver or anything that knows the
429 1.45 chuck * underlying tty will not be revoked(2)'d away. [otherwise,
430 1.45 chuck * use tprintf]
431 1.12 cgd */
432 1.12 cgd void
433 1.12 cgd #ifdef __STDC__
434 1.12 cgd ttyprintf(struct tty *tp, const char *fmt, ...)
435 1.12 cgd #else
436 1.14 cgd ttyprintf(tp, fmt, va_alist)
437 1.12 cgd struct tty *tp;
438 1.12 cgd char *fmt;
439 1.20 christos va_dcl
440 1.12 cgd #endif
441 1.12 cgd {
442 1.12 cgd va_list ap;
443 1.12 cgd
444 1.12 cgd va_start(ap, fmt);
445 1.45 chuck kprintf(fmt, TOTTY, tp, NULL, ap);
446 1.12 cgd va_end(ap);
447 1.12 cgd }
448 1.12 cgd
449 1.45 chuck #ifdef DDB
450 1.12 cgd
451 1.12 cgd /*
452 1.45 chuck * db_printf: printf for DDB (via db_putchar)
453 1.12 cgd */
454 1.45 chuck
455 1.12 cgd void
456 1.12 cgd #ifdef __STDC__
457 1.45 chuck db_printf(const char *fmt, ...)
458 1.12 cgd #else
459 1.45 chuck db_printf(fmt, va_alist)
460 1.12 cgd char *fmt;
461 1.20 christos va_dcl
462 1.12 cgd #endif
463 1.12 cgd {
464 1.12 cgd va_list ap;
465 1.12 cgd
466 1.12 cgd va_start(ap, fmt);
467 1.45 chuck kprintf(fmt, TODDB, NULL, NULL, ap);
468 1.12 cgd va_end(ap);
469 1.12 cgd }
470 1.12 cgd
471 1.45 chuck #endif /* DDB */
472 1.12 cgd
473 1.12 cgd
474 1.45 chuck /*
475 1.45 chuck * normal kernel printf functions: printf, vprintf, sprintf
476 1.45 chuck */
477 1.12 cgd
478 1.45 chuck /*
479 1.45 chuck * printf: print a message to the console and the log
480 1.45 chuck */
481 1.12 cgd void
482 1.12 cgd #ifdef __STDC__
483 1.30 christos printf(const char *fmt, ...)
484 1.12 cgd #else
485 1.30 christos printf(fmt, va_alist)
486 1.12 cgd char *fmt;
487 1.20 christos va_dcl
488 1.12 cgd #endif
489 1.12 cgd {
490 1.12 cgd va_list ap;
491 1.45 chuck int savintr;
492 1.12 cgd
493 1.12 cgd savintr = consintr; /* disable interrupts */
494 1.12 cgd consintr = 0;
495 1.12 cgd va_start(ap, fmt);
496 1.45 chuck kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap);
497 1.12 cgd va_end(ap);
498 1.12 cgd if (!panicstr)
499 1.12 cgd logwakeup();
500 1.12 cgd consintr = savintr; /* reenable interrupts */
501 1.12 cgd }
502 1.12 cgd
503 1.45 chuck /*
504 1.45 chuck * vprintf: print a message to the console and the log [already have
505 1.45 chuck * va_alist]
506 1.45 chuck */
507 1.45 chuck
508 1.40 thorpej void
509 1.40 thorpej vprintf(fmt, ap)
510 1.40 thorpej const char *fmt;
511 1.40 thorpej va_list ap;
512 1.40 thorpej {
513 1.45 chuck int savintr;
514 1.40 thorpej
515 1.40 thorpej savintr = consintr; /* disable interrupts */
516 1.40 thorpej consintr = 0;
517 1.45 chuck kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap);
518 1.40 thorpej if (!panicstr)
519 1.40 thorpej logwakeup();
520 1.40 thorpej consintr = savintr; /* reenable interrupts */
521 1.40 thorpej }
522 1.40 thorpej
523 1.12 cgd /*
524 1.45 chuck * sprintf: print a message to a buffer
525 1.12 cgd */
526 1.45 chuck int
527 1.45 chuck #ifdef __STDC__
528 1.45 chuck sprintf(char *buf, const char *fmt, ...)
529 1.45 chuck #else
530 1.45 chuck sprintf(buf, fmt, va_alist)
531 1.45 chuck char *buf;
532 1.45 chuck const char *cfmt;
533 1.45 chuck va_dcl
534 1.45 chuck #endif
535 1.45 chuck {
536 1.45 chuck int retval;
537 1.12 cgd va_list ap;
538 1.12 cgd
539 1.45 chuck va_start(ap, fmt);
540 1.45 chuck retval = kprintf(fmt, TOBUFONLY, NULL, buf, ap);
541 1.12 cgd va_end(ap);
542 1.45 chuck *(buf + retval) = 0; /* null terminate */
543 1.45 chuck return(retval);
544 1.12 cgd }
545 1.12 cgd
546 1.12 cgd /*
547 1.45 chuck * bitmask_snprintf: print a kernel-printf "%b" message to a buffer
548 1.45 chuck *
549 1.45 chuck * => returns pointer to the buffer
550 1.45 chuck * => XXX: useful vs. kernel %b?
551 1.37 thorpej */
552 1.37 thorpej char *
553 1.37 thorpej bitmask_snprintf(ul, p, buf, buflen)
554 1.37 thorpej u_long ul;
555 1.37 thorpej const char *p;
556 1.37 thorpej char *buf;
557 1.37 thorpej size_t buflen;
558 1.37 thorpej {
559 1.37 thorpej char *bp, *q;
560 1.37 thorpej size_t left;
561 1.37 thorpej register int n;
562 1.45 chuck int tmp;
563 1.45 chuck char snbuf[KPRINTF_BUFSIZE];
564 1.37 thorpej
565 1.37 thorpej bp = buf;
566 1.37 thorpej bzero(buf, buflen);
567 1.37 thorpej
568 1.37 thorpej /*
569 1.37 thorpej * Always leave room for the trailing NULL.
570 1.37 thorpej */
571 1.37 thorpej left = buflen - 1;
572 1.37 thorpej
573 1.37 thorpej /*
574 1.37 thorpej * Print the value into the buffer. Abort if there's not
575 1.37 thorpej * enough room.
576 1.37 thorpej */
577 1.45 chuck if (buflen < KPRINTF_BUFSIZE)
578 1.37 thorpej return (buf);
579 1.37 thorpej
580 1.45 chuck if (*p == 8)
581 1.45 chuck sprintf(snbuf,"%lo", ul);
582 1.45 chuck else if (*p == 10)
583 1.45 chuck sprintf(snbuf, "%ld", ul);
584 1.45 chuck else if (*p == 16)
585 1.45 chuck sprintf(snbuf, "%lx", ul);
586 1.45 chuck else
587 1.45 chuck return(buf); /* punt if not oct, dec, or hex */
588 1.45 chuck p++;
589 1.45 chuck
590 1.45 chuck for (q = snbuf ; *q ; q++) {
591 1.45 chuck *bp++ = *q;
592 1.37 thorpej left--;
593 1.37 thorpej }
594 1.37 thorpej
595 1.37 thorpej /*
596 1.37 thorpej * If the value we printed was 0, or if we don't have room for
597 1.37 thorpej * "<x>", we're done.
598 1.37 thorpej */
599 1.37 thorpej if (ul == 0 || left < 3)
600 1.37 thorpej return (buf);
601 1.37 thorpej
602 1.37 thorpej #define PUTBYTE(b, c, l) \
603 1.37 thorpej *(b)++ = (c); \
604 1.37 thorpej if (--(l) == 0) \
605 1.37 thorpej goto out;
606 1.37 thorpej
607 1.37 thorpej for (tmp = 0; (n = *p++) != 0;) {
608 1.37 thorpej if (ul & (1 << (n - 1))) {
609 1.37 thorpej PUTBYTE(bp, tmp ? ',' : '<', left);
610 1.37 thorpej for (; (n = *p) > ' '; ++p) {
611 1.37 thorpej PUTBYTE(bp, n, left);
612 1.37 thorpej }
613 1.37 thorpej tmp = 1;
614 1.37 thorpej } else
615 1.37 thorpej for (; *p > ' '; ++p)
616 1.37 thorpej continue;
617 1.37 thorpej }
618 1.37 thorpej if (tmp)
619 1.37 thorpej *bp = '>';
620 1.37 thorpej
621 1.37 thorpej #undef PUTBYTE
622 1.37 thorpej
623 1.37 thorpej out:
624 1.37 thorpej return (buf);
625 1.45 chuck }
626 1.45 chuck
627 1.45 chuck /*
628 1.45 chuck * kprintf: scaled down version of printf(3).
629 1.45 chuck *
630 1.45 chuck * this version based on vfprintf() from libc which was derived from
631 1.45 chuck * software contributed to Berkeley by Chris Torek.
632 1.45 chuck *
633 1.45 chuck * Two additional formats:
634 1.45 chuck *
635 1.45 chuck * The format %b is supported to decode error registers.
636 1.45 chuck * Its usage is:
637 1.45 chuck *
638 1.45 chuck * printf("reg=%b\n", regval, "<base><arg>*");
639 1.45 chuck *
640 1.45 chuck * where <base> is the output base expressed as a control character, e.g.
641 1.45 chuck * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
642 1.45 chuck * the first of which gives the bit number to be inspected (origin 1), and
643 1.45 chuck * the next characters (up to a control character, i.e. a character <= 32),
644 1.45 chuck * give the name of the register. Thus:
645 1.45 chuck *
646 1.45 chuck * kprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
647 1.45 chuck *
648 1.45 chuck * would produce output:
649 1.45 chuck *
650 1.45 chuck * reg=3<BITTWO,BITONE>
651 1.45 chuck *
652 1.45 chuck * The format %: passes an additional format string and argument list
653 1.45 chuck * recursively. Its usage is:
654 1.45 chuck *
655 1.45 chuck * fn(char *fmt, ...)
656 1.45 chuck * {
657 1.45 chuck * va_list ap;
658 1.45 chuck * va_start(ap, fmt);
659 1.45 chuck * printf("prefix: %: suffix\n", fmt, ap);
660 1.45 chuck * va_end(ap);
661 1.45 chuck * }
662 1.45 chuck *
663 1.45 chuck * this is the actual printf innards
664 1.45 chuck *
665 1.45 chuck * This code is large and complicated...
666 1.45 chuck */
667 1.45 chuck
668 1.45 chuck /*
669 1.45 chuck * macros for converting digits to letters and vice versa
670 1.45 chuck */
671 1.45 chuck #define to_digit(c) ((c) - '0')
672 1.45 chuck #define is_digit(c) ((unsigned)to_digit(c) <= 9)
673 1.45 chuck #define to_char(n) ((n) + '0')
674 1.45 chuck
675 1.45 chuck /*
676 1.45 chuck * flags used during conversion.
677 1.45 chuck */
678 1.45 chuck #define ALT 0x001 /* alternate form */
679 1.45 chuck #define HEXPREFIX 0x002 /* add 0x or 0X prefix */
680 1.45 chuck #define LADJUST 0x004 /* left adjustment */
681 1.45 chuck #define LONGDBL 0x008 /* long double; unimplemented */
682 1.45 chuck #define LONGINT 0x010 /* long integer */
683 1.45 chuck #define QUADINT 0x020 /* quad integer */
684 1.45 chuck #define SHORTINT 0x040 /* short integer */
685 1.45 chuck #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */
686 1.45 chuck #define FPT 0x100 /* Floating point number */
687 1.45 chuck
688 1.45 chuck /*
689 1.45 chuck * To extend shorts properly, we need both signed and unsigned
690 1.45 chuck * argument extraction methods.
691 1.45 chuck */
692 1.45 chuck #define SARG() \
693 1.45 chuck (flags&QUADINT ? va_arg(ap, quad_t) : \
694 1.45 chuck flags&LONGINT ? va_arg(ap, long) : \
695 1.45 chuck flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
696 1.45 chuck (long)va_arg(ap, int))
697 1.45 chuck #define UARG() \
698 1.45 chuck (flags&QUADINT ? va_arg(ap, u_quad_t) : \
699 1.45 chuck flags&LONGINT ? va_arg(ap, u_long) : \
700 1.45 chuck flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
701 1.45 chuck (u_long)va_arg(ap, u_int))
702 1.45 chuck
703 1.45 chuck #define KPRINTF_PUTCHAR(C) \
704 1.45 chuck (oflags == TOBUFONLY) ? *sbuf++ = (C) : putchar((C), oflags, tp);
705 1.45 chuck
706 1.45 chuck int
707 1.45 chuck kprintf(fmt0, oflags, tp, sbuf, ap)
708 1.45 chuck const char *fmt0;
709 1.45 chuck int oflags;
710 1.45 chuck struct tty *tp;
711 1.45 chuck char *sbuf;
712 1.45 chuck va_list ap;
713 1.45 chuck {
714 1.45 chuck char *fmt; /* format string */
715 1.45 chuck int ch; /* character from fmt */
716 1.45 chuck int n; /* handy integer (short term usage) */
717 1.45 chuck char *cp; /* handy char pointer (short term usage) */
718 1.45 chuck int flags; /* flags as above */
719 1.45 chuck int ret; /* return value accumulator */
720 1.45 chuck int width; /* width from format (%8d), or 0 */
721 1.45 chuck int prec; /* precision from format (%.3d), or -1 */
722 1.45 chuck char sign; /* sign prefix (' ', '+', '-', or \0) */
723 1.45 chuck
724 1.45 chuck u_quad_t _uquad; /* integer arguments %[diouxX] */
725 1.45 chuck enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
726 1.45 chuck int dprec; /* a copy of prec if [diouxX], 0 otherwise */
727 1.45 chuck int realsz; /* field size expanded by dprec */
728 1.45 chuck int size; /* size of converted field or string */
729 1.45 chuck char *xdigs; /* digits for [xX] conversion */
730 1.45 chuck char buf[KPRINTF_BUFSIZE]; /* space for %c, %[diouxX] */
731 1.45 chuck
732 1.45 chuck cp = NULL; /* XXX: shutup gcc */
733 1.45 chuck size = 0; /* XXX: shutup gcc */
734 1.45 chuck
735 1.45 chuck fmt = (char *)fmt0;
736 1.45 chuck ret = 0;
737 1.45 chuck
738 1.45 chuck xdigs = NULL; /* XXX: shut up gcc warning */
739 1.45 chuck
740 1.45 chuck /*
741 1.45 chuck * Scan the format for conversions (`%' character).
742 1.45 chuck */
743 1.45 chuck for (;;) {
744 1.45 chuck while (*fmt != '%' && *fmt) {
745 1.45 chuck KPRINTF_PUTCHAR(*fmt++);
746 1.45 chuck ret++;
747 1.45 chuck }
748 1.45 chuck if (*fmt == 0)
749 1.45 chuck goto done;
750 1.45 chuck
751 1.45 chuck fmt++; /* skip over '%' */
752 1.45 chuck
753 1.45 chuck flags = 0;
754 1.45 chuck dprec = 0;
755 1.45 chuck width = 0;
756 1.45 chuck prec = -1;
757 1.45 chuck sign = '\0';
758 1.45 chuck
759 1.45 chuck rflag: ch = *fmt++;
760 1.45 chuck reswitch: switch (ch) {
761 1.45 chuck /* XXX: non-standard '%:' format */
762 1.45 chuck #ifndef __powerpc__
763 1.46 ross #ifndef __alpha__
764 1.45 chuck case ':':
765 1.45 chuck if (oflags != TOBUFONLY) {
766 1.45 chuck cp = va_arg(ap, char *);
767 1.45 chuck kprintf(cp, oflags, tp,
768 1.45 chuck NULL, va_arg(ap, va_list));
769 1.45 chuck }
770 1.45 chuck continue; /* no output */
771 1.46 ross #endif
772 1.45 chuck #endif
773 1.45 chuck /* XXX: non-standard '%b' format */
774 1.45 chuck case 'b': {
775 1.45 chuck char *b, *z;
776 1.45 chuck int tmp;
777 1.45 chuck _uquad = va_arg(ap, int);
778 1.45 chuck b = va_arg(ap, char *);
779 1.45 chuck if (*b == 8)
780 1.45 chuck sprintf(buf, "%qo", _uquad);
781 1.45 chuck else if (*b == 10)
782 1.45 chuck sprintf(buf, "%qd", _uquad);
783 1.45 chuck else if (*b == 16)
784 1.45 chuck sprintf(buf, "%qx", _uquad);
785 1.45 chuck else
786 1.45 chuck break;
787 1.45 chuck b++;
788 1.45 chuck
789 1.45 chuck z = buf;
790 1.45 chuck while (*z) {
791 1.45 chuck KPRINTF_PUTCHAR(*z++);
792 1.45 chuck ret++;
793 1.45 chuck }
794 1.45 chuck
795 1.45 chuck if (_uquad) {
796 1.45 chuck tmp = 0;
797 1.45 chuck while ((n = *b++) != 0) {
798 1.45 chuck if (_uquad & (1 << (n - 1))) {
799 1.45 chuck KPRINTF_PUTCHAR(tmp ? ',':'<');
800 1.45 chuck ret++;
801 1.45 chuck while ((n = *b) > ' ') {
802 1.45 chuck KPRINTF_PUTCHAR(n);
803 1.45 chuck ret++;
804 1.45 chuck b++;
805 1.45 chuck }
806 1.45 chuck tmp = 1;
807 1.45 chuck } else {
808 1.45 chuck while(*b > ' ')
809 1.45 chuck b++;
810 1.45 chuck }
811 1.45 chuck }
812 1.45 chuck if (tmp) {
813 1.45 chuck KPRINTF_PUTCHAR('>');
814 1.45 chuck ret++;
815 1.45 chuck }
816 1.45 chuck }
817 1.45 chuck continue; /* no output */
818 1.45 chuck }
819 1.45 chuck
820 1.45 chuck #ifdef DDB
821 1.45 chuck /* XXX: non-standard '%r' format (print int in db_radix) */
822 1.45 chuck case 'r':
823 1.45 chuck if ((oflags & TODDB) == 0)
824 1.45 chuck goto default_case;
825 1.45 chuck
826 1.45 chuck if (db_radix == 16)
827 1.45 chuck goto case_z; /* signed hex */
828 1.45 chuck _uquad = SARG();
829 1.45 chuck if ((quad_t)_uquad < 0) {
830 1.45 chuck _uquad = -_uquad;
831 1.45 chuck sign = '-';
832 1.45 chuck }
833 1.45 chuck base = (db_radix == 8) ? OCT : DEC;
834 1.45 chuck goto number;
835 1.45 chuck
836 1.45 chuck
837 1.45 chuck /* XXX: non-standard '%z' format ("signed hex", a "hex %i")*/
838 1.45 chuck case 'z':
839 1.45 chuck case_z:
840 1.45 chuck if ((oflags & TODDB) == 0)
841 1.45 chuck goto default_case;
842 1.45 chuck
843 1.45 chuck xdigs = "0123456789abcdef";
844 1.45 chuck ch = 'x'; /* the 'x' in '0x' (below) */
845 1.45 chuck _uquad = SARG();
846 1.45 chuck base = HEX;
847 1.45 chuck /* leading 0x/X only if non-zero */
848 1.45 chuck if (flags & ALT && _uquad != 0)
849 1.45 chuck flags |= HEXPREFIX;
850 1.45 chuck if ((quad_t)_uquad < 0) {
851 1.45 chuck _uquad = -_uquad;
852 1.45 chuck sign = '-';
853 1.45 chuck }
854 1.45 chuck goto number;
855 1.45 chuck #endif
856 1.45 chuck
857 1.45 chuck case ' ':
858 1.45 chuck /*
859 1.45 chuck * ``If the space and + flags both appear, the space
860 1.45 chuck * flag will be ignored.''
861 1.45 chuck * -- ANSI X3J11
862 1.45 chuck */
863 1.45 chuck if (!sign)
864 1.45 chuck sign = ' ';
865 1.45 chuck goto rflag;
866 1.45 chuck case '#':
867 1.45 chuck flags |= ALT;
868 1.45 chuck goto rflag;
869 1.45 chuck case '*':
870 1.45 chuck /*
871 1.45 chuck * ``A negative field width argument is taken as a
872 1.45 chuck * - flag followed by a positive field width.''
873 1.45 chuck * -- ANSI X3J11
874 1.45 chuck * They don't exclude field widths read from args.
875 1.45 chuck */
876 1.45 chuck if ((width = va_arg(ap, int)) >= 0)
877 1.45 chuck goto rflag;
878 1.45 chuck width = -width;
879 1.45 chuck /* FALLTHROUGH */
880 1.45 chuck case '-':
881 1.45 chuck flags |= LADJUST;
882 1.45 chuck goto rflag;
883 1.45 chuck case '+':
884 1.45 chuck sign = '+';
885 1.45 chuck goto rflag;
886 1.45 chuck case '.':
887 1.45 chuck if ((ch = *fmt++) == '*') {
888 1.45 chuck n = va_arg(ap, int);
889 1.45 chuck prec = n < 0 ? -1 : n;
890 1.45 chuck goto rflag;
891 1.45 chuck }
892 1.45 chuck n = 0;
893 1.45 chuck while (is_digit(ch)) {
894 1.45 chuck n = 10 * n + to_digit(ch);
895 1.45 chuck ch = *fmt++;
896 1.45 chuck }
897 1.45 chuck prec = n < 0 ? -1 : n;
898 1.45 chuck goto reswitch;
899 1.45 chuck case '0':
900 1.45 chuck /*
901 1.45 chuck * ``Note that 0 is taken as a flag, not as the
902 1.45 chuck * beginning of a field width.''
903 1.45 chuck * -- ANSI X3J11
904 1.45 chuck */
905 1.45 chuck flags |= ZEROPAD;
906 1.45 chuck goto rflag;
907 1.45 chuck case '1': case '2': case '3': case '4':
908 1.45 chuck case '5': case '6': case '7': case '8': case '9':
909 1.45 chuck n = 0;
910 1.45 chuck do {
911 1.45 chuck n = 10 * n + to_digit(ch);
912 1.45 chuck ch = *fmt++;
913 1.45 chuck } while (is_digit(ch));
914 1.45 chuck width = n;
915 1.45 chuck goto reswitch;
916 1.45 chuck case 'h':
917 1.45 chuck flags |= SHORTINT;
918 1.45 chuck goto rflag;
919 1.45 chuck case 'l':
920 1.45 chuck if (*fmt == 'l') {
921 1.45 chuck fmt++;
922 1.45 chuck flags |= QUADINT;
923 1.45 chuck } else {
924 1.45 chuck flags |= LONGINT;
925 1.45 chuck }
926 1.45 chuck goto rflag;
927 1.45 chuck case 'q':
928 1.45 chuck flags |= QUADINT;
929 1.45 chuck goto rflag;
930 1.45 chuck case 'c':
931 1.45 chuck *(cp = buf) = va_arg(ap, int);
932 1.45 chuck size = 1;
933 1.45 chuck sign = '\0';
934 1.45 chuck break;
935 1.45 chuck case 'D':
936 1.45 chuck flags |= LONGINT;
937 1.45 chuck /*FALLTHROUGH*/
938 1.45 chuck case 'd':
939 1.45 chuck case 'i':
940 1.45 chuck _uquad = SARG();
941 1.45 chuck if ((quad_t)_uquad < 0) {
942 1.45 chuck _uquad = -_uquad;
943 1.45 chuck sign = '-';
944 1.45 chuck }
945 1.45 chuck base = DEC;
946 1.45 chuck goto number;
947 1.45 chuck case 'n':
948 1.45 chuck #ifdef DDB
949 1.45 chuck /* XXX: non-standard '%n' format */
950 1.45 chuck /*
951 1.45 chuck * XXX: HACK! DDB wants '%n' to be a '%u' printed
952 1.45 chuck * in db_radix format. this should die since '%n'
953 1.45 chuck * is already defined in standard printf to write
954 1.45 chuck * the number of chars printed so far to the arg (which
955 1.45 chuck * should be a pointer.
956 1.45 chuck */
957 1.45 chuck if (oflags & TODDB) {
958 1.45 chuck if (db_radix == 16)
959 1.45 chuck ch = 'x'; /* convert to %x */
960 1.45 chuck else if (db_radix == 8)
961 1.45 chuck ch = 'o'; /* convert to %o */
962 1.45 chuck else
963 1.45 chuck ch = 'u'; /* convert to %u */
964 1.45 chuck
965 1.45 chuck /* ... and start again */
966 1.45 chuck goto reswitch;
967 1.45 chuck }
968 1.45 chuck
969 1.45 chuck #endif
970 1.45 chuck if (flags & QUADINT)
971 1.45 chuck *va_arg(ap, quad_t *) = ret;
972 1.45 chuck else if (flags & LONGINT)
973 1.45 chuck *va_arg(ap, long *) = ret;
974 1.45 chuck else if (flags & SHORTINT)
975 1.45 chuck *va_arg(ap, short *) = ret;
976 1.45 chuck else
977 1.45 chuck *va_arg(ap, int *) = ret;
978 1.45 chuck continue; /* no output */
979 1.45 chuck case 'O':
980 1.45 chuck flags |= LONGINT;
981 1.45 chuck /*FALLTHROUGH*/
982 1.45 chuck case 'o':
983 1.45 chuck _uquad = UARG();
984 1.45 chuck base = OCT;
985 1.45 chuck goto nosign;
986 1.45 chuck case 'p':
987 1.45 chuck /*
988 1.45 chuck * ``The argument shall be a pointer to void. The
989 1.45 chuck * value of the pointer is converted to a sequence
990 1.45 chuck * of printable characters, in an implementation-
991 1.45 chuck * defined manner.''
992 1.45 chuck * -- ANSI X3J11
993 1.45 chuck */
994 1.45 chuck /* NOSTRICT */
995 1.45 chuck _uquad = (u_long)va_arg(ap, void *);
996 1.45 chuck base = HEX;
997 1.45 chuck xdigs = "0123456789abcdef";
998 1.45 chuck flags |= HEXPREFIX;
999 1.45 chuck ch = 'x';
1000 1.45 chuck goto nosign;
1001 1.45 chuck case 's':
1002 1.45 chuck if ((cp = va_arg(ap, char *)) == NULL)
1003 1.45 chuck cp = "(null)";
1004 1.45 chuck if (prec >= 0) {
1005 1.45 chuck /*
1006 1.45 chuck * can't use strlen; can only look for the
1007 1.45 chuck * NUL in the first `prec' characters, and
1008 1.45 chuck * strlen() will go further.
1009 1.45 chuck */
1010 1.45 chuck char *p = memchr(cp, 0, prec);
1011 1.45 chuck
1012 1.45 chuck if (p != NULL) {
1013 1.45 chuck size = p - cp;
1014 1.45 chuck if (size > prec)
1015 1.45 chuck size = prec;
1016 1.45 chuck } else
1017 1.45 chuck size = prec;
1018 1.45 chuck } else
1019 1.45 chuck size = strlen(cp);
1020 1.45 chuck sign = '\0';
1021 1.45 chuck break;
1022 1.45 chuck case 'U':
1023 1.45 chuck flags |= LONGINT;
1024 1.45 chuck /*FALLTHROUGH*/
1025 1.45 chuck case 'u':
1026 1.45 chuck _uquad = UARG();
1027 1.45 chuck base = DEC;
1028 1.45 chuck goto nosign;
1029 1.45 chuck case 'X':
1030 1.45 chuck xdigs = "0123456789ABCDEF";
1031 1.45 chuck goto hex;
1032 1.45 chuck case 'x':
1033 1.45 chuck xdigs = "0123456789abcdef";
1034 1.45 chuck hex: _uquad = UARG();
1035 1.45 chuck base = HEX;
1036 1.45 chuck /* leading 0x/X only if non-zero */
1037 1.45 chuck if (flags & ALT && _uquad != 0)
1038 1.45 chuck flags |= HEXPREFIX;
1039 1.45 chuck
1040 1.45 chuck /* unsigned conversions */
1041 1.45 chuck nosign: sign = '\0';
1042 1.45 chuck /*
1043 1.45 chuck * ``... diouXx conversions ... if a precision is
1044 1.45 chuck * specified, the 0 flag will be ignored.''
1045 1.45 chuck * -- ANSI X3J11
1046 1.45 chuck */
1047 1.45 chuck number: if ((dprec = prec) >= 0)
1048 1.45 chuck flags &= ~ZEROPAD;
1049 1.45 chuck
1050 1.45 chuck /*
1051 1.45 chuck * ``The result of converting a zero value with an
1052 1.45 chuck * explicit precision of zero is no characters.''
1053 1.45 chuck * -- ANSI X3J11
1054 1.45 chuck */
1055 1.45 chuck cp = buf + KPRINTF_BUFSIZE;
1056 1.45 chuck if (_uquad != 0 || prec != 0) {
1057 1.45 chuck /*
1058 1.45 chuck * Unsigned mod is hard, and unsigned mod
1059 1.45 chuck * by a constant is easier than that by
1060 1.45 chuck * a variable; hence this switch.
1061 1.45 chuck */
1062 1.45 chuck switch (base) {
1063 1.45 chuck case OCT:
1064 1.45 chuck do {
1065 1.45 chuck *--cp = to_char(_uquad & 7);
1066 1.45 chuck _uquad >>= 3;
1067 1.45 chuck } while (_uquad);
1068 1.45 chuck /* handle octal leading 0 */
1069 1.45 chuck if (flags & ALT && *cp != '0')
1070 1.45 chuck *--cp = '0';
1071 1.45 chuck break;
1072 1.45 chuck
1073 1.45 chuck case DEC:
1074 1.45 chuck /* many numbers are 1 digit */
1075 1.45 chuck while (_uquad >= 10) {
1076 1.45 chuck *--cp = to_char(_uquad % 10);
1077 1.45 chuck _uquad /= 10;
1078 1.45 chuck }
1079 1.45 chuck *--cp = to_char(_uquad);
1080 1.45 chuck break;
1081 1.45 chuck
1082 1.45 chuck case HEX:
1083 1.45 chuck do {
1084 1.45 chuck *--cp = xdigs[_uquad & 15];
1085 1.45 chuck _uquad >>= 4;
1086 1.45 chuck } while (_uquad);
1087 1.45 chuck break;
1088 1.45 chuck
1089 1.45 chuck default:
1090 1.45 chuck cp = "bug in kprintf: bad base";
1091 1.45 chuck size = strlen(cp);
1092 1.45 chuck goto skipsize;
1093 1.45 chuck }
1094 1.45 chuck }
1095 1.45 chuck size = buf + KPRINTF_BUFSIZE - cp;
1096 1.45 chuck skipsize:
1097 1.45 chuck break;
1098 1.45 chuck default: /* "%?" prints ?, unless ? is NUL */
1099 1.45 chuck #ifdef DDB
1100 1.45 chuck default_case: /* DDB */
1101 1.45 chuck #endif
1102 1.45 chuck if (ch == '\0')
1103 1.45 chuck goto done;
1104 1.45 chuck /* pretend it was %c with argument ch */
1105 1.45 chuck cp = buf;
1106 1.45 chuck *cp = ch;
1107 1.45 chuck size = 1;
1108 1.45 chuck sign = '\0';
1109 1.45 chuck break;
1110 1.45 chuck }
1111 1.45 chuck
1112 1.45 chuck /*
1113 1.45 chuck * All reasonable formats wind up here. At this point, `cp'
1114 1.45 chuck * points to a string which (if not flags&LADJUST) should be
1115 1.45 chuck * padded out to `width' places. If flags&ZEROPAD, it should
1116 1.45 chuck * first be prefixed by any sign or other prefix; otherwise,
1117 1.45 chuck * it should be blank padded before the prefix is emitted.
1118 1.45 chuck * After any left-hand padding and prefixing, emit zeroes
1119 1.45 chuck * required by a decimal [diouxX] precision, then print the
1120 1.45 chuck * string proper, then emit zeroes required by any leftover
1121 1.45 chuck * floating precision; finally, if LADJUST, pad with blanks.
1122 1.45 chuck *
1123 1.45 chuck * Compute actual size, so we know how much to pad.
1124 1.45 chuck * size excludes decimal prec; realsz includes it.
1125 1.45 chuck */
1126 1.45 chuck realsz = dprec > size ? dprec : size;
1127 1.45 chuck if (sign)
1128 1.45 chuck realsz++;
1129 1.45 chuck else if (flags & HEXPREFIX)
1130 1.45 chuck realsz+= 2;
1131 1.45 chuck
1132 1.45 chuck /* right-adjusting blank padding */
1133 1.45 chuck if ((flags & (LADJUST|ZEROPAD)) == 0) {
1134 1.45 chuck n = width - realsz;
1135 1.45 chuck while (n-- > 0)
1136 1.45 chuck KPRINTF_PUTCHAR(' ');
1137 1.45 chuck }
1138 1.45 chuck
1139 1.45 chuck /* prefix */
1140 1.45 chuck if (sign) {
1141 1.45 chuck KPRINTF_PUTCHAR(sign);
1142 1.45 chuck } else if (flags & HEXPREFIX) {
1143 1.45 chuck KPRINTF_PUTCHAR('0');
1144 1.45 chuck KPRINTF_PUTCHAR(ch);
1145 1.45 chuck }
1146 1.45 chuck
1147 1.45 chuck /* right-adjusting zero padding */
1148 1.45 chuck if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) {
1149 1.45 chuck n = width - realsz;
1150 1.45 chuck while (n-- > 0)
1151 1.45 chuck KPRINTF_PUTCHAR('0');
1152 1.45 chuck }
1153 1.45 chuck
1154 1.45 chuck /* leading zeroes from decimal precision */
1155 1.45 chuck n = dprec - size;
1156 1.45 chuck while (n-- > 0)
1157 1.45 chuck KPRINTF_PUTCHAR('0');
1158 1.45 chuck
1159 1.45 chuck /* the string or number proper */
1160 1.45 chuck while (size--)
1161 1.45 chuck KPRINTF_PUTCHAR(*cp++);
1162 1.45 chuck /* left-adjusting padding (always blank) */
1163 1.45 chuck if (flags & LADJUST) {
1164 1.45 chuck n = width - realsz;
1165 1.45 chuck while (n-- > 0)
1166 1.45 chuck KPRINTF_PUTCHAR(' ');
1167 1.45 chuck }
1168 1.45 chuck
1169 1.45 chuck /* finally, adjust ret */
1170 1.45 chuck ret += width > realsz ? width : realsz;
1171 1.45 chuck
1172 1.45 chuck }
1173 1.45 chuck done:
1174 1.45 chuck return (ret);
1175 1.45 chuck /* NOTREACHED */
1176 1.12 cgd }
1177