home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / readline.zip / readline-2.1 / nls.c < prev    next >
C/C++ Source or Header  |  1997-05-02  |  6KB  |  228 lines

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