1 /* histexpand.c -- history expansion. */
3 /* Copyright (C) 1989-2004 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>
52 #define HISTORY_WORD_DELIMITERS " \t\n;&()|<>"
53 #define HISTORY_QUOTE_CHARACTERS "\"'`"
55 #define slashify_in_quotes "\\`\"$"
57 typedef int _hist_search_func_t
PARAMS((const char *, int));
59 extern int rl_byte_oriented
; /* declared in mbutil.c */
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));
70 static int history_tokenize_word
PARAMS((const char *, int));
71 static char *history_substring
PARAMS((const char *, int, int));
73 static char *quote_breaks
PARAMS((char *));
75 /* Variables exported by this file. */
76 /* The character that represents the start of a history expansion
77 request. This is usually `!'. */
78 char history_expansion_char
= '!';
80 /* The character that invokes word substitution if found at the start of
81 a line. This is usually `^'. */
82 char history_subst_char
= '^';
84 /* During tokenization, if this character is seen as the first character
85 of a word, then it, and all subsequent characters upto a newline are
86 ignored. For a Bourne shell, this should be '#'. Bash special cases
87 the interactive comment character to not be a comment delimiter. */
88 char history_comment_char
= '\0';
90 /* The list of characters which inhibit the expansion of text if found
91 immediately following history_expansion_char. */
92 char *history_no_expand_chars
= " \t\n\r=";
94 /* If set to a non-zero value, single quotes inhibit history expansion.
96 int history_quotes_inhibit_expansion
= 0;
98 /* Used to split words by history_tokenize_internal. */
99 char *history_word_delimiters
= HISTORY_WORD_DELIMITERS
;
101 /* If set, this points to a function that is called to verify that a
102 particular history expansion should be performed. */
103 rl_linebuf_func_t
*history_inhibit_expansion_function
;
105 /* **************************************************************** */
107 /* History Expansion */
109 /* **************************************************************** */
111 /* Hairy history expansion on text, not tokens. This is of general
112 use, and thus belongs in this library. */
114 /* The last string searched for by a !?string? search. */
115 static char *search_string
;
117 /* The last string matched by a !?string? search. */
118 static char *search_match
;
120 /* Return the event specified at TEXT + OFFSET modifying OFFSET to
121 point to after the event specifier. Just a pointer to the history
122 line is returned; NULL is returned in the event of a bad specifier.
123 You pass STRING with *INDEX equal to the history_expansion_char that
124 begins this specification.
125 DELIMITING_QUOTE is a character that is allowed to end the string
126 specification for what to search for in addition to the normal
127 characters `:', ` ', `\t', `\n', and sometimes `?'.
128 So you might call this function like:
129 line = get_history_event ("!echo:p", &index, 0); */
131 get_history_event (string
, caller_index
, delimiting_quote
)
134 int delimiting_quote
;
139 int which
, sign
, local_index
, substring_okay
;
140 _hist_search_func_t
*search_func
;
143 /* The event can be specified in a number of ways.
145 !! the previous command
147 !-n current command-line minus N
148 !str the most recent command starting with STR
150 the most recent command containing STR
152 All values N are determined via HISTORY_BASE. */
156 if (string
[i
] != history_expansion_char
)
157 return ((char *)NULL
);
159 /* Move on to the specification. */
165 #define RETURN_ENTRY(e, w) \
166 return ((e = history_get (w)) ? e->line : (char *)NULL)
168 /* Handle !! case. */
169 if (string
[i
] == history_expansion_char
)
172 which
= history_base
+ (history_length
- 1);
174 RETURN_ENTRY (entry
, which
);
177 /* Hack case of numeric line specification. */
178 if (string
[i
] == '-')
184 if (_rl_digit_p (string
[i
]))
186 /* Get the extent of the digits and compute the value. */
187 for (which
= 0; _rl_digit_p (string
[i
]); i
++)
188 which
= (which
* 10) + _rl_digit_value (string
[i
]);
193 which
= (history_length
+ history_base
) - which
;
195 RETURN_ENTRY (entry
, which
);
198 /* This must be something to search for. If the spec begins with
199 a '?', then the string may be anywhere on the line. Otherwise,
200 the string must be found at the start of a line. */
201 if (string
[i
] == '?')
207 /* Only a closing `?' or a newline delimit a substring search string. */
208 for (local_index
= i
; c
= string
[i
]; i
++)
210 #if defined (HANDLE_MULTIBYTE)
211 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
216 memset (&ps
, 0, sizeof (mbstate_t));
217 /* These produce warnings because we're passing a const string to a
218 function that takes a non-const string. */
219 _rl_adjust_point ((char *)string
, i
, &ps
);
220 if ((v
= _rl_get_char_len ((char *)string
+ i
, &ps
)) > 1)
227 #endif /* HANDLE_MULTIBYTE */
228 if ((!substring_okay
&& (whitespace (c
) || c
== ':' ||
229 (history_search_delimiter_chars
&& member (c
, history_search_delimiter_chars
)) ||
230 string
[i
] == delimiting_quote
)) ||
232 (substring_okay
&& string
[i
] == '?'))
236 which
= i
- local_index
;
237 temp
= (char *)xmalloc (1 + which
);
239 strncpy (temp
, string
+ local_index
, which
);
242 if (substring_okay
&& string
[i
] == '?')
247 #define FAIL_SEARCH() \
249 history_offset = history_length; free (temp) ; return (char *)NULL; \
252 /* If there is no search string, try to use the previous search string,
253 if one exists. If not, fail immediately. */
254 if (*temp
== '\0' && substring_okay
)
259 temp
= savestring (search_string
);
265 search_func
= substring_okay
? history_search
: history_search_prefix
;
268 local_index
= (*search_func
) (temp
, -1);
273 if (local_index
== 0 || substring_okay
)
275 entry
= current_history ();
276 history_offset
= history_length
;
278 /* If this was a substring search, then remember the
279 string that we matched for word substitution. */
282 FREE (search_string
);
283 search_string
= temp
;
286 search_match
= history_find_word (entry
->line
, local_index
);
291 return (entry
->line
);
303 /* Function for extracting single-quoted strings. Used for inhibiting
304 history expansion within single quotes. */
306 /* Extract the contents of STRING as if it is enclosed in single quotes.
307 SINDEX, when passed in, is the offset of the character immediately
308 following the opening single quote; on exit, SINDEX is left pointing
309 to the closing single quote. */
311 hist_string_extract_single_quoted (string
, sindex
)
317 for (i
= *sindex
; string
[i
] && string
[i
] != '\''; i
++)
327 register char *p
, *r
;
331 for (p
= s
; p
&& *p
; p
++, len
++)
335 else if (whitespace (*p
) || *p
== '\n')
339 r
= ret
= (char *)xmalloc (len
);
341 for (p
= s
; p
&& *p
; )
351 else if (whitespace (*p
) || *p
== '\n')
366 hist_error(s
, start
, current
, errtype
)
368 int start
, current
, errtype
;
374 ll
= current
- start
;
378 case EVENT_NOT_FOUND
:
379 emsg
= "event not found";
383 emsg
= "bad word specifier";
387 emsg
= "substitution failed";
391 emsg
= "unrecognized history modifier";
395 emsg
= "no previous substitution";
399 emsg
= "unknown expansion error";
404 temp
= (char *)xmalloc (ll
+ elen
+ 3);
405 strncpy (temp
, s
+ start
, ll
);
408 strcpy (temp
+ ll
+ 2, emsg
);
412 /* Get a history substitution string from STR starting at *IPTR
413 and return it. The length is returned in LENPTR.
415 A backslash can quote the delimiter. If the string is the
416 empty string, the previous pattern is used. If there is
417 no previous pattern for the lhs, the last history search
420 If IS_RHS is 1, we ignore empty strings and set the pattern
421 to "" anyway. subst_lhs is not changed if the lhs is empty;
422 subst_rhs is allowed to be set to the empty string. */
425 get_subst_pattern (str
, iptr
, delimiter
, is_rhs
, lenptr
)
427 int *iptr
, delimiter
, is_rhs
, *lenptr
;
429 register int si
, i
, j
, k
;
431 #if defined (HANDLE_MULTIBYTE)
438 #if defined (HANDLE_MULTIBYTE)
439 memset (&ps
, 0, sizeof (mbstate_t));
440 _rl_adjust_point (str
, i
, &ps
);
443 for (si
= i
; str
[si
] && str
[si
] != delimiter
; si
++)
444 #if defined (HANDLE_MULTIBYTE)
445 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
448 if ((v
= _rl_get_char_len (str
+ si
, &ps
)) > 1)
450 else if (str
[si
] == '\\' && str
[si
+ 1] == delimiter
)
454 #endif /* HANDLE_MULTIBYTE */
455 if (str
[si
] == '\\' && str
[si
+ 1] == delimiter
)
458 if (si
> i
|| is_rhs
)
460 s
= (char *)xmalloc (si
- i
+ 1);
461 for (j
= 0, k
= i
; k
< si
; j
++, k
++)
463 /* Remove a backslash quoting the search string delimiter. */
464 if (str
[k
] == '\\' && str
[k
+ 1] == delimiter
)
482 postproc_subst_rhs ()
487 new = (char *)xmalloc (new_size
= subst_rhs_len
+ subst_lhs_len
);
488 for (i
= j
= 0; i
< subst_rhs_len
; i
++)
490 if (subst_rhs
[i
] == '&')
492 if (j
+ subst_lhs_len
>= new_size
)
493 new = (char *)xrealloc (new, (new_size
= new_size
* 2 + subst_lhs_len
));
494 strcpy (new + j
, subst_lhs
);
499 /* a single backslash protects the `&' from lhs interpolation */
500 if (subst_rhs
[i
] == '\\' && subst_rhs
[i
+ 1] == '&')
503 new = (char *)xrealloc (new, new_size
*= 2);
504 new[j
++] = subst_rhs
[i
];
513 /* Expand the bulk of a history specifier starting at STRING[START].
514 Returns 0 if everything is OK, -1 if an error occurred, and 1
515 if the `p' modifier was supplied and the caller should just print
516 the returned string. Returns the new index into string in
517 *END_INDEX_PTR, and the expanded specifier in *RET_STRING. */
519 history_expand_internal (string
, start
, end_index_ptr
, ret_string
, current_line
)
521 int start
, *end_index_ptr
;
523 char *current_line
; /* for !# */
525 int i
, n
, starting_index
;
526 int substitute_globally
, subst_bywords
, want_quotes
, print_only
;
527 char *event
, *temp
, *result
, *tstr
, *t
, c
, *word_spec
;
529 #if defined (HANDLE_MULTIBYTE)
532 memset (&ps
, 0, sizeof (mbstate_t));
535 result
= (char *)xmalloc (result_len
= 128);
539 /* If it is followed by something that starts a word specifier,
540 then !! is implied as the event specifier. */
542 if (member (string
[i
+ 1], ":$*%^"))
547 fake_s
[0] = fake_s
[1] = history_expansion_char
;
549 event
= get_history_event (fake_s
, &fake_i
, 0);
551 else if (string
[i
+ 1] == '#')
554 event
= current_line
;
558 int quoted_search_delimiter
= 0;
560 /* If the character before this `!' is a double or single
561 quote, then this expansion takes place inside of the
562 quoted string. If we have to search for some text ("!foo"),
563 allow the delimiter to end the search string. */
564 #if defined (HANDLE_MULTIBYTE)
565 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
568 l
= _rl_find_prev_mbchar (string
, i
, MB_FIND_ANY
);
570 /* XXX - original patch had i - 1 ??? If i == 0 it would fail. */
571 if (i
&& (c
== '\'' || c
== '"'))
572 quoted_search_delimiter
= c
;
575 #endif /* HANDLE_MULTIBYTE */
576 if (i
&& (string
[i
- 1] == '\'' || string
[i
- 1] == '"'))
577 quoted_search_delimiter
= string
[i
- 1];
579 event
= get_history_event (string
, &i
, quoted_search_delimiter
);
584 *ret_string
= hist_error (string
, start
, i
, EVENT_NOT_FOUND
);
589 /* If a word specifier is found, then do what that requires. */
591 word_spec
= get_history_word_specifier (string
, event
, &i
);
593 /* There is no such thing as a `malformed word specifier'. However,
594 it is possible for a specifier that has no match. In that case,
596 if (word_spec
== (char *)&error_pointer
)
598 *ret_string
= hist_error (string
, starting_index
, i
, BAD_WORD_SPEC
);
603 /* If no word specifier, than the thing of interest was the event. */
604 temp
= word_spec
? savestring (word_spec
) : savestring (event
);
607 /* Perhaps there are other modifiers involved. Do what they say. */
608 want_quotes
= substitute_globally
= subst_bywords
= print_only
= 0;
611 while (string
[i
] == ':')
615 if (c
== 'g' || c
== 'a')
617 substitute_globally
= 1;
631 *ret_string
= hist_error (string
, i
+1, i
+2, BAD_MODIFIER
);
644 /* :p means make this the last executed line. So we
645 return an error state after adding this line to the
651 /* :t discards all but the last part of the pathname. */
653 tstr
= strrchr (temp
, '/');
657 t
= savestring (tstr
);
663 /* :h discards the last part of a pathname. */
665 tstr
= strrchr (temp
, '/');
670 /* :r discards the suffix. */
672 tstr
= strrchr (temp
, '.');
677 /* :e discards everything but the suffix. */
679 tstr
= strrchr (temp
, '.');
682 t
= savestring (tstr
);
688 /* :s/this/that substitutes `that' for the first
689 occurrence of `this'. :gs/this/that substitutes `that'
690 for each occurrence of `this'. :& repeats the last
691 substitution. :g& repeats the last substitution
698 int delimiter
, failed
, si
, l_temp
, ws
, we
;
702 if (i
+ 2 < (int)strlen (string
))
704 #if defined (HANDLE_MULTIBYTE)
705 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
707 _rl_adjust_point (string
, i
+ 2, &ps
);
708 if (_rl_get_char_len (string
+ i
+ 2, &ps
) > 1)
711 delimiter
= string
[i
+ 2];
714 #endif /* HANDLE_MULTIBYTE */
715 delimiter
= string
[i
+ 2];
718 break; /* no search delimiter */
722 t
= get_subst_pattern (string
, &i
, delimiter
, 0, &subst_lhs_len
);
723 /* An empty substitution lhs with no previous substitution
724 uses the last search string as the lhs. */
732 if (search_string
&& *search_string
)
734 subst_lhs
= savestring (search_string
);
735 subst_lhs_len
= strlen (subst_lhs
);
739 subst_lhs
= (char *) NULL
;
745 subst_rhs
= get_subst_pattern (string
, &i
, delimiter
, 1, &subst_rhs_len
);
747 /* If `&' appears in the rhs, it's supposed to be replaced
749 if (member ('&', subst_rhs
))
750 postproc_subst_rhs ();
755 /* If there is no lhs, the substitution can't succeed. */
756 if (subst_lhs_len
== 0)
758 *ret_string
= hist_error (string
, starting_index
, i
, NO_PREV_SUBST
);
764 l_temp
= strlen (temp
);
765 /* Ignore impossible cases. */
766 if (subst_lhs_len
> l_temp
)
768 *ret_string
= hist_error (string
, starting_index
, i
, SUBST_FAILED
);
774 /* Find the first occurrence of THIS in TEMP. */
775 /* Substitute SUBST_RHS for SUBST_LHS in TEMP. There are three
778 1. substitute_globally == subst_bywords == 0
779 2. substitute_globally == 1 && subst_bywords == 0
780 3. substitute_globally == 0 && subst_bywords == 1
782 In the first case, we substitute for the first occurrence only.
783 In the second case, we substitute for every occurrence.
784 In the third case, we tokenize into words and substitute the
785 first occurrence of each word. */
788 for (failed
= 1; (si
+ subst_lhs_len
) <= l_temp
; si
++)
790 /* First skip whitespace and find word boundaries if
791 we're past the end of the word boundary we found
793 if (subst_bywords
&& si
> we
)
795 for (; temp
[si
] && whitespace (temp
[si
]); si
++)
798 we
= history_tokenize_word (temp
, si
);
801 if (STREQN (temp
+si
, subst_lhs
, subst_lhs_len
))
803 int len
= subst_rhs_len
- subst_lhs_len
+ l_temp
;
804 new_event
= (char *)xmalloc (1 + len
);
805 strncpy (new_event
, temp
, si
);
806 strncpy (new_event
+ si
, subst_rhs
, subst_rhs_len
);
807 strncpy (new_event
+ si
+ subst_rhs_len
,
808 temp
+ si
+ subst_lhs_len
,
809 l_temp
- (si
+ subst_lhs_len
));
810 new_event
[len
] = '\0';
816 if (substitute_globally
)
818 /* Reported to fix a bug that causes it to skip every
819 other match when matching a single character. Was
820 si += subst_rhs_len previously. */
821 si
+= subst_rhs_len
- 1;
822 l_temp
= strlen (temp
);
823 substitute_globally
++;
826 else if (subst_bywords
)
829 l_temp
= strlen (temp
);
837 if (substitute_globally
> 1)
839 substitute_globally
= 0;
840 continue; /* don't want to increment i */
844 continue; /* don't want to increment i */
846 *ret_string
= hist_error (string
, starting_index
, i
, SUBST_FAILED
);
854 /* Done with modfiers. */
855 /* Believe it or not, we have to back the pointer up by one. */
862 if (want_quotes
== 'q')
863 x
= sh_single_quote (temp
);
864 else if (want_quotes
== 'x')
865 x
= quote_breaks (temp
);
867 x
= savestring (temp
);
875 result
= (char *)xrealloc (result
, n
+ 2);
876 strcpy (result
, temp
);
880 *ret_string
= result
;
884 /* Expand the string STRING, placing the result into OUTPUT, a pointer
885 to a string. Returns:
887 -1) If there was an error in expansion.
888 0) If no expansions took place (or, if the only change in
889 the text was the de-slashifying of the history expansion
891 1) If expansions did take place
892 2) If the `p' modifier was given and the caller should print the result
894 If an error ocurred in expansion, then OUTPUT contains a descriptive
897 #define ADD_STRING(s) \
900 int sl = strlen (s); \
902 if (j >= result_len) \
904 while (j >= result_len) \
906 result = (char *)xrealloc (result, result_len); \
908 strcpy (result + j - sl, s); \
912 #define ADD_CHAR(c) \
915 if (j >= result_len - 1) \
916 result = (char *)xrealloc (result, result_len += 64); \
923 history_expand (hstring
, output
)
928 int i
, r
, l
, passc
, cc
, modified
, eindex
, only_printing
, dquote
;
931 /* The output string, and its length. */
935 #if defined (HANDLE_MULTIBYTE)
940 /* Used when adding the string. */
946 /* Setting the history expansion character to 0 inhibits all
947 history expansion. */
948 if (history_expansion_char
== 0)
950 *output
= savestring (hstring
);
954 /* Prepare the buffer for printing error messages. */
955 result
= (char *)xmalloc (result_len
= 256);
958 only_printing
= modified
= 0;
959 l
= strlen (hstring
);
961 /* Grovel the string. Only backslash and single quotes can quote the
962 history escape character. We also handle arg specifiers. */
964 /* Before we grovel forever, see if the history_expansion_char appears
965 anywhere within the text. */
967 /* The quick substitution character is a history expansion all right. That
968 is to say, "^this^that^" is equivalent to "!!:s^this^that^", and in fact,
969 that is the substitution that we do. */
970 if (hstring
[0] == history_subst_char
)
972 string
= (char *)xmalloc (l
+ 5);
974 string
[0] = string
[1] = history_expansion_char
;
977 strcpy (string
+ 4, hstring
);
982 #if defined (HANDLE_MULTIBYTE)
983 memset (&ps
, 0, sizeof (mbstate_t));
987 /* If not quick substitution, still maybe have to do expansion. */
989 /* `!' followed by one of the characters in history_no_expand_chars
990 is NOT an expansion. */
991 for (i
= dquote
= 0; string
[i
]; i
++)
993 #if defined (HANDLE_MULTIBYTE)
994 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
997 v
= _rl_get_char_len (string
+ i
, &ps
);
1004 #endif /* HANDLE_MULTIBYTE */
1007 /* The history_comment_char, if set, appearing at the beginning
1008 of a word signifies that the rest of the line should not have
1009 history expansion performed on it.
1010 Skip the rest of the line and break out of the loop. */
1011 if (history_comment_char
&& string
[i
] == history_comment_char
&&
1012 (i
== 0 || member (string
[i
- 1], history_word_delimiters
)))
1018 else if (string
[i
] == history_expansion_char
)
1020 if (!cc
|| member (cc
, history_no_expand_chars
))
1022 /* If the calling application has set
1023 history_inhibit_expansion_function to a function that checks
1024 for special cases that should not be history expanded,
1025 call the function and skip the expansion if it returns a
1027 else if (history_inhibit_expansion_function
&&
1028 (*history_inhibit_expansion_function
) (string
, i
))
1033 /* Shell-like quoting: allow backslashes to quote double quotes
1034 inside a double-quoted string. */
1035 else if (dquote
&& string
[i
] == '\\' && cc
== '"')
1037 /* More shell-like quoting: if we're paying attention to single
1038 quotes and letting them quote the history expansion character,
1039 then we need to pay attention to double quotes, because single
1040 quotes are not special inside double-quoted strings. */
1041 else if (history_quotes_inhibit_expansion
&& string
[i
] == '"')
1043 dquote
= 1 - dquote
;
1045 else if (dquote
== 0 && history_quotes_inhibit_expansion
&& string
[i
] == '\'')
1047 /* If this is bash, single quotes inhibit history expansion. */
1049 hist_string_extract_single_quoted (string
, &i
);
1051 else if (history_quotes_inhibit_expansion
&& string
[i
] == '\\')
1053 /* If this is bash, allow backslashes to quote single
1054 quotes and the history expansion character. */
1055 if (cc
== '\'' || cc
== history_expansion_char
)
1061 if (string
[i
] != history_expansion_char
)
1064 *output
= savestring (string
);
1069 /* Extract and perform the substitution. */
1070 for (passc
= dquote
= i
= j
= 0; i
< l
; i
++)
1072 int tchar
= string
[i
];
1081 #if defined (HANDLE_MULTIBYTE)
1082 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
1087 memset (mb
, 0, sizeof (mb
));
1088 for (k
= 0; k
< MB_LEN_MAX
; k
++)
1091 memset (&ps
, 0, sizeof (mbstate_t));
1092 if (_rl_get_char_len (mb
, &ps
) == -2)
1097 if (strlen (mb
) > 1)
1103 #endif /* HANDLE_MULTIBYTE */
1105 if (tchar
== history_expansion_char
)
1107 else if (tchar
== history_comment_char
)
1113 ADD_CHAR (string
[i
]);
1122 dquote
= 1 - dquote
;
1128 /* If history_quotes_inhibit_expansion is set, single quotes
1129 inhibit history expansion. */
1130 if (dquote
== 0 && history_quotes_inhibit_expansion
)
1135 hist_string_extract_single_quoted (string
, &i
);
1137 slen
= i
- quote
+ 2;
1138 temp
= (char *)xmalloc (slen
);
1139 strncpy (temp
, string
+ quote
, slen
);
1140 temp
[slen
- 1] = '\0';
1145 ADD_CHAR (string
[i
]);
1149 case -2: /* history_comment_char */
1150 if (i
== 0 || member (string
[i
- 1], history_word_delimiters
))
1152 temp
= (char *)xmalloc (l
- i
+ 1);
1153 strcpy (temp
, string
+ i
);
1159 ADD_CHAR (string
[i
]);
1162 case -3: /* history_expansion_char */
1165 /* If the history_expansion_char is followed by one of the
1166 characters in history_no_expand_chars, then it is not a
1167 candidate for expansion of any kind. */
1168 if (member (cc
, history_no_expand_chars
))
1170 ADD_CHAR (string
[i
]);
1174 #if defined (NO_BANG_HASH_MODIFIERS)
1175 /* There is something that is listed as a `word specifier' in csh
1176 documentation which means `the expanded text to this point'.
1177 That is not a word specifier, it is an event specifier. If we
1178 don't want to allow modifiers with `!#', just stick the current
1179 output line in again. */
1184 temp
= (char *)xmalloc (1 + strlen (result
));
1185 strcpy (temp
, result
);
1194 r
= history_expand_internal (string
, i
, &eindex
, &temp
, result
);
1199 if (string
!= hstring
)
1212 only_printing
= r
== 1;
1220 if (string
!= hstring
)
1226 add_history (result
);
1231 return (modified
!= 0);
1234 /* Return a consed string which is the word specified in SPEC, and found
1235 in FROM. NULL is returned if there is no spec. The address of
1236 ERROR_POINTER is returned if the word specified cannot be found.
1237 CALLER_INDEX is the offset in SPEC to start looking; it is updated
1238 to point to just after the last character parsed. */
1240 get_history_word_specifier (spec
, from
, caller_index
)
1244 register int i
= *caller_index
;
1246 int expecting_word_spec
= 0;
1249 /* The range of words to return doesn't exist yet. */
1251 result
= (char *)NULL
;
1253 /* If we found a colon, then this *must* be a word specification. If
1254 it isn't, then it is an error. */
1258 expecting_word_spec
++;
1261 /* Handle special cases first. */
1263 /* `%' is the word last searched for. */
1266 *caller_index
= i
+ 1;
1267 return (search_match
? savestring (search_match
) : savestring (""));
1270 /* `*' matches all of the arguments, but not the command. */
1273 *caller_index
= i
+ 1;
1274 result
= history_arg_extract (1, '$', from
);
1275 return (result
? result
: savestring (""));
1278 /* `$' is last arg. */
1281 *caller_index
= i
+ 1;
1282 return (history_arg_extract ('$', '$', from
));
1285 /* Try to get FIRST and LAST figured out. */
1289 else if (spec
[i
] == '^')
1294 else if (_rl_digit_p (spec
[i
]) && expecting_word_spec
)
1296 for (first
= 0; _rl_digit_p (spec
[i
]); i
++)
1297 first
= (first
* 10) + _rl_digit_value (spec
[i
]);
1300 return ((char *)NULL
); /* no valid `first' for word specifier */
1302 if (spec
[i
] == '^' || spec
[i
] == '*')
1304 last
= (spec
[i
] == '^') ? 1 : '$'; /* x* abbreviates x-$ */
1307 else if (spec
[i
] != '-')
1313 if (_rl_digit_p (spec
[i
]))
1315 for (last
= 0; _rl_digit_p (spec
[i
]); i
++)
1316 last
= (last
* 10) + _rl_digit_value (spec
[i
]);
1318 else if (spec
[i
] == '$')
1324 else if (!spec
[i
] || spec
[i
] == ':')
1325 /* check against `:' because there could be a modifier separator */
1328 /* csh seems to allow anything to terminate the word spec here,
1329 leaving it as an abbreviation. */
1331 last
= -1; /* x- abbreviates x-$ omitting word `$' */
1336 if (last
>= first
|| last
== '$' || last
< 0)
1337 result
= history_arg_extract (first
, last
, from
);
1339 return (result
? result
: (char *)&error_pointer
);
1342 /* Extract the args specified, starting at FIRST, and ending at LAST.
1343 The args are taken from STRING. If either FIRST or LAST is < 0,
1344 then make that arg count from the right (subtract from the number of
1345 tokens, so that FIRST = -1 means the next to last token on the line).
1346 If LAST is `$' the last arg from STRING is used. */
1348 history_arg_extract (first
, last
, string
)
1352 register int i
, len
;
1357 /* XXX - think about making history_tokenize return a struct array,
1358 each struct in array being a string and a length to avoid the
1359 calls to strlen below. */
1360 if ((list
= history_tokenize (string
)) == NULL
)
1361 return ((char *)NULL
);
1363 for (len
= 0; list
[len
]; len
++)
1367 last
= len
+ last
- 1;
1370 first
= len
+ first
- 1;
1380 if (first
>= len
|| last
> len
|| first
< 0 || last
< 0 || first
> last
)
1381 result
= ((char *)NULL
);
1384 for (size
= 0, i
= first
; i
< last
; i
++)
1385 size
+= strlen (list
[i
]) + 1;
1386 result
= (char *)xmalloc (size
+ 1);
1389 for (i
= first
, offset
= 0; i
< last
; i
++)
1391 strcpy (result
+ offset
, list
[i
]);
1392 offset
+= strlen (list
[i
]);
1395 result
[offset
++] = ' ';
1401 for (i
= 0; i
< len
; i
++)
1409 history_tokenize_word (string
, ind
)
1419 if (member (string
[i
], "()\n"))
1425 if (member (string
[i
], "<>;&|$"))
1427 int peek
= string
[i
+ 1];
1429 if (peek
== string
[i
] && peek
!= '$')
1431 if (peek
== '<' && string
[i
+ 2] == '-')
1438 if ((peek
== '&' && (string
[i
] == '>' || string
[i
] == '<')) ||
1439 (peek
== '>' && string
[i
] == '&') ||
1440 (peek
== '(' && (string
[i
] == '>' || string
[i
] == '<')) || /* ) */
1441 (peek
== '(' && string
[i
] == '$')) /* ) */
1448 if (string
[i
] != '$')
1455 /* Get word from string + i; */
1457 if (member (string
[i
], HISTORY_QUOTE_CHARACTERS
))
1458 delimiter
= string
[i
++];
1460 for (; string
[i
]; i
++)
1462 if (string
[i
] == '\\' && string
[i
+ 1] == '\n')
1468 if (string
[i
] == '\\' && delimiter
!= '\'' &&
1469 (delimiter
!= '"' || member (string
[i
], slashify_in_quotes
)))
1475 if (delimiter
&& string
[i
] == delimiter
)
1481 if (!delimiter
&& (member (string
[i
], history_word_delimiters
)))
1484 if (!delimiter
&& member (string
[i
], HISTORY_QUOTE_CHARACTERS
))
1485 delimiter
= string
[i
];
1492 history_substring (string
, start
, end
)
1497 register char *result
;
1500 result
= (char *)xmalloc (len
+ 1);
1501 strncpy (result
, string
+ start
, len
);
1506 /* Parse STRING into tokens and return an array of strings. If WIND is
1507 not -1 and INDP is not null, we also want the word surrounding index
1508 WIND. The position in the returned array of strings is returned in
1511 history_tokenize_internal (string
, wind
, indp
)
1516 register int i
, start
, result_index
, size
;
1518 /* If we're searching for a string that's not part of a word (e.g., " "),
1519 make sure we set *INDP to a reasonable value. */
1520 if (indp
&& wind
!= -1)
1523 /* Get a token, and stuff it into RESULT. The tokens are split
1524 exactly where the shell would split them. */
1525 for (i
= result_index
= size
= 0, result
= (char **)NULL
; string
[i
]; )
1527 /* Skip leading whitespace. */
1528 for (; string
[i
] && whitespace (string
[i
]); i
++)
1530 if (string
[i
] == 0 || string
[i
] == history_comment_char
)
1535 i
= history_tokenize_word (string
, start
);
1537 /* If we have a non-whitespace delimiter character (which would not be
1538 skipped by the loop above), use it and any adjacent delimiters to
1539 make a separate field. Any adjacent white space will be skipped the
1540 next time through the loop. */
1541 if (i
== start
&& history_word_delimiters
)
1544 while (string
[i
] && member (string
[i
], history_word_delimiters
))
1548 /* If we are looking for the word in which the character at a
1549 particular index falls, remember it. */
1550 if (indp
&& wind
!= -1 && wind
>= start
&& wind
< i
)
1551 *indp
= result_index
;
1553 if (result_index
+ 2 >= size
)
1554 result
= (char **)xrealloc (result
, ((size
+= 10) * sizeof (char *)));
1556 result
[result_index
++] = history_substring (string
, start
, i
);
1557 result
[result_index
] = (char *)NULL
;
1563 /* Return an array of tokens, much as the shell might. The tokens are
1564 parsed out of STRING. */
1566 history_tokenize (string
)
1569 return (history_tokenize_internal (string
, -1, (int *)NULL
));
1572 /* Find and return the word which contains the character at index IND
1573 in the history line LINE. Used to save the word matched by the
1574 last history !?string? search. */
1576 history_find_word (line
, ind
)
1583 words
= history_tokenize_internal (line
, ind
, &wind
);
1584 if (wind
== -1 || words
== 0)
1585 return ((char *)NULL
);
1587 for (i
= 0; i
< wind
; i
++)
1589 for (i
= wind
+ 1; words
[i
]; i
++)