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 * 4. 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
35 static char sccsid
[] = "@(#)histedit.c 8.2 (Berkeley) 5/4/95";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
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) ??? */
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 el
= el_init(arg0
, el_in
, el_out
, el_err
);
113 el_set(el
, EL_HIST
, history
, hist
);
114 el_set(el
, EL_PROMPT
, getprompt
);
117 out2str("sh: can't initialize editing\n");
120 } else if (!editing
&& el
) {
128 el_set(el
, EL_EDITOR
, "vi");
130 el_set(el
, EL_EDITOR
, "emacs");
135 if (el
) { /* no editing if not interactive */
156 if (hs
== NULL
|| *hs
== '\0' ||
157 (histsize
= atoi(hs
)) < 0)
159 history(hist
, &he
, H_SETSIZE
, histsize
);
164 histcmd(int argc
, char **argv
)
169 int lflg
= 0, nflg
= 0, rflg
= 0, sflg
= 0;
171 char *firststr
, *laststr
;
172 int first
, last
, direction
;
173 char *pat
= NULL
, *repl
;
174 static int active
= 0;
175 struct jmploc jmploc
;
176 struct jmploc
*volatile savehandler
;
177 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
, &he
, H_FIRST
);
339 retval
= history(hist
, &he
, H_NEXT_EVENT
, first
);
340 for (;retval
!= -1; retval
= history(hist
, &he
, 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
360 history(hist
, &he
, H_ENTER
, s
);
362 * XXX H_ENTER moves the internal
363 * cursor, set it back to the current
366 retval
= history(hist
, &he
,
367 H_NEXT_EVENT
, oldhistnum
);
373 * At end? (if we were to lose last, we'd sure be
383 editcmd
= stalloc(strlen(editor
) + strlen(editfile
) + 2);
384 sprintf(editcmd
, "%s %s", editor
, editfile
);
385 evalstring(editcmd
); /* XXX - should use no JC command */
387 readcmdfile(editfile
); /* XXX - should read back - quick tst */
391 if (lflg
== 0 && active
> 0)
399 fc_replace(const char *s
, char *p
, char *r
)
402 int plen
= strlen(p
);
406 if (*s
== *p
&& strncmp(s
, p
, plen
) == 0) {
410 *p
= '\0'; /* so no more matches */
415 dest
= grabstackstr(dest
);
421 not_fcnumber(char *s
)
427 return (!is_number(s
));
431 str_to_event(char *str
, int last
)
438 retval
= history(hist
, &he
, H_FIRST
);
449 while (retval
!= -1 && i
--) {
450 retval
= history(hist
, &he
, H_NEXT
);
453 retval
= history(hist
, &he
, H_LAST
);
455 retval
= history(hist
, &he
, H_NEXT_EVENT
, i
);
458 * the notion of first and last is
459 * backwards to that of the history package
461 retval
= history(hist
, &he
, last
? H_FIRST
: H_LAST
);
465 error("history number %s not found (internal error)",
471 retval
= history(hist
, &he
, H_PREV_STR
, str
);
473 error("history pattern not found: %s", str
);
479 bindcmd(int argc
, char **argv
)
483 error("line editing is disabled");
484 return (el_parse(el
, argc
, (const char **)argv
));
491 histcmd(int argc
, char **argv
)
494 error("not compiled with history support");
500 bindcmd(int argc
, char **argv
)
503 error("not compiled with line editing support");