linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Maico Timmerman <maico.timmerman@gmail.com>
To: linux-bluetooth@vger.kernel.org
Cc: Maico Timmerman <maico.timmerman@gmail.com>
Subject: [PATCH 1/1] Client: Added support for the history file.
Date: Thu, 12 Apr 2018 12:34:32 +0200	[thread overview]
Message-ID: <20180412103432.9760-2-maico.timmerman@gmail.com> (raw)
In-Reply-To: <20180412103432.9760-1-maico.timmerman@gmail.com>

Individual interactive tools can set a history file, which will be read
on startup and written on clean-up.
---
 AUTHORS            |  1 +
 client/main.c      |  1 +
 src/shared/shell.c | 23 +++++++++++++++++++++++
 src/shared/shell.h |  2 ++
 4 files changed, 27 insertions(+)

diff --git a/AUTHORS b/AUTHORS
index df9cb96ad..d2edbf749 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -100,3 +100,4 @@ Bharat Panda <bharat.panda@samsung.com>
 Marie Janssen <jamuraa@chromium.org>
 Jaganath Kanakkassery <jaganath.k@samsung.com>
 Michał Narajowski <michal.narajowski@codecoup.pl>
+Maico Timmerman <maico.timmerman@gmail.com>
diff --git a/client/main.c b/client/main.c
index b96278d45..ed536a720 100644
--- a/client/main.c
+++ b/client/main.c
@@ -2544,6 +2544,7 @@ int main(int argc, char *argv[])
 	bt_shell_add_submenu(&scan_menu);
 	bt_shell_add_submenu(&gatt_menu);
 	bt_shell_set_prompt(PROMPT_OFF);
+	bt_shell_set_history_file(".bluetoothctl_history");
 
 	if (agent_option)
 		auto_register_agent = g_strdup(agent_option);
diff --git a/src/shared/shell.c b/src/shared/shell.c
index be2a8dfe0..39583de1e 100644
--- a/src/shared/shell.c
+++ b/src/shared/shell.c
@@ -38,6 +38,7 @@
 
 #include <readline/readline.h>
 #include <readline/history.h>
+#include <pwd.h>
 
 #include "src/shared/mainloop.h"
 #include "src/shared/timeout.h"
@@ -69,6 +70,8 @@ static struct {
 	int timeout;
 	struct io *input;
 
+	char *bt_shell_history;
+
 	bool saved_prompt;
 	bt_shell_prompt_input_func saved_func;
 	void *saved_user_data;
@@ -1027,6 +1030,11 @@ void bt_shell_cleanup(void)
 	bt_shell_release_prompt("");
 	bt_shell_detach();
 
+	if (data.bt_shell_history) {
+		write_history(data.bt_shell_history);
+		history_truncate_file(data.bt_shell_history, 500);
+	}
+
 	if (data.envs) {
 		queue_destroy(data.envs, env_destroy);
 		data.envs = NULL;
@@ -1079,6 +1087,21 @@ bool bt_shell_add_submenu(const struct bt_shell_menu *menu)
 	return true;
 }
 
+void bt_shell_set_history_file(const char *string)
+{
+	char *homedir;
+
+	// First check HOME env variable, otherwise read homedir from /etc/passwd.
+	if ((homedir = getenv("HOME")) == NULL) {
+		homedir = getpwuid(getuid())->pw_dir;
+	}
+	data.bt_shell_history = strcat(strcat(homedir, "/"), string);;
+
+	// Read history file and set index of history beyond latest position.
+	read_history(data.bt_shell_history);
+	history_set_pos(history_length);
+}
+
 void bt_shell_set_prompt(const char *string)
 {
 	if (!data.init || data.mode)
diff --git a/src/shared/shell.h b/src/shared/shell.h
index 8b7cb7f30..1c4ff0e7b 100644
--- a/src/shared/shell.h
+++ b/src/shared/shell.h
@@ -79,6 +79,8 @@ bool bt_shell_remove_submenu(const struct bt_shell_menu *menu);
 
 void bt_shell_set_prompt(const char *string);
 
+void bt_shell_set_history_file(const char *string);
+
 void bt_shell_printf(const char *fmt,
 				...) __attribute__((format(printf, 1, 2)));
 void bt_shell_hexdump(const unsigned char *buf, size_t len);
-- 
2.17.0


  reply	other threads:[~2018-04-12 10:34 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-12 10:34 [PATCH 0/1] History for interactive shell across sessions Maico Timmerman
2018-04-12 10:34 ` Maico Timmerman [this message]
2018-04-12 13:01   ` [PATCH 1/1] Client: Added support for the history file Bastien Nocera
2018-04-12 14:39   ` Luiz Augusto von Dentz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180412103432.9760-2-maico.timmerman@gmail.com \
    --to=maico.timmerman@gmail.com \
    --cc=linux-bluetooth@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).