blob: 8fda2281339af121643c6e2c313ebdd562b3aa82 [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
Georg Brandle677adc2005-09-29 13:40:49 +0000606 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000607 }
608 return result;
609}
610
611static int
612on_startup_hook(void)
613{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000614 return on_hook(startup_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000615}
616
617#ifdef HAVE_RL_PRE_INPUT_HOOK
618static int
619on_pre_input_hook(void)
620{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000621 return on_hook(pre_input_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000622}
623#endif
624
Guido van Rossum05ac4492003-01-07 20:04:12 +0000625
Guido van Rossum290900a1997-09-26 21:51:21 +0000626/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000627
Guido van Rossum290900a1997-09-26 21:51:21 +0000628static char *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000629on_completion(char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000630{
Guido van Rossum290900a1997-09-26 21:51:21 +0000631 char *result = NULL;
632 if (completer != NULL) {
633 PyObject *r;
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000634#ifdef WITH_THREAD
635 PyGILState_STATE gilstate = PyGILState_Ensure();
636#endif
Michael W. Hudson0c1ceaf2002-02-13 11:58:25 +0000637 rl_attempted_completion_over = 1;
Guido van Rossum290900a1997-09-26 21:51:21 +0000638 r = PyObject_CallFunction(completer, "si", text, state);
639 if (r == NULL)
640 goto error;
641 if (r == Py_None) {
642 result = NULL;
643 }
644 else {
645 char *s = PyString_AsString(r);
646 if (s == NULL)
647 goto error;
648 result = strdup(s);
649 }
650 Py_DECREF(r);
651 goto done;
652 error:
653 PyErr_Clear();
654 Py_XDECREF(r);
655 done:
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000656#ifdef WITH_THREAD
657 PyGILState_Release(gilstate);
658#endif
Georg Brandle677adc2005-09-29 13:40:49 +0000659 return result;
Guido van Rossum290900a1997-09-26 21:51:21 +0000660 }
661 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000662}
663
Guido van Rossum290900a1997-09-26 21:51:21 +0000664
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000665/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000666 * before calling the normal completer */
667
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000668static char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000669flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000670{
671 Py_XDECREF(begidx);
672 Py_XDECREF(endidx);
673 begidx = PyInt_FromLong((long) start);
674 endidx = PyInt_FromLong((long) end);
675 return completion_matches(text, *on_completion);
676}
677
Guido van Rossum05ac4492003-01-07 20:04:12 +0000678
Guido van Rossum290900a1997-09-26 21:51:21 +0000679/* Helper to initialize GNU readline properly. */
680
681static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000682setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000683{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000684#ifdef SAVE_LOCALE
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000685 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwis701abe72004-08-20 06:26:59 +0000686 if (!saved_locale)
687 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000688#endif
689
Skip Montanaroa0392742002-06-11 14:32:46 +0000690 using_history();
691
Guido van Rossum290900a1997-09-26 21:51:21 +0000692 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000693#if defined(PYOS_OS2) && defined(PYCC_GCC)
694 /* Allow $if term= in .inputrc to work */
695 rl_terminal_name = getenv("TERM");
696#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000697 /* Force rebind of TAB to insert-tab */
698 rl_bind_key('\t', rl_insert);
699 /* Bind both ESC-TAB and ESC-ESC to the completion function */
700 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
701 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000702 /* Set our hook functions */
703 rl_startup_hook = (Function *)on_startup_hook;
704#ifdef HAVE_RL_PRE_INPUT_HOOK
705 rl_pre_input_hook = (Function *)on_pre_input_hook;
706#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000707 /* Set our completion function */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000708 rl_attempted_completion_function = (CPPFunction *)flex_complete;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000709 /* Set Python word break characters */
710 rl_completer_word_break_characters =
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000711 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000712 /* All nonalphanums except '.' */
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000713#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum84271bb2002-05-30 15:41:56 +0000714 rl_completion_append_character ='\0';
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000715#endif
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000716
717 begidx = PyInt_FromLong(0L);
718 endidx = PyInt_FromLong(0L);
Barry Warsawf7612871999-01-29 21:55:03 +0000719 /* Initialize (allows .inputrc to override)
720 *
721 * XXX: A bug in the readline-2.2 library causes a memory leak
722 * inside this function. Nothing we can do about it.
723 */
Guido van Rossum290900a1997-09-26 21:51:21 +0000724 rl_initialize();
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000725
726#ifdef SAVE_LOCALE
727 setlocale(LC_CTYPE, saved_locale); /* Restore locale */
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000728 free(saved_locale);
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000729#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000730}
731
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000732/* Wrapper around GNU readline that handles signals differently. */
733
734
735#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
736
737static char *completed_input_string;
738static void
739rlhandler(char *text)
740{
741 completed_input_string = text;
742 rl_callback_handler_remove();
743}
744
745extern PyThreadState* _PyOS_ReadlineTState;
746
747static char *
748readline_until_enter_or_signal(char *prompt, int *signal)
749{
750 char * not_done_reading = "";
751 fd_set selectset;
752
753 *signal = 0;
754#ifdef HAVE_RL_CATCH_SIGNAL
755 rl_catch_signals = 0;
756#endif
757
758 rl_callback_handler_install (prompt, rlhandler);
759 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000760
761 completed_input_string = not_done_reading;
762
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000763 while (completed_input_string == not_done_reading) {
764 int has_input = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000765
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000766 while (!has_input)
767 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
768 FD_SET(fileno(rl_instream), &selectset);
769 /* select resets selectset if no input was available */
770 has_input = select(fileno(rl_instream) + 1, &selectset,
771 NULL, NULL, &timeout);
772 if(PyOS_InputHook) PyOS_InputHook();
773 }
774
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000775 if(has_input > 0) {
776 rl_callback_read_char();
777 }
778 else if (errno == EINTR) {
779 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000780#ifdef WITH_THREAD
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000781 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000782#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000783 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000784#ifdef WITH_THREAD
Michael W. Hudson23849902004-07-08 15:28:26 +0000785 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000786#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000787 if (s < 0) {
788 rl_free_line_state();
789 rl_cleanup_after_signal();
790 rl_callback_handler_remove();
791 *signal = 1;
792 completed_input_string = NULL;
793 }
794 }
795 }
796
797 return completed_input_string;
798}
799
800
801#else
Guido van Rossum290900a1997-09-26 21:51:21 +0000802
803/* Interrupt handler */
804
805static jmp_buf jbuf;
806
Guido van Rossum0969d361997-08-05 21:27:50 +0000807/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +0000808static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000809onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000810{
Guido van Rossum290900a1997-09-26 21:51:21 +0000811 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000812}
813
Guido van Rossum290900a1997-09-26 21:51:21 +0000814
Guido van Rossum0969d361997-08-05 21:27:50 +0000815static char *
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000816readline_until_enter_or_signal(char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +0000817{
Guido van Rossum174efc92000-09-16 16:37:53 +0000818 PyOS_sighandler_t old_inthandler;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000819 char *p;
820
821 *signal = 0;
Guido van Rossum74f31432003-01-07 20:01:29 +0000822
Guido van Rossum174efc92000-09-16 16:37:53 +0000823 old_inthandler = PyOS_setsig(SIGINT, onintr);
Guido van Rossum0969d361997-08-05 21:27:50 +0000824 if (setjmp(jbuf)) {
825#ifdef HAVE_SIGRELSE
826 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
827 sigrelse(SIGINT);
828#endif
Guido van Rossum174efc92000-09-16 16:37:53 +0000829 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000830 *signal = 1;
Guido van Rossum0969d361997-08-05 21:27:50 +0000831 return NULL;
832 }
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000833 rl_event_hook = PyOS_InputHook;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000834 p = readline(prompt);
835 PyOS_setsig(SIGINT, old_inthandler);
836
837 return p;
838}
839#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
840
841
842static char *
843call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
844{
Neal Norwitz1fa040b2004-08-25 01:20:18 +0000845 size_t n;
846 char *p, *q;
847 int signal;
848
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000849#ifdef SAVE_LOCALE
850 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwis701abe72004-08-20 06:26:59 +0000851 if (!saved_locale)
852 Py_FatalError("not enough memory to save locale");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000853 setlocale(LC_CTYPE, "");
854#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000855
Guido van Rossum74f31432003-01-07 20:01:29 +0000856 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
857 rl_instream = sys_stdin;
858 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000859#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum74f31432003-01-07 20:01:29 +0000860 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000861#endif
Guido van Rossum74f31432003-01-07 20:01:29 +0000862 }
863
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000864 p = readline_until_enter_or_signal(prompt, &signal);
865
866 /* we got an interrupt signal */
867 if(signal) {
868 return NULL;
869 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000870
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000871 /* We got an EOF, return a empty string. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000872 if (p == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000873 p = PyMem_Malloc(1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000874 if (p != NULL)
875 *p = '\0';
876 return p;
877 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000878
879 /* we have a valid line */
Guido van Rossum0969d361997-08-05 21:27:50 +0000880 n = strlen(p);
Skip Montanaroa0392742002-06-11 14:32:46 +0000881 if (n > 0) {
882 char *line;
883 HISTORY_STATE *state = history_get_history_state();
884 if (state->length > 0)
885 line = history_get(state->length)->line;
886 else
887 line = "";
888 if (strcmp(p, line))
889 add_history(p);
890 /* the history docs don't say so, but the address of state
891 changes each time history_get_history_state is called
892 which makes me think it's freshly malloc'd memory...
893 on the other hand, the address of the last line stays the
894 same as long as history isn't extended, so it appears to
895 be malloc'd but managed by the history package... */
896 free(state);
897 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000898 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
899 release the original. */
900 q = p;
901 p = PyMem_Malloc(n+2);
902 if (p != NULL) {
903 strncpy(p, q, n);
Guido van Rossum0969d361997-08-05 21:27:50 +0000904 p[n] = '\n';
905 p[n+1] = '\0';
906 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000907 free(q);
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000908#ifdef SAVE_LOCALE
909 setlocale(LC_CTYPE, saved_locale); /* Restore locale */
910 free(saved_locale);
911#endif
Guido van Rossum0969d361997-08-05 21:27:50 +0000912 return p;
913}
914
Guido van Rossum290900a1997-09-26 21:51:21 +0000915
916/* Initialize the module */
917
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000918PyDoc_STRVAR(doc_module,
919"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000920
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000921PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000922initreadline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000923{
Guido van Rossum1ea64ea2000-10-02 15:53:08 +0000924 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +0000925
926 m = Py_InitModule4("readline", readline_methods, doc_module,
927 (PyObject *)NULL, PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000928 if (m == NULL)
929 return;
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000930
Guido van Rossum74f31432003-01-07 20:01:29 +0000931 PyOS_ReadlineFunctionPointer = call_readline;
932 setup_readline();
Guido van Rossum0969d361997-08-05 21:27:50 +0000933}