blob: bbab0f882e3f16336fdcc07ca00cecdc63c96b67 [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 <signal.h>
Guido van Rossum290900a1997-09-26 21:51:21 +000011#include <errno.h>
Michael W. Hudson8da2b012004-10-07 13:46:33 +000012#include <sys/time.h>
Guido van Rossum0969d361997-08-05 21:27:50 +000013
Skip Montanaro7befb992004-02-10 16:50:21 +000014#if defined(HAVE_SETLOCALE)
Guido van Rossum60c8a3a2002-10-09 21:27:33 +000015/* GNU readline() mistakenly sets the LC_CTYPE locale.
16 * This is evil. Only the user or the app's main() should do this!
17 * We must save and restore the locale around the rl_initialize() call.
18 */
19#define SAVE_LOCALE
20#include <locale.h>
21#endif
22
Thomas Wouters0e3f5912006-08-11 14:57:12 +000023#ifdef SAVE_LOCALE
24# define RESTORE_LOCALE(sl) { setlocale(LC_CTYPE, sl); free(sl); }
25#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000026# define RESTORE_LOCALE(sl)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000027#endif
28
Guido van Rossum290900a1997-09-26 21:51:21 +000029/* GNU readline definitions */
Guido van Rossumb0e51b22001-04-13 18:14:27 +000030#undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
Guido van Rossumbcc20741998-08-04 22:53:56 +000031#include <readline/readline.h>
32#include <readline/history.h>
Guido van Rossum730806d1998-04-10 22:27:42 +000033
Guido van Rossum353ae582001-07-10 16:45:32 +000034#ifdef HAVE_RL_COMPLETION_MATCHES
Guido van Rossum74f31432003-01-07 20:01:29 +000035#define completion_matches(x, y) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036 rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
Guido van Rossumd8faa362007-04-27 19:54:29 +000037#else
Martin v. Löwisb37509b2008-11-04 20:45:29 +000038#if defined(_RL_FUNCTION_TYPEDEF)
Guido van Rossumd8faa362007-04-27 19:54:29 +000039extern char **completion_matches(char *, rl_compentry_func_t *);
Martin v. Löwisb37509b2008-11-04 20:45:29 +000040#else
Ronald Oussoren25696bb2010-02-11 13:15:00 +000041
42#if !defined(__APPLE__)
Martin v. Löwisb37509b2008-11-04 20:45:29 +000043extern char **completion_matches(char *, CPFunction *);
44#endif
Guido van Rossum353ae582001-07-10 16:45:32 +000045#endif
Ronald Oussoren25696bb2010-02-11 13:15:00 +000046#endif
Guido van Rossum353ae582001-07-10 16:45:32 +000047
Ronald Oussoren2efd9242009-09-20 14:53:22 +000048/*
49 * It is possible to link the readline module to the readline
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 * emulation library of editline/libedit.
51 *
serge-sans-paille71053192019-12-04 17:02:57 +010052 * This emulation library is not 100% API compatible with the "real" readline
53 * and cannot be detected at compile-time,
54 * hence we use a runtime check to detect if the Python readlinke module is
55 * linked to libedit.
Ronald Oussoren2efd9242009-09-20 14:53:22 +000056 *
Ned Deily5d4121a2013-10-12 15:47:58 -070057 * Currently there is one known API incompatibility:
Ronald Oussoren2efd9242009-09-20 14:53:22 +000058 * - 'get_history' has a 1-based index with GNU readline, and a 0-based
Ned Deily5d4121a2013-10-12 15:47:58 -070059 * index with older versions of libedit's emulation.
Ronald Oussoren2efd9242009-09-20 14:53:22 +000060 * - Note that replace_history and remove_history use a 0-based index
Ned Deily5d4121a2013-10-12 15:47:58 -070061 * with both implementations.
Ronald Oussoren2efd9242009-09-20 14:53:22 +000062 */
63static int using_libedit_emulation = 0;
64static const char libedit_version_tag[] = "EditLine wrapper";
Ned Deilyf70f4a62013-09-06 15:16:19 -070065
66static int libedit_history_start = 0;
Ronald Oussoren2efd9242009-09-20 14:53:22 +000067
Georg Brandl646fdd62010-10-18 07:27:55 +000068#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Christian Heimes32fbe592007-11-12 15:01:33 +000069static void
70on_completion_display_matches_hook(char **matches,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 int num_matches, int max_length);
Georg Brandl646fdd62010-10-18 07:27:55 +000072#endif
Guido van Rossum0969d361997-08-05 21:27:50 +000073
Antoine Pitroua7f7deb2013-05-06 21:51:03 +020074/* Memory allocated for rl_completer_word_break_characters
75 (see issue #17289 for the motivation). */
76static char *completer_word_break_characters;
77
Antoine Pitrou5c30a752013-07-31 21:52:53 +020078typedef struct {
Martin Panterf6e9f472016-03-22 02:19:29 +000079 /* Specify hook functions in Python */
Antoine Pitrou5c30a752013-07-31 21:52:53 +020080 PyObject *completion_display_matches_hook;
81 PyObject *startup_hook;
82 PyObject *pre_input_hook;
Martin Panterf6e9f472016-03-22 02:19:29 +000083
84 PyObject *completer; /* Specify a word completer in Python */
Antoine Pitrou5c30a752013-07-31 21:52:53 +020085 PyObject *begidx;
86 PyObject *endidx;
87} readlinestate;
88
Hai Shif707d942020-03-16 21:15:01 +080089static inline readlinestate*
Hai Shi5f104d52020-03-17 01:16:32 +080090get_readline_state(PyObject *module)
Hai Shif707d942020-03-16 21:15:01 +080091{
92 void *state = PyModule_GetState(module);
93 assert(state != NULL);
94 return (readlinestate *)state;
95}
Antoine Pitrou5c30a752013-07-31 21:52:53 +020096
Zackery Spytzb7047e52020-07-12 10:01:03 -060097/*[clinic input]
98module readline
99[clinic start generated code]*/
100/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ad49da781b9c8721]*/
101
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200102static int
103readline_clear(PyObject *m)
104{
Hai Shif707d942020-03-16 21:15:01 +0800105 readlinestate *state = get_readline_state(m);
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200106 Py_CLEAR(state->completion_display_matches_hook);
107 Py_CLEAR(state->startup_hook);
108 Py_CLEAR(state->pre_input_hook);
109 Py_CLEAR(state->completer);
110 Py_CLEAR(state->begidx);
111 Py_CLEAR(state->endidx);
112 return 0;
113}
114
115static int
116readline_traverse(PyObject *m, visitproc visit, void *arg)
117{
Hai Shif707d942020-03-16 21:15:01 +0800118 readlinestate *state = get_readline_state(m);
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200119 Py_VISIT(state->completion_display_matches_hook);
120 Py_VISIT(state->startup_hook);
121 Py_VISIT(state->pre_input_hook);
122 Py_VISIT(state->completer);
123 Py_VISIT(state->begidx);
124 Py_VISIT(state->endidx);
125 return 0;
126}
127
128static void
129readline_free(void *m)
130{
131 readline_clear((PyObject *)m);
132}
133
134static PyModuleDef readlinemodule;
135
136#define readlinestate_global ((readlinestate *)PyModule_GetState(PyState_FindModule(&readlinemodule)))
137
138
Martin Panterf00c49d2016-06-14 01:16:16 +0000139/* Convert to/from multibyte C strings */
140
141static PyObject *
142encode(PyObject *b)
143{
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100144 return PyUnicode_EncodeLocale(b, "surrogateescape");
Martin Panterf00c49d2016-06-14 01:16:16 +0000145}
146
147static PyObject *
148decode(const char *s)
149{
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100150 return PyUnicode_DecodeLocale(s, "surrogateescape");
Martin Panterf00c49d2016-06-14 01:16:16 +0000151}
152
153
Guido van Rossum290900a1997-09-26 21:51:21 +0000154/* Exported function to send one line to readline's init file parser */
155
Zackery Spytzb7047e52020-07-12 10:01:03 -0600156/*[clinic input]
157readline.parse_and_bind
158
159 string: object
160 /
161
162Execute the init line provided in the string argument.
163[clinic start generated code]*/
164
Guido van Rossum290900a1997-09-26 21:51:21 +0000165static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600166readline_parse_and_bind(PyObject *module, PyObject *string)
167/*[clinic end generated code: output=1a1ede8afb9546c1 input=8a28a00bb4d61eec]*/
Guido van Rossum290900a1997-09-26 21:51:21 +0000168{
Martin Panterf00c49d2016-06-14 01:16:16 +0000169 char *copy;
170 PyObject *encoded = encode(string);
171 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 return NULL;
Martin Panterf00c49d2016-06-14 01:16:16 +0000173 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 /* Make a copy -- rl_parse_and_bind() modifies its argument */
175 /* Bernard Herzog */
Martin Panterf00c49d2016-06-14 01:16:16 +0000176 copy = PyMem_Malloc(1 + PyBytes_GET_SIZE(encoded));
177 if (copy == NULL) {
178 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179 return PyErr_NoMemory();
Martin Panterf00c49d2016-06-14 01:16:16 +0000180 }
181 strcpy(copy, PyBytes_AS_STRING(encoded));
182 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 rl_parse_and_bind(copy);
Victor Stinnerb6404912013-07-07 16:21:41 +0200184 PyMem_Free(copy); /* Free the copy */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +0000186}
187
Guido van Rossum290900a1997-09-26 21:51:21 +0000188/* Exported function to parse a readline init file */
189
Zackery Spytzb7047e52020-07-12 10:01:03 -0600190/*[clinic input]
191readline.read_init_file
192
193 filename as filename_obj: object = None
194 /
195
196Execute a readline initialization file.
197
198The default filename is the last filename used.
199[clinic start generated code]*/
200
Guido van Rossum290900a1997-09-26 21:51:21 +0000201static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600202readline_read_init_file_impl(PyObject *module, PyObject *filename_obj)
203/*[clinic end generated code: output=8e059b676142831e input=4c80c473e448139d]*/
Guido van Rossum290900a1997-09-26 21:51:21 +0000204{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600205 PyObject *filename_bytes;
Victor Stinner19e65a32010-06-11 22:27:14 +0000206 if (filename_obj != Py_None) {
207 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
208 return NULL;
Zackery Spytzb7047e52020-07-12 10:01:03 -0600209 errno = rl_read_init_file(PyBytes_AS_STRING(filename_bytes));
Victor Stinner19e65a32010-06-11 22:27:14 +0000210 Py_DECREF(filename_bytes);
211 } else
212 errno = rl_read_init_file(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 if (errno)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300214 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +0000216}
217
Skip Montanaro28067822000-07-06 18:55:12 +0000218/* Exported function to load a readline history file */
219
Zackery Spytzb7047e52020-07-12 10:01:03 -0600220/*[clinic input]
221readline.read_history_file
222
223 filename as filename_obj: object = None
224 /
225
226Load a readline history file.
227
228The default filename is ~/.history.
229[clinic start generated code]*/
230
Skip Montanaro28067822000-07-06 18:55:12 +0000231static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600232readline_read_history_file_impl(PyObject *module, PyObject *filename_obj)
233/*[clinic end generated code: output=66a951836fb54fbb input=3d29d755b7e6932e]*/
Skip Montanaro28067822000-07-06 18:55:12 +0000234{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600235 PyObject *filename_bytes;
Victor Stinner19e65a32010-06-11 22:27:14 +0000236 if (filename_obj != Py_None) {
237 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
238 return NULL;
Zackery Spytzb7047e52020-07-12 10:01:03 -0600239 errno = read_history(PyBytes_AS_STRING(filename_bytes));
Victor Stinner19e65a32010-06-11 22:27:14 +0000240 Py_DECREF(filename_bytes);
241 } else
242 errno = read_history(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 if (errno)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300244 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000246}
247
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000248static int _history_length = -1; /* do not truncate history by default */
Skip Montanaro28067822000-07-06 18:55:12 +0000249
250/* Exported function to save a readline history file */
251
Zackery Spytzb7047e52020-07-12 10:01:03 -0600252/*[clinic input]
253readline.write_history_file
254
255 filename as filename_obj: object = None
256 /
257
258Save a readline history file.
259
260The default filename is ~/.history.
261[clinic start generated code]*/
262
Skip Montanaro28067822000-07-06 18:55:12 +0000263static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600264readline_write_history_file_impl(PyObject *module, PyObject *filename_obj)
265/*[clinic end generated code: output=fbcad13d8ef59ae6 input=28a8e062fe363703]*/
Skip Montanaro28067822000-07-06 18:55:12 +0000266{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600267 PyObject *filename_bytes;
Serhiy Storchaka8f87eef2020-04-12 14:58:27 +0300268 const char *filename;
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100269 int err;
Victor Stinner19e65a32010-06-11 22:27:14 +0000270 if (filename_obj != Py_None) {
271 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
272 return NULL;
Zackery Spytzb7047e52020-07-12 10:01:03 -0600273 filename = PyBytes_AS_STRING(filename_bytes);
Victor Stinner19e65a32010-06-11 22:27:14 +0000274 } else {
275 filename_bytes = NULL;
276 filename = NULL;
277 }
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100278 errno = err = write_history(filename);
279 if (!err && _history_length >= 0)
Victor Stinner19e65a32010-06-11 22:27:14 +0000280 history_truncate_file(filename, _history_length);
281 Py_XDECREF(filename_bytes);
Antoine Pitrouc345ce12011-12-16 12:28:32 +0100282 errno = err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 if (errno)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300284 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000286}
287
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600288#ifdef HAVE_RL_APPEND_HISTORY
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600289/* Exported function to save part of a readline history file */
290
Zackery Spytzb7047e52020-07-12 10:01:03 -0600291/*[clinic input]
292readline.append_history_file
293
294 nelements: int
295 filename as filename_obj: object = None
296 /
297
298Append the last nelements items of the history list to file.
299
300The default filename is ~/.history.
301[clinic start generated code]*/
302
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600303static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600304readline_append_history_file_impl(PyObject *module, int nelements,
305 PyObject *filename_obj)
306/*[clinic end generated code: output=5df06fc9da56e4e4 input=784b774db3a4b7c5]*/
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600307{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600308 PyObject *filename_bytes;
Serhiy Storchaka8f87eef2020-04-12 14:58:27 +0300309 const char *filename;
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600310 int err;
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600311 if (filename_obj != Py_None) {
312 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
313 return NULL;
Zackery Spytzb7047e52020-07-12 10:01:03 -0600314 filename = PyBytes_AS_STRING(filename_bytes);
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600315 } else {
316 filename_bytes = NULL;
317 filename = NULL;
318 }
319 errno = err = append_history(nelements, filename);
320 if (!err && _history_length >= 0)
321 history_truncate_file(filename, _history_length);
322 Py_XDECREF(filename_bytes);
323 errno = err;
324 if (errno)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300325 return PyErr_SetFromErrno(PyExc_OSError);
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600326 Py_RETURN_NONE;
327}
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600328#endif
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600329
330
Guido van Rossum74f31432003-01-07 20:01:29 +0000331/* Set history length */
332
Zackery Spytzb7047e52020-07-12 10:01:03 -0600333/*[clinic input]
334readline.set_history_length
335
336 length: int
337 /
338
339Set the maximal number of lines which will be written to the history file.
340
341A negative length is used to inhibit history truncation.
342[clinic start generated code]*/
343
344static PyObject *
345readline_set_history_length_impl(PyObject *module, int length)
346/*[clinic end generated code: output=e161a53e45987dc7 input=b8901bf16488b760]*/
Guido van Rossum74f31432003-01-07 20:01:29 +0000347{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 _history_length = length;
349 Py_RETURN_NONE;
Guido van Rossum74f31432003-01-07 20:01:29 +0000350}
351
Guido van Rossum74f31432003-01-07 20:01:29 +0000352/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000353
Zackery Spytzb7047e52020-07-12 10:01:03 -0600354/*[clinic input]
355readline.get_history_length
356
357Return the maximum number of lines that will be written to the history file.
358[clinic start generated code]*/
359
360static PyObject *
361readline_get_history_length_impl(PyObject *module)
362/*[clinic end generated code: output=83a2eeae35b6d2b9 input=5dce2eeba4327817]*/
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000363{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 return PyLong_FromLong(_history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000365}
366
Martin v. Löwis0daad592001-09-30 21:09:59 +0000367/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000368
Martin v. Löwis0daad592001-09-30 21:09:59 +0000369static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600370set_hook(const char *funcname, PyObject **hook_var, PyObject *function)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000371{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 if (function == Py_None) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200373 Py_CLEAR(*hook_var);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 }
375 else if (PyCallable_Check(function)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 Py_INCREF(function);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300377 Py_XSETREF(*hook_var, function);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 }
379 else {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100380 PyErr_Format(PyExc_TypeError,
381 "set_%.50s(func): argument not callable",
382 funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 return NULL;
384 }
385 Py_RETURN_NONE;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000386}
387
Zackery Spytzb7047e52020-07-12 10:01:03 -0600388/*[clinic input]
389readline.set_completion_display_matches_hook
390
391 function: object = None
392 /
393
394Set or remove the completion display function.
395
396The function is called as
397 function(substitution, [matches], longest_match_length)
398once each time matches need to be displayed.
399[clinic start generated code]*/
Guido van Rossum74f31432003-01-07 20:01:29 +0000400
Martin v. Löwis0daad592001-09-30 21:09:59 +0000401static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600402readline_set_completion_display_matches_hook_impl(PyObject *module,
403 PyObject *function)
404/*[clinic end generated code: output=516e5cb8db75a328 input=4f0bfd5ab0179a26]*/
Thomas Wouters89d996e2007-09-08 17:39:28 +0000405{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 PyObject *result = set_hook("completion_display_matches_hook",
Zackery Spytzb7047e52020-07-12 10:01:03 -0600407 &readlinestate_global->completion_display_matches_hook,
408 function);
Christian Heimes32fbe592007-11-12 15:01:33 +0000409#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 /* We cannot set this hook globally, since it replaces the
411 default completion display. */
412 rl_completion_display_matches_hook =
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200413 readlinestate_global->completion_display_matches_hook ?
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000414#if defined(_RL_FUNCTION_TYPEDEF)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000416#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 (VFunction *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000418#endif
Christian Heimes32fbe592007-11-12 15:01:33 +0000419#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 return result;
Christian Heimes32fbe592007-11-12 15:01:33 +0000421
Thomas Wouters89d996e2007-09-08 17:39:28 +0000422}
423
Zackery Spytzb7047e52020-07-12 10:01:03 -0600424/*[clinic input]
425readline.set_startup_hook
426
427 function: object = None
428 /
429
430Set or remove the function invoked by the rl_startup_hook callback.
431
432The function is called with no arguments just
433before readline prints the first prompt.
434[clinic start generated code]*/
Thomas Wouters89d996e2007-09-08 17:39:28 +0000435
436static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600437readline_set_startup_hook_impl(PyObject *module, PyObject *function)
438/*[clinic end generated code: output=02cd0e0c4fa082ad input=7783b4334b26d16d]*/
Martin v. Löwis0daad592001-09-30 21:09:59 +0000439{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600440 return set_hook("startup_hook", &readlinestate_global->startup_hook,
441 function);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000442}
443
Martin v. Löwis0daad592001-09-30 21:09:59 +0000444#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000445
446/* Set pre-input hook */
447
Zackery Spytzb7047e52020-07-12 10:01:03 -0600448/*[clinic input]
449readline.set_pre_input_hook
450
451 function: object = None
452 /
453
454Set or remove the function invoked by the rl_pre_input_hook callback.
455
456The function is called with no arguments after the first prompt
457has been printed and just before readline starts reading input
458characters.
459[clinic start generated code]*/
460
Martin v. Löwis0daad592001-09-30 21:09:59 +0000461static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600462readline_set_pre_input_hook_impl(PyObject *module, PyObject *function)
463/*[clinic end generated code: output=fe1a96505096f464 input=4f3eaeaf7ce1fdbe]*/
Martin v. Löwis0daad592001-09-30 21:09:59 +0000464{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600465 return set_hook("pre_input_hook", &readlinestate_global->pre_input_hook,
466 function);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000467}
Martin v. Löwis0daad592001-09-30 21:09:59 +0000468#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000469
Guido van Rossum74f31432003-01-07 20:01:29 +0000470
Thomas Wouters89d996e2007-09-08 17:39:28 +0000471/* Get the completion type for the scope of the tab-completion */
Zackery Spytzb7047e52020-07-12 10:01:03 -0600472
473/*[clinic input]
474readline.get_completion_type
475
476Get the type of completion being attempted.
477[clinic start generated code]*/
478
Thomas Wouters89d996e2007-09-08 17:39:28 +0000479static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600480readline_get_completion_type_impl(PyObject *module)
481/*[clinic end generated code: output=5c54d58a04997c07 input=04b92bc7a82dac91]*/
Thomas Wouters89d996e2007-09-08 17:39:28 +0000482{
Christian Heimes217cfd12007-12-02 14:31:20 +0000483 return PyLong_FromLong(rl_completion_type);
Thomas Wouters89d996e2007-09-08 17:39:28 +0000484}
485
Guido van Rossum74f31432003-01-07 20:01:29 +0000486/* Get the beginning index for the scope of the tab-completion */
487
Zackery Spytzb7047e52020-07-12 10:01:03 -0600488/*[clinic input]
489readline.get_begidx
490
491Get the beginning index of the completion scope.
492[clinic start generated code]*/
493
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000494static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600495readline_get_begidx_impl(PyObject *module)
496/*[clinic end generated code: output=362616ee8ed1b2b1 input=e083b81c8eb4bac3]*/
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000497{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200498 Py_INCREF(readlinestate_global->begidx);
499 return readlinestate_global->begidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000500}
501
Guido van Rossum74f31432003-01-07 20:01:29 +0000502/* Get the ending index for the scope of the tab-completion */
503
Zackery Spytzb7047e52020-07-12 10:01:03 -0600504/*[clinic input]
505readline.get_endidx
506
507Get the ending index of the completion scope.
508[clinic start generated code]*/
509
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000510static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600511readline_get_endidx_impl(PyObject *module)
512/*[clinic end generated code: output=7f763350b12d7517 input=d4c7e34a625fd770]*/
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000513{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200514 Py_INCREF(readlinestate_global->endidx);
515 return readlinestate_global->endidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000516}
517
Guido van Rossum74f31432003-01-07 20:01:29 +0000518/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000519
Zackery Spytzb7047e52020-07-12 10:01:03 -0600520/*[clinic input]
521readline.set_completer_delims
522
523 string: object
524 /
525
526Set the word delimiters for completion.
527[clinic start generated code]*/
528
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000529static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600530readline_set_completer_delims(PyObject *module, PyObject *string)
531/*[clinic end generated code: output=4305b266106c4f1f input=ae945337ebd01e20]*/
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000532{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 char *break_chars;
Martin Panterf00c49d2016-06-14 01:16:16 +0000534 PyObject *encoded = encode(string);
535 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 return NULL;
537 }
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200538 /* Keep a reference to the allocated memory in the module state in case
539 some other module modifies rl_completer_word_break_characters
540 (see issue #17289). */
Martin Panterf00c49d2016-06-14 01:16:16 +0000541 break_chars = strdup(PyBytes_AS_STRING(encoded));
542 Py_DECREF(encoded);
Serhiy Storchaka11384392015-09-27 22:34:59 +0300543 if (break_chars) {
544 free(completer_word_break_characters);
545 completer_word_break_characters = break_chars;
546 rl_completer_word_break_characters = break_chars;
Antoine Pitroua7f7deb2013-05-06 21:51:03 +0200547 Py_RETURN_NONE;
548 }
549 else
550 return PyErr_NoMemory();
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000551}
552
Mark Dickinson29b238e2010-08-03 16:08:16 +0000553/* _py_free_history_entry: Utility function to free a history entry. */
554
555#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
556
557/* Readline version >= 5.0 introduced a timestamp field into the history entry
558 structure; this needs to be freed to avoid a memory leak. This version of
559 readline also introduced the handy 'free_history_entry' function, which
560 takes care of the timestamp. */
561
562static void
563_py_free_history_entry(HIST_ENTRY *entry)
564{
565 histdata_t data = free_history_entry(entry);
566 free(data);
567}
568
569#else
570
571/* No free_history_entry function; free everything manually. */
572
573static void
574_py_free_history_entry(HIST_ENTRY *entry)
575{
576 if (entry->line)
577 free((void *)entry->line);
578 if (entry->data)
579 free(entry->data);
580 free(entry);
581}
582
583#endif
584
Zackery Spytzb7047e52020-07-12 10:01:03 -0600585/*[clinic input]
586readline.remove_history_item
587
588 pos as entry_number: int
589 /
590
591Remove history item given by its position.
592[clinic start generated code]*/
593
Skip Montanaroe5069012004-08-15 14:32:06 +0000594static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600595readline_remove_history_item_impl(PyObject *module, int entry_number)
596/*[clinic end generated code: output=ab114f029208c7e8 input=c8520ac3da50224e]*/
Skip Montanaroe5069012004-08-15 14:32:06 +0000597{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 HIST_ENTRY *entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 if (entry_number < 0) {
601 PyErr_SetString(PyExc_ValueError,
602 "History index cannot be negative");
603 return NULL;
604 }
605 entry = remove_history(entry_number);
606 if (!entry) {
607 PyErr_Format(PyExc_ValueError,
608 "No history item at position %d",
609 entry_number);
610 return NULL;
611 }
612 /* free memory allocated for the history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000613 _py_free_history_entry(entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000615}
616
Zackery Spytzb7047e52020-07-12 10:01:03 -0600617/*[clinic input]
618readline.replace_history_item
619
620 pos as entry_number: int
621 line: unicode
622 /
623
624Replaces history item given by its position with contents of line.
625[clinic start generated code]*/
Skip Montanaroe5069012004-08-15 14:32:06 +0000626
627static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600628readline_replace_history_item_impl(PyObject *module, int entry_number,
629 PyObject *line)
630/*[clinic end generated code: output=f8cec2770ca125eb input=b7ccef0780ae041b]*/
Skip Montanaroe5069012004-08-15 14:32:06 +0000631{
Martin Panterf00c49d2016-06-14 01:16:16 +0000632 PyObject *encoded;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 HIST_ENTRY *old_entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 if (entry_number < 0) {
636 PyErr_SetString(PyExc_ValueError,
637 "History index cannot be negative");
638 return NULL;
639 }
Martin Panterf00c49d2016-06-14 01:16:16 +0000640 encoded = encode(line);
641 if (encoded == NULL) {
642 return NULL;
643 }
644 old_entry = replace_history_entry(entry_number, PyBytes_AS_STRING(encoded), (void *)NULL);
645 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 if (!old_entry) {
647 PyErr_Format(PyExc_ValueError,
648 "No history item at position %d",
649 entry_number);
650 return NULL;
651 }
652 /* free memory allocated for the old history entry */
Mark Dickinson29b238e2010-08-03 16:08:16 +0000653 _py_free_history_entry(old_entry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000655}
656
Guido van Rossum74f31432003-01-07 20:01:29 +0000657/* Add a line to the history buffer */
658
Zackery Spytzb7047e52020-07-12 10:01:03 -0600659/*[clinic input]
660readline.add_history
661
662 string: object
663 /
664
665Add an item to the history buffer.
666[clinic start generated code]*/
667
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000668static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600669readline_add_history(PyObject *module, PyObject *string)
670/*[clinic end generated code: output=b107b7e8106e803d input=e57c1cf6bc68d7e3]*/
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000671{
Martin Panterf00c49d2016-06-14 01:16:16 +0000672 PyObject *encoded = encode(string);
673 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 return NULL;
675 }
Martin Panterf00c49d2016-06-14 01:16:16 +0000676 add_history(PyBytes_AS_STRING(encoded));
677 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 Py_RETURN_NONE;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000679}
680
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000681static int should_auto_add_history = 1;
682
683/* Enable or disable automatic history */
684
Zackery Spytzb7047e52020-07-12 10:01:03 -0600685/*[clinic input]
686readline.set_auto_history
687
688 enabled as _should_auto_add_history: bool
689 /
690
691Enables or disables automatic history.
692[clinic start generated code]*/
693
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000694static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600695readline_set_auto_history_impl(PyObject *module,
696 int _should_auto_add_history)
697/*[clinic end generated code: output=619c6968246fd82b input=3d413073a1a03355]*/
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000698{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600699 should_auto_add_history = _should_auto_add_history;
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000700 Py_RETURN_NONE;
701}
702
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000703
Guido van Rossum74f31432003-01-07 20:01:29 +0000704/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000705
Zackery Spytzb7047e52020-07-12 10:01:03 -0600706/*[clinic input]
707readline.get_completer_delims
708
709Get the word delimiters for completion.
710[clinic start generated code]*/
711
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000712static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600713readline_get_completer_delims_impl(PyObject *module)
714/*[clinic end generated code: output=6b060280fa68ef43 input=e36eb14fb8a1f08a]*/
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000715{
Martin Panterf00c49d2016-06-14 01:16:16 +0000716 return decode(rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000717}
Guido van Rossum74f31432003-01-07 20:01:29 +0000718
Guido van Rossum74f31432003-01-07 20:01:29 +0000719/* Set the completer function */
720
Zackery Spytzb7047e52020-07-12 10:01:03 -0600721/*[clinic input]
722readline.set_completer
723
724 function: object = None
725 /
726
727Set or remove the completer function.
728
729The function is called as function(text, state),
730for state in 0, 1, 2, ..., until it returns a non-string.
731It should return the next possible completion starting with 'text'.
732[clinic start generated code]*/
733
Guido van Rossum290900a1997-09-26 21:51:21 +0000734static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600735readline_set_completer_impl(PyObject *module, PyObject *function)
736/*[clinic end generated code: output=171a2a60f81d3204 input=51e81e13118eb877]*/
Guido van Rossum290900a1997-09-26 21:51:21 +0000737{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600738 return set_hook("completer", &readlinestate_global->completer, function);
Guido van Rossum290900a1997-09-26 21:51:21 +0000739}
740
Zackery Spytzb7047e52020-07-12 10:01:03 -0600741/*[clinic input]
742readline.get_completer
Guido van Rossum290900a1997-09-26 21:51:21 +0000743
Zackery Spytzb7047e52020-07-12 10:01:03 -0600744Get the current completer function.
745[clinic start generated code]*/
Guido van Rossum74f31432003-01-07 20:01:29 +0000746
Michael W. Hudson796df152003-01-30 10:12:51 +0000747static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600748readline_get_completer_impl(PyObject *module)
749/*[clinic end generated code: output=6e6bbd8226d14475 input=6457522e56d70d13]*/
Michael W. Hudson796df152003-01-30 10:12:51 +0000750{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200751 if (readlinestate_global->completer == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 Py_RETURN_NONE;
753 }
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200754 Py_INCREF(readlinestate_global->completer);
755 return readlinestate_global->completer;
Michael W. Hudson796df152003-01-30 10:12:51 +0000756}
757
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000758/* Private function to get current length of history. XXX It may be
759 * possible to replace this with a direct use of history_length instead,
760 * but it's not clear whether BSD's libedit keeps history_length up to date.
761 * See issue #8065.*/
762
763static int
764_py_get_history_length(void)
765{
766 HISTORY_STATE *hist_st = history_get_history_state();
767 int length = hist_st->length;
768 /* the history docs don't say so, but the address of hist_st changes each
769 time history_get_history_state is called which makes me think it's
770 freshly malloc'd memory... on the other hand, the address of the last
771 line stays the same as long as history isn't extended, so it appears to
772 be malloc'd but managed by the history package... */
773 free(hist_st);
774 return length;
775}
776
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000777/* Exported function to get any element of history */
778
Zackery Spytzb7047e52020-07-12 10:01:03 -0600779/*[clinic input]
780readline.get_history_item
781
782 index as idx: int
783 /
784
785Return the current contents of history item at index.
786[clinic start generated code]*/
787
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000788static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600789readline_get_history_item_impl(PyObject *module, int idx)
790/*[clinic end generated code: output=83d3e53ea5f34b3d input=63fff0c3c4323269]*/
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000791{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 HIST_ENTRY *hist_ent;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 if (using_libedit_emulation) {
Ned Deilyf70f4a62013-09-06 15:16:19 -0700795 /* Older versions of libedit's readline emulation
796 * use 0-based indexes, while readline and newer
797 * versions of libedit use 1-based indexes.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 */
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000799 int length = _py_get_history_length();
Ned Deilyf70f4a62013-09-06 15:16:19 -0700800
801 idx = idx - 1 + libedit_history_start;
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 /*
804 * Apple's readline emulation crashes when
805 * the index is out of range, therefore
806 * test for that and fail gracefully.
807 */
Ned Deilyf70f4a62013-09-06 15:16:19 -0700808 if (idx < (0 + libedit_history_start)
809 || idx >= (length + libedit_history_start)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 Py_RETURN_NONE;
811 }
812 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 if ((hist_ent = history_get(idx)))
Martin Panterf00c49d2016-06-14 01:16:16 +0000814 return decode(hist_ent->line);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 else {
816 Py_RETURN_NONE;
817 }
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000818}
819
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000820/* Exported function to get current length of history */
821
Zackery Spytzb7047e52020-07-12 10:01:03 -0600822/*[clinic input]
823readline.get_current_history_length
824
825Return the current (not the maximum) length of history.
826[clinic start generated code]*/
827
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000828static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600829readline_get_current_history_length_impl(PyObject *module)
830/*[clinic end generated code: output=436b294f12ba1e3f input=9cb3f431a68d071f]*/
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000831{
Mark Dickinson6b54e1f2010-08-03 16:49:49 +0000832 return PyLong_FromLong((long)_py_get_history_length());
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000833}
834
Guido van Rossum79378ff1997-10-07 14:53:21 +0000835/* Exported function to read the current line buffer */
836
Zackery Spytzb7047e52020-07-12 10:01:03 -0600837/*[clinic input]
838readline.get_line_buffer
839
840Return the current contents of the line buffer.
841[clinic start generated code]*/
842
Guido van Rossum79378ff1997-10-07 14:53:21 +0000843static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600844readline_get_line_buffer_impl(PyObject *module)
845/*[clinic end generated code: output=d22f9025ecad80e4 input=5f5fbc0d12c69412]*/
Guido van Rossum79378ff1997-10-07 14:53:21 +0000846{
Martin Panterf00c49d2016-06-14 01:16:16 +0000847 return decode(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000848}
849
Martin v. Löwise7a97962003-09-20 16:08:33 +0000850#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
851
852/* Exported function to clear the current history */
853
Zackery Spytzb7047e52020-07-12 10:01:03 -0600854/*[clinic input]
855readline.clear_history
856
857Clear the current readline history.
858[clinic start generated code]*/
859
Martin v. Löwise7a97962003-09-20 16:08:33 +0000860static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600861readline_clear_history_impl(PyObject *module)
862/*[clinic end generated code: output=1f2dbb0dfa5d5ebb input=208962c4393f5d16]*/
Martin v. Löwise7a97962003-09-20 16:08:33 +0000863{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 clear_history();
865 Py_RETURN_NONE;
Martin v. Löwise7a97962003-09-20 16:08:33 +0000866}
Martin v. Löwise7a97962003-09-20 16:08:33 +0000867#endif
868
869
Guido van Rossum79378ff1997-10-07 14:53:21 +0000870/* Exported function to insert text into the line buffer */
871
Zackery Spytzb7047e52020-07-12 10:01:03 -0600872/*[clinic input]
873readline.insert_text
874
875 string: object
876 /
877
878Insert text into the line buffer at the cursor position.
879[clinic start generated code]*/
880
Guido van Rossum79378ff1997-10-07 14:53:21 +0000881static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600882readline_insert_text(PyObject *module, PyObject *string)
883/*[clinic end generated code: output=23d792821d320c19 input=bc96c3c848d5ccb5]*/
Guido van Rossum79378ff1997-10-07 14:53:21 +0000884{
Martin Panterf00c49d2016-06-14 01:16:16 +0000885 PyObject *encoded = encode(string);
886 if (encoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 return NULL;
Martin Panterf00c49d2016-06-14 01:16:16 +0000888 }
889 rl_insert_text(PyBytes_AS_STRING(encoded));
890 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 Py_RETURN_NONE;
Guido van Rossum79378ff1997-10-07 14:53:21 +0000892}
893
Guido van Rossum74f31432003-01-07 20:01:29 +0000894/* Redisplay the line buffer */
895
Zackery Spytzb7047e52020-07-12 10:01:03 -0600896/*[clinic input]
897readline.redisplay
898
899Change what's displayed on the screen to reflect contents of the line buffer.
900[clinic start generated code]*/
901
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000902static PyObject *
Zackery Spytzb7047e52020-07-12 10:01:03 -0600903readline_redisplay_impl(PyObject *module)
904/*[clinic end generated code: output=a8b9725827c3c34b input=b485151058d75edc]*/
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000905{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 rl_redisplay();
907 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000908}
909
Zackery Spytzb7047e52020-07-12 10:01:03 -0600910#include "clinic/readline.c.h"
Guido van Rossum74f31432003-01-07 20:01:29 +0000911
Guido van Rossum290900a1997-09-26 21:51:21 +0000912/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000913
914static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000915{
Zackery Spytzb7047e52020-07-12 10:01:03 -0600916 READLINE_PARSE_AND_BIND_METHODDEF
917 READLINE_GET_LINE_BUFFER_METHODDEF
918 READLINE_INSERT_TEXT_METHODDEF
919 READLINE_REDISPLAY_METHODDEF
920 READLINE_READ_INIT_FILE_METHODDEF
921 READLINE_READ_HISTORY_FILE_METHODDEF
922 READLINE_WRITE_HISTORY_FILE_METHODDEF
Benjamin Petersond1e22ba2014-11-26 14:35:12 -0600923#ifdef HAVE_RL_APPEND_HISTORY
Zackery Spytzb7047e52020-07-12 10:01:03 -0600924 READLINE_APPEND_HISTORY_FILE_METHODDEF
Ned Deily8007cbc2014-11-26 13:02:33 -0800925#endif
Zackery Spytzb7047e52020-07-12 10:01:03 -0600926 READLINE_GET_HISTORY_ITEM_METHODDEF
927 READLINE_GET_CURRENT_HISTORY_LENGTH_METHODDEF
928 READLINE_SET_HISTORY_LENGTH_METHODDEF
929 READLINE_GET_HISTORY_LENGTH_METHODDEF
930 READLINE_SET_COMPLETER_METHODDEF
931 READLINE_GET_COMPLETER_METHODDEF
932 READLINE_GET_COMPLETION_TYPE_METHODDEF
933 READLINE_GET_BEGIDX_METHODDEF
934 READLINE_GET_ENDIDX_METHODDEF
935 READLINE_SET_COMPLETER_DELIMS_METHODDEF
936 READLINE_SET_AUTO_HISTORY_METHODDEF
937 READLINE_ADD_HISTORY_METHODDEF
938 READLINE_REMOVE_HISTORY_ITEM_METHODDEF
939 READLINE_REPLACE_HISTORY_ITEM_METHODDEF
940 READLINE_GET_COMPLETER_DELIMS_METHODDEF
941 READLINE_SET_COMPLETION_DISPLAY_MATCHES_HOOK_METHODDEF
942 READLINE_SET_STARTUP_HOOK_METHODDEF
Martin v. Löwis0daad592001-09-30 21:09:59 +0000943#ifdef HAVE_RL_PRE_INPUT_HOOK
Zackery Spytzb7047e52020-07-12 10:01:03 -0600944 READLINE_SET_PRE_INPUT_HOOK_METHODDEF
Martin v. Löwis0daad592001-09-30 21:09:59 +0000945#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000946#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Zackery Spytzb7047e52020-07-12 10:01:03 -0600947 READLINE_CLEAR_HISTORY_METHODDEF
Martin v. Löwise7a97962003-09-20 16:08:33 +0000948#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000950};
951
Guido van Rossum05ac4492003-01-07 20:04:12 +0000952
Martin v. Löwis0daad592001-09-30 21:09:59 +0000953/* C function to call the Python hooks. */
954
955static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000956on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000957{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 int result = 0;
959 if (func != NULL) {
960 PyObject *r;
Victor Stinner2ff58a22019-06-17 14:27:23 +0200961 r = PyObject_CallNoArgs(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 if (r == NULL)
963 goto error;
964 if (r == Py_None)
965 result = 0;
966 else {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300967 result = _PyLong_AsInt(r);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 if (result == -1 && PyErr_Occurred())
969 goto error;
970 }
971 Py_DECREF(r);
972 goto done;
973 error:
974 PyErr_Clear();
975 Py_XDECREF(r);
976 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 return result;
978 }
979 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000980}
981
982static int
Ned Deily7b9ddea2014-02-05 16:53:10 -0800983#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000984on_startup_hook(void)
Ned Deily7b9ddea2014-02-05 16:53:10 -0800985#else
986on_startup_hook()
987#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000988{
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200989 int r;
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200990 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200991 r = on_hook(readlinestate_global->startup_hook);
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200992 PyGILState_Release(gilstate);
Antoine Pitrou5c30a752013-07-31 21:52:53 +0200993 return r;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000994}
995
996#ifdef HAVE_RL_PRE_INPUT_HOOK
997static int
Ned Deily7b9ddea2014-02-05 16:53:10 -0800998#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000999on_pre_input_hook(void)
Ned Deily7b9ddea2014-02-05 16:53:10 -08001000#else
1001on_pre_input_hook()
1002#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +00001003{
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001004 int r;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001005 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001006 r = on_hook(readlinestate_global->pre_input_hook);
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001007 PyGILState_Release(gilstate);
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001008 return r;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001009}
1010#endif
1011
Guido van Rossum05ac4492003-01-07 20:04:12 +00001012
Thomas Wouters89d996e2007-09-08 17:39:28 +00001013/* C function to call the Python completion_display_matches */
1014
Georg Brandl646fdd62010-10-18 07:27:55 +00001015#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Thomas Wouters89d996e2007-09-08 17:39:28 +00001016static void
1017on_completion_display_matches_hook(char **matches,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 int num_matches, int max_length)
Thomas Wouters89d996e2007-09-08 17:39:28 +00001019{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 int i;
Martin Panterf00c49d2016-06-14 01:16:16 +00001021 PyObject *sub, *m=NULL, *s=NULL, *r=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 m = PyList_New(num_matches);
1024 if (m == NULL)
1025 goto error;
1026 for (i = 0; i < num_matches; i++) {
Martin Panterf00c49d2016-06-14 01:16:16 +00001027 s = decode(matches[i+1]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 if (s == NULL)
1029 goto error;
Zackery Spytz99d56b52018-12-08 07:16:55 -07001030 PyList_SET_ITEM(m, i, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 }
Martin Panterf00c49d2016-06-14 01:16:16 +00001032 sub = decode(matches[0]);
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001033 r = PyObject_CallFunction(readlinestate_global->completion_display_matches_hook,
Martin Panterf00c49d2016-06-14 01:16:16 +00001034 "NNi", sub, m, max_length);
Christian Heimes32fbe592007-11-12 15:01:33 +00001035
Martin Panterf00c49d2016-06-14 01:16:16 +00001036 m=NULL;
Christian Heimes32fbe592007-11-12 15:01:33 +00001037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 if (r == NULL ||
1039 (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) {
1040 goto error;
1041 }
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001042 Py_CLEAR(r);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043
1044 if (0) {
1045 error:
1046 PyErr_Clear();
1047 Py_XDECREF(m);
1048 Py_XDECREF(r);
1049 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 PyGILState_Release(gilstate);
Thomas Wouters89d996e2007-09-08 17:39:28 +00001051}
1052
Senthil Kumaran95c07002010-11-04 03:51:05 +00001053#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +00001054
Martin Panter5dbbf1a2016-04-03 02:54:58 +00001055#ifdef HAVE_RL_RESIZE_TERMINAL
1056static volatile sig_atomic_t sigwinch_received;
Martin Panter28f35b22016-04-03 08:00:49 +00001057static PyOS_sighandler_t sigwinch_ohandler;
Martin Panter5dbbf1a2016-04-03 02:54:58 +00001058
1059static void
1060readline_sigwinch_handler(int signum)
1061{
1062 sigwinch_received = 1;
1063 if (sigwinch_ohandler &&
1064 sigwinch_ohandler != SIG_IGN && sigwinch_ohandler != SIG_DFL)
1065 sigwinch_ohandler(signum);
1066
1067#ifndef HAVE_SIGACTION
1068 /* If the handler was installed with signal() rather than sigaction(),
1069 we need to reinstall it. */
1070 PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
1071#endif
1072}
1073#endif
1074
Guido van Rossum290900a1997-09-26 21:51:21 +00001075/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +00001076
Guido van Rossum290900a1997-09-26 21:51:21 +00001077static char *
Guido van Rossumd8faa362007-04-27 19:54:29 +00001078on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +00001079{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 char *result = NULL;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001081 if (readlinestate_global->completer != NULL) {
Martin Panterf00c49d2016-06-14 01:16:16 +00001082 PyObject *r = NULL, *t;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 rl_attempted_completion_over = 1;
Martin Panterf00c49d2016-06-14 01:16:16 +00001085 t = decode(text);
1086 r = PyObject_CallFunction(readlinestate_global->completer, "Ni", t, state);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 if (r == NULL)
1088 goto error;
1089 if (r == Py_None) {
1090 result = NULL;
1091 }
1092 else {
Martin Panterf00c49d2016-06-14 01:16:16 +00001093 PyObject *encoded = encode(r);
1094 if (encoded == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 goto error;
Martin Panterf00c49d2016-06-14 01:16:16 +00001096 result = strdup(PyBytes_AS_STRING(encoded));
1097 Py_DECREF(encoded);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 }
1099 Py_DECREF(r);
1100 goto done;
1101 error:
1102 PyErr_Clear();
1103 Py_XDECREF(r);
1104 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 PyGILState_Release(gilstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 return result;
1107 }
1108 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +00001109}
1110
Guido van Rossum290900a1997-09-26 21:51:21 +00001111
Guido van Rossum6d0d3652003-01-07 20:34:19 +00001112/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001113 * before calling the normal completer */
1114
Neal Norwitzc355f0c2003-02-21 00:30:18 +00001115static char **
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001116flex_complete(const char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001117{
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001118 char **result;
Martin Panterf00c49d2016-06-14 01:16:16 +00001119 char saved;
1120 size_t start_size, end_size;
1121 wchar_t *s;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001122 PyGILState_STATE gilstate = PyGILState_Ensure();
Antoine Pitroudc0900b2009-10-19 18:22:37 +00001123#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 rl_completion_append_character ='\0';
Antoine Pitroud5131772009-10-26 19:22:14 +00001125#endif
1126#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 rl_completion_suppress_append = 0;
Antoine Pitroudc0900b2009-10-19 18:22:37 +00001128#endif
Martin Panterf00c49d2016-06-14 01:16:16 +00001129
1130 saved = rl_line_buffer[start];
1131 rl_line_buffer[start] = 0;
1132 s = Py_DecodeLocale(rl_line_buffer, &start_size);
1133 rl_line_buffer[start] = saved;
1134 if (s == NULL) {
1135 goto done;
1136 }
1137 PyMem_RawFree(s);
1138 saved = rl_line_buffer[end];
1139 rl_line_buffer[end] = 0;
1140 s = Py_DecodeLocale(rl_line_buffer + start, &end_size);
1141 rl_line_buffer[end] = saved;
1142 if (s == NULL) {
1143 goto done;
1144 }
1145 PyMem_RawFree(s);
1146 start = (int)start_size;
1147 end = start + (int)end_size;
1148
1149done:
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001150 Py_XDECREF(readlinestate_global->begidx);
1151 Py_XDECREF(readlinestate_global->endidx);
1152 readlinestate_global->begidx = PyLong_FromLong((long) start);
1153 readlinestate_global->endidx = PyLong_FromLong((long) end);
Benjamin Peterson1bb0c0b2016-09-05 18:26:19 -07001154 result = completion_matches((char *)text, *on_completion);
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001155 PyGILState_Release(gilstate);
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001156 return result;
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001157}
1158
Guido van Rossum05ac4492003-01-07 20:04:12 +00001159
Victor Stinner1d8da612019-10-30 16:39:27 +01001160/* Helper to initialize GNU readline properly.
1161 Return -1 on memory allocation failure, return 0 on success. */
1162static int
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001163setup_readline(readlinestate *mod_state)
Guido van Rossum290900a1997-09-26 21:51:21 +00001164{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +00001165#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Victor Stinner1d8da612019-10-30 16:39:27 +01001167 if (!saved_locale) {
1168 return -1;
1169 }
Guido van Rossum60c8a3a2002-10-09 21:27:33 +00001170#endif
1171
Zvezdan Petkovicc2f082e2018-05-17 02:45:10 -04001172 /* The name must be defined before initialization */
1173 rl_readline_name = "python";
1174
Victor Stinner6ced7c42011-03-21 18:15:42 +01001175 /* the libedit readline emulation resets key bindings etc
R. David Murray52d1b4e2010-12-18 03:48:32 +00001176 * when calling rl_initialize. So call it upfront
1177 */
1178 if (using_libedit_emulation)
1179 rl_initialize();
Ned Deilyf70f4a62013-09-06 15:16:19 -07001180
1181 /* Detect if libedit's readline emulation uses 0-based
1182 * indexing or 1-based indexing.
1183 */
1184 add_history("1");
1185 if (history_get(1) == NULL) {
1186 libedit_history_start = 0;
1187 } else {
1188 libedit_history_start = 1;
1189 }
1190 clear_history();
R. David Murray52d1b4e2010-12-18 03:48:32 +00001191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 using_history();
Skip Montanaroa0392742002-06-11 14:32:46 +00001193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 /* Force rebind of TAB to insert-tab */
1195 rl_bind_key('\t', rl_insert);
1196 /* Bind both ESC-TAB and ESC-ESC to the completion function */
1197 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
1198 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin Panter5dbbf1a2016-04-03 02:54:58 +00001199#ifdef HAVE_RL_RESIZE_TERMINAL
1200 /* Set up signal handler for window resize */
1201 sigwinch_ohandler = PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
1202#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 /* Set our hook functions */
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001204 rl_startup_hook = on_startup_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001205#ifdef HAVE_RL_PRE_INPUT_HOOK
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001206 rl_pre_input_hook = on_pre_input_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +00001207#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 /* Set our completion function */
Benjamin Petersonf0b463a2014-01-24 11:44:16 -05001209 rl_attempted_completion_function = flex_complete;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 /* Set Python word break characters */
Antoine Pitroua7f7deb2013-05-06 21:51:03 +02001211 completer_word_break_characters =
1212 rl_completer_word_break_characters =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
1214 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +00001215
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001216 mod_state->begidx = PyLong_FromLong(0L);
1217 mod_state->endidx = PyLong_FromLong(0L);
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001218
Martin Panterc427b8d2016-08-27 03:23:11 +00001219 if (!using_libedit_emulation)
Martin Panterc427b8d2016-08-27 03:23:11 +00001220 {
1221 if (!isatty(STDOUT_FILENO)) {
1222 /* Issue #19884: stdout is not a terminal. Disable meta modifier
1223 keys to not write the ANSI sequence "\033[1034h" into stdout. On
1224 terminals supporting 8 bit characters like TERM=xterm-256color
1225 (which is now the default Fedora since Fedora 18), the meta key is
1226 used to enable support of 8 bit characters (ANSI sequence
1227 "\033[1034h").
1228
1229 With libedit, this call makes readline() crash. */
1230 rl_variable_bind ("enable-meta-key", "off");
1231 }
1232 }
Victor Stinnera3c80ce2014-07-24 12:23:56 +02001233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 /* Initialize (allows .inputrc to override)
1235 *
1236 * XXX: A bug in the readline-2.2 library causes a memory leak
1237 * inside this function. Nothing we can do about it.
1238 */
R. David Murray52d1b4e2010-12-18 03:48:32 +00001239 if (using_libedit_emulation)
Victor Stinner6ced7c42011-03-21 18:15:42 +01001240 rl_read_init_file(NULL);
R. David Murray52d1b4e2010-12-18 03:48:32 +00001241 else
R. David Murray52d1b4e2010-12-18 03:48:32 +00001242 rl_initialize();
Victor Stinner6ced7c42011-03-21 18:15:42 +01001243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 RESTORE_LOCALE(saved_locale)
Victor Stinner1d8da612019-10-30 16:39:27 +01001245 return 0;
Guido van Rossum290900a1997-09-26 21:51:21 +00001246}
1247
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001248/* Wrapper around GNU readline that handles signals differently. */
1249
Antoine Pitrouf474c5a2017-07-18 17:05:03 +02001250static char *completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001251static void
1252rlhandler(char *text)
1253{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 completed_input_string = text;
1255 rl_callback_handler_remove();
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001256}
1257
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001258static char *
Serhiy Storchakac6792272013-10-19 21:03:34 +03001259readline_until_enter_or_signal(const char *prompt, int *signal)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 char * not_done_reading = "";
1262 fd_set selectset;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 *signal = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001265#ifdef HAVE_RL_CATCH_SIGNAL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 rl_catch_signals = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001267#endif
1268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 rl_callback_handler_install (prompt, rlhandler);
1270 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 completed_input_string = not_done_reading;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 while (completed_input_string == not_done_reading) {
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001275 int has_input = 0, err = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 while (!has_input)
1278 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Michael W. Hudson8da2b012004-10-07 13:46:33 +00001279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 /* [Bug #1552726] Only limit the pause if an input hook has been
1281 defined. */
1282 struct timeval *timeoutp = NULL;
1283 if (PyOS_InputHook)
1284 timeoutp = &timeout;
Martin Panter5dbbf1a2016-04-03 02:54:58 +00001285#ifdef HAVE_RL_RESIZE_TERMINAL
1286 /* Update readline's view of the window size after SIGWINCH */
1287 if (sigwinch_received) {
1288 sigwinch_received = 0;
1289 rl_resize_terminal();
1290 }
1291#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 FD_SET(fileno(rl_instream), &selectset);
1293 /* select resets selectset if no input was available */
1294 has_input = select(fileno(rl_instream) + 1, &selectset,
1295 NULL, NULL, timeoutp);
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001296 err = errno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 if(PyOS_InputHook) PyOS_InputHook();
1298 }
1299
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001300 if (has_input > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 rl_callback_read_char();
1302 }
Antoine Pitrouc345ce12011-12-16 12:28:32 +01001303 else if (err == EINTR) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 int s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 PyEval_RestoreThread(_PyOS_ReadlineTState);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 s = PyErr_CheckSignals();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 PyEval_SaveThread();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 if (s < 0) {
1309 rl_free_line_state();
Martin Panterd6990d22016-03-22 07:24:05 +00001310#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0700
1311 rl_callback_sigcleanup();
1312#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 rl_cleanup_after_signal();
1314 rl_callback_handler_remove();
1315 *signal = 1;
1316 completed_input_string = NULL;
1317 }
1318 }
1319 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 return completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001322}
1323
1324
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001325static char *
Serhiy Storchakac6792272013-10-19 21:03:34 +03001326call_readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 size_t n;
Victor Stinner1600f602018-11-30 15:03:53 +01001329 char *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 int signal;
Neal Norwitz1fa040b2004-08-25 01:20:18 +00001331
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001332#ifdef SAVE_LOCALE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1334 if (!saved_locale)
1335 Py_FatalError("not enough memory to save locale");
xdegaye1588be62017-11-12 12:45:59 +01001336 _Py_SetLocaleFromEnv(LC_CTYPE);
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001337#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
1340 rl_instream = sys_stdin;
1341 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001342#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001344#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 }
Guido van Rossum74f31432003-01-07 20:01:29 +00001346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 p = readline_until_enter_or_signal(prompt, &signal);
Guido van Rossumb18618d2000-05-03 23:44:39 +00001348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 /* we got an interrupt signal */
1350 if (signal) {
1351 RESTORE_LOCALE(saved_locale)
1352 return NULL;
1353 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001354
Martin Panter7462b6492015-11-02 03:37:02 +00001355 /* We got an EOF, return an empty string. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 if (p == NULL) {
Victor Stinner2fe9bac2013-10-10 16:18:20 +02001357 p = PyMem_RawMalloc(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 if (p != NULL)
1359 *p = '\0';
1360 RESTORE_LOCALE(saved_locale)
1361 return p;
1362 }
1363
1364 /* we have a valid line */
1365 n = strlen(p);
Martin Panterf0dbf7a2016-05-15 01:26:25 +00001366 if (should_auto_add_history && n > 0) {
Brett Cannon2525dc82010-08-22 20:36:25 +00001367 const char *line;
Mark Dickinson6b54e1f2010-08-03 16:49:49 +00001368 int length = _py_get_history_length();
Nir Sofferfae8f4a2017-07-07 09:10:46 +03001369 if (length > 0) {
1370 HIST_ENTRY *hist_ent;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 if (using_libedit_emulation) {
Ned Deilyf70f4a62013-09-06 15:16:19 -07001372 /* handle older 0-based or newer 1-based indexing */
Nir Sofferfae8f4a2017-07-07 09:10:46 +03001373 hist_ent = history_get(length + libedit_history_start - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 } else
Nir Sofferfae8f4a2017-07-07 09:10:46 +03001375 hist_ent = history_get(length);
1376 line = hist_ent ? hist_ent->line : "";
1377 } else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 line = "";
1379 if (strcmp(p, line))
1380 add_history(p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 }
1382 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
1383 release the original. */
Victor Stinner1600f602018-11-30 15:03:53 +01001384 char *q = p;
Victor Stinner2fe9bac2013-10-10 16:18:20 +02001385 p = PyMem_RawMalloc(n+2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 if (p != NULL) {
Victor Stinner1600f602018-11-30 15:03:53 +01001387 memcpy(p, q, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 p[n] = '\n';
1389 p[n+1] = '\0';
1390 }
1391 free(q);
1392 RESTORE_LOCALE(saved_locale)
1393 return p;
Guido van Rossum0969d361997-08-05 21:27:50 +00001394}
1395
Guido van Rossum290900a1997-09-26 21:51:21 +00001396
1397/* Initialize the module */
1398
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001399PyDoc_STRVAR(doc_module,
1400"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001401
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001402PyDoc_STRVAR(doc_module_le,
1403"Importing this module enables command line editing using libedit readline.");
Martin v. Löwis1a214512008-06-11 05:26:20 +00001404
1405static struct PyModuleDef readlinemodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 PyModuleDef_HEAD_INIT,
1407 "readline",
1408 doc_module,
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001409 sizeof(readlinestate),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 readline_methods,
1411 NULL,
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001412 readline_traverse,
1413 readline_clear,
1414 readline_free
Martin v. Löwis1a214512008-06-11 05:26:20 +00001415};
1416
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001417
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001418PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001419PyInit_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001420{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 PyObject *m;
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001422 readlinestate *mod_state;
Guido van Rossum290900a1997-09-26 21:51:21 +00001423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
1425 using_libedit_emulation = 1;
1426 }
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 if (using_libedit_emulation)
1429 readlinemodule.m_doc = doc_module_le;
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001430
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 m = PyModule_Create(&readlinemodule);
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 if (m == NULL)
1435 return NULL;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001436
Victor Stinner0efc0242017-11-30 17:21:07 +01001437 if (PyModule_AddIntConstant(m, "_READLINE_VERSION",
1438 RL_READLINE_VERSION) < 0) {
1439 goto error;
1440 }
1441 if (PyModule_AddIntConstant(m, "_READLINE_RUNTIME_VERSION",
1442 rl_readline_version) < 0) {
1443 goto error;
1444 }
1445 if (PyModule_AddStringConstant(m, "_READLINE_LIBRARY_VERSION",
1446 rl_library_version) < 0)
1447 {
1448 goto error;
1449 }
1450
Antoine Pitrou5c30a752013-07-31 21:52:53 +02001451 mod_state = (readlinestate *) PyModule_GetState(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 PyOS_ReadlineFunctionPointer = call_readline;
Victor Stinner1d8da612019-10-30 16:39:27 +01001453 if (setup_readline(mod_state) < 0) {
1454 PyErr_NoMemory();
1455 goto error;
1456 }
Antoine Pitrou7e8b8672014-11-04 14:52:10 +01001457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 return m;
Victor Stinner0efc0242017-11-30 17:21:07 +01001459
1460error:
1461 Py_DECREF(m);
1462 return NULL;
Guido van Rossum0969d361997-08-05 21:27:50 +00001463}