OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gdb-6.8/] [readline/] [mbutil.c] - Diff between revs 157 and 816

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 157 Rev 816
/* mbutil.c -- readline multibyte character utility functions */
/* mbutil.c -- readline multibyte character utility functions */
 
 
/* Copyright (C) 2001-2005 Free Software Foundation, Inc.
/* Copyright (C) 2001-2005 Free Software Foundation, Inc.
 
 
   This file is part of the GNU Readline Library, a library for
   This file is part of the GNU Readline Library, a library for
   reading lines of text with interactive input and history editing.
   reading lines of text with interactive input and history editing.
 
 
   The GNU Readline Library is free software; you can redistribute it
   The GNU Readline Library is free software; you can redistribute it
   and/or modify it under the terms of the GNU General Public License
   and/or modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2, or
   as published by the Free Software Foundation; either version 2, or
   (at your option) any later version.
   (at your option) any later version.
 
 
   The GNU Readline Library is distributed in the hope that it will be
   The GNU Readline Library is distributed in the hope that it will be
   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   GNU General Public License for more details.
 
 
   The GNU General Public License is often shipped with GNU software, and
   The GNU General Public License is often shipped with GNU software, and
   is generally kept in a file called COPYING or LICENSE.  If you do not
   is generally kept in a file called COPYING or LICENSE.  If you do not
   have a copy of the license, write to the Free Software Foundation,
   have a copy of the license, write to the Free Software Foundation,
   59 Temple Place, Suite 330, Boston, MA 02111 USA. */
   59 Temple Place, Suite 330, Boston, MA 02111 USA. */
#define READLINE_LIBRARY
#define READLINE_LIBRARY
 
 
#if defined (HAVE_CONFIG_H)
#if defined (HAVE_CONFIG_H)
#  include <config.h>
#  include <config.h>
#endif
#endif
 
 
#include <sys/types.h>
#include <sys/types.h>
#include <fcntl.h>
#include <fcntl.h>
#include "posixjmp.h"
#include "posixjmp.h"
 
 
#if defined (HAVE_UNISTD_H)
#if defined (HAVE_UNISTD_H)
#  include <unistd.h>      /* for _POSIX_VERSION */
#  include <unistd.h>      /* for _POSIX_VERSION */
#endif /* HAVE_UNISTD_H */
#endif /* HAVE_UNISTD_H */
 
 
#if defined (HAVE_STDLIB_H)
#if defined (HAVE_STDLIB_H)
#  include <stdlib.h>
#  include <stdlib.h>
#else
#else
#  include "ansi_stdlib.h"
#  include "ansi_stdlib.h"
#endif /* HAVE_STDLIB_H */
#endif /* HAVE_STDLIB_H */
 
 
#include <stdio.h>
#include <stdio.h>
#include <ctype.h>
#include <ctype.h>
 
 
/* System-specific feature definitions and include files. */
/* System-specific feature definitions and include files. */
#include "rldefs.h"
#include "rldefs.h"
#include "rlmbutil.h"
#include "rlmbutil.h"
 
 
#if defined (TIOCSTAT_IN_SYS_IOCTL)
#if defined (TIOCSTAT_IN_SYS_IOCTL)
#  include <sys/ioctl.h>
#  include <sys/ioctl.h>
#endif /* TIOCSTAT_IN_SYS_IOCTL */
#endif /* TIOCSTAT_IN_SYS_IOCTL */
 
 
/* Some standard library routines. */
/* Some standard library routines. */
#include "readline.h"
#include "readline.h"
 
 
#include "rlprivate.h"
#include "rlprivate.h"
#include "xmalloc.h"
#include "xmalloc.h"
 
 
/* Declared here so it can be shared between the readline and history
/* Declared here so it can be shared between the readline and history
   libraries. */
   libraries. */
