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