blob: 90529376b614111b9f0f61de4c18de64bd333a5a [file] [log] [blame]
Guido van Rossum290900a1997-09-26 21:51:21 +00001/* This module makes GNU readline available to Python. It has ideas
2 * contributed by Lee Busby, LLNL, and William Magro, Cornell Theory
Michael W. Hudson9a8c3142005-03-30 10:09:12 +00003 * Center. The completer interface was inspired by Lele Gaifax. More
4 * recently, it was largely rewritten by Guido van Rossum.
Guido van Rossum0969d361997-08-05 21:27:50 +00005 */
6
Guido van Rossum290900a1997-09-26 21:51:21 +00007/* Standard definitions */
Guido van Rossum0969d361997-08-05 21:27:50 +00008#include "Python.h"
9#include <setjmp.h>
10#include <signal.h>
Guido van Rossum290900a1997-09-26 21:51:21 +000011#include <errno.h>
Michael W. Hudson8da2b012004-10-07 13:46:33 +000012#include <sys/time.h>
Guido van Rossum0969d361997-08-05 21:27:50 +000013
Skip Montanaro7befb992004-02-10 16:50:21 +000014#if defined(HAVE_SETLOCALE)
Guido van Rossum60c8a3a2002-10-09 21:27:33 +000015/* GNU readline() mistakenly sets the LC_CTYPE locale.
16 * This is evil. Only the user or the app's main() should do this!
17 * We must save and restore the locale around the rl_initialize() call.
18 */
19#define SAVE_LOCALE
20#include <locale.h>
21#endif
22
Thomas Wouters0e3f5912006-08-11 14:57:12 +000023#ifdef SAVE_LOCALE
24# define RESTORE_LOCALE(sl) { setlocale(LC_CTYPE, sl); free(sl); }
25#else
26# define RESTORE_LOCALE(sl)
27#endif
28
Guido van Rossum290900a1997-09-26 21:51:21 +000029/* GNU readline definitions */
Guido van Rossumb0e51b22001-04-13 18:14:27 +000030#undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
Guido van Rossumbcc20741998-08-04 22:53:56 +000031#include <readline/readline.h>
32#include <readline/history.h>
Guido van Rossum730806d1998-04-10 22:27:42 +000033
Guido van Rossum353ae582001-07-10 16:45:32 +000034#ifdef HAVE_RL_COMPLETION_MATCHES
Guido van Rossum74f31432003-01-07 20:01:29 +000035#define completion_matches(x, y) \
36 rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
Guido van Rossumd8faa362007-04-27 19:54:29 +000037#else
38extern char **completion_matches(char *, rl_compentry_func_t *);
Guido van Rossum353ae582001-07-10 16:45:32 +000039#endif
40
Christian Heimes32fbe592007-11-12 15:01:33 +000041static void
42on_completion_display_matches_hook(char **matches,
43 int num_matches, int max_length);
44
Guido van Rossum0969d361997-08-05 21:27:50 +000045
Guido van Rossum290900a1997-09-26 21:51:21 +000046/* Exported function to send one line to readline's init file parser */
47
48static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000049parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000050{
Guido van Rossum3b5330e1998-12-04 15:34:39 +000051 char *s, *copy;
Guido van Rossum43713e52000-02-29 13:59:29 +000052 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000053 return NULL;
Guido van Rossum3b5330e1998-12-04 15:34:39 +000054 /* Make a copy -- rl_parse_and_bind() modifies its argument */
55 /* Bernard Herzog */
56 copy = malloc(1 + strlen(s));
57 if (copy == NULL)
58 return PyErr_NoMemory();
59 strcpy(copy, s);
60 rl_parse_and_bind(copy);
61 free(copy); /* Free the copy */
Guido van Rossum290900a1997-09-26 21:51:21 +000062 Py_INCREF(Py_None);
63 return Py_None;
64}
65
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000066PyDoc_STRVAR(doc_parse_and_bind,
67"parse_and_bind(string) -> None\n\
68Parse and execute single line of a readline init file.");
Guido van Rossum290900a1997-09-26 21:51:21 +000069
70
71/* Exported function to parse a readline init file */
72
73static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000074read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000075{
76 char *s = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +000077 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000078 return NULL;
79 errno = rl_read_init_file(s);
80 if (errno)
81 return PyErr_SetFromErrno(PyExc_IOError);
82 Py_INCREF(Py_None);
83 return Py_None;
84}
85
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000086PyDoc_STRVAR(doc_read_init_file,
87"read_init_file([filename]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +000088Parse a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000089The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +000090
91
Skip Montanaro28067822000-07-06 18:55:12 +000092/* Exported function to load a readline history file */
93
94static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000095read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +000096{
97 char *s = NULL;
98 if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
99 return NULL;
100 errno = read_history(s);
101 if (errno)
102 return PyErr_SetFromErrno(PyExc_IOError);
103 Py_INCREF(Py_None);
104 return Py_None;
105}
106
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000107static int _history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000108PyDoc_STRVAR(doc_read_history_file,
109"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000110Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000111The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000112
113
114/* Exported function to save a readline history file */
115
116static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000117write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000118{
119 char *s = NULL;
120 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
121 return NULL;
122 errno = write_history(s);
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000123 if (!errno && _history_length >= 0)
124 history_truncate_file(s, _history_length);
Skip Montanaro28067822000-07-06 18:55:12 +0000125 if (errno)
126 return PyErr_SetFromErrno(PyExc_IOError);
127 Py_INCREF(Py_None);
128 return Py_None;
129}
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{
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000142 int length = _history_length;
Guido van Rossum74f31432003-01-07 20:01:29 +0000143 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
144 return NULL;
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000145 _history_length = length;
Guido van Rossum74f31432003-01-07 20:01:29 +0000146 Py_INCREF(Py_None);
147 return Py_None;
148}
149
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000150PyDoc_STRVAR(set_history_length_doc,
151"set_history_length(length) -> None\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000152set the maximal number of items which will be written to\n\
153the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000154history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000155
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000156
Guido van Rossum74f31432003-01-07 20:01:29 +0000157/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000158
159static PyObject*
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000160get_history_length(PyObject *self, PyObject *noarg)
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000161{
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000162 return PyInt_FromLong(_history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000163}
164
Guido van Rossum74f31432003-01-07 20:01:29 +0000165PyDoc_STRVAR(get_history_length_doc,
166"get_history_length() -> int\n\
167return the maximum number of items that will be written to\n\
168the history file.");
169
170
Martin v. Löwis0daad592001-09-30 21:09:59 +0000171/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000172
Martin v. Löwis0daad592001-09-30 21:09:59 +0000173static PyObject *
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000174set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000175{
176 PyObject *function = Py_None;
177 char buf[80];
Tim Peters885d4572001-11-28 20:27:42 +0000178 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000179 if (!PyArg_ParseTuple(args, buf, &function))
180 return NULL;
181 if (function == Py_None) {
182 Py_XDECREF(*hook_var);
183 *hook_var = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000184 }
185 else if (PyCallable_Check(function)) {
186 PyObject *tmp = *hook_var;
187 Py_INCREF(function);
188 *hook_var = function;
189 Py_XDECREF(tmp);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000190 }
191 else {
Tim Peters885d4572001-11-28 20:27:42 +0000192 PyOS_snprintf(buf, sizeof(buf),
193 "set_%.50s(func): argument not callable",
194 funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000195 PyErr_SetString(PyExc_TypeError, buf);
196 return NULL;
197 }
198 Py_INCREF(Py_None);
199 return Py_None;
200}
201
Guido van Rossum74f31432003-01-07 20:01:29 +0000202
Martin v. Löwis0daad592001-09-30 21:09:59 +0000203/* Exported functions to specify hook functions in Python */
204
Thomas Wouters89d996e2007-09-08 17:39:28 +0000205static PyObject *completion_display_matches_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000206static PyObject *startup_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000207
208#ifdef HAVE_RL_PRE_INPUT_HOOK
209static PyObject *pre_input_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000210#endif
211
212static PyObject *
Thomas Wouters89d996e2007-09-08 17:39:28 +0000213set_completion_display_matches_hook(PyObject *self, PyObject *args)
214{
Christian Heimes32fbe592007-11-12 15:01:33 +0000215 PyObject *result = set_hook("completion_display_matches_hook",
Thomas Wouters89d996e2007-09-08 17:39:28 +0000216 &completion_display_matches_hook, args);
Christian Heimes32fbe592007-11-12 15:01:33 +0000217#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
218 /* We cannot set this hook globally, since it replaces the
219 default completion display. */
220 rl_completion_display_matches_hook =
221 completion_display_matches_hook ?
222 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
223#endif
224 return result;
225
Thomas Wouters89d996e2007-09-08 17:39:28 +0000226}
227
228PyDoc_STRVAR(doc_set_completion_display_matches_hook,
229"set_completion_display_matches_hook([function]) -> None\n\
230Set or remove the completion display function.\n\
231The function is called as\n\
232 function(substitution, [matches], longest_match_length)\n\
233once each time matches need to be displayed.");
234
235static PyObject *
Martin v. Löwis0daad592001-09-30 21:09:59 +0000236set_startup_hook(PyObject *self, PyObject *args)
237{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000238 return set_hook("startup_hook", &startup_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000239}
240
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000241PyDoc_STRVAR(doc_set_startup_hook,
242"set_startup_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000243Set or remove the startup_hook function.\n\
244The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000245before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000246
Guido van Rossum74f31432003-01-07 20:01:29 +0000247
Martin v. Löwis0daad592001-09-30 21:09:59 +0000248#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000249
250/* Set pre-input hook */
251
Martin v. Löwis0daad592001-09-30 21:09:59 +0000252static PyObject *
253set_pre_input_hook(PyObject *self, PyObject *args)
254{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000255 return set_hook("pre_input_hook", &pre_input_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000256}
257
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000258PyDoc_STRVAR(doc_set_pre_input_hook,
259"set_pre_input_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000260Set or remove the pre_input_hook function.\n\
261The function is called with no arguments after the first prompt\n\
262has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000263characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000264
Martin v. Löwis0daad592001-09-30 21:09:59 +0000265#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000266
Guido van Rossum74f31432003-01-07 20:01:29 +0000267
Guido van Rossum290900a1997-09-26 21:51:21 +0000268/* Exported function to specify a word completer in Python */
269
270static PyObject *completer = NULL;
Guido van Rossum290900a1997-09-26 21:51:21 +0000271
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000272static PyObject *begidx = NULL;
273static PyObject *endidx = NULL;
274
Guido van Rossum74f31432003-01-07 20:01:29 +0000275
Thomas Wouters89d996e2007-09-08 17:39:28 +0000276/* Get the completion type for the scope of the tab-completion */
277static PyObject *
278get_completion_type(PyObject *self, PyObject *noarg)
279{
280 return PyInt_FromLong(rl_completion_type);
281}
282
283PyDoc_STRVAR(doc_get_completion_type,
284"get_completion_type() -> int\n\
285Get the type of completion being attempted.");
286
287
Guido van Rossum74f31432003-01-07 20:01:29 +0000288/* Get the beginning index for the scope of the tab-completion */
289
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000290static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000291get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000292{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000293 Py_INCREF(begidx);
294 return begidx;
295}
296
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000297PyDoc_STRVAR(doc_get_begidx,
298"get_begidx() -> int\n\
299get the beginning index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000300
Guido van Rossum74f31432003-01-07 20:01:29 +0000301
302/* Get the ending index for the scope of the tab-completion */
303
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000304static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000305get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000306{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000307 Py_INCREF(endidx);
308 return endidx;
309}
310
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000311PyDoc_STRVAR(doc_get_endidx,
312"get_endidx() -> int\n\
313get the ending index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000314
315
Guido van Rossum74f31432003-01-07 20:01:29 +0000316/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000317
318static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000319set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000320{
321 char *break_chars;
322
Guido van Rossum43713e52000-02-29 13:59:29 +0000323 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000324 return NULL;
325 }
Neal Norwitz0e0ee592002-04-21 15:03:18 +0000326 free((void*)rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000327 rl_completer_word_break_characters = strdup(break_chars);
328 Py_INCREF(Py_None);
329 return Py_None;
330}
331
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000332PyDoc_STRVAR(doc_set_completer_delims,
333"set_completer_delims(string) -> None\n\
334set the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000335
Skip Montanaroe5069012004-08-15 14:32:06 +0000336static PyObject *
337py_remove_history(PyObject *self, PyObject *args)
338{
339 int entry_number;
340 HIST_ENTRY *entry;
341
342 if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
343 return NULL;
Martin v. Löwis9533e342005-02-27 20:33:25 +0000344 if (entry_number < 0) {
345 PyErr_SetString(PyExc_ValueError,
346 "History index cannot be negative");
347 return NULL;
348 }
Skip Montanaroe5069012004-08-15 14:32:06 +0000349 entry = remove_history(entry_number);
350 if (!entry) {
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000351 PyErr_Format(PyExc_ValueError,
352 "No history item at position %d",
353 entry_number);
Skip Montanaroe5069012004-08-15 14:32:06 +0000354 return NULL;
355 }
356 /* free memory allocated for the history entry */
357 if (entry->line)
358 free(entry->line);
359 if (entry->data)
360 free(entry->data);
361 free(entry);
362
363 Py_INCREF(Py_None);
364 return Py_None;
365}
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{
374 int entry_number;
375 char *line;
376 HIST_ENTRY *old_entry;
377
378 if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number, &line)) {
379 return NULL;
380 }
Martin v. Löwis9533e342005-02-27 20:33:25 +0000381 if (entry_number < 0) {
382 PyErr_SetString(PyExc_ValueError,
383 "History index cannot be negative");
384 return NULL;
385 }
Skip Montanaroe5069012004-08-15 14:32:06 +0000386 old_entry = replace_history_entry(entry_number, line, (void *)NULL);
387 if (!old_entry) {
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000388 PyErr_Format(PyExc_ValueError,
389 "No history item at position %d",
390 entry_number);
Skip Montanaroe5069012004-08-15 14:32:06 +0000391 return NULL;
392 }
393 /* free memory allocated for the old history entry */
394 if (old_entry->line)
395 free(old_entry->line);
396 if (old_entry->data)
397 free(old_entry->data);
398 free(old_entry);
399
400 Py_INCREF(Py_None);
401 return Py_None;
402}
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{
413 char *line;
414
415 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
416 return NULL;
417 }
418 add_history(line);
419 Py_INCREF(Py_None);
420 return Py_None;
421}
422
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000423PyDoc_STRVAR(doc_add_history,
424"add_history(string) -> None\n\
425add a line to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000426
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000427
Guido van Rossum74f31432003-01-07 20:01:29 +0000428/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000429
430static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000431get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000432{
Christian Heimesaec75c32007-11-11 22:42:36 +0000433 return PyUnicode_FromString(rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000434}
Guido van Rossum74f31432003-01-07 20:01:29 +0000435
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000436PyDoc_STRVAR(doc_get_completer_delims,
437"get_completer_delims() -> string\n\
438get the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000439
Guido van Rossum74f31432003-01-07 20:01:29 +0000440
441/* Set the completer function */
442
Guido van Rossum290900a1997-09-26 21:51:21 +0000443static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000444set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000445{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000446 return set_hook("completer", &completer, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000447}
448
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000449PyDoc_STRVAR(doc_set_completer,
450"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000451Set or remove the completer function.\n\
452The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000453for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000454It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000455
Guido van Rossum74f31432003-01-07 20:01:29 +0000456
Michael W. Hudson796df152003-01-30 10:12:51 +0000457static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000458get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000459{
460 if (completer == NULL) {
461 Py_INCREF(Py_None);
462 return Py_None;
463 }
464 Py_INCREF(completer);
465 return completer;
466}
467
468PyDoc_STRVAR(doc_get_completer,
469"get_completer() -> function\n\
470\n\
471Returns current completer function.");
472
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000473/* Exported function to get any element of history */
474
475static PyObject *
476get_history_item(PyObject *self, PyObject *args)
477{
478 int idx = 0;
479 HIST_ENTRY *hist_ent;
480
481 if (!PyArg_ParseTuple(args, "i:index", &idx))
482 return NULL;
483 if ((hist_ent = history_get(idx)))
Christian Heimesaec75c32007-11-11 22:42:36 +0000484 return PyUnicode_FromString(hist_ent->line);
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000485 else {
486 Py_INCREF(Py_None);
487 return Py_None;
488 }
489}
490
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000491PyDoc_STRVAR(doc_get_history_item,
492"get_history_item() -> string\n\
493return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000494
Guido van Rossum74f31432003-01-07 20:01:29 +0000495
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000496/* Exported function to get current length of history */
497
498static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000499get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000500{
501 HISTORY_STATE *hist_st;
502
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000503 hist_st = history_get_history_state();
504 return PyInt_FromLong(hist_st ? (long) hist_st->length : (long) 0);
505}
506
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000507PyDoc_STRVAR(doc_get_current_history_length,
508"get_current_history_length() -> integer\n\
509return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000510
Guido van Rossum74f31432003-01-07 20:01:29 +0000511
Guido van Rossum79378ff1997-10-07 14:53:21 +0000512/* Exported function to read the current line buffer */
513
514static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000515get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000516{
Christian Heimesaec75c32007-11-11 22:42:36 +0000517 return PyUnicode_FromString(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000518}
519
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000520PyDoc_STRVAR(doc_get_line_buffer,
521"get_line_buffer() -> string\n\
522return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000523
Guido van Rossum74f31432003-01-07 20:01:29 +0000524
Martin v. Löwise7a97962003-09-20 16:08:33 +0000525#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
526
527/* Exported function to clear the current history */
528
529static PyObject *
530py_clear_history(PyObject *self, PyObject *noarg)
531{
532 clear_history();
533 Py_INCREF(Py_None);
534 return Py_None;
535}
536
537PyDoc_STRVAR(doc_clear_history,
538"clear_history() -> None\n\
539Clear the current readline history.");
540#endif
541
542
Guido van Rossum79378ff1997-10-07 14:53:21 +0000543/* Exported function to insert text into the line buffer */
544
545static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000546insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000547{
548 char *s;
Guido van Rossum43713e52000-02-29 13:59:29 +0000549 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000550 return NULL;
551 rl_insert_text(s);
552 Py_INCREF(Py_None);
553 return Py_None;
554}
555
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000556PyDoc_STRVAR(doc_insert_text,
557"insert_text(string) -> None\n\
558Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000559
Guido van Rossum74f31432003-01-07 20:01:29 +0000560
561/* Redisplay the line buffer */
562
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000563static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000564redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000565{
566 rl_redisplay();
567 Py_INCREF(Py_None);
568 return Py_None;
569}
570
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000571PyDoc_STRVAR(doc_redisplay,
572"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000573Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000574contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000575
Guido van Rossum74f31432003-01-07 20:01:29 +0000576
Guido van Rossum290900a1997-09-26 21:51:21 +0000577/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000578
579static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000580{
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000581 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000582 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000583 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000584 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000585 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000586 {"read_history_file", read_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000587 METH_VARARGS, doc_read_history_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000588 {"write_history_file", write_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000589 METH_VARARGS, doc_write_history_file},
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000590 {"get_history_item", get_history_item,
591 METH_VARARGS, doc_get_history_item},
Neal Norwitz767f8352002-03-31 16:13:39 +0000592 {"get_current_history_length", (PyCFunction)get_current_history_length,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000593 METH_NOARGS, doc_get_current_history_length},
Guido van Rossum74f31432003-01-07 20:01:29 +0000594 {"set_history_length", set_history_length,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000595 METH_VARARGS, set_history_length_doc},
Guido van Rossum74f31432003-01-07 20:01:29 +0000596 {"get_history_length", get_history_length,
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000597 METH_NOARGS, get_history_length_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000598 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
Michael W. Hudson796df152003-01-30 10:12:51 +0000599 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
Thomas Wouters89d996e2007-09-08 17:39:28 +0000600 {"get_completion_type", get_completion_type,
601 METH_NOARGS, doc_get_completion_type},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000602 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
603 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000604
Guido van Rossum74f31432003-01-07 20:01:29 +0000605 {"set_completer_delims", set_completer_delims,
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000606 METH_VARARGS, doc_set_completer_delims},
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000607 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
Skip Montanaroe5069012004-08-15 14:32:06 +0000608 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
609 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000610 {"get_completer_delims", get_completer_delims,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000611 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000612
Thomas Wouters89d996e2007-09-08 17:39:28 +0000613 {"set_completion_display_matches_hook", set_completion_display_matches_hook,
614 METH_VARARGS, doc_set_completion_display_matches_hook},
Guido van Rossum74f31432003-01-07 20:01:29 +0000615 {"set_startup_hook", set_startup_hook,
616 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000617#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000618 {"set_pre_input_hook", set_pre_input_hook,
619 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000620#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000621#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
622 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
623#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000624 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000625};
626
Guido van Rossum05ac4492003-01-07 20:04:12 +0000627
Martin v. Löwis0daad592001-09-30 21:09:59 +0000628/* C function to call the Python hooks. */
629
630static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000631on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000632{
633 int result = 0;
634 if (func != NULL) {
635 PyObject *r;
Christian Heimesaec75c32007-11-11 22:42:36 +0000636#ifdef WITH_THREAD
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000637 PyGILState_STATE gilstate = PyGILState_Ensure();
638#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000639 r = PyObject_CallFunction(func, NULL);
640 if (r == NULL)
641 goto error;
Guido van Rossum74f31432003-01-07 20:01:29 +0000642 if (r == Py_None)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000643 result = 0;
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000644 else {
Martin v. Löwis0daad592001-09-30 21:09:59 +0000645 result = PyInt_AsLong(r);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000646 if (result == -1 && PyErr_Occurred())
647 goto error;
648 }
Martin v. Löwis0daad592001-09-30 21:09:59 +0000649 Py_DECREF(r);
650 goto done;
651 error:
652 PyErr_Clear();
653 Py_XDECREF(r);
654 done:
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000655#ifdef WITH_THREAD
656 PyGILState_Release(gilstate);
657#endif
Georg Brandle677adc2005-09-29 13:40:49 +0000658 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000659 }
660 return result;
661}
662
663static int
664on_startup_hook(void)
665{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000666 return on_hook(startup_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000667}
668
669#ifdef HAVE_RL_PRE_INPUT_HOOK
670static int
671on_pre_input_hook(void)
672{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000673 return on_hook(pre_input_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000674}
675#endif
676
Guido van Rossum05ac4492003-01-07 20:04:12 +0000677
Thomas Wouters89d996e2007-09-08 17:39:28 +0000678/* C function to call the Python completion_display_matches */
679
680static void
681on_completion_display_matches_hook(char **matches,
682 int num_matches, int max_length)
683{
Christian Heimes32fbe592007-11-12 15:01:33 +0000684 int i;
685 PyObject *m, *s;
686 PyObject *r;
Christian Heimesaec75c32007-11-11 22:42:36 +0000687#ifdef WITH_THREAD
Christian Heimes32fbe592007-11-12 15:01:33 +0000688 PyGILState_STATE gilstate = PyGILState_Ensure();
Thomas Wouters89d996e2007-09-08 17:39:28 +0000689#endif
Christian Heimes32fbe592007-11-12 15:01:33 +0000690 m = PyList_New(num_matches);
691 for (i = 0; i < num_matches; i++) {
692 s = PyUnicode_FromString(matches[i+1]);
693 if (s) {
694 PyList_SetItem(m, i, s);
Thomas Wouters89d996e2007-09-08 17:39:28 +0000695 }
Christian Heimes32fbe592007-11-12 15:01:33 +0000696 else {
697 goto error;
Thomas Wouters89d996e2007-09-08 17:39:28 +0000698 }
Thomas Wouters89d996e2007-09-08 17:39:28 +0000699 }
Christian Heimes32fbe592007-11-12 15:01:33 +0000700 r = PyObject_CallFunction(completion_display_matches_hook,
701 "sOi", matches[0], m, max_length);
702
703 Py_DECREF(m), m=NULL;
704
705 if (r == NULL ||
706 (r != Py_None && PyInt_AsLong(r) == -1 && PyErr_Occurred())) {
707 goto error;
708 }
709
710 Py_DECREF(r);
711 goto done;
712 error:
713 PyErr_Clear();
714 Py_XDECREF(r);
715 done:
716#ifdef WITH_THREAD
717 PyGILState_Release(gilstate);
718#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000719}
720
721
Guido van Rossum290900a1997-09-26 21:51:21 +0000722/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000723
Guido van Rossum290900a1997-09-26 21:51:21 +0000724static char *
Guido van Rossumd8faa362007-04-27 19:54:29 +0000725on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000726{
Guido van Rossum290900a1997-09-26 21:51:21 +0000727 char *result = NULL;
728 if (completer != NULL) {
729 PyObject *r;
Christian Heimesaec75c32007-11-11 22:42:36 +0000730#ifdef WITH_THREAD
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000731 PyGILState_STATE gilstate = PyGILState_Ensure();
732#endif
Michael W. Hudson0c1ceaf2002-02-13 11:58:25 +0000733 rl_attempted_completion_over = 1;
Guido van Rossum290900a1997-09-26 21:51:21 +0000734 r = PyObject_CallFunction(completer, "si", text, state);
735 if (r == NULL)
736 goto error;
737 if (r == Py_None) {
738 result = NULL;
739 }
740 else {
Christian Heimesaec75c32007-11-11 22:42:36 +0000741 char *s = PyUnicode_AsString(r);
Guido van Rossum290900a1997-09-26 21:51:21 +0000742 if (s == NULL)
743 goto error;
744 result = strdup(s);
745 }
746 Py_DECREF(r);
747 goto done;
748 error:
749 PyErr_Clear();
750 Py_XDECREF(r);
751 done:
Christian Heimesaec75c32007-11-11 22:42:36 +0000752#ifdef WITH_THREAD
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000753 PyGILState_Release(gilstate);
754#endif
Georg Brandle677adc2005-09-29 13:40:49 +0000755 return result;
Guido van Rossum290900a1997-09-26 21:51:21 +0000756 }
757 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000758}
759
Guido van Rossum290900a1997-09-26 21:51:21 +0000760
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000761/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000762 * before calling the normal completer */
763
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000764static char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000765flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000766{
767 Py_XDECREF(begidx);
768 Py_XDECREF(endidx);
769 begidx = PyInt_FromLong((long) start);
770 endidx = PyInt_FromLong((long) end);
771 return completion_matches(text, *on_completion);
772}
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
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000781 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwis701abe72004-08-20 06:26:59 +0000782 if (!saved_locale)
783 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000784#endif
785
Skip Montanaroa0392742002-06-11 14:32:46 +0000786 using_history();
787
Guido van Rossum290900a1997-09-26 21:51:21 +0000788 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000789#if defined(PYOS_OS2) && defined(PYCC_GCC)
790 /* Allow $if term= in .inputrc to work */
791 rl_terminal_name = getenv("TERM");
792#endif
Guido van Rossum290900a1997-09-26 21:51: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);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000798 /* Set our hook functions */
799 rl_startup_hook = (Function *)on_startup_hook;
800#ifdef HAVE_RL_PRE_INPUT_HOOK
801 rl_pre_input_hook = (Function *)on_pre_input_hook;
802#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000803 /* Set our completion function */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000804 rl_attempted_completion_function = (CPPFunction *)flex_complete;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000805 /* Set Python word break characters */
806 rl_completer_word_break_characters =
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000807 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000808 /* All nonalphanums except '.' */
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000809#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum84271bb2002-05-30 15:41:56 +0000810 rl_completion_append_character ='\0';
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000811#endif
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000812
813 begidx = PyInt_FromLong(0L);
814 endidx = PyInt_FromLong(0L);
Barry Warsawf7612871999-01-29 21:55:03 +0000815 /* Initialize (allows .inputrc to override)
816 *
817 * XXX: A bug in the readline-2.2 library causes a memory leak
818 * inside this function. Nothing we can do about it.
819 */
Guido van Rossum290900a1997-09-26 21:51:21 +0000820 rl_initialize();
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000821
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000822 RESTORE_LOCALE(saved_locale)
Guido van Rossum290900a1997-09-26 21:51:21 +0000823}
824
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000825/* Wrapper around GNU readline that handles signals differently. */
826
827
828#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
829
830static char *completed_input_string;
831static void
832rlhandler(char *text)
833{
834 completed_input_string = text;
835 rl_callback_handler_remove();
836}
837
838extern PyThreadState* _PyOS_ReadlineTState;
839
840static char *
841readline_until_enter_or_signal(char *prompt, int *signal)
842{
843 char * not_done_reading = "";
844 fd_set selectset;
845
846 *signal = 0;
847#ifdef HAVE_RL_CATCH_SIGNAL
848 rl_catch_signals = 0;
849#endif
850
851 rl_callback_handler_install (prompt, rlhandler);
852 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000853
854 completed_input_string = not_done_reading;
855
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000856 while (completed_input_string == not_done_reading) {
857 int has_input = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000858
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000859 while (!has_input)
860 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000861
862 /* [Bug #1552726] Only limit the pause if an input hook has been
863 defined. */
864 struct timeval *timeoutp = NULL;
865 if (PyOS_InputHook)
866 timeoutp = &timeout;
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000867 FD_SET(fileno(rl_instream), &selectset);
868 /* select resets selectset if no input was available */
869 has_input = select(fileno(rl_instream) + 1, &selectset,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000870 NULL, NULL, timeoutp);
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000871 if(PyOS_InputHook) PyOS_InputHook();
872 }
873
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000874 if(has_input > 0) {
875 rl_callback_read_char();
876 }
877 else if (errno == EINTR) {
878 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000879#ifdef WITH_THREAD
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000880 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000881#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000882 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000883#ifdef WITH_THREAD
Michael W. Hudson23849902004-07-08 15:28:26 +0000884 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000885#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000886 if (s < 0) {
887 rl_free_line_state();
888 rl_cleanup_after_signal();
889 rl_callback_handler_remove();
890 *signal = 1;
891 completed_input_string = NULL;
892 }
893 }
894 }
895
896 return completed_input_string;
897}
898
899
900#else
Guido van Rossum290900a1997-09-26 21:51:21 +0000901
902/* Interrupt handler */
903
904static jmp_buf jbuf;
905
Guido van Rossum0969d361997-08-05 21:27:50 +0000906/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +0000907static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000908onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000909{
Guido van Rossum290900a1997-09-26 21:51:21 +0000910 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000911}
912
Guido van Rossum290900a1997-09-26 21:51:21 +0000913
Guido van Rossum0969d361997-08-05 21:27:50 +0000914static char *
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000915readline_until_enter_or_signal(char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +0000916{
Guido van Rossum174efc92000-09-16 16:37:53 +0000917 PyOS_sighandler_t old_inthandler;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000918 char *p;
919
920 *signal = 0;
Guido van Rossum74f31432003-01-07 20:01:29 +0000921
Guido van Rossum174efc92000-09-16 16:37:53 +0000922 old_inthandler = PyOS_setsig(SIGINT, onintr);
Guido van Rossum0969d361997-08-05 21:27:50 +0000923 if (setjmp(jbuf)) {
924#ifdef HAVE_SIGRELSE
925 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
926 sigrelse(SIGINT);
927#endif
Guido van Rossum174efc92000-09-16 16:37:53 +0000928 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000929 *signal = 1;
Guido van Rossum0969d361997-08-05 21:27:50 +0000930 return NULL;
931 }
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000932 rl_event_hook = PyOS_InputHook;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000933 p = readline(prompt);
934 PyOS_setsig(SIGINT, old_inthandler);
935
936 return p;
937}
938#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
939
940
941static char *
942call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
943{
Neal Norwitz1fa040b2004-08-25 01:20:18 +0000944 size_t n;
945 char *p, *q;
946 int signal;
947
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000948#ifdef SAVE_LOCALE
949 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwis701abe72004-08-20 06:26:59 +0000950 if (!saved_locale)
951 Py_FatalError("not enough memory to save locale");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000952 setlocale(LC_CTYPE, "");
953#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000954
Guido van Rossum74f31432003-01-07 20:01:29 +0000955 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
956 rl_instream = sys_stdin;
957 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000958#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum74f31432003-01-07 20:01:29 +0000959 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000960#endif
Guido van Rossum74f31432003-01-07 20:01:29 +0000961 }
962
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000963 p = readline_until_enter_or_signal(prompt, &signal);
964
965 /* we got an interrupt signal */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000966 if (signal) {
967 RESTORE_LOCALE(saved_locale)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000968 return NULL;
969 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000970
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000971 /* We got an EOF, return a empty string. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000972 if (p == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000973 p = PyMem_Malloc(1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000974 if (p != NULL)
975 *p = '\0';
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000976 RESTORE_LOCALE(saved_locale)
Guido van Rossum0969d361997-08-05 21:27:50 +0000977 return p;
978 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000979
980 /* we have a valid line */
Guido van Rossum0969d361997-08-05 21:27:50 +0000981 n = strlen(p);
Skip Montanaroa0392742002-06-11 14:32:46 +0000982 if (n > 0) {
983 char *line;
984 HISTORY_STATE *state = history_get_history_state();
985 if (state->length > 0)
986 line = history_get(state->length)->line;
987 else
988 line = "";
989 if (strcmp(p, line))
990 add_history(p);
991 /* the history docs don't say so, but the address of state
992 changes each time history_get_history_state is called
993 which makes me think it's freshly malloc'd memory...
994 on the other hand, the address of the last line stays the
995 same as long as history isn't extended, so it appears to
996 be malloc'd but managed by the history package... */
997 free(state);
998 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000999 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
1000 release the original. */
1001 q = p;
1002 p = PyMem_Malloc(n+2);
1003 if (p != NULL) {
1004 strncpy(p, q, n);
Guido van Rossum0969d361997-08-05 21:27:50 +00001005 p[n] = '\n';
1006 p[n+1] = '\0';
1007 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001008 free(q);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001009 RESTORE_LOCALE(saved_locale)
Guido van Rossum0969d361997-08-05 21:27:50 +00001010 return p;
1011}
1012
Guido van Rossum290900a1997-09-26 21:51:21 +00001013
1014/* Initialize the module */
1015
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001016PyDoc_STRVAR(doc_module,
1017"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001018
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001019PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001020initreadline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001021{
Guido van Rossum1ea64ea2000-10-02 15:53:08 +00001022 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +00001023
1024 m = Py_InitModule4("readline", readline_methods, doc_module,
1025 (PyObject *)NULL, PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001026 if (m == NULL)
1027 return;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001028
Guido van Rossum74f31432003-01-07 20:01:29 +00001029 PyOS_ReadlineFunctionPointer = call_readline;
1030 setup_readline();
Guido van Rossum0969d361997-08-05 21:27:50 +00001031}