This commit was manufactured by cvs2svn to create tag 'r221c2'.
[python/dscho.git] / Modules / readline.c
blobfa4fc06863dc83d20c6f174ad6e795cd17e0b3f2
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.
5 * More recently, it was largely rewritten by Guido van Rossum who is
6 * now maintaining it.
7 */
9 /* Standard definitions */
10 #include "Python.h"
11 #include <setjmp.h>
12 #include <signal.h>
13 #include <errno.h>
15 /* GNU readline definitions */
16 #undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
17 #include <readline/readline.h>
18 #include <readline/history.h>
20 #ifdef HAVE_RL_COMPLETION_MATCHES
21 #define completion_matches(x, y) rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
22 #endif
24 /* Pointers needed from outside (but not declared in a header file). */
25 extern DL_IMPORT(int) (*PyOS_InputHook)(void);
26 extern DL_IMPORT(char) *(*PyOS_ReadlineFunctionPointer)(char *);
29 /* Exported function to send one line to readline's init file parser */
31 static PyObject *
32 parse_and_bind(PyObject *self, PyObject *args)
34 char *s, *copy;
35 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
36 return NULL;
37 /* Make a copy -- rl_parse_and_bind() modifies its argument */
38 /* Bernard Herzog */
39 copy = malloc(1 + strlen(s));
40 if (copy == NULL)
41 return PyErr_NoMemory();
42 strcpy(copy, s);
43 rl_parse_and_bind(copy);
44 free(copy); /* Free the copy */
45 Py_INCREF(Py_None);
46 return Py_None;
49 static char doc_parse_and_bind[] = "\
50 parse_and_bind(string) -> None\n\
51 Parse and execute single line of a readline init file.\
55 /* Exported function to parse a readline init file */
57 static PyObject *
58 read_init_file(PyObject *self, PyObject *args)
60 char *s = NULL;
61 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
62 return NULL;
63 errno = rl_read_init_file(s);
64 if (errno)
65 return PyErr_SetFromErrno(PyExc_IOError);
66 Py_INCREF(Py_None);
67 return Py_None;
70 static char doc_read_init_file[] = "\
71 read_init_file([filename]) -> None\n\
72 Parse a readline initialization file.\n\
73 The default filename is the last filename used.\
77 /* Exported function to load a readline history file */
79 static PyObject *
80 read_history_file(PyObject *self, PyObject *args)
82 char *s = NULL;
83 if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
84 return NULL;
85 errno = read_history(s);
86 if (errno)
87 return PyErr_SetFromErrno(PyExc_IOError);
88 Py_INCREF(Py_None);
89 return Py_None;
92 static int history_length = -1; /* do not truncate history by default */
93 static char doc_read_history_file[] = "\
94 read_history_file([filename]) -> None\n\
95 Load a readline history file.\n\
96 The default filename is ~/.history.\
100 /* Exported function to save a readline history file */
102 static PyObject *
103 write_history_file(PyObject *self, PyObject *args)
105 char *s = NULL;
106 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
107 return NULL;
108 errno = write_history(s);
109 if (!errno && history_length >= 0)
110 history_truncate_file(s, history_length);
111 if (errno)
112 return PyErr_SetFromErrno(PyExc_IOError);
113 Py_INCREF(Py_None);
114 return Py_None;
117 static char doc_write_history_file[] = "\
118 write_history_file([filename]) -> None\n\
119 Save a readline history file.\n\
120 The default filename is ~/.history.\
124 static char set_history_length_doc[] = "\
125 set_history_length(length) -> None\n\
126 set the maximal number of items which will be written to\n\
127 the history file. A negative length is used to inhibit\n\
128 history truncation.\n\
131 static PyObject*
132 set_history_length(PyObject *self, PyObject *args)
134 int length = history_length;
135 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
136 return NULL;
137 history_length = length;
138 Py_INCREF(Py_None);
139 return Py_None;
144 static char get_history_length_doc[] = "\
145 get_history_length() -> int\n\
146 return the current history length value.\n\
149 static PyObject*
150 get_history_length(PyObject *self, PyObject *args)
152 if (!PyArg_ParseTuple(args, ":get_history_length"))
153 return NULL;
154 return Py_BuildValue("i", history_length);
157 /* Generic hook function setter */
159 static PyObject *
160 set_hook(const char * funcname, PyObject **hook_var, PyThreadState **tstate, PyObject *args)
162 PyObject *function = Py_None;
163 char buf[80];
164 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
165 if (!PyArg_ParseTuple(args, buf, &function))
166 return NULL;
167 if (function == Py_None) {
168 Py_XDECREF(*hook_var);
169 *hook_var = NULL;
170 *tstate = NULL;
172 else if (PyCallable_Check(function)) {
173 PyObject *tmp = *hook_var;
174 Py_INCREF(function);
175 *hook_var = function;
176 Py_XDECREF(tmp);
177 *tstate = PyThreadState_Get();
179 else {
180 PyOS_snprintf(buf, sizeof(buf),
181 "set_%.50s(func): argument not callable",
182 funcname);
183 PyErr_SetString(PyExc_TypeError, buf);
184 return NULL;
186 Py_INCREF(Py_None);
187 return Py_None;
190 /* Exported functions to specify hook functions in Python */
192 static PyObject *startup_hook = NULL;
193 static PyThreadState *startup_hook_tstate = NULL;
195 #ifdef HAVE_RL_PRE_INPUT_HOOK
196 static PyObject *pre_input_hook = NULL;
197 static PyThreadState *pre_input_hook_tstate = NULL;
198 #endif
200 static PyObject *
201 set_startup_hook(PyObject *self, PyObject *args)
203 return set_hook("startup_hook", &startup_hook, &startup_hook_tstate, args);
206 static char doc_set_startup_hook[] = "\
207 set_startup_hook([function]) -> None\n\
208 Set or remove the startup_hook function.\n\
209 The function is called with no arguments just\n\
210 before readline prints the first prompt.\n\
213 #ifdef HAVE_RL_PRE_INPUT_HOOK
214 static PyObject *
215 set_pre_input_hook(PyObject *self, PyObject *args)
217 return set_hook("pre_input_hook", &pre_input_hook, &pre_input_hook_tstate, args);
220 static char doc_set_pre_input_hook[] = "\
221 set_pre_input_hook([function]) -> None\n\
222 Set or remove the pre_input_hook function.\n\
223 The function is called with no arguments after the first prompt\n\
224 has been printed and just before readline starts reading input\n\
225 characters.\n\
227 #endif
229 /* Exported function to specify a word completer in Python */
231 static PyObject *completer = NULL;
232 static PyThreadState *completer_tstate = NULL;
234 static PyObject *begidx = NULL;
235 static PyObject *endidx = NULL;
237 /* get the beginning index for the scope of the tab-completion */
238 static PyObject *
239 get_begidx(PyObject *self, PyObject *args)
241 if(!PyArg_NoArgs(args)) {
242 return NULL;
244 Py_INCREF(begidx);
245 return begidx;
248 static char doc_get_begidx[] = "\
249 get_begidx() -> int\n\
250 get the beginning index of the readline tab-completion scope";
252 /* get the ending index for the scope of the tab-completion */
253 static PyObject *
254 get_endidx(PyObject *self, PyObject *args)
256 if(!PyArg_NoArgs(args)) {
257 return NULL;
259 Py_INCREF(endidx);
260 return endidx;
263 static char doc_get_endidx[] = "\
264 get_endidx() -> int\n\
265 get the ending index of the readline tab-completion scope";
268 /* set the tab-completion word-delimiters that readline uses */
270 static PyObject *
271 set_completer_delims(PyObject *self, PyObject *args)
273 char *break_chars;
275 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
276 return NULL;
278 free(rl_completer_word_break_characters);
279 rl_completer_word_break_characters = strdup(break_chars);
280 Py_INCREF(Py_None);
281 return Py_None;
284 static char doc_set_completer_delims[] = "\
285 set_completer_delims(string) -> None\n\
286 set the readline word delimiters for tab-completion";
288 static PyObject *
289 py_add_history(PyObject *self, PyObject *args)
291 char *line;
293 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
294 return NULL;
296 add_history(line);
297 Py_INCREF(Py_None);
298 return Py_None;
301 static char doc_add_history[] = "\
302 add_history(string) -> None\n\
303 add a line to the history buffer";
306 /* get the tab-completion word-delimiters that readline uses */
308 static PyObject *
309 get_completer_delims(PyObject *self, PyObject *args)
311 if(!PyArg_NoArgs(args)) {
312 return NULL;
314 return PyString_FromString(rl_completer_word_break_characters);
317 static char doc_get_completer_delims[] = "\
318 get_completer_delims() -> string\n\
319 get the readline word delimiters for tab-completion";
321 static PyObject *
322 set_completer(PyObject *self, PyObject *args)
324 return set_hook("completer", &completer, &completer_tstate, args);
327 static char doc_set_completer[] = "\
328 set_completer([function]) -> None\n\
329 Set or remove the completer function.\n\
330 The function is called as function(text, state),\n\
331 for state in 0, 1, 2, ..., until it returns a non-string.\n\
332 It should return the next possible completion starting with 'text'.\
335 /* Exported function to read the current line buffer */
337 static PyObject *
338 get_line_buffer(PyObject *self, PyObject *args)
340 if (!PyArg_NoArgs(args))
341 return NULL;
342 return PyString_FromString(rl_line_buffer);
345 static char doc_get_line_buffer[] = "\
346 get_line_buffer() -> string\n\
347 return the current contents of the line buffer.\
350 /* Exported function to insert text into the line buffer */
352 static PyObject *
353 insert_text(PyObject *self, PyObject *args)
355 char *s;
356 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
357 return NULL;
358 rl_insert_text(s);
359 Py_INCREF(Py_None);
360 return Py_None;
364 static char doc_insert_text[] = "\
365 insert_text(string) -> None\n\
366 Insert text into the command line.\
370 /* Table of functions exported by the module */
372 static struct PyMethodDef readline_methods[] =
374 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
375 {"get_line_buffer", get_line_buffer,
376 METH_OLDARGS, doc_get_line_buffer},
377 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
378 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
379 {"read_history_file", read_history_file,
380 METH_VARARGS, doc_read_history_file},
381 {"write_history_file", write_history_file,
382 METH_VARARGS, doc_write_history_file},
383 {"set_history_length", set_history_length,
384 METH_VARARGS, set_history_length_doc},
385 {"get_history_length", get_history_length,
386 METH_VARARGS, get_history_length_doc},
387 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
388 {"get_begidx", get_begidx, METH_OLDARGS, doc_get_begidx},
389 {"get_endidx", get_endidx, METH_OLDARGS, doc_get_endidx},
391 {"set_completer_delims", set_completer_delims,
392 METH_VARARGS, doc_set_completer_delims},
393 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
394 {"get_completer_delims", get_completer_delims,
395 METH_OLDARGS, doc_get_completer_delims},
397 {"set_startup_hook", set_startup_hook, METH_VARARGS, doc_set_startup_hook},
398 #ifdef HAVE_RL_PRE_INPUT_HOOK
399 {"set_pre_input_hook", set_pre_input_hook, METH_VARARGS, doc_set_pre_input_hook},
400 #endif
401 {0, 0}
404 /* C function to call the Python hooks. */
406 static int
407 on_hook(PyObject *func, PyThreadState *tstate)
409 int result = 0;
410 if (func != NULL) {
411 PyObject *r;
412 PyThreadState *save_tstate;
413 /* Note that readline is called with the interpreter
414 lock released! */
415 save_tstate = PyThreadState_Swap(NULL);
416 PyEval_RestoreThread(tstate);
417 r = PyObject_CallFunction(func, NULL);
418 if (r == NULL)
419 goto error;
420 if (r == Py_None)
421 result = 0;
422 else
423 result = PyInt_AsLong(r);
424 Py_DECREF(r);
425 goto done;
426 error:
427 PyErr_Clear();
428 Py_XDECREF(r);
429 done:
430 PyEval_SaveThread();
431 PyThreadState_Swap(save_tstate);
433 return result;
436 static int
437 on_startup_hook(void)
439 return on_hook(startup_hook, startup_hook_tstate);
442 #ifdef HAVE_RL_PRE_INPUT_HOOK
443 static int
444 on_pre_input_hook(void)
446 return on_hook(pre_input_hook, pre_input_hook_tstate);
448 #endif
450 /* C function to call the Python completer. */
452 static char *
453 on_completion(char *text, int state)
455 char *result = NULL;
456 if (completer != NULL) {
457 PyObject *r;
458 PyThreadState *save_tstate;
459 /* Note that readline is called with the interpreter
460 lock released! */
461 save_tstate = PyThreadState_Swap(NULL);
462 PyEval_RestoreThread(completer_tstate);
463 r = PyObject_CallFunction(completer, "si", text, state);
464 if (r == NULL)
465 goto error;
466 if (r == Py_None) {
467 result = NULL;
469 else {
470 char *s = PyString_AsString(r);
471 if (s == NULL)
472 goto error;
473 result = strdup(s);
475 Py_DECREF(r);
476 goto done;
477 error:
478 PyErr_Clear();
479 Py_XDECREF(r);
480 done:
481 PyEval_SaveThread();
482 PyThreadState_Swap(save_tstate);
484 return result;
488 /* a more flexible constructor that saves the "begidx" and "endidx"
489 * before calling the normal completer */
491 char **
492 flex_complete(char *text, int start, int end)
494 Py_XDECREF(begidx);
495 Py_XDECREF(endidx);
496 begidx = PyInt_FromLong((long) start);
497 endidx = PyInt_FromLong((long) end);
498 return completion_matches(text, *on_completion);
501 /* Helper to initialize GNU readline properly. */
503 static void
504 setup_readline(void)
506 rl_readline_name = "python";
507 /* Force rebind of TAB to insert-tab */
508 rl_bind_key('\t', rl_insert);
509 /* Bind both ESC-TAB and ESC-ESC to the completion function */
510 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
511 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
512 /* Set our hook functions */
513 rl_startup_hook = (Function *)on_startup_hook;
514 #ifdef HAVE_RL_PRE_INPUT_HOOK
515 rl_pre_input_hook = (Function *)on_pre_input_hook;
516 #endif
517 /* Set our completion function */
518 rl_attempted_completion_function = (CPPFunction *)flex_complete;
519 /* Set Python word break characters */
520 rl_completer_word_break_characters =
521 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
522 /* All nonalphanums except '.' */
524 begidx = PyInt_FromLong(0L);
525 endidx = PyInt_FromLong(0L);
526 /* Initialize (allows .inputrc to override)
528 * XXX: A bug in the readline-2.2 library causes a memory leak
529 * inside this function. Nothing we can do about it.
531 rl_initialize();
535 /* Interrupt handler */
537 static jmp_buf jbuf;
539 /* ARGSUSED */
540 static void
541 onintr(int sig)
543 longjmp(jbuf, 1);
547 /* Wrapper around GNU readline that handles signals differently. */
549 static char *
550 call_readline(char *prompt)
552 size_t n;
553 char *p, *q;
554 PyOS_sighandler_t old_inthandler;
556 old_inthandler = PyOS_setsig(SIGINT, onintr);
557 if (setjmp(jbuf)) {
558 #ifdef HAVE_SIGRELSE
559 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
560 sigrelse(SIGINT);
561 #endif
562 PyOS_setsig(SIGINT, old_inthandler);
563 return NULL;
565 rl_event_hook = PyOS_InputHook;
566 p = readline(prompt);
567 PyOS_setsig(SIGINT, old_inthandler);
569 /* We must return a buffer allocated with PyMem_Malloc. */
570 if (p == NULL) {
571 p = PyMem_Malloc(1);
572 if (p != NULL)
573 *p = '\0';
574 return p;
576 n = strlen(p);
577 if (n > 0)
578 add_history(p);
579 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
580 release the original. */
581 q = p;
582 p = PyMem_Malloc(n+2);
583 if (p != NULL) {
584 strncpy(p, q, n);
585 p[n] = '\n';
586 p[n+1] = '\0';
588 free(q);
589 return p;
593 /* Initialize the module */
595 static char doc_module[] =
596 "Importing this module enables command line editing using GNU readline.";
598 DL_EXPORT(void)
599 initreadline(void)
601 PyObject *m;
603 m = Py_InitModule4("readline", readline_methods, doc_module,
604 (PyObject *)NULL, PYTHON_API_VERSION);
605 if (isatty(fileno(stdin))) {
606 PyOS_ReadlineFunctionPointer = call_readline;
607 setup_readline();