Sun Dec 17 15:56:35 1995 Miles Bader <miles@gnu.ai.mit.edu>
[glibc.git] / locale / localedef.c
blob56ec1a2f6d930f380774d2f1d06eb5343df9a021
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 <errno.h>
28 #include "error.h"
30 #include "localedef.h"
32 /* The charmap file used. If none given DEFAULT_CHARMAP is used. */
33 static char *charmap_file;
35 /* If set output is always written, even when warning are given. */
36 static int force_output;
38 /* The input file name. */
39 static char *input_file;
41 /* Path leading to the destination directory for the produced files. */
42 char *output_path;
44 /* If this is defined be POSIX conform. */
45 int posix_conformance;
47 /* If not zero give a lot more messages. */
48 int verbose;
50 /* Long options. */
51 static const struct option long_options[] =
53 { "charmap", required_argument, NULL, 'f' },
54 { "debug", no_argument, NULL, 'd' },
55 { "help", no_argument, NULL, 'h' },
56 { "force", no_argument, NULL, 'c' },
57 { "inputfile", required_argument, NULL, 'i' },
58 { "posix", no_argument, &posix_conformance, 1 },
59 { "verbose", no_argument, &verbose, 1},
60 { "version", no_argument, NULL, 'V' },
61 { NULL, 0, NULL, 0 }
65 /* Prototypes for local functions. */
66 static void usage (int status) __attribute__ ((noreturn));
67 static int construct_output_path (const char *path);
69 int
70 main(int argc, char *argv[])
72 int optchar;
73 int cannot_write;
74 int do_help = 0;
75 int do_version = 0;
77 /* Set initial values for global varaibles. */
78 charmap_file = NULL;
79 force_output = 0;
80 input_file = 0;
81 posix_conformance = getenv ("POSIXLY_CORRECT") != NULL;
82 verbose = 0;
84 /* Set locale. Do not set LC_ALL because the other categories must
85 not be affected (acccording to POSIX.2). */
86 setlocale (LC_MESSAGES, "");
87 setlocale (LC_CTYPE, "");
89 /* Initialize the message catalog. */
90 textdomain (PACKAGE);
92 while ((optchar = getopt_long (argc, argv, "cdf:hi:vV", long_options, NULL))
93 != EOF)
94 switch (optchar)
96 case '\0':
97 break;
98 case 'c':
99 force_output = 1;
100 break;
101 case 'f':
102 if (charmap_file != NULL)
103 error (0, 0, gettext ("\"%s %s\" overwrites old option \"%s\""),
104 "-f", optarg, charmap_file);
105 charmap_file = optarg;
106 break;
107 case 'h':
108 do_help = 1;
109 break;
110 case 'i':
111 if (input_file != NULL)
112 error (0, 0, gettext ("\"%s %s\" overwrites old option \"%s\""),
113 "-i", optarg, input_file);
114 input_file = optarg;
115 break;
116 case 'v':
117 verbose = 1;
118 break;
119 case 'V':
120 do_version = 1;
121 break;
122 default:
123 usage (4);
124 break;
127 /* POSIX.2 requires to be verbose about missing characters in the
128 character map. */
129 verbose |= posix_conformance;
131 /* Version information is requested. */
132 if (do_version)
134 fprintf (stderr, "GNU %s %s\n", PACKAGE, VERSION);
135 exit (EXIT_SUCCESS);
138 /* Help is requested. */
139 if (do_help)
140 usage (0);
142 if (argc - optind != 1)
143 /* We need exactly one non-option parameter. */
144 usage (4);
146 /* The parameter describes the output path of the constructed files.
147 If the files cannot be written return a non-zero value. */
148 cannot_write = construct_output_path (argv[optind]);
150 /* Now that the parameters are processed we have to reset the local
151 ctype locale. (POSIX.2 4.35.5.2) */
152 setlocale (LC_CTYPE, "POSIX");
154 /* Look whether the system really allows locale definitions. */
155 if (sysconf (_SC_2_LOCALEDEF) < 0)
156 error (3, 0,
157 gettext ("warning: system does not define `_POSIX2_LOCALEDEF'"));
159 /* Process charmap file. */
160 charmap_read (charmap_file);
162 /* Now read the locale file. */
163 locfile_read (input_file);
165 /* Check all categories for consistency. */
166 categories_check ();
168 /* We are now able to write the data files. If warning were given we
169 do it only if it is explicitly requested (--force). */
170 if (error_message_count == 0 || force_output != 0)
171 if (cannot_write != 0)
172 error (0, 0, gettext ("cannot write output file `%s': %s"),
173 output_path, strerror (cannot_write));
174 else
175 categories_write ();
176 else
177 error (0, 0,
178 gettext ("no output file produced because warning were issued"));
180 exit (EXIT_SUCCESS);
184 /* Display usage information and exit. */
185 static void
186 usage(int status)
188 if (status != EXIT_SUCCESS)
189 fprintf (stderr, gettext ("Try `%s --help' for more information.\n"),
190 program_invocation_name);
191 else
192 printf(gettext ("\
193 Usage: %s [OPTION]... name\n\
194 Mandatory arguments to long options are mandatory for short options too.\n\
195 -c, --force create output even if warning messages have been issued\n\
196 -h, --help display this help and exit\n\
197 -V, --version output version information and exit\n\
199 -i, --inputfile=FILE source definitions are found in FILE\n\
200 -f, --charmap=FILE symbolic character names defined in FILE\n\
202 -v, --verbose print more messages\n\
203 --posix be strictly POSIX conform\n\
205 System's directory for character maps: %s\n\
206 locale files : %s\n\
207 "), program_invocation_name, CHARMAP_PATH, LOCALE_PATH);
209 exit (status);
213 /* The parameter to localedef describes the output path. If it does
214 contain a '/' character it is a relativ path. Otherwise it names the
215 locale this definition is for. */
216 static int
217 construct_output_path (const char *path)
219 int result = 0;
221 if (strchr (path, '/') == NULL)
223 /* This is a system path. */
224 int path_max_len = pathconf (LOCALE_PATH, _PC_PATH_MAX) + 1;
225 output_path = (char *) xmalloc (path_max_len);
227 snprintf (output_path, path_max_len, "%s/%s", LOCALE_PATH, path);
229 else
231 char *t;
232 /* This is a user path. */
233 output_path = malloc (strlen (path) + 2);
234 t = stpcpy (output_path, path);
235 *t = '\0';
238 if (euidaccess (output_path, W_OK) == -1)
239 /* Perhaps the directory does not exist now. Try to create it. */
240 if (errno == ENOENT)
242 if (mkdir (output_path, 0777) == -1)
243 result = errno;
245 else
246 result = errno;
248 if (result == 0)
249 strcat (output_path, "/");
251 return result;
255 * Local Variables:
256 * mode:c
257 * c-basic-offset:2
258 * End: