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 1, 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 675 Mass Ave, Cambridge, MA 02139, 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)
| 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 1, 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 675 Mass Ave, Cambridge, MA 02139, 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# include <unistd.h> 37#endif 38#if defined (HAVE_STRING_H) 39# include <string.h> 40#else 41# include <strings.h> 42#endif /* !HAVE_STRING_H */ 43 44#include "history.h" 45#include "histlib.h" 46 47/* Variables imported from other history library files. */ 48extern int history_offset; 49 50/* The list of alternate characters that can delimit a history search 51 string. */ 52char *history_search_delimiter_chars = (char *)NULL; 53 54/* Search the history for STRING, starting at history_offset. 55 If DIRECTION < 0, then the search is through previous entries, else 56 through subsequent. If ANCHORED is non-zero, the string must 57 appear at the beginning of a history line, otherwise, the string 58 may appear anywhere in the line. If the string is found, then 59 current_history () is the history entry, and the value of this 60 function is the offset in the line of that history entry that the 61 string was found in. Otherwise, nothing is changed, and a -1 is 62 returned. */ 63 64static int 65history_search_internal (string, direction, anchored) 66 char *string; 67 int direction, anchored; 68{ 69 register int i, reverse; 70 register char *line; 71 register int line_index; 72 int string_len; 73 HIST_ENTRY **the_history; /* local */ 74 75 i = history_offset; 76 reverse = (direction < 0); 77 78 /* Take care of trivial cases first. */ 79 if (string == 0 || *string == '\0') 80 return (-1); 81 82 if (!history_length || ((i == history_length) && !reverse)) 83 return (-1); 84 85 if (reverse && (i == history_length)) 86 i--; 87 88#define NEXT_LINE() do { if (reverse) i--; else i++; } while (0) 89 90 the_history = history_list (); 91 string_len = strlen (string); 92 while (1) 93 { 94 /* Search each line in the history list for STRING. */ 95 96 /* At limit for direction? */ 97 if ((reverse && i < 0) || (!reverse && i == history_length)) 98 return (-1); 99 100 line = the_history[i]->line; 101 line_index = strlen (line); 102 103 /* If STRING is longer than line, no match. */ 104 if (string_len > line_index) 105 { 106 NEXT_LINE (); 107 continue; 108 } 109 110 /* Handle anchored searches first. */ 111 if (anchored == ANCHORED_SEARCH) 112 { 113 if (STREQN (string, line, string_len)) 114 { 115 history_offset = i; 116 return (0); 117 } 118 119 NEXT_LINE (); 120 continue; 121 } 122 123 /* Do substring search. */ 124 if (reverse) 125 { 126 line_index -= string_len; 127 128 while (line_index >= 0) 129 { 130 if (STREQN (string, line + line_index, string_len)) 131 { 132 history_offset = i; 133 return (line_index); 134 } 135 line_index--; 136 } 137 } 138 else 139 { 140 register int limit; 141 142 limit = line_index - string_len + 1; 143 line_index = 0; 144 145 while (line_index < limit) 146 { 147 if (STREQN (string, line + line_index, string_len)) 148 { 149 history_offset = i; 150 return (line_index); 151 } 152 line_index++; 153 } 154 } 155 NEXT_LINE (); 156 } 157} 158 159/* Do a non-anchored search for STRING through the history in DIRECTION. */ 160int 161history_search (string, direction) 162 char *string; 163 int direction; 164{ 165 return (history_search_internal (string, direction, NON_ANCHORED_SEARCH)); 166} 167 168/* Do an anchored search for string through the history in DIRECTION. */ 169int 170history_search_prefix (string, direction) 171 char *string; 172 int direction; 173{ 174 return (history_search_internal (string, direction, ANCHORED_SEARCH)); 175} 176 177/* Search for STRING in the history list. DIR is < 0 for searching 178 backwards. POS is an absolute index into the history list at 179 which point to begin searching. */ 180int 181history_search_pos (string, dir, pos) 182 char *string; 183 int dir, pos; 184{ 185 int ret, old; 186 187 old = where_history (); 188 history_set_pos (pos); 189 if (history_search (string, dir) == -1) 190 { 191 history_set_pos (old); 192 return (-1); 193 } 194 ret = where_history (); 195 history_set_pos (old); 196 return ret; 197}
| 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/* Variables imported from other history library files. */ 51extern int history_offset; 52 53/* The list of alternate characters that can delimit a history search 54 string. */ 55char *history_search_delimiter_chars = (char *)NULL; 56 57/* Search the history for STRING, starting at history_offset. 58 If DIRECTION < 0, then the search is through previous entries, else 59 through subsequent. If ANCHORED is non-zero, the string must 60 appear at the beginning of a history line, otherwise, the string 61 may appear anywhere in the line. If the string is found, then 62 current_history () is the history entry, and the value of this 63 function is the offset in the line of that history entry that the 64 string was found in. Otherwise, nothing is changed, and a -1 is 65 returned. */ 66 67static int 68history_search_internal (string, direction, anchored) 69 char *string; 70 int direction, anchored; 71{ 72 register int i, reverse; 73 register char *line; 74 register int line_index; 75 int string_len; 76 HIST_ENTRY **the_history; /* local */ 77 78 i = history_offset; 79 reverse = (direction < 0); 80 81 /* Take care of trivial cases first. */ 82 if (string == 0 || *string == '\0') 83 return (-1); 84 85 if (!history_length || ((i == history_length) && !reverse)) 86 return (-1); 87 88 if (reverse && (i == history_length)) 89 i--; 90 91#define NEXT_LINE() do { if (reverse) i--; else i++; } while (0) 92 93 the_history = history_list (); 94 string_len = strlen (string); 95 while (1) 96 { 97 /* Search each line in the history list for STRING. */ 98 99 /* At limit for direction? */ 100 if ((reverse && i < 0) || (!reverse && i == history_length)) 101 return (-1); 102 103 line = the_history[i]->line; 104 line_index = strlen (line); 105 106 /* If STRING is longer than line, no match. */ 107 if (string_len > line_index) 108 { 109 NEXT_LINE (); 110 continue; 111 } 112 113 /* Handle anchored searches first. */ 114 if (anchored == ANCHORED_SEARCH) 115 { 116 if (STREQN (string, line, string_len)) 117 { 118 history_offset = i; 119 return (0); 120 } 121 122 NEXT_LINE (); 123 continue; 124 } 125 126 /* Do substring search. */ 127 if (reverse) 128 { 129 line_index -= string_len; 130 131 while (line_index >= 0) 132 { 133 if (STREQN (string, line + line_index, string_len)) 134 { 135 history_offset = i; 136 return (line_index); 137 } 138 line_index--; 139 } 140 } 141 else 142 { 143 register int limit; 144 145 limit = line_index - string_len + 1; 146 line_index = 0; 147 148 while (line_index < limit) 149 { 150 if (STREQN (string, line + line_index, string_len)) 151 { 152 history_offset = i; 153 return (line_index); 154 } 155 line_index++; 156 } 157 } 158 NEXT_LINE (); 159 } 160} 161 162/* Do a non-anchored search for STRING through the history in DIRECTION. */ 163int 164history_search (string, direction) 165 char *string; 166 int direction; 167{ 168 return (history_search_internal (string, direction, NON_ANCHORED_SEARCH)); 169} 170 171/* Do an anchored search for string through the history in DIRECTION. */ 172int 173history_search_prefix (string, direction) 174 char *string; 175 int direction; 176{ 177 return (history_search_internal (string, direction, ANCHORED_SEARCH)); 178} 179 180/* Search for STRING in the history list. DIR is < 0 for searching 181 backwards. POS is an absolute index into the history list at 182 which point to begin searching. */ 183int 184history_search_pos (string, dir, pos) 185 char *string; 186 int dir, pos; 187{ 188 int ret, old; 189 190 old = where_history (); 191 history_set_pos (pos); 192 if (history_search (string, dir) == -1) 193 { 194 history_set_pos (old); 195 return (-1); 196 } 197 ret = where_history (); 198 history_set_pos (old); 199 return ret; 200}
|