xref: /dflybsd-src/lib/libc/stdio/xprintf_str.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
1*86d7f5d3SJohn Marino /*-
2*86d7f5d3SJohn Marino  * Copyright (c) 2005 Poul-Henning Kamp
3*86d7f5d3SJohn Marino  * Copyright (c) 1990, 1993
4*86d7f5d3SJohn Marino  *	The Regents of the University of California.  All rights reserved.
5*86d7f5d3SJohn Marino  *
6*86d7f5d3SJohn Marino  * This code is derived from software contributed to Berkeley by
7*86d7f5d3SJohn Marino  * Chris Torek.
8*86d7f5d3SJohn Marino  *
9*86d7f5d3SJohn Marino  * Redistribution and use in source and binary forms, with or without
10*86d7f5d3SJohn Marino  * modification, are permitted provided that the following conditions
11*86d7f5d3SJohn Marino  * are met:
12*86d7f5d3SJohn Marino  * 1. Redistributions of source code must retain the above copyright
13*86d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer.
14*86d7f5d3SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
15*86d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
16*86d7f5d3SJohn Marino  *    documentation and/or other materials provided with the distribution.
17*86d7f5d3SJohn Marino  * 3. Neither the name of the University nor the names of its contributors
18*86d7f5d3SJohn Marino  *    may be used to endorse or promote products derived from this software
19*86d7f5d3SJohn Marino  *    without specific prior written permission.
20*86d7f5d3SJohn Marino  *
21*86d7f5d3SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22*86d7f5d3SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23*86d7f5d3SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24*86d7f5d3SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25*86d7f5d3SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*86d7f5d3SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27*86d7f5d3SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28*86d7f5d3SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29*86d7f5d3SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30*86d7f5d3SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*86d7f5d3SJohn Marino  * SUCH DAMAGE.
32*86d7f5d3SJohn Marino  *
33*86d7f5d3SJohn Marino  * $FreeBSD: src/lib/libc/stdio/xprintf_str.c,v 1.1 2005/12/16 18:56:38 phk Exp $
34*86d7f5d3SJohn Marino  */
35*86d7f5d3SJohn Marino 
36*86d7f5d3SJohn Marino #include "namespace.h"
37*86d7f5d3SJohn Marino #include <stdio.h>
38*86d7f5d3SJohn Marino #include <stdlib.h>
39*86d7f5d3SJohn Marino #include <string.h>
40*86d7f5d3SJohn Marino #include <limits.h>
41*86d7f5d3SJohn Marino #include <stdint.h>
42*86d7f5d3SJohn Marino #include <assert.h>
43*86d7f5d3SJohn Marino #include <wchar.h>
44*86d7f5d3SJohn Marino #include "un-namespace.h"
45*86d7f5d3SJohn Marino #include "printf.h"
46*86d7f5d3SJohn Marino 
47*86d7f5d3SJohn Marino /*
48*86d7f5d3SJohn Marino  * Convert a wide character string argument for the %ls format to a multibyte
49*86d7f5d3SJohn Marino  * string representation. If not -1, prec specifies the maximum number of
50*86d7f5d3SJohn Marino  * bytes to output, and also means that we can't assume that the wide char.
51*86d7f5d3SJohn Marino  * string ends is null-terminated.
52*86d7f5d3SJohn Marino  */
53*86d7f5d3SJohn Marino static char *
__wcsconv(wchar_t * wcsarg,int prec)54*86d7f5d3SJohn Marino __wcsconv(wchar_t *wcsarg, int prec)
55*86d7f5d3SJohn Marino {
56*86d7f5d3SJohn Marino 	static const mbstate_t initial;
57*86d7f5d3SJohn Marino 	mbstate_t mbs;
58*86d7f5d3SJohn Marino 	char buf[MB_LEN_MAX];
59*86d7f5d3SJohn Marino 	wchar_t *p;
60*86d7f5d3SJohn Marino 	char *convbuf;
61*86d7f5d3SJohn Marino 	size_t clen, nbytes;
62*86d7f5d3SJohn Marino 
63*86d7f5d3SJohn Marino 	/* Allocate space for the maximum number of bytes we could output. */
64*86d7f5d3SJohn Marino 	if (prec < 0) {
65*86d7f5d3SJohn Marino 		p = wcsarg;
66*86d7f5d3SJohn Marino 		mbs = initial;
67*86d7f5d3SJohn Marino 		nbytes = wcsrtombs(NULL, (const wchar_t **)&p, 0, &mbs);
68*86d7f5d3SJohn Marino 		if (nbytes == (size_t)-1)
69*86d7f5d3SJohn Marino 			return (NULL);
70*86d7f5d3SJohn Marino 	} else {
71*86d7f5d3SJohn Marino 		/*
72*86d7f5d3SJohn Marino 		 * Optimisation: if the output precision is small enough,
73*86d7f5d3SJohn Marino 		 * just allocate enough memory for the maximum instead of
74*86d7f5d3SJohn Marino 		 * scanning the string.
75*86d7f5d3SJohn Marino 		 */
76*86d7f5d3SJohn Marino 		if (prec < 128)
77*86d7f5d3SJohn Marino 			nbytes = prec;
78*86d7f5d3SJohn Marino 		else {
79*86d7f5d3SJohn Marino 			nbytes = 0;
80*86d7f5d3SJohn Marino 			p = wcsarg;
81*86d7f5d3SJohn Marino 			mbs = initial;
82*86d7f5d3SJohn Marino 			for (;;) {
83*86d7f5d3SJohn Marino 				clen = wcrtomb(buf, *p++, &mbs);
84*86d7f5d3SJohn Marino 				if (clen == 0 || clen == (size_t)-1 ||
85*86d7f5d3SJohn Marino 				    (int)(nbytes + clen) > prec)
86*86d7f5d3SJohn Marino 					break;
87*86d7f5d3SJohn Marino 				nbytes += clen;
88*86d7f5d3SJohn Marino 			}
89*86d7f5d3SJohn Marino 		}
90*86d7f5d3SJohn Marino 	}
91*86d7f5d3SJohn Marino 	if ((convbuf = malloc(nbytes + 1)) == NULL)
92*86d7f5d3SJohn Marino 		return (NULL);
93*86d7f5d3SJohn Marino 
94*86d7f5d3SJohn Marino 	/* Fill the output buffer. */
95*86d7f5d3SJohn Marino 	p = wcsarg;
96*86d7f5d3SJohn Marino 	mbs = initial;
97*86d7f5d3SJohn Marino 	if ((nbytes = wcsrtombs(convbuf, (const wchar_t **)&p,
98*86d7f5d3SJohn Marino 	    nbytes, &mbs)) == (size_t)-1) {
99*86d7f5d3SJohn Marino 		free(convbuf);
100*86d7f5d3SJohn Marino 		return (NULL);
101*86d7f5d3SJohn Marino 	}
102*86d7f5d3SJohn Marino 	convbuf[nbytes] = '\0';
103*86d7f5d3SJohn Marino 	return (convbuf);
104*86d7f5d3SJohn Marino }
105*86d7f5d3SJohn Marino 
106*86d7f5d3SJohn Marino 
107*86d7f5d3SJohn Marino /* 's' ---------------------------------------------------------------*/
108*86d7f5d3SJohn Marino 
109*86d7f5d3SJohn Marino int
__printf_arginfo_str(const struct printf_info * pi,size_t n,int * argt)110*86d7f5d3SJohn Marino __printf_arginfo_str(const struct printf_info *pi, size_t n, int *argt)
111*86d7f5d3SJohn Marino {
112*86d7f5d3SJohn Marino 
113*86d7f5d3SJohn Marino 	assert (n > 0);
114*86d7f5d3SJohn Marino 	if (pi->is_long || pi->spec == 'C')
115*86d7f5d3SJohn Marino 		argt[0] = PA_WSTRING;
116*86d7f5d3SJohn Marino 	else
117*86d7f5d3SJohn Marino 		argt[0] = PA_STRING;
118*86d7f5d3SJohn Marino 	return (1);
119*86d7f5d3SJohn Marino }
120*86d7f5d3SJohn Marino 
121*86d7f5d3SJohn Marino int
__printf_render_str(struct __printf_io * io,const struct printf_info * pi,const void * const * arg)122*86d7f5d3SJohn Marino __printf_render_str(struct __printf_io *io, const struct printf_info *pi,
123*86d7f5d3SJohn Marino 		    const void *const *arg)
124*86d7f5d3SJohn Marino {
125*86d7f5d3SJohn Marino 	const char *p;
126*86d7f5d3SJohn Marino 	wchar_t *wcp;
127*86d7f5d3SJohn Marino 	char *convbuf;
128*86d7f5d3SJohn Marino 	int l;
129*86d7f5d3SJohn Marino 
130*86d7f5d3SJohn Marino 	if (pi->is_long || pi->spec == 'S') {
131*86d7f5d3SJohn Marino 		wcp = *((wint_t **)arg[0]);
132*86d7f5d3SJohn Marino 		if (wcp == NULL)
133*86d7f5d3SJohn Marino 			return (__printf_out(io, pi, "(null)", 6));
134*86d7f5d3SJohn Marino 		convbuf = __wcsconv(wcp, pi->prec);
135*86d7f5d3SJohn Marino 		if (convbuf == NULL)
136*86d7f5d3SJohn Marino 			return (-1);
137*86d7f5d3SJohn Marino 		l = __printf_out(io, pi, convbuf, strlen(convbuf));
138*86d7f5d3SJohn Marino 		free(convbuf);
139*86d7f5d3SJohn Marino 		return (l);
140*86d7f5d3SJohn Marino 	}
141*86d7f5d3SJohn Marino 	p = *((char **)arg[0]);
142*86d7f5d3SJohn Marino 	if (p == NULL)
143*86d7f5d3SJohn Marino 		return (__printf_out(io, pi, "(null)", 6));
144*86d7f5d3SJohn Marino 	l = strlen(p);
145*86d7f5d3SJohn Marino 	if (pi->prec >= 0 && pi->prec < l)
146*86d7f5d3SJohn Marino 		l = pi->prec;
147*86d7f5d3SJohn Marino 	return (__printf_out(io, pi, p, l));
148*86d7f5d3SJohn Marino }
149*86d7f5d3SJohn Marino 
150*86d7f5d3SJohn Marino /* 'c' ---------------------------------------------------------------*/
151*86d7f5d3SJohn Marino 
152*86d7f5d3SJohn Marino int
__printf_arginfo_chr(const struct printf_info * pi,size_t n,int * argt)153*86d7f5d3SJohn Marino __printf_arginfo_chr(const struct printf_info *pi, size_t n, int *argt)
154*86d7f5d3SJohn Marino {
155*86d7f5d3SJohn Marino 
156*86d7f5d3SJohn Marino 	assert (n > 0);
157*86d7f5d3SJohn Marino 	if (pi->is_long || pi->spec == 'C')
158*86d7f5d3SJohn Marino 		argt[0] = PA_WCHAR;
159*86d7f5d3SJohn Marino 	else
160*86d7f5d3SJohn Marino 		argt[0] = PA_INT;
161*86d7f5d3SJohn Marino 	return (1);
162*86d7f5d3SJohn Marino }
163*86d7f5d3SJohn Marino 
164*86d7f5d3SJohn Marino int
__printf_render_chr(struct __printf_io * io,const struct printf_info * pi,const void * const * arg)165*86d7f5d3SJohn Marino __printf_render_chr(struct __printf_io *io, const struct printf_info *pi,
166*86d7f5d3SJohn Marino 		    const void *const *arg)
167*86d7f5d3SJohn Marino {
168*86d7f5d3SJohn Marino 	int i;
169*86d7f5d3SJohn Marino 	wint_t ii;
170*86d7f5d3SJohn Marino 	unsigned char c;
171*86d7f5d3SJohn Marino 	static const mbstate_t initial;		/* XXX: this is bogus! */
172*86d7f5d3SJohn Marino 	mbstate_t mbs;
173*86d7f5d3SJohn Marino 	size_t mbseqlen;
174*86d7f5d3SJohn Marino 	char buf[MB_CUR_MAX];
175*86d7f5d3SJohn Marino 
176*86d7f5d3SJohn Marino 	if (pi->is_long || pi->spec == 'C') {
177*86d7f5d3SJohn Marino 		ii = *((wint_t *)arg[0]);
178*86d7f5d3SJohn Marino 
179*86d7f5d3SJohn Marino 		mbs = initial;
180*86d7f5d3SJohn Marino 		mbseqlen = wcrtomb(buf, (wchar_t)ii, &mbs);
181*86d7f5d3SJohn Marino 		if (mbseqlen == (size_t) -1)
182*86d7f5d3SJohn Marino 			return (-1);
183*86d7f5d3SJohn Marino 		return (__printf_out(io, pi, buf, mbseqlen));
184*86d7f5d3SJohn Marino 	}
185*86d7f5d3SJohn Marino 	i = *((int *)arg[0]);
186*86d7f5d3SJohn Marino 	c = i;
187*86d7f5d3SJohn Marino 	i = __printf_out(io, pi, &c, 1);
188*86d7f5d3SJohn Marino 	__printf_flush(io);
189*86d7f5d3SJohn Marino 	return (i);
190*86d7f5d3SJohn Marino }
191