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: src/bin/sh/histedit.c,v 1.13.2.4 2002/08/27 01:36:28 tjr Exp $
38 * $DragonFly: src/bin/sh/histedit.c,v 1.6 2005/11/13 11:58:30 corecode Exp $
41 #include <sys/param.h>
48 * Editline and history functions (and glue).
58 #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;
71 STATIC char *fc_replace(const char *, char *, char *);
74 * Set history and editing status. Called whenever the status may
75 * have changed (figures out what to do).
82 #define editing (Eflag || Vflag)
90 hist = history_init();
94 sethistsize(histsizeval());
96 out2str("sh: can't initialize history\n");
98 if (editing && !el && isatty(0)) { /* && isatty(2) ??? */
106 el_in = fdopen(0, "r");
108 el_out = fdopen(2, "w");
109 if (el_in == NULL || el_out == NULL)
112 term = lookupvar("TERM");
114 setenv("TERM", term, 1);
118 if (shname[0] == '-')
120 el = el_init(arg0, el_in, el_out, el_err);
123 el_set(el, EL_HIST, history, hist);
124 el_set(el, EL_PROMPT, getprompt);
127 out2str("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");
145 if (el) { /* no editing if not interactive */
159 sethistsize(const char *hs)
165 if (hs == NULL || *hs == '\0' ||
166 (histsize = atoi(hs)) < 0)
168 history(hist, &he, H_SETSIZE, histsize);
173 * This command is provided since POSIX decided to standardize
174 * the Korn shell fc command. Oh well...
177 histcmd(int argc, char **argv)
180 const char *editor = NULL;
182 int lflg = 0, nflg = 0, rflg = 0, sflg = 0;
184 const char *firststr, *laststr;
185 int first, last, direction;
186 char *pat = NULL, *repl; /* ksh "fc old=new" crap */
187 static int active = 0;
188 struct jmploc jmploc;
189 struct jmploc *volatile savehandler;
190 char editfile[PATH_MAX];
193 /* Avoid longjmp clobbering */
209 error("history not active");
212 error("missing history argument");
214 optreset = 1; optind = 1; /* initialize getopt */
216 while (not_fcnumber(argv[optind]) &&
217 (ch = getopt(argc, argv, ":e:lnrs")) != -1)
235 error("option -%c expects argument", optopt);
238 error("unknown option: -%c", optopt);
240 argc -= optind, argv += optind;
245 if (lflg == 0 || editor || sflg) {
246 lflg = 0; /* ignore */
249 * Catch interrupts to reset active counter and
250 * cleanup temp files.
252 if (setjmp(jmploc.loc)) {
256 handler = savehandler;
257 longjmp(handler->loc, 1);
259 savehandler = handler;
261 if (++active > MAXHISTLOOPS) {
264 error("called recursively too many times");
270 if (editor == NULL &&
271 (editor = bltinlookup("FCEDIT", 1)) == NULL &&
272 (editor = bltinlookup("EDITOR", 1)) == NULL)
274 if (editor[0] == '-' && editor[1] == '\0') {
275 sflg = 1; /* no edit */
282 * If executing, parse [old=new] now
284 if (lflg == 0 && argc > 0 &&
285 ((repl = strchr(argv[0], '=')) != NULL)) {
291 * determine [first] and [last]
295 firststr = lflg ? "-16" : "-1";
300 laststr = lflg ? "-1" : argv[0];
307 error("too many args");
310 * Turn into event numbers.
312 first = str_to_event(firststr, 0);
313 last = str_to_event(laststr, 1);
321 * XXX - this should not depend on the event numbers
322 * always increasing. Add sequence numbers or offset
323 * to the history element in next (diskbased) release.
325 direction = first < last ? H_PREV : H_NEXT;
328 * If editing, grab a temp file.
333 sprintf(editfile, "%s/_shXXXXXX", _PATH_TMP);
334 if ((fd = mkstemp(editfile)) < 0)
335 error("can't create temporary file %s", editfile);
336 if ((efp = fdopen(fd, "w")) == NULL) {
338 error("can't allocate stdio buffer for temp");
343 * Loop through selected history events. If listing or executing,
344 * do it now. Otherwise, put into temp file and call the editor
347 * The history interface needs rethinking, as the following
348 * convolutions will demonstrate.
350 history(hist, &he, H_FIRST);
351 retval = history(hist, &he, H_NEXT_EVENT, first);
352 for (;retval != -1; retval = history(hist, &he, direction)) {
355 out1fmt("%5d ", he.num);
359 fc_replace(he.str, pat, repl) : (char *)he.str;
366 if (displayhist && hist) {
368 * XXX what about recursive and
371 history(hist, &he, H_ENTER, s);
377 * At end? (if we were to loose last, we'd sure be
387 editcmd = stalloc(strlen(editor) + strlen(editfile) + 2);
388 sprintf(editcmd, "%s %s", editor, editfile);
389 evalstring(editcmd); /* XXX - should use no JC command */
391 readcmdfile(editfile); /* XXX - should read back - quick tst */
395 if (lflg == 0 && active > 0)
403 fc_replace(const char *s, char *p, char *r)
406 int plen = strlen(p);
410 if (*s == *p && strncmp(s, p, plen) == 0) {
414 *p = '\0'; /* so no more matches */
419 dest = grabstackstr(dest);
425 not_fcnumber(char *s)
431 return (!is_number(s));
435 str_to_event(const char *str, int last)
442 history(hist, &he, H_FIRST);
453 while (retval != -1 && i--) {
454 retval = history(hist, &he, H_NEXT);
457 retval = history(hist, &he, H_LAST);
459 retval = history(hist, &he, H_NEXT_EVENT, i);
462 * the notion of first and last is
463 * backwards to that of the history package
465 retval = history(hist, &he, last ? H_FIRST : H_LAST);
469 error("history number %s not found (internal error)",
475 retval = history(hist, &he, H_PREV_STR, str);
477 error("history pattern not found: %s", str);
483 bindcmd(int argc, char **argv)
487 error("line editing is disabled");
488 return (el_parse(el, argc, (const char **)argv));
495 histcmd(int argc, char **argv)
498 error("not compiled with history support");
504 bindcmd(int argc, char **argv)
507 error("not compiled with line editing support");