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