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