shared/shell: Add history support
authorLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Wed, 13 Jun 2018 12:55:36 +0000 (15:55 +0300)
committerhimanshu <h.himanshu@samsung.com>
Tue, 14 Jan 2020 08:53:35 +0000 (14:23 +0530)
This uses read_history/write_history to load and save input history.

Change-Id: Iaca94569f29a8c3017cb2b5427aaa5e036bb3ad1
Signed-off-by: himanshu <h.himanshu@samsung.com>
src/shared/shell.c

index e87336a..4e57bbd 100644 (file)
@@ -64,6 +64,7 @@ struct bt_shell_env {
 static struct {
        bool init;
        char *name;
+       char history[256];
        int argc;
        char **argv;
        bool mode;
@@ -892,6 +893,48 @@ static struct io *setup_signalfd(void)
        return io;
 }
 
+static void rl_init_history(void)
+{
+       const char *name;
+       char *dir;
+
+       memset(data.history, 0, sizeof(data.history));
+
+       name = strrchr(data.name, '/');
+       if (!name)
+               name = data.name;
+       else
+               name++;
+
+       dir = getenv("XDG_CACHE_HOME");
+       if (dir) {
+               snprintf(data.history, sizeof(data.history), "%s/.%s_history",
+                                                       dir, name);
+               goto done;
+       }
+
+       dir = getenv("HOME");
+       if (dir) {
+               snprintf(data.history, sizeof(data.history),
+                               "%s/.cache/.%s_history", dir, name);
+               goto done;
+       }
+
+       dir = getenv("PWD");
+       if (dir) {
+               snprintf(data.history, sizeof(data.history), "%s/.%s_history",
+                                                       dir, name);
+               goto done;
+       }
+
+       return;
+
+done:
+       read_history(data.history);
+       using_history();
+       bt_shell_set_env("HISTORY", data.history);
+}
+
 static void rl_init(void)
 {
        if (data.mode)
@@ -902,6 +945,8 @@ static void rl_init(void)
 
        rl_erase_empty_line = 1;
        rl_callback_handler_install(NULL, rl_handler);
+
+       rl_init_history();
 }
 
 static const struct option main_options[] = {
@@ -1002,6 +1047,9 @@ static void rl_cleanup(void)
        if (data.mode)
                return;
 
+       if (data.history[0] != '\0')
+               write_history(data.history);
+
        rl_message("");
        rl_callback_handler_remove();
 }