1 /* $NetBSD: histedit.c,v 1.36 2005/05/09 11:35:19 christos Exp $ */
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 static char sccsid
[] = "@(#)histedit.c 8.2 (Berkeley) 5/4/95";
39 __RCSID("$NetBSD: histedit.c,v 1.36 2005/05/09 11:35:19 christos Exp $");
47 * Editline and history functions (and glue).
56 #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
;
70 unsigned char _el_fn_complete(EditLine
*, int);
72 STATIC
const char *fc_replace(const char *, char *, char *);
75 extern FILE *tracefile
;
79 * Set history and editing status. Called whenever the status may
80 * have changed (figures out what to do).
87 #define editing (Eflag(psh) || Vflag(psh))
95 hist
= history_init();
99 sethistsize(histsizeval());
101 out2str(psh
, "sh: can't initialize history\n");
103 if (editing
&& !el
&& isatty(0)) { /* && isatty(2) ??? */
111 el_in
= fdopen(0, "r");
113 el_out
= fdopen(2, "w");
114 if (el_in
== NULL
|| el_out
== NULL
)
121 term
= lookupvar(psh
, "TERM");
123 setenv("TERM", term
, 1);
127 if (shname
[0] == '-')
129 el
= el_init(shname
, el_in
, el_out
, el_err
);
132 el_set(el
, EL_HIST
, history
, hist
);
133 el_set(el
, EL_PROMPT
, getprompt
);
134 el_set(el
, EL_SIGNAL
, 1);
135 el_set(el
, EL_ADDFN
, "rl-complete",
136 "ReadLine compatible completion function",
140 out2str(psh
, "sh: can't initialize editing\n");
143 } else if (!editing
&& el
) {
151 el_set(el
, EL_EDITOR
, "vi");
153 el_set(el
, EL_EDITOR
, "emacs");
154 el_set(el
, EL_BIND
, "^I",
155 tabcomplete(psh
) ? "rl-complete" : "ed-insert", NULL
);
160 if (el
) { /* no editing if not interactive */
174 sethistsize(shinstance
*psh
, const char *hs
)
180 if (hs
== NULL
|| *hs
== '\0' ||
181 (histsize
= atoi(hs
)) < 0)
183 history(hist
, &he
, H_SETSIZE
, histsize
);
188 setterm(shinstance
*psh
, const char *term
)
190 if (el
!= NULL
&& term
!= NULL
)
191 if (el_set(el
, EL_TERMINAL
, term
) != 0) {
192 outfmt(psh
->out2
, "sh: Can't set terminal type %s\n", term
);
193 outfmt(psh
->out2
, "sh: Using dumb terminal settings.\n");
203 out2str(psh
, "usage: inputrc file\n");
207 if (el_source(el
, argv
[1])) {
208 out2str(psh
, "inputrc: failed\n");
213 out2str(psh
, "sh: inputrc ignored, not editing\n");
219 * This command is provided since POSIX decided to standardize
220 * the Korn shell fc command. Oh well...
223 histcmd(int argc
, char **argv
)
226 const char *editor
= NULL
;
228 int lflg
= 0, nflg
= 0, rflg
= 0, sflg
= 0;
230 const char *firststr
, *laststr
;
231 int first
, last
, direction
;
232 char *pat
= NULL
, *repl
; /* ksh "fc old=new" crap */
233 static int active
= 0;
234 struct jmploc jmploc
;
235 struct jmploc
*volatile savehandler
;
236 char editfile
[MAXPATHLEN
+ 1];
239 /* Avoid longjmp clobbering */
255 error(psh
, "history not active");
258 error(psh
, "missing history argument");
260 optreset
= 1; optind
= 1; /* initialize getopt */
261 while (not_fcnumber(argv
[optind
]) &&
262 (ch
= getopt(argc
, argv
, ":e:lnrs")) != -1)
280 error(psh
, "option -%c expects argument", optopt
);
284 error(psh
, "unknown option: -%c", optopt
);
287 argc
-= optind
, argv
+= optind
;
292 if (lflg
== 0 || editor
|| sflg
) {
293 lflg
= 0; /* ignore */
296 * Catch interrupts to reset active counter and
297 * cleanup temp files.
299 if (setjmp(jmploc
.loc
)) {
303 handler
= savehandler
;
304 longjmp(handler
->loc
, 1);
306 savehandler
= handler
;
308 if (++active
> MAXHISTLOOPS
) {
311 error(psh
, "called recursively too many times");
317 if (editor
== NULL
&&
318 (editor
= bltinlookup(psh
, "FCEDIT", 1)) == NULL
&&
319 (editor
= bltinlookup(psh
, "EDITOR", 1)) == NULL
)
321 if (editor
[0] == '-' && editor
[1] == '\0') {
322 sflg
= 1; /* no edit */
329 * If executing, parse [old=new] now
331 if (lflg
== 0 && argc
> 0 &&
332 ((repl
= strchr(argv
[0], '=')) != NULL
)) {
338 * determine [first] and [last]
342 firststr
= lflg
? "-16" : "-1";
347 laststr
= lflg
? "-1" : argv
[0];
354 error(psh
, "too many args");
358 * Turn into event numbers.
360 first
= str_to_event(firststr
, 0);
361 last
= str_to_event(laststr
, 1);
369 * XXX - this should not depend on the event numbers
370 * always increasing. Add sequence numbers or offset
371 * to the history element in next (diskbased) release.
373 direction
= first
< last
? H_PREV
: H_NEXT
;
376 * If editing, grab a temp file.
381 snprintf(editfile
, sizeof(editfile
), "%s_shXXXXXX", _PATH_TMP
);
382 if ((fd
= mkstemp(editfile
)) < 0)
383 error(psh
, "can't create temporary file %s", editfile
);
384 if ((efp
= fdopen(fd
, "w")) == NULL
) {
385 shfile_close(&psh
->fdtab
, fd
);
386 error(psh
, "can't allocate stdio buffer for temp");
391 * Loop through selected history events. If listing or executing,
392 * do it now. Otherwise, put into temp file and call the editor
395 * The history interface needs rethinking, as the following
396 * convolutions will demonstrate.
398 history(hist
, &he
, H_FIRST
);
399 retval
= history(hist
, &he
, H_NEXT_EVENT
, first
);
400 for (;retval
!= -1; retval
= history(hist
, &he
, direction
)) {
403 out1fmt(psh
, "%5d ", he
.num
);
404 out1str(psh
, he
.str
);
406 const char *s
= pat
?
407 fc_replace(he
.str
, pat
, repl
) : he
.str
;
414 evalstring(psh
, strcpy(stalloc(psh
, strlen(s
) + 1), s
), 0);
415 if (displayhist
&& hist
) {
417 * XXX what about recursive and
420 history(hist
, &he
, H_ENTER
, s
);
426 * At end? (if we were to lose last, we'd sure be
436 editcmd
= stalloc(psh
, strlen(editor
) + strlen(editfile
) + 2);
437 sprintf(editcmd
, "%s %s", editor
, editfile
);
438 evalstring(psh
, editcmd
, 0); /* XXX - should use no JC command */
440 readcmdfile(psh
, editfile
); /* XXX - should read back - quick tst */
444 if (lflg
== 0 && active
> 0)
452 fc_replace(const char *s
, char *p
, char *r
)
455 int plen
= strlen(p
);
457 STARTSTACKSTR(psh
, dest
);
459 if (*s
== *p
&& strncmp(s
, p
, plen
) == 0) {
461 STPUTC(psh
, *r
++, dest
);
463 *p
= '\0'; /* so no more matches */
465 STPUTC(psh
, *s
++, dest
);
467 STACKSTRNUL(psh
, dest
);
468 dest
= grabstackstr(psh
, dest
);
474 not_fcnumber(char *s
)
480 return (!is_number(s
));
484 str_to_event(const char *str
, int last
)
491 retval
= history(hist
, &he
, H_FIRST
);
502 while (retval
!= -1 && i
--) {
503 retval
= history(hist
, &he
, H_NEXT
);
506 retval
= history(hist
, &he
, H_LAST
);
508 retval
= history(hist
, &he
, H_NEXT_EVENT
, i
);
511 * the notion of first and last is
512 * backwards to that of the history package
514 retval
= history(hist
, &he
,
515 last
? H_FIRST
: H_LAST
);
519 error(psh
, "history number %s not found (internal error)",
525 retval
= history(hist
, &he
, H_PREV_STR
, str
);
527 error(psh
, "history pattern not found: %s", str
);
533 histcmd(shinstance
*psh
, int argc
, char **argv
)
535 error(psh
, "not compiled with history support");
540 inputrc(shinstance
*psh
, int argc
, char **argv
)
542 error(psh
, "not compiled with history support");