Skip to contentSkip to conversation

shared/shell: Set shell name on SHELL env

PATCH Applied Luiz Augusto von Dentz 1 participant 3 messages Jun 14, 2018 – Jun 15, 2018 Bluetooth drivers

lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH v2 1/2 shared/shell: Set shell name on SHELL env

From: Luiz Augusto von Dentz <[email protected]>

This set SHELL to the first argument given.

---
 src/shared/shell.c | 5 +++++
 1 file changed, 5 insertions(+)
src/shared/shell.c +5 -0
index 97d91577e..e87336a0c 100644
@@ -63,6 +63,7 @@ struct bt_shell_env {
 
 static struct {
 	bool init;
+	char *name;
 	int argc;
 	char **argv;
 	bool mode;
@@ -978,6 +979,9 @@ void bt_shell_init(int argc, char **argv, const struct bt_shell_opt *opt)
 		index = -1;
 	}
 
+	data.name = strdup(argv[0]);
+	bt_shell_set_env("SHELL", data.name);
+
 	data.argc = argc - optind;
 	data.argv = argv + optind;
 	optind = 0;
@@ -1039,6 +1043,7 @@ void bt_shell_cleanup(void)
 	rl_cleanup();
 
 	data.init = false;
+	free(data.name);
 }
 
 void bt_shell_quit(int status)
Signature · 1 line
2.17.1
author maintainer
#1 lore raw

PATCH v2 2/2 shared/shell: Add history support

From: Luiz Augusto von Dentz <[email protected]>

This uses read_history/write_history to load and save input history.

---
 src/shared/shell.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
src/shared/shell.c +48 -0
index e87336a0c..4e57bbd10 100644
@@ -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();
 }
Signature · 1 line
2.17.1
author maintainer Applied
#2 lore raw
In reply to Luiz Augusto von Dentz

Hi,

Quoted 38 lines · Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This set SHELL to the first argument given.

---
 src/shared/shell.c | 5 +++++
 1 file changed, 5 insertions(+)
src/shared/shell.c +5 -1
index 97d91577e..e87336a0c 100644
@@ -63,6 +63,7 @@ struct bt_shell_env {
 
 static struct {
        bool init;
+       char *name;
        int argc;
        char **argv;
        bool mode;
@@ -978,6 +979,9 @@ void bt_shell_init(int argc, char **argv, const struct bt_shell_opt *opt)
                index = -1;
        }
 
+       data.name = strdup(argv[0]);
+       bt_shell_set_env("SHELL", data.name);
+
        data.argc = argc - optind;
        data.argv = argv + optind;
        optind = 0;
@@ -1039,6 +1043,7 @@ void bt_shell_cleanup(void)
        rl_cleanup();
 
        data.init = false;
+       free(data.name);
 }
 
 void bt_shell_quit(int status)
--
Quoted 1 line

2.17.1

Applied.

Signature · 1 line
Luiz Augusto von Dentz