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