src/console.h
author Tero Marttila <terom@fixme.fi>
Tue, 31 Mar 2009 15:41:24 +0300
changeset 92 99661e5aac91
child 93 42ade8285570
permissions -rw-r--r--
add a simple interactive readline console
#ifndef CONSOLE_H
#define 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>
#include <stdbool.h>

/**
 * Callbacks for event-based actions
 */
struct console_callbacks {
    /** A line was read from the console */
    void (*on_line) (char *line, void *arg);
};

/**
 * Configuration info for console operation
 */
struct console_config {
    /** The prompt to use when displaying lines */
    const char *prompt;
};

/**
 * The console state...
 */
struct console {
    /** The input event */
    struct event *ev;

    /** The callback functions */
    const struct console_callbacks *callbacks;

    /** The callback context argument */
    void *cb_arg;

    /** Already initialized? */
    bool initialized;
};

/**
 * Initialize the console, setting up the TTY and input handler
 */
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, struct error_info *err);

/**
 * Deinitialize the console, restoring the TTY and releasing resources
 */
void console_destroy (struct console *console);

#endif /* CONSOLE_H */