blob: e4a1d55e058a8dfbe0c11891a5485d915fa6f366 [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
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000026# define RESTORE_LOCALE(sl)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000027#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) \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000036 rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
Guido van Rossumd8faa362007-04-27 19:54:29 +000037#else
Martin v. Löwisb37509b2008-11-04 20:45:29 +000038#if defined(_RL_FUNCTION_TYPEDEF)
Guido van Rossumd8faa362007-04-27 19:54:29 +000039extern char **completion_matches(char *, rl_compentry_func_t *);
Martin v. Löwisb37509b2008-11-04 20:45:29 +000040#else
41extern char **completion_matches(char *, CPFunction *);
42#endif
Guido van Rossum353ae582001-07-10 16:45:32 +000043#endif
44
Christian Heimes32fbe592007-11-12 15:01:33 +000045static void
46on_completion_display_matches_hook(char **matches,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000047 int num_matches, int max_length);
Christian Heimes32fbe592007-11-12 15:01:33 +000048
Guido van Rossum0969d361997-08-05 21:27:50 +000049
Guido van Rossum290900a1997-09-26 21:51:21 +000050/* Exported function to send one line to readline's init file parser */
51
52static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000053parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000054{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000055 char *s, *copy;
56 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
57 return NULL;
58 /* Make a copy -- rl_parse_and_bind() modifies its argument */
59 /* Bernard Herzog */
60 copy = malloc(1 + strlen(s));
61 if (copy == NULL)
62 return PyErr_NoMemory();
63 strcpy(copy, s);
64 rl_parse_and_bind(copy);
65 free(copy); /* Free the copy */
66 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +000067}
68
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000069PyDoc_STRVAR(doc_parse_and_bind,
70"parse_and_bind(string) -> None\n\
71Parse and execute single line of a readline init file.");
Guido van Rossum290900a1997-09-26 21:51:21 +000072
73
74/* Exported function to parse a readline init file */
75
76static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000077read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000078{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000079 char *s = NULL;
80 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
81 return NULL;
82 errno = rl_read_init_file(s);
83 if (errno)
84 return PyErr_SetFromErrno(PyExc_IOError);
85 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +000086}
87
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000088PyDoc_STRVAR(doc_read_init_file,
89"read_init_file([filename]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +000090Parse a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000091The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +000092
93
Skip Montanaro28067822000-07-06 18:55:12 +000094/* Exported function to load a readline history file */
95
96static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000097read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +000098{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000099 char *s = NULL;
100 if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
101 return NULL;
102 errno = read_history(s);
103 if (errno)
104 return PyErr_SetFromErrno(PyExc_IOError);
105 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000106}
107
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000108static int _history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000109PyDoc_STRVAR(doc_read_history_file,
110"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000111Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000112The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000113
114
115/* Exported function to save a readline history file */
116
117static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000118write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000119{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000120 char *s = NULL;
121 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
122 return NULL;
123 errno = write_history(s);
124 if (!errno && _history_length >= 0)
125 history_truncate_file(s, _history_length);
126 if (errno)
127 return PyErr_SetFromErrno(PyExc_IOError);
128 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000129}
130
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000131PyDoc_STRVAR(doc_write_history_file,
132"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000133Save a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000134The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000135
136
Guido van Rossum74f31432003-01-07 20:01:29 +0000137/* Set history length */
138
139static PyObject*
140set_history_length(PyObject *self, PyObject *args)
141{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000142 int length = _history_length;
143 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
144 return NULL;
145 _history_length = length;
146 Py_RETURN_NONE;
Guido van Rossum74f31432003-01-07 20:01:29 +0000147}
148
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000149PyDoc_STRVAR(set_history_length_doc,
150"set_history_length(length) -> None\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000151set the maximal number of items which will be written to\n\
152the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000153history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000154
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000155
Guido van Rossum74f31432003-01-07 20:01:29 +0000156/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000157
158static PyObject*
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000159get_history_length(PyObject *self, PyObject *noarg)
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000160{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000161 return PyLong_FromLong(_history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000162}
163
Guido van Rossum74f31432003-01-07 20:01:29 +0000164PyDoc_STRVAR(get_history_length_doc,
165"get_history_length() -> int\n\
166return the maximum number of items that will be written to\n\
167the history file.");
168
169
Martin v. Löwis0daad592001-09-30 21:09:59 +0000170/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000171
Martin v. Löwis0daad592001-09-30 21:09:59 +0000172static PyObject *
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000173set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000174{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000175 PyObject *function = Py_None;
176 char buf[80];
177 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
178 if (!PyArg_ParseTuple(args, buf, &function))
179 return NULL;
180 if (function == Py_None) {
181 Py_XDECREF(*hook_var);
182 *hook_var = NULL;
183 }
184 else if (PyCallable_Check(function)) {
185 PyObject *tmp = *hook_var;
186 Py_INCREF(function);
187 *hook_var = function;
188 Py_XDECREF(tmp);
189 }
190 else {
191 PyOS_snprintf(buf, sizeof(buf),
192 "set_%.50s(func): argument not callable",
193 funcname);
194 PyErr_SetString(PyExc_TypeError, buf);
195 return NULL;
196 }
197 Py_RETURN_NONE;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000198}
199
Guido van Rossum74f31432003-01-07 20:01:29 +0000200
Martin v. Löwis0daad592001-09-30 21:09:59 +0000201/* Exported functions to specify hook functions in Python */
202
Thomas Wouters89d996e2007-09-08 17:39:28 +0000203static PyObject *completion_display_matches_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000204static PyObject *startup_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000205
206#ifdef HAVE_RL_PRE_INPUT_HOOK
207static PyObject *pre_input_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000208#endif
209
210static PyObject *
Thomas Wouters89d996e2007-09-08 17:39:28 +0000211set_completion_display_matches_hook(PyObject *self, PyObject *args)
212{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000213 PyObject *result = set_hook("completion_display_matches_hook",
214 &completion_display_matches_hook, args);
Christian Heimes32fbe592007-11-12 15:01:33 +0000215#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000216 /* We cannot set this hook globally, since it replaces the
217 default completion display. */
218 rl_completion_display_matches_hook =
219 completion_display_matches_hook ?
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000220#if defined(_RL_FUNCTION_TYPEDEF)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000221 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000222#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000223 (VFunction *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000224#endif
Christian Heimes32fbe592007-11-12 15:01:33 +0000225#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000226 return result;
Christian Heimes32fbe592007-11-12 15:01:33 +0000227
Thomas Wouters89d996e2007-09-08 17:39:28 +0000228}
229
230PyDoc_STRVAR(doc_set_completion_display_matches_hook,
231"set_completion_display_matches_hook([function]) -> None\n\
232Set or remove the completion display function.\n\
233The function is called as\n\
234 function(substitution, [matches], longest_match_length)\n\
235once each time matches need to be displayed.");
236
237static PyObject *
Martin v. Löwis0daad592001-09-30 21:09:59 +0000238set_startup_hook(PyObject *self, PyObject *args)
239{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000240 return set_hook("startup_hook", &startup_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000241}
242
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000243PyDoc_STRVAR(doc_set_startup_hook,
244"set_startup_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000245Set or remove the startup_hook function.\n\
246The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000247before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000248
Guido van Rossum74f31432003-01-07 20:01:29 +0000249
Martin v. Löwis0daad592001-09-30 21:09:59 +0000250#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000251
252/* Set pre-input hook */
253
Martin v. Löwis0daad592001-09-30 21:09:59 +0000254static PyObject *
255set_pre_input_hook(PyObject *self, PyObject *args)
256{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000257 return set_hook("pre_input_hook", &pre_input_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000258}
259
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000260PyDoc_STRVAR(doc_set_pre_input_hook,
261"set_pre_input_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000262Set or remove the pre_input_hook function.\n\
263The function is called with no arguments after the first prompt\n\
264has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000265characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000266
Martin v. Löwis0daad592001-09-30 21:09:59 +0000267#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000268
Guido van Rossum74f31432003-01-07 20:01:29 +0000269
Guido van Rossum290900a1997-09-26 21:51:21 +0000270/* Exported function to specify a word completer in Python */
271
272static PyObject *completer = NULL;
Guido van Rossum290900a1997-09-26 21:51:21 +0000273
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000274static PyObject *begidx = NULL;
275static PyObject *endidx = NULL;
276
Guido van Rossum74f31432003-01-07 20:01:29 +0000277
Thomas Wouters89d996e2007-09-08 17:39:28 +0000278/* Get the completion type for the scope of the tab-completion */
279static PyObject *
280get_completion_type(PyObject *self, PyObject *noarg)
281{
Christian Heimes217cfd12007-12-02 14:31:20 +0000282 return PyLong_FromLong(rl_completion_type);
Thomas Wouters89d996e2007-09-08 17:39:28 +0000283}
284
285PyDoc_STRVAR(doc_get_completion_type,
286"get_completion_type() -> int\n\
287Get the type of completion being attempted.");
288
289
Guido van Rossum74f31432003-01-07 20:01:29 +0000290/* Get the beginning index for the scope of the tab-completion */
291
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000292static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000293get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000294{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000295 Py_INCREF(begidx);
296 return begidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000297}
298
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000299PyDoc_STRVAR(doc_get_begidx,
300"get_begidx() -> int\n\
301get the beginning index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000302
Guido van Rossum74f31432003-01-07 20:01:29 +0000303
304/* Get the ending index for the scope of the tab-completion */
305
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000306static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000307get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000308{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000309 Py_INCREF(endidx);
310 return endidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000311}
312
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000313PyDoc_STRVAR(doc_get_endidx,
314"get_endidx() -> int\n\
315get the ending index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000316
317
Guido van Rossum74f31432003-01-07 20:01:29 +0000318/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000319
320static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000321set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000322{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000323 char *break_chars;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000324
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000325 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
326 return NULL;
327 }
328 free((void*)rl_completer_word_break_characters);
329 rl_completer_word_break_characters = strdup(break_chars);
330 Py_RETURN_NONE;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000331}
332
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000333PyDoc_STRVAR(doc_set_completer_delims,
334"set_completer_delims(string) -> None\n\
335set the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000336
Skip Montanaroe5069012004-08-15 14:32:06 +0000337static PyObject *
338py_remove_history(PyObject *self, PyObject *args)
339{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000340 int entry_number;
341 HIST_ENTRY *entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000342
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000343 if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
344 return NULL;
345 if (entry_number < 0) {
346 PyErr_SetString(PyExc_ValueError,
347 "History index cannot be negative");
348 return NULL;
349 }
350 entry = remove_history(entry_number);
351 if (!entry) {
352 PyErr_Format(PyExc_ValueError,
353 "No history item at position %d",
354 entry_number);
355 return NULL;
356 }
357 /* free memory allocated for the history entry */
358 if (entry->line)
359 free(entry->line);
360 if (entry->data)
361 free(entry->data);
362 free(entry);
Skip Montanaroe5069012004-08-15 14:32:06 +0000363
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000364 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000365}
366
367PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000368"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000369remove history item given by its position");
370
371static PyObject *
372py_replace_history(PyObject *self, PyObject *args)
373{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000374 int entry_number;
375 char *line;
376 HIST_ENTRY *old_entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000377
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000378 if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number,
379 &line)) {
380 return NULL;
381 }
382 if (entry_number < 0) {
383 PyErr_SetString(PyExc_ValueError,
384 "History index cannot be negative");
385 return NULL;
386 }
387 old_entry = replace_history_entry(entry_number, line, (void *)NULL);
388 if (!old_entry) {
389 PyErr_Format(PyExc_ValueError,
390 "No history item at position %d",
391 entry_number);
392 return NULL;
393 }
394 /* free memory allocated for the old history entry */
395 if (old_entry->line)
396 free(old_entry->line);
397 if (old_entry->data)
398 free(old_entry->data);
399 free(old_entry);
Skip Montanaroe5069012004-08-15 14:32:06 +0000400
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000401 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000402}
403
404PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000405"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000406replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000407
408/* Add a line to the history buffer */
409
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000410static PyObject *
411py_add_history(PyObject *self, PyObject *args)
412{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000413 char *line;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000414
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000415 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
416 return NULL;
417 }
418 add_history(line);
419 Py_RETURN_NONE;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000420}
421
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000422PyDoc_STRVAR(doc_add_history,
423"add_history(string) -> None\n\
424add a line to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000425
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000426
Guido van Rossum74f31432003-01-07 20:01:29 +0000427/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000428
429static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000430get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000431{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000432 return PyUnicode_FromString(rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000433}
Guido van Rossum74f31432003-01-07 20:01:29 +0000434
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000435PyDoc_STRVAR(doc_get_completer_delims,
436"get_completer_delims() -> string\n\
437get the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000438
Guido van Rossum74f31432003-01-07 20:01:29 +0000439
440/* Set the completer function */
441
Guido van Rossum290900a1997-09-26 21:51:21 +0000442static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000443set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000444{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000445 return set_hook("completer", &completer, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000446}
447
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000448PyDoc_STRVAR(doc_set_completer,
449"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000450Set or remove the completer function.\n\
451The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000452for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000453It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000454
Guido van Rossum74f31432003-01-07 20:01:29 +0000455
Michael W. Hudson796df152003-01-30 10:12:51 +0000456static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000457get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000458{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000459 if (completer == NULL) {
460 Py_RETURN_NONE;
461 }
462 Py_INCREF(completer);
463 return completer;
Michael W. Hudson796df152003-01-30 10:12:51 +0000464}
465
466PyDoc_STRVAR(doc_get_completer,
467"get_completer() -> function\n\
468\n\
469Returns current completer function.");
470
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000471/* Exported function to get any element of history */
472
473static PyObject *
474get_history_item(PyObject *self, PyObject *args)
475{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000476 int idx = 0;
477 HIST_ENTRY *hist_ent;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000478
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000479 if (!PyArg_ParseTuple(args, "i:index", &idx))
480 return NULL;
481 if ((hist_ent = history_get(idx)))
482 return PyUnicode_FromString(hist_ent->line);
483 else {
484 Py_RETURN_NONE;
485 }
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000486}
487
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000488PyDoc_STRVAR(doc_get_history_item,
489"get_history_item() -> string\n\
490return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000491
Guido van Rossum74f31432003-01-07 20:01:29 +0000492
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000493/* Exported function to get current length of history */
494
495static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000496get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000497{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000498 HISTORY_STATE *hist_st;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000499
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000500 hist_st = history_get_history_state();
501 return PyLong_FromLong(hist_st ? (long) hist_st->length : (long) 0);
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000502}
503
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000504PyDoc_STRVAR(doc_get_current_history_length,
505"get_current_history_length() -> integer\n\
506return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000507
Guido van Rossum74f31432003-01-07 20:01:29 +0000508
Guido van Rossum79378ff1997-10-07 14:53:21 +0000509/* Exported function to read the current line buffer */
510
511static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000512get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000513{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000514 return PyUnicode_FromString(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000515}
516
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000517PyDoc_STRVAR(doc_get_line_buffer,
518"get_line_buffer() -> string\n\
519return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000520
Guido van Rossum74f31432003-01-07 20:01:29 +0000521
Martin v. Löwise7a97962003-09-20 16:08:33 +0000522#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
523
524/* Exported function to clear the current history */
525
526static PyObject *
527py_clear_history(PyObject *self, PyObject *noarg)
528{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000529 clear_history();
530 Py_RETURN_NONE;
Martin v. Löwise7a97962003-09-20 16:08:33 +0000531}
532
533PyDoc_STRVAR(doc_clear_history,
534"clear_history() -> None\n\
535Clear the current readline history.");
536#endif
537
538
Guido van Rossum79378ff1997-10-07 14:53:21 +0000539/* Exported function to insert text into the line buffer */
540
541static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000542insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000543{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000544 char *s;
545 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
546 return NULL;
547 rl_insert_text(s);
548 Py_RETURN_NONE;
Guido van Rossum79378ff1997-10-07 14:53:21 +0000549}
550
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000551PyDoc_STRVAR(doc_insert_text,
552"insert_text(string) -> None\n\
553Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000554
Guido van Rossum74f31432003-01-07 20:01:29 +0000555
556/* Redisplay the line buffer */
557
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000558static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000559redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000560{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000561 rl_redisplay();
562 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000563}
564
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000565PyDoc_STRVAR(doc_redisplay,
566"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000567Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000568contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000569
Guido van Rossum74f31432003-01-07 20:01:29 +0000570
Guido van Rossum290900a1997-09-26 21:51:21 +0000571/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000572
573static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000574{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000575 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
576 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
577 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
578 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
579 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
580 {"read_history_file", read_history_file,
581 METH_VARARGS, doc_read_history_file},
582 {"write_history_file", write_history_file,
583 METH_VARARGS, doc_write_history_file},
584 {"get_history_item", get_history_item,
585 METH_VARARGS, doc_get_history_item},
586 {"get_current_history_length", (PyCFunction)get_current_history_length,
587 METH_NOARGS, doc_get_current_history_length},
588 {"set_history_length", set_history_length,
589 METH_VARARGS, set_history_length_doc},
590 {"get_history_length", get_history_length,
591 METH_NOARGS, get_history_length_doc},
592 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
593 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
594 {"get_completion_type", get_completion_type,
595 METH_NOARGS, doc_get_completion_type},
596 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
597 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000598
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000599 {"set_completer_delims", set_completer_delims,
600 METH_VARARGS, doc_set_completer_delims},
601 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
602 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
603 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
604 {"get_completer_delims", get_completer_delims,
605 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000606
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000607 {"set_completion_display_matches_hook", set_completion_display_matches_hook,
608 METH_VARARGS, doc_set_completion_display_matches_hook},
609 {"set_startup_hook", set_startup_hook,
610 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000611#ifdef HAVE_RL_PRE_INPUT_HOOK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000612 {"set_pre_input_hook", set_pre_input_hook,
613 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000614#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000615#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000616 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
Martin v. Löwise7a97962003-09-20 16:08:33 +0000617#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000618 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000619};
620
Guido van Rossum05ac4492003-01-07 20:04:12 +0000621
Martin v. Löwis0daad592001-09-30 21:09:59 +0000622/* C function to call the Python hooks. */
623
624static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000625on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000626{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000627 int result = 0;
628 if (func != NULL) {
629 PyObject *r;
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000630#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000631 PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000632#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000633 r = PyObject_CallFunction(func, NULL);
634 if (r == NULL)
635 goto error;
636 if (r == Py_None)
637 result = 0;
638 else {
639 result = PyLong_AsLong(r);
640 if (result == -1 && PyErr_Occurred())
641 goto error;
642 }
643 Py_DECREF(r);
644 goto done;
645 error:
646 PyErr_Clear();
647 Py_XDECREF(r);
648 done:
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000649#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000650 PyGILState_Release(gilstate);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000651#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000652 return result;
653 }
654 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000655}
656
657static int
658on_startup_hook(void)
659{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000660 return on_hook(startup_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000661}
662
663#ifdef HAVE_RL_PRE_INPUT_HOOK
664static int
665on_pre_input_hook(void)
666{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000667 return on_hook(pre_input_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000668}
669#endif
670
Guido van Rossum05ac4492003-01-07 20:04:12 +0000671
Thomas Wouters89d996e2007-09-08 17:39:28 +0000672/* C function to call the Python completion_display_matches */
673
674static void
675on_completion_display_matches_hook(char **matches,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000676 int num_matches, int max_length)
Thomas Wouters89d996e2007-09-08 17:39:28 +0000677{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000678 int i;
679 PyObject *m=NULL, *s=NULL, *r=NULL;
Christian Heimesaec75c32007-11-11 22:42:36 +0000680#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000681 PyGILState_STATE gilstate = PyGILState_Ensure();
Thomas Wouters89d996e2007-09-08 17:39:28 +0000682#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000683 m = PyList_New(num_matches);
684 if (m == NULL)
685 goto error;
686 for (i = 0; i < num_matches; i++) {
687 s = PyUnicode_FromString(matches[i+1]);
688 if (s == NULL)
689 goto error;
690 if (PyList_SetItem(m, i, s) == -1)
691 goto error;
692 }
693 r = PyObject_CallFunction(completion_display_matches_hook,
694 "sOi", matches[0], m, max_length);
Christian Heimes32fbe592007-11-12 15:01:33 +0000695
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000696 Py_DECREF(m); m=NULL;
Christian Heimes32fbe592007-11-12 15:01:33 +0000697
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000698 if (r == NULL ||
699 (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) {
700 goto error;
701 }
702 Py_XDECREF(r); r=NULL;
703
704 if (0) {
705 error:
706 PyErr_Clear();
707 Py_XDECREF(m);
708 Py_XDECREF(r);
709 }
Christian Heimes32fbe592007-11-12 15:01:33 +0000710#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000711 PyGILState_Release(gilstate);
Christian Heimes32fbe592007-11-12 15:01:33 +0000712#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000713}
714
715
Guido van Rossum290900a1997-09-26 21:51:21 +0000716/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000717
Guido van Rossum290900a1997-09-26 21:51:21 +0000718static char *
Guido van Rossumd8faa362007-04-27 19:54:29 +0000719on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000720{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000721 char *result = NULL;
722 if (completer != NULL) {
723 PyObject *r;
Christian Heimesaec75c32007-11-11 22:42:36 +0000724#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000725 PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000726#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000727 rl_attempted_completion_over = 1;
728 r = PyObject_CallFunction(completer, "si", text, state);
729 if (r == NULL)
730 goto error;
731 if (r == Py_None) {
732 result = NULL;
733 }
734 else {
735 char *s = _PyUnicode_AsString(r);
736 if (s == NULL)
737 goto error;
738 result = strdup(s);
739 }
740 Py_DECREF(r);
741 goto done;
742 error:
743 PyErr_Clear();
744 Py_XDECREF(r);
745 done:
Christian Heimesaec75c32007-11-11 22:42:36 +0000746#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000747 PyGILState_Release(gilstate);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000748#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000749 return result;
750 }
751 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000752}
753
Guido van Rossum290900a1997-09-26 21:51:21 +0000754
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000755/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000756 * before calling the normal completer */
757
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000758static char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000759flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000760{
Antoine Pitroue566bda2009-10-19 18:24:35 +0000761#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000762 rl_completion_append_character ='\0';
Antoine Pitrou37276002009-10-26 19:32:51 +0000763#endif
764#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000765 rl_completion_suppress_append = 0;
Antoine Pitroue566bda2009-10-19 18:24:35 +0000766#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000767 Py_XDECREF(begidx);
768 Py_XDECREF(endidx);
769 begidx = PyLong_FromLong((long) start);
770 endidx = PyLong_FromLong((long) end);
771 return completion_matches(text, *on_completion);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000772}
773
Guido van Rossum05ac4492003-01-07 20:04:12 +0000774
Guido van Rossum290900a1997-09-26 21:51:21 +0000775/* Helper to initialize GNU readline properly. */
776
777static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000778setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000779{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000780#ifdef SAVE_LOCALE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000781 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
782 if (!saved_locale)
783 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000784#endif
785
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000786 using_history();
Skip Montanaroa0392742002-06-11 14:32:46 +0000787
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000788 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000789#if defined(PYOS_OS2) && defined(PYCC_GCC)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000790 /* Allow $if term= in .inputrc to work */
791 rl_terminal_name = getenv("TERM");
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000792#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000793 /* Force rebind of TAB to insert-tab */
794 rl_bind_key('\t', rl_insert);
795 /* Bind both ESC-TAB and ESC-ESC to the completion function */
796 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
797 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
798 /* Set our hook functions */
799 rl_startup_hook = (Function *)on_startup_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000800#ifdef HAVE_RL_PRE_INPUT_HOOK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000801 rl_pre_input_hook = (Function *)on_pre_input_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000802#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000803 /* Set our completion function */
804 rl_attempted_completion_function = (CPPFunction *)flex_complete;
805 /* Set Python word break characters */
806 rl_completer_word_break_characters =
807 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
808 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000809
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000810 begidx = PyLong_FromLong(0L);
811 endidx = PyLong_FromLong(0L);
812 /* Initialize (allows .inputrc to override)
813 *
814 * XXX: A bug in the readline-2.2 library causes a memory leak
815 * inside this function. Nothing we can do about it.
816 */
817 rl_initialize();
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000818
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000819 RESTORE_LOCALE(saved_locale)
Guido van Rossum290900a1997-09-26 21:51:21 +0000820}
821
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000822/* Wrapper around GNU readline that handles signals differently. */
823
824
825#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
826
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000827static char *completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000828static void
829rlhandler(char *text)
830{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000831 completed_input_string = text;
832 rl_callback_handler_remove();
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000833}
834
835extern PyThreadState* _PyOS_ReadlineTState;
836
837static char *
838readline_until_enter_or_signal(char *prompt, int *signal)
839{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000840 char * not_done_reading = "";
841 fd_set selectset;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000842
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000843 *signal = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000844#ifdef HAVE_RL_CATCH_SIGNAL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000845 rl_catch_signals = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000846#endif
847
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000848 rl_callback_handler_install (prompt, rlhandler);
849 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000850
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000851 completed_input_string = not_done_reading;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000852
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000853 while (completed_input_string == not_done_reading) {
854 int has_input = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000855
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000856 while (!has_input)
857 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000858
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000859 /* [Bug #1552726] Only limit the pause if an input hook has been
860 defined. */
861 struct timeval *timeoutp = NULL;
862 if (PyOS_InputHook)
863 timeoutp = &timeout;
864 FD_SET(fileno(rl_instream), &selectset);
865 /* select resets selectset if no input was available */
866 has_input = select(fileno(rl_instream) + 1, &selectset,
867 NULL, NULL, timeoutp);
868 if(PyOS_InputHook) PyOS_InputHook();
869 }
870
871 if(has_input > 0) {
872 rl_callback_read_char();
873 }
874 else if (errno == EINTR) {
875 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000876#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000877 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000878#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000879 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000880#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000881 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000882#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000883 if (s < 0) {
884 rl_free_line_state();
885 rl_cleanup_after_signal();
886 rl_callback_handler_remove();
887 *signal = 1;
888 completed_input_string = NULL;
889 }
890 }
891 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000892
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000893 return completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000894}
895
896
897#else
Guido van Rossum290900a1997-09-26 21:51:21 +0000898
899/* Interrupt handler */
900
901static jmp_buf jbuf;
902
Guido van Rossum0969d361997-08-05 21:27:50 +0000903/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +0000904static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000905onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000906{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000907 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000908}
909
Guido van Rossum290900a1997-09-26 21:51:21 +0000910
Guido van Rossum0969d361997-08-05 21:27:50 +0000911static char *
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000912readline_until_enter_or_signal(char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +0000913{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000914 PyOS_sighandler_t old_inthandler;
915 char *p;
Guido van Rossum74f31432003-01-07 20:01:29 +0000916
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000917 *signal = 0;
918
919 old_inthandler = PyOS_setsig(SIGINT, onintr);
920 if (setjmp(jbuf)) {
Guido van Rossum0969d361997-08-05 21:27:50 +0000921#ifdef HAVE_SIGRELSE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000922 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
923 sigrelse(SIGINT);
Guido van Rossum0969d361997-08-05 21:27:50 +0000924#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000925 PyOS_setsig(SIGINT, old_inthandler);
926 *signal = 1;
927 return NULL;
928 }
929 rl_event_hook = PyOS_InputHook;
930 p = readline(prompt);
931 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000932
933 return p;
934}
935#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
936
937
938static char *
939call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
940{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000941 size_t n;
942 char *p, *q;
943 int signal;
Neal Norwitz1fa040b2004-08-25 01:20:18 +0000944
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000945#ifdef SAVE_LOCALE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000946 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
947 if (!saved_locale)
948 Py_FatalError("not enough memory to save locale");
949 setlocale(LC_CTYPE, "");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000950#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000951
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000952 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
953 rl_instream = sys_stdin;
954 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000955#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000956 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000957#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000958 }
Guido van Rossum74f31432003-01-07 20:01:29 +0000959
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000960 p = readline_until_enter_or_signal(prompt, &signal);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000961
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000962 /* we got an interrupt signal */
963 if (signal) {
964 RESTORE_LOCALE(saved_locale)
965 return NULL;
966 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000967
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000968 /* We got an EOF, return a empty string. */
969 if (p == NULL) {
970 p = PyMem_Malloc(1);
971 if (p != NULL)
972 *p = '\0';
973 RESTORE_LOCALE(saved_locale)
974 return p;
975 }
976
977 /* we have a valid line */
978 n = strlen(p);
979 if (n > 0) {
980 char *line;
981 HISTORY_STATE *state = history_get_history_state();
982 if (state->length > 0)
983 line = history_get(state->length)->line;
984 else
985 line = "";
986 if (strcmp(p, line))
987 add_history(p);
988 /* the history docs don't say so, but the address of state
989 changes each time history_get_history_state is called
990 which makes me think it's freshly malloc'd memory...
991 on the other hand, the address of the last line stays the
992 same as long as history isn't extended, so it appears to
993 be malloc'd but managed by the history package... */
994 free(state);
995 }
996 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
997 release the original. */
998 q = p;
999 p = PyMem_Malloc(n+2);
1000 if (p != NULL) {
1001 strncpy(p, q, n);
1002 p[n] = '\n';
1003 p[n+1] = '\0';
1004 }
1005 free(q);
1006 RESTORE_LOCALE(saved_locale)
1007 return p;
Guido van Rossum0969d361997-08-05 21:27:50 +00001008}
1009
Guido van Rossum290900a1997-09-26 21:51:21 +00001010
1011/* Initialize the module */
1012
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001013PyDoc_STRVAR(doc_module,
1014"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001015
Martin v. Löwis1a214512008-06-11 05:26:20 +00001016
1017static struct PyModuleDef readlinemodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001018 PyModuleDef_HEAD_INIT,
1019 "readline",
1020 doc_module,
1021 -1,
1022 readline_methods,
1023 NULL,
1024 NULL,
1025 NULL,
1026 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001027};
1028
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001029PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001030PyInit_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001031{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001032 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +00001033
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001034 m = PyModule_Create(&readlinemodule);
1035 if (m == NULL)
1036 return NULL;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001037
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001038 PyOS_ReadlineFunctionPointer = call_readline;
1039 setup_readline();
1040 return m;
Guido van Rossum0969d361997-08-05 21:27:50 +00001041}