Factor out the common code in lookup_{static,global}_symbol
[deliverable/binutils-gdb.git] / readline / search.c
index bf525e6d3ce3aee56c8f2befb2ff6c217e3c05ab..c9c1f5d1dd331cba431ec891300ba3693f6e1c14 100644 (file)
@@ -1,6 +1,6 @@
 /* search.c - code for non-incremental searching in emacs and vi modes. */
 
-/* Copyright (C) 1992-2009 Free Software Foundation, Inc.
+/* Copyright (C) 1992-2017 Free Software Foundation, Inc.
 
    This file is part of the GNU Readline Library (Readline), a library
    for reading lines of text with interactive input and history editing.      
@@ -43,6 +43,7 @@
 
 #include "readline.h"
 #include "history.h"
+#include "histlib.h"
 
 #include "rlprivate.h"
 #include "xmalloc.h"
@@ -57,7 +58,7 @@ _rl_search_cxt *_rl_nscxt = 0;
 extern HIST_ENTRY *_rl_saved_line_for_history;
 
 /* Functions imported from the rest of the library. */
-extern int _rl_free_history_entry PARAMS((HIST_ENTRY *));
+extern void _rl_free_history_entry PARAMS((HIST_ENTRY *));
 
 static char *noninc_search_string = (char *) NULL;
 static int noninc_history_pos;
@@ -66,18 +67,19 @@ static char *prev_line_found = (char *) NULL;
 
 static int rl_history_search_len;
 static int rl_history_search_pos;
+static int rl_history_search_flags;
+
 static char *history_search_string;
 static int history_string_size;
 
 static void make_history_line_current PARAMS((HIST_ENTRY *));
-static int noninc_search_from_pos PARAMS((char *, int, int));
-static int noninc_dosearch PARAMS((char *, int));
+static int noninc_search_from_pos PARAMS((char *, int, int, int, int *));
+static int noninc_dosearch PARAMS((char *, int, int));
 static int noninc_search PARAMS((int, int));
 static int rl_history_search_internal PARAMS((int, int));
-static void rl_history_search_reinit PARAMS((void));
+static void rl_history_search_reinit PARAMS((int));
 
 static _rl_search_cxt *_rl_nsearch_init PARAMS((int, int));
-static int _rl_nsearch_cleanup PARAMS((_rl_search_cxt *, int));
 static void _rl_nsearch_abort PARAMS((_rl_search_cxt *));
 static int _rl_nsearch_dispatch PARAMS((_rl_search_cxt *, int));
 
@@ -85,8 +87,7 @@ static int _rl_nsearch_dispatch PARAMS((_rl_search_cxt *, int));
    current line.  This doesn't do anything with rl_point; the caller
    must set it. */
 static void