#if defined (HANDLE_MULTIBYTE)
#if defined (HANDLE_MULTIBYTE)
int rl_byte_oriented = 0;
int rl_byte_oriented = 0;
#else
#else
int rl_byte_oriented = 1;
int rl_byte_oriented = 1;
#endif
#endif
 
 
/* **************************************************************** */
/* **************************************************************** */
/*                                                                  */
/*                                                                  */
/*              Multibyte Character Utility Functions               */
/*              Multibyte Character Utility Functions               */
/*                                                                  */
/*                                                                  */
/* **************************************************************** */
/* **************************************************************** */
 
 
#if defined(HANDLE_MULTIBYTE)
#if defined(HANDLE_MULTIBYTE)
 
 
static int
static int
_rl_find_next_mbchar_internal (string, seed, count, find_non_zero)
_rl_find_next_mbchar_internal (string, seed, count, find_non_zero)
     char *string;
     char *string;
     int seed, count, find_non_zero;
     int seed, count, find_non_zero;
{
{
  size_t tmp;
  size_t tmp;
  mbstate_t ps;
  mbstate_t ps;
  int point;
  int point;
  wchar_t wc;
  wchar_t wc;
 
 
  tmp = 0;
  tmp = 0;
 
 
  memset(&ps, 0, sizeof (mbstate_t));
  memset(&ps, 0, sizeof (mbstate_t));
  if (seed < 0)
  if (seed < 0)
    seed = 0;
    seed = 0;
  if (count <= 0)
  if (count <= 0)
    return seed;
    return seed;
 
 
  point = seed + _rl_adjust_point (string, seed, &ps);
  point = seed + _rl_adjust_point (string, seed, &ps);
  /* if this is true, means that seed was not pointed character
  /* if this is true, means that seed was not pointed character
     started byte.  So correct the point and consume count */
     started byte.  So correct the point and consume count */
  if (seed < point)
  if (seed < point)
    count--;
    count--;
 
 
  while (count > 0)
  while (count > 0)
    {
    {
      tmp = mbrtowc (&wc, string+point, strlen(string + point), &ps);
      tmp = mbrtowc (&wc, string+point, strlen(string + point), &ps);
      if (MB_INVALIDCH ((size_t)tmp))
      if (MB_INVALIDCH ((size_t)tmp))
        {
        {
          /* invalid bytes. asume a byte represents a character */
          /* invalid bytes. asume a byte represents a character */
          point++;
          point++;
          count--;
          count--;
          /* reset states. */
          /* reset states. */
          memset(&ps, 0, sizeof(mbstate_t));
          memset(&ps, 0, sizeof(mbstate_t));
        }
        }
      else if (MB_NULLWCH (tmp))
      else if (MB_NULLWCH (tmp))
        break;                  /* found wide '\0' */
        break;                  /* found wide '\0' */
      else
      else
        {
        {
          /* valid bytes */
          /* valid bytes */
          point += tmp;
          point += tmp;
          if (find_non_zero)
          if (find_non_zero)
            {
            {
              if (wcwidth (wc) == 0)
              if (wcwidth (wc) == 0)
                continue;
                continue;
              else
              else
                count--;
                count--;
            }
            }
          else
          else
            count--;
            count--;
        }
        }
    }
    }
 
 
  if (find_non_zero)
  if (find_non_zero)
    {
    {
      tmp = mbrtowc (&wc, string + point, strlen (string + point), &ps);
      tmp = mbrtowc (&wc, string + point, strlen (string + point), &ps);
      while (tmp > 0 && wcwidth (wc) == 0)
      while (tmp > 0 && wcwidth (wc) == 0)
        {
        {
          point += tmp;
          point += tmp;
          tmp = mbrtowc (&wc, string + point, strlen (string + point), &ps);
          tmp = mbrtowc (&wc, string + point, strlen (string + point), &ps);
          if (MB_NULLWCH (tmp) || MB_INVALIDCH (tmp))
          if (MB_NULLWCH (tmp) || MB_INVALIDCH (tmp))
            break;
            break;
        }
        }
    }
    }
 
 
  return point;
  return point;
}
}
 
 
static int
static int
_rl_find_prev_mbchar_internal (string, seed, find_non_zero)
_rl_find_prev_mbchar_internal (string, seed, find_non_zero)
     char *string;
     char *string;
     int seed, find_non_zero;
     int seed, find_non_zero;
{
{
  mbstate_t ps;
  mbstate_t ps;
  int prev, non_zero_prev, point, length;
  int prev, non_zero_prev, point, length;
  size_t tmp;
  size_t tmp;
  wchar_t wc;
  wchar_t wc;
 
 
  memset(&ps, 0, sizeof(mbstate_t));
  memset(&ps, 0, sizeof(mbstate_t));
  length = strlen(string);
  length = strlen(string);
 
 
  if (seed < 0)
  if (seed < 0)
    return 0;
    return 0;
  else if (length < seed)
  else if (length < seed)
    return length;
    return length;
 
 
  prev = non_zero_prev = point = 0;
  prev = non_zero_prev = point = 0;
  while (point < seed)
  while (point < seed)
    {
    {
      tmp = mbrtowc (&wc, string + point, length - point, &ps);
      tmp = mbrtowc (&wc, string + point, length - point, &ps);
      if (MB_INVALIDCH ((size_t)tmp))
      if (MB_INVALIDCH ((size_t)tmp))
        {
        {
          /* in this case, bytes are invalid or shorted to compose
          /* in this case, bytes are invalid or shorted to compose
             multibyte char, so assume that the first byte represents
             multibyte char, so assume that the first byte represents
             a single character anyway. */
             a single character anyway. */
          tmp = 1;
          tmp = 1;
          /* clear the state of the byte sequence, because
          /* clear the state of the byte sequence, because
             in this case effect of mbstate is undefined  */
             in this case effect of mbstate is undefined  */
          memset(&ps, 0, sizeof (mbstate_t));
          memset(&ps, 0, sizeof (mbstate_t));
 
 
          /* Since we're assuming that this byte represents a single
          /* Since we're assuming that this byte represents a single
             non-zero-width character, don't forget about it. */
             non-zero-width character, don't forget about it. */
          prev = point;
          prev = point;
        }
        }
      else if (MB_NULLWCH (tmp))
      else if (MB_NULLWCH (tmp))
        break;                  /* Found '\0' char.  Can this happen? */
        break;                  /* Found '\0' char.  Can this happen? */
      else
      else
        {
        {
          if (find_non_zero)
          if (find_non_zero)
            {
            {
              if (wcwidth (wc) != 0)
              if (wcwidth (wc) != 0)
                prev = point;
                prev = point;
            }
            }
          else
          else
            prev = point;
            prev = point;
        }
        }
 
 
      point += tmp;
      point += tmp;
    }
    }
 
 
  return prev;
  return prev;
}
}
 
 
/* return the number of bytes parsed from the multibyte sequence starting
/* return the number of bytes parsed from the multibyte sequence starting
   at src, if a non-L'\0' wide character was recognized. It returns 0,
   at src, if a non-L'\0' wide character was recognized. It returns 0,
   if a L'\0' wide character was recognized. It  returns (size_t)(-1),
   if a L'\0' wide character was recognized. It  returns (size_t)(-1),
   if an invalid multibyte sequence was encountered. It returns (size_t)(-2)
   if an invalid multibyte sequence was encountered. It returns (size_t)(-2)
   if it couldn't parse a complete  multibyte character.  */
   if it couldn't parse a complete  multibyte character.  */
