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";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/bin/sh/histedit.c,v 1.26 2004/04/06 20:06:51 markm Exp $");
51 * Editline and history functions (and glue).
61 #if !defined(NO_HISTORY)
62 #include "myhistedit.h"
68 #define MAXHISTLOOPS 4 /* max recursions through fc */
69 #define DEFEDITOR "ed" /* default editor *should* be $EDITOR */
71 History
*hist
; /* history cookie */
72 EditLine
*el
; /* editline cookie */
74 static FILE *el_in
, *el_out
, *el_err
;
76 STATIC
char *fc_replace(const char *, char *, char *);
77 STATIC
int not_fcnumber(char *);
78 STATIC
int str_to_event(char *, int);
81 * Set history and editing status. Called whenever the status may
82 * have changed (figures out what to do).
88 #define editing (Eflag || Vflag)
96 hist
= history_init();
100 sethistsize(histsizeval());
102 out2str("sh: can't initialize history\n");
104 if (editing
&& !el
&& isatty(0)) { /* && isatty(2) ??? */
110 el_in
= fdopen(0, "r");
112 el_err
= fdopen(1, "w");
114 el_out
= fdopen(2, "w");
115 if (el_in
== NULL
|| el_err
== NULL
|| el_out
== NULL
)
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
);
124 out2str("sh: can't initialize editing\n");
127 } else if (!editing
&& el
) {
135 el_set(el
, EL_EDITOR
, "vi");
137 el_set(el
, EL_EDITOR
, "emacs");
138 el_set(el
, EL_ADDFN
, "ed-do-complete",
139 "Complete Argument", complete
);
140 el_set(el
, EL_ADDFN
, "ed-list-complete",
141 "List Argument Completions", complete_list
);
142 el_set(el
, EL_ADDFN
, "ed-maybe-complete",
143 "Complete Argument Or List Completions",
145 el_set(el
, EL_ADDFN
, "ed-expand",
146 "Expand Completions", complete_expand
);
147 el_set(el
, EL_BIND
, "^I", "ed-maybe-complete", NULL
);
148 el_set(el
, EL_BIND
, "-a", "=", "ed-list-complete",
150 el_set(el
, EL_BIND
, "-a", "\\\\", "ed-do-complete",
152 el_set(el
, EL_BIND
, "-a", "*", "ed-expand",
158 if (el
) { /* no editing if not interactive */
179 if (hs
== NULL
|| *hs
== '\0' ||
180 (histsize
= atoi(hs
)) < 0)
182 history(hist
, &he
, H_SETSIZE
, histsize
);
187 histcmd(int argc
, char **argv
)
192 int lflg
= 0, nflg
= 0, rflg
= 0, sflg
= 0;
194 char *firststr
, *laststr
;
195 int first
, last
, direction
;
196 char *pat
= NULL
, *repl
;
197 static int active
= 0;
198 struct jmploc jmploc
;
199 struct jmploc
*volatile savehandler
;
200 char editfile
[PATH_MAX
];
204 /* Avoid longjmp clobbering */
220 error("history not active");
223 error("missing history argument");
225 optreset
= 1; optind
= 1; /* initialize getopt */
227 while (not_fcnumber(argv
[optind
]) &&
228 (ch
= getopt(argc
, argv
, ":e:lnrs")) != -1)
246 error("option -%c expects argument", optopt
);
249 error("unknown option: -%c", optopt
);
251 argc
-= optind
, argv
+= optind
;
256 if (lflg
== 0 || editor
|| sflg
) {
257 lflg
= 0; /* ignore */
260 * Catch interrupts to reset active counter and
261 * cleanup temp files.
263 if (setjmp(jmploc
.loc
)) {
267 handler
= savehandler
;
268 longjmp(handler
->loc
, 1);
270 savehandler
= handler
;
272 if (++active
> MAXHISTLOOPS
) {
275 error("called recursively too many times");
281 if (editor
== NULL
&&
282 (editor
= bltinlookup("FCEDIT", 1)) == NULL
&&
283 (editor
= bltinlookup("EDITOR", 1)) == NULL
)
285 if (editor
[0] == '-' && editor
[1] == '\0') {
286 sflg
= 1; /* no edit */
293 * If executing, parse [old=new] now
295 if (lflg
== 0 && argc
> 0 &&
296 ((repl
= strchr(argv
[0], '=')) != NULL
)) {
302 * determine [first] and [last]
306 firststr
= lflg
? "-16" : "-1";
311 laststr
= lflg
? "-1" : argv
[0];
318 error("too many args");
321 * Turn into event numbers.
323 first
= str_to_event(firststr
, 0);
324 last
= str_to_event(laststr
, 1);
332 * XXX - this should not depend on the event numbers
333 * always increasing. Add sequence numbers or offset
334 * to the history element in next (diskbased) release.
336 direction
= first
< last
? H_PREV
: H_NEXT
;
339 * If editing, grab a temp file.
344 sprintf(editfile
, "%s/_shXXXXXX", _PATH_TMP
);
345 if ((fd
= mkstemp(editfile
)) < 0)
346 error("can't create temporary file %s", editfile
);
347 if ((efp
= fdopen(fd
, "w")) == NULL
) {
349 error("can't allocate stdio buffer for temp");
354 * Loop through selected history events. If listing or executing,
355 * do it now. Otherwise, put into temp file and call the editor
358 * The history interface needs rethinking, as the following
359 * convolutions will demonstrate.
361 history(hist
, &he
, H_FIRST
);
362 retval
= history(hist
, &he
, H_NEXT_EVENT
, first
);
363 for (;retval
!= -1; retval
= history(hist
, &he
, direction
)) {
366 out1fmt("%5d ", he
.num
);
370 fc_replace(he
.str
, pat
, repl
) : (char *)he
.str
;
377 if (displayhist
&& hist
) {
379 * XXX what about recursive and
383 history(hist
, &he
, H_ENTER
, s
);
385 * XXX H_ENTER moves the internal
386 * cursor, set it back to the current
389 retval
= history(hist
, &he
,
390 H_NEXT_EVENT
, oldhistnum
);
396 * At end? (if we were to loose last, we'd sure be
406 editcmd
= stalloc(strlen(editor
) + strlen(editfile
) + 2);
407 sprintf(editcmd
, "%s %s", editor
, editfile
);
408 evalstring(editcmd
); /* XXX - should use no JC command */
410 readcmdfile(editfile
); /* XXX - should read back - quick tst */
414 if (lflg
== 0 && active
> 0)
422 fc_replace(const char *s
, char *p
, char *r
)
425 int plen
= strlen(p
);
429 if (*s
== *p
&& strncmp(s
, p
, plen
) == 0) {
433 *p
= '\0'; /* so no more matches */
438 dest
= grabstackstr(dest
);
444 not_fcnumber(char *s
)
450 return (!is_number(s
));
454 str_to_event(char *str
, int last
)
461 retval
= history(hist
, &he
, H_FIRST
);
472 while (retval
!= -1 && i
--) {
473 retval
= history(hist
, &he
, H_NEXT
);
476 retval
= history(hist
, &he
, H_LAST
);
478 retval
= history(hist
, &he
, H_NEXT_EVENT
, i
);
481 * the notion of first and last is
482 * backwards to that of the history package
484 retval
= history(hist
, &he
, last
? H_FIRST
: H_LAST
);
488 error("history number %s not found (internal error)",
494 retval
= history(hist
, &he
, H_PREV_STR
, str
);
496 error("history pattern not found: %s", str
);
502 bindcmd(int argc
, char **argv
)
506 error("line editing is disabled");
507 return (el_parse(el
, argc
, argv
));
514 histcmd(int argc
, char **argv
)
517 error("not compiled with history support");
523 bindcmd(int argc
, char **argv
)
526 error("not compiled with line editing support");
529 #endif /* !NO_HISTORY */
532 * $PchId: histedit.c,v 1.6 2006/04/10 14:52:58 philip Exp $