1 /* $NetBSD: histedit.c,v 1.40 2006/10/16 00:36: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
35 #include <sys/cdefs.h>
38 static char sccsid
[] = "@(#)histedit.c 8.2 (Berkeley) 5/4/95";
40 __RCSID("$NetBSD: histedit.c,v 1.40 2006/10/16 00:36:19 christos Exp $");
44 #include <sys/param.h>
50 * Editline and history functions (and glue).
59 #include "myhistedit.h"
65 #define MAXHISTLOOPS 4 /* max recursions through fc */
66 #define DEFEDITOR "ed" /* default editor *should* be $EDITOR */
68 History
*hist
; /* history cookie */
69 EditLine
*el
; /* editline cookie */
71 static FILE *el_in
, *el_out
;
72 unsigned char _el_fn_complete(EditLine
*, int);
74 STATIC
const char *fc_replace(const char *, char *, char *);
77 extern FILE *tracefile
;
81 * Set history and editing status. Called whenever the status may
82 * have changed (figures out what to do).
89 #define editing (Eflag || Vflag)
97 hist
= history_init();
101 sethistsize(histsizeval());
103 out2str("sh: can't initialize history\n");
105 if (editing
&& !el
&& isatty(0)) { /* && isatty(2) ??? */
113 el_in
= fdopen(0, "r");
115 el_out
= fdopen(2, "w");
116 if (el_in
== NULL
|| el_out
== NULL
)
123 term
= lookupvar("TERM");
125 setenv("TERM", term
, 1);
129 if (shname
[0] == '-')
131 el
= el_init(shname
, el_in
, el_out
, el_err
);
134 el_set(el
, EL_HIST
, history
, hist
);
135 el_set(el
, EL_PROMPT
, getprompt
);
136 el_set(el
, EL_SIGNAL
, 1);
137 el_set(el
, EL_ADDFN
, "rl-complete",
138 "ReadLine compatible completion function",
142 out2str("sh: can't initialize editing\n");
145 } else if (!editing
&& el
) {
153 el_set(el
, EL_EDITOR
, "vi");
155 el_set(el
, EL_EDITOR
, "emacs");
156 el_set(el
, EL_BIND
, "^I",
157 tabcomplete
? "rl-complete" : "ed-insert", NULL
);
162 if (el
) { /* no editing if not interactive */
176 sethistsize(const char *hs
)
182 if (hs
== NULL
|| *hs
== '\0' ||
183 (histsize
= atoi(hs
)) < 0)
185 history(hist
, &he
, H_SETSIZE
, histsize
);
186 history(hist
, &he
, H_SETUNIQUE
, 1);
191 setterm(const char *term
)
193 if (el
!= NULL
&& term
!= NULL
)
194 if (el_set(el
, EL_TERMINAL
, term
) != 0) {
195 outfmt(out2
, "sh: Can't set terminal type %s\n", term
);
196 outfmt(out2
, "sh: Using dumb terminal settings.\n");
206 out2str("usage: inputrc file\n");
210 if (el_source(el
, argv
[1])) {
211 out2str("inputrc: failed\n");
216 out2str("sh: inputrc ignored, not editing\n");
222 * This command is provided since POSIX decided to standardize
223 * the Korn shell fc command. Oh well...
226 histcmd(int argc
, char **argv
)
229 const char * volatile editor
= NULL
;
232 volatile int nflg
= 0, rflg
= 0, sflg
= 0;
234 const char *firststr
, *laststr
;
235 int first
, last
, direction
;
236 char *pat
= NULL
, *repl
; /* ksh "fc old=new" crap */
237 static int active
= 0;
238 struct jmploc jmploc
;
239 struct jmploc
*volatile savehandler
;
240 char editfile
[MAXPATHLEN
+ 1];
243 repl
= NULL
; /* XXX gcc4 */
244 efp
= NULL
; /* XXX gcc4 */
248 error("history not active");
251 error("missing history argument");
253 optreset
= 1; optind
= 1; /* initialize getopt */
254 while (not_fcnumber(argv
[optind
]) &&
255 (ch
= getopt(argc
, argv
, ":e:lnrs")) != -1)
273 error("option -%c expects argument", optopt
);
277 error("unknown option: -%c", optopt
);
280 argc
-= optind
, argv
+= optind
;
285 if (lflg
== 0 || editor
|| sflg
) {
286 lflg
= 0; /* ignore */
289 * Catch interrupts to reset active counter and
290 * cleanup temp files.
292 if (setjmp(jmploc
.loc
)) {
296 handler
= savehandler
;
297 longjmp(handler
->loc
, 1);
299 savehandler
= handler
;
301 if (++active
> MAXHISTLOOPS
) {
304 error("called recursively too many times");
310 if (editor
== NULL
&&
311 (editor
= bltinlookup("FCEDIT", 1)) == NULL
&&
312 (editor
= bltinlookup("EDITOR", 1)) == NULL
)
314 if (editor
[0] == '-' && editor
[1] == '\0') {
315 sflg
= 1; /* no edit */
322 * If executing, parse [old=new] now
324 if (lflg
== 0 && argc
> 0 &&
325 ((repl
= strchr(argv
[0], '=')) != NULL
)) {
332 * If -s is specified, accept only one operand
334 if (sflg
&& argc
>= 2)
335 error("too many args");
338 * determine [first] and [last]
342 firststr
= lflg
? "-16" : "-1";
347 laststr
= lflg
? "-1" : argv
[0];
354 error("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("can't create temporary file %s", editfile
);
384 if ((efp
= fdopen(fd
, "w")) == NULL
) {
386 error("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("%5d ", he
.num
);
406 const char *s
= pat
?
407 fc_replace(he
.str
, pat
, repl
) : he
.str
;
414 evalstring(strcpy(stalloc(strlen(s
) + 1), s
), 0);
415 if (displayhist
&& hist
) {
417 * XXX what about recursive and
420 history(hist
, &he
, H_ENTER
, s
);
428 * At end? (if we were to lose last, we'd sure be
438 editcmd
= stalloc(strlen(editor
) + strlen(editfile
) + 2);
439 sprintf(editcmd
, "%s %s", editor
, editfile
);
440 evalstring(editcmd
, 0); /* XXX - should use no JC command */
442 readcmdfile(editfile
); /* XXX - should read back - quick tst */
446 if (lflg
== 0 && active
> 0)
454 fc_replace(const char *s
, char *p
, char *r
)
457 int plen
= strlen(p
);
461 if (*s
== *p
&& strncmp(s
, p
, plen
) == 0) {
465 *p
= '\0'; /* so no more matches */
470 dest
= grabstackstr(dest
);
476 not_fcnumber(char *s
)
482 return (!is_number(s
));
486 str_to_event(const char *str
, int last
)
493 retval
= history(hist
, &he
, H_FIRST
);
504 while (retval
!= -1 && i
--) {
505 retval
= history(hist
, &he
, H_NEXT
);
508 retval
= history(hist
, &he
, H_LAST
);
510 retval
= history(hist
, &he
, H_NEXT_EVENT
, i
);
513 * the notion of first and last is
514 * backwards to that of the history package
516 retval
= history(hist
, &he
,
517 last
? H_FIRST
: H_LAST
);
521 error("history number %s not found (internal error)",
527 retval
= history(hist
, &he
, H_PREV_STR
, str
);
529 error("history pattern not found: %s", str
);
535 histcmd(int argc
, char **argv
)
537 error("not compiled with history support");
541 inputrc(int argc
, char **argv
)
543 error("not compiled with history support");