author | Tero Marttila <terom@fixme.fi> |
Thu, 21 May 2009 16:57:28 +0300 | |
changeset 212 | ddc79529a1e0 |
parent 206 | 47837a6bbbea |
child 217 | 7728d6ec3abf |
permissions | -rw-r--r-- |
92
99661e5aac91
add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
1 |
#ifndef CONSOLE_H |
99661e5aac91
add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
2 |
#define CONSOLE_H |
99661e5aac91
add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
3 |
|
99661e5aac91
add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
4 |
/** |
99661e5aac91
add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
5 |
* @file |
99661e5aac91
add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
6 |
* |
99661e5aac91
add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
7 |
* An interactive line-based console interface for runtime configuration. |
99661e5aac91
add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
8 |
* |
99661e5aac91
add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
9 |
* This uses the GNU readline library to implement the console, and hence, the console uses global state, and there |
99661e5aac91
add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
10 |
* can only be one console per process - not that it should matter, since the console requires a tty. |
99661e5aac91
add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
11 |
* |
99661e5aac91
add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
12 |
* XXX: the log module interferes with this |
99661e5aac91
add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
13 |
*/ |
99661e5aac91
add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
14 |
#include "error.h" |
99661e5aac91
add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
15 |
#include <event2/event.h> |
99661e5aac91
add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
16 |
#include <stdbool.h> |
99661e5aac91
add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
17 |
|
99661e5aac91
add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
18 |
/** |
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
|
19 |
* The console state. |
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 |
*/ |
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 |
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
|
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 |
/** |
203
ffdf53fd0337
implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
200
diff
changeset
|
24 |
* Return codes for console_callbacks::on_line |
ffdf53fd0337
implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
200
diff
changeset
|
25 |
*/ |
ffdf53fd0337
implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
200
diff
changeset
|
26 |
enum console_line_status { |
ffdf53fd0337
implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
200
diff
changeset
|
27 |
/** |
ffdf53fd0337
implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
200
diff
changeset
|
28 |
* OK, line was processed, display prompt for next input line |
ffdf53fd0337
implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
200
diff
changeset
|
29 |
*/ |
ffdf53fd0337
implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
200
diff
changeset
|
30 |
CONSOLE_CONTINUE, |
ffdf53fd0337
implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
200
diff
changeset
|
31 |
|
ffdf53fd0337
implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
200
diff
changeset
|
32 |
/** |
ffdf53fd0337
implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
200
diff
changeset
|
33 |
* Line is still executing, do not prompt for next line . |
ffdf53fd0337
implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
200
diff
changeset
|
34 |
* |
ffdf53fd0337
implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
200
diff
changeset
|
35 |
* Call console_continue() once the line was handled. |
ffdf53fd0337
implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
200
diff
changeset
|
36 |
*/ |
ffdf53fd0337
implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
200
diff
changeset
|
37 |
CONSOLE_WAIT, |
ffdf53fd0337
implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
200
diff
changeset
|
38 |
}; |
ffdf53fd0337
implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
200
diff
changeset
|
39 |
|
ffdf53fd0337
implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
200
diff
changeset
|
40 |
/** |
92
99661e5aac91
add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
41 |
* Callbacks for event-based actions |
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 |
struct console_callbacks { |
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
|
44 |
/** |
203
ffdf53fd0337
implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
200
diff
changeset
|
45 |
* A line was read from the console. |
ffdf53fd0337
implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
200
diff
changeset
|
46 |
* |
ffdf53fd0337
implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
200
diff
changeset
|
47 |
* The return code defines how execution continues. |
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
|
48 |
*/ |
203
ffdf53fd0337
implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
200
diff
changeset
|
49 |
enum console_line_status (*on_line) (const char *line, void *arg); |
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
|
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 |
/** |
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
|
52 |
* EOF was read on the 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
|
53 |
* |
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
|
54 |
* Note that for interactive consoles, EOF isn't actually EOF - there might be multiple EOFs... |
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
|
55 |
*/ |
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
|
56 |
void (*on_eof) (void *arg); |
212
ddc79529a1e0
add a console_callbacks::on_interrupt callback triggered by SIGINT while waiting
Tero Marttila <terom@fixme.fi>
parents:
206
diff
changeset
|
57 |
|
ddc79529a1e0
add a console_callbacks::on_interrupt callback triggered by SIGINT while waiting
Tero Marttila <terom@fixme.fi>
parents:
206
diff
changeset
|
58 |
/** |
ddc79529a1e0
add a console_callbacks::on_interrupt callback triggered by SIGINT while waiting
Tero Marttila <terom@fixme.fi>
parents:
206
diff
changeset
|
59 |
* An input interrupt (SIGINT) was recieved while in the wait state. This can be used to abort execution and |
ddc79529a1e0
add a console_callbacks::on_interrupt callback triggered by SIGINT while waiting
Tero Marttila <terom@fixme.fi>
parents:
206
diff
changeset
|
60 |
* call console_continue quickly. |
ddc79529a1e0
add a console_callbacks::on_interrupt callback triggered by SIGINT while waiting
Tero Marttila <terom@fixme.fi>
parents:
206
diff
changeset
|
61 |
*/ |
ddc79529a1e0
add a console_callbacks::on_interrupt callback triggered by SIGINT while waiting
Tero Marttila <terom@fixme.fi>
parents:
206
diff
changeset
|
62 |
void (*on_interrupt) (void *arg); |
92
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 |
|
99661e5aac91
add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
65 |
/** |
99661e5aac91
add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
66 |
* Configuration info for console operation |
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 |
struct console_config { |
99661e5aac91
add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
69 |
/** The prompt to use when displaying lines */ |
99661e5aac91
add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
70 |
const char *prompt; |
99661e5aac91
add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
71 |
}; |
99661e5aac91
add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
72 |
|
99661e5aac91
add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
73 |
/** |
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
|
74 |
* Initialize the console, setting up the TTY and input handler. |
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
|
75 |
* |
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
|
76 |
* @param console_ptr returned new console struct |
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
|
77 |
* @param ev_base the libevent base to use |
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
|
78 |
* @param config configuration things for the console |
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
|
79 |
* @param callbacks optional callbacks, can be updated later |
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 |
* @param cb_arg option callback argument, can be updated later |
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
|
81 |
* @param err returned error info |
92
99661e5aac91
add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
82 |
*/ |
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 |
/** |
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 |
* Replace the current callbacks with the given new ones. |
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 |
*/ |
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 |
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
|
90 |
|
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
|
91 |
/** |
203
ffdf53fd0337
implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
200
diff
changeset
|
92 |
* Continue reading input after a CONSOLE_WAIT return code from console_callbacks::on_line. |
206
47837a6bbbea
fix console to ignore input while waiting, and rename have_input -> have_prompt
Tero Marttila <terom@fixme.fi>
parents:
203
diff
changeset
|
93 |
* |
47837a6bbbea
fix console to ignore input while waiting, and rename have_input -> have_prompt
Tero Marttila <terom@fixme.fi>
parents:
203
diff
changeset
|
94 |
* This does nothing if the console is not currently in the waiting state (i.e. last console_callbacks::on_line |
47837a6bbbea
fix console to ignore input while waiting, and rename have_input -> have_prompt
Tero Marttila <terom@fixme.fi>
parents:
203
diff
changeset
|
95 |
* returned CONSOLE_WAIT). |
203
ffdf53fd0337
implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
200
diff
changeset
|
96 |
*/ |
ffdf53fd0337
implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
200
diff
changeset
|
97 |
void console_continue (struct console *console); |
ffdf53fd0337
implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
200
diff
changeset
|
98 |
|
ffdf53fd0337
implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
200
diff
changeset
|
99 |
/** |
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
93
diff
changeset
|
100 |
* Output a full line (without included newline) on the console, trying not to interfere with the input too much. |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
93
diff
changeset
|
101 |
*/ |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
93
diff
changeset
|
102 |
err_t console_print (struct console *console, const char *line); |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
93
diff
changeset
|
103 |
|
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
93
diff
changeset
|
104 |
/** |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
93
diff
changeset
|
105 |
* Install this console as the log output handler |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
93
diff
changeset
|
106 |
*/ |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
93
diff
changeset
|
107 |
void console_set_log_output (struct console *console); |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
93
diff
changeset
|
108 |
|
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
93
diff
changeset
|
109 |
/** |
92
99661e5aac91
add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
110 |
* Deinitialize the console, restoring the TTY and releasing resources |
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 |
void console_destroy (struct console *console); |
99661e5aac91
add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
113 |
|
99661e5aac91
add a simple interactive readline console
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
114 |
#endif /* CONSOLE_H */ |