xref: /netbsd-src/lib/libc/stdlib/strfmon.c (revision 6a9b3088d8d2341ca1454531d365c15fe9c1c589)
1 /*	$NetBSD: strfmon.c,v 1.7 2009/01/30 23:46:03 lukem 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.7 2009/01/30 23:46:03 lukem 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 <ctype.h>
46 #include <errno.h>
47 #include <limits.h>
48 #include <locale.h>
49 #include <stdarg.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <stddef.h>
54 
55 /* internal flags */
56 #define	NEED_GROUPING		0x01	/* print digits grouped (default) */
57 #define	SIGN_POSN_USED		0x02	/* '+' or '(' usage flag */
58 #define	LOCALE_POSN		0x04	/* use locale defined +/- (default) */
59 #define	PARENTH_POSN		0x08	/* enclose negative amount in () */
60 #define	SUPRESS_CURR_SYMBOL	0x10	/* supress the currency from output */
61 #define	LEFT_JUSTIFY		0x20	/* left justify */
62 #define	USE_INTL_CURRENCY	0x40	/* use international currency symbol */
63 #define IS_NEGATIVE		0x80	/* is argument value negative ? */
64 
65 /* internal macros */
66 #define PRINT(CH) do {						\
67 	if (dst >= s + maxsize) 				\
68 		goto e2big_error;				\
69 	*dst++ = CH;						\
70 } while (/* CONSTCOND */ 0)
71 
72 #define PRINTS(STR) do {					\
73 	const char *tmps = STR;					\
74 	while (*tmps != '\0')					\
75 		PRINT(*tmps++);					\
76 } while (/* CONSTCOND */ 0)
77 
78 #define GET_NUMBER(VAR)	do {					\
79 	VAR = 0;						\
80 	while (isdigit((unsigned char)*fmt)) {			\
81 		VAR *= 10;					\
82 		VAR += *fmt - '0';				\
83 		if (VAR > 0x00ffffff)				\
84 			goto e2big_error;			\
85 		fmt++;						\
86 	}							\
87 } while (/* CONSTCOND */ 0)
88 
89 #define GRPCPY(howmany) do {					\
90 	int i = howmany;					\
91 	while (i-- > 0) {					\
92 		avalue_size--;					\
93 		*--bufend = *(avalue+avalue_size+padded);	\
94 	}							\
95 } while (/* CONSTCOND */ 0)
96 
97 #define GRPSEP do {						\
98 	*--bufend = thousands_sep;				\
99 	groups++;						\
100 } while (/* CONSTCOND */ 0)
101 
102 static void __setup_vars(int, char *, char *, char *, const char **);
103 static int __calc_left_pad(int, char *);
104 static char *__format_grouped_double(double, int *, int, int, int);
105 
106 ssize_t
107 strfmon(char * __restrict s, size_t maxsize, const char * __restrict format,
108     ...)
109 {
110 	va_list		ap;
111 	char 		*dst;		/* output destination pointer */
112 	const char 	*fmt;		/* current format poistion pointer */
113 	struct lconv 	*lc;		/* pointer to lconv structure */
114 	char		*asciivalue;	/* formatted double pointer */
115 
116 	int		flags;		/* formatting options */
117 	int		pad_char;	/* padding character */
118 	int		pad_size;	/* pad size */
119 	int		width;		/* field width */
120 	int		left_prec;	/* left precision */
121 	int		right_prec;	/* right precision */
122 	double		value;		/* just value */
123 	char		space_char = ' '; /* space after currency */
124 
125 	char		cs_precedes,	/* values gathered from struct lconv */
126 			sep_by_space,
127 			sign_posn,
128 			*currency_symbol;
129 	const char	*signstr;
130 
131 	char		*tmpptr;	/* temporary vars */
132 	int		sverrno;
133 
134         va_start(ap, format);
135 
136 	lc = localeconv();
137 	dst = s;
138 	fmt = format;
139 	asciivalue = NULL;
140 	currency_symbol = NULL;
141 	pad_size = 0;
142 
143 	while (*fmt) {
144 		/* pass nonformating characters AS IS */
145 		if (*fmt != '%')
146 			goto literal;
147 
148 		/* '%' found ! */
149 
150 		/* "%%" mean just '%' */
151 		if (*(fmt+1) == '%') {
152 			fmt++;
153 	literal:
154 			PRINT(*fmt++);
155 			continue;
156 		}
157 
158 		/* set up initial values */
159 		flags = (NEED_GROUPING|LOCALE_POSN);
160 		pad_char = ' ';		/* padding character is "space" */
161 		left_prec = -1;		/* no left precision specified */
162 		right_prec = -1;	/* no right precision specified */
163 		width = -1;		/* no width specified */
164 		value = 0;		/* we have no value to print now */
165 
166 		/* Flags */
167 		while (/* CONSTCOND */ 1) {
168 			switch (*++fmt) {
169 				case '=':	/* fill character */
170 					pad_char = *++fmt;
171 					if (pad_char == '\0')
172 						goto format_error;
173 					continue;
174 				case '^':	/* not group currency  */
175 					flags &= ~(NEED_GROUPING);
176 					continue;
177 				case '+':	/* use locale defined signs */
178 					if (flags & SIGN_POSN_USED)
179 						goto format_error;
180 					flags |= (SIGN_POSN_USED|LOCALE_POSN);
181 					continue;
182 				case '(':	/* enclose negatives with () */
183 					if (flags & SIGN_POSN_USED)
184 						goto format_error;
185 					flags |= (SIGN_POSN_USED|PARENTH_POSN);
186 					continue;
187 				case '!':	/* suppress currency symbol */
188 					flags |= SUPRESS_CURR_SYMBOL;
189 					continue;
190 				case '-':	/* alignment (left)  */
191 					flags |= LEFT_JUSTIFY;
192 					continue;
193 				default:
194 					break;
195 			}
196 			break;
197 		}
198 
199 		/* field Width */
200 		if (isdigit((unsigned char)*fmt)) {
201 			ptrdiff_t d = dst - s;
202 			GET_NUMBER(width);
203 			/* Do we have enough space to put number with
204 			 * required width ?
205 			 */
206 
207 			if ((size_t)(d + width) >= maxsize)
208 				goto e2big_error;
209 		}
210 
211 		/* Left precision */
212 		if (*fmt == '#') {
213 			if (!isdigit((unsigned char)*++fmt))
214 				goto format_error;
215 			GET_NUMBER(left_prec);
216 		}
217 
218 		/* Right precision */
219 		if (*fmt == '.') {
220 			if (!isdigit((unsigned char)*++fmt))
221 				goto format_error;
222 			GET_NUMBER(right_prec);
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:	/* required character is missing or
234 					   premature EOS */
235 				goto format_error;
236 		}
237 
238 		if (currency_symbol)
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 		asciivalue = __format_grouped_double(value, &flags,
269 				left_prec, right_prec, pad_char);
270 		if (asciivalue == NULL)
271 			goto end_error;		/* errno already set     */
272 						/* to ENOMEM by malloc() */
273 
274 		/* set some variables for later use */
275 		__setup_vars(flags, &cs_precedes, &sep_by_space,
276 				&sign_posn, &signstr);
277 
278 		/*
279 		 * Description of some LC_MONETARY's values:
280 		 *
281 		 * p_cs_precedes & n_cs_precedes
282 		 *
283 		 * = 1 - $currency_symbol precedes the value
284 		 *       for a monetary quantity with a non-negative value
285 		 * = 0 - symbol succeeds the value
286 		 *
287 		 * p_sep_by_space & n_sep_by_space
288                  *
289 		 * = 0 - no space separates $currency_symbol
290 		 *       from the value for a monetary quantity with a
291 		 *	 non-negative value
292 		 * = 1 - space separates the symbol from the value
293 		 * = 2 - space separates the symbol and the sign string,
294 		 *       if adjacent.
295                  *
296 		 * p_sign_posn & n_sign_posn
297                  *
298 		 * = 0 - parentheses enclose the quantity and the
299 		 *	 $currency_symbol
300 		 * = 1 - the sign string precedes the quantity and the
301 		 *       $currency_symbol
302 		 * = 2 - the sign string succeeds the quantity and the
303 		 *       $currency_symbol
304 		 * = 3 - the sign string precedes the $currency_symbol
305 		 * = 4 - the sign string succeeds the $currency_symbol
306                  *
307 		 */
308 
309 		tmpptr = dst;
310 
311 		while (pad_size-- > 0)
312 			PRINT(' ');
313 
314 		if (sign_posn == 0 && (flags & IS_NEGATIVE))
315 			PRINT('(');
316 
317 		if (cs_precedes == 1) {
318 			if (sign_posn == 1 || sign_posn == 3) {
319 				PRINTS(signstr);
320 				if (sep_by_space == 2)		/* XXX: ? */
321 					PRINT(' ');
322 			}
323 
324 			if (!(flags & SUPRESS_CURR_SYMBOL)) {
325 				PRINTS(currency_symbol);
326 
327 				if (sign_posn == 4) {
328 					if (sep_by_space == 2)
329 						PRINT(space_char);
330 					PRINTS(signstr);
331 					if (sep_by_space == 1)
332 						PRINT(' ');
333 				} else if (sep_by_space == 1)
334 					PRINT(space_char);
335 			}
336 		} else if (sign_posn == 1)
337 			PRINTS(signstr);
338 
339 		PRINTS(asciivalue);
340 
341 		if (cs_precedes == 0) {
342 			if (sign_posn == 3) {
343 				if (sep_by_space == 1)
344 					PRINT(' ');
345 				PRINTS(signstr);
346 			}
347 
348 			if (!(flags & SUPRESS_CURR_SYMBOL)) {
349 				if ((sign_posn == 3 && sep_by_space == 2)
350 				    || (sep_by_space == 1
351 				    && (sign_posn == 0
352 				    || sign_posn == 1
353 				    || 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 				memmove(tmpptr + width-pad_size, tmpptr,
381 				    (size_t) pad_size);
382 				memset(tmpptr, ' ', (size_t) 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);	/* return 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, const char **signstr) {
415 	struct lconv *lc = localeconv();
416 
417 	if ((flags & IS_NEGATIVE) && (flags & USE_INTL_CURRENCY)) {
418 		*cs_precedes = lc->int_n_cs_precedes;
419 		*sep_by_space = lc->int_n_sep_by_space;
420 		*sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_n_sign_posn;
421 		*signstr = (lc->negative_sign == '\0') ? "-"
422 		    : lc->negative_sign;
423 	} else if (flags & USE_INTL_CURRENCY) {
424 		*cs_precedes = lc->int_p_cs_precedes;
425 		*sep_by_space = lc->int_p_sep_by_space;
426 		*sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_p_sign_posn;
427 		*signstr = lc->positive_sign;
428 	} else if (flags & IS_NEGATIVE) {
429 		*cs_precedes = lc->n_cs_precedes;
430 		*sep_by_space = lc->n_sep_by_space;
431 		*sign_posn = (flags & PARENTH_POSN) ? 0 : lc->n_sign_posn;
432 		*signstr = (lc->negative_sign == '\0') ? "-"
433 		    : lc->negative_sign;
434 	} else {
435 		*cs_precedes = lc->p_cs_precedes;
436 		*sep_by_space = lc->p_sep_by_space;
437 		*sign_posn = (flags & PARENTH_POSN) ? 0 : lc->p_sign_posn;
438 		*signstr = lc->positive_sign;
439 	}
440 
441 	/* Set defult values for unspecified information. */
442 	if (*cs_precedes != 0)
443 		*cs_precedes = 1;
444 	if (*sep_by_space == CHAR_MAX)
445 		*sep_by_space = 0;
446 	if (*sign_posn == CHAR_MAX)
447 		*sign_posn = 0;
448 }
449 
450 static int
451 __calc_left_pad(int flags, char *cur_symb) {
452 
453 	char cs_precedes, sep_by_space, sign_posn;
454 	const char *signstr;
455 	int left_chars = 0;
456 
457 	__setup_vars(flags, &cs_precedes, &sep_by_space, &sign_posn, &signstr);
458 
459 	if (cs_precedes != 0) {
460 		left_chars += strlen(cur_symb);
461 		if (sep_by_space != 0)
462 			left_chars++;
463 	}
464 
465 	switch (sign_posn) {
466 		case 1:
467 			left_chars += strlen(signstr);
468 			break;
469 		case 3:
470 		case 4:
471 			if (cs_precedes != 0)
472 				left_chars += strlen(signstr);
473 	}
474 	return (left_chars);
475 }
476 
477 static int
478 get_groups(int size, char *grouping) {
479 
480 	int	chars = 0;
481 
482 	if (*grouping == CHAR_MAX || *grouping <= 0)	/* no grouping ? */
483 		return (0);
484 
485 	while (size > (int)*grouping) {
486 		chars++;
487 		size -= (int)*grouping++;
488 		/* no more grouping ? */
489 		if (*grouping == CHAR_MAX)
490 			break;
491 		/* rest grouping with same value ? */
492 		if (*grouping == 0) {
493 			chars += (size - 1) / *(grouping - 1);
494 			break;
495 		}
496 	}
497 	return (chars);
498 }
499 
500 /* convert double to ASCII */
501 static char *
502 __format_grouped_double(double value, int *flags,
503 			int left_prec, int right_prec, int pad_char) {
504 
505 	char		*rslt;
506 	char		*avalue;
507 	int		avalue_size;
508 	char		fmt[32];
509 
510 	size_t		bufsize;
511 	char		*bufend;
512 
513 	int		padded;
514 
515 	struct lconv	*lc = localeconv();
516 	char		*grouping;
517 	char		decimal_point;
518 	char		thousands_sep;
519 
520 	int groups = 0;
521 
522 	grouping = lc->mon_grouping;
523 	decimal_point = *lc->mon_decimal_point;
524 	if (decimal_point == '\0')
525 		decimal_point = *lc->decimal_point;
526 	thousands_sep = *lc->mon_thousands_sep;
527 	if (thousands_sep == '\0')
528 		thousands_sep = *lc->thousands_sep;
529 
530 	/* fill left_prec with default value */
531 	if (left_prec == -1)
532 		left_prec = 0;
533 
534 	/* fill right_prec with default value */
535 	if (right_prec == -1) {
536                 if (*flags & USE_INTL_CURRENCY)
537                         right_prec = lc->int_frac_digits;
538                 else
539                         right_prec = lc->frac_digits;
540 
541 		if (right_prec == CHAR_MAX)	/* POSIX locale ? */
542 			right_prec = 2;
543 	}
544 
545 	if (*flags & NEED_GROUPING)
546 		left_prec += get_groups(left_prec, grouping);
547 
548 	/* convert to string */
549 	snprintf(fmt, sizeof(fmt), "%%%d.%df", left_prec + right_prec + 1,
550 	    right_prec);
551 	avalue_size = asprintf(&avalue, fmt, value);
552 	if (avalue_size < 0)
553 		return (NULL);
554 
555 	/* make sure that we've enough space for result string */
556 	bufsize = strlen(avalue)*2+1;
557 	rslt = malloc(bufsize);
558 	if (rslt == NULL) {
559 		free(avalue);
560 		return (NULL);
561 	}
562 	memset(rslt, 0, bufsize);
563 	bufend = rslt + bufsize - 1;	/* reserve space for trailing '\0' */
564 
565 	/* skip spaces at beggining */
566 	padded = 0;
567 	while (avalue[padded] == ' ') {
568 		padded++;
569 		avalue_size--;
570 	}
571 
572 	if (right_prec > 0) {
573 		bufend -= right_prec;
574 		memcpy(bufend, avalue + avalue_size+padded-right_prec,
575 		    (size_t) right_prec);
576 		*--bufend = decimal_point;
577 		avalue_size -= (right_prec + 1);
578 	}
579 
580 	if ((*flags & NEED_GROUPING) &&
581 	    thousands_sep != '\0' &&	/* XXX: need investigation */
582 	    *grouping != CHAR_MAX &&
583 	    *grouping > 0) {
584 		while (avalue_size > (int)*grouping) {
585 			GRPCPY(*grouping);
586 			GRPSEP;
587 			grouping++;
588 
589 			/* no more grouping ? */
590 			if (*grouping == CHAR_MAX)
591 				break;
592 
593 			/* rest grouping with same value ? */
594 			if (*grouping == 0) {
595 				grouping--;
596 				while (avalue_size > *grouping) {
597 					GRPCPY(*grouping);
598 					GRPSEP;
599 				}
600 			}
601 		}
602 		if (avalue_size != 0)
603 			GRPCPY(avalue_size);
604 		padded -= groups;
605 
606 	} else {
607 		bufend -= avalue_size;
608 		memcpy(bufend, avalue+padded, (size_t) avalue_size);
609 		if (right_prec == 0)
610 			padded--;	/* decrease assumed $decimal_point */
611 	}
612 
613 	/* do padding with pad_char */
614 	if (padded > 0) {
615 		bufend -= padded;
616 		memset(bufend, pad_char, (size_t) padded);
617 	}
618 
619 	bufsize = bufsize - (bufend - rslt) + 1;
620 	memmove(rslt, bufend, bufsize);
621 	free(avalue);
622 	return (rslt);
623 }
624