c4de9e09d6690c52e9390ade307136df066dc913 — Drew DeVault 6 months ago acb409b
Add mrsh_get_alias
5 files changed, 18 insertions(+), 8 deletions(-)

M include/mrsh/parser.h
M include/mrsh/shell.h
M main.c
M parser/parser.c
M shell/shell.c
M include/mrsh/parser.h => include/mrsh/parser.h +1 -1
@@ 52,7 52,7 @@ /**
   * Set the alias callback.
   */
- void mrsh_parser_set_alias(struct mrsh_parser *state,
+ void mrsh_parser_set_alias_func(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.

M include/mrsh/shell.h => include/mrsh/shell.h +4 -0
@@ 92,7 92,11 @@   void mrsh_function_destroy(struct mrsh_function *fn);
  
+ struct mrsh_parser;
+ 
  void mrsh_state_init(struct mrsh_state *state);
+ void mrsh_state_set_parser_alias_func(
+ 		struct mrsh_state *state, struct mrsh_parser *parser);
  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);

M main.c => main.c +1 -6
@@ 14,11 14,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);


@@ 68,7 63,7 @@ parser = mrsh_parser_with_fd(fd);
  		}
  	}
- 	mrsh_parser_set_alias(parser, get_alias, &state);
+ 	mrsh_state_set_parser_alias_func(&state, parser);
  
  	struct mrsh_buffer read_buffer = {0};
  	while (state.exit == -1) {

M parser/parser.c => parser/parser.c +1 -1
@@ 352,7 352,7 @@ return state->has_sym && state->sym == EOF_TOKEN;
  }
  
- void mrsh_parser_set_alias(struct mrsh_parser *state,
+ void mrsh_parser_set_alias_func(struct mrsh_parser *state,
  		mrsh_parser_alias_func_t alias, void *user_data) {
  	state->alias = alias;
  	state->alias_user_data = user_data;

M shell/shell.c => shell/shell.c +11 -0
@@ 1,6 1,7 @@ #define _POSIX_C_SOURCE 200809L
  #include <assert.h>
  #include <mrsh/hashtable.h>
+ #include <mrsh/parser.h>
  #include <stdlib.h>
  #include <string.h>
  #include <unistd.h>


@@ 22,6 23,16 @@ state->args = calloc(1, sizeof(struct mrsh_call_frame));
  }
  
+ static const char *get_alias(const char *name, void *data) {
+ 	struct mrsh_state *state = data;
+ 	return mrsh_hashtable_get(&state->aliases, name);
+ }
+ 
+ void mrsh_state_set_parser_alias_func(
+ 		struct mrsh_state *state, struct mrsh_parser *parser) {
+ 	mrsh_parser_set_alias_func(parser, get_alias, state);
+ }
+ 
  static void state_string_finish_iterator(const char *key, void *value,
  		void *user_data) {
  	free(value);