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