xref: /freebsd/bin/sh/histedit.c (revision 94b793c4)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1993
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
64b88c807SRodney W. Grimes  * Kenneth Almquist.
74b88c807SRodney W. Grimes  *
84b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
94b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
104b88c807SRodney W. Grimes  * are met:
114b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
124b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
134b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
144b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
154b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
16fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
174b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
184b88c807SRodney W. Grimes  *    without specific prior written permission.
194b88c807SRodney W. Grimes  *
204b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
214b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
234b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
244b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
254b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
264b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
274b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
284b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
294b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
304b88c807SRodney W. Grimes  * SUCH DAMAGE.
314b88c807SRodney W. Grimes  */
324b88c807SRodney W. Grimes 
334b88c807SRodney W. Grimes #ifndef lint
343d7b5b93SPhilippe Charnier #if 0
353d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)histedit.c	8.2 (Berkeley) 5/4/95";
363d7b5b93SPhilippe Charnier #endif
374b88c807SRodney W. Grimes #endif /* not lint */
382749b141SDavid E. O'Brien #include <sys/cdefs.h>
392749b141SDavid E. O'Brien __FBSDID("$FreeBSD$");
404b88c807SRodney W. Grimes 
414b88c807SRodney W. Grimes #include <sys/param.h>
4292e331afSWarner Losh #include <limits.h>
434b88c807SRodney W. Grimes #include <paths.h>
444b88c807SRodney W. Grimes #include <stdio.h>
45aa9caaf6SPeter Wemm #include <stdlib.h>
46aa9caaf6SPeter Wemm #include <unistd.h>
47aa9caaf6SPeter Wemm /*
48aa9caaf6SPeter Wemm  * Editline and history functions (and glue).
49aa9caaf6SPeter Wemm  */
504b88c807SRodney W. Grimes #include "shell.h"
514b88c807SRodney W. Grimes #include "parser.h"
524b88c807SRodney W. Grimes #include "var.h"
534b88c807SRodney W. Grimes #include "options.h"
54aa9caaf6SPeter Wemm #include "main.h"
55aa9caaf6SPeter Wemm #include "output.h"
564b88c807SRodney W. Grimes #include "mystring.h"
57aa9caaf6SPeter Wemm #ifndef NO_HISTORY
58aa9caaf6SPeter Wemm #include "myhistedit.h"
594b88c807SRodney W. Grimes #include "error.h"
60aa9caaf6SPeter Wemm #include "eval.h"
614b88c807SRodney W. Grimes #include "memalloc.h"
62454a02b3SJilles Tjoelker #include "builtins.h"
634b88c807SRodney W. Grimes 
644b88c807SRodney W. Grimes #define MAXHISTLOOPS	4	/* max recursions through fc */
654b88c807SRodney W. Grimes #define DEFEDITOR	"ed"	/* default editor *should* be $EDITOR */
664b88c807SRodney W. Grimes 
674b88c807SRodney W. Grimes History *hist;	/* history cookie */
684b88c807SRodney W. Grimes EditLine *el;	/* editline cookie */
694b88c807SRodney W. Grimes int displayhist;
70f91d2e21SJilles Tjoelker static FILE *el_in, *el_out;
714b88c807SRodney W. Grimes 
7288328642SDavid E. O'Brien static char *fc_replace(const char *, char *, char *);
73260fc3f4SJilles Tjoelker static int not_fcnumber(const char *);
74260fc3f4SJilles Tjoelker static int str_to_event(const char *, int);
754b88c807SRodney W. Grimes 
764b88c807SRodney W. Grimes /*
774b88c807SRodney W. Grimes  * Set history and editing status.  Called whenever the status may
784b88c807SRodney W. Grimes  * have changed (figures out what to do).
794b88c807SRodney W. Grimes  */
80aa9caaf6SPeter Wemm void
815134c3f7SWarner Losh histedit(void)
82aa9caaf6SPeter Wemm {
834b88c807SRodney W. Grimes 
844b88c807SRodney W. Grimes #define editing (Eflag || Vflag)
854b88c807SRodney W. Grimes 
864b88c807SRodney W. Grimes 	if (iflag) {
874b88c807SRodney W. Grimes 		if (!hist) {
884b88c807SRodney W. Grimes 			/*
894b88c807SRodney W. Grimes 			 * turn history on
904b88c807SRodney W. Grimes 			 */
914b88c807SRodney W. Grimes 			INTOFF;
924b88c807SRodney W. Grimes 			hist = history_init();
934b88c807SRodney W. Grimes 			INTON;
944b88c807SRodney W. Grimes 
954b88c807SRodney W. Grimes 			if (hist != NULL)
96ab0a2172SSteve Price 				sethistsize(histsizeval());
974b88c807SRodney W. Grimes 			else
98c6204d4aSJilles Tjoelker 				out2fmt_flush("sh: can't initialize history\n");
994b88c807SRodney W. Grimes 		}
1004b88c807SRodney W. Grimes 		if (editing && !el && isatty(0)) { /* && isatty(2) ??? */
1014b88c807SRodney W. Grimes 			/*
1024b88c807SRodney W. Grimes 			 * turn editing on
1034b88c807SRodney W. Grimes 			 */
104580eefdfSJilles Tjoelker 			char *term;
105580eefdfSJilles Tjoelker 
1064b88c807SRodney W. Grimes 			INTOFF;
1074b88c807SRodney W. Grimes 			if (el_in == NULL)
1084b88c807SRodney W. Grimes 				el_in = fdopen(0, "r");
1094b88c807SRodney W. Grimes 			if (el_out == NULL)
1104b88c807SRodney W. Grimes 				el_out = fdopen(2, "w");
111f91d2e21SJilles Tjoelker 			if (el_in == NULL || el_out == NULL)
1124b88c807SRodney W. Grimes 				goto bad;
113580eefdfSJilles Tjoelker 			term = lookupvar("TERM");
114580eefdfSJilles Tjoelker 			if (term)
115580eefdfSJilles Tjoelker 				setenv("TERM", term, 1);
116580eefdfSJilles Tjoelker 			else
117580eefdfSJilles Tjoelker 				unsetenv("TERM");
118f91d2e21SJilles Tjoelker 			el = el_init(arg0, el_in, el_out, el_out);
1194b88c807SRodney W. Grimes 			if (el != NULL) {
1204b88c807SRodney W. Grimes 				if (hist)
1214b88c807SRodney W. Grimes 					el_set(el, EL_HIST, history, hist);
1224b88c807SRodney W. Grimes 				el_set(el, EL_PROMPT, getprompt);
123e46b12b7SJilles Tjoelker 				el_set(el, EL_ADDFN, "sh-complete",
124e46b12b7SJilles Tjoelker 				    "Filename completion",
125e46b12b7SJilles Tjoelker 				    _el_fn_sh_complete);
1264b88c807SRodney W. Grimes 			} else {
1274b88c807SRodney W. Grimes bad:
128c6204d4aSJilles Tjoelker 				out2fmt_flush("sh: can't initialize editing\n");
1294b88c807SRodney W. Grimes 			}
1304b88c807SRodney W. Grimes 			INTON;
1314b88c807SRodney W. Grimes 		} else if (!editing && el) {
1324b88c807SRodney W. Grimes 			INTOFF;
1334b88c807SRodney W. Grimes 			el_end(el);
1344b88c807SRodney W. Grimes 			el = NULL;
1354b88c807SRodney W. Grimes 			INTON;
1364b88c807SRodney W. Grimes 		}
1374b88c807SRodney W. Grimes 		if (el) {
1384b88c807SRodney W. Grimes 			if (Vflag)
1394b88c807SRodney W. Grimes 				el_set(el, EL_EDITOR, "vi");
1404b88c807SRodney W. Grimes 			else if (Eflag)
1414b88c807SRodney W. Grimes 				el_set(el, EL_EDITOR, "emacs");
142e46b12b7SJilles Tjoelker 			el_set(el, EL_BIND, "^I", "sh-complete", NULL);
143ecd807fbSTim J. Robbins 			el_source(el, NULL);
1444b88c807SRodney W. Grimes 		}
1454b88c807SRodney W. Grimes 	} else {
1464b88c807SRodney W. Grimes 		INTOFF;
1474b88c807SRodney W. Grimes 		if (el) {	/* no editing if not interactive */
1484b88c807SRodney W. Grimes 			el_end(el);
1494b88c807SRodney W. Grimes 			el = NULL;
1504b88c807SRodney W. Grimes 		}
1514b88c807SRodney W. Grimes 		if (hist) {
1524b88c807SRodney W. Grimes 			history_end(hist);
1534b88c807SRodney W. Grimes 			hist = NULL;
1544b88c807SRodney W. Grimes 		}
1554b88c807SRodney W. Grimes 		INTON;
1564b88c807SRodney W. Grimes 	}
1574b88c807SRodney W. Grimes }
1584b88c807SRodney W. Grimes 
159aa9caaf6SPeter Wemm 
160aa9caaf6SPeter Wemm void
1612fae4c3dSPhilippe Charnier sethistsize(const char *hs)
162aa9caaf6SPeter Wemm {
1634b88c807SRodney W. Grimes 	int histsize;
164757eeda0SDavid E. O'Brien 	HistEvent he;
1654b88c807SRodney W. Grimes 
1664b88c807SRodney W. Grimes 	if (hist != NULL) {
167ef9e6178SJilles Tjoelker 		if (hs == NULL || !is_number(hs))
1684b88c807SRodney W. Grimes 			histsize = 100;
169ef9e6178SJilles Tjoelker 		else
170ef9e6178SJilles Tjoelker 			histsize = atoi(hs);
1712110d9c3SStefan Farfeleder 		history(hist, &he, H_SETSIZE, histsize);
172933803fbSJilles Tjoelker 		history(hist, &he, H_SETUNIQUE, 1);
1734b88c807SRodney W. Grimes 	}
1744b88c807SRodney W. Grimes }
1754b88c807SRodney W. Grimes 
176580eefdfSJilles Tjoelker void
177580eefdfSJilles Tjoelker setterm(const char *term)
178580eefdfSJilles Tjoelker {
179580eefdfSJilles Tjoelker 	if (rootshell && el != NULL && term != NULL)
180580eefdfSJilles Tjoelker 		el_set(el, EL_TERMINAL, term);
181580eefdfSJilles Tjoelker }
182580eefdfSJilles Tjoelker 
183aa9caaf6SPeter Wemm int
1847cbda738SJilles Tjoelker histcmd(int argc, char **argv __unused)
1854b88c807SRodney W. Grimes {
1864b88c807SRodney W. Grimes 	int ch;
187384aedabSJilles Tjoelker 	const char *editor = NULL;
188757eeda0SDavid E. O'Brien 	HistEvent he;
1894b88c807SRodney W. Grimes 	int lflg = 0, nflg = 0, rflg = 0, sflg = 0;
190757eeda0SDavid E. O'Brien 	int i, retval;
191384aedabSJilles Tjoelker 	const char *firststr, *laststr;
1924b88c807SRodney W. Grimes 	int first, last, direction;
193384aedabSJilles Tjoelker 	char *pat = NULL, *repl = NULL;
1944b88c807SRodney W. Grimes 	static int active = 0;
1954b88c807SRodney W. Grimes 	struct jmploc jmploc;
196224fbf9fSJilles Tjoelker 	struct jmploc *savehandler;
197224fbf9fSJilles Tjoelker 	char editfilestr[PATH_MAX];
198224fbf9fSJilles Tjoelker 	char *volatile editfile;
199384aedabSJilles Tjoelker 	FILE *efp = NULL;
20032c07786STim J. Robbins 	int oldhistnum;
2014b88c807SRodney W. Grimes 
2024b88c807SRodney W. Grimes 	if (hist == NULL)
2034b88c807SRodney W. Grimes 		error("history not active");
2044b88c807SRodney W. Grimes 
2054b88c807SRodney W. Grimes 	if (argc == 1)
2064b88c807SRodney W. Grimes 		error("missing history argument");
2074b88c807SRodney W. Grimes 
2087cbda738SJilles Tjoelker 	while (not_fcnumber(*argptr) && (ch = nextopt("e:lnrs")) != '\0')
2094b88c807SRodney W. Grimes 		switch ((char)ch) {
2104b88c807SRodney W. Grimes 		case 'e':
2117cbda738SJilles Tjoelker 			editor = shoptarg;
2124b88c807SRodney W. Grimes 			break;
2134b88c807SRodney W. Grimes 		case 'l':
2144b88c807SRodney W. Grimes 			lflg = 1;
2154b88c807SRodney W. Grimes 			break;
2164b88c807SRodney W. Grimes 		case 'n':
2174b88c807SRodney W. Grimes 			nflg = 1;
2184b88c807SRodney W. Grimes 			break;
2194b88c807SRodney W. Grimes 		case 'r':
2204b88c807SRodney W. Grimes 			rflg = 1;
2214b88c807SRodney W. Grimes 			break;
2224b88c807SRodney W. Grimes 		case 's':
2234b88c807SRodney W. Grimes 			sflg = 1;
2244b88c807SRodney W. Grimes 			break;
2254b88c807SRodney W. Grimes 		}
2264b88c807SRodney W. Grimes 
227685a2705SJilles Tjoelker 	savehandler = handler;
2284b88c807SRodney W. Grimes 	/*
2294b88c807SRodney W. Grimes 	 * If executing...
2304b88c807SRodney W. Grimes 	 */
2314b88c807SRodney W. Grimes 	if (lflg == 0 || editor || sflg) {
2324b88c807SRodney W. Grimes 		lflg = 0;	/* ignore */
233224fbf9fSJilles Tjoelker 		editfile = NULL;
2344b88c807SRodney W. Grimes 		/*
2354b88c807SRodney W. Grimes 		 * Catch interrupts to reset active counter and
2364b88c807SRodney W. Grimes 		 * cleanup temp files.
2374b88c807SRodney W. Grimes 		 */
2384b88c807SRodney W. Grimes 		if (setjmp(jmploc.loc)) {
2394b88c807SRodney W. Grimes 			active = 0;
240224fbf9fSJilles Tjoelker 			if (editfile)
2414b88c807SRodney W. Grimes 				unlink(editfile);
2424b88c807SRodney W. Grimes 			handler = savehandler;
2434b88c807SRodney W. Grimes 			longjmp(handler->loc, 1);
2444b88c807SRodney W. Grimes 		}
2454b88c807SRodney W. Grimes 		handler = &jmploc;
2464b88c807SRodney W. Grimes 		if (++active > MAXHISTLOOPS) {
2474b88c807SRodney W. Grimes 			active = 0;
2484b88c807SRodney W. Grimes 			displayhist = 0;
2494b88c807SRodney W. Grimes 			error("called recursively too many times");
2504b88c807SRodney W. Grimes 		}
2514b88c807SRodney W. Grimes 		/*
2524b88c807SRodney W. Grimes 		 * Set editor.
2534b88c807SRodney W. Grimes 		 */
2544b88c807SRodney W. Grimes 		if (sflg == 0) {
2554b88c807SRodney W. Grimes 			if (editor == NULL &&
2564b88c807SRodney W. Grimes 			    (editor = bltinlookup("FCEDIT", 1)) == NULL &&
2574b88c807SRodney W. Grimes 			    (editor = bltinlookup("EDITOR", 1)) == NULL)
2584b88c807SRodney W. Grimes 				editor = DEFEDITOR;
2594b88c807SRodney W. Grimes 			if (editor[0] == '-' && editor[1] == '\0') {
2604b88c807SRodney W. Grimes 				sflg = 1;	/* no edit */
2614b88c807SRodney W. Grimes 				editor = NULL;
2624b88c807SRodney W. Grimes 			}
2634b88c807SRodney W. Grimes 		}
2644b88c807SRodney W. Grimes 	}
2654b88c807SRodney W. Grimes 
2664b88c807SRodney W. Grimes 	/*
2674b88c807SRodney W. Grimes 	 * If executing, parse [old=new] now
2684b88c807SRodney W. Grimes 	 */
2697cbda738SJilles Tjoelker 	if (lflg == 0 && *argptr != NULL &&
2707cbda738SJilles Tjoelker 	     ((repl = strchr(*argptr, '=')) != NULL)) {
2717cbda738SJilles Tjoelker 		pat = *argptr;
2724b88c807SRodney W. Grimes 		*repl++ = '\0';
2737cbda738SJilles Tjoelker 		argptr++;
2744b88c807SRodney W. Grimes 	}
2754b88c807SRodney W. Grimes 	/*
2764b88c807SRodney W. Grimes 	 * determine [first] and [last]
2774b88c807SRodney W. Grimes 	 */
2787cbda738SJilles Tjoelker 	if (*argptr == NULL) {
2794b88c807SRodney W. Grimes 		firststr = lflg ? "-16" : "-1";
2804b88c807SRodney W. Grimes 		laststr = "-1";
2817cbda738SJilles Tjoelker 	} else if (argptr[1] == NULL) {
2827cbda738SJilles Tjoelker 		firststr = argptr[0];
2837cbda738SJilles Tjoelker 		laststr = lflg ? "-1" : argptr[0];
2847cbda738SJilles Tjoelker 	} else if (argptr[2] == NULL) {
2857cbda738SJilles Tjoelker 		firststr = argptr[0];
2867cbda738SJilles Tjoelker 		laststr = argptr[1];
2877cbda738SJilles Tjoelker 	} else
288274110dfSJilles Tjoelker 		error("too many arguments");
2894b88c807SRodney W. Grimes 	/*
2904b88c807SRodney W. Grimes 	 * Turn into event numbers.
2914b88c807SRodney W. Grimes 	 */
2924b88c807SRodney W. Grimes 	first = str_to_event(firststr, 0);
2934b88c807SRodney W. Grimes 	last = str_to_event(laststr, 1);
2944b88c807SRodney W. Grimes 
2954b88c807SRodney W. Grimes 	if (rflg) {
2964b88c807SRodney W. Grimes 		i = last;
2974b88c807SRodney W. Grimes 		last = first;
2984b88c807SRodney W. Grimes 		first = i;
2994b88c807SRodney W. Grimes 	}
3004b88c807SRodney W. Grimes 	/*
3014b88c807SRodney W. Grimes 	 * XXX - this should not depend on the event numbers
3024b88c807SRodney W. Grimes 	 * always increasing.  Add sequence numbers or offset
3034b88c807SRodney W. Grimes 	 * to the history element in next (diskbased) release.
3044b88c807SRodney W. Grimes 	 */
3054b88c807SRodney W. Grimes 	direction = first < last ? H_PREV : H_NEXT;
3064b88c807SRodney W. Grimes 
3074b88c807SRodney W. Grimes 	/*
3084b88c807SRodney W. Grimes 	 * If editing, grab a temp file.
3094b88c807SRodney W. Grimes 	 */
3104b88c807SRodney W. Grimes 	if (editor) {
3114b88c807SRodney W. Grimes 		int fd;
3124b88c807SRodney W. Grimes 		INTOFF;		/* easier */
313224fbf9fSJilles Tjoelker 		sprintf(editfilestr, "%s/_shXXXXXX", _PATH_TMP);
314224fbf9fSJilles Tjoelker 		if ((fd = mkstemp(editfilestr)) < 0)
3154b88c807SRodney W. Grimes 			error("can't create temporary file %s", editfile);
316224fbf9fSJilles Tjoelker 		editfile = editfilestr;
3174b88c807SRodney W. Grimes 		if ((efp = fdopen(fd, "w")) == NULL) {
3184b88c807SRodney W. Grimes 			close(fd);
319274110dfSJilles Tjoelker 			error("Out of space");
3204b88c807SRodney W. Grimes 		}
3214b88c807SRodney W. Grimes 	}
3224b88c807SRodney W. Grimes 
3234b88c807SRodney W. Grimes 	/*
3244b88c807SRodney W. Grimes 	 * Loop through selected history events.  If listing or executing,
3254b88c807SRodney W. Grimes 	 * do it now.  Otherwise, put into temp file and call the editor
3264b88c807SRodney W. Grimes 	 * after.
3274b88c807SRodney W. Grimes 	 *
3284b88c807SRodney W. Grimes 	 * The history interface needs rethinking, as the following
3294b88c807SRodney W. Grimes 	 * convolutions will demonstrate.
3304b88c807SRodney W. Grimes 	 */
331757eeda0SDavid E. O'Brien 	history(hist, &he, H_FIRST);
332757eeda0SDavid E. O'Brien 	retval = history(hist, &he, H_NEXT_EVENT, first);
333757eeda0SDavid E. O'Brien 	for (;retval != -1; retval = history(hist, &he, direction)) {
3344b88c807SRodney W. Grimes 		if (lflg) {
3354b88c807SRodney W. Grimes 			if (!nflg)
336757eeda0SDavid E. O'Brien 				out1fmt("%5d ", he.num);
337757eeda0SDavid E. O'Brien 			out1str(he.str);
3384b88c807SRodney W. Grimes 		} else {
33922afca9bSJilles Tjoelker 			const char *s = pat ?
34022afca9bSJilles Tjoelker 			   fc_replace(he.str, pat, repl) : he.str;
3414b88c807SRodney W. Grimes 
3424b88c807SRodney W. Grimes 			if (sflg) {
3434b88c807SRodney W. Grimes 				if (displayhist) {
3444b88c807SRodney W. Grimes 					out2str(s);
345c6204d4aSJilles Tjoelker 					flushout(out2);
3464b88c807SRodney W. Grimes 				}
347cb806389SStefan Farfeleder 				evalstring(s, 0);
3484b88c807SRodney W. Grimes 				if (displayhist && hist) {
3494b88c807SRodney W. Grimes 					/*
3504b88c807SRodney W. Grimes 					 *  XXX what about recursive and
3514b88c807SRodney W. Grimes 					 *  relative histnums.
3524b88c807SRodney W. Grimes 					 */
35332c07786STim J. Robbins 					oldhistnum = he.num;
354757eeda0SDavid E. O'Brien 					history(hist, &he, H_ENTER, s);
35532c07786STim J. Robbins 					/*
35632c07786STim J. Robbins 					 * XXX H_ENTER moves the internal
35732c07786STim J. Robbins 					 * cursor, set it back to the current
35832c07786STim J. Robbins 					 * entry.
35932c07786STim J. Robbins 					 */
360acb4eadaSJilles Tjoelker 					history(hist, &he,
36132c07786STim J. Robbins 					    H_NEXT_EVENT, oldhistnum);
3624b88c807SRodney W. Grimes 				}
3634b88c807SRodney W. Grimes 			} else
3644b88c807SRodney W. Grimes 				fputs(s, efp);
3654b88c807SRodney W. Grimes 		}
3664b88c807SRodney W. Grimes 		/*
367776fc0e9SYaroslav Tykhiy 		 * At end?  (if we were to lose last, we'd sure be
3684b88c807SRodney W. Grimes 		 * messed up).
3694b88c807SRodney W. Grimes 		 */
370757eeda0SDavid E. O'Brien 		if (he.num == last)
3714b88c807SRodney W. Grimes 			break;
3724b88c807SRodney W. Grimes 	}
3734b88c807SRodney W. Grimes 	if (editor) {
3744b88c807SRodney W. Grimes 		char *editcmd;
3754b88c807SRodney W. Grimes 
3764b88c807SRodney W. Grimes 		fclose(efp);
37779fb1e45SJilles Tjoelker 		INTON;
3784b88c807SRodney W. Grimes 		editcmd = stalloc(strlen(editor) + strlen(editfile) + 2);
3794b88c807SRodney W. Grimes 		sprintf(editcmd, "%s %s", editor, editfile);
380cb806389SStefan Farfeleder 		evalstring(editcmd, 0);	/* XXX - should use no JC command */
3814b88c807SRodney W. Grimes 		readcmdfile(editfile);	/* XXX - should read back - quick tst */
3824b88c807SRodney W. Grimes 		unlink(editfile);
3834b88c807SRodney W. Grimes 	}
3844b88c807SRodney W. Grimes 
3854b88c807SRodney W. Grimes 	if (lflg == 0 && active > 0)
3864b88c807SRodney W. Grimes 		--active;
3874b88c807SRodney W. Grimes 	if (displayhist)
3884b88c807SRodney W. Grimes 		displayhist = 0;
389685a2705SJilles Tjoelker 	handler = savehandler;
390aa9caaf6SPeter Wemm 	return 0;
3914b88c807SRodney W. Grimes }
3924b88c807SRodney W. Grimes 
39388328642SDavid E. O'Brien static char *
3945134c3f7SWarner Losh fc_replace(const char *s, char *p, char *r)
3954b88c807SRodney W. Grimes {
3964b88c807SRodney W. Grimes 	char *dest;
3974b88c807SRodney W. Grimes 	int plen = strlen(p);
3984b88c807SRodney W. Grimes 
3994b88c807SRodney W. Grimes 	STARTSTACKSTR(dest);
4004b88c807SRodney W. Grimes 	while (*s) {
4014b88c807SRodney W. Grimes 		if (*s == *p && strncmp(s, p, plen) == 0) {
4029d37e157SJilles Tjoelker 			STPUTS(r, dest);
4034b88c807SRodney W. Grimes 			s += plen;
4044b88c807SRodney W. Grimes 			*p = '\0';	/* so no more matches */
4054b88c807SRodney W. Grimes 		} else
4064b88c807SRodney W. Grimes 			STPUTC(*s++, dest);
4074b88c807SRodney W. Grimes 	}
4087cfe6941SDavid E. O'Brien 	STPUTC('\0', dest);
4094b88c807SRodney W. Grimes 	dest = grabstackstr(dest);
4104b88c807SRodney W. Grimes 
4114b88c807SRodney W. Grimes 	return (dest);
4124b88c807SRodney W. Grimes }
4134b88c807SRodney W. Grimes 
414260fc3f4SJilles Tjoelker static int
4152cac6e36SJilles Tjoelker not_fcnumber(const char *s)
4164b88c807SRodney W. Grimes {
417ab0a2172SSteve Price 	if (s == NULL)
41862730a71SSteve Price 		return (0);
4194b88c807SRodney W. Grimes 	if (*s == '-')
4204b88c807SRodney W. Grimes 		s++;
4214b88c807SRodney W. Grimes 	return (!is_number(s));
4224b88c807SRodney W. Grimes }
4234b88c807SRodney W. Grimes 
424260fc3f4SJilles Tjoelker static int
4252cac6e36SJilles Tjoelker str_to_event(const char *str, int last)
4264b88c807SRodney W. Grimes {
427757eeda0SDavid E. O'Brien 	HistEvent he;
4282cac6e36SJilles Tjoelker 	const char *s = str;
4294b88c807SRodney W. Grimes 	int relative = 0;
430757eeda0SDavid E. O'Brien 	int i, retval;
4314b88c807SRodney W. Grimes 
432757eeda0SDavid E. O'Brien 	retval = history(hist, &he, H_FIRST);
4334b88c807SRodney W. Grimes 	switch (*s) {
4344b88c807SRodney W. Grimes 	case '-':
4354b88c807SRodney W. Grimes 		relative = 1;
4364b88c807SRodney W. Grimes 		/*FALLTHROUGH*/
4374b88c807SRodney W. Grimes 	case '+':
4384b88c807SRodney W. Grimes 		s++;
4394b88c807SRodney W. Grimes 	}
4404b88c807SRodney W. Grimes 	if (is_number(s)) {
4414b88c807SRodney W. Grimes 		i = atoi(s);
4424b88c807SRodney W. Grimes 		if (relative) {
443757eeda0SDavid E. O'Brien 			while (retval != -1 && i--) {
444757eeda0SDavid E. O'Brien 				retval = history(hist, &he, H_NEXT);
4454b88c807SRodney W. Grimes 			}
446757eeda0SDavid E. O'Brien 			if (retval == -1)
447757eeda0SDavid E. O'Brien 				retval = history(hist, &he, H_LAST);
4484b88c807SRodney W. Grimes 		} else {
449757eeda0SDavid E. O'Brien 			retval = history(hist, &he, H_NEXT_EVENT, i);
450757eeda0SDavid E. O'Brien 			if (retval == -1) {
4514b88c807SRodney W. Grimes 				/*
4524b88c807SRodney W. Grimes 				 * the notion of first and last is
4534b88c807SRodney W. Grimes 				 * backwards to that of the history package
4544b88c807SRodney W. Grimes 				 */
455757eeda0SDavid E. O'Brien 				retval = history(hist, &he, last ? H_FIRST : H_LAST);
4564b88c807SRodney W. Grimes 			}
4574b88c807SRodney W. Grimes 		}
458757eeda0SDavid E. O'Brien 		if (retval == -1)
4594b88c807SRodney W. Grimes 			error("history number %s not found (internal error)",
4604b88c807SRodney W. Grimes 			       str);
4614b88c807SRodney W. Grimes 	} else {
4624b88c807SRodney W. Grimes 		/*
4634b88c807SRodney W. Grimes 		 * pattern
4644b88c807SRodney W. Grimes 		 */
465757eeda0SDavid E. O'Brien 		retval = history(hist, &he, H_PREV_STR, str);
466757eeda0SDavid E. O'Brien 		if (retval == -1)
4674b88c807SRodney W. Grimes 			error("history pattern not found: %s", str);
4684b88c807SRodney W. Grimes 	}
469757eeda0SDavid E. O'Brien 	return (he.num);
4704b88c807SRodney W. Grimes }
471088acf90STim J. Robbins 
472088acf90STim J. Robbins int
473088acf90STim J. Robbins bindcmd(int argc, char **argv)
474088acf90STim J. Robbins {
47594b793c4SJilles Tjoelker 	int ret;
47694b793c4SJilles Tjoelker 	FILE *old;
47794b793c4SJilles Tjoelker 	FILE *out;
478088acf90STim J. Robbins 
479088acf90STim J. Robbins 	if (el == NULL)
480088acf90STim J. Robbins 		error("line editing is disabled");
48194b793c4SJilles Tjoelker 
48294b793c4SJilles Tjoelker 	INTOFF;
48394b793c4SJilles Tjoelker 
48494b793c4SJilles Tjoelker 	out = out1fp();
48594b793c4SJilles Tjoelker 	if (out == NULL)
48694b793c4SJilles Tjoelker 		error("Out of space");
48794b793c4SJilles Tjoelker 
48894b793c4SJilles Tjoelker 	el_get(el, EL_GETFP, 1, &old);
48994b793c4SJilles Tjoelker 	el_set(el, EL_SETFP, 1, out);
49094b793c4SJilles Tjoelker 
49194b793c4SJilles Tjoelker 	ret = el_parse(el, argc, __DECONST(const char **, argv));
49294b793c4SJilles Tjoelker 
49394b793c4SJilles Tjoelker 	el_set(el, EL_SETFP, 1, old);
49494b793c4SJilles Tjoelker 
49594b793c4SJilles Tjoelker 	fclose(out);
49694b793c4SJilles Tjoelker 
49794b793c4SJilles Tjoelker 	INTON;
49894b793c4SJilles Tjoelker 
49994b793c4SJilles Tjoelker 	return ret;
500088acf90STim J. Robbins }
501088acf90STim J. Robbins 
50262730a71SSteve Price #else
50362730a71SSteve Price #include "error.h"
50462730a71SSteve Price 
50562730a71SSteve Price int
5065134c3f7SWarner Losh histcmd(int argc, char **argv)
50762730a71SSteve Price {
50862730a71SSteve Price 
50962730a71SSteve Price 	error("not compiled with history support");
51062730a71SSteve Price 	/*NOTREACHED*/
51162730a71SSteve Price 	return (0);
51262730a71SSteve Price }
513088acf90STim J. Robbins 
514088acf90STim J. Robbins int
515088acf90STim J. Robbins bindcmd(int argc, char **argv)
516088acf90STim J. Robbins {
517088acf90STim J. Robbins 
518088acf90STim J. Robbins 	error("not compiled with line editing support");
519088acf90STim J. Robbins 	return (0);
520088acf90STim J. Robbins }
52162730a71SSteve Price #endif
522