160705Sbostic /*-
260706Sbostic * Copyright (c) 1993
360706Sbostic * The Regents of the University of California. All rights reserved.
460705Sbostic *
560705Sbostic * This code is derived from software contributed to Berkeley by
660705Sbostic * Kenneth Almquist.
760705Sbostic *
860705Sbostic * %sccs.include.redist.c%
960705Sbostic */
1060705Sbostic
1160705Sbostic #ifndef lint
12*69272Schristos static char sccsid[] = "@(#)histedit.c 8.2 (Berkeley) 05/04/95";
1360705Sbostic #endif /* not lint */
1460705Sbostic
15*69272Schristos #include <sys/param.h>
16*69272Schristos #include <paths.h>
17*69272Schristos #include <stdio.h>
18*69272Schristos #include <stdlib.h>
19*69272Schristos #include <unistd.h>
2053861Smarc /*
2153861Smarc * Editline and history functions (and glue).
2253861Smarc */
2355242Smarc #include "shell.h"
2453861Smarc #include "parser.h"
2555242Smarc #include "var.h"
2653861Smarc #include "options.h"
27*69272Schristos #include "main.h"
28*69272Schristos #include "output.h"
2955242Smarc #include "mystring.h"
30*69272Schristos #ifndef NO_HISTORY
31*69272Schristos #include "myhistedit.h"
32*69272Schristos #endif
3353861Smarc #include "error.h"
34*69272Schristos #include "eval.h"
3555242Smarc #include "memalloc.h"
3653861Smarc
3755242Smarc #define MAXHISTLOOPS 4 /* max recursions through fc */
3855242Smarc #define DEFEDITOR "ed" /* default editor *should* be $EDITOR */
3955242Smarc
4053861Smarc History *hist; /* history cookie */
4153861Smarc EditLine *el; /* editline cookie */
4255242Smarc int displayhist;
4353861Smarc static FILE *el_in, *el_out;
4453861Smarc
4555242Smarc STATIC char *fc_replace __P((const char *, char *, char *));
4653861Smarc
4753861Smarc /*
4853861Smarc * Set history and editing status. Called whenever the status may
4953861Smarc * have changed (figures out what to do).
5053861Smarc */
51*69272Schristos void
histedit()52*69272Schristos histedit()
53*69272Schristos {
5453861Smarc
5555242Smarc #define editing (Eflag || Vflag)
5655242Smarc
5755242Smarc if (iflag) {
5853861Smarc if (!hist) {
5953861Smarc /*
6053861Smarc * turn history on
6153861Smarc */
6253861Smarc INTOFF;
6353861Smarc hist = history_init();
6453861Smarc INTON;
6553861Smarc
6653861Smarc if (hist != NULL)
6753861Smarc sethistsize();
6853861Smarc else
6953861Smarc out2str("sh: can't initialize history\n");
7053861Smarc }
7155242Smarc if (editing && !el && isatty(0)) { /* && isatty(2) ??? */
7253861Smarc /*
7353861Smarc * turn editing on
7453861Smarc */
7553861Smarc INTOFF;
7653861Smarc if (el_in == NULL)
7753861Smarc el_in = fdopen(0, "r");
7853861Smarc if (el_out == NULL)
7953861Smarc el_out = fdopen(2, "w");
8053861Smarc if (el_in == NULL || el_out == NULL)
8153861Smarc goto bad;
8253861Smarc el = el_init(arg0, el_in, el_out);
8353861Smarc if (el != NULL) {
8453861Smarc if (hist)
8555242Smarc el_set(el, EL_HIST, history, hist);
8653861Smarc el_set(el, EL_PROMPT, getprompt);
8753861Smarc } else {
8853861Smarc bad:
8953861Smarc out2str("sh: can't initialize editing\n");
9053861Smarc }
9153861Smarc INTON;
9255242Smarc } else if (!editing && el) {
9353861Smarc INTOFF;
9453861Smarc el_end(el);
9553861Smarc el = NULL;
9653861Smarc INTON;
9753861Smarc }
9855242Smarc if (el) {
9955242Smarc if (Vflag)
10055242Smarc el_set(el, EL_EDITOR, "vi");
10155242Smarc else if (Eflag)
10255242Smarc el_set(el, EL_EDITOR, "emacs");
10355242Smarc }
10453861Smarc } else {
10553861Smarc INTOFF;
10653861Smarc if (el) { /* no editing if not interactive */
10753861Smarc el_end(el);
10853861Smarc el = NULL;
10953861Smarc }
11053861Smarc if (hist) {
11153861Smarc history_end(hist);
11253861Smarc hist = NULL;
11353861Smarc }
11453861Smarc INTON;
11553861Smarc }
11653861Smarc }
11753861Smarc
118*69272Schristos
119*69272Schristos void
sethistsize()120*69272Schristos sethistsize()
121*69272Schristos {
12253861Smarc char *cp;
12353861Smarc int histsize;
12453861Smarc
12553861Smarc if (hist != NULL) {
12653861Smarc cp = lookupvar("HISTSIZE");
12753861Smarc if (cp == NULL || *cp == '\0' ||
12853861Smarc (histsize = atoi(cp)) < 0)
12953861Smarc histsize = 100;
13055242Smarc history(hist, H_EVENT, histsize);
13153861Smarc }
13253861Smarc }
13355242Smarc
13455242Smarc /*
13555242Smarc * This command is provided since POSIX decided to standardize
13655242Smarc * the Korn shell fc command. Oh well...
13755242Smarc */
138*69272Schristos int
histcmd(argc,argv)13955242Smarc histcmd(argc, argv)
140*69272Schristos int argc;
141*69272Schristos char **argv;
14255242Smarc {
14355242Smarc extern char *optarg;
14455242Smarc extern int optind, optopt, optreset;
14555242Smarc int ch;
14655242Smarc char *editor = NULL;
14755242Smarc const HistEvent *he;
14855242Smarc int lflg = 0, nflg = 0, rflg = 0, sflg = 0;
14955242Smarc int i;
15055242Smarc char *firststr, *laststr;
15155242Smarc int first, last, direction;
15255242Smarc char *pat = NULL, *repl; /* ksh "fc old=new" crap */
15355242Smarc static int active = 0;
15455242Smarc struct jmploc jmploc;
15555242Smarc struct jmploc *volatile savehandler;
15655242Smarc char editfile[MAXPATHLEN + 1];
15755242Smarc FILE *efp;
158*69272Schristos #ifdef __GNUC__
159*69272Schristos /* Avoid longjmp clobbering */
160*69272Schristos (void) &editor;
161*69272Schristos (void) &lflg;
162*69272Schristos (void) &nflg;
163*69272Schristos (void) &rflg;
164*69272Schristos (void) &sflg;
165*69272Schristos (void) &firststr;
166*69272Schristos (void) &laststr;
167*69272Schristos (void) &pat;
168*69272Schristos (void) &repl;
169*69272Schristos (void) &efp;
170*69272Schristos (void) &argc;
171*69272Schristos (void) &argv;
172*69272Schristos #endif
17355242Smarc
17455242Smarc if (hist == NULL)
17555242Smarc error("history not active");
17656930Sbostic
17756930Sbostic if (argc == 1)
17856930Sbostic error("missing history argument");
17955242Smarc
18055242Smarc optreset = 1; optind = 1; /* initialize getopt */
18155242Smarc while (not_fcnumber(argv[optind]) &&
18255242Smarc (ch = getopt(argc, argv, ":e:lnrs")) != EOF)
18355242Smarc switch ((char)ch) {
18455242Smarc case 'e':
18555242Smarc editor = optarg;
18655242Smarc break;
18755242Smarc case 'l':
18855242Smarc lflg = 1;
18955242Smarc break;
19055242Smarc case 'n':
19155242Smarc nflg = 1;
19255242Smarc break;
19355242Smarc case 'r':
19455242Smarc rflg = 1;
19555242Smarc break;
19655242Smarc case 's':
19755242Smarc sflg = 1;
19855242Smarc break;
19955242Smarc case ':':
20055242Smarc error("option -%c expects argument", optopt);
20155242Smarc case '?':
20255242Smarc default:
20355242Smarc error("unknown option: -%c", optopt);
20455242Smarc }
20555242Smarc argc -= optind, argv += optind;
20655242Smarc
20755242Smarc /*
20855242Smarc * If executing...
20955242Smarc */
21055242Smarc if (lflg == 0 || editor || sflg) {
21155242Smarc lflg = 0; /* ignore */
21255242Smarc editfile[0] = '\0';
21355242Smarc /*
21455242Smarc * Catch interrupts to reset active counter and
21555242Smarc * cleanup temp files.
21655242Smarc */
21755242Smarc if (setjmp(jmploc.loc)) {
21855242Smarc active = 0;
21955242Smarc if (*editfile)
22055242Smarc unlink(editfile);
22155242Smarc handler = savehandler;
22255242Smarc longjmp(handler->loc, 1);
22355242Smarc }
22455242Smarc savehandler = handler;
22555242Smarc handler = &jmploc;
22655242Smarc if (++active > MAXHISTLOOPS) {
22755242Smarc active = 0;
22855242Smarc displayhist = 0;
22955242Smarc error("called recursively too many times");
23055242Smarc }
23155242Smarc /*
23255242Smarc * Set editor.
23355242Smarc */
23455242Smarc if (sflg == 0) {
23555242Smarc if (editor == NULL &&
23655246Smarc (editor = bltinlookup("FCEDIT", 1)) == NULL &&
23755246Smarc (editor = bltinlookup("EDITOR", 1)) == NULL)
23855242Smarc editor = DEFEDITOR;
23955242Smarc if (editor[0] == '-' && editor[1] == '\0') {
24055242Smarc sflg = 1; /* no edit */
24155242Smarc editor = NULL;
24255242Smarc }
24355242Smarc }
24455242Smarc }
24555242Smarc
24655242Smarc /*
24755242Smarc * If executing, parse [old=new] now
24855242Smarc */
24955242Smarc if (lflg == 0 && argc > 0 &&
25055242Smarc ((repl = strchr(argv[0], '=')) != NULL)) {
25155242Smarc pat = argv[0];
25255242Smarc *repl++ = '\0';
25355242Smarc argc--, argv++;
25455242Smarc }
25555242Smarc /*
25655242Smarc * determine [first] and [last]
25755242Smarc */
25855242Smarc switch (argc) {
25955242Smarc case 0:
26055242Smarc firststr = lflg ? "-16" : "-1";
26155242Smarc laststr = "-1";
26255242Smarc break;
26355242Smarc case 1:
26455242Smarc firststr = argv[0];
26555242Smarc laststr = lflg ? "-1" : argv[0];
26655242Smarc break;
26755242Smarc case 2:
26855242Smarc firststr = argv[0];
26955242Smarc laststr = argv[1];
27055242Smarc break;
27155242Smarc default:
27255242Smarc error("too many args");
27355242Smarc }
27455242Smarc /*
27555242Smarc * Turn into event numbers.
27655242Smarc */
27755242Smarc first = str_to_event(firststr, 0);
27855242Smarc last = str_to_event(laststr, 1);
27955242Smarc
28055242Smarc if (rflg) {
28155242Smarc i = last;
28255242Smarc last = first;
28355242Smarc first = i;
28455242Smarc }
28555242Smarc /*
28655242Smarc * XXX - this should not depend on the event numbers
28755242Smarc * always increasing. Add sequence numbers or offset
28855242Smarc * to the history element in next (diskbased) release.
28955242Smarc */
29055242Smarc direction = first < last ? H_PREV : H_NEXT;
29155242Smarc
29255242Smarc /*
29355242Smarc * If editing, grab a temp file.
29455242Smarc */
29555242Smarc if (editor) {
29655242Smarc int fd;
29755242Smarc INTOFF; /* easier */
29855242Smarc sprintf(editfile, "%s/_shXXXXXX", _PATH_TMP);
29955242Smarc if ((fd = mkstemp(editfile)) < 0)
30055242Smarc error("can't create temporary file %s", editfile);
30155242Smarc if ((efp = fdopen(fd, "w")) == NULL) {
30255242Smarc close(fd);
30355242Smarc error("can't allocate stdio buffer for temp\n");
30455242Smarc }
30555242Smarc }
30655242Smarc
30755242Smarc /*
30855242Smarc * Loop through selected history events. If listing or executing,
30955242Smarc * do it now. Otherwise, put into temp file and call the editor
31055242Smarc * after.
31155242Smarc *
31255242Smarc * The history interface needs rethinking, as the following
31355242Smarc * convolutions will demonstrate.
31455242Smarc */
31555242Smarc history(hist, H_FIRST);
31655242Smarc he = history(hist, H_NEXT_EVENT, first);
31755242Smarc for (;he != NULL; he = history(hist, direction)) {
31855242Smarc if (lflg) {
31955242Smarc if (!nflg)
32055242Smarc out1fmt("%5d ", he->num);
32155242Smarc out1str(he->str);
32255242Smarc } else {
32355242Smarc char *s = pat ?
32455242Smarc fc_replace(he->str, pat, repl) : (char *)he->str;
32555242Smarc
32655242Smarc if (sflg) {
32755242Smarc if (displayhist) {
32855242Smarc out2str(s);
32955242Smarc }
33055242Smarc evalstring(s);
33155242Smarc if (displayhist && hist) {
33255242Smarc /*
33355242Smarc * XXX what about recursive and
33455242Smarc * relative histnums.
33555242Smarc */
33655242Smarc history(hist, H_ENTER, s);
33755242Smarc }
33855242Smarc } else
33955242Smarc fputs(s, efp);
34055242Smarc }
34155242Smarc /*
34255242Smarc * At end? (if we were to loose last, we'd sure be
34355242Smarc * messed up).
34455242Smarc */
34555242Smarc if (he->num == last)
34655242Smarc break;
34755242Smarc }
34855242Smarc if (editor) {
34955242Smarc char *editcmd;
35055242Smarc
35155242Smarc fclose(efp);
35255242Smarc editcmd = stalloc(strlen(editor) + strlen(editfile) + 2);
35355242Smarc sprintf(editcmd, "%s %s", editor, editfile);
35455242Smarc evalstring(editcmd); /* XXX - should use no JC command */
35555242Smarc INTON;
35655242Smarc readcmdfile(editfile); /* XXX - should read back - quick tst */
35755242Smarc unlink(editfile);
35855242Smarc }
35955242Smarc
36055242Smarc if (lflg == 0 && active > 0)
36155242Smarc --active;
36255242Smarc if (displayhist)
36355242Smarc displayhist = 0;
364*69272Schristos return 0;
36555242Smarc }
36655242Smarc
36755242Smarc STATIC char *
fc_replace(s,p,r)36855242Smarc fc_replace(s, p, r)
36955242Smarc const char *s;
37055242Smarc char *p, *r;
37155242Smarc {
37255242Smarc char *dest;
37355242Smarc int plen = strlen(p);
37455242Smarc
37555242Smarc STARTSTACKSTR(dest);
37655242Smarc while (*s) {
37755242Smarc if (*s == *p && strncmp(s, p, plen) == 0) {
37855242Smarc while (*r)
37955242Smarc STPUTC(*r++, dest);
38055242Smarc s += plen;
38155242Smarc *p = '\0'; /* so no more matches */
38255242Smarc } else
38355242Smarc STPUTC(*s++, dest);
38455242Smarc }
38555242Smarc STACKSTRNUL(dest);
38655242Smarc dest = grabstackstr(dest);
38755242Smarc
38855242Smarc return (dest);
38955242Smarc }
39055242Smarc
391*69272Schristos int
not_fcnumber(s)39255242Smarc not_fcnumber(s)
39355242Smarc char *s;
39455242Smarc {
395*69272Schristos if (s == NULL)
396*69272Schristos return 0;
39755242Smarc if (*s == '-')
39855242Smarc s++;
39955242Smarc return (!is_number(s));
40055242Smarc }
40155242Smarc
402*69272Schristos int
str_to_event(str,last)40355242Smarc str_to_event(str, last)
40455242Smarc char *str;
40555242Smarc int last;
40655242Smarc {
40755242Smarc const HistEvent *he;
40855242Smarc char *s = str;
40955242Smarc int relative = 0;
410*69272Schristos int i;
41155242Smarc
41255242Smarc he = history(hist, H_FIRST);
41355242Smarc switch (*s) {
41455242Smarc case '-':
41555242Smarc relative = 1;
41655242Smarc /*FALLTHROUGH*/
41755242Smarc case '+':
41855242Smarc s++;
41955242Smarc }
42055242Smarc if (is_number(s)) {
42155242Smarc i = atoi(s);
42255242Smarc if (relative) {
42355242Smarc while (he != NULL && i--) {
42455242Smarc he = history(hist, H_NEXT);
42555242Smarc }
42655242Smarc if (he == NULL)
42755242Smarc he = history(hist, H_LAST);
42855242Smarc } else {
42955242Smarc he = history(hist, H_NEXT_EVENT, i);
43055242Smarc if (he == NULL) {
43155242Smarc /*
43255242Smarc * the notion of first and last is
43355242Smarc * backwards to that of the history package
43455242Smarc */
43555242Smarc he = history(hist, last ? H_FIRST : H_LAST);
43655242Smarc }
43755242Smarc }
43855242Smarc if (he == NULL)
43955242Smarc error("history number %s not found (internal error)",
44055242Smarc str);
44155242Smarc } else {
44255242Smarc /*
44355242Smarc * pattern
44455242Smarc */
44555298Smarc he = history(hist, H_PREV_STR, str);
44655242Smarc if (he == NULL)
44755242Smarc error("history pattern not found: %s", str);
44855242Smarc }
44955242Smarc return (he->num);
45055242Smarc }
451