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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)histedit.c 8.2 (Berkeley) 5/4/95
37 * $FreeBSD: head/bin/sh/histedit.c 240541 2012-09-15 21:56:30Z jilles $
40 #include <sys/param.h>
47 * Editline and history functions (and glue).
56 #include "myhistedit.h"
63 #define MAXHISTLOOPS 4 /* max recursions through fc */
64 #define DEFEDITOR "ed" /* default editor *should* be $EDITOR */
66 History *hist; /* history cookie */
67 EditLine *el; /* editline cookie */
69 static FILE *el_in, *el_out, *el_err;
71 static char *fc_replace(const char *, char *, char *);
72 static int not_fcnumber(const char *);
73 static int str_to_event(const char *, int);
76 * Set history and editing status. Called whenever the status may
77 * have changed (figures out what to do).
83 #define editing (Eflag || Vflag)
91 hist = history_init();
95 sethistsize(histsizeval());
97 out2fmt_flush("sh: can't initialize history\n");
99 if (editing && !el && isatty(0)) { /* && isatty(2) ??? */
107 el_in = fdopen(0, "r");
109 el_err = fdopen(1, "w");
111 el_out = fdopen(2, "w");
112 if (el_in == NULL || el_err == NULL || el_out == NULL)
114 term = lookupvar("TERM");
116 if (setenv("TERM", term, 1) == -1)
117 error("setenv: cannot set TERM=1");
121 el = el_init(arg0, el_in, el_out, el_err);
124 el_set(el, EL_HIST, history, hist);
125 el_set(el, EL_PROMPT, getprompt);
126 el_set(el, EL_ADDFN, "rl-complete",
127 "ReadLine compatible completion function",
131 out2fmt_flush("sh: can't initialize editing\n");
134 } else if (!editing && el) {
142 el_set(el, EL_EDITOR, "vi");
144 el_set(el, EL_EDITOR, "emacs");
145 el_set(el, EL_BIND, "^I",
146 tabcomplete ? "rl-complete" : "ed-insert", NULL);
151 if (el) { /* no editing if not interactive */
165 sethistsize(const char *hs)
171 if (hs == NULL || *hs == '\0' ||
172 (histsize = atoi(hs)) < 0)
174 history(hist, &he, H_SETSIZE, histsize);
175 history(hist, &he, H_SETUNIQUE, 1);
180 setterm(const char *term)
182 if (rootshell && el != NULL && term != NULL)
183 el_set(el, EL_TERMINAL, term);
187 histcmd(int argc, char **argv __unused)
190 const char *volatile editor = NULL;
192 volatile int lflg = 0, nflg = 0, rflg = 0, sflg = 0;
194 const char *firststr = NULL, *laststr = NULL;
195 int first, last, direction;
196 char *pat = NULL, *repl = NULL;
197 static int active = 0;
198 struct jmploc jmploc;
199 struct jmploc *savehandler;
200 char editfilestr[PATH_MAX];
201 char *volatile editfile;
206 error("history not active");
209 error("missing history argument");
211 while (not_fcnumber(*argptr) && (ch = nextopt("e:lnrs")) != '\0')
230 savehandler = handler;
234 if (lflg == 0 || editor || sflg) {
235 lflg = 0; /* ignore */
238 * Catch interrupts to reset active counter and
239 * cleanup temp files.
241 if (setjmp(jmploc.loc)) {
245 handler = savehandler;
246 longjmp(handler->loc, 1);
249 if (++active > MAXHISTLOOPS) {
252 error("called recursively too many times");
258 if (editor == NULL &&
259 (editor = bltinlookup("FCEDIT", 1)) == NULL &&
260 (editor = bltinlookup("EDITOR", 1)) == NULL)
262 if (editor[0] == '-' && editor[1] == '\0') {
263 sflg = 1; /* no edit */
270 * If executing, parse [old=new] now
272 if (lflg == 0 && *argptr != NULL &&
273 ((repl = strchr(*argptr, '=')) != NULL)) {
279 * determine [first] and [last]
281 if (*argptr == NULL) {
282 firststr = lflg ? "-16" : "-1";
284 } else if (argptr[1] == NULL) {
285 firststr = argptr[0];
286 laststr = lflg ? "-1" : argptr[0];
287 } else if (argptr[2] == NULL) {
288 firststr = argptr[0];
291 error("too many arguments");
293 * Turn into event numbers.
295 first = str_to_event(firststr, 0);
296 last = str_to_event(laststr, 1);
304 * XXX - this should not depend on the event numbers
305 * always increasing. Add sequence numbers or offset
306 * to the history element in next (diskbased) release.
308 direction = first < last ? H_PREV : H_NEXT;
311 * If editing, grab a temp file.
316 sprintf(editfilestr, "%s/_shXXXXXX", _PATH_TMP);
317 if ((fd = mkstemp(editfilestr)) < 0)
318 error("can't create temporary file %s", editfile);
319 editfile = editfilestr;
320 if ((efp = fdopen(fd, "w")) == NULL) {
322 error("Out of space");
327 * Loop through selected history events. If listing or executing,
328 * do it now. Otherwise, put into temp file and call the editor
331 * The history interface needs rethinking, as the following
332 * convolutions will demonstrate.
334 history(hist, &he, H_FIRST);
335 retval = history(hist, &he, H_NEXT_EVENT, first);
336 for (;retval != -1; retval = history(hist, &he, direction)) {
339 out1fmt("%5d ", he.num);
343 fc_replace(he.str, pat, repl) :
344 __DECONST(char *, he.str);
352 if (displayhist && hist) {
354 * XXX what about recursive and
358 history(hist, &he, H_ENTER, s);
360 * XXX H_ENTER moves the internal
361 * cursor, set it back to the current
364 retval = history(hist, &he,
365 H_NEXT_EVENT, oldhistnum);
371 * At end? (if we were to lose last, we'd sure be
381 editcmd = stalloc(strlen(editor) + strlen(editfile) + 2);
382 sprintf(editcmd, "%s %s", editor, editfile);
383 evalstring(editcmd, 0); /* XXX - should use no JC command */
385 readcmdfile(editfile); /* XXX - should read back - quick tst */
389 if (lflg == 0 && active > 0)
393 handler = savehandler;
398 fc_replace(const char *s, char *p, char *r)
401 int plen = strlen(p);
405 if (*s == *p && strncmp(s, p, plen) == 0) {
408 *p = '\0'; /* so no more matches */
413 dest = grabstackstr(dest);
419 not_fcnumber(const char *s)
425 return (!is_number(s));
429 str_to_event(const char *str, int last)
436 retval = history(hist, &he, H_FIRST);
447 while (retval != -1 && i--) {
448 retval = history(hist, &he, H_NEXT);
451 retval = history(hist, &he, H_LAST);
453 retval = history(hist, &he, H_NEXT_EVENT, i);
456 * the notion of first and last is
457 * backwards to that of the history package
459 retval = history(hist, &he, last ? H_FIRST : H_LAST);
463 error("history number %s not found (internal error)",
469 retval = history(hist, &he, H_PREV_STR, str);
471 error("history pattern not found: %s", str);
477 bindcmd(int argc, char **argv)
481 error("line editing is disabled");
482 return (el_parse(el, argc, (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");