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