Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1 | This file is history.def, from which is created history.c. |
| 2 | It implements the builtin "history" in Bash. |
| 3 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 4 | Copyright (C) 1987-2009 Free Software Foundation, Inc. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5 | |
| 6 | This file is part of GNU Bash, the Bourne Again SHell. |
| 7 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 8 | Bash is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 3 of the License, or |
| 11 | (at your option) any later version. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 12 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 13 | Bash is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 17 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 18 | You should have received a copy of the GNU General Public License |
| 19 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 20 | |
| 21 | $PRODUCES history.c |
| 22 | |
| 23 | $BUILTIN history |
| 24 | $FUNCTION history_builtin |
| 25 | $DEPENDS_ON HISTORY |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 26 | $SHORT_DOC history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg...] |
| 27 | Display or manipulate the history list. |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 28 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 29 | Display the history list with line numbers, prefixing each modified |
| 30 | entry with a `*'. An argument of N lists only the last N entries. |
| 31 | |
| 32 | Options: |
| 33 | -c clear the history list by deleting all of the entries |
| 34 | -d offset delete the history entry at offset OFFSET. |
| 35 | |
| 36 | -a append history lines from this session to the history file |
| 37 | -n read all history lines not already read from the history file |
| 38 | -r read the history file and append the contents to the history |
| 39 | list |
| 40 | -w write the current history to the history file |
| 41 | and append them to the history list |
| 42 | |
| 43 | -p perform history expansion on each ARG and display the result |
| 44 | without storing it in the history list |
| 45 | -s append the ARGs to the history list as a single entry |
| 46 | |
| 47 | If FILENAME is given, it is used as the history file. Otherwise, |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 48 | if $HISTFILE has a value, that is used, else ~/.bash_history. |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 49 | |
| 50 | If the $HISTTIMEFORMAT variable is set and not null, its value is used |
| 51 | as a format string for strftime(3) to print the time stamp associated |
| 52 | with each displayed history entry. No time stamps are printed otherwise. |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 53 | |
| 54 | Exit Status: |
| 55 | Returns success unless an invalid option is given or an error occurs. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 56 | $END |
| 57 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 58 | #include <config.h> |
| 59 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 60 | #if defined (HISTORY) |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 61 | #include "../bashtypes.h" |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 62 | #if ! defined(_MINIX) && defined (HAVE_SYS_FILE_H) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 63 | # include <sys/file.h> |
| 64 | #endif |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 65 | #include "posixstat.h" |
| 66 | #include "filecntl.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 67 | #include <errno.h> |
| 68 | #include <stdio.h> |
| 69 | #if defined (HAVE_UNISTD_H) |
| 70 | # include <unistd.h> |
| 71 | #endif |
| 72 | |
| 73 | #include "../bashansi.h" |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 74 | #include "../bashintl.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 75 | |
| 76 | #include "../shell.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 77 | #include "../bashhist.h" |
| 78 | #include <readline/history.h> |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 79 | #include "bashgetopt.h" |
| 80 | #include "common.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 81 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 82 | #if !defined (errno) |
Ricardo Cerqueira | a02fbff | 2013-07-25 22:35:34 +0100 | [diff] [blame] | 83 | #include <errno.h> |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 84 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 85 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 86 | extern int current_command_line_count; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 87 | extern int force_append_history; /* shopt -s histappend */ |
| 88 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 89 | static char *histtime __P((HIST_ENTRY *, const char *)); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 90 | static int display_history __P((WORD_LIST *)); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 91 | static void push_history __P((WORD_LIST *)); |
| 92 | static int expand_and_print_history __P((WORD_LIST *)); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 93 | |
| 94 | #define AFLAG 0x01 |
| 95 | #define RFLAG 0x02 |
| 96 | #define WFLAG 0x04 |
| 97 | #define NFLAG 0x08 |
| 98 | #define SFLAG 0x10 |
| 99 | #define PFLAG 0x20 |
| 100 | #define CFLAG 0x40 |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 101 | #define DFLAG 0x80 |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 102 | |
| 103 | int |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 104 | history_builtin (list) |
| 105 | WORD_LIST *list; |
| 106 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 107 | int flags, opt, result, old_history_lines, obase; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 108 | char *filename, *delete_arg; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 109 | intmax_t delete_offset; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 110 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 111 | flags = 0; |
| 112 | reset_internal_getopt (); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 113 | while ((opt = internal_getopt (list, "acd:npsrw")) != -1) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 114 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 115 | switch (opt) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 116 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 117 | case 'a': |
| 118 | flags |= AFLAG; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 119 | break; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 120 | case 'c': |
| 121 | flags |= CFLAG; |
| 122 | break; |
| 123 | case 'n': |
| 124 | flags |= NFLAG; |
| 125 | break; |
| 126 | case 'r': |
| 127 | flags |= RFLAG; |
| 128 | break; |
| 129 | case 'w': |
| 130 | flags |= WFLAG; |
| 131 | break; |
| 132 | case 's': |
| 133 | flags |= SFLAG; |
| 134 | break; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 135 | case 'd': |
| 136 | flags |= DFLAG; |
| 137 | delete_arg = list_optarg; |
| 138 | break; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 139 | case 'p': |
| 140 | #if defined (BANG_HISTORY) |
| 141 | flags |= PFLAG; |
| 142 | #endif |
| 143 | break; |
| 144 | default: |
| 145 | builtin_usage (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 146 | return (EX_USAGE); |
| 147 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 148 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 149 | list = loptend; |
| 150 | |
| 151 | opt = flags & (AFLAG|RFLAG|WFLAG|NFLAG); |
| 152 | if (opt && opt != AFLAG && opt != RFLAG && opt != WFLAG && opt != NFLAG) |
| 153 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 154 | builtin_error (_("cannot use more than one of -anrw")); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 155 | return (EXECUTION_FAILURE); |
| 156 | } |
| 157 | |
| 158 | /* clear the history, but allow other arguments to add to it again. */ |
| 159 | if (flags & CFLAG) |
| 160 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 161 | bash_clear_history (); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 162 | if (list == 0) |
| 163 | return (EXECUTION_SUCCESS); |
| 164 | } |
| 165 | |
| 166 | if (flags & SFLAG) |
| 167 | { |
| 168 | if (list) |
| 169 | push_history (list); |
| 170 | return (EXECUTION_SUCCESS); |
| 171 | } |
| 172 | #if defined (BANG_HISTORY) |
| 173 | else if (flags & PFLAG) |
| 174 | { |
| 175 | if (list) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 176 | return (expand_and_print_history (list)); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 177 | return (sh_chkwrite (EXECUTION_SUCCESS)); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 178 | } |
| 179 | #endif |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 180 | else if (flags & DFLAG) |
| 181 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 182 | if ((legal_number (delete_arg, &delete_offset) == 0) |
| 183 | || (delete_offset < history_base) |
| 184 | || (delete_offset > (history_base + history_length))) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 185 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 186 | sh_erange (delete_arg, _("history position")); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 187 | return (EXECUTION_FAILURE); |
| 188 | } |
| 189 | opt = delete_offset; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 190 | result = bash_delete_histent (opt - history_base); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 191 | /* Since remove_history changes history_length, this can happen if |
| 192 | we delete the last history entry. */ |
| 193 | if (where_history () > history_length) |
| 194 | history_set_pos (history_length); |
| 195 | return (result ? EXECUTION_SUCCESS : EXECUTION_FAILURE); |
| 196 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 197 | else if ((flags & (AFLAG|RFLAG|NFLAG|WFLAG|CFLAG)) == 0) |
| 198 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 199 | result = display_history (list); |
| 200 | return (sh_chkwrite (result)); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | filename = list ? list->word->word : get_string_value ("HISTFILE"); |
| 204 | result = EXECUTION_SUCCESS; |
| 205 | |
| 206 | if (flags & AFLAG) /* Append session's history to file. */ |
| 207 | result = maybe_append_history (filename); |
| 208 | else if (flags & WFLAG) /* Write entire history. */ |
| 209 | result = write_history (filename); |
| 210 | else if (flags & RFLAG) /* Read entire file. */ |
| 211 | result = read_history (filename); |
| 212 | else if (flags & NFLAG) /* Read `new' history from file. */ |
| 213 | { |
| 214 | /* Read all of the lines in the file that we haven't already read. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 215 | old_history_lines = history_lines_in_file; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 216 | obase = history_base; |
| 217 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 218 | using_history (); |
| 219 | result = read_history_range (filename, history_lines_in_file, -1); |
| 220 | using_history (); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 221 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 222 | history_lines_in_file = where_history (); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 223 | |
| 224 | /* If we're rewriting the history file at shell exit rather than just |
| 225 | appending the lines from this session to it, the question is whether |
| 226 | we reset history_lines_this_session to 0, losing any history entries |
| 227 | we had before we read the new entries from the history file, or |
| 228 | whether we count the new entries we just read from the file as |
| 229 | history lines added during this session. |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 230 | Right now, we do the latter. This will cause these history entries |
| 231 | to be written to the history file along with any intermediate entries |
| 232 | we add when we do a `history -a', but the alternative is losing |
| 233 | them altogether. */ |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 234 | if (force_append_history == 0) |
| 235 | history_lines_this_session += history_lines_in_file - old_history_lines + |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 236 | history_base - obase; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | return (result ? EXECUTION_FAILURE : EXECUTION_SUCCESS); |
| 240 | } |
| 241 | |
| 242 | /* Accessors for HIST_ENTRY lists that are called HLIST. */ |
| 243 | #define histline(i) (hlist[(i)]->line) |
| 244 | #define histdata(i) (hlist[(i)]->data) |
| 245 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 246 | static char * |
| 247 | histtime (hlist, histtimefmt) |
| 248 | HIST_ENTRY *hlist; |
| 249 | const char *histtimefmt; |
| 250 | { |
| 251 | static char timestr[128]; |
| 252 | time_t t; |
| 253 | |
| 254 | t = history_get_time (hlist); |
| 255 | if (t) |
| 256 | strftime (timestr, sizeof (timestr), histtimefmt, localtime (&t)); |
| 257 | else |
| 258 | strcpy (timestr, "??"); |
| 259 | return timestr; |
| 260 | } |
| 261 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 262 | static int |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 263 | display_history (list) |
| 264 | WORD_LIST *list; |
| 265 | { |
| 266 | register int i; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 267 | intmax_t limit; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 268 | HIST_ENTRY **hlist; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 269 | char *histtimefmt, *timestr; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 270 | |
| 271 | if (list) |
| 272 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 273 | if (get_numeric_arg (list, 0, &limit) == 0) |
| 274 | return (EXECUTION_FAILURE); |
| 275 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 276 | if (limit < 0) |
| 277 | limit = -limit; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 278 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 279 | else |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 280 | limit = -1; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 281 | |
| 282 | hlist = history_list (); |
| 283 | |
| 284 | if (hlist) |
| 285 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 286 | for (i = 0; hlist[i]; i++) |
| 287 | ; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 288 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 289 | if (0 <= limit && limit < i) |
| 290 | i -= limit; |
| 291 | else |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 292 | i = 0; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 293 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 294 | histtimefmt = get_string_value ("HISTTIMEFORMAT"); |
| 295 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 296 | while (hlist[i]) |
| 297 | { |
| 298 | QUIT; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 299 | |
| 300 | timestr = (histtimefmt && *histtimefmt) ? histtime (hlist[i], histtimefmt) : (char *)NULL; |
| 301 | printf ("%5d%c %s%s\n", i + history_base, |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 302 | histdata(i) ? '*' : ' ', |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 303 | ((timestr && *timestr) ? timestr : ""), |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 304 | histline(i)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 305 | i++; |
| 306 | } |
| 307 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 308 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 309 | return (EXECUTION_SUCCESS); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | /* Remove the last entry in the history list and add each argument in |
| 313 | LIST to the history. */ |
| 314 | static void |
| 315 | push_history (list) |
| 316 | WORD_LIST *list; |
| 317 | { |
| 318 | char *s; |
| 319 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 320 | /* Delete the last history entry if it was a single entry added to the |
| 321 | history list (generally the `history -s' itself), or if `history -s' |
| 322 | is being used in a compound command and the compound command was |
| 323 | added to the history as a single element (command-oriented history). |
| 324 | If you don't want history -s to remove the compound command from the |
| 325 | history, change #if 0 to #if 1 below. */ |
| 326 | #if 0 |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 327 | if (remember_on_history && hist_last_line_pushed == 0 && |
| 328 | hist_last_line_added && bash_delete_last_history () == 0) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 329 | #else |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 330 | if (remember_on_history && hist_last_line_pushed == 0 && |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 331 | (hist_last_line_added || |
| 332 | (current_command_line_count > 0 && current_command_first_line_saved && command_oriented_history)) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 333 | && bash_delete_last_history () == 0) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 334 | #endif |
| 335 | return; |
| 336 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 337 | s = string_list (list); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 338 | /* Call check_add_history with FORCE set to 1 to skip the check against |
| 339 | current_command_line_count. If history -s is used in a compound |
| 340 | command, the above code will delete the compound command's history |
| 341 | entry and this call will add the line to the history as a separate |
| 342 | entry. Without FORCE=1, if current_command_line_count were > 1, the |
| 343 | line would be appended to the entry before the just-deleted entry. */ |
| 344 | check_add_history (s, 1); /* obeys HISTCONTROL, HISTIGNORE */ |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 345 | |
| 346 | hist_last_line_pushed = 1; /* XXX */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 347 | free (s); |
| 348 | } |
| 349 | |
| 350 | #if defined (BANG_HISTORY) |
| 351 | static int |
| 352 | expand_and_print_history (list) |
| 353 | WORD_LIST *list; |
| 354 | { |
| 355 | char *s; |
| 356 | int r, result; |
| 357 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 358 | if (hist_last_line_pushed == 0 && hist_last_line_added && bash_delete_last_history () == 0) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 359 | return EXECUTION_FAILURE; |
| 360 | result = EXECUTION_SUCCESS; |
| 361 | while (list) |
| 362 | { |
| 363 | r = history_expand (list->word->word, &s); |
| 364 | if (r < 0) |
| 365 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 366 | builtin_error (_("%s: history expansion failed"), list->word->word); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 367 | result = EXECUTION_FAILURE; |
| 368 | } |
| 369 | else |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 370 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 371 | fputs (s, stdout); |
| 372 | putchar ('\n'); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 373 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 374 | FREE (s); |
| 375 | list = list->next; |
| 376 | } |
| 377 | fflush (stdout); |
| 378 | return result; |
| 379 | } |
| 380 | #endif /* BANG_HISTORY */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 381 | #endif /* HISTORY */ |