linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH v2] shared/shell: Introduce bt_shell_{get,set}_env
Date: Mon, 22 Jan 2018 14:46:19 -0200	[thread overview]
Message-ID: <20180122164619.11881-1-luiz.dentz@gmail.com> (raw)

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

These function can be used to share environment variable accross
different files.
---
 src/shared/shell.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/shared/shell.h |  3 +++
 2 files changed, 66 insertions(+)

diff --git a/src/shared/shell.c b/src/shared/shell.c
index 6cdea1c7e..52a7e2e6d 100644
--- a/src/shared/shell.c
+++ b/src/shared/shell.c
@@ -56,6 +56,11 @@
 
 static GMainLoop *main_loop;
 
+struct bt_shell_env {
+	char *name;
+	void *value;
+};
+
 static struct {
 	struct io *input;
 
@@ -66,6 +71,8 @@ static struct {
 	const struct bt_shell_menu *menu;
 	const struct bt_shell_menu *main;
 	struct queue *submenus;
+
+	struct queue *envs;
 } data;
 
 static void shell_print_menu(void);
@@ -759,6 +766,14 @@ static void rl_cleanup(void)
 	rl_callback_handler_remove();
 }
 
+static void env_destroy(void *data)
+{
+	struct bt_shell_env *env = data;
+
+	free(env->name);
+	free(env);
+}
+
 void bt_shell_run(void)
 {
 	struct io *signal;
@@ -775,6 +790,11 @@ void bt_shell_run(void)
 	g_main_loop_unref(main_loop);
 	main_loop = NULL;
 
+	if (data.envs) {
+		queue_destroy(data.envs, env_destroy);
+		data.envs = NULL;
+	}
+
 	rl_cleanup();
 }
 
@@ -849,3 +869,46 @@ bool bt_shell_detach(void)
 
 	return true;
 }
+
+static bool match_env(const void *data, const void *user_data)
+{
+	const struct bt_shell_env *env = data;
+	const char *name = user_data;
+
+	return !strcmp(env->name, name);
+}
+
+void bt_shell_set_env(const char *name, void *value)
+{
+	struct bt_shell_env *env;
+
+	if (!data.envs) {
+		data.envs = queue_new();
+		goto done;
+	}
+
+	env = queue_remove_if(data.envs, match_env, (void *) name);
+	if (env)
+		env_destroy(env);
+
+done:
+	env = new0(struct bt_shell_env, 1);
+	env->name = strdup(name);
+	env->value = value;
+
+	queue_push_tail(data.envs, env);
+}
+
+void *bt_shell_get_env(const char *name)
+{
+	const struct bt_shell_env *env;
+
+	if (!data.envs)
+		return NULL;
+
+	env = queue_find(data.envs, match_env, name);
+	if (!env)
+		return NULL;
+
+	return env->value;
+}
diff --git a/src/shared/shell.h b/src/shared/shell.h
index 8b8b1f634..359629896 100644
--- a/src/shared/shell.h
+++ b/src/shared/shell.h
@@ -87,4 +87,7 @@ int bt_shell_release_prompt(const char *input);
 bool bt_shell_attach(int fd);
 bool bt_shell_detach(void);
 
+void bt_shell_set_env(const char *name, void *value);
+void *bt_shell_get_env(const char *name);
+
 void bt_shell_cleanup(void);
-- 
2.14.3


                 reply	other threads:[~2018-01-22 16:46 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20180122164619.11881-1-luiz.dentz@gmail.com \
    --to=luiz.dentz@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).