Home | History | Annotate | Line # | Download | only in libsa
subr_prf.c revision 1.7
      1 /*	$NetBSD: subr_prf.c,v 1.7 2003/04/20 22:23:59 bjh21 Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)printf.c	8.1 (Berkeley) 6/11/93
     36  */
     37 
     38 /*
     39  * Scaled down version of printf(3).
     40  *
     41  * One additional format:
     42  *
     43  * The format %b is supported to decode error registers.
     44  * Its usage is:
     45  *
     46  *	printf("reg=%b\n", regval, "<base><arg>*");
     47  *
     48  * where <base> is the output base expressed as a control character, e.g.
     49  * \10 gives octal; \20 gives hex.  Each arg is a sequence of characters,
     50  * the first of which gives the bit number to be inspected (origin 1), and
     51  * the next characters (up to a control character, i.e. a character <= 32),
     52  * give the name of the register.  Thus:
     53  *
     54  *	printf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
     55  *
     56  * would produce output:
     57  *
     58  *	reg=3<BITTWO,BITONE>
     59  */
     60 
     61 #include <sys/cdefs.h>
     62 #include <sys/types.h>
     63 #include <machine/stdarg.h>
     64 
     65 #include "stand.h"
     66 
     67 static void kprintn(void (*)(int), u_long, int);
     68 static void sputchar(int);
     69 static void kdoprnt(void (*)(int), const char *, va_list);
     70 
     71 static char *sbuf, *ebuf;
     72 
     73 static void
     74 sputchar(int c)
     75 {
     76 
     77 	if (sbuf < ebuf)
     78 		*sbuf++ = c;
     79 }
     80 
     81 void
     82 vprintf(const char *fmt, va_list ap)
     83 {
     84 
     85 	kdoprnt(putchar, fmt, ap);
     86 }
     87 
     88 int
     89 vsnprintf(char *buf, size_t size, const char *fmt, va_list ap)
     90 {
     91 
     92 	sbuf = buf;
     93 	ebuf = buf + size - 1;
     94 	kdoprnt(sputchar, fmt, ap);
     95 	*sbuf = '\0';
     96 	return (sbuf - buf);
     97 }
     98 
     99 static void
    100 kdoprnt(void (*put)(int), const char *fmt, va_list ap)
    101 {
    102 	char *p;
    103 	int ch, n;
    104 	unsigned long ul;
    105 	int lflag, set;
    106 
    107 	for (;;) {
    108 		while ((ch = *fmt++) != '%') {
    109 			if (ch == '\0')
    110 				return;
    111 			put(ch);
    112 		}
    113 		lflag = 0;
    114 reswitch:	switch (ch = *fmt++) {
    115 		case 'l':
    116 			lflag = 1;
    117 			goto reswitch;
    118 		case 'b':
    119 			ul = va_arg(ap, int);
    120 			p = va_arg(ap, char *);
    121 			kprintn(put, ul, *p++);
    122 
    123 			if (!ul)
    124 				break;
    125 
    126 			for (set = 0; (n = *p++);) {
    127 				if (ul & (1 << (n - 1))) {
    128 					put(set ? ',' : '<');
    129 					for (; (n = *p) > ' '; ++p)
    130 						put(n);
    131 					set = 1;
    132 				} else
    133 					for (; *p > ' '; ++p);
    134 			}
    135 			if (set)
    136 				put('>');
    137 			break;
    138 		case 'c':
    139 			ch = va_arg(ap, int);
    140 				put(ch & 0x7f);
    141 			break;
    142 		case 's':
    143 			p = va_arg(ap, char *);
    144 			while ((ch = *p++))
    145 				put(ch);
    146 			break;
    147 		case 'd':
    148 			ul = lflag ?
    149 			    va_arg(ap, long) : va_arg(ap, int);
    150 			if ((long)ul < 0) {
    151 				put('-');
    152 				ul = -(long)ul;
    153 			}
    154 			kprintn(put, ul, 10);
    155 			break;
    156 		case 'o':
    157 			ul = lflag ?
    158 			    va_arg(ap, u_long) : va_arg(ap, u_int);
    159 			kprintn(put, ul, 8);
    160 			break;
    161 		case 'u':
    162 			ul = lflag ?
    163 			    va_arg(ap, u_long) : va_arg(ap, u_int);
    164 			kprintn(put, ul, 10);
    165 			break;
    166 		case 'p':
    167 			put('0');
    168 			put('x');
    169 			lflag = 1;
    170 			/* fall through */
    171 		case 'x':
    172 			ul = lflag ?
    173 			    va_arg(ap, u_long) : va_arg(ap, u_int);
    174 			kprintn(put, ul, 16);
    175 			break;
    176 		default:
    177 			put('%');
    178 			if (lflag)
    179 				put('l');
    180 			if (ch == '\0')
    181 				return;
    182 			put(ch);
    183 		}
    184 	}
    185 }
    186 
    187 static void
    188 kprintn(void (*put)(int), unsigned long ul, int base)
    189 {
    190 					/* hold a long in base 8 */
    191 	char *p, buf[(sizeof(long) * NBBY / 3) + 1];
    192 
    193 	p = buf;
    194 	do {
    195 		*p++ = "0123456789abcdef"[ul % base];
    196 	} while (ul /= base);
    197 	do {
    198 		put(*--p);
    199 	} while (p > buf);
    200 }
    201