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 | if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
|
119 | return NULL;
|
120 | /* Make a copy -- rl_parse_and_bind() modifies its argument */
|
121 | /* Bernard Herzog */
|
122 | copy = malloc(1 + strlen(s));
|
123 | if (copy == NULL)
|
124 | return PyErr_NoMemory();
|
125 | strcpy(copy, s);
|
126 | rl_parse_and_bind(copy);
|
127 | free(copy); /* Free the copy */
|
128 | Py_RETURN_NONE;
|
129 | }
|
130 |
|
131 | PyDoc_STRVAR(doc_parse_and_bind,
|
132 | "parse_and_bind(string) -> None\n\
|
133 | Execute the init line provided in the string argument.");
|
134 |
|
135 |
|
136 | /* Exported function to parse a readline init file */
|
137 |
|
138 | static PyObject *
|
139 | read_init_file(PyObject *self, PyObject *args)
|
140 | {
|
141 | char *s = NULL;
|
142 | if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
|
143 | return NULL;
|
144 | errno = rl_read_init_file(s);
|
145 | if (errno)
|
146 | return PyErr_SetFromErrno(PyExc_IOError);
|
147 | Py_RETURN_NONE;
|
148 | }
|
149 |
|
150 | PyDoc_STRVAR(doc_read_init_file,
|
151 | "read_init_file([filename]) -> None\n\
|
152 | Execute a readline initialization file.\n\
|
153 | The default filename is the last filename used.");
|
154 |
|
155 |
|
156 | /* Exported function to load a readline history file */
|
157 |
|
158 | static PyObject *
|
159 | read_history_file(PyObject *self, PyObject *args)
|
160 | {
|
161 | char *s = NULL;
|
162 | if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
|
163 | return NULL;
|
164 | errno = read_history(s);
|
165 | if (errno)
|
166 | return PyErr_SetFromErrno(PyExc_IOError);
|
167 | Py_RETURN_NONE;
|
168 | }
|
169 |
|
170 | static int _history_length = -1; /* do not truncate history by default */
|
171 | PyDoc_STRVAR(doc_read_history_file,
|
172 | "read_history_file([filename]) -> None\n\
|
173 | Load a readline history file.\n\
|
174 | The default filename is ~/.history.");
|
175 |
|
176 |
|
177 | /* Exported function to save a readline history file */
|
178 |
|
179 | static PyObject *
|
180 | write_history_file(PyObject *self, PyObject *args)
|
181 | {
|
182 | char *s = NULL;
|
183 | if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
|
184 | return NULL;
|
185 | errno = write_history(s);
|
186 | if (!errno && _history_length >= 0)
|
187 | history_truncate_file(s, _history_length);
|
188 | if (errno)
|
189 | return PyErr_SetFromErrno(PyExc_IOError);
|
190 | Py_RETURN_NONE;
|
191 | }
|
192 |
|
193 | PyDoc_STRVAR(doc_write_history_file,
|
194 | "write_history_file([filename]) -> None\n\
|
195 | Save a readline history file.\n\
|
196 | The default filename is ~/.history.");
|
197 |
|
198 |
|
199 | /* Set history length */
|
200 |
|
201 | static PyObject*
|
202 | set_history_length(PyObject *self, PyObject *args)
|
203 | {
|
204 | int length = _history_length;
|
205 | if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
|
206 | return NULL;
|
207 | _history_length = length;
|
208 | Py_RETURN_NONE;
|
209 | }
|
210 |
|
211 | PyDoc_STRVAR(set_history_length_doc,
|
212 | "set_history_length(length) -> None\n\
|
213 | set the maximal number of lines which will be written to\n\
|
214 | the history file. A negative length is used to inhibit\n\
|
215 | history truncation.");
|
216 |
|
217 |
|
218 | /* Get history length */
|
219 |
|
220 | static PyObject*
|
221 | get_history_length(PyObject *self, PyObject *noarg)
|
222 | {
|
223 | return PyInt_FromLong(_history_length);
|
224 | }
|
225 |
|
226 | PyDoc_STRVAR(get_history_length_doc,
|
227 | "get_history_length() -> int\n\
|
228 | return the maximum number of lines that will be written to\n\
|
229 | the history file.");
|
230 |
|
231 |
|
232 | /* Generic hook function setter */
|
233 |
|
234 | static PyObject *
|
235 | set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
|
236 | {
|
237 | PyObject *function = Py_None;
|
238 | char buf[80];
|
239 | PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
|
240 | if (!PyArg_ParseTuple(args, buf, &function))
|
241 | return NULL;
|
242 | if (function == Py_None) {
|
243 | Py_CLEAR(*hook_var);
|
244 | }
|
245 | else if (PyCallable_Check(function)) {
|
246 | PyObject *tmp = *hook_var;
|
247 | Py_INCREF(function);
|
248 | *hook_var = function;
|
249 | Py_XDECREF(tmp);
|
250 | }
|
251 | else {
|
252 | PyOS_snprintf(buf, sizeof(buf),
|
253 | "set_%.50s(func): argument not callable",
|
254 | funcname);
|
255 | PyErr_SetString(PyExc_TypeError, buf);
|
256 | return NULL;
|
257 | }
|
258 | Py_RETURN_NONE;
|
259 | }
|
260 |
|
261 |
|
262 | /* Exported functions to specify hook functions in Python */
|
263 |
|
264 | static PyObject *completion_display_matches_hook = NULL;
|
265 | static PyObject *startup_hook = NULL;
|
266 |
|
267 | #ifdef HAVE_RL_PRE_INPUT_HOOK
|
268 | static PyObject *pre_input_hook = NULL;
|
269 | #endif
|
270 |
|
271 | static PyObject *
|
272 | set_completion_display_matches_hook(PyObject *self, PyObject *args)
|
273 | {
|
274 | PyObject *result = set_hook("completion_display_matches_hook",
|
275 | &completion_display_matches_hook, args);
|
276 | #ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
|
277 | /* We cannot set this hook globally, since it replaces the
|
278 | default completion display. */
|
279 | rl_completion_display_matches_hook =
|
280 | completion_display_matches_hook ?
|
281 | #if defined(_RL_FUNCTION_TYPEDEF)
|
282 | (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
|
283 | #else
|
284 | (VFunction *)on_completion_display_matches_hook : 0;
|
285 | #endif
|
286 | #endif
|
287 | return result;
|
288 |
|
289 | }
|
290 |
|
291 | PyDoc_STRVAR(doc_set_completion_display_matches_hook,
|
292 | "set_completion_display_matches_hook([function]) -> None\n\
|
293 | Set or remove the completion display function.\n\
|
294 | The function is called as\n\
|
295 | function(substitution, [matches], longest_match_length)\n\
|
296 | once each time matches need to be displayed.");
|
297 |
|
298 | static PyObject *
|
299 | set_startup_hook(PyObject *self, PyObject *args)
|
300 | {
|
301 | return set_hook("startup_hook", &startup_hook, args);
|
302 | }
|
303 |
|
304 | PyDoc_STRVAR(doc_set_startup_hook,
|
305 | "set_startup_hook([function]) -> None\n\
|
306 | Set or remove the function invoked by the rl_startup_hook callback.\n\
|
307 | The function is called with no arguments just\n\
|
308 | before readline prints the first prompt.");
|
309 |
|
310 |
|
311 | #ifdef HAVE_RL_PRE_INPUT_HOOK
|
312 |
|
313 | /* Set pre-input hook */
|
314 |
|
315 | static PyObject *
|
316 | set_pre_input_hook(PyObject *self, PyObject *args)
|
317 | {
|
318 | return set_hook("pre_input_hook", &pre_input_hook, args);
|
319 | }
|
320 |
|
321 | PyDoc_STRVAR(doc_set_pre_input_hook,
|
322 | "set_pre_input_hook([function]) -> None\n\
|
323 | Set or remove the function invoked by the rl_pre_input_hook callback.\n\
|
324 | The function is called with no arguments after the first prompt\n\
|
325 | has been printed and just before readline starts reading input\n\
|
326 | characters.");
|
327 |
|
328 | #endif
|
329 |
|
330 |
|
331 | /* Exported function to specify a word completer in Python */
|
332 |
|
333 | static PyObject *completer = NULL;
|
334 |
|
335 | static PyObject *begidx = NULL;
|
336 | static PyObject *endidx = NULL;
|
337 |
|
338 |
|
339 | /* Get the completion type for the scope of the tab-completion */
|
340 | static PyObject *
|
341 | get_completion_type(PyObject *self, PyObject *noarg)
|
342 | {
|
343 | return PyInt_FromLong(rl_completion_type);
|
344 | }
|
345 |
|
346 | PyDoc_STRVAR(doc_get_completion_type,
|
347 | "get_completion_type() -> int\n\
|
348 | Get the type of completion being attempted.");
|
349 |
|
350 |
|
351 | /* Get the beginning index for the scope of the tab-completion */
|
352 |
|
353 | static PyObject *
|
354 | get_begidx(PyObject *self, PyObject *noarg)
|
355 | {
|
356 | Py_INCREF(begidx);
|
357 | return begidx;
|
358 | }
|
359 |
|
360 | PyDoc_STRVAR(doc_get_begidx,
|
361 | "get_begidx() -> int\n\
|
362 | get the beginning index of the completion scope");
|
363 |
|
364 |
|
365 | /* Get the ending index for the scope of the tab-completion */
|
366 |
|
367 | static PyObject *
|
368 | get_endidx(PyObject *self, PyObject *noarg)
|
369 | {
|
370 | Py_INCREF(endidx);
|
371 | return endidx;
|
372 | }
|
373 |
|
374 | PyDoc_STRVAR(doc_get_endidx,
|
375 | "get_endidx() -> int\n\
|
376 | get the ending index of the completion scope");
|
377 |
|
378 |
|
379 | /* Set the tab-completion word-delimiters that readline uses */
|
380 |
|
381 | static PyObject *
|
382 | set_completer_delims(PyObject *self, PyObject *args)
|
383 | {
|
384 | char *break_chars;
|
385 |
|
386 | if (!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
|
387 | return NULL;
|
388 | }
|
389 | /* Keep a reference to the allocated memory in the module state in case
|
390 | some other module modifies rl_completer_word_break_characters
|
391 | (see issue #17289). */
|
392 | break_chars = strdup(break_chars);
|
393 | if (break_chars) {
|
394 | free(completer_word_break_characters);
|
395 | completer_word_break_characters = break_chars;
|
396 | rl_completer_word_break_characters = break_chars;
|
397 | Py_RETURN_NONE;
|
398 | }
|
399 | else
|
400 | return PyErr_NoMemory();
|
401 | }
|
402 |
|
403 | PyDoc_STRVAR(doc_set_completer_delims,
|
404 | "set_completer_delims(string) -> None\n\
|
405 | set the word delimiters for completion");
|
406 |
|
407 | /* _py_free_history_entry: Utility function to free a history entry. */
|
408 |
|
409 | #if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
|
410 |
|
411 | /* Readline version >= 5.0 introduced a timestamp field into the history entry
|
412 | structure; this needs to be freed to avoid a memory leak. This version of
|
413 | readline also introduced the handy 'free_history_entry' function, which
|
414 | takes care of the timestamp. */
|
415 |
|
416 | static void
|
417 | _py_free_history_entry(HIST_ENTRY *entry)
|
418 | {
|
419 | histdata_t data = free_history_entry(entry);
|
420 | free(data);
|
421 | }
|
422 |
|
423 | #else
|
424 |
|
425 | /* No free_history_entry function; free everything manually. */
|
426 |
|
427 | static void
|
428 | _py_free_history_entry(HIST_ENTRY *entry)
|
429 | {
|
430 | if (entry->line)
|
431 | free((void *)entry->line);
|
432 | if (entry->data)
|
433 | free(entry->data);
|
434 | free(entry);
|
435 | }
|
436 |
|
437 | #endif
|
438 |
|
439 | static PyObject *
|
440 | py_remove_history(PyObject *self, PyObject *args)
|
441 | {
|
442 | int entry_number;
|
443 | HIST_ENTRY *entry;
|
444 |
|
445 | if (!PyArg_ParseTuple(args, "i:remove_history_item", &entry_number))
|
446 | return NULL;
|
447 | if (entry_number < 0) {
|
448 | PyErr_SetString(PyExc_ValueError,
|
449 | "History index cannot be negative");
|
450 | return NULL;
|
451 | }
|
452 | entry = remove_history(entry_number);
|
453 | if (!entry) {
|
454 | PyErr_Format(PyExc_ValueError,
|
455 | "No history item at position %d",
|
456 | entry_number);
|
457 | return NULL;
|
458 | }
|
459 | /* free memory allocated for the history entry */
|
460 | _py_free_history_entry(entry);
|
461 | Py_RETURN_NONE;
|
462 | }
|
463 |
|
464 | PyDoc_STRVAR(doc_remove_history,
|
465 | "remove_history_item(pos) -> None\n\
|
466 | remove history item given by its position");
|
467 |
|
468 | static PyObject *
|
469 | py_replace_history(PyObject *self, PyObject *args)
|
470 | {
|
471 | int entry_number;
|
472 | char *line;
|
473 | HIST_ENTRY *old_entry;
|
474 |
|
475 | if (!PyArg_ParseTuple(args, "is:replace_history_item", &entry_number,
|
476 | &line)) {
|
477 | return NULL;
|
478 | }
|
479 | if (entry_number < 0) {
|
480 | PyErr_SetString(PyExc_ValueError,
|
481 | "History index cannot be negative");
|
482 | return NULL;
|
483 | }
|
484 | old_entry = replace_history_entry(entry_number, line, (void *)NULL);
|
485 | if (!old_entry) {
|
486 | PyErr_Format(PyExc_ValueError,
|
487 | "No history item at position %d",
|
488 | entry_number);
|
489 | return NULL;
|
490 | }
|
491 | /* free memory allocated for the old history entry */
|
492 | _py_free_history_entry(old_entry);
|
493 | Py_RETURN_NONE;
|
494 | }
|
495 |
|
496 | PyDoc_STRVAR(doc_replace_history,
|
497 | "replace_history_item(pos, line) -> None\n\
|
498 | replaces history item given by its position with contents of line");
|
499 |
|
500 | /* Add a line to the history buffer */
|
501 |
|
502 | static PyObject *
|
503 | py_add_history(PyObject *self, PyObject *args)
|
504 | {
|
505 | char *line;
|
506 |
|
507 | if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
|
508 | return NULL;
|
509 | }
|
510 | add_history(line);
|
511 | Py_RETURN_NONE;
|
512 | }
|
513 |
|
514 | PyDoc_STRVAR(doc_add_history,
|
515 | "add_history(string) -> None\n\
|
516 | add an item to the history buffer");
|
517 |
|
518 |
|
519 | /* Get the tab-completion word-delimiters that readline uses */
|
520 |
|
521 | static PyObject *
|
522 | get_completer_delims(PyObject *self, PyObject *noarg)
|
523 | {
|
524 | return PyString_FromString(rl_completer_word_break_characters);
|
525 | }
|
526 |
|
527 | PyDoc_STRVAR(doc_get_completer_delims,
|
528 | "get_completer_delims() -> string\n\
|
529 | get the word delimiters for completion");
|
530 |
|
531 |
|
532 | /* Set the completer function */
|
533 |
|
534 | static PyObject *
|
535 | set_completer(PyObject *self, PyObject *args)
|
536 | {
|
537 | return set_hook("completer", &completer, args);
|
538 | }
|
539 |
|
540 | PyDoc_STRVAR(doc_set_completer,
|
541 | "set_completer([function]) -> None\n\
|
542 | Set or remove the completer function.\n\
|
543 | The function is called as function(text, state),\n\
|
544 | for state in 0, 1, 2, ..., until it returns a non-string.\n\
|
545 | It should return the next possible completion starting with 'text'.");
|
546 |
|
547 |
|
548 | static PyObject *
|
549 | get_completer(PyObject *self, PyObject *noargs)
|
550 | {
|
551 | if (completer == NULL) {
|
552 | Py_RETURN_NONE;
|
553 | }
|
554 | Py_INCREF(completer);
|
555 | return completer;
|
556 | }
|
557 |
|
558 | PyDoc_STRVAR(doc_get_completer,
|
559 | "get_completer() -> function\n\
|
560 | \n\
|
561 | Returns current completer function.");
|
562 |
|
563 | /* Private function to get current length of history. XXX It may be
|
564 | * possible to replace this with a direct use of history_length instead,
|
565 | * but it's not clear whether BSD's libedit keeps history_length up to date.
|
566 | * See issue #8065.*/
|
567 |
|
568 | static int
|
569 | _py_get_history_length(void)
|
570 | {
|
571 | HISTORY_STATE *hist_st = history_get_history_state();
|
572 | int length = hist_st->length;
|
573 | /* the history docs don't say so, but the address of hist_st changes each
|
574 | time history_get_history_state is called which makes me think it's
|
575 | freshly malloc'd memory... on the other hand, the address of the last
|
576 | line stays the same as long as history isn't extended, so it appears to
|
577 | be malloc'd but managed by the history package... */
|
578 | free(hist_st);
|
579 | return length;
|
580 | }
|
581 |
|
582 | /* Exported function to get any element of history */
|
583 |
|
584 | static PyObject *
|
585 | get_history_item(PyObject *self, PyObject *args)
|
586 | {
|
587 | int idx = 0;
|
588 | HIST_ENTRY *hist_ent;
|
589 |
|
590 | if (!PyArg_ParseTuple(args, "i:get_history_item", &idx))
|
591 | return NULL;
|
592 | #ifdef __APPLE__
|
593 | if (using_libedit_emulation) {
|
594 | /* Older versions of libedit's readline emulation
|
595 | * use 0-based indexes, while readline and newer
|
596 | * versions of libedit use 1-based indexes.
|
597 | */
|
598 | int length = _py_get_history_length();
|
599 |
|
600 | idx = idx - 1 + libedit_history_start;
|
601 |
|
602 | /*
|
603 | * Apple's readline emulation crashes when
|
604 | * the index is out of range, therefore
|
605 | * test for that and fail gracefully.
|
606 | */
|
607 | if (idx < (0 + libedit_history_start)
|
608 | || idx >= (length + libedit_history_start)) {
|
609 | Py_RETURN_NONE;
|
610 | }
|
611 | }
|
612 | #endif /* __APPLE__ */
|
613 | if ((hist_ent = history_get(idx)))
|
614 | return PyString_FromString(hist_ent->line);
|
615 | else {
|
616 | Py_RETURN_NONE;
|
617 | }
|
618 | }
|
619 |
|
620 | PyDoc_STRVAR(doc_get_history_item,
|
621 | "get_history_item() -> string\n\
|
622 | return the current contents of history item at index.");
|
623 |
|
624 |
|
625 | /* Exported function to get current length of history */
|
626 |
|
627 | static PyObject *
|
628 | get_current_history_length(PyObject *self, PyObject *noarg)
|
629 | {
|
630 | return PyInt_FromLong((long)_py_get_history_length());
|
631 | }
|
632 |
|
633 | PyDoc_STRVAR(doc_get_current_history_length,
|
634 | "get_current_history_length() -> integer\n\
|
635 | return the current (not the maximum) length of history.");
|
636 |
|
637 |
|
638 | /* Exported function to read the current line buffer */
|
639 |
|
640 | static PyObject *
|
641 | get_line_buffer(PyObject *self, PyObject *noarg)
|
642 | {
|
643 | return PyString_FromString(rl_line_buffer);
|
644 | }
|
645 |
|
646 | PyDoc_STRVAR(doc_get_line_buffer,
|
647 | "get_line_buffer() -> string\n\
|
648 | return the current contents of the line buffer.");
|
649 |
|
650 |
|
651 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
|
652 |
|
653 | /* Exported function to clear the current history */
|
654 |
|
655 | static PyObject *
|
656 | py_clear_history(PyObject *self, PyObject *noarg)
|
657 | {
|
658 | clear_history();
|
659 | Py_RETURN_NONE;
|
660 | }
|
661 |
|
662 | PyDoc_STRVAR(doc_clear_history,
|
663 | "clear_history() -> None\n\
|
664 | Clear the current readline history.");
|
665 | #endif
|
666 |
|
667 | /* Added for OSH. We need to call this in our SIGWINCH handler so global
|
668 | * variables in readline get updated. */
|
669 | static PyObject *
|
670 | py_resize_terminal(PyObject *self, PyObject *noarg)
|
671 | {
|
672 | rl_resize_terminal();
|
673 | Py_RETURN_NONE;
|
674 | }
|
675 |
|
676 | /* Exported function to insert text into the line buffer */
|
677 |
|
678 | static PyObject *
|
679 | insert_text(PyObject *self, PyObject *args)
|
680 | {
|
681 | char *s;
|
682 | if (!PyArg_ParseTuple(args, "s:insert_text", &s))
|
683 | return NULL;
|
684 | rl_insert_text(s);
|
685 | Py_RETURN_NONE;
|
686 | }
|
687 |
|
688 | PyDoc_STRVAR(doc_insert_text,
|
689 | "insert_text(string) -> None\n\
|
690 | Insert text into the line buffer at the cursor position.");
|
691 |
|
692 |
|
693 | /* Redisplay the line buffer */
|
694 |
|
695 | static PyObject *
|
696 | redisplay(PyObject *self, PyObject *noarg)
|
697 | {
|
698 | rl_redisplay();
|
699 | Py_RETURN_NONE;
|
700 | }
|
701 |
|
702 | PyDoc_STRVAR(doc_redisplay,
|
703 | "redisplay() -> None\n\
|
704 | Change what's displayed on the screen to reflect the current\n\
|
705 | contents of the line buffer.");
|
706 |
|
707 |
|
708 | /* List binding functions */
|
709 |
|
710 | static PyObject*
|
711 | list_funmap_names(PyObject *self, PyObject *args)
|
712 | {
|
713 | rl_list_funmap_names();
|
714 | Py_RETURN_NONE;
|
715 | }
|
716 |
|
717 | PyDoc_STRVAR(doc_list_funmap_names,
|
718 | "list_funmap_names() -> None\n\
|
719 | Print all of the available readline functions.");
|
720 |
|
721 | /* Print readline functions and their bindings */
|
722 |
|
723 | static PyObject*
|
724 | function_dumper(PyObject *self, PyObject *args)
|
725 | {
|
726 | int print_readably;
|
727 |
|
728 | if (!PyArg_ParseTuple(args, "i:function_dumper", &print_readably))
|
729 | return NULL;
|
730 |
|
731 | rl_function_dumper(print_readably);
|
732 | Py_RETURN_NONE;
|
733 | }
|
734 |
|
735 | PyDoc_STRVAR(doc_list_function_dumper,
|
736 | "function_dumper(bool) -> None\n\
|
737 | Print all readline functions and their bindings.");
|
738 |
|
739 | /* Print macros, their bindings, and their string outputs */
|
740 |
|
741 | static PyObject*
|
742 | macro_dumper(PyObject *self, PyObject *args)
|
743 | {
|
744 | int print_readably;
|
745 |
|
746 | if (!PyArg_ParseTuple(args, "i:macro_dumper", &print_readably))
|
747 | return NULL;
|
748 |
|
749 | rl_macro_dumper(print_readably);
|
750 | Py_RETURN_NONE;
|
751 | }
|
752 |
|
753 | PyDoc_STRVAR(doc_list_macro_dumper,
|
754 | "macro_dumper(bool) -> None\n\
|
755 | Print all readline sequences bound to macros and the strings they output.");
|
756 |
|
757 | /* List readline variables */
|
758 |
|
759 | static PyObject*
|
760 | variable_dumper(PyObject *self, PyObject *args)
|
761 | {
|
762 | int print_readably;
|
763 |
|
764 | if (!PyArg_ParseTuple(args, "i:variable_dumper", &print_readably))
|
765 | return NULL;
|
766 |
|
767 | rl_variable_dumper(print_readably);
|
768 | Py_RETURN_NONE;
|
769 | }
|
770 |
|
771 | PyDoc_STRVAR(doc_list_variable_dumper,
|
772 | "variable_dumper(bool) -> None\n\
|
773 | List readline variables and their values.");
|
774 |
|
775 |
|
776 |
|
777 | /* Query bindings for a function name */
|
778 |
|
779 | // readline returns null-terminated string arrays
|
780 | void _strvec_dispose(char **strvec) {
|
781 | register int i;
|
782 |
|
783 | if (strvec == NULL)
|
784 | return;
|
785 |
|
786 | for (i = 0; strvec[i]; i++) {
|
787 | free(strvec[i]);
|
788 | }
|
789 |
|
790 | free(strvec);
|
791 | }
|
792 |
|
793 | // Nicely prints a strvec with commas and an and
|
794 | // like '"foo", "bar", and "moop"'
|
795 | void _pprint_strvec_list(char **strvec) {
|
796 | int i;
|
797 |
|
798 | for (i = 0; strvec[i]; i++) {
|
799 | printf("\"%s\"", strvec[i]);
|
800 | if (strvec[i + 1]) {
|
801 | printf(", ");
|
802 | if (!strvec[i + 2])
|
803 | printf("and ");
|
804 | }
|
805 | }
|
806 | }
|
807 |
|
808 | static PyObject*
|
809 | query_bindings(PyObject *self, PyObject *args)
|
810 | {
|
811 | char *fn_name;
|
812 | rl_command_func_t *cmd_fn;
|
813 | char **key_seqs;
|
814 | int i;
|
815 |
|
816 | if (!PyArg_ParseTuple(args, "s:query_bindings", &fn_name))
|
817 | return NULL;
|
818 |
|
819 | cmd_fn = rl_named_function(fn_name);
|
820 |
|
821 | if (cmd_fn == NULL) {
|
822 | PyErr_Format(PyExc_ValueError, "`%s': unknown function name", fn_name);
|
823 | return NULL;
|
824 | }
|
825 |
|
826 | key_seqs = rl_invoking_keyseqs(cmd_fn);
|
827 |
|
828 | if (!key_seqs) {
|
829 | PyErr_Format(PyExc_ValueError, "%s is not bound to any keys", fn_name);
|
830 | return NULL;
|
831 | }
|
832 |
|
833 | printf("%s can be invoked via ", fn_name);
|
834 | _pprint_strvec_list(key_seqs);
|
835 | printf(".\n");
|
836 |
|
837 | _strvec_dispose(key_seqs);
|
838 |
|
839 | Py_RETURN_NONE;
|
840 | }
|
841 |
|
842 | PyDoc_STRVAR(doc_query_bindings,
|
843 | "query_bindings(str) -> None\n\
|
844 | Query bindings to see what's bound to a given function.");
|
845 |
|
846 |
|
847 | /* Table of functions exported by the module */
|
848 |
|
849 | #ifdef OVM_MAIN
|
850 | #include "pyext/line_input.c/readline_methods.def"
|
851 | #else
|
852 | static struct PyMethodDef readline_methods[] = {
|
853 | {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
|
854 | {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
|
855 | {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
|
856 | {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
|
857 | {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
|
858 | {"read_history_file", read_history_file,
|
859 | METH_VARARGS, doc_read_history_file},
|
860 | {"write_history_file", write_history_file,
|
861 | METH_VARARGS, doc_write_history_file},
|
862 | {"get_history_item", get_history_item,
|
863 | METH_VARARGS, doc_get_history_item},
|
864 | {"get_current_history_length", (PyCFunction)get_current_history_length,
|
865 | METH_NOARGS, doc_get_current_history_length},
|
866 | {"set_history_length", set_history_length,
|
867 | METH_VARARGS, set_history_length_doc},
|
868 | {"get_history_length", get_history_length,
|
869 | METH_NOARGS, get_history_length_doc},
|
870 | {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
|
871 | {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
|
872 | {"get_completion_type", get_completion_type,
|
873 | METH_NOARGS, doc_get_completion_type},
|
874 | {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
|
875 | {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
|
876 |
|
877 | {"set_completer_delims", set_completer_delims,
|
878 | METH_VARARGS, doc_set_completer_delims},
|
879 | {"add_history", py_add_history, METH_VARARGS, doc_add_history},
|
880 | {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
|
881 | {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
|
882 | {"get_completer_delims", get_completer_delims,
|
883 | METH_NOARGS, doc_get_completer_delims},
|
884 |
|
885 | {"set_completion_display_matches_hook", set_completion_display_matches_hook,
|
886 | METH_VARARGS, doc_set_completion_display_matches_hook},
|
887 | {"set_startup_hook", set_startup_hook,
|
888 | METH_VARARGS, doc_set_startup_hook},
|
889 | {"set_pre_input_hook", set_pre_input_hook,
|
890 | METH_VARARGS, doc_set_pre_input_hook},
|
891 | {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
|
892 | {"resize_terminal", py_resize_terminal, METH_NOARGS, ""},
|
893 | {"list_funmap_names", list_funmap_names, METH_NOARGS, doc_list_funmap_names},
|
894 | {"function_dumper", function_dumper, METH_VARARGS, doc_list_function_dumper},
|
895 | {"macro_dumper", macro_dumper, METH_VARARGS, doc_list_macro_dumper},
|
896 | {"variable_dumper", variable_dumper, METH_VARARGS, doc_list_variable_dumper},
|
897 | {"query_bindings", query_bindings, METH_VARARGS, doc_query_bindings},
|
898 | {0, 0}
|
899 | };
|
900 | #endif
|
901 |
|
902 |
|
903 | /* C function to call the Python hooks. */
|
904 |
|
905 | static int
|
906 | on_hook(PyObject *func)
|
907 | {
|
908 | int result = 0;
|
909 | if (func != NULL) {
|
910 | PyObject *r;
|
911 | #ifdef WITH_THREAD
|
912 | PyGILState_STATE gilstate = PyGILState_Ensure();
|
913 | #endif
|
914 | r = PyObject_CallFunction(func, NULL);
|
915 | if (r == NULL)
|
916 | goto error;
|
917 | if (r == Py_None)
|
918 | result = 0;
|
919 | else {
|
920 | result = PyInt_AsLong(r);
|
921 | if (result == -1 && PyErr_Occurred())
|
922 | goto error;
|
923 | }
|
924 | Py_DECREF(r);
|
925 | goto done;
|
926 | error:
|
927 | PyErr_Clear();
|
928 | Py_XDECREF(r);
|
929 | done:
|
930 | #ifdef WITH_THREAD
|
931 | PyGILState_Release(gilstate);
|
932 | #endif
|
933 | return result;
|
934 | }
|
935 | return result;
|
936 | }
|
937 |
|
938 | static int
|
939 | #if defined(_RL_FUNCTION_TYPEDEF)
|
940 | on_startup_hook(void)
|
941 | #else
|
942 | on_startup_hook()
|
943 | #endif
|
944 | {
|
945 | return on_hook(startup_hook);
|
946 | }
|
947 |
|
948 | #ifdef HAVE_RL_PRE_INPUT_HOOK
|
949 | static int
|
950 | #if defined(_RL_FUNCTION_TYPEDEF)
|
951 | on_pre_input_hook(void)
|
952 | #else
|
953 | on_pre_input_hook()
|
954 | #endif
|
955 | {
|
956 | return on_hook(pre_input_hook);
|
957 | }
|
958 | #endif
|
959 |
|
960 |
|
961 | /* C function to call the Python completion_display_matches */
|
962 |
|
963 | #ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
|
964 | static void
|
965 | on_completion_display_matches_hook(char **matches,
|
966 | int num_matches, int max_length)
|
967 | {
|
968 | int i;
|
969 | PyObject *m=NULL, *s=NULL, *r=NULL;
|
970 | #ifdef WITH_THREAD
|
971 | PyGILState_STATE gilstate = PyGILState_Ensure();
|
972 | #endif
|
973 | m = PyList_New(num_matches);
|
974 | if (m == NULL)
|
975 | goto error;
|
976 | for (i = 0; i < num_matches; i++) {
|
977 | s = PyString_FromString(matches[i+1]);
|
978 | if (s == NULL)
|
979 | goto error;
|
980 | if (PyList_SetItem(m, i, s) == -1)
|
981 | goto error;
|
982 | }
|
983 |
|
984 | r = PyObject_CallFunction(completion_display_matches_hook,
|
985 | "sOi", matches[0], m, max_length);
|
986 |
|
987 | Py_DECREF(m); m=NULL;
|
988 |
|
989 | if (r == NULL ||
|
990 | (r != Py_None && PyInt_AsLong(r) == -1 && PyErr_Occurred())) {
|
991 | goto error;
|
992 | }
|
993 | Py_XDECREF(r); r=NULL;
|
994 |
|
995 | if (0) {
|
996 | error:
|
997 | PyErr_Clear();
|
998 | Py_XDECREF(m);
|
999 | Py_XDECREF(r);
|
1000 | }
|
1001 | #ifdef WITH_THREAD
|
1002 | PyGILState_Release(gilstate);
|
1003 | #endif
|
1004 | }
|
1005 |
|
1006 | #endif
|
1007 |
|
1008 | #ifdef HAVE_RL_RESIZE_TERMINAL
|
1009 | static volatile sig_atomic_t sigwinch_received;
|
1010 | static PyOS_sighandler_t sigwinch_ohandler;
|
1011 |
|
1012 | static void
|
1013 | readline_sigwinch_handler(int signum)
|
1014 | {
|
1015 | sigwinch_received = 1;
|
1016 | if (sigwinch_ohandler &&
|
1017 | sigwinch_ohandler != SIG_IGN && sigwinch_ohandler != SIG_DFL)
|
1018 | sigwinch_ohandler(signum);
|
1019 | }
|
1020 | #endif
|
1021 |
|
1022 | /* C function to call the Python completer. */
|
1023 |
|
1024 | static char *
|
1025 | on_completion(const char *text, int state)
|
1026 | {
|
1027 | char *result = NULL;
|
1028 | if (completer != NULL) {
|
1029 | PyObject *r;
|
1030 | #ifdef WITH_THREAD
|
1031 | PyGILState_STATE gilstate = PyGILState_Ensure();
|
1032 | #endif
|
1033 | rl_attempted_completion_over = 1;
|
1034 | r = PyObject_CallFunction(completer, "si", text, state);
|
1035 | if (r == NULL)
|
1036 | goto error;
|
1037 | if (r == Py_None) {
|
1038 | result = NULL;
|
1039 | }
|
1040 | else {
|
1041 | char *s = PyString_AsString(r);
|
1042 | if (s == NULL)
|
1043 | goto error;
|
1044 | result = strdup(s);
|
1045 | }
|
1046 | Py_DECREF(r);
|
1047 | goto done;
|
1048 | error:
|
1049 | PyErr_Clear();
|
1050 | Py_XDECREF(r);
|
1051 | done:
|
1052 | #ifdef WITH_THREAD
|
1053 | PyGILState_Release(gilstate);
|
1054 | #endif
|
1055 | return result;
|
1056 | }
|
1057 | return result;
|
1058 | }
|
1059 |
|
1060 |
|
1061 | /* A more flexible constructor that saves the "begidx" and "endidx"
|
1062 | * before calling the normal completer */
|
1063 |
|
1064 | static char **
|
1065 | flex_complete(const char *text, int start, int end)
|
1066 | {
|
1067 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
|
1068 | rl_completion_append_character ='\0';
|
1069 | #endif
|
1070 | #ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
|
1071 | rl_completion_suppress_append = 0;
|
1072 | #endif
|
1073 | Py_XDECREF(begidx);
|
1074 | Py_XDECREF(endidx);
|
1075 | begidx = PyInt_FromLong((long) start);
|
1076 | endidx = PyInt_FromLong((long) end);
|
1077 | return completion_matches(text, *on_completion);
|
1078 | }
|
1079 |
|
1080 |
|
1081 | /* Helper to initialize GNU readline properly. */
|
1082 |
|
1083 | static void
|
1084 | setup_readline(void)
|
1085 | {
|
1086 | #ifdef SAVE_LOCALE
|
1087 | char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
|
1088 | if (!saved_locale)
|
1089 | Py_FatalError("not enough memory to save locale");
|
1090 | #endif
|
1091 |
|
1092 | #ifdef __APPLE__
|
1093 | /* the libedit readline emulation resets key bindings etc
|
1094 | * when calling rl_initialize. So call it upfront
|
1095 | */
|
1096 | if (using_libedit_emulation)
|
1097 | rl_initialize();
|
1098 |
|
1099 | /* Detect if libedit's readline emulation uses 0-based
|
1100 | * indexing or 1-based indexing.
|
1101 | */
|
1102 | add_history("1");
|
1103 | if (history_get(1) == NULL) {
|
1104 | libedit_history_start = 0;
|
1105 | } else {
|
1106 | libedit_history_start = 1;
|
1107 | }
|
1108 | clear_history();
|
1109 | #endif /* __APPLE__ */
|
1110 |
|
1111 | using_history();
|
1112 |
|
1113 | rl_readline_name = "oils";
|
1114 | #if defined(PYOS_OS2) && defined(PYCC_GCC)
|
1115 | /* Allow $if term= in .inputrc to work */
|
1116 | rl_terminal_name = getenv("TERM");
|
1117 | #endif
|
1118 | /* Force rebind of TAB to insert-tab */
|
1119 | rl_bind_key('\t', rl_insert);
|
1120 | /* Bind both ESC-TAB and ESC-ESC to the completion function */
|
1121 | rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
|
1122 | rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
|
1123 | #ifdef HAVE_RL_RESIZE_TERMINAL
|
1124 | /* Set up signal handler for window resize */
|
1125 | sigwinch_ohandler = PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
|
1126 | #endif
|
1127 | /* Set our hook functions */
|
1128 | rl_startup_hook = on_startup_hook;
|
1129 | #ifdef HAVE_RL_PRE_INPUT_HOOK
|
1130 | rl_pre_input_hook = on_pre_input_hook;
|
1131 | #endif
|
1132 | /* Set our completion function */
|
1133 | rl_attempted_completion_function = flex_complete;
|
1134 | /* Set Python word break characters */
|
1135 | completer_word_break_characters =
|
1136 | rl_completer_word_break_characters =
|
1137 | strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
|
1138 | /* All nonalphanums except '.' */
|
1139 |
|
1140 | begidx = PyInt_FromLong(0L);
|
1141 | endidx = PyInt_FromLong(0L);
|
1142 |
|
1143 | #ifdef __APPLE__
|
1144 | if (!using_libedit_emulation)
|
1145 | #endif
|
1146 | {
|
1147 | if (!isatty(STDOUT_FILENO)) {
|
1148 | /* Issue #19884: stdout is not a terminal. Disable meta modifier
|
1149 | keys to not write the ANSI sequence "\033[1034h" into stdout. On
|
1150 | terminals supporting 8 bit characters like TERM=xterm-256color
|
1151 | (which is now the default Fedora since Fedora 18), the meta key is
|
1152 | used to enable support of 8 bit characters (ANSI sequence
|
1153 | "\033[1034h").
|
1154 |
|
1155 | With libedit, this call makes readline() crash. */
|
1156 | rl_variable_bind ("enable-meta-key", "off");
|
1157 | }
|
1158 | }
|
1159 |
|
1160 | /* Initialize (allows .inputrc to override)
|
1161 | *
|
1162 | * XXX: A bug in the readline-2.2 library causes a memory leak
|
1163 | * inside this function. Nothing we can do about it.
|
1164 | */
|
1165 | #ifdef __APPLE__
|
1166 | if (using_libedit_emulation)
|
1167 | rl_read_init_file(NULL);
|
1168 | else
|
1169 | #endif /* __APPLE__ */
|
1170 | rl_initialize();
|
1171 |
|
1172 | RESTORE_LOCALE(saved_locale)
|
1173 | }
|
1174 |
|
1175 | /* Wrapper around GNU readline that handles signals differently. */
|
1176 |
|
1177 |
|
1178 | #if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
|
1179 |
|
1180 | static char *completed_input_string;
|
1181 | static void
|
1182 | rlhandler(char *text)
|
1183 | {
|
1184 | completed_input_string = text;
|
1185 | rl_callback_handler_remove();
|
1186 | }
|
1187 |
|
1188 | static char *
|
1189 | readline_until_enter_or_signal(char *prompt, int *signal)
|
1190 | {
|
1191 | char * not_done_reading = "";
|
1192 | fd_set selectset;
|
1193 |
|
1194 | *signal = 0;
|
1195 | #ifdef HAVE_RL_CATCH_SIGNAL
|
1196 | rl_catch_signals = 0;
|
1197 | #endif
|
1198 | /* OVM_MAIN: Oil is handling SIGWINCH, so readline shouldn't handle it.
|
1199 | * Without this line, strace reveals that GNU readline is constantly
|
1200 | * turning it on and off.
|
1201 | * */
|
1202 | rl_catch_sigwinch = 0;
|
1203 |
|
1204 | rl_callback_handler_install (prompt, rlhandler);
|
1205 | FD_ZERO(&selectset);
|
1206 |
|
1207 | completed_input_string = not_done_reading;
|
1208 |
|
1209 | while (completed_input_string == not_done_reading) {
|
1210 | int has_input = 0;
|
1211 |
|
1212 | while (!has_input)
|
1213 | { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
|
1214 |
|
1215 | /* [Bug #1552726] Only limit the pause if an input hook has been
|
1216 | defined. */
|
1217 | struct timeval *timeoutp = NULL;
|
1218 | if (PyOS_InputHook)
|
1219 | timeoutp = &timeout;
|
1220 | #ifdef HAVE_RL_RESIZE_TERMINAL
|
1221 | /* Update readline's view of the window size after SIGWINCH */
|
1222 | if (sigwinch_received) {
|
1223 | sigwinch_received = 0;
|
1224 | rl_resize_terminal();
|
1225 | }
|
1226 | #endif
|
1227 | FD_SET(fileno(rl_instream), &selectset);
|
1228 | /* select resets selectset if no input was available */
|
1229 | has_input = select(fileno(rl_instream) + 1, &selectset,
|
1230 | NULL, NULL, timeoutp);
|
1231 | if(PyOS_InputHook) PyOS_InputHook();
|
1232 | }
|
1233 |
|
1234 | if(has_input > 0) {
|
1235 | rl_callback_read_char();
|
1236 | }
|
1237 | else if (errno == EINTR) {
|
1238 | int s;
|
1239 | #ifdef WITH_THREAD
|
1240 | PyEval_RestoreThread(_PyOS_ReadlineTState);
|
1241 | #endif
|
1242 | s = PyErr_CheckSignals();
|
1243 | #ifdef WITH_THREAD
|
1244 | PyEval_SaveThread();
|
1245 | #endif
|
1246 | if (s < 0) {
|
1247 | rl_free_line_state();
|
1248 | #if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0700
|
1249 | rl_callback_sigcleanup();
|
1250 | #endif
|
1251 | rl_cleanup_after_signal();
|
1252 | rl_callback_handler_remove();
|
1253 | *signal = 1;
|
1254 | completed_input_string = NULL;
|
1255 | }
|
1256 | }
|
1257 | }
|
1258 |
|
1259 | return completed_input_string;
|
1260 | }
|
1261 |
|
1262 |
|
1263 | #else
|
1264 |
|
1265 | /* Interrupt handler */
|
1266 |
|
1267 | static jmp_buf jbuf;
|
1268 |
|
1269 | /* ARGSUSED */
|
1270 | static void
|
1271 | onintr(int sig)
|
1272 | {
|
1273 | longjmp(jbuf, 1);
|
1274 | }
|
1275 |
|
1276 |
|
1277 | static char *
|
1278 | readline_until_enter_or_signal(char *prompt, int *signal)
|
1279 | {
|
1280 | PyOS_sighandler_t old_inthandler;
|
1281 | char *p;
|
1282 |
|
1283 | *signal = 0;
|
1284 |
|
1285 | old_inthandler = PyOS_setsig(SIGINT, onintr);
|
1286 | if (setjmp(jbuf)) {
|
1287 | #ifdef HAVE_SIGRELSE
|
1288 | /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
|
1289 | sigrelse(SIGINT);
|
1290 | #endif
|
1291 | PyOS_setsig(SIGINT, old_inthandler);
|
1292 | *signal = 1;
|
1293 | return NULL;
|
1294 | }
|
1295 | rl_event_hook = PyOS_InputHook;
|
1296 | p = readline(prompt);
|
1297 | PyOS_setsig(SIGINT, old_inthandler);
|
1298 |
|
1299 | return p;
|
1300 | }
|
1301 | #endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
|
1302 |
|
1303 |
|
1304 | static char *
|
1305 | call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
|
1306 | {
|
1307 | size_t n;
|
1308 | char *p, *q;
|
1309 | int signal;
|
1310 |
|
1311 | #ifdef SAVE_LOCALE
|
1312 | char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
|
1313 | if (!saved_locale)
|
1314 | Py_FatalError("not enough memory to save locale");
|
1315 | setlocale(LC_CTYPE, "");
|
1316 | #endif
|
1317 |
|
1318 | if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
|
1319 | rl_instream = sys_stdin;
|
1320 | rl_outstream = sys_stdout;
|
1321 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
|
1322 | rl_prep_terminal (1);
|
1323 | #endif
|
1324 | }
|
1325 |
|
1326 | p = readline_until_enter_or_signal(prompt, &signal);
|
1327 |
|
1328 | /* we got an interrupt signal */
|
1329 | if (signal) {
|
1330 | RESTORE_LOCALE(saved_locale)
|
1331 | return NULL;
|
1332 | }
|
1333 |
|
1334 | /* We got an EOF, return an empty string. */
|
1335 | if (p == NULL) {
|
1336 | p = PyMem_Malloc(1);
|
1337 | if (p != NULL)
|
1338 | *p = '\0';
|
1339 | RESTORE_LOCALE(saved_locale)
|
1340 | return p;
|
1341 | }
|
1342 |
|
1343 | /* we have a valid line */
|
1344 | n = strlen(p);
|
1345 | /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
|
1346 | release the original. */
|
1347 | q = p;
|
1348 | p = PyMem_Malloc(n+2);
|
1349 | if (p != NULL) {
|
1350 | strncpy(p, q, n);
|
1351 | p[n] = '\n';
|
1352 | p[n+1] = '\0';
|
1353 | }
|
1354 | free(q);
|
1355 | RESTORE_LOCALE(saved_locale)
|
1356 | return p;
|
1357 | }
|
1358 |
|
1359 |
|
1360 | /* Initialize the module */
|
1361 |
|
1362 | PyDoc_STRVAR(doc_module,
|
1363 | "Importing this module enables command line editing using GNU readline.");
|
1364 |
|
1365 | #ifdef __APPLE__
|
1366 | PyDoc_STRVAR(doc_module_le,
|
1367 | "Importing this module enables command line editing using libedit readline.");
|
1368 | #endif /* __APPLE__ */
|
1369 |
|
1370 | PyMODINIT_FUNC
|
1371 | initline_input(void)
|
1372 | {
|
1373 | PyObject *m;
|
1374 |
|
1375 | #ifdef __APPLE__
|
1376 | if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
|
1377 | using_libedit_emulation = 1;
|
1378 | }
|
1379 |
|
1380 | if (using_libedit_emulation)
|
1381 | m = Py_InitModule4("line_input", readline_methods, doc_module_le,
|
1382 | (PyObject *)NULL, PYTHON_API_VERSION);
|
1383 | else
|
1384 |
|
1385 | #endif /* __APPLE__ */
|
1386 |
|
1387 | m = Py_InitModule4("line_input", readline_methods, doc_module,
|
1388 | (PyObject *)NULL, PYTHON_API_VERSION);
|
1389 | if (m == NULL)
|
1390 | return;
|
1391 |
|
1392 | PyOS_ReadlineFunctionPointer = call_readline;
|
1393 | setup_readline();
|
1394 |
|
1395 | PyModule_AddIntConstant(m, "_READLINE_VERSION", RL_READLINE_VERSION);
|
1396 | PyModule_AddIntConstant(m, "_READLINE_RUNTIME_VERSION", rl_readline_version);
|
1397 | }
|