i-bash/builtins/history.def

341 lines
8 KiB
Modula-2
Raw Normal View History

1996-08-26 18:22:31 +00:00
This file is history.def, from which is created history.c.
It implements the builtin "history" in Bash.
Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
Bash is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
2000-03-17 21:46:59 +00:00
Software Foundation; either version 2, or (at your option) any later
1996-08-26 18:22:31 +00:00
version.
Bash is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License along
with Bash; see the file COPYING. If not, write to the Free Software
2000-03-17 21:46:59 +00:00
Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
1996-08-26 18:22:31 +00:00
$PRODUCES history.c
$BUILTIN history
$FUNCTION history_builtin
$DEPENDS_ON HISTORY
2000-03-17 21:46:59 +00:00
$SHORT_DOC history [-c] [-d offset] [n] or history -awrn [filename] or history -ps arg [arg...]
1996-08-26 18:22:31 +00:00
Display the history list with line numbers. Lines listed with
with a `*' have been modified. Argument of N says to list only
2000-03-17 21:46:59 +00:00
the last N lines. The `-c' option causes the history list to be
cleared by deleting all of the entries. The `-d' option deletes
the history entry at offset OFFSET. The `-w' option writes out the
1996-12-23 17:02:34 +00:00
current history to the history file; `-r' means to read the file and
append the contents to the history list instead. `-a' means
1996-08-26 18:22:31 +00:00
to append history lines from this session to the history file.
Argument `-n' means to read all history lines not already read
1996-12-23 17:02:34 +00:00
from the history file and append them to the history list. If
FILENAME is given, then that is used as the history file else
if $HISTFILE has a value, that is used, else ~/.bash_history.
If the -s option is supplied, the non-option ARGs are appended to
the history list as a single entry. The -p option means to perform
history expansion on each ARG and display the result, without storing
anything in the history list.
1996-08-26 18:22:31 +00:00
$END
1996-12-23 17:02:34 +00:00
#include <config.h>
1996-08-26 18:22:31 +00:00
#if defined (HISTORY)
1997-06-05 14:59:13 +00:00
#include "../bashtypes.h"
1998-04-17 19:52:44 +00:00
#ifndef _MINIX
# include <sys/file.h>
#endif
2000-03-17 21:46:59 +00:00
#include "posixstat.h"
#include "filecntl.h"
1996-12-23 17:02:34 +00:00
#include <errno.h>
#include <stdio.h>
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
#include "../bashansi.h"
#include "../shell.h"
1996-08-26 18:22:31 +00:00
#include "../bashhist.h"
#include <readline/history.h>
1996-12-23 17:02:34 +00:00
#include "bashgetopt.h"
#include "common.h"
#if !defined (errno)
extern int errno;
#endif
1996-08-26 18:22:31 +00:00
1996-12-23 17:02:34 +00:00
static void display_history ();
static void push_history ();
static int expand_and_print_history ();
2000-03-17 21:46:59 +00:00
static int delete_histent ();
1996-08-26 18:22:31 +00:00
1996-12-23 17:02:34 +00:00
#define AFLAG 0x01
#define RFLAG 0x02
#define WFLAG 0x04
#define NFLAG 0x08
#define SFLAG 0x10
#define PFLAG 0x20
#define CFLAG 0x40
2000-03-17 21:46:59 +00:00
#define DFLAG 0x80
1996-12-23 17:02:34 +00:00
int
1996-08-26 18:22:31 +00:00
history_builtin (list)
WORD_LIST *list;
{
1996-12-23 17:02:34 +00:00
int flags, opt, result;
2000-03-17 21:46:59 +00:00
char *filename, *delete_arg;
long delete_offset;
1996-08-26 18:22:31 +00:00
1996-12-23 17:02:34 +00:00
flags = 0;
reset_internal_getopt ();
2000-03-17 21:46:59 +00:00
while ((opt = internal_getopt (list, "acd:npsrw")) != -1)
1996-08-26 18:22:31 +00:00
{
1996-12-23 17:02:34 +00:00
switch (opt)
1996-08-26 18:22:31 +00:00
{
1996-12-23 17:02:34 +00:00
case 'a':
flags |= AFLAG;
1996-08-26 18:22:31 +00:00
break;
1996-12-23 17:02:34 +00:00
case 'c':
flags |= CFLAG;
break;
case 'n':
flags |= NFLAG;
break;
case 'r':
flags |= RFLAG;
break;
case 'w':
flags |= WFLAG;
break;
case 's':
flags |= SFLAG;
break;
2000-03-17 21:46:59 +00:00
case 'd':
flags |= DFLAG;
delete_arg = list_optarg;
break;
1996-12-23 17:02:34 +00:00
case 'p':
#if defined (BANG_HISTORY)
flags |= PFLAG;
#endif
break;
default:
builtin_usage ();
1996-08-26 18:22:31 +00:00
return (EX_USAGE);
}
1996-12-23 17:02:34 +00:00
}
list = loptend;
opt = flags & (AFLAG|RFLAG|WFLAG|NFLAG);
if (opt && opt != AFLAG && opt != RFLAG && opt != WFLAG && opt != NFLAG)
{
builtin_error ("cannot use more than one of -anrw");
return (EXECUTION_FAILURE);
}
/* clear the history, but allow other arguments to add to it again. */
if (flags & CFLAG)
{
clear_history ();
if (list == 0)
return (EXECUTION_SUCCESS);
1996-08-26 18:22:31 +00:00
}
1996-12-23 17:02:34 +00:00
if (flags & SFLAG)
{
if (list)
push_history (list);
return (EXECUTION_SUCCESS);
}
#if defined (BANG_HISTORY)
else if (flags & PFLAG)
{
if (list)
return (expand_and_print_history (list));
return (EXECUTION_SUCCESS);
}
#endif
2000-03-17 21:46:59 +00:00
else if (flags & DFLAG)
{
if (legal_number (delete_arg, &delete_offset) == 0)
{
builtin_error ("%s: not a valid history position", delete_arg);
return (EXECUTION_FAILURE);
}
opt = delete_offset;
if (opt < history_base || opt < 0 || opt > (history_base + history_length))
{
builtin_error ("%d: not a valid history position", opt);
return (EXECUTION_FAILURE);
}
result = delete_histent (opt - history_base);
/* Since remove_history changes history_length, this can happen if
we delete the last history entry. */
if (where_history () > history_length)
history_set_pos (history_length);
return (result ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
}
1996-12-23 17:02:34 +00:00
else if ((flags & (AFLAG|RFLAG|NFLAG|WFLAG|CFLAG)) == 0)
{
display_history (list);
return (EXECUTION_SUCCESS);
}
filename = list ? list->word->word : get_string_value ("HISTFILE");
result = EXECUTION_SUCCESS;
if (flags & AFLAG) /* Append session's history to file. */
result = maybe_append_history (filename);
else if (flags & WFLAG) /* Write entire history. */
result = write_history (filename);
else if (flags & RFLAG) /* Read entire file. */
result = read_history (filename);
else if (flags & NFLAG) /* Read `new' history from file. */
{
/* Read all of the lines in the file that we haven't already read. */
using_history ();
result = read_history_range (filename, history_lines_in_file, -1);
using_history ();
history_lines_in_file = where_history ();
}
return (result ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
}
/* Accessors for HIST_ENTRY lists that are called HLIST. */
#define histline(i) (hlist[(i)]->line)
#define histdata(i) (hlist[(i)]->data)
static void
display_history (list)
WORD_LIST *list;
{
register int i;
int limited, limit;
HIST_ENTRY **hlist;
1996-08-26 18:22:31 +00:00
if (list)
{
limited = 1;
1997-06-05 14:59:13 +00:00
limit = get_numeric_arg (list, 0);
1996-08-26 18:22:31 +00:00
}
1996-12-23 17:02:34 +00:00
else
limited = limit = 0;
1996-08-26 18:22:31 +00:00
hlist = history_list ();
if (hlist)
{
1996-12-23 17:02:34 +00:00
for (i = 0; hlist[i]; i++)
;
1996-08-26 18:22:31 +00:00
if (limit < 0)
limit = -limit;
1996-12-23 17:02:34 +00:00
if ((limited == 0) || ((i -= limit) < 0))
1996-08-26 18:22:31 +00:00
i = 0;
while (hlist[i])
{
QUIT;
printf ("%5d%c %s\n", i + history_base,
1996-12-23 17:02:34 +00:00
histdata(i) ? '*' : ' ',
histline(i));
1996-08-26 18:22:31 +00:00
i++;
}
}
}
1996-12-23 17:02:34 +00:00
2000-03-17 21:46:59 +00:00
/* Delete and free the history list entry at offset I. */
static int
delete_histent (i)
int i;
{
HIST_ENTRY *discard;
discard = remove_history (i);
if (discard)
{
if (discard->line)
free (discard->line);
free ((char *) discard);
}
return 1;
}
1996-12-23 17:02:34 +00:00
static int
delete_last_history ()
{
register int i;
2000-03-17 21:46:59 +00:00
HIST_ENTRY **hlist, *histent;
1996-12-23 17:02:34 +00:00
hlist = history_list ();
if (hlist == NULL)
return 0;
for (i = 0; hlist[i]; i++)
;
i--;
/* History_get () takes a parameter that must be offset by history_base. */
histent = history_get (history_base + i); /* Don't free this */
if (histent == NULL)
return 0;
2000-03-17 21:46:59 +00:00
return (delete_histent (i));
1996-12-23 17:02:34 +00:00
}
/* Remove the last entry in the history list and add each argument in
LIST to the history. */
static void
push_history (list)
WORD_LIST *list;
{
char *s;
1997-06-05 14:59:13 +00:00
if (hist_last_line_added && delete_last_history () == 0)
1996-12-23 17:02:34 +00:00
return;
s = string_list (list);
maybe_add_history (s); /* Obeys HISTCONTROL setting. */
free (s);
}
#if defined (BANG_HISTORY)
static int
expand_and_print_history (list)
WORD_LIST *list;
{
char *s;
int r, result;
1997-06-05 14:59:13 +00:00
if (hist_last_line_added && delete_last_history () == 0)
1996-12-23 17:02:34 +00:00
return EXECUTION_FAILURE;
result = EXECUTION_SUCCESS;
while (list)
{
r = history_expand (list->word->word, &s);
if (r < 0)
{
builtin_error ("%s: history expansion failed", list->word->word);
result = EXECUTION_FAILURE;
}
else
{
fputs (s, stdout);
putchar ('\n');
}
FREE (s);
list = list->next;
}
fflush (stdout);
return result;
}
#endif /* BANG_HISTORY */
1996-08-26 18:22:31 +00:00
#endif /* HISTORY */