OpenCoverage

histsearch.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/bash/src/lib/readline/histsearch.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* histsearch.c -- searching the history list. */-
2-
3/* Copyright (C) 1989, 1992-2009,2017 Free Software Foundation, Inc.-
4-
5 This file contains the GNU History Library (History), a set of-
6 routines for managing the text of previously typed lines.-
7-
8 History 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 3 of the License, or-
11 (at your option) any later version.-
12-
13 History is distributed in the hope that it will be useful,-
14 but WITHOUT ANY WARRANTY; without even the implied warranty of-
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the-
16 GNU General Public License for more details.-
17-
18 You should have received a copy of the GNU General Public License-
19 along with History. If not, see <http://www.gnu.org/licenses/>.-
20*/-
21-
22#define READLINE_LIBRARY-
23-
24#if defined (HAVE_CONFIG_H)-
25# include <config.h>-
26#endif-
27-
28#include <stdio.h>-
29#if defined (HAVE_STDLIB_H)-
30# include <stdlib.h>-
31#else-
32# include "ansi_stdlib.h"-
33#endif /* HAVE_STDLIB_H */-
34-
35#if defined (HAVE_UNISTD_H)-
36# ifdef _MINIX-
37# include <sys/types.h>-
38# endif-
39# include <unistd.h>-
40#endif-
41-
42#if defined (HAVE_FNMATCH)-
43# include <fnmatch.h>-
44#endif-
45-
46#include "history.h"-
47#include "histlib.h"-
48#include "xmalloc.h"-
49-
50/* The list of alternate characters that can delimit a history search-
51 string. */-
52char *history_search_delimiter_chars = (char *)NULL;-
53-
54static 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-
66static int-
67history_search_internal (const char *string, int direction, int flags)-
68{-
69 register int i, reverse;-
70 register char *line;-
71 register int line_index;-
72 int string_len, anchored, patsearch;-
73 HIST_ENTRY **the_history; /* local */-
74-
75 i = history_offset;-
76 reverse = (direction < 0);-
77 anchored = (flags & ANCHORED_SEARCH);-
78#if defined (HAVE_FNMATCH)-
79 patsearch = (flags & PATTERN_SEARCH);-
80#else-
81 patsearch = 0;-
82#endif-
83-
84 /* Take care of trivial cases first. */-
85 if (string == 0 || *string == '\0')
string == 0Description
TRUEnever evaluated
FALSEevaluated 12 times by 1 test
Evaluated by:
  • Self test
*string == '\0'Description
TRUEnever evaluated
FALSEevaluated 12 times by 1 test
Evaluated by:
  • Self test
0-12
86 return (-1);
never executed: return (-1);
0
87-
88 if (!history_length || ((i >= history_length) && !reverse))
!history_lengthDescription
TRUEnever evaluated
FALSEevaluated 12 times by 1 test
Evaluated by:
  • Self test
(i >= history_length)Description
TRUEevaluated 12 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
!reverseDescription
TRUEnever evaluated
FALSEevaluated 12 times by 1 test
Evaluated by:
  • Self test
0-12
89 return (-1);
never executed: return (-1);
0
90-
91 if (reverse && (i >= history_length))
reverseDescription
TRUEevaluated 12 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
(i >= history_length)Description
TRUEevaluated 12 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-12
92 i = history_length - 1;
executed 12 times by 1 test: i = history_length - 1;
Executed by:
  • Self test
12
93-
94#define NEXT_LINE() do { if (reverse) i--; else i++; } while (0)-
95-
96 the_history = history_list ();-
97 string_len = strlen (string);-
98 while (1)-
99 {-
100 /* Search each line in the history list for STRING. */-
101-
102 /* At limit for direction? */-
103 if ((reverse && i < 0) || (!reverse && i == history_length))
reverseDescription
TRUEevaluated 39 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
i < 0Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • Self test
FALSEevaluated 38 times by 1 test
Evaluated by:
  • Self test
!reverseDescription
TRUEnever evaluated
FALSEevaluated 38 times by 1 test
Evaluated by:
  • Self test
i == history_lengthDescription
TRUEnever evaluated
FALSEnever evaluated
0-39
104 return (-1);
executed 1 time by 1 test: return (-1);
Executed by:
  • Self test
1
105-
106 line = the_history[i]->line;-
107 line_index = strlen (line);-
108-
109 /* If STRING is longer than line, no match. */-
110 if (patsearch == 0 && (string_len > line_index))
patsearch == 0Description
TRUEevaluated 38 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
(string_len > line_index)Description
TRUEnever evaluated
FALSEevaluated 38 times by 1 test
Evaluated by:
  • Self test
0-38
111 {-
112 NEXT_LINE ();
never executed: i--;
never executed: i++;
reverseDescription
TRUEnever evaluated
FALSEnever evaluated
0
113 continue;
never executed: continue;
0
114 }-
115-
116 /* Handle anchored searches first. */-
117 if (anchored == ANCHORED_SEARCH)
anchored == 0x01Description
TRUEevaluated 24 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 14 times by 1 test
Evaluated by:
  • Self test
14-24
118 {-
119#if defined (HAVE_FNMATCH)-
120 if (patsearch)
patsearchDescription
TRUEnever evaluated
FALSEevaluated 24 times by 1 test
Evaluated by:
  • Self test
0-24
121 {-
122 if (fnmatch (string, line, 0) == 0)
fnmatch (string, line, 0) == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
123 {-
124 history_offset = i;-
125 return (0);
never executed: return (0);
0
126 }-
127 }
never executed: end of block
0
128 else-
129#endif-
130 if (STREQN (string, line, string_len))
never executed: __result = (((const unsigned char *) (const char *) ( (string) ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
never executed: __result = (((const unsigned char *) (const char *) ( (line) ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
(((string_len)...en) ))) == 0))Description
TRUEevaluated 6 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 18 times by 1 test
Evaluated by:
  • Self test
((string_len) == 0)Description
TRUEnever evaluated
FALSEevaluated 24 times by 1 test
Evaluated by:
  • Self test
((string)[0] == (line)[0])Description
TRUEevaluated 6 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 18 times by 1 test
Evaluated by:
  • Self test
( (__extension...len) ))) == 0)Description
TRUEevaluated 6 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
__builtin_cons...(string_len) )Description
TRUEnever evaluated
FALSEevaluated 6 times by 1 test
Evaluated by:
  • Self test
__builtin_cons...p ( (string) )Description
TRUEnever evaluated
FALSEnever evaluated
strlen ( (stri...string_len) ))Description
TRUEnever evaluated
FALSEnever evaluated
__builtin_cons...t_p ( (line) )Description
TRUEnever evaluated
FALSEnever evaluated
strlen ( (line...string_len) ))Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
0-24
131 {-
132 history_offset = i;-
133 return (0);
executed 6 times by 1 test: return (0);
Executed by:
  • Self test
6
134 }-
135-
136 NEXT_LINE ();
executed 18 times by 1 test: i--;
Executed by:
  • Self test
