blob: f8876e0560e293b21bd1594b5a970926529da94d [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 <setjmp.h>
11#include <signal.h>
Guido van Rossum290900a1997-09-26 21:51:21 +000012#include <errno.h>
Michael W. Hudson8da2b012004-10-07 13:46:33 +000013#include <sys/time.h>
Guido van Rossum0969d361997-08-05 21:27:50 +000014
Skip Montanaro7befb992004-02-10 16:50:21 +000015#if defined(HAVE_SETLOCALE)
Guido van Rossum60c8a3a2002-10-09 21:27:33 +000016/* GNU readline() mistakenly sets the LC_CTYPE locale.
17 * This is evil. Only the user or the app's main() should do this!
18 * We must save and restore the locale around the rl_initialize() call.
19 */
20#define SAVE_LOCALE
21#include <locale.h>
22#endif
23
Thomas Wouters0e3f5912006-08-11 14:57:12 +000024#ifdef SAVE_LOCALE
25# define RESTORE_LOCALE(sl) { setlocale(LC_CTYPE, sl); free(sl); }
26#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000027# define RESTORE_LOCALE(sl)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000028#endif
29
Guido van Rossum290900a1997-09-26 21:51:21 +000030/* GNU readline definitions */
Guido van Rossumb0e51b22001-04-13 18:14:27 +000031#undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
Guido van Rossumbcc20741998-08-04 22:53:56 +000032#include <readline/readline.h>
33#include <readline/history.h>
Guido van Rossum730806d1998-04-10 22:27:42 +000034
Guido van Rossum353ae582001-07-10 16:45:32 +000035#ifdef HAVE_RL_COMPLETION_MATCHES
Guido van Rossum74f31432003-01-07 20:01:29 +000036#define completion_matches(x, y) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
Guido van Rossumd8faa362007-04-27 19:54:29 +000038#else
Martin v. Löwisb37509b2008-11-04 20:45:29 +000039#if defined(_RL_FUNCTION_TYPEDEF)
Guido van Rossumd8faa362007-04-27 19:54:29 +000040extern char **completion_matches(char *, rl_compentry_func_t *);
Martin v. Löwisb37509b2008-11-04 20:45:29 +000041#else
Ronald Oussoren25696bb2010-02-11 13:15:00 +000042
43#if !defined(__APPLE__)
Martin v. Löwisb37509b2008-11-04 20:45:29 +000044extern char **completion_matches(char *, CPFunction *);
45#endif
Guido van Rossum353ae582001-07-10 16:45:32 +000046#endif
Ronald Oussoren25696bb2010-02-11 13:15:00 +000047#endif
Guido van Rossum353ae582001-07-10 16:45:32 +000048
Ronald Oussoren2efd9242009-09-20 14:53:22 +000049#ifdef __APPLE__
50/*
51 * It is possible to link the readline module to the readline
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 * emulation library of editline/libedit.
53 *
Ronald Oussoren2efd9242009-09-20 14:53:22 +000054 * On OSX this emulation library is not 100% API compatible
55 * with the "real" readline and cannot be detected at compile-time,
56 * hence we use a runtime check to detect if we're using libedit
57 *
Ned Deily5d4121a2013-10-12 15:47:58 -070058 * Currently there is one known API incompatibility:
Ronald Oussoren2efd9242009-09-20 14:53:22 +000059 * - 'get_history' has a 1-based index with GNU readline, and a 0-based
Ned Deily5d4121a2013-10-12 15:47:58 -070060 * index with older versions of libedit's emulation.
Ronald Oussoren2efd9242009-09-20 14:53:22 +000061 * - Note that replace_history and remove_history use a 0-based index
Ned Deily5d4121a2013-10-12 15:47:58 -070062 * with both implementations.
Ronald Oussoren2efd9242009-09-20 14:53:22 +000063 */
64static int using_libedit_emulation = 0;
65static const char libedit_version_tag[] = "EditLine wrapper";
Ned Deilyf70f4a62013-09-06 15:16:19 -070066
67static int libedit_history_start = 0;
Ronald Oussoren2efd9242009-09-20 14:53:22 +000068#endif /* __APPLE__ */
69
Georg Brandl646fdd62010-10-18 07:27:55 +000070#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Christian Heimes32fbe592007-11-12 15:01:33 +000071static void
72on_completion_display_matches_hook(char **matches,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 int num_matches, int max_length);
Georg Brandl646fdd62010-10-18 07:27:55 +000074#endif
Guido van Rossum0969d361997-08-05 21:27:50 +000075
Antoine Pitroua7f7deb2013-05-06 21:51:03 +020076/* Memory allocated for rl_completer_word_break_characters
77 (see issue #17289 for the motivation). */
78static char *completer_word_break_characters;
79
Antoine Pitrou5c30a752013-07-31 21:52:53 +020080typedef struct {
Martin Panterf6e9f472016-03-22 02:19:29 +000081 /* Specify hook functions in Python */
Antoine Pitrou5c30a752013-07-31 21:52:53 +020082 PyObject *completion_display_matches_hook;
83 PyObject *startup_hook;
84 PyObject *pre_input_hook;
Martin Panterf6e9f472016-03-22 02:19:29 +000085
86 PyObject *completer; /* Specify a word completer in Python */
Antoine Pitrou5c30a752013-07-31 21:52:53 +020087 PyObject *begidx;
88 PyObject *endidx;
89} readlinestate;
90
91
92#define readline_state(o) ((readlinestate *)PyModule_GetState(o))
93
94static int
95readline_clear(PyObject *m)
96{
97 readlinestate *state = readline_state(m);
98 Py_CLEAR(state->completion_display_matches_hook);
99 Py_CLEAR(state->startup_hook);
100 Py_CLEAR(state->pre_input_hook);
101 Py_CLEAR(state->completer);
102 Py_CLEAR(state->begidx);
103 Py_CLEAR(state->endidx);
104 return 0;
105}
106
107static int
108readline_traverse(PyObject *m, visitproc visit, void *arg)
109{
110 readlinestate *state = readline_state(m);
111 Py_VISIT(state->completion_display_matches_hook);
112 Py_VISIT(state->startup_hook);
113 Py_VISIT(state->pre_input_hook);
114 Py_VISIT(state->completer);
115 Py_VISIT(state->begidx);
116 Py_VISIT(state->endidx);
117 return 0;
118}
119
120static void
121readline_free(void *m)
122{
123 readline_clear((PyObject *)m);
124}
125
126static PyModuleDef readlinemodule;
127
128#define readlinestate_global ((readlinestate *)PyModule_GetState(PyState_FindModule(&readlinemodule)))
129
130
Martin Panterf00c49d2016-06-14 01:16:16 +0000131/* Convert to/from multibyte C strings */
132
133static PyObject *
134encode(PyObject *b)
135{
136 return PyUnicode_EncodeLocale(b, "surrogateescape");
137}
138
139static PyObject *
140decode(const char *s)
141{
142 return PyUnicode_DecodeLocale(s, "surrogateescape");
143}
144
145
Guido van Rossum290900a1997-09-26 21:51:21 +0000146/* Exported function to send one line to readline's init file parser */
147
148static PyObject *
Martin Panterf00c49d2016-06-14 01:16:16 +0000149parse_and_bind(PyObject *self, PyObject *string)
Guido van Rossum290900a1997-09-26 21:51:21 +0000150{
Martin Panterf00c49d2016-06-14 01:16:16 +0000151 char *copy;
152 PyObject *encoded = encode(string);
153 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 return NULL;
Martin Panterf00c49d2016-06-14 01:16:16 +0000155 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 /* Make a copy -- rl_parse_and_bind() modifies its argument */
157 /* Bernard Herzog */
Martin Panterf00c49d2016-06-14 01:16:16 +0000158 copy = PyMem_Malloc(1 + PyBytes_GET_SIZE(encoded));
159 if (copy == NULL) {
160 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000161 return PyErr_NoMemory();
Martin Panterf00c49d2016-06-14 01:16:16 +0000162 }
163 strcpy(copy, PyBytes_AS_STRING(encoded));
164 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 rl_parse_and_bind(copy);
Victor Stinnerb6404912013-07-07 16:21:41 +0200166 PyMem_Free(copy); /* Free the copy */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +0000168}
169
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000170PyDoc_STRVAR(doc_parse_and_bind,
171"parse_and_bind(string) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000172Execute the init line provided in the string argument.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000173
174
175/* Exported function to parse a readline init file */
176
177static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000178read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000179{
Victor Stinner19e65a32010-06-11 22:27:14 +0000180 PyObject *filename_obj = Py_None, *filename_bytes;
181 if (!PyArg_ParseTuple(args, "|O:read_init_file", &filename_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000182 return NULL;
Victor Stinner19e65a32010-06-11 22:27:14 +0000183 if (filename_obj != Py_None) {
184 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
185 return NULL;
186 errno = rl_read_init_file(PyBytes_AsString(filename_bytes));
187 Py_DECREF(filename_bytes);
188 } else
189 errno = rl_read_init_file(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 if (errno)
191 return PyErr_SetFromErrno(PyExc_IOError);
192 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +0000193}
194
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000195PyDoc_STRVAR(doc_read_init_file,
196"read_init_file([filename]) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000197Execute a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000198The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000199
200
Skip Montanaro28067822000-07-06 18:55:12 +0000201/* Exported function to load a readline history file */
202
203static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000204read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000205{
Victor Stinner19e65a32010-06-11 22:27:14 +0000206 PyObject *filename_obj = Py_None, *filename_bytes;
207 if (!PyArg_ParseTuple(args, "|O:read_history_file", &filename_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 return NULL;
Victor Stinner19e65a32010-06-11 22:27:14 +0000209 if (filename_obj != Py_None) {
210 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
211 return NULL;
212 errno = read_history(PyBytes_AsString(filename_bytes));
213 Py_DECREF(filename_bytes);
214 } else
215 errno = read_history(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 if (errno)
217 return PyErr_SetFromErrno(PyExc_IOError);
218 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000219}
220
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000221static int _history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000222PyDoc_STRVAR(doc_read_history_file,
223"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000224Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000225The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000226
227
228/* Exported function to save a readline history file */
229
230static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000231write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000232{
Victor Stinner19e65a32010-06-11 22:27:14 +0000233 PyObject *filename_obj = Py_None, *filename_bytes;
234 char *filename;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100235 int err;
Victor Stinner19e65a32010-06-11 22:27:14 +0000236 if (!PyArg_ParseTuple(args, "|O:write_history_file", &filename_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 return NULL;
Victor Stinner19e65a32010-06-11 22:27:14 +0000238 if (filename_obj != Py_None) {
239 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
240 return NULL;
241 filename = PyBytes_AsString(filename_bytes);
242 } else {
243 filename_bytes = NULL;
244 filename = NULL;
245 }
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100246 errno = err = write_history(filename);
247 if (!err && _history_length >= 0)
Victor Stinner19e65a32010-06-11 22:27:14 +0000248 history_truncate_file(filename, _history_length);
249 Py_XDECREF(filename_bytes);
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100250 errno = err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 if (errno)
252 return PyErr_SetFromErrno(PyExc_IOError);
253 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000254}
255
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000256PyDoc_STRVAR(doc_write_history_file,
257"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000258Save a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000259The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000260
261
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600262#ifdef HAVE_RL_APPEND_HISTORY
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600263/* Exported function to save part of a readline history file */
264
265static PyObject *
266append_history_file(PyObject *self, PyObject *args)
267{
268 int nelements;
269 PyObject *filename_obj = Py_None, *filename_bytes;
270 char *filename;
271 int err;
272 if (!PyArg_ParseTuple(args, "i|O:append_history_file", &nelements, &filename_obj))
273 return NULL;
274 if (filename_obj != Py_None) {
275 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
276 return NULL;
277 filename = PyBytes_AsString(filename_bytes);
278 } else {
279 filename_bytes = NULL;
280 filename = NULL;
281 }
282 errno = err = append_history(nelements, filename);
283 if (!err && _history_length >= 0)
284 history_truncate_file(filename, _history_length);
285 Py_XDECREF(filename_bytes);
286 errno = err;
287 if (errno)
288 return PyErr_SetFromErrno(PyExc_IOError);
289 Py_RETURN_NONE;
290}
291
292PyDoc_STRVAR(doc_append_history_file,
293"append_history_file(nelements[, filename]) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000294Append the last nelements items of the history list to file.\n\
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600295The default filename is ~/.history.");
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600296#endif
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600297
298
Guido van Rossum74f31432003-01-07 20:01:29 +0000299/* Set history length */
300
301static PyObject*
302set_history_length(PyObject *self, PyObject *args)
303{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 int length = _history_length;
305 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
306 return NULL;
307 _history_length = length;
308 Py_RETURN_NONE;
Guido van Rossum74f31432003-01-07 20:01:29 +0000309}
310
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000311PyDoc_STRVAR(set_history_length_doc,
312"set_history_length(length) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000313set the maximal number of lines which will be written to\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000314the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000315history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000316
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000317
Guido van Rossum74f31432003-01-07 20:01:29 +0000318/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000319
320static PyObject*
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000321get_history_length(PyObject *self, PyObject *noarg)
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000322{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 return PyLong_FromLong(_history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000324}
325
Guido van Rossum74f31432003-01-07 20:01:29 +0000326PyDoc_STRVAR(get_history_length_doc,
327"get_history_length() -> int\n\
Martin Panter0f767392016-04-05 07:37:22 +0000328return the maximum number of lines that will be written to\n\
Guido van Rossum74f31432003-01-07 20:01:29 +0000329the history file.");
330
331
Martin v. Löwis0daad592001-09-30 21:09:59 +0000332/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000333
Martin v. Löwis0daad592001-09-30 21:09:59 +0000334static PyObject *
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000335set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000336{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 PyObject *function = Py_None;
338 char buf[80];
339 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
340 if (!PyArg_ParseTuple(args, buf, &function))
341 return NULL;
342 if (function == Py_None) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200343 Py_CLEAR(*hook_var);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 }
345 else if (PyCallable_Check(function)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 Py_INCREF(function);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300347 Py_XSETREF(*hook_var, function);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 }
349 else {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100350 PyErr_Format(PyExc_TypeError,
351 "set_%.50s(func): argument not callable",
352 funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 return NULL;
354 }
355 Py_RETURN_NONE;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000356}
357
Guido van Rossum74f31432003-01-07 20:01:29 +0000358
Martin v. Löwis0daad592001-09-30 21:09:59 +0000359static PyObject *
Thomas Wouters89d996e2007-09-08 17:39:28 +0000360set_completion_display_matches_hook(PyObject *self, PyObject *args)
361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 PyObject *result = set_hook("completion_display_matches_hook",
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200363 &readlinestate_global->completion_display_matches_hook, args);
Christian Heimes32fbe592007-11-12 15:01:33 +0000364#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 /* We cannot set this hook globally, since it replaces the
366 default completion display. */
367 rl_completion_display_matches_hook =
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200368 readlinestate_global->completion_display_matches_hook ?
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000369#if defined(_RL_FUNCTION_TYPEDEF)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000371#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 (VFunction *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000373#endif
Christian Heimes32fbe592007-11-12 15:01:33 +0000374#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 return result;
Christian Heimes32fbe592007-11-12 15:01:33 +0000376
Thomas Wouters89d996e2007-09-08 17:39:28 +0000377}
378
379PyDoc_STRVAR(doc_set_completion_display_matches_hook,
380"set_completion_display_matches_hook([function]) -> None\n\
381Set or remove the completion display function.\n\
382The function is called as\n\
383 function(substitution, [matches], longest_match_length)\n\
384once each time matches need to be displayed.");
385
386static PyObject *
Martin v. Löwis0daad592001-09-30 21:09:59 +0000387set_startup_hook(PyObject *self, PyObject *args)
388{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200389 return set_hook("startup_hook", &readlinestate_global->startup_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000390}
391
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000392PyDoc_STRVAR(doc_set_startup_hook,
393"set_startup_hook([function]) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000394Set or remove the function invoked by the rl_startup_hook callback.\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000395The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000396before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000397
Guido van Rossum74f31432003-01-07 20:01:29 +0000398
Martin v. Löwis0daad592001-09-30 21:09:59 +0000399#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000400
401/* Set pre-input hook */
402
Martin v. Löwis0daad592001-09-30 21:09:59 +0000403static PyObject *
404set_pre_input_hook(PyObject *self, PyObject *args)
405{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200406 return set_hook("pre_input_hook", &readlinestate_global->pre_input_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000407}
408
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000409PyDoc_STRVAR(doc_set_pre_input_hook,
410"set_pre_input_hook([function]) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000411Set or remove the function invoked by the rl_pre_input_hook callback.\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000412The function is called with no arguments after the first prompt\n\
413has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000414characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000415
Martin v. Löwis0daad592001-09-30 21:09:59 +0000416#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000417
Guido van Rossum74f31432003-01-07 20:01:29 +0000418
Thomas Wouters89d996e2007-09-08 17:39:28 +0000419/* Get the completion type for the scope of the tab-completion */
420static PyObject *
421get_completion_type(PyObject *self, PyObject *noarg)
422{
Christian Heimes217cfd12007-12-02 14:31:20 +0000423 return PyLong_FromLong(rl_completion_type);
Thomas Wouters89d996e2007-09-08 17:39:28 +0000424}
425
426PyDoc_STRVAR(doc_get_completion_type,
427"get_completion_type() -> int\n\
428Get the type of completion being attempted.");
429
430
Guido van Rossum74f31432003-01-07 20:01:29 +0000431/* Get the beginning index for the scope of the tab-completion */
432
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000433static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000434get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000435{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200436 Py_INCREF(readlinestate_global->begidx);
437 return readlinestate_global->begidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000438}
439
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000440PyDoc_STRVAR(doc_get_begidx,
441"get_begidx() -> int\n\
Martin Panter0f767392016-04-05 07:37:22 +0000442get the beginning index of the completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000443
Guido van Rossum74f31432003-01-07 20:01:29 +0000444
445/* Get the ending index for the scope of the tab-completion */
446
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000447static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000448get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000449{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200450 Py_INCREF(readlinestate_global->endidx);
451 return readlinestate_global->endidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000452}
453
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000454PyDoc_STRVAR(doc_get_endidx,
455"get_endidx() -> int\n\
Martin Panter0f767392016-04-05 07:37:22 +0000456get the ending index of the completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000457
458
Guido van Rossum74f31432003-01-07 20:01:29 +0000459/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000460
461static PyObject *
Martin Panterf00c49d2016-06-14 01:16:16 +0000462set_completer_delims(PyObject *self, PyObject *string)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000463{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 char *break_chars;
Martin Panterf00c49d2016-06-14 01:16:16 +0000465 PyObject *encoded = encode(string);
466 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 return NULL;
468 }
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200469 /* Keep a reference to the allocated memory in the module state in case
470 some other module modifies rl_completer_word_break_characters
471 (see issue #17289). */
Martin Panterf00c49d2016-06-14 01:16:16 +0000472 break_chars = strdup(PyBytes_AS_STRING(encoded));
473 Py_DECREF(encoded);
Serhiy Storchaka11384392015-09-27 22:34:59 +0300474 if (break_chars) {
475 free(completer_word_break_characters);
476 completer_word_break_characters = break_chars;
477 rl_completer_word_break_characters = break_chars;
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200478 Py_RETURN_NONE;
479 }
480 else
481 return PyErr_NoMemory();
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000482}
483
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000484PyDoc_STRVAR(doc_set_completer_delims,
485"set_completer_delims(string) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000486set the word delimiters for completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000487
Mark Dickinson29b238e2010-08-03 16:08:16 +0000488/* _py_free_history_entry: Utility function to free a history entry. */
489
490#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
491
492/* Readline version >= 5.0 introduced a timestamp field into the history entry
493 structure; this needs to be freed to avoid a memory leak. This version of
494 readline also introduced the handy 'free_history_entry' function, which
495 takes care of the timestamp. */
496
497static void
498_py_free_history_entry(HIST_ENTRY *entry)
499{
500 histdata_t data = free_history_entry(entry);
501 free(data);
502}
503
504#else
505
506/* No free_history_entry function; free everything manually. */
507
508static void
509_py_free_history_entry(HIST_ENTRY *entry)
510{
511 if (entry->line)
512 free((void *)entry->line);
513 if (entry->data)
514 free(entry->data);
515 free(entry);
516}
517
518#endif
519
Skip Montanaroe5069012004-08-15 14:32:06 +0000520static PyObject *
521py_remove_history(PyObject *self, PyObject *args)
522{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 int entry_number;
524 HIST_ENTRY *entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000525
Martin Panter0f767392016-04-05 07:37:22 +0000526 if (!PyArg_ParseTuple(args, "i:remove_history_item", &entry_number))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 return NULL;
528 if (entry_number < 0) {
529 PyErr_SetString(PyExc_ValueError,
530 "History index cannot be negative");
531 return NULL;
532 }
533 entry = remove_history(entry_number);
534 if (!entry) {
535 PyErr_Format(PyExc_ValueError,
536 "No history item at position %d",
537 entry_number);
538 return NULL;
539 }
540 /* free memory allocated for the history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000541 _py_free_history_entry(entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000543}
544
545PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000546"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000547remove history item given by its position");
548
549static PyObject *
550py_replace_history(PyObject *self, PyObject *args)
551{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 int entry_number;
Martin Panterf00c49d2016-06-14 01:16:16 +0000553 PyObject *line;
554 PyObject *encoded;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 HIST_ENTRY *old_entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000556
Martin Panterf00c49d2016-06-14 01:16:16 +0000557 if (!PyArg_ParseTuple(args, "iU:replace_history_item", &entry_number,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 &line)) {
559 return NULL;
560 }
561 if (entry_number < 0) {
562 PyErr_SetString(PyExc_ValueError,
563 "History index cannot be negative");
564 return NULL;
565 }
Martin Panterf00c49d2016-06-14 01:16:16 +0000566 encoded = encode(line);
567 if (encoded == NULL) {
568 return NULL;
569 }
570 old_entry = replace_history_entry(entry_number, PyBytes_AS_STRING(encoded), (void *)NULL);
571 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 if (!old_entry) {
573 PyErr_Format(PyExc_ValueError,
574 "No history item at position %d",
575 entry_number);
576 return NULL;
577 }
578 /* free memory allocated for the old history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000579 _py_free_history_entry(old_entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000581}
582
583PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000584"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000585replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000586
587/* Add a line to the history buffer */
588
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000589static PyObject *
Martin Panterf00c49d2016-06-14 01:16:16 +0000590py_add_history(PyObject *self, PyObject *string)
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000591{
Martin Panterf00c49d2016-06-14 01:16:16 +0000592 PyObject *encoded = encode(string);
593 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 return NULL;
595 }
Martin Panterf00c49d2016-06-14 01:16:16 +0000596 add_history(PyBytes_AS_STRING(encoded));
597 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 Py_RETURN_NONE;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000599}
600
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000601PyDoc_STRVAR(doc_add_history,
602"add_history(string) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000603add an item to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000604
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000605static int should_auto_add_history = 1;
606
607/* Enable or disable automatic history */
608
609static PyObject *
610py_set_auto_history(PyObject *self, PyObject *args)
611{
612 if (!PyArg_ParseTuple(args, "p:set_auto_history",
613 &should_auto_add_history)) {
614 return NULL;
615 }
616 Py_RETURN_NONE;
617}
618
619PyDoc_STRVAR(doc_set_auto_history,
620"set_auto_history(enabled) -> None\n\
621Enables or disables automatic history.");
622
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000623
Guido van Rossum74f31432003-01-07 20:01:29 +0000624/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000625
626static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000627get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000628{
Martin Panterf00c49d2016-06-14 01:16:16 +0000629 return decode(rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000630}
Guido van Rossum74f31432003-01-07 20:01:29 +0000631
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000632PyDoc_STRVAR(doc_get_completer_delims,
633"get_completer_delims() -> string\n\
Martin Panter0f767392016-04-05 07:37:22 +0000634get the word delimiters for completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000635
Guido van Rossum74f31432003-01-07 20:01:29 +0000636
637/* Set the completer function */
638
Guido van Rossum290900a1997-09-26 21:51:21 +0000639static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000640set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000641{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200642 return set_hook("completer", &readlinestate_global->completer, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000643}
644
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000645PyDoc_STRVAR(doc_set_completer,
646"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000647Set or remove the completer function.\n\
648The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000649for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000650It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000651
Guido van Rossum74f31432003-01-07 20:01:29 +0000652
Michael W. Hudson796df152003-01-30 10:12:51 +0000653static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000654get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000655{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200656 if (readlinestate_global->completer == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 Py_RETURN_NONE;
658 }
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200659 Py_INCREF(readlinestate_global->completer);
660 return readlinestate_global->completer;
Michael W. Hudson796df152003-01-30 10:12:51 +0000661}
662
663PyDoc_STRVAR(doc_get_completer,
664"get_completer() -> function\n\
665\n\
666Returns current completer function.");
667
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000668/* Private function to get current length of history. XXX It may be
669 * possible to replace this with a direct use of history_length instead,
670 * but it's not clear whether BSD's libedit keeps history_length up to date.
671 * See issue #8065.*/
672
673static int
674_py_get_history_length(void)
675{
676 HISTORY_STATE *hist_st = history_get_history_state();
677 int length = hist_st->length;
678 /* the history docs don't say so, but the address of hist_st changes each
679 time history_get_history_state is called which makes me think it's
680 freshly malloc'd memory... on the other hand, the address of the last
681 line stays the same as long as history isn't extended, so it appears to
682 be malloc'd but managed by the history package... */
683 free(hist_st);
684 return length;
685}
686
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000687/* Exported function to get any element of history */
688
689static PyObject *
690get_history_item(PyObject *self, PyObject *args)
691{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 int idx = 0;
693 HIST_ENTRY *hist_ent;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000694
Martin Panter0f767392016-04-05 07:37:22 +0000695 if (!PyArg_ParseTuple(args, "i:get_history_item", &idx))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 return NULL;
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000697#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 if (using_libedit_emulation) {
Ned Deilyf70f4a62013-09-06 15:16:19 -0700699 /* Older versions of libedit's readline emulation
700 * use 0-based indexes, while readline and newer
701 * versions of libedit use 1-based indexes.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 */
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000703 int length = _py_get_history_length();
Ned Deilyf70f4a62013-09-06 15:16:19 -0700704
705 idx = idx - 1 + libedit_history_start;
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 /*
708 * Apple's readline emulation crashes when
709 * the index is out of range, therefore
710 * test for that and fail gracefully.
711 */
Ned Deilyf70f4a62013-09-06 15:16:19 -0700712 if (idx < (0 + libedit_history_start)
713 || idx >= (length + libedit_history_start)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 Py_RETURN_NONE;
715 }
716 }
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000717#endif /* __APPLE__ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 if ((hist_ent = history_get(idx)))
Martin Panterf00c49d2016-06-14 01:16:16 +0000719 return decode(hist_ent->line);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 else {
721 Py_RETURN_NONE;
722 }
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000723}
724
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000725PyDoc_STRVAR(doc_get_history_item,
726"get_history_item() -> string\n\
727return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000728
Guido van Rossum74f31432003-01-07 20:01:29 +0000729
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000730/* Exported function to get current length of history */
731
732static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000733get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000734{
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000735 return PyLong_FromLong((long)_py_get_history_length());
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000736}
737
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000738PyDoc_STRVAR(doc_get_current_history_length,
739"get_current_history_length() -> integer\n\
740return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000741
Guido van Rossum74f31432003-01-07 20:01:29 +0000742
Guido van Rossum79378ff1997-10-07 14:53:21 +0000743/* Exported function to read the current line buffer */
744
745static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000746get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000747{
Martin Panterf00c49d2016-06-14 01:16:16 +0000748 return decode(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000749}
750
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000751PyDoc_STRVAR(doc_get_line_buffer,
752"get_line_buffer() -> string\n\
753return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000754
Guido van Rossum74f31432003-01-07 20:01:29 +0000755
Martin v. Löwise7a97962003-09-20 16:08:33 +0000756#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
757
758/* Exported function to clear the current history */
759
760static PyObject *
761py_clear_history(PyObject *self, PyObject *noarg)
762{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 clear_history();
764 Py_RETURN_NONE;
Martin v. Löwise7a97962003-09-20 16:08:33 +0000765}
766
767PyDoc_STRVAR(doc_clear_history,
768"clear_history() -> None\n\
769Clear the current readline history.");
770#endif
771
772
Guido van Rossum79378ff1997-10-07 14:53:21 +0000773/* Exported function to insert text into the line buffer */
774
775static PyObject *
Martin Panterf00c49d2016-06-14 01:16:16 +0000776insert_text(PyObject *self, PyObject *string)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000777{
Martin Panterf00c49d2016-06-14 01:16:16 +0000778 PyObject *encoded = encode(string);
779 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 return NULL;
Martin Panterf00c49d2016-06-14 01:16:16 +0000781 }
782 rl_insert_text(PyBytes_AS_STRING(encoded));
783 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 Py_RETURN_NONE;
Guido van Rossum79378ff1997-10-07 14:53:21 +0000785}
786
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000787PyDoc_STRVAR(doc_insert_text,
788"insert_text(string) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000789Insert text into the line buffer at the cursor position.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000790
Guido van Rossum74f31432003-01-07 20:01:29 +0000791
792/* Redisplay the line buffer */
793
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000794static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000795redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000796{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 rl_redisplay();
798 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000799}
800
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000801PyDoc_STRVAR(doc_redisplay,
802"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000803Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000804contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000805
Guido van Rossum74f31432003-01-07 20:01:29 +0000806
Guido van Rossum290900a1997-09-26 21:51:21 +0000807/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000808
809static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000810{
Martin Panterf00c49d2016-06-14 01:16:16 +0000811 {"parse_and_bind", parse_and_bind, METH_O, doc_parse_and_bind},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
Martin Panterf00c49d2016-06-14 01:16:16 +0000813 {"insert_text", insert_text, METH_O, doc_insert_text},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
815 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
816 {"read_history_file", read_history_file,
817 METH_VARARGS, doc_read_history_file},
818 {"write_history_file", write_history_file,
819 METH_VARARGS, doc_write_history_file},
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600820#ifdef HAVE_RL_APPEND_HISTORY
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600821 {"append_history_file", append_history_file,
822 METH_VARARGS, doc_append_history_file},
Ned Deily8007cbc2014-11-26 13:02:33 -0800823#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 {"get_history_item", get_history_item,
825 METH_VARARGS, doc_get_history_item},
826 {"get_current_history_length", (PyCFunction)get_current_history_length,
827 METH_NOARGS, doc_get_current_history_length},
828 {"set_history_length", set_history_length,
829 METH_VARARGS, set_history_length_doc},
830 {"get_history_length", get_history_length,
831 METH_NOARGS, get_history_length_doc},
832 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
833 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
834 {"get_completion_type", get_completion_type,
835 METH_NOARGS, doc_get_completion_type},
836 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
837 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 {"set_completer_delims", set_completer_delims,
Martin Panterf00c49d2016-06-14 01:16:16 +0000840 METH_O, doc_set_completer_delims},
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000841 {"set_auto_history", py_set_auto_history, METH_VARARGS, doc_set_auto_history},
Martin Panterf00c49d2016-06-14 01:16:16 +0000842 {"add_history", py_add_history, METH_O, doc_add_history},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
844 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
845 {"get_completer_delims", get_completer_delims,
846 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 {"set_completion_display_matches_hook", set_completion_display_matches_hook,
849 METH_VARARGS, doc_set_completion_display_matches_hook},
850 {"set_startup_hook", set_startup_hook,
851 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000852#ifdef HAVE_RL_PRE_INPUT_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 {"set_pre_input_hook", set_pre_input_hook,
854 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000855#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000856#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
Martin v. Löwise7a97962003-09-20 16:08:33 +0000858#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000860};
861
Guido van Rossum05ac4492003-01-07 20:04:12 +0000862
Martin v. Löwis0daad592001-09-30 21:09:59 +0000863/* C function to call the Python hooks. */
864
865static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000866on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000867{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 int result = 0;
869 if (func != NULL) {
870 PyObject *r;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 r = PyObject_CallFunction(func, NULL);
872 if (r == NULL)
873 goto error;
874 if (r == Py_None)
875 result = 0;
876 else {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300877 result = _PyLong_AsInt(r);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 if (result == -1 && PyErr_Occurred())
879 goto error;
880 }
881 Py_DECREF(r);
882 goto done;
883 error:
884 PyErr_Clear();
885 Py_XDECREF(r);
886 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 return result;
888 }
889 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000890}
891
892static int
Ned Deily7b9ddea2014-02-05 16:53:10 -0800893#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000894on_startup_hook(void)
Ned Deily7b9ddea2014-02-05 16:53:10 -0800895#else
896on_startup_hook()
897#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000898{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200899 int r;
900#ifdef WITH_THREAD
901 PyGILState_STATE gilstate = PyGILState_Ensure();
902#endif
903 r = on_hook(readlinestate_global->startup_hook);
904#ifdef WITH_THREAD
905 PyGILState_Release(gilstate);
906#endif
907 return r;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000908}
909
910#ifdef HAVE_RL_PRE_INPUT_HOOK
911static int
Ned Deily7b9ddea2014-02-05 16:53:10 -0800912#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000913on_pre_input_hook(void)
Ned Deily7b9ddea2014-02-05 16:53:10 -0800914#else
915on_pre_input_hook()
916#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000917{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200918 int r;
919#ifdef WITH_THREAD
920 PyGILState_STATE gilstate = PyGILState_Ensure();
921#endif
922 r = on_hook(readlinestate_global->pre_input_hook);
923#ifdef WITH_THREAD
924 PyGILState_Release(gilstate);
925#endif
926 return r;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000927}
928#endif
929
Guido van Rossum05ac4492003-01-07 20:04:12 +0000930
Thomas Wouters89d996e2007-09-08 17:39:28 +0000931/* C function to call the Python completion_display_matches */
932
Georg Brandl646fdd62010-10-18 07:27:55 +0000933#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Thomas Wouters89d996e2007-09-08 17:39:28 +0000934static void
935on_completion_display_matches_hook(char **matches,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 int num_matches, int max_length)
Thomas Wouters89d996e2007-09-08 17:39:28 +0000937{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 int i;
Martin Panterf00c49d2016-06-14 01:16:16 +0000939 PyObject *sub, *m=NULL, *s=NULL, *r=NULL;
Christian Heimesaec75c32007-11-11 22:42:36 +0000940#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 PyGILState_STATE gilstate = PyGILState_Ensure();
Thomas Wouters89d996e2007-09-08 17:39:28 +0000942#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 m = PyList_New(num_matches);
944 if (m == NULL)
945 goto error;
946 for (i = 0; i < num_matches; i++) {
Martin Panterf00c49d2016-06-14 01:16:16 +0000947 s = decode(matches[i+1]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 if (s == NULL)
949 goto error;
950 if (PyList_SetItem(m, i, s) == -1)
951 goto error;
952 }
Martin Panterf00c49d2016-06-14 01:16:16 +0000953 sub = decode(matches[0]);
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200954 r = PyObject_CallFunction(readlinestate_global->completion_display_matches_hook,
Martin Panterf00c49d2016-06-14 01:16:16 +0000955 "NNi", sub, m, max_length);
Christian Heimes32fbe592007-11-12 15:01:33 +0000956
Martin Panterf00c49d2016-06-14 01:16:16 +0000957 m=NULL;
Christian Heimes32fbe592007-11-12 15:01:33 +0000958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 if (r == NULL ||
960 (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) {
961 goto error;
962 }
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200963 Py_CLEAR(r);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964
965 if (0) {
966 error:
967 PyErr_Clear();
968 Py_XDECREF(m);
969 Py_XDECREF(r);
970 }
Christian Heimes32fbe592007-11-12 15:01:33 +0000971#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 PyGILState_Release(gilstate);
Christian Heimes32fbe592007-11-12 15:01:33 +0000973#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000974}
975
Senthil Kumaran95c07002010-11-04 03:51:05 +0000976#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000977
Martin Panter5dbbf1a2016-04-03 02:54:58 +0000978#ifdef HAVE_RL_RESIZE_TERMINAL
979static volatile sig_atomic_t sigwinch_received;
Martin Panter28f35b22016-04-03 08:00:49 +0000980static PyOS_sighandler_t sigwinch_ohandler;
Martin Panter5dbbf1a2016-04-03 02:54:58 +0000981
982static void
983readline_sigwinch_handler(int signum)
984{
985 sigwinch_received = 1;
986 if (sigwinch_ohandler &&
987 sigwinch_ohandler != SIG_IGN && sigwinch_ohandler != SIG_DFL)
988 sigwinch_ohandler(signum);
989
990#ifndef HAVE_SIGACTION
991 /* If the handler was installed with signal() rather than sigaction(),
992 we need to reinstall it. */
993 PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
994#endif
995}
996#endif
997
Guido van Rossum290900a1997-09-26 21:51:21 +0000998/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000999
Guido van Rossum290900a1997-09-26 21:51:21 +00001000static char *
Guido van Rossumd8faa362007-04-27 19:54:29 +00001001on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +00001002{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 char *result = NULL;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001004 if (readlinestate_global->completer != NULL) {
Martin Panterf00c49d2016-06-14 01:16:16 +00001005 PyObject *r = NULL, *t;
Christian Heimesaec75c32007-11-11 22:42:36 +00001006#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudsonda6242c2005-03-30 11:21:53 +00001008#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 rl_attempted_completion_over = 1;
Martin Panterf00c49d2016-06-14 01:16:16 +00001010 t = decode(text);
1011 r = PyObject_CallFunction(readlinestate_global->completer, "Ni", t, state);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 if (r == NULL)
1013 goto error;
1014 if (r == Py_None) {
1015 result = NULL;
1016 }
1017 else {
Martin Panterf00c49d2016-06-14 01:16:16 +00001018 PyObject *encoded = encode(r);
1019 if (encoded == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 goto error;
Martin Panterf00c49d2016-06-14 01:16:16 +00001021 result = strdup(PyBytes_AS_STRING(encoded));
1022 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 }
1024 Py_DECREF(r);
1025 goto done;
1026 error:
1027 PyErr_Clear();
1028 Py_XDECREF(r);
1029 done:
Christian Heimesaec75c32007-11-11 22:42:36 +00001030#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 PyGILState_Release(gilstate);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +00001032#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 return result;
1034 }
1035 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +00001036}
1037
Guido van Rossum290900a1997-09-26 21:51:21 +00001038
Guido van Rossum6d0d3652003-01-07 20:34:19 +00001039/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001040 * before calling the normal completer */
1041
Neal Norwitzc355f0c2003-02-21 00:30:18 +00001042static char **
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001043flex_complete(const char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001044{
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001045 char **result;
Martin Panterf00c49d2016-06-14 01:16:16 +00001046 char saved;
1047 size_t start_size, end_size;
1048 wchar_t *s;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001049#ifdef WITH_THREAD
1050 PyGILState_STATE gilstate = PyGILState_Ensure();
1051#endif
Antoine Pitroudc0900b2009-10-19 18:22:37 +00001052#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 rl_completion_append_character ='\0';
Antoine Pitroud5131772009-10-26 19:22:14 +00001054#endif
1055#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 rl_completion_suppress_append = 0;
Antoine Pitroudc0900b2009-10-19 18:22:37 +00001057#endif
Martin Panterf00c49d2016-06-14 01:16:16 +00001058
1059 saved = rl_line_buffer[start];
1060 rl_line_buffer[start] = 0;
1061 s = Py_DecodeLocale(rl_line_buffer, &start_size);
1062 rl_line_buffer[start] = saved;
1063 if (s == NULL) {
1064 goto done;
1065 }
1066 PyMem_RawFree(s);
1067 saved = rl_line_buffer[end];
1068 rl_line_buffer[end] = 0;
1069 s = Py_DecodeLocale(rl_line_buffer + start, &end_size);
1070 rl_line_buffer[end] = saved;
1071 if (s == NULL) {
1072 goto done;
1073 }
1074 PyMem_RawFree(s);
1075 start = (int)start_size;
1076 end = start + (int)end_size;
1077
1078done:
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001079 Py_XDECREF(readlinestate_global->begidx);
1080 Py_XDECREF(readlinestate_global->endidx);
1081 readlinestate_global->begidx = PyLong_FromLong((long) start);
1082 readlinestate_global->endidx = PyLong_FromLong((long) end);
1083 result = completion_matches(text, *on_completion);
1084#ifdef WITH_THREAD
1085 PyGILState_Release(gilstate);
1086#endif
1087 return result;
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001088}
1089
Guido van Rossum05ac4492003-01-07 20:04:12 +00001090
Guido van Rossum290900a1997-09-26 21:51:21 +00001091/* Helper to initialize GNU readline properly. */
1092
1093static void
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001094setup_readline(readlinestate *mod_state)
Guido van Rossum290900a1997-09-26 21:51:21 +00001095{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +00001096#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1098 if (!saved_locale)
1099 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +00001100#endif
1101
R. David Murray52d1b4e2010-12-18 03:48:32 +00001102#ifdef __APPLE__
Victor Stinner6ced7c42011-03-21 18:15:42 +01001103 /* the libedit readline emulation resets key bindings etc
R. David Murray52d1b4e2010-12-18 03:48:32 +00001104 * when calling rl_initialize. So call it upfront
1105 */
1106 if (using_libedit_emulation)
1107 rl_initialize();
Ned Deilyf70f4a62013-09-06 15:16:19 -07001108
1109 /* Detect if libedit's readline emulation uses 0-based
1110 * indexing or 1-based indexing.
1111 */
1112 add_history("1");
1113 if (history_get(1) == NULL) {
1114 libedit_history_start = 0;
1115 } else {
1116 libedit_history_start = 1;
1117 }
1118 clear_history();
R. David Murray52d1b4e2010-12-18 03:48:32 +00001119#endif /* __APPLE__ */
1120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 using_history();
Skip Montanaroa0392742002-06-11 14:32:46 +00001122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 rl_readline_name = "python";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 /* Force rebind of TAB to insert-tab */
1125 rl_bind_key('\t', rl_insert);
1126 /* Bind both ESC-TAB and ESC-ESC to the completion function */
1127 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
1128 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin Panter5dbbf1a2016-04-03 02:54:58 +00001129#ifdef HAVE_RL_RESIZE_TERMINAL
1130 /* Set up signal handler for window resize */
1131 sigwinch_ohandler = PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
1132#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 /* Set our hook functions */
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001134 rl_startup_hook = on_startup_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001135#ifdef HAVE_RL_PRE_INPUT_HOOK
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001136 rl_pre_input_hook = on_pre_input_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001137#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 /* Set our completion function */
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001139 rl_attempted_completion_function = flex_complete;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 /* Set Python word break characters */
Antoine Pitroua7f7deb2013-05-06 21:51:03 +02001141 completer_word_break_characters =
1142 rl_completer_word_break_characters =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
1144 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001145
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001146 mod_state->begidx = PyLong_FromLong(0L);
1147 mod_state->endidx = PyLong_FromLong(0L);
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001148
Victor Stinner92639cc2014-07-24 22:11:38 +02001149#ifndef __APPLE__
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001150 if (!isatty(STDOUT_FILENO)) {
Martin Pantere26da7c2016-06-02 10:07:09 +00001151 /* Issue #19884: stdout is not a terminal. Disable meta modifier
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001152 keys to not write the ANSI sequence "\033[1034h" into stdout. On
1153 terminals supporting 8 bit characters like TERM=xterm-256color
1154 (which is now the default Fedora since Fedora 18), the meta key is
1155 used to enable support of 8 bit characters (ANSI sequence
Victor Stinner92639cc2014-07-24 22:11:38 +02001156 "\033[1034h").
1157
1158 With libedit, this call makes readline() crash. */
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001159 rl_variable_bind ("enable-meta-key", "off");
1160 }
Victor Stinner92639cc2014-07-24 22:11:38 +02001161#endif
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 /* Initialize (allows .inputrc to override)
1164 *
1165 * XXX: A bug in the readline-2.2 library causes a memory leak
1166 * inside this function. Nothing we can do about it.
1167 */
R. David Murray52d1b4e2010-12-18 03:48:32 +00001168#ifdef __APPLE__
1169 if (using_libedit_emulation)
Victor Stinner6ced7c42011-03-21 18:15:42 +01001170 rl_read_init_file(NULL);
R. David Murray52d1b4e2010-12-18 03:48:32 +00001171 else
1172#endif /* __APPLE__ */
1173 rl_initialize();
Victor Stinner6ced7c42011-03-21 18:15:42 +01001174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 RESTORE_LOCALE(saved_locale)
Guido van Rossum290900a1997-09-26 21:51:21 +00001176}
1177
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001178/* Wrapper around GNU readline that handles signals differently. */
1179
1180
1181#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
1182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183static char *completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001184static void
1185rlhandler(char *text)
1186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 completed_input_string = text;
1188 rl_callback_handler_remove();
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001189}
1190
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001191static char *
Serhiy Storchakac6792272013-10-19 21:03:34 +03001192readline_until_enter_or_signal(const char *prompt, int *signal)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001193{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 char * not_done_reading = "";
1195 fd_set selectset;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 *signal = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001198#ifdef HAVE_RL_CATCH_SIGNAL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 rl_catch_signals = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001200#endif
1201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 rl_callback_handler_install (prompt, rlhandler);
1203 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001204
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 completed_input_string = not_done_reading;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 while (completed_input_string == not_done_reading) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001208 int has_input = 0, err = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 while (!has_input)
1211 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Michael W. Hudson8da2b012004-10-07 13:46:33 +00001212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 /* [Bug #1552726] Only limit the pause if an input hook has been
1214 defined. */
1215 struct timeval *timeoutp = NULL;
1216 if (PyOS_InputHook)
1217 timeoutp = &timeout;
Martin Panter5dbbf1a2016-04-03 02:54:58 +00001218#ifdef HAVE_RL_RESIZE_TERMINAL
1219 /* Update readline's view of the window size after SIGWINCH */
1220 if (sigwinch_received) {
1221 sigwinch_received = 0;
1222 rl_resize_terminal();
1223 }
1224#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 FD_SET(fileno(rl_instream), &selectset);
1226 /* select resets selectset if no input was available */
1227 has_input = select(fileno(rl_instream) + 1, &selectset,
1228 NULL, NULL, timeoutp);
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001229 err = errno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 if(PyOS_InputHook) PyOS_InputHook();
1231 }
1232
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001233 if (has_input > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 rl_callback_read_char();
1235 }
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001236 else if (err == EINTR) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001238#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001240#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001242#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001244#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 if (s < 0) {
1246 rl_free_line_state();
Martin Panterd6990d22016-03-22 07:24:05 +00001247#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0700
1248 rl_callback_sigcleanup();
1249#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 rl_cleanup_after_signal();
1251 rl_callback_handler_remove();
1252 *signal = 1;
1253 completed_input_string = NULL;
1254 }
1255 }
1256 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 return completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001259}
1260
1261
1262#else
Guido van Rossum290900a1997-09-26 21:51:21 +00001263
1264/* Interrupt handler */
1265
1266static jmp_buf jbuf;
1267
Guido van Rossum0969d361997-08-05 21:27:50 +00001268/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +00001269static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001270onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +00001271{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +00001273}
1274
Guido van Rossum290900a1997-09-26 21:51:21 +00001275
Guido van Rossum0969d361997-08-05 21:27:50 +00001276static char *
Christian Heimesa3da7c52013-12-04 09:31:47 +01001277readline_until_enter_or_signal(const char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +00001278{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 PyOS_sighandler_t old_inthandler;
1280 char *p;
Guido van Rossum74f31432003-01-07 20:01:29 +00001281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 *signal = 0;
1283
1284 old_inthandler = PyOS_setsig(SIGINT, onintr);
1285 if (setjmp(jbuf)) {
Guido van Rossum0969d361997-08-05 21:27:50 +00001286#ifdef HAVE_SIGRELSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
1288 sigrelse(SIGINT);
Guido van Rossum0969d361997-08-05 21:27:50 +00001289#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 PyOS_setsig(SIGINT, old_inthandler);
1291 *signal = 1;
1292 return NULL;
1293 }
1294 rl_event_hook = PyOS_InputHook;
1295 p = readline(prompt);
1296 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001297
1298 return p;
1299}
1300#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
1301
1302
1303static char *
Serhiy Storchakac6792272013-10-19 21:03:34 +03001304call_readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 size_t n;
1307 char *p, *q;
1308 int signal;
Neal Norwitz1fa040b2004-08-25 01:20:18 +00001309
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001310#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1312 if (!saved_locale)
1313 Py_FatalError("not enough memory to save locale");
Nadeem Vawda63752572013-02-02 20:05:11 +01001314 setlocale(LC_CTYPE, "");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001315#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
1318 rl_instream = sys_stdin;
1319 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001320#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001322#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 }
Guido van Rossum74f31432003-01-07 20:01:29 +00001324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 p = readline_until_enter_or_signal(prompt, &signal);
Guido van Rossumb18618d2000-05-03 23:44:39 +00001326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 /* we got an interrupt signal */
1328 if (signal) {
1329 RESTORE_LOCALE(saved_locale)
1330 return NULL;
1331 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001332
Martin Panter7462b6492015-11-02 03:37:02 +00001333 /* We got an EOF, return an empty string. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 if (p == NULL) {
Victor Stinner2fe9bac2013-10-10 16:18:20 +02001335 p = PyMem_RawMalloc(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 if (p != NULL)
1337 *p = '\0';
1338 RESTORE_LOCALE(saved_locale)
1339 return p;
1340 }
1341
1342 /* we have a valid line */
1343 n = strlen(p);
Martin Panterf0dbf7a2016-05-15 01:26:25 +00001344 if (should_auto_add_history && n > 0) {
Brett Cannon2525dc82010-08-22 20:36:25 +00001345 const char *line;
Mark Dickinson6b54e1f2010-08-03 16:49:49 +00001346 int length = _py_get_history_length();
1347 if (length > 0)
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001348#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 if (using_libedit_emulation) {
Ned Deilyf70f4a62013-09-06 15:16:19 -07001350 /* handle older 0-based or newer 1-based indexing */
1351 line = (const char *)history_get(length + libedit_history_start - 1)->line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 } else
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001353#endif /* __APPLE__ */
Brett Cannon2525dc82010-08-22 20:36:25 +00001354 line = (const char *)history_get(length)->line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 else
1356 line = "";
1357 if (strcmp(p, line))
1358 add_history(p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 }
1360 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
1361 release the original. */
1362 q = p;
Victor Stinner2fe9bac2013-10-10 16:18:20 +02001363 p = PyMem_RawMalloc(n+2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 if (p != NULL) {
1365 strncpy(p, q, n);
1366 p[n] = '\n';
1367 p[n+1] = '\0';
1368 }
1369 free(q);
1370 RESTORE_LOCALE(saved_locale)
1371 return p;
Guido van Rossum0969d361997-08-05 21:27:50 +00001372}
1373
Guido van Rossum290900a1997-09-26 21:51:21 +00001374
1375/* Initialize the module */
1376
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001377PyDoc_STRVAR(doc_module,
1378"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001379
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001380#ifdef __APPLE__
1381PyDoc_STRVAR(doc_module_le,
1382"Importing this module enables command line editing using libedit readline.");
1383#endif /* __APPLE__ */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001384
1385static struct PyModuleDef readlinemodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 PyModuleDef_HEAD_INIT,
1387 "readline",
1388 doc_module,
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001389 sizeof(readlinestate),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 readline_methods,
1391 NULL,
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001392 readline_traverse,
1393 readline_clear,
1394 readline_free
Martin v. Löwis1a214512008-06-11 05:26:20 +00001395};
1396
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001397
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001398PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001399PyInit_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001400{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 PyObject *m;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001402 readlinestate *mod_state;
Guido van Rossum290900a1997-09-26 21:51:21 +00001403
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001404#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
1406 using_libedit_emulation = 1;
1407 }
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 if (using_libedit_emulation)
1410 readlinemodule.m_doc = doc_module_le;
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001411
1412#endif /* __APPLE__ */
1413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 m = PyModule_Create(&readlinemodule);
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 if (m == NULL)
1417 return NULL;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001418
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001419 mod_state = (readlinestate *) PyModule_GetState(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 PyOS_ReadlineFunctionPointer = call_readline;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001421 setup_readline(mod_state);
Antoine Pitrou7e8b8672014-11-04 14:52:10 +01001422
1423 PyModule_AddIntConstant(m, "_READLINE_VERSION", RL_READLINE_VERSION);
1424 PyModule_AddIntConstant(m, "_READLINE_RUNTIME_VERSION", rl_readline_version);
1425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 return m;
Guido van Rossum0969d361997-08-05 21:27:50 +00001427}