1 | /* This module makes GNU readline available to Python. It has ideas
|
2 | * contributed by Lee Busby, LLNL, and William Magro, Cornell Theory
|
3 | * Center. The completer interface was inspired by Lele Gaifax. More
|
4 | * recently, it was largely rewritten by Guido van Rossum.
|
5 | */
|
6 |
|
7 | /* Standard definitions */
|
8 | #include "Python.h"
|
9 | #include <setjmp.h>
|
10 | #include <signal.h>
|
11 | #include <errno.h>
|
12 | #include <sys/time.h>
|
13 |
|
14 | /* ------------------------------------------------------------------------- */
|
15 |
|
16 | /* OVM_MAIN: This section copied from autotool-generated pyconfig.h.
|
17 | * We're not detecting any of it in Oil's configure script. They are for
|
18 | * ancient readline versions.
|
19 | * */
|
20 |
|
21 | /* Define if you have readline 2.1 */
|
22 | #define HAVE_RL_CALLBACK 1
|
23 |
|
24 | /* Define if you can turn off readline's signal handling. */
|
25 | #define HAVE_RL_CATCH_SIGNAL 1
|
26 |
|
27 | /* Define if you have readline 2.2 */
|
28 | #define HAVE_RL_COMPLETION_APPEND_CHARACTER 1
|
29 |
|
30 | /* Define if you have readline 4.0 */
|
31 | #define HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK 1
|
32 |
|
33 | /* Define if you have readline 4.2 */
|
34 | #define HAVE_RL_COMPLETION_MATCHES 1
|
35 |
|
36 | /* Define if you have rl_completion_suppress_append */
|
37 | #define HAVE_RL_COMPLETION_SUPPRESS_APPEND 1
|
38 |
|
39 | /* Define if you have readline 4.0 */
|
40 | #define HAVE_RL_PRE_INPUT_HOOK 1
|
41 |
|
42 | /* Define if you have readline 4.0 */
|
43 | #define HAVE_RL_RESIZE_TERMINAL 1
|
44 |
|
45 | /* ------------------------------------------------------------------------- */
|
46 |
|
47 | #if defined(HAVE_SETLOCALE)
|
48 | /* GNU readline() mistakenly sets the LC_CTYPE locale.
|
49 | * This is evil. Only the user or the app's main() should do this!
|
50 | * We must save and restore the locale around the rl_initialize() call.
|
51 | */
|
52 | #define SAVE_LOCALE
|
53 | #include <locale.h>
|
54 | #endif
|
55 |
|
56 | #ifdef SAVE_LOCALE
|
57 | # define RESTORE_LOCALE(sl) { setlocale(LC_CTYPE, sl); free(sl); }
|
58 | #else
|
59 | # define RESTORE_LOCALE(sl)
|
60 | #endif
|
61 |
|
62 | /* GNU readline definitions */
|
63 | #undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
|
64 | #include <readline/readline.h>
|
65 | #include <readline/history.h>
|
66 |
|
67 | #ifdef HAVE_RL_COMPLETION_MATCHES
|
68 | #define completion_matches(x, y) \
|
69 | rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
|
70 | #else
|
71 | #if defined(_RL_FUNCTION_TYPEDEF)
|
72 | extern char **completion_matches(char *, rl_compentry_func_t *);
|
73 | #else
|
74 |
|
75 | #if !defined(__APPLE__)
|
76 | extern char **completion_matches(char *, CPFunction *);
|
77 | #endif
|
78 | #endif
|
79 | #endif
|
80 |
|
81 | #ifdef __APPLE__
|
82 | /*
|
83 | * It is possible to link the readline module to the readline
|
84 | * emulation library of editline/libedit.
|
85 | *
|
86 | * On OSX this emulation library is not 100% API compatible
|
87 | * with the "real" readline and cannot be detected at compile-time,
|
88 | * hence we use a runtime check to detect if we're using libedit
|
89 | *
|
90 | * Currently there is one known API incompatibility:
|
91 | * - 'get_history' has a 1-based index with GNU readline, and a 0-based
|
92 | * index with older versions of libedit's emulation.
|
93 | * - Note that replace_history and remove_history use a 0-based index
|
94 | * with both implementations.
|
95 | */
|
96 | static int using_libedit_emulation = 0;
|
97 | static const char libedit_version_tag[] = "EditLine wrapper";
|
98 |
|
99 | static int libedit_history_start = 0;
|
100 | #endif /* __APPLE__ */
|
101 |
|
102 | #ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
|
103 | static void
|
104 | on_completion_display_matches_hook(char **matches,
|
105 | int num_matches, int max_length);
|
106 | #endif
|
107 |
|
108 | /* Memory allocated for rl_completer_word_break_characters
|
109 | (see issue #17289 for the motivation). */
|
110 | static char *completer_word_break_characters;
|
111 |
|
112 | /* Exported function to send one line to readline's init file parser */
|
113 |
|
114 | static PyObject *
|
115 | parse_and_bind(PyObject *self, PyObject *args)
|
116 | {
|
117 | char *s, *copy;
|
118 | int binding_result;
|
119 |
|
120 | if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
|
121 | return NULL;
|
122 | /* Make a copy -- rl_parse_and_bind() modifies its argument */
|
123 | /* Bernard Herzog */
|
124 | copy = malloc(1 + strlen(s));
|
125 | if (copy == NULL)
|
126 | return PyErr_NoMemory();
|
127 | strcpy(copy, s);
|
128 |
|
129 | binding_result = rl_parse_and_bind(copy);
|
130 | free(copy); /* Free the copy */
|
131 |
|
132 | if (binding_result != 0) {
|
133 | PyErr_Format(PyExc_ValueError, "'%s': invalid binding", s);
|
134 | return NULL;
|
135 | }
|
136 |
|
137 | Py_RETURN_NONE;
|
138 | }
|
139 |
|
140 | PyDoc_STRVAR(doc_parse_and_bind,
|
141 | "parse_and_bind(string) -> None\n\
|
142 | Bind a key sequence to a readline function (or a variable to a value).");
|
143 |
|
144 |
|
145 |
|
146 | /* Exported function to parse a readline init file */
|
147 |
|
148 | static PyObject *
|
149 | read_init_file(PyObject *self, PyObject *args)
|
150 | {
|
151 | char *s = NULL;
|
152 | if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
|
153 | return NULL;
|
154 | errno = rl_read_init_file(s);
|
155 | if (errno)
|
156 | return PyErr_SetFromErrno(PyExc_IOError);
|
157 | Py_RETURN_NONE;
|
158 | }
|
159 |
|
160 | PyDoc_STRVAR(doc_read_init_file,
|
161 | "read_init_file([filename]) -> None\n\
|
162 | Execute a readline initialization file.\n\
|
163 | The default filename is the last filename used.");
|
164 |
|
165 |
|
166 | /* Exported function to load a readline history file */
|
167 |
|
168 | static PyObject *
|
169 | read_history_file(PyObject *self, PyObject *args)
|
170 | {
|
171 | char *s = NULL;
|
172 | if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
|
173 | return NULL;
|
174 | errno = read_history(s);
|
175 | if (errno)
|
176 | return PyErr_SetFromErrno(PyExc_IOError);
|
177 | Py_RETURN_NONE;
|
178 | }
|
179 |
|
180 | static int _history_length = -1; /* do not truncate history by default */
|
181 | PyDoc_STRVAR(doc_read_history_file,
|
182 | "read_history_file([filename]) -> None\n\
|
183 | Load a readline history file.\n\
|
184 | The default filename is ~/.history.");
|
185 |
|
186 |
|
187 | /* Exported function to save a readline history file */
|
188 |
|
189 | static PyObject *
|
190 | write_history_file(PyObject *self, PyObject *args)
|
191 | {
|
192 | char *s = NULL;
|
193 | if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
|
194 | return NULL;
|
195 | errno = write_history(s);
|
196 | if (!errno && _history_length >= 0)
|
197 | history_truncate_file(s, _history_length);
|
198 | if (errno)
|
199 | return PyErr_SetFromErrno(PyExc_IOError);
|
200 | Py_RETURN_NONE;
|
201 | }
|
202 |
|
203 | PyDoc_STRVAR(doc_write_history_file,
|
204 | "write_history_file([filename]) -> None\n\
|
205 | Save a readline history file.\n\
|
206 | The default filename is ~/.history.");
|
207 |
|
208 |
|
209 | /* Set history length */
|
210 |
|
211 | static PyObject*
|
212 | set_history_length(PyObject *self, PyObject *args)
|
213 | {
|
214 | int length = _history_length;
|
215 | if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
|
216 | return NULL;
|
217 | _history_length = length;
|
218 | Py_RETURN_NONE;
|
219 | }
|
220 |
|
221 | PyDoc_STRVAR(set_history_length_doc,
|
222 | "set_history_length(length) -> None\n\
|
223 | set the maximal number of lines which will be written to\n\
|
224 | the history file. A negative length is used to inhibit\n\
|
225 | history truncation.");
|
226 |
|
227 |
|
228 | /* Get history length */
|
229 |
|
230 | static PyObject*
|
231 | get_history_length(PyObject *self, PyObject *noarg)
|
232 | {
|
233 | return PyInt_FromLong(_history_length);
|
234 | }
|
235 |
|
236 | PyDoc_STRVAR(get_history_length_doc,
|
237 | "get_history_length() -> int\n\
|
238 | return the maximum number of lines that will be written to\n\
|
239 | the history file.");
|
240 |
|
241 |
|
242 | /* Generic hook function setter */
|
243 |
|
244 | static PyObject *
|
245 | set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
|
246 | {
|
247 | PyObject *function = Py_None;
|
248 | char buf[80];
|
249 | PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
|
250 | if (!PyArg_ParseTuple(args, buf, &function))
|
251 | return NULL;
|
252 | if (function == Py_None) {
|
253 | Py_CLEAR(*hook_var);
|
254 | }
|
255 | else if (PyCallable_Check(function)) {
|
256 | PyObject *tmp = *hook_var;
|
257 | Py_INCREF(function);
|
258 | *hook_var = function;
|
259 | Py_XDECREF(tmp);
|
260 | }
|
261 | else {
|
262 | PyOS_snprintf(buf, sizeof(buf),
|
263 | "set_%.50s(func): argument not callable",
|
264 | funcname);
|
265 | PyErr_SetString(PyExc_TypeError, buf);
|
266 | return NULL;
|
267 | }
|
268 | Py_RETURN_NONE;
|
269 | }
|
270 |
|
271 |
|
272 | /* Exported functions to specify hook functions in Python */
|
273 |
|
274 | static PyObject *completion_display_matches_hook = NULL;
|
275 | static PyObject *startup_hook = NULL;
|
276 |
|
277 | #ifdef HAVE_RL_PRE_INPUT_HOOK
|
278 | static PyObject *pre_input_hook = NULL;
|
279 | #endif
|
280 |
|
281 | static PyObject *
|
282 | set_completion_display_matches_hook(PyObject *self, PyObject *args)
|
283 | {
|
284 | PyObject *result = set_hook("completion_display_matches_hook",
|
285 | &completion_display_matches_hook, args);
|
286 | #ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
|
287 | /* We cannot set this hook globally, since it replaces the
|
288 | default completion display. */
|
289 | rl_completion_display_matches_hook =
|
290 | completion_display_matches_hook ?
|
291 | #if defined(_RL_FUNCTION_TYPEDEF)
|
292 | (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
|
293 | #else
|
294 | (VFunction *)on_completion_display_matches_hook : 0;
|
295 | #endif
|
296 | #endif
|
297 | return result;
|
298 |
|
299 | }
|
300 |
|
301 | PyDoc_STRVAR(doc_set_completion_display_matches_hook,
|
302 | "set_completion_display_matches_hook([function]) -> None\n\
|
303 | Set or remove the completion display function.\n\
|
304 | The function is called as\n\
|
305 | function(substitution, [matches], longest_match_length)\n\
|
306 | once each time matches need to be displayed.");
|
307 |
|
308 | static PyObject *
|
309 | set_startup_hook(PyObject *self, PyObject *args)
|
310 | {
|
311 | return set_hook("startup_hook", &startup_hook, args);
|
312 | }
|
313 |
|
314 | PyDoc_STRVAR(doc_set_startup_hook,
|
315 | "set_startup_hook([function]) -> None\n\
|
316 | Set or remove the function invoked by the rl_startup_hook callback.\n\
|
317 | The function is called with no arguments just\n\
|
318 | before readline prints the first prompt.");
|
319 |
|
320 |
|
321 | #ifdef HAVE_RL_PRE_INPUT_HOOK
|
322 |
|
323 | /* Set pre-input hook */
|
324 |
|
325 | static PyObject *
|
326 | set_pre_input_hook(PyObject *self, PyObject *args)
|
327 | {
|
328 | return set_hook("pre_input_hook", &pre_input_hook, args);
|
329 | }
|
330 |
|
331 | PyDoc_STRVAR(doc_set_pre_input_hook,
|
332 | "set_pre_input_hook([function]) -> None\n\
|
333 | Set or remove the function invoked by the rl_pre_input_hook callback.\n\
|
334 | The function is called with no arguments after the first prompt\n\
|
335 | has been printed and just before readline starts reading input\n\
|
336 | characters.");
|
337 |
|
338 | #endif
|
339 |
|
340 |
|
341 | /* Exported function to specify a word completer in Python */
|
342 |
|
343 | static PyObject *completer = NULL;
|
344 |
|
345 | static PyObject *begidx = NULL;
|
346 | static PyObject *endidx = NULL;
|
347 |
|
348 |
|
349 | /* Get the completion type for the scope of the tab-completion */
|
350 | static PyObject *
|
351 | get_completion_type(PyObject *self, PyObject *noarg)
|
352 | {
|
353 | return PyInt_FromLong(rl_completion_type);
|
354 | }
|
355 |
|
356 | PyDoc_STRVAR(doc_get_completion_type,
|
357 | "get_completion_type() -> int\n\
|
358 | Get the type of completion being attempted.");
|
359 |
|
360 |
|
361 | /* Get the beginning index for the scope of the tab-completion */
|
362 |
|
363 | static PyObject *
|
364 | get_begidx(PyObject *self, PyObject *noarg)
|
365 | {
|
366 | Py_INCREF(begidx);
|
367 | return begidx;
|
368 | }
|
369 |
|
370 | PyDoc_STRVAR(doc_get_begidx,
|
371 | "get_begidx() -> int\n\
|
372 | get the beginning index of the completion scope");
|
373 |
|
374 |
|
375 | /* Get the ending index for the scope of the tab-completion */
|
376 |
|
377 | static PyObject *
|
378 | get_endidx(PyObject *self, PyObject *noarg)
|
379 | {
|
380 | Py_INCREF(endidx);
|
381 | return endidx;
|
382 | }
|
383 |
|
384 | PyDoc_STRVAR(doc_get_endidx,
|
385 | "get_endidx() -> int\n\
|
386 | get the ending index of the completion scope");
|
387 |
|
388 |
|
389 | /* Set the tab-completion word-delimiters that readline uses */
|
390 |
|
391 | static PyObject *
|
392 | set_completer_delims(PyObject *self, PyObject *args)
|
393 | {
|
394 | char *break_chars;
|
395 |
|
396 | if (!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
|
397 | return NULL;
|
398 | }
|
399 | /* Keep a reference to the allocated memory in the module state in case
|
400 | some other module modifies rl_completer_word_break_characters
|
401 | (see issue #17289). */
|
402 | break_chars = strdup(break_chars);
|
403 | if (break_chars) {
|
404 | free(completer_word_break_characters);
|
405 | completer_word_break_characters = break_chars;
|
406 | rl_completer_word_break_characters = break_chars;
|
407 | Py_RETURN_NONE;
|
408 | }
|
409 | else
|
410 | return PyErr_NoMemory();
|
411 | }
|
412 |
|
413 | PyDoc_STRVAR(doc_set_completer_delims,
|
414 | "set_completer_delims(string) -> None\n\
|
415 | set the word delimiters for completion");
|
416 |
|
417 | /* _py_free_history_entry: Utility function to free a history entry. */
|
418 |
|
419 | #if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
|
420 |
|
421 | /* Readline version >= 5.0 introduced a timestamp field into the history entry
|
422 | structure; this needs to be freed to avoid a memory leak. This version of
|
423 | readline also introduced the handy 'free_history_entry' function, which
|
424 | takes care of the timestamp. */
|
425 |
|
426 | static void
|
427 | _py_free_history_entry(HIST_ENTRY *entry)
|
428 | {
|
429 | histdata_t data = free_history_entry(entry);
|
430 | free(data);
|
431 | }
|
432 |
|
433 | #else
|
434 |
|
435 | /* No free_history_entry function; free everything manually. */
|
436 |
|
437 | static void
|
438 | _py_free_history_entry(HIST_ENTRY *entry)
|
439 | {
|
440 | if (entry->line)
|
441 | free((void *)entry->line);
|
442 | if (entry->data)
|
443 | free(entry->data);
|
444 | free(entry);
|
445 | }
|
446 |
|
447 | #endif
|
448 |
|
449 | static PyObject *
|
450 | py_remove_history(PyObject *self, PyObject *args)
|
451 | {
|
452 | int entry_number;
|
453 | HIST_ENTRY *entry;
|
454 |
|
455 | if (!PyArg_ParseTuple(args, "i:remove_history_item", &entry_number))
|
456 | return NULL;
|
457 | if (entry_number < 0) {
|
458 | PyErr_SetString(PyExc_ValueError,
|
459 | "History index cannot be negative");
|
460 | return NULL;
|
461 | }
|
462 | entry = remove_history(entry_number);
|
463 | if (!entry) {
|
464 | PyErr_Format(PyExc_ValueError,
|
465 | "No history item at position %d",
|
466 | entry_number);
|
467 | return NULL;
|
468 | }
|
469 | /* free memory allocated for the history entry */
|
470 | _py_free_history_entry(entry);
|
471 | Py_RETURN_NONE;
|
472 | }
|
473 |
|
474 | PyDoc_STRVAR(doc_remove_history,
|
475 | "remove_history_item(pos) -> None\n\
|
476 | remove history item given by its position");
|
477 |
|
478 | static PyObject *
|
479 | py_replace_history(PyObject *self, PyObject *args)
|
480 | {
|
481 | int entry_number;
|
482 | char *line;
|
483 | HIST_ENTRY *old_entry;
|
484 |
|
485 | if (!PyArg_ParseTuple(args, "is:replace_history_item", &entry_number,
|
486 | &line)) {
|
487 | return NULL;
|
488 | }
|
489 | if (entry_number < 0) {
|
490 | PyErr_SetString(PyExc_ValueError,
|
491 | "History index cannot be negative");
|
492 | return NULL;
|
493 | }
|
494 | old_entry = replace_history_entry(entry_number, line, (void *)NULL);
|
495 | if (!old_entry) {
|
496 | PyErr_Format(PyExc_ValueError,
|
497 | "No history item at position %d",
|
498 | entry_number);
|
499 | return NULL;
|
500 | }
|
501 | /* free memory allocated for the old history entry */
|
502 | _py_free_history_entry(old_entry);
|
503 | Py_RETURN_NONE;
|
504 | }
|
505 |
|
506 | PyDoc_STRVAR(doc_replace_history,
|
507 | "replace_history_item(pos, line) -> None\n\
|
508 | replaces history item given by its position with contents of line");
|
509 |
|
510 | /* Add a line to the history buffer */
|
511 |
|
512 | static PyObject *
|
513 | py_add_history(PyObject *self, PyObject *args)
|
514 | {
|
515 | char *line;
|
516 |
|
517 | if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
|
518 | return NULL;
|
519 | }
|
520 | add_history(line);
|
521 | Py_RETURN_NONE;
|
522 | }
|
523 |
|
524 | PyDoc_STRVAR(doc_add_history,
|
525 | "add_history(string) -> None\n\
|
526 | add an item to the history buffer");
|
527 |
|
528 |
|
529 | /* Get the tab-completion word-delimiters that readline uses */
|
530 |
|
531 | static PyObject *
|
532 | get_completer_delims(PyObject *self, PyObject *noarg)
|
533 | {
|
534 | return PyString_FromString(rl_completer_word_break_characters);
|
535 | }
|
536 |
|
537 | PyDoc_STRVAR(doc_get_completer_delims,
|
538 | "get_completer_delims() -> string\n\
|
539 | get the word delimiters for completion");
|
540 |
|
541 |
|
542 | /* Set the completer function */
|
543 |
|
544 | static PyObject *
|
545 | set_completer(PyObject *self, PyObject *args)
|
546 | {
|
547 | return set_hook("completer", &completer, args);
|
548 | }
|
549 |
|
550 | PyDoc_STRVAR(doc_set_completer,
|
551 | "set_completer([function]) -> None\n\
|
552 | Set or remove the completer function.\n\
|
553 | The function is called as function(text, state),\n\
|
554 | for state in 0, 1, 2, ..., until it returns a non-string.\n\
|
555 | It should return the next possible completion starting with 'text'.");
|
556 |
|
557 |
|
558 | static PyObject *
|
559 | get_completer(PyObject *self, PyObject *noargs)
|
560 | {
|
561 | if (completer == NULL) {
|
562 | Py_RETURN_NONE;
|
563 | }
|
564 | Py_INCREF(completer);
|
565 | return completer;
|
566 | }
|
567 |
|
568 | PyDoc_STRVAR(doc_get_completer,
|
569 | "get_completer() -> function\n\
|
570 | \n\
|
571 | Returns current completer function.");
|
572 |
|
573 | /* Private function to get current length of history. XXX It may be
|
574 | * possible to replace this with a direct use of history_length instead,
|
575 | * but it's not clear whether BSD's libedit keeps history_length up to date.
|
576 | * See issue #8065.*/
|
577 |
|
578 | static int
|
579 | _py_get_history_length(void)
|
580 | {
|
581 | HISTORY_STATE *hist_st = history_get_history_state();
|
582 | int length = hist_st->length;
|
583 | /* the history docs don't say so, but the address of hist_st changes each
|
584 | time history_get_history_state is called which makes me think it's
|
585 | freshly malloc'd memory... on the other hand, the address of the last
|
586 | line stays the same as long as history isn't extended, so it appears to
|
587 | be malloc'd but managed by the history package... */
|
588 | free(hist_st);
|
589 | return length;
|
590 | }
|
591 |
|
592 | /* Exported function to get any element of history */
|
593 |
|
594 | static PyObject *
|
595 | get_history_item(PyObject *self, PyObject *args)
|
596 | {
|
597 | int idx = 0;
|
598 | HIST_ENTRY *hist_ent;
|
599 |
|
600 | if (!PyArg_ParseTuple(args, "i:get_history_item", &idx))
|
601 | return NULL;
|
602 | #ifdef __APPLE__
|
603 | if (using_libedit_emulation) {
|
604 | /* Older versions of libedit's readline emulation
|
605 | * use 0-based indexes, while readline and newer
|
606 | * versions of libedit use 1-based indexes.
|
607 | */
|
608 | int length = _py_get_history_length();
|
609 |
|
610 | idx = idx - 1 + libedit_history_start;
|
611 |
|
612 | /*
|
613 | * Apple's readline emulation crashes when
|
614 | * the index is out of range, therefore
|
615 | * test for that and fail gracefully.
|
616 | */
|
617 | if (idx < (0 + libedit_history_start)
|
618 | || idx >= (length + libedit_history_start)) {
|
619 | Py_RETURN_NONE;
|
620 | }
|
621 | }
|
622 | #endif /* __APPLE__ */
|
623 | if ((hist_ent = history_get(idx)))
|
624 | return PyString_FromString(hist_ent->line);
|
625 | else {
|
626 | Py_RETURN_NONE;
|
627 | }
|
628 | }
|
629 |
|
630 | PyDoc_STRVAR(doc_get_history_item,
|
631 | "get_history_item() -> string\n\
|
632 | return the current contents of history item at index.");
|
633 |
|
634 |
|
635 | /* Exported function to get current length of history */
|
636 |
|
637 | static PyObject *
|
638 | get_current_history_length(PyObject *self, PyObject *noarg)
|
639 | {
|
640 | return PyInt_FromLong((long)_py_get_history_length());
|
641 | }
|
642 |
|
643 | PyDoc_STRVAR(doc_get_current_history_length,
|
644 | "get_current_history_length() -> integer\n\
|
645 | return the current (not the maximum) length of history.");
|
646 |
|
647 |
|
648 | /* Exported function to read the current line buffer */
|
649 |
|
650 | static PyObject *
|
651 | get_line_buffer(PyObject *self, PyObject *noarg)
|
652 | {
|
653 | return PyString_FromString(rl_line_buffer);
|
654 | }
|
655 |
|
656 | PyDoc_STRVAR(doc_get_line_buffer,
|
657 | "get_line_buffer() -> string\n\
|
658 | return the current contents of the line buffer.");
|
659 |
|
660 |
|
661 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
|
662 |
|
663 | /* Exported function to clear the current history */
|
664 |
|
665 | static PyObject *
|
666 | py_clear_history(PyObject *self, PyObject *noarg)
|
667 | {
|
668 | clear_history();
|
669 | Py_RETURN_NONE;
|
670 | }
|
671 |
|
672 | PyDoc_STRVAR(doc_clear_history,
|
673 | "clear_history() -> None\n\
|
674 | Clear the current readline history.");
|
675 | #endif
|
676 |
|
677 | /* Added for OSH. We need to call this in our SIGWINCH handler so global
|
678 | * variables in readline get updated. */
|
679 | static PyObject *
|
680 | py_resize_terminal(PyObject *self, PyObject *noarg)
|
681 | {
|
682 | rl_resize_terminal();
|
683 | Py_RETURN_NONE;
|
684 | }
|
685 |
|
686 | /* Exported function to insert text into the line buffer */
|
687 |
|
688 | static PyObject *
|
689 | insert_text(PyObject *self, PyObject *args)
|
690 | {
|
691 | char *s;
|
692 | if (!PyArg_ParseTuple(args, "s:insert_text", &s))
|
693 | return NULL;
|
694 | rl_insert_text(s);
|
695 | Py_RETURN_NONE;
|
696 | }
|
697 |
|
698 | PyDoc_STRVAR(doc_insert_text,
|
699 | "insert_text(string) -> None\n\
|
700 | Insert text into the line buffer at the cursor position.");
|
701 |
|
702 |
|
703 | /* Redisplay the line buffer */
|
704 |
|
705 | static PyObject *
|
706 | redisplay(PyObject *self, PyObject *noarg)
|
707 | {
|
708 | rl_redisplay();
|
709 | Py_RETURN_NONE;
|
710 | }
|
711 |
|
712 | PyDoc_STRVAR(doc_redisplay,
|
713 | "redisplay() -> None\n\
|
714 | Change what's displayed on the screen to reflect the current\n\
|
715 | contents of the line buffer.");
|
716 |
|
717 |
|
718 | /* Functions added to implement the 'bind' builtin in OSH */
|
719 |
|
720 | /* -x/-X command keymaps */
|
721 | static Keymap emacs_cmd_map;
|
722 | static Keymap vi_insert_cmd_map;
|
723 | static Keymap vi_movement_cmd_map;
|
724 |
|
725 | /*
|
726 | These forcibly cast between a Keymap* and a rl_command_func_t*. Readline
|
727 | uses an additional `.type` field to keep track of the pointer's true type.
|
728 | */
|
729 | #define RL_KEYMAP_TO_FUNCTION(data) (rl_command_func_t *)(data)
|
730 | #define RL_FUNCTION_TO_KEYMAP(map, key) (Keymap)(map[key].function)
|
731 |
|
732 | static void
|
733 | _init_command_maps(void)
|
734 | {
|
735 | emacs_cmd_map = rl_make_bare_keymap();
|
736 | vi_insert_cmd_map = rl_make_bare_keymap();
|
737 | vi_movement_cmd_map = rl_make_bare_keymap();
|
738 |
|
739 | /* Ensure that Esc- and Ctrl-X are also keymaps */
|
740 | emacs_cmd_map[CTRL('X')].type = ISKMAP;
|
741 | emacs_cmd_map[CTRL('X')].function = RL_KEYMAP_TO_FUNCTION(rl_make_bare_keymap());
|
742 | emacs_cmd_map[ESC].type = ISKMAP;
|
743 | emacs_cmd_map[ESC].function = RL_KEYMAP_TO_FUNCTION(rl_make_bare_keymap());
|
744 | }
|
745 |
|
746 | static Keymap
|
747 | _get_associated_cmd_map(Keymap kmap)
|
748 | {
|
749 | if (emacs_cmd_map == NULL)
|
750 | _init_command_maps();
|
751 |
|
752 | if (kmap == emacs_standard_keymap)
|
753 | return emacs_cmd_map;
|
754 | else if (kmap == vi_insertion_keymap)
|
755 | return vi_insert_cmd_map;
|
756 | else if (kmap == vi_movement_keymap)
|
757 | return vi_movement_cmd_map;
|
758 | else if (kmap == emacs_meta_keymap)
|
759 | return (RL_FUNCTION_TO_KEYMAP(emacs_cmd_map, ESC));
|
760 | else if (kmap == emacs_ctlx_keymap)
|
761 | return (RL_FUNCTION_TO_KEYMAP(emacs_cmd_map, CTRL('X')));
|
762 |
|
763 | return (Keymap) NULL;
|
764 | }
|
765 |
|
766 | /* List binding functions */
|
767 | static PyObject*
|
768 | list_funmap_names(PyObject *self, PyObject *args)
|
769 | {
|
770 | rl_list_funmap_names();
|
771 | // printf ("Compiled w/ readline version: %s\n", rl_library_version ? rl_library_version : "unknown");
|
772 | Py_RETURN_NONE;
|
773 | }
|
774 |
|
775 | PyDoc_STRVAR(doc_list_funmap_names,
|
776 | "list_funmap_names() -> None\n\
|
777 | Print all of the available readline functions.");
|
778 |
|
779 | /* Print readline functions and their bindings */
|
780 |
|
781 | static PyObject*
|
782 | function_dumper(PyObject *self, PyObject *args)
|
783 | {
|
784 | int print_readably;
|
785 |
|
786 | if (!PyArg_ParseTuple(args, "i:function_dumper", &print_readably))
|
787 | return NULL;
|
788 |
|
789 | rl_function_dumper(print_readably);
|
790 | Py_RETURN_NONE;
|
791 | }
|
792 |
|
793 | PyDoc_STRVAR(doc_list_function_dumper,
|
794 | "function_dumper(bool) -> None\n\
|
795 | Print all readline functions and their bindings.");
|
796 |
|
797 | /* Print macros, their bindings, and their string outputs */
|
798 |
|
799 | static PyObject*
|
800 | macro_dumper(PyObject *self, PyObject *args)
|
801 | {
|
802 | int print_readably;
|
803 |
|
804 | if (!PyArg_ParseTuple(args, "i:macro_dumper", &print_readably))
|
805 | return NULL;
|
806 |
|
807 | rl_macro_dumper(print_readably);
|
808 | Py_RETURN_NONE;
|
809 | }
|
810 |
|
811 | PyDoc_STRVAR(doc_list_macro_dumper,
|
812 | "macro_dumper(bool) -> None\n\
|
813 | Print all readline sequences bound to macros and the strings they output.");
|
814 |
|
815 | /* List readline variables */
|
816 |
|
817 | static PyObject*
|
818 | variable_dumper(PyObject *self, PyObject *args)
|
819 | {
|
820 | int print_readably;
|
821 |
|
822 | if (!PyArg_ParseTuple(args, "i:variable_dumper", &print_readably))
|
823 | return NULL;
|
824 |
|
825 | rl_variable_dumper(print_readably);
|
826 | Py_RETURN_NONE;
|
827 | }
|
828 |
|
829 | PyDoc_STRVAR(doc_list_variable_dumper,
|
830 | "variable_dumper(bool) -> None\n\
|
831 | List readline variables and their values.");
|
832 |
|
833 |
|
834 | /* Query bindings for a function name */
|
835 |
|
836 | // readline returns null-terminated string arrays
|
837 | void _strvec_dispose(char **strvec) {
|
838 | register int i;
|
839 |
|
840 | if (strvec == NULL)
|
841 | return;
|
842 |
|
843 | for (i = 0; strvec[i]; i++) {
|
844 | free(strvec[i]);
|
845 | }
|
846 |
|
847 | free(strvec);
|
848 | }
|
849 |
|
850 | // Nicely prints a strvec with commas and an and
|
851 | // like '"foo", "bar", and "moop"'
|
852 | void _pprint_strvec_list(char **strvec) {
|
853 | int i;
|
854 |
|
855 | for (i = 0; strvec[i]; i++) {
|
856 | printf("\"%s\"", strvec[i]);
|
857 | if (strvec[i + 1]) {
|
858 | printf(", ");
|
859 | if (!strvec[i + 2])
|
860 | printf("and ");
|
861 | }
|
862 | }
|
863 | }
|
864 |
|
865 | /*
|
866 | NB: readline (and bash) have a bug where they don't see certain keyseqs, even
|
867 | if the bindings work. E.g., if you bind a number key like "\C-7", it will be
|
868 | bound, but reporting code like query_bindings and function_dumper won't count it.
|
869 | */
|
870 |
|
871 | static PyObject*
|
872 | query_bindings(PyObject *self, PyObject *args)
|
873 | {
|
874 | char *fn_name;
|
875 | rl_command_func_t *cmd_fn;
|
876 | char **key_seqs;
|
877 |
|
878 | if (!PyArg_ParseTuple(args, "s:query_bindings", &fn_name))
|
879 | return NULL;
|
880 |
|
881 | cmd_fn = rl_named_function(fn_name);
|
882 |
|
883 | if (cmd_fn == NULL) {
|
884 | PyErr_Format(PyExc_ValueError, "`%s': unknown function name", fn_name);
|
885 | return NULL;
|
886 | }
|
887 |
|
888 | key_seqs = rl_invoking_keyseqs(cmd_fn);
|
889 |
|
890 | if (!key_seqs) {
|
891 | // print to stdout, but return an error
|
892 | printf("%s is not bound to any keys.\n", fn_name);
|
893 | PyErr_SetNone(PyExc_ValueError);
|
894 | return NULL;
|
895 | }
|
896 |
|
897 | printf("%s can be invoked via ", fn_name);
|
898 | _pprint_strvec_list(key_seqs);
|
899 | printf(".\n");
|
900 |
|
901 | _strvec_dispose(key_seqs);
|
902 |
|
903 | Py_RETURN_NONE;
|
904 | }
|
905 |
|
906 | PyDoc_STRVAR(doc_query_bindings,
|
907 | "query_bindings(str) -> None\n\
|
908 | Query bindings to see what's bound to a given function.");
|
909 |
|
910 |
|
911 | static PyObject*
|
912 | unbind_rl_function(PyObject *self, PyObject *args)
|
913 | {
|
914 | char *fn_name;
|
915 | rl_command_func_t *cmd_fn;
|
916 |
|
917 | if (!PyArg_ParseTuple(args, "s:unbind_rl_function", &fn_name))
|
918 | return NULL;
|
919 |
|
920 | cmd_fn = rl_named_function(fn_name);
|
921 | if (cmd_fn == NULL) {
|
922 | PyErr_Format(PyExc_ValueError, "`%s': unknown function name", fn_name);
|
923 | return NULL;
|
924 | }
|
925 |
|
926 | rl_unbind_function_in_map(cmd_fn, rl_get_keymap());
|
927 | Py_RETURN_NONE;
|
928 | }
|
929 |
|
930 | PyDoc_STRVAR(doc_unbind_rl_function,
|
931 | "unbind_rl_function(function_name) -> None\n\
|
932 | Unbind all keys bound to the named readline function in the current keymap.");
|
933 |
|
934 |
|
935 | static PyObject*
|
936 | unbind_shell_cmd(PyObject *self, PyObject *args)
|
937 | {
|
938 | char *keyseq;
|
939 | Keymap cmd_map;
|
940 |
|
941 | if (!PyArg_ParseTuple(args, "s:unbind_shell_cmd", &keyseq))
|
942 | return NULL;
|
943 |
|
944 | cmd_map = _get_associated_cmd_map(rl_get_keymap());
|
945 | if (cmd_map == NULL) {
|
946 | PyErr_SetString(PyExc_ValueError, "Could not get command map for current keymap");
|
947 | return NULL;
|
948 | }
|
949 |
|
950 | if (rl_bind_keyseq_in_map(keyseq, (rl_command_func_t *)NULL, cmd_map) != 0) {
|
951 | PyErr_Format(PyExc_ValueError, "'%s': can't unbind from shell command keymap", keyseq);
|
952 | return NULL;
|
953 | }
|
954 |
|
955 | Py_RETURN_NONE;
|
956 | }
|
957 |
|
958 | PyDoc_STRVAR(doc_unbind_shell_cmd,
|
959 | "unbind_shell_cmd(key_sequence) -> None\n\
|
960 | Unbind a key sequence from the current keymap's associated shell command map.");
|
961 |
|
962 |
|
963 | static PyObject*
|
964 | print_shell_cmd_map(PyObject *self, PyObject *noarg)
|
965 | {
|
966 | Keymap curr_map, cmd_map;
|
967 |
|
968 | curr_map = rl_get_keymap();
|
969 | cmd_map = _get_associated_cmd_map(curr_map);
|
970 |
|
971 | if (cmd_map == NULL) {
|
972 | PyErr_SetString(PyExc_ValueError, "Could not get shell command map for current keymap");
|
973 | return NULL;
|
974 | }
|
975 |
|
976 | rl_set_keymap(cmd_map);
|
977 | rl_macro_dumper(1);
|
978 | rl_set_keymap(curr_map);
|
979 |
|
980 | Py_RETURN_NONE;
|
981 | }
|
982 |
|
983 | PyDoc_STRVAR(doc_print_shell_cmd_map,
|
984 | "print_shell_cmd_map() -> None\n\
|
985 | Print all bindings for shell commands in the current keymap.");
|
986 |
|
987 |
|
988 | /* Remove all bindings for a given keyseq */
|
989 |
|
990 | static PyObject*
|
991 | unbind_keyseq(PyObject *self, PyObject *args)
|
992 | {
|
993 | /* Disabled because of rl_function_of_keyseq_len() error */
|
994 | Py_RETURN_NONE;
|
995 | #if 0
|
996 | char *seq, *keyseq;
|
997 | int kslen, type;
|
998 | rl_command_func_t *fn;
|
999 |
|
1000 | if (!PyArg_ParseTuple(args, "s:unbind_keyseq", &seq))
|
1001 | return NULL;
|
1002 |
|
1003 | keyseq = (char *)malloc((2 * strlen(seq)) + 1);
|
1004 | if (rl_translate_keyseq(seq, keyseq, &kslen) != 0) {
|
1005 | free(keyseq);
|
1006 | PyErr_Format(PyExc_ValueError, "'%s': cannot translate key sequence", seq);
|
1007 | return NULL;
|
1008 | }
|
1009 |
|
1010 | fn = rl_function_of_keyseq_len(keyseq, kslen, (Keymap)NULL, &type);
|
1011 | if (!fn) {
|
1012 | free(keyseq);
|
1013 | Py_RETURN_NONE;
|
1014 | }
|
1015 |
|
1016 | if (type == ISKMAP) {
|
1017 | fn = ((Keymap)fn)[ANYOTHERKEY].function;
|
1018 | }
|
1019 |
|
1020 | if (rl_bind_keyseq(seq, (rl_command_func_t *)NULL) != 0) {
|
1021 | free(keyseq);
|
1022 | PyErr_Format(PyExc_ValueError, "'%s': cannot unbind", seq);
|
1023 | return NULL;
|
1024 | }
|
1025 |
|
1026 | /*
|
1027 | TODO: Handle shell command unbinding if f == bash_execute_unix_command or
|
1028 | rather, whatever the osh equivalent will be
|
1029 | */
|
1030 |
|
1031 | free(keyseq);
|
1032 | Py_RETURN_NONE;
|
1033 | #endif
|
1034 | }
|
1035 |
|
1036 | PyDoc_STRVAR(doc_unbind_keyseq,
|
1037 | "unbind_keyseq(sequence) -> None\n\
|
1038 | Unbind a key sequence from the current keymap.");
|
1039 |
|
1040 |
|
1041 | /* Keymap toggling code */
|
1042 | static Keymap orig_keymap = NULL;
|
1043 |
|
1044 | static PyObject*
|
1045 | use_temp_keymap(PyObject *self, PyObject *args)
|
1046 | {
|
1047 | char *keymap_name;
|
1048 | Keymap new_keymap;
|
1049 |
|
1050 | if (!PyArg_ParseTuple(args, "s:use_temp_keymap", &keymap_name))
|
1051 | return NULL;
|
1052 |
|
1053 | new_keymap = rl_get_keymap_by_name(keymap_name);
|
1054 | if (new_keymap == NULL) {
|
1055 | PyErr_Format(PyExc_ValueError, "`%s': unknown keymap name", keymap_name);
|
1056 | return NULL;
|
1057 | }
|
1058 |
|
1059 | orig_keymap = rl_get_keymap();
|
1060 | rl_set_keymap(new_keymap);
|
1061 |
|
1062 | Py_RETURN_NONE;
|
1063 | }
|
1064 |
|
1065 | PyDoc_STRVAR(doc_use_temp_keymap,
|
1066 | "use_temp_keymap(keymap_name) -> None\n\
|
1067 | Temporarily switch to named keymap, saving the current one.");
|
1068 |
|
1069 | static PyObject*
|
1070 | restore_orig_keymap(PyObject *self, PyObject *args)
|
1071 | {
|
1072 | if (orig_keymap != NULL) {
|
1073 | rl_set_keymap(orig_keymap);
|
1074 | orig_keymap = NULL;
|
1075 | }
|
1076 |
|
1077 | Py_RETURN_NONE;
|
1078 | }
|
1079 |
|
1080 | PyDoc_STRVAR(doc_restore_orig_keymap,
|
1081 | "restore_orig_keymap() -> None\n\
|
1082 | Restore the previously saved keymap if one exists.");
|
1083 |
|
1084 | /* Table of functions exported by the module */
|
1085 |
|
1086 | #ifdef OVM_MAIN
|
1087 | #include "pyext/line_input.c/readline_methods.def"
|
1088 | #else
|
1089 | static struct PyMethodDef readline_methods[] = {
|
1090 | {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
|
1091 | {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
|
1092 | {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
|
1093 | {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
|
1094 | {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
|
1095 | {"read_history_file", read_history_file,
|
1096 | METH_VARARGS, doc_read_history_file},
|
1097 | {"write_history_file", write_history_file,
|
1098 | METH_VARARGS, doc_write_history_file},
|
1099 | {"get_history_item", get_history_item,
|
1100 | METH_VARARGS, doc_get_history_item},
|
1101 | {"get_current_history_length", (PyCFunction)get_current_history_length,
|
1102 | METH_NOARGS, doc_get_current_history_length},
|
1103 | {"set_history_length", set_history_length,
|
1104 | METH_VARARGS, set_history_length_doc},
|
1105 | {"get_history_length", get_history_length,
|
1106 | METH_NOARGS, get_history_length_doc},
|
1107 | {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
|
1108 | {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
|
1109 | {"get_completion_type", get_completion_type,
|
1110 | METH_NOARGS, doc_get_completion_type},
|
1111 | {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
|
1112 | {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
|
1113 |
|
1114 | {"set_completer_delims", set_completer_delims,
|
1115 | METH_VARARGS, doc_set_completer_delims},
|
1116 | {"add_history", py_add_history, METH_VARARGS, doc_add_history},
|
1117 | {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
|
1118 | {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
|
1119 | {"get_completer_delims", get_completer_delims,
|
1120 | METH_NOARGS, doc_get_completer_delims},
|
1121 |
|
1122 | {"set_completion_display_matches_hook", set_completion_display_matches_hook,
|
1123 | METH_VARARGS, doc_set_completion_display_matches_hook},
|
1124 | {"set_startup_hook", set_startup_hook,
|
1125 | METH_VARARGS, doc_set_startup_hook},
|
1126 | {"set_pre_input_hook", set_pre_input_hook,
|
1127 | METH_VARARGS, doc_set_pre_input_hook},
|
1128 | {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
|
1129 | {"resize_terminal", py_resize_terminal, METH_NOARGS, ""},
|
1130 |
|
1131 | /* Functions added to implement the 'bind' builtin in OSH */
|
1132 | {"list_funmap_names", list_funmap_names, METH_NOARGS, doc_list_funmap_names},
|
1133 | {"function_dumper", function_dumper, METH_VARARGS, doc_list_function_dumper},
|
1134 | {"macro_dumper", macro_dumper, METH_VARARGS, doc_list_macro_dumper},
|
1135 | {"variable_dumper", variable_dumper, METH_VARARGS, doc_list_variable_dumper},
|
1136 | {"query_bindings", query_bindings, METH_VARARGS, doc_query_bindings},
|
1137 | {"unbind_rl_function", unbind_rl_function, METH_VARARGS, doc_unbind_rl_function},
|
1138 | {"use_temp_keymap", use_temp_keymap, METH_VARARGS, doc_use_temp_keymap},
|
1139 | {"restore_orig_keymap", restore_orig_keymap, METH_NOARGS, doc_restore_orig_keymap},
|
1140 | {"unbind_shell_cmd", unbind_shell_cmd, METH_VARARGS, doc_unbind_shell_cmd},
|
1141 | {"print_shell_cmd_map", print_shell_cmd_map, METH_NOARGS, doc_print_shell_cmd_map},
|
1142 | {"unbind_keyseq", unbind_keyseq, METH_VARARGS, doc_unbind_keyseq},
|
1143 | {0, 0}
|
1144 | };
|
1145 | #endif
|
1146 |
|
1147 |
|
1148 | /* C function to call the Python hooks. */
|
1149 |
|
1150 | static int
|
1151 | on_hook(PyObject *func)
|
1152 | {
|
1153 | int result = 0;
|
1154 | if (func != NULL) {
|
1155 | PyObject *r;
|
1156 | #ifdef WITH_THREAD
|
1157 | PyGILState_STATE gilstate = PyGILState_Ensure();
|
1158 | #endif
|
1159 | r = PyObject_CallFunction(func, NULL);
|
1160 | if (r == NULL)
|
1161 | goto error;
|
1162 | if (r == Py_None)
|
1163 | result = 0;
|
1164 | else {
|
1165 | result = PyInt_AsLong(r);
|
1166 | if (result == -1 && PyErr_Occurred())
|
1167 | goto error;
|
1168 | }
|
1169 | Py_DECREF(r);
|
1170 | goto done;
|
1171 | error:
|
1172 | PyErr_Clear();
|
1173 | Py_XDECREF(r);
|
1174 | done:
|
1175 | #ifdef WITH_THREAD
|
1176 | PyGILState_Release(gilstate);
|
1177 | #endif
|
1178 | return result;
|
1179 | }
|
1180 | return result;
|
1181 | }
|
1182 |
|
1183 | static int
|
1184 | #if defined(_RL_FUNCTION_TYPEDEF)
|
1185 | on_startup_hook(void)
|
1186 | #else
|
1187 | on_startup_hook()
|
1188 | #endif
|
1189 | {
|
1190 | return on_hook(startup_hook);
|
1191 | }
|
1192 |
|
1193 | #ifdef HAVE_RL_PRE_INPUT_HOOK
|
1194 | static int
|
1195 | #if defined(_RL_FUNCTION_TYPEDEF)
|
1196 | on_pre_input_hook(void)
|
1197 | #else
|
1198 | on_pre_input_hook()
|
1199 | #endif
|
1200 | {
|
1201 | return on_hook(pre_input_hook);
|
1202 | }
|
1203 | #endif
|
1204 |
|
1205 |
|
1206 | /* C function to call the Python completion_display_matches */
|
1207 |
|
1208 | #ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
|
1209 | static void
|
1210 | on_completion_display_matches_hook(char **matches,
|
1211 | int num_matches, int max_length)
|
1212 | {
|
1213 | int i;
|
1214 | PyObject *m=NULL, *s=NULL, *r=NULL;
|
1215 | #ifdef WITH_THREAD
|
1216 | PyGILState_STATE gilstate = PyGILState_Ensure();
|
1217 | #endif
|
1218 | m = PyList_New(num_matches);
|
1219 | if (m == NULL)
|
1220 | goto error;
|
1221 | for (i = 0; i < num_matches; i++) {
|
1222 | s = PyString_FromString(matches[i+1]);
|
1223 | if (s == NULL)
|
1224 | goto error;
|
1225 | if (PyList_SetItem(m, i, s) == -1)
|
1226 | goto error;
|
1227 | }
|
1228 |
|
1229 | r = PyObject_CallFunction(completion_display_matches_hook,
|
1230 | "sOi", matches[0], m, max_length);
|
1231 |
|
1232 | Py_DECREF(m); m=NULL;
|
1233 |
|
1234 | if (r == NULL ||
|
1235 | (r != Py_None && PyInt_AsLong(r) == -1 && PyErr_Occurred())) {
|
1236 | goto error;
|
1237 | }
|
1238 | Py_XDECREF(r); r=NULL;
|
1239 |
|
1240 | if (0) {
|
1241 | error:
|
1242 | PyErr_Clear();
|
1243 | Py_XDECREF(m);
|
1244 | Py_XDECREF(r);
|
1245 | }
|
1246 | #ifdef WITH_THREAD
|
1247 | PyGILState_Release(gilstate);
|
1248 | #endif
|
1249 | }
|
1250 |
|
1251 | #endif
|
1252 |
|
1253 | #ifdef HAVE_RL_RESIZE_TERMINAL
|
1254 | static volatile sig_atomic_t sigwinch_received;
|
1255 | static PyOS_sighandler_t sigwinch_ohandler;
|
1256 |
|
1257 | static void
|
1258 | readline_sigwinch_handler(int signum)
|
1259 | {
|
1260 | sigwinch_received = 1;
|
1261 | if (sigwinch_ohandler &&
|
1262 | sigwinch_ohandler != SIG_IGN && sigwinch_ohandler != SIG_DFL)
|
1263 | sigwinch_ohandler(signum);
|
1264 | }
|
1265 | #endif
|
1266 |
|
1267 | /* C function to call the Python completer. */
|
1268 |
|
1269 | static char *
|
1270 | on_completion(const char *text, int state)
|
1271 | {
|
1272 | char *result = NULL;
|
1273 | if (completer != NULL) {
|
1274 | PyObject *r;
|
1275 | #ifdef WITH_THREAD
|
1276 | PyGILState_STATE gilstate = PyGILState_Ensure();
|
1277 | #endif
|
1278 | rl_attempted_completion_over = 1;
|
1279 | r = PyObject_CallFunction(completer, "si", text, state);
|
1280 | if (r == NULL)
|
1281 | goto error;
|
1282 | if (r == Py_None) {
|
1283 | result = NULL;
|
1284 | }
|
1285 | else {
|
1286 | char *s = PyString_AsString(r);
|
1287 | if (s == NULL)
|
1288 | goto error;
|
1289 | result = strdup(s);
|
1290 | }
|
1291 | Py_DECREF(r);
|
1292 | goto done;
|
1293 | error:
|
1294 | PyErr_Clear();
|
1295 | Py_XDECREF(r);
|
1296 | done:
|
1297 | #ifdef WITH_THREAD
|
1298 | PyGILState_Release(gilstate);
|
1299 | #endif
|
1300 | return result;
|
1301 | }
|
1302 | return result;
|
1303 | }
|
1304 |
|
1305 |
|
1306 | /* A more flexible constructor that saves the "begidx" and "endidx"
|
1307 | * before calling the normal completer */
|
1308 |
|
1309 | static char **
|
1310 | flex_complete(const char *text, int start, int end)
|
1311 | {
|
1312 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
|
1313 | rl_completion_append_character ='\0';
|
1314 | #endif
|
1315 | #ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
|
1316 | rl_completion_suppress_append = 0;
|
1317 | #endif
|
1318 | Py_XDECREF(begidx);
|
1319 | Py_XDECREF(endidx);
|
1320 | begidx = PyInt_FromLong((long) start);
|
1321 | endidx = PyInt_FromLong((long) end);
|
1322 | return completion_matches(text, *on_completion);
|
1323 | }
|
1324 |
|
1325 |
|
1326 | /* Helper to initialize GNU readline properly. */
|
1327 |
|
1328 | static void
|
1329 | setup_readline(void)
|
1330 | {
|
1331 | #ifdef SAVE_LOCALE
|
1332 | char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
|
1333 | if (!saved_locale)
|
1334 | Py_FatalError("not enough memory to save locale");
|
1335 | #endif
|
1336 |
|
1337 | #ifdef __APPLE__
|
1338 | /* the libedit readline emulation resets key bindings etc
|
1339 | * when calling rl_initialize. So call it upfront
|
1340 | */
|
1341 | if (using_libedit_emulation)
|
1342 | rl_initialize();
|
1343 |
|
1344 | /* Detect if libedit's readline emulation uses 0-based
|
1345 | * indexing or 1-based indexing.
|
1346 | */
|
1347 | add_history("1");
|
1348 | if (history_get(1) == NULL) {
|
1349 | libedit_history_start = 0;
|
1350 | } else {
|
1351 | libedit_history_start = 1;
|
1352 | }
|
1353 | clear_history();
|
1354 | #endif /* __APPLE__ */
|
1355 |
|
1356 | using_history();
|
1357 |
|
1358 | rl_readline_name = "oils";
|
1359 | #if defined(PYOS_OS2) && defined(PYCC_GCC)
|
1360 | /* Allow $if term= in .inputrc to work */
|
1361 | rl_terminal_name = getenv("TERM");
|
1362 | #endif
|
1363 | /* Force rebind of TAB to insert-tab */
|
1364 | rl_bind_key('\t', rl_insert);
|
1365 | /* Bind both ESC-TAB and ESC-ESC to the completion function */
|
1366 | rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
|
1367 | rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
|
1368 | #ifdef HAVE_RL_RESIZE_TERMINAL
|
1369 | /* Set up signal handler for window resize */
|
1370 | sigwinch_ohandler = PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
|
1371 | #endif
|
1372 | /* Set our hook functions */
|
1373 | rl_startup_hook = on_startup_hook;
|
1374 | #ifdef HAVE_RL_PRE_INPUT_HOOK
|
1375 | rl_pre_input_hook = on_pre_input_hook;
|
1376 | #endif
|
1377 | /* Set our completion function */
|
1378 | rl_attempted_completion_function = flex_complete;
|
1379 | /* Set Python word break characters */
|
1380 | completer_word_break_characters =
|
1381 | rl_completer_word_break_characters =
|
1382 | strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
|
1383 | /* All nonalphanums except '.' */
|
1384 |
|
1385 | begidx = PyInt_FromLong(0L);
|
1386 | endidx = PyInt_FromLong(0L);
|
1387 |
|
1388 | #ifdef __APPLE__
|
1389 | if (!using_libedit_emulation)
|
1390 | #endif
|
1391 | {
|
1392 | if (!isatty(STDOUT_FILENO)) {
|
1393 | /* Issue #19884: stdout is not a terminal. Disable meta modifier
|
1394 | keys to not write the ANSI sequence "\033[1034h" into stdout. On
|
1395 | terminals supporting 8 bit characters like TERM=xterm-256color
|
1396 | (which is now the default Fedora since Fedora 18), the meta key is
|
1397 | used to enable support of 8 bit characters (ANSI sequence
|
1398 | "\033[1034h").
|
1399 |
|
1400 | With libedit, this call makes readline() crash. */
|
1401 | rl_variable_bind ("enable-meta-key", "off");
|
1402 | }
|
1403 | }
|
1404 |
|
1405 | /* Initialize (allows .inputrc to override)
|
1406 | *
|
1407 | * XXX: A bug in the readline-2.2 library causes a memory leak
|
1408 | * inside this function. Nothing we can do about it.
|
1409 | */
|
1410 | #ifdef __APPLE__
|
1411 | if (using_libedit_emulation)
|
1412 | rl_read_init_file(NULL);
|
1413 | else
|
1414 | #endif /* __APPLE__ */
|
1415 | rl_initialize();
|
1416 |
|
1417 | RESTORE_LOCALE(saved_locale)
|
1418 | }
|
1419 |
|
1420 | /* Wrapper around GNU readline that handles signals differently. */
|
1421 |
|
1422 |
|
1423 | #if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
|
1424 |
|
1425 | static char *completed_input_string;
|
1426 | static void
|
1427 | rlhandler(char *text)
|
1428 | {
|
1429 | completed_input_string = text;
|
1430 | rl_callback_handler_remove();
|
1431 | }
|
1432 |
|
1433 | static char *
|
1434 | readline_until_enter_or_signal(char *prompt, int *signal)
|
1435 | {
|
1436 | char * not_done_reading = "";
|
1437 | fd_set selectset;
|
1438 |
|
1439 | *signal = 0;
|
1440 | #ifdef HAVE_RL_CATCH_SIGNAL
|
1441 | rl_catch_signals = 0;
|
1442 | #endif
|
1443 | /* OVM_MAIN: Oil is handling SIGWINCH, so readline shouldn't handle it.
|
1444 | * Without this line, strace reveals that GNU readline is constantly
|
1445 | * turning it on and off.
|
1446 | * */
|
1447 | rl_catch_sigwinch = 0;
|
1448 |
|
1449 | rl_callback_handler_install (prompt, rlhandler);
|
1450 | FD_ZERO(&selectset);
|
1451 |
|
1452 | completed_input_string = not_done_reading;
|
1453 |
|
1454 | while (completed_input_string == not_done_reading) {
|
1455 | int has_input = 0;
|
1456 |
|
1457 | while (!has_input)
|
1458 | { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
|
1459 |
|
1460 | /* [Bug #1552726] Only limit the pause if an input hook has been
|
1461 | defined. */
|
1462 | struct timeval *timeoutp = NULL;
|
1463 | if (PyOS_InputHook)
|
1464 | timeoutp = &timeout;
|
1465 | #ifdef HAVE_RL_RESIZE_TERMINAL
|
1466 | /* Update readline's view of the window size after SIGWINCH */
|
1467 | if (sigwinch_received) {
|
1468 | sigwinch_received = 0;
|
1469 | rl_resize_terminal();
|
1470 | }
|
1471 | #endif
|
1472 | FD_SET(fileno(rl_instream), &selectset);
|
1473 | /* select resets selectset if no input was available */
|
1474 | has_input = select(fileno(rl_instream) + 1, &selectset,
|
1475 | NULL, NULL, timeoutp);
|
1476 | if(PyOS_InputHook) PyOS_InputHook();
|
1477 | }
|
1478 |
|
1479 | if(has_input > 0) {
|
1480 | rl_callback_read_char();
|
1481 | }
|
1482 | else if (errno == EINTR) {
|
1483 | int s;
|
1484 | #ifdef WITH_THREAD
|
1485 | PyEval_RestoreThread(_PyOS_ReadlineTState);
|
1486 | #endif
|
1487 | s = PyErr_CheckSignals();
|
1488 | #ifdef WITH_THREAD
|
1489 | PyEval_SaveThread();
|
1490 | #endif
|
1491 | if (s < 0) {
|
1492 | rl_free_line_state();
|
1493 | #if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0700
|
1494 | rl_callback_sigcleanup();
|
1495 | #endif
|
1496 | rl_cleanup_after_signal();
|
1497 | rl_callback_handler_remove();
|
1498 | *signal = 1;
|
1499 | completed_input_string = NULL;
|
1500 | }
|
1501 | }
|
1502 | }
|
1503 |
|
1504 | return completed_input_string;
|
1505 | }
|
1506 |
|
1507 |
|
1508 | #else
|
1509 |
|
1510 | /* Interrupt handler */
|
1511 |
|
1512 | static jmp_buf jbuf;
|
1513 |
|
1514 | /* ARGSUSED */
|
1515 | static void
|
1516 | onintr(int sig)
|
1517 | {
|
1518 | longjmp(jbuf, 1);
|
1519 | }
|
1520 |
|
1521 |
|
1522 | static char *
|
1523 | readline_until_enter_or_signal(char *prompt, int *signal)
|
1524 | {
|
1525 | PyOS_sighandler_t old_inthandler;
|
1526 | char *p;
|
1527 |
|
1528 | *signal = 0;
|
1529 |
|
1530 | old_inthandler = PyOS_setsig(SIGINT, onintr);
|
1531 | if (setjmp(jbuf)) {
|
1532 | #ifdef HAVE_SIGRELSE
|
1533 | /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
|
1534 | sigrelse(SIGINT);
|
1535 | #endif
|
1536 | PyOS_setsig(SIGINT, old_inthandler);
|
1537 | *signal = 1;
|
1538 | return NULL;
|
1539 | }
|
1540 | rl_event_hook = PyOS_InputHook;
|
1541 | p = readline(prompt);
|
1542 | PyOS_setsig(SIGINT, old_inthandler);
|
1543 |
|
1544 | return p;
|
1545 | }
|
1546 | #endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
|
1547 |
|
1548 |
|
1549 | static char *
|
1550 | call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
|
1551 | {
|
1552 | size_t n;
|
1553 | char *p, *q;
|
1554 | int signal;
|
1555 |
|
1556 | #ifdef SAVE_LOCALE
|
1557 | char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
|
1558 | if (!saved_locale)
|
1559 | Py_FatalError("not enough memory to save locale");
|
1560 | setlocale(LC_CTYPE, "");
|
1561 | #endif
|
1562 |
|
1563 | if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
|
1564 | rl_instream = sys_stdin;
|
1565 | rl_outstream = sys_stdout;
|
1566 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
|
1567 | rl_prep_terminal (1);
|
1568 | #endif
|
1569 | }
|
1570 |
|
1571 | p = readline_until_enter_or_signal(prompt, &signal);
|
1572 |
|
1573 | /* we got an interrupt signal */
|
1574 | if (signal) {
|
1575 | RESTORE_LOCALE(saved_locale)
|
1576 | return NULL;
|
1577 | }
|
1578 |
|
1579 | /* We got an EOF, return an empty string. */
|
1580 | if (p == NULL) {
|
1581 | p = PyMem_Malloc(1);
|
1582 | if (p != NULL)
|
1583 | *p = '\0';
|
1584 | RESTORE_LOCALE(saved_locale)
|
1585 | return p;
|
1586 | }
|
1587 |
|
1588 | /* we have a valid line */
|
1589 | n = strlen(p);
|
1590 | /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
|
1591 | release the original. */
|
1592 | q = p;
|
1593 | p = PyMem_Malloc(n+2);
|
1594 | if (p != NULL) {
|
1595 | strncpy(p, q, n);
|
1596 | p[n] = '\n';
|
1597 | p[n+1] = '\0';
|
1598 | }
|
1599 | free(q);
|
1600 | RESTORE_LOCALE(saved_locale)
|
1601 | return p;
|
1602 | }
|
1603 |
|
1604 |
|
1605 | /* Initialize the module */
|
1606 |
|
1607 | PyDoc_STRVAR(doc_module,
|
1608 | "Importing this module enables command line editing using GNU readline.");
|
1609 |
|
1610 | #ifdef __APPLE__
|
1611 | PyDoc_STRVAR(doc_module_le,
|
1612 | "Importing this module enables command line editing using libedit readline.");
|
1613 | #endif /* __APPLE__ */
|
1614 |
|
1615 | PyMODINIT_FUNC
|
1616 | initline_input(void)
|
1617 | {
|
1618 | PyObject *m;
|
1619 |
|
1620 | #ifdef __APPLE__
|
1621 | if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
|
1622 | using_libedit_emulation = 1;
|
1623 | }
|
1624 |
|
1625 | if (using_libedit_emulation)
|
1626 | m = Py_InitModule4("line_input", readline_methods, doc_module_le,
|
1627 | (PyObject *)NULL, PYTHON_API_VERSION);
|
1628 | else
|
1629 |
|
1630 | #endif /* __APPLE__ */
|
1631 |
|
1632 | m = Py_InitModule4("line_input", readline_methods, doc_module,
|
1633 | (PyObject *)NULL, PYTHON_API_VERSION);
|
1634 | if (m == NULL)
|
1635 | return;
|
1636 |
|
1637 | PyOS_ReadlineFunctionPointer = call_readline;
|
1638 | setup_readline();
|
1639 |
|
1640 | PyModule_AddIntConstant(m, "_READLINE_VERSION", RL_READLINE_VERSION);
|
1641 | PyModule_AddIntConstant(m, "_READLINE_RUNTIME_VERSION", rl_readline_version);
|
1642 | }
|