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