blob: f4a5e5adcb030fcc918cc5e3cfecd46153dc06ee [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#ifdef __APPLE__
49/*
50 * It is possible to link the readline module to the readline
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 * emulation library of editline/libedit.
52 *
Ronald Oussoren2efd9242009-09-20 14:53:22 +000053 * On OSX this emulation library is not 100% API compatible
54 * with the "real" readline and cannot be detected at compile-time,
55 * hence we use a runtime check to detect if we're using libedit
56 *
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#endif /* __APPLE__ */
68
Georg Brandl646fdd62010-10-18 07:27:55 +000069#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Christian Heimes32fbe592007-11-12 15:01:33 +000070static void
71on_completion_display_matches_hook(char **matches,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 int num_matches, int max_length);
Georg Brandl646fdd62010-10-18 07:27:55 +000073#endif
Guido van Rossum0969d361997-08-05 21:27:50 +000074
Antoine Pitroua7f7deb2013-05-06 21:51:03 +020075/* Memory allocated for rl_completer_word_break_characters
76 (see issue #17289 for the motivation). */
77static char *completer_word_break_characters;
78
Antoine Pitrou5c30a752013-07-31 21:52:53 +020079typedef struct {
Martin Panterf6e9f472016-03-22 02:19:29 +000080 /* Specify hook functions in Python */
Antoine Pitrou5c30a752013-07-31 21:52:53 +020081 PyObject *completion_display_matches_hook;
82 PyObject *startup_hook;
83 PyObject *pre_input_hook;
Martin Panterf6e9f472016-03-22 02:19:29 +000084
85 PyObject *completer; /* Specify a word completer in Python */
Antoine Pitrou5c30a752013-07-31 21:52:53 +020086 PyObject *begidx;
87 PyObject *endidx;
88} readlinestate;
89
90
91#define readline_state(o) ((readlinestate *)PyModule_GetState(o))
92
93static int
94readline_clear(PyObject *m)
95{
96 readlinestate *state = readline_state(m);
97 Py_CLEAR(state->completion_display_matches_hook);
98 Py_CLEAR(state->startup_hook);
99 Py_CLEAR(state->pre_input_hook);
100 Py_CLEAR(state->completer);
101 Py_CLEAR(state->begidx);
102 Py_CLEAR(state->endidx);
103 return 0;
104}
105
106static int
107readline_traverse(PyObject *m, visitproc visit, void *arg)
108{
109 readlinestate *state = readline_state(m);
110 Py_VISIT(state->completion_display_matches_hook);
111 Py_VISIT(state->startup_hook);
112 Py_VISIT(state->pre_input_hook);
113 Py_VISIT(state->completer);
114 Py_VISIT(state->begidx);
115 Py_VISIT(state->endidx);
116 return 0;
117}
118
119static void
120readline_free(void *m)
121{
122 readline_clear((PyObject *)m);
123}
124
125static PyModuleDef readlinemodule;
126
127#define readlinestate_global ((readlinestate *)PyModule_GetState(PyState_FindModule(&readlinemodule)))
128
129
Martin Panterf00c49d2016-06-14 01:16:16 +0000130/* Convert to/from multibyte C strings */
131
132static PyObject *
133encode(PyObject *b)
134{
135 return PyUnicode_EncodeLocale(b, "surrogateescape");
136}
137
138static PyObject *
139decode(const char *s)
140{
141 return PyUnicode_DecodeLocale(s, "surrogateescape");
142}
143
144
Guido van Rossum290900a1997-09-26 21:51:21 +0000145/* Exported function to send one line to readline's init file parser */
146
147static PyObject *
Martin Panterf00c49d2016-06-14 01:16:16 +0000148parse_and_bind(PyObject *self, PyObject *string)
Guido van Rossum290900a1997-09-26 21:51:21 +0000149{
Martin Panterf00c49d2016-06-14 01:16:16 +0000150 char *copy;
151 PyObject *encoded = encode(string);
152 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 return NULL;
Martin Panterf00c49d2016-06-14 01:16:16 +0000154 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 /* Make a copy -- rl_parse_and_bind() modifies its argument */
156 /* Bernard Herzog */
Martin Panterf00c49d2016-06-14 01:16:16 +0000157 copy = PyMem_Malloc(1 + PyBytes_GET_SIZE(encoded));
158 if (copy == NULL) {
159 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 return PyErr_NoMemory();
Martin Panterf00c49d2016-06-14 01:16:16 +0000161 }
162 strcpy(copy, PyBytes_AS_STRING(encoded));
163 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 rl_parse_and_bind(copy);
Victor Stinnerb6404912013-07-07 16:21:41 +0200165 PyMem_Free(copy); /* Free the copy */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +0000167}
168
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000169PyDoc_STRVAR(doc_parse_and_bind,
170"parse_and_bind(string) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000171Execute the init line provided in the string argument.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000172
173
174/* Exported function to parse a readline init file */
175
176static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000177read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000178{
Victor Stinner19e65a32010-06-11 22:27:14 +0000179 PyObject *filename_obj = Py_None, *filename_bytes;
180 if (!PyArg_ParseTuple(args, "|O:read_init_file", &filename_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 return NULL;
Victor Stinner19e65a32010-06-11 22:27:14 +0000182 if (filename_obj != Py_None) {
183 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
184 return NULL;
185 errno = rl_read_init_file(PyBytes_AsString(filename_bytes));
186 Py_DECREF(filename_bytes);
187 } else
188 errno = rl_read_init_file(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 if (errno)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300190 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +0000192}
193
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000194PyDoc_STRVAR(doc_read_init_file,
195"read_init_file([filename]) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000196Execute a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000197The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000198
199
Skip Montanaro28067822000-07-06 18:55:12 +0000200/* Exported function to load a readline history file */
201
202static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000203read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000204{
Victor Stinner19e65a32010-06-11 22:27:14 +0000205 PyObject *filename_obj = Py_None, *filename_bytes;
206 if (!PyArg_ParseTuple(args, "|O:read_history_file", &filename_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 return NULL;
Victor Stinner19e65a32010-06-11 22:27:14 +0000208 if (filename_obj != Py_None) {
209 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
210 return NULL;
211 errno = read_history(PyBytes_AsString(filename_bytes));
212 Py_DECREF(filename_bytes);
213 } else
214 errno = read_history(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 if (errno)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300216 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000218}
219
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000220static int _history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000221PyDoc_STRVAR(doc_read_history_file,
222"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000223Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000224The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000225
226
227/* Exported function to save a readline history file */
228
229static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000230write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000231{
Victor Stinner19e65a32010-06-11 22:27:14 +0000232 PyObject *filename_obj = Py_None, *filename_bytes;
233 char *filename;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100234 int err;
Victor Stinner19e65a32010-06-11 22:27:14 +0000235 if (!PyArg_ParseTuple(args, "|O:write_history_file", &filename_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 return NULL;
Victor Stinner19e65a32010-06-11 22:27:14 +0000237 if (filename_obj != Py_None) {
238 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
239 return NULL;
240 filename = PyBytes_AsString(filename_bytes);
241 } else {
242 filename_bytes = NULL;
243 filename = NULL;
244 }
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100245 errno = err = write_history(filename);
246 if (!err && _history_length >= 0)
Victor Stinner19e65a32010-06-11 22:27:14 +0000247 history_truncate_file(filename, _history_length);
248 Py_XDECREF(filename_bytes);
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100249 errno = err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 if (errno)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300251 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000253}
254
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000255PyDoc_STRVAR(doc_write_history_file,
256"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000257Save a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000258The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000259
260
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600261#ifdef HAVE_RL_APPEND_HISTORY
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600262/* Exported function to save part of a readline history file */
263
264static PyObject *
265append_history_file(PyObject *self, PyObject *args)
266{
267 int nelements;
268 PyObject *filename_obj = Py_None, *filename_bytes;
269 char *filename;
270 int err;
271 if (!PyArg_ParseTuple(args, "i|O:append_history_file", &nelements, &filename_obj))
272 return NULL;
273 if (filename_obj != Py_None) {
274 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
275 return NULL;
276 filename = PyBytes_AsString(filename_bytes);
277 } else {
278 filename_bytes = NULL;
279 filename = NULL;
280 }
281 errno = err = append_history(nelements, filename);
282 if (!err && _history_length >= 0)
283 history_truncate_file(filename, _history_length);
284 Py_XDECREF(filename_bytes);
285 errno = err;
286 if (errno)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300287 return PyErr_SetFromErrno(PyExc_OSError);
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600288 Py_RETURN_NONE;
289}
290
291PyDoc_STRVAR(doc_append_history_file,
292"append_history_file(nelements[, filename]) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000293Append the last nelements items of the history list to file.\n\
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600294The default filename is ~/.history.");
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600295#endif
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600296
297
Guido van Rossum74f31432003-01-07 20:01:29 +0000298/* Set history length */
299
300static PyObject*
301set_history_length(PyObject *self, PyObject *args)
302{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 int length = _history_length;
304 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
305 return NULL;
306 _history_length = length;
307 Py_RETURN_NONE;
Guido van Rossum74f31432003-01-07 20:01:29 +0000308}
309
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000310PyDoc_STRVAR(set_history_length_doc,
311"set_history_length(length) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000312set the maximal number of lines which will be written to\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000313the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000314history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000315
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000316
Guido van Rossum74f31432003-01-07 20:01:29 +0000317/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000318
319static PyObject*
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000320get_history_length(PyObject *self, PyObject *noarg)
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000321{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 return PyLong_FromLong(_history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000323}
324
Guido van Rossum74f31432003-01-07 20:01:29 +0000325PyDoc_STRVAR(get_history_length_doc,
326"get_history_length() -> int\n\
Martin Panter0f767392016-04-05 07:37:22 +0000327return the maximum number of lines that will be written to\n\
Guido van Rossum74f31432003-01-07 20:01:29 +0000328the history file.");
329
330
Martin v. Löwis0daad592001-09-30 21:09:59 +0000331/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000332
Martin v. Löwis0daad592001-09-30 21:09:59 +0000333static PyObject *
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000334set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 PyObject *function = Py_None;
337 char buf[80];
338 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
339 if (!PyArg_ParseTuple(args, buf, &function))
340 return NULL;
341 if (function == Py_None) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200342 Py_CLEAR(*hook_var);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 }
344 else if (PyCallable_Check(function)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 Py_INCREF(function);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300346 Py_XSETREF(*hook_var, function);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 }
348 else {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100349 PyErr_Format(PyExc_TypeError,
350 "set_%.50s(func): argument not callable",
351 funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 return NULL;
353 }
354 Py_RETURN_NONE;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000355}
356
Guido van Rossum74f31432003-01-07 20:01:29 +0000357
Martin v. Löwis0daad592001-09-30 21:09:59 +0000358static PyObject *
Thomas Wouters89d996e2007-09-08 17:39:28 +0000359set_completion_display_matches_hook(PyObject *self, PyObject *args)
360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 PyObject *result = set_hook("completion_display_matches_hook",
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200362 &readlinestate_global->completion_display_matches_hook, args);
Christian Heimes32fbe592007-11-12 15:01:33 +0000363#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 /* We cannot set this hook globally, since it replaces the
365 default completion display. */
366 rl_completion_display_matches_hook =
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200367 readlinestate_global->completion_display_matches_hook ?
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000368#if defined(_RL_FUNCTION_TYPEDEF)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000370#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 (VFunction *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000372#endif
Christian Heimes32fbe592007-11-12 15:01:33 +0000373#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 return result;
Christian Heimes32fbe592007-11-12 15:01:33 +0000375
Thomas Wouters89d996e2007-09-08 17:39:28 +0000376}
377
378PyDoc_STRVAR(doc_set_completion_display_matches_hook,
379"set_completion_display_matches_hook([function]) -> None\n\
380Set or remove the completion display function.\n\
381The function is called as\n\
382 function(substitution, [matches], longest_match_length)\n\
383once each time matches need to be displayed.");
384
385static PyObject *
Martin v. Löwis0daad592001-09-30 21:09:59 +0000386set_startup_hook(PyObject *self, PyObject *args)
387{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200388 return set_hook("startup_hook", &readlinestate_global->startup_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000389}
390
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000391PyDoc_STRVAR(doc_set_startup_hook,
392"set_startup_hook([function]) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000393Set or remove the function invoked by the rl_startup_hook callback.\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000394The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000395before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000396
Guido van Rossum74f31432003-01-07 20:01:29 +0000397
Martin v. Löwis0daad592001-09-30 21:09:59 +0000398#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000399
400/* Set pre-input hook */
401
Martin v. Löwis0daad592001-09-30 21:09:59 +0000402static PyObject *
403set_pre_input_hook(PyObject *self, PyObject *args)
404{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200405 return set_hook("pre_input_hook", &readlinestate_global->pre_input_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000406}
407
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000408PyDoc_STRVAR(doc_set_pre_input_hook,
409"set_pre_input_hook([function]) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000410Set or remove the function invoked by the rl_pre_input_hook callback.\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000411The function is called with no arguments after the first prompt\n\
412has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000413characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000414
Martin v. Löwis0daad592001-09-30 21:09:59 +0000415#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000416
Guido van Rossum74f31432003-01-07 20:01:29 +0000417
Thomas Wouters89d996e2007-09-08 17:39:28 +0000418/* Get the completion type for the scope of the tab-completion */
419static PyObject *
420get_completion_type(PyObject *self, PyObject *noarg)
421{
Christian Heimes217cfd12007-12-02 14:31:20 +0000422 return PyLong_FromLong(rl_completion_type);
Thomas Wouters89d996e2007-09-08 17:39:28 +0000423}
424
425PyDoc_STRVAR(doc_get_completion_type,
426"get_completion_type() -> int\n\
427Get the type of completion being attempted.");
428
429
Guido van Rossum74f31432003-01-07 20:01:29 +0000430/* Get the beginning index for the scope of the tab-completion */
431
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000432static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000433get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000434{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200435 Py_INCREF(readlinestate_global->begidx);
436 return readlinestate_global->begidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000437}
438
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000439PyDoc_STRVAR(doc_get_begidx,
440"get_begidx() -> int\n\
Martin Panter0f767392016-04-05 07:37:22 +0000441get the beginning index of the completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000442
Guido van Rossum74f31432003-01-07 20:01:29 +0000443
444/* Get the ending index for the scope of the tab-completion */
445
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000446static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000447get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000448{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200449 Py_INCREF(readlinestate_global->endidx);
450 return readlinestate_global->endidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000451}
452
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000453PyDoc_STRVAR(doc_get_endidx,
454"get_endidx() -> int\n\
Martin Panter0f767392016-04-05 07:37:22 +0000455get the ending index of the completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000456
457
Guido van Rossum74f31432003-01-07 20:01:29 +0000458/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000459
460static PyObject *
Martin Panterf00c49d2016-06-14 01:16:16 +0000461set_completer_delims(PyObject *self, PyObject *string)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000462{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 char *break_chars;
Martin Panterf00c49d2016-06-14 01:16:16 +0000464 PyObject *encoded = encode(string);
465 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 return NULL;
467 }
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200468 /* Keep a reference to the allocated memory in the module state in case
469 some other module modifies rl_completer_word_break_characters
470 (see issue #17289). */
Martin Panterf00c49d2016-06-14 01:16:16 +0000471 break_chars = strdup(PyBytes_AS_STRING(encoded));
472 Py_DECREF(encoded);
Serhiy Storchaka11384392015-09-27 22:34:59 +0300473 if (break_chars) {
474 free(completer_word_break_characters);
475 completer_word_break_characters = break_chars;
476 rl_completer_word_break_characters = break_chars;
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200477 Py_RETURN_NONE;
478 }
479 else
480 return PyErr_NoMemory();
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000481}
482
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000483PyDoc_STRVAR(doc_set_completer_delims,
484"set_completer_delims(string) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000485set the word delimiters for completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000486
Mark Dickinson29b238e2010-08-03 16:08:16 +0000487/* _py_free_history_entry: Utility function to free a history entry. */
488
489#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
490
491/* Readline version >= 5.0 introduced a timestamp field into the history entry
492 structure; this needs to be freed to avoid a memory leak. This version of
493 readline also introduced the handy 'free_history_entry' function, which
494 takes care of the timestamp. */
495
496static void
497_py_free_history_entry(HIST_ENTRY *entry)
498{
499 histdata_t data = free_history_entry(entry);
500 free(data);
501}
502
503#else
504
505/* No free_history_entry function; free everything manually. */
506
507static void
508_py_free_history_entry(HIST_ENTRY *entry)
509{
510 if (entry->line)
511 free((void *)entry->line);
512 if (entry->data)
513 free(entry->data);
514 free(entry);
515}
516
517#endif
518
Skip Montanaroe5069012004-08-15 14:32:06 +0000519static PyObject *
520py_remove_history(PyObject *self, PyObject *args)
521{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 int entry_number;
523 HIST_ENTRY *entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000524
Martin Panter0f767392016-04-05 07:37:22 +0000525 if (!PyArg_ParseTuple(args, "i:remove_history_item", &entry_number))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 return NULL;
527 if (entry_number < 0) {
528 PyErr_SetString(PyExc_ValueError,
529 "History index cannot be negative");
530 return NULL;
531 }
532 entry = remove_history(entry_number);
533 if (!entry) {
534 PyErr_Format(PyExc_ValueError,
535 "No history item at position %d",
536 entry_number);
537 return NULL;
538 }
539 /* free memory allocated for the history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000540 _py_free_history_entry(entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000542}
543
544PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000545"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000546remove history item given by its position");
547
548static PyObject *
549py_replace_history(PyObject *self, PyObject *args)
550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 int entry_number;
Martin Panterf00c49d2016-06-14 01:16:16 +0000552 PyObject *line;
553 PyObject *encoded;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 HIST_ENTRY *old_entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000555
Martin Panterf00c49d2016-06-14 01:16:16 +0000556 if (!PyArg_ParseTuple(args, "iU:replace_history_item", &entry_number,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 &line)) {
558 return NULL;
559 }
560 if (entry_number < 0) {
561 PyErr_SetString(PyExc_ValueError,
562 "History index cannot be negative");
563 return NULL;
564 }
Martin Panterf00c49d2016-06-14 01:16:16 +0000565 encoded = encode(line);
566 if (encoded == NULL) {
567 return NULL;
568 }
569 old_entry = replace_history_entry(entry_number, PyBytes_AS_STRING(encoded), (void *)NULL);
570 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 if (!old_entry) {
572 PyErr_Format(PyExc_ValueError,
573 "No history item at position %d",
574 entry_number);
575 return NULL;
576 }
577 /* free memory allocated for the old history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000578 _py_free_history_entry(old_entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000580}
581
582PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000583"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000584replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000585
586/* Add a line to the history buffer */
587
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000588static PyObject *
Martin Panterf00c49d2016-06-14 01:16:16 +0000589py_add_history(PyObject *self, PyObject *string)
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000590{
Martin Panterf00c49d2016-06-14 01:16:16 +0000591 PyObject *encoded = encode(string);
592 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 return NULL;
594 }
Martin Panterf00c49d2016-06-14 01:16:16 +0000595 add_history(PyBytes_AS_STRING(encoded));
596 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 Py_RETURN_NONE;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000598}
599
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000600PyDoc_STRVAR(doc_add_history,
601"add_history(string) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000602add an item to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000603
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000604static int should_auto_add_history = 1;
605
606/* Enable or disable automatic history */
607
608static PyObject *
609py_set_auto_history(PyObject *self, PyObject *args)
610{
611 if (!PyArg_ParseTuple(args, "p:set_auto_history",
612 &should_auto_add_history)) {
613 return NULL;
614 }
615 Py_RETURN_NONE;
616}
617
618PyDoc_STRVAR(doc_set_auto_history,
619"set_auto_history(enabled) -> None\n\
620Enables or disables automatic history.");
621
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000622
Guido van Rossum74f31432003-01-07 20:01:29 +0000623/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000624
625static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000626get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000627{
Martin Panterf00c49d2016-06-14 01:16:16 +0000628 return decode(rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000629}
Guido van Rossum74f31432003-01-07 20:01:29 +0000630
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000631PyDoc_STRVAR(doc_get_completer_delims,
632"get_completer_delims() -> string\n\
Martin Panter0f767392016-04-05 07:37:22 +0000633get the word delimiters for completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000634
Guido van Rossum74f31432003-01-07 20:01:29 +0000635
636/* Set the completer function */
637
Guido van Rossum290900a1997-09-26 21:51:21 +0000638static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000639set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000640{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200641 return set_hook("completer", &readlinestate_global->completer, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000642}
643
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000644PyDoc_STRVAR(doc_set_completer,
645"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000646Set or remove the completer function.\n\
647The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000648for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000649It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000650
Guido van Rossum74f31432003-01-07 20:01:29 +0000651
Michael W. Hudson796df152003-01-30 10:12:51 +0000652static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000653get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000654{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200655 if (readlinestate_global->completer == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 Py_RETURN_NONE;
657 }
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200658 Py_INCREF(readlinestate_global->completer);
659 return readlinestate_global->completer;
Michael W. Hudson796df152003-01-30 10:12:51 +0000660}
661
662PyDoc_STRVAR(doc_get_completer,
663"get_completer() -> function\n\
664\n\
665Returns current completer function.");
666
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000667/* Private function to get current length of history. XXX It may be
668 * possible to replace this with a direct use of history_length instead,
669 * but it's not clear whether BSD's libedit keeps history_length up to date.
670 * See issue #8065.*/
671
672static int
673_py_get_history_length(void)
674{
675 HISTORY_STATE *hist_st = history_get_history_state();
676 int length = hist_st->length;
677 /* the history docs don't say so, but the address of hist_st changes each
678 time history_get_history_state is called which makes me think it's
679 freshly malloc'd memory... on the other hand, the address of the last
680 line stays the same as long as history isn't extended, so it appears to
681 be malloc'd but managed by the history package... */
682 free(hist_st);
683 return length;
684}
685
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000686/* Exported function to get any element of history */
687
688static PyObject *
689get_history_item(PyObject *self, PyObject *args)
690{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 int idx = 0;
692 HIST_ENTRY *hist_ent;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000693
Martin Panter0f767392016-04-05 07:37:22 +0000694 if (!PyArg_ParseTuple(args, "i:get_history_item", &idx))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 return NULL;
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000696#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 if (using_libedit_emulation) {
Ned Deilyf70f4a62013-09-06 15:16:19 -0700698 /* Older versions of libedit's readline emulation
699 * use 0-based indexes, while readline and newer
700 * versions of libedit use 1-based indexes.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 */
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000702 int length = _py_get_history_length();
Ned Deilyf70f4a62013-09-06 15:16:19 -0700703
704 idx = idx - 1 + libedit_history_start;
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 /*
707 * Apple's readline emulation crashes when
708 * the index is out of range, therefore
709 * test for that and fail gracefully.
710 */
Ned Deilyf70f4a62013-09-06 15:16:19 -0700711 if (idx < (0 + libedit_history_start)
712 || idx >= (length + libedit_history_start)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 Py_RETURN_NONE;
714 }
715 }
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000716#endif /* __APPLE__ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 if ((hist_ent = history_get(idx)))
Martin Panterf00c49d2016-06-14 01:16:16 +0000718 return decode(hist_ent->line);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 else {
720 Py_RETURN_NONE;
721 }
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000722}
723
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000724PyDoc_STRVAR(doc_get_history_item,
725"get_history_item() -> string\n\
726return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000727
Guido van Rossum74f31432003-01-07 20:01:29 +0000728
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000729/* Exported function to get current length of history */
730
731static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000732get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000733{
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000734 return PyLong_FromLong((long)_py_get_history_length());
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000735}
736
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000737PyDoc_STRVAR(doc_get_current_history_length,
738"get_current_history_length() -> integer\n\
739return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000740
Guido van Rossum74f31432003-01-07 20:01:29 +0000741
Guido van Rossum79378ff1997-10-07 14:53:21 +0000742/* Exported function to read the current line buffer */
743
744static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000745get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000746{
Martin Panterf00c49d2016-06-14 01:16:16 +0000747 return decode(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000748}
749
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000750PyDoc_STRVAR(doc_get_line_buffer,
751"get_line_buffer() -> string\n\
752return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000753
Guido van Rossum74f31432003-01-07 20:01:29 +0000754
Martin v. Löwise7a97962003-09-20 16:08:33 +0000755#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
756
757/* Exported function to clear the current history */
758
759static PyObject *
760py_clear_history(PyObject *self, PyObject *noarg)
761{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 clear_history();
763 Py_RETURN_NONE;
Martin v. Löwise7a97962003-09-20 16:08:33 +0000764}
765
766PyDoc_STRVAR(doc_clear_history,
767"clear_history() -> None\n\
768Clear the current readline history.");
769#endif
770
771
Guido van Rossum79378ff1997-10-07 14:53:21 +0000772/* Exported function to insert text into the line buffer */
773
774static PyObject *
Martin Panterf00c49d2016-06-14 01:16:16 +0000775insert_text(PyObject *self, PyObject *string)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000776{
Martin Panterf00c49d2016-06-14 01:16:16 +0000777 PyObject *encoded = encode(string);
778 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 return NULL;
Martin Panterf00c49d2016-06-14 01:16:16 +0000780 }
781 rl_insert_text(PyBytes_AS_STRING(encoded));
782 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 Py_RETURN_NONE;
Guido van Rossum79378ff1997-10-07 14:53:21 +0000784}
785
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000786PyDoc_STRVAR(doc_insert_text,
787"insert_text(string) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000788Insert text into the line buffer at the cursor position.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000789
Guido van Rossum74f31432003-01-07 20:01:29 +0000790
791/* Redisplay the line buffer */
792
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000793static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000794redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000795{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 rl_redisplay();
797 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000798}
799
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000800PyDoc_STRVAR(doc_redisplay,
801"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000802Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000803contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000804
Guido van Rossum74f31432003-01-07 20:01:29 +0000805
Guido van Rossum290900a1997-09-26 21:51:21 +0000806/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000807
808static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000809{
Martin Panterf00c49d2016-06-14 01:16:16 +0000810 {"parse_and_bind", parse_and_bind, METH_O, doc_parse_and_bind},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
Martin Panterf00c49d2016-06-14 01:16:16 +0000812 {"insert_text", insert_text, METH_O, doc_insert_text},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
814 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
815 {"read_history_file", read_history_file,
816 METH_VARARGS, doc_read_history_file},
817 {"write_history_file", write_history_file,
818 METH_VARARGS, doc_write_history_file},
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600819#ifdef HAVE_RL_APPEND_HISTORY
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600820 {"append_history_file", append_history_file,
821 METH_VARARGS, doc_append_history_file},
Ned Deily8007cbc2014-11-26 13:02:33 -0800822#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 {"get_history_item", get_history_item,
824 METH_VARARGS, doc_get_history_item},
825 {"get_current_history_length", (PyCFunction)get_current_history_length,
826 METH_NOARGS, doc_get_current_history_length},
827 {"set_history_length", set_history_length,
828 METH_VARARGS, set_history_length_doc},
829 {"get_history_length", get_history_length,
830 METH_NOARGS, get_history_length_doc},
831 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
832 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
833 {"get_completion_type", get_completion_type,
834 METH_NOARGS, doc_get_completion_type},
835 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
836 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 {"set_completer_delims", set_completer_delims,
Martin Panterf00c49d2016-06-14 01:16:16 +0000839 METH_O, doc_set_completer_delims},
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000840 {"set_auto_history", py_set_auto_history, METH_VARARGS, doc_set_auto_history},
Martin Panterf00c49d2016-06-14 01:16:16 +0000841 {"add_history", py_add_history, METH_O, doc_add_history},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
843 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
844 {"get_completer_delims", get_completer_delims,
845 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 {"set_completion_display_matches_hook", set_completion_display_matches_hook,
848 METH_VARARGS, doc_set_completion_display_matches_hook},
849 {"set_startup_hook", set_startup_hook,
850 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000851#ifdef HAVE_RL_PRE_INPUT_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 {"set_pre_input_hook", set_pre_input_hook,
853 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000854#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000855#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
Martin v. Löwise7a97962003-09-20 16:08:33 +0000857#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000859};
860
Guido van Rossum05ac4492003-01-07 20:04:12 +0000861
Martin v. Löwis0daad592001-09-30 21:09:59 +0000862/* C function to call the Python hooks. */
863
864static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000865on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000866{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 int result = 0;
868 if (func != NULL) {
869 PyObject *r;
Victor Stinner4778eab2016-12-01 14:51:04 +0100870 r = _PyObject_CallNoArg(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 if (r == NULL)
872 goto error;
873 if (r == Py_None)
874 result = 0;
875 else {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300876 result = _PyLong_AsInt(r);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 if (result == -1 && PyErr_Occurred())
878 goto error;
879 }
880 Py_DECREF(r);
881 goto done;
882 error:
883 PyErr_Clear();
884 Py_XDECREF(r);
885 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 return result;
887 }
888 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000889}
890
891static int
Ned Deily7b9ddea2014-02-05 16:53:10 -0800892#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000893on_startup_hook(void)
Ned Deily7b9ddea2014-02-05 16:53:10 -0800894#else
895on_startup_hook()
896#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000897{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200898 int r;
899#ifdef WITH_THREAD
900 PyGILState_STATE gilstate = PyGILState_Ensure();
901#endif
902 r = on_hook(readlinestate_global->startup_hook);
903#ifdef WITH_THREAD
904 PyGILState_Release(gilstate);
905#endif
906 return r;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000907}
908
909#ifdef HAVE_RL_PRE_INPUT_HOOK
910static int
Ned Deily7b9ddea2014-02-05 16:53:10 -0800911#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000912on_pre_input_hook(void)
Ned Deily7b9ddea2014-02-05 16:53:10 -0800913#else
914on_pre_input_hook()
915#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000916{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200917 int r;
918#ifdef WITH_THREAD
919 PyGILState_STATE gilstate = PyGILState_Ensure();
920#endif
921 r = on_hook(readlinestate_global->pre_input_hook);
922#ifdef WITH_THREAD
923 PyGILState_Release(gilstate);
924#endif
925 return r;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000926}
927#endif
928
Guido van Rossum05ac4492003-01-07 20:04:12 +0000929
Thomas Wouters89d996e2007-09-08 17:39:28 +0000930/* C function to call the Python completion_display_matches */
931
Georg Brandl646fdd62010-10-18 07:27:55 +0000932#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Thomas Wouters89d996e2007-09-08 17:39:28 +0000933static void
934on_completion_display_matches_hook(char **matches,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 int num_matches, int max_length)
Thomas Wouters89d996e2007-09-08 17:39:28 +0000936{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 int i;
Martin Panterf00c49d2016-06-14 01:16:16 +0000938 PyObject *sub, *m=NULL, *s=NULL, *r=NULL;
Christian Heimesaec75c32007-11-11 22:42:36 +0000939#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 PyGILState_STATE gilstate = PyGILState_Ensure();
Thomas Wouters89d996e2007-09-08 17:39:28 +0000941#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 m = PyList_New(num_matches);
943 if (m == NULL)
944 goto error;
945 for (i = 0; i < num_matches; i++) {
Martin Panterf00c49d2016-06-14 01:16:16 +0000946 s = decode(matches[i+1]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 if (s == NULL)
948 goto error;
949 if (PyList_SetItem(m, i, s) == -1)
950 goto error;
951 }
Martin Panterf00c49d2016-06-14 01:16:16 +0000952 sub = decode(matches[0]);
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200953 r = PyObject_CallFunction(readlinestate_global->completion_display_matches_hook,
Martin Panterf00c49d2016-06-14 01:16:16 +0000954 "NNi", sub, m, max_length);
Christian Heimes32fbe592007-11-12 15:01:33 +0000955
Martin Panterf00c49d2016-06-14 01:16:16 +0000956 m=NULL;
Christian Heimes32fbe592007-11-12 15:01:33 +0000957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 if (r == NULL ||
959 (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) {
960 goto error;
961 }
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200962 Py_CLEAR(r);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963
964 if (0) {
965 error:
966 PyErr_Clear();
967 Py_XDECREF(m);
968 Py_XDECREF(r);
969 }
Christian Heimes32fbe592007-11-12 15:01:33 +0000970#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 PyGILState_Release(gilstate);
Christian Heimes32fbe592007-11-12 15:01:33 +0000972#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000973}
974
Senthil Kumaran95c07002010-11-04 03:51:05 +0000975#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000976
Martin Panter5dbbf1a2016-04-03 02:54:58 +0000977#ifdef HAVE_RL_RESIZE_TERMINAL
978static volatile sig_atomic_t sigwinch_received;
Martin Panter28f35b22016-04-03 08:00:49 +0000979static PyOS_sighandler_t sigwinch_ohandler;
Martin Panter5dbbf1a2016-04-03 02:54:58 +0000980
981static void
982readline_sigwinch_handler(int signum)
983{
984 sigwinch_received = 1;
985 if (sigwinch_ohandler &&
986 sigwinch_ohandler != SIG_IGN && sigwinch_ohandler != SIG_DFL)
987 sigwinch_ohandler(signum);
988
989#ifndef HAVE_SIGACTION
990 /* If the handler was installed with signal() rather than sigaction(),
991 we need to reinstall it. */
992 PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
993#endif
994}
995#endif
996
Guido van Rossum290900a1997-09-26 21:51:21 +0000997/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000998
Guido van Rossum290900a1997-09-26 21:51:21 +0000999static char *
Guido van Rossumd8faa362007-04-27 19:54:29 +00001000on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +00001001{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 char *result = NULL;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001003 if (readlinestate_global->completer != NULL) {
Martin Panterf00c49d2016-06-14 01:16:16 +00001004 PyObject *r = NULL, *t;
Christian Heimesaec75c32007-11-11 22:42:36 +00001005#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudsonda6242c2005-03-30 11:21:53 +00001007#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 rl_attempted_completion_over = 1;
Martin Panterf00c49d2016-06-14 01:16:16 +00001009 t = decode(text);
1010 r = PyObject_CallFunction(readlinestate_global->completer, "Ni", t, state);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 if (r == NULL)
1012 goto error;
1013 if (r == Py_None) {
1014 result = NULL;
1015 }
1016 else {
Martin Panterf00c49d2016-06-14 01:16:16 +00001017 PyObject *encoded = encode(r);
1018 if (encoded == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 goto error;
Martin Panterf00c49d2016-06-14 01:16:16 +00001020 result = strdup(PyBytes_AS_STRING(encoded));
1021 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 }
1023 Py_DECREF(r);
1024 goto done;
1025 error:
1026 PyErr_Clear();
1027 Py_XDECREF(r);
1028 done:
Christian Heimesaec75c32007-11-11 22:42:36 +00001029#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 PyGILState_Release(gilstate);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +00001031#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 return result;
1033 }
1034 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +00001035}
1036
Guido van Rossum290900a1997-09-26 21:51:21 +00001037
Guido van Rossum6d0d3652003-01-07 20:34:19 +00001038/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001039 * before calling the normal completer */
1040
Neal Norwitzc355f0c2003-02-21 00:30:18 +00001041static char **
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001042flex_complete(const char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001043{
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001044 char **result;
Martin Panterf00c49d2016-06-14 01:16:16 +00001045 char saved;
1046 size_t start_size, end_size;
1047 wchar_t *s;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001048#ifdef WITH_THREAD
1049 PyGILState_STATE gilstate = PyGILState_Ensure();
1050#endif
Antoine Pitroudc0900b2009-10-19 18:22:37 +00001051#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 rl_completion_append_character ='\0';
Antoine Pitroud5131772009-10-26 19:22:14 +00001053#endif
1054#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 rl_completion_suppress_append = 0;
Antoine Pitroudc0900b2009-10-19 18:22:37 +00001056#endif
Martin Panterf00c49d2016-06-14 01:16:16 +00001057
1058 saved = rl_line_buffer[start];
1059 rl_line_buffer[start] = 0;
1060 s = Py_DecodeLocale(rl_line_buffer, &start_size);
1061 rl_line_buffer[start] = saved;
1062 if (s == NULL) {
1063 goto done;
1064 }
1065 PyMem_RawFree(s);
1066 saved = rl_line_buffer[end];
1067 rl_line_buffer[end] = 0;
1068 s = Py_DecodeLocale(rl_line_buffer + start, &end_size);
1069 rl_line_buffer[end] = saved;
1070 if (s == NULL) {
1071 goto done;
1072 }
1073 PyMem_RawFree(s);
1074 start = (int)start_size;
1075 end = start + (int)end_size;
1076
1077done:
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001078 Py_XDECREF(readlinestate_global->begidx);
1079 Py_XDECREF(readlinestate_global->endidx);
1080 readlinestate_global->begidx = PyLong_FromLong((long) start);
1081 readlinestate_global->endidx = PyLong_FromLong((long) end);
Benjamin Peterson1bb0c0b2016-09-05 18:26:19 -07001082 result = completion_matches((char *)text, *on_completion);
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001083#ifdef WITH_THREAD
1084 PyGILState_Release(gilstate);
1085#endif
1086 return result;
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001087}
1088
Guido van Rossum05ac4492003-01-07 20:04:12 +00001089
Guido van Rossum290900a1997-09-26 21:51:21 +00001090/* Helper to initialize GNU readline properly. */
1091
1092static void
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001093setup_readline(readlinestate *mod_state)
Guido van Rossum290900a1997-09-26 21:51:21 +00001094{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +00001095#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1097 if (!saved_locale)
1098 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +00001099#endif
1100
R. David Murray52d1b4e2010-12-18 03:48:32 +00001101#ifdef __APPLE__
Victor Stinner6ced7c42011-03-21 18:15:42 +01001102 /* the libedit readline emulation resets key bindings etc
R. David Murray52d1b4e2010-12-18 03:48:32 +00001103 * when calling rl_initialize. So call it upfront
1104 */
1105 if (using_libedit_emulation)
1106 rl_initialize();
Ned Deilyf70f4a62013-09-06 15:16:19 -07001107
1108 /* Detect if libedit's readline emulation uses 0-based
1109 * indexing or 1-based indexing.
1110 */
1111 add_history("1");
1112 if (history_get(1) == NULL) {
1113 libedit_history_start = 0;
1114 } else {
1115 libedit_history_start = 1;
1116 }
1117 clear_history();
R. David Murray52d1b4e2010-12-18 03:48:32 +00001118#endif /* __APPLE__ */
1119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 using_history();
Skip Montanaroa0392742002-06-11 14:32:46 +00001121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 rl_readline_name = "python";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 /* Force rebind of TAB to insert-tab */
1124 rl_bind_key('\t', rl_insert);
1125 /* Bind both ESC-TAB and ESC-ESC to the completion function */
1126 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
1127 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin Panter5dbbf1a2016-04-03 02:54:58 +00001128#ifdef HAVE_RL_RESIZE_TERMINAL
1129 /* Set up signal handler for window resize */
1130 sigwinch_ohandler = PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
1131#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 /* Set our hook functions */
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001133 rl_startup_hook = on_startup_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001134#ifdef HAVE_RL_PRE_INPUT_HOOK
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001135 rl_pre_input_hook = on_pre_input_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001136#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 /* Set our completion function */
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001138 rl_attempted_completion_function = flex_complete;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 /* Set Python word break characters */
Antoine Pitroua7f7deb2013-05-06 21:51:03 +02001140 completer_word_break_characters =
1141 rl_completer_word_break_characters =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
1143 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001144
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001145 mod_state->begidx = PyLong_FromLong(0L);
1146 mod_state->endidx = PyLong_FromLong(0L);
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001147
Martin Panterc427b8d2016-08-27 03:23:11 +00001148#ifdef __APPLE__
1149 if (!using_libedit_emulation)
Victor Stinner92639cc2014-07-24 22:11:38 +02001150#endif
Martin Panterc427b8d2016-08-27 03:23:11 +00001151 {
1152 if (!isatty(STDOUT_FILENO)) {
1153 /* Issue #19884: stdout is not a terminal. Disable meta modifier
1154 keys to not write the ANSI sequence "\033[1034h" into stdout. On
1155 terminals supporting 8 bit characters like TERM=xterm-256color
1156 (which is now the default Fedora since Fedora 18), the meta key is
1157 used to enable support of 8 bit characters (ANSI sequence
1158 "\033[1034h").
1159
1160 With libedit, this call makes readline() crash. */
1161 rl_variable_bind ("enable-meta-key", "off");
1162 }
1163 }
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 /* Initialize (allows .inputrc to override)
1166 *
1167 * XXX: A bug in the readline-2.2 library causes a memory leak
1168 * inside this function. Nothing we can do about it.
1169 */
R. David Murray52d1b4e2010-12-18 03:48:32 +00001170#ifdef __APPLE__
1171 if (using_libedit_emulation)
Victor Stinner6ced7c42011-03-21 18:15:42 +01001172 rl_read_init_file(NULL);
R. David Murray52d1b4e2010-12-18 03:48:32 +00001173 else
1174#endif /* __APPLE__ */
1175 rl_initialize();
Victor Stinner6ced7c42011-03-21 18:15:42 +01001176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 RESTORE_LOCALE(saved_locale)
Guido van Rossum290900a1997-09-26 21:51:21 +00001178}
1179
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001180/* Wrapper around GNU readline that handles signals differently. */
1181
Antoine Pitrouf474c5a2017-07-18 17:05:03 +02001182static char *completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001183static void
1184rlhandler(char *text)
1185{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 completed_input_string = text;
1187 rl_callback_handler_remove();
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001188}
1189
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001190static char *
Serhiy Storchakac6792272013-10-19 21:03:34 +03001191readline_until_enter_or_signal(const char *prompt, int *signal)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001192{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 char * not_done_reading = "";
1194 fd_set selectset;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 *signal = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001197#ifdef HAVE_RL_CATCH_SIGNAL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 rl_catch_signals = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001199#endif
1200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 rl_callback_handler_install (prompt, rlhandler);
1202 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 completed_input_string = not_done_reading;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 while (completed_input_string == not_done_reading) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001207 int has_input = 0, err = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 while (!has_input)
1210 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Michael W. Hudson8da2b012004-10-07 13:46:33 +00001211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 /* [Bug #1552726] Only limit the pause if an input hook has been
1213 defined. */
1214 struct timeval *timeoutp = NULL;
1215 if (PyOS_InputHook)
1216 timeoutp = &timeout;
Martin Panter5dbbf1a2016-04-03 02:54:58 +00001217#ifdef HAVE_RL_RESIZE_TERMINAL
1218 /* Update readline's view of the window size after SIGWINCH */
1219 if (sigwinch_received) {
1220 sigwinch_received = 0;
1221 rl_resize_terminal();
1222 }
1223#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 FD_SET(fileno(rl_instream), &selectset);
1225 /* select resets selectset if no input was available */
1226 has_input = select(fileno(rl_instream) + 1, &selectset,
1227 NULL, NULL, timeoutp);
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001228 err = errno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 if(PyOS_InputHook) PyOS_InputHook();
1230 }
1231
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001232 if (has_input > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 rl_callback_read_char();
1234 }
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001235 else if (err == EINTR) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001237#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001239#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001241#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001243#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 if (s < 0) {
1245 rl_free_line_state();
Martin Panterd6990d22016-03-22 07:24:05 +00001246#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0700
1247 rl_callback_sigcleanup();
1248#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 rl_cleanup_after_signal();
1250 rl_callback_handler_remove();
1251 *signal = 1;
1252 completed_input_string = NULL;
1253 }
1254 }
1255 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 return completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001258}
1259
1260
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001261static char *
Serhiy Storchakac6792272013-10-19 21:03:34 +03001262call_readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 size_t n;
1265 char *p, *q;
1266 int signal;
Neal Norwitz1fa040b2004-08-25 01:20:18 +00001267
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001268#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1270 if (!saved_locale)
1271 Py_FatalError("not enough memory to save locale");
Nadeem Vawda63752572013-02-02 20:05:11 +01001272 setlocale(LC_CTYPE, "");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001273#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
1276 rl_instream = sys_stdin;
1277 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001278#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001280#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 }
Guido van Rossum74f31432003-01-07 20:01:29 +00001282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 p = readline_until_enter_or_signal(prompt, &signal);
Guido van Rossumb18618d2000-05-03 23:44:39 +00001284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 /* we got an interrupt signal */
1286 if (signal) {
1287 RESTORE_LOCALE(saved_locale)
1288 return NULL;
1289 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001290
Martin Panter7462b6492015-11-02 03:37:02 +00001291 /* We got an EOF, return an empty string. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 if (p == NULL) {
Victor Stinner2fe9bac2013-10-10 16:18:20 +02001293 p = PyMem_RawMalloc(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 if (p != NULL)
1295 *p = '\0';
1296 RESTORE_LOCALE(saved_locale)
1297 return p;
1298 }
1299
1300 /* we have a valid line */
1301 n = strlen(p);
Martin Panterf0dbf7a2016-05-15 01:26:25 +00001302 if (should_auto_add_history && n > 0) {
Brett Cannon2525dc82010-08-22 20:36:25 +00001303 const char *line;
Mark Dickinson6b54e1f2010-08-03 16:49:49 +00001304 int length = _py_get_history_length();
Nir Sofferfae8f4a2017-07-07 09:10:46 +03001305 if (length > 0) {
1306 HIST_ENTRY *hist_ent;
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001307#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 if (using_libedit_emulation) {
Ned Deilyf70f4a62013-09-06 15:16:19 -07001309 /* handle older 0-based or newer 1-based indexing */
Nir Sofferfae8f4a2017-07-07 09:10:46 +03001310 hist_ent = history_get(length + libedit_history_start - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 } else
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001312#endif /* __APPLE__ */
Nir Sofferfae8f4a2017-07-07 09:10:46 +03001313 hist_ent = history_get(length);
1314 line = hist_ent ? hist_ent->line : "";
1315 } else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 line = "";
1317 if (strcmp(p, line))
1318 add_history(p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 }
1320 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
1321 release the original. */
1322 q = p;
Victor Stinner2fe9bac2013-10-10 16:18:20 +02001323 p = PyMem_RawMalloc(n+2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 if (p != NULL) {
1325 strncpy(p, q, n);
1326 p[n] = '\n';
1327 p[n+1] = '\0';
1328 }
1329 free(q);
1330 RESTORE_LOCALE(saved_locale)
1331 return p;
Guido van Rossum0969d361997-08-05 21:27:50 +00001332}
1333
Guido van Rossum290900a1997-09-26 21:51:21 +00001334
1335/* Initialize the module */
1336
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001337PyDoc_STRVAR(doc_module,
1338"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001339
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001340#ifdef __APPLE__
1341PyDoc_STRVAR(doc_module_le,
1342"Importing this module enables command line editing using libedit readline.");
1343#endif /* __APPLE__ */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001344
1345static struct PyModuleDef readlinemodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 PyModuleDef_HEAD_INIT,
1347 "readline",
1348 doc_module,
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001349 sizeof(readlinestate),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 readline_methods,
1351 NULL,
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001352 readline_traverse,
1353 readline_clear,
1354 readline_free
Martin v. Löwis1a214512008-06-11 05:26:20 +00001355};
1356
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001357
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001358PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001359PyInit_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 PyObject *m;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001362 readlinestate *mod_state;
Guido van Rossum290900a1997-09-26 21:51:21 +00001363
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001364#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
1366 using_libedit_emulation = 1;
1367 }
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 if (using_libedit_emulation)
1370 readlinemodule.m_doc = doc_module_le;
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001371
1372#endif /* __APPLE__ */
1373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 m = PyModule_Create(&readlinemodule);
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 if (m == NULL)
1377 return NULL;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001378
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001379 mod_state = (readlinestate *) PyModule_GetState(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 PyOS_ReadlineFunctionPointer = call_readline;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001381 setup_readline(mod_state);
Antoine Pitrou7e8b8672014-11-04 14:52:10 +01001382
1383 PyModule_AddIntConstant(m, "_READLINE_VERSION", RL_READLINE_VERSION);
1384 PyModule_AddIntConstant(m, "_READLINE_RUNTIME_VERSION", rl_readline_version);
Victor Stinner1881bef2017-07-07 16:06:58 +02001385 PyModule_AddStringConstant(m, "_READLINE_LIBRARY_VERSION", rl_library_version);
Antoine Pitrou7e8b8672014-11-04 14:52:10 +01001386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 return m;
Guido van Rossum0969d361997-08-05 21:27:50 +00001388}