histedit.c revision 1.5 1 1.1 jtc /*-
2 1.1 jtc * Copyright (c) 1993
3 1.1 jtc * The Regents of the University of California. All rights reserved.
4 1.1 jtc *
5 1.1 jtc * This code is derived from software contributed to Berkeley by
6 1.1 jtc * Kenneth Almquist.
7 1.1 jtc *
8 1.1 jtc * Redistribution and use in source and binary forms, with or without
9 1.1 jtc * modification, are permitted provided that the following conditions
10 1.1 jtc * are met:
11 1.1 jtc * 1. Redistributions of source code must retain the above copyright
12 1.1 jtc * notice, this list of conditions and the following disclaimer.
13 1.1 jtc * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 jtc * notice, this list of conditions and the following disclaimer in the
15 1.1 jtc * documentation and/or other materials provided with the distribution.
16 1.1 jtc * 3. All advertising materials mentioning features or use of this software
17 1.1 jtc * must display the following acknowledgement:
18 1.1 jtc * This product includes software developed by the University of
19 1.1 jtc * California, Berkeley and its contributors.
20 1.1 jtc * 4. Neither the name of the University nor the names of its contributors
21 1.1 jtc * may be used to endorse or promote products derived from this software
22 1.1 jtc * without specific prior written permission.
23 1.1 jtc *
24 1.1 jtc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 jtc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 jtc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 jtc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 jtc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 jtc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 jtc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 jtc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 jtc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 jtc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 jtc * SUCH DAMAGE.
35 1.1 jtc */
36 1.1 jtc
37 1.1 jtc #ifndef lint
38 1.3 mycroft /*static char sccsid[] = "from: @(#)histedit.c 8.1 (Berkeley) 5/31/93";*/
39 1.5 cgd static char *rcsid = "$Id: histedit.c,v 1.5 1994/12/05 19:07:42 cgd Exp $";
40 1.1 jtc #endif /* not lint */
41 1.1 jtc
42 1.1 jtc /*
43 1.1 jtc * Editline and history functions (and glue).
44 1.1 jtc */
45 1.1 jtc #include <sys/param.h>
46 1.1 jtc #include <paths.h>
47 1.1 jtc #include <stdio.h>
48 1.2 jtc #include <stdlib.h>
49 1.2 jtc #include <unistd.h>
50 1.1 jtc #include "shell.h"
51 1.1 jtc #include "parser.h"
52 1.1 jtc #include "var.h"
53 1.1 jtc #include "options.h"
54 1.4 cgd #include "output.h"
55 1.1 jtc #include "mystring.h"
56 1.1 jtc #include "error.h"
57 1.5 cgd #include "eval.h"
58 1.1 jtc #include "histedit.h"
59 1.1 jtc #include "memalloc.h"
60 1.4 cgd #include "extern.h"
61 1.1 jtc
62 1.1 jtc #define MAXHISTLOOPS 4 /* max recursions through fc */
63 1.1 jtc #define DEFEDITOR "ed" /* default editor *should* be $EDITOR */
64 1.1 jtc
65 1.1 jtc History *hist; /* history cookie */
66 1.1 jtc EditLine *el; /* editline cookie */
67 1.1 jtc int displayhist;
68 1.1 jtc static FILE *el_in, *el_out;
69 1.1 jtc
70 1.1 jtc STATIC char *fc_replace __P((const char *, char *, char *));
71 1.4 cgd int not_fcnumber __P((char *));
72 1.4 cgd int str_to_event __P((char *, int));
73 1.1 jtc
74 1.1 jtc /*
75 1.1 jtc * Set history and editing status. Called whenever the status may
76 1.1 jtc * have changed (figures out what to do).
77 1.1 jtc */
78 1.5 cgd void
79 1.4 cgd histedit()
80 1.4 cgd {
81 1.1 jtc
82 1.1 jtc #define editing (Eflag || Vflag)
83 1.1 jtc
84 1.1 jtc if (iflag) {
85 1.1 jtc if (!hist) {
86 1.1 jtc /*
87 1.1 jtc * turn history on
88 1.1 jtc */
89 1.1 jtc INTOFF;
90 1.1 jtc hist = history_init();
91 1.1 jtc INTON;
92 1.1 jtc
93 1.1 jtc if (hist != NULL)
94 1.1 jtc sethistsize();
95 1.1 jtc else
96 1.1 jtc out2str("sh: can't initialize history\n");
97 1.1 jtc }
98 1.1 jtc if (editing && !el && isatty(0)) { /* && isatty(2) ??? */
99 1.1 jtc /*
100 1.1 jtc * turn editing on
101 1.1 jtc */
102 1.1 jtc INTOFF;
103 1.1 jtc if (el_in == NULL)
104 1.1 jtc el_in = fdopen(0, "r");
105 1.1 jtc if (el_out == NULL)
106 1.1 jtc el_out = fdopen(2, "w");
107 1.1 jtc if (el_in == NULL || el_out == NULL)
108 1.1 jtc goto bad;
109 1.1 jtc el = el_init(arg0, el_in, el_out);
110 1.1 jtc if (el != NULL) {
111 1.1 jtc if (hist)
112 1.1 jtc el_set(el, EL_HIST, history, hist);
113 1.1 jtc el_set(el, EL_PROMPT, getprompt);
114 1.1 jtc } else {
115 1.1 jtc bad:
116 1.1 jtc out2str("sh: can't initialize editing\n");
117 1.1 jtc }
118 1.1 jtc INTON;
119 1.1 jtc } else if (!editing && el) {
120 1.1 jtc INTOFF;
121 1.1 jtc el_end(el);
122 1.1 jtc el = NULL;
123 1.1 jtc INTON;
124 1.1 jtc }
125 1.1 jtc if (el) {
126 1.1 jtc if (Vflag)
127 1.1 jtc el_set(el, EL_EDITOR, "vi");
128 1.1 jtc else if (Eflag)
129 1.1 jtc el_set(el, EL_EDITOR, "emacs");
130 1.1 jtc }
131 1.1 jtc } else {
132 1.1 jtc INTOFF;
133 1.1 jtc if (el) { /* no editing if not interactive */
134 1.1 jtc el_end(el);
135 1.1 jtc el = NULL;
136 1.1 jtc }
137 1.1 jtc if (hist) {
138 1.1 jtc history_end(hist);
139 1.1 jtc hist = NULL;
140 1.1 jtc }
141 1.1 jtc INTON;
142 1.1 jtc }
143 1.1 jtc }
144 1.1 jtc
145 1.4 cgd
146 1.4 cgd void
147 1.1 jtc sethistsize() {
148 1.1 jtc char *cp;
149 1.1 jtc int histsize;
150 1.1 jtc
151 1.1 jtc if (hist != NULL) {
152 1.1 jtc cp = lookupvar("HISTSIZE");
153 1.1 jtc if (cp == NULL || *cp == '\0' ||
154 1.1 jtc (histsize = atoi(cp)) < 0)
155 1.1 jtc histsize = 100;
156 1.1 jtc history(hist, H_EVENT, histsize);
157 1.1 jtc }
158 1.1 jtc }
159 1.1 jtc
160 1.1 jtc /*
161 1.1 jtc * This command is provided since POSIX decided to standardize
162 1.1 jtc * the Korn shell fc command. Oh well...
163 1.1 jtc */
164 1.4 cgd int
165 1.1 jtc histcmd(argc, argv)
166 1.4 cgd int argc;
167 1.1 jtc char *argv[];
168 1.1 jtc {
169 1.1 jtc extern char *optarg;
170 1.1 jtc extern int optind, optopt, optreset;
171 1.1 jtc int ch;
172 1.1 jtc char *editor = NULL;
173 1.1 jtc const HistEvent *he;
174 1.1 jtc int lflg = 0, nflg = 0, rflg = 0, sflg = 0;
175 1.1 jtc int i;
176 1.1 jtc char *firststr, *laststr;
177 1.1 jtc int first, last, direction;
178 1.1 jtc char *pat = NULL, *repl; /* ksh "fc old=new" crap */
179 1.1 jtc static int active = 0;
180 1.1 jtc struct jmploc jmploc;
181 1.1 jtc struct jmploc *volatile savehandler;
182 1.1 jtc char editfile[MAXPATHLEN + 1];
183 1.1 jtc FILE *efp;
184 1.1 jtc
185 1.1 jtc if (hist == NULL)
186 1.1 jtc error("history not active");
187 1.1 jtc
188 1.1 jtc if (argc == 1)
189 1.1 jtc error("missing history argument");
190 1.1 jtc
191 1.1 jtc optreset = 1; optind = 1; /* initialize getopt */
192 1.1 jtc while (not_fcnumber(argv[optind]) &&
193 1.1 jtc (ch = getopt(argc, argv, ":e:lnrs")) != EOF)
194 1.1 jtc switch ((char)ch) {
195 1.1 jtc case 'e':
196 1.1 jtc editor = optarg;
197 1.1 jtc break;
198 1.1 jtc case 'l':
199 1.1 jtc lflg = 1;
200 1.1 jtc break;
201 1.1 jtc case 'n':
202 1.1 jtc nflg = 1;
203 1.1 jtc break;
204 1.1 jtc case 'r':
205 1.1 jtc rflg = 1;
206 1.1 jtc break;
207 1.1 jtc case 's':
208 1.1 jtc sflg = 1;
209 1.1 jtc break;
210 1.1 jtc case ':':
211 1.1 jtc error("option -%c expects argument", optopt);
212 1.1 jtc case '?':
213 1.1 jtc default:
214 1.1 jtc error("unknown option: -%c", optopt);
215 1.1 jtc }
216 1.1 jtc argc -= optind, argv += optind;
217 1.1 jtc
218 1.1 jtc /*
219 1.1 jtc * If executing...
220 1.1 jtc */
221 1.1 jtc if (lflg == 0 || editor || sflg) {
222 1.1 jtc lflg = 0; /* ignore */
223 1.1 jtc editfile[0] = '\0';
224 1.1 jtc /*
225 1.1 jtc * Catch interrupts to reset active counter and
226 1.1 jtc * cleanup temp files.
227 1.1 jtc */
228 1.1 jtc if (setjmp(jmploc.loc)) {
229 1.1 jtc active = 0;
230 1.1 jtc if (*editfile)
231 1.1 jtc unlink(editfile);
232 1.1 jtc handler = savehandler;
233 1.1 jtc longjmp(handler->loc, 1);
234 1.1 jtc }
235 1.1 jtc savehandler = handler;
236 1.1 jtc handler = &jmploc;
237 1.1 jtc if (++active > MAXHISTLOOPS) {
238 1.1 jtc active = 0;
239 1.1 jtc displayhist = 0;
240 1.1 jtc error("called recursively too many times");
241 1.1 jtc }
242 1.1 jtc /*
243 1.1 jtc * Set editor.
244 1.1 jtc */
245 1.1 jtc if (sflg == 0) {
246 1.1 jtc if (editor == NULL &&
247 1.1 jtc (editor = bltinlookup("FCEDIT", 1)) == NULL &&
248 1.1 jtc (editor = bltinlookup("EDITOR", 1)) == NULL)
249 1.1 jtc editor = DEFEDITOR;
250 1.1 jtc if (editor[0] == '-' && editor[1] == '\0') {
251 1.1 jtc sflg = 1; /* no edit */
252 1.1 jtc editor = NULL;
253 1.1 jtc }
254 1.1 jtc }
255 1.1 jtc }
256 1.1 jtc
257 1.1 jtc /*
258 1.1 jtc * If executing, parse [old=new] now
259 1.1 jtc */
260 1.1 jtc if (lflg == 0 && argc > 0 &&
261 1.1 jtc ((repl = strchr(argv[0], '=')) != NULL)) {
262 1.1 jtc pat = argv[0];
263 1.1 jtc *repl++ = '\0';
264 1.1 jtc argc--, argv++;
265 1.1 jtc }
266 1.1 jtc /*
267 1.1 jtc * determine [first] and [last]
268 1.1 jtc */
269 1.1 jtc switch (argc) {
270 1.1 jtc case 0:
271 1.1 jtc firststr = lflg ? "-16" : "-1";
272 1.1 jtc laststr = "-1";
273 1.1 jtc break;
274 1.1 jtc case 1:
275 1.1 jtc firststr = argv[0];
276 1.1 jtc laststr = lflg ? "-1" : argv[0];
277 1.1 jtc break;
278 1.1 jtc case 2:
279 1.1 jtc firststr = argv[0];
280 1.1 jtc laststr = argv[1];
281 1.1 jtc break;
282 1.1 jtc default:
283 1.1 jtc error("too many args");
284 1.1 jtc }
285 1.1 jtc /*
286 1.1 jtc * Turn into event numbers.
287 1.1 jtc */
288 1.1 jtc first = str_to_event(firststr, 0);
289 1.1 jtc last = str_to_event(laststr, 1);
290 1.1 jtc
291 1.1 jtc if (rflg) {
292 1.1 jtc i = last;
293 1.1 jtc last = first;
294 1.1 jtc first = i;
295 1.1 jtc }
296 1.1 jtc /*
297 1.1 jtc * XXX - this should not depend on the event numbers
298 1.1 jtc * always increasing. Add sequence numbers or offset
299 1.1 jtc * to the history element in next (diskbased) release.
300 1.1 jtc */
301 1.1 jtc direction = first < last ? H_PREV : H_NEXT;
302 1.1 jtc
303 1.1 jtc /*
304 1.1 jtc * If editing, grab a temp file.
305 1.1 jtc */
306 1.1 jtc if (editor) {
307 1.1 jtc int fd;
308 1.1 jtc INTOFF; /* easier */
309 1.1 jtc sprintf(editfile, "%s/_shXXXXXX", _PATH_TMP);
310 1.1 jtc if ((fd = mkstemp(editfile)) < 0)
311 1.1 jtc error("can't create temporary file %s", editfile);
312 1.1 jtc if ((efp = fdopen(fd, "w")) == NULL) {
313 1.1 jtc close(fd);
314 1.1 jtc error("can't allocate stdio buffer for temp\n");
315 1.1 jtc }
316 1.1 jtc }
317 1.1 jtc
318 1.1 jtc /*
319 1.1 jtc * Loop through selected history events. If listing or executing,
320 1.1 jtc * do it now. Otherwise, put into temp file and call the editor
321 1.1 jtc * after.
322 1.1 jtc *
323 1.1 jtc * The history interface needs rethinking, as the following
324 1.1 jtc * convolutions will demonstrate.
325 1.1 jtc */
326 1.1 jtc history(hist, H_FIRST);
327 1.1 jtc he = history(hist, H_NEXT_EVENT, first);
328 1.1 jtc for (;he != NULL; he = history(hist, direction)) {
329 1.1 jtc if (lflg) {
330 1.1 jtc if (!nflg)
331 1.1 jtc out1fmt("%5d ", he->num);
332 1.1 jtc out1str(he->str);
333 1.1 jtc } else {
334 1.1 jtc char *s = pat ?
335 1.1 jtc fc_replace(he->str, pat, repl) : (char *)he->str;
336 1.1 jtc
337 1.1 jtc if (sflg) {
338 1.1 jtc if (displayhist) {
339 1.1 jtc out2str(s);
340 1.1 jtc }
341 1.1 jtc evalstring(s);
342 1.1 jtc if (displayhist && hist) {
343 1.1 jtc /*
344 1.1 jtc * XXX what about recursive and
345 1.1 jtc * relative histnums.
346 1.1 jtc */
347 1.1 jtc history(hist, H_ENTER, s);
348 1.1 jtc }
349 1.1 jtc } else
350 1.1 jtc fputs(s, efp);
351 1.1 jtc }
352 1.1 jtc /*
353 1.1 jtc * At end? (if we were to loose last, we'd sure be
354 1.1 jtc * messed up).
355 1.1 jtc */
356 1.1 jtc if (he->num == last)
357 1.1 jtc break;
358 1.1 jtc }
359 1.1 jtc if (editor) {
360 1.1 jtc char *editcmd;
361 1.1 jtc
362 1.1 jtc fclose(efp);
363 1.1 jtc editcmd = stalloc(strlen(editor) + strlen(editfile) + 2);
364 1.1 jtc sprintf(editcmd, "%s %s", editor, editfile);
365 1.1 jtc evalstring(editcmd); /* XXX - should use no JC command */
366 1.1 jtc INTON;
367 1.1 jtc readcmdfile(editfile); /* XXX - should read back - quick tst */
368 1.1 jtc unlink(editfile);
369 1.1 jtc }
370 1.1 jtc
371 1.1 jtc if (lflg == 0 && active > 0)
372 1.1 jtc --active;
373 1.1 jtc if (displayhist)
374 1.1 jtc displayhist = 0;
375 1.1 jtc }
376 1.1 jtc
377 1.1 jtc STATIC char *
378 1.1 jtc fc_replace(s, p, r)
379 1.1 jtc const char *s;
380 1.1 jtc char *p, *r;
381 1.1 jtc {
382 1.1 jtc char *dest;
383 1.1 jtc int plen = strlen(p);
384 1.1 jtc
385 1.1 jtc STARTSTACKSTR(dest);
386 1.1 jtc while (*s) {
387 1.1 jtc if (*s == *p && strncmp(s, p, plen) == 0) {
388 1.1 jtc while (*r)
389 1.1 jtc STPUTC(*r++, dest);
390 1.1 jtc s += plen;
391 1.1 jtc *p = '\0'; /* so no more matches */
392 1.1 jtc } else
393 1.1 jtc STPUTC(*s++, dest);
394 1.1 jtc }
395 1.1 jtc STACKSTRNUL(dest);
396 1.1 jtc dest = grabstackstr(dest);
397 1.1 jtc
398 1.1 jtc return (dest);
399 1.1 jtc }
400 1.1 jtc
401 1.4 cgd int
402 1.1 jtc not_fcnumber(s)
403 1.1 jtc char *s;
404 1.1 jtc {
405 1.1 jtc if (*s == '-')
406 1.1 jtc s++;
407 1.1 jtc return (!is_number(s));
408 1.1 jtc }
409 1.1 jtc
410 1.4 cgd int
411 1.1 jtc str_to_event(str, last)
412 1.1 jtc char *str;
413 1.1 jtc int last;
414 1.1 jtc {
415 1.1 jtc const HistEvent *he;
416 1.1 jtc char *s = str;
417 1.1 jtc int relative = 0;
418 1.4 cgd int i;
419 1.1 jtc
420 1.1 jtc he = history(hist, H_FIRST);
421 1.1 jtc switch (*s) {
422 1.1 jtc case '-':
423 1.1 jtc relative = 1;
424 1.1 jtc /*FALLTHROUGH*/
425 1.1 jtc case '+':
426 1.1 jtc s++;
427 1.1 jtc }
428 1.1 jtc if (is_number(s)) {
429 1.1 jtc i = atoi(s);
430 1.1 jtc if (relative) {
431 1.1 jtc while (he != NULL && i--) {
432 1.1 jtc he = history(hist, H_NEXT);
433 1.1 jtc }
434 1.1 jtc if (he == NULL)
435 1.1 jtc he = history(hist, H_LAST);
436 1.1 jtc } else {
437 1.1 jtc he = history(hist, H_NEXT_EVENT, i);
438 1.1 jtc if (he == NULL) {
439 1.1 jtc /*
440 1.1 jtc * the notion of first and last is
441 1.1 jtc * backwards to that of the history package
442 1.1 jtc */
443 1.1 jtc he = history(hist, last ? H_FIRST : H_LAST);
444 1.1 jtc }
445 1.1 jtc }
446 1.1 jtc if (he == NULL)
447 1.1 jtc error("history number %s not found (internal error)",
448 1.1 jtc str);
449 1.1 jtc } else {
450 1.1 jtc /*
451 1.1 jtc * pattern
452 1.1 jtc */
453 1.1 jtc he = history(hist, H_PREV_STR, str);
454 1.1 jtc if (he == NULL)
455 1.1 jtc error("history pattern not found: %s", str);
456 1.1 jtc }
457 1.1 jtc return (he->num);
458 1.1 jtc }
459