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 |
| 3 | * Center. The completer interface was inspired by Lele Gaifax. |
| 4 | * |
| 5 | * More recently, it was largely rewritten by Guido van Rossum who is |
| 6 | * now maintaining it. |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 9 | /* Standard definitions */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 10 | #include "Python.h" |
| 11 | #include <setjmp.h> |
| 12 | #include <signal.h> |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 13 | #include <errno.h> |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | 60c8a3a | 2002-10-09 21:27:33 +0000 | [diff] [blame] | 15 | #if defined(HAVE_LOCALE_H) && defined(HAVE_SETLOCALE) |
| 16 | /* GNU readline() mistakenly sets the LC_CTYPE locale. |
| 17 | * This is evil. Only the user or the app's main() should do this! |
| 18 | * We must save and restore the locale around the rl_initialize() call. |
| 19 | */ |
| 20 | #define SAVE_LOCALE |
| 21 | #include <locale.h> |
| 22 | #endif |
| 23 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 24 | /* GNU readline definitions */ |
Guido van Rossum | b0e51b2 | 2001-04-13 18:14:27 +0000 | [diff] [blame] | 25 | #undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */ |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 26 | #include <readline/readline.h> |
| 27 | #include <readline/history.h> |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | 353ae58 | 2001-07-10 16:45:32 +0000 | [diff] [blame] | 29 | #ifdef HAVE_RL_COMPLETION_MATCHES |
| 30 | #define completion_matches(x, y) rl_completion_matches((x), ((rl_compentry_func_t *)(y))) |
| 31 | #endif |
| 32 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 33 | /* Pointers needed from outside (but not declared in a header file). */ |
Guido van Rossum | 5a53019 | 2001-01-10 21:03:32 +0000 | [diff] [blame] | 34 | extern DL_IMPORT(int) (*PyOS_InputHook)(void); |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 35 | extern DL_IMPORT(char) *(*PyOS_ReadlineFunctionPointer)(FILE *, FILE *,char *); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 38 | /* Exported function to send one line to readline's init file parser */ |
| 39 | |
| 40 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 41 | parse_and_bind(PyObject *self, PyObject *args) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 42 | { |
Guido van Rossum | 3b5330e | 1998-12-04 15:34:39 +0000 | [diff] [blame] | 43 | char *s, *copy; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 44 | if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s)) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 45 | return NULL; |
Guido van Rossum | 3b5330e | 1998-12-04 15:34:39 +0000 | [diff] [blame] | 46 | /* Make a copy -- rl_parse_and_bind() modifies its argument */ |
| 47 | /* Bernard Herzog */ |
| 48 | copy = malloc(1 + strlen(s)); |
| 49 | if (copy == NULL) |
| 50 | return PyErr_NoMemory(); |
| 51 | strcpy(copy, s); |
| 52 | rl_parse_and_bind(copy); |
| 53 | free(copy); /* Free the copy */ |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 54 | Py_INCREF(Py_None); |
| 55 | return Py_None; |
| 56 | } |
| 57 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 58 | PyDoc_STRVAR(doc_parse_and_bind, |
| 59 | "parse_and_bind(string) -> None\n\ |
| 60 | Parse and execute single line of a readline init file."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 61 | |
| 62 | |
| 63 | /* Exported function to parse a readline init file */ |
| 64 | |
| 65 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 66 | read_init_file(PyObject *self, PyObject *args) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 67 | { |
| 68 | char *s = NULL; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 69 | if (!PyArg_ParseTuple(args, "|z:read_init_file", &s)) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 70 | return NULL; |
| 71 | errno = rl_read_init_file(s); |
| 72 | if (errno) |
| 73 | return PyErr_SetFromErrno(PyExc_IOError); |
| 74 | Py_INCREF(Py_None); |
| 75 | return Py_None; |
| 76 | } |
| 77 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 78 | PyDoc_STRVAR(doc_read_init_file, |
| 79 | "read_init_file([filename]) -> None\n\ |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 80 | Parse a readline initialization file.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 81 | The default filename is the last filename used."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 82 | |
| 83 | |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 84 | /* Exported function to load a readline history file */ |
| 85 | |
| 86 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 87 | read_history_file(PyObject *self, PyObject *args) |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 88 | { |
| 89 | char *s = NULL; |
| 90 | if (!PyArg_ParseTuple(args, "|z:read_history_file", &s)) |
| 91 | return NULL; |
| 92 | errno = read_history(s); |
| 93 | if (errno) |
| 94 | return PyErr_SetFromErrno(PyExc_IOError); |
| 95 | Py_INCREF(Py_None); |
| 96 | return Py_None; |
| 97 | } |
| 98 | |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 99 | 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] | 100 | PyDoc_STRVAR(doc_read_history_file, |
| 101 | "read_history_file([filename]) -> None\n\ |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 102 | Load a readline history file.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 103 | The default filename is ~/.history."); |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 104 | |
| 105 | |
| 106 | /* Exported function to save a readline history file */ |
| 107 | |
| 108 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 109 | write_history_file(PyObject *self, PyObject *args) |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 110 | { |
| 111 | char *s = NULL; |
| 112 | if (!PyArg_ParseTuple(args, "|z:write_history_file", &s)) |
| 113 | return NULL; |
| 114 | errno = write_history(s); |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 115 | if (!errno && history_length >= 0) |
| 116 | history_truncate_file(s, history_length); |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 117 | if (errno) |
| 118 | return PyErr_SetFromErrno(PyExc_IOError); |
| 119 | Py_INCREF(Py_None); |
| 120 | return Py_None; |
| 121 | } |
| 122 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 123 | PyDoc_STRVAR(doc_write_history_file, |
| 124 | "write_history_file([filename]) -> None\n\ |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 125 | Save a readline history file.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 126 | The default filename is ~/.history."); |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 127 | |
| 128 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 129 | PyDoc_STRVAR(set_history_length_doc, |
| 130 | "set_history_length(length) -> None\n\ |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 131 | set the maximal number of items which will be written to\n\ |
| 132 | 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] | 133 | history truncation."); |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 134 | |
| 135 | static PyObject* |
| 136 | set_history_length(PyObject *self, PyObject *args) |
| 137 | { |
| 138 | int length = history_length; |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 139 | if (!PyArg_ParseTuple(args, "i:set_history_length", &length)) |
| 140 | return NULL; |
| 141 | history_length = length; |
| 142 | Py_INCREF(Py_None); |
| 143 | return Py_None; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 148 | PyDoc_STRVAR(get_history_length_doc, |
| 149 | "get_history_length() -> int\n\ |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 150 | return the maximum number of items that will be written to\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 151 | the history file."); |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 152 | |
| 153 | static PyObject* |
| 154 | get_history_length(PyObject *self, PyObject *args) |
| 155 | { |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 156 | if (!PyArg_ParseTuple(args, ":get_history_length")) |
| 157 | return NULL; |
| 158 | return Py_BuildValue("i", history_length); |
| 159 | } |
| 160 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 161 | /* Generic hook function setter */ |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 162 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 163 | static PyObject * |
| 164 | set_hook(const char * funcname, PyObject **hook_var, PyThreadState **tstate, PyObject *args) |
| 165 | { |
| 166 | PyObject *function = Py_None; |
| 167 | char buf[80]; |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 168 | PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 169 | if (!PyArg_ParseTuple(args, buf, &function)) |
| 170 | return NULL; |
| 171 | if (function == Py_None) { |
| 172 | Py_XDECREF(*hook_var); |
| 173 | *hook_var = NULL; |
| 174 | *tstate = NULL; |
| 175 | } |
| 176 | else if (PyCallable_Check(function)) { |
| 177 | PyObject *tmp = *hook_var; |
| 178 | Py_INCREF(function); |
| 179 | *hook_var = function; |
| 180 | Py_XDECREF(tmp); |
| 181 | *tstate = PyThreadState_Get(); |
| 182 | } |
| 183 | else { |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 184 | PyOS_snprintf(buf, sizeof(buf), |
| 185 | "set_%.50s(func): argument not callable", |
| 186 | funcname); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 187 | PyErr_SetString(PyExc_TypeError, buf); |
| 188 | return NULL; |
| 189 | } |
| 190 | Py_INCREF(Py_None); |
| 191 | return Py_None; |
| 192 | } |
| 193 | |
| 194 | /* Exported functions to specify hook functions in Python */ |
| 195 | |
| 196 | static PyObject *startup_hook = NULL; |
| 197 | static PyThreadState *startup_hook_tstate = NULL; |
| 198 | |
| 199 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
| 200 | static PyObject *pre_input_hook = NULL; |
| 201 | static PyThreadState *pre_input_hook_tstate = NULL; |
| 202 | #endif |
| 203 | |
| 204 | static PyObject * |
| 205 | set_startup_hook(PyObject *self, PyObject *args) |
| 206 | { |
| 207 | return set_hook("startup_hook", &startup_hook, &startup_hook_tstate, args); |
| 208 | } |
| 209 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 210 | PyDoc_STRVAR(doc_set_startup_hook, |
| 211 | "set_startup_hook([function]) -> None\n\ |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 212 | Set or remove the startup_hook function.\n\ |
| 213 | The function is called with no arguments just\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 214 | before readline prints the first prompt."); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 215 | |
| 216 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
| 217 | static PyObject * |
| 218 | set_pre_input_hook(PyObject *self, PyObject *args) |
| 219 | { |
| 220 | return set_hook("pre_input_hook", &pre_input_hook, &pre_input_hook_tstate, args); |
| 221 | } |
| 222 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 223 | PyDoc_STRVAR(doc_set_pre_input_hook, |
| 224 | "set_pre_input_hook([function]) -> None\n\ |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 225 | Set or remove the pre_input_hook function.\n\ |
| 226 | The function is called with no arguments after the first prompt\n\ |
| 227 | 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] | 228 | characters."); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 229 | #endif |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 230 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 231 | /* Exported function to specify a word completer in Python */ |
| 232 | |
| 233 | static PyObject *completer = NULL; |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 234 | static PyThreadState *completer_tstate = NULL; |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 235 | |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 236 | static PyObject *begidx = NULL; |
| 237 | static PyObject *endidx = NULL; |
| 238 | |
| 239 | /* get the beginning index for the scope of the tab-completion */ |
| 240 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 241 | get_begidx(PyObject *self) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 242 | { |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 243 | Py_INCREF(begidx); |
| 244 | return begidx; |
| 245 | } |
| 246 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 247 | PyDoc_STRVAR(doc_get_begidx, |
| 248 | "get_begidx() -> int\n\ |
| 249 | get the beginning index of the readline tab-completion scope"); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 250 | |
| 251 | /* get the ending index for the scope of the tab-completion */ |
| 252 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 253 | get_endidx(PyObject *self) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 254 | { |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 255 | Py_INCREF(endidx); |
| 256 | return endidx; |
| 257 | } |
| 258 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 259 | PyDoc_STRVAR(doc_get_endidx, |
| 260 | "get_endidx() -> int\n\ |
| 261 | get the ending index of the readline tab-completion scope"); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 262 | |
| 263 | |
| 264 | /* set the tab-completion word-delimiters that readline uses */ |
| 265 | |
| 266 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 267 | set_completer_delims(PyObject *self, PyObject *args) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 268 | { |
| 269 | char *break_chars; |
| 270 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 271 | if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) { |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 272 | return NULL; |
| 273 | } |
Neal Norwitz | 0e0ee59 | 2002-04-21 15:03:18 +0000 | [diff] [blame] | 274 | free((void*)rl_completer_word_break_characters); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 275 | rl_completer_word_break_characters = strdup(break_chars); |
| 276 | Py_INCREF(Py_None); |
| 277 | return Py_None; |
| 278 | } |
| 279 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 280 | PyDoc_STRVAR(doc_set_completer_delims, |
| 281 | "set_completer_delims(string) -> None\n\ |
| 282 | set the readline word delimiters for tab-completion"); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 283 | |
Guido van Rossum | b6c1d52 | 2001-10-19 01:18:43 +0000 | [diff] [blame] | 284 | static PyObject * |
| 285 | py_add_history(PyObject *self, PyObject *args) |
| 286 | { |
| 287 | char *line; |
| 288 | |
| 289 | if(!PyArg_ParseTuple(args, "s:add_history", &line)) { |
| 290 | return NULL; |
| 291 | } |
| 292 | add_history(line); |
| 293 | Py_INCREF(Py_None); |
| 294 | return Py_None; |
| 295 | } |
| 296 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 297 | PyDoc_STRVAR(doc_add_history, |
| 298 | "add_history(string) -> None\n\ |
| 299 | add a line to the history buffer"); |
Guido van Rossum | b6c1d52 | 2001-10-19 01:18:43 +0000 | [diff] [blame] | 300 | |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 301 | |
| 302 | /* get the tab-completion word-delimiters that readline uses */ |
| 303 | |
| 304 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 305 | get_completer_delims(PyObject *self) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 306 | { |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 307 | return PyString_FromString(rl_completer_word_break_characters); |
| 308 | } |
| 309 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 310 | PyDoc_STRVAR(doc_get_completer_delims, |
| 311 | "get_completer_delims() -> string\n\ |
| 312 | get the readline word delimiters for tab-completion"); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 313 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 314 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 315 | set_completer(PyObject *self, PyObject *args) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 316 | { |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 317 | return set_hook("completer", &completer, &completer_tstate, args); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 320 | PyDoc_STRVAR(doc_set_completer, |
| 321 | "set_completer([function]) -> None\n\ |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 322 | Set or remove the completer function.\n\ |
| 323 | The function is called as function(text, state),\n\ |
Fred Drake | 52d55a3 | 2001-08-01 21:44:14 +0000 | [diff] [blame] | 324 | 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] | 325 | It should return the next possible completion starting with 'text'."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 326 | |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 327 | /* Exported function to get any element of history */ |
| 328 | |
| 329 | static PyObject * |
| 330 | get_history_item(PyObject *self, PyObject *args) |
| 331 | { |
| 332 | int idx = 0; |
| 333 | HIST_ENTRY *hist_ent; |
| 334 | |
| 335 | if (!PyArg_ParseTuple(args, "i:index", &idx)) |
| 336 | return NULL; |
| 337 | if ((hist_ent = history_get(idx))) |
| 338 | return PyString_FromString(hist_ent->line); |
| 339 | else { |
| 340 | Py_INCREF(Py_None); |
| 341 | return Py_None; |
| 342 | } |
| 343 | } |
| 344 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 345 | PyDoc_STRVAR(doc_get_history_item, |
| 346 | "get_history_item() -> string\n\ |
| 347 | return the current contents of history item at index."); |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 348 | |
| 349 | /* Exported function to get current length of history */ |
| 350 | |
| 351 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 352 | get_current_history_length(PyObject *self) |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 353 | { |
| 354 | HISTORY_STATE *hist_st; |
| 355 | |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 356 | hist_st = history_get_history_state(); |
| 357 | return PyInt_FromLong(hist_st ? (long) hist_st->length : (long) 0); |
| 358 | } |
| 359 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 360 | PyDoc_STRVAR(doc_get_current_history_length, |
| 361 | "get_current_history_length() -> integer\n\ |
| 362 | return the current (not the maximum) length of history."); |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 363 | |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 364 | /* Exported function to read the current line buffer */ |
| 365 | |
| 366 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 367 | get_line_buffer(PyObject *self) |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 368 | { |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 369 | return PyString_FromString(rl_line_buffer); |
| 370 | } |
| 371 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 372 | PyDoc_STRVAR(doc_get_line_buffer, |
| 373 | "get_line_buffer() -> string\n\ |
| 374 | return the current contents of the line buffer."); |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 375 | |
| 376 | /* Exported function to insert text into the line buffer */ |
| 377 | |
| 378 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 379 | insert_text(PyObject *self, PyObject *args) |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 380 | { |
| 381 | char *s; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 382 | if (!PyArg_ParseTuple(args, "s:insert_text", &s)) |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 383 | return NULL; |
| 384 | rl_insert_text(s); |
| 385 | Py_INCREF(Py_None); |
| 386 | return Py_None; |
| 387 | } |
| 388 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 389 | PyDoc_STRVAR(doc_insert_text, |
| 390 | "insert_text(string) -> None\n\ |
| 391 | Insert text into the command line."); |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 392 | |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 393 | static PyObject * |
| 394 | redisplay(PyObject *self) |
| 395 | { |
| 396 | rl_redisplay(); |
| 397 | Py_INCREF(Py_None); |
| 398 | return Py_None; |
| 399 | } |
| 400 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 401 | PyDoc_STRVAR(doc_redisplay, |
| 402 | "redisplay() -> None\n\ |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 403 | 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] | 404 | contents of the line buffer."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 405 | |
| 406 | /* Table of functions exported by the module */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 407 | |
| 408 | static struct PyMethodDef readline_methods[] = |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 409 | { |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 410 | {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind}, |
Neal Norwitz | 767f835 | 2002-03-31 16:13:39 +0000 | [diff] [blame] | 411 | {"get_line_buffer", (PyCFunction)get_line_buffer, |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 412 | METH_NOARGS, doc_get_line_buffer}, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 413 | {"insert_text", insert_text, METH_VARARGS, doc_insert_text}, |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 414 | {"redisplay", (PyCFunction)redisplay, METH_NOARGS, doc_redisplay}, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 415 | {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file}, |
| 416 | {"read_history_file", read_history_file, |
| 417 | METH_VARARGS, doc_read_history_file}, |
| 418 | {"write_history_file", write_history_file, |
| 419 | METH_VARARGS, doc_write_history_file}, |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 420 | {"get_history_item", get_history_item, |
| 421 | METH_VARARGS, doc_get_history_item}, |
Neal Norwitz | 767f835 | 2002-03-31 16:13:39 +0000 | [diff] [blame] | 422 | {"get_current_history_length", (PyCFunction)get_current_history_length, |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 423 | METH_NOARGS, doc_get_current_history_length}, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 424 | {"set_history_length", set_history_length, |
| 425 | METH_VARARGS, set_history_length_doc}, |
| 426 | {"get_history_length", get_history_length, |
| 427 | METH_VARARGS, get_history_length_doc}, |
| 428 | {"set_completer", set_completer, METH_VARARGS, doc_set_completer}, |
Neal Norwitz | 767f835 | 2002-03-31 16:13:39 +0000 | [diff] [blame] | 429 | {"get_begidx", (PyCFunction)get_begidx, METH_NOARGS, doc_get_begidx}, |
| 430 | {"get_endidx", (PyCFunction)get_endidx, METH_NOARGS, doc_get_endidx}, |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 431 | |
Andrew M. Kuchling | a1abb72 | 2000-08-03 02:34:44 +0000 | [diff] [blame] | 432 | {"set_completer_delims", set_completer_delims, |
| 433 | METH_VARARGS, doc_set_completer_delims}, |
Guido van Rossum | b6c1d52 | 2001-10-19 01:18:43 +0000 | [diff] [blame] | 434 | {"add_history", py_add_history, METH_VARARGS, doc_add_history}, |
Neal Norwitz | 767f835 | 2002-03-31 16:13:39 +0000 | [diff] [blame] | 435 | {"get_completer_delims", (PyCFunction)get_completer_delims, |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 436 | METH_NOARGS, doc_get_completer_delims}, |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 437 | |
| 438 | {"set_startup_hook", set_startup_hook, METH_VARARGS, doc_set_startup_hook}, |
| 439 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
| 440 | {"set_pre_input_hook", set_pre_input_hook, METH_VARARGS, doc_set_pre_input_hook}, |
| 441 | #endif |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 442 | {0, 0} |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 443 | }; |
| 444 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 445 | /* C function to call the Python hooks. */ |
| 446 | |
| 447 | static int |
| 448 | on_hook(PyObject *func, PyThreadState *tstate) |
| 449 | { |
| 450 | int result = 0; |
| 451 | if (func != NULL) { |
| 452 | PyObject *r; |
| 453 | PyThreadState *save_tstate; |
| 454 | /* Note that readline is called with the interpreter |
| 455 | lock released! */ |
| 456 | save_tstate = PyThreadState_Swap(NULL); |
| 457 | PyEval_RestoreThread(tstate); |
| 458 | r = PyObject_CallFunction(func, NULL); |
| 459 | if (r == NULL) |
| 460 | goto error; |
| 461 | if (r == Py_None) |
| 462 | result = 0; |
| 463 | else |
| 464 | result = PyInt_AsLong(r); |
| 465 | Py_DECREF(r); |
| 466 | goto done; |
| 467 | error: |
| 468 | PyErr_Clear(); |
| 469 | Py_XDECREF(r); |
| 470 | done: |
| 471 | PyEval_SaveThread(); |
| 472 | PyThreadState_Swap(save_tstate); |
| 473 | } |
| 474 | return result; |
| 475 | } |
| 476 | |
| 477 | static int |
| 478 | on_startup_hook(void) |
| 479 | { |
| 480 | return on_hook(startup_hook, startup_hook_tstate); |
| 481 | } |
| 482 | |
| 483 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
| 484 | static int |
| 485 | on_pre_input_hook(void) |
| 486 | { |
| 487 | return on_hook(pre_input_hook, pre_input_hook_tstate); |
| 488 | } |
| 489 | #endif |
| 490 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 491 | /* C function to call the Python completer. */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 492 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 493 | static char * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 494 | on_completion(char *text, int state) |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 495 | { |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 496 | char *result = NULL; |
| 497 | if (completer != NULL) { |
| 498 | PyObject *r; |
Guido van Rossum | a59406a | 1997-10-10 17:39:19 +0000 | [diff] [blame] | 499 | PyThreadState *save_tstate; |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 500 | /* Note that readline is called with the interpreter |
| 501 | lock released! */ |
Guido van Rossum | a59406a | 1997-10-10 17:39:19 +0000 | [diff] [blame] | 502 | save_tstate = PyThreadState_Swap(NULL); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 503 | PyEval_RestoreThread(completer_tstate); |
Michael W. Hudson | 0c1ceaf | 2002-02-13 11:58:25 +0000 | [diff] [blame] | 504 | /* Don't use the default filename completion if we |
| 505 | * have a custom completion function... */ |
| 506 | rl_attempted_completion_over = 1; |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 507 | r = PyObject_CallFunction(completer, "si", text, state); |
| 508 | if (r == NULL) |
| 509 | goto error; |
| 510 | if (r == Py_None) { |
| 511 | result = NULL; |
| 512 | } |
| 513 | else { |
| 514 | char *s = PyString_AsString(r); |
| 515 | if (s == NULL) |
| 516 | goto error; |
| 517 | result = strdup(s); |
| 518 | } |
| 519 | Py_DECREF(r); |
| 520 | goto done; |
| 521 | error: |
| 522 | PyErr_Clear(); |
| 523 | Py_XDECREF(r); |
| 524 | done: |
| 525 | PyEval_SaveThread(); |
Guido van Rossum | a59406a | 1997-10-10 17:39:19 +0000 | [diff] [blame] | 526 | PyThreadState_Swap(save_tstate); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 527 | } |
| 528 | return result; |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 531 | |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 532 | /* a more flexible constructor that saves the "begidx" and "endidx" |
| 533 | * before calling the normal completer */ |
| 534 | |
| 535 | char ** |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 536 | flex_complete(char *text, int start, int end) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 537 | { |
| 538 | Py_XDECREF(begidx); |
| 539 | Py_XDECREF(endidx); |
| 540 | begidx = PyInt_FromLong((long) start); |
| 541 | endidx = PyInt_FromLong((long) end); |
| 542 | return completion_matches(text, *on_completion); |
| 543 | } |
| 544 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 545 | /* Helper to initialize GNU readline properly. */ |
| 546 | |
| 547 | static void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 548 | setup_readline(void) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 549 | { |
Guido van Rossum | 60c8a3a | 2002-10-09 21:27:33 +0000 | [diff] [blame] | 550 | #ifdef SAVE_LOCALE |
| 551 | char *saved_locale = setlocale(LC_CTYPE, NULL); |
| 552 | #endif |
| 553 | |
Skip Montanaro | a039274 | 2002-06-11 14:32:46 +0000 | [diff] [blame] | 554 | using_history(); |
| 555 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 556 | rl_readline_name = "python"; |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 557 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 558 | /* Allow $if term= in .inputrc to work */ |
| 559 | rl_terminal_name = getenv("TERM"); |
| 560 | #endif |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 561 | /* Force rebind of TAB to insert-tab */ |
| 562 | rl_bind_key('\t', rl_insert); |
| 563 | /* Bind both ESC-TAB and ESC-ESC to the completion function */ |
| 564 | rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap); |
| 565 | 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] | 566 | /* Set our hook functions */ |
| 567 | rl_startup_hook = (Function *)on_startup_hook; |
| 568 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
| 569 | rl_pre_input_hook = (Function *)on_pre_input_hook; |
| 570 | #endif |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 571 | /* Set our completion function */ |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 572 | rl_attempted_completion_function = (CPPFunction *)flex_complete; |
Guido van Rossum | b6c935a | 1997-09-26 23:00:37 +0000 | [diff] [blame] | 573 | /* Set Python word break characters */ |
| 574 | rl_completer_word_break_characters = |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 575 | strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?"); |
Guido van Rossum | b6c935a | 1997-09-26 23:00:37 +0000 | [diff] [blame] | 576 | /* All nonalphanums except '.' */ |
Guido van Rossum | 84271bb | 2002-05-30 15:41:56 +0000 | [diff] [blame] | 577 | rl_completion_append_character ='\0'; |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 578 | |
| 579 | begidx = PyInt_FromLong(0L); |
| 580 | endidx = PyInt_FromLong(0L); |
Barry Warsaw | f761287 | 1999-01-29 21:55:03 +0000 | [diff] [blame] | 581 | /* Initialize (allows .inputrc to override) |
| 582 | * |
| 583 | * XXX: A bug in the readline-2.2 library causes a memory leak |
| 584 | * inside this function. Nothing we can do about it. |
| 585 | */ |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 586 | rl_initialize(); |
Guido van Rossum | 60c8a3a | 2002-10-09 21:27:33 +0000 | [diff] [blame] | 587 | |
| 588 | #ifdef SAVE_LOCALE |
| 589 | setlocale(LC_CTYPE, saved_locale); /* Restore locale */ |
| 590 | #endif |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | |
| 594 | /* Interrupt handler */ |
| 595 | |
| 596 | static jmp_buf jbuf; |
| 597 | |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 598 | /* ARGSUSED */ |
Tim Peters | 4f1b208 | 2000-07-23 21:18:09 +0000 | [diff] [blame] | 599 | static void |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 600 | onintr(int sig) |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 601 | { |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 602 | longjmp(jbuf, 1); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 605 | |
| 606 | /* Wrapper around GNU readline that handles signals differently. */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 607 | |
| 608 | static char * |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 609 | call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt) |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 610 | { |
Guido van Rossum | 26418a9 | 2000-06-28 21:30:31 +0000 | [diff] [blame] | 611 | size_t n; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 612 | char *p, *q; |
Guido van Rossum | 174efc9 | 2000-09-16 16:37:53 +0000 | [diff] [blame] | 613 | PyOS_sighandler_t old_inthandler; |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 614 | |
Guido van Rossum | 174efc9 | 2000-09-16 16:37:53 +0000 | [diff] [blame] | 615 | old_inthandler = PyOS_setsig(SIGINT, onintr); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 616 | if (setjmp(jbuf)) { |
| 617 | #ifdef HAVE_SIGRELSE |
| 618 | /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */ |
| 619 | sigrelse(SIGINT); |
| 620 | #endif |
Guido van Rossum | 174efc9 | 2000-09-16 16:37:53 +0000 | [diff] [blame] | 621 | PyOS_setsig(SIGINT, old_inthandler); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 622 | return NULL; |
| 623 | } |
Guido van Rossum | 4462064 | 1997-08-11 18:57:29 +0000 | [diff] [blame] | 624 | rl_event_hook = PyOS_InputHook; |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 625 | |
| 626 | if (sys_stdin != rl_instream || sys_stdout != rl_outstream) { |
| 627 | rl_instream = sys_stdin; |
| 628 | rl_outstream = sys_stdout; |
| 629 | rl_prep_terminal (1); |
| 630 | } |
| 631 | |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 632 | p = readline(prompt); |
Guido van Rossum | 174efc9 | 2000-09-16 16:37:53 +0000 | [diff] [blame] | 633 | PyOS_setsig(SIGINT, old_inthandler); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 634 | |
| 635 | /* We must return a buffer allocated with PyMem_Malloc. */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 636 | if (p == NULL) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 637 | p = PyMem_Malloc(1); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 638 | if (p != NULL) |
| 639 | *p = '\0'; |
| 640 | return p; |
| 641 | } |
| 642 | n = strlen(p); |
Skip Montanaro | a039274 | 2002-06-11 14:32:46 +0000 | [diff] [blame] | 643 | if (n > 0) { |
| 644 | char *line; |
| 645 | HISTORY_STATE *state = history_get_history_state(); |
| 646 | if (state->length > 0) |
| 647 | line = history_get(state->length)->line; |
| 648 | else |
| 649 | line = ""; |
| 650 | if (strcmp(p, line)) |
| 651 | add_history(p); |
| 652 | /* the history docs don't say so, but the address of state |
| 653 | changes each time history_get_history_state is called |
| 654 | which makes me think it's freshly malloc'd memory... |
| 655 | on the other hand, the address of the last line stays the |
| 656 | same as long as history isn't extended, so it appears to |
| 657 | be malloc'd but managed by the history package... */ |
| 658 | free(state); |
| 659 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 660 | /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and |
| 661 | release the original. */ |
| 662 | q = p; |
| 663 | p = PyMem_Malloc(n+2); |
| 664 | if (p != NULL) { |
| 665 | strncpy(p, q, n); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 666 | p[n] = '\n'; |
| 667 | p[n+1] = '\0'; |
| 668 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 669 | free(q); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 670 | return p; |
| 671 | } |
| 672 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 673 | |
| 674 | /* Initialize the module */ |
| 675 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 676 | PyDoc_STRVAR(doc_module, |
| 677 | "Importing this module enables command line editing using GNU readline."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 678 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 679 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 680 | initreadline(void) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 681 | { |
Guido van Rossum | 1ea64ea | 2000-10-02 15:53:08 +0000 | [diff] [blame] | 682 | PyObject *m; |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 683 | |
| 684 | m = Py_InitModule4("readline", readline_methods, doc_module, |
| 685 | (PyObject *)NULL, PYTHON_API_VERSION); |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 686 | |
| 687 | PyOS_ReadlineFunctionPointer = call_readline; |
| 688 | setup_readline(); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 689 | } |