blob: b2b56a2279cdaaf035ea2b57129fa1dcb38966b9 [file] [log] [blame]
Guido van Rossum290900a1997-09-26 21:51:21 +00001/* 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 Rossum0969d361997-08-05 21:27:50 +00007 */
8
Guido van Rossum290900a1997-09-26 21:51:21 +00009/* Standard definitions */
Guido van Rossum0969d361997-08-05 21:27:50 +000010#include "Python.h"
11#include <setjmp.h>
12#include <signal.h>
Guido van Rossum290900a1997-09-26 21:51:21 +000013#include <errno.h>
Guido van Rossum0969d361997-08-05 21:27:50 +000014
Guido van Rossum73bacfc1998-01-19 22:05:22 +000015#ifdef HAVE_UNISTD_H
16#include <unistd.h> /* For isatty() */
17#endif
18
Guido van Rossum290900a1997-09-26 21:51:21 +000019/* GNU readline definitions */
Guido van Rossumbcc20741998-08-04 22:53:56 +000020/* 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 Rossum290900a1997-09-26 21:51:21 +000028#include <readline/readline.h> /* You may need to add an -I option to Setup */
29
Guido van Rossum730806d1998-04-10 22:27:42 +000030extern int rl_parse_and_bind();
31extern int rl_read_init_file();
32extern int rl_insert_text();
33extern int rl_bind_key();
34extern int rl_bind_key_in_map();
35extern int rl_initialize();
36extern int add_history();
Guido van Rossum54ecc3d1999-01-27 17:53:11 +000037extern Function *rl_event_hook;
Guido van Rossumbcc20741998-08-04 22:53:56 +000038#endif
Guido van Rossum730806d1998-04-10 22:27:42 +000039
Guido van Rossum290900a1997-09-26 21:51:21 +000040/* Pointers needed from outside (but not declared in a header file). */
Guido van Rossum44620641997-08-11 18:57:29 +000041extern int (*PyOS_InputHook)();
Guido van Rossum0969d361997-08-05 21:27:50 +000042extern char *(*PyOS_ReadlineFunctionPointer) Py_PROTO((char *));
43
Guido van Rossum0969d361997-08-05 21:27:50 +000044
Guido van Rossum290900a1997-09-26 21:51:21 +000045/* Exported function to send one line to readline's init file parser */
46
47static PyObject *
48parse_and_bind(self, args)
49 PyObject *self;
50 PyObject *args;
51{
Guido van Rossum3b5330e1998-12-04 15:34:39 +000052 char *s, *copy;
Guido van Rossum290900a1997-09-26 21:51:21 +000053 if (!PyArg_ParseTuple(args, "s", &s))
54 return NULL;
Guido van Rossum3b5330e1998-12-04 15:34:39 +000055 /* Make a copy -- rl_parse_and_bind() modifies its argument */
56 /* Bernard Herzog */
57 copy = malloc(1 + strlen(s));
58 if (copy == NULL)
59 return PyErr_NoMemory();
60 strcpy(copy, s);
61 rl_parse_and_bind(copy);
62 free(copy); /* Free the copy */
Guido van Rossum290900a1997-09-26 21:51:21 +000063 Py_INCREF(Py_None);
64 return Py_None;
65}
66
67static char doc_parse_and_bind[] = "\
68parse_and_bind(string) -> None\n\
69Parse and execute single line of a readline init file.\
70";
71
72
73/* Exported function to parse a readline init file */
74
75static PyObject *
76read_init_file(self, args)
77 PyObject *self;
78 PyObject *args;
79{
80 char *s = NULL;
81 if (!PyArg_ParseTuple(args, "|z", &s))
82 return NULL;
83 errno = rl_read_init_file(s);
84 if (errno)
85 return PyErr_SetFromErrno(PyExc_IOError);
86 Py_INCREF(Py_None);
87 return Py_None;
88}
89
90static char doc_read_init_file[] = "\
91read_init_file([filename]) -> None\n\
92Parse a readline initialization file.\n\
93The default filename is the last filename used.\
94";
95
96
97/* Exported function to specify a word completer in Python */
98
99static PyObject *completer = NULL;
100static PyThreadState *tstate = NULL;
101
102static PyObject *
103set_completer(self, args)
104 PyObject *self;
105 PyObject *args;
106{
107 PyObject *function = Py_None;
108 if (!PyArg_ParseTuple(args, "|O", &function))
109 return NULL;
110 if (function == Py_None) {
111 Py_XDECREF(completer);
112 completer = NULL;
113 tstate = NULL;
114 }
115 else if (PyCallable_Check(function)) {
116 PyObject *tmp = completer;
117 Py_INCREF(function);
118 completer = function;
119 Py_XDECREF(tmp);
120 tstate = PyThreadState_Get();
121 }
122 else {
123 PyErr_SetString(PyExc_TypeError,
124 "set_completer(func): argument not callable");
125 return NULL;
126 }
127 Py_INCREF(Py_None);
128 return Py_None;
129}
130
131static char doc_set_completer[] = "\
132set_completer([function]) -> None\n\
133Set or remove the completer function.\n\
134The function is called as function(text, state),\n\
135for i in [0, 1, 2, ...] until it returns a non-string.\n\
136It should return the next possible completion starting with 'text'.\
137";
138
Guido van Rossum79378ff1997-10-07 14:53:21 +0000139/* Exported function to read the current line buffer */
140
141static PyObject *
142get_line_buffer(self, args)
143 PyObject *self;
144 PyObject *args;
145{
146 if (PyArg_NoArgs(args))
147 return NULL;
148 return PyString_FromString(rl_line_buffer);
149}
150
151static char doc_get_line_buffer[] = "\
Guido van Rossuma88c5f31998-05-20 15:50:56 +0000152get_line_buffer() -> string\n\
Guido van Rossum79378ff1997-10-07 14:53:21 +0000153return the current contents of the line buffer.\
154";
155
156/* Exported function to insert text into the line buffer */
157
158static PyObject *
159insert_text(self, args)
160 PyObject *self;
161 PyObject *args;
162{
163 char *s;
164 if (!PyArg_ParseTuple(args, "s", &s))
165 return NULL;
166 rl_insert_text(s);
167 Py_INCREF(Py_None);
168 return Py_None;
169}
170
171
172static char doc_insert_text[] = "\
173insert_text(string) -> None\n\
174Insert text into the command line.\
175";
176
Guido van Rossum290900a1997-09-26 21:51:21 +0000177
178/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000179
180static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000181{
182 {"parse_and_bind", parse_and_bind, 1, doc_parse_and_bind},
Guido van Rossum79378ff1997-10-07 14:53:21 +0000183 {"get_line_buffer", get_line_buffer, 1, doc_get_line_buffer},
184 {"insert_text", insert_text, 1, doc_insert_text},
Guido van Rossum290900a1997-09-26 21:51:21 +0000185 {"read_init_file", read_init_file, 1, doc_read_init_file},
186 {"set_completer", set_completer, 1, doc_set_completer},
187 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000188};
189
Guido van Rossum290900a1997-09-26 21:51:21 +0000190/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000191
Guido van Rossum290900a1997-09-26 21:51:21 +0000192static char *
193on_completion(text, state)
194 char *text;
195 int state;
Guido van Rossum0969d361997-08-05 21:27:50 +0000196{
Guido van Rossum290900a1997-09-26 21:51:21 +0000197 char *result = NULL;
198 if (completer != NULL) {
199 PyObject *r;
Guido van Rossuma59406a1997-10-10 17:39:19 +0000200 PyThreadState *save_tstate;
Guido van Rossum290900a1997-09-26 21:51:21 +0000201 /* Note that readline is called with the interpreter
202 lock released! */
Guido van Rossuma59406a1997-10-10 17:39:19 +0000203 save_tstate = PyThreadState_Swap(NULL);
Guido van Rossum290900a1997-09-26 21:51:21 +0000204 PyEval_RestoreThread(tstate);
205 r = PyObject_CallFunction(completer, "si", text, state);
206 if (r == NULL)
207 goto error;
208 if (r == Py_None) {
209 result = NULL;
210 }
211 else {
212 char *s = PyString_AsString(r);
213 if (s == NULL)
214 goto error;
215 result = strdup(s);
216 }
217 Py_DECREF(r);
218 goto done;
219 error:
220 PyErr_Clear();
221 Py_XDECREF(r);
222 done:
223 PyEval_SaveThread();
Guido van Rossuma59406a1997-10-10 17:39:19 +0000224 PyThreadState_Swap(save_tstate);
Guido van Rossum290900a1997-09-26 21:51:21 +0000225 }
226 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000227}
228
Guido van Rossum290900a1997-09-26 21:51:21 +0000229
230/* Helper to initialize GNU readline properly. */
231
232static void
233setup_readline()
234{
235 rl_readline_name = "python";
236 /* Force rebind of TAB to insert-tab */
237 rl_bind_key('\t', rl_insert);
238 /* Bind both ESC-TAB and ESC-ESC to the completion function */
239 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
240 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
241 /* Set our completion function */
242 rl_completion_entry_function = (Function *) on_completion;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000243 /* Set Python word break characters */
244 rl_completer_word_break_characters =
245 " \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?";
246 /* All nonalphanums except '.' */
Barry Warsawf7612871999-01-29 21:55:03 +0000247 /* Initialize (allows .inputrc to override)
248 *
249 * XXX: A bug in the readline-2.2 library causes a memory leak
250 * inside this function. Nothing we can do about it.
251 */
Guido van Rossum290900a1997-09-26 21:51:21 +0000252 rl_initialize();
253}
254
255
256/* Interrupt handler */
257
258static jmp_buf jbuf;
259
Guido van Rossum0969d361997-08-05 21:27:50 +0000260/* ARGSUSED */
261static RETSIGTYPE
262onintr(sig)
Guido van Rossum290900a1997-09-26 21:51:21 +0000263 int sig;
Guido van Rossum0969d361997-08-05 21:27:50 +0000264{
Guido van Rossum290900a1997-09-26 21:51:21 +0000265 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000266}
267
Guido van Rossum290900a1997-09-26 21:51:21 +0000268
269/* Wrapper around GNU readline that handles signals differently. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000270
271static char *
Guido van Rossum290900a1997-09-26 21:51:21 +0000272call_readline(prompt)
Guido van Rossum0969d361997-08-05 21:27:50 +0000273 char *prompt;
274{
275 int n;
276 char *p;
277 RETSIGTYPE (*old_inthandler)();
278 old_inthandler = signal(SIGINT, onintr);
279 if (setjmp(jbuf)) {
280#ifdef HAVE_SIGRELSE
281 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
282 sigrelse(SIGINT);
283#endif
284 signal(SIGINT, old_inthandler);
285 return NULL;
286 }
Guido van Rossum44620641997-08-11 18:57:29 +0000287 rl_event_hook = PyOS_InputHook;
Guido van Rossum0969d361997-08-05 21:27:50 +0000288 p = readline(prompt);
289 signal(SIGINT, old_inthandler);
290 if (p == NULL) {
291 p = malloc(1);
292 if (p != NULL)
293 *p = '\0';
294 return p;
295 }
296 n = strlen(p);
297 if (n > 0)
298 add_history(p);
299 if ((p = realloc(p, n+2)) != NULL) {
300 p[n] = '\n';
301 p[n+1] = '\0';
302 }
303 return p;
304}
305
Guido van Rossum290900a1997-09-26 21:51:21 +0000306
307/* Initialize the module */
308
309static char doc_module[] =
310"Importing this module enables command line editing using GNU readline.";
311
Guido van Rossum3886bb61998-12-04 18:50:17 +0000312DL_EXPORT(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000313initreadline()
314{
315 PyObject *m;
316
317 m = Py_InitModule4("readline", readline_methods, doc_module,
318 (PyObject *)NULL, PYTHON_API_VERSION);
319 if (isatty(fileno(stdin))) {
320 PyOS_ReadlineFunctionPointer = call_readline;
321 setup_readline();
322 }
Guido van Rossum0969d361997-08-05 21:27:50 +0000323}