1 This file is history.def, from which is created history.c.
2 It implements the builtin "history" in Bash.
4 Copyright (C) 1987, 1989, 1991 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 1, 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, 675 Mass Ave, Cambridge, MA 02139, USA.
25 $FUNCTION history_builtin
27 $SHORT_DOC history [-c] [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 `-w' option writes out the
32 current history to the history file; `-r' means to read the file and
33 append the contents to the history list instead. `-a' means
34 to append history lines from this session to the history file.
35 Argument `-n' means to read all history lines not already read
36 from the history file and append them to the history list. If
37 FILENAME is given, then that is used as the history file else
38 if $HISTFILE has a value, that is used, else ~/.bash_history.
39 If the -s option is supplied, the non-option ARGs are appended to
40 the history list as a single entry. The -p option means to perform
41 history expansion on each ARG and display the result, without storing
42 anything in the history list.
48 #include "../bashtypes.h"
50 #include "../posixstat.h"
51 #include "../filecntl.h"
54 #if defined (HAVE_UNISTD_H)
58 #include "../bashansi.h"
61 #include "../bashhist.h"
62 #include <readline/history.h>
63 #include "bashgetopt.h"
70 static void display_history ();
71 static void push_history ();
72 static int expand_and_print_history ();
83 history_builtin (list)
86 int flags, opt, result;
90 reset_internal_getopt ();
91 while ((opt = internal_getopt (list, "acnpsrw")) != -1)
114 #if defined (BANG_HISTORY)
125 opt = flags & (AFLAG|RFLAG|WFLAG|NFLAG);
126 if (opt && opt != AFLAG && opt != RFLAG && opt != WFLAG && opt != NFLAG)
128 builtin_error ("cannot use more than one of -anrw");
129 return (EXECUTION_FAILURE);
132 /* clear the history, but allow other arguments to add to it again. */
137 return (EXECUTION_SUCCESS);
144 return (EXECUTION_SUCCESS);
146 #if defined (BANG_HISTORY)
147 else if (flags & PFLAG)
150 return (expand_and_print_history (list));
151 return (EXECUTION_SUCCESS);
154 else if ((flags & (AFLAG|RFLAG|NFLAG|WFLAG|CFLAG)) == 0)
156 display_history (list);
157 return (EXECUTION_SUCCESS);
160 filename = list ? list->word->word : get_string_value ("HISTFILE");
161 result = EXECUTION_SUCCESS;
163 if (flags & AFLAG) /* Append session's history to file. */
164 result = maybe_append_history (filename);
165 else if (flags & WFLAG) /* Write entire history. */
166 result = write_history (filename);
167 else if (flags & RFLAG) /* Read entire file. */
168 result = read_history (filename);
169 else if (flags & NFLAG) /* Read `new' history from file. */
171 /* Read all of the lines in the file that we haven't already read. */
173 result = read_history_range (filename, history_lines_in_file, -1);
175 history_lines_in_file = where_history ();
178 return (result ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
181 /* Accessors for HIST_ENTRY lists that are called HLIST. */
182 #define histline(i) (hlist[(i)]->line)
183 #define histdata(i) (hlist[(i)]->data)
186 display_history (list)
196 limit = get_numeric_arg (list, 0);
201 hlist = history_list ();
205 for (i = 0; hlist[i]; i++)
211 if ((limited == 0) || ((i -= limit) < 0))
217 printf ("%5d%c %s\n", i + history_base,
218 histdata(i) ? '*' : ' ',
226 delete_last_history ()
229 HIST_ENTRY **hlist, *histent, *discard;
231 hlist = history_list ();
235 for (i = 0; hlist[i]; i++)
239 /* History_get () takes a parameter that must be offset by history_base. */
240 histent = history_get (history_base + i); /* Don't free this */
244 discard = remove_history (i);
248 free (discard->line);
249 free ((char *) discard);
254 /* Remove the last entry in the history list and add each argument in
255 LIST to the history. */
262 if (hist_last_line_added && delete_last_history () == 0)
264 s = string_list (list);
265 maybe_add_history (s); /* Obeys HISTCONTROL setting. */
269 #if defined (BANG_HISTORY)
271 expand_and_print_history (list)
277 if (hist_last_line_added && delete_last_history () == 0)
278 return EXECUTION_FAILURE;
279 result = EXECUTION_SUCCESS;
282 r = history_expand (list->word->word, &s);
285 builtin_error ("%s: history expansion failed", list->word->word);
286 result = EXECUTION_FAILURE;
299 #endif /* BANG_HISTORY */