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