Back to index

glibc  2.9
localedef.c
Go to the documentation of this file.
00001 /* Copyright (C) 1995-2007, 2008 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1995.
00004 
00005    This program is free software; you can redistribute it and/or modify
00006    it under the terms of the GNU General Public License as published
00007    by the Free Software Foundation; version 2 of the License, or
00008    (at your option) any later version.
00009 
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013    GNU General Public License for more details.
00014 
00015    You should have received a copy of the GNU General Public License
00016    along with this program; if not, write to the Free Software Foundation,
00017    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
00018 
00019 #ifdef HAVE_CONFIG_H
00020 # include <config.h>
00021 #endif
00022 
00023 #include <argp.h>
00024 #include <errno.h>
00025 #include <fcntl.h>
00026 #include <libintl.h>
00027 #include <locale.h>
00028 #include <mcheck.h>
00029 #include <stdbool.h>
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033 #include <unistd.h>
00034 #include <error.h>
00035 #include <sys/mman.h>
00036 #include <sys/stat.h>
00037 
00038 #include "localedef.h"
00039 #include "charmap.h"
00040 #include "locfile.h"
00041 
00042 /* Undefine the following line in the production version.  */
00043 /* #define NDEBUG 1 */
00044 #include <assert.h>
00045 
00046 
00047 /* List of copied locales.  */
00048 struct copy_def_list_t *copy_list;
00049 
00050 /* If this is defined be POSIX conform.  */
00051 int posix_conformance;
00052 
00053 /* If not zero give a lot more messages.  */
00054 int verbose;
00055 
00056 /* If not zero suppress warnings and information messages.  */
00057 int be_quiet;
00058 
00059 /* If not zero, produce old-style hash table instead of 3-level access
00060    tables.  */
00061 int oldstyle_tables;
00062 
00063 /* If not zero force output even if warning were issued.  */
00064 static int force_output;
00065 
00066 /* Prefix for output files.  */
00067 const char *output_prefix;
00068 
00069 /* Name of the character map file.  */
00070 static const char *charmap_file;
00071 
00072 /* Name of the locale definition file.  */
00073 static const char *input_file;
00074 
00075 /* Name of the repertoire map file.  */
00076 const char *repertoire_global;
00077 
00078 /* Name of the locale.alias file.  */
00079 const char *alias_file;
00080 
00081 /* List of all locales.  */
00082 static struct localedef_t *locales;
00083 
00084 /* If true don't add locale data to archive.  */
00085 bool no_archive;
00086 
00087 /* If true add named locales to archive.  */
00088 static bool add_to_archive;
00089 
00090 /* If true delete named locales from archive.  */
00091 static bool delete_from_archive;
00092 
00093 /* If true replace archive content when adding.  */
00094 static bool replace_archive;
00095 
00096 /* If true list archive content.  */
00097 static bool list_archive;
00098 
00099 /* Maximum number of retries when opening the locale archive.  */
00100 int max_locarchive_open_retry = 10;
00101 
00102 
00103 /* Name and version of program.  */
00104 static void print_version (FILE *stream, struct argp_state *state);
00105 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
00106 
00107 #define OPT_POSIX 301
00108 #define OPT_QUIET 302
00109 #define OPT_OLDSTYLE 303
00110 #define OPT_PREFIX 304
00111 #define OPT_NO_ARCHIVE 305
00112 #define OPT_ADD_TO_ARCHIVE 306
00113 #define OPT_REPLACE 307
00114 #define OPT_DELETE_FROM_ARCHIVE 308
00115 #define OPT_LIST_ARCHIVE 309
00116 
00117 /* Definitions of arguments for argp functions.  */
00118 static const struct argp_option options[] =
00119 {
00120   { NULL, 0, NULL, 0, N_("Input Files:") },
00121   { "charmap", 'f', "FILE", 0,
00122     N_("Symbolic character names defined in FILE") },
00123   { "inputfile", 'i', "FILE", 0, N_("Source definitions are found in FILE") },
00124   { "repertoire-map", 'u', "FILE", 0,
00125     N_("FILE contains mapping from symbolic names to UCS4 values") },
00126 
00127   { NULL, 0, NULL, 0, N_("Output control:") },
00128   { "force", 'c', NULL, 0,
00129     N_("Create output even if warning messages were issued") },
00130   { "old-style", OPT_OLDSTYLE, NULL, 0, N_("Create old-style tables") },
00131   { "prefix", OPT_PREFIX, "PATH", 0, N_("Optional output file prefix") },
00132   { "posix", OPT_POSIX, NULL, 0, N_("Be strictly POSIX conform") },
00133   { "quiet", OPT_QUIET, NULL, 0,
00134     N_("Suppress warnings and information messages") },
00135   { "verbose", 'v', NULL, 0, N_("Print more messages") },
00136   { NULL, 0, NULL, 0, N_("Archive control:") },
00137   { "no-archive", OPT_NO_ARCHIVE, NULL, 0,
00138     N_("Don't add new data to archive") },
00139   { "add-to-archive", OPT_ADD_TO_ARCHIVE, NULL, 0,
00140     N_("Add locales named by parameters to archive") },
00141   { "replace", OPT_REPLACE, NULL, 0, N_("Replace existing archive content") },
00142   { "delete-from-archive", OPT_DELETE_FROM_ARCHIVE, NULL, 0,
00143     N_("Remove locales named by parameters from archive") },
00144   { "list-archive", OPT_LIST_ARCHIVE, NULL, 0, N_("List content of archive") },
00145   { "alias-file", 'A', "FILE", 0,
00146     N_("locale.alias file to consult when making archive")},
00147   { NULL, 0, NULL, 0, NULL }
00148 };
00149 
00150 /* Short description of program.  */
00151 static const char doc[] = N_("Compile locale specification");
00152 
00153 /* Strings for arguments in help texts.  */
00154 static const char args_doc[] = N_("\
00155 NAME\n\
00156 [--add-to-archive|--delete-from-archive] FILE...\n\
00157 --list-archive [FILE]");
00158 
00159 /* Prototype for option handler.  */
00160 static error_t parse_opt (int key, char *arg, struct argp_state *state);
00161 
00162 /* Function to print some extra text in the help message.  */
00163 static char *more_help (int key, const char *text, void *input);
00164 
00165 /* Data structure to communicate with argp functions.  */
00166 static struct argp argp =
00167 {
00168   options, parse_opt, args_doc, doc, NULL, more_help
00169 };
00170 
00171 
00172 /* Prototypes for global functions.  */
00173 extern void *xmalloc (size_t __n);
00174 
00175 /* Prototypes for local functions.  */
00176 static void error_print (void);
00177 static const char *construct_output_path (char *path);
00178 static const char *normalize_codeset (const char *codeset, size_t name_len);
00179 
00180 
00181 int
00182 main (int argc, char *argv[])
00183 {
00184   const char *output_path;
00185   int cannot_write_why;
00186   struct charmap_t *charmap;
00187   struct localedef_t global;
00188   int remaining;
00189 
00190   /* Set initial values for global variables.  */
00191   copy_list = NULL;
00192   posix_conformance = getenv ("POSIXLY_CORRECT") != NULL;
00193   error_print_progname = error_print;
00194 
00195   /* Set locale.  Do not set LC_ALL because the other categories must
00196      not be affected (according to POSIX.2).  */
00197   setlocale (LC_MESSAGES, "");
00198   setlocale (LC_CTYPE, "");
00199 
00200   /* Initialize the message catalog.  */
00201   textdomain (_libc_intl_domainname);
00202 
00203   /* Parse and process arguments.  */
00204   argp_err_exit_status = 4;
00205   argp_parse (&argp, argc, argv, 0, &remaining, NULL);
00206 
00207   /* Handle a few special cases.  */
00208   if (list_archive)
00209     show_archive_content (verbose);
00210   if (add_to_archive)
00211     return add_locales_to_archive (argc - remaining, &argv[remaining],
00212                                replace_archive);
00213   if (delete_from_archive)
00214     return delete_locales_from_archive (argc - remaining, &argv[remaining]);
00215 
00216   /* POSIX.2 requires to be verbose about missing characters in the
00217      character map.  */
00218   verbose |= posix_conformance;
00219 
00220   if (argc - remaining != 1)
00221     {
00222       /* We need exactly one non-option parameter.  */
00223       argp_help (&argp, stdout, ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR,
00224                program_invocation_short_name);
00225       exit (4);
00226     }
00227 
00228   /* The parameter describes the output path of the constructed files.
00229      If the described files cannot be written return a NULL pointer.  */
00230   output_path  = construct_output_path (argv[remaining]);
00231   if (output_path == NULL && ! no_archive)
00232     error (4, errno, _("cannot create directory for output files"));
00233   cannot_write_why = errno;
00234 
00235   /* Now that the parameters are processed we have to reset the local
00236      ctype locale.  (P1003.2 4.35.5.2)  */
00237   setlocale (LC_CTYPE, "POSIX");
00238 
00239   /* Look whether the system really allows locale definitions.  POSIX
00240      defines error code 3 for this situation so I think it must be
00241      a fatal error (see P1003.2 4.35.8).  */
00242   if (sysconf (_SC_2_LOCALEDEF) < 0)
00243     WITH_CUR_LOCALE (error (3, 0, _("\
00244 FATAL: system does not define `_POSIX2_LOCALEDEF'")));
00245 
00246   /* Process charmap file.  */
00247   charmap = charmap_read (charmap_file, verbose, 1, be_quiet, 1);
00248 
00249   /* Add the first entry in the locale list.  */
00250   memset (&global, '\0', sizeof (struct localedef_t));
00251   global.name = input_file ?: "/dev/stdin";
00252   global.needed = ALL_LOCALES;
00253   locales = &global;
00254 
00255   /* Now read the locale file.  */
00256   if (locfile_read (&global, charmap) != 0)
00257     WITH_CUR_LOCALE (error (4, errno, _("\
00258 cannot open locale definition file `%s'"), input_file));
00259 
00260   /* Perhaps we saw some `copy' instructions.  */
00261   while (1)
00262     {
00263       struct localedef_t *runp = locales;
00264 
00265       while (runp != NULL && (runp->needed & runp->avail) == runp->needed)
00266        runp = runp->next;
00267 
00268       if (runp == NULL)
00269        /* Everything read.  */
00270        break;
00271 
00272       if (locfile_read (runp, charmap) != 0)
00273        WITH_CUR_LOCALE (error (4, errno, _("\
00274 cannot open locale definition file `%s'"), runp->name));
00275     }
00276 
00277   /* Check the categories we processed in source form.  */
00278   check_all_categories (locales, charmap);
00279 
00280   /* We are now able to write the data files.  If warning were given we
00281      do it only if it is explicitly requested (--force).  */
00282   if (error_message_count == 0 || force_output != 0)
00283     {
00284       if (cannot_write_why != 0)
00285        WITH_CUR_LOCALE (error (4, cannot_write_why, _("\
00286 cannot write output files to `%s'"), output_path));
00287       else
00288        write_all_categories (locales, charmap, argv[remaining], output_path);
00289     }
00290   else
00291     WITH_CUR_LOCALE (error (4, 0, _("\
00292 no output file produced because warnings were issued")));
00293 
00294   /* This exit status is prescribed by POSIX.2 4.35.7.  */
00295   exit (error_message_count != 0);
00296 }
00297 
00298 
00299 /* Handle program arguments.  */
00300 static error_t
00301 parse_opt (int key, char *arg, struct argp_state *state)
00302 {
00303   switch (key)
00304     {
00305     case OPT_QUIET:
00306       be_quiet = 1;
00307       break;
00308     case OPT_POSIX:
00309       posix_conformance = 1;
00310       break;
00311     case OPT_OLDSTYLE:
00312       oldstyle_tables = 1;
00313       break;
00314     case OPT_PREFIX:
00315       output_prefix = arg;
00316       break;
00317     case OPT_NO_ARCHIVE:
00318       no_archive = true;
00319       break;
00320     case OPT_ADD_TO_ARCHIVE:
00321       add_to_archive = true;
00322       break;
00323     case OPT_REPLACE:
00324       replace_archive = true;
00325       break;
00326     case OPT_DELETE_FROM_ARCHIVE:
00327       delete_from_archive = true;
00328       break;
00329     case OPT_LIST_ARCHIVE:
00330       list_archive = true;
00331       break;
00332     case 'c':
00333       force_output = 1;
00334       break;
00335     case 'f':
00336       charmap_file = arg;
00337       break;
00338     case 'A':
00339       alias_file = arg;
00340       break;
00341     case 'i':
00342       input_file = arg;
00343       break;
00344     case 'u':
00345       repertoire_global = arg;
00346       break;
00347     case 'v':
00348       verbose = 1;
00349       break;
00350     default:
00351       return ARGP_ERR_UNKNOWN;
00352     }
00353   return 0;
00354 }
00355 
00356 
00357 static char *
00358 more_help (int key, const char *text, void *input)
00359 {
00360   char *cp;
00361 
00362   switch (key)
00363     {
00364     case ARGP_KEY_HELP_EXTRA:
00365       /* We print some extra information.  */
00366       if (asprintf (&cp, gettext ("\
00367 System's directory for character maps : %s\n\
00368                        repertoire maps: %s\n\
00369                        locale path    : %s\n\
00370 %s"),
00371                   CHARMAP_PATH, REPERTOIREMAP_PATH, LOCALE_PATH, gettext ("\
00372 For bug reporting instructions, please see:\n\
00373 <http://www.gnu.org/software/libc/bugs.html>.\n")) < 0)
00374        return NULL;
00375       return cp;
00376     default:
00377       break;
00378     }
00379   return (char *) text;
00380 }
00381 
00382 /* Print the version information.  */
00383 static void
00384 print_version (FILE *stream, struct argp_state *state)
00385 {
00386   fprintf (stream, "localedef (GNU %s) %s\n", PACKAGE, VERSION);
00387   fprintf (stream, gettext ("\
00388 Copyright (C) %s Free Software Foundation, Inc.\n\
00389 This is free software; see the source for copying conditions.  There is NO\n\
00390 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
00391 "), "2008");
00392   fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
00393 }
00394 
00395 
00396 /* The address of this function will be assigned to the hook in the error
00397    functions.  */
00398 static void
00399 error_print (void)
00400 {
00401 }
00402 
00403 
00404 /* The parameter to localedef describes the output path.  If it does
00405    contain a '/' character it is a relative path.  Otherwise it names the
00406    locale this definition is for.  */
00407 static const char *
00408 construct_output_path (char *path)
00409 {
00410   const char *normal = NULL;
00411   char *result;
00412   char *endp;
00413 
00414   if (strchr (path, '/') == NULL)
00415     {
00416       /* This is a system path.  First examine whether the locale name
00417         contains a reference to the codeset.  This should be
00418         normalized.  */
00419       char *startp;
00420       size_t n;
00421 
00422       startp = path;
00423       /* We must be prepared for finding a CEN name or a location of
00424         the introducing `.' where it is not possible anymore.  */
00425       while (*startp != '\0' && *startp != '@' && *startp != '.')
00426        ++startp;
00427       if (*startp == '.')
00428        {
00429          /* We found a codeset specification.  Now find the end.  */
00430          endp = ++startp;
00431          while (*endp != '\0' && *endp != '@')
00432            ++endp;
00433 
00434          if (endp > startp)
00435            normal = normalize_codeset (startp, endp - startp);
00436        }
00437       else
00438        /* This is to keep gcc quiet.  */
00439        endp = NULL;
00440 
00441       /* We put an additional '\0' at the end of the string because at
00442         the end of the function we need another byte for the trailing
00443         '/'.  */
00444       if (normal == NULL)
00445        n = asprintf (&result, "%s%s/%s%c",
00446                     output_prefix ?: "", LOCALEDIR, path, '\0');
00447       else
00448        n = asprintf (&result, "%s%s/%.*s%s%s%c",
00449                     output_prefix ?: "", LOCALEDIR,
00450                     (int) (startp - path), path, normal, endp, '\0');
00451 
00452       if (n < 0)
00453        return NULL;
00454 
00455       endp = result + n - 1;
00456     }
00457   else
00458     {
00459       /* This is a user path.  Please note the additional byte in the
00460         memory allocation.  */
00461       size_t len = strlen (path) + 1;
00462       result = xmalloc (len + 1);
00463       endp = mempcpy (result, path, len) - 1;
00464 
00465       /* If the user specified an output path we cannot add the output
00466         to the archive.  */
00467       no_archive = true;
00468     }
00469 
00470   errno = 0;
00471 
00472   if (no_archive && euidaccess (result, W_OK) == -1)
00473     /* Perhaps the directory does not exist now.  Try to create it.  */
00474     if (errno == ENOENT)
00475       {
00476        errno = 0;
00477        if (mkdir (result, 0777) < 0)
00478          return NULL;
00479       }
00480 
00481   *endp++ = '/';
00482   *endp = '\0';
00483 
00484   return result;
00485 }
00486 
00487 
00488 /* Normalize codeset name.  There is no standard for the codeset
00489    names.  Normalization allows the user to use any of the common
00490    names.  */
00491 static const char *
00492 normalize_codeset (codeset, name_len)
00493      const char *codeset;
00494      size_t name_len;
00495 {
00496   int len = 0;
00497   int only_digit = 1;
00498   char *retval;
00499   char *wp;
00500   size_t cnt;
00501 
00502   for (cnt = 0; cnt < name_len; ++cnt)
00503     if (isalnum (codeset[cnt]))
00504       {
00505        ++len;
00506 
00507        if (isalpha (codeset[cnt]))
00508          only_digit = 0;
00509       }
00510 
00511   retval = (char *) malloc ((only_digit ? 3 : 0) + len + 1);
00512 
00513   if (retval != NULL)
00514     {
00515       if (only_digit)
00516        wp = stpcpy (retval, "iso");
00517       else
00518        wp = retval;
00519 
00520       for (cnt = 0; cnt < name_len; ++cnt)
00521        if (isalpha (codeset[cnt]))
00522          *wp++ = tolower (codeset[cnt]);
00523        else if (isdigit (codeset[cnt]))
00524          *wp++ = codeset[cnt];
00525 
00526       *wp = '\0';
00527     }
00528 
00529   return (const char *) retval;
00530 }
00531 
00532 
00533 struct localedef_t *
00534 add_to_readlist (int category, const char *name, const char *repertoire_name,
00535                int generate, struct localedef_t *copy_locale)
00536 {
00537   struct localedef_t *runp = locales;
00538 
00539   while (runp != NULL && strcmp (name, runp->name) != 0)
00540     runp = runp->next;
00541 
00542   if (runp == NULL)
00543     {
00544       /* Add a new entry at the end.  */
00545       struct localedef_t *newp;
00546 
00547       assert (generate == 1);
00548 
00549       newp = xcalloc (1, sizeof (struct localedef_t));
00550       newp->name = name;
00551       newp->repertoire_name = repertoire_name;
00552 
00553       if (locales == NULL)
00554        runp = locales = newp;
00555       else
00556        {
00557          runp = locales;
00558          while (runp->next != NULL)
00559            runp = runp->next;
00560          runp = runp->next = newp;
00561        }
00562     }
00563 
00564   if (generate
00565       && (runp->needed & (1 << category)) != 0
00566       && (runp->avail & (1 << category)) == 0)
00567     WITH_CUR_LOCALE (error (5, 0, _("\
00568 circular dependencies between locale definitions")));
00569 
00570   if (copy_locale != NULL)
00571     {
00572       if (runp->categories[category].generic != NULL)
00573        WITH_CUR_LOCALE (error (5, 0, _("\
00574 cannot add already read locale `%s' a second time"), name));
00575       else
00576        runp->categories[category].generic =
00577          copy_locale->categories[category].generic;
00578     }
00579 
00580   runp->needed |= 1 << category;
00581 
00582   return runp;
00583 }
00584 
00585 
00586 struct localedef_t *
00587 find_locale (int category, const char *name, const char *repertoire_name,
00588             const struct charmap_t *charmap)
00589 {
00590   struct localedef_t *result;
00591 
00592   /* Find the locale, but do not generate it since this would be a bug.  */
00593   result = add_to_readlist (category, name, repertoire_name, 0, NULL);
00594 
00595   assert (result != NULL);
00596 
00597   if ((result->avail & (1 << category)) == 0
00598       && locfile_read (result, charmap) != 0)
00599     WITH_CUR_LOCALE (error (4, errno, _("\
00600 cannot open locale definition file `%s'"), result->name));
00601 
00602   return result;
00603 }
00604 
00605 
00606 struct localedef_t *
00607 load_locale (int category, const char *name, const char *repertoire_name,
00608             const struct charmap_t *charmap, struct localedef_t *copy_locale)
00609 {
00610   struct localedef_t *result;
00611 
00612   /* Generate the locale if it does not exist.  */
00613   result = add_to_readlist (category, name, repertoire_name, 1, copy_locale);
00614 
00615   assert (result != NULL);
00616 
00617   if ((result->avail & (1 << category)) == 0
00618       && locfile_read (result, charmap) != 0)
00619     WITH_CUR_LOCALE (error (4, errno, _("\
00620 cannot open locale definition file `%s'"), result->name));
00621 
00622   return result;
00623 }
00624 
00625 static void
00626 turn_on_mcheck (void)
00627 {
00628   /* Enable `malloc' debugging.  */
00629   mcheck (NULL);
00630   /* Use the following line for a more thorough but much slower testing.  */
00631   /* mcheck_pedantic (NULL); */
00632 }
00633 
00634 void (*__malloc_initialize_hook) (void) = turn_on_mcheck;