histedit.c revision 199629
11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1993
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * This code is derived from software contributed to Berkeley by
61556Srgrimes * Kenneth Almquist.
71556Srgrimes *
81556Srgrimes * Redistribution and use in source and binary forms, with or without
91556Srgrimes * modification, are permitted provided that the following conditions
101556Srgrimes * are met:
111556Srgrimes * 1. Redistributions of source code must retain the above copyright
121556Srgrimes *    notice, this list of conditions and the following disclaimer.
131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141556Srgrimes *    notice, this list of conditions and the following disclaimer in the
151556Srgrimes *    documentation and/or other materials provided with the distribution.
161556Srgrimes * 4. Neither the name of the University nor the names of its contributors
171556Srgrimes *    may be used to endorse or promote products derived from this software
181556Srgrimes *    without specific prior written permission.
191556Srgrimes *
201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301556Srgrimes * SUCH DAMAGE.
311556Srgrimes */
321556Srgrimes
331556Srgrimes#ifndef lint
3436150Scharnier#if 0
3536150Scharnierstatic char sccsid[] = "@(#)histedit.c	8.2 (Berkeley) 5/4/95";
3636150Scharnier#endif
371556Srgrimes#endif /* not lint */
3899110Sobrien#include <sys/cdefs.h>
3999110Sobrien__FBSDID("$FreeBSD: head/bin/sh/histedit.c 199629 2009-11-21 14:28:32Z jilles $");
401556Srgrimes
4117987Speter#include <sys/param.h>
4277463Simp#include <limits.h>
4317987Speter#include <paths.h>
4417987Speter#include <stdio.h>
4517987Speter#include <stdlib.h>
4617987Speter#include <unistd.h>
471556Srgrimes/*
481556Srgrimes * Editline and history functions (and glue).
491556Srgrimes */
501556Srgrimes#include "shell.h"
511556Srgrimes#include "parser.h"
521556Srgrimes#include "var.h"
531556Srgrimes#include "options.h"
5417987Speter#include "main.h"
5517987Speter#include "output.h"
561556Srgrimes#include "mystring.h"
5717987Speter#ifndef NO_HISTORY
5817987Speter#include "myhistedit.h"
591556Srgrimes#include "error.h"
6017987Speter#include "eval.h"
611556Srgrimes#include "memalloc.h"
621556Srgrimes
631556Srgrimes#define MAXHISTLOOPS	4	/* max recursions through fc */
641556Srgrimes#define DEFEDITOR	"ed"	/* default editor *should* be $EDITOR */
651556Srgrimes
661556SrgrimesHistory *hist;	/* history cookie */
671556SrgrimesEditLine *el;	/* editline cookie */
681556Srgrimesint displayhist;
6984261Sobrienstatic FILE *el_in, *el_out, *el_err;
701556Srgrimes
7190111SimpSTATIC char *fc_replace(const char *, char *, char *);
721556Srgrimes
731556Srgrimes/*
741556Srgrimes * Set history and editing status.  Called whenever the status may
751556Srgrimes * have changed (figures out what to do).
761556Srgrimes */
7717987Spetervoid
7890111Simphistedit(void)
7917987Speter{
801556Srgrimes
811556Srgrimes#define editing (Eflag || Vflag)
821556Srgrimes
831556Srgrimes	if (iflag) {
841556Srgrimes		if (!hist) {
851556Srgrimes			/*
861556Srgrimes			 * turn history on
871556Srgrimes			 */
881556Srgrimes			INTOFF;
891556Srgrimes			hist = history_init();
901556Srgrimes			INTON;
911556Srgrimes
921556Srgrimes			if (hist != NULL)
9320425Ssteve				sethistsize(histsizeval());
941556Srgrimes			else
95199629Sjilles				out2fmt_flush("sh: can't initialize history\n");
961556Srgrimes		}
971556Srgrimes		if (editing && !el && isatty(0)) { /* && isatty(2) ??? */
981556Srgrimes			/*
991556Srgrimes			 * turn editing on
1001556Srgrimes			 */
1011556Srgrimes			INTOFF;
1021556Srgrimes			if (el_in == NULL)
1031556Srgrimes				el_in = fdopen(0, "r");
10484261Sobrien			if (el_err == NULL)
10584261Sobrien				el_err = fdopen(1, "w");
1061556Srgrimes			if (el_out == NULL)
1071556Srgrimes				el_out = fdopen(2, "w");
10884261Sobrien			if (el_in == NULL || el_err == NULL || el_out == NULL)
1091556Srgrimes				goto bad;
11084261Sobrien			el = el_init(arg0, el_in, el_out, el_err);
1111556Srgrimes			if (el != NULL) {
1121556Srgrimes				if (hist)
1131556Srgrimes					el_set(el, EL_HIST, history, hist);
1141556Srgrimes				el_set(el, EL_PROMPT, getprompt);
1151556Srgrimes			} else {
1161556Srgrimesbad:
117199629Sjilles				out2fmt_flush("sh: can't initialize editing\n");
1181556Srgrimes			}
1191556Srgrimes			INTON;
1201556Srgrimes		} else if (!editing && el) {
1211556Srgrimes			INTOFF;
1221556Srgrimes			el_end(el);
1231556Srgrimes			el = NULL;
1241556Srgrimes			INTON;
1251556Srgrimes		}
1261556Srgrimes		if (el) {
1271556Srgrimes			if (Vflag)
1281556Srgrimes				el_set(el, EL_EDITOR, "vi");
1291556Srgrimes			else if (Eflag)
1301556Srgrimes				el_set(el, EL_EDITOR, "emacs");
131100568Stjr			el_source(el, NULL);
1321556Srgrimes		}
1331556Srgrimes	} else {
1341556Srgrimes		INTOFF;
1351556Srgrimes		if (el) {	/* no editing if not interactive */
1361556Srgrimes			el_end(el);
1371556Srgrimes			el = NULL;
1381556Srgrimes		}
1391556Srgrimes		if (hist) {
1401556Srgrimes			history_end(hist);
1411556Srgrimes			hist = NULL;
1421556Srgrimes		}
1431556Srgrimes		INTON;
1441556Srgrimes	}
1451556Srgrimes}
1461556Srgrimes
14717987Speter
14817987Spetervoid
14920425Sstevesethistsize(hs)
15020425Ssteve	const char *hs;
15117987Speter{
1521556Srgrimes	int histsize;
15384261Sobrien	HistEvent he;
1541556Srgrimes
1551556Srgrimes	if (hist != NULL) {
15620425Ssteve		if (hs == NULL || *hs == '\0' ||
15720425Ssteve		   (histsize = atoi(hs)) < 0)
1581556Srgrimes			histsize = 100;
159151471Sstefanf		history(hist, &he, H_SETSIZE, histsize);
1601556Srgrimes	}
1611556Srgrimes}
1621556Srgrimes
16317987Speterint
16490111Simphistcmd(int argc, char **argv)
1651556Srgrimes{
1661556Srgrimes	int ch;
1671556Srgrimes	char *editor = NULL;
16884261Sobrien	HistEvent he;
1691556Srgrimes	int lflg = 0, nflg = 0, rflg = 0, sflg = 0;
17084261Sobrien	int i, retval;
1711556Srgrimes	char *firststr, *laststr;
1721556Srgrimes	int first, last, direction;
173106192Stjr	char *pat = NULL, *repl;
1741556Srgrimes	static int active = 0;
1751556Srgrimes	struct jmploc jmploc;
176194765Sjilles	struct jmploc *savehandler;
177194765Sjilles	char editfilestr[PATH_MAX];
178194765Sjilles	char *volatile editfile;
1791556Srgrimes	FILE *efp;
18097730Stjr	int oldhistnum;
1811556Srgrimes
1821556Srgrimes	if (hist == NULL)
1831556Srgrimes		error("history not active");
1848855Srgrimes
1851556Srgrimes	if (argc == 1)
1861556Srgrimes		error("missing history argument");
1871556Srgrimes
1881556Srgrimes	optreset = 1; optind = 1; /* initialize getopt */
189100663Stjr	opterr = 0;
1901556Srgrimes	while (not_fcnumber(argv[optind]) &&
19124348Simp	      (ch = getopt(argc, argv, ":e:lnrs")) != -1)
1921556Srgrimes		switch ((char)ch) {
1931556Srgrimes		case 'e':
19497731Stjr			editor = optarg;
1951556Srgrimes			break;
1961556Srgrimes		case 'l':
1971556Srgrimes			lflg = 1;
1981556Srgrimes			break;
1991556Srgrimes		case 'n':
2001556Srgrimes			nflg = 1;
2011556Srgrimes			break;
2021556Srgrimes		case 'r':
2031556Srgrimes			rflg = 1;
2041556Srgrimes			break;
2051556Srgrimes		case 's':
2061556Srgrimes			sflg = 1;
2071556Srgrimes			break;
2081556Srgrimes		case ':':
2091556Srgrimes			error("option -%c expects argument", optopt);
2101556Srgrimes		case '?':
2111556Srgrimes		default:
2121556Srgrimes			error("unknown option: -%c", optopt);
2131556Srgrimes		}
2141556Srgrimes	argc -= optind, argv += optind;
2151556Srgrimes
2161556Srgrimes	/*
2171556Srgrimes	 * If executing...
2181556Srgrimes	 */
2191556Srgrimes	if (lflg == 0 || editor || sflg) {
2201556Srgrimes		lflg = 0;	/* ignore */
221194765Sjilles		editfile = NULL;
2221556Srgrimes		/*
2231556Srgrimes		 * Catch interrupts to reset active counter and
2241556Srgrimes		 * cleanup temp files.
2251556Srgrimes		 */
226194765Sjilles		savehandler = handler;
2271556Srgrimes		if (setjmp(jmploc.loc)) {
2281556Srgrimes			active = 0;
229194765Sjilles			if (editfile)
2301556Srgrimes				unlink(editfile);
2311556Srgrimes			handler = savehandler;
2321556Srgrimes			longjmp(handler->loc, 1);
2331556Srgrimes		}
2341556Srgrimes		handler = &jmploc;
2351556Srgrimes		if (++active > MAXHISTLOOPS) {
2361556Srgrimes			active = 0;
2371556Srgrimes			displayhist = 0;
2381556Srgrimes			error("called recursively too many times");
2391556Srgrimes		}
2401556Srgrimes		/*
2411556Srgrimes		 * Set editor.
2421556Srgrimes		 */
2431556Srgrimes		if (sflg == 0) {
2441556Srgrimes			if (editor == NULL &&
2451556Srgrimes			    (editor = bltinlookup("FCEDIT", 1)) == NULL &&
2461556Srgrimes			    (editor = bltinlookup("EDITOR", 1)) == NULL)
2471556Srgrimes				editor = DEFEDITOR;
2481556Srgrimes			if (editor[0] == '-' && editor[1] == '\0') {
2491556Srgrimes				sflg = 1;	/* no edit */
2501556Srgrimes				editor = NULL;
2511556Srgrimes			}
2521556Srgrimes		}
2531556Srgrimes	}
2541556Srgrimes
2551556Srgrimes	/*
2561556Srgrimes	 * If executing, parse [old=new] now
2571556Srgrimes	 */
2588855Srgrimes	if (lflg == 0 && argc > 0 &&
2591556Srgrimes	     ((repl = strchr(argv[0], '=')) != NULL)) {
2601556Srgrimes		pat = argv[0];
2611556Srgrimes		*repl++ = '\0';
2621556Srgrimes		argc--, argv++;
2631556Srgrimes	}
2641556Srgrimes	/*
2651556Srgrimes	 * determine [first] and [last]
2661556Srgrimes	 */
2671556Srgrimes	switch (argc) {
2681556Srgrimes	case 0:
2691556Srgrimes		firststr = lflg ? "-16" : "-1";
2701556Srgrimes		laststr = "-1";
2711556Srgrimes		break;
2721556Srgrimes	case 1:
2731556Srgrimes		firststr = argv[0];
2741556Srgrimes		laststr = lflg ? "-1" : argv[0];
2751556Srgrimes		break;
2761556Srgrimes	case 2:
2771556Srgrimes		firststr = argv[0];
2781556Srgrimes		laststr = argv[1];
2791556Srgrimes		break;
2801556Srgrimes	default:
2811556Srgrimes		error("too many args");
2821556Srgrimes	}
2831556Srgrimes	/*
2841556Srgrimes	 * Turn into event numbers.
2851556Srgrimes	 */
2861556Srgrimes	first = str_to_event(firststr, 0);
2871556Srgrimes	last = str_to_event(laststr, 1);
2881556Srgrimes
2891556Srgrimes	if (rflg) {
2901556Srgrimes		i = last;
2911556Srgrimes		last = first;
2921556Srgrimes		first = i;
2931556Srgrimes	}
2941556Srgrimes	/*
2951556Srgrimes	 * XXX - this should not depend on the event numbers
2968855Srgrimes	 * always increasing.  Add sequence numbers or offset
2971556Srgrimes	 * to the history element in next (diskbased) release.
2981556Srgrimes	 */
2991556Srgrimes	direction = first < last ? H_PREV : H_NEXT;
3001556Srgrimes
3011556Srgrimes	/*
3021556Srgrimes	 * If editing, grab a temp file.
3031556Srgrimes	 */
3041556Srgrimes	if (editor) {
3051556Srgrimes		int fd;
3061556Srgrimes		INTOFF;		/* easier */
307194765Sjilles		sprintf(editfilestr, "%s/_shXXXXXX", _PATH_TMP);
308194765Sjilles		if ((fd = mkstemp(editfilestr)) < 0)
3091556Srgrimes			error("can't create temporary file %s", editfile);
310194765Sjilles		editfile = editfilestr;
3111556Srgrimes		if ((efp = fdopen(fd, "w")) == NULL) {
3121556Srgrimes			close(fd);
31318016Speter			error("can't allocate stdio buffer for temp");
3141556Srgrimes		}
3151556Srgrimes	}
3161556Srgrimes
3171556Srgrimes	/*
3181556Srgrimes	 * Loop through selected history events.  If listing or executing,
3191556Srgrimes	 * do it now.  Otherwise, put into temp file and call the editor
3201556Srgrimes	 * after.
3211556Srgrimes	 *
3221556Srgrimes	 * The history interface needs rethinking, as the following
3231556Srgrimes	 * convolutions will demonstrate.
3241556Srgrimes	 */
32584261Sobrien	history(hist, &he, H_FIRST);
32684261Sobrien	retval = history(hist, &he, H_NEXT_EVENT, first);
32784261Sobrien	for (;retval != -1; retval = history(hist, &he, direction)) {
3281556Srgrimes		if (lflg) {
3291556Srgrimes			if (!nflg)
33084261Sobrien				out1fmt("%5d ", he.num);
33184261Sobrien			out1str(he.str);
3321556Srgrimes		} else {
3338855Srgrimes			char *s = pat ?
33484261Sobrien			   fc_replace(he.str, pat, repl) : (char *)he.str;
3351556Srgrimes
3361556Srgrimes			if (sflg) {
3371556Srgrimes				if (displayhist) {
3381556Srgrimes					out2str(s);
339199629Sjilles					flushout(out2);
3401556Srgrimes				}
341193169Sstefanf				evalstring(s, 0);
3421556Srgrimes				if (displayhist && hist) {
3431556Srgrimes					/*
3448855Srgrimes					 *  XXX what about recursive and
3451556Srgrimes					 *  relative histnums.
3461556Srgrimes					 */
34797730Stjr					oldhistnum = he.num;
34884261Sobrien					history(hist, &he, H_ENTER, s);
34997730Stjr					/*
35097730Stjr					 * XXX H_ENTER moves the internal
35197730Stjr					 * cursor, set it back to the current
35297730Stjr					 * entry.
35397730Stjr					 */
35497730Stjr					retval = history(hist, &he,
35597730Stjr					    H_NEXT_EVENT, oldhistnum);
3561556Srgrimes				}
3571556Srgrimes			} else
3581556Srgrimes				fputs(s, efp);
3591556Srgrimes		}
3601556Srgrimes		/*
361160964Syar		 * At end?  (if we were to lose last, we'd sure be
3621556Srgrimes		 * messed up).
3631556Srgrimes		 */
36484261Sobrien		if (he.num == last)
3651556Srgrimes			break;
3661556Srgrimes	}
3671556Srgrimes	if (editor) {
3681556Srgrimes		char *editcmd;
3691556Srgrimes
3701556Srgrimes		fclose(efp);
3711556Srgrimes		editcmd = stalloc(strlen(editor) + strlen(editfile) + 2);
3721556Srgrimes		sprintf(editcmd, "%s %s", editor, editfile);
373193169Sstefanf		evalstring(editcmd, 0);	/* XXX - should use no JC command */
3741556Srgrimes		INTON;
3751556Srgrimes		readcmdfile(editfile);	/* XXX - should read back - quick tst */
3761556Srgrimes		unlink(editfile);
3771556Srgrimes	}
3788855Srgrimes
3791556Srgrimes	if (lflg == 0 && active > 0)
3801556Srgrimes		--active;
3811556Srgrimes	if (displayhist)
3821556Srgrimes		displayhist = 0;
38317987Speter	return 0;
3841556Srgrimes}
3851556Srgrimes
3861556SrgrimesSTATIC char *
38790111Simpfc_replace(const char *s, char *p, char *r)
3881556Srgrimes{
3891556Srgrimes	char *dest;
3901556Srgrimes	int plen = strlen(p);
3911556Srgrimes
3921556Srgrimes	STARTSTACKSTR(dest);
3931556Srgrimes	while (*s) {
3941556Srgrimes		if (*s == *p && strncmp(s, p, plen) == 0) {
3951556Srgrimes			while (*r)
3961556Srgrimes				STPUTC(*r++, dest);
3971556Srgrimes			s += plen;
3981556Srgrimes			*p = '\0';	/* so no more matches */
3991556Srgrimes		} else
4001556Srgrimes			STPUTC(*s++, dest);
4011556Srgrimes	}
4021556Srgrimes	STACKSTRNUL(dest);
4031556Srgrimes	dest = grabstackstr(dest);
4041556Srgrimes
4051556Srgrimes	return (dest);
4061556Srgrimes}
4071556Srgrimes
40817987Speterint
40990111Simpnot_fcnumber(char *s)
4101556Srgrimes{
41120425Ssteve	if (s == NULL)
41225224Ssteve		return (0);
41320425Ssteve	if (*s == '-')
41420425Ssteve		s++;
4151556Srgrimes	return (!is_number(s));
4161556Srgrimes}
4171556Srgrimes
41817987Speterint
41990111Simpstr_to_event(char *str, int last)
4201556Srgrimes{
42184261Sobrien	HistEvent he;
4221556Srgrimes	char *s = str;
4231556Srgrimes	int relative = 0;
42484261Sobrien	int i, retval;
4251556Srgrimes
42684261Sobrien	retval = history(hist, &he, H_FIRST);
4271556Srgrimes	switch (*s) {
4281556Srgrimes	case '-':
4291556Srgrimes		relative = 1;
4301556Srgrimes		/*FALLTHROUGH*/
4311556Srgrimes	case '+':
4321556Srgrimes		s++;
4331556Srgrimes	}
4341556Srgrimes	if (is_number(s)) {
4351556Srgrimes		i = atoi(s);
4361556Srgrimes		if (relative) {
43784261Sobrien			while (retval != -1 && i--) {
43884261Sobrien				retval = history(hist, &he, H_NEXT);
4391556Srgrimes			}
44084261Sobrien			if (retval == -1)
44184261Sobrien				retval = history(hist, &he, H_LAST);
4421556Srgrimes		} else {
44384261Sobrien			retval = history(hist, &he, H_NEXT_EVENT, i);
44484261Sobrien			if (retval == -1) {
4451556Srgrimes				/*
4461556Srgrimes				 * the notion of first and last is
4471556Srgrimes				 * backwards to that of the history package
4481556Srgrimes				 */
44984261Sobrien				retval = history(hist, &he, last ? H_FIRST : H_LAST);
4501556Srgrimes			}
4511556Srgrimes		}
45284261Sobrien		if (retval == -1)
4531556Srgrimes			error("history number %s not found (internal error)",
4541556Srgrimes			       str);
4551556Srgrimes	} else {
4561556Srgrimes		/*
4578855Srgrimes		 * pattern
4581556Srgrimes		 */
45984261Sobrien		retval = history(hist, &he, H_PREV_STR, str);
46084261Sobrien		if (retval == -1)
4611556Srgrimes			error("history pattern not found: %s", str);
4621556Srgrimes	}
46384261Sobrien	return (he.num);
4641556Srgrimes}
465100565Stjr
466100565Stjrint
467100565Stjrbindcmd(int argc, char **argv)
468100565Stjr{
469100565Stjr
470100565Stjr	if (el == NULL)
471100565Stjr		error("line editing is disabled");
472148974Sstefanf	return (el_parse(el, argc, (const char **)argv));
473100565Stjr}
474100565Stjr
47525224Ssteve#else
47625224Ssteve#include "error.h"
47725224Ssteve
47825224Ssteveint
47990111Simphistcmd(int argc, char **argv)
48025224Ssteve{
48125224Ssteve
48225224Ssteve	error("not compiled with history support");
48325224Ssteve	/*NOTREACHED*/
48425224Ssteve	return (0);
48525224Ssteve}
486100565Stjr
487100565Stjrint
488100565Stjrbindcmd(int argc, char **argv)
489100565Stjr{
490100565Stjr
491100565Stjr	error("not compiled with line editing support");
492100565Stjr	return (0);
493100565Stjr}
49425224Ssteve#endif
495