blob: ffc29f6539dab077b1301bb1d31ad1900a3cbabf [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
Antoine Pitroua7f7deb2013-05-06 21:51:03 +020073/* Memory allocated for rl_completer_word_break_characters
74 (see issue #17289 for the motivation). */
75static char *completer_word_break_characters;
76
Guido van Rossum290900a1997-09-26 21:51:21 +000077/* Exported function to send one line to readline's init file parser */
78
79static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000080parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 char *s, *copy;
83 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
84 return NULL;
85 /* Make a copy -- rl_parse_and_bind() modifies its argument */
86 /* Bernard Herzog */
Victor Stinnerb6404912013-07-07 16:21:41 +020087 copy = PyMem_Malloc(1 + strlen(s));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 if (copy == NULL)
89 return PyErr_NoMemory();
90 strcpy(copy, s);
91 rl_parse_and_bind(copy);
Victor Stinnerb6404912013-07-07 16:21:41 +020092 PyMem_Free(copy); /* Free the copy */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +000094}
95
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000096PyDoc_STRVAR(doc_parse_and_bind,
97"parse_and_bind(string) -> None\n\
98Parse and execute single line of a readline init file.");
Guido van Rossum290900a1997-09-26 21:51:21 +000099
100
101/* Exported function to parse a readline init file */
102
103static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000104read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000105{
Victor Stinner19e65a32010-06-11 22:27:14 +0000106 PyObject *filename_obj = Py_None, *filename_bytes;
107 if (!PyArg_ParseTuple(args, "|O:read_init_file", &filename_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 return NULL;
Victor Stinner19e65a32010-06-11 22:27:14 +0000109 if (filename_obj != Py_None) {
110 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
111 return NULL;
112 errno = rl_read_init_file(PyBytes_AsString(filename_bytes));
113 Py_DECREF(filename_bytes);
114 } else
115 errno = rl_read_init_file(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 if (errno)
117 return PyErr_SetFromErrno(PyExc_IOError);
118 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +0000119}
120
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000121PyDoc_STRVAR(doc_read_init_file,
122"read_init_file([filename]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000123Parse a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000124The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000125
126
Skip Montanaro28067822000-07-06 18:55:12 +0000127/* Exported function to load a readline history file */
128
129static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000130read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000131{
Victor Stinner19e65a32010-06-11 22:27:14 +0000132 PyObject *filename_obj = Py_None, *filename_bytes;
133 if (!PyArg_ParseTuple(args, "|O:read_history_file", &filename_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 return NULL;
Victor Stinner19e65a32010-06-11 22:27:14 +0000135 if (filename_obj != Py_None) {
136 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
137 return NULL;
138 errno = read_history(PyBytes_AsString(filename_bytes));
139 Py_DECREF(filename_bytes);
140 } else
141 errno = read_history(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 if (errno)
143 return PyErr_SetFromErrno(PyExc_IOError);
144 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000145}
146
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000147static int _history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000148PyDoc_STRVAR(doc_read_history_file,
149"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000150Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000151The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000152
153
154/* Exported function to save a readline history file */
155
156static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000157write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000158{
Victor Stinner19e65a32010-06-11 22:27:14 +0000159 PyObject *filename_obj = Py_None, *filename_bytes;
160 char *filename;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100161 int err;
Victor Stinner19e65a32010-06-11 22:27:14 +0000162 if (!PyArg_ParseTuple(args, "|O:write_history_file", &filename_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 return NULL;
Victor Stinner19e65a32010-06-11 22:27:14 +0000164 if (filename_obj != Py_None) {
165 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
166 return NULL;
167 filename = PyBytes_AsString(filename_bytes);
168 } else {
169 filename_bytes = NULL;
170 filename = NULL;
171 }
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100172 errno = err = write_history(filename);
173 if (!err && _history_length >= 0)
Victor Stinner19e65a32010-06-11 22:27:14 +0000174 history_truncate_file(filename, _history_length);
175 Py_XDECREF(filename_bytes);
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100176 errno = err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 if (errno)
178 return PyErr_SetFromErrno(PyExc_IOError);
179 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000180}
181
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000182PyDoc_STRVAR(doc_write_history_file,
183"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000184Save a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000185The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000186
187
Guido van Rossum74f31432003-01-07 20:01:29 +0000188/* Set history length */
189
190static PyObject*
191set_history_length(PyObject *self, PyObject *args)
192{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 int length = _history_length;
194 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
195 return NULL;
196 _history_length = length;
197 Py_RETURN_NONE;
Guido van Rossum74f31432003-01-07 20:01:29 +0000198}
199
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000200PyDoc_STRVAR(set_history_length_doc,
201"set_history_length(length) -> None\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000202set the maximal number of items which will be written to\n\
203the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000204history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000205
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000206
Guido van Rossum74f31432003-01-07 20:01:29 +0000207/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000208
209static PyObject*
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000210get_history_length(PyObject *self, PyObject *noarg)
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000211{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 return PyLong_FromLong(_history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000213}
214
Guido van Rossum74f31432003-01-07 20:01:29 +0000215PyDoc_STRVAR(get_history_length_doc,
216"get_history_length() -> int\n\
217return the maximum number of items that will be written to\n\
218the history file.");
219
220
Martin v. Löwis0daad592001-09-30 21:09:59 +0000221/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000222
Martin v. Löwis0daad592001-09-30 21:09:59 +0000223static PyObject *
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000224set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 PyObject *function = Py_None;
227 char buf[80];
228 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
229 if (!PyArg_ParseTuple(args, buf, &function))
230 return NULL;
231 if (function == Py_None) {
232 Py_XDECREF(*hook_var);
233 *hook_var = NULL;
234 }
235 else if (PyCallable_Check(function)) {
236 PyObject *tmp = *hook_var;
237 Py_INCREF(function);
238 *hook_var = function;
239 Py_XDECREF(tmp);
240 }
241 else {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100242 PyErr_Format(PyExc_TypeError,
243 "set_%.50s(func): argument not callable",
244 funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 return NULL;
246 }
247 Py_RETURN_NONE;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000248}
249
Guido van Rossum74f31432003-01-07 20:01:29 +0000250
Martin v. Löwis0daad592001-09-30 21:09:59 +0000251/* Exported functions to specify hook functions in Python */
252
Thomas Wouters89d996e2007-09-08 17:39:28 +0000253static PyObject *completion_display_matches_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000254static PyObject *startup_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000255
256#ifdef HAVE_RL_PRE_INPUT_HOOK
257static PyObject *pre_input_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000258#endif
259
260static PyObject *
Thomas Wouters89d996e2007-09-08 17:39:28 +0000261set_completion_display_matches_hook(PyObject *self, PyObject *args)
262{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 PyObject *result = set_hook("completion_display_matches_hook",
264 &completion_display_matches_hook, args);
Christian Heimes32fbe592007-11-12 15:01:33 +0000265#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 /* We cannot set this hook globally, since it replaces the
267 default completion display. */
268 rl_completion_display_matches_hook =
269 completion_display_matches_hook ?
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000270#if defined(_RL_FUNCTION_TYPEDEF)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000272#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 (VFunction *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000274#endif
Christian Heimes32fbe592007-11-12 15:01:33 +0000275#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 return result;
Christian Heimes32fbe592007-11-12 15:01:33 +0000277
Thomas Wouters89d996e2007-09-08 17:39:28 +0000278}
279
280PyDoc_STRVAR(doc_set_completion_display_matches_hook,
281"set_completion_display_matches_hook([function]) -> None\n\
282Set or remove the completion display function.\n\
283The function is called as\n\
284 function(substitution, [matches], longest_match_length)\n\
285once each time matches need to be displayed.");
286
287static PyObject *
Martin v. Löwis0daad592001-09-30 21:09:59 +0000288set_startup_hook(PyObject *self, PyObject *args)
289{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 return set_hook("startup_hook", &startup_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000291}
292
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000293PyDoc_STRVAR(doc_set_startup_hook,
294"set_startup_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000295Set or remove the startup_hook function.\n\
296The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000297before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000298
Guido van Rossum74f31432003-01-07 20:01:29 +0000299
Martin v. Löwis0daad592001-09-30 21:09:59 +0000300#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000301
302/* Set pre-input hook */
303
Martin v. Löwis0daad592001-09-30 21:09:59 +0000304static PyObject *
305set_pre_input_hook(PyObject *self, PyObject *args)
306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 return set_hook("pre_input_hook", &pre_input_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000308}
309
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000310PyDoc_STRVAR(doc_set_pre_input_hook,
311"set_pre_input_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000312Set or remove the pre_input_hook function.\n\
313The function is called with no arguments after the first prompt\n\
314has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000315characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000316
Martin v. Löwis0daad592001-09-30 21:09:59 +0000317#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000318
Guido van Rossum74f31432003-01-07 20:01:29 +0000319
Guido van Rossum290900a1997-09-26 21:51:21 +0000320/* Exported function to specify a word completer in Python */
321
322static PyObject *completer = NULL;
Guido van Rossum290900a1997-09-26 21:51:21 +0000323
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000324static PyObject *begidx = NULL;
325static PyObject *endidx = NULL;
326
Guido van Rossum74f31432003-01-07 20:01:29 +0000327
Thomas Wouters89d996e2007-09-08 17:39:28 +0000328/* Get the completion type for the scope of the tab-completion */
329static PyObject *
330get_completion_type(PyObject *self, PyObject *noarg)
331{
Christian Heimes217cfd12007-12-02 14:31:20 +0000332 return PyLong_FromLong(rl_completion_type);
Thomas Wouters89d996e2007-09-08 17:39:28 +0000333}
334
335PyDoc_STRVAR(doc_get_completion_type,
336"get_completion_type() -> int\n\
337Get the type of completion being attempted.");
338
339
Guido van Rossum74f31432003-01-07 20:01:29 +0000340/* Get the beginning index for the scope of the tab-completion */
341
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000342static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000343get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000344{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 Py_INCREF(begidx);
346 return begidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000347}
348
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000349PyDoc_STRVAR(doc_get_begidx,
350"get_begidx() -> int\n\
351get the beginning index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000352
Guido van Rossum74f31432003-01-07 20:01:29 +0000353
354/* Get the ending index for the scope of the tab-completion */
355
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000356static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000357get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000358{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 Py_INCREF(endidx);
360 return endidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000361}
362
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000363PyDoc_STRVAR(doc_get_endidx,
364"get_endidx() -> int\n\
365get the ending index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000366
367
Guido van Rossum74f31432003-01-07 20:01:29 +0000368/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000369
370static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000371set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000372{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 char *break_chars;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000374
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200375 if (!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 return NULL;
377 }
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200378 /* Keep a reference to the allocated memory in the module state in case
379 some other module modifies rl_completer_word_break_characters
380 (see issue #17289). */
381 free(completer_word_break_characters);
382 completer_word_break_characters = strdup(break_chars);
383 if (completer_word_break_characters) {
384 rl_completer_word_break_characters = completer_word_break_characters;
385 Py_RETURN_NONE;
386 }
387 else
388 return PyErr_NoMemory();
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000389}
390
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000391PyDoc_STRVAR(doc_set_completer_delims,
392"set_completer_delims(string) -> None\n\
393set the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000394
Mark Dickinson29b238e2010-08-03 16:08:16 +0000395/* _py_free_history_entry: Utility function to free a history entry. */
396
397#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
398
399/* Readline version >= 5.0 introduced a timestamp field into the history entry
400 structure; this needs to be freed to avoid a memory leak. This version of
401 readline also introduced the handy 'free_history_entry' function, which
402 takes care of the timestamp. */
403
404static void
405_py_free_history_entry(HIST_ENTRY *entry)
406{
407 histdata_t data = free_history_entry(entry);
408 free(data);
409}
410
411#else
412
413/* No free_history_entry function; free everything manually. */
414
415static void
416_py_free_history_entry(HIST_ENTRY *entry)
417{
418 if (entry->line)
419 free((void *)entry->line);
420 if (entry->data)
421 free(entry->data);
422 free(entry);
423}
424
425#endif
426
Skip Montanaroe5069012004-08-15 14:32:06 +0000427static PyObject *
428py_remove_history(PyObject *self, PyObject *args)
429{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 int entry_number;
431 HIST_ENTRY *entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
434 return NULL;
435 if (entry_number < 0) {
436 PyErr_SetString(PyExc_ValueError,
437 "History index cannot be negative");
438 return NULL;
439 }
440 entry = remove_history(entry_number);
441 if (!entry) {
442 PyErr_Format(PyExc_ValueError,
443 "No history item at position %d",
444 entry_number);
445 return NULL;
446 }
447 /* free memory allocated for the history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000448 _py_free_history_entry(entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000450}
451
452PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000453"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000454remove history item given by its position");
455
456static PyObject *
457py_replace_history(PyObject *self, PyObject *args)
458{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 int entry_number;
460 char *line;
461 HIST_ENTRY *old_entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number,
464 &line)) {
465 return NULL;
466 }
467 if (entry_number < 0) {
468 PyErr_SetString(PyExc_ValueError,
469 "History index cannot be negative");
470 return NULL;
471 }
472 old_entry = replace_history_entry(entry_number, line, (void *)NULL);
473 if (!old_entry) {
474 PyErr_Format(PyExc_ValueError,
475 "No history item at position %d",
476 entry_number);
477 return NULL;
478 }
479 /* free memory allocated for the old history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000480 _py_free_history_entry(old_entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000482}
483
484PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000485"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000486replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000487
488/* Add a line to the history buffer */
489
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000490static PyObject *
491py_add_history(PyObject *self, PyObject *args)
492{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 char *line;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
496 return NULL;
497 }
498 add_history(line);
499 Py_RETURN_NONE;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000500}
501
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000502PyDoc_STRVAR(doc_add_history,
503"add_history(string) -> None\n\
504add a line to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000505
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000506
Guido van Rossum74f31432003-01-07 20:01:29 +0000507/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000508
509static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000510get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000511{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 return PyUnicode_FromString(rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000513}
Guido van Rossum74f31432003-01-07 20:01:29 +0000514
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000515PyDoc_STRVAR(doc_get_completer_delims,
516"get_completer_delims() -> string\n\
517get the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000518
Guido van Rossum74f31432003-01-07 20:01:29 +0000519
520/* Set the completer function */
521
Guido van Rossum290900a1997-09-26 21:51:21 +0000522static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000523set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000524{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 return set_hook("completer", &completer, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000526}
527
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000528PyDoc_STRVAR(doc_set_completer,
529"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000530Set or remove the completer function.\n\
531The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000532for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000533It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000534
Guido van Rossum74f31432003-01-07 20:01:29 +0000535
Michael W. Hudson796df152003-01-30 10:12:51 +0000536static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000537get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000538{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 if (completer == NULL) {
540 Py_RETURN_NONE;
541 }
542 Py_INCREF(completer);
543 return completer;
Michael W. Hudson796df152003-01-30 10:12:51 +0000544}
545
546PyDoc_STRVAR(doc_get_completer,
547"get_completer() -> function\n\
548\n\
549Returns current completer function.");
550
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000551/* Private function to get current length of history. XXX It may be
552 * possible to replace this with a direct use of history_length instead,
553 * but it's not clear whether BSD's libedit keeps history_length up to date.
554 * See issue #8065.*/
555
556static int
557_py_get_history_length(void)
558{
559 HISTORY_STATE *hist_st = history_get_history_state();
560 int length = hist_st->length;
561 /* the history docs don't say so, but the address of hist_st changes each
562 time history_get_history_state is called which makes me think it's
563 freshly malloc'd memory... on the other hand, the address of the last
564 line stays the same as long as history isn't extended, so it appears to
565 be malloc'd but managed by the history package... */
566 free(hist_st);
567 return length;
568}
569
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000570/* Exported function to get any element of history */
571
572static PyObject *
573get_history_item(PyObject *self, PyObject *args)
574{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 int idx = 0;
576 HIST_ENTRY *hist_ent;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 if (!PyArg_ParseTuple(args, "i:index", &idx))
579 return NULL;
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000580#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 if (using_libedit_emulation) {
582 /* Libedit emulation uses 0-based indexes,
583 * the real one uses 1-based indexes,
584 * adjust the index to ensure that Python
585 * code doesn't have to worry about the
586 * difference.
587 */
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000588 int length = _py_get_history_length();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 idx --;
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 /*
592 * Apple's readline emulation crashes when
593 * the index is out of range, therefore
594 * test for that and fail gracefully.
595 */
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000596 if (idx < 0 || idx >= length) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 Py_RETURN_NONE;
598 }
599 }
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000600#endif /* __APPLE__ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 if ((hist_ent = history_get(idx)))
602 return PyUnicode_FromString(hist_ent->line);
603 else {
604 Py_RETURN_NONE;
605 }
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000606}
607
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000608PyDoc_STRVAR(doc_get_history_item,
609"get_history_item() -> string\n\
610return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000611
Guido van Rossum74f31432003-01-07 20:01:29 +0000612
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000613/* Exported function to get current length of history */
614
615static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000616get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000617{
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000618 return PyLong_FromLong((long)_py_get_history_length());
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000619}
620
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000621PyDoc_STRVAR(doc_get_current_history_length,
622"get_current_history_length() -> integer\n\
623return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000624
Guido van Rossum74f31432003-01-07 20:01:29 +0000625
Guido van Rossum79378ff1997-10-07 14:53:21 +0000626/* Exported function to read the current line buffer */
627
628static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000629get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 return PyUnicode_FromString(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000632}
633
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000634PyDoc_STRVAR(doc_get_line_buffer,
635"get_line_buffer() -> string\n\
636return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000637
Guido van Rossum74f31432003-01-07 20:01:29 +0000638
Martin v. Löwise7a97962003-09-20 16:08:33 +0000639#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
640
641/* Exported function to clear the current history */
642
643static PyObject *
644py_clear_history(PyObject *self, PyObject *noarg)
645{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 clear_history();
647 Py_RETURN_NONE;
Martin v. Löwise7a97962003-09-20 16:08:33 +0000648}
649
650PyDoc_STRVAR(doc_clear_history,
651"clear_history() -> None\n\
652Clear the current readline history.");
653#endif
654
655
Guido van Rossum79378ff1997-10-07 14:53:21 +0000656/* Exported function to insert text into the line buffer */
657
658static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000659insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000660{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 char *s;
662 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
663 return NULL;
664 rl_insert_text(s);
665 Py_RETURN_NONE;
Guido van Rossum79378ff1997-10-07 14:53:21 +0000666}
667
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000668PyDoc_STRVAR(doc_insert_text,
669"insert_text(string) -> None\n\
670Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000671
Guido van Rossum74f31432003-01-07 20:01:29 +0000672
673/* Redisplay the line buffer */
674
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000675static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000676redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000677{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 rl_redisplay();
679 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000680}
681
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000682PyDoc_STRVAR(doc_redisplay,
683"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000684Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000685contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000686
Guido van Rossum74f31432003-01-07 20:01:29 +0000687
Guido van Rossum290900a1997-09-26 21:51:21 +0000688/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000689
690static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000691{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
693 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
694 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
695 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
696 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
697 {"read_history_file", read_history_file,
698 METH_VARARGS, doc_read_history_file},
699 {"write_history_file", write_history_file,
700 METH_VARARGS, doc_write_history_file},
701 {"get_history_item", get_history_item,
702 METH_VARARGS, doc_get_history_item},
703 {"get_current_history_length", (PyCFunction)get_current_history_length,
704 METH_NOARGS, doc_get_current_history_length},
705 {"set_history_length", set_history_length,
706 METH_VARARGS, set_history_length_doc},
707 {"get_history_length", get_history_length,
708 METH_NOARGS, get_history_length_doc},
709 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
710 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
711 {"get_completion_type", get_completion_type,
712 METH_NOARGS, doc_get_completion_type},
713 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
714 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 {"set_completer_delims", set_completer_delims,
717 METH_VARARGS, doc_set_completer_delims},
718 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
719 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
720 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
721 {"get_completer_delims", get_completer_delims,
722 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 {"set_completion_display_matches_hook", set_completion_display_matches_hook,
725 METH_VARARGS, doc_set_completion_display_matches_hook},
726 {"set_startup_hook", set_startup_hook,
727 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000728#ifdef HAVE_RL_PRE_INPUT_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 {"set_pre_input_hook", set_pre_input_hook,
730 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000731#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000732#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
Martin v. Löwise7a97962003-09-20 16:08:33 +0000734#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000736};
737
Guido van Rossum05ac4492003-01-07 20:04:12 +0000738
Martin v. Löwis0daad592001-09-30 21:09:59 +0000739/* C function to call the Python hooks. */
740
741static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000742on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000743{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 int result = 0;
745 if (func != NULL) {
746 PyObject *r;
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000747#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000749#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 r = PyObject_CallFunction(func, NULL);
751 if (r == NULL)
752 goto error;
753 if (r == Py_None)
754 result = 0;
755 else {
756 result = PyLong_AsLong(r);
757 if (result == -1 && PyErr_Occurred())
758 goto error;
759 }
760 Py_DECREF(r);
761 goto done;
762 error:
763 PyErr_Clear();
764 Py_XDECREF(r);
765 done:
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000766#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 PyGILState_Release(gilstate);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000768#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 return result;
770 }
771 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000772}
773
774static int
775on_startup_hook(void)
776{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 return on_hook(startup_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000778}
779
780#ifdef HAVE_RL_PRE_INPUT_HOOK
781static int
782on_pre_input_hook(void)
783{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 return on_hook(pre_input_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000785}
786#endif
787
Guido van Rossum05ac4492003-01-07 20:04:12 +0000788
Thomas Wouters89d996e2007-09-08 17:39:28 +0000789/* C function to call the Python completion_display_matches */
790
Georg Brandl646fdd62010-10-18 07:27:55 +0000791#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Thomas Wouters89d996e2007-09-08 17:39:28 +0000792static void
793on_completion_display_matches_hook(char **matches,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 int num_matches, int max_length)
Thomas Wouters89d996e2007-09-08 17:39:28 +0000795{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 int i;
797 PyObject *m=NULL, *s=NULL, *r=NULL;
Christian Heimesaec75c32007-11-11 22:42:36 +0000798#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 PyGILState_STATE gilstate = PyGILState_Ensure();
Thomas Wouters89d996e2007-09-08 17:39:28 +0000800#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 m = PyList_New(num_matches);
802 if (m == NULL)
803 goto error;
804 for (i = 0; i < num_matches; i++) {
805 s = PyUnicode_FromString(matches[i+1]);
806 if (s == NULL)
807 goto error;
808 if (PyList_SetItem(m, i, s) == -1)
809 goto error;
810 }
811 r = PyObject_CallFunction(completion_display_matches_hook,
812 "sOi", matches[0], m, max_length);
Christian Heimes32fbe592007-11-12 15:01:33 +0000813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 Py_DECREF(m); m=NULL;
Christian Heimes32fbe592007-11-12 15:01:33 +0000815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 if (r == NULL ||
817 (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) {
818 goto error;
819 }
820 Py_XDECREF(r); r=NULL;
821
822 if (0) {
823 error:
824 PyErr_Clear();
825 Py_XDECREF(m);
826 Py_XDECREF(r);
827 }
Christian Heimes32fbe592007-11-12 15:01:33 +0000828#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 PyGILState_Release(gilstate);
Christian Heimes32fbe592007-11-12 15:01:33 +0000830#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000831}
832
Senthil Kumaran95c07002010-11-04 03:51:05 +0000833#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000834
Guido van Rossum290900a1997-09-26 21:51:21 +0000835/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000836
Guido van Rossum290900a1997-09-26 21:51:21 +0000837static char *
Guido van Rossumd8faa362007-04-27 19:54:29 +0000838on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000839{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 char *result = NULL;
841 if (completer != NULL) {
842 PyObject *r;
Christian Heimesaec75c32007-11-11 22:42:36 +0000843#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000845#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 rl_attempted_completion_over = 1;
847 r = PyObject_CallFunction(completer, "si", text, state);
848 if (r == NULL)
849 goto error;
850 if (r == Py_None) {
851 result = NULL;
852 }
853 else {
854 char *s = _PyUnicode_AsString(r);
855 if (s == NULL)
856 goto error;
857 result = strdup(s);
858 }
859 Py_DECREF(r);
860 goto done;
861 error:
862 PyErr_Clear();
863 Py_XDECREF(r);
864 done:
Christian Heimesaec75c32007-11-11 22:42:36 +0000865#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 PyGILState_Release(gilstate);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000867#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 return result;
869 }
870 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000871}
872
Guido van Rossum290900a1997-09-26 21:51:21 +0000873
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000874/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000875 * before calling the normal completer */
876
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000877static char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000878flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000879{
Antoine Pitroudc0900b2009-10-19 18:22:37 +0000880#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 rl_completion_append_character ='\0';
Antoine Pitroud5131772009-10-26 19:22:14 +0000882#endif
883#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 rl_completion_suppress_append = 0;
Antoine Pitroudc0900b2009-10-19 18:22:37 +0000885#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 Py_XDECREF(begidx);
887 Py_XDECREF(endidx);
888 begidx = PyLong_FromLong((long) start);
889 endidx = PyLong_FromLong((long) end);
890 return completion_matches(text, *on_completion);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000891}
892
Guido van Rossum05ac4492003-01-07 20:04:12 +0000893
Guido van Rossum290900a1997-09-26 21:51:21 +0000894/* Helper to initialize GNU readline properly. */
895
896static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000897setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000898{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000899#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
901 if (!saved_locale)
902 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000903#endif
904
R. David Murray52d1b4e2010-12-18 03:48:32 +0000905#ifdef __APPLE__
Victor Stinner6ced7c42011-03-21 18:15:42 +0100906 /* the libedit readline emulation resets key bindings etc
R. David Murray52d1b4e2010-12-18 03:48:32 +0000907 * when calling rl_initialize. So call it upfront
908 */
909 if (using_libedit_emulation)
910 rl_initialize();
911#endif /* __APPLE__ */
912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 using_history();
Skip Montanaroa0392742002-06-11 14:32:46 +0000914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 rl_readline_name = "python";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 /* Force rebind of TAB to insert-tab */
917 rl_bind_key('\t', rl_insert);
918 /* Bind both ESC-TAB and ESC-ESC to the completion function */
919 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
920 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
921 /* Set our hook functions */
922 rl_startup_hook = (Function *)on_startup_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000923#ifdef HAVE_RL_PRE_INPUT_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 rl_pre_input_hook = (Function *)on_pre_input_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000925#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 /* Set our completion function */
927 rl_attempted_completion_function = (CPPFunction *)flex_complete;
928 /* Set Python word break characters */
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200929 completer_word_break_characters =
930 rl_completer_word_break_characters =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
932 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 begidx = PyLong_FromLong(0L);
935 endidx = PyLong_FromLong(0L);
936 /* Initialize (allows .inputrc to override)
937 *
938 * XXX: A bug in the readline-2.2 library causes a memory leak
939 * inside this function. Nothing we can do about it.
940 */
R. David Murray52d1b4e2010-12-18 03:48:32 +0000941#ifdef __APPLE__
942 if (using_libedit_emulation)
Victor Stinner6ced7c42011-03-21 18:15:42 +0100943 rl_read_init_file(NULL);
R. David Murray52d1b4e2010-12-18 03:48:32 +0000944 else
945#endif /* __APPLE__ */
946 rl_initialize();
Victor Stinner6ced7c42011-03-21 18:15:42 +0100947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 RESTORE_LOCALE(saved_locale)
Guido van Rossum290900a1997-09-26 21:51:21 +0000949}
950
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000951/* Wrapper around GNU readline that handles signals differently. */
952
953
954#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956static char *completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000957static void
958rlhandler(char *text)
959{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 completed_input_string = text;
961 rl_callback_handler_remove();
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000962}
963
964extern PyThreadState* _PyOS_ReadlineTState;
965
966static char *
967readline_until_enter_or_signal(char *prompt, int *signal)
968{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 char * not_done_reading = "";
970 fd_set selectset;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 *signal = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000973#ifdef HAVE_RL_CATCH_SIGNAL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 rl_catch_signals = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000975#endif
976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 rl_callback_handler_install (prompt, rlhandler);
978 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 completed_input_string = not_done_reading;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 while (completed_input_string == not_done_reading) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100983 int has_input = 0, err = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 while (!has_input)
986 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 /* [Bug #1552726] Only limit the pause if an input hook has been
989 defined. */
990 struct timeval *timeoutp = NULL;
991 if (PyOS_InputHook)
992 timeoutp = &timeout;
993 FD_SET(fileno(rl_instream), &selectset);
994 /* select resets selectset if no input was available */
995 has_input = select(fileno(rl_instream) + 1, &selectset,
996 NULL, NULL, timeoutp);
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100997 err = errno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 if(PyOS_InputHook) PyOS_InputHook();
999 }
1000
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001001 if (has_input > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 rl_callback_read_char();
1003 }
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001004 else if (err == EINTR) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001006#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001008#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001010#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001012#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 if (s < 0) {
1014 rl_free_line_state();
1015 rl_cleanup_after_signal();
1016 rl_callback_handler_remove();
1017 *signal = 1;
1018 completed_input_string = NULL;
1019 }
1020 }
1021 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001022
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 return completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001024}
1025
1026
1027#else
Guido van Rossum290900a1997-09-26 21:51:21 +00001028
1029/* Interrupt handler */
1030
1031static jmp_buf jbuf;
1032
Guido van Rossum0969d361997-08-05 21:27:50 +00001033/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +00001034static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001035onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +00001036{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +00001038}
1039
Guido van Rossum290900a1997-09-26 21:51:21 +00001040
Guido van Rossum0969d361997-08-05 21:27:50 +00001041static char *
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001042readline_until_enter_or_signal(char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +00001043{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 PyOS_sighandler_t old_inthandler;
1045 char *p;
Guido van Rossum74f31432003-01-07 20:01:29 +00001046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 *signal = 0;
1048
1049 old_inthandler = PyOS_setsig(SIGINT, onintr);
1050 if (setjmp(jbuf)) {
Guido van Rossum0969d361997-08-05 21:27:50 +00001051#ifdef HAVE_SIGRELSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
1053 sigrelse(SIGINT);
Guido van Rossum0969d361997-08-05 21:27:50 +00001054#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 PyOS_setsig(SIGINT, old_inthandler);
1056 *signal = 1;
1057 return NULL;
1058 }
1059 rl_event_hook = PyOS_InputHook;
1060 p = readline(prompt);
1061 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001062
1063 return p;
1064}
1065#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
1066
1067
1068static char *
1069call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
1070{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 size_t n;
1072 char *p, *q;
1073 int signal;
Neal Norwitz1fa040b2004-08-25 01:20:18 +00001074
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001075#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1077 if (!saved_locale)
1078 Py_FatalError("not enough memory to save locale");
Nadeem Vawda63752572013-02-02 20:05:11 +01001079 setlocale(LC_CTYPE, "");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001080#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
1083 rl_instream = sys_stdin;
1084 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001085#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001087#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 }
Guido van Rossum74f31432003-01-07 20:01:29 +00001089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 p = readline_until_enter_or_signal(prompt, &signal);
Guido van Rossumb18618d2000-05-03 23:44:39 +00001091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 /* we got an interrupt signal */
1093 if (signal) {
1094 RESTORE_LOCALE(saved_locale)
1095 return NULL;
1096 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 /* We got an EOF, return a empty string. */
1099 if (p == NULL) {
1100 p = PyMem_Malloc(1);
1101 if (p != NULL)
1102 *p = '\0';
1103 RESTORE_LOCALE(saved_locale)
1104 return p;
1105 }
1106
1107 /* we have a valid line */
1108 n = strlen(p);
1109 if (n > 0) {
Brett Cannon2525dc82010-08-22 20:36:25 +00001110 const char *line;
Mark Dickinson6b54e1f2010-08-03 16:49:49 +00001111 int length = _py_get_history_length();
1112 if (length > 0)
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001113#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 if (using_libedit_emulation) {
1115 /*
1116 * Libedit's emulation uses 0-based indexes,
1117 * the real readline uses 1-based indexes.
1118 */
Brett Cannon2525dc82010-08-22 20:36:25 +00001119 line = (const char *)history_get(length - 1)->line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 } else
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001121#endif /* __APPLE__ */
Brett Cannon2525dc82010-08-22 20:36:25 +00001122 line = (const char *)history_get(length)->line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 else
1124 line = "";
1125 if (strcmp(p, line))
1126 add_history(p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 }
1128 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
1129 release the original. */
1130 q = p;
1131 p = PyMem_Malloc(n+2);
1132 if (p != NULL) {
1133 strncpy(p, q, n);
1134 p[n] = '\n';
1135 p[n+1] = '\0';
1136 }
1137 free(q);
1138 RESTORE_LOCALE(saved_locale)
1139 return p;
Guido van Rossum0969d361997-08-05 21:27:50 +00001140}
1141
Guido van Rossum290900a1997-09-26 21:51:21 +00001142
1143/* Initialize the module */
1144
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001145PyDoc_STRVAR(doc_module,
1146"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001147
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001148#ifdef __APPLE__
1149PyDoc_STRVAR(doc_module_le,
1150"Importing this module enables command line editing using libedit readline.");
1151#endif /* __APPLE__ */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001152
1153static struct PyModuleDef readlinemodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 PyModuleDef_HEAD_INIT,
1155 "readline",
1156 doc_module,
1157 -1,
1158 readline_methods,
1159 NULL,
1160 NULL,
1161 NULL,
1162 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001163};
1164
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001165
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001166PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001167PyInit_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001168{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +00001170
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001171#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
1173 using_libedit_emulation = 1;
1174 }
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 if (using_libedit_emulation)
1177 readlinemodule.m_doc = doc_module_le;
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001178
1179#endif /* __APPLE__ */
1180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 m = PyModule_Create(&readlinemodule);
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 if (m == NULL)
1184 return NULL;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 PyOS_ReadlineFunctionPointer = call_readline;
1187 setup_readline();
1188 return m;
Guido van Rossum0969d361997-08-05 21:27:50 +00001189}