blob: 853874be2506e78b65bcab6a970542a2d9dc80ad [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
Neal Norwitz5eaf7722006-07-16 02:15:27 +000023#ifdef SAVE_LOCALE
24# define RESTORE_LOCALE(sl) { setlocale(LC_CTYPE, sl); free(sl); }
25#else
26# define RESTORE_LOCALE(sl)
27#endif
28
Guido van Rossum290900a1997-09-26 21:51:21 +000029/* GNU readline definitions */
Guido van Rossumb0e51b22001-04-13 18:14:27 +000030#undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
Guido van Rossumbcc20741998-08-04 22:53:56 +000031#include <readline/readline.h>
32#include <readline/history.h>
Guido van Rossum730806d1998-04-10 22:27:42 +000033
Guido van Rossum353ae582001-07-10 16:45:32 +000034#ifdef HAVE_RL_COMPLETION_MATCHES
Guido van Rossum74f31432003-01-07 20:01:29 +000035#define completion_matches(x, y) \
36 rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
Guido van Rossum353ae582001-07-10 16:45:32 +000037#endif
38
Guido van Rossum0969d361997-08-05 21:27:50 +000039
Guido van Rossum290900a1997-09-26 21:51:21 +000040/* Exported function to send one line to readline's init file parser */
41
42static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000043parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000044{
Guido van Rossum3b5330e1998-12-04 15:34:39 +000045 char *s, *copy;
Guido van Rossum43713e52000-02-29 13:59:29 +000046 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000047 return NULL;
Guido van Rossum3b5330e1998-12-04 15:34:39 +000048 /* Make a copy -- rl_parse_and_bind() modifies its argument */
49 /* Bernard Herzog */
50 copy = malloc(1 + strlen(s));
51 if (copy == NULL)
52 return PyErr_NoMemory();
53 strcpy(copy, s);
54 rl_parse_and_bind(copy);
55 free(copy); /* Free the copy */
Guido van Rossum290900a1997-09-26 21:51:21 +000056 Py_INCREF(Py_None);
57 return Py_None;
58}
59
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000060PyDoc_STRVAR(doc_parse_and_bind,
61"parse_and_bind(string) -> None\n\
62Parse and execute single line of a readline init file.");
Guido van Rossum290900a1997-09-26 21:51:21 +000063
64
65/* Exported function to parse a readline init file */
66
67static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000068read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000069{
70 char *s = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +000071 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000072 return NULL;
73 errno = rl_read_init_file(s);
74 if (errno)
75 return PyErr_SetFromErrno(PyExc_IOError);
76 Py_INCREF(Py_None);
77 return Py_None;
78}
79
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000080PyDoc_STRVAR(doc_read_init_file,
81"read_init_file([filename]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +000082Parse a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000083The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +000084
85
Skip Montanaro28067822000-07-06 18:55:12 +000086/* Exported function to load a readline history file */
87
88static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000089read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +000090{
91 char *s = NULL;
92 if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
93 return NULL;
94 errno = read_history(s);
95 if (errno)
96 return PyErr_SetFromErrno(PyExc_IOError);
97 Py_INCREF(Py_None);
98 return Py_None;
99}
100
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000101static int _history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000102PyDoc_STRVAR(doc_read_history_file,
103"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000104Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000105The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000106
107
108/* Exported function to save a readline history file */
109
110static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000111write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000112{
113 char *s = NULL;
114 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
115 return NULL;
116 errno = write_history(s);
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000117 if (!errno && _history_length >= 0)
118 history_truncate_file(s, _history_length);
Skip Montanaro28067822000-07-06 18:55:12 +0000119 if (errno)
120 return PyErr_SetFromErrno(PyExc_IOError);
121 Py_INCREF(Py_None);
122 return Py_None;
123}
124
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000125PyDoc_STRVAR(doc_write_history_file,
126"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000127Save a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000128The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000129
130
Guido van Rossum74f31432003-01-07 20:01:29 +0000131/* Set history length */
132
133static PyObject*
134set_history_length(PyObject *self, PyObject *args)
135{
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000136 int length = _history_length;
Guido van Rossum74f31432003-01-07 20:01:29 +0000137 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
138 return NULL;
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000139 _history_length = length;
Guido van Rossum74f31432003-01-07 20:01:29 +0000140 Py_INCREF(Py_None);
141 return Py_None;
142}
143
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000144PyDoc_STRVAR(set_history_length_doc,
145"set_history_length(length) -> None\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000146set the maximal number of items which will be written to\n\
147the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000148history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000149
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000150
Guido van Rossum74f31432003-01-07 20:01:29 +0000151/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000152
153static PyObject*
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000154get_history_length(PyObject *self, PyObject *noarg)
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000155{
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000156 return PyInt_FromLong(_history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000157}
158
Guido van Rossum74f31432003-01-07 20:01:29 +0000159PyDoc_STRVAR(get_history_length_doc,
160"get_history_length() -> int\n\
161return the maximum number of items that will be written to\n\
162the history file.");
163
164
Martin v. Löwis0daad592001-09-30 21:09:59 +0000165/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000166
Martin v. Löwis0daad592001-09-30 21:09:59 +0000167static PyObject *
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000168set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000169{
170 PyObject *function = Py_None;
171 char buf[80];
Tim Peters885d4572001-11-28 20:27:42 +0000172 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000173 if (!PyArg_ParseTuple(args, buf, &function))
174 return NULL;
175 if (function == Py_None) {
176 Py_XDECREF(*hook_var);
177 *hook_var = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000178 }
179 else if (PyCallable_Check(function)) {
180 PyObject *tmp = *hook_var;
181 Py_INCREF(function);
182 *hook_var = function;
183 Py_XDECREF(tmp);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000184 }
185 else {
Tim Peters885d4572001-11-28 20:27:42 +0000186 PyOS_snprintf(buf, sizeof(buf),
187 "set_%.50s(func): argument not callable",
188 funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000189 PyErr_SetString(PyExc_TypeError, buf);
190 return NULL;
191 }
192 Py_INCREF(Py_None);
193 return Py_None;
194}
195
Guido van Rossum74f31432003-01-07 20:01:29 +0000196
Martin v. Löwis0daad592001-09-30 21:09:59 +0000197/* Exported functions to specify hook functions in Python */
198
199static PyObject *startup_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000200
201#ifdef HAVE_RL_PRE_INPUT_HOOK
202static PyObject *pre_input_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000203#endif
204
205static PyObject *
206set_startup_hook(PyObject *self, PyObject *args)
207{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000208 return set_hook("startup_hook", &startup_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000209}
210
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000211PyDoc_STRVAR(doc_set_startup_hook,
212"set_startup_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000213Set or remove the startup_hook function.\n\
214The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000215before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000216
Guido van Rossum74f31432003-01-07 20:01:29 +0000217
Martin v. Löwis0daad592001-09-30 21:09:59 +0000218#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000219
220/* Set pre-input hook */
221
Martin v. Löwis0daad592001-09-30 21:09:59 +0000222static PyObject *
223set_pre_input_hook(PyObject *self, PyObject *args)
224{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000225 return set_hook("pre_input_hook", &pre_input_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000226}
227
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000228PyDoc_STRVAR(doc_set_pre_input_hook,
229"set_pre_input_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000230Set or remove the pre_input_hook function.\n\
231The function is called with no arguments after the first prompt\n\
232has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000233characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000234
Martin v. Löwis0daad592001-09-30 21:09:59 +0000235#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000236
Guido van Rossum74f31432003-01-07 20:01:29 +0000237
Guido van Rossum290900a1997-09-26 21:51:21 +0000238/* Exported function to specify a word completer in Python */
239
240static PyObject *completer = NULL;
Guido van Rossum290900a1997-09-26 21:51:21 +0000241
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000242static PyObject *begidx = NULL;
243static PyObject *endidx = NULL;
244
Guido van Rossum74f31432003-01-07 20:01:29 +0000245
246/* Get the beginning index for the scope of the tab-completion */
247
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000248static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000249get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000250{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000251 Py_INCREF(begidx);
252 return begidx;
253}
254
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000255PyDoc_STRVAR(doc_get_begidx,
256"get_begidx() -> int\n\
257get the beginning index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000258
Guido van Rossum74f31432003-01-07 20:01:29 +0000259
260/* Get the ending index for the scope of the tab-completion */
261
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000262static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000263get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000264{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000265 Py_INCREF(endidx);
266 return endidx;
267}
268
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000269PyDoc_STRVAR(doc_get_endidx,
270"get_endidx() -> int\n\
271get the ending index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000272
273
Guido van Rossum74f31432003-01-07 20:01:29 +0000274/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000275
276static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000277set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000278{
279 char *break_chars;
280
Guido van Rossum43713e52000-02-29 13:59:29 +0000281 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000282 return NULL;
283 }
Neal Norwitz0e0ee592002-04-21 15:03:18 +0000284 free((void*)rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000285 rl_completer_word_break_characters = strdup(break_chars);
286 Py_INCREF(Py_None);
287 return Py_None;
288}
289
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000290PyDoc_STRVAR(doc_set_completer_delims,
291"set_completer_delims(string) -> None\n\
292set the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000293
Skip Montanaroe5069012004-08-15 14:32:06 +0000294static PyObject *
295py_remove_history(PyObject *self, PyObject *args)
296{
297 int entry_number;
298 HIST_ENTRY *entry;
299
300 if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
301 return NULL;
Martin v. Löwis9533e342005-02-27 20:33:25 +0000302 if (entry_number < 0) {
303 PyErr_SetString(PyExc_ValueError,
304 "History index cannot be negative");
305 return NULL;
306 }
Skip Montanaroe5069012004-08-15 14:32:06 +0000307 entry = remove_history(entry_number);
308 if (!entry) {
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000309 PyErr_Format(PyExc_ValueError,
310 "No history item at position %d",
311 entry_number);
Skip Montanaroe5069012004-08-15 14:32:06 +0000312 return NULL;
313 }
314 /* free memory allocated for the history entry */
315 if (entry->line)
316 free(entry->line);
317 if (entry->data)
318 free(entry->data);
319 free(entry);
320
321 Py_INCREF(Py_None);
322 return Py_None;
323}
324
325PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000326"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000327remove history item given by its position");
328
329static PyObject *
330py_replace_history(PyObject *self, PyObject *args)
331{
332 int entry_number;
333 char *line;
334 HIST_ENTRY *old_entry;
335
336 if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number, &line)) {
337 return NULL;
338 }
Martin v. Löwis9533e342005-02-27 20:33:25 +0000339 if (entry_number < 0) {
340 PyErr_SetString(PyExc_ValueError,
341 "History index cannot be negative");
342 return NULL;
343 }
Skip Montanaroe5069012004-08-15 14:32:06 +0000344 old_entry = replace_history_entry(entry_number, line, (void *)NULL);
345 if (!old_entry) {
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000346 PyErr_Format(PyExc_ValueError,
347 "No history item at position %d",
348 entry_number);
Skip Montanaroe5069012004-08-15 14:32:06 +0000349 return NULL;
350 }
351 /* free memory allocated for the old history entry */
352 if (old_entry->line)
353 free(old_entry->line);
354 if (old_entry->data)
355 free(old_entry->data);
356 free(old_entry);
357
358 Py_INCREF(Py_None);
359 return Py_None;
360}
361
362PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000363"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000364replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000365
366/* Add a line to the history buffer */
367
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000368static PyObject *
369py_add_history(PyObject *self, PyObject *args)
370{
371 char *line;
372
373 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
374 return NULL;
375 }
376 add_history(line);
377 Py_INCREF(Py_None);
378 return Py_None;
379}
380
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000381PyDoc_STRVAR(doc_add_history,
382"add_history(string) -> None\n\
383add a line to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000384
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000385
Guido van Rossum74f31432003-01-07 20:01:29 +0000386/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000387
388static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000389get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000390{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000391 return PyString_FromString(rl_completer_word_break_characters);
392}
Guido van Rossum74f31432003-01-07 20:01:29 +0000393
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000394PyDoc_STRVAR(doc_get_completer_delims,
395"get_completer_delims() -> string\n\
396get the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000397
Guido van Rossum74f31432003-01-07 20:01:29 +0000398
399/* Set the completer function */
400
Guido van Rossum290900a1997-09-26 21:51:21 +0000401static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000402set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000403{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000404 return set_hook("completer", &completer, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000405}
406
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000407PyDoc_STRVAR(doc_set_completer,
408"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000409Set or remove the completer function.\n\
410The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000411for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000412It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000413
Guido van Rossum74f31432003-01-07 20:01:29 +0000414
Michael W. Hudson796df152003-01-30 10:12:51 +0000415static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000416get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000417{
418 if (completer == NULL) {
419 Py_INCREF(Py_None);
420 return Py_None;
421 }
422 Py_INCREF(completer);
423 return completer;
424}
425
426PyDoc_STRVAR(doc_get_completer,
427"get_completer() -> function\n\
428\n\
429Returns current completer function.");
430
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000431/* Exported function to get any element of history */
432
433static PyObject *
434get_history_item(PyObject *self, PyObject *args)
435{
436 int idx = 0;
437 HIST_ENTRY *hist_ent;
438
439 if (!PyArg_ParseTuple(args, "i:index", &idx))
440 return NULL;
441 if ((hist_ent = history_get(idx)))
Guido van Rossum05ac4492003-01-07 20:04:12 +0000442 return PyString_FromString(hist_ent->line);
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000443 else {
444 Py_INCREF(Py_None);
445 return Py_None;
446 }
447}
448
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000449PyDoc_STRVAR(doc_get_history_item,
450"get_history_item() -> string\n\
451return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000452
Guido van Rossum74f31432003-01-07 20:01:29 +0000453
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000454/* Exported function to get current length of history */
455
456static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000457get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000458{
459 HISTORY_STATE *hist_st;
460
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000461 hist_st = history_get_history_state();
462 return PyInt_FromLong(hist_st ? (long) hist_st->length : (long) 0);
463}
464
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000465PyDoc_STRVAR(doc_get_current_history_length,
466"get_current_history_length() -> integer\n\
467return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000468
Guido van Rossum74f31432003-01-07 20:01:29 +0000469
Guido van Rossum79378ff1997-10-07 14:53:21 +0000470/* Exported function to read the current line buffer */
471
472static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000473get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000474{
Guido van Rossum79378ff1997-10-07 14:53:21 +0000475 return PyString_FromString(rl_line_buffer);
476}
477
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000478PyDoc_STRVAR(doc_get_line_buffer,
479"get_line_buffer() -> string\n\
480return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000481
Guido van Rossum74f31432003-01-07 20:01:29 +0000482
Martin v. Löwise7a97962003-09-20 16:08:33 +0000483#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
484
485/* Exported function to clear the current history */
486
487static PyObject *
488py_clear_history(PyObject *self, PyObject *noarg)
489{
490 clear_history();
491 Py_INCREF(Py_None);
492 return Py_None;
493}
494
495PyDoc_STRVAR(doc_clear_history,
496"clear_history() -> None\n\
497Clear the current readline history.");
498#endif
499
500
Guido van Rossum79378ff1997-10-07 14:53:21 +0000501/* Exported function to insert text into the line buffer */
502
503static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000504insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000505{
506 char *s;
Guido van Rossum43713e52000-02-29 13:59:29 +0000507 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000508 return NULL;
509 rl_insert_text(s);
510 Py_INCREF(Py_None);
511 return Py_None;
512}
513
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000514PyDoc_STRVAR(doc_insert_text,
515"insert_text(string) -> None\n\
516Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000517
Guido van Rossum74f31432003-01-07 20:01:29 +0000518
519/* Redisplay the line buffer */
520
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000521static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000522redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000523{
524 rl_redisplay();
525 Py_INCREF(Py_None);
526 return Py_None;
527}
528
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000529PyDoc_STRVAR(doc_redisplay,
530"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000531Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000532contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000533
Guido van Rossum74f31432003-01-07 20:01:29 +0000534
Guido van Rossum290900a1997-09-26 21:51:21 +0000535/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000536
537static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000538{
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000539 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000540 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000541 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000542 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000543 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000544 {"read_history_file", read_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000545 METH_VARARGS, doc_read_history_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000546 {"write_history_file", write_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000547 METH_VARARGS, doc_write_history_file},
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000548 {"get_history_item", get_history_item,
549 METH_VARARGS, doc_get_history_item},
Neal Norwitz767f8352002-03-31 16:13:39 +0000550 {"get_current_history_length", (PyCFunction)get_current_history_length,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000551 METH_NOARGS, doc_get_current_history_length},
Guido van Rossum74f31432003-01-07 20:01:29 +0000552 {"set_history_length", set_history_length,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000553 METH_VARARGS, set_history_length_doc},
Guido van Rossum74f31432003-01-07 20:01:29 +0000554 {"get_history_length", get_history_length,
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000555 METH_NOARGS, get_history_length_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000556 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
Michael W. Hudson796df152003-01-30 10:12:51 +0000557 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000558 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
559 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000560
Guido van Rossum74f31432003-01-07 20:01:29 +0000561 {"set_completer_delims", set_completer_delims,
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000562 METH_VARARGS, doc_set_completer_delims},
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000563 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
Skip Montanaroe5069012004-08-15 14:32:06 +0000564 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
565 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000566 {"get_completer_delims", get_completer_delims,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000567 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000568
569 {"set_startup_hook", set_startup_hook,
570 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000571#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000572 {"set_pre_input_hook", set_pre_input_hook,
573 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000574#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000575#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
576 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
577#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000578 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000579};
580
Guido van Rossum05ac4492003-01-07 20:04:12 +0000581
Martin v. Löwis0daad592001-09-30 21:09:59 +0000582/* C function to call the Python hooks. */
583
584static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000585on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000586{
587 int result = 0;
588 if (func != NULL) {
589 PyObject *r;
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000590#ifdef WITH_THREAD
591 PyGILState_STATE gilstate = PyGILState_Ensure();
592#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000593 r = PyObject_CallFunction(func, NULL);
594 if (r == NULL)
595 goto error;
Guido van Rossum74f31432003-01-07 20:01:29 +0000596 if (r == Py_None)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000597 result = 0;
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000598 else {
Martin v. Löwis0daad592001-09-30 21:09:59 +0000599 result = PyInt_AsLong(r);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000600 if (result == -1 && PyErr_Occurred())
601 goto error;
602 }
Martin v. Löwis0daad592001-09-30 21:09:59 +0000603 Py_DECREF(r);
604 goto done;
605 error:
606 PyErr_Clear();
607 Py_XDECREF(r);
608 done:
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000609#ifdef WITH_THREAD
610 PyGILState_Release(gilstate);
611#endif
Georg Brandle677adc2005-09-29 13:40:49 +0000612 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000613 }
614 return result;
615}
616
617static int
618on_startup_hook(void)
619{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000620 return on_hook(startup_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000621}
622
623#ifdef HAVE_RL_PRE_INPUT_HOOK
624static int
625on_pre_input_hook(void)
626{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000627 return on_hook(pre_input_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000628}
629#endif
630
Guido van Rossum05ac4492003-01-07 20:04:12 +0000631
Guido van Rossum290900a1997-09-26 21:51:21 +0000632/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000633
Guido van Rossum290900a1997-09-26 21:51:21 +0000634static char *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000635on_completion(char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000636{
Guido van Rossum290900a1997-09-26 21:51:21 +0000637 char *result = NULL;
638 if (completer != NULL) {
639 PyObject *r;
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000640#ifdef WITH_THREAD
641 PyGILState_STATE gilstate = PyGILState_Ensure();
642#endif
Michael W. Hudson0c1ceaf2002-02-13 11:58:25 +0000643 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:
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000662#ifdef WITH_THREAD
663 PyGILState_Release(gilstate);
664#endif
Georg Brandle677adc2005-09-29 13:40:49 +0000665 return result;
Guido van Rossum290900a1997-09-26 21:51:21 +0000666 }
667 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000668}
669
Guido van Rossum290900a1997-09-26 21:51:21 +0000670
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000671/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000672 * before calling the normal completer */
673
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000674static char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000675flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000676{
677 Py_XDECREF(begidx);
678 Py_XDECREF(endidx);
679 begidx = PyInt_FromLong((long) start);
680 endidx = PyInt_FromLong((long) end);
681 return completion_matches(text, *on_completion);
682}
683
Guido van Rossum05ac4492003-01-07 20:04:12 +0000684
Guido van Rossum290900a1997-09-26 21:51:21 +0000685/* Helper to initialize GNU readline properly. */
686
687static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000688setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000689{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000690#ifdef SAVE_LOCALE
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000691 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwis701abe72004-08-20 06:26:59 +0000692 if (!saved_locale)
693 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000694#endif
695
Skip Montanaroa0392742002-06-11 14:32:46 +0000696 using_history();
697
Guido van Rossum290900a1997-09-26 21:51:21 +0000698 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000699#if defined(PYOS_OS2) && defined(PYCC_GCC)
700 /* Allow $if term= in .inputrc to work */
701 rl_terminal_name = getenv("TERM");
702#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000703 /* Force rebind of TAB to insert-tab */
704 rl_bind_key('\t', rl_insert);
705 /* Bind both ESC-TAB and ESC-ESC to the completion function */
706 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
707 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000708 /* Set our hook functions */
709 rl_startup_hook = (Function *)on_startup_hook;
710#ifdef HAVE_RL_PRE_INPUT_HOOK
711 rl_pre_input_hook = (Function *)on_pre_input_hook;
712#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000713 /* Set our completion function */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000714 rl_attempted_completion_function = (CPPFunction *)flex_complete;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000715 /* Set Python word break characters */
716 rl_completer_word_break_characters =
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000717 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000718 /* All nonalphanums except '.' */
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000719#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum84271bb2002-05-30 15:41:56 +0000720 rl_completion_append_character ='\0';
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000721#endif
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000722
723 begidx = PyInt_FromLong(0L);
724 endidx = PyInt_FromLong(0L);
Barry Warsawf7612871999-01-29 21:55:03 +0000725 /* Initialize (allows .inputrc to override)
726 *
727 * XXX: A bug in the readline-2.2 library causes a memory leak
728 * inside this function. Nothing we can do about it.
729 */
Guido van Rossum290900a1997-09-26 21:51:21 +0000730 rl_initialize();
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000731
Neal Norwitz5eaf7722006-07-16 02:15:27 +0000732 RESTORE_LOCALE(saved_locale)
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 */
Andrew M. Kuchling62e475b2006-09-07 13:59:38 +0000771
772 /* [Bug #1552726] Only limit the pause if an input hook has been
773 defined. */
774 struct timeval *timeoutp = NULL;
775 if (PyOS_InputHook)
776 timeoutp = &timeout;
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000777 FD_SET(fileno(rl_instream), &selectset);
778 /* select resets selectset if no input was available */
779 has_input = select(fileno(rl_instream) + 1, &selectset,
Andrew M. Kuchling62e475b2006-09-07 13:59:38 +0000780 NULL, NULL, timeoutp);
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000781 if(PyOS_InputHook) PyOS_InputHook();
782 }
783
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000784 if(has_input > 0) {
785 rl_callback_read_char();
786 }
787 else if (errno == EINTR) {
788 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000789#ifdef WITH_THREAD
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000790 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000791#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000792 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000793#ifdef WITH_THREAD
Michael W. Hudson23849902004-07-08 15:28:26 +0000794 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000795#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000796 if (s < 0) {
797 rl_free_line_state();
798 rl_cleanup_after_signal();
799 rl_callback_handler_remove();
800 *signal = 1;
801 completed_input_string = NULL;
802 }
803 }
804 }
805
806 return completed_input_string;
807}
808
809
810#else
Guido van Rossum290900a1997-09-26 21:51:21 +0000811
812/* Interrupt handler */
813
814static jmp_buf jbuf;
815
Guido van Rossum0969d361997-08-05 21:27:50 +0000816/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +0000817static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000818onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000819{
Guido van Rossum290900a1997-09-26 21:51:21 +0000820 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000821}
822
Guido van Rossum290900a1997-09-26 21:51:21 +0000823
Guido van Rossum0969d361997-08-05 21:27:50 +0000824static char *
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000825readline_until_enter_or_signal(char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +0000826{
Guido van Rossum174efc92000-09-16 16:37:53 +0000827 PyOS_sighandler_t old_inthandler;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000828 char *p;
829
830 *signal = 0;
Guido van Rossum74f31432003-01-07 20:01:29 +0000831
Guido van Rossum174efc92000-09-16 16:37:53 +0000832 old_inthandler = PyOS_setsig(SIGINT, onintr);
Guido van Rossum0969d361997-08-05 21:27:50 +0000833 if (setjmp(jbuf)) {
834#ifdef HAVE_SIGRELSE
835 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
836 sigrelse(SIGINT);
837#endif
Guido van Rossum174efc92000-09-16 16:37:53 +0000838 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000839 *signal = 1;
Guido van Rossum0969d361997-08-05 21:27:50 +0000840 return NULL;
841 }
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000842 rl_event_hook = PyOS_InputHook;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000843 p = readline(prompt);
844 PyOS_setsig(SIGINT, old_inthandler);
845
846 return p;
847}
848#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
849
850
851static char *
852call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
853{
Neal Norwitz1fa040b2004-08-25 01:20:18 +0000854 size_t n;
855 char *p, *q;
856 int signal;
857
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000858#ifdef SAVE_LOCALE
859 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwis701abe72004-08-20 06:26:59 +0000860 if (!saved_locale)
861 Py_FatalError("not enough memory to save locale");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000862 setlocale(LC_CTYPE, "");
863#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000864
Guido van Rossum74f31432003-01-07 20:01:29 +0000865 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
866 rl_instream = sys_stdin;
867 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000868#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum74f31432003-01-07 20:01:29 +0000869 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000870#endif
Guido van Rossum74f31432003-01-07 20:01:29 +0000871 }
872
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000873 p = readline_until_enter_or_signal(prompt, &signal);
874
875 /* we got an interrupt signal */
Neal Norwitz5eaf7722006-07-16 02:15:27 +0000876 if (signal) {
877 RESTORE_LOCALE(saved_locale)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000878 return NULL;
879 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000880
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000881 /* We got an EOF, return a empty string. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000882 if (p == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000883 p = PyMem_Malloc(1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000884 if (p != NULL)
885 *p = '\0';
Neal Norwitz5eaf7722006-07-16 02:15:27 +0000886 RESTORE_LOCALE(saved_locale)
Guido van Rossum0969d361997-08-05 21:27:50 +0000887 return p;
888 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000889
890 /* we have a valid line */
Guido van Rossum0969d361997-08-05 21:27:50 +0000891 n = strlen(p);
Skip Montanaroa0392742002-06-11 14:32:46 +0000892 if (n > 0) {
893 char *line;
894 HISTORY_STATE *state = history_get_history_state();
895 if (state->length > 0)
896 line = history_get(state->length)->line;
897 else
898 line = "";
899 if (strcmp(p, line))
900 add_history(p);
901 /* the history docs don't say so, but the address of state
902 changes each time history_get_history_state is called
903 which makes me think it's freshly malloc'd memory...
904 on the other hand, the address of the last line stays the
905 same as long as history isn't extended, so it appears to
906 be malloc'd but managed by the history package... */
907 free(state);
908 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000909 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
910 release the original. */
911 q = p;
912 p = PyMem_Malloc(n+2);
913 if (p != NULL) {
914 strncpy(p, q, n);
Guido van Rossum0969d361997-08-05 21:27:50 +0000915 p[n] = '\n';
916 p[n+1] = '\0';
917 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000918 free(q);
Neal Norwitz5eaf7722006-07-16 02:15:27 +0000919 RESTORE_LOCALE(saved_locale)
Guido van Rossum0969d361997-08-05 21:27:50 +0000920 return p;
921}
922
Guido van Rossum290900a1997-09-26 21:51:21 +0000923
924/* Initialize the module */
925
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000926PyDoc_STRVAR(doc_module,
927"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000928
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000929PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000930initreadline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000931{
Guido van Rossum1ea64ea2000-10-02 15:53:08 +0000932 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +0000933
934 m = Py_InitModule4("readline", readline_methods, doc_module,
935 (PyObject *)NULL, PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000936 if (m == NULL)
937 return;
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000938
Guido van Rossum74f31432003-01-07 20:01:29 +0000939 PyOS_ReadlineFunctionPointer = call_readline;
940 setup_readline();
Guido van Rossum0969d361997-08-05 21:27:50 +0000941}