Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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