blob: 47f1b13c9e8e3a35bd92d26f0344a9edb2feab4a [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
Guido van Rossum290900a1997-09-26 21:51:21 +0000131/* Exported function to send one line to readline's init file parser */
132
133static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000134parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000135{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 char *s, *copy;
137 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
138 return NULL;
139 /* Make a copy -- rl_parse_and_bind() modifies its argument */
140 /* Bernard Herzog */
Victor Stinnerb6404912013-07-07 16:21:41 +0200141 copy = PyMem_Malloc(1 + strlen(s));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 if (copy == NULL)
143 return PyErr_NoMemory();
144 strcpy(copy, s);
145 rl_parse_and_bind(copy);
Victor Stinnerb6404912013-07-07 16:21:41 +0200146 PyMem_Free(copy); /* Free the copy */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +0000148}
149
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000150PyDoc_STRVAR(doc_parse_and_bind,
151"parse_and_bind(string) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000152Execute the init line provided in the string argument.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000153
154
155/* Exported function to parse a readline init file */
156
157static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000158read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000159{
Victor Stinner19e65a32010-06-11 22:27:14 +0000160 PyObject *filename_obj = Py_None, *filename_bytes;
161 if (!PyArg_ParseTuple(args, "|O:read_init_file", &filename_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 return NULL;
Victor Stinner19e65a32010-06-11 22:27:14 +0000163 if (filename_obj != Py_None) {
164 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
165 return NULL;
166 errno = rl_read_init_file(PyBytes_AsString(filename_bytes));
167 Py_DECREF(filename_bytes);
168 } else
169 errno = rl_read_init_file(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 if (errno)
171 return PyErr_SetFromErrno(PyExc_IOError);
172 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +0000173}
174
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000175PyDoc_STRVAR(doc_read_init_file,
176"read_init_file([filename]) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000177Execute a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000178The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000179
180
Skip Montanaro28067822000-07-06 18:55:12 +0000181/* Exported function to load a readline history file */
182
183static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000184read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000185{
Victor Stinner19e65a32010-06-11 22:27:14 +0000186 PyObject *filename_obj = Py_None, *filename_bytes;
187 if (!PyArg_ParseTuple(args, "|O:read_history_file", &filename_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 return NULL;
Victor Stinner19e65a32010-06-11 22:27:14 +0000189 if (filename_obj != Py_None) {
190 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
191 return NULL;
192 errno = read_history(PyBytes_AsString(filename_bytes));
193 Py_DECREF(filename_bytes);
194 } else
195 errno = read_history(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 if (errno)
197 return PyErr_SetFromErrno(PyExc_IOError);
198 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000199}
200
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000201static int _history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000202PyDoc_STRVAR(doc_read_history_file,
203"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000204Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000205The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000206
207
208/* Exported function to save a readline history file */
209
210static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000211write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000212{
Victor Stinner19e65a32010-06-11 22:27:14 +0000213 PyObject *filename_obj = Py_None, *filename_bytes;
214 char *filename;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100215 int err;
Victor Stinner19e65a32010-06-11 22:27:14 +0000216 if (!PyArg_ParseTuple(args, "|O:write_history_file", &filename_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 return NULL;
Victor Stinner19e65a32010-06-11 22:27:14 +0000218 if (filename_obj != Py_None) {
219 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
220 return NULL;
221 filename = PyBytes_AsString(filename_bytes);
222 } else {
223 filename_bytes = NULL;
224 filename = NULL;
225 }
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100226 errno = err = write_history(filename);
227 if (!err && _history_length >= 0)
Victor Stinner19e65a32010-06-11 22:27:14 +0000228 history_truncate_file(filename, _history_length);
229 Py_XDECREF(filename_bytes);
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100230 errno = err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 if (errno)
232 return PyErr_SetFromErrno(PyExc_IOError);
233 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000234}
235
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000236PyDoc_STRVAR(doc_write_history_file,
237"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000238Save a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000239The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000240
241
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600242#ifdef HAVE_RL_APPEND_HISTORY
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600243/* Exported function to save part of a readline history file */
244
245static PyObject *
246append_history_file(PyObject *self, PyObject *args)
247{
248 int nelements;
249 PyObject *filename_obj = Py_None, *filename_bytes;
250 char *filename;
251 int err;
252 if (!PyArg_ParseTuple(args, "i|O:append_history_file", &nelements, &filename_obj))
253 return NULL;
254 if (filename_obj != Py_None) {
255 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
256 return NULL;
257 filename = PyBytes_AsString(filename_bytes);
258 } else {
259 filename_bytes = NULL;
260 filename = NULL;
261 }
262 errno = err = append_history(nelements, filename);
263 if (!err && _history_length >= 0)
264 history_truncate_file(filename, _history_length);
265 Py_XDECREF(filename_bytes);
266 errno = err;
267 if (errno)
268 return PyErr_SetFromErrno(PyExc_IOError);
269 Py_RETURN_NONE;
270}
271
272PyDoc_STRVAR(doc_append_history_file,
273"append_history_file(nelements[, filename]) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000274Append the last nelements items of the history list to file.\n\
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600275The default filename is ~/.history.");
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600276#endif
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600277
278
Guido van Rossum74f31432003-01-07 20:01:29 +0000279/* Set history length */
280
281static PyObject*
282set_history_length(PyObject *self, PyObject *args)
283{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 int length = _history_length;
285 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
286 return NULL;
287 _history_length = length;
288 Py_RETURN_NONE;
Guido van Rossum74f31432003-01-07 20:01:29 +0000289}
290
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000291PyDoc_STRVAR(set_history_length_doc,
292"set_history_length(length) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000293set the maximal number of lines which will be written to\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000294the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000295history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000296
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000297
Guido van Rossum74f31432003-01-07 20:01:29 +0000298/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000299
300static PyObject*
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000301get_history_length(PyObject *self, PyObject *noarg)
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000302{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 return PyLong_FromLong(_history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000304}
305
Guido van Rossum74f31432003-01-07 20:01:29 +0000306PyDoc_STRVAR(get_history_length_doc,
307"get_history_length() -> int\n\
Martin Panter0f767392016-04-05 07:37:22 +0000308return the maximum number of lines that will be written to\n\
Guido van Rossum74f31432003-01-07 20:01:29 +0000309the history file.");
310
311
Martin v. Löwis0daad592001-09-30 21:09:59 +0000312/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000313
Martin v. Löwis0daad592001-09-30 21:09:59 +0000314static PyObject *
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000315set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000316{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 PyObject *function = Py_None;
318 char buf[80];
319 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
320 if (!PyArg_ParseTuple(args, buf, &function))
321 return NULL;
322 if (function == Py_None) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200323 Py_CLEAR(*hook_var);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 }
325 else if (PyCallable_Check(function)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 Py_INCREF(function);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300327 Py_XSETREF(*hook_var, function);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 }
329 else {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100330 PyErr_Format(PyExc_TypeError,
331 "set_%.50s(func): argument not callable",
332 funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 return NULL;
334 }
335 Py_RETURN_NONE;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000336}
337
Guido van Rossum74f31432003-01-07 20:01:29 +0000338
Martin v. Löwis0daad592001-09-30 21:09:59 +0000339static PyObject *
Thomas Wouters89d996e2007-09-08 17:39:28 +0000340set_completion_display_matches_hook(PyObject *self, PyObject *args)
341{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 PyObject *result = set_hook("completion_display_matches_hook",
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200343 &readlinestate_global->completion_display_matches_hook, args);
Christian Heimes32fbe592007-11-12 15:01:33 +0000344#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 /* We cannot set this hook globally, since it replaces the
346 default completion display. */
347 rl_completion_display_matches_hook =
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200348 readlinestate_global->completion_display_matches_hook ?
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000349#if defined(_RL_FUNCTION_TYPEDEF)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000351#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 (VFunction *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000353#endif
Christian Heimes32fbe592007-11-12 15:01:33 +0000354#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 return result;
Christian Heimes32fbe592007-11-12 15:01:33 +0000356
Thomas Wouters89d996e2007-09-08 17:39:28 +0000357}
358
359PyDoc_STRVAR(doc_set_completion_display_matches_hook,
360"set_completion_display_matches_hook([function]) -> None\n\
361Set or remove the completion display function.\n\
362The function is called as\n\
363 function(substitution, [matches], longest_match_length)\n\
364once each time matches need to be displayed.");
365
366static PyObject *
Martin v. Löwis0daad592001-09-30 21:09:59 +0000367set_startup_hook(PyObject *self, PyObject *args)
368{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200369 return set_hook("startup_hook", &readlinestate_global->startup_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000370}
371
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000372PyDoc_STRVAR(doc_set_startup_hook,
373"set_startup_hook([function]) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000374Set or remove the function invoked by the rl_startup_hook callback.\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000375The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000376before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000377
Guido van Rossum74f31432003-01-07 20:01:29 +0000378
Martin v. Löwis0daad592001-09-30 21:09:59 +0000379#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000380
381/* Set pre-input hook */
382
Martin v. Löwis0daad592001-09-30 21:09:59 +0000383static PyObject *
384set_pre_input_hook(PyObject *self, PyObject *args)
385{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200386 return set_hook("pre_input_hook", &readlinestate_global->pre_input_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000387}
388
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000389PyDoc_STRVAR(doc_set_pre_input_hook,
390"set_pre_input_hook([function]) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000391Set or remove the function invoked by the rl_pre_input_hook callback.\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000392The function is called with no arguments after the first prompt\n\
393has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000394characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000395
Martin v. Löwis0daad592001-09-30 21:09:59 +0000396#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000397
Guido van Rossum74f31432003-01-07 20:01:29 +0000398
Thomas Wouters89d996e2007-09-08 17:39:28 +0000399/* Get the completion type for the scope of the tab-completion */
400static PyObject *
401get_completion_type(PyObject *self, PyObject *noarg)
402{
Christian Heimes217cfd12007-12-02 14:31:20 +0000403 return PyLong_FromLong(rl_completion_type);
Thomas Wouters89d996e2007-09-08 17:39:28 +0000404}
405
406PyDoc_STRVAR(doc_get_completion_type,
407"get_completion_type() -> int\n\
408Get the type of completion being attempted.");
409
410
Guido van Rossum74f31432003-01-07 20:01:29 +0000411/* Get the beginning index for the scope of the tab-completion */
412
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000413static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000414get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000415{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200416 Py_INCREF(readlinestate_global->begidx);
417 return readlinestate_global->begidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000418}
419
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000420PyDoc_STRVAR(doc_get_begidx,
421"get_begidx() -> int\n\
Martin Panter0f767392016-04-05 07:37:22 +0000422get the beginning index of the completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000423
Guido van Rossum74f31432003-01-07 20:01:29 +0000424
425/* Get the ending index for the scope of the tab-completion */
426
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000427static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000428get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000429{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200430 Py_INCREF(readlinestate_global->endidx);
431 return readlinestate_global->endidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000432}
433
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000434PyDoc_STRVAR(doc_get_endidx,
435"get_endidx() -> int\n\
Martin Panter0f767392016-04-05 07:37:22 +0000436get the ending index of the completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000437
438
Guido van Rossum74f31432003-01-07 20:01:29 +0000439/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000440
441static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000442set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000443{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 char *break_chars;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000445
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200446 if (!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 return NULL;
448 }
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200449 /* Keep a reference to the allocated memory in the module state in case
450 some other module modifies rl_completer_word_break_characters
451 (see issue #17289). */
Serhiy Storchaka11384392015-09-27 22:34:59 +0300452 break_chars = strdup(break_chars);
453 if (break_chars) {
454 free(completer_word_break_characters);
455 completer_word_break_characters = break_chars;
456 rl_completer_word_break_characters = break_chars;
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200457 Py_RETURN_NONE;
458 }
459 else
460 return PyErr_NoMemory();
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000461}
462
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000463PyDoc_STRVAR(doc_set_completer_delims,
464"set_completer_delims(string) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000465set the word delimiters for completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000466
Mark Dickinson29b238e2010-08-03 16:08:16 +0000467/* _py_free_history_entry: Utility function to free a history entry. */
468
469#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
470
471/* Readline version >= 5.0 introduced a timestamp field into the history entry
472 structure; this needs to be freed to avoid a memory leak. This version of
473 readline also introduced the handy 'free_history_entry' function, which
474 takes care of the timestamp. */
475
476static void
477_py_free_history_entry(HIST_ENTRY *entry)
478{
479 histdata_t data = free_history_entry(entry);
480 free(data);
481}
482
483#else
484
485/* No free_history_entry function; free everything manually. */
486
487static void
488_py_free_history_entry(HIST_ENTRY *entry)
489{
490 if (entry->line)
491 free((void *)entry->line);
492 if (entry->data)
493 free(entry->data);
494 free(entry);
495}
496
497#endif
498
Skip Montanaroe5069012004-08-15 14:32:06 +0000499static PyObject *
500py_remove_history(PyObject *self, PyObject *args)
501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 int entry_number;
503 HIST_ENTRY *entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000504
Martin Panter0f767392016-04-05 07:37:22 +0000505 if (!PyArg_ParseTuple(args, "i:remove_history_item", &entry_number))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 return NULL;
507 if (entry_number < 0) {
508 PyErr_SetString(PyExc_ValueError,
509 "History index cannot be negative");
510 return NULL;
511 }
512 entry = remove_history(entry_number);
513 if (!entry) {
514 PyErr_Format(PyExc_ValueError,
515 "No history item at position %d",
516 entry_number);
517 return NULL;
518 }
519 /* free memory allocated for the history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000520 _py_free_history_entry(entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000522}
523
524PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000525"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000526remove history item given by its position");
527
528static PyObject *
529py_replace_history(PyObject *self, PyObject *args)
530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 int entry_number;
532 char *line;
533 HIST_ENTRY *old_entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000534
Martin Panter0f767392016-04-05 07:37:22 +0000535 if (!PyArg_ParseTuple(args, "is:replace_history_item", &entry_number,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 &line)) {
537 return NULL;
538 }
539 if (entry_number < 0) {
540 PyErr_SetString(PyExc_ValueError,
541 "History index cannot be negative");
542 return NULL;
543 }
544 old_entry = replace_history_entry(entry_number, line, (void *)NULL);
545 if (!old_entry) {
546 PyErr_Format(PyExc_ValueError,
547 "No history item at position %d",
548 entry_number);
549 return NULL;
550 }
551 /* free memory allocated for the old history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000552 _py_free_history_entry(old_entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000554}
555
556PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000557"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000558replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000559
560/* Add a line to the history buffer */
561
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000562static PyObject *
563py_add_history(PyObject *self, PyObject *args)
564{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 char *line;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
568 return NULL;
569 }
570 add_history(line);
571 Py_RETURN_NONE;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000572}
573
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000574PyDoc_STRVAR(doc_add_history,
575"add_history(string) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000576add an item to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000577
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000578static int should_auto_add_history = 1;
579
580/* Enable or disable automatic history */
581
582static PyObject *
583py_set_auto_history(PyObject *self, PyObject *args)
584{
585 if (!PyArg_ParseTuple(args, "p:set_auto_history",
586 &should_auto_add_history)) {
587 return NULL;
588 }
589 Py_RETURN_NONE;
590}
591
592PyDoc_STRVAR(doc_set_auto_history,
593"set_auto_history(enabled) -> None\n\
594Enables or disables automatic history.");
595
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000596
Guido van Rossum74f31432003-01-07 20:01:29 +0000597/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000598
599static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000600get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 return PyUnicode_FromString(rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000603}
Guido van Rossum74f31432003-01-07 20:01:29 +0000604
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000605PyDoc_STRVAR(doc_get_completer_delims,
606"get_completer_delims() -> string\n\
Martin Panter0f767392016-04-05 07:37:22 +0000607get the word delimiters for completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000608
Guido van Rossum74f31432003-01-07 20:01:29 +0000609
610/* Set the completer function */
611
Guido van Rossum290900a1997-09-26 21:51:21 +0000612static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000613set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000614{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200615 return set_hook("completer", &readlinestate_global->completer, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000616}
617
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000618PyDoc_STRVAR(doc_set_completer,
619"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000620Set or remove the completer function.\n\
621The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000622for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000623It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000624
Guido van Rossum74f31432003-01-07 20:01:29 +0000625
Michael W. Hudson796df152003-01-30 10:12:51 +0000626static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000627get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000628{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200629 if (readlinestate_global->completer == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 Py_RETURN_NONE;
631 }
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200632 Py_INCREF(readlinestate_global->completer);
633 return readlinestate_global->completer;
Michael W. Hudson796df152003-01-30 10:12:51 +0000634}
635
636PyDoc_STRVAR(doc_get_completer,
637"get_completer() -> function\n\
638\n\
639Returns current completer function.");
640
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000641/* Private function to get current length of history. XXX It may be
642 * possible to replace this with a direct use of history_length instead,
643 * but it's not clear whether BSD's libedit keeps history_length up to date.
644 * See issue #8065.*/
645
646static int
647_py_get_history_length(void)
648{
649 HISTORY_STATE *hist_st = history_get_history_state();
650 int length = hist_st->length;
651 /* the history docs don't say so, but the address of hist_st changes each
652 time history_get_history_state is called which makes me think it's
653 freshly malloc'd memory... on the other hand, the address of the last
654 line stays the same as long as history isn't extended, so it appears to
655 be malloc'd but managed by the history package... */
656 free(hist_st);
657 return length;
658}
659
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000660/* Exported function to get any element of history */
661
662static PyObject *
663get_history_item(PyObject *self, PyObject *args)
664{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 int idx = 0;
666 HIST_ENTRY *hist_ent;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000667
Martin Panter0f767392016-04-05 07:37:22 +0000668 if (!PyArg_ParseTuple(args, "i:get_history_item", &idx))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 return NULL;
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000670#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 if (using_libedit_emulation) {
Ned Deilyf70f4a62013-09-06 15:16:19 -0700672 /* Older versions of libedit's readline emulation
673 * use 0-based indexes, while readline and newer
674 * versions of libedit use 1-based indexes.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 */
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000676 int length = _py_get_history_length();
Ned Deilyf70f4a62013-09-06 15:16:19 -0700677
678 idx = idx - 1 + libedit_history_start;
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 /*
681 * Apple's readline emulation crashes when
682 * the index is out of range, therefore
683 * test for that and fail gracefully.
684 */
Ned Deilyf70f4a62013-09-06 15:16:19 -0700685 if (idx < (0 + libedit_history_start)
686 || idx >= (length + libedit_history_start)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 Py_RETURN_NONE;
688 }
689 }
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000690#endif /* __APPLE__ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 if ((hist_ent = history_get(idx)))
692 return PyUnicode_FromString(hist_ent->line);
693 else {
694 Py_RETURN_NONE;
695 }
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000696}
697
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000698PyDoc_STRVAR(doc_get_history_item,
699"get_history_item() -> string\n\
700return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000701
Guido van Rossum74f31432003-01-07 20:01:29 +0000702
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000703/* Exported function to get current length of history */
704
705static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000706get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000707{
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000708 return PyLong_FromLong((long)_py_get_history_length());
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000709}
710
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000711PyDoc_STRVAR(doc_get_current_history_length,
712"get_current_history_length() -> integer\n\
713return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000714
Guido van Rossum74f31432003-01-07 20:01:29 +0000715
Guido van Rossum79378ff1997-10-07 14:53:21 +0000716/* Exported function to read the current line buffer */
717
718static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000719get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000720{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 return PyUnicode_FromString(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000722}
723
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000724PyDoc_STRVAR(doc_get_line_buffer,
725"get_line_buffer() -> string\n\
726return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000727
Guido van Rossum74f31432003-01-07 20:01:29 +0000728
Martin v. Löwise7a97962003-09-20 16:08:33 +0000729#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
730
731/* Exported function to clear the current history */
732
733static PyObject *
734py_clear_history(PyObject *self, PyObject *noarg)
735{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 clear_history();
737 Py_RETURN_NONE;
Martin v. Löwise7a97962003-09-20 16:08:33 +0000738}
739
740PyDoc_STRVAR(doc_clear_history,
741"clear_history() -> None\n\
742Clear the current readline history.");
743#endif
744
745
Guido van Rossum79378ff1997-10-07 14:53:21 +0000746/* Exported function to insert text into the line buffer */
747
748static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000749insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 char *s;
752 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
753 return NULL;
754 rl_insert_text(s);
755 Py_RETURN_NONE;
Guido van Rossum79378ff1997-10-07 14:53:21 +0000756}
757
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000758PyDoc_STRVAR(doc_insert_text,
759"insert_text(string) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000760Insert text into the line buffer at the cursor position.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000761
Guido van Rossum74f31432003-01-07 20:01:29 +0000762
763/* Redisplay the line buffer */
764
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000765static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000766redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000767{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 rl_redisplay();
769 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000770}
771
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000772PyDoc_STRVAR(doc_redisplay,
773"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000774Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000775contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000776
Guido van Rossum74f31432003-01-07 20:01:29 +0000777
Guido van Rossum290900a1997-09-26 21:51:21 +0000778/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000779
780static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000781{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
783 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
784 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
785 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
786 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
787 {"read_history_file", read_history_file,
788 METH_VARARGS, doc_read_history_file},
789 {"write_history_file", write_history_file,
790 METH_VARARGS, doc_write_history_file},
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600791#ifdef HAVE_RL_APPEND_HISTORY
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600792 {"append_history_file", append_history_file,
793 METH_VARARGS, doc_append_history_file},
Ned Deily8007cbc2014-11-26 13:02:33 -0800794#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 {"get_history_item", get_history_item,
796 METH_VARARGS, doc_get_history_item},
797 {"get_current_history_length", (PyCFunction)get_current_history_length,
798 METH_NOARGS, doc_get_current_history_length},
799 {"set_history_length", set_history_length,
800 METH_VARARGS, set_history_length_doc},
801 {"get_history_length", get_history_length,
802 METH_NOARGS, get_history_length_doc},
803 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
804 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
805 {"get_completion_type", get_completion_type,
806 METH_NOARGS, doc_get_completion_type},
807 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
808 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 {"set_completer_delims", set_completer_delims,
811 METH_VARARGS, doc_set_completer_delims},
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000812 {"set_auto_history", py_set_auto_history, METH_VARARGS, doc_set_auto_history},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
814 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
815 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
816 {"get_completer_delims", get_completer_delims,
817 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 {"set_completion_display_matches_hook", set_completion_display_matches_hook,
820 METH_VARARGS, doc_set_completion_display_matches_hook},
821 {"set_startup_hook", set_startup_hook,
822 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000823#ifdef HAVE_RL_PRE_INPUT_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 {"set_pre_input_hook", set_pre_input_hook,
825 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000826#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000827#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
Martin v. Löwise7a97962003-09-20 16:08:33 +0000829#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000831};
832
Guido van Rossum05ac4492003-01-07 20:04:12 +0000833
Martin v. Löwis0daad592001-09-30 21:09:59 +0000834/* C function to call the Python hooks. */
835
836static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000837on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000838{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 int result = 0;
840 if (func != NULL) {
841 PyObject *r;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 r = PyObject_CallFunction(func, NULL);
843 if (r == NULL)
844 goto error;
845 if (r == Py_None)
846 result = 0;
847 else {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300848 result = _PyLong_AsInt(r);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 if (result == -1 && PyErr_Occurred())
850 goto error;
851 }
852 Py_DECREF(r);
853 goto done;
854 error:
855 PyErr_Clear();
856 Py_XDECREF(r);
857 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 return result;
859 }
860 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000861}
862
863static int
Ned Deily7b9ddea2014-02-05 16:53:10 -0800864#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000865on_startup_hook(void)
Ned Deily7b9ddea2014-02-05 16:53:10 -0800866#else
867on_startup_hook()
868#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000869{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200870 int r;
871#ifdef WITH_THREAD
872 PyGILState_STATE gilstate = PyGILState_Ensure();
873#endif
874 r = on_hook(readlinestate_global->startup_hook);
875#ifdef WITH_THREAD
876 PyGILState_Release(gilstate);
877#endif
878 return r;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000879}
880
881#ifdef HAVE_RL_PRE_INPUT_HOOK
882static int
Ned Deily7b9ddea2014-02-05 16:53:10 -0800883#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000884on_pre_input_hook(void)
Ned Deily7b9ddea2014-02-05 16:53:10 -0800885#else
886on_pre_input_hook()
887#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000888{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200889 int r;
890#ifdef WITH_THREAD
891 PyGILState_STATE gilstate = PyGILState_Ensure();
892#endif
893 r = on_hook(readlinestate_global->pre_input_hook);
894#ifdef WITH_THREAD
895 PyGILState_Release(gilstate);
896#endif
897 return r;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000898}
899#endif
900
Guido van Rossum05ac4492003-01-07 20:04:12 +0000901
Thomas Wouters89d996e2007-09-08 17:39:28 +0000902/* C function to call the Python completion_display_matches */
903
Georg Brandl646fdd62010-10-18 07:27:55 +0000904#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Thomas Wouters89d996e2007-09-08 17:39:28 +0000905static void
906on_completion_display_matches_hook(char **matches,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 int num_matches, int max_length)
Thomas Wouters89d996e2007-09-08 17:39:28 +0000908{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 int i;
910 PyObject *m=NULL, *s=NULL, *r=NULL;
Christian Heimesaec75c32007-11-11 22:42:36 +0000911#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 PyGILState_STATE gilstate = PyGILState_Ensure();
Thomas Wouters89d996e2007-09-08 17:39:28 +0000913#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 m = PyList_New(num_matches);
915 if (m == NULL)
916 goto error;
917 for (i = 0; i < num_matches; i++) {
918 s = PyUnicode_FromString(matches[i+1]);
919 if (s == NULL)
920 goto error;
921 if (PyList_SetItem(m, i, s) == -1)
922 goto error;
923 }
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200924 r = PyObject_CallFunction(readlinestate_global->completion_display_matches_hook,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 "sOi", matches[0], m, max_length);
Christian Heimes32fbe592007-11-12 15:01:33 +0000926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 Py_DECREF(m); m=NULL;
Christian Heimes32fbe592007-11-12 15:01:33 +0000928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 if (r == NULL ||
930 (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) {
931 goto error;
932 }
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200933 Py_CLEAR(r);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934
935 if (0) {
936 error:
937 PyErr_Clear();
938 Py_XDECREF(m);
939 Py_XDECREF(r);
940 }
Christian Heimes32fbe592007-11-12 15:01:33 +0000941#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 PyGILState_Release(gilstate);
Christian Heimes32fbe592007-11-12 15:01:33 +0000943#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000944}
945
Senthil Kumaran95c07002010-11-04 03:51:05 +0000946#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000947
Martin Panter5dbbf1a2016-04-03 02:54:58 +0000948#ifdef HAVE_RL_RESIZE_TERMINAL
949static volatile sig_atomic_t sigwinch_received;
Martin Panter28f35b22016-04-03 08:00:49 +0000950static PyOS_sighandler_t sigwinch_ohandler;
Martin Panter5dbbf1a2016-04-03 02:54:58 +0000951
952static void
953readline_sigwinch_handler(int signum)
954{
955 sigwinch_received = 1;
956 if (sigwinch_ohandler &&
957 sigwinch_ohandler != SIG_IGN && sigwinch_ohandler != SIG_DFL)
958 sigwinch_ohandler(signum);
959
960#ifndef HAVE_SIGACTION
961 /* If the handler was installed with signal() rather than sigaction(),
962 we need to reinstall it. */
963 PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
964#endif
965}
966#endif
967
Guido van Rossum290900a1997-09-26 21:51:21 +0000968/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000969
Guido van Rossum290900a1997-09-26 21:51:21 +0000970static char *
Guido van Rossumd8faa362007-04-27 19:54:29 +0000971on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000972{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 char *result = NULL;
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200974 if (readlinestate_global->completer != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 PyObject *r;
Christian Heimesaec75c32007-11-11 22:42:36 +0000976#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000978#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 rl_attempted_completion_over = 1;
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200980 r = PyObject_CallFunction(readlinestate_global->completer, "si", text, state);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 if (r == NULL)
982 goto error;
983 if (r == Py_None) {
984 result = NULL;
985 }
986 else {
987 char *s = _PyUnicode_AsString(r);
988 if (s == NULL)
989 goto error;
990 result = strdup(s);
991 }
992 Py_DECREF(r);
993 goto done;
994 error:
995 PyErr_Clear();
996 Py_XDECREF(r);
997 done:
Christian Heimesaec75c32007-11-11 22:42:36 +0000998#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 PyGILState_Release(gilstate);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +00001000#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 return result;
1002 }
1003 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +00001004}
1005
Guido van Rossum290900a1997-09-26 21:51:21 +00001006
Guido van Rossum6d0d3652003-01-07 20:34:19 +00001007/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001008 * before calling the normal completer */
1009
Neal Norwitzc355f0c2003-02-21 00:30:18 +00001010static char **
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001011flex_complete(const char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001012{
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001013 char **result;
1014#ifdef WITH_THREAD
1015 PyGILState_STATE gilstate = PyGILState_Ensure();
1016#endif
Antoine Pitroudc0900b2009-10-19 18:22:37 +00001017#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 rl_completion_append_character ='\0';
Antoine Pitroud5131772009-10-26 19:22:14 +00001019#endif
1020#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 rl_completion_suppress_append = 0;
Antoine Pitroudc0900b2009-10-19 18:22:37 +00001022#endif
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001023 Py_XDECREF(readlinestate_global->begidx);
1024 Py_XDECREF(readlinestate_global->endidx);
1025 readlinestate_global->begidx = PyLong_FromLong((long) start);
1026 readlinestate_global->endidx = PyLong_FromLong((long) end);
1027 result = completion_matches(text, *on_completion);
1028#ifdef WITH_THREAD
1029 PyGILState_Release(gilstate);
1030#endif
1031 return result;
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001032}
1033
Guido van Rossum05ac4492003-01-07 20:04:12 +00001034
Guido van Rossum290900a1997-09-26 21:51:21 +00001035/* Helper to initialize GNU readline properly. */
1036
1037static void
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001038setup_readline(readlinestate *mod_state)
Guido van Rossum290900a1997-09-26 21:51:21 +00001039{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +00001040#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1042 if (!saved_locale)
1043 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +00001044#endif
1045
R. David Murray52d1b4e2010-12-18 03:48:32 +00001046#ifdef __APPLE__
Victor Stinner6ced7c42011-03-21 18:15:42 +01001047 /* the libedit readline emulation resets key bindings etc
R. David Murray52d1b4e2010-12-18 03:48:32 +00001048 * when calling rl_initialize. So call it upfront
1049 */
1050 if (using_libedit_emulation)
1051 rl_initialize();
Ned Deilyf70f4a62013-09-06 15:16:19 -07001052
1053 /* Detect if libedit's readline emulation uses 0-based
1054 * indexing or 1-based indexing.
1055 */
1056 add_history("1");
1057 if (history_get(1) == NULL) {
1058 libedit_history_start = 0;
1059 } else {
1060 libedit_history_start = 1;
1061 }
1062 clear_history();
R. David Murray52d1b4e2010-12-18 03:48:32 +00001063#endif /* __APPLE__ */
1064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 using_history();
Skip Montanaroa0392742002-06-11 14:32:46 +00001066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 rl_readline_name = "python";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 /* Force rebind of TAB to insert-tab */
1069 rl_bind_key('\t', rl_insert);
1070 /* Bind both ESC-TAB and ESC-ESC to the completion function */
1071 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
1072 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin Panter5dbbf1a2016-04-03 02:54:58 +00001073#ifdef HAVE_RL_RESIZE_TERMINAL
1074 /* Set up signal handler for window resize */
1075 sigwinch_ohandler = PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
1076#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 /* Set our hook functions */
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001078 rl_startup_hook = on_startup_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001079#ifdef HAVE_RL_PRE_INPUT_HOOK
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001080 rl_pre_input_hook = on_pre_input_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001081#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 /* Set our completion function */
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001083 rl_attempted_completion_function = flex_complete;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 /* Set Python word break characters */
Antoine Pitroua7f7deb2013-05-06 21:51:03 +02001085 completer_word_break_characters =
1086 rl_completer_word_break_characters =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
1088 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001089
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001090 mod_state->begidx = PyLong_FromLong(0L);
1091 mod_state->endidx = PyLong_FromLong(0L);
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001092
Victor Stinner92639cc2014-07-24 22:11:38 +02001093#ifndef __APPLE__
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001094 if (!isatty(STDOUT_FILENO)) {
Martin Pantere26da7c2016-06-02 10:07:09 +00001095 /* Issue #19884: stdout is not a terminal. Disable meta modifier
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001096 keys to not write the ANSI sequence "\033[1034h" into stdout. On
1097 terminals supporting 8 bit characters like TERM=xterm-256color
1098 (which is now the default Fedora since Fedora 18), the meta key is
1099 used to enable support of 8 bit characters (ANSI sequence
Victor Stinner92639cc2014-07-24 22:11:38 +02001100 "\033[1034h").
1101
1102 With libedit, this call makes readline() crash. */
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001103 rl_variable_bind ("enable-meta-key", "off");
1104 }
Victor Stinner92639cc2014-07-24 22:11:38 +02001105#endif
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 /* Initialize (allows .inputrc to override)
1108 *
1109 * XXX: A bug in the readline-2.2 library causes a memory leak
1110 * inside this function. Nothing we can do about it.
1111 */
R. David Murray52d1b4e2010-12-18 03:48:32 +00001112#ifdef __APPLE__
1113 if (using_libedit_emulation)
Victor Stinner6ced7c42011-03-21 18:15:42 +01001114 rl_read_init_file(NULL);
R. David Murray52d1b4e2010-12-18 03:48:32 +00001115 else
1116#endif /* __APPLE__ */
1117 rl_initialize();
Victor Stinner6ced7c42011-03-21 18:15:42 +01001118
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 RESTORE_LOCALE(saved_locale)
Guido van Rossum290900a1997-09-26 21:51:21 +00001120}
1121
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001122/* Wrapper around GNU readline that handles signals differently. */
1123
1124
1125#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
1126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127static char *completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001128static void
1129rlhandler(char *text)
1130{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 completed_input_string = text;
1132 rl_callback_handler_remove();
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001133}
1134
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001135static char *
Serhiy Storchakac6792272013-10-19 21:03:34 +03001136readline_until_enter_or_signal(const char *prompt, int *signal)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001137{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 char * not_done_reading = "";
1139 fd_set selectset;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 *signal = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001142#ifdef HAVE_RL_CATCH_SIGNAL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 rl_catch_signals = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001144#endif
1145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 rl_callback_handler_install (prompt, rlhandler);
1147 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 completed_input_string = not_done_reading;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 while (completed_input_string == not_done_reading) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001152 int has_input = 0, err = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 while (!has_input)
1155 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Michael W. Hudson8da2b012004-10-07 13:46:33 +00001156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 /* [Bug #1552726] Only limit the pause if an input hook has been
1158 defined. */
1159 struct timeval *timeoutp = NULL;
1160 if (PyOS_InputHook)
1161 timeoutp = &timeout;
Martin Panter5dbbf1a2016-04-03 02:54:58 +00001162#ifdef HAVE_RL_RESIZE_TERMINAL
1163 /* Update readline's view of the window size after SIGWINCH */
1164 if (sigwinch_received) {
1165 sigwinch_received = 0;
1166 rl_resize_terminal();
1167 }
1168#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 FD_SET(fileno(rl_instream), &selectset);
1170 /* select resets selectset if no input was available */
1171 has_input = select(fileno(rl_instream) + 1, &selectset,
1172 NULL, NULL, timeoutp);
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001173 err = errno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 if(PyOS_InputHook) PyOS_InputHook();
1175 }
1176
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001177 if (has_input > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 rl_callback_read_char();
1179 }
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001180 else if (err == EINTR) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001182#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001184#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001186#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001188#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 if (s < 0) {
1190 rl_free_line_state();
Martin Panterd6990d22016-03-22 07:24:05 +00001191#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0700
1192 rl_callback_sigcleanup();
1193#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 rl_cleanup_after_signal();
1195 rl_callback_handler_remove();
1196 *signal = 1;
1197 completed_input_string = NULL;
1198 }
1199 }
1200 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 return completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001203}
1204
1205
1206#else
Guido van Rossum290900a1997-09-26 21:51:21 +00001207
1208/* Interrupt handler */
1209
1210static jmp_buf jbuf;
1211
Guido van Rossum0969d361997-08-05 21:27:50 +00001212/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +00001213static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001214onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +00001215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +00001217}
1218
Guido van Rossum290900a1997-09-26 21:51:21 +00001219
Guido van Rossum0969d361997-08-05 21:27:50 +00001220static char *
Christian Heimesa3da7c52013-12-04 09:31:47 +01001221readline_until_enter_or_signal(const char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +00001222{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 PyOS_sighandler_t old_inthandler;
1224 char *p;
Guido van Rossum74f31432003-01-07 20:01:29 +00001225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 *signal = 0;
1227
1228 old_inthandler = PyOS_setsig(SIGINT, onintr);
1229 if (setjmp(jbuf)) {
Guido van Rossum0969d361997-08-05 21:27:50 +00001230#ifdef HAVE_SIGRELSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
1232 sigrelse(SIGINT);
Guido van Rossum0969d361997-08-05 21:27:50 +00001233#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 PyOS_setsig(SIGINT, old_inthandler);
1235 *signal = 1;
1236 return NULL;
1237 }
1238 rl_event_hook = PyOS_InputHook;
1239 p = readline(prompt);
1240 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001241
1242 return p;
1243}
1244#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
1245
1246
1247static char *
Serhiy Storchakac6792272013-10-19 21:03:34 +03001248call_readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001249{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 size_t n;
1251 char *p, *q;
1252 int signal;
Neal Norwitz1fa040b2004-08-25 01:20:18 +00001253
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001254#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1256 if (!saved_locale)
1257 Py_FatalError("not enough memory to save locale");
Nadeem Vawda63752572013-02-02 20:05:11 +01001258 setlocale(LC_CTYPE, "");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001259#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
1262 rl_instream = sys_stdin;
1263 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001264#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001266#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 }
Guido van Rossum74f31432003-01-07 20:01:29 +00001268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 p = readline_until_enter_or_signal(prompt, &signal);
Guido van Rossumb18618d2000-05-03 23:44:39 +00001270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 /* we got an interrupt signal */
1272 if (signal) {
1273 RESTORE_LOCALE(saved_locale)
1274 return NULL;
1275 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001276
Martin Panter7462b6492015-11-02 03:37:02 +00001277 /* We got an EOF, return an empty string. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 if (p == NULL) {
Victor Stinner2fe9bac2013-10-10 16:18:20 +02001279 p = PyMem_RawMalloc(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 if (p != NULL)
1281 *p = '\0';
1282 RESTORE_LOCALE(saved_locale)
1283 return p;
1284 }
1285
1286 /* we have a valid line */
1287 n = strlen(p);
Martin Panterf0dbf7a2016-05-15 01:26:25 +00001288 if (should_auto_add_history && n > 0) {
Brett Cannon2525dc82010-08-22 20:36:25 +00001289 const char *line;
Mark Dickinson6b54e1f2010-08-03 16:49:49 +00001290 int length = _py_get_history_length();
1291 if (length > 0)
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001292#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 if (using_libedit_emulation) {
Ned Deilyf70f4a62013-09-06 15:16:19 -07001294 /* handle older 0-based or newer 1-based indexing */
1295 line = (const char *)history_get(length + libedit_history_start - 1)->line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 } else
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001297#endif /* __APPLE__ */
Brett Cannon2525dc82010-08-22 20:36:25 +00001298 line = (const char *)history_get(length)->line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 else
1300 line = "";
1301 if (strcmp(p, line))
1302 add_history(p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 }
1304 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
1305 release the original. */
1306 q = p;
Victor Stinner2fe9bac2013-10-10 16:18:20 +02001307 p = PyMem_RawMalloc(n+2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 if (p != NULL) {
1309 strncpy(p, q, n);
1310 p[n] = '\n';
1311 p[n+1] = '\0';
1312 }
1313 free(q);
1314 RESTORE_LOCALE(saved_locale)
1315 return p;
Guido van Rossum0969d361997-08-05 21:27:50 +00001316}
1317
Guido van Rossum290900a1997-09-26 21:51:21 +00001318
1319/* Initialize the module */
1320
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001321PyDoc_STRVAR(doc_module,
1322"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001323
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001324#ifdef __APPLE__
1325PyDoc_STRVAR(doc_module_le,
1326"Importing this module enables command line editing using libedit readline.");
1327#endif /* __APPLE__ */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001328
1329static struct PyModuleDef readlinemodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 PyModuleDef_HEAD_INIT,
1331 "readline",
1332 doc_module,
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001333 sizeof(readlinestate),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 readline_methods,
1335 NULL,
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001336 readline_traverse,
1337 readline_clear,
1338 readline_free
Martin v. Löwis1a214512008-06-11 05:26:20 +00001339};
1340
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001341
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001342PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001343PyInit_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001344{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 PyObject *m;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001346 readlinestate *mod_state;
Guido van Rossum290900a1997-09-26 21:51:21 +00001347
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001348#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
1350 using_libedit_emulation = 1;
1351 }
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 if (using_libedit_emulation)
1354 readlinemodule.m_doc = doc_module_le;
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001355
1356#endif /* __APPLE__ */
1357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 m = PyModule_Create(&readlinemodule);
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 if (m == NULL)
1361 return NULL;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001362
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001363 mod_state = (readlinestate *) PyModule_GetState(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 PyOS_ReadlineFunctionPointer = call_readline;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001365 setup_readline(mod_state);
Antoine Pitrou7e8b8672014-11-04 14:52:10 +01001366
1367 PyModule_AddIntConstant(m, "_READLINE_VERSION", RL_READLINE_VERSION);
1368 PyModule_AddIntConstant(m, "_READLINE_RUNTIME_VERSION", rl_readline_version);
1369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 return m;
Guido van Rossum0969d361997-08-05 21:27:50 +00001371}