Home | History | Annotate | Line # | Download | only in ksh
history.c revision 1.5
      1  1.5       agc /*	$NetBSD: history.c,v 1.5 2003/06/23 11:38:57 agc Exp $	*/
      2  1.2       tls 
      3  1.1       jtc /*
      4  1.1       jtc  * command history
      5  1.1       jtc  *
      6  1.1       jtc  * only implements in-memory history.
      7  1.1       jtc  */
      8  1.1       jtc 
      9  1.1       jtc /*
     10  1.1       jtc  *	This file contains
     11  1.1       jtc  *	a)	the original in-memory history  mechanism
     12  1.1       jtc  *	b)	a simple file saving history mechanism done by  sjg@zen
     13  1.1       jtc  *		define EASY_HISTORY to get this
     14  1.1       jtc  *	c)	a more complicated mechanism done by  pc (at) hillside.co.uk
     15  1.1       jtc  *		that more closely follows the real ksh way of doing
     16  1.1       jtc  *		things. You need to have the mmap system call for this
     17  1.1       jtc  *		to work on your system
     18  1.1       jtc  */
     19  1.5       agc #include <sys/cdefs.h>
     20  1.5       agc 
     21  1.5       agc #ifndef lint
     22  1.5       agc __RCSID("$NetBSD: history.c,v 1.5 2003/06/23 11:38:57 agc Exp $");
     23  1.5       agc #endif
     24  1.5       agc 
     25  1.1       jtc 
     26  1.1       jtc #include "sh.h"
     27  1.1       jtc #include "ksh_stat.h"
     28  1.1       jtc 
     29  1.1       jtc #ifdef HISTORY
     30  1.1       jtc # ifdef EASY_HISTORY
     31  1.1       jtc 
     32  1.1       jtc #  ifndef HISTFILE
     33  1.1       jtc #   ifdef OS2
     34  1.1       jtc #    define HISTFILE "history.ksh"
     35  1.1       jtc #   else /* OS2 */
     36  1.4   hubertf #    define HISTFILE ".pdksh_history"
     37  1.1       jtc #   endif /* OS2 */
     38  1.1       jtc #  endif
     39  1.1       jtc 
     40  1.1       jtc # else
     41  1.1       jtc /*	Defines and includes for the complicated case */
     42  1.1       jtc 
     43  1.1       jtc #  include <sys/file.h>
     44  1.1       jtc #  include <sys/mman.h>
     45  1.1       jtc 
     46  1.1       jtc /*
     47  1.1       jtc  *	variables for handling the data file
     48  1.1       jtc  */
     49  1.1       jtc static int	histfd;
     50  1.1       jtc static int	hsize;
     51  1.1       jtc 
     52  1.1       jtc static int hist_count_lines ARGS((unsigned char *, int));
     53  1.1       jtc static int hist_shrink ARGS((unsigned char *, int));
     54  1.1       jtc static unsigned char *hist_skip_back ARGS((unsigned char *,int *,int));
     55  1.1       jtc static void histload ARGS((Source *, unsigned char *, int));
     56  1.1       jtc static void histinsert ARGS((Source *, int, unsigned char *));
     57  1.1       jtc static void writehistfile ARGS((int, char *));
     58  1.1       jtc static int sprinkle ARGS((int));
     59  1.1       jtc 
     60  1.1       jtc #  ifdef MAP_FILE
     61  1.1       jtc #   define MAP_FLAGS	(MAP_FILE|MAP_PRIVATE)
     62  1.1       jtc #  else
     63  1.1       jtc #   define MAP_FLAGS	MAP_PRIVATE
     64  1.1       jtc #  endif
     65  1.1       jtc 
     66  1.1       jtc # endif	/* of EASY_HISTORY */
     67  1.1       jtc 
     68  1.1       jtc static int	hist_execute ARGS((char *cmd));
     69  1.1       jtc static int	hist_replace ARGS((char **hp, const char *pat, const char *rep,
     70  1.1       jtc 				   int global));
     71  1.1       jtc static char   **hist_get ARGS((const char *str, int approx, int allow_cur));
     72  1.1       jtc static char   **hist_get_newest ARGS((int allow_cur));
     73  1.3  christos static char   **hist_get_oldest ARGS((void));
     74  1.1       jtc static void	histbackup ARGS((void));
     75  1.1       jtc 
     76  1.1       jtc static char   **current;	/* current postition in history[] */
     77  1.1       jtc static int	curpos;		/* current index in history[] */
     78  1.1       jtc static char    *hname;		/* current name of history file */
     79  1.1       jtc static int	hstarted;	/* set after hist_init() called */
     80  1.1       jtc static Source	*hist_source;
     81  1.1       jtc 
     82  1.1       jtc 
     83  1.1       jtc int
     84  1.1       jtc c_fc(wp)
     85  1.1       jtc 	char **wp;
     86  1.1       jtc {
     87  1.1       jtc 	struct shf *shf;
     88  1.1       jtc 	struct temp UNINITIALIZED(*tf);
     89  1.1       jtc 	char *p, *editor = (char *) 0;
     90  1.1       jtc 	int gflag = 0, lflag = 0, nflag = 0, sflag = 0, rflag = 0;
     91  1.1       jtc 	int optc;
     92  1.1       jtc 	char *first = (char *) 0, *last = (char *) 0;
     93  1.1       jtc 	char **hfirst, **hlast, **hp;
     94  1.1       jtc 
     95  1.1       jtc 	while ((optc = ksh_getopt(wp, &builtin_opt, "e:glnrs0,1,2,3,4,5,6,7,8,9,")) != EOF)
     96  1.1       jtc 		switch (optc) {
     97  1.1       jtc 		  case 'e':
     98  1.1       jtc 			p = builtin_opt.optarg;
     99  1.1       jtc 			if (strcmp(p, "-") == 0)
    100  1.1       jtc 				sflag++;
    101  1.1       jtc 			else {
    102  1.1       jtc 				editor = str_nsave(p, strlen(p) + 4, ATEMP);
    103  1.1       jtc 				strcat(editor, " $_");
    104  1.1       jtc 			}
    105  1.1       jtc 			break;
    106  1.1       jtc 		  case 'g': /* non-at&t ksh */
    107  1.1       jtc 			gflag++;
    108  1.1       jtc 			break;
    109  1.1       jtc 		  case 'l':
    110  1.1       jtc 			lflag++;
    111  1.1       jtc 			break;
    112  1.1       jtc 		  case 'n':
    113  1.1       jtc 			nflag++;
    114  1.1       jtc 			break;
    115  1.1       jtc 		  case 'r':
    116  1.1       jtc 			rflag++;
    117  1.1       jtc 			break;
    118  1.1       jtc 		  case 's':	/* posix version of -e - */
    119  1.1       jtc 			sflag++;
    120  1.1       jtc 			break;
    121  1.1       jtc 		  /* kludge city - accept -num as -- -num (kind of) */
    122  1.1       jtc 		  case '0': case '1': case '2': case '3': case '4':
    123  1.1       jtc 		  case '5': case '6': case '7': case '8': case '9':
    124  1.1       jtc 			p = shf_smprintf("-%c%s",
    125  1.1       jtc 					optc, builtin_opt.optarg);
    126  1.1       jtc 			if (!first)
    127  1.1       jtc 				first = p;
    128  1.1       jtc 			else if (!last)
    129  1.1       jtc 				last = p;
    130  1.1       jtc 			else {
    131  1.1       jtc 				bi_errorf("too many arguments");
    132  1.1       jtc 				return 1;
    133  1.1       jtc 			}
    134  1.1       jtc 			break;
    135  1.1       jtc 		  case '?':
    136  1.1       jtc 			return 1;
    137  1.1       jtc 		}
    138  1.1       jtc 	wp += builtin_opt.optind;
    139  1.1       jtc 
    140  1.1       jtc 	/* Substitute and execute command */
    141  1.1       jtc 	if (sflag) {
    142  1.1       jtc 		char *pat = (char *) 0, *rep = (char *) 0;
    143  1.1       jtc 
    144  1.1       jtc 		if (editor || lflag || nflag || rflag) {
    145  1.1       jtc 			bi_errorf("can't use -e, -l, -n, -r with -s (-e -)");
    146  1.1       jtc 			return 1;
    147  1.1       jtc 		}
    148  1.1       jtc 
    149  1.1       jtc 		/* Check for pattern replacement argument */
    150  1.1       jtc 		if (*wp && **wp && (p = strchr(*wp + 1, '='))) {
    151  1.1       jtc 			pat = str_save(*wp, ATEMP);
    152  1.1       jtc 			p = pat + (p - *wp);
    153  1.1       jtc 			*p++ = '\0';
    154  1.1       jtc 			rep = p;
    155  1.1       jtc 			wp++;
    156  1.1       jtc 		}
    157  1.1       jtc 		/* Check for search prefix */
    158  1.1       jtc 		if (!first && (first = *wp))
    159  1.1       jtc 			wp++;
    160  1.1       jtc 		if (last || *wp) {
    161  1.1       jtc 			bi_errorf("too many arguments");
    162  1.1       jtc 			return 1;
    163  1.1       jtc 		}
    164  1.1       jtc 
    165  1.1       jtc 		hp = first ? hist_get(first, FALSE, FALSE)
    166  1.1       jtc 			   : hist_get_newest(FALSE);
    167  1.1       jtc 		if (!hp)
    168  1.1       jtc 			return 1;
    169  1.1       jtc 		return hist_replace(hp, pat, rep, gflag);
    170  1.1       jtc 	}
    171  1.1       jtc 
    172  1.1       jtc 	if (editor && (lflag || nflag)) {
    173  1.1       jtc 		bi_errorf("can't use -l, -n with -e");
    174  1.1       jtc 		return 1;
    175  1.1       jtc 	}
    176  1.1       jtc 
    177  1.1       jtc 	if (!first && (first = *wp))
    178  1.1       jtc 		wp++;
    179  1.1       jtc 	if (!last && (last = *wp))
    180  1.1       jtc 		wp++;
    181  1.1       jtc 	if (*wp) {
    182  1.1       jtc 		bi_errorf("too many arguments");
    183  1.1       jtc 		return 1;
    184  1.1       jtc 	}
    185  1.1       jtc 	if (!first) {
    186  1.1       jtc 		hfirst = lflag ? hist_get("-16", TRUE, TRUE)
    187  1.1       jtc 			       : hist_get_newest(FALSE);
    188  1.1       jtc 		if (!hfirst)
    189  1.1       jtc 			return 1;
    190  1.1       jtc 		/* can't fail if hfirst didn't fail */
    191  1.1       jtc 		hlast = hist_get_newest(FALSE);
    192  1.1       jtc 	} else {
    193  1.1       jtc 		/* POSIX says not an error if first/last out of bounds
    194  1.1       jtc 		 * when range is specified; at&t ksh and pdksh allow out of
    195  1.1       jtc 		 * bounds for -l as well.
    196  1.1       jtc 		 */
    197  1.1       jtc 		hfirst = hist_get(first, (lflag || last) ? TRUE : FALSE,
    198  1.1       jtc 				lflag ? TRUE : FALSE);
    199  1.1       jtc 		if (!hfirst)
    200  1.1       jtc 			return 1;
    201  1.1       jtc 		hlast = last ? hist_get(last, TRUE, lflag ? TRUE : FALSE)
    202  1.1       jtc 			    : (lflag ? hist_get_newest(FALSE) : hfirst);
    203  1.1       jtc 		if (!hlast)
    204  1.1       jtc 			return 1;
    205  1.1       jtc 	}
    206  1.1       jtc 	if (hfirst > hlast) {
    207  1.1       jtc 		char **temp;
    208  1.1       jtc 
    209  1.1       jtc 		temp = hfirst; hfirst = hlast; hlast = temp;
    210  1.1       jtc 		rflag = !rflag; /* POSIX */
    211  1.1       jtc 	}
    212  1.1       jtc 
    213  1.1       jtc 	/* List history */
    214  1.1       jtc 	if (lflag) {
    215  1.1       jtc 		char *s, *t;
    216  1.1       jtc 		const char *nfmt = nflag ? "\t" : "%d\t";
    217  1.1       jtc 
    218  1.1       jtc 		for (hp = rflag ? hlast : hfirst;
    219  1.1       jtc 		     hp >= hfirst && hp <= hlast; hp += rflag ? -1 : 1)
    220  1.1       jtc 		{
    221  1.1       jtc 			shf_fprintf(shl_stdout, nfmt,
    222  1.1       jtc 				hist_source->line - (int) (histptr - hp));
    223  1.1       jtc 			/* print multi-line commands correctly */
    224  1.1       jtc 			for (s = *hp; (t = strchr(s, '\n')); s = t)
    225  1.1       jtc 				shf_fprintf(shl_stdout, "%.*s\t", ++t - s, s);
    226  1.1       jtc 			shf_fprintf(shl_stdout, "%s\n", s);
    227  1.1       jtc 		}
    228  1.1       jtc 		shf_flush(shl_stdout);
    229  1.1       jtc 		return 0;
    230  1.1       jtc 	}
    231  1.1       jtc 
    232  1.1       jtc 	/* Run editor on selected lines, then run resulting commands */
    233  1.1       jtc 
    234  1.4   hubertf 	tf = maketemp(ATEMP, TT_HIST_EDIT, &e->temps);
    235  1.1       jtc 	if (!(shf = tf->shf)) {
    236  1.1       jtc 		bi_errorf("cannot create temp file %s - %s",
    237  1.1       jtc 			tf->name, strerror(errno));
    238  1.1       jtc 		return 1;
    239  1.1       jtc 	}
    240  1.1       jtc 	for (hp = rflag ? hlast : hfirst;
    241  1.1       jtc 	     hp >= hfirst && hp <= hlast; hp += rflag ? -1 : 1)
    242  1.1       jtc 		shf_fprintf(shf, "%s\n", *hp);
    243  1.1       jtc 	if (shf_close(shf) == EOF) {
    244  1.1       jtc 		bi_errorf("error writing temporary file - %s", strerror(errno));
    245  1.1       jtc 		return 1;
    246  1.1       jtc 	}
    247  1.1       jtc 
    248  1.4   hubertf 	/* Ignore setstr errors here (arbitrary) */
    249  1.4   hubertf 	setstr(local("_", FALSE), tf->name, KSH_RETURN_ERROR);
    250  1.1       jtc 
    251  1.1       jtc 	/* XXX: source should not get trashed by this.. */
    252  1.1       jtc 	{
    253  1.1       jtc 		Source *sold = source;
    254  1.1       jtc 		int ret;
    255  1.1       jtc 
    256  1.1       jtc 		ret = command(editor ? editor : "${FCEDIT:-/bin/ed} $_");
    257  1.1       jtc 		source = sold;
    258  1.1       jtc 		if (ret)
    259  1.1       jtc 			return ret;
    260  1.1       jtc 	}
    261  1.1       jtc 
    262  1.1       jtc 	{
    263  1.1       jtc 		struct stat statb;
    264  1.1       jtc 		XString xs;
    265  1.1       jtc 		char *xp;
    266  1.1       jtc 		int n;
    267  1.1       jtc 
    268  1.1       jtc 		if (!(shf = shf_open(tf->name, O_RDONLY, 0, 0))) {
    269  1.1       jtc 			bi_errorf("cannot open temp file %s", tf->name);
    270  1.1       jtc 			return 1;
    271  1.1       jtc 		}
    272  1.1       jtc 
    273  1.1       jtc 		n = fstat(shf_fileno(shf), &statb) < 0 ? 128
    274  1.1       jtc 			: statb.st_size + 1;
    275  1.1       jtc 		Xinit(xs, xp, n, hist_source->areap);
    276  1.1       jtc 		while ((n = shf_read(xp, Xnleft(xs, xp), shf)) > 0) {
    277  1.1       jtc 			xp += n;
    278  1.1       jtc 			if (Xnleft(xs, xp) <= 0)
    279  1.1       jtc 				XcheckN(xs, xp, Xlength(xs, xp));
    280  1.1       jtc 		}
    281  1.1       jtc 		if (n < 0) {
    282  1.1       jtc 			bi_errorf("error reading temp file %s - %s",
    283  1.1       jtc 				tf->name, strerror(shf_errno(shf)));
    284  1.1       jtc 			shf_close(shf);
    285  1.1       jtc 			return 1;
    286  1.1       jtc 		}
    287  1.1       jtc 		shf_close(shf);
    288  1.1       jtc 		*xp = '\0';
    289  1.1       jtc 		strip_nuls(Xstring(xs, xp), Xlength(xs, xp));
    290  1.1       jtc 		return hist_execute(Xstring(xs, xp));
    291  1.1       jtc 	}
    292  1.1       jtc }
    293  1.1       jtc 
    294  1.1       jtc /* Save cmd in history, execute cmd (cmd gets trashed) */
    295  1.1       jtc static int
    296  1.1       jtc hist_execute(cmd)
    297  1.1       jtc 	char *cmd;
    298  1.1       jtc {
    299  1.1       jtc 	Source *sold;
    300  1.1       jtc 	int ret;
    301  1.1       jtc 	char *p, *q;
    302  1.1       jtc 
    303  1.1       jtc 	histbackup();
    304  1.1       jtc 
    305  1.1       jtc 	for (p = cmd; p; p = q) {
    306  1.1       jtc 		if ((q = strchr(p, '\n'))) {
    307  1.1       jtc 			*q++ = '\0'; /* kill the newline */
    308  1.1       jtc 			if (!*q) /* ignore trailing newline */
    309  1.1       jtc 				q = (char *) 0;
    310  1.1       jtc 		}
    311  1.1       jtc #ifdef EASY_HISTORY
    312  1.1       jtc 		if (p != cmd)
    313  1.1       jtc 			histappend(p, TRUE);
    314  1.1       jtc 		else
    315  1.1       jtc #endif /* EASY_HISTORY */
    316  1.1       jtc 			histsave(++(hist_source->line), p, 1);
    317  1.1       jtc 
    318  1.1       jtc 		shellf("%s\n", p); /* POSIX doesn't say this is done... */
    319  1.1       jtc 		if ((p = q)) /* restore \n (trailing \n not restored) */
    320  1.1       jtc 			q[-1] = '\n';
    321  1.1       jtc 	}
    322  1.1       jtc 
    323  1.1       jtc 	/* Commands are executed here instead of pushing them onto the
    324  1.1       jtc 	 * input 'cause posix says the redirection and variable assignments
    325  1.1       jtc 	 * in
    326  1.1       jtc 	 *	X=y fc -e - 42 2> /dev/null
    327  1.1       jtc 	 * are to effect the repeated commands environment.
    328  1.1       jtc 	 */
    329  1.1       jtc 	/* XXX: source should not get trashed by this.. */
    330  1.1       jtc 	sold = source;
    331  1.1       jtc 	ret = command(cmd);
    332  1.1       jtc 	source = sold;
    333  1.1       jtc 	return ret;
    334  1.1       jtc }
    335  1.1       jtc 
    336  1.1       jtc static int
    337  1.1       jtc hist_replace(hp, pat, rep, global)
    338  1.1       jtc 	char **hp;
    339  1.1       jtc 	const char *pat;
    340  1.1       jtc 	const char *rep;
    341  1.1       jtc 	int global;
    342  1.1       jtc {
    343  1.1       jtc 	char *line;
    344  1.1       jtc 
    345  1.1       jtc 	if (!pat)
    346  1.1       jtc 		line = str_save(*hp, ATEMP);
    347  1.1       jtc 	else {
    348  1.1       jtc 		char *s, *s1;
    349  1.1       jtc 		int pat_len = strlen(pat);
    350  1.1       jtc 		int rep_len = strlen(rep);
    351  1.1       jtc 		int len;
    352  1.1       jtc 		XString xs;
    353  1.1       jtc 		char *xp;
    354  1.1       jtc 		int any_subst = 0;
    355  1.1       jtc 
    356  1.1       jtc 		Xinit(xs, xp, 128, ATEMP);
    357  1.1       jtc 		for (s = *hp; (s1 = strstr(s, pat))
    358  1.1       jtc 			      && (!any_subst || global) ; s = s1 + pat_len)
    359  1.1       jtc 		{
    360  1.1       jtc 			any_subst = 1;
    361  1.1       jtc 			len = s1 - s;
    362  1.1       jtc 			XcheckN(xs, xp, len + rep_len);
    363  1.1       jtc 			memcpy(xp, s, len);		/* first part */
    364  1.1       jtc 			xp += len;
    365  1.1       jtc 			memcpy(xp, rep, rep_len);	/* replacement */
    366  1.1       jtc 			xp += rep_len;
    367  1.1       jtc 		}
    368  1.1       jtc 		if (!any_subst) {
    369  1.1       jtc 			bi_errorf("substitution failed");
    370  1.1       jtc 			return 1;
    371  1.1       jtc 		}
    372  1.1       jtc 		len = strlen(s) + 1;
    373  1.1       jtc 		XcheckN(xs, xp, len);
    374  1.1       jtc 		memcpy(xp, s, len);
    375  1.1       jtc 		xp += len;
    376  1.1       jtc 		line = Xclose(xs, xp);
    377  1.1       jtc 	}
    378  1.1       jtc 	return hist_execute(line);
    379  1.1       jtc }
    380  1.1       jtc 
    381  1.1       jtc /*
    382  1.1       jtc  * get pointer to history given pattern
    383  1.1       jtc  * pattern is a number or string
    384  1.1       jtc  */
    385  1.1       jtc static char **
    386  1.1       jtc hist_get(str, approx, allow_cur)
    387  1.1       jtc 	const char *str;
    388  1.1       jtc 	int approx;
    389  1.1       jtc 	int allow_cur;
    390  1.1       jtc {
    391  1.1       jtc 	char **hp = (char **) 0;
    392  1.1       jtc 	int n;
    393  1.1       jtc 
    394  1.1       jtc 	if (getn(str, &n)) {
    395  1.1       jtc 		hp = histptr + (n < 0 ? n : (n - hist_source->line));
    396  1.1       jtc 		if (hp < history) {
    397  1.1       jtc 			if (approx)
    398  1.1       jtc 				hp = hist_get_oldest();
    399  1.1       jtc 			else {
    400  1.1       jtc 				bi_errorf("%s: not in history", str);
    401  1.1       jtc 				hp = (char **) 0;
    402  1.1       jtc 			}
    403  1.1       jtc 		} else if (hp > histptr) {
    404  1.1       jtc 			if (approx)
    405  1.1       jtc 				hp = hist_get_newest(allow_cur);
    406  1.1       jtc 			else {
    407  1.1       jtc 				bi_errorf("%s: not in history", str);
    408  1.1       jtc 				hp = (char **) 0;
    409  1.1       jtc 			}
    410  1.1       jtc 		} else if (!allow_cur && hp == histptr) {
    411  1.1       jtc 			bi_errorf("%s: invalid range", str);
    412  1.1       jtc 			hp = (char **) 0;
    413  1.1       jtc 		}
    414  1.1       jtc 	} else {
    415  1.1       jtc 		int anchored = *str == '?' ? (++str, 0) : 1;
    416  1.1       jtc 
    417  1.1       jtc 		/* the -1 is to avoid the current fc command */
    418  1.1       jtc 		n = findhist(histptr - history - 1, 0, str, anchored);
    419  1.1       jtc 		if (n < 0) {
    420  1.1       jtc 			bi_errorf("%s: not in history", str);
    421  1.1       jtc 			hp = (char **) 0;
    422  1.1       jtc 		} else
    423  1.1       jtc 			hp = &history[n];
    424  1.1       jtc 	}
    425  1.1       jtc 	return hp;
    426  1.1       jtc }
    427  1.1       jtc 
    428  1.1       jtc /* Return a pointer to the newest command in the history */
    429  1.1       jtc static char **
    430  1.1       jtc hist_get_newest(allow_cur)
    431  1.1       jtc 	int allow_cur;
    432  1.1       jtc {
    433  1.1       jtc 	if (histptr < history || (!allow_cur && histptr == history)) {
    434  1.1       jtc 		bi_errorf("no history (yet)");
    435  1.1       jtc 		return (char **) 0;
    436  1.1       jtc 	}
    437  1.1       jtc 	if (allow_cur)
    438  1.1       jtc 		return histptr;
    439  1.1       jtc 	return histptr - 1;
    440  1.1       jtc }
    441  1.1       jtc 
    442  1.1       jtc /* Return a pointer to the newest command in the history */
    443  1.1       jtc static char **
    444  1.1       jtc hist_get_oldest()
    445  1.1       jtc {
    446  1.1       jtc 	if (histptr <= history) {
    447  1.1       jtc 		bi_errorf("no history (yet)");
    448  1.1       jtc 		return (char **) 0;
    449  1.1       jtc 	}
    450  1.1       jtc 	return history;
    451  1.1       jtc }
    452  1.1       jtc 
    453  1.1       jtc /******************************/
    454  1.1       jtc /* Back up over last histsave */
    455  1.1       jtc /******************************/
    456  1.1       jtc static void
    457  1.1       jtc histbackup()
    458  1.1       jtc {
    459  1.1       jtc 	static int last_line = -1;
    460  1.1       jtc 
    461  1.1       jtc 	if (histptr >= history && last_line != hist_source->line) {
    462  1.1       jtc 		hist_source->line--;
    463  1.1       jtc 		afree((void*)*histptr, APERM);
    464  1.1       jtc 		histptr--;
    465  1.1       jtc 		last_line = hist_source->line;
    466  1.1       jtc 	}
    467  1.1       jtc }
    468  1.1       jtc 
    469  1.1       jtc /*
    470  1.1       jtc  * Return the current position.
    471  1.1       jtc  */
    472  1.1       jtc char **
    473  1.1       jtc histpos()
    474  1.1       jtc {
    475  1.1       jtc 	return current;
    476  1.1       jtc }
    477  1.1       jtc 
    478  1.1       jtc int
    479  1.1       jtc histN()
    480  1.1       jtc {
    481  1.1       jtc 	return curpos;
    482  1.1       jtc }
    483  1.1       jtc 
    484  1.1       jtc int
    485  1.1       jtc histnum(n)
    486  1.1       jtc 	int	n;
    487  1.1       jtc {
    488  1.1       jtc 	int	last = histptr - history;
    489  1.1       jtc 
    490  1.1       jtc 	if (n < 0 || n >= last) {
    491  1.1       jtc 		current = histptr;
    492  1.1       jtc 		curpos = last;
    493  1.1       jtc 		return last;
    494  1.4   hubertf 	} else {
    495  1.1       jtc 		current = &history[n];
    496  1.1       jtc 		curpos = n;
    497  1.1       jtc 		return n;
    498  1.1       jtc 	}
    499  1.1       jtc }
    500  1.1       jtc 
    501  1.1       jtc /*
    502  1.1       jtc  * This will become unecessary if hist_get is modified to allow
    503  1.1       jtc  * searching from positions other than the end, and in either
    504  1.1       jtc  * direction.
    505  1.1       jtc  */
    506  1.1       jtc int
    507  1.1       jtc findhist(start, fwd, str, anchored)
    508  1.1       jtc 	int	start;
    509  1.1       jtc 	int	fwd;
    510  1.1       jtc 	const char  *str;
    511  1.1       jtc 	int	anchored;
    512  1.1       jtc {
    513  1.1       jtc 	char	**hp;
    514  1.1       jtc 	int	maxhist = histptr - history;
    515  1.1       jtc 	int	incr = fwd ? 1 : -1;
    516  1.1       jtc 	int	len = strlen(str);
    517  1.1       jtc 
    518  1.1       jtc 	if (start < 0 || start >= maxhist)
    519  1.1       jtc 		start = maxhist;
    520  1.1       jtc 
    521  1.1       jtc 	hp = &history[start];
    522  1.1       jtc 	for (; hp >= history && hp <= histptr; hp += incr)
    523  1.1       jtc 		if ((anchored && strncmp(*hp, str, len) == 0)
    524  1.1       jtc 		    || (!anchored && strstr(*hp, str)))
    525  1.1       jtc 			return hp - history;
    526  1.1       jtc 
    527  1.1       jtc 	return -1;
    528  1.1       jtc }
    529  1.1       jtc 
    530  1.1       jtc /*
    531  1.1       jtc  *	set history
    532  1.1       jtc  *	this means reallocating the dataspace
    533  1.1       jtc  */
    534  1.1       jtc void
    535  1.1       jtc sethistsize(n)
    536  1.1       jtc 	int n;
    537  1.1       jtc {
    538  1.1       jtc 	if (n > 0 && n != histsize) {
    539  1.1       jtc 		int cursize = histptr - history;
    540  1.1       jtc 
    541  1.1       jtc 		/* save most recent history */
    542  1.1       jtc 		if (n < cursize) {
    543  1.1       jtc 			memmove(history, histptr - n, n * sizeof(char *));
    544  1.1       jtc 			cursize = n;
    545  1.1       jtc 		}
    546  1.1       jtc 
    547  1.1       jtc 		history = (char **)aresize(history, n*sizeof(char *), APERM);
    548  1.1       jtc 
    549  1.1       jtc 		histsize = n;
    550  1.1       jtc 		histptr = history + cursize;
    551  1.1       jtc 	}
    552  1.1       jtc }
    553  1.1       jtc 
    554  1.1       jtc /*
    555  1.1       jtc  *	set history file
    556  1.1       jtc  *	This can mean reloading/resetting/starting history file
    557  1.1       jtc  *	maintenance
    558  1.1       jtc  */
    559  1.1       jtc void
    560  1.1       jtc sethistfile(name)
    561  1.1       jtc 	const char *name;
    562  1.1       jtc {
    563  1.1       jtc 	/* if not started then nothing to do */
    564  1.1       jtc 	if (hstarted == 0)
    565  1.1       jtc 		return;
    566  1.1       jtc 
    567  1.1       jtc 	/* if the name is the same as the name we have */
    568  1.1       jtc 	if (hname && strcmp(hname, name) == 0)
    569  1.1       jtc 		return;
    570  1.1       jtc 
    571  1.1       jtc 	/*
    572  1.1       jtc 	 * its a new name - possibly
    573  1.1       jtc 	 */
    574  1.1       jtc # ifdef EASY_HISTORY
    575  1.1       jtc 	if (hname) {
    576  1.1       jtc 		afree(hname, APERM);
    577  1.1       jtc 		hname = NULL;
    578  1.1       jtc 	}
    579  1.1       jtc # else
    580  1.1       jtc 	if (histfd) {
    581  1.1       jtc 		/* yes the file is open */
    582  1.1       jtc 		(void) close(histfd);
    583  1.1       jtc 		histfd = 0;
    584  1.1       jtc 		hsize = 0;
    585  1.1       jtc 		afree(hname, APERM);
    586  1.1       jtc 		hname = NULL;
    587  1.1       jtc 		/* let's reset the history */
    588  1.1       jtc 		histptr = history - 1;
    589  1.1       jtc 		hist_source->line = 0;
    590  1.1       jtc 	}
    591  1.1       jtc # endif
    592  1.1       jtc 
    593  1.1       jtc 	hist_init(hist_source);
    594  1.1       jtc }
    595  1.1       jtc 
    596  1.1       jtc /*
    597  1.1       jtc  *	initialise the history vector
    598  1.1       jtc  */
    599  1.1       jtc void
    600  1.1       jtc init_histvec()
    601  1.1       jtc {
    602  1.1       jtc 	if (history == (char **)NULL) {
    603  1.1       jtc 		histsize = HISTORYSIZE;
    604  1.1       jtc 		history = (char **)alloc(histsize*sizeof (char *), APERM);
    605  1.1       jtc 		histptr = history - 1;
    606  1.1       jtc 	}
    607  1.1       jtc }
    608  1.1       jtc 
    609  1.1       jtc # ifdef EASY_HISTORY
    610  1.1       jtc /*
    611  1.1       jtc  * save command in history
    612  1.1       jtc  */
    613  1.1       jtc void
    614  1.1       jtc histsave(lno, cmd, dowrite)
    615  1.1       jtc 	int lno;	/* ignored (compatibility with COMPLEX_HISTORY) */
    616  1.1       jtc 	const char *cmd;
    617  1.1       jtc 	int dowrite;	/* ignored (compatibility with COMPLEX_HISTORY) */
    618  1.1       jtc {
    619  1.1       jtc 	register char **hp = histptr;
    620  1.1       jtc 	char *cp;
    621  1.1       jtc 
    622  1.1       jtc 	if (++hp >= history + histsize) { /* remove oldest command */
    623  1.1       jtc 		afree((void*)history[0], APERM);
    624  1.1       jtc 		memmove(history, history + 1,
    625  1.1       jtc 			sizeof(history[0]) * (histsize - 1));
    626  1.1       jtc 		hp = &history[histsize - 1];
    627  1.1       jtc 	}
    628  1.1       jtc 	*hp = str_save(cmd, APERM);
    629  1.1       jtc 	/* trash trailing newline but allow imbedded newlines */
    630  1.1       jtc 	cp = *hp + strlen(*hp);
    631  1.1       jtc 	if (cp > *hp && cp[-1] == '\n')
    632  1.1       jtc 		cp[-1] = '\0';
    633  1.1       jtc 	histptr = hp;
    634  1.1       jtc }
    635  1.1       jtc 
    636  1.1       jtc /*
    637  1.1       jtc  * Append an entry to the last saved command. Used for multiline
    638  1.1       jtc  * commands
    639  1.1       jtc  */
    640  1.1       jtc void
    641  1.4   hubertf histappend(cmd, nl_separate)
    642  1.1       jtc 	const char *cmd;
    643  1.4   hubertf 	int	nl_separate;
    644  1.1       jtc {
    645  1.1       jtc 	int	hlen, clen;
    646  1.1       jtc 	char	*p;
    647  1.1       jtc 
    648  1.1       jtc 	hlen = strlen(*histptr);
    649  1.1       jtc 	clen = strlen(cmd);
    650  1.1       jtc 	if (clen > 0 && cmd[clen-1] == '\n')
    651  1.1       jtc 		clen--;
    652  1.1       jtc 	p = *histptr = (char *) aresize(*histptr, hlen + clen + 2, APERM);
    653  1.1       jtc 	p += hlen;
    654  1.4   hubertf 	if (nl_separate)
    655  1.1       jtc 		*p++ = '\n';
    656  1.1       jtc 	memcpy(p, cmd, clen);
    657  1.1       jtc 	p[clen] = '\0';
    658  1.1       jtc }
    659  1.1       jtc 
    660  1.1       jtc /*
    661  1.1       jtc  * 92-04-25 <sjg@zen>
    662  1.1       jtc  * A simple history file implementation.
    663  1.1       jtc  * At present we only save the history when we exit.
    664  1.1       jtc  * This can cause problems when there are multiple shells are
    665  1.1       jtc  * running under the same user-id.  The last shell to exit gets
    666  1.1       jtc  * to save its history.
    667  1.1       jtc  */
    668  1.1       jtc void
    669  1.1       jtc hist_init(s)
    670  1.1       jtc 	Source *s;
    671  1.1       jtc {
    672  1.1       jtc 	char *f;
    673  1.1       jtc 	FILE *fh;
    674  1.1       jtc 
    675  1.1       jtc 	if (Flag(FTALKING) == 0)
    676  1.1       jtc 		return;
    677  1.1       jtc 
    678  1.1       jtc 	hstarted = 1;
    679  1.1       jtc 
    680  1.1       jtc 	hist_source = s;
    681  1.1       jtc 
    682  1.1       jtc 	if ((f = str_val(global("HISTFILE"))) == NULL || *f == '\0') {
    683  1.1       jtc # if 1 /* Don't use history file unless the user asks for it */
    684  1.1       jtc 		hname = NULL;
    685  1.1       jtc 		return;
    686  1.1       jtc # else
    687  1.1       jtc 		char *home = str_val(global("HOME"));
    688  1.1       jtc 		int len;
    689  1.1       jtc 
    690  1.1       jtc 		if (home == NULL)
    691  1.1       jtc 			home = null;
    692  1.1       jtc 		f = HISTFILE;
    693  1.1       jtc 		hname = alloc(len = strlen(home) + strlen(f) + 2, APERM);
    694  1.1       jtc 		shf_snprintf(hname, len, "%s/%s", home, f);
    695  1.1       jtc # endif
    696  1.1       jtc 	} else
    697  1.1       jtc 		hname = str_save(f, APERM);
    698  1.1       jtc 
    699  1.1       jtc 	if ((fh = fopen(hname, "r"))) {
    700  1.1       jtc 		int pos = 0, nread = 0;
    701  1.1       jtc 		int contin = 0;		/* continuation of previous command */
    702  1.1       jtc 		char *end;
    703  1.1       jtc 		char hline[LINE + 1];
    704  1.1       jtc 
    705  1.1       jtc 		while (1) {
    706  1.1       jtc 			if (pos >= nread) {
    707  1.1       jtc 				pos = 0;
    708  1.1       jtc 				nread = fread(hline, 1, LINE, fh);
    709  1.1       jtc 				if (nread <= 0)
    710  1.1       jtc 					break;
    711  1.1       jtc 				hline[nread] = '\0';
    712  1.1       jtc 			}
    713  1.1       jtc 			end = strchr(hline + pos, 0); /* will always succeed */
    714  1.1       jtc 			if (contin)
    715  1.1       jtc 				histappend(hline + pos, 0);
    716  1.1       jtc 			else {
    717  1.1       jtc 				hist_source->line++;
    718  1.1       jtc 				histsave(0, hline + pos, 0);
    719  1.1       jtc 			}
    720  1.1       jtc 			pos = end - hline + 1;
    721  1.1       jtc 			contin = end == &hline[nread];
    722  1.1       jtc 		}
    723  1.1       jtc 		fclose(fh);
    724  1.1       jtc 	}
    725  1.1       jtc }
    726  1.1       jtc 
    727  1.1       jtc /*
    728  1.1       jtc  * save our history.
    729  1.1       jtc  * We check that we do not have more than we are allowed.
    730  1.1       jtc  * If the history file is read-only we do nothing.
    731  1.1       jtc  * Handy for having all shells start with a useful history set.
    732  1.1       jtc  */
    733  1.1       jtc 
    734  1.1       jtc void
    735  1.1       jtc hist_finish()
    736  1.1       jtc {
    737  1.1       jtc   static int once;
    738  1.1       jtc   FILE *fh;
    739  1.1       jtc   register int i;
    740  1.1       jtc   register char **hp;
    741  1.1       jtc 
    742  1.1       jtc   if (once++)
    743  1.1       jtc     return;
    744  1.1       jtc   /* check how many we have */
    745  1.1       jtc   i = histptr - history;
    746  1.1       jtc   if (i >= histsize)
    747  1.1       jtc     hp = &histptr[-histsize];
    748  1.1       jtc   else
    749  1.1       jtc     hp = history;
    750  1.1       jtc   if (hname && (fh = fopen(hname, "w")))
    751  1.1       jtc   {
    752  1.1       jtc     for (i = 0; hp + i <= histptr && hp[i]; i++)
    753  1.1       jtc       fprintf(fh, "%s%c", hp[i], '\0');
    754  1.1       jtc     fclose(fh);
    755  1.1       jtc   }
    756  1.1       jtc }
    757  1.1       jtc 
    758  1.1       jtc # else /* EASY_HISTORY */
    759  1.1       jtc 
    760  1.1       jtc /*
    761  1.1       jtc  *	Routines added by Peter Collinson BSDI(Europe)/Hillside Systems to
    762  1.1       jtc  *	a) permit HISTSIZE to control number of lines of history stored
    763  1.1       jtc  *	b) maintain a physical history file
    764  1.1       jtc  *
    765  1.1       jtc  *	It turns out that there is a lot of ghastly hackery here
    766  1.1       jtc  */
    767  1.1       jtc 
    768  1.1       jtc 
    769  1.1       jtc /*
    770  1.1       jtc  * save command in history
    771  1.1       jtc  */
    772  1.1       jtc void
    773  1.1       jtc histsave(lno, cmd, dowrite)
    774  1.1       jtc 	int lno;
    775  1.1       jtc 	const char *cmd;
    776  1.1       jtc 	int dowrite;
    777  1.1       jtc {
    778  1.1       jtc 	register char **hp;
    779  1.1       jtc 	char *c, *cp;
    780  1.1       jtc 
    781  1.1       jtc 	c = str_save(cmd, APERM);
    782  1.1       jtc 	if ((cp = strchr(c, '\n')) != NULL)
    783  1.1       jtc 		*cp = '\0';
    784  1.1       jtc 
    785  1.1       jtc 	if (histfd && dowrite)
    786  1.1       jtc 		writehistfile(lno, c);
    787  1.1       jtc 
    788  1.1       jtc 	hp = histptr;
    789  1.1       jtc 
    790  1.1       jtc 	if (++hp >= history + histsize) { /* remove oldest command */
    791  1.1       jtc 		afree((void*)*history, APERM);
    792  1.1       jtc 		for (hp = history; hp < history + histsize - 1; hp++)
    793  1.1       jtc 			hp[0] = hp[1];
    794  1.1       jtc 	}
    795  1.1       jtc 	*hp = c;
    796  1.1       jtc 	histptr = hp;
    797  1.1       jtc }
    798  1.1       jtc 
    799  1.1       jtc /*
    800  1.1       jtc  *	Write history data to a file nominated by HISTFILE
    801  1.1       jtc  *	if HISTFILE is unset then history still happens, but
    802  1.1       jtc  *	the data is not written to a file
    803  1.1       jtc  *	All copies of ksh looking at the file will maintain the
    804  1.1       jtc  *	same history. This is ksh behaviour.
    805  1.1       jtc  *
    806  1.1       jtc  *	This stuff uses mmap()
    807  1.1       jtc  *	if your system ain't got it - then you'll have to undef HISTORYFILE
    808  1.1       jtc  */
    809  1.1       jtc 
    810  1.1       jtc /*
    811  1.1       jtc  *	Open a history file
    812  1.1       jtc  *	Format is:
    813  1.1       jtc  *	Bytes 1, 2: HMAGIC - just to check that we are dealing with
    814  1.1       jtc  *		    the correct object
    815  1.1       jtc  *	Then follows a number of stored commands
    816  1.1       jtc  *	Each command is
    817  1.1       jtc  *	<command byte><command number(4 bytes)><bytes><null>
    818  1.1       jtc  */
    819  1.1       jtc # define HMAGIC1		0xab
    820  1.1       jtc # define HMAGIC2		0xcd
    821  1.1       jtc # define COMMAND		0xff
    822  1.1       jtc 
    823  1.1       jtc void
    824  1.1       jtc hist_init(s)
    825  1.1       jtc 	Source *s;
    826  1.1       jtc {
    827  1.1       jtc 	unsigned char	*base;
    828  1.1       jtc 	int	lines;
    829  1.1       jtc 	int	fd;
    830  1.1       jtc 
    831  1.1       jtc 	if (Flag(FTALKING) == 0)
    832  1.1       jtc 		return;
    833  1.1       jtc 
    834  1.1       jtc 	hstarted = 1;
    835  1.1       jtc 
    836  1.1       jtc 	hist_source = s;
    837  1.1       jtc 
    838  1.1       jtc 	hname = str_val(global("HISTFILE"));
    839  1.1       jtc 	if (hname == NULL)
    840  1.1       jtc 		return;
    841  1.1       jtc 	hname = str_save(hname, APERM);
    842  1.1       jtc 
    843  1.1       jtc   retry:
    844  1.1       jtc 	/* we have a file and are interactive */
    845  1.1       jtc 	if ((fd = open(hname, O_RDWR|O_CREAT|O_APPEND, 0600)) < 0)
    846  1.1       jtc 		return;
    847  1.1       jtc 
    848  1.1       jtc 	histfd = savefd(fd, 0);
    849  1.1       jtc 
    850  1.1       jtc 	(void) flock(histfd, LOCK_EX);
    851  1.1       jtc 
    852  1.1       jtc 	hsize = lseek(histfd, 0L, SEEK_END);
    853  1.1       jtc 
    854  1.1       jtc 	if (hsize == 0) {
    855  1.1       jtc 		/* add magic */
    856  1.1       jtc 		if (sprinkle(histfd)) {
    857  1.1       jtc 			hist_finish();
    858  1.1       jtc 			return;
    859  1.1       jtc 		}
    860  1.1       jtc 	}
    861  1.1       jtc 	else if (hsize > 0) {
    862  1.1       jtc 		/*
    863  1.1       jtc 		 * we have some data
    864  1.1       jtc 		 */
    865  1.1       jtc 		base = (unsigned char *)mmap(0, hsize, PROT_READ, MAP_FLAGS, histfd, 0);
    866  1.1       jtc 		/*
    867  1.1       jtc 		 * check on its validity
    868  1.1       jtc 		 */
    869  1.1       jtc 		if ((int)base == -1 || *base != HMAGIC1 || base[1] != HMAGIC2) {
    870  1.1       jtc 			if ((int)base !=  -1)
    871  1.1       jtc 				munmap((caddr_t)base, hsize);
    872  1.1       jtc 			hist_finish();
    873  1.1       jtc 			unlink(hname);
    874  1.1       jtc 			goto retry;
    875  1.1       jtc 		}
    876  1.1       jtc 		if (hsize > 2) {
    877  1.1       jtc 			lines = hist_count_lines(base+2, hsize-2);
    878  1.1       jtc 			if (lines > histsize) {
    879  1.1       jtc 				/* we need to make the file smaller */
    880  1.1       jtc 				if (hist_shrink(base, hsize))
    881  1.1       jtc 					unlink(hname);
    882  1.1       jtc 				munmap((caddr_t)base, hsize);
    883  1.1       jtc 				hist_finish();
    884  1.1       jtc 				goto retry;
    885  1.1       jtc 			}
    886  1.1       jtc 		}
    887  1.1       jtc 		histload(hist_source, base+2, hsize-2);
    888  1.1       jtc 		munmap((caddr_t)base, hsize);
    889  1.1       jtc 	}
    890  1.1       jtc 	(void) flock(histfd, LOCK_UN);
    891  1.1       jtc 	hsize = lseek(histfd, 0L, SEEK_END);
    892  1.1       jtc }
    893  1.1       jtc 
    894  1.1       jtc typedef enum state {
    895  1.1       jtc 	shdr,		/* expecting a header */
    896  1.1       jtc 	sline,		/* looking for a null byte to end the line */
    897  1.1       jtc 	sn1,		/* bytes 1 to 4 of a line no */
    898  1.1       jtc 	sn2, sn3, sn4,
    899  1.1       jtc } State;
    900  1.1       jtc 
    901  1.1       jtc static int
    902  1.1       jtc hist_count_lines(base, bytes)
    903  1.1       jtc 	register unsigned char *base;
    904  1.1       jtc 	register int bytes;
    905  1.1       jtc {
    906  1.1       jtc 	State state = shdr;
    907  1.1       jtc 	register lines = 0;
    908  1.1       jtc 
    909  1.1       jtc 	while (bytes--) {
    910  1.1       jtc 		switch (state)
    911  1.1       jtc 		{
    912  1.1       jtc 		case shdr:
    913  1.1       jtc 			if (*base == COMMAND)
    914  1.1       jtc 				state = sn1;
    915  1.1       jtc 			break;
    916  1.1       jtc 		case sn1:
    917  1.1       jtc 			state = sn2; break;
    918  1.1       jtc 		case sn2:
    919  1.1       jtc 			state = sn3; break;
    920  1.1       jtc 		case sn3:
    921  1.1       jtc 			state = sn4; break;
    922  1.1       jtc 		case sn4:
    923  1.1       jtc 			state = sline; break;
    924  1.1       jtc 		case sline:
    925  1.1       jtc 			if (*base == '\0')
    926  1.1       jtc 				lines++, state = shdr;
    927  1.1       jtc 		}
    928  1.1       jtc 		base++;
    929  1.1       jtc 	}
    930  1.1       jtc 	return lines;
    931  1.1       jtc }
    932  1.1       jtc 
    933  1.1       jtc /*
    934  1.1       jtc  *	Shrink the history file to histsize lines
    935  1.1       jtc  */
    936  1.1       jtc static int
    937  1.1       jtc hist_shrink(oldbase, oldbytes)
    938  1.1       jtc 	unsigned char *oldbase;
    939  1.1       jtc 	int oldbytes;
    940  1.1       jtc {
    941  1.1       jtc 	int fd;
    942  1.1       jtc 	char	nfile[1024];
    943  1.1       jtc 	struct	stat statb;
    944  1.1       jtc 	unsigned char *nbase = oldbase;
    945  1.1       jtc 	int nbytes = oldbytes;
    946  1.1       jtc 
    947  1.1       jtc 	nbase = hist_skip_back(nbase, &nbytes, histsize);
    948  1.1       jtc 	if (nbase == NULL)
    949  1.1       jtc 		return 1;
    950  1.1       jtc 	if (nbase == oldbase)
    951  1.1       jtc 		return 0;
    952  1.1       jtc 
    953  1.1       jtc 	/*
    954  1.1       jtc 	 *	create temp file
    955  1.1       jtc 	 */
    956  1.1       jtc 	(void) shf_snprintf(nfile, sizeof(nfile), "%s.%d", hname, procpid);
    957  1.1       jtc 	if ((fd = creat(nfile, 0600)) < 0)
    958  1.1       jtc 		return 1;
    959  1.1       jtc 
    960  1.1       jtc 	if (sprinkle(fd)) {
    961  1.1       jtc 		close(fd);
    962  1.1       jtc 		unlink(nfile);
    963  1.1       jtc 		return 1;
    964  1.1       jtc 	}
    965  1.1       jtc 	if (write(fd, nbase, nbytes) != nbytes) {
    966  1.1       jtc 		close(fd);
    967  1.1       jtc 		unlink(nfile);
    968  1.1       jtc 		return 1;
    969  1.1       jtc 	}
    970  1.1       jtc 	/*
    971  1.1       jtc 	 *	worry about who owns this file
    972  1.1       jtc 	 */
    973  1.1       jtc 	if (fstat(histfd, &statb) >= 0)
    974  1.1       jtc 		fchown(fd, statb.st_uid, statb.st_gid);
    975  1.1       jtc 	close(fd);
    976  1.1       jtc 
    977  1.1       jtc 	/*
    978  1.1       jtc 	 *	rename
    979  1.1       jtc 	 */
    980  1.1       jtc 	if (rename(nfile, hname) < 0)
    981  1.1       jtc 		return 1;
    982  1.1       jtc 	return 0;
    983  1.1       jtc }
    984  1.1       jtc 
    985  1.1       jtc 
    986  1.1       jtc /*
    987  1.1       jtc  *	find a pointer to the data `no' back from the end of the file
    988  1.1       jtc  *	return the pointer and the number of bytes left
    989  1.1       jtc  */
    990  1.1       jtc static unsigned char *
    991  1.1       jtc hist_skip_back(base, bytes, no)
    992  1.1       jtc 	unsigned char *base;
    993  1.1       jtc 	int *bytes;
    994  1.1       jtc 	int no;
    995  1.1       jtc {
    996  1.1       jtc 	register int lines = 0;
    997  1.1       jtc 	register unsigned char *ep;
    998  1.1       jtc 
    999  1.1       jtc 	for (ep = base + *bytes; --ep > base; ) {
   1000  1.1       jtc 		/* this doesn't really work: the 4 byte line number that is
   1001  1.1       jtc 		 * encoded after the COMMAND byte can itself contain the
   1002  1.1       jtc 		 * COMMAND byte....
   1003  1.1       jtc 		 */
   1004  1.1       jtc 		for (; ep > base && *ep != COMMAND; ep--)
   1005  1.1       jtc 			;
   1006  1.1       jtc 		if (ep == base)
   1007  1.1       jtc 			break;
   1008  1.1       jtc 		if (++lines == no) {
   1009  1.1       jtc 			*bytes = *bytes - ((char *)ep - (char *)base);
   1010  1.1       jtc 			return ep;
   1011  1.1       jtc 		}
   1012  1.1       jtc 	}
   1013  1.1       jtc 	return NULL;
   1014  1.1       jtc }
   1015  1.1       jtc 
   1016  1.1       jtc /*
   1017  1.1       jtc  *	load the history structure from the stored data
   1018  1.1       jtc  */
   1019  1.1       jtc static void
   1020  1.1       jtc histload(s, base, bytes)
   1021  1.1       jtc 	Source *s;
   1022  1.1       jtc 	register unsigned char *base;
   1023  1.1       jtc 	register int bytes;
   1024  1.1       jtc {
   1025  1.1       jtc 	State state;
   1026  1.1       jtc 	int	lno;
   1027  1.1       jtc 	unsigned char	*line;
   1028  1.1       jtc 
   1029  1.1       jtc 	for (state = shdr; bytes-- > 0; base++) {
   1030  1.1       jtc 		switch (state) {
   1031  1.1       jtc 		case shdr:
   1032  1.1       jtc 			if (*base == COMMAND)
   1033  1.1       jtc 				state = sn1;
   1034  1.1       jtc 			break;
   1035  1.1       jtc 		case sn1:
   1036  1.1       jtc 			lno = (((*base)&0xff)<<24);
   1037  1.1       jtc 			state = sn2;
   1038  1.1       jtc 			break;
   1039  1.1       jtc 		case sn2:
   1040  1.1       jtc 			lno |= (((*base)&0xff)<<16);
   1041  1.1       jtc 			state = sn3;
   1042  1.1       jtc 			break;
   1043  1.1       jtc 		case sn3:
   1044  1.1       jtc 			lno |= (((*base)&0xff)<<8);
   1045  1.1       jtc 			state = sn4;
   1046  1.1       jtc 			break;
   1047  1.1       jtc 		case sn4:
   1048  1.1       jtc 			lno |= (*base)&0xff;
   1049  1.1       jtc 			line = base+1;
   1050  1.1       jtc 			state = sline;
   1051  1.1       jtc 			break;
   1052  1.1       jtc 		case sline:
   1053  1.1       jtc 			if (*base == '\0') {
   1054  1.1       jtc 				/* worry about line numbers */
   1055  1.1       jtc 				if (histptr >= history && lno-1 != s->line) {
   1056  1.1       jtc 					/* a replacement ? */
   1057  1.1       jtc 					histinsert(s, lno, line);
   1058  1.1       jtc 				}
   1059  1.1       jtc 				else {
   1060  1.1       jtc 					s->line = lno;
   1061  1.1       jtc 					histsave(lno, (char *)line, 0);
   1062  1.1       jtc 				}
   1063  1.1       jtc 				state = shdr;
   1064  1.1       jtc 			}
   1065  1.1       jtc 		}
   1066  1.1       jtc 	}
   1067  1.1       jtc }
   1068  1.1       jtc 
   1069  1.1       jtc /*
   1070  1.1       jtc  *	Insert a line into the history at a specified number
   1071  1.1       jtc  */
   1072  1.1       jtc static void
   1073  1.1       jtc histinsert(s, lno, line)
   1074  1.1       jtc 	Source *s;
   1075  1.1       jtc 	int lno;
   1076  1.1       jtc 	unsigned char *line;
   1077  1.1       jtc {
   1078  1.1       jtc 	register char **hp;
   1079  1.1       jtc 
   1080  1.1       jtc 	if (lno >= s->line-(histptr-history) && lno <= s->line) {
   1081  1.1       jtc 		hp = &histptr[lno-s->line];
   1082  1.1       jtc 		if (*hp)
   1083  1.1       jtc 			afree((void*)*hp, APERM);
   1084  1.1       jtc 		*hp = str_save((char *)line, APERM);
   1085  1.1       jtc 	}
   1086  1.1       jtc }
   1087  1.1       jtc 
   1088  1.1       jtc /*
   1089  1.1       jtc  *	write a command to the end of the history file
   1090  1.1       jtc  *	This *MAY* seem easy but it's also necessary to check
   1091  1.1       jtc  *	that the history file has not changed in size.
   1092  1.1       jtc  *	If it has - then some other shell has written to it
   1093  1.1       jtc  *	and we should read those commands to update our history
   1094  1.1       jtc  */
   1095  1.1       jtc static void
   1096  1.1       jtc writehistfile(lno, cmd)
   1097  1.1       jtc 	int lno;
   1098  1.1       jtc 	char *cmd;
   1099  1.1       jtc {
   1100  1.1       jtc 	int	sizenow;
   1101  1.1       jtc 	unsigned char	*base;
   1102  1.1       jtc 	unsigned char	*new;
   1103  1.1       jtc 	int	bytes;
   1104  1.1       jtc 	char	hdr[5];
   1105  1.1       jtc 
   1106  1.1       jtc 	(void) flock(histfd, LOCK_EX);
   1107  1.1       jtc 	sizenow = lseek(histfd, 0L, SEEK_END);
   1108  1.1       jtc 	if (sizenow != hsize) {
   1109  1.1       jtc 		/*
   1110  1.1       jtc 		 *	Things have changed
   1111  1.1       jtc 		 */
   1112  1.1       jtc 		if (sizenow > hsize) {
   1113  1.1       jtc 			/* someone has added some lines */
   1114  1.1       jtc 			bytes = sizenow - hsize;
   1115  1.1       jtc 			base = (unsigned char *)mmap(0, sizenow, PROT_READ, MAP_FLAGS, histfd, 0);
   1116  1.1       jtc 			if ((int)base == -1)
   1117  1.1       jtc 				goto bad;
   1118  1.1       jtc 			new = base + hsize;
   1119  1.1       jtc 			if (*new != COMMAND) {
   1120  1.1       jtc 				munmap((caddr_t)base, sizenow);
   1121  1.1       jtc 				goto bad;
   1122  1.1       jtc 			}
   1123  1.1       jtc 			hist_source->line--;
   1124  1.1       jtc 			histload(hist_source, new, bytes);
   1125  1.1       jtc 			hist_source->line++;
   1126  1.1       jtc 			lno = hist_source->line;
   1127  1.1       jtc 			munmap((caddr_t)base, sizenow);
   1128  1.1       jtc 			hsize = sizenow;
   1129  1.1       jtc 		} else {
   1130  1.1       jtc 			/* it has shrunk */
   1131  1.1       jtc 			/* but to what? */
   1132  1.1       jtc 			/* we'll give up for now */
   1133  1.1       jtc 			goto bad;
   1134  1.1       jtc 		}
   1135  1.1       jtc 	}
   1136  1.1       jtc 	/*
   1137  1.1       jtc 	 *	we can write our bit now
   1138  1.1       jtc 	 */
   1139  1.1       jtc 	hdr[0] = COMMAND;
   1140  1.1       jtc 	hdr[1] = (lno>>24)&0xff;
   1141  1.1       jtc 	hdr[2] = (lno>>16)&0xff;
   1142  1.1       jtc 	hdr[3] = (lno>>8)&0xff;
   1143  1.1       jtc 	hdr[4] = lno&0xff;
   1144  1.1       jtc 	(void) write(histfd, hdr, 5);
   1145  1.1       jtc 	(void) write(histfd, cmd, strlen(cmd)+1);
   1146  1.1       jtc 	hsize = lseek(histfd, 0L, SEEK_END);
   1147  1.1       jtc 	(void) flock(histfd, LOCK_UN);
   1148  1.1       jtc 	return;
   1149  1.1       jtc bad:
   1150  1.1       jtc 	hist_finish();
   1151  1.1       jtc }
   1152  1.1       jtc 
   1153  1.1       jtc void
   1154  1.1       jtc hist_finish()
   1155  1.1       jtc {
   1156  1.1       jtc 	(void) flock(histfd, LOCK_UN);
   1157  1.1       jtc 	(void) close(histfd);
   1158  1.1       jtc 	histfd = 0;
   1159  1.1       jtc }
   1160  1.1       jtc 
   1161  1.1       jtc /*
   1162  1.1       jtc  *	add magic to the history file
   1163  1.1       jtc  */
   1164  1.1       jtc static int
   1165  1.1       jtc sprinkle(fd)
   1166  1.1       jtc 	int fd;
   1167  1.1       jtc {
   1168  1.1       jtc 	static char mag[] = { HMAGIC1, HMAGIC2 };
   1169  1.1       jtc 
   1170  1.1       jtc 	return(write(fd, mag, 2) != 2);
   1171  1.1       jtc }
   1172  1.1       jtc 
   1173  1.1       jtc # endif
   1174  1.1       jtc #else /* HISTORY */
   1175  1.1       jtc 
   1176  1.1       jtc /* No history to be compiled in: dummy routines to avoid lots more ifdefs */
   1177  1.1       jtc void
   1178  1.1       jtc init_histvec()
   1179  1.1       jtc {
   1180  1.1       jtc }
   1181  1.1       jtc void
   1182  1.1       jtc hist_init(s)
   1183  1.1       jtc 	Source *s;
   1184  1.1       jtc {
   1185  1.1       jtc }
   1186  1.1       jtc void
   1187  1.1       jtc hist_finish()
   1188  1.1       jtc {
   1189  1.1       jtc }
   1190  1.1       jtc void
   1191  1.1       jtc histsave(lno, cmd, dowrite)
   1192  1.1       jtc 	int lno;
   1193  1.1       jtc 	const char *cmd;
   1194  1.1       jtc 	int dowrite;
   1195  1.1       jtc {
   1196  1.1       jtc 	errorf("history not enabled");
   1197  1.1       jtc }
   1198  1.1       jtc #endif /* HISTORY */
   1199