src/console.h
author Tero Marttila <terom@fixme.fi>
Wed, 20 May 2009 22:52:01 +0300
changeset 202 210c43e6c088
parent 200 c414343101df
child 203 ffdf53fd0337
permissions -rw-r--r--
slight tweaks to nexus_lua, use error_t and no need to pop the loaded chunk ourselv - lua_pcall will always do that
#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>

/**
 * The console state.
 */
struct console;

/**
 * Callbacks for event-based actions
 */
struct console_callbacks {
    /**
     * A line was read from the console. 
     */
    void (*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);
};

/**
 * 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, struct error_info *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);

/**
 * 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 /* CONSOLE_H */