1994-05-26 06:18:55 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Kenneth Almquist.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1998-05-18 06:44:24 +00:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)histedit.c 8.2 (Berkeley) 5/4/95";
|
|
|
|
#endif
|
1994-05-26 06:18:55 +00:00
|
|
|
#endif /* not lint */
|
2002-06-30 05:15:05 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
2001-05-30 03:37:28 +00:00
|
|
|
#include <limits.h>
|
1994-05-26 06:18:55 +00:00
|
|
|
#include <paths.h>
|
|
|
|
#include <stdio.h>
|
1996-09-01 10:22:36 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
/*
|
|
|
|
* Editline and history functions (and glue).
|
|
|
|
*/
|
1994-05-26 06:18:55 +00:00
|
|
|
#include "shell.h"
|
|
|
|
#include "parser.h"
|
|
|
|
#include "var.h"
|
|
|
|
#include "options.h"
|
1996-09-01 10:22:36 +00:00
|
|
|
#include "main.h"
|
|
|
|
#include "output.h"
|
1994-05-26 06:18:55 +00:00
|
|
|
#include "mystring.h"
|
1996-09-01 10:22:36 +00:00
|
|
|
#ifndef NO_HISTORY
|
|
|
|
#include "myhistedit.h"
|
1994-05-26 06:18:55 +00:00
|
|
|
#include "error.h"
|
1996-09-01 10:22:36 +00:00
|
|
|
#include "eval.h"
|
1994-05-26 06:18:55 +00:00
|
|
|
#include "memalloc.h"
|
|
|
|
|
|
|
|
#define MAXHISTLOOPS 4 /* max recursions through fc */
|
|
|
|
#define DEFEDITOR "ed" /* default editor *should* be $EDITOR */
|
|
|
|
|
|
|
|
History *hist; /* history cookie */
|
|
|
|
EditLine *el; /* editline cookie */
|
|
|
|
int displayhist;
|
2001-10-01 08:43:58 +00:00
|
|
|
static FILE *el_in, *el_out, *el_err;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static char *fc_replace(const char *, char *, char *);
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set history and editing status. Called whenever the status may
|
|
|
|
* have changed (figures out what to do).
|
|
|
|
*/
|
1996-09-01 10:22:36 +00:00
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
histedit(void)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
#define editing (Eflag || Vflag)
|
|
|
|
|
|
|
|
if (iflag) {
|
|
|
|
if (!hist) {
|
|
|
|
/*
|
|
|
|
* turn history on
|
|
|
|
*/
|
|
|
|
INTOFF;
|
|
|
|
hist = history_init();
|
|
|
|
INTON;
|
|
|
|
|
|
|
|
if (hist != NULL)
|
1996-12-14 06:20:03 +00:00
|
|
|
sethistsize(histsizeval());
|
1994-05-26 06:18:55 +00:00
|
|
|
else
|
2009-11-21 14:28:32 +00:00
|
|
|
out2fmt_flush("sh: can't initialize history\n");
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
if (editing && !el && isatty(0)) { /* && isatty(2) ??? */
|
|
|
|
/*
|
|
|
|
* turn editing on
|
|
|
|
*/
|
2010-06-02 19:16:58 +00:00
|
|
|
char *term;
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
INTOFF;
|
|
|
|
if (el_in == NULL)
|
|
|
|
el_in = fdopen(0, "r");
|
2001-10-01 08:43:58 +00:00
|
|
|
if (el_err == NULL)
|
|
|
|
el_err = fdopen(1, "w");
|
1994-05-26 06:18:55 +00:00
|
|
|
if (el_out == NULL)
|
|
|
|
el_out = fdopen(2, "w");
|
2001-10-01 08:43:58 +00:00
|
|
|
if (el_in == NULL || el_err == NULL || el_out == NULL)
|
1994-05-26 06:18:55 +00:00
|
|
|
goto bad;
|
2010-06-02 19:16:58 +00:00
|
|
|
term = lookupvar("TERM");
|
|
|
|
if (term)
|
|
|
|
setenv("TERM", term, 1);
|
|
|
|
else
|
|
|
|
unsetenv("TERM");
|
2001-10-01 08:43:58 +00:00
|
|
|
el = el_init(arg0, el_in, el_out, el_err);
|
1994-05-26 06:18:55 +00:00
|
|
|
if (el != NULL) {
|
|
|
|
if (hist)
|
|
|
|
el_set(el, EL_HIST, history, hist);
|
|
|
|
el_set(el, EL_PROMPT, getprompt);
|
2010-06-15 21:58:40 +00:00
|
|
|
el_set(el, EL_ADDFN, "sh-complete",
|
|
|
|
"Filename completion",
|
|
|
|
_el_fn_sh_complete);
|
1994-05-26 06:18:55 +00:00
|
|
|
} else {
|
|
|
|
bad:
|
2009-11-21 14:28:32 +00:00
|
|
|
out2fmt_flush("sh: can't initialize editing\n");
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
INTON;
|
|
|
|
} else if (!editing && el) {
|
|
|
|
INTOFF;
|
|
|
|
el_end(el);
|
|
|
|
el = NULL;
|
|
|
|
INTON;
|
|
|
|
}
|
|
|
|
if (el) {
|
|
|
|
if (Vflag)
|
|
|
|
el_set(el, EL_EDITOR, "vi");
|
|
|
|
else if (Eflag)
|
|
|
|
el_set(el, EL_EDITOR, "emacs");
|
2010-06-15 21:58:40 +00:00
|
|
|
el_set(el, EL_BIND, "^I", "sh-complete", NULL);
|
2002-07-23 12:26:34 +00:00
|
|
|
el_source(el, NULL);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
INTOFF;
|
|
|
|
if (el) { /* no editing if not interactive */
|
|
|
|
el_end(el);
|
|
|
|
el = NULL;
|
|
|
|
}
|
|
|
|
if (hist) {
|
|
|
|
history_end(hist);
|
|
|
|
hist = NULL;
|
|
|
|
}
|
|
|
|
INTON;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
|
|
|
|
void
|
1996-12-14 06:20:03 +00:00
|
|
|
sethistsize(hs)
|
|
|
|
const char *hs;
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
int histsize;
|
2001-10-01 08:43:58 +00:00
|
|
|
HistEvent he;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
if (hist != NULL) {
|
1996-12-14 06:20:03 +00:00
|
|
|
if (hs == NULL || *hs == '\0' ||
|
|
|
|
(histsize = atoi(hs)) < 0)
|
1994-05-26 06:18:55 +00:00
|
|
|
histsize = 100;
|
2005-10-19 15:37:43 +00:00
|
|
|
history(hist, &he, H_SETSIZE, histsize);
|
2010-08-01 16:37:51 +00:00
|
|
|
history(hist, &he, H_SETUNIQUE, 1);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-02 19:16:58 +00:00
|
|
|
void
|
|
|
|
setterm(const char *term)
|
|
|
|
{
|
|
|
|
if (rootshell && el != NULL && term != NULL)
|
|
|
|
el_set(el, EL_TERMINAL, term);
|
|
|
|
}
|
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
int
|
2002-02-02 06:50:57 +00:00
|
|
|
histcmd(int argc, char **argv)
|
1994-05-26 06:18:55 +00:00
|
|
|
{
|
|
|
|
int ch;
|
2009-12-27 18:04:05 +00:00
|
|
|
const char *editor = NULL;
|
2001-10-01 08:43:58 +00:00
|
|
|
HistEvent he;
|
1994-05-26 06:18:55 +00:00
|
|
|
int lflg = 0, nflg = 0, rflg = 0, sflg = 0;
|
2001-10-01 08:43:58 +00:00
|
|
|
int i, retval;
|
2009-12-27 18:04:05 +00:00
|
|
|
const char *firststr, *laststr;
|
1994-05-26 06:18:55 +00:00
|
|
|
int first, last, direction;
|
2009-12-27 18:04:05 +00:00
|
|
|
char *pat = NULL, *repl = NULL;
|
1994-05-26 06:18:55 +00:00
|
|
|
static int active = 0;
|
|
|
|
struct jmploc jmploc;
|
2009-06-23 20:45:12 +00:00
|
|
|
struct jmploc *savehandler;
|
|
|
|
char editfilestr[PATH_MAX];
|
|
|
|
char *volatile editfile;
|
2009-12-27 18:04:05 +00:00
|
|
|
FILE *efp = NULL;
|
2002-06-02 08:27:04 +00:00
|
|
|
int oldhistnum;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
if (hist == NULL)
|
|
|
|
error("history not active");
|
1995-05-30 00:07:29 +00:00
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
if (argc == 1)
|
|
|
|
error("missing history argument");
|
|
|
|
|
|
|
|
optreset = 1; optind = 1; /* initialize getopt */
|
2002-07-25 10:47:38 +00:00
|
|
|
opterr = 0;
|
1994-05-26 06:18:55 +00:00
|
|
|
while (not_fcnumber(argv[optind]) &&
|
1997-03-28 15:24:41 +00:00
|
|
|
(ch = getopt(argc, argv, ":e:lnrs")) != -1)
|
1994-05-26 06:18:55 +00:00
|
|
|
switch ((char)ch) {
|
|
|
|
case 'e':
|
2002-06-02 08:34:09 +00:00
|
|
|
editor = optarg;
|
1994-05-26 06:18:55 +00:00
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
lflg = 1;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
nflg = 1;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
rflg = 1;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
sflg = 1;
|
|
|
|
break;
|
|
|
|
case ':':
|
|
|
|
error("option -%c expects argument", optopt);
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
error("unknown option: -%c", optopt);
|
|
|
|
}
|
|
|
|
argc -= optind, argv += optind;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If executing...
|
|
|
|
*/
|
|
|
|
if (lflg == 0 || editor || sflg) {
|
|
|
|
lflg = 0; /* ignore */
|
2009-06-23 20:45:12 +00:00
|
|
|
editfile = NULL;
|
1994-05-26 06:18:55 +00:00
|
|
|
/*
|
|
|
|
* Catch interrupts to reset active counter and
|
|
|
|
* cleanup temp files.
|
|
|
|
*/
|
2009-06-23 20:45:12 +00:00
|
|
|
savehandler = handler;
|
1994-05-26 06:18:55 +00:00
|
|
|
if (setjmp(jmploc.loc)) {
|
|
|
|
active = 0;
|
2009-06-23 20:45:12 +00:00
|
|
|
if (editfile)
|
1994-05-26 06:18:55 +00:00
|
|
|
unlink(editfile);
|
|
|
|
handler = savehandler;
|
|
|
|
longjmp(handler->loc, 1);
|
|
|
|
}
|
|
|
|
handler = &jmploc;
|
|
|
|
if (++active > MAXHISTLOOPS) {
|
|
|
|
active = 0;
|
|
|
|
displayhist = 0;
|
|
|
|
error("called recursively too many times");
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Set editor.
|
|
|
|
*/
|
|
|
|
if (sflg == 0) {
|
|
|
|
if (editor == NULL &&
|
|
|
|
(editor = bltinlookup("FCEDIT", 1)) == NULL &&
|
|
|
|
(editor = bltinlookup("EDITOR", 1)) == NULL)
|
|
|
|
editor = DEFEDITOR;
|
|
|
|
if (editor[0] == '-' && editor[1] == '\0') {
|
|
|
|
sflg = 1; /* no edit */
|
|
|
|
editor = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If executing, parse [old=new] now
|
|
|
|
*/
|
1995-05-30 00:07:29 +00:00
|
|
|
if (lflg == 0 && argc > 0 &&
|
1994-05-26 06:18:55 +00:00
|
|
|
((repl = strchr(argv[0], '=')) != NULL)) {
|
|
|
|
pat = argv[0];
|
|
|
|
*repl++ = '\0';
|
|
|
|
argc--, argv++;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* determine [first] and [last]
|
|
|
|
*/
|
|
|
|
switch (argc) {
|
|
|
|
case 0:
|
|
|
|
firststr = lflg ? "-16" : "-1";
|
|
|
|
laststr = "-1";
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
firststr = argv[0];
|
|
|
|
laststr = lflg ? "-1" : argv[0];
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
firststr = argv[0];
|
|
|
|
laststr = argv[1];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("too many args");
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Turn into event numbers.
|
|
|
|
*/
|
|
|
|
first = str_to_event(firststr, 0);
|
|
|
|
last = str_to_event(laststr, 1);
|
|
|
|
|
|
|
|
if (rflg) {
|
|
|
|
i = last;
|
|
|
|
last = first;
|
|
|
|
first = i;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* XXX - this should not depend on the event numbers
|
1995-05-30 00:07:29 +00:00
|
|
|
* always increasing. Add sequence numbers or offset
|
1994-05-26 06:18:55 +00:00
|
|
|
* to the history element in next (diskbased) release.
|
|
|
|
*/
|
|
|
|
direction = first < last ? H_PREV : H_NEXT;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If editing, grab a temp file.
|
|
|
|
*/
|
|
|
|
if (editor) {
|
|
|
|
int fd;
|
|
|
|
INTOFF; /* easier */
|
2009-06-23 20:45:12 +00:00
|
|
|
sprintf(editfilestr, "%s/_shXXXXXX", _PATH_TMP);
|
|
|
|
if ((fd = mkstemp(editfilestr)) < 0)
|
1994-05-26 06:18:55 +00:00
|
|
|
error("can't create temporary file %s", editfile);
|
2009-06-23 20:45:12 +00:00
|
|
|
editfile = editfilestr;
|
1994-05-26 06:18:55 +00:00
|
|
|
if ((efp = fdopen(fd, "w")) == NULL) {
|
|
|
|
close(fd);
|
1996-09-03 13:35:11 +00:00
|
|
|
error("can't allocate stdio buffer for temp");
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Loop through selected history events. If listing or executing,
|
|
|
|
* do it now. Otherwise, put into temp file and call the editor
|
|
|
|
* after.
|
|
|
|
*
|
|
|
|
* The history interface needs rethinking, as the following
|
|
|
|
* convolutions will demonstrate.
|
|
|
|
*/
|
2001-10-01 08:43:58 +00:00
|
|
|
history(hist, &he, H_FIRST);
|
|
|
|
retval = history(hist, &he, H_NEXT_EVENT, first);
|
|
|
|
for (;retval != -1; retval = history(hist, &he, direction)) {
|
1994-05-26 06:18:55 +00:00
|
|
|
if (lflg) {
|
|
|
|
if (!nflg)
|
2001-10-01 08:43:58 +00:00
|
|
|
out1fmt("%5d ", he.num);
|
|
|
|
out1str(he.str);
|
1994-05-26 06:18:55 +00:00
|
|
|
} else {
|
1995-05-30 00:07:29 +00:00
|
|
|
char *s = pat ?
|
2001-10-01 08:43:58 +00:00
|
|
|
fc_replace(he.str, pat, repl) : (char *)he.str;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
if (sflg) {
|
|
|
|
if (displayhist) {
|
|
|
|
out2str(s);
|
2009-11-21 14:28:32 +00:00
|
|
|
flushout(out2);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
2009-05-31 12:36:14 +00:00
|
|
|
evalstring(s, 0);
|
1994-05-26 06:18:55 +00:00
|
|
|
if (displayhist && hist) {
|
|
|
|
/*
|
1995-05-30 00:07:29 +00:00
|
|
|
* XXX what about recursive and
|
1994-05-26 06:18:55 +00:00
|
|
|
* relative histnums.
|
|
|
|
*/
|
2002-06-02 08:27:04 +00:00
|
|
|
oldhistnum = he.num;
|
2001-10-01 08:43:58 +00:00
|
|
|
history(hist, &he, H_ENTER, s);
|
2002-06-02 08:27:04 +00:00
|
|
|
/*
|
|
|
|
* XXX H_ENTER moves the internal
|
|
|
|
* cursor, set it back to the current
|
|
|
|
* entry.
|
|
|
|
*/
|
|
|
|
retval = history(hist, &he,
|
|
|
|
H_NEXT_EVENT, oldhistnum);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
fputs(s, efp);
|
|
|
|
}
|
|
|
|
/*
|
2006-08-04 07:56:35 +00:00
|
|
|
* At end? (if we were to lose last, we'd sure be
|
1994-05-26 06:18:55 +00:00
|
|
|
* messed up).
|
|
|
|
*/
|
2001-10-01 08:43:58 +00:00
|
|
|
if (he.num == last)
|
1994-05-26 06:18:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (editor) {
|
|
|
|
char *editcmd;
|
|
|
|
|
|
|
|
fclose(efp);
|
|
|
|
editcmd = stalloc(strlen(editor) + strlen(editfile) + 2);
|
|
|
|
sprintf(editcmd, "%s %s", editor, editfile);
|
2009-05-31 12:36:14 +00:00
|
|
|
evalstring(editcmd, 0); /* XXX - should use no JC command */
|
1994-05-26 06:18:55 +00:00
|
|
|
INTON;
|
|
|
|
readcmdfile(editfile); /* XXX - should read back - quick tst */
|
|
|
|
unlink(editfile);
|
|
|
|
}
|
1995-05-30 00:07:29 +00:00
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
if (lflg == 0 && active > 0)
|
|
|
|
--active;
|
|
|
|
if (displayhist)
|
|
|
|
displayhist = 0;
|
1996-09-01 10:22:36 +00:00
|
|
|
return 0;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static char *
|
2002-02-02 06:50:57 +00:00
|
|
|
fc_replace(const char *s, char *p, char *r)
|
1994-05-26 06:18:55 +00:00
|
|
|
{
|
|
|
|
char *dest;
|
|
|
|
int plen = strlen(p);
|
|
|
|
|
|
|
|
STARTSTACKSTR(dest);
|
|
|
|
while (*s) {
|
|
|
|
if (*s == *p && strncmp(s, p, plen) == 0) {
|
|
|
|
while (*r)
|
|
|
|
STPUTC(*r++, dest);
|
|
|
|
s += plen;
|
|
|
|
*p = '\0'; /* so no more matches */
|
|
|
|
} else
|
|
|
|
STPUTC(*s++, dest);
|
|
|
|
}
|
|
|
|
STACKSTRNUL(dest);
|
|
|
|
dest = grabstackstr(dest);
|
|
|
|
|
|
|
|
return (dest);
|
|
|
|
}
|
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
int
|
2009-12-24 18:41:14 +00:00
|
|
|
not_fcnumber(const char *s)
|
1994-05-26 06:18:55 +00:00
|
|
|
{
|
1996-12-14 06:20:03 +00:00
|
|
|
if (s == NULL)
|
1997-04-28 03:10:31 +00:00
|
|
|
return (0);
|
1996-12-14 06:20:03 +00:00
|
|
|
if (*s == '-')
|
|
|
|
s++;
|
1994-05-26 06:18:55 +00:00
|
|
|
return (!is_number(s));
|
|
|
|
}
|
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
int
|
2009-12-24 18:41:14 +00:00
|
|
|
str_to_event(const char *str, int last)
|
1994-05-26 06:18:55 +00:00
|
|
|
{
|
2001-10-01 08:43:58 +00:00
|
|
|
HistEvent he;
|
2009-12-24 18:41:14 +00:00
|
|
|
const char *s = str;
|
1994-05-26 06:18:55 +00:00
|
|
|
int relative = 0;
|
2001-10-01 08:43:58 +00:00
|
|
|
int i, retval;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2001-10-01 08:43:58 +00:00
|
|
|
retval = history(hist, &he, H_FIRST);
|
1994-05-26 06:18:55 +00:00
|
|
|
switch (*s) {
|
|
|
|
case '-':
|
|
|
|
relative = 1;
|
|
|
|
/*FALLTHROUGH*/
|
|
|
|
case '+':
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
if (is_number(s)) {
|
|
|
|
i = atoi(s);
|
|
|
|
if (relative) {
|
2001-10-01 08:43:58 +00:00
|
|
|
while (retval != -1 && i--) {
|
|
|
|
retval = history(hist, &he, H_NEXT);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
2001-10-01 08:43:58 +00:00
|
|
|
if (retval == -1)
|
|
|
|
retval = history(hist, &he, H_LAST);
|
1994-05-26 06:18:55 +00:00
|
|
|
} else {
|
2001-10-01 08:43:58 +00:00
|
|
|
retval = history(hist, &he, H_NEXT_EVENT, i);
|
|
|
|
if (retval == -1) {
|
1994-05-26 06:18:55 +00:00
|
|
|
/*
|
|
|
|
* the notion of first and last is
|
|
|
|
* backwards to that of the history package
|
|
|
|
*/
|
2001-10-01 08:43:58 +00:00
|
|
|
retval = history(hist, &he, last ? H_FIRST : H_LAST);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
2001-10-01 08:43:58 +00:00
|
|
|
if (retval == -1)
|
1994-05-26 06:18:55 +00:00
|
|
|
error("history number %s not found (internal error)",
|
|
|
|
str);
|
|
|
|
} else {
|
|
|
|
/*
|
1995-05-30 00:07:29 +00:00
|
|
|
* pattern
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
2001-10-01 08:43:58 +00:00
|
|
|
retval = history(hist, &he, H_PREV_STR, str);
|
|
|
|
if (retval == -1)
|
1994-05-26 06:18:55 +00:00
|
|
|
error("history pattern not found: %s", str);
|
|
|
|
}
|
2001-10-01 08:43:58 +00:00
|
|
|
return (he.num);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
2002-07-23 11:50:53 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
bindcmd(int argc, char **argv)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (el == NULL)
|
|
|
|
error("line editing is disabled");
|
2005-08-11 20:28:26 +00:00
|
|
|
return (el_parse(el, argc, (const char **)argv));
|
2002-07-23 11:50:53 +00:00
|
|
|
}
|
|
|
|
|
1997-04-28 03:10:31 +00:00
|
|
|
#else
|
|
|
|
#include "error.h"
|
|
|
|
|
|
|
|
int
|
2002-02-02 06:50:57 +00:00
|
|
|
histcmd(int argc, char **argv)
|
1997-04-28 03:10:31 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
error("not compiled with history support");
|
|
|
|
/*NOTREACHED*/
|
|
|
|
return (0);
|
|
|
|
}
|
2002-07-23 11:50:53 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
bindcmd(int argc, char **argv)
|
|
|
|
{
|
|
|
|
|
|
|
|
error("not compiled with line editing support");
|
|
|
|
return (0);
|
|
|
|
}
|
1997-04-28 03:10:31 +00:00
|
|
|
#endif
|