Home | History | Annotate | Line # | Download | only in sh
histedit.c revision 1.70
      1  1.70       kre /*	$NetBSD: histedit.c,v 1.70 2024/07/12 07:30:30 kre Exp $	*/
      2   1.6       cgd 
      3   1.1       jtc /*-
      4   1.1       jtc  * Copyright (c) 1993
      5   1.1       jtc  *	The Regents of the University of California.  All rights reserved.
      6   1.1       jtc  *
      7   1.1       jtc  * This code is derived from software contributed to Berkeley by
      8   1.1       jtc  * Kenneth Almquist.
      9   1.1       jtc  *
     10   1.1       jtc  * Redistribution and use in source and binary forms, with or without
     11   1.1       jtc  * modification, are permitted provided that the following conditions
     12   1.1       jtc  * are met:
     13   1.1       jtc  * 1. Redistributions of source code must retain the above copyright
     14   1.1       jtc  *    notice, this list of conditions and the following disclaimer.
     15   1.1       jtc  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       jtc  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       jtc  *    documentation and/or other materials provided with the distribution.
     18  1.31       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1       jtc  *    may be used to endorse or promote products derived from this software
     20   1.1       jtc  *    without specific prior written permission.
     21   1.1       jtc  *
     22   1.1       jtc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1       jtc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1       jtc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1       jtc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1       jtc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1       jtc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1       jtc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1       jtc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1       jtc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1       jtc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1       jtc  * SUCH DAMAGE.
     33   1.1       jtc  */
     34   1.1       jtc 
     35  1.14  christos #include <sys/cdefs.h>
     36   1.1       jtc #ifndef lint
     37   1.6       cgd #if 0
     38   1.8  christos static char sccsid[] = "@(#)histedit.c	8.2 (Berkeley) 5/4/95";
     39   1.6       cgd #else
     40  1.70       kre __RCSID("$NetBSD: histedit.c,v 1.70 2024/07/12 07:30:30 kre Exp $");
     41   1.6       cgd #endif
     42   1.1       jtc #endif /* not lint */
     43   1.1       jtc 
     44   1.1       jtc #include <sys/param.h>
     45  1.56  christos #include <sys/stat.h>
     46  1.56  christos #include <dirent.h>
     47   1.1       jtc #include <paths.h>
     48   1.1       jtc #include <stdio.h>
     49   1.2       jtc #include <stdlib.h>
     50   1.2       jtc #include <unistd.h>
     51   1.8  christos /*
     52   1.8  christos  * Editline and history functions (and glue).
     53   1.8  christos  */
     54   1.1       jtc #include "shell.h"
     55   1.1       jtc #include "parser.h"
     56   1.1       jtc #include "var.h"
     57   1.1       jtc #include "options.h"
     58  1.43  christos #include "builtins.h"
     59   1.8  christos #include "main.h"
     60   1.4       cgd #include "output.h"
     61   1.1       jtc #include "mystring.h"
     62   1.8  christos #include "myhistedit.h"
     63   1.1       jtc #include "error.h"
     64  1.47  christos #include "alias.h"
     65  1.67       kre 
     66  1.67       kre #ifndef SMALL		/* almost all the rest of this file */
     67  1.67       kre 
     68   1.5       cgd #include "eval.h"
     69   1.1       jtc #include "memalloc.h"
     70  1.65       kre #include "show.h"
     71   1.1       jtc 
     72   1.1       jtc #define MAXHISTLOOPS	4	/* max recursions through fc */
     73   1.1       jtc #define DEFEDITOR	"ed"	/* default editor *should* be $EDITOR */
     74   1.1       jtc 
     75   1.1       jtc History *hist;	/* history cookie */
     76   1.1       jtc EditLine *el;	/* editline cookie */
     77   1.1       jtc int displayhist;
     78   1.1       jtc static FILE *el_in, *el_out;
     79  1.56  christos static int curpos;
     80  1.27  christos 
     81  1.27  christos #ifdef DEBUG
     82  1.27  christos extern FILE *tracefile;
     83  1.27  christos #endif
     84   1.1       jtc 
     85  1.56  christos static const char *fc_replace(const char *, char *, char *);
     86  1.56  christos static int not_fcnumber(const char *);
     87  1.56  christos static int str_to_event(const char *, int);
     88  1.56  christos static int comparator(const void *, const void *);
     89  1.56  christos static char **sh_matches(const char *, int, int);
     90  1.56  christos static unsigned char sh_complete(EditLine *, int);
     91  1.56  christos 
     92   1.1       jtc /*
     93   1.1       jtc  * Set history and editing status.  Called whenever the status may
     94   1.1       jtc  * have changed (figures out what to do).
     95   1.1       jtc  */
     96   1.5       cgd void
     97  1.27  christos histedit(void)
     98   1.4       cgd {
     99  1.27  christos 	FILE *el_err;
    100   1.1       jtc 
    101   1.1       jtc #define editing (Eflag || Vflag)
    102   1.1       jtc 
    103  1.65       kre 	CTRACE(DBG_HISTORY, ("histedit: %cE%cV %sinteractive\n",
    104  1.65       kre 	    Eflag ? '-' : '+',  Vflag ? '-' : '+', iflag ? "" : "not "));
    105  1.65       kre 
    106  1.37  christos 	if (iflag == 1) {
    107   1.1       jtc 		if (!hist) {
    108   1.1       jtc 			/*
    109   1.1       jtc 			 * turn history on
    110   1.1       jtc 			 */
    111   1.1       jtc 			INTOFF;
    112   1.1       jtc 			hist = history_init();
    113   1.1       jtc 			INTON;
    114   1.1       jtc 
    115   1.1       jtc 			if (hist != NULL)
    116  1.70       kre 				sethistsize(histsizeval(), histsizeflags());
    117   1.1       jtc 			else
    118   1.1       jtc 				out2str("sh: can't initialize history\n");
    119   1.1       jtc 		}
    120   1.1       jtc 		if (editing && !el && isatty(0)) { /* && isatty(2) ??? */
    121   1.1       jtc 			/*
    122   1.1       jtc 			 * turn editing on
    123   1.1       jtc 			 */
    124  1.59       kre 			char *term;
    125  1.32     lukem 
    126   1.1       jtc 			INTOFF;
    127   1.1       jtc 			if (el_in == NULL)
    128   1.1       jtc 				el_in = fdopen(0, "r");
    129   1.1       jtc 			if (el_out == NULL)
    130   1.1       jtc 				el_out = fdopen(2, "w");
    131   1.1       jtc 			if (el_in == NULL || el_out == NULL)
    132   1.1       jtc 				goto bad;
    133  1.27  christos 			el_err = el_out;
    134  1.27  christos #if DEBUG
    135  1.27  christos 			if (tracefile)
    136  1.27  christos 				el_err = tracefile;
    137  1.27  christos #endif
    138  1.58       kre 			/*
    139  1.58       kre 			 * This odd piece of code doesn't affect the shell
    140  1.58       kre 			 * at all, the environment modified here is the
    141  1.58       kre 			 * stuff accessed via "environ" (the incoming
    142  1.61    rillig 			 * environment to the shell) which is only ever
    143  1.58       kre 			 * touched at sh startup time (long before we get
    144  1.58       kre 			 * here) and ignored thereafter.
    145  1.58       kre 			 *
    146  1.58       kre 			 * But libedit calls getenv() to discover TERM
    147  1.58       kre 			 * and that searches the "environ" environment,
    148  1.58       kre 			 * not the shell's internal variable data struct,
    149  1.58       kre 			 * so we need to make sure that TERM in there is
    150  1.58       kre 			 * correct.
    151  1.58       kre 			 *
    152  1.58       kre 			 * This sequence copies TERM from the shell into
    153  1.58       kre 			 * the old "environ" environment.
    154  1.58       kre 			 */
    155  1.32     lukem 			term = lookupvar("TERM");
    156  1.32     lukem 			if (term)
    157  1.32     lukem 				setenv("TERM", term, 1);
    158  1.32     lukem 			else
    159  1.32     lukem 				unsetenv("TERM");
    160  1.59       kre 			el = el_init("sh", el_in, el_out, el_err);
    161  1.65       kre 			VTRACE(DBG_HISTORY, ("el_init() %sed\n",
    162  1.65       kre 			    el != NULL ? "succeed" : "fail"));
    163   1.1       jtc 			if (el != NULL) {
    164   1.1       jtc 				if (hist)
    165   1.1       jtc 					el_set(el, EL_HIST, history, hist);
    166  1.52       kre 
    167  1.70       kre 				set_prompt_lit(lookupvar("PSlit"), 0);
    168  1.33  christos 				el_set(el, EL_SIGNAL, 1);
    169  1.56  christos 				el_set(el, EL_SAFEREAD, 1);
    170  1.47  christos 				el_set(el, EL_ALIAS_TEXT, alias_text, NULL);
    171  1.36  christos 				el_set(el, EL_ADDFN, "rl-complete",
    172  1.36  christos 				    "ReadLine compatible completion function",
    173  1.56  christos 				    sh_complete);
    174   1.1       jtc 			} else {
    175  1.67       kre  bad:;
    176   1.1       jtc 				out2str("sh: can't initialize editing\n");
    177   1.1       jtc 			}
    178   1.1       jtc 			INTON;
    179   1.1       jtc 		} else if (!editing && el) {
    180   1.1       jtc 			INTOFF;
    181   1.1       jtc 			el_end(el);
    182   1.1       jtc 			el = NULL;
    183  1.65       kre 			VTRACE(DBG_HISTORY, ("line editing disabled\n"));
    184   1.1       jtc 			INTON;
    185   1.1       jtc 		}
    186   1.1       jtc 		if (el) {
    187  1.54       kre 			INTOFF;
    188   1.1       jtc 			if (Vflag)
    189   1.1       jtc 				el_set(el, EL_EDITOR, "vi");
    190   1.1       jtc 			else if (Eflag)
    191   1.1       jtc 				el_set(el, EL_EDITOR, "emacs");
    192  1.65       kre 			VTRACE(DBG_HISTORY, ("reading $EDITRC\n"));
    193  1.64       nia 			el_source(el, lookupvar("EDITRC"));
    194  1.66       kre 			el_set(el, EL_BIND, "^I",
    195  1.36  christos 			    tabcomplete ? "rl-complete" : "ed-insert", NULL);
    196  1.54       kre 			INTON;
    197   1.1       jtc 		}
    198   1.1       jtc 	} else {
    199   1.1       jtc 		INTOFF;
    200   1.1       jtc 		if (el) {	/* no editing if not interactive */
    201   1.1       jtc 			el_end(el);
    202   1.1       jtc 			el = NULL;
    203   1.1       jtc 		}
    204   1.1       jtc 		if (hist) {
    205   1.1       jtc 			history_end(hist);
    206   1.1       jtc 			hist = NULL;
    207   1.1       jtc 		}
    208   1.1       jtc 		INTON;
    209  1.65       kre 		VTRACE(DBG_HISTORY, ("line editing & history disabled\n"));
    210   1.1       jtc 	}
    211   1.1       jtc }
    212   1.1       jtc 
    213  1.50       kre void
    214  1.70       kre set_prompt_lit(char *lit_ch, int flags __unused)
    215  1.52       kre {
    216  1.52       kre 	wchar_t wc;
    217  1.52       kre 
    218  1.52       kre 	if (!(iflag && editing && el))
    219  1.52       kre 		return;
    220  1.52       kre 
    221  1.52       kre 	if (lit_ch == NULL) {
    222  1.52       kre 		el_set(el, EL_PROMPT, getprompt);
    223  1.52       kre 		return;
    224  1.52       kre 	}
    225  1.52       kre 
    226  1.52       kre 	mbtowc(&wc, NULL, 1);		/* state init */
    227  1.52       kre 
    228  1.54       kre 	INTOFF;
    229  1.52       kre 	if (mbtowc(&wc, lit_ch, strlen(lit_ch)) <= 0)
    230  1.52       kre 		el_set(el, EL_PROMPT, getprompt);
    231  1.52       kre 	else
    232  1.52       kre 		el_set(el, EL_PROMPT_ESC, getprompt, (int)wc);
    233  1.54       kre 	INTON;
    234  1.52       kre }
    235  1.52       kre 
    236  1.52       kre void
    237  1.70       kre set_editrc(char *fname, int flags)
    238  1.50       kre {
    239  1.54       kre 	INTOFF;
    240  1.70       kre 	if (iflag && editing && el && !(flags & VUNSET))
    241  1.50       kre 		el_source(el, fname);
    242  1.54       kre 	INTON;
    243  1.50       kre }
    244   1.4       cgd 
    245   1.4       cgd void
    246  1.70       kre sethistsize(char *hs, int flags)
    247   1.8  christos {
    248   1.1       jtc 	int histsize;
    249  1.16  christos 	HistEvent he;
    250   1.1       jtc 
    251  1.70       kre 	CTRACE(DBG_HISTORY, ("Set HISTSIZE=%s [%x] %s\n",
    252  1.70       kre 	    (hs == NULL ? "''" : hs), flags, "!hist" + (hist != NULL)));
    253  1.70       kre 
    254  1.70       kre 	if (hs != NULL && *hs != '\0' && (flags & VUNSAFE) && !is_number(hs))
    255  1.70       kre 		hs = NULL;
    256  1.70       kre 
    257  1.70       kre 	if (hs == NULL || *hs == '\0' || (flags & VUNSET) ||
    258  1.70       kre 	    (histsize = number(hs)) < 0)
    259  1.70       kre 		histsize = 100;
    260  1.70       kre 
    261   1.1       jtc 	if (hist != NULL) {
    262  1.54       kre 		INTOFF;
    263  1.19  christos 		history(hist, &he, H_SETSIZE, histsize);
    264  1.41     joerg 		history(hist, &he, H_SETUNIQUE, 1);
    265  1.54       kre 		INTON;
    266   1.1       jtc 	}
    267  1.13  christos }
    268  1.13  christos 
    269  1.13  christos void
    270  1.70       kre setterm(char *term, int flags __unused)
    271  1.13  christos {
    272  1.54       kre 	INTOFF;
    273  1.70       kre 	if (el != NULL && term != NULL && *term != '\0')
    274  1.13  christos 		if (el_set(el, EL_TERMINAL, term) != 0) {
    275  1.13  christos 			outfmt(out2, "sh: Can't set terminal type %s\n", term);
    276  1.13  christos 			outfmt(out2, "sh: Using dumb terminal settings.\n");
    277  1.13  christos 		}
    278  1.54       kre 	INTON;
    279   1.1       jtc }
    280   1.1       jtc 
    281  1.28  gmcgarry int
    282  1.45      matt inputrc(int argc, char **argv)
    283  1.28  gmcgarry {
    284  1.65       kre 	CTRACE(DBG_HISTORY, ("inputrc (%d arg%s)", argc-1, argc==2?"":"s"));
    285  1.28  gmcgarry 	if (argc != 2) {
    286  1.65       kre 		CTRACE(DBG_HISTORY, (" -- bad\n"));
    287  1.28  gmcgarry 		out2str("usage: inputrc file\n");
    288  1.28  gmcgarry 		return 1;
    289  1.28  gmcgarry 	}
    290  1.65       kre 	CTRACE(DBG_HISTORY, (" file: \"%s\"\n", argv[1]));
    291  1.28  gmcgarry 	if (el != NULL) {
    292  1.54       kre 		INTOFF;
    293  1.28  gmcgarry 		if (el_source(el, argv[1])) {
    294  1.54       kre 			INTON;
    295  1.28  gmcgarry 			out2str("inputrc: failed\n");
    296  1.28  gmcgarry 			return 1;
    297  1.54       kre 		}
    298  1.54       kre 		INTON;
    299  1.54       kre 		return 0;
    300  1.28  gmcgarry 	} else {
    301  1.28  gmcgarry 		out2str("sh: inputrc ignored, not editing\n");
    302  1.28  gmcgarry 		return 1;
    303  1.28  gmcgarry 	}
    304  1.28  gmcgarry }
    305  1.28  gmcgarry 
    306   1.1       jtc /*
    307   1.1       jtc  *  This command is provided since POSIX decided to standardize
    308   1.1       jtc  *  the Korn shell fc command.  Oh well...
    309   1.1       jtc  */
    310   1.4       cgd int
    311  1.48  christos histcmd(volatile int argc, char ** volatile argv)
    312   1.1       jtc {
    313   1.1       jtc 	int ch;
    314  1.40  christos 	const char * volatile editor = NULL;
    315  1.16  christos 	HistEvent he;
    316  1.48  christos 	volatile int lflg = 0, nflg = 0, rflg = 0, sflg = 0;
    317  1.16  christos 	int i, retval;
    318  1.21  christos 	const char *firststr, *laststr;
    319   1.1       jtc 	int first, last, direction;
    320  1.67       kre 
    321  1.67       kre 	char * volatile pat = NULL;	/* ksh "fc old=new" crap */
    322  1.67       kre 	char * volatile repl;
    323  1.67       kre 
    324   1.1       jtc 	static int active = 0;
    325   1.1       jtc 	struct jmploc jmploc;
    326   1.1       jtc 	struct jmploc *volatile savehandler;
    327   1.1       jtc 	char editfile[MAXPATHLEN + 1];
    328  1.48  christos 	FILE * volatile efp;
    329  1.67       kre 
    330   1.8  christos #ifdef __GNUC__
    331  1.39       mrg 	repl = NULL;	/* XXX gcc4 */
    332  1.39       mrg 	efp = NULL;	/* XXX gcc4 */
    333   1.8  christos #endif
    334   1.1       jtc 
    335   1.1       jtc 	if (hist == NULL)
    336   1.1       jtc 		error("history not active");
    337  1.10  christos 
    338  1.65       kre 	CTRACE(DBG_HISTORY, ("histcmd (fc) %d arg%s\n", argc, argc==1?"":"s"));
    339   1.1       jtc 	if (argc == 1)
    340   1.1       jtc 		error("missing history argument");
    341   1.1       jtc 
    342   1.1       jtc 	optreset = 1; optind = 1; /* initialize getopt */
    343   1.1       jtc 	while (not_fcnumber(argv[optind]) &&
    344  1.15     lukem 	      (ch = getopt(argc, argv, ":e:lnrs")) != -1)
    345   1.1       jtc 		switch ((char)ch) {
    346   1.1       jtc 		case 'e':
    347  1.60       kre 			editor = optarg;
    348  1.65       kre 			VTRACE(DBG_HISTORY, ("histcmd -e %s\n", editor));
    349   1.1       jtc 			break;
    350   1.1       jtc 		case 'l':
    351   1.1       jtc 			lflg = 1;
    352  1.65       kre 			VTRACE(DBG_HISTORY, ("histcmd -l\n"));
    353   1.1       jtc 			break;
    354   1.1       jtc 		case 'n':
    355   1.1       jtc 			nflg = 1;
    356  1.65       kre 			VTRACE(DBG_HISTORY, ("histcmd -n\n"));
    357   1.1       jtc 			break;
    358   1.1       jtc 		case 'r':
    359   1.1       jtc 			rflg = 1;
    360  1.65       kre 			VTRACE(DBG_HISTORY, ("histcmd -r\n"));
    361   1.1       jtc 			break;
    362   1.1       jtc 		case 's':
    363   1.1       jtc 			sflg = 1;
    364  1.65       kre 			VTRACE(DBG_HISTORY, ("histcmd -s\n"));
    365   1.1       jtc 			break;
    366   1.1       jtc 		case ':':
    367   1.1       jtc 			error("option -%c expects argument", optopt);
    368  1.20   mycroft 			/* NOTREACHED */
    369   1.1       jtc 		case '?':
    370   1.1       jtc 		default:
    371   1.1       jtc 			error("unknown option: -%c", optopt);
    372  1.20   mycroft 			/* NOTREACHED */
    373   1.1       jtc 		}
    374   1.1       jtc 	argc -= optind, argv += optind;
    375   1.1       jtc 
    376   1.1       jtc 	/*
    377   1.1       jtc 	 * If executing...
    378   1.1       jtc 	 */
    379   1.1       jtc 	if (lflg == 0 || editor || sflg) {
    380   1.1       jtc 		lflg = 0;	/* ignore */
    381   1.1       jtc 		editfile[0] = '\0';
    382   1.1       jtc 		/*
    383   1.1       jtc 		 * Catch interrupts to reset active counter and
    384   1.1       jtc 		 * cleanup temp files.
    385   1.1       jtc 		 */
    386  1.44     joerg 		savehandler = handler;
    387   1.1       jtc 		if (setjmp(jmploc.loc)) {
    388   1.1       jtc 			active = 0;
    389  1.65       kre 			if (*editfile) {
    390  1.65       kre 				VTRACE(DBG_HISTORY,
    391  1.65       kre 				    ("histcmd err jump unlink temp \"%s\"\n",
    392  1.69       kre 				    editfile));
    393   1.1       jtc 				unlink(editfile);
    394  1.65       kre 			}
    395   1.1       jtc 			handler = savehandler;
    396   1.1       jtc 			longjmp(handler->loc, 1);
    397   1.1       jtc 		}
    398   1.1       jtc 		handler = &jmploc;
    399  1.69       kre 		VTRACE(DBG_HISTORY, ("histcmd is active %d(++)\n", active));
    400   1.1       jtc 		if (++active > MAXHISTLOOPS) {
    401   1.1       jtc 			active = 0;
    402   1.1       jtc 			displayhist = 0;
    403   1.1       jtc 			error("called recursively too many times");
    404   1.1       jtc 		}
    405   1.1       jtc 		/*
    406   1.1       jtc 		 * Set editor.
    407   1.1       jtc 		 */
    408   1.1       jtc 		if (sflg == 0) {
    409   1.1       jtc 			if (editor == NULL &&
    410   1.1       jtc 			    (editor = bltinlookup("FCEDIT", 1)) == NULL &&
    411   1.1       jtc 			    (editor = bltinlookup("EDITOR", 1)) == NULL)
    412   1.1       jtc 				editor = DEFEDITOR;
    413   1.1       jtc 			if (editor[0] == '-' && editor[1] == '\0') {
    414   1.1       jtc 				sflg = 1;	/* no edit */
    415   1.1       jtc 				editor = NULL;
    416   1.1       jtc 			}
    417  1.65       kre 			VTRACE(DBG_HISTORY, ("histcmd using %s as editor\n",
    418  1.65       kre 			    editor == NULL ? "-nothing-" : editor));
    419   1.1       jtc 		}
    420   1.1       jtc 	}
    421   1.1       jtc 
    422   1.1       jtc 	/*
    423   1.1       jtc 	 * If executing, parse [old=new] now
    424   1.1       jtc 	 */
    425  1.10  christos 	if (lflg == 0 && argc > 0 &&
    426   1.1       jtc 	     ((repl = strchr(argv[0], '=')) != NULL)) {
    427   1.1       jtc 		pat = argv[0];
    428   1.1       jtc 		*repl++ = '\0';
    429   1.1       jtc 		argc--, argv++;
    430  1.65       kre 		VTRACE(DBG_HISTORY, ("histcmd replace old=\"%s\" new=\"%s\""
    431  1.65       kre 		    " (%d args)\n", pat, repl, argc));
    432   1.1       jtc 	}
    433  1.38   aymeric 
    434  1.38   aymeric 	/*
    435  1.38   aymeric 	 * If -s is specified, accept only one operand
    436  1.38   aymeric 	 */
    437  1.38   aymeric 	if (sflg && argc >= 2)
    438  1.38   aymeric 		error("too many args");
    439  1.38   aymeric 
    440   1.1       jtc 	/*
    441   1.1       jtc 	 * determine [first] and [last]
    442   1.1       jtc 	 */
    443   1.1       jtc 	switch (argc) {
    444   1.1       jtc 	case 0:
    445  1.69       kre 		if (lflg) {
    446  1.69       kre 			firststr = "-16";
    447  1.69       kre 			laststr = "-1";
    448  1.69       kre 		} else
    449  1.69       kre 			firststr = laststr = "-1";
    450   1.1       jtc 		break;
    451   1.1       jtc 	case 1:
    452   1.1       jtc 		firststr = argv[0];
    453   1.1       jtc 		laststr = lflg ? "-1" : argv[0];
    454   1.1       jtc 		break;
    455   1.1       jtc 	case 2:
    456   1.1       jtc 		firststr = argv[0];
    457   1.1       jtc 		laststr = argv[1];
    458   1.1       jtc 		break;
    459   1.1       jtc 	default:
    460   1.1       jtc 		error("too many args");
    461  1.20   mycroft 		/* NOTREACHED */
    462   1.1       jtc 	}
    463   1.1       jtc 	/*
    464   1.1       jtc 	 * Turn into event numbers.
    465   1.1       jtc 	 */
    466   1.1       jtc 	first = str_to_event(firststr, 0);
    467   1.1       jtc 	last = str_to_event(laststr, 1);
    468   1.1       jtc 
    469  1.69       kre 	if (first == -1 || last == -1) {
    470  1.69       kre 		if (lflg)   /* no history exists, that's OK */
    471  1.69       kre 			return 0;
    472  1.69       kre 		if (first == -1 && last == -1) {
    473  1.69       kre 			if (firststr != laststr)
    474  1.69       kre 				error("history events %s to %s do not exist",
    475  1.69       kre 				    firststr, laststr);
    476  1.69       kre 			else
    477  1.69       kre 				error("history event %s does not exist",
    478  1.69       kre 				    firststr);
    479  1.69       kre 		} else {
    480  1.69       kre 			error("history event %s does not exist",
    481  1.69       kre 			    first == -1 ? firststr : laststr);
    482  1.69       kre 		}
    483  1.69       kre 	}
    484  1.69       kre 
    485   1.1       jtc 	if (rflg) {
    486   1.1       jtc 		i = last;
    487   1.1       jtc 		last = first;
    488   1.1       jtc 		first = i;
    489   1.1       jtc 	}
    490  1.65       kre 	VTRACE(DBG_HISTORY, ("histcmd%s first=\"%s\" (#%d) last=\"%s\" (#%d)\n",
    491  1.65       kre 	    rflg ? " reversed" : "", rflg ? laststr : firststr, first,
    492  1.65       kre 	    rflg ? firststr : laststr, last));
    493  1.65       kre 
    494   1.1       jtc 	/*
    495   1.1       jtc 	 * XXX - this should not depend on the event numbers
    496  1.10  christos 	 * always increasing.  Add sequence numbers or offset
    497   1.1       jtc 	 * to the history element in next (diskbased) release.
    498   1.1       jtc 	 */
    499   1.1       jtc 	direction = first < last ? H_PREV : H_NEXT;
    500   1.1       jtc 
    501   1.1       jtc 	/*
    502   1.1       jtc 	 * If editing, grab a temp file.
    503   1.1       jtc 	 */
    504   1.1       jtc 	if (editor) {
    505   1.1       jtc 		int fd;
    506  1.67       kre 
    507   1.1       jtc 		INTOFF;		/* easier */
    508  1.69       kre 		snprintf(editfile, sizeof(editfile),
    509  1.69       kre 		    "%s_shXXXXXX", _PATH_TMP);
    510   1.1       jtc 		if ((fd = mkstemp(editfile)) < 0)
    511   1.1       jtc 			error("can't create temporary file %s", editfile);
    512   1.1       jtc 		if ((efp = fdopen(fd, "w")) == NULL) {
    513   1.1       jtc 			close(fd);
    514  1.10  christos 			error("can't allocate stdio buffer for temp");
    515   1.1       jtc 		}
    516  1.65       kre 		VTRACE(DBG_HISTORY, ("histcmd created \"%s\" for edit buffer"
    517  1.65       kre 		    " fd=%d\n", editfile, fd));
    518   1.1       jtc 	}
    519   1.1       jtc 
    520   1.1       jtc 	/*
    521   1.1       jtc 	 * Loop through selected history events.  If listing or executing,
    522   1.1       jtc 	 * do it now.  Otherwise, put into temp file and call the editor
    523   1.1       jtc 	 * after.
    524   1.1       jtc 	 *
    525   1.1       jtc 	 * The history interface needs rethinking, as the following
    526   1.1       jtc 	 * convolutions will demonstrate.
    527   1.1       jtc 	 */
    528  1.16  christos 	history(hist, &he, H_FIRST);
    529  1.16  christos 	retval = history(hist, &he, H_NEXT_EVENT, first);
    530  1.67       kre 	for ( ; retval != -1; retval = history(hist, &he, direction)) {
    531   1.1       jtc 		if (lflg) {
    532   1.1       jtc 			if (!nflg)
    533  1.16  christos 				out1fmt("%5d ", he.num);
    534  1.16  christos 			out1str(he.str);
    535   1.1       jtc 		} else {
    536  1.21  christos 			const char *s = pat ?
    537  1.21  christos 			   fc_replace(he.str, pat, repl) : he.str;
    538   1.1       jtc 
    539   1.1       jtc 			if (sflg) {
    540  1.65       kre 				VTRACE(DBG_HISTORY, ("histcmd -s \"%s\"\n", s));
    541   1.1       jtc 				if (displayhist) {
    542   1.1       jtc 					out2str(s);
    543   1.1       jtc 				}
    544  1.21  christos 
    545  1.67       kre 				evalstring(strcpy(stalloc(strlen(s)+1), s), 0);
    546   1.1       jtc 				if (displayhist && hist) {
    547   1.1       jtc 					/*
    548  1.10  christos 					 *  XXX what about recursive and
    549   1.1       jtc 					 *  relative histnums.
    550   1.1       jtc 					 */
    551  1.16  christos 					history(hist, &he, H_ENTER, s);
    552   1.1       jtc 				}
    553  1.38   aymeric 
    554  1.38   aymeric 				break;
    555   1.1       jtc 			} else
    556   1.1       jtc 				fputs(s, efp);
    557   1.1       jtc 		}
    558   1.1       jtc 		/*
    559  1.16  christos 		 * At end?  (if we were to lose last, we'd sure be
    560   1.1       jtc 		 * messed up).
    561   1.1       jtc 		 */
    562  1.16  christos 		if (he.num == last)
    563   1.1       jtc 			break;
    564   1.1       jtc 	}
    565   1.1       jtc 	if (editor) {
    566   1.1       jtc 		char *editcmd;
    567  1.46  dholland 		size_t cmdlen;
    568   1.1       jtc 
    569   1.1       jtc 		fclose(efp);
    570  1.46  dholland 		cmdlen = strlen(editor) + strlen(editfile) + 2;
    571  1.46  dholland 		editcmd = stalloc(cmdlen);
    572  1.46  dholland 		snprintf(editcmd, cmdlen, "%s %s", editor, editfile);
    573  1.65       kre 		VTRACE(DBG_HISTORY, ("histcmd editing: \"%s\"\n", editcmd));
    574  1.22  christos 		evalstring(editcmd, 0);	/* XXX - should use no JC command */
    575  1.54       kre 		stunalloc(editcmd);
    576  1.65       kre 		VTRACE(DBG_HISTORY, ("histcmd read cmds from %s\n", editfile));
    577   1.1       jtc 		readcmdfile(editfile);	/* XXX - should read back - quick tst */
    578  1.65       kre 		VTRACE(DBG_HISTORY, ("histcmd unlink %s\n", editfile));
    579   1.1       jtc 		unlink(editfile);
    580  1.69       kre 		editfile[0] = '\0';
    581  1.54       kre 		INTON;
    582   1.1       jtc 	}
    583  1.10  christos 
    584   1.1       jtc 	if (lflg == 0 && active > 0)
    585   1.1       jtc 		--active;
    586   1.1       jtc 	if (displayhist)
    587   1.1       jtc 		displayhist = 0;
    588   1.8  christos 	return 0;
    589   1.1       jtc }
    590   1.1       jtc 
    591  1.56  christos static const char *
    592  1.27  christos fc_replace(const char *s, char *p, char *r)
    593   1.1       jtc {
    594   1.1       jtc 	char *dest;
    595   1.1       jtc 	int plen = strlen(p);
    596   1.1       jtc 
    597  1.65       kre 	VTRACE(DBG_HISTORY, ("histcmd s/%s/%s/ in \"%s\" -> ", p, r, s));
    598   1.1       jtc 	STARTSTACKSTR(dest);
    599   1.1       jtc 	while (*s) {
    600   1.1       jtc 		if (*s == *p && strncmp(s, p, plen) == 0) {
    601   1.1       jtc 			while (*r)
    602   1.1       jtc 				STPUTC(*r++, dest);
    603   1.1       jtc 			s += plen;
    604   1.1       jtc 			*p = '\0';	/* so no more matches */
    605   1.1       jtc 		} else
    606   1.1       jtc 			STPUTC(*s++, dest);
    607   1.1       jtc 	}
    608   1.1       jtc 	STACKSTRNUL(dest);
    609   1.1       jtc 	dest = grabstackstr(dest);
    610  1.65       kre 	VTRACE(DBG_HISTORY, ("\"%s\"\n", dest));
    611   1.1       jtc 
    612  1.56  christos 	return dest;
    613  1.56  christos }
    614  1.56  christos 
    615  1.56  christos 
    616  1.56  christos /*
    617  1.56  christos  * Comparator function for qsort(). The use of curpos here is to skip
    618  1.56  christos  * characters that we already know to compare equal (common prefix).
    619  1.56  christos  */
    620  1.56  christos static int
    621  1.56  christos comparator(const void *a, const void *b)
    622  1.56  christos {
    623  1.56  christos 	return strcmp(*(char *const *)a + curpos,
    624  1.56  christos 		*(char *const *)b + curpos);
    625  1.56  christos }
    626  1.56  christos 
    627  1.56  christos /*
    628  1.56  christos  * This function is passed to libedit's fn_complete(). The library will
    629  1.56  christos  * use it instead of its standard function to find matches, which
    630  1.56  christos  * searches for files in current directory. If we're at the start of the
    631  1.56  christos  * line, we want to look for available commands from all paths in $PATH.
    632  1.56  christos  */
    633  1.67       kre static char **
    634  1.67       kre sh_matches(const char *text, int start, int end)
    635  1.56  christos {
    636  1.56  christos 	char *free_path = NULL, *dirname, *path;
    637  1.56  christos 	char **matches = NULL;
    638  1.56  christos 	size_t i = 0, size = 16;
    639  1.56  christos 
    640  1.56  christos 	if (start > 0)
    641  1.56  christos 		return NULL;
    642  1.56  christos 	curpos = end - start;
    643  1.56  christos 	if ((free_path = path = strdup(pathval())) == NULL)
    644  1.56  christos 		goto out;
    645  1.56  christos 	if ((matches = malloc(size * sizeof(matches[0]))) == NULL)
    646  1.56  christos 		goto out;
    647  1.56  christos 	while ((dirname = strsep(&path, ":")) != NULL) {
    648  1.56  christos 		struct dirent *entry;
    649  1.56  christos 		DIR *dir;
    650  1.56  christos 		int dfd;
    651  1.56  christos 
    652  1.56  christos 		if ((dir = opendir(dirname)) == NULL)
    653  1.56  christos 			continue;
    654  1.56  christos 		if ((dfd = dirfd(dir)) == -1)
    655  1.56  christos 			continue;
    656  1.56  christos 		while ((entry = readdir(dir)) != NULL) {
    657  1.56  christos 			struct stat statb;
    658  1.56  christos 
    659  1.56  christos 			if (strncmp(entry->d_name, text, curpos) != 0)
    660  1.56  christos 				continue;
    661  1.67       kre 			if (entry->d_type == DT_UNKNOWN ||
    662  1.67       kre 			    entry->d_type == DT_LNK) {
    663  1.67       kre 				if (fstatat(dfd, entry->d_name, &statb, 0)
    664  1.67       kre 				    == -1)
    665  1.56  christos 					continue;
    666  1.56  christos 				if (!S_ISREG(statb.st_mode))
    667  1.56  christos 					continue;
    668  1.56  christos 			} else if (entry->d_type != DT_REG)
    669  1.56  christos 				continue;
    670  1.56  christos 			if (++i >= size - 1) {
    671  1.56  christos 				size *= 2;
    672  1.56  christos 				if (reallocarr(&matches, size,
    673  1.56  christos 				    sizeof(*matches)))
    674  1.56  christos 				{
    675  1.56  christos 					closedir(dir);
    676  1.56  christos 					goto out;
    677  1.56  christos 				}
    678  1.56  christos 			}
    679  1.56  christos 			matches[i] = strdup(entry->d_name);
    680  1.56  christos 		}
    681  1.56  christos 		closedir(dir);
    682  1.56  christos 	}
    683  1.67       kre  out:;
    684  1.56  christos 	free(free_path);
    685  1.56  christos 	if (i == 0) {
    686  1.56  christos 		free(matches);
    687  1.56  christos 		return NULL;
    688  1.56  christos 	}
    689  1.56  christos 	if (i == 1) {
    690  1.56  christos 		matches[0] = strdup(matches[1]);
    691  1.56  christos 		matches[i + 1] = NULL;
    692  1.56  christos 	} else {
    693  1.56  christos 		size_t j, k;
    694  1.56  christos 
    695  1.56  christos 		qsort(matches + 1, i, sizeof(matches[0]), comparator);
    696  1.56  christos 		for (j = 1, k = 2; k <= i; k++)
    697  1.56  christos 			if (strcmp(matches[j] + curpos, matches[k] + curpos)
    698  1.56  christos 			    == 0)
    699  1.56  christos 				free(matches[k]);
    700  1.56  christos 			else
    701  1.56  christos 				matches[++j] = matches[k];
    702  1.56  christos 		matches[0] = strdup(text);
    703  1.56  christos 		matches[j + 1] = NULL;
    704  1.56  christos 	}
    705  1.56  christos 	return matches;
    706  1.56  christos }
    707  1.56  christos 
    708  1.56  christos /*
    709  1.56  christos  * This is passed to el_set(el, EL_ADDFN, ...) so that it's possible to
    710  1.56  christos  * bind a key (tab by default) to execute the function.
    711  1.56  christos  */
    712  1.56  christos unsigned char
    713  1.56  christos sh_complete(EditLine *sel, int ch __unused)
    714  1.56  christos {
    715  1.57  christos 	return (unsigned char)fn_complete2(sel, NULL, sh_matches,
    716  1.56  christos 		L" \t\n\"\\'`@$><=;|&{(", NULL, NULL, (size_t)100,
    717  1.57  christos 		NULL, &((int) {0}), NULL, NULL, FN_QUOTE_MATCH);
    718   1.1       jtc }
    719   1.1       jtc 
    720  1.56  christos static int
    721  1.56  christos not_fcnumber(const char *s)
    722   1.1       jtc {
    723   1.7  christos 	if (s == NULL)
    724   1.7  christos 		return 0;
    725  1.67       kre 	if (*s == '-')
    726  1.67       kre 		s++;
    727  1.56  christos 	return !is_number(s);
    728   1.1       jtc }
    729   1.1       jtc 
    730  1.56  christos static int
    731  1.27  christos str_to_event(const char *str, int last)
    732   1.1       jtc {
    733  1.16  christos 	HistEvent he;
    734  1.21  christos 	const char *s = str;
    735   1.1       jtc 	int relative = 0;
    736  1.16  christos 	int i, retval;
    737   1.1       jtc 
    738  1.16  christos 	retval = history(hist, &he, H_FIRST);
    739   1.1       jtc 	switch (*s) {
    740   1.1       jtc 	case '-':
    741   1.1       jtc 		relative = 1;
    742   1.1       jtc 		/*FALLTHROUGH*/
    743   1.1       jtc 	case '+':
    744   1.1       jtc 		s++;
    745   1.1       jtc 	}
    746   1.1       jtc 	if (is_number(s)) {
    747  1.53       kre 		i = number(s);
    748   1.1       jtc 		if (relative) {
    749  1.16  christos 			while (retval != -1 && i--) {
    750  1.16  christos 				retval = history(hist, &he, H_NEXT);
    751   1.1       jtc 			}
    752  1.16  christos 			if (retval == -1)
    753  1.16  christos 				retval = history(hist, &he, H_LAST);
    754   1.1       jtc 		} else {
    755  1.16  christos 			retval = history(hist, &he, H_NEXT_EVENT, i);
    756  1.16  christos 			if (retval == -1) {
    757   1.1       jtc 				/*
    758   1.1       jtc 				 * the notion of first and last is
    759   1.1       jtc 				 * backwards to that of the history package
    760   1.1       jtc 				 */
    761  1.16  christos 				retval = history(hist, &he,
    762  1.16  christos 						last ? H_FIRST : H_LAST);
    763   1.1       jtc 			}
    764   1.1       jtc 		}
    765  1.16  christos 		if (retval == -1)
    766  1.69       kre 			return -1;
    767   1.1       jtc 	} else {
    768   1.1       jtc 		/*
    769  1.10  christos 		 * pattern
    770   1.1       jtc 		 */
    771  1.16  christos 		retval = history(hist, &he, H_PREV_STR, str);
    772  1.16  christos 		if (retval == -1)
    773   1.1       jtc 			error("history pattern not found: %s", str);
    774   1.1       jtc 	}
    775  1.56  christos 	return he.num;
    776   1.1       jtc }
    777  1.67       kre 
    778  1.67       kre #else	/* defined(SMALL) */
    779  1.67       kre 
    780  1.11  christos int
    781  1.27  christos histcmd(int argc, char **argv)
    782  1.28  gmcgarry {
    783  1.28  gmcgarry 	error("not compiled with history support");
    784  1.28  gmcgarry 	/* NOTREACHED */
    785  1.28  gmcgarry }
    786  1.67       kre 
    787  1.28  gmcgarry int
    788  1.28  gmcgarry inputrc(int argc, char **argv)
    789  1.11  christos {
    790  1.11  christos 	error("not compiled with history support");
    791  1.20   mycroft 	/* NOTREACHED */
    792  1.11  christos }
    793  1.67       kre 
    794  1.67       kre #endif	/* SMALL */
    795