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. */
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. */
44 /* If this is defined be POSIX conform. */
45 int posix_conformance
;
47 /* If not zero give a lot more messages. */
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' },
65 /* Prototypes for local functions. */
66 static void usage (int status
) __attribute__ ((noreturn
));
67 static int construct_output_path (const char *path
);
70 main(int argc
, char *argv
[])
77 /* Set initial values for global varaibles. */
81 posix_conformance
= getenv ("POSIXLY_CORRECT") != NULL
;
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. */
92 while ((optchar
= getopt_long (argc
, argv
, "cdf:hi:vV", long_options
, NULL
))
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
;
111 if (input_file
!= NULL
)
112 error (0, 0, gettext ("\"%s %s\" overwrites old option \"%s\""),
113 "-i", optarg
, input_file
);
127 /* POSIX.2 requires to be verbose about missing characters in the
129 verbose
|= posix_conformance
;
131 /* Version information is requested. */
134 fprintf (stderr
, "GNU %s %s\n", PACKAGE
, VERSION
);
138 /* Help is requested. */
142 if (argc
- optind
!= 1)
143 /* We need exactly one non-option parameter. */
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)
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. */
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
));
178 gettext ("no output file produced because warning were issued"));
184 /* Display usage information and exit. */
188 if (status
!= EXIT_SUCCESS
)
189 fprintf (stderr
, gettext ("Try `%s --help' for more information.\n"),
190 program_invocation_name
);
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\
207 "), program_invocation_name
, CHARMAP_PATH
, LOCALE_PATH
);
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. */
217 construct_output_path (const char *path
)
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
);
232 /* This is a user path. */
233 output_path
= malloc (strlen (path
) + 2);
234 t
= stpcpy (output_path
, path
);
238 if (euidaccess (output_path
, W_OK
) == -1)
239 /* Perhaps the directory does not exist now. Try to create it. */
242 if (mkdir (output_path
, 0777) == -1)
249 strcat (output_path
, "/");