summaryrefslogtreecommitdiffstats
path: root/Modules/readline.c
diff options
context:
space:
mode:
Diffstat (limited to 'Modules/readline.c')
-rw-r--r--Modules/readline.c256
1 files changed, 187 insertions, 69 deletions
diff --git a/Modules/readline.c b/Modules/readline.c
index e9eabf5..91f7cca 100644
--- a/Modules/readline.c
+++ b/Modules/readline.c
@@ -78,10 +78,12 @@ on_completion_display_matches_hook(char **matches,
static char *completer_word_break_characters;
typedef struct {
+ /* Specify hook functions in Python */
PyObject *completion_display_matches_hook;
PyObject *startup_hook;
PyObject *pre_input_hook;
- PyObject *completer;
+
+ PyObject *completer; /* Specify a word completer in Python */
PyObject *begidx;
PyObject *endidx;
} readlinestate;
@@ -126,20 +128,40 @@ static PyModuleDef readlinemodule;
#define readlinestate_global ((readlinestate *)PyModule_GetState(PyState_FindModule(&readlinemodule)))
+/* Convert to/from multibyte C strings */
+
+static PyObject *
+encode(PyObject *b)
+{
+ return PyUnicode_EncodeLocale(b, "surrogateescape");
+}
+
+static PyObject *
+decode(const char *s)
+{
+ return PyUnicode_DecodeLocale(s, "surrogateescape");
+}
+
+
/* Exported function to send one line to readline's init file parser */
static PyObject *
-parse_and_bind(PyObject *self, PyObject *args)
+parse_and_bind(PyObject *self, PyObject *string)
{
- char *s, *copy;
- if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
+ char *copy;
+ PyObject *encoded = encode(string);
+ if (encoded == NULL) {
return NULL;
+ }
/* Make a copy -- rl_parse_and_bind() modifies its argument */
/* Bernard Herzog */
- copy = PyMem_Malloc(1 + strlen(s));
- if (copy == NULL)
+ copy = PyMem_Malloc(1 + PyBytes_GET_SIZE(encoded));
+ if (copy == NULL) {
+ Py_DECREF(encoded);
return PyErr_NoMemory();
- strcpy(copy, s);
+ }
+ strcpy(copy, PyBytes_AS_STRING(encoded));
+ Py_DECREF(encoded);
rl_parse_and_bind(copy);
PyMem_Free(copy); /* Free the copy */
Py_RETURN_NONE;
@@ -147,7 +169,7 @@ parse_and_bind(PyObject *self, PyObject *args)
PyDoc_STRVAR(doc_parse_and_bind,
"parse_and_bind(string) -> None\n\
-Parse and execute single line of a readline init file.");
+Execute the init line provided in the string argument.");
/* Exported function to parse a readline init file */
@@ -172,7 +194,7 @@ read_init_file(PyObject *self, PyObject *args)
PyDoc_STRVAR(doc_read_init_file,
"read_init_file([filename]) -> None\n\
-Parse a readline initialization file.\n\
+Execute a readline initialization file.\n\
The default filename is the last filename used.");
@@ -237,6 +259,43 @@ Save a readline history file.\n\
The default filename is ~/.history.");
+#ifdef HAVE_RL_APPEND_HISTORY
+/* Exported function to save part of a readline history file */
+
+static PyObject *
+append_history_file(PyObject *self, PyObject *args)
+{
+ int nelements;
+ PyObject *filename_obj = Py_None, *filename_bytes;
+ char *filename;
+ int err;
+ if (!PyArg_ParseTuple(args, "i|O:append_history_file", &nelements, &filename_obj))
+ return NULL;
+ if (filename_obj != Py_None) {
+ if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
+ return NULL;
+ filename = PyBytes_AsString(filename_bytes);
+ } else {
+ filename_bytes = NULL;
+ filename = NULL;
+ }
+ errno = err = append_history(nelements, filename);
+ if (!err && _history_length >= 0)
+ history_truncate_file(filename, _history_length);
+ Py_XDECREF(filename_bytes);
+ errno = err;
+ if (errno)
+ return PyErr_SetFromErrno(PyExc_IOError);
+ Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(doc_append_history_file,
+"append_history_file(nelements[, filename]) -> None\n\
+Append the last nelements items of the history list to file.\n\
+The default filename is ~/.history.");
+#endif
+
+
/* Set history length */
static PyObject*
@@ -251,7 +310,7 @@ set_history_length(PyObject *self, PyObject *args)
PyDoc_STRVAR(set_history_length_doc,
"set_history_length(length) -> None\n\
-set the maximal number of items which will be written to\n\
+set the maximal number of lines which will be written to\n\
the history file. A negative length is used to inhibit\n\
history truncation.");
@@ -266,7 +325,7 @@ get_history_length(PyObject *self, PyObject *noarg)
PyDoc_STRVAR(get_history_length_doc,
"get_history_length() -> int\n\
-return the maximum number of items that will be written to\n\
+return the maximum number of lines that will be written to\n\
the history file.");
@@ -299,13 +358,6 @@ set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
}
-/* Exported functions to specify hook functions in Python */
-
-
-#ifdef HAVE_RL_PRE_INPUT_HOOK
-
-#endif
-
static PyObject *
set_completion_display_matches_hook(PyObject *self, PyObject *args)
{
@@ -341,7 +393,7 @@ set_startup_hook(PyObject *self, PyObject *args)
PyDoc_STRVAR(doc_set_startup_hook,
"set_startup_hook([function]) -> None\n\
-Set or remove the startup_hook function.\n\
+Set or remove the function invoked by the rl_startup_hook callback.\n\
The function is called with no arguments just\n\
before readline prints the first prompt.");
@@ -358,7 +410,7 @@ set_pre_input_hook(PyObject *self, PyObject *args)
PyDoc_STRVAR(doc_set_pre_input_hook,
"set_pre_input_hook([function]) -> None\n\
-Set or remove the pre_input_hook function.\n\
+Set or remove the function invoked by the rl_pre_input_hook callback.\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.");
@@ -366,14 +418,6 @@ characters.");
#endif
-/* Exported function to specify a word completer in Python */
-
-
-
-
-
-
-
/* Get the completion type for the scope of the tab-completion */
static PyObject *
get_completion_type(PyObject *self, PyObject *noarg)
@@ -397,7 +441,7 @@ get_begidx(PyObject *self, PyObject *noarg)
PyDoc_STRVAR(doc_get_begidx,
"get_begidx() -> int\n\
-get the beginning index of the readline tab-completion scope");
+get the beginning index of the completion scope");
/* Get the ending index for the scope of the tab-completion */
@@ -411,23 +455,24 @@ get_endidx(PyObject *self, PyObject *noarg)
PyDoc_STRVAR(doc_get_endidx,
"get_endidx() -> int\n\
-get the ending index of the readline tab-completion scope");
+get the ending index of the completion scope");
/* Set the tab-completion word-delimiters that readline uses */
static PyObject *
-set_completer_delims(PyObject *self, PyObject *args)
+set_completer_delims(PyObject *self, PyObject *string)
{
char *break_chars;
-
- if (!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
+ PyObject *encoded = encode(string);
+ if (encoded == NULL) {
return NULL;
}
/* Keep a reference to the allocated memory in the module state in case
some other module modifies rl_completer_word_break_characters
(see issue #17289). */
- break_chars = strdup(break_chars);
+ break_chars = strdup(PyBytes_AS_STRING(encoded));
+ Py_DECREF(encoded);
if (break_chars) {
free(completer_word_break_characters);
completer_word_break_characters = break_chars;
@@ -440,7 +485,7 @@ set_completer_delims(PyObject *self, PyObject *args)
PyDoc_STRVAR(doc_set_completer_delims,
"set_completer_delims(string) -> None\n\
-set the readline word delimiters for tab-completion");
+set the word delimiters for completion");
/* _py_free_history_entry: Utility function to free a history entry. */
@@ -480,7 +525,7 @@ py_remove_history(PyObject *self, PyObject *args)
int entry_number;
HIST_ENTRY *entry;
- if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
+ if (!PyArg_ParseTuple(args, "i:remove_history_item", &entry_number))
return NULL;
if (entry_number < 0) {
PyErr_SetString(PyExc_ValueError,
@@ -507,10 +552,11 @@ static PyObject *
py_replace_history(PyObject *self, PyObject *args)
{
int entry_number;
- char *line;
+ PyObject *line;
+ PyObject *encoded;
HIST_ENTRY *old_entry;
- if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number,
+ if (!PyArg_ParseTuple(args, "iU:replace_history_item", &entry_number,
&line)) {
return NULL;
}
@@ -519,7 +565,12 @@ py_replace_history(PyObject *self, PyObject *args)
"History index cannot be negative");
return NULL;
}
- old_entry = replace_history_entry(entry_number, line, (void *)NULL);
+ encoded = encode(line);
+ if (encoded == NULL) {
+ return NULL;
+ }
+ old_entry = replace_history_entry(entry_number, PyBytes_AS_STRING(encoded), (void *)NULL);
+ Py_DECREF(encoded);
if (!old_entry) {
PyErr_Format(PyExc_ValueError,
"No history item at position %d",
@@ -538,20 +589,20 @@ replaces history item given by its position with contents of line");
/* Add a line to the history buffer */
static PyObject *
-py_add_history(PyObject *self, PyObject *args)
+py_add_history(PyObject *self, PyObject *string)
{
- char *line;
-
- if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
+ PyObject *encoded = encode(string);
+ if (encoded == NULL) {
return NULL;
}
- add_history(line);
+ add_history(PyBytes_AS_STRING(encoded));
+ Py_DECREF(encoded);
Py_RETURN_NONE;
}
PyDoc_STRVAR(doc_add_history,
"add_history(string) -> None\n\
-add a line to the history buffer");
+add an item to the history buffer");
/* Get the tab-completion word-delimiters that readline uses */
@@ -559,12 +610,12 @@ add a line to the history buffer");
static PyObject *
get_completer_delims(PyObject *self, PyObject *noarg)
{
- return PyUnicode_FromString(rl_completer_word_break_characters);
+ return decode(rl_completer_word_break_characters);
}
PyDoc_STRVAR(doc_get_completer_delims,
"get_completer_delims() -> string\n\
-get the readline word delimiters for tab-completion");
+get the word delimiters for completion");
/* Set the completer function */
@@ -625,7 +676,7 @@ get_history_item(PyObject *self, PyObject *args)
int idx = 0;
HIST_ENTRY *hist_ent;
- if (!PyArg_ParseTuple(args, "i:index", &idx))
+ if (!PyArg_ParseTuple(args, "i:get_history_item", &idx))
return NULL;
#ifdef __APPLE__
if (using_libedit_emulation) {
@@ -649,7 +700,7 @@ get_history_item(PyObject *self, PyObject *args)
}
#endif /* __APPLE__ */
if ((hist_ent = history_get(idx)))
- return PyUnicode_FromString(hist_ent->line);
+ return decode(hist_ent->line);
else {
Py_RETURN_NONE;
}
@@ -678,7 +729,7 @@ return the current (not the maximum) length of history.");
static PyObject *
get_line_buffer(PyObject *self, PyObject *noarg)
{
- return PyUnicode_FromString(rl_line_buffer);
+ return decode(rl_line_buffer);
}
PyDoc_STRVAR(doc_get_line_buffer,
@@ -706,18 +757,20 @@ Clear the current readline history.");
/* Exported function to insert text into the line buffer */
static PyObject *
-insert_text(PyObject *self, PyObject *args)
+insert_text(PyObject *self, PyObject *string)
{
- char *s;
- if (!PyArg_ParseTuple(args, "s:insert_text", &s))
+ PyObject *encoded = encode(string);
+ if (encoded == NULL) {
return NULL;
- rl_insert_text(s);
+ }
+ rl_insert_text(PyBytes_AS_STRING(encoded));
+ Py_DECREF(encoded);
Py_RETURN_NONE;
}
PyDoc_STRVAR(doc_insert_text,
"insert_text(string) -> None\n\
-Insert text into the command line.");
+Insert text into the line buffer at the cursor position.");
/* Redisplay the line buffer */
@@ -739,15 +792,19 @@ contents of the line buffer.");
static struct PyMethodDef readline_methods[] =
{
- {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
+ {"parse_and_bind", parse_and_bind, METH_O, doc_parse_and_bind},
{"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
- {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
+ {"insert_text", insert_text, METH_O, doc_insert_text},
{"redisplay", redisplay, METH_NOARGS, doc_redisplay},
{"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},
+#ifdef HAVE_RL_APPEND_HISTORY
+ {"append_history_file", append_history_file,
+ METH_VARARGS, doc_append_history_file},
+#endif
{"get_history_item", get_history_item,
METH_VARARGS, doc_get_history_item},
{"get_current_history_length", (PyCFunction)get_current_history_length,
@@ -764,8 +821,8 @@ static struct PyMethodDef readline_methods[] =
{"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
{"set_completer_delims", set_completer_delims,
- METH_VARARGS, doc_set_completer_delims},
- {"add_history", py_add_history, METH_VARARGS, doc_add_history},
+ METH_O, doc_set_completer_delims},
+ {"add_history", py_add_history, METH_O, doc_add_history},
{"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
{"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
{"get_completer_delims", get_completer_delims,
@@ -862,7 +919,7 @@ on_completion_display_matches_hook(char **matches,
int num_matches, int max_length)
{
int i;
- PyObject *m=NULL, *s=NULL, *r=NULL;
+ PyObject *sub, *m=NULL, *s=NULL, *r=NULL;
#ifdef WITH_THREAD
PyGILState_STATE gilstate = PyGILState_Ensure();
#endif
@@ -870,16 +927,17 @@ on_completion_display_matches_hook(char **matches,
if (m == NULL)
goto error;
for (i = 0; i < num_matches; i++) {
- s = PyUnicode_FromString(matches[i+1]);
+ s = decode(matches[i+1]);
if (s == NULL)
goto error;
if (PyList_SetItem(m, i, s) == -1)
goto error;
}
+ sub = decode(matches[0]);
r = PyObject_CallFunction(readlinestate_global->completion_display_matches_hook,
- "sOi", matches[0], m, max_length);
+ "NNi", sub, m, max_length);
- Py_DECREF(m); m=NULL;
+ m=NULL;
if (r == NULL ||
(r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) {
@@ -900,6 +958,26 @@ on_completion_display_matches_hook(char **matches,
#endif
+#ifdef HAVE_RL_RESIZE_TERMINAL
+static volatile sig_atomic_t sigwinch_received;
+static PyOS_sighandler_t sigwinch_ohandler;
+
+static void
+readline_sigwinch_handler(int signum)
+{
+ sigwinch_received = 1;
+ if (sigwinch_ohandler &&
+ sigwinch_ohandler != SIG_IGN && sigwinch_ohandler != SIG_DFL)
+ sigwinch_ohandler(signum);
+
+#ifndef HAVE_SIGACTION
+ /* If the handler was installed with signal() rather than sigaction(),
+ we need to reinstall it. */
+ PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
+#endif
+}
+#endif
+
/* C function to call the Python completer. */
static char *
@@ -907,22 +985,24 @@ on_completion(const char *text, int state)
{
char *result = NULL;
if (readlinestate_global->completer != NULL) {
- PyObject *r;
+ PyObject *r = NULL, *t;
#ifdef WITH_THREAD
PyGILState_STATE gilstate = PyGILState_Ensure();
#endif
rl_attempted_completion_over = 1;
- r = PyObject_CallFunction(readlinestate_global->completer, "si", text, state);
+ t = decode(text);
+ r = PyObject_CallFunction(readlinestate_global->completer, "Ni", t, state);
if (r == NULL)
goto error;
if (r == Py_None) {
result = NULL;
}
else {
- char *s = _PyUnicode_AsString(r);
- if (s == NULL)
+ PyObject *encoded = encode(r);
+ if (encoded == NULL)
goto error;
- result = strdup(s);
+ result = strdup(PyBytes_AS_STRING(encoded));
+ Py_DECREF(encoded);
}
Py_DECREF(r);
goto done;
@@ -946,6 +1026,9 @@ static char **
flex_complete(const char *text, int start, int end)
{
char **result;
+ char saved;
+ size_t start_size, end_size;
+ wchar_t *s;
#ifdef WITH_THREAD
PyGILState_STATE gilstate = PyGILState_Ensure();
#endif
@@ -955,6 +1038,27 @@ flex_complete(const char *text, int start, int end)
#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
rl_completion_suppress_append = 0;
#endif
+
+ saved = rl_line_buffer[start];
+ rl_line_buffer[start] = 0;
+ s = Py_DecodeLocale(rl_line_buffer, &start_size);
+ rl_line_buffer[start] = saved;
+ if (s == NULL) {
+ goto done;
+ }
+ PyMem_RawFree(s);
+ saved = rl_line_buffer[end];
+ rl_line_buffer[end] = 0;
+ s = Py_DecodeLocale(rl_line_buffer + start, &end_size);
+ rl_line_buffer[end] = saved;
+ if (s == NULL) {
+ goto done;
+ }
+ PyMem_RawFree(s);
+ start = (int)start_size;
+ end = start + (int)end_size;
+
+done:
Py_XDECREF(readlinestate_global->begidx);
Py_XDECREF(readlinestate_global->endidx);
readlinestate_global->begidx = PyLong_FromLong((long) start);
@@ -1005,6 +1109,10 @@ setup_readline(readlinestate *mod_state)
/* 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);
+#ifdef HAVE_RL_RESIZE_TERMINAL
+ /* Set up signal handler for window resize */
+ sigwinch_ohandler = PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
+#endif
/* Set our hook functions */
rl_startup_hook = on_startup_hook;
#ifdef HAVE_RL_PRE_INPUT_HOOK
@@ -1023,7 +1131,7 @@ setup_readline(readlinestate *mod_state)
#ifndef __APPLE__
if (!isatty(STDOUT_FILENO)) {
- /* Issue #19884: stdout is no a terminal. Disable meta modifier
+ /* Issue #19884: stdout is not a terminal. Disable meta modifier
keys to not write the ANSI sequence "\033[1034h" into stdout. On
terminals supporting 8 bit characters like TERM=xterm-256color
(which is now the default Fedora since Fedora 18), the meta key is
@@ -1090,6 +1198,13 @@ readline_until_enter_or_signal(const char *prompt, int *signal)
struct timeval *timeoutp = NULL;
if (PyOS_InputHook)
timeoutp = &timeout;
+#ifdef HAVE_RL_RESIZE_TERMINAL
+ /* Update readline's view of the window size after SIGWINCH */
+ if (sigwinch_received) {
+ sigwinch_received = 0;
+ rl_resize_terminal();
+ }
+#endif
FD_SET(fileno(rl_instream), &selectset);
/* select resets selectset if no input was available */
has_input = select(fileno(rl_instream) + 1, &selectset,
@@ -1112,6 +1227,9 @@ readline_until_enter_or_signal(const char *prompt, int *signal)
#endif
if (s < 0) {
rl_free_line_state();
+#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0700
+ rl_callback_sigcleanup();
+#endif
rl_cleanup_after_signal();
rl_callback_handler_remove();
*signal = 1;