blob: 54f15bc1c934de688ae585c290e08db8cc2440f2 [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)) {
346 PyObject *tmp = *hook_var;
347 Py_INCREF(function);
348 *hook_var = function;
349 Py_XDECREF(tmp);
350 }
351 else {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100352 PyErr_Format(PyExc_TypeError,
353 "set_%.50s(func): argument not callable",
354 funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 return NULL;
356 }
357 Py_RETURN_NONE;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000358}
359
Guido van Rossum74f31432003-01-07 20:01:29 +0000360
Martin v. Löwis0daad592001-09-30 21:09:59 +0000361static PyObject *
Thomas Wouters89d996e2007-09-08 17:39:28 +0000362set_completion_display_matches_hook(PyObject *self, PyObject *args)
363{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 PyObject *result = set_hook("completion_display_matches_hook",
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200365 &readlinestate_global->completion_display_matches_hook, args);
Christian Heimes32fbe592007-11-12 15:01:33 +0000366#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 /* We cannot set this hook globally, since it replaces the
368 default completion display. */
369 rl_completion_display_matches_hook =
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200370 readlinestate_global->completion_display_matches_hook ?
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000371#if defined(_RL_FUNCTION_TYPEDEF)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000373#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 (VFunction *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000375#endif
Christian Heimes32fbe592007-11-12 15:01:33 +0000376#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 return result;
Christian Heimes32fbe592007-11-12 15:01:33 +0000378
Thomas Wouters89d996e2007-09-08 17:39:28 +0000379}
380
381PyDoc_STRVAR(doc_set_completion_display_matches_hook,
382"set_completion_display_matches_hook([function]) -> None\n\
383Set or remove the completion display function.\n\
384The function is called as\n\
385 function(substitution, [matches], longest_match_length)\n\
386once each time matches need to be displayed.");
387
388static PyObject *
Martin v. Löwis0daad592001-09-30 21:09:59 +0000389set_startup_hook(PyObject *self, PyObject *args)
390{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200391 return set_hook("startup_hook", &readlinestate_global->startup_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000392}
393
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000394PyDoc_STRVAR(doc_set_startup_hook,
395"set_startup_hook([function]) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000396Set or remove the function invoked by the rl_startup_hook callback.\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000397The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000398before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000399
Guido van Rossum74f31432003-01-07 20:01:29 +0000400
Martin v. Löwis0daad592001-09-30 21:09:59 +0000401#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000402
403/* Set pre-input hook */
404
Martin v. Löwis0daad592001-09-30 21:09:59 +0000405static PyObject *
406set_pre_input_hook(PyObject *self, PyObject *args)
407{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200408 return set_hook("pre_input_hook", &readlinestate_global->pre_input_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000409}
410
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000411PyDoc_STRVAR(doc_set_pre_input_hook,
412"set_pre_input_hook([function]) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000413Set or remove the function invoked by the rl_pre_input_hook callback.\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000414The function is called with no arguments after the first prompt\n\
415has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000416characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000417
Martin v. Löwis0daad592001-09-30 21:09:59 +0000418#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000419
Guido van Rossum74f31432003-01-07 20:01:29 +0000420
Thomas Wouters89d996e2007-09-08 17:39:28 +0000421/* Get the completion type for the scope of the tab-completion */
422static PyObject *
423get_completion_type(PyObject *self, PyObject *noarg)
424{
Christian Heimes217cfd12007-12-02 14:31:20 +0000425 return PyLong_FromLong(rl_completion_type);
Thomas Wouters89d996e2007-09-08 17:39:28 +0000426}
427
428PyDoc_STRVAR(doc_get_completion_type,
429"get_completion_type() -> int\n\
430Get the type of completion being attempted.");
431
432
Guido van Rossum74f31432003-01-07 20:01:29 +0000433/* Get the beginning index for the scope of the tab-completion */
434
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000435static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000436get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000437{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200438 Py_INCREF(readlinestate_global->begidx);
439 return readlinestate_global->begidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000440}
441
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000442PyDoc_STRVAR(doc_get_begidx,
443"get_begidx() -> int\n\
Martin Panter0f767392016-04-05 07:37:22 +0000444get the beginning index of the completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000445
Guido van Rossum74f31432003-01-07 20:01:29 +0000446
447/* Get the ending index for the scope of the tab-completion */
448
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000449static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000450get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000451{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200452 Py_INCREF(readlinestate_global->endidx);
453 return readlinestate_global->endidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000454}
455
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000456PyDoc_STRVAR(doc_get_endidx,
457"get_endidx() -> int\n\
Martin Panter0f767392016-04-05 07:37:22 +0000458get the ending index of the completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000459
460
Guido van Rossum74f31432003-01-07 20:01:29 +0000461/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000462
463static PyObject *
Martin Panterf00c49d2016-06-14 01:16:16 +0000464set_completer_delims(PyObject *self, PyObject *string)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000465{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 char *break_chars;
Martin Panterf00c49d2016-06-14 01:16:16 +0000467 PyObject *encoded = encode(string);
468 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 return NULL;
470 }
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200471 /* Keep a reference to the allocated memory in the module state in case
472 some other module modifies rl_completer_word_break_characters
473 (see issue #17289). */
Martin Panterf00c49d2016-06-14 01:16:16 +0000474 break_chars = strdup(PyBytes_AS_STRING(encoded));
475 Py_DECREF(encoded);
Serhiy Storchaka11384392015-09-27 22:34:59 +0300476 if (break_chars) {
477 free(completer_word_break_characters);
478 completer_word_break_characters = break_chars;
479 rl_completer_word_break_characters = break_chars;
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200480 Py_RETURN_NONE;
481 }
482 else
483 return PyErr_NoMemory();
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000484}
485
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000486PyDoc_STRVAR(doc_set_completer_delims,
487"set_completer_delims(string) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000488set the word delimiters for completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000489
Mark Dickinson29b238e2010-08-03 16:08:16 +0000490/* _py_free_history_entry: Utility function to free a history entry. */
491
492#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
493
494/* Readline version >= 5.0 introduced a timestamp field into the history entry
495 structure; this needs to be freed to avoid a memory leak. This version of
496 readline also introduced the handy 'free_history_entry' function, which
497 takes care of the timestamp. */
498
499static void
500_py_free_history_entry(HIST_ENTRY *entry)
501{
502 histdata_t data = free_history_entry(entry);
503 free(data);
504}
505
506#else
507
508/* No free_history_entry function; free everything manually. */
509
510static void
511_py_free_history_entry(HIST_ENTRY *entry)
512{
513 if (entry->line)
514 free((void *)entry->line);
515 if (entry->data)
516 free(entry->data);
517 free(entry);
518}
519
520#endif
521
Skip Montanaroe5069012004-08-15 14:32:06 +0000522static PyObject *
523py_remove_history(PyObject *self, PyObject *args)
524{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 int entry_number;
526 HIST_ENTRY *entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000527
Martin Panter0f767392016-04-05 07:37:22 +0000528 if (!PyArg_ParseTuple(args, "i:remove_history_item", &entry_number))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 return NULL;
530 if (entry_number < 0) {
531 PyErr_SetString(PyExc_ValueError,
532 "History index cannot be negative");
533 return NULL;
534 }
535 entry = remove_history(entry_number);
536 if (!entry) {
537 PyErr_Format(PyExc_ValueError,
538 "No history item at position %d",
539 entry_number);
540 return NULL;
541 }
542 /* free memory allocated for the history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000543 _py_free_history_entry(entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000545}
546
547PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000548"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000549remove history item given by its position");
550
551static PyObject *
552py_replace_history(PyObject *self, PyObject *args)
553{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 int entry_number;
Martin Panterf00c49d2016-06-14 01:16:16 +0000555 PyObject *line;
556 PyObject *encoded;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 HIST_ENTRY *old_entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000558
Martin Panterf00c49d2016-06-14 01:16:16 +0000559 if (!PyArg_ParseTuple(args, "iU:replace_history_item", &entry_number,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 &line)) {
561 return NULL;
562 }
563 if (entry_number < 0) {
564 PyErr_SetString(PyExc_ValueError,
565 "History index cannot be negative");
566 return NULL;
567 }
Martin Panterf00c49d2016-06-14 01:16:16 +0000568 encoded = encode(line);
569 if (encoded == NULL) {
570 return NULL;
571 }
572 old_entry = replace_history_entry(entry_number, PyBytes_AS_STRING(encoded), (void *)NULL);
573 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 if (!old_entry) {
575 PyErr_Format(PyExc_ValueError,
576 "No history item at position %d",
577 entry_number);
578 return NULL;
579 }
580 /* free memory allocated for the old history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000581 _py_free_history_entry(old_entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000583}
584
585PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000586"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000587replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000588
589/* Add a line to the history buffer */
590
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000591static PyObject *
Martin Panterf00c49d2016-06-14 01:16:16 +0000592py_add_history(PyObject *self, PyObject *string)
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000593{
Martin Panterf00c49d2016-06-14 01:16:16 +0000594 PyObject *encoded = encode(string);
595 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 return NULL;
597 }
Martin Panterf00c49d2016-06-14 01:16:16 +0000598 add_history(PyBytes_AS_STRING(encoded));
599 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 Py_RETURN_NONE;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000601}
602
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000603PyDoc_STRVAR(doc_add_history,
604"add_history(string) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000605add an item to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000606
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000607
Guido van Rossum74f31432003-01-07 20:01:29 +0000608/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000609
610static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000611get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000612{
Martin Panterf00c49d2016-06-14 01:16:16 +0000613 return decode(rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000614}
Guido van Rossum74f31432003-01-07 20:01:29 +0000615
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000616PyDoc_STRVAR(doc_get_completer_delims,
617"get_completer_delims() -> string\n\
Martin Panter0f767392016-04-05 07:37:22 +0000618get the word delimiters for completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000619
Guido van Rossum74f31432003-01-07 20:01:29 +0000620
621/* Set the completer function */
622
Guido van Rossum290900a1997-09-26 21:51:21 +0000623static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000624set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000625{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200626 return set_hook("completer", &readlinestate_global->completer, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000627}
628
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000629PyDoc_STRVAR(doc_set_completer,
630"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000631Set or remove the completer function.\n\
632The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000633for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000634It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000635
Guido van Rossum74f31432003-01-07 20:01:29 +0000636
Michael W. Hudson796df152003-01-30 10:12:51 +0000637static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000638get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000639{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200640 if (readlinestate_global->completer == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 Py_RETURN_NONE;
642 }
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200643 Py_INCREF(readlinestate_global->completer);
644 return readlinestate_global->completer;
Michael W. Hudson796df152003-01-30 10:12:51 +0000645}
646
647PyDoc_STRVAR(doc_get_completer,
648"get_completer() -> function\n\
649\n\
650Returns current completer function.");
651
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000652/* Private function to get current length of history. XXX It may be
653 * possible to replace this with a direct use of history_length instead,
654 * but it's not clear whether BSD's libedit keeps history_length up to date.
655 * See issue #8065.*/
656
657static int
658_py_get_history_length(void)
659{
660 HISTORY_STATE *hist_st = history_get_history_state();
661 int length = hist_st->length;
662 /* the history docs don't say so, but the address of hist_st changes each
663 time history_get_history_state is called which makes me think it's
664 freshly malloc'd memory... on the other hand, the address of the last
665 line stays the same as long as history isn't extended, so it appears to
666 be malloc'd but managed by the history package... */
667 free(hist_st);
668 return length;
669}
670
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000671/* Exported function to get any element of history */
672
673static PyObject *
674get_history_item(PyObject *self, PyObject *args)
675{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 int idx = 0;
677 HIST_ENTRY *hist_ent;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000678
Martin Panter0f767392016-04-05 07:37:22 +0000679 if (!PyArg_ParseTuple(args, "i:get_history_item", &idx))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 return NULL;
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000681#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 if (using_libedit_emulation) {
Ned Deilyf70f4a62013-09-06 15:16:19 -0700683 /* Older versions of libedit's readline emulation
684 * use 0-based indexes, while readline and newer
685 * versions of libedit use 1-based indexes.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 */
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000687 int length = _py_get_history_length();
Ned Deilyf70f4a62013-09-06 15:16:19 -0700688
689 idx = idx - 1 + libedit_history_start;
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 /*
692 * Apple's readline emulation crashes when
693 * the index is out of range, therefore
694 * test for that and fail gracefully.
695 */
Ned Deilyf70f4a62013-09-06 15:16:19 -0700696 if (idx < (0 + libedit_history_start)
697 || idx >= (length + libedit_history_start)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 Py_RETURN_NONE;
699 }
700 }
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000701#endif /* __APPLE__ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 if ((hist_ent = history_get(idx)))
Martin Panterf00c49d2016-06-14 01:16:16 +0000703 return decode(hist_ent->line);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 else {
705 Py_RETURN_NONE;
706 }
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000707}
708
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000709PyDoc_STRVAR(doc_get_history_item,
710"get_history_item() -> string\n\
711return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000712
Guido van Rossum74f31432003-01-07 20:01:29 +0000713
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000714/* Exported function to get current length of history */
715
716static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000717get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000718{
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000719 return PyLong_FromLong((long)_py_get_history_length());
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000720}
721
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000722PyDoc_STRVAR(doc_get_current_history_length,
723"get_current_history_length() -> integer\n\
724return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000725
Guido van Rossum74f31432003-01-07 20:01:29 +0000726
Guido van Rossum79378ff1997-10-07 14:53:21 +0000727/* Exported function to read the current line buffer */
728
729static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000730get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000731{
Martin Panterf00c49d2016-06-14 01:16:16 +0000732 return decode(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000733}
734
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000735PyDoc_STRVAR(doc_get_line_buffer,
736"get_line_buffer() -> string\n\
737return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000738
Guido van Rossum74f31432003-01-07 20:01:29 +0000739
Martin v. Löwise7a97962003-09-20 16:08:33 +0000740#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
741
742/* Exported function to clear the current history */
743
744static PyObject *
745py_clear_history(PyObject *self, PyObject *noarg)
746{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 clear_history();
748 Py_RETURN_NONE;
Martin v. Löwise7a97962003-09-20 16:08:33 +0000749}
750
751PyDoc_STRVAR(doc_clear_history,
752"clear_history() -> None\n\
753Clear the current readline history.");
754#endif
755
756
Guido van Rossum79378ff1997-10-07 14:53:21 +0000757/* Exported function to insert text into the line buffer */
758
759static PyObject *
Martin Panterf00c49d2016-06-14 01:16:16 +0000760insert_text(PyObject *self, PyObject *string)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000761{
Martin Panterf00c49d2016-06-14 01:16:16 +0000762 PyObject *encoded = encode(string);
763 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 return NULL;
Martin Panterf00c49d2016-06-14 01:16:16 +0000765 }
766 rl_insert_text(PyBytes_AS_STRING(encoded));
767 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 Py_RETURN_NONE;
Guido van Rossum79378ff1997-10-07 14:53:21 +0000769}
770
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000771PyDoc_STRVAR(doc_insert_text,
772"insert_text(string) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000773Insert text into the line buffer at the cursor position.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000774
Guido van Rossum74f31432003-01-07 20:01:29 +0000775
776/* Redisplay the line buffer */
777
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000778static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000779redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000780{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 rl_redisplay();
782 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000783}
784
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000785PyDoc_STRVAR(doc_redisplay,
786"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000787Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000788contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000789
Guido van Rossum74f31432003-01-07 20:01:29 +0000790
Guido van Rossum290900a1997-09-26 21:51:21 +0000791/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000792
793static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000794{
Martin Panterf00c49d2016-06-14 01:16:16 +0000795 {"parse_and_bind", parse_and_bind, METH_O, doc_parse_and_bind},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
Martin Panterf00c49d2016-06-14 01:16:16 +0000797 {"insert_text", insert_text, METH_O, doc_insert_text},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
799 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
800 {"read_history_file", read_history_file,
801 METH_VARARGS, doc_read_history_file},
802 {"write_history_file", write_history_file,
803 METH_VARARGS, doc_write_history_file},
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600804#ifdef HAVE_RL_APPEND_HISTORY
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600805 {"append_history_file", append_history_file,
806 METH_VARARGS, doc_append_history_file},
Ned Deily8007cbc2014-11-26 13:02:33 -0800807#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 {"get_history_item", get_history_item,
809 METH_VARARGS, doc_get_history_item},
810 {"get_current_history_length", (PyCFunction)get_current_history_length,
811 METH_NOARGS, doc_get_current_history_length},
812 {"set_history_length", set_history_length,
813 METH_VARARGS, set_history_length_doc},
814 {"get_history_length", get_history_length,
815 METH_NOARGS, get_history_length_doc},
816 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
817 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
818 {"get_completion_type", get_completion_type,
819 METH_NOARGS, doc_get_completion_type},
820 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
821 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 {"set_completer_delims", set_completer_delims,
Martin Panterf00c49d2016-06-14 01:16:16 +0000824 METH_O, doc_set_completer_delims},
825 {"add_history", py_add_history, METH_O, doc_add_history},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
827 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
828 {"get_completer_delims", get_completer_delims,
829 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 {"set_completion_display_matches_hook", set_completion_display_matches_hook,
832 METH_VARARGS, doc_set_completion_display_matches_hook},
833 {"set_startup_hook", set_startup_hook,
834 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000835#ifdef HAVE_RL_PRE_INPUT_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 {"set_pre_input_hook", set_pre_input_hook,
837 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000838#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000839#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
Martin v. Löwise7a97962003-09-20 16:08:33 +0000841#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000843};
844
Guido van Rossum05ac4492003-01-07 20:04:12 +0000845
Martin v. Löwis0daad592001-09-30 21:09:59 +0000846/* C function to call the Python hooks. */
847
848static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000849on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000850{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 int result = 0;
852 if (func != NULL) {
853 PyObject *r;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 r = PyObject_CallFunction(func, NULL);
855 if (r == NULL)
856 goto error;
857 if (r == Py_None)
858 result = 0;
859 else {
860 result = PyLong_AsLong(r);
861 if (result == -1 && PyErr_Occurred())
862 goto error;
863 }
864 Py_DECREF(r);
865 goto done;
866 error:
867 PyErr_Clear();
868 Py_XDECREF(r);
869 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 return result;
871 }
872 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000873}
874
875static int
Ned Deily7b9ddea2014-02-05 16:53:10 -0800876#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000877on_startup_hook(void)
Ned Deily7b9ddea2014-02-05 16:53:10 -0800878#else
879on_startup_hook()
880#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000881{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200882 int r;
883#ifdef WITH_THREAD
884 PyGILState_STATE gilstate = PyGILState_Ensure();
885#endif
886 r = on_hook(readlinestate_global->startup_hook);
887#ifdef WITH_THREAD
888 PyGILState_Release(gilstate);
889#endif
890 return r;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000891}
892
893#ifdef HAVE_RL_PRE_INPUT_HOOK
894static int
Ned Deily7b9ddea2014-02-05 16:53:10 -0800895#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000896on_pre_input_hook(void)
Ned Deily7b9ddea2014-02-05 16:53:10 -0800897#else
898on_pre_input_hook()
899#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000900{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200901 int r;
902#ifdef WITH_THREAD
903 PyGILState_STATE gilstate = PyGILState_Ensure();
904#endif
905 r = on_hook(readlinestate_global->pre_input_hook);
906#ifdef WITH_THREAD
907 PyGILState_Release(gilstate);
908#endif
909 return r;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000910}
911#endif
912
Guido van Rossum05ac4492003-01-07 20:04:12 +0000913
Thomas Wouters89d996e2007-09-08 17:39:28 +0000914/* C function to call the Python completion_display_matches */
915
Georg Brandl646fdd62010-10-18 07:27:55 +0000916#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Thomas Wouters89d996e2007-09-08 17:39:28 +0000917static void
918on_completion_display_matches_hook(char **matches,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 int num_matches, int max_length)
Thomas Wouters89d996e2007-09-08 17:39:28 +0000920{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 int i;
Martin Panterf00c49d2016-06-14 01:16:16 +0000922 PyObject *sub, *m=NULL, *s=NULL, *r=NULL;
Christian Heimesaec75c32007-11-11 22:42:36 +0000923#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 PyGILState_STATE gilstate = PyGILState_Ensure();
Thomas Wouters89d996e2007-09-08 17:39:28 +0000925#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 m = PyList_New(num_matches);
927 if (m == NULL)
928 goto error;
929 for (i = 0; i < num_matches; i++) {
Martin Panterf00c49d2016-06-14 01:16:16 +0000930 s = decode(matches[i+1]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 if (s == NULL)
932 goto error;
933 if (PyList_SetItem(m, i, s) == -1)
934 goto error;
935 }
Martin Panterf00c49d2016-06-14 01:16:16 +0000936 sub = decode(matches[0]);
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200937 r = PyObject_CallFunction(readlinestate_global->completion_display_matches_hook,
Martin Panterf00c49d2016-06-14 01:16:16 +0000938 "NNi", sub, m, max_length);
Christian Heimes32fbe592007-11-12 15:01:33 +0000939
Martin Panterf00c49d2016-06-14 01:16:16 +0000940 m=NULL;
Christian Heimes32fbe592007-11-12 15:01:33 +0000941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 if (r == NULL ||
943 (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) {
944 goto error;
945 }
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200946 Py_CLEAR(r);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947
948 if (0) {
949 error:
950 PyErr_Clear();
951 Py_XDECREF(m);
952 Py_XDECREF(r);
953 }
Christian Heimes32fbe592007-11-12 15:01:33 +0000954#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 PyGILState_Release(gilstate);
Christian Heimes32fbe592007-11-12 15:01:33 +0000956#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000957}
958
Senthil Kumaran95c07002010-11-04 03:51:05 +0000959#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000960
Martin Panter5dbbf1a2016-04-03 02:54:58 +0000961#ifdef HAVE_RL_RESIZE_TERMINAL
962static volatile sig_atomic_t sigwinch_received;
Martin Panter28f35b22016-04-03 08:00:49 +0000963static PyOS_sighandler_t sigwinch_ohandler;
Martin Panter5dbbf1a2016-04-03 02:54:58 +0000964
965static void
966readline_sigwinch_handler(int signum)
967{
968 sigwinch_received = 1;
969 if (sigwinch_ohandler &&
970 sigwinch_ohandler != SIG_IGN && sigwinch_ohandler != SIG_DFL)
971 sigwinch_ohandler(signum);
972
973#ifndef HAVE_SIGACTION
974 /* If the handler was installed with signal() rather than sigaction(),
975 we need to reinstall it. */
976 PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
977#endif
978}
979#endif
980
Guido van Rossum290900a1997-09-26 21:51:21 +0000981/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000982
Guido van Rossum290900a1997-09-26 21:51:21 +0000983static char *
Guido van Rossumd8faa362007-04-27 19:54:29 +0000984on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000985{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 char *result = NULL;
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200987 if (readlinestate_global->completer != NULL) {
Martin Panterf00c49d2016-06-14 01:16:16 +0000988 PyObject *r = NULL, *t;
Christian Heimesaec75c32007-11-11 22:42:36 +0000989#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000991#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 rl_attempted_completion_over = 1;
Martin Panterf00c49d2016-06-14 01:16:16 +0000993 t = decode(text);
994 r = PyObject_CallFunction(readlinestate_global->completer, "Ni", t, state);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 if (r == NULL)
996 goto error;
997 if (r == Py_None) {
998 result = NULL;
999 }
1000 else {
Martin Panterf00c49d2016-06-14 01:16:16 +00001001 PyObject *encoded = encode(r);
1002 if (encoded == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 goto error;
Martin Panterf00c49d2016-06-14 01:16:16 +00001004 result = strdup(PyBytes_AS_STRING(encoded));
1005 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 }
1007 Py_DECREF(r);
1008 goto done;
1009 error:
1010 PyErr_Clear();
1011 Py_XDECREF(r);
1012 done:
Christian Heimesaec75c32007-11-11 22:42:36 +00001013#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 PyGILState_Release(gilstate);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +00001015#endif
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#ifdef WITH_THREAD
1033 PyGILState_STATE gilstate = PyGILState_Ensure();
1034#endif
Antoine Pitroudc0900b2009-10-19 18:22:37 +00001035#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 rl_completion_append_character ='\0';
Antoine Pitroud5131772009-10-26 19:22:14 +00001037#endif
1038#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 rl_completion_suppress_append = 0;
Antoine Pitroudc0900b2009-10-19 18:22:37 +00001040#endif
Martin Panterf00c49d2016-06-14 01:16:16 +00001041
1042 saved = rl_line_buffer[start];
1043 rl_line_buffer[start] = 0;
1044 s = Py_DecodeLocale(rl_line_buffer, &start_size);
1045 rl_line_buffer[start] = saved;
1046 if (s == NULL) {
1047 goto done;
1048 }
1049 PyMem_RawFree(s);
1050 saved = rl_line_buffer[end];
1051 rl_line_buffer[end] = 0;
1052 s = Py_DecodeLocale(rl_line_buffer + start, &end_size);
1053 rl_line_buffer[end] = saved;
1054 if (s == NULL) {
1055 goto done;
1056 }
1057 PyMem_RawFree(s);
1058 start = (int)start_size;
1059 end = start + (int)end_size;
1060
1061done:
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001062 Py_XDECREF(readlinestate_global->begidx);
1063 Py_XDECREF(readlinestate_global->endidx);
1064 readlinestate_global->begidx = PyLong_FromLong((long) start);
1065 readlinestate_global->endidx = PyLong_FromLong((long) end);
Benjamin Peterson1bb0c0b2016-09-05 18:26:19 -07001066 result = completion_matches((char *)text, *on_completion);
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001067#ifdef WITH_THREAD
1068 PyGILState_Release(gilstate);
1069#endif
1070 return result;
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001071}
1072
Guido van Rossum05ac4492003-01-07 20:04:12 +00001073
Guido van Rossum290900a1997-09-26 21:51:21 +00001074/* Helper to initialize GNU readline properly. */
1075
1076static void
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001077setup_readline(readlinestate *mod_state)
Guido van Rossum290900a1997-09-26 21:51:21 +00001078{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +00001079#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1081 if (!saved_locale)
1082 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +00001083#endif
1084
R. David Murray52d1b4e2010-12-18 03:48:32 +00001085#ifdef __APPLE__
Victor Stinner6ced7c42011-03-21 18:15:42 +01001086 /* the libedit readline emulation resets key bindings etc
R. David Murray52d1b4e2010-12-18 03:48:32 +00001087 * when calling rl_initialize. So call it upfront
1088 */
1089 if (using_libedit_emulation)
1090 rl_initialize();
Ned Deilyf70f4a62013-09-06 15:16:19 -07001091
1092 /* Detect if libedit's readline emulation uses 0-based
1093 * indexing or 1-based indexing.
1094 */
1095 add_history("1");
1096 if (history_get(1) == NULL) {
1097 libedit_history_start = 0;
1098 } else {
1099 libedit_history_start = 1;
1100 }
1101 clear_history();
R. David Murray52d1b4e2010-12-18 03:48:32 +00001102#endif /* __APPLE__ */
1103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 using_history();
Skip Montanaroa0392742002-06-11 14:32:46 +00001105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 rl_readline_name = "python";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 /* Force rebind of TAB to insert-tab */
1108 rl_bind_key('\t', rl_insert);
1109 /* Bind both ESC-TAB and ESC-ESC to the completion function */
1110 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
1111 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin Panter5dbbf1a2016-04-03 02:54:58 +00001112#ifdef HAVE_RL_RESIZE_TERMINAL
1113 /* Set up signal handler for window resize */
1114 sigwinch_ohandler = PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
1115#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 /* Set our hook functions */
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001117 rl_startup_hook = on_startup_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001118#ifdef HAVE_RL_PRE_INPUT_HOOK
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001119 rl_pre_input_hook = on_pre_input_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001120#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 /* Set our completion function */
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001122 rl_attempted_completion_function = flex_complete;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 /* Set Python word break characters */
Antoine Pitroua7f7deb2013-05-06 21:51:03 +02001124 completer_word_break_characters =
1125 rl_completer_word_break_characters =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
1127 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001128
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001129 mod_state->begidx = PyLong_FromLong(0L);
1130 mod_state->endidx = PyLong_FromLong(0L);
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001131
Martin Panterc427b8d2016-08-27 03:23:11 +00001132#ifdef __APPLE__
1133 if (!using_libedit_emulation)
Victor Stinner92639cc2014-07-24 22:11:38 +02001134#endif
Martin Panterc427b8d2016-08-27 03:23:11 +00001135 {
1136 if (!isatty(STDOUT_FILENO)) {
1137 /* Issue #19884: stdout is not a terminal. Disable meta modifier
1138 keys to not write the ANSI sequence "\033[1034h" into stdout. On
1139 terminals supporting 8 bit characters like TERM=xterm-256color
1140 (which is now the default Fedora since Fedora 18), the meta key is
1141 used to enable support of 8 bit characters (ANSI sequence
1142 "\033[1034h").
1143
1144 With libedit, this call makes readline() crash. */
1145 rl_variable_bind ("enable-meta-key", "off");
1146 }
1147 }
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 /* Initialize (allows .inputrc to override)
1150 *
1151 * XXX: A bug in the readline-2.2 library causes a memory leak
1152 * inside this function. Nothing we can do about it.
1153 */
R. David Murray52d1b4e2010-12-18 03:48:32 +00001154#ifdef __APPLE__
1155 if (using_libedit_emulation)
Victor Stinner6ced7c42011-03-21 18:15:42 +01001156 rl_read_init_file(NULL);
R. David Murray52d1b4e2010-12-18 03:48:32 +00001157 else
1158#endif /* __APPLE__ */
1159 rl_initialize();
Victor Stinner6ced7c42011-03-21 18:15:42 +01001160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 RESTORE_LOCALE(saved_locale)
Guido van Rossum290900a1997-09-26 21:51:21 +00001162}
1163
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001164/* Wrapper around GNU readline that handles signals differently. */
1165
1166
1167#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
1168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169static char *completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001170static void
1171rlhandler(char *text)
1172{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 completed_input_string = text;
1174 rl_callback_handler_remove();
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001175}
1176
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001177static char *
Serhiy Storchakac6792272013-10-19 21:03:34 +03001178readline_until_enter_or_signal(const char *prompt, int *signal)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 char * not_done_reading = "";
1181 fd_set selectset;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 *signal = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001184#ifdef HAVE_RL_CATCH_SIGNAL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 rl_catch_signals = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001186#endif
1187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 rl_callback_handler_install (prompt, rlhandler);
1189 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 completed_input_string = not_done_reading;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 while (completed_input_string == not_done_reading) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001194 int has_input = 0, err = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 while (!has_input)
1197 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Michael W. Hudson8da2b012004-10-07 13:46:33 +00001198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 /* [Bug #1552726] Only limit the pause if an input hook has been
1200 defined. */
1201 struct timeval *timeoutp = NULL;
1202 if (PyOS_InputHook)
1203 timeoutp = &timeout;
Martin Panter5dbbf1a2016-04-03 02:54:58 +00001204#ifdef HAVE_RL_RESIZE_TERMINAL
1205 /* Update readline's view of the window size after SIGWINCH */
1206 if (sigwinch_received) {
1207 sigwinch_received = 0;
1208 rl_resize_terminal();
1209 }
1210#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 FD_SET(fileno(rl_instream), &selectset);
1212 /* select resets selectset if no input was available */
1213 has_input = select(fileno(rl_instream) + 1, &selectset,
1214 NULL, NULL, timeoutp);
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001215 err = errno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 if(PyOS_InputHook) PyOS_InputHook();
1217 }
1218
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001219 if (has_input > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 rl_callback_read_char();
1221 }
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001222 else if (err == EINTR) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001224#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001226#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001228#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001230#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 if (s < 0) {
1232 rl_free_line_state();
Martin Panterd6990d22016-03-22 07:24:05 +00001233#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0700
1234 rl_callback_sigcleanup();
1235#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 rl_cleanup_after_signal();
1237 rl_callback_handler_remove();
1238 *signal = 1;
1239 completed_input_string = NULL;
1240 }
1241 }
1242 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 return completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001245}
1246
1247
1248#else
Guido van Rossum290900a1997-09-26 21:51:21 +00001249
1250/* Interrupt handler */
1251
1252static jmp_buf jbuf;
1253
Guido van Rossum0969d361997-08-05 21:27:50 +00001254/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +00001255static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001256onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +00001257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +00001259}
1260
Guido van Rossum290900a1997-09-26 21:51:21 +00001261
Guido van Rossum0969d361997-08-05 21:27:50 +00001262static char *
Christian Heimesa3da7c52013-12-04 09:31:47 +01001263readline_until_enter_or_signal(const char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +00001264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 PyOS_sighandler_t old_inthandler;
1266 char *p;
Guido van Rossum74f31432003-01-07 20:01:29 +00001267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 *signal = 0;
1269
1270 old_inthandler = PyOS_setsig(SIGINT, onintr);
1271 if (setjmp(jbuf)) {
Guido van Rossum0969d361997-08-05 21:27:50 +00001272#ifdef HAVE_SIGRELSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
1274 sigrelse(SIGINT);
Guido van Rossum0969d361997-08-05 21:27:50 +00001275#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 PyOS_setsig(SIGINT, old_inthandler);
1277 *signal = 1;
1278 return NULL;
1279 }
1280 rl_event_hook = PyOS_InputHook;
1281 p = readline(prompt);
1282 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001283
1284 return p;
1285}
1286#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
1287
1288
1289static char *
Serhiy Storchakac6792272013-10-19 21:03:34 +03001290call_readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001291{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 size_t n;
1293 char *p, *q;
1294 int signal;
Neal Norwitz1fa040b2004-08-25 01:20:18 +00001295
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001296#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1298 if (!saved_locale)
1299 Py_FatalError("not enough memory to save locale");
Nadeem Vawda63752572013-02-02 20:05:11 +01001300 setlocale(LC_CTYPE, "");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001301#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
1304 rl_instream = sys_stdin;
1305 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001306#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001308#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 }
Guido van Rossum74f31432003-01-07 20:01:29 +00001310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 p = readline_until_enter_or_signal(prompt, &signal);
Guido van Rossumb18618d2000-05-03 23:44:39 +00001312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 /* we got an interrupt signal */
1314 if (signal) {
1315 RESTORE_LOCALE(saved_locale)
1316 return NULL;
1317 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001318
Martin Panter7462b6492015-11-02 03:37:02 +00001319 /* We got an EOF, return an empty string. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 if (p == NULL) {
Victor Stinner2fe9bac2013-10-10 16:18:20 +02001321 p = PyMem_RawMalloc(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 if (p != NULL)
1323 *p = '\0';
1324 RESTORE_LOCALE(saved_locale)
1325 return p;
1326 }
1327
1328 /* we have a valid line */
1329 n = strlen(p);
1330 if (n > 0) {
Brett Cannon2525dc82010-08-22 20:36:25 +00001331 const char *line;
Mark Dickinson6b54e1f2010-08-03 16:49:49 +00001332 int length = _py_get_history_length();
1333 if (length > 0)
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001334#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 if (using_libedit_emulation) {
Ned Deilyf70f4a62013-09-06 15:16:19 -07001336 /* handle older 0-based or newer 1-based indexing */
1337 line = (const char *)history_get(length + libedit_history_start - 1)->line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 } else
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001339#endif /* __APPLE__ */
Brett Cannon2525dc82010-08-22 20:36:25 +00001340 line = (const char *)history_get(length)->line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 else
1342 line = "";
1343 if (strcmp(p, line))
1344 add_history(p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 }
1346 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
1347 release the original. */
1348 q = p;
Victor Stinner2fe9bac2013-10-10 16:18:20 +02001349 p = PyMem_RawMalloc(n+2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 if (p != NULL) {
1351 strncpy(p, q, n);
1352 p[n] = '\n';
1353 p[n+1] = '\0';
1354 }
1355 free(q);
1356 RESTORE_LOCALE(saved_locale)
1357 return p;
Guido van Rossum0969d361997-08-05 21:27:50 +00001358}
1359
Guido van Rossum290900a1997-09-26 21:51:21 +00001360
1361/* Initialize the module */
1362
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001363PyDoc_STRVAR(doc_module,
1364"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001365
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001366#ifdef __APPLE__
1367PyDoc_STRVAR(doc_module_le,
1368"Importing this module enables command line editing using libedit readline.");
1369#endif /* __APPLE__ */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001370
1371static struct PyModuleDef readlinemodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 PyModuleDef_HEAD_INIT,
1373 "readline",
1374 doc_module,
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001375 sizeof(readlinestate),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 readline_methods,
1377 NULL,
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001378 readline_traverse,
1379 readline_clear,
1380 readline_free
Martin v. Löwis1a214512008-06-11 05:26:20 +00001381};
1382
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001383
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001384PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001385PyInit_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001386{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 PyObject *m;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001388 readlinestate *mod_state;
Guido van Rossum290900a1997-09-26 21:51:21 +00001389
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001390#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
1392 using_libedit_emulation = 1;
1393 }
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 if (using_libedit_emulation)
1396 readlinemodule.m_doc = doc_module_le;
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001397
1398#endif /* __APPLE__ */
1399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 m = PyModule_Create(&readlinemodule);
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 if (m == NULL)
1403 return NULL;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001404
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001405 mod_state = (readlinestate *) PyModule_GetState(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 PyOS_ReadlineFunctionPointer = call_readline;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001407 setup_readline(mod_state);
Antoine Pitrou7e8b8672014-11-04 14:52:10 +01001408
1409 PyModule_AddIntConstant(m, "_READLINE_VERSION", RL_READLINE_VERSION);
1410 PyModule_AddIntConstant(m, "_READLINE_RUNTIME_VERSION", rl_readline_version);
1411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 return m;
Guido van Rossum0969d361997-08-05 21:27:50 +00001413}