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 | 73bacfc | 1998-01-19 22:05:22 +0000 | [diff] [blame] | 15 | #ifdef HAVE_UNISTD_H |
| 16 | #include <unistd.h> /* For isatty() */ |
| 17 | #endif |
| 18 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 19 | /* GNU readline definitions */ |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 20 | /* If you have string.h, you might need to add yourself to this #if... [cjh] */ |
| 21 | #if defined(__BEOS__) |
| 22 | #undef HAVE_CONFIG_H |
| 23 | /* At max warnings, we need protos for everything. [cjh] */ |
| 24 | #include <readline/readline.h> |
| 25 | #include <readline/history.h> |
| 26 | #include <unistd.h> |
| 27 | #else |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 28 | #include <readline/readline.h> /* You may need to add an -I option to Setup */ |
| 29 | |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 30 | extern int rl_parse_and_bind(); |
| 31 | extern int rl_read_init_file(); |
| 32 | extern int rl_insert_text(); |
| 33 | extern int rl_bind_key(); |
| 34 | extern int rl_bind_key_in_map(); |
| 35 | extern int rl_initialize(); |
| 36 | extern int add_history(); |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 37 | extern Function *rl_event_hook; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 38 | #endif |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 39 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 40 | /* Pointers needed from outside (but not declared in a header file). */ |
Guido van Rossum | 4462064 | 1997-08-11 18:57:29 +0000 | [diff] [blame] | 41 | extern int (*PyOS_InputHook)(); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 42 | extern char *(*PyOS_ReadlineFunctionPointer) Py_PROTO((char *)); |
| 43 | |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 45 | /* Exported function to send one line to readline's init file parser */ |
| 46 | |
| 47 | static PyObject * |
| 48 | parse_and_bind(self, args) |
| 49 | PyObject *self; |
| 50 | PyObject *args; |
| 51 | { |
Guido van Rossum | 3b5330e | 1998-12-04 15:34:39 +0000 | [diff] [blame] | 52 | char *s, *copy; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 53 | if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s)) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 54 | return NULL; |
Guido van Rossum | 3b5330e | 1998-12-04 15:34:39 +0000 | [diff] [blame] | 55 | /* Make a copy -- rl_parse_and_bind() modifies its argument */ |
| 56 | /* Bernard Herzog */ |
| 57 | copy = malloc(1 + strlen(s)); |
| 58 | if (copy == NULL) |
| 59 | return PyErr_NoMemory(); |
| 60 | strcpy(copy, s); |
| 61 | rl_parse_and_bind(copy); |
| 62 | free(copy); /* Free the copy */ |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 63 | Py_INCREF(Py_None); |
| 64 | return Py_None; |
| 65 | } |
| 66 | |
| 67 | static char doc_parse_and_bind[] = "\ |
| 68 | parse_and_bind(string) -> None\n\ |
| 69 | Parse and execute single line of a readline init file.\ |
| 70 | "; |
| 71 | |
| 72 | |
| 73 | /* Exported function to parse a readline init file */ |
| 74 | |
| 75 | static PyObject * |
| 76 | read_init_file(self, args) |
| 77 | PyObject *self; |
| 78 | PyObject *args; |
| 79 | { |
| 80 | char *s = NULL; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 81 | if (!PyArg_ParseTuple(args, "|z:read_init_file", &s)) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 82 | return NULL; |
| 83 | errno = rl_read_init_file(s); |
| 84 | if (errno) |
| 85 | return PyErr_SetFromErrno(PyExc_IOError); |
| 86 | Py_INCREF(Py_None); |
| 87 | return Py_None; |
| 88 | } |
| 89 | |
| 90 | static char doc_read_init_file[] = "\ |
| 91 | read_init_file([filename]) -> None\n\ |
| 92 | Parse a readline initialization file.\n\ |
| 93 | The default filename is the last filename used.\ |
| 94 | "; |
| 95 | |
| 96 | |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 97 | /* Exported function to load a readline history file */ |
| 98 | |
| 99 | static PyObject * |
| 100 | read_history_file(self, args) |
| 101 | PyObject *self; |
| 102 | PyObject *args; |
| 103 | { |
| 104 | char *s = NULL; |
| 105 | if (!PyArg_ParseTuple(args, "|z:read_history_file", &s)) |
| 106 | return NULL; |
| 107 | errno = read_history(s); |
| 108 | if (errno) |
| 109 | return PyErr_SetFromErrno(PyExc_IOError); |
| 110 | Py_INCREF(Py_None); |
| 111 | return Py_None; |
| 112 | } |
| 113 | |
| 114 | static char doc_read_history_file[] = "\ |
| 115 | read_history_file([filename]) -> None\n\ |
| 116 | Load a readline history file.\n\ |
| 117 | The default filename is ~/.history.\ |
| 118 | "; |
| 119 | |
| 120 | |
| 121 | /* Exported function to save a readline history file */ |
| 122 | |
| 123 | static PyObject * |
| 124 | write_history_file(self, args) |
| 125 | PyObject *self; |
| 126 | PyObject *args; |
| 127 | { |
| 128 | char *s = NULL; |
| 129 | if (!PyArg_ParseTuple(args, "|z:write_history_file", &s)) |
| 130 | return NULL; |
| 131 | errno = write_history(s); |
| 132 | if (errno) |
| 133 | return PyErr_SetFromErrno(PyExc_IOError); |
| 134 | Py_INCREF(Py_None); |
| 135 | return Py_None; |
| 136 | } |
| 137 | |
| 138 | static char doc_write_history_file[] = "\ |
| 139 | write_history_file([filename]) -> None\n\ |
| 140 | Save a readline history file.\n\ |
| 141 | The default filename is ~/.history.\ |
| 142 | "; |
| 143 | |
| 144 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 145 | /* Exported function to specify a word completer in Python */ |
| 146 | |
| 147 | static PyObject *completer = NULL; |
| 148 | static PyThreadState *tstate = NULL; |
| 149 | |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 150 | static PyObject *begidx = NULL; |
| 151 | static PyObject *endidx = NULL; |
| 152 | |
| 153 | /* get the beginning index for the scope of the tab-completion */ |
| 154 | static PyObject * |
| 155 | get_begidx(self, args) |
| 156 | PyObject *self; |
| 157 | PyObject *args; |
| 158 | { |
| 159 | if(!PyArg_NoArgs(args)) { |
| 160 | return NULL; |
| 161 | } |
| 162 | Py_INCREF(begidx); |
| 163 | return begidx; |
| 164 | } |
| 165 | |
| 166 | static char doc_get_begidx[] = "\ |
| 167 | get_begidx() -> int\n\ |
| 168 | get the beginning index of the readline tab-completion scope"; |
| 169 | |
| 170 | /* get the ending index for the scope of the tab-completion */ |
| 171 | static PyObject * |
| 172 | get_endidx(self, args) |
| 173 | PyObject *self; |
| 174 | PyObject *args; |
| 175 | { |
| 176 | if(!PyArg_NoArgs(args)) { |
| 177 | return NULL; |
| 178 | } |
| 179 | Py_INCREF(endidx); |
| 180 | return endidx; |
| 181 | } |
| 182 | |
| 183 | static char doc_get_endidx[] = "\ |
| 184 | get_endidx() -> int\n\ |
| 185 | get the ending index of the readline tab-completion scope"; |
| 186 | |
| 187 | |
| 188 | /* set the tab-completion word-delimiters that readline uses */ |
| 189 | |
| 190 | static PyObject * |
| 191 | set_completer_delims(self, args) |
| 192 | PyObject *self; |
| 193 | PyObject *args; |
| 194 | { |
| 195 | char *break_chars; |
| 196 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 197 | if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) { |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 198 | return NULL; |
| 199 | } |
| 200 | free(rl_completer_word_break_characters); |
| 201 | rl_completer_word_break_characters = strdup(break_chars); |
| 202 | Py_INCREF(Py_None); |
| 203 | return Py_None; |
| 204 | } |
| 205 | |
| 206 | static char doc_set_completer_delims[] = "\ |
| 207 | set_completer_delims(string) -> None\n\ |
| 208 | set the readline word delimiters for tab-completion"; |
| 209 | |
| 210 | |
| 211 | /* get the tab-completion word-delimiters that readline uses */ |
| 212 | |
| 213 | static PyObject * |
| 214 | get_completer_delims(self, args) |
| 215 | PyObject *self; |
| 216 | PyObject *args; |
| 217 | { |
| 218 | if(!PyArg_NoArgs(args)) { |
| 219 | return NULL; |
| 220 | } |
| 221 | return PyString_FromString(rl_completer_word_break_characters); |
| 222 | } |
| 223 | |
| 224 | static char doc_get_completer_delims[] = "\ |
| 225 | get_completer_delims() -> string\n\ |
| 226 | get the readline word delimiters for tab-completion"; |
| 227 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 228 | static PyObject * |
| 229 | set_completer(self, args) |
| 230 | PyObject *self; |
| 231 | PyObject *args; |
| 232 | { |
| 233 | PyObject *function = Py_None; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 234 | if (!PyArg_ParseTuple(args, "|O:set_completer", &function)) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 235 | return NULL; |
| 236 | if (function == Py_None) { |
| 237 | Py_XDECREF(completer); |
| 238 | completer = NULL; |
| 239 | tstate = NULL; |
| 240 | } |
| 241 | else if (PyCallable_Check(function)) { |
| 242 | PyObject *tmp = completer; |
| 243 | Py_INCREF(function); |
| 244 | completer = function; |
| 245 | Py_XDECREF(tmp); |
| 246 | tstate = PyThreadState_Get(); |
| 247 | } |
| 248 | else { |
| 249 | PyErr_SetString(PyExc_TypeError, |
| 250 | "set_completer(func): argument not callable"); |
| 251 | return NULL; |
| 252 | } |
| 253 | Py_INCREF(Py_None); |
| 254 | return Py_None; |
| 255 | } |
| 256 | |
| 257 | static char doc_set_completer[] = "\ |
| 258 | set_completer([function]) -> None\n\ |
| 259 | Set or remove the completer function.\n\ |
| 260 | The function is called as function(text, state),\n\ |
| 261 | for i in [0, 1, 2, ...] until it returns a non-string.\n\ |
| 262 | It should return the next possible completion starting with 'text'.\ |
| 263 | "; |
| 264 | |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 265 | /* Exported function to read the current line buffer */ |
| 266 | |
| 267 | static PyObject * |
| 268 | get_line_buffer(self, args) |
| 269 | PyObject *self; |
| 270 | PyObject *args; |
| 271 | { |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 272 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 273 | return NULL; |
| 274 | return PyString_FromString(rl_line_buffer); |
| 275 | } |
| 276 | |
| 277 | static char doc_get_line_buffer[] = "\ |
Guido van Rossum | a88c5f3 | 1998-05-20 15:50:56 +0000 | [diff] [blame] | 278 | get_line_buffer() -> string\n\ |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 279 | return the current contents of the line buffer.\ |
| 280 | "; |
| 281 | |
| 282 | /* Exported function to insert text into the line buffer */ |
| 283 | |
| 284 | static PyObject * |
| 285 | insert_text(self, args) |
| 286 | PyObject *self; |
| 287 | PyObject *args; |
| 288 | { |
| 289 | char *s; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 290 | if (!PyArg_ParseTuple(args, "s:insert_text", &s)) |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 291 | return NULL; |
| 292 | rl_insert_text(s); |
| 293 | Py_INCREF(Py_None); |
| 294 | return Py_None; |
| 295 | } |
| 296 | |
| 297 | |
| 298 | static char doc_insert_text[] = "\ |
| 299 | insert_text(string) -> None\n\ |
| 300 | Insert text into the command line.\ |
| 301 | "; |
| 302 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 303 | |
| 304 | /* Table of functions exported by the module */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 305 | |
| 306 | static struct PyMethodDef readline_methods[] = |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 307 | { |
| 308 | {"parse_and_bind", parse_and_bind, 1, doc_parse_and_bind}, |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 309 | {"get_line_buffer", get_line_buffer, 0, doc_get_line_buffer}, |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 310 | {"insert_text", insert_text, 1, doc_insert_text}, |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 311 | {"read_init_file", read_init_file, 1, doc_read_init_file}, |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 312 | {"read_history_file", read_history_file, 1, doc_read_history_file}, |
| 313 | {"write_history_file", write_history_file, 1, doc_write_history_file}, |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 314 | {"set_completer", set_completer, 1, doc_set_completer}, |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 315 | {"get_begidx", get_begidx, 0, doc_get_begidx}, |
| 316 | {"get_endidx", get_endidx, 0, doc_get_endidx}, |
| 317 | |
| 318 | {"set_completer_delims", set_completer_delims, METH_VARARGS, |
| 319 | doc_set_completer_delims}, |
| 320 | {"get_completer_delims", get_completer_delims, 0, |
| 321 | doc_get_completer_delims}, |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 322 | {0, 0} |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 323 | }; |
| 324 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 325 | /* C function to call the Python completer. */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 326 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 327 | static char * |
| 328 | on_completion(text, state) |
| 329 | char *text; |
| 330 | int state; |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 331 | { |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 332 | char *result = NULL; |
| 333 | if (completer != NULL) { |
| 334 | PyObject *r; |
Guido van Rossum | a59406a | 1997-10-10 17:39:19 +0000 | [diff] [blame] | 335 | PyThreadState *save_tstate; |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 336 | /* Note that readline is called with the interpreter |
| 337 | lock released! */ |
Guido van Rossum | a59406a | 1997-10-10 17:39:19 +0000 | [diff] [blame] | 338 | save_tstate = PyThreadState_Swap(NULL); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 339 | PyEval_RestoreThread(tstate); |
| 340 | r = PyObject_CallFunction(completer, "si", text, state); |
| 341 | if (r == NULL) |
| 342 | goto error; |
| 343 | if (r == Py_None) { |
| 344 | result = NULL; |
| 345 | } |
| 346 | else { |
| 347 | char *s = PyString_AsString(r); |
| 348 | if (s == NULL) |
| 349 | goto error; |
| 350 | result = strdup(s); |
| 351 | } |
| 352 | Py_DECREF(r); |
| 353 | goto done; |
| 354 | error: |
| 355 | PyErr_Clear(); |
| 356 | Py_XDECREF(r); |
| 357 | done: |
| 358 | PyEval_SaveThread(); |
Guido van Rossum | a59406a | 1997-10-10 17:39:19 +0000 | [diff] [blame] | 359 | PyThreadState_Swap(save_tstate); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 360 | } |
| 361 | return result; |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 364 | |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 365 | /* a more flexible constructor that saves the "begidx" and "endidx" |
| 366 | * before calling the normal completer */ |
| 367 | |
| 368 | char ** |
| 369 | flex_complete(text, start, end) |
| 370 | char *text; |
| 371 | int start; |
| 372 | int end; |
| 373 | { |
| 374 | Py_XDECREF(begidx); |
| 375 | Py_XDECREF(endidx); |
| 376 | begidx = PyInt_FromLong((long) start); |
| 377 | endidx = PyInt_FromLong((long) end); |
| 378 | return completion_matches(text, *on_completion); |
| 379 | } |
| 380 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 381 | /* Helper to initialize GNU readline properly. */ |
| 382 | |
| 383 | static void |
| 384 | setup_readline() |
| 385 | { |
| 386 | rl_readline_name = "python"; |
| 387 | /* Force rebind of TAB to insert-tab */ |
| 388 | rl_bind_key('\t', rl_insert); |
| 389 | /* Bind both ESC-TAB and ESC-ESC to the completion function */ |
| 390 | rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap); |
| 391 | rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap); |
| 392 | /* Set our completion function */ |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 393 | rl_attempted_completion_function = (CPPFunction *)flex_complete; |
Guido van Rossum | b6c935a | 1997-09-26 23:00:37 +0000 | [diff] [blame] | 394 | /* Set Python word break characters */ |
| 395 | rl_completer_word_break_characters = |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 396 | strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?"); |
Guido van Rossum | b6c935a | 1997-09-26 23:00:37 +0000 | [diff] [blame] | 397 | /* All nonalphanums except '.' */ |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 398 | |
| 399 | begidx = PyInt_FromLong(0L); |
| 400 | endidx = PyInt_FromLong(0L); |
Barry Warsaw | f761287 | 1999-01-29 21:55:03 +0000 | [diff] [blame] | 401 | /* Initialize (allows .inputrc to override) |
| 402 | * |
| 403 | * XXX: A bug in the readline-2.2 library causes a memory leak |
| 404 | * inside this function. Nothing we can do about it. |
| 405 | */ |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 406 | rl_initialize(); |
| 407 | } |
| 408 | |
| 409 | |
| 410 | /* Interrupt handler */ |
| 411 | |
| 412 | static jmp_buf jbuf; |
| 413 | |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 414 | /* ARGSUSED */ |
| 415 | static RETSIGTYPE |
| 416 | onintr(sig) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 417 | int sig; |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 418 | { |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 419 | longjmp(jbuf, 1); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 422 | |
| 423 | /* Wrapper around GNU readline that handles signals differently. */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 424 | |
| 425 | static char * |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 426 | call_readline(prompt) |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 427 | char *prompt; |
| 428 | { |
Guido van Rossum | 26418a9 | 2000-06-28 21:30:31 +0000 | [diff] [blame] | 429 | size_t n; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 430 | char *p, *q; |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 431 | RETSIGTYPE (*old_inthandler)(); |
| 432 | old_inthandler = signal(SIGINT, onintr); |
| 433 | if (setjmp(jbuf)) { |
| 434 | #ifdef HAVE_SIGRELSE |
| 435 | /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */ |
| 436 | sigrelse(SIGINT); |
| 437 | #endif |
| 438 | signal(SIGINT, old_inthandler); |
| 439 | return NULL; |
| 440 | } |
Guido van Rossum | 4462064 | 1997-08-11 18:57:29 +0000 | [diff] [blame] | 441 | rl_event_hook = PyOS_InputHook; |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 442 | p = readline(prompt); |
| 443 | signal(SIGINT, old_inthandler); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 444 | |
| 445 | /* We must return a buffer allocated with PyMem_Malloc. */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 446 | if (p == NULL) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 447 | p = PyMem_Malloc(1); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 448 | if (p != NULL) |
| 449 | *p = '\0'; |
| 450 | return p; |
| 451 | } |
| 452 | n = strlen(p); |
| 453 | if (n > 0) |
| 454 | add_history(p); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 455 | /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and |
| 456 | release the original. */ |
| 457 | q = p; |
| 458 | p = PyMem_Malloc(n+2); |
| 459 | if (p != NULL) { |
| 460 | strncpy(p, q, n); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 461 | p[n] = '\n'; |
| 462 | p[n+1] = '\0'; |
| 463 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 464 | free(q); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 465 | return p; |
| 466 | } |
| 467 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 468 | |
| 469 | /* Initialize the module */ |
| 470 | |
| 471 | static char doc_module[] = |
| 472 | "Importing this module enables command line editing using GNU readline."; |
| 473 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 474 | DL_EXPORT(void) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 475 | initreadline() |
| 476 | { |
| 477 | PyObject *m; |
| 478 | |
| 479 | m = Py_InitModule4("readline", readline_methods, doc_module, |
| 480 | (PyObject *)NULL, PYTHON_API_VERSION); |
| 481 | if (isatty(fileno(stdin))) { |
| 482 | PyOS_ReadlineFunctionPointer = call_readline; |
| 483 | setup_readline(); |
| 484 | } |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 485 | } |