This file is fc.def, from which is created fc.c.
It implements the builtin "fc" in Bash.
-Copyright (C) 1987-2010 Free Software Foundation, Inc.
+Copyright (C) 1987-2011 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
#include <config.h>
#if defined (HISTORY)
-#ifndef _MINIX
+#if defined (HAVE_SYS_PARAM_H)
# include <sys/param.h>
#endif
#include "../bashtypes.h"
extern int errno;
#endif /* !errno */
-extern int current_command_line_count, saved_command_line_count;
+extern int current_command_line_count;
extern int literal_history;
extern int posixly_correct;
extern int subshell_environment, interactive_shell;
register int i;
register char *sep;
int numbering, reverse, listing, execute;
- int histbeg, histend, last_hist, retval, opt, rh;
+ int histbeg, histend, last_hist, retval, opt, rh, real_last;
FILE *stream;
REPL *rlist, *rl;
char *ename, *command, *newcom, *fcedit;
rh = remember_on_history || ((subshell_environment & SUBSHELL_COMSUB) && enable_history_list);
last_hist = i - rh - hist_last_line_added;
+ /* Make sure that real_last is calculated the same way here and in
+ fc_gethnum. The return value from fc_gethnum is treated specially if
+ it is == real_last and we are listing commands. */
+ real_last = i;
+ /* back up from the end to the last non-null history entry */
+ while (hlist[real_last] == 0 && real_last > 0)
+ real_last--;
+
/* XXX */
if (i == last_hist && hlist[last_hist] == 0)
while (last_hist >= 0 && hlist[last_hist] == 0)
if (list)
histend = fc_gethnum (list->word->word, hlist);
+ else if (histbeg == real_last)
+ histend = listing ? real_last : histbeg;
else
histend = listing ? last_hist : histbeg;
}
HIST_ENTRY **hlist;
{
int sign, n, clen, rh;
- register int i, j, last_hist;
+ register int i, j, last_hist, real_last;
register char *s;
sign = 1;
if (last_hist < 0)
return (-1);
+ real_last = i;
i = last_hist;
/* No specification defaults to most recent command. */
if (command == NULL)
return (i);
+ /* back up from the end to the last non-null history entry */
+ while (hlist[real_last] == 0 && real_last > 0)
+ real_last--;
+
/* Otherwise, there is a specification. It can be a number relative to
the current position, or an absolute history number. */
s = command;
return (n < 0 ? 0 : n);
}
else if (n == 0)
- return (i);
+ return ((sign == -1) ? real_last : i);
else
{
n -= history_base;