blob: 27a993f449f33ecb3d57ce3e103ac40a96902ec5 [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
89
90#define readline_state(o) ((readlinestate *)PyModule_GetState(o))
91
92static int
93readline_clear(PyObject *m)
94{
95 readlinestate *state = readline_state(m);
96 Py_CLEAR(state->completion_display_matches_hook);
97 Py_CLEAR(state->startup_hook);
98 Py_CLEAR(state->pre_input_hook);
99 Py_CLEAR(state->completer);
100 Py_CLEAR(state->begidx);
101 Py_CLEAR(state->endidx);
102 return 0;
103}
104
105static int
106readline_traverse(PyObject *m, visitproc visit, void *arg)
107{
108 readlinestate *state = readline_state(m);
109 Py_VISIT(state->completion_display_matches_hook);
110 Py_VISIT(state->startup_hook);
111 Py_VISIT(state->pre_input_hook);
112 Py_VISIT(state->completer);
113 Py_VISIT(state->begidx);
114 Py_VISIT(state->endidx);
115 return 0;
116}
117
118static void
119readline_free(void *m)
120{
121 readline_clear((PyObject *)m);
122}
123
124static PyModuleDef readlinemodule;
125
126#define readlinestate_global ((readlinestate *)PyModule_GetState(PyState_FindModule(&readlinemodule)))
127
128
Martin Panterf00c49d2016-06-14 01:16:16 +0000129/* Convert to/from multibyte C strings */
130
131static PyObject *
132encode(PyObject *b)
133{
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100134 return PyUnicode_EncodeLocale(b, "surrogateescape");
Martin Panterf00c49d2016-06-14 01:16:16 +0000135}
136
137static PyObject *
138decode(const char *s)
139{
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100140 return PyUnicode_DecodeLocale(s, "surrogateescape");
Martin Panterf00c49d2016-06-14 01:16:16 +0000141}
142
143
Guido van Rossum290900a1997-09-26 21:51:21 +0000144/* Exported function to send one line to readline's init file parser */
145
146static PyObject *
Martin Panterf00c49d2016-06-14 01:16:16 +0000147parse_and_bind(PyObject *self, PyObject *string)
Guido van Rossum290900a1997-09-26 21:51:21 +0000148{
Martin Panterf00c49d2016-06-14 01:16:16 +0000149 char *copy;
150 PyObject *encoded = encode(string);
151 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 return NULL;
Martin Panterf00c49d2016-06-14 01:16:16 +0000153 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 /* Make a copy -- rl_parse_and_bind() modifies its argument */
155 /* Bernard Herzog */
Martin Panterf00c49d2016-06-14 01:16:16 +0000156 copy = PyMem_Malloc(1 + PyBytes_GET_SIZE(encoded));
157 if (copy == NULL) {
158 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 return PyErr_NoMemory();
Martin Panterf00c49d2016-06-14 01:16:16 +0000160 }
161 strcpy(copy, PyBytes_AS_STRING(encoded));
162 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 rl_parse_and_bind(copy);
Victor Stinnerb6404912013-07-07 16:21:41 +0200164 PyMem_Free(copy); /* Free the copy */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +0000166}
167
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000168PyDoc_STRVAR(doc_parse_and_bind,
169"parse_and_bind(string) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000170Execute the init line provided in the string argument.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000171
172
173/* Exported function to parse a readline init file */
174
175static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000176read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000177{
Victor Stinner19e65a32010-06-11 22:27:14 +0000178 PyObject *filename_obj = Py_None, *filename_bytes;
179 if (!PyArg_ParseTuple(args, "|O:read_init_file", &filename_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180 return NULL;
Victor Stinner19e65a32010-06-11 22:27:14 +0000181 if (filename_obj != Py_None) {
182 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
183 return NULL;
184 errno = rl_read_init_file(PyBytes_AsString(filename_bytes));
185 Py_DECREF(filename_bytes);
186 } else
187 errno = rl_read_init_file(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 if (errno)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300189 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +0000191}
192
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000193PyDoc_STRVAR(doc_read_init_file,
194"read_init_file([filename]) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000195Execute a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000196The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000197
198
Skip Montanaro28067822000-07-06 18:55:12 +0000199/* Exported function to load a readline history file */
200
201static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000202read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000203{
Victor Stinner19e65a32010-06-11 22:27:14 +0000204 PyObject *filename_obj = Py_None, *filename_bytes;
205 if (!PyArg_ParseTuple(args, "|O:read_history_file", &filename_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 return NULL;
Victor Stinner19e65a32010-06-11 22:27:14 +0000207 if (filename_obj != Py_None) {
208 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
209 return NULL;
210 errno = read_history(PyBytes_AsString(filename_bytes));
211 Py_DECREF(filename_bytes);
212 } else
213 errno = read_history(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 if (errno)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300215 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000217}
218
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000219static int _history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000220PyDoc_STRVAR(doc_read_history_file,
221"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000222Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000223The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000224
225
226/* Exported function to save a readline history file */
227
228static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000229write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000230{
Victor Stinner19e65a32010-06-11 22:27:14 +0000231 PyObject *filename_obj = Py_None, *filename_bytes;
232 char *filename;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100233 int err;
Victor Stinner19e65a32010-06-11 22:27:14 +0000234 if (!PyArg_ParseTuple(args, "|O:write_history_file", &filename_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 return NULL;
Victor Stinner19e65a32010-06-11 22:27:14 +0000236 if (filename_obj != Py_None) {
237 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
238 return NULL;
239 filename = PyBytes_AsString(filename_bytes);
240 } else {
241 filename_bytes = NULL;
242 filename = NULL;
243 }
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100244 errno = err = write_history(filename);
245 if (!err && _history_length >= 0)
Victor Stinner19e65a32010-06-11 22:27:14 +0000246 history_truncate_file(filename, _history_length);
247 Py_XDECREF(filename_bytes);
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100248 errno = err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 if (errno)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300250 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000252}
253
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000254PyDoc_STRVAR(doc_write_history_file,
255"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000256Save a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000257The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000258
259
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600260#ifdef HAVE_RL_APPEND_HISTORY
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600261/* Exported function to save part of a readline history file */
262
263static PyObject *
264append_history_file(PyObject *self, PyObject *args)
265{
266 int nelements;
267 PyObject *filename_obj = Py_None, *filename_bytes;
268 char *filename;
269 int err;
270 if (!PyArg_ParseTuple(args, "i|O:append_history_file", &nelements, &filename_obj))
271 return NULL;
272 if (filename_obj != Py_None) {
273 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
274 return NULL;
275 filename = PyBytes_AsString(filename_bytes);
276 } else {
277 filename_bytes = NULL;
278 filename = NULL;
279 }
280 errno = err = append_history(nelements, filename);
281 if (!err && _history_length >= 0)
282 history_truncate_file(filename, _history_length);
283 Py_XDECREF(filename_bytes);
284 errno = err;
285 if (errno)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300286 return PyErr_SetFromErrno(PyExc_OSError);
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600287 Py_RETURN_NONE;
288}
289
290PyDoc_STRVAR(doc_append_history_file,
291"append_history_file(nelements[, filename]) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000292Append the last nelements items of the history list to file.\n\
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600293The default filename is ~/.history.");
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600294#endif
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600295
296
Guido van Rossum74f31432003-01-07 20:01:29 +0000297/* Set history length */
298
299static PyObject*
300set_history_length(PyObject *self, PyObject *args)
301{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 int length = _history_length;
303 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
304 return NULL;
305 _history_length = length;
306 Py_RETURN_NONE;
Guido van Rossum74f31432003-01-07 20:01:29 +0000307}
308
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000309PyDoc_STRVAR(set_history_length_doc,
310"set_history_length(length) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000311set the maximal number of lines which will be written to\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000312the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000313history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000314
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000315
Guido van Rossum74f31432003-01-07 20:01:29 +0000316/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000317
318static PyObject*
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000319get_history_length(PyObject *self, PyObject *noarg)
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000320{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 return PyLong_FromLong(_history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000322}
323
Guido van Rossum74f31432003-01-07 20:01:29 +0000324PyDoc_STRVAR(get_history_length_doc,
325"get_history_length() -> int\n\
Martin Panter0f767392016-04-05 07:37:22 +0000326return the maximum number of lines that will be written to\n\
Guido van Rossum74f31432003-01-07 20:01:29 +0000327the history file.");
328
329
Martin v. Löwis0daad592001-09-30 21:09:59 +0000330/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000331
Martin v. Löwis0daad592001-09-30 21:09:59 +0000332static PyObject *
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000333set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000334{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 PyObject *function = Py_None;
336 char buf[80];
337 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
338 if (!PyArg_ParseTuple(args, buf, &function))
339 return NULL;
340 if (function == Py_None) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200341 Py_CLEAR(*hook_var);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 }
343 else if (PyCallable_Check(function)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 Py_INCREF(function);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300345 Py_XSETREF(*hook_var, function);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 }
347 else {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100348 PyErr_Format(PyExc_TypeError,
349 "set_%.50s(func): argument not callable",
350 funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 return NULL;
352 }
353 Py_RETURN_NONE;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000354}
355
Guido van Rossum74f31432003-01-07 20:01:29 +0000356
Martin v. Löwis0daad592001-09-30 21:09:59 +0000357static PyObject *
Thomas Wouters89d996e2007-09-08 17:39:28 +0000358set_completion_display_matches_hook(PyObject *self, PyObject *args)
359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 PyObject *result = set_hook("completion_display_matches_hook",
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200361 &readlinestate_global->completion_display_matches_hook, args);
Christian Heimes32fbe592007-11-12 15:01:33 +0000362#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 /* We cannot set this hook globally, since it replaces the
364 default completion display. */
365 rl_completion_display_matches_hook =
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200366 readlinestate_global->completion_display_matches_hook ?
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000367#if defined(_RL_FUNCTION_TYPEDEF)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000369#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 (VFunction *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000371#endif
Christian Heimes32fbe592007-11-12 15:01:33 +0000372#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 return result;
Christian Heimes32fbe592007-11-12 15:01:33 +0000374
Thomas Wouters89d996e2007-09-08 17:39:28 +0000375}
376
377PyDoc_STRVAR(doc_set_completion_display_matches_hook,
378"set_completion_display_matches_hook([function]) -> None\n\
379Set or remove the completion display function.\n\
380The function is called as\n\
381 function(substitution, [matches], longest_match_length)\n\
382once each time matches need to be displayed.");
383
384static PyObject *
Martin v. Löwis0daad592001-09-30 21:09:59 +0000385set_startup_hook(PyObject *self, PyObject *args)
386{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200387 return set_hook("startup_hook", &readlinestate_global->startup_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000388}
389
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000390PyDoc_STRVAR(doc_set_startup_hook,
391"set_startup_hook([function]) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000392Set or remove the function invoked by the rl_startup_hook callback.\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000393The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000394before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000395
Guido van Rossum74f31432003-01-07 20:01:29 +0000396
Martin v. Löwis0daad592001-09-30 21:09:59 +0000397#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000398
399/* Set pre-input hook */
400
Martin v. Löwis0daad592001-09-30 21:09:59 +0000401static PyObject *
402set_pre_input_hook(PyObject *self, PyObject *args)
403{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200404 return set_hook("pre_input_hook", &readlinestate_global->pre_input_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000405}
406
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000407PyDoc_STRVAR(doc_set_pre_input_hook,
408"set_pre_input_hook([function]) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000409Set or remove the function invoked by the rl_pre_input_hook callback.\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000410The function is called with no arguments after the first prompt\n\
411has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000412characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000413
Martin v. Löwis0daad592001-09-30 21:09:59 +0000414#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000415
Guido van Rossum74f31432003-01-07 20:01:29 +0000416
Thomas Wouters89d996e2007-09-08 17:39:28 +0000417/* Get the completion type for the scope of the tab-completion */
418static PyObject *
419get_completion_type(PyObject *self, PyObject *noarg)
420{
Christian Heimes217cfd12007-12-02 14:31:20 +0000421 return PyLong_FromLong(rl_completion_type);
Thomas Wouters89d996e2007-09-08 17:39:28 +0000422}
423
424PyDoc_STRVAR(doc_get_completion_type,
425"get_completion_type() -> int\n\
426Get the type of completion being attempted.");
427
428
Guido van Rossum74f31432003-01-07 20:01:29 +0000429/* Get the beginning index for the scope of the tab-completion */
430
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000431static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000432get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000433{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200434 Py_INCREF(readlinestate_global->begidx);
435 return readlinestate_global->begidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000436}
437
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000438PyDoc_STRVAR(doc_get_begidx,
439"get_begidx() -> int\n\
Martin Panter0f767392016-04-05 07:37:22 +0000440get the beginning index of the completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000441
Guido van Rossum74f31432003-01-07 20:01:29 +0000442
443/* Get the ending index for the scope of the tab-completion */
444
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000445static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000446get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000447{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200448 Py_INCREF(readlinestate_global->endidx);
449 return readlinestate_global->endidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000450}
451
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000452PyDoc_STRVAR(doc_get_endidx,
453"get_endidx() -> int\n\
Martin Panter0f767392016-04-05 07:37:22 +0000454get the ending index of the completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000455
456
Guido van Rossum74f31432003-01-07 20:01:29 +0000457/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000458
459static PyObject *
Martin Panterf00c49d2016-06-14 01:16:16 +0000460set_completer_delims(PyObject *self, PyObject *string)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000461{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 char *break_chars;
Martin Panterf00c49d2016-06-14 01:16:16 +0000463 PyObject *encoded = encode(string);
464 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 return NULL;
466 }
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200467 /* Keep a reference to the allocated memory in the module state in case
468 some other module modifies rl_completer_word_break_characters
469 (see issue #17289). */
Martin Panterf00c49d2016-06-14 01:16:16 +0000470 break_chars = strdup(PyBytes_AS_STRING(encoded));
471 Py_DECREF(encoded);
Serhiy Storchaka11384392015-09-27 22:34:59 +0300472 if (break_chars) {
473 free(completer_word_break_characters);
474 completer_word_break_characters = break_chars;
475 rl_completer_word_break_characters = break_chars;
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200476 Py_RETURN_NONE;
477 }
478 else
479 return PyErr_NoMemory();
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000480}
481
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000482PyDoc_STRVAR(doc_set_completer_delims,
483"set_completer_delims(string) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000484set the word delimiters for completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000485
Mark Dickinson29b238e2010-08-03 16:08:16 +0000486/* _py_free_history_entry: Utility function to free a history entry. */
487
488#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
489
490/* Readline version >= 5.0 introduced a timestamp field into the history entry
491 structure; this needs to be freed to avoid a memory leak. This version of
492 readline also introduced the handy 'free_history_entry' function, which
493 takes care of the timestamp. */
494
495static void
496_py_free_history_entry(HIST_ENTRY *entry)
497{
498 histdata_t data = free_history_entry(entry);
499 free(data);
500}
501
502#else
503
504/* No free_history_entry function; free everything manually. */
505
506static void
507_py_free_history_entry(HIST_ENTRY *entry)
508{
509 if (entry->line)
510 free((void *)entry->line);
511 if (entry->data)
512 free(entry->data);
513 free(entry);
514}
515
516#endif
517
Skip Montanaroe5069012004-08-15 14:32:06 +0000518static PyObject *
519py_remove_history(PyObject *self, PyObject *args)
520{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 int entry_number;
522 HIST_ENTRY *entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000523
Martin Panter0f767392016-04-05 07:37:22 +0000524 if (!PyArg_ParseTuple(args, "i:remove_history_item", &entry_number))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 return NULL;
526 if (entry_number < 0) {
527 PyErr_SetString(PyExc_ValueError,
528 "History index cannot be negative");
529 return NULL;
530 }
531 entry = remove_history(entry_number);
532 if (!entry) {
533 PyErr_Format(PyExc_ValueError,
534 "No history item at position %d",
535 entry_number);
536 return NULL;
537 }
538 /* free memory allocated for the history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000539 _py_free_history_entry(entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000541}
542
543PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000544"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000545remove history item given by its position");
546
547static PyObject *
548py_replace_history(PyObject *self, PyObject *args)
549{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 int entry_number;
Martin Panterf00c49d2016-06-14 01:16:16 +0000551 PyObject *line;
552 PyObject *encoded;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 HIST_ENTRY *old_entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000554
Martin Panterf00c49d2016-06-14 01:16:16 +0000555 if (!PyArg_ParseTuple(args, "iU:replace_history_item", &entry_number,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 &line)) {
557 return NULL;
558 }
559 if (entry_number < 0) {
560 PyErr_SetString(PyExc_ValueError,
561 "History index cannot be negative");
562 return NULL;
563 }
Martin Panterf00c49d2016-06-14 01:16:16 +0000564 encoded = encode(line);
565 if (encoded == NULL) {
566 return NULL;
567 }
568 old_entry = replace_history_entry(entry_number, PyBytes_AS_STRING(encoded), (void *)NULL);
569 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 if (!old_entry) {
571 PyErr_Format(PyExc_ValueError,
572 "No history item at position %d",
573 entry_number);
574 return NULL;
575 }
576 /* free memory allocated for the old history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000577 _py_free_history_entry(old_entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000579}
580
581PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000582"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000583replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000584
585/* Add a line to the history buffer */
586
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000587static PyObject *
Martin Panterf00c49d2016-06-14 01:16:16 +0000588py_add_history(PyObject *self, PyObject *string)
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000589{
Martin Panterf00c49d2016-06-14 01:16:16 +0000590 PyObject *encoded = encode(string);
591 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 return NULL;
593 }
Martin Panterf00c49d2016-06-14 01:16:16 +0000594 add_history(PyBytes_AS_STRING(encoded));
595 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 Py_RETURN_NONE;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000597}
598
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000599PyDoc_STRVAR(doc_add_history,
600"add_history(string) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000601add an item to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000602
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000603static int should_auto_add_history = 1;
604
605/* Enable or disable automatic history */
606
607static PyObject *
608py_set_auto_history(PyObject *self, PyObject *args)
609{
610 if (!PyArg_ParseTuple(args, "p:set_auto_history",
611 &should_auto_add_history)) {
612 return NULL;
613 }
614 Py_RETURN_NONE;
615}
616
617PyDoc_STRVAR(doc_set_auto_history,
618"set_auto_history(enabled) -> None\n\
619Enables or disables automatic history.");
620
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000621
Guido van Rossum74f31432003-01-07 20:01:29 +0000622/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000623
624static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000625get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000626{
Martin Panterf00c49d2016-06-14 01:16:16 +0000627 return decode(rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000628}
Guido van Rossum74f31432003-01-07 20:01:29 +0000629
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000630PyDoc_STRVAR(doc_get_completer_delims,
631"get_completer_delims() -> string\n\
Martin Panter0f767392016-04-05 07:37:22 +0000632get the word delimiters for completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000633
Guido van Rossum74f31432003-01-07 20:01:29 +0000634
635/* Set the completer function */
636
Guido van Rossum290900a1997-09-26 21:51:21 +0000637static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000638set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000639{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200640 return set_hook("completer", &readlinestate_global->completer, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000641}
642
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000643PyDoc_STRVAR(doc_set_completer,
644"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000645Set or remove the completer function.\n\
646The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000647for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000648It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000649
Guido van Rossum74f31432003-01-07 20:01:29 +0000650
Michael W. Hudson796df152003-01-30 10:12:51 +0000651static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000652get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000653{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200654 if (readlinestate_global->completer == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 Py_RETURN_NONE;
656 }
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200657 Py_INCREF(readlinestate_global->completer);
658 return readlinestate_global->completer;
Michael W. Hudson796df152003-01-30 10:12:51 +0000659}
660
661PyDoc_STRVAR(doc_get_completer,
662"get_completer() -> function\n\
663\n\
664Returns current completer function.");
665
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000666/* Private function to get current length of history. XXX It may be
667 * possible to replace this with a direct use of history_length instead,
668 * but it's not clear whether BSD's libedit keeps history_length up to date.
669 * See issue #8065.*/
670
671static int
672_py_get_history_length(void)
673{
674 HISTORY_STATE *hist_st = history_get_history_state();
675 int length = hist_st->length;
676 /* the history docs don't say so, but the address of hist_st changes each
677 time history_get_history_state is called which makes me think it's
678 freshly malloc'd memory... on the other hand, the address of the last
679 line stays the same as long as history isn't extended, so it appears to
680 be malloc'd but managed by the history package... */
681 free(hist_st);
682 return length;
683}
684
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000685/* Exported function to get any element of history */
686
687static PyObject *
688get_history_item(PyObject *self, PyObject *args)
689{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 int idx = 0;
691 HIST_ENTRY *hist_ent;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000692
Martin Panter0f767392016-04-05 07:37:22 +0000693 if (!PyArg_ParseTuple(args, "i:get_history_item", &idx))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 if (using_libedit_emulation) {
Ned Deilyf70f4a62013-09-06 15:16:19 -0700696 /* Older versions of libedit's readline emulation
697 * use 0-based indexes, while readline and newer
698 * versions of libedit use 1-based indexes.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 */
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000700 int length = _py_get_history_length();
Ned Deilyf70f4a62013-09-06 15:16:19 -0700701
702 idx = idx - 1 + libedit_history_start;
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 /*
705 * Apple's readline emulation crashes when
706 * the index is out of range, therefore
707 * test for that and fail gracefully.
708 */
Ned Deilyf70f4a62013-09-06 15:16:19 -0700709 if (idx < (0 + libedit_history_start)
710 || idx >= (length + libedit_history_start)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 Py_RETURN_NONE;
712 }
713 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 if ((hist_ent = history_get(idx)))
Martin Panterf00c49d2016-06-14 01:16:16 +0000715 return decode(hist_ent->line);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 else {
717 Py_RETURN_NONE;
718 }
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000719}
720
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000721PyDoc_STRVAR(doc_get_history_item,
722"get_history_item() -> string\n\
723return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000724
Guido van Rossum74f31432003-01-07 20:01:29 +0000725
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000726/* Exported function to get current length of history */
727
728static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000729get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000730{
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000731 return PyLong_FromLong((long)_py_get_history_length());
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000732}
733
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000734PyDoc_STRVAR(doc_get_current_history_length,
735"get_current_history_length() -> integer\n\
736return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000737
Guido van Rossum74f31432003-01-07 20:01:29 +0000738
Guido van Rossum79378ff1997-10-07 14:53:21 +0000739/* Exported function to read the current line buffer */
740
741static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000742get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000743{
Martin Panterf00c49d2016-06-14 01:16:16 +0000744 return decode(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000745}
746
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000747PyDoc_STRVAR(doc_get_line_buffer,
748"get_line_buffer() -> string\n\
749return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000750
Guido van Rossum74f31432003-01-07 20:01:29 +0000751
Martin v. Löwise7a97962003-09-20 16:08:33 +0000752#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
753
754/* Exported function to clear the current history */
755
756static PyObject *
757py_clear_history(PyObject *self, PyObject *noarg)
758{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 clear_history();
760 Py_RETURN_NONE;
Martin v. Löwise7a97962003-09-20 16:08:33 +0000761}
762
763PyDoc_STRVAR(doc_clear_history,
764"clear_history() -> None\n\
765Clear the current readline history.");
766#endif
767
768
Guido van Rossum79378ff1997-10-07 14:53:21 +0000769/* Exported function to insert text into the line buffer */
770
771static PyObject *
Martin Panterf00c49d2016-06-14 01:16:16 +0000772insert_text(PyObject *self, PyObject *string)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000773{
Martin Panterf00c49d2016-06-14 01:16:16 +0000774 PyObject *encoded = encode(string);
775 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 return NULL;
Martin Panterf00c49d2016-06-14 01:16:16 +0000777 }
778 rl_insert_text(PyBytes_AS_STRING(encoded));
779 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 Py_RETURN_NONE;
Guido van Rossum79378ff1997-10-07 14:53:21 +0000781}
782
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000783PyDoc_STRVAR(doc_insert_text,
784"insert_text(string) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000785Insert text into the line buffer at the cursor position.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000786
Guido van Rossum74f31432003-01-07 20:01:29 +0000787
788/* Redisplay the line buffer */
789
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000790static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000791redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000792{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 rl_redisplay();
794 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000795}
796
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000797PyDoc_STRVAR(doc_redisplay,
798"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000799Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000800contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000801
Guido van Rossum74f31432003-01-07 20:01:29 +0000802
Guido van Rossum290900a1997-09-26 21:51:21 +0000803/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000804
805static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000806{
Martin Panterf00c49d2016-06-14 01:16:16 +0000807 {"parse_and_bind", parse_and_bind, METH_O, doc_parse_and_bind},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
Martin Panterf00c49d2016-06-14 01:16:16 +0000809 {"insert_text", insert_text, METH_O, doc_insert_text},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
811 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
812 {"read_history_file", read_history_file,
813 METH_VARARGS, doc_read_history_file},
814 {"write_history_file", write_history_file,
815 METH_VARARGS, doc_write_history_file},
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600816#ifdef HAVE_RL_APPEND_HISTORY
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600817 {"append_history_file", append_history_file,
818 METH_VARARGS, doc_append_history_file},
Ned Deily8007cbc2014-11-26 13:02:33 -0800819#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 {"get_history_item", get_history_item,
821 METH_VARARGS, doc_get_history_item},
822 {"get_current_history_length", (PyCFunction)get_current_history_length,
823 METH_NOARGS, doc_get_current_history_length},
824 {"set_history_length", set_history_length,
825 METH_VARARGS, set_history_length_doc},
826 {"get_history_length", get_history_length,
827 METH_NOARGS, get_history_length_doc},
828 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
829 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
830 {"get_completion_type", get_completion_type,
831 METH_NOARGS, doc_get_completion_type},
832 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
833 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 {"set_completer_delims", set_completer_delims,
Martin Panterf00c49d2016-06-14 01:16:16 +0000836 METH_O, doc_set_completer_delims},
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000837 {"set_auto_history", py_set_auto_history, METH_VARARGS, doc_set_auto_history},
Martin Panterf00c49d2016-06-14 01:16:16 +0000838 {"add_history", py_add_history, METH_O, doc_add_history},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
840 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
841 {"get_completer_delims", get_completer_delims,
842 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 {"set_completion_display_matches_hook", set_completion_display_matches_hook,
845 METH_VARARGS, doc_set_completion_display_matches_hook},
846 {"set_startup_hook", set_startup_hook,
847 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000848#ifdef HAVE_RL_PRE_INPUT_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 {"set_pre_input_hook", set_pre_input_hook,
850 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000851#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000852#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
Martin v. Löwise7a97962003-09-20 16:08:33 +0000854#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000856};
857
Guido van Rossum05ac4492003-01-07 20:04:12 +0000858
Martin v. Löwis0daad592001-09-30 21:09:59 +0000859/* C function to call the Python hooks. */
860
861static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000862on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000863{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 int result = 0;
865 if (func != NULL) {
866 PyObject *r;
Victor Stinner2ff58a22019-06-17 14:27:23 +0200867 r = PyObject_CallNoArgs(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 if (r == NULL)
869 goto error;
870 if (r == Py_None)
871 result = 0;
872 else {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300873 result = _PyLong_AsInt(r);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 if (result == -1 && PyErr_Occurred())
875 goto error;
876 }
877 Py_DECREF(r);
878 goto done;
879 error:
880 PyErr_Clear();
881 Py_XDECREF(r);
882 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 return result;
884 }
885 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000886}
887
888static int
Ned Deily7b9ddea2014-02-05 16:53:10 -0800889#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000890on_startup_hook(void)
Ned Deily7b9ddea2014-02-05 16:53:10 -0800891#else
892on_startup_hook()
893#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000894{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200895 int r;
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200896 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200897 r = on_hook(readlinestate_global->startup_hook);
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200898 PyGILState_Release(gilstate);
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200899 return r;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000900}
901
902#ifdef HAVE_RL_PRE_INPUT_HOOK
903static int
Ned Deily7b9ddea2014-02-05 16:53:10 -0800904#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000905on_pre_input_hook(void)
Ned Deily7b9ddea2014-02-05 16:53:10 -0800906#else
907on_pre_input_hook()
908#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000909{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200910 int r;
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200911 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200912 r = on_hook(readlinestate_global->pre_input_hook);
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200913 PyGILState_Release(gilstate);
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200914 return r;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000915}
916#endif
917
Guido van Rossum05ac4492003-01-07 20:04:12 +0000918
Thomas Wouters89d996e2007-09-08 17:39:28 +0000919/* C function to call the Python completion_display_matches */
920
Georg Brandl646fdd62010-10-18 07:27:55 +0000921#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Thomas Wouters89d996e2007-09-08 17:39:28 +0000922static void
923on_completion_display_matches_hook(char **matches,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 int num_matches, int max_length)
Thomas Wouters89d996e2007-09-08 17:39:28 +0000925{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 int i;
Martin Panterf00c49d2016-06-14 01:16:16 +0000927 PyObject *sub, *m=NULL, *s=NULL, *r=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 m = PyList_New(num_matches);
930 if (m == NULL)
931 goto error;
932 for (i = 0; i < num_matches; i++) {
Martin Panterf00c49d2016-06-14 01:16:16 +0000933 s = decode(matches[i+1]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 if (s == NULL)
935 goto error;
Zackery Spytz99d56b52018-12-08 07:16:55 -0700936 PyList_SET_ITEM(m, i, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 }
Martin Panterf00c49d2016-06-14 01:16:16 +0000938 sub = decode(matches[0]);
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200939 r = PyObject_CallFunction(readlinestate_global->completion_display_matches_hook,
Martin Panterf00c49d2016-06-14 01:16:16 +0000940 "NNi", sub, m, max_length);
Christian Heimes32fbe592007-11-12 15:01:33 +0000941
Martin Panterf00c49d2016-06-14 01:16:16 +0000942 m=NULL;
Christian Heimes32fbe592007-11-12 15:01:33 +0000943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 if (r == NULL ||
945 (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) {
946 goto error;
947 }
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200948 Py_CLEAR(r);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949
950 if (0) {
951 error:
952 PyErr_Clear();
953 Py_XDECREF(m);
954 Py_XDECREF(r);
955 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 PyGILState_Release(gilstate);
Thomas Wouters89d996e2007-09-08 17:39:28 +0000957}
958
Senthil Kumaran95c07002010-11-04 03:51:05 +0000959#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000960
Martin Panter5dbbf1a2016-04-03 02:54:58 +0000961#ifdef HAVE_RL_RESIZE_TERMINAL
962static volatile sig_atomic_t sigwinch_received;
Martin Panter28f35b22016-04-03 08:00:49 +0000963static PyOS_sighandler_t sigwinch_ohandler;
Martin Panter5dbbf1a2016-04-03 02:54:58 +0000964
965static void
966readline_sigwinch_handler(int signum)
967{
968 sigwinch_received = 1;
969 if (sigwinch_ohandler &&
970 sigwinch_ohandler != SIG_IGN && sigwinch_ohandler != SIG_DFL)
971 sigwinch_ohandler(signum);
972
973#ifndef HAVE_SIGACTION
974 /* If the handler was installed with signal() rather than sigaction(),
975 we need to reinstall it. */
976 PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
977#endif
978}
979#endif
980
Guido van Rossum290900a1997-09-26 21:51:21 +0000981/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000982
Guido van Rossum290900a1997-09-26 21:51:21 +0000983static char *
Guido van Rossumd8faa362007-04-27 19:54:29 +0000984on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000985{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 char *result = NULL;
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200987 if (readlinestate_global->completer != NULL) {
Martin Panterf00c49d2016-06-14 01:16:16 +0000988 PyObject *r = NULL, *t;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 rl_attempted_completion_over = 1;
Martin Panterf00c49d2016-06-14 01:16:16 +0000991 t = decode(text);
992 r = PyObject_CallFunction(readlinestate_global->completer, "Ni", t, state);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 if (r == NULL)
994 goto error;
995 if (r == Py_None) {
996 result = NULL;
997 }
998 else {
Martin Panterf00c49d2016-06-14 01:16:16 +0000999 PyObject *encoded = encode(r);
1000 if (encoded == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 goto error;
Martin Panterf00c49d2016-06-14 01:16:16 +00001002 result = strdup(PyBytes_AS_STRING(encoded));
1003 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 }
1005 Py_DECREF(r);
1006 goto done;
1007 error:
1008 PyErr_Clear();
1009 Py_XDECREF(r);
1010 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 PyGILState_Release(gilstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 return result;
1013 }
1014 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +00001015}
1016
Guido van Rossum290900a1997-09-26 21:51:21 +00001017
Guido van Rossum6d0d3652003-01-07 20:34:19 +00001018/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001019 * before calling the normal completer */
1020
Neal Norwitzc355f0c2003-02-21 00:30:18 +00001021static char **
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001022flex_complete(const char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001023{
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001024 char **result;
Martin Panterf00c49d2016-06-14 01:16:16 +00001025 char saved;
1026 size_t start_size, end_size;
1027 wchar_t *s;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001028 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitroudc0900b2009-10-19 18:22:37 +00001029#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 rl_completion_append_character ='\0';
Antoine Pitroud5131772009-10-26 19:22:14 +00001031#endif
1032#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 rl_completion_suppress_append = 0;
Antoine Pitroudc0900b2009-10-19 18:22:37 +00001034#endif
Martin Panterf00c49d2016-06-14 01:16:16 +00001035
1036 saved = rl_line_buffer[start];
1037 rl_line_buffer[start] = 0;
1038 s = Py_DecodeLocale(rl_line_buffer, &start_size);
1039 rl_line_buffer[start] = saved;
1040 if (s == NULL) {
1041 goto done;
1042 }
1043 PyMem_RawFree(s);
1044 saved = rl_line_buffer[end];
1045 rl_line_buffer[end] = 0;
1046 s = Py_DecodeLocale(rl_line_buffer + start, &end_size);
1047 rl_line_buffer[end] = saved;
1048 if (s == NULL) {
1049 goto done;
1050 }
1051 PyMem_RawFree(s);
1052 start = (int)start_size;
1053 end = start + (int)end_size;
1054
1055done:
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001056 Py_XDECREF(readlinestate_global->begidx);
1057 Py_XDECREF(readlinestate_global->endidx);
1058 readlinestate_global->begidx = PyLong_FromLong((long) start);
1059 readlinestate_global->endidx = PyLong_FromLong((long) end);
Benjamin Peterson1bb0c0b2016-09-05 18:26:19 -07001060 result = completion_matches((char *)text, *on_completion);
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001061 PyGILState_Release(gilstate);
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001062 return result;
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001063}
1064
Guido van Rossum05ac4492003-01-07 20:04:12 +00001065
Victor Stinner1d8da612019-10-30 16:39:27 +01001066/* Helper to initialize GNU readline properly.
1067 Return -1 on memory allocation failure, return 0 on success. */
1068static int
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001069setup_readline(readlinestate *mod_state)
Guido van Rossum290900a1997-09-26 21:51:21 +00001070{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +00001071#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Victor Stinner1d8da612019-10-30 16:39:27 +01001073 if (!saved_locale) {
1074 return -1;
1075 }
Guido van Rossum60c8a3a2002-10-09 21:27:33 +00001076#endif
1077
Zvezdan Petkovicc2f082e2018-05-17 02:45:10 -04001078 /* The name must be defined before initialization */
1079 rl_readline_name = "python";
1080
Victor Stinner6ced7c42011-03-21 18:15:42 +01001081 /* the libedit readline emulation resets key bindings etc
R. David Murray52d1b4e2010-12-18 03:48:32 +00001082 * when calling rl_initialize. So call it upfront
1083 */
1084 if (using_libedit_emulation)
1085 rl_initialize();
Ned Deilyf70f4a62013-09-06 15:16:19 -07001086
1087 /* Detect if libedit's readline emulation uses 0-based
1088 * indexing or 1-based indexing.
1089 */
1090 add_history("1");
1091 if (history_get(1) == NULL) {
1092 libedit_history_start = 0;
1093 } else {
1094 libedit_history_start = 1;
1095 }
1096 clear_history();
R. David Murray52d1b4e2010-12-18 03:48:32 +00001097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 using_history();
Skip Montanaroa0392742002-06-11 14:32:46 +00001099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 /* Force rebind of TAB to insert-tab */
1101 rl_bind_key('\t', rl_insert);
1102 /* Bind both ESC-TAB and ESC-ESC to the completion function */
1103 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
1104 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin Panter5dbbf1a2016-04-03 02:54:58 +00001105#ifdef HAVE_RL_RESIZE_TERMINAL
1106 /* Set up signal handler for window resize */
1107 sigwinch_ohandler = PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
1108#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 /* Set our hook functions */
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001110 rl_startup_hook = on_startup_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001111#ifdef HAVE_RL_PRE_INPUT_HOOK
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001112 rl_pre_input_hook = on_pre_input_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001113#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 /* Set our completion function */
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001115 rl_attempted_completion_function = flex_complete;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 /* Set Python word break characters */
Antoine Pitroua7f7deb2013-05-06 21:51:03 +02001117 completer_word_break_characters =
1118 rl_completer_word_break_characters =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
1120 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001121
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001122 mod_state->begidx = PyLong_FromLong(0L);
1123 mod_state->endidx = PyLong_FromLong(0L);
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001124
Martin Panterc427b8d2016-08-27 03:23:11 +00001125 if (!using_libedit_emulation)
Martin Panterc427b8d2016-08-27 03:23:11 +00001126 {
1127 if (!isatty(STDOUT_FILENO)) {
1128 /* Issue #19884: stdout is not a terminal. Disable meta modifier
1129 keys to not write the ANSI sequence "\033[1034h" into stdout. On
1130 terminals supporting 8 bit characters like TERM=xterm-256color
1131 (which is now the default Fedora since Fedora 18), the meta key is
1132 used to enable support of 8 bit characters (ANSI sequence
1133 "\033[1034h").
1134
1135 With libedit, this call makes readline() crash. */
1136 rl_variable_bind ("enable-meta-key", "off");
1137 }
1138 }
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 /* Initialize (allows .inputrc to override)
1141 *
1142 * XXX: A bug in the readline-2.2 library causes a memory leak
1143 * inside this function. Nothing we can do about it.
1144 */
R. David Murray52d1b4e2010-12-18 03:48:32 +00001145 if (using_libedit_emulation)
Victor Stinner6ced7c42011-03-21 18:15:42 +01001146 rl_read_init_file(NULL);
R. David Murray52d1b4e2010-12-18 03:48:32 +00001147 else
R. David Murray52d1b4e2010-12-18 03:48:32 +00001148 rl_initialize();
Victor Stinner6ced7c42011-03-21 18:15:42 +01001149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 RESTORE_LOCALE(saved_locale)
Victor Stinner1d8da612019-10-30 16:39:27 +01001151 return 0;
Guido van Rossum290900a1997-09-26 21:51:21 +00001152}
1153
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001154/* Wrapper around GNU readline that handles signals differently. */
1155
Antoine Pitrouf474c5a2017-07-18 17:05:03 +02001156static char *completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001157static void
1158rlhandler(char *text)
1159{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 completed_input_string = text;
1161 rl_callback_handler_remove();
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001162}
1163
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001164static char *
Serhiy Storchakac6792272013-10-19 21:03:34 +03001165readline_until_enter_or_signal(const char *prompt, int *signal)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001166{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 char * not_done_reading = "";
1168 fd_set selectset;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 *signal = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001171#ifdef HAVE_RL_CATCH_SIGNAL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 rl_catch_signals = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001173#endif
1174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 rl_callback_handler_install (prompt, rlhandler);
1176 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 completed_input_string = not_done_reading;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 while (completed_input_string == not_done_reading) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001181 int has_input = 0, err = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 while (!has_input)
1184 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Michael W. Hudson8da2b012004-10-07 13:46:33 +00001185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 /* [Bug #1552726] Only limit the pause if an input hook has been
1187 defined. */
1188 struct timeval *timeoutp = NULL;
1189 if (PyOS_InputHook)
1190 timeoutp = &timeout;
Martin Panter5dbbf1a2016-04-03 02:54:58 +00001191#ifdef HAVE_RL_RESIZE_TERMINAL
1192 /* Update readline's view of the window size after SIGWINCH */
1193 if (sigwinch_received) {
1194 sigwinch_received = 0;
1195 rl_resize_terminal();
1196 }
1197#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 FD_SET(fileno(rl_instream), &selectset);
1199 /* select resets selectset if no input was available */
1200 has_input = select(fileno(rl_instream) + 1, &selectset,
1201 NULL, NULL, timeoutp);
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001202 err = errno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 if(PyOS_InputHook) PyOS_InputHook();
1204 }
1205
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001206 if (has_input > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 rl_callback_read_char();
1208 }
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001209 else if (err == EINTR) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 int s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 PyEval_RestoreThread(_PyOS_ReadlineTState);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 s = PyErr_CheckSignals();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 PyEval_SaveThread();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 if (s < 0) {
1215 rl_free_line_state();
Martin Panterd6990d22016-03-22 07:24:05 +00001216#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0700
1217 rl_callback_sigcleanup();
1218#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 rl_cleanup_after_signal();
1220 rl_callback_handler_remove();
1221 *signal = 1;
1222 completed_input_string = NULL;
1223 }
1224 }
1225 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 return completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001228}
1229
1230
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001231static char *
Serhiy Storchakac6792272013-10-19 21:03:34 +03001232call_readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001233{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 size_t n;
Victor Stinner1600f602018-11-30 15:03:53 +01001235 char *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 int signal;
Neal Norwitz1fa040b2004-08-25 01:20:18 +00001237
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001238#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1240 if (!saved_locale)
1241 Py_FatalError("not enough memory to save locale");
xdegaye1588be62017-11-12 12:45:59 +01001242 _Py_SetLocaleFromEnv(LC_CTYPE);
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001243#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
1246 rl_instream = sys_stdin;
1247 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001248#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001250#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 }
Guido van Rossum74f31432003-01-07 20:01:29 +00001252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 p = readline_until_enter_or_signal(prompt, &signal);
Guido van Rossumb18618d2000-05-03 23:44:39 +00001254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 /* we got an interrupt signal */
1256 if (signal) {
1257 RESTORE_LOCALE(saved_locale)
1258 return NULL;
1259 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001260
Martin Panter7462b6492015-11-02 03:37:02 +00001261 /* We got an EOF, return an empty string. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 if (p == NULL) {
Victor Stinner2fe9bac2013-10-10 16:18:20 +02001263 p = PyMem_RawMalloc(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 if (p != NULL)
1265 *p = '\0';
1266 RESTORE_LOCALE(saved_locale)
1267 return p;
1268 }
1269
1270 /* we have a valid line */
1271 n = strlen(p);
Martin Panterf0dbf7a2016-05-15 01:26:25 +00001272 if (should_auto_add_history && n > 0) {
Brett Cannon2525dc82010-08-22 20:36:25 +00001273 const char *line;
Mark Dickinson6b54e1f2010-08-03 16:49:49 +00001274 int length = _py_get_history_length();
Nir Sofferfae8f4a2017-07-07 09:10:46 +03001275 if (length > 0) {
1276 HIST_ENTRY *hist_ent;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 if (using_libedit_emulation) {
Ned Deilyf70f4a62013-09-06 15:16:19 -07001278 /* handle older 0-based or newer 1-based indexing */
Nir Sofferfae8f4a2017-07-07 09:10:46 +03001279 hist_ent = history_get(length + libedit_history_start - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 } else
Nir Sofferfae8f4a2017-07-07 09:10:46 +03001281 hist_ent = history_get(length);
1282 line = hist_ent ? hist_ent->line : "";
1283 } else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 line = "";
1285 if (strcmp(p, line))
1286 add_history(p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 }
1288 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
1289 release the original. */
Victor Stinner1600f602018-11-30 15:03:53 +01001290 char *q = p;
Victor Stinner2fe9bac2013-10-10 16:18:20 +02001291 p = PyMem_RawMalloc(n+2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 if (p != NULL) {
Victor Stinner1600f602018-11-30 15:03:53 +01001293 memcpy(p, q, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 p[n] = '\n';
1295 p[n+1] = '\0';
1296 }
1297 free(q);
1298 RESTORE_LOCALE(saved_locale)
1299 return p;
Guido van Rossum0969d361997-08-05 21:27:50 +00001300}
1301
Guido van Rossum290900a1997-09-26 21:51:21 +00001302
1303/* Initialize the module */
1304
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001305PyDoc_STRVAR(doc_module,
1306"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001307
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001308PyDoc_STRVAR(doc_module_le,
1309"Importing this module enables command line editing using libedit readline.");
Martin v. Löwis1a214512008-06-11 05:26:20 +00001310
1311static struct PyModuleDef readlinemodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 PyModuleDef_HEAD_INIT,
1313 "readline",
1314 doc_module,
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001315 sizeof(readlinestate),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 readline_methods,
1317 NULL,
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001318 readline_traverse,
1319 readline_clear,
1320 readline_free
Martin v. Löwis1a214512008-06-11 05:26:20 +00001321};
1322
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001323
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001324PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001325PyInit_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 PyObject *m;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001328 readlinestate *mod_state;
Guido van Rossum290900a1997-09-26 21:51:21 +00001329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
1331 using_libedit_emulation = 1;
1332 }
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 if (using_libedit_emulation)
1335 readlinemodule.m_doc = doc_module_le;
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001336
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 m = PyModule_Create(&readlinemodule);
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 if (m == NULL)
1341 return NULL;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001342
Victor Stinner0efc0242017-11-30 17:21:07 +01001343 if (PyModule_AddIntConstant(m, "_READLINE_VERSION",
1344 RL_READLINE_VERSION) < 0) {
1345 goto error;
1346 }
1347 if (PyModule_AddIntConstant(m, "_READLINE_RUNTIME_VERSION",
1348 rl_readline_version) < 0) {
1349 goto error;
1350 }
1351 if (PyModule_AddStringConstant(m, "_READLINE_LIBRARY_VERSION",
1352 rl_library_version) < 0)
1353 {
1354 goto error;
1355 }
1356
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001357 mod_state = (readlinestate *) PyModule_GetState(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 PyOS_ReadlineFunctionPointer = call_readline;
Victor Stinner1d8da612019-10-30 16:39:27 +01001359 if (setup_readline(mod_state) < 0) {
1360 PyErr_NoMemory();
1361 goto error;
1362 }
Antoine Pitrou7e8b8672014-11-04 14:52:10 +01001363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 return m;
Victor Stinner0efc0242017-11-30 17:21:07 +01001365
1366error:
1367 Py_DECREF(m);
1368 return NULL;
Guido van Rossum0969d361997-08-05 21:27:50 +00001369}