src/console.h
author Tero Marttila <terom@fixme.fi>
Sun, 12 Apr 2009 22:19:54 +0300
changeset 137 c607c357c486
parent 93 42ade8285570
child 170 1b2f28e26eef
permissions -rw-r--r--
implement console_print and log_set_func
#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. 
     *
     * XXX: currently, line might be NULL on EOF, but this is probably a second callback
     */
    void (*on_line) (const 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.
 *
 * You may replace the callbacks/cb_arg field with a new one at any time, using console_set_callbacks().
 */
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;

    /** Set as log output function? */
    bool log_output;

    /** In the middle of input? */
    bool have_input;
};

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