i18n.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910
  1. /* Copyright 2006-2014,2017-2019
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <alloca.h>
  19. #include <locale.h>
  20. #include <string.h> /* `strcoll ()' */
  21. #include <ctype.h> /* `toupper ()' et al. */
  22. #include <errno.h>
  23. #include <unicase.h>
  24. #include <unistr.h>
  25. #include "boolean.h"
  26. #include "chars.h"
  27. #include "dynwind.h"
  28. #include "extensions.h"
  29. #include "feature.h"
  30. #include "gsubr.h"
  31. #include "list.h"
  32. #include "modules.h"
  33. #include "numbers.h"
  34. #include "pairs.h"
  35. #include "posix.h" /* for `scm_i_locale_mutex' */
  36. #include "smob.h"
  37. #include "strings.h"
  38. #include "symbols.h"
  39. #include "syscalls.h"
  40. #include "threads.h"
  41. #include "values.h"
  42. #include "variable.h"
  43. #include "version.h"
  44. #include "i18n.h"
  45. #ifndef SCM_MAX_ALLOCA
  46. # define SCM_MAX_ALLOCA 4096 /* Max bytes per string to allocate via alloca */
  47. #endif
  48. #if defined HAVE_NEWLOCALE && defined HAVE_STRCOLL_L && defined HAVE_USELOCALE
  49. /* The GNU thread-aware locale API is documented in ``Thread-Aware Locale
  50. Model, a Proposal'', by Ulrich Drepper:
  51. http://people.redhat.com/drepper/tllocale.ps.gz
  52. It is now also implemented by Darwin:
  53. http://developer.apple.com/documentation/Darwin/Reference/ManPages/man3/newlocale.3.html
  54. The whole API was eventually standardized in the ``Open Group Base
  55. Specifications Issue 7'' (aka. "POSIX 2008"):
  56. http://www.opengroup.org/onlinepubs/9699919799/basedefs/locale.h.html */
  57. # define USE_GNU_LOCALE_API
  58. #endif
  59. /* Use Gnulib's header, which also provides `nl_item' & co. */
  60. #include <langinfo.h>
  61. #ifndef HAVE_SETLOCALE
  62. static inline char *
  63. setlocale (int category, const char *name)
  64. {
  65. errno = ENOSYS;
  66. return NULL;
  67. }
  68. #endif
  69. /* The newlib C library has alternative names for locale constants. */
  70. #if HAVE_DECL__NL_NUMERIC_GROUPING
  71. #define INT_CURR_SYMBOL _NL_MONETARY_INT_CURR_SYMBOL
  72. #define MON_DECIMAL_POINT _NL_MONETARY_MON_DECIMAL_POINT
  73. #define MON_THOUSANDS_SEP _NL_MONETARY_MON_THOUSANDS_SEP
  74. #define MON_GROUPING _NL_MONETARY_MON_GROUPING
  75. #define POSITIVE_SIGN _NL_MONETARY_POSITIVE_SIGN
  76. #define NEGATIVE_SIGN _NL_MONETARY_NEGATIVE_SIGN
  77. #define GROUPING _NL_NUMERIC_GROUPING
  78. #define INT_FRAC_DIGITS _NL_MONETARY_INT_FRAC_DIGITS
  79. #define FRAC_DIGITS _NL_MONETARY_FRAC_DIGITS
  80. #define P_CS_PRECEDES _NL_MONETARY_P_CS_PRECEDES
  81. #define P_SEP_BY_SPACE _NL_MONETARY_P_SEP_BY_SPACE
  82. #define N_CS_PRECEDES _NL_MONETARY_N_CS_PRECEDES
  83. #define N_SEP_BY_SPACE _NL_MONETARY_N_SEP_BY_SPACE
  84. #define P_SIGN_POSN _NL_MONETARY_P_SIGN_POSN
  85. #define N_SIGN_POSN _NL_MONETARY_N_SIGN_POSN
  86. #define INT_P_CS_PRECEDES _NL_MONETARY_INT_P_CS_PRECEDES
  87. #define INT_P_SEP_BY_SPACE _NL_MONETARY_INT_P_SEP_BY_SPACE
  88. #define INT_N_CS_PRECEDES _NL_MONETARY_INT_N_CS_PRECEDES
  89. #define INT_N_SEP_BY_SPACE _NL_MONETARY_INT_N_SEP_BY_SPACE
  90. #define INT_P_SIGN_POSN _NL_MONETARY_INT_P_SIGN_POSN
  91. #define INT_N_SIGN_POSN _NL_MONETARY_INT_N_SIGN_POSN
  92. #endif
  93. /* Helper stringification macro. */
  94. #define SCM_I18N_STRINGIFY(_name) # _name
  95. /* Acquiring and releasing the locale lock. */
  96. static inline void
  97. lock_locale_mutex (void)
  98. {
  99. #ifdef HAVE_POSIX
  100. scm_i_pthread_mutex_lock (&scm_i_locale_mutex);
  101. #else
  102. #endif
  103. }
  104. static inline void
  105. unlock_locale_mutex (void)
  106. {
  107. #ifdef HAVE_POSIX
  108. scm_i_pthread_mutex_unlock (&scm_i_locale_mutex);
  109. #else
  110. #endif
  111. }
  112. /* Locale objects, string and character collation, and other locale-dependent
  113. string operations.
  114. A large part of the code here deals with emulating glibc's reentrant
  115. locale API on non-GNU systems. The emulation is a bit "brute-force":
  116. Whenever a `-locale<?' procedure is passed a locale object, then:
  117. 1. The `scm_i_locale_mutex' is locked.
  118. 2. A series of `setlocale ()' call is performed to store the current
  119. locale for each category in an `scm_t_locale' object.
  120. 3. A series of `setlocale ()' call is made to install each of the locale
  121. categories of each of the base locales of each locale object,
  122. recursively, starting from the last locale object of the chain.
  123. 4. The settings captured in step (2) are restored.
  124. 5. The `scm_i_locale_mutex' is released.
  125. Hopefully, the X/Open standard will eventually make this hack useless.
  126. Note: We don't wrap glibc's `uselocale ()' call because it sets the locale
  127. of the current _thread_ (unlike `setlocale ()') and doing so would require
  128. maintaining per-thread locale information on non-GNU systems and always
  129. re-installing this locale upon locale-dependent calls. */
  130. /* Return the category mask corresponding to CAT. */
  131. #define SCM_LOCALE_CATEGORY_MASK(_cat) LC_ ## _cat ## _MASK
  132. #ifndef USE_GNU_LOCALE_API
  133. /* Provide the locale category masks as found in glibc. This must be kept in
  134. sync with `locale-categories.h'. */
  135. # define LC_CTYPE_MASK 1
  136. # define LC_COLLATE_MASK 2
  137. # define LC_MESSAGES_MASK 4
  138. # define LC_MONETARY_MASK 8
  139. # define LC_NUMERIC_MASK 16
  140. # define LC_TIME_MASK 32
  141. # ifdef LC_PAPER
  142. # define LC_PAPER_MASK 64
  143. # else
  144. # define LC_PAPER_MASK 0
  145. # endif
  146. # ifdef LC_NAME
  147. # define LC_NAME_MASK 128
  148. # else
  149. # define LC_NAME_MASK 0
  150. # endif
  151. # ifdef LC_ADDRESS
  152. # define LC_ADDRESS_MASK 256
  153. # else
  154. # define LC_ADDRESS_MASK 0
  155. # endif
  156. # ifdef LC_TELEPHONE
  157. # define LC_TELEPHONE_MASK 512
  158. # else
  159. # define LC_TELEPHONE_MASK 0
  160. # endif
  161. # ifdef LC_MEASUREMENT
  162. # define LC_MEASUREMENT_MASK 1024
  163. # else
  164. # define LC_MEASUREMENT_MASK 0
  165. # endif
  166. # ifdef LC_IDENTIFICATION
  167. # define LC_IDENTIFICATION_MASK 2048
  168. # else
  169. # define LC_IDENTIFICATION_MASK 0
  170. # endif
  171. # define LC_ALL_MASK (LC_CTYPE_MASK \
  172. | LC_NUMERIC_MASK \
  173. | LC_TIME_MASK \
  174. | LC_COLLATE_MASK \
  175. | LC_MONETARY_MASK \
  176. | LC_MESSAGES_MASK \
  177. | LC_PAPER_MASK \
  178. | LC_NAME_MASK \
  179. | LC_ADDRESS_MASK \
  180. | LC_TELEPHONE_MASK \
  181. | LC_MEASUREMENT_MASK \
  182. | LC_IDENTIFICATION_MASK \
  183. )
  184. /* Locale objects as returned by `make-locale' on non-GNU systems. */
  185. typedef struct scm_locale
  186. {
  187. SCM base_locale; /* a `locale' object */
  188. char *locale_name;
  189. int category_mask;
  190. } *scm_t_locale;
  191. #else /* USE_GNU_LOCALE_API */
  192. /* Alias for glibc's locale type. */
  193. typedef locale_t scm_t_locale;
  194. #endif /* USE_GNU_LOCALE_API */
  195. /* A locale object denoting the global locale. */
  196. SCM_GLOBAL_VARIABLE (scm_global_locale, "%global-locale");
  197. /* Validate parameter ARG as a locale object and set C_LOCALE to the
  198. corresponding C locale object. */
  199. #define SCM_VALIDATE_LOCALE_COPY(_pos, _arg, _c_locale) \
  200. do \
  201. { \
  202. SCM_VALIDATE_SMOB ((_pos), (_arg), locale_smob_type); \
  203. (_c_locale) = (scm_t_locale)SCM_SMOB_DATA (_arg); \
  204. } \
  205. while (0)
  206. /* Validate optional parameter ARG as either undefined or bound to a locale
  207. object. Set C_LOCALE to the corresponding C locale object or NULL. */
  208. #define SCM_VALIDATE_OPTIONAL_LOCALE_COPY(_pos, _arg, _c_locale) \
  209. do \
  210. { \
  211. if (!SCM_UNBNDP (_arg)) \
  212. SCM_VALIDATE_LOCALE_COPY (_pos, _arg, _c_locale); \
  213. else \
  214. (_c_locale) = NULL; \
  215. } \
  216. while (0)
  217. SCM_SMOB (scm_tc16_locale_smob_type, "locale", 0);
  218. #ifdef USE_GNU_LOCALE_API
  219. SCM_SMOB_FREE (scm_tc16_locale_smob_type, smob_locale_free, locale)
  220. {
  221. scm_t_locale c_locale;
  222. c_locale = (scm_t_locale) SCM_SMOB_DATA (locale);
  223. if (c_locale)
  224. freelocale (c_locale);
  225. return 0;
  226. }
  227. #endif /* USE_GNU_LOCALE_API */
  228. static void inline scm_locale_error (const char *, int) SCM_NORETURN;
  229. /* Throw an exception corresponding to error ERR. */
  230. static void inline
  231. scm_locale_error (const char *func_name, int err)
  232. {
  233. scm_syserror_msg (func_name,
  234. "Failed to install locale",
  235. SCM_EOL, err);
  236. }
  237. /* Emulating GNU's reentrant locale API. */
  238. #ifndef USE_GNU_LOCALE_API
  239. /* Maximum number of chained locales (via `base_locale'). */
  240. #define LOCALE_STACK_SIZE_MAX 256
  241. typedef struct
  242. {
  243. #define SCM_DEFINE_LOCALE_CATEGORY(_name) char * _name;
  244. #include "locale-categories.h"
  245. #undef SCM_DEFINE_LOCALE_CATEGORY
  246. } scm_t_locale_settings;
  247. /* Fill out SETTINGS according to the current locale settings. On success
  248. zero is returned and SETTINGS is properly initialized. */
  249. static int
  250. get_current_locale_settings (scm_t_locale_settings *settings)
  251. {
  252. const char *locale_name;
  253. #define SCM_DEFINE_LOCALE_CATEGORY(_name) \
  254. { \
  255. SCM_SYSCALL (locale_name = setlocale (LC_ ## _name, NULL)); \
  256. if (locale_name == NULL) \
  257. goto handle_error; \
  258. \
  259. settings-> _name = strdup (locale_name); \
  260. if (settings-> _name == NULL) \
  261. goto handle_oom; \
  262. }
  263. #include "locale-categories.h"
  264. #undef SCM_DEFINE_LOCALE_CATEGORY
  265. return 0;
  266. handle_error:
  267. return EINVAL;
  268. handle_oom:
  269. return ENOMEM;
  270. }
  271. /* Restore locale settings SETTINGS. On success, return zero. */
  272. static int
  273. restore_locale_settings (const scm_t_locale_settings *settings)
  274. {
  275. const char *result;
  276. #define SCM_DEFINE_LOCALE_CATEGORY(_name) \
  277. SCM_SYSCALL (result = setlocale (LC_ ## _name, settings-> _name)); \
  278. if (result == NULL) \
  279. goto handle_error;
  280. #include "locale-categories.h"
  281. #undef SCM_DEFINE_LOCALE_CATEGORY
  282. return 0;
  283. handle_error:
  284. return EINVAL;
  285. }
  286. /* Free memory associated with SETTINGS. */
  287. static void
  288. free_locale_settings (scm_t_locale_settings *settings)
  289. {
  290. #define SCM_DEFINE_LOCALE_CATEGORY(_name) \
  291. free (settings-> _name); \
  292. settings->_name = NULL;
  293. #include "locale-categories.h"
  294. #undef SCM_DEFINE_LOCALE_CATEGORY
  295. }
  296. /* Install the locale named LOCALE_NAME for all the categories listed in
  297. CATEGORY_MASK. */
  298. static int
  299. install_locale_categories (const char *locale_name, int category_mask)
  300. {
  301. const char *result;
  302. if (category_mask == LC_ALL_MASK)
  303. {
  304. SCM_SYSCALL (result = setlocale (LC_ALL, locale_name));
  305. if (result == NULL)
  306. goto handle_error;
  307. }
  308. else
  309. {
  310. #define SCM_DEFINE_LOCALE_CATEGORY(_name) \
  311. if (category_mask & SCM_LOCALE_CATEGORY_MASK (_name)) \
  312. { \
  313. SCM_SYSCALL (result = setlocale (LC_ ## _name, locale_name)); \
  314. if (result == NULL) \
  315. goto handle_error; \
  316. }
  317. #include "locale-categories.h"
  318. #undef SCM_DEFINE_LOCALE_CATEGORY
  319. }
  320. return 0;
  321. handle_error:
  322. return EINVAL;
  323. }
  324. /* Install LOCALE, recursively installing its base locales first. On
  325. success, zero is returned. */
  326. static int
  327. install_locale (scm_t_locale locale)
  328. {
  329. scm_t_locale stack[LOCALE_STACK_SIZE_MAX];
  330. int category_mask = 0;
  331. size_t stack_size = 0;
  332. int stack_offset = 0;
  333. const char *result = NULL;
  334. /* Build up a locale stack by traversing the `base_locale' link. */
  335. do
  336. {
  337. if (stack_size >= LOCALE_STACK_SIZE_MAX)
  338. /* We cannot use `scm_error ()' here because otherwise the locale
  339. mutex may remain locked. */
  340. return EINVAL;
  341. stack[stack_size++] = locale;
  342. /* Keep track of which categories have already been taken into
  343. account. */
  344. category_mask |= locale->category_mask;
  345. if (!SCM_UNBNDP (locale->base_locale))
  346. locale = (scm_t_locale) SCM_SMOB_DATA (locale->base_locale);
  347. else
  348. locale = NULL;
  349. }
  350. while ((locale != NULL) && (category_mask != LC_ALL_MASK));
  351. /* Install the C locale to start from a pristine state. */
  352. SCM_SYSCALL (result = setlocale (LC_ALL, "C"));
  353. if (result == NULL)
  354. goto handle_error;
  355. /* Install the locales in reverse order. */
  356. for (stack_offset = stack_size - 1;
  357. stack_offset >= 0;
  358. stack_offset--)
  359. {
  360. int err;
  361. scm_t_locale locale;
  362. locale = stack[stack_offset];
  363. err = install_locale_categories (locale->locale_name,
  364. locale->category_mask);
  365. if (err)
  366. goto handle_error;
  367. }
  368. return 0;
  369. handle_error:
  370. return EINVAL;
  371. }
  372. /* Leave the locked locale section. */
  373. static inline void
  374. leave_locale_section (const scm_t_locale_settings *settings)
  375. {
  376. /* Restore the previous locale settings. */
  377. (void)restore_locale_settings (settings);
  378. unlock_locale_mutex ();
  379. }
  380. /* Enter a locked locale section. */
  381. static inline int
  382. enter_locale_section (scm_t_locale locale,
  383. scm_t_locale_settings *prev_locale)
  384. {
  385. int err;
  386. lock_locale_mutex ();
  387. err = get_current_locale_settings (prev_locale);
  388. if (err)
  389. {
  390. unlock_locale_mutex ();
  391. return err;
  392. }
  393. err = install_locale (locale);
  394. if (err)
  395. {
  396. leave_locale_section (prev_locale);
  397. free_locale_settings (prev_locale);
  398. }
  399. return err;
  400. }
  401. /* Convenient macro to run STATEMENT in the locale context of C_LOCALE. */
  402. #define RUN_IN_LOCALE_SECTION(_c_locale, _statement) \
  403. do \
  404. { \
  405. int lsec_err; \
  406. scm_t_locale_settings lsec_prev_locale; \
  407. \
  408. lsec_err = enter_locale_section ((_c_locale), &lsec_prev_locale); \
  409. if (lsec_err) \
  410. scm_locale_error (FUNC_NAME, lsec_err); \
  411. else \
  412. { \
  413. _statement ; \
  414. \
  415. leave_locale_section (&lsec_prev_locale); \
  416. free_locale_settings (&lsec_prev_locale); \
  417. } \
  418. } \
  419. while (0)
  420. /* Convert the current locale settings into a locale SMOB. On success, zero
  421. is returned and RESULT points to the new SMOB. Otherwise, an error is
  422. returned. */
  423. static int
  424. get_current_locale (SCM *result)
  425. {
  426. int err = 0;
  427. scm_t_locale c_locale;
  428. const char *current_locale;
  429. c_locale = scm_gc_malloc (sizeof (* c_locale), "locale");
  430. lock_locale_mutex ();
  431. c_locale->category_mask = LC_ALL_MASK;
  432. c_locale->base_locale = SCM_UNDEFINED;
  433. current_locale = setlocale (LC_ALL, NULL);
  434. if (current_locale != NULL)
  435. c_locale->locale_name = scm_gc_strdup (current_locale, "locale");
  436. else
  437. err = EINVAL;
  438. unlock_locale_mutex ();
  439. if (err == 0)
  440. SCM_NEWSMOB (*result, scm_tc16_locale_smob_type, c_locale);
  441. else
  442. *result = SCM_BOOL_F;
  443. return err;
  444. }
  445. #else /* USE_GNU_LOCALE_API */
  446. /* Convenient macro to run STATEMENT in the locale context of C_LOCALE. */
  447. #define RUN_IN_LOCALE_SECTION(_c_locale, _statement) \
  448. do \
  449. { \
  450. scm_t_locale old_loc; \
  451. \
  452. old_loc = uselocale (_c_locale); \
  453. _statement ; \
  454. uselocale (old_loc); \
  455. } \
  456. while (0)
  457. #endif /* USE_GNU_LOCALE_API */
  458. /* `make-locale' can take either category lists or single categories (the
  459. `LC_*' integer constants). */
  460. #define SCM_LIST_OR_INTEGER_P(arg) \
  461. (scm_is_integer (arg) || scm_is_true (scm_list_p (arg)))
  462. /* Return the category mask corresponding to CATEGORY (an `LC_' integer
  463. constant). */
  464. static inline int
  465. category_to_category_mask (SCM category,
  466. const char *func_name, int pos)
  467. {
  468. int c_category;
  469. int c_category_mask;
  470. c_category = scm_to_int (category);
  471. #define SCM_DEFINE_LOCALE_CATEGORY(_name) \
  472. case LC_ ## _name: \
  473. c_category_mask = SCM_LOCALE_CATEGORY_MASK (_name); \
  474. break;
  475. switch (c_category)
  476. {
  477. #include "locale-categories.h"
  478. case LC_ALL:
  479. c_category_mask = LC_ALL_MASK;
  480. break;
  481. default:
  482. scm_wrong_type_arg_msg (func_name, pos, category,
  483. "locale category");
  484. }
  485. #undef SCM_DEFINE_LOCALE_CATEGORY
  486. return c_category_mask;
  487. }
  488. /* Convert CATEGORIES, a list of locale categories or a single category (an
  489. integer), into a category mask. */
  490. static int
  491. category_list_to_category_mask (SCM categories,
  492. const char *func_name, int pos)
  493. {
  494. int c_category_mask = 0;
  495. if (scm_is_integer (categories))
  496. c_category_mask = category_to_category_mask (categories,
  497. func_name, pos);
  498. else
  499. for (; !scm_is_null (categories); categories = SCM_CDR (categories))
  500. {
  501. SCM category = SCM_CAR (categories);
  502. c_category_mask |=
  503. category_to_category_mask (category, func_name, pos);
  504. }
  505. return c_category_mask;
  506. }
  507. SCM_DEFINE (scm_make_locale, "make-locale", 2, 1, 0,
  508. (SCM category_list, SCM locale_name, SCM base_locale),
  509. "Return a reference to a data structure representing a set of "
  510. "locale datasets. @var{category_list} should be either a list "
  511. "of locale categories or a single category as used with "
  512. "@code{setlocale} (@pxref{Locales, @code{setlocale}}) and "
  513. "@var{locale_name} should be the name of the locale considered "
  514. "(e.g., @code{\"sl_SI\"}). Optionally, if @code{base_locale} is "
  515. "passed, it should be a locale object denoting settings for "
  516. "categories not listed in @var{category_list}.")
  517. #define FUNC_NAME s_scm_make_locale
  518. {
  519. SCM locale = SCM_BOOL_F;
  520. int err = 0;
  521. int c_category_mask;
  522. char *c_locale_name;
  523. scm_t_locale c_base_locale, c_locale;
  524. SCM_MAKE_VALIDATE (1, category_list, LIST_OR_INTEGER_P);
  525. SCM_VALIDATE_STRING (2, locale_name);
  526. SCM_VALIDATE_OPTIONAL_LOCALE_COPY (3, base_locale, c_base_locale);
  527. c_category_mask = category_list_to_category_mask (category_list,
  528. FUNC_NAME, 1);
  529. c_locale_name = scm_to_locale_string (locale_name);
  530. #ifdef USE_GNU_LOCALE_API
  531. if (scm_is_eq (base_locale, SCM_VARIABLE_REF (scm_global_locale)))
  532. c_base_locale = LC_GLOBAL_LOCALE;
  533. if (c_base_locale != (locale_t) 0)
  534. {
  535. /* C_BASE_LOCALE is to be consumed by `newlocale ()' so it needs to be
  536. duplicated before. */
  537. c_base_locale = duplocale (c_base_locale);
  538. if (c_base_locale == (locale_t) 0)
  539. {
  540. err = errno;
  541. goto fail;
  542. }
  543. }
  544. c_locale = newlocale (c_category_mask, c_locale_name, c_base_locale);
  545. free (c_locale_name);
  546. c_locale_name = NULL;
  547. if (c_locale == (locale_t) 0)
  548. {
  549. if (c_base_locale != (locale_t) 0)
  550. freelocale (c_base_locale);
  551. scm_locale_error (FUNC_NAME, errno);
  552. }
  553. else
  554. SCM_NEWSMOB (locale, scm_tc16_locale_smob_type, c_locale);
  555. #else
  556. c_locale = scm_gc_malloc (sizeof (* c_locale), "locale");
  557. c_locale->category_mask = c_category_mask;
  558. c_locale->locale_name = scm_gc_strdup (c_locale_name, "locale");
  559. free (c_locale_name);
  560. c_locale_name = NULL;
  561. if (scm_is_eq (base_locale, SCM_VARIABLE_REF (scm_global_locale)))
  562. {
  563. /* Get the current locale settings and turn them into a locale
  564. object. */
  565. err = get_current_locale (&base_locale);
  566. if (err)
  567. goto fail;
  568. }
  569. c_locale->base_locale = base_locale;
  570. {
  571. /* Try out the new locale and raise an exception if it doesn't work. */
  572. int err;
  573. scm_t_locale_settings prev_locale;
  574. err = enter_locale_section (c_locale, &prev_locale);
  575. if (err)
  576. goto fail;
  577. else
  578. {
  579. leave_locale_section (&prev_locale);
  580. SCM_NEWSMOB (locale, scm_tc16_locale_smob_type, c_locale);
  581. }
  582. }
  583. /* silence gcc's unused variable warning */
  584. (void) c_base_locale;
  585. #endif
  586. return locale;
  587. fail:
  588. #ifndef USE_GNU_LOCALE_API
  589. scm_gc_free (c_locale, sizeof (* c_locale), "locale");
  590. #endif
  591. free (c_locale_name);
  592. scm_locale_error (FUNC_NAME, err);
  593. return SCM_BOOL_F;
  594. }
  595. #undef FUNC_NAME
  596. SCM_DEFINE (scm_locale_p, "locale?", 1, 0, 0,
  597. (SCM obj),
  598. "Return true if @var{obj} is a locale object.")
  599. #define FUNC_NAME s_scm_locale_p
  600. {
  601. return scm_from_bool (SCM_SMOB_PREDICATE (scm_tc16_locale_smob_type, obj));
  602. }
  603. #undef FUNC_NAME
  604. /* Locale-dependent string comparison.
  605. A similar API can be found in MzScheme starting from version 200:
  606. http://download.plt-scheme.org/chronology/mzmr200alpha14.html . */
  607. #define SCM_STRING_TO_U32_BUF(str, c_str, c_str_malloc_p) \
  608. do \
  609. { \
  610. if (scm_i_is_narrow_string (str)) \
  611. { \
  612. size_t i, len, bytes; \
  613. const char *buf = scm_i_string_chars (str); \
  614. \
  615. len = scm_i_string_length (str); \
  616. bytes = (len + 1) * sizeof (scm_t_wchar); \
  617. c_str_malloc_p = (bytes > SCM_MAX_ALLOCA); \
  618. c_str = c_str_malloc_p ? malloc (bytes) : alloca (bytes); \
  619. \
  620. for (i = 0; i < len; i ++) \
  621. c_str[i] = (unsigned char ) buf[i]; \
  622. c_str[len] = 0; \
  623. } \
  624. else \
  625. { \
  626. c_str_malloc_p = 0; \
  627. c_str = (scm_t_wchar *) scm_i_string_wide_chars (str); \
  628. } \
  629. } while (0)
  630. #define SCM_CLEANUP_U32_BUF(c_str, c_str_malloc_p) \
  631. do \
  632. { \
  633. if (c_str_malloc_p) \
  634. free (c_str); \
  635. } while (0)
  636. /* Compare UTF-32 strings according to LOCALE. Returns a negative value if
  637. S1 compares smaller than S2, a positive value if S1 compares larger than
  638. S2, or 0 if they compare equal. */
  639. static inline int
  640. compare_u32_strings (SCM s1, SCM s2, SCM locale, const char *func_name)
  641. #define FUNC_NAME func_name
  642. {
  643. int result;
  644. scm_t_locale c_locale;
  645. scm_t_wchar *c_s1, *c_s2;
  646. int c_s1_malloc_p, c_s2_malloc_p;
  647. SCM_VALIDATE_OPTIONAL_LOCALE_COPY (3, locale, c_locale);
  648. SCM_STRING_TO_U32_BUF (s1, c_s1, c_s1_malloc_p);
  649. SCM_STRING_TO_U32_BUF (s2, c_s2, c_s2_malloc_p);
  650. if (c_locale)
  651. RUN_IN_LOCALE_SECTION (c_locale,
  652. result = u32_strcoll ((const uint32_t *) c_s1,
  653. (const uint32_t *) c_s2));
  654. else
  655. result = u32_strcoll ((const uint32_t *) c_s1,
  656. (const uint32_t *) c_s2);
  657. SCM_CLEANUP_U32_BUF(c_s1, c_s1_malloc_p);
  658. SCM_CLEANUP_U32_BUF(c_s2, c_s2_malloc_p);
  659. scm_remember_upto_here_2 (s1, s2);
  660. scm_remember_upto_here (locale);
  661. return result;
  662. }
  663. #undef FUNC_NAME
  664. /* Return the current language of the locale. */
  665. static const char *
  666. locale_language ()
  667. {
  668. /* Note: If the locale has been set with 'uselocale', uc_locale_language
  669. from libunistring versions 0.9.1 and older will return the incorrect
  670. (non-thread-specific) locale. This is fixed in versions 0.9.2 and
  671. newer. */
  672. return uc_locale_language ();
  673. }
  674. static inline int
  675. u32_locale_casecoll (const char *func_name, const uint32_t *c_s1,
  676. const uint32_t *c_s2,
  677. int *result)
  678. {
  679. /* Note: Since this is called from `RUN_IN_LOCALE_SECTION', it must note
  680. make any non-local exit. */
  681. int ret;
  682. const char *loc = locale_language ();
  683. ret = u32_casecoll (c_s1, u32_strlen (c_s1),
  684. c_s2, u32_strlen (c_s2),
  685. loc, UNINORM_NFC, result);
  686. return ret == 0 ? ret : errno;
  687. }
  688. static inline int
  689. compare_u32_strings_ci (SCM s1, SCM s2, SCM locale, const char *func_name)
  690. #define FUNC_NAME func_name
  691. {
  692. int result, ret = 0;
  693. scm_t_locale c_locale;
  694. scm_t_wchar *c_s1, *c_s2;
  695. int c_s1_malloc_p, c_s2_malloc_p;
  696. SCM_VALIDATE_OPTIONAL_LOCALE_COPY (3, locale, c_locale);
  697. SCM_STRING_TO_U32_BUF (s1, c_s1, c_s1_malloc_p);
  698. SCM_STRING_TO_U32_BUF (s2, c_s2, c_s2_malloc_p);
  699. if (c_locale)
  700. RUN_IN_LOCALE_SECTION
  701. (c_locale,
  702. ret = u32_locale_casecoll (func_name,
  703. (const uint32_t *) c_s1,
  704. (const uint32_t *) c_s2,
  705. &result));
  706. else
  707. ret = u32_locale_casecoll (func_name,
  708. (const uint32_t *) c_s1,
  709. (const uint32_t *) c_s2,
  710. &result);
  711. SCM_CLEANUP_U32_BUF(c_s1, c_s1_malloc_p);
  712. SCM_CLEANUP_U32_BUF(c_s2, c_s2_malloc_p);
  713. if (SCM_UNLIKELY (ret != 0))
  714. {
  715. errno = ret;
  716. scm_syserror (FUNC_NAME);
  717. }
  718. scm_remember_upto_here_2 (s1, s2);
  719. scm_remember_upto_here (locale);
  720. return result;
  721. }
  722. #undef FUNC_NAME
  723. SCM_DEFINE (scm_string_locale_lt, "string-locale<?", 2, 1, 0,
  724. (SCM s1, SCM s2, SCM locale),
  725. "Compare strings @var{s1} and @var{s2} in a locale-dependent way."
  726. "If @var{locale} is provided, it should be locale object (as "
  727. "returned by @code{make-locale}) and will be used to perform the "
  728. "comparison; otherwise, the current system locale is used.")
  729. #define FUNC_NAME s_scm_string_locale_lt
  730. {
  731. int result;
  732. SCM_VALIDATE_STRING (1, s1);
  733. SCM_VALIDATE_STRING (2, s2);
  734. result = compare_u32_strings (s1, s2, locale, FUNC_NAME);
  735. return scm_from_bool (result < 0);
  736. }
  737. #undef FUNC_NAME
  738. SCM_DEFINE (scm_string_locale_gt, "string-locale>?", 2, 1, 0,
  739. (SCM s1, SCM s2, SCM locale),
  740. "Compare strings @var{s1} and @var{s2} in a locale-dependent way."
  741. "If @var{locale} is provided, it should be locale object (as "
  742. "returned by @code{make-locale}) and will be used to perform the "
  743. "comparison; otherwise, the current system locale is used.")
  744. #define FUNC_NAME s_scm_string_locale_gt
  745. {
  746. int result;
  747. SCM_VALIDATE_STRING (1, s1);
  748. SCM_VALIDATE_STRING (2, s2);
  749. result = compare_u32_strings (s1, s2, locale, FUNC_NAME);
  750. return scm_from_bool (result > 0);
  751. }
  752. #undef FUNC_NAME
  753. SCM_DEFINE (scm_string_locale_ci_lt, "string-locale-ci<?", 2, 1, 0,
  754. (SCM s1, SCM s2, SCM locale),
  755. "Compare strings @var{s1} and @var{s2} in a case-insensitive, "
  756. "and locale-dependent way. If @var{locale} is provided, it "
  757. "should be locale object (as returned by @code{make-locale}) "
  758. "and will be used to perform the comparison; otherwise, the "
  759. "current system locale is used.")
  760. #define FUNC_NAME s_scm_string_locale_ci_lt
  761. {
  762. int result;
  763. SCM_VALIDATE_STRING (1, s1);
  764. SCM_VALIDATE_STRING (2, s2);
  765. result = compare_u32_strings_ci (s1, s2, locale, FUNC_NAME);
  766. return scm_from_bool (result < 0);
  767. }
  768. #undef FUNC_NAME
  769. SCM_DEFINE (scm_string_locale_ci_gt, "string-locale-ci>?", 2, 1, 0,
  770. (SCM s1, SCM s2, SCM locale),
  771. "Compare strings @var{s1} and @var{s2} in a case-insensitive, "
  772. "and locale-dependent way. If @var{locale} is provided, it "
  773. "should be locale object (as returned by @code{make-locale}) "
  774. "and will be used to perform the comparison; otherwise, the "
  775. "current system locale is used.")
  776. #define FUNC_NAME s_scm_string_locale_ci_gt
  777. {
  778. int result;
  779. SCM_VALIDATE_STRING (1, s1);
  780. SCM_VALIDATE_STRING (2, s2);
  781. result = compare_u32_strings_ci (s1, s2, locale, FUNC_NAME);
  782. return scm_from_bool (result > 0);
  783. }
  784. #undef FUNC_NAME
  785. SCM_DEFINE (scm_string_locale_ci_eq, "string-locale-ci=?", 2, 1, 0,
  786. (SCM s1, SCM s2, SCM locale),
  787. "Compare strings @var{s1} and @var{s2} in a case-insensitive, "
  788. "and locale-dependent way. If @var{locale} is provided, it "
  789. "should be locale object (as returned by @code{make-locale}) "
  790. "and will be used to perform the comparison; otherwise, the "
  791. "current system locale is used.")
  792. #define FUNC_NAME s_scm_string_locale_ci_eq
  793. {
  794. int result;
  795. SCM_VALIDATE_STRING (1, s1);
  796. SCM_VALIDATE_STRING (2, s2);
  797. result = compare_u32_strings_ci (s1, s2, locale, FUNC_NAME);
  798. return scm_from_bool (result == 0);
  799. }
  800. #undef FUNC_NAME
  801. SCM_DEFINE (scm_char_locale_lt, "char-locale<?", 2, 1, 0,
  802. (SCM c1, SCM c2, SCM locale),
  803. "Return true if character @var{c1} is lower than @var{c2} "
  804. "according to @var{locale} or to the current locale.")
  805. #define FUNC_NAME s_scm_char_locale_lt
  806. {
  807. int result;
  808. SCM_VALIDATE_CHAR (1, c1);
  809. SCM_VALIDATE_CHAR (2, c2);
  810. result = compare_u32_strings (scm_string (scm_list_1 (c1)),
  811. scm_string (scm_list_1 (c2)),
  812. locale, FUNC_NAME);
  813. return scm_from_bool (result < 0);
  814. }
  815. #undef FUNC_NAME
  816. SCM_DEFINE (scm_char_locale_gt, "char-locale>?", 2, 1, 0,
  817. (SCM c1, SCM c2, SCM locale),
  818. "Return true if character @var{c1} is greater than @var{c2} "
  819. "according to @var{locale} or to the current locale.")
  820. #define FUNC_NAME s_scm_char_locale_gt
  821. {
  822. int result;
  823. SCM_VALIDATE_CHAR (1, c1);
  824. SCM_VALIDATE_CHAR (2, c2);
  825. result = compare_u32_strings (scm_string (scm_list_1 (c1)),
  826. scm_string (scm_list_1 (c2)),
  827. locale, FUNC_NAME);
  828. return scm_from_bool (result > 0);
  829. }
  830. #undef FUNC_NAME
  831. SCM_DEFINE (scm_char_locale_ci_lt, "char-locale-ci<?", 2, 1, 0,
  832. (SCM c1, SCM c2, SCM locale),
  833. "Return true if character @var{c1} is lower than @var{c2}, "
  834. "in a case insensitive way according to @var{locale} or to "
  835. "the current locale.")
  836. #define FUNC_NAME s_scm_char_locale_ci_lt
  837. {
  838. int result;
  839. SCM_VALIDATE_CHAR (1, c1);
  840. SCM_VALIDATE_CHAR (2, c2);
  841. result = compare_u32_strings_ci (scm_string (scm_list_1 (c1)),
  842. scm_string (scm_list_1 (c2)),
  843. locale, FUNC_NAME);
  844. return scm_from_bool (result < 0);
  845. }
  846. #undef FUNC_NAME
  847. SCM_DEFINE (scm_char_locale_ci_gt, "char-locale-ci>?", 2, 1, 0,
  848. (SCM c1, SCM c2, SCM locale),
  849. "Return true if character @var{c1} is greater than @var{c2}, "
  850. "in a case insensitive way according to @var{locale} or to "
  851. "the current locale.")
  852. #define FUNC_NAME s_scm_char_locale_ci_gt
  853. {
  854. int result;
  855. SCM_VALIDATE_CHAR (1, c1);
  856. SCM_VALIDATE_CHAR (2, c2);
  857. result = compare_u32_strings_ci (scm_string (scm_list_1 (c1)),
  858. scm_string (scm_list_1 (c2)),
  859. locale, FUNC_NAME);
  860. return scm_from_bool (result > 0);
  861. }
  862. #undef FUNC_NAME
  863. SCM_DEFINE (scm_char_locale_ci_eq, "char-locale-ci=?", 2, 1, 0,
  864. (SCM c1, SCM c2, SCM locale),
  865. "Return true if character @var{c1} is equal to @var{c2}, "
  866. "in a case insensitive way according to @var{locale} or to "
  867. "the current locale.")
  868. #define FUNC_NAME s_scm_char_locale_ci_eq
  869. {
  870. int result;
  871. SCM_VALIDATE_CHAR (1, c1);
  872. SCM_VALIDATE_CHAR (2, c2);
  873. result = compare_u32_strings_ci (scm_string (scm_list_1 (c1)),
  874. scm_string (scm_list_1 (c2)),
  875. locale, FUNC_NAME);
  876. return scm_from_bool (result == 0);
  877. }
  878. #undef FUNC_NAME
  879. /* Locale-dependent alphabetic character mapping. */
  880. static inline int
  881. u32_locale_tocase (const uint32_t *c_s1, size_t len,
  882. uint32_t **p_c_s2, size_t * p_len2,
  883. uint32_t *(*func) (const uint32_t *, size_t,
  884. const char *, uninorm_t,
  885. uint32_t *, size_t *))
  886. {
  887. /* Note: Since this is called from `RUN_IN_LOCALE_SECTION', it must not
  888. make any non-local exit. */
  889. uint32_t *ret;
  890. const char *loc = locale_language ();
  891. /* The first NULL here indicates that no NFC or NFKC normalization
  892. is done. The second NULL means the return buffer is
  893. malloc'ed here. */
  894. ret = func (c_s1, len, loc, NULL, NULL, p_len2);
  895. if (ret == NULL)
  896. {
  897. *p_c_s2 = (uint32_t *) NULL;
  898. *p_len2 = 0;
  899. return errno;
  900. }
  901. *p_c_s2 = ret;
  902. return 0;
  903. }
  904. static SCM
  905. chr_to_case (SCM chr, scm_t_locale c_locale,
  906. uint32_t *(*func) (const uint32_t *, size_t, const char *,
  907. uninorm_t, uint32_t *, size_t *),
  908. const char *func_name,
  909. int *err)
  910. #define FUNC_NAME func_name
  911. {
  912. int ret;
  913. uint32_t c;
  914. uint32_t *convbuf;
  915. size_t convlen;
  916. SCM convchar;
  917. c = SCM_CHAR (chr);
  918. if (c_locale != NULL)
  919. RUN_IN_LOCALE_SECTION (c_locale, ret =
  920. u32_locale_tocase (&c, 1, &convbuf, &convlen, func));
  921. else
  922. ret =
  923. u32_locale_tocase (&c, 1, &convbuf, &convlen, func);
  924. if (SCM_UNLIKELY (ret != 0))
  925. {
  926. *err = ret;
  927. return SCM_BOOL_F;
  928. }
  929. if (convlen == 1)
  930. convchar = SCM_MAKE_CHAR ((scm_t_wchar) convbuf[0]);
  931. else
  932. convchar = chr;
  933. free (convbuf);
  934. return convchar;
  935. }
  936. #undef FUNC_NAME
  937. SCM_DEFINE (scm_char_locale_downcase, "char-locale-downcase", 1, 1, 0,
  938. (SCM chr, SCM locale),
  939. "Return the lowercase character that corresponds to @var{chr} "
  940. "according to either @var{locale} or the current locale.")
  941. #define FUNC_NAME s_scm_char_locale_downcase
  942. {
  943. scm_t_locale c_locale;
  944. SCM ret;
  945. int err = 0;
  946. SCM_VALIDATE_CHAR (1, chr);
  947. SCM_VALIDATE_OPTIONAL_LOCALE_COPY (2, locale, c_locale);
  948. ret = chr_to_case (chr, c_locale, u32_tolower, FUNC_NAME, &err);
  949. if (err != 0)
  950. {
  951. errno = err;
  952. scm_syserror (FUNC_NAME);
  953. }
  954. return ret;
  955. }
  956. #undef FUNC_NAME
  957. SCM_DEFINE (scm_char_locale_upcase, "char-locale-upcase", 1, 1, 0,
  958. (SCM chr, SCM locale),
  959. "Return the uppercase character that corresponds to @var{chr} "
  960. "according to either @var{locale} or the current locale.")
  961. #define FUNC_NAME s_scm_char_locale_upcase
  962. {
  963. scm_t_locale c_locale;
  964. SCM ret;
  965. int err = 0;
  966. SCM_VALIDATE_CHAR (1, chr);
  967. SCM_VALIDATE_OPTIONAL_LOCALE_COPY (2, locale, c_locale);
  968. ret = chr_to_case (chr, c_locale, u32_toupper, FUNC_NAME, &err);
  969. if (err != 0)
  970. {
  971. errno = err;
  972. scm_syserror (FUNC_NAME);
  973. }
  974. return ret;
  975. }
  976. #undef FUNC_NAME
  977. SCM_DEFINE (scm_char_locale_titlecase, "char-locale-titlecase", 1, 1, 0,
  978. (SCM chr, SCM locale),
  979. "Return the titlecase character that corresponds to @var{chr} "
  980. "according to either @var{locale} or the current locale.")
  981. #define FUNC_NAME s_scm_char_locale_titlecase
  982. {
  983. scm_t_locale c_locale;
  984. SCM ret;
  985. int err = 0;
  986. SCM_VALIDATE_CHAR (1, chr);
  987. SCM_VALIDATE_OPTIONAL_LOCALE_COPY (2, locale, c_locale);
  988. ret = chr_to_case (chr, c_locale, u32_totitle, FUNC_NAME, &err);
  989. if (err != 0)
  990. {
  991. errno = err;
  992. scm_syserror (FUNC_NAME);
  993. }
  994. return ret;
  995. }
  996. #undef FUNC_NAME
  997. static SCM
  998. str_to_case (SCM str, scm_t_locale c_locale,
  999. uint32_t *(*func) (const uint32_t *, size_t, const char *,
  1000. uninorm_t, uint32_t *, size_t *),
  1001. const char *func_name,
  1002. int *err)
  1003. #define FUNC_NAME func_name
  1004. {
  1005. scm_t_wchar *c_str, *c_buf;
  1006. uint32_t *c_convstr;
  1007. size_t len, convlen;
  1008. int ret, c_str_malloc_p;
  1009. SCM convstr;
  1010. len = scm_i_string_length (str);
  1011. if (len == 0)
  1012. return scm_nullstr;
  1013. SCM_STRING_TO_U32_BUF (str, c_str, c_str_malloc_p);
  1014. if (c_locale)
  1015. RUN_IN_LOCALE_SECTION (c_locale, ret =
  1016. u32_locale_tocase ((uint32_t *) c_str, len,
  1017. &c_convstr,
  1018. &convlen, func));
  1019. else
  1020. ret =
  1021. u32_locale_tocase ((uint32_t *) c_str, len,
  1022. &c_convstr, &convlen, func);
  1023. SCM_CLEANUP_U32_BUF(c_str, c_str_malloc_p);
  1024. scm_remember_upto_here (str);
  1025. if (SCM_UNLIKELY (ret != 0))
  1026. {
  1027. *err = ret;
  1028. return SCM_BOOL_F;
  1029. }
  1030. convstr = scm_i_make_wide_string (convlen, &c_buf, 0);
  1031. memcpy (c_buf, c_convstr, convlen * sizeof (scm_t_wchar));
  1032. free (c_convstr);
  1033. scm_i_try_narrow_string (convstr);
  1034. return convstr;
  1035. }
  1036. #undef FUNC_NAME
  1037. SCM_DEFINE (scm_string_locale_upcase, "string-locale-upcase", 1, 1, 0,
  1038. (SCM str, SCM locale),
  1039. "Return a new string that is the uppercase version of "
  1040. "@var{str} according to either @var{locale} or the current "
  1041. "locale.")
  1042. #define FUNC_NAME s_scm_string_locale_upcase
  1043. {
  1044. scm_t_locale c_locale;
  1045. SCM ret;
  1046. int err = 0;
  1047. SCM_VALIDATE_STRING (1, str);
  1048. SCM_VALIDATE_OPTIONAL_LOCALE_COPY (2, locale, c_locale);
  1049. ret = str_to_case (str, c_locale, u32_toupper, FUNC_NAME, &err);
  1050. if (err != 0)
  1051. {
  1052. errno = err;
  1053. scm_syserror (FUNC_NAME);
  1054. }
  1055. return ret;
  1056. }
  1057. #undef FUNC_NAME
  1058. SCM_DEFINE (scm_string_locale_downcase, "string-locale-downcase", 1, 1, 0,
  1059. (SCM str, SCM locale),
  1060. "Return a new string that is the down-case version of "
  1061. "@var{str} according to either @var{locale} or the current "
  1062. "locale.")
  1063. #define FUNC_NAME s_scm_string_locale_downcase
  1064. {
  1065. scm_t_locale c_locale;
  1066. SCM ret;
  1067. int err = 0;
  1068. SCM_VALIDATE_STRING (1, str);
  1069. SCM_VALIDATE_OPTIONAL_LOCALE_COPY (2, locale, c_locale);
  1070. ret = str_to_case (str, c_locale, u32_tolower, FUNC_NAME, &err);
  1071. if (err != 0)
  1072. {
  1073. errno = err;
  1074. scm_syserror (FUNC_NAME);
  1075. }
  1076. return ret;
  1077. }
  1078. #undef FUNC_NAME
  1079. SCM_DEFINE (scm_string_locale_titlecase, "string-locale-titlecase", 1, 1, 0,
  1080. (SCM str, SCM locale),
  1081. "Return a new string that is the title-case version of "
  1082. "@var{str} according to either @var{locale} or the current "
  1083. "locale.")
  1084. #define FUNC_NAME s_scm_string_locale_titlecase
  1085. {
  1086. scm_t_locale c_locale;
  1087. SCM ret;
  1088. int err = 0;
  1089. SCM_VALIDATE_STRING (1, str);
  1090. SCM_VALIDATE_OPTIONAL_LOCALE_COPY (2, locale, c_locale);
  1091. ret = str_to_case (str, c_locale, u32_totitle, FUNC_NAME, &err);
  1092. if (err != 0)
  1093. {
  1094. errno = err;
  1095. scm_syserror (FUNC_NAME);
  1096. }
  1097. return ret;
  1098. }
  1099. #undef FUNC_NAME
  1100. /* Note: We don't provide mutative versions of `string-locale-(up|down)case'
  1101. because, in some languages, a single downcase character maps to a couple
  1102. of uppercase characters. Read the SRFI-13 document for a detailed
  1103. discussion about this. */
  1104. /* Locale-dependent number parsing. */
  1105. SCM_DEFINE (scm_locale_string_to_integer, "locale-string->integer",
  1106. 1, 2, 0, (SCM str, SCM base, SCM locale),
  1107. "Convert string @var{str} into an integer according to either "
  1108. "@var{locale} (a locale object as returned by @code{make-locale}) "
  1109. "or the current process locale. Return two values: an integer "
  1110. "(on success) or @code{#f}, and the number of characters read "
  1111. "from @var{str} (@code{0} on failure).")
  1112. #define FUNC_NAME s_scm_locale_string_to_integer
  1113. {
  1114. SCM result;
  1115. long c_result;
  1116. int c_base;
  1117. const char *c_str;
  1118. char *c_endptr;
  1119. scm_t_locale c_locale;
  1120. SCM_VALIDATE_STRING (1, str);
  1121. c_str = scm_i_string_chars (str);
  1122. if (!scm_is_eq (base, SCM_UNDEFINED))
  1123. SCM_VALIDATE_INT_COPY (2, base, c_base);
  1124. else
  1125. c_base = 10;
  1126. SCM_VALIDATE_OPTIONAL_LOCALE_COPY (3, locale, c_locale);
  1127. if (c_locale != NULL)
  1128. {
  1129. #if defined USE_GNU_LOCALE_API && defined HAVE_STRTOL_L
  1130. c_result = strtol_l (c_str, &c_endptr, c_base, c_locale);
  1131. #else
  1132. RUN_IN_LOCALE_SECTION (c_locale,
  1133. c_result = strtol (c_str, &c_endptr, c_base));
  1134. #endif
  1135. }
  1136. else
  1137. c_result = strtol (c_str, &c_endptr, c_base);
  1138. scm_remember_upto_here (str);
  1139. if (c_endptr == c_str)
  1140. result = SCM_BOOL_F;
  1141. else
  1142. result = scm_from_long (c_result);
  1143. return scm_values_2 (result, scm_from_long (c_endptr - c_str));
  1144. }
  1145. #undef FUNC_NAME
  1146. SCM_DEFINE (scm_locale_string_to_inexact, "locale-string->inexact",
  1147. 1, 1, 0, (SCM str, SCM locale),
  1148. "Convert string @var{str} into an inexact number according to "
  1149. "either @var{locale} (a locale object as returned by "
  1150. "@code{make-locale}) or the current process locale. Return "
  1151. "two values: an inexact number (on success) or @code{#f}, and "
  1152. "the number of characters read from @var{str} (@code{0} on "
  1153. "failure).")
  1154. #define FUNC_NAME s_scm_locale_string_to_inexact
  1155. {
  1156. SCM result;
  1157. double c_result;
  1158. const char *c_str;
  1159. char *c_endptr;
  1160. scm_t_locale c_locale;
  1161. SCM_VALIDATE_STRING (1, str);
  1162. c_str = scm_i_string_chars (str);
  1163. SCM_VALIDATE_OPTIONAL_LOCALE_COPY (2, locale, c_locale);
  1164. if (c_locale != NULL)
  1165. {
  1166. #if defined USE_GNU_LOCALE_API && defined HAVE_STRTOD_L
  1167. c_result = strtod_l (c_str, &c_endptr, c_locale);
  1168. #else
  1169. RUN_IN_LOCALE_SECTION (c_locale,
  1170. c_result = strtod (c_str, &c_endptr));
  1171. #endif
  1172. }
  1173. else
  1174. c_result = strtod (c_str, &c_endptr);
  1175. scm_remember_upto_here (str);
  1176. if (c_endptr == c_str)
  1177. result = SCM_BOOL_F;
  1178. else
  1179. result = scm_from_double (c_result);
  1180. return scm_values_2 (result, scm_from_long (c_endptr - c_str));
  1181. }
  1182. #undef FUNC_NAME
  1183. /* Language information, aka. `nl_langinfo ()'. */
  1184. /* FIXME: Issues related to `nl-langinfo'.
  1185. 1. The `CODESET' value is not normalized. This is a secondary issue, but
  1186. still a practical issue. See
  1187. http://www.cl.cam.ac.uk/~mgk25/ucs/norm_charmap.c for codeset
  1188. normalization.
  1189. 2. `nl_langinfo ()' is not available on Windows.
  1190. 3. `nl_langinfo ()' may return strings encoded in a locale different from
  1191. the current one.
  1192. For example:
  1193. (nl-langinfo DAY_1 (make-locale LC_ALL "eo_EO.UTF-8"))
  1194. returns a result that is a UTF-8 string, regardless of the
  1195. setting of the current locale. If nl_langinfo supports CODESET,
  1196. we can convert the string properly using scm_from_stringn. If
  1197. CODESET is not supported, we won't be able to make much sense of
  1198. the returned string.
  1199. Note: We don't use Gnulib's `nl_langinfo' module because it's currently not
  1200. as complete as the compatibility hacks in `i18n.scm'. */
  1201. static char *
  1202. copy_string_or_null (const char *s)
  1203. {
  1204. if (s == NULL)
  1205. return NULL;
  1206. else
  1207. return strdup (s);
  1208. }
  1209. SCM_DEFINE (scm_nl_langinfo, "nl-langinfo", 1, 1, 0,
  1210. (SCM item, SCM locale),
  1211. "Return a string denoting locale information for @var{item} "
  1212. "in the current locale or that specified by @var{locale}. "
  1213. "The semantics and arguments are the same as those of the "
  1214. "X/Open @code{nl_langinfo} function (@pxref{The Elegant and "
  1215. "Fast Way, @code{nl_langinfo},, libc, The GNU C Library "
  1216. "Reference Manual}).")
  1217. #define FUNC_NAME s_scm_nl_langinfo
  1218. {
  1219. SCM result;
  1220. nl_item c_item;
  1221. char *c_result;
  1222. scm_t_locale c_locale;
  1223. char *codeset;
  1224. SCM_VALIDATE_INT_COPY (2, item, c_item);
  1225. SCM_VALIDATE_OPTIONAL_LOCALE_COPY (2, locale, c_locale);
  1226. /* Sadly, `nl_langinfo ()' returns a pointer to a static string. According
  1227. to SuS v2, that static string may be modified by subsequent calls to
  1228. `nl_langinfo ()' as well as by calls to `setlocale ()'. Thus, we must
  1229. acquire the locale mutex before doing invoking `nl_langinfo ()'. See
  1230. http://opengroup.org/onlinepubs/007908799/xsh/nl_langinfo.html for
  1231. details. */
  1232. lock_locale_mutex ();
  1233. if (c_locale != NULL)
  1234. {
  1235. #ifdef USE_GNU_LOCALE_API
  1236. c_result = copy_string_or_null (nl_langinfo_l (c_item, c_locale));
  1237. codeset = copy_string_or_null (nl_langinfo_l (CODESET, c_locale));
  1238. #else /* !USE_GNU_LOCALE_API */
  1239. /* We can't use `RUN_IN_LOCALE_SECTION ()' here because the locale
  1240. mutex is already taken. */
  1241. int lsec_err;
  1242. scm_t_locale_settings lsec_prev_locale;
  1243. lsec_err = get_current_locale_settings (&lsec_prev_locale);
  1244. if (lsec_err)
  1245. unlock_locale_mutex ();
  1246. else
  1247. {
  1248. lsec_err = install_locale (c_locale);
  1249. if (lsec_err)
  1250. {
  1251. leave_locale_section (&lsec_prev_locale);
  1252. free_locale_settings (&lsec_prev_locale);
  1253. }
  1254. }
  1255. if (lsec_err)
  1256. scm_locale_error (FUNC_NAME, lsec_err);
  1257. else
  1258. {
  1259. c_result = copy_string_or_null (nl_langinfo (c_item));
  1260. codeset = copy_string_or_null (nl_langinfo (CODESET));
  1261. restore_locale_settings (&lsec_prev_locale);
  1262. free_locale_settings (&lsec_prev_locale);
  1263. }
  1264. #endif
  1265. }
  1266. else
  1267. {
  1268. c_result = copy_string_or_null (nl_langinfo (c_item));
  1269. codeset = copy_string_or_null (nl_langinfo (CODESET));
  1270. }
  1271. unlock_locale_mutex ();
  1272. if (c_result == NULL)
  1273. result = SCM_BOOL_F;
  1274. else
  1275. {
  1276. switch (c_item)
  1277. {
  1278. #if (defined GROUPING) && (defined MON_GROUPING)
  1279. case GROUPING:
  1280. case MON_GROUPING:
  1281. {
  1282. char *p;
  1283. /* In this cases, the result is to be interpreted as a list
  1284. of numbers. If the last item is `CHAR_MAX' or a negative
  1285. number, it has the special meaning "no more grouping"
  1286. (negative numbers aren't specified in POSIX but can be
  1287. used by glibc; see
  1288. <http://lists.gnu.org/archive/html/bug-guile/2011-02/msg00159.html>). */
  1289. result = SCM_EOL;
  1290. for (p = c_result; (*p > 0) && (*p != CHAR_MAX); p++)
  1291. result = scm_cons (SCM_I_MAKINUM ((int) *p), result);
  1292. {
  1293. SCM last_pair = result;
  1294. result = scm_reverse_x (result, SCM_EOL);
  1295. if (*p == 0)
  1296. {
  1297. /* Cyclic grouping information. */
  1298. if (!scm_is_null (last_pair))
  1299. SCM_SETCDR (last_pair, result);
  1300. }
  1301. }
  1302. free (c_result);
  1303. break;
  1304. }
  1305. #endif
  1306. #if defined FRAC_DIGITS || defined INT_FRAC_DIGITS
  1307. #ifdef FRAC_DIGITS
  1308. case FRAC_DIGITS:
  1309. #endif
  1310. #ifdef INT_FRAC_DIGITS
  1311. case INT_FRAC_DIGITS:
  1312. #endif
  1313. /* This is to be interpreted as a single integer. */
  1314. if (*c_result == CHAR_MAX)
  1315. /* Unspecified. */
  1316. result = SCM_BOOL_F;
  1317. else
  1318. result = SCM_I_MAKINUM (*c_result);
  1319. free (c_result);
  1320. break;
  1321. #endif
  1322. #if defined P_CS_PRECEDES || defined N_CS_PRECEDES || \
  1323. defined INT_P_CS_PRECEDES || defined INT_N_CS_PRECEDES || \
  1324. defined P_SEP_BY_SPACE || defined N_SEP_BY_SPACE || \
  1325. defined INT_P_SEP_BY_SPACE || defined INT_N_SEP_BY_SPACE
  1326. #ifdef P_CS_PRECEDES
  1327. case P_CS_PRECEDES:
  1328. case N_CS_PRECEDES:
  1329. #endif
  1330. #ifdef INT_N_CS_PRECEDES
  1331. case INT_P_CS_PRECEDES:
  1332. case INT_N_CS_PRECEDES:
  1333. #endif
  1334. #ifdef P_SEP_BY_SPACE
  1335. case P_SEP_BY_SPACE:
  1336. case N_SEP_BY_SPACE:
  1337. #endif
  1338. #ifdef INT_P_SEP_BY_SPACE
  1339. case INT_P_SEP_BY_SPACE:
  1340. case INT_N_SEP_BY_SPACE:
  1341. #endif
  1342. /* This is to be interpreted as a boolean. */
  1343. result = scm_from_bool (*c_result);
  1344. free (c_result);
  1345. break;
  1346. #endif
  1347. #if defined P_SIGN_POSN || defined N_SIGN_POSN || \
  1348. defined INT_P_SIGN_POSN || defined INT_N_SIGN_POSN
  1349. #ifdef P_SIGN_POSN
  1350. case P_SIGN_POSN:
  1351. case N_SIGN_POSN:
  1352. #endif
  1353. #ifdef INT_P_SIGN_POSN
  1354. case INT_P_SIGN_POSN:
  1355. case INT_N_SIGN_POSN:
  1356. #endif
  1357. /* See `(libc) Sign of Money Amount' for the interpretation of the
  1358. return value here. */
  1359. switch (*c_result)
  1360. {
  1361. case 0:
  1362. result = scm_from_latin1_symbol ("parenthesize");
  1363. break;
  1364. case 1:
  1365. result = scm_from_latin1_symbol ("sign-before");
  1366. break;
  1367. case 2:
  1368. result = scm_from_latin1_symbol ("sign-after");
  1369. break;
  1370. case 3:
  1371. result = scm_from_latin1_symbol ("sign-before-currency-symbol");
  1372. break;
  1373. case 4:
  1374. result = scm_from_latin1_symbol ("sign-after-currency-symbol");
  1375. break;
  1376. default:
  1377. result = scm_from_latin1_symbol ("unspecified");
  1378. }
  1379. free (c_result);
  1380. break;
  1381. #endif
  1382. default:
  1383. result = scm_from_stringn (c_result, strlen (c_result),
  1384. codeset,
  1385. SCM_FAILED_CONVERSION_QUESTION_MARK);
  1386. free (c_result);
  1387. }
  1388. }
  1389. if (codeset != NULL)
  1390. free (codeset);
  1391. return result;
  1392. }
  1393. #undef FUNC_NAME
  1394. /* Define the `nl_item' constants. */
  1395. static inline void
  1396. define_langinfo_items (void)
  1397. {
  1398. #define DEFINE_NLITEM_CONSTANT(_item) \
  1399. scm_c_define (# _item, scm_from_int (_item))
  1400. DEFINE_NLITEM_CONSTANT (CODESET);
  1401. /* Abbreviated days of the week. */
  1402. DEFINE_NLITEM_CONSTANT (ABDAY_1);
  1403. DEFINE_NLITEM_CONSTANT (ABDAY_2);
  1404. DEFINE_NLITEM_CONSTANT (ABDAY_3);
  1405. DEFINE_NLITEM_CONSTANT (ABDAY_4);
  1406. DEFINE_NLITEM_CONSTANT (ABDAY_5);
  1407. DEFINE_NLITEM_CONSTANT (ABDAY_6);
  1408. DEFINE_NLITEM_CONSTANT (ABDAY_7);
  1409. /* Long-named days of the week. */
  1410. DEFINE_NLITEM_CONSTANT (DAY_1); /* Sunday */
  1411. DEFINE_NLITEM_CONSTANT (DAY_2); /* Monday */
  1412. DEFINE_NLITEM_CONSTANT (DAY_3); /* Tuesday */
  1413. DEFINE_NLITEM_CONSTANT (DAY_4); /* Wednesday */
  1414. DEFINE_NLITEM_CONSTANT (DAY_5); /* Thursday */
  1415. DEFINE_NLITEM_CONSTANT (DAY_6); /* Friday */
  1416. DEFINE_NLITEM_CONSTANT (DAY_7); /* Saturday */
  1417. /* Abbreviated month names. */
  1418. DEFINE_NLITEM_CONSTANT (ABMON_1); /* Jan */
  1419. DEFINE_NLITEM_CONSTANT (ABMON_2);
  1420. DEFINE_NLITEM_CONSTANT (ABMON_3);
  1421. DEFINE_NLITEM_CONSTANT (ABMON_4);
  1422. DEFINE_NLITEM_CONSTANT (ABMON_5);
  1423. DEFINE_NLITEM_CONSTANT (ABMON_6);
  1424. DEFINE_NLITEM_CONSTANT (ABMON_7);
  1425. DEFINE_NLITEM_CONSTANT (ABMON_8);
  1426. DEFINE_NLITEM_CONSTANT (ABMON_9);
  1427. DEFINE_NLITEM_CONSTANT (ABMON_10);
  1428. DEFINE_NLITEM_CONSTANT (ABMON_11);
  1429. DEFINE_NLITEM_CONSTANT (ABMON_12);
  1430. /* Long month names. */
  1431. DEFINE_NLITEM_CONSTANT (MON_1); /* January */
  1432. DEFINE_NLITEM_CONSTANT (MON_2);
  1433. DEFINE_NLITEM_CONSTANT (MON_3);
  1434. DEFINE_NLITEM_CONSTANT (MON_4);
  1435. DEFINE_NLITEM_CONSTANT (MON_5);
  1436. DEFINE_NLITEM_CONSTANT (MON_6);
  1437. DEFINE_NLITEM_CONSTANT (MON_7);
  1438. DEFINE_NLITEM_CONSTANT (MON_8);
  1439. DEFINE_NLITEM_CONSTANT (MON_9);
  1440. DEFINE_NLITEM_CONSTANT (MON_10);
  1441. DEFINE_NLITEM_CONSTANT (MON_11);
  1442. DEFINE_NLITEM_CONSTANT (MON_12);
  1443. DEFINE_NLITEM_CONSTANT (AM_STR); /* Ante meridiem string. */
  1444. DEFINE_NLITEM_CONSTANT (PM_STR); /* Post meridiem string. */
  1445. DEFINE_NLITEM_CONSTANT (D_T_FMT); /* Date and time format for strftime. */
  1446. DEFINE_NLITEM_CONSTANT (D_FMT); /* Date format for strftime. */
  1447. DEFINE_NLITEM_CONSTANT (T_FMT); /* Time format for strftime. */
  1448. DEFINE_NLITEM_CONSTANT (T_FMT_AMPM);/* 12-hour time format for strftime. */
  1449. #ifdef ERA
  1450. DEFINE_NLITEM_CONSTANT (ERA); /* Alternate era. */
  1451. #endif
  1452. #ifdef ERA_D_FMT
  1453. DEFINE_NLITEM_CONSTANT (ERA_D_FMT); /* Date in alternate era format. */
  1454. #endif
  1455. #ifdef ERA_D_T_FMT
  1456. DEFINE_NLITEM_CONSTANT (ERA_D_T_FMT); /* Date and time in alternate era
  1457. format. */
  1458. #endif
  1459. #ifdef ERA_T_FMT
  1460. DEFINE_NLITEM_CONSTANT (ERA_T_FMT); /* Time in alternate era format. */
  1461. #endif
  1462. #ifdef ALT_DIGITS
  1463. DEFINE_NLITEM_CONSTANT (ALT_DIGITS); /* Alternate symbols for digits. */
  1464. #endif
  1465. DEFINE_NLITEM_CONSTANT (RADIXCHAR);
  1466. DEFINE_NLITEM_CONSTANT (THOUSEP);
  1467. #ifdef YESEXPR
  1468. DEFINE_NLITEM_CONSTANT (YESEXPR);
  1469. #endif
  1470. #ifdef NOEXPR
  1471. DEFINE_NLITEM_CONSTANT (NOEXPR);
  1472. #endif
  1473. #ifdef CRNCYSTR /* currency symbol */
  1474. DEFINE_NLITEM_CONSTANT (CRNCYSTR);
  1475. #endif
  1476. /* GNU extensions. */
  1477. #ifdef ERA_YEAR
  1478. DEFINE_NLITEM_CONSTANT (ERA_YEAR); /* Year in alternate era format. */
  1479. #endif
  1480. /* LC_MONETARY category: formatting of monetary quantities.
  1481. These items each correspond to a member of `struct lconv',
  1482. defined in <locale.h>. */
  1483. #ifdef INT_CURR_SYMBOL
  1484. DEFINE_NLITEM_CONSTANT (INT_CURR_SYMBOL);
  1485. #endif
  1486. #ifdef MON_DECIMAL_POINT
  1487. DEFINE_NLITEM_CONSTANT (MON_DECIMAL_POINT);
  1488. #endif
  1489. #ifdef MON_THOUSANDS_SEP
  1490. DEFINE_NLITEM_CONSTANT (MON_THOUSANDS_SEP);
  1491. #endif
  1492. #ifdef MON_GROUPING
  1493. DEFINE_NLITEM_CONSTANT (MON_GROUPING);
  1494. #endif
  1495. #ifdef POSITIVE_SIGN
  1496. DEFINE_NLITEM_CONSTANT (POSITIVE_SIGN);
  1497. #endif
  1498. #ifdef NEGATIVE_SIGN
  1499. DEFINE_NLITEM_CONSTANT (NEGATIVE_SIGN);
  1500. #endif
  1501. #ifdef GROUPING
  1502. DEFINE_NLITEM_CONSTANT (GROUPING);
  1503. #endif
  1504. #ifdef INT_FRAC_DIGITS
  1505. DEFINE_NLITEM_CONSTANT (INT_FRAC_DIGITS);
  1506. #endif
  1507. #ifdef FRAC_DIGITS
  1508. DEFINE_NLITEM_CONSTANT (FRAC_DIGITS);
  1509. #endif
  1510. #ifdef P_CS_PRECEDES
  1511. DEFINE_NLITEM_CONSTANT (P_CS_PRECEDES);
  1512. #endif
  1513. #ifdef P_SEP_BY_SPACE
  1514. DEFINE_NLITEM_CONSTANT (P_SEP_BY_SPACE);
  1515. #endif
  1516. #ifdef N_CS_PRECEDES
  1517. DEFINE_NLITEM_CONSTANT (N_CS_PRECEDES);
  1518. #endif
  1519. #ifdef N_SEP_BY_SPACE
  1520. DEFINE_NLITEM_CONSTANT (N_SEP_BY_SPACE);
  1521. #endif
  1522. #ifdef P_SIGN_POSN
  1523. DEFINE_NLITEM_CONSTANT (P_SIGN_POSN);
  1524. #endif
  1525. #ifdef N_SIGN_POSN
  1526. DEFINE_NLITEM_CONSTANT (N_SIGN_POSN);
  1527. #endif
  1528. #ifdef INT_P_CS_PRECEDES
  1529. DEFINE_NLITEM_CONSTANT (INT_P_CS_PRECEDES);
  1530. #endif
  1531. #ifdef INT_P_SEP_BY_SPACE
  1532. DEFINE_NLITEM_CONSTANT (INT_P_SEP_BY_SPACE);
  1533. #endif
  1534. #ifdef INT_N_CS_PRECEDES
  1535. DEFINE_NLITEM_CONSTANT (INT_N_CS_PRECEDES);
  1536. #endif
  1537. #ifdef INT_N_SEP_BY_SPACE
  1538. DEFINE_NLITEM_CONSTANT (INT_N_SEP_BY_SPACE);
  1539. #endif
  1540. #ifdef INT_P_SIGN_POSN
  1541. DEFINE_NLITEM_CONSTANT (INT_P_SIGN_POSN);
  1542. #endif
  1543. #ifdef INT_N_SIGN_POSN
  1544. DEFINE_NLITEM_CONSTANT (INT_N_SIGN_POSN);
  1545. #endif
  1546. #undef DEFINE_NLITEM_CONSTANT
  1547. }
  1548. void
  1549. scm_init_i18n ()
  1550. {
  1551. SCM global_locale_smob;
  1552. scm_add_feature ("nl-langinfo");
  1553. define_langinfo_items ();
  1554. #include "i18n.x"
  1555. /* Initialize the global locale object with a special `locale' SMOB. */
  1556. /* XXX: We don't define it as `LC_GLOBAL_LOCALE' because of bugs as of
  1557. glibc <= 2.11 not (yet) worked around by Gnulib. See
  1558. http://sourceware.org/bugzilla/show_bug.cgi?id=11009 for details. */
  1559. SCM_NEWSMOB (global_locale_smob, scm_tc16_locale_smob_type, NULL);
  1560. SCM_VARIABLE_SET (scm_global_locale, global_locale_smob);
  1561. }
  1562. void
  1563. scm_bootstrap_i18n ()
  1564. {
  1565. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  1566. "scm_init_i18n",
  1567. (scm_t_extension_init_func) scm_init_i18n,
  1568. NULL);
  1569. }