update and reformat some comments in the history code

This commit is contained in:
tg
2011-11-26 18:19:00 +00:00
parent b3f4c69f05
commit 44a27fa8e0

View File

@ -26,7 +26,7 @@
#include <sys/file.h> #include <sys/file.h>
#endif #endif
__RCSID("$MirOS: src/bin/mksh/histrap.c,v 1.111 2011/09/07 15:24:16 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/histrap.c,v 1.112 2011/11/26 18:19:00 tg Exp $");
/*- /*-
* MirOS: This is the default mapping type, and need not be specified. * MirOS: This is the default mapping type, and need not be specified.
@ -527,8 +527,7 @@ findhist(int start, int fwd, const char *str, int anchored)
} }
/* /*
* set history * set history; this means reallocating the dataspace
* this means reallocating the dataspace
*/ */
void void
sethistsize(int n) sethistsize(int n)
@ -551,9 +550,8 @@ sethistsize(int n)
#if HAVE_PERSISTENT_HISTORY #if HAVE_PERSISTENT_HISTORY
/* /*
* set history file * set history file; this can mean reloading/resetting/starting
* This can mean reloading/resetting/starting history file * history file maintenance
* maintenance
*/ */
void void
sethistfile(const char *name) sethistfile(const char *name)
@ -567,7 +565,7 @@ sethistfile(const char *name)
return; return;
/* /*
* its a new name - possibly * it's a new name - possibly
*/ */
if (histfd) { if (histfd) {
/* yes the file is open */ /* yes the file is open */
@ -600,10 +598,6 @@ init_histvec(void)
/* /*
* Routines added by Peter Collinson BSDI(Europe)/Hillside Systems to
* a) permit HISTSIZE to control number of lines of history stored
* b) maintain a physical history file
*
* It turns out that there is a lot of ghastly hackery here * It turns out that there is a lot of ghastly hackery here
*/ */
@ -670,25 +664,23 @@ histsave(int *lnp, const char *cmd, bool dowrite MKSH_A_UNUSED, bool ignoredups)
} }
/* /*
* Write history data to a file nominated by HISTFILE * Write history data to a file nominated by HISTFILE;
* if HISTFILE is unset then history still happens, but * if HISTFILE is unset then history still happens, but
* the data is not written to a file * the data is not written to a file. All copies of ksh
* All copies of ksh looking at the file will maintain the * looking at the file will maintain the same history.
* same history. This is ksh behaviour. * This is ksh behaviour.
* *
* This stuff uses mmap() * This stuff uses mmap()
* if your system ain't got it - then you'll have to undef HISTORYFILE
*/ */
/*- /*-
* Open a history file * Open a history file
* Format is: * Format is:
* Bytes 1, 2: * Bytes 1, 2:
* HMAGIC - just to check that we are dealing with * HMAGIC - just to check that we are dealing with the correct object
* the correct object
* Then follows a number of stored commands * Then follows a number of stored commands
* Each command is * Each command is
* <command byte><command number(4 bytes)><bytes><null> * <command byte><command number(4 bytes)><bytes><NUL>
*/ */
#define HMAGIC1 0xab #define HMAGIC1 0xab
#define HMAGIC2 0xcd #define HMAGIC2 0xcd
@ -787,10 +779,12 @@ hist_init(Source *s)
#if HAVE_PERSISTENT_HISTORY #if HAVE_PERSISTENT_HISTORY
typedef enum state { typedef enum state {
shdr, /* expecting a header */ /* expecting a header */
sline, /* looking for a null byte to end the line */ shdr,
sn1, /* bytes 1 to 4 of a line no */ /* looking for a NUL byte to end the line */
sn2, sn3, sn4 sline,
/* bytes 1 to 4 of a line number */
sn1, sn2, sn3, sn4
} State; } State;
static int static int
@ -825,7 +819,7 @@ hist_count_lines(unsigned char *base, int bytes)
} }
/* /*
* Shrink the history file to histsize lines * Shrink the history file to HISTSIZE lines
*/ */
static int static int
hist_shrink(unsigned char *oldbase, int oldbytes) hist_shrink(unsigned char *oldbase, int oldbytes)
@ -874,7 +868,7 @@ hist_shrink(unsigned char *oldbase, int oldbytes)
} }
/* /*
* find a pointer to the data 'no' back from the end of the file * find a pointer to the data 'no' back from the end of the file;
* return the pointer and the number of bytes left * return the pointer and the number of bytes left
*/ */
static unsigned char * static unsigned char *
@ -887,7 +881,7 @@ hist_skip_back(unsigned char *base, int *bytes, int no)
/* /*
* this doesn't really work: the 4 byte line number that * this doesn't really work: the 4 byte line number that
* is encoded after the COMMAND byte can itself contain * is encoded after the COMMAND byte can itself contain
* the COMMAND byte.... * the COMMAND byte...
*/ */
for (; ep > base && *ep != COMMAND; ep--) for (; ep > base && *ep != COMMAND; ep--)
; ;
@ -918,27 +912,27 @@ histload(Source *s, unsigned char *base, int bytes)
state = sn1; state = sn1;
break; break;
case sn1: case sn1:
lno = (((*base)&0xff)<<24); lno = (((*base) & 0xFF) << 24);
state = sn2; state = sn2;
break; break;
case sn2: case sn2:
lno |= (((*base)&0xff)<<16); lno |= (((*base) & 0xFF) << 16);
state = sn3; state = sn3;
break; break;
case sn3: case sn3:
lno |= (((*base)&0xff)<<8); lno |= (((*base) & 0xFF) << 8);
state = sn4; state = sn4;
break; break;
case sn4: case sn4:
lno |= (*base)&0xff; lno |= (*base) & 0xFF;
line = base+1; line = base + 1;
state = sline; state = sline;
break; break;
case sline: case sline:
if (*base == '\0') { if (*base == '\0') {
/* worry about line numbers */ /* worry about line numbers */
if (histptr >= history && lno-1 != s->line) { if (histptr >= history && lno-1 != s->line) {
/* a replacement ? */ /* a replacement? */
histinsert(s, lno, (char *)line); histinsert(s, lno, (char *)line);
} else { } else {
s->line = lno--; s->line = lno--;
@ -969,10 +963,11 @@ histinsert(Source *s, int lno, const char *line)
/* /*
* write a command to the end of the history file * write a command to the end of the history file
*
* This *MAY* seem easy but it's also necessary to check * This *MAY* seem easy but it's also necessary to check
* that the history file has not changed in size. * that the history file has not changed in size.
* If it has - then some other shell has written to it * If it has - then some other shell has written to it and
* and we should read those commands to update our history * we should (re)read those commands to update our history
*/ */
static void static void
writehistfile(int lno, char *cmd) writehistfile(int lno, char *cmd)
@ -1017,10 +1012,10 @@ writehistfile(int lno, char *cmd)
* we can write our bit now * we can write our bit now
*/ */
hdr[0] = COMMAND; hdr[0] = COMMAND;
hdr[1] = (lno>>24)&0xff; hdr[1] = (lno >> 24) & 0xFF;
hdr[2] = (lno>>16)&0xff; hdr[2] = (lno >> 16) & 0xFF;
hdr[3] = (lno>>8)&0xff; hdr[3] = (lno >> 8) & 0xFF;
hdr[4] = lno&0xff; hdr[4] = lno & 0xFF;
bytes = strlen(cmd) + 1; bytes = strlen(cmd) + 1;
if ((write(histfd, hdr, 5) != 5) || if ((write(histfd, hdr, 5) != 5) ||
(write(histfd, cmd, bytes) != bytes)) (write(histfd, cmd, bytes) != bytes))