- for (hp = Histlist.Hnext; hp; hp = hp->Hnext)
- if (matchev(hp, cp, anyarg))
- return (hp);
+ for (hp = Histlist.Hnext; hp; hp = hp->Hnext) {
+ char *dp;
+ register char *p, *q;
+ register struct wordent *lp = hp->Hlex.next;
+ int argno = 0;
+
+ /*
+ * The entries added by alias substitution don't
+ * have a newline but do have a negative event number.
+ * Savehist() trims off these entries, but it happens
+ * before alias expansion, too early to delete those
+ * from the previous command.
+ */
+ if (hp->Hnum < 0)
+ continue;
+ if (lp->word[0] == '\n')
+ continue;
+ if (!anyarg) {
+ p = cp;
+ q = lp->word;
+ do
+ if (!*p)
+ return (hp);
+ while (*p++ == *q++);
+ continue;
+ }
+ do {
+ for (dp = lp->word; *dp; dp++) {
+ p = cp;
+ q = dp;
+ do
+ if (!*p) {
+ quesarg = argno;
+ return (hp);
+ }
+ while (*p++ == *q++);
+ }
+ lp = lp->next;
+ argno++;
+ } while (lp->word[0] != '\n');
+ }