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

Subversion Repositories or1k

[/] [or1k/] [tags/] [start/] [gdb-5.0/] [readline/] [search.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
/* search.c - code for non-incremental searching in emacs and vi modes. */
/* search.c - code for non-incremental searching in emacs and vi modes. */
 
 
/* Copyright (C) 1992 Free Software Foundation, Inc.
/* Copyright (C) 1992 Free Software Foundation, Inc.
 
 
   This file is part of the Readline Library (the Library), a set of
   This file is part of the Readline Library (the Library), a set of
   routines for providing Emacs style line input to programs that ask
   routines for providing Emacs style line input to programs that ask
   for it.
   for it.
 
 
   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 <sys/types.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdio.h>
 
 
#if defined (HAVE_UNISTD_H)
#if defined (HAVE_UNISTD_H)
#  include <unistd.h>
#  include <unistd.h>
#endif
#endif
 
 
#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
#endif
 
 
#include "rldefs.h"
#include "rldefs.h"
#include "readline.h"
#include "readline.h"
#include "history.h"
#include "history.h"
 
 
#ifdef abs
#ifdef abs
#  undef abs
#  undef abs
#endif
#endif
#define abs(x)          (((x) >= 0) ? (x) : -(x))
#define abs(x)          (((x) >= 0) ? (x) : -(x))
 
 
extern char *xmalloc (), *xrealloc ();
extern char *xmalloc (), *xrealloc ();
 
 
/* Variables imported from readline.c */
/* Variables imported from readline.c */
extern int rl_point, rl_end, rl_line_buffer_len;
extern int rl_point, rl_end, rl_line_buffer_len;
extern int rl_editing_mode;
extern int rl_editing_mode;
extern char *rl_prompt;
extern char *rl_prompt;
extern char *rl_line_buffer;
extern char *rl_line_buffer;
extern HIST_ENTRY *saved_line_for_history;
extern HIST_ENTRY *saved_line_for_history;
extern Function *rl_last_func;
extern Function *rl_last_func;
 
 
/* Functions imported from the rest of the library. */
/* Functions imported from the rest of the library. */
extern int _rl_free_history_entry ();
extern int _rl_free_history_entry ();
extern char *_rl_make_prompt_for_search ();
extern char *_rl_make_prompt_for_search ();
extern void rl_extend_line_buffer ();
extern void rl_extend_line_buffer ();
 
 
static char *noninc_search_string = (char *) NULL;
static char *noninc_search_string = (char *) NULL;
static int noninc_history_pos;
static int noninc_history_pos;
static char *prev_line_found = (char *) NULL;
static char *prev_line_found = (char *) NULL;
 
 
/* Search the history list for STRING starting at absolute history position
/* Search the history list for STRING starting at absolute history position
   POS.  If STRING begins with `^', the search must match STRING at the
   POS.  If STRING begins with `^', the search must match STRING at the
   beginning of a history line, otherwise a full substring match is performed
   beginning of a history line, otherwise a full substring match is performed
   for STRING.  DIR < 0 means to search backwards through the history list,
   for STRING.  DIR < 0 means to search backwards through the history list,
   DIR >= 0 means to search forward. */
   DIR >= 0 means to search forward. */
static int
static int
noninc_search_from_pos (string, pos, dir)
noninc_search_from_pos (string, pos, dir)
     char *string;
     char *string;
     int pos, dir;
     int pos, dir;
{
{
  int ret, old;
  int ret, old;
 
 
  old = where_history ();
  old = where_history ();
  history_set_pos (pos);
  history_set_pos (pos);
 
 
  if (*string == '^')
  if (*string == '^')
    ret = history_search_prefix (string + 1, dir);
    ret = history_search_prefix (string + 1, dir);
  else
  else
    ret = history_search (string, dir);
    ret = history_search (string, dir);
 
 
  if (ret != -1)
  if (ret != -1)
    ret = where_history ();
    ret = where_history ();
 
 
  history_set_pos (old);
  history_set_pos (old);
  return (ret);
  return (ret);
}
}
 
 
/* Search for a line in the history containing STRING.  If DIR is < 0, the
/* Search for a line in the history containing STRING.  If DIR is < 0, the
   search is backwards through previous entries, else through subsequent
   search is backwards through previous entries, else through subsequent
   entries. */
   entries. */
static void
static void
noninc_dosearch (string, dir)
noninc_dosearch (string, dir)
     char *string;
     char *string;
     int dir;
     int dir;
{
{
  int oldpos, pos, line_len;
  int oldpos, pos, line_len;
  HIST_ENTRY *entry;
  HIST_ENTRY *entry;
 
 
  if (string == 0 || *string == '\0' || noninc_history_pos < 0)
  if (string == 0 || *string == '\0' || noninc_history_pos < 0)
    {
    {
      ding ();
      ding ();
      return;
      return;
    }
    }
 
 
  pos = noninc_search_from_pos (string, noninc_history_pos + dir, dir);
  pos = noninc_search_from_pos (string, noninc_history_pos + dir, dir);
  if (pos == -1)
  if (pos == -1)
    {
    {
      /* Search failed, current history position unchanged. */
      /* Search failed, current history position unchanged. */
      maybe_unsave_line ();
      maybe_unsave_line ();
      rl_clear_message ();
      rl_clear_message ();
      rl_point = 0;
      rl_point = 0;
      ding ();
      ding ();
      return;
      return;
    }
    }
 
 
  noninc_history_pos = pos;
  noninc_history_pos = pos;
 
 
  oldpos = where_history ();
  oldpos = where_history ();
  history_set_pos (noninc_history_pos);
  history_set_pos (noninc_history_pos);
  entry = current_history ();
  entry = current_history ();
#if defined (VI_MODE)
#if defined (VI_MODE)
  if (rl_editing_mode != vi_mode)
  if (rl_editing_mode != vi_mode)
#endif
#endif
  history_set_pos (oldpos);
  history_set_pos (oldpos);
 
 
  line_len = strlen (entry->line);
  line_len = strlen (entry->line);
  if (line_len >= rl_line_buffer_len)
  if (line_len >= rl_line_buffer_len)
    rl_extend_line_buffer (line_len);
    rl_extend_line_buffer (line_len);
  strcpy (rl_line_buffer, entry->line);
  strcpy (rl_line_buffer, entry->line);
 
 
  rl_undo_list = (UNDO_LIST *)entry->data;
  rl_undo_list = (UNDO_LIST *)entry->data;
  rl_end = strlen (rl_line_buffer);
  rl_end = strlen (rl_line_buffer);
  rl_point = 0;
  rl_point = 0;
  rl_clear_message ();
  rl_clear_message ();
 
 
  if (saved_line_for_history)
  if (saved_line_for_history)
    _rl_free_history_entry (saved_line_for_history);
    _rl_free_history_entry (saved_line_for_history);
  saved_line_for_history = (HIST_ENTRY *)NULL;
  saved_line_for_history = (HIST_ENTRY *)NULL;
}
}
 
 
/* Search non-interactively through the history list.  DIR < 0 means to
/* Search non-interactively through the history list.  DIR < 0 means to
   search backwards through the history of previous commands; otherwise
   search backwards through the history of previous commands; otherwise
   the search is for commands subsequent to the current position in the
   the search is for commands subsequent to the current position in the
   history list.  PCHAR is the character to use for prompting when reading
   history list.  PCHAR is the character to use for prompting when reading
   the search string; if not specified (0), it defaults to `:'. */
   the search string; if not specified (0), it defaults to `:'. */
static void
static void
noninc_search (dir, pchar)
noninc_search (dir, pchar)
     int dir;
     int dir;
     int pchar;
     int pchar;
{
{
  int saved_point, c;
  int saved_point, c;
  char *p;
  char *p;
 
 
  maybe_save_line ();
  maybe_save_line ();
  saved_point = rl_point;
  saved_point = rl_point;
 
 
  /* Use the line buffer to read the search string. */
  /* Use the line buffer to read the search string. */
  rl_line_buffer[0] = 0;
  rl_line_buffer[0] = 0;
  rl_end = rl_point = 0;
  rl_end = rl_point = 0;
 
 
  p = _rl_make_prompt_for_search (pchar ? pchar : ':');
  p = _rl_make_prompt_for_search (pchar ? pchar : ':');
  rl_message (p, 0, 0);
  rl_message (p, 0, 0);
  free (p);
  free (p);
 
 
#define SEARCH_RETURN rl_restore_prompt (); return
#define SEARCH_RETURN rl_restore_prompt (); return
 
 
  /* Read the search string. */
  /* Read the search string. */
  while (c = rl_read_key ())
  while (c = rl_read_key ())
    {
    {
      switch (c)
      switch (c)
        {
        {
        case CTRL('H'):
        case CTRL('H'):
        case RUBOUT:
        case RUBOUT:
          if (rl_point == 0)
          if (rl_point == 0)
            {
            {
              maybe_unsave_line ();
              maybe_unsave_line ();
              rl_clear_message ();
              rl_clear_message ();
              rl_point = saved_point;
              rl_point = saved_point;
              SEARCH_RETURN;
              SEARCH_RETURN;
            }
            }
          rl_rubout (1, c);
          rl_rubout (1, c);
          break;
          break;
 
 
        case CTRL('W'):
        case CTRL('W'):
          rl_unix_word_rubout (1, c);
          rl_unix_word_rubout (1, c);
          break;
          break;
 
 
        case CTRL('U'):
        case CTRL('U'):
          rl_unix_line_discard (1, c);
          rl_unix_line_discard (1, c);
          break;
          break;
 
 
        case RETURN:
        case RETURN:
        case NEWLINE:
        case NEWLINE:
          goto dosearch;
          goto dosearch;
          /* NOTREACHED */
          /* NOTREACHED */
          break;
          break;
 
 
        case CTRL('C'):
        case CTRL('C'):
        case CTRL('G'):
        case CTRL('G'):
          maybe_unsave_line ();
          maybe_unsave_line ();
          rl_clear_message ();
          rl_clear_message ();
          rl_point = saved_point;
          rl_point = saved_point;
          ding ();
          ding ();
          SEARCH_RETURN;
          SEARCH_RETURN;
 
 
        default:
        default:
          rl_insert (1, c);
          rl_insert (1, c);
          break;
          break;
        }
        }
      (*rl_redisplay_function) ();
      (*rl_redisplay_function) ();
    }
    }
 
 
 dosearch:
 dosearch:
  /* If rl_point == 0, we want to re-use the previous search string and
  /* If rl_point == 0, we want to re-use the previous search string and
     start from the saved history position.  If there's no previous search
     start from the saved history position.  If there's no previous search
     string, punt. */
     string, punt. */
  if (rl_point == 0)
  if (rl_point == 0)
    {
    {
      if (!noninc_search_string)
      if (!noninc_search_string)
        {
        {
          ding ();
          ding ();
          SEARCH_RETURN;
          SEARCH_RETURN;
        }
        }
    }
    }
  else
  else
    {
    {
      /* We want to start the search from the current history position. */
      /* We want to start the search from the current history position. */
      noninc_history_pos = where_history ();
      noninc_history_pos = where_history ();
      if (noninc_search_string)
      if (noninc_search_string)
        free (noninc_search_string);
        free (noninc_search_string);
      noninc_search_string = savestring (rl_line_buffer);
      noninc_search_string = savestring (rl_line_buffer);
    }
    }
 
 
  rl_restore_prompt ();
  rl_restore_prompt ();
  noninc_dosearch (noninc_search_string, dir);
  noninc_dosearch (noninc_search_string, dir);
}
}
 
 
/* Search forward through the history list for a string.  If the vi-mode
/* Search forward through the history list for a string.  If the vi-mode
   code calls this, KEY will be `?'. */
   code calls this, KEY will be `?'. */
int
int
rl_noninc_forward_search (count, key)
rl_noninc_forward_search (count, key)
     int count, key;
     int count, key;
{
{
  noninc_search (1, (key == '?') ? '?' : 0);
  noninc_search (1, (key == '?') ? '?' : 0);
  return 0;
  return 0;
}
}
 
 
/* Reverse search the history list for a string.  If the vi-mode code
/* Reverse search the history list for a string.  If the vi-mode code
   calls this, KEY will be `/'. */
   calls this, KEY will be `/'. */
int
int
rl_noninc_reverse_search (count, key)
rl_noninc_reverse_search (count, key)
     int count, key;
     int count, key;
{
{
  noninc_search (-1, (key == '/') ? '/' : 0);
  noninc_search (-1, (key == '/') ? '/' : 0);
  return 0;
  return 0;
}
}
 
 
/* Search forward through the history list for the last string searched
/* Search forward through the history list for the last string searched
   for.  If there is no saved search string, abort. */
   for.  If there is no saved search string, abort. */
int
int
rl_noninc_forward_search_again (count, key)
rl_noninc_forward_search_again (count, key)
     int count, key;
     int count, key;
{
{
  if (!noninc_search_string)
  if (!noninc_search_string)
    {
    {
      ding ();
      ding ();
      return (-1);
      return (-1);
    }
    }
  noninc_dosearch (noninc_search_string, 1);
  noninc_dosearch (noninc_search_string, 1);
  return 0;
  return 0;
}
}
 
 
/* Reverse search in the history list for the last string searched
/* Reverse search in the history list for the last string searched
   for.  If there is no saved search string, abort. */
   for.  If there is no saved search string, abort. */
int
int
rl_noninc_reverse_search_again (count, key)
rl_noninc_reverse_search_again (count, key)
     int count, key;
     int count, key;
{
{
  if (!noninc_search_string)
  if (!noninc_search_string)
    {
    {
      ding ();
      ding ();
      return (-1);
      return (-1);
    }
    }
  noninc_dosearch (noninc_search_string, -1);
  noninc_dosearch (noninc_search_string, -1);
  return 0;
  return 0;
}
}
 
 
static int
static int
rl_history_search_internal (count, direction)
rl_history_search_internal (count, direction)
     int count, direction;
     int count, direction;
{
{
  HIST_ENTRY *temp, *old_temp;
  HIST_ENTRY *temp, *old_temp;
  int line_len;
  int line_len;
 
 
  maybe_save_line ();
  maybe_save_line ();
 
 
  temp = old_temp = (HIST_ENTRY *)NULL;
  temp = old_temp = (HIST_ENTRY *)NULL;
  while (count)
  while (count)
    {
    {
      temp = (direction < 0) ? previous_history () : next_history ();
      temp = (direction < 0) ? previous_history () : next_history ();
      if (temp == 0)
      if (temp == 0)
        break;
        break;
      /* On an empty prefix, make this the same as previous-history. */
      /* On an empty prefix, make this the same as previous-history. */
      if (rl_point == 0)
      if (rl_point == 0)
        {
        {
          count--;
          count--;
          continue;
          continue;
        }
        }
      if (STREQN (rl_line_buffer, temp->line, rl_point))
      if (STREQN (rl_line_buffer, temp->line, rl_point))
        {
        {
          /* Don't find multiple instances of the same line. */
          /* Don't find multiple instances of the same line. */
          if (prev_line_found && STREQ (prev_line_found, temp->line))
          if (prev_line_found && STREQ (prev_line_found, temp->line))
            continue;
            continue;
          if (direction < 0)
          if (direction < 0)
            old_temp = temp;
            old_temp = temp;
          prev_line_found = temp->line;
          prev_line_found = temp->line;
          count--;
          count--;
        }
        }
    }
    }
 
 
  if (temp == 0)
  if (temp == 0)
    {
    {
      if (direction < 0 && old_temp)
      if (direction < 0 && old_temp)
        temp = old_temp;
        temp = old_temp;
      else
      else
        {
        {
          maybe_unsave_line ();
          maybe_unsave_line ();
          ding ();
          ding ();
          return 1;
          return 1;
        }
        }
    }
    }
 
 
  line_len = strlen (temp->line);
  line_len = strlen (temp->line);
  if (line_len >= rl_line_buffer_len)
  if (line_len >= rl_line_buffer_len)
    rl_extend_line_buffer (line_len);
    rl_extend_line_buffer (line_len);
  strcpy (rl_line_buffer, temp->line);
  strcpy (rl_line_buffer, temp->line);
  rl_undo_list = (UNDO_LIST *)temp->data;
  rl_undo_list = (UNDO_LIST *)temp->data;
  rl_end = line_len;
  rl_end = line_len;
  return 0;
  return 0;
}
}
 
 
/* Search forward in the history for the string of characters
/* Search forward in the history for the string of characters
   from the start of the line to rl_point.  This is a non-incremental
   from the start of the line to rl_point.  This is a non-incremental
   search. */
   search. */
int
int
rl_history_search_forward (count, ignore)
rl_history_search_forward (count, ignore)
     int count, ignore;
     int count, ignore;
{
{
  if (count == 0)
  if (count == 0)
    return (0);
    return (0);
  if (rl_last_func != rl_history_search_forward)
  if (rl_last_func != rl_history_search_forward)
    prev_line_found = (char *)NULL;
    prev_line_found = (char *)NULL;
  return (rl_history_search_internal (abs (count), (count > 0) ? 1 : -1));
  return (rl_history_search_internal (abs (count), (count > 0) ? 1 : -1));
}
}
 
 
/* Search backward through the history for the string of characters
/* Search backward through the history for the string of characters
   from the start of the line to rl_point.  This is a non-incremental
   from the start of the line to rl_point.  This is a non-incremental
   search. */
   search. */
int
int
rl_history_search_backward (count, ignore)
rl_history_search_backward (count, ignore)
     int count, ignore;
     int count, ignore;
{
{
  if (count == 0)
  if (count == 0)
    return (0);
    return (0);
  if (rl_last_func != rl_history_search_backward)
  if (rl_last_func != rl_history_search_backward)
    prev_line_found = (char *)NULL;
    prev_line_found = (char *)NULL;
  return (rl_history_search_internal (abs (count), (count > 0) ? -1 : 1));
  return (rl_history_search_internal (abs (count), (count > 0) ? -1 : 1));
}
}
 
 

powered by: WebSVN 2.1.0

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