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