Skip to content

Commit a6fa324

Browse files
committed
Add function for dispatching matches to handlers
1 parent 7fe3a44 commit a6fa324

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/commands.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ static inline bool is_debug_info(const char* command) {
3232
return laco_is_match(debug_info_matches, command);
3333
}
3434

35+
void laco_dispatch(const struct LacoCommand* commands,
36+
struct LacoState* laco, const char* command_keyword,
37+
const char** arguments) {
38+
int i;
39+
const char** matches;
40+
41+
for(i = 0; (matches = commands[i].matches); i++) {
42+
if(laco_is_match(matches, command_keyword)) {
43+
commands[i].handler(laco, arguments);
44+
break;
45+
}
46+
}
47+
}
48+
3549
void laco_handle_command(struct LacoState* laco, char* line) {
3650
if(laco != NULL && line != NULL) {
3751
char* command_line = strdup(line + 1);

src/commands.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ struct LacoCommand {
1010
LacoHandler handler;
1111
};
1212

13+
void laco_dispatch(const struct LacoCommand* commands,
14+
struct LacoState* laco, const char* command_keyword,
15+
const char** arguments);
16+
1317
/**
1418
* Gets passed ever line to see if it matches one of the REPL command. If it
1519
* does, that command will be executed.

0 commit comments

Comments
 (0)