Home | History | Annotate | Line # | Download | only in libsa
printf.c revision 1.6
      1 /*	$NetBSD: printf.c,v 1.6 1995/09/03 20:51:21 pk 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 #ifdef __STDC__
     64 #include <stdarg.h>
     65 #else
     66 #include <varargs.h>
     67 #endif
     68 
     69 #include "stand.h"
     70 
     71 static void kprintn __P((void (*)(int), u_long, int));
     72 static void sputchar __P((int));
     73 static void kprintf __P((void (*)(int), const char *, va_list));
     74 
     75 static char *sbuf;
     76 
     77 static void
     78 sputchar(c)
     79 	int c;
     80 {
     81 	*sbuf++ = c;
     82 }
     83 
     84 void
     85 #ifdef __STDC__
     86 sprintf(char *buf, const char *fmt, ...)
     87 #else
     88 sprintf(buf, fmt, va_alist)
     89 	char *buf, *fmt;
     90 #endif
     91 {
     92 	va_list ap;
     93 
     94 	sbuf = buf;
     95 #ifdef __STDC__
     96 	va_start(ap, fmt);
     97 #else
     98 	va_start(ap);
     99 #endif
    100 	kprintf(sputchar, fmt, ap);
    101 	va_end(ap);
    102 	*sbuf = '\0';
    103 }
    104 
    105 void
    106 #ifdef __STDC__
    107 printf(const char *fmt, ...)
    108 #else
    109 printf(fmt, va_alist)
    110 	char *fmt;
    111 #endif
    112 {
    113 	va_list ap;
    114 
    115 #ifdef __STDC__
    116 	va_start(ap, fmt);
    117 #else
    118 	va_start(ap);
    119 #endif
    120 	kprintf(putchar, fmt, ap);
    121 	va_end(ap);
    122 }
    123 
    124 void
    125 kprintf(put, fmt, ap)
    126 	void (*put)__P((int));
    127 	const char *fmt;
    128 	va_list ap;
    129 {
    130 	register char *p;
    131 	register int ch, n;
    132 	unsigned long ul;
    133 	int lflag, set;
    134 
    135 	for (;;) {
    136 		while ((ch = *fmt++) != '%') {
    137 			if (ch == '\0')
    138 				return;
    139 			put(ch);
    140 		}
    141 		lflag = 0;
    142 reswitch:	switch (ch = *fmt++) {
    143 		case 'l':
    144 			lflag = 1;
    145 			goto reswitch;
    146 		case 'b':
    147 			ul = va_arg(ap, int);
    148 			p = va_arg(ap, char *);
    149 			kprintn(put, ul, *p++);
    150 
    151 			if (!ul)
    152 				break;
    153 
    154 			for (set = 0; (n = *p++);) {
    155 				if (ul & (1 << (n - 1))) {
    156 					put(set ? ',' : '<');
    157 					for (; (n = *p) > ' '; ++p)
    158 						put(n);
    159 					set = 1;
    160 				} else
    161 					for (; *p > ' '; ++p);
    162 			}
    163 			if (set)
    164 				put('>');
    165 			break;
    166 		case 'c':
    167 			ch = va_arg(ap, int);
    168 				put(ch & 0x7f);
    169 			break;
    170 		case 's':
    171 			p = va_arg(ap, char *);
    172 			while ((ch = *p++))
    173 				put(ch);
    174 			break;
    175 		case 'd':
    176 			ul = lflag ?
    177 			    va_arg(ap, long) : va_arg(ap, int);
    178 			if ((long)ul < 0) {
    179 				put('-');
    180 				ul = -(long)ul;
    181 			}
    182 			kprintn(put, ul, 10);
    183 			break;
    184 		case 'o':
    185 			ul = lflag ?
    186 			    va_arg(ap, u_long) : va_arg(ap, u_int);
    187 			kprintn(put, ul, 8);
    188 			break;
    189 		case 'u':
    190 			ul = lflag ?
    191 			    va_arg(ap, u_long) : va_arg(ap, u_int);
    192 			kprintn(put, ul, 10);
    193 			break;
    194 		case 'x':
    195 			ul = lflag ?
    196 			    va_arg(ap, u_long) : va_arg(ap, u_int);
    197 			kprintn(put, ul, 16);
    198 			break;
    199 		default:
    200 			put('%');
    201 			if (lflag)
    202 				put('l');
    203 			put(ch);
    204 		}
    205 	}
    206 	va_end(ap);
    207 }
    208 
    209 static void
    210 kprintn(put, ul, base)
    211 	void (*put)__P((int));
    212 	unsigned long ul;
    213 	int base;
    214 {
    215 					/* hold a long in base 8 */
    216 	char *p, buf[(sizeof(long) * NBBY / 3) + 1];
    217 
    218 	p = buf;
    219 	do {
    220 		*p++ = "0123456789abcdef"[ul % base];
    221 	} while (ul /= base);
    222 	do {
    223 		put(*--p);
    224 	} while (p > buf);
    225 }
    226 
    227 void
    228 twiddle()
    229 {
    230 	static int pos;
    231 
    232 	putchar("|/-\\"[pos++ & 3]);
    233 	putchar('\b');
    234 }
    235