xref: /netbsd-src/lib/libc/stdlib/strfmon.c (revision 567219e1d7461bff1b180e494a9674a287b057a7)
1 /*	$NetBSD: strfmon.c,v 1.9 2012/03/13 21:13:48 christos Exp $	*/
2 
3 /*-
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 #include <sys/cdefs.h>
31 #if defined(LIBC_SCCS) && !defined(lint)
32 #if 0
33 __FBSDID("$FreeBSD: src/lib/libc/stdlib/strfmon.c,v 1.14 2003/03/20 08:18:55 ache Exp $");
34 #else
35 __RCSID("$NetBSD: strfmon.c,v 1.9 2012/03/13 21:13:48 christos Exp $");
36 #endif
37 #endif /* LIBC_SCCS and not lint */
38 
39 #if defined(__NetBSD__)
40 #include "namespace.h"
41 #include <monetary.h>
42 #endif
43 
44 #include <sys/types.h>
45 #include <assert.h>
46 #include <ctype.h>
47 #include <errno.h>
48 #include <limits.h>
49 #include <locale.h>
50 #include <stdarg.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <stddef.h>
55 
56 /* internal flags */
57 #define	NEED_GROUPING		0x01	/* print digits grouped (default) */
58 #define	SIGN_POSN_USED		0x02	/* '+' or '(' usage flag */
59 #define	LOCALE_POSN		0x04	/* use locale defined +/- (default) */
60 #define	PARENTH_POSN		0x08	/* enclose negative amount in () */
61 #define	SUPRESS_CURR_SYMBOL	0x10	/* supress the currency from output */
62 #define	LEFT_JUSTIFY		0x20	/* left justify */
63 #define	USE_INTL_CURRENCY	0x40	/* use international currency symbol */
64 #define IS_NEGATIVE		0x80	/* is argument value negative ? */
65 
66 /* internal macros */
67 #define PRINT(CH) do {						\
68 	if (dst >= s + maxsize) 				\
69 		goto e2big_error;				\
70 	*dst++ = CH;						\
71 } while (/* CONSTCOND */ 0)
72 
73 #define PRINTS(STR) do {					\
74 	const char *tmps = STR;					\
75 	while (*tmps != '\0')					\
76 		PRINT(*tmps++);					\
77 } while (/* CONSTCOND */ 0)
78 
79 #define GET_NUMBER(VAR)	do {					\
80 	VAR = 0;						\
81 	while (isdigit((unsigned char)*fmt)) {			\
82 		VAR *= 10;					\
83 		VAR += *fmt - '0';				\
84 		if (VAR > 0x00ffffff)				\
85 			goto e2big_error;			\
86 		fmt++;						\
87 	}							\
88 } while (/* CONSTCOND */ 0)
89 
90 #define GRPCPY(howmany) do {					\
91 	int i = howmany;					\
92 	while (i-- > 0) {					\
93 		avalue_size--;					\
94 		*--bufend = *(avalue+avalue_size+padded);	\
95 	}							\
96 } while (/* CONSTCOND */ 0)
97 
98 #define GRPSEP do {						\
99 	*--bufend = thousands_sep;				\
100 	groups++;						\
101 } while (/* CONSTCOND */ 0)
102 
103 static void __setup_vars(int, char *, char *, char *, const char **);
104 static int __calc_left_pad(int, char *);
105 static char *__format_grouped_double(double, int *, int, int, int);
106 
107 ssize_t
108 strfmon(char * __restrict s, size_t maxsize, const char * __restrict format,
109     ...)
110 {
111 	va_list		ap;
112 	char 		*dst;		/* output destination pointer */
113 	const char 	*fmt;		/* current format poistion pointer */
114 	struct lconv 	*lc;		/* pointer to lconv structure */
115 	char		*asciivalue;	/* formatted double pointer */
116 
117 	int		flags;		/* formatting options */
118 	int		pad_char;	/* padding character */
119 	int		pad_size;	/* pad size */
120 	int		width;		/* field width */
121 	int		left_prec;	/* left precision */
122 	int		right_prec;	/* right precision */
123 	double		value;		/* just value */
124 	char		space_char = ' '; /* space after currency */
125 
126 	char		cs_precedes,	/* values gathered from struct lconv */
127 			sep_by_space,
128 			sign_posn,
129 			*currency_symbol;
130 	const char	*signstr;
131 
132 	char		*tmpptr;	/* temporary vars */
133 	int		sverrno;
134 
135         va_start(ap, format);
136 
137 	lc = localeconv();
138 	dst = s;
139 	fmt = format;
140 	asciivalue = NULL;
141 	currency_symbol = NULL;
142 	pad_size = 0;
143 
144 	while (*fmt) {
145 		/* pass nonformating characters AS IS */
146 		if (*fmt != '%')
147 			goto literal;
148 
149 		/* '%' found ! */
150 
151 		/* "%%" mean just '%' */
152 		if (*(fmt+1) == '%') {
153 			fmt++;
154 	literal:
155 			PRINT(*fmt++);
156 			continue;
157 		}
158 
159 		/* set up initial values */
160 		flags = (NEED_GROUPING|LOCALE_POSN);
161 		pad_char = ' ';		/* padding character is "space" */
162 		left_prec = -1;		/* no left precision specified */
163 		right_prec = -1;	/* no right precision specified */
164 		width = -1;		/* no width specified */
165 		value = 0;		/* we have no value to print now */
166 
167 		/* Flags */
168 		while (/* CONSTCOND */ 1) {
169 			switch (*++fmt) {
170 				case '=':	/* fill character */
171 					pad_char = *++fmt;
172 					if (pad_char == '\0')
173 						goto format_error;
174 					continue;
175 				case '^':	/* not group currency  */
176 					flags &= ~(NEED_GROUPING);
177 					continue;
178 				case '+':	/* use locale defined signs */
179 					if (flags & SIGN_POSN_USED)
180 						goto format_error;
181 					flags |= (SIGN_POSN_USED|LOCALE_POSN);
182 					continue;
183 				case '(':	/* enclose negatives with () */
184 					if (flags & SIGN_POSN_USED)
185 						goto format_error;
186 					flags |= (SIGN_POSN_USED|PARENTH_POSN);
187 					continue;
188 				case '!':	/* suppress currency symbol */
189 					flags |= SUPRESS_CURR_SYMBOL;
190 					continue;
191 				case '-':	/* alignment (left)  */
192 					flags |= LEFT_JUSTIFY;
193 					continue;
194 				default:
195 					break;
196 			}
197 			break;
198 		}
199 
200 		/* field Width */
201 		if (isdigit((unsigned char)*fmt)) {
202 			ptrdiff_t d = dst - s;
203 			GET_NUMBER(width);
204 			/* Do we have enough space to put number with
205 			 * required width ?
206 			 */
207 
208 			if ((size_t)(d + width) >= maxsize)
209 				goto e2big_error;
210 		}
211 
212 		/* Left precision */
213 		if (*fmt == '#') {
214 			if (!isdigit((unsigned char)*++fmt))
215 				goto format_error;
216 			GET_NUMBER(left_prec);
217 		}
218 
219 		/* Right precision */
220 		if (*fmt == '.') {
221 			if (!isdigit((unsigned char)*++fmt))
222 				goto format_error;
223 			GET_NUMBER(right_prec);
224 		}
225 
226 		/* Conversion Characters */
227 		switch (*fmt++) {
228 			case 'i':	/* use internaltion currency format */
229 				flags |= USE_INTL_CURRENCY;
230 				break;
231 			case 'n':	/* use national currency format */
232 				flags &= ~(USE_INTL_CURRENCY);
233 				break;
234 			default:	/* required character is missing or
235 					   premature EOS */
236 				goto format_error;
237 		}
238 
239 		if (currency_symbol)
240 			free(currency_symbol);
241 		if (flags & USE_INTL_CURRENCY) {
242 			currency_symbol = strdup(lc->int_curr_symbol);
243 			if (currency_symbol != NULL)
244 				space_char = *(currency_symbol+3);
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) -
264 				   __calc_left_pad(flags, currency_symbol);
265 			if (pad_size < 0)
266 				pad_size = 0;
267 		}
268 
269 		asciivalue = __format_grouped_double(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(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)		/* XXX: ? */
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
352 				    && (sign_posn == 0
353 				    || sign_posn == 1
354 				    || sign_posn == 2
355 				    || sign_posn == 4)))
356 					PRINT(space_char);
357 				PRINTS(currency_symbol); /* XXX: len */
358 				if (sign_posn == 4) {
359 					if (sep_by_space == 2)
360 						PRINT(' ');
361 					PRINTS(signstr);
362 				}
363 			}
364 		}
365 
366 		if (sign_posn == 2) {
367 			if (sep_by_space == 2)
368 				PRINT(' ');
369 			PRINTS(signstr);
370 		}
371 
372 		if (sign_posn == 0 && (flags & IS_NEGATIVE))
373 			PRINT(')');
374 
375 		if (dst - tmpptr < width) {
376 			if (flags & LEFT_JUSTIFY) {
377 				while (dst - tmpptr < width)
378 					PRINT(' ');
379 			} else {
380 				_DIAGASSERT(__type_fit(int, dst - tmpptr));
381 				pad_size = dst - tmpptr;
382 				memmove(tmpptr + width-pad_size, tmpptr,
383 				    (size_t) pad_size);
384 				memset(tmpptr, ' ', (size_t) width-pad_size);
385 				dst += width-pad_size;
386 			}
387 		}
388 	}
389 
390 	PRINT('\0');
391 	va_end(ap);
392 	free(asciivalue);
393 	free(currency_symbol);
394 	return (dst - s - 1);	/* return size of put data except trailing '\0' */
395 
396 e2big_error:
397 	errno = E2BIG;
398 	goto end_error;
399 
400 format_error:
401 	errno = EINVAL;
402 
403 end_error:
404 	sverrno = errno;
405 	if (asciivalue != NULL)
406 		free(asciivalue);
407 	if (currency_symbol != NULL)
408 		free(currency_symbol);
409 	errno = sverrno;
410 	va_end(ap);
411 	return (-1);
412 }
413 
414 static void
415 __setup_vars(int flags, char *cs_precedes, char *sep_by_space,
416 		char *sign_posn, const char **signstr) {
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') ? "-"
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') ? "-"
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 	char cs_precedes, sep_by_space, sign_posn;
456 	const char *signstr;
457 	size_t 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 	_DIAGASSERT(__type_fit(int, left_chars));
477 	return (int)left_chars;
478 }
479 
480 static int
481 get_groups(int size, char *grouping) {
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 	char		*rslt;
509 	char		*avalue;
510 	int		avalue_size;
511 
512 	size_t		bufsize;
513 	char		*bufend;
514 
515 	int		padded;
516 
517 	struct lconv	*lc = localeconv();
518 	char		*grouping;
519 	char		decimal_point;
520 	char		thousands_sep;
521 
522 	int groups = 0;
523 
524 	grouping = lc->mon_grouping;
525 	decimal_point = *lc->mon_decimal_point;
526 	if (decimal_point == '\0')
527 		decimal_point = *lc->decimal_point;
528 	thousands_sep = *lc->mon_thousands_sep;
529 	if (thousands_sep == '\0')
530 		thousands_sep = *lc->thousands_sep;
531 
532 	/* fill left_prec with default value */
533 	if (left_prec == -1)
534 		left_prec = 0;
535 
536 	/* fill right_prec with default value */
537 	if (right_prec == -1) {
538                 if (*flags & USE_INTL_CURRENCY)
539                         right_prec = lc->int_frac_digits;
540                 else
541                         right_prec = lc->frac_digits;
542 
543 		if (right_prec == CHAR_MAX)	/* POSIX locale ? */
544 			right_prec = 2;
545 	}
546 
547 	if (*flags & NEED_GROUPING)
548 		left_prec += get_groups(left_prec, grouping);
549 
550 	/* convert to string */
551 	avalue_size = asprintf(&avalue, "%*.*f", left_prec + right_prec + 1,
552 	    right_prec, value);
553 	if (avalue_size < 0)
554 		return (NULL);
555 
556 	/* make sure that we've enough space for result string */
557 	bufsize = avalue_size * 2 + 1;
558 	rslt = malloc(bufsize);
559 	if (rslt == NULL) {
560 		free(avalue);
561 		return (NULL);
562 	}
563 	memset(rslt, 0, bufsize);
564 	bufend = rslt + bufsize - 1;	/* reserve space for trailing '\0' */
565 
566 	/* skip spaces at beggining */
567 	padded = 0;
568 	while (avalue[padded] == ' ') {
569 		padded++;
570 		avalue_size--;
571 	}
572 
573 	if (right_prec > 0) {
574 		bufend -= right_prec;
575 		memcpy(bufend, avalue + avalue_size+padded-right_prec,
576 		    (size_t) right_prec);
577 		*--bufend = decimal_point;
578 		avalue_size -= (right_prec + 1);
579 	}
580 
581         /* XXX: Why not use %' instead? */
582 	if ((*flags & NEED_GROUPING) &&
583 	    thousands_sep != '\0' &&	/* XXX: need investigation */
584 	    *grouping != CHAR_MAX &&
585 	    *grouping > 0) {
586 		while (avalue_size > (int)*grouping) {
587 			GRPCPY(*grouping);
588 			GRPSEP;
589 			grouping++;
590 
591 			/* no more grouping ? */
592 			if (*grouping == CHAR_MAX)
593 				break;
594 
595 			/* rest grouping with same value ? */
596 			if (*grouping == 0) {
597 				grouping--;
598 				while (avalue_size > *grouping) {
599 					GRPCPY(*grouping);
600 					GRPSEP;
601 				}
602 			}
603 		}
604 		if (avalue_size != 0)
605 			GRPCPY(avalue_size);
606 		padded -= groups;
607 
608 	} else {
609 		bufend -= avalue_size;
610 		memcpy(bufend, avalue+padded, (size_t) avalue_size);
611 		if (right_prec == 0)
612 			padded--;	/* decrease assumed $decimal_point */
613 	}
614 
615 	/* do padding with pad_char */
616 	if (padded > 0) {
617 		bufend -= padded;
618 		memset(bufend, pad_char, (size_t) padded);
619 	}
620 
621 	bufsize = bufsize - (bufend - rslt) + 1;
622 	memmove(rslt, bufend, bufsize);
623 	free(avalue);
624 	return (rslt);
625 }
626