| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1 | /* bashhist.c -- bash interface to the GNU history library. */ |
| 2 | |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 3 | /* Copyright (C) 1993-2015 Free Software Foundation, Inc. |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4 | |
| 5 | This file is part of GNU Bash, the Bourne Again SHell. |
| 6 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 7 | Bash is free software: you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation, either version 3 of the License, or |
| 10 | (at your option) any later version. |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 11 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 12 | Bash is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 16 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 17 | You should have received a copy of the GNU General Public License |
| 18 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 20 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 21 | #include "config.h" |
| 22 | |
| 23 | #if defined (HISTORY) |
| 24 | |
| 25 | #if defined (HAVE_UNISTD_H) |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 26 | # ifdef _MINIX |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 27 | # include <sys/types.h> |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 28 | # endif |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 29 | # include <unistd.h> |
| 30 | #endif |
| 31 | |
| 32 | #include "bashtypes.h" |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 33 | #include <stdio.h> |
| 34 | #include <errno.h> |
| 35 | #include "bashansi.h" |
| 36 | #include "posixstat.h" |
| 37 | #include "filecntl.h" |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 38 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 39 | #include "bashintl.h" |
| 40 | |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 41 | #if defined (SYSLOG_HISTORY) |
| 42 | # include <syslog.h> |
| 43 | #endif |
| 44 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 45 | #include "shell.h" |
| 46 | #include "flags.h" |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame^] | 47 | #include "parser.h" |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 48 | #include "input.h" |
| 49 | #include "parser.h" /* for the struct dstack stuff. */ |
| 50 | #include "pathexp.h" /* for the struct ignorevar stuff */ |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 51 | #include "bashhist.h" /* matching prototypes and declarations */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 52 | #include "builtins/common.h" |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 53 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 54 | #include <readline/history.h> |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 55 | #include <glob/glob.h> |
| 56 | #include <glob/strmatch.h> |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 57 | |
| 58 | #if defined (READLINE) |
| 59 | # include "bashline.h" |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 60 | extern int rl_done, rl_dispatching; /* should really include readline.h */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 61 | #endif |
| 62 | |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame^] | 63 | #ifndef HISTSIZE_DEFAULT |
| 64 | # define HISTSIZE_DEFAULT "500" |
| 65 | #endif |
| 66 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 67 | #if !defined (errno) |
| 68 | extern int errno; |
| 69 | #endif |
| 70 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 71 | static int histignore_item_func __P((struct ign *)); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 72 | static int check_history_control __P((char *)); |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 73 | static void hc_erasedups __P((char *)); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 74 | static void really_add_history __P((char *)); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 75 | |
| 76 | static struct ignorevar histignore = |
| 77 | { |
| 78 | "HISTIGNORE", |
| 79 | (struct ign *)0, |
| 80 | 0, |
| 81 | (char *)0, |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 82 | (sh_iv_item_func_t *)histignore_item_func, |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | #define HIGN_EXPAND 0x01 |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 86 | |
| 87 | /* Declarations of bash history variables. */ |
| 88 | /* Non-zero means to remember lines typed to the shell on the history |
| 89 | list. This is different than the user-controlled behaviour; this |
| 90 | becomes zero when we read lines from a file, for example. */ |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 91 | int remember_on_history = 0; |
| 92 | int enable_history_list = 0; /* value for `set -o history' */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 93 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 94 | /* The number of lines that Bash has added to this history session. The |
| 95 | difference between the number of the top element in the history list |
| 96 | (offset from history_base) and the number of lines in the history file. |
| 97 | Appending this session's history to the history file resets this to 0. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 98 | int history_lines_this_session; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 99 | |
| 100 | /* The number of lines that Bash has read from the history file. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 101 | int history_lines_in_file; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 102 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 103 | #if defined (BANG_HISTORY) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 104 | /* Non-zero means do no history expansion on this line, regardless |
| 105 | of what history_expansion says. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 106 | int history_expansion_inhibited; |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 107 | /* If non-zero, double quotes can quote the history expansion character. */ |
| 108 | int double_quotes_inhibit_history_expansion = 0; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 109 | #endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 110 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 111 | /* With the old default, every line was saved in the history individually. |
| 112 | I.e., if the user enters: |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 113 | bash$ for i in a b c |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 114 | > do |
| 115 | > echo $i |
| 116 | > done |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 117 | Each line will be individually saved in the history. |
| 118 | bash$ history |
| 119 | 10 for i in a b c |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 120 | 11 do |
| 121 | 12 echo $i |
| 122 | 13 done |
| 123 | 14 history |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 124 | If the variable command_oriented_history is set, multiple lines |
| 125 | which form one command will be saved as one history entry. |
| 126 | bash$ for i in a b c |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 127 | > do |
| 128 | > echo $i |
| 129 | > done |
| 130 | bash$ history |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 131 | 10 for i in a b c |
| 132 | do |
| 133 | echo $i |
| 134 | done |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 135 | 11 history |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 136 | The user can then recall the whole command all at once instead |
| 137 | of just being able to recall one line at a time. |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 138 | |
| 139 | This is now enabled by default. |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 140 | */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 141 | int command_oriented_history = 1; |
| 142 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 143 | /* Set to 1 if the first line of a possibly-multi-line command was saved |
| 144 | in the history list. Managed by maybe_add_history(), but global so |
| 145 | the history-manipluating builtins can see it. */ |
| 146 | int current_command_first_line_saved = 0; |
| 147 | |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame^] | 148 | /* Set to the number of the most recent line of a possibly-multi-line command |
| 149 | that contains a shell comment. Used by bash_add_history() to determine |
| 150 | whether to add a newline or a semicolon. */ |
| 151 | int current_command_line_comment = 0; |
| 152 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 153 | /* Non-zero means to store newlines in the history list when using |
| 154 | command_oriented_history rather than trying to use semicolons. */ |
| 155 | int literal_history; |
| 156 | |
| 157 | /* Non-zero means to append the history to the history file at shell |
| 158 | exit, even if the history has been stifled. */ |
| 159 | int force_append_history; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 160 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 161 | /* A nit for picking at history saving. Flags have the following values: |
| 162 | |
| 163 | Value == 0 means save all lines parsed by the shell on the history. |
| 164 | Value & HC_IGNSPACE means save all lines that do not start with a space. |
| 165 | Value & HC_IGNDUPS means save all lines that do not match the last |
| 166 | line saved. |
| 167 | Value & HC_ERASEDUPS means to remove all other matching lines from the |
| 168 | history list before saving the latest line. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 169 | int history_control; |
| 170 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 171 | /* Set to 1 if the last command was added to the history list successfully |
| 172 | as a separate history entry; set to 0 if the line was ignored or added |
| 173 | to a previous entry as part of command-oriented-history processing. */ |
| 174 | int hist_last_line_added; |
| 175 | |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 176 | /* Set to 1 if builtins/history.def:push_history added the last history |
| 177 | entry. */ |
| 178 | int hist_last_line_pushed; |
| 179 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 180 | #if defined (READLINE) |
| 181 | /* If non-zero, and readline is being used, the user is offered the |
| 182 | chance to re-edit a failed history expansion. */ |
| 183 | int history_reediting; |
| 184 | |
| 185 | /* If non-zero, and readline is being used, don't directly execute a |
| 186 | line with history substitution. Reload it into the editing buffer |
| 187 | instead and let the user further edit and confirm with a newline. */ |
| 188 | int hist_verify; |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 189 | |
| 190 | #endif /* READLINE */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 191 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 192 | /* Non-zero means to not save function definitions in the history list. */ |
| 193 | int dont_save_function_defs; |
| 194 | |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 195 | #if defined (BANG_HISTORY) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 196 | static int bash_history_inhibit_expansion __P((char *, int)); |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 197 | #endif |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 198 | #if defined (READLINE) |
| 199 | static void re_edit __P((char *)); |
| 200 | #endif |
| 201 | static int history_expansion_p __P((char *)); |
| 202 | static int shell_comment __P((char *)); |
| 203 | static int should_expand __P((char *)); |
| 204 | static HIST_ENTRY *last_history_entry __P((void)); |
| 205 | static char *expand_histignore_pattern __P((char *)); |
| 206 | static int history_should_ignore __P((char *)); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 207 | |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 208 | #if defined (BANG_HISTORY) |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 209 | /* Is the history expansion starting at string[i] one that should not |
| 210 | be expanded? */ |
| 211 | static int |
| 212 | bash_history_inhibit_expansion (string, i) |
| 213 | char *string; |
| 214 | int i; |
| 215 | { |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame^] | 216 | int t, si; |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 217 | char hx[2]; |
| 218 | |
| 219 | hx[0] = history_expansion_char; |
| 220 | hx[1] = '\0'; |
| 221 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 222 | /* The shell uses ! as a pattern negation character in globbing [...] |
| 223 | expressions, so let those pass without expansion. */ |
| 224 | if (i > 0 && (string[i - 1] == '[') && member (']', string + i + 1)) |
| 225 | return (1); |
| 226 | /* The shell uses ! as the indirect expansion character, so let those |
| 227 | expansions pass as well. */ |
| 228 | else if (i > 1 && string[i - 1] == '{' && string[i - 2] == '$' && |
| 229 | member ('}', string + i + 1)) |
| 230 | return (1); |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 231 | /* The shell uses $! as a defined parameter expansion. */ |
| 232 | else if (i > 1 && string[i - 1] == '$' && string[i] == '!') |
| 233 | return (1); |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 234 | #if defined (EXTENDED_GLOB) |
| 235 | else if (extended_glob && i > 1 && string[i+1] == '(' && member (')', string + i + 2)) |
| 236 | return (1); |
| 237 | #endif |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 238 | |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame^] | 239 | si = 0; |
| 240 | /* If we're supposed to be in single-quoted string, skip over the |
| 241 | single-quoted part and then look at what's left. */ |
| 242 | if (history_quoting_state == '\'') |
| 243 | { |
| 244 | si = skip_to_delim (string, 0, "'", SD_NOJMP|SD_HISTEXP); |
| 245 | if (string[si] == 0 || si >= i) |
| 246 | return (1); |
| 247 | si++; |
| 248 | } |
| 249 | |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 250 | /* Make sure the history expansion should not be skipped by quoting or |
| 251 | command/process substitution. */ |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame^] | 252 | if ((t = skip_to_histexp (string, si, hx, SD_NOJMP|SD_HISTEXP)) > 0) |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 253 | { |
| 254 | /* Skip instances of history expansion appearing on the line before |
| 255 | this one. */ |
| 256 | while (t < i) |
| 257 | { |
| 258 | t = skip_to_histexp (string, t+1, hx, SD_NOJMP|SD_HISTEXP); |
| 259 | if (t <= 0) |
| 260 | return 0; |
| 261 | } |
| 262 | return (t > i); |
| 263 | } |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 264 | else |
| 265 | return (0); |
| 266 | } |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 267 | #endif |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 268 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 269 | void |
| 270 | bash_initialize_history () |
| 271 | { |
| 272 | history_quotes_inhibit_expansion = 1; |
| 273 | history_search_delimiter_chars = ";&()|<>"; |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 274 | #if defined (BANG_HISTORY) |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 275 | history_inhibit_expansion_function = bash_history_inhibit_expansion; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 276 | sv_histchars ("histchars"); |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 277 | #endif |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | void |
| 281 | bash_history_reinit (interact) |
| 282 | int interact; |
| 283 | { |
| 284 | #if defined (BANG_HISTORY) |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame^] | 285 | history_expansion = (interact == 0) ? histexp_flag : HISTEXPAND_DEFAULT; |
| 286 | history_expansion_inhibited = (interact == 0) ? 1 - histexp_flag : 0; /* changed in bash_history_enable() */ |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 287 | history_inhibit_expansion_function = bash_history_inhibit_expansion; |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 288 | #endif |
| 289 | remember_on_history = enable_history_list; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | void |
| 293 | bash_history_disable () |
| 294 | { |
| 295 | remember_on_history = 0; |
| 296 | #if defined (BANG_HISTORY) |
| 297 | history_expansion_inhibited = 1; |
| 298 | #endif |
| 299 | } |
| 300 | |
| 301 | void |
| 302 | bash_history_enable () |
| 303 | { |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 304 | remember_on_history = enable_history_list = 1; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 305 | #if defined (BANG_HISTORY) |
| 306 | history_expansion_inhibited = 0; |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 307 | history_inhibit_expansion_function = bash_history_inhibit_expansion; |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 308 | #endif |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 309 | sv_history_control ("HISTCONTROL"); |
| 310 | sv_histignore ("HISTIGNORE"); |
| 311 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 312 | |
| 313 | /* Load the history list from the history file. */ |
| 314 | void |
| 315 | load_history () |
| 316 | { |
| 317 | char *hf; |
| 318 | |
| 319 | /* Truncate history file for interactive shells which desire it. |
| 320 | Note that the history file is automatically truncated to the |
| 321 | size of HISTSIZE if the user does not explicitly set the size |
| 322 | differently. */ |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame^] | 323 | set_if_not ("HISTSIZE", HISTSIZE_DEFAULT); |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 324 | sv_histsize ("HISTSIZE"); |
| 325 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 326 | set_if_not ("HISTFILESIZE", get_string_value ("HISTSIZE")); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 327 | sv_histsize ("HISTFILESIZE"); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 328 | |
| 329 | /* Read the history in HISTFILE into the history list. */ |
| 330 | hf = get_string_value ("HISTFILE"); |
| 331 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 332 | if (hf && *hf && file_exists (hf)) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 333 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 334 | read_history (hf); |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 335 | /* We have read all of the lines from the history file, even if we |
| 336 | read more lines than $HISTSIZE. Remember the total number of lines |
| 337 | we read so we don't count the last N lines as new over and over |
| 338 | again. */ |
| 339 | history_lines_in_file = history_lines_read_from_file; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 340 | using_history (); |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 341 | /* history_lines_in_file = where_history () + history_base - 1; */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 345 | void |
| 346 | bash_clear_history () |
| 347 | { |
| 348 | clear_history (); |
| 349 | history_lines_this_session = 0; |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 350 | /* XXX - reset history_lines_read_from_file? */ |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | /* Delete and free the history list entry at offset I. */ |
| 354 | int |
| 355 | bash_delete_histent (i) |
| 356 | int i; |
| 357 | { |
| 358 | HIST_ENTRY *discard; |
| 359 | |
| 360 | discard = remove_history (i); |
| 361 | if (discard) |
| 362 | free_history_entry (discard); |
| 363 | history_lines_this_session--; |
| 364 | |
| 365 | return 1; |
| 366 | } |
| 367 | |
| 368 | int |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame^] | 369 | bash_delete_history_range (first, last) |
| 370 | int first, last; |
| 371 | { |
| 372 | register int i; |
| 373 | HIST_ENTRY **discard_list; |
| 374 | |
| 375 | discard_list = remove_history_range (first, last); |
| 376 | for (i = 0; discard_list && discard_list[i]; i++) |
| 377 | free_history_entry (discard_list[i]); |
| 378 | history_lines_this_session -= i; |
| 379 | |
| 380 | return 1; |
| 381 | } |
| 382 | |
| 383 | int |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 384 | bash_delete_last_history () |
| 385 | { |
| 386 | register int i; |
| 387 | HIST_ENTRY **hlist, *histent; |
| 388 | int r; |
| 389 | |
| 390 | hlist = history_list (); |
| 391 | if (hlist == NULL) |
| 392 | return 0; |
| 393 | |
| 394 | for (i = 0; hlist[i]; i++) |
| 395 | ; |
| 396 | i--; |
| 397 | |
| 398 | /* History_get () takes a parameter that must be offset by history_base. */ |
| 399 | histent = history_get (history_base + i); /* Don't free this */ |
| 400 | if (histent == NULL) |
| 401 | return 0; |
| 402 | |
| 403 | r = bash_delete_histent (i); |
| 404 | |
| 405 | if (where_history () > history_length) |
| 406 | history_set_pos (history_length); |
| 407 | |
| 408 | return r; |
| 409 | } |
| 410 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 411 | #ifdef INCLUDE_UNUSED |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 412 | /* Write the existing history out to the history file. */ |
| 413 | void |
| 414 | save_history () |
| 415 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 416 | char *hf; |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 417 | int r; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 418 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 419 | hf = get_string_value ("HISTFILE"); |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 420 | if (hf && *hf && file_exists (hf)) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 421 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 422 | /* Append only the lines that occurred this session to |
| 423 | the history file. */ |
| 424 | using_history (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 425 | |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 426 | if (history_lines_this_session <= where_history () || force_append_history) |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 427 | r = append_history (history_lines_this_session, hf); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 428 | else |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 429 | r = write_history (hf); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 430 | sv_histsize ("HISTFILESIZE"); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 431 | } |
| 432 | } |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 433 | #endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 434 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 435 | int |
| 436 | maybe_append_history (filename) |
| 437 | char *filename; |
| 438 | { |
| 439 | int fd, result; |
| 440 | struct stat buf; |
| 441 | |
| 442 | result = EXECUTION_SUCCESS; |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 443 | if (history_lines_this_session > 0 && (history_lines_this_session <= where_history ())) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 444 | { |
| 445 | /* If the filename was supplied, then create it if necessary. */ |
| 446 | if (stat (filename, &buf) == -1 && errno == ENOENT) |
| 447 | { |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 448 | fd = open (filename, O_WRONLY|O_CREAT, 0600); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 449 | if (fd < 0) |
| 450 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 451 | builtin_error (_("%s: cannot create: %s"), filename, strerror (errno)); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 452 | return (EXECUTION_FAILURE); |
| 453 | } |
| 454 | close (fd); |
| 455 | } |
| 456 | result = append_history (history_lines_this_session, filename); |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 457 | /* Pretend we already read these lines from the file because we just |
| 458 | added them */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 459 | history_lines_in_file += history_lines_this_session; |
| 460 | history_lines_this_session = 0; |
| 461 | } |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 462 | else |
| 463 | history_lines_this_session = 0; /* reset if > where_history() */ |
| 464 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 465 | return (result); |
| 466 | } |
| 467 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 468 | /* If this is an interactive shell, then append the lines executed |
| 469 | this session to the history file. */ |
| 470 | int |
| 471 | maybe_save_shell_history () |
| 472 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 473 | int result; |
| 474 | char *hf; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 475 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 476 | result = 0; |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 477 | if (history_lines_this_session > 0) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 478 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 479 | hf = get_string_value ("HISTFILE"); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 480 | |
| 481 | if (hf && *hf) |
| 482 | { |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 483 | /* If the file doesn't exist, then create it. */ |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 484 | if (file_exists (hf) == 0) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 485 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 486 | int file; |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 487 | file = open (hf, O_CREAT | O_TRUNC | O_WRONLY, 0600); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 488 | if (file != -1) |
| 489 | close (file); |
| 490 | } |
| 491 | |
| 492 | /* Now actually append the lines if the history hasn't been |
| 493 | stifled. If the history has been stifled, rewrite the |
| 494 | history file. */ |
| 495 | using_history (); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 496 | if (history_lines_this_session <= where_history () || force_append_history) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 497 | { |
| 498 | result = append_history (history_lines_this_session, hf); |
| 499 | history_lines_in_file += history_lines_this_session; |
| 500 | } |
| 501 | else |
| 502 | { |
| 503 | result = write_history (hf); |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 504 | history_lines_in_file = history_lines_written_to_file; |
| 505 | /* history_lines_in_file = where_history () + history_base - 1; */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 506 | } |
| 507 | history_lines_this_session = 0; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 508 | |
| 509 | sv_histsize ("HISTFILESIZE"); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | return (result); |
| 513 | } |
| 514 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 515 | #if defined (READLINE) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 516 | /* Tell readline () that we have some text for it to edit. */ |
| 517 | static void |
| 518 | re_edit (text) |
| 519 | char *text; |
| 520 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 521 | if (bash_input.type == st_stdin) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 522 | bash_re_edit (text); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 523 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 524 | #endif /* READLINE */ |
| 525 | |
| 526 | /* Return 1 if this line needs history expansion. */ |
| 527 | static int |
| 528 | history_expansion_p (line) |
| 529 | char *line; |
| 530 | { |
| 531 | register char *s; |
| 532 | |
| 533 | for (s = line; *s; s++) |
| 534 | if (*s == history_expansion_char || *s == history_subst_char) |
| 535 | return 1; |
| 536 | return 0; |
| 537 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 538 | |
| 539 | /* Do pre-processing on LINE. If PRINT_CHANGES is non-zero, then |
| 540 | print the results of expanding the line if there were any changes. |
| 541 | If there is an error, return NULL, otherwise the expanded line is |
| 542 | returned. If ADDIT is non-zero the line is added to the history |
| 543 | list after history expansion. ADDIT is just a suggestion; |
| 544 | REMEMBER_ON_HISTORY can veto, and does. |
| 545 | Right now this does history expansion. */ |
| 546 | char * |
| 547 | pre_process_line (line, print_changes, addit) |
| 548 | char *line; |
| 549 | int print_changes, addit; |
| 550 | { |
| 551 | char *history_value; |
| 552 | char *return_value; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 553 | int expanded; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 554 | |
| 555 | return_value = line; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 556 | expanded = 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 557 | |
| 558 | # if defined (BANG_HISTORY) |
| 559 | /* History expand the line. If this results in no errors, then |
| 560 | add that line to the history if ADDIT is non-zero. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 561 | if (!history_expansion_inhibited && history_expansion && history_expansion_p (line)) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 562 | { |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame^] | 563 | /* If we are expanding the second or later line of a multi-line |
| 564 | command, decrease history_length so references to history expansions |
| 565 | in these lines refer to the previous history entry and not the |
| 566 | current command. */ |
| 567 | if (history_length > 0 && command_oriented_history && current_command_first_line_saved && current_command_line_count > 1) |
| 568 | history_length--; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 569 | expanded = history_expand (line, &history_value); |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame^] | 570 | if (history_length >= 0 && command_oriented_history && current_command_first_line_saved && current_command_line_count > 1) |
| 571 | history_length++; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 572 | |
| 573 | if (expanded) |
| 574 | { |
| 575 | if (print_changes) |
| 576 | { |
| 577 | if (expanded < 0) |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 578 | internal_error ("%s", history_value); |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 579 | #if defined (READLINE) |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 580 | else if (hist_verify == 0 || expanded == 2) |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 581 | #else |
| 582 | else |
| 583 | #endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 584 | fprintf (stderr, "%s\n", history_value); |
| 585 | } |
| 586 | |
| 587 | /* If there was an error, return NULL. */ |
| 588 | if (expanded < 0 || expanded == 2) /* 2 == print only */ |
| 589 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 590 | # if defined (READLINE) |
| 591 | if (expanded == 2 && rl_dispatching == 0 && *history_value) |
| 592 | # else |
| 593 | if (expanded == 2 && *history_value) |
| 594 | # endif /* !READLINE */ |
| 595 | maybe_add_history (history_value); |
| 596 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 597 | free (history_value); |
| 598 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 599 | # if defined (READLINE) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 600 | /* New hack. We can allow the user to edit the |
| 601 | failed history expansion. */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 602 | if (history_reediting && expanded < 0 && rl_done) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 603 | re_edit (line); |
| 604 | # endif /* READLINE */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 605 | return ((char *)NULL); |
| 606 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 607 | |
| 608 | # if defined (READLINE) |
| 609 | if (hist_verify && expanded == 1) |
| 610 | { |
| 611 | re_edit (history_value); |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 612 | free (history_value); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 613 | return ((char *)NULL); |
| 614 | } |
| 615 | # endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | /* Let other expansions know that return_value can be free'ed, |
| 619 | and that a line has been added to the history list. Note |
| 620 | that we only add lines that have something in them. */ |
| 621 | expanded = 1; |
| 622 | return_value = history_value; |
| 623 | } |
| 624 | # endif /* BANG_HISTORY */ |
| 625 | |
| 626 | if (addit && remember_on_history && *return_value) |
| 627 | maybe_add_history (return_value); |
| 628 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 629 | #if 0 |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 630 | if (expanded == 0) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 631 | return_value = savestring (line); |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 632 | #endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 633 | |
| 634 | return (return_value); |
| 635 | } |
| 636 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 637 | /* Return 1 if the first non-whitespace character in LINE is a `#', indicating |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame^] | 638 | that the line is a shell comment. Return 2 if there is a comment after the |
| 639 | first non-whitespace character. Return 0 if the line does not contain a |
| 640 | comment. */ |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 641 | static int |
| 642 | shell_comment (line) |
| 643 | char *line; |
| 644 | { |
| 645 | char *p; |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame^] | 646 | int n; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 647 | |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame^] | 648 | if (line == 0) |
| 649 | return 0; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 650 | for (p = line; p && *p && whitespace (*p); p++) |
| 651 | ; |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame^] | 652 | if (p && *p == '#') |
| 653 | return 1; |
| 654 | n = skip_to_delim (line, p - line, "#", SD_NOJMP|SD_GLOB|SD_EXTGLOB|SD_COMPLETE); |
| 655 | return (line[n] == '#') ? 2 : 0; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 656 | } |
| 657 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 658 | #ifdef INCLUDE_UNUSED |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 659 | /* Remove shell comments from LINE. A `#' and anything after it is a comment. |
| 660 | This isn't really useful yet, since it doesn't handle quoting. */ |
| 661 | static char * |
| 662 | filter_comments (line) |
| 663 | char *line; |
| 664 | { |
| 665 | char *p; |
| 666 | |
| 667 | for (p = line; p && *p && *p != '#'; p++) |
| 668 | ; |
| 669 | if (p && *p == '#') |
| 670 | *p = '\0'; |
| 671 | return (line); |
| 672 | } |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 673 | #endif |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 674 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 675 | /* Check LINE against what HISTCONTROL says to do. Returns 1 if the line |
| 676 | should be saved; 0 if it should be discarded. */ |
| 677 | static int |
| 678 | check_history_control (line) |
| 679 | char *line; |
| 680 | { |
| 681 | HIST_ENTRY *temp; |
| 682 | int r; |
| 683 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 684 | if (history_control == 0) |
| 685 | return 1; |
| 686 | |
| 687 | /* ignorespace or ignoreboth */ |
| 688 | if ((history_control & HC_IGNSPACE) && *line == ' ') |
| 689 | return 0; |
| 690 | |
| 691 | /* ignoredups or ignoreboth */ |
| 692 | if (history_control & HC_IGNDUPS) |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 693 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 694 | using_history (); |
| 695 | temp = previous_history (); |
| 696 | |
| 697 | r = (temp == 0 || STREQ (temp->line, line) == 0); |
| 698 | |
| 699 | using_history (); |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 700 | |
| 701 | if (r == 0) |
| 702 | return r; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 703 | } |
| 704 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 705 | return 1; |
| 706 | } |
| 707 | |
| 708 | /* Remove all entries matching LINE from the history list. Triggered when |
| 709 | HISTCONTROL includes `erasedups'. */ |
| 710 | static void |
| 711 | hc_erasedups (line) |
| 712 | char *line; |
| 713 | { |
| 714 | HIST_ENTRY *temp; |
| 715 | int r; |
| 716 | |
| 717 | using_history (); |
| 718 | while (temp = previous_history ()) |
| 719 | { |
| 720 | if (STREQ (temp->line, line)) |
| 721 | { |
| 722 | r = where_history (); |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 723 | temp = remove_history (r); |
| 724 | if (temp) |
| 725 | free_history_entry (temp); |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 726 | } |
| 727 | } |
| 728 | using_history (); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | /* Add LINE to the history list, handling possibly multi-line compound |
| 732 | commands. We note whether or not we save the first line of each command |
| 733 | (which is usually the entire command and history entry), and don't add |
| 734 | the second and subsequent lines of a multi-line compound command if we |
| 735 | didn't save the first line. We don't usually save shell comment lines in |
| 736 | compound commands in the history, because they could have the effect of |
| 737 | commenting out the rest of the command when the entire command is saved as |
| 738 | a single history entry (when COMMAND_ORIENTED_HISTORY is enabled). If |
| 739 | LITERAL_HISTORY is set, we're saving lines in the history with embedded |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 740 | newlines, so it's OK to save comment lines. If we're collecting the body |
| 741 | of a here-document, we should act as if literal_history is enabled, because |
| 742 | we want to save the entire contents of the here-document as it was |
| 743 | entered. We also make sure to save multiple-line quoted strings or other |
| 744 | constructs. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 745 | void |
| 746 | maybe_add_history (line) |
| 747 | char *line; |
| 748 | { |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame^] | 749 | int is_comment; |
| 750 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 751 | hist_last_line_added = 0; |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame^] | 752 | is_comment = shell_comment (line); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 753 | |
| 754 | /* Don't use the value of history_control to affect the second |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 755 | and subsequent lines of a multi-line command (old code did |
| 756 | this only when command_oriented_history is enabled). */ |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 757 | if (current_command_line_count > 1) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 758 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 759 | if (current_command_first_line_saved && |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame^] | 760 | ((parser_state & PST_HEREDOC) || literal_history || dstack.delimiter_depth != 0 || is_comment != 1)) |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 761 | bash_add_history (line); |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame^] | 762 | current_command_line_comment = is_comment ? current_command_line_count : -2; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 763 | return; |
| 764 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 765 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 766 | /* This is the first line of a (possible multi-line) command. Note whether |
| 767 | or not we should save the first line and remember it. */ |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame^] | 768 | current_command_line_comment = is_comment ? current_command_line_count : -2; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 769 | current_command_first_line_saved = check_add_history (line, 0); |
| 770 | } |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 771 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 772 | /* Just check LINE against HISTCONTROL and HISTIGNORE and add it to the |
| 773 | history if it's OK. Used by `history -s' as well as maybe_add_history(). |
| 774 | Returns 1 if the line was saved in the history, 0 otherwise. */ |
| 775 | int |
| 776 | check_add_history (line, force) |
| 777 | char *line; |
| 778 | int force; |
| 779 | { |
| 780 | if (check_history_control (line) && history_should_ignore (line) == 0) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 781 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 782 | /* We're committed to saving the line. If the user has requested it, |
| 783 | remove other matching lines from the history. */ |
| 784 | if (history_control & HC_ERASEDUPS) |
| 785 | hc_erasedups (line); |
| 786 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 787 | if (force) |
| 788 | { |
| 789 | really_add_history (line); |
| 790 | using_history (); |
| 791 | } |
| 792 | else |
| 793 | bash_add_history (line); |
| 794 | return 1; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 795 | } |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 796 | return 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 797 | } |
| 798 | |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 799 | #if defined (SYSLOG_HISTORY) |
| 800 | #define SYSLOG_MAXLEN 600 |
| 801 | |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 802 | #ifndef OPENLOG_OPTS |
| 803 | #define OPENLOG_OPTS 0 |
| 804 | #endif |
| 805 | |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame^] | 806 | #if defined (SYSLOG_SHOPT) |
| 807 | int syslog_history = SYSLOG_SHOPT; |
| 808 | #else |
| 809 | int syslog_history = 1; |
| 810 | #endif |
| 811 | |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 812 | void |
| 813 | bash_syslog_history (line) |
| 814 | const char *line; |
| 815 | { |
| 816 | char trunc[SYSLOG_MAXLEN]; |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 817 | static int first = 1; |
| 818 | |
| 819 | if (first) |
| 820 | { |
| 821 | openlog (shell_name, OPENLOG_OPTS, SYSLOG_FACILITY); |
| 822 | first = 0; |
| 823 | } |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 824 | |
| 825 | if (strlen(line) < SYSLOG_MAXLEN) |
| 826 | syslog (SYSLOG_FACILITY|SYSLOG_LEVEL, "HISTORY: PID=%d UID=%d %s", getpid(), current_user.uid, line); |
| 827 | else |
| 828 | { |
| 829 | strncpy (trunc, line, SYSLOG_MAXLEN); |
| 830 | trunc[SYSLOG_MAXLEN - 1] = '\0'; |
| 831 | syslog (SYSLOG_FACILITY|SYSLOG_LEVEL, "HISTORY (TRUNCATED): PID=%d UID=%d %s", getpid(), current_user.uid, trunc); |
| 832 | } |
| 833 | } |
| 834 | #endif |
| 835 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 836 | /* Add a line to the history list. |
| 837 | The variable COMMAND_ORIENTED_HISTORY controls the style of history |
| 838 | remembering; when non-zero, and LINE is not the first line of a |
| 839 | complete parser construct, append LINE to the last history line instead |
| 840 | of adding it as a new line. */ |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 841 | void |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 842 | bash_add_history (line) |
| 843 | char *line; |
| 844 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 845 | int add_it, offset, curlen; |
| 846 | HIST_ENTRY *current, *old; |
| 847 | char *chars_to_add, *new_line; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 848 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 849 | add_it = 1; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 850 | if (command_oriented_history && current_command_line_count > 1) |
| 851 | { |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 852 | /* The second and subsequent lines of a here document have the trailing |
| 853 | newline preserved. We don't want to add extra newlines here, but we |
| 854 | do want to add one after the first line (which is the command that |
| 855 | contains the here-doc specifier). parse.y:history_delimiting_chars() |
| 856 | does the right thing to take care of this for us. We don't want to |
| 857 | add extra newlines if the user chooses to enable literal_history, |
| 858 | so we have to duplicate some of what that function does here. */ |
| 859 | if ((parser_state & PST_HEREDOC) && literal_history && current_command_line_count > 2 && line[strlen (line) - 1] == '\n') |
| 860 | chars_to_add = ""; |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame^] | 861 | else if (current_command_line_count == current_command_line_comment+1) |
| 862 | chars_to_add = "\n"; |
| 863 | else if (literal_history) |
| 864 | chars_to_add = "\n"; |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 865 | else |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame^] | 866 | chars_to_add = history_delimiting_chars (line); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 867 | |
| 868 | using_history (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 869 | current = previous_history (); |
| 870 | |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame^] | 871 | current_command_line_comment = shell_comment (line) ? current_command_line_count : -2; |
| 872 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 873 | if (current) |
| 874 | { |
| 875 | /* If the previous line ended with an escaped newline (escaped |
| 876 | with backslash, but otherwise unquoted), then remove the quoted |
| 877 | newline, since that is what happens when the line is parsed. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 878 | curlen = strlen (current->line); |
| 879 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 880 | if (dstack.delimiter_depth == 0 && current->line[curlen - 1] == '\\' && |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 881 | current->line[curlen - 2] != '\\') |
| 882 | { |
| 883 | current->line[curlen - 1] = '\0'; |
| 884 | curlen--; |
| 885 | chars_to_add = ""; |
| 886 | } |
| 887 | |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 888 | /* If we're not in some kind of quoted construct, the current history |
| 889 | entry ends with a newline, and we're going to add a semicolon, |
| 890 | don't. In some cases, it results in a syntax error (e.g., before |
| 891 | a close brace), and it should not be needed. */ |
| 892 | if (dstack.delimiter_depth == 0 && current->line[curlen - 1] == '\n' && *chars_to_add == ';') |
| 893 | chars_to_add++; |
| 894 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 895 | new_line = (char *)xmalloc (1 |
| 896 | + curlen |
| 897 | + strlen (line) |
| 898 | + strlen (chars_to_add)); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 899 | sprintf (new_line, "%s%s%s", current->line, chars_to_add, line); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 900 | offset = where_history (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 901 | old = replace_history_entry (offset, new_line, current->data); |
| 902 | free (new_line); |
| 903 | |
| 904 | if (old) |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 905 | free_history_entry (old); |
| 906 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 907 | add_it = 0; |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | if (add_it) |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 912 | really_add_history (line); |
| 913 | |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 914 | #if defined (SYSLOG_HISTORY) |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame^] | 915 | if (syslog_history) |
| 916 | bash_syslog_history (line); |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 917 | #endif |
| 918 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 919 | using_history (); |
| 920 | } |
| 921 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 922 | static void |
| 923 | really_add_history (line) |
| 924 | char *line; |
| 925 | { |
| 926 | hist_last_line_added = 1; |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 927 | hist_last_line_pushed = 0; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 928 | add_history (line); |
| 929 | history_lines_this_session++; |
| 930 | } |
| 931 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 932 | int |
| 933 | history_number () |
| 934 | { |
| 935 | using_history (); |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 936 | return (remember_on_history ? history_base + where_history () : 1); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 937 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 938 | |
| 939 | static int |
| 940 | should_expand (s) |
| 941 | char *s; |
| 942 | { |
| 943 | char *p; |
| 944 | |
| 945 | for (p = s; p && *p; p++) |
| 946 | { |
| 947 | if (*p == '\\') |
| 948 | p++; |
| 949 | else if (*p == '&') |
| 950 | return 1; |
| 951 | } |
| 952 | return 0; |
| 953 | } |
| 954 | |
| 955 | static int |
| 956 | histignore_item_func (ign) |
| 957 | struct ign *ign; |
| 958 | { |
| 959 | if (should_expand (ign->val)) |
| 960 | ign->flags |= HIGN_EXPAND; |
| 961 | return (0); |
| 962 | } |
| 963 | |
| 964 | void |
| 965 | setup_history_ignore (varname) |
| 966 | char *varname; |
| 967 | { |
| 968 | setup_ignore_patterns (&histignore); |
| 969 | } |
| 970 | |
| 971 | static HIST_ENTRY * |
| 972 | last_history_entry () |
| 973 | { |
| 974 | HIST_ENTRY *he; |
| 975 | |
| 976 | using_history (); |
| 977 | he = previous_history (); |
| 978 | using_history (); |
| 979 | return he; |
| 980 | } |
| 981 | |
| 982 | char * |
| 983 | last_history_line () |
| 984 | { |
| 985 | HIST_ENTRY *he; |
| 986 | |
| 987 | he = last_history_entry (); |
| 988 | if (he == 0) |
| 989 | return ((char *)NULL); |
| 990 | return he->line; |
| 991 | } |
| 992 | |
| 993 | static char * |
| 994 | expand_histignore_pattern (pat) |
| 995 | char *pat; |
| 996 | { |
| 997 | HIST_ENTRY *phe; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 998 | char *ret; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 999 | |
| 1000 | phe = last_history_entry (); |
| 1001 | |
| 1002 | if (phe == (HIST_ENTRY *)0) |
| 1003 | return (savestring (pat)); |
| 1004 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1005 | ret = strcreplace (pat, '&', phe->line, 1); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1006 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1007 | return ret; |
| 1008 | } |
| 1009 | |
| 1010 | /* Return 1 if we should not put LINE into the history according to the |
| 1011 | patterns in HISTIGNORE. */ |
| 1012 | static int |
| 1013 | history_should_ignore (line) |
| 1014 | char *line; |
| 1015 | { |
| 1016 | register int i, match; |
| 1017 | char *npat; |
| 1018 | |
| 1019 | if (histignore.num_ignores == 0) |
| 1020 | return 0; |
| 1021 | |
| 1022 | for (i = match = 0; i < histignore.num_ignores; i++) |
| 1023 | { |
| 1024 | if (histignore.ignores[i].flags & HIGN_EXPAND) |
| 1025 | npat = expand_histignore_pattern (histignore.ignores[i].val); |
| 1026 | else |
| 1027 | npat = histignore.ignores[i].val; |
| 1028 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1029 | match = strmatch (npat, line, FNMATCH_EXTFLAG) != FNM_NOMATCH; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1030 | |
| 1031 | if (histignore.ignores[i].flags & HIGN_EXPAND) |
| 1032 | free (npat); |
| 1033 | |
| 1034 | if (match) |
| 1035 | break; |
| 1036 | } |
| 1037 | |
| 1038 | return match; |
| 1039 | } |
| 1040 | #endif /* HISTORY */ |