blob: fd800ffa1aa2d046bef6fcbd52728ac31692347b [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
Thomas Wouters0e3f5912006-08-11 14:57:12 +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 Rossumd8faa362007-04-27 19:54:29 +000037#else
38extern char **completion_matches(char *, rl_compentry_func_t *);
Guido van Rossum353ae582001-07-10 16:45:32 +000039#endif
40
Guido van Rossum0969d361997-08-05 21:27:50 +000041
Guido van Rossum290900a1997-09-26 21:51:21 +000042/* Exported function to send one line to readline's init file parser */
43
44static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000045parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000046{
Guido van Rossum3b5330e1998-12-04 15:34:39 +000047 char *s, *copy;
Guido van Rossum43713e52000-02-29 13:59:29 +000048 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000049 return NULL;
Guido van Rossum3b5330e1998-12-04 15:34:39 +000050 /* Make a copy -- rl_parse_and_bind() modifies its argument */
51 /* Bernard Herzog */
52 copy = malloc(1 + strlen(s));
53 if (copy == NULL)
54 return PyErr_NoMemory();
55 strcpy(copy, s);
56 rl_parse_and_bind(copy);
57 free(copy); /* Free the copy */
Guido van Rossum290900a1997-09-26 21:51:21 +000058 Py_INCREF(Py_None);
59 return Py_None;
60}
61
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000062PyDoc_STRVAR(doc_parse_and_bind,
63"parse_and_bind(string) -> None\n\
64Parse and execute single line of a readline init file.");
Guido van Rossum290900a1997-09-26 21:51:21 +000065
66
67/* Exported function to parse a readline init file */
68
69static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000070read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000071{
72 char *s = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +000073 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000074 return NULL;
75 errno = rl_read_init_file(s);
76 if (errno)
77 return PyErr_SetFromErrno(PyExc_IOError);
78 Py_INCREF(Py_None);
79 return Py_None;
80}
81
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000082PyDoc_STRVAR(doc_read_init_file,
83"read_init_file([filename]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +000084Parse a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000085The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +000086
87
Skip Montanaro28067822000-07-06 18:55:12 +000088/* Exported function to load a readline history file */
89
90static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000091read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +000092{
93 char *s = NULL;
94 if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
95 return NULL;
96 errno = read_history(s);
97 if (errno)
98 return PyErr_SetFromErrno(PyExc_IOError);
99 Py_INCREF(Py_None);
100 return Py_None;
101}
102
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000103static int _history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000104PyDoc_STRVAR(doc_read_history_file,
105"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000106Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000107The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000108
109
110/* Exported function to save a readline history file */
111
112static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000113write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000114{
115 char *s = NULL;
116 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
117 return NULL;
118 errno = write_history(s);
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000119 if (!errno && _history_length >= 0)
120 history_truncate_file(s, _history_length);
Skip Montanaro28067822000-07-06 18:55:12 +0000121 if (errno)
122 return PyErr_SetFromErrno(PyExc_IOError);
123 Py_INCREF(Py_None);
124 return Py_None;
125}
126
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000127PyDoc_STRVAR(doc_write_history_file,
128"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000129Save a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000130The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000131
132
Guido van Rossum74f31432003-01-07 20:01:29 +0000133/* Set history length */
134
135static PyObject*
136set_history_length(PyObject *self, PyObject *args)
137{
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000138 int length = _history_length;
Guido van Rossum74f31432003-01-07 20:01:29 +0000139 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
140 return NULL;
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000141 _history_length = length;
Guido van Rossum74f31432003-01-07 20:01:29 +0000142 Py_INCREF(Py_None);
143 return Py_None;
144}
145
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000146PyDoc_STRVAR(set_history_length_doc,
147"set_history_length(length) -> None\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000148set the maximal number of items which will be written to\n\
149the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000150history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000151
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000152
Guido van Rossum74f31432003-01-07 20:01:29 +0000153/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000154
155static PyObject*
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000156get_history_length(PyObject *self, PyObject *noarg)
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000157{
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000158 return PyInt_FromLong(_history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000159}
160
Guido van Rossum74f31432003-01-07 20:01:29 +0000161PyDoc_STRVAR(get_history_length_doc,
162"get_history_length() -> int\n\
163return the maximum number of items that will be written to\n\
164the history file.");
165
166
Martin v. Löwis0daad592001-09-30 21:09:59 +0000167/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000168
Martin v. Löwis0daad592001-09-30 21:09:59 +0000169static PyObject *
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000170set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000171{
172 PyObject *function = Py_None;
173 char buf[80];
Tim Peters885d4572001-11-28 20:27:42 +0000174 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000175 if (!PyArg_ParseTuple(args, buf, &function))
176 return NULL;
177 if (function == Py_None) {
178 Py_XDECREF(*hook_var);
179 *hook_var = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000180 }
181 else if (PyCallable_Check(function)) {
182 PyObject *tmp = *hook_var;
183 Py_INCREF(function);
184 *hook_var = function;
185 Py_XDECREF(tmp);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000186 }
187 else {
Tim Peters885d4572001-11-28 20:27:42 +0000188 PyOS_snprintf(buf, sizeof(buf),
189 "set_%.50s(func): argument not callable",
190 funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000191 PyErr_SetString(PyExc_TypeError, buf);
192 return NULL;
193 }
194 Py_INCREF(Py_None);
195 return Py_None;
196}
197
Guido van Rossum74f31432003-01-07 20:01:29 +0000198
Martin v. Löwis0daad592001-09-30 21:09:59 +0000199/* Exported functions to specify hook functions in Python */
200
201static PyObject *startup_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000202
203#ifdef HAVE_RL_PRE_INPUT_HOOK
204static PyObject *pre_input_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000205#endif
206
207static PyObject *
208set_startup_hook(PyObject *self, PyObject *args)
209{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000210 return set_hook("startup_hook", &startup_hook, 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{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000227 return set_hook("pre_input_hook", &pre_input_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000228}
229
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000230PyDoc_STRVAR(doc_set_pre_input_hook,
231"set_pre_input_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000232Set or remove the pre_input_hook function.\n\
233The function is called with no arguments after the first prompt\n\
234has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000235characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000236
Martin v. Löwis0daad592001-09-30 21:09:59 +0000237#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000238
Guido van Rossum74f31432003-01-07 20:01:29 +0000239
Guido van Rossum290900a1997-09-26 21:51:21 +0000240/* Exported function to specify a word completer in Python */
241
242static PyObject *completer = NULL;
Guido van Rossum290900a1997-09-26 21:51:21 +0000243
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000244static PyObject *begidx = NULL;
245static PyObject *endidx = NULL;
246
Guido van Rossum74f31432003-01-07 20:01:29 +0000247
248/* Get the beginning index for the scope of the tab-completion */
249
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000250static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000251get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000252{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000253 Py_INCREF(begidx);
254 return begidx;
255}
256
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000257PyDoc_STRVAR(doc_get_begidx,
258"get_begidx() -> int\n\
259get the beginning index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000260
Guido van Rossum74f31432003-01-07 20:01:29 +0000261
262/* Get the ending index for the scope of the tab-completion */
263
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000264static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000265get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000266{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000267 Py_INCREF(endidx);
268 return endidx;
269}
270
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000271PyDoc_STRVAR(doc_get_endidx,
272"get_endidx() -> int\n\
273get the ending index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000274
275
Guido van Rossum74f31432003-01-07 20:01:29 +0000276/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000277
278static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000279set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000280{
281 char *break_chars;
282
Guido van Rossum43713e52000-02-29 13:59:29 +0000283 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000284 return NULL;
285 }
Neal Norwitz0e0ee592002-04-21 15:03:18 +0000286 free((void*)rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000287 rl_completer_word_break_characters = strdup(break_chars);
288 Py_INCREF(Py_None);
289 return Py_None;
290}
291
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000292PyDoc_STRVAR(doc_set_completer_delims,
293"set_completer_delims(string) -> None\n\
294set the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000295
Skip Montanaroe5069012004-08-15 14:32:06 +0000296static PyObject *
297py_remove_history(PyObject *self, PyObject *args)
298{
299 int entry_number;
300 HIST_ENTRY *entry;
301
302 if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
303 return NULL;
Martin v. Löwis9533e342005-02-27 20:33:25 +0000304 if (entry_number < 0) {
305 PyErr_SetString(PyExc_ValueError,
306 "History index cannot be negative");
307 return NULL;
308 }
Skip Montanaroe5069012004-08-15 14:32:06 +0000309 entry = remove_history(entry_number);
310 if (!entry) {
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000311 PyErr_Format(PyExc_ValueError,
312 "No history item at position %d",
313 entry_number);
Skip Montanaroe5069012004-08-15 14:32:06 +0000314 return NULL;
315 }
316 /* free memory allocated for the history entry */
317 if (entry->line)
318 free(entry->line);
319 if (entry->data)
320 free(entry->data);
321 free(entry);
322
323 Py_INCREF(Py_None);
324 return Py_None;
325}
326
327PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000328"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000329remove history item given by its position");
330
331static PyObject *
332py_replace_history(PyObject *self, PyObject *args)
333{
334 int entry_number;
335 char *line;
336 HIST_ENTRY *old_entry;
337
338 if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number, &line)) {
339 return NULL;
340 }
Martin v. Löwis9533e342005-02-27 20:33:25 +0000341 if (entry_number < 0) {
342 PyErr_SetString(PyExc_ValueError,
343 "History index cannot be negative");
344 return NULL;
345 }
Skip Montanaroe5069012004-08-15 14:32:06 +0000346 old_entry = replace_history_entry(entry_number, line, (void *)NULL);
347 if (!old_entry) {
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000348 PyErr_Format(PyExc_ValueError,
349 "No history item at position %d",
350 entry_number);
Skip Montanaroe5069012004-08-15 14:32:06 +0000351 return NULL;
352 }
353 /* free memory allocated for the old history entry */
354 if (old_entry->line)
355 free(old_entry->line);
356 if (old_entry->data)
357 free(old_entry->data);
358 free(old_entry);
359
360 Py_INCREF(Py_None);
361 return Py_None;
362}
363
364PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000365"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000366replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000367
368/* Add a line to the history buffer */
369
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000370static PyObject *
371py_add_history(PyObject *self, PyObject *args)
372{
373 char *line;
374
375 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
376 return NULL;
377 }
378 add_history(line);
379 Py_INCREF(Py_None);
380 return Py_None;
381}
382
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000383PyDoc_STRVAR(doc_add_history,
384"add_history(string) -> None\n\
385add a line to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000386
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000387
Guido van Rossum74f31432003-01-07 20:01:29 +0000388/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000389
390static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000391get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000392{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000393 return PyString_FromString(rl_completer_word_break_characters);
394}
Guido van Rossum74f31432003-01-07 20:01:29 +0000395
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000396PyDoc_STRVAR(doc_get_completer_delims,
397"get_completer_delims() -> string\n\
398get the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000399
Guido van Rossum74f31432003-01-07 20:01:29 +0000400
401/* Set the completer function */
402
Guido van Rossum290900a1997-09-26 21:51:21 +0000403static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000404set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000405{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000406 return set_hook("completer", &completer, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000407}
408
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000409PyDoc_STRVAR(doc_set_completer,
410"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000411Set or remove the completer function.\n\
412The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000413for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000414It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000415
Guido van Rossum74f31432003-01-07 20:01:29 +0000416
Michael W. Hudson796df152003-01-30 10:12:51 +0000417static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000418get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000419{
420 if (completer == NULL) {
421 Py_INCREF(Py_None);
422 return Py_None;
423 }
424 Py_INCREF(completer);
425 return completer;
426}
427
428PyDoc_STRVAR(doc_get_completer,
429"get_completer() -> function\n\
430\n\
431Returns current completer function.");
432
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000433/* Exported function to get any element of history */
434
435static PyObject *
436get_history_item(PyObject *self, PyObject *args)
437{
438 int idx = 0;
439 HIST_ENTRY *hist_ent;
440
441 if (!PyArg_ParseTuple(args, "i:index", &idx))
442 return NULL;
443 if ((hist_ent = history_get(idx)))
Guido van Rossum05ac4492003-01-07 20:04:12 +0000444 return PyString_FromString(hist_ent->line);
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000445 else {
446 Py_INCREF(Py_None);
447 return Py_None;
448 }
449}
450
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000451PyDoc_STRVAR(doc_get_history_item,
452"get_history_item() -> string\n\
453return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000454
Guido van Rossum74f31432003-01-07 20:01:29 +0000455
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000456/* Exported function to get current length of history */
457
458static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000459get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000460{
461 HISTORY_STATE *hist_st;
462
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000463 hist_st = history_get_history_state();
464 return PyInt_FromLong(hist_st ? (long) hist_st->length : (long) 0);
465}
466
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000467PyDoc_STRVAR(doc_get_current_history_length,
468"get_current_history_length() -> integer\n\
469return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000470
Guido van Rossum74f31432003-01-07 20:01:29 +0000471
Guido van Rossum79378ff1997-10-07 14:53:21 +0000472/* Exported function to read the current line buffer */
473
474static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000475get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000476{
Guido van Rossum79378ff1997-10-07 14:53:21 +0000477 return PyString_FromString(rl_line_buffer);
478}
479
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000480PyDoc_STRVAR(doc_get_line_buffer,
481"get_line_buffer() -> string\n\
482return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000483
Guido van Rossum74f31432003-01-07 20:01:29 +0000484
Martin v. Löwise7a97962003-09-20 16:08:33 +0000485#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
486
487/* Exported function to clear the current history */
488
489static PyObject *
490py_clear_history(PyObject *self, PyObject *noarg)
491{
492 clear_history();
493 Py_INCREF(Py_None);
494 return Py_None;
495}
496
497PyDoc_STRVAR(doc_clear_history,
498"clear_history() -> None\n\
499Clear the current readline history.");
500#endif
501
502
Guido van Rossum79378ff1997-10-07 14:53:21 +0000503/* Exported function to insert text into the line buffer */
504
505static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000506insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000507{
508 char *s;
Guido van Rossum43713e52000-02-29 13:59:29 +0000509 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000510 return NULL;
511 rl_insert_text(s);
512 Py_INCREF(Py_None);
513 return Py_None;
514}
515
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000516PyDoc_STRVAR(doc_insert_text,
517"insert_text(string) -> None\n\
518Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000519
Guido van Rossum74f31432003-01-07 20:01:29 +0000520
521/* Redisplay the line buffer */
522
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000523static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000524redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000525{
526 rl_redisplay();
527 Py_INCREF(Py_None);
528 return Py_None;
529}
530
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000531PyDoc_STRVAR(doc_redisplay,
532"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000533Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000534contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000535
Guido van Rossum74f31432003-01-07 20:01:29 +0000536
Guido van Rossum290900a1997-09-26 21:51:21 +0000537/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000538
539static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000540{
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000541 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000542 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000543 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000544 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000545 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000546 {"read_history_file", read_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000547 METH_VARARGS, doc_read_history_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000548 {"write_history_file", write_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000549 METH_VARARGS, doc_write_history_file},
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000550 {"get_history_item", get_history_item,
551 METH_VARARGS, doc_get_history_item},
Neal Norwitz767f8352002-03-31 16:13:39 +0000552 {"get_current_history_length", (PyCFunction)get_current_history_length,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000553 METH_NOARGS, doc_get_current_history_length},
Guido van Rossum74f31432003-01-07 20:01:29 +0000554 {"set_history_length", set_history_length,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000555 METH_VARARGS, set_history_length_doc},
Guido van Rossum74f31432003-01-07 20:01:29 +0000556 {"get_history_length", get_history_length,
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000557 METH_NOARGS, get_history_length_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000558 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
Michael W. Hudson796df152003-01-30 10:12:51 +0000559 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000560 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
561 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000562
Guido van Rossum74f31432003-01-07 20:01:29 +0000563 {"set_completer_delims", set_completer_delims,
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000564 METH_VARARGS, doc_set_completer_delims},
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000565 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
Skip Montanaroe5069012004-08-15 14:32:06 +0000566 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
567 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000568 {"get_completer_delims", get_completer_delims,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000569 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000570
571 {"set_startup_hook", set_startup_hook,
572 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000573#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000574 {"set_pre_input_hook", set_pre_input_hook,
575 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000576#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000577#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
578 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
579#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000580 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000581};
582
Guido van Rossum05ac4492003-01-07 20:04:12 +0000583
Martin v. Löwis0daad592001-09-30 21:09:59 +0000584/* C function to call the Python hooks. */
585
586static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000587on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000588{
589 int result = 0;
590 if (func != NULL) {
591 PyObject *r;
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000592#ifdef WITH_THREAD
593 PyGILState_STATE gilstate = PyGILState_Ensure();
594#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000595 r = PyObject_CallFunction(func, NULL);
596 if (r == NULL)
597 goto error;
Guido van Rossum74f31432003-01-07 20:01:29 +0000598 if (r == Py_None)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000599 result = 0;
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000600 else {
Martin v. Löwis0daad592001-09-30 21:09:59 +0000601 result = PyInt_AsLong(r);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000602 if (result == -1 && PyErr_Occurred())
603 goto error;
604 }
Martin v. Löwis0daad592001-09-30 21:09:59 +0000605 Py_DECREF(r);
606 goto done;
607 error:
608 PyErr_Clear();
609 Py_XDECREF(r);
610 done:
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000611#ifdef WITH_THREAD
612 PyGILState_Release(gilstate);
613#endif
Georg Brandle677adc2005-09-29 13:40:49 +0000614 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000615 }
616 return result;
617}
618
619static int
620on_startup_hook(void)
621{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000622 return on_hook(startup_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000623}
624
625#ifdef HAVE_RL_PRE_INPUT_HOOK
626static int
627on_pre_input_hook(void)
628{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000629 return on_hook(pre_input_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000630}
631#endif
632
Guido van Rossum05ac4492003-01-07 20:04:12 +0000633
Guido van Rossum290900a1997-09-26 21:51:21 +0000634/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000635
Guido van Rossum290900a1997-09-26 21:51:21 +0000636static char *
Guido van Rossumd8faa362007-04-27 19:54:29 +0000637on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000638{
Guido van Rossum290900a1997-09-26 21:51:21 +0000639 char *result = NULL;
640 if (completer != NULL) {
641 PyObject *r;
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000642#ifdef WITH_THREAD
643 PyGILState_STATE gilstate = PyGILState_Ensure();
644#endif
Michael W. Hudson0c1ceaf2002-02-13 11:58:25 +0000645 rl_attempted_completion_over = 1;
Guido van Rossum290900a1997-09-26 21:51:21 +0000646 r = PyObject_CallFunction(completer, "si", text, state);
647 if (r == NULL)
648 goto error;
649 if (r == Py_None) {
650 result = NULL;
651 }
652 else {
653 char *s = PyString_AsString(r);
654 if (s == NULL)
655 goto error;
656 result = strdup(s);
657 }
658 Py_DECREF(r);
659 goto done;
660 error:
661 PyErr_Clear();
662 Py_XDECREF(r);
663 done:
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000664#ifdef WITH_THREAD
665 PyGILState_Release(gilstate);
666#endif
Georg Brandle677adc2005-09-29 13:40:49 +0000667 return result;
Guido van Rossum290900a1997-09-26 21:51:21 +0000668 }
669 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000670}
671
Guido van Rossum290900a1997-09-26 21:51:21 +0000672
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000673/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000674 * before calling the normal completer */
675
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000676static char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000677flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000678{
679 Py_XDECREF(begidx);
680 Py_XDECREF(endidx);
681 begidx = PyInt_FromLong((long) start);
682 endidx = PyInt_FromLong((long) end);
683 return completion_matches(text, *on_completion);
684}
685
Guido van Rossum05ac4492003-01-07 20:04:12 +0000686
Guido van Rossum290900a1997-09-26 21:51:21 +0000687/* Helper to initialize GNU readline properly. */
688
689static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000690setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000691{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000692#ifdef SAVE_LOCALE
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000693 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwis701abe72004-08-20 06:26:59 +0000694 if (!saved_locale)
695 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000696#endif
697
Skip Montanaroa0392742002-06-11 14:32:46 +0000698 using_history();
699
Guido van Rossum290900a1997-09-26 21:51:21 +0000700 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000701#if defined(PYOS_OS2) && defined(PYCC_GCC)
702 /* Allow $if term= in .inputrc to work */
703 rl_terminal_name = getenv("TERM");
704#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000705 /* Force rebind of TAB to insert-tab */
706 rl_bind_key('\t', rl_insert);
707 /* Bind both ESC-TAB and ESC-ESC to the completion function */
708 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
709 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000710 /* Set our hook functions */
711 rl_startup_hook = (Function *)on_startup_hook;
712#ifdef HAVE_RL_PRE_INPUT_HOOK
713 rl_pre_input_hook = (Function *)on_pre_input_hook;
714#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000715 /* Set our completion function */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000716 rl_attempted_completion_function = (CPPFunction *)flex_complete;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000717 /* Set Python word break characters */
718 rl_completer_word_break_characters =
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000719 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000720 /* All nonalphanums except '.' */
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000721#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum84271bb2002-05-30 15:41:56 +0000722 rl_completion_append_character ='\0';
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000723#endif
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000724
725 begidx = PyInt_FromLong(0L);
726 endidx = PyInt_FromLong(0L);
Barry Warsawf7612871999-01-29 21:55:03 +0000727 /* Initialize (allows .inputrc to override)
728 *
729 * XXX: A bug in the readline-2.2 library causes a memory leak
730 * inside this function. Nothing we can do about it.
731 */
Guido van Rossum290900a1997-09-26 21:51:21 +0000732 rl_initialize();
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000733
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000734 RESTORE_LOCALE(saved_locale)
Guido van Rossum290900a1997-09-26 21:51:21 +0000735}
736
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000737/* Wrapper around GNU readline that handles signals differently. */
738
739
740#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
741
742static char *completed_input_string;
743static void
744rlhandler(char *text)
745{
746 completed_input_string = text;
747 rl_callback_handler_remove();
748}
749
750extern PyThreadState* _PyOS_ReadlineTState;
751
752static char *
753readline_until_enter_or_signal(char *prompt, int *signal)
754{
755 char * not_done_reading = "";
756 fd_set selectset;
757
758 *signal = 0;
759#ifdef HAVE_RL_CATCH_SIGNAL
760 rl_catch_signals = 0;
761#endif
762
763 rl_callback_handler_install (prompt, rlhandler);
764 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000765
766 completed_input_string = not_done_reading;
767
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000768 while (completed_input_string == not_done_reading) {
769 int has_input = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000770
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000771 while (!has_input)
772 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000773
774 /* [Bug #1552726] Only limit the pause if an input hook has been
775 defined. */
776 struct timeval *timeoutp = NULL;
777 if (PyOS_InputHook)
778 timeoutp = &timeout;
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000779 FD_SET(fileno(rl_instream), &selectset);
780 /* select resets selectset if no input was available */
781 has_input = select(fileno(rl_instream) + 1, &selectset,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000782 NULL, NULL, timeoutp);
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000783 if(PyOS_InputHook) PyOS_InputHook();
784 }
785
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000786 if(has_input > 0) {
787 rl_callback_read_char();
788 }
789 else if (errno == EINTR) {
790 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000791#ifdef WITH_THREAD
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000792 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000793#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000794 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000795#ifdef WITH_THREAD
Michael W. Hudson23849902004-07-08 15:28:26 +0000796 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000797#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000798 if (s < 0) {
799 rl_free_line_state();
800 rl_cleanup_after_signal();
801 rl_callback_handler_remove();
802 *signal = 1;
803 completed_input_string = NULL;
804 }
805 }
806 }
807
808 return completed_input_string;
809}
810
811
812#else
Guido van Rossum290900a1997-09-26 21:51:21 +0000813
814/* Interrupt handler */
815
816static jmp_buf jbuf;
817
Guido van Rossum0969d361997-08-05 21:27:50 +0000818/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +0000819static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000820onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000821{
Guido van Rossum290900a1997-09-26 21:51:21 +0000822 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000823}
824
Guido van Rossum290900a1997-09-26 21:51:21 +0000825
Guido van Rossum0969d361997-08-05 21:27:50 +0000826static char *
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000827readline_until_enter_or_signal(char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +0000828{
Guido van Rossum174efc92000-09-16 16:37:53 +0000829 PyOS_sighandler_t old_inthandler;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000830 char *p;
831
832 *signal = 0;
Guido van Rossum74f31432003-01-07 20:01:29 +0000833
Guido van Rossum174efc92000-09-16 16:37:53 +0000834 old_inthandler = PyOS_setsig(SIGINT, onintr);
Guido van Rossum0969d361997-08-05 21:27:50 +0000835 if (setjmp(jbuf)) {
836#ifdef HAVE_SIGRELSE
837 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
838 sigrelse(SIGINT);
839#endif
Guido van Rossum174efc92000-09-16 16:37:53 +0000840 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000841 *signal = 1;
Guido van Rossum0969d361997-08-05 21:27:50 +0000842 return NULL;
843 }
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000844 rl_event_hook = PyOS_InputHook;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000845 p = readline(prompt);
846 PyOS_setsig(SIGINT, old_inthandler);
847
848 return p;
849}
850#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
851
852
853static char *
854call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
855{
Neal Norwitz1fa040b2004-08-25 01:20:18 +0000856 size_t n;
857 char *p, *q;
858 int signal;
859
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000860#ifdef SAVE_LOCALE
861 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwis701abe72004-08-20 06:26:59 +0000862 if (!saved_locale)
863 Py_FatalError("not enough memory to save locale");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000864 setlocale(LC_CTYPE, "");
865#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000866
Guido van Rossum74f31432003-01-07 20:01:29 +0000867 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
868 rl_instream = sys_stdin;
869 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000870#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum74f31432003-01-07 20:01:29 +0000871 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000872#endif
Guido van Rossum74f31432003-01-07 20:01:29 +0000873 }
874
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000875 p = readline_until_enter_or_signal(prompt, &signal);
876
877 /* we got an interrupt signal */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000878 if (signal) {
879 RESTORE_LOCALE(saved_locale)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000880 return NULL;
881 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000882
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000883 /* We got an EOF, return a empty string. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000884 if (p == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000885 p = PyMem_Malloc(1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000886 if (p != NULL)
887 *p = '\0';
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000888 RESTORE_LOCALE(saved_locale)
Guido van Rossum0969d361997-08-05 21:27:50 +0000889 return p;
890 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000891
892 /* we have a valid line */
Guido van Rossum0969d361997-08-05 21:27:50 +0000893 n = strlen(p);
Skip Montanaroa0392742002-06-11 14:32:46 +0000894 if (n > 0) {
895 char *line;
896 HISTORY_STATE *state = history_get_history_state();
897 if (state->length > 0)
898 line = history_get(state->length)->line;
899 else
900 line = "";
901 if (strcmp(p, line))
902 add_history(p);
903 /* the history docs don't say so, but the address of state
904 changes each time history_get_history_state is called
905 which makes me think it's freshly malloc'd memory...
906 on the other hand, the address of the last line stays the
907 same as long as history isn't extended, so it appears to
908 be malloc'd but managed by the history package... */
909 free(state);
910 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000911 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
912 release the original. */
913 q = p;
914 p = PyMem_Malloc(n+2);
915 if (p != NULL) {
916 strncpy(p, q, n);
Guido van Rossum0969d361997-08-05 21:27:50 +0000917 p[n] = '\n';
918 p[n+1] = '\0';
919 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000920 free(q);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000921 RESTORE_LOCALE(saved_locale)
Guido van Rossum0969d361997-08-05 21:27:50 +0000922 return p;
923}
924
Guido van Rossum290900a1997-09-26 21:51:21 +0000925
926/* Initialize the module */
927
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000928PyDoc_STRVAR(doc_module,
929"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000930
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000931PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000932initreadline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000933{
Guido van Rossum1ea64ea2000-10-02 15:53:08 +0000934 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +0000935
936 m = Py_InitModule4("readline", readline_methods, doc_module,
937 (PyObject *)NULL, PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000938 if (m == NULL)
939 return;
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000940
Guido van Rossum74f31432003-01-07 20:01:29 +0000941 PyOS_ReadlineFunctionPointer = call_readline;
942 setup_readline();
Guido van Rossum0969d361997-08-05 21:27:50 +0000943}