histedit.c revision 148974
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 148974 2005-08-11 20:28:26Z stefanf $");
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
951556Srgrimes				out2str("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:
1171556Srgrimes				out2str("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;
15984261Sobrien		history(hist, &he, H_EVENT, 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;
1761556Srgrimes	struct jmploc *volatile savehandler;
17777463Simp	char editfile[PATH_MAX];
1781556Srgrimes	FILE *efp;
17997730Stjr	int oldhistnum;
18017987Speter#ifdef __GNUC__
18117987Speter	/* Avoid longjmp clobbering */
18217987Speter	(void) &editor;
18317987Speter	(void) &lflg;
18417987Speter	(void) &nflg;
18517987Speter	(void) &rflg;
18617987Speter	(void) &sflg;
18717987Speter	(void) &firststr;
18817987Speter	(void) &laststr;
18917987Speter	(void) &pat;
19017987Speter	(void) &repl;
19117987Speter	(void) &efp;
19217987Speter	(void) &argc;
19317987Speter	(void) &argv;
19417987Speter#endif
1951556Srgrimes
1961556Srgrimes	if (hist == NULL)
1971556Srgrimes		error("history not active");
1988855Srgrimes
1991556Srgrimes	if (argc == 1)
2001556Srgrimes		error("missing history argument");
2011556Srgrimes
2021556Srgrimes	optreset = 1; optind = 1; /* initialize getopt */
203100663Stjr	opterr = 0;
2041556Srgrimes	while (not_fcnumber(argv[optind]) &&
20524348Simp	      (ch = getopt(argc, argv, ":e:lnrs")) != -1)
2061556Srgrimes		switch ((char)ch) {
2071556Srgrimes		case 'e':
20897731Stjr			editor = optarg;
2091556Srgrimes			break;
2101556Srgrimes		case 'l':
2111556Srgrimes			lflg = 1;
2121556Srgrimes			break;
2131556Srgrimes		case 'n':
2141556Srgrimes			nflg = 1;
2151556Srgrimes			break;
2161556Srgrimes		case 'r':
2171556Srgrimes			rflg = 1;
2181556Srgrimes			break;
2191556Srgrimes		case 's':
2201556Srgrimes			sflg = 1;
2211556Srgrimes			break;
2221556Srgrimes		case ':':
2231556Srgrimes			error("option -%c expects argument", optopt);
2241556Srgrimes		case '?':
2251556Srgrimes		default:
2261556Srgrimes			error("unknown option: -%c", optopt);
2271556Srgrimes		}
2281556Srgrimes	argc -= optind, argv += optind;
2291556Srgrimes
2301556Srgrimes	/*
2311556Srgrimes	 * If executing...
2321556Srgrimes	 */
2331556Srgrimes	if (lflg == 0 || editor || sflg) {
2341556Srgrimes		lflg = 0;	/* ignore */
2351556Srgrimes		editfile[0] = '\0';
2361556Srgrimes		/*
2371556Srgrimes		 * Catch interrupts to reset active counter and
2381556Srgrimes		 * cleanup temp files.
2391556Srgrimes		 */
2401556Srgrimes		if (setjmp(jmploc.loc)) {
2411556Srgrimes			active = 0;
2421556Srgrimes			if (*editfile)
2431556Srgrimes				unlink(editfile);
2441556Srgrimes			handler = savehandler;
2451556Srgrimes			longjmp(handler->loc, 1);
2461556Srgrimes		}
2471556Srgrimes		savehandler = handler;
2481556Srgrimes		handler = &jmploc;
2491556Srgrimes		if (++active > MAXHISTLOOPS) {
2501556Srgrimes			active = 0;
2511556Srgrimes			displayhist = 0;
2521556Srgrimes			error("called recursively too many times");
2531556Srgrimes		}
2541556Srgrimes		/*
2551556Srgrimes		 * Set editor.
2561556Srgrimes		 */
2571556Srgrimes		if (sflg == 0) {
2581556Srgrimes			if (editor == NULL &&
2591556Srgrimes			    (editor = bltinlookup("FCEDIT", 1)) == NULL &&
2601556Srgrimes			    (editor = bltinlookup("EDITOR", 1)) == NULL)
2611556Srgrimes				editor = DEFEDITOR;
2621556Srgrimes			if (editor[0] == '-' && editor[1] == '\0') {
2631556Srgrimes				sflg = 1;	/* no edit */
2641556Srgrimes				editor = NULL;
2651556Srgrimes			}
2661556Srgrimes		}
2671556Srgrimes	}
2681556Srgrimes
2691556Srgrimes	/*
2701556Srgrimes	 * If executing, parse [old=new] now
2711556Srgrimes	 */
2728855Srgrimes	if (lflg == 0 && argc > 0 &&
2731556Srgrimes	     ((repl = strchr(argv[0], '=')) != NULL)) {
2741556Srgrimes		pat = argv[0];
2751556Srgrimes		*repl++ = '\0';
2761556Srgrimes		argc--, argv++;
2771556Srgrimes	}
2781556Srgrimes	/*
2791556Srgrimes	 * determine [first] and [last]
2801556Srgrimes	 */
2811556Srgrimes	switch (argc) {
2821556Srgrimes	case 0:
2831556Srgrimes		firststr = lflg ? "-16" : "-1";
2841556Srgrimes		laststr = "-1";
2851556Srgrimes		break;
2861556Srgrimes	case 1:
2871556Srgrimes		firststr = argv[0];
2881556Srgrimes		laststr = lflg ? "-1" : argv[0];
2891556Srgrimes		break;
2901556Srgrimes	case 2:
2911556Srgrimes		firststr = argv[0];
2921556Srgrimes		laststr = argv[1];
2931556Srgrimes		break;
2941556Srgrimes	default:
2951556Srgrimes		error("too many args");
2961556Srgrimes	}
2971556Srgrimes	/*
2981556Srgrimes	 * Turn into event numbers.
2991556Srgrimes	 */
3001556Srgrimes	first = str_to_event(firststr, 0);
3011556Srgrimes	last = str_to_event(laststr, 1);
3021556Srgrimes
3031556Srgrimes	if (rflg) {
3041556Srgrimes		i = last;
3051556Srgrimes		last = first;
3061556Srgrimes		first = i;
3071556Srgrimes	}
3081556Srgrimes	/*
3091556Srgrimes	 * XXX - this should not depend on the event numbers
3108855Srgrimes	 * always increasing.  Add sequence numbers or offset
3111556Srgrimes	 * to the history element in next (diskbased) release.
3121556Srgrimes	 */
3131556Srgrimes	direction = first < last ? H_PREV : H_NEXT;
3141556Srgrimes
3151556Srgrimes	/*
3161556Srgrimes	 * If editing, grab a temp file.
3171556Srgrimes	 */
3181556Srgrimes	if (editor) {
3191556Srgrimes		int fd;
3201556Srgrimes		INTOFF;		/* easier */
3211556Srgrimes		sprintf(editfile, "%s/_shXXXXXX", _PATH_TMP);
3221556Srgrimes		if ((fd = mkstemp(editfile)) < 0)
3231556Srgrimes			error("can't create temporary file %s", editfile);
3241556Srgrimes		if ((efp = fdopen(fd, "w")) == NULL) {
3251556Srgrimes			close(fd);
32618016Speter			error("can't allocate stdio buffer for temp");
3271556Srgrimes		}
3281556Srgrimes	}
3291556Srgrimes
3301556Srgrimes	/*
3311556Srgrimes	 * Loop through selected history events.  If listing or executing,
3321556Srgrimes	 * do it now.  Otherwise, put into temp file and call the editor
3331556Srgrimes	 * after.
3341556Srgrimes	 *
3351556Srgrimes	 * The history interface needs rethinking, as the following
3361556Srgrimes	 * convolutions will demonstrate.
3371556Srgrimes	 */
33884261Sobrien	history(hist, &he, H_FIRST);
33984261Sobrien	retval = history(hist, &he, H_NEXT_EVENT, first);
34084261Sobrien	for (;retval != -1; retval = history(hist, &he, direction)) {
3411556Srgrimes		if (lflg) {
3421556Srgrimes			if (!nflg)
34384261Sobrien				out1fmt("%5d ", he.num);
34484261Sobrien			out1str(he.str);
3451556Srgrimes		} else {
3468855Srgrimes			char *s = pat ?
34784261Sobrien			   fc_replace(he.str, pat, repl) : (char *)he.str;
3481556Srgrimes
3491556Srgrimes			if (sflg) {
3501556Srgrimes				if (displayhist) {
3511556Srgrimes					out2str(s);
3521556Srgrimes				}
3531556Srgrimes				evalstring(s);
3541556Srgrimes				if (displayhist && hist) {
3551556Srgrimes					/*
3568855Srgrimes					 *  XXX what about recursive and
3571556Srgrimes					 *  relative histnums.
3581556Srgrimes					 */
35997730Stjr					oldhistnum = he.num;
36084261Sobrien					history(hist, &he, H_ENTER, s);
36197730Stjr					/*
36297730Stjr					 * XXX H_ENTER moves the internal
36397730Stjr					 * cursor, set it back to the current
36497730Stjr					 * entry.
36597730Stjr					 */
36697730Stjr					retval = history(hist, &he,
36797730Stjr					    H_NEXT_EVENT, oldhistnum);
3681556Srgrimes				}
3691556Srgrimes			} else
3701556Srgrimes				fputs(s, efp);
3711556Srgrimes		}
3721556Srgrimes		/*
3731556Srgrimes		 * At end?  (if we were to loose last, we'd sure be
3741556Srgrimes		 * messed up).
3751556Srgrimes		 */
37684261Sobrien		if (he.num == last)
3771556Srgrimes			break;
3781556Srgrimes	}
3791556Srgrimes	if (editor) {
3801556Srgrimes		char *editcmd;
3811556Srgrimes
3821556Srgrimes		fclose(efp);
3831556Srgrimes		editcmd = stalloc(strlen(editor) + strlen(editfile) + 2);
3841556Srgrimes		sprintf(editcmd, "%s %s", editor, editfile);
3851556Srgrimes		evalstring(editcmd);	/* XXX - should use no JC command */
3861556Srgrimes		INTON;
3871556Srgrimes		readcmdfile(editfile);	/* XXX - should read back - quick tst */
3881556Srgrimes		unlink(editfile);
3891556Srgrimes	}
3908855Srgrimes
3911556Srgrimes	if (lflg == 0 && active > 0)
3921556Srgrimes		--active;
3931556Srgrimes	if (displayhist)
3941556Srgrimes		displayhist = 0;
39517987Speter	return 0;
3961556Srgrimes}
3971556Srgrimes
3981556SrgrimesSTATIC char *
39990111Simpfc_replace(const char *s, char *p, char *r)
4001556Srgrimes{
4011556Srgrimes	char *dest;
4021556Srgrimes	int plen = strlen(p);
4031556Srgrimes
4041556Srgrimes	STARTSTACKSTR(dest);
4051556Srgrimes	while (*s) {
4061556Srgrimes		if (*s == *p && strncmp(s, p, plen) == 0) {
4071556Srgrimes			while (*r)
4081556Srgrimes				STPUTC(*r++, dest);
4091556Srgrimes			s += plen;
4101556Srgrimes			*p = '\0';	/* so no more matches */
4111556Srgrimes		} else
4121556Srgrimes			STPUTC(*s++, dest);
4131556Srgrimes	}
4141556Srgrimes	STACKSTRNUL(dest);
4151556Srgrimes	dest = grabstackstr(dest);
4161556Srgrimes
4171556Srgrimes	return (dest);
4181556Srgrimes}
4191556Srgrimes
42017987Speterint
42190111Simpnot_fcnumber(char *s)
4221556Srgrimes{
42320425Ssteve	if (s == NULL)
42425224Ssteve		return (0);
42520425Ssteve	if (*s == '-')
42620425Ssteve		s++;
4271556Srgrimes	return (!is_number(s));
4281556Srgrimes}
4291556Srgrimes
43017987Speterint
43190111Simpstr_to_event(char *str, int last)
4321556Srgrimes{
43384261Sobrien	HistEvent he;
4341556Srgrimes	char *s = str;
4351556Srgrimes	int relative = 0;
43684261Sobrien	int i, retval;
4371556Srgrimes
43884261Sobrien	retval = history(hist, &he, H_FIRST);
4391556Srgrimes	switch (*s) {
4401556Srgrimes	case '-':
4411556Srgrimes		relative = 1;
4421556Srgrimes		/*FALLTHROUGH*/
4431556Srgrimes	case '+':
4441556Srgrimes		s++;
4451556Srgrimes	}
4461556Srgrimes	if (is_number(s)) {
4471556Srgrimes		i = atoi(s);
4481556Srgrimes		if (relative) {
44984261Sobrien			while (retval != -1 && i--) {
45084261Sobrien				retval = history(hist, &he, H_NEXT);
4511556Srgrimes			}
45284261Sobrien			if (retval == -1)
45384261Sobrien				retval = history(hist, &he, H_LAST);
4541556Srgrimes		} else {
45584261Sobrien			retval = history(hist, &he, H_NEXT_EVENT, i);
45684261Sobrien			if (retval == -1) {
4571556Srgrimes				/*
4581556Srgrimes				 * the notion of first and last is
4591556Srgrimes				 * backwards to that of the history package
4601556Srgrimes				 */
46184261Sobrien				retval = history(hist, &he, last ? H_FIRST : H_LAST);
4621556Srgrimes			}
4631556Srgrimes		}
46484261Sobrien		if (retval == -1)
4651556Srgrimes			error("history number %s not found (internal error)",
4661556Srgrimes			       str);
4671556Srgrimes	} else {
4681556Srgrimes		/*
4698855Srgrimes		 * pattern
4701556Srgrimes		 */
47184261Sobrien		retval = history(hist, &he, H_PREV_STR, str);
47284261Sobrien		if (retval == -1)
4731556Srgrimes			error("history pattern not found: %s", str);
4741556Srgrimes	}
47584261Sobrien	return (he.num);
4761556Srgrimes}
477100565Stjr
478100565Stjrint
479100565Stjrbindcmd(int argc, char **argv)
480100565Stjr{
481100565Stjr
482100565Stjr	if (el == NULL)
483100565Stjr		error("line editing is disabled");
484148974Sstefanf	return (el_parse(el, argc, (const char **)argv));
485100565Stjr}
486100565Stjr
48725224Ssteve#else
48825224Ssteve#include "error.h"
48925224Ssteve
49025224Ssteveint
49190111Simphistcmd(int argc, char **argv)
49225224Ssteve{
49325224Ssteve
49425224Ssteve	error("not compiled with history support");
49525224Ssteve	/*NOTREACHED*/
49625224Ssteve	return (0);
49725224Ssteve}
498100565Stjr
499100565Stjrint
500100565Stjrbindcmd(int argc, char **argv)
501100565Stjr{
502100565Stjr
503100565Stjr	error("not compiled with line editing support");
504100565Stjr	return (0);
505100565Stjr}
50625224Ssteve#endif
507