histedit.c revision 99110
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.
351556Srgrimes */
361556Srgrimes
371556Srgrimes#ifndef lint
3836150Scharnier#if 0
3936150Scharnierstatic char sccsid[] = "@(#)histedit.c	8.2 (Berkeley) 5/4/95";
4036150Scharnier#endif
411556Srgrimes#endif /* not lint */
4299110Sobrien#include <sys/cdefs.h>
4399110Sobrien__FBSDID("$FreeBSD: head/bin/sh/histedit.c 99110 2002-06-30 05:15:05Z obrien $");
441556Srgrimes
4517987Speter#include <sys/param.h>
4677463Simp#include <limits.h>
4717987Speter#include <paths.h>
4817987Speter#include <stdio.h>
4917987Speter#include <stdlib.h>
5017987Speter#include <unistd.h>
511556Srgrimes/*
521556Srgrimes * Editline and history functions (and glue).
531556Srgrimes */
541556Srgrimes#include "shell.h"
551556Srgrimes#include "parser.h"
561556Srgrimes#include "var.h"
571556Srgrimes#include "options.h"
5817987Speter#include "main.h"
5917987Speter#include "output.h"
601556Srgrimes#include "mystring.h"
6117987Speter#ifndef NO_HISTORY
6217987Speter#include "myhistedit.h"
631556Srgrimes#include "error.h"
6417987Speter#include "eval.h"
651556Srgrimes#include "memalloc.h"
661556Srgrimes
671556Srgrimes#define MAXHISTLOOPS	4	/* max recursions through fc */
681556Srgrimes#define DEFEDITOR	"ed"	/* default editor *should* be $EDITOR */
691556Srgrimes
701556SrgrimesHistory *hist;	/* history cookie */
711556SrgrimesEditLine *el;	/* editline cookie */
721556Srgrimesint displayhist;
7384261Sobrienstatic FILE *el_in, *el_out, *el_err;
741556Srgrimes
7590111SimpSTATIC char *fc_replace(const char *, char *, char *);
761556Srgrimes
771556Srgrimes/*
781556Srgrimes * Set history and editing status.  Called whenever the status may
791556Srgrimes * have changed (figures out what to do).
801556Srgrimes */
8117987Spetervoid
8290111Simphistedit(void)
8317987Speter{
841556Srgrimes
851556Srgrimes#define editing (Eflag || Vflag)
861556Srgrimes
871556Srgrimes	if (iflag) {
881556Srgrimes		if (!hist) {
891556Srgrimes			/*
901556Srgrimes			 * turn history on
911556Srgrimes			 */
921556Srgrimes			INTOFF;
931556Srgrimes			hist = history_init();
941556Srgrimes			INTON;
951556Srgrimes
961556Srgrimes			if (hist != NULL)
9720425Ssteve				sethistsize(histsizeval());
981556Srgrimes			else
991556Srgrimes				out2str("sh: can't initialize history\n");
1001556Srgrimes		}
1011556Srgrimes		if (editing && !el && isatty(0)) { /* && isatty(2) ??? */
1021556Srgrimes			/*
1031556Srgrimes			 * turn editing on
1041556Srgrimes			 */
1051556Srgrimes			INTOFF;
1061556Srgrimes			if (el_in == NULL)
1071556Srgrimes				el_in = fdopen(0, "r");
10884261Sobrien			if (el_err == NULL)
10984261Sobrien				el_err = fdopen(1, "w");
1101556Srgrimes			if (el_out == NULL)
1111556Srgrimes				el_out = fdopen(2, "w");
11284261Sobrien			if (el_in == NULL || el_err == NULL || el_out == NULL)
1131556Srgrimes				goto bad;
11484261Sobrien			el = el_init(arg0, el_in, el_out, el_err);
1151556Srgrimes			if (el != NULL) {
1161556Srgrimes				if (hist)
1171556Srgrimes					el_set(el, EL_HIST, history, hist);
1181556Srgrimes				el_set(el, EL_PROMPT, getprompt);
1191556Srgrimes			} else {
1201556Srgrimesbad:
1211556Srgrimes				out2str("sh: can't initialize editing\n");
1221556Srgrimes			}
1231556Srgrimes			INTON;
1241556Srgrimes		} else if (!editing && el) {
1251556Srgrimes			INTOFF;
1261556Srgrimes			el_end(el);
1271556Srgrimes			el = NULL;
1281556Srgrimes			INTON;
1291556Srgrimes		}
1301556Srgrimes		if (el) {
1311556Srgrimes			if (Vflag)
1321556Srgrimes				el_set(el, EL_EDITOR, "vi");
1331556Srgrimes			else if (Eflag)
1341556Srgrimes				el_set(el, EL_EDITOR, "emacs");
1351556Srgrimes		}
1361556Srgrimes	} else {
1371556Srgrimes		INTOFF;
1381556Srgrimes		if (el) {	/* no editing if not interactive */
1391556Srgrimes			el_end(el);
1401556Srgrimes			el = NULL;
1411556Srgrimes		}
1421556Srgrimes		if (hist) {
1431556Srgrimes			history_end(hist);
1441556Srgrimes			hist = NULL;
1451556Srgrimes		}
1461556Srgrimes		INTON;
1471556Srgrimes	}
1481556Srgrimes}
1491556Srgrimes
15017987Speter
15117987Spetervoid
15220425Sstevesethistsize(hs)
15320425Ssteve	const char *hs;
15417987Speter{
1551556Srgrimes	int histsize;
15684261Sobrien	HistEvent he;
1571556Srgrimes
1581556Srgrimes	if (hist != NULL) {
15920425Ssteve		if (hs == NULL || *hs == '\0' ||
16020425Ssteve		   (histsize = atoi(hs)) < 0)
1611556Srgrimes			histsize = 100;
16284261Sobrien		history(hist, &he, H_EVENT, histsize);
1631556Srgrimes	}
1641556Srgrimes}
1651556Srgrimes
1661556Srgrimes/*
1671556Srgrimes *  This command is provided since POSIX decided to standardize
1681556Srgrimes *  the Korn shell fc command.  Oh well...
1691556Srgrimes */
17017987Speterint
17190111Simphistcmd(int argc, char **argv)
1721556Srgrimes{
1731556Srgrimes	int ch;
1741556Srgrimes	char *editor = NULL;
17584261Sobrien	HistEvent he;
1761556Srgrimes	int lflg = 0, nflg = 0, rflg = 0, sflg = 0;
17784261Sobrien	int i, retval;
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;
18477463Simp	char editfile[PATH_MAX];
1851556Srgrimes	FILE *efp;
18697730Stjr	int oldhistnum;
18717987Speter#ifdef __GNUC__
18817987Speter	/* Avoid longjmp clobbering */
18917987Speter	(void) &editor;
19017987Speter	(void) &lflg;
19117987Speter	(void) &nflg;
19217987Speter	(void) &rflg;
19317987Speter	(void) &sflg;
19417987Speter	(void) &firststr;
19517987Speter	(void) &laststr;
19617987Speter	(void) &pat;
19717987Speter	(void) &repl;
19817987Speter	(void) &efp;
19917987Speter	(void) &argc;
20017987Speter	(void) &argv;
20117987Speter#endif
2021556Srgrimes
2031556Srgrimes	if (hist == NULL)
2041556Srgrimes		error("history not active");
2058855Srgrimes
2061556Srgrimes	if (argc == 1)
2071556Srgrimes		error("missing history argument");
2081556Srgrimes
2091556Srgrimes	optreset = 1; optind = 1; /* initialize getopt */
2101556Srgrimes	while (not_fcnumber(argv[optind]) &&
21124348Simp	      (ch = getopt(argc, argv, ":e:lnrs")) != -1)
2121556Srgrimes		switch ((char)ch) {
2131556Srgrimes		case 'e':
21497731Stjr			editor = optarg;
2151556Srgrimes			break;
2161556Srgrimes		case 'l':
2171556Srgrimes			lflg = 1;
2181556Srgrimes			break;
2191556Srgrimes		case 'n':
2201556Srgrimes			nflg = 1;
2211556Srgrimes			break;
2221556Srgrimes		case 'r':
2231556Srgrimes			rflg = 1;
2241556Srgrimes			break;
2251556Srgrimes		case 's':
2261556Srgrimes			sflg = 1;
2271556Srgrimes			break;
2281556Srgrimes		case ':':
2291556Srgrimes			error("option -%c expects argument", optopt);
2301556Srgrimes		case '?':
2311556Srgrimes		default:
2321556Srgrimes			error("unknown option: -%c", optopt);
2331556Srgrimes		}
2341556Srgrimes	argc -= optind, argv += optind;
2351556Srgrimes
2361556Srgrimes	/*
2371556Srgrimes	 * If executing...
2381556Srgrimes	 */
2391556Srgrimes	if (lflg == 0 || editor || sflg) {
2401556Srgrimes		lflg = 0;	/* ignore */
2411556Srgrimes		editfile[0] = '\0';
2421556Srgrimes		/*
2431556Srgrimes		 * Catch interrupts to reset active counter and
2441556Srgrimes		 * cleanup temp files.
2451556Srgrimes		 */
2461556Srgrimes		if (setjmp(jmploc.loc)) {
2471556Srgrimes			active = 0;
2481556Srgrimes			if (*editfile)
2491556Srgrimes				unlink(editfile);
2501556Srgrimes			handler = savehandler;
2511556Srgrimes			longjmp(handler->loc, 1);
2521556Srgrimes		}
2531556Srgrimes		savehandler = handler;
2541556Srgrimes		handler = &jmploc;
2551556Srgrimes		if (++active > MAXHISTLOOPS) {
2561556Srgrimes			active = 0;
2571556Srgrimes			displayhist = 0;
2581556Srgrimes			error("called recursively too many times");
2591556Srgrimes		}
2601556Srgrimes		/*
2611556Srgrimes		 * Set editor.
2621556Srgrimes		 */
2631556Srgrimes		if (sflg == 0) {
2641556Srgrimes			if (editor == NULL &&
2651556Srgrimes			    (editor = bltinlookup("FCEDIT", 1)) == NULL &&
2661556Srgrimes			    (editor = bltinlookup("EDITOR", 1)) == NULL)
2671556Srgrimes				editor = DEFEDITOR;
2681556Srgrimes			if (editor[0] == '-' && editor[1] == '\0') {
2691556Srgrimes				sflg = 1;	/* no edit */
2701556Srgrimes				editor = NULL;
2711556Srgrimes			}
2721556Srgrimes		}
2731556Srgrimes	}
2741556Srgrimes
2751556Srgrimes	/*
2761556Srgrimes	 * If executing, parse [old=new] now
2771556Srgrimes	 */
2788855Srgrimes	if (lflg == 0 && argc > 0 &&
2791556Srgrimes	     ((repl = strchr(argv[0], '=')) != NULL)) {
2801556Srgrimes		pat = argv[0];
2811556Srgrimes		*repl++ = '\0';
2821556Srgrimes		argc--, argv++;
2831556Srgrimes	}
2841556Srgrimes	/*
2851556Srgrimes	 * determine [first] and [last]
2861556Srgrimes	 */
2871556Srgrimes	switch (argc) {
2881556Srgrimes	case 0:
2891556Srgrimes		firststr = lflg ? "-16" : "-1";
2901556Srgrimes		laststr = "-1";
2911556Srgrimes		break;
2921556Srgrimes	case 1:
2931556Srgrimes		firststr = argv[0];
2941556Srgrimes		laststr = lflg ? "-1" : argv[0];
2951556Srgrimes		break;
2961556Srgrimes	case 2:
2971556Srgrimes		firststr = argv[0];
2981556Srgrimes		laststr = argv[1];
2991556Srgrimes		break;
3001556Srgrimes	default:
3011556Srgrimes		error("too many args");
3021556Srgrimes	}
3031556Srgrimes	/*
3041556Srgrimes	 * Turn into event numbers.
3051556Srgrimes	 */
3061556Srgrimes	first = str_to_event(firststr, 0);
3071556Srgrimes	last = str_to_event(laststr, 1);
3081556Srgrimes
3091556Srgrimes	if (rflg) {
3101556Srgrimes		i = last;
3111556Srgrimes		last = first;
3121556Srgrimes		first = i;
3131556Srgrimes	}
3141556Srgrimes	/*
3151556Srgrimes	 * XXX - this should not depend on the event numbers
3168855Srgrimes	 * always increasing.  Add sequence numbers or offset
3171556Srgrimes	 * to the history element in next (diskbased) release.
3181556Srgrimes	 */
3191556Srgrimes	direction = first < last ? H_PREV : H_NEXT;
3201556Srgrimes
3211556Srgrimes	/*
3221556Srgrimes	 * If editing, grab a temp file.
3231556Srgrimes	 */
3241556Srgrimes	if (editor) {
3251556Srgrimes		int fd;
3261556Srgrimes		INTOFF;		/* easier */
3271556Srgrimes		sprintf(editfile, "%s/_shXXXXXX", _PATH_TMP);
3281556Srgrimes		if ((fd = mkstemp(editfile)) < 0)
3291556Srgrimes			error("can't create temporary file %s", editfile);
3301556Srgrimes		if ((efp = fdopen(fd, "w")) == NULL) {
3311556Srgrimes			close(fd);
33218016Speter			error("can't allocate stdio buffer for temp");
3331556Srgrimes		}
3341556Srgrimes	}
3351556Srgrimes
3361556Srgrimes	/*
3371556Srgrimes	 * Loop through selected history events.  If listing or executing,
3381556Srgrimes	 * do it now.  Otherwise, put into temp file and call the editor
3391556Srgrimes	 * after.
3401556Srgrimes	 *
3411556Srgrimes	 * The history interface needs rethinking, as the following
3421556Srgrimes	 * convolutions will demonstrate.
3431556Srgrimes	 */
34484261Sobrien	history(hist, &he, H_FIRST);
34584261Sobrien	retval = history(hist, &he, H_NEXT_EVENT, first);
34684261Sobrien	for (;retval != -1; retval = history(hist, &he, direction)) {
3471556Srgrimes		if (lflg) {
3481556Srgrimes			if (!nflg)
34984261Sobrien				out1fmt("%5d ", he.num);
35084261Sobrien			out1str(he.str);
3511556Srgrimes		} else {
3528855Srgrimes			char *s = pat ?
35384261Sobrien			   fc_replace(he.str, pat, repl) : (char *)he.str;
3541556Srgrimes
3551556Srgrimes			if (sflg) {
3561556Srgrimes				if (displayhist) {
3571556Srgrimes					out2str(s);
3581556Srgrimes				}
3591556Srgrimes				evalstring(s);
3601556Srgrimes				if (displayhist && hist) {
3611556Srgrimes					/*
3628855Srgrimes					 *  XXX what about recursive and
3631556Srgrimes					 *  relative histnums.
3641556Srgrimes					 */
36597730Stjr					oldhistnum = he.num;
36684261Sobrien					history(hist, &he, H_ENTER, s);
36797730Stjr					/*
36897730Stjr					 * XXX H_ENTER moves the internal
36997730Stjr					 * cursor, set it back to the current
37097730Stjr					 * entry.
37197730Stjr					 */
37297730Stjr					retval = history(hist, &he,
37397730Stjr					    H_NEXT_EVENT, oldhistnum);
3741556Srgrimes				}
3751556Srgrimes			} else
3761556Srgrimes				fputs(s, efp);
3771556Srgrimes		}
3781556Srgrimes		/*
3791556Srgrimes		 * At end?  (if we were to loose last, we'd sure be
3801556Srgrimes		 * messed up).
3811556Srgrimes		 */
38284261Sobrien		if (he.num == last)
3831556Srgrimes			break;
3841556Srgrimes	}
3851556Srgrimes	if (editor) {
3861556Srgrimes		char *editcmd;
3871556Srgrimes
3881556Srgrimes		fclose(efp);
3891556Srgrimes		editcmd = stalloc(strlen(editor) + strlen(editfile) + 2);
3901556Srgrimes		sprintf(editcmd, "%s %s", editor, editfile);
3911556Srgrimes		evalstring(editcmd);	/* XXX - should use no JC command */
3921556Srgrimes		INTON;
3931556Srgrimes		readcmdfile(editfile);	/* XXX - should read back - quick tst */
3941556Srgrimes		unlink(editfile);
3951556Srgrimes	}
3968855Srgrimes
3971556Srgrimes	if (lflg == 0 && active > 0)
3981556Srgrimes		--active;
3991556Srgrimes	if (displayhist)
4001556Srgrimes		displayhist = 0;
40117987Speter	return 0;
4021556Srgrimes}
4031556Srgrimes
4041556SrgrimesSTATIC char *
40590111Simpfc_replace(const char *s, char *p, char *r)
4061556Srgrimes{
4071556Srgrimes	char *dest;
4081556Srgrimes	int plen = strlen(p);
4091556Srgrimes
4101556Srgrimes	STARTSTACKSTR(dest);
4111556Srgrimes	while (*s) {
4121556Srgrimes		if (*s == *p && strncmp(s, p, plen) == 0) {
4131556Srgrimes			while (*r)
4141556Srgrimes				STPUTC(*r++, dest);
4151556Srgrimes			s += plen;
4161556Srgrimes			*p = '\0';	/* so no more matches */
4171556Srgrimes		} else
4181556Srgrimes			STPUTC(*s++, dest);
4191556Srgrimes	}
4201556Srgrimes	STACKSTRNUL(dest);
4211556Srgrimes	dest = grabstackstr(dest);
4221556Srgrimes
4231556Srgrimes	return (dest);
4241556Srgrimes}
4251556Srgrimes
42617987Speterint
42790111Simpnot_fcnumber(char *s)
4281556Srgrimes{
42920425Ssteve	if (s == NULL)
43025224Ssteve		return (0);
43120425Ssteve	if (*s == '-')
43220425Ssteve		s++;
4331556Srgrimes	return (!is_number(s));
4341556Srgrimes}
4351556Srgrimes
43617987Speterint
43790111Simpstr_to_event(char *str, int last)
4381556Srgrimes{
43984261Sobrien	HistEvent he;
4401556Srgrimes	char *s = str;
4411556Srgrimes	int relative = 0;
44284261Sobrien	int i, retval;
4431556Srgrimes
44484261Sobrien	retval = history(hist, &he, H_FIRST);
4451556Srgrimes	switch (*s) {
4461556Srgrimes	case '-':
4471556Srgrimes		relative = 1;
4481556Srgrimes		/*FALLTHROUGH*/
4491556Srgrimes	case '+':
4501556Srgrimes		s++;
4511556Srgrimes	}
4521556Srgrimes	if (is_number(s)) {
4531556Srgrimes		i = atoi(s);
4541556Srgrimes		if (relative) {
45584261Sobrien			while (retval != -1 && i--) {
45684261Sobrien				retval = history(hist, &he, H_NEXT);
4571556Srgrimes			}
45884261Sobrien			if (retval == -1)
45984261Sobrien				retval = history(hist, &he, H_LAST);
4601556Srgrimes		} else {
46184261Sobrien			retval = history(hist, &he, H_NEXT_EVENT, i);
46284261Sobrien			if (retval == -1) {
4631556Srgrimes				/*
4641556Srgrimes				 * the notion of first and last is
4651556Srgrimes				 * backwards to that of the history package
4661556Srgrimes				 */
46784261Sobrien				retval = history(hist, &he, last ? H_FIRST : H_LAST);
4681556Srgrimes			}
4691556Srgrimes		}
47084261Sobrien		if (retval == -1)
4711556Srgrimes			error("history number %s not found (internal error)",
4721556Srgrimes			       str);
4731556Srgrimes	} else {
4741556Srgrimes		/*
4758855Srgrimes		 * pattern
4761556Srgrimes		 */
47784261Sobrien		retval = history(hist, &he, H_PREV_STR, str);
47884261Sobrien		if (retval == -1)
4791556Srgrimes			error("history pattern not found: %s", str);
4801556Srgrimes	}
48184261Sobrien	return (he.num);
4821556Srgrimes}
48325224Ssteve#else
48425224Ssteve#include "error.h"
48525224Ssteve
48625224Ssteveint
48790111Simphistcmd(int argc, char **argv)
48825224Ssteve{
48925224Ssteve
49025224Ssteve	error("not compiled with history support");
49125224Ssteve	/*NOTREACHED*/
49225224Ssteve	return (0);
49325224Ssteve}
49425224Ssteve#endif
495