locale.c 43 KB

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