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