]> git.ipfire.org Git - thirdparty/bash.git/blob - lib/readline/histsearch.c
76303f4c3993aafcd90284181a0e0918de3eee5b
[thirdparty/bash.git] / lib / readline / histsearch.c
1 /* histsearch.c -- searching the history list. */
2
3 /* Copyright (C) 1989, 1992 Free Software Foundation, Inc.
4
5 This file contains the GNU History Library (the Library), a set of
6 routines for managing the text of previously typed lines.
7
8 The Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 The Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 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 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
22
23 #define READLINE_LIBRARY
24
25 #if defined (HAVE_CONFIG_H)
26 # include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #if defined (HAVE_STDLIB_H)
31 # include <stdlib.h>
32 #else
33 # include "ansi_stdlib.h"
34 #endif /* HAVE_STDLIB_H */
35 #if defined (HAVE_UNISTD_H)
36 # ifdef _MINIX
37 # include <sys/types.h>
38 # endif
39 # include <unistd.h>
40 #endif
41 #if defined (HAVE_STRING_H)
42 # include <string.h>
43 #else
44 # include <strings.h>
45 #endif /* !HAVE_STRING_H */
46
47 #include "history.h"
48 #include "histlib.h"
49
50 /* The list of alternate characters that can delimit a history search
51 string. */
52 char *history_search_delimiter_chars = (char *)NULL;
53
54 static int history_search_internal PARAMS((const char *, int, int));
55
56 /* Search the history for STRING, starting at history_offset.
57 If DIRECTION < 0, then the search is through previous entries, else
58 through subsequent. If ANCHORED is non-zero, the string must
59 appear at the beginning of a history line, otherwise, the string
60 may appear anywhere in the line. If the string is found, then
61 current_history () is the history entry, and the value of this
62 function is the offset in the line of that history entry that the
63 string was found in. Otherwise, nothing is changed, and a -1 is
64 returned. */
65
66 static int
67 history_search_internal (string, direction, anchored)
68 const char *string;
69 int direction, anchored;
70 {
71 register int i, reverse;
72 register char *line;
73 register int line_index;
74 int string_len;
75 HIST_ENTRY **the_history; /* local */
76
77 i = history_offset;
78 reverse = (direction < 0);
79
80 /* Take care of trivial cases first. */
81 if (string == 0 || *string == '\0')
82 return (-1);
83
84 if (!history_length || ((i == history_length) && !reverse))
85 return (-1);
86
87 if (reverse && (i == history_length))
88 i--;
89
90 #define NEXT_LINE() do { if (reverse) i--; else i++; } while (0)
91
92 the_history = history_list ();
93 string_len = strlen (string);
94 while (1)
95 {
96 /* Search each line in the history list for STRING. */
97
98 /* At limit for direction? */
99 if ((reverse && i < 0) || (!reverse && i == history_length))
100 return (-1);
101
102 line = the_history[i]->line;
103 line_index = strlen (line);
104
105 /* If STRING is longer than line, no match. */
106 if (string_len > line_index)
107 {
108 NEXT_LINE ();
109 continue;
110 }
111
112 /* Handle anchored searches first. */
113 if (anchored == ANCHORED_SEARCH)
114 {
115 if (STREQN (string, line, string_len))
116 {
117 history_offset = i;
118 return (0);
119 }
120
121 NEXT_LINE ();
122 continue;
123 }
124
125 /* Do substring search. */
126 if (reverse)
127 {
128 line_index -= string_len;
129
130 while (line_index >= 0)
131 {
132 if (STREQN (string, line + line_index, string_len))
133 {
134 history_offset = i;
135 return (line_index);
136 }
137 line_index--;
138 }
139 }
140 else
141 {
142 register int limit;
143
144 limit = line_index - string_len + 1;
145 line_index = 0;
146
147 while (line_index < limit)
148 {
149 if (STREQN (string, line + line_index, string_len))
150 {
151 history_offset = i;
152 return (line_index);
153 }
154 line_index++;
155 }
156 }
157 NEXT_LINE ();
158 }
159 }
160
161 /* Do a non-anchored search for STRING through the history in DIRECTION. */
162 int
163 history_search (string, direction)
164 const char *string;
165 int direction;
166 {
167 return (history_search_internal (string, direction, NON_ANCHORED_SEARCH));
168 }
169
170 /* Do an anchored search for string through the history in DIRECTION. */
171 int
172 history_search_prefix (string, direction)
173 const char *string;
174 int direction;
175 {
176 return (history_search_internal (string, direction, ANCHORED_SEARCH));
177 }
178
179 /* Search for STRING in the history list. DIR is < 0 for searching
180 backwards. POS is an absolute index into the history list at
181 which point to begin searching. */
182 int
183 history_search_pos (string, dir, pos)
184 const char *string;
185 int dir, pos;
186 {
187 int ret, old;
188
189 old = where_history ();
190 history_set_pos (pos);
191 if (history_search (string, dir) == -1)
192 {
193 history_set_pos (old);
194 return (-1);
195 }
196 ret = where_history ();
197 history_set_pos (old);
198 return ret;
199 }