xref: /titanic_44/usr/src/cmd/localedef/monetary.c (revision 6b5e5868e7ebf1aff3a5abd7d0c4ef0e5fbf3648)
1*6b5e5868SGarrett D'Amore /*
2*6b5e5868SGarrett D'Amore  * This file and its contents are supplied under the terms of the
3*6b5e5868SGarrett D'Amore  * Common Development and Distribution License ("CDDL"), version 1.0.
4*6b5e5868SGarrett D'Amore  * You may only use this file in accordance with the terms version 1.0
5*6b5e5868SGarrett D'Amore  * of the CDDL.
6*6b5e5868SGarrett D'Amore  *
7*6b5e5868SGarrett D'Amore  * A full copy of the text of the CDDL should have accompanied this
8*6b5e5868SGarrett D'Amore  * source.  A copy of the CDDL is also available via the Internet at
9*6b5e5868SGarrett D'Amore  * http://www.illumos.org/license/CDDL.
10*6b5e5868SGarrett D'Amore  */
11*6b5e5868SGarrett D'Amore 
12*6b5e5868SGarrett D'Amore /*
13*6b5e5868SGarrett D'Amore  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
14*6b5e5868SGarrett D'Amore  */
15*6b5e5868SGarrett D'Amore 
16*6b5e5868SGarrett D'Amore /*
17*6b5e5868SGarrett D'Amore  * LC_MONETARY database generation routines for localedef.
18*6b5e5868SGarrett D'Amore  */
19*6b5e5868SGarrett D'Amore 
20*6b5e5868SGarrett D'Amore #include <stdio.h>
21*6b5e5868SGarrett D'Amore #include <stdlib.h>
22*6b5e5868SGarrett D'Amore #include <errno.h>
23*6b5e5868SGarrett D'Amore #include <sys/types.h>
24*6b5e5868SGarrett D'Amore #include <string.h>
25*6b5e5868SGarrett D'Amore #include <unistd.h>
26*6b5e5868SGarrett D'Amore #include "localedef.h"
27*6b5e5868SGarrett D'Amore #include "parser.tab.h"
28*6b5e5868SGarrett D'Amore #include "lmonetary.h"
29*6b5e5868SGarrett D'Amore 
30*6b5e5868SGarrett D'Amore static struct lc_monetary_T mon;
31*6b5e5868SGarrett D'Amore 
32*6b5e5868SGarrett D'Amore void
33*6b5e5868SGarrett D'Amore init_monetary(void)
34*6b5e5868SGarrett D'Amore {
35*6b5e5868SGarrett D'Amore 	(void) memset(&mon, 0, sizeof (mon));
36*6b5e5868SGarrett D'Amore }
37*6b5e5868SGarrett D'Amore 
38*6b5e5868SGarrett D'Amore void
39*6b5e5868SGarrett D'Amore add_monetary_str(wchar_t *wcs)
40*6b5e5868SGarrett D'Amore {
41*6b5e5868SGarrett D'Amore 	char *str;
42*6b5e5868SGarrett D'Amore 
43*6b5e5868SGarrett D'Amore 	if ((str = to_mb_string(wcs)) == NULL) {
44*6b5e5868SGarrett D'Amore 		INTERR;
45*6b5e5868SGarrett D'Amore 		return;
46*6b5e5868SGarrett D'Amore 	}
47*6b5e5868SGarrett D'Amore 	free(wcs);
48*6b5e5868SGarrett D'Amore 	switch (last_kw) {
49*6b5e5868SGarrett D'Amore 	case T_INT_CURR_SYMBOL:
50*6b5e5868SGarrett D'Amore 		mon.int_curr_symbol = str;
51*6b5e5868SGarrett D'Amore 		break;
52*6b5e5868SGarrett D'Amore 	case T_CURRENCY_SYMBOL:
53*6b5e5868SGarrett D'Amore 		mon.currency_symbol = str;
54*6b5e5868SGarrett D'Amore 		break;
55*6b5e5868SGarrett D'Amore 	case T_MON_DECIMAL_POINT:
56*6b5e5868SGarrett D'Amore 		mon.mon_decimal_point = str;
57*6b5e5868SGarrett D'Amore 		break;
58*6b5e5868SGarrett D'Amore 	case T_MON_THOUSANDS_SEP:
59*6b5e5868SGarrett D'Amore 		mon.mon_thousands_sep = str;
60*6b5e5868SGarrett D'Amore 		break;
61*6b5e5868SGarrett D'Amore 	case T_POSITIVE_SIGN:
62*6b5e5868SGarrett D'Amore 		mon.positive_sign = str;
63*6b5e5868SGarrett D'Amore 		break;
64*6b5e5868SGarrett D'Amore 	case T_NEGATIVE_SIGN:
65*6b5e5868SGarrett D'Amore 		mon.negative_sign = str;
66*6b5e5868SGarrett D'Amore 		break;
67*6b5e5868SGarrett D'Amore 	default:
68*6b5e5868SGarrett D'Amore 		free(str);
69*6b5e5868SGarrett D'Amore 		INTERR;
70*6b5e5868SGarrett D'Amore 		break;
71*6b5e5868SGarrett D'Amore 	}
72*6b5e5868SGarrett D'Amore }
73*6b5e5868SGarrett D'Amore 
74*6b5e5868SGarrett D'Amore void
75*6b5e5868SGarrett D'Amore add_monetary_num(int n)
76*6b5e5868SGarrett D'Amore {
77*6b5e5868SGarrett D'Amore 	char *str = NULL;
78*6b5e5868SGarrett D'Amore 
79*6b5e5868SGarrett D'Amore 	(void) asprintf(&str, "%d", n);
80*6b5e5868SGarrett D'Amore 	if (str == NULL) {
81*6b5e5868SGarrett D'Amore 		errf(_("out of memory"));
82*6b5e5868SGarrett D'Amore 		return;
83*6b5e5868SGarrett D'Amore 	}
84*6b5e5868SGarrett D'Amore 
85*6b5e5868SGarrett D'Amore 	switch (last_kw) {
86*6b5e5868SGarrett D'Amore 	case T_INT_FRAC_DIGITS:
87*6b5e5868SGarrett D'Amore 		mon.int_frac_digits = str;
88*6b5e5868SGarrett D'Amore 		break;
89*6b5e5868SGarrett D'Amore 	case T_FRAC_DIGITS:
90*6b5e5868SGarrett D'Amore 		mon.frac_digits = str;
91*6b5e5868SGarrett D'Amore 		break;
92*6b5e5868SGarrett D'Amore 	case T_P_CS_PRECEDES:
93*6b5e5868SGarrett D'Amore 		mon.p_cs_precedes = str;
94*6b5e5868SGarrett D'Amore 		break;
95*6b5e5868SGarrett D'Amore 	case T_P_SEP_BY_SPACE:
96*6b5e5868SGarrett D'Amore 		mon.p_sep_by_space = str;
97*6b5e5868SGarrett D'Amore 		break;
98*6b5e5868SGarrett D'Amore 	case T_N_CS_PRECEDES:
99*6b5e5868SGarrett D'Amore 		mon.n_cs_precedes = str;
100*6b5e5868SGarrett D'Amore 		break;
101*6b5e5868SGarrett D'Amore 	case T_N_SEP_BY_SPACE:
102*6b5e5868SGarrett D'Amore 		mon.n_sep_by_space = str;
103*6b5e5868SGarrett D'Amore 		break;
104*6b5e5868SGarrett D'Amore 	case T_P_SIGN_POSN:
105*6b5e5868SGarrett D'Amore 		mon.p_sign_posn = str;
106*6b5e5868SGarrett D'Amore 		break;
107*6b5e5868SGarrett D'Amore 	case T_N_SIGN_POSN:
108*6b5e5868SGarrett D'Amore 		mon.n_sign_posn = str;
109*6b5e5868SGarrett D'Amore 		break;
110*6b5e5868SGarrett D'Amore 	case T_INT_P_CS_PRECEDES:
111*6b5e5868SGarrett D'Amore 		mon.int_p_cs_precedes = str;
112*6b5e5868SGarrett D'Amore 		break;
113*6b5e5868SGarrett D'Amore 	case T_INT_N_CS_PRECEDES:
114*6b5e5868SGarrett D'Amore 		mon.int_n_cs_precedes = str;
115*6b5e5868SGarrett D'Amore 		break;
116*6b5e5868SGarrett D'Amore 	case T_INT_P_SEP_BY_SPACE:
117*6b5e5868SGarrett D'Amore 		mon.int_p_sep_by_space = str;
118*6b5e5868SGarrett D'Amore 		break;
119*6b5e5868SGarrett D'Amore 	case T_INT_N_SEP_BY_SPACE:
120*6b5e5868SGarrett D'Amore 		mon.int_n_sep_by_space = str;
121*6b5e5868SGarrett D'Amore 		break;
122*6b5e5868SGarrett D'Amore 	case T_INT_P_SIGN_POSN:
123*6b5e5868SGarrett D'Amore 		mon.int_p_sign_posn = str;
124*6b5e5868SGarrett D'Amore 		break;
125*6b5e5868SGarrett D'Amore 	case T_INT_N_SIGN_POSN:
126*6b5e5868SGarrett D'Amore 		mon.int_n_sign_posn = str;
127*6b5e5868SGarrett D'Amore 		break;
128*6b5e5868SGarrett D'Amore 	case T_MON_GROUPING:
129*6b5e5868SGarrett D'Amore 		mon.mon_grouping = str;
130*6b5e5868SGarrett D'Amore 		break;
131*6b5e5868SGarrett D'Amore 	default:
132*6b5e5868SGarrett D'Amore 		INTERR;
133*6b5e5868SGarrett D'Amore 		break;
134*6b5e5868SGarrett D'Amore 	}
135*6b5e5868SGarrett D'Amore }
136*6b5e5868SGarrett D'Amore 
137*6b5e5868SGarrett D'Amore void
138*6b5e5868SGarrett D'Amore reset_monetary_group(void)
139*6b5e5868SGarrett D'Amore {
140*6b5e5868SGarrett D'Amore 	free((char *)mon.mon_grouping);
141*6b5e5868SGarrett D'Amore 	mon.mon_grouping = NULL;
142*6b5e5868SGarrett D'Amore }
143*6b5e5868SGarrett D'Amore 
144*6b5e5868SGarrett D'Amore void
145*6b5e5868SGarrett D'Amore add_monetary_group(int n)
146*6b5e5868SGarrett D'Amore {
147*6b5e5868SGarrett D'Amore 	char *s = NULL;
148*6b5e5868SGarrett D'Amore 
149*6b5e5868SGarrett D'Amore 	if (mon.mon_grouping == NULL) {
150*6b5e5868SGarrett D'Amore 		(void) asprintf(&s, "%d", n);
151*6b5e5868SGarrett D'Amore 	} else {
152*6b5e5868SGarrett D'Amore 		(void) asprintf(&s, "%s;%d", mon.mon_grouping, n);
153*6b5e5868SGarrett D'Amore 	}
154*6b5e5868SGarrett D'Amore 	if (s == NULL)
155*6b5e5868SGarrett D'Amore 		errf(_("out of memory"));
156*6b5e5868SGarrett D'Amore 
157*6b5e5868SGarrett D'Amore 	free((char *)mon.mon_grouping);
158*6b5e5868SGarrett D'Amore 	mon.mon_grouping = s;
159*6b5e5868SGarrett D'Amore }
160*6b5e5868SGarrett D'Amore 
161*6b5e5868SGarrett D'Amore void
162*6b5e5868SGarrett D'Amore dump_monetary(void)
163*6b5e5868SGarrett D'Amore {
164*6b5e5868SGarrett D'Amore 	FILE *f;
165*6b5e5868SGarrett D'Amore 
166*6b5e5868SGarrett D'Amore 	if ((f = open_category()) == NULL) {
167*6b5e5868SGarrett D'Amore 		return;
168*6b5e5868SGarrett D'Amore 	}
169*6b5e5868SGarrett D'Amore 
170*6b5e5868SGarrett D'Amore 	if ((putl_category(mon.int_curr_symbol, f) == EOF) ||
171*6b5e5868SGarrett D'Amore 	    (putl_category(mon.currency_symbol, f) == EOF) ||
172*6b5e5868SGarrett D'Amore 	    (putl_category(mon.mon_decimal_point, f) == EOF) ||
173*6b5e5868SGarrett D'Amore 	    (putl_category(mon.mon_thousands_sep, f) == EOF) ||
174*6b5e5868SGarrett D'Amore 	    (putl_category(mon.mon_grouping, f) == EOF) ||
175*6b5e5868SGarrett D'Amore 	    (putl_category(mon.positive_sign, f) == EOF) ||
176*6b5e5868SGarrett D'Amore 	    (putl_category(mon.negative_sign, f) == EOF) ||
177*6b5e5868SGarrett D'Amore 	    (putl_category(mon.int_frac_digits, f) == EOF) ||
178*6b5e5868SGarrett D'Amore 	    (putl_category(mon.frac_digits, f) == EOF) ||
179*6b5e5868SGarrett D'Amore 	    (putl_category(mon.p_cs_precedes, f) == EOF) ||
180*6b5e5868SGarrett D'Amore 	    (putl_category(mon.p_sep_by_space, f) == EOF) ||
181*6b5e5868SGarrett D'Amore 	    (putl_category(mon.n_cs_precedes, f) == EOF) ||
182*6b5e5868SGarrett D'Amore 	    (putl_category(mon.n_sep_by_space, f) == EOF) ||
183*6b5e5868SGarrett D'Amore 	    (putl_category(mon.p_sign_posn, f) == EOF) ||
184*6b5e5868SGarrett D'Amore 	    (putl_category(mon.n_sign_posn, f) == EOF) ||
185*6b5e5868SGarrett D'Amore 	    (putl_category(mon.int_p_cs_precedes, f) == EOF) ||
186*6b5e5868SGarrett D'Amore 	    (putl_category(mon.int_n_cs_precedes, f) == EOF) ||
187*6b5e5868SGarrett D'Amore 	    (putl_category(mon.int_p_sep_by_space, f) == EOF) ||
188*6b5e5868SGarrett D'Amore 	    (putl_category(mon.int_n_sep_by_space, f) == EOF) ||
189*6b5e5868SGarrett D'Amore 	    (putl_category(mon.int_p_sign_posn, f) == EOF) ||
190*6b5e5868SGarrett D'Amore 	    (putl_category(mon.int_n_sign_posn, f) == EOF)) {
191*6b5e5868SGarrett D'Amore 		return;
192*6b5e5868SGarrett D'Amore 	}
193*6b5e5868SGarrett D'Amore 	close_category(f);
194*6b5e5868SGarrett D'Amore }
195