locale.c 43 KB

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