1 /* histexpand.c -- history expansion. */
3 /* Copyright (C) 1989-2018 Free Software Foundation, Inc.
5 This file contains the GNU History Library (History), a set of
6 routines for managing the text of previously typed lines.
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.
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.
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/>.
22 #define READLINE_LIBRARY
24 #if defined (HAVE_CONFIG_H)
30 #if defined (HAVE_STDLIB_H)
33 # include "ansi_stdlib.h"
34 #endif /* HAVE_STDLIB_H */
36 #if defined (HAVE_UNISTD_H)
38 # include <sys/types.h>
52 #define HISTORY_WORD_DELIMITERS " \t\n;&()|<>"
53 #define HISTORY_QUOTE_CHARACTERS "\"'`"
54 #define HISTORY_EVENT_DELIMITERS "^$*%-"
56 #define slashify_in_quotes "\\`\"$"
58 #define fielddelim(c) (whitespace(c) || (c) == '\n')
60 typedef int _hist_search_func_t
PARAMS((const char *, int));
62 static char error_pointer
;
64 static char *subst_lhs
;
65 static char *subst_rhs
;
66 static int subst_lhs_len
;
67 static int subst_rhs_len
;
69 /* Characters that delimit history event specifications and separate event
70 specifications from word designators. Static for now */
71 static char *history_event_delimiter_chars
= HISTORY_EVENT_DELIMITERS
;
73 static char *get_history_word_specifier
PARAMS((char *, char *, int *));
74 static int history_tokenize_word
PARAMS((const char *, int));
75 static char **history_tokenize_internal
PARAMS((const char *, int, int *));
76 static char *history_substring
PARAMS((const char *, int, int));
77 static void freewords
PARAMS((char **, int));
78 static char *history_find_word
PARAMS((char *, int));
80 static char *quote_breaks
PARAMS((char *));
82 /* Variables exported by this file. */
83 /* The character that represents the start of a history expansion
84 request. This is usually `!'. */
85 char history_expansion_char
= '!';
87 /* The character that invokes word substitution if found at the start of
88 a line. This is usually `^'. */
89 char history_subst_char
= '^';
91 /* During tokenization, if this character is seen as the first character
92 of a word, then it, and all subsequent characters up to a newline are
93 ignored. For a Bourne shell, this should be '#'. Bash special cases
94 the interactive comment character to not be a comment delimiter. */
95 char history_comment_char
= '\0';
97 /* The list of characters which inhibit the expansion of text if found
98 immediately following history_expansion_char. */
99 char *history_no_expand_chars
= " \t\n\r=";
101 /* If set to a non-zero value, single quotes inhibit history expansion.
103 int history_quotes_inhibit_expansion
= 0;
105 /* Used to split words by history_tokenize_internal. */
106 char *history_word_delimiters
= HISTORY_WORD_DELIMITERS
;
108 /* If set, this points to a function that is called to verify that a
109 particular history expansion should be performed. */
110 rl_linebuf_func_t
*history_inhibit_expansion_function
;
112 int history_quoting_state
= 0;
114 /* **************************************************************** */
116 /* History Expansion */
118 /* **************************************************************** */
120 /* Hairy history expansion on text, not tokens. This is of general
121 use, and thus belongs in this library. */
123 /* The last string searched for by a !?string? search. */
124 static char *search_string
;
125 /* The last string matched by a !?string? search. */
126 static char *search_match
;
128 /* Return the event specified at TEXT + OFFSET modifying OFFSET to
129 point to after the event specifier. Just a pointer to the history
130 line is returned; NULL is returned in the event of a bad specifier.
131 You pass STRING with *INDEX equal to the history_expansion_char that
132 begins this specification.
133 DELIMITING_QUOTE is a character that is allowed to end the string
134 specification for what to search for in addition to the normal
135 characters `:', ` ', `\t', `\n', and sometimes `?'.
136 So you might call this function like:
137 line = get_history_event ("!echo:p", &index, 0); */
139 get_history_event (const char *string
, int *caller_index
, int delimiting_quote
)
144 int which
, sign
, local_index
, substring_okay
;
145 _hist_search_func_t
*search_func
;
148 /* The event can be specified in a number of ways.
150 !! the previous command
152 !-n current command-line minus N
153 !str the most recent command starting with STR
155 the most recent command containing STR
157 All values N are determined via HISTORY_BASE. */
161 if (string
[i
] != history_expansion_char
)
162 return ((char *)NULL
);
164 /* Move on to the specification. */
170 #define RETURN_ENTRY(e, w) \
171 return ((e = history_get (w)) ? e->line : (char *)NULL)
173 /* Handle !! case. */
174 if (string
[i
] == history_expansion_char
)
177 which
= history_base
+ (history_length
- 1);
179 RETURN_ENTRY (entry
, which
);
182 /* Hack case of numeric line specification. */
183 if (string
[i
] == '-' && _rl_digit_p (string
[i
+1]))
189 if (_rl_digit_p (string
[i
]))
191 /* Get the extent of the digits and compute the value. */
192 for (which
= 0; _rl_digit_p (string
[i
]); i
++)
193 which
= (which
* 10) + _rl_digit_value (string
[i
]);
198 which
= (history_length
+ history_base
) - which
;
200 RETURN_ENTRY (entry
, which
);
203 /* This must be something to search for. If the spec begins with
204 a '?', then the string may be anywhere on the line. Otherwise,
205 the string must be found at the start of a line. */
206 if (string
[i
] == '?')
212 /* Only a closing `?' or a newline delimit a substring search string. */
213 for (local_index
= i
; c
= string
[i
]; i
++)
215 #if defined (HANDLE_MULTIBYTE)
216 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
221 memset (&ps
, 0, sizeof (mbstate_t));
222 /* These produce warnings because we're passing a const string to a
223 function that takes a non-const string. */
224 _rl_adjust_point ((char *)string
, i
, &ps
);
225 if ((v
= _rl_get_char_len ((char *)string
+ i
, &ps
)) > 1)
232 #endif /* HANDLE_MULTIBYTE */
233 if ((!substring_okay
&&
234 (whitespace (c
) || c
== ':' ||
235 (i
> local_index
&& history_event_delimiter_chars
&& c
== '-') ||
236 (c
!= '-' && history_event_delimiter_chars
&& member (c
, history_event_delimiter_chars
)) ||
237 (history_search_delimiter_chars
&& member (c
, history_search_delimiter_chars
)) ||
238 string
[i
] == delimiting_quote
)) ||
240 (substring_okay
&& string
[i
] == '?'))
244 which
= i
- local_index
;
245 temp
= (char *)xmalloc (1 + which
);
247 strncpy (temp
, string
+ local_index
, which
);
250 if (substring_okay
&& string
[i
] == '?')
255 #define FAIL_SEARCH() \
257 history_offset = history_length; xfree (temp) ; return (char *)NULL; \
260 /* If there is no search string, try to use the previous search string,
261 if one exists. If not, fail immediately. */
262 if (*temp
== '\0' && substring_okay
)
267 temp
= savestring (search_string
);
273 search_func
= substring_okay
? history_search
: history_search_prefix
;
276 local_index
= (*search_func
) (temp
, -1);
281 if (local_index
== 0 || substring_okay
)
283 entry
= current_history ();
286 history_offset
= history_length
;
288 /* If this was a substring search, then remember the
289 string that we matched for word substitution. */
292 FREE (search_string
);
293 search_string
= temp
;
296 search_match
= history_find_word (entry
->line
, local_index
);
301 return (entry
->line
);
313 /* Function for extracting single-quoted strings. Used for inhibiting
314 history expansion within single quotes. */
316 /* Extract the contents of STRING as if it is enclosed in single quotes.
317 SINDEX, when passed in, is the offset of the character immediately
318 following the opening single quote; on exit, SINDEX is left pointing
319 to the closing single quote. FLAGS currently used to allow backslash
320 to escape a single quote (e.g., for bash $'...'). */
322 hist_string_extract_single_quoted (char *string
, int *sindex
, int flags
)
326 for (i
= *sindex
; string
[i
] && string
[i
] != '\''; i
++)
328 if ((flags
& 1) && string
[i
] == '\\' && string
[i
+1])
336 quote_breaks (char *s
)
338 register char *p
, *r
;
342 for (p
= s
; p
&& *p
; p
++, len
++)
346 else if (whitespace (*p
) || *p
== '\n')
350 r
= ret
= (char *)xmalloc (len
);
352 for (p
= s
; p
&& *p
; )
362 else if (whitespace (*p
) || *p
== '\n')
377 hist_error(char *s
, int start
, int current
, int errtype
)
383 ll
= current
- start
;
387 case EVENT_NOT_FOUND
:
388 emsg
= "event not found";
392 emsg
= "bad word specifier";
396 emsg
= "substitution failed";
400 emsg
= "unrecognized history modifier";
404 emsg
= "no previous substitution";
408 emsg
= "unknown expansion error";
413 temp
= (char *)xmalloc (ll
+ elen
+ 3);
415 strncpy (temp
, s
+ start
, ll
);
420 strcpy (temp
+ ll
+ 2, emsg
);
424 /* Get a history substitution string from STR starting at *IPTR
425 and return it. The length is returned in LENPTR.
427 A backslash can quote the delimiter. If the string is the
428 empty string, the previous pattern is used. If there is
429 no previous pattern for the lhs, the last history search
432 If IS_RHS is 1, we ignore empty strings and set the pattern
433 to "" anyway. subst_lhs is not changed if the lhs is empty;
434 subst_rhs is allowed to be set to the empty string. */
437 get_subst_pattern (char *str
, int *iptr
, int delimiter
, int is_rhs
, int *lenptr
)
439 register int si
, i
, j
, k
;
441 #if defined (HANDLE_MULTIBYTE)
448 #if defined (HANDLE_MULTIBYTE)
449 memset (&ps
, 0, sizeof (mbstate_t));
450 _rl_adjust_point (str
, i
, &ps
);
453 for (si
= i
; str
[si
] && str
[si
] != delimiter
; si
++)
454 #if defined (HANDLE_MULTIBYTE)
455 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
458 if ((v
= _rl_get_char_len (str
+ si
, &ps
)) > 1)
460 else if (str
[si
] == '\\' && str
[si
+ 1] == delimiter
)
464 #endif /* HANDLE_MULTIBYTE */
465 if (str
[si
] == '\\' && str
[si
+ 1] == delimiter
)
468 if (si
> i
|| is_rhs
)
470 s
= (char *)xmalloc (si
- i
+ 1);
471 for (j
= 0, k
= i
; k
< si
; j
++, k
++)
473 /* Remove a backslash quoting the search string delimiter. */
474 if (str
[k
] == '\\' && str
[k
+ 1] == delimiter
)
492 postproc_subst_rhs (void)
497 new = (char *)xmalloc (new_size
= subst_rhs_len
+ subst_lhs_len
);
498 for (i
= j
= 0; i
< subst_rhs_len
; i
++)
500 if (subst_rhs
[i
] == '&')
502 if (j
+ subst_lhs_len
>= new_size
)
503 new = (char *)xrealloc (new, (new_size
= new_size
* 2 + subst_lhs_len
));
504 strcpy (new + j
, subst_lhs
);
509 /* a single backslash protects the `&' from lhs interpolation */
510 if (subst_rhs
[i
] == '\\' && subst_rhs
[i
+ 1] == '&')
513 new = (char *)xrealloc (new, new_size
*= 2);
514 new[j
++] = subst_rhs
[i
];
523 /* Expand the bulk of a history specifier starting at STRING[START].
524 Returns 0 if everything is OK, -1 if an error occurred, and 1
525 if the `p' modifier was supplied and the caller should just print
526 the returned string. Returns the new index into string in
527 *END_INDEX_PTR, and the expanded specifier in *RET_STRING. */
528 /* need current line for !# */
530 history_expand_internal (char *string
, int start
, int qc
, int *end_index_ptr
, char **ret_string
, char *current_line
)
532 int i
, n
, starting_index
;
533 int substitute_globally
, subst_bywords
, want_quotes
, print_only
;
534 char *event
, *temp
, *result
, *tstr
, *t
, c
, *word_spec
;
536 #if defined (HANDLE_MULTIBYTE)
539 memset (&ps
, 0, sizeof (mbstate_t));
542 result
= (char *)xmalloc (result_len
= 128);
546 /* If it is followed by something that starts a word specifier,
547 then !! is implied as the event specifier. */
549 if (member (string
[i
+ 1], ":$*%^"))
554 fake_s
[0] = fake_s
[1] = history_expansion_char
;
556 event
= get_history_event (fake_s
, &fake_i
, 0);
558 else if (string
[i
+ 1] == '#')
561 event
= current_line
;
564 event
= get_history_event (string
, &i
, qc
);
568 *ret_string
= hist_error (string
, start
, i
, EVENT_NOT_FOUND
);
573 /* If a word specifier is found, then do what that requires. */
575 word_spec
= get_history_word_specifier (string
, event
, &i
);
577 /* There is no such thing as a `malformed word specifier'. However,
578 it is possible for a specifier that has no match. In that case,
580 if (word_spec
== (char *)&error_pointer
)
582 *ret_string
= hist_error (string
, starting_index
, i
, BAD_WORD_SPEC
);
587 /* If no word specifier, than the thing of interest was the event. */
588 temp
= word_spec
? savestring (word_spec
) : savestring (event
);
591 /* Perhaps there are other modifiers involved. Do what they say. */
592 want_quotes
= substitute_globally
= subst_bywords
= print_only
= 0;
595 while (string
[i
] == ':')
599 if (c
== 'g' || c
== 'a')
601 substitute_globally
= 1;
615 *ret_string
= hist_error (string
, i
+1, i
+2, BAD_MODIFIER
);
628 /* :p means make this the last executed line. So we
629 return an error state after adding this line to the
635 /* :t discards all but the last part of the pathname. */
637 tstr
= strrchr (temp
, '/');
641 t
= savestring (tstr
);
647 /* :h discards the last part of a pathname. */
649 tstr
= strrchr (temp
, '/');
654 /* :r discards the suffix. */
656 tstr
= strrchr (temp
, '.');
661 /* :e discards everything but the suffix. */
663 tstr
= strrchr (temp
, '.');
666 t
= savestring (tstr
);
672 /* :s/this/that substitutes `that' for the first
673 occurrence of `this'. :gs/this/that substitutes `that'
674 for each occurrence of `this'. :& repeats the last
675 substitution. :g& repeats the last substitution
682 int delimiter
, failed
, si
, l_temp
, ws
, we
;
686 if (i
+ 2 < (int)strlen (string
))
688 #if defined (HANDLE_MULTIBYTE)
689 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
691 _rl_adjust_point (string
, i
+ 2, &ps
);
692 if (_rl_get_char_len (string
+ i
+ 2, &ps
) > 1)
695 delimiter
= string
[i
+ 2];
698 #endif /* HANDLE_MULTIBYTE */
699 delimiter
= string
[i
+ 2];
702 break; /* no search delimiter */
706 t
= get_subst_pattern (string
, &i
, delimiter
, 0, &subst_lhs_len
);
707 /* An empty substitution lhs with no previous substitution
708 uses the last search string as the lhs. */
716 if (search_string
&& *search_string
)
718 subst_lhs
= savestring (search_string
);
719 subst_lhs_len
= strlen (subst_lhs
);
723 subst_lhs
= (char *) NULL
;
729 subst_rhs
= get_subst_pattern (string
, &i
, delimiter
, 1, &subst_rhs_len
);
731 /* If `&' appears in the rhs, it's supposed to be replaced
733 if (member ('&', subst_rhs
))
734 postproc_subst_rhs ();
739 /* If there is no lhs, the substitution can't succeed. */
740 if (subst_lhs_len
== 0)
742 *ret_string
= hist_error (string
, starting_index
, i
, NO_PREV_SUBST
);
748 l_temp
= strlen (temp
);
749 /* Ignore impossible cases. */
750 if (subst_lhs_len
> l_temp
)
752 *ret_string
= hist_error (string
, starting_index
, i
, SUBST_FAILED
);
758 /* Find the first occurrence of THIS in TEMP. */
759 /* Substitute SUBST_RHS for SUBST_LHS in TEMP. There are three
762 1. substitute_globally == subst_bywords == 0
763 2. substitute_globally == 1 && subst_bywords == 0
764 3. substitute_globally == 0 && subst_bywords == 1
766 In the first case, we substitute for the first occurrence only.
767 In the second case, we substitute for every occurrence.
768 In the third case, we tokenize into words and substitute the
769 first occurrence of each word. */
772 for (failed
= 1; (si
+ subst_lhs_len
) <= l_temp
; si
++)
774 /* First skip whitespace and find word boundaries if
775 we're past the end of the word boundary we found
777 if (subst_bywords
&& si
> we
)
779 for (; temp
[si
] && fielddelim (temp
[si
]); si
++)
782 we
= history_tokenize_word (temp
, si
);
785 if (STREQN (temp
+si
, subst_lhs
, subst_lhs_len
))
787 int len
= subst_rhs_len
- subst_lhs_len
+ l_temp
;
788 new_event
= (char *)xmalloc (1 + len
);
789 strncpy (new_event
, temp
, si
);
790 strncpy (new_event
+ si
, subst_rhs
, subst_rhs_len
);
791 strncpy (new_event
+ si
+ subst_rhs_len
,
792 temp
+ si
+ subst_lhs_len
,
793 l_temp
- (si
+ subst_lhs_len
));
794 new_event
[len
] = '\0';
800 if (substitute_globally
)
802 /* Reported to fix a bug that causes it to skip every
803 other match when matching a single character. Was
804 si += subst_rhs_len previously. */
805 si
+= subst_rhs_len
- 1;
806 l_temp
= strlen (temp
);
807 substitute_globally
++;
810 else if (subst_bywords
)
813 l_temp
= strlen (temp
);
821 if (substitute_globally
> 1)
823 substitute_globally
= 0;
824 continue; /* don't want to increment i */
828 continue; /* don't want to increment i */
830 *ret_string
= hist_error (string
, starting_index
, i
, SUBST_FAILED
);
838 /* Done with modifiers. */
839 /* Believe it or not, we have to back the pointer up by one. */
846 if (want_quotes
== 'q')
847 x
= sh_single_quote (temp
);
848 else if (want_quotes
== 'x')
849 x
= quote_breaks (temp
);
851 x
= savestring (temp
);
859 result
= (char *)xrealloc (result
, n
+ 2);
860 strcpy (result
, temp
);
864 *ret_string
= result
;
868 /* Expand the string STRING, placing the result into OUTPUT, a pointer
869 to a string. Returns:
871 -1) If there was an error in expansion.
872 0) If no expansions took place (or, if the only change in
873 the text was the de-slashifying of the history expansion
875 1) If expansions did take place
876 2) If the `p' modifier was given and the caller should print the result
878 If an error occurred in expansion, then OUTPUT contains a descriptive
881 #define ADD_STRING(s) \
884 int sl = strlen (s); \
886 if (j >= result_len) \
888 while (j >= result_len) \
890 result = (char *)xrealloc (result, result_len); \
892 strcpy (result + j - sl, s); \
896 #define ADD_CHAR(c) \
899 if (j >= result_len - 1) \
900 result = (char *)xrealloc (result, result_len += 64); \
907 history_expand (char *hstring
, char **output
)
910 int i
, r
, l
, passc
, cc
, modified
, eindex
, only_printing
, dquote
, squote
, flag
;
913 /* The output string, and its length. */
917 #if defined (HANDLE_MULTIBYTE)
922 /* Used when adding the string. */
928 /* Setting the history expansion character to 0 inhibits all
929 history expansion. */
930 if (history_expansion_char
== 0)
932 *output
= savestring (hstring
);
936 /* Prepare the buffer for printing error messages. */
937 result
= (char *)xmalloc (result_len
= 256);
940 only_printing
= modified
= 0;
941 l
= strlen (hstring
);
943 /* Grovel the string. Only backslash and single quotes can quote the
944 history escape character. We also handle arg specifiers. */
946 /* Before we grovel forever, see if the history_expansion_char appears
947 anywhere within the text. */
949 /* The quick substitution character is a history expansion all right. That
950 is to say, "^this^that^" is equivalent to "!!:s^this^that^", and in fact,
951 that is the substitution that we do. */
952 if (hstring
[0] == history_subst_char
)
954 string
= (char *)xmalloc (l
+ 5);
956 string
[0] = string
[1] = history_expansion_char
;
959 strcpy (string
+ 4, hstring
);
964 #if defined (HANDLE_MULTIBYTE)
965 memset (&ps
, 0, sizeof (mbstate_t));
969 /* If not quick substitution, still maybe have to do expansion. */
971 /* `!' followed by one of the characters in history_no_expand_chars
972 is NOT an expansion. */
973 dquote
= history_quoting_state
== '"';
974 squote
= history_quoting_state
== '\'';
976 /* If the calling application tells us we are already reading a
977 single-quoted string, consume the rest of the string right now
980 if (squote
&& history_quotes_inhibit_expansion
)
982 hist_string_extract_single_quoted (string
, &i
, 0);
988 for ( ; string
[i
]; i
++)
990 #if defined (HANDLE_MULTIBYTE)
991 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
994 v
= _rl_get_char_len (string
+ i
, &ps
);
1001 #endif /* HANDLE_MULTIBYTE */
1004 /* The history_comment_char, if set, appearing at the beginning
1005 of a word signifies that the rest of the line should not have
1006 history expansion performed on it.
1007 Skip the rest of the line and break out of the loop. */
1008 if (history_comment_char
&& string
[i
] == history_comment_char
&&
1010 (i
== 0 || member (string
[i
- 1], history_word_delimiters
)))
1016 else if (string
[i
] == history_expansion_char
)
1018 if (cc
== 0 || member (cc
, history_no_expand_chars
))
1020 /* DQUOTE won't be set unless history_quotes_inhibit_expansion
1021 is set. The idea here is to treat double-quoted strings the
1022 same as the word outside double quotes; in effect making the
1023 double quote part of history_no_expand_chars when DQUOTE is
1025 else if (dquote
&& cc
== '"')
1027 /* If the calling application has set
1028 history_inhibit_expansion_function to a function that checks
1029 for special cases that should not be history expanded,
1030 call the function and skip the expansion if it returns a
1032 else if (history_inhibit_expansion_function
&&
1033 (*history_inhibit_expansion_function
) (string
, i
))
1038 /* Shell-like quoting: allow backslashes to quote double quotes
1039 inside a double-quoted string. */
1040 else if (dquote
&& string
[i
] == '\\' && cc
== '"')
1042 /* More shell-like quoting: if we're paying attention to single
1043 quotes and letting them quote the history expansion character,
1044 then we need to pay attention to double quotes, because single
1045 quotes are not special inside double-quoted strings. */
1046 else if (history_quotes_inhibit_expansion
&& string
[i
] == '"')
1048 dquote
= 1 - dquote
;
1050 else if (dquote
== 0 && history_quotes_inhibit_expansion
&& string
[i
] == '\'')
1052 /* If this is bash, single quotes inhibit history expansion. */
1053 flag
= (i
> 0 && string
[i
- 1] == '$');
1055 hist_string_extract_single_quoted (string
, &i
, flag
);
1057 else if (history_quotes_inhibit_expansion
&& string
[i
] == '\\')
1059 /* If this is bash, allow backslashes to quote single
1060 quotes and the history expansion character. */
1061 if (cc
== '\'' || cc
== history_expansion_char
)
1067 if (string
[i
] != history_expansion_char
)
1070 *output
= savestring (string
);
1075 /* Extract and perform the substitution. */
1076 dquote
= history_quoting_state
== '"';
1077 squote
= history_quoting_state
== '\'';
1079 /* If the calling application tells us we are already reading a
1080 single-quoted string, consume the rest of the string right now
1083 if (squote
&& history_quotes_inhibit_expansion
)
1087 hist_string_extract_single_quoted (string
, &i
, 0);
1089 for (c
= 0; c
< i
; c
++)
1090 ADD_CHAR (string
[c
]);
1093 ADD_CHAR (string
[i
]);
1098 for (passc
= 0; i
< l
; i
++)
1100 int qc
, tchar
= string
[i
];
1109 #if defined (HANDLE_MULTIBYTE)
1110 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1115 memset (mb
, 0, sizeof (mb
));
1116 for (k
= 0; k
< MB_LEN_MAX
; k
++)
1119 memset (&ps
, 0, sizeof (mbstate_t));
1120 if (_rl_get_char_len (mb
, &ps
) == -2)
1125 if (strlen (mb
) > 1)
1131 #endif /* HANDLE_MULTIBYTE */
1133 if (tchar
== history_expansion_char
)
1135 else if (tchar
== history_comment_char
)
1141 ADD_CHAR (string
[i
]);
1150 dquote
= 1 - dquote
;
1156 /* If history_quotes_inhibit_expansion is set, single quotes
1157 inhibit history expansion, otherwise they are treated like
1164 else if (dquote
== 0 && history_quotes_inhibit_expansion
)
1168 flag
= (i
> 0 && string
[i
- 1] == '$');
1170 hist_string_extract_single_quoted (string
, &i
, flag
);
1172 slen
= i
- quote
+ 2;
1173 temp
= (char *)xmalloc (slen
);
1174 strncpy (temp
, string
+ quote
, slen
);
1175 temp
[slen
- 1] = '\0';
1179 else if (dquote
== 0 && squote
== 0 && history_quotes_inhibit_expansion
== 0)
1182 ADD_CHAR (string
[i
]);
1185 ADD_CHAR (string
[i
]);
1189 case -2: /* history_comment_char */
1190 if ((dquote
== 0 || history_quotes_inhibit_expansion
== 0) &&
1191 (i
== 0 || member (string
[i
- 1], history_word_delimiters
)))
1193 temp
= (char *)xmalloc (l
- i
+ 1);
1194 strcpy (temp
, string
+ i
);
1200 ADD_CHAR (string
[i
]);
1203 case -3: /* history_expansion_char */
1206 /* If the history_expansion_char is followed by one of the
1207 characters in history_no_expand_chars, then it is not a
1208 candidate for expansion of any kind. */
1209 if (cc
== 0 || member (cc
, history_no_expand_chars
) ||
1210 (dquote
&& cc
== '"') ||
1211 (history_inhibit_expansion_function
&& (*history_inhibit_expansion_function
) (string
, i
)))
1213 ADD_CHAR (string
[i
]);
1217 #if defined (NO_BANG_HASH_MODIFIERS)
1218 /* There is something that is listed as a `word specifier' in csh
1219 documentation which means `the expanded text to this point'.
1220 That is not a word specifier, it is an event specifier. If we
1221 don't want to allow modifiers with `!#', just stick the current
1222 output line in again. */
1227 temp
= (char *)xmalloc (1 + strlen (result
));
1228 strcpy (temp
, result
);
1236 qc
= squote
? '\'' : (dquote
? '"' : 0);
1237 r
= history_expand_internal (string
, i
, qc
, &eindex
, &temp
, result
);
1242 if (string
!= hstring
)
1255 only_printing
+= r
== 1;
1263 if (string
!= hstring
)
1269 add_history (result
);
1274 return (modified
!= 0);
1277 /* Return a consed string which is the word specified in SPEC, and found
1278 in FROM. NULL is returned if there is no spec. The address of
1279 ERROR_POINTER is returned if the word specified cannot be found.
1280 CALLER_INDEX is the offset in SPEC to start looking; it is updated
1281 to point to just after the last character parsed. */
1283 get_history_word_specifier (char *spec
, char *from
, int *caller_index
)
1285 register int i
= *caller_index
;
1287 int expecting_word_spec
= 0;
1290 /* The range of words to return doesn't exist yet. */
1292 result
= (char *)NULL
;
1294 /* If we found a colon, then this *must* be a word specification. If
1295 it isn't, then it is an error. */
1299 expecting_word_spec
++;
1302 /* Handle special cases first. */
1304 /* `%' is the word last searched for. */
1307 *caller_index
= i
+ 1;
1308 return (search_match
? savestring (search_match
) : savestring (""));
1311 /* `*' matches all of the arguments, but not the command. */
1314 *caller_index
= i
+ 1;
1315 result
= history_arg_extract (1, '$', from
);
1316 return (result
? result
: savestring (""));
1319 /* `$' is last arg. */
1322 *caller_index
= i
+ 1;
1323 return (history_arg_extract ('$', '$', from
));
1326 /* Try to get FIRST and LAST figured out. */
1330 else if (spec
[i
] == '^')
1335 else if (_rl_digit_p (spec
[i
]) && expecting_word_spec
)
1337 for (first
= 0; _rl_digit_p (spec
[i
]); i
++)
1338 first
= (first
* 10) + _rl_digit_value (spec
[i
]);
1341 return ((char *)NULL
); /* no valid `first' for word specifier */
1343 if (spec
[i
] == '^' || spec
[i
] == '*')
1345 last
= (spec
[i
] == '^') ? 1 : '$'; /* x* abbreviates x-$ */
1348 else if (spec
[i
] != '-')
1354 if (_rl_digit_p (spec
[i
]))
1356 for (last
= 0; _rl_digit_p (spec
[i
]); i
++)
1357 last
= (last
* 10) + _rl_digit_value (spec
[i
]);
1359 else if (spec
[i
] == '$')
1364 else if (spec
[i
] == '^')
1370 else if (!spec
[i
] || spec
[i
] == ':')
1371 /* check against `:' because there could be a modifier separator */
1374 /* csh seems to allow anything to terminate the word spec here,
1375 leaving it as an abbreviation. */
1377 last
= -1; /* x- abbreviates x-$ omitting word `$' */
1382 if (last
>= first
|| last
== '$' || last
< 0)
1383 result
= history_arg_extract (first
, last
, from
);
1385 return (result
? result
: (char *)&error_pointer
);
1388 /* Extract the args specified, starting at FIRST, and ending at LAST.
1389 The args are taken from STRING. If either FIRST or LAST is < 0,
1390 then make that arg count from the right (subtract from the number of
1391 tokens, so that FIRST = -1 means the next to last token on the line).
1392 If LAST is `$' the last arg from STRING is used. */
1394 history_arg_extract (int first
, int last
, const char *string
)
1396 register int i
, len
;
1401 /* XXX - think about making history_tokenize return a struct array,
1402 each struct in array being a string and a length to avoid the
1403 calls to strlen below. */
1404 if ((list
= history_tokenize (string
)) == NULL
)
1405 return ((char *)NULL
);
1407 for (len
= 0; list
[len
]; len
++)
1411 last
= len
+ last
- 1;
1414 first
= len
+ first
- 1;
1424 if (first
>= len
|| last
> len
|| first
< 0 || last
< 0 || first
> last
)
1425 result
= ((char *)NULL
);
1428 for (size
= 0, i
= first
; i
< last
; i
++)
1429 size
+= strlen (list
[i
]) + 1;
1430 result
= (char *)xmalloc (size
+ 1);
1433 for (i
= first
, offset
= 0; i
< last
; i
++)
1435 strcpy (result
+ offset
, list
[i
]);
1436 offset
+= strlen (list
[i
]);
1439 result
[offset
++] = ' ';
1445 for (i
= 0; i
< len
; i
++)
1453 history_tokenize_word (const char *string
, int ind
)
1456 int delimiter
, nestdelim
, delimopen
;
1459 delimiter
= nestdelim
= 0;
1461 if (member (string
[i
], "()\n")) /* XXX - included \n, but why? been here forever */
1467 if (ISDIGIT (string
[i
]))
1470 while (string
[j
] && ISDIGIT (string
[j
]))
1474 if (string
[j
] == '<' || string
[j
] == '>')
1475 i
= j
; /* digit sequence is a file descriptor */
1479 goto get_word
; /* digit sequence is part of a word */
1483 if (member (string
[i
], "<>;&|"))
1485 int peek
= string
[i
+ 1];
1487 if (peek
== string
[i
])
1489 if (peek
== '<' && string
[i
+ 2] == '-')
1491 else if (peek
== '<' && string
[i
+ 2] == '<')
1496 else if (peek
== '&' && (string
[i
] == '>' || string
[i
] == '<'))
1499 while (string
[j
] && ISDIGIT (string
[j
])) /* file descriptor */
1501 if (string
[j
] =='-') /* <&[digits]-, >&[digits]- */
1505 else if ((peek
== '>' && string
[i
] == '&') || (peek
== '|' && string
[i
] == '>'))
1510 /* XXX - process substitution -- separated out for later -- bash-4.2 */
1511 else if (peek
== '(' && (string
[i
] == '>' || string
[i
] == '<')) /*)*/
1525 /* Get word from string + i; */
1527 if (delimiter
== 0 && member (string
[i
], HISTORY_QUOTE_CHARACTERS
))
1528 delimiter
= string
[i
++];
1530 for (; string
[i
]; i
++)
1532 if (string
[i
] == '\\' && string
[i
+ 1] == '\n')
1538 if (string
[i
] == '\\' && delimiter
!= '\'' &&
1539 (delimiter
!= '"' || member (string
[i
], slashify_in_quotes
)))
1545 /* delimiter must be set and set to something other than a quote if
1546 nestdelim is set, so these tests are safe. */
1547 if (nestdelim
&& string
[i
] == delimopen
)
1552 if (nestdelim
&& string
[i
] == delimiter
)
1560 if (delimiter
&& string
[i
] == delimiter
)
1566 /* Command and process substitution; shell extended globbing patterns */
1567 if (nestdelim
== 0 && delimiter
== 0 && member (string
[i
], "<>$!@?+*") && string
[i
+1] == '(') /*)*/
1576 if (delimiter
== 0 && (member (string
[i
], history_word_delimiters
)))
1579 if (delimiter
== 0 && member (string
[i
], HISTORY_QUOTE_CHARACTERS
))
1580 delimiter
= string
[i
];
1587 history_substring (const char *string
, int start
, int end
)
1590 register char *result
;
1593 result
= (char *)xmalloc (len
+ 1);
1594 strncpy (result
, string
+ start
, len
);
1599 /* Parse STRING into tokens and return an array of strings. If WIND is
1600 not -1 and INDP is not null, we also want the word surrounding index
1601 WIND. The position in the returned array of strings is returned in
1604 history_tokenize_internal (const char *string
, int wind
, int *indp
)
1607 register int i
, start
, result_index
, size
;
1609 /* If we're searching for a string that's not part of a word (e.g., " "),
1610 make sure we set *INDP to a reasonable value. */
1611 if (indp
&& wind
!= -1)
1614 /* Get a token, and stuff it into RESULT. The tokens are split
1615 exactly where the shell would split them. */
1616 for (i
= result_index
= size
= 0, result
= (char **)NULL
; string
[i
]; )
1618 /* Skip leading whitespace. */
1619 for (; string
[i
] && fielddelim (string
[i
]); i
++)
1621 if (string
[i
] == 0 || string
[i
] == history_comment_char
)
1626 i
= history_tokenize_word (string
, start
);
1628 /* If we have a non-whitespace delimiter character (which would not be
1629 skipped by the loop above), use it and any adjacent delimiters to
1630 make a separate field. Any adjacent white space will be skipped the
1631 next time through the loop. */
1632 if (i
== start
&& history_word_delimiters
)
1635 while (string
[i
] && member (string
[i
], history_word_delimiters
))
1639 /* If we are looking for the word in which the character at a
1640 particular index falls, remember it. */
1641 if (indp
&& wind
!= -1 && wind
>= start
&& wind
< i
)
1642 *indp
= result_index
;
1644 if (result_index
+ 2 >= size
)
1645 result
= (char **)xrealloc (result
, ((size
+= 10) * sizeof (char *)));
1647 result
[result_index
++] = history_substring (string
, start
, i
);
1648 result
[result_index
] = (char *)NULL
;
1654 /* Return an array of tokens, much as the shell might. The tokens are
1655 parsed out of STRING. */
1657 history_tokenize (const char *string
)
1659 return (history_tokenize_internal (string
, -1, (int *)NULL
));
1662 /* Free members of WORDS from START to an empty string */
1664 freewords (char **words
, int start
)
1668 for (i
= start
; words
[i
]; i
++)
1672 /* Find and return the word which contains the character at index IND
1673 in the history line LINE. Used to save the word matched by the
1674 last history !?string? search. */
1676 history_find_word (char *line
, int ind
)
1681 words
= history_tokenize_internal (line
, ind
, &wind
);
1682 if (wind
== -1 || words
== 0)
1685 freewords (words
, 0);
1687 return ((char *)NULL
);
1690 for (i
= 0; i
< wind
; i
++)
1692 freewords (words
, wind
+ 1);