never executed: i++;
reverseDescription
TRUEevaluated 18 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-18
137 continue;
executed 18 times by 1 test: continue;
Executed by:
  • Self test
18
138 }-
139-
140 /* Do substring search. */-
141 if (reverse)
reverseDescription
TRUEevaluated 14 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-14
142 {-
143 line_index -= (patsearch == 0) ? string_len : 1;
(patsearch == 0)Description
TRUEevaluated 14 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-14
144-
145 while (line_index >= 0)
line_index >= 0Description
TRUEevaluated 176 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 9 times by 1 test
Evaluated by:
  • Self test
9-176
146 {-
147#if defined (HAVE_FNMATCH)-
148 if (patsearch)
patsearchDescription
TRUEnever evaluated
FALSEevaluated 176 times by 1 test
Evaluated by:
  • Self test
0-176
149 {-
150 if (fnmatch (string, line + line_index, 0) == 0)
fnmatch (strin...index, 0) == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
151 {-
152 history_offset = i;-
153 return (line_index);
never executed: return (line_index);
0
154 }-
155 }
never executed: end of block
0
156 else-
157#endif-
158 if (STREQN (string, line + line_index, string_len))
never executed: __result = (((const unsigned char *) (const char *) ( (string) ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
never executed: __result = (((const unsigned char *) (const char *) ( (line + line_index) ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
(((string_len)...en) ))) == 0))Description
TRUEevaluated 5 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 171 times by 1 test
Evaluated by:
  • Self test
((string_len) == 0)Description
TRUEnever evaluated
FALSEevaluated 176 times by 1 test
Evaluated by:
  • Self test
((string)[0] =...ine_index)[0])Description
TRUEevaluated 8 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 168 times by 1 test
Evaluated by:
  • Self test
( (__extension...len) ))) == 0)Description
TRUEevaluated 5 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 3 times by 1 test
Evaluated by:
  • Self test
