terom@92: #ifndef CONSOLE_H terom@92: #define CONSOLE_H terom@92: terom@92: /** terom@92: * @file terom@92: * terom@92: * An interactive line-based console interface for runtime configuration. terom@92: * terom@92: * This uses the GNU readline library to implement the console, and hence, the console uses global state, and there terom@92: * can only be one console per process - not that it should matter, since the console requires a tty. terom@92: * terom@92: * XXX: the log module interferes with this terom@92: */ terom@92: #include "error.h" terom@92: #include terom@92: #include terom@92: terom@92: /** terom@92: * Callbacks for event-based actions terom@92: */ terom@92: struct console_callbacks { terom@93: /** terom@93: * A line was read from the console. terom@93: * terom@93: * XXX: currently, line might be NULL on EOF, but this is probably a second callback terom@93: */ terom@93: void (*on_line) (const char *line, void *arg); terom@92: }; terom@92: terom@92: /** terom@92: * Configuration info for console operation terom@92: */ terom@92: struct console_config { terom@92: /** The prompt to use when displaying lines */ terom@92: const char *prompt; terom@92: }; terom@92: terom@92: /** terom@93: * The console state. terom@93: * terom@93: * You may replace the callbacks/cb_arg field with a new one at any time, using console_set_callbacks(). terom@92: */ terom@92: struct console { terom@92: /** The input event */ terom@92: struct event *ev; terom@92: terom@92: /** The callback functions */ terom@92: const struct console_callbacks *callbacks; terom@92: terom@92: /** The callback context argument */ terom@92: void *cb_arg; terom@92: terom@92: /** Already initialized? */ terom@92: bool initialized; terom@92: }; terom@92: terom@92: /** terom@93: * Initialize the console, setting up the TTY and input handler. terom@93: * terom@93: * @param console_ptr returned new console struct terom@93: * @param ev_base the libevent base to use terom@93: * @param config configuration things for the console terom@93: * @param callbacks optional callbacks, can be updated later terom@93: * @param cb_arg option callback argument, can be updated later terom@93: * @param err returned error info terom@92: */ terom@92: err_t console_init (struct console **console_ptr, struct event_base *ev_base, const struct console_config *config, terom@92: const struct console_callbacks *callbacks, void *cb_arg, struct error_info *err); terom@92: terom@92: /** terom@93: * Replace the current callbacks with the given new ones. terom@93: */ terom@93: void console_set_callbacks (struct console *console, const struct console_callbacks *callbacks, void *cb_arg); terom@93: terom@93: /** terom@92: * Deinitialize the console, restoring the TTY and releasing resources terom@92: */ terom@92: void console_destroy (struct console *console); terom@92: terom@92: #endif /* CONSOLE_H */