blob: 466ec512e6852acf930fbb2a014b825a9efd0fb8 [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 {
236 PyOS_snprintf(buf, sizeof(buf),
237 "set_%.50s(func): argument not callable",
238 funcname);
239 PyErr_SetString(PyExc_TypeError, buf);
240 return NULL;
241 }
242 Py_RETURN_NONE;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000243}
244
Guido van Rossum74f31432003-01-07 20:01:29 +0000245
Martin v. Löwis0daad592001-09-30 21:09:59 +0000246/* Exported functions to specify hook functions in Python */
247
Thomas Wouters89d996e2007-09-08 17:39:28 +0000248static PyObject *completion_display_matches_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000249static PyObject *startup_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000250
251#ifdef HAVE_RL_PRE_INPUT_HOOK
252static PyObject *pre_input_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000253#endif
254
255static PyObject *
Thomas Wouters89d996e2007-09-08 17:39:28 +0000256set_completion_display_matches_hook(PyObject *self, PyObject *args)
257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 PyObject *result = set_hook("completion_display_matches_hook",
259 &completion_display_matches_hook, args);
Christian Heimes32fbe592007-11-12 15:01:33 +0000260#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 /* We cannot set this hook globally, since it replaces the
262 default completion display. */
263 rl_completion_display_matches_hook =
264 completion_display_matches_hook ?
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000265#if defined(_RL_FUNCTION_TYPEDEF)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000267#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 (VFunction *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000269#endif
Christian Heimes32fbe592007-11-12 15:01:33 +0000270#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 return result;
Christian Heimes32fbe592007-11-12 15:01:33 +0000272
Thomas Wouters89d996e2007-09-08 17:39:28 +0000273}
274
275PyDoc_STRVAR(doc_set_completion_display_matches_hook,
276"set_completion_display_matches_hook([function]) -> None\n\
277Set or remove the completion display function.\n\
278The function is called as\n\
279 function(substitution, [matches], longest_match_length)\n\
280once each time matches need to be displayed.");
281
282static PyObject *
Martin v. Löwis0daad592001-09-30 21:09:59 +0000283set_startup_hook(PyObject *self, PyObject *args)
284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 return set_hook("startup_hook", &startup_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000286}
287
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000288PyDoc_STRVAR(doc_set_startup_hook,
289"set_startup_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000290Set or remove the startup_hook function.\n\
291The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000292before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000293
Guido van Rossum74f31432003-01-07 20:01:29 +0000294
Martin v. Löwis0daad592001-09-30 21:09:59 +0000295#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000296
297/* Set pre-input hook */
298
Martin v. Löwis0daad592001-09-30 21:09:59 +0000299static PyObject *
300set_pre_input_hook(PyObject *self, PyObject *args)
301{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 return set_hook("pre_input_hook", &pre_input_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000303}
304
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000305PyDoc_STRVAR(doc_set_pre_input_hook,
306"set_pre_input_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000307Set or remove the pre_input_hook function.\n\
308The function is called with no arguments after the first prompt\n\
309has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000310characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000311
Martin v. Löwis0daad592001-09-30 21:09:59 +0000312#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000313
Guido van Rossum74f31432003-01-07 20:01:29 +0000314
Guido van Rossum290900a1997-09-26 21:51:21 +0000315/* Exported function to specify a word completer in Python */
316
317static PyObject *completer = NULL;
Guido van Rossum290900a1997-09-26 21:51:21 +0000318
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000319static PyObject *begidx = NULL;
320static PyObject *endidx = NULL;
321
Guido van Rossum74f31432003-01-07 20:01:29 +0000322
Thomas Wouters89d996e2007-09-08 17:39:28 +0000323/* Get the completion type for the scope of the tab-completion */
324static PyObject *
325get_completion_type(PyObject *self, PyObject *noarg)
326{
Christian Heimes217cfd12007-12-02 14:31:20 +0000327 return PyLong_FromLong(rl_completion_type);
Thomas Wouters89d996e2007-09-08 17:39:28 +0000328}
329
330PyDoc_STRVAR(doc_get_completion_type,
331"get_completion_type() -> int\n\
332Get the type of completion being attempted.");
333
334
Guido van Rossum74f31432003-01-07 20:01:29 +0000335/* Get the beginning index for the scope of the tab-completion */
336
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000337static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000338get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000339{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 Py_INCREF(begidx);
341 return begidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000342}
343
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000344PyDoc_STRVAR(doc_get_begidx,
345"get_begidx() -> int\n\
346get the beginning index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000347
Guido van Rossum74f31432003-01-07 20:01:29 +0000348
349/* Get the ending index for the scope of the tab-completion */
350
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000351static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000352get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000353{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 Py_INCREF(endidx);
355 return endidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000356}
357
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000358PyDoc_STRVAR(doc_get_endidx,
359"get_endidx() -> int\n\
360get the ending index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000361
362
Guido van Rossum74f31432003-01-07 20:01:29 +0000363/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000364
365static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000366set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000367{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 char *break_chars;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
371 return NULL;
372 }
373 free((void*)rl_completer_word_break_characters);
374 rl_completer_word_break_characters = strdup(break_chars);
375 Py_RETURN_NONE;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000376}
377
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000378PyDoc_STRVAR(doc_set_completer_delims,
379"set_completer_delims(string) -> None\n\
380set the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000381
Mark Dickinson29b238e2010-08-03 16:08:16 +0000382/* _py_free_history_entry: Utility function to free a history entry. */
383
384#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
385
386/* Readline version >= 5.0 introduced a timestamp field into the history entry
387 structure; this needs to be freed to avoid a memory leak. This version of
388 readline also introduced the handy 'free_history_entry' function, which
389 takes care of the timestamp. */
390
391static void
392_py_free_history_entry(HIST_ENTRY *entry)
393{
394 histdata_t data = free_history_entry(entry);
395 free(data);
396}
397
398#else
399
400/* No free_history_entry function; free everything manually. */
401
402static void
403_py_free_history_entry(HIST_ENTRY *entry)
404{
405 if (entry->line)
406 free((void *)entry->line);
407 if (entry->data)
408 free(entry->data);
409 free(entry);
410}
411
412#endif
413
Skip Montanaroe5069012004-08-15 14:32:06 +0000414static PyObject *
415py_remove_history(PyObject *self, PyObject *args)
416{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 int entry_number;
418 HIST_ENTRY *entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
421 return NULL;
422 if (entry_number < 0) {
423 PyErr_SetString(PyExc_ValueError,
424 "History index cannot be negative");
425 return NULL;
426 }
427 entry = remove_history(entry_number);
428 if (!entry) {
429 PyErr_Format(PyExc_ValueError,
430 "No history item at position %d",
431 entry_number);
432 return NULL;
433 }
434 /* free memory allocated for the history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000435 _py_free_history_entry(entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000437}
438
439PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000440"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000441remove history item given by its position");
442
443static PyObject *
444py_replace_history(PyObject *self, PyObject *args)
445{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 int entry_number;
447 char *line;
448 HIST_ENTRY *old_entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number,
451 &line)) {
452 return NULL;
453 }
454 if (entry_number < 0) {
455 PyErr_SetString(PyExc_ValueError,
456 "History index cannot be negative");
457 return NULL;
458 }
459 old_entry = replace_history_entry(entry_number, line, (void *)NULL);
460 if (!old_entry) {
461 PyErr_Format(PyExc_ValueError,
462 "No history item at position %d",
463 entry_number);
464 return NULL;
465 }
466 /* free memory allocated for the old history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000467 _py_free_history_entry(old_entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000469}
470
471PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000472"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000473replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000474
475/* Add a line to the history buffer */
476
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000477static PyObject *
478py_add_history(PyObject *self, PyObject *args)
479{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 char *line;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
483 return NULL;
484 }
485 add_history(line);
486 Py_RETURN_NONE;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000487}
488
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000489PyDoc_STRVAR(doc_add_history,
490"add_history(string) -> None\n\
491add a line to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000492
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000493
Guido van Rossum74f31432003-01-07 20:01:29 +0000494/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000495
496static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000497get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 return PyUnicode_FromString(rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000500}
Guido van Rossum74f31432003-01-07 20:01:29 +0000501
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000502PyDoc_STRVAR(doc_get_completer_delims,
503"get_completer_delims() -> string\n\
504get the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000505
Guido van Rossum74f31432003-01-07 20:01:29 +0000506
507/* Set the completer function */
508
Guido van Rossum290900a1997-09-26 21:51:21 +0000509static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000510set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000511{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 return set_hook("completer", &completer, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000513}
514
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000515PyDoc_STRVAR(doc_set_completer,
516"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000517Set or remove the completer function.\n\
518The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000519for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000520It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000521
Guido van Rossum74f31432003-01-07 20:01:29 +0000522
Michael W. Hudson796df152003-01-30 10:12:51 +0000523static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000524get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000525{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 if (completer == NULL) {
527 Py_RETURN_NONE;
528 }
529 Py_INCREF(completer);
530 return completer;
Michael W. Hudson796df152003-01-30 10:12:51 +0000531}
532
533PyDoc_STRVAR(doc_get_completer,
534"get_completer() -> function\n\
535\n\
536Returns current completer function.");
537
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000538/* Private function to get current length of history. XXX It may be
539 * possible to replace this with a direct use of history_length instead,
540 * but it's not clear whether BSD's libedit keeps history_length up to date.
541 * See issue #8065.*/
542
543static int
544_py_get_history_length(void)
545{
546 HISTORY_STATE *hist_st = history_get_history_state();
547 int length = hist_st->length;
548 /* the history docs don't say so, but the address of hist_st changes each
549 time history_get_history_state is called which makes me think it's
550 freshly malloc'd memory... on the other hand, the address of the last
551 line stays the same as long as history isn't extended, so it appears to
552 be malloc'd but managed by the history package... */
553 free(hist_st);
554 return length;
555}
556
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000557/* Exported function to get any element of history */
558
559static PyObject *
560get_history_item(PyObject *self, PyObject *args)
561{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 int idx = 0;
563 HIST_ENTRY *hist_ent;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 if (!PyArg_ParseTuple(args, "i:index", &idx))
566 return NULL;
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000567#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 if (using_libedit_emulation) {
569 /* Libedit emulation uses 0-based indexes,
570 * the real one uses 1-based indexes,
571 * adjust the index to ensure that Python
572 * code doesn't have to worry about the
573 * difference.
574 */
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000575 int length = _py_get_history_length();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 idx --;
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 /*
579 * Apple's readline emulation crashes when
580 * the index is out of range, therefore
581 * test for that and fail gracefully.
582 */
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000583 if (idx < 0 || idx >= length) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 Py_RETURN_NONE;
585 }
586 }
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000587#endif /* __APPLE__ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 if ((hist_ent = history_get(idx)))
589 return PyUnicode_FromString(hist_ent->line);
590 else {
591 Py_RETURN_NONE;
592 }
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000593}
594
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000595PyDoc_STRVAR(doc_get_history_item,
596"get_history_item() -> string\n\
597return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000598
Guido van Rossum74f31432003-01-07 20:01:29 +0000599
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000600/* Exported function to get current length of history */
601
602static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000603get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000604{
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000605 return PyLong_FromLong((long)_py_get_history_length());
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000606}
607
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000608PyDoc_STRVAR(doc_get_current_history_length,
609"get_current_history_length() -> integer\n\
610return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000611
Guido van Rossum74f31432003-01-07 20:01:29 +0000612
Guido van Rossum79378ff1997-10-07 14:53:21 +0000613/* Exported function to read the current line buffer */
614
615static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000616get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000617{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 return PyUnicode_FromString(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000619}
620
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000621PyDoc_STRVAR(doc_get_line_buffer,
622"get_line_buffer() -> string\n\
623return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000624
Guido van Rossum74f31432003-01-07 20:01:29 +0000625
Martin v. Löwise7a97962003-09-20 16:08:33 +0000626#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
627
628/* Exported function to clear the current history */
629
630static PyObject *
631py_clear_history(PyObject *self, PyObject *noarg)
632{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 clear_history();
634 Py_RETURN_NONE;
Martin v. Löwise7a97962003-09-20 16:08:33 +0000635}
636
637PyDoc_STRVAR(doc_clear_history,
638"clear_history() -> None\n\
639Clear the current readline history.");
640#endif
641
642
Guido van Rossum79378ff1997-10-07 14:53:21 +0000643/* Exported function to insert text into the line buffer */
644
645static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000646insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000647{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 char *s;
649 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
650 return NULL;
651 rl_insert_text(s);
652 Py_RETURN_NONE;
Guido van Rossum79378ff1997-10-07 14:53:21 +0000653}
654
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000655PyDoc_STRVAR(doc_insert_text,
656"insert_text(string) -> None\n\
657Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000658
Guido van Rossum74f31432003-01-07 20:01:29 +0000659
660/* Redisplay the line buffer */
661
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000662static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000663redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000664{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 rl_redisplay();
666 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000667}
668
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000669PyDoc_STRVAR(doc_redisplay,
670"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000671Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000672contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000673
Guido van Rossum74f31432003-01-07 20:01:29 +0000674
Guido van Rossum290900a1997-09-26 21:51:21 +0000675/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000676
677static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000678{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
680 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
681 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
682 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
683 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
684 {"read_history_file", read_history_file,
685 METH_VARARGS, doc_read_history_file},
686 {"write_history_file", write_history_file,
687 METH_VARARGS, doc_write_history_file},
688 {"get_history_item", get_history_item,
689 METH_VARARGS, doc_get_history_item},
690 {"get_current_history_length", (PyCFunction)get_current_history_length,
691 METH_NOARGS, doc_get_current_history_length},
692 {"set_history_length", set_history_length,
693 METH_VARARGS, set_history_length_doc},
694 {"get_history_length", get_history_length,
695 METH_NOARGS, get_history_length_doc},
696 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
697 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
698 {"get_completion_type", get_completion_type,
699 METH_NOARGS, doc_get_completion_type},
700 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
701 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 {"set_completer_delims", set_completer_delims,
704 METH_VARARGS, doc_set_completer_delims},
705 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
706 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
707 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
708 {"get_completer_delims", get_completer_delims,
709 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 {"set_completion_display_matches_hook", set_completion_display_matches_hook,
712 METH_VARARGS, doc_set_completion_display_matches_hook},
713 {"set_startup_hook", set_startup_hook,
714 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000715#ifdef HAVE_RL_PRE_INPUT_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 {"set_pre_input_hook", set_pre_input_hook,
717 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000718#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000719#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
Martin v. Löwise7a97962003-09-20 16:08:33 +0000721#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000723};
724
Guido van Rossum05ac4492003-01-07 20:04:12 +0000725
Martin v. Löwis0daad592001-09-30 21:09:59 +0000726/* C function to call the Python hooks. */
727
728static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000729on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000730{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 int result = 0;
732 if (func != NULL) {
733 PyObject *r;
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000734#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000736#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 r = PyObject_CallFunction(func, NULL);
738 if (r == NULL)
739 goto error;
740 if (r == Py_None)
741 result = 0;
742 else {
743 result = PyLong_AsLong(r);
744 if (result == -1 && PyErr_Occurred())
745 goto error;
746 }
747 Py_DECREF(r);
748 goto done;
749 error:
750 PyErr_Clear();
751 Py_XDECREF(r);
752 done:
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000753#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 PyGILState_Release(gilstate);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000755#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 return result;
757 }
758 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000759}
760
761static int
762on_startup_hook(void)
763{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 return on_hook(startup_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000765}
766
767#ifdef HAVE_RL_PRE_INPUT_HOOK
768static int
769on_pre_input_hook(void)
770{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 return on_hook(pre_input_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000772}
773#endif
774
Guido van Rossum05ac4492003-01-07 20:04:12 +0000775
Thomas Wouters89d996e2007-09-08 17:39:28 +0000776/* C function to call the Python completion_display_matches */
777
Georg Brandl646fdd62010-10-18 07:27:55 +0000778#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Thomas Wouters89d996e2007-09-08 17:39:28 +0000779static void
780on_completion_display_matches_hook(char **matches,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 int num_matches, int max_length)
Thomas Wouters89d996e2007-09-08 17:39:28 +0000782{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 int i;
784 PyObject *m=NULL, *s=NULL, *r=NULL;
Christian Heimesaec75c32007-11-11 22:42:36 +0000785#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 PyGILState_STATE gilstate = PyGILState_Ensure();
Thomas Wouters89d996e2007-09-08 17:39:28 +0000787#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 m = PyList_New(num_matches);
789 if (m == NULL)
790 goto error;
791 for (i = 0; i < num_matches; i++) {
792 s = PyUnicode_FromString(matches[i+1]);
793 if (s == NULL)
794 goto error;
795 if (PyList_SetItem(m, i, s) == -1)
796 goto error;
797 }
798 r = PyObject_CallFunction(completion_display_matches_hook,
799 "sOi", matches[0], m, max_length);
Christian Heimes32fbe592007-11-12 15:01:33 +0000800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 Py_DECREF(m); m=NULL;
Christian Heimes32fbe592007-11-12 15:01:33 +0000802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 if (r == NULL ||
804 (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) {
805 goto error;
806 }
807 Py_XDECREF(r); r=NULL;
808
809 if (0) {
810 error:
811 PyErr_Clear();
812 Py_XDECREF(m);
813 Py_XDECREF(r);
814 }
Christian Heimes32fbe592007-11-12 15:01:33 +0000815#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 PyGILState_Release(gilstate);
Christian Heimes32fbe592007-11-12 15:01:33 +0000817#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000818}
819
Senthil Kumaran95c07002010-11-04 03:51:05 +0000820#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000821
Guido van Rossum290900a1997-09-26 21:51:21 +0000822/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000823
Guido van Rossum290900a1997-09-26 21:51:21 +0000824static char *
Guido van Rossumd8faa362007-04-27 19:54:29 +0000825on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000826{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 char *result = NULL;
828 if (completer != NULL) {
829 PyObject *r;
Christian Heimesaec75c32007-11-11 22:42:36 +0000830#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000832#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 rl_attempted_completion_over = 1;
834 r = PyObject_CallFunction(completer, "si", text, state);
835 if (r == NULL)
836 goto error;
837 if (r == Py_None) {
838 result = NULL;
839 }
840 else {
841 char *s = _PyUnicode_AsString(r);
842 if (s == NULL)
843 goto error;
844 result = strdup(s);
845 }
846 Py_DECREF(r);
847 goto done;
848 error:
849 PyErr_Clear();
850 Py_XDECREF(r);
851 done:
Christian Heimesaec75c32007-11-11 22:42:36 +0000852#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 PyGILState_Release(gilstate);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000854#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 return result;
856 }
857 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000858}
859
Guido van Rossum290900a1997-09-26 21:51:21 +0000860
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000861/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000862 * before calling the normal completer */
863
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000864static char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000865flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000866{
Antoine Pitroudc0900b2009-10-19 18:22:37 +0000867#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 rl_completion_append_character ='\0';
Antoine Pitroud5131772009-10-26 19:22:14 +0000869#endif
870#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 rl_completion_suppress_append = 0;
Antoine Pitroudc0900b2009-10-19 18:22:37 +0000872#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 Py_XDECREF(begidx);
874 Py_XDECREF(endidx);
875 begidx = PyLong_FromLong((long) start);
876 endidx = PyLong_FromLong((long) end);
877 return completion_matches(text, *on_completion);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000878}
879
Guido van Rossum05ac4492003-01-07 20:04:12 +0000880
Guido van Rossum290900a1997-09-26 21:51:21 +0000881/* Helper to initialize GNU readline properly. */
882
883static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000884setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000885{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000886#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
888 if (!saved_locale)
889 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000890#endif
891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 using_history();
Skip Montanaroa0392742002-06-11 14:32:46 +0000893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000895#if defined(PYOS_OS2) && defined(PYCC_GCC)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 /* Allow $if term= in .inputrc to work */
897 rl_terminal_name = getenv("TERM");
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000898#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 /* Force rebind of TAB to insert-tab */
900 rl_bind_key('\t', rl_insert);
901 /* Bind both ESC-TAB and ESC-ESC to the completion function */
902 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
903 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
904 /* Set our hook functions */
905 rl_startup_hook = (Function *)on_startup_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000906#ifdef HAVE_RL_PRE_INPUT_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 rl_pre_input_hook = (Function *)on_pre_input_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000908#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 /* Set our completion function */
910 rl_attempted_completion_function = (CPPFunction *)flex_complete;
911 /* Set Python word break characters */
912 rl_completer_word_break_characters =
913 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
914 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 begidx = PyLong_FromLong(0L);
917 endidx = PyLong_FromLong(0L);
918 /* Initialize (allows .inputrc to override)
919 *
920 * XXX: A bug in the readline-2.2 library causes a memory leak
921 * inside this function. Nothing we can do about it.
922 */
923 rl_initialize();
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 RESTORE_LOCALE(saved_locale)
Guido van Rossum290900a1997-09-26 21:51:21 +0000926}
927
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000928/* Wrapper around GNU readline that handles signals differently. */
929
930
931#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933static char *completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000934static void
935rlhandler(char *text)
936{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 completed_input_string = text;
938 rl_callback_handler_remove();
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000939}
940
941extern PyThreadState* _PyOS_ReadlineTState;
942
943static char *
944readline_until_enter_or_signal(char *prompt, int *signal)
945{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 char * not_done_reading = "";
947 fd_set selectset;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 *signal = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000950#ifdef HAVE_RL_CATCH_SIGNAL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 rl_catch_signals = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000952#endif
953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 rl_callback_handler_install (prompt, rlhandler);
955 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 completed_input_string = not_done_reading;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 while (completed_input_string == not_done_reading) {
960 int has_input = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 while (!has_input)
963 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 /* [Bug #1552726] Only limit the pause if an input hook has been
966 defined. */
967 struct timeval *timeoutp = NULL;
968 if (PyOS_InputHook)
969 timeoutp = &timeout;
970 FD_SET(fileno(rl_instream), &selectset);
971 /* select resets selectset if no input was available */
972 has_input = select(fileno(rl_instream) + 1, &selectset,
973 NULL, NULL, timeoutp);
974 if(PyOS_InputHook) PyOS_InputHook();
975 }
976
977 if(has_input > 0) {
978 rl_callback_read_char();
979 }
980 else if (errno == EINTR) {
981 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000982#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000984#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000986#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000988#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 if (s < 0) {
990 rl_free_line_state();
991 rl_cleanup_after_signal();
992 rl_callback_handler_remove();
993 *signal = 1;
994 completed_input_string = NULL;
995 }
996 }
997 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 return completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001000}
1001
1002
1003#else
Guido van Rossum290900a1997-09-26 21:51:21 +00001004
1005/* Interrupt handler */
1006
1007static jmp_buf jbuf;
1008
Guido van Rossum0969d361997-08-05 21:27:50 +00001009/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +00001010static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001011onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +00001012{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +00001014}
1015
Guido van Rossum290900a1997-09-26 21:51:21 +00001016
Guido van Rossum0969d361997-08-05 21:27:50 +00001017static char *
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001018readline_until_enter_or_signal(char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +00001019{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 PyOS_sighandler_t old_inthandler;
1021 char *p;
Guido van Rossum74f31432003-01-07 20:01:29 +00001022
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 *signal = 0;
1024
1025 old_inthandler = PyOS_setsig(SIGINT, onintr);
1026 if (setjmp(jbuf)) {
Guido van Rossum0969d361997-08-05 21:27:50 +00001027#ifdef HAVE_SIGRELSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
1029 sigrelse(SIGINT);
Guido van Rossum0969d361997-08-05 21:27:50 +00001030#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 PyOS_setsig(SIGINT, old_inthandler);
1032 *signal = 1;
1033 return NULL;
1034 }
1035 rl_event_hook = PyOS_InputHook;
1036 p = readline(prompt);
1037 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001038
1039 return p;
1040}
1041#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
1042
1043
1044static char *
1045call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
1046{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 size_t n;
1048 char *p, *q;
1049 int signal;
Neal Norwitz1fa040b2004-08-25 01:20:18 +00001050
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001051#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1053 if (!saved_locale)
1054 Py_FatalError("not enough memory to save locale");
1055 setlocale(LC_CTYPE, "");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001056#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
1059 rl_instream = sys_stdin;
1060 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001061#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001063#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 }
Guido van Rossum74f31432003-01-07 20:01:29 +00001065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 p = readline_until_enter_or_signal(prompt, &signal);
Guido van Rossumb18618d2000-05-03 23:44:39 +00001067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 /* we got an interrupt signal */
1069 if (signal) {
1070 RESTORE_LOCALE(saved_locale)
1071 return NULL;
1072 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 /* We got an EOF, return a empty string. */
1075 if (p == NULL) {
1076 p = PyMem_Malloc(1);
1077 if (p != NULL)
1078 *p = '\0';
1079 RESTORE_LOCALE(saved_locale)
1080 return p;
1081 }
1082
1083 /* we have a valid line */
1084 n = strlen(p);
1085 if (n > 0) {
Brett Cannon2525dc82010-08-22 20:36:25 +00001086 const char *line;
Mark Dickinson6b54e1f2010-08-03 16:49:49 +00001087 int length = _py_get_history_length();
1088 if (length > 0)
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001089#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 if (using_libedit_emulation) {
1091 /*
1092 * Libedit's emulation uses 0-based indexes,
1093 * the real readline uses 1-based indexes.
1094 */
Brett Cannon2525dc82010-08-22 20:36:25 +00001095 line = (const char *)history_get(length - 1)->line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 } else
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001097#endif /* __APPLE__ */
Brett Cannon2525dc82010-08-22 20:36:25 +00001098 line = (const char *)history_get(length)->line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 else
1100 line = "";
1101 if (strcmp(p, line))
1102 add_history(p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 }
1104 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
1105 release the original. */
1106 q = p;
1107 p = PyMem_Malloc(n+2);
1108 if (p != NULL) {
1109 strncpy(p, q, n);
1110 p[n] = '\n';
1111 p[n+1] = '\0';
1112 }
1113 free(q);
1114 RESTORE_LOCALE(saved_locale)
1115 return p;
Guido van Rossum0969d361997-08-05 21:27:50 +00001116}
1117
Guido van Rossum290900a1997-09-26 21:51:21 +00001118
1119/* Initialize the module */
1120
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001121PyDoc_STRVAR(doc_module,
1122"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001123
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001124#ifdef __APPLE__
1125PyDoc_STRVAR(doc_module_le,
1126"Importing this module enables command line editing using libedit readline.");
1127#endif /* __APPLE__ */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001128
1129static struct PyModuleDef readlinemodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 PyModuleDef_HEAD_INIT,
1131 "readline",
1132 doc_module,
1133 -1,
1134 readline_methods,
1135 NULL,
1136 NULL,
1137 NULL,
1138 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001139};
1140
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001141
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001142PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001143PyInit_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +00001146
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001147#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
1149 using_libedit_emulation = 1;
1150 }
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 if (using_libedit_emulation)
1153 readlinemodule.m_doc = doc_module_le;
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001154
1155#endif /* __APPLE__ */
1156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 m = PyModule_Create(&readlinemodule);
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 if (m == NULL)
1160 return NULL;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001161
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001162
1163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 PyOS_ReadlineFunctionPointer = call_readline;
1165 setup_readline();
1166 return m;
Guido van Rossum0969d361997-08-05 21:27:50 +00001167}