blob: ad51349bef192ec8e1b8f1a5760600665e978171 [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"
Antoine Pitrou5c30a752013-07-31 21:52:53 +02009#include <stddef.h>
Guido van Rossum0969d361997-08-05 21:27:50 +000010#include <setjmp.h>
11#include <signal.h>
Guido van Rossum290900a1997-09-26 21:51:21 +000012#include <errno.h>
Michael W. Hudson8da2b012004-10-07 13:46:33 +000013#include <sys/time.h>
Guido van Rossum0969d361997-08-05 21:27:50 +000014
Skip Montanaro7befb992004-02-10 16:50:21 +000015#if defined(HAVE_SETLOCALE)
Guido van Rossum60c8a3a2002-10-09 21:27:33 +000016/* GNU readline() mistakenly sets the LC_CTYPE locale.
17 * This is evil. Only the user or the app's main() should do this!
18 * We must save and restore the locale around the rl_initialize() call.
19 */
20#define SAVE_LOCALE
21#include <locale.h>
22#endif
23
Thomas Wouters0e3f5912006-08-11 14:57:12 +000024#ifdef SAVE_LOCALE
25# define RESTORE_LOCALE(sl) { setlocale(LC_CTYPE, sl); free(sl); }
26#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000027# define RESTORE_LOCALE(sl)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000028#endif
29
Guido van Rossum290900a1997-09-26 21:51:21 +000030/* GNU readline definitions */
Guido van Rossumb0e51b22001-04-13 18:14:27 +000031#undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
Guido van Rossumbcc20741998-08-04 22:53:56 +000032#include <readline/readline.h>
33#include <readline/history.h>
Guido van Rossum730806d1998-04-10 22:27:42 +000034
Guido van Rossum353ae582001-07-10 16:45:32 +000035#ifdef HAVE_RL_COMPLETION_MATCHES
Guido van Rossum74f31432003-01-07 20:01:29 +000036#define completion_matches(x, y) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
Guido van Rossumd8faa362007-04-27 19:54:29 +000038#else
Martin v. Löwisb37509b2008-11-04 20:45:29 +000039#if defined(_RL_FUNCTION_TYPEDEF)
Guido van Rossumd8faa362007-04-27 19:54:29 +000040extern char **completion_matches(char *, rl_compentry_func_t *);
Martin v. Löwisb37509b2008-11-04 20:45:29 +000041#else
Ronald Oussoren25696bb2010-02-11 13:15:00 +000042
43#if !defined(__APPLE__)
Martin v. Löwisb37509b2008-11-04 20:45:29 +000044extern char **completion_matches(char *, CPFunction *);
45#endif
Guido van Rossum353ae582001-07-10 16:45:32 +000046#endif
Ronald Oussoren25696bb2010-02-11 13:15:00 +000047#endif
Guido van Rossum353ae582001-07-10 16:45:32 +000048
Ronald Oussoren2efd9242009-09-20 14:53:22 +000049#ifdef __APPLE__
50/*
51 * It is possible to link the readline module to the readline
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 * emulation library of editline/libedit.
53 *
Ronald Oussoren2efd9242009-09-20 14:53:22 +000054 * On OSX this emulation library is not 100% API compatible
55 * with the "real" readline and cannot be detected at compile-time,
56 * hence we use a runtime check to detect if we're using libedit
57 *
Ned Deily5d4121a2013-10-12 15:47:58 -070058 * Currently there is one known API incompatibility:
Ronald Oussoren2efd9242009-09-20 14:53:22 +000059 * - 'get_history' has a 1-based index with GNU readline, and a 0-based
Ned Deily5d4121a2013-10-12 15:47:58 -070060 * index with older versions of libedit's emulation.
Ronald Oussoren2efd9242009-09-20 14:53:22 +000061 * - Note that replace_history and remove_history use a 0-based index
Ned Deily5d4121a2013-10-12 15:47:58 -070062 * with both implementations.
Ronald Oussoren2efd9242009-09-20 14:53:22 +000063 */
64static int using_libedit_emulation = 0;
65static const char libedit_version_tag[] = "EditLine wrapper";
Ned Deilyf70f4a62013-09-06 15:16:19 -070066
67static int libedit_history_start = 0;
Ronald Oussoren2efd9242009-09-20 14:53:22 +000068#endif /* __APPLE__ */
69
Georg Brandl646fdd62010-10-18 07:27:55 +000070#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Christian Heimes32fbe592007-11-12 15:01:33 +000071static void
72on_completion_display_matches_hook(char **matches,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 int num_matches, int max_length);
Georg Brandl646fdd62010-10-18 07:27:55 +000074#endif
Guido van Rossum0969d361997-08-05 21:27:50 +000075
Antoine Pitroua7f7deb2013-05-06 21:51:03 +020076/* Memory allocated for rl_completer_word_break_characters
77 (see issue #17289 for the motivation). */
78static char *completer_word_break_characters;
79
Antoine Pitrou5c30a752013-07-31 21:52:53 +020080typedef struct {
81 PyObject *completion_display_matches_hook;
82 PyObject *startup_hook;
83 PyObject *pre_input_hook;
84 PyObject *completer;
85 PyObject *begidx;
86 PyObject *endidx;
87} readlinestate;
88
89
90#define readline_state(o) ((readlinestate *)PyModule_GetState(o))
91
92static int
93readline_clear(PyObject *m)
94{
95 readlinestate *state = readline_state(m);
96 Py_CLEAR(state->completion_display_matches_hook);
97 Py_CLEAR(state->startup_hook);
98 Py_CLEAR(state->pre_input_hook);
99 Py_CLEAR(state->completer);
100 Py_CLEAR(state->begidx);
101 Py_CLEAR(state->endidx);
102 return 0;
103}
104
105static int
106readline_traverse(PyObject *m, visitproc visit, void *arg)
107{
108 readlinestate *state = readline_state(m);
109 Py_VISIT(state->completion_display_matches_hook);
110 Py_VISIT(state->startup_hook);
111 Py_VISIT(state->pre_input_hook);
112 Py_VISIT(state->completer);
113 Py_VISIT(state->begidx);
114 Py_VISIT(state->endidx);
115 return 0;
116}
117
118static void
119readline_free(void *m)
120{
121 readline_clear((PyObject *)m);
122}
123
124static PyModuleDef readlinemodule;
125
126#define readlinestate_global ((readlinestate *)PyModule_GetState(PyState_FindModule(&readlinemodule)))
127
128
Guido van Rossum290900a1997-09-26 21:51:21 +0000129/* Exported function to send one line to readline's init file parser */
130
131static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000132parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000133{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 char *s, *copy;
135 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
136 return NULL;
137 /* Make a copy -- rl_parse_and_bind() modifies its argument */
138 /* Bernard Herzog */
Victor Stinnerb6404912013-07-07 16:21:41 +0200139 copy = PyMem_Malloc(1 + strlen(s));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 if (copy == NULL)
141 return PyErr_NoMemory();
142 strcpy(copy, s);
143 rl_parse_and_bind(copy);
Victor Stinnerb6404912013-07-07 16:21:41 +0200144 PyMem_Free(copy); /* Free the copy */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +0000146}
147
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000148PyDoc_STRVAR(doc_parse_and_bind,
149"parse_and_bind(string) -> None\n\
150Parse and execute single line of a readline init file.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000151
152
153/* Exported function to parse a readline init file */
154
155static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000156read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000157{
Victor Stinner19e65a32010-06-11 22:27:14 +0000158 PyObject *filename_obj = Py_None, *filename_bytes;
159 if (!PyArg_ParseTuple(args, "|O:read_init_file", &filename_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 return NULL;
Victor Stinner19e65a32010-06-11 22:27:14 +0000161 if (filename_obj != Py_None) {
162 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
163 return NULL;
164 errno = rl_read_init_file(PyBytes_AsString(filename_bytes));
165 Py_DECREF(filename_bytes);
166 } else
167 errno = rl_read_init_file(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 if (errno)
169 return PyErr_SetFromErrno(PyExc_IOError);
170 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +0000171}
172
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000173PyDoc_STRVAR(doc_read_init_file,
174"read_init_file([filename]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000175Parse a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000176The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000177
178
Skip Montanaro28067822000-07-06 18:55:12 +0000179/* Exported function to load a readline history file */
180
181static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000182read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000183{
Victor Stinner19e65a32010-06-11 22:27:14 +0000184 PyObject *filename_obj = Py_None, *filename_bytes;
185 if (!PyArg_ParseTuple(args, "|O:read_history_file", &filename_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 return NULL;
Victor Stinner19e65a32010-06-11 22:27:14 +0000187 if (filename_obj != Py_None) {
188 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
189 return NULL;
190 errno = read_history(PyBytes_AsString(filename_bytes));
191 Py_DECREF(filename_bytes);
192 } else
193 errno = read_history(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 if (errno)
195 return PyErr_SetFromErrno(PyExc_IOError);
196 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000197}
198
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000199static int _history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000200PyDoc_STRVAR(doc_read_history_file,
201"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000202Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000203The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000204
205
206/* Exported function to save a readline history file */
207
208static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000209write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000210{
Victor Stinner19e65a32010-06-11 22:27:14 +0000211 PyObject *filename_obj = Py_None, *filename_bytes;
212 char *filename;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100213 int err;
Victor Stinner19e65a32010-06-11 22:27:14 +0000214 if (!PyArg_ParseTuple(args, "|O:write_history_file", &filename_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 return NULL;
Victor Stinner19e65a32010-06-11 22:27:14 +0000216 if (filename_obj != Py_None) {
217 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
218 return NULL;
219 filename = PyBytes_AsString(filename_bytes);
220 } else {
221 filename_bytes = NULL;
222 filename = NULL;
223 }
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100224 errno = err = write_history(filename);
225 if (!err && _history_length >= 0)
Victor Stinner19e65a32010-06-11 22:27:14 +0000226 history_truncate_file(filename, _history_length);
227 Py_XDECREF(filename_bytes);
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100228 errno = err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 if (errno)
230 return PyErr_SetFromErrno(PyExc_IOError);
231 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000232}
233
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000234PyDoc_STRVAR(doc_write_history_file,
235"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000236Save a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000237The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000238
239
Guido van Rossum74f31432003-01-07 20:01:29 +0000240/* Set history length */
241
242static PyObject*
243set_history_length(PyObject *self, PyObject *args)
244{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 int length = _history_length;
246 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
247 return NULL;
248 _history_length = length;
249 Py_RETURN_NONE;
Guido van Rossum74f31432003-01-07 20:01:29 +0000250}
251
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000252PyDoc_STRVAR(set_history_length_doc,
253"set_history_length(length) -> None\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000254set the maximal number of items which will be written to\n\
255the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000256history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000257
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000258
Guido van Rossum74f31432003-01-07 20:01:29 +0000259/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000260
261static PyObject*
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000262get_history_length(PyObject *self, PyObject *noarg)
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 return PyLong_FromLong(_history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000265}
266
Guido van Rossum74f31432003-01-07 20:01:29 +0000267PyDoc_STRVAR(get_history_length_doc,
268"get_history_length() -> int\n\
269return the maximum number of items that will be written to\n\
270the history file.");
271
272
Martin v. Löwis0daad592001-09-30 21:09:59 +0000273/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000274
Martin v. Löwis0daad592001-09-30 21:09:59 +0000275static PyObject *
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000276set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000277{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 PyObject *function = Py_None;
279 char buf[80];
280 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
281 if (!PyArg_ParseTuple(args, buf, &function))
282 return NULL;
283 if (function == Py_None) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200284 Py_CLEAR(*hook_var);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 }
286 else if (PyCallable_Check(function)) {
287 PyObject *tmp = *hook_var;
288 Py_INCREF(function);
289 *hook_var = function;
290 Py_XDECREF(tmp);
291 }
292 else {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100293 PyErr_Format(PyExc_TypeError,
294 "set_%.50s(func): argument not callable",
295 funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 return NULL;
297 }
298 Py_RETURN_NONE;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000299}
300
Guido van Rossum74f31432003-01-07 20:01:29 +0000301
Martin v. Löwis0daad592001-09-30 21:09:59 +0000302/* Exported functions to specify hook functions in Python */
303
Martin v. Löwis0daad592001-09-30 21:09:59 +0000304
305#ifdef HAVE_RL_PRE_INPUT_HOOK
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200306
Martin v. Löwis0daad592001-09-30 21:09:59 +0000307#endif
308
309static PyObject *
Thomas Wouters89d996e2007-09-08 17:39:28 +0000310set_completion_display_matches_hook(PyObject *self, PyObject *args)
311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 PyObject *result = set_hook("completion_display_matches_hook",
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200313 &readlinestate_global->completion_display_matches_hook, args);
Christian Heimes32fbe592007-11-12 15:01:33 +0000314#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 /* We cannot set this hook globally, since it replaces the
316 default completion display. */
317 rl_completion_display_matches_hook =
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200318 readlinestate_global->completion_display_matches_hook ?
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000319#if defined(_RL_FUNCTION_TYPEDEF)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000321#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 (VFunction *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000323#endif
Christian Heimes32fbe592007-11-12 15:01:33 +0000324#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 return result;
Christian Heimes32fbe592007-11-12 15:01:33 +0000326
Thomas Wouters89d996e2007-09-08 17:39:28 +0000327}
328
329PyDoc_STRVAR(doc_set_completion_display_matches_hook,
330"set_completion_display_matches_hook([function]) -> None\n\
331Set or remove the completion display function.\n\
332The function is called as\n\
333 function(substitution, [matches], longest_match_length)\n\
334once each time matches need to be displayed.");
335
336static PyObject *
Martin v. Löwis0daad592001-09-30 21:09:59 +0000337set_startup_hook(PyObject *self, PyObject *args)
338{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200339 return set_hook("startup_hook", &readlinestate_global->startup_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000340}
341
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000342PyDoc_STRVAR(doc_set_startup_hook,
343"set_startup_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000344Set or remove the startup_hook function.\n\
345The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000346before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000347
Guido van Rossum74f31432003-01-07 20:01:29 +0000348
Martin v. Löwis0daad592001-09-30 21:09:59 +0000349#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000350
351/* Set pre-input hook */
352
Martin v. Löwis0daad592001-09-30 21:09:59 +0000353static PyObject *
354set_pre_input_hook(PyObject *self, PyObject *args)
355{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200356 return set_hook("pre_input_hook", &readlinestate_global->pre_input_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000357}
358
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000359PyDoc_STRVAR(doc_set_pre_input_hook,
360"set_pre_input_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000361Set or remove the pre_input_hook function.\n\
362The function is called with no arguments after the first prompt\n\
363has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000364characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000365
Martin v. Löwis0daad592001-09-30 21:09:59 +0000366#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000367
Guido van Rossum74f31432003-01-07 20:01:29 +0000368
Guido van Rossum290900a1997-09-26 21:51:21 +0000369/* Exported function to specify a word completer in Python */
370
Guido van Rossum290900a1997-09-26 21:51:21 +0000371
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200372
373
374
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000375
Guido van Rossum74f31432003-01-07 20:01:29 +0000376
Thomas Wouters89d996e2007-09-08 17:39:28 +0000377/* Get the completion type for the scope of the tab-completion */
378static PyObject *
379get_completion_type(PyObject *self, PyObject *noarg)
380{
Christian Heimes217cfd12007-12-02 14:31:20 +0000381 return PyLong_FromLong(rl_completion_type);
Thomas Wouters89d996e2007-09-08 17:39:28 +0000382}
383
384PyDoc_STRVAR(doc_get_completion_type,
385"get_completion_type() -> int\n\
386Get the type of completion being attempted.");
387
388
Guido van Rossum74f31432003-01-07 20:01:29 +0000389/* Get the beginning index for the scope of the tab-completion */
390
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000391static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000392get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000393{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200394 Py_INCREF(readlinestate_global->begidx);
395 return readlinestate_global->begidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000396}
397
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000398PyDoc_STRVAR(doc_get_begidx,
399"get_begidx() -> int\n\
400get the beginning index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000401
Guido van Rossum74f31432003-01-07 20:01:29 +0000402
403/* Get the ending index for the scope of the tab-completion */
404
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000405static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000406get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000407{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200408 Py_INCREF(readlinestate_global->endidx);
409 return readlinestate_global->endidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000410}
411
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000412PyDoc_STRVAR(doc_get_endidx,
413"get_endidx() -> int\n\
414get the ending index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000415
416
Guido van Rossum74f31432003-01-07 20:01:29 +0000417/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000418
419static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000420set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000421{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 char *break_chars;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000423
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200424 if (!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 return NULL;
426 }
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200427 /* Keep a reference to the allocated memory in the module state in case
428 some other module modifies rl_completer_word_break_characters
429 (see issue #17289). */
Serhiy Storchaka11384392015-09-27 22:34:59 +0300430 break_chars = strdup(break_chars);
431 if (break_chars) {
432 free(completer_word_break_characters);
433 completer_word_break_characters = break_chars;
434 rl_completer_word_break_characters = break_chars;
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200435 Py_RETURN_NONE;
436 }
437 else
438 return PyErr_NoMemory();
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000439}
440
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000441PyDoc_STRVAR(doc_set_completer_delims,
442"set_completer_delims(string) -> None\n\
443set the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000444
Mark Dickinson29b238e2010-08-03 16:08:16 +0000445/* _py_free_history_entry: Utility function to free a history entry. */
446
447#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
448
449/* Readline version >= 5.0 introduced a timestamp field into the history entry
450 structure; this needs to be freed to avoid a memory leak. This version of
451 readline also introduced the handy 'free_history_entry' function, which
452 takes care of the timestamp. */
453
454static void
455_py_free_history_entry(HIST_ENTRY *entry)
456{
457 histdata_t data = free_history_entry(entry);
458 free(data);
459}
460
461#else
462
463/* No free_history_entry function; free everything manually. */
464
465static void
466_py_free_history_entry(HIST_ENTRY *entry)
467{
468 if (entry->line)
469 free((void *)entry->line);
470 if (entry->data)
471 free(entry->data);
472 free(entry);
473}
474
475#endif
476
Skip Montanaroe5069012004-08-15 14:32:06 +0000477static PyObject *
478py_remove_history(PyObject *self, PyObject *args)
479{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 int entry_number;
481 HIST_ENTRY *entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
484 return NULL;
485 if (entry_number < 0) {
486 PyErr_SetString(PyExc_ValueError,
487 "History index cannot be negative");
488 return NULL;
489 }
490 entry = remove_history(entry_number);
491 if (!entry) {
492 PyErr_Format(PyExc_ValueError,
493 "No history item at position %d",
494 entry_number);
495 return NULL;
496 }
497 /* free memory allocated for the history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000498 _py_free_history_entry(entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000500}
501
502PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000503"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000504remove history item given by its position");
505
506static PyObject *
507py_replace_history(PyObject *self, PyObject *args)
508{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 int entry_number;
510 char *line;
511 HIST_ENTRY *old_entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number,
514 &line)) {
515 return NULL;
516 }
517 if (entry_number < 0) {
518 PyErr_SetString(PyExc_ValueError,
519 "History index cannot be negative");
520 return NULL;
521 }
522 old_entry = replace_history_entry(entry_number, line, (void *)NULL);
523 if (!old_entry) {
524 PyErr_Format(PyExc_ValueError,
525 "No history item at position %d",
526 entry_number);
527 return NULL;
528 }
529 /* free memory allocated for the old history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000530 _py_free_history_entry(old_entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000532}
533
534PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000535"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000536replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000537
538/* Add a line to the history buffer */
539
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000540static PyObject *
541py_add_history(PyObject *self, PyObject *args)
542{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 char *line;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
546 return NULL;
547 }
548 add_history(line);
549 Py_RETURN_NONE;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000550}
551
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000552PyDoc_STRVAR(doc_add_history,
553"add_history(string) -> None\n\
554add a line to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000555
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000556
Guido van Rossum74f31432003-01-07 20:01:29 +0000557/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000558
559static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000560get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000561{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 return PyUnicode_FromString(rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000563}
Guido van Rossum74f31432003-01-07 20:01:29 +0000564
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000565PyDoc_STRVAR(doc_get_completer_delims,
566"get_completer_delims() -> string\n\
567get the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000568
Guido van Rossum74f31432003-01-07 20:01:29 +0000569
570/* Set the completer function */
571
Guido van Rossum290900a1997-09-26 21:51:21 +0000572static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000573set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000574{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200575 return set_hook("completer", &readlinestate_global->completer, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000576}
577
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000578PyDoc_STRVAR(doc_set_completer,
579"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000580Set or remove the completer function.\n\
581The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000582for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000583It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000584
Guido van Rossum74f31432003-01-07 20:01:29 +0000585
Michael W. Hudson796df152003-01-30 10:12:51 +0000586static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000587get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000588{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200589 if (readlinestate_global->completer == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 Py_RETURN_NONE;
591 }
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200592 Py_INCREF(readlinestate_global->completer);
593 return readlinestate_global->completer;
Michael W. Hudson796df152003-01-30 10:12:51 +0000594}
595
596PyDoc_STRVAR(doc_get_completer,
597"get_completer() -> function\n\
598\n\
599Returns current completer function.");
600
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000601/* Private function to get current length of history. XXX It may be
602 * possible to replace this with a direct use of history_length instead,
603 * but it's not clear whether BSD's libedit keeps history_length up to date.
604 * See issue #8065.*/
605
606static int
607_py_get_history_length(void)
608{
609 HISTORY_STATE *hist_st = history_get_history_state();
610 int length = hist_st->length;
611 /* the history docs don't say so, but the address of hist_st changes each
612 time history_get_history_state is called which makes me think it's
613 freshly malloc'd memory... on the other hand, the address of the last
614 line stays the same as long as history isn't extended, so it appears to
615 be malloc'd but managed by the history package... */
616 free(hist_st);
617 return length;
618}
619
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000620/* Exported function to get any element of history */
621
622static PyObject *
623get_history_item(PyObject *self, PyObject *args)
624{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 int idx = 0;
626 HIST_ENTRY *hist_ent;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 if (!PyArg_ParseTuple(args, "i:index", &idx))
629 return NULL;
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000630#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 if (using_libedit_emulation) {
Ned Deilyf70f4a62013-09-06 15:16:19 -0700632 /* Older versions of libedit's readline emulation
633 * use 0-based indexes, while readline and newer
634 * versions of libedit use 1-based indexes.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 */
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000636 int length = _py_get_history_length();
Ned Deilyf70f4a62013-09-06 15:16:19 -0700637
638 idx = idx - 1 + libedit_history_start;
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 /*
641 * Apple's readline emulation crashes when
642 * the index is out of range, therefore
643 * test for that and fail gracefully.
644 */
Ned Deilyf70f4a62013-09-06 15:16:19 -0700645 if (idx < (0 + libedit_history_start)
646 || idx >= (length + libedit_history_start)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 Py_RETURN_NONE;
648 }
649 }
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000650#endif /* __APPLE__ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 if ((hist_ent = history_get(idx)))
652 return PyUnicode_FromString(hist_ent->line);
653 else {
654 Py_RETURN_NONE;
655 }
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000656}
657
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000658PyDoc_STRVAR(doc_get_history_item,
659"get_history_item() -> string\n\
660return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000661
Guido van Rossum74f31432003-01-07 20:01:29 +0000662
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000663/* Exported function to get current length of history */
664
665static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000666get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000667{
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000668 return PyLong_FromLong((long)_py_get_history_length());
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000669}
670
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000671PyDoc_STRVAR(doc_get_current_history_length,
672"get_current_history_length() -> integer\n\
673return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000674
Guido van Rossum74f31432003-01-07 20:01:29 +0000675
Guido van Rossum79378ff1997-10-07 14:53:21 +0000676/* Exported function to read the current line buffer */
677
678static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000679get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000680{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 return PyUnicode_FromString(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000682}
683
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000684PyDoc_STRVAR(doc_get_line_buffer,
685"get_line_buffer() -> string\n\
686return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000687
Guido van Rossum74f31432003-01-07 20:01:29 +0000688
Martin v. Löwise7a97962003-09-20 16:08:33 +0000689#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
690
691/* Exported function to clear the current history */
692
693static PyObject *
694py_clear_history(PyObject *self, PyObject *noarg)
695{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 clear_history();
697 Py_RETURN_NONE;
Martin v. Löwise7a97962003-09-20 16:08:33 +0000698}
699
700PyDoc_STRVAR(doc_clear_history,
701"clear_history() -> None\n\
702Clear the current readline history.");
703#endif
704
705
Guido van Rossum79378ff1997-10-07 14:53:21 +0000706/* Exported function to insert text into the line buffer */
707
708static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000709insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000710{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 char *s;
712 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
713 return NULL;
714 rl_insert_text(s);
715 Py_RETURN_NONE;
Guido van Rossum79378ff1997-10-07 14:53:21 +0000716}
717
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000718PyDoc_STRVAR(doc_insert_text,
719"insert_text(string) -> None\n\
720Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000721
Guido van Rossum74f31432003-01-07 20:01:29 +0000722
723/* Redisplay the line buffer */
724
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000725static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000726redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000727{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 rl_redisplay();
729 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000730}
731
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000732PyDoc_STRVAR(doc_redisplay,
733"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000734Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000735contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000736
Guido van Rossum74f31432003-01-07 20:01:29 +0000737
Guido van Rossum290900a1997-09-26 21:51:21 +0000738/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000739
740static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
743 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
744 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
745 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
746 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
747 {"read_history_file", read_history_file,
748 METH_VARARGS, doc_read_history_file},
749 {"write_history_file", write_history_file,
750 METH_VARARGS, doc_write_history_file},
751 {"get_history_item", get_history_item,
752 METH_VARARGS, doc_get_history_item},
753 {"get_current_history_length", (PyCFunction)get_current_history_length,
754 METH_NOARGS, doc_get_current_history_length},
755 {"set_history_length", set_history_length,
756 METH_VARARGS, set_history_length_doc},
757 {"get_history_length", get_history_length,
758 METH_NOARGS, get_history_length_doc},
759 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
760 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
761 {"get_completion_type", get_completion_type,
762 METH_NOARGS, doc_get_completion_type},
763 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
764 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 {"set_completer_delims", set_completer_delims,
767 METH_VARARGS, doc_set_completer_delims},
768 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
769 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
770 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
771 {"get_completer_delims", get_completer_delims,
772 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 {"set_completion_display_matches_hook", set_completion_display_matches_hook,
775 METH_VARARGS, doc_set_completion_display_matches_hook},
776 {"set_startup_hook", set_startup_hook,
777 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000778#ifdef HAVE_RL_PRE_INPUT_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 {"set_pre_input_hook", set_pre_input_hook,
780 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000781#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000782#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
Martin v. Löwise7a97962003-09-20 16:08:33 +0000784#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000786};
787
Guido van Rossum05ac4492003-01-07 20:04:12 +0000788
Martin v. Löwis0daad592001-09-30 21:09:59 +0000789/* C function to call the Python hooks. */
790
791static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000792on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000793{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 int result = 0;
795 if (func != NULL) {
796 PyObject *r;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 r = PyObject_CallFunction(func, NULL);
798 if (r == NULL)
799 goto error;
800 if (r == Py_None)
801 result = 0;
802 else {
803 result = PyLong_AsLong(r);
804 if (result == -1 && PyErr_Occurred())
805 goto error;
806 }
807 Py_DECREF(r);
808 goto done;
809 error:
810 PyErr_Clear();
811 Py_XDECREF(r);
812 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 return result;
814 }
815 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000816}
817
818static int
Ned Deily7b9ddea2014-02-05 16:53:10 -0800819#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000820on_startup_hook(void)
Ned Deily7b9ddea2014-02-05 16:53:10 -0800821#else
822on_startup_hook()
823#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000824{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200825 int r;
826#ifdef WITH_THREAD
827 PyGILState_STATE gilstate = PyGILState_Ensure();
828#endif
829 r = on_hook(readlinestate_global->startup_hook);
830#ifdef WITH_THREAD
831 PyGILState_Release(gilstate);
832#endif
833 return r;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000834}
835
836#ifdef HAVE_RL_PRE_INPUT_HOOK
837static int
Ned Deily7b9ddea2014-02-05 16:53:10 -0800838#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000839on_pre_input_hook(void)
Ned Deily7b9ddea2014-02-05 16:53:10 -0800840#else
841on_pre_input_hook()
842#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000843{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200844 int r;
845#ifdef WITH_THREAD
846 PyGILState_STATE gilstate = PyGILState_Ensure();
847#endif
848 r = on_hook(readlinestate_global->pre_input_hook);
849#ifdef WITH_THREAD
850 PyGILState_Release(gilstate);
851#endif
852 return r;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000853}
854#endif
855
Guido van Rossum05ac4492003-01-07 20:04:12 +0000856
Thomas Wouters89d996e2007-09-08 17:39:28 +0000857/* C function to call the Python completion_display_matches */
858
Georg Brandl646fdd62010-10-18 07:27:55 +0000859#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Thomas Wouters89d996e2007-09-08 17:39:28 +0000860static void
861on_completion_display_matches_hook(char **matches,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 int num_matches, int max_length)
Thomas Wouters89d996e2007-09-08 17:39:28 +0000863{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 int i;
865 PyObject *m=NULL, *s=NULL, *r=NULL;
Christian Heimesaec75c32007-11-11 22:42:36 +0000866#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 PyGILState_STATE gilstate = PyGILState_Ensure();
Thomas Wouters89d996e2007-09-08 17:39:28 +0000868#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 m = PyList_New(num_matches);
870 if (m == NULL)
871 goto error;
872 for (i = 0; i < num_matches; i++) {
873 s = PyUnicode_FromString(matches[i+1]);
874 if (s == NULL)
875 goto error;
876 if (PyList_SetItem(m, i, s) == -1)
877 goto error;
878 }
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200879 r = PyObject_CallFunction(readlinestate_global->completion_display_matches_hook,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 "sOi", matches[0], m, max_length);
Christian Heimes32fbe592007-11-12 15:01:33 +0000881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 Py_DECREF(m); m=NULL;
Christian Heimes32fbe592007-11-12 15:01:33 +0000883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 if (r == NULL ||
885 (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) {
886 goto error;
887 }
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200888 Py_CLEAR(r);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889
890 if (0) {
891 error:
892 PyErr_Clear();
893 Py_XDECREF(m);
894 Py_XDECREF(r);
895 }
Christian Heimes32fbe592007-11-12 15:01:33 +0000896#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 PyGILState_Release(gilstate);
Christian Heimes32fbe592007-11-12 15:01:33 +0000898#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000899}
900
Senthil Kumaran95c07002010-11-04 03:51:05 +0000901#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000902
Guido van Rossum290900a1997-09-26 21:51:21 +0000903/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000904
Guido van Rossum290900a1997-09-26 21:51:21 +0000905static char *
Guido van Rossumd8faa362007-04-27 19:54:29 +0000906on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000907{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 char *result = NULL;
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200909 if (readlinestate_global->completer != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 PyObject *r;
Christian Heimesaec75c32007-11-11 22:42:36 +0000911#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000913#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 rl_attempted_completion_over = 1;
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200915 r = PyObject_CallFunction(readlinestate_global->completer, "si", text, state);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 if (r == NULL)
917 goto error;
918 if (r == Py_None) {
919 result = NULL;
920 }
921 else {
922 char *s = _PyUnicode_AsString(r);
923 if (s == NULL)
924 goto error;
925 result = strdup(s);
926 }
927 Py_DECREF(r);
928 goto done;
929 error:
930 PyErr_Clear();
931 Py_XDECREF(r);
932 done:
Christian Heimesaec75c32007-11-11 22:42:36 +0000933#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 PyGILState_Release(gilstate);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000935#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 return result;
937 }
938 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000939}
940
Guido van Rossum290900a1997-09-26 21:51:21 +0000941
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000942/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000943 * before calling the normal completer */
944
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000945static char **
Benjamin Petersonf0b463a2014-01-24 11:44:16 -0500946flex_complete(const char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000947{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200948 char **result;
949#ifdef WITH_THREAD
950 PyGILState_STATE gilstate = PyGILState_Ensure();
951#endif
Antoine Pitroudc0900b2009-10-19 18:22:37 +0000952#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 rl_completion_append_character ='\0';
Antoine Pitroud5131772009-10-26 19:22:14 +0000954#endif
955#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 rl_completion_suppress_append = 0;
Antoine Pitroudc0900b2009-10-19 18:22:37 +0000957#endif
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200958 Py_XDECREF(readlinestate_global->begidx);
959 Py_XDECREF(readlinestate_global->endidx);
960 readlinestate_global->begidx = PyLong_FromLong((long) start);
961 readlinestate_global->endidx = PyLong_FromLong((long) end);
962 result = completion_matches(text, *on_completion);
963#ifdef WITH_THREAD
964 PyGILState_Release(gilstate);
965#endif
966 return result;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000967}
968
Guido van Rossum05ac4492003-01-07 20:04:12 +0000969
Guido van Rossum290900a1997-09-26 21:51:21 +0000970/* Helper to initialize GNU readline properly. */
971
972static void
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200973setup_readline(readlinestate *mod_state)
Guido van Rossum290900a1997-09-26 21:51:21 +0000974{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000975#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
977 if (!saved_locale)
978 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000979#endif
980
R. David Murray52d1b4e2010-12-18 03:48:32 +0000981#ifdef __APPLE__
Victor Stinner6ced7c42011-03-21 18:15:42 +0100982 /* the libedit readline emulation resets key bindings etc
R. David Murray52d1b4e2010-12-18 03:48:32 +0000983 * when calling rl_initialize. So call it upfront
984 */
985 if (using_libedit_emulation)
986 rl_initialize();
Ned Deilyf70f4a62013-09-06 15:16:19 -0700987
988 /* Detect if libedit's readline emulation uses 0-based
989 * indexing or 1-based indexing.
990 */
991 add_history("1");
992 if (history_get(1) == NULL) {
993 libedit_history_start = 0;
994 } else {
995 libedit_history_start = 1;
996 }
997 clear_history();
R. David Murray52d1b4e2010-12-18 03:48:32 +0000998#endif /* __APPLE__ */
999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 using_history();
Skip Montanaroa0392742002-06-11 14:32:46 +00001001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 rl_readline_name = "python";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 /* Force rebind of TAB to insert-tab */
1004 rl_bind_key('\t', rl_insert);
1005 /* Bind both ESC-TAB and ESC-ESC to the completion function */
1006 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
1007 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
1008 /* Set our hook functions */
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001009 rl_startup_hook = on_startup_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001010#ifdef HAVE_RL_PRE_INPUT_HOOK
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001011 rl_pre_input_hook = on_pre_input_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001012#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 /* Set our completion function */
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001014 rl_attempted_completion_function = flex_complete;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 /* Set Python word break characters */
Antoine Pitroua7f7deb2013-05-06 21:51:03 +02001016 completer_word_break_characters =
1017 rl_completer_word_break_characters =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
1019 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001020
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001021 mod_state->begidx = PyLong_FromLong(0L);
1022 mod_state->endidx = PyLong_FromLong(0L);
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001023
Victor Stinner92639cc2014-07-24 22:11:38 +02001024#ifndef __APPLE__
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001025 if (!isatty(STDOUT_FILENO)) {
1026 /* Issue #19884: stdout is no a terminal. Disable meta modifier
1027 keys to not write the ANSI sequence "\033[1034h" into stdout. On
1028 terminals supporting 8 bit characters like TERM=xterm-256color
1029 (which is now the default Fedora since Fedora 18), the meta key is
1030 used to enable support of 8 bit characters (ANSI sequence
Victor Stinner92639cc2014-07-24 22:11:38 +02001031 "\033[1034h").
1032
1033 With libedit, this call makes readline() crash. */
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001034 rl_variable_bind ("enable-meta-key", "off");
1035 }
Victor Stinner92639cc2014-07-24 22:11:38 +02001036#endif
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 /* Initialize (allows .inputrc to override)
1039 *
1040 * XXX: A bug in the readline-2.2 library causes a memory leak
1041 * inside this function. Nothing we can do about it.
1042 */
R. David Murray52d1b4e2010-12-18 03:48:32 +00001043#ifdef __APPLE__
1044 if (using_libedit_emulation)
Victor Stinner6ced7c42011-03-21 18:15:42 +01001045 rl_read_init_file(NULL);
R. David Murray52d1b4e2010-12-18 03:48:32 +00001046 else
1047#endif /* __APPLE__ */
1048 rl_initialize();
Victor Stinner6ced7c42011-03-21 18:15:42 +01001049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 RESTORE_LOCALE(saved_locale)
Guido van Rossum290900a1997-09-26 21:51:21 +00001051}
1052
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001053/* Wrapper around GNU readline that handles signals differently. */
1054
1055
1056#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
1057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058static char *completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001059static void
1060rlhandler(char *text)
1061{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 completed_input_string = text;
1063 rl_callback_handler_remove();
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001064}
1065
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001066static char *
Serhiy Storchakac6792272013-10-19 21:03:34 +03001067readline_until_enter_or_signal(const char *prompt, int *signal)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001068{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 char * not_done_reading = "";
1070 fd_set selectset;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001071
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 *signal = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001073#ifdef HAVE_RL_CATCH_SIGNAL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 rl_catch_signals = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001075#endif
1076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 rl_callback_handler_install (prompt, rlhandler);
1078 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 completed_input_string = not_done_reading;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 while (completed_input_string == not_done_reading) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001083 int has_input = 0, err = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 while (!has_input)
1086 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Michael W. Hudson8da2b012004-10-07 13:46:33 +00001087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 /* [Bug #1552726] Only limit the pause if an input hook has been
1089 defined. */
1090 struct timeval *timeoutp = NULL;
1091 if (PyOS_InputHook)
1092 timeoutp = &timeout;
1093 FD_SET(fileno(rl_instream), &selectset);
1094 /* select resets selectset if no input was available */
1095 has_input = select(fileno(rl_instream) + 1, &selectset,
1096 NULL, NULL, timeoutp);
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001097 err = errno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 if(PyOS_InputHook) PyOS_InputHook();
1099 }
1100
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001101 if (has_input > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 rl_callback_read_char();
1103 }
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001104 else if (err == EINTR) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001106#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001108#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001110#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001112#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 if (s < 0) {
1114 rl_free_line_state();
1115 rl_cleanup_after_signal();
1116 rl_callback_handler_remove();
1117 *signal = 1;
1118 completed_input_string = NULL;
1119 }
1120 }
1121 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 return completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001124}
1125
1126
1127#else
Guido van Rossum290900a1997-09-26 21:51:21 +00001128
1129/* Interrupt handler */
1130
1131static jmp_buf jbuf;
1132
Guido van Rossum0969d361997-08-05 21:27:50 +00001133/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +00001134static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001135onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +00001136{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +00001138}
1139
Guido van Rossum290900a1997-09-26 21:51:21 +00001140
Guido van Rossum0969d361997-08-05 21:27:50 +00001141static char *
Christian Heimesa3da7c52013-12-04 09:31:47 +01001142readline_until_enter_or_signal(const char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +00001143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 PyOS_sighandler_t old_inthandler;
1145 char *p;
Guido van Rossum74f31432003-01-07 20:01:29 +00001146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 *signal = 0;
1148
1149 old_inthandler = PyOS_setsig(SIGINT, onintr);
1150 if (setjmp(jbuf)) {
Guido van Rossum0969d361997-08-05 21:27:50 +00001151#ifdef HAVE_SIGRELSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
1153 sigrelse(SIGINT);
Guido van Rossum0969d361997-08-05 21:27:50 +00001154#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 PyOS_setsig(SIGINT, old_inthandler);
1156 *signal = 1;
1157 return NULL;
1158 }
1159 rl_event_hook = PyOS_InputHook;
1160 p = readline(prompt);
1161 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001162
1163 return p;
1164}
1165#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
1166
1167
1168static char *
Serhiy Storchakac6792272013-10-19 21:03:34 +03001169call_readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001170{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 size_t n;
1172 char *p, *q;
1173 int signal;
Neal Norwitz1fa040b2004-08-25 01:20:18 +00001174
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001175#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1177 if (!saved_locale)
1178 Py_FatalError("not enough memory to save locale");
Nadeem Vawda63752572013-02-02 20:05:11 +01001179 setlocale(LC_CTYPE, "");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001180#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
1183 rl_instream = sys_stdin;
1184 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001185#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001187#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 }
Guido van Rossum74f31432003-01-07 20:01:29 +00001189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 p = readline_until_enter_or_signal(prompt, &signal);
Guido van Rossumb18618d2000-05-03 23:44:39 +00001191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 /* we got an interrupt signal */
1193 if (signal) {
1194 RESTORE_LOCALE(saved_locale)
1195 return NULL;
1196 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 /* We got an EOF, return a empty string. */
1199 if (p == NULL) {
Victor Stinner2fe9bac2013-10-10 16:18:20 +02001200 p = PyMem_RawMalloc(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 if (p != NULL)
1202 *p = '\0';
1203 RESTORE_LOCALE(saved_locale)
1204 return p;
1205 }
1206
1207 /* we have a valid line */
1208 n = strlen(p);
1209 if (n > 0) {
Brett Cannon2525dc82010-08-22 20:36:25 +00001210 const char *line;
Mark Dickinson6b54e1f2010-08-03 16:49:49 +00001211 int length = _py_get_history_length();
1212 if (length > 0)
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001213#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 if (using_libedit_emulation) {
Ned Deilyf70f4a62013-09-06 15:16:19 -07001215 /* handle older 0-based or newer 1-based indexing */
1216 line = (const char *)history_get(length + libedit_history_start - 1)->line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 } else
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001218#endif /* __APPLE__ */
Brett Cannon2525dc82010-08-22 20:36:25 +00001219 line = (const char *)history_get(length)->line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 else
1221 line = "";
1222 if (strcmp(p, line))
1223 add_history(p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 }
1225 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
1226 release the original. */
1227 q = p;
Victor Stinner2fe9bac2013-10-10 16:18:20 +02001228 p = PyMem_RawMalloc(n+2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 if (p != NULL) {
1230 strncpy(p, q, n);
1231 p[n] = '\n';
1232 p[n+1] = '\0';
1233 }
1234 free(q);
1235 RESTORE_LOCALE(saved_locale)
1236 return p;
Guido van Rossum0969d361997-08-05 21:27:50 +00001237}
1238
Guido van Rossum290900a1997-09-26 21:51:21 +00001239
1240/* Initialize the module */
1241
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001242PyDoc_STRVAR(doc_module,
1243"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001244
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001245#ifdef __APPLE__
1246PyDoc_STRVAR(doc_module_le,
1247"Importing this module enables command line editing using libedit readline.");
1248#endif /* __APPLE__ */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001249
1250static struct PyModuleDef readlinemodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 PyModuleDef_HEAD_INIT,
1252 "readline",
1253 doc_module,
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001254 sizeof(readlinestate),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 readline_methods,
1256 NULL,
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001257 readline_traverse,
1258 readline_clear,
1259 readline_free
Martin v. Löwis1a214512008-06-11 05:26:20 +00001260};
1261
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001262
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001263PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001264PyInit_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001265{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 PyObject *m;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001267 readlinestate *mod_state;
Guido van Rossum290900a1997-09-26 21:51:21 +00001268
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001269#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
1271 using_libedit_emulation = 1;
1272 }
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 if (using_libedit_emulation)
1275 readlinemodule.m_doc = doc_module_le;
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001276
1277#endif /* __APPLE__ */
1278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 m = PyModule_Create(&readlinemodule);
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 if (m == NULL)
1282 return NULL;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001283
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001284 mod_state = (readlinestate *) PyModule_GetState(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 PyOS_ReadlineFunctionPointer = call_readline;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001286 setup_readline(mod_state);
Antoine Pitrou7e8b8672014-11-04 14:52:10 +01001287
1288 PyModule_AddIntConstant(m, "_READLINE_VERSION", RL_READLINE_VERSION);
1289 PyModule_AddIntConstant(m, "_READLINE_RUNTIME_VERSION", rl_readline_version);
1290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 return m;
Guido van Rossum0969d361997-08-05 21:27:50 +00001292}