diff options
Diffstat (limited to 'kern/shell.h')
-rw-r--r-- | kern/shell.h | 106 |
1 files changed, 79 insertions, 27 deletions
diff --git a/kern/shell.h b/kern/shell.h index 0526019e..bde2a341 100644 --- a/kern/shell.h +++ b/kern/shell.h @@ -24,34 +24,52 @@ #ifndef KERN_SHELL_H #define KERN_SHELL_H +#include <stdarg.h> #include <stddef.h> #include <kern/error.h> #include <kern/init.h> #include <kern/macros.h> -#define SHELL_REGISTER_CMDS(cmds) \ -MACRO_BEGIN \ - size_t i_; \ - int error_; \ - \ - for (i_ = 0; i_ < ARRAY_SIZE(cmds); i_++) { \ - error_ = shell_cmd_register(&(cmds)[i_]); \ - error_check(error_, __func__); \ - } \ -MACRO_END +/* + * Types for I/O functions. + */ +typedef int (*shell_getc_fn_t)(void *io_object); +typedef void (*shell_vfprintf_fn_t)(void *io_object, + const char *format, va_list ap); -typedef void (*shell_fn_t)(int argc, char *argv[]); +/* + * Shell structure, statically allocatable. + */ +struct shell; -struct shell_cmd { - struct shell_cmd *ht_next; - struct shell_cmd *ls_next; - const char *name; - shell_fn_t fn; - const char *usage; - const char *short_desc; - const char *long_desc; -}; +/* + * Shell command structure. + */ +struct shell_cmd; + +/* + * Command container, shareable across multiple shell instances. + */ +struct shell_cmd_set; + +/* + * Type for command implementation callbacks. + */ +typedef void (*shell_fn_t)(struct shell *shell, int argc, char **argv); + +#include <kern/shell_i.h> + +#define SHELL_REGISTER_CMDS(cmds, cmd_set) \ +MACRO_BEGIN \ + size_t i_; \ + int error_; \ + \ + for (i_ = 0; i_ < ARRAY_SIZE(cmds); i_++) { \ + error_ = shell_cmd_set_register(cmd_set, &(cmds)[i_]); \ + error_check(error_, __func__); \ + } \ +MACRO_END /* * Static shell command initializers. @@ -69,9 +87,9 @@ void shell_cmd_init(struct shell_cmd *cmd, const char *name, const char *short_desc, const char *long_desc); /* - * Start the shell thread. + * Initialize a command set. */ -void shell_start(void); +void shell_cmd_set_init(struct shell_cmd_set *cmd_set); /* * Register a shell command. @@ -79,16 +97,50 @@ void shell_start(void); * The command name must be unique. It must not include characters outside * the [a-zA-Z0-9-_] class. * - * The structure passed when calling this function is directly reused by - * the shell module and must persist in memory. + * Commands may safely be registered while the command set is used. + * + * The command structure must persist in memory as long as the command set + * is used. + */ +int shell_cmd_set_register(struct shell_cmd_set *cmd_set, + struct shell_cmd *cmd); + +/* + * Initialize a shell instance. + * + * On return, shell commands can be registered. + */ +void shell_init(struct shell *shell, struct shell_cmd_set *cmd_set, + shell_getc_fn_t getc_fn, shell_vfprintf_fn_t vfprintf_fn, + void *io_object); + +/* + * Run the shell. + * + * This function doesn't return. */ -int shell_cmd_register(struct shell_cmd *cmd); +void shell_run(struct shell *shell); + +/* + * Obtain the command set associated with a shell. + */ +struct shell_cmd_set * shell_get_cmd_set(struct shell *shell); + +/* + * Printf-like functions specific to the given shell instance. + */ +void shell_printf(struct shell *shell, const char *format, ...) + __attribute__((format(printf, 2, 3))); +void shell_vprintf(struct shell *shell, const char *format, va_list ap) + __attribute__((format(printf, 2, 0))); /* * This init operation provides : - * - commands can be registered - * - module fully initialized + * - main shell command registration */ INIT_OP_DECLARE(shell_setup); +struct shell_cmd_set * shell_get_main_cmd_set(void); +void shell_start(void); + #endif /* KERN_SHELL_H */ |