From fffa48ae5c3e7c03b6e6118b60863adfd4fccbde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Diego=20Aur=C3=A9lio=20Mesquita?= Date: Tue, 5 Sep 2017 23:49:50 -0300 Subject: [PATCH 3/5] Enable history persistence for executed commands MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marco Diego Aurélio Mesquita --- src/files.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/nano.c | 6 +++++ src/nano.h | 1 + src/proto.h | 2 ++ src/rcfile.c | 3 +++ 5 files changed, 91 insertions(+) diff --git a/src/files.c b/src/files.c index 6b9f42d9..e8134629 100644 --- a/src/files.c +++ b/src/files.c @@ -2742,6 +2742,11 @@ char *histfilename(void) return construct_filename("/.nano/search_history"); } +char *executehistfilename(void) +{ + return construct_filename("/.nano/execute_history"); +} + /* Construct the legacy history filename. */ /* (To be removed in 2018.) */ char *legacyhistfilename(void) @@ -2796,6 +2801,46 @@ int check_dotnano(void) return ret; } +/* Load the execute command history from ~/.nano/execute_history. */ +void load_executehistory(void) +{ + char *searchhist = executehistfilename(); + FILE *hist; + + /* If no home directory was found, we can't do anything. */ + if (searchhist == NULL) + return; + + hist = fopen(searchhist, "rb"); + + if (hist == NULL) { + if (errno != ENOENT) { + UNSET(EXECUTELOG); + /* When reading failed, don't save history when we quit. */ + history_error(N_("Error reading %s: %s"), searchhist, + strerror(errno)); + } + } else { + filestruct **history = &execute_history; + char *line = NULL; + size_t buf_len = 0; + ssize_t read; + + while ((read = getline(&line, &buf_len, hist)) > 1) { + line[--read] = '\0'; + /* Encode any embedded NUL as 0x0A. */ + unsunder(line, read); + update_history(history, line); + } + + fclose(hist); + free(line); + } + + free(searchhist); +} + + /* Load the search and replace histories from ~/.nano/search_history. */ void load_history(void) { @@ -2880,6 +2925,40 @@ bool writehist(FILE *hist, const filestruct *head) return TRUE; } +/* Save the execute command history to ~/.nano/execute_history. */ +void save_executehistory(void) +{ + char *searchhist; + FILE *hist; + + /* If the histories are unchanged or empty, don't bother saving them. */ + if (!history_has_changed() || executebot->lineno == 1) + return; + + searchhist = executehistfilename(); + + if (searchhist == NULL) + return; + + hist = fopen(searchhist, "wb"); + + if (hist == NULL) + fprintf(stderr, _("Error writing %s: %s\n"), searchhist, + strerror(errno)); + else { + /* Don't allow others to read or write the history file. */ + chmod(searchhist, S_IRUSR | S_IWUSR); + + if (!writehist(hist, executeage)) + fprintf(stderr, _("Error writing %s: %s\n"), searchhist, + strerror(errno)); + + fclose(hist); + } + + free(searchhist); +} + /* Save the search and replace histories to ~/.nano/search_history. */ void save_history(void) { diff --git a/src/nano.c b/src/nano.c index 9012dee3..5bf6f78f 100644 --- a/src/nano.c +++ b/src/nano.c @@ -564,6 +564,8 @@ void finish(void) #ifndef DISABLE_HISTORIES if (ISSET(HISTORYLOG)) save_history(); + if (ISSET(EXECUTELOG)) + save_executehistory(); if (ISSET(POS_HISTORY)) { update_poshistory(openfile->filename, openfile->current->lineno, xplustabs() + 1); save_poshistory(); @@ -2252,6 +2254,7 @@ int main(int argc, char **argv) #ifdef ENABLE_NANORC no_rcfiles = TRUE; UNSET(HISTORYLOG); + UNSET(EXECUTELOG); UNSET(POS_HISTORY); #endif } @@ -2374,11 +2377,14 @@ int main(int argc, char **argv) get_homedir(); if (homedir == NULL || check_dotnano() == 0) { UNSET(HISTORYLOG); + UNSET(EXECUTELOG); UNSET(POS_HISTORY); } } if (ISSET(HISTORYLOG)) load_history(); + if (ISSET(EXECUTELOG)) + load_executehistory(); if (ISSET(POS_HISTORY)) load_poshistory(); #endif /* !DISABLE_HISTORIES */ diff --git a/src/nano.h b/src/nano.h index 1a77097a..ee48c4fb 100644 --- a/src/nano.h +++ b/src/nano.h @@ -501,6 +501,7 @@ enum NO_COLOR_SYNTAX, PRESERVE, HISTORYLOG, + EXECUTELOG, RESTRICTED, SMART_HOME, WHITESPACE_DISPLAY, diff --git a/src/proto.h b/src/proto.h index bdffa312..d74561e7 100644 --- a/src/proto.h +++ b/src/proto.h @@ -323,6 +323,8 @@ const char *tail(const char *path); #ifndef DISABLE_HISTORIES void load_history(void); void save_history(void); +void load_executehistory(void); +void save_executehistory(void); int check_dotnano(void); void load_poshistory(void); void save_poshistory(void); diff --git a/src/rcfile.c b/src/rcfile.c index 3750bf3e..fd715031 100644 --- a/src/rcfile.c +++ b/src/rcfile.c @@ -44,6 +44,9 @@ static const rcoption rcopts[] = { #endif {"const", CONSTANT_SHOW}, /* deprecated form, remove in 2018 */ {"constantshow", CONSTANT_SHOW}, +#ifndef DISABLE_HISTORIES + {"executelog", EXECUTELOG}, +#endif #ifndef DISABLE_WRAPJUSTIFY {"fill", 0}, #endif -- 2.11.0