blob: a5e48ab70c27dfd20f73c44c8f803dd9017c4429 [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 *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 * Currently there is one know API incompatibility:
Ronald Oussoren2efd9242009-09-20 14:53:22 +000058 * - 'get_history' has a 1-based index with GNU readline, and a 0-based
59 * index with libedit's emulation.
60 * - Note that replace_history and remove_history use a 0-based index
61 * with both implementation.
62 */
63static int using_libedit_emulation = 0;
64static const char libedit_version_tag[] = "EditLine wrapper";
65#endif /* __APPLE__ */
66
Georg Brandl646fdd62010-10-18 07:27:55 +000067#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Christian Heimes32fbe592007-11-12 15:01:33 +000068static void
69on_completion_display_matches_hook(char **matches,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 int num_matches, int max_length);
Georg Brandl646fdd62010-10-18 07:27:55 +000071#endif
Guido van Rossum0969d361997-08-05 21:27:50 +000072
Guido van Rossum290900a1997-09-26 21:51:21 +000073/* Exported function to send one line to readline's init file parser */
74
75static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000076parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000077{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 char *s, *copy;
79 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
80 return NULL;
81 /* Make a copy -- rl_parse_and_bind() modifies its argument */
82 /* Bernard Herzog */
83 copy = malloc(1 + strlen(s));
84 if (copy == NULL)
85 return PyErr_NoMemory();
86 strcpy(copy, s);
87 rl_parse_and_bind(copy);
88 free(copy); /* Free the copy */
89 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +000090}
91
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000092PyDoc_STRVAR(doc_parse_and_bind,
93"parse_and_bind(string) -> None\n\
94Parse and execute single line of a readline init file.");
Guido van Rossum290900a1997-09-26 21:51:21 +000095
96
97/* Exported function to parse a readline init file */
98
99static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000100read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000101{
Victor Stinner19e65a32010-06-11 22:27:14 +0000102 PyObject *filename_obj = Py_None, *filename_bytes;
103 if (!PyArg_ParseTuple(args, "|O:read_init_file", &filename_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 return NULL;
Victor Stinner19e65a32010-06-11 22:27:14 +0000105 if (filename_obj != Py_None) {
106 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
107 return NULL;
108 errno = rl_read_init_file(PyBytes_AsString(filename_bytes));
109 Py_DECREF(filename_bytes);
110 } else
111 errno = rl_read_init_file(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 if (errno)
113 return PyErr_SetFromErrno(PyExc_IOError);
114 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +0000115}
116
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000117PyDoc_STRVAR(doc_read_init_file,
118"read_init_file([filename]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000119Parse a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000120The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000121
122
Skip Montanaro28067822000-07-06 18:55:12 +0000123/* Exported function to load a readline history file */
124
125static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000126read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000127{
Victor Stinner19e65a32010-06-11 22:27:14 +0000128 PyObject *filename_obj = Py_None, *filename_bytes;
129 if (!PyArg_ParseTuple(args, "|O:read_history_file", &filename_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 return NULL;
Victor Stinner19e65a32010-06-11 22:27:14 +0000131 if (filename_obj != Py_None) {
132 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
133 return NULL;
134 errno = read_history(PyBytes_AsString(filename_bytes));
135 Py_DECREF(filename_bytes);
136 } else
137 errno = read_history(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 if (errno)
139 return PyErr_SetFromErrno(PyExc_IOError);
140 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000141}
142
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000143static int _history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000144PyDoc_STRVAR(doc_read_history_file,
145"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000146Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000147The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000148
149
150/* Exported function to save a readline history file */
151
152static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000153write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000154{
Victor Stinner19e65a32010-06-11 22:27:14 +0000155 PyObject *filename_obj = Py_None, *filename_bytes;
156 char *filename;
157 if (!PyArg_ParseTuple(args, "|O:write_history_file", &filename_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 return NULL;
Victor Stinner19e65a32010-06-11 22:27:14 +0000159 if (filename_obj != Py_None) {
160 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
161 return NULL;
162 filename = PyBytes_AsString(filename_bytes);
163 } else {
164 filename_bytes = NULL;
165 filename = NULL;
166 }
167 errno = write_history(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 if (!errno && _history_length >= 0)
Victor Stinner19e65a32010-06-11 22:27:14 +0000169 history_truncate_file(filename, _history_length);
170 Py_XDECREF(filename_bytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 if (errno)
172 return PyErr_SetFromErrno(PyExc_IOError);
173 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000174}
175
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000176PyDoc_STRVAR(doc_write_history_file,
177"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000178Save a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000179The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000180
181
Guido van Rossum74f31432003-01-07 20:01:29 +0000182/* Set history length */
183
184static PyObject*
185set_history_length(PyObject *self, PyObject *args)
186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 int length = _history_length;
188 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
189 return NULL;
190 _history_length = length;
191 Py_RETURN_NONE;
Guido van Rossum74f31432003-01-07 20:01:29 +0000192}
193
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000194PyDoc_STRVAR(set_history_length_doc,
195"set_history_length(length) -> None\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000196set the maximal number of items which will be written to\n\
197the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000198history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000199
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000200
Guido van Rossum74f31432003-01-07 20:01:29 +0000201/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000202
203static PyObject*
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000204get_history_length(PyObject *self, PyObject *noarg)
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 return PyLong_FromLong(_history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000207}
208
Guido van Rossum74f31432003-01-07 20:01:29 +0000209PyDoc_STRVAR(get_history_length_doc,
210"get_history_length() -> int\n\
211return the maximum number of items that will be written to\n\
212the history file.");
213
214
Martin v. Löwis0daad592001-09-30 21:09:59 +0000215/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000216
Martin v. Löwis0daad592001-09-30 21:09:59 +0000217static PyObject *
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000218set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000219{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 PyObject *function = Py_None;
221 char buf[80];
222 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
223 if (!PyArg_ParseTuple(args, buf, &function))
224 return NULL;
225 if (function == Py_None) {
226 Py_XDECREF(*hook_var);
227 *hook_var = NULL;
228 }
229 else if (PyCallable_Check(function)) {
230 PyObject *tmp = *hook_var;
231 Py_INCREF(function);
232 *hook_var = function;
233 Py_XDECREF(tmp);
234 }
235 else {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100236 PyErr_Format(PyExc_TypeError,
237 "set_%.50s(func): argument not callable",
238 funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 return NULL;
240 }
241 Py_RETURN_NONE;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000242}
243
Guido van Rossum74f31432003-01-07 20:01:29 +0000244
Martin v. Löwis0daad592001-09-30 21:09:59 +0000245/* Exported functions to specify hook functions in Python */
246
Thomas Wouters89d996e2007-09-08 17:39:28 +0000247static PyObject *completion_display_matches_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000248static PyObject *startup_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000249
250#ifdef HAVE_RL_PRE_INPUT_HOOK
251static PyObject *pre_input_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000252#endif
253
254static PyObject *
Thomas Wouters89d996e2007-09-08 17:39:28 +0000255set_completion_display_matches_hook(PyObject *self, PyObject *args)
256{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 PyObject *result = set_hook("completion_display_matches_hook",
258 &completion_display_matches_hook, args);
Christian Heimes32fbe592007-11-12 15:01:33 +0000259#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 /* We cannot set this hook globally, since it replaces the
261 default completion display. */
262 rl_completion_display_matches_hook =
263 completion_display_matches_hook ?
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000264#if defined(_RL_FUNCTION_TYPEDEF)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000266#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 (VFunction *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000268#endif
Christian Heimes32fbe592007-11-12 15:01:33 +0000269#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 return result;
Christian Heimes32fbe592007-11-12 15:01:33 +0000271
Thomas Wouters89d996e2007-09-08 17:39:28 +0000272}
273
274PyDoc_STRVAR(doc_set_completion_display_matches_hook,
275"set_completion_display_matches_hook([function]) -> None\n\
276Set or remove the completion display function.\n\
277The function is called as\n\
278 function(substitution, [matches], longest_match_length)\n\
279once each time matches need to be displayed.");
280
281static PyObject *
Martin v. Löwis0daad592001-09-30 21:09:59 +0000282set_startup_hook(PyObject *self, PyObject *args)
283{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 return set_hook("startup_hook", &startup_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_startup_hook,
288"set_startup_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000289Set or remove the startup_hook function.\n\
290The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000291before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000292
Guido van Rossum74f31432003-01-07 20:01:29 +0000293
Martin v. Löwis0daad592001-09-30 21:09:59 +0000294#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000295
296/* Set pre-input hook */
297
Martin v. Löwis0daad592001-09-30 21:09:59 +0000298static PyObject *
299set_pre_input_hook(PyObject *self, PyObject *args)
300{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 return set_hook("pre_input_hook", &pre_input_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000302}
303
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000304PyDoc_STRVAR(doc_set_pre_input_hook,
305"set_pre_input_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000306Set or remove the pre_input_hook function.\n\
307The function is called with no arguments after the first prompt\n\
308has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000309characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000310
Martin v. Löwis0daad592001-09-30 21:09:59 +0000311#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000312
Guido van Rossum74f31432003-01-07 20:01:29 +0000313
Guido van Rossum290900a1997-09-26 21:51:21 +0000314/* Exported function to specify a word completer in Python */
315
316static PyObject *completer = NULL;
Guido van Rossum290900a1997-09-26 21:51:21 +0000317
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000318static PyObject *begidx = NULL;
319static PyObject *endidx = NULL;
320
Guido van Rossum74f31432003-01-07 20:01:29 +0000321
Thomas Wouters89d996e2007-09-08 17:39:28 +0000322/* Get the completion type for the scope of the tab-completion */
323static PyObject *
324get_completion_type(PyObject *self, PyObject *noarg)
325{
Christian Heimes217cfd12007-12-02 14:31:20 +0000326 return PyLong_FromLong(rl_completion_type);
Thomas Wouters89d996e2007-09-08 17:39:28 +0000327}
328
329PyDoc_STRVAR(doc_get_completion_type,
330"get_completion_type() -> int\n\
331Get the type of completion being attempted.");
332
333
Guido van Rossum74f31432003-01-07 20:01:29 +0000334/* Get the beginning index for the scope of the tab-completion */
335
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000336static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000337get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000338{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 Py_INCREF(begidx);
340 return begidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000341}
342
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000343PyDoc_STRVAR(doc_get_begidx,
344"get_begidx() -> int\n\
345get the beginning index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000346
Guido van Rossum74f31432003-01-07 20:01:29 +0000347
348/* Get the ending index for the scope of the tab-completion */
349
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000350static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000351get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000352{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 Py_INCREF(endidx);
354 return endidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000355}
356
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000357PyDoc_STRVAR(doc_get_endidx,
358"get_endidx() -> int\n\
359get the ending index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000360
361
Guido van Rossum74f31432003-01-07 20:01:29 +0000362/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000363
364static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000365set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000366{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 char *break_chars;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
370 return NULL;
371 }
372 free((void*)rl_completer_word_break_characters);
373 rl_completer_word_break_characters = strdup(break_chars);
374 Py_RETURN_NONE;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000375}
376
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000377PyDoc_STRVAR(doc_set_completer_delims,
378"set_completer_delims(string) -> None\n\
379set the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000380
Mark Dickinson29b238e2010-08-03 16:08:16 +0000381/* _py_free_history_entry: Utility function to free a history entry. */
382
383#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
384
385/* Readline version >= 5.0 introduced a timestamp field into the history entry
386 structure; this needs to be freed to avoid a memory leak. This version of
387 readline also introduced the handy 'free_history_entry' function, which
388 takes care of the timestamp. */
389
390static void
391_py_free_history_entry(HIST_ENTRY *entry)
392{
393 histdata_t data = free_history_entry(entry);
394 free(data);
395}
396
397#else
398
399/* No free_history_entry function; free everything manually. */
400
401static void
402_py_free_history_entry(HIST_ENTRY *entry)
403{
404 if (entry->line)
405 free((void *)entry->line);
406 if (entry->data)
407 free(entry->data);
408 free(entry);
409}
410
411#endif
412
Skip Montanaroe5069012004-08-15 14:32:06 +0000413static PyObject *
414py_remove_history(PyObject *self, PyObject *args)
415{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 int entry_number;
417 HIST_ENTRY *entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
420 return NULL;
421 if (entry_number < 0) {
422 PyErr_SetString(PyExc_ValueError,
423 "History index cannot be negative");
424 return NULL;
425 }
426 entry = remove_history(entry_number);
427 if (!entry) {
428 PyErr_Format(PyExc_ValueError,
429 "No history item at position %d",
430 entry_number);
431 return NULL;
432 }
433 /* free memory allocated for the history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000434 _py_free_history_entry(entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000436}
437
438PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000439"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000440remove history item given by its position");
441
442static PyObject *
443py_replace_history(PyObject *self, PyObject *args)
444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 int entry_number;
446 char *line;
447 HIST_ENTRY *old_entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number,
450 &line)) {
451 return NULL;
452 }
453 if (entry_number < 0) {
454 PyErr_SetString(PyExc_ValueError,
455 "History index cannot be negative");
456 return NULL;
457 }
458 old_entry = replace_history_entry(entry_number, line, (void *)NULL);
459 if (!old_entry) {
460 PyErr_Format(PyExc_ValueError,
461 "No history item at position %d",
462 entry_number);
463 return NULL;
464 }
465 /* free memory allocated for the old history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000466 _py_free_history_entry(old_entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000468}
469
470PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000471"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000472replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000473
474/* Add a line to the history buffer */
475
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000476static PyObject *
477py_add_history(PyObject *self, PyObject *args)
478{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 char *line;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
482 return NULL;
483 }
484 add_history(line);
485 Py_RETURN_NONE;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000486}
487
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000488PyDoc_STRVAR(doc_add_history,
489"add_history(string) -> None\n\
490add a line to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000491
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000492
Guido van Rossum74f31432003-01-07 20:01:29 +0000493/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000494
495static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000496get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000497{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 return PyUnicode_FromString(rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000499}
Guido van Rossum74f31432003-01-07 20:01:29 +0000500
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000501PyDoc_STRVAR(doc_get_completer_delims,
502"get_completer_delims() -> string\n\
503get the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000504
Guido van Rossum74f31432003-01-07 20:01:29 +0000505
506/* Set the completer function */
507
Guido van Rossum290900a1997-09-26 21:51:21 +0000508static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000509set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000510{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 return set_hook("completer", &completer, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000512}
513
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000514PyDoc_STRVAR(doc_set_completer,
515"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000516Set or remove the completer function.\n\
517The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000518for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000519It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000520
Guido van Rossum74f31432003-01-07 20:01:29 +0000521
Michael W. Hudson796df152003-01-30 10:12:51 +0000522static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000523get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000524{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 if (completer == NULL) {
526 Py_RETURN_NONE;
527 }
528 Py_INCREF(completer);
529 return completer;
Michael W. Hudson796df152003-01-30 10:12:51 +0000530}
531
532PyDoc_STRVAR(doc_get_completer,
533"get_completer() -> function\n\
534\n\
535Returns current completer function.");
536
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000537/* Private function to get current length of history. XXX It may be
538 * possible to replace this with a direct use of history_length instead,
539 * but it's not clear whether BSD's libedit keeps history_length up to date.
540 * See issue #8065.*/
541
542static int
543_py_get_history_length(void)
544{
545 HISTORY_STATE *hist_st = history_get_history_state();
546 int length = hist_st->length;
547 /* the history docs don't say so, but the address of hist_st changes each
548 time history_get_history_state is called which makes me think it's
549 freshly malloc'd memory... on the other hand, the address of the last
550 line stays the same as long as history isn't extended, so it appears to
551 be malloc'd but managed by the history package... */
552 free(hist_st);
553 return length;
554}
555
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000556/* Exported function to get any element of history */
557
558static PyObject *
559get_history_item(PyObject *self, PyObject *args)
560{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 int idx = 0;
562 HIST_ENTRY *hist_ent;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 if (!PyArg_ParseTuple(args, "i:index", &idx))
565 return NULL;
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000566#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 if (using_libedit_emulation) {
568 /* Libedit emulation uses 0-based indexes,
569 * the real one uses 1-based indexes,
570 * adjust the index to ensure that Python
571 * code doesn't have to worry about the
572 * difference.
573 */
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000574 int length = _py_get_history_length();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 idx --;
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 /*
578 * Apple's readline emulation crashes when
579 * the index is out of range, therefore
580 * test for that and fail gracefully.
581 */
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000582 if (idx < 0 || idx >= length) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 Py_RETURN_NONE;
584 }
585 }
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000586#endif /* __APPLE__ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 if ((hist_ent = history_get(idx)))
588 return PyUnicode_FromString(hist_ent->line);
589 else {
590 Py_RETURN_NONE;
591 }
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000592}
593
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000594PyDoc_STRVAR(doc_get_history_item,
595"get_history_item() -> string\n\
596return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000597
Guido van Rossum74f31432003-01-07 20:01:29 +0000598
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000599/* Exported function to get current length of history */
600
601static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000602get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000603{
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000604 return PyLong_FromLong((long)_py_get_history_length());
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000605}
606
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000607PyDoc_STRVAR(doc_get_current_history_length,
608"get_current_history_length() -> integer\n\
609return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000610
Guido van Rossum74f31432003-01-07 20:01:29 +0000611
Guido van Rossum79378ff1997-10-07 14:53:21 +0000612/* Exported function to read the current line buffer */
613
614static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000615get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 return PyUnicode_FromString(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000618}
619
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000620PyDoc_STRVAR(doc_get_line_buffer,
621"get_line_buffer() -> string\n\
622return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000623
Guido van Rossum74f31432003-01-07 20:01:29 +0000624
Martin v. Löwise7a97962003-09-20 16:08:33 +0000625#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
626
627/* Exported function to clear the current history */
628
629static PyObject *
630py_clear_history(PyObject *self, PyObject *noarg)
631{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 clear_history();
633 Py_RETURN_NONE;
Martin v. Löwise7a97962003-09-20 16:08:33 +0000634}
635
636PyDoc_STRVAR(doc_clear_history,
637"clear_history() -> None\n\
638Clear the current readline history.");
639#endif
640
641
Guido van Rossum79378ff1997-10-07 14:53:21 +0000642/* Exported function to insert text into the line buffer */
643
644static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000645insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000646{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 char *s;
648 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
649 return NULL;
650 rl_insert_text(s);
651 Py_RETURN_NONE;
Guido van Rossum79378ff1997-10-07 14:53:21 +0000652}
653
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000654PyDoc_STRVAR(doc_insert_text,
655"insert_text(string) -> None\n\
656Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000657
Guido van Rossum74f31432003-01-07 20:01:29 +0000658
659/* Redisplay the line buffer */
660
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000661static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000662redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000663{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 rl_redisplay();
665 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000666}
667
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000668PyDoc_STRVAR(doc_redisplay,
669"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000670Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000671contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000672
Guido van Rossum74f31432003-01-07 20:01:29 +0000673
Guido van Rossum290900a1997-09-26 21:51:21 +0000674/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000675
676static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000677{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
679 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
680 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
681 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
682 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
683 {"read_history_file", read_history_file,
684 METH_VARARGS, doc_read_history_file},
685 {"write_history_file", write_history_file,
686 METH_VARARGS, doc_write_history_file},
687 {"get_history_item", get_history_item,
688 METH_VARARGS, doc_get_history_item},
689 {"get_current_history_length", (PyCFunction)get_current_history_length,
690 METH_NOARGS, doc_get_current_history_length},
691 {"set_history_length", set_history_length,
692 METH_VARARGS, set_history_length_doc},
693 {"get_history_length", get_history_length,
694 METH_NOARGS, get_history_length_doc},
695 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
696 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
697 {"get_completion_type", get_completion_type,
698 METH_NOARGS, doc_get_completion_type},
699 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
700 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 {"set_completer_delims", set_completer_delims,
703 METH_VARARGS, doc_set_completer_delims},
704 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
705 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
706 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
707 {"get_completer_delims", get_completer_delims,
708 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000709
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 {"set_completion_display_matches_hook", set_completion_display_matches_hook,
711 METH_VARARGS, doc_set_completion_display_matches_hook},
712 {"set_startup_hook", set_startup_hook,
713 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000714#ifdef HAVE_RL_PRE_INPUT_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 {"set_pre_input_hook", set_pre_input_hook,
716 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000717#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000718#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
Martin v. Löwise7a97962003-09-20 16:08:33 +0000720#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000722};
723
Guido van Rossum05ac4492003-01-07 20:04:12 +0000724
Martin v. Löwis0daad592001-09-30 21:09:59 +0000725/* C function to call the Python hooks. */
726
727static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000728on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000729{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 int result = 0;
731 if (func != NULL) {
732 PyObject *r;
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000733#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000735#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 r = PyObject_CallFunction(func, NULL);
737 if (r == NULL)
738 goto error;
739 if (r == Py_None)
740 result = 0;
741 else {
742 result = PyLong_AsLong(r);
743 if (result == -1 && PyErr_Occurred())
744 goto error;
745 }
746 Py_DECREF(r);
747 goto done;
748 error:
749 PyErr_Clear();
750 Py_XDECREF(r);
751 done:
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000752#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 PyGILState_Release(gilstate);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000754#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 return result;
756 }
757 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000758}
759
760static int
761on_startup_hook(void)
762{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 return on_hook(startup_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000764}
765
766#ifdef HAVE_RL_PRE_INPUT_HOOK
767static int
768on_pre_input_hook(void)
769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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
Thomas Wouters89d996e2007-09-08 17:39:28 +0000775/* C function to call the Python completion_display_matches */
776
Georg Brandl646fdd62010-10-18 07:27:55 +0000777#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Thomas Wouters89d996e2007-09-08 17:39:28 +0000778static void
779on_completion_display_matches_hook(char **matches,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 int num_matches, int max_length)
Thomas Wouters89d996e2007-09-08 17:39:28 +0000781{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 int i;
783 PyObject *m=NULL, *s=NULL, *r=NULL;
Christian Heimesaec75c32007-11-11 22:42:36 +0000784#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 PyGILState_STATE gilstate = PyGILState_Ensure();
Thomas Wouters89d996e2007-09-08 17:39:28 +0000786#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 m = PyList_New(num_matches);
788 if (m == NULL)
789 goto error;
790 for (i = 0; i < num_matches; i++) {
791 s = PyUnicode_FromString(matches[i+1]);
792 if (s == NULL)
793 goto error;
794 if (PyList_SetItem(m, i, s) == -1)
795 goto error;
796 }
797 r = PyObject_CallFunction(completion_display_matches_hook,
798 "sOi", matches[0], m, max_length);
Christian Heimes32fbe592007-11-12 15:01:33 +0000799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 Py_DECREF(m); m=NULL;
Christian Heimes32fbe592007-11-12 15:01:33 +0000801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 if (r == NULL ||
803 (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) {
804 goto error;
805 }
806 Py_XDECREF(r); r=NULL;
807
808 if (0) {
809 error:
810 PyErr_Clear();
811 Py_XDECREF(m);
812 Py_XDECREF(r);
813 }
Christian Heimes32fbe592007-11-12 15:01:33 +0000814#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 PyGILState_Release(gilstate);
Christian Heimes32fbe592007-11-12 15:01:33 +0000816#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000817}
818
Senthil Kumaran95c07002010-11-04 03:51:05 +0000819#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000820
Guido van Rossum290900a1997-09-26 21:51:21 +0000821/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000822
Guido van Rossum290900a1997-09-26 21:51:21 +0000823static char *
Guido van Rossumd8faa362007-04-27 19:54:29 +0000824on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000825{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 char *result = NULL;
827 if (completer != NULL) {
828 PyObject *r;
Christian Heimesaec75c32007-11-11 22:42:36 +0000829#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000831#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 rl_attempted_completion_over = 1;
833 r = PyObject_CallFunction(completer, "si", text, state);
834 if (r == NULL)
835 goto error;
836 if (r == Py_None) {
837 result = NULL;
838 }
839 else {
840 char *s = _PyUnicode_AsString(r);
841 if (s == NULL)
842 goto error;
843 result = strdup(s);
844 }
845 Py_DECREF(r);
846 goto done;
847 error:
848 PyErr_Clear();
849 Py_XDECREF(r);
850 done:
Christian Heimesaec75c32007-11-11 22:42:36 +0000851#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 PyGILState_Release(gilstate);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000853#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 return result;
855 }
856 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000857}
858
Guido van Rossum290900a1997-09-26 21:51:21 +0000859
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000860/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000861 * before calling the normal completer */
862
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000863static char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000864flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000865{
Antoine Pitroudc0900b2009-10-19 18:22:37 +0000866#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 rl_completion_append_character ='\0';
Antoine Pitroud5131772009-10-26 19:22:14 +0000868#endif
869#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 rl_completion_suppress_append = 0;
Antoine Pitroudc0900b2009-10-19 18:22:37 +0000871#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 Py_XDECREF(begidx);
873 Py_XDECREF(endidx);
874 begidx = PyLong_FromLong((long) start);
875 endidx = PyLong_FromLong((long) end);
876 return completion_matches(text, *on_completion);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000877}
878
Guido van Rossum05ac4492003-01-07 20:04:12 +0000879
Guido van Rossum290900a1997-09-26 21:51:21 +0000880/* Helper to initialize GNU readline properly. */
881
882static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000883setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000884{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000885#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
887 if (!saved_locale)
888 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000889#endif
890
R. David Murray52d1b4e2010-12-18 03:48:32 +0000891#ifdef __APPLE__
Victor Stinner6ced7c42011-03-21 18:15:42 +0100892 /* the libedit readline emulation resets key bindings etc
R. David Murray52d1b4e2010-12-18 03:48:32 +0000893 * when calling rl_initialize. So call it upfront
894 */
895 if (using_libedit_emulation)
896 rl_initialize();
897#endif /* __APPLE__ */
898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 using_history();
Skip Montanaroa0392742002-06-11 14:32:46 +0000900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000902#if defined(PYOS_OS2) && defined(PYCC_GCC)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 /* Allow $if term= in .inputrc to work */
904 rl_terminal_name = getenv("TERM");
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000905#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 /* Force rebind of TAB to insert-tab */
907 rl_bind_key('\t', rl_insert);
908 /* Bind both ESC-TAB and ESC-ESC to the completion function */
909 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
910 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
911 /* Set our hook functions */
912 rl_startup_hook = (Function *)on_startup_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000913#ifdef HAVE_RL_PRE_INPUT_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 rl_pre_input_hook = (Function *)on_pre_input_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000915#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 /* Set our completion function */
917 rl_attempted_completion_function = (CPPFunction *)flex_complete;
918 /* Set Python word break characters */
919 rl_completer_word_break_characters =
920 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
921 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 begidx = PyLong_FromLong(0L);
924 endidx = PyLong_FromLong(0L);
925 /* Initialize (allows .inputrc to override)
926 *
927 * XXX: A bug in the readline-2.2 library causes a memory leak
928 * inside this function. Nothing we can do about it.
929 */
R. David Murray52d1b4e2010-12-18 03:48:32 +0000930#ifdef __APPLE__
931 if (using_libedit_emulation)
Victor Stinner6ced7c42011-03-21 18:15:42 +0100932 rl_read_init_file(NULL);
R. David Murray52d1b4e2010-12-18 03:48:32 +0000933 else
934#endif /* __APPLE__ */
935 rl_initialize();
Victor Stinner6ced7c42011-03-21 18:15:42 +0100936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 RESTORE_LOCALE(saved_locale)
Guido van Rossum290900a1997-09-26 21:51:21 +0000938}
939
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000940/* Wrapper around GNU readline that handles signals differently. */
941
942
943#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945static char *completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000946static void
947rlhandler(char *text)
948{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 completed_input_string = text;
950 rl_callback_handler_remove();
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000951}
952
953extern PyThreadState* _PyOS_ReadlineTState;
954
955static char *
956readline_until_enter_or_signal(char *prompt, int *signal)
957{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 char * not_done_reading = "";
959 fd_set selectset;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 *signal = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000962#ifdef HAVE_RL_CATCH_SIGNAL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 rl_catch_signals = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000964#endif
965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 rl_callback_handler_install (prompt, rlhandler);
967 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000968
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 completed_input_string = not_done_reading;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 while (completed_input_string == not_done_reading) {
972 int has_input = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 while (!has_input)
975 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 /* [Bug #1552726] Only limit the pause if an input hook has been
978 defined. */
979 struct timeval *timeoutp = NULL;
980 if (PyOS_InputHook)
981 timeoutp = &timeout;
982 FD_SET(fileno(rl_instream), &selectset);
983 /* select resets selectset if no input was available */
984 has_input = select(fileno(rl_instream) + 1, &selectset,
985 NULL, NULL, timeoutp);
986 if(PyOS_InputHook) PyOS_InputHook();
987 }
988
989 if(has_input > 0) {
990 rl_callback_read_char();
991 }
992 else if (errno == EINTR) {
993 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000994#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000996#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000998#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001000#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 if (s < 0) {
1002 rl_free_line_state();
1003 rl_cleanup_after_signal();
1004 rl_callback_handler_remove();
1005 *signal = 1;
1006 completed_input_string = NULL;
1007 }
1008 }
1009 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 return completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001012}
1013
1014
1015#else
Guido van Rossum290900a1997-09-26 21:51:21 +00001016
1017/* Interrupt handler */
1018
1019static jmp_buf jbuf;
1020
Guido van Rossum0969d361997-08-05 21:27:50 +00001021/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +00001022static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001023onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +00001024{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +00001026}
1027
Guido van Rossum290900a1997-09-26 21:51:21 +00001028
Guido van Rossum0969d361997-08-05 21:27:50 +00001029static char *
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001030readline_until_enter_or_signal(char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +00001031{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 PyOS_sighandler_t old_inthandler;
1033 char *p;
Guido van Rossum74f31432003-01-07 20:01:29 +00001034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 *signal = 0;
1036
1037 old_inthandler = PyOS_setsig(SIGINT, onintr);
1038 if (setjmp(jbuf)) {
Guido van Rossum0969d361997-08-05 21:27:50 +00001039#ifdef HAVE_SIGRELSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
1041 sigrelse(SIGINT);
Guido van Rossum0969d361997-08-05 21:27:50 +00001042#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 PyOS_setsig(SIGINT, old_inthandler);
1044 *signal = 1;
1045 return NULL;
1046 }
1047 rl_event_hook = PyOS_InputHook;
1048 p = readline(prompt);
1049 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001050
1051 return p;
1052}
1053#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
1054
1055
1056static char *
1057call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
1058{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 size_t n;
1060 char *p, *q;
1061 int signal;
Neal Norwitz1fa040b2004-08-25 01:20:18 +00001062
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001063#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1065 if (!saved_locale)
1066 Py_FatalError("not enough memory to save locale");
1067 setlocale(LC_CTYPE, "");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001068#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
1071 rl_instream = sys_stdin;
1072 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001073#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001075#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 }
Guido van Rossum74f31432003-01-07 20:01:29 +00001077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 p = readline_until_enter_or_signal(prompt, &signal);
Guido van Rossumb18618d2000-05-03 23:44:39 +00001079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 /* we got an interrupt signal */
1081 if (signal) {
1082 RESTORE_LOCALE(saved_locale)
1083 return NULL;
1084 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 /* We got an EOF, return a empty string. */
1087 if (p == NULL) {
1088 p = PyMem_Malloc(1);
1089 if (p != NULL)
1090 *p = '\0';
1091 RESTORE_LOCALE(saved_locale)
1092 return p;
1093 }
1094
1095 /* we have a valid line */
1096 n = strlen(p);
1097 if (n > 0) {
Brett Cannon2525dc82010-08-22 20:36:25 +00001098 const char *line;
Mark Dickinson6b54e1f2010-08-03 16:49:49 +00001099 int length = _py_get_history_length();
1100 if (length > 0)
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001101#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 if (using_libedit_emulation) {
1103 /*
1104 * Libedit's emulation uses 0-based indexes,
1105 * the real readline uses 1-based indexes.
1106 */
Brett Cannon2525dc82010-08-22 20:36:25 +00001107 line = (const char *)history_get(length - 1)->line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 } else
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001109#endif /* __APPLE__ */
Brett Cannon2525dc82010-08-22 20:36:25 +00001110 line = (const char *)history_get(length)->line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 else
1112 line = "";
1113 if (strcmp(p, line))
1114 add_history(p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 }
1116 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
1117 release the original. */
1118 q = p;
1119 p = PyMem_Malloc(n+2);
1120 if (p != NULL) {
1121 strncpy(p, q, n);
1122 p[n] = '\n';
1123 p[n+1] = '\0';
1124 }
1125 free(q);
1126 RESTORE_LOCALE(saved_locale)
1127 return p;
Guido van Rossum0969d361997-08-05 21:27:50 +00001128}
1129
Guido van Rossum290900a1997-09-26 21:51:21 +00001130
1131/* Initialize the module */
1132
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001133PyDoc_STRVAR(doc_module,
1134"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001135
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001136#ifdef __APPLE__
1137PyDoc_STRVAR(doc_module_le,
1138"Importing this module enables command line editing using libedit readline.");
1139#endif /* __APPLE__ */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001140
1141static struct PyModuleDef readlinemodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 PyModuleDef_HEAD_INIT,
1143 "readline",
1144 doc_module,
1145 -1,
1146 readline_methods,
1147 NULL,
1148 NULL,
1149 NULL,
1150 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001151};
1152
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001153
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001154PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001155PyInit_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001156{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +00001158
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001159#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
1161 using_libedit_emulation = 1;
1162 }
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 if (using_libedit_emulation)
1165 readlinemodule.m_doc = doc_module_le;
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001166
1167#endif /* __APPLE__ */
1168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 m = PyModule_Create(&readlinemodule);
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 if (m == NULL)
1172 return NULL;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001173
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001174
1175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 PyOS_ReadlineFunctionPointer = call_readline;
1177 setup_readline();
1178 return m;
Guido van Rossum0969d361997-08-05 21:27:50 +00001179}