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