14b88c807SRodney W. Grimes /*- 24b88c807SRodney W. Grimes * Copyright (c) 1993 34b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved. 44b88c807SRodney W. Grimes * 54b88c807SRodney W. Grimes * This code is derived from software contributed to Berkeley by 64b88c807SRodney W. Grimes * Kenneth Almquist. 74b88c807SRodney W. Grimes * 84b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 94b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions 104b88c807SRodney W. Grimes * are met: 114b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 124b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 134b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 144b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 154b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution. 164b88c807SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 174b88c807SRodney W. Grimes * must display the following acknowledgement: 184b88c807SRodney W. Grimes * This product includes software developed by the University of 194b88c807SRodney W. Grimes * California, Berkeley and its contributors. 204b88c807SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 214b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software 224b88c807SRodney W. Grimes * without specific prior written permission. 234b88c807SRodney W. Grimes * 244b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 254b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 264b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 274b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 284b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 294b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 304b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 314b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 324b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 334b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 344b88c807SRodney W. Grimes * SUCH DAMAGE. 354b88c807SRodney W. Grimes */ 364b88c807SRodney W. Grimes 374b88c807SRodney W. Grimes #ifndef lint 383d7b5b93SPhilippe Charnier #if 0 393d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)histedit.c 8.2 (Berkeley) 5/4/95"; 403d7b5b93SPhilippe Charnier #endif 413d7b5b93SPhilippe Charnier static const char rcsid[] = 422a456239SPeter Wemm "$FreeBSD$"; 434b88c807SRodney W. Grimes #endif /* not lint */ 444b88c807SRodney W. Grimes 454b88c807SRodney W. Grimes #include <sys/param.h> 4692e331afSWarner Losh #include <limits.h> 474b88c807SRodney W. Grimes #include <paths.h> 484b88c807SRodney W. Grimes #include <stdio.h> 49aa9caaf6SPeter Wemm #include <stdlib.h> 50aa9caaf6SPeter Wemm #include <unistd.h> 51aa9caaf6SPeter Wemm /* 52aa9caaf6SPeter Wemm * Editline and history functions (and glue). 53aa9caaf6SPeter Wemm */ 544b88c807SRodney W. Grimes #include "shell.h" 554b88c807SRodney W. Grimes #include "parser.h" 564b88c807SRodney W. Grimes #include "var.h" 574b88c807SRodney W. Grimes #include "options.h" 58aa9caaf6SPeter Wemm #include "main.h" 59aa9caaf6SPeter Wemm #include "output.h" 604b88c807SRodney W. Grimes #include "mystring.h" 61aa9caaf6SPeter Wemm #ifndef NO_HISTORY 62aa9caaf6SPeter Wemm #include "myhistedit.h" 634b88c807SRodney W. Grimes #include "error.h" 64aa9caaf6SPeter Wemm #include "eval.h" 654b88c807SRodney W. Grimes #include "memalloc.h" 664b88c807SRodney W. Grimes 674b88c807SRodney W. Grimes #define MAXHISTLOOPS 4 /* max recursions through fc */ 684b88c807SRodney W. Grimes #define DEFEDITOR "ed" /* default editor *should* be $EDITOR */ 694b88c807SRodney W. Grimes 704b88c807SRodney W. Grimes History *hist; /* history cookie */ 714b88c807SRodney W. Grimes EditLine *el; /* editline cookie */ 724b88c807SRodney W. Grimes int displayhist; 734b88c807SRodney W. Grimes static FILE *el_in, *el_out; 744b88c807SRodney W. Grimes 754b88c807SRodney W. Grimes STATIC char *fc_replace __P((const char *, char *, char *)); 764b88c807SRodney W. Grimes 774b88c807SRodney W. Grimes /* 784b88c807SRodney W. Grimes * Set history and editing status. Called whenever the status may 794b88c807SRodney W. Grimes * have changed (figures out what to do). 804b88c807SRodney W. Grimes */ 81aa9caaf6SPeter Wemm void 82aa9caaf6SPeter Wemm histedit() 83aa9caaf6SPeter Wemm { 844b88c807SRodney W. Grimes 854b88c807SRodney W. Grimes #define editing (Eflag || Vflag) 864b88c807SRodney W. Grimes 874b88c807SRodney W. Grimes if (iflag) { 884b88c807SRodney W. Grimes if (!hist) { 894b88c807SRodney W. Grimes /* 904b88c807SRodney W. Grimes * turn history on 914b88c807SRodney W. Grimes */ 924b88c807SRodney W. Grimes INTOFF; 934b88c807SRodney W. Grimes hist = history_init(); 944b88c807SRodney W. Grimes INTON; 954b88c807SRodney W. Grimes 964b88c807SRodney W. Grimes if (hist != NULL) 97ab0a2172SSteve Price sethistsize(histsizeval()); 984b88c807SRodney W. Grimes else 994b88c807SRodney W. Grimes out2str("sh: can't initialize history\n"); 1004b88c807SRodney W. Grimes } 1014b88c807SRodney W. Grimes if (editing && !el && isatty(0)) { /* && isatty(2) ??? */ 1024b88c807SRodney W. Grimes /* 1034b88c807SRodney W. Grimes * turn editing on 1044b88c807SRodney W. Grimes */ 1054b88c807SRodney W. Grimes INTOFF; 1064b88c807SRodney W. Grimes if (el_in == NULL) 1074b88c807SRodney W. Grimes el_in = fdopen(0, "r"); 1084b88c807SRodney W. Grimes if (el_out == NULL) 1094b88c807SRodney W. Grimes el_out = fdopen(2, "w"); 1104b88c807SRodney W. Grimes if (el_in == NULL || el_out == NULL) 1114b88c807SRodney W. Grimes goto bad; 1124b88c807SRodney W. Grimes el = el_init(arg0, el_in, el_out); 1134b88c807SRodney W. Grimes if (el != NULL) { 1144b88c807SRodney W. Grimes if (hist) 1154b88c807SRodney W. Grimes el_set(el, EL_HIST, history, hist); 1164b88c807SRodney W. Grimes el_set(el, EL_PROMPT, getprompt); 1174b88c807SRodney W. Grimes } else { 1184b88c807SRodney W. Grimes bad: 1194b88c807SRodney W. Grimes out2str("sh: can't initialize editing\n"); 1204b88c807SRodney W. Grimes } 1214b88c807SRodney W. Grimes INTON; 1224b88c807SRodney W. Grimes } else if (!editing && el) { 1234b88c807SRodney W. Grimes INTOFF; 1244b88c807SRodney W. Grimes el_end(el); 1254b88c807SRodney W. Grimes el = NULL; 1264b88c807SRodney W. Grimes INTON; 1274b88c807SRodney W. Grimes } 1284b88c807SRodney W. Grimes if (el) { 1294b88c807SRodney W. Grimes if (Vflag) 1304b88c807SRodney W. Grimes el_set(el, EL_EDITOR, "vi"); 1314b88c807SRodney W. Grimes else if (Eflag) 1324b88c807SRodney W. Grimes el_set(el, EL_EDITOR, "emacs"); 1334b88c807SRodney W. Grimes } 1344b88c807SRodney W. Grimes } else { 1354b88c807SRodney W. Grimes INTOFF; 1364b88c807SRodney W. Grimes if (el) { /* no editing if not interactive */ 1374b88c807SRodney W. Grimes el_end(el); 1384b88c807SRodney W. Grimes el = NULL; 1394b88c807SRodney W. Grimes } 1404b88c807SRodney W. Grimes if (hist) { 1414b88c807SRodney W. Grimes history_end(hist); 1424b88c807SRodney W. Grimes hist = NULL; 1434b88c807SRodney W. Grimes } 1444b88c807SRodney W. Grimes INTON; 1454b88c807SRodney W. Grimes } 1464b88c807SRodney W. Grimes } 1474b88c807SRodney W. Grimes 148aa9caaf6SPeter Wemm 149aa9caaf6SPeter Wemm void 150ab0a2172SSteve Price sethistsize(hs) 151ab0a2172SSteve Price const char *hs; 152aa9caaf6SPeter Wemm { 1534b88c807SRodney W. Grimes int histsize; 1544b88c807SRodney W. Grimes 1554b88c807SRodney W. Grimes if (hist != NULL) { 156ab0a2172SSteve Price if (hs == NULL || *hs == '\0' || 157ab0a2172SSteve Price (histsize = atoi(hs)) < 0) 1584b88c807SRodney W. Grimes histsize = 100; 1594b88c807SRodney W. Grimes history(hist, H_EVENT, histsize); 1604b88c807SRodney W. Grimes } 1614b88c807SRodney W. Grimes } 1624b88c807SRodney W. Grimes 1634b88c807SRodney W. Grimes /* 1644b88c807SRodney W. Grimes * This command is provided since POSIX decided to standardize 1654b88c807SRodney W. Grimes * the Korn shell fc command. Oh well... 1664b88c807SRodney W. Grimes */ 167aa9caaf6SPeter Wemm int 1684b88c807SRodney W. Grimes histcmd(argc, argv) 169aa9caaf6SPeter Wemm int argc; 170aa9caaf6SPeter Wemm char **argv; 1714b88c807SRodney W. Grimes { 1724b88c807SRodney W. Grimes int ch; 1734b88c807SRodney W. Grimes char *editor = NULL; 1744b88c807SRodney W. Grimes const HistEvent *he; 1754b88c807SRodney W. Grimes int lflg = 0, nflg = 0, rflg = 0, sflg = 0; 1764b88c807SRodney W. Grimes int i; 1774b88c807SRodney W. Grimes char *firststr, *laststr; 1784b88c807SRodney W. Grimes int first, last, direction; 1794b88c807SRodney W. Grimes char *pat = NULL, *repl; /* ksh "fc old=new" crap */ 1804b88c807SRodney W. Grimes static int active = 0; 1814b88c807SRodney W. Grimes struct jmploc jmploc; 1824b88c807SRodney W. Grimes struct jmploc *volatile savehandler; 18392e331afSWarner Losh char editfile[PATH_MAX]; 1844b88c807SRodney W. Grimes FILE *efp; 185aa9caaf6SPeter Wemm #ifdef __GNUC__ 186aa9caaf6SPeter Wemm /* Avoid longjmp clobbering */ 187aa9caaf6SPeter Wemm (void) &editor; 188aa9caaf6SPeter Wemm (void) &lflg; 189aa9caaf6SPeter Wemm (void) &nflg; 190aa9caaf6SPeter Wemm (void) &rflg; 191aa9caaf6SPeter Wemm (void) &sflg; 192aa9caaf6SPeter Wemm (void) &firststr; 193aa9caaf6SPeter Wemm (void) &laststr; 194aa9caaf6SPeter Wemm (void) &pat; 195aa9caaf6SPeter Wemm (void) &repl; 196aa9caaf6SPeter Wemm (void) &efp; 197aa9caaf6SPeter Wemm (void) &argc; 198aa9caaf6SPeter Wemm (void) &argv; 199aa9caaf6SPeter Wemm #endif 2004b88c807SRodney W. Grimes 2014b88c807SRodney W. Grimes if (hist == NULL) 2024b88c807SRodney W. Grimes error("history not active"); 2034b88c807SRodney W. Grimes 2044b88c807SRodney W. Grimes if (argc == 1) 2054b88c807SRodney W. Grimes error("missing history argument"); 2064b88c807SRodney W. Grimes 2074b88c807SRodney W. Grimes optreset = 1; optind = 1; /* initialize getopt */ 2084b88c807SRodney W. Grimes while (not_fcnumber(argv[optind]) && 20993ef08afSWarner Losh (ch = getopt(argc, argv, ":e:lnrs")) != -1) 2104b88c807SRodney W. Grimes switch ((char)ch) { 2114b88c807SRodney W. Grimes case 'e': 212f01e3d0cSMartin Cracauer editor = shoptarg; 2134b88c807SRodney W. Grimes break; 2144b88c807SRodney W. Grimes case 'l': 2154b88c807SRodney W. Grimes lflg = 1; 2164b88c807SRodney W. Grimes break; 2174b88c807SRodney W. Grimes case 'n': 2184b88c807SRodney W. Grimes nflg = 1; 2194b88c807SRodney W. Grimes break; 2204b88c807SRodney W. Grimes case 'r': 2214b88c807SRodney W. Grimes rflg = 1; 2224b88c807SRodney W. Grimes break; 2234b88c807SRodney W. Grimes case 's': 2244b88c807SRodney W. Grimes sflg = 1; 2254b88c807SRodney W. Grimes break; 2264b88c807SRodney W. Grimes case ':': 2274b88c807SRodney W. Grimes error("option -%c expects argument", optopt); 2284b88c807SRodney W. Grimes case '?': 2294b88c807SRodney W. Grimes default: 2304b88c807SRodney W. Grimes error("unknown option: -%c", optopt); 2314b88c807SRodney W. Grimes } 2324b88c807SRodney W. Grimes argc -= optind, argv += optind; 2334b88c807SRodney W. Grimes 2344b88c807SRodney W. Grimes /* 2354b88c807SRodney W. Grimes * If executing... 2364b88c807SRodney W. Grimes */ 2374b88c807SRodney W. Grimes if (lflg == 0 || editor || sflg) { 2384b88c807SRodney W. Grimes lflg = 0; /* ignore */ 2394b88c807SRodney W. Grimes editfile[0] = '\0'; 2404b88c807SRodney W. Grimes /* 2414b88c807SRodney W. Grimes * Catch interrupts to reset active counter and 2424b88c807SRodney W. Grimes * cleanup temp files. 2434b88c807SRodney W. Grimes */ 2444b88c807SRodney W. Grimes if (setjmp(jmploc.loc)) { 2454b88c807SRodney W. Grimes active = 0; 2464b88c807SRodney W. Grimes if (*editfile) 2474b88c807SRodney W. Grimes unlink(editfile); 2484b88c807SRodney W. Grimes handler = savehandler; 2494b88c807SRodney W. Grimes longjmp(handler->loc, 1); 2504b88c807SRodney W. Grimes } 2514b88c807SRodney W. Grimes savehandler = handler; 2524b88c807SRodney W. Grimes handler = &jmploc; 2534b88c807SRodney W. Grimes if (++active > MAXHISTLOOPS) { 2544b88c807SRodney W. Grimes active = 0; 2554b88c807SRodney W. Grimes displayhist = 0; 2564b88c807SRodney W. Grimes error("called recursively too many times"); 2574b88c807SRodney W. Grimes } 2584b88c807SRodney W. Grimes /* 2594b88c807SRodney W. Grimes * Set editor. 2604b88c807SRodney W. Grimes */ 2614b88c807SRodney W. Grimes if (sflg == 0) { 2624b88c807SRodney W. Grimes if (editor == NULL && 2634b88c807SRodney W. Grimes (editor = bltinlookup("FCEDIT", 1)) == NULL && 2644b88c807SRodney W. Grimes (editor = bltinlookup("EDITOR", 1)) == NULL) 2654b88c807SRodney W. Grimes editor = DEFEDITOR; 2664b88c807SRodney W. Grimes if (editor[0] == '-' && editor[1] == '\0') { 2674b88c807SRodney W. Grimes sflg = 1; /* no edit */ 2684b88c807SRodney W. Grimes editor = NULL; 2694b88c807SRodney W. Grimes } 2704b88c807SRodney W. Grimes } 2714b88c807SRodney W. Grimes } 2724b88c807SRodney W. Grimes 2734b88c807SRodney W. Grimes /* 2744b88c807SRodney W. Grimes * If executing, parse [old=new] now 2754b88c807SRodney W. Grimes */ 2764b88c807SRodney W. Grimes if (lflg == 0 && argc > 0 && 2774b88c807SRodney W. Grimes ((repl = strchr(argv[0], '=')) != NULL)) { 2784b88c807SRodney W. Grimes pat = argv[0]; 2794b88c807SRodney W. Grimes *repl++ = '\0'; 2804b88c807SRodney W. Grimes argc--, argv++; 2814b88c807SRodney W. Grimes } 2824b88c807SRodney W. Grimes /* 2834b88c807SRodney W. Grimes * determine [first] and [last] 2844b88c807SRodney W. Grimes */ 2854b88c807SRodney W. Grimes switch (argc) { 2864b88c807SRodney W. Grimes case 0: 2874b88c807SRodney W. Grimes firststr = lflg ? "-16" : "-1"; 2884b88c807SRodney W. Grimes laststr = "-1"; 2894b88c807SRodney W. Grimes break; 2904b88c807SRodney W. Grimes case 1: 2914b88c807SRodney W. Grimes firststr = argv[0]; 2924b88c807SRodney W. Grimes laststr = lflg ? "-1" : argv[0]; 2934b88c807SRodney W. Grimes break; 2944b88c807SRodney W. Grimes case 2: 2954b88c807SRodney W. Grimes firststr = argv[0]; 2964b88c807SRodney W. Grimes laststr = argv[1]; 2974b88c807SRodney W. Grimes break; 2984b88c807SRodney W. Grimes default: 2994b88c807SRodney W. Grimes error("too many args"); 3004b88c807SRodney W. Grimes } 3014b88c807SRodney W. Grimes /* 3024b88c807SRodney W. Grimes * Turn into event numbers. 3034b88c807SRodney W. Grimes */ 3044b88c807SRodney W. Grimes first = str_to_event(firststr, 0); 3054b88c807SRodney W. Grimes last = str_to_event(laststr, 1); 3064b88c807SRodney W. Grimes 3074b88c807SRodney W. Grimes if (rflg) { 3084b88c807SRodney W. Grimes i = last; 3094b88c807SRodney W. Grimes last = first; 3104b88c807SRodney W. Grimes first = i; 3114b88c807SRodney W. Grimes } 3124b88c807SRodney W. Grimes /* 3134b88c807SRodney W. Grimes * XXX - this should not depend on the event numbers 3144b88c807SRodney W. Grimes * always increasing. Add sequence numbers or offset 3154b88c807SRodney W. Grimes * to the history element in next (diskbased) release. 3164b88c807SRodney W. Grimes */ 3174b88c807SRodney W. Grimes direction = first < last ? H_PREV : H_NEXT; 3184b88c807SRodney W. Grimes 3194b88c807SRodney W. Grimes /* 3204b88c807SRodney W. Grimes * If editing, grab a temp file. 3214b88c807SRodney W. Grimes */ 3224b88c807SRodney W. Grimes if (editor) { 3234b88c807SRodney W. Grimes int fd; 3244b88c807SRodney W. Grimes INTOFF; /* easier */ 3254b88c807SRodney W. Grimes sprintf(editfile, "%s/_shXXXXXX", _PATH_TMP); 3264b88c807SRodney W. Grimes if ((fd = mkstemp(editfile)) < 0) 3274b88c807SRodney W. Grimes error("can't create temporary file %s", editfile); 3284b88c807SRodney W. Grimes if ((efp = fdopen(fd, "w")) == NULL) { 3294b88c807SRodney W. Grimes close(fd); 33016992ff4SPeter Wemm error("can't allocate stdio buffer for temp"); 3314b88c807SRodney W. Grimes } 3324b88c807SRodney W. Grimes } 3334b88c807SRodney W. Grimes 3344b88c807SRodney W. Grimes /* 3354b88c807SRodney W. Grimes * Loop through selected history events. If listing or executing, 3364b88c807SRodney W. Grimes * do it now. Otherwise, put into temp file and call the editor 3374b88c807SRodney W. Grimes * after. 3384b88c807SRodney W. Grimes * 3394b88c807SRodney W. Grimes * The history interface needs rethinking, as the following 3404b88c807SRodney W. Grimes * convolutions will demonstrate. 3414b88c807SRodney W. Grimes */ 3424b88c807SRodney W. Grimes history(hist, H_FIRST); 3434b88c807SRodney W. Grimes he = history(hist, H_NEXT_EVENT, first); 3444b88c807SRodney W. Grimes for (;he != NULL; he = history(hist, direction)) { 3454b88c807SRodney W. Grimes if (lflg) { 3464b88c807SRodney W. Grimes if (!nflg) 3474b88c807SRodney W. Grimes out1fmt("%5d ", he->num); 3484b88c807SRodney W. Grimes out1str(he->str); 3494b88c807SRodney W. Grimes } else { 3504b88c807SRodney W. Grimes char *s = pat ? 3514b88c807SRodney W. Grimes fc_replace(he->str, pat, repl) : (char *)he->str; 3524b88c807SRodney W. Grimes 3534b88c807SRodney W. Grimes if (sflg) { 3544b88c807SRodney W. Grimes if (displayhist) { 3554b88c807SRodney W. Grimes out2str(s); 3564b88c807SRodney W. Grimes } 3574b88c807SRodney W. Grimes evalstring(s); 3584b88c807SRodney W. Grimes if (displayhist && hist) { 3594b88c807SRodney W. Grimes /* 3604b88c807SRodney W. Grimes * XXX what about recursive and 3614b88c807SRodney W. Grimes * relative histnums. 3624b88c807SRodney W. Grimes */ 3634b88c807SRodney W. Grimes history(hist, H_ENTER, s); 3644b88c807SRodney W. Grimes } 3654b88c807SRodney W. Grimes } else 3664b88c807SRodney W. Grimes fputs(s, efp); 3674b88c807SRodney W. Grimes } 3684b88c807SRodney W. Grimes /* 3694b88c807SRodney W. Grimes * At end? (if we were to loose last, we'd sure be 3704b88c807SRodney W. Grimes * messed up). 3714b88c807SRodney W. Grimes */ 3724b88c807SRodney W. Grimes if (he->num == last) 3734b88c807SRodney W. Grimes break; 3744b88c807SRodney W. Grimes } 3754b88c807SRodney W. Grimes if (editor) { 3764b88c807SRodney W. Grimes char *editcmd; 3774b88c807SRodney W. Grimes 3784b88c807SRodney W. Grimes fclose(efp); 3794b88c807SRodney W. Grimes editcmd = stalloc(strlen(editor) + strlen(editfile) + 2); 3804b88c807SRodney W. Grimes sprintf(editcmd, "%s %s", editor, editfile); 3814b88c807SRodney W. Grimes evalstring(editcmd); /* XXX - should use no JC command */ 3824b88c807SRodney W. Grimes INTON; 3834b88c807SRodney W. Grimes readcmdfile(editfile); /* XXX - should read back - quick tst */ 3844b88c807SRodney W. Grimes unlink(editfile); 3854b88c807SRodney W. Grimes } 3864b88c807SRodney W. Grimes 3874b88c807SRodney W. Grimes if (lflg == 0 && active > 0) 3884b88c807SRodney W. Grimes --active; 3894b88c807SRodney W. Grimes if (displayhist) 3904b88c807SRodney W. Grimes displayhist = 0; 391aa9caaf6SPeter Wemm return 0; 3924b88c807SRodney W. Grimes } 3934b88c807SRodney W. Grimes 3944b88c807SRodney W. Grimes STATIC char * 3954b88c807SRodney W. Grimes fc_replace(s, p, r) 3964b88c807SRodney W. Grimes const char *s; 3974b88c807SRodney W. Grimes char *p, *r; 3984b88c807SRodney W. Grimes { 3994b88c807SRodney W. Grimes char *dest; 4004b88c807SRodney W. Grimes int plen = strlen(p); 4014b88c807SRodney W. Grimes 4024b88c807SRodney W. Grimes STARTSTACKSTR(dest); 4034b88c807SRodney W. Grimes while (*s) { 4044b88c807SRodney W. Grimes if (*s == *p && strncmp(s, p, plen) == 0) { 4054b88c807SRodney W. Grimes while (*r) 4064b88c807SRodney W. Grimes STPUTC(*r++, dest); 4074b88c807SRodney W. Grimes s += plen; 4084b88c807SRodney W. Grimes *p = '\0'; /* so no more matches */ 4094b88c807SRodney W. Grimes } else 4104b88c807SRodney W. Grimes STPUTC(*s++, dest); 4114b88c807SRodney W. Grimes } 4124b88c807SRodney W. Grimes STACKSTRNUL(dest); 4134b88c807SRodney W. Grimes dest = grabstackstr(dest); 4144b88c807SRodney W. Grimes 4154b88c807SRodney W. Grimes return (dest); 4164b88c807SRodney W. Grimes } 4174b88c807SRodney W. Grimes 418aa9caaf6SPeter Wemm int 4194b88c807SRodney W. Grimes not_fcnumber(s) 4204b88c807SRodney W. Grimes char *s; 4214b88c807SRodney W. Grimes { 422ab0a2172SSteve Price if (s == NULL) 42362730a71SSteve Price return (0); 4244b88c807SRodney W. Grimes if (*s == '-') 4254b88c807SRodney W. Grimes s++; 4264b88c807SRodney W. Grimes return (!is_number(s)); 4274b88c807SRodney W. Grimes } 4284b88c807SRodney W. Grimes 429aa9caaf6SPeter Wemm int 4304b88c807SRodney W. Grimes str_to_event(str, last) 4314b88c807SRodney W. Grimes char *str; 4324b88c807SRodney W. Grimes int last; 4334b88c807SRodney W. Grimes { 4344b88c807SRodney W. Grimes const HistEvent *he; 4354b88c807SRodney W. Grimes char *s = str; 4364b88c807SRodney W. Grimes int relative = 0; 437aa9caaf6SPeter Wemm int i; 4384b88c807SRodney W. Grimes 4394b88c807SRodney W. Grimes he = history(hist, H_FIRST); 4404b88c807SRodney W. Grimes switch (*s) { 4414b88c807SRodney W. Grimes case '-': 4424b88c807SRodney W. Grimes relative = 1; 4434b88c807SRodney W. Grimes /*FALLTHROUGH*/ 4444b88c807SRodney W. Grimes case '+': 4454b88c807SRodney W. Grimes s++; 4464b88c807SRodney W. Grimes } 4474b88c807SRodney W. Grimes if (is_number(s)) { 4484b88c807SRodney W. Grimes i = atoi(s); 4494b88c807SRodney W. Grimes if (relative) { 4504b88c807SRodney W. Grimes while (he != NULL && i--) { 4514b88c807SRodney W. Grimes he = history(hist, H_NEXT); 4524b88c807SRodney W. Grimes } 4534b88c807SRodney W. Grimes if (he == NULL) 4544b88c807SRodney W. Grimes he = history(hist, H_LAST); 4554b88c807SRodney W. Grimes } else { 4564b88c807SRodney W. Grimes he = history(hist, H_NEXT_EVENT, i); 4574b88c807SRodney W. Grimes if (he == NULL) { 4584b88c807SRodney W. Grimes /* 4594b88c807SRodney W. Grimes * the notion of first and last is 4604b88c807SRodney W. Grimes * backwards to that of the history package 4614b88c807SRodney W. Grimes */ 4624b88c807SRodney W. Grimes he = history(hist, last ? H_FIRST : H_LAST); 4634b88c807SRodney W. Grimes } 4644b88c807SRodney W. Grimes } 4654b88c807SRodney W. Grimes if (he == NULL) 4664b88c807SRodney W. Grimes error("history number %s not found (internal error)", 4674b88c807SRodney W. Grimes str); 4684b88c807SRodney W. Grimes } else { 4694b88c807SRodney W. Grimes /* 4704b88c807SRodney W. Grimes * pattern 4714b88c807SRodney W. Grimes */ 4724b88c807SRodney W. Grimes he = history(hist, H_PREV_STR, str); 4734b88c807SRodney W. Grimes if (he == NULL) 4744b88c807SRodney W. Grimes error("history pattern not found: %s", str); 4754b88c807SRodney W. Grimes } 4764b88c807SRodney W. Grimes return (he->num); 4774b88c807SRodney W. Grimes } 47862730a71SSteve Price #else 47962730a71SSteve Price #include "error.h" 48062730a71SSteve Price 48162730a71SSteve Price int 48262730a71SSteve Price histcmd(argc, argv) 48362730a71SSteve Price int argc; 48462730a71SSteve Price char **argv; 48562730a71SSteve Price { 48662730a71SSteve Price 48762730a71SSteve Price error("not compiled with history support"); 48862730a71SSteve Price /*NOTREACHED*/ 48962730a71SSteve Price return (0); 49062730a71SSteve Price } 49162730a71SSteve Price #endif 492