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