l10nflist.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /* Copyright (C) 1995-1999, 2000, 2001, 2002 Free Software Foundation, Inc.
  2. Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU Library General Public License as published
  5. by the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  14. USA. */
  15. /* Tell glibc's <string.h> to provide a prototype for stpcpy().
  16. This must come before <config.h> because <config.h> may include
  17. <features.h>, and once <features.h> has been included, it's too late. */
  18. #ifndef _GNU_SOURCE
  19. # define _GNU_SOURCE 1
  20. #endif
  21. #ifdef HAVE_CONFIG_H
  22. # include <config.h>
  23. #endif
  24. #include <string.h>
  25. #if defined _LIBC || defined HAVE_ARGZ_H
  26. # include <argz.h>
  27. #endif
  28. #include <ctype.h>
  29. #include <sys/types.h>
  30. #include <stdlib.h>
  31. #include "loadinfo.h"
  32. /* On some strange systems still no definition of NULL is found. Sigh! */
  33. #ifndef NULL
  34. # if defined __STDC__ && __STDC__
  35. # define NULL ((void *) 0)
  36. # else
  37. # define NULL 0
  38. # endif
  39. #endif
  40. /* @@ end of prolog @@ */
  41. #ifdef _LIBC
  42. /* Rename the non ANSI C functions. This is required by the standard
  43. because some ANSI C functions will require linking with this object
  44. file and the name space must not be polluted. */
  45. # ifndef stpcpy
  46. # define stpcpy(dest, src) __stpcpy(dest, src)
  47. # endif
  48. #else
  49. # ifndef HAVE_STPCPY
  50. static char *stpcpy PARAMS ((char *dest, const char *src));
  51. # endif
  52. #endif
  53. /* Pathname support.
  54. ISSLASH(C) tests whether C is a directory separator character.
  55. IS_ABSOLUTE_PATH(P) tests whether P is an absolute path. If it is not,
  56. it may be concatenated to a directory pathname.
  57. */
  58. #if defined _WIN32 || defined __WIN32__ || defined __EMX__ || defined __DJGPP__
  59. /* Win32, OS/2, DOS */
  60. # define ISSLASH(C) ((C) == '/' || (C) == '\\')
  61. # define HAS_DEVICE(P) \
  62. ((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) \
  63. && (P)[1] == ':')
  64. # define IS_ABSOLUTE_PATH(P) (ISSLASH ((P)[0]) || HAS_DEVICE (P))
  65. #else
  66. /* Unix */
  67. # define ISSLASH(C) ((C) == '/')
  68. # define IS_ABSOLUTE_PATH(P) ISSLASH ((P)[0])
  69. #endif
  70. /* Define function which are usually not available. */
  71. #if !defined _LIBC && !defined HAVE___ARGZ_COUNT
  72. /* Returns the number of strings in ARGZ. */
  73. static size_t argz_count__ PARAMS ((const char *argz, size_t len));
  74. static size_t
  75. argz_count__ (argz, len)
  76. const char *argz;
  77. size_t len;
  78. {
  79. size_t count = 0;
  80. while (len > 0)
  81. {
  82. size_t part_len = strlen (argz);
  83. argz += part_len + 1;
  84. len -= part_len + 1;
  85. count++;
  86. }
  87. return count;
  88. }
  89. # undef __argz_count
  90. # define __argz_count(argz, len) argz_count__ (argz, len)
  91. #else
  92. # ifdef _LIBC
  93. # define __argz_count(argz, len) INTUSE(__argz_count) (argz, len)
  94. # endif
  95. #endif /* !_LIBC && !HAVE___ARGZ_COUNT */
  96. #if !defined _LIBC && !defined HAVE___ARGZ_STRINGIFY
  97. /* Make '\0' separated arg vector ARGZ printable by converting all the '\0's
  98. except the last into the character SEP. */
  99. static void argz_stringify__ PARAMS ((char *argz, size_t len, int sep));
  100. static void
  101. argz_stringify__ (argz, len, sep)
  102. char *argz;
  103. size_t len;
  104. int sep;
  105. {
  106. while (len > 0)
  107. {
  108. size_t part_len = strlen (argz);
  109. argz += part_len;
  110. len -= part_len + 1;
  111. if (len > 0)
  112. *argz++ = sep;
  113. }
  114. }
  115. # undef __argz_stringify
  116. # define __argz_stringify(argz, len, sep) argz_stringify__ (argz, len, sep)
  117. #else
  118. # ifdef _LIBC
  119. # define __argz_stringify(argz, len, sep) \
  120. INTUSE(__argz_stringify) (argz, len, sep)
  121. # endif
  122. #endif /* !_LIBC && !HAVE___ARGZ_STRINGIFY */
  123. #if !defined _LIBC && !defined HAVE___ARGZ_NEXT
  124. static char *argz_next__ PARAMS ((char *argz, size_t argz_len,
  125. const char *entry));
  126. static char *
  127. argz_next__ (argz, argz_len, entry)
  128. char *argz;
  129. size_t argz_len;
  130. const char *entry;
  131. {
  132. if (entry)
  133. {
  134. if (entry < argz + argz_len)
  135. entry = strchr (entry, '\0') + 1;
  136. return entry >= argz + argz_len ? NULL : (char *) entry;
  137. }
  138. else
  139. if (argz_len > 0)
  140. return argz;
  141. else
  142. return 0;
  143. }
  144. # undef __argz_next
  145. # define __argz_next(argz, len, entry) argz_next__ (argz, len, entry)
  146. #endif /* !_LIBC && !HAVE___ARGZ_NEXT */
  147. /* Return number of bits set in X. */
  148. static int pop PARAMS ((int x));
  149. static inline int
  150. pop (x)
  151. int x;
  152. {
  153. /* We assume that no more than 16 bits are used. */
  154. x = ((x & ~0x5555) >> 1) + (x & 0x5555);
  155. x = ((x & ~0x3333) >> 2) + (x & 0x3333);
  156. x = ((x >> 4) + x) & 0x0f0f;
  157. x = ((x >> 8) + x) & 0xff;
  158. return x;
  159. }
  160. struct loaded_l10nfile *
  161. _nl_make_l10nflist (l10nfile_list, dirlist, dirlist_len, mask, language,
  162. territory, codeset, normalized_codeset, modifier, special,
  163. sponsor, revision, filename, do_allocate)
  164. struct loaded_l10nfile **l10nfile_list;
  165. const char *dirlist;
  166. size_t dirlist_len;
  167. int mask;
  168. const char *language;
  169. const char *territory;
  170. const char *codeset;
  171. const char *normalized_codeset;
  172. const char *modifier;
  173. const char *special;
  174. const char *sponsor;
  175. const char *revision;
  176. const char *filename;
  177. int do_allocate;
  178. {
  179. char *abs_filename;
  180. struct loaded_l10nfile **lastp;
  181. struct loaded_l10nfile *retval;
  182. char *cp;
  183. size_t dirlist_count;
  184. size_t entries;
  185. int cnt;
  186. /* If LANGUAGE contains an absolute directory specification, we ignore
  187. DIRLIST. */
  188. if (IS_ABSOLUTE_PATH (language))
  189. dirlist_len = 0;
  190. /* Allocate room for the full file name. */
  191. abs_filename = (char *) malloc (dirlist_len
  192. + strlen (language)
  193. + ((mask & TERRITORY) != 0
  194. ? strlen (territory) + 1 : 0)
  195. + ((mask & XPG_CODESET) != 0
  196. ? strlen (codeset) + 1 : 0)
  197. + ((mask & XPG_NORM_CODESET) != 0
  198. ? strlen (normalized_codeset) + 1 : 0)
  199. + (((mask & XPG_MODIFIER) != 0
  200. || (mask & CEN_AUDIENCE) != 0)
  201. ? strlen (modifier) + 1 : 0)
  202. + ((mask & CEN_SPECIAL) != 0
  203. ? strlen (special) + 1 : 0)
  204. + (((mask & CEN_SPONSOR) != 0
  205. || (mask & CEN_REVISION) != 0)
  206. ? (1 + ((mask & CEN_SPONSOR) != 0
  207. ? strlen (sponsor) : 0)
  208. + ((mask & CEN_REVISION) != 0
  209. ? strlen (revision) + 1 : 0)) : 0)
  210. + 1 + strlen (filename) + 1);
  211. if (abs_filename == NULL)
  212. return NULL;
  213. /* Construct file name. */
  214. cp = abs_filename;
  215. if (dirlist_len > 0)
  216. {
  217. memcpy (cp, dirlist, dirlist_len);
  218. __argz_stringify (cp, dirlist_len, PATH_SEPARATOR);
  219. cp += dirlist_len;
  220. cp[-1] = '/';
  221. }
  222. cp = stpcpy (cp, language);
  223. if ((mask & TERRITORY) != 0)
  224. {
  225. *cp++ = '_';
  226. cp = stpcpy (cp, territory);
  227. }
  228. if ((mask & XPG_CODESET) != 0)
  229. {
  230. *cp++ = '.';
  231. cp = stpcpy (cp, codeset);
  232. }
  233. if ((mask & XPG_NORM_CODESET) != 0)
  234. {
  235. *cp++ = '.';
  236. cp = stpcpy (cp, normalized_codeset);
  237. }
  238. if ((mask & (XPG_MODIFIER | CEN_AUDIENCE)) != 0)
  239. {
  240. /* This component can be part of both syntaces but has different
  241. leading characters. For CEN we use `+', else `@'. */
  242. *cp++ = (mask & CEN_AUDIENCE) != 0 ? '+' : '@';
  243. cp = stpcpy (cp, modifier);
  244. }
  245. if ((mask & CEN_SPECIAL) != 0)
  246. {
  247. *cp++ = '+';
  248. cp = stpcpy (cp, special);
  249. }
  250. if ((mask & (CEN_SPONSOR | CEN_REVISION)) != 0)
  251. {
  252. *cp++ = ',';
  253. if ((mask & CEN_SPONSOR) != 0)
  254. cp = stpcpy (cp, sponsor);
  255. if ((mask & CEN_REVISION) != 0)
  256. {
  257. *cp++ = '_';
  258. cp = stpcpy (cp, revision);
  259. }
  260. }
  261. *cp++ = '/';
  262. stpcpy (cp, filename);
  263. /* Look in list of already loaded domains whether it is already
  264. available. */
  265. lastp = l10nfile_list;
  266. for (retval = *l10nfile_list; retval != NULL; retval = retval->next)
  267. if (retval->filename != NULL)
  268. {
  269. int compare = strcmp (retval->filename, abs_filename);
  270. if (compare == 0)
  271. /* We found it! */
  272. break;
  273. if (compare < 0)
  274. {
  275. /* It's not in the list. */
  276. retval = NULL;
  277. break;
  278. }
  279. lastp = &retval->next;
  280. }
  281. if (retval != NULL || do_allocate == 0)
  282. {
  283. free (abs_filename);
  284. return retval;
  285. }
  286. dirlist_count = (dirlist_len > 0 ? __argz_count (dirlist, dirlist_len) : 1);
  287. /* Allocate a new loaded_l10nfile. */
  288. retval =
  289. (struct loaded_l10nfile *)
  290. malloc (sizeof (*retval)
  291. + (((dirlist_count << pop (mask)) + (dirlist_count > 1 ? 1 : 0))
  292. * sizeof (struct loaded_l10nfile *)));
  293. if (retval == NULL)
  294. return NULL;
  295. retval->filename = abs_filename;
  296. /* We set retval->data to NULL here; it is filled in later.
  297. Setting retval->decided to 1 here means that retval does not
  298. correspond to a real file (dirlist_count > 1) or is not worth
  299. looking up (if an unnormalized codeset was specified). */
  300. retval->decided = (dirlist_count > 1
  301. || ((mask & XPG_CODESET) != 0
  302. && (mask & XPG_NORM_CODESET) != 0));
  303. retval->data = NULL;
  304. retval->next = *lastp;
  305. *lastp = retval;
  306. entries = 0;
  307. /* Recurse to fill the inheritance list of RETVAL.
  308. If the DIRLIST is a real list (i.e. DIRLIST_COUNT > 1), the RETVAL
  309. entry does not correspond to a real file; retval->filename contains
  310. colons. In this case we loop across all elements of DIRLIST and
  311. across all bit patterns dominated by MASK.
  312. If the DIRLIST is a single directory or entirely redundant (i.e.
  313. DIRLIST_COUNT == 1), we loop across all bit patterns dominated by
  314. MASK, excluding MASK itself.
  315. In either case, we loop down from MASK to 0. This has the effect
  316. that the extra bits in the locale name are dropped in this order:
  317. first the modifier, then the territory, then the codeset, then the
  318. normalized_codeset. */
  319. for (cnt = dirlist_count > 1 ? mask : mask - 1; cnt >= 0; --cnt)
  320. if ((cnt & ~mask) == 0
  321. && ((cnt & CEN_SPECIFIC) == 0 || (cnt & XPG_SPECIFIC) == 0)
  322. && ((cnt & XPG_CODESET) == 0 || (cnt & XPG_NORM_CODESET) == 0))
  323. {
  324. if (dirlist_count > 1)
  325. {
  326. /* Iterate over all elements of the DIRLIST. */
  327. char *dir = NULL;
  328. while ((dir = __argz_next ((char *) dirlist, dirlist_len, dir))
  329. != NULL)
  330. retval->successor[entries++]
  331. = _nl_make_l10nflist (l10nfile_list, dir, strlen (dir) + 1,
  332. cnt, language, territory, codeset,
  333. normalized_codeset, modifier, special,
  334. sponsor, revision, filename, 1);
  335. }
  336. else
  337. retval->successor[entries++]
  338. = _nl_make_l10nflist (l10nfile_list, dirlist, dirlist_len,
  339. cnt, language, territory, codeset,
  340. normalized_codeset, modifier, special,
  341. sponsor, revision, filename, 1);
  342. }
  343. retval->successor[entries] = NULL;
  344. return retval;
  345. }
  346. /* Normalize codeset name. There is no standard for the codeset
  347. names. Normalization allows the user to use any of the common
  348. names. The return value is dynamically allocated and has to be
  349. freed by the caller. */
  350. const char *
  351. _nl_normalize_codeset (codeset, name_len)
  352. const char *codeset;
  353. size_t name_len;
  354. {
  355. int len = 0;
  356. int only_digit = 1;
  357. char *retval;
  358. char *wp;
  359. size_t cnt;
  360. for (cnt = 0; cnt < name_len; ++cnt)
  361. if (isalnum ((unsigned char) codeset[cnt]))
  362. {
  363. ++len;
  364. if (isalpha ((unsigned char) codeset[cnt]))
  365. only_digit = 0;
  366. }
  367. retval = (char *) malloc ((only_digit ? 3 : 0) + len + 1);
  368. if (retval != NULL)
  369. {
  370. if (only_digit)
  371. wp = stpcpy (retval, "iso");
  372. else
  373. wp = retval;
  374. for (cnt = 0; cnt < name_len; ++cnt)
  375. if (isalpha ((unsigned char) codeset[cnt]))
  376. *wp++ = tolower ((unsigned char) codeset[cnt]);
  377. else if (isdigit ((unsigned char) codeset[cnt]))
  378. *wp++ = codeset[cnt];
  379. *wp = '\0';
  380. }
  381. return (const char *) retval;
  382. }
  383. /* @@ begin of epilog @@ */
  384. /* We don't want libintl.a to depend on any other library. So we
  385. avoid the non-standard function stpcpy. In GNU C Library this
  386. function is available, though. Also allow the symbol HAVE_STPCPY
  387. to be defined. */
  388. #if !_LIBC && !HAVE_STPCPY
  389. static char *
  390. stpcpy (dest, src)
  391. char *dest;
  392. const char *src;
  393. {
  394. while ((*dest++ = *src++) != '\0')
  395. /* Do nothing. */ ;
  396. return dest - 1;
  397. }
  398. #endif