diff options
author | Steven Rostedt (Google) <rostedt@goodmis.org> | 2022-01-31 09:38:28 -0500 |
---|---|---|
committer | Steven Rostedt (Google) <rostedt@goodmis.org> | 2022-01-31 09:38:28 -0500 |
commit | 2709c2de791c58dc12e75f55b12fa672ce6b4cf7 (patch) | |
tree | 3852eb5bb36ccc6a2bff7c98e6eadd0cd814f980 | |
parent | 23a9a5bc2d65d3cfb02b1a9577caabb1180dbe1d (diff) | |
download | ktrace-2709c2de791c58dc12e75f55b12fa672ce6b4cf7.tar.gz |
ktrace: Save and load history of commands
Use the new ccli API that lets you save and restore history. Add it to the
.cache/ktrace file.
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
-rw-r--r-- | src/ktrace.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/ktrace.c b/src/ktrace.c index db4b560..81be866 100644 --- a/src/ktrace.c +++ b/src/ktrace.c @@ -38,6 +38,56 @@ void pdie(const char *fmt, ...) va_end(ap); } +static char *get_cache_file(void) +{ + char *cache_path; + char *home; + char *file; + int ret; + + cache_path = secure_getenv("XDG_CACHE_HOME"); + if (!cache_path) { + home = secure_getenv("HOME"); + if (!home) + return NULL; + ret = asprintf(&cache_path, "%s/.cache", home); + if (ret < 0) + return NULL; + } else { + /* So we can always free it later */ + cache_path = strdup(cache_path); + } + if (!cache_path) + return NULL; + + ret = asprintf(&file, "%s/ktrace", cache_path); + free(cache_path); + + return ret < 0 ? NULL : file; +} + +static void load_history(struct ccli *ccli) +{ + char *cache_file; + + cache_file = get_cache_file(); + if (!cache_file) + return; + ccli_history_load_file(ccli, "ktrace", cache_file); + free(cache_file); +} + +static void save_history(struct ccli *ccli) +{ + char *cache_file; + + cache_file = get_cache_file(); + if (!cache_file) + return; + ccli_history_save_file(ccli, "ktrace", cache_file); + free(cache_file); +} + int main (int argc, char **argv) { struct ccli *ccli; @@ -54,10 +104,14 @@ int main (int argc, char **argv) if (!ccli) pdie("ccli initialization"); + load_history(ccli); + ccli_register_command(ccli, "create", cmd_create, tep); ccli_register_completion(ccli, "create", create_completion); ccli_loop(ccli); + + save_history(ccli); ccli_free(ccli); tep_free(tep); |