locale.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  1. /* Copyright (C) 2002 Manuel Novoa III
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Library General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2 of the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Library General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Library General Public
  14. * License along with this library; if not, write to the Free
  15. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. /* Nov. 1, 2002
  18. * Reworked setlocale() return values and locale arg processing to
  19. * be more like glibc. Applications expecting to be able to
  20. * query locale settings should now work... at the cost of almost
  21. * doubling the size of the setlocale object code.
  22. * Fixed a bug in the internal fixed-size-string locale specifier code.
  23. *
  24. * Dec 20, 2002
  25. * Added in collation support and updated stub nl_langinfo.
  26. *
  27. * Aug 1, 2003
  28. * Added glibc-like extended locale stuff (newlocale, duplocale, etc).
  29. *
  30. * Aug 18, 2003
  31. * Bug in duplocale... collation data wasn't copied.
  32. * Bug in newlocale... translate 1<<LC_ALL to LC_ALL_MASK.
  33. * Bug in _wchar_utf8sntowcs... fix cut-n-paste error.
  34. *
  35. * Aug 31, 2003
  36. * Hack around bg_BG bug; grouping specified but no thousands separator.
  37. * Also, disable the locale link_warnings for now, as they generate a
  38. * lot of noise when using libstd++.
  39. */
  40. /* TODO:
  41. * Implement the shared mmap code so non-mmu platforms can use this.
  42. * Add some basic collate functionality similar to what the previous
  43. * locale support had (8-bit codesets only).
  44. */
  45. #define _GNU_SOURCE
  46. #define __CTYPE_HAS_8_BIT_LOCALES 1
  47. #include <string.h>
  48. #include <stdlib.h>
  49. #include <stddef.h>
  50. #include <limits.h>
  51. #include <stdint.h>
  52. #include <assert.h>
  53. #include <errno.h>
  54. #include <ctype.h>
  55. #include <stdio.h>
  56. #ifdef __UCLIBC_MJN3_ONLY__
  57. #ifdef L_setlocale
  58. #warning TODO: Make the link_warning()s a config option?
  59. #endif
  60. #endif
  61. #undef link_warning
  62. #define link_warning(A,B)
  63. #undef __LOCALE_C_ONLY
  64. #ifndef __UCLIBC_HAS_LOCALE__
  65. #define __LOCALE_C_ONLY
  66. #endif /* __UCLIBC_HAS_LOCALE__ */
  67. #ifdef __LOCALE_C_ONLY
  68. #include <locale.h>
  69. #else /* __LOCALE_C_ONLY */
  70. #ifdef __UCLIBC_MJN3_ONLY__
  71. #ifdef L_setlocale
  72. #warning TODO: Fix the __CTYPE_HAS_8_BIT_LOCALES define at the top of the file.
  73. #warning TODO: Fix __WCHAR_ENABLED.
  74. #endif
  75. #endif
  76. /* Need to include this before locale.h and xlocale.h! */
  77. #include <bits/uClibc_locale.h>
  78. #undef CODESET_LIST
  79. #define CODESET_LIST (__locale_mmap->codeset_list)
  80. #ifdef __UCLIBC_HAS_XLOCALE__
  81. #include <xlocale.h>
  82. #include <locale.h>
  83. #else /* __UCLIBC_HAS_XLOCALE__ */
  84. /* We need this internally... */
  85. #define __UCLIBC_HAS_XLOCALE__ 1
  86. #include <xlocale.h>
  87. #include <locale.h>
  88. #undef __UCLIBC_HAS_XLOCALE__
  89. #endif /* __UCLIBC_HAS_XLOCALE__ */
  90. #include <wchar.h>
  91. #define LOCALE_NAMES (__locale_mmap->locale_names5)
  92. #define LOCALES (__locale_mmap->locales)
  93. #define LOCALE_AT_MODIFIERS (__locale_mmap->locale_at_modifiers)
  94. #define CATEGORY_NAMES (__locale_mmap->lc_names)
  95. #ifdef __UCLIBC_MJN3_ONLY__
  96. #warning REMINDER: redo the MAX_LOCALE_STR stuff...
  97. #endif
  98. #define MAX_LOCALE_STR 256 /* TODO: Only sufficient for current case. */
  99. #define MAX_LOCALE_CATEGORY_STR 32 /* TODO: Only sufficient for current case. */
  100. /* Note: Best if MAX_LOCALE_CATEGORY_STR is a power of 2. */
  101. extern int _locale_set_l(const unsigned char *p, __locale_t base);
  102. extern void _locale_init_l(__locale_t base);
  103. #endif /* __LOCALE_C_ONLY */
  104. #undef LOCALE_STRING_SIZE
  105. #define LOCALE_SELECTOR_SIZE (2 * __LC_ALL + 2)
  106. #ifdef __UCLIBC_MJN3_ONLY__
  107. #ifdef L_setlocale
  108. #warning TODO: Create a C locale selector string.
  109. #endif
  110. #endif
  111. #define C_LOCALE_SELECTOR "\x23\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80"
  112. #include <langinfo.h>
  113. #include <nl_types.h>
  114. /**********************************************************************/
  115. #ifdef L_setlocale
  116. #ifdef __LOCALE_C_ONLY
  117. link_warning(setlocale,"REMINDER: The 'setlocale' function supports only C|POSIX locales.")
  118. static const char C_string[] = "C";
  119. char *setlocale(int category, register const char *locale)
  120. {
  121. return ( (((unsigned int)(category)) <= LC_ALL)
  122. && ( (!locale) /* Request for locale category string. */
  123. || (!*locale) /* Implementation-defined default is C. */
  124. || ((*locale == 'C') && !locale[1])
  125. || (!strcmp(locale, "POSIX"))) )
  126. ? (char *) C_string /* Always in C/POSIX locale. */
  127. : NULL;
  128. }
  129. #else /* ---------------------------------------------- __LOCALE_C_ONLY */
  130. #ifdef __UCLIBC_HAS_THREADS__
  131. link_warning(setlocale,"REMINDER: The 'setlocale' function is _not_ threadsafe except for simple queries.")
  132. #endif
  133. #if !defined(__LOCALE_DATA_NUM_LOCALES) || (__LOCALE_DATA_NUM_LOCALES <= 1)
  134. #error locales enabled, but not data other than for C locale!
  135. #endif
  136. #ifdef __UCLIBC_MJN3_ONLY__
  137. #warning TODO: Move posix and utf8 strings.
  138. #endif
  139. static const char posix[] = "POSIX";
  140. static const char utf8[] = "UTF-8";
  141. #ifdef __UCLIBC_MJN3_ONLY__
  142. #warning TODO: Fix dimensions of hr_locale.
  143. #endif
  144. /* Individual category strings start at hr_locale + category * MAX_LOCALE_CATEGORY.
  145. * This holds for LC_ALL as well.
  146. */
  147. static char hr_locale[(MAX_LOCALE_CATEGORY_STR * LC_ALL) + MAX_LOCALE_STR];
  148. static void update_hr_locale(const unsigned char *spec)
  149. {
  150. const unsigned char *loc;
  151. const unsigned char *s;
  152. char *n;
  153. int i, category, done;
  154. done = category = 0;
  155. do {
  156. s = spec + 1;
  157. n = hr_locale + category * MAX_LOCALE_CATEGORY_STR;
  158. if (category == LC_ALL) {
  159. done = 1;
  160. for (i = 0 ; i < LC_ALL-1 ; i += 2) {
  161. if ((s[i] != s[i+2]) || (s[i+1] != s[i+3])) {
  162. goto SKIP;
  163. }
  164. }
  165. /* All categories the same, so simplify string by using a single
  166. * category. */
  167. category = LC_CTYPE;
  168. }
  169. SKIP:
  170. i = (category == LC_ALL) ? 0 : category;
  171. s += 2*i;
  172. do {
  173. if ((*s != 0xff) || (s[1] != 0xff)) {
  174. loc = LOCALES
  175. + __LOCALE_DATA_WIDTH_LOCALES * ((((int)(*s & 0x7f)) << 7)
  176. + (s[1] & 0x7f));
  177. if (category == LC_ALL) {
  178. n = stpcpy(n, CATEGORY_NAMES + (int) CATEGORY_NAMES[i]);
  179. *n++ = '=';
  180. }
  181. if (*loc == 0) {
  182. *n++ = 'C';
  183. *n = 0;
  184. } else {
  185. char at = 0;
  186. memcpy(n, LOCALE_NAMES + 5*((*loc)-1), 5);
  187. if (n[2] != '_') {
  188. at = n[2];
  189. n[2] = '_';
  190. }
  191. n += 5;
  192. *n++ = '.';
  193. if (loc[2] == 2) {
  194. n = stpcpy(n, utf8);
  195. } else if (loc[2] >= 3) {
  196. n = stpcpy(n, CODESET_LIST + (int)(CODESET_LIST[loc[2] - 3]));
  197. }
  198. if (at) {
  199. const char *q;
  200. *n++ = '@';
  201. q = LOCALE_AT_MODIFIERS;
  202. do {
  203. if (q[1] == at) {
  204. n = stpcpy(n, q+2);
  205. break;
  206. }
  207. q += 2 + *q;
  208. } while (*q);
  209. }
  210. }
  211. *n++ = ';';
  212. }
  213. s += 2;
  214. } while (++i < category);
  215. *--n = 0; /* Remove trailing ';' and nul-terminate. */
  216. ++category;
  217. } while (!done);
  218. }
  219. char *setlocale(int category, const char *locale)
  220. {
  221. if (((unsigned int)(category)) > LC_ALL) {
  222. #if 0
  223. __set_errno(EINVAL); /* glibc sets errno -- SUSv3 doesn't say. */
  224. #endif
  225. return NULL; /* Illegal/unsupported category. */
  226. }
  227. if (locale != NULL) { /* Not just a query... */
  228. if (!__newlocale((1 << category), locale, __global_locale)) {
  229. return NULL; /* Failed! */
  230. }
  231. update_hr_locale(__global_locale->cur_locale);
  232. }
  233. /* Either a query or a successful set, so return current locale string. */
  234. return hr_locale + (category * MAX_LOCALE_CATEGORY_STR);
  235. }
  236. #endif /* __LOCALE_C_ONLY */
  237. #endif
  238. /**********************************************************************/
  239. #ifdef L_localeconv
  240. /* Note: We assume here that the compiler does the sane thing regarding
  241. * placement of the fields in the struct. If necessary, we could ensure
  242. * this usings an array of offsets but at some size cost. */
  243. #ifdef __LOCALE_C_ONLY
  244. link_warning(localeconv,"REMINDER: The 'localeconv' function is hardwired for C/POSIX locale only.")
  245. static struct lconv the_lconv;
  246. static const char decpt[] = ".";
  247. struct lconv *localeconv(void)
  248. {
  249. register char *p = (char *)(&the_lconv);
  250. *((char **)p) = (char *) decpt;
  251. do {
  252. p += sizeof(char **);
  253. *((char **)p) = (char *) (decpt+1);
  254. } while (p < (char *) &the_lconv.negative_sign);
  255. p = (&the_lconv.int_frac_digits);
  256. do {
  257. *p = CHAR_MAX;
  258. ++p;
  259. } while (p <= &the_lconv.int_n_sign_posn);
  260. return &the_lconv;
  261. }
  262. #else /* __LOCALE_C_ONLY */
  263. static struct lconv the_lconv;
  264. struct lconv *localeconv(void)
  265. {
  266. register char *p = (char *) &the_lconv;
  267. register char **q = (char **) &(__UCLIBC_CURLOCALE_DATA).decimal_point;
  268. do {
  269. *((char **)p) = *q;
  270. p += sizeof(char **);
  271. ++q;
  272. } while (p < &the_lconv.int_frac_digits);
  273. do {
  274. *p = **q;
  275. ++p;
  276. ++q;
  277. } while (p <= &the_lconv.int_n_sign_posn);
  278. return &the_lconv;
  279. }
  280. #endif /* __LOCALE_C_ONLY */
  281. #endif
  282. /**********************************************************************/
  283. #if defined(L__locale_init) && !defined(__LOCALE_C_ONLY)
  284. __uclibc_locale_t __global_locale_data;
  285. __locale_t __global_locale = &__global_locale_data;
  286. #ifdef __UCLIBC_HAS_XLOCALE__
  287. __locale_t __curlocale_var = &__global_locale_data;
  288. #endif
  289. /*----------------------------------------------------------------------*/
  290. #ifdef __UCLIBC_MJN3_ONLY__
  291. #warning TODO: Move utf8 and ascii strings.
  292. #endif
  293. static const char utf8[] = "UTF-8";
  294. static const char ascii[] = "ASCII";
  295. typedef struct {
  296. uint16_t num_base;
  297. uint16_t num_der;
  298. uint16_t MAX_WEIGHTS;
  299. uint16_t num_index2weight;
  300. #define num_index2ruleidx num_index2weight
  301. uint16_t num_weightstr;
  302. uint16_t num_multistart;
  303. uint16_t num_override;
  304. uint16_t num_ruletable;
  305. } coldata_header_t;
  306. typedef struct {
  307. uint16_t num_weights;
  308. uint16_t num_starters;
  309. uint16_t ii_shift;
  310. uint16_t ti_shift;
  311. uint16_t ii_len;
  312. uint16_t ti_len;
  313. uint16_t max_weight;
  314. uint16_t num_col_base;
  315. uint16_t max_col_index;
  316. uint16_t undefined_idx;
  317. uint16_t range_low;
  318. uint16_t range_count;
  319. uint16_t range_base_weight;
  320. uint16_t range_rule_offset;
  321. uint16_t index2weight_offset;
  322. uint16_t index2ruleidx_offset;
  323. uint16_t multistart_offset;
  324. uint16_t wcs2colidt_offset_low;
  325. uint16_t wcs2colidt_offset_hi;
  326. } coldata_base_t;
  327. typedef struct {
  328. uint16_t base_idx;
  329. uint16_t undefined_idx;
  330. uint16_t overrides_offset;
  331. uint16_t multistart_offset;
  332. } coldata_der_t;
  333. static int init_cur_collate(int der_num, __collate_t *cur_collate)
  334. {
  335. const uint16_t *__locale_collate_tbl = __locale_mmap->collate_data;
  336. coldata_header_t *cdh;
  337. coldata_base_t *cdb;
  338. coldata_der_t *cdd;
  339. const uint16_t *p;
  340. size_t n;
  341. uint16_t i, w;
  342. assert(sizeof(coldata_base_t) == 19*2);
  343. assert(sizeof(coldata_der_t) == 4*2);
  344. assert(sizeof(coldata_header_t) == 8*2);
  345. if (!der_num) { /* C locale... special */
  346. cur_collate->num_weights = 0;
  347. return 1;
  348. }
  349. --der_num;
  350. cdh = (coldata_header_t *) __locale_collate_tbl;
  351. #ifdef __UCLIBC_MJN3_ONLY__
  352. #warning CONSIDER: Should we assert here?
  353. #endif
  354. #if 0
  355. if (der_num >= cdh->num_der) {
  356. return 0;
  357. }
  358. #else
  359. assert((der_num < cdh->num_der));
  360. #endif
  361. cdd = (coldata_der_t *)(__locale_collate_tbl
  362. + (sizeof(coldata_header_t)
  363. + cdh->num_base * sizeof(coldata_base_t)
  364. + der_num * sizeof(coldata_der_t)
  365. )/2 );
  366. cdb = (coldata_base_t *)(__locale_collate_tbl
  367. + (sizeof(coldata_header_t)
  368. + cdd->base_idx * sizeof(coldata_base_t)
  369. )/2 );
  370. memcpy(cur_collate, cdb, offsetof(coldata_base_t,index2weight_offset));
  371. cur_collate->undefined_idx = cdd->undefined_idx;
  372. cur_collate->ti_mask = (1 << cur_collate->ti_shift)-1;
  373. cur_collate->ii_mask = (1 << cur_collate->ii_shift)-1;
  374. /* fflush(stdout); */
  375. /* fprintf(stderr,"base=%d num_col_base: %d %d\n", cdd->base_idx ,cur_collate->num_col_base, cdb->num_col_base); */
  376. n = (sizeof(coldata_header_t) + cdh->num_base * sizeof(coldata_base_t)
  377. + cdh->num_der * sizeof(coldata_der_t))/2;
  378. /* fprintf(stderr,"n = %d\n", n); */
  379. cur_collate->index2weight_tbl = __locale_collate_tbl + n + cdb->index2weight_offset;
  380. /* fprintf(stderr,"i2w = %d\n", n + cdb->index2weight_offset); */
  381. n += cdh->num_index2weight;
  382. cur_collate->index2ruleidx_tbl = __locale_collate_tbl + n + cdb->index2ruleidx_offset;
  383. /* fprintf(stderr,"i2r = %d\n", n + cdb->index2ruleidx_offset); */
  384. n += cdh->num_index2ruleidx;
  385. cur_collate->multistart_tbl = __locale_collate_tbl + n + cdd->multistart_offset;
  386. /* fprintf(stderr,"mts = %d\n", n + cdb->multistart_offset); */
  387. n += cdh->num_multistart;
  388. cur_collate->overrides_tbl = __locale_collate_tbl + n + cdd->overrides_offset;
  389. /* fprintf(stderr,"ovr = %d\n", n + cdd->overrides_offset); */
  390. n += cdh->num_override;
  391. cur_collate->ruletable = __locale_collate_tbl + n;
  392. /* fprintf(stderr, "rtb = %d\n", n); */
  393. n += cdh->num_ruletable;
  394. cur_collate->weightstr = __locale_collate_tbl + n;
  395. /* fprintf(stderr,"wts = %d\n", n); */
  396. n += cdh->num_weightstr;
  397. cur_collate->wcs2colidt_tbl = __locale_collate_tbl + n
  398. + (((unsigned long)(cdb->wcs2colidt_offset_hi)) << 16)
  399. + cdb->wcs2colidt_offset_low;
  400. /* fprintf(stderr,"wcs = %lu\n", n + (((unsigned long)(cdb->wcs2colidt_offset_hi)) << 16) */
  401. /* + cdb->wcs2colidt_offset_low); */
  402. cur_collate->MAX_WEIGHTS = cdh->MAX_WEIGHTS;
  403. #ifdef __UCLIBC_MJN3_ONLY__
  404. #warning CONSIDER: Fix the +1 by increasing max_col_index?
  405. #warning CONSIDER: Since this collate info is dependent only on LC_COLLATE ll_cc and not on codeset, we could just globally allocate this for each in a table
  406. #endif
  407. cur_collate->index2weight = calloc(2*cur_collate->max_col_index+2,
  408. sizeof(uint16_t));
  409. if (!cur_collate->index2weight) {
  410. return 0;
  411. }
  412. cur_collate->index2ruleidx = cur_collate->index2weight
  413. + cur_collate->max_col_index + 1;
  414. memcpy(cur_collate->index2weight, cur_collate->index2weight_tbl,
  415. cur_collate->num_col_base * sizeof(uint16_t));
  416. memcpy(cur_collate->index2ruleidx, cur_collate->index2ruleidx_tbl,
  417. cur_collate->num_col_base * sizeof(uint16_t));
  418. /* now do the overrides */
  419. p = cur_collate->overrides_tbl;
  420. while (*p > 1) {
  421. /* fprintf(stderr, "processing override -- count = %d\n", *p); */
  422. n = *p++;
  423. w = *p++;
  424. do {
  425. i = *p++;
  426. /* fprintf(stderr, " i=%d (%#x) w=%d *p=%d\n", i, i, w, *p); */
  427. cur_collate->index2weight[i-1] = w++;
  428. cur_collate->index2ruleidx[i-1] = *p++;
  429. } while (--n);
  430. }
  431. assert(*p == 1);
  432. while (*++p) {
  433. i = *p;
  434. /* fprintf(stderr, " i=%d (%#x) w=%d *p=%d\n", i, i, p[1], p[2]); */
  435. cur_collate->index2weight[i-1] = *++p;
  436. cur_collate->index2ruleidx[i-1] = *++p;
  437. }
  438. for (i=0 ; i < cur_collate->multistart_tbl[0] ; i++) {
  439. p = cur_collate->multistart_tbl;
  440. /* fprintf(stderr, "%2d of %2d: %d ", i, cur_collate->multistart_tbl[0], p[i]); */
  441. p += p[i];
  442. do {
  443. n = *p++;
  444. do {
  445. if (!*p) { /* found it */
  446. /* fprintf(stderr, "found: n=%d (%#lx) |%.*ls|\n", n, (int) *cs->s, n, cs->s); */
  447. /* fprintf(stderr, ": %d - single\n", n); */
  448. goto FOUND;
  449. }
  450. /* the lookup check here is safe since we're assured that *p is a valid colidex */
  451. /* fprintf(stderr, "lookup(%lc)==%d *p==%d\n", cs->s[n], lookup(cs->s[n]), (int) *p); */
  452. /* fprintf(stderr, ": %d - ", n); */
  453. do {
  454. /* fprintf(stderr, "%d|", *p); */
  455. } while (*p++);
  456. break;
  457. } while (1);
  458. } while (1);
  459. FOUND:
  460. continue;
  461. }
  462. return 1;
  463. }
  464. int _locale_set_l(const unsigned char *p, __locale_t base)
  465. {
  466. const char **x;
  467. unsigned char *s = base->cur_locale + 1;
  468. const size_t *stp;
  469. const unsigned char *r;
  470. const uint16_t *io;
  471. const uint16_t *ii;
  472. const unsigned char *d;
  473. int row; /* locale row */
  474. int crow; /* category row */
  475. int len;
  476. int c;
  477. int i = 0;
  478. __collate_t newcol;
  479. ++p;
  480. newcol.index2weight = NULL;
  481. if ((p[2*LC_COLLATE] != s[2*LC_COLLATE])
  482. || (p[2*LC_COLLATE + 1] != s[2*LC_COLLATE + 1])
  483. ) {
  484. row = (((int)(*p & 0x7f)) << 7) + (p[1] & 0x7f);
  485. assert(row < __LOCALE_DATA_NUM_LOCALES);
  486. if (!init_cur_collate(__locale_mmap->locales[ __LOCALE_DATA_WIDTH_LOCALES
  487. * row + 3 + LC_COLLATE ],
  488. &newcol)
  489. ) {
  490. return 0; /* calloc failed. */
  491. }
  492. free(base->collate.index2weight);
  493. memcpy(&base->collate, &newcol, sizeof(__collate_t));
  494. }
  495. do {
  496. if ((*p != *s) || (p[1] != s[1])) {
  497. row = (((int)(*p & 0x7f)) << 7) + (p[1] & 0x7f);
  498. assert(row < __LOCALE_DATA_NUM_LOCALES);
  499. *s = *p;
  500. s[1] = p[1];
  501. if ((i != LC_COLLATE)
  502. && ((len = __locale_mmap->lc_common_item_offsets_LEN[i]) != 0)
  503. ) {
  504. crow = __locale_mmap->locales[ __LOCALE_DATA_WIDTH_LOCALES * row
  505. + 3 + i ]
  506. * len;
  507. x = (const char **)(((char *) base)
  508. + base->category_offsets[i]);
  509. stp = __locale_mmap->lc_common_tbl_offsets + 4*i;
  510. r = (const unsigned char *)( ((char *)__locale_mmap) + *stp );
  511. io = (const uint16_t *)( ((char *)__locale_mmap) + *++stp );
  512. ii = (const uint16_t *)( ((char *)__locale_mmap) + *++stp );
  513. d = (const unsigned char *)( ((char *)__locale_mmap) + *++stp );
  514. for (c=0 ; c < len ; c++) {
  515. *(x + c) = d + ii[ r[crow + c] + io[c] ];
  516. }
  517. }
  518. if (i == LC_CTYPE) {
  519. c = __locale_mmap->locales[ __LOCALE_DATA_WIDTH_LOCALES * row
  520. + 2 ]; /* codeset */
  521. if (c <= 2) {
  522. if (c == 2) {
  523. base->codeset = utf8;
  524. base->encoding = __ctype_encoding_utf8;
  525. /* TODO - fix for bcc */
  526. base->mb_cur_max = 6;
  527. } else {
  528. assert(c==1);
  529. base->codeset = ascii;
  530. base->encoding = __ctype_encoding_7_bit;
  531. base->mb_cur_max = 1;
  532. }
  533. } else {
  534. const __codeset_8_bit_t *c8b;
  535. r = CODESET_LIST;
  536. base->codeset = r + r[c -= 3];
  537. base->encoding = __ctype_encoding_8_bit;
  538. #ifdef __UCLIBC_MJN3_ONLY__
  539. #warning REMINDER: update 8 bit mb_cur_max when translit implemented!
  540. #endif
  541. /* TODO - update when translit implemented! */
  542. base->mb_cur_max = 1;
  543. c8b = __locale_mmap->codeset_8_bit + c;
  544. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  545. base->idx8ctype = c8b->idx8ctype;
  546. base->idx8uplow = c8b->idx8uplow;
  547. #ifdef __UCLIBC_HAS_WCHAR__
  548. base->idx8c2wc = c8b->idx8c2wc;
  549. base->idx8wc2c = c8b->idx8wc2c;
  550. /* translit */
  551. #endif /* __UCLIBC_HAS_WCHAR__ */
  552. /* What follows is fairly bloated, but it is just a hack
  553. * to get the 8-bit codeset ctype stuff functioning.
  554. * All of this will be replaced in the next generation
  555. * of locale support anyway... */
  556. memcpy(base->__ctype_b_data,
  557. __C_ctype_b - __UCLIBC_CTYPE_B_TBL_OFFSET,
  558. (256 + __UCLIBC_CTYPE_B_TBL_OFFSET)
  559. * sizeof(__ctype_mask_t));
  560. memcpy(base->__ctype_tolower_data,
  561. __C_ctype_tolower - __UCLIBC_CTYPE_TO_TBL_OFFSET,
  562. (256 + __UCLIBC_CTYPE_TO_TBL_OFFSET)
  563. * sizeof(__ctype_touplow_t));
  564. memcpy(base->__ctype_toupper_data,
  565. __C_ctype_toupper - __UCLIBC_CTYPE_TO_TBL_OFFSET,
  566. (256 + __UCLIBC_CTYPE_TO_TBL_OFFSET)
  567. * sizeof(__ctype_touplow_t));
  568. #define Cctype_TBL_MASK ((1 << __LOCALE_DATA_Cctype_IDX_SHIFT) - 1)
  569. #define Cctype_IDX_OFFSET (128 >> __LOCALE_DATA_Cctype_IDX_SHIFT)
  570. {
  571. int u;
  572. __ctype_mask_t m;
  573. for (u=0 ; u < 128 ; u++) {
  574. #ifdef __LOCALE_DATA_Cctype_PACKED
  575. c = base->tbl8ctype
  576. [ ((int)(c8b->idx8ctype
  577. [(u >> __LOCALE_DATA_Cctype_IDX_SHIFT) ])
  578. << (__LOCALE_DATA_Cctype_IDX_SHIFT - 1))
  579. + ((u & Cctype_TBL_MASK) >> 1)];
  580. c = (u & 1) ? (c >> 4) : (c & 0xf);
  581. #else
  582. c = base->tbl8ctype
  583. [ ((int)(c8b->idx8ctype
  584. [(u >> __LOCALE_DATA_Cctype_IDX_SHIFT) ])
  585. << __LOCALE_DATA_Cctype_IDX_SHIFT)
  586. + (u & Cctype_TBL_MASK) ];
  587. #endif
  588. m = base->code2flag[c];
  589. base->__ctype_b_data
  590. [128 + __UCLIBC_CTYPE_B_TBL_OFFSET + u]
  591. = m;
  592. #ifdef __UCLIBC_HAS_CTYPE_SIGNED__
  593. if (((signed char)(128 + u)) != -1) {
  594. base->__ctype_b_data[__UCLIBC_CTYPE_B_TBL_OFFSET
  595. + ((signed char)(128 + u))]
  596. = m;
  597. }
  598. #endif
  599. base->__ctype_tolower_data
  600. [128 + __UCLIBC_CTYPE_TO_TBL_OFFSET + u]
  601. = 128 + u;
  602. base->__ctype_toupper_data
  603. [128 + __UCLIBC_CTYPE_TO_TBL_OFFSET + u]
  604. = 128 + u;
  605. if (m & (_ISlower|_ISupper)) {
  606. c = base->tbl8uplow
  607. [ ((int)(c8b->idx8uplow
  608. [u >> __LOCALE_DATA_Cuplow_IDX_SHIFT])
  609. << __LOCALE_DATA_Cuplow_IDX_SHIFT)
  610. + ((128 + u)
  611. & ((1 << __LOCALE_DATA_Cuplow_IDX_SHIFT)
  612. - 1)) ];
  613. if (m & _ISlower) {
  614. base->__ctype_toupper_data
  615. [128 + __UCLIBC_CTYPE_TO_TBL_OFFSET + u]
  616. = (unsigned char)(128 + u + c);
  617. #ifdef __UCLIBC_HAS_CTYPE_SIGNED__
  618. if (((signed char)(128 + u)) != -1) {
  619. base->__ctype_toupper_data
  620. [__UCLIBC_CTYPE_TO_TBL_OFFSET
  621. + ((signed char)(128 + u))]
  622. = (unsigned char)(128 + u + c);
  623. }
  624. #endif
  625. } else {
  626. base->__ctype_tolower_data
  627. [128 + __UCLIBC_CTYPE_TO_TBL_OFFSET + u]
  628. = (unsigned char)(128 + u - c);
  629. #ifdef __UCLIBC_HAS_CTYPE_SIGNED__
  630. if (((signed char)(128 + u)) != -1) {
  631. base->__ctype_tolower_data
  632. [__UCLIBC_CTYPE_TO_TBL_OFFSET
  633. + ((signed char)(128 + u))]
  634. = (unsigned char)(128 + u - c);
  635. }
  636. #endif
  637. }
  638. }
  639. }
  640. }
  641. #ifdef __UCLIBC_HAS_XLOCALE__
  642. base->__ctype_b = base->__ctype_b_data
  643. + __UCLIBC_CTYPE_B_TBL_OFFSET;
  644. base->__ctype_tolower = base->__ctype_tolower_data
  645. + __UCLIBC_CTYPE_TO_TBL_OFFSET;
  646. base->__ctype_toupper = base->__ctype_toupper_data
  647. + __UCLIBC_CTYPE_TO_TBL_OFFSET;
  648. #else /* __UCLIBC_HAS_XLOCALE__ */
  649. __ctype_b = base->__ctype_b_data
  650. + __UCLIBC_CTYPE_B_TBL_OFFSET;
  651. __ctype_tolower = base->__ctype_tolower_data
  652. + __UCLIBC_CTYPE_TO_TBL_OFFSET;
  653. __ctype_toupper = base->__ctype_toupper_data
  654. + __UCLIBC_CTYPE_TO_TBL_OFFSET;
  655. #endif /* __UCLIBC_HAS_XLOCALE__ */
  656. #endif /* __CTYPE_HAS_8_BIT_LOCALES */
  657. }
  658. #ifdef __UCLIBC_MJN3_ONLY__
  659. #warning TODO: Put the outdigit string length in the locale_mmap object.
  660. #endif
  661. d = base->outdigit_length;
  662. x = &base->outdigit0_mb;
  663. for (c = 0 ; c < 10 ; c++) {
  664. ((unsigned char *)d)[c] = strlen(x[c]);
  665. assert(d[c] > 0);
  666. }
  667. } else if (i == LC_NUMERIC) {
  668. assert(LC_NUMERIC > LC_CTYPE); /* Need ctype initialized. */
  669. base->decimal_point_len
  670. = __locale_mbrtowc_l(&base->decimal_point_wc,
  671. base->decimal_point, base);
  672. assert(base->decimal_point_len > 0);
  673. assert(base->decimal_point[base->decimal_point_len] == 0);
  674. if (*base->grouping) {
  675. base->thousands_sep_len
  676. = __locale_mbrtowc_l(&base->thousands_sep_wc,
  677. base->thousands_sep, base);
  678. #if 1
  679. #ifdef __UCLIBC_MJN3_ONLY__
  680. #warning TODO: Remove hack involving grouping without a thousep char (bg_BG).
  681. #endif
  682. assert(base->thousands_sep_len >= 0);
  683. if (base->thousands_sep_len == 0) {
  684. base->grouping = base->thousands_sep; /* empty string */
  685. }
  686. assert(base->thousands_sep[base->thousands_sep_len] == 0);
  687. #else
  688. assert(base->thousands_sep_len > 0);
  689. assert(base->thousands_sep[base->thousands_sep_len] == 0);
  690. #endif
  691. }
  692. /* } else if (i == LC_COLLATE) { */
  693. /* init_cur_collate(__locale_mmap->locales[ __LOCALE_DATA_WIDTH_LOCALES */
  694. /* * row + 3 + i ], */
  695. /* &base->collate); */
  696. }
  697. }
  698. ++i;
  699. p += 2;
  700. s += 2;
  701. } while (i < LC_ALL);
  702. return 1;
  703. }
  704. static const uint16_t __code2flag[16] = {
  705. 0, /* unclassified = 0 */
  706. _ISprint|_ISgraph|_ISalnum|_ISalpha, /* alpha_nonupper_nonlower */
  707. _ISprint|_ISgraph|_ISalnum|_ISalpha|_ISlower, /* alpha_lower */
  708. _ISprint|_ISgraph|_ISalnum|_ISalpha|_ISlower|_ISupper, /* alpha_upper_lower */
  709. _ISprint|_ISgraph|_ISalnum|_ISalpha|_ISupper, /* alpha_upper */
  710. _ISprint|_ISgraph|_ISalnum|_ISdigit, /* digit */
  711. _ISprint|_ISgraph|_ISpunct, /* punct */
  712. _ISprint|_ISgraph, /* graph */
  713. _ISprint|_ISspace, /* print_space_nonblank */
  714. _ISprint|_ISspace|_ISblank, /* print_space_blank */
  715. _ISspace, /* space_nonblank_noncntrl */
  716. _ISspace|_ISblank, /* space_blank_noncntrl */
  717. _IScntrl|_ISspace, /* cntrl_space_nonblank */
  718. _IScntrl|_ISspace|_ISblank, /* cntrl_space_blank */
  719. _IScntrl /* cntrl_nonspace */
  720. };
  721. void _locale_init_l(__locale_t base)
  722. {
  723. memset(base->cur_locale, 0, LOCALE_SELECTOR_SIZE);
  724. base->cur_locale[0] = '#';
  725. memcpy(base->category_item_count,
  726. __locale_mmap->lc_common_item_offsets_LEN,
  727. LC_ALL);
  728. ++base->category_item_count[0]; /* Increment for codeset entry. */
  729. base->category_offsets[0] = offsetof(__uclibc_locale_t, outdigit0_mb);
  730. base->category_offsets[1] = offsetof(__uclibc_locale_t, decimal_point);
  731. base->category_offsets[2] = offsetof(__uclibc_locale_t, int_curr_symbol);
  732. base->category_offsets[3] = offsetof(__uclibc_locale_t, abday_1);
  733. /* base->category_offsets[4] = offsetof(__uclibc_locale_t, collate???); */
  734. base->category_offsets[5] = offsetof(__uclibc_locale_t, yesexpr);
  735. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  736. base->tbl8ctype
  737. = (const unsigned char *) &__locale_mmap->tbl8ctype;
  738. base->tbl8uplow
  739. = (const unsigned char *) &__locale_mmap->tbl8uplow;
  740. #ifdef __UCLIBC_HAS_WCHAR__
  741. base->tbl8c2wc
  742. = (const uint16_t *) &__locale_mmap->tbl8c2wc;
  743. base->tbl8wc2c
  744. = (const unsigned char *) &__locale_mmap->tbl8wc2c;
  745. /* translit */
  746. #endif /* __UCLIBC_HAS_WCHAR__ */
  747. #endif /* __CTYPE_HAS_8_BIT_LOCALES */
  748. #ifdef __UCLIBC_HAS_WCHAR__
  749. base->tblwctype
  750. = (const unsigned char *) &__locale_mmap->tblwctype;
  751. base->tblwuplow
  752. = (const unsigned char *) &__locale_mmap->tblwuplow;
  753. base->tblwuplow_diff
  754. = (const uint16_t *) &__locale_mmap->tblwuplow_diff;
  755. /* base->tblwcomb */
  756. /* = (const unsigned char *) &__locale_mmap->tblwcomb; */
  757. /* width?? */
  758. #endif /* __UCLIBC_HAS_WCHAR__ */
  759. /* Initially, set things up to use the global C ctype tables.
  760. * This is correct for C (ASCII) and UTF-8 based locales (except tr_TR). */
  761. #ifdef __UCLIBC_HAS_XLOCALE__
  762. base->__ctype_b = __C_ctype_b;
  763. base->__ctype_tolower = __C_ctype_tolower;
  764. base->__ctype_toupper = __C_ctype_toupper;
  765. #else /* __UCLIBC_HAS_XLOCALE__ */
  766. __ctype_b = __C_ctype_b;
  767. __ctype_tolower = __C_ctype_tolower;
  768. __ctype_toupper = __C_ctype_toupper;
  769. #endif /* __UCLIBC_HAS_XLOCALE__ */
  770. #ifdef __UCLIBC_MJN3_ONLY__
  771. #warning TODO: Initialize code2flag correctly based on locale_mmap.
  772. #endif
  773. base->code2flag = __code2flag;
  774. _locale_set_l(C_LOCALE_SELECTOR, base);
  775. }
  776. void _locale_init(void)
  777. {
  778. /* TODO: mmap the locale file */
  779. /* TODO - ??? */
  780. _locale_init_l(__global_locale);
  781. }
  782. #endif
  783. /**********************************************************************/
  784. #if defined(L_nl_langinfo) || defined(L_nl_langinfo_l)
  785. #ifdef __LOCALE_C_ONLY
  786. /* We need to index 320 bytes of data, so you might initially think we
  787. * need to store the offsets in shorts. But since the offset of the
  788. * 64th item is 182, we'll store "offset - 2*64" for all items >= 64
  789. * and always calculate the data offset as "offset[i] + 2*(i & 64)".
  790. * This allows us to pack the data offsets in an unsigned char while
  791. * also avoiding an "if".
  792. *
  793. * Note: Category order is assumed to be:
  794. * ctype, numeric, monetary, time, collate, messages, all
  795. */
  796. #define C_LC_ALL 6
  797. /* Combine the data to avoid size penalty for seperate char arrays when
  798. * compiler aligns objects. The original code is left in as documentation. */
  799. #define cat_start nl_data
  800. #define C_locale_data (nl_data + C_LC_ALL + 1 + 90)
  801. static const unsigned char nl_data[C_LC_ALL + 1 + 90 + 320] = {
  802. /* static const char cat_start[LC_ALL + 1] = { */
  803. '\x00', '\x0b', '\x0e', '\x24', '\x56', '\x56', '\x5a',
  804. /* }; */
  805. /* static const char item_offset[90] = { */
  806. '\x00', '\x02', '\x04', '\x06', '\x08', '\x0a', '\x0c', '\x0e',
  807. '\x10', '\x12', '\x14', '\x1a', '\x1b', '\x1b', '\x1b', '\x1b',
  808. '\x1b', '\x1b', '\x1b', '\x1b', '\x1b', '\x1c', '\x1c', '\x1c',
  809. '\x1c', '\x1c', '\x1c', '\x1c', '\x1c', '\x1c', '\x1c', '\x1c',
  810. '\x1c', '\x1c', '\x1c', '\x1e', '\x20', '\x24', '\x28', '\x2c',
  811. '\x30', '\x34', '\x38', '\x3c', '\x43', '\x4a', '\x52', '\x5c',
  812. '\x65', '\x6c', '\x75', '\x79', '\x7d', '\x81', '\x85', '\x89',
  813. '\x8d', '\x91', '\x95', '\x99', '\x9d', '\xa1', '\xa5', '\xad',
  814. '\x36', '\x3c', '\x42', '\x46', '\x4b', '\x50', '\x57', '\x61',
  815. '\x69', '\x72', '\x7b', '\x7e', '\x81', '\x96', '\x9f', '\xa8',
  816. '\xb3', '\xb3', '\xb3', '\xb3', '\xb3', '\xb3', '\xb4', '\xba',
  817. '\xbf', '\xbf',
  818. /* }; */
  819. /* static const char C_locale_data[320] = { */
  820. '0', '\x00', '1', '\x00', '2', '\x00', '3', '\x00',
  821. '4', '\x00', '5', '\x00', '6', '\x00', '7', '\x00',
  822. '8', '\x00', '9', '\x00', 'A', 'S', 'C', 'I',
  823. 'I', '\x00', '.', '\x00', '\x7f', '\x00', '-', '\x00',
  824. 'S', 'u', 'n', '\x00', 'M', 'o', 'n', '\x00',
  825. 'T', 'u', 'e', '\x00', 'W', 'e', 'd', '\x00',
  826. 'T', 'h', 'u', '\x00', 'F', 'r', 'i', '\x00',
  827. 'S', 'a', 't', '\x00', 'S', 'u', 'n', 'd',
  828. 'a', 'y', '\x00', 'M', 'o', 'n', 'd', 'a',
  829. 'y', '\x00', 'T', 'u', 'e', 's', 'd', 'a',
  830. 'y', '\x00', 'W', 'e', 'd', 'n', 'e', 's',
  831. 'd', 'a', 'y', '\x00', 'T', 'h', 'u', 'r',
  832. 's', 'd', 'a', 'y', '\x00', 'F', 'r', 'i',
  833. 'd', 'a', 'y', '\x00', 'S', 'a', 't', 'u',
  834. 'r', 'd', 'a', 'y', '\x00', 'J', 'a', 'n',
  835. '\x00', 'F', 'e', 'b', '\x00', 'M', 'a', 'r',
  836. '\x00', 'A', 'p', 'r', '\x00', 'M', 'a', 'y',
  837. '\x00', 'J', 'u', 'n', '\x00', 'J', 'u', 'l',
  838. '\x00', 'A', 'u', 'g', '\x00', 'S', 'e', 'p',
  839. '\x00', 'O', 'c', 't', '\x00', 'N', 'o', 'v',
  840. '\x00', 'D', 'e', 'c', '\x00', 'J', 'a', 'n',
  841. 'u', 'a', 'r', 'y', '\x00', 'F', 'e', 'b',
  842. 'r', 'u', 'a', 'r', 'y', '\x00', 'M', 'a',
  843. 'r', 'c', 'h', '\x00', 'A', 'p', 'r', 'i',
  844. 'l', '\x00', 'M', 'a', 'y', '\x00', 'J', 'u',
  845. 'n', 'e', '\x00', 'J', 'u', 'l', 'y', '\x00',
  846. 'A', 'u', 'g', 'u', 's', 't', '\x00', 'S',
  847. 'e', 'p', 't', 'e', 'm', 'b', 'e', 'r',
  848. '\x00', 'O', 'c', 't', 'o', 'b', 'e', 'r',
  849. '\x00', 'N', 'o', 'v', 'e', 'm', 'b', 'e',
  850. 'r', '\x00', 'D', 'e', 'c', 'e', 'm', 'b',
  851. 'e', 'r', '\x00', 'A', 'M', '\x00', 'P', 'M',
  852. '\x00', '%', 'a', ' ', '%', 'b', ' ', '%',
  853. 'e', ' ', '%', 'H', ':', '%', 'M', ':',
  854. '%', 'S', ' ', '%', 'Y', '\x00', '%', 'm',
  855. '/', '%', 'd', '/', '%', 'y', '\x00', '%',
  856. 'H', ':', '%', 'M', ':', '%', 'S', '\x00',
  857. '%', 'I', ':', '%', 'M', ':', '%', 'S',
  858. ' ', '%', 'p', '\x00', '^', '[', 'y', 'Y',
  859. ']', '\x00', '^', '[', 'n', 'N', ']', '\x00',
  860. };
  861. char *nl_langinfo(nl_item item)
  862. {
  863. unsigned int c;
  864. unsigned int i;
  865. if ((c = _NL_ITEM_CATEGORY(item)) < C_LC_ALL) {
  866. if ((i = cat_start[c] + _NL_ITEM_INDEX(item)) < cat_start[c+1]) {
  867. /* return (char *) C_locale_data + item_offset[i] + (i & 64); */
  868. return (char *) C_locale_data + nl_data[C_LC_ALL+1+i] + 2*(i & 64);
  869. }
  870. }
  871. return (char *) cat_start; /* Conveniently, this is the empty string. */
  872. }
  873. #else /* __LOCALE_C_ONLY */
  874. #if defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE)
  875. char *nl_langinfo(nl_item item)
  876. {
  877. return __nl_langinfo_l(item, __UCLIBC_CURLOCALE);
  878. }
  879. #else /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  880. static const char empty[] = "";
  881. char *__XL(nl_langinfo)(nl_item item __LOCALE_PARAM )
  882. {
  883. unsigned int c = _NL_ITEM_CATEGORY(item);
  884. unsigned int i = _NL_ITEM_INDEX(item);
  885. if ((c < LC_ALL) && (i < __LOCALE_PTR->category_item_count[c])) {
  886. return ((char **)(((char *) __LOCALE_PTR)
  887. + __LOCALE_PTR->category_offsets[c]))[i];
  888. }
  889. return (char *) empty;
  890. }
  891. #endif /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  892. #endif /* __LOCALE_C_ONLY */
  893. #endif
  894. /**********************************************************************/
  895. #ifdef L_newlocale
  896. #ifdef __UCLIBC_MJN3_ONLY__
  897. #warning TODO: Move posix and utf8 strings.
  898. #endif
  899. static const char posix[] = "POSIX";
  900. static const char utf8[] = "UTF-8";
  901. static int find_locale(int category_mask, const char *p,
  902. unsigned char *new_locale)
  903. {
  904. int i;
  905. const unsigned char *s;
  906. uint16_t n;
  907. unsigned char lang_cult, codeset;
  908. #if defined(__LOCALE_DATA_AT_MODIFIERS_LENGTH) && 1
  909. /* Support standard locale handling for @-modifiers. */
  910. #ifdef __UCLIBC_MJN3_ONLY__
  911. #warning REMINDER: Fix buf size in find_locale.
  912. #endif
  913. char buf[18]; /* TODO: 7+{max codeset name length} */
  914. const char *q;
  915. if ((q = strchr(p,'@')) != NULL) {
  916. if ((((size_t)((q-p)-5)) > (sizeof(buf) - 5)) || (p[2] != '_')) {
  917. return 0;
  918. }
  919. /* locale name at least 5 chars long and 3rd char is '_' */
  920. s = LOCALE_AT_MODIFIERS;
  921. do {
  922. if (!strcmp(s+2, q+1)) {
  923. break;
  924. }
  925. s += 2 + *s; /* TODO - fix this throughout */
  926. } while (*s);
  927. if (!*s) {
  928. return 0;
  929. }
  930. assert(q - p < sizeof(buf));
  931. memcpy(buf, p, q-p);
  932. buf[q-p] = 0;
  933. buf[2] = s[1];
  934. p = buf;
  935. }
  936. #endif
  937. lang_cult = codeset = 0; /* Assume C and default codeset. */
  938. if (((*p == 'C') && !p[1]) || !strcmp(p, posix)) {
  939. goto FIND_LOCALE;
  940. }
  941. if ((strlen(p) > 5) && (p[5] == '.')) { /* Codeset in locale name? */
  942. /* TODO: maybe CODESET_LIST + *s ??? */
  943. /* 7bit is 1, UTF-8 is 2, 8-bit is >= 3 */
  944. codeset = 2;
  945. if (strcmp(utf8,p+6) != 0) {/* TODO - fix! */
  946. s = CODESET_LIST;
  947. do {
  948. ++codeset; /* Increment codeset first. */
  949. if (!strcmp(CODESET_LIST+*s, p+6)) {
  950. goto FIND_LANG_CULT;
  951. }
  952. } while (*++s);
  953. return 0; /* No matching codeset! */
  954. }
  955. }
  956. FIND_LANG_CULT: /* Find language_culture number. */
  957. s = LOCALE_NAMES;
  958. do { /* TODO -- do a binary search? */
  959. /* TODO -- fix gen_mmap!*/
  960. ++lang_cult; /* Increment first since C/POSIX is 0. */
  961. if (!strncmp(s,p,5)) { /* Found a matching locale name; */
  962. goto FIND_LOCALE;
  963. }
  964. s += 5;
  965. } while (lang_cult < __LOCALE_DATA_NUM_LOCALE_NAMES);
  966. return 0; /* No matching language_culture! */
  967. FIND_LOCALE: /* Find locale row matching name and codeset */
  968. s = LOCALES;
  969. n = 0;
  970. do { /* TODO -- do a binary search? */
  971. if ((lang_cult == *s) && ((codeset == s[1]) || (codeset == s[2]))) {
  972. i = 1;
  973. s = new_locale + 1;
  974. do {
  975. if (category_mask & i) {
  976. /* Encode current locale row number. */
  977. ((unsigned char *) s)[0] = (n >> 7) | 0x80;
  978. ((unsigned char *) s)[1] = (n & 0x7f) | 0x80;
  979. }
  980. s += 2;
  981. i += i;
  982. } while (i < (1 << LC_ALL));
  983. return i; /* Return non-zero */
  984. }
  985. s += __LOCALE_DATA_WIDTH_LOCALES;
  986. ++n;
  987. } while (n <= __LOCALE_DATA_NUM_LOCALES); /* We started at 1!!! */
  988. return 0; /* Unsupported locale. */
  989. }
  990. static unsigned char *composite_locale(int category_mask, const char *locale,
  991. unsigned char *new_locale)
  992. {
  993. char buf[MAX_LOCALE_STR];
  994. char *t;
  995. char *e;
  996. int c;
  997. int component_mask;
  998. if (!strchr(locale,'=')) {
  999. if (!find_locale(category_mask, locale, new_locale)) {
  1000. return NULL;
  1001. }
  1002. return new_locale;
  1003. }
  1004. if (strlen(locale) >= sizeof(buf)) {
  1005. return NULL;
  1006. }
  1007. stpcpy(buf, locale);
  1008. component_mask = 0;
  1009. t = strtok_r(buf, "=", &e); /* This can't fail because of strchr test above. */
  1010. do {
  1011. c = 0;
  1012. while (strcmp(CATEGORY_NAMES + (int) CATEGORY_NAMES[c], t)) {
  1013. if (++c == LC_ALL) { /* Unknown category name! */
  1014. return NULL;
  1015. }
  1016. }
  1017. t = strtok_r(NULL, ";", &e);
  1018. c = (1 << c);
  1019. if (component_mask & c) { /* Multiple components for one category. */
  1020. return NULL;
  1021. }
  1022. component_mask |= c;
  1023. if ((category_mask & c) && (!t || !find_locale(c, t, new_locale))) {
  1024. return NULL;
  1025. }
  1026. } while ((t = strtok_r(NULL, "=", &e)) != NULL);
  1027. if (category_mask & ~component_mask) { /* Category component(s) missing. */
  1028. return NULL;
  1029. }
  1030. return new_locale;
  1031. }
  1032. __locale_t __newlocale(int category_mask, const char *locale, __locale_t base)
  1033. {
  1034. const unsigned char *p;
  1035. int i, j, k;
  1036. unsigned char new_selector[LOCALE_SELECTOR_SIZE];
  1037. if (category_mask == (1 << LC_ALL)) {
  1038. category_mask = LC_ALL_MASK;
  1039. }
  1040. if (!locale || (((unsigned int)(category_mask)) > LC_ALL_MASK)) {
  1041. INVALID:
  1042. __set_errno(EINVAL);
  1043. return NULL; /* No locale or illegal/unsupported category. */
  1044. }
  1045. #ifdef __UCLIBC_MJN3_ONLY__
  1046. #warning TODO: Rename cur_locale to locale_selector.
  1047. #endif
  1048. strcpy((char *) new_selector,
  1049. (base ? (char *) base->cur_locale : C_LOCALE_SELECTOR));
  1050. if (!*locale) { /* locale == "", so check environment. */
  1051. #ifndef __UCLIBC_HAS_THREADS__
  1052. static /* If no threads, then envstr can be static. */
  1053. #endif /* __UCLIBC_HAS_THREADS__ */
  1054. const char *envstr[4] = { "LC_ALL", NULL, "LANG", posix };
  1055. i = 1;
  1056. k = 0;
  1057. do {
  1058. if (category_mask & i) {
  1059. /* Note: SUSv3 doesn't define a fallback mechanism here.
  1060. * So, if LC_ALL is invalid, we do _not_ continue trying
  1061. * the other environment vars. */
  1062. envstr[1] = CATEGORY_NAMES + CATEGORY_NAMES[k];
  1063. j = 0;
  1064. do {
  1065. p = envstr[j];
  1066. } while ((++j < 4) && (!(p = getenv(p)) || !*p));
  1067. /* The user set something... is it valid? */
  1068. /* Note: Since we don't support user-supplied locales and
  1069. * alternate paths, we don't need to worry about special
  1070. * handling for suid/sgid apps. */
  1071. if (!find_locale(i, p, new_selector)) {
  1072. goto INVALID;
  1073. }
  1074. }
  1075. i += i;
  1076. } while (++k < LC_ALL);
  1077. } else if (!composite_locale(category_mask, locale, new_selector)) {
  1078. goto INVALID;
  1079. }
  1080. #ifdef __UCLIBC_MJN3_ONLY__
  1081. #warning TODO: Do a compatible codeset check!
  1082. #endif
  1083. /* If we get here, the new selector corresponds to a valid locale. */
  1084. #ifdef __UCLIBC_MJN3_ONLY__
  1085. #warning CONSIDER: Probably want a _locale_new func to allow for caching of locales.
  1086. #endif
  1087. #if 0
  1088. if (base) {
  1089. _locale_set_l(new_selector, base);
  1090. } else {
  1091. base = _locale_new(new_selector);
  1092. }
  1093. #else
  1094. if (!base) {
  1095. if ((base = malloc(sizeof(__uclibc_locale_t))) == NULL) {
  1096. return base;
  1097. }
  1098. _locale_init_l(base);
  1099. }
  1100. _locale_set_l(new_selector, base);
  1101. #endif
  1102. return base;
  1103. }
  1104. weak_alias(__newlocale, newlocale)
  1105. #endif
  1106. /**********************************************************************/
  1107. #ifdef L_duplocale
  1108. #ifdef __UCLIBC_MJN3_ONLY__
  1109. #warning REMINDER: When we allocate ctype tables, remember to dup them.
  1110. #endif
  1111. __locale_t __duplocale(__locale_t dataset)
  1112. {
  1113. __locale_t r;
  1114. uint16_t * i2w;
  1115. size_t n;
  1116. assert(dataset != LC_GLOBAL_LOCALE);
  1117. if ((r = malloc(sizeof(__uclibc_locale_t))) != NULL) {
  1118. n = 2*dataset->collate.max_col_index+2;
  1119. if ((i2w = calloc(n, sizeof(uint16_t)))
  1120. != NULL
  1121. ) {
  1122. memcpy(r, dataset, sizeof(__uclibc_locale_t));
  1123. r->collate.index2weight = i2w;
  1124. memcpy(i2w, dataset->collate.index2weight, n * sizeof(uint16_t));
  1125. } else {
  1126. free(r);
  1127. r = NULL;
  1128. }
  1129. }
  1130. return r;
  1131. }
  1132. weak_alias(__duplocale, duplocale)
  1133. #endif
  1134. /**********************************************************************/
  1135. #ifdef L_freelocale
  1136. #ifdef __UCLIBC_MJN3_ONLY__
  1137. #warning REMINDER: When we allocate ctype tables, remember to free them.
  1138. #endif
  1139. void __freelocale(__locale_t dataset)
  1140. {
  1141. assert(dataset != __global_locale);
  1142. assert(dataset != LC_GLOBAL_LOCALE);
  1143. free(dataset->collate.index2weight); /* Free collation data. */
  1144. free(dataset); /* Free locale */
  1145. }
  1146. weak_alias(__freelocale, freelocale)
  1147. #endif
  1148. /**********************************************************************/
  1149. #ifdef L_uselocale
  1150. __locale_t __uselocale(__locale_t dataset)
  1151. {
  1152. __locale_t old;
  1153. if (!dataset) {
  1154. old = __UCLIBC_CURLOCALE;
  1155. } else {
  1156. if (dataset == LC_GLOBAL_LOCALE) {
  1157. dataset = __global_locale;
  1158. }
  1159. #ifdef __UCLIBC_HAS_THREADS__
  1160. old = __curlocale_set(dataset);
  1161. #else
  1162. old = __curlocale_var;
  1163. __curlocale_var = dataset;
  1164. #endif
  1165. }
  1166. if (old == __global_locale) {
  1167. return LC_GLOBAL_LOCALE;
  1168. }
  1169. return old;
  1170. }
  1171. weak_alias(__uselocale, uselocale)
  1172. #endif
  1173. /**********************************************************************/
  1174. #ifdef L___curlocale
  1175. #ifdef __UCLIBC_HAS_THREADS__
  1176. __locale_t weak_const_function __curlocale(void)
  1177. {
  1178. return __curlocale_var; /* This is overriden by the thread version. */
  1179. }
  1180. __locale_t weak_function __curlocale_set(__locale_t newloc)
  1181. {
  1182. __locale_t oldloc = __curlocale_var;
  1183. assert(newloc != LC_GLOBAL_LOCALE);
  1184. __curlocale_var = newloc;
  1185. return oldloc;
  1186. }
  1187. #endif
  1188. #endif
  1189. /**********************************************************************/
  1190. #ifdef L___locale_mbrtowc_l
  1191. /* NOTE: This returns an int... not size_t. Also, it is not a general
  1192. * routine. It is actually a very stripped-down version of mbrtowc
  1193. * that takes a __locale_t arg. This is used by strcoll and strxfrm.
  1194. * It is also used above to generate wchar_t versions of the decimal point
  1195. * and thousands seperator. */
  1196. #ifndef __CTYPE_HAS_UTF_8_LOCALES
  1197. #warning __CTYPE_HAS_UTF_8_LOCALES not set!
  1198. #endif
  1199. #ifndef __CTYPE_HAS_8_BIT_LOCALES
  1200. #warning __CTYPE_HAS_8_BIT_LOCALES not set!
  1201. #endif
  1202. #define Cc2wc_IDX_SHIFT __LOCALE_DATA_Cc2wc_IDX_SHIFT
  1203. #define Cc2wc_ROW_LEN __LOCALE_DATA_Cc2wc_ROW_LEN
  1204. extern size_t _wchar_utf8sntowcs(wchar_t *__restrict pwc, size_t wn,
  1205. const char **__restrict src, size_t n,
  1206. mbstate_t *ps, int allow_continuation);
  1207. int __locale_mbrtowc_l(wchar_t *__restrict dst,
  1208. const char *__restrict src,
  1209. __locale_t loc )
  1210. {
  1211. #ifdef __CTYPE_HAS_UTF_8_LOCALES
  1212. if (loc->encoding == __ctype_encoding_utf8) {
  1213. mbstate_t ps;
  1214. const char *p = src;
  1215. size_t r;
  1216. ps.__mask = 0;
  1217. r = _wchar_utf8sntowcs(dst, 1, &p, SIZE_MAX, &ps, 1);
  1218. return (r == 1) ? (p-src) : r; /* Need to return 0 if nul char. */
  1219. }
  1220. #endif
  1221. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  1222. assert((loc->encoding == __ctype_encoding_7_bit) || (loc->encoding == __ctype_encoding_8_bit));
  1223. #else
  1224. assert(loc->encoding == __ctype_encoding_7_bit);
  1225. #endif
  1226. if ((*dst = ((unsigned char)(*src))) < 0x80) { /* ASCII... */
  1227. return (*src != 0);
  1228. }
  1229. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  1230. if (loc->encoding == __ctype_encoding_8_bit) {
  1231. wchar_t wc = *dst - 0x80;
  1232. *dst = loc->tbl8c2wc[
  1233. (loc->idx8c2wc[wc >> Cc2wc_IDX_SHIFT]
  1234. << Cc2wc_IDX_SHIFT) + (wc & (Cc2wc_ROW_LEN - 1))];
  1235. if (*dst) {
  1236. return 1;
  1237. }
  1238. }
  1239. #endif
  1240. return -1;
  1241. }
  1242. #endif
  1243. /**********************************************************************/