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