All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 1/2] shared/shell: Set shell name on SHELL env
@ 2018-06-14 11:26 Luiz Augusto von Dentz
  2018-06-14 11:26 ` [PATCH v2 2/2] shared/shell: Add history support Luiz Augusto von Dentz
  2018-06-15  6:41 ` [PATCH v2 1/2] shared/shell: Set shell name on SHELL env Luiz Augusto von Dentz
  0 siblings, 2 replies; 3+ messages in thread
From: Luiz Augusto von Dentz @ 2018-06-14 11:26 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

This set SHELL to the first argument given.
---
 src/shared/shell.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/src/shared/shell.c b/src/shared/shell.c
index 97d91577e..e87336a0c 100644
--- a/src/shared/shell.c
+++ b/src/shared/shell.c
@@ -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)
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH v2 2/2] shared/shell: Add history support
  2018-06-14 11:26 [PATCH v2 1/2] shared/shell: Set shell name on SHELL env Luiz Augusto von Dentz
@ 2018-06-14 11:26 ` Luiz Augusto von Dentz
  2018-06-15  6:41 ` [PATCH v2 1/2] shared/shell: Set shell name on SHELL env Luiz Augusto von Dentz
  1 sibling, 0 replies; 3+ messages in thread
From: Luiz Augusto von Dentz @ 2018-06-14 11:26 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

This uses read_history/write_history to load and save input history.
---
 src/shared/shell.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

diff --git a/src/shared/shell.c b/src/shared/shell.c
index e87336a0c..4e57bbd10 100644
--- a/src/shared/shell.c
+++ b/src/shared/shell.c
@@ -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();
 }
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2 1/2] shared/shell: Set shell name on SHELL env
  2018-06-14 11:26 [PATCH v2 1/2] shared/shell: Set shell name on SHELL env Luiz Augusto von Dentz
  2018-06-14 11:26 ` [PATCH v2 2/2] shared/shell: Add history support Luiz Augusto von Dentz
@ 2018-06-15  6:41 ` Luiz Augusto von Dentz
  1 sibling, 0 replies; 3+ messages in thread
From: Luiz Augusto von Dentz @ 2018-06-15  6:41 UTC (permalink / raw)
  To: linux-bluetooth

Hi,

On Thu, Jun 14, 2018 at 2:26 PM, Luiz Augusto von Dentz
<luiz.dentz@gmail.com> wrote:
> From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
>
> This set SHELL to the first argument given.
> ---
>  src/shared/shell.c | 5 +++++
>  1 file changed, 5 insertions(+)
>
> diff --git a/src/shared/shell.c b/src/shared/shell.c
> index 97d91577e..e87336a0c 100644
> --- a/src/shared/shell.c
> +++ b/src/shared/shell.c
> @@ -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)
> --
> 2.17.1

Applied.


-- 
Luiz Augusto von Dentz

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-06-15  6:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-06-14 11:26 [PATCH v2 1/2] shared/shell: Set shell name on SHELL env Luiz Augusto von Dentz
2018-06-14 11:26 ` [PATCH v2 2/2] shared/shell: Add history support Luiz Augusto von Dentz
2018-06-15  6:41 ` [PATCH v2 1/2] shared/shell: Set shell name on SHELL env Luiz Augusto von Dentz

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.