3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1997-2005
5 * Herbert Xu <herbert@gondor.apana.org.au>. 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/param.h>
44 * Editline and history functions (and glue).
55 #include "myhistedit.h"
59 #define MAXHISTLOOPS 4 /* max recursions through fc */
60 #define DEFEDITOR "ed" /* default editor *should* be $EDITOR */
62 History
*hist
; /* history cookie */
63 EditLine
*el
; /* editline cookie */
65 static FILE *el_in
, *el_out
;
67 STATIC
const char *fc_replace(const char *, char *, char *);
70 extern FILE *tracefile
;
74 * Set history and editing status. Called whenever the status may
75 * have changed (figures out what to do).
82 #define editing (Eflag || Vflag)
90 hist
= history_init();
94 sethistsize(histsizeval());
96 out2str("sh: can't initialize history\n");
98 if (editing
&& !el
&& isatty(0)) { /* && isatty(2) ??? */
104 el_in
= fdopen(0, "r");
106 el_out
= fdopen(2, "w");
107 if (el_in
== NULL
|| el_out
== NULL
)
114 el
= el_init(arg0
, el_in
, el_out
, el_err
);
117 el_set(el
, EL_HIST
, history
, hist
);
118 el_set(el
, EL_PROMPT
, getprompt
);
121 out2str("sh: can't initialize editing\n");
124 } else if (!editing
&& el
) {
132 el_set(el
, EL_EDITOR
, "vi");
134 el_set(el
, EL_EDITOR
, "emacs");
139 if (el
) { /* no editing if not interactive */
153 sethistsize(const char *hs
)
159 if (hs
== NULL
|| *hs
== '\0' ||
160 (histsize
= atoi(hs
)) < 0)
162 history(hist
, &he
, H_SETSIZE
, histsize
);
167 setterm(const char *term
)
169 if (el
!= NULL
&& term
!= NULL
)
170 if (el_set(el
, EL_TERMINAL
, term
) != 0) {
171 outfmt(out2
, "sh: Can't set terminal type %s\n", term
);
172 outfmt(out2
, "sh: Using dumb terminal settings.\n");
177 * This command is provided since POSIX decided to standardize
178 * the Korn shell fc command. Oh well...
181 histcmd(int argc
, char **argv
)
184 const char *editor
= NULL
;
186 int lflg
= 0, nflg
= 0, rflg
= 0, sflg
= 0;
188 const char *firststr
, *laststr
;
189 int first
, last
, direction
;
190 char *pat
= NULL
, *repl
; /* ksh "fc old=new" crap */
191 static int active
= 0;
192 struct jmploc jmploc
;
193 struct jmploc
*volatile savehandler
;
194 char editfile
[MAXPATHLEN
+ 1];
197 /* Avoid longjmp clobbering */
213 sh_error("history not active");
216 sh_error("missing history argument");
221 optreset
= 1; optind
= 1; /* initialize getopt */
223 while (not_fcnumber(argv
[optind
]) &&
224 (ch
= getopt(argc
, argv
, ":e:lnrs")) != -1)
242 sh_error("option -%c expects argument", optopt
);
246 sh_error("unknown option: -%c", optopt
);
249 argc
-= optind
, argv
+= optind
;
254 if (lflg
== 0 || editor
|| sflg
) {
255 lflg
= 0; /* ignore */
258 * Catch interrupts to reset active counter and
259 * cleanup temp files.
261 if (setjmp(jmploc
.loc
)) {
265 handler
= savehandler
;
266 longjmp(handler
->loc
, 1);
268 savehandler
= handler
;
270 if (++active
> MAXHISTLOOPS
) {
273 sh_error("called recursively too many times");
279 if (editor
== NULL
&&
280 (editor
= bltinlookup("FCEDIT")) == NULL
&&
281 (editor
= bltinlookup("EDITOR")) == NULL
)
283 if (editor
[0] == '-' && editor
[1] == '\0') {
284 sflg
= 1; /* no edit */
291 * If executing, parse [old=new] now
293 if (lflg
== 0 && argc
> 0 &&
294 ((repl
= strchr(argv
[0], '=')) != NULL
)) {
300 * determine [first] and [last]
304 firststr
= lflg
? "-16" : "-1";
309 laststr
= lflg
? "-1" : argv
[0];
316 sh_error("too many args");
320 * Turn into event numbers.
322 first
= str_to_event(firststr
, 0);
323 last
= str_to_event(laststr
, 1);
331 * XXX - this should not depend on the event numbers
332 * always increasing. Add sequence numbers or offset
333 * to the history element in next (diskbased) release.
335 direction
= first
< last
? H_PREV
: H_NEXT
;
338 * If editing, grab a temp file.
343 sprintf(editfile
, "%s_shXXXXXX", _PATH_TMP
);
344 if ((fd
= mkstemp(editfile
)) < 0)
345 sh_error("can't create temporary file %s", editfile
);
346 if ((efp
= fdopen(fd
, "w")) == NULL
) {
348 sh_error("can't allocate stdio buffer for temp");
353 * Loop through selected history events. If listing or executing,
354 * do it now. Otherwise, put into temp file and call the editor
357 * The history interface needs rethinking, as the following
358 * convolutions will demonstrate.
360 history(hist
, &he
, H_FIRST
);
361 retval
= history(hist
, &he
, H_NEXT_EVENT
, first
);
362 for (;retval
!= -1; retval
= history(hist
, &he
, direction
)) {
365 out1fmt("%5d ", he
.num
);
368 const char *s
= pat
?
369 fc_replace(he
.str
, pat
, repl
) : he
.str
;
377 if (displayhist
&& hist
) {
379 * XXX what about recursive and
382 history(hist
, &he
, H_ENTER
, s
);
388 * At end? (if we were to lose last, we'd sure be
398 editcmd
= stalloc(strlen(editor
) + strlen(editfile
) + 2);
399 sprintf(editcmd
, "%s %s", editor
, editfile
);
400 /* XXX - should use no JC command */
401 evalstring(editcmd
, 0);
403 readcmdfile(editfile
); /* XXX - should read back - quick tst */
407 if (lflg
== 0 && active
> 0)
415 fc_replace(const char *s
, char *p
, char *r
)
418 int plen
= strlen(p
);
422 if (*s
== *p
&& strncmp(s
, p
, plen
) == 0) {
426 *p
= '\0'; /* so no more matches */
431 dest
= grabstackstr(dest
);
437 not_fcnumber(char *s
)
443 return (!is_number(s
));
447 str_to_event(const char *str
, int last
)
454 retval
= history(hist
, &he
, H_FIRST
);
465 while (retval
!= -1 && i
--) {
466 retval
= history(hist
, &he
, H_NEXT
);
469 retval
= history(hist
, &he
, H_LAST
);
471 retval
= history(hist
, &he
, H_NEXT_EVENT
, i
);
474 * the notion of first and last is
475 * backwards to that of the history package
477 retval
= history(hist
, &he
,
478 last
? H_FIRST
: H_LAST
);
482 sh_error("history number %s not found (internal error)",
488 retval
= history(hist
, &he
, H_PREV_STR
, str
);
490 sh_error("history pattern not found: %s", str
);