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