src/console.c
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
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
200
c414343101df don't keep console SIGINT behaviour after it was destroyed, instead, have nexus handle it like SIGTERM, and have console override that
Tero Marttila <terom@fixme.fi>
parents: 170
diff changeset
    13
struct console {
c414343101df don't keep console SIGINT behaviour after it was destroyed, instead, have nexus handle it like SIGTERM, and have console override that
Tero Marttila <terom@fixme.fi>
parents: 170
diff changeset
    14
    /** Configuration */
c414343101df don't keep console SIGINT behaviour after it was destroyed, instead, have nexus handle it like SIGTERM, and have console override that
Tero Marttila <terom@fixme.fi>
parents: 170
diff changeset
    15
    struct console_config config;
c414343101df don't keep console SIGINT behaviour after it was destroyed, instead, have nexus handle it like SIGTERM, and have console override that
Tero Marttila <terom@fixme.fi>
parents: 170
diff changeset
    16
c414343101df don't keep console SIGINT behaviour after it was destroyed, instead, have nexus handle it like SIGTERM, and have console override that
Tero Marttila <terom@fixme.fi>
parents: 170
diff changeset
    17
    /** Input event */
c414343101df don't keep console SIGINT behaviour after it was destroyed, instead, have nexus handle it like SIGTERM, and have console override that
Tero Marttila <terom@fixme.fi>
parents: 170
diff changeset
    18
    struct event *ev;
c414343101df don't keep console SIGINT behaviour after it was destroyed, instead, have nexus handle it like SIGTERM, and have console override that
Tero Marttila <terom@fixme.fi>
parents: 170
diff changeset
    19
c414343101df don't keep console SIGINT behaviour after it was destroyed, instead, have nexus handle it like SIGTERM, and have console override that
Tero Marttila <terom@fixme.fi>
parents: 170
diff changeset
    20
    /** Callback functions */
c414343101df don't keep console SIGINT behaviour after it was destroyed, instead, have nexus handle it like SIGTERM, and have console override that
Tero Marttila <terom@fixme.fi>
parents: 170
diff changeset
    21
    const struct console_callbacks *callbacks;
c414343101df don't keep console SIGINT behaviour after it was destroyed, instead, have nexus handle it like SIGTERM, and have console override that
Tero Marttila <terom@fixme.fi>
parents: 170
diff changeset
    22
c414343101df don't keep console SIGINT behaviour after it was destroyed, instead, have nexus handle it like SIGTERM, and have console override that
Tero Marttila <terom@fixme.fi>
parents: 170
diff changeset
    23
    /** Callback context argument */
c414343101df don't keep console SIGINT behaviour after it was destroyed, instead, have nexus handle it like SIGTERM, and have console override that
Tero Marttila <terom@fixme.fi>
parents: 170
diff changeset
    24
    void *cb_arg;
c414343101df don't keep console SIGINT behaviour after it was destroyed, instead, have nexus handle it like SIGTERM, and have console override that
Tero Marttila <terom@fixme.fi>
parents: 170
diff changeset
    25
c414343101df don't keep console SIGINT behaviour after it was destroyed, instead, have nexus handle it like SIGTERM, and have console override that
Tero Marttila <terom@fixme.fi>
parents: 170
diff changeset
    26
    /** Old SIGINT handler */
c414343101df don't keep console SIGINT behaviour after it was destroyed, instead, have nexus handle it like SIGTERM, and have console override that
Tero Marttila <terom@fixme.fi>
parents: 170
diff changeset
    27
    struct sigaction old_sigint;
c414343101df don't keep console SIGINT behaviour after it was destroyed, instead, have nexus handle it like SIGTERM, and have console override that
Tero Marttila <terom@fixme.fi>
parents: 170
diff changeset
    28
c414343101df don't keep console SIGINT behaviour after it was destroyed, instead, have nexus handle it like SIGTERM, and have console override that
Tero Marttila <terom@fixme.fi>
parents: 170
diff changeset
    29
    /** Already initialized? */
c414343101df don't keep console SIGINT behaviour after it was destroyed, instead, have nexus handle it like SIGTERM, and have console override that
Tero Marttila <terom@fixme.fi>
parents: 170
diff changeset
    30
    bool initialized;
c414343101df don't keep console SIGINT behaviour after it was destroyed, instead, have nexus handle it like SIGTERM, and have console override that
Tero Marttila <terom@fixme.fi>
parents: 170
diff changeset
    31
c414343101df don't keep console SIGINT behaviour after it was destroyed, instead, have nexus handle it like SIGTERM, and have console override that
Tero Marttila <terom@fixme.fi>
parents: 170
diff changeset
    32
    /** Set as log output function? */
c414343101df don't keep console SIGINT behaviour after it was destroyed, instead, have nexus handle it like SIGTERM, and have console override that
Tero Marttila <terom@fixme.fi>
parents: 170
diff changeset
    33
    bool log_output;
c414343101df don't keep console SIGINT behaviour after it was destroyed, instead, have nexus handle it like SIGTERM, and have console override that
Tero Marttila <terom@fixme.fi>
parents: 170
diff changeset
    34
c414343101df don't keep console SIGINT behaviour after it was destroyed, instead, have nexus handle it like SIGTERM, and have console override that
Tero Marttila <terom@fixme.fi>
parents: 170
diff changeset
    35
    /** In the middle of input? */
c414343101df don't keep console SIGINT behaviour after it was destroyed, instead, have nexus handle it like SIGTERM, and have console override that
Tero Marttila <terom@fixme.fi>
parents: 170
diff changeset
    36
    bool have_input;
c414343101df don't keep console SIGINT behaviour after it was destroyed, instead, have nexus handle it like SIGTERM, and have console override that
Tero Marttila <terom@fixme.fi>
parents: 170
diff changeset
    37
};
c414343101df don't keep console SIGINT behaviour after it was destroyed, instead, have nexus handle it like SIGTERM, and have console override that
Tero Marttila <terom@fixme.fi>
parents: 170
diff changeset
    38
