src/lib/console.h
branchnew-lib-errors
changeset 218 5229a5d098b2
parent 217 7728d6ec3abf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/console.h	Thu May 28 00:35:02 2009 +0300
@@ -0,0 +1,113 @@
+#ifndef LIBQMSK_CONSOLE_H
+#define LIBQMSK_CONSOLE_H
+
+/**
+ * @file
+ *
+ * An interactive line-based console interface for runtime configuration.
+ *
+ * This uses the GNU readline library to implement the console, and hence, the console uses global state, and there
+ * can only be one console per process - not that it should matter, since the console requires a tty.
+ *
+ * XXX: the log module interferes with this
+ */
+#include "error.h"
+#include <event2/event.h>
+
+/**
+ * The console state.
+ */
+struct console;
+
+/**
+ * Return codes for console_callbacks::on_line
+ */
+enum console_line_status {
+    /** 
+     * OK, line was processed, display prompt for next input line 
+     */
+    CONSOLE_CONTINUE,
+
+    /**
+     * Line is still executing, do not prompt for next line .
+     *
+     * Call console_continue() once the line was handled.
+     */
+    CONSOLE_WAIT,
+};
+
+/**
+ * Callbacks for event-based actions
+ */
+struct console_callbacks {
+    /**
+     * A line was read from the console.
+     *
+     * The return code defines how execution continues.
+     */
+    enum console_line_status (*on_line) (const char *line, void *arg);
+
+    /**
+     * EOF was read on the console.
+     *
+     * Note that for interactive consoles, EOF isn't actually EOF - there might be multiple EOFs...
+     */
+    void (*on_eof) (void *arg);
+
+    /**
+     * An input interrupt (SIGINT) was recieved while in the wait state. This can be used to abort execution and
+     * call console_continue quickly.
+     */
+    void (*on_interrupt) (void *arg);
+};
+
+/**
+ * Configuration info for console operation
+ */
+struct console_config {
+    /** The prompt to use when displaying lines */
+    const char *prompt;
+};
+
+/**
+ * Initialize the console, setting up the TTY and input handler.
+ *
+ * @param console_ptr returned new console struct
+ * @param ev_base the libevent base to use
+ * @param config configuration things for the console
+ * @param callbacks optional callbacks, can be updated later
+ * @param cb_arg option callback argument, can be updated later
+ * @param err returned error info
+ */
+err_t console_init (struct console **console_ptr, struct event_base *ev_base, const struct console_config *config,
+        const struct console_callbacks *callbacks, void *cb_arg, error_t *err);
+
+/**
+ * Replace the current callbacks with the given new ones.
+ */
+void console_set_callbacks (struct console *console, const struct console_callbacks *callbacks, void *cb_arg);
+
+/**
+ * Continue reading input after a CONSOLE_WAIT return code from console_callbacks::on_line.
+ *
+ * This does nothing if the console is not currently in the waiting state (i.e. last console_callbacks::on_line
+ * returned CONSOLE_WAIT).
+ */
+void console_continue (struct console *console);
+
+/**
+ * Output a full line (without included newline) on the console, trying not to interfere with the input too much.
+ */
+err_t console_print (struct console *console, const char *line);
+
+/**
+ * Install this console as the log output handler
+ */
+void console_set_log_output (struct console *console);
+
+/**
+ * Deinitialize the console, restoring the TTY and releasing resources
+ */
+void console_destroy (struct console *console);
+
+#endif /* LIBQMSK_CONSOLE_H */