xref: /freebsd/usr.bin/localedef/localedef.c (revision c321779676d0722f5d0a65c30e8b23b8a6547364)
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  * POSIX 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 <sys/stat.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <libgen.h>
45 #include <stddef.h>
46 #include <unistd.h>
47 #include <limits.h>
48 #include <locale.h>
49 #include <dirent.h>
50 #include "localedef.h"
51 #include "parser.h"
52 
53 #ifndef	TEXT_DOMAIN
54 #define	TEXT_DOMAIN	"SYS_TEST"
55 #endif
56 
57 static int bsd = 0;
58 int verbose = 0;
59 int undefok = 0;
60 int warnok = 0;
61 static char *locname = NULL;
62 static char locpath[PATH_MAX];
63 
64 const char *
65 category_name(void)
66 {
67 	switch (get_category()) {
68 	case T_CHARMAP:
69 		return ("CHARMAP");
70 	case T_WIDTH:
71 		return ("WIDTH");
72 	case T_COLLATE:
73 		return ("LC_COLLATE");
74 	case T_CTYPE:
75 		return ("LC_CTYPE");
76 	case T_MESSAGES:
77 		return ("LC_MESSAGES");
78 	case T_MONETARY:
79 		return ("LC_MONETARY");
80 	case T_NUMERIC:
81 		return ("LC_NUMERIC");
82 	case T_TIME:
83 		return ("LC_TIME");
84 	default:
85 		INTERR;
86 		return (NULL);
87 	}
88 }
89 
90 static char *
91 category_file(void)
92 {
93 	if (bsd)
94 		(void) snprintf(locpath, sizeof (locpath), "%s.%s",
95 		    locname, category_name());
96 	else
97 		(void) snprintf(locpath, sizeof (locpath), "%s/%s",
98 		    locname, category_name());
99 	return (locpath);
100 }
101 
102 FILE *
103 open_category(void)
104 {
105 	FILE *file;
106 
107 	if (verbose) {
108 		(void) printf("Writing category %s: ", category_name());
109 		(void) fflush(stdout);
110 	}
111 
112 	/* make the parent directory */
113 	if (!bsd)
114 		(void) mkdir(dirname(category_file()), 0755);
115 
116 	/*
117 	 * note that we have to regenerate the file name, as dirname
118 	 * clobbered it.
119 	 */
120 	file = fopen(category_file(), "w");
121 	if (file == NULL) {
122 		errf(strerror(errno));
123 		return (NULL);
124 	}
125 	return (file);
126 }
127 
128 void
129 close_category(FILE *f)
130 {
131 	if (fchmod(fileno(f), 0644) < 0) {
132 		(void) fclose(f);
133 		(void) unlink(category_file());
134 		errf(strerror(errno));
135 	}
136 	if (fclose(f) < 0) {
137 		(void) unlink(category_file());
138 		errf(strerror(errno));
139 	}
140 	if (verbose) {
141 		(void) fprintf(stdout, "done.\n");
142 		(void) fflush(stdout);
143 	}
144 }
145 
146 /*
147  * This function is used when copying the category from another
148  * locale.  Note that the copy is actually performed using a hard
149  * link for efficiency.
150  */
151 void
152 copy_category(char *src)
153 {
154 	char	srcpath[PATH_MAX];
155 	int	rv;
156 
157 	(void) snprintf(srcpath, sizeof (srcpath), "%s/%s",
158 	    src, category_name());
159 	rv = access(srcpath, R_OK);
160 	if ((rv != 0) && (strchr(srcpath, '/') == NULL)) {
161 		/* Maybe we should try the system locale */
162 		(void) snprintf(srcpath, sizeof (srcpath),
163 		    "/usr/lib/locale/%s/%s", src, category_name());
164 		rv = access(srcpath, R_OK);
165 	}
166 
167 	if (rv != 0) {
168 		fprintf(stderr,"source locale data unavailable: %s", src);
169 		return;
170 	}
171 
172 	if (verbose > 1) {
173 		(void) printf("Copying category %s from %s: ",
174 		    category_name(), src);
175 		(void) fflush(stdout);
176 	}
177 
178 	/* make the parent directory */
179 	if (!bsd)
180 		(void) mkdir(dirname(category_file()), 0755);
181 
182 	if (link(srcpath, category_file()) != 0) {
183 		fprintf(stderr,"unable to copy locale data: %s",
184 			strerror(errno));
185 		return;
186 	}
187 	if (verbose > 1) {
188 		(void) printf("done.\n");
189 	}
190 }
191 
192 int
193 putl_category(const char *s, FILE *f)
194 {
195 	if (s && fputs(s, f) == EOF) {
196 		(void) fclose(f);
197 		(void) unlink(category_file());
198 		errf(strerror(errno));
199 		return (EOF);
200 	}
201 	if (fputc('\n', f) == EOF) {
202 		(void) fclose(f);
203 		(void) unlink(category_file());
204 		errf(strerror(errno));
205 		return (EOF);
206 	}
207 	return (0);
208 }
209 
210 int
211 wr_category(void *buf, size_t sz, FILE *f)
212 {
213 	if (!sz) {
214 		return (0);
215 	}
216 	if (fwrite(buf, sz, 1, f) < 1) {
217 		(void) fclose(f);
218 		(void) unlink(category_file());
219 		errf(strerror(errno));
220 		return (EOF);
221 	}
222 	return (0);
223 }
224 
225 int yyparse(void);
226 
227 static void
228 usage(void)
229 {
230 	(void) fprintf(stderr, "Usage: localedef [options] localename\n");
231 	(void) fprintf(stderr, "[options] are:\n");
232 	(void) fprintf(stderr, "  -D          : BSD-style output\n");
233 	(void) fprintf(stderr, "  -c          : ignore warnings\n");
234 	(void) fprintf(stderr, "  -v          : verbose output\n");
235 	(void) fprintf(stderr, "  -U          : ignore undefined symbols\n");
236 	(void) fprintf(stderr, "  -f charmap  : use given charmap file\n");
237 	(void) fprintf(stderr, "  -u encoding : assume encoding\n");
238 	(void) fprintf(stderr, "  -w widths   : use screen widths file\n");
239 	(void) fprintf(stderr, "  -i locsrc   : source file for locale\n");
240 	exit(4);
241 }
242 
243 int
244 main(int argc, char **argv)
245 {
246 	int c;
247 	char *lfname = NULL;
248 	char *cfname = NULL;
249 	char *wfname = NULL;
250 	DIR *dir;
251 
252 	init_charmap();
253 	init_collate();
254 	init_ctype();
255 	init_messages();
256 	init_monetary();
257 	init_numeric();
258 	init_time();
259 
260 	yydebug = 0;
261 
262 	(void) setlocale(LC_ALL, "");
263 
264 	while ((c = getopt(argc, argv, "w:i:cf:u:vUD")) != -1) {
265 		switch (c) {
266 		case 'D':
267 			bsd = 1;
268 			break;
269 		case 'v':
270 			verbose++;
271 			break;
272 		case 'i':
273 			lfname = optarg;
274 			break;
275 		case 'u':
276 			set_wide_encoding(optarg);
277 			break;
278 		case 'f':
279 			cfname = optarg;
280 			break;
281 		case 'U':
282 			undefok++;
283 			break;
284 		case 'c':
285 			warnok++;
286 			break;
287 		case 'w':
288 			wfname = optarg;
289 			break;
290 		case '?':
291 			usage();
292 			break;
293 		}
294 	}
295 
296 	if ((argc - 1) != (optind)) {
297 		usage();
298 	}
299 	locname = argv[argc - 1];
300 	if (verbose) {
301 		(void) printf("Processing locale %s.\n", locname);
302 	}
303 
304 	if (cfname) {
305 		if (verbose)
306 			(void) printf("Loading charmap %s.\n", cfname);
307 		reset_scanner(cfname);
308 		(void) yyparse();
309 	}
310 
311 	if (wfname) {
312 		if (verbose)
313 			(void) printf("Loading widths %s.\n", wfname);
314 		reset_scanner(wfname);
315 		(void) yyparse();
316 	}
317 
318 	if (verbose) {
319 		(void) printf("Loading POSIX portable characters.\n");
320 	}
321 	add_charmap_posix();
322 
323 	if (lfname) {
324 		reset_scanner(lfname);
325 	} else {
326 		reset_scanner(NULL);
327 	}
328 
329 	/* make the directory for the locale if not already present */
330 	if (!bsd) {
331 		while ((dir = opendir(locname)) == NULL) {
332 			if ((errno != ENOENT) ||
333 			    (mkdir(locname, 0755) <  0)) {
334 				errf(strerror(errno));
335 			}
336 		}
337 		(void) closedir(dir);
338 		(void) mkdir(dirname(category_file()), 0755);
339 	}
340 
341 	(void) yyparse();
342 	if (verbose) {
343 		(void) printf("All done.\n");
344 	}
345 	return (warnings ? 1 : 0);
346 }
347