l10nflist.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /* Copyright (C) 1995-1999, 2000-2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  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 (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 __CYGWIN__ || defined __EMX__ || defined __DJGPP__
  59. /* Win32, Cygwin, 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. #ifdef _LIBC
  72. # define __argz_count(argz, len) INTUSE(__argz_count) (argz, len)
  73. #elif defined HAVE_ARGZ_COUNT
  74. # undef __argz_count
  75. # define __argz_count argz_count
  76. #else
  77. /* Returns the number of strings in ARGZ. */
  78. static size_t
  79. argz_count__ (const char *argz, size_t len)
  80. {
  81. size_t count = 0;
  82. while (len > 0)
  83. {
  84. size_t part_len = strlen (argz);
  85. argz += part_len + 1;
  86. len -= part_len + 1;
  87. count++;
  88. }
  89. return count;
  90. }
  91. # undef __argz_count
  92. # define __argz_count(argz, len) argz_count__ (argz, len)
  93. #endif /* !_LIBC && !HAVE_ARGZ_COUNT */
  94. #ifdef _LIBC
  95. # define __argz_stringify(argz, len, sep) \
  96. INTUSE(__argz_stringify) (argz, len, sep)
  97. #elif defined HAVE_ARGZ_STRINGIFY
  98. # undef __argz_stringify
  99. # define __argz_stringify argz_stringify
  100. #else
  101. /* Make '\0' separated arg vector ARGZ printable by converting all the '\0's
  102. except the last into the character SEP. */
  103. static void
  104. argz_stringify__ (char *argz, size_t len, 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. #endif /* !_LIBC && !HAVE_ARGZ_STRINGIFY */
  118. #ifdef _LIBC
  119. #elif defined HAVE_ARGZ_NEXT
  120. # undef __argz_next
  121. # define __argz_next argz_next
  122. #else
  123. static char *
  124. argz_next__ (char *argz, size_t argz_len, const char *entry)
  125. {
  126. if (entry)
  127. {
  128. if (entry < argz + argz_len)
  129. entry = strchr (entry, '\0') + 1;
  130. return entry >= argz + argz_len ? NULL : (char *) entry;
  131. }
  132. else
  133. if (argz_len > 0)
  134. return argz;
  135. else
  136. return 0;
  137. }
  138. # undef __argz_next
  139. # define __argz_next(argz, len, entry) argz_next__ (argz, len, entry)
  140. #endif /* !_LIBC && !HAVE_ARGZ_NEXT */
  141. /* Return number of bits set in X. */
  142. static inline int
  143. pop (int x)
  144. {
  145. /* We assume that no more than 16 bits are used. */
  146. x = ((x & ~0x5555) >> 1) + (x & 0x5555);
  147. x = ((x & ~0x3333) >> 2) + (x & 0x3333);
  148. x = ((x >> 4) + x) & 0x0f0f;
  149. x = ((x >> 8) + x) & 0xff;
  150. return x;
  151. }
  152. struct loaded_l10nfile *
  153. _nl_make_l10nflist (struct loaded_l10nfile **l10nfile_list,
  154. const char *dirlist, size_t dirlist_len,
  155. int mask, const char *language, const char *territory,
  156. const char *codeset, const char *normalized_codeset,
  157. const char *modifier,
  158. const char *filename, int do_allocate)
  159. {
  160. char *abs_filename;
  161. struct loaded_l10nfile **lastp;
  162. struct loaded_l10nfile *retval;
  163. char *cp;
  164. size_t dirlist_count;
  165. size_t entries;
  166. int cnt;
  167. /* If LANGUAGE contains an absolute directory specification, we ignore
  168. DIRLIST. */
  169. if (IS_ABSOLUTE_PATH (language))
  170. dirlist_len = 0;
  171. /* Allocate room for the full file name. */
  172. abs_filename = (char *) malloc (dirlist_len
  173. + strlen (language)
  174. + ((mask & XPG_TERRITORY) != 0
  175. ? strlen (territory) + 1 : 0)
  176. + ((mask & XPG_CODESET) != 0
  177. ? strlen (codeset) + 1 : 0)
  178. + ((mask & XPG_NORM_CODESET) != 0
  179. ? strlen (normalized_codeset) + 1 : 0)
  180. + ((mask & XPG_MODIFIER) != 0
  181. ? strlen (modifier) + 1 : 0)
  182. + 1 + strlen (filename) + 1);
  183. if (abs_filename == NULL)
  184. return NULL;
  185. /* Construct file name. */
  186. cp = abs_filename;
  187. if (dirlist_len > 0)
  188. {
  189. memcpy (cp, dirlist, dirlist_len);
  190. __argz_stringify (cp, dirlist_len, PATH_SEPARATOR);
  191. cp += dirlist_len;
  192. cp[-1] = '/';
  193. }
  194. cp = stpcpy (cp, language);
  195. if ((mask & XPG_TERRITORY) != 0)
  196. {
  197. *cp++ = '_';
  198. cp = stpcpy (cp, territory);
  199. }
  200. if ((mask & XPG_CODESET) != 0)
  201. {
  202. *cp++ = '.';
  203. cp = stpcpy (cp, codeset);
  204. }
  205. if ((mask & XPG_NORM_CODESET) != 0)
  206. {
  207. *cp++ = '.';
  208. cp = stpcpy (cp, normalized_codeset);
  209. }
  210. if ((mask & XPG_MODIFIER) != 0)
  211. {
  212. *cp++ = '@';
  213. cp = stpcpy (cp, modifier);
  214. }
  215. *cp++ = '/';
  216. stpcpy (cp, filename);
  217. /* Look in list of already loaded domains whether it is already
  218. available. */
  219. lastp = l10nfile_list;
  220. for (retval = *l10nfile_list; retval != NULL; retval = retval->next)
  221. if (retval->filename != NULL)
  222. {
  223. int compare = strcmp (retval->filename, abs_filename);
  224. if (compare == 0)
  225. /* We found it! */
  226. break;
  227. if (compare < 0)
  228. {
  229. /* It's not in the list. */
  230. retval = NULL;
  231. break;
  232. }
  233. lastp = &retval->next;
  234. }
  235. if (retval != NULL || do_allocate == 0)
  236. {
  237. free (abs_filename);
  238. return retval;
  239. }
  240. dirlist_count = (dirlist_len > 0 ? __argz_count (dirlist, dirlist_len) : 1);
  241. /* Allocate a new loaded_l10nfile. */
  242. retval =
  243. (struct loaded_l10nfile *)
  244. malloc (sizeof (*retval)
  245. + (((dirlist_count << pop (mask)) + (dirlist_count > 1 ? 1 : 0))
  246. * sizeof (struct loaded_l10nfile *)));
  247. if (retval == NULL)
  248. {
  249. free (abs_filename);
  250. return NULL;
  251. }
  252. retval->filename = abs_filename;
  253. /* We set retval->data to NULL here; it is filled in later.
  254. Setting retval->decided to 1 here means that retval does not
  255. correspond to a real file (dirlist_count > 1) or is not worth
  256. looking up (if an unnormalized codeset was specified). */
  257. retval->decided = (dirlist_count > 1
  258. || ((mask & XPG_CODESET) != 0
  259. && (mask & XPG_NORM_CODESET) != 0));
  260. retval->data = NULL;
  261. retval->next = *lastp;
  262. *lastp = retval;
  263. entries = 0;
  264. /* Recurse to fill the inheritance list of RETVAL.
  265. If the DIRLIST is a real list (i.e. DIRLIST_COUNT > 1), the RETVAL
  266. entry does not correspond to a real file; retval->filename contains
  267. colons. In this case we loop across all elements of DIRLIST and
  268. across all bit patterns dominated by MASK.
  269. If the DIRLIST is a single directory or entirely redundant (i.e.
  270. DIRLIST_COUNT == 1), we loop across all bit patterns dominated by
  271. MASK, excluding MASK itself.
  272. In either case, we loop down from MASK to 0. This has the effect
  273. that the extra bits in the locale name are dropped in this order:
  274. first the modifier, then the territory, then the codeset, then the
  275. normalized_codeset. */
  276. for (cnt = dirlist_count > 1 ? mask : mask - 1; cnt >= 0; --cnt)
  277. if ((cnt & ~mask) == 0
  278. && !((cnt & XPG_CODESET) != 0 && (cnt & XPG_NORM_CODESET) != 0))
  279. {
  280. if (dirlist_count > 1)
  281. {
  282. /* Iterate over all elements of the DIRLIST. */
  283. char *dir = NULL;
  284. while ((dir = __argz_next ((char *) dirlist, dirlist_len, dir))
  285. != NULL)
  286. retval->successor[entries++]
  287. = _nl_make_l10nflist (l10nfile_list, dir, strlen (dir) + 1,
  288. cnt, language, territory, codeset,
  289. normalized_codeset, modifier, filename,
  290. 1);
  291. }
  292. else
  293. retval->successor[entries++]
  294. = _nl_make_l10nflist (l10nfile_list, dirlist, dirlist_len,
  295. cnt, language, territory, codeset,
  296. normalized_codeset, modifier, filename, 1);
  297. }
  298. retval->successor[entries] = NULL;
  299. return retval;
  300. }
  301. /* Normalize codeset name. There is no standard for the codeset
  302. names. Normalization allows the user to use any of the common
  303. names. The return value is dynamically allocated and has to be
  304. freed by the caller. */
  305. const char *
  306. _nl_normalize_codeset (const char *codeset, size_t name_len)
  307. {
  308. int len = 0;
  309. int only_digit = 1;
  310. char *retval;
  311. char *wp;
  312. size_t cnt;
  313. for (cnt = 0; cnt < name_len; ++cnt)
  314. if (isalnum ((unsigned char) codeset[cnt]))
  315. {
  316. ++len;
  317. if (isalpha ((unsigned char) codeset[cnt]))
  318. only_digit = 0;
  319. }
  320. retval = (char *) malloc ((only_digit ? 3 : 0) + len + 1);
  321. if (retval != NULL)
  322. {
  323. if (only_digit)
  324. wp = stpcpy (retval, "iso");
  325. else
  326. wp = retval;
  327. for (cnt = 0; cnt < name_len; ++cnt)
  328. if (isalpha ((unsigned char) codeset[cnt]))
  329. *wp++ = tolower ((unsigned char) codeset[cnt]);
  330. else if (isdigit ((unsigned char) codeset[cnt]))
  331. *wp++ = codeset[cnt];
  332. *wp = '\0';
  333. }
  334. return (const char *) retval;
  335. }
  336. /* @@ begin of epilog @@ */
  337. /* We don't want libintl.a to depend on any other library. So we
  338. avoid the non-standard function stpcpy. In GNU C Library this
  339. function is available, though. Also allow the symbol HAVE_STPCPY
  340. to be defined. */
  341. #if !_LIBC && !HAVE_STPCPY
  342. static char *
  343. stpcpy (char *dest, const char *src)
  344. {
  345. while ((*dest++ = *src++) != '\0')
  346. /* Do nothing. */ ;
  347. return dest - 1;
  348. }
  349. #endif