blob: 12d6cc78e38a77491a21615c41573fe0ca4ad6f6 [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
Guido van Rossum290900a1997-09-26 21:51:21 +000029/* GNU readline definitions */
Guido van Rossumb0e51b22001-04-13 18:14:27 +000030#undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
Guido van Rossumbcc20741998-08-04 22:53:56 +000031#include <readline/readline.h>
32#include <readline/history.h>
Guido van Rossum730806d1998-04-10 22:27:42 +000033
Guido van Rossum353ae582001-07-10 16:45:32 +000034#ifdef HAVE_RL_COMPLETION_MATCHES
Guido van Rossum74f31432003-01-07 20:01:29 +000035#define completion_matches(x, y) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036 rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
Guido van Rossumd8faa362007-04-27 19:54:29 +000037#else
Martin v. Löwisb37509b2008-11-04 20:45:29 +000038#if defined(_RL_FUNCTION_TYPEDEF)
Guido van Rossumd8faa362007-04-27 19:54:29 +000039extern char **completion_matches(char *, rl_compentry_func_t *);
Martin v. Löwisb37509b2008-11-04 20:45:29 +000040#else
Ronald Oussoren25696bb2010-02-11 13:15:00 +000041
42#if !defined(__APPLE__)
Martin v. Löwisb37509b2008-11-04 20:45:29 +000043extern char **completion_matches(char *, CPFunction *);
44#endif
Guido van Rossum353ae582001-07-10 16:45:32 +000045#endif
Ronald Oussoren25696bb2010-02-11 13:15:00 +000046#endif
Guido van Rossum353ae582001-07-10 16:45:32 +000047
Ronald Oussoren2efd9242009-09-20 14:53:22 +000048/*
49 * It is possible to link the readline module to the readline
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 * emulation library of editline/libedit.
51 *
serge-sans-paille71053192019-12-04 17:02:57 +010052 * This emulation library is not 100% API compatible with the "real" readline
53 * and cannot be detected at compile-time,
54 * hence we use a runtime check to detect if the Python readlinke module is
55 * linked to libedit.
Ronald Oussoren2efd9242009-09-20 14:53:22 +000056 *
Ned Deily5d4121a2013-10-12 15:47:58 -070057 * Currently there is one known API incompatibility:
Ronald Oussoren2efd9242009-09-20 14:53:22 +000058 * - 'get_history' has a 1-based index with GNU readline, and a 0-based
Ned Deily5d4121a2013-10-12 15:47:58 -070059 * index with older versions of libedit's emulation.
Ronald Oussoren2efd9242009-09-20 14:53:22 +000060 * - Note that replace_history and remove_history use a 0-based index
Ned Deily5d4121a2013-10-12 15:47:58 -070061 * with both implementations.
Ronald Oussoren2efd9242009-09-20 14:53:22 +000062 */
63static int using_libedit_emulation = 0;
64static const char libedit_version_tag[] = "EditLine wrapper";
Ned Deilyf70f4a62013-09-06 15:16:19 -070065
66static int libedit_history_start = 0;
Ronald Oussoren2efd9242009-09-20 14:53:22 +000067
Georg Brandl646fdd62010-10-18 07:27:55 +000068#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Christian Heimes32fbe592007-11-12 15:01:33 +000069static void
70on_completion_display_matches_hook(char **matches,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 int num_matches, int max_length);
Georg Brandl646fdd62010-10-18 07:27:55 +000072#endif
Guido van Rossum0969d361997-08-05 21:27:50 +000073
Antoine Pitroua7f7deb2013-05-06 21:51:03 +020074/* Memory allocated for rl_completer_word_break_characters
75 (see issue #17289 for the motivation). */
76static char *completer_word_break_characters;
77
Antoine Pitrou5c30a752013-07-31 21:52:53 +020078typedef struct {
Martin Panterf6e9f472016-03-22 02:19:29 +000079 /* Specify hook functions in Python */
Antoine Pitrou5c30a752013-07-31 21:52:53 +020080 PyObject *completion_display_matches_hook;
81 PyObject *startup_hook;
82 PyObject *pre_input_hook;
Martin Panterf6e9f472016-03-22 02:19:29 +000083
84 PyObject *completer; /* Specify a word completer in Python */
Antoine Pitrou5c30a752013-07-31 21:52:53 +020085 PyObject *begidx;
86 PyObject *endidx;
87} readlinestate;
88
Hai Shif707d942020-03-16 21:15:01 +080089static inline readlinestate*
Hai Shi5f104d52020-03-17 01:16:32 +080090get_readline_state(PyObject *module)
Hai Shif707d942020-03-16 21:15:01 +080091{
92 void *state = PyModule_GetState(module);
93 assert(state != NULL);
94 return (readlinestate *)state;
95}
Antoine Pitrou5c30a752013-07-31 21:52:53 +020096
97static int
98readline_clear(PyObject *m)
99{
Hai Shif707d942020-03-16 21:15:01 +0800100 readlinestate *state = get_readline_state(m);
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200101 Py_CLEAR(state->completion_display_matches_hook);
102 Py_CLEAR(state->startup_hook);
103 Py_CLEAR(state->pre_input_hook);
104 Py_CLEAR(state->completer);
105 Py_CLEAR(state->begidx);
106 Py_CLEAR(state->endidx);
107 return 0;
108}
109
110static int
111readline_traverse(PyObject *m, visitproc visit, void *arg)
112{
Hai Shif707d942020-03-16 21:15:01 +0800113 readlinestate *state = get_readline_state(m);
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200114 Py_VISIT(state->completion_display_matches_hook);
115 Py_VISIT(state->startup_hook);
116 Py_VISIT(state->pre_input_hook);
117 Py_VISIT(state->completer);
118 Py_VISIT(state->begidx);
119 Py_VISIT(state->endidx);
120 return 0;
121}
122
123static void
124readline_free(void *m)
125{
126 readline_clear((PyObject *)m);
127}
128
129static PyModuleDef readlinemodule;
130
131#define readlinestate_global ((readlinestate *)PyModule_GetState(PyState_FindModule(&readlinemodule)))
132
133
Martin Panterf00c49d2016-06-14 01:16:16 +0000134/* Convert to/from multibyte C strings */
135
136static PyObject *
137encode(PyObject *b)
138{
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100139 return PyUnicode_EncodeLocale(b, "surrogateescape");
Martin Panterf00c49d2016-06-14 01:16:16 +0000140}
141
142static PyObject *
143decode(const char *s)
144{
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100145 return PyUnicode_DecodeLocale(s, "surrogateescape");
Martin Panterf00c49d2016-06-14 01:16:16 +0000146}
147
148
Guido van Rossum290900a1997-09-26 21:51:21 +0000149/* Exported function to send one line to readline's init file parser */
150
151static PyObject *
Martin Panterf00c49d2016-06-14 01:16:16 +0000152parse_and_bind(PyObject *self, PyObject *string)
Guido van Rossum290900a1997-09-26 21:51:21 +0000153{
Martin Panterf00c49d2016-06-14 01:16:16 +0000154 char *copy;
155 PyObject *encoded = encode(string);
156 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 return NULL;
Martin Panterf00c49d2016-06-14 01:16:16 +0000158 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 /* Make a copy -- rl_parse_and_bind() modifies its argument */
160 /* Bernard Herzog */
Martin Panterf00c49d2016-06-14 01:16:16 +0000161 copy = PyMem_Malloc(1 + PyBytes_GET_SIZE(encoded));
162 if (copy == NULL) {
163 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 return PyErr_NoMemory();
Martin Panterf00c49d2016-06-14 01:16:16 +0000165 }
166 strcpy(copy, PyBytes_AS_STRING(encoded));
167 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 rl_parse_and_bind(copy);
Victor Stinnerb6404912013-07-07 16:21:41 +0200169 PyMem_Free(copy); /* Free the copy */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +0000171}
172
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000173PyDoc_STRVAR(doc_parse_and_bind,
174"parse_and_bind(string) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000175Execute the init line provided in the string argument.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000176
177
178/* Exported function to parse a readline init file */
179
180static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000181read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000182{
Victor Stinner19e65a32010-06-11 22:27:14 +0000183 PyObject *filename_obj = Py_None, *filename_bytes;
184 if (!PyArg_ParseTuple(args, "|O:read_init_file", &filename_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 return NULL;
Victor Stinner19e65a32010-06-11 22:27:14 +0000186 if (filename_obj != Py_None) {
187 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
188 return NULL;
189 errno = rl_read_init_file(PyBytes_AsString(filename_bytes));
190 Py_DECREF(filename_bytes);
191 } else
192 errno = rl_read_init_file(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 if (errno)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300194 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +0000196}
197
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000198PyDoc_STRVAR(doc_read_init_file,
199"read_init_file([filename]) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000200Execute a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000201The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000202
203
Skip Montanaro28067822000-07-06 18:55:12 +0000204/* Exported function to load a readline history file */
205
206static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000207read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000208{
Victor Stinner19e65a32010-06-11 22:27:14 +0000209 PyObject *filename_obj = Py_None, *filename_bytes;
210 if (!PyArg_ParseTuple(args, "|O:read_history_file", &filename_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 return NULL;
Victor Stinner19e65a32010-06-11 22:27:14 +0000212 if (filename_obj != Py_None) {
213 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
214 return NULL;
215 errno = read_history(PyBytes_AsString(filename_bytes));
216 Py_DECREF(filename_bytes);
217 } else
218 errno = read_history(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 if (errno)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300220 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000222}
223
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000224static int _history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000225PyDoc_STRVAR(doc_read_history_file,
226"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000227Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000228The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000229
230
231/* Exported function to save a readline history file */
232
233static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000234write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000235{
Victor Stinner19e65a32010-06-11 22:27:14 +0000236 PyObject *filename_obj = Py_None, *filename_bytes;
Serhiy Storchaka8f87eef2020-04-12 14:58:27 +0300237 const char *filename;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100238 int err;
Victor Stinner19e65a32010-06-11 22:27:14 +0000239 if (!PyArg_ParseTuple(args, "|O:write_history_file", &filename_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 return NULL;
Victor Stinner19e65a32010-06-11 22:27:14 +0000241 if (filename_obj != Py_None) {
242 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
243 return NULL;
244 filename = PyBytes_AsString(filename_bytes);
245 } else {
246 filename_bytes = NULL;
247 filename = NULL;
248 }
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100249 errno = err = write_history(filename);
250 if (!err && _history_length >= 0)
Victor Stinner19e65a32010-06-11 22:27:14 +0000251 history_truncate_file(filename, _history_length);
252 Py_XDECREF(filename_bytes);
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100253 errno = err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 if (errno)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300255 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000257}
258
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000259PyDoc_STRVAR(doc_write_history_file,
260"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000261Save a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000262The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000263
264
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600265#ifdef HAVE_RL_APPEND_HISTORY
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600266/* Exported function to save part of a readline history file */
267
268static PyObject *
269append_history_file(PyObject *self, PyObject *args)
270{
271 int nelements;
272 PyObject *filename_obj = Py_None, *filename_bytes;
Serhiy Storchaka8f87eef2020-04-12 14:58:27 +0300273 const char *filename;
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600274 int err;
275 if (!PyArg_ParseTuple(args, "i|O:append_history_file", &nelements, &filename_obj))
276 return NULL;
277 if (filename_obj != Py_None) {
278 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
279 return NULL;
280 filename = PyBytes_AsString(filename_bytes);
281 } else {
282 filename_bytes = NULL;
283 filename = NULL;
284 }
285 errno = err = append_history(nelements, filename);
286 if (!err && _history_length >= 0)
287 history_truncate_file(filename, _history_length);
288 Py_XDECREF(filename_bytes);
289 errno = err;
290 if (errno)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300291 return PyErr_SetFromErrno(PyExc_OSError);
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600292 Py_RETURN_NONE;
293}
294
295PyDoc_STRVAR(doc_append_history_file,
296"append_history_file(nelements[, filename]) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000297Append the last nelements items of the history list to file.\n\
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600298The default filename is ~/.history.");
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600299#endif
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600300
301
Guido van Rossum74f31432003-01-07 20:01:29 +0000302/* Set history length */
303
304static PyObject*
305set_history_length(PyObject *self, PyObject *args)
306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 int length = _history_length;
308 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
309 return NULL;
310 _history_length = length;
311 Py_RETURN_NONE;
Guido van Rossum74f31432003-01-07 20:01:29 +0000312}
313
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000314PyDoc_STRVAR(set_history_length_doc,
315"set_history_length(length) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000316set the maximal number of lines which will be written to\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000317the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000318history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000319
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000320
Guido van Rossum74f31432003-01-07 20:01:29 +0000321/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000322
323static PyObject*
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000324get_history_length(PyObject *self, PyObject *noarg)
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 return PyLong_FromLong(_history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000327}
328
Guido van Rossum74f31432003-01-07 20:01:29 +0000329PyDoc_STRVAR(get_history_length_doc,
330"get_history_length() -> int\n\
Martin Panter0f767392016-04-05 07:37:22 +0000331return the maximum number of lines that will be written to\n\
Guido van Rossum74f31432003-01-07 20:01:29 +0000332the history file.");
333
334
Martin v. Löwis0daad592001-09-30 21:09:59 +0000335/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000336
Martin v. Löwis0daad592001-09-30 21:09:59 +0000337static PyObject *
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000338set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000339{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 PyObject *function = Py_None;
341 char buf[80];
342 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
343 if (!PyArg_ParseTuple(args, buf, &function))
344 return NULL;
345 if (function == Py_None) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200346 Py_CLEAR(*hook_var);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 }
348 else if (PyCallable_Check(function)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 Py_INCREF(function);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300350 Py_XSETREF(*hook_var, function);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 }
352 else {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100353 PyErr_Format(PyExc_TypeError,
354 "set_%.50s(func): argument not callable",
355 funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 return NULL;
357 }
358 Py_RETURN_NONE;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000359}
360
Guido van Rossum74f31432003-01-07 20:01:29 +0000361
Martin v. Löwis0daad592001-09-30 21:09:59 +0000362static PyObject *
Thomas Wouters89d996e2007-09-08 17:39:28 +0000363set_completion_display_matches_hook(PyObject *self, PyObject *args)
364{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 PyObject *result = set_hook("completion_display_matches_hook",
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200366 &readlinestate_global->completion_display_matches_hook, args);
Christian Heimes32fbe592007-11-12 15:01:33 +0000367#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 /* We cannot set this hook globally, since it replaces the
369 default completion display. */
370 rl_completion_display_matches_hook =
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200371 readlinestate_global->completion_display_matches_hook ?
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000372#if defined(_RL_FUNCTION_TYPEDEF)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000374#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 (VFunction *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000376#endif
Christian Heimes32fbe592007-11-12 15:01:33 +0000377#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 return result;
Christian Heimes32fbe592007-11-12 15:01:33 +0000379
Thomas Wouters89d996e2007-09-08 17:39:28 +0000380}
381
382PyDoc_STRVAR(doc_set_completion_display_matches_hook,
383"set_completion_display_matches_hook([function]) -> None\n\
384Set or remove the completion display function.\n\
385The function is called as\n\
386 function(substitution, [matches], longest_match_length)\n\
387once each time matches need to be displayed.");
388
389static PyObject *
Martin v. Löwis0daad592001-09-30 21:09:59 +0000390set_startup_hook(PyObject *self, PyObject *args)
391{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200392 return set_hook("startup_hook", &readlinestate_global->startup_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000393}
394
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000395PyDoc_STRVAR(doc_set_startup_hook,
396"set_startup_hook([function]) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000397Set or remove the function invoked by the rl_startup_hook callback.\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000398The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000399before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000400
Guido van Rossum74f31432003-01-07 20:01:29 +0000401
Martin v. Löwis0daad592001-09-30 21:09:59 +0000402#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000403
404/* Set pre-input hook */
405
Martin v. Löwis0daad592001-09-30 21:09:59 +0000406static PyObject *
407set_pre_input_hook(PyObject *self, PyObject *args)
408{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200409 return set_hook("pre_input_hook", &readlinestate_global->pre_input_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000410}
411
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000412PyDoc_STRVAR(doc_set_pre_input_hook,
413"set_pre_input_hook([function]) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000414Set or remove the function invoked by the rl_pre_input_hook callback.\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000415The function is called with no arguments after the first prompt\n\
416has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000417characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000418
Martin v. Löwis0daad592001-09-30 21:09:59 +0000419#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000420
Guido van Rossum74f31432003-01-07 20:01:29 +0000421
Thomas Wouters89d996e2007-09-08 17:39:28 +0000422/* Get the completion type for the scope of the tab-completion */
423static PyObject *
424get_completion_type(PyObject *self, PyObject *noarg)
425{
Christian Heimes217cfd12007-12-02 14:31:20 +0000426 return PyLong_FromLong(rl_completion_type);
Thomas Wouters89d996e2007-09-08 17:39:28 +0000427}
428
429PyDoc_STRVAR(doc_get_completion_type,
430"get_completion_type() -> int\n\
431Get the type of completion being attempted.");
432
433
Guido van Rossum74f31432003-01-07 20:01:29 +0000434/* Get the beginning index for the scope of the tab-completion */
435
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000436static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000437get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000438{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200439 Py_INCREF(readlinestate_global->begidx);
440 return readlinestate_global->begidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000441}
442
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000443PyDoc_STRVAR(doc_get_begidx,
444"get_begidx() -> int\n\
Martin Panter0f767392016-04-05 07:37:22 +0000445get the beginning index of the completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000446
Guido van Rossum74f31432003-01-07 20:01:29 +0000447
448/* Get the ending index for the scope of the tab-completion */
449
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000450static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000451get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000452{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200453 Py_INCREF(readlinestate_global->endidx);
454 return readlinestate_global->endidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000455}
456
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000457PyDoc_STRVAR(doc_get_endidx,
458"get_endidx() -> int\n\
Martin Panter0f767392016-04-05 07:37:22 +0000459get the ending index of the completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000460
461
Guido van Rossum74f31432003-01-07 20:01:29 +0000462/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000463
464static PyObject *
Martin Panterf00c49d2016-06-14 01:16:16 +0000465set_completer_delims(PyObject *self, PyObject *string)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000466{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 char *break_chars;
Martin Panterf00c49d2016-06-14 01:16:16 +0000468 PyObject *encoded = encode(string);
469 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 return NULL;
471 }
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200472 /* Keep a reference to the allocated memory in the module state in case
473 some other module modifies rl_completer_word_break_characters
474 (see issue #17289). */
Martin Panterf00c49d2016-06-14 01:16:16 +0000475 break_chars = strdup(PyBytes_AS_STRING(encoded));
476 Py_DECREF(encoded);
Serhiy Storchaka11384392015-09-27 22:34:59 +0300477 if (break_chars) {
478 free(completer_word_break_characters);
479 completer_word_break_characters = break_chars;
480 rl_completer_word_break_characters = break_chars;
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200481 Py_RETURN_NONE;
482 }
483 else
484 return PyErr_NoMemory();
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000485}
486
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000487PyDoc_STRVAR(doc_set_completer_delims,
488"set_completer_delims(string) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000489set the word delimiters for completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000490
Mark Dickinson29b238e2010-08-03 16:08:16 +0000491/* _py_free_history_entry: Utility function to free a history entry. */
492
493#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
494
495/* Readline version >= 5.0 introduced a timestamp field into the history entry
496 structure; this needs to be freed to avoid a memory leak. This version of
497 readline also introduced the handy 'free_history_entry' function, which
498 takes care of the timestamp. */
499
500static void
501_py_free_history_entry(HIST_ENTRY *entry)
502{
503 histdata_t data = free_history_entry(entry);
504 free(data);
505}
506
507#else
508
509/* No free_history_entry function; free everything manually. */
510
511static void
512_py_free_history_entry(HIST_ENTRY *entry)
513{
514 if (entry->line)
515 free((void *)entry->line);
516 if (entry->data)
517 free(entry->data);
518 free(entry);
519}
520
521#endif
522
Skip Montanaroe5069012004-08-15 14:32:06 +0000523static PyObject *
524py_remove_history(PyObject *self, PyObject *args)
525{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 int entry_number;
527 HIST_ENTRY *entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000528
Martin Panter0f767392016-04-05 07:37:22 +0000529 if (!PyArg_ParseTuple(args, "i:remove_history_item", &entry_number))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 return NULL;
531 if (entry_number < 0) {
532 PyErr_SetString(PyExc_ValueError,
533 "History index cannot be negative");
534 return NULL;
535 }
536 entry = remove_history(entry_number);
537 if (!entry) {
538 PyErr_Format(PyExc_ValueError,
539 "No history item at position %d",
540 entry_number);
541 return NULL;
542 }
543 /* free memory allocated for the history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000544 _py_free_history_entry(entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000546}
547
548PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000549"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000550remove history item given by its position");
551
552static PyObject *
553py_replace_history(PyObject *self, PyObject *args)
554{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 int entry_number;
Martin Panterf00c49d2016-06-14 01:16:16 +0000556 PyObject *line;
557 PyObject *encoded;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 HIST_ENTRY *old_entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000559
Martin Panterf00c49d2016-06-14 01:16:16 +0000560 if (!PyArg_ParseTuple(args, "iU:replace_history_item", &entry_number,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 &line)) {
562 return NULL;
563 }
564 if (entry_number < 0) {
565 PyErr_SetString(PyExc_ValueError,
566 "History index cannot be negative");
567 return NULL;
568 }
Martin Panterf00c49d2016-06-14 01:16:16 +0000569 encoded = encode(line);
570 if (encoded == NULL) {
571 return NULL;
572 }
573 old_entry = replace_history_entry(entry_number, PyBytes_AS_STRING(encoded), (void *)NULL);
574 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 if (!old_entry) {
576 PyErr_Format(PyExc_ValueError,
577 "No history item at position %d",
578 entry_number);
579 return NULL;
580 }
581 /* free memory allocated for the old history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000582 _py_free_history_entry(old_entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000584}
585
586PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000587"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000588replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000589
590/* Add a line to the history buffer */
591
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000592static PyObject *
Martin Panterf00c49d2016-06-14 01:16:16 +0000593py_add_history(PyObject *self, PyObject *string)
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000594{
Martin Panterf00c49d2016-06-14 01:16:16 +0000595 PyObject *encoded = encode(string);
596 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 return NULL;
598 }
Martin Panterf00c49d2016-06-14 01:16:16 +0000599 add_history(PyBytes_AS_STRING(encoded));
600 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 Py_RETURN_NONE;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000602}
603
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000604PyDoc_STRVAR(doc_add_history,
605"add_history(string) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000606add an item to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000607
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000608static int should_auto_add_history = 1;
609
610/* Enable or disable automatic history */
611
612static PyObject *
613py_set_auto_history(PyObject *self, PyObject *args)
614{
615 if (!PyArg_ParseTuple(args, "p:set_auto_history",
616 &should_auto_add_history)) {
617 return NULL;
618 }
619 Py_RETURN_NONE;
620}
621
622PyDoc_STRVAR(doc_set_auto_history,
623"set_auto_history(enabled) -> None\n\
624Enables or disables automatic history.");
625
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000626
Guido van Rossum74f31432003-01-07 20:01:29 +0000627/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000628
629static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000630get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000631{
Martin Panterf00c49d2016-06-14 01:16:16 +0000632 return decode(rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000633}
Guido van Rossum74f31432003-01-07 20:01:29 +0000634
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000635PyDoc_STRVAR(doc_get_completer_delims,
636"get_completer_delims() -> string\n\
Martin Panter0f767392016-04-05 07:37:22 +0000637get the word delimiters for completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000638
Guido van Rossum74f31432003-01-07 20:01:29 +0000639
640/* Set the completer function */
641
Guido van Rossum290900a1997-09-26 21:51:21 +0000642static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000643set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000644{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200645 return set_hook("completer", &readlinestate_global->completer, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000646}
647
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000648PyDoc_STRVAR(doc_set_completer,
649"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000650Set or remove the completer function.\n\
651The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000652for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000653It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000654
Guido van Rossum74f31432003-01-07 20:01:29 +0000655
Michael W. Hudson796df152003-01-30 10:12:51 +0000656static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000657get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000658{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200659 if (readlinestate_global->completer == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 Py_RETURN_NONE;
661 }
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200662 Py_INCREF(readlinestate_global->completer);
663 return readlinestate_global->completer;
Michael W. Hudson796df152003-01-30 10:12:51 +0000664}
665
666PyDoc_STRVAR(doc_get_completer,
667"get_completer() -> function\n\
668\n\
669Returns current completer function.");
670
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000671/* Private function to get current length of history. XXX It may be
672 * possible to replace this with a direct use of history_length instead,
673 * but it's not clear whether BSD's libedit keeps history_length up to date.
674 * See issue #8065.*/
675
676static int
677_py_get_history_length(void)
678{
679 HISTORY_STATE *hist_st = history_get_history_state();
680 int length = hist_st->length;
681 /* the history docs don't say so, but the address of hist_st changes each
682 time history_get_history_state is called which makes me think it's
683 freshly malloc'd memory... on the other hand, the address of the last
684 line stays the same as long as history isn't extended, so it appears to
685 be malloc'd but managed by the history package... */
686 free(hist_st);
687 return length;
688}
689
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000690/* Exported function to get any element of history */
691
692static PyObject *
693get_history_item(PyObject *self, PyObject *args)
694{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 int idx = 0;
696 HIST_ENTRY *hist_ent;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000697
Martin Panter0f767392016-04-05 07:37:22 +0000698 if (!PyArg_ParseTuple(args, "i:get_history_item", &idx))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 if (using_libedit_emulation) {
Ned Deilyf70f4a62013-09-06 15:16:19 -0700701 /* Older versions of libedit's readline emulation
702 * use 0-based indexes, while readline and newer
703 * versions of libedit use 1-based indexes.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 */
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000705 int length = _py_get_history_length();
Ned Deilyf70f4a62013-09-06 15:16:19 -0700706
707 idx = idx - 1 + libedit_history_start;
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 /*
710 * Apple's readline emulation crashes when
711 * the index is out of range, therefore
712 * test for that and fail gracefully.
713 */
Ned Deilyf70f4a62013-09-06 15:16:19 -0700714 if (idx < (0 + libedit_history_start)
715 || idx >= (length + libedit_history_start)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 Py_RETURN_NONE;
717 }
718 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 if ((hist_ent = history_get(idx)))
Martin Panterf00c49d2016-06-14 01:16:16 +0000720 return decode(hist_ent->line);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 else {
722 Py_RETURN_NONE;
723 }
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000724}
725
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000726PyDoc_STRVAR(doc_get_history_item,
727"get_history_item() -> string\n\
728return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000729
Guido van Rossum74f31432003-01-07 20:01:29 +0000730
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000731/* Exported function to get current length of history */
732
733static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000734get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000735{
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000736 return PyLong_FromLong((long)_py_get_history_length());
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000737}
738
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000739PyDoc_STRVAR(doc_get_current_history_length,
740"get_current_history_length() -> integer\n\
741return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000742
Guido van Rossum74f31432003-01-07 20:01:29 +0000743
Guido van Rossum79378ff1997-10-07 14:53:21 +0000744/* Exported function to read the current line buffer */
745
746static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000747get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000748{
Martin Panterf00c49d2016-06-14 01:16:16 +0000749 return decode(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000750}
751
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000752PyDoc_STRVAR(doc_get_line_buffer,
753"get_line_buffer() -> string\n\
754return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000755
Guido van Rossum74f31432003-01-07 20:01:29 +0000756
Martin v. Löwise7a97962003-09-20 16:08:33 +0000757#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
758
759/* Exported function to clear the current history */
760
761static PyObject *
762py_clear_history(PyObject *self, PyObject *noarg)
763{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 clear_history();
765 Py_RETURN_NONE;
Martin v. Löwise7a97962003-09-20 16:08:33 +0000766}
767
768PyDoc_STRVAR(doc_clear_history,
769"clear_history() -> None\n\
770Clear the current readline history.");
771#endif
772
773
Guido van Rossum79378ff1997-10-07 14:53:21 +0000774/* Exported function to insert text into the line buffer */
775
776static PyObject *
Martin Panterf00c49d2016-06-14 01:16:16 +0000777insert_text(PyObject *self, PyObject *string)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000778{
Martin Panterf00c49d2016-06-14 01:16:16 +0000779 PyObject *encoded = encode(string);
780 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 return NULL;
Martin Panterf00c49d2016-06-14 01:16:16 +0000782 }
783 rl_insert_text(PyBytes_AS_STRING(encoded));
784 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 Py_RETURN_NONE;
Guido van Rossum79378ff1997-10-07 14:53:21 +0000786}
787
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000788PyDoc_STRVAR(doc_insert_text,
789"insert_text(string) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000790Insert text into the line buffer at the cursor position.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000791
Guido van Rossum74f31432003-01-07 20:01:29 +0000792
793/* Redisplay the line buffer */
794
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000795static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000796redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000797{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 rl_redisplay();
799 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000800}
801
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000802PyDoc_STRVAR(doc_redisplay,
803"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000804Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000805contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000806
Guido van Rossum74f31432003-01-07 20:01:29 +0000807
Guido van Rossum290900a1997-09-26 21:51:21 +0000808/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000809
810static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000811{
Martin Panterf00c49d2016-06-14 01:16:16 +0000812 {"parse_and_bind", parse_and_bind, METH_O, doc_parse_and_bind},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
Martin Panterf00c49d2016-06-14 01:16:16 +0000814 {"insert_text", insert_text, METH_O, doc_insert_text},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
816 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
817 {"read_history_file", read_history_file,
818 METH_VARARGS, doc_read_history_file},
819 {"write_history_file", write_history_file,
820 METH_VARARGS, doc_write_history_file},
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600821#ifdef HAVE_RL_APPEND_HISTORY
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600822 {"append_history_file", append_history_file,
823 METH_VARARGS, doc_append_history_file},
Ned Deily8007cbc2014-11-26 13:02:33 -0800824#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 {"get_history_item", get_history_item,
826 METH_VARARGS, doc_get_history_item},
827 {"get_current_history_length", (PyCFunction)get_current_history_length,
828 METH_NOARGS, doc_get_current_history_length},
829 {"set_history_length", set_history_length,
830 METH_VARARGS, set_history_length_doc},
831 {"get_history_length", get_history_length,
832 METH_NOARGS, get_history_length_doc},
833 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
834 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
835 {"get_completion_type", get_completion_type,
836 METH_NOARGS, doc_get_completion_type},
837 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
838 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 {"set_completer_delims", set_completer_delims,
Martin Panterf00c49d2016-06-14 01:16:16 +0000841 METH_O, doc_set_completer_delims},
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000842 {"set_auto_history", py_set_auto_history, METH_VARARGS, doc_set_auto_history},
Martin Panterf00c49d2016-06-14 01:16:16 +0000843 {"add_history", py_add_history, METH_O, doc_add_history},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
845 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
846 {"get_completer_delims", get_completer_delims,
847 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 {"set_completion_display_matches_hook", set_completion_display_matches_hook,
850 METH_VARARGS, doc_set_completion_display_matches_hook},
851 {"set_startup_hook", set_startup_hook,
852 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000853#ifdef HAVE_RL_PRE_INPUT_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 {"set_pre_input_hook", set_pre_input_hook,
855 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000856#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000857#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
Martin v. Löwise7a97962003-09-20 16:08:33 +0000859#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000861};
862
Guido van Rossum05ac4492003-01-07 20:04:12 +0000863
Martin v. Löwis0daad592001-09-30 21:09:59 +0000864/* C function to call the Python hooks. */
865
866static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000867on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000868{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 int result = 0;
870 if (func != NULL) {
871 PyObject *r;
Victor Stinner2ff58a22019-06-17 14:27:23 +0200872 r = PyObject_CallNoArgs(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 if (r == NULL)
874 goto error;
875 if (r == Py_None)
876 result = 0;
877 else {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300878 result = _PyLong_AsInt(r);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 if (result == -1 && PyErr_Occurred())
880 goto error;
881 }
882 Py_DECREF(r);
883 goto done;
884 error:
885 PyErr_Clear();
886 Py_XDECREF(r);
887 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 return result;
889 }
890 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000891}
892
893static int
Ned Deily7b9ddea2014-02-05 16:53:10 -0800894#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000895on_startup_hook(void)
Ned Deily7b9ddea2014-02-05 16:53:10 -0800896#else
897on_startup_hook()
898#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000899{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200900 int r;
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200901 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200902 r = on_hook(readlinestate_global->startup_hook);
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200903 PyGILState_Release(gilstate);
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200904 return r;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000905}
906
907#ifdef HAVE_RL_PRE_INPUT_HOOK
908static int
Ned Deily7b9ddea2014-02-05 16:53:10 -0800909#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000910on_pre_input_hook(void)
Ned Deily7b9ddea2014-02-05 16:53:10 -0800911#else
912on_pre_input_hook()
913#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000914{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200915 int r;
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200916 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200917 r = on_hook(readlinestate_global->pre_input_hook);
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200918 PyGILState_Release(gilstate);
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200919 return r;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000920}
921#endif
922
Guido van Rossum05ac4492003-01-07 20:04:12 +0000923
Thomas Wouters89d996e2007-09-08 17:39:28 +0000924/* C function to call the Python completion_display_matches */
925
Georg Brandl646fdd62010-10-18 07:27:55 +0000926#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Thomas Wouters89d996e2007-09-08 17:39:28 +0000927static void
928on_completion_display_matches_hook(char **matches,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 int num_matches, int max_length)
Thomas Wouters89d996e2007-09-08 17:39:28 +0000930{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 int i;
Martin Panterf00c49d2016-06-14 01:16:16 +0000932 PyObject *sub, *m=NULL, *s=NULL, *r=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 m = PyList_New(num_matches);
935 if (m == NULL)
936 goto error;
937 for (i = 0; i < num_matches; i++) {
Martin Panterf00c49d2016-06-14 01:16:16 +0000938 s = decode(matches[i+1]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 if (s == NULL)
940 goto error;
Zackery Spytz99d56b52018-12-08 07:16:55 -0700941 PyList_SET_ITEM(m, i, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 }
Martin Panterf00c49d2016-06-14 01:16:16 +0000943 sub = decode(matches[0]);
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200944 r = PyObject_CallFunction(readlinestate_global->completion_display_matches_hook,
Martin Panterf00c49d2016-06-14 01:16:16 +0000945 "NNi", sub, m, max_length);
Christian Heimes32fbe592007-11-12 15:01:33 +0000946
Martin Panterf00c49d2016-06-14 01:16:16 +0000947 m=NULL;
Christian Heimes32fbe592007-11-12 15:01:33 +0000948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 if (r == NULL ||
950 (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) {
951 goto error;
952 }
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200953 Py_CLEAR(r);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954
955 if (0) {
956 error:
957 PyErr_Clear();
958 Py_XDECREF(m);
959 Py_XDECREF(r);
960 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 PyGILState_Release(gilstate);
Thomas Wouters89d996e2007-09-08 17:39:28 +0000962}
963
Senthil Kumaran95c07002010-11-04 03:51:05 +0000964#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000965
Martin Panter5dbbf1a2016-04-03 02:54:58 +0000966#ifdef HAVE_RL_RESIZE_TERMINAL
967static volatile sig_atomic_t sigwinch_received;
Martin Panter28f35b22016-04-03 08:00:49 +0000968static PyOS_sighandler_t sigwinch_ohandler;
Martin Panter5dbbf1a2016-04-03 02:54:58 +0000969
970static void
971readline_sigwinch_handler(int signum)
972{
973 sigwinch_received = 1;
974 if (sigwinch_ohandler &&
975 sigwinch_ohandler != SIG_IGN && sigwinch_ohandler != SIG_DFL)
976 sigwinch_ohandler(signum);
977
978#ifndef HAVE_SIGACTION
979 /* If the handler was installed with signal() rather than sigaction(),
980 we need to reinstall it. */
981 PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
982#endif
983}
984#endif
985
Guido van Rossum290900a1997-09-26 21:51:21 +0000986/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000987
Guido van Rossum290900a1997-09-26 21:51:21 +0000988static char *
Guido van Rossumd8faa362007-04-27 19:54:29 +0000989on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000990{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 char *result = NULL;
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200992 if (readlinestate_global->completer != NULL) {
Martin Panterf00c49d2016-06-14 01:16:16 +0000993 PyObject *r = NULL, *t;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 rl_attempted_completion_over = 1;
Martin Panterf00c49d2016-06-14 01:16:16 +0000996 t = decode(text);
997 r = PyObject_CallFunction(readlinestate_global->completer, "Ni", t, state);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 if (r == NULL)
999 goto error;
1000 if (r == Py_None) {
1001 result = NULL;
1002 }
1003 else {
Martin Panterf00c49d2016-06-14 01:16:16 +00001004 PyObject *encoded = encode(r);
1005 if (encoded == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 goto error;
Martin Panterf00c49d2016-06-14 01:16:16 +00001007 result = strdup(PyBytes_AS_STRING(encoded));
1008 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 }
1010 Py_DECREF(r);
1011 goto done;
1012 error:
1013 PyErr_Clear();
1014 Py_XDECREF(r);
1015 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 PyGILState_Release(gilstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 return result;
1018 }
1019 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +00001020}
1021
Guido van Rossum290900a1997-09-26 21:51:21 +00001022
Guido van Rossum6d0d3652003-01-07 20:34:19 +00001023/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001024 * before calling the normal completer */
1025
Neal Norwitzc355f0c2003-02-21 00:30:18 +00001026static char **
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001027flex_complete(const char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001028{
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001029 char **result;
Martin Panterf00c49d2016-06-14 01:16:16 +00001030 char saved;
1031 size_t start_size, end_size;
1032 wchar_t *s;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001033 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitroudc0900b2009-10-19 18:22:37 +00001034#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 rl_completion_append_character ='\0';
Antoine Pitroud5131772009-10-26 19:22:14 +00001036#endif
1037#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 rl_completion_suppress_append = 0;
Antoine Pitroudc0900b2009-10-19 18:22:37 +00001039#endif
Martin Panterf00c49d2016-06-14 01:16:16 +00001040
1041 saved = rl_line_buffer[start];
1042 rl_line_buffer[start] = 0;
1043 s = Py_DecodeLocale(rl_line_buffer, &start_size);
1044 rl_line_buffer[start] = saved;
1045 if (s == NULL) {
1046 goto done;
1047 }
1048 PyMem_RawFree(s);
1049 saved = rl_line_buffer[end];
1050 rl_line_buffer[end] = 0;
1051 s = Py_DecodeLocale(rl_line_buffer + start, &end_size);
1052 rl_line_buffer[end] = saved;
1053 if (s == NULL) {
1054 goto done;
1055 }
1056 PyMem_RawFree(s);
1057 start = (int)start_size;
1058 end = start + (int)end_size;
1059
1060done:
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001061 Py_XDECREF(readlinestate_global->begidx);
1062 Py_XDECREF(readlinestate_global->endidx);
1063 readlinestate_global->begidx = PyLong_FromLong((long) start);
1064 readlinestate_global->endidx = PyLong_FromLong((long) end);
Benjamin Peterson1bb0c0b2016-09-05 18:26:19 -07001065 result = completion_matches((char *)text, *on_completion);
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001066 PyGILState_Release(gilstate);
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001067 return result;
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001068}
1069
Guido van Rossum05ac4492003-01-07 20:04:12 +00001070
Victor Stinner1d8da612019-10-30 16:39:27 +01001071/* Helper to initialize GNU readline properly.
1072 Return -1 on memory allocation failure, return 0 on success. */
1073static int
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001074setup_readline(readlinestate *mod_state)
Guido van Rossum290900a1997-09-26 21:51:21 +00001075{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +00001076#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Victor Stinner1d8da612019-10-30 16:39:27 +01001078 if (!saved_locale) {
1079 return -1;
1080 }
Guido van Rossum60c8a3a2002-10-09 21:27:33 +00001081#endif
1082
Zvezdan Petkovicc2f082e2018-05-17 02:45:10 -04001083 /* The name must be defined before initialization */
1084 rl_readline_name = "python";
1085
Victor Stinner6ced7c42011-03-21 18:15:42 +01001086 /* the libedit readline emulation resets key bindings etc
R. David Murray52d1b4e2010-12-18 03:48:32 +00001087 * when calling rl_initialize. So call it upfront
1088 */
1089 if (using_libedit_emulation)
1090 rl_initialize();
Ned Deilyf70f4a62013-09-06 15:16:19 -07001091
1092 /* Detect if libedit's readline emulation uses 0-based
1093 * indexing or 1-based indexing.
1094 */
1095 add_history("1");
1096 if (history_get(1) == NULL) {
1097 libedit_history_start = 0;
1098 } else {
1099 libedit_history_start = 1;
1100 }
1101 clear_history();
R. David Murray52d1b4e2010-12-18 03:48:32 +00001102
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 using_history();
Skip Montanaroa0392742002-06-11 14:32:46 +00001104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 /* Force rebind of TAB to insert-tab */
1106 rl_bind_key('\t', rl_insert);
1107 /* Bind both ESC-TAB and ESC-ESC to the completion function */
1108 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
1109 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin Panter5dbbf1a2016-04-03 02:54:58 +00001110#ifdef HAVE_RL_RESIZE_TERMINAL
1111 /* Set up signal handler for window resize */
1112 sigwinch_ohandler = PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
1113#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 /* Set our hook functions */
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001115 rl_startup_hook = on_startup_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001116#ifdef HAVE_RL_PRE_INPUT_HOOK
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001117 rl_pre_input_hook = on_pre_input_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001118#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 /* Set our completion function */
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001120 rl_attempted_completion_function = flex_complete;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 /* Set Python word break characters */
Antoine Pitroua7f7deb2013-05-06 21:51:03 +02001122 completer_word_break_characters =
1123 rl_completer_word_break_characters =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
1125 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001126
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001127 mod_state->begidx = PyLong_FromLong(0L);
1128 mod_state->endidx = PyLong_FromLong(0L);
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001129
Martin Panterc427b8d2016-08-27 03:23:11 +00001130 if (!using_libedit_emulation)
Martin Panterc427b8d2016-08-27 03:23:11 +00001131 {
1132 if (!isatty(STDOUT_FILENO)) {
1133 /* Issue #19884: stdout is not a terminal. Disable meta modifier
1134 keys to not write the ANSI sequence "\033[1034h" into stdout. On
1135 terminals supporting 8 bit characters like TERM=xterm-256color
1136 (which is now the default Fedora since Fedora 18), the meta key is
1137 used to enable support of 8 bit characters (ANSI sequence
1138 "\033[1034h").
1139
1140 With libedit, this call makes readline() crash. */
1141 rl_variable_bind ("enable-meta-key", "off");
1142 }
1143 }
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 /* Initialize (allows .inputrc to override)
1146 *
1147 * XXX: A bug in the readline-2.2 library causes a memory leak
1148 * inside this function. Nothing we can do about it.
1149 */
R. David Murray52d1b4e2010-12-18 03:48:32 +00001150 if (using_libedit_emulation)
Victor Stinner6ced7c42011-03-21 18:15:42 +01001151 rl_read_init_file(NULL);
R. David Murray52d1b4e2010-12-18 03:48:32 +00001152 else
R. David Murray52d1b4e2010-12-18 03:48:32 +00001153 rl_initialize();
Victor Stinner6ced7c42011-03-21 18:15:42 +01001154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 RESTORE_LOCALE(saved_locale)
Victor Stinner1d8da612019-10-30 16:39:27 +01001156 return 0;
Guido van Rossum290900a1997-09-26 21:51:21 +00001157}
1158
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001159/* Wrapper around GNU readline that handles signals differently. */
1160
Antoine Pitrouf474c5a2017-07-18 17:05:03 +02001161static char *completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001162static void
1163rlhandler(char *text)
1164{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 completed_input_string = text;
1166 rl_callback_handler_remove();
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001167}
1168
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001169static char *
Serhiy Storchakac6792272013-10-19 21:03:34 +03001170readline_until_enter_or_signal(const char *prompt, int *signal)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001171{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 char * not_done_reading = "";
1173 fd_set selectset;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 *signal = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001176#ifdef HAVE_RL_CATCH_SIGNAL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 rl_catch_signals = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001178#endif
1179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 rl_callback_handler_install (prompt, rlhandler);
1181 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 completed_input_string = not_done_reading;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 while (completed_input_string == not_done_reading) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001186 int has_input = 0, err = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 while (!has_input)
1189 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Michael W. Hudson8da2b012004-10-07 13:46:33 +00001190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 /* [Bug #1552726] Only limit the pause if an input hook has been
1192 defined. */
1193 struct timeval *timeoutp = NULL;
1194 if (PyOS_InputHook)
1195 timeoutp = &timeout;
Martin Panter5dbbf1a2016-04-03 02:54:58 +00001196#ifdef HAVE_RL_RESIZE_TERMINAL
1197 /* Update readline's view of the window size after SIGWINCH */
1198 if (sigwinch_received) {
1199 sigwinch_received = 0;
1200 rl_resize_terminal();
1201 }
1202#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 FD_SET(fileno(rl_instream), &selectset);
1204 /* select resets selectset if no input was available */
1205 has_input = select(fileno(rl_instream) + 1, &selectset,
1206 NULL, NULL, timeoutp);
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001207 err = errno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 if(PyOS_InputHook) PyOS_InputHook();
1209 }
1210
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001211 if (has_input > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 rl_callback_read_char();
1213 }
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001214 else if (err == EINTR) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 int s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 PyEval_RestoreThread(_PyOS_ReadlineTState);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 s = PyErr_CheckSignals();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 PyEval_SaveThread();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 if (s < 0) {
1220 rl_free_line_state();
Martin Panterd6990d22016-03-22 07:24:05 +00001221#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0700
1222 rl_callback_sigcleanup();
1223#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 rl_cleanup_after_signal();
1225 rl_callback_handler_remove();
1226 *signal = 1;
1227 completed_input_string = NULL;
1228 }
1229 }
1230 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 return completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001233}
1234
1235
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001236static char *
Serhiy Storchakac6792272013-10-19 21:03:34 +03001237call_readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001238{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 size_t n;
Victor Stinner1600f602018-11-30 15:03:53 +01001240 char *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 int signal;
Neal Norwitz1fa040b2004-08-25 01:20:18 +00001242
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001243#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1245 if (!saved_locale)
1246 Py_FatalError("not enough memory to save locale");
xdegaye1588be62017-11-12 12:45:59 +01001247 _Py_SetLocaleFromEnv(LC_CTYPE);
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001248#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
1251 rl_instream = sys_stdin;
1252 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001253#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001255#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 }
Guido van Rossum74f31432003-01-07 20:01:29 +00001257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 p = readline_until_enter_or_signal(prompt, &signal);
Guido van Rossumb18618d2000-05-03 23:44:39 +00001259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 /* we got an interrupt signal */
1261 if (signal) {
1262 RESTORE_LOCALE(saved_locale)
1263 return NULL;
1264 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001265
Martin Panter7462b6492015-11-02 03:37:02 +00001266 /* We got an EOF, return an empty string. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 if (p == NULL) {
Victor Stinner2fe9bac2013-10-10 16:18:20 +02001268 p = PyMem_RawMalloc(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 if (p != NULL)
1270 *p = '\0';
1271 RESTORE_LOCALE(saved_locale)
1272 return p;
1273 }
1274
1275 /* we have a valid line */
1276 n = strlen(p);
Martin Panterf0dbf7a2016-05-15 01:26:25 +00001277 if (should_auto_add_history && n > 0) {
Brett Cannon2525dc82010-08-22 20:36:25 +00001278 const char *line;
Mark Dickinson6b54e1f2010-08-03 16:49:49 +00001279 int length = _py_get_history_length();
Nir Sofferfae8f4a2017-07-07 09:10:46 +03001280 if (length > 0) {
1281 HIST_ENTRY *hist_ent;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 if (using_libedit_emulation) {
Ned Deilyf70f4a62013-09-06 15:16:19 -07001283 /* handle older 0-based or newer 1-based indexing */
Nir Sofferfae8f4a2017-07-07 09:10:46 +03001284 hist_ent = history_get(length + libedit_history_start - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 } else
Nir Sofferfae8f4a2017-07-07 09:10:46 +03001286 hist_ent = history_get(length);
1287 line = hist_ent ? hist_ent->line : "";
1288 } else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 line = "";
1290 if (strcmp(p, line))
1291 add_history(p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 }
1293 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
1294 release the original. */
Victor Stinner1600f602018-11-30 15:03:53 +01001295 char *q = p;
Victor Stinner2fe9bac2013-10-10 16:18:20 +02001296 p = PyMem_RawMalloc(n+2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 if (p != NULL) {
Victor Stinner1600f602018-11-30 15:03:53 +01001298 memcpy(p, q, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 p[n] = '\n';
1300 p[n+1] = '\0';
1301 }
1302 free(q);
1303 RESTORE_LOCALE(saved_locale)
1304 return p;
Guido van Rossum0969d361997-08-05 21:27:50 +00001305}
1306
Guido van Rossum290900a1997-09-26 21:51:21 +00001307
1308/* Initialize the module */
1309
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001310PyDoc_STRVAR(doc_module,
1311"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001312
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001313PyDoc_STRVAR(doc_module_le,
1314"Importing this module enables command line editing using libedit readline.");
Martin v. Löwis1a214512008-06-11 05:26:20 +00001315
1316static struct PyModuleDef readlinemodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 PyModuleDef_HEAD_INIT,
1318 "readline",
1319 doc_module,
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001320 sizeof(readlinestate),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 readline_methods,
1322 NULL,
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001323 readline_traverse,
1324 readline_clear,
1325 readline_free
Martin v. Löwis1a214512008-06-11 05:26:20 +00001326};
1327
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001328
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001329PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001330PyInit_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001331{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 PyObject *m;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001333 readlinestate *mod_state;
Guido van Rossum290900a1997-09-26 21:51:21 +00001334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
1336 using_libedit_emulation = 1;
1337 }
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 if (using_libedit_emulation)
1340 readlinemodule.m_doc = doc_module_le;
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001341
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 m = PyModule_Create(&readlinemodule);
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 if (m == NULL)
1346 return NULL;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001347
Victor Stinner0efc0242017-11-30 17:21:07 +01001348 if (PyModule_AddIntConstant(m, "_READLINE_VERSION",
1349 RL_READLINE_VERSION) < 0) {
1350 goto error;
1351 }
1352 if (PyModule_AddIntConstant(m, "_READLINE_RUNTIME_VERSION",
1353 rl_readline_version) < 0) {
1354 goto error;
1355 }
1356 if (PyModule_AddStringConstant(m, "_READLINE_LIBRARY_VERSION",
1357 rl_library_version) < 0)
1358 {
1359 goto error;
1360 }
1361
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001362 mod_state = (readlinestate *) PyModule_GetState(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 PyOS_ReadlineFunctionPointer = call_readline;
Victor Stinner1d8da612019-10-30 16:39:27 +01001364 if (setup_readline(mod_state) < 0) {
1365 PyErr_NoMemory();
1366 goto error;
1367 }
Antoine Pitrou7e8b8672014-11-04 14:52:10 +01001368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 return m;
Victor Stinner0efc0242017-11-30 17:21:07 +01001370
1371error:
1372 Py_DECREF(m);
1373 return NULL;
Guido van Rossum0969d361997-08-05 21:27:50 +00001374}