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 "xlocale_private.h" 45 46#include <sys/types.h> 47#include <ctype.h> 48#include <errno.h> 49#include <limits.h> 50#include <locale.h> 51#include <stdarg.h> 52#include <stdio.h> 53#include <stdlib.h> 54#include <string.h> 55#include <stddef.h> 56 57/* internal flags */ 58#define NEED_GROUPING 0x01 /* print digits grouped (default) */ 59#define SIGN_POSN_USED 0x02 /* '+' or '(' usage flag */ 60#define LOCALE_POSN 0x04 /* use locale defined +/- (default) */ 61#define PARENTH_POSN 0x08 /* enclose negative amount in () */ 62#define SUPRESS_CURR_SYMBOL 0x10 /* supress the currency from output */ 63#define LEFT_JUSTIFY 0x20 /* left justify */ 64#define USE_INTL_CURRENCY 0x40 /* use international currency symbol */ 65#define IS_NEGATIVE 0x80 /* is argument value negative ? */ 66 67/* internal macros */ 68#define PRINT(CH) do { \ 69 if (dst >= s + maxsize) \ 70 goto e2big_error; \ 71 *dst++ = CH; \ 72} while (/* CONSTCOND */ 0) 73 74#define PRINTS(STR) do { \ 75 const char *tmps = STR; \ 76 while (*tmps != '\0') \ 77 PRINT(*tmps++); \ 78} while (/* CONSTCOND */ 0) 79 80#define GET_NUMBER(VAR,LOC) do { \ 81 VAR = 0; \ 82 while (isdigit_l((unsigned char)*fmt, (LOC))) { \ 83 VAR *= 10; \ 84 VAR += *fmt - '0'; \ 85 if (VAR > 0x00ffffff) \ 86 goto e2big_error; \ 87 fmt++; \ 88 } \ 89} while (/* CONSTCOND */ 0) 90 91#define GRPCPY(howmany) do { \ 92 int i = howmany; \ 93 while (i-- > 0) { \ 94 avalue_size--; \ 95 *--bufend = *(avalue+avalue_size+padded); \ 96 } \ 97} while (/* CONSTCOND */ 0) 98 99#define GRPSEP do { \ 100 *--bufend = thousands_sep; \ 101 groups++; \ 102} while (/* CONSTCOND */ 0) 103 104static void __setup_vars(int, char *, char *, char *, const char **, struct lconv *); 105static int __calc_left_pad(int, char *, struct lconv *); 106static char *__format_grouped_double(double, int *, int, int, int, struct lconv *, locale_t); 107 108static ssize_t 109_strfmon(char * __restrict s, size_t maxsize, locale_t loc, const char * __restrict format, va_list ap) 110{ 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 lc = localeconv_l(loc); 135 dst = s; 136 fmt = format; 137 asciivalue = NULL; 138 currency_symbol = NULL; 139 pad_size = 0; 140 141 while (*fmt) { 142 /* pass nonformating characters AS IS */ 143 if (*fmt != '%') 144 goto literal; 145 146 /* '%' found ! */ 147 148 /* "%%" mean just '%' */ 149 if (*(fmt+1) == '%') { 150 fmt++; 151 literal: 152 PRINT(*fmt++); 153 continue; 154 } 155 156 /* set up initial values */ 157 flags = (NEED_GROUPING|LOCALE_POSN); 158 pad_char = ' '; /* padding character is "space" */ 159 left_prec = -1; /* no left precision specified */ 160 right_prec = -1; /* no right precision specified */ 161 width = -1; /* no width specified */ 162 value = 0; /* we have no value to print now */ 163 164 /* Flags */ 165 while (/* CONSTCOND */ 1) { 166 switch (*++fmt) { 167 case '=': /* fill character */ 168 pad_char = *++fmt; 169 if (pad_char == '\0') 170 goto format_error; 171 continue; 172 case '^': /* not group currency */ 173 flags &= ~(NEED_GROUPING); 174 continue; 175 case '+': /* use locale defined signs */ 176 if (flags & SIGN_POSN_USED) 177 goto format_error; 178 flags |= (SIGN_POSN_USED|LOCALE_POSN); 179 continue; 180 case '(': /* enclose negatives with () */ 181 if (flags & SIGN_POSN_USED) 182 goto format_error; 183 flags |= (SIGN_POSN_USED|PARENTH_POSN); 184 continue; 185 case '!': /* suppress currency symbol */ 186 flags |= SUPRESS_CURR_SYMBOL; 187 continue; 188 case '-': /* alignment (left) */ 189 flags |= LEFT_JUSTIFY; 190 continue; 191 default: 192 break; 193 } 194 break; 195 } 196 197 /* field Width */ 198 if (isdigit_l((unsigned char)*fmt, loc)) { 199 ptrdiff_t d = dst - s; 200 GET_NUMBER(width, loc); 201 /* Do we have enough space to put number with 202 * required width ? 203 */ 204 205 if ((size_t)(d + width) >= maxsize) 206 goto e2big_error; 207 } 208 209 /* Left precision */ 210 if (*fmt == '#') { 211 if (!isdigit_l((unsigned char)*++fmt, loc)) 212 goto format_error; 213 GET_NUMBER(left_prec, loc); 214 } 215 216 /* Right precision */ 217 if (*fmt == '.') { 218 if (!isdigit_l((unsigned char)*++fmt, loc)) 219 goto format_error; 220 GET_NUMBER(right_prec, loc); 221 } 222 223 /* Conversion Characters */ 224 switch (*fmt++) { 225 case 'i': /* use internaltion currency format */ 226 flags |= USE_INTL_CURRENCY; 227 break; 228 case 'n': /* use national currency format */ 229 flags &= ~(USE_INTL_CURRENCY); 230 break; 231 default: /* required character is missing or 232 premature EOS */ 233 goto format_error; 234 } 235 236 if (currency_symbol) 237 free(currency_symbol); 238 if (flags & USE_INTL_CURRENCY) { 239 currency_symbol = strdup(lc->int_curr_symbol); 240 if (currency_symbol != NULL) { 241 space_char = *(currency_symbol+3); 242 currency_symbol[3] = '\0'; 243 } 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, lc) - 263 __calc_left_pad(flags, currency_symbol, lc); 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, lc, loc); 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, lc); 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 if (sep_by_space == 2) 339 PRINT(' '); 340 } 341 342 PRINTS(asciivalue); 343 344 if (cs_precedes == 0) { 345 if (sign_posn == 3) { 346 if (sep_by_space == 1) 347 PRINT(' '); 348 PRINTS(signstr); 349 } 350 351 if (!(flags & SUPRESS_CURR_SYMBOL)) { 352 if ((sign_posn == 3 && sep_by_space == 2) 353 || (sep_by_space == 1 354 && (sign_posn == 0 355 || sign_posn == 1 356 || sign_posn == 2 357 || sign_posn == 4))) 358 PRINT(space_char); 359 PRINTS(currency_symbol); /* XXX: len */ 360 if (sign_posn == 4) { 361 if (sep_by_space == 2) 362 PRINT(' '); 363 PRINTS(signstr); 364 } 365 } 366 } 367 368 if (sign_posn == 2) { 369 if (sep_by_space == 2) 370 PRINT(' '); 371 PRINTS(signstr); 372 } 373 374 if (sign_posn == 0) { 375 if (flags & IS_NEGATIVE) 376 PRINT(')'); 377 else if (left_prec >= 0) 378 PRINT(' '); 379 } 380 381 if (dst - tmpptr < width) { 382 if (flags & LEFT_JUSTIFY) { 383 while (dst - tmpptr < width) 384 PRINT(' '); 385 } else { 386 pad_size = dst-tmpptr; 387 memmove(tmpptr + width-pad_size, tmpptr, 388 (size_t) pad_size); 389 memset(tmpptr, ' ', (size_t) width-pad_size); 390 dst += width-pad_size; 391 } 392 } 393 } 394 395 PRINT('\0'); 396 free(asciivalue); 397 free(currency_symbol); 398 return (dst - s - 1); /* return size of put data except trailing '\0' */ 399 400e2big_error: 401 errno = E2BIG; 402 goto end_error; 403 404format_error: 405 errno = EINVAL; 406 407end_error: 408 sverrno = errno; 409 if (asciivalue != NULL) 410 free(asciivalue); 411 if (currency_symbol != NULL) 412 free(currency_symbol); 413 errno = sverrno; 414 return (-1); 415} 416 417static void 418__setup_vars(int flags, char *cs_precedes, char *sep_by_space, 419 char *sign_posn, const char **signstr, struct lconv *lc) { 420 421 if ((flags & IS_NEGATIVE) && (flags & USE_INTL_CURRENCY)) { 422 *cs_precedes = lc->int_n_cs_precedes; 423 *sep_by_space = lc->int_n_sep_by_space; 424 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_n_sign_posn; 425 *signstr = (lc->negative_sign == '\0') ? "-" 426 : lc->negative_sign; 427 } else if (flags & USE_INTL_CURRENCY) { 428 *cs_precedes = lc->int_p_cs_precedes; 429 *sep_by_space = lc->int_p_sep_by_space; 430 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_p_sign_posn; 431 *signstr = lc->positive_sign; 432 } else if (flags & IS_NEGATIVE) { 433 *cs_precedes = lc->n_cs_precedes; 434 *sep_by_space = lc->n_sep_by_space; 435 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->n_sign_posn; 436 *signstr = (lc->negative_sign == '\0') ? "-" 437 : lc->negative_sign; 438 } else { 439 *cs_precedes = lc->p_cs_precedes; 440 *sep_by_space = lc->p_sep_by_space; 441 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->p_sign_posn; 442 *signstr = lc->positive_sign; 443 } 444 445 /* Set defult values for unspecified information. */ 446 if (*cs_precedes != 0) 447 *cs_precedes = 1; 448 if (*sep_by_space == CHAR_MAX) 449 *sep_by_space = 0; 450 if (*sign_posn == CHAR_MAX) 451 *sign_posn = 0; 452} 453 454static int 455__calc_left_pad(int flags, char *cur_symb, struct lconv *lc) { 456 457 char cs_precedes, sep_by_space, sign_posn; 458 const char *signstr; 459 int left_chars = 0; 460 461 __setup_vars(flags, &cs_precedes, &sep_by_space, &sign_posn, &signstr, lc); 462 463 if (cs_precedes != 0) { 464 left_chars += strlen(cur_symb); 465 if (sep_by_space != 0) 466 left_chars++; 467 } 468 469 switch (sign_posn) { 470 case 0: 471 if (flags & IS_NEGATIVE) 472 left_chars++; 473 break; 474 case 1: 475 left_chars += strlen(signstr); 476 break; 477 case 3: 478 case 4: 479 if (cs_precedes != 0) 480 left_chars += strlen(signstr); 481 } 482 return (left_chars); 483} 484 485static int 486get_groups(int size, const char *grouping) { 487 488 int chars = 0; 489 490 if (*grouping == CHAR_MAX || *grouping <= 0) /* no grouping ? */ 491 return (0); 492 493 while (size > (int)*grouping) { 494 chars++; 495 size -= (int)*grouping++; 496 /* no more grouping ? */ 497 if (*grouping == CHAR_MAX) 498 break; 499 /* rest grouping with same value ? */ 500 if (*grouping == 0) { 501 chars += (size - 1) / *(grouping - 1); 502 break; 503 } 504 } 505 return (chars); 506} 507 508/* convert double to ASCII */ 509__private_extern__ const char *__fix_nogrouping(const char *); 510 511static char * 512__format_grouped_double(double value, int *flags, 513 int left_prec, int right_prec, int pad_char, struct lconv *lc, locale_t loc) { 514 515 char *rslt; 516 char *avalue; 517 int avalue_size; 518 char fmt[32]; 519 520 size_t bufsize; 521 char *bufend; 522 523 int padded; 524 525 const char *grouping; 526 char decimal_point; 527 char thousands_sep; 528 529 int groups = 0; 530 531 grouping = __fix_nogrouping(lc->mon_grouping); 532 decimal_point = *lc->mon_decimal_point; 533 if (decimal_point == '\0') 534 decimal_point = *lc->decimal_point; 535 thousands_sep = *lc->mon_thousands_sep; 536 if (thousands_sep == '\0') 537 thousands_sep = *lc->thousands_sep; 538 539 /* fill left_prec with default value */ 540 if (left_prec == -1) 541 left_prec = 0; 542 543 /* fill right_prec with default value */ 544 if (right_prec == -1) { 545 if (*flags & USE_INTL_CURRENCY) 546 right_prec = lc->int_frac_digits; 547 else 548 right_prec = lc->frac_digits; 549 550 if (right_prec == CHAR_MAX) /* POSIX locale ? */ 551 right_prec = 2; 552 } 553 554 if (*flags & NEED_GROUPING) 555 left_prec += get_groups(left_prec, grouping); 556 557 /* convert to string */ 558 snprintf_l(fmt, sizeof(fmt), loc, "%%%d.%df", left_prec + right_prec + 1, 559 right_prec); 560 avalue_size = asprintf_l(&avalue, loc, fmt, value); 561 if (avalue_size < 0) 562 return (NULL); 563 564 /* make sure that we've enough space for result string */ 565 bufsize = strlen(avalue)*2+1; 566 rslt = malloc(bufsize); 567 if (rslt == NULL) { 568 free(avalue); 569 return (NULL); 570 } 571 memset(rslt, 0, bufsize); 572 bufend = rslt + bufsize - 1; /* reserve space for trailing '\0' */ 573 574 /* skip spaces at beggining */ 575 padded = 0; 576 while (avalue[padded] == ' ') { 577 padded++; 578 avalue_size--; 579 } 580 581 if (right_prec > 0) { 582 bufend -= right_prec; 583 memcpy(bufend, avalue + avalue_size+padded-right_prec, 584 (size_t) right_prec); 585 *--bufend = decimal_point; 586 avalue_size -= (right_prec + 1); 587 } 588 589 if ((*flags & NEED_GROUPING) && 590 thousands_sep != '\0' && /* XXX: need investigation */ 591 *grouping != CHAR_MAX && 592 *grouping > 0) { 593 while (avalue_size > (int)*grouping) { 594 GRPCPY(*grouping); 595 GRPSEP; 596 grouping++; 597 598 /* no more grouping ? */ 599 if (*grouping == CHAR_MAX) 600 break; 601 602 /* rest grouping with same value ? */ 603 if (*grouping == 0) { 604 grouping--; 605 while (avalue_size > *grouping) { 606 GRPCPY(*grouping); 607 GRPSEP; 608 } 609 } 610 } 611 if (avalue_size != 0) 612 GRPCPY(avalue_size); 613 padded -= groups; 614 615 } else { 616 bufend -= avalue_size; 617 memcpy(bufend, avalue+padded, (size_t) avalue_size); 618 if (right_prec == 0) 619 padded--; /* decrease assumed $decimal_point */ 620 } 621 622 /* do padding with pad_char */ 623 if (padded > 0) { 624 bufend -= padded; 625 memset(bufend, pad_char, (size_t) padded); 626 } 627 628 bufsize = bufsize - (bufend - rslt) + 1; 629 memmove(rslt, bufend, bufsize); 630 free(avalue); 631 return (rslt); 632} 633 634ssize_t 635strfmon(char * __restrict s, size_t maxsize, const char * __restrict format, 636 ...) 637{ 638 ssize_t ret; 639 va_list ap; 640 641 va_start(ap, format); 642 ret = _strfmon(s, maxsize, __current_locale(), format, ap); 643 va_end(ap); 644 return ret; 645} 646 647ssize_t 648strfmon_l(char * __restrict s, size_t maxsize, locale_t loc, 649 const char * __restrict format, ...) 650{ 651 ssize_t ret; 652 va_list ap; 653 654 NORMALIZE_LOCALE(loc); 655 va_start(ap, format); 656 ret = _strfmon(s, maxsize, loc, format, ap); 657 va_end(ap); 658 return ret; 659} 660