Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1 | This file is fc.def, from which is created fc.c. |
| 2 | It implements the builtin "fc" in Bash. |
| 3 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4 | Copyright (C) 1987-2011 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 fc.c |
| 22 | |
| 23 | $BUILTIN fc |
| 24 | $FUNCTION fc_builtin |
| 25 | $DEPENDS_ON HISTORY |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 26 | $SHORT_DOC fc [-e ename] [-lnr] [first] [last] or fc -s [pat=rep] [command] |
| 27 | Display or execute commands from the history list. |
| 28 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 29 | fc is used to list or edit and re-execute commands from the history list. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 30 | FIRST and LAST can be numbers specifying the range, or FIRST can be a |
| 31 | string, which means the most recent command beginning with that |
| 32 | string. |
| 33 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 34 | Options: |
| 35 | -e ENAME select which editor to use. Default is FCEDIT, then EDITOR, |
| 36 | then vi |
| 37 | -l list lines instead of editing |
| 38 | -n omit line numbers when listing |
| 39 | -r reverse the order of the lines (newest listed first) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 40 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 41 | With the `fc -s [pat=rep ...] [command]' format, COMMAND is |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 42 | re-executed after the substitution OLD=NEW is performed. |
| 43 | |
| 44 | A useful alias to use with this is r='fc -s', so that typing `r cc' |
| 45 | runs the last command beginning with `cc' and typing `r' re-executes |
| 46 | the last command. |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 47 | |
| 48 | Exit Status: |
| 49 | Returns success or status of executed command; non-zero if an error occurs. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 50 | $END |
| 51 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 52 | #include <config.h> |
| 53 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 54 | #if defined (HISTORY) |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 55 | #if defined (HAVE_SYS_PARAM_H) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 56 | # include <sys/param.h> |
| 57 | #endif |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 58 | #include "../bashtypes.h" |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 59 | #include "posixstat.h" |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 60 | #if ! defined(_MINIX) && defined (HAVE_SYS_FILE_H) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 61 | # include <sys/file.h> |
| 62 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 63 | |
| 64 | #if defined (HAVE_UNISTD_H) |
| 65 | # include <unistd.h> |
| 66 | #endif |
| 67 | |
| 68 | #include <stdio.h> |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 69 | #include <chartypes.h> |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 70 | |
| 71 | #include "../bashansi.h" |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 72 | #include "../bashintl.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 73 | #include <errno.h> |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 74 | |
| 75 | #include "../shell.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 76 | #include "../builtins.h" |
| 77 | #include "../flags.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 78 | #include "../bashhist.h" |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 79 | #include "maxpath.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 80 | #include <readline/history.h> |
| 81 | #include "bashgetopt.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 82 | #include "common.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 83 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 84 | #if !defined (errno) |
Ricardo Cerqueira | a02fbff | 2013-07-25 22:35:34 +0100 | [diff] [blame] | 85 | #include <errno.h> |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 86 | #endif /* !errno */ |
| 87 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 88 | extern int current_command_line_count; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 89 | extern int literal_history; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 90 | extern int posixly_correct; |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 91 | extern int subshell_environment, interactive_shell; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 92 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 93 | extern int unlink __P((const char *)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 94 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 95 | extern FILE *sh_mktmpfp __P((char *, int, char **)); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 96 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 97 | /* **************************************************************** */ |
| 98 | /* */ |
| 99 | /* The K*rn shell style fc command (Fix Command) */ |
| 100 | /* */ |
| 101 | /* **************************************************************** */ |
| 102 | |
| 103 | /* fc builtin command (fix command) for Bash for those who |
| 104 | like K*rn-style history better than csh-style. |
| 105 | |
| 106 | fc [-e ename] [-nlr] [first] [last] |
| 107 | |
| 108 | FIRST and LAST can be numbers specifying the range, or FIRST can be |
| 109 | a string, which means the most recent command beginning with that |
| 110 | string. |
| 111 | |
| 112 | -e ENAME selects which editor to use. Default is FCEDIT, then EDITOR, |
| 113 | then the editor which corresponds to the current readline editing |
| 114 | mode, then vi. |
| 115 | |
| 116 | -l means list lines instead of editing. |
| 117 | -n means no line numbers listed. |
| 118 | -r means reverse the order of the lines (making it newest listed first). |
| 119 | |
| 120 | fc -e - [pat=rep ...] [command] |
| 121 | fc -s [pat=rep ...] [command] |
| 122 | |
| 123 | Equivalent to !command:sg/pat/rep execpt there can be multiple PAT=REP's. |
| 124 | */ |
| 125 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 126 | /* Data structure describing a list of global replacements to perform. */ |
| 127 | typedef struct repl { |
| 128 | struct repl *next; |
| 129 | char *pat; |
| 130 | char *rep; |
| 131 | } REPL; |
| 132 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 133 | /* Accessors for HIST_ENTRY lists that are called HLIST. */ |
| 134 | #define histline(i) (hlist[(i)]->line) |
| 135 | #define histdata(i) (hlist[(i)]->data) |
| 136 | |
| 137 | #define FREE_RLIST() \ |
| 138 | do { \ |
| 139 | for (rl = rlist; rl; ) { \ |
| 140 | REPL *r; \ |
| 141 | r = rl->next; \ |
| 142 | if (rl->pat) \ |
| 143 | free (rl->pat); \ |
| 144 | if (rl->rep) \ |
| 145 | free (rl->rep); \ |
| 146 | free (rl); \ |
| 147 | rl = r; \ |
| 148 | } \ |
| 149 | } while (0) |
| 150 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 151 | static char *fc_dosubs __P((char *, REPL *)); |
| 152 | static char *fc_gethist __P((char *, HIST_ENTRY **)); |
| 153 | static int fc_gethnum __P((char *, HIST_ENTRY **)); |
| 154 | static int fc_number __P((WORD_LIST *)); |
| 155 | static void fc_replhist __P((char *)); |
| 156 | #ifdef INCLUDE_UNUSED |
| 157 | static char *fc_readline __P((FILE *)); |
| 158 | static void fc_addhist __P((char *)); |
| 159 | #endif |
| 160 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 161 | /* String to execute on a file that we want to edit. */ |
| 162 | #define FC_EDIT_COMMAND "${FCEDIT:-${EDITOR:-vi}}" |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 163 | #if defined (STRICT_POSIX) |
| 164 | # define POSIX_FC_EDIT_COMMAND "${FCEDIT:-ed}" |
| 165 | #else |
| 166 | # define POSIX_FC_EDIT_COMMAND "${FCEDIT:-${EDITOR:-ed}}" |
| 167 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 168 | |
| 169 | int |
| 170 | fc_builtin (list) |
| 171 | WORD_LIST *list; |
| 172 | { |
| 173 | register int i; |
| 174 | register char *sep; |
| 175 | int numbering, reverse, listing, execute; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 176 | int histbeg, histend, last_hist, retval, opt, rh, real_last; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 177 | FILE *stream; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 178 | REPL *rlist, *rl; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 179 | char *ename, *command, *newcom, *fcedit; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 180 | HIST_ENTRY **hlist; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 181 | char *fn; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 182 | |
| 183 | numbering = 1; |
| 184 | reverse = listing = execute = 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 185 | ename = (char *)NULL; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 186 | |
| 187 | /* Parse out the options and set which of the two forms we're in. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 188 | reset_internal_getopt (); |
| 189 | lcurrent = list; /* XXX */ |
| 190 | while (fc_number (loptend = lcurrent) == 0 && |
| 191 | (opt = internal_getopt (list, ":e:lnrs")) != -1) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 192 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 193 | switch (opt) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 194 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 195 | case 'n': |
| 196 | numbering = 0; |
| 197 | break; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 198 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 199 | case 'l': |
| 200 | listing = 1; |
| 201 | break; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 202 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 203 | case 'r': |
| 204 | reverse = 1; |
| 205 | break; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 206 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 207 | case 's': |
| 208 | execute = 1; |
| 209 | break; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 210 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 211 | case 'e': |
| 212 | ename = list_optarg; |
| 213 | break; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 214 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 215 | default: |
| 216 | builtin_usage (); |
| 217 | return (EX_USAGE); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 218 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 221 | list = loptend; |
| 222 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 223 | if (ename && (*ename == '-') && (ename[1] == '\0')) |
| 224 | execute = 1; |
| 225 | |
| 226 | /* The "execute" form of the command (re-run, with possible string |
| 227 | substitutions). */ |
| 228 | if (execute) |
| 229 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 230 | rlist = (REPL *)NULL; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 231 | while (list && ((sep = (char *)strchr (list->word->word, '=')) != NULL)) |
| 232 | { |
| 233 | *sep++ = '\0'; |
| 234 | rl = (REPL *)xmalloc (sizeof (REPL)); |
| 235 | rl->next = (REPL *)NULL; |
| 236 | rl->pat = savestring (list->word->word); |
| 237 | rl->rep = savestring (sep); |
| 238 | |
| 239 | if (rlist == NULL) |
| 240 | rlist = rl; |
| 241 | else |
| 242 | { |
| 243 | rl->next = rlist; |
| 244 | rlist = rl; |
| 245 | } |
| 246 | list = list->next; |
| 247 | } |
| 248 | |
| 249 | /* If we have a list of substitutions to do, then reverse it |
| 250 | to get the replacements in the proper order. */ |
| 251 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 252 | rlist = REVERSE_LIST (rlist, REPL *); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 253 | |
| 254 | hlist = history_list (); |
| 255 | |
| 256 | /* If we still have something in list, it is a command spec. |
| 257 | Otherwise, we use the most recent command in time. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 258 | command = fc_gethist (list ? list->word->word : (char *)NULL, hlist); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 259 | |
| 260 | if (command == NULL) |
| 261 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 262 | builtin_error (_("no command found")); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 263 | if (rlist) |
| 264 | FREE_RLIST (); |
| 265 | |
| 266 | return (EXECUTION_FAILURE); |
| 267 | } |
| 268 | |
| 269 | if (rlist) |
| 270 | { |
| 271 | newcom = fc_dosubs (command, rlist); |
| 272 | free (command); |
| 273 | FREE_RLIST (); |
| 274 | command = newcom; |
| 275 | } |
| 276 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 277 | fprintf (stderr, "%s\n", command); |
| 278 | fc_replhist (command); /* replace `fc -s' with command */ |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 279 | /* Posix says that the re-executed commands should be entered into the |
| 280 | history. */ |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 281 | return (parse_and_execute (command, "fc", SEVAL_NOHIST)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /* This is the second form of the command (the list-or-edit-and-rerun |
| 285 | form). */ |
| 286 | hlist = history_list (); |
| 287 | if (hlist == 0) |
| 288 | return (EXECUTION_SUCCESS); |
| 289 | for (i = 0; hlist[i]; i++); |
| 290 | |
| 291 | /* With the Bash implementation of history, the current command line |
| 292 | ("fc blah..." and so on) is already part of the history list by |
| 293 | the time we get to this point. This just skips over that command |
| 294 | and makes the last command that this deals with be the last command |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 295 | the user entered before the fc. We need to check whether the |
| 296 | line was actually added (HISTIGNORE may have caused it to not be), |
| 297 | so we check hist_last_line_added. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 298 | |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 299 | /* Even though command substitution through parse_and_execute turns off |
| 300 | remember_on_history, command substitution in a shell when set -o history |
| 301 | has been enabled (interactive or not) should use it in the last_hist |
| 302 | calculation as if it were on. */ |
| 303 | rh = remember_on_history || ((subshell_environment & SUBSHELL_COMSUB) && enable_history_list); |
| 304 | last_hist = i - rh - hist_last_line_added; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 305 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 306 | /* Make sure that real_last is calculated the same way here and in |
| 307 | fc_gethnum. The return value from fc_gethnum is treated specially if |
| 308 | it is == real_last and we are listing commands. */ |
| 309 | real_last = i; |
| 310 | /* back up from the end to the last non-null history entry */ |
| 311 | while (hlist[real_last] == 0 && real_last > 0) |
| 312 | real_last--; |
| 313 | |
Chet Ramey | 30d188c | 2011-11-21 20:57:16 -0500 | [diff] [blame] | 314 | /* XXX */ |
Chet Ramey | 7aaa661 | 2011-11-22 20:00:53 -0500 | [diff] [blame] | 315 | if (i == last_hist && hlist[last_hist] == 0) |
Chet Ramey | 30d188c | 2011-11-21 20:57:16 -0500 | [diff] [blame] | 316 | while (last_hist >= 0 && hlist[last_hist] == 0) |
| 317 | last_hist--; |
| 318 | if (last_hist < 0) |
| 319 | { |
| 320 | sh_erange ((char *)NULL, _("history specification")); |
| 321 | return (EXECUTION_FAILURE); |
| 322 | } |
| 323 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 324 | if (list) |
| 325 | { |
| 326 | histbeg = fc_gethnum (list->word->word, hlist); |
| 327 | list = list->next; |
| 328 | |
| 329 | if (list) |
| 330 | histend = fc_gethnum (list->word->word, hlist); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 331 | else if (histbeg == real_last) |
| 332 | histend = listing ? real_last : histbeg; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 333 | else |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 334 | histend = listing ? last_hist : histbeg; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 335 | } |
| 336 | else |
| 337 | { |
| 338 | /* The default for listing is the last 16 history items. */ |
| 339 | if (listing) |
| 340 | { |
| 341 | histend = last_hist; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 342 | histbeg = histend - 16 + 1; /* +1 because loop below uses >= */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 343 | if (histbeg < 0) |
| 344 | histbeg = 0; |
| 345 | } |
| 346 | else |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 347 | /* For editing, it is the last history command. */ |
| 348 | histbeg = histend = last_hist; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 351 | /* "When not listing, the fc command that caused the editing shall not be |
| 352 | entered into the history list." */ |
| 353 | if (listing == 0 && hist_last_line_added) |
| 354 | { |
| 355 | bash_delete_last_history (); |
| 356 | /* If we're editing a single command -- the last command in the |
| 357 | history -- and we just removed the dummy command added by |
| 358 | edit_and_execute_command (), we need to check whether or not we |
| 359 | just removed the last command in the history and need to back |
| 360 | the pointer up. remember_on_history is off because we're running |
| 361 | in parse_and_execute(). */ |
| 362 | if (histbeg == histend && histend == last_hist && hlist[last_hist] == 0) |
| 363 | last_hist = histbeg = --histend; |
| 364 | } |
| 365 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 366 | /* We print error messages for line specifications out of range. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 367 | if ((histbeg < 0) || (histend < 0)) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 368 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 369 | sh_erange ((char *)NULL, _("history specification")); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 370 | return (EXECUTION_FAILURE); |
| 371 | } |
| 372 | |
| 373 | if (histend < histbeg) |
| 374 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 375 | i = histend; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 376 | histend = histbeg; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 377 | histbeg = i; |
| 378 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 379 | reverse = 1; |
| 380 | } |
| 381 | |
| 382 | if (listing) |
| 383 | stream = stdout; |
| 384 | else |
| 385 | { |
| 386 | numbering = 0; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 387 | stream = sh_mktmpfp ("bash-fc", MT_USERANDOM|MT_USETMPDIR, &fn); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 388 | if (stream == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 389 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 390 | builtin_error (_("%s: cannot open temp file: %s"), fn ? fn : "", strerror (errno)); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 391 | FREE (fn); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 392 | return (EXECUTION_FAILURE); |
| 393 | } |
| 394 | } |
| 395 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 396 | for (i = reverse ? histend : histbeg; reverse ? i >= histbeg : i <= histend; reverse ? i-- : i++) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 397 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 398 | QUIT; |
| 399 | if (numbering) |
| 400 | fprintf (stream, "%d", i + history_base); |
| 401 | if (listing) |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 402 | { |
| 403 | if (posixly_correct) |
| 404 | fputs ("\t", stream); |
| 405 | else |
| 406 | fprintf (stream, "\t%c", histdata (i) ? '*' : ' '); |
| 407 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 408 | fprintf (stream, "%s\n", histline (i)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | if (listing) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 412 | return (sh_chkwrite (EXECUTION_SUCCESS)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 413 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 414 | fflush (stream); |
| 415 | if (ferror (stream)) |
| 416 | { |
| 417 | sh_wrerror (); |
| 418 | fclose (stream); |
| 419 | return (EXECUTION_FAILURE); |
| 420 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 421 | fclose (stream); |
| 422 | |
| 423 | /* Now edit the file of commands. */ |
| 424 | if (ename) |
| 425 | { |
| 426 | command = (char *)xmalloc (strlen (ename) + strlen (fn) + 2); |
| 427 | sprintf (command, "%s %s", ename, fn); |
| 428 | } |
| 429 | else |
| 430 | { |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 431 | fcedit = posixly_correct ? POSIX_FC_EDIT_COMMAND : FC_EDIT_COMMAND; |
| 432 | command = (char *)xmalloc (3 + strlen (fcedit) + strlen (fn)); |
| 433 | sprintf (command, "%s %s", fcedit, fn); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 434 | } |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 435 | retval = parse_and_execute (command, "fc", SEVAL_NOHIST); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 436 | if (retval != EXECUTION_SUCCESS) |
| 437 | { |
| 438 | unlink (fn); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 439 | free (fn); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 440 | return (EXECUTION_FAILURE); |
| 441 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 442 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 443 | /* Make sure parse_and_execute doesn't turn this off, even though a |
| 444 | call to parse_and_execute farther up the function call stack (e.g., |
| 445 | if this is called by vi_edit_and_execute_command) may have already |
| 446 | called bash_history_disable. */ |
| 447 | remember_on_history = 1; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 448 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 449 | /* Turn on the `v' flag while fc_execute_file runs so the commands |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 450 | will be echoed as they are read by the parser. */ |
| 451 | begin_unwind_frame ("fc builtin"); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 452 | add_unwind_protect ((Function *)xfree, fn); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 453 | add_unwind_protect (unlink, fn); |
| 454 | unwind_protect_int (echo_input_at_read); |
| 455 | echo_input_at_read = 1; |
| 456 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 457 | retval = fc_execute_file (fn); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 458 | |
| 459 | run_unwind_frame ("fc builtin"); |
| 460 | |
| 461 | return (retval); |
| 462 | } |
| 463 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 464 | /* Return 1 if LIST->word->word is a legal number for fc's use. */ |
| 465 | static int |
| 466 | fc_number (list) |
| 467 | WORD_LIST *list; |
| 468 | { |
| 469 | char *s; |
| 470 | |
| 471 | if (list == 0) |
| 472 | return 0; |
| 473 | s = list->word->word; |
| 474 | if (*s == '-') |
| 475 | s++; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 476 | return (legal_number (s, (intmax_t *)NULL)); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 479 | /* Return an absolute index into HLIST which corresponds to COMMAND. If |
| 480 | COMMAND is a number, then it was specified in relative terms. If it |
| 481 | is a string, then it is the start of a command line present in HLIST. */ |
| 482 | static int |
| 483 | fc_gethnum (command, hlist) |
| 484 | char *command; |
| 485 | HIST_ENTRY **hlist; |
| 486 | { |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 487 | int sign, n, clen, rh; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 488 | register int i, j, last_hist, real_last; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 489 | register char *s; |
| 490 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 491 | sign = 1; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 492 | /* Count history elements. */ |
| 493 | for (i = 0; hlist[i]; i++); |
| 494 | |
| 495 | /* With the Bash implementation of history, the current command line |
| 496 | ("fc blah..." and so on) is already part of the history list by |
| 497 | the time we get to this point. This just skips over that command |
| 498 | and makes the last command that this deals with be the last command |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 499 | the user entered before the fc. We need to check whether the |
| 500 | line was actually added (HISTIGNORE may have caused it to not be), |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 501 | so we check hist_last_line_added. This needs to agree with the |
| 502 | calculation of last_hist in fc_builtin above. */ |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 503 | /* Even though command substitution through parse_and_execute turns off |
| 504 | remember_on_history, command substitution in a shell when set -o history |
| 505 | has been enabled (interactive or not) should use it in the last_hist |
| 506 | calculation as if it were on. */ |
| 507 | rh = remember_on_history || ((subshell_environment & SUBSHELL_COMSUB) && enable_history_list); |
Chet Ramey | 7aaa661 | 2011-11-22 20:00:53 -0500 | [diff] [blame] | 508 | last_hist = i - rh - hist_last_line_added; |
| 509 | |
| 510 | if (i == last_hist && hlist[last_hist] == 0) |
| 511 | while (last_hist >= 0 && hlist[last_hist] == 0) |
| 512 | last_hist--; |
| 513 | if (last_hist < 0) |
| 514 | return (-1); |
| 515 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 516 | real_last = i; |
Chet Ramey | 7aaa661 | 2011-11-22 20:00:53 -0500 | [diff] [blame] | 517 | i = last_hist; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 518 | |
| 519 | /* No specification defaults to most recent command. */ |
| 520 | if (command == NULL) |
| 521 | return (i); |
| 522 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 523 | /* back up from the end to the last non-null history entry */ |
| 524 | while (hlist[real_last] == 0 && real_last > 0) |
| 525 | real_last--; |
| 526 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 527 | /* Otherwise, there is a specification. It can be a number relative to |
| 528 | the current position, or an absolute history number. */ |
| 529 | s = command; |
| 530 | |
| 531 | /* Handle possible leading minus sign. */ |
| 532 | if (s && (*s == '-')) |
| 533 | { |
| 534 | sign = -1; |
| 535 | s++; |
| 536 | } |
| 537 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 538 | if (s && DIGIT(*s)) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 539 | { |
| 540 | n = atoi (s); |
| 541 | n *= sign; |
| 542 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 543 | /* If the value is negative or zero, then it is an offset from |
| 544 | the current history item. */ |
| 545 | if (n < 0) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 546 | { |
| 547 | n += i + 1; |
| 548 | return (n < 0 ? 0 : n); |
| 549 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 550 | else if (n == 0) |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 551 | return ((sign == -1) ? real_last : i); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 552 | else |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 553 | { |
| 554 | n -= history_base; |
| 555 | return (i < n ? i : n); |
| 556 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | clen = strlen (command); |
| 560 | for (j = i; j >= 0; j--) |
| 561 | { |
| 562 | if (STREQN (command, histline (j), clen)) |
| 563 | return (j); |
| 564 | } |
| 565 | return (-1); |
| 566 | } |
| 567 | |
| 568 | /* Locate the most recent history line which begins with |
| 569 | COMMAND in HLIST, and return a malloc()'ed copy of it. */ |
| 570 | static char * |
| 571 | fc_gethist (command, hlist) |
| 572 | char *command; |
| 573 | HIST_ENTRY **hlist; |
| 574 | { |
| 575 | int i; |
| 576 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 577 | if (hlist == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 578 | return ((char *)NULL); |
| 579 | |
| 580 | i = fc_gethnum (command, hlist); |
| 581 | |
| 582 | if (i >= 0) |
| 583 | return (savestring (histline (i))); |
| 584 | else |
| 585 | return ((char *)NULL); |
| 586 | } |
| 587 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 588 | #ifdef INCLUDE_UNUSED |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 589 | /* Read the edited history lines from STREAM and return them |
| 590 | one at a time. This can read unlimited length lines. The |
| 591 | caller should free the storage. */ |
| 592 | static char * |
| 593 | fc_readline (stream) |
| 594 | FILE *stream; |
| 595 | { |
| 596 | register int c; |
| 597 | int line_len = 0, lindex = 0; |
| 598 | char *line = (char *)NULL; |
| 599 | |
| 600 | while ((c = getc (stream)) != EOF) |
| 601 | { |
| 602 | if ((lindex + 2) >= line_len) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 603 | line = (char *)xrealloc (line, (line_len += 128)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 604 | |
| 605 | if (c == '\n') |
| 606 | { |
| 607 | line[lindex++] = '\n'; |
| 608 | line[lindex++] = '\0'; |
| 609 | return (line); |
| 610 | } |
| 611 | else |
| 612 | line[lindex++] = c; |
| 613 | } |
| 614 | |
| 615 | if (!lindex) |
| 616 | { |
| 617 | if (line) |
| 618 | free (line); |
| 619 | |
| 620 | return ((char *)NULL); |
| 621 | } |
| 622 | |
| 623 | if (lindex + 2 >= line_len) |
| 624 | line = (char *)xrealloc (line, lindex + 3); |
| 625 | |
| 626 | line[lindex++] = '\n'; /* Finish with newline if none in file */ |
| 627 | line[lindex++] = '\0'; |
| 628 | return (line); |
| 629 | } |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 630 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 631 | |
| 632 | /* Perform the SUBS on COMMAND. |
| 633 | SUBS is a list of substitutions, and COMMAND is a simple string. |
| 634 | Return a pointer to a malloc'ed string which contains the substituted |
| 635 | command. */ |
| 636 | static char * |
| 637 | fc_dosubs (command, subs) |
| 638 | char *command; |
| 639 | REPL *subs; |
| 640 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 641 | register char *new, *t; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 642 | register REPL *r; |
| 643 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 644 | for (new = savestring (command), r = subs; r; r = r->next) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 645 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 646 | t = strsub (new, r->pat, r->rep, 1); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 647 | free (new); |
| 648 | new = t; |
| 649 | } |
| 650 | return (new); |
| 651 | } |
| 652 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 653 | /* Use `command' to replace the last entry in the history list, which, |
| 654 | by this time, is `fc blah...'. The intent is that the new command |
| 655 | become the history entry, and that `fc' should never appear in the |
| 656 | history list. This way you can do `r' to your heart's content. */ |
| 657 | static void |
| 658 | fc_replhist (command) |
| 659 | char *command; |
| 660 | { |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 661 | int n; |
| 662 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 663 | if (command == 0 || *command == '\0') |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 664 | return; |
| 665 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 666 | n = strlen (command); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 667 | if (command[n - 1] == '\n') |
| 668 | command[n - 1] = '\0'; |
| 669 | |
| 670 | if (command && *command) |
| 671 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 672 | bash_delete_last_history (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 673 | maybe_add_history (command); /* Obeys HISTCONTROL setting. */ |
| 674 | } |
| 675 | } |
| 676 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 677 | #ifdef INCLUDE_UNUSED |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 678 | /* Add LINE to the history, after removing a single trailing newline. */ |
| 679 | static void |
| 680 | fc_addhist (line) |
| 681 | char *line; |
| 682 | { |
| 683 | register int n; |
| 684 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 685 | if (line == 0 || *line == 0) |
| 686 | return; |
| 687 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 688 | n = strlen (line); |
| 689 | |
| 690 | if (line[n - 1] == '\n') |
| 691 | line[n - 1] = '\0'; |
| 692 | |
| 693 | if (line && *line) |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 694 | maybe_add_history (line); /* Obeys HISTCONTROL setting. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 695 | } |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 696 | #endif |
| 697 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 698 | #endif /* HISTORY */ |