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 #if 0 39 static char sccsid[] = "@(#)histedit.c 8.2 (Berkeley) 5/4/95"; 40 #endif 41 #endif /* not lint */ 42 #include <sys/cdefs.h> 43 __FBSDID("$FreeBSD$"); 44 45 #include <sys/param.h> 46 #include <limits.h> 47 #include <paths.h> 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <unistd.h> 51 /* 52 * Editline and history functions (and glue). 53 */ 54 #include "shell.h" 55 #include "parser.h" 56 #include "var.h" 57 #include "options.h" 58 #include "main.h" 59 #include "output.h" 60 #include "mystring.h" 61 #ifndef NO_HISTORY 62 #include "myhistedit.h" 63 #include "error.h" 64 #include "eval.h" 65 #include "memalloc.h" 66 67 #define MAXHISTLOOPS 4 /* max recursions through fc */ 68 #define DEFEDITOR "ed" /* default editor *should* be $EDITOR */ 69 70 History *hist; /* history cookie */ 71 EditLine *el; /* editline cookie */ 72 int displayhist; 73 static FILE *el_in, *el_out, *el_err; 74 75 STATIC char *fc_replace(const char *, char *, char *); 76 77 /* 78 * Set history and editing status. Called whenever the status may 79 * have changed (figures out what to do). 80 */ 81 void 82 histedit(void) 83 { 84 85 #define editing (Eflag || Vflag) 86 87 if (iflag) { 88 if (!hist) { 89 /* 90 * turn history on 91 */ 92 INTOFF; 93 hist = history_init(); 94 INTON; 95 96 if (hist != NULL) 97 sethistsize(histsizeval()); 98 else 99 out2str("sh: can't initialize history\n"); 100 } 101 if (editing && !el && isatty(0)) { /* && isatty(2) ??? */ 102 /* 103 * turn editing on 104 */ 105 INTOFF; 106 if (el_in == NULL) 107 el_in = fdopen(0, "r"); 108 if (el_err == NULL) 109 el_err = fdopen(1, "w"); 110 if (el_out == NULL) 111 el_out = fdopen(2, "w"); 112 if (el_in == NULL || el_err == NULL || el_out == NULL) 113 goto bad; 114 el = el_init(arg0, el_in, el_out, el_err); 115 if (el != NULL) { 116 if (hist) 117 el_set(el, EL_HIST, history, hist); 118 el_set(el, EL_PROMPT, getprompt); 119 } else { 120 bad: 121 out2str("sh: can't initialize editing\n"); 122 } 123 INTON; 124 } else if (!editing && el) { 125 INTOFF; 126 el_end(el); 127 el = NULL; 128 INTON; 129 } 130 if (el) { 131 if (Vflag) 132 el_set(el, EL_EDITOR, "vi"); 133 else if (Eflag) 134 el_set(el, EL_EDITOR, "emacs"); 135 el_source(el, NULL); 136 } 137 } else { 138 INTOFF; 139 if (el) { /* no editing if not interactive */ 140 el_end(el); 141 el = NULL; 142 } 143 if (hist) { 144 history_end(hist); 145 hist = NULL; 146 } 147 INTON; 148 } 149 } 150 151 152 void 153 sethistsize(hs) 154 const char *hs; 155 { 156 int histsize; 157 HistEvent he; 158 159 if (hist != NULL) { 160 if (hs == NULL || *hs == '\0' || 161 (histsize = atoi(hs)) < 0) 162 histsize = 100; 163 history(hist, &he, H_EVENT, histsize); 164 } 165 } 166 167 int 168 histcmd(int argc, char **argv) 169 { 170 int ch; 171 char *editor = NULL; 172 HistEvent he; 173 int lflg = 0, nflg = 0, rflg = 0, sflg = 0; 174 int i, retval; 175 char *firststr, *laststr; 176 int first, last, direction; 177 char *pat = NULL, *repl; 178 static int active = 0; 179 struct jmploc jmploc; 180 struct jmploc *volatile savehandler; 181 char editfile[PATH_MAX]; 182 FILE *efp; 183 int oldhistnum; 184 #ifdef __GNUC__ 185 /* Avoid longjmp clobbering */ 186 (void) &editor; 187 (void) &lflg; 188 (void) &nflg; 189 (void) &rflg; 190 (void) &sflg; 191 (void) &firststr; 192 (void) &laststr; 193 (void) &pat; 194 (void) &repl; 195 (void) &efp; 196 (void) &argc; 197 (void) &argv; 198 #endif 199 200 if (hist == NULL) 201 error("history not active"); 202 203 if (argc == 1) 204 error("missing history argument"); 205 206 optreset = 1; optind = 1; /* initialize getopt */ 207 opterr = 0; 208 while (not_fcnumber(argv[optind]) && 209 (ch = getopt(argc, argv, ":e:lnrs")) != -1) 210 switch ((char)ch) { 211 case 'e': 212 editor = optarg; 213 break; 214 case 'l': 215 lflg = 1; 216 break; 217 case 'n': 218 nflg = 1; 219 break; 220 case 'r': 221 rflg = 1; 222 break; 223 case 's': 224 sflg = 1; 225 break; 226 case ':': 227 error("option -%c expects argument", optopt); 228 case '?': 229 default: 230 error("unknown option: -%c", optopt); 231 } 232 argc -= optind, argv += optind; 233 234 /* 235 * If executing... 236 */ 237 if (lflg == 0 || editor || sflg) { 238 lflg = 0; /* ignore */ 239 editfile[0] = '\0'; 240 /* 241 * Catch interrupts to reset active counter and 242 * cleanup temp files. 243 */ 244 if (setjmp(jmploc.loc)) { 245 active = 0; 246 if (*editfile) 247 unlink(editfile); 248 handler = savehandler; 249 longjmp(handler->loc, 1); 250 } 251 savehandler = handler; 252 handler = &jmploc; 253 if (++active > MAXHISTLOOPS) { 254 active = 0; 255 displayhist = 0; 256 error("called recursively too many times"); 257 } 258 /* 259 * Set editor. 260 */ 261 if (sflg == 0) { 262 if (editor == NULL && 263 (editor = bltinlookup("FCEDIT", 1)) == NULL && 264 (editor = bltinlookup("EDITOR", 1)) == NULL) 265 editor = DEFEDITOR; 266 if (editor[0] == '-' && editor[1] == '\0') { 267 sflg = 1; /* no edit */ 268 editor = NULL; 269 } 270 } 271 } 272 273 /* 274 * If executing, parse [old=new] now 275 */ 276 if (lflg == 0 && argc > 0 && 277 ((repl = strchr(argv[0], '=')) != NULL)) { 278 pat = argv[0]; 279 *repl++ = '\0'; 280 argc--, argv++; 281 } 282 /* 283 * determine [first] and [last] 284 */ 285 switch (argc) { 286 case 0: 287 firststr = lflg ? "-16" : "-1"; 288 laststr = "-1"; 289 break; 290 case 1: 291 firststr = argv[0]; 292 laststr = lflg ? "-1" : argv[0]; 293 break; 294 case 2: 295 firststr = argv[0]; 296 laststr = argv[1]; 297 break; 298 default: 299 error("too many args"); 300 } 301 /* 302 * Turn into event numbers. 303 */ 304 first = str_to_event(firststr, 0); 305 last = str_to_event(laststr, 1); 306 307 if (rflg) { 308 i = last; 309 last = first; 310 first = i; 311 } 312 /* 313 * XXX - this should not depend on the event numbers 314 * always increasing. Add sequence numbers or offset 315 * to the history element in next (diskbased) release. 316 */ 317 direction = first < last ? H_PREV : H_NEXT; 318 319 /* 320 * If editing, grab a temp file. 321 */ 322 if (editor) { 323 int fd; 324 INTOFF; /* easier */ 325 sprintf(editfile, "%s/_shXXXXXX", _PATH_TMP); 326 if ((fd = mkstemp(editfile)) < 0) 327 error("can't create temporary file %s", editfile); 328 if ((efp = fdopen(fd, "w")) == NULL) { 329 close(fd); 330 error("can't allocate stdio buffer for temp"); 331 } 332 } 333 334 /* 335 * Loop through selected history events. If listing or executing, 336 * do it now. Otherwise, put into temp file and call the editor 337 * after. 338 * 339 * The history interface needs rethinking, as the following 340 * convolutions will demonstrate. 341 */ 342 history(hist, &he, H_FIRST); 343 retval = history(hist, &he, H_NEXT_EVENT, first); 344 for (;retval != -1; retval = history(hist, &he, direction)) { 345 if (lflg) { 346 if (!nflg) 347 out1fmt("%5d ", he.num); 348 out1str(he.str); 349 } else { 350 char *s = pat ? 351 fc_replace(he.str, pat, repl) : (char *)he.str; 352 353 if (sflg) { 354 if (displayhist) { 355 out2str(s); 356 } 357 evalstring(s); 358 if (displayhist && hist) { 359 /* 360 * XXX what about recursive and 361 * relative histnums. 362 */ 363 oldhistnum = he.num; 364 history(hist, &he, H_ENTER, s); 365 /* 366 * XXX H_ENTER moves the internal 367 * cursor, set it back to the current 368 * entry. 369 */ 370 retval = history(hist, &he, 371 H_NEXT_EVENT, oldhistnum); 372 } 373 } else 374 fputs(s, efp); 375 } 376 /* 377 * At end? (if we were to loose last, we'd sure be 378 * messed up). 379 */ 380 if (he.num == last) 381 break; 382 } 383 if (editor) { 384 char *editcmd; 385 386 fclose(efp); 387 editcmd = stalloc(strlen(editor) + strlen(editfile) + 2); 388 sprintf(editcmd, "%s %s", editor, editfile); 389 evalstring(editcmd); /* XXX - should use no JC command */ 390 INTON; 391 readcmdfile(editfile); /* XXX - should read back - quick tst */ 392 unlink(editfile); 393 } 394 395 if (lflg == 0 && active > 0) 396 --active; 397 if (displayhist) 398 displayhist = 0; 399 return 0; 400 } 401 402 STATIC char * 403 fc_replace(const char *s, char *p, char *r) 404 { 405 char *dest; 406 int plen = strlen(p); 407 408 STARTSTACKSTR(dest); 409 while (*s) { 410 if (*s == *p && strncmp(s, p, plen) == 0) { 411 while (*r) 412 STPUTC(*r++, dest); 413 s += plen; 414 *p = '\0'; /* so no more matches */ 415 } else 416 STPUTC(*s++, dest); 417 } 418 STACKSTRNUL(dest); 419 dest = grabstackstr(dest); 420 421 return (dest); 422 } 423 424 int 425 not_fcnumber(char *s) 426 { 427 if (s == NULL) 428 return (0); 429 if (*s == '-') 430 s++; 431 return (!is_number(s)); 432 } 433 434 int 435 str_to_event(char *str, int last) 436 { 437 HistEvent he; 438 char *s = str; 439 int relative = 0; 440 int i, retval; 441 442 retval = history(hist, &he, H_FIRST); 443 switch (*s) { 444 case '-': 445 relative = 1; 446 /*FALLTHROUGH*/ 447 case '+': 448 s++; 449 } 450 if (is_number(s)) { 451 i = atoi(s); 452 if (relative) { 453 while (retval != -1 && i--) { 454 retval = history(hist, &he, H_NEXT); 455 } 456 if (retval == -1) 457 retval = history(hist, &he, H_LAST); 458 } else { 459 retval = history(hist, &he, H_NEXT_EVENT, i); 460 if (retval == -1) { 461 /* 462 * the notion of first and last is 463 * backwards to that of the history package 464 */ 465 retval = history(hist, &he, last ? H_FIRST : H_LAST); 466 } 467 } 468 if (retval == -1) 469 error("history number %s not found (internal error)", 470 str); 471 } else { 472 /* 473 * pattern 474 */ 475 retval = history(hist, &he, H_PREV_STR, str); 476 if (retval == -1) 477 error("history pattern not found: %s", str); 478 } 479 return (he.num); 480 } 481 482 int 483 bindcmd(int argc, char **argv) 484 { 485 486 if (el == NULL) 487 error("line editing is disabled"); 488 return (el_parse(el, argc, argv)); 489 } 490 491 #else 492 #include "error.h" 493 494 int 495 histcmd(int argc, char **argv) 496 { 497 498 error("not compiled with history support"); 499 /*NOTREACHED*/ 500 return (0); 501 } 502 503 int 504 bindcmd(int argc, char **argv) 505 { 506 507 error("not compiled with line editing support"); 508 return (0); 509 } 510 #endif 511