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