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.4 2004/03/19 18:39:41 cpressey 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).
81 #define editing (Eflag || Vflag)
89 hist = history_init();
93 sethistsize(histsizeval());
95 out2str("sh: can't initialize history\n");
97 if (editing && !el && isatty(0)) { /* && isatty(2) ??? */
103 el_in = fdopen(0, "r");
105 el_out = fdopen(2, "w");
106 if (el_in == NULL || el_out == NULL)
108 el = el_init(arg0, el_in, el_out);
111 el_set(el, EL_HIST, history, hist);
112 el_set(el, EL_PROMPT, getprompt);
115 out2str("sh: can't initialize editing\n");
118 } else if (!editing && el) {
126 el_set(el, EL_EDITOR, "vi");
128 el_set(el, EL_EDITOR, "emacs");
133 if (el) { /* no editing if not interactive */
153 if (hs == NULL || *hs == '\0' ||
154 (histsize = atoi(hs)) < 0)
156 history(hist, H_EVENT, histsize);
161 * This command is provided since POSIX decided to standardize
162 * the Korn shell fc command. Oh well...
165 histcmd(int argc, char **argv)
168 const char *editor = NULL;
170 int lflg = 0, nflg = 0, rflg = 0, sflg = 0;
172 const char *firststr, *laststr;
173 int first, last, direction;
174 char *pat = NULL, *repl; /* ksh "fc old=new" crap */
175 static int active = 0;
176 struct jmploc jmploc;
177 struct jmploc *volatile savehandler;
178 char editfile[PATH_MAX];
181 /* Avoid longjmp clobbering */
197 error("history not active");
200 error("missing history argument");
202 optreset = 1; optind = 1; /* initialize getopt */
204 while (not_fcnumber(argv[optind]) &&
205 (ch = getopt(argc, argv, ":e:lnrs")) != -1)
223 error("option -%c expects argument", optopt);
226 error("unknown option: -%c", optopt);
228 argc -= optind, argv += optind;
233 if (lflg == 0 || editor || sflg) {
234 lflg = 0; /* ignore */
237 * Catch interrupts to reset active counter and
238 * cleanup temp files.
240 if (setjmp(jmploc.loc)) {
244 handler = savehandler;
245 longjmp(handler->loc, 1);
247 savehandler = handler;
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 && argc > 0 &&
273 ((repl = strchr(argv[0], '=')) != NULL)) {
279 * determine [first] and [last]
283 firststr = lflg ? "-16" : "-1";
288 laststr = lflg ? "-1" : argv[0];
295 error("too many args");
298 * Turn into event numbers.
300 first = str_to_event(firststr, 0);
301 last = str_to_event(laststr, 1);
309 * XXX - this should not depend on the event numbers
310 * always increasing. Add sequence numbers or offset
311 * to the history element in next (diskbased) release.
313 direction = first < last ? H_PREV : H_NEXT;
316 * If editing, grab a temp file.
321 sprintf(editfile, "%s/_shXXXXXX", _PATH_TMP);
322 if ((fd = mkstemp(editfile)) < 0)
323 error("can't create temporary file %s", editfile);
324 if ((efp = fdopen(fd, "w")) == NULL) {
326 error("can't allocate stdio buffer for temp");
331 * Loop through selected history events. If listing or executing,
332 * do it now. Otherwise, put into temp file and call the editor
335 * The history interface needs rethinking, as the following
336 * convolutions will demonstrate.
338 history(hist, H_FIRST);
339 he = history(hist, H_NEXT_EVENT, first);
340 for (;he != NULL; he = history(hist, direction)) {
343 out1fmt("%5d ", he->num);
347 fc_replace(he->str, pat, repl) : (char *)he->str;
354 if (displayhist && hist) {
356 * XXX what about recursive and
359 history(hist, H_ENTER, s);
365 * At end? (if we were to loose last, we'd sure be
375 editcmd = stalloc(strlen(editor) + strlen(editfile) + 2);
376 sprintf(editcmd, "%s %s", editor, editfile);
377 evalstring(editcmd); /* XXX - should use no JC command */
379 readcmdfile(editfile); /* XXX - should read back - quick tst */
383 if (lflg == 0 && active > 0)
391 fc_replace(const char *s, char *p, char *r)
394 int plen = strlen(p);
398 if (*s == *p && strncmp(s, p, plen) == 0) {
402 *p = '\0'; /* so no more matches */
407 dest = grabstackstr(dest);
413 not_fcnumber(char *s)
419 return (!is_number(s));
423 str_to_event(const char *str, int last)
430 he = history(hist, H_FIRST);
441 while (he != NULL && i--) {
442 he = history(hist, H_NEXT);
445 he = history(hist, H_LAST);
447 he = history(hist, H_NEXT_EVENT, i);
450 * the notion of first and last is
451 * backwards to that of the history package
453 he = history(hist, last ? H_FIRST : H_LAST);
457 error("history number %s not found (internal error)",
463 he = history(hist, H_PREV_STR, str);
465 error("history pattern not found: %s", str);
471 bindcmd(int argc, char **argv)
475 error("line editing is disabled");
476 return (el_parse(el, argc, argv));
483 histcmd(int argc, char **argv)
486 error("not compiled with history support");
492 bindcmd(int argc, char **argv)
495 error("not compiled with line editing support");