00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _GNU_SOURCE
00023 # define _GNU_SOURCE 1
00024 #endif
00025
00026 #ifdef HAVE_CONFIG_H
00027 # include <config.h>
00028 #endif
00029
00030 #include <string.h>
00031 #if !HAVE_STRCHR && !defined _LIBC
00032 # ifndef strchr
00033 # define strchr index
00034 # endif
00035 #endif
00036
00037 #if defined _LIBC || defined HAVE_ARGZ_H
00038 # include <argz.h>
00039 #endif
00040 #include <ctype.h>
00041 #include <sys/types.h>
00042 #include <stdlib.h>
00043
00044 #include "loadinfo.h"
00045
00046
00047 #ifndef NULL
00048 # if defined __STDC__ && __STDC__
00049 # define NULL ((void *) 0)
00050 # else
00051 # define NULL 0
00052 # endif
00053 #endif
00054
00055
00056
00057 #ifdef _LIBC
00058
00059
00060
00061 # ifndef stpcpy
00062 # define stpcpy(dest, src) __stpcpy(dest, src)
00063 # endif
00064 #else
00065 # ifndef HAVE_STPCPY
00066 static char *stpcpy PARAMS ((char *dest, const char *src));
00067 # endif
00068 #endif
00069
00070
00071
00072 #if !defined _LIBC && !defined HAVE___ARGZ_COUNT
00073
00074 static size_t argz_count__ PARAMS ((const char *argz, size_t len));
00075
00076 static size_t
00077 argz_count__ (argz, len)
00078 const char *argz;
00079 size_t len;
00080 {
00081 size_t count = 0;
00082 while (len > 0)
00083 {
00084 size_t part_len = strlen (argz);
00085 argz += part_len + 1;
00086 len -= part_len + 1;
00087 count++;
00088 }
00089 return count;
00090 }
00091 # undef __argz_count
00092 # define __argz_count(argz, len) argz_count__ (argz, len)
00093 #endif
00094
00095 #if !defined _LIBC && !defined HAVE___ARGZ_STRINGIFY
00096
00097
00098 static void argz_stringify__ PARAMS ((char *argz, size_t len, int sep));
00099
00100 static void
00101 argz_stringify__ (argz, len, sep)
00102 char *argz;
00103 size_t len;
00104 int sep;
00105 {
00106 while (len > 0)
00107 {
00108 size_t part_len = strlen (argz);
00109 argz += part_len;
00110 len -= part_len + 1;
00111 if (len > 0)
00112 *argz++ = sep;
00113 }
00114 }
00115 # undef __argz_stringify
00116 # define __argz_stringify(argz, len, sep) argz_stringify__ (argz, len, sep)
00117 #endif
00118
00119 #if !defined _LIBC && !defined HAVE___ARGZ_NEXT
00120 static char *argz_next__ PARAMS ((char *argz, size_t argz_len,
00121 const char *entry));
00122
00123 static char *
00124 argz_next__ (argz, argz_len, entry)
00125 char *argz;
00126 size_t argz_len;
00127 const char *entry;
00128 {
00129 if (entry)
00130 {
00131 if (entry < argz + argz_len)
00132 entry = strchr (entry, '\0') + 1;
00133
00134 return entry >= argz + argz_len ? NULL : (char *) entry;
00135 }
00136 else
00137 if (argz_len > 0)
00138 return argz;
00139 else
00140 return 0;
00141 }
00142 # undef __argz_next
00143 # define __argz_next(argz, len, entry) argz_next__ (argz, len, entry)
00144 #endif
00145
00146
00147
00148 static int pop PARAMS ((int x));
00149
00150 static inline int
00151 pop (x)
00152 int x;
00153 {
00154
00155 x = ((x & ~0x5555) >> 1) + (x & 0x5555);
00156 x = ((x & ~0x3333) >> 2) + (x & 0x3333);
00157 x = ((x >> 4) + x) & 0x0f0f;
00158 x = ((x >> 8) + x) & 0xff;
00159
00160 return x;
00161 }
00162
00163
00164 struct loaded_l10nfile *
00165 _nl_make_l10nflist (l10nfile_list, dirlist, dirlist_len, mask, language,
00166 territory, codeset, normalized_codeset, modifier, special,
00167 sponsor, revision, filename, do_allocate)
00168 struct loaded_l10nfile **l10nfile_list;
00169 const char *dirlist;
00170 size_t dirlist_len;
00171 int mask;
00172 const char *language;
00173 const char *territory;
00174 const char *codeset;
00175 const char *normalized_codeset;
00176 const char *modifier;
00177 const char *special;
00178 const char *sponsor;
00179 const char *revision;
00180 const char *filename;
00181 int do_allocate;
00182 {
00183 char *abs_filename;
00184 struct loaded_l10nfile *last = NULL;
00185 struct loaded_l10nfile *retval;
00186 char *cp;
00187 size_t entries;
00188 int cnt;
00189
00190
00191 abs_filename = (char *) malloc (dirlist_len
00192 + strlen (language)
00193 + ((mask & TERRITORY) != 0
00194 ? strlen (territory) + 1 : 0)
00195 + ((mask & XPG_CODESET) != 0
00196 ? strlen (codeset) + 1 : 0)
00197 + ((mask & XPG_NORM_CODESET) != 0
00198 ? strlen (normalized_codeset) + 1 : 0)
00199 + (((mask & XPG_MODIFIER) != 0
00200 || (mask & CEN_AUDIENCE) != 0)
00201 ? strlen (modifier) + 1 : 0)
00202 + ((mask & CEN_SPECIAL) != 0
00203 ? strlen (special) + 1 : 0)
00204 + (((mask & CEN_SPONSOR) != 0
00205 || (mask & CEN_REVISION) != 0)
00206 ? (1 + ((mask & CEN_SPONSOR) != 0
00207 ? strlen (sponsor) + 1 : 0)
00208 + ((mask & CEN_REVISION) != 0
00209 ? strlen (revision) + 1 : 0)) : 0)
00210 + 1 + strlen (filename) + 1);
00211
00212 if (abs_filename == NULL)
00213 return NULL;
00214
00215 retval = NULL;
00216 last = NULL;
00217
00218
00219 memcpy (abs_filename, dirlist, dirlist_len);
00220 __argz_stringify (abs_filename, dirlist_len, PATH_SEPARATOR);
00221 cp = abs_filename + (dirlist_len - 1);
00222 *cp++ = '/';
00223 cp = stpcpy (cp, language);
00224
00225 if ((mask & TERRITORY) != 0)
00226 {
00227 *cp++ = '_';
00228 cp = stpcpy (cp, territory);
00229 }
00230 if ((mask & XPG_CODESET) != 0)
00231 {
00232 *cp++ = '.';
00233 cp = stpcpy (cp, codeset);
00234 }
00235 if ((mask & XPG_NORM_CODESET) != 0)
00236 {
00237 *cp++ = '.';
00238 cp = stpcpy (cp, normalized_codeset);
00239 }
00240 if ((mask & (XPG_MODIFIER | CEN_AUDIENCE)) != 0)
00241 {
00242
00243
00244 *cp++ = (mask & CEN_AUDIENCE) != 0 ? '+' : '@';
00245 cp = stpcpy (cp, modifier);
00246 }
00247 if ((mask & CEN_SPECIAL) != 0)
00248 {
00249 *cp++ = '+';
00250 cp = stpcpy (cp, special);
00251 }
00252 if ((mask & (CEN_SPONSOR | CEN_REVISION)) != 0)
00253 {
00254 *cp++ = ',';
00255 if ((mask & CEN_SPONSOR) != 0)
00256 cp = stpcpy (cp, sponsor);
00257 if ((mask & CEN_REVISION) != 0)
00258 {
00259 *cp++ = '_';
00260 cp = stpcpy (cp, revision);
00261 }
00262 }
00263
00264 *cp++ = '/';
00265 stpcpy (cp, filename);
00266
00267
00268
00269 last = NULL;
00270 for (retval = *l10nfile_list; retval != NULL; retval = retval->next)
00271 if (retval->filename != NULL)
00272 {
00273 int compare = strcmp (retval->filename, abs_filename);
00274 if (compare == 0)
00275
00276 break;
00277 if (compare < 0)
00278 {
00279
00280 retval = NULL;
00281 break;
00282 }
00283
00284 last = retval;
00285 }
00286
00287 if (retval != NULL || do_allocate == 0)
00288 {
00289 free (abs_filename);
00290 return retval;
00291 }
00292
00293 retval = (struct loaded_l10nfile *)
00294 malloc (sizeof (*retval) + (__argz_count (dirlist, dirlist_len)
00295 * (1 << pop (mask))
00296 * sizeof (struct loaded_l10nfile *)));
00297 if (retval == NULL)
00298 return NULL;
00299
00300 retval->filename = abs_filename;
00301 retval->decided = (__argz_count (dirlist, dirlist_len) != 1
00302 || ((mask & XPG_CODESET) != 0
00303 && (mask & XPG_NORM_CODESET) != 0));
00304 retval->data = NULL;
00305
00306 if (last == NULL)
00307 {
00308 retval->next = *l10nfile_list;
00309 *l10nfile_list = retval;
00310 }
00311 else
00312 {
00313 retval->next = last->next;
00314 last->next = retval;
00315 }
00316
00317 entries = 0;
00318
00319
00320
00321 cnt = __argz_count (dirlist, dirlist_len) == 1 ? mask - 1 : mask;
00322 for (; cnt >= 0; --cnt)
00323 if ((cnt & ~mask) == 0
00324 && ((cnt & CEN_SPECIFIC) == 0 || (cnt & XPG_SPECIFIC) == 0)
00325 && ((cnt & XPG_CODESET) == 0 || (cnt & XPG_NORM_CODESET) == 0))
00326 {
00327
00328 char *dir = NULL;
00329
00330 while ((dir = __argz_next ((char *) dirlist, dirlist_len, dir))
00331 != NULL)
00332 retval->successor[entries++]
00333 = _nl_make_l10nflist (l10nfile_list, dir, strlen (dir) + 1, cnt,
00334 language, territory, codeset,
00335 normalized_codeset, modifier, special,
00336 sponsor, revision, filename, 1);
00337 }
00338 retval->successor[entries] = NULL;
00339
00340 return retval;
00341 }
00342
00343
00344
00345
00346
00347 const char *
00348 _nl_normalize_codeset (codeset, name_len)
00349 const char *codeset;
00350 size_t name_len;
00351 {
00352 int len = 0;
00353 int only_digit = 1;
00354 char *retval;
00355 char *wp;
00356 size_t cnt;
00357
00358 for (cnt = 0; cnt < name_len; ++cnt)
00359 if (isalnum ((unsigned char)codeset[cnt]))
00360 {
00361 ++len;
00362
00363 if (isalpha ((unsigned char)codeset[cnt]))
00364 only_digit = 0;
00365 }
00366
00367 retval = (char *) malloc ((only_digit ? 3 : 0) + len + 1);
00368
00369 if (retval != NULL)
00370 {
00371 if (only_digit)
00372 wp = stpcpy (retval, "iso");
00373 else
00374 wp = retval;
00375
00376 for (cnt = 0; cnt < name_len; ++cnt)
00377 if (isalpha ((unsigned char)codeset[cnt]))
00378 *wp++ = tolower ((unsigned char)codeset[cnt]);
00379 else if (isdigit ((unsigned char)codeset[cnt]))
00380 *wp++ = codeset[cnt];
00381
00382 *wp = '\0';
00383 }
00384
00385 return (const char *) retval;
00386 }
00387
00388
00389
00390
00391
00392
00393
00394
00395 #if !_LIBC && !HAVE_STPCPY
00396 static char *
00397 stpcpy (dest, src)
00398 char *dest;
00399 const char *src;
00400 {
00401 while ((*dest++ = *src++) != '\0')
00402 ;
00403 return dest - 1;
00404 }
00405 #endif