1 /* $NetBSD: printf.c,v 1.5 1995/02/21 06:33:23 mycroft 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((u_long, int)); 72 73 void 74 #ifdef __STDC__ 75 printf(const char *fmt, ...) 76 #else 77 printf(fmt, va_alist) 78 char *fmt; 79 #endif 80 { 81 register char *p; 82 register int ch, n; 83 unsigned long ul; 84 int lflag, set; 85 va_list ap; 86 87 #ifdef __STDC__ 88 va_start(ap, fmt); 89 #else 90 va_start(ap); 91 #endif 92 for (;;) { 93 while ((ch = *fmt++) != '%') { 94 if (ch == '\0') 95 return; 96 putchar(ch); 97 } 98 lflag = 0; 99 reswitch: switch (ch = *fmt++) { 100 case 'l': 101 lflag = 1; 102 goto reswitch; 103 case 'b': 104 ul = va_arg(ap, int); 105 p = va_arg(ap, char *); 106 kprintn(ul, *p++); 107 108 if (!ul) 109 break; 110 111 for (set = 0; (n = *p++);) { 112 if (ul & (1 << (n - 1))) { 113 putchar(set ? ',' : '<'); 114 for (; (n = *p) > ' '; ++p) 115 putchar(n); 116 set = 1; 117 } else 118 for (; *p > ' '; ++p); 119 } 120 if (set) 121 putchar('>'); 122 break; 123 case 'c': 124 ch = va_arg(ap, int); 125 putchar(ch & 0x7f); 126 break; 127 case 's': 128 p = va_arg(ap, char *); 129 while ((ch = *p++)) 130 putchar(ch); 131 break; 132 case 'd': 133 ul = lflag ? 134 va_arg(ap, long) : va_arg(ap, int); 135 if ((long)ul < 0) { 136 putchar('-'); 137 ul = -(long)ul; 138 } 139 kprintn(ul, 10); 140 break; 141 case 'o': 142 ul = lflag ? 143 va_arg(ap, u_long) : va_arg(ap, u_int); 144 kprintn(ul, 8); 145 break; 146 case 'u': 147 ul = lflag ? 148 va_arg(ap, u_long) : va_arg(ap, u_int); 149 kprintn(ul, 10); 150 break; 151 case 'x': 152 ul = lflag ? 153 va_arg(ap, u_long) : va_arg(ap, u_int); 154 kprintn(ul, 16); 155 break; 156 default: 157 putchar('%'); 158 if (lflag) 159 putchar('l'); 160 putchar(ch); 161 } 162 } 163 va_end(ap); 164 } 165 166 static void 167 kprintn(ul, base) 168 unsigned long ul; 169 int base; 170 { 171 /* hold a long in base 8 */ 172 char *p, buf[(sizeof(long) * NBBY / 3) + 1]; 173 174 p = buf; 175 do { 176 *p++ = "0123456789abcdef"[ul % base]; 177 } while (ul /= base); 178 do { 179 putchar(*--p); 180 } while (p > buf); 181 } 182 183 void 184 twiddle() 185 { 186 static int pos; 187 188 putchar("|/-\\"[pos++ & 3]); 189 putchar('\b'); 190 } 191