blob: a710652e140f919c251761733f82eea4d2a74b9b [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;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100157 int err;
Victor Stinner19e65a32010-06-11 22:27:14 +0000158 if (!PyArg_ParseTuple(args, "|O:write_history_file", &filename_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 return NULL;
Victor Stinner19e65a32010-06-11 22:27:14 +0000160 if (filename_obj != Py_None) {
161 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
162 return NULL;
163 filename = PyBytes_AsString(filename_bytes);
164 } else {
165 filename_bytes = NULL;
166 filename = NULL;
167 }
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100168 errno = err = write_history(filename);
169 if (!err && _history_length >= 0)
Victor Stinner19e65a32010-06-11 22:27:14 +0000170 history_truncate_file(filename, _history_length);
171 Py_XDECREF(filename_bytes);
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100172 errno = err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 if (errno)
174 return PyErr_SetFromErrno(PyExc_IOError);
175 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000176}
177
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000178PyDoc_STRVAR(doc_write_history_file,
179"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000180Save a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000181The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000182
183
Guido van Rossum74f31432003-01-07 20:01:29 +0000184/* Set history length */
185
186static PyObject*
187set_history_length(PyObject *self, PyObject *args)
188{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 int length = _history_length;
190 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
191 return NULL;
192 _history_length = length;
193 Py_RETURN_NONE;
Guido van Rossum74f31432003-01-07 20:01:29 +0000194}
195
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000196PyDoc_STRVAR(set_history_length_doc,
197"set_history_length(length) -> None\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000198set the maximal number of items which will be written to\n\
199the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000200history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000201
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000202
Guido van Rossum74f31432003-01-07 20:01:29 +0000203/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000204
205static PyObject*
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000206get_history_length(PyObject *self, PyObject *noarg)
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000207{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 return PyLong_FromLong(_history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000209}
210
Guido van Rossum74f31432003-01-07 20:01:29 +0000211PyDoc_STRVAR(get_history_length_doc,
212"get_history_length() -> int\n\
213return the maximum number of items that will be written to\n\
214the history file.");
215
216
Martin v. Löwis0daad592001-09-30 21:09:59 +0000217/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000218
Martin v. Löwis0daad592001-09-30 21:09:59 +0000219static PyObject *
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000220set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000221{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 PyObject *function = Py_None;
223 char buf[80];
224 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
225 if (!PyArg_ParseTuple(args, buf, &function))
226 return NULL;
227 if (function == Py_None) {
228 Py_XDECREF(*hook_var);
229 *hook_var = NULL;
230 }
231 else if (PyCallable_Check(function)) {
232 PyObject *tmp = *hook_var;
233 Py_INCREF(function);
234 *hook_var = function;
235 Py_XDECREF(tmp);
236 }
237 else {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100238 PyErr_Format(PyExc_TypeError,
239 "set_%.50s(func): argument not callable",
240 funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 return NULL;
242 }
243 Py_RETURN_NONE;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000244}
245
Guido van Rossum74f31432003-01-07 20:01:29 +0000246
Martin v. Löwis0daad592001-09-30 21:09:59 +0000247/* Exported functions to specify hook functions in Python */
248
Thomas Wouters89d996e2007-09-08 17:39:28 +0000249static PyObject *completion_display_matches_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000250static PyObject *startup_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000251
252#ifdef HAVE_RL_PRE_INPUT_HOOK
253static PyObject *pre_input_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000254#endif
255
256static PyObject *
Thomas Wouters89d996e2007-09-08 17:39:28 +0000257set_completion_display_matches_hook(PyObject *self, PyObject *args)
258{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 PyObject *result = set_hook("completion_display_matches_hook",
260 &completion_display_matches_hook, args);
Christian Heimes32fbe592007-11-12 15:01:33 +0000261#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 /* We cannot set this hook globally, since it replaces the
263 default completion display. */
264 rl_completion_display_matches_hook =
265 completion_display_matches_hook ?
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000266#if defined(_RL_FUNCTION_TYPEDEF)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000268#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 (VFunction *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000270#endif
Christian Heimes32fbe592007-11-12 15:01:33 +0000271#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 return result;
Christian Heimes32fbe592007-11-12 15:01:33 +0000273
Thomas Wouters89d996e2007-09-08 17:39:28 +0000274}
275
276PyDoc_STRVAR(doc_set_completion_display_matches_hook,
277"set_completion_display_matches_hook([function]) -> None\n\
278Set or remove the completion display function.\n\
279The function is called as\n\
280 function(substitution, [matches], longest_match_length)\n\
281once each time matches need to be displayed.");
282
283static PyObject *
Martin v. Löwis0daad592001-09-30 21:09:59 +0000284set_startup_hook(PyObject *self, PyObject *args)
285{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 return set_hook("startup_hook", &startup_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000287}
288
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000289PyDoc_STRVAR(doc_set_startup_hook,
290"set_startup_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000291Set or remove the startup_hook function.\n\
292The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000293before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000294
Guido van Rossum74f31432003-01-07 20:01:29 +0000295
Martin v. Löwis0daad592001-09-30 21:09:59 +0000296#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000297
298/* Set pre-input hook */
299
Martin v. Löwis0daad592001-09-30 21:09:59 +0000300static PyObject *
301set_pre_input_hook(PyObject *self, PyObject *args)
302{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 return set_hook("pre_input_hook", &pre_input_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000304}
305
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000306PyDoc_STRVAR(doc_set_pre_input_hook,
307"set_pre_input_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000308Set or remove the pre_input_hook function.\n\
309The function is called with no arguments after the first prompt\n\
310has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000311characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000312
Martin v. Löwis0daad592001-09-30 21:09:59 +0000313#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000314
Guido van Rossum74f31432003-01-07 20:01:29 +0000315
Guido van Rossum290900a1997-09-26 21:51:21 +0000316/* Exported function to specify a word completer in Python */
317
318static PyObject *completer = NULL;
Guido van Rossum290900a1997-09-26 21:51:21 +0000319
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000320static PyObject *begidx = NULL;
321static PyObject *endidx = NULL;
322
Guido van Rossum74f31432003-01-07 20:01:29 +0000323
Thomas Wouters89d996e2007-09-08 17:39:28 +0000324/* Get the completion type for the scope of the tab-completion */
325static PyObject *
326get_completion_type(PyObject *self, PyObject *noarg)
327{
Christian Heimes217cfd12007-12-02 14:31:20 +0000328 return PyLong_FromLong(rl_completion_type);
Thomas Wouters89d996e2007-09-08 17:39:28 +0000329}
330
331PyDoc_STRVAR(doc_get_completion_type,
332"get_completion_type() -> int\n\
333Get the type of completion being attempted.");
334
335
Guido van Rossum74f31432003-01-07 20:01:29 +0000336/* Get the beginning index for the scope of the tab-completion */
337
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000338static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000339get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000340{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 Py_INCREF(begidx);
342 return begidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000343}
344
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000345PyDoc_STRVAR(doc_get_begidx,
346"get_begidx() -> int\n\
347get the beginning index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000348
Guido van Rossum74f31432003-01-07 20:01:29 +0000349
350/* Get the ending index for the scope of the tab-completion */
351
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000352static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000353get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000354{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 Py_INCREF(endidx);
356 return endidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000357}
358
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000359PyDoc_STRVAR(doc_get_endidx,
360"get_endidx() -> int\n\
361get the ending index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000362
363
Guido van Rossum74f31432003-01-07 20:01:29 +0000364/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000365
366static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000367set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000368{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 char *break_chars;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
372 return NULL;
373 }
374 free((void*)rl_completer_word_break_characters);
375 rl_completer_word_break_characters = strdup(break_chars);
376 Py_RETURN_NONE;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000377}
378
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000379PyDoc_STRVAR(doc_set_completer_delims,
380"set_completer_delims(string) -> None\n\
381set the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000382
Mark Dickinson29b238e2010-08-03 16:08:16 +0000383/* _py_free_history_entry: Utility function to free a history entry. */
384
385#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
386
387/* Readline version >= 5.0 introduced a timestamp field into the history entry
388 structure; this needs to be freed to avoid a memory leak. This version of
389 readline also introduced the handy 'free_history_entry' function, which
390 takes care of the timestamp. */
391
392static void
393_py_free_history_entry(HIST_ENTRY *entry)
394{
395 histdata_t data = free_history_entry(entry);
396 free(data);
397}
398
399#else
400
401/* No free_history_entry function; free everything manually. */
402
403static void
404_py_free_history_entry(HIST_ENTRY *entry)
405{
406 if (entry->line)
407 free((void *)entry->line);
408 if (entry->data)
409 free(entry->data);
410 free(entry);
411}
412
413#endif
414
Skip Montanaroe5069012004-08-15 14:32:06 +0000415static PyObject *
416py_remove_history(PyObject *self, PyObject *args)
417{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 int entry_number;
419 HIST_ENTRY *entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
422 return NULL;
423 if (entry_number < 0) {
424 PyErr_SetString(PyExc_ValueError,
425 "History index cannot be negative");
426 return NULL;
427 }
428 entry = remove_history(entry_number);
429 if (!entry) {
430 PyErr_Format(PyExc_ValueError,
431 "No history item at position %d",
432 entry_number);
433 return NULL;
434 }
435 /* free memory allocated for the history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000436 _py_free_history_entry(entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000438}
439
440PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000441"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000442remove history item given by its position");
443
444static PyObject *
445py_replace_history(PyObject *self, PyObject *args)
446{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 int entry_number;
448 char *line;
449 HIST_ENTRY *old_entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number,
452 &line)) {
453 return NULL;
454 }
455 if (entry_number < 0) {
456 PyErr_SetString(PyExc_ValueError,
457 "History index cannot be negative");
458 return NULL;
459 }
460 old_entry = replace_history_entry(entry_number, line, (void *)NULL);
461 if (!old_entry) {
462 PyErr_Format(PyExc_ValueError,
463 "No history item at position %d",
464 entry_number);
465 return NULL;
466 }
467 /* free memory allocated for the old history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000468 _py_free_history_entry(old_entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000470}
471
472PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000473"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000474replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000475
476/* Add a line to the history buffer */
477
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000478static PyObject *
479py_add_history(PyObject *self, PyObject *args)
480{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 char *line;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
484 return NULL;
485 }
486 add_history(line);
487 Py_RETURN_NONE;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000488}
489
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000490PyDoc_STRVAR(doc_add_history,
491"add_history(string) -> None\n\
492add a line to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000493
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000494
Guido van Rossum74f31432003-01-07 20:01:29 +0000495/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000496
497static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000498get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000499{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 return PyUnicode_FromString(rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000501}
Guido van Rossum74f31432003-01-07 20:01:29 +0000502
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000503PyDoc_STRVAR(doc_get_completer_delims,
504"get_completer_delims() -> string\n\
505get the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000506
Guido van Rossum74f31432003-01-07 20:01:29 +0000507
508/* Set the completer function */
509
Guido van Rossum290900a1997-09-26 21:51:21 +0000510static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000511set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000512{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 return set_hook("completer", &completer, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000514}
515
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000516PyDoc_STRVAR(doc_set_completer,
517"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000518Set or remove the completer function.\n\
519The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000520for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000521It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000522
Guido van Rossum74f31432003-01-07 20:01:29 +0000523
Michael W. Hudson796df152003-01-30 10:12:51 +0000524static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000525get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000526{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 if (completer == NULL) {
528 Py_RETURN_NONE;
529 }
530 Py_INCREF(completer);
531 return completer;
Michael W. Hudson796df152003-01-30 10:12:51 +0000532}
533
534PyDoc_STRVAR(doc_get_completer,
535"get_completer() -> function\n\
536\n\
537Returns current completer function.");
538
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000539/* Private function to get current length of history. XXX It may be
540 * possible to replace this with a direct use of history_length instead,
541 * but it's not clear whether BSD's libedit keeps history_length up to date.
542 * See issue #8065.*/
543
544static int
545_py_get_history_length(void)
546{
547 HISTORY_STATE *hist_st = history_get_history_state();
548 int length = hist_st->length;
549 /* the history docs don't say so, but the address of hist_st changes each
550 time history_get_history_state is called which makes me think it's
551 freshly malloc'd memory... on the other hand, the address of the last
552 line stays the same as long as history isn't extended, so it appears to
553 be malloc'd but managed by the history package... */
554 free(hist_st);
555 return length;
556}
557
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000558/* Exported function to get any element of history */
559
560static PyObject *
561get_history_item(PyObject *self, PyObject *args)
562{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 int idx = 0;
564 HIST_ENTRY *hist_ent;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 if (!PyArg_ParseTuple(args, "i:index", &idx))
567 return NULL;
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000568#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 if (using_libedit_emulation) {
570 /* Libedit emulation uses 0-based indexes,
571 * the real one uses 1-based indexes,
572 * adjust the index to ensure that Python
573 * code doesn't have to worry about the
574 * difference.
575 */
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000576 int length = _py_get_history_length();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 idx --;
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 /*
580 * Apple's readline emulation crashes when
581 * the index is out of range, therefore
582 * test for that and fail gracefully.
583 */
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000584 if (idx < 0 || idx >= length) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 Py_RETURN_NONE;
586 }
587 }
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000588#endif /* __APPLE__ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 if ((hist_ent = history_get(idx)))
590 return PyUnicode_FromString(hist_ent->line);
591 else {
592 Py_RETURN_NONE;
593 }
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000594}
595
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000596PyDoc_STRVAR(doc_get_history_item,
597"get_history_item() -> string\n\
598return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000599
Guido van Rossum74f31432003-01-07 20:01:29 +0000600
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000601/* Exported function to get current length of history */
602
603static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000604get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000605{
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000606 return PyLong_FromLong((long)_py_get_history_length());
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000607}
608
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000609PyDoc_STRVAR(doc_get_current_history_length,
610"get_current_history_length() -> integer\n\
611return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000612
Guido van Rossum74f31432003-01-07 20:01:29 +0000613
Guido van Rossum79378ff1997-10-07 14:53:21 +0000614/* Exported function to read the current line buffer */
615
616static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000617get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000618{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 return PyUnicode_FromString(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000620}
621
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000622PyDoc_STRVAR(doc_get_line_buffer,
623"get_line_buffer() -> string\n\
624return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000625
Guido van Rossum74f31432003-01-07 20:01:29 +0000626
Martin v. Löwise7a97962003-09-20 16:08:33 +0000627#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
628
629/* Exported function to clear the current history */
630
631static PyObject *
632py_clear_history(PyObject *self, PyObject *noarg)
633{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 clear_history();
635 Py_RETURN_NONE;
Martin v. Löwise7a97962003-09-20 16:08:33 +0000636}
637
638PyDoc_STRVAR(doc_clear_history,
639"clear_history() -> None\n\
640Clear the current readline history.");
641#endif
642
643
Guido van Rossum79378ff1997-10-07 14:53:21 +0000644/* Exported function to insert text into the line buffer */
645
646static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000647insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000648{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 char *s;
650 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
651 return NULL;
652 rl_insert_text(s);
653 Py_RETURN_NONE;
Guido van Rossum79378ff1997-10-07 14:53:21 +0000654}
655
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000656PyDoc_STRVAR(doc_insert_text,
657"insert_text(string) -> None\n\
658Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000659
Guido van Rossum74f31432003-01-07 20:01:29 +0000660
661/* Redisplay the line buffer */
662
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000663static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000664redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000665{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 rl_redisplay();
667 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000668}
669
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000670PyDoc_STRVAR(doc_redisplay,
671"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000672Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000673contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000674
Guido van Rossum74f31432003-01-07 20:01:29 +0000675
Guido van Rossum290900a1997-09-26 21:51:21 +0000676/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000677
678static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000679{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
681 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
682 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
683 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
684 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
685 {"read_history_file", read_history_file,
686 METH_VARARGS, doc_read_history_file},
687 {"write_history_file", write_history_file,
688 METH_VARARGS, doc_write_history_file},
689 {"get_history_item", get_history_item,
690 METH_VARARGS, doc_get_history_item},
691 {"get_current_history_length", (PyCFunction)get_current_history_length,
692 METH_NOARGS, doc_get_current_history_length},
693 {"set_history_length", set_history_length,
694 METH_VARARGS, set_history_length_doc},
695 {"get_history_length", get_history_length,
696 METH_NOARGS, get_history_length_doc},
697 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
698 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
699 {"get_completion_type", get_completion_type,
700 METH_NOARGS, doc_get_completion_type},
701 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
702 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 {"set_completer_delims", set_completer_delims,
705 METH_VARARGS, doc_set_completer_delims},
706 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
707 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
708 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
709 {"get_completer_delims", get_completer_delims,
710 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 {"set_completion_display_matches_hook", set_completion_display_matches_hook,
713 METH_VARARGS, doc_set_completion_display_matches_hook},
714 {"set_startup_hook", set_startup_hook,
715 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000716#ifdef HAVE_RL_PRE_INPUT_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 {"set_pre_input_hook", set_pre_input_hook,
718 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000719#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000720#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
Martin v. Löwise7a97962003-09-20 16:08:33 +0000722#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000724};
725
Guido van Rossum05ac4492003-01-07 20:04:12 +0000726
Martin v. Löwis0daad592001-09-30 21:09:59 +0000727/* C function to call the Python hooks. */
728
729static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000730on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000731{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 int result = 0;
733 if (func != NULL) {
734 PyObject *r;
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000735#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000737#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 r = PyObject_CallFunction(func, NULL);
739 if (r == NULL)
740 goto error;
741 if (r == Py_None)
742 result = 0;
743 else {
744 result = PyLong_AsLong(r);
745 if (result == -1 && PyErr_Occurred())
746 goto error;
747 }
748 Py_DECREF(r);
749 goto done;
750 error:
751 PyErr_Clear();
752 Py_XDECREF(r);
753 done:
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000754#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 PyGILState_Release(gilstate);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000756#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 return result;
758 }
759 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000760}
761
762static int
763on_startup_hook(void)
764{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 return on_hook(startup_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000766}
767
768#ifdef HAVE_RL_PRE_INPUT_HOOK
769static int
770on_pre_input_hook(void)
771{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 return on_hook(pre_input_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000773}
774#endif
775
Guido van Rossum05ac4492003-01-07 20:04:12 +0000776
Thomas Wouters89d996e2007-09-08 17:39:28 +0000777/* C function to call the Python completion_display_matches */
778
Georg Brandl646fdd62010-10-18 07:27:55 +0000779#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Thomas Wouters89d996e2007-09-08 17:39:28 +0000780static void
781on_completion_display_matches_hook(char **matches,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 int num_matches, int max_length)
Thomas Wouters89d996e2007-09-08 17:39:28 +0000783{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 int i;
785 PyObject *m=NULL, *s=NULL, *r=NULL;
Christian Heimesaec75c32007-11-11 22:42:36 +0000786#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 PyGILState_STATE gilstate = PyGILState_Ensure();
Thomas Wouters89d996e2007-09-08 17:39:28 +0000788#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 m = PyList_New(num_matches);
790 if (m == NULL)
791 goto error;
792 for (i = 0; i < num_matches; i++) {
793 s = PyUnicode_FromString(matches[i+1]);
794 if (s == NULL)
795 goto error;
796 if (PyList_SetItem(m, i, s) == -1)
797 goto error;
798 }
799 r = PyObject_CallFunction(completion_display_matches_hook,
800 "sOi", matches[0], m, max_length);
Christian Heimes32fbe592007-11-12 15:01:33 +0000801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 Py_DECREF(m); m=NULL;
Christian Heimes32fbe592007-11-12 15:01:33 +0000803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 if (r == NULL ||
805 (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) {
806 goto error;
807 }
808 Py_XDECREF(r); r=NULL;
809
810 if (0) {
811 error:
812 PyErr_Clear();
813 Py_XDECREF(m);
814 Py_XDECREF(r);
815 }
Christian Heimes32fbe592007-11-12 15:01:33 +0000816#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 PyGILState_Release(gilstate);
Christian Heimes32fbe592007-11-12 15:01:33 +0000818#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000819}
820
Senthil Kumaran95c07002010-11-04 03:51:05 +0000821#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000822
Guido van Rossum290900a1997-09-26 21:51:21 +0000823/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000824
Guido van Rossum290900a1997-09-26 21:51:21 +0000825static char *
Guido van Rossumd8faa362007-04-27 19:54:29 +0000826on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000827{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 char *result = NULL;
829 if (completer != NULL) {
830 PyObject *r;
Christian Heimesaec75c32007-11-11 22:42:36 +0000831#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000833#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 rl_attempted_completion_over = 1;
835 r = PyObject_CallFunction(completer, "si", text, state);
836 if (r == NULL)
837 goto error;
838 if (r == Py_None) {
839 result = NULL;
840 }
841 else {
842 char *s = _PyUnicode_AsString(r);
843 if (s == NULL)
844 goto error;
845 result = strdup(s);
846 }
847 Py_DECREF(r);
848 goto done;
849 error:
850 PyErr_Clear();
851 Py_XDECREF(r);
852 done:
Christian Heimesaec75c32007-11-11 22:42:36 +0000853#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 PyGILState_Release(gilstate);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000855#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 return result;
857 }
858 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000859}
860
Guido van Rossum290900a1997-09-26 21:51:21 +0000861
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000862/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000863 * before calling the normal completer */
864
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000865static char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000866flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000867{
Antoine Pitroudc0900b2009-10-19 18:22:37 +0000868#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 rl_completion_append_character ='\0';
Antoine Pitroud5131772009-10-26 19:22:14 +0000870#endif
871#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 rl_completion_suppress_append = 0;
Antoine Pitroudc0900b2009-10-19 18:22:37 +0000873#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 Py_XDECREF(begidx);
875 Py_XDECREF(endidx);
876 begidx = PyLong_FromLong((long) start);
877 endidx = PyLong_FromLong((long) end);
878 return completion_matches(text, *on_completion);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000879}
880
Guido van Rossum05ac4492003-01-07 20:04:12 +0000881
Guido van Rossum290900a1997-09-26 21:51:21 +0000882/* Helper to initialize GNU readline properly. */
883
884static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000885setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000886{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000887#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
889 if (!saved_locale)
890 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000891#endif
892
R. David Murray52d1b4e2010-12-18 03:48:32 +0000893#ifdef __APPLE__
Victor Stinner6ced7c42011-03-21 18:15:42 +0100894 /* the libedit readline emulation resets key bindings etc
R. David Murray52d1b4e2010-12-18 03:48:32 +0000895 * when calling rl_initialize. So call it upfront
896 */
897 if (using_libedit_emulation)
898 rl_initialize();
899#endif /* __APPLE__ */
900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 using_history();
Skip Montanaroa0392742002-06-11 14:32:46 +0000902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000904#if defined(PYOS_OS2) && defined(PYCC_GCC)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 /* Allow $if term= in .inputrc to work */
906 rl_terminal_name = getenv("TERM");
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000907#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 /* Force rebind of TAB to insert-tab */
909 rl_bind_key('\t', rl_insert);
910 /* Bind both ESC-TAB and ESC-ESC to the completion function */
911 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
912 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
913 /* Set our hook functions */
914 rl_startup_hook = (Function *)on_startup_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000915#ifdef HAVE_RL_PRE_INPUT_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 rl_pre_input_hook = (Function *)on_pre_input_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000917#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 /* Set our completion function */
919 rl_attempted_completion_function = (CPPFunction *)flex_complete;
920 /* Set Python word break characters */
921 rl_completer_word_break_characters =
922 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
923 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 begidx = PyLong_FromLong(0L);
926 endidx = PyLong_FromLong(0L);
927 /* Initialize (allows .inputrc to override)
928 *
929 * XXX: A bug in the readline-2.2 library causes a memory leak
930 * inside this function. Nothing we can do about it.
931 */
R. David Murray52d1b4e2010-12-18 03:48:32 +0000932#ifdef __APPLE__
933 if (using_libedit_emulation)
Victor Stinner6ced7c42011-03-21 18:15:42 +0100934 rl_read_init_file(NULL);
R. David Murray52d1b4e2010-12-18 03:48:32 +0000935 else
936#endif /* __APPLE__ */
937 rl_initialize();
Victor Stinner6ced7c42011-03-21 18:15:42 +0100938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 RESTORE_LOCALE(saved_locale)
Guido van Rossum290900a1997-09-26 21:51:21 +0000940}
941
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000942/* Wrapper around GNU readline that handles signals differently. */
943
944
945#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947static char *completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000948static void
949rlhandler(char *text)
950{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 completed_input_string = text;
952 rl_callback_handler_remove();
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000953}
954
955extern PyThreadState* _PyOS_ReadlineTState;
956
957static char *
958readline_until_enter_or_signal(char *prompt, int *signal)
959{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 char * not_done_reading = "";
961 fd_set selectset;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 *signal = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000964#ifdef HAVE_RL_CATCH_SIGNAL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 rl_catch_signals = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000966#endif
967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 rl_callback_handler_install (prompt, rlhandler);
969 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 completed_input_string = not_done_reading;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 while (completed_input_string == not_done_reading) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100974 int has_input = 0, err = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 while (!has_input)
977 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 /* [Bug #1552726] Only limit the pause if an input hook has been
980 defined. */
981 struct timeval *timeoutp = NULL;
982 if (PyOS_InputHook)
983 timeoutp = &timeout;
984 FD_SET(fileno(rl_instream), &selectset);
985 /* select resets selectset if no input was available */
986 has_input = select(fileno(rl_instream) + 1, &selectset,
987 NULL, NULL, timeoutp);
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100988 err = errno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 if(PyOS_InputHook) PyOS_InputHook();
990 }
991
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100992 if (has_input > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 rl_callback_read_char();
994 }
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100995 else if (err == EINTR) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000997#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000999#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001001#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001003#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 if (s < 0) {
1005 rl_free_line_state();
1006 rl_cleanup_after_signal();
1007 rl_callback_handler_remove();
1008 *signal = 1;
1009 completed_input_string = NULL;
1010 }
1011 }
1012 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 return completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001015}
1016
1017
1018#else
Guido van Rossum290900a1997-09-26 21:51:21 +00001019
1020/* Interrupt handler */
1021
1022static jmp_buf jbuf;
1023
Guido van Rossum0969d361997-08-05 21:27:50 +00001024/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +00001025static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001026onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +00001027{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +00001029}
1030
Guido van Rossum290900a1997-09-26 21:51:21 +00001031
Guido van Rossum0969d361997-08-05 21:27:50 +00001032static char *
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001033readline_until_enter_or_signal(char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +00001034{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 PyOS_sighandler_t old_inthandler;
1036 char *p;
Guido van Rossum74f31432003-01-07 20:01:29 +00001037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 *signal = 0;
1039
1040 old_inthandler = PyOS_setsig(SIGINT, onintr);
1041 if (setjmp(jbuf)) {
Guido van Rossum0969d361997-08-05 21:27:50 +00001042#ifdef HAVE_SIGRELSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
1044 sigrelse(SIGINT);
Guido van Rossum0969d361997-08-05 21:27:50 +00001045#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 PyOS_setsig(SIGINT, old_inthandler);
1047 *signal = 1;
1048 return NULL;
1049 }
1050 rl_event_hook = PyOS_InputHook;
1051 p = readline(prompt);
1052 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001053
1054 return p;
1055}
1056#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
1057
1058
1059static char *
1060call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
1061{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 size_t n;
1063 char *p, *q;
1064 int signal;
Neal Norwitz1fa040b2004-08-25 01:20:18 +00001065
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001066#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1068 if (!saved_locale)
1069 Py_FatalError("not enough memory to save locale");
1070 setlocale(LC_CTYPE, "");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001071#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
1074 rl_instream = sys_stdin;
1075 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001076#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001078#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 }
Guido van Rossum74f31432003-01-07 20:01:29 +00001080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 p = readline_until_enter_or_signal(prompt, &signal);
Guido van Rossumb18618d2000-05-03 23:44:39 +00001082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 /* we got an interrupt signal */
1084 if (signal) {
1085 RESTORE_LOCALE(saved_locale)
1086 return NULL;
1087 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 /* We got an EOF, return a empty string. */
1090 if (p == NULL) {
1091 p = PyMem_Malloc(1);
1092 if (p != NULL)
1093 *p = '\0';
1094 RESTORE_LOCALE(saved_locale)
1095 return p;
1096 }
1097
1098 /* we have a valid line */
1099 n = strlen(p);
1100 if (n > 0) {
Brett Cannon2525dc82010-08-22 20:36:25 +00001101 const char *line;
Mark Dickinson6b54e1f2010-08-03 16:49:49 +00001102 int length = _py_get_history_length();
1103 if (length > 0)
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001104#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 if (using_libedit_emulation) {
1106 /*
1107 * Libedit's emulation uses 0-based indexes,
1108 * the real readline uses 1-based indexes.
1109 */
Brett Cannon2525dc82010-08-22 20:36:25 +00001110 line = (const char *)history_get(length - 1)->line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 } else
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001112#endif /* __APPLE__ */
Brett Cannon2525dc82010-08-22 20:36:25 +00001113 line = (const char *)history_get(length)->line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 else
1115 line = "";
1116 if (strcmp(p, line))
1117 add_history(p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 }
1119 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
1120 release the original. */
1121 q = p;
1122 p = PyMem_Malloc(n+2);
1123 if (p != NULL) {
1124 strncpy(p, q, n);
1125 p[n] = '\n';
1126 p[n+1] = '\0';
1127 }
1128 free(q);
1129 RESTORE_LOCALE(saved_locale)
1130 return p;
Guido van Rossum0969d361997-08-05 21:27:50 +00001131}
1132
Guido van Rossum290900a1997-09-26 21:51:21 +00001133
1134/* Initialize the module */
1135
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001136PyDoc_STRVAR(doc_module,
1137"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001138
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001139#ifdef __APPLE__
1140PyDoc_STRVAR(doc_module_le,
1141"Importing this module enables command line editing using libedit readline.");
1142#endif /* __APPLE__ */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001143
1144static struct PyModuleDef readlinemodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 PyModuleDef_HEAD_INIT,
1146 "readline",
1147 doc_module,
1148 -1,
1149 readline_methods,
1150 NULL,
1151 NULL,
1152 NULL,
1153 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001154};
1155
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001156
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001157PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001158PyInit_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001159{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +00001161
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001162#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
1164 using_libedit_emulation = 1;
1165 }
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 if (using_libedit_emulation)
1168 readlinemodule.m_doc = doc_module_le;
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001169
1170#endif /* __APPLE__ */
1171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 m = PyModule_Create(&readlinemodule);
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 if (m == NULL)
1175 return NULL;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001176
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001177
1178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 PyOS_ReadlineFunctionPointer = call_readline;
1180 setup_readline();
1181 return m;
Guido van Rossum0969d361997-08-05 21:27:50 +00001182}