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