1997-09-26 18:51:21 -03:00
|
|
|
/* This module makes GNU readline available to Python. It has ideas
|
|
|
|
* contributed by Lee Busby, LLNL, and William Magro, Cornell Theory
|
|
|
|
* Center. The completer interface was inspired by Lele Gaifax.
|
|
|
|
*
|
|
|
|
* More recently, it was largely rewritten by Guido van Rossum who is
|
|
|
|
* now maintaining it.
|
1997-08-05 18:27:50 -03:00
|
|
|
*/
|
|
|
|
|
1997-09-26 18:51:21 -03:00
|
|
|
/* Standard definitions */
|
1997-08-05 18:27:50 -03:00
|
|
|
#include "Python.h"
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include <signal.h>
|
1997-09-26 18:51:21 -03:00
|
|
|
#include <errno.h>
|
1997-08-05 18:27:50 -03:00
|
|
|
|
1997-09-26 18:51:21 -03:00
|
|
|
/* GNU readline definitions */
|
2001-04-13 15:14:27 -03:00
|
|
|
#undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
|
1998-08-04 19:53:56 -03:00
|
|
|
#include <readline/readline.h>
|
|
|
|
#include <readline/history.h>
|
1998-04-10 19:27:42 -03:00
|
|
|
|
2001-07-10 13:45:32 -03:00
|
|
|
#ifdef HAVE_RL_COMPLETION_MATCHES
|
|
|
|
#define completion_matches(x, y) rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
|
|
|
|
#endif
|
|
|
|
|
1997-09-26 18:51:21 -03:00
|
|
|
/* Pointers needed from outside (but not declared in a header file). */
|
2001-01-10 17:03:32 -04:00
|
|
|
extern DL_IMPORT(int) (*PyOS_InputHook)(void);
|
|
|
|
extern DL_IMPORT(char) *(*PyOS_ReadlineFunctionPointer)(char *);
|
1997-08-05 18:27:50 -03:00
|
|
|
|
|
|
|
|
1997-09-26 18:51:21 -03:00
|
|
|
/* Exported function to send one line to readline's init file parser */
|
|
|
|
|
|
|
|
static PyObject *
|
2000-07-10 06:57:19 -03:00
|
|
|
parse_and_bind(PyObject *self, PyObject *args)
|
1997-09-26 18:51:21 -03:00
|
|
|
{
|
1998-12-04 11:34:39 -04:00
|
|
|
char *s, *copy;
|
2000-02-29 09:59:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
|
1997-09-26 18:51:21 -03:00
|
|
|
return NULL;
|
1998-12-04 11:34:39 -04:00
|
|
|
/* Make a copy -- rl_parse_and_bind() modifies its argument */
|
|
|
|
/* Bernard Herzog */
|
|
|
|
copy = malloc(1 + strlen(s));
|
|
|
|
if (copy == NULL)
|
|
|
|
return PyErr_NoMemory();
|
|
|
|
strcpy(copy, s);
|
|
|
|
rl_parse_and_bind(copy);
|
|
|
|
free(copy); /* Free the copy */
|
1997-09-26 18:51:21 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char doc_parse_and_bind[] = "\
|
|
|
|
parse_and_bind(string) -> None\n\
|
|
|
|
Parse and execute single line of a readline init file.\
|
|
|
|
";
|
|
|
|
|
|
|
|
|
|
|
|
/* Exported function to parse a readline init file */
|
|
|
|
|
|
|
|
static PyObject *
|
2000-07-10 06:57:19 -03:00
|
|
|
read_init_file(PyObject *self, PyObject *args)
|
1997-09-26 18:51:21 -03:00
|
|
|
{
|
|
|
|
char *s = NULL;
|
2000-02-29 09:59:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
|
1997-09-26 18:51:21 -03:00
|
|
|
return NULL;
|
|
|
|
errno = rl_read_init_file(s);
|
|
|
|
if (errno)
|
|
|
|
return PyErr_SetFromErrno(PyExc_IOError);
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char doc_read_init_file[] = "\
|
|
|
|
read_init_file([filename]) -> None\n\
|
|
|
|
Parse a readline initialization file.\n\
|
|
|
|
The default filename is the last filename used.\
|
|
|
|
";
|
|
|
|
|
|
|
|
|
2000-07-06 15:55:12 -03:00
|
|
|
/* Exported function to load a readline history file */
|
|
|
|
|
|
|
|
static PyObject *
|
2000-07-10 06:57:19 -03:00
|
|
|
read_history_file(PyObject *self, PyObject *args)
|
2000-07-06 15:55:12 -03:00
|
|
|
{
|
|
|
|
char *s = NULL;
|
|
|
|
if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
|
|
|
|
return NULL;
|
|
|
|
errno = read_history(s);
|
|
|
|
if (errno)
|
|
|
|
return PyErr_SetFromErrno(PyExc_IOError);
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
2000-07-19 13:54:53 -03:00
|
|
|
static int history_length = -1; /* do not truncate history by default */
|
2000-07-06 15:55:12 -03:00
|
|
|
static char doc_read_history_file[] = "\
|
|
|
|
read_history_file([filename]) -> None\n\
|
|
|
|
Load a readline history file.\n\
|
|
|
|
The default filename is ~/.history.\
|
|
|
|
";
|
|
|
|
|
|
|
|
|
|
|
|
/* Exported function to save a readline history file */
|
|
|
|
|
|
|
|
static PyObject *
|
2000-07-10 06:57:19 -03:00
|
|
|
write_history_file(PyObject *self, PyObject *args)
|
2000-07-06 15:55:12 -03:00
|
|
|
{
|
|
|
|
char *s = NULL;
|
|
|
|
if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
|
|
|
|
return NULL;
|
|
|
|
errno = write_history(s);
|
2000-07-19 13:54:53 -03:00
|
|
|
if (!errno && history_length >= 0)
|
|
|
|
history_truncate_file(s, history_length);
|
2000-07-06 15:55:12 -03:00
|
|
|
if (errno)
|
|
|
|
return PyErr_SetFromErrno(PyExc_IOError);
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char doc_write_history_file[] = "\
|
|
|
|
write_history_file([filename]) -> None\n\
|
|
|
|
Save a readline history file.\n\
|
|
|
|
The default filename is ~/.history.\
|
|
|
|
";
|
|
|
|
|
|
|
|
|
2000-07-19 13:54:53 -03:00
|
|
|
static char set_history_length_doc[] = "\
|
|
|
|
set_history_length(length) -> None\n\
|
|
|
|
set the maximal number of items which will be written to\n\
|
|
|
|
the history file. A negative length is used to inhibit\n\
|
|
|
|
history truncation.\n\
|
|
|
|
";
|
|
|
|
|
|
|
|
static PyObject*
|
|
|
|
set_history_length(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
int length = history_length;
|
|
|
|
if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
|
|
|
|
return NULL;
|
|
|
|
history_length = length;
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static char get_history_length_doc[] = "\
|
|
|
|
get_history_length() -> int\n\
|
|
|
|
return the current history length value.\n\
|
|
|
|
";
|
|
|
|
|
|
|
|
static PyObject*
|
|
|
|
get_history_length(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
if (!PyArg_ParseTuple(args, ":get_history_length"))
|
|
|
|
return NULL;
|
|
|
|
return Py_BuildValue("i", history_length);
|
|
|
|
}
|
|
|
|
|
2001-09-30 18:09:59 -03:00
|
|
|
/* Generic hook function setter */
|
2000-07-19 13:54:53 -03:00
|
|
|
|
2001-09-30 18:09:59 -03:00
|
|
|
static PyObject *
|
|
|
|
set_hook(const char * funcname, PyObject **hook_var, PyThreadState **tstate, PyObject *args)
|
|
|
|
{
|
|
|
|
PyObject *function = Py_None;
|
|
|
|
char buf[80];
|
2001-11-28 16:27:42 -04:00
|
|
|
PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
|
2001-09-30 18:09:59 -03:00
|
|
|
if (!PyArg_ParseTuple(args, buf, &function))
|
|
|
|
return NULL;
|
|
|
|
if (function == Py_None) {
|
|
|
|
Py_XDECREF(*hook_var);
|
|
|
|
*hook_var = NULL;
|
|
|
|
*tstate = NULL;
|
|
|
|
}
|
|
|
|
else if (PyCallable_Check(function)) {
|
|
|
|
PyObject *tmp = *hook_var;
|
|
|
|
Py_INCREF(function);
|
|
|
|
*hook_var = function;
|
|
|
|
Py_XDECREF(tmp);
|
|
|
|
*tstate = PyThreadState_Get();
|
|
|
|
}
|
|
|
|
else {
|
2001-11-28 16:27:42 -04:00
|
|
|
PyOS_snprintf(buf, sizeof(buf),
|
|
|
|
"set_%.50s(func): argument not callable",
|
|
|
|
funcname);
|
2001-09-30 18:09:59 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError, buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Exported functions to specify hook functions in Python */
|
|
|
|
|
|
|
|
static PyObject *startup_hook = NULL;
|
|
|
|
static PyThreadState *startup_hook_tstate = NULL;
|
|
|
|
|
|
|
|
#ifdef HAVE_RL_PRE_INPUT_HOOK
|
|
|
|
static PyObject *pre_input_hook = NULL;
|
|
|
|
static PyThreadState *pre_input_hook_tstate = NULL;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
set_startup_hook(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
return set_hook("startup_hook", &startup_hook, &startup_hook_tstate, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char doc_set_startup_hook[] = "\
|
|
|
|
set_startup_hook([function]) -> None\n\
|
|
|
|
Set or remove the startup_hook function.\n\
|
|
|
|
The function is called with no arguments just\n\
|
|
|
|
before readline prints the first prompt.\n\
|
|
|
|
";
|
|
|
|
|
|
|
|
#ifdef HAVE_RL_PRE_INPUT_HOOK
|
|
|
|
static PyObject *
|
|
|
|
set_pre_input_hook(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
return set_hook("pre_input_hook", &pre_input_hook, &pre_input_hook_tstate, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char doc_set_pre_input_hook[] = "\
|
|
|
|
set_pre_input_hook([function]) -> None\n\
|
|
|
|
Set or remove the pre_input_hook function.\n\
|
|
|
|
The function is called with no arguments after the first prompt\n\
|
|
|
|
has been printed and just before readline starts reading input\n\
|
|
|
|
characters.\n\
|
|
|
|
";
|
|
|
|
#endif
|
2000-07-19 13:54:53 -03:00
|
|
|
|
1997-09-26 18:51:21 -03:00
|
|
|
/* Exported function to specify a word completer in Python */
|
|
|
|
|
|
|
|
static PyObject *completer = NULL;
|
2001-09-30 18:09:59 -03:00
|
|
|
static PyThreadState *completer_tstate = NULL;
|
1997-09-26 18:51:21 -03:00
|
|
|
|
1999-11-18 13:51:02 -04:00
|
|
|
static PyObject *begidx = NULL;
|
|
|
|
static PyObject *endidx = NULL;
|
|
|
|
|
|
|
|
/* get the beginning index for the scope of the tab-completion */
|
|
|
|
static PyObject *
|
2000-07-10 06:57:19 -03:00
|
|
|
get_begidx(PyObject *self, PyObject *args)
|
1999-11-18 13:51:02 -04:00
|
|
|
{
|
|
|
|
if(!PyArg_NoArgs(args)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_INCREF(begidx);
|
|
|
|
return begidx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char doc_get_begidx[] = "\
|
|
|
|
get_begidx() -> int\n\
|
|
|
|
get the beginning index of the readline tab-completion scope";
|
|
|
|
|
|
|
|
/* get the ending index for the scope of the tab-completion */
|
|
|
|
static PyObject *
|
2000-07-10 06:57:19 -03:00
|
|
|
get_endidx(PyObject *self, PyObject *args)
|
1999-11-18 13:51:02 -04:00
|
|
|
{
|
|
|
|
if(!PyArg_NoArgs(args)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_INCREF(endidx);
|
|
|
|
return endidx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char doc_get_endidx[] = "\
|
|
|
|
get_endidx() -> int\n\
|
|
|
|
get the ending index of the readline tab-completion scope";
|
|
|
|
|
|
|
|
|
|
|
|
/* set the tab-completion word-delimiters that readline uses */
|
|
|
|
|
|
|
|
static PyObject *
|
2000-07-10 06:57:19 -03:00
|
|
|
set_completer_delims(PyObject *self, PyObject *args)
|
1999-11-18 13:51:02 -04:00
|
|
|
{
|
|
|
|
char *break_chars;
|
|
|
|
|
2000-02-29 09:59:29 -04:00
|
|
|
if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
|
1999-11-18 13:51:02 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
free(rl_completer_word_break_characters);
|
|
|
|
rl_completer_word_break_characters = strdup(break_chars);
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char doc_set_completer_delims[] = "\
|
|
|
|
set_completer_delims(string) -> None\n\
|
|
|
|
set the readline word delimiters for tab-completion";
|
|
|
|
|
2001-10-18 22:18:43 -03:00
|
|
|
static PyObject *
|
|
|
|
py_add_history(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
char *line;
|
|
|
|
|
|
|
|
if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
add_history(line);
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char doc_add_history[] = "\
|
|
|
|
add_history(string) -> None\n\
|
|
|
|
add a line to the history buffer";
|
|
|
|
|
1999-11-18 13:51:02 -04:00
|
|
|
|
|
|
|
/* get the tab-completion word-delimiters that readline uses */
|
|
|
|
|
|
|
|
static PyObject *
|
2000-07-10 06:57:19 -03:00
|
|
|
get_completer_delims(PyObject *self, PyObject *args)
|
1999-11-18 13:51:02 -04:00
|
|
|
{
|
|
|
|
if(!PyArg_NoArgs(args)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return PyString_FromString(rl_completer_word_break_characters);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char doc_get_completer_delims[] = "\
|
|
|
|
get_completer_delims() -> string\n\
|
|
|
|
get the readline word delimiters for tab-completion";
|
|
|
|
|
1997-09-26 18:51:21 -03:00
|
|
|
static PyObject *
|
2000-07-10 06:57:19 -03:00
|
|
|
set_completer(PyObject *self, PyObject *args)
|
1997-09-26 18:51:21 -03:00
|
|
|
{
|
2001-09-30 18:09:59 -03:00
|
|
|
return set_hook("completer", &completer, &completer_tstate, args);
|
1997-09-26 18:51:21 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static char doc_set_completer[] = "\
|
|
|
|
set_completer([function]) -> None\n\
|
|
|
|
Set or remove the completer function.\n\
|
|
|
|
The function is called as function(text, state),\n\
|
2001-08-01 18:44:14 -03:00
|
|
|
for state in 0, 1, 2, ..., until it returns a non-string.\n\
|
1997-09-26 18:51:21 -03:00
|
|
|
It should return the next possible completion starting with 'text'.\
|
|
|
|
";
|
|
|
|
|
1997-10-07 11:53:21 -03:00
|
|
|
/* Exported function to read the current line buffer */
|
|
|
|
|
|
|
|
static PyObject *
|
2000-07-10 06:57:19 -03:00
|
|
|
get_line_buffer(PyObject *self, PyObject *args)
|
1997-10-07 11:53:21 -03:00
|
|
|
{
|
1999-11-18 13:51:02 -04:00
|
|
|
if (!PyArg_NoArgs(args))
|
1997-10-07 11:53:21 -03:00
|
|
|
return NULL;
|
|
|
|
return PyString_FromString(rl_line_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char doc_get_line_buffer[] = "\
|
1998-05-20 12:50:56 -03:00
|
|
|
get_line_buffer() -> string\n\
|
1997-10-07 11:53:21 -03:00
|
|
|
return the current contents of the line buffer.\
|
|
|
|
";
|
|
|
|
|
|
|
|
/* Exported function to insert text into the line buffer */
|
|
|
|
|
|
|
|
static PyObject *
|
2000-07-10 06:57:19 -03:00
|
|
|
insert_text(PyObject *self, PyObject *args)
|
1997-10-07 11:53:21 -03:00
|
|
|
{
|
|
|
|
char *s;
|
2000-02-29 09:59:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "s:insert_text", &s))
|
1997-10-07 11:53:21 -03:00
|
|
|
return NULL;
|
|
|
|
rl_insert_text(s);
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char doc_insert_text[] = "\
|
|
|
|
insert_text(string) -> None\n\
|
|
|
|
Insert text into the command line.\
|
|
|
|
";
|
|
|
|
|
1997-09-26 18:51:21 -03:00
|
|
|
|
|
|
|
/* Table of functions exported by the module */
|
1997-08-05 18:27:50 -03:00
|
|
|
|
|
|
|
static struct PyMethodDef readline_methods[] =
|
1997-09-26 18:51:21 -03:00
|
|
|
{
|
2000-08-02 23:06:16 -03:00
|
|
|
{"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
|
2000-08-02 23:34:44 -03:00
|
|
|
{"get_line_buffer", get_line_buffer,
|
|
|
|
METH_OLDARGS, doc_get_line_buffer},
|
2000-08-02 23:06:16 -03:00
|
|
|
{"insert_text", insert_text, METH_VARARGS, doc_insert_text},
|
|
|
|
{"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
|
|
|
|
{"read_history_file", read_history_file,
|
|
|
|
METH_VARARGS, doc_read_history_file},
|
|
|
|
{"write_history_file", write_history_file,
|
|
|
|
METH_VARARGS, doc_write_history_file},
|
|
|
|
{"set_history_length", set_history_length,
|
|
|
|
METH_VARARGS, set_history_length_doc},
|
|
|
|
{"get_history_length", get_history_length,
|
|
|
|
METH_VARARGS, get_history_length_doc},
|
|
|
|
{"set_completer", set_completer, METH_VARARGS, doc_set_completer},
|
2000-08-02 23:34:44 -03:00
|
|
|
{"get_begidx", get_begidx, METH_OLDARGS, doc_get_begidx},
|
|
|
|
{"get_endidx", get_endidx, METH_OLDARGS, doc_get_endidx},
|
1999-11-18 13:51:02 -04:00
|
|
|
|
2000-08-02 23:34:44 -03:00
|
|
|
{"set_completer_delims", set_completer_delims,
|
|
|
|
METH_VARARGS, doc_set_completer_delims},
|
2001-10-18 22:18:43 -03:00
|
|
|
{"add_history", py_add_history, METH_VARARGS, doc_add_history},
|
2000-08-02 23:34:44 -03:00
|
|
|
{"get_completer_delims", get_completer_delims,
|
|
|
|
METH_OLDARGS, doc_get_completer_delims},
|
2001-09-30 18:09:59 -03:00
|
|
|
|
|
|
|
{"set_startup_hook", set_startup_hook, METH_VARARGS, doc_set_startup_hook},
|
|
|
|
#ifdef HAVE_RL_PRE_INPUT_HOOK
|
|
|
|
{"set_pre_input_hook", set_pre_input_hook, METH_VARARGS, doc_set_pre_input_hook},
|
|
|
|
#endif
|
1997-09-26 18:51:21 -03:00
|
|
|
{0, 0}
|
1997-08-05 18:27:50 -03:00
|
|
|
};
|
|
|
|
|
2001-09-30 18:09:59 -03:00
|
|
|
/* C function to call the Python hooks. */
|
|
|
|
|
|
|
|
static int
|
|
|
|
on_hook(PyObject *func, PyThreadState *tstate)
|
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
if (func != NULL) {
|
|
|
|
PyObject *r;
|
|
|
|
PyThreadState *save_tstate;
|
|
|
|
/* Note that readline is called with the interpreter
|
|
|
|
lock released! */
|
|
|
|
save_tstate = PyThreadState_Swap(NULL);
|
|
|
|
PyEval_RestoreThread(tstate);
|
|
|
|
r = PyObject_CallFunction(func, NULL);
|
|
|
|
if (r == NULL)
|
|
|
|
goto error;
|
|
|
|
if (r == Py_None)
|
|
|
|
result = 0;
|
|
|
|
else
|
|
|
|
result = PyInt_AsLong(r);
|
|
|
|
Py_DECREF(r);
|
|
|
|
goto done;
|
|
|
|
error:
|
|
|
|
PyErr_Clear();
|
|
|
|
Py_XDECREF(r);
|
|
|
|
done:
|
|
|
|
PyEval_SaveThread();
|
|
|
|
PyThreadState_Swap(save_tstate);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
on_startup_hook(void)
|
|
|
|
{
|
|
|
|
return on_hook(startup_hook, startup_hook_tstate);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_RL_PRE_INPUT_HOOK
|
|
|
|
static int
|
|
|
|
on_pre_input_hook(void)
|
|
|
|
{
|
|
|
|
return on_hook(pre_input_hook, pre_input_hook_tstate);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1997-09-26 18:51:21 -03:00
|
|
|
/* C function to call the Python completer. */
|
|
|
|
|
|
|
|
static char *
|
2000-07-10 06:57:19 -03:00
|
|
|
on_completion(char *text, int state)
|
1997-09-26 18:51:21 -03:00
|
|
|
{
|
|
|
|
char *result = NULL;
|
|
|
|
if (completer != NULL) {
|
|
|
|
PyObject *r;
|
1997-10-10 14:39:19 -03:00
|
|
|
PyThreadState *save_tstate;
|
1997-09-26 18:51:21 -03:00
|
|
|
/* Note that readline is called with the interpreter
|
|
|
|
lock released! */
|
1997-10-10 14:39:19 -03:00
|
|
|
save_tstate = PyThreadState_Swap(NULL);
|
2001-09-30 18:09:59 -03:00
|
|
|
PyEval_RestoreThread(completer_tstate);
|
1997-09-26 18:51:21 -03:00
|
|
|
r = PyObject_CallFunction(completer, "si", text, state);
|
|
|
|
if (r == NULL)
|
|
|
|
goto error;
|
|
|
|
if (r == Py_None) {
|
|
|
|
result = NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
char *s = PyString_AsString(r);
|
|
|
|
if (s == NULL)
|
|
|
|
goto error;
|
|
|
|
result = strdup(s);
|
|
|
|
}
|
|
|
|
Py_DECREF(r);
|
|
|
|
goto done;
|
|
|
|
error:
|
|
|
|
PyErr_Clear();
|
|
|
|
Py_XDECREF(r);
|
|
|
|
done:
|
|
|
|
PyEval_SaveThread();
|
1997-10-10 14:39:19 -03:00
|
|
|
PyThreadState_Swap(save_tstate);
|
1997-09-26 18:51:21 -03:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-18 13:51:02 -04:00
|
|
|
/* a more flexible constructor that saves the "begidx" and "endidx"
|
|
|
|
* before calling the normal completer */
|
|
|
|
|
|
|
|
char **
|
2000-07-10 06:57:19 -03:00
|
|
|
flex_complete(char *text, int start, int end)
|
1999-11-18 13:51:02 -04:00
|
|
|
{
|
|
|
|
Py_XDECREF(begidx);
|
|
|
|
Py_XDECREF(endidx);
|
|
|
|
begidx = PyInt_FromLong((long) start);
|
|
|
|
endidx = PyInt_FromLong((long) end);
|
|
|
|
return completion_matches(text, *on_completion);
|
|
|
|
}
|
|
|
|
|
1997-09-26 18:51:21 -03:00
|
|
|
/* Helper to initialize GNU readline properly. */
|
1997-08-05 18:27:50 -03:00
|
|
|
|
1997-09-26 18:51:21 -03:00
|
|
|
static void
|
2000-07-21 03:00:07 -03:00
|
|
|
setup_readline(void)
|
1997-08-05 18:27:50 -03:00
|
|
|
{
|
1997-09-26 18:51:21 -03:00
|
|
|
rl_readline_name = "python";
|
|
|
|
/* Force rebind of TAB to insert-tab */
|
|
|
|
rl_bind_key('\t', rl_insert);
|
|
|
|
/* Bind both ESC-TAB and ESC-ESC to the completion function */
|
|
|
|
rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
|
|
|
|
rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
|
2001-09-30 18:09:59 -03:00
|
|
|
/* Set our hook functions */
|
|
|
|
rl_startup_hook = (Function *)on_startup_hook;
|
|
|
|
#ifdef HAVE_RL_PRE_INPUT_HOOK
|
|
|
|
rl_pre_input_hook = (Function *)on_pre_input_hook;
|
|
|
|
#endif
|
1997-09-26 18:51:21 -03:00
|
|
|
/* Set our completion function */
|
1999-11-18 13:51:02 -04:00
|
|
|
rl_attempted_completion_function = (CPPFunction *)flex_complete;
|
1997-09-26 20:00:37 -03:00
|
|
|
/* Set Python word break characters */
|
|
|
|
rl_completer_word_break_characters =
|
1999-11-18 13:51:02 -04:00
|
|
|
strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
|
1997-09-26 20:00:37 -03:00
|
|
|
/* All nonalphanums except '.' */
|
1999-11-18 13:51:02 -04:00
|
|
|
|
|
|
|
begidx = PyInt_FromLong(0L);
|
|
|
|
endidx = PyInt_FromLong(0L);
|
1999-01-29 17:55:03 -04:00
|
|
|
/* Initialize (allows .inputrc to override)
|
|
|
|
*
|
|
|
|
* XXX: A bug in the readline-2.2 library causes a memory leak
|
|
|
|
* inside this function. Nothing we can do about it.
|
|
|
|
*/
|
1997-09-26 18:51:21 -03:00
|
|
|
rl_initialize();
|
1997-08-05 18:27:50 -03:00
|
|
|
}
|
|
|
|
|
1997-09-26 18:51:21 -03:00
|
|
|
|
|
|
|
/* Interrupt handler */
|
|
|
|
|
|
|
|
static jmp_buf jbuf;
|
|
|
|
|
1997-08-05 18:27:50 -03:00
|
|
|
/* ARGSUSED */
|
2000-07-23 18:18:09 -03:00
|
|
|
static void
|
2000-07-10 06:57:19 -03:00
|
|
|
onintr(int sig)
|
1997-08-05 18:27:50 -03:00
|
|
|
{
|
1997-09-26 18:51:21 -03:00
|
|
|
longjmp(jbuf, 1);
|
1997-08-05 18:27:50 -03:00
|
|
|
}
|
|
|
|
|
1997-09-26 18:51:21 -03:00
|
|
|
|
|
|
|
/* Wrapper around GNU readline that handles signals differently. */
|
1997-08-05 18:27:50 -03:00
|
|
|
|
|
|
|
static char *
|
2000-07-10 06:57:19 -03:00
|
|
|
call_readline(char *prompt)
|
1997-08-05 18:27:50 -03:00
|
|
|
{
|
2000-06-28 18:30:31 -03:00
|
|
|
size_t n;
|
2000-05-03 20:44:39 -03:00
|
|
|
char *p, *q;
|
2000-09-16 13:37:53 -03:00
|
|
|
PyOS_sighandler_t old_inthandler;
|
|
|
|
|
|
|
|
old_inthandler = PyOS_setsig(SIGINT, onintr);
|
1997-08-05 18:27:50 -03:00
|
|
|
if (setjmp(jbuf)) {
|
|
|
|
#ifdef HAVE_SIGRELSE
|
|
|
|
/* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
|
|
|
|
sigrelse(SIGINT);
|
|
|
|
#endif
|
2000-09-16 13:37:53 -03:00
|
|
|
PyOS_setsig(SIGINT, old_inthandler);
|
1997-08-05 18:27:50 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-08-11 15:57:29 -03:00
|
|
|
rl_event_hook = PyOS_InputHook;
|
1997-08-05 18:27:50 -03:00
|
|
|
p = readline(prompt);
|
2000-09-16 13:37:53 -03:00
|
|
|
PyOS_setsig(SIGINT, old_inthandler);
|
2000-05-03 20:44:39 -03:00
|
|
|
|
|
|
|
/* We must return a buffer allocated with PyMem_Malloc. */
|
1997-08-05 18:27:50 -03:00
|
|
|
if (p == NULL) {
|
2000-05-03 20:44:39 -03:00
|
|
|
p = PyMem_Malloc(1);
|
1997-08-05 18:27:50 -03:00
|
|
|
if (p != NULL)
|
|
|
|
*p = '\0';
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
n = strlen(p);
|
|
|
|
if (n > 0)
|
|
|
|
add_history(p);
|
2000-05-03 20:44:39 -03:00
|
|
|
/* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
|
|
|
|
release the original. */
|
|
|
|
q = p;
|
|
|
|
p = PyMem_Malloc(n+2);
|
|
|
|
if (p != NULL) {
|
|
|
|
strncpy(p, q, n);
|
1997-08-05 18:27:50 -03:00
|
|
|
p[n] = '\n';
|
|
|
|
p[n+1] = '\0';
|
|
|
|
}
|
2000-05-03 20:44:39 -03:00
|
|
|
free(q);
|
1997-08-05 18:27:50 -03:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
1997-09-26 18:51:21 -03:00
|
|
|
|
|
|
|
/* Initialize the module */
|
|
|
|
|
|
|
|
static char doc_module[] =
|
|
|
|
"Importing this module enables command line editing using GNU readline.";
|
|
|
|
|
1998-12-04 14:50:17 -04:00
|
|
|
DL_EXPORT(void)
|
2000-07-21 03:00:07 -03:00
|
|
|
initreadline(void)
|
1997-09-26 18:51:21 -03:00
|
|
|
{
|
2000-10-02 12:53:08 -03:00
|
|
|
PyObject *m;
|
1997-09-26 18:51:21 -03:00
|
|
|
|
|
|
|
m = Py_InitModule4("readline", readline_methods, doc_module,
|
|
|
|
(PyObject *)NULL, PYTHON_API_VERSION);
|
|
|
|
if (isatty(fileno(stdin))) {
|
|
|
|
PyOS_ReadlineFunctionPointer = call_readline;
|
|
|
|
setup_readline();
|
|
|
|
}
|
1997-08-05 18:27:50 -03:00
|
|
|
}
|