int
int
_rl_get_char_len (src, ps)
_rl_get_char_len (src, ps)
     char *src;
     char *src;
     mbstate_t *ps;
     mbstate_t *ps;
{
{
  size_t tmp;
  size_t tmp;
 
 
  tmp = mbrlen((const char *)src, (size_t)strlen (src), ps);
  tmp = mbrlen((const char *)src, (size_t)strlen (src), ps);
  if (tmp == (size_t)(-2))
  if (tmp == (size_t)(-2))
    {
    {
      /* shorted to compose multibyte char */
      /* shorted to compose multibyte char */
      if (ps)
      if (ps)
        memset (ps, 0, sizeof(mbstate_t));
        memset (ps, 0, sizeof(mbstate_t));
      return -2;
      return -2;
    }
    }
  else if (tmp == (size_t)(-1))
  else if (tmp == (size_t)(-1))
    {
    {
      /* invalid to compose multibyte char */
      /* invalid to compose multibyte char */
      /* initialize the conversion state */
      /* initialize the conversion state */
      if (ps)
      if (ps)
        memset (ps, 0, sizeof(mbstate_t));
        memset (ps, 0, sizeof(mbstate_t));
      return -1;
      return -1;
    }
    }
  else if (tmp == (size_t)0)
  else if (tmp == (size_t)0)
    return 0;
    return 0;
  else
  else
    return (int)tmp;
    return (int)tmp;
}
}
 
 
/* compare the specified two characters. If the characters matched,
/* compare the specified two characters. If the characters matched,
   return 1. Otherwise return 0. */
   return 1. Otherwise return 0. */
