8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / localedef / localedef.c
blob64bd0c36d633ff2f027ca628607d2f8b4bc33ed3
1 /*
2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
5 * 1.0 of the CDDL.
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
13 * Copyright 2017 Nexenta Systems, Inc.
14 * Copyright 2013 DEY Storage Systems, Inc.
18 * POSIX localedef.
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <libgen.h>
29 #include <stddef.h>
30 #include <unistd.h>
31 #include <limits.h>
32 #include <locale.h>
33 #include <dirent.h>
34 #include "localedef.h"
35 #include "parser.tab.h"
37 #ifndef TEXT_DOMAIN
38 #define TEXT_DOMAIN "SYS_TEST"
39 #endif
41 int verbose = 0;
42 int undefok = 0;
43 int warnok = 0;
44 static char *locname = NULL;
45 static char locpath[PATH_MAX];
47 const char *
48 category_name(void)
50 switch (get_category()) {
51 case T_CHARMAP:
52 return ("CHARMAP");
53 case T_WIDTH:
54 return ("WIDTH");
55 case T_COLLATE:
56 return ("LC_COLLATE");
57 case T_CTYPE:
58 return ("LC_CTYPE");
59 case T_MESSAGES:
60 return ("LC_MESSAGES");
61 case T_MONETARY:
62 return ("LC_MONETARY");
63 case T_NUMERIC:
64 return ("LC_NUMERIC");
65 case T_TIME:
66 return ("LC_TIME");
67 default:
68 INTERR;
69 return (NULL);
73 static char *
74 category_file(void)
76 (void) snprintf(locpath, sizeof (locpath), "%s/%s/LCL_DATA",
77 locname, category_name());
78 return (locpath);
81 FILE *
82 open_category(void)
84 FILE *file;
86 if (verbose) {
87 (void) printf(_("Writing category %s: "), category_name());
88 (void) fflush(stdout);
91 /* make the parent directory */
92 (void) mkdirp(dirname(category_file()), 0755);
95 * note that we have to regenerate the file name, as dirname
96 * clobbered it.
98 file = fopen(category_file(), "w");
99 if (file == NULL) {
100 errf(strerror(errno));
101 return (NULL);
103 return (file);
106 void
107 delete_category(FILE *f)
109 (void) fclose(f);
110 (void) unlink(category_file());
113 void
114 close_category(FILE *f)
116 if (fchmod(fileno(f), 0644) < 0 ||
117 fclose(f) != 0) {
118 errf(strerror(errno));
119 delete_category(f);
121 if (verbose) {
122 (void) fprintf(stdout, _("done.\n"));
123 (void) fflush(stdout);
128 * This function is used when copying the category from another
129 * locale. Note that the copy is actually performed using a hard
130 * link for efficiency.
132 void
133 copy_category(char *src)
135 char srcpath[PATH_MAX];
136 int rv;
138 (void) snprintf(srcpath, sizeof (srcpath), "%s/%s/LCL_DATA",
139 src, category_name());
140 rv = access(srcpath, R_OK);
141 if ((rv != 0) && (strchr(srcpath, '/') == NULL)) {
142 /* Maybe we should try the system locale */
143 (void) snprintf(srcpath, sizeof (srcpath),
144 "/usr/lib/locale/%s/%s/LCL_DATA", src, category_name());
145 rv = access(srcpath, R_OK);
148 if (rv != 0) {
149 errf(_("source locale data unavailable"), src);
150 return;
153 if (verbose > 1) {
154 (void) printf(_("Copying category %s from %s: "),
155 category_name(), src);
156 (void) fflush(stdout);
159 /* make the parent directory */
160 (void) mkdirp(dirname(category_file()), 0755);
162 if (link(srcpath, category_file()) != 0) {
163 errf(_("unable to copy locale data: %s"), strerror(errno));
164 return;
166 if (verbose > 1) {
167 (void) printf(_("done.\n"));
172 putl_category(const char *s, FILE *f)
174 if (s && fputs(s, f) == EOF) {
175 errf(strerror(errno));
176 delete_category(f);
177 return (EOF);
179 if (fputc('\n', f) == EOF) {
180 errf(strerror(errno));
181 delete_category(f);
182 return (EOF);
184 return (0);
188 wr_category(void *buf, size_t sz, FILE *f)
190 if (!sz) {
191 return (0);
193 if (fwrite(buf, sz, 1, f) < 1) {
194 errf(strerror(errno));
195 return (EOF);
197 return (0);
200 int yyparse(void);
202 static void
203 usage(void)
205 (void) fprintf(stderr,
206 _("Usage: localedef [options] localename\n"));
207 (void) fprintf(stderr, ("[options] are:\n"));
208 (void) fprintf(stderr, (" -c : ignore warnings\n"));
209 (void) fprintf(stderr, (" -v : verbose output\n"));
210 (void) fprintf(stderr, (" -U : ignore undefined symbols\n"));
211 (void) fprintf(stderr, (" -f charmap : use given charmap file\n"));
212 (void) fprintf(stderr, (" -u encoding : assume encoding\n"));
213 (void) fprintf(stderr, (" -w widths : use screen widths file\n"));
214 (void) fprintf(stderr, (" -i locsrc : source file for locale\n"));
215 exit(4);
219 main(int argc, char **argv)
221 int c;
222 char *lfname = NULL;
223 char *cfname = NULL;
224 char *wfname = NULL;
225 DIR *dir;
227 init_charmap();
228 init_collate();
229 init_ctype();
230 init_messages();
231 init_monetary();
232 init_numeric();
233 init_time();
235 yydebug = 0;
237 (void) setlocale(LC_ALL, "");
238 (void) textdomain(TEXT_DOMAIN);
240 while ((c = getopt(argc, argv, "w:i:cf:u:vU")) != -1) {
241 switch (c) {
242 case 'v':
243 verbose++;
244 break;
245 case 'i':
246 lfname = optarg;
247 break;
248 case 'u':
249 set_wide_encoding(optarg);
250 break;
251 case 'f':
252 cfname = optarg;
253 break;
254 case 'U':
255 undefok++;
256 break;
257 case 'c':
258 warnok++;
259 break;
260 case 'w':
261 wfname = optarg;
262 break;
263 case '?':
264 usage();
265 break;
269 if ((argc - 1) != (optind)) {
270 usage();
272 locname = argv[argc - 1];
273 if (verbose) {
274 (void) printf(_("Processing locale %s.\n"), locname);
277 if (cfname) {
278 if (verbose)
279 (void) printf(_("Loading charmap %s.\n"), cfname);
280 reset_scanner(cfname);
281 (void) yyparse();
284 if (wfname) {
285 if (verbose)
286 (void) printf(_("Loading widths %s.\n"), wfname);
287 reset_scanner(wfname);
288 (void) yyparse();
291 if (verbose) {
292 (void) printf(_("Loading POSIX portable characters.\n"));
294 add_charmap_posix();
296 if (lfname) {
297 reset_scanner(lfname);
298 } else {
299 reset_scanner(NULL);
302 /* make the directory for the locale if not already present */
303 while ((dir = opendir(locname)) == NULL) {
304 if ((errno != ENOENT) ||
305 (mkdir(locname, 0755) < 0)) {
306 errf(strerror(errno));
309 (void) closedir(dir);
311 (void) mkdirp(dirname(category_file()), 0755);
313 (void) yyparse();
314 if (verbose) {
315 (void) printf(_("All done.\n"));
317 return (warnings ? 1 : 0);