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 | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 37 | #endif |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 38 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 39 | /* Pointers needed from outside (but not declared in a header file). */ |
Guido van Rossum | 4462064 | 1997-08-11 18:57:29 +0000 | [diff] [blame] | 40 | extern int (*PyOS_InputHook)(); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 41 | extern char *(*PyOS_ReadlineFunctionPointer) Py_PROTO((char *)); |
| 42 | |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 43 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 44 | /* Exported function to send one line to readline's init file parser */ |
| 45 | |
| 46 | static PyObject * |
| 47 | parse_and_bind(self, args) |
| 48 | PyObject *self; |
| 49 | PyObject *args; |
| 50 | { |
| 51 | char *s; |
| 52 | if (!PyArg_ParseTuple(args, "s", &s)) |
| 53 | return NULL; |
| 54 | rl_parse_and_bind(s); |
| 55 | Py_INCREF(Py_None); |
| 56 | return Py_None; |
| 57 | } |
| 58 | |
| 59 | static char doc_parse_and_bind[] = "\ |
| 60 | parse_and_bind(string) -> None\n\ |
| 61 | Parse and execute single line of a readline init file.\ |
| 62 | "; |
| 63 | |
| 64 | |
| 65 | /* Exported function to parse a readline init file */ |
| 66 | |
| 67 | static PyObject * |
| 68 | read_init_file(self, args) |
| 69 | PyObject *self; |
| 70 | PyObject *args; |
| 71 | { |
| 72 | char *s = NULL; |
| 73 | if (!PyArg_ParseTuple(args, "|z", &s)) |
| 74 | return NULL; |
| 75 | errno = rl_read_init_file(s); |
| 76 | if (errno) |
| 77 | return PyErr_SetFromErrno(PyExc_IOError); |
| 78 | Py_INCREF(Py_None); |
| 79 | return Py_None; |
| 80 | } |
| 81 | |
| 82 | static char doc_read_init_file[] = "\ |
| 83 | read_init_file([filename]) -> None\n\ |
| 84 | Parse a readline initialization file.\n\ |
| 85 | The default filename is the last filename used.\ |
| 86 | "; |
| 87 | |
| 88 | |
| 89 | /* Exported function to specify a word completer in Python */ |
| 90 | |
| 91 | static PyObject *completer = NULL; |
| 92 | static PyThreadState *tstate = NULL; |
| 93 | |
| 94 | static PyObject * |
| 95 | set_completer(self, args) |
| 96 | PyObject *self; |
| 97 | PyObject *args; |
| 98 | { |
| 99 | PyObject *function = Py_None; |
| 100 | if (!PyArg_ParseTuple(args, "|O", &function)) |
| 101 | return NULL; |
| 102 | if (function == Py_None) { |
| 103 | Py_XDECREF(completer); |
| 104 | completer = NULL; |
| 105 | tstate = NULL; |
| 106 | } |
| 107 | else if (PyCallable_Check(function)) { |
| 108 | PyObject *tmp = completer; |
| 109 | Py_INCREF(function); |
| 110 | completer = function; |
| 111 | Py_XDECREF(tmp); |
| 112 | tstate = PyThreadState_Get(); |
| 113 | } |
| 114 | else { |
| 115 | PyErr_SetString(PyExc_TypeError, |
| 116 | "set_completer(func): argument not callable"); |
| 117 | return NULL; |
| 118 | } |
| 119 | Py_INCREF(Py_None); |
| 120 | return Py_None; |
| 121 | } |
| 122 | |
| 123 | static char doc_set_completer[] = "\ |
| 124 | set_completer([function]) -> None\n\ |
| 125 | Set or remove the completer function.\n\ |
| 126 | The function is called as function(text, state),\n\ |
| 127 | for i in [0, 1, 2, ...] until it returns a non-string.\n\ |
| 128 | It should return the next possible completion starting with 'text'.\ |
| 129 | "; |
| 130 | |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 131 | /* Exported function to read the current line buffer */ |
| 132 | |
| 133 | static PyObject * |
| 134 | get_line_buffer(self, args) |
| 135 | PyObject *self; |
| 136 | PyObject *args; |
| 137 | { |
| 138 | if (PyArg_NoArgs(args)) |
| 139 | return NULL; |
| 140 | return PyString_FromString(rl_line_buffer); |
| 141 | } |
| 142 | |
| 143 | static char doc_get_line_buffer[] = "\ |
Guido van Rossum | a88c5f3 | 1998-05-20 15:50:56 +0000 | [diff] [blame] | 144 | get_line_buffer() -> string\n\ |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 145 | return the current contents of the line buffer.\ |
| 146 | "; |
| 147 | |
| 148 | /* Exported function to insert text into the line buffer */ |
| 149 | |
| 150 | static PyObject * |
| 151 | insert_text(self, args) |
| 152 | PyObject *self; |
| 153 | PyObject *args; |
| 154 | { |
| 155 | char *s; |
| 156 | if (!PyArg_ParseTuple(args, "s", &s)) |
| 157 | return NULL; |
| 158 | rl_insert_text(s); |
| 159 | Py_INCREF(Py_None); |
| 160 | return Py_None; |
| 161 | } |
| 162 | |
| 163 | |
| 164 | static char doc_insert_text[] = "\ |
| 165 | insert_text(string) -> None\n\ |
| 166 | Insert text into the command line.\ |
| 167 | "; |
| 168 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 169 | |
| 170 | /* Table of functions exported by the module */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 171 | |
| 172 | static struct PyMethodDef readline_methods[] = |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 173 | { |
| 174 | {"parse_and_bind", parse_and_bind, 1, doc_parse_and_bind}, |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 175 | {"get_line_buffer", get_line_buffer, 1, doc_get_line_buffer}, |
| 176 | {"insert_text", insert_text, 1, doc_insert_text}, |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 177 | {"read_init_file", read_init_file, 1, doc_read_init_file}, |
| 178 | {"set_completer", set_completer, 1, doc_set_completer}, |
| 179 | {0, 0} |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 180 | }; |
| 181 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 182 | /* C function to call the Python completer. */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 183 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 184 | static char * |
| 185 | on_completion(text, state) |
| 186 | char *text; |
| 187 | int state; |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 188 | { |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 189 | char *result = NULL; |
| 190 | if (completer != NULL) { |
| 191 | PyObject *r; |
Guido van Rossum | a59406a | 1997-10-10 17:39:19 +0000 | [diff] [blame] | 192 | PyThreadState *save_tstate; |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 193 | /* Note that readline is called with the interpreter |
| 194 | lock released! */ |
Guido van Rossum | a59406a | 1997-10-10 17:39:19 +0000 | [diff] [blame] | 195 | save_tstate = PyThreadState_Swap(NULL); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 196 | PyEval_RestoreThread(tstate); |
| 197 | r = PyObject_CallFunction(completer, "si", text, state); |
| 198 | if (r == NULL) |
| 199 | goto error; |
| 200 | if (r == Py_None) { |
| 201 | result = NULL; |
| 202 | } |
| 203 | else { |
| 204 | char *s = PyString_AsString(r); |
| 205 | if (s == NULL) |
| 206 | goto error; |
| 207 | result = strdup(s); |
| 208 | } |
| 209 | Py_DECREF(r); |
| 210 | goto done; |
| 211 | error: |
| 212 | PyErr_Clear(); |
| 213 | Py_XDECREF(r); |
| 214 | done: |
| 215 | PyEval_SaveThread(); |
Guido van Rossum | a59406a | 1997-10-10 17:39:19 +0000 | [diff] [blame] | 216 | PyThreadState_Swap(save_tstate); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 217 | } |
| 218 | return result; |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 221 | |
| 222 | /* Helper to initialize GNU readline properly. */ |
| 223 | |
| 224 | static void |
| 225 | setup_readline() |
| 226 | { |
| 227 | rl_readline_name = "python"; |
| 228 | /* Force rebind of TAB to insert-tab */ |
| 229 | rl_bind_key('\t', rl_insert); |
| 230 | /* Bind both ESC-TAB and ESC-ESC to the completion function */ |
| 231 | rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap); |
| 232 | rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap); |
| 233 | /* Set our completion function */ |
| 234 | rl_completion_entry_function = (Function *) on_completion; |
Guido van Rossum | b6c935a | 1997-09-26 23:00:37 +0000 | [diff] [blame] | 235 | /* Set Python word break characters */ |
| 236 | rl_completer_word_break_characters = |
| 237 | " \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?"; |
| 238 | /* All nonalphanums except '.' */ |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 239 | /* Initialize (allows .inputrc to override) */ |
| 240 | rl_initialize(); |
| 241 | } |
| 242 | |
| 243 | |
| 244 | /* Interrupt handler */ |
| 245 | |
| 246 | static jmp_buf jbuf; |
| 247 | |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 248 | /* ARGSUSED */ |
| 249 | static RETSIGTYPE |
| 250 | onintr(sig) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 251 | int sig; |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 252 | { |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 253 | longjmp(jbuf, 1); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 256 | |
| 257 | /* Wrapper around GNU readline that handles signals differently. */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 258 | |
| 259 | static char * |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 260 | call_readline(prompt) |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 261 | char *prompt; |
| 262 | { |
| 263 | int n; |
| 264 | char *p; |
| 265 | RETSIGTYPE (*old_inthandler)(); |
| 266 | old_inthandler = signal(SIGINT, onintr); |
| 267 | if (setjmp(jbuf)) { |
| 268 | #ifdef HAVE_SIGRELSE |
| 269 | /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */ |
| 270 | sigrelse(SIGINT); |
| 271 | #endif |
| 272 | signal(SIGINT, old_inthandler); |
| 273 | return NULL; |
| 274 | } |
Guido van Rossum | 4462064 | 1997-08-11 18:57:29 +0000 | [diff] [blame] | 275 | rl_event_hook = PyOS_InputHook; |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 276 | p = readline(prompt); |
| 277 | signal(SIGINT, old_inthandler); |
| 278 | if (p == NULL) { |
| 279 | p = malloc(1); |
| 280 | if (p != NULL) |
| 281 | *p = '\0'; |
| 282 | return p; |
| 283 | } |
| 284 | n = strlen(p); |
| 285 | if (n > 0) |
| 286 | add_history(p); |
| 287 | if ((p = realloc(p, n+2)) != NULL) { |
| 288 | p[n] = '\n'; |
| 289 | p[n+1] = '\0'; |
| 290 | } |
| 291 | return p; |
| 292 | } |
| 293 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 294 | |
| 295 | /* Initialize the module */ |
| 296 | |
| 297 | static char doc_module[] = |
| 298 | "Importing this module enables command line editing using GNU readline."; |
| 299 | |
| 300 | void |
| 301 | initreadline() |
| 302 | { |
| 303 | PyObject *m; |
| 304 | |
| 305 | m = Py_InitModule4("readline", readline_methods, doc_module, |
| 306 | (PyObject *)NULL, PYTHON_API_VERSION); |
| 307 | if (isatty(fileno(stdin))) { |
| 308 | PyOS_ReadlineFunctionPointer = call_readline; |
| 309 | setup_readline(); |
| 310 | } |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 311 | } |