locale.c 43 KB

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