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