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 [n] [ [-awrn] [filename]]
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. Argument `-w' means to write out the current
31 history file; `-r' means to read it instead. Argument `-a' means
32 to append history lines from this session to the history file.
33 Argument `-n' means to read all history lines not already read
34 from the history file. If FILENAME is given, then use that file,
35 else if $HISTFILE has a value, use that, else use ~/.bash_history.
40 #include <sys/types.h>
42 #include "../filecntl.h"
43 #include "../posixstat.h"
44 #include "../bashhist.h"
45 #include <readline/history.h>
47 /* History. Arg of -w FILENAME means write file, arg of -r FILENAME
48 means read file. Arg of N means only display that many items. */
50 history_builtin (list)
54 int limited = 0, limit = 0;
59 char *arg = list->word->word;
61 if ((arg[0] == '-') &&
62 (strlen (arg) == 2) &&
63 (member (arg[1], "rwan")))
66 int result = EXECUTION_SUCCESS;
69 file = list->next->word->word;
71 file = get_string_value ("HISTFILE");
75 case 'a': /* Append `new' lines to file. */
77 if (history_lines_this_session)
79 void using_history ();
81 if (history_lines_this_session < where_history ())
83 /* If the filename was supplied, then create it
84 if it doesn't already exist. */
89 if (stat (file, &buf) == -1)
93 tem = open (file, O_CREAT, 0666);
99 append_history (history_lines_this_session, file);
100 history_lines_in_file += history_lines_this_session;
101 history_lines_this_session = 0;
107 case 'w': /* Write entire history. */
109 result = write_history (file);
113 case 'r': /* Read entire file. */
115 result = read_history (file);
119 case 'n': /* Read `new' history from file. */
121 /* Read all of the lines in the file that we haven't
124 result = read_history_range (file, history_lines_in_file, -1);
126 history_lines_in_file = where_history ();
131 return (result ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
133 else if (strcmp (list->word->word, "--") == 0)
138 else if (*list->word->word == '-')
140 bad_option (list->word->word);
141 builtin_error ("usage: history [n] [-rwan [filename]]");
151 limit = get_numeric_arg (list);
154 hlist = history_list ();
158 for (i = 0; hlist[i]; i++);
166 if ((i -= limit) < 0)
172 printf ("%5d%c %s\n", i + history_base,
173 hlist[i]->data ? '*' : ' ', hlist[i]->line);
177 return (EXECUTION_SUCCESS);