| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | /* This module makes GNU readline available to Python.  It has ideas
 | 
					
						
							|  |  |  |  * contributed by Lee Busby, LLNL, and William Magro, Cornell Theory | 
					
						
							| 
									
										
										
										
											2005-03-30 10:09:12 +00:00
										 |  |  |  * Center.  The completer interface was inspired by Lele Gaifax.  More | 
					
						
							|  |  |  |  * recently, it was largely rewritten by Guido van Rossum. | 
					
						
							| 
									
										
										
										
											1997-08-05 21:27:50 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | /* Standard definitions */ | 
					
						
							| 
									
										
										
										
											1997-08-05 21:27:50 +00:00
										 |  |  | #include "Python.h"
 | 
					
						
							|  |  |  | #include <setjmp.h>
 | 
					
						
							|  |  |  | #include <signal.h>
 | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | #include <errno.h>
 | 
					
						
							| 
									
										
										
										
											2004-10-07 13:46:33 +00:00
										 |  |  | #include <sys/time.h>
 | 
					
						
							| 
									
										
										
										
											1997-08-05 21:27:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-02-10 16:50:21 +00:00
										 |  |  | #if defined(HAVE_SETLOCALE)
 | 
					
						
							| 
									
										
										
										
											2002-10-09 21:27:33 +00:00
										 |  |  | /* 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
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | /* GNU readline definitions */ | 
					
						
							| 
									
										
										
										
											2001-04-13 18:14:27 +00:00
										 |  |  | #undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
 | 
					
						
							| 
									
										
										
										
											1998-08-04 22:53:56 +00:00
										 |  |  | #include <readline/readline.h>
 | 
					
						
							|  |  |  | #include <readline/history.h>
 | 
					
						
							| 
									
										
										
										
											1998-04-10 22:27:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-07-10 16:45:32 +00:00
										 |  |  | #ifdef HAVE_RL_COMPLETION_MATCHES
 | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | #define completion_matches(x, y) \
 | 
					
						
							|  |  |  | 	rl_completion_matches((x), ((rl_compentry_func_t *)(y))) | 
					
						
							| 
									
										
										
										
											2001-07-10 16:45:32 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-05 21:27:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | /* Exported function to send one line to readline's init file parser */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-10 09:57:19 +00:00
										 |  |  | parse_and_bind(PyObject *self, PyObject *args) | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1998-12-04 15:34:39 +00:00
										 |  |  | 	char *s, *copy; | 
					
						
							| 
									
										
										
										
											2000-02-29 13:59:29 +00:00
										 |  |  | 	if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s)) | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											1998-12-04 15:34:39 +00: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 21:51:21 +00:00
										 |  |  | 	Py_INCREF(Py_None); | 
					
						
							|  |  |  | 	return Py_None; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(doc_parse_and_bind, | 
					
						
							|  |  |  | "parse_and_bind(string) -> None\n\
 | 
					
						
							|  |  |  | Parse and execute single line of a readline init file."); | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Exported function to parse a readline init file */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-10 09:57:19 +00:00
										 |  |  | read_init_file(PyObject *self, PyObject *args) | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	char *s = NULL; | 
					
						
							| 
									
										
										
										
											2000-02-29 13:59:29 +00:00
										 |  |  | 	if (!PyArg_ParseTuple(args, "|z:read_init_file", &s)) | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	errno = rl_read_init_file(s); | 
					
						
							|  |  |  | 	if (errno) | 
					
						
							|  |  |  | 		return PyErr_SetFromErrno(PyExc_IOError); | 
					
						
							|  |  |  | 	Py_INCREF(Py_None); | 
					
						
							|  |  |  | 	return Py_None; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(doc_read_init_file, | 
					
						
							|  |  |  | "read_init_file([filename]) -> None\n\
 | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | Parse a readline initialization file.\n\ | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | The default filename is the last filename used."); | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-06 18:55:12 +00:00
										 |  |  | /* Exported function to load a readline history file */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-10 09:57:19 +00:00
										 |  |  | read_history_file(PyObject *self, PyObject *args) | 
					
						
							| 
									
										
										
										
											2000-07-06 18:55:12 +00: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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-25 04:04:20 +00:00
										 |  |  | static int _history_length = -1; /* do not truncate history by default */ | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(doc_read_history_file, | 
					
						
							|  |  |  | "read_history_file([filename]) -> None\n\
 | 
					
						
							| 
									
										
										
										
											2000-07-06 18:55:12 +00:00
										 |  |  | Load a readline history file.\n\ | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | The default filename is ~/.history."); | 
					
						
							| 
									
										
										
										
											2000-07-06 18:55:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Exported function to save a readline history file */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-10 09:57:19 +00:00
										 |  |  | write_history_file(PyObject *self, PyObject *args) | 
					
						
							| 
									
										
										
										
											2000-07-06 18:55:12 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	char *s = NULL; | 
					
						
							|  |  |  | 	if (!PyArg_ParseTuple(args, "|z:write_history_file", &s)) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	errno = write_history(s); | 
					
						
							| 
									
										
										
										
											2004-11-25 04:04:20 +00:00
										 |  |  | 	if (!errno && _history_length >= 0) | 
					
						
							|  |  |  | 		history_truncate_file(s, _history_length); | 
					
						
							| 
									
										
										
										
											2000-07-06 18:55:12 +00:00
										 |  |  | 	if (errno) | 
					
						
							|  |  |  | 		return PyErr_SetFromErrno(PyExc_IOError); | 
					
						
							|  |  |  | 	Py_INCREF(Py_None); | 
					
						
							|  |  |  | 	return Py_None; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(doc_write_history_file, | 
					
						
							|  |  |  | "write_history_file([filename]) -> None\n\
 | 
					
						
							| 
									
										
										
										
											2000-07-06 18:55:12 +00:00
										 |  |  | Save a readline history file.\n\ | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | The default filename is ~/.history."); | 
					
						
							| 
									
										
										
										
											2000-07-06 18:55:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | /* Set history length */ | 
					
						
							| 
									
										
										
										
											2000-07-19 16:54:53 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static PyObject* | 
					
						
							|  |  |  | set_history_length(PyObject *self, PyObject *args) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2004-11-25 04:04:20 +00:00
										 |  |  | 	int length = _history_length; | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | 	if (!PyArg_ParseTuple(args, "i:set_history_length", &length)) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2004-11-25 04:04:20 +00:00
										 |  |  | 	_history_length = length; | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | 	Py_INCREF(Py_None); | 
					
						
							|  |  |  | 	return Py_None; | 
					
						
							| 
									
										
										
										
											2000-07-19 16:54:53 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | 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."); | 
					
						
							| 
									
										
										
										
											2000-07-19 16:54:53 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | /* Get history length */ | 
					
						
							| 
									
										
										
										
											2000-07-19 16:54:53 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static PyObject* | 
					
						
							| 
									
										
										
										
											2003-01-30 14:17:16 +00:00
										 |  |  | get_history_length(PyObject *self, PyObject *noarg) | 
					
						
							| 
									
										
										
										
											2000-07-19 16:54:53 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-11-25 04:04:20 +00:00
										 |  |  | 	return PyInt_FromLong(_history_length); | 
					
						
							| 
									
										
										
										
											2000-07-19 16:54:53 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | 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."); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-09-30 21:09:59 +00:00
										 |  |  | /* Generic hook function setter */ | 
					
						
							| 
									
										
										
										
											2000-07-19 16:54:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-09-30 21:09:59 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2005-03-30 11:21:53 +00:00
										 |  |  | set_hook(const char *funcname, PyObject **hook_var, PyObject *args) | 
					
						
							| 
									
										
										
										
											2001-09-30 21:09:59 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	PyObject *function = Py_None; | 
					
						
							|  |  |  | 	char buf[80]; | 
					
						
							| 
									
										
										
										
											2001-11-28 20:27:42 +00:00
										 |  |  | 	PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname); | 
					
						
							| 
									
										
										
										
											2001-09-30 21:09:59 +00:00
										 |  |  | 	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 { | 
					
						
							| 
									
										
										
										
											2001-11-28 20:27:42 +00:00
										 |  |  | 		PyOS_snprintf(buf, sizeof(buf), | 
					
						
							|  |  |  | 			      "set_%.50s(func): argument not callable", | 
					
						
							|  |  |  | 			      funcname); | 
					
						
							| 
									
										
										
										
											2001-09-30 21:09:59 +00:00
										 |  |  | 		PyErr_SetString(PyExc_TypeError, buf); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	Py_INCREF(Py_None); | 
					
						
							|  |  |  | 	return Py_None; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-09-30 21:09:59 +00:00
										 |  |  | /* 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) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2005-03-30 11:21:53 +00:00
										 |  |  | 	return set_hook("startup_hook", &startup_hook, args); | 
					
						
							| 
									
										
										
										
											2001-09-30 21:09:59 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(doc_set_startup_hook, | 
					
						
							|  |  |  | "set_startup_hook([function]) -> None\n\
 | 
					
						
							| 
									
										
										
										
											2001-09-30 21:09:59 +00:00
										 |  |  | Set or remove the startup_hook function.\n\ | 
					
						
							|  |  |  | The function is called with no arguments just\n\ | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | before readline prints the first prompt."); | 
					
						
							| 
									
										
										
										
											2001-09-30 21:09:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-09-30 21:09:59 +00:00
										 |  |  | #ifdef HAVE_RL_PRE_INPUT_HOOK
 | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Set pre-input hook */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-09-30 21:09:59 +00:00
										 |  |  | static PyObject * | 
					
						
							|  |  |  | set_pre_input_hook(PyObject *self, PyObject *args) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2005-03-30 11:21:53 +00:00
										 |  |  | 	return set_hook("pre_input_hook", &pre_input_hook, args); | 
					
						
							| 
									
										
										
										
											2001-09-30 21:09:59 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(doc_set_pre_input_hook, | 
					
						
							|  |  |  | "set_pre_input_hook([function]) -> None\n\
 | 
					
						
							| 
									
										
										
										
											2001-09-30 21:09:59 +00:00
										 |  |  | 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\ | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | characters."); | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-09-30 21:09:59 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2000-07-19 16:54:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | /* Exported function to specify a word completer in Python */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject *completer = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-11-18 17:51:02 +00:00
										 |  |  | static PyObject *begidx = NULL; | 
					
						
							|  |  |  | static PyObject *endidx = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Get the beginning index for the scope of the tab-completion */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-11-18 17:51:02 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2003-01-30 14:17:16 +00:00
										 |  |  | get_begidx(PyObject *self, PyObject *noarg) | 
					
						
							| 
									
										
										
										
											1999-11-18 17:51:02 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	Py_INCREF(begidx); | 
					
						
							|  |  |  | 	return begidx; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(doc_get_begidx, | 
					
						
							|  |  |  | "get_begidx() -> int\n\
 | 
					
						
							|  |  |  | get the beginning index of the readline tab-completion scope"); | 
					
						
							| 
									
										
										
										
											1999-11-18 17:51:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Get the ending index for the scope of the tab-completion */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-11-18 17:51:02 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2003-01-30 14:17:16 +00:00
										 |  |  | get_endidx(PyObject *self, PyObject *noarg) | 
					
						
							| 
									
										
										
										
											1999-11-18 17:51:02 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	Py_INCREF(endidx); | 
					
						
							|  |  |  | 	return endidx; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(doc_get_endidx, | 
					
						
							|  |  |  | "get_endidx() -> int\n\
 | 
					
						
							|  |  |  | get the ending index of the readline tab-completion scope"); | 
					
						
							| 
									
										
										
										
											1999-11-18 17:51:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | /* Set the tab-completion word-delimiters that readline uses */ | 
					
						
							| 
									
										
										
										
											1999-11-18 17:51:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-10 09:57:19 +00:00
										 |  |  | set_completer_delims(PyObject *self, PyObject *args) | 
					
						
							| 
									
										
										
										
											1999-11-18 17:51:02 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	char *break_chars; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-02-29 13:59:29 +00:00
										 |  |  | 	if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) { | 
					
						
							| 
									
										
										
										
											1999-11-18 17:51:02 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2002-04-21 15:03:18 +00:00
										 |  |  | 	free((void*)rl_completer_word_break_characters); | 
					
						
							| 
									
										
										
										
											1999-11-18 17:51:02 +00:00
										 |  |  | 	rl_completer_word_break_characters = strdup(break_chars); | 
					
						
							|  |  |  | 	Py_INCREF(Py_None); | 
					
						
							|  |  |  | 	return Py_None; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(doc_set_completer_delims, | 
					
						
							|  |  |  | "set_completer_delims(string) -> None\n\
 | 
					
						
							|  |  |  | set the readline word delimiters for tab-completion"); | 
					
						
							| 
									
										
										
										
											1999-11-18 17:51:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-15 14:32:06 +00:00
										 |  |  | 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; | 
					
						
							| 
									
										
										
										
											2005-02-27 20:33:25 +00:00
										 |  |  |         if (entry_number < 0) { | 
					
						
							|  |  |  |                 PyErr_SetString(PyExc_ValueError, | 
					
						
							|  |  |  |                                 "History index cannot be negative"); | 
					
						
							|  |  |  |                 return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2004-08-15 14:32:06 +00:00
										 |  |  |         entry = remove_history(entry_number); | 
					
						
							|  |  |  |         if (!entry) { | 
					
						
							| 
									
										
										
										
											2004-08-16 16:15:13 +00:00
										 |  |  |                 PyErr_Format(PyExc_ValueError, | 
					
						
							|  |  |  |                              "No history item at position %d", | 
					
						
							|  |  |  |                              entry_number); | 
					
						
							| 
									
										
										
										
											2004-08-15 14:32:06 +00:00
										 |  |  |                 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, | 
					
						
							| 
									
										
										
										
											2004-08-16 16:15:13 +00:00
										 |  |  | "remove_history_item(pos) -> None\n\
 | 
					
						
							| 
									
										
										
										
											2004-08-15 14:32:06 +00:00
										 |  |  | 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; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2005-02-27 20:33:25 +00:00
										 |  |  |         if (entry_number < 0) { | 
					
						
							|  |  |  |                 PyErr_SetString(PyExc_ValueError, | 
					
						
							|  |  |  |                                 "History index cannot be negative"); | 
					
						
							|  |  |  |                 return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2004-08-15 14:32:06 +00:00
										 |  |  |         old_entry = replace_history_entry(entry_number, line, (void *)NULL); | 
					
						
							|  |  |  |         if (!old_entry) { | 
					
						
							| 
									
										
										
										
											2004-08-16 16:15:13 +00:00
										 |  |  |                 PyErr_Format(PyExc_ValueError, | 
					
						
							|  |  |  |                              "No history item at position %d", | 
					
						
							|  |  |  |                              entry_number); | 
					
						
							| 
									
										
										
										
											2004-08-15 14:32:06 +00:00
										 |  |  |                 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, | 
					
						
							| 
									
										
										
										
											2004-08-16 16:15:13 +00:00
										 |  |  | "replace_history_item(pos, line) -> None\n\
 | 
					
						
							| 
									
										
										
										
											2004-08-15 14:32:06 +00:00
										 |  |  | replaces history item given by its position with contents of line"); | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Add a line to the history buffer */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-19 01:18:43 +00: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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(doc_add_history, | 
					
						
							|  |  |  | "add_history(string) -> None\n\
 | 
					
						
							|  |  |  | add a line to the history buffer"); | 
					
						
							| 
									
										
										
										
											2001-10-19 01:18:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-11-18 17:51:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | /* Get the tab-completion word-delimiters that readline uses */ | 
					
						
							| 
									
										
										
										
											1999-11-18 17:51:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2003-01-30 14:17:16 +00:00
										 |  |  | get_completer_delims(PyObject *self, PyObject *noarg) | 
					
						
							| 
									
										
										
										
											1999-11-18 17:51:02 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	return PyString_FromString(rl_completer_word_break_characters); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(doc_get_completer_delims, | 
					
						
							|  |  |  | "get_completer_delims() -> string\n\
 | 
					
						
							|  |  |  | get the readline word delimiters for tab-completion"); | 
					
						
							| 
									
										
										
										
											1999-11-18 17:51:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Set the completer function */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-10 09:57:19 +00:00
										 |  |  | set_completer(PyObject *self, PyObject *args) | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2005-03-30 11:21:53 +00:00
										 |  |  | 	return set_hook("completer", &completer, args); | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(doc_set_completer, | 
					
						
							|  |  |  | "set_completer([function]) -> None\n\
 | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | Set or remove the completer function.\n\ | 
					
						
							|  |  |  | The function is called as function(text, state),\n\ | 
					
						
							| 
									
										
										
										
											2001-08-01 21:44:14 +00:00
										 |  |  | for state in 0, 1, 2, ..., until it returns a non-string.\n\ | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | It should return the next possible completion starting with 'text'."); | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-30 10:12:51 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2003-03-01 15:19:41 +00:00
										 |  |  | get_completer(PyObject *self, PyObject *noargs) | 
					
						
							| 
									
										
										
										
											2003-01-30 10:12:51 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	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."); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-03-24 01:09:04 +00:00
										 |  |  | /* 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))) | 
					
						
							| 
									
										
										
										
											2003-01-07 20:04:12 +00:00
										 |  |  | 		return PyString_FromString(hist_ent->line); | 
					
						
							| 
									
										
										
										
											2002-03-24 01:09:04 +00:00
										 |  |  | 	else { | 
					
						
							|  |  |  | 		Py_INCREF(Py_None); | 
					
						
							|  |  |  | 		return Py_None; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(doc_get_history_item, | 
					
						
							|  |  |  | "get_history_item() -> string\n\
 | 
					
						
							|  |  |  | return the current contents of history item at index."); | 
					
						
							| 
									
										
										
										
											2002-03-24 01:09:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-03-24 01:09:04 +00:00
										 |  |  | /* Exported function to get current length of history */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2003-01-30 14:17:16 +00:00
										 |  |  | get_current_history_length(PyObject *self, PyObject *noarg) | 
					
						
							| 
									
										
										
										
											2002-03-24 01:09:04 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	HISTORY_STATE *hist_st; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	hist_st = history_get_history_state(); | 
					
						
							|  |  |  | 	return PyInt_FromLong(hist_st ? (long) hist_st->length : (long) 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(doc_get_current_history_length, | 
					
						
							|  |  |  | "get_current_history_length() -> integer\n\
 | 
					
						
							|  |  |  | return the current (not the maximum) length of history."); | 
					
						
							| 
									
										
										
										
											2002-03-24 01:09:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-10-07 14:53:21 +00:00
										 |  |  | /* Exported function to read the current line buffer */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2003-01-30 14:17:16 +00:00
										 |  |  | get_line_buffer(PyObject *self, PyObject *noarg) | 
					
						
							| 
									
										
										
										
											1997-10-07 14:53:21 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	return PyString_FromString(rl_line_buffer); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(doc_get_line_buffer, | 
					
						
							|  |  |  | "get_line_buffer() -> string\n\
 | 
					
						
							|  |  |  | return the current contents of the line buffer."); | 
					
						
							| 
									
										
										
										
											1997-10-07 14:53:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-09-20 16:08:33 +00:00
										 |  |  | #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
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-10-07 14:53:21 +00:00
										 |  |  | /* Exported function to insert text into the line buffer */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-10 09:57:19 +00:00
										 |  |  | insert_text(PyObject *self, PyObject *args) | 
					
						
							| 
									
										
										
										
											1997-10-07 14:53:21 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	char *s; | 
					
						
							| 
									
										
										
										
											2000-02-29 13:59:29 +00:00
										 |  |  | 	if (!PyArg_ParseTuple(args, "s:insert_text", &s)) | 
					
						
							| 
									
										
										
										
											1997-10-07 14:53:21 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	rl_insert_text(s); | 
					
						
							|  |  |  | 	Py_INCREF(Py_None); | 
					
						
							|  |  |  | 	return Py_None; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(doc_insert_text, | 
					
						
							|  |  |  | "insert_text(string) -> None\n\
 | 
					
						
							|  |  |  | Insert text into the command line."); | 
					
						
							| 
									
										
										
										
											1997-10-07 14:53:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Redisplay the line buffer */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-03-24 01:09:04 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2003-01-30 14:17:16 +00:00
										 |  |  | redisplay(PyObject *self, PyObject *noarg) | 
					
						
							| 
									
										
										
										
											2002-03-24 01:09:04 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	rl_redisplay(); | 
					
						
							|  |  |  | 	Py_INCREF(Py_None); | 
					
						
							|  |  |  | 	return Py_None; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(doc_redisplay, | 
					
						
							|  |  |  | "redisplay() -> None\n\
 | 
					
						
							| 
									
										
										
										
											2002-03-24 01:09:04 +00:00
										 |  |  | Change what's displayed on the screen to reflect the current\n\ | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | contents of the line buffer."); | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | /* Table of functions exported by the module */ | 
					
						
							| 
									
										
										
										
											1997-08-05 21:27:50 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static struct PyMethodDef readline_methods[] = | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2000-08-03 02:06:16 +00:00
										 |  |  | 	{"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind}, | 
					
						
							| 
									
										
										
										
											2003-01-30 14:17:16 +00:00
										 |  |  | 	{"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer}, | 
					
						
							| 
									
										
										
										
											2000-08-03 02:06:16 +00:00
										 |  |  | 	{"insert_text", insert_text, METH_VARARGS, doc_insert_text}, | 
					
						
							| 
									
										
										
										
											2003-01-30 14:17:16 +00:00
										 |  |  | 	{"redisplay", redisplay, METH_NOARGS, doc_redisplay}, | 
					
						
							| 
									
										
										
										
											2000-08-03 02:06:16 +00:00
										 |  |  | 	{"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file}, | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | 	{"read_history_file", read_history_file, | 
					
						
							| 
									
										
										
										
											2000-08-03 02:06:16 +00:00
										 |  |  | 	 METH_VARARGS, doc_read_history_file}, | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | 	{"write_history_file", write_history_file, | 
					
						
							| 
									
										
										
										
											2000-08-03 02:06:16 +00:00
										 |  |  | 	 METH_VARARGS, doc_write_history_file}, | 
					
						
							| 
									
										
										
										
											2002-03-24 01:09:04 +00:00
										 |  |  | 	{"get_history_item", get_history_item, | 
					
						
							|  |  |  | 	 METH_VARARGS, doc_get_history_item}, | 
					
						
							| 
									
										
										
										
											2002-03-31 16:13:39 +00:00
										 |  |  | 	{"get_current_history_length", (PyCFunction)get_current_history_length, | 
					
						
							| 
									
										
										
										
											2002-03-25 20:46:46 +00:00
										 |  |  | 	 METH_NOARGS, doc_get_current_history_length}, | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  |  	{"set_history_length", set_history_length, | 
					
						
							| 
									
										
										
										
											2000-08-03 02:06:16 +00:00
										 |  |  | 	 METH_VARARGS, set_history_length_doc}, | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  |  	{"get_history_length", get_history_length, | 
					
						
							| 
									
										
										
										
											2003-01-30 14:17:16 +00:00
										 |  |  | 	 METH_NOARGS, get_history_length_doc}, | 
					
						
							| 
									
										
										
										
											2000-08-03 02:06:16 +00:00
										 |  |  | 	{"set_completer", set_completer, METH_VARARGS, doc_set_completer}, | 
					
						
							| 
									
										
										
										
											2003-01-30 10:12:51 +00:00
										 |  |  | 	{"get_completer", get_completer, METH_NOARGS, doc_get_completer}, | 
					
						
							| 
									
										
										
										
											2003-01-30 14:17:16 +00:00
										 |  |  | 	{"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx}, | 
					
						
							|  |  |  | 	{"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx}, | 
					
						
							| 
									
										
										
										
											1999-11-18 17:51:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | 	{"set_completer_delims", set_completer_delims, | 
					
						
							| 
									
										
										
										
											2000-08-03 02:34:44 +00:00
										 |  |  | 	 METH_VARARGS, doc_set_completer_delims}, | 
					
						
							| 
									
										
										
										
											2001-10-19 01:18:43 +00:00
										 |  |  | 	{"add_history", py_add_history, METH_VARARGS, doc_add_history}, | 
					
						
							| 
									
										
										
										
											2004-08-15 14:32:06 +00:00
										 |  |  |         {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history}, | 
					
						
							|  |  |  |         {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history}, | 
					
						
							| 
									
										
										
										
											2003-01-30 14:17:16 +00:00
										 |  |  | 	{"get_completer_delims", get_completer_delims, | 
					
						
							| 
									
										
										
										
											2002-03-25 20:46:46 +00:00
										 |  |  | 	 METH_NOARGS, doc_get_completer_delims}, | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	{"set_startup_hook", set_startup_hook, | 
					
						
							|  |  |  | 	 METH_VARARGS, doc_set_startup_hook}, | 
					
						
							| 
									
										
										
										
											2001-09-30 21:09:59 +00:00
										 |  |  | #ifdef HAVE_RL_PRE_INPUT_HOOK
 | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | 	{"set_pre_input_hook", set_pre_input_hook, | 
					
						
							|  |  |  | 	 METH_VARARGS, doc_set_pre_input_hook}, | 
					
						
							| 
									
										
										
										
											2003-09-20 16:08:33 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
 | 
					
						
							|  |  |  | 	{"clear_history", py_clear_history, METH_NOARGS, doc_clear_history}, | 
					
						
							| 
									
										
										
										
											2001-09-30 21:09:59 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | 	{0, 0} | 
					
						
							| 
									
										
										
										
											1997-08-05 21:27:50 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-07 20:04:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-09-30 21:09:59 +00:00
										 |  |  | /* C function to call the Python hooks. */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2005-03-30 11:21:53 +00:00
										 |  |  | on_hook(PyObject *func) | 
					
						
							| 
									
										
										
										
											2001-09-30 21:09:59 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int result = 0; | 
					
						
							|  |  |  | 	if (func != NULL) { | 
					
						
							|  |  |  | 		PyObject *r; | 
					
						
							| 
									
										
										
										
											2005-03-30 11:21:53 +00:00
										 |  |  | #ifdef WITH_THREAD	      
 | 
					
						
							|  |  |  | 		PyGILState_STATE gilstate = PyGILState_Ensure(); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2001-09-30 21:09:59 +00:00
										 |  |  | 		r = PyObject_CallFunction(func, NULL); | 
					
						
							|  |  |  | 		if (r == NULL) | 
					
						
							|  |  |  | 			goto error; | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | 		if (r == Py_None) | 
					
						
							| 
									
										
										
										
											2001-09-30 21:09:59 +00:00
										 |  |  | 			result = 0; | 
					
						
							| 
									
										
										
										
											2005-03-30 11:21:53 +00:00
										 |  |  | 		else { | 
					
						
							| 
									
										
										
										
											2001-09-30 21:09:59 +00:00
										 |  |  | 			result = PyInt_AsLong(r); | 
					
						
							| 
									
										
										
										
											2005-03-30 11:21:53 +00:00
										 |  |  | 			if (result == -1 && PyErr_Occurred())  | 
					
						
							|  |  |  | 				goto error; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2001-09-30 21:09:59 +00:00
										 |  |  | 		Py_DECREF(r); | 
					
						
							|  |  |  | 		goto done; | 
					
						
							|  |  |  | 	  error: | 
					
						
							|  |  |  | 		PyErr_Clear(); | 
					
						
							|  |  |  | 		Py_XDECREF(r); | 
					
						
							|  |  |  | 	  done: | 
					
						
							| 
									
										
										
										
											2005-03-30 11:21:53 +00:00
										 |  |  | #ifdef WITH_THREAD	      
 | 
					
						
							|  |  |  | 		PyGILState_Release(gilstate); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2005-09-29 13:40:49 +00:00
										 |  |  | 		return result; | 
					
						
							| 
									
										
										
										
											2001-09-30 21:09:59 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | on_startup_hook(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2005-03-30 11:21:53 +00:00
										 |  |  | 	return on_hook(startup_hook); | 
					
						
							| 
									
										
										
										
											2001-09-30 21:09:59 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef HAVE_RL_PRE_INPUT_HOOK
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | on_pre_input_hook(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2005-03-30 11:21:53 +00:00
										 |  |  | 	return on_hook(pre_input_hook); | 
					
						
							| 
									
										
										
										
											2001-09-30 21:09:59 +00:00
										 |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-07 20:04:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | /* C function to call the Python completer. */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char * | 
					
						
							| 
									
										
										
										
											2000-07-10 09:57:19 +00:00
										 |  |  | on_completion(char *text, int state) | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	char *result = NULL; | 
					
						
							|  |  |  | 	if (completer != NULL) { | 
					
						
							|  |  |  | 		PyObject *r; | 
					
						
							| 
									
										
										
										
											2005-03-30 11:21:53 +00:00
										 |  |  | #ifdef WITH_THREAD	      
 | 
					
						
							|  |  |  | 		PyGILState_STATE gilstate = PyGILState_Ensure(); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2002-02-13 11:58:25 +00:00
										 |  |  | 		rl_attempted_completion_over = 1; | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00: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: | 
					
						
							| 
									
										
										
										
											2005-03-30 11:21:53 +00:00
										 |  |  | #ifdef WITH_THREAD	      
 | 
					
						
							|  |  |  | 		PyGILState_Release(gilstate); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2005-09-29 13:40:49 +00:00
										 |  |  | 		return result; | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-07 20:34:19 +00:00
										 |  |  | /* A more flexible constructor that saves the "begidx" and "endidx"
 | 
					
						
							| 
									
										
										
										
											1999-11-18 17:51:02 +00:00
										 |  |  |  * before calling the normal completer */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-02-21 00:30:18 +00:00
										 |  |  | static char ** | 
					
						
							| 
									
										
										
										
											2000-07-10 09:57:19 +00:00
										 |  |  | flex_complete(char *text, int start, int end) | 
					
						
							| 
									
										
										
										
											1999-11-18 17:51:02 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	Py_XDECREF(begidx); | 
					
						
							|  |  |  | 	Py_XDECREF(endidx); | 
					
						
							|  |  |  | 	begidx = PyInt_FromLong((long) start); | 
					
						
							|  |  |  | 	endidx = PyInt_FromLong((long) end); | 
					
						
							|  |  |  | 	return completion_matches(text, *on_completion); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-07 20:04:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | /* Helper to initialize GNU readline properly. */ | 
					
						
							| 
									
										
										
										
											1997-08-05 21:27:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2000-07-21 06:00:07 +00:00
										 |  |  | setup_readline(void) | 
					
						
							| 
									
										
										
										
											1997-08-05 21:27:50 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2002-10-09 21:27:33 +00:00
										 |  |  | #ifdef SAVE_LOCALE
 | 
					
						
							| 
									
										
										
										
											2003-11-13 07:43:21 +00:00
										 |  |  | 	char *saved_locale = strdup(setlocale(LC_CTYPE, NULL)); | 
					
						
							| 
									
										
										
										
											2004-08-20 06:26:59 +00:00
										 |  |  | 	if (!saved_locale) | 
					
						
							|  |  |  | 		Py_FatalError("not enough memory to save locale"); | 
					
						
							| 
									
										
										
										
											2002-10-09 21:27:33 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-11 14:32:46 +00:00
										 |  |  | 	using_history(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | 	rl_readline_name = "python"; | 
					
						
							| 
									
										
										
										
											2002-03-03 02:59:16 +00:00
										 |  |  | #if defined(PYOS_OS2) && defined(PYCC_GCC)
 | 
					
						
							|  |  |  | 	/* Allow $if term= in .inputrc to work */ | 
					
						
							|  |  |  | 	rl_terminal_name = getenv("TERM"); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | 	/* 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 21:09:59 +00: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 21:51:21 +00:00
										 |  |  | 	/* Set our completion function */ | 
					
						
							| 
									
										
										
										
											1999-11-18 17:51:02 +00:00
										 |  |  | 	rl_attempted_completion_function = (CPPFunction *)flex_complete; | 
					
						
							| 
									
										
										
										
											1997-09-26 23:00:37 +00:00
										 |  |  | 	/* Set Python word break characters */ | 
					
						
							|  |  |  | 	rl_completer_word_break_characters = | 
					
						
							| 
									
										
										
										
											1999-11-18 17:51:02 +00:00
										 |  |  | 		strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?"); | 
					
						
							| 
									
										
										
										
											1997-09-26 23:00:37 +00:00
										 |  |  | 		/* All nonalphanums except '.' */ | 
					
						
							| 
									
										
										
										
											2002-12-30 16:25:41 +00:00
										 |  |  | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
 | 
					
						
							| 
									
										
										
										
											2002-05-30 15:41:56 +00:00
										 |  |  | 	rl_completion_append_character ='\0'; | 
					
						
							| 
									
										
										
										
											2002-12-30 16:25:41 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1999-11-18 17:51:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	begidx = PyInt_FromLong(0L); | 
					
						
							|  |  |  | 	endidx = PyInt_FromLong(0L); | 
					
						
							| 
									
										
										
										
											1999-01-29 21:55:03 +00: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 21:51:21 +00:00
										 |  |  | 	rl_initialize(); | 
					
						
							| 
									
										
										
										
											2002-10-09 21:27:33 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef SAVE_LOCALE
 | 
					
						
							|  |  |  | 	setlocale(LC_CTYPE, saved_locale); /* Restore locale */ | 
					
						
							| 
									
										
										
										
											2003-11-13 07:43:21 +00:00
										 |  |  | 	free(saved_locale); | 
					
						
							| 
									
										
										
										
											2002-10-09 21:27:33 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1997-08-05 21:27:50 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-07 17:44:12 +00:00
										 |  |  | /* Wrapper around GNU readline that handles signals differently. */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static	char *completed_input_string; | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | rlhandler(char *text) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	completed_input_string = text; | 
					
						
							|  |  |  | 	rl_callback_handler_remove(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | extern PyThreadState* _PyOS_ReadlineTState; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char * | 
					
						
							|  |  |  | readline_until_enter_or_signal(char *prompt, int *signal) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	char * not_done_reading = ""; | 
					
						
							|  |  |  | 	fd_set selectset; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	*signal = 0; | 
					
						
							|  |  |  | #ifdef HAVE_RL_CATCH_SIGNAL
 | 
					
						
							|  |  |  | 	rl_catch_signals = 0; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rl_callback_handler_install (prompt, rlhandler); | 
					
						
							|  |  |  | 	FD_ZERO(&selectset); | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	completed_input_string = not_done_reading; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-07 13:46:33 +00:00
										 |  |  | 	while (completed_input_string == not_done_reading) { | 
					
						
							|  |  |  | 		int has_input = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		while (!has_input) | 
					
						
							|  |  |  | 		{	struct timeval timeout = {0, 100000}; /* 0.1 seconds */ | 
					
						
							|  |  |  | 			FD_SET(fileno(rl_instream), &selectset); | 
					
						
							|  |  |  | 			/* select resets selectset if no input was available */ | 
					
						
							|  |  |  | 			has_input = select(fileno(rl_instream) + 1, &selectset, | 
					
						
							|  |  |  | 					   NULL, NULL, &timeout); | 
					
						
							|  |  |  | 			if(PyOS_InputHook) PyOS_InputHook(); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-07-07 17:44:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if(has_input > 0) { | 
					
						
							|  |  |  | 			rl_callback_read_char(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else if (errno == EINTR) { | 
					
						
							|  |  |  | 			int s; | 
					
						
							| 
									
										
										
										
											2005-04-07 10:11:19 +00:00
										 |  |  | #ifdef WITH_THREAD
 | 
					
						
							| 
									
										
										
										
											2004-07-07 17:44:12 +00:00
										 |  |  | 			PyEval_RestoreThread(_PyOS_ReadlineTState); | 
					
						
							| 
									
										
										
										
											2005-04-07 10:11:19 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2004-07-07 17:44:12 +00:00
										 |  |  | 			s = PyErr_CheckSignals(); | 
					
						
							| 
									
										
										
										
											2005-04-07 10:11:19 +00:00
										 |  |  | #ifdef WITH_THREAD
 | 
					
						
							| 
									
										
										
										
											2004-07-08 15:28:26 +00:00
										 |  |  | 			PyEval_SaveThread();	 | 
					
						
							| 
									
										
										
										
											2005-04-07 10:11:19 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2004-07-07 17:44:12 +00:00
										 |  |  | 			if (s < 0) { | 
					
						
							|  |  |  | 				rl_free_line_state(); | 
					
						
							|  |  |  | 				rl_cleanup_after_signal(); | 
					
						
							|  |  |  | 				rl_callback_handler_remove(); | 
					
						
							|  |  |  | 				*signal = 1; | 
					
						
							|  |  |  | 				completed_input_string = NULL; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return completed_input_string; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Interrupt handler */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static jmp_buf jbuf; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-05 21:27:50 +00:00
										 |  |  | /* ARGSUSED */ | 
					
						
							| 
									
										
										
										
											2000-07-23 21:18:09 +00:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2000-07-10 09:57:19 +00:00
										 |  |  | onintr(int sig) | 
					
						
							| 
									
										
										
										
											1997-08-05 21:27:50 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | 	longjmp(jbuf, 1); | 
					
						
							| 
									
										
										
										
											1997-08-05 21:27:50 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-05 21:27:50 +00:00
										 |  |  | static char * | 
					
						
							| 
									
										
										
										
											2004-07-07 17:44:12 +00:00
										 |  |  | readline_until_enter_or_signal(char *prompt, int *signal) | 
					
						
							| 
									
										
										
										
											1997-08-05 21:27:50 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2000-09-16 16:37:53 +00:00
										 |  |  | 	PyOS_sighandler_t old_inthandler; | 
					
						
							| 
									
										
										
										
											2004-07-07 17:44:12 +00:00
										 |  |  | 	char *p; | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  | 	*signal = 0; | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-16 16:37:53 +00:00
										 |  |  | 	old_inthandler = PyOS_setsig(SIGINT, onintr); | 
					
						
							| 
									
										
										
										
											1997-08-05 21:27:50 +00:00
										 |  |  | 	if (setjmp(jbuf)) { | 
					
						
							|  |  |  | #ifdef HAVE_SIGRELSE
 | 
					
						
							|  |  |  | 		/* This seems necessary on SunOS 4.1 (Rasmus Hahn) */ | 
					
						
							|  |  |  | 		sigrelse(SIGINT); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2000-09-16 16:37:53 +00:00
										 |  |  | 		PyOS_setsig(SIGINT, old_inthandler); | 
					
						
							| 
									
										
										
										
											2004-07-07 17:44:12 +00:00
										 |  |  | 		*signal = 1; | 
					
						
							| 
									
										
										
										
											1997-08-05 21:27:50 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-10-07 13:46:33 +00:00
										 |  |  | 	rl_event_hook = PyOS_InputHook; | 
					
						
							| 
									
										
										
										
											2004-07-07 17:44:12 +00:00
										 |  |  | 	p = readline(prompt); | 
					
						
							|  |  |  | 	PyOS_setsig(SIGINT, old_inthandler); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return p; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char * | 
					
						
							|  |  |  | call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2004-08-25 01:20:18 +00:00
										 |  |  | 	size_t n; | 
					
						
							|  |  |  | 	char *p, *q; | 
					
						
							|  |  |  | 	int signal; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-18 13:34:00 +00:00
										 |  |  | #ifdef SAVE_LOCALE
 | 
					
						
							|  |  |  | 	char *saved_locale = strdup(setlocale(LC_CTYPE, NULL)); | 
					
						
							| 
									
										
										
										
											2004-08-20 06:26:59 +00:00
										 |  |  | 	if (!saved_locale) | 
					
						
							|  |  |  | 		Py_FatalError("not enough memory to save locale"); | 
					
						
							| 
									
										
										
										
											2004-08-18 13:34:00 +00:00
										 |  |  | 	setlocale(LC_CTYPE, ""); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2004-07-07 17:44:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | 	if (sys_stdin != rl_instream || sys_stdout != rl_outstream) { | 
					
						
							|  |  |  | 		rl_instream = sys_stdin; | 
					
						
							|  |  |  | 		rl_outstream = sys_stdout; | 
					
						
							| 
									
										
										
										
											2002-12-30 16:25:41 +00:00
										 |  |  | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
 | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | 		rl_prep_terminal (1); | 
					
						
							| 
									
										
										
										
											2002-12-30 16:25:41 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-07 17:44:12 +00:00
										 |  |  | 	p = readline_until_enter_or_signal(prompt, &signal); | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	/* we got an interrupt signal */ | 
					
						
							|  |  |  | 	if(signal) { | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2000-05-03 23:44:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-07 17:44:12 +00:00
										 |  |  | 	/* We got an EOF, return a empty string. */ | 
					
						
							| 
									
										
										
										
											1997-08-05 21:27:50 +00:00
										 |  |  | 	if (p == NULL) { | 
					
						
							| 
									
										
										
										
											2000-05-03 23:44:39 +00:00
										 |  |  | 		p = PyMem_Malloc(1); | 
					
						
							| 
									
										
										
										
											1997-08-05 21:27:50 +00:00
										 |  |  | 		if (p != NULL) | 
					
						
							|  |  |  | 			*p = '\0'; | 
					
						
							|  |  |  | 		return p; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-07-07 17:44:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* we have a valid line */ | 
					
						
							| 
									
										
										
										
											1997-08-05 21:27:50 +00:00
										 |  |  | 	n = strlen(p); | 
					
						
							| 
									
										
										
										
											2002-06-11 14:32:46 +00:00
										 |  |  | 	if (n > 0) { | 
					
						
							|  |  |  | 		char *line; | 
					
						
							|  |  |  | 		HISTORY_STATE *state = history_get_history_state(); | 
					
						
							|  |  |  | 		if (state->length > 0) | 
					
						
							|  |  |  | 			line = history_get(state->length)->line; | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			line = ""; | 
					
						
							|  |  |  | 		if (strcmp(p, line)) | 
					
						
							|  |  |  | 			add_history(p); | 
					
						
							|  |  |  | 		/* the history docs don't say so, but the address of state
 | 
					
						
							|  |  |  | 		   changes each time history_get_history_state is called | 
					
						
							|  |  |  | 		   which makes me think it's freshly malloc'd memory... | 
					
						
							|  |  |  | 		   on the other hand, the address of the last line stays the | 
					
						
							|  |  |  | 		   same as long as history isn't extended, so it appears to | 
					
						
							|  |  |  | 		   be malloc'd but managed by the history package... */ | 
					
						
							|  |  |  | 		free(state); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2000-05-03 23:44:39 +00: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 21:27:50 +00:00
										 |  |  | 		p[n] = '\n'; | 
					
						
							|  |  |  | 		p[n+1] = '\0'; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2000-05-03 23:44:39 +00:00
										 |  |  | 	free(q); | 
					
						
							| 
									
										
										
										
											2004-08-18 13:34:00 +00:00
										 |  |  | #ifdef SAVE_LOCALE
 | 
					
						
							|  |  |  | 	setlocale(LC_CTYPE, saved_locale); /* Restore locale */ | 
					
						
							|  |  |  | 	free(saved_locale); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1997-08-05 21:27:50 +00:00
										 |  |  | 	return p; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Initialize the module */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(doc_module, | 
					
						
							|  |  |  | "Importing this module enables command line editing using GNU readline."); | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-08-02 02:27:13 +00:00
										 |  |  | PyMODINIT_FUNC | 
					
						
							| 
									
										
										
										
											2000-07-21 06:00:07 +00:00
										 |  |  | initreadline(void) | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2000-10-02 15:53:08 +00:00
										 |  |  | 	PyObject *m; | 
					
						
							| 
									
										
										
										
											1997-09-26 21:51:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	m = Py_InitModule4("readline", readline_methods, doc_module, | 
					
						
							|  |  |  | 			   (PyObject *)NULL, PYTHON_API_VERSION); | 
					
						
							| 
									
										
										
										
											2002-10-26 14:39:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-07 20:01:29 +00:00
										 |  |  | 	PyOS_ReadlineFunctionPointer = call_readline; | 
					
						
							|  |  |  | 	setup_readline(); | 
					
						
							| 
									
										
										
										
											1997-08-05 21:27:50 +00:00
										 |  |  | } |