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.29 2006/08/04 07:56:31 yar Exp $
38 * $DragonFly: src/bin/sh/histedit.c,v 1.11 2007/01/14 18:29:58 pavalos 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
, *el_err
;
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) ??? */
105 el_in
= fdopen(0, "r");
107 el_err
= fdopen(1, "w");
109 el_out
= fdopen(2, "w");
110 if (el_in
== NULL
|| el_err
== NULL
|| el_out
== NULL
)
112 term
= lookupvar("TERM");
114 if (setenv("TERM", term
, 1) == -1)
115 error("setenv: cannot set TERM=1");
120 if (shname
[0] == '-')
122 el
= el_init(arg0
, el_in
, el_out
, el_err
);
125 el_set(el
, EL_HIST
, history
, hist
);
126 el_set(el
, EL_PROMPT
, getprompt
);
129 out2str("sh: can't initialize editing\n");
132 } else if (!editing
&& el
) {
140 el_set(el
, EL_EDITOR
, "vi");
142 el_set(el
, EL_EDITOR
, "emacs");
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
);
175 histcmd(int argc
, char **argv
)
178 const char *volatile editor
= NULL
;
180 volatile int lflg
= 0, nflg
= 0, rflg
= 0, sflg
= 0;
182 const char *firststr
= NULL
, *laststr
= NULL
;
183 int first
, last
, direction
;
184 char *pat
= NULL
, *repl
= NULL
;
185 static int active
= 0;
186 struct jmploc jmploc
;
187 struct jmploc
*volatile savehandler
;
188 char editfile
[PATH_MAX
];
193 error("history not active");
196 error("missing history argument");
198 optreset
= 1; optind
= 1; /* initialize getopt */
200 while (not_fcnumber(argv
[optind
]) &&
201 (ch
= getopt(argc
, argv
, ":e:lnrs")) != -1)
219 error("option -%c expects argument", optopt
);
222 error("unknown option: -%c", optopt
);
224 argc
-= optind
, argv
+= optind
;
229 if (lflg
== 0 || editor
|| sflg
) {
230 lflg
= 0; /* ignore */
233 * Catch interrupts to reset active counter and
234 * cleanup temp files.
236 if (setjmp(jmploc
.loc
)) {
240 handler
= savehandler
;
241 longjmp(handler
->loc
, 1);
243 savehandler
= handler
;
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 && argc
> 0 &&
269 ((repl
= strchr(argv
[0], '=')) != NULL
)) {
275 * determine [first] and [last]
279 firststr
= lflg
? "-16" : "-1";
284 laststr
= lflg
? "-1" : argv
[0];
291 error("too many args");
294 * Turn into event numbers.
296 first
= str_to_event(firststr
, 0);
297 last
= str_to_event(laststr
, 1);
305 * XXX - this should not depend on the event numbers
306 * always increasing. Add sequence numbers or offset
307 * to the history element in next (diskbased) release.
309 direction
= first
< last
? H_PREV
: H_NEXT
;
312 * If editing, grab a temp file.
317 sprintf(editfile
, "%s/_shXXXXXX", _PATH_TMP
);
318 if ((fd
= mkstemp(editfile
)) < 0)
319 error("can't create temporary file %s", editfile
);
320 if ((efp
= fdopen(fd
, "w")) == NULL
) {
322 error("can't allocate stdio buffer for temp");
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
);
342 const char *s
= pat
?
343 fc_replace(he
.str
, pat
, repl
) : he
.str
;
349 evalstring(strcpy(stalloc(strlen(s
) + 1), s
));
350 if (displayhist
&& hist
) {
352 * XXX what about recursive and
356 history(hist
, &he
, H_ENTER
, s
);
358 * XXX H_ENTER moves the internal
359 * cursor, set it back to the current
362 retval
= history(hist
, &he
,
363 H_NEXT_EVENT
, oldhistnum
);
369 * At end? (if we were to lose last, we'd sure be
379 editcmd
= stalloc(strlen(editor
) + strlen(editfile
) + 2);
380 sprintf(editcmd
, "%s %s", editor
, editfile
);
381 evalstring(editcmd
); /* XXX - should use no JC command */
383 readcmdfile(editfile
); /* XXX - should read back - quick tst */
387 if (lflg
== 0 && active
> 0)
395 fc_replace(const char *s
, char *p
, char *r
)
398 int plen
= strlen(p
);
402 if (*s
== *p
&& strncmp(s
, p
, plen
) == 0) {
406 *p
= '\0'; /* so no more matches */
411 dest
= grabstackstr(dest
);
417 not_fcnumber(char *s
)
423 return (!is_number(s
));
427 str_to_event(const char *str
, int last
)
434 retval
= history(hist
, &he
, H_FIRST
);
445 while (retval
!= -1 && i
--) {
446 retval
= history(hist
, &he
, H_NEXT
);
449 retval
= history(hist
, &he
, H_LAST
);
451 retval
= history(hist
, &he
, H_NEXT_EVENT
, i
);
454 * the notion of first and last is
455 * backwards to that of the history package
457 retval
= history(hist
, &he
, last
? H_FIRST
: H_LAST
);
461 error("history number %s not found (internal error)",
467 retval
= history(hist
, &he
, H_PREV_STR
, str
);
469 error("history pattern not found: %s", str
);
475 bindcmd(int argc
, char **argv
)
479 error("line editing is disabled");
480 return (el_parse(el
, argc
, (const char **)argv
));
487 histcmd(int argc
, char **argv
)
490 error("not compiled with history support");
496 bindcmd(int argc
, char **argv
)
499 error("not compiled with line editing support");