diff options
author | nicm <nicm> | 2015-07-20 15:50:04 +0000 |
---|---|---|
committer | nicm <nicm> | 2015-07-20 15:50:04 +0000 |
commit | 92af3766ecc456bd8ade8fe5746d27f4557fdaa8 (patch) | |
tree | 371174df95c9f8b2bd7e515505325853c88040d0 /status.c | |
parent | d4ce210713cc907fc69497c51cf9b64d2f414ca4 (diff) | |
download | rtmux-92af3766ecc456bd8ade8fe5746d27f4557fdaa8.tar.gz rtmux-92af3766ecc456bd8ade8fe5746d27f4557fdaa8.tar.bz2 rtmux-92af3766ecc456bd8ade8fe5746d27f4557fdaa8.zip |
Add an option (history-file) for a file to save/restore command prompt
history, from Olof-Joachim Frahm.
Diffstat (limited to 'status.c')
-rw-r--r-- | status.c | 91 |
1 files changed, 91 insertions, 0 deletions
@@ -47,11 +47,102 @@ const char **status_prompt_complete_list(u_int *, const char *); char *status_prompt_complete_prefix(const char **, u_int); char *status_prompt_complete(struct session *, const char *); +char *status_prompt_find_history_file(void); + /* Status prompt history. */ #define PROMPT_HISTORY 100 char **status_prompt_hlist; u_int status_prompt_hsize; +/* Find the history file to load/save from/to. */ +char * +status_prompt_find_history_file(void) +{ + const char *home, *history_file; + char *path; + + history_file = options_get_string(&global_options, "history-file"); + if (*history_file == '\0') + return (NULL); + if (*history_file == '/') + return (xstrdup(history_file)); + + if (history_file[0] != '~' || history_file[1] != '/') + return (NULL); + if ((home = find_home()) == NULL) + return (NULL); + xasprintf(&path, "%s%s", home, history_file + 1); + return (path); +} + +/* Load status prompt history from file. */ +void +status_prompt_load_history(void) +{ + FILE *f; + char *history_file, *line, *tmp; + size_t length; + + if ((history_file = status_prompt_find_history_file()) == NULL) + return; + log_debug("loading history from %s", history_file); + + f = fopen(history_file, "r"); + if (f == NULL) { + log_debug("%s: %s", history_file, strerror(errno)); + free(history_file); + return; + } + free(history_file); + + for (;;) { + if ((line = fgetln(f, &length)) == NULL) + break; + + if (length > 0) { + if (line[length - 1] == '\n') { + line[length - 1] = '\0'; + status_prompt_add_history(line); + } else { + tmp = xmalloc(length + 1); + memcpy(tmp, line, length); + tmp[length] = '\0'; + status_prompt_add_history(tmp); + free(tmp); + } + } + } + fclose(f); +} + +/* Save status prompt history to file. */ +void +status_prompt_save_history(void) +{ + FILE *f; + u_int i; + char *history_file; + + if ((history_file = status_prompt_find_history_file()) == NULL) + return; + log_debug("saving history to %s", history_file); + + f = fopen(history_file, "w"); + if (f == NULL) { + log_debug("%s: %s", history_file, strerror(errno)); + free(history_file); + return; + } + free(history_file); + + for (i = 0; i < status_prompt_hsize; i++) { + fputs(status_prompt_hlist[i], f); + fputc('\n', f); + } + fclose(f); + +} + /* Status output tree. */ RB_GENERATE(status_out_tree, status_out, entry, status_out_cmp); |