blob: 939ff1ad551fa315152517319344780d7eab7327 [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
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600240#ifdef HAVE_RL_APPEND_HISTORY
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600241/* Exported function to save part of a readline history file */
242
243static PyObject *
244append_history_file(PyObject *self, PyObject *args)
245{
246 int nelements;
247 PyObject *filename_obj = Py_None, *filename_bytes;
248 char *filename;
249 int err;
250 if (!PyArg_ParseTuple(args, "i|O:append_history_file", &nelements, &filename_obj))
251 return NULL;
252 if (filename_obj != Py_None) {
253 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
254 return NULL;
255 filename = PyBytes_AsString(filename_bytes);
256 } else {
257 filename_bytes = NULL;
258 filename = NULL;
259 }
260 errno = err = append_history(nelements, filename);
261 if (!err && _history_length >= 0)
262 history_truncate_file(filename, _history_length);
263 Py_XDECREF(filename_bytes);
264 errno = err;
265 if (errno)
266 return PyErr_SetFromErrno(PyExc_IOError);
267 Py_RETURN_NONE;
268}
269
270PyDoc_STRVAR(doc_append_history_file,
271"append_history_file(nelements[, filename]) -> None\n\
272Append the last nelements of the history list to file.\n\
273The default filename is ~/.history.");
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600274#endif
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600275
276
Guido van Rossum74f31432003-01-07 20:01:29 +0000277/* Set history length */
278
279static PyObject*
280set_history_length(PyObject *self, PyObject *args)
281{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 int length = _history_length;
283 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
284 return NULL;
285 _history_length = length;
286 Py_RETURN_NONE;
Guido van Rossum74f31432003-01-07 20:01:29 +0000287}
288
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000289PyDoc_STRVAR(set_history_length_doc,
290"set_history_length(length) -> None\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000291set the maximal number of items which will be written to\n\
292the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000293history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000294
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000295
Guido van Rossum74f31432003-01-07 20:01:29 +0000296/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000297
298static PyObject*
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000299get_history_length(PyObject *self, PyObject *noarg)
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000300{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 return PyLong_FromLong(_history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000302}
303
Guido van Rossum74f31432003-01-07 20:01:29 +0000304PyDoc_STRVAR(get_history_length_doc,
305"get_history_length() -> int\n\
306return the maximum number of items that will be written to\n\
307the history file.");
308
309
Martin v. Löwis0daad592001-09-30 21:09:59 +0000310/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000311
Martin v. Löwis0daad592001-09-30 21:09:59 +0000312static PyObject *
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000313set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000314{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 PyObject *function = Py_None;
316 char buf[80];
317 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
318 if (!PyArg_ParseTuple(args, buf, &function))
319 return NULL;
320 if (function == Py_None) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200321 Py_CLEAR(*hook_var);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 }
323 else if (PyCallable_Check(function)) {
324 PyObject *tmp = *hook_var;
325 Py_INCREF(function);
326 *hook_var = function;
327 Py_XDECREF(tmp);
328 }
329 else {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100330 PyErr_Format(PyExc_TypeError,
331 "set_%.50s(func): argument not callable",
332 funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 return NULL;
334 }
335 Py_RETURN_NONE;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000336}
337
Guido van Rossum74f31432003-01-07 20:01:29 +0000338
Martin v. Löwis0daad592001-09-30 21:09:59 +0000339/* Exported functions to specify hook functions in Python */
340
Martin v. Löwis0daad592001-09-30 21:09:59 +0000341
342#ifdef HAVE_RL_PRE_INPUT_HOOK
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200343
Martin v. Löwis0daad592001-09-30 21:09:59 +0000344#endif
345
346static PyObject *
Thomas Wouters89d996e2007-09-08 17:39:28 +0000347set_completion_display_matches_hook(PyObject *self, PyObject *args)
348{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 PyObject *result = set_hook("completion_display_matches_hook",
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200350 &readlinestate_global->completion_display_matches_hook, args);
Christian Heimes32fbe592007-11-12 15:01:33 +0000351#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 /* We cannot set this hook globally, since it replaces the
353 default completion display. */
354 rl_completion_display_matches_hook =
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200355 readlinestate_global->completion_display_matches_hook ?
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000356#if defined(_RL_FUNCTION_TYPEDEF)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000358#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 (VFunction *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000360#endif
Christian Heimes32fbe592007-11-12 15:01:33 +0000361#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 return result;
Christian Heimes32fbe592007-11-12 15:01:33 +0000363
Thomas Wouters89d996e2007-09-08 17:39:28 +0000364}
365
366PyDoc_STRVAR(doc_set_completion_display_matches_hook,
367"set_completion_display_matches_hook([function]) -> None\n\
368Set or remove the completion display function.\n\
369The function is called as\n\
370 function(substitution, [matches], longest_match_length)\n\
371once each time matches need to be displayed.");
372
373static PyObject *
Martin v. Löwis0daad592001-09-30 21:09:59 +0000374set_startup_hook(PyObject *self, PyObject *args)
375{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200376 return set_hook("startup_hook", &readlinestate_global->startup_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000377}
378
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000379PyDoc_STRVAR(doc_set_startup_hook,
380"set_startup_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000381Set or remove the startup_hook function.\n\
382The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000383before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000384
Guido van Rossum74f31432003-01-07 20:01:29 +0000385
Martin v. Löwis0daad592001-09-30 21:09:59 +0000386#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000387
388/* Set pre-input hook */
389
Martin v. Löwis0daad592001-09-30 21:09:59 +0000390static PyObject *
391set_pre_input_hook(PyObject *self, PyObject *args)
392{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200393 return set_hook("pre_input_hook", &readlinestate_global->pre_input_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000394}
395
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000396PyDoc_STRVAR(doc_set_pre_input_hook,
397"set_pre_input_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000398Set or remove the pre_input_hook function.\n\
399The function is called with no arguments after the first prompt\n\
400has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000401characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000402
Martin v. Löwis0daad592001-09-30 21:09:59 +0000403#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000404
Guido van Rossum74f31432003-01-07 20:01:29 +0000405
Guido van Rossum290900a1997-09-26 21:51:21 +0000406/* Exported function to specify a word completer in Python */
407
Guido van Rossum290900a1997-09-26 21:51:21 +0000408
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200409
410
411
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000412
Guido van Rossum74f31432003-01-07 20:01:29 +0000413
Thomas Wouters89d996e2007-09-08 17:39:28 +0000414/* Get the completion type for the scope of the tab-completion */
415static PyObject *
416get_completion_type(PyObject *self, PyObject *noarg)
417{
Christian Heimes217cfd12007-12-02 14:31:20 +0000418 return PyLong_FromLong(rl_completion_type);
Thomas Wouters89d996e2007-09-08 17:39:28 +0000419}
420
421PyDoc_STRVAR(doc_get_completion_type,
422"get_completion_type() -> int\n\
423Get the type of completion being attempted.");
424
425
Guido van Rossum74f31432003-01-07 20:01:29 +0000426/* Get the beginning index for the scope of the tab-completion */
427
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000428static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000429get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000430{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200431 Py_INCREF(readlinestate_global->begidx);
432 return readlinestate_global->begidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000433}
434
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000435PyDoc_STRVAR(doc_get_begidx,
436"get_begidx() -> int\n\
437get the beginning index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000438
Guido van Rossum74f31432003-01-07 20:01:29 +0000439
440/* Get the ending index for the scope of the tab-completion */
441
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000442static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000443get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000444{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200445 Py_INCREF(readlinestate_global->endidx);
446 return readlinestate_global->endidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000447}
448
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000449PyDoc_STRVAR(doc_get_endidx,
450"get_endidx() -> int\n\
451get the ending index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000452
453
Guido van Rossum74f31432003-01-07 20:01:29 +0000454/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000455
456static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000457set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000458{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 char *break_chars;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000460
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200461 if (!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 return NULL;
463 }
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200464 /* Keep a reference to the allocated memory in the module state in case
465 some other module modifies rl_completer_word_break_characters
466 (see issue #17289). */
Serhiy Storchaka11384392015-09-27 22:34:59 +0300467 break_chars = strdup(break_chars);
468 if (break_chars) {
469 free(completer_word_break_characters);
470 completer_word_break_characters = break_chars;
471 rl_completer_word_break_characters = break_chars;
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200472 Py_RETURN_NONE;
473 }
474 else
475 return PyErr_NoMemory();
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000476}
477
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000478PyDoc_STRVAR(doc_set_completer_delims,
479"set_completer_delims(string) -> None\n\
480set the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000481
Mark Dickinson29b238e2010-08-03 16:08:16 +0000482/* _py_free_history_entry: Utility function to free a history entry. */
483
484#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
485
486/* Readline version >= 5.0 introduced a timestamp field into the history entry
487 structure; this needs to be freed to avoid a memory leak. This version of
488 readline also introduced the handy 'free_history_entry' function, which
489 takes care of the timestamp. */
490
491static void
492_py_free_history_entry(HIST_ENTRY *entry)
493{
494 histdata_t data = free_history_entry(entry);
495 free(data);
496}
497
498#else
499
500/* No free_history_entry function; free everything manually. */
501
502static void
503_py_free_history_entry(HIST_ENTRY *entry)
504{
505 if (entry->line)
506 free((void *)entry->line);
507 if (entry->data)
508 free(entry->data);
509 free(entry);
510}
511
512#endif
513
Skip Montanaroe5069012004-08-15 14:32:06 +0000514static PyObject *
515py_remove_history(PyObject *self, PyObject *args)
516{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 int entry_number;
518 HIST_ENTRY *entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
521 return NULL;
522 if (entry_number < 0) {
523 PyErr_SetString(PyExc_ValueError,
524 "History index cannot be negative");
525 return NULL;
526 }
527 entry = remove_history(entry_number);
528 if (!entry) {
529 PyErr_Format(PyExc_ValueError,
530 "No history item at position %d",
531 entry_number);
532 return NULL;
533 }
534 /* free memory allocated for the history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000535 _py_free_history_entry(entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000537}
538
539PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000540"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000541remove history item given by its position");
542
543static PyObject *
544py_replace_history(PyObject *self, PyObject *args)
545{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 int entry_number;
547 char *line;
548 HIST_ENTRY *old_entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number,
551 &line)) {
552 return NULL;
553 }
554 if (entry_number < 0) {
555 PyErr_SetString(PyExc_ValueError,
556 "History index cannot be negative");
557 return NULL;
558 }
559 old_entry = replace_history_entry(entry_number, line, (void *)NULL);
560 if (!old_entry) {
561 PyErr_Format(PyExc_ValueError,
562 "No history item at position %d",
563 entry_number);
564 return NULL;
565 }
566 /* free memory allocated for the old history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000567 _py_free_history_entry(old_entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000569}
570
571PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000572"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000573replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000574
575/* Add a line to the history buffer */
576
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000577static PyObject *
578py_add_history(PyObject *self, PyObject *args)
579{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 char *line;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
583 return NULL;
584 }
585 add_history(line);
586 Py_RETURN_NONE;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000587}
588
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000589PyDoc_STRVAR(doc_add_history,
590"add_history(string) -> None\n\
591add a line to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000592
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000593
Guido van Rossum74f31432003-01-07 20:01:29 +0000594/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000595
596static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000597get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000598{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 return PyUnicode_FromString(rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000600}
Guido van Rossum74f31432003-01-07 20:01:29 +0000601
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000602PyDoc_STRVAR(doc_get_completer_delims,
603"get_completer_delims() -> string\n\
604get the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000605
Guido van Rossum74f31432003-01-07 20:01:29 +0000606
607/* Set the completer function */
608
Guido van Rossum290900a1997-09-26 21:51:21 +0000609static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000610set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000611{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200612 return set_hook("completer", &readlinestate_global->completer, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000613}
614
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000615PyDoc_STRVAR(doc_set_completer,
616"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000617Set or remove the completer function.\n\
618The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000619for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000620It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000621
Guido van Rossum74f31432003-01-07 20:01:29 +0000622
Michael W. Hudson796df152003-01-30 10:12:51 +0000623static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000624get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000625{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200626 if (readlinestate_global->completer == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 Py_RETURN_NONE;
628 }
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200629 Py_INCREF(readlinestate_global->completer);
630 return readlinestate_global->completer;
Michael W. Hudson796df152003-01-30 10:12:51 +0000631}
632
633PyDoc_STRVAR(doc_get_completer,
634"get_completer() -> function\n\
635\n\
636Returns current completer function.");
637
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000638/* Private function to get current length of history. XXX It may be
639 * possible to replace this with a direct use of history_length instead,
640 * but it's not clear whether BSD's libedit keeps history_length up to date.
641 * See issue #8065.*/
642
643static int
644_py_get_history_length(void)
645{
646 HISTORY_STATE *hist_st = history_get_history_state();
647 int length = hist_st->length;
648 /* the history docs don't say so, but the address of hist_st changes each
649 time history_get_history_state is called which makes me think it's
650 freshly malloc'd memory... on the other hand, the address of the last
651 line stays the same as long as history isn't extended, so it appears to
652 be malloc'd but managed by the history package... */
653 free(hist_st);
654 return length;
655}
656
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000657/* Exported function to get any element of history */
658
659static PyObject *
660get_history_item(PyObject *self, PyObject *args)
661{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 int idx = 0;
663 HIST_ENTRY *hist_ent;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 if (!PyArg_ParseTuple(args, "i:index", &idx))
666 return NULL;
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000667#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 if (using_libedit_emulation) {
Ned Deilyf70f4a62013-09-06 15:16:19 -0700669 /* Older versions of libedit's readline emulation
670 * use 0-based indexes, while readline and newer
671 * versions of libedit use 1-based indexes.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 */
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000673 int length = _py_get_history_length();
Ned Deilyf70f4a62013-09-06 15:16:19 -0700674
675 idx = idx - 1 + libedit_history_start;
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 /*
678 * Apple's readline emulation crashes when
679 * the index is out of range, therefore
680 * test for that and fail gracefully.
681 */
Ned Deilyf70f4a62013-09-06 15:16:19 -0700682 if (idx < (0 + libedit_history_start)
683 || idx >= (length + libedit_history_start)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 Py_RETURN_NONE;
685 }
686 }
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000687#endif /* __APPLE__ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 if ((hist_ent = history_get(idx)))
689 return PyUnicode_FromString(hist_ent->line);
690 else {
691 Py_RETURN_NONE;
692 }
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000693}
694
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000695PyDoc_STRVAR(doc_get_history_item,
696"get_history_item() -> string\n\
697return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000698
Guido van Rossum74f31432003-01-07 20:01:29 +0000699
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000700/* Exported function to get current length of history */
701
702static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000703get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000704{
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000705 return PyLong_FromLong((long)_py_get_history_length());
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000706}
707
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000708PyDoc_STRVAR(doc_get_current_history_length,
709"get_current_history_length() -> integer\n\
710return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000711
Guido van Rossum74f31432003-01-07 20:01:29 +0000712
Guido van Rossum79378ff1997-10-07 14:53:21 +0000713/* Exported function to read the current line buffer */
714
715static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000716get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 return PyUnicode_FromString(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000719}
720
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000721PyDoc_STRVAR(doc_get_line_buffer,
722"get_line_buffer() -> string\n\
723return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000724
Guido van Rossum74f31432003-01-07 20:01:29 +0000725
Martin v. Löwise7a97962003-09-20 16:08:33 +0000726#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
727
728/* Exported function to clear the current history */
729
730static PyObject *
731py_clear_history(PyObject *self, PyObject *noarg)
732{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 clear_history();
734 Py_RETURN_NONE;
Martin v. Löwise7a97962003-09-20 16:08:33 +0000735}
736
737PyDoc_STRVAR(doc_clear_history,
738"clear_history() -> None\n\
739Clear the current readline history.");
740#endif
741
742
Guido van Rossum79378ff1997-10-07 14:53:21 +0000743/* Exported function to insert text into the line buffer */
744
745static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000746insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000747{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 char *s;
749 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
750 return NULL;
751 rl_insert_text(s);
752 Py_RETURN_NONE;
Guido van Rossum79378ff1997-10-07 14:53:21 +0000753}
754
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000755PyDoc_STRVAR(doc_insert_text,
756"insert_text(string) -> None\n\
757Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000758
Guido van Rossum74f31432003-01-07 20:01:29 +0000759
760/* Redisplay the line buffer */
761
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000762static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000763redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000764{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 rl_redisplay();
766 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000767}
768
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000769PyDoc_STRVAR(doc_redisplay,
770"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000771Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000772contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000773
Guido van Rossum74f31432003-01-07 20:01:29 +0000774
Guido van Rossum290900a1997-09-26 21:51:21 +0000775/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000776
777static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000778{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
780 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
781 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
782 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
783 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
784 {"read_history_file", read_history_file,
785 METH_VARARGS, doc_read_history_file},
786 {"write_history_file", write_history_file,
787 METH_VARARGS, doc_write_history_file},
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600788#ifdef HAVE_RL_APPEND_HISTORY
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600789 {"append_history_file", append_history_file,
790 METH_VARARGS, doc_append_history_file},
Ned Deily8007cbc2014-11-26 13:02:33 -0800791#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 {"get_history_item", get_history_item,
793 METH_VARARGS, doc_get_history_item},
794 {"get_current_history_length", (PyCFunction)get_current_history_length,
795 METH_NOARGS, doc_get_current_history_length},
796 {"set_history_length", set_history_length,
797 METH_VARARGS, set_history_length_doc},
798 {"get_history_length", get_history_length,
799 METH_NOARGS, get_history_length_doc},
800 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
801 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
802 {"get_completion_type", get_completion_type,
803 METH_NOARGS, doc_get_completion_type},
804 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
805 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 {"set_completer_delims", set_completer_delims,
808 METH_VARARGS, doc_set_completer_delims},
809 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
810 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
811 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
812 {"get_completer_delims", get_completer_delims,
813 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 {"set_completion_display_matches_hook", set_completion_display_matches_hook,
816 METH_VARARGS, doc_set_completion_display_matches_hook},
817 {"set_startup_hook", set_startup_hook,
818 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000819#ifdef HAVE_RL_PRE_INPUT_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 {"set_pre_input_hook", set_pre_input_hook,
821 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000822#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000823#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
Martin v. Löwise7a97962003-09-20 16:08:33 +0000825#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000827};
828
Guido van Rossum05ac4492003-01-07 20:04:12 +0000829
Martin v. Löwis0daad592001-09-30 21:09:59 +0000830/* C function to call the Python hooks. */
831
832static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000833on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000834{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 int result = 0;
836 if (func != NULL) {
837 PyObject *r;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 r = PyObject_CallFunction(func, NULL);
839 if (r == NULL)
840 goto error;
841 if (r == Py_None)
842 result = 0;
843 else {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300844 result = _PyLong_AsInt(r);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 if (result == -1 && PyErr_Occurred())
846 goto error;
847 }
848 Py_DECREF(r);
849 goto done;
850 error:
851 PyErr_Clear();
852 Py_XDECREF(r);
853 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 return result;
855 }
856 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000857}
858
859static int
Ned Deily7b9ddea2014-02-05 16:53:10 -0800860#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000861on_startup_hook(void)
Ned Deily7b9ddea2014-02-05 16:53:10 -0800862#else
863on_startup_hook()
864#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000865{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200866 int r;
867#ifdef WITH_THREAD
868 PyGILState_STATE gilstate = PyGILState_Ensure();
869#endif
870 r = on_hook(readlinestate_global->startup_hook);
871#ifdef WITH_THREAD
872 PyGILState_Release(gilstate);
873#endif
874 return r;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000875}
876
877#ifdef HAVE_RL_PRE_INPUT_HOOK
878static int
Ned Deily7b9ddea2014-02-05 16:53:10 -0800879#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000880on_pre_input_hook(void)
Ned Deily7b9ddea2014-02-05 16:53:10 -0800881#else
882on_pre_input_hook()
883#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000884{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200885 int r;
886#ifdef WITH_THREAD
887 PyGILState_STATE gilstate = PyGILState_Ensure();
888#endif
889 r = on_hook(readlinestate_global->pre_input_hook);
890#ifdef WITH_THREAD
891 PyGILState_Release(gilstate);
892#endif
893 return r;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000894}
895#endif
896
Guido van Rossum05ac4492003-01-07 20:04:12 +0000897
Thomas Wouters89d996e2007-09-08 17:39:28 +0000898/* C function to call the Python completion_display_matches */
899
Georg Brandl646fdd62010-10-18 07:27:55 +0000900#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Thomas Wouters89d996e2007-09-08 17:39:28 +0000901static void
902on_completion_display_matches_hook(char **matches,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 int num_matches, int max_length)
Thomas Wouters89d996e2007-09-08 17:39:28 +0000904{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 int i;
906 PyObject *m=NULL, *s=NULL, *r=NULL;
Christian Heimesaec75c32007-11-11 22:42:36 +0000907#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 PyGILState_STATE gilstate = PyGILState_Ensure();
Thomas Wouters89d996e2007-09-08 17:39:28 +0000909#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 m = PyList_New(num_matches);
911 if (m == NULL)
912 goto error;
913 for (i = 0; i < num_matches; i++) {
914 s = PyUnicode_FromString(matches[i+1]);
915 if (s == NULL)
916 goto error;
917 if (PyList_SetItem(m, i, s) == -1)
918 goto error;
919 }
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200920 r = PyObject_CallFunction(readlinestate_global->completion_display_matches_hook,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 "sOi", matches[0], m, max_length);
Christian Heimes32fbe592007-11-12 15:01:33 +0000922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 Py_DECREF(m); m=NULL;
Christian Heimes32fbe592007-11-12 15:01:33 +0000924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 if (r == NULL ||
926 (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) {
927 goto error;
928 }
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200929 Py_CLEAR(r);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930
931 if (0) {
932 error:
933 PyErr_Clear();
934 Py_XDECREF(m);
935 Py_XDECREF(r);
936 }
Christian Heimes32fbe592007-11-12 15:01:33 +0000937#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 PyGILState_Release(gilstate);
Christian Heimes32fbe592007-11-12 15:01:33 +0000939#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000940}
941
Senthil Kumaran95c07002010-11-04 03:51:05 +0000942#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000943
Guido van Rossum290900a1997-09-26 21:51:21 +0000944/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000945
Guido van Rossum290900a1997-09-26 21:51:21 +0000946static char *
Guido van Rossumd8faa362007-04-27 19:54:29 +0000947on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000948{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 char *result = NULL;
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200950 if (readlinestate_global->completer != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 PyObject *r;
Christian Heimesaec75c32007-11-11 22:42:36 +0000952#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000954#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 rl_attempted_completion_over = 1;
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200956 r = PyObject_CallFunction(readlinestate_global->completer, "si", text, state);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 if (r == NULL)
958 goto error;
959 if (r == Py_None) {
960 result = NULL;
961 }
962 else {
963 char *s = _PyUnicode_AsString(r);
964 if (s == NULL)
965 goto error;
966 result = strdup(s);
967 }
968 Py_DECREF(r);
969 goto done;
970 error:
971 PyErr_Clear();
972 Py_XDECREF(r);
973 done:
Christian Heimesaec75c32007-11-11 22:42:36 +0000974#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 PyGILState_Release(gilstate);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000976#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 return result;
978 }
979 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000980}
981
Guido van Rossum290900a1997-09-26 21:51:21 +0000982
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000983/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000984 * before calling the normal completer */
985
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000986static char **
Benjamin Petersonf0b463a2014-01-24 11:44:16 -0500987flex_complete(const char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000988{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200989 char **result;
990#ifdef WITH_THREAD
991 PyGILState_STATE gilstate = PyGILState_Ensure();
992#endif
Antoine Pitroudc0900b2009-10-19 18:22:37 +0000993#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 rl_completion_append_character ='\0';
Antoine Pitroud5131772009-10-26 19:22:14 +0000995#endif
996#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 rl_completion_suppress_append = 0;
Antoine Pitroudc0900b2009-10-19 18:22:37 +0000998#endif
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200999 Py_XDECREF(readlinestate_global->begidx);
1000 Py_XDECREF(readlinestate_global->endidx);
1001 readlinestate_global->begidx = PyLong_FromLong((long) start);
1002 readlinestate_global->endidx = PyLong_FromLong((long) end);
1003 result = completion_matches(text, *on_completion);
1004#ifdef WITH_THREAD
1005 PyGILState_Release(gilstate);
1006#endif
1007 return result;
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001008}
1009
Guido van Rossum05ac4492003-01-07 20:04:12 +00001010
Guido van Rossum290900a1997-09-26 21:51:21 +00001011/* Helper to initialize GNU readline properly. */
1012
1013static void
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001014setup_readline(readlinestate *mod_state)
Guido van Rossum290900a1997-09-26 21:51:21 +00001015{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +00001016#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1018 if (!saved_locale)
1019 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +00001020#endif
1021
R. David Murray52d1b4e2010-12-18 03:48:32 +00001022#ifdef __APPLE__
Victor Stinner6ced7c42011-03-21 18:15:42 +01001023 /* the libedit readline emulation resets key bindings etc
R. David Murray52d1b4e2010-12-18 03:48:32 +00001024 * when calling rl_initialize. So call it upfront
1025 */
1026 if (using_libedit_emulation)
1027 rl_initialize();
Ned Deilyf70f4a62013-09-06 15:16:19 -07001028
1029 /* Detect if libedit's readline emulation uses 0-based
1030 * indexing or 1-based indexing.
1031 */
1032 add_history("1");
1033 if (history_get(1) == NULL) {
1034 libedit_history_start = 0;
1035 } else {
1036 libedit_history_start = 1;
1037 }
1038 clear_history();
R. David Murray52d1b4e2010-12-18 03:48:32 +00001039#endif /* __APPLE__ */
1040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 using_history();
Skip Montanaroa0392742002-06-11 14:32:46 +00001042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 rl_readline_name = "python";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 /* Force rebind of TAB to insert-tab */
1045 rl_bind_key('\t', rl_insert);
1046 /* Bind both ESC-TAB and ESC-ESC to the completion function */
1047 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
1048 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
1049 /* Set our hook functions */
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001050 rl_startup_hook = on_startup_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001051#ifdef HAVE_RL_PRE_INPUT_HOOK
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001052 rl_pre_input_hook = on_pre_input_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001053#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 /* Set our completion function */
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001055 rl_attempted_completion_function = flex_complete;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 /* Set Python word break characters */
Antoine Pitroua7f7deb2013-05-06 21:51:03 +02001057 completer_word_break_characters =
1058 rl_completer_word_break_characters =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
1060 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001061
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001062 mod_state->begidx = PyLong_FromLong(0L);
1063 mod_state->endidx = PyLong_FromLong(0L);
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001064
Victor Stinner92639cc2014-07-24 22:11:38 +02001065#ifndef __APPLE__
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001066 if (!isatty(STDOUT_FILENO)) {
1067 /* Issue #19884: stdout is no a terminal. Disable meta modifier
1068 keys to not write the ANSI sequence "\033[1034h" into stdout. On
1069 terminals supporting 8 bit characters like TERM=xterm-256color
1070 (which is now the default Fedora since Fedora 18), the meta key is
1071 used to enable support of 8 bit characters (ANSI sequence
Victor Stinner92639cc2014-07-24 22:11:38 +02001072 "\033[1034h").
1073
1074 With libedit, this call makes readline() crash. */
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001075 rl_variable_bind ("enable-meta-key", "off");
1076 }
Victor Stinner92639cc2014-07-24 22:11:38 +02001077#endif
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 /* Initialize (allows .inputrc to override)
1080 *
1081 * XXX: A bug in the readline-2.2 library causes a memory leak
1082 * inside this function. Nothing we can do about it.
1083 */
R. David Murray52d1b4e2010-12-18 03:48:32 +00001084#ifdef __APPLE__
1085 if (using_libedit_emulation)
Victor Stinner6ced7c42011-03-21 18:15:42 +01001086 rl_read_init_file(NULL);
R. David Murray52d1b4e2010-12-18 03:48:32 +00001087 else
1088#endif /* __APPLE__ */
1089 rl_initialize();
Victor Stinner6ced7c42011-03-21 18:15:42 +01001090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 RESTORE_LOCALE(saved_locale)
Guido van Rossum290900a1997-09-26 21:51:21 +00001092}
1093
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001094/* Wrapper around GNU readline that handles signals differently. */
1095
1096
1097#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
1098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099static char *completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001100static void
1101rlhandler(char *text)
1102{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 completed_input_string = text;
1104 rl_callback_handler_remove();
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001105}
1106
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001107static char *
Serhiy Storchakac6792272013-10-19 21:03:34 +03001108readline_until_enter_or_signal(const char *prompt, int *signal)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001109{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001110 char * not_done_reading = "";
1111 fd_set selectset;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 *signal = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001114#ifdef HAVE_RL_CATCH_SIGNAL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 rl_catch_signals = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001116#endif
1117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 rl_callback_handler_install (prompt, rlhandler);
1119 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 completed_input_string = not_done_reading;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 while (completed_input_string == not_done_reading) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001124 int has_input = 0, err = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 while (!has_input)
1127 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Michael W. Hudson8da2b012004-10-07 13:46:33 +00001128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 /* [Bug #1552726] Only limit the pause if an input hook has been
1130 defined. */
1131 struct timeval *timeoutp = NULL;
1132 if (PyOS_InputHook)
1133 timeoutp = &timeout;
1134 FD_SET(fileno(rl_instream), &selectset);
1135 /* select resets selectset if no input was available */
1136 has_input = select(fileno(rl_instream) + 1, &selectset,
1137 NULL, NULL, timeoutp);
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001138 err = errno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 if(PyOS_InputHook) PyOS_InputHook();
1140 }
1141
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001142 if (has_input > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 rl_callback_read_char();
1144 }
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001145 else if (err == EINTR) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001147#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001149#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001151#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001153#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 if (s < 0) {
1155 rl_free_line_state();
1156 rl_cleanup_after_signal();
1157 rl_callback_handler_remove();
1158 *signal = 1;
1159 completed_input_string = NULL;
1160 }
1161 }
1162 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 return completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001165}
1166
1167
1168#else
Guido van Rossum290900a1997-09-26 21:51:21 +00001169
1170/* Interrupt handler */
1171
1172static jmp_buf jbuf;
1173
Guido van Rossum0969d361997-08-05 21:27:50 +00001174/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +00001175static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001176onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +00001177{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +00001179}
1180
Guido van Rossum290900a1997-09-26 21:51:21 +00001181
Guido van Rossum0969d361997-08-05 21:27:50 +00001182static char *
Christian Heimesa3da7c52013-12-04 09:31:47 +01001183readline_until_enter_or_signal(const char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +00001184{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 PyOS_sighandler_t old_inthandler;
1186 char *p;
Guido van Rossum74f31432003-01-07 20:01:29 +00001187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 *signal = 0;
1189
1190 old_inthandler = PyOS_setsig(SIGINT, onintr);
1191 if (setjmp(jbuf)) {
Guido van Rossum0969d361997-08-05 21:27:50 +00001192#ifdef HAVE_SIGRELSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
1194 sigrelse(SIGINT);
Guido van Rossum0969d361997-08-05 21:27:50 +00001195#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 PyOS_setsig(SIGINT, old_inthandler);
1197 *signal = 1;
1198 return NULL;
1199 }
1200 rl_event_hook = PyOS_InputHook;
1201 p = readline(prompt);
1202 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001203
1204 return p;
1205}
1206#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
1207
1208
1209static char *
Serhiy Storchakac6792272013-10-19 21:03:34 +03001210call_readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001211{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 size_t n;
1213 char *p, *q;
1214 int signal;
Neal Norwitz1fa040b2004-08-25 01:20:18 +00001215
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001216#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1218 if (!saved_locale)
1219 Py_FatalError("not enough memory to save locale");
Nadeem Vawda63752572013-02-02 20:05:11 +01001220 setlocale(LC_CTYPE, "");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001221#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
1224 rl_instream = sys_stdin;
1225 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001226#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001228#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 }
Guido van Rossum74f31432003-01-07 20:01:29 +00001230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 p = readline_until_enter_or_signal(prompt, &signal);
Guido van Rossumb18618d2000-05-03 23:44:39 +00001232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 /* we got an interrupt signal */
1234 if (signal) {
1235 RESTORE_LOCALE(saved_locale)
1236 return NULL;
1237 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001238
Martin Panter7462b6492015-11-02 03:37:02 +00001239 /* We got an EOF, return an empty string. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 if (p == NULL) {
Victor Stinner2fe9bac2013-10-10 16:18:20 +02001241 p = PyMem_RawMalloc(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 if (p != NULL)
1243 *p = '\0';
1244 RESTORE_LOCALE(saved_locale)
1245 return p;
1246 }
1247
1248 /* we have a valid line */
1249 n = strlen(p);
1250 if (n > 0) {
Brett Cannon2525dc82010-08-22 20:36:25 +00001251 const char *line;
Mark Dickinson6b54e1f2010-08-03 16:49:49 +00001252 int length = _py_get_history_length();
1253 if (length > 0)
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001254#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 if (using_libedit_emulation) {
Ned Deilyf70f4a62013-09-06 15:16:19 -07001256 /* handle older 0-based or newer 1-based indexing */
1257 line = (const char *)history_get(length + libedit_history_start - 1)->line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 } else
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001259#endif /* __APPLE__ */
Brett Cannon2525dc82010-08-22 20:36:25 +00001260 line = (const char *)history_get(length)->line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 else
1262 line = "";
1263 if (strcmp(p, line))
1264 add_history(p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 }
1266 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
1267 release the original. */
1268 q = p;
Victor Stinner2fe9bac2013-10-10 16:18:20 +02001269 p = PyMem_RawMalloc(n+2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 if (p != NULL) {
1271 strncpy(p, q, n);
1272 p[n] = '\n';
1273 p[n+1] = '\0';
1274 }
1275 free(q);
1276 RESTORE_LOCALE(saved_locale)
1277 return p;
Guido van Rossum0969d361997-08-05 21:27:50 +00001278}
1279
Guido van Rossum290900a1997-09-26 21:51:21 +00001280
1281/* Initialize the module */
1282
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001283PyDoc_STRVAR(doc_module,
1284"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001285
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001286#ifdef __APPLE__
1287PyDoc_STRVAR(doc_module_le,
1288"Importing this module enables command line editing using libedit readline.");
1289#endif /* __APPLE__ */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001290
1291static struct PyModuleDef readlinemodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 PyModuleDef_HEAD_INIT,
1293 "readline",
1294 doc_module,
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001295 sizeof(readlinestate),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 readline_methods,
1297 NULL,
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001298 readline_traverse,
1299 readline_clear,
1300 readline_free
Martin v. Löwis1a214512008-06-11 05:26:20 +00001301};
1302
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001303
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001304PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001305PyInit_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 PyObject *m;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001308 readlinestate *mod_state;
Guido van Rossum290900a1997-09-26 21:51:21 +00001309
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001310#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
1312 using_libedit_emulation = 1;
1313 }
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 if (using_libedit_emulation)
1316 readlinemodule.m_doc = doc_module_le;
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001317
1318#endif /* __APPLE__ */
1319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 m = PyModule_Create(&readlinemodule);
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 if (m == NULL)
1323 return NULL;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001324
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001325 mod_state = (readlinestate *) PyModule_GetState(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 PyOS_ReadlineFunctionPointer = call_readline;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001327 setup_readline(mod_state);
Antoine Pitrou7e8b8672014-11-04 14:52:10 +01001328
1329 PyModule_AddIntConstant(m, "_READLINE_VERSION", RL_READLINE_VERSION);
1330 PyModule_AddIntConstant(m, "_READLINE_RUNTIME_VERSION", rl_readline_version);
1331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 return m;
Guido van Rossum0969d361997-08-05 21:27:50 +00001333}