blob: 233ebf95ad1b40ee55eda6a5e7f50310c9faa260 [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
Neal Norwitz5eaf7722006-07-16 02:15:27 +000023#ifdef SAVE_LOCALE
24# define RESTORE_LOCALE(sl) { setlocale(LC_CTYPE, sl); free(sl); }
25#else
Brett Cannon23b581a2010-05-04 00:52:41 +000026# define RESTORE_LOCALE(sl)
Neal Norwitz5eaf7722006-07-16 02:15:27 +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 Pitrouc83ea132010-05-09 14:46:46 +000036 rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
Neal Norwitzbd538702007-04-19 05:52:37 +000037#else
Martin v. Löwisbb86d832008-11-04 20:40:09 +000038#if defined(_RL_FUNCTION_TYPEDEF)
Neal Norwitzbd538702007-04-19 05:52:37 +000039extern char **completion_matches(char *, rl_compentry_func_t *);
Martin v. Löwisbb86d832008-11-04 20:40:09 +000040#else
Ronald Oussoren333fca92010-02-11 13:13:08 +000041
42#if !defined(__APPLE__)
Martin v. Löwisbb86d832008-11-04 20:40:09 +000043extern char **completion_matches(char *, CPFunction *);
44#endif
Guido van Rossum353ae582001-07-10 16:45:32 +000045#endif
Ronald Oussoren333fca92010-02-11 13:13:08 +000046#endif
Guido van Rossum353ae582001-07-10 16:45:32 +000047
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +000048#ifdef __APPLE__
49/*
50 * It is possible to link the readline module to the readline
Brett Cannon23b581a2010-05-04 00:52:41 +000051 * emulation library of editline/libedit.
52 *
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +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 *
Brett Cannon23b581a2010-05-04 00:52:41 +000057 * Currently there is one know API incompatibility:
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +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
Martin v. Löwisf3548942007-11-12 04:53:02 +000067static void
68on_completion_display_matches_hook(char **matches,
Antoine Pitrouc83ea132010-05-09 14:46:46 +000069 int num_matches, int max_length);
Martin v. Löwisf3548942007-11-12 04:53:02 +000070
Guido van Rossum0969d361997-08-05 21:27:50 +000071
Antoine Pitrou31bc8be2013-05-06 21:51:03 +020072/* Memory allocated for rl_completer_word_break_characters
73 (see issue #17289 for the motivation). */
74static char *completer_word_break_characters;
75
Guido van Rossum290900a1997-09-26 21:51:21 +000076/* Exported function to send one line to readline's init file parser */
77
78static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000079parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000080{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000081 char *s, *copy;
82 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
83 return NULL;
84 /* Make a copy -- rl_parse_and_bind() modifies its argument */
85 /* Bernard Herzog */
86 copy = malloc(1 + strlen(s));
87 if (copy == NULL)
88 return PyErr_NoMemory();
89 strcpy(copy, s);
90 rl_parse_and_bind(copy);
91 free(copy); /* Free the copy */
92 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +000093}
94
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000095PyDoc_STRVAR(doc_parse_and_bind,
96"parse_and_bind(string) -> None\n\
97Parse and execute single line of a readline init file.");
Guido van Rossum290900a1997-09-26 21:51:21 +000098
99
100/* Exported function to parse a readline init file */
101
102static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000103read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000104{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000105 char *s = NULL;
106 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
107 return NULL;
108 errno = rl_read_init_file(s);
109 if (errno)
110 return PyErr_SetFromErrno(PyExc_IOError);
111 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +0000112}
113
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000114PyDoc_STRVAR(doc_read_init_file,
115"read_init_file([filename]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000116Parse a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000117The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000118
119
Skip Montanaro28067822000-07-06 18:55:12 +0000120/* Exported function to load a readline history file */
121
122static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000123read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000124{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000125 char *s = NULL;
126 if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
127 return NULL;
128 errno = read_history(s);
129 if (errno)
130 return PyErr_SetFromErrno(PyExc_IOError);
131 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000132}
133
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000134static int _history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000135PyDoc_STRVAR(doc_read_history_file,
136"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000137Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000138The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000139
140
141/* Exported function to save a readline history file */
142
143static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000144write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000145{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000146 char *s = NULL;
147 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
148 return NULL;
149 errno = write_history(s);
150 if (!errno && _history_length >= 0)
151 history_truncate_file(s, _history_length);
152 if (errno)
153 return PyErr_SetFromErrno(PyExc_IOError);
154 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000155}
156
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000157PyDoc_STRVAR(doc_write_history_file,
158"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000159Save a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000160The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000161
162
Guido van Rossum74f31432003-01-07 20:01:29 +0000163/* Set history length */
164
165static PyObject*
166set_history_length(PyObject *self, PyObject *args)
167{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000168 int length = _history_length;
169 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
170 return NULL;
171 _history_length = length;
172 Py_RETURN_NONE;
Guido van Rossum74f31432003-01-07 20:01:29 +0000173}
174
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000175PyDoc_STRVAR(set_history_length_doc,
176"set_history_length(length) -> None\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000177set the maximal number of items which will be written to\n\
178the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000179history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000180
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000181
Guido van Rossum74f31432003-01-07 20:01:29 +0000182/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000183
184static PyObject*
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000185get_history_length(PyObject *self, PyObject *noarg)
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000186{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000187 return PyInt_FromLong(_history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000188}
189
Guido van Rossum74f31432003-01-07 20:01:29 +0000190PyDoc_STRVAR(get_history_length_doc,
191"get_history_length() -> int\n\
192return the maximum number of items that will be written to\n\
193the history file.");
194
195
Martin v. Löwis0daad592001-09-30 21:09:59 +0000196/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000197
Martin v. Löwis0daad592001-09-30 21:09:59 +0000198static PyObject *
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000199set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000200{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000201 PyObject *function = Py_None;
202 char buf[80];
203 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
204 if (!PyArg_ParseTuple(args, buf, &function))
205 return NULL;
206 if (function == Py_None) {
207 Py_XDECREF(*hook_var);
208 *hook_var = NULL;
209 }
210 else if (PyCallable_Check(function)) {
211 PyObject *tmp = *hook_var;
212 Py_INCREF(function);
213 *hook_var = function;
214 Py_XDECREF(tmp);
215 }
216 else {
217 PyOS_snprintf(buf, sizeof(buf),
218 "set_%.50s(func): argument not callable",
219 funcname);
220 PyErr_SetString(PyExc_TypeError, buf);
221 return NULL;
222 }
223 Py_RETURN_NONE;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000224}
225
Guido van Rossum74f31432003-01-07 20:01:29 +0000226
Martin v. Löwis0daad592001-09-30 21:09:59 +0000227/* Exported functions to specify hook functions in Python */
228
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000229static PyObject *completion_display_matches_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000230static PyObject *startup_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000231
232#ifdef HAVE_RL_PRE_INPUT_HOOK
233static PyObject *pre_input_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000234#endif
235
236static PyObject *
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000237set_completion_display_matches_hook(PyObject *self, PyObject *args)
238{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000239 PyObject *result = set_hook("completion_display_matches_hook",
240 &completion_display_matches_hook, args);
Martin v. Löwisf3548942007-11-12 04:53:02 +0000241#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000242 /* We cannot set this hook globally, since it replaces the
243 default completion display. */
244 rl_completion_display_matches_hook =
245 completion_display_matches_hook ?
Martin v. Löwisbb86d832008-11-04 20:40:09 +0000246#if defined(_RL_FUNCTION_TYPEDEF)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000247 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
Martin v. Löwisbb86d832008-11-04 20:40:09 +0000248#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000249 (VFunction *)on_completion_display_matches_hook : 0;
Martin v. Löwisbb86d832008-11-04 20:40:09 +0000250#endif
Martin v. Löwisf3548942007-11-12 04:53:02 +0000251#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000252 return result;
Martin v. Löwisf3548942007-11-12 04:53:02 +0000253
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000254}
255
256PyDoc_STRVAR(doc_set_completion_display_matches_hook,
257"set_completion_display_matches_hook([function]) -> None\n\
258Set or remove the completion display function.\n\
259The function is called as\n\
260 function(substitution, [matches], longest_match_length)\n\
261once each time matches need to be displayed.");
262
263static PyObject *
Martin v. Löwis0daad592001-09-30 21:09:59 +0000264set_startup_hook(PyObject *self, PyObject *args)
265{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000266 return set_hook("startup_hook", &startup_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000267}
268
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000269PyDoc_STRVAR(doc_set_startup_hook,
270"set_startup_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000271Set or remove the startup_hook function.\n\
272The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000273before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000274
Guido van Rossum74f31432003-01-07 20:01:29 +0000275
Martin v. Löwis0daad592001-09-30 21:09:59 +0000276#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000277
278/* Set pre-input hook */
279
Martin v. Löwis0daad592001-09-30 21:09:59 +0000280static PyObject *
281set_pre_input_hook(PyObject *self, PyObject *args)
282{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000283 return set_hook("pre_input_hook", &pre_input_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000284}
285
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000286PyDoc_STRVAR(doc_set_pre_input_hook,
287"set_pre_input_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000288Set or remove the pre_input_hook function.\n\
289The function is called with no arguments after the first prompt\n\
290has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000291characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000292
Martin v. Löwis0daad592001-09-30 21:09:59 +0000293#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000294
Guido van Rossum74f31432003-01-07 20:01:29 +0000295
Guido van Rossum290900a1997-09-26 21:51:21 +0000296/* Exported function to specify a word completer in Python */
297
298static PyObject *completer = NULL;
Guido van Rossum290900a1997-09-26 21:51:21 +0000299
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000300static PyObject *begidx = NULL;
301static PyObject *endidx = NULL;
302
Guido van Rossum74f31432003-01-07 20:01:29 +0000303
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000304/* Get the completion type for the scope of the tab-completion */
305static PyObject *
306get_completion_type(PyObject *self, PyObject *noarg)
307{
308 return PyInt_FromLong(rl_completion_type);
309}
310
311PyDoc_STRVAR(doc_get_completion_type,
312"get_completion_type() -> int\n\
313Get the type of completion being attempted.");
314
315
Guido van Rossum74f31432003-01-07 20:01:29 +0000316/* Get the beginning index for the scope of the tab-completion */
317
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000318static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000319get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000320{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000321 Py_INCREF(begidx);
322 return begidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000323}
324
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000325PyDoc_STRVAR(doc_get_begidx,
326"get_begidx() -> int\n\
327get the beginning index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000328
Guido van Rossum74f31432003-01-07 20:01:29 +0000329
330/* Get the ending index for the scope of the tab-completion */
331
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000332static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000333get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000334{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000335 Py_INCREF(endidx);
336 return endidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000337}
338
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000339PyDoc_STRVAR(doc_get_endidx,
340"get_endidx() -> int\n\
341get the ending index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000342
343
Guido van Rossum74f31432003-01-07 20:01:29 +0000344/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000345
346static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000347set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000348{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000349 char *break_chars;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000350
Antoine Pitrou31bc8be2013-05-06 21:51:03 +0200351 if (!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000352 return NULL;
353 }
Antoine Pitrou31bc8be2013-05-06 21:51:03 +0200354 /* Keep a reference to the allocated memory in the module state in case
355 some other module modifies rl_completer_word_break_characters
356 (see issue #17289). */
357 free(completer_word_break_characters);
358 completer_word_break_characters = strdup(break_chars);
359 if (completer_word_break_characters) {
360 rl_completer_word_break_characters = completer_word_break_characters;
361 Py_RETURN_NONE;
362 }
363 else
364 return PyErr_NoMemory();
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000365}
366
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000367PyDoc_STRVAR(doc_set_completer_delims,
368"set_completer_delims(string) -> None\n\
369set the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000370
Mark Dickinson4ee98532010-08-03 16:18:39 +0000371/* _py_free_history_entry: Utility function to free a history entry. */
372
373#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
374
375/* Readline version >= 5.0 introduced a timestamp field into the history entry
376 structure; this needs to be freed to avoid a memory leak. This version of
377 readline also introduced the handy 'free_history_entry' function, which
378 takes care of the timestamp. */
379
380static void
381_py_free_history_entry(HIST_ENTRY *entry)
382{
383 histdata_t data = free_history_entry(entry);
384 free(data);
385}
386
387#else
388
389/* No free_history_entry function; free everything manually. */
390
391static void
392_py_free_history_entry(HIST_ENTRY *entry)
393{
394 if (entry->line)
395 free((void *)entry->line);
396 if (entry->data)
397 free(entry->data);
398 free(entry);
399}
400
401#endif
402
Skip Montanaroe5069012004-08-15 14:32:06 +0000403static PyObject *
404py_remove_history(PyObject *self, PyObject *args)
405{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000406 int entry_number;
407 HIST_ENTRY *entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000408
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000409 if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
410 return NULL;
411 if (entry_number < 0) {
412 PyErr_SetString(PyExc_ValueError,
413 "History index cannot be negative");
414 return NULL;
415 }
416 entry = remove_history(entry_number);
417 if (!entry) {
418 PyErr_Format(PyExc_ValueError,
419 "No history item at position %d",
420 entry_number);
421 return NULL;
422 }
423 /* free memory allocated for the history entry */
Mark Dickinson4ee98532010-08-03 16:18:39 +0000424 _py_free_history_entry(entry);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000425 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000426}
427
428PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000429"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000430remove history item given by its position");
431
432static PyObject *
433py_replace_history(PyObject *self, PyObject *args)
434{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000435 int entry_number;
436 char *line;
437 HIST_ENTRY *old_entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000438
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000439 if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number,
440 &line)) {
441 return NULL;
442 }
443 if (entry_number < 0) {
444 PyErr_SetString(PyExc_ValueError,
445 "History index cannot be negative");
446 return NULL;
447 }
448 old_entry = replace_history_entry(entry_number, line, (void *)NULL);
449 if (!old_entry) {
450 PyErr_Format(PyExc_ValueError,
451 "No history item at position %d",
452 entry_number);
453 return NULL;
454 }
455 /* free memory allocated for the old history entry */
Mark Dickinson4ee98532010-08-03 16:18:39 +0000456 _py_free_history_entry(old_entry);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000457 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000458}
459
460PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000461"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000462replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000463
464/* Add a line to the history buffer */
465
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000466static PyObject *
467py_add_history(PyObject *self, PyObject *args)
468{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000469 char *line;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000470
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000471 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
472 return NULL;
473 }
474 add_history(line);
475 Py_RETURN_NONE;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000476}
477
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000478PyDoc_STRVAR(doc_add_history,
479"add_history(string) -> None\n\
480add a line to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000481
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000482
Guido van Rossum74f31432003-01-07 20:01:29 +0000483/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000484
485static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000486get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000487{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000488 return PyString_FromString(rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000489}
Guido van Rossum74f31432003-01-07 20:01:29 +0000490
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000491PyDoc_STRVAR(doc_get_completer_delims,
492"get_completer_delims() -> string\n\
493get the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000494
Guido van Rossum74f31432003-01-07 20:01:29 +0000495
496/* Set the completer function */
497
Guido van Rossum290900a1997-09-26 21:51:21 +0000498static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000499set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000500{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000501 return set_hook("completer", &completer, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000502}
503
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000504PyDoc_STRVAR(doc_set_completer,
505"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000506Set or remove the completer function.\n\
507The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000508for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000509It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000510
Guido van Rossum74f31432003-01-07 20:01:29 +0000511
Michael W. Hudson796df152003-01-30 10:12:51 +0000512static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000513get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000514{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000515 if (completer == NULL) {
516 Py_RETURN_NONE;
517 }
518 Py_INCREF(completer);
519 return completer;
Michael W. Hudson796df152003-01-30 10:12:51 +0000520}
521
522PyDoc_STRVAR(doc_get_completer,
523"get_completer() -> function\n\
524\n\
525Returns current completer function.");
526
Mark Dickinson0f981282010-08-03 16:54:19 +0000527/* Private function to get current length of history. XXX It may be
528 * possible to replace this with a direct use of history_length instead,
529 * but it's not clear whether BSD's libedit keeps history_length up to date.
530 * See issue #8065.*/
531
532static int
533_py_get_history_length(void)
534{
535 HISTORY_STATE *hist_st = history_get_history_state();
536 int length = hist_st->length;
537 /* the history docs don't say so, but the address of hist_st changes each
538 time history_get_history_state is called which makes me think it's
539 freshly malloc'd memory... on the other hand, the address of the last
540 line stays the same as long as history isn't extended, so it appears to
541 be malloc'd but managed by the history package... */
542 free(hist_st);
543 return length;
544}
545
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000546/* Exported function to get any element of history */
547
548static PyObject *
549get_history_item(PyObject *self, PyObject *args)
550{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000551 int idx = 0;
552 HIST_ENTRY *hist_ent;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000553
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000554 if (!PyArg_ParseTuple(args, "i:index", &idx))
555 return NULL;
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +0000556#ifdef __APPLE__
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000557 if (using_libedit_emulation) {
558 /* Libedit emulation uses 0-based indexes,
559 * the real one uses 1-based indexes,
560 * adjust the index to ensure that Python
561 * code doesn't have to worry about the
562 * difference.
563 */
Mark Dickinson0f981282010-08-03 16:54:19 +0000564 int length = _py_get_history_length();
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000565 idx --;
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +0000566
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000567 /*
568 * Apple's readline emulation crashes when
569 * the index is out of range, therefore
570 * test for that and fail gracefully.
571 */
Mark Dickinson0f981282010-08-03 16:54:19 +0000572 if (idx < 0 || idx >= length) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000573 Py_RETURN_NONE;
574 }
575 }
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +0000576#endif /* __APPLE__ */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000577 if ((hist_ent = history_get(idx)))
578 return PyString_FromString(hist_ent->line);
579 else {
580 Py_RETURN_NONE;
581 }
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000582}
583
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000584PyDoc_STRVAR(doc_get_history_item,
585"get_history_item() -> string\n\
586return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000587
Guido van Rossum74f31432003-01-07 20:01:29 +0000588
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000589/* Exported function to get current length of history */
590
591static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000592get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000593{
Mark Dickinson0f981282010-08-03 16:54:19 +0000594 return PyInt_FromLong((long)_py_get_history_length());
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000595}
596
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000597PyDoc_STRVAR(doc_get_current_history_length,
598"get_current_history_length() -> integer\n\
599return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000600
Guido van Rossum74f31432003-01-07 20:01:29 +0000601
Guido van Rossum79378ff1997-10-07 14:53:21 +0000602/* Exported function to read the current line buffer */
603
604static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000605get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000606{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000607 return PyString_FromString(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000608}
609
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000610PyDoc_STRVAR(doc_get_line_buffer,
611"get_line_buffer() -> string\n\
612return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000613
Guido van Rossum74f31432003-01-07 20:01:29 +0000614
Martin v. Löwise7a97962003-09-20 16:08:33 +0000615#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
616
617/* Exported function to clear the current history */
618
619static PyObject *
620py_clear_history(PyObject *self, PyObject *noarg)
621{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000622 clear_history();
623 Py_RETURN_NONE;
Martin v. Löwise7a97962003-09-20 16:08:33 +0000624}
625
626PyDoc_STRVAR(doc_clear_history,
627"clear_history() -> None\n\
628Clear the current readline history.");
629#endif
630
631
Guido van Rossum79378ff1997-10-07 14:53:21 +0000632/* Exported function to insert text into the line buffer */
633
634static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000635insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000636{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000637 char *s;
638 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
639 return NULL;
640 rl_insert_text(s);
641 Py_RETURN_NONE;
Guido van Rossum79378ff1997-10-07 14:53:21 +0000642}
643
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000644PyDoc_STRVAR(doc_insert_text,
645"insert_text(string) -> None\n\
646Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000647
Guido van Rossum74f31432003-01-07 20:01:29 +0000648
649/* Redisplay the line buffer */
650
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000651static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000652redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000653{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000654 rl_redisplay();
655 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000656}
657
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000658PyDoc_STRVAR(doc_redisplay,
659"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000660Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000661contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000662
Guido van Rossum74f31432003-01-07 20:01:29 +0000663
Guido van Rossum290900a1997-09-26 21:51:21 +0000664/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000665
666static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000667{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000668 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
669 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
670 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
671 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
672 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
673 {"read_history_file", read_history_file,
674 METH_VARARGS, doc_read_history_file},
675 {"write_history_file", write_history_file,
676 METH_VARARGS, doc_write_history_file},
677 {"get_history_item", get_history_item,
678 METH_VARARGS, doc_get_history_item},
679 {"get_current_history_length", (PyCFunction)get_current_history_length,
680 METH_NOARGS, doc_get_current_history_length},
681 {"set_history_length", set_history_length,
682 METH_VARARGS, set_history_length_doc},
683 {"get_history_length", get_history_length,
684 METH_NOARGS, get_history_length_doc},
685 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
686 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
687 {"get_completion_type", get_completion_type,
688 METH_NOARGS, doc_get_completion_type},
689 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
690 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000691
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000692 {"set_completer_delims", set_completer_delims,
693 METH_VARARGS, doc_set_completer_delims},
694 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
695 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
696 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
697 {"get_completer_delims", get_completer_delims,
698 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000699
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000700 {"set_completion_display_matches_hook", set_completion_display_matches_hook,
701 METH_VARARGS, doc_set_completion_display_matches_hook},
702 {"set_startup_hook", set_startup_hook,
703 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000704#ifdef HAVE_RL_PRE_INPUT_HOOK
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000705 {"set_pre_input_hook", set_pre_input_hook,
706 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000707#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000708#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000709 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
Martin v. Löwise7a97962003-09-20 16:08:33 +0000710#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000711 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000712};
713
Guido van Rossum05ac4492003-01-07 20:04:12 +0000714
Martin v. Löwis0daad592001-09-30 21:09:59 +0000715/* C function to call the Python hooks. */
716
717static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000718on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000719{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000720 int result = 0;
721 if (func != NULL) {
722 PyObject *r;
Christian Heimes1bc4af42007-11-12 18:58:08 +0000723#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000724 PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000725#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000726 r = PyObject_CallFunction(func, NULL);
727 if (r == NULL)
728 goto error;
729 if (r == Py_None)
730 result = 0;
731 else {
732 result = PyInt_AsLong(r);
733 if (result == -1 && PyErr_Occurred())
734 goto error;
735 }
736 Py_DECREF(r);
737 goto done;
738 error:
739 PyErr_Clear();
740 Py_XDECREF(r);
741 done:
Christian Heimes1bc4af42007-11-12 18:58:08 +0000742#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000743 PyGILState_Release(gilstate);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000744#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000745 return result;
746 }
747 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000748}
749
750static int
751on_startup_hook(void)
752{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000753 return on_hook(startup_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000754}
755
756#ifdef HAVE_RL_PRE_INPUT_HOOK
757static int
758on_pre_input_hook(void)
759{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000760 return on_hook(pre_input_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000761}
762#endif
763
Guido van Rossum05ac4492003-01-07 20:04:12 +0000764
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000765/* C function to call the Python completion_display_matches */
766
767static void
768on_completion_display_matches_hook(char **matches,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000769 int num_matches, int max_length)
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000770{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000771 int i;
772 PyObject *m=NULL, *s=NULL, *r=NULL;
Christian Heimes1bc4af42007-11-12 18:58:08 +0000773#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000774 PyGILState_STATE gilstate = PyGILState_Ensure();
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000775#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000776 m = PyList_New(num_matches);
777 if (m == NULL)
778 goto error;
779 for (i = 0; i < num_matches; i++) {
780 s = PyString_FromString(matches[i+1]);
781 if (s == NULL)
782 goto error;
783 if (PyList_SetItem(m, i, s) == -1)
784 goto error;
785 }
Martin v. Löwisf3548942007-11-12 04:53:02 +0000786
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000787 r = PyObject_CallFunction(completion_display_matches_hook,
788 "sOi", matches[0], m, max_length);
Martin v. Löwisf3548942007-11-12 04:53:02 +0000789
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000790 Py_DECREF(m); m=NULL;
Brett Cannon23b581a2010-05-04 00:52:41 +0000791
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000792 if (r == NULL ||
793 (r != Py_None && PyInt_AsLong(r) == -1 && PyErr_Occurred())) {
794 goto error;
795 }
796 Py_XDECREF(r); r=NULL;
Martin v. Löwisf3548942007-11-12 04:53:02 +0000797
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000798 if (0) {
799 error:
800 PyErr_Clear();
801 Py_XDECREF(m);
802 Py_XDECREF(r);
803 }
Christian Heimes1bc4af42007-11-12 18:58:08 +0000804#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000805 PyGILState_Release(gilstate);
Martin v. Löwisf3548942007-11-12 04:53:02 +0000806#endif
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000807}
808
809
Guido van Rossum290900a1997-09-26 21:51:21 +0000810/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000811
Guido van Rossum290900a1997-09-26 21:51:21 +0000812static char *
Neal Norwitzbd538702007-04-19 05:52:37 +0000813on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000814{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000815 char *result = NULL;
816 if (completer != NULL) {
817 PyObject *r;
Brett Cannon23b581a2010-05-04 00:52:41 +0000818#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000819 PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000820#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000821 rl_attempted_completion_over = 1;
822 r = PyObject_CallFunction(completer, "si", text, state);
823 if (r == NULL)
824 goto error;
825 if (r == Py_None) {
826 result = NULL;
827 }
828 else {
829 char *s = PyString_AsString(r);
830 if (s == NULL)
831 goto error;
832 result = strdup(s);
833 }
834 Py_DECREF(r);
835 goto done;
836 error:
837 PyErr_Clear();
838 Py_XDECREF(r);
839 done:
Brett Cannon23b581a2010-05-04 00:52:41 +0000840#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000841 PyGILState_Release(gilstate);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000842#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000843 return result;
844 }
845 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000846}
847
Guido van Rossum290900a1997-09-26 21:51:21 +0000848
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000849/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000850 * before calling the normal completer */
851
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000852static char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000853flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000854{
Antoine Pitrou119cdef2009-10-19 18:17:18 +0000855#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000856 rl_completion_append_character ='\0';
Antoine Pitroud9ff74e2009-10-26 19:16:46 +0000857#endif
858#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000859 rl_completion_suppress_append = 0;
Antoine Pitrou119cdef2009-10-19 18:17:18 +0000860#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000861 Py_XDECREF(begidx);
862 Py_XDECREF(endidx);
863 begidx = PyInt_FromLong((long) start);
864 endidx = PyInt_FromLong((long) end);
865 return completion_matches(text, *on_completion);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000866}
867
Guido van Rossum05ac4492003-01-07 20:04:12 +0000868
Guido van Rossum290900a1997-09-26 21:51:21 +0000869/* Helper to initialize GNU readline properly. */
870
871static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000872setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000873{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000874#ifdef SAVE_LOCALE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000875 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
876 if (!saved_locale)
877 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000878#endif
879
R. David Murray7a697252010-12-18 03:52:09 +0000880#ifdef __APPLE__
881 /* the libedit readline emulation resets key bindings etc
882 * when calling rl_initialize. So call it upfront
883 */
884 if (using_libedit_emulation)
885 rl_initialize();
886#endif /* __APPLE__ */
887
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000888 using_history();
Skip Montanaroa0392742002-06-11 14:32:46 +0000889
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000890 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000891#if defined(PYOS_OS2) && defined(PYCC_GCC)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000892 /* Allow $if term= in .inputrc to work */
893 rl_terminal_name = getenv("TERM");
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000894#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000895 /* Force rebind of TAB to insert-tab */
896 rl_bind_key('\t', rl_insert);
897 /* Bind both ESC-TAB and ESC-ESC to the completion function */
898 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
899 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
900 /* Set our hook functions */
901 rl_startup_hook = (Function *)on_startup_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000902#ifdef HAVE_RL_PRE_INPUT_HOOK
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000903 rl_pre_input_hook = (Function *)on_pre_input_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000904#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000905 /* Set our completion function */
906 rl_attempted_completion_function = (CPPFunction *)flex_complete;
907 /* Set Python word break characters */
Antoine Pitrou31bc8be2013-05-06 21:51:03 +0200908 completer_word_break_characters =
909 rl_completer_word_break_characters =
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000910 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
911 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000912
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000913 begidx = PyInt_FromLong(0L);
914 endidx = PyInt_FromLong(0L);
915 /* Initialize (allows .inputrc to override)
916 *
917 * XXX: A bug in the readline-2.2 library causes a memory leak
918 * inside this function. Nothing we can do about it.
919 */
R. David Murray7a697252010-12-18 03:52:09 +0000920#ifdef __APPLE__
921 if (using_libedit_emulation)
Antoine Pitrou31bc8be2013-05-06 21:51:03 +0200922 rl_read_init_file(NULL);
R. David Murray7a697252010-12-18 03:52:09 +0000923 else
924#endif /* __APPLE__ */
925 rl_initialize();
926
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000927 RESTORE_LOCALE(saved_locale)
Guido van Rossum290900a1997-09-26 21:51:21 +0000928}
929
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000930/* Wrapper around GNU readline that handles signals differently. */
931
932
933#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
934
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000935static char *completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000936static void
937rlhandler(char *text)
938{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000939 completed_input_string = text;
940 rl_callback_handler_remove();
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000941}
942
943extern PyThreadState* _PyOS_ReadlineTState;
944
945static char *
946readline_until_enter_or_signal(char *prompt, int *signal)
947{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000948 char * not_done_reading = "";
949 fd_set selectset;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000950
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000951 *signal = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000952#ifdef HAVE_RL_CATCH_SIGNAL
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000953 rl_catch_signals = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000954#endif
955
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000956 rl_callback_handler_install (prompt, rlhandler);
957 FD_ZERO(&selectset);
Brett Cannon23b581a2010-05-04 00:52:41 +0000958
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000959 completed_input_string = not_done_reading;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000960
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000961 while (completed_input_string == not_done_reading) {
962 int has_input = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000963
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000964 while (!has_input)
965 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Andrew M. Kuchling62e475b2006-09-07 13:59:38 +0000966
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000967 /* [Bug #1552726] Only limit the pause if an input hook has been
968 defined. */
969 struct timeval *timeoutp = NULL;
970 if (PyOS_InputHook)
971 timeoutp = &timeout;
972 FD_SET(fileno(rl_instream), &selectset);
973 /* select resets selectset if no input was available */
974 has_input = select(fileno(rl_instream) + 1, &selectset,
975 NULL, NULL, timeoutp);
976 if(PyOS_InputHook) PyOS_InputHook();
977 }
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000978
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000979 if(has_input > 0) {
980 rl_callback_read_char();
981 }
982 else if (errno == EINTR) {
983 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000984#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000985 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000986#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000987 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000988#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000989 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000990#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000991 if (s < 0) {
992 rl_free_line_state();
993 rl_cleanup_after_signal();
994 rl_callback_handler_remove();
995 *signal = 1;
996 completed_input_string = NULL;
997 }
998 }
999 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001000
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001001 return completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001002}
1003
1004
1005#else
Guido van Rossum290900a1997-09-26 21:51:21 +00001006
1007/* Interrupt handler */
1008
1009static jmp_buf jbuf;
1010
Guido van Rossum0969d361997-08-05 21:27:50 +00001011/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +00001012static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001013onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +00001014{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001015 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +00001016}
1017
Guido van Rossum290900a1997-09-26 21:51:21 +00001018
Guido van Rossum0969d361997-08-05 21:27:50 +00001019static char *
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001020readline_until_enter_or_signal(char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +00001021{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001022 PyOS_sighandler_t old_inthandler;
1023 char *p;
Brett Cannon23b581a2010-05-04 00:52:41 +00001024
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001025 *signal = 0;
Guido van Rossum74f31432003-01-07 20:01:29 +00001026
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001027 old_inthandler = PyOS_setsig(SIGINT, onintr);
1028 if (setjmp(jbuf)) {
Guido van Rossum0969d361997-08-05 21:27:50 +00001029#ifdef HAVE_SIGRELSE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001030 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
1031 sigrelse(SIGINT);
Guido van Rossum0969d361997-08-05 21:27:50 +00001032#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001033 PyOS_setsig(SIGINT, old_inthandler);
1034 *signal = 1;
1035 return NULL;
1036 }
1037 rl_event_hook = PyOS_InputHook;
1038 p = readline(prompt);
1039 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001040
1041 return p;
1042}
1043#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
1044
1045
1046static char *
1047call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
1048{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001049 size_t n;
1050 char *p, *q;
1051 int signal;
Neal Norwitz1fa040b2004-08-25 01:20:18 +00001052
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001053#ifdef SAVE_LOCALE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001054 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1055 if (!saved_locale)
1056 Py_FatalError("not enough memory to save locale");
Nadeem Vawda1efd9822013-02-02 20:52:54 +01001057 setlocale(LC_CTYPE, "");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001058#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001059
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001060 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
1061 rl_instream = sys_stdin;
1062 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001063#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001064 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001065#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001066 }
Guido van Rossum74f31432003-01-07 20:01:29 +00001067
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001068 p = readline_until_enter_or_signal(prompt, &signal);
Brett Cannon23b581a2010-05-04 00:52:41 +00001069
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001070 /* we got an interrupt signal */
1071 if (signal) {
1072 RESTORE_LOCALE(saved_locale)
1073 return NULL;
1074 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001075
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001076 /* We got an EOF, return a empty string. */
1077 if (p == NULL) {
1078 p = PyMem_Malloc(1);
1079 if (p != NULL)
1080 *p = '\0';
1081 RESTORE_LOCALE(saved_locale)
1082 return p;
1083 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001084
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001085 /* we have a valid line */
1086 n = strlen(p);
1087 if (n > 0) {
1088 const char *line;
Mark Dickinson0f981282010-08-03 16:54:19 +00001089 int length = _py_get_history_length();
1090 if (length > 0)
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00001091#ifdef __APPLE__
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001092 if (using_libedit_emulation) {
1093 /*
1094 * Libedit's emulation uses 0-based indexes,
1095 * the real readline uses 1-based indexes.
1096 */
Mark Dickinson0f981282010-08-03 16:54:19 +00001097 line = history_get(length - 1)->line;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001098 } else
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00001099#endif /* __APPLE__ */
Mark Dickinson0f981282010-08-03 16:54:19 +00001100 line = history_get(length)->line;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001101 else
1102 line = "";
1103 if (strcmp(p, line))
1104 add_history(p);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001105 }
1106 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
1107 release the original. */
1108 q = p;
1109 p = PyMem_Malloc(n+2);
1110 if (p != NULL) {
1111 strncpy(p, q, n);
1112 p[n] = '\n';
1113 p[n+1] = '\0';
1114 }
1115 free(q);
1116 RESTORE_LOCALE(saved_locale)
1117 return p;
Guido van Rossum0969d361997-08-05 21:27:50 +00001118}
1119
Guido van Rossum290900a1997-09-26 21:51:21 +00001120
1121/* Initialize the module */
1122
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001123PyDoc_STRVAR(doc_module,
1124"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001125
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00001126#ifdef __APPLE__
1127PyDoc_STRVAR(doc_module_le,
1128"Importing this module enables command line editing using libedit readline.");
1129#endif /* __APPLE__ */
1130
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001131PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001132initreadline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001133{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001134 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +00001135
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00001136#ifdef __APPLE__
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001137 if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
1138 using_libedit_emulation = 1;
1139 }
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00001140
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001141 if (using_libedit_emulation)
1142 m = Py_InitModule4("readline", readline_methods, doc_module_le,
1143 (PyObject *)NULL, PYTHON_API_VERSION);
1144 else
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00001145
1146#endif /* __APPLE__ */
1147
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001148 m = Py_InitModule4("readline", readline_methods, doc_module,
1149 (PyObject *)NULL, PYTHON_API_VERSION);
1150 if (m == NULL)
1151 return;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001152
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001153 PyOS_ReadlineFunctionPointer = call_readline;
1154 setup_readline();
Guido van Rossum0969d361997-08-05 21:27:50 +00001155}