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