blob: 3377c8eae484f7cb0ab5d1d6c499b64810961fe9 [file] [log] [blame]
Guido van Rossum290900a1997-09-26 21:51:21 +00001/* This module makes GNU readline available to Python. It has ideas
2 * contributed by Lee Busby, LLNL, and William Magro, Cornell Theory
3 * Center. The completer interface was inspired by Lele Gaifax.
4 *
5 * More recently, it was largely rewritten by Guido van Rossum who is
6 * now maintaining it.
Guido van Rossum0969d361997-08-05 21:27:50 +00007 */
8
Guido van Rossum290900a1997-09-26 21:51:21 +00009/* Standard definitions */
Guido van Rossum0969d361997-08-05 21:27:50 +000010#include "Python.h"
11#include <setjmp.h>
12#include <signal.h>
Guido van Rossum290900a1997-09-26 21:51:21 +000013#include <errno.h>
Michael W. Hudson8da2b012004-10-07 13:46:33 +000014#include <sys/time.h>
Guido van Rossum0969d361997-08-05 21:27:50 +000015
Skip Montanaro7befb992004-02-10 16:50:21 +000016#if defined(HAVE_SETLOCALE)
Guido van Rossum60c8a3a2002-10-09 21:27:33 +000017/* GNU readline() mistakenly sets the LC_CTYPE locale.
18 * This is evil. Only the user or the app's main() should do this!
19 * We must save and restore the locale around the rl_initialize() call.
20 */
21#define SAVE_LOCALE
22#include <locale.h>
23#endif
24
Guido van Rossum290900a1997-09-26 21:51:21 +000025/* GNU readline definitions */
Guido van Rossumb0e51b22001-04-13 18:14:27 +000026#undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
Guido van Rossumbcc20741998-08-04 22:53:56 +000027#include <readline/readline.h>
28#include <readline/history.h>
Guido van Rossum730806d1998-04-10 22:27:42 +000029
Guido van Rossum353ae582001-07-10 16:45:32 +000030#ifdef HAVE_RL_COMPLETION_MATCHES
Guido van Rossum74f31432003-01-07 20:01:29 +000031#define completion_matches(x, y) \
32 rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
Guido van Rossum353ae582001-07-10 16:45:32 +000033#endif
34
Guido van Rossum0969d361997-08-05 21:27:50 +000035
Guido van Rossum290900a1997-09-26 21:51:21 +000036/* Exported function to send one line to readline's init file parser */
37
38static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000039parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000040{
Guido van Rossum3b5330e1998-12-04 15:34:39 +000041 char *s, *copy;
Guido van Rossum43713e52000-02-29 13:59:29 +000042 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000043 return NULL;
Guido van Rossum3b5330e1998-12-04 15:34:39 +000044 /* Make a copy -- rl_parse_and_bind() modifies its argument */
45 /* Bernard Herzog */
46 copy = malloc(1 + strlen(s));
47 if (copy == NULL)
48 return PyErr_NoMemory();
49 strcpy(copy, s);
50 rl_parse_and_bind(copy);
51 free(copy); /* Free the copy */
Guido van Rossum290900a1997-09-26 21:51:21 +000052 Py_INCREF(Py_None);
53 return Py_None;
54}
55
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000056PyDoc_STRVAR(doc_parse_and_bind,
57"parse_and_bind(string) -> None\n\
58Parse and execute single line of a readline init file.");
Guido van Rossum290900a1997-09-26 21:51:21 +000059
60
61/* Exported function to parse a readline init file */
62
63static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000064read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000065{
66 char *s = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +000067 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000068 return NULL;
69 errno = rl_read_init_file(s);
70 if (errno)
71 return PyErr_SetFromErrno(PyExc_IOError);
72 Py_INCREF(Py_None);
73 return Py_None;
74}
75
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000076PyDoc_STRVAR(doc_read_init_file,
77"read_init_file([filename]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +000078Parse a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000079The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +000080
81
Skip Montanaro28067822000-07-06 18:55:12 +000082/* Exported function to load a readline history file */
83
84static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000085read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +000086{
87 char *s = NULL;
88 if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
89 return NULL;
90 errno = read_history(s);
91 if (errno)
92 return PyErr_SetFromErrno(PyExc_IOError);
93 Py_INCREF(Py_None);
94 return Py_None;
95}
96
Skip Montanaro49bd24d2000-07-19 16:54:53 +000097static int history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000098PyDoc_STRVAR(doc_read_history_file,
99"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000100Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000101The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000102
103
104/* Exported function to save a readline history file */
105
106static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000107write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000108{
109 char *s = NULL;
110 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
111 return NULL;
112 errno = write_history(s);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000113 if (!errno && history_length >= 0)
114 history_truncate_file(s, history_length);
Skip Montanaro28067822000-07-06 18:55:12 +0000115 if (errno)
116 return PyErr_SetFromErrno(PyExc_IOError);
117 Py_INCREF(Py_None);
118 return Py_None;
119}
120
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000121PyDoc_STRVAR(doc_write_history_file,
122"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000123Save a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000124The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000125
126
Guido van Rossum74f31432003-01-07 20:01:29 +0000127/* Set history length */
128
129static PyObject*
130set_history_length(PyObject *self, PyObject *args)
131{
132 int length = history_length;
133 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
134 return NULL;
135 history_length = length;
136 Py_INCREF(Py_None);
137 return Py_None;
138}
139
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000140PyDoc_STRVAR(set_history_length_doc,
141"set_history_length(length) -> None\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000142set the maximal number of items which will be written to\n\
143the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000144history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000145
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000146
Guido van Rossum74f31432003-01-07 20:01:29 +0000147/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000148
149static PyObject*
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000150get_history_length(PyObject *self, PyObject *noarg)
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000151{
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000152 return PyInt_FromLong(history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000153}
154
Guido van Rossum74f31432003-01-07 20:01:29 +0000155PyDoc_STRVAR(get_history_length_doc,
156"get_history_length() -> int\n\
157return the maximum number of items that will be written to\n\
158the history file.");
159
160
Martin v. Löwis0daad592001-09-30 21:09:59 +0000161/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000162
Martin v. Löwis0daad592001-09-30 21:09:59 +0000163static PyObject *
Guido van Rossum74f31432003-01-07 20:01:29 +0000164set_hook(const char *funcname, PyObject **hook_var,
165 PyThreadState **tstate, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000166{
167 PyObject *function = Py_None;
168 char buf[80];
Tim Peters885d4572001-11-28 20:27:42 +0000169 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000170 if (!PyArg_ParseTuple(args, buf, &function))
171 return NULL;
172 if (function == Py_None) {
173 Py_XDECREF(*hook_var);
174 *hook_var = NULL;
175 *tstate = NULL;
176 }
177 else if (PyCallable_Check(function)) {
178 PyObject *tmp = *hook_var;
179 Py_INCREF(function);
180 *hook_var = function;
181 Py_XDECREF(tmp);
Nicholas Bastin2786d902004-03-25 02:16:23 +0000182 *tstate = PyThreadState_GET();
Martin v. Löwis0daad592001-09-30 21:09:59 +0000183 }
184 else {
Tim Peters885d4572001-11-28 20:27:42 +0000185 PyOS_snprintf(buf, sizeof(buf),
186 "set_%.50s(func): argument not callable",
187 funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000188 PyErr_SetString(PyExc_TypeError, buf);
189 return NULL;
190 }
191 Py_INCREF(Py_None);
192 return Py_None;
193}
194
Guido van Rossum74f31432003-01-07 20:01:29 +0000195
Martin v. Löwis0daad592001-09-30 21:09:59 +0000196/* Exported functions to specify hook functions in Python */
197
198static PyObject *startup_hook = NULL;
199static PyThreadState *startup_hook_tstate = NULL;
200
201#ifdef HAVE_RL_PRE_INPUT_HOOK
202static PyObject *pre_input_hook = NULL;
203static PyThreadState *pre_input_hook_tstate = NULL;
204#endif
205
206static PyObject *
207set_startup_hook(PyObject *self, PyObject *args)
208{
Guido van Rossum74f31432003-01-07 20:01:29 +0000209 return set_hook("startup_hook", &startup_hook,
210 &startup_hook_tstate, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000211}
212
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000213PyDoc_STRVAR(doc_set_startup_hook,
214"set_startup_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000215Set or remove the startup_hook function.\n\
216The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000217before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000218
Guido van Rossum74f31432003-01-07 20:01:29 +0000219
Martin v. Löwis0daad592001-09-30 21:09:59 +0000220#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000221
222/* Set pre-input hook */
223
Martin v. Löwis0daad592001-09-30 21:09:59 +0000224static PyObject *
225set_pre_input_hook(PyObject *self, PyObject *args)
226{
Guido van Rossum74f31432003-01-07 20:01:29 +0000227 return set_hook("pre_input_hook", &pre_input_hook,
228 &pre_input_hook_tstate, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000229}
230
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000231PyDoc_STRVAR(doc_set_pre_input_hook,
232"set_pre_input_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000233Set or remove the pre_input_hook function.\n\
234The function is called with no arguments after the first prompt\n\
235has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000236characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000237
Martin v. Löwis0daad592001-09-30 21:09:59 +0000238#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000239
Guido van Rossum74f31432003-01-07 20:01:29 +0000240
Guido van Rossum290900a1997-09-26 21:51:21 +0000241/* Exported function to specify a word completer in Python */
242
243static PyObject *completer = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000244static PyThreadState *completer_tstate = NULL;
Guido van Rossum290900a1997-09-26 21:51:21 +0000245
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000246static PyObject *begidx = NULL;
247static PyObject *endidx = NULL;
248
Guido van Rossum74f31432003-01-07 20:01:29 +0000249
250/* Get the beginning index for the scope of the tab-completion */
251
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000252static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000253get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000254{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000255 Py_INCREF(begidx);
256 return begidx;
257}
258
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000259PyDoc_STRVAR(doc_get_begidx,
260"get_begidx() -> int\n\
261get the beginning index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000262
Guido van Rossum74f31432003-01-07 20:01:29 +0000263
264/* Get the ending index for the scope of the tab-completion */
265
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000266static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000267get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000268{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000269 Py_INCREF(endidx);
270 return endidx;
271}
272
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000273PyDoc_STRVAR(doc_get_endidx,
274"get_endidx() -> int\n\
275get the ending index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000276
277
Guido van Rossum74f31432003-01-07 20:01:29 +0000278/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000279
280static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000281set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000282{
283 char *break_chars;
284
Guido van Rossum43713e52000-02-29 13:59:29 +0000285 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000286 return NULL;
287 }
Neal Norwitz0e0ee592002-04-21 15:03:18 +0000288 free((void*)rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000289 rl_completer_word_break_characters = strdup(break_chars);
290 Py_INCREF(Py_None);
291 return Py_None;
292}
293
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000294PyDoc_STRVAR(doc_set_completer_delims,
295"set_completer_delims(string) -> None\n\
296set the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000297
Skip Montanaroe5069012004-08-15 14:32:06 +0000298static PyObject *
299py_remove_history(PyObject *self, PyObject *args)
300{
301 int entry_number;
302 HIST_ENTRY *entry;
303
304 if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
305 return NULL;
306 entry = remove_history(entry_number);
307 if (!entry) {
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000308 PyErr_Format(PyExc_ValueError,
309 "No history item at position %d",
310 entry_number);
Skip Montanaroe5069012004-08-15 14:32:06 +0000311 return NULL;
312 }
313 /* free memory allocated for the history entry */
314 if (entry->line)
315 free(entry->line);
316 if (entry->data)
317 free(entry->data);
318 free(entry);
319
320 Py_INCREF(Py_None);
321 return Py_None;
322}
323
324PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000325"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000326remove history item given by its position");
327
328static PyObject *
329py_replace_history(PyObject *self, PyObject *args)
330{
331 int entry_number;
332 char *line;
333 HIST_ENTRY *old_entry;
334
335 if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number, &line)) {
336 return NULL;
337 }
338 old_entry = replace_history_entry(entry_number, line, (void *)NULL);
339 if (!old_entry) {
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000340 PyErr_Format(PyExc_ValueError,
341 "No history item at position %d",
342 entry_number);
Skip Montanaroe5069012004-08-15 14:32:06 +0000343 return NULL;
344 }
345 /* free memory allocated for the old history entry */
346 if (old_entry->line)
347 free(old_entry->line);
348 if (old_entry->data)
349 free(old_entry->data);
350 free(old_entry);
351
352 Py_INCREF(Py_None);
353 return Py_None;
354}
355
356PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000357"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000358replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000359
360/* Add a line to the history buffer */
361
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000362static PyObject *
363py_add_history(PyObject *self, PyObject *args)
364{
365 char *line;
366
367 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
368 return NULL;
369 }
370 add_history(line);
371 Py_INCREF(Py_None);
372 return Py_None;
373}
374
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000375PyDoc_STRVAR(doc_add_history,
376"add_history(string) -> None\n\
377add a line to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000378
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000379
Guido van Rossum74f31432003-01-07 20:01:29 +0000380/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000381
382static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000383get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000384{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000385 return PyString_FromString(rl_completer_word_break_characters);
386}
Guido van Rossum74f31432003-01-07 20:01:29 +0000387
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000388PyDoc_STRVAR(doc_get_completer_delims,
389"get_completer_delims() -> string\n\
390get the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000391
Guido van Rossum74f31432003-01-07 20:01:29 +0000392
393/* Set the completer function */
394
Guido van Rossum290900a1997-09-26 21:51:21 +0000395static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000396set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000397{
Martin v. Löwis0daad592001-09-30 21:09:59 +0000398 return set_hook("completer", &completer, &completer_tstate, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000399}
400
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000401PyDoc_STRVAR(doc_set_completer,
402"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000403Set or remove the completer function.\n\
404The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000405for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000406It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000407
Guido van Rossum74f31432003-01-07 20:01:29 +0000408
Michael W. Hudson796df152003-01-30 10:12:51 +0000409static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000410get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000411{
412 if (completer == NULL) {
413 Py_INCREF(Py_None);
414 return Py_None;
415 }
416 Py_INCREF(completer);
417 return completer;
418}
419
420PyDoc_STRVAR(doc_get_completer,
421"get_completer() -> function\n\
422\n\
423Returns current completer function.");
424
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000425/* Exported function to get any element of history */
426
427static PyObject *
428get_history_item(PyObject *self, PyObject *args)
429{
430 int idx = 0;
431 HIST_ENTRY *hist_ent;
432
433 if (!PyArg_ParseTuple(args, "i:index", &idx))
434 return NULL;
435 if ((hist_ent = history_get(idx)))
Guido van Rossum05ac4492003-01-07 20:04:12 +0000436 return PyString_FromString(hist_ent->line);
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000437 else {
438 Py_INCREF(Py_None);
439 return Py_None;
440 }
441}
442
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000443PyDoc_STRVAR(doc_get_history_item,
444"get_history_item() -> string\n\
445return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000446
Guido van Rossum74f31432003-01-07 20:01:29 +0000447
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000448/* Exported function to get current length of history */
449
450static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000451get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000452{
453 HISTORY_STATE *hist_st;
454
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000455 hist_st = history_get_history_state();
456 return PyInt_FromLong(hist_st ? (long) hist_st->length : (long) 0);
457}
458
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000459PyDoc_STRVAR(doc_get_current_history_length,
460"get_current_history_length() -> integer\n\
461return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000462
Guido van Rossum74f31432003-01-07 20:01:29 +0000463
Guido van Rossum79378ff1997-10-07 14:53:21 +0000464/* Exported function to read the current line buffer */
465
466static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000467get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000468{
Guido van Rossum79378ff1997-10-07 14:53:21 +0000469 return PyString_FromString(rl_line_buffer);
470}
471
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000472PyDoc_STRVAR(doc_get_line_buffer,
473"get_line_buffer() -> string\n\
474return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000475
Guido van Rossum74f31432003-01-07 20:01:29 +0000476
Martin v. Löwise7a97962003-09-20 16:08:33 +0000477#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
478
479/* Exported function to clear the current history */
480
481static PyObject *
482py_clear_history(PyObject *self, PyObject *noarg)
483{
484 clear_history();
485 Py_INCREF(Py_None);
486 return Py_None;
487}
488
489PyDoc_STRVAR(doc_clear_history,
490"clear_history() -> None\n\
491Clear the current readline history.");
492#endif
493
494
Guido van Rossum79378ff1997-10-07 14:53:21 +0000495/* Exported function to insert text into the line buffer */
496
497static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000498insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000499{
500 char *s;
Guido van Rossum43713e52000-02-29 13:59:29 +0000501 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000502 return NULL;
503 rl_insert_text(s);
504 Py_INCREF(Py_None);
505 return Py_None;
506}
507
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000508PyDoc_STRVAR(doc_insert_text,
509"insert_text(string) -> None\n\
510Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000511
Guido van Rossum74f31432003-01-07 20:01:29 +0000512
513/* Redisplay the line buffer */
514
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000515static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000516redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000517{
518 rl_redisplay();
519 Py_INCREF(Py_None);
520 return Py_None;
521}
522
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000523PyDoc_STRVAR(doc_redisplay,
524"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000525Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000526contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000527
Guido van Rossum74f31432003-01-07 20:01:29 +0000528
Guido van Rossum290900a1997-09-26 21:51:21 +0000529/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000530
531static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000532{
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000533 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000534 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000535 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000536 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000537 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000538 {"read_history_file", read_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000539 METH_VARARGS, doc_read_history_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000540 {"write_history_file", write_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000541 METH_VARARGS, doc_write_history_file},
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000542 {"get_history_item", get_history_item,
543 METH_VARARGS, doc_get_history_item},
Neal Norwitz767f8352002-03-31 16:13:39 +0000544 {"get_current_history_length", (PyCFunction)get_current_history_length,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000545 METH_NOARGS, doc_get_current_history_length},
Guido van Rossum74f31432003-01-07 20:01:29 +0000546 {"set_history_length", set_history_length,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000547 METH_VARARGS, set_history_length_doc},
Guido van Rossum74f31432003-01-07 20:01:29 +0000548 {"get_history_length", get_history_length,
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000549 METH_NOARGS, get_history_length_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000550 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
Michael W. Hudson796df152003-01-30 10:12:51 +0000551 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000552 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
553 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000554
Guido van Rossum74f31432003-01-07 20:01:29 +0000555 {"set_completer_delims", set_completer_delims,
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000556 METH_VARARGS, doc_set_completer_delims},
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000557 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
Skip Montanaroe5069012004-08-15 14:32:06 +0000558 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
559 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000560 {"get_completer_delims", get_completer_delims,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000561 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000562
563 {"set_startup_hook", set_startup_hook,
564 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000565#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000566 {"set_pre_input_hook", set_pre_input_hook,
567 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000568#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000569#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
570 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
571#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000572 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000573};
574
Guido van Rossum05ac4492003-01-07 20:04:12 +0000575
Martin v. Löwis0daad592001-09-30 21:09:59 +0000576/* C function to call the Python hooks. */
577
578static int
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000579on_hook(PyObject *func, PyThreadState **tstate)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000580{
581 int result = 0;
582 if (func != NULL) {
583 PyObject *r;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000584 /* Note that readline is called with the interpreter
585 lock released! */
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000586 PyEval_RestoreThread(*tstate);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000587 r = PyObject_CallFunction(func, NULL);
588 if (r == NULL)
589 goto error;
Guido van Rossum74f31432003-01-07 20:01:29 +0000590 if (r == Py_None)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000591 result = 0;
Guido van Rossum74f31432003-01-07 20:01:29 +0000592 else
Martin v. Löwis0daad592001-09-30 21:09:59 +0000593 result = PyInt_AsLong(r);
594 Py_DECREF(r);
595 goto done;
596 error:
597 PyErr_Clear();
598 Py_XDECREF(r);
599 done:
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000600 *tstate = PyEval_SaveThread();
Martin v. Löwis0daad592001-09-30 21:09:59 +0000601 }
602 return result;
603}
604
605static int
606on_startup_hook(void)
607{
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000608 return on_hook(startup_hook, &startup_hook_tstate);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000609}
610
611#ifdef HAVE_RL_PRE_INPUT_HOOK
612static int
613on_pre_input_hook(void)
614{
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000615 return on_hook(pre_input_hook, &pre_input_hook_tstate);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000616}
617#endif
618
Guido van Rossum05ac4492003-01-07 20:04:12 +0000619
Guido van Rossum290900a1997-09-26 21:51:21 +0000620/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000621
Guido van Rossum290900a1997-09-26 21:51:21 +0000622static char *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000623on_completion(char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000624{
Guido van Rossum290900a1997-09-26 21:51:21 +0000625 char *result = NULL;
626 if (completer != NULL) {
627 PyObject *r;
628 /* Note that readline is called with the interpreter
629 lock released! */
Martin v. Löwis0daad592001-09-30 21:09:59 +0000630 PyEval_RestoreThread(completer_tstate);
Michael W. Hudson0c1ceaf2002-02-13 11:58:25 +0000631 /* Don't use the default filename completion if we
632 * have a custom completion function... */
633 rl_attempted_completion_over = 1;
Guido van Rossum290900a1997-09-26 21:51:21 +0000634 r = PyObject_CallFunction(completer, "si", text, state);
635 if (r == NULL)
636 goto error;
637 if (r == Py_None) {
638 result = NULL;
639 }
640 else {
641 char *s = PyString_AsString(r);
642 if (s == NULL)
643 goto error;
644 result = strdup(s);
645 }
646 Py_DECREF(r);
647 goto done;
648 error:
649 PyErr_Clear();
650 Py_XDECREF(r);
651 done:
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000652 completer_tstate = PyEval_SaveThread();
Guido van Rossum290900a1997-09-26 21:51:21 +0000653 }
654 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000655}
656
Guido van Rossum290900a1997-09-26 21:51:21 +0000657
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000658/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000659 * before calling the normal completer */
660
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000661static char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000662flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000663{
664 Py_XDECREF(begidx);
665 Py_XDECREF(endidx);
666 begidx = PyInt_FromLong((long) start);
667 endidx = PyInt_FromLong((long) end);
668 return completion_matches(text, *on_completion);
669}
670
Guido van Rossum05ac4492003-01-07 20:04:12 +0000671
Guido van Rossum290900a1997-09-26 21:51:21 +0000672/* Helper to initialize GNU readline properly. */
673
674static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000675setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000676{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000677#ifdef SAVE_LOCALE
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000678 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwis701abe72004-08-20 06:26:59 +0000679 if (!saved_locale)
680 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000681#endif
682
Skip Montanaroa0392742002-06-11 14:32:46 +0000683 using_history();
684
Guido van Rossum290900a1997-09-26 21:51:21 +0000685 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000686#if defined(PYOS_OS2) && defined(PYCC_GCC)
687 /* Allow $if term= in .inputrc to work */
688 rl_terminal_name = getenv("TERM");
689#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000690 /* Force rebind of TAB to insert-tab */
691 rl_bind_key('\t', rl_insert);
692 /* Bind both ESC-TAB and ESC-ESC to the completion function */
693 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
694 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000695 /* Set our hook functions */
696 rl_startup_hook = (Function *)on_startup_hook;
697#ifdef HAVE_RL_PRE_INPUT_HOOK
698 rl_pre_input_hook = (Function *)on_pre_input_hook;
699#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000700 /* Set our completion function */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000701 rl_attempted_completion_function = (CPPFunction *)flex_complete;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000702 /* Set Python word break characters */
703 rl_completer_word_break_characters =
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000704 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000705 /* All nonalphanums except '.' */
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000706#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum84271bb2002-05-30 15:41:56 +0000707 rl_completion_append_character ='\0';
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000708#endif
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000709
710 begidx = PyInt_FromLong(0L);
711 endidx = PyInt_FromLong(0L);
Barry Warsawf7612871999-01-29 21:55:03 +0000712 /* Initialize (allows .inputrc to override)
713 *
714 * XXX: A bug in the readline-2.2 library causes a memory leak
715 * inside this function. Nothing we can do about it.
716 */
Guido van Rossum290900a1997-09-26 21:51:21 +0000717 rl_initialize();
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000718
719#ifdef SAVE_LOCALE
720 setlocale(LC_CTYPE, saved_locale); /* Restore locale */
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000721 free(saved_locale);
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000722#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000723}
724
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000725/* Wrapper around GNU readline that handles signals differently. */
726
727
728#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
729
730static char *completed_input_string;
731static void
732rlhandler(char *text)
733{
734 completed_input_string = text;
735 rl_callback_handler_remove();
736}
737
738extern PyThreadState* _PyOS_ReadlineTState;
739
740static char *
741readline_until_enter_or_signal(char *prompt, int *signal)
742{
743 char * not_done_reading = "";
744 fd_set selectset;
745
746 *signal = 0;
747#ifdef HAVE_RL_CATCH_SIGNAL
748 rl_catch_signals = 0;
749#endif
750
751 rl_callback_handler_install (prompt, rlhandler);
752 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000753
754 completed_input_string = not_done_reading;
755
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000756 while (completed_input_string == not_done_reading) {
757 int has_input = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000758
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000759 while (!has_input)
760 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
761 FD_SET(fileno(rl_instream), &selectset);
762 /* select resets selectset if no input was available */
763 has_input = select(fileno(rl_instream) + 1, &selectset,
764 NULL, NULL, &timeout);
765 if(PyOS_InputHook) PyOS_InputHook();
766 }
767
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000768 if(has_input > 0) {
769 rl_callback_read_char();
770 }
771 else if (errno == EINTR) {
772 int s;
773 PyEval_RestoreThread(_PyOS_ReadlineTState);
774 s = PyErr_CheckSignals();
Michael W. Hudson23849902004-07-08 15:28:26 +0000775 PyEval_SaveThread();
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000776 if (s < 0) {
777 rl_free_line_state();
778 rl_cleanup_after_signal();
779 rl_callback_handler_remove();
780 *signal = 1;
781 completed_input_string = NULL;
782 }
783 }
784 }
785
786 return completed_input_string;
787}
788
789
790#else
Guido van Rossum290900a1997-09-26 21:51:21 +0000791
792/* Interrupt handler */
793
794static jmp_buf jbuf;
795
Guido van Rossum0969d361997-08-05 21:27:50 +0000796/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +0000797static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000798onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000799{
Guido van Rossum290900a1997-09-26 21:51:21 +0000800 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000801}
802
Guido van Rossum290900a1997-09-26 21:51:21 +0000803
Guido van Rossum0969d361997-08-05 21:27:50 +0000804static char *
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000805readline_until_enter_or_signal(char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +0000806{
Guido van Rossum174efc92000-09-16 16:37:53 +0000807 PyOS_sighandler_t old_inthandler;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000808 char *p;
809
810 *signal = 0;
Guido van Rossum74f31432003-01-07 20:01:29 +0000811
Guido van Rossum174efc92000-09-16 16:37:53 +0000812 old_inthandler = PyOS_setsig(SIGINT, onintr);
Guido van Rossum0969d361997-08-05 21:27:50 +0000813 if (setjmp(jbuf)) {
814#ifdef HAVE_SIGRELSE
815 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
816 sigrelse(SIGINT);
817#endif
Guido van Rossum174efc92000-09-16 16:37:53 +0000818 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000819 *signal = 1;
Guido van Rossum0969d361997-08-05 21:27:50 +0000820 return NULL;
821 }
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000822 rl_event_hook = PyOS_InputHook;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000823 p = readline(prompt);
824 PyOS_setsig(SIGINT, old_inthandler);
825
826 return p;
827}
828#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
829
830
831static char *
832call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
833{
Neal Norwitz1fa040b2004-08-25 01:20:18 +0000834 size_t n;
835 char *p, *q;
836 int signal;
837
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000838#ifdef SAVE_LOCALE
839 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwis701abe72004-08-20 06:26:59 +0000840 if (!saved_locale)
841 Py_FatalError("not enough memory to save locale");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000842 setlocale(LC_CTYPE, "");
843#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000844
Guido van Rossum74f31432003-01-07 20:01:29 +0000845 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
846 rl_instream = sys_stdin;
847 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000848#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum74f31432003-01-07 20:01:29 +0000849 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000850#endif
Guido van Rossum74f31432003-01-07 20:01:29 +0000851 }
852
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000853 p = readline_until_enter_or_signal(prompt, &signal);
854
855 /* we got an interrupt signal */
856 if(signal) {
857 return NULL;
858 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000859
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000860 /* We got an EOF, return a empty string. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000861 if (p == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000862 p = PyMem_Malloc(1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000863 if (p != NULL)
864 *p = '\0';
865 return p;
866 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000867
868 /* we have a valid line */
Guido van Rossum0969d361997-08-05 21:27:50 +0000869 n = strlen(p);
Skip Montanaroa0392742002-06-11 14:32:46 +0000870 if (n > 0) {
871 char *line;
872 HISTORY_STATE *state = history_get_history_state();
873 if (state->length > 0)
874 line = history_get(state->length)->line;
875 else
876 line = "";
877 if (strcmp(p, line))
878 add_history(p);
879 /* the history docs don't say so, but the address of state
880 changes each time history_get_history_state is called
881 which makes me think it's freshly malloc'd memory...
882 on the other hand, the address of the last line stays the
883 same as long as history isn't extended, so it appears to
884 be malloc'd but managed by the history package... */
885 free(state);
886 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000887 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
888 release the original. */
889 q = p;
890 p = PyMem_Malloc(n+2);
891 if (p != NULL) {
892 strncpy(p, q, n);
Guido van Rossum0969d361997-08-05 21:27:50 +0000893 p[n] = '\n';
894 p[n+1] = '\0';
895 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000896 free(q);
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000897#ifdef SAVE_LOCALE
898 setlocale(LC_CTYPE, saved_locale); /* Restore locale */
899 free(saved_locale);
900#endif
Guido van Rossum0969d361997-08-05 21:27:50 +0000901 return p;
902}
903
Guido van Rossum290900a1997-09-26 21:51:21 +0000904
905/* Initialize the module */
906
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000907PyDoc_STRVAR(doc_module,
908"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000909
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000910PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000911initreadline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000912{
Guido van Rossum1ea64ea2000-10-02 15:53:08 +0000913 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +0000914
915 m = Py_InitModule4("readline", readline_methods, doc_module,
916 (PyObject *)NULL, PYTHON_API_VERSION);
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000917
Guido van Rossum74f31432003-01-07 20:01:29 +0000918 PyOS_ReadlineFunctionPointer = call_readline;
919 setup_readline();
Guido van Rossum0969d361997-08-05 21:27:50 +0000920}