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