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
32 * @(#)histedit.c 8.2 (Berkeley) 5/4/95
33 * $FreeBSD: head/bin/sh/histedit.c 240541 2012-09-15 21:56:30Z jilles $
36 #include <sys/param.h>
43 * Editline and history functions (and glue).
52 #include "myhistedit.h"
59 #define MAXHISTLOOPS 4 /* max recursions through fc */
60 #define DEFEDITOR "ed" /* default editor *should* be $EDITOR */
62 History *hist; /* history cookie */
63 EditLine *el; /* editline cookie */
65 static FILE *el_in, *el_out, *el_err;
67 static char *fc_replace(const char *, char *, char *);
68 static int not_fcnumber(const char *);
69 static int str_to_event(const char *, int);
72 * Set history and editing status. Called whenever the status may
73 * have changed (figures out what to do).
79 #define editing (Eflag || Vflag)
87 hist = history_init();
91 sethistsize(histsizeval());
93 out2fmt_flush("sh: can't initialize history\n");
95 if (editing && !el && isatty(0)) { /* && isatty(2) ??? */
103 el_in = fdopen(0, "r");
105 el_err = fdopen(1, "w");
107 el_out = fdopen(2, "w");
108 if (el_in == NULL || el_err == NULL || el_out == NULL)
110 term = lookupvar("TERM");
112 if (setenv("TERM", term, 1) == -1)
113 error("setenv: cannot set TERM=1");
117 el = el_init(arg0, el_in, el_out, el_err);
120 el_set(el, EL_HIST, history, hist);
121 el_set(el, EL_PROMPT, getprompt);
122 el_set(el, EL_ADDFN, "rl-complete",
123 "ReadLine compatible completion function",
127 out2fmt_flush("sh: can't initialize editing\n");
130 } else if (!editing && el) {
138 el_set(el, EL_EDITOR, "vi");
140 el_set(el, EL_EDITOR, "emacs");
141 el_set(el, EL_BIND, "^I",
142 tabcomplete ? "rl-complete" : "ed-insert", NULL);
147 if (el) { /* no editing if not interactive */
161 sethistsize(const char *hs)
167 if (hs == NULL || *hs == '\0' ||
168 (histsize = atoi(hs)) < 0)
170 history(hist, &he, H_SETSIZE, histsize);
171 history(hist, &he, H_SETUNIQUE, 1);
176 setterm(const char *term)
178 if (rootshell && el != NULL && term != NULL)
179 el_set(el, EL_TERMINAL, term);
183 histcmd(int argc, char **argv __unused)
186 const char *volatile editor = NULL;
188 volatile int lflg = 0, nflg = 0, rflg = 0, sflg = 0;
190 const char *firststr = NULL, *laststr = NULL;
191 int first, last, direction;
192 char *pat = NULL, *repl = NULL;
193 static int active = 0;
194 struct jmploc jmploc;
195 struct jmploc *savehandler;
196 char editfilestr[PATH_MAX];
197 char *volatile editfile;
202 error("history not active");
205 error("missing history argument");
207 while (not_fcnumber(*argptr) && (ch = nextopt("e:lnrs")) != '\0')
226 savehandler = handler;
230 if (lflg == 0 || editor || sflg) {
231 lflg = 0; /* ignore */
234 * Catch interrupts to reset active counter and
235 * cleanup temp files.
237 if (setjmp(jmploc.loc)) {
241 handler = savehandler;
242 longjmp(handler->loc, 1);
245 if (++active > MAXHISTLOOPS) {
248 error("called recursively too many times");
254 if (editor == NULL &&
255 (editor = bltinlookup("FCEDIT", 1)) == NULL &&
256 (editor = bltinlookup("EDITOR", 1)) == NULL)
258 if (editor[0] == '-' && editor[1] == '\0') {
259 sflg = 1; /* no edit */
266 * If executing, parse [old=new] now
268 if (lflg == 0 && *argptr != NULL &&
269 ((repl = strchr(*argptr, '=')) != NULL)) {
275 * determine [first] and [last]
277 if (*argptr == NULL) {
278 firststr = lflg ? "-16" : "-1";
280 } else if (argptr[1] == NULL) {
281 firststr = argptr[0];
282 laststr = lflg ? "-1" : argptr[0];
283 } else if (argptr[2] == NULL) {
284 firststr = argptr[0];
287 error("too many arguments");
289 * Turn into event numbers.
291 first = str_to_event(firststr, 0);
292 last = str_to_event(laststr, 1);
300 * XXX - this should not depend on the event numbers
301 * always increasing. Add sequence numbers or offset
302 * to the history element in next (diskbased) release.
304 direction = first < last ? H_PREV : H_NEXT;
307 * If editing, grab a temp file.
312 sprintf(editfilestr, "%s/_shXXXXXX", _PATH_TMP);
313 if ((fd = mkstemp(editfilestr)) < 0)
314 error("can't create temporary file %s", editfile);
315 editfile = editfilestr;
316 if ((efp = fdopen(fd, "w")) == NULL) {
318 error("Out of space");
323 * Loop through selected history events. If listing or executing,
324 * do it now. Otherwise, put into temp file and call the editor
327 * The history interface needs rethinking, as the following
328 * convolutions will demonstrate.
330 history(hist, &he, H_FIRST);
331 retval = history(hist, &he, H_NEXT_EVENT, first);
332 for (;retval != -1; retval = history(hist, &he, direction)) {
335 out1fmt("%5d ", he.num);
339 fc_replace(he.str, pat, repl) :
340 __DECONST(char *, he.str);
348 if (displayhist && hist) {
350 * XXX what about recursive and
354 history(hist, &he, H_ENTER, s);
356 * XXX H_ENTER moves the internal
357 * cursor, set it back to the current
360 retval = history(hist, &he,
361 H_NEXT_EVENT, oldhistnum);
367 * At end? (if we were to lose last, we'd sure be
377 editcmd = stalloc(strlen(editor) + strlen(editfile) + 2);
378 sprintf(editcmd, "%s %s", editor, editfile);
379 evalstring(editcmd, 0); /* XXX - should use no JC command */
381 readcmdfile(editfile); /* XXX - should read back - quick tst */
385 if (lflg == 0 && active > 0)
389 handler = savehandler;
394 fc_replace(const char *s, char *p, char *r)
397 int plen = strlen(p);
401 if (*s == *p && strncmp(s, p, plen) == 0) {
404 *p = '\0'; /* so no more matches */
409 dest = grabstackstr(dest);
415 not_fcnumber(const char *s)
421 return (!is_number(s));
425 str_to_event(const char *str, int last)
432 retval = history(hist, &he, H_FIRST);
443 while (retval != -1 && i--) {
444 retval = history(hist, &he, H_NEXT);
447 retval = history(hist, &he, H_LAST);
449 retval = history(hist, &he, H_NEXT_EVENT, i);
452 * the notion of first and last is
453 * backwards to that of the history package
455 retval = history(hist, &he, last ? H_FIRST : H_LAST);
459 error("history number %s not found (internal error)",
465 retval = history(hist, &he, H_PREV_STR, str);
467 error("history pattern not found: %s", str);
473 bindcmd(int argc, char **argv)
477 error("line editing is disabled");
478 return (el_parse(el, argc, (const char **)argv));
485 histcmd(int argc __unused, char **argv __unused)
488 error("not compiled with history support");
494 bindcmd(int argc __unused, char **argv __unused)
497 error("not compiled with line editing support");