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