xref: /dragonfly/usr.bin/localedef/localedef.c (revision e0eb7cf0)
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 
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <errno.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <libgen.h>
43 #include <stddef.h>
44 #include <limits.h>
45 #include <locale.h>
46 #include <dirent.h>
47 #include "localedef.h"
48 #include "parser.h"
49 
50 #ifndef	TEXT_DOMAIN
51 #define	TEXT_DOMAIN	"SYS_TEST"
52 #endif
53 
54 static int bsd = 0;
55 int verbose = 0;
56 int undefok = 0;
57 int warnok = 0;
58 static char *locname = NULL;
59 static char locpath[PATH_MAX];
60 
61 const char *
62 category_name(void)
63 {
64 	switch (get_category()) {
65 	case T_CHARMAP:
66 		return ("CHARMAP");
67 	case T_WIDTH:
68 		return ("WIDTH");
69 	case T_COLLATE:
70 		return ("LC_COLLATE");
71 	case T_CTYPE:
72 		return ("LC_CTYPE");
73 	case T_MESSAGES:
74 		return ("LC_MESSAGES");
75 	case T_MONETARY:
76 		return ("LC_MONETARY");
77 	case T_NUMERIC:
78 		return ("LC_NUMERIC");
79 	case T_TIME:
80 		return ("LC_TIME");
81 	default:
82 		INTERR;
83 		return (NULL);
84 	}
85 }
86 
87 static char *
88 category_file(void)
89 {
90 	if (bsd)
91 		(void) snprintf(locpath, sizeof (locpath), "%s.%s",
92 		    locname, category_name());
93 	else
94 		(void) snprintf(locpath, sizeof (locpath), "%s/%s",
95 		    locname, category_name());
96 	return (locpath);
97 }
98 
99 FILE *
100 open_category(void)
101 {
102 	FILE *file;
103 
104 	if (verbose) {
105 		(void) printf("Writing category %s: ", category_name());
106 		(void) fflush(stdout);
107 	}
108 
109 	/* make the parent directory */
110 	if (!bsd)
111 		(void) mkdir(dirname(category_file()), 0755);
112 
113 	/*
114 	 * note that we have to regenerate the file name, as dirname
115 	 * clobbered it.
116 	 */
117 	file = fopen(category_file(), "w");
118 	if (file == NULL) {
119 		errf(strerror(errno));
120 		return (NULL);
121 	}
122 	return (file);
123 }
124 
125 void
126 close_category(FILE *f)
127 {
128 	if (fchmod(fileno(f), 0644) < 0) {
129 		(void) fclose(f);
130 		(void) unlink(category_file());
131 		errf(strerror(errno));
132 	}
133 	if (fclose(f) < 0) {
134 		(void) unlink(category_file());
135 		errf(strerror(errno));
136 	}
137 	if (verbose) {
138 		(void) fprintf(stdout, "done.\n");
139 		(void) fflush(stdout);
140 	}
141 }
142 
143 /*
144  * This function is used when copying the category from another
145  * locale.  Note that the copy is actually performed using a hard
146  * link for efficiency.
147  */
148 void
149 copy_category(char *src)
150 {
151 	char	srcpath[PATH_MAX];
152 	int	rv;
153 
154 	(void) snprintf(srcpath, sizeof (srcpath), "%s/%s",
155 	    src, category_name());
156 	rv = access(srcpath, R_OK);
157 	if ((rv != 0) && (strchr(srcpath, '/') == NULL)) {
158 		/* Maybe we should try the system locale */
159 		(void) snprintf(srcpath, sizeof (srcpath),
160 		    "/usr/lib/locale/%s/%s", src, category_name());
161 		rv = access(srcpath, R_OK);
162 	}
163 
164 	if (rv != 0) {
165 		fprintf(stderr,"source locale data unavailable: %s", src);
166 		return;
167 	}
168 
169 	if (verbose > 1) {
170 		(void) printf("Copying category %s from %s: ",
171 		    category_name(), src);
172 		(void) fflush(stdout);
173 	}
174 
175 	/* make the parent directory */
176 	if (!bsd)
177 		(void) mkdir(dirname(category_file()), 0755);
178 
179 	if (link(srcpath, category_file()) != 0) {
180 		fprintf(stderr,"unable to copy locale data: %s",
181 			strerror(errno));
182 		return;
183 	}
184 	if (verbose > 1) {
185 		(void) printf("done.\n");
186 	}
187 }
188 
189 int
190 putl_category(const char *s, FILE *f)
191 {
192 	if (s && fputs(s, f) == EOF) {
193 		(void) fclose(f);
194 		(void) unlink(category_file());
195 		errf(strerror(errno));
196 		return (EOF);
197 	}
198 	if (fputc('\n', f) == EOF) {
199 		(void) fclose(f);
200 		(void) unlink(category_file());
201 		errf(strerror(errno));
202 		return (EOF);
203 	}
204 	return (0);
205 }
206 
207 int
208 wr_category(void *buf, size_t sz, FILE *f)
209 {
210 	if (!sz) {
211 		return (0);
212 	}
213 	if (fwrite(buf, sz, 1, f) < 1) {
214 		(void) fclose(f);
215 		(void) unlink(category_file());
216 		errf(strerror(errno));
217 		return (EOF);
218 	}
219 	return (0);
220 }
221 
222 int yyparse(void);
223 
224 static void
225 usage(void)
226 {
227 	(void) fprintf(stderr, "Usage: localedef [options] localename\n");
228 	(void) fprintf(stderr, "[options] are:\n");
229 	(void) fprintf(stderr, "  -D          : BSD-style output\n");
230 	(void) fprintf(stderr, "  -c          : ignore warnings\n");
231 	(void) fprintf(stderr, "  -v          : verbose output\n");
232 	(void) fprintf(stderr, "  -U          : ignore undefined symbols\n");
233 	(void) fprintf(stderr, "  -f charmap  : use given charmap file\n");
234 	(void) fprintf(stderr, "  -u encoding : assume encoding\n");
235 	(void) fprintf(stderr, "  -w widths   : use screen widths file\n");
236 	(void) fprintf(stderr, "  -i locsrc   : source file for locale\n");
237 	exit(4);
238 }
239 
240 int
241 main(int argc, char **argv)
242 {
243 	int c;
244 	char *lfname = NULL;
245 	char *cfname = NULL;
246 	char *wfname = NULL;
247 	DIR *dir;
248 
249 	init_charmap();
250 	init_collate();
251 	init_ctype();
252 	init_messages();
253 	init_monetary();
254 	init_numeric();
255 	init_time();
256 
257 #if YYDEBUG
258 	yydebug = 0;
259 #endif
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(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