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