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