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