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