1158115Sume/*-
2158115Sume * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
3158115Sume * Copyright 2015 John Marino <draco@marino.st>
4158115Sume *
5158115Sume * This source code is derived from the illumos localedef command, and
6158115Sume * provided under BSD-style license terms by Nexenta Systems, Inc.
7158115Sume *
8158115Sume * Redistribution and use in source and binary forms, with or without
9158115Sume * modification, are permitted provided that the following conditions
10158115Sume * are met:
11158115Sume *
12158115Sume * 1. Redistributions of source code must retain the above copyright
13158115Sume *    notice, this list of conditions and the following disclaimer.
14158115Sume * 2. Redistributions in binary form must reproduce the above copyright
15158115Sume *    notice, this list of conditions and the following disclaimer in the
16158115Sume *    documentation and/or other materials provided with the distribution.
17158115Sume *
18158115Sume * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19158115Sume * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20158115Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21158115Sume * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22158115Sume * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23158115Sume * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24158115Sume * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25158115Sume * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26158115Sume * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27158115Sume * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28158115Sume * POSSIBILITY OF SUCH DAMAGE.
29158115Sume */
30158115Sume
31158115Sume/*
32194093Sdes * LC_MONETARY database generation routines for localedef.
33158115Sume */
34194093Sdes#include <sys/cdefs.h>
35158115Sume#include <stdio.h>
36158115Sume#include <stdlib.h>
37158115Sume#include <errno.h>
38158115Sume#include <sys/types.h>
39158115Sume#include <string.h>
40158115Sume#include <unistd.h>
41158115Sume#include "localedef.h"
42158115Sume#include "parser.h"
43158115Sume#include "lmonetary.h"
44158115Sume
45158115Sumestatic struct lc_monetary_T mon;
46158115Sume
47158115Sumevoid
48158115Sumeinit_monetary(void)
49158115Sume{
50158115Sume	(void) memset(&mon, 0, sizeof (mon));
51158115Sume}
52158115Sume
53158115Sumevoid
54158115Sumeadd_monetary_str(wchar_t *wcs)
55158115Sume{
56158115Sume	char *str;
57158115Sume
58158115Sume	if ((str = to_mb_string(wcs)) == NULL) {
59194087Sdes		INTERR;
60158115Sume		return;
61158115Sume	}
62158115Sume	free(wcs);
63158115Sume	switch (last_kw) {
64194104Sdes	case T_INT_CURR_SYMBOL:
65158115Sume		mon.int_curr_symbol = str;
66158115Sume		break;
67158115Sume	case T_CURRENCY_SYMBOL:
68158115Sume		mon.currency_symbol = str;
69158115Sume		break;
70158115Sume	case T_MON_DECIMAL_POINT:
71158115Sume		mon.mon_decimal_point = str;
72158115Sume		break;
73158115Sume	case T_MON_THOUSANDS_SEP:
74158115Sume		mon.mon_thousands_sep = str;
75158115Sume		break;
76158115Sume	case T_POSITIVE_SIGN:
77158115Sume		mon.positive_sign = str;
78158115Sume		break;
79158115Sume	case T_NEGATIVE_SIGN:
80158115Sume		mon.negative_sign = str;
81194104Sdes		break;
82158115Sume	default:
83158115Sume		free(str);
84158115Sume		INTERR;
85158115Sume		break;
86158115Sume	}
87158115Sume}
88158115Sume
89158115Sumevoid
90158115Sumeadd_monetary_num(int n)
91158115Sume{
92158115Sume	char *str = NULL;
93158115Sume
94158115Sume	(void) asprintf(&str, "%d", n);
95158115Sume	if (str == NULL) {
96158115Sume		fprintf(stderr, "out of memory\n");
97158115Sume		return;
98158115Sume	}
99158115Sume
100158115Sume	switch (last_kw) {
101158115Sume	case T_INT_FRAC_DIGITS:
102158115Sume		mon.int_frac_digits = str;
103158115Sume		break;
104158115Sume	case T_FRAC_DIGITS:
105158115Sume		mon.frac_digits = str;
106158115Sume		break;
107158115Sume	case T_P_CS_PRECEDES:
108158115Sume		mon.p_cs_precedes = str;
109158115Sume		break;
110158115Sume	case T_P_SEP_BY_SPACE:
111158115Sume		mon.p_sep_by_space = str;
112158115Sume		break;
113158115Sume	case T_N_CS_PRECEDES:
114158115Sume		mon.n_cs_precedes = str;
115158115Sume		break;
116158115Sume	case T_N_SEP_BY_SPACE:
117158115Sume		mon.n_sep_by_space = str;
118158115Sume		break;
119158115Sume	case T_P_SIGN_POSN:
120158115Sume		mon.p_sign_posn = str;
121158115Sume		break;
122158115Sume	case T_N_SIGN_POSN:
123158115Sume		mon.n_sign_posn = str;
124158115Sume		break;
125158115Sume	case T_INT_P_CS_PRECEDES:
126158115Sume		mon.int_p_cs_precedes = str;
127158115Sume		break;
128	case T_INT_N_CS_PRECEDES:
129		mon.int_n_cs_precedes = str;
130		break;
131	case T_INT_P_SEP_BY_SPACE:
132		mon.int_p_sep_by_space = str;
133		break;
134	case T_INT_N_SEP_BY_SPACE:
135		mon.int_n_sep_by_space = str;
136		break;
137	case T_INT_P_SIGN_POSN:
138		mon.int_p_sign_posn = str;
139		break;
140	case T_INT_N_SIGN_POSN:
141		mon.int_n_sign_posn = str;
142		break;
143	case T_MON_GROUPING:
144		mon.mon_grouping = str;
145		break;
146	default:
147		INTERR;
148		break;
149	}
150}
151
152void
153reset_monetary_group(void)
154{
155	free((char *)mon.mon_grouping);
156	mon.mon_grouping = NULL;
157}
158
159void
160add_monetary_group(int n)
161{
162	char *s = NULL;
163
164	if (mon.mon_grouping == NULL) {
165		(void) asprintf(&s, "%d", n);
166	} else {
167		(void) asprintf(&s, "%s;%d", mon.mon_grouping, n);
168	}
169	if (s == NULL)
170		fprintf(stderr, "out of memory\n");
171
172	free((char *)mon.mon_grouping);
173	mon.mon_grouping = s;
174}
175
176void
177dump_monetary(void)
178{
179	FILE *f;
180
181	if ((f = open_category()) == NULL) {
182		return;
183	}
184
185	if ((putl_category(mon.int_curr_symbol, f) == EOF) ||
186	    (putl_category(mon.currency_symbol, f) == EOF) ||
187	    (putl_category(mon.mon_decimal_point, f) == EOF) ||
188	    (putl_category(mon.mon_thousands_sep, f) == EOF) ||
189	    (putl_category(mon.mon_grouping, f) == EOF) ||
190	    (putl_category(mon.positive_sign, f) == EOF) ||
191	    (putl_category(mon.negative_sign, f) == EOF) ||
192	    (putl_category(mon.int_frac_digits, f) == EOF) ||
193	    (putl_category(mon.frac_digits, f) == EOF) ||
194	    (putl_category(mon.p_cs_precedes, f) == EOF) ||
195	    (putl_category(mon.p_sep_by_space, f) == EOF) ||
196	    (putl_category(mon.n_cs_precedes, f) == EOF) ||
197	    (putl_category(mon.n_sep_by_space, f) == EOF) ||
198	    (putl_category(mon.p_sign_posn, f) == EOF) ||
199	    (putl_category(mon.n_sign_posn, f) == EOF) ||
200	    (putl_category(mon.int_p_cs_precedes, f) == EOF) ||
201	    (putl_category(mon.int_n_cs_precedes, f) == EOF) ||
202	    (putl_category(mon.int_p_sep_by_space, f) == EOF) ||
203	    (putl_category(mon.int_n_sep_by_space, f) == EOF) ||
204	    (putl_category(mon.int_p_sign_posn, f) == EOF) ||
205	    (putl_category(mon.int_n_sign_posn, f) == EOF)) {
206		return;
207	}
208	close_category(f);
209}
210