blob: 02b2c40e6b900e0de23ca17052fde063adce1432 [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
70static int libedit_history_start = 0;
Ronald Oussoren2efd9242009-09-20 14:53:22 +000071
Georg Brandl646fdd62010-10-18 07:27:55 +000072#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Christian Heimes32fbe592007-11-12 15:01:33 +000073static void
74on_completion_display_matches_hook(char **matches,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 int num_matches, int max_length);
Georg Brandl646fdd62010-10-18 07:27:55 +000076#endif
Guido van Rossum0969d361997-08-05 21:27:50 +000077
Antoine Pitroua7f7deb2013-05-06 21:51:03 +020078/* Memory allocated for rl_completer_word_break_characters
79 (see issue #17289 for the motivation). */
80static char *completer_word_break_characters;
81
Antoine Pitrou5c30a752013-07-31 21:52:53 +020082typedef struct {
Martin Panterf6e9f472016-03-22 02:19:29 +000083 /* Specify hook functions in Python */
Antoine Pitrou5c30a752013-07-31 21:52:53 +020084 PyObject *completion_display_matches_hook;
85 PyObject *startup_hook;
86 PyObject *pre_input_hook;
Martin Panterf6e9f472016-03-22 02:19:29 +000087
88 PyObject *completer; /* Specify a word completer in Python */
Antoine Pitrou5c30a752013-07-31 21:52:53 +020089 PyObject *begidx;
90 PyObject *endidx;
91} readlinestate;
92
Hai Shif707d942020-03-16 21:15:01 +080093static inline readlinestate*
Hai Shi5f104d52020-03-17 01:16:32 +080094get_readline_state(PyObject *module)
Hai Shif707d942020-03-16 21:15:01 +080095{
96 void *state = PyModule_GetState(module);
97 assert(state != NULL);
98 return (readlinestate *)state;
99}
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200100
Zackery Spytzb7047e52020-07-12 10:01:03 -0600101/*[clinic input]
102module readline
103[clinic start generated code]*/
104/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ad49da781b9c8721]*/
105
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200106static int
107readline_clear(PyObject *m)
108{
Hai Shif707d942020-03-16 21:15:01 +0800109 readlinestate *state = get_readline_state(m);
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200110 Py_CLEAR(state->completion_display_matches_hook);
111 Py_CLEAR(state->startup_hook);
112 Py_CLEAR(state->pre_input_hook);
113 Py_CLEAR(state->completer);
114 Py_CLEAR(state->begidx);
115 Py_CLEAR(state->endidx);
116 return 0;
117}
118
119static int
120readline_traverse(PyObject *m, visitproc visit, void *arg)
121{
Hai Shif707d942020-03-16 21:15:01 +0800122 readlinestate *state = get_readline_state(m);
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200123 Py_VISIT(state->completion_display_matches_hook);
124 Py_VISIT(state->startup_hook);
125 Py_VISIT(state->pre_input_hook);
126 Py_VISIT(state->completer);
127 Py_VISIT(state->begidx);
128 Py_VISIT(state->endidx);
129 return 0;
130}
131
132static void
133readline_free(void *m)
134{
135 readline_clear((PyObject *)m);
136}
137
138static PyModuleDef readlinemodule;
139
140#define readlinestate_global ((readlinestate *)PyModule_GetState(PyState_FindModule(&readlinemodule)))
141
142
Martin Panterf00c49d2016-06-14 01:16:16 +0000143/* Convert to/from multibyte C strings */
144
145static PyObject *
146encode(PyObject *b)
147{
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100148 return PyUnicode_EncodeLocale(b, "surrogateescape");
Martin Panterf00c49d2016-06-14 01:16:16 +0000149}
150
151static PyObject *
152decode(const char *s)
153{
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100154 return PyUnicode_DecodeLocale(s, "surrogateescape");
Martin Panterf00c49d2016-06-14 01:16:16 +0000155}
156
157
Guido van Rossum290900a1997-09-26 21:51:21 +0000158/* Exported function to send one line to readline's init file parser */
159
Zackery Spytzb7047e52020-07-12 10:01:03 -0600160/*[clinic input]
161readline.parse_and_bind
162
163 string: object
164 /
165
166Execute the init line provided in the string argument.
167[clinic start generated code]*/
168
Guido van Rossum290900a1997-09-26 21:51:21 +0000169static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600170readline_parse_and_bind(PyObject *module, PyObject *string)
171/*[clinic end generated code: output=1a1ede8afb9546c1 input=8a28a00bb4d61eec]*/
Guido van Rossum290900a1997-09-26 21:51:21 +0000172{
Martin Panterf00c49d2016-06-14 01:16:16 +0000173 char *copy;
174 PyObject *encoded = encode(string);
175 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 return NULL;
Martin Panterf00c49d2016-06-14 01:16:16 +0000177 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 /* Make a copy -- rl_parse_and_bind() modifies its argument */
179 /* Bernard Herzog */
Martin Panterf00c49d2016-06-14 01:16:16 +0000180 copy = PyMem_Malloc(1 + PyBytes_GET_SIZE(encoded));
181 if (copy == NULL) {
182 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 return PyErr_NoMemory();
Martin Panterf00c49d2016-06-14 01:16:16 +0000184 }
185 strcpy(copy, PyBytes_AS_STRING(encoded));
186 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 rl_parse_and_bind(copy);
Victor Stinnerb6404912013-07-07 16:21:41 +0200188 PyMem_Free(copy); /* Free the copy */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +0000190}
191
Guido van Rossum290900a1997-09-26 21:51:21 +0000192/* Exported function to parse a readline init file */
193
Zackery Spytzb7047e52020-07-12 10:01:03 -0600194/*[clinic input]
195readline.read_init_file
196
197 filename as filename_obj: object = None
198 /
199
200Execute a readline initialization file.
201
202The default filename is the last filename used.
203[clinic start generated code]*/
204
Guido van Rossum290900a1997-09-26 21:51:21 +0000205static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600206readline_read_init_file_impl(PyObject *module, PyObject *filename_obj)
207/*[clinic end generated code: output=8e059b676142831e input=4c80c473e448139d]*/
Guido van Rossum290900a1997-09-26 21:51:21 +0000208{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600209 PyObject *filename_bytes;
Victor Stinner19e65a32010-06-11 22:27:14 +0000210 if (filename_obj != Py_None) {
211 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
212 return NULL;
Zackery Spytzb7047e52020-07-12 10:01:03 -0600213 errno = rl_read_init_file(PyBytes_AS_STRING(filename_bytes));
Victor Stinner19e65a32010-06-11 22:27:14 +0000214 Py_DECREF(filename_bytes);
215 } else
216 errno = rl_read_init_file(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 if (errno)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300218 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +0000220}
221
Skip Montanaro28067822000-07-06 18:55:12 +0000222/* Exported function to load a readline history file */
223
Zackery Spytzb7047e52020-07-12 10:01:03 -0600224/*[clinic input]
225readline.read_history_file
226
227 filename as filename_obj: object = None
228 /
229
230Load a readline history file.
231
232The default filename is ~/.history.
233[clinic start generated code]*/
234
Skip Montanaro28067822000-07-06 18:55:12 +0000235static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600236readline_read_history_file_impl(PyObject *module, PyObject *filename_obj)
237/*[clinic end generated code: output=66a951836fb54fbb input=3d29d755b7e6932e]*/
Skip Montanaro28067822000-07-06 18:55:12 +0000238{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600239 PyObject *filename_bytes;
Victor Stinner19e65a32010-06-11 22:27:14 +0000240 if (filename_obj != Py_None) {
241 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
242 return NULL;
Zackery Spytzb7047e52020-07-12 10:01:03 -0600243 errno = read_history(PyBytes_AS_STRING(filename_bytes));
Victor Stinner19e65a32010-06-11 22:27:14 +0000244 Py_DECREF(filename_bytes);
245 } else
246 errno = read_history(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 if (errno)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300248 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000250}
251
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000252static int _history_length = -1; /* do not truncate history by default */
Skip Montanaro28067822000-07-06 18:55:12 +0000253
254/* Exported function to save a readline history file */
255
Zackery Spytzb7047e52020-07-12 10:01:03 -0600256/*[clinic input]
257readline.write_history_file
258
259 filename as filename_obj: object = None
260 /
261
262Save a readline history file.
263
264The default filename is ~/.history.
265[clinic start generated code]*/
266
Skip Montanaro28067822000-07-06 18:55:12 +0000267static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600268readline_write_history_file_impl(PyObject *module, PyObject *filename_obj)
269/*[clinic end generated code: output=fbcad13d8ef59ae6 input=28a8e062fe363703]*/
Skip Montanaro28067822000-07-06 18:55:12 +0000270{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600271 PyObject *filename_bytes;
Serhiy Storchaka8f87eef2020-04-12 14:58:27 +0300272 const char *filename;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100273 int err;
Victor Stinner19e65a32010-06-11 22:27:14 +0000274 if (filename_obj != Py_None) {
275 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
276 return NULL;
Zackery Spytzb7047e52020-07-12 10:01:03 -0600277 filename = PyBytes_AS_STRING(filename_bytes);
Victor Stinner19e65a32010-06-11 22:27:14 +0000278 } else {
279 filename_bytes = NULL;
280 filename = NULL;
281 }
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100282 errno = err = write_history(filename);
283 if (!err && _history_length >= 0)
Victor Stinner19e65a32010-06-11 22:27:14 +0000284 history_truncate_file(filename, _history_length);
285 Py_XDECREF(filename_bytes);
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100286 errno = err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 if (errno)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300288 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000290}
291
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600292#ifdef HAVE_RL_APPEND_HISTORY
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600293/* Exported function to save part of a readline history file */
294
Zackery Spytzb7047e52020-07-12 10:01:03 -0600295/*[clinic input]
296readline.append_history_file
297
298 nelements: int
299 filename as filename_obj: object = None
300 /
301
302Append the last nelements items of the history list to file.
303
304The default filename is ~/.history.
305[clinic start generated code]*/
306
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600307static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600308readline_append_history_file_impl(PyObject *module, int nelements,
309 PyObject *filename_obj)
310/*[clinic end generated code: output=5df06fc9da56e4e4 input=784b774db3a4b7c5]*/
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600311{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600312 PyObject *filename_bytes;
Serhiy Storchaka8f87eef2020-04-12 14:58:27 +0300313 const char *filename;
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600314 int err;
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600315 if (filename_obj != Py_None) {
316 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
317 return NULL;
Zackery Spytzb7047e52020-07-12 10:01:03 -0600318 filename = PyBytes_AS_STRING(filename_bytes);
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600319 } else {
320 filename_bytes = NULL;
321 filename = NULL;
322 }
323 errno = err = append_history(nelements, filename);
324 if (!err && _history_length >= 0)
325 history_truncate_file(filename, _history_length);
326 Py_XDECREF(filename_bytes);
327 errno = err;
328 if (errno)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300329 return PyErr_SetFromErrno(PyExc_OSError);
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600330 Py_RETURN_NONE;
331}
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600332#endif
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600333
334
Guido van Rossum74f31432003-01-07 20:01:29 +0000335/* Set history length */
336
Zackery Spytzb7047e52020-07-12 10:01:03 -0600337/*[clinic input]
338readline.set_history_length
339
340 length: int
341 /
342
343Set the maximal number of lines which will be written to the history file.
344
345A negative length is used to inhibit history truncation.
346[clinic start generated code]*/
347
348static PyObject *
349readline_set_history_length_impl(PyObject *module, int length)
350/*[clinic end generated code: output=e161a53e45987dc7 input=b8901bf16488b760]*/
Guido van Rossum74f31432003-01-07 20:01:29 +0000351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 _history_length = length;
353 Py_RETURN_NONE;
Guido van Rossum74f31432003-01-07 20:01:29 +0000354}
355
Guido van Rossum74f31432003-01-07 20:01:29 +0000356/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000357
Zackery Spytzb7047e52020-07-12 10:01:03 -0600358/*[clinic input]
359readline.get_history_length
360
361Return the maximum number of lines that will be written to the history file.
362[clinic start generated code]*/
363
364static PyObject *
365readline_get_history_length_impl(PyObject *module)
366/*[clinic end generated code: output=83a2eeae35b6d2b9 input=5dce2eeba4327817]*/
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000367{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 return PyLong_FromLong(_history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000369}
370
Martin v. Löwis0daad592001-09-30 21:09:59 +0000371/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000372
Martin v. Löwis0daad592001-09-30 21:09:59 +0000373static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600374set_hook(const char *funcname, PyObject **hook_var, PyObject *function)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000375{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 if (function == Py_None) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200377 Py_CLEAR(*hook_var);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 }
379 else if (PyCallable_Check(function)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 Py_INCREF(function);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300381 Py_XSETREF(*hook_var, function);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 }
383 else {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100384 PyErr_Format(PyExc_TypeError,
385 "set_%.50s(func): argument not callable",
386 funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 return NULL;
388 }
389 Py_RETURN_NONE;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000390}
391
Zackery Spytzb7047e52020-07-12 10:01:03 -0600392/*[clinic input]
393readline.set_completion_display_matches_hook
394
395 function: object = None
396 /
397
398Set or remove the completion display function.
399
400The function is called as
401 function(substitution, [matches], longest_match_length)
402once each time matches need to be displayed.
403[clinic start generated code]*/
Guido van Rossum74f31432003-01-07 20:01:29 +0000404
Martin v. Löwis0daad592001-09-30 21:09:59 +0000405static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600406readline_set_completion_display_matches_hook_impl(PyObject *module,
407 PyObject *function)
408/*[clinic end generated code: output=516e5cb8db75a328 input=4f0bfd5ab0179a26]*/
Thomas Wouters89d996e2007-09-08 17:39:28 +0000409{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 PyObject *result = set_hook("completion_display_matches_hook",
Zackery Spytzb7047e52020-07-12 10:01:03 -0600411 &readlinestate_global->completion_display_matches_hook,
412 function);
Christian Heimes32fbe592007-11-12 15:01:33 +0000413#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 /* We cannot set this hook globally, since it replaces the
415 default completion display. */
416 rl_completion_display_matches_hook =
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200417 readlinestate_global->completion_display_matches_hook ?
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000418#if defined(_RL_FUNCTION_TYPEDEF)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000420#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 (VFunction *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000422#endif
Christian Heimes32fbe592007-11-12 15:01:33 +0000423#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 return result;
Christian Heimes32fbe592007-11-12 15:01:33 +0000425
Thomas Wouters89d996e2007-09-08 17:39:28 +0000426}
427
Zackery Spytzb7047e52020-07-12 10:01:03 -0600428/*[clinic input]
429readline.set_startup_hook
430
431 function: object = None
432 /
433
434Set or remove the function invoked by the rl_startup_hook callback.
435
436The function is called with no arguments just
437before readline prints the first prompt.
438[clinic start generated code]*/
Thomas Wouters89d996e2007-09-08 17:39:28 +0000439
440static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600441readline_set_startup_hook_impl(PyObject *module, PyObject *function)
442/*[clinic end generated code: output=02cd0e0c4fa082ad input=7783b4334b26d16d]*/
Martin v. Löwis0daad592001-09-30 21:09:59 +0000443{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600444 return set_hook("startup_hook", &readlinestate_global->startup_hook,
445 function);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000446}
447
Martin v. Löwis0daad592001-09-30 21:09:59 +0000448#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000449
450/* Set pre-input hook */
451
Zackery Spytzb7047e52020-07-12 10:01:03 -0600452/*[clinic input]
453readline.set_pre_input_hook
454
455 function: object = None
456 /
457
458Set or remove the function invoked by the rl_pre_input_hook callback.
459
460The function is called with no arguments after the first prompt
461has been printed and just before readline starts reading input
462characters.
463[clinic start generated code]*/
464
Martin v. Löwis0daad592001-09-30 21:09:59 +0000465static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600466readline_set_pre_input_hook_impl(PyObject *module, PyObject *function)
467/*[clinic end generated code: output=fe1a96505096f464 input=4f3eaeaf7ce1fdbe]*/
Martin v. Löwis0daad592001-09-30 21:09:59 +0000468{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600469 return set_hook("pre_input_hook", &readlinestate_global->pre_input_hook,
470 function);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000471}
Martin v. Löwis0daad592001-09-30 21:09:59 +0000472#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000473
Guido van Rossum74f31432003-01-07 20:01:29 +0000474
Thomas Wouters89d996e2007-09-08 17:39:28 +0000475/* Get the completion type for the scope of the tab-completion */
Zackery Spytzb7047e52020-07-12 10:01:03 -0600476
477/*[clinic input]
478readline.get_completion_type
479
480Get the type of completion being attempted.
481[clinic start generated code]*/
482
Thomas Wouters89d996e2007-09-08 17:39:28 +0000483static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600484readline_get_completion_type_impl(PyObject *module)
485/*[clinic end generated code: output=5c54d58a04997c07 input=04b92bc7a82dac91]*/
Thomas Wouters89d996e2007-09-08 17:39:28 +0000486{
Christian Heimes217cfd12007-12-02 14:31:20 +0000487 return PyLong_FromLong(rl_completion_type);
Thomas Wouters89d996e2007-09-08 17:39:28 +0000488}
489
Guido van Rossum74f31432003-01-07 20:01:29 +0000490/* Get the beginning index for the scope of the tab-completion */
491
Zackery Spytzb7047e52020-07-12 10:01:03 -0600492/*[clinic input]
493readline.get_begidx
494
495Get the beginning index of the completion scope.
496[clinic start generated code]*/
497
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000498static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600499readline_get_begidx_impl(PyObject *module)
500/*[clinic end generated code: output=362616ee8ed1b2b1 input=e083b81c8eb4bac3]*/
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000501{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200502 Py_INCREF(readlinestate_global->begidx);
503 return readlinestate_global->begidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000504}
505
Guido van Rossum74f31432003-01-07 20:01:29 +0000506/* Get the ending index for the scope of the tab-completion */
507
Zackery Spytzb7047e52020-07-12 10:01:03 -0600508/*[clinic input]
509readline.get_endidx
510
511Get the ending index of the completion scope.
512[clinic start generated code]*/
513
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000514static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600515readline_get_endidx_impl(PyObject *module)
516/*[clinic end generated code: output=7f763350b12d7517 input=d4c7e34a625fd770]*/
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000517{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200518 Py_INCREF(readlinestate_global->endidx);
519 return readlinestate_global->endidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000520}
521
Guido van Rossum74f31432003-01-07 20:01:29 +0000522/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000523
Zackery Spytzb7047e52020-07-12 10:01:03 -0600524/*[clinic input]
525readline.set_completer_delims
526
527 string: object
528 /
529
530Set the word delimiters for completion.
531[clinic start generated code]*/
532
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000533static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600534readline_set_completer_delims(PyObject *module, PyObject *string)
535/*[clinic end generated code: output=4305b266106c4f1f input=ae945337ebd01e20]*/
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000536{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 char *break_chars;
Martin Panterf00c49d2016-06-14 01:16:16 +0000538 PyObject *encoded = encode(string);
539 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 return NULL;
541 }
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200542 /* Keep a reference to the allocated memory in the module state in case
543 some other module modifies rl_completer_word_break_characters
544 (see issue #17289). */
Martin Panterf00c49d2016-06-14 01:16:16 +0000545 break_chars = strdup(PyBytes_AS_STRING(encoded));
546 Py_DECREF(encoded);
Serhiy Storchaka11384392015-09-27 22:34:59 +0300547 if (break_chars) {
548 free(completer_word_break_characters);
549 completer_word_break_characters = break_chars;
550 rl_completer_word_break_characters = break_chars;
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200551 Py_RETURN_NONE;
552 }
553 else
554 return PyErr_NoMemory();
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000555}
556
Mark Dickinson29b238e2010-08-03 16:08:16 +0000557/* _py_free_history_entry: Utility function to free a history entry. */
558
559#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
560
561/* Readline version >= 5.0 introduced a timestamp field into the history entry
562 structure; this needs to be freed to avoid a memory leak. This version of
563 readline also introduced the handy 'free_history_entry' function, which
564 takes care of the timestamp. */
565
566static void
567_py_free_history_entry(HIST_ENTRY *entry)
568{
569 histdata_t data = free_history_entry(entry);
570 free(data);
571}
572
573#else
574
575/* No free_history_entry function; free everything manually. */
576
577static void
578_py_free_history_entry(HIST_ENTRY *entry)
579{
580 if (entry->line)
581 free((void *)entry->line);
582 if (entry->data)
583 free(entry->data);
584 free(entry);
585}
586
587#endif
588
Zackery Spytzb7047e52020-07-12 10:01:03 -0600589/*[clinic input]
590readline.remove_history_item
591
592 pos as entry_number: int
593 /
594
595Remove history item given by its position.
596[clinic start generated code]*/
597
Skip Montanaroe5069012004-08-15 14:32:06 +0000598static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600599readline_remove_history_item_impl(PyObject *module, int entry_number)
600/*[clinic end generated code: output=ab114f029208c7e8 input=c8520ac3da50224e]*/
Skip Montanaroe5069012004-08-15 14:32:06 +0000601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 HIST_ENTRY *entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 if (entry_number < 0) {
605 PyErr_SetString(PyExc_ValueError,
606 "History index cannot be negative");
607 return NULL;
608 }
609 entry = remove_history(entry_number);
610 if (!entry) {
611 PyErr_Format(PyExc_ValueError,
612 "No history item at position %d",
613 entry_number);
614 return NULL;
615 }
616 /* free memory allocated for the history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000617 _py_free_history_entry(entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000619}
620
Zackery Spytzb7047e52020-07-12 10:01:03 -0600621/*[clinic input]
622readline.replace_history_item
623
624 pos as entry_number: int
625 line: unicode
626 /
627
628Replaces history item given by its position with contents of line.
629[clinic start generated code]*/
Skip Montanaroe5069012004-08-15 14:32:06 +0000630
631static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600632readline_replace_history_item_impl(PyObject *module, int entry_number,
633 PyObject *line)
634/*[clinic end generated code: output=f8cec2770ca125eb input=b7ccef0780ae041b]*/
Skip Montanaroe5069012004-08-15 14:32:06 +0000635{
Martin Panterf00c49d2016-06-14 01:16:16 +0000636 PyObject *encoded;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 HIST_ENTRY *old_entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 if (entry_number < 0) {
640 PyErr_SetString(PyExc_ValueError,
641 "History index cannot be negative");
642 return NULL;
643 }
Martin Panterf00c49d2016-06-14 01:16:16 +0000644 encoded = encode(line);
645 if (encoded == NULL) {
646 return NULL;
647 }
648 old_entry = replace_history_entry(entry_number, PyBytes_AS_STRING(encoded), (void *)NULL);
649 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 if (!old_entry) {
651 PyErr_Format(PyExc_ValueError,
652 "No history item at position %d",
653 entry_number);
654 return NULL;
655 }
656 /* free memory allocated for the old history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000657 _py_free_history_entry(old_entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000659}
660
Guido van Rossum74f31432003-01-07 20:01:29 +0000661/* Add a line to the history buffer */
662
Zackery Spytzb7047e52020-07-12 10:01:03 -0600663/*[clinic input]
664readline.add_history
665
666 string: object
667 /
668
669Add an item to the history buffer.
670[clinic start generated code]*/
671
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000672static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600673readline_add_history(PyObject *module, PyObject *string)
674/*[clinic end generated code: output=b107b7e8106e803d input=e57c1cf6bc68d7e3]*/
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000675{
Martin Panterf00c49d2016-06-14 01:16:16 +0000676 PyObject *encoded = encode(string);
677 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 return NULL;
679 }
Martin Panterf00c49d2016-06-14 01:16:16 +0000680 add_history(PyBytes_AS_STRING(encoded));
681 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 Py_RETURN_NONE;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000683}
684
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000685static int should_auto_add_history = 1;
686
687/* Enable or disable automatic history */
688
Zackery Spytzb7047e52020-07-12 10:01:03 -0600689/*[clinic input]
690readline.set_auto_history
691
692 enabled as _should_auto_add_history: bool
693 /
694
695Enables or disables automatic history.
696[clinic start generated code]*/
697
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000698static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600699readline_set_auto_history_impl(PyObject *module,
700 int _should_auto_add_history)
701/*[clinic end generated code: output=619c6968246fd82b input=3d413073a1a03355]*/
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000702{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600703 should_auto_add_history = _should_auto_add_history;
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000704 Py_RETURN_NONE;
705}
706
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000707
Guido van Rossum74f31432003-01-07 20:01:29 +0000708/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000709
Zackery Spytzb7047e52020-07-12 10:01:03 -0600710/*[clinic input]
711readline.get_completer_delims
712
713Get the word delimiters for completion.
714[clinic start generated code]*/
715
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000716static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600717readline_get_completer_delims_impl(PyObject *module)
718/*[clinic end generated code: output=6b060280fa68ef43 input=e36eb14fb8a1f08a]*/
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000719{
Martin Panterf00c49d2016-06-14 01:16:16 +0000720 return decode(rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000721}
Guido van Rossum74f31432003-01-07 20:01:29 +0000722
Guido van Rossum74f31432003-01-07 20:01:29 +0000723/* Set the completer function */
724
Zackery Spytzb7047e52020-07-12 10:01:03 -0600725/*[clinic input]
726readline.set_completer
727
728 function: object = None
729 /
730
731Set or remove the completer function.
732
733The function is called as function(text, state),
734for state in 0, 1, 2, ..., until it returns a non-string.
735It should return the next possible completion starting with 'text'.
736[clinic start generated code]*/
737
Guido van Rossum290900a1997-09-26 21:51:21 +0000738static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600739readline_set_completer_impl(PyObject *module, PyObject *function)
740/*[clinic end generated code: output=171a2a60f81d3204 input=51e81e13118eb877]*/
Guido van Rossum290900a1997-09-26 21:51:21 +0000741{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600742 return set_hook("completer", &readlinestate_global->completer, function);
Guido van Rossum290900a1997-09-26 21:51:21 +0000743}
744
Zackery Spytzb7047e52020-07-12 10:01:03 -0600745/*[clinic input]
746readline.get_completer
Guido van Rossum290900a1997-09-26 21:51:21 +0000747
Zackery Spytzb7047e52020-07-12 10:01:03 -0600748Get the current completer function.
749[clinic start generated code]*/
Guido van Rossum74f31432003-01-07 20:01:29 +0000750
Michael W. Hudson796df152003-01-30 10:12:51 +0000751static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600752readline_get_completer_impl(PyObject *module)
753/*[clinic end generated code: output=6e6bbd8226d14475 input=6457522e56d70d13]*/
Michael W. Hudson796df152003-01-30 10:12:51 +0000754{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200755 if (readlinestate_global->completer == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 Py_RETURN_NONE;
757 }
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200758 Py_INCREF(readlinestate_global->completer);
759 return readlinestate_global->completer;
Michael W. Hudson796df152003-01-30 10:12:51 +0000760}
761
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000762/* Private function to get current length of history. XXX It may be
763 * possible to replace this with a direct use of history_length instead,
764 * but it's not clear whether BSD's libedit keeps history_length up to date.
765 * See issue #8065.*/
766
767static int
768_py_get_history_length(void)
769{
770 HISTORY_STATE *hist_st = history_get_history_state();
771 int length = hist_st->length;
772 /* the history docs don't say so, but the address of hist_st changes each
773 time history_get_history_state is called which makes me think it's
774 freshly malloc'd memory... on the other hand, the address of the last
775 line stays the same as long as history isn't extended, so it appears to
776 be malloc'd but managed by the history package... */
777 free(hist_st);
778 return length;
779}
780
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000781/* Exported function to get any element of history */
782
Zackery Spytzb7047e52020-07-12 10:01:03 -0600783/*[clinic input]
784readline.get_history_item
785
786 index as idx: int
787 /
788
789Return the current contents of history item at index.
790[clinic start generated code]*/
791
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000792static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600793readline_get_history_item_impl(PyObject *module, int idx)
794/*[clinic end generated code: output=83d3e53ea5f34b3d input=63fff0c3c4323269]*/
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000795{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 HIST_ENTRY *hist_ent;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 if (using_libedit_emulation) {
Ned Deilyf70f4a62013-09-06 15:16:19 -0700799 /* Older versions of libedit's readline emulation
800 * use 0-based indexes, while readline and newer
801 * versions of libedit use 1-based indexes.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 */
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000803 int length = _py_get_history_length();
Ned Deilyf70f4a62013-09-06 15:16:19 -0700804
805 idx = idx - 1 + libedit_history_start;
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 /*
808 * Apple's readline emulation crashes when
809 * the index is out of range, therefore
810 * test for that and fail gracefully.
811 */
Ned Deilyf70f4a62013-09-06 15:16:19 -0700812 if (idx < (0 + libedit_history_start)
813 || idx >= (length + libedit_history_start)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 Py_RETURN_NONE;
815 }
816 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 if ((hist_ent = history_get(idx)))
Martin Panterf00c49d2016-06-14 01:16:16 +0000818 return decode(hist_ent->line);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 else {
820 Py_RETURN_NONE;
821 }
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000822}
823
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000824/* Exported function to get current length of history */
825
Zackery Spytzb7047e52020-07-12 10:01:03 -0600826/*[clinic input]
827readline.get_current_history_length
828
829Return the current (not the maximum) length of history.
830[clinic start generated code]*/
831
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000832static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600833readline_get_current_history_length_impl(PyObject *module)
834/*[clinic end generated code: output=436b294f12ba1e3f input=9cb3f431a68d071f]*/
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000835{
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000836 return PyLong_FromLong((long)_py_get_history_length());
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000837}
838
Guido van Rossum79378ff1997-10-07 14:53:21 +0000839/* Exported function to read the current line buffer */
840
Zackery Spytzb7047e52020-07-12 10:01:03 -0600841/*[clinic input]
842readline.get_line_buffer
843
844Return the current contents of the line buffer.
845[clinic start generated code]*/
846
Guido van Rossum79378ff1997-10-07 14:53:21 +0000847static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600848readline_get_line_buffer_impl(PyObject *module)
849/*[clinic end generated code: output=d22f9025ecad80e4 input=5f5fbc0d12c69412]*/
Guido van Rossum79378ff1997-10-07 14:53:21 +0000850{
Martin Panterf00c49d2016-06-14 01:16:16 +0000851 return decode(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000852}
853
Martin v. Löwise7a97962003-09-20 16:08:33 +0000854#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
855
856/* Exported function to clear the current history */
857
Zackery Spytzb7047e52020-07-12 10:01:03 -0600858/*[clinic input]
859readline.clear_history
860
861Clear the current readline history.
862[clinic start generated code]*/
863
Martin v. Löwise7a97962003-09-20 16:08:33 +0000864static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600865readline_clear_history_impl(PyObject *module)
866/*[clinic end generated code: output=1f2dbb0dfa5d5ebb input=208962c4393f5d16]*/
Martin v. Löwise7a97962003-09-20 16:08:33 +0000867{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 clear_history();
869 Py_RETURN_NONE;
Martin v. Löwise7a97962003-09-20 16:08:33 +0000870}
Martin v. Löwise7a97962003-09-20 16:08:33 +0000871#endif
872
873
Guido van Rossum79378ff1997-10-07 14:53:21 +0000874/* Exported function to insert text into the line buffer */
875
Zackery Spytzb7047e52020-07-12 10:01:03 -0600876/*[clinic input]
877readline.insert_text
878
879 string: object
880 /
881
882Insert text into the line buffer at the cursor position.
883[clinic start generated code]*/
884
Guido van Rossum79378ff1997-10-07 14:53:21 +0000885static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600886readline_insert_text(PyObject *module, PyObject *string)
887/*[clinic end generated code: output=23d792821d320c19 input=bc96c3c848d5ccb5]*/
Guido van Rossum79378ff1997-10-07 14:53:21 +0000888{
Martin Panterf00c49d2016-06-14 01:16:16 +0000889 PyObject *encoded = encode(string);
890 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 return NULL;
Martin Panterf00c49d2016-06-14 01:16:16 +0000892 }
893 rl_insert_text(PyBytes_AS_STRING(encoded));
894 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 Py_RETURN_NONE;
Guido van Rossum79378ff1997-10-07 14:53:21 +0000896}
897
Guido van Rossum74f31432003-01-07 20:01:29 +0000898/* Redisplay the line buffer */
899
Zackery Spytzb7047e52020-07-12 10:01:03 -0600900/*[clinic input]
901readline.redisplay
902
903Change what's displayed on the screen to reflect contents of the line buffer.
904[clinic start generated code]*/
905
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000906static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600907readline_redisplay_impl(PyObject *module)
908/*[clinic end generated code: output=a8b9725827c3c34b input=b485151058d75edc]*/
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000909{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 rl_redisplay();
911 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000912}
913
Zackery Spytzb7047e52020-07-12 10:01:03 -0600914#include "clinic/readline.c.h"
Guido van Rossum74f31432003-01-07 20:01:29 +0000915
Guido van Rossum290900a1997-09-26 21:51:21 +0000916/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000917
918static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000919{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600920 READLINE_PARSE_AND_BIND_METHODDEF
921 READLINE_GET_LINE_BUFFER_METHODDEF
922 READLINE_INSERT_TEXT_METHODDEF
923 READLINE_REDISPLAY_METHODDEF
924 READLINE_READ_INIT_FILE_METHODDEF
925 READLINE_READ_HISTORY_FILE_METHODDEF
926 READLINE_WRITE_HISTORY_FILE_METHODDEF
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600927#ifdef HAVE_RL_APPEND_HISTORY
Zackery Spytzb7047e52020-07-12 10:01:03 -0600928 READLINE_APPEND_HISTORY_FILE_METHODDEF
Ned Deily8007cbc2014-11-26 13:02:33 -0800929#endif
Zackery Spytzb7047e52020-07-12 10:01:03 -0600930 READLINE_GET_HISTORY_ITEM_METHODDEF
931 READLINE_GET_CURRENT_HISTORY_LENGTH_METHODDEF
932 READLINE_SET_HISTORY_LENGTH_METHODDEF
933 READLINE_GET_HISTORY_LENGTH_METHODDEF
934 READLINE_SET_COMPLETER_METHODDEF
935 READLINE_GET_COMPLETER_METHODDEF
936 READLINE_GET_COMPLETION_TYPE_METHODDEF
937 READLINE_GET_BEGIDX_METHODDEF
938 READLINE_GET_ENDIDX_METHODDEF
939 READLINE_SET_COMPLETER_DELIMS_METHODDEF
940 READLINE_SET_AUTO_HISTORY_METHODDEF
941 READLINE_ADD_HISTORY_METHODDEF
942 READLINE_REMOVE_HISTORY_ITEM_METHODDEF
943 READLINE_REPLACE_HISTORY_ITEM_METHODDEF
944 READLINE_GET_COMPLETER_DELIMS_METHODDEF
945 READLINE_SET_COMPLETION_DISPLAY_MATCHES_HOOK_METHODDEF
946 READLINE_SET_STARTUP_HOOK_METHODDEF
Martin v. Löwis0daad592001-09-30 21:09:59 +0000947#ifdef HAVE_RL_PRE_INPUT_HOOK
Zackery Spytzb7047e52020-07-12 10:01:03 -0600948 READLINE_SET_PRE_INPUT_HOOK_METHODDEF
Martin v. Löwis0daad592001-09-30 21:09:59 +0000949#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000950#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Zackery Spytzb7047e52020-07-12 10:01:03 -0600951 READLINE_CLEAR_HISTORY_METHODDEF
Martin v. Löwise7a97962003-09-20 16:08:33 +0000952#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000954};
955
Guido van Rossum05ac4492003-01-07 20:04:12 +0000956
Martin v. Löwis0daad592001-09-30 21:09:59 +0000957/* C function to call the Python hooks. */
958
959static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000960on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000961{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 int result = 0;
963 if (func != NULL) {
964 PyObject *r;
Victor Stinner2ff58a22019-06-17 14:27:23 +0200965 r = PyObject_CallNoArgs(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 if (r == NULL)
967 goto error;
968 if (r == Py_None)
969 result = 0;
970 else {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300971 result = _PyLong_AsInt(r);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 if (result == -1 && PyErr_Occurred())
973 goto error;
974 }
975 Py_DECREF(r);
976 goto done;
977 error:
978 PyErr_Clear();
979 Py_XDECREF(r);
980 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 return result;
982 }
983 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000984}
985
986static int
Ned Deily7b9ddea2014-02-05 16:53:10 -0800987#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000988on_startup_hook(void)
Ned Deily7b9ddea2014-02-05 16:53:10 -0800989#else
990on_startup_hook()
991#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000992{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200993 int r;
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200994 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200995 r = on_hook(readlinestate_global->startup_hook);
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200996 PyGILState_Release(gilstate);
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200997 return r;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000998}
999
1000#ifdef HAVE_RL_PRE_INPUT_HOOK
1001static int
Ned Deily7b9ddea2014-02-05 16:53:10 -08001002#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwis0daad592001-09-30 21:09:59 +00001003on_pre_input_hook(void)
Ned Deily7b9ddea2014-02-05 16:53:10 -08001004#else
1005on_pre_input_hook()
1006#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +00001007{
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001008 int r;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001009 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001010 r = on_hook(readlinestate_global->pre_input_hook);
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001011 PyGILState_Release(gilstate);
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001012 return r;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001013}
1014#endif
1015
Guido van Rossum05ac4492003-01-07 20:04:12 +00001016
Thomas Wouters89d996e2007-09-08 17:39:28 +00001017/* C function to call the Python completion_display_matches */
1018
Georg Brandl646fdd62010-10-18 07:27:55 +00001019#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Thomas Wouters89d996e2007-09-08 17:39:28 +00001020static void
1021on_completion_display_matches_hook(char **matches,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 int num_matches, int max_length)
Thomas Wouters89d996e2007-09-08 17:39:28 +00001023{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 int i;
Martin Panterf00c49d2016-06-14 01:16:16 +00001025 PyObject *sub, *m=NULL, *s=NULL, *r=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 m = PyList_New(num_matches);
1028 if (m == NULL)
1029 goto error;
1030 for (i = 0; i < num_matches; i++) {
Martin Panterf00c49d2016-06-14 01:16:16 +00001031 s = decode(matches[i+1]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 if (s == NULL)
1033 goto error;
Zackery Spytz99d56b52018-12-08 07:16:55 -07001034 PyList_SET_ITEM(m, i, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 }
Martin Panterf00c49d2016-06-14 01:16:16 +00001036 sub = decode(matches[0]);
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001037 r = PyObject_CallFunction(readlinestate_global->completion_display_matches_hook,
Martin Panterf00c49d2016-06-14 01:16:16 +00001038 "NNi", sub, m, max_length);
Christian Heimes32fbe592007-11-12 15:01:33 +00001039
Martin Panterf00c49d2016-06-14 01:16:16 +00001040 m=NULL;
Christian Heimes32fbe592007-11-12 15:01:33 +00001041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 if (r == NULL ||
1043 (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) {
1044 goto error;
1045 }
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001046 Py_CLEAR(r);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047
1048 if (0) {
1049 error:
1050 PyErr_Clear();
1051 Py_XDECREF(m);
1052 Py_XDECREF(r);
1053 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 PyGILState_Release(gilstate);
Thomas Wouters89d996e2007-09-08 17:39:28 +00001055}
1056
Senthil Kumaran95c07002010-11-04 03:51:05 +00001057#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +00001058
Martin Panter5dbbf1a2016-04-03 02:54:58 +00001059#ifdef HAVE_RL_RESIZE_TERMINAL
1060static volatile sig_atomic_t sigwinch_received;
Martin Panter28f35b22016-04-03 08:00:49 +00001061static PyOS_sighandler_t sigwinch_ohandler;
Martin Panter5dbbf1a2016-04-03 02:54:58 +00001062
1063static void
1064readline_sigwinch_handler(int signum)
1065{
1066 sigwinch_received = 1;
1067 if (sigwinch_ohandler &&
1068 sigwinch_ohandler != SIG_IGN && sigwinch_ohandler != SIG_DFL)
1069 sigwinch_ohandler(signum);
1070
1071#ifndef HAVE_SIGACTION
1072 /* If the handler was installed with signal() rather than sigaction(),
1073 we need to reinstall it. */
1074 PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
1075#endif
1076}
1077#endif
1078
Guido van Rossum290900a1997-09-26 21:51:21 +00001079/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +00001080
Guido van Rossum290900a1997-09-26 21:51:21 +00001081static char *
Guido van Rossumd8faa362007-04-27 19:54:29 +00001082on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +00001083{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 char *result = NULL;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001085 if (readlinestate_global->completer != NULL) {
Martin Panterf00c49d2016-06-14 01:16:16 +00001086 PyObject *r = NULL, *t;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 rl_attempted_completion_over = 1;
Martin Panterf00c49d2016-06-14 01:16:16 +00001089 t = decode(text);
1090 r = PyObject_CallFunction(readlinestate_global->completer, "Ni", t, state);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 if (r == NULL)
1092 goto error;
1093 if (r == Py_None) {
1094 result = NULL;
1095 }
1096 else {
Martin Panterf00c49d2016-06-14 01:16:16 +00001097 PyObject *encoded = encode(r);
1098 if (encoded == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 goto error;
Martin Panterf00c49d2016-06-14 01:16:16 +00001100 result = strdup(PyBytes_AS_STRING(encoded));
1101 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 }
1103 Py_DECREF(r);
1104 goto done;
1105 error:
1106 PyErr_Clear();
1107 Py_XDECREF(r);
1108 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 PyGILState_Release(gilstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001110 return result;
1111 }
1112 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +00001113}
1114
Guido van Rossum290900a1997-09-26 21:51:21 +00001115
Guido van Rossum6d0d3652003-01-07 20:34:19 +00001116/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001117 * before calling the normal completer */
1118
Neal Norwitzc355f0c2003-02-21 00:30:18 +00001119static char **
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001120flex_complete(const char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001121{
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001122 char **result;
Martin Panterf00c49d2016-06-14 01:16:16 +00001123 char saved;
1124 size_t start_size, end_size;
1125 wchar_t *s;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001126 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitroudc0900b2009-10-19 18:22:37 +00001127#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 rl_completion_append_character ='\0';
Antoine Pitroud5131772009-10-26 19:22:14 +00001129#endif
1130#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 rl_completion_suppress_append = 0;
Antoine Pitroudc0900b2009-10-19 18:22:37 +00001132#endif
Martin Panterf00c49d2016-06-14 01:16:16 +00001133
1134 saved = rl_line_buffer[start];
1135 rl_line_buffer[start] = 0;
1136 s = Py_DecodeLocale(rl_line_buffer, &start_size);
1137 rl_line_buffer[start] = saved;
1138 if (s == NULL) {
1139 goto done;
1140 }
1141 PyMem_RawFree(s);
1142 saved = rl_line_buffer[end];
1143 rl_line_buffer[end] = 0;
1144 s = Py_DecodeLocale(rl_line_buffer + start, &end_size);
1145 rl_line_buffer[end] = saved;
1146 if (s == NULL) {
1147 goto done;
1148 }
1149 PyMem_RawFree(s);
1150 start = (int)start_size;
1151 end = start + (int)end_size;
1152
1153done:
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001154 Py_XDECREF(readlinestate_global->begidx);
1155 Py_XDECREF(readlinestate_global->endidx);
1156 readlinestate_global->begidx = PyLong_FromLong((long) start);
1157 readlinestate_global->endidx = PyLong_FromLong((long) end);
Benjamin Peterson1bb0c0b2016-09-05 18:26:19 -07001158 result = completion_matches((char *)text, *on_completion);
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001159 PyGILState_Release(gilstate);
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001160 return result;
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001161}
1162
Guido van Rossum05ac4492003-01-07 20:04:12 +00001163
Victor Stinner1d8da612019-10-30 16:39:27 +01001164/* Helper to initialize GNU readline properly.
1165 Return -1 on memory allocation failure, return 0 on success. */
1166static int
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001167setup_readline(readlinestate *mod_state)
Guido van Rossum290900a1997-09-26 21:51:21 +00001168{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +00001169#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Victor Stinner1d8da612019-10-30 16:39:27 +01001171 if (!saved_locale) {
1172 return -1;
1173 }
Guido van Rossum60c8a3a2002-10-09 21:27:33 +00001174#endif
1175
Zvezdan Petkovicc2f082e2018-05-17 02:45:10 -04001176 /* The name must be defined before initialization */
1177 rl_readline_name = "python";
1178
Victor Stinner6ced7c42011-03-21 18:15:42 +01001179 /* the libedit readline emulation resets key bindings etc
R. David Murray52d1b4e2010-12-18 03:48:32 +00001180 * when calling rl_initialize. So call it upfront
1181 */
1182 if (using_libedit_emulation)
1183 rl_initialize();
Ned Deilyf70f4a62013-09-06 15:16:19 -07001184
1185 /* Detect if libedit's readline emulation uses 0-based
1186 * indexing or 1-based indexing.
1187 */
1188 add_history("1");
1189 if (history_get(1) == NULL) {
1190 libedit_history_start = 0;
1191 } else {
1192 libedit_history_start = 1;
1193 }
1194 clear_history();
R. David Murray52d1b4e2010-12-18 03:48:32 +00001195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 using_history();
Skip Montanaroa0392742002-06-11 14:32:46 +00001197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 /* Force rebind of TAB to insert-tab */
1199 rl_bind_key('\t', rl_insert);
1200 /* Bind both ESC-TAB and ESC-ESC to the completion function */
1201 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
1202 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin Panter5dbbf1a2016-04-03 02:54:58 +00001203#ifdef HAVE_RL_RESIZE_TERMINAL
1204 /* Set up signal handler for window resize */
1205 sigwinch_ohandler = PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
1206#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 /* Set our hook functions */
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001208 rl_startup_hook = on_startup_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001209#ifdef HAVE_RL_PRE_INPUT_HOOK
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001210 rl_pre_input_hook = on_pre_input_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001211#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 /* Set our completion function */
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001213 rl_attempted_completion_function = flex_complete;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 /* Set Python word break characters */
Antoine Pitroua7f7deb2013-05-06 21:51:03 +02001215 completer_word_break_characters =
1216 rl_completer_word_break_characters =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
1218 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001219
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001220 mod_state->begidx = PyLong_FromLong(0L);
1221 mod_state->endidx = PyLong_FromLong(0L);
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001222
Martin Panterc427b8d2016-08-27 03:23:11 +00001223 if (!using_libedit_emulation)
Martin Panterc427b8d2016-08-27 03:23:11 +00001224 {
1225 if (!isatty(STDOUT_FILENO)) {
1226 /* Issue #19884: stdout is not a terminal. Disable meta modifier
1227 keys to not write the ANSI sequence "\033[1034h" into stdout. On
1228 terminals supporting 8 bit characters like TERM=xterm-256color
1229 (which is now the default Fedora since Fedora 18), the meta key is
1230 used to enable support of 8 bit characters (ANSI sequence
1231 "\033[1034h").
1232
1233 With libedit, this call makes readline() crash. */
1234 rl_variable_bind ("enable-meta-key", "off");
1235 }
1236 }
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 /* Initialize (allows .inputrc to override)
1239 *
1240 * XXX: A bug in the readline-2.2 library causes a memory leak
1241 * inside this function. Nothing we can do about it.
1242 */
R. David Murray52d1b4e2010-12-18 03:48:32 +00001243 if (using_libedit_emulation)
Victor Stinner6ced7c42011-03-21 18:15:42 +01001244 rl_read_init_file(NULL);
R. David Murray52d1b4e2010-12-18 03:48:32 +00001245 else
R. David Murray52d1b4e2010-12-18 03:48:32 +00001246 rl_initialize();
Victor Stinner6ced7c42011-03-21 18:15:42 +01001247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 RESTORE_LOCALE(saved_locale)
Victor Stinner1d8da612019-10-30 16:39:27 +01001249 return 0;
Guido van Rossum290900a1997-09-26 21:51:21 +00001250}
1251
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001252/* Wrapper around GNU readline that handles signals differently. */
1253
Antoine Pitrouf474c5a2017-07-18 17:05:03 +02001254static char *completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001255static void
1256rlhandler(char *text)
1257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 completed_input_string = text;
1259 rl_callback_handler_remove();
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001260}
1261
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001262static char *
Serhiy Storchakac6792272013-10-19 21:03:34 +03001263readline_until_enter_or_signal(const char *prompt, int *signal)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 char * not_done_reading = "";
1266 fd_set selectset;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 *signal = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001269#ifdef HAVE_RL_CATCH_SIGNAL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 rl_catch_signals = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001271#endif
1272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 rl_callback_handler_install (prompt, rlhandler);
1274 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 completed_input_string = not_done_reading;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 while (completed_input_string == not_done_reading) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001279 int has_input = 0, err = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 while (!has_input)
1282 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Michael W. Hudson8da2b012004-10-07 13:46:33 +00001283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 /* [Bug #1552726] Only limit the pause if an input hook has been
1285 defined. */
1286 struct timeval *timeoutp = NULL;
1287 if (PyOS_InputHook)
1288 timeoutp = &timeout;
Martin Panter5dbbf1a2016-04-03 02:54:58 +00001289#ifdef HAVE_RL_RESIZE_TERMINAL
1290 /* Update readline's view of the window size after SIGWINCH */
1291 if (sigwinch_received) {
1292 sigwinch_received = 0;
1293 rl_resize_terminal();
1294 }
1295#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 FD_SET(fileno(rl_instream), &selectset);
1297 /* select resets selectset if no input was available */
1298 has_input = select(fileno(rl_instream) + 1, &selectset,
1299 NULL, NULL, timeoutp);
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001300 err = errno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 if(PyOS_InputHook) PyOS_InputHook();
1302 }
1303
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001304 if (has_input > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 rl_callback_read_char();
1306 }
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001307 else if (err == EINTR) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 int s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 PyEval_RestoreThread(_PyOS_ReadlineTState);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 s = PyErr_CheckSignals();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 PyEval_SaveThread();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 if (s < 0) {
1313 rl_free_line_state();
Martin Panterd6990d22016-03-22 07:24:05 +00001314#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0700
1315 rl_callback_sigcleanup();
1316#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 rl_cleanup_after_signal();
1318 rl_callback_handler_remove();
1319 *signal = 1;
1320 completed_input_string = NULL;
1321 }
1322 }
1323 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 return completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001326}
1327
1328
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001329static char *
Serhiy Storchakac6792272013-10-19 21:03:34 +03001330call_readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001331{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 size_t n;
Victor Stinner1600f602018-11-30 15:03:53 +01001333 char *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 int signal;
Neal Norwitz1fa040b2004-08-25 01:20:18 +00001335
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001336#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1338 if (!saved_locale)
1339 Py_FatalError("not enough memory to save locale");
xdegaye1588be62017-11-12 12:45:59 +01001340 _Py_SetLocaleFromEnv(LC_CTYPE);
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001341#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
1344 rl_instream = sys_stdin;
1345 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001346#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001348#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 }
Guido van Rossum74f31432003-01-07 20:01:29 +00001350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 p = readline_until_enter_or_signal(prompt, &signal);
Guido van Rossumb18618d2000-05-03 23:44:39 +00001352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 /* we got an interrupt signal */
1354 if (signal) {
1355 RESTORE_LOCALE(saved_locale)
1356 return NULL;
1357 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001358
Martin Panter7462b6492015-11-02 03:37:02 +00001359 /* We got an EOF, return an empty string. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 if (p == NULL) {
Victor Stinner2fe9bac2013-10-10 16:18:20 +02001361 p = PyMem_RawMalloc(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 if (p != NULL)
1363 *p = '\0';
1364 RESTORE_LOCALE(saved_locale)
1365 return p;
1366 }
1367
1368 /* we have a valid line */
1369 n = strlen(p);
Martin Panterf0dbf7a2016-05-15 01:26:25 +00001370 if (should_auto_add_history && n > 0) {
Brett Cannon2525dc82010-08-22 20:36:25 +00001371 const char *line;
Mark Dickinson6b54e1f2010-08-03 16:49:49 +00001372 int length = _py_get_history_length();
Nir Sofferfae8f4a2017-07-07 09:10:46 +03001373 if (length > 0) {
1374 HIST_ENTRY *hist_ent;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 if (using_libedit_emulation) {
Ned Deilyf70f4a62013-09-06 15:16:19 -07001376 /* handle older 0-based or newer 1-based indexing */
Nir Sofferfae8f4a2017-07-07 09:10:46 +03001377 hist_ent = history_get(length + libedit_history_start - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 } else
Nir Sofferfae8f4a2017-07-07 09:10:46 +03001379 hist_ent = history_get(length);
1380 line = hist_ent ? hist_ent->line : "";
1381 } else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 line = "";
1383 if (strcmp(p, line))
1384 add_history(p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 }
1386 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
1387 release the original. */
Victor Stinner1600f602018-11-30 15:03:53 +01001388 char *q = p;
Victor Stinner2fe9bac2013-10-10 16:18:20 +02001389 p = PyMem_RawMalloc(n+2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 if (p != NULL) {
Victor Stinner1600f602018-11-30 15:03:53 +01001391 memcpy(p, q, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 p[n] = '\n';
1393 p[n+1] = '\0';
1394 }
1395 free(q);
1396 RESTORE_LOCALE(saved_locale)
1397 return p;
Guido van Rossum0969d361997-08-05 21:27:50 +00001398}
1399
Guido van Rossum290900a1997-09-26 21:51:21 +00001400
1401/* Initialize the module */
1402
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001403PyDoc_STRVAR(doc_module,
1404"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001405
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001406PyDoc_STRVAR(doc_module_le,
1407"Importing this module enables command line editing using libedit readline.");
Martin v. Löwis1a214512008-06-11 05:26:20 +00001408
1409static struct PyModuleDef readlinemodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 PyModuleDef_HEAD_INIT,
1411 "readline",
1412 doc_module,
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001413 sizeof(readlinestate),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 readline_methods,
1415 NULL,
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001416 readline_traverse,
1417 readline_clear,
1418 readline_free
Martin v. Löwis1a214512008-06-11 05:26:20 +00001419};
1420
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001421
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001422PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001423PyInit_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 PyObject *m;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001426 readlinestate *mod_state;
Guido van Rossum290900a1997-09-26 21:51:21 +00001427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
1429 using_libedit_emulation = 1;
1430 }
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 if (using_libedit_emulation)
1433 readlinemodule.m_doc = doc_module_le;
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001434
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 m = PyModule_Create(&readlinemodule);
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 if (m == NULL)
1439 return NULL;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001440
Victor Stinner0efc0242017-11-30 17:21:07 +01001441 if (PyModule_AddIntConstant(m, "_READLINE_VERSION",
1442 RL_READLINE_VERSION) < 0) {
1443 goto error;
1444 }
1445 if (PyModule_AddIntConstant(m, "_READLINE_RUNTIME_VERSION",
1446 rl_readline_version) < 0) {
1447 goto error;
1448 }
1449 if (PyModule_AddStringConstant(m, "_READLINE_LIBRARY_VERSION",
1450 rl_library_version) < 0)
1451 {
1452 goto error;
1453 }
1454
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001455 mod_state = (readlinestate *) PyModule_GetState(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 PyOS_ReadlineFunctionPointer = call_readline;
Victor Stinner1d8da612019-10-30 16:39:27 +01001457 if (setup_readline(mod_state) < 0) {
1458 PyErr_NoMemory();
1459 goto error;
1460 }
Antoine Pitrou7e8b8672014-11-04 14:52:10 +01001461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 return m;
Victor Stinner0efc0242017-11-30 17:21:07 +01001463
1464error:
1465 Py_DECREF(m);
1466 return NULL;
Guido van Rossum0969d361997-08-05 21:27:50 +00001467}