locale.c 42 KB

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