+++ /dev/null
-/* This module makes GNU readline available to Python. It has ideas\r
- * contributed by Lee Busby, LLNL, and William Magro, Cornell Theory\r
- * Center. The completer interface was inspired by Lele Gaifax. More\r
- * recently, it was largely rewritten by Guido van Rossum.\r
- */\r
-\r
-/* Standard definitions */\r
-#include "Python.h"\r
-#include <setjmp.h>\r
-#include <signal.h>\r
-#include <errno.h>\r
-#include <sys/time.h>\r
-\r
-#if defined(HAVE_SETLOCALE)\r
-/* GNU readline() mistakenly sets the LC_CTYPE locale.\r
- * This is evil. Only the user or the app's main() should do this!\r
- * We must save and restore the locale around the rl_initialize() call.\r
- */\r
-#define SAVE_LOCALE\r
-#include <locale.h>\r
-#endif\r
-\r
-#ifdef SAVE_LOCALE\r
-# define RESTORE_LOCALE(sl) { setlocale(LC_CTYPE, sl); free(sl); }\r
-#else\r
-# define RESTORE_LOCALE(sl)\r
-#endif\r
-\r
-/* GNU readline definitions */\r
-#undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */\r
-#include <readline/readline.h>\r
-#include <readline/history.h>\r
-\r
-#ifdef HAVE_RL_COMPLETION_MATCHES\r
-#define completion_matches(x, y) \\r
- rl_completion_matches((x), ((rl_compentry_func_t *)(y)))\r
-#else\r
-#if defined(_RL_FUNCTION_TYPEDEF)\r
-extern char **completion_matches(char *, rl_compentry_func_t *);\r
-#else\r
-\r
-#if !defined(__APPLE__)\r
-extern char **completion_matches(char *, CPFunction *);\r
-#endif\r
-#endif\r
-#endif\r
-\r
-#ifdef __APPLE__\r
-/*\r
- * It is possible to link the readline module to the readline\r
- * emulation library of editline/libedit.\r
- *\r
- * On OSX this emulation library is not 100% API compatible\r
- * with the "real" readline and cannot be detected at compile-time,\r
- * hence we use a runtime check to detect if we're using libedit\r
- *\r
- * Currently there is one know API incompatibility:\r
- * - 'get_history' has a 1-based index with GNU readline, and a 0-based\r
- * index with libedit's emulation.\r
- * - Note that replace_history and remove_history use a 0-based index\r
- * with both implementation.\r
- */\r
-static int using_libedit_emulation = 0;\r
-static const char libedit_version_tag[] = "EditLine wrapper";\r
-#endif /* __APPLE__ */\r
-\r
-static void\r
-on_completion_display_matches_hook(char **matches,\r
- int num_matches, int max_length);\r
-\r
-\r
-/* Exported function to send one line to readline's init file parser */\r
-\r
-static PyObject *\r
-parse_and_bind(PyObject *self, PyObject *args)\r
-{\r
- char *s, *copy;\r
- if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))\r
- return NULL;\r
- /* Make a copy -- rl_parse_and_bind() modifies its argument */\r
- /* Bernard Herzog */\r
- copy = malloc(1 + strlen(s));\r
- if (copy == NULL)\r
- return PyErr_NoMemory();\r
- strcpy(copy, s);\r
- rl_parse_and_bind(copy);\r
- free(copy); /* Free the copy */\r
- Py_RETURN_NONE;\r
-}\r
-\r
-PyDoc_STRVAR(doc_parse_and_bind,\r
-"parse_and_bind(string) -> None\n\\r
-Parse and execute single line of a readline init file.");\r
-\r
-\r
-/* Exported function to parse a readline init file */\r
-\r
-static PyObject *\r
-read_init_file(PyObject *self, PyObject *args)\r
-{\r
- char *s = NULL;\r
- if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))\r
- return NULL;\r
- errno = rl_read_init_file(s);\r
- if (errno)\r
- return PyErr_SetFromErrno(PyExc_IOError);\r
- Py_RETURN_NONE;\r
-}\r
-\r
-PyDoc_STRVAR(doc_read_init_file,\r
-"read_init_file([filename]) -> None\n\\r
-Parse a readline initialization file.\n\\r
-The default filename is the last filename used.");\r
-\r
-\r
-/* Exported function to load a readline history file */\r
-\r
-static PyObject *\r
-read_history_file(PyObject *self, PyObject *args)\r
-{\r
- char *s = NULL;\r
- if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))\r
- return NULL;\r
- errno = read_history(s);\r
- if (errno)\r
- return PyErr_SetFromErrno(PyExc_IOError);\r
- Py_RETURN_NONE;\r
-}\r
-\r
-static int _history_length = -1; /* do not truncate history by default */\r
-PyDoc_STRVAR(doc_read_history_file,\r
-"read_history_file([filename]) -> None\n\\r
-Load a readline history file.\n\\r
-The default filename is ~/.history.");\r
-\r
-\r
-/* Exported function to save a readline history file */\r
-\r
-static PyObject *\r
-write_history_file(PyObject *self, PyObject *args)\r
-{\r
- char *s = NULL;\r
- if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))\r
- return NULL;\r
- errno = write_history(s);\r
- if (!errno && _history_length >= 0)\r
- history_truncate_file(s, _history_length);\r
- if (errno)\r
- return PyErr_SetFromErrno(PyExc_IOError);\r
- Py_RETURN_NONE;\r
-}\r
-\r
-PyDoc_STRVAR(doc_write_history_file,\r
-"write_history_file([filename]) -> None\n\\r
-Save a readline history file.\n\\r
-The default filename is ~/.history.");\r
-\r
-\r
-/* Set history length */\r
-\r
-static PyObject*\r
-set_history_length(PyObject *self, PyObject *args)\r
-{\r
- int length = _history_length;\r
- if (!PyArg_ParseTuple(args, "i:set_history_length", &length))\r
- return NULL;\r
- _history_length = length;\r
- Py_RETURN_NONE;\r
-}\r
-\r
-PyDoc_STRVAR(set_history_length_doc,\r
-"set_history_length(length) -> None\n\\r
-set the maximal number of items which will be written to\n\\r
-the history file. A negative length is used to inhibit\n\\r
-history truncation.");\r
-\r
-\r
-/* Get history length */\r
-\r
-static PyObject*\r
-get_history_length(PyObject *self, PyObject *noarg)\r
-{\r
- return PyInt_FromLong(_history_length);\r
-}\r
-\r
-PyDoc_STRVAR(get_history_length_doc,\r
-"get_history_length() -> int\n\\r
-return the maximum number of items that will be written to\n\\r
-the history file.");\r
-\r
-\r
-/* Generic hook function setter */\r
-\r
-static PyObject *\r
-set_hook(const char *funcname, PyObject **hook_var, PyObject *args)\r
-{\r
- PyObject *function = Py_None;\r
- char buf[80];\r
- PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);\r
- if (!PyArg_ParseTuple(args, buf, &function))\r
- return NULL;\r
- if (function == Py_None) {\r
- Py_XDECREF(*hook_var);\r
- *hook_var = NULL;\r
- }\r
- else if (PyCallable_Check(function)) {\r
- PyObject *tmp = *hook_var;\r
- Py_INCREF(function);\r
- *hook_var = function;\r
- Py_XDECREF(tmp);\r
- }\r
- else {\r
- PyOS_snprintf(buf, sizeof(buf),\r
- "set_%.50s(func): argument not callable",\r
- funcname);\r
- PyErr_SetString(PyExc_TypeError, buf);\r
- return NULL;\r
- }\r
- Py_RETURN_NONE;\r
-}\r
-\r
-\r
-/* Exported functions to specify hook functions in Python */\r
-\r
-static PyObject *completion_display_matches_hook = NULL;\r
-static PyObject *startup_hook = NULL;\r
-\r
-#ifdef HAVE_RL_PRE_INPUT_HOOK\r
-static PyObject *pre_input_hook = NULL;\r
-#endif\r
-\r
-static PyObject *\r
-set_completion_display_matches_hook(PyObject *self, PyObject *args)\r
-{\r
- PyObject *result = set_hook("completion_display_matches_hook",\r
- &completion_display_matches_hook, args);\r
-#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK\r
- /* We cannot set this hook globally, since it replaces the\r
- default completion display. */\r
- rl_completion_display_matches_hook =\r
- completion_display_matches_hook ?\r
-#if defined(_RL_FUNCTION_TYPEDEF)\r
- (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;\r
-#else\r
- (VFunction *)on_completion_display_matches_hook : 0;\r
-#endif\r
-#endif\r
- return result;\r
-\r
-}\r
-\r
-PyDoc_STRVAR(doc_set_completion_display_matches_hook,\r
-"set_completion_display_matches_hook([function]) -> None\n\\r
-Set or remove the completion display function.\n\\r
-The function is called as\n\\r
- function(substitution, [matches], longest_match_length)\n\\r
-once each time matches need to be displayed.");\r
-\r
-static PyObject *\r
-set_startup_hook(PyObject *self, PyObject *args)\r
-{\r
- return set_hook("startup_hook", &startup_hook, args);\r
-}\r
-\r
-PyDoc_STRVAR(doc_set_startup_hook,\r
-"set_startup_hook([function]) -> None\n\\r
-Set or remove the startup_hook function.\n\\r
-The function is called with no arguments just\n\\r
-before readline prints the first prompt.");\r
-\r
-\r
-#ifdef HAVE_RL_PRE_INPUT_HOOK\r
-\r
-/* Set pre-input hook */\r
-\r
-static PyObject *\r
-set_pre_input_hook(PyObject *self, PyObject *args)\r
-{\r
- return set_hook("pre_input_hook", &pre_input_hook, args);\r
-}\r
-\r
-PyDoc_STRVAR(doc_set_pre_input_hook,\r
-"set_pre_input_hook([function]) -> None\n\\r
-Set or remove the pre_input_hook function.\n\\r
-The function is called with no arguments after the first prompt\n\\r
-has been printed and just before readline starts reading input\n\\r
-characters.");\r
-\r
-#endif\r
-\r
-\r
-/* Exported function to specify a word completer in Python */\r
-\r
-static PyObject *completer = NULL;\r
-\r
-static PyObject *begidx = NULL;\r
-static PyObject *endidx = NULL;\r
-\r
-\r
-/* Get the completion type for the scope of the tab-completion */\r
-static PyObject *\r
-get_completion_type(PyObject *self, PyObject *noarg)\r
-{\r
- return PyInt_FromLong(rl_completion_type);\r
-}\r
-\r
-PyDoc_STRVAR(doc_get_completion_type,\r
-"get_completion_type() -> int\n\\r
-Get the type of completion being attempted.");\r
-\r
-\r
-/* Get the beginning index for the scope of the tab-completion */\r
-\r
-static PyObject *\r
-get_begidx(PyObject *self, PyObject *noarg)\r
-{\r
- Py_INCREF(begidx);\r
- return begidx;\r
-}\r
-\r
-PyDoc_STRVAR(doc_get_begidx,\r
-"get_begidx() -> int\n\\r
-get the beginning index of the readline tab-completion scope");\r
-\r
-\r
-/* Get the ending index for the scope of the tab-completion */\r
-\r
-static PyObject *\r
-get_endidx(PyObject *self, PyObject *noarg)\r
-{\r
- Py_INCREF(endidx);\r
- return endidx;\r
-}\r
-\r
-PyDoc_STRVAR(doc_get_endidx,\r
-"get_endidx() -> int\n\\r
-get the ending index of the readline tab-completion scope");\r
-\r
-\r
-/* Set the tab-completion word-delimiters that readline uses */\r
-\r
-static PyObject *\r
-set_completer_delims(PyObject *self, PyObject *args)\r
-{\r
- char *break_chars;\r
-\r
- if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {\r
- return NULL;\r
- }\r
- free((void*)rl_completer_word_break_characters);\r
- rl_completer_word_break_characters = strdup(break_chars);\r
- Py_RETURN_NONE;\r
-}\r
-\r
-PyDoc_STRVAR(doc_set_completer_delims,\r
-"set_completer_delims(string) -> None\n\\r
-set the readline word delimiters for tab-completion");\r
-\r
-/* _py_free_history_entry: Utility function to free a history entry. */\r
-\r
-#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500\r
-\r
-/* Readline version >= 5.0 introduced a timestamp field into the history entry\r
- structure; this needs to be freed to avoid a memory leak. This version of\r
- readline also introduced the handy 'free_history_entry' function, which\r
- takes care of the timestamp. */\r
-\r
-static void\r
-_py_free_history_entry(HIST_ENTRY *entry)\r
-{\r
- histdata_t data = free_history_entry(entry);\r
- free(data);\r
-}\r
-\r
-#else\r
-\r
-/* No free_history_entry function; free everything manually. */\r
-\r
-static void\r
-_py_free_history_entry(HIST_ENTRY *entry)\r
-{\r
- if (entry->line)\r
- free((void *)entry->line);\r
- if (entry->data)\r
- free(entry->data);\r
- free(entry);\r
-}\r
-\r
-#endif\r
-\r
-static PyObject *\r
-py_remove_history(PyObject *self, PyObject *args)\r
-{\r
- int entry_number;\r
- HIST_ENTRY *entry;\r
-\r
- if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))\r
- return NULL;\r
- if (entry_number < 0) {\r
- PyErr_SetString(PyExc_ValueError,\r
- "History index cannot be negative");\r
- return NULL;\r
- }\r
- entry = remove_history(entry_number);\r
- if (!entry) {\r
- PyErr_Format(PyExc_ValueError,\r
- "No history item at position %d",\r
- entry_number);\r
- return NULL;\r
- }\r
- /* free memory allocated for the history entry */\r
- _py_free_history_entry(entry);\r
- Py_RETURN_NONE;\r
-}\r
-\r
-PyDoc_STRVAR(doc_remove_history,\r
-"remove_history_item(pos) -> None\n\\r
-remove history item given by its position");\r
-\r
-static PyObject *\r
-py_replace_history(PyObject *self, PyObject *args)\r
-{\r
- int entry_number;\r
- char *line;\r
- HIST_ENTRY *old_entry;\r
-\r
- if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number,\r
- &line)) {\r
- return NULL;\r
- }\r
- if (entry_number < 0) {\r
- PyErr_SetString(PyExc_ValueError,\r
- "History index cannot be negative");\r
- return NULL;\r
- }\r
- old_entry = replace_history_entry(entry_number, line, (void *)NULL);\r
- if (!old_entry) {\r
- PyErr_Format(PyExc_ValueError,\r
- "No history item at position %d",\r
- entry_number);\r
- return NULL;\r
- }\r
- /* free memory allocated for the old history entry */\r
- _py_free_history_entry(old_entry);\r
- Py_RETURN_NONE;\r
-}\r
-\r
-PyDoc_STRVAR(doc_replace_history,\r
-"replace_history_item(pos, line) -> None\n\\r
-replaces history item given by its position with contents of line");\r
-\r
-/* Add a line to the history buffer */\r
-\r
-static PyObject *\r
-py_add_history(PyObject *self, PyObject *args)\r
-{\r
- char *line;\r
-\r
- if(!PyArg_ParseTuple(args, "s:add_history", &line)) {\r
- return NULL;\r
- }\r
- add_history(line);\r
- Py_RETURN_NONE;\r
-}\r
-\r
-PyDoc_STRVAR(doc_add_history,\r
-"add_history(string) -> None\n\\r
-add a line to the history buffer");\r
-\r
-\r
-/* Get the tab-completion word-delimiters that readline uses */\r
-\r
-static PyObject *\r
-get_completer_delims(PyObject *self, PyObject *noarg)\r
-{\r
- return PyString_FromString(rl_completer_word_break_characters);\r
-}\r
-\r
-PyDoc_STRVAR(doc_get_completer_delims,\r
-"get_completer_delims() -> string\n\\r
-get the readline word delimiters for tab-completion");\r
-\r
-\r
-/* Set the completer function */\r
-\r
-static PyObject *\r
-set_completer(PyObject *self, PyObject *args)\r
-{\r
- return set_hook("completer", &completer, args);\r
-}\r
-\r
-PyDoc_STRVAR(doc_set_completer,\r
-"set_completer([function]) -> None\n\\r
-Set or remove the completer function.\n\\r
-The function is called as function(text, state),\n\\r
-for state in 0, 1, 2, ..., until it returns a non-string.\n\\r
-It should return the next possible completion starting with 'text'.");\r
-\r
-\r
-static PyObject *\r
-get_completer(PyObject *self, PyObject *noargs)\r
-{\r
- if (completer == NULL) {\r
- Py_RETURN_NONE;\r
- }\r
- Py_INCREF(completer);\r
- return completer;\r
-}\r
-\r
-PyDoc_STRVAR(doc_get_completer,\r
-"get_completer() -> function\n\\r
-\n\\r
-Returns current completer function.");\r
-\r
-/* Private function to get current length of history. XXX It may be\r
- * possible to replace this with a direct use of history_length instead,\r
- * but it's not clear whether BSD's libedit keeps history_length up to date.\r
- * See issue #8065.*/\r
-\r
-static int\r
-_py_get_history_length(void)\r
-{\r
- HISTORY_STATE *hist_st = history_get_history_state();\r
- int length = hist_st->length;\r
- /* the history docs don't say so, but the address of hist_st changes each\r
- time history_get_history_state is called which makes me think it's\r
- freshly malloc'd memory... on the other hand, the address of the last\r
- line stays the same as long as history isn't extended, so it appears to\r
- be malloc'd but managed by the history package... */\r
- free(hist_st);\r
- return length;\r
-}\r
-\r
-/* Exported function to get any element of history */\r
-\r
-static PyObject *\r
-get_history_item(PyObject *self, PyObject *args)\r
-{\r
- int idx = 0;\r
- HIST_ENTRY *hist_ent;\r
-\r
- if (!PyArg_ParseTuple(args, "i:index", &idx))\r
- return NULL;\r
-#ifdef __APPLE__\r
- if (using_libedit_emulation) {\r
- /* Libedit emulation uses 0-based indexes,\r
- * the real one uses 1-based indexes,\r
- * adjust the index to ensure that Python\r
- * code doesn't have to worry about the\r
- * difference.\r
- */\r
- int length = _py_get_history_length();\r
- idx --;\r
-\r
- /*\r
- * Apple's readline emulation crashes when\r
- * the index is out of range, therefore\r
- * test for that and fail gracefully.\r
- */\r
- if (idx < 0 || idx >= length) {\r
- Py_RETURN_NONE;\r
- }\r
- }\r
-#endif /* __APPLE__ */\r
- if ((hist_ent = history_get(idx)))\r
- return PyString_FromString(hist_ent->line);\r
- else {\r
- Py_RETURN_NONE;\r
- }\r
-}\r
-\r
-PyDoc_STRVAR(doc_get_history_item,\r
-"get_history_item() -> string\n\\r
-return the current contents of history item at index.");\r
-\r
-\r
-/* Exported function to get current length of history */\r
-\r
-static PyObject *\r
-get_current_history_length(PyObject *self, PyObject *noarg)\r
-{\r
- return PyInt_FromLong((long)_py_get_history_length());\r
-}\r
-\r
-PyDoc_STRVAR(doc_get_current_history_length,\r
-"get_current_history_length() -> integer\n\\r
-return the current (not the maximum) length of history.");\r
-\r
-\r
-/* Exported function to read the current line buffer */\r
-\r
-static PyObject *\r
-get_line_buffer(PyObject *self, PyObject *noarg)\r
-{\r
- return PyString_FromString(rl_line_buffer);\r
-}\r
-\r
-PyDoc_STRVAR(doc_get_line_buffer,\r
-"get_line_buffer() -> string\n\\r
-return the current contents of the line buffer.");\r
-\r
-\r
-#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER\r
-\r
-/* Exported function to clear the current history */\r
-\r
-static PyObject *\r
-py_clear_history(PyObject *self, PyObject *noarg)\r
-{\r
- clear_history();\r
- Py_RETURN_NONE;\r
-}\r
-\r
-PyDoc_STRVAR(doc_clear_history,\r
-"clear_history() -> None\n\\r
-Clear the current readline history.");\r
-#endif\r
-\r
-\r
-/* Exported function to insert text into the line buffer */\r
-\r
-static PyObject *\r
-insert_text(PyObject *self, PyObject *args)\r
-{\r
- char *s;\r
- if (!PyArg_ParseTuple(args, "s:insert_text", &s))\r
- return NULL;\r
- rl_insert_text(s);\r
- Py_RETURN_NONE;\r
-}\r
-\r
-PyDoc_STRVAR(doc_insert_text,\r
-"insert_text(string) -> None\n\\r
-Insert text into the command line.");\r
-\r
-\r
-/* Redisplay the line buffer */\r
-\r
-static PyObject *\r
-redisplay(PyObject *self, PyObject *noarg)\r
-{\r
- rl_redisplay();\r
- Py_RETURN_NONE;\r
-}\r
-\r
-PyDoc_STRVAR(doc_redisplay,\r
-"redisplay() -> None\n\\r
-Change what's displayed on the screen to reflect the current\n\\r
-contents of the line buffer.");\r
-\r
-\r
-/* Table of functions exported by the module */\r
-\r
-static struct PyMethodDef readline_methods[] =\r
-{\r
- {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},\r
- {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},\r
- {"insert_text", insert_text, METH_VARARGS, doc_insert_text},\r
- {"redisplay", redisplay, METH_NOARGS, doc_redisplay},\r
- {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},\r
- {"read_history_file", read_history_file,\r
- METH_VARARGS, doc_read_history_file},\r
- {"write_history_file", write_history_file,\r
- METH_VARARGS, doc_write_history_file},\r
- {"get_history_item", get_history_item,\r
- METH_VARARGS, doc_get_history_item},\r
- {"get_current_history_length", (PyCFunction)get_current_history_length,\r
- METH_NOARGS, doc_get_current_history_length},\r
- {"set_history_length", set_history_length,\r
- METH_VARARGS, set_history_length_doc},\r
- {"get_history_length", get_history_length,\r
- METH_NOARGS, get_history_length_doc},\r
- {"set_completer", set_completer, METH_VARARGS, doc_set_completer},\r
- {"get_completer", get_completer, METH_NOARGS, doc_get_completer},\r
- {"get_completion_type", get_completion_type,\r
- METH_NOARGS, doc_get_completion_type},\r
- {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},\r
- {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},\r
-\r
- {"set_completer_delims", set_completer_delims,\r
- METH_VARARGS, doc_set_completer_delims},\r
- {"add_history", py_add_history, METH_VARARGS, doc_add_history},\r
- {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},\r
- {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},\r
- {"get_completer_delims", get_completer_delims,\r
- METH_NOARGS, doc_get_completer_delims},\r
-\r
- {"set_completion_display_matches_hook", set_completion_display_matches_hook,\r
- METH_VARARGS, doc_set_completion_display_matches_hook},\r
- {"set_startup_hook", set_startup_hook,\r
- METH_VARARGS, doc_set_startup_hook},\r
-#ifdef HAVE_RL_PRE_INPUT_HOOK\r
- {"set_pre_input_hook", set_pre_input_hook,\r
- METH_VARARGS, doc_set_pre_input_hook},\r
-#endif\r
-#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER\r
- {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},\r
-#endif\r
- {0, 0}\r
-};\r
-\r
-\r
-/* C function to call the Python hooks. */\r
-\r
-static int\r
-on_hook(PyObject *func)\r
-{\r
- int result = 0;\r
- if (func != NULL) {\r
- PyObject *r;\r
-#ifdef WITH_THREAD\r
- PyGILState_STATE gilstate = PyGILState_Ensure();\r
-#endif\r
- r = PyObject_CallFunction(func, NULL);\r
- if (r == NULL)\r
- goto error;\r
- if (r == Py_None)\r
- result = 0;\r
- else {\r
- result = PyInt_AsLong(r);\r
- if (result == -1 && PyErr_Occurred())\r
- goto error;\r
- }\r
- Py_DECREF(r);\r
- goto done;\r
- error:\r
- PyErr_Clear();\r
- Py_XDECREF(r);\r
- done:\r
-#ifdef WITH_THREAD\r
- PyGILState_Release(gilstate);\r
-#endif\r
- return result;\r
- }\r
- return result;\r
-}\r
-\r
-static int\r
-on_startup_hook(void)\r
-{\r
- return on_hook(startup_hook);\r
-}\r
-\r
-#ifdef HAVE_RL_PRE_INPUT_HOOK\r
-static int\r
-on_pre_input_hook(void)\r
-{\r
- return on_hook(pre_input_hook);\r
-}\r
-#endif\r
-\r
-\r
-/* C function to call the Python completion_display_matches */\r
-\r
-static void\r
-on_completion_display_matches_hook(char **matches,\r
- int num_matches, int max_length)\r
-{\r
- int i;\r
- PyObject *m=NULL, *s=NULL, *r=NULL;\r
-#ifdef WITH_THREAD\r
- PyGILState_STATE gilstate = PyGILState_Ensure();\r
-#endif\r
- m = PyList_New(num_matches);\r
- if (m == NULL)\r
- goto error;\r
- for (i = 0; i < num_matches; i++) {\r
- s = PyString_FromString(matches[i+1]);\r
- if (s == NULL)\r
- goto error;\r
- if (PyList_SetItem(m, i, s) == -1)\r
- goto error;\r
- }\r
-\r
- r = PyObject_CallFunction(completion_display_matches_hook,\r
- "sOi", matches[0], m, max_length);\r
-\r
- Py_DECREF(m); m=NULL;\r
-\r
- if (r == NULL ||\r
- (r != Py_None && PyInt_AsLong(r) == -1 && PyErr_Occurred())) {\r
- goto error;\r
- }\r
- Py_XDECREF(r); r=NULL;\r
-\r
- if (0) {\r
- error:\r
- PyErr_Clear();\r
- Py_XDECREF(m);\r
- Py_XDECREF(r);\r
- }\r
-#ifdef WITH_THREAD\r
- PyGILState_Release(gilstate);\r
-#endif\r
-}\r
-\r
-\r
-/* C function to call the Python completer. */\r
-\r
-static char *\r
-on_completion(const char *text, int state)\r
-{\r
- char *result = NULL;\r
- if (completer != NULL) {\r
- PyObject *r;\r
-#ifdef WITH_THREAD\r
- PyGILState_STATE gilstate = PyGILState_Ensure();\r
-#endif\r
- rl_attempted_completion_over = 1;\r
- r = PyObject_CallFunction(completer, "si", text, state);\r
- if (r == NULL)\r
- goto error;\r
- if (r == Py_None) {\r
- result = NULL;\r
- }\r
- else {\r
- char *s = PyString_AsString(r);\r
- if (s == NULL)\r
- goto error;\r
- result = strdup(s);\r
- }\r
- Py_DECREF(r);\r
- goto done;\r
- error:\r
- PyErr_Clear();\r
- Py_XDECREF(r);\r
- done:\r
-#ifdef WITH_THREAD\r
- PyGILState_Release(gilstate);\r
-#endif\r
- return result;\r
- }\r
- return result;\r
-}\r
-\r
-\r
-/* A more flexible constructor that saves the "begidx" and "endidx"\r
- * before calling the normal completer */\r
-\r
-static char **\r
-flex_complete(char *text, int start, int end)\r
-{\r
-#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER\r
- rl_completion_append_character ='\0';\r
-#endif\r
-#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND\r
- rl_completion_suppress_append = 0;\r
-#endif\r
- Py_XDECREF(begidx);\r
- Py_XDECREF(endidx);\r
- begidx = PyInt_FromLong((long) start);\r
- endidx = PyInt_FromLong((long) end);\r
- return completion_matches(text, *on_completion);\r
-}\r
-\r
-\r
-/* Helper to initialize GNU readline properly. */\r
-\r
-static void\r
-setup_readline(void)\r
-{\r
-#ifdef SAVE_LOCALE\r
- char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));\r
- if (!saved_locale)\r
- Py_FatalError("not enough memory to save locale");\r
-#endif\r
-\r
-#ifdef __APPLE__\r
- /* the libedit readline emulation resets key bindings etc \r
- * when calling rl_initialize. So call it upfront\r
- */\r
- if (using_libedit_emulation)\r
- rl_initialize();\r
-#endif /* __APPLE__ */\r
-\r
- using_history();\r
-\r
- rl_readline_name = "python";\r
-#if defined(PYOS_OS2) && defined(PYCC_GCC)\r
- /* Allow $if term= in .inputrc to work */\r
- rl_terminal_name = getenv("TERM");\r
-#endif\r
- /* Force rebind of TAB to insert-tab */\r
- rl_bind_key('\t', rl_insert);\r
- /* Bind both ESC-TAB and ESC-ESC to the completion function */\r
- rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);\r
- rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);\r
- /* Set our hook functions */\r
- rl_startup_hook = (Function *)on_startup_hook;\r
-#ifdef HAVE_RL_PRE_INPUT_HOOK\r
- rl_pre_input_hook = (Function *)on_pre_input_hook;\r
-#endif\r
- /* Set our completion function */\r
- rl_attempted_completion_function = (CPPFunction *)flex_complete;\r
- /* Set Python word break characters */\r
- rl_completer_word_break_characters =\r
- strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");\r
- /* All nonalphanums except '.' */\r
-\r
- begidx = PyInt_FromLong(0L);\r
- endidx = PyInt_FromLong(0L);\r
- /* Initialize (allows .inputrc to override)\r
- *\r
- * XXX: A bug in the readline-2.2 library causes a memory leak\r
- * inside this function. Nothing we can do about it.\r
- */\r
-#ifdef __APPLE__\r
- if (using_libedit_emulation)\r
- rl_read_init_file(NULL);\r
- else\r
-#endif /* __APPLE__ */\r
- rl_initialize();\r
- \r
- RESTORE_LOCALE(saved_locale)\r
-}\r
-\r
-/* Wrapper around GNU readline that handles signals differently. */\r
-\r
-\r
-#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)\r
-\r
-static char *completed_input_string;\r
-static void\r
-rlhandler(char *text)\r
-{\r
- completed_input_string = text;\r
- rl_callback_handler_remove();\r
-}\r
-\r
-extern PyThreadState* _PyOS_ReadlineTState;\r
-\r
-static char *\r
-readline_until_enter_or_signal(char *prompt, int *signal)\r
-{\r
- char * not_done_reading = "";\r
- fd_set selectset;\r
-\r
- *signal = 0;\r
-#ifdef HAVE_RL_CATCH_SIGNAL\r
- rl_catch_signals = 0;\r
-#endif\r
-\r
- rl_callback_handler_install (prompt, rlhandler);\r
- FD_ZERO(&selectset);\r
-\r
- completed_input_string = not_done_reading;\r
-\r
- while (completed_input_string == not_done_reading) {\r
- int has_input = 0;\r
-\r
- while (!has_input)\r
- { struct timeval timeout = {0, 100000}; /* 0.1 seconds */\r
-\r
- /* [Bug #1552726] Only limit the pause if an input hook has been\r
- defined. */\r
- struct timeval *timeoutp = NULL;\r
- if (PyOS_InputHook)\r
- timeoutp = &timeout;\r
- FD_SET(fileno(rl_instream), &selectset);\r
- /* select resets selectset if no input was available */\r
- has_input = select(fileno(rl_instream) + 1, &selectset,\r
- NULL, NULL, timeoutp);\r
- if(PyOS_InputHook) PyOS_InputHook();\r
- }\r
-\r
- if(has_input > 0) {\r
- rl_callback_read_char();\r
- }\r
- else if (errno == EINTR) {\r
- int s;\r
-#ifdef WITH_THREAD\r
- PyEval_RestoreThread(_PyOS_ReadlineTState);\r
-#endif\r
- s = PyErr_CheckSignals();\r
-#ifdef WITH_THREAD\r
- PyEval_SaveThread();\r
-#endif\r
- if (s < 0) {\r
- rl_free_line_state();\r
- rl_cleanup_after_signal();\r
- rl_callback_handler_remove();\r
- *signal = 1;\r
- completed_input_string = NULL;\r
- }\r
- }\r
- }\r
-\r
- return completed_input_string;\r
-}\r
-\r
-\r
-#else\r
-\r
-/* Interrupt handler */\r
-\r
-static jmp_buf jbuf;\r
-\r
-/* ARGSUSED */\r
-static void\r
-onintr(int sig)\r
-{\r
- longjmp(jbuf, 1);\r
-}\r
-\r
-\r
-static char *\r
-readline_until_enter_or_signal(char *prompt, int *signal)\r
-{\r
- PyOS_sighandler_t old_inthandler;\r
- char *p;\r
-\r
- *signal = 0;\r
-\r
- old_inthandler = PyOS_setsig(SIGINT, onintr);\r
- if (setjmp(jbuf)) {\r
-#ifdef HAVE_SIGRELSE\r
- /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */\r
- sigrelse(SIGINT);\r
-#endif\r
- PyOS_setsig(SIGINT, old_inthandler);\r
- *signal = 1;\r
- return NULL;\r
- }\r
- rl_event_hook = PyOS_InputHook;\r
- p = readline(prompt);\r
- PyOS_setsig(SIGINT, old_inthandler);\r
-\r
- return p;\r
-}\r
-#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */\r
-\r
-\r
-static char *\r
-call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)\r
-{\r
- size_t n;\r
- char *p, *q;\r
- int signal;\r
-\r
-#ifdef SAVE_LOCALE\r
- char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));\r
- if (!saved_locale)\r
- Py_FatalError("not enough memory to save locale");\r
- setlocale(LC_CTYPE, "");\r
-#endif\r
-\r
- if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {\r
- rl_instream = sys_stdin;\r
- rl_outstream = sys_stdout;\r
-#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER\r
- rl_prep_terminal (1);\r
-#endif\r
- }\r
-\r
- p = readline_until_enter_or_signal(prompt, &signal);\r
-\r
- /* we got an interrupt signal */\r
- if (signal) {\r
- RESTORE_LOCALE(saved_locale)\r
- return NULL;\r
- }\r
-\r
- /* We got an EOF, return a empty string. */\r
- if (p == NULL) {\r
- p = PyMem_Malloc(1);\r
- if (p != NULL)\r
- *p = '\0';\r
- RESTORE_LOCALE(saved_locale)\r
- return p;\r
- }\r
-\r
- /* we have a valid line */\r
- n = strlen(p);\r
- if (n > 0) {\r
- const char *line;\r
- int length = _py_get_history_length();\r
- if (length > 0)\r
-#ifdef __APPLE__\r
- if (using_libedit_emulation) {\r
- /*\r
- * Libedit's emulation uses 0-based indexes,\r
- * the real readline uses 1-based indexes.\r
- */\r
- line = history_get(length - 1)->line;\r
- } else\r
-#endif /* __APPLE__ */\r
- line = history_get(length)->line;\r
- else\r
- line = "";\r
- if (strcmp(p, line))\r
- add_history(p);\r
- }\r
- /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and\r
- release the original. */\r
- q = p;\r
- p = PyMem_Malloc(n+2);\r
- if (p != NULL) {\r
- strncpy(p, q, n);\r
- p[n] = '\n';\r
- p[n+1] = '\0';\r
- }\r
- free(q);\r
- RESTORE_LOCALE(saved_locale)\r
- return p;\r
-}\r
-\r
-\r
-/* Initialize the module */\r
-\r
-PyDoc_STRVAR(doc_module,\r
-"Importing this module enables command line editing using GNU readline.");\r
-\r
-#ifdef __APPLE__\r
-PyDoc_STRVAR(doc_module_le,\r
-"Importing this module enables command line editing using libedit readline.");\r
-#endif /* __APPLE__ */\r
-\r
-PyMODINIT_FUNC\r
-initreadline(void)\r
-{\r
- PyObject *m;\r
-\r
-#ifdef __APPLE__\r
- if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {\r
- using_libedit_emulation = 1;\r
- }\r
-\r
- if (using_libedit_emulation)\r
- m = Py_InitModule4("readline", readline_methods, doc_module_le,\r
- (PyObject *)NULL, PYTHON_API_VERSION);\r
- else\r
-\r
-#endif /* __APPLE__ */\r
-\r
- m = Py_InitModule4("readline", readline_methods, doc_module,\r
- (PyObject *)NULL, PYTHON_API_VERSION);\r
- if (m == NULL)\r
- return;\r
-\r
-\r
-\r
- PyOS_ReadlineFunctionPointer = call_readline;\r
- setup_readline();\r
-}\r