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. 16fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 174b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software 184b88c807SRodney W. Grimes * without specific prior written permission. 194b88c807SRodney W. Grimes * 204b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 214b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 224b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 234b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 244b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 254b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 264b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 274b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 284b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 294b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 304b88c807SRodney W. Grimes * SUCH DAMAGE. 314b88c807SRodney W. Grimes */ 324b88c807SRodney W. Grimes 334b88c807SRodney W. Grimes #ifndef lint 343d7b5b93SPhilippe Charnier #if 0 353d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)histedit.c 8.2 (Berkeley) 5/4/95"; 363d7b5b93SPhilippe Charnier #endif 374b88c807SRodney W. Grimes #endif /* not lint */ 382749b141SDavid E. O'Brien #include <sys/cdefs.h> 392749b141SDavid E. O'Brien __FBSDID("$FreeBSD$"); 404b88c807SRodney W. Grimes 414b88c807SRodney W. Grimes #include <sys/param.h> 42b315a729SPiotr Pawel Stefaniak #include <sys/stat.h> 43b315a729SPiotr Pawel Stefaniak #include <dirent.h> 441f82fb38SPiotr Pawel Stefaniak #include <errno.h> 45988b1bb0SBaptiste Daroussin #include <fcntl.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" 616c346639SBryan Drewery #include "builtins.h" 62aa9caaf6SPeter Wemm #ifndef NO_HISTORY 63aa9caaf6SPeter Wemm #include "myhistedit.h" 644b88c807SRodney W. Grimes #include "error.h" 65aa9caaf6SPeter Wemm #include "eval.h" 664b88c807SRodney W. Grimes #include "memalloc.h" 674b88c807SRodney W. Grimes 684b88c807SRodney W. Grimes #define MAXHISTLOOPS 4 /* max recursions through fc */ 694b88c807SRodney W. Grimes #define DEFEDITOR "ed" /* default editor *should* be $EDITOR */ 704b88c807SRodney W. Grimes 714b88c807SRodney W. Grimes History *hist; /* history cookie */ 724b88c807SRodney W. Grimes EditLine *el; /* editline cookie */ 734b88c807SRodney W. Grimes int displayhist; 741f82fb38SPiotr Pawel Stefaniak static int savehist; 75f91d2e21SJilles Tjoelker static FILE *el_in, *el_out; 764b88c807SRodney W. Grimes 7788328642SDavid E. O'Brien static char *fc_replace(const char *, char *, char *); 78260fc3f4SJilles Tjoelker static int not_fcnumber(const char *); 79260fc3f4SJilles Tjoelker static int str_to_event(const char *, int); 80b315a729SPiotr Pawel Stefaniak static int comparator(const void *, const void *, void *); 81b315a729SPiotr Pawel Stefaniak static char **sh_matches(const char *, int, int); 82b315a729SPiotr Pawel Stefaniak static unsigned char sh_complete(EditLine *, int); 834b88c807SRodney W. Grimes 84988b1bb0SBaptiste Daroussin static const char * 85988b1bb0SBaptiste Daroussin get_histfile(void) 86988b1bb0SBaptiste Daroussin { 87988b1bb0SBaptiste Daroussin const char *histfile; 88988b1bb0SBaptiste Daroussin 89988b1bb0SBaptiste Daroussin /* don't try to save if the history size is 0 */ 90988b1bb0SBaptiste Daroussin if (hist == NULL || histsizeval() == 0) 91988b1bb0SBaptiste Daroussin return (NULL); 92988b1bb0SBaptiste Daroussin histfile = expandstr("${HISTFILE-${HOME-}/.sh_history}"); 93988b1bb0SBaptiste Daroussin 94988b1bb0SBaptiste Daroussin if (histfile[0] == '\0') 95988b1bb0SBaptiste Daroussin return (NULL); 96988b1bb0SBaptiste Daroussin return (histfile); 97988b1bb0SBaptiste Daroussin } 98988b1bb0SBaptiste Daroussin 99988b1bb0SBaptiste Daroussin void 100988b1bb0SBaptiste Daroussin histsave(void) 101988b1bb0SBaptiste Daroussin { 102988b1bb0SBaptiste Daroussin HistEvent he; 103988b1bb0SBaptiste Daroussin char *histtmpname = NULL; 104988b1bb0SBaptiste Daroussin const char *histfile; 105988b1bb0SBaptiste Daroussin int fd; 106988b1bb0SBaptiste Daroussin FILE *f; 107988b1bb0SBaptiste Daroussin 1081f82fb38SPiotr Pawel Stefaniak if (!savehist || (histfile = get_histfile()) == NULL) 109988b1bb0SBaptiste Daroussin return; 110988b1bb0SBaptiste Daroussin INTOFF; 111988b1bb0SBaptiste Daroussin asprintf(&histtmpname, "%s.XXXXXXXXXX", histfile); 112988b1bb0SBaptiste Daroussin if (histtmpname == NULL) { 113988b1bb0SBaptiste Daroussin INTON; 114988b1bb0SBaptiste Daroussin return; 115988b1bb0SBaptiste Daroussin } 116988b1bb0SBaptiste Daroussin fd = mkstemp(histtmpname); 117988b1bb0SBaptiste Daroussin if (fd == -1 || (f = fdopen(fd, "w")) == NULL) { 118988b1bb0SBaptiste Daroussin free(histtmpname); 119988b1bb0SBaptiste Daroussin INTON; 120988b1bb0SBaptiste Daroussin return; 121988b1bb0SBaptiste Daroussin } 122988b1bb0SBaptiste Daroussin if (history(hist, &he, H_SAVE_FP, f) < 1 || 123988b1bb0SBaptiste Daroussin rename(histtmpname, histfile) == -1) 124988b1bb0SBaptiste Daroussin unlink(histtmpname); 125988b1bb0SBaptiste Daroussin fclose(f); 126988b1bb0SBaptiste Daroussin free(histtmpname); 127988b1bb0SBaptiste Daroussin INTON; 128988b1bb0SBaptiste Daroussin 129988b1bb0SBaptiste Daroussin } 130988b1bb0SBaptiste Daroussin 131988b1bb0SBaptiste Daroussin void 132988b1bb0SBaptiste Daroussin histload(void) 133988b1bb0SBaptiste Daroussin { 134988b1bb0SBaptiste Daroussin const char *histfile; 135988b1bb0SBaptiste Daroussin HistEvent he; 136988b1bb0SBaptiste Daroussin 137988b1bb0SBaptiste Daroussin if ((histfile = get_histfile()) == NULL) 138988b1bb0SBaptiste Daroussin return; 1391f82fb38SPiotr Pawel Stefaniak errno = 0; 1401f82fb38SPiotr Pawel Stefaniak if (history(hist, &he, H_LOAD, histfile) != -1 || errno == ENOENT) 1411f82fb38SPiotr Pawel Stefaniak savehist = 1; 142988b1bb0SBaptiste Daroussin } 143988b1bb0SBaptiste Daroussin 1444b88c807SRodney W. Grimes /* 1454b88c807SRodney W. Grimes * Set history and editing status. Called whenever the status may 1464b88c807SRodney W. Grimes * have changed (figures out what to do). 1474b88c807SRodney W. Grimes */ 148aa9caaf6SPeter Wemm void 1495134c3f7SWarner Losh histedit(void) 150aa9caaf6SPeter Wemm { 1514b88c807SRodney W. Grimes 1524b88c807SRodney W. Grimes #define editing (Eflag || Vflag) 1534b88c807SRodney W. Grimes 1544b88c807SRodney W. Grimes if (iflag) { 1554b88c807SRodney W. Grimes if (!hist) { 1564b88c807SRodney W. Grimes /* 1574b88c807SRodney W. Grimes * turn history on 1584b88c807SRodney W. Grimes */ 1594b88c807SRodney W. Grimes INTOFF; 1604b88c807SRodney W. Grimes hist = history_init(); 1614b88c807SRodney W. Grimes INTON; 1624b88c807SRodney W. Grimes 1634b88c807SRodney W. Grimes if (hist != NULL) 164ab0a2172SSteve Price sethistsize(histsizeval()); 1654b88c807SRodney W. Grimes else 166c6204d4aSJilles Tjoelker out2fmt_flush("sh: can't initialize history\n"); 1674b88c807SRodney W. Grimes } 1684b88c807SRodney W. Grimes if (editing && !el && isatty(0)) { /* && isatty(2) ??? */ 1694b88c807SRodney W. Grimes /* 1704b88c807SRodney W. Grimes * turn editing on 1714b88c807SRodney W. Grimes */ 172580eefdfSJilles Tjoelker char *term; 173580eefdfSJilles Tjoelker 1744b88c807SRodney W. Grimes INTOFF; 1754b88c807SRodney W. Grimes if (el_in == NULL) 1764b88c807SRodney W. Grimes el_in = fdopen(0, "r"); 1774b88c807SRodney W. Grimes if (el_out == NULL) 1784b88c807SRodney W. Grimes el_out = fdopen(2, "w"); 179f91d2e21SJilles Tjoelker if (el_in == NULL || el_out == NULL) 1804b88c807SRodney W. Grimes goto bad; 181580eefdfSJilles Tjoelker term = lookupvar("TERM"); 182580eefdfSJilles Tjoelker if (term) 183580eefdfSJilles Tjoelker setenv("TERM", term, 1); 184580eefdfSJilles Tjoelker else 185580eefdfSJilles Tjoelker unsetenv("TERM"); 186f91d2e21SJilles Tjoelker el = el_init(arg0, el_in, el_out, el_out); 1874b88c807SRodney W. Grimes if (el != NULL) { 1884b88c807SRodney W. Grimes if (hist) 1894b88c807SRodney W. Grimes el_set(el, EL_HIST, history, hist); 1904b88c807SRodney W. Grimes el_set(el, EL_PROMPT, getprompt); 191e46b12b7SJilles Tjoelker el_set(el, EL_ADDFN, "sh-complete", 192e46b12b7SJilles Tjoelker "Filename completion", 193b315a729SPiotr Pawel Stefaniak sh_complete); 1944b88c807SRodney W. Grimes } else { 1954b88c807SRodney W. Grimes bad: 196c6204d4aSJilles Tjoelker out2fmt_flush("sh: can't initialize editing\n"); 1974b88c807SRodney W. Grimes } 1984b88c807SRodney W. Grimes INTON; 1994b88c807SRodney W. Grimes } else if (!editing && el) { 2004b88c807SRodney W. Grimes INTOFF; 2014b88c807SRodney W. Grimes el_end(el); 2024b88c807SRodney W. Grimes el = NULL; 2034b88c807SRodney W. Grimes INTON; 2044b88c807SRodney W. Grimes } 2054b88c807SRodney W. Grimes if (el) { 2064b88c807SRodney W. Grimes if (Vflag) 2074b88c807SRodney W. Grimes el_set(el, EL_EDITOR, "vi"); 208660045fbSBaptiste Daroussin else if (Eflag) { 2094b88c807SRodney W. Grimes el_set(el, EL_EDITOR, "emacs"); 210660045fbSBaptiste Daroussin } 211e46b12b7SJilles Tjoelker el_set(el, EL_BIND, "^I", "sh-complete", NULL); 212ecd807fbSTim J. Robbins el_source(el, NULL); 2134b88c807SRodney W. Grimes } 2144b88c807SRodney W. Grimes } else { 2154b88c807SRodney W. Grimes INTOFF; 2164b88c807SRodney W. Grimes if (el) { /* no editing if not interactive */ 2174b88c807SRodney W. Grimes el_end(el); 2184b88c807SRodney W. Grimes el = NULL; 2194b88c807SRodney W. Grimes } 2204b88c807SRodney W. Grimes if (hist) { 2214b88c807SRodney W. Grimes history_end(hist); 2224b88c807SRodney W. Grimes hist = NULL; 2234b88c807SRodney W. Grimes } 2244b88c807SRodney W. Grimes INTON; 2254b88c807SRodney W. Grimes } 2264b88c807SRodney W. Grimes } 2274b88c807SRodney W. Grimes 228aa9caaf6SPeter Wemm 229aa9caaf6SPeter Wemm void 2302fae4c3dSPhilippe Charnier sethistsize(const char *hs) 231aa9caaf6SPeter Wemm { 2324b88c807SRodney W. Grimes int histsize; 233757eeda0SDavid E. O'Brien HistEvent he; 2344b88c807SRodney W. Grimes 2354b88c807SRodney W. Grimes if (hist != NULL) { 236ef9e6178SJilles Tjoelker if (hs == NULL || !is_number(hs)) 2374b88c807SRodney W. Grimes histsize = 100; 238ef9e6178SJilles Tjoelker else 239ef9e6178SJilles Tjoelker histsize = atoi(hs); 2402110d9c3SStefan Farfeleder history(hist, &he, H_SETSIZE, histsize); 241933803fbSJilles Tjoelker history(hist, &he, H_SETUNIQUE, 1); 2424b88c807SRodney W. Grimes } 2434b88c807SRodney W. Grimes } 2444b88c807SRodney W. Grimes 245580eefdfSJilles Tjoelker void 246580eefdfSJilles Tjoelker setterm(const char *term) 247580eefdfSJilles Tjoelker { 248580eefdfSJilles Tjoelker if (rootshell && el != NULL && term != NULL) 249580eefdfSJilles Tjoelker el_set(el, EL_TERMINAL, term); 250580eefdfSJilles Tjoelker } 251580eefdfSJilles Tjoelker 252aa9caaf6SPeter Wemm int 2537cbda738SJilles Tjoelker histcmd(int argc, char **argv __unused) 2544b88c807SRodney W. Grimes { 2554b88c807SRodney W. Grimes int ch; 256384aedabSJilles Tjoelker const char *editor = NULL; 257757eeda0SDavid E. O'Brien HistEvent he; 2584b88c807SRodney W. Grimes int lflg = 0, nflg = 0, rflg = 0, sflg = 0; 259757eeda0SDavid E. O'Brien int i, retval; 260384aedabSJilles Tjoelker const char *firststr, *laststr; 2614b88c807SRodney W. Grimes int first, last, direction; 262384aedabSJilles Tjoelker char *pat = NULL, *repl = NULL; 2634b88c807SRodney W. Grimes static int active = 0; 2644b88c807SRodney W. Grimes struct jmploc jmploc; 265224fbf9fSJilles Tjoelker struct jmploc *savehandler; 266224fbf9fSJilles Tjoelker char editfilestr[PATH_MAX]; 267224fbf9fSJilles Tjoelker char *volatile editfile; 268384aedabSJilles Tjoelker FILE *efp = NULL; 26932c07786STim J. Robbins int oldhistnum; 2704b88c807SRodney W. Grimes 2714b88c807SRodney W. Grimes if (hist == NULL) 2724b88c807SRodney W. Grimes error("history not active"); 2734b88c807SRodney W. Grimes 2744b88c807SRodney W. Grimes if (argc == 1) 2754b88c807SRodney W. Grimes error("missing history argument"); 2764b88c807SRodney W. Grimes 2777cbda738SJilles Tjoelker while (not_fcnumber(*argptr) && (ch = nextopt("e:lnrs")) != '\0') 2784b88c807SRodney W. Grimes switch ((char)ch) { 2794b88c807SRodney W. Grimes case 'e': 2807cbda738SJilles Tjoelker editor = shoptarg; 2814b88c807SRodney W. Grimes break; 2824b88c807SRodney W. Grimes case 'l': 2834b88c807SRodney W. Grimes lflg = 1; 2844b88c807SRodney W. Grimes break; 2854b88c807SRodney W. Grimes case 'n': 2864b88c807SRodney W. Grimes nflg = 1; 2874b88c807SRodney W. Grimes break; 2884b88c807SRodney W. Grimes case 'r': 2894b88c807SRodney W. Grimes rflg = 1; 2904b88c807SRodney W. Grimes break; 2914b88c807SRodney W. Grimes case 's': 2924b88c807SRodney W. Grimes sflg = 1; 2934b88c807SRodney W. Grimes break; 2944b88c807SRodney W. Grimes } 2954b88c807SRodney W. Grimes 296685a2705SJilles Tjoelker savehandler = handler; 2974b88c807SRodney W. Grimes /* 2984b88c807SRodney W. Grimes * If executing... 2994b88c807SRodney W. Grimes */ 3004b88c807SRodney W. Grimes if (lflg == 0 || editor || sflg) { 3014b88c807SRodney W. Grimes lflg = 0; /* ignore */ 302224fbf9fSJilles Tjoelker editfile = NULL; 3034b88c807SRodney W. Grimes /* 3044b88c807SRodney W. Grimes * Catch interrupts to reset active counter and 3054b88c807SRodney W. Grimes * cleanup temp files. 3064b88c807SRodney W. Grimes */ 3074b88c807SRodney W. Grimes if (setjmp(jmploc.loc)) { 3084b88c807SRodney W. Grimes active = 0; 309224fbf9fSJilles Tjoelker if (editfile) 3104b88c807SRodney W. Grimes unlink(editfile); 3114b88c807SRodney W. Grimes handler = savehandler; 3124b88c807SRodney W. Grimes longjmp(handler->loc, 1); 3134b88c807SRodney W. Grimes } 3144b88c807SRodney W. Grimes handler = &jmploc; 3154b88c807SRodney W. Grimes if (++active > MAXHISTLOOPS) { 3164b88c807SRodney W. Grimes active = 0; 3174b88c807SRodney W. Grimes displayhist = 0; 3184b88c807SRodney W. Grimes error("called recursively too many times"); 3194b88c807SRodney W. Grimes } 3204b88c807SRodney W. Grimes /* 3214b88c807SRodney W. Grimes * Set editor. 3224b88c807SRodney W. Grimes */ 3234b88c807SRodney W. Grimes if (sflg == 0) { 3244b88c807SRodney W. Grimes if (editor == NULL && 3254b88c807SRodney W. Grimes (editor = bltinlookup("FCEDIT", 1)) == NULL && 3264b88c807SRodney W. Grimes (editor = bltinlookup("EDITOR", 1)) == NULL) 3274b88c807SRodney W. Grimes editor = DEFEDITOR; 3284b88c807SRodney W. Grimes if (editor[0] == '-' && editor[1] == '\0') { 3294b88c807SRodney W. Grimes sflg = 1; /* no edit */ 3304b88c807SRodney W. Grimes editor = NULL; 3314b88c807SRodney W. Grimes } 3324b88c807SRodney W. Grimes } 3334b88c807SRodney W. Grimes } 3344b88c807SRodney W. Grimes 3354b88c807SRodney W. Grimes /* 3364b88c807SRodney W. Grimes * If executing, parse [old=new] now 3374b88c807SRodney W. Grimes */ 3387cbda738SJilles Tjoelker if (lflg == 0 && *argptr != NULL && 3397cbda738SJilles Tjoelker ((repl = strchr(*argptr, '=')) != NULL)) { 3407cbda738SJilles Tjoelker pat = *argptr; 3414b88c807SRodney W. Grimes *repl++ = '\0'; 3427cbda738SJilles Tjoelker argptr++; 3434b88c807SRodney W. Grimes } 3444b88c807SRodney W. Grimes /* 3454b88c807SRodney W. Grimes * determine [first] and [last] 3464b88c807SRodney W. Grimes */ 3477cbda738SJilles Tjoelker if (*argptr == NULL) { 3484b88c807SRodney W. Grimes firststr = lflg ? "-16" : "-1"; 3494b88c807SRodney W. Grimes laststr = "-1"; 3507cbda738SJilles Tjoelker } else if (argptr[1] == NULL) { 3517cbda738SJilles Tjoelker firststr = argptr[0]; 3527cbda738SJilles Tjoelker laststr = lflg ? "-1" : argptr[0]; 3537cbda738SJilles Tjoelker } else if (argptr[2] == NULL) { 3547cbda738SJilles Tjoelker firststr = argptr[0]; 3557cbda738SJilles Tjoelker laststr = argptr[1]; 3567cbda738SJilles Tjoelker } else 357274110dfSJilles Tjoelker error("too many arguments"); 3584b88c807SRodney W. Grimes /* 3594b88c807SRodney W. Grimes * Turn into event numbers. 3604b88c807SRodney W. Grimes */ 3614b88c807SRodney W. Grimes first = str_to_event(firststr, 0); 3624b88c807SRodney W. Grimes last = str_to_event(laststr, 1); 3634b88c807SRodney W. Grimes 3644b88c807SRodney W. Grimes if (rflg) { 3654b88c807SRodney W. Grimes i = last; 3664b88c807SRodney W. Grimes last = first; 3674b88c807SRodney W. Grimes first = i; 3684b88c807SRodney W. Grimes } 3694b88c807SRodney W. Grimes /* 3704b88c807SRodney W. Grimes * XXX - this should not depend on the event numbers 3714b88c807SRodney W. Grimes * always increasing. Add sequence numbers or offset 3724b88c807SRodney W. Grimes * to the history element in next (diskbased) release. 3734b88c807SRodney W. Grimes */ 3744b88c807SRodney W. Grimes direction = first < last ? H_PREV : H_NEXT; 3754b88c807SRodney W. Grimes 3764b88c807SRodney W. Grimes /* 3774b88c807SRodney W. Grimes * If editing, grab a temp file. 3784b88c807SRodney W. Grimes */ 3794b88c807SRodney W. Grimes if (editor) { 3804b88c807SRodney W. Grimes int fd; 3814b88c807SRodney W. Grimes INTOFF; /* easier */ 382224fbf9fSJilles Tjoelker sprintf(editfilestr, "%s/_shXXXXXX", _PATH_TMP); 383224fbf9fSJilles Tjoelker if ((fd = mkstemp(editfilestr)) < 0) 3844b88c807SRodney W. Grimes error("can't create temporary file %s", editfile); 385224fbf9fSJilles Tjoelker editfile = editfilestr; 3864b88c807SRodney W. Grimes if ((efp = fdopen(fd, "w")) == NULL) { 3874b88c807SRodney W. Grimes close(fd); 388274110dfSJilles Tjoelker error("Out of space"); 3894b88c807SRodney W. Grimes } 3904b88c807SRodney W. Grimes } 3914b88c807SRodney W. Grimes 3924b88c807SRodney W. Grimes /* 3934b88c807SRodney W. Grimes * Loop through selected history events. If listing or executing, 3944b88c807SRodney W. Grimes * do it now. Otherwise, put into temp file and call the editor 3954b88c807SRodney W. Grimes * after. 3964b88c807SRodney W. Grimes * 3974b88c807SRodney W. Grimes * The history interface needs rethinking, as the following 3984b88c807SRodney W. Grimes * convolutions will demonstrate. 3994b88c807SRodney W. Grimes */ 400757eeda0SDavid E. O'Brien history(hist, &he, H_FIRST); 401757eeda0SDavid E. O'Brien retval = history(hist, &he, H_NEXT_EVENT, first); 402757eeda0SDavid E. O'Brien for (;retval != -1; retval = history(hist, &he, direction)) { 4034b88c807SRodney W. Grimes if (lflg) { 4044b88c807SRodney W. Grimes if (!nflg) 405757eeda0SDavid E. O'Brien out1fmt("%5d ", he.num); 406757eeda0SDavid E. O'Brien out1str(he.str); 4074b88c807SRodney W. Grimes } else { 40822afca9bSJilles Tjoelker const char *s = pat ? 40922afca9bSJilles Tjoelker fc_replace(he.str, pat, repl) : he.str; 4104b88c807SRodney W. Grimes 4114b88c807SRodney W. Grimes if (sflg) { 4124b88c807SRodney W. Grimes if (displayhist) { 4134b88c807SRodney W. Grimes out2str(s); 414c6204d4aSJilles Tjoelker flushout(out2); 4154b88c807SRodney W. Grimes } 416cb806389SStefan Farfeleder evalstring(s, 0); 4174b88c807SRodney W. Grimes if (displayhist && hist) { 4184b88c807SRodney W. Grimes /* 4194b88c807SRodney W. Grimes * XXX what about recursive and 4204b88c807SRodney W. Grimes * relative histnums. 4214b88c807SRodney W. Grimes */ 42232c07786STim J. Robbins oldhistnum = he.num; 423757eeda0SDavid E. O'Brien history(hist, &he, H_ENTER, s); 42432c07786STim J. Robbins /* 42532c07786STim J. Robbins * XXX H_ENTER moves the internal 42632c07786STim J. Robbins * cursor, set it back to the current 42732c07786STim J. Robbins * entry. 42832c07786STim J. Robbins */ 429acb4eadaSJilles Tjoelker history(hist, &he, 43032c07786STim J. Robbins H_NEXT_EVENT, oldhistnum); 4314b88c807SRodney W. Grimes } 4324b88c807SRodney W. Grimes } else 4334b88c807SRodney W. Grimes fputs(s, efp); 4344b88c807SRodney W. Grimes } 4354b88c807SRodney W. Grimes /* 436776fc0e9SYaroslav Tykhiy * At end? (if we were to lose last, we'd sure be 4374b88c807SRodney W. Grimes * messed up). 4384b88c807SRodney W. Grimes */ 439757eeda0SDavid E. O'Brien if (he.num == last) 4404b88c807SRodney W. Grimes break; 4414b88c807SRodney W. Grimes } 4424b88c807SRodney W. Grimes if (editor) { 4434b88c807SRodney W. Grimes char *editcmd; 4444b88c807SRodney W. Grimes 4454b88c807SRodney W. Grimes fclose(efp); 44679fb1e45SJilles Tjoelker INTON; 4474b88c807SRodney W. Grimes editcmd = stalloc(strlen(editor) + strlen(editfile) + 2); 4484b88c807SRodney W. Grimes sprintf(editcmd, "%s %s", editor, editfile); 449cb806389SStefan Farfeleder evalstring(editcmd, 0); /* XXX - should use no JC command */ 4504b88c807SRodney W. Grimes readcmdfile(editfile); /* XXX - should read back - quick tst */ 4514b88c807SRodney W. Grimes unlink(editfile); 4524b88c807SRodney W. Grimes } 4534b88c807SRodney W. Grimes 4544b88c807SRodney W. Grimes if (lflg == 0 && active > 0) 4554b88c807SRodney W. Grimes --active; 4564b88c807SRodney W. Grimes if (displayhist) 4574b88c807SRodney W. Grimes displayhist = 0; 458685a2705SJilles Tjoelker handler = savehandler; 459aa9caaf6SPeter Wemm return 0; 4604b88c807SRodney W. Grimes } 4614b88c807SRodney W. Grimes 46288328642SDavid E. O'Brien static char * 4635134c3f7SWarner Losh fc_replace(const char *s, char *p, char *r) 4644b88c807SRodney W. Grimes { 4654b88c807SRodney W. Grimes char *dest; 4664b88c807SRodney W. Grimes int plen = strlen(p); 4674b88c807SRodney W. Grimes 4684b88c807SRodney W. Grimes STARTSTACKSTR(dest); 4694b88c807SRodney W. Grimes while (*s) { 4704b88c807SRodney W. Grimes if (*s == *p && strncmp(s, p, plen) == 0) { 4719d37e157SJilles Tjoelker STPUTS(r, dest); 4724b88c807SRodney W. Grimes s += plen; 4734b88c807SRodney W. Grimes *p = '\0'; /* so no more matches */ 4744b88c807SRodney W. Grimes } else 4754b88c807SRodney W. Grimes STPUTC(*s++, dest); 4764b88c807SRodney W. Grimes } 4777cfe6941SDavid E. O'Brien STPUTC('\0', dest); 4784b88c807SRodney W. Grimes dest = grabstackstr(dest); 4794b88c807SRodney W. Grimes 4804b88c807SRodney W. Grimes return (dest); 4814b88c807SRodney W. Grimes } 4824b88c807SRodney W. Grimes 483260fc3f4SJilles Tjoelker static int 4842cac6e36SJilles Tjoelker not_fcnumber(const char *s) 4854b88c807SRodney W. Grimes { 486ab0a2172SSteve Price if (s == NULL) 48762730a71SSteve Price return (0); 4884b88c807SRodney W. Grimes if (*s == '-') 4894b88c807SRodney W. Grimes s++; 4904b88c807SRodney W. Grimes return (!is_number(s)); 4914b88c807SRodney W. Grimes } 4924b88c807SRodney W. Grimes 493260fc3f4SJilles Tjoelker static int 4942cac6e36SJilles Tjoelker str_to_event(const char *str, int last) 4954b88c807SRodney W. Grimes { 496757eeda0SDavid E. O'Brien HistEvent he; 4972cac6e36SJilles Tjoelker const char *s = str; 4984b88c807SRodney W. Grimes int relative = 0; 499757eeda0SDavid E. O'Brien int i, retval; 5004b88c807SRodney W. Grimes 501757eeda0SDavid E. O'Brien retval = history(hist, &he, H_FIRST); 5024b88c807SRodney W. Grimes switch (*s) { 5034b88c807SRodney W. Grimes case '-': 5044b88c807SRodney W. Grimes relative = 1; 5054b88c807SRodney W. Grimes /*FALLTHROUGH*/ 5064b88c807SRodney W. Grimes case '+': 5074b88c807SRodney W. Grimes s++; 5084b88c807SRodney W. Grimes } 5094b88c807SRodney W. Grimes if (is_number(s)) { 5104b88c807SRodney W. Grimes i = atoi(s); 5114b88c807SRodney W. Grimes if (relative) { 512757eeda0SDavid E. O'Brien while (retval != -1 && i--) { 513757eeda0SDavid E. O'Brien retval = history(hist, &he, H_NEXT); 5144b88c807SRodney W. Grimes } 515757eeda0SDavid E. O'Brien if (retval == -1) 516757eeda0SDavid E. O'Brien retval = history(hist, &he, H_LAST); 5174b88c807SRodney W. Grimes } else { 518757eeda0SDavid E. O'Brien retval = history(hist, &he, H_NEXT_EVENT, i); 519757eeda0SDavid E. O'Brien if (retval == -1) { 5204b88c807SRodney W. Grimes /* 5214b88c807SRodney W. Grimes * the notion of first and last is 5224b88c807SRodney W. Grimes * backwards to that of the history package 5234b88c807SRodney W. Grimes */ 524757eeda0SDavid E. O'Brien retval = history(hist, &he, last ? H_FIRST : H_LAST); 5254b88c807SRodney W. Grimes } 5264b88c807SRodney W. Grimes } 527757eeda0SDavid E. O'Brien if (retval == -1) 5284b88c807SRodney W. Grimes error("history number %s not found (internal error)", 5294b88c807SRodney W. Grimes str); 5304b88c807SRodney W. Grimes } else { 5314b88c807SRodney W. Grimes /* 5324b88c807SRodney W. Grimes * pattern 5334b88c807SRodney W. Grimes */ 534757eeda0SDavid E. O'Brien retval = history(hist, &he, H_PREV_STR, str); 535757eeda0SDavid E. O'Brien if (retval == -1) 5364b88c807SRodney W. Grimes error("history pattern not found: %s", str); 5374b88c807SRodney W. Grimes } 538757eeda0SDavid E. O'Brien return (he.num); 5394b88c807SRodney W. Grimes } 540088acf90STim J. Robbins 541088acf90STim J. Robbins int 542088acf90STim J. Robbins bindcmd(int argc, char **argv) 543088acf90STim J. Robbins { 54494b793c4SJilles Tjoelker int ret; 54594b793c4SJilles Tjoelker FILE *old; 54694b793c4SJilles Tjoelker FILE *out; 547088acf90STim J. Robbins 548088acf90STim J. Robbins if (el == NULL) 549088acf90STim J. Robbins error("line editing is disabled"); 55094b793c4SJilles Tjoelker 55194b793c4SJilles Tjoelker INTOFF; 55294b793c4SJilles Tjoelker 55394b793c4SJilles Tjoelker out = out1fp(); 55494b793c4SJilles Tjoelker if (out == NULL) 55594b793c4SJilles Tjoelker error("Out of space"); 55694b793c4SJilles Tjoelker 55794b793c4SJilles Tjoelker el_get(el, EL_GETFP, 1, &old); 55894b793c4SJilles Tjoelker el_set(el, EL_SETFP, 1, out); 55994b793c4SJilles Tjoelker 56094b793c4SJilles Tjoelker ret = el_parse(el, argc, __DECONST(const char **, argv)); 56194b793c4SJilles Tjoelker 56294b793c4SJilles Tjoelker el_set(el, EL_SETFP, 1, old); 56394b793c4SJilles Tjoelker 56494b793c4SJilles Tjoelker fclose(out); 56594b793c4SJilles Tjoelker 5669413dfd3SPiotr Pawel Stefaniak if (argc > 1 && argv[1][0] == '-' && 5679413dfd3SPiotr Pawel Stefaniak memchr("ve", argv[1][1], 2) != NULL) { 5689413dfd3SPiotr Pawel Stefaniak Vflag = argv[1][1] == 'v'; 5699413dfd3SPiotr Pawel Stefaniak Eflag = !Vflag; 5709413dfd3SPiotr Pawel Stefaniak histedit(); 5719413dfd3SPiotr Pawel Stefaniak } 5729413dfd3SPiotr Pawel Stefaniak 57394b793c4SJilles Tjoelker INTON; 57494b793c4SJilles Tjoelker 57594b793c4SJilles Tjoelker return ret; 576088acf90STim J. Robbins } 577088acf90STim J. Robbins 578b315a729SPiotr Pawel Stefaniak /* 579b315a729SPiotr Pawel Stefaniak * Comparator function for qsort(). The use of curpos here is to skip 580b315a729SPiotr Pawel Stefaniak * characters that we already know to compare equal (common prefix). 581b315a729SPiotr Pawel Stefaniak */ 582b315a729SPiotr Pawel Stefaniak static int 583b315a729SPiotr Pawel Stefaniak comparator(const void *a, const void *b, void *thunk) 584b315a729SPiotr Pawel Stefaniak { 585b315a729SPiotr Pawel Stefaniak size_t curpos = (intptr_t)thunk; 586b315a729SPiotr Pawel Stefaniak return (strcmp(*(char *const *)a + curpos, 587b315a729SPiotr Pawel Stefaniak *(char *const *)b + curpos)); 588b315a729SPiotr Pawel Stefaniak } 589b315a729SPiotr Pawel Stefaniak 590b315a729SPiotr Pawel Stefaniak /* 591b315a729SPiotr Pawel Stefaniak * This function is passed to libedit's fn_complete2(). The library will 592b315a729SPiotr Pawel Stefaniak * use it instead of its standard function that finds matching files in 593b315a729SPiotr Pawel Stefaniak * current directory. If we're at the start of the line, we want to look 594b315a729SPiotr Pawel Stefaniak * for available commands from all paths in $PATH. 595b315a729SPiotr Pawel Stefaniak */ 596b315a729SPiotr Pawel Stefaniak static char 597b315a729SPiotr Pawel Stefaniak **sh_matches(const char *text, int start, int end) 598b315a729SPiotr Pawel Stefaniak { 599b315a729SPiotr Pawel Stefaniak char *free_path = NULL, *path; 600b315a729SPiotr Pawel Stefaniak const char *dirname; 601b315a729SPiotr Pawel Stefaniak char **matches = NULL; 602b8ff849cSPiotr Pawel Stefaniak size_t i = 0, size = 16, uniq; 603c866d0c7SPiotr Pawel Stefaniak size_t curpos = end - start, lcstring = -1; 604b315a729SPiotr Pawel Stefaniak 605b315a729SPiotr Pawel Stefaniak if (start > 0 || memchr("/.~", text[0], 3) != NULL) 606b315a729SPiotr Pawel Stefaniak return (NULL); 607b315a729SPiotr Pawel Stefaniak if ((free_path = path = strdup(pathval())) == NULL) 608b315a729SPiotr Pawel Stefaniak goto out; 609b315a729SPiotr Pawel Stefaniak if ((matches = malloc(size * sizeof(matches[0]))) == NULL) 610b315a729SPiotr Pawel Stefaniak goto out; 611b315a729SPiotr Pawel Stefaniak while ((dirname = strsep(&path, ":")) != NULL) { 612b315a729SPiotr Pawel Stefaniak struct dirent *entry; 613b315a729SPiotr Pawel Stefaniak DIR *dir; 614b315a729SPiotr Pawel Stefaniak int dfd; 615b315a729SPiotr Pawel Stefaniak 616b315a729SPiotr Pawel Stefaniak dir = opendir(dirname[0] == '\0' ? "." : dirname); 617b315a729SPiotr Pawel Stefaniak if (dir == NULL) 618b315a729SPiotr Pawel Stefaniak continue; 619b315a729SPiotr Pawel Stefaniak if ((dfd = dirfd(dir)) == -1) { 620b315a729SPiotr Pawel Stefaniak closedir(dir); 621b315a729SPiotr Pawel Stefaniak continue; 622b315a729SPiotr Pawel Stefaniak } 623b315a729SPiotr Pawel Stefaniak while ((entry = readdir(dir)) != NULL) { 624b315a729SPiotr Pawel Stefaniak struct stat statb; 625b315a729SPiotr Pawel Stefaniak char **rmatches; 626b315a729SPiotr Pawel Stefaniak 627b315a729SPiotr Pawel Stefaniak if (strncmp(entry->d_name, text, curpos) != 0) 628b315a729SPiotr Pawel Stefaniak continue; 629b315a729SPiotr Pawel Stefaniak if (entry->d_type == DT_UNKNOWN || entry->d_type == DT_LNK) { 630b315a729SPiotr Pawel Stefaniak if (fstatat(dfd, entry->d_name, &statb, 0) == -1) 631b315a729SPiotr Pawel Stefaniak continue; 632b315a729SPiotr Pawel Stefaniak if (!S_ISREG(statb.st_mode)) 633b315a729SPiotr Pawel Stefaniak continue; 634b315a729SPiotr Pawel Stefaniak } else if (entry->d_type != DT_REG) 635b315a729SPiotr Pawel Stefaniak continue; 636b315a729SPiotr Pawel Stefaniak matches[++i] = strdup(entry->d_name); 637b315a729SPiotr Pawel Stefaniak if (i < size - 1) 638b315a729SPiotr Pawel Stefaniak continue; 639b315a729SPiotr Pawel Stefaniak size *= 2; 640b315a729SPiotr Pawel Stefaniak rmatches = reallocarray(matches, size, sizeof(matches[0])); 641b315a729SPiotr Pawel Stefaniak if (rmatches == NULL) { 642b315a729SPiotr Pawel Stefaniak closedir(dir); 643b315a729SPiotr Pawel Stefaniak goto out; 644b315a729SPiotr Pawel Stefaniak } 645b315a729SPiotr Pawel Stefaniak matches = rmatches; 646b315a729SPiotr Pawel Stefaniak } 647b315a729SPiotr Pawel Stefaniak closedir(dir); 648b315a729SPiotr Pawel Stefaniak } 649b315a729SPiotr Pawel Stefaniak out: 650b315a729SPiotr Pawel Stefaniak free(free_path); 651b8ff849cSPiotr Pawel Stefaniak if (i == 0) { 652b8ff849cSPiotr Pawel Stefaniak free(matches); 653b8ff849cSPiotr Pawel Stefaniak return (NULL); 654b8ff849cSPiotr Pawel Stefaniak } 655b8ff849cSPiotr Pawel Stefaniak uniq = 1; 656b8ff849cSPiotr Pawel Stefaniak if (i > 1) { 657b8ff849cSPiotr Pawel Stefaniak qsort_s(matches + 1, i, sizeof(matches[0]), comparator, 658b8ff849cSPiotr Pawel Stefaniak (void *)(intptr_t)curpos); 659c866d0c7SPiotr Pawel Stefaniak for (size_t k = 2; k <= i; k++) { 660c866d0c7SPiotr Pawel Stefaniak const char *l = matches[uniq] + curpos; 661c866d0c7SPiotr Pawel Stefaniak const char *r = matches[k] + curpos; 662c866d0c7SPiotr Pawel Stefaniak size_t common = 0; 663c866d0c7SPiotr Pawel Stefaniak 664c866d0c7SPiotr Pawel Stefaniak while (*l != '\0' && *r != '\0' && *l == *r) 665c866d0c7SPiotr Pawel Stefaniak (void)l++, r++, common++; 666c866d0c7SPiotr Pawel Stefaniak if (common < lcstring) 667c866d0c7SPiotr Pawel Stefaniak lcstring = common; 668c866d0c7SPiotr Pawel Stefaniak if (*l == *r) 669b8ff849cSPiotr Pawel Stefaniak free(matches[k]); 670b8ff849cSPiotr Pawel Stefaniak else 671b8ff849cSPiotr Pawel Stefaniak matches[++uniq] = matches[k]; 672b8ff849cSPiotr Pawel Stefaniak } 673c866d0c7SPiotr Pawel Stefaniak } 674b8ff849cSPiotr Pawel Stefaniak matches[uniq + 1] = NULL; 675b315a729SPiotr Pawel Stefaniak /* 676b315a729SPiotr Pawel Stefaniak * matches[0] is special: it's not a real matching file name but a common 677b315a729SPiotr Pawel Stefaniak * prefix for all matching names. It can't be null, unlike any other 678b315a729SPiotr Pawel Stefaniak * element of the array. When strings matches[0] and matches[1] compare 679b315a729SPiotr Pawel Stefaniak * equal and matches[2] is null that means to libedit that there is only 680b315a729SPiotr Pawel Stefaniak * a single match. It will then replace user input with possibly escaped 681b315a729SPiotr Pawel Stefaniak * string in matches[0] which is the reason to copy the full name of the 682b315a729SPiotr Pawel Stefaniak * only match. 683b315a729SPiotr Pawel Stefaniak */ 684c866d0c7SPiotr Pawel Stefaniak if (uniq == 1) 685c866d0c7SPiotr Pawel Stefaniak matches[0] = strdup(matches[1]); 686c866d0c7SPiotr Pawel Stefaniak else if (lcstring != (size_t)-1) 687c866d0c7SPiotr Pawel Stefaniak matches[0] = strndup(matches[1], curpos + lcstring); 688c866d0c7SPiotr Pawel Stefaniak else 689c866d0c7SPiotr Pawel Stefaniak matches[0] = strdup(text); 690b315a729SPiotr Pawel Stefaniak if (matches[0] == NULL) { 691b8ff849cSPiotr Pawel Stefaniak for (size_t k = 1; k <= uniq; k++) 692b8ff849cSPiotr Pawel Stefaniak free(matches[k]); 693b315a729SPiotr Pawel Stefaniak free(matches); 694b315a729SPiotr Pawel Stefaniak return (NULL); 695b315a729SPiotr Pawel Stefaniak } 696b315a729SPiotr Pawel Stefaniak return (matches); 697b315a729SPiotr Pawel Stefaniak } 698b315a729SPiotr Pawel Stefaniak 699b315a729SPiotr Pawel Stefaniak /* 700b315a729SPiotr Pawel Stefaniak * This is passed to el_set(el, EL_ADDFN, ...) so that it's possible to 701b315a729SPiotr Pawel Stefaniak * bind a key (tab by default) to execute the function. 702b315a729SPiotr Pawel Stefaniak */ 703b315a729SPiotr Pawel Stefaniak unsigned char 704b315a729SPiotr Pawel Stefaniak sh_complete(EditLine *sel, int ch __unused) 705b315a729SPiotr Pawel Stefaniak { 706b315a729SPiotr Pawel Stefaniak return (unsigned char)fn_complete2(sel, NULL, sh_matches, 707b315a729SPiotr Pawel Stefaniak L" \t\n\"\\'`@$><=;|&{(", NULL, NULL, (size_t)100, 708b315a729SPiotr Pawel Stefaniak NULL, &((int) {0}), NULL, NULL, FN_QUOTE_MATCH); 709b315a729SPiotr Pawel Stefaniak } 71035b253d9SPiotr Pawel Stefaniak 71135b253d9SPiotr Pawel Stefaniak #else 71235b253d9SPiotr Pawel Stefaniak #include "error.h" 71335b253d9SPiotr Pawel Stefaniak 71435b253d9SPiotr Pawel Stefaniak int 71535b253d9SPiotr Pawel Stefaniak histcmd(int argc __unused, char **argv __unused) 71635b253d9SPiotr Pawel Stefaniak { 71735b253d9SPiotr Pawel Stefaniak 71835b253d9SPiotr Pawel Stefaniak error("not compiled with history support"); 71935b253d9SPiotr Pawel Stefaniak /*NOTREACHED*/ 72035b253d9SPiotr Pawel Stefaniak return (0); 72135b253d9SPiotr Pawel Stefaniak } 72235b253d9SPiotr Pawel Stefaniak 72335b253d9SPiotr Pawel Stefaniak int 72435b253d9SPiotr Pawel Stefaniak bindcmd(int argc __unused, char **argv __unused) 72535b253d9SPiotr Pawel Stefaniak { 72635b253d9SPiotr Pawel Stefaniak 72735b253d9SPiotr Pawel Stefaniak error("not compiled with line editing support"); 72835b253d9SPiotr Pawel Stefaniak return (0); 72935b253d9SPiotr Pawel Stefaniak } 73035b253d9SPiotr Pawel Stefaniak #endif 731