forked from oils-for-unix/oils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrontend_pyreadline.cc
300 lines (263 loc) · 7.1 KB
/
frontend_pyreadline.cc
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include "frontend_pyreadline.h"
#include <assert.h>
#include <errno.h> // errno, EINTR
#include <signal.h> // SIGINT
#include <sys/select.h> // select(), FD_ISSET, FD_SET, FD_ZERO
#include "_build/detected-cpp-config.h"
#if HAVE_READLINE
#include <readline/history.h>
#include <readline/readline.h>
#endif
#include "cpp/core.h"
namespace py_readline {
static Readline* gReadline = nullptr;
// Assuming readline 4.0+
#if HAVE_READLINE
static char* do_complete(const char* text, int state) {
if (gReadline->completer_ == nullptr) {
return nullptr;
}
rl_attempted_completion_over = 1;
Str* gc_text = StrFromC(text);
Str* result = completion::ExecuteReadlineCallback(gReadline->completer_,
gc_text, state);
if (result == nullptr) {
return nullptr;
}
// According to https://web.mit.edu/gnu/doc/html/rlman_2.html#SEC37, readline
// will free any memory we return to it.
return strdup(result->data());
}
static char** completion_handler(const char* text, int start, int end) {
rl_completion_append_character = '\0';
rl_completion_suppress_append = 0;
gReadline->begidx_ = start;
gReadline->endidx_ = end;
return rl_completion_matches(text,
static_cast<rl_compentry_func_t*>(do_complete));
}
static void display_matches_hook(char** matches, int num_matches,
int max_length) {
if (gReadline->display_ == nullptr) {
return;
}
auto* gc_matches = Alloc<List<Str*>>();
// It isn't clear from the readline documentation, but matches[0] is the
// completion text and the matches returned by any callbacks start at index 1.
for (int i = 1; i <= num_matches; i++) {
gc_matches->append(StrFromC(matches[i]));
}
comp_ui::ExecutePrintCandidates(gReadline->display_, nullptr, gc_matches,
max_length);
}
#endif
Readline::Readline()
: GC_CLASS_FIXED(header_, Readline::field_mask(), sizeof(Readline)),
begidx_(),
endidx_(),
completer_delims_(StrFromC(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?")),
completer_(),
display_(),
latest_line_() {
#if HAVE_READLINE
using_history();
rl_readline_name = "oil";
/* Force rebind of TAB to insert-tab */
rl_bind_key('\t', rl_insert);
/* Bind both ESC-TAB and ESC-ESC to the completion function */
rl_bind_key_in_map('\t', rl_complete, emacs_meta_keymap);
rl_bind_key_in_map('\033', rl_complete, emacs_meta_keymap);
rl_attempted_completion_function = completion_handler;
rl_completion_display_matches_hook = display_matches_hook;
rl_catch_signals = 0;
rl_catch_sigwinch = 0;
rl_initialize();
#else
assert(0); // not implemented
#endif
}
void Readline::parse_and_bind(Str* s) {
#if HAVE_READLINE
// Make a copy -- rl_parse_and_bind() modifies its argument
Str* copy = StrFromC(s->data(), len(s));
rl_parse_and_bind(copy->data());
#else
assert(0); // not implemented
#endif
}
void Readline::add_history(Str* line) {
#if HAVE_READLINE
assert(line != nullptr);
::add_history(line->data());
#else
assert(0); // not implemented
#endif
}
void Readline::read_history_file(Str* path) {
#if HAVE_READLINE
char* p = nullptr;
if (path != nullptr) {
p = path->data();
}
read_history(p);
#else
assert(0); // not implemented
#endif
}
void Readline::write_history_file(Str* path) {
#if HAVE_READLINE
char* p = nullptr;
if (path != nullptr) {
p = path->data();
}
write_history(p);
#else
assert(0); // not implemented
#endif
}
void Readline::set_completer(completion::ReadlineCallback* completer) {
#if HAVE_READLINE
completer_ = completer;
#else
assert(0); // not implemented
#endif
}
void Readline::set_completer_delims(Str* delims) {
#if HAVE_READLINE
completer_delims_ = StrFromC(delims->data(), len(delims));
rl_completer_word_break_characters = completer_delims_->data();
#else
assert(0); // not implemented
#endif
}
void Readline::set_completion_display_matches_hook(
comp_ui::_IDisplay* display) {
#if HAVE_READLINE
display_ = display;
#else
assert(0); // not implemented
#endif
}
Str* Readline::get_line_buffer() {
#if HAVE_READLINE
return StrFromC(rl_line_buffer);
#else
assert(0); // not implemented
#endif
}
int Readline::get_begidx() {
#if HAVE_READLINE
return begidx_;
#else
assert(0); // not implemented
#endif
}
int Readline::get_endidx() {
#if HAVE_READLINE
return endidx_;
#else
assert(0); // not implemented
#endif
}
void Readline::clear_history() {
#if HAVE_READLINE
rl_clear_history();
#else
assert(0); // not implemented
#endif
}
void Readline::remove_history_item(int pos) {
#if HAVE_READLINE
HIST_ENTRY* entry = remove_history(pos);
histdata_t data = free_history_entry(entry);
free(data);
#else
assert(0); // not implemented
#endif
}
Str* Readline::get_history_item(int pos) {
#if HAVE_READLINE
HIST_ENTRY* hist_ent = history_get(pos);
if (hist_ent != nullptr) {
return StrFromC(hist_ent->line);
}
return nullptr;
#else
assert(0); // not implemented
#endif
}
int Readline::get_current_history_length() {
#if HAVE_READLINE
HISTORY_STATE* hist_st = history_get_history_state();
int length = hist_st->length;
free(hist_st);
return length;
#else
assert(0); // not implemented
#endif
}
void Readline::resize_terminal() {
#if HAVE_READLINE
rl_resize_terminal();
#else
assert(0); // not implemented
#endif
}
Readline* MaybeGetReadline() {
#ifdef HAVE_READLINE
gReadline = Alloc<Readline>();
gHeap.RootGlobalVar(gReadline);
return gReadline;
#else
return nullptr;
#endif
}
static void readline_cb(char* line) {
if (line == nullptr) {
gReadline->latest_line_ = nullptr;
} else {
gReadline->latest_line_ = line;
}
gReadline->ready_ = true;
rl_callback_handler_remove();
}
// See the following for some loose documentation on the approach here:
// https://tiswww.case.edu/php/chet/readline/readline.html#Alternate-Interface-Example
Str* readline(Str* prompt) {
fd_set fds;
FD_ZERO(&fds);
rl_callback_handler_install(prompt->data(), readline_cb);
gReadline->latest_line_ = nullptr;
gReadline->ready_ = false;
while (!gReadline->ready_) {
// Wait until stdin is ready or we are interrupted.
FD_SET(fileno(rl_instream), &fds);
int ec = select(FD_SETSIZE, &fds, NULL, NULL, NULL);
if (ec == -1) {
if (errno == EINTR && pyos::SigintCount() > 0) {
// User is trying to cancel. Abort and cleanup readline state.
rl_free_line_state();
rl_callback_sigcleanup();
rl_cleanup_after_signal();
rl_callback_handler_remove();
throw Alloc<KeyboardInterrupt>();
}
// To be consistent with CPython, retry on all other errors and signals.
continue;
}
// Remove this check if we start calling select() with a timeout above.
DCHECK(ec > 0);
if (FD_ISSET(fileno(rl_instream), &fds)) {
// Feed readline.
rl_callback_read_char();
}
}
if (gReadline->latest_line_ != nullptr) {
Str* s = StrFromC(gReadline->latest_line_);
free(gReadline->latest_line_);
gReadline->latest_line_ = nullptr;
return s;
}
return nullptr;
}
} // namespace py_readline