1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright (c) 1995-1996, by Sun Microsystems, Inc. 24*7c478bd9Sstevel@tonic-gate * All rights reserved. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #include <sys/promif.h> 30*7c478bd9Sstevel@tonic-gate #include <sys/promimpl.h> 31*7c478bd9Sstevel@tonic-gate #include <sys/varargs.h> 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate static void _doprint(const char *, va_list, char **); 34*7c478bd9Sstevel@tonic-gate static void _printn(uint64_t, int, int, int, char **); 35*7c478bd9Sstevel@tonic-gate 36*7c478bd9Sstevel@tonic-gate /* 37*7c478bd9Sstevel@tonic-gate * Emit character functions... 38*7c478bd9Sstevel@tonic-gate */ 39*7c478bd9Sstevel@tonic-gate 40*7c478bd9Sstevel@tonic-gate static void 41*7c478bd9Sstevel@tonic-gate _pput_flush(char *start, char *end) 42*7c478bd9Sstevel@tonic-gate { 43*7c478bd9Sstevel@tonic-gate while (prom_write(prom_stdout_ihandle(), 44*7c478bd9Sstevel@tonic-gate start, end - start, 0, BYTE) == -1) 45*7c478bd9Sstevel@tonic-gate ; 46*7c478bd9Sstevel@tonic-gate } 47*7c478bd9Sstevel@tonic-gate 48*7c478bd9Sstevel@tonic-gate static void 49*7c478bd9Sstevel@tonic-gate _sput(char c, char **p) 50*7c478bd9Sstevel@tonic-gate { 51*7c478bd9Sstevel@tonic-gate **p = c; 52*7c478bd9Sstevel@tonic-gate *p += 1; 53*7c478bd9Sstevel@tonic-gate } 54*7c478bd9Sstevel@tonic-gate 55*7c478bd9Sstevel@tonic-gate /*VARARGS1*/ 56*7c478bd9Sstevel@tonic-gate void 57*7c478bd9Sstevel@tonic-gate prom_printf(const char *fmt, ...) 58*7c478bd9Sstevel@tonic-gate { 59*7c478bd9Sstevel@tonic-gate va_list adx; 60*7c478bd9Sstevel@tonic-gate 61*7c478bd9Sstevel@tonic-gate va_start(adx, fmt); 62*7c478bd9Sstevel@tonic-gate _doprint(fmt, adx, (char **)0); 63*7c478bd9Sstevel@tonic-gate va_end(adx); 64*7c478bd9Sstevel@tonic-gate } 65*7c478bd9Sstevel@tonic-gate 66*7c478bd9Sstevel@tonic-gate void 67*7c478bd9Sstevel@tonic-gate prom_vprintf(const char *fmt, va_list adx) 68*7c478bd9Sstevel@tonic-gate { 69*7c478bd9Sstevel@tonic-gate _doprint(fmt, adx, (char **)0); 70*7c478bd9Sstevel@tonic-gate } 71*7c478bd9Sstevel@tonic-gate 72*7c478bd9Sstevel@tonic-gate /*VARARGS2*/ 73*7c478bd9Sstevel@tonic-gate char * 74*7c478bd9Sstevel@tonic-gate prom_sprintf(char *s, const char *fmt, ...) 75*7c478bd9Sstevel@tonic-gate { 76*7c478bd9Sstevel@tonic-gate char *bp = s; 77*7c478bd9Sstevel@tonic-gate va_list adx; 78*7c478bd9Sstevel@tonic-gate 79*7c478bd9Sstevel@tonic-gate va_start(adx, fmt); 80*7c478bd9Sstevel@tonic-gate _doprint(fmt, adx, &bp); 81*7c478bd9Sstevel@tonic-gate *bp++ = (char)0; 82*7c478bd9Sstevel@tonic-gate va_end(adx); 83*7c478bd9Sstevel@tonic-gate return (s); 84*7c478bd9Sstevel@tonic-gate } 85*7c478bd9Sstevel@tonic-gate 86*7c478bd9Sstevel@tonic-gate char * 87*7c478bd9Sstevel@tonic-gate prom_vsprintf(char *s, const char *fmt, va_list adx) 88*7c478bd9Sstevel@tonic-gate { 89*7c478bd9Sstevel@tonic-gate char *bp = s; 90*7c478bd9Sstevel@tonic-gate 91*7c478bd9Sstevel@tonic-gate _doprint(fmt, adx, &bp); 92*7c478bd9Sstevel@tonic-gate *bp++ = (char)0; 93*7c478bd9Sstevel@tonic-gate return (s); 94*7c478bd9Sstevel@tonic-gate } 95*7c478bd9Sstevel@tonic-gate 96*7c478bd9Sstevel@tonic-gate static void 97*7c478bd9Sstevel@tonic-gate _doprint(const char *fmt, va_list adx, char **bp) 98*7c478bd9Sstevel@tonic-gate { 99*7c478bd9Sstevel@tonic-gate int b, c, i, pad, width, ells; 100*7c478bd9Sstevel@tonic-gate char *s, *start; 101*7c478bd9Sstevel@tonic-gate char localbuf[100], *lbp; 102*7c478bd9Sstevel@tonic-gate int64_t l; 103*7c478bd9Sstevel@tonic-gate uint64_t ul; 104*7c478bd9Sstevel@tonic-gate 105*7c478bd9Sstevel@tonic-gate if (bp == 0) { 106*7c478bd9Sstevel@tonic-gate bp = &lbp; 107*7c478bd9Sstevel@tonic-gate lbp = &localbuf[0]; 108*7c478bd9Sstevel@tonic-gate } 109*7c478bd9Sstevel@tonic-gate start = *bp; 110*7c478bd9Sstevel@tonic-gate loop: 111*7c478bd9Sstevel@tonic-gate width = 0; 112*7c478bd9Sstevel@tonic-gate while ((c = *fmt++) != '%') { 113*7c478bd9Sstevel@tonic-gate if (c == '\0') 114*7c478bd9Sstevel@tonic-gate goto out; 115*7c478bd9Sstevel@tonic-gate if (c == '\n') { 116*7c478bd9Sstevel@tonic-gate _sput('\r', bp); 117*7c478bd9Sstevel@tonic-gate _sput('\n', bp); 118*7c478bd9Sstevel@tonic-gate if (start == localbuf) { 119*7c478bd9Sstevel@tonic-gate _pput_flush(start, *bp); 120*7c478bd9Sstevel@tonic-gate lbp = &localbuf[0]; 121*7c478bd9Sstevel@tonic-gate } 122*7c478bd9Sstevel@tonic-gate } else 123*7c478bd9Sstevel@tonic-gate _sput((char)c, bp); 124*7c478bd9Sstevel@tonic-gate if (start == localbuf && (*bp - start > 80)) { 125*7c478bd9Sstevel@tonic-gate _pput_flush(start, *bp); 126*7c478bd9Sstevel@tonic-gate lbp = &localbuf[0]; 127*7c478bd9Sstevel@tonic-gate } 128*7c478bd9Sstevel@tonic-gate } 129*7c478bd9Sstevel@tonic-gate 130*7c478bd9Sstevel@tonic-gate c = *fmt++; 131*7c478bd9Sstevel@tonic-gate for (pad = ' '; c == '0'; c = *fmt++) 132*7c478bd9Sstevel@tonic-gate pad = '0'; 133*7c478bd9Sstevel@tonic-gate 134*7c478bd9Sstevel@tonic-gate for (width = 0; c >= '0' && c <= '9'; c = *fmt++) 135*7c478bd9Sstevel@tonic-gate width = width * 10 + c - '0'; 136*7c478bd9Sstevel@tonic-gate 137*7c478bd9Sstevel@tonic-gate for (ells = 0; c == 'l'; c = *fmt++) 138*7c478bd9Sstevel@tonic-gate ells++; 139*7c478bd9Sstevel@tonic-gate 140*7c478bd9Sstevel@tonic-gate switch (c) { 141*7c478bd9Sstevel@tonic-gate case 'd': 142*7c478bd9Sstevel@tonic-gate case 'D': 143*7c478bd9Sstevel@tonic-gate b = 10; 144*7c478bd9Sstevel@tonic-gate if (ells == 0) 145*7c478bd9Sstevel@tonic-gate l = (int64_t)va_arg(adx, int); 146*7c478bd9Sstevel@tonic-gate else if (ells == 1) 147*7c478bd9Sstevel@tonic-gate l = (int64_t)va_arg(adx, long); 148*7c478bd9Sstevel@tonic-gate else 149*7c478bd9Sstevel@tonic-gate l = (int64_t)va_arg(adx, int64_t); 150*7c478bd9Sstevel@tonic-gate if (l < 0) { 151*7c478bd9Sstevel@tonic-gate _sput('-', bp); 152*7c478bd9Sstevel@tonic-gate width--; 153*7c478bd9Sstevel@tonic-gate ul = -l; 154*7c478bd9Sstevel@tonic-gate } else 155*7c478bd9Sstevel@tonic-gate ul = l; 156*7c478bd9Sstevel@tonic-gate goto number; 157*7c478bd9Sstevel@tonic-gate 158*7c478bd9Sstevel@tonic-gate case 'p': 159*7c478bd9Sstevel@tonic-gate ells = 1; 160*7c478bd9Sstevel@tonic-gate /*FALLTHROUGH*/ 161*7c478bd9Sstevel@tonic-gate case 'x': 162*7c478bd9Sstevel@tonic-gate case 'X': 163*7c478bd9Sstevel@tonic-gate b = 16; 164*7c478bd9Sstevel@tonic-gate goto u_number; 165*7c478bd9Sstevel@tonic-gate 166*7c478bd9Sstevel@tonic-gate case 'u': 167*7c478bd9Sstevel@tonic-gate b = 10; 168*7c478bd9Sstevel@tonic-gate goto u_number; 169*7c478bd9Sstevel@tonic-gate 170*7c478bd9Sstevel@tonic-gate case 'o': 171*7c478bd9Sstevel@tonic-gate case 'O': 172*7c478bd9Sstevel@tonic-gate b = 8; 173*7c478bd9Sstevel@tonic-gate u_number: 174*7c478bd9Sstevel@tonic-gate if (ells == 0) 175*7c478bd9Sstevel@tonic-gate ul = (uint64_t)va_arg(adx, u_int); 176*7c478bd9Sstevel@tonic-gate else if (ells == 1) 177*7c478bd9Sstevel@tonic-gate ul = (uint64_t)va_arg(adx, u_long); 178*7c478bd9Sstevel@tonic-gate else 179*7c478bd9Sstevel@tonic-gate ul = (uint64_t)va_arg(adx, uint64_t); 180*7c478bd9Sstevel@tonic-gate number: 181*7c478bd9Sstevel@tonic-gate _printn(ul, b, width, pad, bp); 182*7c478bd9Sstevel@tonic-gate break; 183*7c478bd9Sstevel@tonic-gate 184*7c478bd9Sstevel@tonic-gate case 'c': 185*7c478bd9Sstevel@tonic-gate b = va_arg(adx, int); 186*7c478bd9Sstevel@tonic-gate for (i = 24; i >= 0; i -= 8) 187*7c478bd9Sstevel@tonic-gate if ((c = ((b >> i) & 0x7f)) != 0) { 188*7c478bd9Sstevel@tonic-gate if (c == '\n') 189*7c478bd9Sstevel@tonic-gate _sput('\r', bp); 190*7c478bd9Sstevel@tonic-gate _sput((char)c, bp); 191*7c478bd9Sstevel@tonic-gate } 192*7c478bd9Sstevel@tonic-gate break; 193*7c478bd9Sstevel@tonic-gate 194*7c478bd9Sstevel@tonic-gate case 's': 195*7c478bd9Sstevel@tonic-gate s = va_arg(adx, char *); 196*7c478bd9Sstevel@tonic-gate while ((c = *s++) != 0) { 197*7c478bd9Sstevel@tonic-gate if (c == '\n') 198*7c478bd9Sstevel@tonic-gate _sput('\r', bp); 199*7c478bd9Sstevel@tonic-gate _sput((char)c, bp); 200*7c478bd9Sstevel@tonic-gate if (start == localbuf && (*bp - start > 80)) { 201*7c478bd9Sstevel@tonic-gate _pput_flush(start, *bp); 202*7c478bd9Sstevel@tonic-gate lbp = &localbuf[0]; 203*7c478bd9Sstevel@tonic-gate } 204*7c478bd9Sstevel@tonic-gate } 205*7c478bd9Sstevel@tonic-gate break; 206*7c478bd9Sstevel@tonic-gate 207*7c478bd9Sstevel@tonic-gate case '%': 208*7c478bd9Sstevel@tonic-gate _sput('%', bp); 209*7c478bd9Sstevel@tonic-gate break; 210*7c478bd9Sstevel@tonic-gate } 211*7c478bd9Sstevel@tonic-gate if (start == localbuf && (*bp - start > 80)) { 212*7c478bd9Sstevel@tonic-gate _pput_flush(start, *bp); 213*7c478bd9Sstevel@tonic-gate lbp = &localbuf[0]; 214*7c478bd9Sstevel@tonic-gate } 215*7c478bd9Sstevel@tonic-gate goto loop; 216*7c478bd9Sstevel@tonic-gate out: 217*7c478bd9Sstevel@tonic-gate if (start == localbuf && (*bp - start > 0)) 218*7c478bd9Sstevel@tonic-gate _pput_flush(start, *bp); 219*7c478bd9Sstevel@tonic-gate } 220*7c478bd9Sstevel@tonic-gate 221*7c478bd9Sstevel@tonic-gate /* 222*7c478bd9Sstevel@tonic-gate * Printn prints a number n in base b. 223*7c478bd9Sstevel@tonic-gate * We don't use recursion to avoid deep kernel stacks. 224*7c478bd9Sstevel@tonic-gate */ 225*7c478bd9Sstevel@tonic-gate static void 226*7c478bd9Sstevel@tonic-gate _printn(uint64_t n, int b, int width, int pad, char **bp) 227*7c478bd9Sstevel@tonic-gate { 228*7c478bd9Sstevel@tonic-gate char prbuf[40]; 229*7c478bd9Sstevel@tonic-gate char *cp; 230*7c478bd9Sstevel@tonic-gate 231*7c478bd9Sstevel@tonic-gate cp = prbuf; 232*7c478bd9Sstevel@tonic-gate do { 233*7c478bd9Sstevel@tonic-gate *cp++ = "0123456789abcdef"[n%b]; 234*7c478bd9Sstevel@tonic-gate n /= b; 235*7c478bd9Sstevel@tonic-gate width--; 236*7c478bd9Sstevel@tonic-gate } while (n); 237*7c478bd9Sstevel@tonic-gate while (width-- > 0) 238*7c478bd9Sstevel@tonic-gate *cp++ = (char)pad; 239*7c478bd9Sstevel@tonic-gate do { 240*7c478bd9Sstevel@tonic-gate _sput(*--cp, bp); 241*7c478bd9Sstevel@tonic-gate } while (cp > prbuf); 242*7c478bd9Sstevel@tonic-gate } 243