locale.c 37 KB

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