add opendir alias
[minix.git] / commands / ash / histedit.c
blobe341c56d83d61900e6101b5da96651b576a5bf39
1 /*-
2 * Copyright (c) 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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
30 * SUCH DAMAGE.
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)histedit.c 8.2 (Berkeley) 5/4/95";
36 #endif
37 #endif /* not lint */
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/bin/sh/histedit.c,v 1.26 2004/04/06 20:06:51 markm Exp $");
43 #include <limits.h>
44 #ifndef NO_PATHS_H
45 #include <paths.h>
46 #endif
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <unistd.h>
51 * Editline and history functions (and glue).
53 #include "shell.h"
54 #include "parser.h"
55 #include "var.h"
56 #include "options.h"
57 #include "main.h"
58 #include "output.h"
59 #include "mystring.h"
60 #include "builtins.h"
61 #if !defined(NO_HISTORY)
62 #include "myhistedit.h"
63 #include "complete.h"
64 #include "error.h"
65 #include "eval.h"
66 #include "memalloc.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 */
73 int displayhist;
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).
84 void
85 histedit(void)
88 #define editing (Eflag || Vflag)
90 if (iflag) {
91 if (!hist) {
93 * turn history on
95 INTOFF;
96 hist = history_init();
97 INTON;
99 if (hist != NULL)
100 sethistsize(histsizeval());
101 else
102 out2str("sh: can't initialize history\n");
104 if (editing && !el && isatty(0)) { /* && isatty(2) ??? */
106 * turn editing on
108 INTOFF;
109 if (el_in == NULL)
110 el_in = fdopen(0, "r");
111 if (el_err == NULL)
112 el_err = fdopen(1, "w");
113 if (el_out == NULL)
114 el_out = fdopen(2, "w");
115 if (el_in == NULL || el_err == NULL || el_out == NULL)
116 goto bad;
117 el = el_init(arg0, el_in, el_out, el_err);
118 if (el != NULL) {
119 if (hist)
120 el_set(el, EL_HIST, history, hist);
121 el_set(el, EL_PROMPT, getprompt);
122 } else {
123 bad:
124 out2str("sh: can't initialize editing\n");
126 INTON;
127 } else if (!editing && el) {
128 INTOFF;
129 el_end(el);
130 el = NULL;
131 INTON;
133 if (el) {
134 if (Vflag)
135 el_set(el, EL_EDITOR, "vi");
136 else if (Eflag)
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",
144 complete_or_list);
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",
149 NULL);
150 el_set(el, EL_BIND, "-a", "\\\\", "ed-do-complete",
151 NULL);
152 el_set(el, EL_BIND, "-a", "*", "ed-expand",
153 NULL);
154 el_source(el, NULL);
156 } else {
157 INTOFF;
158 if (el) { /* no editing if not interactive */
159 el_end(el);
160 el = NULL;
162 if (hist) {
163 history_end(hist);
164 hist = NULL;
166 INTON;
171 void
172 sethistsize(hs)
173 const char *hs;
175 int histsize;
176 HistEvent he;
178 if (hist != NULL) {
179 if (hs == NULL || *hs == '\0' ||
180 (histsize = atoi(hs)) < 0)
181 histsize = 100;
182 history(hist, &he, H_SETSIZE, histsize);
187 histcmd(int argc, char **argv)
189 int ch;
190 char *editor = NULL;
191 HistEvent he;
192 int lflg = 0, nflg = 0, rflg = 0, sflg = 0;
193 int i, retval;
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];
201 FILE *efp;
202 int oldhistnum;
203 #ifdef __GNUC__
204 /* Avoid longjmp clobbering */
205 (void) &editor;
206 (void) &lflg;
207 (void) &nflg;
208 (void) &rflg;
209 (void) &sflg;
210 (void) &firststr;
211 (void) &laststr;
212 (void) &pat;
213 (void) &repl;
214 (void) &efp;
215 (void) &argc;
216 (void) &argv;
217 #endif
219 if (hist == NULL)
220 error("history not active");
222 if (argc == 1)
223 error("missing history argument");
225 optreset = 1; optind = 1; /* initialize getopt */
226 opterr = 0;
227 while (not_fcnumber(argv[optind]) &&
228 (ch = getopt(argc, argv, ":e:lnrs")) != -1)
229 switch ((char)ch) {
230 case 'e':
231 editor = optarg;
232 break;
233 case 'l':
234 lflg = 1;
235 break;
236 case 'n':
237 nflg = 1;
238 break;
239 case 'r':
240 rflg = 1;
241 break;
242 case 's':
243 sflg = 1;
244 break;
245 case ':':
246 error("option -%c expects argument", optopt);
247 case '?':
248 default:
249 error("unknown option: -%c", optopt);
251 argc -= optind, argv += optind;
254 * If executing...
256 if (lflg == 0 || editor || sflg) {
257 lflg = 0; /* ignore */
258 editfile[0] = '\0';
260 * Catch interrupts to reset active counter and
261 * cleanup temp files.
263 if (setjmp(jmploc.loc)) {
264 active = 0;
265 if (*editfile)
266 unlink(editfile);
267 handler = savehandler;
268 longjmp(handler->loc, 1);
270 savehandler = handler;
271 handler = &jmploc;
272 if (++active > MAXHISTLOOPS) {
273 active = 0;
274 displayhist = 0;
275 error("called recursively too many times");
278 * Set editor.
280 if (sflg == 0) {
281 if (editor == NULL &&
282 (editor = bltinlookup("FCEDIT", 1)) == NULL &&
283 (editor = bltinlookup("EDITOR", 1)) == NULL)
284 editor = DEFEDITOR;
285 if (editor[0] == '-' && editor[1] == '\0') {
286 sflg = 1; /* no edit */
287 editor = NULL;
293 * If executing, parse [old=new] now
295 if (lflg == 0 && argc > 0 &&
296 ((repl = strchr(argv[0], '=')) != NULL)) {
297 pat = argv[0];
298 *repl++ = '\0';
299 argc--, argv++;
302 * determine [first] and [last]
304 switch (argc) {
305 case 0:
306 firststr = lflg ? "-16" : "-1";
307 laststr = "-1";
308 break;
309 case 1:
310 firststr = argv[0];
311 laststr = lflg ? "-1" : argv[0];
312 break;
313 case 2:
314 firststr = argv[0];
315 laststr = argv[1];
316 break;
317 default:
318 error("too many args");
321 * Turn into event numbers.
323 first = str_to_event(firststr, 0);
324 last = str_to_event(laststr, 1);
326 if (rflg) {
327 i = last;
328 last = first;
329 first = i;
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.
341 if (editor) {
342 int fd;
343 INTOFF; /* easier */
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) {
348 close(fd);
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
356 * after.
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)) {
364 if (lflg) {
365 if (!nflg)
366 out1fmt("%5d ", he.num);
367 out1str(he.str);
368 } else {
369 char *s = pat ?
370 fc_replace(he.str, pat, repl) : (char *)he.str;
372 if (sflg) {
373 if (displayhist) {
374 out2str(s);
376 evalstring(s);
377 if (displayhist && hist) {
379 * XXX what about recursive and
380 * relative histnums.
382 oldhistnum = he.num;
383 history(hist, &he, H_ENTER, s);
385 * XXX H_ENTER moves the internal
386 * cursor, set it back to the current
387 * entry.
389 retval = history(hist, &he,
390 H_NEXT_EVENT, oldhistnum);
392 } else
393 fputs(s, efp);
396 * At end? (if we were to loose last, we'd sure be
397 * messed up).
399 if (he.num == last)
400 break;
402 if (editor) {
403 char *editcmd;
405 fclose(efp);
406 editcmd = stalloc(strlen(editor) + strlen(editfile) + 2);
407 sprintf(editcmd, "%s %s", editor, editfile);
408 evalstring(editcmd); /* XXX - should use no JC command */
409 INTON;
410 readcmdfile(editfile); /* XXX - should read back - quick tst */
411 unlink(editfile);
414 if (lflg == 0 && active > 0)
415 --active;
416 if (displayhist)
417 displayhist = 0;
418 return 0;
421 STATIC char *
422 fc_replace(const char *s, char *p, char *r)
424 char *dest;
425 int plen = strlen(p);
427 STARTSTACKSTR(dest);
428 while (*s) {
429 if (*s == *p && strncmp(s, p, plen) == 0) {
430 while (*r)
431 STPUTC(*r++, dest);
432 s += plen;
433 *p = '\0'; /* so no more matches */
434 } else
435 STPUTC(*s++, dest);
437 STACKSTRNUL(dest);
438 dest = grabstackstr(dest);
440 return (dest);
443 STATIC int
444 not_fcnumber(char *s)
446 if (s == NULL)
447 return (0);
448 if (*s == '-')
449 s++;
450 return (!is_number(s));
453 STATIC int
454 str_to_event(char *str, int last)
456 HistEvent he;
457 char *s = str;
458 int relative = 0;
459 int i, retval;
461 retval = history(hist, &he, H_FIRST);
462 switch (*s) {
463 case '-':
464 relative = 1;
465 /*FALLTHROUGH*/
466 case '+':
467 s++;
469 if (is_number(s)) {
470 i = atoi(s);
471 if (relative) {
472 while (retval != -1 && i--) {
473 retval = history(hist, &he, H_NEXT);
475 if (retval == -1)
476 retval = history(hist, &he, H_LAST);
477 } else {
478 retval = history(hist, &he, H_NEXT_EVENT, i);
479 if (retval == -1) {
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);
487 if (retval == -1)
488 error("history number %s not found (internal error)",
489 str);
490 } else {
492 * pattern
494 retval = history(hist, &he, H_PREV_STR, str);
495 if (retval == -1)
496 error("history pattern not found: %s", str);
498 return (he.num);
502 bindcmd(int argc, char **argv)
505 if (el == NULL)
506 error("line editing is disabled");
507 return (el_parse(el, argc, argv));
510 #else
511 #include "error.h"
514 histcmd(int argc, char **argv)
517 error("not compiled with history support");
518 /*NOTREACHED*/
519 return (0);
523 bindcmd(int argc, char **argv)
526 error("not compiled with line editing support");
527 return (0);
529 #endif /* !NO_HISTORY */
532 * $PchId: histedit.c,v 1.6 2006/04/10 14:52:58 philip Exp $