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