Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 1 | /* This module makes GNU readline available to Python. It has ideas |
| 2 | * contributed by Lee Busby, LLNL, and William Magro, Cornell Theory |
Michael W. Hudson | 9a8c314 | 2005-03-30 10:09:12 +0000 | [diff] [blame] | 3 | * Center. The completer interface was inspired by Lele Gaifax. More |
| 4 | * recently, it was largely rewritten by Guido van Rossum. |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 7 | /* Standard definitions */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 8 | #include "Python.h" |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 9 | #include <stddef.h> |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 10 | #include <signal.h> |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 11 | #include <errno.h> |
Michael W. Hudson | 8da2b01 | 2004-10-07 13:46:33 +0000 | [diff] [blame] | 12 | #include <sys/time.h> |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 13 | |
Skip Montanaro | 7befb99 | 2004-02-10 16:50:21 +0000 | [diff] [blame] | 14 | #if defined(HAVE_SETLOCALE) |
Guido van Rossum | 60c8a3a | 2002-10-09 21:27:33 +0000 | [diff] [blame] | 15 | /* 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 Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 23 | #ifdef SAVE_LOCALE |
| 24 | # define RESTORE_LOCALE(sl) { setlocale(LC_CTYPE, sl); free(sl); } |
| 25 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 26 | # define RESTORE_LOCALE(sl) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 27 | #endif |
| 28 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 29 | /* GNU readline definitions */ |
Guido van Rossum | b0e51b2 | 2001-04-13 18:14:27 +0000 | [diff] [blame] | 30 | #undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */ |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 31 | #include <readline/readline.h> |
| 32 | #include <readline/history.h> |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | 353ae58 | 2001-07-10 16:45:32 +0000 | [diff] [blame] | 34 | #ifdef HAVE_RL_COMPLETION_MATCHES |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 35 | #define completion_matches(x, y) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 36 | rl_completion_matches((x), ((rl_compentry_func_t *)(y))) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 37 | #else |
Martin v. Löwis | b37509b | 2008-11-04 20:45:29 +0000 | [diff] [blame] | 38 | #if defined(_RL_FUNCTION_TYPEDEF) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 39 | extern char **completion_matches(char *, rl_compentry_func_t *); |
Martin v. Löwis | b37509b | 2008-11-04 20:45:29 +0000 | [diff] [blame] | 40 | #else |
Ronald Oussoren | 25696bb | 2010-02-11 13:15:00 +0000 | [diff] [blame] | 41 | |
| 42 | #if !defined(__APPLE__) |
Martin v. Löwis | b37509b | 2008-11-04 20:45:29 +0000 | [diff] [blame] | 43 | extern char **completion_matches(char *, CPFunction *); |
| 44 | #endif |
Guido van Rossum | 353ae58 | 2001-07-10 16:45:32 +0000 | [diff] [blame] | 45 | #endif |
Ronald Oussoren | 25696bb | 2010-02-11 13:15:00 +0000 | [diff] [blame] | 46 | #endif |
Guido van Rossum | 353ae58 | 2001-07-10 16:45:32 +0000 | [diff] [blame] | 47 | |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 48 | /* |
| 49 | * It is possible to link the readline module to the readline |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 50 | * emulation library of editline/libedit. |
| 51 | * |
serge-sans-paille | 7105319 | 2019-12-04 17:02:57 +0100 | [diff] [blame] | 52 | * 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 Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 56 | * |
Ned Deily | 5d4121a | 2013-10-12 15:47:58 -0700 | [diff] [blame] | 57 | * Currently there is one known API incompatibility: |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 58 | * - 'get_history' has a 1-based index with GNU readline, and a 0-based |
Ned Deily | 5d4121a | 2013-10-12 15:47:58 -0700 | [diff] [blame] | 59 | * index with older versions of libedit's emulation. |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 60 | * - Note that replace_history and remove_history use a 0-based index |
Ned Deily | 5d4121a | 2013-10-12 15:47:58 -0700 | [diff] [blame] | 61 | * with both implementations. |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 62 | */ |
| 63 | static int using_libedit_emulation = 0; |
| 64 | static const char libedit_version_tag[] = "EditLine wrapper"; |
Ned Deily | f70f4a6 | 2013-09-06 15:16:19 -0700 | [diff] [blame] | 65 | |
| 66 | static int libedit_history_start = 0; |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 67 | |
Georg Brandl | 646fdd6 | 2010-10-18 07:27:55 +0000 | [diff] [blame] | 68 | #ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK |
Christian Heimes | 32fbe59 | 2007-11-12 15:01:33 +0000 | [diff] [blame] | 69 | static void |
| 70 | on_completion_display_matches_hook(char **matches, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 71 | int num_matches, int max_length); |
Georg Brandl | 646fdd6 | 2010-10-18 07:27:55 +0000 | [diff] [blame] | 72 | #endif |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 73 | |
Antoine Pitrou | a7f7deb | 2013-05-06 21:51:03 +0200 | [diff] [blame] | 74 | /* Memory allocated for rl_completer_word_break_characters |
| 75 | (see issue #17289 for the motivation). */ |
| 76 | static char *completer_word_break_characters; |
| 77 | |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 78 | typedef struct { |
Martin Panter | f6e9f47 | 2016-03-22 02:19:29 +0000 | [diff] [blame] | 79 | /* Specify hook functions in Python */ |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 80 | PyObject *completion_display_matches_hook; |
| 81 | PyObject *startup_hook; |
| 82 | PyObject *pre_input_hook; |
Martin Panter | f6e9f47 | 2016-03-22 02:19:29 +0000 | [diff] [blame] | 83 | |
| 84 | PyObject *completer; /* Specify a word completer in Python */ |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 85 | PyObject *begidx; |
| 86 | PyObject *endidx; |
| 87 | } readlinestate; |
| 88 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 89 | static inline readlinestate* |
| 90 | get_readline_state(PyModule *module) |
| 91 | { |
| 92 | void *state = PyModule_GetState(module); |
| 93 | assert(state != NULL); |
| 94 | return (readlinestate *)state; |
| 95 | } |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 96 | |
| 97 | static int |
| 98 | readline_clear(PyObject *m) |
| 99 | { |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 100 | readlinestate *state = get_readline_state(m); |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 101 | Py_CLEAR(state->completion_display_matches_hook); |
| 102 | Py_CLEAR(state->startup_hook); |
| 103 | Py_CLEAR(state->pre_input_hook); |
| 104 | Py_CLEAR(state->completer); |
| 105 | Py_CLEAR(state->begidx); |
| 106 | Py_CLEAR(state->endidx); |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static int |
| 111 | readline_traverse(PyObject *m, visitproc visit, void *arg) |
| 112 | { |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 113 | readlinestate *state = get_readline_state(m); |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 114 | Py_VISIT(state->completion_display_matches_hook); |
| 115 | Py_VISIT(state->startup_hook); |
| 116 | Py_VISIT(state->pre_input_hook); |
| 117 | Py_VISIT(state->completer); |
| 118 | Py_VISIT(state->begidx); |
| 119 | Py_VISIT(state->endidx); |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static void |
| 124 | readline_free(void *m) |
| 125 | { |
| 126 | readline_clear((PyObject *)m); |
| 127 | } |
| 128 | |
| 129 | static PyModuleDef readlinemodule; |
| 130 | |
| 131 | #define readlinestate_global ((readlinestate *)PyModule_GetState(PyState_FindModule(&readlinemodule))) |
| 132 | |
| 133 | |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 134 | /* Convert to/from multibyte C strings */ |
| 135 | |
| 136 | static PyObject * |
| 137 | encode(PyObject *b) |
| 138 | { |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 139 | return PyUnicode_EncodeLocale(b, "surrogateescape"); |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static PyObject * |
| 143 | decode(const char *s) |
| 144 | { |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 145 | return PyUnicode_DecodeLocale(s, "surrogateescape"); |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 149 | /* Exported function to send one line to readline's init file parser */ |
| 150 | |
| 151 | static PyObject * |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 152 | parse_and_bind(PyObject *self, PyObject *string) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 153 | { |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 154 | char *copy; |
| 155 | PyObject *encoded = encode(string); |
| 156 | if (encoded == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 157 | return NULL; |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 158 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 159 | /* Make a copy -- rl_parse_and_bind() modifies its argument */ |
| 160 | /* Bernard Herzog */ |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 161 | copy = PyMem_Malloc(1 + PyBytes_GET_SIZE(encoded)); |
| 162 | if (copy == NULL) { |
| 163 | Py_DECREF(encoded); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 164 | return PyErr_NoMemory(); |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 165 | } |
| 166 | strcpy(copy, PyBytes_AS_STRING(encoded)); |
| 167 | Py_DECREF(encoded); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 168 | rl_parse_and_bind(copy); |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 169 | PyMem_Free(copy); /* Free the copy */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 170 | Py_RETURN_NONE; |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 173 | PyDoc_STRVAR(doc_parse_and_bind, |
| 174 | "parse_and_bind(string) -> None\n\ |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 175 | Execute the init line provided in the string argument."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 176 | |
| 177 | |
| 178 | /* Exported function to parse a readline init file */ |
| 179 | |
| 180 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 181 | read_init_file(PyObject *self, PyObject *args) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 182 | { |
Victor Stinner | 19e65a3 | 2010-06-11 22:27:14 +0000 | [diff] [blame] | 183 | PyObject *filename_obj = Py_None, *filename_bytes; |
| 184 | if (!PyArg_ParseTuple(args, "|O:read_init_file", &filename_obj)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 185 | return NULL; |
Victor Stinner | 19e65a3 | 2010-06-11 22:27:14 +0000 | [diff] [blame] | 186 | if (filename_obj != Py_None) { |
| 187 | if (!PyUnicode_FSConverter(filename_obj, &filename_bytes)) |
| 188 | return NULL; |
| 189 | errno = rl_read_init_file(PyBytes_AsString(filename_bytes)); |
| 190 | Py_DECREF(filename_bytes); |
| 191 | } else |
| 192 | errno = rl_read_init_file(NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 193 | if (errno) |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 194 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 195 | Py_RETURN_NONE; |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 198 | PyDoc_STRVAR(doc_read_init_file, |
| 199 | "read_init_file([filename]) -> None\n\ |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 200 | Execute a readline initialization file.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 201 | The default filename is the last filename used."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 202 | |
| 203 | |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 204 | /* Exported function to load a readline history file */ |
| 205 | |
| 206 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 207 | read_history_file(PyObject *self, PyObject *args) |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 208 | { |
Victor Stinner | 19e65a3 | 2010-06-11 22:27:14 +0000 | [diff] [blame] | 209 | PyObject *filename_obj = Py_None, *filename_bytes; |
| 210 | if (!PyArg_ParseTuple(args, "|O:read_history_file", &filename_obj)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 211 | return NULL; |
Victor Stinner | 19e65a3 | 2010-06-11 22:27:14 +0000 | [diff] [blame] | 212 | if (filename_obj != Py_None) { |
| 213 | if (!PyUnicode_FSConverter(filename_obj, &filename_bytes)) |
| 214 | return NULL; |
| 215 | errno = read_history(PyBytes_AsString(filename_bytes)); |
| 216 | Py_DECREF(filename_bytes); |
| 217 | } else |
| 218 | errno = read_history(NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 219 | if (errno) |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 220 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 221 | Py_RETURN_NONE; |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Hye-Shik Chang | 7a8173a | 2004-11-25 04:04:20 +0000 | [diff] [blame] | 224 | static int _history_length = -1; /* do not truncate history by default */ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 225 | PyDoc_STRVAR(doc_read_history_file, |
| 226 | "read_history_file([filename]) -> None\n\ |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 227 | Load a readline history file.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 228 | The default filename is ~/.history."); |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 229 | |
| 230 | |
| 231 | /* Exported function to save a readline history file */ |
| 232 | |
| 233 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 234 | write_history_file(PyObject *self, PyObject *args) |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 235 | { |
Victor Stinner | 19e65a3 | 2010-06-11 22:27:14 +0000 | [diff] [blame] | 236 | PyObject *filename_obj = Py_None, *filename_bytes; |
| 237 | char *filename; |
Antoine Pitrou | c345ce1 | 2011-12-16 12:28:32 +0100 | [diff] [blame] | 238 | int err; |
Victor Stinner | 19e65a3 | 2010-06-11 22:27:14 +0000 | [diff] [blame] | 239 | if (!PyArg_ParseTuple(args, "|O:write_history_file", &filename_obj)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 240 | return NULL; |
Victor Stinner | 19e65a3 | 2010-06-11 22:27:14 +0000 | [diff] [blame] | 241 | if (filename_obj != Py_None) { |
| 242 | if (!PyUnicode_FSConverter(filename_obj, &filename_bytes)) |
| 243 | return NULL; |
| 244 | filename = PyBytes_AsString(filename_bytes); |
| 245 | } else { |
| 246 | filename_bytes = NULL; |
| 247 | filename = NULL; |
| 248 | } |
Antoine Pitrou | c345ce1 | 2011-12-16 12:28:32 +0100 | [diff] [blame] | 249 | errno = err = write_history(filename); |
| 250 | if (!err && _history_length >= 0) |
Victor Stinner | 19e65a3 | 2010-06-11 22:27:14 +0000 | [diff] [blame] | 251 | history_truncate_file(filename, _history_length); |
| 252 | Py_XDECREF(filename_bytes); |
Antoine Pitrou | c345ce1 | 2011-12-16 12:28:32 +0100 | [diff] [blame] | 253 | errno = err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 254 | if (errno) |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 255 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 256 | Py_RETURN_NONE; |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 259 | PyDoc_STRVAR(doc_write_history_file, |
| 260 | "write_history_file([filename]) -> None\n\ |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 261 | Save a readline history file.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 262 | The default filename is ~/.history."); |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 263 | |
| 264 | |
Benjamin Peterson | d1e22ba | 2014-11-26 14:35:12 -0600 | [diff] [blame] | 265 | #ifdef HAVE_RL_APPEND_HISTORY |
Benjamin Peterson | 33f8f15 | 2014-11-26 13:58:16 -0600 | [diff] [blame] | 266 | /* Exported function to save part of a readline history file */ |
| 267 | |
| 268 | static PyObject * |
| 269 | append_history_file(PyObject *self, PyObject *args) |
| 270 | { |
| 271 | int nelements; |
| 272 | PyObject *filename_obj = Py_None, *filename_bytes; |
| 273 | char *filename; |
| 274 | int err; |
| 275 | if (!PyArg_ParseTuple(args, "i|O:append_history_file", &nelements, &filename_obj)) |
| 276 | return NULL; |
| 277 | if (filename_obj != Py_None) { |
| 278 | if (!PyUnicode_FSConverter(filename_obj, &filename_bytes)) |
| 279 | return NULL; |
| 280 | filename = PyBytes_AsString(filename_bytes); |
| 281 | } else { |
| 282 | filename_bytes = NULL; |
| 283 | filename = NULL; |
| 284 | } |
| 285 | errno = err = append_history(nelements, filename); |
| 286 | if (!err && _history_length >= 0) |
| 287 | history_truncate_file(filename, _history_length); |
| 288 | Py_XDECREF(filename_bytes); |
| 289 | errno = err; |
| 290 | if (errno) |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 291 | return PyErr_SetFromErrno(PyExc_OSError); |
Benjamin Peterson | 33f8f15 | 2014-11-26 13:58:16 -0600 | [diff] [blame] | 292 | Py_RETURN_NONE; |
| 293 | } |
| 294 | |
| 295 | PyDoc_STRVAR(doc_append_history_file, |
| 296 | "append_history_file(nelements[, filename]) -> None\n\ |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 297 | Append the last nelements items of the history list to file.\n\ |
Benjamin Peterson | 33f8f15 | 2014-11-26 13:58:16 -0600 | [diff] [blame] | 298 | The default filename is ~/.history."); |
Benjamin Peterson | d1e22ba | 2014-11-26 14:35:12 -0600 | [diff] [blame] | 299 | #endif |
Benjamin Peterson | 33f8f15 | 2014-11-26 13:58:16 -0600 | [diff] [blame] | 300 | |
| 301 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 302 | /* Set history length */ |
| 303 | |
| 304 | static PyObject* |
| 305 | set_history_length(PyObject *self, PyObject *args) |
| 306 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 307 | int length = _history_length; |
| 308 | if (!PyArg_ParseTuple(args, "i:set_history_length", &length)) |
| 309 | return NULL; |
| 310 | _history_length = length; |
| 311 | Py_RETURN_NONE; |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 314 | PyDoc_STRVAR(set_history_length_doc, |
| 315 | "set_history_length(length) -> None\n\ |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 316 | set the maximal number of lines which will be written to\n\ |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 317 | the history file. A negative length is used to inhibit\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 318 | history truncation."); |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 319 | |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 320 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 321 | /* Get history length */ |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 322 | |
| 323 | static PyObject* |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 324 | get_history_length(PyObject *self, PyObject *noarg) |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 325 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 326 | return PyLong_FromLong(_history_length); |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 329 | PyDoc_STRVAR(get_history_length_doc, |
| 330 | "get_history_length() -> int\n\ |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 331 | return the maximum number of lines that will be written to\n\ |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 332 | the history file."); |
| 333 | |
| 334 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 335 | /* Generic hook function setter */ |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 336 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 337 | static PyObject * |
Michael W. Hudson | da6242c | 2005-03-30 11:21:53 +0000 | [diff] [blame] | 338 | set_hook(const char *funcname, PyObject **hook_var, PyObject *args) |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 339 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 340 | PyObject *function = Py_None; |
| 341 | char buf[80]; |
| 342 | PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname); |
| 343 | if (!PyArg_ParseTuple(args, buf, &function)) |
| 344 | return NULL; |
| 345 | if (function == Py_None) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 346 | Py_CLEAR(*hook_var); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 347 | } |
| 348 | else if (PyCallable_Check(function)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 349 | Py_INCREF(function); |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 350 | Py_XSETREF(*hook_var, function); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 351 | } |
| 352 | else { |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 353 | PyErr_Format(PyExc_TypeError, |
| 354 | "set_%.50s(func): argument not callable", |
| 355 | funcname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 356 | return NULL; |
| 357 | } |
| 358 | Py_RETURN_NONE; |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 361 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 362 | static PyObject * |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 363 | set_completion_display_matches_hook(PyObject *self, PyObject *args) |
| 364 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 365 | PyObject *result = set_hook("completion_display_matches_hook", |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 366 | &readlinestate_global->completion_display_matches_hook, args); |
Christian Heimes | 32fbe59 | 2007-11-12 15:01:33 +0000 | [diff] [blame] | 367 | #ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 368 | /* We cannot set this hook globally, since it replaces the |
| 369 | default completion display. */ |
| 370 | rl_completion_display_matches_hook = |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 371 | readlinestate_global->completion_display_matches_hook ? |
Martin v. Löwis | b37509b | 2008-11-04 20:45:29 +0000 | [diff] [blame] | 372 | #if defined(_RL_FUNCTION_TYPEDEF) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 373 | (rl_compdisp_func_t *)on_completion_display_matches_hook : 0; |
Martin v. Löwis | b37509b | 2008-11-04 20:45:29 +0000 | [diff] [blame] | 374 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 375 | (VFunction *)on_completion_display_matches_hook : 0; |
Martin v. Löwis | b37509b | 2008-11-04 20:45:29 +0000 | [diff] [blame] | 376 | #endif |
Christian Heimes | 32fbe59 | 2007-11-12 15:01:33 +0000 | [diff] [blame] | 377 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 378 | return result; |
Christian Heimes | 32fbe59 | 2007-11-12 15:01:33 +0000 | [diff] [blame] | 379 | |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | PyDoc_STRVAR(doc_set_completion_display_matches_hook, |
| 383 | "set_completion_display_matches_hook([function]) -> None\n\ |
| 384 | Set or remove the completion display function.\n\ |
| 385 | The function is called as\n\ |
| 386 | function(substitution, [matches], longest_match_length)\n\ |
| 387 | once each time matches need to be displayed."); |
| 388 | |
| 389 | static PyObject * |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 390 | set_startup_hook(PyObject *self, PyObject *args) |
| 391 | { |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 392 | return set_hook("startup_hook", &readlinestate_global->startup_hook, args); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 395 | PyDoc_STRVAR(doc_set_startup_hook, |
| 396 | "set_startup_hook([function]) -> None\n\ |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 397 | Set or remove the function invoked by the rl_startup_hook callback.\n\ |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 398 | The function is called with no arguments just\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 399 | before readline prints the first prompt."); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 400 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 401 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 402 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 403 | |
| 404 | /* Set pre-input hook */ |
| 405 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 406 | static PyObject * |
| 407 | set_pre_input_hook(PyObject *self, PyObject *args) |
| 408 | { |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 409 | return set_hook("pre_input_hook", &readlinestate_global->pre_input_hook, args); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 412 | PyDoc_STRVAR(doc_set_pre_input_hook, |
| 413 | "set_pre_input_hook([function]) -> None\n\ |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 414 | Set or remove the function invoked by the rl_pre_input_hook callback.\n\ |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 415 | The function is called with no arguments after the first prompt\n\ |
| 416 | has been printed and just before readline starts reading input\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 417 | characters."); |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 418 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 419 | #endif |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 420 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 421 | |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 422 | /* Get the completion type for the scope of the tab-completion */ |
| 423 | static PyObject * |
| 424 | get_completion_type(PyObject *self, PyObject *noarg) |
| 425 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 426 | return PyLong_FromLong(rl_completion_type); |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | PyDoc_STRVAR(doc_get_completion_type, |
| 430 | "get_completion_type() -> int\n\ |
| 431 | Get the type of completion being attempted."); |
| 432 | |
| 433 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 434 | /* Get the beginning index for the scope of the tab-completion */ |
| 435 | |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 436 | static PyObject * |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 437 | get_begidx(PyObject *self, PyObject *noarg) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 438 | { |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 439 | Py_INCREF(readlinestate_global->begidx); |
| 440 | return readlinestate_global->begidx; |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 443 | PyDoc_STRVAR(doc_get_begidx, |
| 444 | "get_begidx() -> int\n\ |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 445 | get the beginning index of the completion scope"); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 446 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 447 | |
| 448 | /* Get the ending index for the scope of the tab-completion */ |
| 449 | |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 450 | static PyObject * |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 451 | get_endidx(PyObject *self, PyObject *noarg) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 452 | { |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 453 | Py_INCREF(readlinestate_global->endidx); |
| 454 | return readlinestate_global->endidx; |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 457 | PyDoc_STRVAR(doc_get_endidx, |
| 458 | "get_endidx() -> int\n\ |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 459 | get the ending index of the completion scope"); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 460 | |
| 461 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 462 | /* Set the tab-completion word-delimiters that readline uses */ |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 463 | |
| 464 | static PyObject * |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 465 | set_completer_delims(PyObject *self, PyObject *string) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 466 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 467 | char *break_chars; |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 468 | PyObject *encoded = encode(string); |
| 469 | if (encoded == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 470 | return NULL; |
| 471 | } |
Antoine Pitrou | a7f7deb | 2013-05-06 21:51:03 +0200 | [diff] [blame] | 472 | /* Keep a reference to the allocated memory in the module state in case |
| 473 | some other module modifies rl_completer_word_break_characters |
| 474 | (see issue #17289). */ |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 475 | break_chars = strdup(PyBytes_AS_STRING(encoded)); |
| 476 | Py_DECREF(encoded); |
Serhiy Storchaka | 1138439 | 2015-09-27 22:34:59 +0300 | [diff] [blame] | 477 | if (break_chars) { |
| 478 | free(completer_word_break_characters); |
| 479 | completer_word_break_characters = break_chars; |
| 480 | rl_completer_word_break_characters = break_chars; |
Antoine Pitrou | a7f7deb | 2013-05-06 21:51:03 +0200 | [diff] [blame] | 481 | Py_RETURN_NONE; |
| 482 | } |
| 483 | else |
| 484 | return PyErr_NoMemory(); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 487 | PyDoc_STRVAR(doc_set_completer_delims, |
| 488 | "set_completer_delims(string) -> None\n\ |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 489 | set the word delimiters for completion"); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 490 | |
Mark Dickinson | 29b238e | 2010-08-03 16:08:16 +0000 | [diff] [blame] | 491 | /* _py_free_history_entry: Utility function to free a history entry. */ |
| 492 | |
| 493 | #if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500 |
| 494 | |
| 495 | /* Readline version >= 5.0 introduced a timestamp field into the history entry |
| 496 | structure; this needs to be freed to avoid a memory leak. This version of |
| 497 | readline also introduced the handy 'free_history_entry' function, which |
| 498 | takes care of the timestamp. */ |
| 499 | |
| 500 | static void |
| 501 | _py_free_history_entry(HIST_ENTRY *entry) |
| 502 | { |
| 503 | histdata_t data = free_history_entry(entry); |
| 504 | free(data); |
| 505 | } |
| 506 | |
| 507 | #else |
| 508 | |
| 509 | /* No free_history_entry function; free everything manually. */ |
| 510 | |
| 511 | static void |
| 512 | _py_free_history_entry(HIST_ENTRY *entry) |
| 513 | { |
| 514 | if (entry->line) |
| 515 | free((void *)entry->line); |
| 516 | if (entry->data) |
| 517 | free(entry->data); |
| 518 | free(entry); |
| 519 | } |
| 520 | |
| 521 | #endif |
| 522 | |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 523 | static PyObject * |
| 524 | py_remove_history(PyObject *self, PyObject *args) |
| 525 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 526 | int entry_number; |
| 527 | HIST_ENTRY *entry; |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 528 | |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 529 | if (!PyArg_ParseTuple(args, "i:remove_history_item", &entry_number)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 530 | return NULL; |
| 531 | if (entry_number < 0) { |
| 532 | PyErr_SetString(PyExc_ValueError, |
| 533 | "History index cannot be negative"); |
| 534 | return NULL; |
| 535 | } |
| 536 | entry = remove_history(entry_number); |
| 537 | if (!entry) { |
| 538 | PyErr_Format(PyExc_ValueError, |
| 539 | "No history item at position %d", |
| 540 | entry_number); |
| 541 | return NULL; |
| 542 | } |
| 543 | /* free memory allocated for the history entry */ |
Mark Dickinson | 29b238e | 2010-08-03 16:08:16 +0000 | [diff] [blame] | 544 | _py_free_history_entry(entry); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 545 | Py_RETURN_NONE; |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | PyDoc_STRVAR(doc_remove_history, |
Skip Montanaro | 6c06cd5 | 2004-08-16 16:15:13 +0000 | [diff] [blame] | 549 | "remove_history_item(pos) -> None\n\ |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 550 | remove history item given by its position"); |
| 551 | |
| 552 | static PyObject * |
| 553 | py_replace_history(PyObject *self, PyObject *args) |
| 554 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 555 | int entry_number; |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 556 | PyObject *line; |
| 557 | PyObject *encoded; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 558 | HIST_ENTRY *old_entry; |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 559 | |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 560 | if (!PyArg_ParseTuple(args, "iU:replace_history_item", &entry_number, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 561 | &line)) { |
| 562 | return NULL; |
| 563 | } |
| 564 | if (entry_number < 0) { |
| 565 | PyErr_SetString(PyExc_ValueError, |
| 566 | "History index cannot be negative"); |
| 567 | return NULL; |
| 568 | } |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 569 | encoded = encode(line); |
| 570 | if (encoded == NULL) { |
| 571 | return NULL; |
| 572 | } |
| 573 | old_entry = replace_history_entry(entry_number, PyBytes_AS_STRING(encoded), (void *)NULL); |
| 574 | Py_DECREF(encoded); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 575 | if (!old_entry) { |
| 576 | PyErr_Format(PyExc_ValueError, |
| 577 | "No history item at position %d", |
| 578 | entry_number); |
| 579 | return NULL; |
| 580 | } |
| 581 | /* free memory allocated for the old history entry */ |
Mark Dickinson | 29b238e | 2010-08-03 16:08:16 +0000 | [diff] [blame] | 582 | _py_free_history_entry(old_entry); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 583 | Py_RETURN_NONE; |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | PyDoc_STRVAR(doc_replace_history, |
Skip Montanaro | 6c06cd5 | 2004-08-16 16:15:13 +0000 | [diff] [blame] | 587 | "replace_history_item(pos, line) -> None\n\ |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 588 | replaces history item given by its position with contents of line"); |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 589 | |
| 590 | /* Add a line to the history buffer */ |
| 591 | |
Guido van Rossum | b6c1d52 | 2001-10-19 01:18:43 +0000 | [diff] [blame] | 592 | static PyObject * |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 593 | py_add_history(PyObject *self, PyObject *string) |
Guido van Rossum | b6c1d52 | 2001-10-19 01:18:43 +0000 | [diff] [blame] | 594 | { |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 595 | PyObject *encoded = encode(string); |
| 596 | if (encoded == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 597 | return NULL; |
| 598 | } |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 599 | add_history(PyBytes_AS_STRING(encoded)); |
| 600 | Py_DECREF(encoded); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 601 | Py_RETURN_NONE; |
Guido van Rossum | b6c1d52 | 2001-10-19 01:18:43 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 604 | PyDoc_STRVAR(doc_add_history, |
| 605 | "add_history(string) -> None\n\ |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 606 | add an item to the history buffer"); |
Guido van Rossum | b6c1d52 | 2001-10-19 01:18:43 +0000 | [diff] [blame] | 607 | |
Martin Panter | f0dbf7a | 2016-05-15 01:26:25 +0000 | [diff] [blame] | 608 | static int should_auto_add_history = 1; |
| 609 | |
| 610 | /* Enable or disable automatic history */ |
| 611 | |
| 612 | static PyObject * |
| 613 | py_set_auto_history(PyObject *self, PyObject *args) |
| 614 | { |
| 615 | if (!PyArg_ParseTuple(args, "p:set_auto_history", |
| 616 | &should_auto_add_history)) { |
| 617 | return NULL; |
| 618 | } |
| 619 | Py_RETURN_NONE; |
| 620 | } |
| 621 | |
| 622 | PyDoc_STRVAR(doc_set_auto_history, |
| 623 | "set_auto_history(enabled) -> None\n\ |
| 624 | Enables or disables automatic history."); |
| 625 | |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 626 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 627 | /* Get the tab-completion word-delimiters that readline uses */ |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 628 | |
| 629 | static PyObject * |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 630 | get_completer_delims(PyObject *self, PyObject *noarg) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 631 | { |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 632 | return decode(rl_completer_word_break_characters); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 633 | } |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 634 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 635 | PyDoc_STRVAR(doc_get_completer_delims, |
| 636 | "get_completer_delims() -> string\n\ |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 637 | get the word delimiters for completion"); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 638 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 639 | |
| 640 | /* Set the completer function */ |
| 641 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 642 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 643 | set_completer(PyObject *self, PyObject *args) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 644 | { |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 645 | return set_hook("completer", &readlinestate_global->completer, args); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 648 | PyDoc_STRVAR(doc_set_completer, |
| 649 | "set_completer([function]) -> None\n\ |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 650 | Set or remove the completer function.\n\ |
| 651 | The function is called as function(text, state),\n\ |
Fred Drake | 52d55a3 | 2001-08-01 21:44:14 +0000 | [diff] [blame] | 652 | for state in 0, 1, 2, ..., until it returns a non-string.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 653 | It should return the next possible completion starting with 'text'."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 654 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 655 | |
Michael W. Hudson | 796df15 | 2003-01-30 10:12:51 +0000 | [diff] [blame] | 656 | static PyObject * |
Neal Norwitz | d9efdc5 | 2003-03-01 15:19:41 +0000 | [diff] [blame] | 657 | get_completer(PyObject *self, PyObject *noargs) |
Michael W. Hudson | 796df15 | 2003-01-30 10:12:51 +0000 | [diff] [blame] | 658 | { |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 659 | if (readlinestate_global->completer == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 660 | Py_RETURN_NONE; |
| 661 | } |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 662 | Py_INCREF(readlinestate_global->completer); |
| 663 | return readlinestate_global->completer; |
Michael W. Hudson | 796df15 | 2003-01-30 10:12:51 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | PyDoc_STRVAR(doc_get_completer, |
| 667 | "get_completer() -> function\n\ |
| 668 | \n\ |
| 669 | Returns current completer function."); |
| 670 | |
Mark Dickinson | 6b54e1f | 2010-08-03 16:49:49 +0000 | [diff] [blame] | 671 | /* Private function to get current length of history. XXX It may be |
| 672 | * possible to replace this with a direct use of history_length instead, |
| 673 | * but it's not clear whether BSD's libedit keeps history_length up to date. |
| 674 | * See issue #8065.*/ |
| 675 | |
| 676 | static int |
| 677 | _py_get_history_length(void) |
| 678 | { |
| 679 | HISTORY_STATE *hist_st = history_get_history_state(); |
| 680 | int length = hist_st->length; |
| 681 | /* the history docs don't say so, but the address of hist_st changes each |
| 682 | time history_get_history_state is called which makes me think it's |
| 683 | freshly malloc'd memory... on the other hand, the address of the last |
| 684 | line stays the same as long as history isn't extended, so it appears to |
| 685 | be malloc'd but managed by the history package... */ |
| 686 | free(hist_st); |
| 687 | return length; |
| 688 | } |
| 689 | |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 690 | /* Exported function to get any element of history */ |
| 691 | |
| 692 | static PyObject * |
| 693 | get_history_item(PyObject *self, PyObject *args) |
| 694 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 695 | int idx = 0; |
| 696 | HIST_ENTRY *hist_ent; |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 697 | |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 698 | if (!PyArg_ParseTuple(args, "i:get_history_item", &idx)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 699 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 700 | if (using_libedit_emulation) { |
Ned Deily | f70f4a6 | 2013-09-06 15:16:19 -0700 | [diff] [blame] | 701 | /* Older versions of libedit's readline emulation |
| 702 | * use 0-based indexes, while readline and newer |
| 703 | * versions of libedit use 1-based indexes. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 704 | */ |
Mark Dickinson | 6b54e1f | 2010-08-03 16:49:49 +0000 | [diff] [blame] | 705 | int length = _py_get_history_length(); |
Ned Deily | f70f4a6 | 2013-09-06 15:16:19 -0700 | [diff] [blame] | 706 | |
| 707 | idx = idx - 1 + libedit_history_start; |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 708 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 709 | /* |
| 710 | * Apple's readline emulation crashes when |
| 711 | * the index is out of range, therefore |
| 712 | * test for that and fail gracefully. |
| 713 | */ |
Ned Deily | f70f4a6 | 2013-09-06 15:16:19 -0700 | [diff] [blame] | 714 | if (idx < (0 + libedit_history_start) |
| 715 | || idx >= (length + libedit_history_start)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 716 | Py_RETURN_NONE; |
| 717 | } |
| 718 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 719 | if ((hist_ent = history_get(idx))) |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 720 | return decode(hist_ent->line); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 721 | else { |
| 722 | Py_RETURN_NONE; |
| 723 | } |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 726 | PyDoc_STRVAR(doc_get_history_item, |
| 727 | "get_history_item() -> string\n\ |
| 728 | return the current contents of history item at index."); |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 729 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 730 | |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 731 | /* Exported function to get current length of history */ |
| 732 | |
| 733 | static PyObject * |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 734 | get_current_history_length(PyObject *self, PyObject *noarg) |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 735 | { |
Mark Dickinson | 6b54e1f | 2010-08-03 16:49:49 +0000 | [diff] [blame] | 736 | return PyLong_FromLong((long)_py_get_history_length()); |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 739 | PyDoc_STRVAR(doc_get_current_history_length, |
| 740 | "get_current_history_length() -> integer\n\ |
| 741 | return the current (not the maximum) length of history."); |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 742 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 743 | |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 744 | /* Exported function to read the current line buffer */ |
| 745 | |
| 746 | static PyObject * |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 747 | get_line_buffer(PyObject *self, PyObject *noarg) |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 748 | { |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 749 | return decode(rl_line_buffer); |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 752 | PyDoc_STRVAR(doc_get_line_buffer, |
| 753 | "get_line_buffer() -> string\n\ |
| 754 | return the current contents of the line buffer."); |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 755 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 756 | |
Martin v. Löwis | e7a9796 | 2003-09-20 16:08:33 +0000 | [diff] [blame] | 757 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER |
| 758 | |
| 759 | /* Exported function to clear the current history */ |
| 760 | |
| 761 | static PyObject * |
| 762 | py_clear_history(PyObject *self, PyObject *noarg) |
| 763 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 764 | clear_history(); |
| 765 | Py_RETURN_NONE; |
Martin v. Löwis | e7a9796 | 2003-09-20 16:08:33 +0000 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | PyDoc_STRVAR(doc_clear_history, |
| 769 | "clear_history() -> None\n\ |
| 770 | Clear the current readline history."); |
| 771 | #endif |
| 772 | |
| 773 | |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 774 | /* Exported function to insert text into the line buffer */ |
| 775 | |
| 776 | static PyObject * |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 777 | insert_text(PyObject *self, PyObject *string) |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 778 | { |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 779 | PyObject *encoded = encode(string); |
| 780 | if (encoded == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 781 | return NULL; |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 782 | } |
| 783 | rl_insert_text(PyBytes_AS_STRING(encoded)); |
| 784 | Py_DECREF(encoded); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 785 | Py_RETURN_NONE; |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 788 | PyDoc_STRVAR(doc_insert_text, |
| 789 | "insert_text(string) -> None\n\ |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 790 | Insert text into the line buffer at the cursor position."); |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 791 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 792 | |
| 793 | /* Redisplay the line buffer */ |
| 794 | |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 795 | static PyObject * |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 796 | redisplay(PyObject *self, PyObject *noarg) |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 797 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 798 | rl_redisplay(); |
| 799 | Py_RETURN_NONE; |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 800 | } |
| 801 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 802 | PyDoc_STRVAR(doc_redisplay, |
| 803 | "redisplay() -> None\n\ |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 804 | Change what's displayed on the screen to reflect the current\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 805 | contents of the line buffer."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 806 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 807 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 808 | /* Table of functions exported by the module */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 809 | |
| 810 | static struct PyMethodDef readline_methods[] = |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 811 | { |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 812 | {"parse_and_bind", parse_and_bind, METH_O, doc_parse_and_bind}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 813 | {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer}, |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 814 | {"insert_text", insert_text, METH_O, doc_insert_text}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 815 | {"redisplay", redisplay, METH_NOARGS, doc_redisplay}, |
| 816 | {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file}, |
| 817 | {"read_history_file", read_history_file, |
| 818 | METH_VARARGS, doc_read_history_file}, |
| 819 | {"write_history_file", write_history_file, |
| 820 | METH_VARARGS, doc_write_history_file}, |
Benjamin Peterson | d1e22ba | 2014-11-26 14:35:12 -0600 | [diff] [blame] | 821 | #ifdef HAVE_RL_APPEND_HISTORY |
Benjamin Peterson | 33f8f15 | 2014-11-26 13:58:16 -0600 | [diff] [blame] | 822 | {"append_history_file", append_history_file, |
| 823 | METH_VARARGS, doc_append_history_file}, |
Ned Deily | 8007cbc | 2014-11-26 13:02:33 -0800 | [diff] [blame] | 824 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 825 | {"get_history_item", get_history_item, |
| 826 | METH_VARARGS, doc_get_history_item}, |
| 827 | {"get_current_history_length", (PyCFunction)get_current_history_length, |
| 828 | METH_NOARGS, doc_get_current_history_length}, |
| 829 | {"set_history_length", set_history_length, |
| 830 | METH_VARARGS, set_history_length_doc}, |
| 831 | {"get_history_length", get_history_length, |
| 832 | METH_NOARGS, get_history_length_doc}, |
| 833 | {"set_completer", set_completer, METH_VARARGS, doc_set_completer}, |
| 834 | {"get_completer", get_completer, METH_NOARGS, doc_get_completer}, |
| 835 | {"get_completion_type", get_completion_type, |
| 836 | METH_NOARGS, doc_get_completion_type}, |
| 837 | {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx}, |
| 838 | {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx}, |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 839 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 840 | {"set_completer_delims", set_completer_delims, |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 841 | METH_O, doc_set_completer_delims}, |
Martin Panter | f0dbf7a | 2016-05-15 01:26:25 +0000 | [diff] [blame] | 842 | {"set_auto_history", py_set_auto_history, METH_VARARGS, doc_set_auto_history}, |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 843 | {"add_history", py_add_history, METH_O, doc_add_history}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 844 | {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history}, |
| 845 | {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history}, |
| 846 | {"get_completer_delims", get_completer_delims, |
| 847 | METH_NOARGS, doc_get_completer_delims}, |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 848 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 849 | {"set_completion_display_matches_hook", set_completion_display_matches_hook, |
| 850 | METH_VARARGS, doc_set_completion_display_matches_hook}, |
| 851 | {"set_startup_hook", set_startup_hook, |
| 852 | METH_VARARGS, doc_set_startup_hook}, |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 853 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 854 | {"set_pre_input_hook", set_pre_input_hook, |
| 855 | METH_VARARGS, doc_set_pre_input_hook}, |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 856 | #endif |
Martin v. Löwis | e7a9796 | 2003-09-20 16:08:33 +0000 | [diff] [blame] | 857 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 858 | {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history}, |
Martin v. Löwis | e7a9796 | 2003-09-20 16:08:33 +0000 | [diff] [blame] | 859 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 860 | {0, 0} |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 861 | }; |
| 862 | |
Guido van Rossum | 05ac449 | 2003-01-07 20:04:12 +0000 | [diff] [blame] | 863 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 864 | /* C function to call the Python hooks. */ |
| 865 | |
| 866 | static int |
Michael W. Hudson | da6242c | 2005-03-30 11:21:53 +0000 | [diff] [blame] | 867 | on_hook(PyObject *func) |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 868 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 869 | int result = 0; |
| 870 | if (func != NULL) { |
| 871 | PyObject *r; |
Victor Stinner | 2ff58a2 | 2019-06-17 14:27:23 +0200 | [diff] [blame] | 872 | r = PyObject_CallNoArgs(func); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 873 | if (r == NULL) |
| 874 | goto error; |
| 875 | if (r == Py_None) |
| 876 | result = 0; |
| 877 | else { |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 878 | result = _PyLong_AsInt(r); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 879 | if (result == -1 && PyErr_Occurred()) |
| 880 | goto error; |
| 881 | } |
| 882 | Py_DECREF(r); |
| 883 | goto done; |
| 884 | error: |
| 885 | PyErr_Clear(); |
| 886 | Py_XDECREF(r); |
| 887 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 888 | return result; |
| 889 | } |
| 890 | return result; |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | static int |
Ned Deily | 7b9ddea | 2014-02-05 16:53:10 -0800 | [diff] [blame] | 894 | #if defined(_RL_FUNCTION_TYPEDEF) |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 895 | on_startup_hook(void) |
Ned Deily | 7b9ddea | 2014-02-05 16:53:10 -0800 | [diff] [blame] | 896 | #else |
| 897 | on_startup_hook() |
| 898 | #endif |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 899 | { |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 900 | int r; |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 901 | PyGILState_STATE gilstate = PyGILState_Ensure(); |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 902 | r = on_hook(readlinestate_global->startup_hook); |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 903 | PyGILState_Release(gilstate); |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 904 | return r; |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
| 908 | static int |
Ned Deily | 7b9ddea | 2014-02-05 16:53:10 -0800 | [diff] [blame] | 909 | #if defined(_RL_FUNCTION_TYPEDEF) |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 910 | on_pre_input_hook(void) |
Ned Deily | 7b9ddea | 2014-02-05 16:53:10 -0800 | [diff] [blame] | 911 | #else |
| 912 | on_pre_input_hook() |
| 913 | #endif |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 914 | { |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 915 | int r; |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 916 | PyGILState_STATE gilstate = PyGILState_Ensure(); |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 917 | r = on_hook(readlinestate_global->pre_input_hook); |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 918 | PyGILState_Release(gilstate); |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 919 | return r; |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 920 | } |
| 921 | #endif |
| 922 | |
Guido van Rossum | 05ac449 | 2003-01-07 20:04:12 +0000 | [diff] [blame] | 923 | |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 924 | /* C function to call the Python completion_display_matches */ |
| 925 | |
Georg Brandl | 646fdd6 | 2010-10-18 07:27:55 +0000 | [diff] [blame] | 926 | #ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 927 | static void |
| 928 | on_completion_display_matches_hook(char **matches, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 929 | int num_matches, int max_length) |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 930 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 931 | int i; |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 932 | PyObject *sub, *m=NULL, *s=NULL, *r=NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 933 | PyGILState_STATE gilstate = PyGILState_Ensure(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 934 | m = PyList_New(num_matches); |
| 935 | if (m == NULL) |
| 936 | goto error; |
| 937 | for (i = 0; i < num_matches; i++) { |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 938 | s = decode(matches[i+1]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 939 | if (s == NULL) |
| 940 | goto error; |
Zackery Spytz | 99d56b5 | 2018-12-08 07:16:55 -0700 | [diff] [blame] | 941 | PyList_SET_ITEM(m, i, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 942 | } |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 943 | sub = decode(matches[0]); |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 944 | r = PyObject_CallFunction(readlinestate_global->completion_display_matches_hook, |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 945 | "NNi", sub, m, max_length); |
Christian Heimes | 32fbe59 | 2007-11-12 15:01:33 +0000 | [diff] [blame] | 946 | |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 947 | m=NULL; |
Christian Heimes | 32fbe59 | 2007-11-12 15:01:33 +0000 | [diff] [blame] | 948 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 949 | if (r == NULL || |
| 950 | (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) { |
| 951 | goto error; |
| 952 | } |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 953 | Py_CLEAR(r); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 954 | |
| 955 | if (0) { |
| 956 | error: |
| 957 | PyErr_Clear(); |
| 958 | Py_XDECREF(m); |
| 959 | Py_XDECREF(r); |
| 960 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 961 | PyGILState_Release(gilstate); |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Senthil Kumaran | 95c0700 | 2010-11-04 03:51:05 +0000 | [diff] [blame] | 964 | #endif |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 965 | |
Martin Panter | 5dbbf1a | 2016-04-03 02:54:58 +0000 | [diff] [blame] | 966 | #ifdef HAVE_RL_RESIZE_TERMINAL |
| 967 | static volatile sig_atomic_t sigwinch_received; |
Martin Panter | 28f35b2 | 2016-04-03 08:00:49 +0000 | [diff] [blame] | 968 | static PyOS_sighandler_t sigwinch_ohandler; |
Martin Panter | 5dbbf1a | 2016-04-03 02:54:58 +0000 | [diff] [blame] | 969 | |
| 970 | static void |
| 971 | readline_sigwinch_handler(int signum) |
| 972 | { |
| 973 | sigwinch_received = 1; |
| 974 | if (sigwinch_ohandler && |
| 975 | sigwinch_ohandler != SIG_IGN && sigwinch_ohandler != SIG_DFL) |
| 976 | sigwinch_ohandler(signum); |
| 977 | |
| 978 | #ifndef HAVE_SIGACTION |
| 979 | /* If the handler was installed with signal() rather than sigaction(), |
| 980 | we need to reinstall it. */ |
| 981 | PyOS_setsig(SIGWINCH, readline_sigwinch_handler); |
| 982 | #endif |
| 983 | } |
| 984 | #endif |
| 985 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 986 | /* C function to call the Python completer. */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 987 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 988 | static char * |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 989 | on_completion(const char *text, int state) |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 990 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 991 | char *result = NULL; |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 992 | if (readlinestate_global->completer != NULL) { |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 993 | PyObject *r = NULL, *t; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 994 | PyGILState_STATE gilstate = PyGILState_Ensure(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 995 | rl_attempted_completion_over = 1; |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 996 | t = decode(text); |
| 997 | r = PyObject_CallFunction(readlinestate_global->completer, "Ni", t, state); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 998 | if (r == NULL) |
| 999 | goto error; |
| 1000 | if (r == Py_None) { |
| 1001 | result = NULL; |
| 1002 | } |
| 1003 | else { |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 1004 | PyObject *encoded = encode(r); |
| 1005 | if (encoded == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1006 | goto error; |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 1007 | result = strdup(PyBytes_AS_STRING(encoded)); |
| 1008 | Py_DECREF(encoded); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1009 | } |
| 1010 | Py_DECREF(r); |
| 1011 | goto done; |
| 1012 | error: |
| 1013 | PyErr_Clear(); |
| 1014 | Py_XDECREF(r); |
| 1015 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1016 | PyGILState_Release(gilstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1017 | return result; |
| 1018 | } |
| 1019 | return result; |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 1022 | |
Guido van Rossum | 6d0d365 | 2003-01-07 20:34:19 +0000 | [diff] [blame] | 1023 | /* A more flexible constructor that saves the "begidx" and "endidx" |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 1024 | * before calling the normal completer */ |
| 1025 | |
Neal Norwitz | c355f0c | 2003-02-21 00:30:18 +0000 | [diff] [blame] | 1026 | static char ** |
Benjamin Peterson | f0b463a | 2014-01-24 11:44:16 -0500 | [diff] [blame] | 1027 | flex_complete(const char *text, int start, int end) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 1028 | { |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 1029 | char **result; |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 1030 | char saved; |
| 1031 | size_t start_size, end_size; |
| 1032 | wchar_t *s; |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 1033 | PyGILState_STATE gilstate = PyGILState_Ensure(); |
Antoine Pitrou | dc0900b | 2009-10-19 18:22:37 +0000 | [diff] [blame] | 1034 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1035 | rl_completion_append_character ='\0'; |
Antoine Pitrou | d513177 | 2009-10-26 19:22:14 +0000 | [diff] [blame] | 1036 | #endif |
| 1037 | #ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1038 | rl_completion_suppress_append = 0; |
Antoine Pitrou | dc0900b | 2009-10-19 18:22:37 +0000 | [diff] [blame] | 1039 | #endif |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 1040 | |
| 1041 | saved = rl_line_buffer[start]; |
| 1042 | rl_line_buffer[start] = 0; |
| 1043 | s = Py_DecodeLocale(rl_line_buffer, &start_size); |
| 1044 | rl_line_buffer[start] = saved; |
| 1045 | if (s == NULL) { |
| 1046 | goto done; |
| 1047 | } |
| 1048 | PyMem_RawFree(s); |
| 1049 | saved = rl_line_buffer[end]; |
| 1050 | rl_line_buffer[end] = 0; |
| 1051 | s = Py_DecodeLocale(rl_line_buffer + start, &end_size); |
| 1052 | rl_line_buffer[end] = saved; |
| 1053 | if (s == NULL) { |
| 1054 | goto done; |
| 1055 | } |
| 1056 | PyMem_RawFree(s); |
| 1057 | start = (int)start_size; |
| 1058 | end = start + (int)end_size; |
| 1059 | |
| 1060 | done: |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 1061 | Py_XDECREF(readlinestate_global->begidx); |
| 1062 | Py_XDECREF(readlinestate_global->endidx); |
| 1063 | readlinestate_global->begidx = PyLong_FromLong((long) start); |
| 1064 | readlinestate_global->endidx = PyLong_FromLong((long) end); |
Benjamin Peterson | 1bb0c0b | 2016-09-05 18:26:19 -0700 | [diff] [blame] | 1065 | result = completion_matches((char *)text, *on_completion); |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 1066 | PyGILState_Release(gilstate); |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 1067 | return result; |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
Guido van Rossum | 05ac449 | 2003-01-07 20:04:12 +0000 | [diff] [blame] | 1070 | |
Victor Stinner | 1d8da61 | 2019-10-30 16:39:27 +0100 | [diff] [blame] | 1071 | /* Helper to initialize GNU readline properly. |
| 1072 | Return -1 on memory allocation failure, return 0 on success. */ |
| 1073 | static int |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 1074 | setup_readline(readlinestate *mod_state) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 1075 | { |
Guido van Rossum | 60c8a3a | 2002-10-09 21:27:33 +0000 | [diff] [blame] | 1076 | #ifdef SAVE_LOCALE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1077 | char *saved_locale = strdup(setlocale(LC_CTYPE, NULL)); |
Victor Stinner | 1d8da61 | 2019-10-30 16:39:27 +0100 | [diff] [blame] | 1078 | if (!saved_locale) { |
| 1079 | return -1; |
| 1080 | } |
Guido van Rossum | 60c8a3a | 2002-10-09 21:27:33 +0000 | [diff] [blame] | 1081 | #endif |
| 1082 | |
Zvezdan Petkovic | c2f082e | 2018-05-17 02:45:10 -0400 | [diff] [blame] | 1083 | /* The name must be defined before initialization */ |
| 1084 | rl_readline_name = "python"; |
| 1085 | |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 1086 | /* the libedit readline emulation resets key bindings etc |
R. David Murray | 52d1b4e | 2010-12-18 03:48:32 +0000 | [diff] [blame] | 1087 | * when calling rl_initialize. So call it upfront |
| 1088 | */ |
| 1089 | if (using_libedit_emulation) |
| 1090 | rl_initialize(); |
Ned Deily | f70f4a6 | 2013-09-06 15:16:19 -0700 | [diff] [blame] | 1091 | |
| 1092 | /* Detect if libedit's readline emulation uses 0-based |
| 1093 | * indexing or 1-based indexing. |
| 1094 | */ |
| 1095 | add_history("1"); |
| 1096 | if (history_get(1) == NULL) { |
| 1097 | libedit_history_start = 0; |
| 1098 | } else { |
| 1099 | libedit_history_start = 1; |
| 1100 | } |
| 1101 | clear_history(); |
R. David Murray | 52d1b4e | 2010-12-18 03:48:32 +0000 | [diff] [blame] | 1102 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1103 | using_history(); |
Skip Montanaro | a039274 | 2002-06-11 14:32:46 +0000 | [diff] [blame] | 1104 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1105 | /* Force rebind of TAB to insert-tab */ |
| 1106 | rl_bind_key('\t', rl_insert); |
| 1107 | /* Bind both ESC-TAB and ESC-ESC to the completion function */ |
| 1108 | rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap); |
| 1109 | rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap); |
Martin Panter | 5dbbf1a | 2016-04-03 02:54:58 +0000 | [diff] [blame] | 1110 | #ifdef HAVE_RL_RESIZE_TERMINAL |
| 1111 | /* Set up signal handler for window resize */ |
| 1112 | sigwinch_ohandler = PyOS_setsig(SIGWINCH, readline_sigwinch_handler); |
| 1113 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1114 | /* Set our hook functions */ |
Benjamin Peterson | f0b463a | 2014-01-24 11:44:16 -0500 | [diff] [blame] | 1115 | rl_startup_hook = on_startup_hook; |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 1116 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
Benjamin Peterson | f0b463a | 2014-01-24 11:44:16 -0500 | [diff] [blame] | 1117 | rl_pre_input_hook = on_pre_input_hook; |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 1118 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1119 | /* Set our completion function */ |
Benjamin Peterson | f0b463a | 2014-01-24 11:44:16 -0500 | [diff] [blame] | 1120 | rl_attempted_completion_function = flex_complete; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1121 | /* Set Python word break characters */ |
Antoine Pitrou | a7f7deb | 2013-05-06 21:51:03 +0200 | [diff] [blame] | 1122 | completer_word_break_characters = |
| 1123 | rl_completer_word_break_characters = |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1124 | strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?"); |
| 1125 | /* All nonalphanums except '.' */ |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 1126 | |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 1127 | mod_state->begidx = PyLong_FromLong(0L); |
| 1128 | mod_state->endidx = PyLong_FromLong(0L); |
Victor Stinner | a3c80ce | 2014-07-24 12:23:56 +0200 | [diff] [blame] | 1129 | |
Martin Panter | c427b8d | 2016-08-27 03:23:11 +0000 | [diff] [blame] | 1130 | if (!using_libedit_emulation) |
Martin Panter | c427b8d | 2016-08-27 03:23:11 +0000 | [diff] [blame] | 1131 | { |
| 1132 | if (!isatty(STDOUT_FILENO)) { |
| 1133 | /* Issue #19884: stdout is not a terminal. Disable meta modifier |
| 1134 | keys to not write the ANSI sequence "\033[1034h" into stdout. On |
| 1135 | terminals supporting 8 bit characters like TERM=xterm-256color |
| 1136 | (which is now the default Fedora since Fedora 18), the meta key is |
| 1137 | used to enable support of 8 bit characters (ANSI sequence |
| 1138 | "\033[1034h"). |
| 1139 | |
| 1140 | With libedit, this call makes readline() crash. */ |
| 1141 | rl_variable_bind ("enable-meta-key", "off"); |
| 1142 | } |
| 1143 | } |
Victor Stinner | a3c80ce | 2014-07-24 12:23:56 +0200 | [diff] [blame] | 1144 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1145 | /* Initialize (allows .inputrc to override) |
| 1146 | * |
| 1147 | * XXX: A bug in the readline-2.2 library causes a memory leak |
| 1148 | * inside this function. Nothing we can do about it. |
| 1149 | */ |
R. David Murray | 52d1b4e | 2010-12-18 03:48:32 +0000 | [diff] [blame] | 1150 | if (using_libedit_emulation) |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 1151 | rl_read_init_file(NULL); |
R. David Murray | 52d1b4e | 2010-12-18 03:48:32 +0000 | [diff] [blame] | 1152 | else |
R. David Murray | 52d1b4e | 2010-12-18 03:48:32 +0000 | [diff] [blame] | 1153 | rl_initialize(); |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 1154 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1155 | RESTORE_LOCALE(saved_locale) |
Victor Stinner | 1d8da61 | 2019-10-30 16:39:27 +0100 | [diff] [blame] | 1156 | return 0; |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1159 | /* Wrapper around GNU readline that handles signals differently. */ |
| 1160 | |
Antoine Pitrou | f474c5a | 2017-07-18 17:05:03 +0200 | [diff] [blame] | 1161 | static char *completed_input_string; |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1162 | static void |
| 1163 | rlhandler(char *text) |
| 1164 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1165 | completed_input_string = text; |
| 1166 | rl_callback_handler_remove(); |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1169 | static char * |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 1170 | readline_until_enter_or_signal(const char *prompt, int *signal) |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1171 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1172 | char * not_done_reading = ""; |
| 1173 | fd_set selectset; |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1174 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1175 | *signal = 0; |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1176 | #ifdef HAVE_RL_CATCH_SIGNAL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1177 | rl_catch_signals = 0; |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1178 | #endif |
| 1179 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1180 | rl_callback_handler_install (prompt, rlhandler); |
| 1181 | FD_ZERO(&selectset); |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1182 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1183 | completed_input_string = not_done_reading; |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1184 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1185 | while (completed_input_string == not_done_reading) { |
Antoine Pitrou | c345ce1 | 2011-12-16 12:28:32 +0100 | [diff] [blame] | 1186 | int has_input = 0, err = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1187 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1188 | while (!has_input) |
| 1189 | { struct timeval timeout = {0, 100000}; /* 0.1 seconds */ |
Michael W. Hudson | 8da2b01 | 2004-10-07 13:46:33 +0000 | [diff] [blame] | 1190 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1191 | /* [Bug #1552726] Only limit the pause if an input hook has been |
| 1192 | defined. */ |
| 1193 | struct timeval *timeoutp = NULL; |
| 1194 | if (PyOS_InputHook) |
| 1195 | timeoutp = &timeout; |
Martin Panter | 5dbbf1a | 2016-04-03 02:54:58 +0000 | [diff] [blame] | 1196 | #ifdef HAVE_RL_RESIZE_TERMINAL |
| 1197 | /* Update readline's view of the window size after SIGWINCH */ |
| 1198 | if (sigwinch_received) { |
| 1199 | sigwinch_received = 0; |
| 1200 | rl_resize_terminal(); |
| 1201 | } |
| 1202 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1203 | FD_SET(fileno(rl_instream), &selectset); |
| 1204 | /* select resets selectset if no input was available */ |
| 1205 | has_input = select(fileno(rl_instream) + 1, &selectset, |
| 1206 | NULL, NULL, timeoutp); |
Antoine Pitrou | c345ce1 | 2011-12-16 12:28:32 +0100 | [diff] [blame] | 1207 | err = errno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1208 | if(PyOS_InputHook) PyOS_InputHook(); |
| 1209 | } |
| 1210 | |
Antoine Pitrou | c345ce1 | 2011-12-16 12:28:32 +0100 | [diff] [blame] | 1211 | if (has_input > 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1212 | rl_callback_read_char(); |
| 1213 | } |
Antoine Pitrou | c345ce1 | 2011-12-16 12:28:32 +0100 | [diff] [blame] | 1214 | else if (err == EINTR) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1215 | int s; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1216 | PyEval_RestoreThread(_PyOS_ReadlineTState); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1217 | s = PyErr_CheckSignals(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1218 | PyEval_SaveThread(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1219 | if (s < 0) { |
| 1220 | rl_free_line_state(); |
Martin Panter | d6990d2 | 2016-03-22 07:24:05 +0000 | [diff] [blame] | 1221 | #if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0700 |
| 1222 | rl_callback_sigcleanup(); |
| 1223 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1224 | rl_cleanup_after_signal(); |
| 1225 | rl_callback_handler_remove(); |
| 1226 | *signal = 1; |
| 1227 | completed_input_string = NULL; |
| 1228 | } |
| 1229 | } |
| 1230 | } |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1231 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1232 | return completed_input_string; |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1236 | static char * |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 1237 | call_readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt) |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1238 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1239 | size_t n; |
Victor Stinner | 1600f60 | 2018-11-30 15:03:53 +0100 | [diff] [blame] | 1240 | char *p; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1241 | int signal; |
Neal Norwitz | 1fa040b | 2004-08-25 01:20:18 +0000 | [diff] [blame] | 1242 | |
Martin v. Löwis | 78a8acc | 2004-08-18 13:34:00 +0000 | [diff] [blame] | 1243 | #ifdef SAVE_LOCALE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1244 | char *saved_locale = strdup(setlocale(LC_CTYPE, NULL)); |
| 1245 | if (!saved_locale) |
| 1246 | Py_FatalError("not enough memory to save locale"); |
xdegaye | 1588be6 | 2017-11-12 12:45:59 +0100 | [diff] [blame] | 1247 | _Py_SetLocaleFromEnv(LC_CTYPE); |
Martin v. Löwis | 78a8acc | 2004-08-18 13:34:00 +0000 | [diff] [blame] | 1248 | #endif |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1249 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1250 | if (sys_stdin != rl_instream || sys_stdout != rl_outstream) { |
| 1251 | rl_instream = sys_stdin; |
| 1252 | rl_outstream = sys_stdout; |
Guido van Rossum | faf5e4d | 2002-12-30 16:25:41 +0000 | [diff] [blame] | 1253 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1254 | rl_prep_terminal (1); |
Guido van Rossum | faf5e4d | 2002-12-30 16:25:41 +0000 | [diff] [blame] | 1255 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1256 | } |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 1257 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1258 | p = readline_until_enter_or_signal(prompt, &signal); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1259 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1260 | /* we got an interrupt signal */ |
| 1261 | if (signal) { |
| 1262 | RESTORE_LOCALE(saved_locale) |
| 1263 | return NULL; |
| 1264 | } |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1265 | |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 1266 | /* We got an EOF, return an empty string. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1267 | if (p == NULL) { |
Victor Stinner | 2fe9bac | 2013-10-10 16:18:20 +0200 | [diff] [blame] | 1268 | p = PyMem_RawMalloc(1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1269 | if (p != NULL) |
| 1270 | *p = '\0'; |
| 1271 | RESTORE_LOCALE(saved_locale) |
| 1272 | return p; |
| 1273 | } |
| 1274 | |
| 1275 | /* we have a valid line */ |
| 1276 | n = strlen(p); |
Martin Panter | f0dbf7a | 2016-05-15 01:26:25 +0000 | [diff] [blame] | 1277 | if (should_auto_add_history && n > 0) { |
Brett Cannon | 2525dc8 | 2010-08-22 20:36:25 +0000 | [diff] [blame] | 1278 | const char *line; |
Mark Dickinson | 6b54e1f | 2010-08-03 16:49:49 +0000 | [diff] [blame] | 1279 | int length = _py_get_history_length(); |
Nir Soffer | fae8f4a | 2017-07-07 09:10:46 +0300 | [diff] [blame] | 1280 | if (length > 0) { |
| 1281 | HIST_ENTRY *hist_ent; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1282 | if (using_libedit_emulation) { |
Ned Deily | f70f4a6 | 2013-09-06 15:16:19 -0700 | [diff] [blame] | 1283 | /* handle older 0-based or newer 1-based indexing */ |
Nir Soffer | fae8f4a | 2017-07-07 09:10:46 +0300 | [diff] [blame] | 1284 | hist_ent = history_get(length + libedit_history_start - 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1285 | } else |
Nir Soffer | fae8f4a | 2017-07-07 09:10:46 +0300 | [diff] [blame] | 1286 | hist_ent = history_get(length); |
| 1287 | line = hist_ent ? hist_ent->line : ""; |
| 1288 | } else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1289 | line = ""; |
| 1290 | if (strcmp(p, line)) |
| 1291 | add_history(p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1292 | } |
| 1293 | /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and |
| 1294 | release the original. */ |
Victor Stinner | 1600f60 | 2018-11-30 15:03:53 +0100 | [diff] [blame] | 1295 | char *q = p; |
Victor Stinner | 2fe9bac | 2013-10-10 16:18:20 +0200 | [diff] [blame] | 1296 | p = PyMem_RawMalloc(n+2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1297 | if (p != NULL) { |
Victor Stinner | 1600f60 | 2018-11-30 15:03:53 +0100 | [diff] [blame] | 1298 | memcpy(p, q, n); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1299 | p[n] = '\n'; |
| 1300 | p[n+1] = '\0'; |
| 1301 | } |
| 1302 | free(q); |
| 1303 | RESTORE_LOCALE(saved_locale) |
| 1304 | return p; |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 1307 | |
| 1308 | /* Initialize the module */ |
| 1309 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1310 | PyDoc_STRVAR(doc_module, |
| 1311 | "Importing this module enables command line editing using GNU readline."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 1312 | |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 1313 | PyDoc_STRVAR(doc_module_le, |
| 1314 | "Importing this module enables command line editing using libedit readline."); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1315 | |
| 1316 | static struct PyModuleDef readlinemodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1317 | PyModuleDef_HEAD_INIT, |
| 1318 | "readline", |
| 1319 | doc_module, |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 1320 | sizeof(readlinestate), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1321 | readline_methods, |
| 1322 | NULL, |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 1323 | readline_traverse, |
| 1324 | readline_clear, |
| 1325 | readline_free |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1326 | }; |
| 1327 | |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 1328 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 1329 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1330 | PyInit_readline(void) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 1331 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1332 | PyObject *m; |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 1333 | readlinestate *mod_state; |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 1334 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1335 | if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) { |
| 1336 | using_libedit_emulation = 1; |
| 1337 | } |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 1338 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1339 | if (using_libedit_emulation) |
| 1340 | readlinemodule.m_doc = doc_module_le; |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 1341 | |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 1342 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1343 | m = PyModule_Create(&readlinemodule); |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 1344 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1345 | if (m == NULL) |
| 1346 | return NULL; |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 1347 | |
Victor Stinner | 0efc024 | 2017-11-30 17:21:07 +0100 | [diff] [blame] | 1348 | if (PyModule_AddIntConstant(m, "_READLINE_VERSION", |
| 1349 | RL_READLINE_VERSION) < 0) { |
| 1350 | goto error; |
| 1351 | } |
| 1352 | if (PyModule_AddIntConstant(m, "_READLINE_RUNTIME_VERSION", |
| 1353 | rl_readline_version) < 0) { |
| 1354 | goto error; |
| 1355 | } |
| 1356 | if (PyModule_AddStringConstant(m, "_READLINE_LIBRARY_VERSION", |
| 1357 | rl_library_version) < 0) |
| 1358 | { |
| 1359 | goto error; |
| 1360 | } |
| 1361 | |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 1362 | mod_state = (readlinestate *) PyModule_GetState(m); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1363 | PyOS_ReadlineFunctionPointer = call_readline; |
Victor Stinner | 1d8da61 | 2019-10-30 16:39:27 +0100 | [diff] [blame] | 1364 | if (setup_readline(mod_state) < 0) { |
| 1365 | PyErr_NoMemory(); |
| 1366 | goto error; |
| 1367 | } |
Antoine Pitrou | 7e8b867 | 2014-11-04 14:52:10 +0100 | [diff] [blame] | 1368 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1369 | return m; |
Victor Stinner | 0efc024 | 2017-11-30 17:21:07 +0100 | [diff] [blame] | 1370 | |
| 1371 | error: |
| 1372 | Py_DECREF(m); |
| 1373 | return NULL; |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 1374 | } |