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