-make_history_line_current (entry)
-     HIST_ENTRY *entry;
+make_history_line_current (HIST_ENTRY *entry)
 {
   _rl_replace_text (entry->line, 0, rl_end);
   _rl_fix_point (1);
@@ -110,11 +111,10 @@ make_history_line_current (entry)
    for STRING.  DIR < 0 means to search backwards through the history list,
    DIR >= 0 means to search forward. */
 static int
-noninc_search_from_pos (string, pos, dir)
-     char *string;
-     int pos, dir;
+noninc_search_from_pos (char *string, int pos, int dir, int flags, int *ncp)
 {
-  int ret, old;
+  int ret, old, sflags;
+  char *s;
 
   if (pos < 0)
     return -1;
@@ -124,12 +124,28 @@ noninc_search_from_pos (string, pos, dir)
     return -1;
 
   RL_SETSTATE(RL_STATE_SEARCH);
-  if (*string == '^')
+  /* These functions return the match offset in the line; history_offset gives
+     the matching line in the history list */
+  if (flags & SF_PATTERN)
+    {
+      s = string;
+      sflags = 0;              /* Non-anchored search */
+      if (*s == '^')
+       {
+         sflags |= ANCHORED_SEARCH;
+         s++;
+       }
+      ret = _hs_history_patsearch (s, dir, sflags);
+    }
+  else if (*string == '^')
     ret = history_search_prefix (string + 1, dir);
   else
     ret = history_search (string, dir);
   RL_UNSETSTATE(RL_STATE_SEARCH);
 
+  if (ncp)
+    *ncp = ret;                /* caller will catch -1 to indicate no-op */
+
   if (ret != -1)
     ret = where_history ();
 
@@ -141,9 +157,7 @@ noninc_search_from_pos (string, pos, dir)
    search is backwards through previous entries, else through subsequent
    entries.  Returns 1 if the search was successful, 0 otherwise. */
 static int
-noninc_dosearch (string, dir)
-     char *string;
-     int dir;
+noninc_dosearch (char *string, int dir, int flags)
 {
   int oldpos, pos;
   HIST_ENTRY *entry;
@@ -154,7 +168,7 @@ noninc_dosearch (string, dir)
       return 0;
     }
 
-  pos = noninc_search_from_pos (string, noninc_history_pos + dir, dir);
+  pos = noninc_search_from_pos (string, noninc_history_pos + dir, dir, flags, (int *)0);
   if (pos == -1)
     {
       /* Search failed, current history position unchanged. */
@@ -169,7 +183,8 @@ noninc_dosearch (string, dir)
 
   oldpos = where_history ();
   history_set_pos (noninc_history_pos);
-  entry = current_history ();
+  entry = current_history ();          /* will never be NULL after successful search */
+  
 #if defined (VI_MODE)
   if (rl_editing_mode != vi_mode)
 #endif
@@ -185,8 +200,7 @@ noninc_dosearch (string, dir)
 }
 
 static _rl_search_cxt *
-_rl_nsearch_init (dir, pchar)
-     int dir, pchar;
+_rl_nsearch_init (int dir, int pchar)
 {
   _rl_search_cxt *cxt;
   char *p;
@@ -194,6 +208,10 @@ _rl_nsearch_init (dir, pchar)
   cxt = _rl_scxt_alloc (RL_SEARCH_NSEARCH, 0);
   if (dir < 0)
     cxt->sflags |= SF_REVERSE;         /* not strictly needed */
+#if defined (VI_MODE)
+  if (VI_COMMAND_MODE() && (pchar == '?' || pchar == '/'))
+    cxt->sflags |= SF_PATTERN;
+#endif
 
   cxt->direction = dir;
   cxt->history_pos = cxt->save_line;
@@ -220,10 +238,8 @@ _rl_nsearch_init (dir, pchar)
   return cxt;
 }
 
-static int
-_rl_nsearch_cleanup (cxt, r)
-     _rl_search_cxt *cxt;
-     int r;
+int
+_rl_nsearch_cleanup (_rl_search_cxt *cxt, int r)
 {
   _rl_scxt_dispose (cxt, 0);
   _rl_nscxt = 0;
@@ -234,8 +250,7 @@ _rl_nsearch_cleanup (cxt, r)
 }
 
 static void
-_rl_nsearch_abort (cxt)
-     _rl_search_cxt *cxt;
+_rl_nsearch_abort (_rl_search_cxt *cxt)
 {
   rl_maybe_unsave_line ();
   rl_clear_message ();
@@ -250,10 +265,11 @@ _rl_nsearch_abort (cxt)
    if the caller should abort the search, 0 if we should break out of the
    loop, and 1 if we should continue to read characters. */
 static int
-_rl_nsearch_dispatch (cxt, c)
-     _rl_search_cxt *cxt;
-     int c;
+_rl_nsearch_dispatch (_rl_search_cxt *cxt, int c)
 {
+  if (c < 0)
+    c = CTRL ('C');  
+
   switch (c)
     {
     case CTRL('W'):
@@ -303,8 +319,7 @@ _rl_nsearch_dispatch (cxt, c)
    using _rl_nsearch_cleanup ().  Returns 1 if the search was successful,
    0 otherwise. */
 static int
-_rl_nsearch_dosearch (cxt)
-     _rl_search_cxt *cxt;
+_rl_nsearch_dosearch (_rl_search_cxt *cxt)
 {
   rl_mark = cxt->save_mark;
 
@@ -337,7 +352,7 @@ _rl_nsearch_dosearch (cxt)
     }
 
   rl_restore_prompt ();
-  return (noninc_dosearch (noninc_search_string, cxt->direction));
+  return (noninc_dosearch (noninc_search_string, cxt->direction, cxt->sflags&SF_PATTERN));
 }
 
 /* Search non-interactively through the history list.  DIR < 0 means to
@@ -346,9 +361,7 @@ _rl_nsearch_dosearch (cxt)
    history list.  PCHAR is the character to use for prompting when reading
    the search string; if not specified (0), it defaults to `:'. */
 static int
-noninc_search (dir, pchar)
-     int dir;
-     int pchar;
+noninc_search (int dir, int pchar)
 {
   _rl_search_cxt *cxt;
   int c, r;
@@ -364,6 +377,12 @@ noninc_search (dir, pchar)
     {
       c = _rl_search_getchar (cxt);
 
+      if (c < 0)
+       {
+         _rl_nsearch_abort (cxt);
+         return 1;
+       }
+         
       if (c == 0)
        break;
 
@@ -381,8 +400,7 @@ noninc_search (dir, pchar)
 /* Search forward through the history list for a string.  If the vi-mode
    code calls this, KEY will be `?'. */
 int
-rl_noninc_forward_search (count, key)
-     int count, key;
+rl_noninc_forward_search (int count, int key)
 {
   return noninc_search (1, (key == '?') ? '?' : 0);
 }
@@ -390,54 +408,68 @@ rl_noninc_forward_search (count, key)
 /* Reverse search the history list for a string.  If the vi-mode code
    calls this, KEY will be `/'. */
 int
-rl_noninc_reverse_search (count, key)
-     int count, key;
+rl_noninc_reverse_search (int count, int key)
 {
   return noninc_search (-1, (key == '/') ? '/' : 0);
 }
 
 /* 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.  If the vi-mode code
+   calls this, KEY will be `N'. */
 int
-rl_noninc_forward_search_again (count, key)
-     int count, key;
+rl_noninc_forward_search_again (int count, int key)
 {
   int r;
 
   if (!noninc_search_string)
     {
       rl_ding ();
-      return (-1);
+      return (1);
     }
-  r = noninc_dosearch (noninc_search_string, 1);
+#if defined (VI_MODE)
+  if (VI_COMMAND_MODE() && key == 'N')
+    r = noninc_dosearch (noninc_search_string, 1, SF_PATTERN);
+  else
+#endif
+    r = noninc_dosearch (noninc_search_string, 1, 0);
   return (r != 1);
 }
 
 /* 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.  If the vi-mode code
+   calls this, KEY will be `n'. */
 int
-rl_noninc_reverse_search_again (count, key)
-     int count, key;
+rl_noninc_reverse_search_again (int count, int key)
 {
   int r;
 
   if (!noninc_search_string)
     {
       rl_ding ();
-      return (-1);
+      return (1);
     }
-  r = noninc_dosearch (noninc_search_string, -1);
+#if defined (VI_MODE)
+  if (VI_COMMAND_MODE() && key == 'n')
+    r = noninc_dosearch (noninc_search_string, -1, SF_PATTERN);
+  else
+#endif
+    r = noninc_dosearch (noninc_search_string, -1, 0);
   return (r != 1);
 }
 
 #if defined (READLINE_CALLBACKS)
 int
-_rl_nsearch_callback (cxt)
-     _rl_search_cxt *cxt;
+_rl_nsearch_callback (_rl_search_cxt *cxt)
 {
   int c, r;
 
   c = _rl_search_getchar (cxt);
+  if (c <= 0)
+    {
+      if (c < 0)
+        _rl_nsearch_abort (cxt);
+      return 1;
+    }
   r = _rl_nsearch_dispatch (cxt, c);
   if (r != 0)
     return 1;
@@ -448,21 +480,24 @@ _rl_nsearch_callback (cxt)
 #endif
   
 static int
-rl_history_search_internal (count, dir)
-     int count, dir;
+rl_history_search_internal (int count, int dir)
 {
   HIST_ENTRY *temp;
-  int ret, oldpos;
+  int ret, oldpos, newcol;
+  char *t;
 
   rl_maybe_save_line ();
   temp = (HIST_ENTRY *)NULL;
 
-  /* Search COUNT times through the history for a line whose prefix
-     matches history_search_string.  When this loop finishes, TEMP,
-     if non-null, is the history line to copy into the line buffer. */
+  /* Search COUNT times through the history for a line matching
+     history_search_string.  If history_search_string[0] == '^', the
+     line must match from the start; otherwise any substring can match.
+     When this loop finishes, TEMP, if non-null, is the history line to
+     copy into the line buffer. */
   while (count)
     {
-      ret = noninc_search_from_pos (history_search_string, rl_history_search_pos + dir, dir);
+      RL_CHECK_SIGNALS ();
+      ret = noninc_search_from_pos (history_search_string, rl_history_search_pos + dir, dir, 0, &newcol);
       if (ret == -1)
        break;
 
@@ -470,7 +505,7 @@ rl_history_search_internal (count, dir)
       rl_history_search_pos = ret;
       oldpos = where_history ();
       history_set_pos (rl_history_search_pos);
-      temp = current_history ();
+      temp = current_history ();       /* will never be NULL after successful search */
       history_set_pos (oldpos);
 
       /* Don't find multiple instances of the same line. */
@@ -505,45 +540,62 @@ rl_history_search_internal (count, dir)
   /* Copy the line we found into the current line buffer. */
   make_history_line_current (temp);
 
-  rl_point = rl_history_search_len;
+  /* decide where to put rl_point -- need to change this for pattern search */
+  if (rl_history_search_flags & ANCHORED_SEARCH)
+    rl_point = rl_history_search_len;  /* easy case */
+  else
+    {
+#if 0
+      t = strstr (rl_line_buffer, history_search_string);      /* XXX */
+      rl_point = t ? (int)(t - rl_line_buffer) + rl_history_search_len : rl_end;
+#else
+      rl_point = (newcol >= 0) ? newcol : rl_end;
+#endif
+    }
   rl_mark = rl_end;
 
   return 0;
 }
 
 static void
-rl_history_search_reinit ()
+rl_history_search_reinit (int flags)
 {
+  int sind;
+
   rl_history_search_pos = where_history ();
   rl_history_search_len = rl_point;
+  rl_history_search_flags = flags;
+
   prev_line_found = (char *)NULL;
   if (rl_point)
     {
+      /* Allocate enough space for anchored and non-anchored searches */
       if (rl_history_search_len >= history_string_size - 2)
        {
          history_string_size = rl_history_search_len + 2;
          history_search_string = (char *)xrealloc (history_search_string, history_string_size);
        }
-      history_search_string[0] = '^';
-      strncpy (history_search_string + 1, rl_line_buffer, rl_point);
-      history_search_string[rl_point + 1] = '\0';
+      sind = 0;
+      if (flags & ANCHORED_SEARCH)
+       history_search_string[sind++] = '^';
+      strncpy (history_search_string + sind, rl_line_buffer, rl_point);
+      history_search_string[rl_point + sind] = '\0';
     }
   _rl_free_saved_history_line ();
 }
 
 /* Search forward in the history for the string of characters
    from the start of the line to rl_point.  This is a non-incremental
-   search. */
+   search.  The search is anchored to the beginning of the history line. */
 int
-rl_history_search_forward (count, ignore)
-     int count, ignore;
+rl_history_search_forward (int count, int ignore)
 {
   if (count == 0)
     return (0);
 
   if (rl_last_func != rl_history_search_forward &&
       rl_last_func != rl_history_search_backward)
-    rl_history_search_reinit ();
+    rl_history_search_reinit (ANCHORED_SEARCH);
 
   if (rl_history_search_len == 0)
     return (rl_get_next_history (count, ignore));
@@ -554,15 +606,51 @@ rl_history_search_forward (count, ignore)
    from the start of the line to rl_point.  This is a non-incremental
    search. */
 int
-rl_history_search_backward (count, ignore)
-     int count, ignore;
+rl_history_search_backward (int count, int ignore)
 {
   if (count == 0)
     return (0);
 
   if (rl_last_func != rl_history_search_forward &&
       rl_last_func != rl_history_search_backward)
-    rl_history_search_reinit ();
+    rl_history_search_reinit (ANCHORED_SEARCH);
+
+  if (rl_history_search_len == 0)
+    return (rl_get_previous_history (count, ignore));
+  return (rl_history_search_internal (abs (count), (count > 0) ? -1 : 1));
+}
+
+/* Search forward in the history for the string of characters
+   from the start of the line to rl_point.  This is a non-incremental
+   search.  The search succeeds if the search string is present anywhere
+   in the history line. */
+int
+rl_history_substr_search_forward (int count, int ignore)
+{
+  if (count == 0)
+    return (0);
+
+  if (rl_last_func != rl_history_substr_search_forward &&
+      rl_last_func != rl_history_substr_search_backward)
+    rl_history_search_reinit (NON_ANCHORED_SEARCH);
+
+  if (rl_history_search_len == 0)
+    return (rl_get_next_history (count, ignore));
+  return (rl_history_search_internal (abs (count), (count > 0) ? 1 : -1));
+}
+
+/* Search backward through the history for the string of characters
+   from the start of the line to rl_point.  This is a non-incremental
+   search. */
+int
+rl_history_substr_search_backward (int count, int ignore)
+{
+  if (count == 0)
+    return (0);
+
+  if (rl_last_func != rl_history_substr_search_forward &&
+      rl_last_func != rl_history_substr_search_backward)
+    rl_history_search_reinit (NON_ANCHORED_SEARCH);
 
   if (rl_history_search_len == 0)
     return (rl_get_previous_history (count, ignore));
This page took 0.028358 seconds and 4 git commands to generate.