3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char sccsid[] = "@(#)histedit.c 8.2 (Berkeley) 5/4/95";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
48 * Editline and history functions (and glue).
58 #include "myhistedit.h"
64 #define MAXHISTLOOPS 4 /* max recursions through fc */
65 #define DEFEDITOR "ed" /* default editor *should* be $EDITOR */
67 History *hist; /* history cookie */
68 EditLine *el; /* editline cookie */
70 static FILE *el_in, *el_out, *el_err;
72 static char *fc_replace(const char *, char *, char *);
75 * Set history and editing status. Called whenever the status may
76 * have changed (figures out what to do).
82 #define editing (Eflag || Vflag)
90 hist = history_init();
94 sethistsize(histsizeval());
96 out2fmt_flush("sh: can't initialize history\n");
98 if (editing && !el && isatty(0)) { /* && isatty(2) ??? */
106 el_in = fdopen(0, "r");
108 el_err = fdopen(1, "w");
110 el_out = fdopen(2, "w");
111 if (el_in == NULL || el_err == NULL || el_out == NULL)
113 term = lookupvar("TERM");
115 setenv("TERM", term, 1);
118 el = el_init(arg0, el_in, el_out, el_err);
121 el_set(el, EL_HIST, history, hist);
122 el_set(el, EL_PROMPT, getprompt);
123 el_set(el, EL_ADDFN, "sh-complete",
124 "Filename completion",
128 out2fmt_flush("sh: can't initialize editing\n");
131 } else if (!editing && el) {
139 el_set(el, EL_EDITOR, "vi");
141 el_set(el, EL_EDITOR, "emacs");
142 el_set(el, EL_BIND, "^I", "sh-complete", NULL);
147 if (el) { /* no editing if not interactive */
168 if (hs == NULL || *hs == '\0' ||
169 (histsize = atoi(hs)) < 0)
171 history(hist, &he, H_SETSIZE, histsize);
172 history(hist, &he, H_SETUNIQUE, 1);
177 setterm(const char *term)
179 if (rootshell && el != NULL && term != NULL)
180 el_set(el, EL_TERMINAL, term);
184 histcmd(int argc, char **argv)
187 const char *editor = NULL;
189 int lflg = 0, nflg = 0, rflg = 0, sflg = 0;
191 const char *firststr, *laststr;
192 int first, last, direction;
193 char *pat = NULL, *repl = NULL;
194 static int active = 0;
195 struct jmploc jmploc;
196 struct jmploc *savehandler;
197 char editfilestr[PATH_MAX];
198 char *volatile editfile;
203 error("history not active");
206 error("missing history argument");
208 optreset = 1; optind = 1; /* initialize getopt */
210 while (not_fcnumber(argv[optind]) &&
211 (ch = getopt(argc, argv, ":e:lnrs")) != -1)
229 error("option -%c expects argument", optopt);
232 error("unknown option: -%c", optopt);
234 argc -= optind, argv += optind;
236 savehandler = handler;
240 if (lflg == 0 || editor || sflg) {
241 lflg = 0; /* ignore */
244 * Catch interrupts to reset active counter and
245 * cleanup temp files.
247 if (setjmp(jmploc.loc)) {
251 handler = savehandler;
252 longjmp(handler->loc, 1);
255 if (++active > MAXHISTLOOPS) {
258 error("called recursively too many times");
264 if (editor == NULL &&
265 (editor = bltinlookup("FCEDIT", 1)) == NULL &&
266 (editor = bltinlookup("EDITOR", 1)) == NULL)
268 if (editor[0] == '-' && editor[1] == '\0') {
269 sflg = 1; /* no edit */
276 * If executing, parse [old=new] now
278 if (lflg == 0 && argc > 0 &&
279 ((repl = strchr(argv[0], '=')) != NULL)) {
285 * determine [first] and [last]
289 firststr = lflg ? "-16" : "-1";
294 laststr = lflg ? "-1" : argv[0];
301 error("too many arguments");
304 * Turn into event numbers.
306 first = str_to_event(firststr, 0);
307 last = str_to_event(laststr, 1);
315 * XXX - this should not depend on the event numbers
316 * always increasing. Add sequence numbers or offset
317 * to the history element in next (diskbased) release.
319 direction = first < last ? H_PREV : H_NEXT;
322 * If editing, grab a temp file.
327 sprintf(editfilestr, "%s/_shXXXXXX", _PATH_TMP);
328 if ((fd = mkstemp(editfilestr)) < 0)
329 error("can't create temporary file %s", editfile);
330 editfile = editfilestr;
331 if ((efp = fdopen(fd, "w")) == NULL) {
333 error("Out of space");
338 * Loop through selected history events. If listing or executing,
339 * do it now. Otherwise, put into temp file and call the editor
342 * The history interface needs rethinking, as the following
343 * convolutions will demonstrate.
345 history(hist, &he, H_FIRST);
346 retval = history(hist, &he, H_NEXT_EVENT, first);
347 for (;retval != -1; retval = history(hist, &he, direction)) {
350 out1fmt("%5d ", he.num);
354 fc_replace(he.str, pat, repl) : (char *)he.str;
362 if (displayhist && hist) {
364 * XXX what about recursive and
368 history(hist, &he, H_ENTER, s);
370 * XXX H_ENTER moves the internal
371 * cursor, set it back to the current
374 retval = history(hist, &he,
375 H_NEXT_EVENT, oldhistnum);
381 * At end? (if we were to lose last, we'd sure be
391 editcmd = stalloc(strlen(editor) + strlen(editfile) + 2);
392 sprintf(editcmd, "%s %s", editor, editfile);
393 evalstring(editcmd, 0); /* XXX - should use no JC command */
395 readcmdfile(editfile); /* XXX - should read back - quick tst */
399 if (lflg == 0 && active > 0)
403 handler = savehandler;
408 fc_replace(const char *s, char *p, char *r)
411 int plen = strlen(p);
415 if (*s == *p && strncmp(s, p, plen) == 0) {
418 *p = '\0'; /* so no more matches */
423 dest = grabstackstr(dest);
429 not_fcnumber(const char *s)
435 return (!is_number(s));
439 str_to_event(const char *str, int last)
446 retval = history(hist, &he, H_FIRST);
457 while (retval != -1 && i--) {
458 retval = history(hist, &he, H_NEXT);
461 retval = history(hist, &he, H_LAST);
463 retval = history(hist, &he, H_NEXT_EVENT, i);
466 * the notion of first and last is
467 * backwards to that of the history package
469 retval = history(hist, &he, last ? H_FIRST : H_LAST);
473 error("history number %s not found (internal error)",
479 retval = history(hist, &he, H_PREV_STR, str);
481 error("history pattern not found: %s", str);
487 bindcmd(int argc, char **argv)
491 error("line editing is disabled");
492 return (el_parse(el, argc, (const char **)argv));
499 histcmd(int argc, char **argv)
502 error("not compiled with history support");
508 bindcmd(int argc, char **argv)
511 error("not compiled with line editing support");