blob: a62244dda4e1e3c1c527aa6a923334f3466718cc [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>
Guido van Rossum0969d361997-08-05 21:27:50 +000014
Skip Montanaro7befb992004-02-10 16:50:21 +000015#if defined(HAVE_SETLOCALE)
Guido van Rossum60c8a3a2002-10-09 21:27:33 +000016/* GNU readline() mistakenly sets the LC_CTYPE locale.
17 * This is evil. Only the user or the app's main() should do this!
18 * We must save and restore the locale around the rl_initialize() call.
19 */
20#define SAVE_LOCALE
21#include <locale.h>
22#endif
23
Guido van Rossum290900a1997-09-26 21:51:21 +000024/* GNU readline definitions */
Guido van Rossumb0e51b22001-04-13 18:14:27 +000025#undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
Guido van Rossumbcc20741998-08-04 22:53:56 +000026#include <readline/readline.h>
27#include <readline/history.h>
Guido van Rossum730806d1998-04-10 22:27:42 +000028
Guido van Rossum353ae582001-07-10 16:45:32 +000029#ifdef HAVE_RL_COMPLETION_MATCHES
Guido van Rossum74f31432003-01-07 20:01:29 +000030#define completion_matches(x, y) \
31 rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
Guido van Rossum353ae582001-07-10 16:45:32 +000032#endif
33
Guido van Rossum0969d361997-08-05 21:27:50 +000034
Guido van Rossum290900a1997-09-26 21:51:21 +000035/* Exported function to send one line to readline's init file parser */
36
37static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000038parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000039{
Guido van Rossum3b5330e1998-12-04 15:34:39 +000040 char *s, *copy;
Guido van Rossum43713e52000-02-29 13:59:29 +000041 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000042 return NULL;
Guido van Rossum3b5330e1998-12-04 15:34:39 +000043 /* Make a copy -- rl_parse_and_bind() modifies its argument */
44 /* Bernard Herzog */
45 copy = malloc(1 + strlen(s));
46 if (copy == NULL)
47 return PyErr_NoMemory();
48 strcpy(copy, s);
49 rl_parse_and_bind(copy);
50 free(copy); /* Free the copy */
Guido van Rossum290900a1997-09-26 21:51:21 +000051 Py_INCREF(Py_None);
52 return Py_None;
53}
54
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000055PyDoc_STRVAR(doc_parse_and_bind,
56"parse_and_bind(string) -> None\n\
57Parse and execute single line of a readline init file.");
Guido van Rossum290900a1997-09-26 21:51:21 +000058
59
60/* Exported function to parse a readline init file */
61
62static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000063read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000064{
65 char *s = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +000066 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000067 return NULL;
68 errno = rl_read_init_file(s);
69 if (errno)
70 return PyErr_SetFromErrno(PyExc_IOError);
71 Py_INCREF(Py_None);
72 return Py_None;
73}
74
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000075PyDoc_STRVAR(doc_read_init_file,
76"read_init_file([filename]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +000077Parse a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000078The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +000079
80
Skip Montanaro28067822000-07-06 18:55:12 +000081/* Exported function to load a readline history file */
82
83static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000084read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +000085{
86 char *s = NULL;
87 if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
88 return NULL;
89 errno = read_history(s);
90 if (errno)
91 return PyErr_SetFromErrno(PyExc_IOError);
92 Py_INCREF(Py_None);
93 return Py_None;
94}
95
Skip Montanaro49bd24d2000-07-19 16:54:53 +000096static int history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000097PyDoc_STRVAR(doc_read_history_file,
98"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +000099Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000100The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000101
102
103/* Exported function to save a readline history file */
104
105static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000106write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000107{
108 char *s = NULL;
109 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
110 return NULL;
111 errno = write_history(s);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000112 if (!errno && history_length >= 0)
113 history_truncate_file(s, history_length);
Skip Montanaro28067822000-07-06 18:55:12 +0000114 if (errno)
115 return PyErr_SetFromErrno(PyExc_IOError);
116 Py_INCREF(Py_None);
117 return Py_None;
118}
119
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000120PyDoc_STRVAR(doc_write_history_file,
121"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000122Save a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000123The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000124
125
Guido van Rossum74f31432003-01-07 20:01:29 +0000126/* Set history length */
127
128static PyObject*
129set_history_length(PyObject *self, PyObject *args)
130{
131 int length = history_length;
132 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
133 return NULL;
134 history_length = length;
135 Py_INCREF(Py_None);
136 return Py_None;
137}
138
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000139PyDoc_STRVAR(set_history_length_doc,
140"set_history_length(length) -> None\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000141set the maximal number of items which will be written to\n\
142the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000143history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000144
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000145
Guido van Rossum74f31432003-01-07 20:01:29 +0000146/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000147
148static PyObject*
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000149get_history_length(PyObject *self, PyObject *noarg)
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000150{
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000151 return PyInt_FromLong(history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000152}
153
Guido van Rossum74f31432003-01-07 20:01:29 +0000154PyDoc_STRVAR(get_history_length_doc,
155"get_history_length() -> int\n\
156return the maximum number of items that will be written to\n\
157the history file.");
158
159
Martin v. Löwis0daad592001-09-30 21:09:59 +0000160/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000161
Martin v. Löwis0daad592001-09-30 21:09:59 +0000162static PyObject *
Guido van Rossum74f31432003-01-07 20:01:29 +0000163set_hook(const char *funcname, PyObject **hook_var,
164 PyThreadState **tstate, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000165{
166 PyObject *function = Py_None;
167 char buf[80];
Tim Peters885d4572001-11-28 20:27:42 +0000168 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000169 if (!PyArg_ParseTuple(args, buf, &function))
170 return NULL;
171 if (function == Py_None) {
172 Py_XDECREF(*hook_var);
173 *hook_var = NULL;
174 *tstate = NULL;
175 }
176 else if (PyCallable_Check(function)) {
177 PyObject *tmp = *hook_var;
178 Py_INCREF(function);
179 *hook_var = function;
180 Py_XDECREF(tmp);
Nicholas Bastin2786d902004-03-25 02:16:23 +0000181 *tstate = PyThreadState_GET();
Martin v. Löwis0daad592001-09-30 21:09:59 +0000182 }
183 else {
Tim Peters885d4572001-11-28 20:27:42 +0000184 PyOS_snprintf(buf, sizeof(buf),
185 "set_%.50s(func): argument not callable",
186 funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000187 PyErr_SetString(PyExc_TypeError, buf);
188 return NULL;
189 }
190 Py_INCREF(Py_None);
191 return Py_None;
192}
193
Guido van Rossum74f31432003-01-07 20:01:29 +0000194
Martin v. Löwis0daad592001-09-30 21:09:59 +0000195/* Exported functions to specify hook functions in Python */
196
197static PyObject *startup_hook = NULL;
198static PyThreadState *startup_hook_tstate = NULL;
199
200#ifdef HAVE_RL_PRE_INPUT_HOOK
201static PyObject *pre_input_hook = NULL;
202static PyThreadState *pre_input_hook_tstate = NULL;
203#endif
204
205static PyObject *
206set_startup_hook(PyObject *self, PyObject *args)
207{
Guido van Rossum74f31432003-01-07 20:01:29 +0000208 return set_hook("startup_hook", &startup_hook,
209 &startup_hook_tstate, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000210}
211
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000212PyDoc_STRVAR(doc_set_startup_hook,
213"set_startup_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000214Set or remove the startup_hook function.\n\
215The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000216before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000217
Guido van Rossum74f31432003-01-07 20:01:29 +0000218
Martin v. Löwis0daad592001-09-30 21:09:59 +0000219#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000220
221/* Set pre-input hook */
222
Martin v. Löwis0daad592001-09-30 21:09:59 +0000223static PyObject *
224set_pre_input_hook(PyObject *self, PyObject *args)
225{
Guido van Rossum74f31432003-01-07 20:01:29 +0000226 return set_hook("pre_input_hook", &pre_input_hook,
227 &pre_input_hook_tstate, 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;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000243static PyThreadState *completer_tstate = NULL;
Guido van Rossum290900a1997-09-26 21:51:21 +0000244
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000245static PyObject *begidx = NULL;
246static PyObject *endidx = NULL;
247
Guido van Rossum74f31432003-01-07 20:01:29 +0000248
249/* Get the beginning index for the scope of the tab-completion */
250
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000251static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000252get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000253{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000254 Py_INCREF(begidx);
255 return begidx;
256}
257
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000258PyDoc_STRVAR(doc_get_begidx,
259"get_begidx() -> int\n\
260get the beginning index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000261
Guido van Rossum74f31432003-01-07 20:01:29 +0000262
263/* Get the ending index for the scope of the tab-completion */
264
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000265static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000266get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000267{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000268 Py_INCREF(endidx);
269 return endidx;
270}
271
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000272PyDoc_STRVAR(doc_get_endidx,
273"get_endidx() -> int\n\
274get the ending index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000275
276
Guido van Rossum74f31432003-01-07 20:01:29 +0000277/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000278
279static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000280set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000281{
282 char *break_chars;
283
Guido van Rossum43713e52000-02-29 13:59:29 +0000284 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000285 return NULL;
286 }
Neal Norwitz0e0ee592002-04-21 15:03:18 +0000287 free((void*)rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000288 rl_completer_word_break_characters = strdup(break_chars);
289 Py_INCREF(Py_None);
290 return Py_None;
291}
292
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000293PyDoc_STRVAR(doc_set_completer_delims,
294"set_completer_delims(string) -> None\n\
295set the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000296
Skip Montanaroe5069012004-08-15 14:32:06 +0000297static PyObject *
298py_remove_history(PyObject *self, PyObject *args)
299{
300 int entry_number;
301 HIST_ENTRY *entry;
302
303 if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
304 return NULL;
305 entry = remove_history(entry_number);
306 if (!entry) {
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000307 PyErr_Format(PyExc_ValueError,
308 "No history item at position %d",
309 entry_number);
Skip Montanaroe5069012004-08-15 14:32:06 +0000310 return NULL;
311 }
312 /* free memory allocated for the history entry */
313 if (entry->line)
314 free(entry->line);
315 if (entry->data)
316 free(entry->data);
317 free(entry);
318
319 Py_INCREF(Py_None);
320 return Py_None;
321}
322
323PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000324"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000325remove history item given by its position");
326
327static PyObject *
328py_replace_history(PyObject *self, PyObject *args)
329{
330 int entry_number;
331 char *line;
332 HIST_ENTRY *old_entry;
333
334 if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number, &line)) {
335 return NULL;
336 }
337 old_entry = replace_history_entry(entry_number, line, (void *)NULL);
338 if (!old_entry) {
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000339 PyErr_Format(PyExc_ValueError,
340 "No history item at position %d",
341 entry_number);
Skip Montanaroe5069012004-08-15 14:32:06 +0000342 return NULL;
343 }
344 /* free memory allocated for the old history entry */
345 if (old_entry->line)
346 free(old_entry->line);
347 if (old_entry->data)
348 free(old_entry->data);
349 free(old_entry);
350
351 Py_INCREF(Py_None);
352 return Py_None;
353}
354
355PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000356"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000357replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000358
359/* Add a line to the history buffer */
360
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000361static PyObject *
362py_add_history(PyObject *self, PyObject *args)
363{
364 char *line;
365
366 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
367 return NULL;
368 }
369 add_history(line);
370 Py_INCREF(Py_None);
371 return Py_None;
372}
373
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000374PyDoc_STRVAR(doc_add_history,
375"add_history(string) -> None\n\
376add a line to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000377
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000378
Guido van Rossum74f31432003-01-07 20:01:29 +0000379/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000380
381static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000382get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000383{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000384 return PyString_FromString(rl_completer_word_break_characters);
385}
Guido van Rossum74f31432003-01-07 20:01:29 +0000386
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000387PyDoc_STRVAR(doc_get_completer_delims,
388"get_completer_delims() -> string\n\
389get the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000390
Guido van Rossum74f31432003-01-07 20:01:29 +0000391
392/* Set the completer function */
393
Guido van Rossum290900a1997-09-26 21:51:21 +0000394static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000395set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000396{
Martin v. Löwis0daad592001-09-30 21:09:59 +0000397 return set_hook("completer", &completer, &completer_tstate, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000398}
399
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000400PyDoc_STRVAR(doc_set_completer,
401"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000402Set or remove the completer function.\n\
403The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000404for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000405It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000406
Guido van Rossum74f31432003-01-07 20:01:29 +0000407
Michael W. Hudson796df152003-01-30 10:12:51 +0000408static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000409get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000410{
411 if (completer == NULL) {
412 Py_INCREF(Py_None);
413 return Py_None;
414 }
415 Py_INCREF(completer);
416 return completer;
417}
418
419PyDoc_STRVAR(doc_get_completer,
420"get_completer() -> function\n\
421\n\
422Returns current completer function.");
423
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000424/* Exported function to get any element of history */
425
426static PyObject *
427get_history_item(PyObject *self, PyObject *args)
428{
429 int idx = 0;
430 HIST_ENTRY *hist_ent;
431
432 if (!PyArg_ParseTuple(args, "i:index", &idx))
433 return NULL;
434 if ((hist_ent = history_get(idx)))
Guido van Rossum05ac4492003-01-07 20:04:12 +0000435 return PyString_FromString(hist_ent->line);
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000436 else {
437 Py_INCREF(Py_None);
438 return Py_None;
439 }
440}
441
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000442PyDoc_STRVAR(doc_get_history_item,
443"get_history_item() -> string\n\
444return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000445
Guido van Rossum74f31432003-01-07 20:01:29 +0000446
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000447/* Exported function to get current length of history */
448
449static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000450get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000451{
452 HISTORY_STATE *hist_st;
453
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000454 hist_st = history_get_history_state();
455 return PyInt_FromLong(hist_st ? (long) hist_st->length : (long) 0);
456}
457
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000458PyDoc_STRVAR(doc_get_current_history_length,
459"get_current_history_length() -> integer\n\
460return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000461
Guido van Rossum74f31432003-01-07 20:01:29 +0000462
Guido van Rossum79378ff1997-10-07 14:53:21 +0000463/* Exported function to read the current line buffer */
464
465static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000466get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000467{
Guido van Rossum79378ff1997-10-07 14:53:21 +0000468 return PyString_FromString(rl_line_buffer);
469}
470
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000471PyDoc_STRVAR(doc_get_line_buffer,
472"get_line_buffer() -> string\n\
473return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000474
Guido van Rossum74f31432003-01-07 20:01:29 +0000475
Martin v. Löwise7a97962003-09-20 16:08:33 +0000476#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
477
478/* Exported function to clear the current history */
479
480static PyObject *
481py_clear_history(PyObject *self, PyObject *noarg)
482{
483 clear_history();
484 Py_INCREF(Py_None);
485 return Py_None;
486}
487
488PyDoc_STRVAR(doc_clear_history,
489"clear_history() -> None\n\
490Clear the current readline history.");
491#endif
492
493
Guido van Rossum79378ff1997-10-07 14:53:21 +0000494/* Exported function to insert text into the line buffer */
495
496static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000497insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000498{
499 char *s;
Guido van Rossum43713e52000-02-29 13:59:29 +0000500 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000501 return NULL;
502 rl_insert_text(s);
503 Py_INCREF(Py_None);
504 return Py_None;
505}
506
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000507PyDoc_STRVAR(doc_insert_text,
508"insert_text(string) -> None\n\
509Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000510
Guido van Rossum74f31432003-01-07 20:01:29 +0000511
512/* Redisplay the line buffer */
513
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000514static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000515redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000516{
517 rl_redisplay();
518 Py_INCREF(Py_None);
519 return Py_None;
520}
521
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000522PyDoc_STRVAR(doc_redisplay,
523"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000524Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000525contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000526
Guido van Rossum74f31432003-01-07 20:01:29 +0000527
Guido van Rossum290900a1997-09-26 21:51:21 +0000528/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000529
530static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000531{
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000532 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000533 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000534 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000535 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000536 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000537 {"read_history_file", read_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000538 METH_VARARGS, doc_read_history_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000539 {"write_history_file", write_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000540 METH_VARARGS, doc_write_history_file},
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000541 {"get_history_item", get_history_item,
542 METH_VARARGS, doc_get_history_item},
Neal Norwitz767f8352002-03-31 16:13:39 +0000543 {"get_current_history_length", (PyCFunction)get_current_history_length,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000544 METH_NOARGS, doc_get_current_history_length},
Guido van Rossum74f31432003-01-07 20:01:29 +0000545 {"set_history_length", set_history_length,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000546 METH_VARARGS, set_history_length_doc},
Guido van Rossum74f31432003-01-07 20:01:29 +0000547 {"get_history_length", get_history_length,
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000548 METH_NOARGS, get_history_length_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000549 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
Michael W. Hudson796df152003-01-30 10:12:51 +0000550 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000551 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
552 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000553
Guido van Rossum74f31432003-01-07 20:01:29 +0000554 {"set_completer_delims", set_completer_delims,
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000555 METH_VARARGS, doc_set_completer_delims},
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000556 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
Skip Montanaroe5069012004-08-15 14:32:06 +0000557 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
558 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000559 {"get_completer_delims", get_completer_delims,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000560 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000561
562 {"set_startup_hook", set_startup_hook,
563 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000564#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000565 {"set_pre_input_hook", set_pre_input_hook,
566 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000567#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000568#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
569 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
570#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000571 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000572};
573
Guido van Rossum05ac4492003-01-07 20:04:12 +0000574
Martin v. Löwis0daad592001-09-30 21:09:59 +0000575/* C function to call the Python hooks. */
576
577static int
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000578on_hook(PyObject *func, PyThreadState **tstate)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000579{
580 int result = 0;
581 if (func != NULL) {
582 PyObject *r;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000583 /* Note that readline is called with the interpreter
584 lock released! */
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000585 PyEval_RestoreThread(*tstate);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000586 r = PyObject_CallFunction(func, NULL);
587 if (r == NULL)
588 goto error;
Guido van Rossum74f31432003-01-07 20:01:29 +0000589 if (r == Py_None)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000590 result = 0;
Guido van Rossum74f31432003-01-07 20:01:29 +0000591 else
Martin v. Löwis0daad592001-09-30 21:09:59 +0000592 result = PyInt_AsLong(r);
593 Py_DECREF(r);
594 goto done;
595 error:
596 PyErr_Clear();
597 Py_XDECREF(r);
598 done:
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000599 *tstate = PyEval_SaveThread();
Martin v. Löwis0daad592001-09-30 21:09:59 +0000600 }
601 return result;
602}
603
604static int
605on_startup_hook(void)
606{
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000607 return on_hook(startup_hook, &startup_hook_tstate);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000608}
609
610#ifdef HAVE_RL_PRE_INPUT_HOOK
611static int
612on_pre_input_hook(void)
613{
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000614 return on_hook(pre_input_hook, &pre_input_hook_tstate);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000615}
616#endif
617
Guido van Rossum05ac4492003-01-07 20:04:12 +0000618
Guido van Rossum290900a1997-09-26 21:51:21 +0000619/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000620
Guido van Rossum290900a1997-09-26 21:51:21 +0000621static char *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000622on_completion(char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000623{
Guido van Rossum290900a1997-09-26 21:51:21 +0000624 char *result = NULL;
625 if (completer != NULL) {
626 PyObject *r;
627 /* Note that readline is called with the interpreter
628 lock released! */
Martin v. Löwis0daad592001-09-30 21:09:59 +0000629 PyEval_RestoreThread(completer_tstate);
Michael W. Hudson0c1ceaf2002-02-13 11:58:25 +0000630 /* Don't use the default filename completion if we
631 * have a custom completion function... */
632 rl_attempted_completion_over = 1;
Guido van Rossum290900a1997-09-26 21:51:21 +0000633 r = PyObject_CallFunction(completer, "si", text, state);
634 if (r == NULL)
635 goto error;
636 if (r == Py_None) {
637 result = NULL;
638 }
639 else {
640 char *s = PyString_AsString(r);
641 if (s == NULL)
642 goto error;
643 result = strdup(s);
644 }
645 Py_DECREF(r);
646 goto done;
647 error:
648 PyErr_Clear();
649 Py_XDECREF(r);
650 done:
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000651 completer_tstate = PyEval_SaveThread();
Guido van Rossum290900a1997-09-26 21:51:21 +0000652 }
653 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000654}
655
Guido van Rossum290900a1997-09-26 21:51:21 +0000656
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000657/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000658 * before calling the normal completer */
659
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000660static char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000661flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000662{
663 Py_XDECREF(begidx);
664 Py_XDECREF(endidx);
665 begidx = PyInt_FromLong((long) start);
666 endidx = PyInt_FromLong((long) end);
667 return completion_matches(text, *on_completion);
668}
669
Guido van Rossum05ac4492003-01-07 20:04:12 +0000670
Guido van Rossum290900a1997-09-26 21:51:21 +0000671/* Helper to initialize GNU readline properly. */
672
673static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000674setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000675{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000676#ifdef SAVE_LOCALE
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000677 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwis701abe72004-08-20 06:26:59 +0000678 if (!saved_locale)
679 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000680#endif
681
Skip Montanaroa0392742002-06-11 14:32:46 +0000682 using_history();
683
Guido van Rossum290900a1997-09-26 21:51:21 +0000684 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000685#if defined(PYOS_OS2) && defined(PYCC_GCC)
686 /* Allow $if term= in .inputrc to work */
687 rl_terminal_name = getenv("TERM");
688#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000689 /* Force rebind of TAB to insert-tab */
690 rl_bind_key('\t', rl_insert);
691 /* Bind both ESC-TAB and ESC-ESC to the completion function */
692 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
693 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000694 /* Set our hook functions */
695 rl_startup_hook = (Function *)on_startup_hook;
696#ifdef HAVE_RL_PRE_INPUT_HOOK
697 rl_pre_input_hook = (Function *)on_pre_input_hook;
698#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000699 /* Set our completion function */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000700 rl_attempted_completion_function = (CPPFunction *)flex_complete;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000701 /* Set Python word break characters */
702 rl_completer_word_break_characters =
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000703 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000704 /* All nonalphanums except '.' */
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000705#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum84271bb2002-05-30 15:41:56 +0000706 rl_completion_append_character ='\0';
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000707#endif
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000708
709 begidx = PyInt_FromLong(0L);
710 endidx = PyInt_FromLong(0L);
Barry Warsawf7612871999-01-29 21:55:03 +0000711 /* Initialize (allows .inputrc to override)
712 *
713 * XXX: A bug in the readline-2.2 library causes a memory leak
714 * inside this function. Nothing we can do about it.
715 */
Guido van Rossum290900a1997-09-26 21:51:21 +0000716 rl_initialize();
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000717
718#ifdef SAVE_LOCALE
719 setlocale(LC_CTYPE, saved_locale); /* Restore locale */
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000720 free(saved_locale);
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000721#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000722}
723
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000724/* Wrapper around GNU readline that handles signals differently. */
725
726
727#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
728
729static char *completed_input_string;
730static void
731rlhandler(char *text)
732{
733 completed_input_string = text;
734 rl_callback_handler_remove();
735}
736
737extern PyThreadState* _PyOS_ReadlineTState;
738
739static char *
740readline_until_enter_or_signal(char *prompt, int *signal)
741{
742 char * not_done_reading = "";
743 fd_set selectset;
744
745 *signal = 0;
746#ifdef HAVE_RL_CATCH_SIGNAL
747 rl_catch_signals = 0;
748#endif
749
750 rl_callback_handler_install (prompt, rlhandler);
751 FD_ZERO(&selectset);
752 FD_SET(fileno(rl_instream), &selectset);
753
754 completed_input_string = not_done_reading;
755
756 while(completed_input_string == not_done_reading) {
757 int has_input;
758
759 has_input = select(fileno(rl_instream) + 1, &selectset,
760 NULL, NULL, NULL);
761 if(has_input > 0) {
762 rl_callback_read_char();
763 }
764 else if (errno == EINTR) {
765 int s;
766 PyEval_RestoreThread(_PyOS_ReadlineTState);
767 s = PyErr_CheckSignals();
Michael W. Hudson23849902004-07-08 15:28:26 +0000768 PyEval_SaveThread();
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000769 if (s < 0) {
770 rl_free_line_state();
771 rl_cleanup_after_signal();
772 rl_callback_handler_remove();
773 *signal = 1;
774 completed_input_string = NULL;
775 }
776 }
777 }
778
779 return completed_input_string;
780}
781
782
783#else
Guido van Rossum290900a1997-09-26 21:51:21 +0000784
785/* Interrupt handler */
786
787static jmp_buf jbuf;
788
Guido van Rossum0969d361997-08-05 21:27:50 +0000789/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +0000790static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000791onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000792{
Guido van Rossum290900a1997-09-26 21:51:21 +0000793 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000794}
795
Guido van Rossum290900a1997-09-26 21:51:21 +0000796
Guido van Rossum0969d361997-08-05 21:27:50 +0000797static char *
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000798readline_until_enter_or_signal(char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +0000799{
Guido van Rossum174efc92000-09-16 16:37:53 +0000800 PyOS_sighandler_t old_inthandler;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000801 char *p;
802
803 *signal = 0;
Guido van Rossum74f31432003-01-07 20:01:29 +0000804
Guido van Rossum174efc92000-09-16 16:37:53 +0000805 old_inthandler = PyOS_setsig(SIGINT, onintr);
Guido van Rossum0969d361997-08-05 21:27:50 +0000806 if (setjmp(jbuf)) {
807#ifdef HAVE_SIGRELSE
808 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
809 sigrelse(SIGINT);
810#endif
Guido van Rossum174efc92000-09-16 16:37:53 +0000811 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000812 *signal = 1;
Guido van Rossum0969d361997-08-05 21:27:50 +0000813 return NULL;
814 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000815 p = readline(prompt);
816 PyOS_setsig(SIGINT, old_inthandler);
817
818 return p;
819}
820#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
821
822
823static char *
824call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
825{
Neal Norwitz1fa040b2004-08-25 01:20:18 +0000826 size_t n;
827 char *p, *q;
828 int signal;
829
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000830#ifdef SAVE_LOCALE
831 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwis701abe72004-08-20 06:26:59 +0000832 if (!saved_locale)
833 Py_FatalError("not enough memory to save locale");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000834 setlocale(LC_CTYPE, "");
835#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000836
Guido van Rossum44620641997-08-11 18:57:29 +0000837 rl_event_hook = PyOS_InputHook;
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000838
Guido van Rossum74f31432003-01-07 20:01:29 +0000839 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
840 rl_instream = sys_stdin;
841 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000842#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum74f31432003-01-07 20:01:29 +0000843 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000844#endif
Guido van Rossum74f31432003-01-07 20:01:29 +0000845 }
846
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000847 p = readline_until_enter_or_signal(prompt, &signal);
848
849 /* we got an interrupt signal */
850 if(signal) {
851 return NULL;
852 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000853
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000854 /* We got an EOF, return a empty string. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000855 if (p == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000856 p = PyMem_Malloc(1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000857 if (p != NULL)
858 *p = '\0';
859 return p;
860 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000861
862 /* we have a valid line */
Guido van Rossum0969d361997-08-05 21:27:50 +0000863 n = strlen(p);
Skip Montanaroa0392742002-06-11 14:32:46 +0000864 if (n > 0) {
865 char *line;
866 HISTORY_STATE *state = history_get_history_state();
867 if (state->length > 0)
868 line = history_get(state->length)->line;
869 else
870 line = "";
871 if (strcmp(p, line))
872 add_history(p);
873 /* the history docs don't say so, but the address of state
874 changes each time history_get_history_state is called
875 which makes me think it's freshly malloc'd memory...
876 on the other hand, the address of the last line stays the
877 same as long as history isn't extended, so it appears to
878 be malloc'd but managed by the history package... */
879 free(state);
880 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000881 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
882 release the original. */
883 q = p;
884 p = PyMem_Malloc(n+2);
885 if (p != NULL) {
886 strncpy(p, q, n);
Guido van Rossum0969d361997-08-05 21:27:50 +0000887 p[n] = '\n';
888 p[n+1] = '\0';
889 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000890 free(q);
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000891#ifdef SAVE_LOCALE
892 setlocale(LC_CTYPE, saved_locale); /* Restore locale */
893 free(saved_locale);
894#endif
Guido van Rossum0969d361997-08-05 21:27:50 +0000895 return p;
896}
897
Guido van Rossum290900a1997-09-26 21:51:21 +0000898
899/* Initialize the module */
900
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000901PyDoc_STRVAR(doc_module,
902"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000903
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000904PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000905initreadline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000906{
Guido van Rossum1ea64ea2000-10-02 15:53:08 +0000907 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +0000908
909 m = Py_InitModule4("readline", readline_methods, doc_module,
910 (PyObject *)NULL, PYTHON_API_VERSION);
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000911
Guido van Rossum74f31432003-01-07 20:01:29 +0000912 PyOS_ReadlineFunctionPointer = call_readline;
913 setup_readline();
Guido van Rossum0969d361997-08-05 21:27:50 +0000914}