VirtualBox

source: kBuild/trunk/src/kash/histedit.c@ 2629

Last change on this file since 2629 was 1233, checked in by bird, 17 years ago

keywords.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 11.9 KB
Line 
1/* $NetBSD: histedit.c,v 1.36 2005/05/09 11:35:19 christos Exp $ */
2
3/*-
4 * Copyright (c) 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Kenneth Almquist.
9 *
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.
21 *
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.
33 */
34
35#if 0
36#ifndef lint
37static 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
42
43#include <stdio.h>
44#include <stdlib.h>
45
46/*
47 * Editline and history functions (and glue).
48 */
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"
58
59#ifndef SMALL
60#include "eval.h"
61#include "memalloc.h"
62
63#define MAXHISTLOOPS 4 /* max recursions through fc */
64#define DEFEDITOR "ed" /* default editor *should* be $EDITOR */
65
66History *hist; /* history cookie */
67EditLine *el; /* editline cookie */
68int displayhist;
69static FILE *el_in, *el_out;
70unsigned char _el_fn_complete(EditLine *, int);
71
72STATIC const char *fc_replace(const char *, char *, char *);
73
74#ifdef DEBUG
75extern FILE *tracefile;
76#endif
77
78/*
79 * Set history and editing status. Called whenever the status may
80 * have changed (figures out what to do).
81 */
82void
83histedit(void)
84{
85 FILE *el_err;
86
87#define editing (Eflag(psh) || Vflag(psh))
88
89 if (iflag(psh)) {
90 if (!hist) {
91 /*
92 * turn history on
93 */
94 INTOFF;
95 hist = history_init();
96 INTON;
97
98 if (hist != NULL)
99 sethistsize(histsizeval());
100 else
101 out2str(psh, "sh: can't initialize history\n");
102 }
103 if (editing && !el && isatty(0)) { /* && isatty(2) ??? */
104 /*
105 * turn editing on
106 */
107 char *term, *shname;
108
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 {
139bad:
140 out2str(psh, "sh: can't initialize editing\n");
141 }
142 INTON;
143 } else if (!editing && el) {
144 INTOFF;
145 el_end(el);
146 el = NULL;
147 INTON;
148 }
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);
157 }
158 } else {
159 INTOFF;
160 if (el) { /* no editing if not interactive */
161 el_end(el);
162 el = NULL;
163 }
164 if (hist) {
165 history_end(hist);
166 hist = NULL;
167 }
168 INTON;
169 }
170}
171
172
173void
174sethistsize(shinstance *psh, const char *hs)
175{
176 int histsize;
177 HistEvent he;
178
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);
184 }
185}
186
187void
188setterm(shinstance *psh, const char *term)
189{
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");
194 }
195}
196
197int
198inputrc(argc, argv)
199 int argc;
200 char **argv;
201{
202 if (argc != 2) {
203 out2str(psh, "usage: inputrc file\n");
204 return 1;
205 }
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;
215 }
216}
217
218/*
219 * This command is provided since POSIX decided to standardize
220 * the Korn shell fc command. Oh well...
221 */
222int
223histcmd(int argc, char **argv)
224{
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
253
254 if (hist == NULL)
255 error(psh, "history not active");
256
257 if (argc == 1)
258 error(psh, "missing history argument");
259
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 */
286 }
287 argc -= optind, argv += optind;
288
289 /*
290 * If executing...
291 */
292 if (lflg == 0 || editor || sflg) {
293 lflg = 0; /* ignore */
294 editfile[0] = '\0';
295 /*
296 * Catch interrupts to reset active counter and
297 * cleanup temp files.
298 */
299 if (setjmp(jmploc.loc)) {
300 active = 0;
301 if (*editfile)
302 unlink(editfile);
303 handler = savehandler;
304 longjmp(handler->loc, 1);
305 }
306 savehandler = handler;
307 handler = &jmploc;
308 if (++active > MAXHISTLOOPS) {
309 active = 0;
310 displayhist = 0;
311 error(psh, "called recursively too many times");
312 }
313 /*
314 * Set editor.
315 */
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;
324 }
325 }
326 }
327
328 /*
329 * If executing, parse [old=new] now
330 */
331 if (lflg == 0 && argc > 0 &&
332 ((repl = strchr(argv[0], '=')) != NULL)) {
333 pat = argv[0];
334 *repl++ = '\0';
335 argc--, argv++;
336 }
337 /*
338 * determine [first] and [last]
339 */
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 */
356 }
357 /*
358 * Turn into event numbers.
359 */
360 first = str_to_event(firststr, 0);
361 last = str_to_event(laststr, 1);
362
363 if (rflg) {
364 i = last;
365 last = first;
366 first = i;
367 }
368 /*
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.
372 */
373 direction = first < last ? H_PREV : H_NEXT;
374
375 /*
376 * If editing, grab a temp file.
377 */
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");
387 }
388 }
389
390 /*
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.
394 *
395 * The history interface needs rethinking, as the following
396 * convolutions will demonstrate.
397 */
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;
408
409 if (sflg) {
410 if (displayhist) {
411 out2str(psh, s);
412 }
413
414 evalstring(psh, strcpy(stalloc(psh, strlen(s) + 1), s), 0);
415 if (displayhist && hist) {
416 /*
417 * XXX what about recursive and
418 * relative histnums.
419 */
420 history(hist, &he, H_ENTER, s);
421 }
422 } else
423 fputs(s, efp);
424 }
425 /*
426 * At end? (if we were to lose last, we'd sure be
427 * messed up).
428 */
429 if (he.num == last)
430 break;
431 }
432 if (editor) {
433 char *editcmd;
434
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);
442 }
443
444 if (lflg == 0 && active > 0)
445 --active;
446 if (displayhist)
447 displayhist = 0;
448 return 0;
449}
450
451STATIC const char *
452fc_replace(const char *s, char *p, char *r)
453{
454 char *dest;
455 int plen = strlen(p);
456
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);
466 }
467 STACKSTRNUL(psh, dest);
468 dest = grabstackstr(psh, dest);
469
470 return (dest);
471}
472
473int
474not_fcnumber(char *s)
475{
476 if (s == NULL)
477 return 0;
478 if (*s == '-')
479 s++;
480 return (!is_number(s));
481}
482
483int
484str_to_event(const char *str, int last)
485{
486 HistEvent he;
487 const char *s = str;
488 int relative = 0;
489 int i, retval;
490
491 retval = history(hist, &he, H_FIRST);
492 switch (*s) {
493 case '-':
494 relative = 1;
495 /*FALLTHROUGH*/
496 case '+':
497 s++;
498 }
499 if (is_number(s)) {
500 i = atoi(s);
501 if (relative) {
502 while (retval != -1 && i--) {
503 retval = history(hist, &he, H_NEXT);
504 }
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) {
510 /*
511 * the notion of first and last is
512 * backwards to that of the history package
513 */
514 retval = history(hist, &he,
515 last ? H_FIRST : H_LAST);
516 }
517 }
518 if (retval == -1)
519 error(psh, "history number %s not found (internal error)",
520 str);
521 } else {
522 /*
523 * pattern
524 */
525 retval = history(hist, &he, H_PREV_STR, str);
526 if (retval == -1)
527 error(psh, "history pattern not found: %s", str);
528 }
529 return (he.num);
530}
531#else /* SMALL */
532int
533histcmd(shinstance *psh, int argc, char **argv)
534{
535 error(psh, "not compiled with history support");
536 /* NOTREACHED */
537 return -1;
538}
539int
540inputrc(shinstance *psh, int argc, char **argv)
541{
542 error(psh, "not compiled with history support");
543 /* NOTREACHED */
544 return -1;
545}
546#endif /* SMALL */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette