/* 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. */ /* Standard definitions */#include "Python.h"#include <setjmp.h>#include <signal.h>#include <errno.h>#include <sys/time.h> #if defined(HAVE_SETLOCALE)/* GNU readline() mistakenly sets the LC_CTYPE locale. * This is evil. Only the user or the app's main() should do this! * We must save and restore the locale around the rl_initialize() call. */#define SAVE_LOCALE#include <locale.h>#endif #ifdef SAVE_LOCALE# define RESTORE_LOCALE(sl) { setlocale(LC_CTYPE, sl); free(sl); }#else# define RESTORE_LOCALE(sl) #endif /* GNU readline definitions */#undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */#include <readline/readline.h>#include <readline/history.h> #ifdef HAVE_RL_COMPLETION_MATCHES#define completion_matches(x, y) \ rl_completion_matches((x), ((rl_compentry_func_t *)(y)))#endif /* Exported function to send one line to readline's init file parser */ static PyObject *parse_and_bind(PyObject *self, PyObject *args){ char *s, *copy; if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s)) return NULL; /* 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 */ Py_INCREF(Py_None); return Py_None;} PyDoc_STRVAR(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 *read_init_file(PyObject *self, PyObject *args){ char *s = NULL; if (!PyArg_ParseTuple(args, "|z:read_init_file", &s)) return NULL; errno = rl_read_init_file(s); if (errno) return PyErr_SetFromErrno(PyExc_IOError); Py_INCREF(Py_None); return Py_None;} PyDoc_STRVAR(doc_read_init_file,"read_init_file([filename]) -> None\n\Parse a readline initialization file.\n\The default filename is the last filename used."); /* Exported function to load a readline history file */ static PyObject *read_history_file(PyObject *self, PyObject *args){ 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;} static int _history_length = -1; /* do not truncate history by default */PyDoc_STRVAR(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 *write_history_file(PyObject *self, PyObject *args){ char *s = NULL; if (!PyArg_ParseTuple(args, "|z:write_history_file", &s)) return NULL; errno = write_history(s); if (!errno && _history_length >= 0) history_truncate_file(s, _history_length); if (errno) return PyErr_SetFromErrno(PyExc_IOError); Py_INCREF(Py_None); return Py_None;} PyDoc_STRVAR(doc_write_history_file,"write_history_file([filename]) -> None\n\Save a readline history file.\n\The default filename is ~/.history."); /* Set history length */ 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;} PyDoc_STRVAR(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."); /* Get history length */ static PyObject*get_history_length(PyObject *self, PyObject *noarg){ return PyInt_FromLong(_history_length);} PyDoc_STRVAR(get_history_length_doc,"get_history_length() -> int\n\return the maximum number of items that will be written to\n\the history file."); /* Generic hook function setter */ static PyObject *set_hook(const char *funcname, PyObject **hook_var, PyObject *args){ PyObject *function = Py_None; char buf[80]; PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname); if (!PyArg_ParseTuple(args, buf, &function)) return NULL; if (function == Py_None) { Py_XDECREF(*hook_var); *hook_var = NULL; } else if (PyCallable_Check(function)) { PyObject *tmp = *hook_var; Py_INCREF(function); *hook_var = function; Py_XDECREF(tmp); } else { PyOS_snprintf(buf, sizeof(buf), "set_%.50s(func): argument not callable", funcname); 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; #ifdef HAVE_RL_PRE_INPUT_HOOKstatic PyObject *pre_input_hook = NULL;#endif static PyObject *set_startup_hook(PyObject *self, PyObject *args){ return set_hook("startup_hook", &startup_hook, args);} PyDoc_STRVAR(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."); #ifdef HAVE_RL_PRE_INPUT_HOOK /* Set pre-input hook */ static PyObject *set_pre_input_hook(PyObject *self, PyObject *args){ return set_hook("pre_input_hook", &pre_input_hook, args);} PyDoc_STRVAR(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."); #endif /* Exported function to specify a word completer in Python */ static PyObject *completer = NULL; static PyObject *begidx = NULL;static PyObject *endidx = NULL; /* Get the beginning index for the scope of the tab-completion */ static PyObject *get_begidx(PyObject *self, PyObject *noarg){ Py_INCREF(begidx); return begidx;} PyDoc_STRVAR(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 *get_endidx(PyObject *self, PyObject *noarg){ Py_INCREF(endidx); return endidx;} PyDoc_STRVAR(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 *set_completer_delims(PyObject *self, PyObject *args){ char *break_chars; if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) { return NULL; } free((void*)rl_completer_word_break_characters); rl_completer_word_break_characters = strdup(break_chars); Py_INCREF(Py_None); return Py_None;} PyDoc_STRVAR(doc_set_completer_delims,"set_completer_delims(string) -> None\n\set the readline word delimiters for tab-completion"); static PyObject *py_remove_history(PyObject *self, PyObject *args){ int entry_number; HIST_ENTRY *entry; if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number)) return NULL; if (entry_number < 0) { PyErr_SetString(PyExc_ValueError, "History index cannot be negative"); return NULL; } entry = remove_history(entry_number); if (!entry) { PyErr_Format(PyExc_ValueError, "No history item at position %d", entry_number); return NULL; } /* free memory allocated for the history entry */ if (entry->line) free(entry->line); if (entry->data) free(entry->data); free(entry); Py_INCREF(Py_None); return Py_None;} PyDoc_STRVAR(doc_remove_history,"remove_history_item(pos) -> None\n\remove history item given by its position"); static PyObject *py_replace_history(PyObject *self, PyObject *args){ int entry_number; char *line; HIST_ENTRY *old_entry; if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number, &line)) { return NULL; } if (entry_number < 0) { PyErr_SetString(PyExc_ValueError, "History index cannot be negative"); return NULL; } old_entry = replace_history_entry(entry_number, line, (void *)NULL); if (!old_entry) { PyErr_Format(PyExc_ValueError, "No history item at position %d", entry_number); return NULL; } /* free memory allocated for the old history entry */ if (old_entry->line) free(old_entry->line); if (old_entry->data) free(old_entry->data); free(old_entry); Py_INCREF(Py_None); return Py_None;} PyDoc_STRVAR(doc_replace_history,"replace_history_item(pos, line) -> None\n\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){ char *line; if(!PyArg_ParseTuple(args, "s:add_history", &line)) { return NULL; } add_history(line); Py_INCREF(Py_None); return Py_None;} PyDoc_STRVAR(doc_add_history,"add_history(string) -> None\n\add a line to the history buffer"); /* Get the tab-completion word-delimiters that readline uses */ static PyObject *get_completer_delims(PyObject *self, PyObject *noarg){ return PyString_FromString(rl_completer_word_break_characters);} PyDoc_STRVAR(doc_get_completer_delims,"get_completer_delims() -> string\n\get the readline word delimiters for tab-completion"); /* Set the completer function */ static PyObject *set_completer(PyObject *self, PyObject *args){ return set_hook("completer", &completer, args);} PyDoc_STRVAR(doc_set_completer,"set_completer([function]) -> None\n\Set or remove the completer function.\n\The function is called as function(text, state),\n\for state in 0, 1, 2, ..., until it returns a non-string.\n\It should return the next possible completion starting with 'text'."); static PyObject *get_completer(PyObject *self, PyObject *noargs){ if (completer == NULL) { Py_INCREF(Py_None); return Py_None; } Py_INCREF(completer); return completer;} PyDoc_STRVAR(doc_get_completer,"get_completer() -> function\n\\n\Returns current completer function."); /* Exported function to get any element of history */ static PyObject *get_history_item(PyObject *self, PyObject *args){ int idx = 0; HIST_ENTRY *hist_ent; if (!PyArg_ParseTuple(args, "i:index", &idx)) return NULL; if ((hist_ent = history_get(idx))) return PyString_FromString(hist_ent->line); else { Py_INCREF(Py_None); return Py_None; }} PyDoc_STRVAR(doc_get_history_item,"get_history_item() -> string\n\return the current contents of history item at index."); /* Exported function to get current length of history */ static PyObject *get_current_history_length(PyObject *self, PyObject *noarg){ HISTORY_STATE *hist_st; hist_st = history_get_history_state(); return PyInt_FromLong(hist_st ? (long) hist_st->length : (long) 0);} PyDoc_STRVAR(doc_get_current_history_length,"get_current_history_length() -> integer\n\return the current (not the maximum) length of history."); /* Exported function to read the current line buffer */ static PyObject *get_line_buffer(PyObject *self, PyObject *noarg){ return PyString_FromString(rl_line_buffer);} PyDoc_STRVAR(doc_get_line_buffer,"get_line_buffer() -> string\n\return the current contents of the line buffer."); #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER /* Exported function to clear the current history */ static PyObject *py_clear_history(PyObject *self, PyObject *noarg){ clear_history(); Py_INCREF(Py_None); return Py_None;} PyDoc_STRVAR(doc_clear_history,"clear_history() -> None\n\Clear the current readline history.");#endif Lines 1–500 of 941. Load more
/* 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. */ /* Standard definitions */ #include "Python.h" #include <setjmp.h> #include <signal.h> #include <errno.h> #include <sys/time.h> #if defined(HAVE_SETLOCALE) /* GNU readline() mistakenly sets the LC_CTYPE locale. * This is evil. Only the user or the app's main() should do this! * We must save and restore the locale around the rl_initialize() call. */ #define SAVE_LOCALE #include <locale.h> #endif #ifdef SAVE_LOCALE # define RESTORE_LOCALE(sl) { setlocale(LC_CTYPE, sl); free(sl); } #else # define RESTORE_LOCALE(sl) #endif /* GNU readline definitions */ #undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */ #include <readline/readline.h> #include <readline/history.h> #ifdef HAVE_RL_COMPLETION_MATCHES #define completion_matches(x, y) \ rl_completion_matches((x), ((rl_compentry_func_t *)(y))) #endif /* Exported function to send one line to readline's init file parser */ static PyObject * parse_and_bind(PyObject *self, PyObject *args) { char *s, *copy; if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s)) return NULL; /* 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 */ Py_INCREF(Py_None); return Py_None; } PyDoc_STRVAR(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 * read_init_file(PyObject *self, PyObject *args) { char *s = NULL; if (!PyArg_ParseTuple(args, "|z:read_init_file", &s)) return NULL; errno = rl_read_init_file(s); if (errno) return PyErr_SetFromErrno(PyExc_IOError); Py_INCREF(Py_None); return Py_None; } PyDoc_STRVAR(doc_read_init_file, "read_init_file([filename]) -> None\n\ Parse a readline initialization file.\n\ The default filename is the last filename used."); /* Exported function to load a readline history file */ static PyObject * read_history_file(PyObject *self, PyObject *args) { 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; } static int _history_length = -1; /* do not truncate history by default */ PyDoc_STRVAR(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 * write_history_file(PyObject *self, PyObject *args) { char *s = NULL; if (!PyArg_ParseTuple(args, "|z:write_history_file", &s)) return NULL; errno = write_history(s); if (!errno && _history_length >= 0) history_truncate_file(s, _history_length); if (errno) return PyErr_SetFromErrno(PyExc_IOError); Py_INCREF(Py_None); return Py_None; } PyDoc_STRVAR(doc_write_history_file, "write_history_file([filename]) -> None\n\ Save a readline history file.\n\ The default filename is ~/.history."); /* Set history length */ 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; } PyDoc_STRVAR(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."); /* Get history length */ static PyObject* get_history_length(PyObject *self, PyObject *noarg) { return PyInt_FromLong(_history_length); } PyDoc_STRVAR(get_history_length_doc, "get_history_length() -> int\n\ return the maximum number of items that will be written to\n\ the history file."); /* Generic hook function setter */ static PyObject * set_hook(const char *funcname, PyObject **hook_var, PyObject *args) { PyObject *function = Py_None; char buf[80]; PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname); if (!PyArg_ParseTuple(args, buf, &function)) return NULL; if (function == Py_None) { Py_XDECREF(*hook_var); *hook_var = NULL; } else if (PyCallable_Check(function)) { PyObject *tmp = *hook_var; Py_INCREF(function); *hook_var = function; Py_XDECREF(tmp); } else { PyOS_snprintf(buf, sizeof(buf), "set_%.50s(func): argument not callable", funcname); 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; #ifdef HAVE_RL_PRE_INPUT_HOOK static PyObject *pre_input_hook = NULL; #endif static PyObject * set_startup_hook(PyObject *self, PyObject *args) { return set_hook("startup_hook", &startup_hook, args); } PyDoc_STRVAR(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."); #ifdef HAVE_RL_PRE_INPUT_HOOK /* Set pre-input hook */ static PyObject * set_pre_input_hook(PyObject *self, PyObject *args) { return set_hook("pre_input_hook", &pre_input_hook, args); } PyDoc_STRVAR(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."); #endif /* Exported function to specify a word completer in Python */ static PyObject *completer = NULL; static PyObject *begidx = NULL; static PyObject *endidx = NULL; /* Get the beginning index for the scope of the tab-completion */ static PyObject * get_begidx(PyObject *self, PyObject *noarg) { Py_INCREF(begidx); return begidx; } PyDoc_STRVAR(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 * get_endidx(PyObject *self, PyObject *noarg) { Py_INCREF(endidx); return endidx; } PyDoc_STRVAR(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 * set_completer_delims(PyObject *self, PyObject *args) { char *break_chars; if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) { return NULL; } free((void*)rl_completer_word_break_characters); rl_completer_word_break_characters = strdup(break_chars); Py_INCREF(Py_None); return Py_None; } PyDoc_STRVAR(doc_set_completer_delims, "set_completer_delims(string) -> None\n\ set the readline word delimiters for tab-completion"); static PyObject * py_remove_history(PyObject *self, PyObject *args) { int entry_number; HIST_ENTRY *entry; if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number)) return NULL; if (entry_number < 0) { PyErr_SetString(PyExc_ValueError, "History index cannot be negative"); return NULL; } entry = remove_history(entry_number); if (!entry) { PyErr_Format(PyExc_ValueError, "No history item at position %d", entry_number); return NULL; } /* free memory allocated for the history entry */ if (entry->line) free(entry->line); if (entry->data) free(entry->data); free(entry); Py_INCREF(Py_None); return Py_None; } PyDoc_STRVAR(doc_remove_history, "remove_history_item(pos) -> None\n\ remove history item given by its position"); static PyObject * py_replace_history(PyObject *self, PyObject *args) { int entry_number; char *line; HIST_ENTRY *old_entry; if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number, &line)) { return NULL; } if (entry_number < 0) { PyErr_SetString(PyExc_ValueError, "History index cannot be negative"); return NULL; } old_entry = replace_history_entry(entry_number, line, (void *)NULL); if (!old_entry) { PyErr_Format(PyExc_ValueError, "No history item at position %d", entry_number); return NULL; } /* free memory allocated for the old history entry */ if (old_entry->line) free(old_entry->line); if (old_entry->data) free(old_entry->data); free(old_entry); Py_INCREF(Py_None); return Py_None; } PyDoc_STRVAR(doc_replace_history, "replace_history_item(pos, line) -> None\n\ 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) { char *line; if(!PyArg_ParseTuple(args, "s:add_history", &line)) { return NULL; } add_history(line); Py_INCREF(Py_None); return Py_None; } PyDoc_STRVAR(doc_add_history, "add_history(string) -> None\n\ add a line to the history buffer"); /* Get the tab-completion word-delimiters that readline uses */ static PyObject * get_completer_delims(PyObject *self, PyObject *noarg) { return PyString_FromString(rl_completer_word_break_characters); } PyDoc_STRVAR(doc_get_completer_delims, "get_completer_delims() -> string\n\ get the readline word delimiters for tab-completion"); /* Set the completer function */ static PyObject * set_completer(PyObject *self, PyObject *args) { return set_hook("completer", &completer, args); } PyDoc_STRVAR(doc_set_completer, "set_completer([function]) -> None\n\ Set or remove the completer function.\n\ The function is called as function(text, state),\n\ for state in 0, 1, 2, ..., until it returns a non-string.\n\ It should return the next possible completion starting with 'text'."); static PyObject * get_completer(PyObject *self, PyObject *noargs) { if (completer == NULL) { Py_INCREF(Py_None); return Py_None; } Py_INCREF(completer); return completer; } PyDoc_STRVAR(doc_get_completer, "get_completer() -> function\n\ \n\ Returns current completer function."); /* Exported function to get any element of history */ static PyObject * get_history_item(PyObject *self, PyObject *args) { int idx = 0; HIST_ENTRY *hist_ent; if (!PyArg_ParseTuple(args, "i:index", &idx)) return NULL; if ((hist_ent = history_get(idx))) return PyString_FromString(hist_ent->line); else { Py_INCREF(Py_None); return Py_None; } } PyDoc_STRVAR(doc_get_history_item, "get_history_item() -> string\n\ return the current contents of history item at index."); /* Exported function to get current length of history */ static PyObject * get_current_history_length(PyObject *self, PyObject *noarg) { HISTORY_STATE *hist_st; hist_st = history_get_history_state(); return PyInt_FromLong(hist_st ? (long) hist_st->length : (long) 0); } PyDoc_STRVAR(doc_get_current_history_length, "get_current_history_length() -> integer\n\ return the current (not the maximum) length of history."); /* Exported function to read the current line buffer */ static PyObject * get_line_buffer(PyObject *self, PyObject *noarg) { return PyString_FromString(rl_line_buffer); } PyDoc_STRVAR(doc_get_line_buffer, "get_line_buffer() -> string\n\ return the current contents of the line buffer."); #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER /* Exported function to clear the current history */ static PyObject * py_clear_history(PyObject *self, PyObject *noarg) { clear_history(); Py_INCREF(Py_None); return Py_None; } PyDoc_STRVAR(doc_clear_history, "clear_history() -> None\n\ Clear the current readline history."); #endif