Home | History | Annotate | Line # | Download | only in sh
histedit.c revision 1.16
      1  1.16  christos /*	$NetBSD: histedit.c,v 1.16 1997/10/14 15:06:42 christos 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.1       jtc  * 3. All advertising materials mentioning features or use of this software
     19   1.1       jtc  *    must display the following acknowledgement:
     20   1.1       jtc  *	This product includes software developed by the University of
     21   1.1       jtc  *	California, Berkeley and its contributors.
     22   1.1       jtc  * 4. Neither the name of the University nor the names of its contributors
     23   1.1       jtc  *    may be used to endorse or promote products derived from this software
     24   1.1       jtc  *    without specific prior written permission.
     25   1.1       jtc  *
     26   1.1       jtc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27   1.1       jtc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28   1.1       jtc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29   1.1       jtc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30   1.1       jtc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31   1.1       jtc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32   1.1       jtc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33   1.1       jtc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34   1.1       jtc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35   1.1       jtc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36   1.1       jtc  * SUCH DAMAGE.
     37   1.1       jtc  */
     38   1.1       jtc 
     39  1.14  christos #include <sys/cdefs.h>
     40   1.1       jtc #ifndef lint
     41   1.6       cgd #if 0
     42   1.8  christos static char sccsid[] = "@(#)histedit.c	8.2 (Berkeley) 5/4/95";
     43   1.6       cgd #else
     44  1.16  christos __RCSID("$NetBSD: histedit.c,v 1.16 1997/10/14 15:06:42 christos Exp $");
     45   1.6       cgd #endif
     46   1.1       jtc #endif /* not lint */
     47   1.1       jtc 
     48   1.1       jtc #include <sys/param.h>
     49   1.1       jtc #include <paths.h>
     50   1.1       jtc #include <stdio.h>
     51   1.2       jtc #include <stdlib.h>
     52   1.2       jtc #include <unistd.h>
     53   1.8  christos /*
     54   1.8  christos  * Editline and history functions (and glue).
     55   1.8  christos  */
     56   1.1       jtc #include "shell.h"
     57   1.1       jtc #include "parser.h"
     58   1.1       jtc #include "var.h"
     59   1.1       jtc #include "options.h"
     60   1.8  christos #include "main.h"
     61   1.4       cgd #include "output.h"
     62   1.1       jtc #include "mystring.h"
     63  1.12  christos #ifndef SMALL
     64   1.8  christos #include "myhistedit.h"
     65   1.1       jtc #include "error.h"
     66   1.5       cgd #include "eval.h"
     67   1.1       jtc #include "memalloc.h"
     68   1.1       jtc 
     69   1.1       jtc #define MAXHISTLOOPS	4	/* max recursions through fc */
     70   1.1       jtc #define DEFEDITOR	"ed"	/* default editor *should* be $EDITOR */
     71   1.1       jtc 
     72   1.1       jtc History *hist;	/* history cookie */
     73   1.1       jtc EditLine *el;	/* editline cookie */
     74   1.1       jtc int displayhist;
     75   1.1       jtc static FILE *el_in, *el_out;
     76   1.1       jtc 
     77   1.1       jtc STATIC char *fc_replace __P((const char *, char *, char *));
     78   1.1       jtc 
     79   1.1       jtc /*
     80   1.1       jtc  * Set history and editing status.  Called whenever the status may
     81   1.1       jtc  * have changed (figures out what to do).
     82   1.1       jtc  */
     83   1.5       cgd void
     84  1.10  christos histedit()
     85   1.4       cgd {
     86   1.1       jtc 
     87   1.1       jtc #define editing (Eflag || Vflag)
     88   1.1       jtc 
     89   1.1       jtc 	if (iflag) {
     90   1.1       jtc 		if (!hist) {
     91   1.1       jtc 			/*
     92   1.1       jtc 			 * turn history on
     93   1.1       jtc 			 */
     94   1.1       jtc 			INTOFF;
     95   1.1       jtc 			hist = history_init();
     96   1.1       jtc 			INTON;
     97   1.1       jtc 
     98   1.1       jtc 			if (hist != NULL)
     99   1.9  christos 				sethistsize(histsizeval());
    100   1.1       jtc 			else
    101   1.1       jtc 				out2str("sh: can't initialize history\n");
    102   1.1       jtc 		}
    103   1.1       jtc 		if (editing && !el && isatty(0)) { /* && isatty(2) ??? */
    104   1.1       jtc 			/*
    105   1.1       jtc 			 * turn editing on
    106   1.1       jtc 			 */
    107   1.1       jtc 			INTOFF;
    108   1.1       jtc 			if (el_in == NULL)
    109   1.1       jtc 				el_in = fdopen(0, "r");
    110   1.1       jtc 			if (el_out == NULL)
    111   1.1       jtc 				el_out = fdopen(2, "w");
    112   1.1       jtc 			if (el_in == NULL || el_out == NULL)
    113   1.1       jtc 				goto bad;
    114   1.1       jtc 			el = el_init(arg0, el_in, el_out);
    115   1.1       jtc 			if (el != NULL) {
    116   1.1       jtc 				if (hist)
    117   1.1       jtc 					el_set(el, EL_HIST, history, hist);
    118   1.1       jtc 				el_set(el, EL_PROMPT, getprompt);
    119   1.1       jtc 			} else {
    120   1.1       jtc bad:
    121   1.1       jtc 				out2str("sh: can't initialize editing\n");
    122   1.1       jtc 			}
    123   1.1       jtc 			INTON;
    124   1.1       jtc 		} else if (!editing && el) {
    125   1.1       jtc 			INTOFF;
    126   1.1       jtc 			el_end(el);
    127   1.1       jtc 			el = NULL;
    128   1.1       jtc 			INTON;
    129   1.1       jtc 		}
    130   1.1       jtc 		if (el) {
    131   1.1       jtc 			if (Vflag)
    132   1.1       jtc 				el_set(el, EL_EDITOR, "vi");
    133   1.1       jtc 			else if (Eflag)
    134   1.1       jtc 				el_set(el, EL_EDITOR, "emacs");
    135   1.1       jtc 		}
    136   1.1       jtc 	} else {
    137   1.1       jtc 		INTOFF;
    138   1.1       jtc 		if (el) {	/* no editing if not interactive */
    139   1.1       jtc 			el_end(el);
    140   1.1       jtc 			el = NULL;
    141   1.1       jtc 		}
    142   1.1       jtc 		if (hist) {
    143   1.1       jtc 			history_end(hist);
    144   1.1       jtc 			hist = NULL;
    145   1.1       jtc 		}
    146   1.1       jtc 		INTON;
    147   1.1       jtc 	}
    148   1.1       jtc }
    149   1.1       jtc 
    150   1.4       cgd 
    151   1.4       cgd void
    152   1.9  christos sethistsize(hs)
    153   1.9  christos 	const char *hs;
    154   1.8  christos {
    155   1.1       jtc 	int histsize;
    156  1.16  christos 	HistEvent he;
    157   1.1       jtc 
    158   1.1       jtc 	if (hist != NULL) {
    159  1.10  christos 		if (hs == NULL || *hs == '\0' ||
    160   1.9  christos 		   (histsize = atoi(hs)) < 0)
    161   1.1       jtc 			histsize = 100;
    162  1.16  christos 		history(hist, &he, H_SETMAXSIZE, histsize);
    163   1.1       jtc 	}
    164  1.13  christos }
    165  1.13  christos 
    166  1.13  christos void
    167  1.13  christos setterm(term)
    168  1.13  christos 	const char *term;
    169  1.13  christos {
    170  1.13  christos 	if (el != NULL && term != NULL)
    171  1.13  christos 		if (el_set(el, EL_TERMINAL, term) != 0) {
    172  1.13  christos 			outfmt(out2, "sh: Can't set terminal type %s\n", term);
    173  1.13  christos 			outfmt(out2, "sh: Using dumb terminal settings.\n");
    174  1.13  christos 		}
    175   1.1       jtc }
    176   1.1       jtc 
    177   1.1       jtc /*
    178   1.1       jtc  *  This command is provided since POSIX decided to standardize
    179   1.1       jtc  *  the Korn shell fc command.  Oh well...
    180   1.1       jtc  */
    181   1.4       cgd int
    182   1.1       jtc histcmd(argc, argv)
    183   1.4       cgd 	int argc;
    184   1.8  christos 	char **argv;
    185   1.1       jtc {
    186   1.1       jtc 	extern char *optarg;
    187   1.1       jtc 	extern int optind, optopt, optreset;
    188   1.1       jtc 	int ch;
    189   1.1       jtc 	char *editor = NULL;
    190  1.16  christos 	HistEvent he;
    191   1.1       jtc 	int lflg = 0, nflg = 0, rflg = 0, sflg = 0;
    192  1.16  christos 	int i, retval;
    193   1.1       jtc 	char *firststr, *laststr;
    194   1.1       jtc 	int first, last, direction;
    195   1.1       jtc 	char *pat = NULL, *repl;	/* ksh "fc old=new" crap */
    196   1.1       jtc 	static int active = 0;
    197   1.1       jtc 	struct jmploc jmploc;
    198   1.1       jtc 	struct jmploc *volatile savehandler;
    199   1.1       jtc 	char editfile[MAXPATHLEN + 1];
    200   1.1       jtc 	FILE *efp;
    201   1.8  christos #ifdef __GNUC__
    202   1.8  christos 	/* Avoid longjmp clobbering */
    203   1.8  christos 	(void) &editor;
    204   1.8  christos 	(void) &lflg;
    205   1.8  christos 	(void) &nflg;
    206   1.8  christos 	(void) &rflg;
    207   1.8  christos 	(void) &sflg;
    208   1.8  christos 	(void) &firststr;
    209   1.8  christos 	(void) &laststr;
    210   1.8  christos 	(void) &pat;
    211   1.8  christos 	(void) &repl;
    212   1.8  christos 	(void) &efp;
    213   1.8  christos 	(void) &argc;
    214   1.8  christos 	(void) &argv;
    215   1.8  christos #endif
    216   1.1       jtc 
    217   1.1       jtc 	if (hist == NULL)
    218   1.1       jtc 		error("history not active");
    219  1.10  christos 
    220   1.1       jtc 	if (argc == 1)
    221   1.1       jtc 		error("missing history argument");
    222   1.1       jtc 
    223   1.1       jtc 	optreset = 1; optind = 1; /* initialize getopt */
    224   1.1       jtc 	while (not_fcnumber(argv[optind]) &&
    225  1.15     lukem 	      (ch = getopt(argc, argv, ":e:lnrs")) != -1)
    226   1.1       jtc 		switch ((char)ch) {
    227   1.1       jtc 		case 'e':
    228   1.1       jtc 			editor = optarg;
    229   1.1       jtc 			break;
    230   1.1       jtc 		case 'l':
    231   1.1       jtc 			lflg = 1;
    232   1.1       jtc 			break;
    233   1.1       jtc 		case 'n':
    234   1.1       jtc 			nflg = 1;
    235   1.1       jtc 			break;
    236   1.1       jtc 		case 'r':
    237   1.1       jtc 			rflg = 1;
    238   1.1       jtc 			break;
    239   1.1       jtc 		case 's':
    240   1.1       jtc 			sflg = 1;
    241   1.1       jtc 			break;
    242   1.1       jtc 		case ':':
    243   1.1       jtc 			error("option -%c expects argument", optopt);
    244   1.1       jtc 		case '?':
    245   1.1       jtc 		default:
    246   1.1       jtc 			error("unknown option: -%c", optopt);
    247   1.1       jtc 		}
    248   1.1       jtc 	argc -= optind, argv += optind;
    249   1.1       jtc 
    250   1.1       jtc 	/*
    251   1.1       jtc 	 * If executing...
    252   1.1       jtc 	 */
    253   1.1       jtc 	if (lflg == 0 || editor || sflg) {
    254   1.1       jtc 		lflg = 0;	/* ignore */
    255   1.1       jtc 		editfile[0] = '\0';
    256   1.1       jtc 		/*
    257   1.1       jtc 		 * Catch interrupts to reset active counter and
    258   1.1       jtc 		 * cleanup temp files.
    259   1.1       jtc 		 */
    260   1.1       jtc 		if (setjmp(jmploc.loc)) {
    261   1.1       jtc 			active = 0;
    262   1.1       jtc 			if (*editfile)
    263   1.1       jtc 				unlink(editfile);
    264   1.1       jtc 			handler = savehandler;
    265   1.1       jtc 			longjmp(handler->loc, 1);
    266   1.1       jtc 		}
    267   1.1       jtc 		savehandler = handler;
    268   1.1       jtc 		handler = &jmploc;
    269   1.1       jtc 		if (++active > MAXHISTLOOPS) {
    270   1.1       jtc 			active = 0;
    271   1.1       jtc 			displayhist = 0;
    272   1.1       jtc 			error("called recursively too many times");
    273   1.1       jtc 		}
    274   1.1       jtc 		/*
    275   1.1       jtc 		 * Set editor.
    276   1.1       jtc 		 */
    277   1.1       jtc 		if (sflg == 0) {
    278   1.1       jtc 			if (editor == NULL &&
    279   1.1       jtc 			    (editor = bltinlookup("FCEDIT", 1)) == NULL &&
    280   1.1       jtc 			    (editor = bltinlookup("EDITOR", 1)) == NULL)
    281   1.1       jtc 				editor = DEFEDITOR;
    282   1.1       jtc 			if (editor[0] == '-' && editor[1] == '\0') {
    283   1.1       jtc 				sflg = 1;	/* no edit */
    284   1.1       jtc 				editor = NULL;
    285   1.1       jtc 			}
    286   1.1       jtc 		}
    287   1.1       jtc 	}
    288   1.1       jtc 
    289   1.1       jtc 	/*
    290   1.1       jtc 	 * If executing, parse [old=new] now
    291   1.1       jtc 	 */
    292  1.10  christos 	if (lflg == 0 && argc > 0 &&
    293   1.1       jtc 	     ((repl = strchr(argv[0], '=')) != NULL)) {
    294   1.1       jtc 		pat = argv[0];
    295   1.1       jtc 		*repl++ = '\0';
    296   1.1       jtc 		argc--, argv++;
    297   1.1       jtc 	}
    298   1.1       jtc 	/*
    299   1.1       jtc 	 * determine [first] and [last]
    300   1.1       jtc 	 */
    301   1.1       jtc 	switch (argc) {
    302   1.1       jtc 	case 0:
    303   1.1       jtc 		firststr = lflg ? "-16" : "-1";
    304   1.1       jtc 		laststr = "-1";
    305   1.1       jtc 		break;
    306   1.1       jtc 	case 1:
    307   1.1       jtc 		firststr = argv[0];
    308   1.1       jtc 		laststr = lflg ? "-1" : argv[0];
    309   1.1       jtc 		break;
    310   1.1       jtc 	case 2:
    311   1.1       jtc 		firststr = argv[0];
    312   1.1       jtc 		laststr = argv[1];
    313   1.1       jtc 		break;
    314   1.1       jtc 	default:
    315   1.1       jtc 		error("too many args");
    316   1.1       jtc 	}
    317   1.1       jtc 	/*
    318   1.1       jtc 	 * Turn into event numbers.
    319   1.1       jtc 	 */
    320   1.1       jtc 	first = str_to_event(firststr, 0);
    321   1.1       jtc 	last = str_to_event(laststr, 1);
    322   1.1       jtc 
    323   1.1       jtc 	if (rflg) {
    324   1.1       jtc 		i = last;
    325   1.1       jtc 		last = first;
    326   1.1       jtc 		first = i;
    327   1.1       jtc 	}
    328   1.1       jtc 	/*
    329   1.1       jtc 	 * XXX - this should not depend on the event numbers
    330  1.10  christos 	 * always increasing.  Add sequence numbers or offset
    331   1.1       jtc 	 * to the history element in next (diskbased) release.
    332   1.1       jtc 	 */
    333   1.1       jtc 	direction = first < last ? H_PREV : H_NEXT;
    334   1.1       jtc 
    335   1.1       jtc 	/*
    336   1.1       jtc 	 * If editing, grab a temp file.
    337   1.1       jtc 	 */
    338   1.1       jtc 	if (editor) {
    339   1.1       jtc 		int fd;
    340   1.1       jtc 		INTOFF;		/* easier */
    341   1.1       jtc 		sprintf(editfile, "%s/_shXXXXXX", _PATH_TMP);
    342   1.1       jtc 		if ((fd = mkstemp(editfile)) < 0)
    343   1.1       jtc 			error("can't create temporary file %s", editfile);
    344   1.1       jtc 		if ((efp = fdopen(fd, "w")) == NULL) {
    345   1.1       jtc 			close(fd);
    346  1.10  christos 			error("can't allocate stdio buffer for temp");
    347   1.1       jtc 		}
    348   1.1       jtc 	}
    349   1.1       jtc 
    350   1.1       jtc 	/*
    351   1.1       jtc 	 * Loop through selected history events.  If listing or executing,
    352   1.1       jtc 	 * do it now.  Otherwise, put into temp file and call the editor
    353   1.1       jtc 	 * after.
    354   1.1       jtc 	 *
    355   1.1       jtc 	 * The history interface needs rethinking, as the following
    356   1.1       jtc 	 * convolutions will demonstrate.
    357   1.1       jtc 	 */
    358  1.16  christos 	history(hist, &he, H_FIRST);
    359  1.16  christos 	retval = history(hist, &he, H_NEXT_EVENT, first);
    360  1.16  christos 	for (;retval != -1; retval = history(hist, &he, direction)) {
    361   1.1       jtc 		if (lflg) {
    362   1.1       jtc 			if (!nflg)
    363  1.16  christos 				out1fmt("%5d ", he.num);
    364  1.16  christos 			out1str(he.str);
    365   1.1       jtc 		} else {
    366  1.10  christos 			char *s = pat ?
    367  1.16  christos 			   fc_replace(he.str, pat, repl) : (char *)he.str;
    368   1.1       jtc 
    369   1.1       jtc 			if (sflg) {
    370   1.1       jtc 				if (displayhist) {
    371   1.1       jtc 					out2str(s);
    372   1.1       jtc 				}
    373   1.1       jtc 				evalstring(s);
    374   1.1       jtc 				if (displayhist && hist) {
    375   1.1       jtc 					/*
    376  1.10  christos 					 *  XXX what about recursive and
    377   1.1       jtc 					 *  relative histnums.
    378   1.1       jtc 					 */
    379  1.16  christos 					history(hist, &he, H_ENTER, s);
    380   1.1       jtc 				}
    381   1.1       jtc 			} else
    382   1.1       jtc 				fputs(s, efp);
    383   1.1       jtc 		}
    384   1.1       jtc 		/*
    385  1.16  christos 		 * At end?  (if we were to lose last, we'd sure be
    386   1.1       jtc 		 * messed up).
    387   1.1       jtc 		 */
    388  1.16  christos 		if (he.num == last)
    389   1.1       jtc 			break;
    390   1.1       jtc 	}
    391   1.1       jtc 	if (editor) {
    392   1.1       jtc 		char *editcmd;
    393   1.1       jtc 
    394   1.1       jtc 		fclose(efp);
    395   1.1       jtc 		editcmd = stalloc(strlen(editor) + strlen(editfile) + 2);
    396   1.1       jtc 		sprintf(editcmd, "%s %s", editor, editfile);
    397   1.1       jtc 		evalstring(editcmd);	/* XXX - should use no JC command */
    398   1.1       jtc 		INTON;
    399   1.1       jtc 		readcmdfile(editfile);	/* XXX - should read back - quick tst */
    400   1.1       jtc 		unlink(editfile);
    401   1.1       jtc 	}
    402  1.10  christos 
    403   1.1       jtc 	if (lflg == 0 && active > 0)
    404   1.1       jtc 		--active;
    405   1.1       jtc 	if (displayhist)
    406   1.1       jtc 		displayhist = 0;
    407   1.8  christos 	return 0;
    408   1.1       jtc }
    409   1.1       jtc 
    410   1.1       jtc STATIC char *
    411   1.1       jtc fc_replace(s, p, r)
    412   1.1       jtc 	const char *s;
    413   1.1       jtc 	char *p, *r;
    414   1.1       jtc {
    415   1.1       jtc 	char *dest;
    416   1.1       jtc 	int plen = strlen(p);
    417   1.1       jtc 
    418   1.1       jtc 	STARTSTACKSTR(dest);
    419   1.1       jtc 	while (*s) {
    420   1.1       jtc 		if (*s == *p && strncmp(s, p, plen) == 0) {
    421   1.1       jtc 			while (*r)
    422   1.1       jtc 				STPUTC(*r++, dest);
    423   1.1       jtc 			s += plen;
    424   1.1       jtc 			*p = '\0';	/* so no more matches */
    425   1.1       jtc 		} else
    426   1.1       jtc 			STPUTC(*s++, dest);
    427   1.1       jtc 	}
    428   1.1       jtc 	STACKSTRNUL(dest);
    429   1.1       jtc 	dest = grabstackstr(dest);
    430   1.1       jtc 
    431   1.1       jtc 	return (dest);
    432   1.1       jtc }
    433   1.1       jtc 
    434   1.4       cgd int
    435   1.1       jtc not_fcnumber(s)
    436   1.1       jtc         char *s;
    437   1.1       jtc {
    438   1.7  christos 	if (s == NULL)
    439   1.7  christos 		return 0;
    440   1.1       jtc         if (*s == '-')
    441   1.1       jtc                 s++;
    442   1.1       jtc 	return (!is_number(s));
    443   1.1       jtc }
    444   1.1       jtc 
    445   1.4       cgd int
    446   1.1       jtc str_to_event(str, last)
    447   1.1       jtc 	char *str;
    448   1.1       jtc 	int last;
    449   1.1       jtc {
    450  1.16  christos 	HistEvent he;
    451   1.1       jtc 	char *s = str;
    452   1.1       jtc 	int relative = 0;
    453  1.16  christos 	int i, retval;
    454   1.1       jtc 
    455  1.16  christos 	retval = history(hist, &he, H_FIRST);
    456   1.1       jtc 	switch (*s) {
    457   1.1       jtc 	case '-':
    458   1.1       jtc 		relative = 1;
    459   1.1       jtc 		/*FALLTHROUGH*/
    460   1.1       jtc 	case '+':
    461   1.1       jtc 		s++;
    462   1.1       jtc 	}
    463   1.1       jtc 	if (is_number(s)) {
    464   1.1       jtc 		i = atoi(s);
    465   1.1       jtc 		if (relative) {
    466  1.16  christos 			while (retval != -1 && i--) {
    467  1.16  christos 				retval = history(hist, &he, H_NEXT);
    468   1.1       jtc 			}
    469  1.16  christos 			if (retval == -1)
    470  1.16  christos 				retval = history(hist, &he, H_LAST);
    471   1.1       jtc 		} else {
    472  1.16  christos 			retval = history(hist, &he, H_NEXT_EVENT, i);
    473  1.16  christos 			if (retval == -1) {
    474   1.1       jtc 				/*
    475   1.1       jtc 				 * the notion of first and last is
    476   1.1       jtc 				 * backwards to that of the history package
    477   1.1       jtc 				 */
    478  1.16  christos 				retval = history(hist, &he,
    479  1.16  christos 						last ? H_FIRST : H_LAST);
    480   1.1       jtc 			}
    481   1.1       jtc 		}
    482  1.16  christos 		if (retval == -1)
    483   1.1       jtc 			error("history number %s not found (internal error)",
    484   1.1       jtc 			       str);
    485   1.1       jtc 	} else {
    486   1.1       jtc 		/*
    487  1.10  christos 		 * pattern
    488   1.1       jtc 		 */
    489  1.16  christos 		retval = history(hist, &he, H_PREV_STR, str);
    490  1.16  christos 		if (retval == -1)
    491   1.1       jtc 			error("history pattern not found: %s", str);
    492   1.1       jtc 	}
    493  1.16  christos 	return (he.num);
    494   1.1       jtc }
    495  1.11  christos #else
    496  1.11  christos int
    497  1.11  christos histcmd(argc, argv)
    498  1.11  christos 	int argc;
    499  1.11  christos 	char **argv;
    500  1.11  christos {
    501  1.11  christos 	error("not compiled with history support");
    502  1.11  christos }
    503  1.11  christos #endif
    504