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 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #include <sys/promif.h>
28*7c478bd9Sstevel@tonic-gate #include <sys/promimpl.h>
29*7c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate static void _doprint(const char *, va_list, void (*)(char, char **), char **);
32*7c478bd9Sstevel@tonic-gate static void _printn(uint64_t, int, int, int, void (*)(char, char **), char **);
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate /*
35*7c478bd9Sstevel@tonic-gate  * Emit character functions...
36*7c478bd9Sstevel@tonic-gate  */
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
39*7c478bd9Sstevel@tonic-gate static void
_pput(char c,char ** p)40*7c478bd9Sstevel@tonic-gate _pput(char c, char **p)
41*7c478bd9Sstevel@tonic-gate {
42*7c478bd9Sstevel@tonic-gate 	(void) prom_putchar(c);
43*7c478bd9Sstevel@tonic-gate }
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate static void
_sput(char c,char ** p)46*7c478bd9Sstevel@tonic-gate _sput(char c, char **p)
47*7c478bd9Sstevel@tonic-gate {
48*7c478bd9Sstevel@tonic-gate 	**p = c;
49*7c478bd9Sstevel@tonic-gate 	*p += 1;
50*7c478bd9Sstevel@tonic-gate }
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate /*VARARGS1*/
53*7c478bd9Sstevel@tonic-gate void
prom_printf(const char * fmt,...)54*7c478bd9Sstevel@tonic-gate prom_printf(const char *fmt, ...)
55*7c478bd9Sstevel@tonic-gate {
56*7c478bd9Sstevel@tonic-gate 	va_list adx;
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate 	va_start(adx, fmt);
59*7c478bd9Sstevel@tonic-gate 	(void) _doprint(fmt, adx, _pput, (char **)0);
60*7c478bd9Sstevel@tonic-gate 	va_end(adx);
61*7c478bd9Sstevel@tonic-gate }
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate void
prom_vprintf(const char * fmt,va_list adx)64*7c478bd9Sstevel@tonic-gate prom_vprintf(const char *fmt, va_list adx)
65*7c478bd9Sstevel@tonic-gate {
66*7c478bd9Sstevel@tonic-gate 	va_list tadx;
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate 	va_copy(tadx, adx);
69*7c478bd9Sstevel@tonic-gate 	(void) _doprint(fmt, tadx, _pput, (char **)0);
70*7c478bd9Sstevel@tonic-gate 	va_end(tadx);
71*7c478bd9Sstevel@tonic-gate }
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate /*VARARGS2*/
74*7c478bd9Sstevel@tonic-gate char *
prom_sprintf(char * s,const char * fmt,...)75*7c478bd9Sstevel@tonic-gate prom_sprintf(char *s, const char *fmt, ...)
76*7c478bd9Sstevel@tonic-gate {
77*7c478bd9Sstevel@tonic-gate 	char *bp = s;
78*7c478bd9Sstevel@tonic-gate 	va_list adx;
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	va_start(adx, fmt);
81*7c478bd9Sstevel@tonic-gate 	(void) _doprint(fmt, adx, _sput, &bp);
82*7c478bd9Sstevel@tonic-gate 	*bp++ = (char)0;
83*7c478bd9Sstevel@tonic-gate 	va_end(adx);
84*7c478bd9Sstevel@tonic-gate 	return (s);
85*7c478bd9Sstevel@tonic-gate }
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate char *
prom_vsprintf(char * s,const char * fmt,va_list adx)88*7c478bd9Sstevel@tonic-gate prom_vsprintf(char *s, const char *fmt, va_list adx)
89*7c478bd9Sstevel@tonic-gate {
90*7c478bd9Sstevel@tonic-gate 	char *bp = s;
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	(void) _doprint(fmt, adx, _sput, &bp);
93*7c478bd9Sstevel@tonic-gate 	*bp++ = (char)0;
94*7c478bd9Sstevel@tonic-gate 	return (s);
95*7c478bd9Sstevel@tonic-gate }
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate static void
_doprint(const char * fmt,va_list adx,void (* emit)(char,char **),char ** bp)98*7c478bd9Sstevel@tonic-gate _doprint(const char *fmt, va_list adx, void (*emit)(char, char **), char **bp)
99*7c478bd9Sstevel@tonic-gate {
100*7c478bd9Sstevel@tonic-gate 	int b, c, i, pad, width, ells;
101*7c478bd9Sstevel@tonic-gate 	register char *s;
102*7c478bd9Sstevel@tonic-gate 	int64_t	l;
103*7c478bd9Sstevel@tonic-gate 	uint64_t ul;
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate loop:
106*7c478bd9Sstevel@tonic-gate 	width = 0;
107*7c478bd9Sstevel@tonic-gate 	while ((c = *fmt++) != '%') {
108*7c478bd9Sstevel@tonic-gate 		if (c == '\0')
109*7c478bd9Sstevel@tonic-gate 			return;
110*7c478bd9Sstevel@tonic-gate 		if (c == '\n')
111*7c478bd9Sstevel@tonic-gate 			(*emit)('\r', bp);
112*7c478bd9Sstevel@tonic-gate 		(*emit)(c, bp);
113*7c478bd9Sstevel@tonic-gate 	}
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 	c = *fmt++;
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 	for (pad = ' '; c == '0'; c = *fmt++)
118*7c478bd9Sstevel@tonic-gate 		pad = '0';
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 	for (width = 0; c >= '0' && c <= '9'; c = *fmt++)
121*7c478bd9Sstevel@tonic-gate 		width = (width * 10) + (c - '0');
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 	for (ells = 0; c == 'l'; c = *fmt++)
124*7c478bd9Sstevel@tonic-gate 		ells++;
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	switch (c) {
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 	case 'd':
129*7c478bd9Sstevel@tonic-gate 	case 'D':
130*7c478bd9Sstevel@tonic-gate 		b = 10;
131*7c478bd9Sstevel@tonic-gate 		if (ells == 0)
132*7c478bd9Sstevel@tonic-gate 			l = (int64_t)va_arg(adx, int);
133*7c478bd9Sstevel@tonic-gate 		else if (ells == 1)
134*7c478bd9Sstevel@tonic-gate 			l = (int64_t)va_arg(adx, long);
135*7c478bd9Sstevel@tonic-gate 		else
136*7c478bd9Sstevel@tonic-gate 			l = (int64_t)va_arg(adx, int64_t);
137*7c478bd9Sstevel@tonic-gate 		if (l < 0) {
138*7c478bd9Sstevel@tonic-gate 			(*emit)('-', bp);
139*7c478bd9Sstevel@tonic-gate 			width--;
140*7c478bd9Sstevel@tonic-gate 			ul = -l;
141*7c478bd9Sstevel@tonic-gate 		} else
142*7c478bd9Sstevel@tonic-gate 			ul = l;
143*7c478bd9Sstevel@tonic-gate 		goto number;
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 	case 'p':
146*7c478bd9Sstevel@tonic-gate 		ells = 1;
147*7c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
148*7c478bd9Sstevel@tonic-gate 	case 'x':
149*7c478bd9Sstevel@tonic-gate 	case 'X':
150*7c478bd9Sstevel@tonic-gate 		b = 16;
151*7c478bd9Sstevel@tonic-gate 		goto u_number;
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 	case 'u':
154*7c478bd9Sstevel@tonic-gate 		b = 10;
155*7c478bd9Sstevel@tonic-gate 		goto u_number;
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 	case 'o':
158*7c478bd9Sstevel@tonic-gate 	case 'O':
159*7c478bd9Sstevel@tonic-gate 		b = 8;
160*7c478bd9Sstevel@tonic-gate u_number:
161*7c478bd9Sstevel@tonic-gate 		if (ells == 0)
162*7c478bd9Sstevel@tonic-gate 			ul = (uint64_t)va_arg(adx, uint_t);
163*7c478bd9Sstevel@tonic-gate 		else if (ells == 1)
164*7c478bd9Sstevel@tonic-gate 			ul = (uint64_t)va_arg(adx, ulong_t);
165*7c478bd9Sstevel@tonic-gate 		else
166*7c478bd9Sstevel@tonic-gate 			ul = (uint64_t)va_arg(adx, uint64_t);
167*7c478bd9Sstevel@tonic-gate number:
168*7c478bd9Sstevel@tonic-gate 		_printn(ul, b, width, pad, emit, bp);
169*7c478bd9Sstevel@tonic-gate 		break;
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate 	case 'c':
172*7c478bd9Sstevel@tonic-gate 		b = va_arg(adx, int);
173*7c478bd9Sstevel@tonic-gate 		for (i = 24; i >= 0; i -= 8)
174*7c478bd9Sstevel@tonic-gate 			if ((c = ((b >> i) & 0x7f)) != 0) {
175*7c478bd9Sstevel@tonic-gate 				if (c == '\n')
176*7c478bd9Sstevel@tonic-gate 					(*emit)('\r', bp);
177*7c478bd9Sstevel@tonic-gate 				(*emit)(c, bp);
178*7c478bd9Sstevel@tonic-gate 			}
179*7c478bd9Sstevel@tonic-gate 		break;
180*7c478bd9Sstevel@tonic-gate 	case 's':
181*7c478bd9Sstevel@tonic-gate 		s = va_arg(adx, char *);
182*7c478bd9Sstevel@tonic-gate 		while ((c = *s++) != 0) {
183*7c478bd9Sstevel@tonic-gate 			if (c == '\n')
184*7c478bd9Sstevel@tonic-gate 				(*emit)('\r', bp);
185*7c478bd9Sstevel@tonic-gate 			(*emit)(c, bp);
186*7c478bd9Sstevel@tonic-gate 		}
187*7c478bd9Sstevel@tonic-gate 		break;
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate 	case '%':
190*7c478bd9Sstevel@tonic-gate 		(*emit)('%', bp);
191*7c478bd9Sstevel@tonic-gate 		break;
192*7c478bd9Sstevel@tonic-gate 	}
193*7c478bd9Sstevel@tonic-gate 	goto loop;
194*7c478bd9Sstevel@tonic-gate }
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate /*
197*7c478bd9Sstevel@tonic-gate  * Printn prints a number n in base b.
198*7c478bd9Sstevel@tonic-gate  * We don't use recursion to avoid deep kernel stacks.
199*7c478bd9Sstevel@tonic-gate  */
200*7c478bd9Sstevel@tonic-gate static void
_printn(uint64_t n,int b,int width,int pad,void (* emit)(char,char **),char ** bp)201*7c478bd9Sstevel@tonic-gate _printn(uint64_t n, int b, int width, int pad, void (*emit)(char, char **),
202*7c478bd9Sstevel@tonic-gate 	char **bp)
203*7c478bd9Sstevel@tonic-gate {
204*7c478bd9Sstevel@tonic-gate 	char prbuf[40];
205*7c478bd9Sstevel@tonic-gate 	register char *cp;
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate 	cp = prbuf;
208*7c478bd9Sstevel@tonic-gate 	do {
209*7c478bd9Sstevel@tonic-gate 		*cp++ = "0123456789abcdef"[n%b];
210*7c478bd9Sstevel@tonic-gate 		n /= b;
211*7c478bd9Sstevel@tonic-gate 		width--;
212*7c478bd9Sstevel@tonic-gate 	} while (n);
213*7c478bd9Sstevel@tonic-gate 	while (width-- > 0)
214*7c478bd9Sstevel@tonic-gate 		*cp++ = (char)pad;
215*7c478bd9Sstevel@tonic-gate 	do {
216*7c478bd9Sstevel@tonic-gate 		(*emit)(*--cp, bp);
217*7c478bd9Sstevel@tonic-gate 	} while (cp > prbuf);
218*7c478bd9Sstevel@tonic-gate }
219