99
1010static const char * quit_matches [] = {"quit" , "q" , NULL };
1111static const char * help_matches [] = {"help" , "?" , NULL };
12-
13- /* Debugger commands */
1412static const char * debug_info_matches [] = {"info" , NULL };
1513
16- static void print_commands_help () {
14+ static void handle_quit (struct LacoState * laco , const char * * arguments ) {
15+ laco_kill (laco , 0 , "Exiting laco..." );
16+ }
17+
18+ static void handle_help (struct LacoState * laco , const char * * arguments ) {
1719 puts (" Commands available:\n" );
1820 puts (" :quit, :q \tExit laco" );
1921 puts (" :help, :? \tDisplay this list of commands" );
2022 puts (" :info <name> \tShow information on given function" );
2123}
2224
23- static inline bool is_quit (const char * command ) {
24- return laco_is_match (quit_matches , command );
25+ static void handle_debug_info (struct LacoState * laco ,
26+ const char * * arguments ) {
27+ laco_print_debug_info (laco , arguments [0 ]);
2528}
2629
27- static inline bool is_help (const char * command ) {
28- return laco_is_match (help_matches , command );
29- }
30+ static const struct LacoCommand line_commands [] = {
31+ { quit_matches , handle_quit },
32+ { help_matches , handle_help },
33+
34+ /* Debugger commands */
35+ { debug_info_matches , handle_debug_info },
36+
37+ { NULL , NULL }
38+ };
3039
31- static inline bool is_debug_info (const char * command ) {
32- return laco_is_match (debug_info_matches , command );
40+ /* External API */
41+
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+ }
3354}
3455
3556void laco_handle_command (struct LacoState * laco , char * line ) {
@@ -41,13 +62,7 @@ void laco_handle_command(struct LacoState* laco, char* line) {
4162 const char * command = command_words [0 ];
4263 const char * * arguments = (const char * * ) command_words + 1 ;
4364
44- if (is_quit (command )) {
45- laco_kill (laco , 0 , "Exiting laco..." );
46- } else if (is_help (command )) {
47- print_commands_help ();
48- } else if (is_debug_info (command )) {
49- laco_print_debug_info (laco , arguments [0 ]);
50- }
65+ laco_dispatch (line_commands , laco , command , arguments );
5166
5267 free (command_line );
5368 free (command_words );
0 commit comments