1 /* histexpand.c -- history expansion. */
3 /* Copyright (C) 1989, 1992 Free Software Foundation, Inc.
5 This file contains the GNU History Library (the Library), a set of
6 routines for managing the text of previously typed lines.
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)
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.
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. */
23 #define READLINE_LIBRARY
25 #if defined (HAVE_CONFIG_H)
31 #if defined (HAVE_STDLIB_H)
34 # include "ansi_stdlib.h"
35 #endif /* HAVE_STDLIB_H */
37 #if defined (HAVE_UNISTD_H)
39 # include <sys/types.h>
44 #if defined (HAVE_STRING_H)
48 #endif /* !HAVE_STRING_H */
56 #define HISTORY_WORD_DELIMITERS " \t\n;&()|<>"
57 #define HISTORY_QUOTE_CHARACTERS "\"'`"
59 typedef int _hist_search_func_t PARAMS((const char *, int));
61 static char error_pointer;
63 static char *subst_lhs;
64 static char *subst_rhs;
65 static int subst_lhs_len;
66 static int subst_rhs_len;
68 static char *get_history_word_specifier PARAMS((char *, char *, int *));
69 static char *history_find_word PARAMS((char *, int));
71 static char *quote_breaks PARAMS((char *));
73 /* Variables exported by this file. */
74 /* The character that represents the start of a history expansion
75 request. This is usually `!'. */
76 char history_expansion_char = '!';
78 /* The character that invokes word substitution if found at the start of
79 a line. This is usually `^'. */
80 char history_subst_char = '^';
82 /* During tokenization, if this character is seen as the first character
83 of a word, then it, and all subsequent characters upto a newline are
84 ignored. For a Bourne shell, this should be '#'. Bash special cases
85 the interactive comment character to not be a comment delimiter. */
86 char history_comment_char = '\0';
88 /* The list of characters which inhibit the expansion of text if found
89 immediately following history_expansion_char. */
90 char *history_no_expand_chars = " \t\n\r=";
92 /* If set to a non-zero value, single quotes inhibit history expansion.
94 int history_quotes_inhibit_expansion = 0;
96 /* Used to split words by history_tokenize_internal. */
97 char *history_word_delimiters = HISTORY_WORD_DELIMITERS;
99 /* If set, this points to a function that is called to verify that a
100 particular history expansion should be performed. */
101 rl_linebuf_func_t *history_inhibit_expansion_function;
103 /* **************************************************************** */
105 /* History Expansion */
107 /* **************************************************************** */
109 /* Hairy history expansion on text, not tokens. This is of general
110 use, and thus belongs in this library. */
112 /* The last string searched for by a !?string? search. */
113 static char *search_string;
115 /* The last string matched by a !?string? search. */
116 static char *search_match;
118 /* Return the event specified at TEXT + OFFSET modifying OFFSET to
119 point to after the event specifier. Just a pointer to the history
120 line is returned; NULL is returned in the event of a bad specifier.
121 You pass STRING with *INDEX equal to the history_expansion_char that
122 begins this specification.
123 DELIMITING_QUOTE is a character that is allowed to end the string
124 specification for what to search for in addition to the normal
125 characters `:', ` ', `\t', `\n', and sometimes `?'.
126 So you might call this function like:
127 line = get_history_event ("!echo:p", &index, 0); */
129 get_history_event (string, caller_index, delimiting_quote)
132 int delimiting_quote;
137 int which, sign, local_index, substring_okay;
138 _hist_search_func_t *search_func;
141 /* The event can be specified in a number of ways.
143 !! the previous command
145 !-n current command-line minus N
146 !str the most recent command starting with STR
148 the most recent command containing STR
150 All values N are determined via HISTORY_BASE. */
154 if (string[i] != history_expansion_char)
155 return ((char *)NULL);
157 /* Move on to the specification. */
163 #define RETURN_ENTRY(e, w) \
164 return ((e = history_get (w)) ? e->line : (char *)NULL)
166 /* Handle !! case. */
167 if (string[i] == history_expansion_char)
170 which = history_base + (history_length - 1);
172 RETURN_ENTRY (entry, which);
175 /* Hack case of numeric line specification. */
176 if (string[i] == '-')
182 if (_rl_digit_p (string[i]))
184 /* Get the extent of the digits and compute the value. */
185 for (which = 0; _rl_digit_p (string[i]); i++)
186 which = (which * 10) + _rl_digit_value (string[i]);
191 which = (history_length + history_base) - which;
193 RETURN_ENTRY (entry, which);
196 /* This must be something to search for. If the spec begins with
197 a '?', then the string may be anywhere on the line. Otherwise,
198 the string must be found at the start of a line. */
199 if (string[i] == '?')
205 /* Only a closing `?' or a newline delimit a substring search string. */
206 for (local_index = i; c = string[i]; i++)
207 if ((!substring_okay && (whitespace (c) || c == ':' ||
208 (history_search_delimiter_chars && member (c, history_search_delimiter_chars)) ||
209 string[i] == delimiting_quote)) ||
211 (substring_okay && string[i] == '?'))
214 which = i - local_index;
215 temp = (char *)xmalloc (1 + which);
217 strncpy (temp, string + local_index, which);
220 if (substring_okay && string[i] == '?')
225 #define FAIL_SEARCH() \
227 history_offset = history_length; free (temp) ; return (char *)NULL; \
230 /* If there is no search string, try to use the previous search string,
231 if one exists. If not, fail immediately. */
232 if (*temp == '\0' && substring_okay)
237 temp = savestring (search_string);
243 search_func = substring_okay ? history_search : history_search_prefix;
246 local_index = (*search_func) (temp, -1);
251 if (local_index == 0 || substring_okay)
253 entry = current_history ();
254 history_offset = history_length;
256 /* If this was a substring search, then remember the
257 string that we matched for word substitution. */
260 FREE (search_string);
261 search_string = temp;
264 search_match = history_find_word (entry->line, local_index);
269 return (entry->line);
281 /* Function for extracting single-quoted strings. Used for inhibiting
282 history expansion within single quotes. */
284 /* Extract the contents of STRING as if it is enclosed in single quotes.
285 SINDEX, when passed in, is the offset of the character immediately
286 following the opening single quote; on exit, SINDEX is left pointing
287 to the closing single quote. */
289 hist_string_extract_single_quoted (string, sindex)
295 for (i = *sindex; string[i] && string[i] != '\''; i++)
305 register char *p, *r;
309 for (p = s; p && *p; p++, len++)
313 else if (whitespace (*p) || *p == '\n')
317 r = ret = (char *)xmalloc (len);
319 for (p = s; p && *p; )
329 else if (whitespace (*p) || *p == '\n')
344 hist_error(s, start, current, errtype)
346 int start, current, errtype;
352 ll = current - start;
356 case EVENT_NOT_FOUND:
357 emsg = "event not found";
361 emsg = "bad word specifier";
365 emsg = "substitution failed";
369 emsg = "unrecognized history modifier";
373 emsg = "no previous substitution";
377 emsg = "unknown expansion error";
382 temp = (char *)xmalloc (ll + elen + 3);
383 strncpy (temp, s + start, ll);
386 strcpy (temp + ll + 2, emsg);
390 /* Get a history substitution string from STR starting at *IPTR
391 and return it. The length is returned in LENPTR.
393 A backslash can quote the delimiter. If the string is the
394 empty string, the previous pattern is used. If there is
395 no previous pattern for the lhs, the last history search
398 If IS_RHS is 1, we ignore empty strings and set the pattern
399 to "" anyway. subst_lhs is not changed if the lhs is empty;
400 subst_rhs is allowed to be set to the empty string. */
403 get_subst_pattern (str, iptr, delimiter, is_rhs, lenptr)
405 int *iptr, delimiter, is_rhs, *lenptr;
407 register int si, i, j, k;
408 char *s = (char *) NULL;
412 for (si = i; str[si] && str[si] != delimiter; si++)
413 if (str[si] == '\\' && str[si + 1] == delimiter)
416 if (si > i || is_rhs)
418 s = (char *)xmalloc (si - i + 1);
419 for (j = 0, k = i; k < si; j++, k++)
421 /* Remove a backslash quoting the search string delimiter. */
422 if (str[k] == '\\' && str[k + 1] == delimiter)
440 postproc_subst_rhs ()
445 new = (char *)xmalloc (new_size = subst_rhs_len + subst_lhs_len);
446 for (i = j = 0; i < subst_rhs_len; i++)
448 if (subst_rhs[i] == '&')
450 if (j + subst_lhs_len >= new_size)
451 new = (char *)xrealloc (new, (new_size = new_size * 2 + subst_lhs_len));
452 strcpy (new + j, subst_lhs);
457 /* a single backslash protects the `&' from lhs interpolation */
458 if (subst_rhs[i] == '\\' && subst_rhs[i + 1] == '&')
461 new = (char *)xrealloc (new, new_size *= 2);
462 new[j++] = subst_rhs[i];
471 /* Expand the bulk of a history specifier starting at STRING[START].
472 Returns 0 if everything is OK, -1 if an error occurred, and 1
473 if the `p' modifier was supplied and the caller should just print
474 the returned string. Returns the new index into string in
475 *END_INDEX_PTR, and the expanded specifier in *RET_STRING. */
477 history_expand_internal (string, start, end_index_ptr, ret_string, current_line)
479 int start, *end_index_ptr;
481 char *current_line; /* for !# */
483 int i, n, starting_index;
484 int substitute_globally, want_quotes, print_only;
485 char *event, *temp, *result, *tstr, *t, c, *word_spec;
488 result = (char *)xmalloc (result_len = 128);
492 /* If it is followed by something that starts a word specifier,
493 then !! is implied as the event specifier. */
495 if (member (string[i + 1], ":$*%^"))
500 fake_s[0] = fake_s[1] = history_expansion_char;
502 event = get_history_event (fake_s, &fake_i, 0);
504 else if (string[i + 1] == '#')
507 event = current_line;
511 int quoted_search_delimiter = 0;
513 /* If the character before this `!' is a double or single
514 quote, then this expansion takes place inside of the
515 quoted string. If we have to search for some text ("!foo"),
516 allow the delimiter to end the search string. */
517 if (i && (string[i - 1] == '\'' || string[i - 1] == '"'))
518 quoted_search_delimiter = string[i - 1];
519 event = get_history_event (string, &i, quoted_search_delimiter);
524 *ret_string = hist_error (string, start, i, EVENT_NOT_FOUND);
529 /* If a word specifier is found, then do what that requires. */
531 word_spec = get_history_word_specifier (string, event, &i);
533 /* There is no such thing as a `malformed word specifier'. However,
534 it is possible for a specifier that has no match. In that case,
536 if (word_spec == (char *)&error_pointer)
538 *ret_string = hist_error (string, starting_index, i, BAD_WORD_SPEC);
543 /* If no word specifier, than the thing of interest was the event. */
544 temp = word_spec ? savestring (word_spec) : savestring (event);
547 /* Perhaps there are other modifiers involved. Do what they say. */
548 want_quotes = substitute_globally = print_only = 0;
551 while (string[i] == ':')
557 substitute_globally = 1;
565 *ret_string = hist_error (string, i+1, i+2, BAD_MODIFIER);
578 /* :p means make this the last executed line. So we
579 return an error state after adding this line to the
585 /* :t discards all but the last part of the pathname. */
587 tstr = strrchr (temp, '/');
591 t = savestring (tstr);
597 /* :h discards the last part of a pathname. */
599 tstr = strrchr (temp, '/');
604 /* :r discards the suffix. */
606 tstr = strrchr (temp, '.');
611 /* :e discards everything but the suffix. */
613 tstr = strrchr (temp, '.');
616 t = savestring (tstr);
622 /* :s/this/that substitutes `that' for the first
623 occurrence of `this'. :gs/this/that substitutes `that'
624 for each occurrence of `this'. :& repeats the last
625 substitution. :g& repeats the last substitution
632 int delimiter, failed, si, l_temp;
636 if (i + 2 < (int)strlen (string))
637 delimiter = string[i + 2];
639 break; /* no search delimiter */
643 t = get_subst_pattern (string, &i, delimiter, 0, &subst_lhs_len);
644 /* An empty substitution lhs with no previous substitution
645 uses the last search string as the lhs. */
653 if (search_string && *search_string)
655 subst_lhs = savestring (search_string);
656 subst_lhs_len = strlen (subst_lhs);
660 subst_lhs = (char *) NULL;
666 subst_rhs = get_subst_pattern (string, &i, delimiter, 1, &subst_rhs_len);
668 /* If `&' appears in the rhs, it's supposed to be replaced
670 if (member ('&', subst_rhs))
671 postproc_subst_rhs ();
676 /* If there is no lhs, the substitution can't succeed. */
677 if (subst_lhs_len == 0)
679 *ret_string = hist_error (string, starting_index, i, NO_PREV_SUBST);
685 l_temp = strlen (temp);
686 /* Ignore impossible cases. */
687 if (subst_lhs_len > l_temp)
689 *ret_string = hist_error (string, starting_index, i, SUBST_FAILED);
695 /* Find the first occurrence of THIS in TEMP. */
697 for (failed = 1; (si + subst_lhs_len) <= l_temp; si++)
698 if (STREQN (temp+si, subst_lhs, subst_lhs_len))
700 int len = subst_rhs_len - subst_lhs_len + l_temp;
701 new_event = (char *)xmalloc (1 + len);
702 strncpy (new_event, temp, si);
703 strncpy (new_event + si, subst_rhs, subst_rhs_len);
704 strncpy (new_event + si + subst_rhs_len,
705 temp + si + subst_lhs_len,
706 l_temp - (si + subst_lhs_len));
707 new_event[len] = '\0';
713 if (substitute_globally)
716 l_temp = strlen (temp);
717 substitute_globally++;
724 if (substitute_globally > 1)
726 substitute_globally = 0;
727 continue; /* don't want to increment i */
731 continue; /* don't want to increment i */
733 *ret_string = hist_error (string, starting_index, i, SUBST_FAILED);
741 /* Done with modfiers. */
742 /* Believe it or not, we have to back the pointer up by one. */
749 if (want_quotes == 'q')
750 x = sh_single_quote (temp);
751 else if (want_quotes == 'x')
752 x = quote_breaks (temp);
754 x = savestring (temp);
762 result = (char *)xrealloc (result, n + 2);
763 strcpy (result, temp);
767 *ret_string = result;
771 /* Expand the string STRING, placing the result into OUTPUT, a pointer
772 to a string. Returns:
774 -1) If there was an error in expansion.
775 0) If no expansions took place (or, if the only change in
776 the text was the de-slashifying of the history expansion
778 1) If expansions did take place
779 2) If the `p' modifier was given and the caller should print the result
781 If an error ocurred in expansion, then OUTPUT contains a descriptive
784 #define ADD_STRING(s) \
787 int sl = strlen (s); \
789 if (j >= result_len) \
791 while (j >= result_len) \
793 result = (char *)xrealloc (result, result_len); \
795 strcpy (result + j - sl, s); \
799 #define ADD_CHAR(c) \
802 if (j >= result_len - 1) \
803 result = (char *)xrealloc (result, result_len += 64); \
810 history_expand (hstring, output)
815 int i, r, l, passc, cc, modified, eindex, only_printing;
818 /* The output string, and its length. */
822 /* Used when adding the string. */
828 /* Setting the history expansion character to 0 inhibits all
829 history expansion. */
830 if (history_expansion_char == 0)
832 *output = savestring (hstring);
836 /* Prepare the buffer for printing error messages. */
837 result = (char *)xmalloc (result_len = 256);
840 only_printing = modified = 0;
841 l = strlen (hstring);
843 /* Grovel the string. Only backslash and single quotes can quote the
844 history escape character. We also handle arg specifiers. */
846 /* Before we grovel forever, see if the history_expansion_char appears
847 anywhere within the text. */
849 /* The quick substitution character is a history expansion all right. That
850 is to say, "^this^that^" is equivalent to "!!:s^this^that^", and in fact,
851 that is the substitution that we do. */
852 if (hstring[0] == history_subst_char)
854 string = (char *)xmalloc (l + 5);
856 string[0] = string[1] = history_expansion_char;
859 strcpy (string + 4, hstring);
865 /* If not quick substitution, still maybe have to do expansion. */
867 /* `!' followed by one of the characters in history_no_expand_chars
868 is NOT an expansion. */
869 for (i = 0; string[i]; i++)
872 /* The history_comment_char, if set, appearing that the beginning
873 of a word signifies that the rest of the line should not have
874 history expansion performed on it.
875 Skip the rest of the line and break out of the loop. */
876 if (history_comment_char && string[i] == history_comment_char &&
877 (i == 0 || member (string[i - 1], history_word_delimiters)))
883 else if (string[i] == history_expansion_char)
885 if (!cc || member (cc, history_no_expand_chars))
887 /* If the calling application has set
888 history_inhibit_expansion_function to a function that checks
889 for special cases that should not be history expanded,
890 call the function and skip the expansion if it returns a
892 else if (history_inhibit_expansion_function &&
893 (*history_inhibit_expansion_function) (string, i))
898 /* XXX - at some point, might want to extend this to handle
899 double quotes as well. */
900 else if (history_quotes_inhibit_expansion && string[i] == '\'')
902 /* If this is bash, single quotes inhibit history expansion. */
904 hist_string_extract_single_quoted (string, &i);
906 else if (history_quotes_inhibit_expansion && string[i] == '\\')
908 /* If this is bash, allow backslashes to quote single
909 quotes and the history expansion character. */
910 if (cc == '\'' || cc == history_expansion_char)
915 if (string[i] != history_expansion_char)
918 *output = savestring (string);
923 /* Extract and perform the substitution. */
924 for (passc = i = j = 0; i < l; i++)
926 int tchar = string[i];
935 if (tchar == history_expansion_char)
937 else if (tchar == history_comment_char)
943 ADD_CHAR (string[i]);
953 /* If history_quotes_inhibit_expansion is set, single quotes
954 inhibit history expansion. */
955 if (history_quotes_inhibit_expansion)
960 hist_string_extract_single_quoted (string, &i);
962 slen = i - quote + 2;
963 temp = (char *)xmalloc (slen);
964 strncpy (temp, string + quote, slen);
965 temp[slen - 1] = '\0';
970 ADD_CHAR (string[i]);
974 case -2: /* history_comment_char */
975 if (i == 0 || member (string[i - 1], history_word_delimiters))
977 temp = (char *)xmalloc (l - i + 1);
978 strcpy (temp, string + i);
984 ADD_CHAR (string[i]);
987 case -3: /* history_expansion_char */
990 /* If the history_expansion_char is followed by one of the
991 characters in history_no_expand_chars, then it is not a
992 candidate for expansion of any kind. */
993 if (member (cc, history_no_expand_chars))
995 ADD_CHAR (string[i]);
999 #if defined (NO_BANG_HASH_MODIFIERS)
1000 /* There is something that is listed as a `word specifier' in csh
1001 documentation which means `the expanded text to this point'.
1002 That is not a word specifier, it is an event specifier. If we
1003 don't want to allow modifiers with `!#', just stick the current
1004 output line in again. */
1009 temp = (char *)xmalloc (1 + strlen (result));
1010 strcpy (temp, result);
1019 r = history_expand_internal (string, i, &eindex, &temp, result);
1024 if (string != hstring)
1037 only_printing = r == 1;
1045 if (string != hstring)
1050 add_history (result);
1054 return (modified != 0);
1057 /* Return a consed string which is the word specified in SPEC, and found
1058 in FROM. NULL is returned if there is no spec. The address of
1059 ERROR_POINTER is returned if the word specified cannot be found.
1060 CALLER_INDEX is the offset in SPEC to start looking; it is updated
1061 to point to just after the last character parsed. */
1063 get_history_word_specifier (spec, from, caller_index)
1067 register int i = *caller_index;
1069 int expecting_word_spec = 0;
1072 /* The range of words to return doesn't exist yet. */
1074 result = (char *)NULL;
1076 /* If we found a colon, then this *must* be a word specification. If
1077 it isn't, then it is an error. */
1081 expecting_word_spec++;
1084 /* Handle special cases first. */
1086 /* `%' is the word last searched for. */
1089 *caller_index = i + 1;
1090 return (search_match ? savestring (search_match) : savestring (""));
1093 /* `*' matches all of the arguments, but not the command. */
1096 *caller_index = i + 1;
1097 result = history_arg_extract (1, '$', from);
1098 return (result ? result : savestring (""));
1101 /* `$' is last arg. */
1104 *caller_index = i + 1;
1105 return (history_arg_extract ('$', '$', from));
1108 /* Try to get FIRST and LAST figured out. */
1112 else if (spec[i] == '^')
1114 else if (_rl_digit_p (spec[i]) && expecting_word_spec)
1116 for (first = 0; _rl_digit_p (spec[i]); i++)
1117 first = (first * 10) + _rl_digit_value (spec[i]);
1120 return ((char *)NULL); /* no valid `first' for word specifier */
1122 if (spec[i] == '^' || spec[i] == '*')
1124 last = (spec[i] == '^') ? 1 : '$'; /* x* abbreviates x-$ */
1127 else if (spec[i] != '-')
1133 if (_rl_digit_p (spec[i]))
1135 for (last = 0; _rl_digit_p (spec[i]); i++)
1136 last = (last * 10) + _rl_digit_value (spec[i]);
1138 else if (spec[i] == '$')
1144 else if (!spec[i] || spec[i] == ':')
1145 /* check against `:' because there could be a modifier separator */
1148 /* csh seems to allow anything to terminate the word spec here,
1149 leaving it as an abbreviation. */
1151 last = -1; /* x- abbreviates x-$ omitting word `$' */
1156 if (last >= first || last == '$' || last < 0)
1157 result = history_arg_extract (first, last, from);
1159 return (result ? result : (char *)&error_pointer);
1162 /* Extract the args specified, starting at FIRST, and ending at LAST.
1163 The args are taken from STRING. If either FIRST or LAST is < 0,
1164 then make that arg count from the right (subtract from the number of
1165 tokens, so that FIRST = -1 means the next to last token on the line).
1166 If LAST is `$' the last arg from STRING is used. */
1168 history_arg_extract (first, last, string)
1172 register int i, len;
1177 /* XXX - think about making history_tokenize return a struct array,
1178 each struct in array being a string and a length to avoid the
1179 calls to strlen below. */
1180 if ((list = history_tokenize (string)) == NULL)
1181 return ((char *)NULL);
1183 for (len = 0; list[len]; len++)
1187 last = len + last - 1;
1190 first = len + first - 1;
1200 if (first >= len || last > len || first < 0 || last < 0 || first > last)
1201 result = ((char *)NULL);
1204 for (size = 0, i = first; i < last; i++)
1205 size += strlen (list[i]) + 1;
1206 result = (char *)xmalloc (size + 1);
1209 for (i = first, offset = 0; i < last; i++)
1211 strcpy (result + offset, list[i]);
1212 offset += strlen (list[i]);
1215 result[offset++] = ' ';
1221 for (i = 0; i < len; i++)
1228 #define slashify_in_quotes "\\`\"$"
1230 /* Parse STRING into tokens and return an array of strings. If WIND is
1231 not -1 and INDP is not null, we also want the word surrounding index
1232 WIND. The position in the returned array of strings is returned in
1235 history_tokenize_internal (string, wind, indp)
1240 register int i, start, result_index, size;
1243 /* If we're searching for a string that's not part of a word (e.g., " "),
1244 make sure we set *INDP to a reasonable value. */
1245 if (indp && wind != -1)
1248 /* Get a token, and stuff it into RESULT. The tokens are split
1249 exactly where the shell would split them. */
1250 for (i = result_index = size = 0, result = (char **)NULL; string[i]; )
1254 /* Skip leading whitespace. */
1255 for (; string[i] && whitespace (string[i]); i++)
1257 if (string[i] == 0 || string[i] == history_comment_char)
1262 if (member (string[i], "()\n"))
1268 if (member (string[i], "<>;&|$"))
1270 int peek = string[i + 1];
1272 if (peek == string[i] && peek != '$')
1274 if (peek == '<' && string[i + 2] == '-')
1281 if ((peek == '&' && (string[i] == '>' || string[i] == '<')) ||
1282 ((peek == '>') && (string[i] == '&')) ||
1283 ((peek == '(') && (string[i] == '$')))
1289 if (string[i] != '$')
1296 /* Get word from string + i; */
1298 if (member (string[i], HISTORY_QUOTE_CHARACTERS))
1299 delimiter = string[i++];
1301 for (; string[i]; i++)
1303 if (string[i] == '\\' && string[i + 1] == '\n')
1309 if (string[i] == '\\' && delimiter != '\'' &&
1310 (delimiter != '"' || member (string[i], slashify_in_quotes)))
1316 if (delimiter && string[i] == delimiter)
1322 if (!delimiter && (member (string[i], history_word_delimiters)))
1325 if (!delimiter && member (string[i], HISTORY_QUOTE_CHARACTERS))
1326 delimiter = string[i];
1331 /* If we are looking for the word in which the character at a
1332 particular index falls, remember it. */
1333 if (indp && wind != -1 && wind >= start && wind < i)
1334 *indp = result_index;
1337 if (result_index + 2 >= size)
1338 result = (char **)xrealloc (result, ((size += 10) * sizeof (char *)));
1339 result[result_index] = (char *)xmalloc (1 + len);
1340 strncpy (result[result_index], string + start, len);
1341 result[result_index][len] = '\0';
1342 result[++result_index] = (char *)NULL;
1348 /* Return an array of tokens, much as the shell might. The tokens are
1349 parsed out of STRING. */
1351 history_tokenize (string)
1354 return (history_tokenize_internal (string, -1, (int *)NULL));
1357 /* Find and return the word which contains the character at index IND
1358 in the history line LINE. Used to save the word matched by the
1359 last history !?string? search. */
1361 history_find_word (line, ind)
1368 words = history_tokenize_internal (line, ind, &wind);
1369 if (wind == -1 || words == 0)
1370 return ((char *)NULL);
1372 for (i = 0; i < wind; i++)
1374 for (i = wind + 1; words[i]; i++)