nls.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* nls.c -- skeletal internationalization code. */
  2. /* Copyright (C) 1996-2009 Free Software Foundation, Inc.
  3. This file is part of the GNU Readline Library (Readline), a library
  4. for reading lines of text with interactive input and history editing.
  5. Readline is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. Readline is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Readline. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #define READLINE_LIBRARY
  17. #if defined (HAVE_CONFIG_H)
  18. # include <config.h>
  19. #endif
  20. #include <sys/types.h>
  21. #include <stdio.h>
  22. #if defined (HAVE_UNISTD_H)
  23. # include <unistd.h>
  24. #endif /* HAVE_UNISTD_H */
  25. #if defined (HAVE_STDLIB_H)
  26. # include <stdlib.h>
  27. #else
  28. # include "ansi_stdlib.h"
  29. #endif /* HAVE_STDLIB_H */
  30. #if defined (HAVE_LOCALE_H)
  31. # include <locale.h>
  32. #endif
  33. #include <ctype.h>
  34. #include "rldefs.h"
  35. #include "readline.h"
  36. #include "rlshell.h"
  37. #include "rlprivate.h"
  38. #if !defined (HAVE_SETLOCALE)
  39. /* A list of legal values for the LANG or LC_CTYPE environment variables.
  40. If a locale name in this list is the value for the LC_ALL, LC_CTYPE,
  41. or LANG environment variable (using the first of those with a value),
  42. readline eight-bit mode is enabled. */
  43. static char *legal_lang_values[] =
  44. {
  45. "iso88591",
  46. "iso88592",
  47. "iso88593",
  48. "iso88594",
  49. "iso88595",
  50. "iso88596",
  51. "iso88597",
  52. "iso88598",
  53. "iso88599",
  54. "iso885910",
  55. "koi8r",
  56. 0
  57. };
  58. static char *normalize_codeset PARAMS((char *));
  59. static char *find_codeset PARAMS((char *, size_t *));
  60. #endif /* !HAVE_SETLOCALE */
  61. static char *_rl_get_locale_var PARAMS((const char *));
  62. static char *
  63. _rl_get_locale_var (v)
  64. const char *v;
  65. {
  66. char *lspec;
  67. lspec = sh_get_env_value ("LC_ALL");
  68. if (lspec == 0 || *lspec == 0)
  69. lspec = sh_get_env_value (v);
  70. if (lspec == 0 || *lspec == 0)
  71. lspec = sh_get_env_value ("LANG");
  72. return lspec;
  73. }
  74. /* Check for LC_ALL, LC_CTYPE, and LANG and use the first with a value
  75. to decide the defaults for 8-bit character input and output. Returns
  76. 1 if we set eight-bit mode. */
  77. int
  78. _rl_init_eightbit ()
  79. {
  80. /* If we have setlocale(3), just check the current LC_CTYPE category
  81. value, and go into eight-bit mode if it's not C or POSIX. */
  82. #if defined (HAVE_SETLOCALE)
  83. char *lspec, *t;
  84. /* Set the LC_CTYPE locale category from environment variables. */
  85. lspec = _rl_get_locale_var ("LC_CTYPE");
  86. /* Since _rl_get_locale_var queries the right environment variables,
  87. we query the current locale settings with setlocale(), and, if
  88. that doesn't return anything, we set lspec to the empty string to
  89. force the subsequent call to setlocale() to define the `native'
  90. environment. */
  91. if (lspec == 0 || *lspec == 0)
  92. lspec = setlocale (LC_CTYPE, (char *)NULL);
  93. if (lspec == 0)
  94. lspec = "";
  95. t = setlocale (LC_CTYPE, lspec);
  96. if (t && *t && (t[0] != 'C' || t[1]) && (STREQ (t, "POSIX") == 0))
  97. {
  98. _rl_meta_flag = 1;
  99. _rl_convert_meta_chars_to_ascii = 0;
  100. _rl_output_meta_chars = 1;
  101. return (1);
  102. }
  103. else
  104. return (0);
  105. #else /* !HAVE_SETLOCALE */
  106. char *lspec, *t;
  107. int i;
  108. /* We don't have setlocale. Finesse it. Check the environment for the
  109. appropriate variables and set eight-bit mode if they have the right
  110. values. */
  111. lspec = _rl_get_locale_var ("LC_CTYPE");
  112. if (lspec == 0 || (t = normalize_codeset (lspec)) == 0)
  113. return (0);
  114. for (i = 0; t && legal_lang_values[i]; i++)
  115. if (STREQ (t, legal_lang_values[i]))
  116. {
  117. _rl_meta_flag = 1;
  118. _rl_convert_meta_chars_to_ascii = 0;
  119. _rl_output_meta_chars = 1;
  120. break;
  121. }
  122. xfree (t);
  123. return (legal_lang_values[i] ? 1 : 0);
  124. #endif /* !HAVE_SETLOCALE */
  125. }
  126. #if !defined (HAVE_SETLOCALE)
  127. static char *
  128. normalize_codeset (codeset)
  129. char *codeset;
  130. {
  131. size_t namelen, i;
  132. int len, all_digits;
  133. char *wp, *retval;
  134. codeset = find_codeset (codeset, &namelen);
  135. if (codeset == 0)
  136. return (codeset);
  137. all_digits = 1;
  138. for (len = 0, i = 0; i < namelen; i++)
  139. {
  140. if (ISALNUM ((unsigned char)codeset[i]))
  141. {
  142. len++;
  143. all_digits &= _rl_digit_p (codeset[i]);
  144. }
  145. }
  146. retval = (char *)malloc ((all_digits ? 3 : 0) + len + 1);
  147. if (retval == 0)
  148. return ((char *)0);
  149. wp = retval;
  150. /* Add `iso' to beginning of an all-digit codeset */
  151. if (all_digits)
  152. {
  153. *wp++ = 'i';
  154. *wp++ = 's';
  155. *wp++ = 'o';
  156. }
  157. for (i = 0; i < namelen; i++)
  158. if (ISALPHA ((unsigned char)codeset[i]))
  159. *wp++ = _rl_to_lower (codeset[i]);
  160. else if (_rl_digit_p (codeset[i]))
  161. *wp++ = codeset[i];
  162. *wp = '\0';
  163. return retval;
  164. }
  165. /* Isolate codeset portion of locale specification. */
  166. static char *
  167. find_codeset (name, lenp)
  168. char *name;
  169. size_t *lenp;
  170. {
  171. char *cp, *language, *result;
  172. cp = language = name;
  173. result = (char *)0;
  174. while (*cp && *cp != '_' && *cp != '@' && *cp != '+' && *cp != ',')
  175. cp++;
  176. /* This does not make sense: language has to be specified. As
  177. an exception we allow the variable to contain only the codeset
  178. name. Perhaps there are funny codeset names. */
  179. if (language == cp)
  180. {
  181. *lenp = strlen (language);
  182. result = language;
  183. }
  184. else
  185. {
  186. /* Next is the territory. */
  187. if (*cp == '_')
  188. do
  189. ++cp;
  190. while (*cp && *cp != '.' && *cp != '@' && *cp != '+' && *cp != ',' && *cp != '_');
  191. /* Now, finally, is the codeset. */
  192. result = cp;
  193. if (*cp == '.')
  194. do
  195. ++cp;
  196. while (*cp && *cp != '@');
  197. if (cp - result > 2)
  198. {
  199. result++;
  200. *lenp = cp - result;
  201. }
  202. else
  203. {
  204. *lenp = strlen (language);
  205. result = language;
  206. }
  207. }
  208. return result;
  209. }
  210. #endif /* !HAVE_SETLOCALE */