blob: c900e079543c4f85eca68b9d70ef950504b722a8 [file] [log] [blame]
Guido van Rossum290900a1997-09-26 21:51:21 +00001/* This module makes GNU readline available to Python. It has ideas
2 * contributed by Lee Busby, LLNL, and William Magro, Cornell Theory
Michael W. Hudson9a8c3142005-03-30 10:09:12 +00003 * Center. The completer interface was inspired by Lele Gaifax. More
4 * recently, it was largely rewritten by Guido van Rossum.
Guido van Rossum0969d361997-08-05 21:27:50 +00005 */
6
Guido van Rossum290900a1997-09-26 21:51:21 +00007/* Standard definitions */
Guido van Rossum0969d361997-08-05 21:27:50 +00008#include "Python.h"
Antoine Pitrou5c30a752013-07-31 21:52:53 +02009#include <stddef.h>
Guido van Rossum0969d361997-08-05 21:27:50 +000010#include <signal.h>
Guido van Rossum290900a1997-09-26 21:51:21 +000011#include <errno.h>
Michael W. Hudson8da2b012004-10-07 13:46:33 +000012#include <sys/time.h>
Guido van Rossum0969d361997-08-05 21:27:50 +000013
Skip Montanaro7befb992004-02-10 16:50:21 +000014#if defined(HAVE_SETLOCALE)
Guido van Rossum60c8a3a2002-10-09 21:27:33 +000015/* GNU readline() mistakenly sets the LC_CTYPE locale.
16 * This is evil. Only the user or the app's main() should do this!
17 * We must save and restore the locale around the rl_initialize() call.
18 */
19#define SAVE_LOCALE
20#include <locale.h>
21#endif
22
Thomas Wouters0e3f5912006-08-11 14:57:12 +000023#ifdef SAVE_LOCALE
24# define RESTORE_LOCALE(sl) { setlocale(LC_CTYPE, sl); free(sl); }
25#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000026# define RESTORE_LOCALE(sl)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000027#endif
28
Roland Hiebere1f77692021-02-09 02:05:25 +010029#ifdef WITH_EDITLINE
30# include <editline/readline.h>
31#else
Guido van Rossum290900a1997-09-26 21:51:21 +000032/* GNU readline definitions */
Roland Hiebere1f77692021-02-09 02:05:25 +010033# undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
34# include <readline/readline.h>
35# include <readline/history.h>
36#endif
Guido van Rossum730806d1998-04-10 22:27:42 +000037
Guido van Rossum353ae582001-07-10 16:45:32 +000038#ifdef HAVE_RL_COMPLETION_MATCHES
Guido van Rossum74f31432003-01-07 20:01:29 +000039#define completion_matches(x, y) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
Guido van Rossumd8faa362007-04-27 19:54:29 +000041#else
Martin v. Löwisb37509b2008-11-04 20:45:29 +000042#if defined(_RL_FUNCTION_TYPEDEF)
Guido van Rossumd8faa362007-04-27 19:54:29 +000043extern char **completion_matches(char *, rl_compentry_func_t *);
Martin v. Löwisb37509b2008-11-04 20:45:29 +000044#else
Ronald Oussoren25696bb2010-02-11 13:15:00 +000045
46#if !defined(__APPLE__)
Martin v. Löwisb37509b2008-11-04 20:45:29 +000047extern char **completion_matches(char *, CPFunction *);
48#endif
Guido van Rossum353ae582001-07-10 16:45:32 +000049#endif
Ronald Oussoren25696bb2010-02-11 13:15:00 +000050#endif
Guido van Rossum353ae582001-07-10 16:45:32 +000051
Ronald Oussoren2efd9242009-09-20 14:53:22 +000052/*
53 * It is possible to link the readline module to the readline
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 * emulation library of editline/libedit.
55 *
serge-sans-paille71053192019-12-04 17:02:57 +010056 * This emulation library is not 100% API compatible with the "real" readline
57 * and cannot be detected at compile-time,
Jero Badoa4258e82020-12-29 20:26:57 +080058 * hence we use a runtime check to detect if the Python readline module is
serge-sans-paille71053192019-12-04 17:02:57 +010059 * linked to libedit.
Ronald Oussoren2efd9242009-09-20 14:53:22 +000060 *
Ned Deily5d4121a2013-10-12 15:47:58 -070061 * Currently there is one known API incompatibility:
Ronald Oussoren2efd9242009-09-20 14:53:22 +000062 * - 'get_history' has a 1-based index with GNU readline, and a 0-based
Ned Deily5d4121a2013-10-12 15:47:58 -070063 * index with older versions of libedit's emulation.
Ronald Oussoren2efd9242009-09-20 14:53:22 +000064 * - Note that replace_history and remove_history use a 0-based index
Ned Deily5d4121a2013-10-12 15:47:58 -070065 * with both implementations.
Ronald Oussoren2efd9242009-09-20 14:53:22 +000066 */
67static int using_libedit_emulation = 0;
68static const char libedit_version_tag[] = "EditLine wrapper";
Ned Deilyf70f4a62013-09-06 15:16:19 -070069
Gregory P. Smithfd053fd2021-02-12 12:04:46 -080070static int8_t libedit_history_start = 0;
71static int8_t libedit_append_replace_history_offset = 0;
Ronald Oussoren2efd9242009-09-20 14:53:22 +000072
Georg Brandl646fdd62010-10-18 07:27:55 +000073#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Christian Heimes32fbe592007-11-12 15:01:33 +000074static void
75on_completion_display_matches_hook(char **matches,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 int num_matches, int max_length);
Georg Brandl646fdd62010-10-18 07:27:55 +000077#endif
Guido van Rossum0969d361997-08-05 21:27:50 +000078
Antoine Pitroua7f7deb2013-05-06 21:51:03 +020079/* Memory allocated for rl_completer_word_break_characters
80 (see issue #17289 for the motivation). */
81static char *completer_word_break_characters;
82
Antoine Pitrou5c30a752013-07-31 21:52:53 +020083typedef struct {
Martin Panterf6e9f472016-03-22 02:19:29 +000084 /* Specify hook functions in Python */
Antoine Pitrou5c30a752013-07-31 21:52:53 +020085 PyObject *completion_display_matches_hook;
86 PyObject *startup_hook;
87 PyObject *pre_input_hook;
Martin Panterf6e9f472016-03-22 02:19:29 +000088
89 PyObject *completer; /* Specify a word completer in Python */
Antoine Pitrou5c30a752013-07-31 21:52:53 +020090 PyObject *begidx;
91 PyObject *endidx;
92} readlinestate;
93
Hai Shif707d942020-03-16 21:15:01 +080094static inline readlinestate*
Hai Shi5f104d52020-03-17 01:16:32 +080095get_readline_state(PyObject *module)
Hai Shif707d942020-03-16 21:15:01 +080096{
97 void *state = PyModule_GetState(module);
98 assert(state != NULL);
99 return (readlinestate *)state;
100}
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200101
Zackery Spytzb7047e52020-07-12 10:01:03 -0600102/*[clinic input]
103module readline
104[clinic start generated code]*/
105/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ad49da781b9c8721]*/
106
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200107static int
108readline_clear(PyObject *m)
109{
Hai Shif707d942020-03-16 21:15:01 +0800110 readlinestate *state = get_readline_state(m);
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200111 Py_CLEAR(state->completion_display_matches_hook);
112 Py_CLEAR(state->startup_hook);
113 Py_CLEAR(state->pre_input_hook);
114 Py_CLEAR(state->completer);
115 Py_CLEAR(state->begidx);
116 Py_CLEAR(state->endidx);
117 return 0;
118}
119
120static int
121readline_traverse(PyObject *m, visitproc visit, void *arg)
122{
Hai Shif707d942020-03-16 21:15:01 +0800123 readlinestate *state = get_readline_state(m);
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200124 Py_VISIT(state->completion_display_matches_hook);
125 Py_VISIT(state->startup_hook);
126 Py_VISIT(state->pre_input_hook);
127 Py_VISIT(state->completer);
128 Py_VISIT(state->begidx);
129 Py_VISIT(state->endidx);
130 return 0;
131}
132
133static void
134readline_free(void *m)
135{
136 readline_clear((PyObject *)m);
137}
138
139static PyModuleDef readlinemodule;
140
141#define readlinestate_global ((readlinestate *)PyModule_GetState(PyState_FindModule(&readlinemodule)))
142
143
Martin Panterf00c49d2016-06-14 01:16:16 +0000144/* Convert to/from multibyte C strings */
145
146static PyObject *
147encode(PyObject *b)
148{
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100149 return PyUnicode_EncodeLocale(b, "surrogateescape");
Martin Panterf00c49d2016-06-14 01:16:16 +0000150}
151
152static PyObject *
153decode(const char *s)
154{
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100155 return PyUnicode_DecodeLocale(s, "surrogateescape");
Martin Panterf00c49d2016-06-14 01:16:16 +0000156}
157
158
Guido van Rossum290900a1997-09-26 21:51:21 +0000159/* Exported function to send one line to readline's init file parser */
160
Zackery Spytzb7047e52020-07-12 10:01:03 -0600161/*[clinic input]
162readline.parse_and_bind
163
164 string: object
165 /
166
167Execute the init line provided in the string argument.
168[clinic start generated code]*/
169
Guido van Rossum290900a1997-09-26 21:51:21 +0000170static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600171readline_parse_and_bind(PyObject *module, PyObject *string)
172/*[clinic end generated code: output=1a1ede8afb9546c1 input=8a28a00bb4d61eec]*/
Guido van Rossum290900a1997-09-26 21:51:21 +0000173{
Martin Panterf00c49d2016-06-14 01:16:16 +0000174 char *copy;
175 PyObject *encoded = encode(string);
176 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 return NULL;
Martin Panterf00c49d2016-06-14 01:16:16 +0000178 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179 /* Make a copy -- rl_parse_and_bind() modifies its argument */
180 /* Bernard Herzog */
Martin Panterf00c49d2016-06-14 01:16:16 +0000181 copy = PyMem_Malloc(1 + PyBytes_GET_SIZE(encoded));
182 if (copy == NULL) {
183 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 return PyErr_NoMemory();
Martin Panterf00c49d2016-06-14 01:16:16 +0000185 }
186 strcpy(copy, PyBytes_AS_STRING(encoded));
187 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 rl_parse_and_bind(copy);
Victor Stinnerb6404912013-07-07 16:21:41 +0200189 PyMem_Free(copy); /* Free the copy */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +0000191}
192
Guido van Rossum290900a1997-09-26 21:51:21 +0000193/* Exported function to parse a readline init file */
194
Zackery Spytzb7047e52020-07-12 10:01:03 -0600195/*[clinic input]
196readline.read_init_file
197
198 filename as filename_obj: object = None
199 /
200
201Execute a readline initialization file.
202
203The default filename is the last filename used.
204[clinic start generated code]*/
205
Guido van Rossum290900a1997-09-26 21:51:21 +0000206static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600207readline_read_init_file_impl(PyObject *module, PyObject *filename_obj)
208/*[clinic end generated code: output=8e059b676142831e input=4c80c473e448139d]*/
Guido van Rossum290900a1997-09-26 21:51:21 +0000209{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600210 PyObject *filename_bytes;
Victor Stinner19e65a32010-06-11 22:27:14 +0000211 if (filename_obj != Py_None) {
212 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
213 return NULL;
Zackery Spytzb7047e52020-07-12 10:01:03 -0600214 errno = rl_read_init_file(PyBytes_AS_STRING(filename_bytes));
Victor Stinner19e65a32010-06-11 22:27:14 +0000215 Py_DECREF(filename_bytes);
216 } else
217 errno = rl_read_init_file(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 if (errno)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300219 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +0000221}
222
Skip Montanaro28067822000-07-06 18:55:12 +0000223/* Exported function to load a readline history file */
224
Zackery Spytzb7047e52020-07-12 10:01:03 -0600225/*[clinic input]
226readline.read_history_file
227
228 filename as filename_obj: object = None
229 /
230
231Load a readline history file.
232
233The default filename is ~/.history.
234[clinic start generated code]*/
235
Skip Montanaro28067822000-07-06 18:55:12 +0000236static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600237readline_read_history_file_impl(PyObject *module, PyObject *filename_obj)
238/*[clinic end generated code: output=66a951836fb54fbb input=3d29d755b7e6932e]*/
Skip Montanaro28067822000-07-06 18:55:12 +0000239{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600240 PyObject *filename_bytes;
Victor Stinner19e65a32010-06-11 22:27:14 +0000241 if (filename_obj != Py_None) {
242 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
243 return NULL;
Zackery Spytzb7047e52020-07-12 10:01:03 -0600244 errno = read_history(PyBytes_AS_STRING(filename_bytes));
Victor Stinner19e65a32010-06-11 22:27:14 +0000245 Py_DECREF(filename_bytes);
246 } else
247 errno = read_history(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 if (errno)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300249 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000251}
252
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000253static int _history_length = -1; /* do not truncate history by default */
Skip Montanaro28067822000-07-06 18:55:12 +0000254
255/* Exported function to save a readline history file */
256
Zackery Spytzb7047e52020-07-12 10:01:03 -0600257/*[clinic input]
258readline.write_history_file
259
260 filename as filename_obj: object = None
261 /
262
263Save a readline history file.
264
265The default filename is ~/.history.
266[clinic start generated code]*/
267
Skip Montanaro28067822000-07-06 18:55:12 +0000268static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600269readline_write_history_file_impl(PyObject *module, PyObject *filename_obj)
270/*[clinic end generated code: output=fbcad13d8ef59ae6 input=28a8e062fe363703]*/
Skip Montanaro28067822000-07-06 18:55:12 +0000271{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600272 PyObject *filename_bytes;
Serhiy Storchaka8f87eef2020-04-12 14:58:27 +0300273 const char *filename;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100274 int err;
Victor Stinner19e65a32010-06-11 22:27:14 +0000275 if (filename_obj != Py_None) {
276 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
277 return NULL;
Zackery Spytzb7047e52020-07-12 10:01:03 -0600278 filename = PyBytes_AS_STRING(filename_bytes);
Victor Stinner19e65a32010-06-11 22:27:14 +0000279 } else {
280 filename_bytes = NULL;
281 filename = NULL;
282 }
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100283 errno = err = write_history(filename);
284 if (!err && _history_length >= 0)
Victor Stinner19e65a32010-06-11 22:27:14 +0000285 history_truncate_file(filename, _history_length);
286 Py_XDECREF(filename_bytes);
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100287 errno = err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 if (errno)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300289 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000291}
292
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600293#ifdef HAVE_RL_APPEND_HISTORY
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600294/* Exported function to save part of a readline history file */
295
Zackery Spytzb7047e52020-07-12 10:01:03 -0600296/*[clinic input]
297readline.append_history_file
298
299 nelements: int
300 filename as filename_obj: object = None
301 /
302
303Append the last nelements items of the history list to file.
304
305The default filename is ~/.history.
306[clinic start generated code]*/
307
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600308static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600309readline_append_history_file_impl(PyObject *module, int nelements,
310 PyObject *filename_obj)
311/*[clinic end generated code: output=5df06fc9da56e4e4 input=784b774db3a4b7c5]*/
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600312{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600313 PyObject *filename_bytes;
Serhiy Storchaka8f87eef2020-04-12 14:58:27 +0300314 const char *filename;
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600315 int err;
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600316 if (filename_obj != Py_None) {
317 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
318 return NULL;
Zackery Spytzb7047e52020-07-12 10:01:03 -0600319 filename = PyBytes_AS_STRING(filename_bytes);
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600320 } else {
321 filename_bytes = NULL;
322 filename = NULL;
323 }
Gregory P. Smithfd053fd2021-02-12 12:04:46 -0800324 errno = err = append_history(
325 nelements - libedit_append_replace_history_offset, filename);
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600326 if (!err && _history_length >= 0)
327 history_truncate_file(filename, _history_length);
328 Py_XDECREF(filename_bytes);
329 errno = err;
330 if (errno)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300331 return PyErr_SetFromErrno(PyExc_OSError);
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600332 Py_RETURN_NONE;
333}
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600334#endif
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600335
336
Guido van Rossum74f31432003-01-07 20:01:29 +0000337/* Set history length */
338
Zackery Spytzb7047e52020-07-12 10:01:03 -0600339/*[clinic input]
340readline.set_history_length
341
342 length: int
343 /
344
345Set the maximal number of lines which will be written to the history file.
346
347A negative length is used to inhibit history truncation.
348[clinic start generated code]*/
349
350static PyObject *
351readline_set_history_length_impl(PyObject *module, int length)
352/*[clinic end generated code: output=e161a53e45987dc7 input=b8901bf16488b760]*/
Guido van Rossum74f31432003-01-07 20:01:29 +0000353{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 _history_length = length;
355 Py_RETURN_NONE;
Guido van Rossum74f31432003-01-07 20:01:29 +0000356}
357
Guido van Rossum74f31432003-01-07 20:01:29 +0000358/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000359
Zackery Spytzb7047e52020-07-12 10:01:03 -0600360/*[clinic input]
361readline.get_history_length
362
363Return the maximum number of lines that will be written to the history file.
364[clinic start generated code]*/
365
366static PyObject *
367readline_get_history_length_impl(PyObject *module)
368/*[clinic end generated code: output=83a2eeae35b6d2b9 input=5dce2eeba4327817]*/
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000369{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 return PyLong_FromLong(_history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000371}
372
Martin v. Löwis0daad592001-09-30 21:09:59 +0000373/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000374
Martin v. Löwis0daad592001-09-30 21:09:59 +0000375static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600376set_hook(const char *funcname, PyObject **hook_var, PyObject *function)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000377{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 if (function == Py_None) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200379 Py_CLEAR(*hook_var);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 }
381 else if (PyCallable_Check(function)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 Py_INCREF(function);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300383 Py_XSETREF(*hook_var, function);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 }
385 else {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100386 PyErr_Format(PyExc_TypeError,
387 "set_%.50s(func): argument not callable",
388 funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 return NULL;
390 }
391 Py_RETURN_NONE;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000392}
393
Zackery Spytzb7047e52020-07-12 10:01:03 -0600394/*[clinic input]
395readline.set_completion_display_matches_hook
396
397 function: object = None
398 /
399
400Set or remove the completion display function.
401
402The function is called as
403 function(substitution, [matches], longest_match_length)
404once each time matches need to be displayed.
405[clinic start generated code]*/
Guido van Rossum74f31432003-01-07 20:01:29 +0000406
Martin v. Löwis0daad592001-09-30 21:09:59 +0000407static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600408readline_set_completion_display_matches_hook_impl(PyObject *module,
409 PyObject *function)
410/*[clinic end generated code: output=516e5cb8db75a328 input=4f0bfd5ab0179a26]*/
Thomas Wouters89d996e2007-09-08 17:39:28 +0000411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 PyObject *result = set_hook("completion_display_matches_hook",
Zackery Spytzb7047e52020-07-12 10:01:03 -0600413 &readlinestate_global->completion_display_matches_hook,
414 function);
Christian Heimes32fbe592007-11-12 15:01:33 +0000415#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 /* We cannot set this hook globally, since it replaces the
417 default completion display. */
418 rl_completion_display_matches_hook =
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200419 readlinestate_global->completion_display_matches_hook ?
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000420#if defined(_RL_FUNCTION_TYPEDEF)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000422#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 (VFunction *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000424#endif
Christian Heimes32fbe592007-11-12 15:01:33 +0000425#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 return result;
Christian Heimes32fbe592007-11-12 15:01:33 +0000427
Thomas Wouters89d996e2007-09-08 17:39:28 +0000428}
429
Zackery Spytzb7047e52020-07-12 10:01:03 -0600430/*[clinic input]
431readline.set_startup_hook
432
433 function: object = None
434 /
435
436Set or remove the function invoked by the rl_startup_hook callback.
437
438The function is called with no arguments just
439before readline prints the first prompt.
440[clinic start generated code]*/
Thomas Wouters89d996e2007-09-08 17:39:28 +0000441
442static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600443readline_set_startup_hook_impl(PyObject *module, PyObject *function)
444/*[clinic end generated code: output=02cd0e0c4fa082ad input=7783b4334b26d16d]*/
Martin v. Löwis0daad592001-09-30 21:09:59 +0000445{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600446 return set_hook("startup_hook", &readlinestate_global->startup_hook,
447 function);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000448}
449
Martin v. Löwis0daad592001-09-30 21:09:59 +0000450#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000451
452/* Set pre-input hook */
453
Zackery Spytzb7047e52020-07-12 10:01:03 -0600454/*[clinic input]
455readline.set_pre_input_hook
456
457 function: object = None
458 /
459
460Set or remove the function invoked by the rl_pre_input_hook callback.
461
462The function is called with no arguments after the first prompt
463has been printed and just before readline starts reading input
464characters.
465[clinic start generated code]*/
466
Martin v. Löwis0daad592001-09-30 21:09:59 +0000467static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600468readline_set_pre_input_hook_impl(PyObject *module, PyObject *function)
469/*[clinic end generated code: output=fe1a96505096f464 input=4f3eaeaf7ce1fdbe]*/
Martin v. Löwis0daad592001-09-30 21:09:59 +0000470{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600471 return set_hook("pre_input_hook", &readlinestate_global->pre_input_hook,
472 function);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000473}
Martin v. Löwis0daad592001-09-30 21:09:59 +0000474#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000475
Guido van Rossum74f31432003-01-07 20:01:29 +0000476
Thomas Wouters89d996e2007-09-08 17:39:28 +0000477/* Get the completion type for the scope of the tab-completion */
Zackery Spytzb7047e52020-07-12 10:01:03 -0600478
479/*[clinic input]
480readline.get_completion_type
481
482Get the type of completion being attempted.
483[clinic start generated code]*/
484
Thomas Wouters89d996e2007-09-08 17:39:28 +0000485static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600486readline_get_completion_type_impl(PyObject *module)
487/*[clinic end generated code: output=5c54d58a04997c07 input=04b92bc7a82dac91]*/
Thomas Wouters89d996e2007-09-08 17:39:28 +0000488{
Christian Heimes217cfd12007-12-02 14:31:20 +0000489 return PyLong_FromLong(rl_completion_type);
Thomas Wouters89d996e2007-09-08 17:39:28 +0000490}
491
Guido van Rossum74f31432003-01-07 20:01:29 +0000492/* Get the beginning index for the scope of the tab-completion */
493
Zackery Spytzb7047e52020-07-12 10:01:03 -0600494/*[clinic input]
495readline.get_begidx
496
497Get the beginning index of the completion scope.
498[clinic start generated code]*/
499
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000500static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600501readline_get_begidx_impl(PyObject *module)
502/*[clinic end generated code: output=362616ee8ed1b2b1 input=e083b81c8eb4bac3]*/
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000503{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200504 Py_INCREF(readlinestate_global->begidx);
505 return readlinestate_global->begidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000506}
507
Guido van Rossum74f31432003-01-07 20:01:29 +0000508/* Get the ending index for the scope of the tab-completion */
509
Zackery Spytzb7047e52020-07-12 10:01:03 -0600510/*[clinic input]
511readline.get_endidx
512
513Get the ending index of the completion scope.
514[clinic start generated code]*/
515
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000516static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600517readline_get_endidx_impl(PyObject *module)
518/*[clinic end generated code: output=7f763350b12d7517 input=d4c7e34a625fd770]*/
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000519{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200520 Py_INCREF(readlinestate_global->endidx);
521 return readlinestate_global->endidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000522}
523
Guido van Rossum74f31432003-01-07 20:01:29 +0000524/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000525
Zackery Spytzb7047e52020-07-12 10:01:03 -0600526/*[clinic input]
527readline.set_completer_delims
528
529 string: object
530 /
531
532Set the word delimiters for completion.
533[clinic start generated code]*/
534
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000535static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600536readline_set_completer_delims(PyObject *module, PyObject *string)
537/*[clinic end generated code: output=4305b266106c4f1f input=ae945337ebd01e20]*/
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000538{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 char *break_chars;
Martin Panterf00c49d2016-06-14 01:16:16 +0000540 PyObject *encoded = encode(string);
541 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 return NULL;
543 }
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200544 /* Keep a reference to the allocated memory in the module state in case
545 some other module modifies rl_completer_word_break_characters
546 (see issue #17289). */
Martin Panterf00c49d2016-06-14 01:16:16 +0000547 break_chars = strdup(PyBytes_AS_STRING(encoded));
548 Py_DECREF(encoded);
Serhiy Storchaka11384392015-09-27 22:34:59 +0300549 if (break_chars) {
550 free(completer_word_break_characters);
551 completer_word_break_characters = break_chars;
552 rl_completer_word_break_characters = break_chars;
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200553 Py_RETURN_NONE;
554 }
555 else
556 return PyErr_NoMemory();
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000557}
558
Mark Dickinson29b238e2010-08-03 16:08:16 +0000559/* _py_free_history_entry: Utility function to free a history entry. */
560
561#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
562
563/* Readline version >= 5.0 introduced a timestamp field into the history entry
564 structure; this needs to be freed to avoid a memory leak. This version of
565 readline also introduced the handy 'free_history_entry' function, which
566 takes care of the timestamp. */
567
568static void
569_py_free_history_entry(HIST_ENTRY *entry)
570{
571 histdata_t data = free_history_entry(entry);
572 free(data);
573}
574
575#else
576
577/* No free_history_entry function; free everything manually. */
578
579static void
580_py_free_history_entry(HIST_ENTRY *entry)
581{
582 if (entry->line)
583 free((void *)entry->line);
584 if (entry->data)
585 free(entry->data);
586 free(entry);
587}
588
589#endif
590
Zackery Spytzb7047e52020-07-12 10:01:03 -0600591/*[clinic input]
592readline.remove_history_item
593
594 pos as entry_number: int
595 /
596
Gregory P. Smithfd053fd2021-02-12 12:04:46 -0800597Remove history item given by its zero-based position.
Zackery Spytzb7047e52020-07-12 10:01:03 -0600598[clinic start generated code]*/
599
Skip Montanaroe5069012004-08-15 14:32:06 +0000600static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600601readline_remove_history_item_impl(PyObject *module, int entry_number)
Gregory P. Smithfd053fd2021-02-12 12:04:46 -0800602/*[clinic end generated code: output=ab114f029208c7e8 input=f248beb720ff1838]*/
Skip Montanaroe5069012004-08-15 14:32:06 +0000603{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 HIST_ENTRY *entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 if (entry_number < 0) {
607 PyErr_SetString(PyExc_ValueError,
608 "History index cannot be negative");
609 return NULL;
610 }
611 entry = remove_history(entry_number);
612 if (!entry) {
613 PyErr_Format(PyExc_ValueError,
614 "No history item at position %d",
615 entry_number);
616 return NULL;
617 }
618 /* free memory allocated for the history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000619 _py_free_history_entry(entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000621}
622
Zackery Spytzb7047e52020-07-12 10:01:03 -0600623/*[clinic input]
624readline.replace_history_item
625
626 pos as entry_number: int
627 line: unicode
628 /
629
630Replaces history item given by its position with contents of line.
Gregory P. Smithfd053fd2021-02-12 12:04:46 -0800631
632pos is zero-based.
Zackery Spytzb7047e52020-07-12 10:01:03 -0600633[clinic start generated code]*/
Skip Montanaroe5069012004-08-15 14:32:06 +0000634
635static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600636readline_replace_history_item_impl(PyObject *module, int entry_number,
637 PyObject *line)
Gregory P. Smithfd053fd2021-02-12 12:04:46 -0800638/*[clinic end generated code: output=f8cec2770ca125eb input=368bb66fe5ee5222]*/
Skip Montanaroe5069012004-08-15 14:32:06 +0000639{
Martin Panterf00c49d2016-06-14 01:16:16 +0000640 PyObject *encoded;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 HIST_ENTRY *old_entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 if (entry_number < 0) {
644 PyErr_SetString(PyExc_ValueError,
645 "History index cannot be negative");
646 return NULL;
647 }
Martin Panterf00c49d2016-06-14 01:16:16 +0000648 encoded = encode(line);
649 if (encoded == NULL) {
650 return NULL;
651 }
Gregory P. Smithfd053fd2021-02-12 12:04:46 -0800652 old_entry = replace_history_entry(
653 entry_number + libedit_append_replace_history_offset,
654 PyBytes_AS_STRING(encoded), (void *)NULL);
Martin Panterf00c49d2016-06-14 01:16:16 +0000655 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 if (!old_entry) {
657 PyErr_Format(PyExc_ValueError,
658 "No history item at position %d",
659 entry_number);
660 return NULL;
661 }
662 /* free memory allocated for the old history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000663 _py_free_history_entry(old_entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000665}
666
Guido van Rossum74f31432003-01-07 20:01:29 +0000667/* Add a line to the history buffer */
668
Zackery Spytzb7047e52020-07-12 10:01:03 -0600669/*[clinic input]
670readline.add_history
671
672 string: object
673 /
674
675Add an item to the history buffer.
676[clinic start generated code]*/
677
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000678static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600679readline_add_history(PyObject *module, PyObject *string)
680/*[clinic end generated code: output=b107b7e8106e803d input=e57c1cf6bc68d7e3]*/
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000681{
Martin Panterf00c49d2016-06-14 01:16:16 +0000682 PyObject *encoded = encode(string);
683 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 return NULL;
685 }
Martin Panterf00c49d2016-06-14 01:16:16 +0000686 add_history(PyBytes_AS_STRING(encoded));
687 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 Py_RETURN_NONE;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000689}
690
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000691static int should_auto_add_history = 1;
692
693/* Enable or disable automatic history */
694
Zackery Spytzb7047e52020-07-12 10:01:03 -0600695/*[clinic input]
696readline.set_auto_history
697
698 enabled as _should_auto_add_history: bool
699 /
700
701Enables or disables automatic history.
702[clinic start generated code]*/
703
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000704static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600705readline_set_auto_history_impl(PyObject *module,
706 int _should_auto_add_history)
707/*[clinic end generated code: output=619c6968246fd82b input=3d413073a1a03355]*/
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000708{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600709 should_auto_add_history = _should_auto_add_history;
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000710 Py_RETURN_NONE;
711}
712
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000713
Guido van Rossum74f31432003-01-07 20:01:29 +0000714/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000715
Zackery Spytzb7047e52020-07-12 10:01:03 -0600716/*[clinic input]
717readline.get_completer_delims
718
719Get the word delimiters for completion.
720[clinic start generated code]*/
721
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000722static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600723readline_get_completer_delims_impl(PyObject *module)
724/*[clinic end generated code: output=6b060280fa68ef43 input=e36eb14fb8a1f08a]*/
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000725{
Martin Panterf00c49d2016-06-14 01:16:16 +0000726 return decode(rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000727}
Guido van Rossum74f31432003-01-07 20:01:29 +0000728
Guido van Rossum74f31432003-01-07 20:01:29 +0000729/* Set the completer function */
730
Zackery Spytzb7047e52020-07-12 10:01:03 -0600731/*[clinic input]
732readline.set_completer
733
734 function: object = None
735 /
736
737Set or remove the completer function.
738
739The function is called as function(text, state),
740for state in 0, 1, 2, ..., until it returns a non-string.
741It should return the next possible completion starting with 'text'.
742[clinic start generated code]*/
743
Guido van Rossum290900a1997-09-26 21:51:21 +0000744static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600745readline_set_completer_impl(PyObject *module, PyObject *function)
746/*[clinic end generated code: output=171a2a60f81d3204 input=51e81e13118eb877]*/
Guido van Rossum290900a1997-09-26 21:51:21 +0000747{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600748 return set_hook("completer", &readlinestate_global->completer, function);
Guido van Rossum290900a1997-09-26 21:51:21 +0000749}
750
Zackery Spytzb7047e52020-07-12 10:01:03 -0600751/*[clinic input]
752readline.get_completer
Guido van Rossum290900a1997-09-26 21:51:21 +0000753
Zackery Spytzb7047e52020-07-12 10:01:03 -0600754Get the current completer function.
755[clinic start generated code]*/
Guido van Rossum74f31432003-01-07 20:01:29 +0000756
Michael W. Hudson796df152003-01-30 10:12:51 +0000757static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600758readline_get_completer_impl(PyObject *module)
759/*[clinic end generated code: output=6e6bbd8226d14475 input=6457522e56d70d13]*/
Michael W. Hudson796df152003-01-30 10:12:51 +0000760{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200761 if (readlinestate_global->completer == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 Py_RETURN_NONE;
763 }
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200764 Py_INCREF(readlinestate_global->completer);
765 return readlinestate_global->completer;
Michael W. Hudson796df152003-01-30 10:12:51 +0000766}
767
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000768/* Private function to get current length of history. XXX It may be
769 * possible to replace this with a direct use of history_length instead,
770 * but it's not clear whether BSD's libedit keeps history_length up to date.
771 * See issue #8065.*/
772
773static int
774_py_get_history_length(void)
775{
776 HISTORY_STATE *hist_st = history_get_history_state();
777 int length = hist_st->length;
778 /* the history docs don't say so, but the address of hist_st changes each
779 time history_get_history_state is called which makes me think it's
780 freshly malloc'd memory... on the other hand, the address of the last
781 line stays the same as long as history isn't extended, so it appears to
782 be malloc'd but managed by the history package... */
783 free(hist_st);
784 return length;
785}
786
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000787/* Exported function to get any element of history */
788
Zackery Spytzb7047e52020-07-12 10:01:03 -0600789/*[clinic input]
790readline.get_history_item
791
792 index as idx: int
793 /
794
Gregory P. Smithfd053fd2021-02-12 12:04:46 -0800795Return the current contents of history item at one-based index.
Zackery Spytzb7047e52020-07-12 10:01:03 -0600796[clinic start generated code]*/
797
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000798static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600799readline_get_history_item_impl(PyObject *module, int idx)
Gregory P. Smithfd053fd2021-02-12 12:04:46 -0800800/*[clinic end generated code: output=83d3e53ea5f34b3d input=8adf5c80e6c7ff2b]*/
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000801{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 HIST_ENTRY *hist_ent;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 if (using_libedit_emulation) {
Ned Deilyf70f4a62013-09-06 15:16:19 -0700805 /* Older versions of libedit's readline emulation
806 * use 0-based indexes, while readline and newer
807 * versions of libedit use 1-based indexes.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 */
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000809 int length = _py_get_history_length();
Ned Deilyf70f4a62013-09-06 15:16:19 -0700810
811 idx = idx - 1 + libedit_history_start;
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 /*
814 * Apple's readline emulation crashes when
815 * the index is out of range, therefore
816 * test for that and fail gracefully.
817 */
Ned Deilyf70f4a62013-09-06 15:16:19 -0700818 if (idx < (0 + libedit_history_start)
819 || idx >= (length + libedit_history_start)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 Py_RETURN_NONE;
821 }
822 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 if ((hist_ent = history_get(idx)))
Martin Panterf00c49d2016-06-14 01:16:16 +0000824 return decode(hist_ent->line);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 else {
826 Py_RETURN_NONE;
827 }
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000828}
829
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000830/* Exported function to get current length of history */
831
Zackery Spytzb7047e52020-07-12 10:01:03 -0600832/*[clinic input]
833readline.get_current_history_length
834
835Return the current (not the maximum) length of history.
836[clinic start generated code]*/
837
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000838static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600839readline_get_current_history_length_impl(PyObject *module)
840/*[clinic end generated code: output=436b294f12ba1e3f input=9cb3f431a68d071f]*/
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000841{
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000842 return PyLong_FromLong((long)_py_get_history_length());
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000843}
844
Guido van Rossum79378ff1997-10-07 14:53:21 +0000845/* Exported function to read the current line buffer */
846
Zackery Spytzb7047e52020-07-12 10:01:03 -0600847/*[clinic input]
848readline.get_line_buffer
849
850Return the current contents of the line buffer.
851[clinic start generated code]*/
852
Guido van Rossum79378ff1997-10-07 14:53:21 +0000853static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600854readline_get_line_buffer_impl(PyObject *module)
855/*[clinic end generated code: output=d22f9025ecad80e4 input=5f5fbc0d12c69412]*/
Guido van Rossum79378ff1997-10-07 14:53:21 +0000856{
Martin Panterf00c49d2016-06-14 01:16:16 +0000857 return decode(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000858}
859
Martin v. Löwise7a97962003-09-20 16:08:33 +0000860#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
861
862/* Exported function to clear the current history */
863
Zackery Spytzb7047e52020-07-12 10:01:03 -0600864/*[clinic input]
865readline.clear_history
866
867Clear the current readline history.
868[clinic start generated code]*/
869
Martin v. Löwise7a97962003-09-20 16:08:33 +0000870static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600871readline_clear_history_impl(PyObject *module)
872/*[clinic end generated code: output=1f2dbb0dfa5d5ebb input=208962c4393f5d16]*/
Martin v. Löwise7a97962003-09-20 16:08:33 +0000873{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 clear_history();
875 Py_RETURN_NONE;
Martin v. Löwise7a97962003-09-20 16:08:33 +0000876}
Martin v. Löwise7a97962003-09-20 16:08:33 +0000877#endif
878
879
Guido van Rossum79378ff1997-10-07 14:53:21 +0000880/* Exported function to insert text into the line buffer */
881
Zackery Spytzb7047e52020-07-12 10:01:03 -0600882/*[clinic input]
883readline.insert_text
884
885 string: object
886 /
887
888Insert text into the line buffer at the cursor position.
889[clinic start generated code]*/
890
Guido van Rossum79378ff1997-10-07 14:53:21 +0000891static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600892readline_insert_text(PyObject *module, PyObject *string)
893/*[clinic end generated code: output=23d792821d320c19 input=bc96c3c848d5ccb5]*/
Guido van Rossum79378ff1997-10-07 14:53:21 +0000894{
Martin Panterf00c49d2016-06-14 01:16:16 +0000895 PyObject *encoded = encode(string);
896 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 return NULL;
Martin Panterf00c49d2016-06-14 01:16:16 +0000898 }
899 rl_insert_text(PyBytes_AS_STRING(encoded));
900 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 Py_RETURN_NONE;
Guido van Rossum79378ff1997-10-07 14:53:21 +0000902}
903
Guido van Rossum74f31432003-01-07 20:01:29 +0000904/* Redisplay the line buffer */
905
Zackery Spytzb7047e52020-07-12 10:01:03 -0600906/*[clinic input]
907readline.redisplay
908
909Change what's displayed on the screen to reflect contents of the line buffer.
910[clinic start generated code]*/
911
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000912static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600913readline_redisplay_impl(PyObject *module)
914/*[clinic end generated code: output=a8b9725827c3c34b input=b485151058d75edc]*/
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000915{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 rl_redisplay();
917 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000918}
919
Zackery Spytzb7047e52020-07-12 10:01:03 -0600920#include "clinic/readline.c.h"
Guido van Rossum74f31432003-01-07 20:01:29 +0000921
Guido van Rossum290900a1997-09-26 21:51:21 +0000922/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000923
924static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000925{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600926 READLINE_PARSE_AND_BIND_METHODDEF
927 READLINE_GET_LINE_BUFFER_METHODDEF
928 READLINE_INSERT_TEXT_METHODDEF
929 READLINE_REDISPLAY_METHODDEF
930 READLINE_READ_INIT_FILE_METHODDEF
931 READLINE_READ_HISTORY_FILE_METHODDEF
932 READLINE_WRITE_HISTORY_FILE_METHODDEF
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600933#ifdef HAVE_RL_APPEND_HISTORY
Zackery Spytzb7047e52020-07-12 10:01:03 -0600934 READLINE_APPEND_HISTORY_FILE_METHODDEF
Ned Deily8007cbc2014-11-26 13:02:33 -0800935#endif
Zackery Spytzb7047e52020-07-12 10:01:03 -0600936 READLINE_GET_HISTORY_ITEM_METHODDEF
937 READLINE_GET_CURRENT_HISTORY_LENGTH_METHODDEF
938 READLINE_SET_HISTORY_LENGTH_METHODDEF
939 READLINE_GET_HISTORY_LENGTH_METHODDEF
940 READLINE_SET_COMPLETER_METHODDEF
941 READLINE_GET_COMPLETER_METHODDEF
942 READLINE_GET_COMPLETION_TYPE_METHODDEF
943 READLINE_GET_BEGIDX_METHODDEF
944 READLINE_GET_ENDIDX_METHODDEF
945 READLINE_SET_COMPLETER_DELIMS_METHODDEF
946 READLINE_SET_AUTO_HISTORY_METHODDEF
947 READLINE_ADD_HISTORY_METHODDEF
948 READLINE_REMOVE_HISTORY_ITEM_METHODDEF
949 READLINE_REPLACE_HISTORY_ITEM_METHODDEF
950 READLINE_GET_COMPLETER_DELIMS_METHODDEF
951 READLINE_SET_COMPLETION_DISPLAY_MATCHES_HOOK_METHODDEF
952 READLINE_SET_STARTUP_HOOK_METHODDEF
Martin v. Löwis0daad592001-09-30 21:09:59 +0000953#ifdef HAVE_RL_PRE_INPUT_HOOK
Zackery Spytzb7047e52020-07-12 10:01:03 -0600954 READLINE_SET_PRE_INPUT_HOOK_METHODDEF
Martin v. Löwis0daad592001-09-30 21:09:59 +0000955#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000956#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Zackery Spytzb7047e52020-07-12 10:01:03 -0600957 READLINE_CLEAR_HISTORY_METHODDEF
Martin v. Löwise7a97962003-09-20 16:08:33 +0000958#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000960};
961
Guido van Rossum05ac4492003-01-07 20:04:12 +0000962
Martin v. Löwis0daad592001-09-30 21:09:59 +0000963/* C function to call the Python hooks. */
964
965static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000966on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000967{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 int result = 0;
969 if (func != NULL) {
970 PyObject *r;
Victor Stinner2ff58a22019-06-17 14:27:23 +0200971 r = PyObject_CallNoArgs(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 if (r == NULL)
973 goto error;
974 if (r == Py_None)
975 result = 0;
976 else {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300977 result = _PyLong_AsInt(r);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 if (result == -1 && PyErr_Occurred())
979 goto error;
980 }
981 Py_DECREF(r);
982 goto done;
983 error:
984 PyErr_Clear();
985 Py_XDECREF(r);
986 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 return result;
988 }
989 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000990}
991
992static int
Ned Deily7b9ddea2014-02-05 16:53:10 -0800993#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000994on_startup_hook(void)
Ned Deily7b9ddea2014-02-05 16:53:10 -0800995#else
996on_startup_hook()
997#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000998{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200999 int r;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001000 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001001 r = on_hook(readlinestate_global->startup_hook);
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001002 PyGILState_Release(gilstate);
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001003 return r;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001004}
1005
1006#ifdef HAVE_RL_PRE_INPUT_HOOK
1007static int
Ned Deily7b9ddea2014-02-05 16:53:10 -08001008#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwis0daad592001-09-30 21:09:59 +00001009on_pre_input_hook(void)
Ned Deily7b9ddea2014-02-05 16:53:10 -08001010#else
1011on_pre_input_hook()
1012#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +00001013{
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001014 int r;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001015 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001016 r = on_hook(readlinestate_global->pre_input_hook);
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001017 PyGILState_Release(gilstate);
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001018 return r;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001019}
1020#endif
1021
Guido van Rossum05ac4492003-01-07 20:04:12 +00001022
Thomas Wouters89d996e2007-09-08 17:39:28 +00001023/* C function to call the Python completion_display_matches */
1024
Georg Brandl646fdd62010-10-18 07:27:55 +00001025#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Thomas Wouters89d996e2007-09-08 17:39:28 +00001026static void
1027on_completion_display_matches_hook(char **matches,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 int num_matches, int max_length)
Thomas Wouters89d996e2007-09-08 17:39:28 +00001029{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 int i;
Martin Panterf00c49d2016-06-14 01:16:16 +00001031 PyObject *sub, *m=NULL, *s=NULL, *r=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 m = PyList_New(num_matches);
1034 if (m == NULL)
1035 goto error;
1036 for (i = 0; i < num_matches; i++) {
Martin Panterf00c49d2016-06-14 01:16:16 +00001037 s = decode(matches[i+1]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 if (s == NULL)
1039 goto error;
Zackery Spytz99d56b52018-12-08 07:16:55 -07001040 PyList_SET_ITEM(m, i, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 }
Martin Panterf00c49d2016-06-14 01:16:16 +00001042 sub = decode(matches[0]);
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001043 r = PyObject_CallFunction(readlinestate_global->completion_display_matches_hook,
Martin Panterf00c49d2016-06-14 01:16:16 +00001044 "NNi", sub, m, max_length);
Christian Heimes32fbe592007-11-12 15:01:33 +00001045
Martin Panterf00c49d2016-06-14 01:16:16 +00001046 m=NULL;
Christian Heimes32fbe592007-11-12 15:01:33 +00001047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 if (r == NULL ||
1049 (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) {
1050 goto error;
1051 }
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001052 Py_CLEAR(r);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053
1054 if (0) {
1055 error:
1056 PyErr_Clear();
1057 Py_XDECREF(m);
1058 Py_XDECREF(r);
1059 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 PyGILState_Release(gilstate);
Thomas Wouters89d996e2007-09-08 17:39:28 +00001061}
1062
Senthil Kumaran95c07002010-11-04 03:51:05 +00001063#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +00001064
Martin Panter5dbbf1a2016-04-03 02:54:58 +00001065#ifdef HAVE_RL_RESIZE_TERMINAL
1066static volatile sig_atomic_t sigwinch_received;
Martin Panter28f35b22016-04-03 08:00:49 +00001067static PyOS_sighandler_t sigwinch_ohandler;
Martin Panter5dbbf1a2016-04-03 02:54:58 +00001068
1069static void
1070readline_sigwinch_handler(int signum)
1071{
1072 sigwinch_received = 1;
1073 if (sigwinch_ohandler &&
1074 sigwinch_ohandler != SIG_IGN && sigwinch_ohandler != SIG_DFL)
1075 sigwinch_ohandler(signum);
1076
1077#ifndef HAVE_SIGACTION
1078 /* If the handler was installed with signal() rather than sigaction(),
1079 we need to reinstall it. */
1080 PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
1081#endif
1082}
1083#endif
1084
Guido van Rossum290900a1997-09-26 21:51:21 +00001085/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +00001086
Guido van Rossum290900a1997-09-26 21:51:21 +00001087static char *
Guido van Rossumd8faa362007-04-27 19:54:29 +00001088on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +00001089{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 char *result = NULL;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001091 if (readlinestate_global->completer != NULL) {
Martin Panterf00c49d2016-06-14 01:16:16 +00001092 PyObject *r = NULL, *t;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 rl_attempted_completion_over = 1;
Martin Panterf00c49d2016-06-14 01:16:16 +00001095 t = decode(text);
1096 r = PyObject_CallFunction(readlinestate_global->completer, "Ni", t, state);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 if (r == NULL)
1098 goto error;
1099 if (r == Py_None) {
1100 result = NULL;
1101 }
1102 else {
Martin Panterf00c49d2016-06-14 01:16:16 +00001103 PyObject *encoded = encode(r);
1104 if (encoded == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 goto error;
Martin Panterf00c49d2016-06-14 01:16:16 +00001106 result = strdup(PyBytes_AS_STRING(encoded));
1107 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 }
1109 Py_DECREF(r);
1110 goto done;
1111 error:
1112 PyErr_Clear();
1113 Py_XDECREF(r);
1114 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 PyGILState_Release(gilstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 return result;
1117 }
1118 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +00001119}
1120
Guido van Rossum290900a1997-09-26 21:51:21 +00001121
Guido van Rossum6d0d3652003-01-07 20:34:19 +00001122/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001123 * before calling the normal completer */
1124
Neal Norwitzc355f0c2003-02-21 00:30:18 +00001125static char **
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001126flex_complete(const char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001127{
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001128 char **result;
Martin Panterf00c49d2016-06-14 01:16:16 +00001129 char saved;
1130 size_t start_size, end_size;
1131 wchar_t *s;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001132 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitroudc0900b2009-10-19 18:22:37 +00001133#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 rl_completion_append_character ='\0';
Antoine Pitroud5131772009-10-26 19:22:14 +00001135#endif
1136#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 rl_completion_suppress_append = 0;
Antoine Pitroudc0900b2009-10-19 18:22:37 +00001138#endif
Martin Panterf00c49d2016-06-14 01:16:16 +00001139
1140 saved = rl_line_buffer[start];
1141 rl_line_buffer[start] = 0;
1142 s = Py_DecodeLocale(rl_line_buffer, &start_size);
1143 rl_line_buffer[start] = saved;
1144 if (s == NULL) {
1145 goto done;
1146 }
1147 PyMem_RawFree(s);
1148 saved = rl_line_buffer[end];
1149 rl_line_buffer[end] = 0;
1150 s = Py_DecodeLocale(rl_line_buffer + start, &end_size);
1151 rl_line_buffer[end] = saved;
1152 if (s == NULL) {
1153 goto done;
1154 }
1155 PyMem_RawFree(s);
1156 start = (int)start_size;
1157 end = start + (int)end_size;
1158
1159done:
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001160 Py_XDECREF(readlinestate_global->begidx);
1161 Py_XDECREF(readlinestate_global->endidx);
1162 readlinestate_global->begidx = PyLong_FromLong((long) start);
1163 readlinestate_global->endidx = PyLong_FromLong((long) end);
Benjamin Peterson1bb0c0b2016-09-05 18:26:19 -07001164 result = completion_matches((char *)text, *on_completion);
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001165 PyGILState_Release(gilstate);
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001166 return result;
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001167}
1168
Guido van Rossum05ac4492003-01-07 20:04:12 +00001169
Victor Stinner1d8da612019-10-30 16:39:27 +01001170/* Helper to initialize GNU readline properly.
1171 Return -1 on memory allocation failure, return 0 on success. */
1172static int
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001173setup_readline(readlinestate *mod_state)
Guido van Rossum290900a1997-09-26 21:51:21 +00001174{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +00001175#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Victor Stinner1d8da612019-10-30 16:39:27 +01001177 if (!saved_locale) {
1178 return -1;
1179 }
Guido van Rossum60c8a3a2002-10-09 21:27:33 +00001180#endif
1181
Zvezdan Petkovicc2f082e2018-05-17 02:45:10 -04001182 /* The name must be defined before initialization */
1183 rl_readline_name = "python";
1184
Victor Stinner6ced7c42011-03-21 18:15:42 +01001185 /* the libedit readline emulation resets key bindings etc
R. David Murray52d1b4e2010-12-18 03:48:32 +00001186 * when calling rl_initialize. So call it upfront
1187 */
1188 if (using_libedit_emulation)
1189 rl_initialize();
Ned Deilyf70f4a62013-09-06 15:16:19 -07001190
1191 /* Detect if libedit's readline emulation uses 0-based
1192 * indexing or 1-based indexing.
1193 */
1194 add_history("1");
1195 if (history_get(1) == NULL) {
1196 libedit_history_start = 0;
1197 } else {
1198 libedit_history_start = 1;
1199 }
Gregory P. Smithfd053fd2021-02-12 12:04:46 -08001200 /* Some libedit implementations use 1 based indexing on
1201 * replace_history_entry where libreadline uses 0 based.
1202 * The API our module presents is supposed to be 0 based.
1203 * It's a mad mad mad mad world.
1204 */
1205 {
1206 add_history("2");
1207 HIST_ENTRY *old_entry = replace_history_entry(1, "X", NULL);
1208 _py_free_history_entry(old_entry);
1209 HIST_ENTRY *item = history_get(libedit_history_start);
1210 if (item && item->line && strcmp(item->line, "X")) {
1211 libedit_append_replace_history_offset = 0;
1212 } else {
1213 libedit_append_replace_history_offset = 1;
1214 }
1215 }
Ned Deilyf70f4a62013-09-06 15:16:19 -07001216 clear_history();
R. David Murray52d1b4e2010-12-18 03:48:32 +00001217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 using_history();
Skip Montanaroa0392742002-06-11 14:32:46 +00001219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 /* Force rebind of TAB to insert-tab */
1221 rl_bind_key('\t', rl_insert);
1222 /* Bind both ESC-TAB and ESC-ESC to the completion function */
1223 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
1224 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin Panter5dbbf1a2016-04-03 02:54:58 +00001225#ifdef HAVE_RL_RESIZE_TERMINAL
1226 /* Set up signal handler for window resize */
1227 sigwinch_ohandler = PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
1228#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 /* Set our hook functions */
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001230 rl_startup_hook = on_startup_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001231#ifdef HAVE_RL_PRE_INPUT_HOOK
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001232 rl_pre_input_hook = on_pre_input_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001233#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 /* Set our completion function */
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001235 rl_attempted_completion_function = flex_complete;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 /* Set Python word break characters */
Antoine Pitroua7f7deb2013-05-06 21:51:03 +02001237 completer_word_break_characters =
1238 rl_completer_word_break_characters =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
1240 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001241
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001242 mod_state->begidx = PyLong_FromLong(0L);
1243 mod_state->endidx = PyLong_FromLong(0L);
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001244
Martin Panterc427b8d2016-08-27 03:23:11 +00001245 if (!using_libedit_emulation)
Martin Panterc427b8d2016-08-27 03:23:11 +00001246 {
1247 if (!isatty(STDOUT_FILENO)) {
1248 /* Issue #19884: stdout is not a terminal. Disable meta modifier
1249 keys to not write the ANSI sequence "\033[1034h" into stdout. On
1250 terminals supporting 8 bit characters like TERM=xterm-256color
1251 (which is now the default Fedora since Fedora 18), the meta key is
1252 used to enable support of 8 bit characters (ANSI sequence
1253 "\033[1034h").
1254
1255 With libedit, this call makes readline() crash. */
1256 rl_variable_bind ("enable-meta-key", "off");
1257 }
1258 }
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 /* Initialize (allows .inputrc to override)
1261 *
1262 * XXX: A bug in the readline-2.2 library causes a memory leak
1263 * inside this function. Nothing we can do about it.
1264 */
R. David Murray52d1b4e2010-12-18 03:48:32 +00001265 if (using_libedit_emulation)
Victor Stinner6ced7c42011-03-21 18:15:42 +01001266 rl_read_init_file(NULL);
R. David Murray52d1b4e2010-12-18 03:48:32 +00001267 else
R. David Murray52d1b4e2010-12-18 03:48:32 +00001268 rl_initialize();
Victor Stinner6ced7c42011-03-21 18:15:42 +01001269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 RESTORE_LOCALE(saved_locale)
Victor Stinner1d8da612019-10-30 16:39:27 +01001271 return 0;
Guido van Rossum290900a1997-09-26 21:51:21 +00001272}
1273
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001274/* Wrapper around GNU readline that handles signals differently. */
1275
Antoine Pitrouf474c5a2017-07-18 17:05:03 +02001276static char *completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001277static void
1278rlhandler(char *text)
1279{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 completed_input_string = text;
1281 rl_callback_handler_remove();
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001282}
1283
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001284static char *
Serhiy Storchakac6792272013-10-19 21:03:34 +03001285readline_until_enter_or_signal(const char *prompt, int *signal)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 char * not_done_reading = "";
1288 fd_set selectset;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 *signal = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001291#ifdef HAVE_RL_CATCH_SIGNAL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 rl_catch_signals = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001293#endif
1294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 rl_callback_handler_install (prompt, rlhandler);
1296 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 completed_input_string = not_done_reading;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 while (completed_input_string == not_done_reading) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001301 int has_input = 0, err = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 while (!has_input)
1304 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Michael W. Hudson8da2b012004-10-07 13:46:33 +00001305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 /* [Bug #1552726] Only limit the pause if an input hook has been
1307 defined. */
1308 struct timeval *timeoutp = NULL;
1309 if (PyOS_InputHook)
1310 timeoutp = &timeout;
Martin Panter5dbbf1a2016-04-03 02:54:58 +00001311#ifdef HAVE_RL_RESIZE_TERMINAL
1312 /* Update readline's view of the window size after SIGWINCH */
1313 if (sigwinch_received) {
1314 sigwinch_received = 0;
1315 rl_resize_terminal();
1316 }
1317#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 FD_SET(fileno(rl_instream), &selectset);
1319 /* select resets selectset if no input was available */
1320 has_input = select(fileno(rl_instream) + 1, &selectset,
1321 NULL, NULL, timeoutp);
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001322 err = errno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 if(PyOS_InputHook) PyOS_InputHook();
1324 }
1325
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001326 if (has_input > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 rl_callback_read_char();
1328 }
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001329 else if (err == EINTR) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 int s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 PyEval_RestoreThread(_PyOS_ReadlineTState);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 s = PyErr_CheckSignals();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 PyEval_SaveThread();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 if (s < 0) {
1335 rl_free_line_state();
Martin Panterd6990d22016-03-22 07:24:05 +00001336#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0700
1337 rl_callback_sigcleanup();
1338#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 rl_cleanup_after_signal();
1340 rl_callback_handler_remove();
1341 *signal = 1;
1342 completed_input_string = NULL;
1343 }
1344 }
1345 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 return completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001348}
1349
1350
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001351static char *
Serhiy Storchakac6792272013-10-19 21:03:34 +03001352call_readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001353{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 size_t n;
Victor Stinner1600f602018-11-30 15:03:53 +01001355 char *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 int signal;
Neal Norwitz1fa040b2004-08-25 01:20:18 +00001357
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001358#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1360 if (!saved_locale)
1361 Py_FatalError("not enough memory to save locale");
xdegaye1588be62017-11-12 12:45:59 +01001362 _Py_SetLocaleFromEnv(LC_CTYPE);
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001363#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
1366 rl_instream = sys_stdin;
1367 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001368#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001370#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 }
Guido van Rossum74f31432003-01-07 20:01:29 +00001372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 p = readline_until_enter_or_signal(prompt, &signal);
Guido van Rossumb18618d2000-05-03 23:44:39 +00001374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 /* we got an interrupt signal */
1376 if (signal) {
1377 RESTORE_LOCALE(saved_locale)
1378 return NULL;
1379 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001380
Martin Panter7462b6492015-11-02 03:37:02 +00001381 /* We got an EOF, return an empty string. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 if (p == NULL) {
Victor Stinner2fe9bac2013-10-10 16:18:20 +02001383 p = PyMem_RawMalloc(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 if (p != NULL)
1385 *p = '\0';
1386 RESTORE_LOCALE(saved_locale)
1387 return p;
1388 }
1389
1390 /* we have a valid line */
1391 n = strlen(p);
Martin Panterf0dbf7a2016-05-15 01:26:25 +00001392 if (should_auto_add_history && n > 0) {
Brett Cannon2525dc82010-08-22 20:36:25 +00001393 const char *line;
Mark Dickinson6b54e1f2010-08-03 16:49:49 +00001394 int length = _py_get_history_length();
Nir Sofferfae8f4a2017-07-07 09:10:46 +03001395 if (length > 0) {
1396 HIST_ENTRY *hist_ent;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 if (using_libedit_emulation) {
Ned Deilyf70f4a62013-09-06 15:16:19 -07001398 /* handle older 0-based or newer 1-based indexing */
Nir Sofferfae8f4a2017-07-07 09:10:46 +03001399 hist_ent = history_get(length + libedit_history_start - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 } else
Nir Sofferfae8f4a2017-07-07 09:10:46 +03001401 hist_ent = history_get(length);
1402 line = hist_ent ? hist_ent->line : "";
1403 } else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 line = "";
1405 if (strcmp(p, line))
1406 add_history(p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 }
1408 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
1409 release the original. */
Victor Stinner1600f602018-11-30 15:03:53 +01001410 char *q = p;
Victor Stinner2fe9bac2013-10-10 16:18:20 +02001411 p = PyMem_RawMalloc(n+2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 if (p != NULL) {
Victor Stinner1600f602018-11-30 15:03:53 +01001413 memcpy(p, q, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 p[n] = '\n';
1415 p[n+1] = '\0';
1416 }
1417 free(q);
1418 RESTORE_LOCALE(saved_locale)
1419 return p;
Guido van Rossum0969d361997-08-05 21:27:50 +00001420}
1421
Guido van Rossum290900a1997-09-26 21:51:21 +00001422
1423/* Initialize the module */
1424
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001425PyDoc_STRVAR(doc_module,
1426"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001427
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001428PyDoc_STRVAR(doc_module_le,
1429"Importing this module enables command line editing using libedit readline.");
Martin v. Löwis1a214512008-06-11 05:26:20 +00001430
1431static struct PyModuleDef readlinemodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 PyModuleDef_HEAD_INIT,
1433 "readline",
1434 doc_module,
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001435 sizeof(readlinestate),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 readline_methods,
1437 NULL,
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001438 readline_traverse,
1439 readline_clear,
1440 readline_free
Martin v. Löwis1a214512008-06-11 05:26:20 +00001441};
1442
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001443
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001444PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001445PyInit_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001446{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 PyObject *m;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001448 readlinestate *mod_state;
Guido van Rossum290900a1997-09-26 21:51:21 +00001449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
1451 using_libedit_emulation = 1;
1452 }
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 if (using_libedit_emulation)
1455 readlinemodule.m_doc = doc_module_le;
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001456
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 m = PyModule_Create(&readlinemodule);
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 if (m == NULL)
1461 return NULL;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001462
Victor Stinner0efc0242017-11-30 17:21:07 +01001463 if (PyModule_AddIntConstant(m, "_READLINE_VERSION",
1464 RL_READLINE_VERSION) < 0) {
1465 goto error;
1466 }
1467 if (PyModule_AddIntConstant(m, "_READLINE_RUNTIME_VERSION",
1468 rl_readline_version) < 0) {
1469 goto error;
1470 }
1471 if (PyModule_AddStringConstant(m, "_READLINE_LIBRARY_VERSION",
1472 rl_library_version) < 0)
1473 {
1474 goto error;
1475 }
1476
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001477 mod_state = (readlinestate *) PyModule_GetState(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 PyOS_ReadlineFunctionPointer = call_readline;
Victor Stinner1d8da612019-10-30 16:39:27 +01001479 if (setup_readline(mod_state) < 0) {
1480 PyErr_NoMemory();
1481 goto error;
1482 }
Antoine Pitrou7e8b8672014-11-04 14:52:10 +01001483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 return m;
Victor Stinner0efc0242017-11-30 17:21:07 +01001485
1486error:
1487 Py_DECREF(m);
1488 return NULL;
Guido van Rossum0969d361997-08-05 21:27:50 +00001489}