Skip to content

Commit f439145

Browse files
committed
Merge branch 'move-dispatch'
2 parents 96018d0 + 05914d2 commit f439145

File tree

5 files changed

+42
-33
lines changed

5 files changed

+42
-33
lines changed

src/commands.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,6 @@ static const struct LacoCommand line_commands[] = {
3939

4040
/* External API */
4141

42-
void laco_dispatch(const struct LacoCommand* commands,
43-
struct LacoState* laco, const char* command_keyword,
44-
const char** arguments) {
45-
int i;
46-
const char** matches;
47-
48-
for(i = 0; (matches = commands[i].matches); i++) {
49-
if(laco_is_match(matches, command_keyword)) {
50-
commands[i].handler(laco, arguments);
51-
break;
52-
}
53-
}
54-
}
55-
5642
void laco_handle_command(struct LacoState* laco, char* line) {
5743
if(laco != NULL && line != NULL) {
5844
char* command_line = strdup(line + 1);

src/commands.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,6 @@
33

44
struct LacoState;
55

6-
typedef void (*LacoHandler)(struct LacoState* laco, const char** arguments);
7-
8-
struct LacoCommand {
9-
const char** matches;
10-
LacoHandler handler;
11-
};
12-
13-
/**
14-
* Goes through each instance from the list of commands and see if there is
15-
* a match with for command_keyword. When there is a match, the defined
16-
* handler inside the LacoCommand gets called -- passing in LacoState and
17-
* the arguments. The list of commands expects the last entry of the array
18-
* to be `{ NULL, NULL }` for ease of iteration.
19-
*/
20-
void laco_dispatch(const struct LacoCommand* commands,
21-
struct LacoState* laco, const char* command_keyword,
22-
const char** arguments);
23-
246
/**
257
* Gets passed ever line to see if it matches one of the REPL command. If it
268
* does, that command will be executed.

src/flags.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include <stdio.h>
44

5-
#include "commands.h"
65
#include "laco.h"
76
#include "util.h"
87

src/util.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,17 @@ char** laco_split_by(const char split_with, char* string,
5858

5959
return result;
6060
}
61+
62+
void laco_dispatch(const struct LacoCommand* commands,
63+
struct LacoState* laco, const char* command_keyword,
64+
const char** arguments) {
65+
int i;
66+
const char** matches;
67+
68+
for(i = 0; (matches = commands[i].matches); i++) {
69+
if(laco_is_match(matches, command_keyword)) {
70+
commands[i].handler(laco, arguments);
71+
break;
72+
}
73+
}
74+
}

src/util.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,23 @@
55

66
struct LacoState;
77

8+
/**
9+
* Generalized function pointer for handling commands from from either a
10+
* command line flag or a command inside of laco itself.
11+
*/
12+
typedef void (*LacoHandler)(struct LacoState* laco, const char** arguments);
13+
14+
/**
15+
* A representation of each command with a list of string matches and a
16+
* function pointer to the behavior of said command. When there is a match
17+
* to one of the string, this function will be called and passed in the
18+
* appropriate arguments.
19+
*/
20+
struct LacoCommand {
21+
const char** matches;
22+
LacoHandler handler;
23+
};
24+
825
/**
926
* Load a line into the lua stack to evaluated later. This function will
1027
* return false if there is no line input to load.
@@ -51,4 +68,15 @@ char** laco_split_by(const char split_with, char* string,
5168
#define laco_line_to_words(line) \
5269
laco_split_by(' ', line, 1)
5370

71+
/**
72+
* Goes through each instance from the list of commands and see if there is
73+
* a match with for command_keyword. When there is a match, the defined
74+
* handler inside the LacoCommand gets called -- passing in LacoState and
75+
* the arguments. The list of commands expects the last entry of the array
76+
* to be `{ NULL, NULL }` for ease of iteration.
77+
*/
78+
void laco_dispatch(const struct LacoCommand* commands,
79+
struct LacoState* laco, const char* command_keyword,
80+
const char** arguments);
81+
5482
#endif /* LACO_UTIL_H */

0 commit comments

Comments
 (0)