Skip to content

Commit

Permalink
Fix history merging
Browse files Browse the repository at this point in the history
  • Loading branch information
suominen committed Apr 4, 2024
1 parent 8ca47db commit 991f66c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 27 deletions.
31 changes: 4 additions & 27 deletions sh.hist.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ static void hfree (struct Hist *);

/* #define DEBUG_HIST 1 */

static const int fastMergeErase = 1;
static unsigned histCount = 0; /* number elements on history list */
static int histlen = 0;
static struct Hist *histTail = NULL; /* last element on history list */
Expand Down Expand Up @@ -626,15 +625,6 @@ heq(const struct wordent *a0, const struct wordent *b0)
}
}

/* Renumber entries following p, which we will be deleting. */
PG_STATIC void
renumberHist(struct Hist *p)
{
int n = p->Href;
while ((p = p->Hnext))
p->Href = n--;
}

/* The hash table is implemented as an array of pointers to Hist entries. Each
* entry is located in the table using hash2tableIndex() and checking the
* following entries in case of a collision (linear rehash). Free entries in
Expand Down Expand Up @@ -964,8 +954,6 @@ enthist(
* to insert the new entry, then remember the place. */
if (mflg && Htime != 0 && p->Hprev->Htime >= Htime)
pTime = p->Hprev;
if (!fastMergeErase)
renumberHist(p); /* Reset Href of subsequent entries */
hremove(p);
hfree(p);
p = NULL; /* so new entry is allocated below */
Expand Down Expand Up @@ -1031,11 +1019,10 @@ enthist(
}
}
/* pp is now the last entry with time >= to np. */
if (!fastMergeErase) { /* renumber at end of loadhist */
/* Before inserting np after pp, bubble its Hnum & Href values down
* through the earlier part of list. */
bubbleHnumHrefDown(np, pp);
}
/* renumber at end of loadhist */
/* Before inserting np after pp, bubble its Hnum & Href values down
* through the earlier part of list. */
bubbleHnumHrefDown(np, pp);
}
else
pp = &Histlist; /* insert at beginning of history */
Expand Down Expand Up @@ -1389,16 +1376,6 @@ loadhist(Char *fname, int mflg)
loadhist_cmd[2] = STRtildothist;

dosource(loadhist_cmd, NULL);

/* During history merging (enthist sees mflg set), we disable management of
* Hnum and Href (because fastMergeErase is true). So now reset all the
* values based on the final ordering of the history list. */
if (mflg) {
int n = eventno;
struct Hist *hp = &Histlist;
while ((hp = hp->Hnext))
hp->Hnum = hp->Href = n--;
}
}

void
Expand Down
46 changes: 46 additions & 0 deletions tests/history.at
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,52 @@ dnl savehist) set to 0 instead of 1.

AT_CLEANUP()

AT_SETUP([History merge])
dnl Check if history merge really works out

AT_DATA([hist-merge.csh],
[[set histfile=test.history histdup=prev history=(6 "%h TIME %R\n")
set savehist=(6 merge)
printf "'%s' %s\n" "$histdup" "$history"
history -c
: 1
: 2
: 3
: 4
: 5
history -S
: a
: b
: c
: d
: e
history -S
history -L
history 6
]])

AT_CHECK([tcsh -f -q -i < hist-merge.csh], ,
[> 'prev' 6 %h TIME %R\n
24 TIME : b
25 TIME : c
26 TIME : d
27 TIME : e
28 TIME history -S
29 TIME history 6
> exit
],)

dnl In broken case we see the former history instead
dnl > 'prev' 6 %h TIME %R\n
dnl 24 TIME : 4
dnl 25 TIME : 3
dnl 26 TIME : 2
dnl 27 TIME : 1
dnl 28 TIME history -S
dnl 29 TIME history 6
dnl > exit

AT_CLEANUP()

dnl
dnl History faults
Expand Down

0 comments on commit 991f66c

Please sign in to comment.