xref: /illumos-gate/usr/src/cmd/localedef/monetary.c (revision 2d08521b)
16b5e5868SGarrett D'Amore /*
26b5e5868SGarrett D'Amore  * This file and its contents are supplied under the terms of the
36b5e5868SGarrett D'Amore  * Common Development and Distribution License ("CDDL"), version 1.0.
45aec55ebSGarrett D'Amore  * You may only use this file in accordance with the terms of version
55aec55ebSGarrett D'Amore  * 1.0 of the CDDL.
66b5e5868SGarrett D'Amore  *
76b5e5868SGarrett D'Amore  * A full copy of the text of the CDDL should have accompanied this
86b5e5868SGarrett D'Amore  * source.  A copy of the CDDL is also available via the Internet at
96b5e5868SGarrett D'Amore  * http://www.illumos.org/license/CDDL.
106b5e5868SGarrett D'Amore  */
116b5e5868SGarrett D'Amore 
126b5e5868SGarrett D'Amore /*
13*2d08521bSGarrett D'Amore  * Copyright 2013 Garrett D'Amore <garrett@damore.org>
146b5e5868SGarrett D'Amore  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
156b5e5868SGarrett D'Amore  */
166b5e5868SGarrett D'Amore 
176b5e5868SGarrett D'Amore /*
186b5e5868SGarrett D'Amore  * LC_MONETARY database generation routines for localedef.
196b5e5868SGarrett D'Amore  */
206b5e5868SGarrett D'Amore 
216b5e5868SGarrett D'Amore #include <stdio.h>
226b5e5868SGarrett D'Amore #include <stdlib.h>
236b5e5868SGarrett D'Amore #include <errno.h>
246b5e5868SGarrett D'Amore #include <sys/types.h>
256b5e5868SGarrett D'Amore #include <string.h>
266b5e5868SGarrett D'Amore #include <unistd.h>
276b5e5868SGarrett D'Amore #include "localedef.h"
286b5e5868SGarrett D'Amore #include "parser.tab.h"
296b5e5868SGarrett D'Amore #include "lmonetary.h"
306b5e5868SGarrett D'Amore 
31*2d08521bSGarrett D'Amore static struct lc_monetary mon;
326b5e5868SGarrett D'Amore 
336b5e5868SGarrett D'Amore void
init_monetary(void)346b5e5868SGarrett D'Amore init_monetary(void)
356b5e5868SGarrett D'Amore {
366b5e5868SGarrett D'Amore 	(void) memset(&mon, 0, sizeof (mon));
376b5e5868SGarrett D'Amore }
386b5e5868SGarrett D'Amore 
396b5e5868SGarrett D'Amore void
add_monetary_str(wchar_t * wcs)406b5e5868SGarrett D'Amore add_monetary_str(wchar_t *wcs)
416b5e5868SGarrett D'Amore {
426b5e5868SGarrett D'Amore 	char *str;
436b5e5868SGarrett D'Amore 
446b5e5868SGarrett D'Amore 	if ((str = to_mb_string(wcs)) == NULL) {
456b5e5868SGarrett D'Amore 		INTERR;
466b5e5868SGarrett D'Amore 		return;
476b5e5868SGarrett D'Amore 	}
486b5e5868SGarrett D'Amore 	free(wcs);
496b5e5868SGarrett D'Amore 	switch (last_kw) {
506b5e5868SGarrett D'Amore 	case T_INT_CURR_SYMBOL:
516b5e5868SGarrett D'Amore 		mon.int_curr_symbol = str;
526b5e5868SGarrett D'Amore 		break;
536b5e5868SGarrett D'Amore 	case T_CURRENCY_SYMBOL:
546b5e5868SGarrett D'Amore 		mon.currency_symbol = str;
556b5e5868SGarrett D'Amore 		break;
566b5e5868SGarrett D'Amore 	case T_MON_DECIMAL_POINT:
576b5e5868SGarrett D'Amore 		mon.mon_decimal_point = str;
586b5e5868SGarrett D'Amore 		break;
596b5e5868SGarrett D'Amore 	case T_MON_THOUSANDS_SEP:
606b5e5868SGarrett D'Amore 		mon.mon_thousands_sep = str;
616b5e5868SGarrett D'Amore 		break;
626b5e5868SGarrett D'Amore 	case T_POSITIVE_SIGN:
636b5e5868SGarrett D'Amore 		mon.positive_sign = str;
646b5e5868SGarrett D'Amore 		break;
656b5e5868SGarrett D'Amore 	case T_NEGATIVE_SIGN:
666b5e5868SGarrett D'Amore 		mon.negative_sign = str;
676b5e5868SGarrett D'Amore 		break;
686b5e5868SGarrett D'Amore 	default:
696b5e5868SGarrett D'Amore 		free(str);
706b5e5868SGarrett D'Amore 		INTERR;
716b5e5868SGarrett D'Amore 		break;
726b5e5868SGarrett D'Amore 	}
736b5e5868SGarrett D'Amore }
746b5e5868SGarrett D'Amore 
756b5e5868SGarrett D'Amore void
add_monetary_num(int n)766b5e5868SGarrett D'Amore add_monetary_num(int n)
776b5e5868SGarrett D'Amore {
786b5e5868SGarrett D'Amore 	char *str = NULL;
796b5e5868SGarrett D'Amore 
806b5e5868SGarrett D'Amore 	(void) asprintf(&str, "%d", n);
816b5e5868SGarrett D'Amore 	if (str == NULL) {
826b5e5868SGarrett D'Amore 		errf(_("out of memory"));
836b5e5868SGarrett D'Amore 		return;
846b5e5868SGarrett D'Amore 	}
856b5e5868SGarrett D'Amore 
866b5e5868SGarrett D'Amore 	switch (last_kw) {
876b5e5868SGarrett D'Amore 	case T_INT_FRAC_DIGITS:
886b5e5868SGarrett D'Amore 		mon.int_frac_digits = str;
896b5e5868SGarrett D'Amore 		break;
906b5e5868SGarrett D'Amore 	case T_FRAC_DIGITS:
916b5e5868SGarrett D'Amore 		mon.frac_digits = str;
926b5e5868SGarrett D'Amore 		break;
936b5e5868SGarrett D'Amore 	case T_P_CS_PRECEDES:
946b5e5868SGarrett D'Amore 		mon.p_cs_precedes = str;
956b5e5868SGarrett D'Amore 		break;
966b5e5868SGarrett D'Amore 	case T_P_SEP_BY_SPACE:
976b5e5868SGarrett D'Amore 		mon.p_sep_by_space = str;
986b5e5868SGarrett D'Amore 		break;
996b5e5868SGarrett D'Amore 	case T_N_CS_PRECEDES:
1006b5e5868SGarrett D'Amore 		mon.n_cs_precedes = str;
1016b5e5868SGarrett D'Amore 		break;
1026b5e5868SGarrett D'Amore 	case T_N_SEP_BY_SPACE:
1036b5e5868SGarrett D'Amore 		mon.n_sep_by_space = str;
1046b5e5868SGarrett D'Amore 		break;
1056b5e5868SGarrett D'Amore 	case T_P_SIGN_POSN:
1066b5e5868SGarrett D'Amore 		mon.p_sign_posn = str;
1076b5e5868SGarrett D'Amore 		break;
1086b5e5868SGarrett D'Amore 	case T_N_SIGN_POSN:
1096b5e5868SGarrett D'Amore 		mon.n_sign_posn = str;
1106b5e5868SGarrett D'Amore 		break;
1116b5e5868SGarrett D'Amore 	case T_INT_P_CS_PRECEDES:
1126b5e5868SGarrett D'Amore 		mon.int_p_cs_precedes = str;
1136b5e5868SGarrett D'Amore 		break;
1146b5e5868SGarrett D'Amore 	case T_INT_N_CS_PRECEDES:
1156b5e5868SGarrett D'Amore 		mon.int_n_cs_precedes = str;
1166b5e5868SGarrett D'Amore 		break;
1176b5e5868SGarrett D'Amore 	case T_INT_P_SEP_BY_SPACE:
1186b5e5868SGarrett D'Amore 		mon.int_p_sep_by_space = str;
1196b5e5868SGarrett D'Amore 		break;
1206b5e5868SGarrett D'Amore 	case T_INT_N_SEP_BY_SPACE:
1216b5e5868SGarrett D'Amore 		mon.int_n_sep_by_space = str;
1226b5e5868SGarrett D'Amore 		break;
1236b5e5868SGarrett D'Amore 	case T_INT_P_SIGN_POSN:
1246b5e5868SGarrett D'Amore 		mon.int_p_sign_posn = str;
1256b5e5868SGarrett D'Amore 		break;
1266b5e5868SGarrett D'Amore 	case T_INT_N_SIGN_POSN:
1276b5e5868SGarrett D'Amore 		mon.int_n_sign_posn = str;
1286b5e5868SGarrett D'Amore 		break;
1296b5e5868SGarrett D'Amore 	case T_MON_GROUPING:
1306b5e5868SGarrett D'Amore 		mon.mon_grouping = str;
1316b5e5868SGarrett D'Amore 		break;
1326b5e5868SGarrett D'Amore 	default:
1336b5e5868SGarrett D'Amore 		INTERR;
1346b5e5868SGarrett D'Amore 		break;
1356b5e5868SGarrett D'Amore 	}
1366b5e5868SGarrett D'Amore }
1376b5e5868SGarrett D'Amore 
1386b5e5868SGarrett D'Amore void
reset_monetary_group(void)1396b5e5868SGarrett D'Amore reset_monetary_group(void)
1406b5e5868SGarrett D'Amore {
1416b5e5868SGarrett D'Amore 	free((char *)mon.mon_grouping);
1426b5e5868SGarrett D'Amore 	mon.mon_grouping = NULL;
1436b5e5868SGarrett D'Amore }
1446b5e5868SGarrett D'Amore 
1456b5e5868SGarrett D'Amore void
add_monetary_group(int n)1466b5e5868SGarrett D'Amore add_monetary_group(int n)
1476b5e5868SGarrett D'Amore {
1486b5e5868SGarrett D'Amore 	char *s = NULL;
1496b5e5868SGarrett D'Amore 
1506b5e5868SGarrett D'Amore 	if (mon.mon_grouping == NULL) {
1516b5e5868SGarrett D'Amore 		(void) asprintf(&s, "%d", n);
1526b5e5868SGarrett D'Amore 	} else {
1536b5e5868SGarrett D'Amore 		(void) asprintf(&s, "%s;%d", mon.mon_grouping, n);
1546b5e5868SGarrett D'Amore 	}
1556b5e5868SGarrett D'Amore 	if (s == NULL)
1566b5e5868SGarrett D'Amore 		errf(_("out of memory"));
1576b5e5868SGarrett D'Amore 
1586b5e5868SGarrett D'Amore 	free((char *)mon.mon_grouping);
1596b5e5868SGarrett D'Amore 	mon.mon_grouping = s;
1606b5e5868SGarrett D'Amore }
1616b5e5868SGarrett D'Amore 
1626b5e5868SGarrett D'Amore void
dump_monetary(void)1636b5e5868SGarrett D'Amore dump_monetary(void)
1646b5e5868SGarrett D'Amore {
1656b5e5868SGarrett D'Amore 	FILE *f;
1666b5e5868SGarrett D'Amore 
1676b5e5868SGarrett D'Amore 	if ((f = open_category()) == NULL) {
1686b5e5868SGarrett D'Amore 		return;
1696b5e5868SGarrett D'Amore 	}
1706b5e5868SGarrett D'Amore 
1716b5e5868SGarrett D'Amore 	if ((putl_category(mon.int_curr_symbol, f) == EOF) ||
1726b5e5868SGarrett D'Amore 	    (putl_category(mon.currency_symbol, f) == EOF) ||
1736b5e5868SGarrett D'Amore 	    (putl_category(mon.mon_decimal_point, f) == EOF) ||
1746b5e5868SGarrett D'Amore 	    (putl_category(mon.mon_thousands_sep, f) == EOF) ||
1756b5e5868SGarrett D'Amore 	    (putl_category(mon.mon_grouping, f) == EOF) ||
1766b5e5868SGarrett D'Amore 	    (putl_category(mon.positive_sign, f) == EOF) ||
1776b5e5868SGarrett D'Amore 	    (putl_category(mon.negative_sign, f) == EOF) ||
1786b5e5868SGarrett D'Amore 	    (putl_category(mon.int_frac_digits, f) == EOF) ||
1796b5e5868SGarrett D'Amore 	    (putl_category(mon.frac_digits, f) == EOF) ||
1806b5e5868SGarrett D'Amore 	    (putl_category(mon.p_cs_precedes, f) == EOF) ||
1816b5e5868SGarrett D'Amore 	    (putl_category(mon.p_sep_by_space, f) == EOF) ||
1826b5e5868SGarrett D'Amore 	    (putl_category(mon.n_cs_precedes, f) == EOF) ||
1836b5e5868SGarrett D'Amore 	    (putl_category(mon.n_sep_by_space, f) == EOF) ||
1846b5e5868SGarrett D'Amore 	    (putl_category(mon.p_sign_posn, f) == EOF) ||
1856b5e5868SGarrett D'Amore 	    (putl_category(mon.n_sign_posn, f) == EOF) ||
1866b5e5868SGarrett D'Amore 	    (putl_category(mon.int_p_cs_precedes, f) == EOF) ||
1876b5e5868SGarrett D'Amore 	    (putl_category(mon.int_n_cs_precedes, f) == EOF) ||
1886b5e5868SGarrett D'Amore 	    (putl_category(mon.int_p_sep_by_space, f) == EOF) ||
1896b5e5868SGarrett D'Amore 	    (putl_category(mon.int_n_sep_by_space, f) == EOF) ||
1906b5e5868SGarrett D'Amore 	    (putl_category(mon.int_p_sign_posn, f) == EOF) ||
1916b5e5868SGarrett D'Amore 	    (putl_category(mon.int_n_sign_posn, f) == EOF)) {
1926b5e5868SGarrett D'Amore 		return;
1936b5e5868SGarrett D'Amore 	}
1946b5e5868SGarrett D'Amore 	close_category(f);
1956b5e5868SGarrett D'Amore }
196