1 2

[PATCH] Add mrsh_get_alias

Details
Message ID
<20181227180456.23517-1-sir@cmpwn.com>
DKIM signature
permerror
Download raw message
Patch: +10 -8
---
 include/mrsh/parser.h | 2 +-
 include/mrsh/shell.h  | 2 ++
 main.c                | 7 +------
 parser/parser.c       | 2 +-
 shell/shell.c         | 5 +++++
 5 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/include/mrsh/parser.h b/include/mrsh/parser.h
index c67f59d..29f1992 100644
--- a/include/mrsh/parser.h
+++ b/include/mrsh/parser.h
@@ -52,7 +52,7 @@ bool mrsh_parser_eof(struct mrsh_parser *state);
 /**
  * Set the alias callback.
  */
-void mrsh_parser_set_alias(struct mrsh_parser *state,
+void mrsh_parser_set_alias_fn(struct mrsh_parser *state,
 	mrsh_parser_alias_func_t alias, void *user_data);
 /**
  * Check if the parser ended with a syntax error. The error message is returned.
diff --git a/include/mrsh/shell.h b/include/mrsh/shell.h
index e0fae04..62b28c4 100644
--- a/include/mrsh/shell.h
+++ b/include/mrsh/shell.h
@@ -93,6 +93,8 @@ struct mrsh_state {
 void mrsh_function_destroy(struct mrsh_function *fn);
 
 void mrsh_state_init(struct mrsh_state *state);
+/** Helper function suitable for use in mrsh_parser_set_alias */
+const char *mrsh_get_alias(const char *name, void *data);
 void mrsh_state_finish(struct mrsh_state *state);
 void mrsh_env_set(struct mrsh_state *state,
 	const char *key, const char *value, uint32_t attribs);
diff --git a/main.c b/main.c
index 06bc0ab..474032b 100644
--- a/main.c
+++ b/main.c
@@ -13,11 +13,6 @@
 #include <unistd.h>
 #include "frontend.h"
 
-static const char *get_alias(const char *name, void *data) {
-	struct mrsh_state *state = data;
-	return mrsh_hashtable_get(&state->aliases, name);
-}
-
 int main(int argc, char *argv[]) {
 	struct mrsh_state state = {0};
 	mrsh_state_init(&state);
@@ -65,7 +60,7 @@ int main(int argc, char *argv[]) {
 			parser = mrsh_parser_with_fd(fd);
 		}
 	}
-	mrsh_parser_set_alias(parser, get_alias, &state);
+	mrsh_parser_set_alias_fn(parser, mrsh_get_alias, &state);
 
 	struct mrsh_buffer read_buffer = {0};
 	while (state.exit == -1) {
diff --git a/parser/parser.c b/parser/parser.c
index d320313..184349b 100644
--- a/parser/parser.c
+++ b/parser/parser.c
@@ -352,7 +352,7 @@ bool mrsh_parser_eof(struct mrsh_parser *state) {
 	return state->has_sym && state->sym == EOF_TOKEN;
 }
 
-void mrsh_parser_set_alias(struct mrsh_parser *state,
+void mrsh_parser_set_alias_fn(struct mrsh_parser *state,
 		mrsh_parser_alias_func_t alias, void *user_data) {
 	state->alias = alias;
 	state->alias_user_data = user_data;
diff --git a/shell/shell.c b/shell/shell.c
index fb010ba..bc3c943 100644
--- a/shell/shell.c
+++ b/shell/shell.c
@@ -22,6 +22,11 @@ void mrsh_state_init(struct mrsh_state *state) {
 	state->args = calloc(1, sizeof(struct mrsh_call_frame));
 }
 
+const char *mrsh_get_alias(const char *name, void *data) {
+	struct mrsh_state *state = data;
+	return mrsh_hashtable_get(&state->aliases, name);
+}
+
 static void state_string_finish_iterator(const char *key, void *value,
 		void *user_data) {
 	free(value);
-- 
-- 
2.20.1
Details
Message ID
<wsQo0OqjSkBCF6iD5AKVmIVMb0_rUiUdQPBLw4VIzNFEYMwNd0js5sYKqxiKkia5uUkKaNxyZmgZROdgdvuOgiP5URK0IS_R_WAmOBrIbaE=@emersion.fr>
In-Reply-To
<20181227180456.23517-1-sir@cmpwn.com> (view parent)
DKIM signature
pass
Download raw message
Hmm. I'm not a fan of exposing a function with this name and a void *
argument. Maybe we could replace it with a mrsh_set_default_alias_func
function that sets up the default unexported callback? Or maybe just
rename this function to mrsh_get_alias_func to make it clear it's not
meant to be used as-is?