blob: c79d22f85f84ea3b1b4a5809cf1dc7dcf72168b7 [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
Dustin Rodrigues755f3c12021-02-15 18:28:24 -0500159/*
160Explicitly disable bracketed paste in the interactive interpreter, even if it's
161set in the inputrc, is enabled by default (eg GNU Readline 8.1), or a user calls
162readline.read_init_file(). The Python REPL has not implemented bracketed
163paste support. Also, bracketed mode writes the "\x1b[?2004h" escape sequence
164into stdout which causes test failures in applications that don't support it.
165It can still be explicitly enabled by calling readline.parse_and_bind("set
166enable-bracketed-paste on"). See bpo-42819 for more details.
167
168This should be removed if bracketed paste mode is implemented (bpo-39820).
169*/
170
171static void
172disable_bracketed_paste(void)
173{
174 if (!using_libedit_emulation) {
175 rl_variable_bind ("enable-bracketed-paste", "off");
176 }
177}
178
Guido van Rossum290900a1997-09-26 21:51:21 +0000179/* Exported function to send one line to readline's init file parser */
180
Zackery Spytzb7047e52020-07-12 10:01:03 -0600181/*[clinic input]
182readline.parse_and_bind
183
184 string: object
185 /
186
187Execute the init line provided in the string argument.
188[clinic start generated code]*/
189
Guido van Rossum290900a1997-09-26 21:51:21 +0000190static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600191readline_parse_and_bind(PyObject *module, PyObject *string)
192/*[clinic end generated code: output=1a1ede8afb9546c1 input=8a28a00bb4d61eec]*/
Guido van Rossum290900a1997-09-26 21:51:21 +0000193{
Martin Panterf00c49d2016-06-14 01:16:16 +0000194 char *copy;
195 PyObject *encoded = encode(string);
196 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 return NULL;
Martin Panterf00c49d2016-06-14 01:16:16 +0000198 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 /* Make a copy -- rl_parse_and_bind() modifies its argument */
200 /* Bernard Herzog */
Martin Panterf00c49d2016-06-14 01:16:16 +0000201 copy = PyMem_Malloc(1 + PyBytes_GET_SIZE(encoded));
202 if (copy == NULL) {
203 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 return PyErr_NoMemory();
Martin Panterf00c49d2016-06-14 01:16:16 +0000205 }
206 strcpy(copy, PyBytes_AS_STRING(encoded));
207 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 rl_parse_and_bind(copy);
Victor Stinnerb6404912013-07-07 16:21:41 +0200209 PyMem_Free(copy); /* Free the copy */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +0000211}
212
Guido van Rossum290900a1997-09-26 21:51:21 +0000213/* Exported function to parse a readline init file */
214
Zackery Spytzb7047e52020-07-12 10:01:03 -0600215/*[clinic input]
216readline.read_init_file
217
218 filename as filename_obj: object = None
219 /
220
221Execute a readline initialization file.
222
223The default filename is the last filename used.
224[clinic start generated code]*/
225
Guido van Rossum290900a1997-09-26 21:51:21 +0000226static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600227readline_read_init_file_impl(PyObject *module, PyObject *filename_obj)
228/*[clinic end generated code: output=8e059b676142831e input=4c80c473e448139d]*/
Guido van Rossum290900a1997-09-26 21:51:21 +0000229{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600230 PyObject *filename_bytes;
Victor Stinner19e65a32010-06-11 22:27:14 +0000231 if (filename_obj != Py_None) {
232 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
233 return NULL;
Zackery Spytzb7047e52020-07-12 10:01:03 -0600234 errno = rl_read_init_file(PyBytes_AS_STRING(filename_bytes));
Victor Stinner19e65a32010-06-11 22:27:14 +0000235 Py_DECREF(filename_bytes);
236 } else
237 errno = rl_read_init_file(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 if (errno)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300239 return PyErr_SetFromErrno(PyExc_OSError);
Dustin Rodrigues755f3c12021-02-15 18:28:24 -0500240 disable_bracketed_paste();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +0000242}
243
Skip Montanaro28067822000-07-06 18:55:12 +0000244/* Exported function to load a readline history file */
245
Zackery Spytzb7047e52020-07-12 10:01:03 -0600246/*[clinic input]
247readline.read_history_file
248
249 filename as filename_obj: object = None
250 /
251
252Load a readline history file.
253
254The default filename is ~/.history.
255[clinic start generated code]*/
256
Skip Montanaro28067822000-07-06 18:55:12 +0000257static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600258readline_read_history_file_impl(PyObject *module, PyObject *filename_obj)
259/*[clinic end generated code: output=66a951836fb54fbb input=3d29d755b7e6932e]*/
Skip Montanaro28067822000-07-06 18:55:12 +0000260{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600261 PyObject *filename_bytes;
Victor Stinner19e65a32010-06-11 22:27:14 +0000262 if (filename_obj != Py_None) {
263 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
264 return NULL;
Zackery Spytzb7047e52020-07-12 10:01:03 -0600265 errno = read_history(PyBytes_AS_STRING(filename_bytes));
Victor Stinner19e65a32010-06-11 22:27:14 +0000266 Py_DECREF(filename_bytes);
267 } else
268 errno = read_history(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 if (errno)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300270 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000272}
273
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000274static int _history_length = -1; /* do not truncate history by default */
Skip Montanaro28067822000-07-06 18:55:12 +0000275
276/* Exported function to save a readline history file */
277
Zackery Spytzb7047e52020-07-12 10:01:03 -0600278/*[clinic input]
279readline.write_history_file
280
281 filename as filename_obj: object = None
282 /
283
284Save a readline history file.
285
286The default filename is ~/.history.
287[clinic start generated code]*/
288
Skip Montanaro28067822000-07-06 18:55:12 +0000289static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600290readline_write_history_file_impl(PyObject *module, PyObject *filename_obj)
291/*[clinic end generated code: output=fbcad13d8ef59ae6 input=28a8e062fe363703]*/
Skip Montanaro28067822000-07-06 18:55:12 +0000292{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600293 PyObject *filename_bytes;
Serhiy Storchaka8f87eef2020-04-12 14:58:27 +0300294 const char *filename;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100295 int err;
Victor Stinner19e65a32010-06-11 22:27:14 +0000296 if (filename_obj != Py_None) {
297 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
298 return NULL;
Zackery Spytzb7047e52020-07-12 10:01:03 -0600299 filename = PyBytes_AS_STRING(filename_bytes);
Victor Stinner19e65a32010-06-11 22:27:14 +0000300 } else {
301 filename_bytes = NULL;
302 filename = NULL;
303 }
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100304 errno = err = write_history(filename);
305 if (!err && _history_length >= 0)
Victor Stinner19e65a32010-06-11 22:27:14 +0000306 history_truncate_file(filename, _history_length);
307 Py_XDECREF(filename_bytes);
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100308 errno = err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 if (errno)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300310 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000312}
313
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600314#ifdef HAVE_RL_APPEND_HISTORY
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600315/* Exported function to save part of a readline history file */
316
Zackery Spytzb7047e52020-07-12 10:01:03 -0600317/*[clinic input]
318readline.append_history_file
319
320 nelements: int
321 filename as filename_obj: object = None
322 /
323
324Append the last nelements items of the history list to file.
325
326The default filename is ~/.history.
327[clinic start generated code]*/
328
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600329static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600330readline_append_history_file_impl(PyObject *module, int nelements,
331 PyObject *filename_obj)
332/*[clinic end generated code: output=5df06fc9da56e4e4 input=784b774db3a4b7c5]*/
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600333{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600334 PyObject *filename_bytes;
Serhiy Storchaka8f87eef2020-04-12 14:58:27 +0300335 const char *filename;
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600336 int err;
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600337 if (filename_obj != Py_None) {
338 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
339 return NULL;
Zackery Spytzb7047e52020-07-12 10:01:03 -0600340 filename = PyBytes_AS_STRING(filename_bytes);
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600341 } else {
342 filename_bytes = NULL;
343 filename = NULL;
344 }
Gregory P. Smithfd053fd2021-02-12 12:04:46 -0800345 errno = err = append_history(
346 nelements - libedit_append_replace_history_offset, filename);
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600347 if (!err && _history_length >= 0)
348 history_truncate_file(filename, _history_length);
349 Py_XDECREF(filename_bytes);
350 errno = err;
351 if (errno)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300352 return PyErr_SetFromErrno(PyExc_OSError);
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600353 Py_RETURN_NONE;
354}
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600355#endif
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600356
357
Guido van Rossum74f31432003-01-07 20:01:29 +0000358/* Set history length */
359
Zackery Spytzb7047e52020-07-12 10:01:03 -0600360/*[clinic input]
361readline.set_history_length
362
363 length: int
364 /
365
366Set the maximal number of lines which will be written to the history file.
367
368A negative length is used to inhibit history truncation.
369[clinic start generated code]*/
370
371static PyObject *
372readline_set_history_length_impl(PyObject *module, int length)
373/*[clinic end generated code: output=e161a53e45987dc7 input=b8901bf16488b760]*/
Guido van Rossum74f31432003-01-07 20:01:29 +0000374{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 _history_length = length;
376 Py_RETURN_NONE;
Guido van Rossum74f31432003-01-07 20:01:29 +0000377}
378
Guido van Rossum74f31432003-01-07 20:01:29 +0000379/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000380
Zackery Spytzb7047e52020-07-12 10:01:03 -0600381/*[clinic input]
382readline.get_history_length
383
384Return the maximum number of lines that will be written to the history file.
385[clinic start generated code]*/
386
387static PyObject *
388readline_get_history_length_impl(PyObject *module)
389/*[clinic end generated code: output=83a2eeae35b6d2b9 input=5dce2eeba4327817]*/
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000390{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 return PyLong_FromLong(_history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000392}
393
Martin v. Löwis0daad592001-09-30 21:09:59 +0000394/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000395
Martin v. Löwis0daad592001-09-30 21:09:59 +0000396static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600397set_hook(const char *funcname, PyObject **hook_var, PyObject *function)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000398{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 if (function == Py_None) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200400 Py_CLEAR(*hook_var);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 }
402 else if (PyCallable_Check(function)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 Py_INCREF(function);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300404 Py_XSETREF(*hook_var, function);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 }
406 else {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100407 PyErr_Format(PyExc_TypeError,
408 "set_%.50s(func): argument not callable",
409 funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 return NULL;
411 }
412 Py_RETURN_NONE;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000413}
414
Zackery Spytzb7047e52020-07-12 10:01:03 -0600415/*[clinic input]
416readline.set_completion_display_matches_hook
417
418 function: object = None
419 /
420
421Set or remove the completion display function.
422
423The function is called as
424 function(substitution, [matches], longest_match_length)
425once each time matches need to be displayed.
426[clinic start generated code]*/
Guido van Rossum74f31432003-01-07 20:01:29 +0000427
Martin v. Löwis0daad592001-09-30 21:09:59 +0000428static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600429readline_set_completion_display_matches_hook_impl(PyObject *module,
430 PyObject *function)
431/*[clinic end generated code: output=516e5cb8db75a328 input=4f0bfd5ab0179a26]*/
Thomas Wouters89d996e2007-09-08 17:39:28 +0000432{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 PyObject *result = set_hook("completion_display_matches_hook",
Zackery Spytzb7047e52020-07-12 10:01:03 -0600434 &readlinestate_global->completion_display_matches_hook,
435 function);
Christian Heimes32fbe592007-11-12 15:01:33 +0000436#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 /* We cannot set this hook globally, since it replaces the
438 default completion display. */
439 rl_completion_display_matches_hook =
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200440 readlinestate_global->completion_display_matches_hook ?
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000441#if defined(_RL_FUNCTION_TYPEDEF)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000443#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 (VFunction *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000445#endif
Christian Heimes32fbe592007-11-12 15:01:33 +0000446#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 return result;
Christian Heimes32fbe592007-11-12 15:01:33 +0000448
Thomas Wouters89d996e2007-09-08 17:39:28 +0000449}
450
Zackery Spytzb7047e52020-07-12 10:01:03 -0600451/*[clinic input]
452readline.set_startup_hook
453
454 function: object = None
455 /
456
457Set or remove the function invoked by the rl_startup_hook callback.
458
459The function is called with no arguments just
460before readline prints the first prompt.
461[clinic start generated code]*/
Thomas Wouters89d996e2007-09-08 17:39:28 +0000462
463static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600464readline_set_startup_hook_impl(PyObject *module, PyObject *function)
465/*[clinic end generated code: output=02cd0e0c4fa082ad input=7783b4334b26d16d]*/
Martin v. Löwis0daad592001-09-30 21:09:59 +0000466{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600467 return set_hook("startup_hook", &readlinestate_global->startup_hook,
468 function);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000469}
470
Martin v. Löwis0daad592001-09-30 21:09:59 +0000471#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000472
473/* Set pre-input hook */
474
Zackery Spytzb7047e52020-07-12 10:01:03 -0600475/*[clinic input]
476readline.set_pre_input_hook
477
478 function: object = None
479 /
480
481Set or remove the function invoked by the rl_pre_input_hook callback.
482
483The function is called with no arguments after the first prompt
484has been printed and just before readline starts reading input
485characters.
486[clinic start generated code]*/
487
Martin v. Löwis0daad592001-09-30 21:09:59 +0000488static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600489readline_set_pre_input_hook_impl(PyObject *module, PyObject *function)
490/*[clinic end generated code: output=fe1a96505096f464 input=4f3eaeaf7ce1fdbe]*/
Martin v. Löwis0daad592001-09-30 21:09:59 +0000491{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600492 return set_hook("pre_input_hook", &readlinestate_global->pre_input_hook,
493 function);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000494}
Martin v. Löwis0daad592001-09-30 21:09:59 +0000495#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000496
Guido van Rossum74f31432003-01-07 20:01:29 +0000497
Thomas Wouters89d996e2007-09-08 17:39:28 +0000498/* Get the completion type for the scope of the tab-completion */
Zackery Spytzb7047e52020-07-12 10:01:03 -0600499
500/*[clinic input]
501readline.get_completion_type
502
503Get the type of completion being attempted.
504[clinic start generated code]*/
505
Thomas Wouters89d996e2007-09-08 17:39:28 +0000506static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600507readline_get_completion_type_impl(PyObject *module)
508/*[clinic end generated code: output=5c54d58a04997c07 input=04b92bc7a82dac91]*/
Thomas Wouters89d996e2007-09-08 17:39:28 +0000509{
Christian Heimes217cfd12007-12-02 14:31:20 +0000510 return PyLong_FromLong(rl_completion_type);
Thomas Wouters89d996e2007-09-08 17:39:28 +0000511}
512
Guido van Rossum74f31432003-01-07 20:01:29 +0000513/* Get the beginning index for the scope of the tab-completion */
514
Zackery Spytzb7047e52020-07-12 10:01:03 -0600515/*[clinic input]
516readline.get_begidx
517
518Get the beginning index of the completion scope.
519[clinic start generated code]*/
520
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000521static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600522readline_get_begidx_impl(PyObject *module)
523/*[clinic end generated code: output=362616ee8ed1b2b1 input=e083b81c8eb4bac3]*/
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000524{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200525 Py_INCREF(readlinestate_global->begidx);
526 return readlinestate_global->begidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000527}
528
Guido van Rossum74f31432003-01-07 20:01:29 +0000529/* Get the ending index for the scope of the tab-completion */
530
Zackery Spytzb7047e52020-07-12 10:01:03 -0600531/*[clinic input]
532readline.get_endidx
533
534Get the ending index of the completion scope.
535[clinic start generated code]*/
536
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000537static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600538readline_get_endidx_impl(PyObject *module)
539/*[clinic end generated code: output=7f763350b12d7517 input=d4c7e34a625fd770]*/
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000540{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200541 Py_INCREF(readlinestate_global->endidx);
542 return readlinestate_global->endidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000543}
544
Guido van Rossum74f31432003-01-07 20:01:29 +0000545/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000546
Zackery Spytzb7047e52020-07-12 10:01:03 -0600547/*[clinic input]
548readline.set_completer_delims
549
550 string: object
551 /
552
553Set the word delimiters for completion.
554[clinic start generated code]*/
555
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000556static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600557readline_set_completer_delims(PyObject *module, PyObject *string)
558/*[clinic end generated code: output=4305b266106c4f1f input=ae945337ebd01e20]*/
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 char *break_chars;
Martin Panterf00c49d2016-06-14 01:16:16 +0000561 PyObject *encoded = encode(string);
562 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 return NULL;
564 }
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200565 /* Keep a reference to the allocated memory in the module state in case
566 some other module modifies rl_completer_word_break_characters
567 (see issue #17289). */
Martin Panterf00c49d2016-06-14 01:16:16 +0000568 break_chars = strdup(PyBytes_AS_STRING(encoded));
569 Py_DECREF(encoded);
Serhiy Storchaka11384392015-09-27 22:34:59 +0300570 if (break_chars) {
571 free(completer_word_break_characters);
572 completer_word_break_characters = break_chars;
573 rl_completer_word_break_characters = break_chars;
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200574 Py_RETURN_NONE;
575 }
576 else
577 return PyErr_NoMemory();
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000578}
579
Mark Dickinson29b238e2010-08-03 16:08:16 +0000580/* _py_free_history_entry: Utility function to free a history entry. */
581
582#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
583
584/* Readline version >= 5.0 introduced a timestamp field into the history entry
585 structure; this needs to be freed to avoid a memory leak. This version of
586 readline also introduced the handy 'free_history_entry' function, which
587 takes care of the timestamp. */
588
589static void
590_py_free_history_entry(HIST_ENTRY *entry)
591{
592 histdata_t data = free_history_entry(entry);
593 free(data);
594}
595
596#else
597
598/* No free_history_entry function; free everything manually. */
599
600static void
601_py_free_history_entry(HIST_ENTRY *entry)
602{
603 if (entry->line)
604 free((void *)entry->line);
605 if (entry->data)
606 free(entry->data);
607 free(entry);
608}
609
610#endif
611
Zackery Spytzb7047e52020-07-12 10:01:03 -0600612/*[clinic input]
613readline.remove_history_item
614
615 pos as entry_number: int
616 /
617
Gregory P. Smithfd053fd2021-02-12 12:04:46 -0800618Remove history item given by its zero-based position.
Zackery Spytzb7047e52020-07-12 10:01:03 -0600619[clinic start generated code]*/
620
Skip Montanaroe5069012004-08-15 14:32:06 +0000621static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600622readline_remove_history_item_impl(PyObject *module, int entry_number)
Gregory P. Smithfd053fd2021-02-12 12:04:46 -0800623/*[clinic end generated code: output=ab114f029208c7e8 input=f248beb720ff1838]*/
Skip Montanaroe5069012004-08-15 14:32:06 +0000624{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 HIST_ENTRY *entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 if (entry_number < 0) {
628 PyErr_SetString(PyExc_ValueError,
629 "History index cannot be negative");
630 return NULL;
631 }
632 entry = remove_history(entry_number);
633 if (!entry) {
634 PyErr_Format(PyExc_ValueError,
635 "No history item at position %d",
636 entry_number);
637 return NULL;
638 }
639 /* free memory allocated for the history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000640 _py_free_history_entry(entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000642}
643
Zackery Spytzb7047e52020-07-12 10:01:03 -0600644/*[clinic input]
645readline.replace_history_item
646
647 pos as entry_number: int
648 line: unicode
649 /
650
651Replaces history item given by its position with contents of line.
Gregory P. Smithfd053fd2021-02-12 12:04:46 -0800652
653pos is zero-based.
Zackery Spytzb7047e52020-07-12 10:01:03 -0600654[clinic start generated code]*/
Skip Montanaroe5069012004-08-15 14:32:06 +0000655
656static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600657readline_replace_history_item_impl(PyObject *module, int entry_number,
658 PyObject *line)
Gregory P. Smithfd053fd2021-02-12 12:04:46 -0800659/*[clinic end generated code: output=f8cec2770ca125eb input=368bb66fe5ee5222]*/
Skip Montanaroe5069012004-08-15 14:32:06 +0000660{
Martin Panterf00c49d2016-06-14 01:16:16 +0000661 PyObject *encoded;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 HIST_ENTRY *old_entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 if (entry_number < 0) {
665 PyErr_SetString(PyExc_ValueError,
666 "History index cannot be negative");
667 return NULL;
668 }
Martin Panterf00c49d2016-06-14 01:16:16 +0000669 encoded = encode(line);
670 if (encoded == NULL) {
671 return NULL;
672 }
Gregory P. Smithfd053fd2021-02-12 12:04:46 -0800673 old_entry = replace_history_entry(
674 entry_number + libedit_append_replace_history_offset,
675 PyBytes_AS_STRING(encoded), (void *)NULL);
Martin Panterf00c49d2016-06-14 01:16:16 +0000676 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 if (!old_entry) {
678 PyErr_Format(PyExc_ValueError,
679 "No history item at position %d",
680 entry_number);
681 return NULL;
682 }
683 /* free memory allocated for the old history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000684 _py_free_history_entry(old_entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000686}
687
Guido van Rossum74f31432003-01-07 20:01:29 +0000688/* Add a line to the history buffer */
689
Zackery Spytzb7047e52020-07-12 10:01:03 -0600690/*[clinic input]
691readline.add_history
692
693 string: object
694 /
695
696Add an item to the history buffer.
697[clinic start generated code]*/
698
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000699static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600700readline_add_history(PyObject *module, PyObject *string)
701/*[clinic end generated code: output=b107b7e8106e803d input=e57c1cf6bc68d7e3]*/
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000702{
Martin Panterf00c49d2016-06-14 01:16:16 +0000703 PyObject *encoded = encode(string);
704 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 return NULL;
706 }
Martin Panterf00c49d2016-06-14 01:16:16 +0000707 add_history(PyBytes_AS_STRING(encoded));
708 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 Py_RETURN_NONE;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000710}
711
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000712static int should_auto_add_history = 1;
713
714/* Enable or disable automatic history */
715
Zackery Spytzb7047e52020-07-12 10:01:03 -0600716/*[clinic input]
717readline.set_auto_history
718
719 enabled as _should_auto_add_history: bool
720 /
721
722Enables or disables automatic history.
723[clinic start generated code]*/
724
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000725static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600726readline_set_auto_history_impl(PyObject *module,
727 int _should_auto_add_history)
728/*[clinic end generated code: output=619c6968246fd82b input=3d413073a1a03355]*/
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000729{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600730 should_auto_add_history = _should_auto_add_history;
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000731 Py_RETURN_NONE;
732}
733
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000734
Guido van Rossum74f31432003-01-07 20:01:29 +0000735/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000736
Zackery Spytzb7047e52020-07-12 10:01:03 -0600737/*[clinic input]
738readline.get_completer_delims
739
740Get the word delimiters for completion.
741[clinic start generated code]*/
742
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000743static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600744readline_get_completer_delims_impl(PyObject *module)
745/*[clinic end generated code: output=6b060280fa68ef43 input=e36eb14fb8a1f08a]*/
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000746{
Martin Panterf00c49d2016-06-14 01:16:16 +0000747 return decode(rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000748}
Guido van Rossum74f31432003-01-07 20:01:29 +0000749
Guido van Rossum74f31432003-01-07 20:01:29 +0000750/* Set the completer function */
751
Zackery Spytzb7047e52020-07-12 10:01:03 -0600752/*[clinic input]
753readline.set_completer
754
755 function: object = None
756 /
757
758Set or remove the completer function.
759
760The function is called as function(text, state),
761for state in 0, 1, 2, ..., until it returns a non-string.
762It should return the next possible completion starting with 'text'.
763[clinic start generated code]*/
764
Guido van Rossum290900a1997-09-26 21:51:21 +0000765static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600766readline_set_completer_impl(PyObject *module, PyObject *function)
767/*[clinic end generated code: output=171a2a60f81d3204 input=51e81e13118eb877]*/
Guido van Rossum290900a1997-09-26 21:51:21 +0000768{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600769 return set_hook("completer", &readlinestate_global->completer, function);
Guido van Rossum290900a1997-09-26 21:51:21 +0000770}
771
Zackery Spytzb7047e52020-07-12 10:01:03 -0600772/*[clinic input]
773readline.get_completer
Guido van Rossum290900a1997-09-26 21:51:21 +0000774
Zackery Spytzb7047e52020-07-12 10:01:03 -0600775Get the current completer function.
776[clinic start generated code]*/
Guido van Rossum74f31432003-01-07 20:01:29 +0000777
Michael W. Hudson796df152003-01-30 10:12:51 +0000778static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600779readline_get_completer_impl(PyObject *module)
780/*[clinic end generated code: output=6e6bbd8226d14475 input=6457522e56d70d13]*/
Michael W. Hudson796df152003-01-30 10:12:51 +0000781{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200782 if (readlinestate_global->completer == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 Py_RETURN_NONE;
784 }
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200785 Py_INCREF(readlinestate_global->completer);
786 return readlinestate_global->completer;
Michael W. Hudson796df152003-01-30 10:12:51 +0000787}
788
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000789/* Private function to get current length of history. XXX It may be
790 * possible to replace this with a direct use of history_length instead,
791 * but it's not clear whether BSD's libedit keeps history_length up to date.
792 * See issue #8065.*/
793
794static int
795_py_get_history_length(void)
796{
797 HISTORY_STATE *hist_st = history_get_history_state();
798 int length = hist_st->length;
799 /* the history docs don't say so, but the address of hist_st changes each
800 time history_get_history_state is called which makes me think it's
801 freshly malloc'd memory... on the other hand, the address of the last
802 line stays the same as long as history isn't extended, so it appears to
803 be malloc'd but managed by the history package... */
804 free(hist_st);
805 return length;
806}
807
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000808/* Exported function to get any element of history */
809
Zackery Spytzb7047e52020-07-12 10:01:03 -0600810/*[clinic input]
811readline.get_history_item
812
813 index as idx: int
814 /
815
Gregory P. Smithfd053fd2021-02-12 12:04:46 -0800816Return the current contents of history item at one-based index.
Zackery Spytzb7047e52020-07-12 10:01:03 -0600817[clinic start generated code]*/
818
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000819static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600820readline_get_history_item_impl(PyObject *module, int idx)
Gregory P. Smithfd053fd2021-02-12 12:04:46 -0800821/*[clinic end generated code: output=83d3e53ea5f34b3d input=8adf5c80e6c7ff2b]*/
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000822{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 HIST_ENTRY *hist_ent;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 if (using_libedit_emulation) {
Ned Deilyf70f4a62013-09-06 15:16:19 -0700826 /* Older versions of libedit's readline emulation
827 * use 0-based indexes, while readline and newer
828 * versions of libedit use 1-based indexes.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 */
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000830 int length = _py_get_history_length();
Ned Deilyf70f4a62013-09-06 15:16:19 -0700831
832 idx = idx - 1 + libedit_history_start;
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 /*
835 * Apple's readline emulation crashes when
836 * the index is out of range, therefore
837 * test for that and fail gracefully.
838 */
Ned Deilyf70f4a62013-09-06 15:16:19 -0700839 if (idx < (0 + libedit_history_start)
840 || idx >= (length + libedit_history_start)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 Py_RETURN_NONE;
842 }
843 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 if ((hist_ent = history_get(idx)))
Martin Panterf00c49d2016-06-14 01:16:16 +0000845 return decode(hist_ent->line);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 else {
847 Py_RETURN_NONE;
848 }
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000849}
850
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000851/* Exported function to get current length of history */
852
Zackery Spytzb7047e52020-07-12 10:01:03 -0600853/*[clinic input]
854readline.get_current_history_length
855
856Return the current (not the maximum) length of history.
857[clinic start generated code]*/
858
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000859static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600860readline_get_current_history_length_impl(PyObject *module)
861/*[clinic end generated code: output=436b294f12ba1e3f input=9cb3f431a68d071f]*/
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000862{
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000863 return PyLong_FromLong((long)_py_get_history_length());
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000864}
865
Guido van Rossum79378ff1997-10-07 14:53:21 +0000866/* Exported function to read the current line buffer */
867
Zackery Spytzb7047e52020-07-12 10:01:03 -0600868/*[clinic input]
869readline.get_line_buffer
870
871Return the current contents of the line buffer.
872[clinic start generated code]*/
873
Guido van Rossum79378ff1997-10-07 14:53:21 +0000874static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600875readline_get_line_buffer_impl(PyObject *module)
876/*[clinic end generated code: output=d22f9025ecad80e4 input=5f5fbc0d12c69412]*/
Guido van Rossum79378ff1997-10-07 14:53:21 +0000877{
Martin Panterf00c49d2016-06-14 01:16:16 +0000878 return decode(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000879}
880
Martin v. Löwise7a97962003-09-20 16:08:33 +0000881#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
882
883/* Exported function to clear the current history */
884
Zackery Spytzb7047e52020-07-12 10:01:03 -0600885/*[clinic input]
886readline.clear_history
887
888Clear the current readline history.
889[clinic start generated code]*/
890
Martin v. Löwise7a97962003-09-20 16:08:33 +0000891static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600892readline_clear_history_impl(PyObject *module)
893/*[clinic end generated code: output=1f2dbb0dfa5d5ebb input=208962c4393f5d16]*/
Martin v. Löwise7a97962003-09-20 16:08:33 +0000894{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 clear_history();
896 Py_RETURN_NONE;
Martin v. Löwise7a97962003-09-20 16:08:33 +0000897}
Martin v. Löwise7a97962003-09-20 16:08:33 +0000898#endif
899
900
Guido van Rossum79378ff1997-10-07 14:53:21 +0000901/* Exported function to insert text into the line buffer */
902
Zackery Spytzb7047e52020-07-12 10:01:03 -0600903/*[clinic input]
904readline.insert_text
905
906 string: object
907 /
908
909Insert text into the line buffer at the cursor position.
910[clinic start generated code]*/
911
Guido van Rossum79378ff1997-10-07 14:53:21 +0000912static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600913readline_insert_text(PyObject *module, PyObject *string)
914/*[clinic end generated code: output=23d792821d320c19 input=bc96c3c848d5ccb5]*/
Guido van Rossum79378ff1997-10-07 14:53:21 +0000915{
Martin Panterf00c49d2016-06-14 01:16:16 +0000916 PyObject *encoded = encode(string);
917 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 return NULL;
Martin Panterf00c49d2016-06-14 01:16:16 +0000919 }
920 rl_insert_text(PyBytes_AS_STRING(encoded));
921 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 Py_RETURN_NONE;
Guido van Rossum79378ff1997-10-07 14:53:21 +0000923}
924
Guido van Rossum74f31432003-01-07 20:01:29 +0000925/* Redisplay the line buffer */
926
Zackery Spytzb7047e52020-07-12 10:01:03 -0600927/*[clinic input]
928readline.redisplay
929
930Change what's displayed on the screen to reflect contents of the line buffer.
931[clinic start generated code]*/
932
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000933static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600934readline_redisplay_impl(PyObject *module)
935/*[clinic end generated code: output=a8b9725827c3c34b input=b485151058d75edc]*/
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000936{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 rl_redisplay();
938 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000939}
940
Zackery Spytzb7047e52020-07-12 10:01:03 -0600941#include "clinic/readline.c.h"
Guido van Rossum74f31432003-01-07 20:01:29 +0000942
Guido van Rossum290900a1997-09-26 21:51:21 +0000943/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000944
945static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000946{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600947 READLINE_PARSE_AND_BIND_METHODDEF
948 READLINE_GET_LINE_BUFFER_METHODDEF
949 READLINE_INSERT_TEXT_METHODDEF
950 READLINE_REDISPLAY_METHODDEF
951 READLINE_READ_INIT_FILE_METHODDEF
952 READLINE_READ_HISTORY_FILE_METHODDEF
953 READLINE_WRITE_HISTORY_FILE_METHODDEF
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600954#ifdef HAVE_RL_APPEND_HISTORY
Zackery Spytzb7047e52020-07-12 10:01:03 -0600955 READLINE_APPEND_HISTORY_FILE_METHODDEF
Ned Deily8007cbc2014-11-26 13:02:33 -0800956#endif
Zackery Spytzb7047e52020-07-12 10:01:03 -0600957 READLINE_GET_HISTORY_ITEM_METHODDEF
958 READLINE_GET_CURRENT_HISTORY_LENGTH_METHODDEF
959 READLINE_SET_HISTORY_LENGTH_METHODDEF
960 READLINE_GET_HISTORY_LENGTH_METHODDEF
961 READLINE_SET_COMPLETER_METHODDEF
962 READLINE_GET_COMPLETER_METHODDEF
963 READLINE_GET_COMPLETION_TYPE_METHODDEF
964 READLINE_GET_BEGIDX_METHODDEF
965 READLINE_GET_ENDIDX_METHODDEF
966 READLINE_SET_COMPLETER_DELIMS_METHODDEF
967 READLINE_SET_AUTO_HISTORY_METHODDEF
968 READLINE_ADD_HISTORY_METHODDEF
969 READLINE_REMOVE_HISTORY_ITEM_METHODDEF
970 READLINE_REPLACE_HISTORY_ITEM_METHODDEF
971 READLINE_GET_COMPLETER_DELIMS_METHODDEF
972 READLINE_SET_COMPLETION_DISPLAY_MATCHES_HOOK_METHODDEF
973 READLINE_SET_STARTUP_HOOK_METHODDEF
Martin v. Löwis0daad592001-09-30 21:09:59 +0000974#ifdef HAVE_RL_PRE_INPUT_HOOK
Zackery Spytzb7047e52020-07-12 10:01:03 -0600975 READLINE_SET_PRE_INPUT_HOOK_METHODDEF
Martin v. Löwis0daad592001-09-30 21:09:59 +0000976#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000977#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Zackery Spytzb7047e52020-07-12 10:01:03 -0600978 READLINE_CLEAR_HISTORY_METHODDEF
Martin v. Löwise7a97962003-09-20 16:08:33 +0000979#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000981};
982
Guido van Rossum05ac4492003-01-07 20:04:12 +0000983
Martin v. Löwis0daad592001-09-30 21:09:59 +0000984/* C function to call the Python hooks. */
985
986static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000987on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000988{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 int result = 0;
990 if (func != NULL) {
991 PyObject *r;
Victor Stinner2ff58a22019-06-17 14:27:23 +0200992 r = PyObject_CallNoArgs(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 if (r == NULL)
994 goto error;
995 if (r == Py_None)
996 result = 0;
997 else {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300998 result = _PyLong_AsInt(r);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 if (result == -1 && PyErr_Occurred())
1000 goto error;
1001 }
1002 Py_DECREF(r);
1003 goto done;
1004 error:
1005 PyErr_Clear();
1006 Py_XDECREF(r);
1007 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 return result;
1009 }
1010 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001011}
1012
1013static int
Ned Deily7b9ddea2014-02-05 16:53:10 -08001014#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwis0daad592001-09-30 21:09:59 +00001015on_startup_hook(void)
Ned Deily7b9ddea2014-02-05 16:53:10 -08001016#else
1017on_startup_hook()
1018#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +00001019{
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001020 int r;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001021 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001022 r = on_hook(readlinestate_global->startup_hook);
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001023 PyGILState_Release(gilstate);
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001024 return r;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001025}
1026
1027#ifdef HAVE_RL_PRE_INPUT_HOOK
1028static int
Ned Deily7b9ddea2014-02-05 16:53:10 -08001029#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwis0daad592001-09-30 21:09:59 +00001030on_pre_input_hook(void)
Ned Deily7b9ddea2014-02-05 16:53:10 -08001031#else
1032on_pre_input_hook()
1033#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +00001034{
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001035 int r;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001036 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001037 r = on_hook(readlinestate_global->pre_input_hook);
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001038 PyGILState_Release(gilstate);
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001039 return r;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001040}
1041#endif
1042
Guido van Rossum05ac4492003-01-07 20:04:12 +00001043
Thomas Wouters89d996e2007-09-08 17:39:28 +00001044/* C function to call the Python completion_display_matches */
1045
Georg Brandl646fdd62010-10-18 07:27:55 +00001046#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Thomas Wouters89d996e2007-09-08 17:39:28 +00001047static void
1048on_completion_display_matches_hook(char **matches,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 int num_matches, int max_length)
Thomas Wouters89d996e2007-09-08 17:39:28 +00001050{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 int i;
Martin Panterf00c49d2016-06-14 01:16:16 +00001052 PyObject *sub, *m=NULL, *s=NULL, *r=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 m = PyList_New(num_matches);
1055 if (m == NULL)
1056 goto error;
1057 for (i = 0; i < num_matches; i++) {
Martin Panterf00c49d2016-06-14 01:16:16 +00001058 s = decode(matches[i+1]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 if (s == NULL)
1060 goto error;
Zackery Spytz99d56b52018-12-08 07:16:55 -07001061 PyList_SET_ITEM(m, i, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 }
Martin Panterf00c49d2016-06-14 01:16:16 +00001063 sub = decode(matches[0]);
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001064 r = PyObject_CallFunction(readlinestate_global->completion_display_matches_hook,
Martin Panterf00c49d2016-06-14 01:16:16 +00001065 "NNi", sub, m, max_length);
Christian Heimes32fbe592007-11-12 15:01:33 +00001066
Martin Panterf00c49d2016-06-14 01:16:16 +00001067 m=NULL;
Christian Heimes32fbe592007-11-12 15:01:33 +00001068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 if (r == NULL ||
1070 (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) {
1071 goto error;
1072 }
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001073 Py_CLEAR(r);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074
1075 if (0) {
1076 error:
1077 PyErr_Clear();
1078 Py_XDECREF(m);
1079 Py_XDECREF(r);
1080 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 PyGILState_Release(gilstate);
Thomas Wouters89d996e2007-09-08 17:39:28 +00001082}
1083
Senthil Kumaran95c07002010-11-04 03:51:05 +00001084#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +00001085
Martin Panter5dbbf1a2016-04-03 02:54:58 +00001086#ifdef HAVE_RL_RESIZE_TERMINAL
1087static volatile sig_atomic_t sigwinch_received;
Martin Panter28f35b22016-04-03 08:00:49 +00001088static PyOS_sighandler_t sigwinch_ohandler;
Martin Panter5dbbf1a2016-04-03 02:54:58 +00001089
1090static void
1091readline_sigwinch_handler(int signum)
1092{
1093 sigwinch_received = 1;
1094 if (sigwinch_ohandler &&
1095 sigwinch_ohandler != SIG_IGN && sigwinch_ohandler != SIG_DFL)
1096 sigwinch_ohandler(signum);
1097
1098#ifndef HAVE_SIGACTION
1099 /* If the handler was installed with signal() rather than sigaction(),
1100 we need to reinstall it. */
1101 PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
1102#endif
1103}
1104#endif
1105
Guido van Rossum290900a1997-09-26 21:51:21 +00001106/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +00001107
Guido van Rossum290900a1997-09-26 21:51:21 +00001108static char *
Guido van Rossumd8faa362007-04-27 19:54:29 +00001109on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +00001110{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 char *result = NULL;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001112 if (readlinestate_global->completer != NULL) {
Martin Panterf00c49d2016-06-14 01:16:16 +00001113 PyObject *r = NULL, *t;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 rl_attempted_completion_over = 1;
Martin Panterf00c49d2016-06-14 01:16:16 +00001116 t = decode(text);
1117 r = PyObject_CallFunction(readlinestate_global->completer, "Ni", t, state);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 if (r == NULL)
1119 goto error;
1120 if (r == Py_None) {
1121 result = NULL;
1122 }
1123 else {
Martin Panterf00c49d2016-06-14 01:16:16 +00001124 PyObject *encoded = encode(r);
1125 if (encoded == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 goto error;
Martin Panterf00c49d2016-06-14 01:16:16 +00001127 result = strdup(PyBytes_AS_STRING(encoded));
1128 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 }
1130 Py_DECREF(r);
1131 goto done;
1132 error:
1133 PyErr_Clear();
1134 Py_XDECREF(r);
1135 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 PyGILState_Release(gilstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 return result;
1138 }
1139 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +00001140}
1141
Guido van Rossum290900a1997-09-26 21:51:21 +00001142
Guido van Rossum6d0d3652003-01-07 20:34:19 +00001143/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001144 * before calling the normal completer */
1145
Neal Norwitzc355f0c2003-02-21 00:30:18 +00001146static char **
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001147flex_complete(const char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001148{
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001149 char **result;
Martin Panterf00c49d2016-06-14 01:16:16 +00001150 char saved;
1151 size_t start_size, end_size;
1152 wchar_t *s;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001153 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitroudc0900b2009-10-19 18:22:37 +00001154#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 rl_completion_append_character ='\0';
Antoine Pitroud5131772009-10-26 19:22:14 +00001156#endif
1157#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 rl_completion_suppress_append = 0;
Antoine Pitroudc0900b2009-10-19 18:22:37 +00001159#endif
Martin Panterf00c49d2016-06-14 01:16:16 +00001160
1161 saved = rl_line_buffer[start];
1162 rl_line_buffer[start] = 0;
1163 s = Py_DecodeLocale(rl_line_buffer, &start_size);
1164 rl_line_buffer[start] = saved;
1165 if (s == NULL) {
1166 goto done;
1167 }
1168 PyMem_RawFree(s);
1169 saved = rl_line_buffer[end];
1170 rl_line_buffer[end] = 0;
1171 s = Py_DecodeLocale(rl_line_buffer + start, &end_size);
1172 rl_line_buffer[end] = saved;
1173 if (s == NULL) {
1174 goto done;
1175 }
1176 PyMem_RawFree(s);
1177 start = (int)start_size;
1178 end = start + (int)end_size;
1179
1180done:
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001181 Py_XDECREF(readlinestate_global->begidx);
1182 Py_XDECREF(readlinestate_global->endidx);
1183 readlinestate_global->begidx = PyLong_FromLong((long) start);
1184 readlinestate_global->endidx = PyLong_FromLong((long) end);
Benjamin Peterson1bb0c0b2016-09-05 18:26:19 -07001185 result = completion_matches((char *)text, *on_completion);
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001186 PyGILState_Release(gilstate);
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001187 return result;
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001188}
1189
Guido van Rossum05ac4492003-01-07 20:04:12 +00001190
Victor Stinner1d8da612019-10-30 16:39:27 +01001191/* Helper to initialize GNU readline properly.
1192 Return -1 on memory allocation failure, return 0 on success. */
1193static int
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001194setup_readline(readlinestate *mod_state)
Guido van Rossum290900a1997-09-26 21:51:21 +00001195{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +00001196#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Victor Stinner1d8da612019-10-30 16:39:27 +01001198 if (!saved_locale) {
1199 return -1;
1200 }
Guido van Rossum60c8a3a2002-10-09 21:27:33 +00001201#endif
1202
Zvezdan Petkovicc2f082e2018-05-17 02:45:10 -04001203 /* The name must be defined before initialization */
1204 rl_readline_name = "python";
1205
Victor Stinner6ced7c42011-03-21 18:15:42 +01001206 /* the libedit readline emulation resets key bindings etc
R. David Murray52d1b4e2010-12-18 03:48:32 +00001207 * when calling rl_initialize. So call it upfront
1208 */
1209 if (using_libedit_emulation)
1210 rl_initialize();
Ned Deilyf70f4a62013-09-06 15:16:19 -07001211
1212 /* Detect if libedit's readline emulation uses 0-based
1213 * indexing or 1-based indexing.
1214 */
1215 add_history("1");
1216 if (history_get(1) == NULL) {
1217 libedit_history_start = 0;
1218 } else {
1219 libedit_history_start = 1;
1220 }
Gregory P. Smithfd053fd2021-02-12 12:04:46 -08001221 /* Some libedit implementations use 1 based indexing on
1222 * replace_history_entry where libreadline uses 0 based.
1223 * The API our module presents is supposed to be 0 based.
1224 * It's a mad mad mad mad world.
1225 */
1226 {
1227 add_history("2");
1228 HIST_ENTRY *old_entry = replace_history_entry(1, "X", NULL);
1229 _py_free_history_entry(old_entry);
1230 HIST_ENTRY *item = history_get(libedit_history_start);
1231 if (item && item->line && strcmp(item->line, "X")) {
1232 libedit_append_replace_history_offset = 0;
1233 } else {
1234 libedit_append_replace_history_offset = 1;
1235 }
1236 }
Ned Deilyf70f4a62013-09-06 15:16:19 -07001237 clear_history();
R. David Murray52d1b4e2010-12-18 03:48:32 +00001238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 using_history();
Skip Montanaroa0392742002-06-11 14:32:46 +00001240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 /* Force rebind of TAB to insert-tab */
1242 rl_bind_key('\t', rl_insert);
1243 /* Bind both ESC-TAB and ESC-ESC to the completion function */
1244 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
1245 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin Panter5dbbf1a2016-04-03 02:54:58 +00001246#ifdef HAVE_RL_RESIZE_TERMINAL
1247 /* Set up signal handler for window resize */
1248 sigwinch_ohandler = PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
1249#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 /* Set our hook functions */
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001251 rl_startup_hook = on_startup_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001252#ifdef HAVE_RL_PRE_INPUT_HOOK
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001253 rl_pre_input_hook = on_pre_input_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001254#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 /* Set our completion function */
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001256 rl_attempted_completion_function = flex_complete;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 /* Set Python word break characters */
Antoine Pitroua7f7deb2013-05-06 21:51:03 +02001258 completer_word_break_characters =
1259 rl_completer_word_break_characters =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
1261 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001262
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001263 mod_state->begidx = PyLong_FromLong(0L);
1264 mod_state->endidx = PyLong_FromLong(0L);
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001265
Martin Panterc427b8d2016-08-27 03:23:11 +00001266 if (!using_libedit_emulation)
Martin Panterc427b8d2016-08-27 03:23:11 +00001267 {
1268 if (!isatty(STDOUT_FILENO)) {
1269 /* Issue #19884: stdout is not a terminal. Disable meta modifier
1270 keys to not write the ANSI sequence "\033[1034h" into stdout. On
1271 terminals supporting 8 bit characters like TERM=xterm-256color
1272 (which is now the default Fedora since Fedora 18), the meta key is
1273 used to enable support of 8 bit characters (ANSI sequence
1274 "\033[1034h").
1275
1276 With libedit, this call makes readline() crash. */
1277 rl_variable_bind ("enable-meta-key", "off");
1278 }
1279 }
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 /* Initialize (allows .inputrc to override)
1282 *
1283 * XXX: A bug in the readline-2.2 library causes a memory leak
1284 * inside this function. Nothing we can do about it.
1285 */
R. David Murray52d1b4e2010-12-18 03:48:32 +00001286 if (using_libedit_emulation)
Victor Stinner6ced7c42011-03-21 18:15:42 +01001287 rl_read_init_file(NULL);
R. David Murray52d1b4e2010-12-18 03:48:32 +00001288 else
R. David Murray52d1b4e2010-12-18 03:48:32 +00001289 rl_initialize();
Victor Stinner6ced7c42011-03-21 18:15:42 +01001290
Dustin Rodrigues755f3c12021-02-15 18:28:24 -05001291 disable_bracketed_paste();
1292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 RESTORE_LOCALE(saved_locale)
Victor Stinner1d8da612019-10-30 16:39:27 +01001294 return 0;
Guido van Rossum290900a1997-09-26 21:51:21 +00001295}
1296
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001297/* Wrapper around GNU readline that handles signals differently. */
1298
Antoine Pitrouf474c5a2017-07-18 17:05:03 +02001299static char *completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001300static void
1301rlhandler(char *text)
1302{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 completed_input_string = text;
1304 rl_callback_handler_remove();
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001305}
1306
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001307static char *
Serhiy Storchakac6792272013-10-19 21:03:34 +03001308readline_until_enter_or_signal(const char *prompt, int *signal)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001309{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 char * not_done_reading = "";
1311 fd_set selectset;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 *signal = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001314#ifdef HAVE_RL_CATCH_SIGNAL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 rl_catch_signals = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001316#endif
1317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 rl_callback_handler_install (prompt, rlhandler);
1319 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 completed_input_string = not_done_reading;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 while (completed_input_string == not_done_reading) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001324 int has_input = 0, err = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 while (!has_input)
1327 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Michael W. Hudson8da2b012004-10-07 13:46:33 +00001328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 /* [Bug #1552726] Only limit the pause if an input hook has been
1330 defined. */
1331 struct timeval *timeoutp = NULL;
1332 if (PyOS_InputHook)
1333 timeoutp = &timeout;
Martin Panter5dbbf1a2016-04-03 02:54:58 +00001334#ifdef HAVE_RL_RESIZE_TERMINAL
1335 /* Update readline's view of the window size after SIGWINCH */
1336 if (sigwinch_received) {
1337 sigwinch_received = 0;
1338 rl_resize_terminal();
1339 }
1340#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 FD_SET(fileno(rl_instream), &selectset);
1342 /* select resets selectset if no input was available */
1343 has_input = select(fileno(rl_instream) + 1, &selectset,
1344 NULL, NULL, timeoutp);
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001345 err = errno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 if(PyOS_InputHook) PyOS_InputHook();
1347 }
1348
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001349 if (has_input > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 rl_callback_read_char();
1351 }
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001352 else if (err == EINTR) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 int s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 PyEval_RestoreThread(_PyOS_ReadlineTState);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 s = PyErr_CheckSignals();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 PyEval_SaveThread();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 if (s < 0) {
1358 rl_free_line_state();
Martin Panterd6990d22016-03-22 07:24:05 +00001359#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0700
1360 rl_callback_sigcleanup();
1361#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 rl_cleanup_after_signal();
1363 rl_callback_handler_remove();
1364 *signal = 1;
1365 completed_input_string = NULL;
1366 }
1367 }
1368 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 return completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001371}
1372
1373
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001374static char *
Serhiy Storchakac6792272013-10-19 21:03:34 +03001375call_readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001376{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 size_t n;
Victor Stinner1600f602018-11-30 15:03:53 +01001378 char *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 int signal;
Neal Norwitz1fa040b2004-08-25 01:20:18 +00001380
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001381#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1383 if (!saved_locale)
1384 Py_FatalError("not enough memory to save locale");
xdegaye1588be62017-11-12 12:45:59 +01001385 _Py_SetLocaleFromEnv(LC_CTYPE);
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001386#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
1389 rl_instream = sys_stdin;
1390 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001391#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001393#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 }
Guido van Rossum74f31432003-01-07 20:01:29 +00001395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 p = readline_until_enter_or_signal(prompt, &signal);
Guido van Rossumb18618d2000-05-03 23:44:39 +00001397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 /* we got an interrupt signal */
1399 if (signal) {
1400 RESTORE_LOCALE(saved_locale)
1401 return NULL;
1402 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001403
Martin Panter7462b6492015-11-02 03:37:02 +00001404 /* We got an EOF, return an empty string. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 if (p == NULL) {
Victor Stinner2fe9bac2013-10-10 16:18:20 +02001406 p = PyMem_RawMalloc(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 if (p != NULL)
1408 *p = '\0';
1409 RESTORE_LOCALE(saved_locale)
1410 return p;
1411 }
1412
1413 /* we have a valid line */
1414 n = strlen(p);
Martin Panterf0dbf7a2016-05-15 01:26:25 +00001415 if (should_auto_add_history && n > 0) {
Brett Cannon2525dc82010-08-22 20:36:25 +00001416 const char *line;
Mark Dickinson6b54e1f2010-08-03 16:49:49 +00001417 int length = _py_get_history_length();
Nir Sofferfae8f4a2017-07-07 09:10:46 +03001418 if (length > 0) {
1419 HIST_ENTRY *hist_ent;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 if (using_libedit_emulation) {
Ned Deilyf70f4a62013-09-06 15:16:19 -07001421 /* handle older 0-based or newer 1-based indexing */
Nir Sofferfae8f4a2017-07-07 09:10:46 +03001422 hist_ent = history_get(length + libedit_history_start - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 } else
Nir Sofferfae8f4a2017-07-07 09:10:46 +03001424 hist_ent = history_get(length);
1425 line = hist_ent ? hist_ent->line : "";
1426 } else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 line = "";
1428 if (strcmp(p, line))
1429 add_history(p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 }
1431 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
1432 release the original. */
Victor Stinner1600f602018-11-30 15:03:53 +01001433 char *q = p;
Victor Stinner2fe9bac2013-10-10 16:18:20 +02001434 p = PyMem_RawMalloc(n+2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 if (p != NULL) {
Victor Stinner1600f602018-11-30 15:03:53 +01001436 memcpy(p, q, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 p[n] = '\n';
1438 p[n+1] = '\0';
1439 }
1440 free(q);
1441 RESTORE_LOCALE(saved_locale)
1442 return p;
Guido van Rossum0969d361997-08-05 21:27:50 +00001443}
1444
Guido van Rossum290900a1997-09-26 21:51:21 +00001445
1446/* Initialize the module */
1447
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001448PyDoc_STRVAR(doc_module,
1449"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001450
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001451PyDoc_STRVAR(doc_module_le,
1452"Importing this module enables command line editing using libedit readline.");
Martin v. Löwis1a214512008-06-11 05:26:20 +00001453
1454static struct PyModuleDef readlinemodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 PyModuleDef_HEAD_INIT,
1456 "readline",
1457 doc_module,
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001458 sizeof(readlinestate),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001459 readline_methods,
1460 NULL,
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001461 readline_traverse,
1462 readline_clear,
1463 readline_free
Martin v. Löwis1a214512008-06-11 05:26:20 +00001464};
1465
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001466
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001467PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001468PyInit_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001469{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 PyObject *m;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001471 readlinestate *mod_state;
Guido van Rossum290900a1997-09-26 21:51:21 +00001472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
1474 using_libedit_emulation = 1;
1475 }
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 if (using_libedit_emulation)
1478 readlinemodule.m_doc = doc_module_le;
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001479
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 m = PyModule_Create(&readlinemodule);
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 if (m == NULL)
1484 return NULL;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001485
Victor Stinner0efc0242017-11-30 17:21:07 +01001486 if (PyModule_AddIntConstant(m, "_READLINE_VERSION",
1487 RL_READLINE_VERSION) < 0) {
1488 goto error;
1489 }
1490 if (PyModule_AddIntConstant(m, "_READLINE_RUNTIME_VERSION",
1491 rl_readline_version) < 0) {
1492 goto error;
1493 }
1494 if (PyModule_AddStringConstant(m, "_READLINE_LIBRARY_VERSION",
1495 rl_library_version) < 0)
1496 {
1497 goto error;
1498 }
1499
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001500 mod_state = (readlinestate *) PyModule_GetState(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 PyOS_ReadlineFunctionPointer = call_readline;
Victor Stinner1d8da612019-10-30 16:39:27 +01001502 if (setup_readline(mod_state) < 0) {
1503 PyErr_NoMemory();
1504 goto error;
1505 }
Antoine Pitrou7e8b8672014-11-04 14:52:10 +01001506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 return m;
Victor Stinner0efc0242017-11-30 17:21:07 +01001508
1509error:
1510 Py_DECREF(m);
1511 return NULL;
Guido van Rossum0969d361997-08-05 21:27:50 +00001512}