locale.c 43 KB

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