(bind): Don't use file_invoke_translator, as it doesn't work. Instead just lookup...
[glibc.git] / locale / localedef.c
blobfceebc56357dc9b0e366248e33b135b0062c333f
1 /* Copyright (C) 1995 Free Software Foundation, Inc.
3 The GNU C Library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Library General Public License as
5 published by the Free Software Foundation; either version 2 of the
6 License, or (at your option) any later version.
8 The GNU C Library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public
14 License along with the GNU C Library; see the file COPYING.LIB. If
15 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
16 Cambridge, MA 02139, USA. */
18 #include <getopt.h>
19 #include <libintl.h>
20 #include <locale.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/stat.h>
27 #include "error.h"
29 #include "localedef.h"
31 /* The charmap file used. If none given DEFAULT_CHARMAP is used. */
32 static char *charmap_file;
34 /* If set output is always written, even when warning are given. */
35 static int force_output;
37 /* The input file name. */
38 static char *input_file;
40 /* Path leading to the destination directory for the produced files. */
41 char *output_path;
43 /* If this is defined be POSIX conform. */
44 int posix_conformance;
46 /* If not zero give a lot more messages. */
47 int verbose;
49 /* Long options. */
50 static const struct option long_options[] =
52 { "charmap", required_argument, NULL, 'f' },
53 { "debug", no_argument, NULL, 'd' },
54 { "help", no_argument, NULL, 'h' },
55 { "force", no_argument, NULL, 'c' },
56 { "inputfile", required_argument, NULL, 'i' },
57 { "posix", no_argument, &posix_conformance, 1 },
58 { "verbose", no_argument, &verbose, 1},
59 { "version", no_argument, NULL, 'V' },
60 { NULL, 0, NULL, 0 }
64 /* Prototypes for local functions. */
65 static void usage (int status) __attribute__ ((noreturn));
66 static int construct_output_path (const char *path);
68 int
69 main(int argc, char *argv[])
71 int optchar;
72 int cannot_write;
73 int do_help = 0;
74 int do_version = 0;
76 /* Set initial values for global varaibles. */
77 charmap_file = NULL;
78 force_output = 0;
79 input_file = 0;
80 posix_conformance = getenv ("POSIXLY_CORRECT") != NULL;
81 verbose = 0;
83 /* Set locale. Do not set LC_ALL because the other categories must
84 not be affected (acccording to POSIX.2). */
85 setlocale (LC_MESSAGES, "");
86 setlocale (LC_CTYPE, "");
88 /* Initialize the message catalog. */
89 textdomain (PACKAGE);
91 while ((optchar = getopt_long (argc, argv, "cdf:hi:vV", long_options, NULL))
92 != EOF)
93 switch (optchar)
95 case '\0':
96 break;
97 case 'c':
98 force_output = 1;
99 break;
100 case 'f':
101 if (charmap_file != NULL)
102 error (0, 0, gettext ("\"%s %s\" overwrites old option \"%s\""),
103 "-f", optarg, charmap_file);
104 charmap_file = optarg;
105 break;
106 case 'h':
107 do_help = 1;
108 break;
109 case 'i':
110 if (input_file != NULL)
111 error (0, 0, gettext ("\"%s %s\" overwrites old option \"%s\""),
112 "-i", optarg, input_file);
113 input_file = optarg;
114 break;
115 case 'v':
116 verbose = 1;
117 break;
118 case 'V':
119 do_version = 1;
120 break;
121 default:
122 usage (4);
123 break;
126 /* POSIX.2 requires to be verbose about missing characters in the
127 character map. */
128 verbose |= posix_conformance;
130 /* Version information is requested. */
131 if (do_version)
133 fprintf (stderr, "GNU %s %s\n", PACKAGE, VERSION);
134 exit (EXIT_SUCCESS);
137 /* Help is requested. */
138 if (do_help)
139 usage (0);
141 if (argc - optind != 1)
142 /* We need exactly one non-option parameter. */
143 usage (4);
145 /* The parameter describes the output path of the constructed files.
146 If the files cannot be written return a non-zero value. */
147 cannot_write = construct_output_path (argv[optind]);
149 /* Now that the parameters are processed we have to reset the local
150 ctype locale. (POSIX.2 4.35.5.2) */
151 setlocale (LC_CTYPE, "POSIX");
153 /* Look whether the system really allows locale definitions. */
154 if (sysconf (_SC_2_LOCALEDEF) < 0)
155 error (3, 0,
156 gettext ("warning: system does not define `_POSIX2_LOCALEDEF'"));
158 /* Process charmap file. */
159 charmap_read (charmap_file);
161 /* Now read the locale file. */
162 locfile_read (input_file);
164 /* Check all categories for consistency. */
165 categories_check ();
167 /* We are now able to write the data files. If warning were given we
168 do it only if it is explicitly requested (--force). */
169 if (error_message_count == 0 || force_output != 0)
170 if (cannot_write != 0)
171 error (0, 0, gettext ("cannot write output file `%s': %s"),
172 output_path, strerror (cannot_write));
173 else
174 categories_write ();
175 else
176 error (0, 0,
177 gettext ("no output file produced because warning were issued"));
179 exit (EXIT_SUCCESS);
183 /* Display usage information and exit. */
184 static void
185 usage(int status)
187 if (status != EXIT_SUCCESS)
188 fprintf (stderr, gettext ("Try `%s --help' for more information.\n"),
189 program_invocation_name);
190 else
191 printf(gettext ("\
192 Usage: %s [OPTION]... name\n\
193 Mandatory arguments to long options are mandatory for short options too.\n\
194 -c, --force create output even if warning messages have been issued\n\
195 -h, --help display this help and exit\n\
196 -V, --version output version information and exit\n\
198 -i, --inputfile=FILE source definitions are found in FILE\n\
199 -f, --charmap=FILE symbolic character names defined in FILE\n\
201 -v, --verbose print more messages\n\
202 --posix be strictly POSIX conform\n\
204 System's directory for character maps: %s\n\
205 locale files : %s\n\
206 "), program_invocation_name, CHARMAP_PATH, LOCALE_PATH);
208 exit (status);
212 /* The parameter to localedef describes the output path. If it does
213 contain a '/' character it is a relativ path. Otherwise it names the
214 locale this definition is for. */
215 static int
216 construct_output_path (const char *path)
218 int result = 0;
220 if (strchr (path, '/') == NULL)
222 /* This is a system path. */
223 int path_max_len = pathconf (LOCALE_PATH, _PC_PATH_MAX) + 1;
224 output_path = (char *) xmalloc (path_max_len);
226 snprintf (output_path, path_max_len, "%s/%s", LOCALE_PATH, path);
228 else
230 char *t;
231 /* This is a user path. */
232 output_path = malloc (strlen (path) + 2);
233 t = stpcpy (output_path, path);
234 *t = '\0';
237 if (euidaccess (output_path, W_OK) == -1)
238 /* Perhaps the directory does not exist now. Try to create it. */
239 if (errno == ENOENT)
241 if (mkdir (output_path, 0777) == -1)
242 result = errno;
244 else
245 result = errno;
247 if (result == 0)
248 strcat (output_path, "/");
250 return result;
254 * Local Variables:
255 * mode:c
256 * c-basic-offset:2
257 * End: