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 * 3. 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"
67 #define MAXHISTLOOPS 4 /* max recursions through fc */
68 #define DEFEDITOR "ed" /* default editor *should* be $EDITOR */
70 History *hist; /* history cookie */
71 EditLine *el; /* editline cookie */
73 static FILE *el_in, *el_out, *el_err;
75 static char *fc_replace(const char *, char *, char *);
76 static int not_fcnumber(const char *);
77 static int str_to_event(const char *, int);
80 * Set history and editing status. Called whenever the status may
81 * have changed (figures out what to do).
87 #define editing (Eflag || Vflag)
95 hist = history_init();
99 sethistsize(histsizeval());
101 out2fmt_flush("sh: can't initialize history\n");
103 if (editing && !el && isatty(0)) { /* && isatty(2) ??? */
111 el_in = fdopen(0, "r");
113 el_err = fdopen(1, "w");
115 el_out = fdopen(2, "w");
116 if (el_in == NULL || el_err == NULL || el_out == NULL)
118 term = lookupvar("TERM");
120 setenv("TERM", term, 1);
123 el = el_init(arg0, el_in, el_out, el_err);
126 el_set(el, EL_HIST, history, hist);
127 el_set(el, EL_PROMPT, getprompt);
128 el_set(el, EL_ADDFN, "sh-complete",
129 "Filename completion",
133 out2fmt_flush("sh: can't initialize editing\n");
136 } else if (!editing && el) {
144 el_set(el, EL_EDITOR, "vi");
146 el_set(el, EL_EDITOR, "emacs");
147 el_set(el, EL_BIND, "^I", "sh-complete", NULL);
152 if (el) { /* no editing if not interactive */
166 sethistsize(const char *hs)
172 if (hs == NULL || !is_number(hs))
176 history(hist, &he, H_SETSIZE, histsize);
177 history(hist, &he, H_SETUNIQUE, 1);
182 setterm(const char *term)
184 if (rootshell && el != NULL && term != NULL)
185 el_set(el, EL_TERMINAL, term);
189 histcmd(int argc, char **argv __unused)
192 const char *editor = NULL;
194 int lflg = 0, nflg = 0, rflg = 0, sflg = 0;
196 const char *firststr, *laststr;
197 int first, last, direction;
198 char *pat = NULL, *repl = NULL;
199 static int active = 0;
200 struct jmploc jmploc;
201 struct jmploc *savehandler;
202 char editfilestr[PATH_MAX];
203 char *volatile editfile;
208 error("history not active");
211 error("missing history argument");
213 while (not_fcnumber(*argptr) && (ch = nextopt("e:lnrs")) != '\0')
232 savehandler = handler;
236 if (lflg == 0 || editor || sflg) {
237 lflg = 0; /* ignore */
240 * Catch interrupts to reset active counter and
241 * cleanup temp files.
243 if (setjmp(jmploc.loc)) {
247 handler = savehandler;
248 longjmp(handler->loc, 1);
251 if (++active > MAXHISTLOOPS) {
254 error("called recursively too many times");
260 if (editor == NULL &&
261 (editor = bltinlookup("FCEDIT", 1)) == NULL &&
262 (editor = bltinlookup("EDITOR", 1)) == NULL)
264 if (editor[0] == '-' && editor[1] == '\0') {
265 sflg = 1; /* no edit */
272 * If executing, parse [old=new] now
274 if (lflg == 0 && *argptr != NULL &&
275 ((repl = strchr(*argptr, '=')) != NULL)) {
281 * determine [first] and [last]
283 if (*argptr == NULL) {
284 firststr = lflg ? "-16" : "-1";
286 } else if (argptr[1] == NULL) {
287 firststr = argptr[0];
288 laststr = lflg ? "-1" : argptr[0];
289 } else if (argptr[2] == NULL) {
290 firststr = argptr[0];
293 error("too many arguments");
295 * Turn into event numbers.
297 first = str_to_event(firststr, 0);
298 last = str_to_event(laststr, 1);
306 * XXX - this should not depend on the event numbers
307 * always increasing. Add sequence numbers or offset
308 * to the history element in next (diskbased) release.
310 direction = first < last ? H_PREV : H_NEXT;
313 * If editing, grab a temp file.
318 sprintf(editfilestr, "%s/_shXXXXXX", _PATH_TMP);
319 if ((fd = mkstemp(editfilestr)) < 0)
320 error("can't create temporary file %s", editfile);
321 editfile = editfilestr;
322 if ((efp = fdopen(fd, "w")) == NULL) {
324 error("Out of space");
329 * Loop through selected history events. If listing or executing,
330 * do it now. Otherwise, put into temp file and call the editor
333 * The history interface needs rethinking, as the following
334 * convolutions will demonstrate.
336 history(hist, &he, H_FIRST);
337 retval = history(hist, &he, H_NEXT_EVENT, first);
338 for (;retval != -1; retval = history(hist, &he, direction)) {
341 out1fmt("%5d ", he.num);
344 const char *s = pat ?
345 fc_replace(he.str, pat, repl) : he.str;
353 if (displayhist && hist) {
355 * XXX what about recursive and
359 history(hist, &he, H_ENTER, s);
361 * XXX H_ENTER moves the internal
362 * cursor, set it back to the current
365 retval = history(hist, &he,
366 H_NEXT_EVENT, oldhistnum);
372 * At end? (if we were to lose last, we'd sure be
382 editcmd = stalloc(strlen(editor) + strlen(editfile) + 2);
383 sprintf(editcmd, "%s %s", editor, editfile);
384 evalstring(editcmd, 0); /* XXX - should use no JC command */
386 readcmdfile(editfile); /* XXX - should read back - quick tst */
390 if (lflg == 0 && active > 0)
394 handler = savehandler;
399 fc_replace(const char *s, char *p, char *r)
402 int plen = strlen(p);
406 if (*s == *p && strncmp(s, p, plen) == 0) {
409 *p = '\0'; /* so no more matches */
414 dest = grabstackstr(dest);
420 not_fcnumber(const char *s)
426 return (!is_number(s));
430 str_to_event(const char *str, int last)
437 retval = history(hist, &he, H_FIRST);
448 while (retval != -1 && i--) {
449 retval = history(hist, &he, H_NEXT);
452 retval = history(hist, &he, H_LAST);
454 retval = history(hist, &he, H_NEXT_EVENT, i);
457 * the notion of first and last is
458 * backwards to that of the history package
460 retval = history(hist, &he, last ? H_FIRST : H_LAST);
464 error("history number %s not found (internal error)",
470 retval = history(hist, &he, H_PREV_STR, str);
472 error("history pattern not found: %s", str);
478 bindcmd(int argc, char **argv)
482 error("line editing is disabled");
483 return (el_parse(el, argc, __DECONST(const char **, argv)));
489 histcmd(int argc __unused, char **argv __unused)
492 error("not compiled with history support");
498 bindcmd(int argc __unused, char **argv __unused)
501 error("not compiled with line editing support");