xref: /freebsd/bin/sh/histedit.c (revision 660045fb)
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>
42b315a729SPiotr Pawel Stefaniak #include <sys/stat.h>
43b315a729SPiotr Pawel Stefaniak #include <dirent.h>
4492e331afSWarner Losh #include <limits.h>
454b88c807SRodney W. Grimes #include <paths.h>
464b88c807SRodney W. Grimes #include <stdio.h>
47aa9caaf6SPeter Wemm #include <stdlib.h>
48aa9caaf6SPeter Wemm #include <unistd.h>
49aa9caaf6SPeter Wemm /*
50aa9caaf6SPeter Wemm  * Editline and history functions (and glue).
51aa9caaf6SPeter Wemm  */
524b88c807SRodney W. Grimes #include "shell.h"
534b88c807SRodney W. Grimes #include "parser.h"
544b88c807SRodney W. Grimes #include "var.h"
554b88c807SRodney W. Grimes #include "options.h"
56aa9caaf6SPeter Wemm #include "main.h"
57aa9caaf6SPeter Wemm #include "output.h"
584b88c807SRodney W. Grimes #include "mystring.h"
596c346639SBryan Drewery #include "builtins.h"
60aa9caaf6SPeter Wemm #ifndef NO_HISTORY
61aa9caaf6SPeter Wemm #include "myhistedit.h"
624b88c807SRodney W. Grimes #include "error.h"
63aa9caaf6SPeter Wemm #include "eval.h"
644b88c807SRodney W. Grimes #include "memalloc.h"
654b88c807SRodney W. Grimes 
664b88c807SRodney W. Grimes #define MAXHISTLOOPS	4	/* max recursions through fc */
674b88c807SRodney W. Grimes #define DEFEDITOR	"ed"	/* default editor *should* be $EDITOR */
684b88c807SRodney W. Grimes 
694b88c807SRodney W. Grimes History *hist;	/* history cookie */
704b88c807SRodney W. Grimes EditLine *el;	/* editline cookie */
714b88c807SRodney W. Grimes int displayhist;
72f91d2e21SJilles Tjoelker static FILE *el_in, *el_out;
734b88c807SRodney W. Grimes 
7488328642SDavid E. O'Brien static char *fc_replace(const char *, char *, char *);
75260fc3f4SJilles Tjoelker static int not_fcnumber(const char *);
76260fc3f4SJilles Tjoelker static int str_to_event(const char *, int);
77b315a729SPiotr Pawel Stefaniak static int comparator(const void *, const void *, void *);
78b315a729SPiotr Pawel Stefaniak static char **sh_matches(const char *, int, int);
79b315a729SPiotr Pawel Stefaniak static unsigned char sh_complete(EditLine *, int);
804b88c807SRodney W. Grimes 
814b88c807SRodney W. Grimes /*
824b88c807SRodney W. Grimes  * Set history and editing status.  Called whenever the status may
834b88c807SRodney W. Grimes  * have changed (figures out what to do).
844b88c807SRodney W. Grimes  */
85aa9caaf6SPeter Wemm void
865134c3f7SWarner Losh histedit(void)
87aa9caaf6SPeter Wemm {
884b88c807SRodney W. Grimes 
894b88c807SRodney W. Grimes #define editing (Eflag || Vflag)
904b88c807SRodney W. Grimes 
914b88c807SRodney W. Grimes 	if (iflag) {
924b88c807SRodney W. Grimes 		if (!hist) {
934b88c807SRodney W. Grimes 			/*
944b88c807SRodney W. Grimes 			 * turn history on
954b88c807SRodney W. Grimes 			 */
964b88c807SRodney W. Grimes 			INTOFF;
974b88c807SRodney W. Grimes 			hist = history_init();
984b88c807SRodney W. Grimes 			INTON;
994b88c807SRodney W. Grimes 
1004b88c807SRodney W. Grimes 			if (hist != NULL)
101ab0a2172SSteve Price 				sethistsize(histsizeval());
1024b88c807SRodney W. Grimes 			else
103c6204d4aSJilles Tjoelker 				out2fmt_flush("sh: can't initialize history\n");
1044b88c807SRodney W. Grimes 		}
1054b88c807SRodney W. Grimes 		if (editing && !el && isatty(0)) { /* && isatty(2) ??? */
1064b88c807SRodney W. Grimes 			/*
1074b88c807SRodney W. Grimes 			 * turn editing on
1084b88c807SRodney W. Grimes 			 */
109580eefdfSJilles Tjoelker 			char *term;
110580eefdfSJilles Tjoelker 
1114b88c807SRodney W. Grimes 			INTOFF;
1124b88c807SRodney W. Grimes 			if (el_in == NULL)
1134b88c807SRodney W. Grimes 				el_in = fdopen(0, "r");
1144b88c807SRodney W. Grimes 			if (el_out == NULL)
1154b88c807SRodney W. Grimes 				el_out = fdopen(2, "w");
116f91d2e21SJilles Tjoelker 			if (el_in == NULL || el_out == NULL)
1174b88c807SRodney W. Grimes 				goto bad;
118580eefdfSJilles Tjoelker 			term = lookupvar("TERM");
119580eefdfSJilles Tjoelker 			if (term)
120580eefdfSJilles Tjoelker 				setenv("TERM", term, 1);
121580eefdfSJilles Tjoelker 			else
122580eefdfSJilles Tjoelker 				unsetenv("TERM");
123f91d2e21SJilles Tjoelker 			el = el_init(arg0, el_in, el_out, el_out);
1244b88c807SRodney W. Grimes 			if (el != NULL) {
1254b88c807SRodney W. Grimes 				if (hist)
1264b88c807SRodney W. Grimes 					el_set(el, EL_HIST, history, hist);
1274b88c807SRodney W. Grimes 				el_set(el, EL_PROMPT, getprompt);
128e46b12b7SJilles Tjoelker 				el_set(el, EL_ADDFN, "sh-complete",
129e46b12b7SJilles Tjoelker 				    "Filename completion",
130b315a729SPiotr Pawel Stefaniak 				    sh_complete);
1314b88c807SRodney W. Grimes 			} else {
1324b88c807SRodney W. Grimes bad:
133c6204d4aSJilles Tjoelker 				out2fmt_flush("sh: can't initialize editing\n");
1344b88c807SRodney W. Grimes 			}
1354b88c807SRodney W. Grimes 			INTON;
1364b88c807SRodney W. Grimes 		} else if (!editing && el) {
1374b88c807SRodney W. Grimes 			INTOFF;
1384b88c807SRodney W. Grimes 			el_end(el);
1394b88c807SRodney W. Grimes 			el = NULL;
1404b88c807SRodney W. Grimes 			INTON;
1414b88c807SRodney W. Grimes 		}
1424b88c807SRodney W. Grimes 		if (el) {
1434b88c807SRodney W. Grimes 			if (Vflag)
1444b88c807SRodney W. Grimes 				el_set(el, EL_EDITOR, "vi");
145660045fbSBaptiste Daroussin 			else if (Eflag) {
1464b88c807SRodney W. Grimes 				el_set(el, EL_EDITOR, "emacs");
147660045fbSBaptiste Daroussin 				el_set(el, EL_BIND, "^R", "em-inc-search-prev", NULL);
148660045fbSBaptiste Daroussin 			}
149e46b12b7SJilles Tjoelker 			el_set(el, EL_BIND, "^I", "sh-complete", NULL);
150ecd807fbSTim J. Robbins 			el_source(el, NULL);
1514b88c807SRodney W. Grimes 		}
1524b88c807SRodney W. Grimes 	} else {
1534b88c807SRodney W. Grimes 		INTOFF;
1544b88c807SRodney W. Grimes 		if (el) {	/* no editing if not interactive */
1554b88c807SRodney W. Grimes 			el_end(el);
1564b88c807SRodney W. Grimes 			el = NULL;
1574b88c807SRodney W. Grimes 		}
1584b88c807SRodney W. Grimes 		if (hist) {
1594b88c807SRodney W. Grimes 			history_end(hist);
1604b88c807SRodney W. Grimes 			hist = NULL;
1614b88c807SRodney W. Grimes 		}
1624b88c807SRodney W. Grimes 		INTON;
1634b88c807SRodney W. Grimes 	}
1644b88c807SRodney W. Grimes }
1654b88c807SRodney W. Grimes 
166aa9caaf6SPeter Wemm 
167aa9caaf6SPeter Wemm void
1682fae4c3dSPhilippe Charnier sethistsize(const char *hs)
169aa9caaf6SPeter Wemm {
1704b88c807SRodney W. Grimes 	int histsize;
171757eeda0SDavid E. O'Brien 	HistEvent he;
1724b88c807SRodney W. Grimes 
1734b88c807SRodney W. Grimes 	if (hist != NULL) {
174ef9e6178SJilles Tjoelker 		if (hs == NULL || !is_number(hs))
1754b88c807SRodney W. Grimes 			histsize = 100;
176ef9e6178SJilles Tjoelker 		else
177ef9e6178SJilles Tjoelker 			histsize = atoi(hs);
1782110d9c3SStefan Farfeleder 		history(hist, &he, H_SETSIZE, histsize);
179933803fbSJilles Tjoelker 		history(hist, &he, H_SETUNIQUE, 1);
1804b88c807SRodney W. Grimes 	}
1814b88c807SRodney W. Grimes }
1824b88c807SRodney W. Grimes 
183580eefdfSJilles Tjoelker void
184580eefdfSJilles Tjoelker setterm(const char *term)
185580eefdfSJilles Tjoelker {
186580eefdfSJilles Tjoelker 	if (rootshell && el != NULL && term != NULL)
187580eefdfSJilles Tjoelker 		el_set(el, EL_TERMINAL, term);
188580eefdfSJilles Tjoelker }
189580eefdfSJilles Tjoelker 
190aa9caaf6SPeter Wemm int
1917cbda738SJilles Tjoelker histcmd(int argc, char **argv __unused)
1924b88c807SRodney W. Grimes {
1934b88c807SRodney W. Grimes 	int ch;
194384aedabSJilles Tjoelker 	const char *editor = NULL;
195757eeda0SDavid E. O'Brien 	HistEvent he;
1964b88c807SRodney W. Grimes 	int lflg = 0, nflg = 0, rflg = 0, sflg = 0;
197757eeda0SDavid E. O'Brien 	int i, retval;
198384aedabSJilles Tjoelker 	const char *firststr, *laststr;
1994b88c807SRodney W. Grimes 	int first, last, direction;
200384aedabSJilles Tjoelker 	char *pat = NULL, *repl = NULL;
2014b88c807SRodney W. Grimes 	static int active = 0;
2024b88c807SRodney W. Grimes 	struct jmploc jmploc;
203224fbf9fSJilles Tjoelker 	struct jmploc *savehandler;
204224fbf9fSJilles Tjoelker 	char editfilestr[PATH_MAX];
205224fbf9fSJilles Tjoelker 	char *volatile editfile;
206384aedabSJilles Tjoelker 	FILE *efp = NULL;
20732c07786STim J. Robbins 	int oldhistnum;
2084b88c807SRodney W. Grimes 
2094b88c807SRodney W. Grimes 	if (hist == NULL)
2104b88c807SRodney W. Grimes 		error("history not active");
2114b88c807SRodney W. Grimes 
2124b88c807SRodney W. Grimes 	if (argc == 1)
2134b88c807SRodney W. Grimes 		error("missing history argument");
2144b88c807SRodney W. Grimes 
2157cbda738SJilles Tjoelker 	while (not_fcnumber(*argptr) && (ch = nextopt("e:lnrs")) != '\0')
2164b88c807SRodney W. Grimes 		switch ((char)ch) {
2174b88c807SRodney W. Grimes 		case 'e':
2187cbda738SJilles Tjoelker 			editor = shoptarg;
2194b88c807SRodney W. Grimes 			break;
2204b88c807SRodney W. Grimes 		case 'l':
2214b88c807SRodney W. Grimes 			lflg = 1;
2224b88c807SRodney W. Grimes 			break;
2234b88c807SRodney W. Grimes 		case 'n':
2244b88c807SRodney W. Grimes 			nflg = 1;
2254b88c807SRodney W. Grimes 			break;
2264b88c807SRodney W. Grimes 		case 'r':
2274b88c807SRodney W. Grimes 			rflg = 1;
2284b88c807SRodney W. Grimes 			break;
2294b88c807SRodney W. Grimes 		case 's':
2304b88c807SRodney W. Grimes 			sflg = 1;
2314b88c807SRodney W. Grimes 			break;
2324b88c807SRodney W. Grimes 		}
2334b88c807SRodney W. Grimes 
234685a2705SJilles Tjoelker 	savehandler = handler;
2354b88c807SRodney W. Grimes 	/*
2364b88c807SRodney W. Grimes 	 * If executing...
2374b88c807SRodney W. Grimes 	 */
2384b88c807SRodney W. Grimes 	if (lflg == 0 || editor || sflg) {
2394b88c807SRodney W. Grimes 		lflg = 0;	/* ignore */
240224fbf9fSJilles Tjoelker 		editfile = NULL;
2414b88c807SRodney W. Grimes 		/*
2424b88c807SRodney W. Grimes 		 * Catch interrupts to reset active counter and
2434b88c807SRodney W. Grimes 		 * cleanup temp files.
2444b88c807SRodney W. Grimes 		 */
2454b88c807SRodney W. Grimes 		if (setjmp(jmploc.loc)) {
2464b88c807SRodney W. Grimes 			active = 0;
247224fbf9fSJilles Tjoelker 			if (editfile)
2484b88c807SRodney W. Grimes 				unlink(editfile);
2494b88c807SRodney W. Grimes 			handler = savehandler;
2504b88c807SRodney W. Grimes 			longjmp(handler->loc, 1);
2514b88c807SRodney W. Grimes 		}
2524b88c807SRodney W. Grimes 		handler = &jmploc;
2534b88c807SRodney W. Grimes 		if (++active > MAXHISTLOOPS) {
2544b88c807SRodney W. Grimes 			active = 0;
2554b88c807SRodney W. Grimes 			displayhist = 0;
2564b88c807SRodney W. Grimes 			error("called recursively too many times");
2574b88c807SRodney W. Grimes 		}
2584b88c807SRodney W. Grimes 		/*
2594b88c807SRodney W. Grimes 		 * Set editor.
2604b88c807SRodney W. Grimes 		 */
2614b88c807SRodney W. Grimes 		if (sflg == 0) {
2624b88c807SRodney W. Grimes 			if (editor == NULL &&
2634b88c807SRodney W. Grimes 			    (editor = bltinlookup("FCEDIT", 1)) == NULL &&
2644b88c807SRodney W. Grimes 			    (editor = bltinlookup("EDITOR", 1)) == NULL)
2654b88c807SRodney W. Grimes 				editor = DEFEDITOR;
2664b88c807SRodney W. Grimes 			if (editor[0] == '-' && editor[1] == '\0') {
2674b88c807SRodney W. Grimes 				sflg = 1;	/* no edit */
2684b88c807SRodney W. Grimes 				editor = NULL;
2694b88c807SRodney W. Grimes 			}
2704b88c807SRodney W. Grimes 		}
2714b88c807SRodney W. Grimes 	}
2724b88c807SRodney W. Grimes 
2734b88c807SRodney W. Grimes 	/*
2744b88c807SRodney W. Grimes 	 * If executing, parse [old=new] now
2754b88c807SRodney W. Grimes 	 */
2767cbda738SJilles Tjoelker 	if (lflg == 0 && *argptr != NULL &&
2777cbda738SJilles Tjoelker 	     ((repl = strchr(*argptr, '=')) != NULL)) {
2787cbda738SJilles Tjoelker 		pat = *argptr;
2794b88c807SRodney W. Grimes 		*repl++ = '\0';
2807cbda738SJilles Tjoelker 		argptr++;
2814b88c807SRodney W. Grimes 	}
2824b88c807SRodney W. Grimes 	/*
2834b88c807SRodney W. Grimes 	 * determine [first] and [last]
2844b88c807SRodney W. Grimes 	 */
2857cbda738SJilles Tjoelker 	if (*argptr == NULL) {
2864b88c807SRodney W. Grimes 		firststr = lflg ? "-16" : "-1";
2874b88c807SRodney W. Grimes 		laststr = "-1";
2887cbda738SJilles Tjoelker 	} else if (argptr[1] == NULL) {
2897cbda738SJilles Tjoelker 		firststr = argptr[0];
2907cbda738SJilles Tjoelker 		laststr = lflg ? "-1" : argptr[0];
2917cbda738SJilles Tjoelker 	} else if (argptr[2] == NULL) {
2927cbda738SJilles Tjoelker 		firststr = argptr[0];
2937cbda738SJilles Tjoelker 		laststr = argptr[1];
2947cbda738SJilles Tjoelker 	} else
295274110dfSJilles Tjoelker 		error("too many arguments");
2964b88c807SRodney W. Grimes 	/*
2974b88c807SRodney W. Grimes 	 * Turn into event numbers.
2984b88c807SRodney W. Grimes 	 */
2994b88c807SRodney W. Grimes 	first = str_to_event(firststr, 0);
3004b88c807SRodney W. Grimes 	last = str_to_event(laststr, 1);
3014b88c807SRodney W. Grimes 
3024b88c807SRodney W. Grimes 	if (rflg) {
3034b88c807SRodney W. Grimes 		i = last;
3044b88c807SRodney W. Grimes 		last = first;
3054b88c807SRodney W. Grimes 		first = i;
3064b88c807SRodney W. Grimes 	}
3074b88c807SRodney W. Grimes 	/*
3084b88c807SRodney W. Grimes 	 * XXX - this should not depend on the event numbers
3094b88c807SRodney W. Grimes 	 * always increasing.  Add sequence numbers or offset
3104b88c807SRodney W. Grimes 	 * to the history element in next (diskbased) release.
3114b88c807SRodney W. Grimes 	 */
3124b88c807SRodney W. Grimes 	direction = first < last ? H_PREV : H_NEXT;
3134b88c807SRodney W. Grimes 
3144b88c807SRodney W. Grimes 	/*
3154b88c807SRodney W. Grimes 	 * If editing, grab a temp file.
3164b88c807SRodney W. Grimes 	 */
3174b88c807SRodney W. Grimes 	if (editor) {
3184b88c807SRodney W. Grimes 		int fd;
3194b88c807SRodney W. Grimes 		INTOFF;		/* easier */
320224fbf9fSJilles Tjoelker 		sprintf(editfilestr, "%s/_shXXXXXX", _PATH_TMP);
321224fbf9fSJilles Tjoelker 		if ((fd = mkstemp(editfilestr)) < 0)
3224b88c807SRodney W. Grimes 			error("can't create temporary file %s", editfile);
323224fbf9fSJilles Tjoelker 		editfile = editfilestr;
3244b88c807SRodney W. Grimes 		if ((efp = fdopen(fd, "w")) == NULL) {
3254b88c807SRodney W. Grimes 			close(fd);
326274110dfSJilles Tjoelker 			error("Out of space");
3274b88c807SRodney W. Grimes 		}
3284b88c807SRodney W. Grimes 	}
3294b88c807SRodney W. Grimes 
3304b88c807SRodney W. Grimes 	/*
3314b88c807SRodney W. Grimes 	 * Loop through selected history events.  If listing or executing,
3324b88c807SRodney W. Grimes 	 * do it now.  Otherwise, put into temp file and call the editor
3334b88c807SRodney W. Grimes 	 * after.
3344b88c807SRodney W. Grimes 	 *
3354b88c807SRodney W. Grimes 	 * The history interface needs rethinking, as the following
3364b88c807SRodney W. Grimes 	 * convolutions will demonstrate.
3374b88c807SRodney W. Grimes 	 */
338757eeda0SDavid E. O'Brien 	history(hist, &he, H_FIRST);
339757eeda0SDavid E. O'Brien 	retval = history(hist, &he, H_NEXT_EVENT, first);
340757eeda0SDavid E. O'Brien 	for (;retval != -1; retval = history(hist, &he, direction)) {
3414b88c807SRodney W. Grimes 		if (lflg) {
3424b88c807SRodney W. Grimes 			if (!nflg)
343757eeda0SDavid E. O'Brien 				out1fmt("%5d ", he.num);
344757eeda0SDavid E. O'Brien 			out1str(he.str);
3454b88c807SRodney W. Grimes 		} else {
34622afca9bSJilles Tjoelker 			const char *s = pat ?
34722afca9bSJilles Tjoelker 			   fc_replace(he.str, pat, repl) : he.str;
3484b88c807SRodney W. Grimes 
3494b88c807SRodney W. Grimes 			if (sflg) {
3504b88c807SRodney W. Grimes 				if (displayhist) {
3514b88c807SRodney W. Grimes 					out2str(s);
352c6204d4aSJilles Tjoelker 					flushout(out2);
3534b88c807SRodney W. Grimes 				}
354cb806389SStefan Farfeleder 				evalstring(s, 0);
3554b88c807SRodney W. Grimes 				if (displayhist && hist) {
3564b88c807SRodney W. Grimes 					/*
3574b88c807SRodney W. Grimes 					 *  XXX what about recursive and
3584b88c807SRodney W. Grimes 					 *  relative histnums.
3594b88c807SRodney W. Grimes 					 */
36032c07786STim J. Robbins 					oldhistnum = he.num;
361757eeda0SDavid E. O'Brien 					history(hist, &he, H_ENTER, s);
36232c07786STim J. Robbins 					/*
36332c07786STim J. Robbins 					 * XXX H_ENTER moves the internal
36432c07786STim J. Robbins 					 * cursor, set it back to the current
36532c07786STim J. Robbins 					 * entry.
36632c07786STim J. Robbins 					 */
367acb4eadaSJilles Tjoelker 					history(hist, &he,
36832c07786STim J. Robbins 					    H_NEXT_EVENT, oldhistnum);
3694b88c807SRodney W. Grimes 				}
3704b88c807SRodney W. Grimes 			} else
3714b88c807SRodney W. Grimes 				fputs(s, efp);
3724b88c807SRodney W. Grimes 		}
3734b88c807SRodney W. Grimes 		/*
374776fc0e9SYaroslav Tykhiy 		 * At end?  (if we were to lose last, we'd sure be
3754b88c807SRodney W. Grimes 		 * messed up).
3764b88c807SRodney W. Grimes 		 */
377757eeda0SDavid E. O'Brien 		if (he.num == last)
3784b88c807SRodney W. Grimes 			break;
3794b88c807SRodney W. Grimes 	}
3804b88c807SRodney W. Grimes 	if (editor) {
3814b88c807SRodney W. Grimes 		char *editcmd;
3824b88c807SRodney W. Grimes 
3834b88c807SRodney W. Grimes 		fclose(efp);
38479fb1e45SJilles Tjoelker 		INTON;
3854b88c807SRodney W. Grimes 		editcmd = stalloc(strlen(editor) + strlen(editfile) + 2);
3864b88c807SRodney W. Grimes 		sprintf(editcmd, "%s %s", editor, editfile);
387cb806389SStefan Farfeleder 		evalstring(editcmd, 0);	/* XXX - should use no JC command */
3884b88c807SRodney W. Grimes 		readcmdfile(editfile);	/* XXX - should read back - quick tst */
3894b88c807SRodney W. Grimes 		unlink(editfile);
3904b88c807SRodney W. Grimes 	}
3914b88c807SRodney W. Grimes 
3924b88c807SRodney W. Grimes 	if (lflg == 0 && active > 0)
3934b88c807SRodney W. Grimes 		--active;
3944b88c807SRodney W. Grimes 	if (displayhist)
3954b88c807SRodney W. Grimes 		displayhist = 0;
396685a2705SJilles Tjoelker 	handler = savehandler;
397aa9caaf6SPeter Wemm 	return 0;
3984b88c807SRodney W. Grimes }
3994b88c807SRodney W. Grimes 
40088328642SDavid E. O'Brien static char *
4015134c3f7SWarner Losh fc_replace(const char *s, char *p, char *r)
4024b88c807SRodney W. Grimes {
4034b88c807SRodney W. Grimes 	char *dest;
4044b88c807SRodney W. Grimes 	int plen = strlen(p);
4054b88c807SRodney W. Grimes 
4064b88c807SRodney W. Grimes 	STARTSTACKSTR(dest);
4074b88c807SRodney W. Grimes 	while (*s) {
4084b88c807SRodney W. Grimes 		if (*s == *p && strncmp(s, p, plen) == 0) {
4099d37e157SJilles Tjoelker 			STPUTS(r, dest);
4104b88c807SRodney W. Grimes 			s += plen;
4114b88c807SRodney W. Grimes 			*p = '\0';	/* so no more matches */
4124b88c807SRodney W. Grimes 		} else
4134b88c807SRodney W. Grimes 			STPUTC(*s++, dest);
4144b88c807SRodney W. Grimes 	}
4157cfe6941SDavid E. O'Brien 	STPUTC('\0', dest);
4164b88c807SRodney W. Grimes 	dest = grabstackstr(dest);
4174b88c807SRodney W. Grimes 
4184b88c807SRodney W. Grimes 	return (dest);
4194b88c807SRodney W. Grimes }
4204b88c807SRodney W. Grimes 
421260fc3f4SJilles Tjoelker static int
4222cac6e36SJilles Tjoelker not_fcnumber(const char *s)
4234b88c807SRodney W. Grimes {
424ab0a2172SSteve Price 	if (s == NULL)
42562730a71SSteve Price 		return (0);
4264b88c807SRodney W. Grimes 	if (*s == '-')
4274b88c807SRodney W. Grimes 		s++;
4284b88c807SRodney W. Grimes 	return (!is_number(s));
4294b88c807SRodney W. Grimes }
4304b88c807SRodney W. Grimes 
431260fc3f4SJilles Tjoelker static int
4322cac6e36SJilles Tjoelker str_to_event(const char *str, int last)
4334b88c807SRodney W. Grimes {
434757eeda0SDavid E. O'Brien 	HistEvent he;
4352cac6e36SJilles Tjoelker 	const char *s = str;
4364b88c807SRodney W. Grimes 	int relative = 0;
437757eeda0SDavid E. O'Brien 	int i, retval;
4384b88c807SRodney W. Grimes 
439757eeda0SDavid E. O'Brien 	retval = history(hist, &he, H_FIRST);
4404b88c807SRodney W. Grimes 	switch (*s) {
4414b88c807SRodney W. Grimes 	case '-':
4424b88c807SRodney W. Grimes 		relative = 1;
4434b88c807SRodney W. Grimes 		/*FALLTHROUGH*/
4444b88c807SRodney W. Grimes 	case '+':
4454b88c807SRodney W. Grimes 		s++;
4464b88c807SRodney W. Grimes 	}
4474b88c807SRodney W. Grimes 	if (is_number(s)) {
4484b88c807SRodney W. Grimes 		i = atoi(s);
4494b88c807SRodney W. Grimes 		if (relative) {
450757eeda0SDavid E. O'Brien 			while (retval != -1 && i--) {
451757eeda0SDavid E. O'Brien 				retval = history(hist, &he, H_NEXT);
4524b88c807SRodney W. Grimes 			}
453757eeda0SDavid E. O'Brien 			if (retval == -1)
454757eeda0SDavid E. O'Brien 				retval = history(hist, &he, H_LAST);
4554b88c807SRodney W. Grimes 		} else {
456757eeda0SDavid E. O'Brien 			retval = history(hist, &he, H_NEXT_EVENT, i);
457757eeda0SDavid E. O'Brien 			if (retval == -1) {
4584b88c807SRodney W. Grimes 				/*
4594b88c807SRodney W. Grimes 				 * the notion of first and last is
4604b88c807SRodney W. Grimes 				 * backwards to that of the history package
4614b88c807SRodney W. Grimes 				 */
462757eeda0SDavid E. O'Brien 				retval = history(hist, &he, last ? H_FIRST : H_LAST);
4634b88c807SRodney W. Grimes 			}
4644b88c807SRodney W. Grimes 		}
465757eeda0SDavid E. O'Brien 		if (retval == -1)
4664b88c807SRodney W. Grimes 			error("history number %s not found (internal error)",
4674b88c807SRodney W. Grimes 			       str);
4684b88c807SRodney W. Grimes 	} else {
4694b88c807SRodney W. Grimes 		/*
4704b88c807SRodney W. Grimes 		 * pattern
4714b88c807SRodney W. Grimes 		 */
472757eeda0SDavid E. O'Brien 		retval = history(hist, &he, H_PREV_STR, str);
473757eeda0SDavid E. O'Brien 		if (retval == -1)
4744b88c807SRodney W. Grimes 			error("history pattern not found: %s", str);
4754b88c807SRodney W. Grimes 	}
476757eeda0SDavid E. O'Brien 	return (he.num);
4774b88c807SRodney W. Grimes }
478088acf90STim J. Robbins 
479088acf90STim J. Robbins int
480088acf90STim J. Robbins bindcmd(int argc, char **argv)
481088acf90STim J. Robbins {
48294b793c4SJilles Tjoelker 	int ret;
48394b793c4SJilles Tjoelker 	FILE *old;
48494b793c4SJilles Tjoelker 	FILE *out;
485088acf90STim J. Robbins 
486088acf90STim J. Robbins 	if (el == NULL)
487088acf90STim J. Robbins 		error("line editing is disabled");
48894b793c4SJilles Tjoelker 
48994b793c4SJilles Tjoelker 	INTOFF;
49094b793c4SJilles Tjoelker 
49194b793c4SJilles Tjoelker 	out = out1fp();
49294b793c4SJilles Tjoelker 	if (out == NULL)
49394b793c4SJilles Tjoelker 		error("Out of space");
49494b793c4SJilles Tjoelker 
49594b793c4SJilles Tjoelker 	el_get(el, EL_GETFP, 1, &old);
49694b793c4SJilles Tjoelker 	el_set(el, EL_SETFP, 1, out);
49794b793c4SJilles Tjoelker 
49894b793c4SJilles Tjoelker 	ret = el_parse(el, argc, __DECONST(const char **, argv));
49994b793c4SJilles Tjoelker 
50094b793c4SJilles Tjoelker 	el_set(el, EL_SETFP, 1, old);
50194b793c4SJilles Tjoelker 
50294b793c4SJilles Tjoelker 	fclose(out);
50394b793c4SJilles Tjoelker 
50494b793c4SJilles Tjoelker 	INTON;
50594b793c4SJilles Tjoelker 
50694b793c4SJilles Tjoelker 	return ret;
507088acf90STim J. Robbins }
508088acf90STim J. Robbins 
50962730a71SSteve Price #else
51062730a71SSteve Price #include "error.h"
51162730a71SSteve Price 
51262730a71SSteve Price int
5136c346639SBryan Drewery histcmd(int argc __unused, char **argv __unused)
51462730a71SSteve Price {
51562730a71SSteve Price 
51662730a71SSteve Price 	error("not compiled with history support");
51762730a71SSteve Price 	/*NOTREACHED*/
51862730a71SSteve Price 	return (0);
51962730a71SSteve Price }
520088acf90STim J. Robbins 
521088acf90STim J. Robbins int
5226c346639SBryan Drewery bindcmd(int argc __unused, char **argv __unused)
523088acf90STim J. Robbins {
524088acf90STim J. Robbins 
525088acf90STim J. Robbins 	error("not compiled with line editing support");
526088acf90STim J. Robbins 	return (0);
527088acf90STim J. Robbins }
52862730a71SSteve Price #endif
529b315a729SPiotr Pawel Stefaniak 
530b315a729SPiotr Pawel Stefaniak /*
531b315a729SPiotr Pawel Stefaniak  * Comparator function for qsort(). The use of curpos here is to skip
532b315a729SPiotr Pawel Stefaniak  * characters that we already know to compare equal (common prefix).
533b315a729SPiotr Pawel Stefaniak  */
534b315a729SPiotr Pawel Stefaniak static int
535b315a729SPiotr Pawel Stefaniak comparator(const void *a, const void *b, void *thunk)
536b315a729SPiotr Pawel Stefaniak {
537b315a729SPiotr Pawel Stefaniak 	size_t curpos = (intptr_t)thunk;
538b315a729SPiotr Pawel Stefaniak 	return (strcmp(*(char *const *)a + curpos,
539b315a729SPiotr Pawel Stefaniak 		*(char *const *)b + curpos));
540b315a729SPiotr Pawel Stefaniak }
541b315a729SPiotr Pawel Stefaniak 
542b315a729SPiotr Pawel Stefaniak /*
543b315a729SPiotr Pawel Stefaniak  * This function is passed to libedit's fn_complete2(). The library will
544b315a729SPiotr Pawel Stefaniak  * use it instead of its standard function that finds matching files in
545b315a729SPiotr Pawel Stefaniak  * current directory. If we're at the start of the line, we want to look
546b315a729SPiotr Pawel Stefaniak  * for available commands from all paths in $PATH.
547b315a729SPiotr Pawel Stefaniak  */
548b315a729SPiotr Pawel Stefaniak static char
549b315a729SPiotr Pawel Stefaniak **sh_matches(const char *text, int start, int end)
550b315a729SPiotr Pawel Stefaniak {
551b315a729SPiotr Pawel Stefaniak 	char *free_path = NULL, *path;
552b315a729SPiotr Pawel Stefaniak 	const char *dirname;
553b315a729SPiotr Pawel Stefaniak 	char **matches = NULL;
554b315a729SPiotr Pawel Stefaniak 	size_t i = 0, size = 16, j, k;
555b315a729SPiotr Pawel Stefaniak 	size_t curpos = end - start;
556b315a729SPiotr Pawel Stefaniak 
557b315a729SPiotr Pawel Stefaniak 	if (start > 0 || memchr("/.~", text[0], 3) != NULL)
558b315a729SPiotr Pawel Stefaniak 		return (NULL);
559b315a729SPiotr Pawel Stefaniak 	if ((free_path = path = strdup(pathval())) == NULL)
560b315a729SPiotr Pawel Stefaniak 		goto out;
561b315a729SPiotr Pawel Stefaniak 	if ((matches = malloc(size * sizeof(matches[0]))) == NULL)
562b315a729SPiotr Pawel Stefaniak 		goto out;
563b315a729SPiotr Pawel Stefaniak 	while ((dirname = strsep(&path, ":")) != NULL) {
564b315a729SPiotr Pawel Stefaniak 		struct dirent *entry;
565b315a729SPiotr Pawel Stefaniak 		DIR *dir;
566b315a729SPiotr Pawel Stefaniak 		int dfd;
567b315a729SPiotr Pawel Stefaniak 
568b315a729SPiotr Pawel Stefaniak 		dir = opendir(dirname[0] == '\0' ? "." : dirname);
569b315a729SPiotr Pawel Stefaniak 		if (dir == NULL)
570b315a729SPiotr Pawel Stefaniak 			continue;
571b315a729SPiotr Pawel Stefaniak 		if ((dfd = dirfd(dir)) == -1) {
572b315a729SPiotr Pawel Stefaniak 			closedir(dir);
573b315a729SPiotr Pawel Stefaniak 			continue;
574b315a729SPiotr Pawel Stefaniak 		}
575b315a729SPiotr Pawel Stefaniak 		while ((entry = readdir(dir)) != NULL) {
576b315a729SPiotr Pawel Stefaniak 			struct stat statb;
577b315a729SPiotr Pawel Stefaniak 			char **rmatches;
578b315a729SPiotr Pawel Stefaniak 
579b315a729SPiotr Pawel Stefaniak 			if (strncmp(entry->d_name, text, curpos) != 0)
580b315a729SPiotr Pawel Stefaniak 				continue;
581b315a729SPiotr Pawel Stefaniak 			if (entry->d_type == DT_UNKNOWN || entry->d_type == DT_LNK) {
582b315a729SPiotr Pawel Stefaniak 				if (fstatat(dfd, entry->d_name, &statb, 0) == -1)
583b315a729SPiotr Pawel Stefaniak 					continue;
584b315a729SPiotr Pawel Stefaniak 				if (!S_ISREG(statb.st_mode))
585b315a729SPiotr Pawel Stefaniak 					continue;
586b315a729SPiotr Pawel Stefaniak 			} else if (entry->d_type != DT_REG)
587b315a729SPiotr Pawel Stefaniak 				continue;
588b315a729SPiotr Pawel Stefaniak 			matches[++i] = strdup(entry->d_name);
589b315a729SPiotr Pawel Stefaniak 			if (i < size - 1)
590b315a729SPiotr Pawel Stefaniak 				continue;
591b315a729SPiotr Pawel Stefaniak 			size *= 2;
592b315a729SPiotr Pawel Stefaniak 			rmatches = reallocarray(matches, size, sizeof(matches[0]));
593b315a729SPiotr Pawel Stefaniak 			if (rmatches == NULL) {
594b315a729SPiotr Pawel Stefaniak 				closedir(dir);
595b315a729SPiotr Pawel Stefaniak 				goto out;
596b315a729SPiotr Pawel Stefaniak 			}
597b315a729SPiotr Pawel Stefaniak 			matches = rmatches;
598b315a729SPiotr Pawel Stefaniak 		}
599b315a729SPiotr Pawel Stefaniak 		closedir(dir);
600b315a729SPiotr Pawel Stefaniak 	}
601b315a729SPiotr Pawel Stefaniak out:
602b315a729SPiotr Pawel Stefaniak 	free(free_path);
603b315a729SPiotr Pawel Stefaniak 	/*
604b315a729SPiotr Pawel Stefaniak 	 * matches[0] is special: it's not a real matching file name but a common
605b315a729SPiotr Pawel Stefaniak 	 * prefix for all matching names. It can't be null, unlike any other
606b315a729SPiotr Pawel Stefaniak 	 * element of the array. When strings matches[0] and matches[1] compare
607b315a729SPiotr Pawel Stefaniak 	 * equal and matches[2] is null that means to libedit that there is only
608b315a729SPiotr Pawel Stefaniak 	 * a single match. It will then replace user input with possibly escaped
609b315a729SPiotr Pawel Stefaniak 	 * string in matches[0] which is the reason to copy the full name of the
610b315a729SPiotr Pawel Stefaniak 	 * only match.
611b315a729SPiotr Pawel Stefaniak 	 */
612b315a729SPiotr Pawel Stefaniak 	if (i == 0) {
613b315a729SPiotr Pawel Stefaniak 		free(matches);
614b315a729SPiotr Pawel Stefaniak 		return (NULL);
615b315a729SPiotr Pawel Stefaniak 	} else if (i == 1) {
616b315a729SPiotr Pawel Stefaniak 		matches[0] = strdup(matches[1]);
617b315a729SPiotr Pawel Stefaniak 		matches[2] = NULL;
618b315a729SPiotr Pawel Stefaniak 		if (matches[0] != NULL)
619b315a729SPiotr Pawel Stefaniak 			return (matches);
620b315a729SPiotr Pawel Stefaniak 	} else
621b315a729SPiotr Pawel Stefaniak 		matches[0] = strdup(text);
622b315a729SPiotr Pawel Stefaniak 	if (matches[0] == NULL) {
623b315a729SPiotr Pawel Stefaniak 		for (j = 1; j <= i; j++)
624b315a729SPiotr Pawel Stefaniak 			free(matches[j]);
625b315a729SPiotr Pawel Stefaniak 		free(matches);
626b315a729SPiotr Pawel Stefaniak 		return (NULL);
627b315a729SPiotr Pawel Stefaniak 	}
628b315a729SPiotr Pawel Stefaniak 	qsort_s(matches + 1, i, sizeof(matches[0]), comparator,
629b315a729SPiotr Pawel Stefaniak 		(void *)(intptr_t)curpos);
630b315a729SPiotr Pawel Stefaniak 	for (j = 1, k = 2; k <= i; k++)
631b315a729SPiotr Pawel Stefaniak 		if (strcmp(matches[j] + curpos, matches[k] + curpos) == 0)
632b315a729SPiotr Pawel Stefaniak 			free(matches[k]);
633b315a729SPiotr Pawel Stefaniak 		else
634b315a729SPiotr Pawel Stefaniak 			matches[++j] = matches[k];
635b315a729SPiotr Pawel Stefaniak 	matches[j + 1] = NULL;
636b315a729SPiotr Pawel Stefaniak 	return (matches);
637b315a729SPiotr Pawel Stefaniak }
638b315a729SPiotr Pawel Stefaniak 
639b315a729SPiotr Pawel Stefaniak /*
640b315a729SPiotr Pawel Stefaniak  * This is passed to el_set(el, EL_ADDFN, ...) so that it's possible to
641b315a729SPiotr Pawel Stefaniak  * bind a key (tab by default) to execute the function.
642b315a729SPiotr Pawel Stefaniak  */
643b315a729SPiotr Pawel Stefaniak unsigned char
644b315a729SPiotr Pawel Stefaniak sh_complete(EditLine *sel, int ch __unused)
645b315a729SPiotr Pawel Stefaniak {
646b315a729SPiotr Pawel Stefaniak 	return (unsigned char)fn_complete2(sel, NULL, sh_matches,
647b315a729SPiotr Pawel Stefaniak 		L" \t\n\"\\'`@$><=;|&{(", NULL, NULL, (size_t)100,
648b315a729SPiotr Pawel Stefaniak 		NULL, &((int) {0}), NULL, NULL, FN_QUOTE_MATCH);
649b315a729SPiotr Pawel Stefaniak }
650