locale.c 42 KB

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