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