histedit.c revision 17987
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 * 3. All advertising materials mentioning features or use of this software
171556Srgrimes *    must display the following acknowledgement:
181556Srgrimes *	This product includes software developed by the University of
191556Srgrimes *	California, Berkeley and its contributors.
201556Srgrimes * 4. Neither the name of the University nor the names of its contributors
211556Srgrimes *    may be used to endorse or promote products derived from this software
221556Srgrimes *    without specific prior written permission.
231556Srgrimes *
241556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341556Srgrimes * SUCH DAMAGE.
353044Sdg *
3617987Speter *	$Id: histedit.c,v 1.4 1995/05/30 00:07:14 rgrimes Exp $
371556Srgrimes */
381556Srgrimes
391556Srgrimes#ifndef lint
4017987Speterstatic char sccsid[] = "@(#)histedit.c	8.2 (Berkeley) 5/4/95";
411556Srgrimes#endif /* not lint */
421556Srgrimes
4317987Speter#include <sys/param.h>
4417987Speter#include <paths.h>
4517987Speter#include <stdio.h>
4617987Speter#include <stdlib.h>
4717987Speter#include <unistd.h>
481556Srgrimes/*
491556Srgrimes * Editline and history functions (and glue).
501556Srgrimes */
511556Srgrimes#include "shell.h"
521556Srgrimes#include "parser.h"
531556Srgrimes#include "var.h"
541556Srgrimes#include "options.h"
5517987Speter#include "main.h"
5617987Speter#include "output.h"
571556Srgrimes#include "mystring.h"
5817987Speter#ifndef NO_HISTORY
5917987Speter#include "myhistedit.h"
6017987Speter#endif
611556Srgrimes#include "error.h"
6217987Speter#include "eval.h"
631556Srgrimes#include "memalloc.h"
641556Srgrimes
651556Srgrimes#define MAXHISTLOOPS	4	/* max recursions through fc */
661556Srgrimes#define DEFEDITOR	"ed"	/* default editor *should* be $EDITOR */
671556Srgrimes
681556SrgrimesHistory *hist;	/* history cookie */
691556SrgrimesEditLine *el;	/* editline cookie */
701556Srgrimesint displayhist;
711556Srgrimesstatic FILE *el_in, *el_out;
721556Srgrimes
731556SrgrimesSTATIC char *fc_replace __P((const char *, char *, char *));
741556Srgrimes
751556Srgrimes/*
761556Srgrimes * Set history and editing status.  Called whenever the status may
771556Srgrimes * have changed (figures out what to do).
781556Srgrimes */
7917987Spetervoid
8017987Speterhistedit()
8117987Speter{
821556Srgrimes
831556Srgrimes#define editing (Eflag || Vflag)
841556Srgrimes
851556Srgrimes	if (iflag) {
861556Srgrimes		if (!hist) {
871556Srgrimes			/*
881556Srgrimes			 * turn history on
891556Srgrimes			 */
901556Srgrimes			INTOFF;
911556Srgrimes			hist = history_init();
921556Srgrimes			INTON;
931556Srgrimes
941556Srgrimes			if (hist != NULL)
951556Srgrimes				sethistsize();
961556Srgrimes			else
971556Srgrimes				out2str("sh: can't initialize history\n");
981556Srgrimes		}
991556Srgrimes		if (editing && !el && isatty(0)) { /* && isatty(2) ??? */
1001556Srgrimes			/*
1011556Srgrimes			 * turn editing on
1021556Srgrimes			 */
1031556Srgrimes			INTOFF;
1041556Srgrimes			if (el_in == NULL)
1051556Srgrimes				el_in = fdopen(0, "r");
1061556Srgrimes			if (el_out == NULL)
1071556Srgrimes				el_out = fdopen(2, "w");
1081556Srgrimes			if (el_in == NULL || el_out == NULL)
1091556Srgrimes				goto bad;
1101556Srgrimes			el = el_init(arg0, el_in, el_out);
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");
1311556Srgrimes		}
1321556Srgrimes	} else {
1331556Srgrimes		INTOFF;
1341556Srgrimes		if (el) {	/* no editing if not interactive */
1351556Srgrimes			el_end(el);
1361556Srgrimes			el = NULL;
1371556Srgrimes		}
1381556Srgrimes		if (hist) {
1391556Srgrimes			history_end(hist);
1401556Srgrimes			hist = NULL;
1411556Srgrimes		}
1421556Srgrimes		INTON;
1431556Srgrimes	}
1441556Srgrimes}
1451556Srgrimes
14617987Speter
14717987Spetervoid
14817987Spetersethistsize()
14917987Speter{
1501556Srgrimes	char *cp;
1511556Srgrimes	int histsize;
1521556Srgrimes
1531556Srgrimes	if (hist != NULL) {
1541556Srgrimes		cp = lookupvar("HISTSIZE");
1558855Srgrimes		if (cp == NULL || *cp == '\0' ||
1561556Srgrimes		   (histsize = atoi(cp)) < 0)
1571556Srgrimes			histsize = 100;
1581556Srgrimes		history(hist, H_EVENT, histsize);
1591556Srgrimes	}
1601556Srgrimes}
1611556Srgrimes
1621556Srgrimes/*
1631556Srgrimes *  This command is provided since POSIX decided to standardize
1641556Srgrimes *  the Korn shell fc command.  Oh well...
1651556Srgrimes */
16617987Speterint
1671556Srgrimeshistcmd(argc, argv)
16817987Speter	int argc;
16917987Speter	char **argv;
1701556Srgrimes{
1711556Srgrimes	extern char *optarg;
1721556Srgrimes	extern int optind, optopt, optreset;
1731556Srgrimes	int ch;
1741556Srgrimes	char *editor = NULL;
1751556Srgrimes	const HistEvent *he;
1761556Srgrimes	int lflg = 0, nflg = 0, rflg = 0, sflg = 0;
1771556Srgrimes	int i;
1781556Srgrimes	char *firststr, *laststr;
1791556Srgrimes	int first, last, direction;
1801556Srgrimes	char *pat = NULL, *repl;	/* ksh "fc old=new" crap */
1811556Srgrimes	static int active = 0;
1821556Srgrimes	struct jmploc jmploc;
1831556Srgrimes	struct jmploc *volatile savehandler;
1841556Srgrimes	char editfile[MAXPATHLEN + 1];
1851556Srgrimes	FILE *efp;
18617987Speter#ifdef __GNUC__
18717987Speter	/* Avoid longjmp clobbering */
18817987Speter	(void) &editor;
18917987Speter	(void) &lflg;
19017987Speter	(void) &nflg;
19117987Speter	(void) &rflg;
19217987Speter	(void) &sflg;
19317987Speter	(void) &firststr;
19417987Speter	(void) &laststr;
19517987Speter	(void) &pat;
19617987Speter	(void) &repl;
19717987Speter	(void) &efp;
19817987Speter	(void) &argc;
19917987Speter	(void) &argv;
20017987Speter#endif
2011556Srgrimes
2021556Srgrimes	if (hist == NULL)
2031556Srgrimes		error("history not active");
2048855Srgrimes
2051556Srgrimes	if (argc == 1)
2061556Srgrimes		error("missing history argument");
2071556Srgrimes
2081556Srgrimes	optreset = 1; optind = 1; /* initialize getopt */
2091556Srgrimes	while (not_fcnumber(argv[optind]) &&
2101556Srgrimes	      (ch = getopt(argc, argv, ":e:lnrs")) != EOF)
2111556Srgrimes		switch ((char)ch) {
2121556Srgrimes		case 'e':
2131556Srgrimes			editor = optarg;
2141556Srgrimes			break;
2151556Srgrimes		case 'l':
2161556Srgrimes			lflg = 1;
2171556Srgrimes			break;
2181556Srgrimes		case 'n':
2191556Srgrimes			nflg = 1;
2201556Srgrimes			break;
2211556Srgrimes		case 'r':
2221556Srgrimes			rflg = 1;
2231556Srgrimes			break;
2241556Srgrimes		case 's':
2251556Srgrimes			sflg = 1;
2261556Srgrimes			break;
2271556Srgrimes		case ':':
2281556Srgrimes			error("option -%c expects argument", optopt);
2291556Srgrimes		case '?':
2301556Srgrimes		default:
2311556Srgrimes			error("unknown option: -%c", optopt);
2321556Srgrimes		}
2331556Srgrimes	argc -= optind, argv += optind;
2341556Srgrimes
2351556Srgrimes	/*
2361556Srgrimes	 * If executing...
2371556Srgrimes	 */
2381556Srgrimes	if (lflg == 0 || editor || sflg) {
2391556Srgrimes		lflg = 0;	/* ignore */
2401556Srgrimes		editfile[0] = '\0';
2411556Srgrimes		/*
2421556Srgrimes		 * Catch interrupts to reset active counter and
2431556Srgrimes		 * cleanup temp files.
2441556Srgrimes		 */
2451556Srgrimes		if (setjmp(jmploc.loc)) {
2461556Srgrimes			active = 0;
2471556Srgrimes			if (*editfile)
2481556Srgrimes				unlink(editfile);
2491556Srgrimes			handler = savehandler;
2501556Srgrimes			longjmp(handler->loc, 1);
2511556Srgrimes		}
2521556Srgrimes		savehandler = handler;
2531556Srgrimes		handler = &jmploc;
2541556Srgrimes		if (++active > MAXHISTLOOPS) {
2551556Srgrimes			active = 0;
2561556Srgrimes			displayhist = 0;
2571556Srgrimes			error("called recursively too many times");
2581556Srgrimes		}
2591556Srgrimes		/*
2601556Srgrimes		 * Set editor.
2611556Srgrimes		 */
2621556Srgrimes		if (sflg == 0) {
2631556Srgrimes			if (editor == NULL &&
2641556Srgrimes			    (editor = bltinlookup("FCEDIT", 1)) == NULL &&
2651556Srgrimes			    (editor = bltinlookup("EDITOR", 1)) == NULL)
2661556Srgrimes				editor = DEFEDITOR;
2671556Srgrimes			if (editor[0] == '-' && editor[1] == '\0') {
2681556Srgrimes				sflg = 1;	/* no edit */
2691556Srgrimes				editor = NULL;
2701556Srgrimes			}
2711556Srgrimes		}
2721556Srgrimes	}
2731556Srgrimes
2741556Srgrimes	/*
2751556Srgrimes	 * If executing, parse [old=new] now
2761556Srgrimes	 */
2778855Srgrimes	if (lflg == 0 && argc > 0 &&
2781556Srgrimes	     ((repl = strchr(argv[0], '=')) != NULL)) {
2791556Srgrimes		pat = argv[0];
2801556Srgrimes		*repl++ = '\0';
2811556Srgrimes		argc--, argv++;
2821556Srgrimes	}
2831556Srgrimes	/*
2841556Srgrimes	 * determine [first] and [last]
2851556Srgrimes	 */
2861556Srgrimes	switch (argc) {
2871556Srgrimes	case 0:
2881556Srgrimes		firststr = lflg ? "-16" : "-1";
2891556Srgrimes		laststr = "-1";
2901556Srgrimes		break;
2911556Srgrimes	case 1:
2921556Srgrimes		firststr = argv[0];
2931556Srgrimes		laststr = lflg ? "-1" : argv[0];
2941556Srgrimes		break;
2951556Srgrimes	case 2:
2961556Srgrimes		firststr = argv[0];
2971556Srgrimes		laststr = argv[1];
2981556Srgrimes		break;
2991556Srgrimes	default:
3001556Srgrimes		error("too many args");
3011556Srgrimes	}
3021556Srgrimes	/*
3031556Srgrimes	 * Turn into event numbers.
3041556Srgrimes	 */
3051556Srgrimes	first = str_to_event(firststr, 0);
3061556Srgrimes	last = str_to_event(laststr, 1);
3071556Srgrimes
3081556Srgrimes	if (rflg) {
3091556Srgrimes		i = last;
3101556Srgrimes		last = first;
3111556Srgrimes		first = i;
3121556Srgrimes	}
3131556Srgrimes	/*
3141556Srgrimes	 * XXX - this should not depend on the event numbers
3158855Srgrimes	 * always increasing.  Add sequence numbers or offset
3161556Srgrimes	 * to the history element in next (diskbased) release.
3171556Srgrimes	 */
3181556Srgrimes	direction = first < last ? H_PREV : H_NEXT;
3191556Srgrimes
3201556Srgrimes	/*
3211556Srgrimes	 * If editing, grab a temp file.
3221556Srgrimes	 */
3231556Srgrimes	if (editor) {
3241556Srgrimes		int fd;
3251556Srgrimes		INTOFF;		/* easier */
3261556Srgrimes		sprintf(editfile, "%s/_shXXXXXX", _PATH_TMP);
3271556Srgrimes		if ((fd = mkstemp(editfile)) < 0)
3281556Srgrimes			error("can't create temporary file %s", editfile);
3291556Srgrimes		if ((efp = fdopen(fd, "w")) == NULL) {
3301556Srgrimes			close(fd);
3311556Srgrimes			error("can't allocate stdio buffer for temp\n");
3321556Srgrimes		}
3331556Srgrimes	}
3341556Srgrimes
3351556Srgrimes	/*
3361556Srgrimes	 * Loop through selected history events.  If listing or executing,
3371556Srgrimes	 * do it now.  Otherwise, put into temp file and call the editor
3381556Srgrimes	 * after.
3391556Srgrimes	 *
3401556Srgrimes	 * The history interface needs rethinking, as the following
3411556Srgrimes	 * convolutions will demonstrate.
3421556Srgrimes	 */
3431556Srgrimes	history(hist, H_FIRST);
3441556Srgrimes	he = history(hist, H_NEXT_EVENT, first);
3451556Srgrimes	for (;he != NULL; he = history(hist, direction)) {
3461556Srgrimes		if (lflg) {
3471556Srgrimes			if (!nflg)
3481556Srgrimes				out1fmt("%5d ", he->num);
3491556Srgrimes			out1str(he->str);
3501556Srgrimes		} else {
3518855Srgrimes			char *s = pat ?
3521556Srgrimes			   fc_replace(he->str, pat, repl) : (char *)he->str;
3531556Srgrimes
3541556Srgrimes			if (sflg) {
3551556Srgrimes				if (displayhist) {
3561556Srgrimes					out2str(s);
3571556Srgrimes				}
3581556Srgrimes				evalstring(s);
3591556Srgrimes				if (displayhist && hist) {
3601556Srgrimes					/*
3618855Srgrimes					 *  XXX what about recursive and
3621556Srgrimes					 *  relative histnums.
3631556Srgrimes					 */
3641556Srgrimes					history(hist, H_ENTER, s);
3651556Srgrimes				}
3661556Srgrimes			} else
3671556Srgrimes				fputs(s, efp);
3681556Srgrimes		}
3691556Srgrimes		/*
3701556Srgrimes		 * At end?  (if we were to loose last, we'd sure be
3711556Srgrimes		 * messed up).
3721556Srgrimes		 */
3731556Srgrimes		if (he->num == last)
3741556Srgrimes			break;
3751556Srgrimes	}
3761556Srgrimes	if (editor) {
3771556Srgrimes		char *editcmd;
3781556Srgrimes
3791556Srgrimes		fclose(efp);
3801556Srgrimes		editcmd = stalloc(strlen(editor) + strlen(editfile) + 2);
3811556Srgrimes		sprintf(editcmd, "%s %s", editor, editfile);
3821556Srgrimes		evalstring(editcmd);	/* XXX - should use no JC command */
3831556Srgrimes		INTON;
3841556Srgrimes		readcmdfile(editfile);	/* XXX - should read back - quick tst */
3851556Srgrimes		unlink(editfile);
3861556Srgrimes	}
3878855Srgrimes
3881556Srgrimes	if (lflg == 0 && active > 0)
3891556Srgrimes		--active;
3901556Srgrimes	if (displayhist)
3911556Srgrimes		displayhist = 0;
39217987Speter	return 0;
3931556Srgrimes}
3941556Srgrimes
3951556SrgrimesSTATIC char *
3961556Srgrimesfc_replace(s, p, r)
3971556Srgrimes	const char *s;
3981556Srgrimes	char *p, *r;
3991556Srgrimes{
4001556Srgrimes	char *dest;
4011556Srgrimes	int plen = strlen(p);
4021556Srgrimes
4031556Srgrimes	STARTSTACKSTR(dest);
4041556Srgrimes	while (*s) {
4051556Srgrimes		if (*s == *p && strncmp(s, p, plen) == 0) {
4061556Srgrimes			while (*r)
4071556Srgrimes				STPUTC(*r++, dest);
4081556Srgrimes			s += plen;
4091556Srgrimes			*p = '\0';	/* so no more matches */
4101556Srgrimes		} else
4111556Srgrimes			STPUTC(*s++, dest);
4121556Srgrimes	}
4131556Srgrimes	STACKSTRNUL(dest);
4141556Srgrimes	dest = grabstackstr(dest);
4151556Srgrimes
4161556Srgrimes	return (dest);
4171556Srgrimes}
4181556Srgrimes
41917987Speterint
4201556Srgrimesnot_fcnumber(s)
4211556Srgrimes        char *s;
4221556Srgrimes{
4238289Sdg	if (s == NULL) {
4248289Sdg		/* NULL is not a fc_number */
4258289Sdg		return (1);
4268289Sdg	}
4271556Srgrimes        if (*s == '-')
4281556Srgrimes                s++;
4291556Srgrimes	return (!is_number(s));
4301556Srgrimes}
4311556Srgrimes
43217987Speterint
4331556Srgrimesstr_to_event(str, last)
4341556Srgrimes	char *str;
4351556Srgrimes	int last;
4361556Srgrimes{
4371556Srgrimes	const HistEvent *he;
4381556Srgrimes	char *s = str;
4391556Srgrimes	int relative = 0;
44017987Speter	int i;
4411556Srgrimes
4421556Srgrimes	he = history(hist, H_FIRST);
4431556Srgrimes	switch (*s) {
4441556Srgrimes	case '-':
4451556Srgrimes		relative = 1;
4461556Srgrimes		/*FALLTHROUGH*/
4471556Srgrimes	case '+':
4481556Srgrimes		s++;
4491556Srgrimes	}
4501556Srgrimes	if (is_number(s)) {
4511556Srgrimes		i = atoi(s);
4521556Srgrimes		if (relative) {
4531556Srgrimes			while (he != NULL && i--) {
4541556Srgrimes				he = history(hist, H_NEXT);
4551556Srgrimes			}
4561556Srgrimes			if (he == NULL)
4571556Srgrimes				he = history(hist, H_LAST);
4581556Srgrimes		} else {
4591556Srgrimes			he = history(hist, H_NEXT_EVENT, i);
4601556Srgrimes			if (he == NULL) {
4611556Srgrimes				/*
4621556Srgrimes				 * the notion of first and last is
4631556Srgrimes				 * backwards to that of the history package
4641556Srgrimes				 */
4651556Srgrimes				he = history(hist, last ? H_FIRST : H_LAST);
4661556Srgrimes			}
4671556Srgrimes		}
4681556Srgrimes		if (he == NULL)
4691556Srgrimes			error("history number %s not found (internal error)",
4701556Srgrimes			       str);
4711556Srgrimes	} else {
4721556Srgrimes		/*
4738855Srgrimes		 * pattern
4741556Srgrimes		 */
4751556Srgrimes		he = history(hist, H_PREV_STR, str);
4761556Srgrimes		if (he == NULL)
4771556Srgrimes			error("history pattern not found: %s", str);
4781556Srgrimes	}
4791556Srgrimes	return (he->num);
4801556Srgrimes}
481