[626] | 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 |
|
---|
[1214] | 35 | #if 0
|
---|
[626] | 36 | #ifndef lint
|
---|
| 37 | static 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 $");
|
---|
[1214] | 40 | #endif /* not lint */
|
---|
[626] | 41 | #endif
|
---|
| 42 |
|
---|
| 43 | #include <stdio.h>
|
---|
| 44 | #include <stdlib.h>
|
---|
[1214] | 45 |
|
---|
[626] | 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"
|
---|
[1214] | 58 |
|
---|
[626] | 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 |
|
---|
| 66 | History *hist; /* history cookie */
|
---|
| 67 | EditLine *el; /* editline cookie */
|
---|
| 68 | int displayhist;
|
---|
| 69 | static FILE *el_in, *el_out;
|
---|
| 70 | unsigned char _el_fn_complete(EditLine *, int);
|
---|
| 71 |
|
---|
| 72 | STATIC const char *fc_replace(const char *, char *, char *);
|
---|
| 73 |
|
---|
| 74 | #ifdef DEBUG
|
---|
| 75 | extern 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 | */
|
---|
| 82 | void
|
---|
| 83 | histedit(void)
|
---|
| 84 | {
|
---|
| 85 | FILE *el_err;
|
---|
| 86 |
|
---|
[1198] | 87 | #define editing (Eflag(psh) || Vflag(psh))
|
---|
[626] | 88 |
|
---|
[1198] | 89 | if (iflag(psh)) {
|
---|
[626] | 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
|
---|
[1199] | 101 | out2str(psh, "sh: can't initialize history\n");
|
---|
[626] | 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
|
---|
[1202] | 121 | term = lookupvar(psh, "TERM");
|
---|
[626] | 122 | if (term)
|
---|
| 123 | setenv("TERM", term, 1);
|
---|
| 124 | else
|
---|
| 125 | unsetenv("TERM");
|
---|
[1203] | 126 | shname = psh->arg0;
|
---|
[626] | 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 {
|
---|
| 139 | bad:
|
---|
[1199] | 140 | out2str(psh, "sh: can't initialize editing\n");
|
---|
[626] | 141 | }
|
---|
| 142 | INTON;
|
---|
| 143 | } else if (!editing && el) {
|
---|
| 144 | INTOFF;
|
---|
| 145 | el_end(el);
|
---|
| 146 | el = NULL;
|
---|
| 147 | INTON;
|
---|
| 148 | }
|
---|
| 149 | if (el) {
|
---|
[1198] | 150 | if (Vflag(psh))
|
---|
[626] | 151 | el_set(el, EL_EDITOR, "vi");
|
---|
[1198] | 152 | else if (Eflag(psh))
|
---|
[626] | 153 | el_set(el, EL_EDITOR, "emacs");
|
---|
[809] | 154 | el_set(el, EL_BIND, "^I",
|
---|
[1198] | 155 | tabcomplete(psh) ? "rl-complete" : "ed-insert", NULL);
|
---|
[626] | 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 |
|
---|
| 173 | void
|
---|
[1209] | 174 | sethistsize(shinstance *psh, const char *hs)
|
---|
[626] | 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 |
|
---|
| 187 | void
|
---|
[1209] | 188 | setterm(shinstance *psh, const char *term)
|
---|
[626] | 189 | {
|
---|
| 190 | if (el != NULL && term != NULL)
|
---|
| 191 | if (el_set(el, EL_TERMINAL, term) != 0) {
|
---|
[1201] | 192 | outfmt(psh->out2, "sh: Can't set terminal type %s\n", term);
|
---|
| 193 | outfmt(psh->out2, "sh: Using dumb terminal settings.\n");
|
---|
[626] | 194 | }
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | int
|
---|
| 198 | inputrc(argc, argv)
|
---|
| 199 | int argc;
|
---|
| 200 | char **argv;
|
---|
| 201 | {
|
---|
| 202 | if (argc != 2) {
|
---|
[1199] | 203 | out2str(psh, "usage: inputrc file\n");
|
---|
[626] | 204 | return 1;
|
---|
| 205 | }
|
---|
| 206 | if (el != NULL) {
|
---|
| 207 | if (el_source(el, argv[1])) {
|
---|
[1199] | 208 | out2str(psh, "inputrc: failed\n");
|
---|
[626] | 209 | return 1;
|
---|
| 210 | } else
|
---|
| 211 | return 0;
|
---|
| 212 | } else {
|
---|
[1199] | 213 | out2str(psh, "sh: inputrc ignored, not editing\n");
|
---|
[626] | 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 | */
|
---|
| 222 | int
|
---|
| 223 | histcmd(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)
|
---|
[1199] | 255 | error(psh, "history not active");
|
---|
[626] | 256 |
|
---|
| 257 | if (argc == 1)
|
---|
[1199] | 258 | error(psh, "missing history argument");
|
---|
[626] | 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 ':':
|
---|
[1199] | 280 | error(psh, "option -%c expects argument", optopt);
|
---|
[626] | 281 | /* NOTREACHED */
|
---|
| 282 | case '?':
|
---|
| 283 | default:
|
---|
[1199] | 284 | error(psh, "unknown option: -%c", optopt);
|
---|
[626] | 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;
|
---|
[1199] | 311 | error(psh, "called recursively too many times");
|
---|
[626] | 312 | }
|
---|
| 313 | /*
|
---|
| 314 | * Set editor.
|
---|
| 315 | */
|
---|
| 316 | if (sflg == 0) {
|
---|
| 317 | if (editor == NULL &&
|
---|
[1202] | 318 | (editor = bltinlookup(psh, "FCEDIT", 1)) == NULL &&
|
---|
| 319 | (editor = bltinlookup(psh, "EDITOR", 1)) == NULL)
|
---|
[626] | 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:
|
---|
[1199] | 354 | error(psh, "too many args");
|
---|
[626] | 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)
|
---|
[1199] | 383 | error(psh, "can't create temporary file %s", editfile);
|
---|
[626] | 384 | if ((efp = fdopen(fd, "w")) == NULL) {
|
---|
[1207] | 385 | shfile_close(&psh->fdtab, fd);
|
---|
[1199] | 386 | error(psh, "can't allocate stdio buffer for temp");
|
---|
[626] | 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)
|
---|
[1199] | 403 | out1fmt(psh, "%5d ", he.num);
|
---|
| 404 | out1str(psh, he.str);
|
---|
[626] | 405 | } else {
|
---|
| 406 | const char *s = pat ?
|
---|
| 407 | fc_replace(he.str, pat, repl) : he.str;
|
---|
| 408 |
|
---|
| 409 | if (sflg) {
|
---|
| 410 | if (displayhist) {
|
---|
[1199] | 411 | out2str(psh, s);
|
---|
[626] | 412 | }
|
---|
| 413 |
|
---|
[1200] | 414 | evalstring(psh, strcpy(stalloc(psh, strlen(s) + 1), s), 0);
|
---|
[626] | 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);
|
---|
[1198] | 436 | editcmd = stalloc(psh, strlen(editor) + strlen(editfile) + 2);
|
---|
[626] | 437 | sprintf(editcmd, "%s %s", editor, editfile);
|
---|
[1200] | 438 | evalstring(psh, editcmd, 0); /* XXX - should use no JC command */
|
---|
[626] | 439 | INTON;
|
---|
[1202] | 440 | readcmdfile(psh, editfile); /* XXX - should read back - quick tst */
|
---|
[626] | 441 | unlink(editfile);
|
---|
| 442 | }
|
---|
| 443 |
|
---|
| 444 | if (lflg == 0 && active > 0)
|
---|
| 445 | --active;
|
---|
| 446 | if (displayhist)
|
---|
| 447 | displayhist = 0;
|
---|
| 448 | return 0;
|
---|
| 449 | }
|
---|
| 450 |
|
---|
| 451 | STATIC const char *
|
---|
| 452 | fc_replace(const char *s, char *p, char *r)
|
---|
| 453 | {
|
---|
| 454 | char *dest;
|
---|
| 455 | int plen = strlen(p);
|
---|
| 456 |
|
---|
[1198] | 457 | STARTSTACKSTR(psh, dest);
|
---|
[626] | 458 | while (*s) {
|
---|
| 459 | if (*s == *p && strncmp(s, p, plen) == 0) {
|
---|
| 460 | while (*r)
|
---|
[1198] | 461 | STPUTC(psh, *r++, dest);
|
---|
[626] | 462 | s += plen;
|
---|
| 463 | *p = '\0'; /* so no more matches */
|
---|
| 464 | } else
|
---|
[1198] | 465 | STPUTC(psh, *s++, dest);
|
---|
[626] | 466 | }
|
---|
[1198] | 467 | STACKSTRNUL(psh, dest);
|
---|
| 468 | dest = grabstackstr(psh, dest);
|
---|
[626] | 469 |
|
---|
| 470 | return (dest);
|
---|
| 471 | }
|
---|
| 472 |
|
---|
| 473 | int
|
---|
| 474 | not_fcnumber(char *s)
|
---|
| 475 | {
|
---|
| 476 | if (s == NULL)
|
---|
| 477 | return 0;
|
---|
| 478 | if (*s == '-')
|
---|
| 479 | s++;
|
---|
| 480 | return (!is_number(s));
|
---|
| 481 | }
|
---|
| 482 |
|
---|
| 483 | int
|
---|
| 484 | str_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)
|
---|
[1199] | 519 | error(psh, "history number %s not found (internal error)",
|
---|
[626] | 520 | str);
|
---|
| 521 | } else {
|
---|
| 522 | /*
|
---|
| 523 | * pattern
|
---|
| 524 | */
|
---|
| 525 | retval = history(hist, &he, H_PREV_STR, str);
|
---|
| 526 | if (retval == -1)
|
---|
[1199] | 527 | error(psh, "history pattern not found: %s", str);
|
---|
[626] | 528 | }
|
---|
| 529 | return (he.num);
|
---|
| 530 | }
|
---|
[1214] | 531 | #else /* SMALL */
|
---|
[626] | 532 | int
|
---|
[1198] | 533 | histcmd(shinstance *psh, int argc, char **argv)
|
---|
[626] | 534 | {
|
---|
[1198] | 535 | error(psh, "not compiled with history support");
|
---|
[626] | 536 | /* NOTREACHED */
|
---|
[1198] | 537 | return -1;
|
---|
[626] | 538 | }
|
---|
| 539 | int
|
---|
[1198] | 540 | inputrc(shinstance *psh, int argc, char **argv)
|
---|
[626] | 541 | {
|
---|
[1198] | 542 | error(psh, "not compiled with history support");
|
---|
[626] | 543 | /* NOTREACHED */
|
---|
[1198] | 544 | return -1;
|
---|
[626] | 545 | }
|
---|
[1214] | 546 | #endif /* SMALL */
|
---|