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