int
int
_rl_compare_chars (buf1, pos1, ps1, buf2, pos2, ps2)
_rl_compare_chars (buf1, pos1, ps1, buf2, pos2, ps2)
     char *buf1;
     char *buf1;
     int pos1;
     int pos1;
     mbstate_t *ps1;
     mbstate_t *ps1;
     char *buf2;
     char *buf2;
     int pos2;
     int pos2;
     mbstate_t *ps2;
     mbstate_t *ps2;
{
{
  int i, w1, w2;
  int i, w1, w2;
 
 
  if ((w1 = _rl_get_char_len (&buf1[pos1], ps1)) <= 0 ||
  if ((w1 = _rl_get_char_len (&buf1[pos1], ps1)) <= 0 ||
        (w2 = _rl_get_char_len (&buf2[pos2], ps2)) <= 0 ||
        (w2 = _rl_get_char_len (&buf2[pos2], ps2)) <= 0 ||
        (w1 != w2) ||
        (w1 != w2) ||
        (buf1[pos1] != buf2[pos2]))
        (buf1[pos1] != buf2[pos2]))
    return 0;
    return 0;
 
 
  for (i = 1; i < w1; i++)
  for (i = 1; i < w1; i++)
    if (buf1[pos1+i] != buf2[pos2+i])
    if (buf1[pos1+i] != buf2[pos2+i])
      return 0;
      return 0;
 
 
  return 1;
  return 1;
}
}
 
 
/* adjust pointed byte and find mbstate of the point of string.
/* adjust pointed byte and find mbstate of the point of string.
   adjusted point will be point <= adjusted_point, and returns
   adjusted point will be point <= adjusted_point, and returns
   differences of the byte(adjusted_point - point).
   differences of the byte(adjusted_point - point).
   if point is invalied (point < 0 || more than string length),
   if point is invalied (point < 0 || more than string length),
   it returns -1 */
   it returns -1 */
