blob: 4ee17a91cd977dd96ffb3adda7cbe92da0151032 [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
Thomas Wouters0e3f5912006-08-11 14:57:12 +000023#ifdef SAVE_LOCALE
24# define RESTORE_LOCALE(sl) { setlocale(LC_CTYPE, sl); free(sl); }
25#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000026# define RESTORE_LOCALE(sl)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000027#endif
28
Guido van Rossum290900a1997-09-26 21:51:21 +000029/* GNU readline definitions */
Guido van Rossumb0e51b22001-04-13 18:14:27 +000030#undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
Guido van Rossumbcc20741998-08-04 22:53:56 +000031#include <readline/readline.h>
32#include <readline/history.h>
Guido van Rossum730806d1998-04-10 22:27:42 +000033
Guido van Rossum353ae582001-07-10 16:45:32 +000034#ifdef HAVE_RL_COMPLETION_MATCHES
Guido van Rossum74f31432003-01-07 20:01:29 +000035#define completion_matches(x, y) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036 rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
Guido van Rossumd8faa362007-04-27 19:54:29 +000037#else
Martin v. Löwisb37509b2008-11-04 20:45:29 +000038#if defined(_RL_FUNCTION_TYPEDEF)
Guido van Rossumd8faa362007-04-27 19:54:29 +000039extern char **completion_matches(char *, rl_compentry_func_t *);
Martin v. Löwisb37509b2008-11-04 20:45:29 +000040#else
Ronald Oussoren25696bb2010-02-11 13:15:00 +000041
42#if !defined(__APPLE__)
Martin v. Löwisb37509b2008-11-04 20:45:29 +000043extern char **completion_matches(char *, CPFunction *);
44#endif
Guido van Rossum353ae582001-07-10 16:45:32 +000045#endif
Ronald Oussoren25696bb2010-02-11 13:15:00 +000046#endif
Guido van Rossum353ae582001-07-10 16:45:32 +000047
Ronald Oussoren2efd9242009-09-20 14:53:22 +000048#ifdef __APPLE__
49/*
50 * It is possible to link the readline module to the readline
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 * emulation library of editline/libedit.
52 *
Ronald Oussoren2efd9242009-09-20 14:53:22 +000053 * On OSX this emulation library is not 100% API compatible
54 * with the "real" readline and cannot be detected at compile-time,
55 * hence we use a runtime check to detect if we're using libedit
56 *
Ned Deily5d4121a2013-10-12 15:47:58 -070057 * Currently there is one known API incompatibility:
Ronald Oussoren2efd9242009-09-20 14:53:22 +000058 * - 'get_history' has a 1-based index with GNU readline, and a 0-based
Ned Deily5d4121a2013-10-12 15:47:58 -070059 * index with older versions of libedit's emulation.
Ronald Oussoren2efd9242009-09-20 14:53:22 +000060 * - Note that replace_history and remove_history use a 0-based index
Ned Deily5d4121a2013-10-12 15:47:58 -070061 * with both implementations.
Ronald Oussoren2efd9242009-09-20 14:53:22 +000062 */
63static int using_libedit_emulation = 0;
64static const char libedit_version_tag[] = "EditLine wrapper";
Ned Deily5d4121a2013-10-12 15:47:58 -070065
66static int libedit_history_start = 0;
Ronald Oussoren2efd9242009-09-20 14:53:22 +000067#endif /* __APPLE__ */
68
Georg Brandl646fdd62010-10-18 07:27:55 +000069#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Christian Heimes32fbe592007-11-12 15:01:33 +000070static void
71on_completion_display_matches_hook(char **matches,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 int num_matches, int max_length);
Georg Brandl646fdd62010-10-18 07:27:55 +000073#endif
Guido van Rossum0969d361997-08-05 21:27:50 +000074
Antoine Pitroua7f7deb2013-05-06 21:51:03 +020075/* Memory allocated for rl_completer_word_break_characters
76 (see issue #17289 for the motivation). */
77static char *completer_word_break_characters;
78
Guido van Rossum290900a1997-09-26 21:51:21 +000079/* Exported function to send one line to readline's init file parser */
80
81static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000082parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000083{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 char *s, *copy;
85 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
86 return NULL;
87 /* Make a copy -- rl_parse_and_bind() modifies its argument */
88 /* Bernard Herzog */
89 copy = malloc(1 + strlen(s));
90 if (copy == NULL)
91 return PyErr_NoMemory();
92 strcpy(copy, s);
93 rl_parse_and_bind(copy);
94 free(copy); /* Free the copy */
95 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +000096}
97
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000098PyDoc_STRVAR(doc_parse_and_bind,
99"parse_and_bind(string) -> None\n\
100Parse and execute single line of a readline init file.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000101
102
103/* Exported function to parse a readline init file */
104
105static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000106read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000107{
Victor Stinner19e65a32010-06-11 22:27:14 +0000108 PyObject *filename_obj = Py_None, *filename_bytes;
109 if (!PyArg_ParseTuple(args, "|O:read_init_file", &filename_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 return NULL;
Victor Stinner19e65a32010-06-11 22:27:14 +0000111 if (filename_obj != Py_None) {
112 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
113 return NULL;
114 errno = rl_read_init_file(PyBytes_AsString(filename_bytes));
115 Py_DECREF(filename_bytes);
116 } else
117 errno = rl_read_init_file(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 if (errno)
119 return PyErr_SetFromErrno(PyExc_IOError);
120 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +0000121}
122
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000123PyDoc_STRVAR(doc_read_init_file,
124"read_init_file([filename]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000125Parse a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000126The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000127
128
Skip Montanaro28067822000-07-06 18:55:12 +0000129/* Exported function to load a readline history file */
130
131static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000132read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000133{
Victor Stinner19e65a32010-06-11 22:27:14 +0000134 PyObject *filename_obj = Py_None, *filename_bytes;
135 if (!PyArg_ParseTuple(args, "|O:read_history_file", &filename_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 return NULL;
Victor Stinner19e65a32010-06-11 22:27:14 +0000137 if (filename_obj != Py_None) {
138 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
139 return NULL;
140 errno = read_history(PyBytes_AsString(filename_bytes));
141 Py_DECREF(filename_bytes);
142 } else
143 errno = read_history(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 if (errno)
145 return PyErr_SetFromErrno(PyExc_IOError);
146 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000147}
148
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000149static int _history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000150PyDoc_STRVAR(doc_read_history_file,
151"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000152Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000153The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000154
155
156/* Exported function to save a readline history file */
157
158static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000159write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000160{
Victor Stinner19e65a32010-06-11 22:27:14 +0000161 PyObject *filename_obj = Py_None, *filename_bytes;
162 char *filename;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100163 int err;
Victor Stinner19e65a32010-06-11 22:27:14 +0000164 if (!PyArg_ParseTuple(args, "|O:write_history_file", &filename_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 return NULL;
Victor Stinner19e65a32010-06-11 22:27:14 +0000166 if (filename_obj != Py_None) {
167 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
168 return NULL;
169 filename = PyBytes_AsString(filename_bytes);
170 } else {
171 filename_bytes = NULL;
172 filename = NULL;
173 }
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100174 errno = err = write_history(filename);
175 if (!err && _history_length >= 0)
Victor Stinner19e65a32010-06-11 22:27:14 +0000176 history_truncate_file(filename, _history_length);
177 Py_XDECREF(filename_bytes);
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100178 errno = err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179 if (errno)
180 return PyErr_SetFromErrno(PyExc_IOError);
181 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000182}
183
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000184PyDoc_STRVAR(doc_write_history_file,
185"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000186Save a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000187The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000188
189
Guido van Rossum74f31432003-01-07 20:01:29 +0000190/* Set history length */
191
192static PyObject*
193set_history_length(PyObject *self, PyObject *args)
194{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 int length = _history_length;
196 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
197 return NULL;
198 _history_length = length;
199 Py_RETURN_NONE;
Guido van Rossum74f31432003-01-07 20:01:29 +0000200}
201
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000202PyDoc_STRVAR(set_history_length_doc,
203"set_history_length(length) -> None\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000204set the maximal number of items which will be written to\n\
205the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000206history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000207
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000208
Guido van Rossum74f31432003-01-07 20:01:29 +0000209/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000210
211static PyObject*
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000212get_history_length(PyObject *self, PyObject *noarg)
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 return PyLong_FromLong(_history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000215}
216
Guido van Rossum74f31432003-01-07 20:01:29 +0000217PyDoc_STRVAR(get_history_length_doc,
218"get_history_length() -> int\n\
219return the maximum number of items that will be written to\n\
220the history file.");
221
222
Martin v. Löwis0daad592001-09-30 21:09:59 +0000223/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000224
Martin v. Löwis0daad592001-09-30 21:09:59 +0000225static PyObject *
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000226set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000227{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 PyObject *function = Py_None;
229 char buf[80];
230 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
231 if (!PyArg_ParseTuple(args, buf, &function))
232 return NULL;
233 if (function == Py_None) {
234 Py_XDECREF(*hook_var);
235 *hook_var = NULL;
236 }
237 else if (PyCallable_Check(function)) {
238 PyObject *tmp = *hook_var;
239 Py_INCREF(function);
240 *hook_var = function;
241 Py_XDECREF(tmp);
242 }
243 else {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100244 PyErr_Format(PyExc_TypeError,
245 "set_%.50s(func): argument not callable",
246 funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 return NULL;
248 }
249 Py_RETURN_NONE;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000250}
251
Guido van Rossum74f31432003-01-07 20:01:29 +0000252
Martin v. Löwis0daad592001-09-30 21:09:59 +0000253/* Exported functions to specify hook functions in Python */
254
Thomas Wouters89d996e2007-09-08 17:39:28 +0000255static PyObject *completion_display_matches_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000256static PyObject *startup_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000257
258#ifdef HAVE_RL_PRE_INPUT_HOOK
259static PyObject *pre_input_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000260#endif
261
262static PyObject *
Thomas Wouters89d996e2007-09-08 17:39:28 +0000263set_completion_display_matches_hook(PyObject *self, PyObject *args)
264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 PyObject *result = set_hook("completion_display_matches_hook",
266 &completion_display_matches_hook, args);
Christian Heimes32fbe592007-11-12 15:01:33 +0000267#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 /* We cannot set this hook globally, since it replaces the
269 default completion display. */
270 rl_completion_display_matches_hook =
271 completion_display_matches_hook ?
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000272#if defined(_RL_FUNCTION_TYPEDEF)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000274#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 (VFunction *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000276#endif
Christian Heimes32fbe592007-11-12 15:01:33 +0000277#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 return result;
Christian Heimes32fbe592007-11-12 15:01:33 +0000279
Thomas Wouters89d996e2007-09-08 17:39:28 +0000280}
281
282PyDoc_STRVAR(doc_set_completion_display_matches_hook,
283"set_completion_display_matches_hook([function]) -> None\n\
284Set or remove the completion display function.\n\
285The function is called as\n\
286 function(substitution, [matches], longest_match_length)\n\
287once each time matches need to be displayed.");
288
289static PyObject *
Martin v. Löwis0daad592001-09-30 21:09:59 +0000290set_startup_hook(PyObject *self, PyObject *args)
291{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 return set_hook("startup_hook", &startup_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000293}
294
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000295PyDoc_STRVAR(doc_set_startup_hook,
296"set_startup_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000297Set or remove the startup_hook function.\n\
298The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000299before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000300
Guido van Rossum74f31432003-01-07 20:01:29 +0000301
Martin v. Löwis0daad592001-09-30 21:09:59 +0000302#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000303
304/* Set pre-input hook */
305
Martin v. Löwis0daad592001-09-30 21:09:59 +0000306static PyObject *
307set_pre_input_hook(PyObject *self, PyObject *args)
308{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 return set_hook("pre_input_hook", &pre_input_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000310}
311
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000312PyDoc_STRVAR(doc_set_pre_input_hook,
313"set_pre_input_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000314Set or remove the pre_input_hook function.\n\
315The function is called with no arguments after the first prompt\n\
316has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000317characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000318
Martin v. Löwis0daad592001-09-30 21:09:59 +0000319#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000320
Guido van Rossum74f31432003-01-07 20:01:29 +0000321
Guido van Rossum290900a1997-09-26 21:51:21 +0000322/* Exported function to specify a word completer in Python */
323
324static PyObject *completer = NULL;
Guido van Rossum290900a1997-09-26 21:51:21 +0000325
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000326static PyObject *begidx = NULL;
327static PyObject *endidx = NULL;
328
Guido van Rossum74f31432003-01-07 20:01:29 +0000329
Thomas Wouters89d996e2007-09-08 17:39:28 +0000330/* Get the completion type for the scope of the tab-completion */
331static PyObject *
332get_completion_type(PyObject *self, PyObject *noarg)
333{
Christian Heimes217cfd12007-12-02 14:31:20 +0000334 return PyLong_FromLong(rl_completion_type);
Thomas Wouters89d996e2007-09-08 17:39:28 +0000335}
336
337PyDoc_STRVAR(doc_get_completion_type,
338"get_completion_type() -> int\n\
339Get the type of completion being attempted.");
340
341
Guido van Rossum74f31432003-01-07 20:01:29 +0000342/* Get the beginning index for the scope of the tab-completion */
343
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000344static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000345get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000346{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 Py_INCREF(begidx);
348 return begidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000349}
350
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000351PyDoc_STRVAR(doc_get_begidx,
352"get_begidx() -> int\n\
353get the beginning index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000354
Guido van Rossum74f31432003-01-07 20:01:29 +0000355
356/* Get the ending index for the scope of the tab-completion */
357
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000358static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000359get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 Py_INCREF(endidx);
362 return endidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000363}
364
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000365PyDoc_STRVAR(doc_get_endidx,
366"get_endidx() -> int\n\
367get the ending index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000368
369
Guido van Rossum74f31432003-01-07 20:01:29 +0000370/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000371
372static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000373set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000374{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 char *break_chars;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000376
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200377 if (!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 return NULL;
379 }
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200380 /* Keep a reference to the allocated memory in the module state in case
381 some other module modifies rl_completer_word_break_characters
382 (see issue #17289). */
383 free(completer_word_break_characters);
384 completer_word_break_characters = strdup(break_chars);
385 if (completer_word_break_characters) {
386 rl_completer_word_break_characters = completer_word_break_characters;
387 Py_RETURN_NONE;
388 }
389 else
390 return PyErr_NoMemory();
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000391}
392
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000393PyDoc_STRVAR(doc_set_completer_delims,
394"set_completer_delims(string) -> None\n\
395set the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000396
Mark Dickinson29b238e2010-08-03 16:08:16 +0000397/* _py_free_history_entry: Utility function to free a history entry. */
398
399#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
400
401/* Readline version >= 5.0 introduced a timestamp field into the history entry
402 structure; this needs to be freed to avoid a memory leak. This version of
403 readline also introduced the handy 'free_history_entry' function, which
404 takes care of the timestamp. */
405
406static void
407_py_free_history_entry(HIST_ENTRY *entry)
408{
409 histdata_t data = free_history_entry(entry);
410 free(data);
411}
412
413#else
414
415/* No free_history_entry function; free everything manually. */
416
417static void
418_py_free_history_entry(HIST_ENTRY *entry)
419{
420 if (entry->line)
421 free((void *)entry->line);
422 if (entry->data)
423 free(entry->data);
424 free(entry);
425}
426
427#endif
428
Skip Montanaroe5069012004-08-15 14:32:06 +0000429static PyObject *
430py_remove_history(PyObject *self, PyObject *args)
431{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 int entry_number;
433 HIST_ENTRY *entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
436 return NULL;
437 if (entry_number < 0) {
438 PyErr_SetString(PyExc_ValueError,
439 "History index cannot be negative");
440 return NULL;
441 }
442 entry = remove_history(entry_number);
443 if (!entry) {
444 PyErr_Format(PyExc_ValueError,
445 "No history item at position %d",
446 entry_number);
447 return NULL;
448 }
449 /* free memory allocated for the history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000450 _py_free_history_entry(entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000452}
453
454PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000455"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000456remove history item given by its position");
457
458static PyObject *
459py_replace_history(PyObject *self, PyObject *args)
460{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 int entry_number;
462 char *line;
463 HIST_ENTRY *old_entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number,
466 &line)) {
467 return NULL;
468 }
469 if (entry_number < 0) {
470 PyErr_SetString(PyExc_ValueError,
471 "History index cannot be negative");
472 return NULL;
473 }
474 old_entry = replace_history_entry(entry_number, line, (void *)NULL);
475 if (!old_entry) {
476 PyErr_Format(PyExc_ValueError,
477 "No history item at position %d",
478 entry_number);
479 return NULL;
480 }
481 /* free memory allocated for the old history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000482 _py_free_history_entry(old_entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000484}
485
486PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000487"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000488replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000489
490/* Add a line to the history buffer */
491
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000492static PyObject *
493py_add_history(PyObject *self, PyObject *args)
494{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 char *line;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
498 return NULL;
499 }
500 add_history(line);
501 Py_RETURN_NONE;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000502}
503
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000504PyDoc_STRVAR(doc_add_history,
505"add_history(string) -> None\n\
506add a line to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000507
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000508
Guido van Rossum74f31432003-01-07 20:01:29 +0000509/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000510
511static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000512get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000513{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 return PyUnicode_FromString(rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000515}
Guido van Rossum74f31432003-01-07 20:01:29 +0000516
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000517PyDoc_STRVAR(doc_get_completer_delims,
518"get_completer_delims() -> string\n\
519get the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000520
Guido van Rossum74f31432003-01-07 20:01:29 +0000521
522/* Set the completer function */
523
Guido van Rossum290900a1997-09-26 21:51:21 +0000524static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000525set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000526{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 return set_hook("completer", &completer, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000528}
529
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000530PyDoc_STRVAR(doc_set_completer,
531"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000532Set or remove the completer function.\n\
533The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000534for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000535It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000536
Guido van Rossum74f31432003-01-07 20:01:29 +0000537
Michael W. Hudson796df152003-01-30 10:12:51 +0000538static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000539get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000540{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 if (completer == NULL) {
542 Py_RETURN_NONE;
543 }
544 Py_INCREF(completer);
545 return completer;
Michael W. Hudson796df152003-01-30 10:12:51 +0000546}
547
548PyDoc_STRVAR(doc_get_completer,
549"get_completer() -> function\n\
550\n\
551Returns current completer function.");
552
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000553/* Private function to get current length of history. XXX It may be
554 * possible to replace this with a direct use of history_length instead,
555 * but it's not clear whether BSD's libedit keeps history_length up to date.
556 * See issue #8065.*/
557
558static int
559_py_get_history_length(void)
560{
561 HISTORY_STATE *hist_st = history_get_history_state();
562 int length = hist_st->length;
563 /* the history docs don't say so, but the address of hist_st changes each
564 time history_get_history_state is called which makes me think it's
565 freshly malloc'd memory... on the other hand, the address of the last
566 line stays the same as long as history isn't extended, so it appears to
567 be malloc'd but managed by the history package... */
568 free(hist_st);
569 return length;
570}
571
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000572/* Exported function to get any element of history */
573
574static PyObject *
575get_history_item(PyObject *self, PyObject *args)
576{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 int idx = 0;
578 HIST_ENTRY *hist_ent;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 if (!PyArg_ParseTuple(args, "i:index", &idx))
581 return NULL;
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000582#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 if (using_libedit_emulation) {
Ned Deily5d4121a2013-10-12 15:47:58 -0700584 /* Older versions of libedit's readline emulation
585 * use 0-based indexes, while readline and newer
586 * versions of libedit use 1-based indexes.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 */
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000588 int length = _py_get_history_length();
Ned Deily5d4121a2013-10-12 15:47:58 -0700589
590 idx = idx - 1 + libedit_history_start;
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 /*
593 * Apple's readline emulation crashes when
594 * the index is out of range, therefore
595 * test for that and fail gracefully.
596 */
Ned Deily5d4121a2013-10-12 15:47:58 -0700597 if (idx < (0 + libedit_history_start)
598 || idx >= (length + libedit_history_start)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 Py_RETURN_NONE;
600 }
601 }
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000602#endif /* __APPLE__ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 if ((hist_ent = history_get(idx)))
604 return PyUnicode_FromString(hist_ent->line);
605 else {
606 Py_RETURN_NONE;
607 }
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000608}
609
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000610PyDoc_STRVAR(doc_get_history_item,
611"get_history_item() -> string\n\
612return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000613
Guido van Rossum74f31432003-01-07 20:01:29 +0000614
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000615/* Exported function to get current length of history */
616
617static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000618get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000619{
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000620 return PyLong_FromLong((long)_py_get_history_length());
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000621}
622
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000623PyDoc_STRVAR(doc_get_current_history_length,
624"get_current_history_length() -> integer\n\
625return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000626
Guido van Rossum74f31432003-01-07 20:01:29 +0000627
Guido van Rossum79378ff1997-10-07 14:53:21 +0000628/* Exported function to read the current line buffer */
629
630static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000631get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000632{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 return PyUnicode_FromString(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000634}
635
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000636PyDoc_STRVAR(doc_get_line_buffer,
637"get_line_buffer() -> string\n\
638return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000639
Guido van Rossum74f31432003-01-07 20:01:29 +0000640
Martin v. Löwise7a97962003-09-20 16:08:33 +0000641#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
642
643/* Exported function to clear the current history */
644
645static PyObject *
646py_clear_history(PyObject *self, PyObject *noarg)
647{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 clear_history();
649 Py_RETURN_NONE;
Martin v. Löwise7a97962003-09-20 16:08:33 +0000650}
651
652PyDoc_STRVAR(doc_clear_history,
653"clear_history() -> None\n\
654Clear the current readline history.");
655#endif
656
657
Guido van Rossum79378ff1997-10-07 14:53:21 +0000658/* Exported function to insert text into the line buffer */
659
660static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000661insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000662{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 char *s;
664 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
665 return NULL;
666 rl_insert_text(s);
667 Py_RETURN_NONE;
Guido van Rossum79378ff1997-10-07 14:53:21 +0000668}
669
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000670PyDoc_STRVAR(doc_insert_text,
671"insert_text(string) -> None\n\
672Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000673
Guido van Rossum74f31432003-01-07 20:01:29 +0000674
675/* Redisplay the line buffer */
676
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000677static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000678redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000679{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 rl_redisplay();
681 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000682}
683
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000684PyDoc_STRVAR(doc_redisplay,
685"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000686Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000687contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000688
Guido van Rossum74f31432003-01-07 20:01:29 +0000689
Guido van Rossum290900a1997-09-26 21:51:21 +0000690/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000691
692static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000693{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
695 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
696 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
697 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
698 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
699 {"read_history_file", read_history_file,
700 METH_VARARGS, doc_read_history_file},
701 {"write_history_file", write_history_file,
702 METH_VARARGS, doc_write_history_file},
703 {"get_history_item", get_history_item,
704 METH_VARARGS, doc_get_history_item},
705 {"get_current_history_length", (PyCFunction)get_current_history_length,
706 METH_NOARGS, doc_get_current_history_length},
707 {"set_history_length", set_history_length,
708 METH_VARARGS, set_history_length_doc},
709 {"get_history_length", get_history_length,
710 METH_NOARGS, get_history_length_doc},
711 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
712 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
713 {"get_completion_type", get_completion_type,
714 METH_NOARGS, doc_get_completion_type},
715 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
716 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 {"set_completer_delims", set_completer_delims,
719 METH_VARARGS, doc_set_completer_delims},
720 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
721 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
722 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
723 {"get_completer_delims", get_completer_delims,
724 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000725
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 {"set_completion_display_matches_hook", set_completion_display_matches_hook,
727 METH_VARARGS, doc_set_completion_display_matches_hook},
728 {"set_startup_hook", set_startup_hook,
729 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000730#ifdef HAVE_RL_PRE_INPUT_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 {"set_pre_input_hook", set_pre_input_hook,
732 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000733#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000734#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
Martin v. Löwise7a97962003-09-20 16:08:33 +0000736#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000738};
739
Guido van Rossum05ac4492003-01-07 20:04:12 +0000740
Martin v. Löwis0daad592001-09-30 21:09:59 +0000741/* C function to call the Python hooks. */
742
743static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000744on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000745{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 int result = 0;
747 if (func != NULL) {
748 PyObject *r;
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000749#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000751#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 r = PyObject_CallFunction(func, NULL);
753 if (r == NULL)
754 goto error;
755 if (r == Py_None)
756 result = 0;
757 else {
758 result = PyLong_AsLong(r);
759 if (result == -1 && PyErr_Occurred())
760 goto error;
761 }
762 Py_DECREF(r);
763 goto done;
764 error:
765 PyErr_Clear();
766 Py_XDECREF(r);
767 done:
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000768#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 PyGILState_Release(gilstate);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000770#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 return result;
772 }
773 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000774}
775
776static int
777on_startup_hook(void)
778{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 return on_hook(startup_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000780}
781
782#ifdef HAVE_RL_PRE_INPUT_HOOK
783static int
784on_pre_input_hook(void)
785{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 return on_hook(pre_input_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000787}
788#endif
789
Guido van Rossum05ac4492003-01-07 20:04:12 +0000790
Thomas Wouters89d996e2007-09-08 17:39:28 +0000791/* C function to call the Python completion_display_matches */
792
Georg Brandl646fdd62010-10-18 07:27:55 +0000793#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Thomas Wouters89d996e2007-09-08 17:39:28 +0000794static void
795on_completion_display_matches_hook(char **matches,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 int num_matches, int max_length)
Thomas Wouters89d996e2007-09-08 17:39:28 +0000797{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 int i;
799 PyObject *m=NULL, *s=NULL, *r=NULL;
Christian Heimesaec75c32007-11-11 22:42:36 +0000800#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 PyGILState_STATE gilstate = PyGILState_Ensure();
Thomas Wouters89d996e2007-09-08 17:39:28 +0000802#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 m = PyList_New(num_matches);
804 if (m == NULL)
805 goto error;
806 for (i = 0; i < num_matches; i++) {
807 s = PyUnicode_FromString(matches[i+1]);
808 if (s == NULL)
809 goto error;
810 if (PyList_SetItem(m, i, s) == -1)
811 goto error;
812 }
813 r = PyObject_CallFunction(completion_display_matches_hook,
814 "sOi", matches[0], m, max_length);
Christian Heimes32fbe592007-11-12 15:01:33 +0000815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 Py_DECREF(m); m=NULL;
Christian Heimes32fbe592007-11-12 15:01:33 +0000817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 if (r == NULL ||
819 (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) {
820 goto error;
821 }
822 Py_XDECREF(r); r=NULL;
823
824 if (0) {
825 error:
826 PyErr_Clear();
827 Py_XDECREF(m);
828 Py_XDECREF(r);
829 }
Christian Heimes32fbe592007-11-12 15:01:33 +0000830#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 PyGILState_Release(gilstate);
Christian Heimes32fbe592007-11-12 15:01:33 +0000832#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000833}
834
Senthil Kumaran95c07002010-11-04 03:51:05 +0000835#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000836
Guido van Rossum290900a1997-09-26 21:51:21 +0000837/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000838
Guido van Rossum290900a1997-09-26 21:51:21 +0000839static char *
Guido van Rossumd8faa362007-04-27 19:54:29 +0000840on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000841{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 char *result = NULL;
843 if (completer != NULL) {
844 PyObject *r;
Christian Heimesaec75c32007-11-11 22:42:36 +0000845#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000847#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 rl_attempted_completion_over = 1;
849 r = PyObject_CallFunction(completer, "si", text, state);
850 if (r == NULL)
851 goto error;
852 if (r == Py_None) {
853 result = NULL;
854 }
855 else {
856 char *s = _PyUnicode_AsString(r);
857 if (s == NULL)
858 goto error;
859 result = strdup(s);
860 }
861 Py_DECREF(r);
862 goto done;
863 error:
864 PyErr_Clear();
865 Py_XDECREF(r);
866 done:
Christian Heimesaec75c32007-11-11 22:42:36 +0000867#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 PyGILState_Release(gilstate);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000869#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 return result;
871 }
872 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000873}
874
Guido van Rossum290900a1997-09-26 21:51:21 +0000875
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000876/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000877 * before calling the normal completer */
878
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000879static char **
Benjamin Petersonf0b463a2014-01-24 11:44:16 -0500880flex_complete(const char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000881{
Antoine Pitroudc0900b2009-10-19 18:22:37 +0000882#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 rl_completion_append_character ='\0';
Antoine Pitroud5131772009-10-26 19:22:14 +0000884#endif
885#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 rl_completion_suppress_append = 0;
Antoine Pitroudc0900b2009-10-19 18:22:37 +0000887#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 Py_XDECREF(begidx);
889 Py_XDECREF(endidx);
890 begidx = PyLong_FromLong((long) start);
891 endidx = PyLong_FromLong((long) end);
892 return completion_matches(text, *on_completion);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000893}
894
Guido van Rossum05ac4492003-01-07 20:04:12 +0000895
Guido van Rossum290900a1997-09-26 21:51:21 +0000896/* Helper to initialize GNU readline properly. */
897
898static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000899setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000900{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000901#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
903 if (!saved_locale)
904 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000905#endif
906
R. David Murray52d1b4e2010-12-18 03:48:32 +0000907#ifdef __APPLE__
Victor Stinner6ced7c42011-03-21 18:15:42 +0100908 /* the libedit readline emulation resets key bindings etc
R. David Murray52d1b4e2010-12-18 03:48:32 +0000909 * when calling rl_initialize. So call it upfront
910 */
911 if (using_libedit_emulation)
912 rl_initialize();
Ned Deily5d4121a2013-10-12 15:47:58 -0700913
914 /* Detect if libedit's readline emulation uses 0-based
915 * indexing or 1-based indexing.
916 */
917 add_history("1");
918 if (history_get(1) == NULL) {
919 libedit_history_start = 0;
920 } else {
921 libedit_history_start = 1;
922 }
923 clear_history();
R. David Murray52d1b4e2010-12-18 03:48:32 +0000924#endif /* __APPLE__ */
925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 using_history();
Skip Montanaroa0392742002-06-11 14:32:46 +0000927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000929#if defined(PYOS_OS2) && defined(PYCC_GCC)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 /* Allow $if term= in .inputrc to work */
931 rl_terminal_name = getenv("TERM");
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000932#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 /* Force rebind of TAB to insert-tab */
934 rl_bind_key('\t', rl_insert);
935 /* Bind both ESC-TAB and ESC-ESC to the completion function */
936 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
937 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
938 /* Set our hook functions */
Benjamin Petersonf0b463a2014-01-24 11:44:16 -0500939 rl_startup_hook = on_startup_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000940#ifdef HAVE_RL_PRE_INPUT_HOOK
Benjamin Petersonf0b463a2014-01-24 11:44:16 -0500941 rl_pre_input_hook = on_pre_input_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000942#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 /* Set our completion function */
Benjamin Petersonf0b463a2014-01-24 11:44:16 -0500944 rl_attempted_completion_function = flex_complete;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 /* Set Python word break characters */
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200946 completer_word_break_characters =
947 rl_completer_word_break_characters =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
949 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 begidx = PyLong_FromLong(0L);
952 endidx = PyLong_FromLong(0L);
953 /* Initialize (allows .inputrc to override)
954 *
955 * XXX: A bug in the readline-2.2 library causes a memory leak
956 * inside this function. Nothing we can do about it.
957 */
R. David Murray52d1b4e2010-12-18 03:48:32 +0000958#ifdef __APPLE__
959 if (using_libedit_emulation)
Victor Stinner6ced7c42011-03-21 18:15:42 +0100960 rl_read_init_file(NULL);
R. David Murray52d1b4e2010-12-18 03:48:32 +0000961 else
962#endif /* __APPLE__ */
963 rl_initialize();
Victor Stinner6ced7c42011-03-21 18:15:42 +0100964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 RESTORE_LOCALE(saved_locale)
Guido van Rossum290900a1997-09-26 21:51:21 +0000966}
967
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000968/* Wrapper around GNU readline that handles signals differently. */
969
970
971#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973static char *completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000974static void
975rlhandler(char *text)
976{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 completed_input_string = text;
978 rl_callback_handler_remove();
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000979}
980
981extern PyThreadState* _PyOS_ReadlineTState;
982
983static char *
984readline_until_enter_or_signal(char *prompt, int *signal)
985{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 char * not_done_reading = "";
987 fd_set selectset;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 *signal = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000990#ifdef HAVE_RL_CATCH_SIGNAL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 rl_catch_signals = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000992#endif
993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 rl_callback_handler_install (prompt, rlhandler);
995 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 completed_input_string = not_done_reading;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 while (completed_input_string == not_done_reading) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001000 int has_input = 0, err = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 while (!has_input)
1003 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Michael W. Hudson8da2b012004-10-07 13:46:33 +00001004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 /* [Bug #1552726] Only limit the pause if an input hook has been
1006 defined. */
1007 struct timeval *timeoutp = NULL;
1008 if (PyOS_InputHook)
1009 timeoutp = &timeout;
1010 FD_SET(fileno(rl_instream), &selectset);
1011 /* select resets selectset if no input was available */
1012 has_input = select(fileno(rl_instream) + 1, &selectset,
1013 NULL, NULL, timeoutp);
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001014 err = errno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 if(PyOS_InputHook) PyOS_InputHook();
1016 }
1017
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001018 if (has_input > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 rl_callback_read_char();
1020 }
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001021 else if (err == EINTR) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001023#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001025#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001027#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001029#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 if (s < 0) {
1031 rl_free_line_state();
1032 rl_cleanup_after_signal();
1033 rl_callback_handler_remove();
1034 *signal = 1;
1035 completed_input_string = NULL;
1036 }
1037 }
1038 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 return completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001041}
1042
1043
1044#else
Guido van Rossum290900a1997-09-26 21:51:21 +00001045
1046/* Interrupt handler */
1047
1048static jmp_buf jbuf;
1049
Guido van Rossum0969d361997-08-05 21:27:50 +00001050/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +00001051static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001052onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +00001053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +00001055}
1056
Guido van Rossum290900a1997-09-26 21:51:21 +00001057
Guido van Rossum0969d361997-08-05 21:27:50 +00001058static char *
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001059readline_until_enter_or_signal(char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +00001060{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 PyOS_sighandler_t old_inthandler;
1062 char *p;
Guido van Rossum74f31432003-01-07 20:01:29 +00001063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 *signal = 0;
1065
1066 old_inthandler = PyOS_setsig(SIGINT, onintr);
1067 if (setjmp(jbuf)) {
Guido van Rossum0969d361997-08-05 21:27:50 +00001068#ifdef HAVE_SIGRELSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
1070 sigrelse(SIGINT);
Guido van Rossum0969d361997-08-05 21:27:50 +00001071#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 PyOS_setsig(SIGINT, old_inthandler);
1073 *signal = 1;
1074 return NULL;
1075 }
1076 rl_event_hook = PyOS_InputHook;
1077 p = readline(prompt);
1078 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001079
1080 return p;
1081}
1082#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
1083
1084
1085static char *
1086call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
1087{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 size_t n;
1089 char *p, *q;
1090 int signal;
Neal Norwitz1fa040b2004-08-25 01:20:18 +00001091
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001092#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1094 if (!saved_locale)
1095 Py_FatalError("not enough memory to save locale");
Nadeem Vawda63752572013-02-02 20:05:11 +01001096 setlocale(LC_CTYPE, "");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001097#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
1100 rl_instream = sys_stdin;
1101 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001102#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001104#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 }
Guido van Rossum74f31432003-01-07 20:01:29 +00001106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 p = readline_until_enter_or_signal(prompt, &signal);
Guido van Rossumb18618d2000-05-03 23:44:39 +00001108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 /* we got an interrupt signal */
1110 if (signal) {
1111 RESTORE_LOCALE(saved_locale)
1112 return NULL;
1113 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 /* We got an EOF, return a empty string. */
1116 if (p == NULL) {
1117 p = PyMem_Malloc(1);
1118 if (p != NULL)
1119 *p = '\0';
1120 RESTORE_LOCALE(saved_locale)
1121 return p;
1122 }
1123
1124 /* we have a valid line */
1125 n = strlen(p);
1126 if (n > 0) {
Brett Cannon2525dc82010-08-22 20:36:25 +00001127 const char *line;
Mark Dickinson6b54e1f2010-08-03 16:49:49 +00001128 int length = _py_get_history_length();
1129 if (length > 0)
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001130#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 if (using_libedit_emulation) {
Ned Deily5d4121a2013-10-12 15:47:58 -07001132 /* handle older 0-based or newer 1-based indexing */
1133 line = (const char *)history_get(length + libedit_history_start - 1)->line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 } else
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001135#endif /* __APPLE__ */
Brett Cannon2525dc82010-08-22 20:36:25 +00001136 line = (const char *)history_get(length)->line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 else
1138 line = "";
1139 if (strcmp(p, line))
1140 add_history(p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 }
1142 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
1143 release the original. */
1144 q = p;
1145 p = PyMem_Malloc(n+2);
1146 if (p != NULL) {
1147 strncpy(p, q, n);
1148 p[n] = '\n';
1149 p[n+1] = '\0';
1150 }
1151 free(q);
1152 RESTORE_LOCALE(saved_locale)
1153 return p;
Guido van Rossum0969d361997-08-05 21:27:50 +00001154}
1155
Guido van Rossum290900a1997-09-26 21:51:21 +00001156
1157/* Initialize the module */
1158
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001159PyDoc_STRVAR(doc_module,
1160"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001161
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001162#ifdef __APPLE__
1163PyDoc_STRVAR(doc_module_le,
1164"Importing this module enables command line editing using libedit readline.");
1165#endif /* __APPLE__ */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001166
1167static struct PyModuleDef readlinemodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 PyModuleDef_HEAD_INIT,
1169 "readline",
1170 doc_module,
1171 -1,
1172 readline_methods,
1173 NULL,
1174 NULL,
1175 NULL,
1176 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001177};
1178
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001179
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001180PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001181PyInit_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +00001184
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001185#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
1187 using_libedit_emulation = 1;
1188 }
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 if (using_libedit_emulation)
1191 readlinemodule.m_doc = doc_module_le;
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001192
1193#endif /* __APPLE__ */
1194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 m = PyModule_Create(&readlinemodule);
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 if (m == NULL)
1198 return NULL;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 PyOS_ReadlineFunctionPointer = call_readline;
1201 setup_readline();
1202 return m;
Guido van Rossum0969d361997-08-05 21:27:50 +00001203}