histedit.c revision 25224
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 *
3625224Ssteve *	$Id: histedit.c,v 1.10 1997/03/28 15:24:38 imp Exp $
371556Srgrimes */
381556Srgrimes
391556Srgrimes#ifndef lint
4020425Sstevestatic char const 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"
601556Srgrimes#include "error.h"
6117987Speter#include "eval.h"
621556Srgrimes#include "memalloc.h"
631556Srgrimes
641556Srgrimes#define MAXHISTLOOPS	4	/* max recursions through fc */
651556Srgrimes#define DEFEDITOR	"ed"	/* default editor *should* be $EDITOR */
661556Srgrimes
671556SrgrimesHistory *hist;	/* history cookie */
681556SrgrimesEditLine *el;	/* editline cookie */
691556Srgrimesint displayhist;
701556Srgrimesstatic FILE *el_in, *el_out;
711556Srgrimes
721556SrgrimesSTATIC char *fc_replace __P((const char *, char *, char *));
731556Srgrimes
741556Srgrimes/*
751556Srgrimes * Set history and editing status.  Called whenever the status may
761556Srgrimes * have changed (figures out what to do).
771556Srgrimes */
7817987Spetervoid
7920425Sstevehistedit()
8017987Speter{
811556Srgrimes
821556Srgrimes#define editing (Eflag || Vflag)
831556Srgrimes
841556Srgrimes	if (iflag) {
851556Srgrimes		if (!hist) {
861556Srgrimes			/*
871556Srgrimes			 * turn history on
881556Srgrimes			 */
891556Srgrimes			INTOFF;
901556Srgrimes			hist = history_init();
911556Srgrimes			INTON;
921556Srgrimes
931556Srgrimes			if (hist != NULL)
9420425Ssteve				sethistsize(histsizeval());
951556Srgrimes			else
961556Srgrimes				out2str("sh: can't initialize history\n");
971556Srgrimes		}
981556Srgrimes		if (editing && !el && isatty(0)) { /* && isatty(2) ??? */
991556Srgrimes			/*
1001556Srgrimes			 * turn editing on
1011556Srgrimes			 */
1021556Srgrimes			INTOFF;
1031556Srgrimes			if (el_in == NULL)
1041556Srgrimes				el_in = fdopen(0, "r");
1051556Srgrimes			if (el_out == NULL)
1061556Srgrimes				el_out = fdopen(2, "w");
1071556Srgrimes			if (el_in == NULL || el_out == NULL)
1081556Srgrimes				goto bad;
1091556Srgrimes			el = el_init(arg0, el_in, el_out);
1101556Srgrimes			if (el != NULL) {
1111556Srgrimes				if (hist)
1121556Srgrimes					el_set(el, EL_HIST, history, hist);
1131556Srgrimes				el_set(el, EL_PROMPT, getprompt);
1141556Srgrimes			} else {
1151556Srgrimesbad:
1161556Srgrimes				out2str("sh: can't initialize editing\n");
1171556Srgrimes			}
1181556Srgrimes			INTON;
1191556Srgrimes		} else if (!editing && el) {
1201556Srgrimes			INTOFF;
1211556Srgrimes			el_end(el);
1221556Srgrimes			el = NULL;
1231556Srgrimes			INTON;
1241556Srgrimes		}
1251556Srgrimes		if (el) {
1261556Srgrimes			if (Vflag)
1271556Srgrimes				el_set(el, EL_EDITOR, "vi");
1281556Srgrimes			else if (Eflag)
1291556Srgrimes				el_set(el, EL_EDITOR, "emacs");
1301556Srgrimes		}
1311556Srgrimes	} else {
1321556Srgrimes		INTOFF;
1331556Srgrimes		if (el) {	/* no editing if not interactive */
1341556Srgrimes			el_end(el);
1351556Srgrimes			el = NULL;
1361556Srgrimes		}
1371556Srgrimes		if (hist) {
1381556Srgrimes			history_end(hist);
1391556Srgrimes			hist = NULL;
1401556Srgrimes		}
1411556Srgrimes		INTON;
1421556Srgrimes	}
1431556Srgrimes}
1441556Srgrimes
14517987Speter
14617987Spetervoid
14720425Sstevesethistsize(hs)
14820425Ssteve	const char *hs;
14917987Speter{
1501556Srgrimes	int histsize;
1511556Srgrimes
1521556Srgrimes	if (hist != NULL) {
15320425Ssteve		if (hs == NULL || *hs == '\0' ||
15420425Ssteve		   (histsize = atoi(hs)) < 0)
1551556Srgrimes			histsize = 100;
1561556Srgrimes		history(hist, H_EVENT, histsize);
1571556Srgrimes	}
1581556Srgrimes}
1591556Srgrimes
1601556Srgrimes/*
1611556Srgrimes *  This command is provided since POSIX decided to standardize
1621556Srgrimes *  the Korn shell fc command.  Oh well...
1631556Srgrimes */
16417987Speterint
1651556Srgrimeshistcmd(argc, argv)
16617987Speter	int argc;
16717987Speter	char **argv;
1681556Srgrimes{
1691556Srgrimes	extern char *optarg;
1701556Srgrimes	extern int optind, optopt, optreset;
1711556Srgrimes	int ch;
1721556Srgrimes	char *editor = NULL;
1731556Srgrimes	const HistEvent *he;
1741556Srgrimes	int lflg = 0, nflg = 0, rflg = 0, sflg = 0;
1751556Srgrimes	int i;
1761556Srgrimes	char *firststr, *laststr;
1771556Srgrimes	int first, last, direction;
1781556Srgrimes	char *pat = NULL, *repl;	/* ksh "fc old=new" crap */
1791556Srgrimes	static int active = 0;
1801556Srgrimes	struct jmploc jmploc;
1811556Srgrimes	struct jmploc *volatile savehandler;
1821556Srgrimes	char editfile[MAXPATHLEN + 1];
1831556Srgrimes	FILE *efp;
18417987Speter#ifdef __GNUC__
18517987Speter	/* Avoid longjmp clobbering */
18617987Speter	(void) &editor;
18717987Speter	(void) &lflg;
18817987Speter	(void) &nflg;
18917987Speter	(void) &rflg;
19017987Speter	(void) &sflg;
19117987Speter	(void) &firststr;
19217987Speter	(void) &laststr;
19317987Speter	(void) &pat;
19417987Speter	(void) &repl;
19517987Speter	(void) &efp;
19617987Speter	(void) &argc;
19717987Speter	(void) &argv;
19817987Speter#endif
1991556Srgrimes
2001556Srgrimes	if (hist == NULL)
2011556Srgrimes		error("history not active");
2028855Srgrimes
2031556Srgrimes	if (argc == 1)
2041556Srgrimes		error("missing history argument");
2051556Srgrimes
2061556Srgrimes	optreset = 1; optind = 1; /* initialize getopt */
2071556Srgrimes	while (not_fcnumber(argv[optind]) &&
20824348Simp	      (ch = getopt(argc, argv, ":e:lnrs")) != -1)
2091556Srgrimes		switch ((char)ch) {
2101556Srgrimes		case 'e':
2111556Srgrimes			editor = optarg;
2121556Srgrimes			break;
2131556Srgrimes		case 'l':
2141556Srgrimes			lflg = 1;
2151556Srgrimes			break;
2161556Srgrimes		case 'n':
2171556Srgrimes			nflg = 1;
2181556Srgrimes			break;
2191556Srgrimes		case 'r':
2201556Srgrimes			rflg = 1;
2211556Srgrimes			break;
2221556Srgrimes		case 's':
2231556Srgrimes			sflg = 1;
2241556Srgrimes			break;
2251556Srgrimes		case ':':
2261556Srgrimes			error("option -%c expects argument", optopt);
2271556Srgrimes		case '?':
2281556Srgrimes		default:
2291556Srgrimes			error("unknown option: -%c", optopt);
2301556Srgrimes		}
2311556Srgrimes	argc -= optind, argv += optind;
2321556Srgrimes
2331556Srgrimes	/*
2341556Srgrimes	 * If executing...
2351556Srgrimes	 */
2361556Srgrimes	if (lflg == 0 || editor || sflg) {
2371556Srgrimes		lflg = 0;	/* ignore */
2381556Srgrimes		editfile[0] = '\0';
2391556Srgrimes		/*
2401556Srgrimes		 * Catch interrupts to reset active counter and
2411556Srgrimes		 * cleanup temp files.
2421556Srgrimes		 */
2431556Srgrimes		if (setjmp(jmploc.loc)) {
2441556Srgrimes			active = 0;
2451556Srgrimes			if (*editfile)
2461556Srgrimes				unlink(editfile);
2471556Srgrimes			handler = savehandler;
2481556Srgrimes			longjmp(handler->loc, 1);
2491556Srgrimes		}
2501556Srgrimes		savehandler = handler;
2511556Srgrimes		handler = &jmploc;
2521556Srgrimes		if (++active > MAXHISTLOOPS) {
2531556Srgrimes			active = 0;
2541556Srgrimes			displayhist = 0;
2551556Srgrimes			error("called recursively too many times");
2561556Srgrimes		}
2571556Srgrimes		/*
2581556Srgrimes		 * Set editor.
2591556Srgrimes		 */
2601556Srgrimes		if (sflg == 0) {
2611556Srgrimes			if (editor == NULL &&
2621556Srgrimes			    (editor = bltinlookup("FCEDIT", 1)) == NULL &&
2631556Srgrimes			    (editor = bltinlookup("EDITOR", 1)) == NULL)
2641556Srgrimes				editor = DEFEDITOR;
2651556Srgrimes			if (editor[0] == '-' && editor[1] == '\0') {
2661556Srgrimes				sflg = 1;	/* no edit */
2671556Srgrimes				editor = NULL;
2681556Srgrimes			}
2691556Srgrimes		}
2701556Srgrimes	}
2711556Srgrimes
2721556Srgrimes	/*
2731556Srgrimes	 * If executing, parse [old=new] now
2741556Srgrimes	 */
2758855Srgrimes	if (lflg == 0 && argc > 0 &&
2761556Srgrimes	     ((repl = strchr(argv[0], '=')) != NULL)) {
2771556Srgrimes		pat = argv[0];
2781556Srgrimes		*repl++ = '\0';
2791556Srgrimes		argc--, argv++;
2801556Srgrimes	}
2811556Srgrimes	/*
2821556Srgrimes	 * determine [first] and [last]
2831556Srgrimes	 */
2841556Srgrimes	switch (argc) {
2851556Srgrimes	case 0:
2861556Srgrimes		firststr = lflg ? "-16" : "-1";
2871556Srgrimes		laststr = "-1";
2881556Srgrimes		break;
2891556Srgrimes	case 1:
2901556Srgrimes		firststr = argv[0];
2911556Srgrimes		laststr = lflg ? "-1" : argv[0];
2921556Srgrimes		break;
2931556Srgrimes	case 2:
2941556Srgrimes		firststr = argv[0];
2951556Srgrimes		laststr = argv[1];
2961556Srgrimes		break;
2971556Srgrimes	default:
2981556Srgrimes		error("too many args");
2991556Srgrimes	}
3001556Srgrimes	/*
3011556Srgrimes	 * Turn into event numbers.
3021556Srgrimes	 */
3031556Srgrimes	first = str_to_event(firststr, 0);
3041556Srgrimes	last = str_to_event(laststr, 1);
3051556Srgrimes
3061556Srgrimes	if (rflg) {
3071556Srgrimes		i = last;
3081556Srgrimes		last = first;
3091556Srgrimes		first = i;
3101556Srgrimes	}
3111556Srgrimes	/*
3121556Srgrimes	 * XXX - this should not depend on the event numbers
3138855Srgrimes	 * always increasing.  Add sequence numbers or offset
3141556Srgrimes	 * to the history element in next (diskbased) release.
3151556Srgrimes	 */
3161556Srgrimes	direction = first < last ? H_PREV : H_NEXT;
3171556Srgrimes
3181556Srgrimes	/*
3191556Srgrimes	 * If editing, grab a temp file.
3201556Srgrimes	 */
3211556Srgrimes	if (editor) {
3221556Srgrimes		int fd;
3231556Srgrimes		INTOFF;		/* easier */
3241556Srgrimes		sprintf(editfile, "%s/_shXXXXXX", _PATH_TMP);
3251556Srgrimes		if ((fd = mkstemp(editfile)) < 0)
3261556Srgrimes			error("can't create temporary file %s", editfile);
3271556Srgrimes		if ((efp = fdopen(fd, "w")) == NULL) {
3281556Srgrimes			close(fd);
32918016Speter			error("can't allocate stdio buffer for temp");
3301556Srgrimes		}
3311556Srgrimes	}
3321556Srgrimes
3331556Srgrimes	/*
3341556Srgrimes	 * Loop through selected history events.  If listing or executing,
3351556Srgrimes	 * do it now.  Otherwise, put into temp file and call the editor
3361556Srgrimes	 * after.
3371556Srgrimes	 *
3381556Srgrimes	 * The history interface needs rethinking, as the following
3391556Srgrimes	 * convolutions will demonstrate.
3401556Srgrimes	 */
3411556Srgrimes	history(hist, H_FIRST);
3421556Srgrimes	he = history(hist, H_NEXT_EVENT, first);
3431556Srgrimes	for (;he != NULL; he = history(hist, direction)) {
3441556Srgrimes		if (lflg) {
3451556Srgrimes			if (!nflg)
3461556Srgrimes				out1fmt("%5d ", he->num);
3471556Srgrimes			out1str(he->str);
3481556Srgrimes		} else {
3498855Srgrimes			char *s = pat ?
3501556Srgrimes			   fc_replace(he->str, pat, repl) : (char *)he->str;
3511556Srgrimes
3521556Srgrimes			if (sflg) {
3531556Srgrimes				if (displayhist) {
3541556Srgrimes					out2str(s);
3551556Srgrimes				}
3561556Srgrimes				evalstring(s);
3571556Srgrimes				if (displayhist && hist) {
3581556Srgrimes					/*
3598855Srgrimes					 *  XXX what about recursive and
3601556Srgrimes					 *  relative histnums.
3611556Srgrimes					 */
3621556Srgrimes					history(hist, H_ENTER, s);
3631556Srgrimes				}
3641556Srgrimes			} else
3651556Srgrimes				fputs(s, efp);
3661556Srgrimes		}
3671556Srgrimes		/*
3681556Srgrimes		 * At end?  (if we were to loose last, we'd sure be
3691556Srgrimes		 * messed up).
3701556Srgrimes		 */
3711556Srgrimes		if (he->num == last)
3721556Srgrimes			break;
3731556Srgrimes	}
3741556Srgrimes	if (editor) {
3751556Srgrimes		char *editcmd;
3761556Srgrimes
3771556Srgrimes		fclose(efp);
3781556Srgrimes		editcmd = stalloc(strlen(editor) + strlen(editfile) + 2);
3791556Srgrimes		sprintf(editcmd, "%s %s", editor, editfile);
3801556Srgrimes		evalstring(editcmd);	/* XXX - should use no JC command */
3811556Srgrimes		INTON;
3821556Srgrimes		readcmdfile(editfile);	/* XXX - should read back - quick tst */
3831556Srgrimes		unlink(editfile);
3841556Srgrimes	}
3858855Srgrimes
3861556Srgrimes	if (lflg == 0 && active > 0)
3871556Srgrimes		--active;
3881556Srgrimes	if (displayhist)
3891556Srgrimes		displayhist = 0;
39017987Speter	return 0;
3911556Srgrimes}
3921556Srgrimes
3931556SrgrimesSTATIC char *
3941556Srgrimesfc_replace(s, p, r)
3951556Srgrimes	const char *s;
3961556Srgrimes	char *p, *r;
3971556Srgrimes{
3981556Srgrimes	char *dest;
3991556Srgrimes	int plen = strlen(p);
4001556Srgrimes
4011556Srgrimes	STARTSTACKSTR(dest);
4021556Srgrimes	while (*s) {
4031556Srgrimes		if (*s == *p && strncmp(s, p, plen) == 0) {
4041556Srgrimes			while (*r)
4051556Srgrimes				STPUTC(*r++, dest);
4061556Srgrimes			s += plen;
4071556Srgrimes			*p = '\0';	/* so no more matches */
4081556Srgrimes		} else
4091556Srgrimes			STPUTC(*s++, dest);
4101556Srgrimes	}
4111556Srgrimes	STACKSTRNUL(dest);
4121556Srgrimes	dest = grabstackstr(dest);
4131556Srgrimes
4141556Srgrimes	return (dest);
4151556Srgrimes}
4161556Srgrimes
41717987Speterint
4181556Srgrimesnot_fcnumber(s)
41920425Ssteve	char *s;
4201556Srgrimes{
42120425Ssteve	if (s == NULL)
42225224Ssteve		return (0);
42320425Ssteve	if (*s == '-')
42420425Ssteve		s++;
4251556Srgrimes	return (!is_number(s));
4261556Srgrimes}
4271556Srgrimes
42817987Speterint
4291556Srgrimesstr_to_event(str, last)
4301556Srgrimes	char *str;
4311556Srgrimes	int last;
4321556Srgrimes{
4331556Srgrimes	const HistEvent *he;
4341556Srgrimes	char *s = str;
4351556Srgrimes	int relative = 0;
43617987Speter	int i;
4371556Srgrimes
4381556Srgrimes	he = history(hist, 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) {
4491556Srgrimes			while (he != NULL && i--) {
4501556Srgrimes				he = history(hist, H_NEXT);
4511556Srgrimes			}
4521556Srgrimes			if (he == NULL)
4531556Srgrimes				he = history(hist, H_LAST);
4541556Srgrimes		} else {
4551556Srgrimes			he = history(hist, H_NEXT_EVENT, i);
4561556Srgrimes			if (he == NULL) {
4571556Srgrimes				/*
4581556Srgrimes				 * the notion of first and last is
4591556Srgrimes				 * backwards to that of the history package
4601556Srgrimes				 */
4611556Srgrimes				he = history(hist, last ? H_FIRST : H_LAST);
4621556Srgrimes			}
4631556Srgrimes		}
4641556Srgrimes		if (he == NULL)
4651556Srgrimes			error("history number %s not found (internal error)",
4661556Srgrimes			       str);
4671556Srgrimes	} else {
4681556Srgrimes		/*
4698855Srgrimes		 * pattern
4701556Srgrimes		 */
4711556Srgrimes		he = history(hist, H_PREV_STR, str);
4721556Srgrimes		if (he == NULL)
4731556Srgrimes			error("history pattern not found: %s", str);
4741556Srgrimes	}
4751556Srgrimes	return (he->num);
4761556Srgrimes}
47725224Ssteve#else
47825224Ssteve#include "error.h"
47925224Ssteve
48025224Ssteveint
48125224Sstevehistcmd(argc, argv)
48225224Ssteve	int argc;
48325224Ssteve	char **argv;
48425224Ssteve{
48525224Ssteve
48625224Ssteve	error("not compiled with history support");
48725224Ssteve	/*NOTREACHED*/
48825224Ssteve	return (0);
48925224Ssteve}
49025224Ssteve#endif
491