File: histsearch.c

package info (click to toggle)
bash 5.3-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 43,860 kB
  • sloc: ansic: 134,738; sh: 8,866; yacc: 5,966; makefile: 4,697; perl: 4,105; asm: 48; awk: 23; sed: 16
file content (343 lines) | stat: -rw-r--r-- 9,117 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/* histsearch.c -- searching the history list. */

/* Copyright (C) 1989, 1992-2009,2017,2021-2024 Free Software Foundation, Inc.

   This file contains the GNU History Library (History), a set of
   routines for managing the text of previously typed lines.

   History is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   History is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with History.  If not, see <http://www.gnu.org/licenses/>.
*/

#define READLINE_LIBRARY

#if defined (HAVE_CONFIG_H)
#  include <config.h>
#endif

#include <stdio.h>
#if defined (HAVE_STDLIB_H)
#  include <stdlib.h>
#else
#  include "ansi_stdlib.h"
#endif /* HAVE_STDLIB_H */

#if defined (HAVE_UNISTD_H)
#  ifdef _MINIX
#    include <sys/types.h>
#  endif
#  include <unistd.h>
#endif

#if defined (HAVE_FNMATCH)
#  include <fnmatch.h>
#endif

#include "history.h"
#include "histlib.h"
#include "xmalloc.h"

#include "rlmbutil.h"

/* The list of alternate characters that can delimit a history search
   string. */
char *history_search_delimiter_chars = (char *)NULL;

static int history_search_internal (const char *, int, int, int);

/* Search the history for STRING, starting at history_offset.
   If LISTDIR < 0, then the search is through previous entries, else
   through subsequent. If ANCHORED is non-zero, the string must
   appear at the beginning of a history line, otherwise, the string
   may appear anywhere in the line. If the search is not anchored, LINEDIR
   determines how the line is searched: if it is < 0, the search proceeds
   from the end of the line to the beginning, otherwise the substring search
   starts at the beginning of each history entry. If PATSEARCH is non-zero,
   and fnmatch(3) is available, fnmatch is used to match the string instead
   of a simple string comparison. If IGNORECASE is set, the string comparison
   is performed case-insensitively. If the string is found, then
   current_history () is the history entry, and the value of this
   function is the offset in the line of that history entry in which the
   string was found. Otherwise, nothing is changed, and a -1 is returned. */

static int
history_search_internal (const char *string, int listdir, int linedir, int flags)
{
  int i, reverse;
  char *line;
  size_t string_len, line_len;
  int line_index;		/* can't be unsigned */
  int anchored, patsearch, igncase;
  int found, mb_cur_max;
  HIST_ENTRY **the_history; 	/* local */

  i = history_offset;
  reverse = (listdir < 0);
  anchored = (flags & ANCHORED_SEARCH);
#if defined (HAVE_FNMATCH)
  patsearch = (flags & PATTERN_SEARCH);
#else
  patsearch = 0;
#endif
  igncase = (flags & CASEFOLD_SEARCH);

  /* Take care of trivial cases first. */
  if (string == 0 || *string == '\0')
    return (-1);

  if (!history_length || ((i >= history_length) && !reverse))
    return (-1);

  if (reverse && (i >= history_length))
    i = history_length - 1;

  mb_cur_max = MB_CUR_MAX;

#define NEXT_LINE() do { if (reverse) i--; else i++; } while (0)

  the_history = history_list ();
  string_len = strlen (string);
  while (1)
    {
      /* Search each line in the history list for STRING. */

      /* At limit for direction? */
      if ((reverse && i < 0) || (!reverse && i == history_length))
	return (-1);

      line = the_history[i]->line;
      line_len = line_index = strlen (line);

      /* If STRING is longer than line, no match. */
      if (patsearch == 0 && (string_len > line_index))
	{
	  NEXT_LINE ();
	  continue;
	}

      /* Handle anchored searches first. */
      if (anchored == ANCHORED_SEARCH)
	{
	  found = 0;
#if defined (HAVE_FNMATCH)
	  if (patsearch)
	    found = fnmatch (string, line, 0) == 0;
	  else
#endif
	  if (igncase)
	    {
#if defined (HANDLE_MULTIBYTE)
	      if (mb_cur_max > 1)	/* no rl_byte_oriented equivalent */
		found = _rl_mb_strcaseeqn (string, string_len,
					   line, line_len,
					   string_len, 0);
	      else
#endif
		found = strncasecmp (string, line, string_len) == 0;
	    }
	  else
	    found = STREQN (string, line, string_len);

	  if (found)
	    {
	      history_offset = i;
	      return (0);
	    }

	  NEXT_LINE ();
	  continue;
	}

      /* Do substring search. */
      if (linedir < 0)		/* search backwards from end */
	{
	  size_t ll;

	  ll = (patsearch == 0) ? string_len : 1;
	  line_index -= ll;
	  found = 0;

	  while (line_index >= 0)
	    {
#if defined (HAVE_FNMATCH)
	      if (patsearch)
		found = fnmatch (string, line + line_index, 0) == 0;
	      else
#endif
	      if (igncase)
		{
#if defined (HANDLE_MULTIBYTE)
		  if (mb_cur_max > 1)	/* no rl_byte_oriented equivalent */
		    found = _rl_mb_strcaseeqn (string, string_len,
					       line + line_index, ll,
					       string_len, 0);
		  else
#endif
		  found = strncasecmp (string, line + line_index, string_len) == 0;
		}
	      else
	        found = STREQN (string, line + line_index, string_len);

	      if (found)
		{
		  history_offset = i;
		  return (line_index);
		}
	      line_index--;
	      ll++;
	    }
	}
      else
	{
	  register int limit;
	  size_t ll;

	  ll = line_len;
	  limit = line_index - string_len + 1;
	  line_index = 0;
	  found = 0;

	  while (line_index < limit)
	    {
#if defined (HAVE_FNMATCH)
	      if (patsearch)
		found = fnmatch (string, line + line_index, 0) == 0;
	      else
#endif
	      if (igncase)
		{
#if defined (HANDLE_MULTIBYTE)
		  if (mb_cur_max > 1)	/* no rl_byte_oriented equivalent */
		    found = _rl_mb_strcaseeqn (string, string_len,
					       line + line_index, ll,
					       string_len, 0);
		  else
#endif
		  found = strncasecmp (string, line + line_index, string_len) == 0;
		}
	      else
		found = STREQN (string, line + line_index, string_len);

	      if (found)
		{
		  history_offset = i;
		  return (line_index);
		}
	      line_index++;
	      ll--;
	    }
	}
      NEXT_LINE ();
    }
}

