blob: 25a43b2341569b92b69c174ab5d6691465f1a9db [file] [log] [blame]
Guido van Rossum290900a1997-09-26 21:51:21 +00001/* This module makes GNU readline available to Python. It has ideas
2 * contributed by Lee Busby, LLNL, and William Magro, Cornell Theory
Michael W. Hudson9a8c3142005-03-30 10:09:12 +00003 * Center. The completer interface was inspired by Lele Gaifax. More
4 * recently, it was largely rewritten by Guido van Rossum.
Guido van Rossum0969d361997-08-05 21:27:50 +00005 */
6
Guido van Rossum290900a1997-09-26 21:51:21 +00007/* Standard definitions */
Guido van Rossum0969d361997-08-05 21:27:50 +00008#include "Python.h"
9#include <setjmp.h>
10#include <signal.h>
Guido van Rossum290900a1997-09-26 21:51:21 +000011#include <errno.h>
Michael W. Hudson8da2b012004-10-07 13:46:33 +000012#include <sys/time.h>
Guido van Rossum0969d361997-08-05 21:27:50 +000013
Skip Montanaro7befb992004-02-10 16:50:21 +000014#if defined(HAVE_SETLOCALE)
Guido van Rossum60c8a3a2002-10-09 21:27:33 +000015/* GNU readline() mistakenly sets the LC_CTYPE locale.
16 * This is evil. Only the user or the app's main() should do this!
17 * We must save and restore the locale around the rl_initialize() call.
18 */
19#define SAVE_LOCALE
20#include <locale.h>
21#endif
22
Guido van Rossum290900a1997-09-26 21:51:21 +000023/* GNU readline definitions */
Guido van Rossumb0e51b22001-04-13 18:14:27 +000024#undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
Guido van Rossumbcc20741998-08-04 22:53:56 +000025#include <readline/readline.h>
26#include <readline/history.h>
Guido van Rossum730806d1998-04-10 22:27:42 +000027
Guido van Rossum353ae582001-07-10 16:45:32 +000028#ifdef HAVE_RL_COMPLETION_MATCHES
Guido van Rossum74f31432003-01-07 20:01:29 +000029#define completion_matches(x, y) \
30 rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
Guido van Rossum353ae582001-07-10 16:45:32 +000031#endif
32
Guido van Rossum0969d361997-08-05 21:27:50 +000033
Guido van Rossum290900a1997-09-26 21:51:21 +000034/* Exported function to send one line to readline's init file parser */
35
36static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000037parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000038{
Guido van Rossum3b5330e1998-12-04 15:34:39 +000039 char *s, *copy;
Guido van Rossum43713e52000-02-29 13:59:29 +000040 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000041 return NULL;
Guido van Rossum3b5330e1998-12-04 15:34:39 +000042 /* Make a copy -- rl_parse_and_bind() modifies its argument */
43 /* Bernard Herzog */
44 copy = malloc(1 + strlen(s));
45 if (copy == NULL)
46 return PyErr_NoMemory();
47 strcpy(copy, s);
48 rl_parse_and_bind(copy);
49 free(copy); /* Free the copy */
Guido van Rossum290900a1997-09-26 21:51:21 +000050 Py_INCREF(Py_None);
51 return Py_None;
52}
53
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000054PyDoc_STRVAR(doc_parse_and_bind,
55"parse_and_bind(string) -> None\n\
56Parse and execute single line of a readline init file.");
Guido van Rossum290900a1997-09-26 21:51:21 +000057
58
59/* Exported function to parse a readline init file */
60
61static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000062read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000063{
64 char *s = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +000065 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000066 return NULL;
67 errno = rl_read_init_file(s);
68 if (errno)
69 return PyErr_SetFromErrno(PyExc_IOError);
70 Py_INCREF(Py_None);
71 return Py_None;
72}
73
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000074PyDoc_STRVAR(doc_read_init_file,
75"read_init_file([filename]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +000076Parse a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000077The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +000078
79
Skip Montanaro28067822000-07-06 18:55:12 +000080/* Exported function to load a readline history file */
81
82static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000083read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +000084{
85 char *s = NULL;
86 if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
87 return NULL;
88 errno = read_history(s);
89 if (errno)
90 return PyErr_SetFromErrno(PyExc_IOError);
91 Py_INCREF(Py_None);
92 return Py_None;
93}
94
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +000095static int _history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000096PyDoc_STRVAR(doc_read_history_file,
97"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +000098Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000099The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000100
101
102/* Exported function to save a readline history file */
103
104static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000105write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000106{
107 char *s = NULL;
108 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
109 return NULL;
110 errno = write_history(s);
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000111 if (!errno && _history_length >= 0)
112 history_truncate_file(s, _history_length);
Skip Montanaro28067822000-07-06 18:55:12 +0000113 if (errno)
114 return PyErr_SetFromErrno(PyExc_IOError);
115 Py_INCREF(Py_None);
116 return Py_None;
117}
118
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000119PyDoc_STRVAR(doc_write_history_file,
120"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000121Save a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000122The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000123
124
Guido van Rossum74f31432003-01-07 20:01:29 +0000125/* Set history length */
126
127static PyObject*
128set_history_length(PyObject *self, PyObject *args)
129{
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000130 int length = _history_length;
Guido van Rossum74f31432003-01-07 20:01:29 +0000131 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
132 return NULL;
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000133 _history_length = length;
Guido van Rossum74f31432003-01-07 20:01:29 +0000134 Py_INCREF(Py_None);
135 return Py_None;
136}
137
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000138PyDoc_STRVAR(set_history_length_doc,
139"set_history_length(length) -> None\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000140set the maximal number of items which will be written to\n\
141the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000142history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000143
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000144
Guido van Rossum74f31432003-01-07 20:01:29 +0000145/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000146
147static PyObject*
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000148get_history_length(PyObject *self, PyObject *noarg)
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000149{
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000150 return PyInt_FromLong(_history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000151}
152
Guido van Rossum74f31432003-01-07 20:01:29 +0000153PyDoc_STRVAR(get_history_length_doc,
154"get_history_length() -> int\n\
155return the maximum number of items that will be written to\n\
156the history file.");
157
158
Martin v. Löwis0daad592001-09-30 21:09:59 +0000159/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000160
Martin v. Löwis0daad592001-09-30 21:09:59 +0000161static PyObject *
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000162set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000163{
164 PyObject *function = Py_None;
165 char buf[80];
Tim Peters885d4572001-11-28 20:27:42 +0000166 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000167 if (!PyArg_ParseTuple(args, buf, &function))
168 return NULL;
169 if (function == Py_None) {
170 Py_XDECREF(*hook_var);
171 *hook_var = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000172 }
173 else if (PyCallable_Check(function)) {
174 PyObject *tmp = *hook_var;
175 Py_INCREF(function);
176 *hook_var = function;
177 Py_XDECREF(tmp);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000178 }
179 else {
Tim Peters885d4572001-11-28 20:27:42 +0000180 PyOS_snprintf(buf, sizeof(buf),
181 "set_%.50s(func): argument not callable",
182 funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000183 PyErr_SetString(PyExc_TypeError, buf);
184 return NULL;
185 }
186 Py_INCREF(Py_None);
187 return Py_None;
188}
189
Guido van Rossum74f31432003-01-07 20:01:29 +0000190
Martin v. Löwis0daad592001-09-30 21:09:59 +0000191/* Exported functions to specify hook functions in Python */
192
193static PyObject *startup_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000194
195#ifdef HAVE_RL_PRE_INPUT_HOOK
196static PyObject *pre_input_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000197#endif
198
199static PyObject *
200set_startup_hook(PyObject *self, PyObject *args)
201{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000202 return set_hook("startup_hook", &startup_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000203}
204
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000205PyDoc_STRVAR(doc_set_startup_hook,
206"set_startup_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000207Set or remove the startup_hook function.\n\
208The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000209before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000210
Guido van Rossum74f31432003-01-07 20:01:29 +0000211
Martin v. Löwis0daad592001-09-30 21:09:59 +0000212#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000213
214/* Set pre-input hook */
215
Martin v. Löwis0daad592001-09-30 21:09:59 +0000216static PyObject *
217set_pre_input_hook(PyObject *self, PyObject *args)
218{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000219 return set_hook("pre_input_hook", &pre_input_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000220}
221
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000222PyDoc_STRVAR(doc_set_pre_input_hook,
223"set_pre_input_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000224Set or remove the pre_input_hook function.\n\
225The function is called with no arguments after the first prompt\n\
226has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000227characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000228
Martin v. Löwis0daad592001-09-30 21:09:59 +0000229#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000230
Guido van Rossum74f31432003-01-07 20:01:29 +0000231
Guido van Rossum290900a1997-09-26 21:51:21 +0000232/* Exported function to specify a word completer in Python */
233
234static PyObject *completer = NULL;
Guido van Rossum290900a1997-09-26 21:51:21 +0000235
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000236static PyObject *begidx = NULL;
237static PyObject *endidx = NULL;
238
Guido van Rossum74f31432003-01-07 20:01:29 +0000239
240/* Get the beginning index for the scope of the tab-completion */
241
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000242static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000243get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000244{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000245 Py_INCREF(begidx);
246 return begidx;
247}
248
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000249PyDoc_STRVAR(doc_get_begidx,
250"get_begidx() -> int\n\
251get the beginning index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000252
Guido van Rossum74f31432003-01-07 20:01:29 +0000253
254/* Get the ending index for the scope of the tab-completion */
255
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000256static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000257get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000258{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000259 Py_INCREF(endidx);
260 return endidx;
261}
262
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000263PyDoc_STRVAR(doc_get_endidx,
264"get_endidx() -> int\n\
265get the ending index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000266
267
Guido van Rossum74f31432003-01-07 20:01:29 +0000268/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000269
270static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000271set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000272{
273 char *break_chars;
274
Guido van Rossum43713e52000-02-29 13:59:29 +0000275 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000276 return NULL;
277 }
Neal Norwitz0e0ee592002-04-21 15:03:18 +0000278 free((void*)rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000279 rl_completer_word_break_characters = strdup(break_chars);
280 Py_INCREF(Py_None);
281 return Py_None;
282}
283
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000284PyDoc_STRVAR(doc_set_completer_delims,
285"set_completer_delims(string) -> None\n\
286set the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000287
Skip Montanaroe5069012004-08-15 14:32:06 +0000288static PyObject *
289py_remove_history(PyObject *self, PyObject *args)
290{
291 int entry_number;
292 HIST_ENTRY *entry;
293
294 if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
295 return NULL;
Martin v. Löwis9533e342005-02-27 20:33:25 +0000296 if (entry_number < 0) {
297 PyErr_SetString(PyExc_ValueError,
298 "History index cannot be negative");
299 return NULL;
300 }
Skip Montanaroe5069012004-08-15 14:32:06 +0000301 entry = remove_history(entry_number);
302 if (!entry) {
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000303 PyErr_Format(PyExc_ValueError,
304 "No history item at position %d",
305 entry_number);
Skip Montanaroe5069012004-08-15 14:32:06 +0000306 return NULL;
307 }
308 /* free memory allocated for the history entry */
309 if (entry->line)
310 free(entry->line);
311 if (entry->data)
312 free(entry->data);
313 free(entry);
314
315 Py_INCREF(Py_None);
316 return Py_None;
317}
318
319PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000320"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000321remove history item given by its position");
322
323static PyObject *
324py_replace_history(PyObject *self, PyObject *args)
325{
326 int entry_number;
327 char *line;
328 HIST_ENTRY *old_entry;
329
330 if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number, &line)) {
331 return NULL;
332 }
Martin v. Löwis9533e342005-02-27 20:33:25 +0000333 if (entry_number < 0) {
334 PyErr_SetString(PyExc_ValueError,
335 "History index cannot be negative");
336 return NULL;
337 }
Skip Montanaroe5069012004-08-15 14:32:06 +0000338 old_entry = replace_history_entry(entry_number, line, (void *)NULL);
339 if (!old_entry) {
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000340 PyErr_Format(PyExc_ValueError,
341 "No history item at position %d",
342 entry_number);
Skip Montanaroe5069012004-08-15 14:32:06 +0000343 return NULL;
344 }
345 /* free memory allocated for the old history entry */
346 if (old_entry->line)
347 free(old_entry->line);
348 if (old_entry->data)
349 free(old_entry->data);
350 free(old_entry);
351
352 Py_INCREF(Py_None);
353 return Py_None;
354}
355
356PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000357"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000358replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000359
360/* Add a line to the history buffer */
361
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000362static PyObject *
363py_add_history(PyObject *self, PyObject *args)
364{
365 char *line;
366
367 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
368 return NULL;
369 }
370 add_history(line);
371 Py_INCREF(Py_None);
372 return Py_None;
373}
374
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000375PyDoc_STRVAR(doc_add_history,
376"add_history(string) -> None\n\
377add a line to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000378
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000379
Guido van Rossum74f31432003-01-07 20:01:29 +0000380/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000381
382static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000383get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000384{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000385 return PyString_FromString(rl_completer_word_break_characters);
386}
Guido van Rossum74f31432003-01-07 20:01:29 +0000387
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000388PyDoc_STRVAR(doc_get_completer_delims,
389"get_completer_delims() -> string\n\
390get the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000391
Guido van Rossum74f31432003-01-07 20:01:29 +0000392
393/* Set the completer function */
394
Guido van Rossum290900a1997-09-26 21:51:21 +0000395static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000396set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000397{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000398 return set_hook("completer", &completer, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000399}
400
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000401PyDoc_STRVAR(doc_set_completer,
402"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000403Set or remove the completer function.\n\
404The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000405for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000406It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000407
Guido van Rossum74f31432003-01-07 20:01:29 +0000408
Michael W. Hudson796df152003-01-30 10:12:51 +0000409static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000410get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000411{
412 if (completer == NULL) {
413 Py_INCREF(Py_None);
414 return Py_None;
415 }
416 Py_INCREF(completer);
417 return completer;
418}
419
420PyDoc_STRVAR(doc_get_completer,
421"get_completer() -> function\n\
422\n\
423Returns current completer function.");
424
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000425/* Exported function to get any element of history */
426
427static PyObject *
428get_history_item(PyObject *self, PyObject *args)
429{
430 int idx = 0;
431 HIST_ENTRY *hist_ent;
432
433 if (!PyArg_ParseTuple(args, "i:index", &idx))
434 return NULL;
435 if ((hist_ent = history_get(idx)))
Guido van Rossum05ac4492003-01-07 20:04:12 +0000436 return PyString_FromString(hist_ent->line);
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000437 else {
438 Py_INCREF(Py_None);
439 return Py_None;
440 }
441}
442
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000443PyDoc_STRVAR(doc_get_history_item,
444"get_history_item() -> string\n\
445return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000446
Guido van Rossum74f31432003-01-07 20:01:29 +0000447
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000448/* Exported function to get current length of history */
449
450static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000451get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000452{
453 HISTORY_STATE *hist_st;
454
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000455 hist_st = history_get_history_state();
456 return PyInt_FromLong(hist_st ? (long) hist_st->length : (long) 0);
457}
458
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000459PyDoc_STRVAR(doc_get_current_history_length,
460"get_current_history_length() -> integer\n\
461return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000462
Guido van Rossum74f31432003-01-07 20:01:29 +0000463
Guido van Rossum79378ff1997-10-07 14:53:21 +0000464/* Exported function to read the current line buffer */
465
466static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000467get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000468{
Guido van Rossum79378ff1997-10-07 14:53:21 +0000469 return PyString_FromString(rl_line_buffer);
470}
471
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000472PyDoc_STRVAR(doc_get_line_buffer,
473"get_line_buffer() -> string\n\
474return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000475
Guido van Rossum74f31432003-01-07 20:01:29 +0000476
Martin v. Löwise7a97962003-09-20 16:08:33 +0000477#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
478
479/* Exported function to clear the current history */
480
481static PyObject *
482py_clear_history(PyObject *self, PyObject *noarg)
483{
484 clear_history();
485 Py_INCREF(Py_None);
486 return Py_None;
487}
488
489PyDoc_STRVAR(doc_clear_history,
490"clear_history() -> None\n\
491Clear the current readline history.");
492#endif
493
494
Guido van Rossum79378ff1997-10-07 14:53:21 +0000495/* Exported function to insert text into the line buffer */
496
497static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000498insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000499{
500 char *s;
Guido van Rossum43713e52000-02-29 13:59:29 +0000501 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000502 return NULL;
503 rl_insert_text(s);
504 Py_INCREF(Py_None);
505 return Py_None;
506}
507
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000508PyDoc_STRVAR(doc_insert_text,
509"insert_text(string) -> None\n\
510Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000511
Guido van Rossum74f31432003-01-07 20:01:29 +0000512
513/* Redisplay the line buffer */
514
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000515static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000516redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000517{
518 rl_redisplay();
519 Py_INCREF(Py_None);
520 return Py_None;
521}
522
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000523PyDoc_STRVAR(doc_redisplay,
524"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000525Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000526contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000527
Guido van Rossum74f31432003-01-07 20:01:29 +0000528
Guido van Rossum290900a1997-09-26 21:51:21 +0000529/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000530
531static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000532{
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000533 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000534 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000535 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000536 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000537 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000538 {"read_history_file", read_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000539 METH_VARARGS, doc_read_history_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000540 {"write_history_file", write_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000541 METH_VARARGS, doc_write_history_file},
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000542 {"get_history_item", get_history_item,
543 METH_VARARGS, doc_get_history_item},
Neal Norwitz767f8352002-03-31 16:13:39 +0000544 {"get_current_history_length", (PyCFunction)get_current_history_length,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000545 METH_NOARGS, doc_get_current_history_length},
Guido van Rossum74f31432003-01-07 20:01:29 +0000546 {"set_history_length", set_history_length,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000547 METH_VARARGS, set_history_length_doc},
Guido van Rossum74f31432003-01-07 20:01:29 +0000548 {"get_history_length", get_history_length,
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000549 METH_NOARGS, get_history_length_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000550 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
Michael W. Hudson796df152003-01-30 10:12:51 +0000551 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000552 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
553 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000554
Guido van Rossum74f31432003-01-07 20:01:29 +0000555 {"set_completer_delims", set_completer_delims,
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000556 METH_VARARGS, doc_set_completer_delims},
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000557 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
Skip Montanaroe5069012004-08-15 14:32:06 +0000558 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
559 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000560 {"get_completer_delims", get_completer_delims,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000561 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000562
563 {"set_startup_hook", set_startup_hook,
564 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000565#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000566 {"set_pre_input_hook", set_pre_input_hook,
567 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000568#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000569#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
570 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
571#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000572 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000573};
574
Guido van Rossum05ac4492003-01-07 20:04:12 +0000575
Martin v. Löwis0daad592001-09-30 21:09:59 +0000576/* C function to call the Python hooks. */
577
578static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000579on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000580{
581 int result = 0;
582 if (func != NULL) {
583 PyObject *r;
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000584#ifdef WITH_THREAD
585 PyGILState_STATE gilstate = PyGILState_Ensure();
586#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000587 r = PyObject_CallFunction(func, NULL);
588 if (r == NULL)
589 goto error;
Guido van Rossum74f31432003-01-07 20:01:29 +0000590 if (r == Py_None)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000591 result = 0;
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000592 else {
Martin v. Löwis0daad592001-09-30 21:09:59 +0000593 result = PyInt_AsLong(r);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000594 if (result == -1 && PyErr_Occurred())
595 goto error;
596 }
Martin v. Löwis0daad592001-09-30 21:09:59 +0000597 Py_DECREF(r);
598 goto done;
599 error:
600 PyErr_Clear();
601 Py_XDECREF(r);
602 done:
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000603#ifdef WITH_THREAD
604 PyGILState_Release(gilstate);
605#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000606 }
607 return result;
608}
609
610static int
611on_startup_hook(void)
612{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000613 return on_hook(startup_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000614}
615
616#ifdef HAVE_RL_PRE_INPUT_HOOK
617static int
618on_pre_input_hook(void)
619{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000620 return on_hook(pre_input_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000621}
622#endif
623
Guido van Rossum05ac4492003-01-07 20:04:12 +0000624
Guido van Rossum290900a1997-09-26 21:51:21 +0000625/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000626
Guido van Rossum290900a1997-09-26 21:51:21 +0000627static char *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000628on_completion(char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000629{
Guido van Rossum290900a1997-09-26 21:51:21 +0000630 char *result = NULL;
631 if (completer != NULL) {
632 PyObject *r;
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000633#ifdef WITH_THREAD
634 PyGILState_STATE gilstate = PyGILState_Ensure();
635#endif
Michael W. Hudson0c1ceaf2002-02-13 11:58:25 +0000636 rl_attempted_completion_over = 1;
Guido van Rossum290900a1997-09-26 21:51:21 +0000637 r = PyObject_CallFunction(completer, "si", text, state);
638 if (r == NULL)
639 goto error;
640 if (r == Py_None) {
641 result = NULL;
642 }
643 else {
644 char *s = PyString_AsString(r);
645 if (s == NULL)
646 goto error;
647 result = strdup(s);
648 }
649 Py_DECREF(r);
650 goto done;
651 error:
652 PyErr_Clear();
653 Py_XDECREF(r);
654 done:
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000655#ifdef WITH_THREAD
656 PyGILState_Release(gilstate);
657#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000658 }
659 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000660}
661
Guido van Rossum290900a1997-09-26 21:51:21 +0000662
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000663/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000664 * before calling the normal completer */
665
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000666static char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000667flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000668{
669 Py_XDECREF(begidx);
670 Py_XDECREF(endidx);
671 begidx = PyInt_FromLong((long) start);
672 endidx = PyInt_FromLong((long) end);
673 return completion_matches(text, *on_completion);
674}
675
Guido van Rossum05ac4492003-01-07 20:04:12 +0000676
Guido van Rossum290900a1997-09-26 21:51:21 +0000677/* Helper to initialize GNU readline properly. */
678
679static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000680setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000681{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000682#ifdef SAVE_LOCALE
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000683 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwis701abe72004-08-20 06:26:59 +0000684 if (!saved_locale)
685 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000686#endif
687
Skip Montanaroa0392742002-06-11 14:32:46 +0000688 using_history();
689
Guido van Rossum290900a1997-09-26 21:51:21 +0000690 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000691#if defined(PYOS_OS2) && defined(PYCC_GCC)
692 /* Allow $if term= in .inputrc to work */
693 rl_terminal_name = getenv("TERM");
694#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000695 /* Force rebind of TAB to insert-tab */
696 rl_bind_key('\t', rl_insert);
697 /* Bind both ESC-TAB and ESC-ESC to the completion function */
698 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
699 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000700 /* Set our hook functions */
701 rl_startup_hook = (Function *)on_startup_hook;
702#ifdef HAVE_RL_PRE_INPUT_HOOK
703 rl_pre_input_hook = (Function *)on_pre_input_hook;
704#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000705 /* Set our completion function */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000706 rl_attempted_completion_function = (CPPFunction *)flex_complete;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000707 /* Set Python word break characters */
708 rl_completer_word_break_characters =
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000709 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000710 /* All nonalphanums except '.' */
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000711#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum84271bb2002-05-30 15:41:56 +0000712 rl_completion_append_character ='\0';
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000713#endif
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000714
715 begidx = PyInt_FromLong(0L);
716 endidx = PyInt_FromLong(0L);
Barry Warsawf7612871999-01-29 21:55:03 +0000717 /* Initialize (allows .inputrc to override)
718 *
719 * XXX: A bug in the readline-2.2 library causes a memory leak
720 * inside this function. Nothing we can do about it.
721 */
Guido van Rossum290900a1997-09-26 21:51:21 +0000722 rl_initialize();
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000723
724#ifdef SAVE_LOCALE
725 setlocale(LC_CTYPE, saved_locale); /* Restore locale */
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000726 free(saved_locale);
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000727#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000728}
729
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000730/* Wrapper around GNU readline that handles signals differently. */
731
732
733#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
734
735static char *completed_input_string;
736static void
737rlhandler(char *text)
738{
739 completed_input_string = text;
740 rl_callback_handler_remove();
741}
742
743extern PyThreadState* _PyOS_ReadlineTState;
744
745static char *
746readline_until_enter_or_signal(char *prompt, int *signal)
747{
748 char * not_done_reading = "";
749 fd_set selectset;
750
751 *signal = 0;
752#ifdef HAVE_RL_CATCH_SIGNAL
753 rl_catch_signals = 0;
754#endif
755
756 rl_callback_handler_install (prompt, rlhandler);
757 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000758
759 completed_input_string = not_done_reading;
760
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000761 while (completed_input_string == not_done_reading) {
762 int has_input = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000763
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000764 while (!has_input)
765 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
766 FD_SET(fileno(rl_instream), &selectset);
767 /* select resets selectset if no input was available */
768 has_input = select(fileno(rl_instream) + 1, &selectset,
769 NULL, NULL, &timeout);
770 if(PyOS_InputHook) PyOS_InputHook();
771 }
772
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000773 if(has_input > 0) {
774 rl_callback_read_char();
775 }
776 else if (errno == EINTR) {
777 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000778#ifdef WITH_THREAD
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000779 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000780#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000781 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000782#ifdef WITH_THREAD
Michael W. Hudson23849902004-07-08 15:28:26 +0000783 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000784#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000785 if (s < 0) {
786 rl_free_line_state();
787 rl_cleanup_after_signal();
788 rl_callback_handler_remove();
789 *signal = 1;
790 completed_input_string = NULL;
791 }
792 }
793 }
794
795 return completed_input_string;
796}
797
798
799#else
Guido van Rossum290900a1997-09-26 21:51:21 +0000800
801/* Interrupt handler */
802
803static jmp_buf jbuf;
804
Guido van Rossum0969d361997-08-05 21:27:50 +0000805/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +0000806static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000807onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000808{
Guido van Rossum290900a1997-09-26 21:51:21 +0000809 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000810}
811
Guido van Rossum290900a1997-09-26 21:51:21 +0000812
Guido van Rossum0969d361997-08-05 21:27:50 +0000813static char *
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000814readline_until_enter_or_signal(char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +0000815{
Guido van Rossum174efc92000-09-16 16:37:53 +0000816 PyOS_sighandler_t old_inthandler;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000817 char *p;
818
819 *signal = 0;
Guido van Rossum74f31432003-01-07 20:01:29 +0000820
Guido van Rossum174efc92000-09-16 16:37:53 +0000821 old_inthandler = PyOS_setsig(SIGINT, onintr);
Guido van Rossum0969d361997-08-05 21:27:50 +0000822 if (setjmp(jbuf)) {
823#ifdef HAVE_SIGRELSE
824 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
825 sigrelse(SIGINT);
826#endif
Guido van Rossum174efc92000-09-16 16:37:53 +0000827 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000828 *signal = 1;
Guido van Rossum0969d361997-08-05 21:27:50 +0000829 return NULL;
830 }
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000831 rl_event_hook = PyOS_InputHook;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000832 p = readline(prompt);
833 PyOS_setsig(SIGINT, old_inthandler);
834
835 return p;
836}
837#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
838
839
840static char *
841call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
842{
Neal Norwitz1fa040b2004-08-25 01:20:18 +0000843 size_t n;
844 char *p, *q;
845 int signal;
846
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000847#ifdef SAVE_LOCALE
848 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwis701abe72004-08-20 06:26:59 +0000849 if (!saved_locale)
850 Py_FatalError("not enough memory to save locale");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000851 setlocale(LC_CTYPE, "");
852#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000853
Guido van Rossum74f31432003-01-07 20:01:29 +0000854 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
855 rl_instream = sys_stdin;
856 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000857#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum74f31432003-01-07 20:01:29 +0000858 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000859#endif
Guido van Rossum74f31432003-01-07 20:01:29 +0000860 }
861
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000862 p = readline_until_enter_or_signal(prompt, &signal);
863
864 /* we got an interrupt signal */
865 if(signal) {
866 return NULL;
867 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000868
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000869 /* We got an EOF, return a empty string. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000870 if (p == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000871 p = PyMem_Malloc(1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000872 if (p != NULL)
873 *p = '\0';
874 return p;
875 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000876
877 /* we have a valid line */
Guido van Rossum0969d361997-08-05 21:27:50 +0000878 n = strlen(p);
Skip Montanaroa0392742002-06-11 14:32:46 +0000879 if (n > 0) {
880 char *line;
881 HISTORY_STATE *state = history_get_history_state();
882 if (state->length > 0)
883 line = history_get(state->length)->line;
884 else
885 line = "";
886 if (strcmp(p, line))
887 add_history(p);
888 /* the history docs don't say so, but the address of state
889 changes each time history_get_history_state is called
890 which makes me think it's freshly malloc'd memory...
891 on the other hand, the address of the last line stays the
892 same as long as history isn't extended, so it appears to
893 be malloc'd but managed by the history package... */
894 free(state);
895 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000896 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
897 release the original. */
898 q = p;
899 p = PyMem_Malloc(n+2);
900 if (p != NULL) {
901 strncpy(p, q, n);
Guido van Rossum0969d361997-08-05 21:27:50 +0000902 p[n] = '\n';
903 p[n+1] = '\0';
904 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000905 free(q);
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000906#ifdef SAVE_LOCALE
907 setlocale(LC_CTYPE, saved_locale); /* Restore locale */
908 free(saved_locale);
909#endif
Guido van Rossum0969d361997-08-05 21:27:50 +0000910 return p;
911}
912
Guido van Rossum290900a1997-09-26 21:51:21 +0000913
914/* Initialize the module */
915
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000916PyDoc_STRVAR(doc_module,
917"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000918
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000919PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000920initreadline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000921{
Guido van Rossum1ea64ea2000-10-02 15:53:08 +0000922 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +0000923
924 m = Py_InitModule4("readline", readline_methods, doc_module,
925 (PyObject *)NULL, PYTHON_API_VERSION);
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000926
Guido van Rossum74f31432003-01-07 20:01:29 +0000927 PyOS_ReadlineFunctionPointer = call_readline;
928 setup_readline();
Guido van Rossum0969d361997-08-05 21:27:50 +0000929}