1N/A/***********************************************************************
1N/A* *
1N/A* This software is part of the ast package *
1N/A* Copyright (c) 1982-2011 AT&T Intellectual Property *
1N/A* and is licensed under the *
1N/A* Common Public License, Version 1.0 *
1N/A* by AT&T Intellectual Property *
1N/A* *
1N/A* A copy of the License is available at *
1N/A* http://www.opensource.org/licenses/cpl1.0.txt *
1N/A* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
1N/A* *
1N/A* Information and Software Systems Research *
1N/A* AT&T Research *
1N/A* Florham Park NJ *
1N/A* *
1N/A* David Korn <dgk@research.att.com> *
1N/A* *
1N/A***********************************************************************/
1N/A#pragma prototyped
1N/A/*
1N/A * History file manipulation routines
1N/A *
1N/A * David Korn
1N/A * AT&T Labs
1N/A *
1N/A */
1N/A
1N/A/*
1N/A * Each command in the history file starts on an even byte is null terminated.
1N/A * The first byte must contain the special character HIST_UNDO and the second
1N/A * byte is the version number. The sequence HIST_UNDO 0, following a command,
1N/A * nullifies the previous command. A six byte sequence starting with
1N/A * HIST_CMDNO is used to store the command number so that it is not necessary
1N/A * to read the file from beginning to end to get to the last block of
1N/A * commands. This format of this sequence is different in version 1
1N/A * then in version 0. Version 1 allows commands to use the full 8 bit
1N/A * character set. It can understand version 0 format files.
1N/A */
1N/A
1N/A
1N/A#define HIST_MAX (sizeof(int)*HIST_BSIZE)
1N/A#define HIST_BIG (0100000-1024) /* 1K less than maximum short */
1N/A#define HIST_LINE 32 /* typical length for history line */
1N/A#define HIST_MARKSZ 6
1N/A#define HIST_RECENT 600
1N/A#define HIST_UNDO 0201 /* invalidate previous command */
1N/A#define HIST_CMDNO 0202 /* next 3 bytes give command number */
1N/A#define HIST_BSIZE 4096 /* size of history file buffer */
1N/A#define HIST_DFLT 512 /* default size of history list */
1N/A
1N/A#if SHOPT_AUDIT
1N/A# define _HIST_AUDIT Sfio_t *auditfp; \
1N/A char *tty; \
1N/A int auditmask;
1N/A#else
1N/A# define _HIST_AUDIT
1N/A#endif
1N/A
1N/A#define _HIST_PRIVATE \
1N/A void *histshell; \
1N/A off_t histcnt; /* offset into history file */\
1N/A off_t histmarker; /* offset of last command marker */ \
1N/A int histflush; /* set if flushed outside of hflush() */\
1N/A int histmask; /* power of two mask for histcnt */ \
1N/A char histbuff[HIST_BSIZE+1]; /* history file buffer */ \
1N/A int histwfail; \
1N/A _HIST_AUDIT \
1N/A off_t histcmds[2]; /* offset for recent commands, must be last */
1N/A
1N/A#define hist_ind(hp,c) ((int)((c)&(hp)->histmask))
1N/A
1N/A#include <ast.h>
1N/A#include <sfio.h>
1N/A#include "FEATURE/time"
1N/A#include <error.h>
1N/A#include <ls.h>
1N/A#if KSHELL
1N/A# include "defs.h"
1N/A# include "variables.h"
1N/A# include "path.h"
1N/A# include "builtins.h"
1N/A# include "io.h"
1N/A#else
1N/A# include <ctype.h>
1N/A#endif /* KSHELL */
1N/A#include "history.h"
1N/A
1N/A#if !KSHELL
1N/A# define new_of(type,x) ((type*)malloc((unsigned)sizeof(type)+(x)))
1N/A# define NIL(type) ((type)0)
1N/A# define path_relative(s,x) (s,x)
1N/A# ifdef __STDC__
1N/A# define nv_getval(s) getenv(#s)
1N/A# else
1N/A# define nv_getval(s) getenv("s")
1N/A# endif /* __STDC__ */
1N/A# define e_unknown "unknown"
1N/A# define sh_translate(x) (x)
1N/A char login_sh = 0;
1N/A char hist_fname[] = "/.history";
1N/A#endif /* KSHELL */
1N/A
1N/A#ifndef O_BINARY
1N/A# define O_BINARY 0
1N/A#endif /* O_BINARY */
1N/A
1N/Aint _Hist = 0;
1N/Astatic void hist_marker(char*,long);
1N/Astatic History_t* hist_trim(History_t*, int);
1N/Astatic int hist_nearend(History_t*,Sfio_t*, off_t);
1N/Astatic int hist_check(int);
1N/Astatic int hist_clean(int);
1N/A#ifdef SF_BUFCONST
1N/A static ssize_t hist_write(Sfio_t*, const void*, size_t, Sfdisc_t*);
1N/A static int hist_exceptf(Sfio_t*, int, void*, Sfdisc_t*);
1N/A#else
1N/A static int hist_write(Sfio_t*, const void*, int, Sfdisc_t*);
1N/A static int hist_exceptf(Sfio_t*, int, Sfdisc_t*);
1N/A#endif
1N/A
1N/A
1N/Astatic int histinit;
1N/Astatic mode_t histmode;
1N/Astatic History_t *wasopen;
1N/Astatic History_t *hist_ptr;
1N/A
1N/A#if SHOPT_ACCTFILE
1N/A static int acctfd;
1N/A static char *logname;
1N/A# include <pwd.h>
1N/A
1N/A static int acctinit(History_t *hp)
1N/A {
1N/A register char *cp, *acctfile;
1N/A Namval_t *np = nv_search("ACCTFILE",((Shell_t*)hp->histshell)->var_tree,0);
1N/A
1N/A if(!np || !(acctfile=nv_getval(np)))
1N/A return(0);
1N/A if(!(cp = getlogin()))
1N/A {
1N/A struct passwd *userinfo = getpwuid(getuid());
1N/A if(userinfo)
1N/A cp = userinfo->pw_name;
1N/A else
1N/A cp = "unknown";
1N/A }
1N/A logname = strdup(cp);
1N/A if((acctfd=sh_open(acctfile,
1N/A O_BINARY|O_WRONLY|O_APPEND|O_CREAT,S_IRUSR|S_IWUSR))>=0 &&
1N/A (unsigned)acctfd < 10)
1N/A {
1N/A int n;
1N/A if((n = fcntl(acctfd, F_DUPFD, 10)) >= 0)
1N/A {
1N/A close(acctfd);
1N/A acctfd = n;
1N/A }
1N/A }
1N/A if(acctfd < 0)
1N/A {
1N/A acctfd = 0;
1N/A return(0);
1N/A }
1N/A if(sh_isdevfd(acctfile))
1N/A {
1N/A char newfile[16];
1N/A sfsprintf(newfile,sizeof(newfile),"%.8s%d\0",e_devfdNN,acctfd);
1N/A nv_putval(np,newfile,NV_RDONLY);
1N/A }
1N/A else
1N/A fcntl(acctfd,F_SETFD,FD_CLOEXEC);
1N/A return(1);
1N/A }
1N/A#endif /* SHOPT_ACCTFILE */
1N/A
1N/A#if SHOPT_AUDIT
1N/Astatic int sh_checkaudit(History_t *hp, const char *name, char *logbuf, size_t len)
1N/A{
1N/A char *buff, *cp, *last;
1N/A int id1, id2, r=0, n, fd;
1N/A if((fd=open(name, O_RDONLY)) < 0)
1N/A return(0);
1N/A if((n = read(fd, logbuf,len-1)) < 0)
1N/A goto done;
1N/A while(logbuf[n-1]=='\n')
1N/A n--;
1N/A logbuf[n] = 0;
1N/A if(!(cp=strchr(logbuf,';')) && !(cp=strchr(logbuf,' ')))
1N/A goto done;
1N/A *cp = 0;
1N/A do
1N/A {
1N/A cp++;
1N/A id1 = id2 = strtol(cp,&last,10);
1N/A if(*last=='-')
1N/A id1 = strtol(last+1,&last,10);
1N/A if(shgd->euserid >=id1 && shgd->euserid <= id2)
1N/A r |= 1;
1N/A if(shgd->userid >=id1 && shgd->userid <= id2)
1N/A r |= 2;
1N/A cp = last;
1N/A }
1N/A while(*cp==';' || *cp==' ');
1N/Adone:
1N/A close(fd);
1N/A return(r);
1N/A
1N/A}
1N/A#endif /*SHOPT_AUDIT*/
1N/A
1N/Astatic const unsigned char hist_stamp[2] = { HIST_UNDO, HIST_VERSION };
1N/Astatic const Sfdisc_t hist_disc = { NULL, hist_write, NULL, hist_exceptf, NULL};
1N/A
1N/Astatic void hist_touch(void *handle)
1N/A{
1N/A touch((char*)handle, (time_t)0, (time_t)0, 0);
1N/A}
1N/A
1N/A/*
1N/A * open the history file
1N/A * if HISTNAME is not given and userid==0 then no history file.
1N/A * if login_sh and HISTFILE is longer than HIST_MAX bytes then it is
1N/A * cleaned up.
1N/A * hist_open() returns 1, if history file is open
1N/A */
1N/Aint sh_histinit(void *sh_context)
1N/A{
1N/A Shell_t *shp = (Shell_t*)sh_context;
1N/A register int fd;
1N/A register History_t *hp;
1N/A register char *histname;
1N/A char *fname=0;
1N/A int histmask, maxlines, hist_start=0;
1N/A register char *cp;
1N/A register off_t hsize = 0;
1N/A
1N/A if(shgd->hist_ptr=hist_ptr)
1N/A return(1);
1N/A if(!(histname = nv_getval(HISTFILE)))
1N/A {
1N/A int offset = staktell();
1N/A if(cp=nv_getval(HOME))
1N/A stakputs(cp);
1N/A stakputs(hist_fname);
1N/A stakputc(0);
1N/A stakseek(offset);
1N/A histname = stakptr(offset);
1N/A }
1N/A#ifdef future
1N/A if(hp=wasopen)
1N/A {
1N/A /* reuse history file if same name */
1N/A wasopen = 0;
1N/A shgd->hist_ptr = hist_ptr = hp;
1N/A if(strcmp(histname,hp->histname)==0)
1N/A return(1);
1N/A else
1N/A hist_free();
1N/A }
1N/A#endif
1N/Aretry:
1N/A cp = path_relative(shp,histname);
1N/A if(!histinit)
1N/A histmode = S_IRUSR|S_IWUSR;
1N/A if((fd=open(cp,O_BINARY|O_APPEND|O_RDWR|O_CREAT,histmode))>=0)
1N/A {
1N/A hsize=lseek(fd,(off_t)0,SEEK_END);
1N/A }
1N/A if((unsigned)fd <=2)
1N/A {
1N/A int n;
1N/A if((n=fcntl(fd,F_DUPFD,10))>=0)
1N/A {
1N/A close(fd);
1N/A fd=n;
1N/A }
1N/A }
1N/A /* make sure that file has history file format */
1N/A if(hsize && hist_check(fd))
1N/A {
1N/A close(fd);
1N/A hsize = 0;
1N/A if(unlink(cp)>=0)
1N/A goto retry;
1N/A fd = -1;
1N/A }
1N/A if(fd < 0)
1N/A {
1N/A#if KSHELL
1N/A /* don't allow root a history_file in /tmp */
1N/A if(shgd->userid)
1N/A#endif /* KSHELL */
1N/A {
1N/A if(!(fname = pathtmp(NIL(char*),0,0,NIL(int*))))
1N/A return(0);
1N/A fd = open(fname,O_BINARY|O_APPEND|O_CREAT|O_RDWR,S_IRUSR|S_IWUSR);
1N/A }
1N/A }
1N/A if(fd<0)
1N/A return(0);
1N/A /* set the file to close-on-exec */
1N/A fcntl(fd,F_SETFD,FD_CLOEXEC);
1N/A if(cp=nv_getval(HISTSIZE))
1N/A maxlines = (unsigned)strtol(cp, (char**)0, 10);
1N/A else
1N/A maxlines = HIST_DFLT;
1N/A for(histmask=16;histmask <= maxlines; histmask <<=1 );
1N/A if(!(hp=new_of(History_t,(--histmask)*sizeof(off_t))))
1N/A {
1N/A close(fd);
1N/A return(0);
1N/A }
1N/A shgd->hist_ptr = hist_ptr = hp;
1N/A hp->histshell = (void*)shp;
1N/A hp->histsize = maxlines;
1N/A hp->histmask = histmask;
1N/A hp->histfp= sfnew(NIL(Sfio_t*),hp->histbuff,HIST_BSIZE,fd,SF_READ|SF_WRITE|SF_APPENDWR|SF_SHARE);
1N/A memset((char*)hp->histcmds,0,sizeof(off_t)*(hp->histmask+1));
1N/A hp->histind = 1;
1N/A hp->histcmds[1] = 2;
1N/A hp->histcnt = 2;
1N/A hp->histname = strdup(histname);
1N/A hp->histdisc = hist_disc;
1N/A if(hsize==0)
1N/A {
1N/A /* put special characters at front of file */
1N/A sfwrite(hp->histfp,(char*)hist_stamp,2);
1N/A sfsync(hp->histfp);
1N/A }
1N/A /* initialize history list */
1N/A else
1N/A {
1N/A int first,last;
1N/A off_t mark,size = (HIST_MAX/4)+maxlines*HIST_LINE;
1N/A hp->histind = first = hist_nearend(hp,hp->histfp,hsize-size);
1N/A hist_eof(hp); /* this sets histind to last command */
1N/A if((hist_start = (last=(int)hp->histind)-maxlines) <=0)
1N/A hist_start = 1;
1N/A mark = hp->histmarker;
1N/A while(first > hist_start)
1N/A {
1N/A size += size;
1N/A first = hist_nearend(hp,hp->histfp,hsize-size);
1N/A hp->histind = first;
1N/A }
1N/A histinit = hist_start;
1N/A hist_eof(hp);
1N/A if(!histinit)
1N/A {
1N/A sfseek(hp->histfp,hp->histcnt=hsize,SEEK_SET);
1N/A hp->histind = last;
1N/A hp->histmarker = mark;
1N/A }
1N/A histinit = 0;
1N/A }
1N/A if(fname)
1N/A {
1N/A unlink(fname);
1N/A free((void*)fname);
1N/A }
1N/A if(hist_clean(fd) && hist_start>1 && hsize > HIST_MAX)
1N/A {
1N/A#ifdef DEBUG
1N/A sfprintf(sfstderr,"%d: hist_trim hsize=%d\n",getpid(),hsize);
1N/A sfsync(sfstderr);
1N/A#endif /* DEBUG */
1N/A hp = hist_trim(hp,(int)hp->histind-maxlines);
1N/A }
1N/A sfdisc(hp->histfp,&hp->histdisc);
1N/A#if KSHELL
1N/A (HISTCUR)->nvalue.lp = (&hp->histind);
1N/A#endif /* KSHELL */
1N/A sh_timeradd(1000L*(HIST_RECENT-30), 1, hist_touch, (void*)hp->histname);
1N/A#if SHOPT_ACCTFILE
1N/A if(sh_isstate(SH_INTERACTIVE))
1N/A acctinit(hp);
1N/A#endif /* SHOPT_ACCTFILE */
1N/A#if SHOPT_AUDIT
1N/A {
1N/A char buff[SF_BUFSIZE];
1N/A hp->auditfp = 0;
1N/A if(sh_isstate(SH_INTERACTIVE) && (hp->auditmask=sh_checkaudit(hp,SHOPT_AUDITFILE, buff, sizeof(buff))))
1N/A {
1N/A if((fd=sh_open(buff,O_BINARY|O_WRONLY|O_APPEND|O_CREAT,S_IRUSR|S_IWUSR))>=0 && fd < 10)
1N/A {
1N/A int n;
1N/A if((n = sh_fcntl(fd,F_DUPFD, 10)) >= 0)
1N/A {
1N/A sh_close(fd);
1N/A fd = n;
1N/A }
1N/A }
1N/A if(fd>=0)
1N/A {
1N/A fcntl(fd,F_SETFD,FD_CLOEXEC);
1N/A hp->tty = strdup(ttyname(2));
1N/A hp->auditfp = sfnew((Sfio_t*)0,NULL,-1,fd,SF_WRITE);
1N/A }
1N/A }
1N/A }
1N/A#endif
1N/A return(1);
1N/A}
1N/A
1N/A/*
1N/A * close the history file and free the space
1N/A */
1N/A
1N/Avoid hist_close(register History_t *hp)
1N/A{
1N/A sfclose(hp->histfp);
1N/A#if SHOPT_AUDIT
1N/A if(hp->auditfp)
1N/A {
1N/A if(hp->tty)
1N/A free((void*)hp->tty);
1N/A sfclose(hp->auditfp);
1N/A }
1N/A#endif /* SHOPT_AUDIT */
1N/A free((char*)hp);
1N/A hist_ptr = 0;
1N/A shgd->hist_ptr = 0;
1N/A#if SHOPT_ACCTFILE
1N/A if(acctfd)
1N/A {
1N/A close(acctfd);
1N/A acctfd = 0;
1N/A }
1N/A#endif /* SHOPT_ACCTFILE */
1N/A}
1N/A
1N/A/*
1N/A * check history file format to see if it begins with special byte
1N/A */
1N/Astatic int hist_check(register int fd)
1N/A{
1N/A unsigned char magic[2];
1N/A lseek(fd,(off_t)0,SEEK_SET);
1N/A if((read(fd,(char*)magic,2)!=2) || (magic[0]!=HIST_UNDO))
1N/A return(1);
1N/A return(0);
1N/A}
1N/A
1N/A/*
1N/A * clean out history file OK if not modified in HIST_RECENT seconds
1N/A */
1N/Astatic int hist_clean(int fd)
1N/A{
1N/A struct stat statb;
1N/A return(fstat(fd,&statb)>=0 && (time((time_t*)0)-statb.st_mtime) >= HIST_RECENT);
1N/A}
1N/A
1N/A/*
1N/A * Copy the last <n> commands to a new file and make this the history file
1N/A */
1N/A
1N/Astatic History_t* hist_trim(History_t *hp, int n)
1N/A{
1N/A register char *cp;
1N/A register int incmd=1, c=0;
1N/A register History_t *hist_new, *hist_old = hp;
1N/A char *buff, *endbuff, *tmpname=0;
1N/A off_t oldp,newp;
1N/A struct stat statb;
1N/A unlink(hist_old->histname);
1N/A if(access(hist_old->histname,F_OK) >= 0)
1N/A {
1N/A /* The unlink can fail on windows 95 */
1N/A int fd;
1N/A char *last, *name=hist_old->histname;
1N/A close(sffileno(hist_old->histfp));
1N/A tmpname = (char*)malloc(strlen(name)+14);
1N/A if(last = strrchr(name,'/'))
1N/A {
1N/A *last = 0;
1N/A pathtmp(tmpname,name,"hist",NIL(int*));
1N/A *last = '/';
1N/A }
1N/A else
1N/A pathtmp(tmpname,".","hist",NIL(int*));
1N/A if(rename(name,tmpname) < 0)
1N/A tmpname = name;
1N/A fd = open(tmpname,O_RDONLY);
1N/A sfsetfd(hist_old->histfp,fd);
1N/A if(tmpname==name)
1N/A tmpname = 0;
1N/A }
1N/A hist_ptr = 0;
1N/A if(fstat(sffileno(hist_old->histfp),&statb)>=0)
1N/A {
1N/A histinit = 1;
1N/A histmode = statb.st_mode;
1N/A }
1N/A if(!sh_histinit(hp->histshell))
1N/A {
1N/A /* use the old history file */
1N/A return hist_ptr = hist_old;
1N/A }
1N/A hist_new = hist_ptr;
1N/A hist_ptr = hist_old;
1N/A if(--n < 0)
1N/A n = 0;
1N/A newp = hist_seek(hist_old,++n);
1N/A while(1)
1N/A {
1N/A if(!incmd)
1N/A {
1N/A c = hist_ind(hist_new,++hist_new->histind);
1N/A hist_new->histcmds[c] = hist_new->histcnt;
1N/A if(hist_new->histcnt > hist_new->histmarker+HIST_BSIZE/2)
1N/A {
1N/A char locbuff[HIST_MARKSZ];
1N/A hist_marker(locbuff,hist_new->histind);
1N/A sfwrite(hist_new->histfp,locbuff,HIST_MARKSZ);
1N/A hist_new->histcnt += HIST_MARKSZ;
1N/A hist_new->histmarker = hist_new->histcmds[hist_ind(hist_new,c)] = hist_new->histcnt;
1N/A }
1N/A oldp = newp;
1N/A newp = hist_seek(hist_old,++n);
1N/A if(newp <=oldp)
1N/A break;
1N/A }
1N/A if(!(buff=(char*)sfreserve(hist_old->histfp,SF_UNBOUND,0)))
1N/A break;
1N/A *(endbuff=(cp=buff)+sfvalue(hist_old->histfp)) = 0;
1N/A /* copy to null byte */
1N/A incmd = 0;
1N/A while(*cp++);
1N/A if(cp > endbuff)
1N/A incmd = 1;
1N/A else if(*cp==0)
1N/A cp++;
1N/A if(cp > endbuff)
1N/A cp = endbuff;
1N/A c = cp-buff;
1N/A hist_new->histcnt += c;
1N/A sfwrite(hist_new->histfp,buff,c);
1N/A }
1N/A hist_cancel(hist_new);
1N/A sfclose(hist_old->histfp);
1N/A if(tmpname)
1N/A {
1N/A unlink(tmpname);
1N/A free(tmpname);
1N/A }
1N/A free((char*)hist_old);
1N/A return hist_ptr = hist_new;
1N/A}
1N/A
1N/A/*
1N/A * position history file at size and find next command number
1N/A */
1N/Astatic int hist_nearend(History_t *hp, Sfio_t *iop, register off_t size)
1N/A{
1N/A register unsigned char *cp, *endbuff;
1N/A register int n, incmd=1;
1N/A unsigned char *buff, marker[4];
1N/A if(size <= 2L || sfseek(iop,size,SEEK_SET)<0)
1N/A goto begin;
1N/A /* skip to marker command and return the number */
1N/A /* numbering commands occur after a null and begin with HIST_CMDNO */
1N/A while(cp=buff=(unsigned char*)sfreserve(iop,SF_UNBOUND,SF_LOCKR))
1N/A {
1N/A n = sfvalue(iop);
1N/A *(endbuff=cp+n) = 0;
1N/A while(1)
1N/A {
1N/A /* check for marker */
1N/A if(!incmd && *cp++==HIST_CMDNO && *cp==0)
1N/A {
1N/A n = cp+1 - buff;
1N/A incmd = -1;
1N/A break;
1N/A }
1N/A incmd = 0;
1N/A while(*cp++);
1N/A if(cp>endbuff)
1N/A {
1N/A incmd = 1;
1N/A break;
1N/A }
1N/A if(*cp==0 && ++cp>endbuff)
1N/A break;
1N/A }
1N/A size += n;
1N/A sfread(iop,(char*)buff,n);
1N/A if(incmd < 0)
1N/A {
1N/A if((n=sfread(iop,(char*)marker,4))==4)
1N/A {
1N/A n = (marker[0]<<16)|(marker[1]<<8)|marker[2];
1N/A if(n < size/2)
1N/A {
1N/A hp->histmarker = hp->histcnt = size+4;
1N/A return(n);
1N/A }
1N/A n=4;
1N/A }
1N/A if(n >0)
1N/A size += n;
1N/A incmd = 0;
1N/A }
1N/A }
1N/Abegin:
1N/A sfseek(iop,(off_t)2,SEEK_SET);
1N/A hp->histmarker = hp->histcnt = 2L;
1N/A return(1);
1N/A}
1N/A
1N/A/*
1N/A * This routine reads the history file from the present position
1N/A * to the end-of-file and puts the information in the in-core
1N/A * history table
1N/A * Note that HIST_CMDNO is only recognized at the beginning of a command
1N/A * and that HIST_UNDO as the first character of a command is skipped
1N/A * unless it is followed by 0. If followed by 0 then it cancels
1N/A * the previous command.
1N/A */
1N/A
1N/Avoid hist_eof(register History_t *hp)
1N/A{
1N/A register char *cp,*first,*endbuff;
1N/A register int incmd = 0;
1N/A register off_t count = hp->histcnt;
1N/A int oldind,n,skip=0;
1N/A off_t last = sfseek(hp->histfp,(off_t)0,SEEK_END);
1N/A if(last < count)
1N/A {
1N/A last = -1;
1N/A count = 2+HIST_MARKSZ;
1N/A oldind = hp->histind;
1N/A if((hp->histind -= hp->histsize) < 0)
1N/A hp->histind = 1;
1N/A }
1N/Aagain:
1N/A sfseek(hp->histfp,count,SEEK_SET);
1N/A while(cp=(char*)sfreserve(hp->histfp,SF_UNBOUND,0))
1N/A {
1N/A n = sfvalue(hp->histfp);
1N/A *(endbuff = cp+n) = 0;
1N/A first = cp += skip;
1N/A while(1)
1N/A {
1N/A while(!incmd)
1N/A {
1N/A if(cp>first)
1N/A {
1N/A count += (cp-first);
1N/A n = hist_ind(hp, ++hp->histind);
1N/A#ifdef future
1N/A if(count==hp->histcmds[n])
1N/A {
1N/A sfprintf(sfstderr,"count match n=%d\n",n);
1N/A if(histinit)
1N/A {
1N/A histinit = 0;
1N/A return;
1N/A }
1N/A }
1N/A else if(n>=histinit)
1N/A#endif
1N/A hp->histcmds[n] = count;
1N/A first = cp;
1N/A }
1N/A switch(*((unsigned char*)(cp++)))
1N/A {
1N/A case HIST_CMDNO:
1N/A if(*cp==0)
1N/A {
1N/A hp->histmarker=count+2;
1N/A cp += (HIST_MARKSZ-1);
1N/A hp->histind--;
1N/A if(cp <= endbuff)
1N/A {
1N/A unsigned char *marker = (unsigned char*)(cp-4);
1N/A hp->histind = ((marker[0]<<16)|(marker[1]<<8)|marker[2]);
1N/A }
1N/A }
1N/A break;
1N/A case HIST_UNDO:
1N/A if(*cp==0)
1N/A {
1N/A cp+=1;
1N/A hp->histind-=2;
1N/A }
1N/A break;
1N/A default:
1N/A cp--;
1N/A incmd = 1;
1N/A }
1N/A if(cp > endbuff)
1N/A {
1N/A cp++;
1N/A goto refill;
1N/A }
1N/A }
1N/A first = cp;
1N/A while(*cp++);
1N/A if(cp > endbuff)
1N/A break;
1N/A incmd = 0;
1N/A while(*cp==0)
1N/A {
1N/A if(++cp > endbuff)
1N/A goto refill;
1N/A }
1N/A }
1N/A refill:
1N/A count += (--cp-first);
1N/A skip = (cp-endbuff);
1N/A if(!incmd && !skip)
1N/A hp->histcmds[hist_ind(hp,++hp->histind)] = count;
1N/A }
1N/A hp->histcnt = count;
1N/A if(incmd && last)
1N/A {
1N/A sfputc(hp->histfp,0);
1N/A hist_cancel(hp);
1N/A count = 2;
1N/A skip = 0;
1N/A oldind -= hp->histind;
1N/A hp->histind = hp->histind-hp->histsize + oldind +2;
1N/A if(hp->histind<0)
1N/A hp->histind = 1;
1N/A if(last<0)
1N/A {
1N/A char buff[HIST_MARKSZ];
1N/A int fd = open(hp->histname,O_RDWR);
1N/A if(fd>=0)
1N/A {
1N/A hist_marker(buff,hp->histind);
1N/A write(fd,(char*)hist_stamp,2);
1N/A write(fd,buff,HIST_MARKSZ);
1N/A close(fd);
1N/A }
1N/A }
1N/A last = 0;
1N/A goto again;
1N/A }
1N/A}
1N/A
1N/A/*
1N/A * This routine will cause the previous command to be cancelled
1N/A */
1N/A
1N/Avoid hist_cancel(register History_t *hp)
1N/A{
1N/A register int c;
1N/A if(!hp)
1N/A return;
1N/A sfputc(hp->histfp,HIST_UNDO);
1N/A sfputc(hp->histfp,0);
1N/A sfsync(hp->histfp);
1N/A hp->histcnt += 2;
1N/A c = hist_ind(hp,--hp->histind);
1N/A hp->histcmds[c] = hp->histcnt;
1N/A}
1N/A
1N/A/*
1N/A * flush the current history command
1N/A */
1N/A
1N/Avoid hist_flush(register History_t *hp)
1N/A{
1N/A register char *buff;
1N/A if(hp)
1N/A {
1N/A if(buff=(char*)sfreserve(hp->histfp,0,SF_LOCKR))
1N/A {
1N/A hp->histflush = sfvalue(hp->histfp)+1;
1N/A sfwrite(hp->histfp,buff,0);
1N/A }
1N/A else
1N/A hp->histflush=0;
1N/A if(sfsync(hp->histfp)<0)
1N/A {
1N/A hist_close(hp);
1N/A if(!sh_histinit(hp->histshell))
1N/A sh_offoption(SH_HISTORY);
1N/A }
1N/A hp->histflush = 0;
1N/A }
1N/A}
1N/A
1N/A/*
1N/A * This is the write discipline for the history file
1N/A * When called from hist_flush(), trailing newlines are deleted and
1N/A * a zero byte. Line sequencing is added as required
1N/A */
1N/A
1N/A#ifdef SF_BUFCONST
1N/Astatic ssize_t hist_write(Sfio_t *iop,const void *buff,register size_t insize,Sfdisc_t* handle)
1N/A#else
1N/Astatic int hist_write(Sfio_t *iop,const void *buff,register int insize,Sfdisc_t* handle)
1N/A#endif
1N/A{
1N/A register History_t *hp = (History_t*)handle;
1N/A register char *bufptr = ((char*)buff)+insize;
1N/A register int c,size = insize;
1N/A register off_t cur;
1N/A int saved=0;
1N/A char saveptr[HIST_MARKSZ];
1N/A if(!hp->histflush)
1N/A return(write(sffileno(iop),(char*)buff,size));
1N/A if((cur = lseek(sffileno(iop),(off_t)0,SEEK_END)) <0)
1N/A {
1N/A errormsg(SH_DICT,2,"hist_flush: EOF seek failed errno=%d",errno);
1N/A return(-1);
1N/A }
1N/A hp->histcnt = cur;
1N/A /* remove whitespace from end of commands */
1N/A while(--bufptr >= (char*)buff)
1N/A {
1N/A c= *bufptr;
1N/A if(!isspace(c))
1N/A {
1N/A if(c=='\\' && *(bufptr+1)!='\n')
1N/A bufptr++;
1N/A break;
1N/A }
1N/A }
1N/A /* don't count empty lines */
1N/A if(++bufptr <= (char*)buff)
1N/A return(insize);
1N/A *bufptr++ = '\n';
1N/A *bufptr++ = 0;
1N/A size = bufptr - (char*)buff;
1N/A#if SHOPT_AUDIT
1N/A if(hp->auditfp)
1N/A {
1N/A time_t t=time((time_t*)0);
1N/A sfprintf(hp->auditfp,"%u;%u;%s;%*s%c",sh_isoption(SH_PRIVILEGED)?shgd->euserid:shgd->userid,t,hp->tty,size,buff,0);
1N/A sfsync(hp->auditfp);
1N/A }
1N/A#endif /* SHOPT_AUDIT */
1N/A#if SHOPT_ACCTFILE
1N/A if(acctfd)
1N/A {
1N/A int timechars, offset;
1N/A offset = staktell();
1N/A stakputs(buff);
1N/A stakseek(staktell() - 1);
1N/A timechars = sfprintf(staksp, "\t%s\t%x\n",logname,time(NIL(long *)));
1N/A lseek(acctfd, (off_t)0, SEEK_END);
1N/A write(acctfd, stakptr(offset), size - 2 + timechars);
1N/A stakseek(offset);
1N/A
1N/A }
1N/A#endif /* SHOPT_ACCTFILE */
1N/A if(size&01)
1N/A {
1N/A size++;
1N/A *bufptr++ = 0;
1N/A }
1N/A hp->histcnt += size;
1N/A c = hist_ind(hp,++hp->histind);
1N/A hp->histcmds[c] = hp->histcnt;
1N/A if(hp->histflush>HIST_MARKSZ && hp->histcnt > hp->histmarker+HIST_BSIZE/2)
1N/A {
1N/A memcpy((void*)saveptr,(void*)bufptr,HIST_MARKSZ);
1N/A saved=1;
1N/A hp->histcnt += HIST_MARKSZ;
1N/A hist_marker(bufptr,hp->histind);
1N/A hp->histmarker = hp->histcmds[hist_ind(hp,c)] = hp->histcnt;
1N/A size += HIST_MARKSZ;
1N/A }
1N/A errno = 0;
1N/A size = write(sffileno(iop),(char*)buff,size);
1N/A if(saved)
1N/A memcpy((void*)bufptr,(void*)saveptr,HIST_MARKSZ);
1N/A if(size>=0)
1N/A {
1N/A hp->histwfail = 0;
1N/A return(insize);
1N/A }
1N/A return(-1);
1N/A}
1N/A
1N/A/*
1N/A * Put history sequence number <n> into buffer <buff>
1N/A * The buffer must be large enough to hold HIST_MARKSZ chars
1N/A */
1N/A
1N/Astatic void hist_marker(register char *buff,register long cmdno)
1N/A{
1N/A *buff++ = HIST_CMDNO;
1N/A *buff++ = 0;
1N/A *buff++ = (cmdno>>16);
1N/A *buff++ = (cmdno>>8);
1N/A *buff++ = cmdno;
1N/A *buff++ = 0;
1N/A}
1N/A
1N/A/*
1N/A * return byte offset in history file for command <n>
1N/A */
1N/Aoff_t hist_tell(register History_t *hp, int n)
1N/A{
1N/A return(hp->histcmds[hist_ind(hp,n)]);
1N/A}
1N/A
1N/A/*
1N/A * seek to the position of command <n>
1N/A */
1N/Aoff_t hist_seek(register History_t *hp, int n)
1N/A{
1N/A return(sfseek(hp->histfp,hp->histcmds[hist_ind(hp,n)],SEEK_SET));
1N/A}
1N/A
1N/A/*
1N/A * write the command starting at offset <offset> onto file <outfile>.
1N/A * if character <last> appears before newline it is deleted
1N/A * each new-line character is replaced with string <nl>.
1N/A */
1N/A
1N/Avoid hist_list(register History_t *hp,Sfio_t *outfile, off_t offset,int last, char *nl)
1N/A{
1N/A register int oldc=0;
1N/A register int c;
1N/A if(offset<0 || !hp)
1N/A {
1N/A sfputr(outfile,sh_translate(e_unknown),'\n');
1N/A return;
1N/A }
1N/A sfseek(hp->histfp,offset,SEEK_SET);
1N/A while((c = sfgetc(hp->histfp)) != EOF)
1N/A {
1N/A if(c && oldc=='\n')
1N/A sfputr(outfile,nl,-1);
1N/A else if(last && (c==0 || (c=='\n' && oldc==last)))
1N/A return;
1N/A else if(oldc)
1N/A sfputc(outfile,oldc);
1N/A oldc = c;
1N/A if(c==0)
1N/A return;
1N/A }
1N/A return;
1N/A}
1N/A
1N/A/*
1N/A * find index for last line with given string
1N/A * If flag==0 then line must begin with string
1N/A * direction < 1 for backwards search
1N/A*/
1N/A
1N/AHistloc_t hist_find(register History_t*hp,char *string,register int index1,int flag,int direction)
1N/A{
1N/A register int index2;
1N/A off_t offset;
1N/A int *coffset=0;
1N/A Histloc_t location;
1N/A location.hist_command = -1;
1N/A location.hist_char = 0;
1N/A location.hist_line = 0;
1N/A if(!hp)
1N/A return(location);
1N/A /* leading ^ means beginning of line unless escaped */
1N/A if(flag)
1N/A {
1N/A index2 = *string;
1N/A if(index2=='\\')
1N/A string++;
1N/A else if(index2=='^')
1N/A {
1N/A flag=0;
1N/A string++;
1N/A }
1N/A }
1N/A if(flag)
1N/A coffset = &location.hist_char;
1N/A index2 = (int)hp->histind;
1N/A if(direction<0)
1N/A {
1N/A index2 -= hp->histsize;
1N/A if(index2<1)
1N/A index2 = 1;
1N/A if(index1 <= index2)
1N/A return(location);
1N/A }
1N/A else if(index1 >= index2)
1N/A return(location);
1N/A while(index1!=index2)
1N/A {
1N/A direction>0?++index1:--index1;
1N/A offset = hist_tell(hp,index1);
1N/A if((location.hist_line=hist_match(hp,offset,string,coffset))>=0)
1N/A {
1N/A location.hist_command = index1;
1N/A return(location);
1N/A }
1N/A#if KSHELL
1N/A /* allow a search to be aborted */
1N/A if(((Shell_t*)hp->histshell)->trapnote&SH_SIGSET)
1N/A break;
1N/A#endif /* KSHELL */
1N/A }
1N/A return(location);
1N/A}
1N/A
1N/A/*
1N/A * search for <string> in history file starting at location <offset>
1N/A * If coffset==0 then line must begin with string
1N/A * returns the line number of the match if successful, otherwise -1
1N/A */
1N/A
1N/Aint hist_match(register History_t *hp,off_t offset,char *string,int *coffset)
1N/A{
1N/A register unsigned char *first, *cp;
1N/A register int m,n,c=1,line=0;
1N/A#if SHOPT_MULTIBYTE
1N/A mbinit();
1N/A#endif /* SHOPT_MULTIBYTE */
1N/A sfseek(hp->histfp,offset,SEEK_SET);
1N/A if(!(cp = first = (unsigned char*)sfgetr(hp->histfp,0,0)))
1N/A return(-1);
1N/A m = sfvalue(hp->histfp);
1N/A n = strlen(string);
1N/A while(m > n)
1N/A {
1N/A if(*cp==*string && memcmp(cp,string,n)==0)
1N/A {
1N/A if(coffset)
1N/A *coffset = (cp-first);
1N/A return(line);
1N/A }
1N/A if(!coffset)
1N/A break;
1N/A if(*cp=='\n')
1N/A line++;
1N/A#if SHOPT_MULTIBYTE
1N/A if((c=mbsize(cp)) < 0)
1N/A c = 1;
1N/A#endif /* SHOPT_MULTIBYTE */
1N/A cp += c;
1N/A m -= c;
1N/A }
1N/A return(-1);
1N/A}
1N/A
1N/A
1N/A#if SHOPT_ESH || SHOPT_VSH
1N/A/*
1N/A * copy command <command> from history file to s1
1N/A * at most <size> characters copied
1N/A * if s1==0 the number of lines for the command is returned
1N/A * line=linenumber for emacs copy and only this line of command will be copied
1N/A * line < 0 for full command copy
1N/A * -1 returned if there is no history file
1N/A */
1N/A
1N/Aint hist_copy(char *s1,int size,int command,int line)
1N/A{
1N/A register int c;
1N/A register History_t *hp = shgd->hist_ptr;
1N/A register int count = 0;
1N/A register char *s1max = s1+size;
1N/A if(!hp)
1N/A return(-1);
1N/A hist_seek(hp,command);
1N/A while ((c = sfgetc(hp->histfp)) && c!=EOF)
1N/A {
1N/A if(c=='\n')
1N/A {
1N/A if(count++ ==line)
1N/A break;
1N/A else if(line >= 0)
1N/A continue;
1N/A }
1N/A if(s1 && (line<0 || line==count))
1N/A {
1N/A if(s1 >= s1max)
1N/A {
1N/A *--s1 = 0;
1N/A break;
1N/A }
1N/A *s1++ = c;
1N/A }
1N/A
1N/A }
1N/A sfseek(hp->histfp,(off_t)0,SEEK_END);
1N/A if(s1==0)
1N/A return(count);
1N/A if(count && (c= *(s1-1)) == '\n')
1N/A s1--;
1N/A *s1 = '\0';
1N/A return(count);
1N/A}
1N/A
1N/A/*
1N/A * return word number <word> from command number <command>
1N/A */
1N/A
1N/Achar *hist_word(char *string,int size,int word)
1N/A{
1N/A register int c;
1N/A register char *s1 = string;
1N/A register unsigned char *cp = (unsigned char*)s1;
1N/A register int flag = 0;
1N/A History_t *hp = hist_ptr;
1N/A if(!hp)
1N/A return(NIL(char*));
1N/A hist_copy(string,size,(int)hp->histind-1,-1);
1N/A for(;c = *cp;cp++)
1N/A {
1N/A c = isspace(c);
1N/A if(c && flag)
1N/A {
1N/A *cp = 0;
1N/A if(--word==0)
1N/A break;
1N/A flag = 0;
1N/A }
1N/A else if(c==0 && flag==0)
1N/A {
1N/A s1 = (char*)cp;
1N/A flag++;
1N/A }
1N/A }
1N/A *cp = 0;
1N/A if(s1 != string)
1N/A strcpy(string,s1);
1N/A return(string);
1N/A}
1N/A
1N/A#endif /* SHOPT_ESH */
1N/A
1N/A#if SHOPT_ESH
1N/A/*
1N/A * given the current command and line number,
1N/A * and number of lines back or foward,
1N/A * compute the new command and line number.
1N/A */
1N/A
1N/AHistloc_t hist_locate(History_t *hp,register int command,register int line,int lines)
1N/A{
1N/A Histloc_t next;
1N/A line += lines;
1N/A if(!hp)
1N/A {
1N/A command = -1;
1N/A goto done;
1N/A }
1N/A if(lines > 0)
1N/A {
1N/A register int count;
1N/A while(command <= hp->histind)
1N/A {
1N/A count = hist_copy(NIL(char*),0, command,-1);
1N/A if(count > line)
1N/A goto done;
1N/A line -= count;
1N/A command++;
1N/A }
1N/A }
1N/A else
1N/A {
1N/A register int least = (int)hp->histind-hp->histsize;
1N/A while(1)
1N/A {
1N/A if(line >=0)
1N/A goto done;
1N/A if(--command < least)
1N/A break;
1N/A line += hist_copy(NIL(char*),0, command,-1);
1N/A }
1N/A command = -1;
1N/A }
1N/Adone:
1N/A next.hist_line = line;
1N/A next.hist_command = command;
1N/A return(next);
1N/A}
1N/A#endif /* SHOPT_ESH */
1N/A
1N/A
1N/A/*
1N/A * Handle history file exceptions
1N/A */
1N/A#ifdef SF_BUFCONST
1N/Astatic int hist_exceptf(Sfio_t* fp, int type, void *data, Sfdisc_t *handle)
1N/A#else
1N/Astatic int hist_exceptf(Sfio_t* fp, int type, Sfdisc_t *handle)
1N/A#endif
1N/A{
1N/A register int newfd,oldfd;
1N/A History_t *hp = (History_t*)handle;
1N/A if(type==SF_WRITE)
1N/A {
1N/A if(errno==ENOSPC || hp->histwfail++ >= 10)
1N/A return(0);
1N/A /* write failure could be NFS problem, try to re-open */
1N/A close(oldfd=sffileno(fp));
1N/A if((newfd=open(hp->histname,O_BINARY|O_APPEND|O_CREAT|O_RDWR,S_IRUSR|S_IWUSR)) >= 0)
1N/A {
1N/A if(fcntl(newfd, F_DUPFD, oldfd) !=oldfd)
1N/A return(-1);
1N/A fcntl(oldfd,F_SETFD,FD_CLOEXEC);
1N/A close(newfd);
1N/A if(lseek(oldfd,(off_t)0,SEEK_END) < hp->histcnt)
1N/A {
1N/A register int index = hp->histind;
1N/A lseek(oldfd,(off_t)2,SEEK_SET);
1N/A hp->histcnt = 2;
1N/A hp->histind = 1;
1N/A hp->histcmds[1] = 2;
1N/A hist_eof(hp);
1N/A hp->histmarker = hp->histcnt;
1N/A hp->histind = index;
1N/A }
1N/A return(1);
1N/A }
1N/A errormsg(SH_DICT,2,"History file write error-%d %s: file unrecoverable",errno,hp->histname);
1N/A return(-1);
1N/A }
1N/A return(0);
1N/A}