2014-08-03 23:10:41 +02:00
|
|
|
/* $NetBSD: histedit.c,v 1.45 2012/03/20 18:42:29 matt Exp $ */
|
|
|
|
|
2006-05-23 14:59:34 +02: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.
|
2014-08-03 23:10:41 +02:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
2006-05-23 14:59:34 +02:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2014-08-03 23:10:41 +02:00
|
|
|
#include <sys/cdefs.h>
|
2006-05-23 14:59:34 +02:00
|
|
|
#ifndef lint
|
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)histedit.c 8.2 (Berkeley) 5/4/95";
|
2014-08-03 23:10:41 +02:00
|
|
|
#else
|
|
|
|
__RCSID("$NetBSD: histedit.c,v 1.45 2012/03/20 18:42:29 matt Exp $");
|
2006-05-23 14:59:34 +02:00
|
|
|
#endif
|
|
|
|
#endif /* not lint */
|
|
|
|
|
2014-08-03 23:10:41 +02:00
|
|
|
#include <sys/param.h>
|
2006-05-23 14:59:34 +02:00
|
|
|
#include <paths.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
/*
|
|
|
|
* Editline and history functions (and glue).
|
|
|
|
*/
|
|
|
|
#include "shell.h"
|
|
|
|
#include "parser.h"
|
|
|
|
#include "var.h"
|
|
|
|
#include "options.h"
|
2014-08-03 23:10:41 +02:00
|
|
|
#include "builtins.h"
|
2006-05-23 14:59:34 +02:00
|
|
|
#include "main.h"
|
|
|
|
#include "output.h"
|
|
|
|
#include "mystring.h"
|
|
|
|
#include "myhistedit.h"
|
|
|
|
#include "error.h"
|
2014-08-03 23:10:41 +02:00
|
|
|
#ifndef SMALL
|
2006-05-23 14:59:34 +02:00
|
|
|
#include "eval.h"
|
|
|
|
#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;
|
2014-08-03 23:10:41 +02:00
|
|
|
static FILE *el_in, *el_out;
|
|
|
|
unsigned char _el_fn_complete(EditLine *, int);
|
2006-05-23 14:59:34 +02:00
|
|
|
|
2014-08-03 23:10:41 +02:00
|
|
|
STATIC const char *fc_replace(const char *, char *, char *);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
extern FILE *tracefile;
|
|
|
|
#endif
|
2006-05-23 14:59:34 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set history and editing status. Called whenever the status may
|
|
|
|
* have changed (figures out what to do).
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
histedit(void)
|
|
|
|
{
|
2014-08-03 23:10:41 +02:00
|
|
|
FILE *el_err;
|
2006-05-23 14:59:34 +02:00
|
|
|
|
|
|
|
#define editing (Eflag || Vflag)
|
|
|
|
|
2014-08-03 23:10:41 +02:00
|
|
|
if (iflag == 1) {
|
2006-05-23 14:59:34 +02:00
|
|
|
if (!hist) {
|
|
|
|
/*
|
|
|
|
* turn history on
|
|
|
|
*/
|
|
|
|
INTOFF;
|
|
|
|
hist = history_init();
|
|
|
|
INTON;
|
|
|
|
|
|
|
|
if (hist != NULL)
|
|
|
|
sethistsize(histsizeval());
|
|
|
|
else
|
|
|
|
out2str("sh: can't initialize history\n");
|
|
|
|
}
|
|
|
|
if (editing && !el && isatty(0)) { /* && isatty(2) ??? */
|
|
|
|
/*
|
|
|
|
* turn editing on
|
|
|
|
*/
|
2014-08-03 23:10:41 +02:00
|
|
|
char *term, *shname;
|
|
|
|
|
2006-05-23 14:59:34 +02:00
|
|
|
INTOFF;
|
|
|
|
if (el_in == NULL)
|
|
|
|
el_in = fdopen(0, "r");
|
|
|
|
if (el_out == NULL)
|
|
|
|
el_out = fdopen(2, "w");
|
2014-08-03 23:10:41 +02:00
|
|
|
if (el_in == NULL || el_out == NULL)
|
2006-05-23 14:59:34 +02:00
|
|
|
goto bad;
|
2014-08-03 23:10:41 +02:00
|
|
|
el_err = el_out;
|
|
|
|
#if DEBUG
|
|
|
|
if (tracefile)
|
|
|
|
el_err = tracefile;
|
|
|
|
#endif
|
|
|
|
term = lookupvar("TERM");
|
|
|
|
if (term)
|
|
|
|
setenv("TERM", term, 1);
|
|
|
|
else
|
|
|
|
unsetenv("TERM");
|
|
|
|
shname = arg0;
|
|
|
|
if (shname[0] == '-')
|
|
|
|
shname++;
|
|
|
|
el = el_init(shname, el_in, el_out, el_err);
|
2006-05-23 14:59:34 +02:00
|
|
|
if (el != NULL) {
|
|
|
|
if (hist)
|
|
|
|
el_set(el, EL_HIST, history, hist);
|
|
|
|
el_set(el, EL_PROMPT, getprompt);
|
2014-08-03 23:10:41 +02:00
|
|
|
el_set(el, EL_SIGNAL, 1);
|
|
|
|
el_set(el, EL_ADDFN, "rl-complete",
|
|
|
|
"ReadLine compatible completion function",
|
|
|
|
_el_fn_complete);
|
2006-05-23 14:59:34 +02:00
|
|
|
} else {
|
|
|
|
bad:
|
|
|
|
out2str("sh: can't initialize editing\n");
|
|
|
|
}
|
|
|
|
INTON;
|
|
|
|
} else if (!editing && el) {
|
|
|
|
INTOFF;
|
|
|
|
el_end(el);
|
|
|
|
el = NULL;
|
|
|
|
INTON;
|
|
|
|
}
|
|
|
|
if (el) {
|
2014-08-03 23:10:41 +02:00
|
|
|
el_source(el, NULL);
|
2006-05-23 14:59:34 +02:00
|
|
|
if (Vflag)
|
|
|
|
el_set(el, EL_EDITOR, "vi");
|
|
|
|
else if (Eflag)
|
|
|
|
el_set(el, EL_EDITOR, "emacs");
|
2014-08-03 23:10:41 +02:00
|
|
|
el_set(el, EL_BIND, "^I",
|
|
|
|
tabcomplete ? "rl-complete" : "ed-insert", NULL);
|
2006-05-23 14:59:34 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
INTOFF;
|
|
|
|
if (el) { /* no editing if not interactive */
|
|
|
|
el_end(el);
|
|
|
|
el = NULL;
|
|
|
|
}
|
|
|
|
if (hist) {
|
|
|
|
history_end(hist);
|
|
|
|
hist = NULL;
|
|
|
|
}
|
|
|
|
INTON;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2014-08-03 23:10:41 +02:00
|
|
|
sethistsize(const char *hs)
|
2006-05-23 14:59:34 +02:00
|
|
|
{
|
|
|
|
int histsize;
|
|
|
|
HistEvent he;
|
|
|
|
|
|
|
|
if (hist != NULL) {
|
|
|
|
if (hs == NULL || *hs == '\0' ||
|
|
|
|
(histsize = atoi(hs)) < 0)
|
|
|
|
histsize = 100;
|
2013-01-30 15:36:51 +01:00
|
|
|
history(hist, &he, H_SETSIZE, histsize);
|
2014-08-03 23:10:41 +02:00
|
|
|
history(hist, &he, H_SETUNIQUE, 1);
|
2006-05-23 14:59:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-03 23:10:41 +02:00
|
|
|
void
|
|
|
|
setterm(const char *term)
|
|
|
|
{
|
|
|
|
if (el != NULL && term != NULL)
|
|
|
|
if (el_set(el, EL_TERMINAL, term) != 0) {
|
|
|
|
outfmt(out2, "sh: Can't set terminal type %s\n", term);
|
|
|
|
outfmt(out2, "sh: Using dumb terminal settings.\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
inputrc(int argc, char **argv)
|
|
|
|
{
|
|
|
|
if (argc != 2) {
|
|
|
|
out2str("usage: inputrc file\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (el != NULL) {
|
|
|
|
if (el_source(el, argv[1])) {
|
|
|
|
out2str("inputrc: failed\n");
|
|
|
|
return 1;
|
|
|
|
} else
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
out2str("sh: inputrc ignored, not editing\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This command is provided since POSIX decided to standardize
|
|
|
|
* the Korn shell fc command. Oh well...
|
|
|
|
*/
|
2006-05-23 14:59:34 +02:00
|
|
|
int
|
|
|
|
histcmd(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int ch;
|
2014-08-03 23:10:41 +02:00
|
|
|
const char * volatile editor = NULL;
|
2006-05-23 14:59:34 +02:00
|
|
|
HistEvent he;
|
2014-08-03 23:10:41 +02:00
|
|
|
int lflg = 0;
|
|
|
|
volatile int nflg = 0, rflg = 0, sflg = 0;
|
2006-05-23 14:59:34 +02:00
|
|
|
int i, retval;
|
2014-08-03 23:10:41 +02:00
|
|
|
const char *firststr, *laststr;
|
2006-05-23 14:59:34 +02:00
|
|
|
int first, last, direction;
|
2014-08-03 23:10:41 +02:00
|
|
|
char *pat = NULL, *repl; /* ksh "fc old=new" crap */
|
2006-05-23 14:59:34 +02:00
|
|
|
static int active = 0;
|
|
|
|
struct jmploc jmploc;
|
|
|
|
struct jmploc *volatile savehandler;
|
2014-08-03 23:10:41 +02:00
|
|
|
char editfile[MAXPATHLEN + 1];
|
2006-05-23 14:59:34 +02:00
|
|
|
FILE *efp;
|
|
|
|
#ifdef __GNUC__
|
2014-08-03 23:10:41 +02:00
|
|
|
repl = NULL; /* XXX gcc4 */
|
|
|
|
efp = NULL; /* XXX gcc4 */
|
2006-05-23 14:59:34 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (hist == NULL)
|
|
|
|
error("history not active");
|
|
|
|
|
|
|
|
if (argc == 1)
|
|
|
|
error("missing history argument");
|
|
|
|
|
|
|
|
optreset = 1; optind = 1; /* initialize getopt */
|
|
|
|
while (not_fcnumber(argv[optind]) &&
|
|
|
|
(ch = getopt(argc, argv, ":e:lnrs")) != -1)
|
|
|
|
switch ((char)ch) {
|
|
|
|
case 'e':
|
2014-08-03 23:10:41 +02:00
|
|
|
editor = optionarg;
|
2006-05-23 14:59:34 +02: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);
|
2014-08-03 23:10:41 +02:00
|
|
|
/* NOTREACHED */
|
2006-05-23 14:59:34 +02:00
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
error("unknown option: -%c", optopt);
|
2014-08-03 23:10:41 +02:00
|
|
|
/* NOTREACHED */
|
2006-05-23 14:59:34 +02:00
|
|
|
}
|
|
|
|
argc -= optind, argv += optind;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If executing...
|
|
|
|
*/
|
|
|
|
if (lflg == 0 || editor || sflg) {
|
|
|
|
lflg = 0; /* ignore */
|
|
|
|
editfile[0] = '\0';
|
|
|
|
/*
|
|
|
|
* Catch interrupts to reset active counter and
|
|
|
|
* cleanup temp files.
|
|
|
|
*/
|
2014-08-03 23:10:41 +02:00
|
|
|
savehandler = handler;
|
2006-05-23 14:59:34 +02:00
|
|
|
if (setjmp(jmploc.loc)) {
|
|
|
|
active = 0;
|
|
|
|
if (*editfile)
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
if (lflg == 0 && argc > 0 &&
|
|
|
|
((repl = strchr(argv[0], '=')) != NULL)) {
|
|
|
|
pat = argv[0];
|
|
|
|
*repl++ = '\0';
|
|
|
|
argc--, argv++;
|
|
|
|
}
|
2014-08-03 23:10:41 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If -s is specified, accept only one operand
|
|
|
|
*/
|
|
|
|
if (sflg && argc >= 2)
|
|
|
|
error("too many args");
|
|
|
|
|
2006-05-23 14:59:34 +02:00
|
|
|
/*
|
|
|
|
* 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");
|
2014-08-03 23:10:41 +02:00
|
|
|
/* NOTREACHED */
|
2006-05-23 14:59:34 +02:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
* always increasing. Add sequence numbers or offset
|
|
|
|
* 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 */
|
2014-08-03 23:10:41 +02:00
|
|
|
snprintf(editfile, sizeof(editfile), "%s_shXXXXXX", _PATH_TMP);
|
2006-05-23 14:59:34 +02:00
|
|
|
if ((fd = mkstemp(editfile)) < 0)
|
|
|
|
error("can't create temporary file %s", editfile);
|
|
|
|
if ((efp = fdopen(fd, "w")) == NULL) {
|
|
|
|
close(fd);
|
|
|
|
error("can't allocate stdio buffer for temp");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
history(hist, &he, H_FIRST);
|
|
|
|
retval = history(hist, &he, H_NEXT_EVENT, first);
|
|
|
|
for (;retval != -1; retval = history(hist, &he, direction)) {
|
|
|
|
if (lflg) {
|
|
|
|
if (!nflg)
|
|
|
|
out1fmt("%5d ", he.num);
|
|
|
|
out1str(he.str);
|
|
|
|
} else {
|
2014-08-03 23:10:41 +02:00
|
|
|
const char *s = pat ?
|
|
|
|
fc_replace(he.str, pat, repl) : he.str;
|
2006-05-23 14:59:34 +02:00
|
|
|
|
|
|
|
if (sflg) {
|
|
|
|
if (displayhist) {
|
|
|
|
out2str(s);
|
|
|
|
}
|
2014-08-03 23:10:41 +02:00
|
|
|
|
|
|
|
evalstring(strcpy(stalloc(strlen(s) + 1), s), 0);
|
2006-05-23 14:59:34 +02:00
|
|
|
if (displayhist && hist) {
|
|
|
|
/*
|
|
|
|
* XXX what about recursive and
|
|
|
|
* relative histnums.
|
|
|
|
*/
|
|
|
|
history(hist, &he, H_ENTER, s);
|
|
|
|
}
|
2014-08-03 23:10:41 +02:00
|
|
|
|
|
|
|
break;
|
2006-05-23 14:59:34 +02:00
|
|
|
} else
|
|
|
|
fputs(s, efp);
|
|
|
|
}
|
|
|
|
/*
|
2014-08-03 23:10:41 +02:00
|
|
|
* At end? (if we were to lose last, we'd sure be
|
2006-05-23 14:59:34 +02:00
|
|
|
* messed up).
|
|
|
|
*/
|
|
|
|
if (he.num == last)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (editor) {
|
|
|
|
char *editcmd;
|
|
|
|
|
|
|
|
fclose(efp);
|
|
|
|
editcmd = stalloc(strlen(editor) + strlen(editfile) + 2);
|
|
|
|
sprintf(editcmd, "%s %s", editor, editfile);
|
2014-08-03 23:10:41 +02:00
|
|
|
evalstring(editcmd, 0); /* XXX - should use no JC command */
|
2006-05-23 14:59:34 +02:00
|
|
|
INTON;
|
|
|
|
readcmdfile(editfile); /* XXX - should read back - quick tst */
|
|
|
|
unlink(editfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lflg == 0 && active > 0)
|
|
|
|
--active;
|
|
|
|
if (displayhist)
|
|
|
|
displayhist = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-03 23:10:41 +02:00
|
|
|
STATIC const char *
|
2006-05-23 14:59:34 +02:00
|
|
|
fc_replace(const char *s, char *p, char *r)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-08-03 23:10:41 +02:00
|
|
|
int
|
2006-05-23 14:59:34 +02:00
|
|
|
not_fcnumber(char *s)
|
|
|
|
{
|
|
|
|
if (s == NULL)
|
2014-08-03 23:10:41 +02:00
|
|
|
return 0;
|
|
|
|
if (*s == '-')
|
|
|
|
s++;
|
2006-05-23 14:59:34 +02:00
|
|
|
return (!is_number(s));
|
|
|
|
}
|
|
|
|
|
2014-08-03 23:10:41 +02:00
|
|
|
int
|
|
|
|
str_to_event(const char *str, int last)
|
2006-05-23 14:59:34 +02:00
|
|
|
{
|
|
|
|
HistEvent he;
|
2014-08-03 23:10:41 +02:00
|
|
|
const char *s = str;
|
2006-05-23 14:59:34 +02:00
|
|
|
int relative = 0;
|
|
|
|
int i, retval;
|
|
|
|
|
|
|
|
retval = history(hist, &he, H_FIRST);
|
|
|
|
switch (*s) {
|
|
|
|
case '-':
|
|
|
|
relative = 1;
|
|
|
|
/*FALLTHROUGH*/
|
|
|
|
case '+':
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
if (is_number(s)) {
|
|
|
|
i = atoi(s);
|
|
|
|
if (relative) {
|
|
|
|
while (retval != -1 && i--) {
|
|
|
|
retval = history(hist, &he, H_NEXT);
|
|
|
|
}
|
|
|
|
if (retval == -1)
|
|
|
|
retval = history(hist, &he, H_LAST);
|
|
|
|
} else {
|
|
|
|
retval = history(hist, &he, H_NEXT_EVENT, i);
|
|
|
|
if (retval == -1) {
|
|
|
|
/*
|
|
|
|
* the notion of first and last is
|
|
|
|
* backwards to that of the history package
|
|
|
|
*/
|
2014-08-03 23:10:41 +02:00
|
|
|
retval = history(hist, &he,
|
|
|
|
last ? H_FIRST : H_LAST);
|
2006-05-23 14:59:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (retval == -1)
|
|
|
|
error("history number %s not found (internal error)",
|
|
|
|
str);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* pattern
|
|
|
|
*/
|
|
|
|
retval = history(hist, &he, H_PREV_STR, str);
|
|
|
|
if (retval == -1)
|
|
|
|
error("history pattern not found: %s", str);
|
|
|
|
}
|
|
|
|
return (he.num);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
int
|
|
|
|
histcmd(int argc, char **argv)
|
|
|
|
{
|
|
|
|
error("not compiled with history support");
|
2014-08-03 23:10:41 +02:00
|
|
|
/* NOTREACHED */
|
2006-05-23 14:59:34 +02:00
|
|
|
}
|
|
|
|
int
|
2014-08-03 23:10:41 +02:00
|
|
|
inputrc(int argc, char **argv)
|
2006-05-23 14:59:34 +02:00
|
|
|
{
|
2014-08-03 23:10:41 +02:00
|
|
|
error("not compiled with history support");
|
|
|
|
/* NOTREACHED */
|
2006-05-23 14:59:34 +02:00
|
|
|
}
|
2014-08-03 23:10:41 +02:00
|
|
|
#endif
|