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