xref: /freebsd/lib/libc/stdlib/strfmon.c (revision 6bfca4dcab07dad45a805879d954876b353c0810)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Copyright (c) 2011 The FreeBSD Foundation
8  *
9  * Portions of this software were developed by David Chisnall
10  * under sponsorship from the FreeBSD Foundation.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  */
34 
35 #include <sys/types.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <limits.h>
39 #include <locale.h>
40 #include <monetary.h>
41 #include <stdarg.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 
46 #include "xlocale_private.h"
47 
48 /* internal flags */
49 #define	NEED_GROUPING		0x01	/* print digits grouped (default) */
50 #define	SIGN_POSN_USED		0x02	/* '+' or '(' usage flag */
51 #define	LOCALE_POSN		0x04	/* use locale defined +/- (default) */
52 #define	PARENTH_POSN		0x08	/* enclose negative amount in () */
53 #define	SUPPRESS_CURR_SYMBOL	0x10	/* suppress the currency from output */
54 #define	LEFT_JUSTIFY		0x20	/* left justify */
55 #define	USE_INTL_CURRENCY	0x40	/* use international currency symbol */
56 #define	IS_NEGATIVE		0x80	/* is argument value negative ? */
57 
58 /* internal macros */
59 #define	PRINT(CH) do {						\
60 	if (dst >= s + maxsize)				\
61 		goto e2big_error;				\
62 	*dst++ = CH;						\
63 } while (0)
64 
65 #define	PRINTS(STR) do {					\
66 	char *tmps = STR;					\
67 	while (*tmps != '\0')					\
68 		PRINT(*tmps++);					\
69 } while (0)
70 
71 #define	GET_NUMBER(VAR, LOC) do {					\
72 	VAR = 0;						\
73 	while (isdigit_l((unsigned char)*fmt, LOC)) {			\
74 		if (VAR > INT_MAX / 10)				\
75 			goto e2big_error;			\
76 		VAR *= 10;					\
77 		VAR += *fmt - '0';				\
78 		if (VAR < 0)					\
79 			goto e2big_error;			\
80 		fmt++;						\
81 	}							\
82 } while (0)
83 
84 #define	GRPCPY(howmany) do {					\
85 	int i = howmany;					\
86 	while (i-- > 0) {					\
87 		avalue_size--;					\
88 		*--bufend = *(avalue + avalue_size + padded);	\
89 	}							\
90 } while (0)
91 
92 #define	GRPSEP do {						\
93 	bufend -= thousands_sep_size;				\
94 	memcpy(bufend, thousands_sep, thousands_sep_size);	\
95 	groups++;						\
96 } while (0)
97 
98 static void __setup_vars(int, char *, char *, char *, char **, struct lconv *);
99 static int __calc_left_pad(int, char *, struct lconv *);
100 static char *__format_grouped_double(double, int *, int, int, int,
101     struct lconv *, locale_t);
102 
103 static ssize_t
104 vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc,
105     const char * __restrict format, va_list ap)
106 {
107 	char		*dst;		/* output destination pointer */
108 	const char	*fmt;		/* current format poistion pointer */
109 	struct lconv	*lc;		/* pointer to lconv structure */
110 	char		*asciivalue;	/* formatted double pointer */
111 
112 	int		flags;		/* formatting options */
113 	int		pad_char;	/* padding character */
114 	int		pad_size;	/* pad size */
115 	int		width;		/* field width */
116 	int		left_prec;	/* left precision */
117 	int		right_prec;	/* right precision */
118 	double		value;		/* just value */
119 	char		space_char = ' '; /* space after currency */
120 
121 	char		cs_precedes,	/* values gathered from struct lconv */
122 			sep_by_space,
123 			sign_posn,
124 			*signstr,
125 			*currency_symbol;
126 
127 	char		*tmpptr;	/* temporary vars */
128 	int		sverrno;
129 	FIX_LOCALE(loc);
130 
131 	lc = localeconv_l(loc);
132 	dst = s;
133 	fmt = format;
134 	asciivalue = NULL;
135 	currency_symbol = NULL;
136 
137 	while (*fmt) {
138 		/* pass nonformating characters AS IS */
139 		if (*fmt != '%')
140 			goto literal;
141 
142 		/* '%' found ! */
143 
144 		/* "%%" mean just '%' */
145 		if (*(fmt + 1) == '%') {
146 			fmt++;
147 	literal:
148 			PRINT(*fmt++);
149 			continue;
150 		}
151 
152 		/* set up initial values */
153 		flags = (NEED_GROUPING|LOCALE_POSN);
154 		pad_char = ' ';		/* padding character is "space" */
155 		pad_size = 0;		/* no padding initially */
156 		left_prec = -1;		/* no left precision specified */
157 		right_prec = -1;	/* no right precision specified */
158 		width = -1;		/* no width specified */
159 		value = 0;		/* we have no value to print now */
160 
161 		/* Flags */
162 		while (1) {
163 			switch (*++fmt) {
164 				case '=':	/* fill character */
165 					pad_char = *++fmt;
166 					if (pad_char == '\0')
167 						goto format_error;
168 					continue;
169 				case '^':	/* not group currency  */
170 					flags &= ~(NEED_GROUPING);
171 					continue;
172 				case '+':	/* use locale defined signs */
173 					if (flags & SIGN_POSN_USED)
174 						goto format_error;
175 					flags |= (SIGN_POSN_USED|LOCALE_POSN);
176 					continue;
177 				case '(':	/* enclose negatives with () */
178 					if (flags & SIGN_POSN_USED)
179 						goto format_error;
180 					flags |= (SIGN_POSN_USED|PARENTH_POSN);
181 					continue;
182 				case '!':	/* suppress currency symbol */
183 					flags |= SUPPRESS_CURR_SYMBOL;
184 					continue;
185 				case '-':	/* alignment (left)  */
186 					flags |= LEFT_JUSTIFY;
187 					continue;
188 				default:
189 					break;
190 			}
191 			break;
192 		}
193 
194 		/* field Width */
195 		if (isdigit_l((unsigned char)*fmt, loc)) {
196 			GET_NUMBER(width, loc);
197 			/* Do we have enough space to put number with
198 			 * required width ?
199 			 */
200 			if ((unsigned int)width >= maxsize - (dst - s))
201 				goto e2big_error;
202 		}
203 
204 		/* Left precision */
205 		if (*fmt == '#') {
206 			if (!isdigit_l((unsigned char)*++fmt, loc))
207 				goto format_error;
208 			GET_NUMBER(left_prec, loc);
209 			if ((unsigned int)left_prec >= maxsize - (dst - s))
210 				goto e2big_error;
211 		}
212 
213 		/* Right precision */
214 		if (*fmt == '.') {
215 			if (!isdigit_l((unsigned char)*++fmt, loc))
216 				goto format_error;
217 			GET_NUMBER(right_prec, loc);
218 			if ((unsigned int)right_prec >= maxsize - (dst - s) -
219 			    left_prec)
220 				goto e2big_error;
221 		}
222 
223 		/* Conversion Characters */
224 		switch (*fmt++) {
225 			case 'i':	/* use international currency format */
226 				flags |= USE_INTL_CURRENCY;
227 				break;
228 			case 'n':	/* use national currency format */
229 				flags &= ~(USE_INTL_CURRENCY);
230 				break;
231 			default:	/* required character is missing or
232 					   premature EOS */
233 				goto format_error;
234 		}
235 
236 		if (currency_symbol != NULL)
237 			free(currency_symbol);
238 		if (flags & USE_INTL_CURRENCY) {
239 			currency_symbol = strdup(lc->int_curr_symbol);
240 			if (currency_symbol != NULL &&
241 			    strlen(currency_symbol) > 3) {
242 				space_char = currency_symbol[3];
243 				currency_symbol[3] = '\0';
244 			}
245 		} else
246 			currency_symbol = strdup(lc->currency_symbol);
247 
248 		if (currency_symbol == NULL)
249 			goto end_error;			/* ENOMEM. */
250 
251 		/* value itself */
252 		value = va_arg(ap, double);
253 
254 		/* detect sign */
255 		if (value < 0) {
256 			flags |= IS_NEGATIVE;
257 			value = -value;
258 		}
259 
260 		/* fill left_prec with amount of padding chars */
261 		if (left_prec >= 0) {
262 			pad_size = __calc_left_pad((flags ^ IS_NEGATIVE),
263 			    currency_symbol, lc) -
264 			    __calc_left_pad(flags, currency_symbol, lc);
265 			if (pad_size < 0)
266 				pad_size = 0;
267 		}
268 
269 		if (asciivalue != NULL)
270 			free(asciivalue);
271 		asciivalue = __format_grouped_double(value, &flags,
272 		    left_prec, right_prec, pad_char, lc, loc);
273 		if (asciivalue == NULL)
274 			goto end_error;		/* errno already set     */
275 						/* to ENOMEM by malloc() */
276 
277 		/* set some variables for later use */
278 		__setup_vars(flags, &cs_precedes, &sep_by_space,
279 		    &sign_posn, &signstr, lc);
280 
281 		/*
282 		 * Description of some LC_MONETARY's values:
283 		 *
284 		 * p_cs_precedes & n_cs_precedes
285 		 *
286 		 * = 1 - $currency_symbol precedes the value
287 		 *       for a monetary quantity with a non-negative value
288 		 * = 0 - symbol succeeds the value
289 		 *
290 		 * p_sep_by_space & n_sep_by_space
291 		 *
292 		 * = 0 - no space separates $currency_symbol
293 		 *       from the value for a monetary quantity with a
294 		 *       non-negative value
295 		 * = 1 - space separates the symbol from the value
296 		 * = 2 - space separates the symbol and the sign string,
297 		 *       if adjacent; otherwise, a space separates
298 		 *       the sign string from the value
299 		 *
300 		 * p_sign_posn & n_sign_posn
301 		 *
302 		 * = 0 - parentheses enclose the quantity and the
303 		 *       $currency_symbol
304 		 * = 1 - the sign string precedes the quantity and the
305 		 *       $currency_symbol
306 		 * = 2 - the sign string succeeds the quantity and the
307 		 *       $currency_symbol
308 		 * = 3 - the sign string precedes the $currency_symbol
309 		 * = 4 - the sign string succeeds the $currency_symbol
310 		 *
311 		 */
312 
313 		tmpptr = dst;
314 
315 		while (pad_size-- > 0)
316 			PRINT(' ');
317 
318 		if (sign_posn == 0 && (flags & IS_NEGATIVE))
319 			PRINT('(');
320 
321 		if (cs_precedes == 1) {
322 			if (sign_posn == 1 || sign_posn == 3) {
323 				PRINTS(signstr);
324 				if (sep_by_space == 2)
325 					PRINT(' ');
326 			}
327 
328 			if (!(flags & SUPPRESS_CURR_SYMBOL)) {
329 				PRINTS(currency_symbol);
330 
331 				if (sign_posn == 4) {
332 					if (sep_by_space == 2)
333 						PRINT(space_char);
334 					PRINTS(signstr);
335 					if (sep_by_space == 1)
336 						PRINT(' ');
337 				} else if (sep_by_space == 1)
338 					PRINT(space_char);
339 			}
340 		} else if (sign_posn == 1) {
341 			PRINTS(signstr);
342 			if (sep_by_space == 2)
343 				PRINT(' ');
344 		}
345 
346 		PRINTS(asciivalue);
347 
348 		if (cs_precedes == 0) {
349 			if (sign_posn == 3) {
350 				if (sep_by_space == 1)
351 					PRINT(' ');
352 				PRINTS(signstr);
353 			}
354 
355 			if (!(flags & SUPPRESS_CURR_SYMBOL)) {
356 				if ((sign_posn == 3 && sep_by_space == 2)
357 				    || (sep_by_space == 1
358 				    && (sign_posn == 0
359 				    || sign_posn == 1
360 				    || sign_posn == 2
361 				    || sign_posn == 4)))
362 					PRINT(space_char);
363 				PRINTS(currency_symbol);
364 				if (sign_posn == 4) {
365 					if (sep_by_space == 2)
366 						PRINT(' ');
367 					PRINTS(signstr);
368 				}
369 			}
370 		}
371 
372 		if (sign_posn == 2) {
373 			if (sep_by_space == 2)
374 				PRINT(' ');
375 			PRINTS(signstr);
376 		}
377 
378 		if (sign_posn == 0) {
379 			if (flags & IS_NEGATIVE)
380 				PRINT(')');
381 			else if (left_prec >= 0)
382 				PRINT(' ');
383 		}
384 
385 		if (dst - tmpptr < width) {
386 			if (flags & LEFT_JUSTIFY) {
387 				while (dst - tmpptr < width)
388 					PRINT(' ');
389 			} else {
390 				pad_size = dst - tmpptr;
391 				memmove(tmpptr + width - pad_size, tmpptr,
392 				    pad_size);
393 				memset(tmpptr, ' ', width - pad_size);
394 				dst += width - pad_size;
395 			}
396 		}
397 	}
398 
399 	PRINT('\0');
400 	free(asciivalue);
401 	free(currency_symbol);
402 	return (dst - s - 1);	/* return size of put data except trailing '\0' */
403 
404 e2big_error:
405 	errno = E2BIG;
406 	goto end_error;
407 
408 format_error:
409 	errno = EINVAL;
410 
411 end_error:
412 	sverrno = errno;
413 	if (asciivalue != NULL)
414 		free(asciivalue);
415 	if (currency_symbol != NULL)
416 		free(currency_symbol);
417 	errno = sverrno;
418 	return (-1);
419 }
420 
421 static void
422 __setup_vars(int flags, char *cs_precedes, char *sep_by_space,
423     char *sign_posn, char **signstr, struct lconv *lc)
424 {
425 	if ((flags & IS_NEGATIVE) && (flags & USE_INTL_CURRENCY)) {
426 		*cs_precedes = lc->int_n_cs_precedes;
427 		*sep_by_space = lc->int_n_sep_by_space;
428 		*sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_n_sign_posn;
429 		*signstr = (lc->negative_sign[0] == '\0') ? "-"
430 		    : lc->negative_sign;
431 	} else if (flags & USE_INTL_CURRENCY) {
432 		*cs_precedes = lc->int_p_cs_precedes;
433 		*sep_by_space = lc->int_p_sep_by_space;
434 		*sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_p_sign_posn;
435 		*signstr = lc->positive_sign;
436 	} else if (flags & IS_NEGATIVE) {
437 		*cs_precedes = lc->n_cs_precedes;
438 		*sep_by_space = lc->n_sep_by_space;
439 		*sign_posn = (flags & PARENTH_POSN) ? 0 : lc->n_sign_posn;
440 		*signstr = (lc->negative_sign[0] == '\0') ? "-"
441 		    : lc->negative_sign;
442 	} else {
443 		*cs_precedes = lc->p_cs_precedes;
444 		*sep_by_space = lc->p_sep_by_space;
445 		*sign_posn = (flags & PARENTH_POSN) ? 0 : lc->p_sign_posn;
446 		*signstr = lc->positive_sign;
447 	}
448 
449 	/* Set default values for unspecified information. */
450 	if (*cs_precedes != 0)
451 		*cs_precedes = 1;
452 	if (*sep_by_space == CHAR_MAX)
453 		*sep_by_space = 0;
454 	if (*sign_posn == CHAR_MAX)
455 		*sign_posn = 0;
456 }
457 
458 static int
459 __calc_left_pad(int flags, char *cur_symb, struct lconv *lc)
460 {
461 	char cs_precedes, sep_by_space, sign_posn, *signstr;
462 	int left_chars = 0;
463 
464 	__setup_vars(flags, &cs_precedes, &sep_by_space, &sign_posn,
465 	    &signstr, lc);
466 
467 	if (cs_precedes != 0) {
468 		left_chars += strlen(cur_symb);
469 		if (sep_by_space != 0)
470 			left_chars++;
471 	}
472 
473 	switch (sign_posn) {
474 		case 0:
475 			if (flags & IS_NEGATIVE)
476 				left_chars++;
477 			break;
478 		case 1:
479 			left_chars += strlen(signstr);
480 			break;
481 		case 3:
482 		case 4:
483 			if (cs_precedes != 0)
484 				left_chars += strlen(signstr);
485 	}
486 	return (left_chars);
487 }
488 
489 static int
490 get_groups(int size, char *grouping)
491 {
492 	int	chars = 0;
493 
494 	if (*grouping == CHAR_MAX || *grouping <= 0)	/* no grouping ? */
495 		return (0);
496 
497 	while (size > (int)*grouping) {
498 		chars++;
499 		size -= (int)*grouping++;
500 		/* no more grouping ? */
501 		if (*grouping == CHAR_MAX)
502 			break;
503 		/* rest grouping with same value ? */
504 		if (*grouping == 0) {
505 			chars += (size - 1) / *(grouping - 1);
506 			break;
507 		}
508 	}
509 	return (chars);
510 }
511 
512 /* convert double to locale-encoded string */
513 static char *
514 __format_grouped_double(double value, int *flags,
515     int left_prec, int right_prec, int pad_char, struct lconv *lc, locale_t loc)
516 {
517 
518 	char		*rslt;
519 	char		*avalue;
520 	int		avalue_size;
521 
522 	size_t		bufsize;
523 	char		*bufend;
524 
525 	int		padded;
526 
527 	char		*grouping;
528 	const char	*decimal_point;
529 	const char	*thousands_sep;
530 	size_t		decimal_point_size;
531 	size_t		thousands_sep_size;
532 
533 	int groups = 0;
534 
535 	grouping = lc->mon_grouping;
536 	decimal_point = lc->mon_decimal_point;
537 	if (*decimal_point == '\0')
538 		decimal_point = lc->decimal_point;
539 	thousands_sep = lc->mon_thousands_sep;
540 	if (*thousands_sep == '\0')
541 		thousands_sep = lc->thousands_sep;
542 
543 	decimal_point_size = strlen(decimal_point);
544 	thousands_sep_size = strlen(thousands_sep);
545 
546 	/* fill left_prec with default value */
547 	if (left_prec == -1)
548 		left_prec = 0;
549 
550 	/* fill right_prec with default value */
551 	if (right_prec == -1) {
552 		if (*flags & USE_INTL_CURRENCY)
553 			right_prec = lc->int_frac_digits;
554 		else
555 			right_prec = lc->frac_digits;
556 
557 		if (right_prec == CHAR_MAX)	/* POSIX locale ? */
558 			right_prec = 2;
559 	}
560 
561 	if (*flags & NEED_GROUPING)
562 		left_prec += get_groups(left_prec, grouping);
563 
564 	/* convert to string */
565 	avalue_size = asprintf_l(&avalue, loc, "%*.*f",
566 	    left_prec + right_prec + 1, right_prec, value);
567 	if (avalue_size < 0)
568 		return (NULL);
569 
570 	/* make sure that we've enough space for result string */
571 	bufsize = avalue_size * (1 + thousands_sep_size) + decimal_point_size +
572 	    1;
573 	rslt = calloc(1, bufsize);
574 	if (rslt == NULL) {
575 		free(avalue);
576 		return (NULL);
577 	}
578 	bufend = rslt + bufsize - 1;	/* reserve space for trailing '\0' */
579 
580 	/* skip spaces at beginning */
581 	padded = 0;
582 	while (avalue[padded] == ' ') {
583 		padded++;
584 		avalue_size--;
585 	}
586 
587 	if (right_prec > 0) {
588 		bufend -= right_prec;
589 		memcpy(bufend, avalue + avalue_size + padded - right_prec,
590 		    right_prec);
591 		bufend -= decimal_point_size;
592 		memcpy(bufend, decimal_point, decimal_point_size);
593 		avalue_size -= (right_prec + 1);
594 	}
595 
596 	if ((*flags & NEED_GROUPING) &&
597 	    thousands_sep_size > 0 &&
598 	    *grouping != CHAR_MAX &&
599 	    *grouping > 0) {
600 		while (avalue_size > (int)*grouping) {
601 			GRPCPY(*grouping);
602 			GRPSEP;
603 			grouping++;
604 
605 			/* no more grouping ? */
606 			if (*grouping == CHAR_MAX)
607 				break;
608 
609 			/* rest grouping with same value ? */
610 			if (*grouping == 0) {
611 				grouping--;
612 				while (avalue_size > *grouping) {
613 					GRPCPY(*grouping);
614 					GRPSEP;
615 				}
616 			}
617 		}
618 		if (avalue_size != 0)
619 			GRPCPY(avalue_size);
620 		padded -= groups;
621 	} else {
622 		bufend -= avalue_size;
623 		memcpy(bufend, avalue + padded, avalue_size);
624 		/* decrease assumed $decimal_point */
625 		if (right_prec == 0)
626 			padded -= decimal_point_size;
627 	}
628 
629 	/* do padding with pad_char */
630 	if (padded > 0) {
631 		bufend -= padded;
632 		memset(bufend, pad_char, padded);
633 	}
634 
635 	bufsize = rslt + bufsize - bufend;
636 	memmove(rslt, bufend, bufsize);
637 	free(avalue);
638 	return (rslt);
639 }
640 
641 ssize_t
642 strfmon(char * __restrict s, size_t maxsize, const char * __restrict format,
643     ...)
644 {
645 	ssize_t ret;
646 	va_list ap;
647 
648 	va_start(ap, format);
649 	ret = vstrfmon_l(s, maxsize, __get_locale(), format, ap);
650 	va_end(ap);
651 
652 	return (ret);
653 }
654 
655 ssize_t
656 strfmon_l(char * __restrict s, size_t maxsize, locale_t loc,
657     const char * __restrict format, ...)
658 {
659 	ssize_t ret;
660 	va_list ap;
661 
662 	va_start(ap, format);
663 	ret = vstrfmon_l(s, maxsize, loc, format, ap);
664 	va_end(ap);
665 
666 	return (ret);
667 }
668