blob: 706eb7a10e753933f358fa3e2010e505c0357844 [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
3 * Center. The completer interface was inspired by Lele Gaifax.
4 *
5 * More recently, it was largely rewritten by Guido van Rossum who is
6 * now maintaining it.
Guido van Rossum0969d361997-08-05 21:27:50 +00007 */
8
Guido van Rossum290900a1997-09-26 21:51:21 +00009/* Standard definitions */
Guido van Rossum0969d361997-08-05 21:27:50 +000010#include "Python.h"
11#include <setjmp.h>
12#include <signal.h>
Guido van Rossum290900a1997-09-26 21:51:21 +000013#include <errno.h>
Michael W. Hudson8da2b012004-10-07 13:46:33 +000014#include <sys/time.h>
Guido van Rossum0969d361997-08-05 21:27:50 +000015
Skip Montanaro7befb992004-02-10 16:50:21 +000016#if defined(HAVE_SETLOCALE)
Guido van Rossum60c8a3a2002-10-09 21:27:33 +000017/* GNU readline() mistakenly sets the LC_CTYPE locale.
18 * This is evil. Only the user or the app's main() should do this!
19 * We must save and restore the locale around the rl_initialize() call.
20 */
21#define SAVE_LOCALE
22#include <locale.h>
23#endif
24
Guido van Rossum290900a1997-09-26 21:51:21 +000025/* GNU readline definitions */
Guido van Rossumb0e51b22001-04-13 18:14:27 +000026#undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
Guido van Rossumbcc20741998-08-04 22:53:56 +000027#include <readline/readline.h>
28#include <readline/history.h>
Guido van Rossum730806d1998-04-10 22:27:42 +000029
Guido van Rossum353ae582001-07-10 16:45:32 +000030#ifdef HAVE_RL_COMPLETION_MATCHES
Guido van Rossum74f31432003-01-07 20:01:29 +000031#define completion_matches(x, y) \
32 rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
Guido van Rossum353ae582001-07-10 16:45:32 +000033#endif
34
Guido van Rossum0969d361997-08-05 21:27:50 +000035
Guido van Rossum290900a1997-09-26 21:51:21 +000036/* Exported function to send one line to readline's init file parser */
37
38static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000039parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000040{
Guido van Rossum3b5330e1998-12-04 15:34:39 +000041 char *s, *copy;
Guido van Rossum43713e52000-02-29 13:59:29 +000042 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000043 return NULL;
Guido van Rossum3b5330e1998-12-04 15:34:39 +000044 /* Make a copy -- rl_parse_and_bind() modifies its argument */
45 /* Bernard Herzog */
46 copy = malloc(1 + strlen(s));
47 if (copy == NULL)
48 return PyErr_NoMemory();
49 strcpy(copy, s);
50 rl_parse_and_bind(copy);
51 free(copy); /* Free the copy */
Guido van Rossum290900a1997-09-26 21:51:21 +000052 Py_INCREF(Py_None);
53 return Py_None;
54}
55
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000056PyDoc_STRVAR(doc_parse_and_bind,
57"parse_and_bind(string) -> None\n\
58Parse and execute single line of a readline init file.");
Guido van Rossum290900a1997-09-26 21:51:21 +000059
60
61/* Exported function to parse a readline init file */
62
63static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000064read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000065{
66 char *s = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +000067 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000068 return NULL;
69 errno = rl_read_init_file(s);
70 if (errno)
71 return PyErr_SetFromErrno(PyExc_IOError);
72 Py_INCREF(Py_None);
73 return Py_None;
74}
75
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000076PyDoc_STRVAR(doc_read_init_file,
77"read_init_file([filename]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +000078Parse a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000079The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +000080
81
Skip Montanaro28067822000-07-06 18:55:12 +000082/* Exported function to load a readline history file */
83
84static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000085read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +000086{
87 char *s = NULL;
88 if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
89 return NULL;
90 errno = read_history(s);
91 if (errno)
92 return PyErr_SetFromErrno(PyExc_IOError);
93 Py_INCREF(Py_None);
94 return Py_None;
95}
96
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +000097static int _history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000098PyDoc_STRVAR(doc_read_history_file,
99"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000100Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000101The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000102
103
104/* Exported function to save a readline history file */
105
106static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000107write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000108{
109 char *s = NULL;
110 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
111 return NULL;
112 errno = write_history(s);
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000113 if (!errno && _history_length >= 0)
114 history_truncate_file(s, _history_length);
Skip Montanaro28067822000-07-06 18:55:12 +0000115 if (errno)
116 return PyErr_SetFromErrno(PyExc_IOError);
117 Py_INCREF(Py_None);
118 return Py_None;
119}
120
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000121PyDoc_STRVAR(doc_write_history_file,
122"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000123Save a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000124The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000125
126
Guido van Rossum74f31432003-01-07 20:01:29 +0000127/* Set history length */
128
129static PyObject*
130set_history_length(PyObject *self, PyObject *args)
131{
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000132 int length = _history_length;
Guido van Rossum74f31432003-01-07 20:01:29 +0000133 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
134 return NULL;
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000135 _history_length = length;
Guido van Rossum74f31432003-01-07 20:01:29 +0000136 Py_INCREF(Py_None);
137 return Py_None;
138}
139
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000140PyDoc_STRVAR(set_history_length_doc,
141"set_history_length(length) -> None\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000142set the maximal number of items which will be written to\n\
143the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000144history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000145
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000146
Guido van Rossum74f31432003-01-07 20:01:29 +0000147/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000148
149static PyObject*
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000150get_history_length(PyObject *self, PyObject *noarg)
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000151{
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000152 return PyInt_FromLong(_history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000153}
154
Guido van Rossum74f31432003-01-07 20:01:29 +0000155PyDoc_STRVAR(get_history_length_doc,
156"get_history_length() -> int\n\
157return the maximum number of items that will be written to\n\
158the history file.");
159
160
Martin v. Löwis0daad592001-09-30 21:09:59 +0000161/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000162
Martin v. Löwis0daad592001-09-30 21:09:59 +0000163static PyObject *
Guido van Rossum74f31432003-01-07 20:01:29 +0000164set_hook(const char *funcname, PyObject **hook_var,
165 PyThreadState **tstate, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000166{
167 PyObject *function = Py_None;
168 char buf[80];
Tim Peters885d4572001-11-28 20:27:42 +0000169 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000170 if (!PyArg_ParseTuple(args, buf, &function))
171 return NULL;
172 if (function == Py_None) {
173 Py_XDECREF(*hook_var);
174 *hook_var = NULL;
175 *tstate = NULL;
176 }
177 else if (PyCallable_Check(function)) {
178 PyObject *tmp = *hook_var;
179 Py_INCREF(function);
180 *hook_var = function;
181 Py_XDECREF(tmp);
Nicholas Bastin2786d902004-03-25 02:16:23 +0000182 *tstate = PyThreadState_GET();
Martin v. Löwis0daad592001-09-30 21:09:59 +0000183 }
184 else {
Tim Peters885d4572001-11-28 20:27:42 +0000185 PyOS_snprintf(buf, sizeof(buf),
186 "set_%.50s(func): argument not callable",
187 funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000188 PyErr_SetString(PyExc_TypeError, buf);
189 return NULL;
190 }
191 Py_INCREF(Py_None);
192 return Py_None;
193}
194
Guido van Rossum74f31432003-01-07 20:01:29 +0000195
Martin v. Löwis0daad592001-09-30 21:09:59 +0000196/* Exported functions to specify hook functions in Python */
197
198static PyObject *startup_hook = NULL;
199static PyThreadState *startup_hook_tstate = NULL;
200
201#ifdef HAVE_RL_PRE_INPUT_HOOK
202static PyObject *pre_input_hook = NULL;
203static PyThreadState *pre_input_hook_tstate = NULL;
204#endif
205
206static PyObject *
207set_startup_hook(PyObject *self, PyObject *args)
208{
Guido van Rossum74f31432003-01-07 20:01:29 +0000209 return set_hook("startup_hook", &startup_hook,
210 &startup_hook_tstate, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000211}
212
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000213PyDoc_STRVAR(doc_set_startup_hook,
214"set_startup_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000215Set or remove the startup_hook function.\n\
216The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000217before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000218
Guido van Rossum74f31432003-01-07 20:01:29 +0000219
Martin v. Löwis0daad592001-09-30 21:09:59 +0000220#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000221
222/* Set pre-input hook */
223
Martin v. Löwis0daad592001-09-30 21:09:59 +0000224static PyObject *
225set_pre_input_hook(PyObject *self, PyObject *args)
226{
Guido van Rossum74f31432003-01-07 20:01:29 +0000227 return set_hook("pre_input_hook", &pre_input_hook,
228 &pre_input_hook_tstate, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000229}
230
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000231PyDoc_STRVAR(doc_set_pre_input_hook,
232"set_pre_input_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000233Set or remove the pre_input_hook function.\n\
234The function is called with no arguments after the first prompt\n\
235has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000236characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000237
Martin v. Löwis0daad592001-09-30 21:09:59 +0000238#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000239
Guido van Rossum74f31432003-01-07 20:01:29 +0000240
Guido van Rossum290900a1997-09-26 21:51:21 +0000241/* Exported function to specify a word completer in Python */
242
243static PyObject *completer = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000244static PyThreadState *completer_tstate = NULL;
Guido van Rossum290900a1997-09-26 21:51:21 +0000245
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000246static PyObject *begidx = NULL;
247static PyObject *endidx = NULL;
248
Guido van Rossum74f31432003-01-07 20:01:29 +0000249
250/* Get the beginning index for the scope of the tab-completion */
251
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000252static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000253get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000254{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000255 Py_INCREF(begidx);
256 return begidx;
257}
258
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000259PyDoc_STRVAR(doc_get_begidx,
260"get_begidx() -> int\n\
261get the beginning index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000262
Guido van Rossum74f31432003-01-07 20:01:29 +0000263
264/* Get the ending index for the scope of the tab-completion */
265
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000266static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000267get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000268{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000269 Py_INCREF(endidx);
270 return endidx;
271}
272
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000273PyDoc_STRVAR(doc_get_endidx,
274"get_endidx() -> int\n\
275get the ending index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000276
277
Guido van Rossum74f31432003-01-07 20:01:29 +0000278/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000279
280static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000281set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000282{
283 char *break_chars;
284
Guido van Rossum43713e52000-02-29 13:59:29 +0000285 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000286 return NULL;
287 }
Neal Norwitz0e0ee592002-04-21 15:03:18 +0000288 free((void*)rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000289 rl_completer_word_break_characters = strdup(break_chars);
290 Py_INCREF(Py_None);
291 return Py_None;
292}
293
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000294PyDoc_STRVAR(doc_set_completer_delims,
295"set_completer_delims(string) -> None\n\
296set the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000297
Skip Montanaroe5069012004-08-15 14:32:06 +0000298static PyObject *
299py_remove_history(PyObject *self, PyObject *args)
300{
301 int entry_number;
302 HIST_ENTRY *entry;
303
304 if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
305 return NULL;
Martin v. Löwis9533e342005-02-27 20:33:25 +0000306 if (entry_number < 0) {
307 PyErr_SetString(PyExc_ValueError,
308 "History index cannot be negative");
309 return NULL;
310 }
Skip Montanaroe5069012004-08-15 14:32:06 +0000311 entry = remove_history(entry_number);
312 if (!entry) {
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000313 PyErr_Format(PyExc_ValueError,
314 "No history item at position %d",
315 entry_number);
Skip Montanaroe5069012004-08-15 14:32:06 +0000316 return NULL;
317 }
318 /* free memory allocated for the history entry */
319 if (entry->line)
320 free(entry->line);
321 if (entry->data)
322 free(entry->data);
323 free(entry);
324
325 Py_INCREF(Py_None);
326 return Py_None;
327}
328
329PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000330"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000331remove history item given by its position");
332
333static PyObject *
334py_replace_history(PyObject *self, PyObject *args)
335{
336 int entry_number;
337 char *line;
338 HIST_ENTRY *old_entry;
339
340 if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number, &line)) {
341 return NULL;
342 }
Martin v. Löwis9533e342005-02-27 20:33:25 +0000343 if (entry_number < 0) {
344 PyErr_SetString(PyExc_ValueError,
345 "History index cannot be negative");
346 return NULL;
347 }
Skip Montanaroe5069012004-08-15 14:32:06 +0000348 old_entry = replace_history_entry(entry_number, line, (void *)NULL);
349 if (!old_entry) {
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000350 PyErr_Format(PyExc_ValueError,
351 "No history item at position %d",
352 entry_number);
Skip Montanaroe5069012004-08-15 14:32:06 +0000353 return NULL;
354 }
355 /* free memory allocated for the old history entry */
356 if (old_entry->line)
357 free(old_entry->line);
358 if (old_entry->data)
359 free(old_entry->data);
360 free(old_entry);
361
362 Py_INCREF(Py_None);
363 return Py_None;
364}
365
366PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000367"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000368replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000369
370/* Add a line to the history buffer */
371
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000372static PyObject *
373py_add_history(PyObject *self, PyObject *args)
374{
375 char *line;
376
377 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
378 return NULL;
379 }
380 add_history(line);
381 Py_INCREF(Py_None);
382 return Py_None;
383}
384
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000385PyDoc_STRVAR(doc_add_history,
386"add_history(string) -> None\n\
387add a line to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000388
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000389
Guido van Rossum74f31432003-01-07 20:01:29 +0000390/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000391
392static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000393get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000394{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000395 return PyString_FromString(rl_completer_word_break_characters);
396}
Guido van Rossum74f31432003-01-07 20:01:29 +0000397
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000398PyDoc_STRVAR(doc_get_completer_delims,
399"get_completer_delims() -> string\n\
400get the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000401
Guido van Rossum74f31432003-01-07 20:01:29 +0000402
403/* Set the completer function */
404
Guido van Rossum290900a1997-09-26 21:51:21 +0000405static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000406set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000407{
Martin v. Löwis0daad592001-09-30 21:09:59 +0000408 return set_hook("completer", &completer, &completer_tstate, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000409}
410
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000411PyDoc_STRVAR(doc_set_completer,
412"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000413Set or remove the completer function.\n\
414The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000415for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000416It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000417
Guido van Rossum74f31432003-01-07 20:01:29 +0000418
Michael W. Hudson796df152003-01-30 10:12:51 +0000419static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000420get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000421{
422 if (completer == NULL) {
423 Py_INCREF(Py_None);
424 return Py_None;
425 }
426 Py_INCREF(completer);
427 return completer;
428}
429
430PyDoc_STRVAR(doc_get_completer,
431"get_completer() -> function\n\
432\n\
433Returns current completer function.");
434
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000435/* Exported function to get any element of history */
436
437static PyObject *
438get_history_item(PyObject *self, PyObject *args)
439{
440 int idx = 0;
441 HIST_ENTRY *hist_ent;
442
443 if (!PyArg_ParseTuple(args, "i:index", &idx))
444 return NULL;
445 if ((hist_ent = history_get(idx)))
Guido van Rossum05ac4492003-01-07 20:04:12 +0000446 return PyString_FromString(hist_ent->line);
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000447 else {
448 Py_INCREF(Py_None);
449 return Py_None;
450 }
451}
452
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000453PyDoc_STRVAR(doc_get_history_item,
454"get_history_item() -> string\n\
455return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000456
Guido van Rossum74f31432003-01-07 20:01:29 +0000457
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000458/* Exported function to get current length of history */
459
460static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000461get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000462{
463 HISTORY_STATE *hist_st;
464
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000465 hist_st = history_get_history_state();
466 return PyInt_FromLong(hist_st ? (long) hist_st->length : (long) 0);
467}
468
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000469PyDoc_STRVAR(doc_get_current_history_length,
470"get_current_history_length() -> integer\n\
471return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000472
Guido van Rossum74f31432003-01-07 20:01:29 +0000473
Guido van Rossum79378ff1997-10-07 14:53:21 +0000474/* Exported function to read the current line buffer */
475
476static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000477get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000478{
Guido van Rossum79378ff1997-10-07 14:53:21 +0000479 return PyString_FromString(rl_line_buffer);
480}
481
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000482PyDoc_STRVAR(doc_get_line_buffer,
483"get_line_buffer() -> string\n\
484return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000485
Guido van Rossum74f31432003-01-07 20:01:29 +0000486
Martin v. Löwise7a97962003-09-20 16:08:33 +0000487#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
488
489/* Exported function to clear the current history */
490
491static PyObject *
492py_clear_history(PyObject *self, PyObject *noarg)
493{
494 clear_history();
495 Py_INCREF(Py_None);
496 return Py_None;
497}
498
499PyDoc_STRVAR(doc_clear_history,
500"clear_history() -> None\n\
501Clear the current readline history.");
502#endif
503
504
Guido van Rossum79378ff1997-10-07 14:53:21 +0000505/* Exported function to insert text into the line buffer */
506
507static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000508insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000509{
510 char *s;
Guido van Rossum43713e52000-02-29 13:59:29 +0000511 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000512 return NULL;
513 rl_insert_text(s);
514 Py_INCREF(Py_None);
515 return Py_None;
516}
517
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000518PyDoc_STRVAR(doc_insert_text,
519"insert_text(string) -> None\n\
520Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000521
Guido van Rossum74f31432003-01-07 20:01:29 +0000522
523/* Redisplay the line buffer */
524
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000525static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000526redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000527{
528 rl_redisplay();
529 Py_INCREF(Py_None);
530 return Py_None;
531}
532
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000533PyDoc_STRVAR(doc_redisplay,
534"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000535Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000536contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000537
Guido van Rossum74f31432003-01-07 20:01:29 +0000538
Guido van Rossum290900a1997-09-26 21:51:21 +0000539/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000540
541static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000542{
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000543 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000544 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000545 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000546 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000547 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000548 {"read_history_file", read_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000549 METH_VARARGS, doc_read_history_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000550 {"write_history_file", write_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000551 METH_VARARGS, doc_write_history_file},
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000552 {"get_history_item", get_history_item,
553 METH_VARARGS, doc_get_history_item},
Neal Norwitz767f8352002-03-31 16:13:39 +0000554 {"get_current_history_length", (PyCFunction)get_current_history_length,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000555 METH_NOARGS, doc_get_current_history_length},
Guido van Rossum74f31432003-01-07 20:01:29 +0000556 {"set_history_length", set_history_length,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000557 METH_VARARGS, set_history_length_doc},
Guido van Rossum74f31432003-01-07 20:01:29 +0000558 {"get_history_length", get_history_length,
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000559 METH_NOARGS, get_history_length_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000560 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
Michael W. Hudson796df152003-01-30 10:12:51 +0000561 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000562 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
563 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000564
Guido van Rossum74f31432003-01-07 20:01:29 +0000565 {"set_completer_delims", set_completer_delims,
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000566 METH_VARARGS, doc_set_completer_delims},
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000567 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
Skip Montanaroe5069012004-08-15 14:32:06 +0000568 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
569 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000570 {"get_completer_delims", get_completer_delims,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000571 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000572
573 {"set_startup_hook", set_startup_hook,
574 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000575#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000576 {"set_pre_input_hook", set_pre_input_hook,
577 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000578#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000579#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
580 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
581#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000582 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000583};
584
Guido van Rossum05ac4492003-01-07 20:04:12 +0000585
Martin v. Löwis0daad592001-09-30 21:09:59 +0000586/* C function to call the Python hooks. */
587
588static int
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000589on_hook(PyObject *func, PyThreadState **tstate)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000590{
591 int result = 0;
592 if (func != NULL) {
593 PyObject *r;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000594 /* Note that readline is called with the interpreter
595 lock released! */
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000596 PyEval_RestoreThread(*tstate);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000597 r = PyObject_CallFunction(func, NULL);
598 if (r == NULL)
599 goto error;
Guido van Rossum74f31432003-01-07 20:01:29 +0000600 if (r == Py_None)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000601 result = 0;
Guido van Rossum74f31432003-01-07 20:01:29 +0000602 else
Martin v. Löwis0daad592001-09-30 21:09:59 +0000603 result = PyInt_AsLong(r);
604 Py_DECREF(r);
605 goto done;
606 error:
607 PyErr_Clear();
608 Py_XDECREF(r);
609 done:
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000610 *tstate = PyEval_SaveThread();
Martin v. Löwis0daad592001-09-30 21:09:59 +0000611 }
612 return result;
613}
614
615static int
616on_startup_hook(void)
617{
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000618 return on_hook(startup_hook, &startup_hook_tstate);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000619}
620
621#ifdef HAVE_RL_PRE_INPUT_HOOK
622static int
623on_pre_input_hook(void)
624{
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000625 return on_hook(pre_input_hook, &pre_input_hook_tstate);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000626}
627#endif
628
Guido van Rossum05ac4492003-01-07 20:04:12 +0000629
Guido van Rossum290900a1997-09-26 21:51:21 +0000630/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000631
Guido van Rossum290900a1997-09-26 21:51:21 +0000632static char *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000633on_completion(char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000634{
Guido van Rossum290900a1997-09-26 21:51:21 +0000635 char *result = NULL;
636 if (completer != NULL) {
637 PyObject *r;
638 /* Note that readline is called with the interpreter
639 lock released! */
Martin v. Löwis0daad592001-09-30 21:09:59 +0000640 PyEval_RestoreThread(completer_tstate);
Michael W. Hudson0c1ceaf2002-02-13 11:58:25 +0000641 /* Don't use the default filename completion if we
642 * have a custom completion function... */
643 rl_attempted_completion_over = 1;
Guido van Rossum290900a1997-09-26 21:51:21 +0000644 r = PyObject_CallFunction(completer, "si", text, state);
645 if (r == NULL)
646 goto error;
647 if (r == Py_None) {
648 result = NULL;
649 }
650 else {
651 char *s = PyString_AsString(r);
652 if (s == NULL)
653 goto error;
654 result = strdup(s);
655 }
656 Py_DECREF(r);
657 goto done;
658 error:
659 PyErr_Clear();
660 Py_XDECREF(r);
661 done:
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000662 completer_tstate = PyEval_SaveThread();
Guido van Rossum290900a1997-09-26 21:51:21 +0000663 }
664 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000665}
666
Guido van Rossum290900a1997-09-26 21:51:21 +0000667
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000668/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000669 * before calling the normal completer */
670
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000671static char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000672flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000673{
674 Py_XDECREF(begidx);
675 Py_XDECREF(endidx);
676 begidx = PyInt_FromLong((long) start);
677 endidx = PyInt_FromLong((long) end);
678 return completion_matches(text, *on_completion);
679}
680
Guido van Rossum05ac4492003-01-07 20:04:12 +0000681
Guido van Rossum290900a1997-09-26 21:51:21 +0000682/* Helper to initialize GNU readline properly. */
683
684static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000685setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000686{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000687#ifdef SAVE_LOCALE
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000688 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwis701abe72004-08-20 06:26:59 +0000689 if (!saved_locale)
690 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000691#endif
692
Skip Montanaroa0392742002-06-11 14:32:46 +0000693 using_history();
694
Guido van Rossum290900a1997-09-26 21:51:21 +0000695 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000696#if defined(PYOS_OS2) && defined(PYCC_GCC)
697 /* Allow $if term= in .inputrc to work */
698 rl_terminal_name = getenv("TERM");
699#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000700 /* Force rebind of TAB to insert-tab */
701 rl_bind_key('\t', rl_insert);
702 /* Bind both ESC-TAB and ESC-ESC to the completion function */
703 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
704 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000705 /* Set our hook functions */
706 rl_startup_hook = (Function *)on_startup_hook;
707#ifdef HAVE_RL_PRE_INPUT_HOOK
708 rl_pre_input_hook = (Function *)on_pre_input_hook;
709#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000710 /* Set our completion function */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000711 rl_attempted_completion_function = (CPPFunction *)flex_complete;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000712 /* Set Python word break characters */
713 rl_completer_word_break_characters =
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000714 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000715 /* All nonalphanums except '.' */
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000716#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum84271bb2002-05-30 15:41:56 +0000717 rl_completion_append_character ='\0';
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000718#endif
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000719
720 begidx = PyInt_FromLong(0L);
721 endidx = PyInt_FromLong(0L);
Barry Warsawf7612871999-01-29 21:55:03 +0000722 /* Initialize (allows .inputrc to override)
723 *
724 * XXX: A bug in the readline-2.2 library causes a memory leak
725 * inside this function. Nothing we can do about it.
726 */
Guido van Rossum290900a1997-09-26 21:51:21 +0000727 rl_initialize();
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000728
729#ifdef SAVE_LOCALE
730 setlocale(LC_CTYPE, saved_locale); /* Restore locale */
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000731 free(saved_locale);
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000732#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000733}
734
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000735/* Wrapper around GNU readline that handles signals differently. */
736
737
738#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
739
740static char *completed_input_string;
741static void
742rlhandler(char *text)
743{
744 completed_input_string = text;
745 rl_callback_handler_remove();
746}
747
748extern PyThreadState* _PyOS_ReadlineTState;
749
750static char *
751readline_until_enter_or_signal(char *prompt, int *signal)
752{
753 char * not_done_reading = "";
754 fd_set selectset;
755
756 *signal = 0;
757#ifdef HAVE_RL_CATCH_SIGNAL
758 rl_catch_signals = 0;
759#endif
760
761 rl_callback_handler_install (prompt, rlhandler);
762 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000763
764 completed_input_string = not_done_reading;
765
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000766 while (completed_input_string == not_done_reading) {
767 int has_input = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000768
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000769 while (!has_input)
770 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
771 FD_SET(fileno(rl_instream), &selectset);
772 /* select resets selectset if no input was available */
773 has_input = select(fileno(rl_instream) + 1, &selectset,
774 NULL, NULL, &timeout);
775 if(PyOS_InputHook) PyOS_InputHook();
776 }
777
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000778 if(has_input > 0) {
779 rl_callback_read_char();
780 }
781 else if (errno == EINTR) {
782 int s;
783 PyEval_RestoreThread(_PyOS_ReadlineTState);
784 s = PyErr_CheckSignals();
Michael W. Hudson23849902004-07-08 15:28:26 +0000785 PyEval_SaveThread();
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000786 if (s < 0) {
787 rl_free_line_state();
788 rl_cleanup_after_signal();
789 rl_callback_handler_remove();
790 *signal = 1;
791 completed_input_string = NULL;
792 }
793 }
794 }
795
796 return completed_input_string;
797}
798
799
800#else
Guido van Rossum290900a1997-09-26 21:51:21 +0000801
802/* Interrupt handler */
803
804static jmp_buf jbuf;
805
Guido van Rossum0969d361997-08-05 21:27:50 +0000806/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +0000807static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000808onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000809{
Guido van Rossum290900a1997-09-26 21:51:21 +0000810 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000811}
812
Guido van Rossum290900a1997-09-26 21:51:21 +0000813
Guido van Rossum0969d361997-08-05 21:27:50 +0000814static char *
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000815readline_until_enter_or_signal(char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +0000816{
Guido van Rossum174efc92000-09-16 16:37:53 +0000817 PyOS_sighandler_t old_inthandler;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000818 char *p;
819
820 *signal = 0;
Guido van Rossum74f31432003-01-07 20:01:29 +0000821
Guido van Rossum174efc92000-09-16 16:37:53 +0000822 old_inthandler = PyOS_setsig(SIGINT, onintr);
Guido van Rossum0969d361997-08-05 21:27:50 +0000823 if (setjmp(jbuf)) {
824#ifdef HAVE_SIGRELSE
825 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
826 sigrelse(SIGINT);
827#endif
Guido van Rossum174efc92000-09-16 16:37:53 +0000828 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000829 *signal = 1;
Guido van Rossum0969d361997-08-05 21:27:50 +0000830 return NULL;
831 }
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000832 rl_event_hook = PyOS_InputHook;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000833 p = readline(prompt);
834 PyOS_setsig(SIGINT, old_inthandler);
835
836 return p;
837}
838#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
839
840
841static char *
842call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
843{
Neal Norwitz1fa040b2004-08-25 01:20:18 +0000844 size_t n;
845 char *p, *q;
846 int signal;
847
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000848#ifdef SAVE_LOCALE
849 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwis701abe72004-08-20 06:26:59 +0000850 if (!saved_locale)
851 Py_FatalError("not enough memory to save locale");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000852 setlocale(LC_CTYPE, "");
853#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000854
Guido van Rossum74f31432003-01-07 20:01:29 +0000855 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
856 rl_instream = sys_stdin;
857 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000858#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum74f31432003-01-07 20:01:29 +0000859 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000860#endif
Guido van Rossum74f31432003-01-07 20:01:29 +0000861 }
862
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000863 p = readline_until_enter_or_signal(prompt, &signal);
864
865 /* we got an interrupt signal */
866 if(signal) {
867 return NULL;
868 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000869
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000870 /* We got an EOF, return a empty string. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000871 if (p == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000872 p = PyMem_Malloc(1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000873 if (p != NULL)
874 *p = '\0';
875 return p;
876 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000877
878 /* we have a valid line */
Guido van Rossum0969d361997-08-05 21:27:50 +0000879 n = strlen(p);
Skip Montanaroa0392742002-06-11 14:32:46 +0000880 if (n > 0) {
881 char *line;
882 HISTORY_STATE *state = history_get_history_state();
883 if (state->length > 0)
884 line = history_get(state->length)->line;
885 else
886 line = "";
887 if (strcmp(p, line))
888 add_history(p);
889 /* the history docs don't say so, but the address of state
890 changes each time history_get_history_state is called
891 which makes me think it's freshly malloc'd memory...
892 on the other hand, the address of the last line stays the
893 same as long as history isn't extended, so it appears to
894 be malloc'd but managed by the history package... */
895 free(state);
896 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000897 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
898 release the original. */
899 q = p;
900 p = PyMem_Malloc(n+2);
901 if (p != NULL) {
902 strncpy(p, q, n);
Guido van Rossum0969d361997-08-05 21:27:50 +0000903 p[n] = '\n';
904 p[n+1] = '\0';
905 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000906 free(q);
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000907#ifdef SAVE_LOCALE
908 setlocale(LC_CTYPE, saved_locale); /* Restore locale */
909 free(saved_locale);
910#endif
Guido van Rossum0969d361997-08-05 21:27:50 +0000911 return p;
912}
913
Guido van Rossum290900a1997-09-26 21:51:21 +0000914
915/* Initialize the module */
916
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000917PyDoc_STRVAR(doc_module,
918"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000919
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000920PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000921initreadline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000922{
Guido van Rossum1ea64ea2000-10-02 15:53:08 +0000923 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +0000924
925 m = Py_InitModule4("readline", readline_methods, doc_module,
926 (PyObject *)NULL, PYTHON_API_VERSION);
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000927
Guido van Rossum74f31432003-01-07 20:01:29 +0000928 PyOS_ReadlineFunctionPointer = call_readline;
929 setup_readline();
Guido van Rossum0969d361997-08-05 21:27:50 +0000930}