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 | |
| 110 | |
| 111 | /* Table of functions exported by the module */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 112 | |
| 113 | static struct PyMethodDef readline_methods[] = |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 114 | { |
| 115 | {"parse_and_bind", parse_and_bind, 1, doc_parse_and_bind}, |
| 116 | {"read_init_file", read_init_file, 1, doc_read_init_file}, |
| 117 | {"set_completer", set_completer, 1, doc_set_completer}, |
| 118 | {0, 0} |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 121 | /* C function to call the Python completer. */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 122 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 123 | static char * |
| 124 | on_completion(text, state) |
| 125 | char *text; |
| 126 | int state; |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 127 | { |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 128 | char *result = NULL; |
| 129 | if (completer != NULL) { |
| 130 | PyObject *r; |
| 131 | /* Note that readline is called with the interpreter |
| 132 | lock released! */ |
| 133 | PyEval_RestoreThread(tstate); |
| 134 | r = PyObject_CallFunction(completer, "si", text, state); |
| 135 | if (r == NULL) |
| 136 | goto error; |
| 137 | if (r == Py_None) { |
| 138 | result = NULL; |
| 139 | } |
| 140 | else { |
| 141 | char *s = PyString_AsString(r); |
| 142 | if (s == NULL) |
| 143 | goto error; |
| 144 | result = strdup(s); |
| 145 | } |
| 146 | Py_DECREF(r); |
| 147 | goto done; |
| 148 | error: |
| 149 | PyErr_Clear(); |
| 150 | Py_XDECREF(r); |
| 151 | done: |
| 152 | PyEval_SaveThread(); |
| 153 | } |
| 154 | return result; |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 157 | |
| 158 | /* Helper to initialize GNU readline properly. */ |
| 159 | |
| 160 | static void |
| 161 | setup_readline() |
| 162 | { |
| 163 | rl_readline_name = "python"; |
| 164 | /* Force rebind of TAB to insert-tab */ |
| 165 | rl_bind_key('\t', rl_insert); |
| 166 | /* Bind both ESC-TAB and ESC-ESC to the completion function */ |
| 167 | rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap); |
| 168 | rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap); |
| 169 | /* Set our completion function */ |
| 170 | rl_completion_entry_function = (Function *) on_completion; |
Guido van Rossum | b6c935a | 1997-09-26 23:00:37 +0000 | [diff] [blame^] | 171 | /* Set Python word break characters */ |
| 172 | rl_completer_word_break_characters = |
| 173 | " \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?"; |
| 174 | /* All nonalphanums except '.' */ |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 175 | /* Initialize (allows .inputrc to override) */ |
| 176 | rl_initialize(); |
| 177 | } |
| 178 | |
| 179 | |
| 180 | /* Interrupt handler */ |
| 181 | |
| 182 | static jmp_buf jbuf; |
| 183 | |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 184 | /* ARGSUSED */ |
| 185 | static RETSIGTYPE |
| 186 | onintr(sig) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 187 | int sig; |
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 | longjmp(jbuf, 1); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 192 | |
| 193 | /* Wrapper around GNU readline that handles signals differently. */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 194 | |
| 195 | static char * |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 196 | call_readline(prompt) |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 197 | char *prompt; |
| 198 | { |
| 199 | int n; |
| 200 | char *p; |
| 201 | RETSIGTYPE (*old_inthandler)(); |
| 202 | old_inthandler = signal(SIGINT, onintr); |
| 203 | if (setjmp(jbuf)) { |
| 204 | #ifdef HAVE_SIGRELSE |
| 205 | /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */ |
| 206 | sigrelse(SIGINT); |
| 207 | #endif |
| 208 | signal(SIGINT, old_inthandler); |
| 209 | return NULL; |
| 210 | } |
Guido van Rossum | 4462064 | 1997-08-11 18:57:29 +0000 | [diff] [blame] | 211 | rl_event_hook = PyOS_InputHook; |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 212 | p = readline(prompt); |
| 213 | signal(SIGINT, old_inthandler); |
| 214 | if (p == NULL) { |
| 215 | p = malloc(1); |
| 216 | if (p != NULL) |
| 217 | *p = '\0'; |
| 218 | return p; |
| 219 | } |
| 220 | n = strlen(p); |
| 221 | if (n > 0) |
| 222 | add_history(p); |
| 223 | if ((p = realloc(p, n+2)) != NULL) { |
| 224 | p[n] = '\n'; |
| 225 | p[n+1] = '\0'; |
| 226 | } |
| 227 | return p; |
| 228 | } |
| 229 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 230 | |
| 231 | /* Initialize the module */ |
| 232 | |
| 233 | static char doc_module[] = |
| 234 | "Importing this module enables command line editing using GNU readline."; |
| 235 | |
| 236 | void |
| 237 | initreadline() |
| 238 | { |
| 239 | PyObject *m; |
| 240 | |
| 241 | m = Py_InitModule4("readline", readline_methods, doc_module, |
| 242 | (PyObject *)NULL, PYTHON_API_VERSION); |
| 243 | if (isatty(fileno(stdin))) { |
| 244 | PyOS_ReadlineFunctionPointer = call_readline; |
| 245 | setup_readline(); |
| 246 | } |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 247 | } |