diff --git a/src/msh.c b/src/msh.c index 0970b3b..2138962 100644 --- a/src/msh.c +++ b/src/msh.c @@ -347,13 +347,22 @@ int main(int argc, char** argv) { } } else if (ansi == ANSI_CURSOR_UP || ansi == ANSI_CURSOR_DOWN) { - int changed = 1; + int load_from_hist = 1; if (ansi == ANSI_CURSOR_UP && historyoffset < historyi) { historyoffset++; + + if (historyoffset == 1) { + if (oldline != NULL) { + free(oldline); + } + + oldline = line; + } } else if (ansi == ANSI_CURSOR_DOWN && historyoffset > 0) { historyoffset--; + // We just went back to the last line, load from oldline if (historyoffset == 0) { free(line); line = oldline; @@ -364,20 +373,15 @@ int main(int argc, char** argv) { oldline = NULL; // Don't look up from history - changed = 0; + load_from_hist = 0; } } else { - // We didn't actually change anything - changed = 0; + // We didn't change our line (already at top or bottom) + load_from_hist = 0; } - if (changed == 1) { - if (oldline != NULL) { - free(oldline); - } - - oldline = line; + if (load_from_hist == 1) { char* historyline = history[historyi - (historyoffset)]; size_t historylinelen = strlen(historyline); line = malloc(historylinelen + 1);