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> 44988b1bb0SBaptiste Daroussin #include <fcntl.h> 4592e331afSWarner Losh #include <limits.h> 464b88c807SRodney W. Grimes #include <paths.h> 474b88c807SRodney W. Grimes #include <stdio.h> 48aa9caaf6SPeter Wemm #include <stdlib.h> 49aa9caaf6SPeter Wemm #include <unistd.h> 50aa9caaf6SPeter Wemm /* 51aa9caaf6SPeter Wemm * Editline and history functions (and glue). 52aa9caaf6SPeter Wemm */ 534b88c807SRodney W. Grimes #include "shell.h" 544b88c807SRodney W. Grimes #include "parser.h" 554b88c807SRodney W. Grimes #include "var.h" 564b88c807SRodney W. Grimes #include "options.h" 57aa9caaf6SPeter Wemm #include "main.h" 58aa9caaf6SPeter Wemm #include "output.h" 594b88c807SRodney W. Grimes #include "mystring.h" 606c346639SBryan Drewery #include "builtins.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; 73f91d2e21SJilles Tjoelker static FILE *el_in, *el_out; 744b88c807SRodney W. Grimes 7588328642SDavid E. O'Brien static char *fc_replace(const char *, char *, char *); 76260fc3f4SJilles Tjoelker static int not_fcnumber(const char *); 77260fc3f4SJilles Tjoelker static int str_to_event(const char *, int); 78b315a729SPiotr Pawel Stefaniak static int comparator(const void *, const void *, void *); 79b315a729SPiotr Pawel Stefaniak static char **sh_matches(const char *, int, int); 80b315a729SPiotr Pawel Stefaniak static unsigned char sh_complete(EditLine *, int); 814b88c807SRodney W. Grimes 82988b1bb0SBaptiste Daroussin static const char * 83988b1bb0SBaptiste Daroussin get_histfile(void) 84988b1bb0SBaptiste Daroussin { 85988b1bb0SBaptiste Daroussin const char *histfile; 86988b1bb0SBaptiste Daroussin 87988b1bb0SBaptiste Daroussin /* don't try to save if the history size is 0 */ 88988b1bb0SBaptiste Daroussin if (hist == NULL || histsizeval() == 0) 89988b1bb0SBaptiste Daroussin return (NULL); 90988b1bb0SBaptiste Daroussin histfile = expandstr("${HISTFILE-${HOME-}/.sh_history}"); 91988b1bb0SBaptiste Daroussin 92988b1bb0SBaptiste Daroussin if (histfile[0] == '\0') 93988b1bb0SBaptiste Daroussin return (NULL); 94988b1bb0SBaptiste Daroussin return (histfile); 95988b1bb0SBaptiste Daroussin } 96988b1bb0SBaptiste Daroussin 97988b1bb0SBaptiste Daroussin void 98988b1bb0SBaptiste Daroussin histsave(void) 99988b1bb0SBaptiste Daroussin { 100988b1bb0SBaptiste Daroussin HistEvent he; 101988b1bb0SBaptiste Daroussin char *histtmpname = NULL; 102988b1bb0SBaptiste Daroussin const char *histfile; 103988b1bb0SBaptiste Daroussin int fd; 104988b1bb0SBaptiste Daroussin FILE *f; 105988b1bb0SBaptiste Daroussin 106988b1bb0SBaptiste Daroussin if ((histfile = get_histfile()) == NULL) 107988b1bb0SBaptiste Daroussin return; 108988b1bb0SBaptiste Daroussin INTOFF; 109988b1bb0SBaptiste Daroussin asprintf(&histtmpname, "%s.XXXXXXXXXX", histfile); 110988b1bb0SBaptiste Daroussin if (histtmpname == NULL) { 111988b1bb0SBaptiste Daroussin INTON; 112988b1bb0SBaptiste Daroussin return; 113988b1bb0SBaptiste Daroussin } 114988b1bb0SBaptiste Daroussin fd = mkstemp(histtmpname); 115988b1bb0SBaptiste Daroussin if (fd == -1 || (f = fdopen(fd, "w")) == NULL) { 116988b1bb0SBaptiste Daroussin free(histtmpname); 117988b1bb0SBaptiste Daroussin INTON; 118988b1bb0SBaptiste Daroussin return; 119988b1bb0SBaptiste Daroussin } 120988b1bb0SBaptiste Daroussin if (history(hist, &he, H_SAVE_FP, f) < 1 || 121988b1bb0SBaptiste Daroussin rename(histtmpname, histfile) == -1) 122988b1bb0SBaptiste Daroussin unlink(histtmpname); 123988b1bb0SBaptiste Daroussin fclose(f); 124988b1bb0SBaptiste Daroussin free(histtmpname); 125988b1bb0SBaptiste Daroussin INTON; 126988b1bb0SBaptiste Daroussin 127988b1bb0SBaptiste Daroussin } 128988b1bb0SBaptiste Daroussin 129988b1bb0SBaptiste Daroussin void 130988b1bb0SBaptiste Daroussin histload(void) 131988b1bb0SBaptiste Daroussin { 132988b1bb0SBaptiste Daroussin const char *histfile; 133988b1bb0SBaptiste Daroussin HistEvent he; 134988b1bb0SBaptiste Daroussin 135988b1bb0SBaptiste Daroussin if ((histfile = get_histfile()) == NULL) 136988b1bb0SBaptiste Daroussin return; 137988b1bb0SBaptiste Daroussin history(hist, &he, H_LOAD, histfile); 138988b1bb0SBaptiste Daroussin } 139988b1bb0SBaptiste Daroussin 1404b88c807SRodney W. Grimes /* 1414b88c807SRodney W. Grimes * Set history and editing status. Called whenever the status may 1424b88c807SRodney W. Grimes * have changed (figures out what to do). 1434b88c807SRodney W. Grimes */ 144aa9caaf6SPeter Wemm void 1455134c3f7SWarner Losh histedit(void) 146aa9caaf6SPeter Wemm { 1474b88c807SRodney W. Grimes 1484b88c807SRodney W. Grimes #define editing (Eflag || Vflag) 1494b88c807SRodney W. Grimes 1504b88c807SRodney W. Grimes if (iflag) { 1514b88c807SRodney W. Grimes if (!hist) { 1524b88c807SRodney W. Grimes /* 1534b88c807SRodney W. Grimes * turn history on 1544b88c807SRodney W. Grimes */ 1554b88c807SRodney W. Grimes INTOFF; 1564b88c807SRodney W. Grimes hist = history_init(); 1574b88c807SRodney W. Grimes INTON; 1584b88c807SRodney W. Grimes 1594b88c807SRodney W. Grimes if (hist != NULL) 160ab0a2172SSteve Price sethistsize(histsizeval()); 1614b88c807SRodney W. Grimes else 162c6204d4aSJilles Tjoelker out2fmt_flush("sh: can't initialize history\n"); 1634b88c807SRodney W. Grimes } 1644b88c807SRodney W. Grimes if (editing && !el && isatty(0)) { /* && isatty(2) ??? */ 1654b88c807SRodney W. Grimes /* 1664b88c807SRodney W. Grimes * turn editing on 1674b88c807SRodney W. Grimes */ 168580eefdfSJilles Tjoelker char *term; 169580eefdfSJilles Tjoelker 1704b88c807SRodney W. Grimes INTOFF; 1714b88c807SRodney W. Grimes if (el_in == NULL) 1724b88c807SRodney W. Grimes el_in = fdopen(0, "r"); 1734b88c807SRodney W. Grimes if (el_out == NULL) 1744b88c807SRodney W. Grimes el_out = fdopen(2, "w"); 175f91d2e21SJilles Tjoelker if (el_in == NULL || el_out == NULL) 1764b88c807SRodney W. Grimes goto bad; 177580eefdfSJilles Tjoelker term = lookupvar("TERM"); 178580eefdfSJilles Tjoelker if (term) 179580eefdfSJilles Tjoelker setenv("TERM", term, 1); 180580eefdfSJilles Tjoelker else 181580eefdfSJilles Tjoelker unsetenv("TERM"); 182f91d2e21SJilles Tjoelker el = el_init(arg0, el_in, el_out, el_out); 1834b88c807SRodney W. Grimes if (el != NULL) { 1844b88c807SRodney W. Grimes if (hist) 1854b88c807SRodney W. Grimes el_set(el, EL_HIST, history, hist); 1864b88c807SRodney W. Grimes el_set(el, EL_PROMPT, getprompt); 187e46b12b7SJilles Tjoelker el_set(el, EL_ADDFN, "sh-complete", 188e46b12b7SJilles Tjoelker "Filename completion", 189b315a729SPiotr Pawel Stefaniak sh_complete); 1904b88c807SRodney W. Grimes } else { 1914b88c807SRodney W. Grimes bad: 192c6204d4aSJilles Tjoelker out2fmt_flush("sh: can't initialize editing\n"); 1934b88c807SRodney W. Grimes } 1944b88c807SRodney W. Grimes INTON; 1954b88c807SRodney W. Grimes } else if (!editing && el) { 1964b88c807SRodney W. Grimes INTOFF; 1974b88c807SRodney W. Grimes el_end(el); 1984b88c807SRodney W. Grimes el = NULL; 1994b88c807SRodney W. Grimes INTON; 2004b88c807SRodney W. Grimes } 2014b88c807SRodney W. Grimes if (el) { 2024b88c807SRodney W. Grimes if (Vflag) 2034b88c807SRodney W. Grimes el_set(el, EL_EDITOR, "vi"); 204660045fbSBaptiste Daroussin else if (Eflag) { 2054b88c807SRodney W. Grimes el_set(el, EL_EDITOR, "emacs"); 206660045fbSBaptiste Daroussin el_set(el, EL_BIND, "^R", "em-inc-search-prev", NULL); 207660045fbSBaptiste Daroussin } 208e46b12b7SJilles Tjoelker el_set(el, EL_BIND, "^I", "sh-complete", NULL); 209ecd807fbSTim J. Robbins el_source(el, NULL); 2104b88c807SRodney W. Grimes } 2114b88c807SRodney W. Grimes } else { 2124b88c807SRodney W. Grimes INTOFF; 2134b88c807SRodney W. Grimes if (el) { /* no editing if not interactive */ 2144b88c807SRodney W. Grimes el_end(el); 2154b88c807SRodney W. Grimes el = NULL; 2164b88c807SRodney W. Grimes } 2174b88c807SRodney W. Grimes if (hist) { 2184b88c807SRodney W. Grimes history_end(hist); 2194b88c807SRodney W. Grimes hist = NULL; 2204b88c807SRodney W. Grimes } 2214b88c807SRodney W. Grimes INTON; 2224b88c807SRodney W. Grimes } 2234b88c807SRodney W. Grimes } 2244b88c807SRodney W. Grimes 225aa9caaf6SPeter Wemm 226aa9caaf6SPeter Wemm void 2272fae4c3dSPhilippe Charnier sethistsize(const char *hs) 228aa9caaf6SPeter Wemm { 2294b88c807SRodney W. Grimes int histsize; 230757eeda0SDavid E. O'Brien HistEvent he; 2314b88c807SRodney W. Grimes 2324b88c807SRodney W. Grimes if (hist != NULL) { 233ef9e6178SJilles Tjoelker if (hs == NULL || !is_number(hs)) 2344b88c807SRodney W. Grimes histsize = 100; 235ef9e6178SJilles Tjoelker else 236ef9e6178SJilles Tjoelker histsize = atoi(hs); 2372110d9c3SStefan Farfeleder history(hist, &he, H_SETSIZE, histsize); 238933803fbSJilles Tjoelker history(hist, &he, H_SETUNIQUE, 1); 2394b88c807SRodney W. Grimes } 2404b88c807SRodney W. Grimes } 2414b88c807SRodney W. Grimes 242580eefdfSJilles Tjoelker void 243580eefdfSJilles Tjoelker setterm(const char *term) 244580eefdfSJilles Tjoelker { 245580eefdfSJilles Tjoelker if (rootshell && el != NULL && term != NULL) 246580eefdfSJilles Tjoelker el_set(el, EL_TERMINAL, term); 247580eefdfSJilles Tjoelker } 248580eefdfSJilles Tjoelker 249aa9caaf6SPeter Wemm int 2507cbda738SJilles Tjoelker histcmd(int argc, char **argv __unused) 2514b88c807SRodney W. Grimes { 2524b88c807SRodney W. Grimes int ch; 253384aedabSJilles Tjoelker const char *editor = NULL; 254757eeda0SDavid E. O'Brien HistEvent he; 2554b88c807SRodney W. Grimes int lflg = 0, nflg = 0, rflg = 0, sflg = 0; 256757eeda0SDavid E. O'Brien int i, retval; 257384aedabSJilles Tjoelker const char *firststr, *laststr; 2584b88c807SRodney W. Grimes int first, last, direction; 259384aedabSJilles Tjoelker char *pat = NULL, *repl = NULL; 2604b88c807SRodney W. Grimes static int active = 0; 2614b88c807SRodney W. Grimes struct jmploc jmploc; 262224fbf9fSJilles Tjoelker struct jmploc *savehandler; 263224fbf9fSJilles Tjoelker char editfilestr[PATH_MAX]; 264224fbf9fSJilles Tjoelker char *volatile editfile; 265384aedabSJilles Tjoelker FILE *efp = NULL; 26632c07786STim J. Robbins int oldhistnum; 2674b88c807SRodney W. Grimes 2684b88c807SRodney W. Grimes if (hist == NULL) 2694b88c807SRodney W. Grimes error("history not active"); 2704b88c807SRodney W. Grimes 2714b88c807SRodney W. Grimes if (argc == 1) 2724b88c807SRodney W. Grimes error("missing history argument"); 2734b88c807SRodney W. Grimes 2747cbda738SJilles Tjoelker while (not_fcnumber(*argptr) && (ch = nextopt("e:lnrs")) != '\0') 2754b88c807SRodney W. Grimes switch ((char)ch) { 2764b88c807SRodney W. Grimes case 'e': 2777cbda738SJilles Tjoelker editor = shoptarg; 2784b88c807SRodney W. Grimes break; 2794b88c807SRodney W. Grimes case 'l': 2804b88c807SRodney W. Grimes lflg = 1; 2814b88c807SRodney W. Grimes break; 2824b88c807SRodney W. Grimes case 'n': 2834b88c807SRodney W. Grimes nflg = 1; 2844b88c807SRodney W. Grimes break; 2854b88c807SRodney W. Grimes case 'r': 2864b88c807SRodney W. Grimes rflg = 1; 2874b88c807SRodney W. Grimes break; 2884b88c807SRodney W. Grimes case 's': 2894b88c807SRodney W. Grimes sflg = 1; 2904b88c807SRodney W. Grimes break; 2914b88c807SRodney W. Grimes } 2924b88c807SRodney W. Grimes 293685a2705SJilles Tjoelker savehandler = handler; 2944b88c807SRodney W. Grimes /* 2954b88c807SRodney W. Grimes * If executing... 2964b88c807SRodney W. Grimes */ 2974b88c807SRodney W. Grimes if (lflg == 0 || editor || sflg) { 2984b88c807SRodney W. Grimes lflg = 0; /* ignore */ 299224fbf9fSJilles Tjoelker editfile = NULL; 3004b88c807SRodney W. Grimes /* 3014b88c807SRodney W. Grimes * Catch interrupts to reset active counter and 3024b88c807SRodney W. Grimes * cleanup temp files. 3034b88c807SRodney W. Grimes */ 3044b88c807SRodney W. Grimes if (setjmp(jmploc.loc)) { 3054b88c807SRodney W. Grimes active = 0; 306224fbf9fSJilles Tjoelker if (editfile) 3074b88c807SRodney W. Grimes unlink(editfile); 3084b88c807SRodney W. Grimes handler = savehandler; 3094b88c807SRodney W. Grimes longjmp(handler->loc, 1); 3104b88c807SRodney W. Grimes } 3114b88c807SRodney W. Grimes handler = &jmploc; 3124b88c807SRodney W. Grimes if (++active > MAXHISTLOOPS) { 3134b88c807SRodney W. Grimes active = 0; 3144b88c807SRodney W. Grimes displayhist = 0; 3154b88c807SRodney W. Grimes error("called recursively too many times"); 3164b88c807SRodney W. Grimes } 3174b88c807SRodney W. Grimes /* 3184b88c807SRodney W. Grimes * Set editor. 3194b88c807SRodney W. Grimes */ 3204b88c807SRodney W. Grimes if (sflg == 0) { 3214b88c807SRodney W. Grimes if (editor == NULL && 3224b88c807SRodney W. Grimes (editor = bltinlookup("FCEDIT", 1)) == NULL && 3234b88c807SRodney W. Grimes (editor = bltinlookup("EDITOR", 1)) == NULL) 3244b88c807SRodney W. Grimes editor = DEFEDITOR; 3254b88c807SRodney W. Grimes if (editor[0] == '-' && editor[1] == '\0') { 3264b88c807SRodney W. Grimes sflg = 1; /* no edit */ 3274b88c807SRodney W. Grimes editor = NULL; 3284b88c807SRodney W. Grimes } 3294b88c807SRodney W. Grimes } 3304b88c807SRodney W. Grimes } 3314b88c807SRodney W. Grimes 3324b88c807SRodney W. Grimes /* 3334b88c807SRodney W. Grimes * If executing, parse [old=new] now 3344b88c807SRodney W. Grimes */ 3357cbda738SJilles Tjoelker if (lflg == 0 && *argptr != NULL && 3367cbda738SJilles Tjoelker ((repl = strchr(*argptr, '=')) != NULL)) { 3377cbda738SJilles Tjoelker pat = *argptr; 3384b88c807SRodney W. Grimes *repl++ = '\0'; 3397cbda738SJilles Tjoelker argptr++; 3404b88c807SRodney W. Grimes } 3414b88c807SRodney W. Grimes /* 3424b88c807SRodney W. Grimes * determine [first] and [last] 3434b88c807SRodney W. Grimes */ 3447cbda738SJilles Tjoelker if (*argptr == NULL) { 3454b88c807SRodney W. Grimes firststr = lflg ? "-16" : "-1"; 3464b88c807SRodney W. Grimes laststr = "-1"; 3477cbda738SJilles Tjoelker } else if (argptr[1] == NULL) { 3487cbda738SJilles Tjoelker firststr = argptr[0]; 3497cbda738SJilles Tjoelker laststr = lflg ? "-1" : argptr[0]; 3507cbda738SJilles Tjoelker } else if (argptr[2] == NULL) { 3517cbda738SJilles Tjoelker firststr = argptr[0]; 3527cbda738SJilles Tjoelker laststr = argptr[1]; 3537cbda738SJilles Tjoelker } else 354274110dfSJilles Tjoelker error("too many arguments"); 3554b88c807SRodney W. Grimes /* 3564b88c807SRodney W. Grimes * Turn into event numbers. 3574b88c807SRodney W. Grimes */ 3584b88c807SRodney W. Grimes first = str_to_event(firststr, 0); 3594b88c807SRodney W. Grimes last = str_to_event(laststr, 1); 3604b88c807SRodney W. Grimes 3614b88c807SRodney W. Grimes if (rflg) { 3624b88c807SRodney W. Grimes i = last; 3634b88c807SRodney W. Grimes last = first; 3644b88c807SRodney W. Grimes first = i; 3654b88c807SRodney W. Grimes } 3664b88c807SRodney W. Grimes /* 3674b88c807SRodney W. Grimes * XXX - this should not depend on the event numbers 3684b88c807SRodney W. Grimes * always increasing. Add sequence numbers or offset 3694b88c807SRodney W. Grimes * to the history element in next (diskbased) release. 3704b88c807SRodney W. Grimes */ 3714b88c807SRodney W. Grimes direction = first < last ? H_PREV : H_NEXT; 3724b88c807SRodney W. Grimes 3734b88c807SRodney W. Grimes /* 3744b88c807SRodney W. Grimes * If editing, grab a temp file. 3754b88c807SRodney W. Grimes */ 3764b88c807SRodney W. Grimes if (editor) { 3774b88c807SRodney W. Grimes int fd; 3784b88c807SRodney W. Grimes INTOFF; /* easier */ 379224fbf9fSJilles Tjoelker sprintf(editfilestr, "%s/_shXXXXXX", _PATH_TMP); 380224fbf9fSJilles Tjoelker if ((fd = mkstemp(editfilestr)) < 0) 3814b88c807SRodney W. Grimes error("can't create temporary file %s", editfile); 382224fbf9fSJilles Tjoelker editfile = editfilestr; 3834b88c807SRodney W. Grimes if ((efp = fdopen(fd, "w")) == NULL) { 3844b88c807SRodney W. Grimes close(fd); 385274110dfSJilles Tjoelker error("Out of space"); 3864b88c807SRodney W. Grimes } 3874b88c807SRodney W. Grimes } 3884b88c807SRodney W. Grimes 3894b88c807SRodney W. Grimes /* 3904b88c807SRodney W. Grimes * Loop through selected history events. If listing or executing, 3914b88c807SRodney W. Grimes * do it now. Otherwise, put into temp file and call the editor 3924b88c807SRodney W. Grimes * after. 3934b88c807SRodney W. Grimes * 3944b88c807SRodney W. Grimes * The history interface needs rethinking, as the following 3954b88c807SRodney W. Grimes * convolutions will demonstrate. 3964b88c807SRodney W. Grimes */ 397757eeda0SDavid E. O'Brien history(hist, &he, H_FIRST); 398757eeda0SDavid E. O'Brien retval = history(hist, &he, H_NEXT_EVENT, first); 399757eeda0SDavid E. O'Brien for (;retval != -1; retval = history(hist, &he, direction)) { 4004b88c807SRodney W. Grimes if (lflg) { 4014b88c807SRodney W. Grimes if (!nflg) 402757eeda0SDavid E. O'Brien out1fmt("%5d ", he.num); 403757eeda0SDavid E. O'Brien out1str(he.str); 4044b88c807SRodney W. Grimes } else { 40522afca9bSJilles Tjoelker const char *s = pat ? 40622afca9bSJilles Tjoelker fc_replace(he.str, pat, repl) : he.str; 4074b88c807SRodney W. Grimes 4084b88c807SRodney W. Grimes if (sflg) { 4094b88c807SRodney W. Grimes if (displayhist) { 4104b88c807SRodney W. Grimes out2str(s); 411c6204d4aSJilles Tjoelker flushout(out2); 4124b88c807SRodney W. Grimes } 413cb806389SStefan Farfeleder evalstring(s, 0); 4144b88c807SRodney W. Grimes if (displayhist && hist) { 4154b88c807SRodney W. Grimes /* 4164b88c807SRodney W. Grimes * XXX what about recursive and 4174b88c807SRodney W. Grimes * relative histnums. 4184b88c807SRodney W. Grimes */ 41932c07786STim J. Robbins oldhistnum = he.num; 420757eeda0SDavid E. O'Brien history(hist, &he, H_ENTER, s); 42132c07786STim J. Robbins /* 42232c07786STim J. Robbins * XXX H_ENTER moves the internal 42332c07786STim J. Robbins * cursor, set it back to the current 42432c07786STim J. Robbins * entry. 42532c07786STim J. Robbins */ 426acb4eadaSJilles Tjoelker history(hist, &he, 42732c07786STim J. Robbins H_NEXT_EVENT, oldhistnum); 4284b88c807SRodney W. Grimes } 4294b88c807SRodney W. Grimes } else 4304b88c807SRodney W. Grimes fputs(s, efp); 4314b88c807SRodney W. Grimes } 4324b88c807SRodney W. Grimes /* 433776fc0e9SYaroslav Tykhiy * At end? (if we were to lose last, we'd sure be 4344b88c807SRodney W. Grimes * messed up). 4354b88c807SRodney W. Grimes */ 436757eeda0SDavid E. O'Brien if (he.num == last) 4374b88c807SRodney W. Grimes break; 4384b88c807SRodney W. Grimes } 4394b88c807SRodney W. Grimes if (editor) { 4404b88c807SRodney W. Grimes char *editcmd; 4414b88c807SRodney W. Grimes 4424b88c807SRodney W. Grimes fclose(efp); 44379fb1e45SJilles Tjoelker INTON; 4444b88c807SRodney W. Grimes editcmd = stalloc(strlen(editor) + strlen(editfile) + 2); 4454b88c807SRodney W. Grimes sprintf(editcmd, "%s %s", editor, editfile); 446cb806389SStefan Farfeleder evalstring(editcmd, 0); /* XXX - should use no JC command */ 4474b88c807SRodney W. Grimes readcmdfile(editfile); /* XXX - should read back - quick tst */ 4484b88c807SRodney W. Grimes unlink(editfile); 4494b88c807SRodney W. Grimes } 4504b88c807SRodney W. Grimes 4514b88c807SRodney W. Grimes if (lflg == 0 && active > 0) 4524b88c807SRodney W. Grimes --active; 4534b88c807SRodney W. Grimes if (displayhist) 4544b88c807SRodney W. Grimes displayhist = 0; 455685a2705SJilles Tjoelker handler = savehandler; 456aa9caaf6SPeter Wemm return 0; 4574b88c807SRodney W. Grimes } 4584b88c807SRodney W. Grimes 45988328642SDavid E. O'Brien static char * 4605134c3f7SWarner Losh fc_replace(const char *s, char *p, char *r) 4614b88c807SRodney W. Grimes { 4624b88c807SRodney W. Grimes char *dest; 4634b88c807SRodney W. Grimes int plen = strlen(p); 4644b88c807SRodney W. Grimes 4654b88c807SRodney W. Grimes STARTSTACKSTR(dest); 4664b88c807SRodney W. Grimes while (*s) { 4674b88c807SRodney W. Grimes if (*s == *p && strncmp(s, p, plen) == 0) { 4689d37e157SJilles Tjoelker STPUTS(r, dest); 4694b88c807SRodney W. Grimes s += plen; 4704b88c807SRodney W. Grimes *p = '\0'; /* so no more matches */ 4714b88c807SRodney W. Grimes } else 4724b88c807SRodney W. Grimes STPUTC(*s++, dest); 4734b88c807SRodney W. Grimes } 4747cfe6941SDavid E. O'Brien STPUTC('\0', dest); 4754b88c807SRodney W. Grimes dest = grabstackstr(dest); 4764b88c807SRodney W. Grimes 4774b88c807SRodney W. Grimes return (dest); 4784b88c807SRodney W. Grimes } 4794b88c807SRodney W. Grimes 480260fc3f4SJilles Tjoelker static int 4812cac6e36SJilles Tjoelker not_fcnumber(const char *s) 4824b88c807SRodney W. Grimes { 483ab0a2172SSteve Price if (s == NULL) 48462730a71SSteve Price return (0); 4854b88c807SRodney W. Grimes if (*s == '-') 4864b88c807SRodney W. Grimes s++; 4874b88c807SRodney W. Grimes return (!is_number(s)); 4884b88c807SRodney W. Grimes } 4894b88c807SRodney W. Grimes 490260fc3f4SJilles Tjoelker static int 4912cac6e36SJilles Tjoelker str_to_event(const char *str, int last) 4924b88c807SRodney W. Grimes { 493757eeda0SDavid E. O'Brien HistEvent he; 4942cac6e36SJilles Tjoelker const char *s = str; 4954b88c807SRodney W. Grimes int relative = 0; 496757eeda0SDavid E. O'Brien int i, retval; 4974b88c807SRodney W. Grimes 498757eeda0SDavid E. O'Brien retval = history(hist, &he, H_FIRST); 4994b88c807SRodney W. Grimes switch (*s) { 5004b88c807SRodney W. Grimes case '-': 5014b88c807SRodney W. Grimes relative = 1; 5024b88c807SRodney W. Grimes /*FALLTHROUGH*/ 5034b88c807SRodney W. Grimes case '+': 5044b88c807SRodney W. Grimes s++; 5054b88c807SRodney W. Grimes } 5064b88c807SRodney W. Grimes if (is_number(s)) { 5074b88c807SRodney W. Grimes i = atoi(s); 5084b88c807SRodney W. Grimes if (relative) { 509757eeda0SDavid E. O'Brien while (retval != -1 && i--) { 510757eeda0SDavid E. O'Brien retval = history(hist, &he, H_NEXT); 5114b88c807SRodney W. Grimes } 512757eeda0SDavid E. O'Brien if (retval == -1) 513757eeda0SDavid E. O'Brien retval = history(hist, &he, H_LAST); 5144b88c807SRodney W. Grimes } else { 515757eeda0SDavid E. O'Brien retval = history(hist, &he, H_NEXT_EVENT, i); 516757eeda0SDavid E. O'Brien if (retval == -1) { 5174b88c807SRodney W. Grimes /* 5184b88c807SRodney W. Grimes * the notion of first and last is 5194b88c807SRodney W. Grimes * backwards to that of the history package 5204b88c807SRodney W. Grimes */ 521757eeda0SDavid E. O'Brien retval = history(hist, &he, last ? H_FIRST : H_LAST); 5224b88c807SRodney W. Grimes } 5234b88c807SRodney W. Grimes } 524757eeda0SDavid E. O'Brien if (retval == -1) 5254b88c807SRodney W. Grimes error("history number %s not found (internal error)", 5264b88c807SRodney W. Grimes str); 5274b88c807SRodney W. Grimes } else { 5284b88c807SRodney W. Grimes /* 5294b88c807SRodney W. Grimes * pattern 5304b88c807SRodney W. Grimes */ 531757eeda0SDavid E. O'Brien retval = history(hist, &he, H_PREV_STR, str); 532757eeda0SDavid E. O'Brien if (retval == -1) 5334b88c807SRodney W. Grimes error("history pattern not found: %s", str); 5344b88c807SRodney W. Grimes } 535757eeda0SDavid E. O'Brien return (he.num); 5364b88c807SRodney W. Grimes } 537088acf90STim J. Robbins 538088acf90STim J. Robbins int 539088acf90STim J. Robbins bindcmd(int argc, char **argv) 540088acf90STim J. Robbins { 54194b793c4SJilles Tjoelker int ret; 54294b793c4SJilles Tjoelker FILE *old; 54394b793c4SJilles Tjoelker FILE *out; 544088acf90STim J. Robbins 545088acf90STim J. Robbins if (el == NULL) 546088acf90STim J. Robbins error("line editing is disabled"); 54794b793c4SJilles Tjoelker 54894b793c4SJilles Tjoelker INTOFF; 54994b793c4SJilles Tjoelker 55094b793c4SJilles Tjoelker out = out1fp(); 55194b793c4SJilles Tjoelker if (out == NULL) 55294b793c4SJilles Tjoelker error("Out of space"); 55394b793c4SJilles Tjoelker 55494b793c4SJilles Tjoelker el_get(el, EL_GETFP, 1, &old); 55594b793c4SJilles Tjoelker el_set(el, EL_SETFP, 1, out); 55694b793c4SJilles Tjoelker 55794b793c4SJilles Tjoelker ret = el_parse(el, argc, __DECONST(const char **, argv)); 55894b793c4SJilles Tjoelker 55994b793c4SJilles Tjoelker el_set(el, EL_SETFP, 1, old); 56094b793c4SJilles Tjoelker 56194b793c4SJilles Tjoelker fclose(out); 56294b793c4SJilles Tjoelker 56394b793c4SJilles Tjoelker INTON; 56494b793c4SJilles Tjoelker 56594b793c4SJilles Tjoelker return ret; 566088acf90STim J. Robbins } 567088acf90STim J. Robbins 568b315a729SPiotr Pawel Stefaniak /* 569b315a729SPiotr Pawel Stefaniak * Comparator function for qsort(). The use of curpos here is to skip 570b315a729SPiotr Pawel Stefaniak * characters that we already know to compare equal (common prefix). 571b315a729SPiotr Pawel Stefaniak */ 572b315a729SPiotr Pawel Stefaniak static int 573b315a729SPiotr Pawel Stefaniak comparator(const void *a, const void *b, void *thunk) 574b315a729SPiotr Pawel Stefaniak { 575b315a729SPiotr Pawel Stefaniak size_t curpos = (intptr_t)thunk; 576b315a729SPiotr Pawel Stefaniak return (strcmp(*(char *const *)a + curpos, 577b315a729SPiotr Pawel Stefaniak *(char *const *)b + curpos)); 578b315a729SPiotr Pawel Stefaniak } 579b315a729SPiotr Pawel Stefaniak 580b315a729SPiotr Pawel Stefaniak /* 581b315a729SPiotr Pawel Stefaniak * This function is passed to libedit's fn_complete2(). The library will 582b315a729SPiotr Pawel Stefaniak * use it instead of its standard function that finds matching files in 583b315a729SPiotr Pawel Stefaniak * current directory. If we're at the start of the line, we want to look 584b315a729SPiotr Pawel Stefaniak * for available commands from all paths in $PATH. 585b315a729SPiotr Pawel Stefaniak */ 586b315a729SPiotr Pawel Stefaniak static char 587b315a729SPiotr Pawel Stefaniak **sh_matches(const char *text, int start, int end) 588b315a729SPiotr Pawel Stefaniak { 589b315a729SPiotr Pawel Stefaniak char *free_path = NULL, *path; 590b315a729SPiotr Pawel Stefaniak const char *dirname; 591b315a729SPiotr Pawel Stefaniak char **matches = NULL; 592b315a729SPiotr Pawel Stefaniak size_t i = 0, size = 16, j, k; 593b315a729SPiotr Pawel Stefaniak size_t curpos = end - start; 594b315a729SPiotr Pawel Stefaniak 595b315a729SPiotr Pawel Stefaniak if (start > 0 || memchr("/.~", text[0], 3) != NULL) 596b315a729SPiotr Pawel Stefaniak return (NULL); 597b315a729SPiotr Pawel Stefaniak if ((free_path = path = strdup(pathval())) == NULL) 598b315a729SPiotr Pawel Stefaniak goto out; 599b315a729SPiotr Pawel Stefaniak if ((matches = malloc(size * sizeof(matches[0]))) == NULL) 600b315a729SPiotr Pawel Stefaniak goto out; 601b315a729SPiotr Pawel Stefaniak while ((dirname = strsep(&path, ":")) != NULL) { 602b315a729SPiotr Pawel Stefaniak struct dirent *entry; 603b315a729SPiotr Pawel Stefaniak DIR *dir; 604b315a729SPiotr Pawel Stefaniak int dfd; 605b315a729SPiotr Pawel Stefaniak 606b315a729SPiotr Pawel Stefaniak dir = opendir(dirname[0] == '\0' ? "." : dirname); 607b315a729SPiotr Pawel Stefaniak if (dir == NULL) 608b315a729SPiotr Pawel Stefaniak continue; 609b315a729SPiotr Pawel Stefaniak if ((dfd = dirfd(dir)) == -1) { 610b315a729SPiotr Pawel Stefaniak closedir(dir); 611b315a729SPiotr Pawel Stefaniak continue; 612b315a729SPiotr Pawel Stefaniak } 613b315a729SPiotr Pawel Stefaniak while ((entry = readdir(dir)) != NULL) { 614b315a729SPiotr Pawel Stefaniak struct stat statb; 615b315a729SPiotr Pawel Stefaniak char **rmatches; 616b315a729SPiotr Pawel Stefaniak 617b315a729SPiotr Pawel Stefaniak if (strncmp(entry->d_name, text, curpos) != 0) 618b315a729SPiotr Pawel Stefaniak continue; 619b315a729SPiotr Pawel Stefaniak if (entry->d_type == DT_UNKNOWN || entry->d_type == DT_LNK) { 620b315a729SPiotr Pawel Stefaniak if (fstatat(dfd, entry->d_name, &statb, 0) == -1) 621b315a729SPiotr Pawel Stefaniak continue; 622b315a729SPiotr Pawel Stefaniak if (!S_ISREG(statb.st_mode)) 623b315a729SPiotr Pawel Stefaniak continue; 624b315a729SPiotr Pawel Stefaniak } else if (entry->d_type != DT_REG) 625b315a729SPiotr Pawel Stefaniak continue; 626b315a729SPiotr Pawel Stefaniak matches[++i] = strdup(entry->d_name); 627b315a729SPiotr Pawel Stefaniak if (i < size - 1) 628b315a729SPiotr Pawel Stefaniak continue; 629b315a729SPiotr Pawel Stefaniak size *= 2; 630b315a729SPiotr Pawel Stefaniak rmatches = reallocarray(matches, size, sizeof(matches[0])); 631b315a729SPiotr Pawel Stefaniak if (rmatches == NULL) { 632b315a729SPiotr Pawel Stefaniak closedir(dir); 633b315a729SPiotr Pawel Stefaniak goto out; 634b315a729SPiotr Pawel Stefaniak } 635b315a729SPiotr Pawel Stefaniak matches = rmatches; 636b315a729SPiotr Pawel Stefaniak } 637b315a729SPiotr Pawel Stefaniak closedir(dir); 638b315a729SPiotr Pawel Stefaniak } 639b315a729SPiotr Pawel Stefaniak out: 640b315a729SPiotr Pawel Stefaniak free(free_path); 641b315a729SPiotr Pawel Stefaniak /* 642b315a729SPiotr Pawel Stefaniak * matches[0] is special: it's not a real matching file name but a common 643b315a729SPiotr Pawel Stefaniak * prefix for all matching names. It can't be null, unlike any other 644b315a729SPiotr Pawel Stefaniak * element of the array. When strings matches[0] and matches[1] compare 645b315a729SPiotr Pawel Stefaniak * equal and matches[2] is null that means to libedit that there is only 646b315a729SPiotr Pawel Stefaniak * a single match. It will then replace user input with possibly escaped 647b315a729SPiotr Pawel Stefaniak * string in matches[0] which is the reason to copy the full name of the 648b315a729SPiotr Pawel Stefaniak * only match. 649b315a729SPiotr Pawel Stefaniak */ 650b315a729SPiotr Pawel Stefaniak if (i == 0) { 651b315a729SPiotr Pawel Stefaniak free(matches); 652b315a729SPiotr Pawel Stefaniak return (NULL); 653b315a729SPiotr Pawel Stefaniak } else if (i == 1) { 654b315a729SPiotr Pawel Stefaniak matches[0] = strdup(matches[1]); 655b315a729SPiotr Pawel Stefaniak matches[2] = NULL; 656b315a729SPiotr Pawel Stefaniak if (matches[0] != NULL) 657b315a729SPiotr Pawel Stefaniak return (matches); 658b315a729SPiotr Pawel Stefaniak } else 659b315a729SPiotr Pawel Stefaniak matches[0] = strdup(text); 660b315a729SPiotr Pawel Stefaniak if (matches[0] == NULL) { 661b315a729SPiotr Pawel Stefaniak for (j = 1; j <= i; j++) 662b315a729SPiotr Pawel Stefaniak free(matches[j]); 663b315a729SPiotr Pawel Stefaniak free(matches); 664b315a729SPiotr Pawel Stefaniak return (NULL); 665b315a729SPiotr Pawel Stefaniak } 666b315a729SPiotr Pawel Stefaniak qsort_s(matches + 1, i, sizeof(matches[0]), comparator, 667b315a729SPiotr Pawel Stefaniak (void *)(intptr_t)curpos); 668b315a729SPiotr Pawel Stefaniak for (j = 1, k = 2; k <= i; k++) 669b315a729SPiotr Pawel Stefaniak if (strcmp(matches[j] + curpos, matches[k] + curpos) == 0) 670b315a729SPiotr Pawel Stefaniak free(matches[k]); 671b315a729SPiotr Pawel Stefaniak else 672b315a729SPiotr Pawel Stefaniak matches[++j] = matches[k]; 673b315a729SPiotr Pawel Stefaniak matches[j + 1] = NULL; 674b315a729SPiotr Pawel Stefaniak return (matches); 675b315a729SPiotr Pawel Stefaniak } 676b315a729SPiotr Pawel Stefaniak 677b315a729SPiotr Pawel Stefaniak /* 678b315a729SPiotr Pawel Stefaniak * This is passed to el_set(el, EL_ADDFN, ...) so that it's possible to 679b315a729SPiotr Pawel Stefaniak * bind a key (tab by default) to execute the function. 680b315a729SPiotr Pawel Stefaniak */ 681b315a729SPiotr Pawel Stefaniak unsigned char 682b315a729SPiotr Pawel Stefaniak sh_complete(EditLine *sel, int ch __unused) 683b315a729SPiotr Pawel Stefaniak { 684b315a729SPiotr Pawel Stefaniak return (unsigned char)fn_complete2(sel, NULL, sh_matches, 685b315a729SPiotr Pawel Stefaniak L" \t\n\"\\'`@$><=;|&{(", NULL, NULL, (size_t)100, 686b315a729SPiotr Pawel Stefaniak NULL, &((int) {0}), NULL, NULL, FN_QUOTE_MATCH); 687b315a729SPiotr Pawel Stefaniak } 68835b253d9SPiotr Pawel Stefaniak 68935b253d9SPiotr Pawel Stefaniak #else 69035b253d9SPiotr Pawel Stefaniak #include "error.h" 69135b253d9SPiotr Pawel Stefaniak 69235b253d9SPiotr Pawel Stefaniak int 69335b253d9SPiotr Pawel Stefaniak histcmd(int argc __unused, char **argv __unused) 69435b253d9SPiotr Pawel Stefaniak { 69535b253d9SPiotr Pawel Stefaniak 69635b253d9SPiotr Pawel Stefaniak error("not compiled with history support"); 69735b253d9SPiotr Pawel Stefaniak /*NOTREACHED*/ 69835b253d9SPiotr Pawel Stefaniak return (0); 69935b253d9SPiotr Pawel Stefaniak } 70035b253d9SPiotr Pawel Stefaniak 70135b253d9SPiotr Pawel Stefaniak int 70235b253d9SPiotr Pawel Stefaniak bindcmd(int argc __unused, char **argv __unused) 70335b253d9SPiotr Pawel Stefaniak { 70435b253d9SPiotr Pawel Stefaniak 70535b253d9SPiotr Pawel Stefaniak error("not compiled with line editing support"); 70635b253d9SPiotr Pawel Stefaniak return (0); 70735b253d9SPiotr Pawel Stefaniak } 70835b253d9SPiotr Pawel Stefaniak #endif 709