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