int
_hs_history_patsearch (const char *string, int listdir, int linedir, int flags)
{
  char *pat;
  size_t len, start;
  int ret, unescaped_backslash;

#if defined (HAVE_FNMATCH)
  /* Assume that the string passed does not have a leading `^' and any
     anchored search request is captured in FLAGS */
  len = strlen (string);
  ret = len - 1;
  /* fnmatch is required to reject a pattern that ends with an unescaped
     backslash */
  if (unescaped_backslash = (string[ret] == '\\'))
    {
      while (ret > 0 && string[--ret] == '\\')
	unescaped_backslash = 1 - unescaped_backslash;
    }
  if (unescaped_backslash)
    return -1;
  pat = (char *)xmalloc (len + 3);
  /* If the search string is not anchored, we'll be calling fnmatch (assuming
     we have it). Prefix a `*' to the front of the search string so we search
     anywhere in the line. */
  if ((flags & ANCHORED_SEARCH) == 0 && string[0] != '*')
    {
      pat[0] = '*';
      start = 1;
      len++;
    }
  else
    {
      start = 0;
    }

  /* Attempt to reduce the number of searches by tacking a `*' onto the end
     of a pattern that doesn't have one.  Assume a pattern that ends in a
     backslash contains an even number of trailing backslashes; we check
     above */
  strcpy (pat + start, string);
  if (pat[len - 1] != '*')
    {
      pat[len] = '*';		/* XXX */
      pat[len+1] = '\0';
    }
#else
  pat = string;
#endif

  ret = history_search_internal (pat, listdir, linedir, flags|PATTERN_SEARCH);

  if (pat != string)
    xfree (pat);
  return ret;
}
	
/* Do a non-anchored search for STRING through the history list in direction
   LISTDIR. */
int
history_search (const char *string, int listdir)
{
  return (history_search_internal (string, listdir, listdir, NON_ANCHORED_SEARCH));
}

/* Do an anchored search for string through the history list in direction
   LISTDIR. */
int
history_search_prefix (const char *string, int listdir)
{
  return (history_search_internal (string, listdir, listdir, ANCHORED_SEARCH));
}

/* Perform a history search for STRING, letting the caller specify the flags.
   At some point, make this public for users of the history library. */
int
_hs_history_search (const char *string, int listdir, int linedir, int flags)
{
  return (history_search_internal (string, listdir, linedir, flags));
}

/* Search for STRING in the history list.  LISTDIR is < 0 for searching
   backwards through the list.  POS is an absolute index into the history
   list where the search should begin. */
int
history_search_pos (const char *string, int listdir, int pos)
{
  int ret, old;

  old = where_history ();
  history_set_pos (pos);
  if (history_search (string, listdir) == -1)
    {
      history_set_pos (old);
      return (-1);
    }
  ret = where_history ();
  history_set_pos (old);
  return ret;
}