blob: 1e98ffb9c22d719b24559f05dfb0f69c1397b02e [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"
9#include <setjmp.h>
10#include <signal.h>
Guido van Rossum290900a1997-09-26 21:51:21 +000011#include <errno.h>
Michael W. Hudson8da2b012004-10-07 13:46:33 +000012#include <sys/time.h>
Guido van Rossum0969d361997-08-05 21:27:50 +000013
Skip Montanaro7befb992004-02-10 16:50:21 +000014#if defined(HAVE_SETLOCALE)
Guido van Rossum60c8a3a2002-10-09 21:27:33 +000015/* GNU readline() mistakenly sets the LC_CTYPE locale.
16 * This is evil. Only the user or the app's main() should do this!
17 * We must save and restore the locale around the rl_initialize() call.
18 */
19#define SAVE_LOCALE
20#include <locale.h>
21#endif
22
Neal Norwitz5eaf7722006-07-16 02:15:27 +000023#ifdef SAVE_LOCALE
24# define RESTORE_LOCALE(sl) { setlocale(LC_CTYPE, sl); free(sl); }
25#else
Brett Cannon23b581a2010-05-04 00:52:41 +000026# define RESTORE_LOCALE(sl)
Neal Norwitz5eaf7722006-07-16 02:15:27 +000027#endif
28
Guido van Rossum290900a1997-09-26 21:51:21 +000029/* GNU readline definitions */
Guido van Rossumb0e51b22001-04-13 18:14:27 +000030#undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
Guido van Rossumbcc20741998-08-04 22:53:56 +000031#include <readline/readline.h>
32#include <readline/history.h>
Guido van Rossum730806d1998-04-10 22:27:42 +000033
Guido van Rossum353ae582001-07-10 16:45:32 +000034#ifdef HAVE_RL_COMPLETION_MATCHES
Guido van Rossum74f31432003-01-07 20:01:29 +000035#define completion_matches(x, y) \
Antoine Pitrouc83ea132010-05-09 14:46:46 +000036 rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
Neal Norwitzbd538702007-04-19 05:52:37 +000037#else
Martin v. Löwisbb86d832008-11-04 20:40:09 +000038#if defined(_RL_FUNCTION_TYPEDEF)
Neal Norwitzbd538702007-04-19 05:52:37 +000039extern char **completion_matches(char *, rl_compentry_func_t *);
Martin v. Löwisbb86d832008-11-04 20:40:09 +000040#else
Ronald Oussoren333fca92010-02-11 13:13:08 +000041
42#if !defined(__APPLE__)
Martin v. Löwisbb86d832008-11-04 20:40:09 +000043extern char **completion_matches(char *, CPFunction *);
44#endif
Guido van Rossum353ae582001-07-10 16:45:32 +000045#endif
Ronald Oussoren333fca92010-02-11 13:13:08 +000046#endif
Guido van Rossum353ae582001-07-10 16:45:32 +000047
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +000048#ifdef __APPLE__
49/*
50 * It is possible to link the readline module to the readline
Brett Cannon23b581a2010-05-04 00:52:41 +000051 * emulation library of editline/libedit.
52 *
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +000053 * On OSX this emulation library is not 100% API compatible
54 * with the "real" readline and cannot be detected at compile-time,
55 * hence we use a runtime check to detect if we're using libedit
56 *
Ned Deily62a19292013-10-12 15:45:25 -070057 * Currently there is one known API incompatibility:
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +000058 * - 'get_history' has a 1-based index with GNU readline, and a 0-based
Ned Deily62a19292013-10-12 15:45:25 -070059 * index with older versions of libedit's emulation.
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +000060 * - Note that replace_history and remove_history use a 0-based index
Ned Deily62a19292013-10-12 15:45:25 -070061 * with both implementations.
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +000062 */
63static int using_libedit_emulation = 0;
64static const char libedit_version_tag[] = "EditLine wrapper";
Ned Deily62a19292013-10-12 15:45:25 -070065
66static int libedit_history_start = 0;
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +000067#endif /* __APPLE__ */
68
Martin v. Löwisf3548942007-11-12 04:53:02 +000069static void
70on_completion_display_matches_hook(char **matches,
Antoine Pitrouc83ea132010-05-09 14:46:46 +000071 int num_matches, int max_length);
Martin v. Löwisf3548942007-11-12 04:53:02 +000072
Guido van Rossum0969d361997-08-05 21:27:50 +000073
Antoine Pitrou31bc8be2013-05-06 21:51:03 +020074/* Memory allocated for rl_completer_word_break_characters
75 (see issue #17289 for the motivation). */
76static char *completer_word_break_characters;
77
Guido van Rossum290900a1997-09-26 21:51:21 +000078/* Exported function to send one line to readline's init file parser */
79
80static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000081parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000082{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000083 char *s, *copy;
84 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
85 return NULL;
86 /* Make a copy -- rl_parse_and_bind() modifies its argument */
87 /* Bernard Herzog */
88 copy = malloc(1 + strlen(s));
89 if (copy == NULL)
90 return PyErr_NoMemory();
91 strcpy(copy, s);
92 rl_parse_and_bind(copy);
93 free(copy); /* Free the copy */
94 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +000095}
96
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000097PyDoc_STRVAR(doc_parse_and_bind,
98"parse_and_bind(string) -> None\n\
99Parse and execute single line of a readline init file.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000100
101
102/* Exported function to parse a readline init file */
103
104static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000105read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000106{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000107 char *s = NULL;
108 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
109 return NULL;
110 errno = rl_read_init_file(s);
111 if (errno)
112 return PyErr_SetFromErrno(PyExc_IOError);
113 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +0000114}
115
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000116PyDoc_STRVAR(doc_read_init_file,
117"read_init_file([filename]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000118Parse a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000119The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000120
121
Skip Montanaro28067822000-07-06 18:55:12 +0000122/* Exported function to load a readline history file */
123
124static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000125read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000126{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000127 char *s = NULL;
128 if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
129 return NULL;
130 errno = read_history(s);
131 if (errno)
132 return PyErr_SetFromErrno(PyExc_IOError);
133 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000134}
135
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000136static int _history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000137PyDoc_STRVAR(doc_read_history_file,
138"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000139Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000140The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000141
142
143/* Exported function to save a readline history file */
144
145static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000146write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000147{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000148 char *s = NULL;
149 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
150 return NULL;
151 errno = write_history(s);
152 if (!errno && _history_length >= 0)
153 history_truncate_file(s, _history_length);
154 if (errno)
155 return PyErr_SetFromErrno(PyExc_IOError);
156 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000157}
158
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000159PyDoc_STRVAR(doc_write_history_file,
160"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000161Save a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000162The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000163
164
Guido van Rossum74f31432003-01-07 20:01:29 +0000165/* Set history length */
166
167static PyObject*
168set_history_length(PyObject *self, PyObject *args)
169{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000170 int length = _history_length;
171 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
172 return NULL;
173 _history_length = length;
174 Py_RETURN_NONE;
Guido van Rossum74f31432003-01-07 20:01:29 +0000175}
176
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000177PyDoc_STRVAR(set_history_length_doc,
178"set_history_length(length) -> None\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000179set the maximal number of items which will be written to\n\
180the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000181history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000182
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000183
Guido van Rossum74f31432003-01-07 20:01:29 +0000184/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000185
186static PyObject*
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000187get_history_length(PyObject *self, PyObject *noarg)
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000188{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000189 return PyInt_FromLong(_history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000190}
191
Guido van Rossum74f31432003-01-07 20:01:29 +0000192PyDoc_STRVAR(get_history_length_doc,
193"get_history_length() -> int\n\
194return the maximum number of items that will be written to\n\
195the history file.");
196
197
Martin v. Löwis0daad592001-09-30 21:09:59 +0000198/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000199
Martin v. Löwis0daad592001-09-30 21:09:59 +0000200static PyObject *
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000201set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000202{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000203 PyObject *function = Py_None;
204 char buf[80];
205 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
206 if (!PyArg_ParseTuple(args, buf, &function))
207 return NULL;
208 if (function == Py_None) {
Serhiy Storchaka98a97222014-02-09 13:14:04 +0200209 Py_CLEAR(*hook_var);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000210 }
211 else if (PyCallable_Check(function)) {
212 PyObject *tmp = *hook_var;
213 Py_INCREF(function);
214 *hook_var = function;
215 Py_XDECREF(tmp);
216 }
217 else {
218 PyOS_snprintf(buf, sizeof(buf),
219 "set_%.50s(func): argument not callable",
220 funcname);
221 PyErr_SetString(PyExc_TypeError, buf);
222 return NULL;
223 }
224 Py_RETURN_NONE;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000225}
226
Guido van Rossum74f31432003-01-07 20:01:29 +0000227
Martin v. Löwis0daad592001-09-30 21:09:59 +0000228/* Exported functions to specify hook functions in Python */
229
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000230static PyObject *completion_display_matches_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000231static PyObject *startup_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000232
233#ifdef HAVE_RL_PRE_INPUT_HOOK
234static PyObject *pre_input_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000235#endif
236
237static PyObject *
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000238set_completion_display_matches_hook(PyObject *self, PyObject *args)
239{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000240 PyObject *result = set_hook("completion_display_matches_hook",
241 &completion_display_matches_hook, args);
Martin v. Löwisf3548942007-11-12 04:53:02 +0000242#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000243 /* We cannot set this hook globally, since it replaces the
244 default completion display. */
245 rl_completion_display_matches_hook =
246 completion_display_matches_hook ?
Martin v. Löwisbb86d832008-11-04 20:40:09 +0000247#if defined(_RL_FUNCTION_TYPEDEF)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000248 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
Martin v. Löwisbb86d832008-11-04 20:40:09 +0000249#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000250 (VFunction *)on_completion_display_matches_hook : 0;
Martin v. Löwisbb86d832008-11-04 20:40:09 +0000251#endif
Martin v. Löwisf3548942007-11-12 04:53:02 +0000252#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000253 return result;
Martin v. Löwisf3548942007-11-12 04:53:02 +0000254
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000255}
256
257PyDoc_STRVAR(doc_set_completion_display_matches_hook,
258"set_completion_display_matches_hook([function]) -> None\n\
259Set or remove the completion display function.\n\
260The function is called as\n\
261 function(substitution, [matches], longest_match_length)\n\
262once each time matches need to be displayed.");
263
264static PyObject *
Martin v. Löwis0daad592001-09-30 21:09:59 +0000265set_startup_hook(PyObject *self, PyObject *args)
266{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000267 return set_hook("startup_hook", &startup_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000268}
269
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000270PyDoc_STRVAR(doc_set_startup_hook,
271"set_startup_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000272Set or remove the startup_hook function.\n\
273The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000274before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000275
Guido van Rossum74f31432003-01-07 20:01:29 +0000276
Martin v. Löwis0daad592001-09-30 21:09:59 +0000277#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000278
279/* Set pre-input hook */
280
Martin v. Löwis0daad592001-09-30 21:09:59 +0000281static PyObject *
282set_pre_input_hook(PyObject *self, PyObject *args)
283{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000284 return set_hook("pre_input_hook", &pre_input_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000285}
286
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000287PyDoc_STRVAR(doc_set_pre_input_hook,
288"set_pre_input_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000289Set or remove the pre_input_hook function.\n\
290The function is called with no arguments after the first prompt\n\
291has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000292characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000293
Martin v. Löwis0daad592001-09-30 21:09:59 +0000294#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000295
Guido van Rossum74f31432003-01-07 20:01:29 +0000296
Guido van Rossum290900a1997-09-26 21:51:21 +0000297/* Exported function to specify a word completer in Python */
298
299static PyObject *completer = NULL;
Guido van Rossum290900a1997-09-26 21:51:21 +0000300
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000301static PyObject *begidx = NULL;
302static PyObject *endidx = NULL;
303
Guido van Rossum74f31432003-01-07 20:01:29 +0000304
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000305/* Get the completion type for the scope of the tab-completion */
306static PyObject *
307get_completion_type(PyObject *self, PyObject *noarg)
308{
309 return PyInt_FromLong(rl_completion_type);
310}
311
312PyDoc_STRVAR(doc_get_completion_type,
313"get_completion_type() -> int\n\
314Get the type of completion being attempted.");
315
316
Guido van Rossum74f31432003-01-07 20:01:29 +0000317/* Get the beginning index for the scope of the tab-completion */
318
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000319static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000320get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000321{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000322 Py_INCREF(begidx);
323 return begidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000324}
325
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000326PyDoc_STRVAR(doc_get_begidx,
327"get_begidx() -> int\n\
328get the beginning index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000329
Guido van Rossum74f31432003-01-07 20:01:29 +0000330
331/* Get the ending index for the scope of the tab-completion */
332
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000333static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000334get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000335{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000336 Py_INCREF(endidx);
337 return endidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000338}
339
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000340PyDoc_STRVAR(doc_get_endidx,
341"get_endidx() -> int\n\
342get the ending index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000343
344
Guido van Rossum74f31432003-01-07 20:01:29 +0000345/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000346
347static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000348set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000349{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000350 char *break_chars;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000351
Antoine Pitrou31bc8be2013-05-06 21:51:03 +0200352 if (!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000353 return NULL;
354 }
Antoine Pitrou31bc8be2013-05-06 21:51:03 +0200355 /* Keep a reference to the allocated memory in the module state in case
356 some other module modifies rl_completer_word_break_characters
357 (see issue #17289). */
Serhiy Storchakaa8041ae2015-09-27 22:34:59 +0300358 break_chars = strdup(break_chars);
359 if (break_chars) {
360 free(completer_word_break_characters);
361 completer_word_break_characters = break_chars;
362 rl_completer_word_break_characters = break_chars;
Antoine Pitrou31bc8be2013-05-06 21:51:03 +0200363 Py_RETURN_NONE;
364 }
365 else
366 return PyErr_NoMemory();
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000367}
368
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000369PyDoc_STRVAR(doc_set_completer_delims,
370"set_completer_delims(string) -> None\n\
371set the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000372
Mark Dickinson4ee98532010-08-03 16:18:39 +0000373/* _py_free_history_entry: Utility function to free a history entry. */
374
375#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
376
377/* Readline version >= 5.0 introduced a timestamp field into the history entry
378 structure; this needs to be freed to avoid a memory leak. This version of
379 readline also introduced the handy 'free_history_entry' function, which
380 takes care of the timestamp. */
381
382static void
383_py_free_history_entry(HIST_ENTRY *entry)
384{
385 histdata_t data = free_history_entry(entry);
386 free(data);
387}
388
389#else
390
391/* No free_history_entry function; free everything manually. */
392
393static void
394_py_free_history_entry(HIST_ENTRY *entry)
395{
396 if (entry->line)
397 free((void *)entry->line);
398 if (entry->data)
399 free(entry->data);
400 free(entry);
401}
402
403#endif
404
Skip Montanaroe5069012004-08-15 14:32:06 +0000405static PyObject *
406py_remove_history(PyObject *self, PyObject *args)
407{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000408 int entry_number;
409 HIST_ENTRY *entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000410
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000411 if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
412 return NULL;
413 if (entry_number < 0) {
414 PyErr_SetString(PyExc_ValueError,
415 "History index cannot be negative");
416 return NULL;
417 }
418 entry = remove_history(entry_number);
419 if (!entry) {
420 PyErr_Format(PyExc_ValueError,
421 "No history item at position %d",
422 entry_number);
423 return NULL;
424 }
425 /* free memory allocated for the history entry */
Mark Dickinson4ee98532010-08-03 16:18:39 +0000426 _py_free_history_entry(entry);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000427 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000428}
429
430PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000431"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000432remove history item given by its position");
433
434static PyObject *
435py_replace_history(PyObject *self, PyObject *args)
436{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000437 int entry_number;
438 char *line;
439 HIST_ENTRY *old_entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000440
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000441 if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number,
442 &line)) {
443 return NULL;
444 }
445 if (entry_number < 0) {
446 PyErr_SetString(PyExc_ValueError,
447 "History index cannot be negative");
448 return NULL;
449 }
450 old_entry = replace_history_entry(entry_number, line, (void *)NULL);
451 if (!old_entry) {
452 PyErr_Format(PyExc_ValueError,
453 "No history item at position %d",
454 entry_number);
455 return NULL;
456 }
457 /* free memory allocated for the old history entry */
Mark Dickinson4ee98532010-08-03 16:18:39 +0000458 _py_free_history_entry(old_entry);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000459 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000460}
461
462PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000463"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000464replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000465
466/* Add a line to the history buffer */
467
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000468static PyObject *
469py_add_history(PyObject *self, PyObject *args)
470{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000471 char *line;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000472
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000473 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
474 return NULL;
475 }
476 add_history(line);
477 Py_RETURN_NONE;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000478}
479
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000480PyDoc_STRVAR(doc_add_history,
481"add_history(string) -> None\n\
482add a line to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000483
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000484
Guido van Rossum74f31432003-01-07 20:01:29 +0000485/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000486
487static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000488get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000489{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000490 return PyString_FromString(rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000491}
Guido van Rossum74f31432003-01-07 20:01:29 +0000492
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000493PyDoc_STRVAR(doc_get_completer_delims,
494"get_completer_delims() -> string\n\
495get the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000496
Guido van Rossum74f31432003-01-07 20:01:29 +0000497
498/* Set the completer function */
499
Guido van Rossum290900a1997-09-26 21:51:21 +0000500static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000501set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000502{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000503 return set_hook("completer", &completer, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000504}
505
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000506PyDoc_STRVAR(doc_set_completer,
507"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000508Set or remove the completer function.\n\
509The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000510for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000511It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000512
Guido van Rossum74f31432003-01-07 20:01:29 +0000513
Michael W. Hudson796df152003-01-30 10:12:51 +0000514static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000515get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000516{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000517 if (completer == NULL) {
518 Py_RETURN_NONE;
519 }
520 Py_INCREF(completer);
521 return completer;
Michael W. Hudson796df152003-01-30 10:12:51 +0000522}
523
524PyDoc_STRVAR(doc_get_completer,
525"get_completer() -> function\n\
526\n\
527Returns current completer function.");
528
Mark Dickinson0f981282010-08-03 16:54:19 +0000529/* Private function to get current length of history. XXX It may be
530 * possible to replace this with a direct use of history_length instead,
531 * but it's not clear whether BSD's libedit keeps history_length up to date.
532 * See issue #8065.*/
533
534static int
535_py_get_history_length(void)
536{
537 HISTORY_STATE *hist_st = history_get_history_state();
538 int length = hist_st->length;
539 /* the history docs don't say so, but the address of hist_st changes each
540 time history_get_history_state is called which makes me think it's
541 freshly malloc'd memory... on the other hand, the address of the last
542 line stays the same as long as history isn't extended, so it appears to
543 be malloc'd but managed by the history package... */
544 free(hist_st);
545 return length;
546}
547
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000548/* Exported function to get any element of history */
549
550static PyObject *
551get_history_item(PyObject *self, PyObject *args)
552{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000553 int idx = 0;
554 HIST_ENTRY *hist_ent;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000555
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000556 if (!PyArg_ParseTuple(args, "i:index", &idx))
557 return NULL;
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +0000558#ifdef __APPLE__
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000559 if (using_libedit_emulation) {
Ned Deily62a19292013-10-12 15:45:25 -0700560 /* Older versions of libedit's readline emulation
561 * use 0-based indexes, while readline and newer
562 * versions of libedit use 1-based indexes.
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000563 */
Mark Dickinson0f981282010-08-03 16:54:19 +0000564 int length = _py_get_history_length();
Ned Deily62a19292013-10-12 15:45:25 -0700565
566 idx = idx - 1 + libedit_history_start;
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +0000567
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000568 /*
569 * Apple's readline emulation crashes when
570 * the index is out of range, therefore
571 * test for that and fail gracefully.
572 */
Ned Deily62a19292013-10-12 15:45:25 -0700573 if (idx < (0 + libedit_history_start)
574 || idx >= (length + libedit_history_start)) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000575 Py_RETURN_NONE;
576 }
577 }
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +0000578#endif /* __APPLE__ */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000579 if ((hist_ent = history_get(idx)))
580 return PyString_FromString(hist_ent->line);
581 else {
582 Py_RETURN_NONE;
583 }
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000584}
585
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000586PyDoc_STRVAR(doc_get_history_item,
587"get_history_item() -> string\n\
588return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000589
Guido van Rossum74f31432003-01-07 20:01:29 +0000590
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000591/* Exported function to get current length of history */
592
593static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000594get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000595{
Mark Dickinson0f981282010-08-03 16:54:19 +0000596 return PyInt_FromLong((long)_py_get_history_length());
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000597}
598
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000599PyDoc_STRVAR(doc_get_current_history_length,
600"get_current_history_length() -> integer\n\
601return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000602
Guido van Rossum74f31432003-01-07 20:01:29 +0000603
Guido van Rossum79378ff1997-10-07 14:53:21 +0000604/* Exported function to read the current line buffer */
605
606static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000607get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000608{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000609 return PyString_FromString(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000610}
611
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000612PyDoc_STRVAR(doc_get_line_buffer,
613"get_line_buffer() -> string\n\
614return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000615
Guido van Rossum74f31432003-01-07 20:01:29 +0000616
Martin v. Löwise7a97962003-09-20 16:08:33 +0000617#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
618
619/* Exported function to clear the current history */
620
621static PyObject *
622py_clear_history(PyObject *self, PyObject *noarg)
623{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000624 clear_history();
625 Py_RETURN_NONE;
Martin v. Löwise7a97962003-09-20 16:08:33 +0000626}
627
628PyDoc_STRVAR(doc_clear_history,
629"clear_history() -> None\n\
630Clear the current readline history.");
631#endif
632
633
Guido van Rossum79378ff1997-10-07 14:53:21 +0000634/* Exported function to insert text into the line buffer */
635
636static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000637insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000638{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000639 char *s;
640 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
641 return NULL;
642 rl_insert_text(s);
643 Py_RETURN_NONE;
Guido van Rossum79378ff1997-10-07 14:53:21 +0000644}
645
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000646PyDoc_STRVAR(doc_insert_text,
647"insert_text(string) -> None\n\
648Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000649
Guido van Rossum74f31432003-01-07 20:01:29 +0000650
651/* Redisplay the line buffer */
652
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000653static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000654redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000655{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000656 rl_redisplay();
657 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000658}
659
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000660PyDoc_STRVAR(doc_redisplay,
661"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000662Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000663contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000664
Guido van Rossum74f31432003-01-07 20:01:29 +0000665
Guido van Rossum290900a1997-09-26 21:51:21 +0000666/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000667
668static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000669{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000670 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
671 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
672 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
673 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
674 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
675 {"read_history_file", read_history_file,
676 METH_VARARGS, doc_read_history_file},
677 {"write_history_file", write_history_file,
678 METH_VARARGS, doc_write_history_file},
679 {"get_history_item", get_history_item,
680 METH_VARARGS, doc_get_history_item},
681 {"get_current_history_length", (PyCFunction)get_current_history_length,
682 METH_NOARGS, doc_get_current_history_length},
683 {"set_history_length", set_history_length,
684 METH_VARARGS, set_history_length_doc},
685 {"get_history_length", get_history_length,
686 METH_NOARGS, get_history_length_doc},
687 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
688 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
689 {"get_completion_type", get_completion_type,
690 METH_NOARGS, doc_get_completion_type},
691 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
692 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000693
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000694 {"set_completer_delims", set_completer_delims,
695 METH_VARARGS, doc_set_completer_delims},
696 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
697 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
698 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
699 {"get_completer_delims", get_completer_delims,
700 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000701
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000702 {"set_completion_display_matches_hook", set_completion_display_matches_hook,
703 METH_VARARGS, doc_set_completion_display_matches_hook},
704 {"set_startup_hook", set_startup_hook,
705 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000706#ifdef HAVE_RL_PRE_INPUT_HOOK
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000707 {"set_pre_input_hook", set_pre_input_hook,
708 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000709#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000710#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000711 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
Martin v. Löwise7a97962003-09-20 16:08:33 +0000712#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000713 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000714};
715
Guido van Rossum05ac4492003-01-07 20:04:12 +0000716
Martin v. Löwis0daad592001-09-30 21:09:59 +0000717/* C function to call the Python hooks. */
718
719static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000720on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000721{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000722 int result = 0;
723 if (func != NULL) {
724 PyObject *r;
Christian Heimes1bc4af42007-11-12 18:58:08 +0000725#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000726 PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000727#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000728 r = PyObject_CallFunction(func, NULL);
729 if (r == NULL)
730 goto error;
731 if (r == Py_None)
732 result = 0;
733 else {
734 result = PyInt_AsLong(r);
735 if (result == -1 && PyErr_Occurred())
736 goto error;
737 }
738 Py_DECREF(r);
739 goto done;
740 error:
741 PyErr_Clear();
742 Py_XDECREF(r);
743 done:
Christian Heimes1bc4af42007-11-12 18:58:08 +0000744#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000745 PyGILState_Release(gilstate);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000746#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000747 return result;
748 }
749 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000750}
751
752static int
Ned Deilyb0fd12d2014-02-05 16:52:26 -0800753#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000754on_startup_hook(void)
Ned Deilyb0fd12d2014-02-05 16:52:26 -0800755#else
756on_startup_hook()
757#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000758{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000759 return on_hook(startup_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000760}
761
762#ifdef HAVE_RL_PRE_INPUT_HOOK
763static int
Ned Deilyb0fd12d2014-02-05 16:52:26 -0800764#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000765on_pre_input_hook(void)
Ned Deilyb0fd12d2014-02-05 16:52:26 -0800766#else
767on_pre_input_hook()
768#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000769{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000770 return on_hook(pre_input_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000771}
772#endif
773
Guido van Rossum05ac4492003-01-07 20:04:12 +0000774
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000775/* C function to call the Python completion_display_matches */
776
777static void
778on_completion_display_matches_hook(char **matches,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000779 int num_matches, int max_length)
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000780{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000781 int i;
782 PyObject *m=NULL, *s=NULL, *r=NULL;
Christian Heimes1bc4af42007-11-12 18:58:08 +0000783#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000784 PyGILState_STATE gilstate = PyGILState_Ensure();
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000785#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000786 m = PyList_New(num_matches);
787 if (m == NULL)
788 goto error;
789 for (i = 0; i < num_matches; i++) {
790 s = PyString_FromString(matches[i+1]);
791 if (s == NULL)
792 goto error;
793 if (PyList_SetItem(m, i, s) == -1)
794 goto error;
795 }
Martin v. Löwisf3548942007-11-12 04:53:02 +0000796
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000797 r = PyObject_CallFunction(completion_display_matches_hook,
798 "sOi", matches[0], m, max_length);
Martin v. Löwisf3548942007-11-12 04:53:02 +0000799
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000800 Py_DECREF(m); m=NULL;
Brett Cannon23b581a2010-05-04 00:52:41 +0000801
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000802 if (r == NULL ||
803 (r != Py_None && PyInt_AsLong(r) == -1 && PyErr_Occurred())) {
804 goto error;
805 }
806 Py_XDECREF(r); r=NULL;
Martin v. Löwisf3548942007-11-12 04:53:02 +0000807
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000808 if (0) {
809 error:
810 PyErr_Clear();
811 Py_XDECREF(m);
812 Py_XDECREF(r);
813 }
Christian Heimes1bc4af42007-11-12 18:58:08 +0000814#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000815 PyGILState_Release(gilstate);
Martin v. Löwisf3548942007-11-12 04:53:02 +0000816#endif
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000817}
818
819
Guido van Rossum290900a1997-09-26 21:51:21 +0000820/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000821
Guido van Rossum290900a1997-09-26 21:51:21 +0000822static char *
Neal Norwitzbd538702007-04-19 05:52:37 +0000823on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000824{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000825 char *result = NULL;
826 if (completer != NULL) {
827 PyObject *r;
Brett Cannon23b581a2010-05-04 00:52:41 +0000828#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000829 PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000830#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000831 rl_attempted_completion_over = 1;
832 r = PyObject_CallFunction(completer, "si", text, state);
833 if (r == NULL)
834 goto error;
835 if (r == Py_None) {
836 result = NULL;
837 }
838 else {
839 char *s = PyString_AsString(r);
840 if (s == NULL)
841 goto error;
842 result = strdup(s);
843 }
844 Py_DECREF(r);
845 goto done;
846 error:
847 PyErr_Clear();
848 Py_XDECREF(r);
849 done:
Brett Cannon23b581a2010-05-04 00:52:41 +0000850#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000851 PyGILState_Release(gilstate);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000852#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000853 return result;
854 }
855 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000856}
857
Guido van Rossum290900a1997-09-26 21:51:21 +0000858
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000859/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000860 * before calling the normal completer */
861
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000862static char **
Benjamin Peterson0ac0ead2014-01-24 11:44:16 -0500863flex_complete(const char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000864{
Antoine Pitrou119cdef2009-10-19 18:17:18 +0000865#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000866 rl_completion_append_character ='\0';
Antoine Pitroud9ff74e2009-10-26 19:16:46 +0000867#endif
868#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000869 rl_completion_suppress_append = 0;
Antoine Pitrou119cdef2009-10-19 18:17:18 +0000870#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000871 Py_XDECREF(begidx);
872 Py_XDECREF(endidx);
873 begidx = PyInt_FromLong((long) start);
874 endidx = PyInt_FromLong((long) end);
875 return completion_matches(text, *on_completion);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000876}
877
Guido van Rossum05ac4492003-01-07 20:04:12 +0000878
Guido van Rossum290900a1997-09-26 21:51:21 +0000879/* Helper to initialize GNU readline properly. */
880
881static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000882setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000883{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000884#ifdef SAVE_LOCALE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000885 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
886 if (!saved_locale)
887 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000888#endif
889
R. David Murray7a697252010-12-18 03:52:09 +0000890#ifdef __APPLE__
Victor Stinner63a47472014-07-24 12:22:24 +0200891 /* the libedit readline emulation resets key bindings etc
R. David Murray7a697252010-12-18 03:52:09 +0000892 * when calling rl_initialize. So call it upfront
893 */
894 if (using_libedit_emulation)
895 rl_initialize();
Ned Deily62a19292013-10-12 15:45:25 -0700896
897 /* Detect if libedit's readline emulation uses 0-based
898 * indexing or 1-based indexing.
899 */
900 add_history("1");
901 if (history_get(1) == NULL) {
902 libedit_history_start = 0;
903 } else {
904 libedit_history_start = 1;
905 }
906 clear_history();
R. David Murray7a697252010-12-18 03:52:09 +0000907#endif /* __APPLE__ */
908
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000909 using_history();
Skip Montanaroa0392742002-06-11 14:32:46 +0000910
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000911 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000912#if defined(PYOS_OS2) && defined(PYCC_GCC)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000913 /* Allow $if term= in .inputrc to work */
914 rl_terminal_name = getenv("TERM");
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000915#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000916 /* Force rebind of TAB to insert-tab */
917 rl_bind_key('\t', rl_insert);
918 /* Bind both ESC-TAB and ESC-ESC to the completion function */
919 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
920 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
921 /* Set our hook functions */
Benjamin Peterson0ac0ead2014-01-24 11:44:16 -0500922 rl_startup_hook = on_startup_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000923#ifdef HAVE_RL_PRE_INPUT_HOOK
Benjamin Peterson0ac0ead2014-01-24 11:44:16 -0500924 rl_pre_input_hook = on_pre_input_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000925#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000926 /* Set our completion function */
Benjamin Peterson0ac0ead2014-01-24 11:44:16 -0500927 rl_attempted_completion_function = flex_complete;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000928 /* Set Python word break characters */
Antoine Pitrou31bc8be2013-05-06 21:51:03 +0200929 completer_word_break_characters =
930 rl_completer_word_break_characters =
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000931 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
932 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000933
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000934 begidx = PyInt_FromLong(0L);
935 endidx = PyInt_FromLong(0L);
Victor Stinner63a47472014-07-24 12:22:24 +0200936
Benjamin Petersonb47b54c2014-08-20 17:30:40 -0500937#ifndef __APPLE__
Victor Stinner63a47472014-07-24 12:22:24 +0200938 if (!isatty(STDOUT_FILENO)) {
939 /* Issue #19884: stdout is no a terminal. Disable meta modifier
940 keys to not write the ANSI sequence "\033[1034h" into stdout. On
941 terminals supporting 8 bit characters like TERM=xterm-256color
942 (which is now the default Fedora since Fedora 18), the meta key is
943 used to enable support of 8 bit characters (ANSI sequence
Benjamin Petersonb47b54c2014-08-20 17:30:40 -0500944 "\033[1034h").
945
946 With libedit, this call makes readline() crash. */
Victor Stinner63a47472014-07-24 12:22:24 +0200947 rl_variable_bind ("enable-meta-key", "off");
948 }
Benjamin Petersonb47b54c2014-08-20 17:30:40 -0500949#endif
Victor Stinner63a47472014-07-24 12:22:24 +0200950
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000951 /* Initialize (allows .inputrc to override)
952 *
953 * XXX: A bug in the readline-2.2 library causes a memory leak
954 * inside this function. Nothing we can do about it.
955 */
R. David Murray7a697252010-12-18 03:52:09 +0000956#ifdef __APPLE__
957 if (using_libedit_emulation)
Antoine Pitrou31bc8be2013-05-06 21:51:03 +0200958 rl_read_init_file(NULL);
R. David Murray7a697252010-12-18 03:52:09 +0000959 else
960#endif /* __APPLE__ */
961 rl_initialize();
Victor Stinner63a47472014-07-24 12:22:24 +0200962
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000963 RESTORE_LOCALE(saved_locale)
Guido van Rossum290900a1997-09-26 21:51:21 +0000964}
965
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000966/* Wrapper around GNU readline that handles signals differently. */
967
968
969#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
970
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000971static char *completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000972static void
973rlhandler(char *text)
974{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000975 completed_input_string = text;
976 rl_callback_handler_remove();
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000977}
978
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000979static char *
980readline_until_enter_or_signal(char *prompt, int *signal)
981{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000982 char * not_done_reading = "";
983 fd_set selectset;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000984
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000985 *signal = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000986#ifdef HAVE_RL_CATCH_SIGNAL
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000987 rl_catch_signals = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000988#endif
989
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000990 rl_callback_handler_install (prompt, rlhandler);
991 FD_ZERO(&selectset);
Brett Cannon23b581a2010-05-04 00:52:41 +0000992
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000993 completed_input_string = not_done_reading;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000994
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000995 while (completed_input_string == not_done_reading) {
996 int has_input = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000997
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000998 while (!has_input)
999 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Andrew M. Kuchling62e475b2006-09-07 13:59:38 +00001000
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001001 /* [Bug #1552726] Only limit the pause if an input hook has been
1002 defined. */
1003 struct timeval *timeoutp = NULL;
1004 if (PyOS_InputHook)
1005 timeoutp = &timeout;
1006 FD_SET(fileno(rl_instream), &selectset);
1007 /* select resets selectset if no input was available */
1008 has_input = select(fileno(rl_instream) + 1, &selectset,
1009 NULL, NULL, timeoutp);
1010 if(PyOS_InputHook) PyOS_InputHook();
1011 }
Michael W. Hudson8da2b012004-10-07 13:46:33 +00001012
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001013 if(has_input > 0) {
1014 rl_callback_read_char();
1015 }
1016 else if (errno == EINTR) {
1017 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001018#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001019 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001020#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001021 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001022#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001023 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001024#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001025 if (s < 0) {
1026 rl_free_line_state();
1027 rl_cleanup_after_signal();
1028 rl_callback_handler_remove();
1029 *signal = 1;
1030 completed_input_string = NULL;
1031 }
1032 }
1033 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001034
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001035 return completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001036}
1037
1038
1039#else
Guido van Rossum290900a1997-09-26 21:51:21 +00001040
1041/* Interrupt handler */
1042
1043static jmp_buf jbuf;
1044
Guido van Rossum0969d361997-08-05 21:27:50 +00001045/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +00001046static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001047onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +00001048{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001049 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +00001050}
1051
Guido van Rossum290900a1997-09-26 21:51:21 +00001052
Guido van Rossum0969d361997-08-05 21:27:50 +00001053static char *
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001054readline_until_enter_or_signal(char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +00001055{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001056 PyOS_sighandler_t old_inthandler;
1057 char *p;
Brett Cannon23b581a2010-05-04 00:52:41 +00001058
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001059 *signal = 0;
Guido van Rossum74f31432003-01-07 20:01:29 +00001060
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001061 old_inthandler = PyOS_setsig(SIGINT, onintr);
1062 if (setjmp(jbuf)) {
Guido van Rossum0969d361997-08-05 21:27:50 +00001063#ifdef HAVE_SIGRELSE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001064 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
1065 sigrelse(SIGINT);
Guido van Rossum0969d361997-08-05 21:27:50 +00001066#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001067 PyOS_setsig(SIGINT, old_inthandler);
1068 *signal = 1;
1069 return NULL;
1070 }
1071 rl_event_hook = PyOS_InputHook;
1072 p = readline(prompt);
1073 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001074
1075 return p;
1076}
1077#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
1078
1079
1080static char *
1081call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
1082{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001083 size_t n;
1084 char *p, *q;
1085 int signal;
Neal Norwitz1fa040b2004-08-25 01:20:18 +00001086
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001087#ifdef SAVE_LOCALE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001088 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1089 if (!saved_locale)
1090 Py_FatalError("not enough memory to save locale");
Nadeem Vawda1efd9822013-02-02 20:52:54 +01001091 setlocale(LC_CTYPE, "");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001092#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001093
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001094 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
1095 rl_instream = sys_stdin;
1096 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001097#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001098 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001099#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001100 }
Guido van Rossum74f31432003-01-07 20:01:29 +00001101
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001102 p = readline_until_enter_or_signal(prompt, &signal);
Brett Cannon23b581a2010-05-04 00:52:41 +00001103
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001104 /* we got an interrupt signal */
1105 if (signal) {
1106 RESTORE_LOCALE(saved_locale)
1107 return NULL;
1108 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001109
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001110 /* We got an EOF, return a empty string. */
1111 if (p == NULL) {
1112 p = PyMem_Malloc(1);
1113 if (p != NULL)
1114 *p = '\0';
1115 RESTORE_LOCALE(saved_locale)
1116 return p;
1117 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001118
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001119 /* we have a valid line */
1120 n = strlen(p);
1121 if (n > 0) {
1122 const char *line;
Mark Dickinson0f981282010-08-03 16:54:19 +00001123 int length = _py_get_history_length();
1124 if (length > 0)
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00001125#ifdef __APPLE__
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001126 if (using_libedit_emulation) {
Ned Deily62a19292013-10-12 15:45:25 -07001127 /* handle older 0-based or newer 1-based indexing */
1128 line = history_get(length + libedit_history_start - 1)->line;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001129 } else
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00001130#endif /* __APPLE__ */
Mark Dickinson0f981282010-08-03 16:54:19 +00001131 line = history_get(length)->line;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001132 else
1133 line = "";
1134 if (strcmp(p, line))
1135 add_history(p);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001136 }
1137 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
1138 release the original. */
1139 q = p;
1140 p = PyMem_Malloc(n+2);
1141 if (p != NULL) {
1142 strncpy(p, q, n);
1143 p[n] = '\n';
1144 p[n+1] = '\0';
1145 }
1146 free(q);
1147 RESTORE_LOCALE(saved_locale)
1148 return p;
Guido van Rossum0969d361997-08-05 21:27:50 +00001149}
1150
Guido van Rossum290900a1997-09-26 21:51:21 +00001151
1152/* Initialize the module */
1153
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001154PyDoc_STRVAR(doc_module,
1155"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001156
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00001157#ifdef __APPLE__
1158PyDoc_STRVAR(doc_module_le,
1159"Importing this module enables command line editing using libedit readline.");
1160#endif /* __APPLE__ */
1161
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001162PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001163initreadline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001164{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001165 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +00001166
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00001167#ifdef __APPLE__
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001168 if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
1169 using_libedit_emulation = 1;
1170 }
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00001171
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001172 if (using_libedit_emulation)
1173 m = Py_InitModule4("readline", readline_methods, doc_module_le,
1174 (PyObject *)NULL, PYTHON_API_VERSION);
1175 else
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00001176
1177#endif /* __APPLE__ */
1178
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001179 m = Py_InitModule4("readline", readline_methods, doc_module,
1180 (PyObject *)NULL, PYTHON_API_VERSION);
1181 if (m == NULL)
1182 return;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001183
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001184 PyOS_ReadlineFunctionPointer = call_readline;
1185 setup_readline();
Antoine Pitrou06c14972014-11-04 14:52:10 +01001186
1187 PyModule_AddIntConstant(m, "_READLINE_VERSION", RL_READLINE_VERSION);
1188 PyModule_AddIntConstant(m, "_READLINE_RUNTIME_VERSION", rl_readline_version);
Guido van Rossum0969d361997-08-05 21:27:50 +00001189}