__builtin_cons...(string_len) )Description
TRUEnever evaluated
FALSEevaluated 8 times by 1 test
Evaluated by:
  • Self test
__builtin_cons...p ( (string) )Description
TRUEnever evaluated
FALSEnever evaluated
strlen ( (stri...string_len) ))Description
TRUEnever evaluated
FALSEnever evaluated
__builtin_cons... line_index) )Description
TRUEnever evaluated
FALSEnever evaluated
strlen ( (line...string_len) ))Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
0-176
159 {-
160 history_offset = i;-
161 return (line_index);
executed 5 times by 1 test: return (line_index);
Executed by:
  • Self test
5
162 }-
163 line_index--;-
164 }
executed 171 times by 1 test: end of block
Executed by:
  • Self test
171
165 }
executed 9 times by 1 test: end of block
Executed by:
  • Self test
9
166 else-
167 {-
168 register int limit;-
169-
170 limit = line_index - string_len + 1;-
171 line_index = 0;-
172-
173 while (line_index < limit)
line_index < limitDescription
TRUEnever evaluated
FALSEnever evaluated
0
174 {-
175#if defined (HAVE_FNMATCH)-
176 if (patsearch)
patsearchDescription
TRUEnever evaluated
FALSEnever evaluated
0
177 {-
178 if (fnmatch (string, line + line_index, 0) == 0)
fnmatch (strin...index, 0) == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
179 {-
180 history_offset = i;-
181 return (line_index);
never executed: return (line_index);
0
182 }-
183 }
never executed: end of block
0
184 else-
185#endif-
186 if (STREQN (string, line + line_index, string_len))
never executed: __result = (((const unsigned char *) (const char *) ( (string) ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
never executed: __result = (((const unsigned char *) (const char *) ( (line + line_index) ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
(((string_len)...en) ))) == 0))Description
TRUEnever evaluated
FALSEnever evaluated
((string_len) == 0)Description
TRUEnever evaluated
FALSEnever evaluated
((string)[0] =...ine_index)[0])Description
TRUEnever evaluated
FALSEnever evaluated
( (__extension...len) ))) == 0)Description
TRUEnever evaluated
FALSEnever evaluated
__builtin_cons...(string_len) )Description
TRUEnever evaluated
FALSEnever evaluated
__builtin_cons...p ( (string) )Description
TRUEnever evaluated
FALSEnever evaluated
strlen ( (stri...string_len) ))Description
TRUEnever evaluated
FALSEnever evaluated
__builtin_cons... line_index) )Description
TRUEnever evaluated
FALSEnever evaluated
strlen ( (line...string_len) ))Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
187 {-
188 history_offset = i;-
189 return (line_index);
never executed: return (line_index);
0
190 }-
191 line_index++;-
192 }
never executed: end of block
0
193 }
never executed: end of block
0
194 NEXT_LINE ();
executed 9 times by 1 test: i--;
Executed by:
  • Self test
