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