92
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
/** The global console state */
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
static struct console _console;
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
/**
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
 * Our stdin input handler
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
 */
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
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
    46
{
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
    struct console *console = arg;
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
    (void) fd;
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
    (void) what;
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
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 = true;
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
    54
92
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
    // tell readline to process it
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
    rl_callback_read_char();
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
}
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
/**
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
 * Our readline line handler
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
 */
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
static void console_line (char *line)
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
{
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
    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
    65
    
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
    66
    // 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
    67
    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
    68
        // 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
    69
        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
    70
        rl_on_new_line();
92
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
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
    72
        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
    73
            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
    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
        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
    76
    }
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
137
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
    78
    // update state
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
    79
    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
    80
92
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    81
    // 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
    82
    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
    83
        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
    84
    
6bb8ef294689 add the add_history() call to console.c
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
    85
    // add to history mechanism
6bb8ef294689 add the add_history() call to console.c
Tero Marttila <terom@fixme.fi>
parents: 93
diff changeset
    86
    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
    87
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
    88
    // 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
    89
    free(line);
137
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
    90
    
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
    91
    // 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
    92
    console->have_input = true;
92
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    93
}
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    94
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
    95
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
    96
{
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
    97
    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
    98
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
    99
    (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
   100
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
    // interrupt the input line
200
c414343101df don't keep console SIGINT behaviour after it was destroyed, instead, have nexus handle it like SIGTERM, and have console override that
Tero Marttila <terom@fixme.fi>
parents: 170
diff changeset
   102
    // XXX: is this the right function to call?
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
   103
    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
   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
    // 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
   106
    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
   107
    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
   108
}
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
   109
92
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   110
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
   111
        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
   112
{
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   113
    struct console *console = &_console;
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
    // check it's not already initialized
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   116
    assert(!console->initialized);
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   117
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
   118
    // 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
   119
    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
   120
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
   121
    // 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
   122
    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
   123
        console_set_callbacks(console, callbacks, cb_arg);
92
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   124
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   125
    // setup the input event
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   126
    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
   127
        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
   128
    
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
   129
    // 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
   130
    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
   131
    
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
   132
    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
   133
    sigact.sa_handler = on_sigint;
200
c414343101df don't keep console SIGINT behaviour after it was destroyed, instead, have nexus handle it like SIGTERM, and have console override that
Tero Marttila <terom@fixme.fi>
parents: 170
diff changeset
   134
    sigaction(SIGINT, &sigact, &console->old_sigint);
92
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   135
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   136
    // 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
   137
    rl_callback_handler_install(config->prompt, console_line);
92
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   138
    
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   139
    // mark it as initialized
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   140
    console->initialized = true;
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   141
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   142
    // enable input
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   143
    if (event_add(console->ev, NULL))
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   144
        JUMP_SET_ERROR(err, ERR_EVENT_ADD);
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   145
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   146
    // ok
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   147
    *console_ptr = console;
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   148
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   149
    return SUCCESS;
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   150
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   151
error:
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   152
    console_destroy(console);
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   153
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   154
    return ERROR_CODE(err);
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   155
}
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   156
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
   157
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
   158
{
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
   159
    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
   160
    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
   161
}
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
   162
137
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   163
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
   164
{
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   165
    if (console->have_input)
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   166
        // don't interrupt current input line
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   167
        rl_crlf();
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   168
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   169
    // output the line
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   170
    if (printf("%s\n", line) < 0)
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   171
        return _ERR_GENERAL;
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   172
    
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   173
    if (console->have_input) {
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   174
        // restore input
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   175
        rl_on_new_line();
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   176
        rl_redisplay();
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   177
    }
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   178
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   179
    // ok
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   180
    return SUCCESS;
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   181
}
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   182
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   183
/**
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   184
 * Our log_output_func implementation
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   185
 */
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   186
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
   187
{
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   188
    struct console *console = _console;
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   189
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   190
    console_print(console, line);
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   191
}
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   192
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   193
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
   194
{
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   195
    // replace old one
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   196
    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
   197
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   198
    // mark
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   199
    console->log_output = true;
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   200
}
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   201
92
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   202
void console_destroy (struct console *console)
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   203
{
137
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   204
    if (console->log_output)
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   205
        // unset ourselves as the log handler
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   206
        log_set_func(NULL, NULL);
c607c357c486 implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents: 95
diff changeset
   207
92
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   208
    // remove the input event
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   209
    if (console->ev)
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   210
        event_free(console->ev); 
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   211
    
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   212
    console->ev = NULL;
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   213
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   214
    // de-init rl?
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   215
    if (console->initialized)
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   216
        rl_callback_handler_remove();
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   217
    
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   218
    console->initialized = false;
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   219
200
c414343101df don't keep console SIGINT behaviour after it was destroyed, instead, have nexus handle it like SIGTERM, and have console override that
Tero Marttila <terom@fixme.fi>
parents: 170
diff changeset
   220
    // restore signal handler
c414343101df don't keep console SIGINT behaviour after it was destroyed, instead, have nexus handle it like SIGTERM, and have console override that
Tero Marttila <terom@fixme.fi>
parents: 170
diff changeset
   221
    sigaction(SIGINT, &console->old_sigint, NULL);
c414343101df don't keep console SIGINT behaviour after it was destroyed, instead, have nexus handle it like SIGTERM, and have console override that
Tero Marttila <terom@fixme.fi>
parents: 170
diff changeset
   222
92
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   223
    // remove stored stuff
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   224
    console->callbacks = console->cb_arg = NULL;
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   225
}
99661e5aac91 add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   226