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 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 23 | #ifdef SAVE_LOCALE |
| 24 | # define RESTORE_LOCALE(sl) { setlocale(LC_CTYPE, sl); free(sl); } |
| 25 | #else |
| 26 | # define RESTORE_LOCALE(sl) |
| 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) \ |
| 36 | rl_completion_matches((x), ((rl_compentry_func_t *)(y))) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 37 | #else |
| 38 | extern char **completion_matches(char *, rl_compentry_func_t *); |
Guido van Rossum | 353ae58 | 2001-07-10 16:45:32 +0000 | [diff] [blame] | 39 | #endif |
| 40 | |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 42 | /* Exported function to send one line to readline's init file parser */ |
| 43 | |
| 44 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 45 | parse_and_bind(PyObject *self, PyObject *args) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 46 | { |
Guido van Rossum | 3b5330e | 1998-12-04 15:34:39 +0000 | [diff] [blame] | 47 | char *s, *copy; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 48 | if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s)) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 49 | return NULL; |
Guido van Rossum | 3b5330e | 1998-12-04 15:34:39 +0000 | [diff] [blame] | 50 | /* Make a copy -- rl_parse_and_bind() modifies its argument */ |
| 51 | /* Bernard Herzog */ |
| 52 | copy = malloc(1 + strlen(s)); |
| 53 | if (copy == NULL) |
| 54 | return PyErr_NoMemory(); |
| 55 | strcpy(copy, s); |
| 56 | rl_parse_and_bind(copy); |
| 57 | free(copy); /* Free the copy */ |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 58 | Py_INCREF(Py_None); |
| 59 | return Py_None; |
| 60 | } |
| 61 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 62 | PyDoc_STRVAR(doc_parse_and_bind, |
| 63 | "parse_and_bind(string) -> None\n\ |
| 64 | Parse and execute single line of a readline init file."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 65 | |
| 66 | |
| 67 | /* Exported function to parse a readline init file */ |
| 68 | |
| 69 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 70 | read_init_file(PyObject *self, PyObject *args) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 71 | { |
| 72 | char *s = NULL; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 73 | if (!PyArg_ParseTuple(args, "|z:read_init_file", &s)) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 74 | return NULL; |
| 75 | errno = rl_read_init_file(s); |
| 76 | if (errno) |
| 77 | return PyErr_SetFromErrno(PyExc_IOError); |
| 78 | Py_INCREF(Py_None); |
| 79 | return Py_None; |
| 80 | } |
| 81 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 82 | PyDoc_STRVAR(doc_read_init_file, |
| 83 | "read_init_file([filename]) -> None\n\ |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 84 | Parse a readline initialization file.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 85 | The default filename is the last filename used."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 86 | |
| 87 | |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 88 | /* Exported function to load a readline history file */ |
| 89 | |
| 90 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 91 | read_history_file(PyObject *self, PyObject *args) |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 92 | { |
| 93 | char *s = NULL; |
| 94 | if (!PyArg_ParseTuple(args, "|z:read_history_file", &s)) |
| 95 | return NULL; |
| 96 | errno = read_history(s); |
| 97 | if (errno) |
| 98 | return PyErr_SetFromErrno(PyExc_IOError); |
| 99 | Py_INCREF(Py_None); |
| 100 | return Py_None; |
| 101 | } |
| 102 | |
Hye-Shik Chang | 7a8173a | 2004-11-25 04:04:20 +0000 | [diff] [blame] | 103 | 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] | 104 | PyDoc_STRVAR(doc_read_history_file, |
| 105 | "read_history_file([filename]) -> None\n\ |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 106 | Load a readline history file.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 107 | The default filename is ~/.history."); |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 108 | |
| 109 | |
| 110 | /* Exported function to save a readline history file */ |
| 111 | |
| 112 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 113 | write_history_file(PyObject *self, PyObject *args) |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 114 | { |
| 115 | char *s = NULL; |
| 116 | if (!PyArg_ParseTuple(args, "|z:write_history_file", &s)) |
| 117 | return NULL; |
| 118 | errno = write_history(s); |
Hye-Shik Chang | 7a8173a | 2004-11-25 04:04:20 +0000 | [diff] [blame] | 119 | if (!errno && _history_length >= 0) |
| 120 | history_truncate_file(s, _history_length); |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 121 | if (errno) |
| 122 | return PyErr_SetFromErrno(PyExc_IOError); |
| 123 | Py_INCREF(Py_None); |
| 124 | return Py_None; |
| 125 | } |
| 126 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 127 | PyDoc_STRVAR(doc_write_history_file, |
| 128 | "write_history_file([filename]) -> None\n\ |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 129 | Save a readline history file.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 130 | The default filename is ~/.history."); |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 131 | |
| 132 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 133 | /* Set history length */ |
| 134 | |
| 135 | static PyObject* |
| 136 | set_history_length(PyObject *self, PyObject *args) |
| 137 | { |
Hye-Shik Chang | 7a8173a | 2004-11-25 04:04:20 +0000 | [diff] [blame] | 138 | int length = _history_length; |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 139 | if (!PyArg_ParseTuple(args, "i:set_history_length", &length)) |
| 140 | return NULL; |
Hye-Shik Chang | 7a8173a | 2004-11-25 04:04:20 +0000 | [diff] [blame] | 141 | _history_length = length; |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 142 | Py_INCREF(Py_None); |
| 143 | return Py_None; |
| 144 | } |
| 145 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 146 | PyDoc_STRVAR(set_history_length_doc, |
| 147 | "set_history_length(length) -> None\n\ |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 148 | set the maximal number of items which will be written to\n\ |
| 149 | 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] | 150 | history truncation."); |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 151 | |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 152 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 153 | /* Get history length */ |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 154 | |
| 155 | static PyObject* |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 156 | get_history_length(PyObject *self, PyObject *noarg) |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 157 | { |
Hye-Shik Chang | 7a8173a | 2004-11-25 04:04:20 +0000 | [diff] [blame] | 158 | return PyInt_FromLong(_history_length); |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 161 | PyDoc_STRVAR(get_history_length_doc, |
| 162 | "get_history_length() -> int\n\ |
| 163 | return the maximum number of items that will be written to\n\ |
| 164 | the history file."); |
| 165 | |
| 166 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 167 | /* Generic hook function setter */ |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 168 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 169 | static PyObject * |
Michael W. Hudson | da6242c | 2005-03-30 11:21:53 +0000 | [diff] [blame] | 170 | set_hook(const char *funcname, PyObject **hook_var, PyObject *args) |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 171 | { |
| 172 | PyObject *function = Py_None; |
| 173 | char buf[80]; |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 174 | PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 175 | if (!PyArg_ParseTuple(args, buf, &function)) |
| 176 | return NULL; |
| 177 | if (function == Py_None) { |
| 178 | Py_XDECREF(*hook_var); |
| 179 | *hook_var = NULL; |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 180 | } |
| 181 | else if (PyCallable_Check(function)) { |
| 182 | PyObject *tmp = *hook_var; |
| 183 | Py_INCREF(function); |
| 184 | *hook_var = function; |
| 185 | Py_XDECREF(tmp); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 186 | } |
| 187 | else { |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 188 | PyOS_snprintf(buf, sizeof(buf), |
| 189 | "set_%.50s(func): argument not callable", |
| 190 | funcname); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 191 | PyErr_SetString(PyExc_TypeError, buf); |
| 192 | return NULL; |
| 193 | } |
| 194 | Py_INCREF(Py_None); |
| 195 | return Py_None; |
| 196 | } |
| 197 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 198 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 199 | /* Exported functions to specify hook functions in Python */ |
| 200 | |
| 201 | static PyObject *startup_hook = NULL; |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 202 | |
| 203 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
| 204 | static PyObject *pre_input_hook = NULL; |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 205 | #endif |
| 206 | |
| 207 | static PyObject * |
| 208 | set_startup_hook(PyObject *self, PyObject *args) |
| 209 | { |
Michael W. Hudson | da6242c | 2005-03-30 11:21:53 +0000 | [diff] [blame] | 210 | return set_hook("startup_hook", &startup_hook, args); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 213 | PyDoc_STRVAR(doc_set_startup_hook, |
| 214 | "set_startup_hook([function]) -> None\n\ |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 215 | Set or remove the startup_hook function.\n\ |
| 216 | The function is called with no arguments just\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 217 | before readline prints the first prompt."); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 218 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 219 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 220 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 221 | |
| 222 | /* Set pre-input hook */ |
| 223 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 224 | static PyObject * |
| 225 | set_pre_input_hook(PyObject *self, PyObject *args) |
| 226 | { |
Michael W. Hudson | da6242c | 2005-03-30 11:21:53 +0000 | [diff] [blame] | 227 | return set_hook("pre_input_hook", &pre_input_hook, args); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 230 | PyDoc_STRVAR(doc_set_pre_input_hook, |
| 231 | "set_pre_input_hook([function]) -> None\n\ |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 232 | Set or remove the pre_input_hook function.\n\ |
| 233 | The function is called with no arguments after the first prompt\n\ |
| 234 | 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] | 235 | characters."); |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 236 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 237 | #endif |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 238 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 239 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 240 | /* Exported function to specify a word completer in Python */ |
| 241 | |
| 242 | static PyObject *completer = NULL; |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 243 | |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 244 | static PyObject *begidx = NULL; |
| 245 | static PyObject *endidx = NULL; |
| 246 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 247 | |
| 248 | /* Get the beginning index for the scope of the tab-completion */ |
| 249 | |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 250 | static PyObject * |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 251 | get_begidx(PyObject *self, PyObject *noarg) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 252 | { |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 253 | Py_INCREF(begidx); |
| 254 | return begidx; |
| 255 | } |
| 256 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 257 | PyDoc_STRVAR(doc_get_begidx, |
| 258 | "get_begidx() -> int\n\ |
| 259 | get the beginning index of the readline tab-completion scope"); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 260 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 261 | |
| 262 | /* Get the ending index for the scope of the tab-completion */ |
| 263 | |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 264 | static PyObject * |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 265 | get_endidx(PyObject *self, PyObject *noarg) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 266 | { |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 267 | Py_INCREF(endidx); |
| 268 | return endidx; |
| 269 | } |
| 270 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 271 | PyDoc_STRVAR(doc_get_endidx, |
| 272 | "get_endidx() -> int\n\ |
| 273 | get the ending index of the readline tab-completion scope"); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 274 | |
| 275 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 276 | /* Set the tab-completion word-delimiters that readline uses */ |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 277 | |
| 278 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 279 | set_completer_delims(PyObject *self, PyObject *args) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 280 | { |
| 281 | char *break_chars; |
| 282 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 283 | if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) { |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 284 | return NULL; |
| 285 | } |
Neal Norwitz | 0e0ee59 | 2002-04-21 15:03:18 +0000 | [diff] [blame] | 286 | free((void*)rl_completer_word_break_characters); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 287 | rl_completer_word_break_characters = strdup(break_chars); |
| 288 | Py_INCREF(Py_None); |
| 289 | return Py_None; |
| 290 | } |
| 291 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 292 | PyDoc_STRVAR(doc_set_completer_delims, |
| 293 | "set_completer_delims(string) -> None\n\ |
| 294 | set the readline word delimiters for tab-completion"); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 295 | |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 296 | static PyObject * |
| 297 | py_remove_history(PyObject *self, PyObject *args) |
| 298 | { |
| 299 | int entry_number; |
| 300 | HIST_ENTRY *entry; |
| 301 | |
| 302 | if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number)) |
| 303 | return NULL; |
Martin v. Löwis | 9533e34 | 2005-02-27 20:33:25 +0000 | [diff] [blame] | 304 | if (entry_number < 0) { |
| 305 | PyErr_SetString(PyExc_ValueError, |
| 306 | "History index cannot be negative"); |
| 307 | return NULL; |
| 308 | } |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 309 | entry = remove_history(entry_number); |
| 310 | if (!entry) { |
Skip Montanaro | 6c06cd5 | 2004-08-16 16:15:13 +0000 | [diff] [blame] | 311 | PyErr_Format(PyExc_ValueError, |
| 312 | "No history item at position %d", |
| 313 | entry_number); |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 314 | return NULL; |
| 315 | } |
| 316 | /* free memory allocated for the history entry */ |
| 317 | if (entry->line) |
| 318 | free(entry->line); |
| 319 | if (entry->data) |
| 320 | free(entry->data); |
| 321 | free(entry); |
| 322 | |
| 323 | Py_INCREF(Py_None); |
| 324 | return Py_None; |
| 325 | } |
| 326 | |
| 327 | PyDoc_STRVAR(doc_remove_history, |
Skip Montanaro | 6c06cd5 | 2004-08-16 16:15:13 +0000 | [diff] [blame] | 328 | "remove_history_item(pos) -> None\n\ |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 329 | remove history item given by its position"); |
| 330 | |
| 331 | static PyObject * |
| 332 | py_replace_history(PyObject *self, PyObject *args) |
| 333 | { |
| 334 | int entry_number; |
| 335 | char *line; |
| 336 | HIST_ENTRY *old_entry; |
| 337 | |
| 338 | if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number, &line)) { |
| 339 | return NULL; |
| 340 | } |
Martin v. Löwis | 9533e34 | 2005-02-27 20:33:25 +0000 | [diff] [blame] | 341 | if (entry_number < 0) { |
| 342 | PyErr_SetString(PyExc_ValueError, |
| 343 | "History index cannot be negative"); |
| 344 | return NULL; |
| 345 | } |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 346 | old_entry = replace_history_entry(entry_number, line, (void *)NULL); |
| 347 | if (!old_entry) { |
Skip Montanaro | 6c06cd5 | 2004-08-16 16:15:13 +0000 | [diff] [blame] | 348 | PyErr_Format(PyExc_ValueError, |
| 349 | "No history item at position %d", |
| 350 | entry_number); |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 351 | return NULL; |
| 352 | } |
| 353 | /* free memory allocated for the old history entry */ |
| 354 | if (old_entry->line) |
| 355 | free(old_entry->line); |
| 356 | if (old_entry->data) |
| 357 | free(old_entry->data); |
| 358 | free(old_entry); |
| 359 | |
| 360 | Py_INCREF(Py_None); |
| 361 | return Py_None; |
| 362 | } |
| 363 | |
| 364 | PyDoc_STRVAR(doc_replace_history, |
Skip Montanaro | 6c06cd5 | 2004-08-16 16:15:13 +0000 | [diff] [blame] | 365 | "replace_history_item(pos, line) -> None\n\ |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 366 | replaces history item given by its position with contents of line"); |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 367 | |
| 368 | /* Add a line to the history buffer */ |
| 369 | |
Guido van Rossum | b6c1d52 | 2001-10-19 01:18:43 +0000 | [diff] [blame] | 370 | static PyObject * |
| 371 | py_add_history(PyObject *self, PyObject *args) |
| 372 | { |
| 373 | char *line; |
| 374 | |
| 375 | if(!PyArg_ParseTuple(args, "s:add_history", &line)) { |
| 376 | return NULL; |
| 377 | } |
| 378 | add_history(line); |
| 379 | Py_INCREF(Py_None); |
| 380 | return Py_None; |
| 381 | } |
| 382 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 383 | PyDoc_STRVAR(doc_add_history, |
| 384 | "add_history(string) -> None\n\ |
| 385 | add a line to the history buffer"); |
Guido van Rossum | b6c1d52 | 2001-10-19 01:18:43 +0000 | [diff] [blame] | 386 | |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 387 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 388 | /* Get the tab-completion word-delimiters that readline uses */ |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 389 | |
| 390 | static PyObject * |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 391 | get_completer_delims(PyObject *self, PyObject *noarg) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 392 | { |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 393 | return PyString_FromString(rl_completer_word_break_characters); |
| 394 | } |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 395 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 396 | PyDoc_STRVAR(doc_get_completer_delims, |
| 397 | "get_completer_delims() -> string\n\ |
| 398 | get the readline word delimiters for tab-completion"); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 399 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 400 | |
| 401 | /* Set the completer function */ |
| 402 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 403 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 404 | set_completer(PyObject *self, PyObject *args) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 405 | { |
Michael W. Hudson | da6242c | 2005-03-30 11:21:53 +0000 | [diff] [blame] | 406 | return set_hook("completer", &completer, args); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 409 | PyDoc_STRVAR(doc_set_completer, |
| 410 | "set_completer([function]) -> None\n\ |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 411 | Set or remove the completer function.\n\ |
| 412 | The function is called as function(text, state),\n\ |
Fred Drake | 52d55a3 | 2001-08-01 21:44:14 +0000 | [diff] [blame] | 413 | 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] | 414 | It should return the next possible completion starting with 'text'."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 415 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 416 | |
Michael W. Hudson | 796df15 | 2003-01-30 10:12:51 +0000 | [diff] [blame] | 417 | static PyObject * |
Neal Norwitz | d9efdc5 | 2003-03-01 15:19:41 +0000 | [diff] [blame] | 418 | get_completer(PyObject *self, PyObject *noargs) |
Michael W. Hudson | 796df15 | 2003-01-30 10:12:51 +0000 | [diff] [blame] | 419 | { |
| 420 | if (completer == NULL) { |
| 421 | Py_INCREF(Py_None); |
| 422 | return Py_None; |
| 423 | } |
| 424 | Py_INCREF(completer); |
| 425 | return completer; |
| 426 | } |
| 427 | |
| 428 | PyDoc_STRVAR(doc_get_completer, |
| 429 | "get_completer() -> function\n\ |
| 430 | \n\ |
| 431 | Returns current completer function."); |
| 432 | |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 433 | /* Exported function to get any element of history */ |
| 434 | |
| 435 | static PyObject * |
| 436 | get_history_item(PyObject *self, PyObject *args) |
| 437 | { |
| 438 | int idx = 0; |
| 439 | HIST_ENTRY *hist_ent; |
| 440 | |
| 441 | if (!PyArg_ParseTuple(args, "i:index", &idx)) |
| 442 | return NULL; |
| 443 | if ((hist_ent = history_get(idx))) |
Guido van Rossum | 05ac449 | 2003-01-07 20:04:12 +0000 | [diff] [blame] | 444 | return PyString_FromString(hist_ent->line); |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 445 | else { |
| 446 | Py_INCREF(Py_None); |
| 447 | return Py_None; |
| 448 | } |
| 449 | } |
| 450 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 451 | PyDoc_STRVAR(doc_get_history_item, |
| 452 | "get_history_item() -> string\n\ |
| 453 | return the current contents of history item at index."); |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 454 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 455 | |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 456 | /* Exported function to get current length of history */ |
| 457 | |
| 458 | static PyObject * |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 459 | get_current_history_length(PyObject *self, PyObject *noarg) |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 460 | { |
| 461 | HISTORY_STATE *hist_st; |
| 462 | |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 463 | hist_st = history_get_history_state(); |
| 464 | return PyInt_FromLong(hist_st ? (long) hist_st->length : (long) 0); |
| 465 | } |
| 466 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 467 | PyDoc_STRVAR(doc_get_current_history_length, |
| 468 | "get_current_history_length() -> integer\n\ |
| 469 | return the current (not the maximum) length of history."); |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 470 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 471 | |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 472 | /* Exported function to read the current line buffer */ |
| 473 | |
| 474 | static PyObject * |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 475 | get_line_buffer(PyObject *self, PyObject *noarg) |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 476 | { |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 477 | return PyString_FromString(rl_line_buffer); |
| 478 | } |
| 479 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 480 | PyDoc_STRVAR(doc_get_line_buffer, |
| 481 | "get_line_buffer() -> string\n\ |
| 482 | return the current contents of the line buffer."); |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 483 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 484 | |
Martin v. Löwis | e7a9796 | 2003-09-20 16:08:33 +0000 | [diff] [blame] | 485 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER |
| 486 | |
| 487 | /* Exported function to clear the current history */ |
| 488 | |
| 489 | static PyObject * |
| 490 | py_clear_history(PyObject *self, PyObject *noarg) |
| 491 | { |
| 492 | clear_history(); |
| 493 | Py_INCREF(Py_None); |
| 494 | return Py_None; |
| 495 | } |
| 496 | |
| 497 | PyDoc_STRVAR(doc_clear_history, |
| 498 | "clear_history() -> None\n\ |
| 499 | Clear the current readline history."); |
| 500 | #endif |
| 501 | |
| 502 | |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 503 | /* Exported function to insert text into the line buffer */ |
| 504 | |
| 505 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 506 | insert_text(PyObject *self, PyObject *args) |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 507 | { |
| 508 | char *s; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 509 | if (!PyArg_ParseTuple(args, "s:insert_text", &s)) |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 510 | return NULL; |
| 511 | rl_insert_text(s); |
| 512 | Py_INCREF(Py_None); |
| 513 | return Py_None; |
| 514 | } |
| 515 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 516 | PyDoc_STRVAR(doc_insert_text, |
| 517 | "insert_text(string) -> None\n\ |
| 518 | Insert text into the command line."); |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 519 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 520 | |
| 521 | /* Redisplay the line buffer */ |
| 522 | |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 523 | static PyObject * |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 524 | redisplay(PyObject *self, PyObject *noarg) |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 525 | { |
| 526 | rl_redisplay(); |
| 527 | Py_INCREF(Py_None); |
| 528 | return Py_None; |
| 529 | } |
| 530 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 531 | PyDoc_STRVAR(doc_redisplay, |
| 532 | "redisplay() -> None\n\ |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 533 | 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] | 534 | contents of the line buffer."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 535 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 536 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 537 | /* Table of functions exported by the module */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 538 | |
| 539 | static struct PyMethodDef readline_methods[] = |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 540 | { |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 541 | {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind}, |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 542 | {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer}, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 543 | {"insert_text", insert_text, METH_VARARGS, doc_insert_text}, |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 544 | {"redisplay", redisplay, METH_NOARGS, doc_redisplay}, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 545 | {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file}, |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 546 | {"read_history_file", read_history_file, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 547 | METH_VARARGS, doc_read_history_file}, |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 548 | {"write_history_file", write_history_file, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 549 | METH_VARARGS, doc_write_history_file}, |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 550 | {"get_history_item", get_history_item, |
| 551 | METH_VARARGS, doc_get_history_item}, |
Neal Norwitz | 767f835 | 2002-03-31 16:13:39 +0000 | [diff] [blame] | 552 | {"get_current_history_length", (PyCFunction)get_current_history_length, |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 553 | METH_NOARGS, doc_get_current_history_length}, |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 554 | {"set_history_length", set_history_length, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 555 | METH_VARARGS, set_history_length_doc}, |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 556 | {"get_history_length", get_history_length, |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 557 | METH_NOARGS, get_history_length_doc}, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 558 | {"set_completer", set_completer, METH_VARARGS, doc_set_completer}, |
Michael W. Hudson | 796df15 | 2003-01-30 10:12:51 +0000 | [diff] [blame] | 559 | {"get_completer", get_completer, METH_NOARGS, doc_get_completer}, |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 560 | {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx}, |
| 561 | {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx}, |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 562 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 563 | {"set_completer_delims", set_completer_delims, |
Andrew M. Kuchling | a1abb72 | 2000-08-03 02:34:44 +0000 | [diff] [blame] | 564 | METH_VARARGS, doc_set_completer_delims}, |
Guido van Rossum | b6c1d52 | 2001-10-19 01:18:43 +0000 | [diff] [blame] | 565 | {"add_history", py_add_history, METH_VARARGS, doc_add_history}, |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 566 | {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history}, |
| 567 | {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history}, |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 568 | {"get_completer_delims", get_completer_delims, |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 569 | METH_NOARGS, doc_get_completer_delims}, |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 570 | |
| 571 | {"set_startup_hook", set_startup_hook, |
| 572 | METH_VARARGS, doc_set_startup_hook}, |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 573 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 574 | {"set_pre_input_hook", set_pre_input_hook, |
| 575 | METH_VARARGS, doc_set_pre_input_hook}, |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 576 | #endif |
Martin v. Löwis | e7a9796 | 2003-09-20 16:08:33 +0000 | [diff] [blame] | 577 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER |
| 578 | {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history}, |
| 579 | #endif |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 580 | {0, 0} |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 581 | }; |
| 582 | |
Guido van Rossum | 05ac449 | 2003-01-07 20:04:12 +0000 | [diff] [blame] | 583 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 584 | /* C function to call the Python hooks. */ |
| 585 | |
| 586 | static int |
Michael W. Hudson | da6242c | 2005-03-30 11:21:53 +0000 | [diff] [blame] | 587 | on_hook(PyObject *func) |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 588 | { |
| 589 | int result = 0; |
| 590 | if (func != NULL) { |
| 591 | PyObject *r; |
Michael W. Hudson | da6242c | 2005-03-30 11:21:53 +0000 | [diff] [blame] | 592 | #ifdef WITH_THREAD |
| 593 | PyGILState_STATE gilstate = PyGILState_Ensure(); |
| 594 | #endif |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 595 | r = PyObject_CallFunction(func, NULL); |
| 596 | if (r == NULL) |
| 597 | goto error; |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 598 | if (r == Py_None) |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 599 | result = 0; |
Michael W. Hudson | da6242c | 2005-03-30 11:21:53 +0000 | [diff] [blame] | 600 | else { |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 601 | result = PyInt_AsLong(r); |
Michael W. Hudson | da6242c | 2005-03-30 11:21:53 +0000 | [diff] [blame] | 602 | if (result == -1 && PyErr_Occurred()) |
| 603 | goto error; |
| 604 | } |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 605 | Py_DECREF(r); |
| 606 | goto done; |
| 607 | error: |
| 608 | PyErr_Clear(); |
| 609 | Py_XDECREF(r); |
| 610 | done: |
Michael W. Hudson | da6242c | 2005-03-30 11:21:53 +0000 | [diff] [blame] | 611 | #ifdef WITH_THREAD |
| 612 | PyGILState_Release(gilstate); |
| 613 | #endif |
Georg Brandl | e677adc | 2005-09-29 13:40:49 +0000 | [diff] [blame] | 614 | return result; |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 615 | } |
| 616 | return result; |
| 617 | } |
| 618 | |
| 619 | static int |
| 620 | on_startup_hook(void) |
| 621 | { |
Michael W. Hudson | da6242c | 2005-03-30 11:21:53 +0000 | [diff] [blame] | 622 | return on_hook(startup_hook); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
| 626 | static int |
| 627 | on_pre_input_hook(void) |
| 628 | { |
Michael W. Hudson | da6242c | 2005-03-30 11:21:53 +0000 | [diff] [blame] | 629 | return on_hook(pre_input_hook); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 630 | } |
| 631 | #endif |
| 632 | |
Guido van Rossum | 05ac449 | 2003-01-07 20:04:12 +0000 | [diff] [blame] | 633 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 634 | /* C function to call the Python completer. */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 635 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 636 | static char * |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 637 | on_completion(const char *text, int state) |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 638 | { |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 639 | char *result = NULL; |
| 640 | if (completer != NULL) { |
| 641 | PyObject *r; |
Michael W. Hudson | da6242c | 2005-03-30 11:21:53 +0000 | [diff] [blame] | 642 | #ifdef WITH_THREAD |
| 643 | PyGILState_STATE gilstate = PyGILState_Ensure(); |
| 644 | #endif |
Michael W. Hudson | 0c1ceaf | 2002-02-13 11:58:25 +0000 | [diff] [blame] | 645 | rl_attempted_completion_over = 1; |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 646 | r = PyObject_CallFunction(completer, "si", text, state); |
| 647 | if (r == NULL) |
| 648 | goto error; |
| 649 | if (r == Py_None) { |
| 650 | result = NULL; |
| 651 | } |
| 652 | else { |
| 653 | char *s = PyString_AsString(r); |
| 654 | if (s == NULL) |
| 655 | goto error; |
| 656 | result = strdup(s); |
| 657 | } |
| 658 | Py_DECREF(r); |
| 659 | goto done; |
| 660 | error: |
| 661 | PyErr_Clear(); |
| 662 | Py_XDECREF(r); |
| 663 | done: |
Michael W. Hudson | da6242c | 2005-03-30 11:21:53 +0000 | [diff] [blame] | 664 | #ifdef WITH_THREAD |
| 665 | PyGILState_Release(gilstate); |
| 666 | #endif |
Georg Brandl | e677adc | 2005-09-29 13:40:49 +0000 | [diff] [blame] | 667 | return result; |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 668 | } |
| 669 | return result; |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 672 | |
Guido van Rossum | 6d0d365 | 2003-01-07 20:34:19 +0000 | [diff] [blame] | 673 | /* A more flexible constructor that saves the "begidx" and "endidx" |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 674 | * before calling the normal completer */ |
| 675 | |
Neal Norwitz | c355f0c | 2003-02-21 00:30:18 +0000 | [diff] [blame] | 676 | static char ** |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 677 | flex_complete(char *text, int start, int end) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 678 | { |
| 679 | Py_XDECREF(begidx); |
| 680 | Py_XDECREF(endidx); |
| 681 | begidx = PyInt_FromLong((long) start); |
| 682 | endidx = PyInt_FromLong((long) end); |
| 683 | return completion_matches(text, *on_completion); |
| 684 | } |
| 685 | |
Guido van Rossum | 05ac449 | 2003-01-07 20:04:12 +0000 | [diff] [blame] | 686 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 687 | /* Helper to initialize GNU readline properly. */ |
| 688 | |
| 689 | static void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 690 | setup_readline(void) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 691 | { |
Guido van Rossum | 60c8a3a | 2002-10-09 21:27:33 +0000 | [diff] [blame] | 692 | #ifdef SAVE_LOCALE |
Martin v. Löwis | f56d015 | 2003-11-13 07:43:21 +0000 | [diff] [blame] | 693 | char *saved_locale = strdup(setlocale(LC_CTYPE, NULL)); |
Martin v. Löwis | 701abe7 | 2004-08-20 06:26:59 +0000 | [diff] [blame] | 694 | if (!saved_locale) |
| 695 | Py_FatalError("not enough memory to save locale"); |
Guido van Rossum | 60c8a3a | 2002-10-09 21:27:33 +0000 | [diff] [blame] | 696 | #endif |
| 697 | |
Skip Montanaro | a039274 | 2002-06-11 14:32:46 +0000 | [diff] [blame] | 698 | using_history(); |
| 699 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 700 | rl_readline_name = "python"; |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 701 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 702 | /* Allow $if term= in .inputrc to work */ |
| 703 | rl_terminal_name = getenv("TERM"); |
| 704 | #endif |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 705 | /* Force rebind of TAB to insert-tab */ |
| 706 | rl_bind_key('\t', rl_insert); |
| 707 | /* Bind both ESC-TAB and ESC-ESC to the completion function */ |
| 708 | rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap); |
| 709 | rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 710 | /* Set our hook functions */ |
| 711 | rl_startup_hook = (Function *)on_startup_hook; |
| 712 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
| 713 | rl_pre_input_hook = (Function *)on_pre_input_hook; |
| 714 | #endif |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 715 | /* Set our completion function */ |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 716 | rl_attempted_completion_function = (CPPFunction *)flex_complete; |
Guido van Rossum | b6c935a | 1997-09-26 23:00:37 +0000 | [diff] [blame] | 717 | /* Set Python word break characters */ |
| 718 | rl_completer_word_break_characters = |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 719 | strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?"); |
Guido van Rossum | b6c935a | 1997-09-26 23:00:37 +0000 | [diff] [blame] | 720 | /* All nonalphanums except '.' */ |
Guido van Rossum | faf5e4d | 2002-12-30 16:25:41 +0000 | [diff] [blame] | 721 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER |
Guido van Rossum | 84271bb | 2002-05-30 15:41:56 +0000 | [diff] [blame] | 722 | rl_completion_append_character ='\0'; |
Guido van Rossum | faf5e4d | 2002-12-30 16:25:41 +0000 | [diff] [blame] | 723 | #endif |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 724 | |
| 725 | begidx = PyInt_FromLong(0L); |
| 726 | endidx = PyInt_FromLong(0L); |
Barry Warsaw | f761287 | 1999-01-29 21:55:03 +0000 | [diff] [blame] | 727 | /* Initialize (allows .inputrc to override) |
| 728 | * |
| 729 | * XXX: A bug in the readline-2.2 library causes a memory leak |
| 730 | * inside this function. Nothing we can do about it. |
| 731 | */ |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 732 | rl_initialize(); |
Guido van Rossum | 60c8a3a | 2002-10-09 21:27:33 +0000 | [diff] [blame] | 733 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 734 | RESTORE_LOCALE(saved_locale) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 737 | /* Wrapper around GNU readline that handles signals differently. */ |
| 738 | |
| 739 | |
| 740 | #if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) |
| 741 | |
| 742 | static char *completed_input_string; |
| 743 | static void |
| 744 | rlhandler(char *text) |
| 745 | { |
| 746 | completed_input_string = text; |
| 747 | rl_callback_handler_remove(); |
| 748 | } |
| 749 | |
| 750 | extern PyThreadState* _PyOS_ReadlineTState; |
| 751 | |
| 752 | static char * |
| 753 | readline_until_enter_or_signal(char *prompt, int *signal) |
| 754 | { |
| 755 | char * not_done_reading = ""; |
| 756 | fd_set selectset; |
| 757 | |
| 758 | *signal = 0; |
| 759 | #ifdef HAVE_RL_CATCH_SIGNAL |
| 760 | rl_catch_signals = 0; |
| 761 | #endif |
| 762 | |
| 763 | rl_callback_handler_install (prompt, rlhandler); |
| 764 | FD_ZERO(&selectset); |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 765 | |
| 766 | completed_input_string = not_done_reading; |
| 767 | |
Michael W. Hudson | 8da2b01 | 2004-10-07 13:46:33 +0000 | [diff] [blame] | 768 | while (completed_input_string == not_done_reading) { |
| 769 | int has_input = 0; |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 770 | |
Michael W. Hudson | 8da2b01 | 2004-10-07 13:46:33 +0000 | [diff] [blame] | 771 | while (!has_input) |
| 772 | { struct timeval timeout = {0, 100000}; /* 0.1 seconds */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 773 | |
| 774 | /* [Bug #1552726] Only limit the pause if an input hook has been |
| 775 | defined. */ |
| 776 | struct timeval *timeoutp = NULL; |
| 777 | if (PyOS_InputHook) |
| 778 | timeoutp = &timeout; |
Michael W. Hudson | 8da2b01 | 2004-10-07 13:46:33 +0000 | [diff] [blame] | 779 | FD_SET(fileno(rl_instream), &selectset); |
| 780 | /* select resets selectset if no input was available */ |
| 781 | has_input = select(fileno(rl_instream) + 1, &selectset, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 782 | NULL, NULL, timeoutp); |
Michael W. Hudson | 8da2b01 | 2004-10-07 13:46:33 +0000 | [diff] [blame] | 783 | if(PyOS_InputHook) PyOS_InputHook(); |
| 784 | } |
| 785 | |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 786 | if(has_input > 0) { |
| 787 | rl_callback_read_char(); |
| 788 | } |
| 789 | else if (errno == EINTR) { |
| 790 | int s; |
Michael W. Hudson | e3afc59 | 2005-04-07 10:11:19 +0000 | [diff] [blame] | 791 | #ifdef WITH_THREAD |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 792 | PyEval_RestoreThread(_PyOS_ReadlineTState); |
Michael W. Hudson | e3afc59 | 2005-04-07 10:11:19 +0000 | [diff] [blame] | 793 | #endif |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 794 | s = PyErr_CheckSignals(); |
Michael W. Hudson | e3afc59 | 2005-04-07 10:11:19 +0000 | [diff] [blame] | 795 | #ifdef WITH_THREAD |
Michael W. Hudson | 2384990 | 2004-07-08 15:28:26 +0000 | [diff] [blame] | 796 | PyEval_SaveThread(); |
Michael W. Hudson | e3afc59 | 2005-04-07 10:11:19 +0000 | [diff] [blame] | 797 | #endif |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 798 | if (s < 0) { |
| 799 | rl_free_line_state(); |
| 800 | rl_cleanup_after_signal(); |
| 801 | rl_callback_handler_remove(); |
| 802 | *signal = 1; |
| 803 | completed_input_string = NULL; |
| 804 | } |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | return completed_input_string; |
| 809 | } |
| 810 | |
| 811 | |
| 812 | #else |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 813 | |
| 814 | /* Interrupt handler */ |
| 815 | |
| 816 | static jmp_buf jbuf; |
| 817 | |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 818 | /* ARGSUSED */ |
Tim Peters | 4f1b208 | 2000-07-23 21:18:09 +0000 | [diff] [blame] | 819 | static void |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 820 | onintr(int sig) |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 821 | { |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 822 | longjmp(jbuf, 1); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 823 | } |
| 824 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 825 | |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 826 | static char * |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 827 | readline_until_enter_or_signal(char *prompt, int *signal) |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 828 | { |
Guido van Rossum | 174efc9 | 2000-09-16 16:37:53 +0000 | [diff] [blame] | 829 | PyOS_sighandler_t old_inthandler; |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 830 | char *p; |
| 831 | |
| 832 | *signal = 0; |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 833 | |
Guido van Rossum | 174efc9 | 2000-09-16 16:37:53 +0000 | [diff] [blame] | 834 | old_inthandler = PyOS_setsig(SIGINT, onintr); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 835 | if (setjmp(jbuf)) { |
| 836 | #ifdef HAVE_SIGRELSE |
| 837 | /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */ |
| 838 | sigrelse(SIGINT); |
| 839 | #endif |
Guido van Rossum | 174efc9 | 2000-09-16 16:37:53 +0000 | [diff] [blame] | 840 | PyOS_setsig(SIGINT, old_inthandler); |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 841 | *signal = 1; |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 842 | return NULL; |
| 843 | } |
Michael W. Hudson | 8da2b01 | 2004-10-07 13:46:33 +0000 | [diff] [blame] | 844 | rl_event_hook = PyOS_InputHook; |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 845 | p = readline(prompt); |
| 846 | PyOS_setsig(SIGINT, old_inthandler); |
| 847 | |
| 848 | return p; |
| 849 | } |
| 850 | #endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */ |
| 851 | |
| 852 | |
| 853 | static char * |
| 854 | call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt) |
| 855 | { |
Neal Norwitz | 1fa040b | 2004-08-25 01:20:18 +0000 | [diff] [blame] | 856 | size_t n; |
| 857 | char *p, *q; |
| 858 | int signal; |
| 859 | |
Martin v. Löwis | 78a8acc | 2004-08-18 13:34:00 +0000 | [diff] [blame] | 860 | #ifdef SAVE_LOCALE |
| 861 | char *saved_locale = strdup(setlocale(LC_CTYPE, NULL)); |
Martin v. Löwis | 701abe7 | 2004-08-20 06:26:59 +0000 | [diff] [blame] | 862 | if (!saved_locale) |
| 863 | Py_FatalError("not enough memory to save locale"); |
Martin v. Löwis | 78a8acc | 2004-08-18 13:34:00 +0000 | [diff] [blame] | 864 | setlocale(LC_CTYPE, ""); |
| 865 | #endif |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 866 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 867 | if (sys_stdin != rl_instream || sys_stdout != rl_outstream) { |
| 868 | rl_instream = sys_stdin; |
| 869 | rl_outstream = sys_stdout; |
Guido van Rossum | faf5e4d | 2002-12-30 16:25:41 +0000 | [diff] [blame] | 870 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 871 | rl_prep_terminal (1); |
Guido van Rossum | faf5e4d | 2002-12-30 16:25:41 +0000 | [diff] [blame] | 872 | #endif |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 873 | } |
| 874 | |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 875 | p = readline_until_enter_or_signal(prompt, &signal); |
| 876 | |
| 877 | /* we got an interrupt signal */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 878 | if (signal) { |
| 879 | RESTORE_LOCALE(saved_locale) |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 880 | return NULL; |
| 881 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 882 | |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 883 | /* We got an EOF, return a empty string. */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 884 | if (p == NULL) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 885 | p = PyMem_Malloc(1); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 886 | if (p != NULL) |
| 887 | *p = '\0'; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 888 | RESTORE_LOCALE(saved_locale) |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 889 | return p; |
| 890 | } |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 891 | |
| 892 | /* we have a valid line */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 893 | n = strlen(p); |
Skip Montanaro | a039274 | 2002-06-11 14:32:46 +0000 | [diff] [blame] | 894 | if (n > 0) { |
| 895 | char *line; |
| 896 | HISTORY_STATE *state = history_get_history_state(); |
| 897 | if (state->length > 0) |
| 898 | line = history_get(state->length)->line; |
| 899 | else |
| 900 | line = ""; |
| 901 | if (strcmp(p, line)) |
| 902 | add_history(p); |
| 903 | /* the history docs don't say so, but the address of state |
| 904 | changes each time history_get_history_state is called |
| 905 | which makes me think it's freshly malloc'd memory... |
| 906 | on the other hand, the address of the last line stays the |
| 907 | same as long as history isn't extended, so it appears to |
| 908 | be malloc'd but managed by the history package... */ |
| 909 | free(state); |
| 910 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 911 | /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and |
| 912 | release the original. */ |
| 913 | q = p; |
| 914 | p = PyMem_Malloc(n+2); |
| 915 | if (p != NULL) { |
| 916 | strncpy(p, q, n); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 917 | p[n] = '\n'; |
| 918 | p[n+1] = '\0'; |
| 919 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 920 | free(q); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 921 | RESTORE_LOCALE(saved_locale) |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 922 | return p; |
| 923 | } |
| 924 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 925 | |
| 926 | /* Initialize the module */ |
| 927 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 928 | PyDoc_STRVAR(doc_module, |
| 929 | "Importing this module enables command line editing using GNU readline."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 930 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 931 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 932 | initreadline(void) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 933 | { |
Guido van Rossum | 1ea64ea | 2000-10-02 15:53:08 +0000 | [diff] [blame] | 934 | PyObject *m; |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 935 | |
| 936 | m = Py_InitModule4("readline", readline_methods, doc_module, |
| 937 | (PyObject *)NULL, PYTHON_API_VERSION); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 938 | if (m == NULL) |
| 939 | return; |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 940 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 941 | PyOS_ReadlineFunctionPointer = call_readline; |
| 942 | setup_readline(); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 943 | } |