1 This file is history.def, from which is created history.c.
2 It implements the builtin "history" in Bash.
4 Copyright (C) 1987-2003 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
8 Bash is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License along
19 with Bash; see the file COPYING. If not, write to the Free Software
20 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
25 $FUNCTION history_builtin
27 $SHORT_DOC history [-c] [-d offset] [n] or history -awrn [filename] or history -ps arg [arg...]
28 Display the history list with line numbers. Lines listed with
29 with a `*' have been modified. Argument of N says to list only
30 the last N lines. The `-c' option causes the history list to be
31 cleared by deleting all of the entries. The `-d' option deletes
32 the history entry at offset OFFSET. The `-w' option writes out the
33 current history to the history file; `-r' means to read the file and
34 append the contents to the history list instead. `-a' means
35 to append history lines from this session to the history file.
36 Argument `-n' means to read all history lines not already read
37 from the history file and append them to the history list.
39 If FILENAME is given, then that is used as the history file else
40 if $HISTFILE has a value, that is used, else ~/.bash_history.
41 If the -s option is supplied, the non-option ARGs are appended to
42 the history list as a single entry. The -p option means to perform
43 history expansion on each ARG and display the result, without storing
44 anything in the history list.
46 If the $HISTTIMEFORMAT variable is set and not null, its value is used
47 as a format string for strftime(3) to print the time stamp associated
48 with each displayed history entry. No time stamps are printed otherwise.
54 #include "../bashtypes.h"
55 #if ! defined(_MINIX) && defined (HAVE_SYS_FILE_H)
56 # include <sys/file.h>
58 #include "posixstat.h"
62 #if defined (HAVE_UNISTD_H)
66 #include "../bashansi.h"
67 #include "../bashintl.h"
70 #include "../bashhist.h"
71 #include <readline/history.h>
72 #include "bashgetopt.h"
79 extern int current_command_line_count;
81 static char *histtime __P((HIST_ENTRY *, const char *));
82 static void display_history __P((WORD_LIST *));
83 static int delete_histent __P((int));
84 static int delete_last_history __P((void));
85 static void push_history __P((WORD_LIST *));
86 static int expand_and_print_history __P((WORD_LIST *));
98 history_builtin (list)
101 int flags, opt, result, old_history_lines, obase;
102 char *filename, *delete_arg;
103 intmax_t delete_offset;
106 reset_internal_getopt ();
107 while ((opt = internal_getopt (list, "acd:npsrw")) != -1)
131 delete_arg = list_optarg;
134 #if defined (BANG_HISTORY)
145 opt = flags & (AFLAG|RFLAG|WFLAG|NFLAG);
146 if (opt && opt != AFLAG && opt != RFLAG && opt != WFLAG && opt != NFLAG)
148 builtin_error (_("cannot use more than one of -anrw"));
149 return (EXECUTION_FAILURE);
152 /* clear the history, but allow other arguments to add to it again. */
157 return (EXECUTION_SUCCESS);
164 return (EXECUTION_SUCCESS);
166 #if defined (BANG_HISTORY)
167 else if (flags & PFLAG)
170 return (expand_and_print_history (list));
171 return (EXECUTION_SUCCESS);
174 else if (flags & DFLAG)
176 if ((legal_number (delete_arg, &delete_offset) == 0)
177 || (delete_offset < history_base)
178 || (delete_offset > (history_base + history_length)))
180 sh_erange (delete_arg, _("history position"));
181 return (EXECUTION_FAILURE);
184 result = delete_histent (opt - history_base);
185 /* Since remove_history changes history_length, this can happen if
186 we delete the last history entry. */
187 if (where_history () > history_length)
188 history_set_pos (history_length);
189 return (result ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
191 else if ((flags & (AFLAG|RFLAG|NFLAG|WFLAG|CFLAG)) == 0)
193 display_history (list);
194 return (EXECUTION_SUCCESS);
197 filename = list ? list->word->word : get_string_value ("HISTFILE");
198 result = EXECUTION_SUCCESS;
200 if (flags & AFLAG) /* Append session's history to file. */
201 result = maybe_append_history (filename);
202 else if (flags & WFLAG) /* Write entire history. */
203 result = write_history (filename);
204 else if (flags & RFLAG) /* Read entire file. */
205 result = read_history (filename);
206 else if (flags & NFLAG) /* Read `new' history from file. */
208 /* Read all of the lines in the file that we haven't already read. */
209 old_history_lines = history_lines_in_file;
210 obase = history_base;
213 result = read_history_range (filename, history_lines_in_file, -1);
216 history_lines_in_file = where_history ();
217 /* The question is whether we reset history_lines_this_session to 0,
218 losing any history entries we had before we read the new entries
219 from the history file, or whether we count the new entries we just
220 read from the file as history lines added during this session.
221 Right now, we do the latter. This will cause these history entries
222 to be written to the history file along with any intermediate entries
223 we add when we do a `history -a', but the alternative is losing
225 history_lines_this_session += history_lines_in_file - old_history_lines +
226 history_base - obase;
229 return (result ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
232 /* Accessors for HIST_ENTRY lists that are called HLIST. */
233 #define histline(i) (hlist[(i)]->line)
234 #define histdata(i) (hlist[(i)]->data)
237 histtime (hlist, histtimefmt)
239 const char *histtimefmt;
241 static char timestr[128];
244 t = history_get_time (hlist);
246 strftime (timestr, sizeof (timestr), histtimefmt, localtime (&t));
248 strcpy (timestr, "??");
253 display_history (list)
259 char *histtimefmt, *timestr;
263 limit = get_numeric_arg (list, 0);
270 hlist = history_list ();
274 for (i = 0; hlist[i]; i++)
277 if (0 <= limit && limit < i)
283 histtimefmt = get_string_value ("HISTTIMEFORMAT");
289 timestr = (histtimefmt && *histtimefmt) ? histtime (hlist[i], histtimefmt) : (char *)NULL;
290 printf ("%5d%c %s%s\n", i + history_base,
291 histdata(i) ? '*' : ' ',
292 ((timestr && *timestr) ? timestr : ""),
299 /* Delete and free the history list entry at offset I. */
306 discard = remove_history (i);
308 free_history_entry (discard);
314 delete_last_history ()
317 HIST_ENTRY **hlist, *histent;
320 hlist = history_list ();
324 for (i = 0; hlist[i]; i++)
328 /* History_get () takes a parameter that must be offset by history_base. */
329 histent = history_get (history_base + i); /* Don't free this */
333 r = delete_histent (i);
335 if (where_history () > history_length)
336 history_set_pos (history_length);
341 /* Remove the last entry in the history list and add each argument in
342 LIST to the history. */
349 /* Delete the last history entry if it was a single entry added to the
350 history list (generally the `history -s' itself), or if `history -s'
351 is being used in a compound command and the compound command was
352 added to the history as a single element (command-oriented history).
353 If you don't want history -s to remove the compound command from the
354 history, change #if 0 to #if 1 below. */
356 if (hist_last_line_added && delete_last_history () == 0)
358 if ((hist_last_line_added || (current_command_line_count > 0 && current_command_first_line_saved && command_oriented_history))
359 && delete_last_history () == 0)
363 s = string_list (list);
364 /* Call check_add_history with FORCE set to 1 to skip the check against
365 current_command_line_count. If history -s is used in a compound
366 command, the above code will delete the compound command's history
367 entry and this call will add the line to the history as a separate
368 entry. Without FORCE=1, if current_command_line_count were > 1, the
369 line would be appended to the entry before the just-deleted entry. */
370 check_add_history (s, 1); /* obeys HISTCONTROL, HISTIGNORE */
374 #if defined (BANG_HISTORY)
376 expand_and_print_history (list)
382 if (hist_last_line_added && delete_last_history () == 0)
383 return EXECUTION_FAILURE;
384 result = EXECUTION_SUCCESS;
387 r = history_expand (list->word->word, &s);
390 builtin_error (_("%s: history expansion failed"), list->word->word);
391 result = EXECUTION_FAILURE;
404 #endif /* BANG_HISTORY */