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;
80 extern int force_append_history; /* shopt -s histappend */
82 int delete_last_history __P((void));
84 static char *histtime __P((HIST_ENTRY *, const char *));
85 static void display_history __P((WORD_LIST *));
86 static int delete_histent __P((int));
87 static void push_history __P((WORD_LIST *));
88 static int expand_and_print_history __P((WORD_LIST *));
100 history_builtin (list)
103 int flags, opt, result, old_history_lines, obase;
104 char *filename, *delete_arg;
105 intmax_t delete_offset;
108 reset_internal_getopt ();
109 while ((opt = internal_getopt (list, "acd:npsrw")) != -1)
133 delete_arg = list_optarg;
136 #if defined (BANG_HISTORY)
147 opt = flags & (AFLAG|RFLAG|WFLAG|NFLAG);
148 if (opt && opt != AFLAG && opt != RFLAG && opt != WFLAG && opt != NFLAG)
150 builtin_error (_("cannot use more than one of -anrw"));
151 return (EXECUTION_FAILURE);
154 /* clear the history, but allow other arguments to add to it again. */
159 return (EXECUTION_SUCCESS);
166 return (EXECUTION_SUCCESS);
168 #if defined (BANG_HISTORY)
169 else if (flags & PFLAG)
172 return (expand_and_print_history (list));
173 return (EXECUTION_SUCCESS);
176 else if (flags & DFLAG)
178 if ((legal_number (delete_arg, &delete_offset) == 0)
179 || (delete_offset < history_base)
180 || (delete_offset > (history_base + history_length)))
182 sh_erange (delete_arg, _("history position"));
183 return (EXECUTION_FAILURE);
186 result = delete_histent (opt - history_base);
187 /* Since remove_history changes history_length, this can happen if
188 we delete the last history entry. */
189 if (where_history () > history_length)
190 history_set_pos (history_length);
191 return (result ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
193 else if ((flags & (AFLAG|RFLAG|NFLAG|WFLAG|CFLAG)) == 0)
195 display_history (list);
196 return (EXECUTION_SUCCESS);
199 filename = list ? list->word->word : get_string_value ("HISTFILE");
200 result = EXECUTION_SUCCESS;
202 if (flags & AFLAG) /* Append session's history to file.
*/
203 result
= maybe_append_history (filename
);
204 else
if (flags
& WFLAG
) /* Write entire history.
*/
205 result
= write_history (filename
);
206 else
if (flags
& RFLAG
) /* Read entire file.
*/
207 result
= read_history (filename
);
208 else
if (flags
& NFLAG
) /* Read `new
' history from file. */
210 /* Read all of the lines in the file that we haven't already read.
*/
211 old_history_lines
= history_lines_in_file
;
212 obase
= history_base
;
215 result
= read_history_range (filename
, history_lines_in_file
, -1);
218 history_lines_in_file
= where_history ();
220 /* If we
're rewriting the history file at shell exit rather than just
221 appending the lines from this session to it, the question is whether
222 we reset history_lines_this_session to 0, losing any history entries
223 we had before we read the new entries from the history file, or
224 whether we count the new entries we just read from the file as
225 history lines added during this session.
226 Right now, we do the latter. This will cause these history entries
227 to be written to the history file along with any intermediate entries
228 we add when we do a `history -a', but the alternative is losing
230 if (force_append_history
== 0)
231 history_lines_this_session
+= history_lines_in_file
- old_history_lines
+
232 history_base
- obase
;
235 return (result ? EXECUTION_FAILURE
: EXECUTION_SUCCESS
);
238 /* Accessors for HIST_ENTRY lists that are called HLIST.
*/
239 #define
histline(i
) (hlist
[(i
)]->line
)
240 #define
histdata(i
) (hlist
[(i
)]->data
)
243 histtime (hlist
, histtimefmt
)
245 const char
*histtimefmt
;
247 static char timestr
[128];
250 t
= history_get_time (hlist
);
252 strftime (timestr
, sizeof (timestr
), histtimefmt
, localtime (&t
));
254 strcpy (timestr
, "??");
259 display_history (list
)
265 char
*histtimefmt
, *timestr
;
269 limit
= get_numeric_arg (list
, 0);
276 hlist
= history_list ();
280 for (i
= 0; hlist
[i
]; i
++)
283 if (0 <= limit
&& limit
< i
)
289 histtimefmt
= get_string_value ("HISTTIMEFORMAT");
295 timestr
= (histtimefmt
&& *histtimefmt
) ?
histtime (hlist
[i
], histtimefmt
) : (char *)NULL
;
296 printf ("%5d%c %s%s\n", i
+ history_base
,
297 histdata(i
) ?
'*' : ' ',
298 ((timestr
&& *timestr
) ? timestr
: ""),
305 /* Delete and free the history list entry at offset I.
*/
312 discard
= remove_history (i
);
314 free_history_entry (discard
);
320 delete_last_history ()
323 HIST_ENTRY
**hlist
, *histent
;
326 hlist
= history_list ();
330 for (i
= 0; hlist
[i
]; i
++)
334 /* History_get () takes a parameter that must be offset by history_base.
*/
335 histent
= history_get (history_base
+ i
); /* Don
't free this */
339 r = delete_histent (i);
341 if (where_history () > history_length)
342 history_set_pos (history_length);
347 /* Remove the last entry in the history list and add each argument in
348 LIST to the history. */
355 /* Delete the last history entry if it was a single entry added to the
356 history list (generally the `history -s' itself
), or if `history
-s
'
357 is being used in a compound command and the compound command was
358 added to the history as a single element (command-oriented history).
359 If you don't want history
-s to remove the compound command from the
360 history
, change #if
0 to #if
1 below.
*/
362 if (hist_last_line_pushed
== 0 && hist_last_line_added
&& delete_last_history () == 0)
364 if (hist_last_line_pushed
== 0 &&
365 (hist_last_line_added ||
366 (current_command_line_count
> 0 && current_command_first_line_saved
&& command_oriented_history
))
367 && delete_last_history () == 0)
371 s
= string_list (list
);
372 /* Call check_add_history with FORCE set to
1 to skip the check against
373 current_command_line_count. If history
-s is used in a compound
374 command
, the above code will delete the compound command
's history
375 entry and this call will add the line to the history as a separate
376 entry. Without FORCE=1, if current_command_line_count were > 1, the
377 line would be appended to the entry before the just-deleted entry. */
378 check_add_history (s, 1); /* obeys HISTCONTROL, HISTIGNORE */
380 hist_last_line_pushed = 1; /* XXX */
384 #if defined (BANG_HISTORY)
386 expand_and_print_history (list)
392 if (hist_last_line_pushed == 0 && hist_last_line_added && delete_last_history () == 0)
393 return EXECUTION_FAILURE;
394 result = EXECUTION_SUCCESS;
397 r = history_expand (list->word->word, &s);
400 builtin_error (_("%s: history expansion failed"), list->word->word);
401 result = EXECUTION_FAILURE;
414 #endif /* BANG_HISTORY */