blob: 951bc82b16dfb7adcdf32d8d82751ef705c100a0 [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;
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200899 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200900 r = on_hook(readlinestate_global->startup_hook);
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200901 PyGILState_Release(gilstate);
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200902 return r;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000903}
904
905#ifdef HAVE_RL_PRE_INPUT_HOOK
906static int
Ned Deily7b9ddea2014-02-05 16:53:10 -0800907#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000908on_pre_input_hook(void)
Ned Deily7b9ddea2014-02-05 16:53:10 -0800909#else
910on_pre_input_hook()
911#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000912{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200913 int r;
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200914 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200915 r = on_hook(readlinestate_global->pre_input_hook);
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200916 PyGILState_Release(gilstate);
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200917 return r;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000918}
919#endif
920
Guido van Rossum05ac4492003-01-07 20:04:12 +0000921
Thomas Wouters89d996e2007-09-08 17:39:28 +0000922/* C function to call the Python completion_display_matches */
923
Georg Brandl646fdd62010-10-18 07:27:55 +0000924#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Thomas Wouters89d996e2007-09-08 17:39:28 +0000925static void
926on_completion_display_matches_hook(char **matches,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 int num_matches, int max_length)
Thomas Wouters89d996e2007-09-08 17:39:28 +0000928{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 int i;
Martin Panterf00c49d2016-06-14 01:16:16 +0000930 PyObject *sub, *m=NULL, *s=NULL, *r=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 m = PyList_New(num_matches);
933 if (m == NULL)
934 goto error;
935 for (i = 0; i < num_matches; i++) {
Martin Panterf00c49d2016-06-14 01:16:16 +0000936 s = decode(matches[i+1]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 if (s == NULL)
938 goto error;
939 if (PyList_SetItem(m, i, s) == -1)
940 goto error;
941 }
Martin Panterf00c49d2016-06-14 01:16:16 +0000942 sub = decode(matches[0]);
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200943 r = PyObject_CallFunction(readlinestate_global->completion_display_matches_hook,
Martin Panterf00c49d2016-06-14 01:16:16 +0000944 "NNi", sub, m, max_length);
Christian Heimes32fbe592007-11-12 15:01:33 +0000945
Martin Panterf00c49d2016-06-14 01:16:16 +0000946 m=NULL;
Christian Heimes32fbe592007-11-12 15:01:33 +0000947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 if (r == NULL ||
949 (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) {
950 goto error;
951 }
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200952 Py_CLEAR(r);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953
954 if (0) {
955 error:
956 PyErr_Clear();
957 Py_XDECREF(m);
958 Py_XDECREF(r);
959 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 PyGILState_Release(gilstate);
Thomas Wouters89d996e2007-09-08 17:39:28 +0000961}
962
Senthil Kumaran95c07002010-11-04 03:51:05 +0000963#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000964
Martin Panter5dbbf1a2016-04-03 02:54:58 +0000965#ifdef HAVE_RL_RESIZE_TERMINAL
966static volatile sig_atomic_t sigwinch_received;
Martin Panter28f35b22016-04-03 08:00:49 +0000967static PyOS_sighandler_t sigwinch_ohandler;
Martin Panter5dbbf1a2016-04-03 02:54:58 +0000968
969static void
970readline_sigwinch_handler(int signum)
971{
972 sigwinch_received = 1;
973 if (sigwinch_ohandler &&
974 sigwinch_ohandler != SIG_IGN && sigwinch_ohandler != SIG_DFL)
975 sigwinch_ohandler(signum);
976
977#ifndef HAVE_SIGACTION
978 /* If the handler was installed with signal() rather than sigaction(),
979 we need to reinstall it. */
980 PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
981#endif
982}
983#endif
984
Guido van Rossum290900a1997-09-26 21:51:21 +0000985/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000986
Guido van Rossum290900a1997-09-26 21:51:21 +0000987static char *
Guido van Rossumd8faa362007-04-27 19:54:29 +0000988on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000989{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 char *result = NULL;
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200991 if (readlinestate_global->completer != NULL) {
Martin Panterf00c49d2016-06-14 01:16:16 +0000992 PyObject *r = NULL, *t;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 rl_attempted_completion_over = 1;
Martin Panterf00c49d2016-06-14 01:16:16 +0000995 t = decode(text);
996 r = PyObject_CallFunction(readlinestate_global->completer, "Ni", t, state);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 if (r == NULL)
998 goto error;
999 if (r == Py_None) {
1000 result = NULL;
1001 }
1002 else {
Martin Panterf00c49d2016-06-14 01:16:16 +00001003 PyObject *encoded = encode(r);
1004 if (encoded == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 goto error;
Martin Panterf00c49d2016-06-14 01:16:16 +00001006 result = strdup(PyBytes_AS_STRING(encoded));
1007 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 }
1009 Py_DECREF(r);
1010 goto done;
1011 error:
1012 PyErr_Clear();
1013 Py_XDECREF(r);
1014 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 PyGILState_Release(gilstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 return result;
1017 }
1018 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +00001019}
1020
Guido van Rossum290900a1997-09-26 21:51:21 +00001021
Guido van Rossum6d0d3652003-01-07 20:34:19 +00001022/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001023 * before calling the normal completer */
1024
Neal Norwitzc355f0c2003-02-21 00:30:18 +00001025static char **
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001026flex_complete(const char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001027{
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001028 char **result;
Martin Panterf00c49d2016-06-14 01:16:16 +00001029 char saved;
1030 size_t start_size, end_size;
1031 wchar_t *s;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001032 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitroudc0900b2009-10-19 18:22:37 +00001033#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 rl_completion_append_character ='\0';
Antoine Pitroud5131772009-10-26 19:22:14 +00001035#endif
1036#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 rl_completion_suppress_append = 0;
Antoine Pitroudc0900b2009-10-19 18:22:37 +00001038#endif
Martin Panterf00c49d2016-06-14 01:16:16 +00001039
1040 saved = rl_line_buffer[start];
1041 rl_line_buffer[start] = 0;
1042 s = Py_DecodeLocale(rl_line_buffer, &start_size);
1043 rl_line_buffer[start] = saved;
1044 if (s == NULL) {
1045 goto done;
1046 }
1047 PyMem_RawFree(s);
1048 saved = rl_line_buffer[end];
1049 rl_line_buffer[end] = 0;
1050 s = Py_DecodeLocale(rl_line_buffer + start, &end_size);
1051 rl_line_buffer[end] = saved;
1052 if (s == NULL) {
1053 goto done;
1054 }
1055 PyMem_RawFree(s);
1056 start = (int)start_size;
1057 end = start + (int)end_size;
1058
1059done:
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001060 Py_XDECREF(readlinestate_global->begidx);
1061 Py_XDECREF(readlinestate_global->endidx);
1062 readlinestate_global->begidx = PyLong_FromLong((long) start);
1063 readlinestate_global->endidx = PyLong_FromLong((long) end);
Benjamin Peterson1bb0c0b2016-09-05 18:26:19 -07001064 result = completion_matches((char *)text, *on_completion);
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001065 PyGILState_Release(gilstate);
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001066 return result;
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001067}
1068
Guido van Rossum05ac4492003-01-07 20:04:12 +00001069
Guido van Rossum290900a1997-09-26 21:51:21 +00001070/* Helper to initialize GNU readline properly. */
1071
1072static void
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001073setup_readline(readlinestate *mod_state)
Guido van Rossum290900a1997-09-26 21:51:21 +00001074{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +00001075#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1077 if (!saved_locale)
1078 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +00001079#endif
1080
R. David Murray52d1b4e2010-12-18 03:48:32 +00001081#ifdef __APPLE__
Victor Stinner6ced7c42011-03-21 18:15:42 +01001082 /* the libedit readline emulation resets key bindings etc
R. David Murray52d1b4e2010-12-18 03:48:32 +00001083 * when calling rl_initialize. So call it upfront
1084 */
1085 if (using_libedit_emulation)
1086 rl_initialize();
Ned Deilyf70f4a62013-09-06 15:16:19 -07001087
1088 /* Detect if libedit's readline emulation uses 0-based
1089 * indexing or 1-based indexing.
1090 */
1091 add_history("1");
1092 if (history_get(1) == NULL) {
1093 libedit_history_start = 0;
1094 } else {
1095 libedit_history_start = 1;
1096 }
1097 clear_history();
R. David Murray52d1b4e2010-12-18 03:48:32 +00001098#endif /* __APPLE__ */
1099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 using_history();
Skip Montanaroa0392742002-06-11 14:32:46 +00001101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 rl_readline_name = "python";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 /* Force rebind of TAB to insert-tab */
1104 rl_bind_key('\t', rl_insert);
1105 /* Bind both ESC-TAB and ESC-ESC to the completion function */
1106 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
1107 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin Panter5dbbf1a2016-04-03 02:54:58 +00001108#ifdef HAVE_RL_RESIZE_TERMINAL
1109 /* Set up signal handler for window resize */
1110 sigwinch_ohandler = PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
1111#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 /* Set our hook functions */
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001113 rl_startup_hook = on_startup_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001114#ifdef HAVE_RL_PRE_INPUT_HOOK
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001115 rl_pre_input_hook = on_pre_input_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001116#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 /* Set our completion function */
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001118 rl_attempted_completion_function = flex_complete;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 /* Set Python word break characters */
Antoine Pitroua7f7deb2013-05-06 21:51:03 +02001120 completer_word_break_characters =
1121 rl_completer_word_break_characters =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
1123 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001124
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001125 mod_state->begidx = PyLong_FromLong(0L);
1126 mod_state->endidx = PyLong_FromLong(0L);
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001127
Martin Panterc427b8d2016-08-27 03:23:11 +00001128#ifdef __APPLE__
1129 if (!using_libedit_emulation)
Victor Stinner92639cc2014-07-24 22:11:38 +02001130#endif
Martin Panterc427b8d2016-08-27 03:23:11 +00001131 {
1132 if (!isatty(STDOUT_FILENO)) {
1133 /* Issue #19884: stdout is not a terminal. Disable meta modifier
1134 keys to not write the ANSI sequence "\033[1034h" into stdout. On
1135 terminals supporting 8 bit characters like TERM=xterm-256color
1136 (which is now the default Fedora since Fedora 18), the meta key is
1137 used to enable support of 8 bit characters (ANSI sequence
1138 "\033[1034h").
1139
1140 With libedit, this call makes readline() crash. */
1141 rl_variable_bind ("enable-meta-key", "off");
1142 }
1143 }
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 /* Initialize (allows .inputrc to override)
1146 *
1147 * XXX: A bug in the readline-2.2 library causes a memory leak
1148 * inside this function. Nothing we can do about it.
1149 */
R. David Murray52d1b4e2010-12-18 03:48:32 +00001150#ifdef __APPLE__
1151 if (using_libedit_emulation)
Victor Stinner6ced7c42011-03-21 18:15:42 +01001152 rl_read_init_file(NULL);
R. David Murray52d1b4e2010-12-18 03:48:32 +00001153 else
1154#endif /* __APPLE__ */
1155 rl_initialize();
Victor Stinner6ced7c42011-03-21 18:15:42 +01001156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 RESTORE_LOCALE(saved_locale)
Guido van Rossum290900a1997-09-26 21:51:21 +00001158}
1159
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001160/* Wrapper around GNU readline that handles signals differently. */
1161
Antoine Pitrouf474c5a2017-07-18 17:05:03 +02001162static char *completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001163static void
1164rlhandler(char *text)
1165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 completed_input_string = text;
1167 rl_callback_handler_remove();
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001168}
1169
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001170static char *
Serhiy Storchakac6792272013-10-19 21:03:34 +03001171readline_until_enter_or_signal(const char *prompt, int *signal)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001172{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 char * not_done_reading = "";
1174 fd_set selectset;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 *signal = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001177#ifdef HAVE_RL_CATCH_SIGNAL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 rl_catch_signals = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001179#endif
1180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 rl_callback_handler_install (prompt, rlhandler);
1182 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 completed_input_string = not_done_reading;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 while (completed_input_string == not_done_reading) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001187 int has_input = 0, err = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 while (!has_input)
1190 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Michael W. Hudson8da2b012004-10-07 13:46:33 +00001191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 /* [Bug #1552726] Only limit the pause if an input hook has been
1193 defined. */
1194 struct timeval *timeoutp = NULL;
1195 if (PyOS_InputHook)
1196 timeoutp = &timeout;
Martin Panter5dbbf1a2016-04-03 02:54:58 +00001197#ifdef HAVE_RL_RESIZE_TERMINAL
1198 /* Update readline's view of the window size after SIGWINCH */
1199 if (sigwinch_received) {
1200 sigwinch_received = 0;
1201 rl_resize_terminal();
1202 }
1203#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 FD_SET(fileno(rl_instream), &selectset);
1205 /* select resets selectset if no input was available */
1206 has_input = select(fileno(rl_instream) + 1, &selectset,
1207 NULL, NULL, timeoutp);
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001208 err = errno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 if(PyOS_InputHook) PyOS_InputHook();
1210 }
1211
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001212 if (has_input > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 rl_callback_read_char();
1214 }
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001215 else if (err == EINTR) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 int s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 PyEval_RestoreThread(_PyOS_ReadlineTState);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 s = PyErr_CheckSignals();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 PyEval_SaveThread();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 if (s < 0) {
1221 rl_free_line_state();
Martin Panterd6990d22016-03-22 07:24:05 +00001222#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0700
1223 rl_callback_sigcleanup();
1224#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 rl_cleanup_after_signal();
1226 rl_callback_handler_remove();
1227 *signal = 1;
1228 completed_input_string = NULL;
1229 }
1230 }
1231 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 return completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001234}
1235
1236
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001237static char *
Serhiy Storchakac6792272013-10-19 21:03:34 +03001238call_readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001239{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 size_t n;
1241 char *p, *q;
1242 int signal;
Neal Norwitz1fa040b2004-08-25 01:20:18 +00001243
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001244#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1246 if (!saved_locale)
1247 Py_FatalError("not enough memory to save locale");
Nadeem Vawda63752572013-02-02 20:05:11 +01001248 setlocale(LC_CTYPE, "");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001249#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
1252 rl_instream = sys_stdin;
1253 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001254#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001256#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 }
Guido van Rossum74f31432003-01-07 20:01:29 +00001258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 p = readline_until_enter_or_signal(prompt, &signal);
Guido van Rossumb18618d2000-05-03 23:44:39 +00001260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 /* we got an interrupt signal */
1262 if (signal) {
1263 RESTORE_LOCALE(saved_locale)
1264 return NULL;
1265 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001266
Martin Panter7462b6492015-11-02 03:37:02 +00001267 /* We got an EOF, return an empty string. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 if (p == NULL) {
Victor Stinner2fe9bac2013-10-10 16:18:20 +02001269 p = PyMem_RawMalloc(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 if (p != NULL)
1271 *p = '\0';
1272 RESTORE_LOCALE(saved_locale)
1273 return p;
1274 }
1275
1276 /* we have a valid line */
1277 n = strlen(p);
Martin Panterf0dbf7a2016-05-15 01:26:25 +00001278 if (should_auto_add_history && n > 0) {
Brett Cannon2525dc82010-08-22 20:36:25 +00001279 const char *line;
Mark Dickinson6b54e1f2010-08-03 16:49:49 +00001280 int length = _py_get_history_length();
Nir Sofferfae8f4a2017-07-07 09:10:46 +03001281 if (length > 0) {
1282 HIST_ENTRY *hist_ent;
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001283#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 if (using_libedit_emulation) {
Ned Deilyf70f4a62013-09-06 15:16:19 -07001285 /* handle older 0-based or newer 1-based indexing */
Nir Sofferfae8f4a2017-07-07 09:10:46 +03001286 hist_ent = history_get(length + libedit_history_start - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 } else
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001288#endif /* __APPLE__ */
Nir Sofferfae8f4a2017-07-07 09:10:46 +03001289 hist_ent = history_get(length);
1290 line = hist_ent ? hist_ent->line : "";
1291 } else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 line = "";
1293 if (strcmp(p, line))
1294 add_history(p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 }
1296 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
1297 release the original. */
1298 q = p;
Victor Stinner2fe9bac2013-10-10 16:18:20 +02001299 p = PyMem_RawMalloc(n+2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 if (p != NULL) {
1301 strncpy(p, q, n);
1302 p[n] = '\n';
1303 p[n+1] = '\0';
1304 }
1305 free(q);
1306 RESTORE_LOCALE(saved_locale)
1307 return p;
Guido van Rossum0969d361997-08-05 21:27:50 +00001308}
1309
Guido van Rossum290900a1997-09-26 21:51:21 +00001310
1311/* Initialize the module */
1312
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001313PyDoc_STRVAR(doc_module,
1314"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001315
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001316#ifdef __APPLE__
1317PyDoc_STRVAR(doc_module_le,
1318"Importing this module enables command line editing using libedit readline.");
1319#endif /* __APPLE__ */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001320
1321static struct PyModuleDef readlinemodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 PyModuleDef_HEAD_INIT,
1323 "readline",
1324 doc_module,
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001325 sizeof(readlinestate),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 readline_methods,
1327 NULL,
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001328 readline_traverse,
1329 readline_clear,
1330 readline_free
Martin v. Löwis1a214512008-06-11 05:26:20 +00001331};
1332
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001333
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001334PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001335PyInit_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001336{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 PyObject *m;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001338 readlinestate *mod_state;
Guido van Rossum290900a1997-09-26 21:51:21 +00001339
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001340#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
1342 using_libedit_emulation = 1;
1343 }
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 if (using_libedit_emulation)
1346 readlinemodule.m_doc = doc_module_le;
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001347
1348#endif /* __APPLE__ */
1349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 m = PyModule_Create(&readlinemodule);
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 if (m == NULL)
1353 return NULL;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001354
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001355 mod_state = (readlinestate *) PyModule_GetState(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 PyOS_ReadlineFunctionPointer = call_readline;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001357 setup_readline(mod_state);
Antoine Pitrou7e8b8672014-11-04 14:52:10 +01001358
1359 PyModule_AddIntConstant(m, "_READLINE_VERSION", RL_READLINE_VERSION);
1360 PyModule_AddIntConstant(m, "_READLINE_RUNTIME_VERSION", rl_readline_version);
Victor Stinner1881bef2017-07-07 16:06:58 +02001361 PyModule_AddStringConstant(m, "_READLINE_LIBRARY_VERSION", rl_library_version);
Antoine Pitrou7e8b8672014-11-04 14:52:10 +01001362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 return m;
Guido van Rossum0969d361997-08-05 21:27:50 +00001364}