l10nflist.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /* Copyright (C) 1995-1999, 2000, 2001 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
  4. it under the terms of the GNU General Public License as published by
  5. 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
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. /* Tell glibc's <string.h> to provide a prototype for stpcpy().
  15. This must come before <config.h> because <config.h> may include
  16. <features.h>, and once <features.h> has been included, it's too late. */
  17. #ifndef _GNU_SOURCE
  18. # define _GNU_SOURCE 1
  19. #endif
  20. #ifdef HAVE_CONFIG_H
  21. # include <config.h>
  22. #endif
  23. #include <string.h>
  24. #if !HAVE_STRCHR && !defined _LIBC
  25. # ifndef strchr
  26. # define strchr index
  27. # endif
  28. #endif
  29. #if defined _LIBC || defined HAVE_ARGZ_H
  30. # include <argz.h>
  31. #endif
  32. #include <ctype.h>
  33. #include <sys/types.h>
  34. #include <stdlib.h>
  35. #include "loadinfo.h"
  36. /* On some strange systems still no definition of NULL is found. Sigh! */
  37. #ifndef NULL
  38. # if defined __STDC__ && __STDC__
  39. # define NULL ((void *) 0)
  40. # else
  41. # define NULL 0
  42. # endif
  43. #endif
  44. /* @@ end of prolog @@ */
  45. #ifdef _LIBC
  46. /* Rename the non ANSI C functions. This is required by the standard
  47. because some ANSI C functions will require linking with this object
  48. file and the name space must not be polluted. */
  49. # ifndef stpcpy
  50. # define stpcpy(dest, src) __stpcpy(dest, src)
  51. # endif
  52. #else
  53. # ifndef HAVE_STPCPY
  54. static char *stpcpy PARAMS ((char *dest, const char *src));
  55. # endif
  56. #endif
  57. /* Define function which are usually not available. */
  58. #if !defined _LIBC && !defined HAVE___ARGZ_COUNT
  59. /* Returns the number of strings in ARGZ. */
  60. static size_t argz_count__ PARAMS ((const char *argz, size_t len));
  61. static size_t
  62. argz_count__ (argz, len)
  63. const char *argz;
  64. size_t len;
  65. {
  66. size_t count = 0;
  67. while (len > 0)
  68. {
  69. size_t part_len = strlen (argz);
  70. argz += part_len + 1;
  71. len -= part_len + 1;
  72. count++;
  73. }
  74. return count;
  75. }
  76. # undef __argz_count
  77. # define __argz_count(argz, len) argz_count__ (argz, len)
  78. #endif /* !_LIBC && !HAVE___ARGZ_COUNT */
  79. #if !defined _LIBC && !defined HAVE___ARGZ_STRINGIFY
  80. /* Make '\0' separated arg vector ARGZ printable by converting all the '\0's
  81. except the last into the character SEP. */
  82. static void argz_stringify__ PARAMS ((char *argz, size_t len, int sep));
  83. static void
  84. argz_stringify__ (argz, len, sep)
  85. char *argz;
  86. size_t len;
  87. int sep;
  88. {
  89. while (len > 0)
  90. {
  91. size_t part_len = strlen (argz);
  92. argz += part_len;
  93. len -= part_len + 1;
  94. if (len > 0)
  95. *argz++ = sep;
  96. }
  97. }
  98. # undef __argz_stringify
  99. # define __argz_stringify(argz, len, sep) argz_stringify__ (argz, len, sep)
  100. #endif /* !_LIBC && !HAVE___ARGZ_STRINGIFY */
  101. #if !defined _LIBC && !defined HAVE___ARGZ_NEXT
  102. static char *argz_next__ PARAMS ((char *argz, size_t argz_len,
  103. const char *entry));
  104. static char *
  105. argz_next__ (argz, argz_len, entry)
  106. char *argz;
  107. size_t argz_len;
  108. const char *entry;
  109. {
  110. if (entry)
  111. {
  112. if (entry < argz + argz_len)
  113. entry = strchr (entry, '\0') + 1;
  114. return entry >= argz + argz_len ? NULL : (char *) entry;
  115. }
  116. else
  117. if (argz_len > 0)
  118. return argz;
  119. else
  120. return 0;
  121. }
  122. # undef __argz_next
  123. # define __argz_next(argz, len, entry) argz_next__ (argz, len, entry)
  124. #endif /* !_LIBC && !HAVE___ARGZ_NEXT */
  125. /* Return number of bits set in X. */
  126. static int pop PARAMS ((int x));
  127. static inline int
  128. pop (x)
  129. int x;
  130. {
  131. /* We assume that no more than 16 bits are used. */
  132. x = ((x & ~0x5555) >> 1) + (x & 0x5555);
  133. x = ((x & ~0x3333) >> 2) + (x & 0x3333);
  134. x = ((x >> 4) + x) & 0x0f0f;
  135. x = ((x >> 8) + x) & 0xff;
  136. return x;
  137. }
  138. struct loaded_l10nfile *
  139. _nl_make_l10nflist (l10nfile_list, dirlist, dirlist_len, mask, language,
  140. territory, codeset, normalized_codeset, modifier, special,
  141. sponsor, revision, filename, do_allocate)
  142. struct loaded_l10nfile **l10nfile_list;
  143. const char *dirlist;
  144. size_t dirlist_len;
  145. int mask;
  146. const char *language;
  147. const char *territory;
  148. const char *codeset;
  149. const char *normalized_codeset;
  150. const char *modifier;
  151. const char *special;
  152. const char *sponsor;
  153. const char *revision;
  154. const char *filename;
  155. int do_allocate;
  156. {
  157. char *abs_filename;
  158. struct loaded_l10nfile *last = NULL;
  159. struct loaded_l10nfile *retval;
  160. char *cp;
  161. size_t entries;
  162. int cnt;
  163. /* Allocate room for the full file name. */
  164. abs_filename = (char *) malloc (dirlist_len
  165. + strlen (language)
  166. + ((mask & TERRITORY) != 0
  167. ? strlen (territory) + 1 : 0)
  168. + ((mask & XPG_CODESET) != 0
  169. ? strlen (codeset) + 1 : 0)
  170. + ((mask & XPG_NORM_CODESET) != 0
  171. ? strlen (normalized_codeset) + 1 : 0)
  172. + (((mask & XPG_MODIFIER) != 0
  173. || (mask & CEN_AUDIENCE) != 0)
  174. ? strlen (modifier) + 1 : 0)
  175. + ((mask & CEN_SPECIAL) != 0
  176. ? strlen (special) + 1 : 0)
  177. + (((mask & CEN_SPONSOR) != 0
  178. || (mask & CEN_REVISION) != 0)
  179. ? (1 + ((mask & CEN_SPONSOR) != 0
  180. ? strlen (sponsor) + 1 : 0)
  181. + ((mask & CEN_REVISION) != 0
  182. ? strlen (revision) + 1 : 0)) : 0)
  183. + 1 + strlen (filename) + 1);
  184. if (abs_filename == NULL)
  185. return NULL;
  186. retval = NULL;
  187. last = NULL;
  188. /* Construct file name. */
  189. memcpy (abs_filename, dirlist, dirlist_len);
  190. __argz_stringify (abs_filename, dirlist_len, PATH_SEPARATOR);
  191. cp = abs_filename + (dirlist_len - 1);
  192. *cp++ = '/';
  193. cp = stpcpy (cp, language);
  194. if ((mask & TERRITORY) != 0)
  195. {
  196. *cp++ = '_';
  197. cp = stpcpy (cp, territory);
  198. }
  199. if ((mask & XPG_CODESET) != 0)
  200. {
  201. *cp++ = '.';
  202. cp = stpcpy (cp, codeset);
  203. }
  204. if ((mask & XPG_NORM_CODESET) != 0)
  205. {
  206. *cp++ = '.';
  207. cp = stpcpy (cp, normalized_codeset);
  208. }
  209. if ((mask & (XPG_MODIFIER | CEN_AUDIENCE)) != 0)
  210. {
  211. /* This component can be part of both syntaces but has different
  212. leading characters. For CEN we use `+', else `@'. */
  213. *cp++ = (mask & CEN_AUDIENCE) != 0 ? '+' : '@';
  214. cp = stpcpy (cp, modifier);
  215. }
  216. if ((mask & CEN_SPECIAL) != 0)
  217. {
  218. *cp++ = '+';
  219. cp = stpcpy (cp, special);
  220. }
  221. if ((mask & (CEN_SPONSOR | CEN_REVISION)) != 0)
  222. {
  223. *cp++ = ',';
  224. if ((mask & CEN_SPONSOR) != 0)
  225. cp = stpcpy (cp, sponsor);
  226. if ((mask & CEN_REVISION) != 0)
  227. {
  228. *cp++ = '_';
  229. cp = stpcpy (cp, revision);
  230. }
  231. }
  232. *cp++ = '/';
  233. stpcpy (cp, filename);
  234. /* Look in list of already loaded domains whether it is already
  235. available. */
  236. last = NULL;
  237. for (retval = *l10nfile_list; retval != NULL; retval = retval->next)
  238. if (retval->filename != NULL)
  239. {
  240. int compare = strcmp (retval->filename, abs_filename);
  241. if (compare == 0)
  242. /* We found it! */
  243. break;
  244. if (compare < 0)
  245. {
  246. /* It's not in the list. */
  247. retval = NULL;
  248. break;
  249. }
  250. last = retval;
  251. }
  252. if (retval != NULL || do_allocate == 0)
  253. {
  254. free (abs_filename);
  255. return retval;
  256. }
  257. retval = (struct loaded_l10nfile *)
  258. malloc (sizeof (*retval) + (__argz_count (dirlist, dirlist_len)
  259. * (1 << pop (mask))
  260. * sizeof (struct loaded_l10nfile *)));
  261. if (retval == NULL)
  262. return NULL;
  263. retval->filename = abs_filename;
  264. retval->decided = (__argz_count (dirlist, dirlist_len) != 1
  265. || ((mask & XPG_CODESET) != 0
  266. && (mask & XPG_NORM_CODESET) != 0));
  267. retval->data = NULL;
  268. if (last == NULL)
  269. {
  270. retval->next = *l10nfile_list;
  271. *l10nfile_list = retval;
  272. }
  273. else
  274. {
  275. retval->next = last->next;
  276. last->next = retval;
  277. }
  278. entries = 0;
  279. /* If the DIRLIST is a real list the RETVAL entry corresponds not to
  280. a real file. So we have to use the DIRLIST separation mechanism
  281. of the inner loop. */
  282. cnt = __argz_count (dirlist, dirlist_len) == 1 ? mask - 1 : mask;
  283. for (; cnt >= 0; --cnt)
  284. if ((cnt & ~mask) == 0
  285. && ((cnt & CEN_SPECIFIC) == 0 || (cnt & XPG_SPECIFIC) == 0)
  286. && ((cnt & XPG_CODESET) == 0 || (cnt & XPG_NORM_CODESET) == 0))
  287. {
  288. /* Iterate over all elements of the DIRLIST. */
  289. char *dir = NULL;
  290. while ((dir = __argz_next ((char *) dirlist, dirlist_len, dir))
  291. != NULL)
  292. retval->successor[entries++]
  293. = _nl_make_l10nflist (l10nfile_list, dir, strlen (dir) + 1, cnt,
  294. language, territory, codeset,
  295. normalized_codeset, modifier, special,
  296. sponsor, revision, 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 (codeset, name_len)
  307. const char *codeset;
  308. size_t name_len;
  309. {
  310. int len = 0;
  311. int only_digit = 1;
  312. char *retval;
  313. char *wp;
  314. size_t cnt;
  315. for (cnt = 0; cnt < name_len; ++cnt)
  316. if (isalnum (codeset[cnt]))
  317. {
  318. ++len;
  319. if (isalpha (codeset[cnt]))
  320. only_digit = 0;
  321. }
  322. retval = (char *) malloc ((only_digit ? 3 : 0) + len + 1);
  323. if (retval != NULL)
  324. {
  325. if (only_digit)
  326. wp = stpcpy (retval, "iso");
  327. else
  328. wp = retval;
  329. for (cnt = 0; cnt < name_len; ++cnt)
  330. if (isalpha (codeset[cnt]))
  331. *wp++ = tolower (codeset[cnt]);
  332. else if (isdigit (codeset[cnt]))
  333. *wp++ = codeset[cnt];
  334. *wp = '\0';
  335. }
  336. return (const char *) retval;
  337. }
  338. /* @@ begin of epilog @@ */
  339. /* We don't want libintl.a to depend on any other library. So we
  340. avoid the non-standard function stpcpy. In GNU C Library this
  341. function is available, though. Also allow the symbol HAVE_STPCPY
  342. to be defined. */
  343. #if !_LIBC && !HAVE_STPCPY
  344. static char *
  345. stpcpy (dest, src)
  346. char *dest;
  347. const char *src;
  348. {
  349. while ((*dest++ = *src++) != '\0')
  350. /* Do nothing. */ ;
  351. return dest - 1;
  352. }
  353. #endif