kbuild-0.1.5.ebuild: 0.1.4 -> 0.1.5.
[kbuild-mirror.git] / src / kash / histedit.c
blobdc02e55a24b3886f34402702119913856501d8e9
1 /* $NetBSD: histedit.c,v 1.36 2005/05/09 11:35:19 christos Exp $ */
3 /*-
4 * Copyright (c) 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Kenneth Almquist.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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
32 * SUCH DAMAGE.
35 #if 0
36 #ifndef lint
37 static char sccsid[] = "@(#)histedit.c 8.2 (Berkeley) 5/4/95";
38 #else
39 __RCSID("$NetBSD: histedit.c,v 1.36 2005/05/09 11:35:19 christos Exp $");
40 #endif /* not lint */
41 #endif
43 #include <stdio.h>
44 #include <stdlib.h>
47 * Editline and history functions (and glue).
49 #include "shell.h"
50 #include "parser.h"
51 #include "var.h"
52 #include "options.h"
53 #include "main.h"
54 #include "output.h"
55 #include "mystring.h"
56 #include "myhistedit.h"
57 #include "error.h"
59 #ifndef SMALL
60 #include "eval.h"
61 #include "memalloc.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 */
68 int displayhist;
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 *);
74 #ifdef DEBUG
75 extern FILE *tracefile;
76 #endif
79 * Set history and editing status. Called whenever the status may
80 * have changed (figures out what to do).
82 void
83 histedit(void)
85 FILE *el_err;
87 #define editing (Eflag(psh) || Vflag(psh))
89 if (iflag(psh)) {
90 if (!hist) {
92 * turn history on
94 INTOFF;
95 hist = history_init();
96 INTON;
98 if (hist != NULL)
99 sethistsize(histsizeval());
100 else
101 out2str(psh, "sh: can't initialize history\n");
103 if (editing && !el && isatty(0)) { /* && isatty(2) ??? */
105 * turn editing on
107 char *term, *shname;
109 INTOFF;
110 if (el_in == NULL)
111 el_in = fdopen(0, "r");
112 if (el_out == NULL)
113 el_out = fdopen(2, "w");
114 if (el_in == NULL || el_out == NULL)
115 goto bad;
116 el_err = el_out;
117 #if DEBUG
118 if (tracefile)
119 el_err = tracefile;
120 #endif
121 term = lookupvar(psh, "TERM");
122 if (term)
123 setenv("TERM", term, 1);
124 else
125 unsetenv("TERM");
126 shname = psh->arg0;
127 if (shname[0] == '-')
128 shname++;
129 el = el_init(shname, el_in, el_out, el_err);
130 if (el != NULL) {
131 if (hist)
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",
137 _el_fn_complete);
138 } else {
139 bad:
140 out2str(psh, "sh: can't initialize editing\n");
142 INTON;
143 } else if (!editing && el) {
144 INTOFF;
145 el_end(el);
146 el = NULL;
147 INTON;
149 if (el) {
150 if (Vflag(psh))
151 el_set(el, EL_EDITOR, "vi");
152 else if (Eflag(psh))
153 el_set(el, EL_EDITOR, "emacs");
154 el_set(el, EL_BIND, "^I",
155 tabcomplete(psh) ? "rl-complete" : "ed-insert", NULL);
156 el_source(el, NULL);
158 } else {
159 INTOFF;
160 if (el) { /* no editing if not interactive */
161 el_end(el);
162 el = NULL;
164 if (hist) {
165 history_end(hist);
166 hist = NULL;
168 INTON;
173 void
174 sethistsize(shinstance *psh, const char *hs)
176 int histsize;
177 HistEvent he;
179 if (hist != NULL) {
180 if (hs == NULL || *hs == '\0' ||
181 (histsize = atoi(hs)) < 0)
182 histsize = 100;
183 history(hist, &he, H_SETSIZE, histsize);
187 void
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");
198 inputrc(argc, argv)
199 int argc;
200 char **argv;
202 if (argc != 2) {
203 out2str(psh, "usage: inputrc file\n");
204 return 1;
206 if (el != NULL) {
207 if (el_source(el, argv[1])) {
208 out2str(psh, "inputrc: failed\n");
209 return 1;
210 } else
211 return 0;
212 } else {
213 out2str(psh, "sh: inputrc ignored, not editing\n");
214 return 1;
219 * This command is provided since POSIX decided to standardize
220 * the Korn shell fc command. Oh well...
223 histcmd(int argc, char **argv)
225 int ch;
226 const char *editor = NULL;
227 HistEvent he;
228 int lflg = 0, nflg = 0, rflg = 0, sflg = 0;
229 int i, retval;
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];
237 FILE *efp;
238 #ifdef __GNUC__
239 /* Avoid longjmp clobbering */
240 (void) &editor;
241 (void) &lflg;
242 (void) &nflg;
243 (void) &rflg;
244 (void) &sflg;
245 (void) &firststr;
246 (void) &laststr;
247 (void) &pat;
248 (void) &repl;
249 (void) &efp;
250 (void) &argc;
251 (void) &argv;
252 #endif
254 if (hist == NULL)
255 error(psh, "history not active");
257 if (argc == 1)
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)
263 switch ((char)ch) {
264 case 'e':
265 editor = optionarg;
266 break;
267 case 'l':
268 lflg = 1;
269 break;
270 case 'n':
271 nflg = 1;
272 break;
273 case 'r':
274 rflg = 1;
275 break;
276 case 's':
277 sflg = 1;
278 break;
279 case ':':
280 error(psh, "option -%c expects argument", optopt);
281 /* NOTREACHED */
282 case '?':
283 default:
284 error(psh, "unknown option: -%c", optopt);
285 /* NOTREACHED */
287 argc -= optind, argv += optind;
290 * If executing...
292 if (lflg == 0 || editor || sflg) {
293 lflg = 0; /* ignore */
294 editfile[0] = '\0';
296 * Catch interrupts to reset active counter and
297 * cleanup temp files.
299 if (setjmp(jmploc.loc)) {
300 active = 0;
301 if (*editfile)
302 unlink(editfile);
303 handler = savehandler;
304 longjmp(handler->loc, 1);
306 savehandler = handler;
307 handler = &jmploc;
308 if (++active > MAXHISTLOOPS) {
309 active = 0;
310 displayhist = 0;
311 error(psh, "called recursively too many times");
314 * Set editor.
316 if (sflg == 0) {
317 if (editor == NULL &&
318 (editor = bltinlookup(psh, "FCEDIT", 1)) == NULL &&
319 (editor = bltinlookup(psh, "EDITOR", 1)) == NULL)
320 editor = DEFEDITOR;
321 if (editor[0] == '-' && editor[1] == '\0') {
322 sflg = 1; /* no edit */
323 editor = NULL;
329 * If executing, parse [old=new] now
331 if (lflg == 0 && argc > 0 &&
332 ((repl = strchr(argv[0], '=')) != NULL)) {
333 pat = argv[0];
334 *repl++ = '\0';
335 argc--, argv++;
338 * determine [first] and [last]
340 switch (argc) {
341 case 0:
342 firststr = lflg ? "-16" : "-1";
343 laststr = "-1";
344 break;
345 case 1:
346 firststr = argv[0];
347 laststr = lflg ? "-1" : argv[0];
348 break;
349 case 2:
350 firststr = argv[0];
351 laststr = argv[1];
352 break;
353 default:
354 error(psh, "too many args");
355 /* NOTREACHED */
358 * Turn into event numbers.
360 first = str_to_event(firststr, 0);
361 last = str_to_event(laststr, 1);
363 if (rflg) {
364 i = last;
365 last = first;
366 first = i;
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.
378 if (editor) {
379 int fd;
380 INTOFF; /* easier */
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
393 * after.
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)) {
401 if (lflg) {
402 if (!nflg)
403 out1fmt(psh, "%5d ", he.num);
404 out1str(psh, he.str);
405 } else {
406 const char *s = pat ?
407 fc_replace(he.str, pat, repl) : he.str;
409 if (sflg) {
410 if (displayhist) {
411 out2str(psh, s);
414 evalstring(psh, strcpy(stalloc(psh, strlen(s) + 1), s), 0);
415 if (displayhist && hist) {
417 * XXX what about recursive and
418 * relative histnums.
420 history(hist, &he, H_ENTER, s);
422 } else
423 fputs(s, efp);
426 * At end? (if we were to lose last, we'd sure be
427 * messed up).
429 if (he.num == last)
430 break;
432 if (editor) {
433 char *editcmd;
435 fclose(efp);
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 */
439 INTON;
440 readcmdfile(psh, editfile); /* XXX - should read back - quick tst */
441 unlink(editfile);
444 if (lflg == 0 && active > 0)
445 --active;
446 if (displayhist)
447 displayhist = 0;
448 return 0;
451 STATIC const char *
452 fc_replace(const char *s, char *p, char *r)
454 char *dest;
455 int plen = strlen(p);
457 STARTSTACKSTR(psh, dest);
458 while (*s) {
459 if (*s == *p && strncmp(s, p, plen) == 0) {
460 while (*r)
461 STPUTC(psh, *r++, dest);
462 s += plen;
463 *p = '\0'; /* so no more matches */
464 } else
465 STPUTC(psh, *s++, dest);
467 STACKSTRNUL(psh, dest);
468 dest = grabstackstr(psh, dest);
470 return (dest);
474 not_fcnumber(char *s)
476 if (s == NULL)
477 return 0;
478 if (*s == '-')
479 s++;
480 return (!is_number(s));
484 str_to_event(const char *str, int last)
486 HistEvent he;
487 const char *s = str;
488 int relative = 0;
489 int i, retval;
491 retval = history(hist, &he, H_FIRST);
492 switch (*s) {
493 case '-':
494 relative = 1;
495 /*FALLTHROUGH*/
496 case '+':
497 s++;
499 if (is_number(s)) {
500 i = atoi(s);
501 if (relative) {
502 while (retval != -1 && i--) {
503 retval = history(hist, &he, H_NEXT);
505 if (retval == -1)
506 retval = history(hist, &he, H_LAST);
507 } else {
508 retval = history(hist, &he, H_NEXT_EVENT, i);
509 if (retval == -1) {
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);
518 if (retval == -1)
519 error(psh, "history number %s not found (internal error)",
520 str);
521 } else {
523 * pattern
525 retval = history(hist, &he, H_PREV_STR, str);
526 if (retval == -1)
527 error(psh, "history pattern not found: %s", str);
529 return (he.num);
531 #else /* SMALL */
533 histcmd(shinstance *psh, int argc, char **argv)
535 error(psh, "not compiled with history support");
536 /* NOTREACHED */
537 return -1;
540 inputrc(shinstance *psh, int argc, char **argv)
542 error(psh, "not compiled with history support");
543 /* NOTREACHED */
544 return -1;
546 #endif /* SMALL */