1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
#include <stdio.h>
#include <string.h>
#include <glib.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <unistd.h>
#include <termios.h>
#include "io.h"
#include "cmumble.h"
/* Readline has no user_data in callbacks,
* so set global_rl_user_data before dispatching
* (and unset afterwards - so we can assert user_data,
* and find where callbacks are called, that we didnt
* expect)
*/
static gpointer global_rl_user_data = NULL;
static gboolean
read_cb(GIOChannel *source, GIOCondition condition, gpointer data)
{
struct cmumble_context *ctx = data;
if (condition & G_IO_IN) {
global_rl_user_data = ctx;
rl_callback_read_char();
global_rl_user_data = NULL;
}
return TRUE;
}
static void
print_preserve_prompt(const gchar *string)
{
int point;
char *line;
/* FIXME */
gboolean preserve = (rl_readline_state & RL_STATE_TERMPREPPED);
//preserve = 1;
if (preserve) {
point = rl_point;
line = rl_copy_text(0, rl_end);
rl_save_prompt();
rl_replace_line("", 0);
rl_redisplay();
}
fputs(string, stdout);
if (preserve) {
rl_restore_prompt();
rl_replace_line(line, 0);
rl_point = point;
rl_redisplay();
free(line);
}
}
static char *
skip_whitespace(char *text)
{
int i;
for (i = 0; text[i] != '\0'; ++i) {
if (text[i] != ' ')
return &text[i];
}
return &text[i];
}
static char *
skip_non_whitespace(char *text)
{
int i;
for (i = 0; text[i] != '\0'; ++i) {
if (text[i] == ' ')
return &text[i];
}
return &text[i];
}
static int
command_split(char *cmd, char ***argv)
{
static char *av[16];
int i;
for (i = 0; i < 15; ++i) {
cmd = skip_whitespace(cmd);
av[i] = cmd;
cmd = skip_non_whitespace(cmd);
if (cmd[0] == '\0') {
++i;
break;
} else {
cmd[0] = '\0';
cmd++;
}
}
av[i] = NULL;
*argv = av;
return i;
}
static void
process_line(char *line)
{
struct cmumble_context *ctx = global_rl_user_data;
int i;
int argc;
char **argv;
const char *cmd;
g_assert(global_rl_user_data);
rl_reset_line_state();
if (line == NULL) {
printf("quit");
rl_already_prompted = 1;
rl_crlf();
g_main_loop_quit(ctx->loop);
return;
}
line = g_strdup(line);
if (strlen(line))
add_history(line);
argc = command_split(line, &argv);
if (argc == 0)
goto out;
cmd = cmumble_command_expand_shortcut(argv[0]);
for (i = 0; ctx->commands[i].name; ++i) {
if (strlen(cmd) == strlen(ctx->commands[i].name) &&
strcmp(cmd, ctx->commands[i].name) == 0) {
ctx->commands[i].callback(ctx, argc, argv);
break;
}
}
if (ctx->commands[i].name == NULL)
g_print("Unknown command: %s\n", line);
out:
free(line);
}
int
cmumble_io_init(struct cmumble_context *ctx)
{
struct termios term;
ctx->io.input_channel = g_io_channel_unix_new(STDIN_FILENO);
g_io_add_watch(ctx->io.input_channel, G_IO_IN | G_IO_HUP,
read_cb, ctx);
if (tcgetattr(STDIN_FILENO, &term) < 0) {
g_printerr("tcgetattr failed");
return -1;
}
ctx->io.term = term;
term.c_lflag &= ~ICANON;
term.c_cc[VTIME] = 1;
if (tcsetattr(STDIN_FILENO, TCSANOW, &term) < 0) {
g_printerr("tcsetattr failed");
return -1;
}
rl_callback_handler_install("cmumble> ", process_line);
g_set_print_handler(print_preserve_prompt);
return 0;
}
int
cmumble_io_fini(struct cmumble_context *ctx)
{
rl_callback_handler_remove();
g_io_channel_unref(ctx->io.input_channel);
if (tcsetattr(STDIN_FILENO, TCSANOW, &ctx->io.term) < 0) {
g_printerr("tcsetattr failed");
return -1;
}
return 0;
}
|