xref: /freebsd/usr.bin/localedef/time.c (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
1 /*
2  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
3  * Copyright 2015 John Marino <draco@marino.st>
4  *
5  * This source code is derived from the illumos localedef command, and
6  * provided under BSD-style license terms by Nexenta Systems, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /*
32  * LC_TIME database generation routines for localedef.
33  */
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <errno.h>
40 #include <sys/types.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include "localedef.h"
44 #include "parser.h"
45 #include "timelocal.h"
46 
47 struct lc_time_T tm;
48 
49 void
50 init_time(void)
51 {
52 	(void) memset(&tm, 0, sizeof (tm));
53 }
54 
55 void
56 add_time_str(wchar_t *wcs)
57 {
58 	char	*str;
59 
60 	if ((str = to_mb_string(wcs)) == NULL) {
61 		INTERR;
62 		return;
63 	}
64 	free(wcs);
65 
66 	switch (last_kw) {
67 	case T_D_T_FMT:
68 		tm.c_fmt = str;
69 		break;
70 	case T_D_FMT:
71 		tm.x_fmt = str;
72 		break;
73 	case T_T_FMT:
74 		tm.X_fmt = str;
75 		break;
76 	case T_T_FMT_AMPM:
77 		tm.ampm_fmt = str;
78 		break;
79 	case T_DATE_FMT:
80 		/*
81 		 * This one is a Solaris extension, Too bad date just
82 		 * doesn't use %c, which would be simpler.
83 		 */
84 		tm.date_fmt = str;
85 		break;
86 	case T_ERA_D_FMT:
87 	case T_ERA_T_FMT:
88 	case T_ERA_D_T_FMT:
89 		/* Silently ignore it. */
90 		break;
91 	default:
92 		free(str);
93 		INTERR;
94 		break;
95 	}
96 }
97 
98 static void
99 add_list(const char *ptr[], char *str, int limit)
100 {
101 	int	i;
102 	for (i = 0; i < limit; i++) {
103 		if (ptr[i] == NULL) {
104 			ptr[i] = str;
105 			return;
106 		}
107 	}
108 	fprintf(stderr,"too many list elements");
109 }
110 
111 void
112 add_time_list(wchar_t *wcs)
113 {
114 	char *str;
115 
116 	if ((str = to_mb_string(wcs)) == NULL) {
117 		INTERR;
118 		return;
119 	}
120 	free(wcs);
121 
122 	switch (last_kw) {
123 	case T_ABMON:
124 		add_list(tm.mon, str, 12);
125 		break;
126 	case T_MON:
127 		add_list(tm.month, str, 12);
128 		break;
129 	case T_ABDAY:
130 		add_list(tm.wday, str, 7);
131 		break;
132 	case T_DAY:
133 		add_list(tm.weekday, str, 7);
134 		break;
135 	case T_AM_PM:
136 		if (tm.am == NULL) {
137 			tm.am = str;
138 		} else if (tm.pm == NULL) {
139 			tm.pm = str;
140 		} else {
141 			fprintf(stderr,"too many list elements");
142 		}
143 		break;
144 	case T_ALT_DIGITS:
145 	case T_ERA:
146 		free(str);
147 		break;
148 	default:
149 		free(str);
150 		INTERR;
151 		break;
152 	}
153 }
154 
155 void
156 check_time_list(void)
157 {
158 	switch (last_kw) {
159 	case T_ABMON:
160 		if (tm.mon[11] != NULL)
161 			return;
162 		break;
163 	case T_MON:
164 		if (tm.month[11] != NULL)
165 			return;
166 		break;
167 	case T_ABDAY:
168 		if (tm.wday[6] != NULL)
169 			return;
170 		break;
171 	case T_DAY:
172 		if (tm.weekday[6] != NULL)
173 			return;
174 		break;
175 	case T_AM_PM:
176 		if (tm.pm != NULL)
177 			return;
178 		break;
179 	case T_ERA:
180 	case T_ALT_DIGITS:
181 		return;
182 	default:
183 		fprintf(stderr,"unknown list");
184 		break;
185 	}
186 
187 	fprintf(stderr,"too few items in list (%d)", last_kw);
188 }
189 
190 void
191 reset_time_list(void)
192 {
193 	int i;
194 	switch (last_kw) {
195 	case T_ABMON:
196 		for (i = 0; i < 12; i++) {
197 			free((char *)tm.mon[i]);
198 			tm.mon[i] = NULL;
199 		}
200 		break;
201 	case T_MON:
202 		for (i = 0; i < 12; i++) {
203 			free((char *)tm.month[i]);
204 			tm.month[i] = NULL;
205 		}
206 		break;
207 	case T_ABDAY:
208 		for (i = 0; i < 7; i++) {
209 			free((char *)tm.wday[i]);
210 			tm.wday[i] = NULL;
211 		}
212 		break;
213 	case T_DAY:
214 		for (i = 0; i < 7; i++) {
215 			free((char *)tm.weekday[i]);
216 			tm.weekday[i] = NULL;
217 		}
218 		break;
219 	case T_AM_PM:
220 		free((char *)tm.am);
221 		tm.am = NULL;
222 		free((char *)tm.pm);
223 		tm.pm = NULL;
224 		break;
225 	}
226 }
227 
228 void
229 dump_time(void)
230 {
231 	FILE *f;
232 	int i;
233 
234 	if ((f = open_category()) == NULL) {
235 		return;
236 	}
237 
238 	for (i = 0; i < 12; i++) {
239 		if (putl_category(tm.mon[i], f) == EOF) {
240 			return;
241 		}
242 	}
243 	for (i = 0; i < 12; i++) {
244 		if (putl_category(tm.month[i], f) == EOF) {
245 			return;
246 		}
247 	}
248 	for (i = 0; i < 7; i++) {
249 		if (putl_category(tm.wday[i], f) == EOF) {
250 			return;
251 		}
252 	}
253 	for (i = 0; i < 7; i++) {
254 		if (putl_category(tm.weekday[i], f) == EOF) {
255 			return;
256 		}
257 	}
258 
259 	/*
260 	 * NOTE: If date_fmt is not specified, then we'll default to
261 	 * using the %c for date.  This is reasonable for most
262 	 * locales, although for reasons that I don't understand
263 	 * Solaris historically has had a seperate format for date.
264 	 */
265 	if ((putl_category(tm.X_fmt, f) == EOF) ||
266 	    (putl_category(tm.x_fmt, f) == EOF) ||
267 	    (putl_category(tm.c_fmt, f) == EOF) ||
268 	    (putl_category(tm.am, f) == EOF) ||
269 	    (putl_category(tm.pm, f) == EOF) ||
270 	    (putl_category(tm.date_fmt ? tm.date_fmt : tm.c_fmt, f) == EOF) ||
271 	    (putl_category(tm.ampm_fmt, f) == EOF)) {
272 		return;
273 	}
274 	close_category(f);
275 }
276