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 #include <sys/param.h>
34b315a729SPiotr Pawel Stefaniak #include <sys/stat.h>
35b315a729SPiotr Pawel Stefaniak #include <dirent.h>
361f82fb38SPiotr Pawel Stefaniak #include <errno.h>
37988b1bb0SBaptiste Daroussin #include <fcntl.h>
3892e331afSWarner Losh #include <limits.h>
394b88c807SRodney W. Grimes #include <paths.h>
4068700941SPiotr Pawel Stefaniak #include <stdbool.h>
414b88c807SRodney W. Grimes #include <stdio.h>
42aa9caaf6SPeter Wemm #include <stdlib.h>
43aa9caaf6SPeter Wemm #include <unistd.h>
44aa9caaf6SPeter Wemm /*
45aa9caaf6SPeter Wemm * Editline and history functions (and glue).
46aa9caaf6SPeter Wemm */
470fd450e2SPiotr Pawel Stefaniak #include "alias.h"
48*8e5c53afSPiotr Pawel Stefaniak #include "exec.h"
494b88c807SRodney W. Grimes #include "shell.h"
504b88c807SRodney W. Grimes #include "parser.h"
514b88c807SRodney W. Grimes #include "var.h"
524b88c807SRodney W. Grimes #include "options.h"
53aa9caaf6SPeter Wemm #include "main.h"
54aa9caaf6SPeter Wemm #include "output.h"
554b88c807SRodney W. Grimes #include "mystring.h"
566c346639SBryan Drewery #include "builtins.h"
57aa9caaf6SPeter Wemm #ifndef NO_HISTORY
58aa9caaf6SPeter Wemm #include "myhistedit.h"
594b88c807SRodney W. Grimes #include "error.h"
60aa9caaf6SPeter Wemm #include "eval.h"
614b88c807SRodney W. Grimes #include "memalloc.h"
624b88c807SRodney W. Grimes
634b88c807SRodney W. Grimes #define MAXHISTLOOPS 4 /* max recursions through fc */
644b88c807SRodney W. Grimes #define DEFEDITOR "ed" /* default editor *should* be $EDITOR */
654b88c807SRodney W. Grimes
664b88c807SRodney W. Grimes History *hist; /* history cookie */
674b88c807SRodney W. Grimes EditLine *el; /* editline cookie */
684b88c807SRodney W. Grimes int displayhist;
691f82fb38SPiotr Pawel Stefaniak static int savehist;
70f91d2e21SJilles Tjoelker static FILE *el_in, *el_out;
7168700941SPiotr Pawel Stefaniak static bool in_command_completion;
724b88c807SRodney W. Grimes
7388328642SDavid E. O'Brien static char *fc_replace(const char *, char *, char *);
74260fc3f4SJilles Tjoelker static int not_fcnumber(const char *);
75260fc3f4SJilles Tjoelker static int str_to_event(const char *, int);
76b315a729SPiotr Pawel Stefaniak static int comparator(const void *, const void *, void *);
77b315a729SPiotr Pawel Stefaniak static char **sh_matches(const char *, int, int);
7868700941SPiotr Pawel Stefaniak static const char *append_char_function(const char *);
79b315a729SPiotr Pawel Stefaniak static unsigned char sh_complete(EditLine *, int);
804b88c807SRodney W. Grimes
81988b1bb0SBaptiste Daroussin static const char *
get_histfile(void)82988b1bb0SBaptiste Daroussin get_histfile(void)
83988b1bb0SBaptiste Daroussin {
84988b1bb0SBaptiste Daroussin const char *histfile;
85988b1bb0SBaptiste Daroussin
86988b1bb0SBaptiste Daroussin /* don't try to save if the history size is 0 */
873ce64010SDaniel Kolesa if (hist == NULL || !strcmp(histsizeval(), "0"))
88988b1bb0SBaptiste Daroussin return (NULL);
89988b1bb0SBaptiste Daroussin histfile = expandstr("${HISTFILE-${HOME-}/.sh_history}");
90988b1bb0SBaptiste Daroussin
91988b1bb0SBaptiste Daroussin if (histfile[0] == '\0')
92988b1bb0SBaptiste Daroussin return (NULL);
93988b1bb0SBaptiste Daroussin return (histfile);
94988b1bb0SBaptiste Daroussin }
95988b1bb0SBaptiste Daroussin
96988b1bb0SBaptiste Daroussin void
histsave(void)97988b1bb0SBaptiste Daroussin histsave(void)
98988b1bb0SBaptiste Daroussin {
99988b1bb0SBaptiste Daroussin HistEvent he;
100988b1bb0SBaptiste Daroussin char *histtmpname = NULL;
101988b1bb0SBaptiste Daroussin const char *histfile;
102988b1bb0SBaptiste Daroussin int fd;
103988b1bb0SBaptiste Daroussin FILE *f;
104988b1bb0SBaptiste Daroussin
1051f82fb38SPiotr Pawel Stefaniak if (!savehist || (histfile = get_histfile()) == NULL)
106988b1bb0SBaptiste Daroussin return;
107988b1bb0SBaptiste Daroussin INTOFF;
108988b1bb0SBaptiste Daroussin asprintf(&histtmpname, "%s.XXXXXXXXXX", histfile);
109988b1bb0SBaptiste Daroussin if (histtmpname == NULL) {
110988b1bb0SBaptiste Daroussin INTON;
111988b1bb0SBaptiste Daroussin return;
112988b1bb0SBaptiste Daroussin }
113988b1bb0SBaptiste Daroussin fd = mkstemp(histtmpname);
114988b1bb0SBaptiste Daroussin if (fd == -1 || (f = fdopen(fd, "w")) == NULL) {
115988b1bb0SBaptiste Daroussin free(histtmpname);
116988b1bb0SBaptiste Daroussin INTON;
117988b1bb0SBaptiste Daroussin return;
118988b1bb0SBaptiste Daroussin }
119988b1bb0SBaptiste Daroussin if (history(hist, &he, H_SAVE_FP, f) < 1 ||
120988b1bb0SBaptiste Daroussin rename(histtmpname, histfile) == -1)
121988b1bb0SBaptiste Daroussin unlink(histtmpname);
122988b1bb0SBaptiste Daroussin fclose(f);
123988b1bb0SBaptiste Daroussin free(histtmpname);
124988b1bb0SBaptiste Daroussin INTON;
125988b1bb0SBaptiste Daroussin
126988b1bb0SBaptiste Daroussin }
127988b1bb0SBaptiste Daroussin
128988b1bb0SBaptiste Daroussin void
histload(void)129988b1bb0SBaptiste Daroussin histload(void)
130988b1bb0SBaptiste Daroussin {
131988b1bb0SBaptiste Daroussin const char *histfile;
132988b1bb0SBaptiste Daroussin HistEvent he;
133988b1bb0SBaptiste Daroussin
134988b1bb0SBaptiste Daroussin if ((histfile = get_histfile()) == NULL)
135988b1bb0SBaptiste Daroussin return;
1361f82fb38SPiotr Pawel Stefaniak errno = 0;
1371f82fb38SPiotr Pawel Stefaniak if (history(hist, &he, H_LOAD, histfile) != -1 || errno == ENOENT)
1381f82fb38SPiotr Pawel Stefaniak savehist = 1;
139988b1bb0SBaptiste Daroussin }
140988b1bb0SBaptiste Daroussin
1414b88c807SRodney W. Grimes /*
1424b88c807SRodney W. Grimes * Set history and editing status. Called whenever the status may
1434b88c807SRodney W. Grimes * have changed (figures out what to do).
1444b88c807SRodney W. Grimes */
145aa9caaf6SPeter Wemm void
histedit(void)1465134c3f7SWarner Losh histedit(void)
147aa9caaf6SPeter Wemm {
1484b88c807SRodney W. Grimes
1494b88c807SRodney W. Grimes #define editing (Eflag || Vflag)
1504b88c807SRodney W. Grimes
1514b88c807SRodney W. Grimes if (iflag) {
1524b88c807SRodney W. Grimes if (!hist) {
1534b88c807SRodney W. Grimes /*
1544b88c807SRodney W. Grimes * turn history on
1554b88c807SRodney W. Grimes */
1564b88c807SRodney W. Grimes INTOFF;
1574b88c807SRodney W. Grimes hist = history_init();
1584b88c807SRodney W. Grimes INTON;
1594b88c807SRodney W. Grimes
1604b88c807SRodney W. Grimes if (hist != NULL)
161ab0a2172SSteve Price sethistsize(histsizeval());
1624b88c807SRodney W. Grimes else
163c6204d4aSJilles Tjoelker out2fmt_flush("sh: can't initialize history\n");
1644b88c807SRodney W. Grimes }
1654b88c807SRodney W. Grimes if (editing && !el && isatty(0)) { /* && isatty(2) ??? */
1664b88c807SRodney W. Grimes /*
1674b88c807SRodney W. Grimes * turn editing on
1684b88c807SRodney W. Grimes */
169580eefdfSJilles Tjoelker char *term;
170580eefdfSJilles Tjoelker
1714b88c807SRodney W. Grimes INTOFF;
1724b88c807SRodney W. Grimes if (el_in == NULL)
1734b88c807SRodney W. Grimes el_in = fdopen(0, "r");
1744b88c807SRodney W. Grimes if (el_out == NULL)
1754b88c807SRodney W. Grimes el_out = fdopen(2, "w");
176f91d2e21SJilles Tjoelker if (el_in == NULL || el_out == NULL)
1774b88c807SRodney W. Grimes goto bad;
178580eefdfSJilles Tjoelker term = lookupvar("TERM");
179580eefdfSJilles Tjoelker if (term)
180580eefdfSJilles Tjoelker setenv("TERM", term, 1);
181580eefdfSJilles Tjoelker else
182580eefdfSJilles Tjoelker unsetenv("TERM");
183f91d2e21SJilles Tjoelker el = el_init(arg0, el_in, el_out, el_out);
1844b88c807SRodney W. Grimes if (el != NULL) {
1854b88c807SRodney W. Grimes if (hist)
1864b88c807SRodney W. Grimes el_set(el, EL_HIST, history, hist);
1873cf65f8aSJuraj Lutter el_set(el, EL_PROMPT_ESC, getprompt, '\001');
188e46b12b7SJilles Tjoelker el_set(el, EL_ADDFN, "sh-complete",
189e46b12b7SJilles Tjoelker "Filename completion",
190b315a729SPiotr Pawel Stefaniak sh_complete);
1914b88c807SRodney W. Grimes } else {
1924b88c807SRodney W. Grimes bad:
193c6204d4aSJilles Tjoelker out2fmt_flush("sh: can't initialize editing\n");
1944b88c807SRodney W. Grimes }
1954b88c807SRodney W. Grimes INTON;
1964b88c807SRodney W. Grimes } else if (!editing && el) {
1974b88c807SRodney W. Grimes INTOFF;
1984b88c807SRodney W. Grimes el_end(el);
1994b88c807SRodney W. Grimes el = NULL;
2004b88c807SRodney W. Grimes INTON;
2014b88c807SRodney W. Grimes }
2024b88c807SRodney W. Grimes if (el) {
2034b88c807SRodney W. Grimes if (Vflag)
2044b88c807SRodney W. Grimes el_set(el, EL_EDITOR, "vi");
205660045fbSBaptiste Daroussin else if (Eflag) {
2064b88c807SRodney W. Grimes el_set(el, EL_EDITOR, "emacs");
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
sethistsize(const char * hs)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
setterm(const char * term)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
histcmd(int argc,char ** argv __unused)2507cbda738SJilles Tjoelker histcmd(int argc, char **argv __unused)
2514b88c807SRodney W. Grimes {
252384aedabSJilles Tjoelker const char *editor = NULL;
253757eeda0SDavid E. O'Brien HistEvent he;
2544b88c807SRodney W. Grimes int lflg = 0, nflg = 0, rflg = 0, sflg = 0;
255757eeda0SDavid E. O'Brien int i, retval;
256384aedabSJilles Tjoelker const char *firststr, *laststr;
2574b88c807SRodney W. Grimes int first, last, direction;
258384aedabSJilles Tjoelker char *pat = NULL, *repl = NULL;
2594b88c807SRodney W. Grimes static int active = 0;
2604b88c807SRodney W. Grimes struct jmploc jmploc;
261224fbf9fSJilles Tjoelker struct jmploc *savehandler;
262224fbf9fSJilles Tjoelker char editfilestr[PATH_MAX];
263224fbf9fSJilles Tjoelker char *volatile editfile;
264384aedabSJilles Tjoelker FILE *efp = NULL;
26532c07786STim J. Robbins int oldhistnum;
2664b88c807SRodney W. Grimes
2674b88c807SRodney W. Grimes if (hist == NULL)
2684b88c807SRodney W. Grimes error("history not active");
2694b88c807SRodney W. Grimes
2704b88c807SRodney W. Grimes if (argc == 1)
2714b88c807SRodney W. Grimes error("missing history argument");
2724b88c807SRodney W. Grimes
273755a1be6SPiotr Pawel Stefaniak while (not_fcnumber(*argptr))
274755a1be6SPiotr Pawel Stefaniak do {
275755a1be6SPiotr Pawel Stefaniak switch (nextopt("e:lnrs")) {
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;
291755a1be6SPiotr Pawel Stefaniak case '\0':
292755a1be6SPiotr Pawel Stefaniak goto operands;
2934b88c807SRodney W. Grimes }
294755a1be6SPiotr Pawel Stefaniak } while (nextopt_optptr != NULL);
295755a1be6SPiotr Pawel Stefaniak operands:
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 */
450d2c23317SStephane Rochoy readcmdfile(editfile, 0 /* verify */); /* 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 *
fc_replace(const char * s,char * p,char * r)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
not_fcnumber(const char * s)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
str_to_event(const char * str,int last)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
bindcmd(int argc,char ** argv)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
comparator(const void * a,const void * b,void * thunk)583b315a729SPiotr Pawel Stefaniak comparator(const void *a, const void *b, void *thunk)
584b315a729SPiotr Pawel Stefaniak {
585b315a729SPiotr Pawel Stefaniak size_t curpos = (intptr_t)thunk;
58643489c14SPiotr Pawel Stefaniak
587b315a729SPiotr Pawel Stefaniak return (strcmp(*(char *const *)a + curpos,
588b315a729SPiotr Pawel Stefaniak *(char *const *)b + curpos));
589b315a729SPiotr Pawel Stefaniak }
590b315a729SPiotr Pawel Stefaniak
59114dd0012SPiotr Pawel Stefaniak static char
add_match(char ** matches,size_t i,size_t * size,char * match_copy)59214dd0012SPiotr Pawel Stefaniak **add_match(char **matches, size_t i, size_t *size, char *match_copy)
59314dd0012SPiotr Pawel Stefaniak {
59414dd0012SPiotr Pawel Stefaniak if (match_copy == NULL)
59514dd0012SPiotr Pawel Stefaniak return (NULL);
59614dd0012SPiotr Pawel Stefaniak matches[i] = match_copy;
59714dd0012SPiotr Pawel Stefaniak if (i >= *size - 1) {
59814dd0012SPiotr Pawel Stefaniak *size *= 2;
59914dd0012SPiotr Pawel Stefaniak matches = reallocarray(matches, *size, sizeof(matches[0]));
60014dd0012SPiotr Pawel Stefaniak }
60114dd0012SPiotr Pawel Stefaniak
60214dd0012SPiotr Pawel Stefaniak return (matches);
60314dd0012SPiotr Pawel Stefaniak }
60414dd0012SPiotr Pawel Stefaniak
605b315a729SPiotr Pawel Stefaniak /*
60643489c14SPiotr Pawel Stefaniak * This function is passed to libedit's fn_complete2(). The library will use
60743489c14SPiotr Pawel Stefaniak * it instead of its standard function that finds matching files in current
60843489c14SPiotr Pawel Stefaniak * directory. If we're at the start of the line, we want to look for
60943489c14SPiotr Pawel Stefaniak * available commands from all paths in $PATH.
610b315a729SPiotr Pawel Stefaniak */
611b315a729SPiotr Pawel Stefaniak static char
sh_matches(const char * text,int start,int end)612b315a729SPiotr Pawel Stefaniak **sh_matches(const char *text, int start, int end)
613b315a729SPiotr Pawel Stefaniak {
614b315a729SPiotr Pawel Stefaniak char *free_path = NULL, *path;
615b315a729SPiotr Pawel Stefaniak const char *dirname;
61614dd0012SPiotr Pawel Stefaniak char **matches = NULL, **rmatches;
617b8ff849cSPiotr Pawel Stefaniak size_t i = 0, size = 16, uniq;
618c866d0c7SPiotr Pawel Stefaniak size_t curpos = end - start, lcstring = -1;
619*8e5c53afSPiotr Pawel Stefaniak struct cmdentry e;
620b315a729SPiotr Pawel Stefaniak
62168700941SPiotr Pawel Stefaniak in_command_completion = false;
622b315a729SPiotr Pawel Stefaniak if (start > 0 || memchr("/.~", text[0], 3) != NULL)
623b315a729SPiotr Pawel Stefaniak return (NULL);
62468700941SPiotr Pawel Stefaniak in_command_completion = true;
625b315a729SPiotr Pawel Stefaniak if ((free_path = path = strdup(pathval())) == NULL)
626b315a729SPiotr Pawel Stefaniak goto out;
627b315a729SPiotr Pawel Stefaniak if ((matches = malloc(size * sizeof(matches[0]))) == NULL)
628b315a729SPiotr Pawel Stefaniak goto out;
629b315a729SPiotr Pawel Stefaniak while ((dirname = strsep(&path, ":")) != NULL) {
630b315a729SPiotr Pawel Stefaniak struct dirent *entry;
631b315a729SPiotr Pawel Stefaniak DIR *dir;
632b315a729SPiotr Pawel Stefaniak int dfd;
633b315a729SPiotr Pawel Stefaniak
634b315a729SPiotr Pawel Stefaniak dir = opendir(dirname[0] == '\0' ? "." : dirname);
635b315a729SPiotr Pawel Stefaniak if (dir == NULL)
636b315a729SPiotr Pawel Stefaniak continue;
637b315a729SPiotr Pawel Stefaniak if ((dfd = dirfd(dir)) == -1) {
638b315a729SPiotr Pawel Stefaniak closedir(dir);
639b315a729SPiotr Pawel Stefaniak continue;
640b315a729SPiotr Pawel Stefaniak }
641b315a729SPiotr Pawel Stefaniak while ((entry = readdir(dir)) != NULL) {
642b315a729SPiotr Pawel Stefaniak struct stat statb;
643b315a729SPiotr Pawel Stefaniak
644b315a729SPiotr Pawel Stefaniak if (strncmp(entry->d_name, text, curpos) != 0)
645b315a729SPiotr Pawel Stefaniak continue;
646b315a729SPiotr Pawel Stefaniak if (entry->d_type == DT_UNKNOWN || entry->d_type == DT_LNK) {
647b315a729SPiotr Pawel Stefaniak if (fstatat(dfd, entry->d_name, &statb, 0) == -1)
648b315a729SPiotr Pawel Stefaniak continue;
649b315a729SPiotr Pawel Stefaniak if (!S_ISREG(statb.st_mode))
650b315a729SPiotr Pawel Stefaniak continue;
651b315a729SPiotr Pawel Stefaniak } else if (entry->d_type != DT_REG)
652b315a729SPiotr Pawel Stefaniak continue;
65314dd0012SPiotr Pawel Stefaniak rmatches = add_match(matches, ++i, &size,
65414dd0012SPiotr Pawel Stefaniak strdup(entry->d_name));
655b315a729SPiotr Pawel Stefaniak if (rmatches == NULL) {
656b315a729SPiotr Pawel Stefaniak closedir(dir);
657b315a729SPiotr Pawel Stefaniak goto out;
658b315a729SPiotr Pawel Stefaniak }
659b315a729SPiotr Pawel Stefaniak matches = rmatches;
660b315a729SPiotr Pawel Stefaniak }
661b315a729SPiotr Pawel Stefaniak closedir(dir);
662b315a729SPiotr Pawel Stefaniak }
66314dd0012SPiotr Pawel Stefaniak for (const unsigned char *bp = builtincmd; *bp != 0; bp += 2 + bp[0]) {
66414dd0012SPiotr Pawel Stefaniak if (curpos > bp[0] || memcmp(bp + 2, text, curpos) != 0)
66514dd0012SPiotr Pawel Stefaniak continue;
66614dd0012SPiotr Pawel Stefaniak rmatches = add_match(matches, ++i, &size, strndup(bp + 2, bp[0]));
66714dd0012SPiotr Pawel Stefaniak if (rmatches == NULL)
66814dd0012SPiotr Pawel Stefaniak goto out;
66914dd0012SPiotr Pawel Stefaniak matches = rmatches;
67014dd0012SPiotr Pawel Stefaniak }
6710fd450e2SPiotr Pawel Stefaniak for (const struct alias *ap = NULL; (ap = iteralias(ap)) != NULL;) {
6720fd450e2SPiotr Pawel Stefaniak if (strncmp(ap->name, text, curpos) != 0)
6730fd450e2SPiotr Pawel Stefaniak continue;
6740fd450e2SPiotr Pawel Stefaniak rmatches = add_match(matches, ++i, &size, strdup(ap->name));
6750fd450e2SPiotr Pawel Stefaniak if (rmatches == NULL)
6760fd450e2SPiotr Pawel Stefaniak goto out;
6770fd450e2SPiotr Pawel Stefaniak matches = rmatches;
6780fd450e2SPiotr Pawel Stefaniak }
679*8e5c53afSPiotr Pawel Stefaniak for (const void *a = NULL; (a = itercmd(a, &e)) != NULL;) {
680*8e5c53afSPiotr Pawel Stefaniak if (e.cmdtype != CMDFUNCTION)
681*8e5c53afSPiotr Pawel Stefaniak continue;
682*8e5c53afSPiotr Pawel Stefaniak if (strncmp(e.cmdname, text, curpos) != 0)
683*8e5c53afSPiotr Pawel Stefaniak continue;
684*8e5c53afSPiotr Pawel Stefaniak rmatches = add_match(matches, ++i, &size, strdup(e.cmdname));
685*8e5c53afSPiotr Pawel Stefaniak if (rmatches == NULL)
686*8e5c53afSPiotr Pawel Stefaniak goto out;
687*8e5c53afSPiotr Pawel Stefaniak matches = rmatches;
688*8e5c53afSPiotr Pawel Stefaniak }
689b315a729SPiotr Pawel Stefaniak out:
690b315a729SPiotr Pawel Stefaniak free(free_path);
691b8ff849cSPiotr Pawel Stefaniak if (i == 0) {
692b8ff849cSPiotr Pawel Stefaniak free(matches);
693b8ff849cSPiotr Pawel Stefaniak return (NULL);
694b8ff849cSPiotr Pawel Stefaniak }
695b8ff849cSPiotr Pawel Stefaniak uniq = 1;
696b8ff849cSPiotr Pawel Stefaniak if (i > 1) {
697b8ff849cSPiotr Pawel Stefaniak qsort_s(matches + 1, i, sizeof(matches[0]), comparator,
698b8ff849cSPiotr Pawel Stefaniak (void *)(intptr_t)curpos);
699c866d0c7SPiotr Pawel Stefaniak for (size_t k = 2; k <= i; k++) {
700c866d0c7SPiotr Pawel Stefaniak const char *l = matches[uniq] + curpos;
701c866d0c7SPiotr Pawel Stefaniak const char *r = matches[k] + curpos;
702c866d0c7SPiotr Pawel Stefaniak size_t common = 0;
703c866d0c7SPiotr Pawel Stefaniak
704c866d0c7SPiotr Pawel Stefaniak while (*l != '\0' && *r != '\0' && *l == *r)
705c866d0c7SPiotr Pawel Stefaniak (void)l++, r++, common++;
706c866d0c7SPiotr Pawel Stefaniak if (common < lcstring)
707c866d0c7SPiotr Pawel Stefaniak lcstring = common;
708c866d0c7SPiotr Pawel Stefaniak if (*l == *r)
709b8ff849cSPiotr Pawel Stefaniak free(matches[k]);
710b8ff849cSPiotr Pawel Stefaniak else
711b8ff849cSPiotr Pawel Stefaniak matches[++uniq] = matches[k];
712b8ff849cSPiotr Pawel Stefaniak }
713c866d0c7SPiotr Pawel Stefaniak }
714b8ff849cSPiotr Pawel Stefaniak matches[uniq + 1] = NULL;
715b315a729SPiotr Pawel Stefaniak /*
71643489c14SPiotr Pawel Stefaniak * matches[0] is special: it's not a real matching file name but
71743489c14SPiotr Pawel Stefaniak * a common prefix for all matching names. It can't be null, unlike
71843489c14SPiotr Pawel Stefaniak * any other element of the array. When strings matches[0] and
71943489c14SPiotr Pawel Stefaniak * matches[1] compare equal and matches[2] is null that means to
72043489c14SPiotr Pawel Stefaniak * libedit that there is only a single match. It will then replace
72143489c14SPiotr Pawel Stefaniak * user input with possibly escaped string in matches[0] which is the
72243489c14SPiotr Pawel Stefaniak * reason to copy the full name of the only match.
723b315a729SPiotr Pawel Stefaniak */
724c866d0c7SPiotr Pawel Stefaniak if (uniq == 1)
725c866d0c7SPiotr Pawel Stefaniak matches[0] = strdup(matches[1]);
726c866d0c7SPiotr Pawel Stefaniak else if (lcstring != (size_t)-1)
727c866d0c7SPiotr Pawel Stefaniak matches[0] = strndup(matches[1], curpos + lcstring);
728c866d0c7SPiotr Pawel Stefaniak else
729c866d0c7SPiotr Pawel Stefaniak matches[0] = strdup(text);
730b315a729SPiotr Pawel Stefaniak if (matches[0] == NULL) {
731b8ff849cSPiotr Pawel Stefaniak for (size_t k = 1; k <= uniq; k++)
732b8ff849cSPiotr Pawel Stefaniak free(matches[k]);
733b315a729SPiotr Pawel Stefaniak free(matches);
734b315a729SPiotr Pawel Stefaniak return (NULL);
735b315a729SPiotr Pawel Stefaniak }
736b315a729SPiotr Pawel Stefaniak return (matches);
737b315a729SPiotr Pawel Stefaniak }
738b315a729SPiotr Pawel Stefaniak
739b315a729SPiotr Pawel Stefaniak /*
74068700941SPiotr Pawel Stefaniak * If we don't specify this function as app_func in the call to fn_complete2,
74168700941SPiotr Pawel Stefaniak * libedit will use the default one, which adds a " " to plain files and
74268700941SPiotr Pawel Stefaniak * a "/" to directories regardless of whether it's a command name or a plain
74368700941SPiotr Pawel Stefaniak * path (relative or absolute). We never want to add "/" to commands.
74468700941SPiotr Pawel Stefaniak *
74568700941SPiotr Pawel Stefaniak * For example, after I did "mkdir rmdir", "rmdi" would be autocompleted to
74668700941SPiotr Pawel Stefaniak * "rmdir/" instead of "rmdir ".
74768700941SPiotr Pawel Stefaniak */
74868700941SPiotr Pawel Stefaniak static const char *
append_char_function(const char * name)74968700941SPiotr Pawel Stefaniak append_char_function(const char *name)
75068700941SPiotr Pawel Stefaniak {
75168700941SPiotr Pawel Stefaniak struct stat stbuf;
75268700941SPiotr Pawel Stefaniak char *expname = name[0] == '~' ? fn_tilde_expand(name) : NULL;
75368700941SPiotr Pawel Stefaniak const char *rs;
75468700941SPiotr Pawel Stefaniak
75568700941SPiotr Pawel Stefaniak if (!in_command_completion &&
75668700941SPiotr Pawel Stefaniak stat(expname ? expname : name, &stbuf) == 0 &&
75768700941SPiotr Pawel Stefaniak S_ISDIR(stbuf.st_mode))
75868700941SPiotr Pawel Stefaniak rs = "/";
75968700941SPiotr Pawel Stefaniak else
76068700941SPiotr Pawel Stefaniak rs = " ";
76168700941SPiotr Pawel Stefaniak free(expname);
76268700941SPiotr Pawel Stefaniak return (rs);
76368700941SPiotr Pawel Stefaniak }
76468700941SPiotr Pawel Stefaniak
76568700941SPiotr Pawel Stefaniak /*
766b315a729SPiotr Pawel Stefaniak * This is passed to el_set(el, EL_ADDFN, ...) so that it's possible to
767b315a729SPiotr Pawel Stefaniak * bind a key (tab by default) to execute the function.
768b315a729SPiotr Pawel Stefaniak */
769b315a729SPiotr Pawel Stefaniak unsigned char
sh_complete(EditLine * sel,int ch __unused)770b315a729SPiotr Pawel Stefaniak sh_complete(EditLine *sel, int ch __unused)
771b315a729SPiotr Pawel Stefaniak {
772b315a729SPiotr Pawel Stefaniak return (unsigned char)fn_complete2(sel, NULL, sh_matches,
77368700941SPiotr Pawel Stefaniak L" \t\n\"\\'`@$><=;|&{(", NULL, append_char_function,
77468700941SPiotr Pawel Stefaniak (size_t)100, NULL, &((int) {0}), NULL, NULL, FN_QUOTE_MATCH);
775b315a729SPiotr Pawel Stefaniak }
77635b253d9SPiotr Pawel Stefaniak
77735b253d9SPiotr Pawel Stefaniak #else
77835b253d9SPiotr Pawel Stefaniak #include "error.h"
77935b253d9SPiotr Pawel Stefaniak
78035b253d9SPiotr Pawel Stefaniak int
histcmd(int argc __unused,char ** argv __unused)78135b253d9SPiotr Pawel Stefaniak histcmd(int argc __unused, char **argv __unused)
78235b253d9SPiotr Pawel Stefaniak {
78335b253d9SPiotr Pawel Stefaniak
78435b253d9SPiotr Pawel Stefaniak error("not compiled with history support");
78535b253d9SPiotr Pawel Stefaniak /*NOTREACHED*/
78635b253d9SPiotr Pawel Stefaniak return (0);
78735b253d9SPiotr Pawel Stefaniak }
78835b253d9SPiotr Pawel Stefaniak
78935b253d9SPiotr Pawel Stefaniak int
bindcmd(int argc __unused,char ** argv __unused)79035b253d9SPiotr Pawel Stefaniak bindcmd(int argc __unused, char **argv __unused)
79135b253d9SPiotr Pawel Stefaniak {
79235b253d9SPiotr Pawel Stefaniak
79335b253d9SPiotr Pawel Stefaniak error("not compiled with line editing support");
79435b253d9SPiotr Pawel Stefaniak return (0);
79535b253d9SPiotr Pawel Stefaniak }
79635b253d9SPiotr Pawel Stefaniak #endif
797