blob: 08569f6caeebb065eff693d52c7e4de845c73d89 [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 Rossumbcc20741998-08-04 22:53:56 +000037#endif
Guido van Rossum730806d1998-04-10 22:27:42 +000038
Guido van Rossum290900a1997-09-26 21:51:21 +000039/* Pointers needed from outside (but not declared in a header file). */
Guido van Rossum44620641997-08-11 18:57:29 +000040extern int (*PyOS_InputHook)();
Guido van Rossum0969d361997-08-05 21:27:50 +000041extern char *(*PyOS_ReadlineFunctionPointer) Py_PROTO((char *));
42
Guido van Rossum0969d361997-08-05 21:27:50 +000043
Guido van Rossum290900a1997-09-26 21:51:21 +000044/* Exported function to send one line to readline's init file parser */
45
46static PyObject *
47parse_and_bind(self, args)
48 PyObject *self;
49 PyObject *args;
50{
Guido van Rossum3b5330e1998-12-04 15:34:39 +000051 char *s, *copy;
Guido van Rossum290900a1997-09-26 21:51:21 +000052 if (!PyArg_ParseTuple(args, "s", &s))
53 return NULL;
Guido van Rossum3b5330e1998-12-04 15:34:39 +000054 /* 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 Rossum290900a1997-09-26 21:51:21 +000062 Py_INCREF(Py_None);
63 return Py_None;
64}
65
66static char doc_parse_and_bind[] = "\
67parse_and_bind(string) -> None\n\
68Parse and execute single line of a readline init file.\
69";
70
71
72/* Exported function to parse a readline init file */
73
74static PyObject *
75read_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
89static char doc_read_init_file[] = "\
90read_init_file([filename]) -> None\n\
91Parse a readline initialization file.\n\
92The default filename is the last filename used.\
93";
94
95
96/* Exported function to specify a word completer in Python */
97
98static PyObject *completer = NULL;
99static PyThreadState *tstate = NULL;
100
101static PyObject *
102set_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
130static char doc_set_completer[] = "\
131set_completer([function]) -> None\n\
132Set or remove the completer function.\n\
133The function is called as function(text, state),\n\
134for i in [0, 1, 2, ...] until it returns a non-string.\n\
135It should return the next possible completion starting with 'text'.\
136";
137
Guido van Rossum79378ff1997-10-07 14:53:21 +0000138/* Exported function to read the current line buffer */
139
140static PyObject *
141get_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
150static char doc_get_line_buffer[] = "\
Guido van Rossuma88c5f31998-05-20 15:50:56 +0000151get_line_buffer() -> string\n\
Guido van Rossum79378ff1997-10-07 14:53:21 +0000152return the current contents of the line buffer.\
153";
154
155/* Exported function to insert text into the line buffer */
156
157static PyObject *
158insert_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
171static char doc_insert_text[] = "\
172insert_text(string) -> None\n\
173Insert text into the command line.\
174";
175
Guido van Rossum290900a1997-09-26 21:51:21 +0000176
177/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000178
179static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000180{
181 {"parse_and_bind", parse_and_bind, 1, doc_parse_and_bind},
Guido van Rossum79378ff1997-10-07 14:53:21 +0000182 {"get_line_buffer", get_line_buffer, 1, doc_get_line_buffer},
183 {"insert_text", insert_text, 1, doc_insert_text},
Guido van Rossum290900a1997-09-26 21:51:21 +0000184 {"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 Rossum0969d361997-08-05 21:27:50 +0000187};
188
Guido van Rossum290900a1997-09-26 21:51:21 +0000189/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000190
Guido van Rossum290900a1997-09-26 21:51:21 +0000191static char *
192on_completion(text, state)
193 char *text;
194 int state;
Guido van Rossum0969d361997-08-05 21:27:50 +0000195{
Guido van Rossum290900a1997-09-26 21:51:21 +0000196 char *result = NULL;
197 if (completer != NULL) {
198 PyObject *r;
Guido van Rossuma59406a1997-10-10 17:39:19 +0000199 PyThreadState *save_tstate;
Guido van Rossum290900a1997-09-26 21:51:21 +0000200 /* Note that readline is called with the interpreter
201 lock released! */
Guido van Rossuma59406a1997-10-10 17:39:19 +0000202 save_tstate = PyThreadState_Swap(NULL);
Guido van Rossum290900a1997-09-26 21:51:21 +0000203 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 Rossuma59406a1997-10-10 17:39:19 +0000223 PyThreadState_Swap(save_tstate);
Guido van Rossum290900a1997-09-26 21:51:21 +0000224 }
225 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000226}
227
Guido van Rossum290900a1997-09-26 21:51:21 +0000228
229/* Helper to initialize GNU readline properly. */
230
231static void
232setup_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 Rossumb6c935a1997-09-26 23:00:37 +0000242 /* Set Python word break characters */
243 rl_completer_word_break_characters =
244 " \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?";
245 /* All nonalphanums except '.' */
Guido van Rossum290900a1997-09-26 21:51:21 +0000246 /* Initialize (allows .inputrc to override) */
247 rl_initialize();
248}
249
250
251/* Interrupt handler */
252
253static jmp_buf jbuf;
254
Guido van Rossum0969d361997-08-05 21:27:50 +0000255/* ARGSUSED */
256static RETSIGTYPE
257onintr(sig)
Guido van Rossum290900a1997-09-26 21:51:21 +0000258 int sig;
Guido van Rossum0969d361997-08-05 21:27:50 +0000259{
Guido van Rossum290900a1997-09-26 21:51:21 +0000260 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000261}
262
Guido van Rossum290900a1997-09-26 21:51:21 +0000263
264/* Wrapper around GNU readline that handles signals differently. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000265
266static char *
Guido van Rossum290900a1997-09-26 21:51:21 +0000267call_readline(prompt)
Guido van Rossum0969d361997-08-05 21:27:50 +0000268 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 Rossum44620641997-08-11 18:57:29 +0000282 rl_event_hook = PyOS_InputHook;
Guido van Rossum0969d361997-08-05 21:27:50 +0000283 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 Rossum290900a1997-09-26 21:51:21 +0000301
302/* Initialize the module */
303
304static char doc_module[] =
305"Importing this module enables command line editing using GNU readline.";
306
Guido van Rossum3886bb61998-12-04 18:50:17 +0000307DL_EXPORT(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000308initreadline()
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 Rossum0969d361997-08-05 21:27:50 +0000318}