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

Subversion Repositories or1k

[/] [or1k/] [tags/] [start/] [gdb-5.0/] [readline/] [histsearch.c] - Diff between revs 579 and 1765

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

Rev 579 Rev 1765
/* histsearch.c -- searching the history list. */
/* histsearch.c -- searching the history list. */
 
 
/* Copyright (C) 1989, 1992 Free Software Foundation, Inc.
/* Copyright (C) 1989, 1992 Free Software Foundation, Inc.
 
 
   This file contains the GNU History Library (the Library), a set of
   This file contains the GNU History Library (the Library), a set of
   routines for managing the text of previously typed lines.
   routines for managing the text of previously typed lines.
 
 
   The Library is free software; you can redistribute it and/or modify
   The Library is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 1, or (at your option)
   the Free Software Foundation; either version 1, or (at your option)
   any later version.
   any later version.
 
 
   The Library is distributed in the hope that it will be useful, but
   The Library is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.
   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,
   675 Mass Ave, Cambridge, MA 02139, USA. */
   675 Mass Ave, Cambridge, MA 02139, 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 <stdio.h>
#include <stdio.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 */
#if defined (HAVE_UNISTD_H)
#if defined (HAVE_UNISTD_H)
#  ifdef _MINIX
#  ifdef _MINIX
#    include <sys/types.h>
#    include <sys/types.h>
#  endif
#  endif
#  include <unistd.h>
#  include <unistd.h>
#endif
#endif
#if defined (HAVE_STRING_H)
#if defined (HAVE_STRING_H)
#  include <string.h>
#  include <string.h>
#else
#else
#  include <strings.h>
#  include <strings.h>
#endif /* !HAVE_STRING_H */
#endif /* !HAVE_STRING_H */
 
 
#include "history.h"
#include "history.h"
#include "histlib.h"
#include "histlib.h"
 
 
/* Variables imported from other history library files. */
/* Variables imported from other history library files. */
extern int history_offset;
extern int history_offset;
 
 
/* The list of alternate characters that can delimit a history search
/* The list of alternate characters that can delimit a history search
   string. */
   string. */
char *history_search_delimiter_chars = (char *)NULL;
char *history_search_delimiter_chars = (char *)NULL;
 
 
/* Search the history for STRING, starting at history_offset.
/* Search the history for STRING, starting at history_offset.
   If DIRECTION < 0, then the search is through previous entries, else
   If DIRECTION < 0, then the search is through previous entries, else
   through subsequent.  If ANCHORED is non-zero, the string must
   through subsequent.  If ANCHORED is non-zero, the string must
   appear at the beginning of a history line, otherwise, the string
   appear at the beginning of a history line, otherwise, the string
   may appear anywhere in the line.  If the string is found, then
   may appear anywhere in the line.  If the string is found, then
   current_history () is the history entry, and the value of this
   current_history () is the history entry, and the value of this
   function is the offset in the line of that history entry that the
   function is the offset in the line of that history entry that the
   string was found in.  Otherwise, nothing is changed, and a -1 is
   string was found in.  Otherwise, nothing is changed, and a -1 is
   returned. */
   returned. */
 
 
