i18n.c 52 KB

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