int
int
_rl_adjust_point(string, point, ps)
_rl_adjust_point(string, point, ps)
     char *string;
     char *string;
     int point;
     int point;
     mbstate_t *ps;
     mbstate_t *ps;
{
{
  size_t tmp = 0;
  size_t tmp = 0;
  int length;
  int length;
  int pos = 0;
  int pos = 0;
 
 
  length = strlen(string);
  length = strlen(string);
  if (point < 0)
  if (point < 0)
    return -1;
    return -1;
  if (length < point)
  if (length < point)
    return -1;
    return -1;
 
 
  while (pos < point)
  while (pos < point)
    {
    {
      tmp = mbrlen (string + pos, length - pos, ps);
      tmp = mbrlen (string + pos, length - pos, ps);
      if (MB_INVALIDCH ((size_t)tmp))
      if (MB_INVALIDCH ((size_t)tmp))
        {
        {
          /* in this case, bytes are invalid or shorted to compose
          /* in this case, bytes are invalid or shorted to compose
             multibyte char, so assume that the first byte represents
             multibyte char, so assume that the first byte represents
             a single character anyway. */
             a single character anyway. */
          pos++;
          pos++;
          /* clear the state of the byte sequence, because
          /* clear the state of the byte sequence, because
             in this case effect of mbstate is undefined  */
             in this case effect of mbstate is undefined  */
          if (ps)
          if (ps)
            memset (ps, 0, sizeof (mbstate_t));
            memset (ps, 0, sizeof (mbstate_t));
        }
        }
      else if (MB_NULLWCH (tmp))
      else if (MB_NULLWCH (tmp))
        pos++;
        pos++;
      else
      else
        pos += tmp;
        pos += tmp;
    }
    }
 
 
  return (pos - point);
  return (pos - point);
}
}
 
 
int
int
_rl_is_mbchar_matched (string, seed, end, mbchar, length)
_rl_is_mbchar_matched (string, seed, end, mbchar, length)
     char *string;
     char *string;
     int seed, end;
     int seed, end;
     char *mbchar;
     char *mbchar;
     int length;
     int length;
{
{
  int i;
  int i;
 
 
  if ((end - seed) < length)
  if ((end - seed) < length)
    return 0;
    return 0;
 
 
  for (i = 0; i < length; i++)
  for (i = 0; i < length; i++)
    if (string[seed + i] != mbchar[i])
    if (string[seed + i] != mbchar[i])
      return 0;
      return 0;
  return 1;
  return 1;
}
}
 
 
wchar_t
wchar_t
_rl_char_value (buf, ind)
_rl_char_value (buf, ind)
     char *buf;
     char *buf;
     int ind;
     int ind;
{
{
  size_t tmp;
  size_t tmp;
  wchar_t wc;
  wchar_t wc;
  mbstate_t ps;
  mbstate_t ps;
  int l;
  int l;
 
 
  if (MB_LEN_MAX == 1 || rl_byte_oriented)
  if (MB_LEN_MAX == 1 || rl_byte_oriented)
    return ((wchar_t) buf[ind]);
    return ((wchar_t) buf[ind]);
  l = strlen (buf);
  l = strlen (buf);
  if (ind >= l - 1)
  if (ind >= l - 1)
    return ((wchar_t) buf[ind]);
    return ((wchar_t) buf[ind]);
  memset (&ps, 0, sizeof (mbstate_t));
  memset (&ps, 0, sizeof (mbstate_t));
  tmp = mbrtowc (&wc, buf + ind, l - ind, &ps);
  tmp = mbrtowc (&wc, buf + ind, l - ind, &ps);
  if (MB_INVALIDCH (tmp) || MB_NULLWCH (tmp))
  if (MB_INVALIDCH (tmp) || MB_NULLWCH (tmp))
    return ((wchar_t) buf[ind]);
    return ((wchar_t) buf[ind]);
  return wc;
  return wc;
}
}
#endif /* HANDLE_MULTIBYTE */
#endif /* HANDLE_MULTIBYTE */
 
 
/* Find next `count' characters started byte point of the specified seed.
/* Find next `count' characters started byte point of the specified seed.
   If flags is MB_FIND_NONZERO, we look for non-zero-width multibyte
   If flags is MB_FIND_NONZERO, we look for non-zero-width multibyte
   characters. */
   characters. */
#undef _rl_find_next_mbchar
#undef _rl_find_next_mbchar
int
int
_rl_find_next_mbchar (string, seed, count, flags)
_rl_find_next_mbchar (string, seed, count, flags)
     char *string;
     char *string;
     int seed, count, flags;
     int seed, count, flags;
{
{
#if defined (HANDLE_MULTIBYTE)
#if defined (HANDLE_MULTIBYTE)
  return _rl_find_next_mbchar_internal (string, seed, count, flags);
  return _rl_find_next_mbchar_internal (string, seed, count, flags);
#else
#else
  return (seed + count);
  return (seed + count);
#endif
#endif
}
}
 
 
/* Find previous character started byte point of the specified seed.
/* Find previous character started byte point of the specified seed.
   Returned point will be point <= seed.  If flags is MB_FIND_NONZERO,
   Returned point will be point <= seed.  If flags is MB_FIND_NONZERO,
   we look for non-zero-width multibyte characters. */
   we look for non-zero-width multibyte characters. */
#undef _rl_find_prev_mbchar
#undef _rl_find_prev_mbchar
int
int
_rl_find_prev_mbchar (string, seed, flags)
_rl_find_prev_mbchar (string, seed, flags)
     char *string;
     char *string;
     int seed, flags;
     int seed, flags;
{
{
#if defined (HANDLE_MULTIBYTE)
#if defined (HANDLE_MULTIBYTE)
  return _rl_find_prev_mbchar_internal (string, seed, flags);
  return _rl_find_prev_mbchar_internal (string, seed, flags);
#else
#else
  return ((seed == 0) ? seed : seed - 1);
  return ((seed == 0) ? seed : seed - 1);
#endif
#endif
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.