static int
static int
history_search_internal (string, direction, anchored)
history_search_internal (string, direction, anchored)
     char *string;
     char *string;
     int direction, anchored;
     int direction, anchored;
{
{
  register int i, reverse;
  register int i, reverse;
  register char *line;
  register char *line;
  register int line_index;
  register int line_index;
  int string_len;
  int string_len;
  HIST_ENTRY **the_history;     /* local */
  HIST_ENTRY **the_history;     /* local */
 
 
  i = history_offset;
  i = history_offset;
  reverse = (direction < 0);
  reverse = (direction < 0);
 
 
  /* Take care of trivial cases first. */
  /* Take care of trivial cases first. */
  if (string == 0 || *string == '\0')
  if (string == 0 || *string == '\0')
    return (-1);
    return (-1);
 
 
  if (!history_length || ((i == history_length) && !reverse))
  if (!history_length || ((i == history_length) && !reverse))
    return (-1);
    return (-1);
 
 
  if (reverse && (i == history_length))
  if (reverse && (i == history_length))
    i--;
    i--;
 
 
#define NEXT_LINE() do { if (reverse) i--; else i++; } while (0)
#define NEXT_LINE() do { if (reverse) i--; else i++; } while (0)
 
 
  the_history = history_list ();
  the_history = history_list ();
  string_len = strlen (string);
  string_len = strlen (string);
  while (1)
  while (1)
    {
    {
      /* Search each line in the history list for STRING. */
      /* Search each line in the history list for STRING. */
 
 
      /* At limit for direction? */
      /* At limit for direction? */
      if ((reverse && i < 0) || (!reverse && i == history_length))
      if ((reverse && i < 0) || (!reverse && i == history_length))
        return (-1);
        return (-1);
 
 
      line = the_history[i]->line;
      line = the_history[i]->line;
      line_index = strlen (line);
      line_index = strlen (line);
 
 
      /* If STRING is longer than line, no match. */
      /* If STRING is longer than line, no match. */
      if (string_len > line_index)
      if (string_len > line_index)
        {
        {
          NEXT_LINE ();
          NEXT_LINE ();
          continue;
          continue;
        }
        }
 
 
      /* Handle anchored searches first. */
      /* Handle anchored searches first. */
      if (anchored == ANCHORED_SEARCH)
      if (anchored == ANCHORED_SEARCH)
        {
        {
          if (STREQN (string, line, string_len))
          if (STREQN (string, line, string_len))
            {
            {
              history_offset = i;
              history_offset = i;
              return (0);
              return (0);
            }
            }
 
 
          NEXT_LINE ();
          NEXT_LINE ();
          continue;
          continue;
        }
        }
 
 
      /* Do substring search. */
      /* Do substring search. */
      if (reverse)
      if (reverse)
        {
        {
          line_index -= string_len;
          line_index -= string_len;
 
 
          while (line_index >= 0)
          while (line_index >= 0)
            {
            {
              if (STREQN (string, line + line_index, string_len))
              if (STREQN (string, line + line_index, string_len))
                {
                {
                  history_offset = i;
                  history_offset = i;
                  return (line_index);
                  return (line_index);
                }
                }
              line_index--;
              line_index--;
            }
            }
        }
        }
      else
      else
        {
        {
          register int limit;
          register int limit;
 
 
          limit = line_index - string_len + 1;
          limit = line_index - string_len + 1;
          line_index = 0;
          line_index = 0;
 
 
          while (line_index < limit)
          while (line_index < limit)
            {
            {
              if (STREQN (string, line + line_index, string_len))
              if (STREQN (string, line + line_index, string_len))
                {
                {
                  history_offset = i;
                  history_offset = i;
                  return (line_index);
                  return (line_index);
                }
                }
              line_index++;
              line_index++;
            }
            }
        }
        }
      NEXT_LINE ();
      NEXT_LINE ();
    }
    }
}
}
 
 
/* Do a non-anchored search for STRING through the history in DIRECTION. */
/* Do a non-anchored search for STRING through the history in DIRECTION. */
int
int
history_search (string, direction)
history_search (string, direction)
     char *string;
     char *string;
     int direction;
     int direction;
{
{
  return (history_search_internal (string, direction, NON_ANCHORED_SEARCH));
  return (history_search_internal (string, direction, NON_ANCHORED_SEARCH));
}
}
 
 
/* Do an anchored search for string through the history in DIRECTION. */
/* Do an anchored search for string through the history in DIRECTION. */
int
int
history_search_prefix (string, direction)
history_search_prefix (string, direction)
     char *string;
     char *string;
     int direction;
     int direction;
{
{
  return (history_search_internal (string, direction, ANCHORED_SEARCH));
  return (history_search_internal (string, direction, ANCHORED_SEARCH));
}
}
 
 
/* Search for STRING in the history list.  DIR is < 0 for searching
/* Search for STRING in the history list.  DIR is < 0 for searching
   backwards.  POS is an absolute index into the history list at
   backwards.  POS is an absolute index into the history list at
   which point to begin searching. */
   which point to begin searching. */
int
int
history_search_pos (string, dir, pos)
history_search_pos (string, dir, pos)
     char *string;
     char *string;
     int dir, pos;
     int dir, pos;
{
{
  int ret, old;
  int ret, old;
 
 
  old = where_history ();
  old = where_history ();
  history_set_pos (pos);
  history_set_pos (pos);
  if (history_search (string, dir) == -1)
  if (history_search (string, dir) == -1)
    {
    {
      history_set_pos (old);
      history_set_pos (old);
      return (-1);
      return (-1);
    }
    }
  ret = where_history ();
  ret = where_history ();
  history_set_pos (old);
  history_set_pos (old);
  return ret;
  return ret;
}
}
 
 

powered by: WebSVN 2.1.0

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