src/console.c
author Tero Marttila <terom@fixme.fi>
Thu, 07 May 2009 02:12:06 +0300
changeset 173 1a7afcd2dd1a
parent 170 1b2f28e26eef
child 200 c414343101df
permissions -rw-r--r--
implement a separate resolver module
92
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
#include "console.h"
137
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
     2
#include "log.h"
92
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
93
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents: 92
diff changeset
     4
#include <stdlib.h>
92
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
#include <unistd.h>
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
#include <stdio.h>
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
#include <readline/readline.h>
95
6bb8ef294689 add the add_history() call to console.c
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
     8
#include <readline/history.h>
137
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
     9
170
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
    10
#include <signal.h>
92
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
#include <assert.h>
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
/** The global console state */
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
static struct console _console;
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
/**
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
 * Our stdin input handler
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
 */
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
static void console_input (int fd, short what, void *arg)
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
{
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
    struct console *console = arg;
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
    (void) fd;
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
    (void) what;
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
137
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
    26
    // update state
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
    27
    console->have_input = true;
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
    28
92
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
    // tell readline to process it
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
    rl_callback_read_char();
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
}
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
/**
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
 * Our readline line handler
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
 */
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
static void console_line (char *line)
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
{
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
    struct console *console = &_console;
170
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
    39
    
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
    40
    // special-case EOF
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
    41
    if (!line) {
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
    42
        // prettify
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
    43
        rl_crlf();
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
    44
        rl_on_new_line();
92
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
170
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
    46
        if (console->callbacks->on_eof)
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
    47
            console->callbacks->on_eof(console->cb_arg);
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
    48
        
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
    49
        return;
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
    50
    }
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
    51
137
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
    52
    // update state
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
    53
    console->have_input = false;
93
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents: 92
diff changeset
    54
92
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
    // invoke the console callback
93
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents: 92
diff changeset
    56
    if (console->callbacks && console->callbacks->on_line)
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents: 92
diff changeset
    57
        console->callbacks->on_line(line, console->cb_arg);
95
6bb8ef294689 add the add_history() call to console.c
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
    58
    
6bb8ef294689 add the add_history() call to console.c
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
    59
    // add to history mechanism
6bb8ef294689 add the add_history() call to console.c
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
    60
    add_history(line);
93
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents: 92
diff changeset
    61
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents: 92
diff changeset
    62
    // release the line
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents: 92
diff changeset
    63
    free(line);
137
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
    64
    
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
    65
    // update state, as the prompt will be displayed again
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
    66
    console->have_input = true;
92
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
}
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
170
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
    69
static void on_sigint (int sig)
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
    70
{
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
    71
    struct console *console = &_console;
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
    72
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
    73
    (void) sig;
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
    74
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
    75
    // interrupt the input line
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
    76
    rl_free_line_state();
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
    77
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
    78
    // redisplay on new line
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
    79
    rl_crlf();
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
    80
    rl_callback_handler_install(console->config.prompt, console_line);
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
    81
}
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
    82
92
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    83
err_t console_init (struct console **console_ptr, struct event_base *ev_base, const struct console_config *config,
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    84
        const struct console_callbacks *callbacks, void *cb_arg, struct error_info *err)
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    85
{
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    86
    struct console *console = &_console;
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    87
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    88
    // check it's not already initialized
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    89
    assert(!console->initialized);
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    90
170
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
    91
    // store
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
    92
    console->config = *config;
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
    93
93
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents: 92
diff changeset
    94
    // store callbacks?
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents: 92
diff changeset
    95
    if (callbacks)
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents: 92
diff changeset
    96
        console_set_callbacks(console, callbacks, cb_arg);
92
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    97
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    98
    // setup the input event
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    99
    if ((console->ev = event_new(ev_base, STDIN_FILENO, EV_READ | EV_PERSIST, &console_input, console)) == NULL)
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   100
        JUMP_SET_ERROR(err, ERR_EVENT_NEW);
170
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
   101
    
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
   102
    // set our SIGINT handler
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
   103
    struct sigaction sigact;
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
   104
    
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
   105
    memset(&sigact, 0, sizeof(sigact));
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
   106
    sigact.sa_handler = on_sigint;
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
   107
    sigaction(SIGINT, &sigact, NULL);
92
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   108
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   109
    // setup readline
170
1b2f28e26eef replace old SIGINT handling with SIGTERM, and have SIGINT just abort the console input line. Now EOF (^D) will cause lua_console to nexus_shutdown
Tero Marttila <terom@fixme.fi>
parents: 137
diff changeset
   110
    rl_callback_handler_install(config->prompt, console_line);
92
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   111
    
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   112
    // mark it as initialized
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   113
    console->initialized = true;
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   114
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   115
    // enable input
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   116
    if (event_add(console->ev, NULL))
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   117
        JUMP_SET_ERROR(err, ERR_EVENT_ADD);
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   118
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   119
    // ok
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   120
    *console_ptr = console;
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   121
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   122
    return SUCCESS;
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   123
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   124
error:
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   125
    console_destroy(console);
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   126
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   127
    return ERROR_CODE(err);
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   128
}
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   129
93
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents: 92
diff changeset
   130
void console_set_callbacks (struct console *console, const struct console_callbacks *callbacks, void *cb_arg)
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents: 92
diff changeset
   131
{
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents: 92
diff changeset
   132
    console->callbacks = callbacks;
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents: 92
diff changeset
   133
    console->cb_arg = cb_arg;
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents: 92
diff changeset
   134
}
42ade8285570 add some rudimentary lua support, by having a simple interactive console, and providing access to irc_client_quit
Tero Marttila <terom@fixme.fi>
parents: 92
diff changeset
   135
137
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   136
err_t console_print (struct console *console, const char *line)
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   137
{
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   138
    if (console->have_input)
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   139
        // don't interrupt current input line
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   140
        rl_crlf();
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   141
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   142
    // output the line
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   143
    if (printf("%s\n", line) < 0)
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   144
        return _ERR_GENERAL;
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   145
    
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   146
    if (console->have_input) {
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   147
        // restore input
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   148
        rl_on_new_line();
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   149
        rl_redisplay();
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   150
    }
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   151
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   152
    // ok
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   153
    return SUCCESS;
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   154
}
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   155
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   156
/**
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   157
 * Our log_output_func implementation
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   158
 */
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   159
static void console_log_output_func (const char *line, void *_console)
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   160
{
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   161
    struct console *console = _console;
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   162
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   163
    console_print(console, line);
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   164
}
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   165
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   166
void console_set_log_output (struct console *console)
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   167
{
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   168
    // replace old one
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   169
    log_set_func(console_log_output_func, console);
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   170
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   171
    // mark
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   172
    console->log_output = true;
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   173
}
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   174
92
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   175
void console_destroy (struct console *console)
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   176
{
137
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   177
    if (console->log_output)
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   178
        // unset ourselves as the log handler
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   179
        log_set_func(NULL, NULL);
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   180
92
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   181
    // remove the input event
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   182
    if (console->ev)
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   183
        event_free(console->ev); 
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   184
    
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   185
    console->ev = NULL;
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   186
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   187
    // de-init rl?
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   188
    if (console->initialized)
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   189
        rl_callback_handler_remove();
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   190
    
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   191
    console->initialized = false;
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   192
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   193
    // remove stored stuff
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   194
    console->callbacks = console->cb_arg = NULL;
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   195
}
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   196