never executed: i++;
reverseDescription
TRUEevaluated 9 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-9
195 }
executed 9 times by 1 test: end of block
Executed by:
  • Self test
9
196}
never executed: end of block
0
197-
198int-
199_hs_history_patsearch (const char *string, int direction, int flags)-
200{-
201 char *pat;-
202 size_t len;-
203 int ret, unescaped_backslash;-
204-
205#if defined (HAVE_FNMATCH)-
206 /* Assume that the string passed does not have a leading `^' and any-
207 anchored search request is captured in FLAGS */-
208 len = strlen (string);-
209 ret = len - 1;-
210 /* fnmatch is required to reject a pattern that ends with an unescaped-
211 backslash */-
212 if (unescaped_backslash = (string[ret] == '\\'))
unescaped_back...[ret] == '\\')Description
TRUEnever evaluated
FALSEnever evaluated
0
213 {-
214 while (ret > 0 && string[--ret] == '\\')
ret > 0Description
TRUEnever evaluated
FALSEnever evaluated
string[--ret] == '\\'Description
TRUEnever evaluated
FALSEnever evaluated
0
215 unescaped_backslash = 1 - unescaped_backslash;
never executed: unescaped_backslash = 1 - unescaped_backslash;
0
216 }
never executed: end of block
0
217 if (unescaped_backslash)
unescaped_backslashDescription
TRUEnever evaluated
FALSEnever evaluated
0
218 return -1;
never executed: return -1;
0
219 pat = (char *)xmalloc (len + 2);-
220 /* Attempt to reduce the number of searches by tacking a `*' onto the end-
221 of a pattern that doesn't have one. Assume a pattern that ends in a-
222 backslash contains an even number of trailing backslashes; we check-
223 above */-
224 strcpy (pat, string);-
225 if (pat[len - 1] != '*')
pat[len - 1] != '*'Description
TRUEnever evaluated
FALSEnever evaluated
0
226 {-
227 pat[len] = '*'; /* XXX */-
228 pat[len+1] = '\0';-
229 }
never executed: end of block
0
230#else-
231 pat = string;-
232#endif-
233-
234 ret = history_search_internal (pat, direction, flags|PATTERN_SEARCH);-
235-
236 if (pat != string)
pat != stringDescription
TRUEnever evaluated
FALSEnever evaluated
0
237 free (pat);
never executed: free (pat);
0
238 return ret;
never executed: return ret;
0
239}-
240 -
241/* Do a non-anchored search for STRING through the history in DIRECTION. */-
242int-
243history_search (const char *string, int direction)-
244{-
245 return (history_search_internal (string, direction, NON_ANCHORED_SEARCH));
executed 5 times by 1 test: return (history_search_internal (string, direction, 0));
Executed by:
  • Self test
5
246}-
247-
248/* Do an anchored search for string through the history in DIRECTION. */-
249int-
250history_search_prefix (const char *string, int direction)-
251{-
252 return (history_search_internal (string, direction, ANCHORED_SEARCH));
executed 7 times by 1 test: return (history_search_internal (string, direction, 0x01));
Executed by:
  • Self test
7
253}-
254-
255/* Search for STRING in the history list. DIR is < 0 for searching-
256 backwards. POS is an absolute index into the history list at-
257 which point to begin searching. */-
258int-
259history_search_pos (const char *string, int dir, int pos)-
260{-
261 int ret, old;-
262-
263 old = where_history ();-
264 history_set_pos (pos);-
265 if (history_search (string, dir) == -1)
history_search...ng, dir) == -1Description
TRUEnever evaluated
FALSEnever evaluated
0
266 {-
267 history_set_pos (old);-
268 return (-1);
never executed: return (-1);
0
269 }-
270 ret = where_history ();-
271 history_set_pos (old);-
272 return ret;
never executed: return ret;
0
273}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2