locale.c 42 KB

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