blob: 8c00decd2570a69cc4aa0d8f74a4b2f46fcf7691 [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 {
Martin Panterf6e9f472016-03-22 02:19:29 +000081 /* Specify hook functions in Python */
Antoine Pitrou5c30a752013-07-31 21:52:53 +020082 PyObject *completion_display_matches_hook;
83 PyObject *startup_hook;
84 PyObject *pre_input_hook;
Martin Panterf6e9f472016-03-22 02:19:29 +000085
86 PyObject *completer; /* Specify a word completer in Python */
Antoine Pitrou5c30a752013-07-31 21:52:53 +020087 PyObject *begidx;
88 PyObject *endidx;
89} readlinestate;
90
91
92#define readline_state(o) ((readlinestate *)PyModule_GetState(o))
93
94static int
95readline_clear(PyObject *m)
96{
97 readlinestate *state = readline_state(m);
98 Py_CLEAR(state->completion_display_matches_hook);
99 Py_CLEAR(state->startup_hook);
100 Py_CLEAR(state->pre_input_hook);
101 Py_CLEAR(state->completer);
102 Py_CLEAR(state->begidx);
103 Py_CLEAR(state->endidx);
104 return 0;
105}
106
107static int
108readline_traverse(PyObject *m, visitproc visit, void *arg)
109{
110 readlinestate *state = readline_state(m);
111 Py_VISIT(state->completion_display_matches_hook);
112 Py_VISIT(state->startup_hook);
113 Py_VISIT(state->pre_input_hook);
114 Py_VISIT(state->completer);
115 Py_VISIT(state->begidx);
116 Py_VISIT(state->endidx);
117 return 0;
118}
119
120static void
121readline_free(void *m)
122{
123 readline_clear((PyObject *)m);
124}
125
126static PyModuleDef readlinemodule;
127
128#define readlinestate_global ((readlinestate *)PyModule_GetState(PyState_FindModule(&readlinemodule)))
129
130
Guido van Rossum290900a1997-09-26 21:51:21 +0000131/* Exported function to send one line to readline's init file parser */
132
133static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000134parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000135{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 char *s, *copy;
137 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
138 return NULL;
139 /* Make a copy -- rl_parse_and_bind() modifies its argument */
140 /* Bernard Herzog */
Victor Stinnerb6404912013-07-07 16:21:41 +0200141 copy = PyMem_Malloc(1 + strlen(s));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 if (copy == NULL)
143 return PyErr_NoMemory();
144 strcpy(copy, s);
145 rl_parse_and_bind(copy);
Victor Stinnerb6404912013-07-07 16:21:41 +0200146 PyMem_Free(copy); /* Free the copy */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +0000148}
149
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000150PyDoc_STRVAR(doc_parse_and_bind,
151"parse_and_bind(string) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000152Execute the init line provided in the string argument.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000153
154
155/* Exported function to parse a readline init file */
156
157static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000158read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000159{
Victor Stinner19e65a32010-06-11 22:27:14 +0000160 PyObject *filename_obj = Py_None, *filename_bytes;
161 if (!PyArg_ParseTuple(args, "|O:read_init_file", &filename_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 return NULL;
Victor Stinner19e65a32010-06-11 22:27:14 +0000163 if (filename_obj != Py_None) {
164 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
165 return NULL;
166 errno = rl_read_init_file(PyBytes_AsString(filename_bytes));
167 Py_DECREF(filename_bytes);
168 } else
169 errno = rl_read_init_file(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 if (errno)
171 return PyErr_SetFromErrno(PyExc_IOError);
172 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +0000173}
174
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000175PyDoc_STRVAR(doc_read_init_file,
176"read_init_file([filename]) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000177Execute a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000178The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000179
180
Skip Montanaro28067822000-07-06 18:55:12 +0000181/* Exported function to load a readline history file */
182
183static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000184read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000185{
Victor Stinner19e65a32010-06-11 22:27:14 +0000186 PyObject *filename_obj = Py_None, *filename_bytes;
187 if (!PyArg_ParseTuple(args, "|O:read_history_file", &filename_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 return NULL;
Victor Stinner19e65a32010-06-11 22:27:14 +0000189 if (filename_obj != Py_None) {
190 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
191 return NULL;
192 errno = read_history(PyBytes_AsString(filename_bytes));
193 Py_DECREF(filename_bytes);
194 } else
195 errno = read_history(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 if (errno)
197 return PyErr_SetFromErrno(PyExc_IOError);
198 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000199}
200
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000201static int _history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000202PyDoc_STRVAR(doc_read_history_file,
203"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000204Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000205The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000206
207
208/* Exported function to save a readline history file */
209
210static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000211write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000212{
Victor Stinner19e65a32010-06-11 22:27:14 +0000213 PyObject *filename_obj = Py_None, *filename_bytes;
214 char *filename;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100215 int err;
Victor Stinner19e65a32010-06-11 22:27:14 +0000216 if (!PyArg_ParseTuple(args, "|O:write_history_file", &filename_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 return NULL;
Victor Stinner19e65a32010-06-11 22:27:14 +0000218 if (filename_obj != Py_None) {
219 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
220 return NULL;
221 filename = PyBytes_AsString(filename_bytes);
222 } else {
223 filename_bytes = NULL;
224 filename = NULL;
225 }
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100226 errno = err = write_history(filename);
227 if (!err && _history_length >= 0)
Victor Stinner19e65a32010-06-11 22:27:14 +0000228 history_truncate_file(filename, _history_length);
229 Py_XDECREF(filename_bytes);
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100230 errno = err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 if (errno)
232 return PyErr_SetFromErrno(PyExc_IOError);
233 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000234}
235
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000236PyDoc_STRVAR(doc_write_history_file,
237"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000238Save a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000239The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000240
241
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600242#ifdef HAVE_RL_APPEND_HISTORY
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600243/* Exported function to save part of a readline history file */
244
245static PyObject *
246append_history_file(PyObject *self, PyObject *args)
247{
248 int nelements;
249 PyObject *filename_obj = Py_None, *filename_bytes;
250 char *filename;
251 int err;
252 if (!PyArg_ParseTuple(args, "i|O:append_history_file", &nelements, &filename_obj))
253 return NULL;
254 if (filename_obj != Py_None) {
255 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
256 return NULL;
257 filename = PyBytes_AsString(filename_bytes);
258 } else {
259 filename_bytes = NULL;
260 filename = NULL;
261 }
262 errno = err = append_history(nelements, filename);
263 if (!err && _history_length >= 0)
264 history_truncate_file(filename, _history_length);
265 Py_XDECREF(filename_bytes);
266 errno = err;
267 if (errno)
268 return PyErr_SetFromErrno(PyExc_IOError);
269 Py_RETURN_NONE;
270}
271
272PyDoc_STRVAR(doc_append_history_file,
273"append_history_file(nelements[, filename]) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000274Append the last nelements items of the history list to file.\n\
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600275The default filename is ~/.history.");
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600276#endif
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600277
278
Guido van Rossum74f31432003-01-07 20:01:29 +0000279/* Set history length */
280
281static PyObject*
282set_history_length(PyObject *self, PyObject *args)
283{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 int length = _history_length;
285 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
286 return NULL;
287 _history_length = length;
288 Py_RETURN_NONE;
Guido van Rossum74f31432003-01-07 20:01:29 +0000289}
290
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000291PyDoc_STRVAR(set_history_length_doc,
292"set_history_length(length) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000293set the maximal number of lines which will be written to\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000294the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000295history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000296
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000297
Guido van Rossum74f31432003-01-07 20:01:29 +0000298/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000299
300static PyObject*
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000301get_history_length(PyObject *self, PyObject *noarg)
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000302{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 return PyLong_FromLong(_history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000304}
305
Guido van Rossum74f31432003-01-07 20:01:29 +0000306PyDoc_STRVAR(get_history_length_doc,
307"get_history_length() -> int\n\
Martin Panter0f767392016-04-05 07:37:22 +0000308return the maximum number of lines that will be written to\n\
Guido van Rossum74f31432003-01-07 20:01:29 +0000309the history file.");
310
311
Martin v. Löwis0daad592001-09-30 21:09:59 +0000312/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000313
Martin v. Löwis0daad592001-09-30 21:09:59 +0000314static PyObject *
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000315set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000316{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 PyObject *function = Py_None;
318 char buf[80];
319 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
320 if (!PyArg_ParseTuple(args, buf, &function))
321 return NULL;
322 if (function == Py_None) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200323 Py_CLEAR(*hook_var);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 }
325 else if (PyCallable_Check(function)) {
326 PyObject *tmp = *hook_var;
327 Py_INCREF(function);
328 *hook_var = function;
329 Py_XDECREF(tmp);
330 }
331 else {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100332 PyErr_Format(PyExc_TypeError,
333 "set_%.50s(func): argument not callable",
334 funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 return NULL;
336 }
337 Py_RETURN_NONE;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000338}
339
Guido van Rossum74f31432003-01-07 20:01:29 +0000340
Martin v. Löwis0daad592001-09-30 21:09:59 +0000341static PyObject *
Thomas Wouters89d996e2007-09-08 17:39:28 +0000342set_completion_display_matches_hook(PyObject *self, PyObject *args)
343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 PyObject *result = set_hook("completion_display_matches_hook",
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200345 &readlinestate_global->completion_display_matches_hook, args);
Christian Heimes32fbe592007-11-12 15:01:33 +0000346#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 /* We cannot set this hook globally, since it replaces the
348 default completion display. */
349 rl_completion_display_matches_hook =
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200350 readlinestate_global->completion_display_matches_hook ?
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000351#if defined(_RL_FUNCTION_TYPEDEF)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000353#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 (VFunction *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000355#endif
Christian Heimes32fbe592007-11-12 15:01:33 +0000356#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 return result;
Christian Heimes32fbe592007-11-12 15:01:33 +0000358
Thomas Wouters89d996e2007-09-08 17:39:28 +0000359}
360
361PyDoc_STRVAR(doc_set_completion_display_matches_hook,
362"set_completion_display_matches_hook([function]) -> None\n\
363Set or remove the completion display function.\n\
364The function is called as\n\
365 function(substitution, [matches], longest_match_length)\n\
366once each time matches need to be displayed.");
367
368static PyObject *
Martin v. Löwis0daad592001-09-30 21:09:59 +0000369set_startup_hook(PyObject *self, PyObject *args)
370{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200371 return set_hook("startup_hook", &readlinestate_global->startup_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000372}
373
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000374PyDoc_STRVAR(doc_set_startup_hook,
375"set_startup_hook([function]) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000376Set or remove the function invoked by the rl_startup_hook callback.\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000377The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000378before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000379
Guido van Rossum74f31432003-01-07 20:01:29 +0000380
Martin v. Löwis0daad592001-09-30 21:09:59 +0000381#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000382
383/* Set pre-input hook */
384
Martin v. Löwis0daad592001-09-30 21:09:59 +0000385static PyObject *
386set_pre_input_hook(PyObject *self, PyObject *args)
387{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200388 return set_hook("pre_input_hook", &readlinestate_global->pre_input_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000389}
390
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000391PyDoc_STRVAR(doc_set_pre_input_hook,
392"set_pre_input_hook([function]) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000393Set or remove the function invoked by the rl_pre_input_hook callback.\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000394The function is called with no arguments after the first prompt\n\
395has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000396characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000397
Martin v. Löwis0daad592001-09-30 21:09:59 +0000398#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000399
Guido van Rossum74f31432003-01-07 20:01:29 +0000400
Thomas Wouters89d996e2007-09-08 17:39:28 +0000401/* Get the completion type for the scope of the tab-completion */
402static PyObject *
403get_completion_type(PyObject *self, PyObject *noarg)
404{
Christian Heimes217cfd12007-12-02 14:31:20 +0000405 return PyLong_FromLong(rl_completion_type);
Thomas Wouters89d996e2007-09-08 17:39:28 +0000406}
407
408PyDoc_STRVAR(doc_get_completion_type,
409"get_completion_type() -> int\n\
410Get the type of completion being attempted.");
411
412
Guido van Rossum74f31432003-01-07 20:01:29 +0000413/* Get the beginning index for the scope of the tab-completion */
414
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000415static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000416get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000417{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200418 Py_INCREF(readlinestate_global->begidx);
419 return readlinestate_global->begidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000420}
421
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000422PyDoc_STRVAR(doc_get_begidx,
423"get_begidx() -> int\n\
Martin Panter0f767392016-04-05 07:37:22 +0000424get the beginning index of the completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000425
Guido van Rossum74f31432003-01-07 20:01:29 +0000426
427/* Get the ending index for the scope of the tab-completion */
428
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000429static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000430get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000431{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200432 Py_INCREF(readlinestate_global->endidx);
433 return readlinestate_global->endidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000434}
435
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000436PyDoc_STRVAR(doc_get_endidx,
437"get_endidx() -> int\n\
Martin Panter0f767392016-04-05 07:37:22 +0000438get the ending index of the completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000439
440
Guido van Rossum74f31432003-01-07 20:01:29 +0000441/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000442
443static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000444set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000445{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 char *break_chars;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000447
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200448 if (!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 return NULL;
450 }
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200451 /* Keep a reference to the allocated memory in the module state in case
452 some other module modifies rl_completer_word_break_characters
453 (see issue #17289). */
Serhiy Storchaka11384392015-09-27 22:34:59 +0300454 break_chars = strdup(break_chars);
455 if (break_chars) {
456 free(completer_word_break_characters);
457 completer_word_break_characters = break_chars;
458 rl_completer_word_break_characters = break_chars;
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200459 Py_RETURN_NONE;
460 }
461 else
462 return PyErr_NoMemory();
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000463}
464
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000465PyDoc_STRVAR(doc_set_completer_delims,
466"set_completer_delims(string) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000467set the word delimiters for completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000468
Mark Dickinson29b238e2010-08-03 16:08:16 +0000469/* _py_free_history_entry: Utility function to free a history entry. */
470
471#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
472
473/* Readline version >= 5.0 introduced a timestamp field into the history entry
474 structure; this needs to be freed to avoid a memory leak. This version of
475 readline also introduced the handy 'free_history_entry' function, which
476 takes care of the timestamp. */
477
478static void
479_py_free_history_entry(HIST_ENTRY *entry)
480{
481 histdata_t data = free_history_entry(entry);
482 free(data);
483}
484
485#else
486
487/* No free_history_entry function; free everything manually. */
488
489static void
490_py_free_history_entry(HIST_ENTRY *entry)
491{
492 if (entry->line)
493 free((void *)entry->line);
494 if (entry->data)
495 free(entry->data);
496 free(entry);
497}
498
499#endif
500
Skip Montanaroe5069012004-08-15 14:32:06 +0000501static PyObject *
502py_remove_history(PyObject *self, PyObject *args)
503{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 int entry_number;
505 HIST_ENTRY *entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000506
Martin Panter0f767392016-04-05 07:37:22 +0000507 if (!PyArg_ParseTuple(args, "i:remove_history_item", &entry_number))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 return NULL;
509 if (entry_number < 0) {
510 PyErr_SetString(PyExc_ValueError,
511 "History index cannot be negative");
512 return NULL;
513 }
514 entry = remove_history(entry_number);
515 if (!entry) {
516 PyErr_Format(PyExc_ValueError,
517 "No history item at position %d",
518 entry_number);
519 return NULL;
520 }
521 /* free memory allocated for the history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000522 _py_free_history_entry(entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000524}
525
526PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000527"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000528remove history item given by its position");
529
530static PyObject *
531py_replace_history(PyObject *self, PyObject *args)
532{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 int entry_number;
534 char *line;
535 HIST_ENTRY *old_entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000536
Martin Panter0f767392016-04-05 07:37:22 +0000537 if (!PyArg_ParseTuple(args, "is:replace_history_item", &entry_number,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 &line)) {
539 return NULL;
540 }
541 if (entry_number < 0) {
542 PyErr_SetString(PyExc_ValueError,
543 "History index cannot be negative");
544 return NULL;
545 }
546 old_entry = replace_history_entry(entry_number, line, (void *)NULL);
547 if (!old_entry) {
548 PyErr_Format(PyExc_ValueError,
549 "No history item at position %d",
550 entry_number);
551 return NULL;
552 }
553 /* free memory allocated for the old history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000554 _py_free_history_entry(old_entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000556}
557
558PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000559"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000560replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000561
562/* Add a line to the history buffer */
563
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000564static PyObject *
565py_add_history(PyObject *self, PyObject *args)
566{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 char *line;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
570 return NULL;
571 }
572 add_history(line);
573 Py_RETURN_NONE;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000574}
575
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000576PyDoc_STRVAR(doc_add_history,
577"add_history(string) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000578add an item to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000579
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000580
Guido van Rossum74f31432003-01-07 20:01:29 +0000581/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000582
583static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000584get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000585{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 return PyUnicode_FromString(rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000587}
Guido van Rossum74f31432003-01-07 20:01:29 +0000588
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000589PyDoc_STRVAR(doc_get_completer_delims,
590"get_completer_delims() -> string\n\
Martin Panter0f767392016-04-05 07:37:22 +0000591get the word delimiters for completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000592
Guido van Rossum74f31432003-01-07 20:01:29 +0000593
594/* Set the completer function */
595
Guido van Rossum290900a1997-09-26 21:51:21 +0000596static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000597set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000598{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200599 return set_hook("completer", &readlinestate_global->completer, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000600}
601
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000602PyDoc_STRVAR(doc_set_completer,
603"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000604Set or remove the completer function.\n\
605The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000606for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000607It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000608
Guido van Rossum74f31432003-01-07 20:01:29 +0000609
Michael W. Hudson796df152003-01-30 10:12:51 +0000610static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000611get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000612{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200613 if (readlinestate_global->completer == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 Py_RETURN_NONE;
615 }
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200616 Py_INCREF(readlinestate_global->completer);
617 return readlinestate_global->completer;
Michael W. Hudson796df152003-01-30 10:12:51 +0000618}
619
620PyDoc_STRVAR(doc_get_completer,
621"get_completer() -> function\n\
622\n\
623Returns current completer function.");
624
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000625/* Private function to get current length of history. XXX It may be
626 * possible to replace this with a direct use of history_length instead,
627 * but it's not clear whether BSD's libedit keeps history_length up to date.
628 * See issue #8065.*/
629
630static int
631_py_get_history_length(void)
632{
633 HISTORY_STATE *hist_st = history_get_history_state();
634 int length = hist_st->length;
635 /* the history docs don't say so, but the address of hist_st changes each
636 time history_get_history_state is called which makes me think it's
637 freshly malloc'd memory... on the other hand, the address of the last
638 line stays the same as long as history isn't extended, so it appears to
639 be malloc'd but managed by the history package... */
640 free(hist_st);
641 return length;
642}
643
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000644/* Exported function to get any element of history */
645
646static PyObject *
647get_history_item(PyObject *self, PyObject *args)
648{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 int idx = 0;
650 HIST_ENTRY *hist_ent;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000651
Martin Panter0f767392016-04-05 07:37:22 +0000652 if (!PyArg_ParseTuple(args, "i:get_history_item", &idx))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 return NULL;
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000654#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 if (using_libedit_emulation) {
Ned Deilyf70f4a62013-09-06 15:16:19 -0700656 /* Older versions of libedit's readline emulation
657 * use 0-based indexes, while readline and newer
658 * versions of libedit use 1-based indexes.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 */
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000660 int length = _py_get_history_length();
Ned Deilyf70f4a62013-09-06 15:16:19 -0700661
662 idx = idx - 1 + libedit_history_start;
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 /*
665 * Apple's readline emulation crashes when
666 * the index is out of range, therefore
667 * test for that and fail gracefully.
668 */
Ned Deilyf70f4a62013-09-06 15:16:19 -0700669 if (idx < (0 + libedit_history_start)
670 || idx >= (length + libedit_history_start)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 Py_RETURN_NONE;
672 }
673 }
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000674#endif /* __APPLE__ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 if ((hist_ent = history_get(idx)))
676 return PyUnicode_FromString(hist_ent->line);
677 else {
678 Py_RETURN_NONE;
679 }
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000680}
681
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000682PyDoc_STRVAR(doc_get_history_item,
683"get_history_item() -> string\n\
684return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000685
Guido van Rossum74f31432003-01-07 20:01:29 +0000686
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000687/* Exported function to get current length of history */
688
689static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000690get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000691{
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000692 return PyLong_FromLong((long)_py_get_history_length());
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000693}
694
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000695PyDoc_STRVAR(doc_get_current_history_length,
696"get_current_history_length() -> integer\n\
697return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000698
Guido van Rossum74f31432003-01-07 20:01:29 +0000699
Guido van Rossum79378ff1997-10-07 14:53:21 +0000700/* Exported function to read the current line buffer */
701
702static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000703get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000704{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 return PyUnicode_FromString(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000706}
707
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000708PyDoc_STRVAR(doc_get_line_buffer,
709"get_line_buffer() -> string\n\
710return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000711
Guido van Rossum74f31432003-01-07 20:01:29 +0000712
Martin v. Löwise7a97962003-09-20 16:08:33 +0000713#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
714
715/* Exported function to clear the current history */
716
717static PyObject *
718py_clear_history(PyObject *self, PyObject *noarg)
719{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 clear_history();
721 Py_RETURN_NONE;
Martin v. Löwise7a97962003-09-20 16:08:33 +0000722}
723
724PyDoc_STRVAR(doc_clear_history,
725"clear_history() -> None\n\
726Clear the current readline history.");
727#endif
728
729
Guido van Rossum79378ff1997-10-07 14:53:21 +0000730/* Exported function to insert text into the line buffer */
731
732static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000733insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000734{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 char *s;
736 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
737 return NULL;
738 rl_insert_text(s);
739 Py_RETURN_NONE;
Guido van Rossum79378ff1997-10-07 14:53:21 +0000740}
741
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000742PyDoc_STRVAR(doc_insert_text,
743"insert_text(string) -> None\n\
Martin Panter0f767392016-04-05 07:37:22 +0000744Insert text into the line buffer at the cursor position.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000745
Guido van Rossum74f31432003-01-07 20:01:29 +0000746
747/* Redisplay the line buffer */
748
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000749static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000750redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000751{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 rl_redisplay();
753 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000754}
755
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000756PyDoc_STRVAR(doc_redisplay,
757"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000758Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000759contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000760
Guido van Rossum74f31432003-01-07 20:01:29 +0000761
Guido van Rossum290900a1997-09-26 21:51:21 +0000762/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000763
764static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000765{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
767 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
768 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
769 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
770 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
771 {"read_history_file", read_history_file,
772 METH_VARARGS, doc_read_history_file},
773 {"write_history_file", write_history_file,
774 METH_VARARGS, doc_write_history_file},
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600775#ifdef HAVE_RL_APPEND_HISTORY
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600776 {"append_history_file", append_history_file,
777 METH_VARARGS, doc_append_history_file},
Ned Deily8007cbc2014-11-26 13:02:33 -0800778#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 {"get_history_item", get_history_item,
780 METH_VARARGS, doc_get_history_item},
781 {"get_current_history_length", (PyCFunction)get_current_history_length,
782 METH_NOARGS, doc_get_current_history_length},
783 {"set_history_length", set_history_length,
784 METH_VARARGS, set_history_length_doc},
785 {"get_history_length", get_history_length,
786 METH_NOARGS, get_history_length_doc},
787 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
788 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
789 {"get_completion_type", get_completion_type,
790 METH_NOARGS, doc_get_completion_type},
791 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
792 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 {"set_completer_delims", set_completer_delims,
795 METH_VARARGS, doc_set_completer_delims},
796 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
797 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
798 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
799 {"get_completer_delims", get_completer_delims,
800 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 {"set_completion_display_matches_hook", set_completion_display_matches_hook,
803 METH_VARARGS, doc_set_completion_display_matches_hook},
804 {"set_startup_hook", set_startup_hook,
805 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000806#ifdef HAVE_RL_PRE_INPUT_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 {"set_pre_input_hook", set_pre_input_hook,
808 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000809#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000810#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
Martin v. Löwise7a97962003-09-20 16:08:33 +0000812#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000814};
815
Guido van Rossum05ac4492003-01-07 20:04:12 +0000816
Martin v. Löwis0daad592001-09-30 21:09:59 +0000817/* C function to call the Python hooks. */
818
819static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000820on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000821{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 int result = 0;
823 if (func != NULL) {
824 PyObject *r;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 r = PyObject_CallFunction(func, NULL);
826 if (r == NULL)
827 goto error;
828 if (r == Py_None)
829 result = 0;
830 else {
831 result = PyLong_AsLong(r);
832 if (result == -1 && PyErr_Occurred())
833 goto error;
834 }
835 Py_DECREF(r);
836 goto done;
837 error:
838 PyErr_Clear();
839 Py_XDECREF(r);
840 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 return result;
842 }
843 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000844}
845
846static int
Ned Deily7b9ddea2014-02-05 16:53:10 -0800847#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000848on_startup_hook(void)
Ned Deily7b9ddea2014-02-05 16:53:10 -0800849#else
850on_startup_hook()
851#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000852{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200853 int r;
854#ifdef WITH_THREAD
855 PyGILState_STATE gilstate = PyGILState_Ensure();
856#endif
857 r = on_hook(readlinestate_global->startup_hook);
858#ifdef WITH_THREAD
859 PyGILState_Release(gilstate);
860#endif
861 return r;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000862}
863
864#ifdef HAVE_RL_PRE_INPUT_HOOK
865static int
Ned Deily7b9ddea2014-02-05 16:53:10 -0800866#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000867on_pre_input_hook(void)
Ned Deily7b9ddea2014-02-05 16:53:10 -0800868#else
869on_pre_input_hook()
870#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000871{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200872 int r;
873#ifdef WITH_THREAD
874 PyGILState_STATE gilstate = PyGILState_Ensure();
875#endif
876 r = on_hook(readlinestate_global->pre_input_hook);
877#ifdef WITH_THREAD
878 PyGILState_Release(gilstate);
879#endif
880 return r;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000881}
882#endif
883
Guido van Rossum05ac4492003-01-07 20:04:12 +0000884
Thomas Wouters89d996e2007-09-08 17:39:28 +0000885/* C function to call the Python completion_display_matches */
886
Georg Brandl646fdd62010-10-18 07:27:55 +0000887#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Thomas Wouters89d996e2007-09-08 17:39:28 +0000888static void
889on_completion_display_matches_hook(char **matches,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 int num_matches, int max_length)
Thomas Wouters89d996e2007-09-08 17:39:28 +0000891{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 int i;
893 PyObject *m=NULL, *s=NULL, *r=NULL;
Christian Heimesaec75c32007-11-11 22:42:36 +0000894#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 PyGILState_STATE gilstate = PyGILState_Ensure();
Thomas Wouters89d996e2007-09-08 17:39:28 +0000896#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 m = PyList_New(num_matches);
898 if (m == NULL)
899 goto error;
900 for (i = 0; i < num_matches; i++) {
901 s = PyUnicode_FromString(matches[i+1]);
902 if (s == NULL)
903 goto error;
904 if (PyList_SetItem(m, i, s) == -1)
905 goto error;
906 }
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200907 r = PyObject_CallFunction(readlinestate_global->completion_display_matches_hook,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 "sOi", matches[0], m, max_length);
Christian Heimes32fbe592007-11-12 15:01:33 +0000909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 Py_DECREF(m); m=NULL;
Christian Heimes32fbe592007-11-12 15:01:33 +0000911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 if (r == NULL ||
913 (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) {
914 goto error;
915 }
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200916 Py_CLEAR(r);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917
918 if (0) {
919 error:
920 PyErr_Clear();
921 Py_XDECREF(m);
922 Py_XDECREF(r);
923 }
Christian Heimes32fbe592007-11-12 15:01:33 +0000924#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 PyGILState_Release(gilstate);
Christian Heimes32fbe592007-11-12 15:01:33 +0000926#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000927}
928
Senthil Kumaran95c07002010-11-04 03:51:05 +0000929#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000930
Martin Panter5dbbf1a2016-04-03 02:54:58 +0000931#ifdef HAVE_RL_RESIZE_TERMINAL
932static volatile sig_atomic_t sigwinch_received;
Martin Panter28f35b22016-04-03 08:00:49 +0000933static PyOS_sighandler_t sigwinch_ohandler;
Martin Panter5dbbf1a2016-04-03 02:54:58 +0000934
935static void
936readline_sigwinch_handler(int signum)
937{
938 sigwinch_received = 1;
939 if (sigwinch_ohandler &&
940 sigwinch_ohandler != SIG_IGN && sigwinch_ohandler != SIG_DFL)
941 sigwinch_ohandler(signum);
942
943#ifndef HAVE_SIGACTION
944 /* If the handler was installed with signal() rather than sigaction(),
945 we need to reinstall it. */
946 PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
947#endif
948}
949#endif
950
Guido van Rossum290900a1997-09-26 21:51:21 +0000951/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000952
Guido van Rossum290900a1997-09-26 21:51:21 +0000953static char *
Guido van Rossumd8faa362007-04-27 19:54:29 +0000954on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000955{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 char *result = NULL;
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200957 if (readlinestate_global->completer != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 PyObject *r;
Christian Heimesaec75c32007-11-11 22:42:36 +0000959#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000961#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 rl_attempted_completion_over = 1;
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200963 r = PyObject_CallFunction(readlinestate_global->completer, "si", text, state);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 if (r == NULL)
965 goto error;
966 if (r == Py_None) {
967 result = NULL;
968 }
969 else {
970 char *s = _PyUnicode_AsString(r);
971 if (s == NULL)
972 goto error;
973 result = strdup(s);
974 }
975 Py_DECREF(r);
976 goto done;
977 error:
978 PyErr_Clear();
979 Py_XDECREF(r);
980 done:
Christian Heimesaec75c32007-11-11 22:42:36 +0000981#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 PyGILState_Release(gilstate);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000983#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 return result;
985 }
986 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000987}
988
Guido van Rossum290900a1997-09-26 21:51:21 +0000989
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000990/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000991 * before calling the normal completer */
992
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000993static char **
Benjamin Petersonf0b463a2014-01-24 11:44:16 -0500994flex_complete(const char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000995{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200996 char **result;
997#ifdef WITH_THREAD
998 PyGILState_STATE gilstate = PyGILState_Ensure();
999#endif
Antoine Pitroudc0900b2009-10-19 18:22:37 +00001000#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 rl_completion_append_character ='\0';
Antoine Pitroud5131772009-10-26 19:22:14 +00001002#endif
1003#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 rl_completion_suppress_append = 0;
Antoine Pitroudc0900b2009-10-19 18:22:37 +00001005#endif
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001006 Py_XDECREF(readlinestate_global->begidx);
1007 Py_XDECREF(readlinestate_global->endidx);
1008 readlinestate_global->begidx = PyLong_FromLong((long) start);
1009 readlinestate_global->endidx = PyLong_FromLong((long) end);
1010 result = completion_matches(text, *on_completion);
1011#ifdef WITH_THREAD
1012 PyGILState_Release(gilstate);
1013#endif
1014 return result;
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001015}
1016
Guido van Rossum05ac4492003-01-07 20:04:12 +00001017
Guido van Rossum290900a1997-09-26 21:51:21 +00001018/* Helper to initialize GNU readline properly. */
1019
1020static void
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001021setup_readline(readlinestate *mod_state)
Guido van Rossum290900a1997-09-26 21:51:21 +00001022{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +00001023#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1025 if (!saved_locale)
1026 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +00001027#endif
1028
R. David Murray52d1b4e2010-12-18 03:48:32 +00001029#ifdef __APPLE__
Victor Stinner6ced7c42011-03-21 18:15:42 +01001030 /* the libedit readline emulation resets key bindings etc
R. David Murray52d1b4e2010-12-18 03:48:32 +00001031 * when calling rl_initialize. So call it upfront
1032 */
1033 if (using_libedit_emulation)
1034 rl_initialize();
Ned Deilyf70f4a62013-09-06 15:16:19 -07001035
1036 /* Detect if libedit's readline emulation uses 0-based
1037 * indexing or 1-based indexing.
1038 */
1039 add_history("1");
1040 if (history_get(1) == NULL) {
1041 libedit_history_start = 0;
1042 } else {
1043 libedit_history_start = 1;
1044 }
1045 clear_history();
R. David Murray52d1b4e2010-12-18 03:48:32 +00001046#endif /* __APPLE__ */
1047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 using_history();
Skip Montanaroa0392742002-06-11 14:32:46 +00001049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 rl_readline_name = "python";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 /* Force rebind of TAB to insert-tab */
1052 rl_bind_key('\t', rl_insert);
1053 /* Bind both ESC-TAB and ESC-ESC to the completion function */
1054 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
1055 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin Panter5dbbf1a2016-04-03 02:54:58 +00001056#ifdef HAVE_RL_RESIZE_TERMINAL
1057 /* Set up signal handler for window resize */
1058 sigwinch_ohandler = PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
1059#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 /* Set our hook functions */
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001061 rl_startup_hook = on_startup_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001062#ifdef HAVE_RL_PRE_INPUT_HOOK
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001063 rl_pre_input_hook = on_pre_input_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001064#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 /* Set our completion function */
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001066 rl_attempted_completion_function = flex_complete;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 /* Set Python word break characters */
Antoine Pitroua7f7deb2013-05-06 21:51:03 +02001068 completer_word_break_characters =
1069 rl_completer_word_break_characters =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
1071 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001072
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001073 mod_state->begidx = PyLong_FromLong(0L);
1074 mod_state->endidx = PyLong_FromLong(0L);
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001075
Victor Stinner92639cc2014-07-24 22:11:38 +02001076#ifndef __APPLE__
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001077 if (!isatty(STDOUT_FILENO)) {
Martin Pantere26da7c2016-06-02 10:07:09 +00001078 /* Issue #19884: stdout is not a terminal. Disable meta modifier
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001079 keys to not write the ANSI sequence "\033[1034h" into stdout. On
1080 terminals supporting 8 bit characters like TERM=xterm-256color
1081 (which is now the default Fedora since Fedora 18), the meta key is
1082 used to enable support of 8 bit characters (ANSI sequence
Victor Stinner92639cc2014-07-24 22:11:38 +02001083 "\033[1034h").
1084
1085 With libedit, this call makes readline() crash. */
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001086 rl_variable_bind ("enable-meta-key", "off");
1087 }
Victor Stinner92639cc2014-07-24 22:11:38 +02001088#endif
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 /* Initialize (allows .inputrc to override)
1091 *
1092 * XXX: A bug in the readline-2.2 library causes a memory leak
1093 * inside this function. Nothing we can do about it.
1094 */
R. David Murray52d1b4e2010-12-18 03:48:32 +00001095#ifdef __APPLE__
1096 if (using_libedit_emulation)
Victor Stinner6ced7c42011-03-21 18:15:42 +01001097 rl_read_init_file(NULL);
R. David Murray52d1b4e2010-12-18 03:48:32 +00001098 else
1099#endif /* __APPLE__ */
1100 rl_initialize();
Victor Stinner6ced7c42011-03-21 18:15:42 +01001101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 RESTORE_LOCALE(saved_locale)
Guido van Rossum290900a1997-09-26 21:51:21 +00001103}
1104
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001105/* Wrapper around GNU readline that handles signals differently. */
1106
1107
1108#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
1109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001110static char *completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001111static void
1112rlhandler(char *text)
1113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 completed_input_string = text;
1115 rl_callback_handler_remove();
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001116}
1117
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001118static char *
Serhiy Storchakac6792272013-10-19 21:03:34 +03001119readline_until_enter_or_signal(const char *prompt, int *signal)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001120{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 char * not_done_reading = "";
1122 fd_set selectset;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001123
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 *signal = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001125#ifdef HAVE_RL_CATCH_SIGNAL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 rl_catch_signals = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001127#endif
1128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 rl_callback_handler_install (prompt, rlhandler);
1130 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 completed_input_string = not_done_reading;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 while (completed_input_string == not_done_reading) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001135 int has_input = 0, err = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 while (!has_input)
1138 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Michael W. Hudson8da2b012004-10-07 13:46:33 +00001139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 /* [Bug #1552726] Only limit the pause if an input hook has been
1141 defined. */
1142 struct timeval *timeoutp = NULL;
1143 if (PyOS_InputHook)
1144 timeoutp = &timeout;
Martin Panter5dbbf1a2016-04-03 02:54:58 +00001145#ifdef HAVE_RL_RESIZE_TERMINAL
1146 /* Update readline's view of the window size after SIGWINCH */
1147 if (sigwinch_received) {
1148 sigwinch_received = 0;
1149 rl_resize_terminal();
1150 }
1151#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 FD_SET(fileno(rl_instream), &selectset);
1153 /* select resets selectset if no input was available */
1154 has_input = select(fileno(rl_instream) + 1, &selectset,
1155 NULL, NULL, timeoutp);
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001156 err = errno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 if(PyOS_InputHook) PyOS_InputHook();
1158 }
1159
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001160 if (has_input > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 rl_callback_read_char();
1162 }
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001163 else if (err == EINTR) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001165#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001167#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001169#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001171#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 if (s < 0) {
1173 rl_free_line_state();
Martin Panterd6990d22016-03-22 07:24:05 +00001174#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0700
1175 rl_callback_sigcleanup();
1176#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 rl_cleanup_after_signal();
1178 rl_callback_handler_remove();
1179 *signal = 1;
1180 completed_input_string = NULL;
1181 }
1182 }
1183 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 return completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001186}
1187
1188
1189#else
Guido van Rossum290900a1997-09-26 21:51:21 +00001190
1191/* Interrupt handler */
1192
1193static jmp_buf jbuf;
1194
Guido van Rossum0969d361997-08-05 21:27:50 +00001195/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +00001196static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001197onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +00001198{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +00001200}
1201
Guido van Rossum290900a1997-09-26 21:51:21 +00001202
Guido van Rossum0969d361997-08-05 21:27:50 +00001203static char *
Christian Heimesa3da7c52013-12-04 09:31:47 +01001204readline_until_enter_or_signal(const char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +00001205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 PyOS_sighandler_t old_inthandler;
1207 char *p;
Guido van Rossum74f31432003-01-07 20:01:29 +00001208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 *signal = 0;
1210
1211 old_inthandler = PyOS_setsig(SIGINT, onintr);
1212 if (setjmp(jbuf)) {
Guido van Rossum0969d361997-08-05 21:27:50 +00001213#ifdef HAVE_SIGRELSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
1215 sigrelse(SIGINT);
Guido van Rossum0969d361997-08-05 21:27:50 +00001216#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 PyOS_setsig(SIGINT, old_inthandler);
1218 *signal = 1;
1219 return NULL;
1220 }
1221 rl_event_hook = PyOS_InputHook;
1222 p = readline(prompt);
1223 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001224
1225 return p;
1226}
1227#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
1228
1229
1230static char *
Serhiy Storchakac6792272013-10-19 21:03:34 +03001231call_readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001232{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 size_t n;
1234 char *p, *q;
1235 int signal;
Neal Norwitz1fa040b2004-08-25 01:20:18 +00001236
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001237#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1239 if (!saved_locale)
1240 Py_FatalError("not enough memory to save locale");
Nadeem Vawda63752572013-02-02 20:05:11 +01001241 setlocale(LC_CTYPE, "");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001242#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
1245 rl_instream = sys_stdin;
1246 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001247#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001249#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 }
Guido van Rossum74f31432003-01-07 20:01:29 +00001251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 p = readline_until_enter_or_signal(prompt, &signal);
Guido van Rossumb18618d2000-05-03 23:44:39 +00001253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 /* we got an interrupt signal */
1255 if (signal) {
1256 RESTORE_LOCALE(saved_locale)
1257 return NULL;
1258 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001259
Martin Panter7462b6492015-11-02 03:37:02 +00001260 /* We got an EOF, return an empty string. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 if (p == NULL) {
Victor Stinner2fe9bac2013-10-10 16:18:20 +02001262 p = PyMem_RawMalloc(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 if (p != NULL)
1264 *p = '\0';
1265 RESTORE_LOCALE(saved_locale)
1266 return p;
1267 }
1268
1269 /* we have a valid line */
1270 n = strlen(p);
1271 if (n > 0) {
Brett Cannon2525dc82010-08-22 20:36:25 +00001272 const char *line;
Mark Dickinson6b54e1f2010-08-03 16:49:49 +00001273 int length = _py_get_history_length();
1274 if (length > 0)
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001275#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 if (using_libedit_emulation) {
Ned Deilyf70f4a62013-09-06 15:16:19 -07001277 /* handle older 0-based or newer 1-based indexing */
1278 line = (const char *)history_get(length + libedit_history_start - 1)->line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 } else
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001280#endif /* __APPLE__ */
Brett Cannon2525dc82010-08-22 20:36:25 +00001281 line = (const char *)history_get(length)->line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 else
1283 line = "";
1284 if (strcmp(p, line))
1285 add_history(p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 }
1287 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
1288 release the original. */
1289 q = p;
Victor Stinner2fe9bac2013-10-10 16:18:20 +02001290 p = PyMem_RawMalloc(n+2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 if (p != NULL) {
1292 strncpy(p, q, n);
1293 p[n] = '\n';
1294 p[n+1] = '\0';
1295 }
1296 free(q);
1297 RESTORE_LOCALE(saved_locale)
1298 return p;
Guido van Rossum0969d361997-08-05 21:27:50 +00001299}
1300
Guido van Rossum290900a1997-09-26 21:51:21 +00001301
1302/* Initialize the module */
1303
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001304PyDoc_STRVAR(doc_module,
1305"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001306
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001307#ifdef __APPLE__
1308PyDoc_STRVAR(doc_module_le,
1309"Importing this module enables command line editing using libedit readline.");
1310#endif /* __APPLE__ */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001311
1312static struct PyModuleDef readlinemodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 PyModuleDef_HEAD_INIT,
1314 "readline",
1315 doc_module,
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001316 sizeof(readlinestate),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 readline_methods,
1318 NULL,
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001319 readline_traverse,
1320 readline_clear,
1321 readline_free
Martin v. Löwis1a214512008-06-11 05:26:20 +00001322};
1323
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001324
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001325PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001326PyInit_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 PyObject *m;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001329 readlinestate *mod_state;
Guido van Rossum290900a1997-09-26 21:51:21 +00001330
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001331#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
1333 using_libedit_emulation = 1;
1334 }
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 if (using_libedit_emulation)
1337 readlinemodule.m_doc = doc_module_le;
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001338
1339#endif /* __APPLE__ */
1340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 m = PyModule_Create(&readlinemodule);
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 if (m == NULL)
1344 return NULL;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001345
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001346 mod_state = (readlinestate *) PyModule_GetState(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 PyOS_ReadlineFunctionPointer = call_readline;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001348 setup_readline(mod_state);
Antoine Pitrou7e8b8672014-11-04 14:52:10 +01001349
1350 PyModule_AddIntConstant(m, "_READLINE_VERSION", RL_READLINE_VERSION);
1351 PyModule_AddIntConstant(m, "_READLINE_RUNTIME_VERSION", rl_readline_version);
1352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 return m;
Guido van Rossum0969d361997-08-05 21:27:50 +00001354}