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