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 <sys/file.h>
52 #include "../posixstat.h"
53 #include "../filecntl.h"
56 #if defined (HAVE_UNISTD_H)
60 #include "../bashansi.h"
63 #include "../bashhist.h"
64 #include <readline/history.h>
65 #include "bashgetopt.h"
72 static void display_history ();
73 static void push_history ();
74 static int expand_and_print_history ();
85 history_builtin (list)
88 int flags, opt, result;
92 reset_internal_getopt ();
93 while ((opt = internal_getopt (list, "acnpsrw")) != -1)
116 #if defined (BANG_HISTORY)
127 opt = flags & (AFLAG|RFLAG|WFLAG|NFLAG);
128 if (opt && opt != AFLAG && opt != RFLAG && opt != WFLAG && opt != NFLAG)
130 builtin_error ("cannot use more than one of -anrw");
131 return (EXECUTION_FAILURE);
134 /* clear the history, but allow other arguments to add to it again. */
139 return (EXECUTION_SUCCESS);
146 return (EXECUTION_SUCCESS);
148 #if defined (BANG_HISTORY)
149 else if (flags & PFLAG)
152 return (expand_and_print_history (list));
153 return (EXECUTION_SUCCESS);
156 else if ((flags & (AFLAG|RFLAG|NFLAG|WFLAG|CFLAG)) == 0)
158 display_history (list);
159 return (EXECUTION_SUCCESS);
162 filename = list ? list->word->word : get_string_value ("HISTFILE");
163 result = EXECUTION_SUCCESS;
165 if (flags & AFLAG) /* Append session's history to file. */
166 result = maybe_append_history (filename);
167 else if (flags & WFLAG) /* Write entire history. */
168 result = write_history (filename);
169 else if (flags & RFLAG) /* Read entire file. */
170 result = read_history (filename);
171 else if (flags & NFLAG) /* Read `new' history from file. */
173 /* Read all of the lines in the file that we haven't already read. */
175 result = read_history_range (filename, history_lines_in_file, -1);
177 history_lines_in_file = where_history ();
180 return (result ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
183 /* Accessors for HIST_ENTRY lists that are called HLIST. */
184 #define histline(i) (hlist[(i)]->line)
185 #define histdata(i) (hlist[(i)]->data)
188 display_history (list)
198 limit = get_numeric_arg (list, 0);
203 hlist = history_list ();
207 for (i = 0; hlist[i]; i++)
213 if ((limited == 0) || ((i -= limit) < 0))
219 printf ("%5d%c %s\n", i + history_base,
220 histdata(i) ? '*' : ' ',
228 delete_last_history ()
231 HIST_ENTRY **hlist, *histent, *discard;
233 hlist = history_list ();
237 for (i = 0; hlist[i]; i++)
241 /* History_get () takes a parameter that must be offset by history_base. */
242 histent = history_get (history_base + i); /* Don't free this */
246 discard = remove_history (i);
250 free (discard->line);
251 free ((char *) discard);
256 /* Remove the last entry in the history list and add each argument in
257 LIST to the history. */
264 if (hist_last_line_added && delete_last_history () == 0)
266 s = string_list (list);
267 maybe_add_history (s); /* Obeys HISTCONTROL setting. */
271 #if defined (BANG_HISTORY)
273 expand_and_print_history (list)
279 if (hist_last_line_added && delete_last_history () == 0)
280 return EXECUTION_FAILURE;
281 result = EXECUTION_SUCCESS;
284 r = history_expand (list->word->word, &s);
287 builtin_error ("%s: history expansion failed", list->word->word);
288 result = EXECUTION_FAILURE;
301 #endif /* BANG_HISTORY */