blob: 85bbafdf4ed88b8d4a9a9ea818d4d8454c80e37f [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
Neal Norwitz5eaf7722006-07-16 02:15:27 +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)))
Neal Norwitzbd538702007-04-19 05:52:37 +000037#else
38extern char **completion_matches(char *, rl_compentry_func_t *);
Guido van Rossum353ae582001-07-10 16:45:32 +000039#endif
40
Martin v. Löwisf3548942007-11-12 04:53:02 +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
Martin v. Löwis58bd49f2007-09-04 13:13:14 +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 *
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000213set_completion_display_matches_hook(PyObject *self, PyObject *args)
214{
Martin v. Löwisf3548942007-11-12 04:53:02 +0000215 PyObject *result = set_hook("completion_display_matches_hook",
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000216 &completion_display_matches_hook, args);
Martin v. Löwisf3548942007-11-12 04:53:02 +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
Martin v. Löwis58bd49f2007-09-04 13:13:14 +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
Martin v. Löwis58bd49f2007-09-04 13:13:14 +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{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000433 return PyString_FromString(rl_completer_word_break_characters);
434}
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)))
Guido van Rossum05ac4492003-01-07 20:04:12 +0000484 return PyString_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{
Guido van Rossum79378ff1997-10-07 14:53:21 +0000517 return PyString_FromString(rl_line_buffer);
518}
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},
Martin v. Löwis58bd49f2007-09-04 13:13:14 +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
Martin v. Löwis58bd49f2007-09-04 13:13:14 +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;
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000636#ifdef WITH_THREAD
637 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
Martin v. Löwis58bd49f2007-09-04 13:13:14 +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{
Martin v. Löwisf3548942007-11-12 04:53:02 +0000684 int i;
685 PyObject *m, *s;
686 PyObject *r;
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000687#ifdef WITH_THREAD
Martin v. Löwisf3548942007-11-12 04:53:02 +0000688 PyGILState_STATE gilstate = PyGILState_Ensure();
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000689#endif
Martin v. Löwisf3548942007-11-12 04:53:02 +0000690 m = PyList_New(num_matches);
691 for (i = 0; i < num_matches; i++) {
692 s = PyString_FromString(matches[i+1]);
693 PyList_SetItem(m, i, s);
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000694 }
Martin v. Löwisf3548942007-11-12 04:53:02 +0000695
696 r = PyObject_CallFunction(completion_display_matches_hook,
697 "sOi", matches[0], m, max_length);
698
699 Py_DECREF(m);
700
701 if (r == NULL ||
702 (r != Py_None && PyInt_AsLong(r) == -1 && PyErr_Occurred())) {
703 goto error;
704 }
705
706 Py_DECREF(r);
707 goto done;
708 error:
709 PyErr_Clear();
710 Py_XDECREF(r);
711 done:
712#ifdef WITH_THREAD
713 PyGILState_Release(gilstate);
714#endif
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000715}
716
717
Guido van Rossum290900a1997-09-26 21:51:21 +0000718/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000719
Guido van Rossum290900a1997-09-26 21:51:21 +0000720static char *
Neal Norwitzbd538702007-04-19 05:52:37 +0000721on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000722{
Guido van Rossum290900a1997-09-26 21:51:21 +0000723 char *result = NULL;
724 if (completer != NULL) {
725 PyObject *r;
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000726#ifdef WITH_THREAD
727 PyGILState_STATE gilstate = PyGILState_Ensure();
728#endif
Michael W. Hudson0c1ceaf2002-02-13 11:58:25 +0000729 rl_attempted_completion_over = 1;
Guido van Rossum290900a1997-09-26 21:51:21 +0000730 r = PyObject_CallFunction(completer, "si", text, state);
731 if (r == NULL)
732 goto error;
733 if (r == Py_None) {
734 result = NULL;
735 }
736 else {
737 char *s = PyString_AsString(r);
738 if (s == NULL)
739 goto error;
740 result = strdup(s);
741 }
742 Py_DECREF(r);
743 goto done;
744 error:
745 PyErr_Clear();
746 Py_XDECREF(r);
747 done:
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000748#ifdef WITH_THREAD
749 PyGILState_Release(gilstate);
750#endif
Georg Brandle677adc2005-09-29 13:40:49 +0000751 return result;
Guido van Rossum290900a1997-09-26 21:51:21 +0000752 }
753 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000754}
755
Guido van Rossum290900a1997-09-26 21:51:21 +0000756
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000757/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000758 * before calling the normal completer */
759
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000760static char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000761flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000762{
763 Py_XDECREF(begidx);
764 Py_XDECREF(endidx);
765 begidx = PyInt_FromLong((long) start);
766 endidx = PyInt_FromLong((long) end);
767 return completion_matches(text, *on_completion);
768}
769
Guido van Rossum05ac4492003-01-07 20:04:12 +0000770
Guido van Rossum290900a1997-09-26 21:51:21 +0000771/* Helper to initialize GNU readline properly. */
772
773static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000774setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000775{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000776#ifdef SAVE_LOCALE
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000777 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwis701abe72004-08-20 06:26:59 +0000778 if (!saved_locale)
779 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000780#endif
781
Skip Montanaroa0392742002-06-11 14:32:46 +0000782 using_history();
783
Guido van Rossum290900a1997-09-26 21:51:21 +0000784 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000785#if defined(PYOS_OS2) && defined(PYCC_GCC)
786 /* Allow $if term= in .inputrc to work */
787 rl_terminal_name = getenv("TERM");
788#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000789 /* Force rebind of TAB to insert-tab */
790 rl_bind_key('\t', rl_insert);
791 /* Bind both ESC-TAB and ESC-ESC to the completion function */
792 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
793 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000794 /* Set our hook functions */
795 rl_startup_hook = (Function *)on_startup_hook;
796#ifdef HAVE_RL_PRE_INPUT_HOOK
797 rl_pre_input_hook = (Function *)on_pre_input_hook;
798#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000799 /* Set our completion function */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000800 rl_attempted_completion_function = (CPPFunction *)flex_complete;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000801 /* Set Python word break characters */
802 rl_completer_word_break_characters =
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000803 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000804 /* All nonalphanums except '.' */
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000805#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum84271bb2002-05-30 15:41:56 +0000806 rl_completion_append_character ='\0';
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000807#endif
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000808
809 begidx = PyInt_FromLong(0L);
810 endidx = PyInt_FromLong(0L);
Barry Warsawf7612871999-01-29 21:55:03 +0000811 /* Initialize (allows .inputrc to override)
812 *
813 * XXX: A bug in the readline-2.2 library causes a memory leak
814 * inside this function. Nothing we can do about it.
815 */
Guido van Rossum290900a1997-09-26 21:51:21 +0000816 rl_initialize();
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000817
Neal Norwitz5eaf7722006-07-16 02:15:27 +0000818 RESTORE_LOCALE(saved_locale)
Guido van Rossum290900a1997-09-26 21:51:21 +0000819}
820
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000821/* Wrapper around GNU readline that handles signals differently. */
822
823
824#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
825
826static char *completed_input_string;
827static void
828rlhandler(char *text)
829{
830 completed_input_string = text;
831 rl_callback_handler_remove();
832}
833
834extern PyThreadState* _PyOS_ReadlineTState;
835
836static char *
837readline_until_enter_or_signal(char *prompt, int *signal)
838{
839 char * not_done_reading = "";
840 fd_set selectset;
841
842 *signal = 0;
843#ifdef HAVE_RL_CATCH_SIGNAL
844 rl_catch_signals = 0;
845#endif
846
847 rl_callback_handler_install (prompt, rlhandler);
848 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000849
850 completed_input_string = not_done_reading;
851
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000852 while (completed_input_string == not_done_reading) {
853 int has_input = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000854
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000855 while (!has_input)
856 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Andrew M. Kuchling62e475b2006-09-07 13:59:38 +0000857
858 /* [Bug #1552726] Only limit the pause if an input hook has been
859 defined. */
860 struct timeval *timeoutp = NULL;
861 if (PyOS_InputHook)
862 timeoutp = &timeout;
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000863 FD_SET(fileno(rl_instream), &selectset);
864 /* select resets selectset if no input was available */
865 has_input = select(fileno(rl_instream) + 1, &selectset,
Andrew M. Kuchling62e475b2006-09-07 13:59:38 +0000866 NULL, NULL, timeoutp);
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000867 if(PyOS_InputHook) PyOS_InputHook();
868 }
869
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000870 if(has_input > 0) {
871 rl_callback_read_char();
872 }
873 else if (errno == EINTR) {
874 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000875#ifdef WITH_THREAD
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000876 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000877#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000878 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000879#ifdef WITH_THREAD
Michael W. Hudson23849902004-07-08 15:28:26 +0000880 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000881#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000882 if (s < 0) {
883 rl_free_line_state();
884 rl_cleanup_after_signal();
885 rl_callback_handler_remove();
886 *signal = 1;
887 completed_input_string = NULL;
888 }
889 }
890 }
891
892 return completed_input_string;
893}
894
895
896#else
Guido van Rossum290900a1997-09-26 21:51:21 +0000897
898/* Interrupt handler */
899
900static jmp_buf jbuf;
901
Guido van Rossum0969d361997-08-05 21:27:50 +0000902/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +0000903static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000904onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000905{
Guido van Rossum290900a1997-09-26 21:51:21 +0000906 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000907}
908
Guido van Rossum290900a1997-09-26 21:51:21 +0000909
Guido van Rossum0969d361997-08-05 21:27:50 +0000910static char *
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000911readline_until_enter_or_signal(char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +0000912{
Guido van Rossum174efc92000-09-16 16:37:53 +0000913 PyOS_sighandler_t old_inthandler;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000914 char *p;
915
916 *signal = 0;
Guido van Rossum74f31432003-01-07 20:01:29 +0000917
Guido van Rossum174efc92000-09-16 16:37:53 +0000918 old_inthandler = PyOS_setsig(SIGINT, onintr);
Guido van Rossum0969d361997-08-05 21:27:50 +0000919 if (setjmp(jbuf)) {
920#ifdef HAVE_SIGRELSE
921 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
922 sigrelse(SIGINT);
923#endif
Guido van Rossum174efc92000-09-16 16:37:53 +0000924 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000925 *signal = 1;
Guido van Rossum0969d361997-08-05 21:27:50 +0000926 return NULL;
927 }
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000928 rl_event_hook = PyOS_InputHook;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000929 p = readline(prompt);
930 PyOS_setsig(SIGINT, old_inthandler);
931
932 return p;
933}
934#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
935
936
937static char *
938call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
939{
Neal Norwitz1fa040b2004-08-25 01:20:18 +0000940 size_t n;
941 char *p, *q;
942 int signal;
943
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000944#ifdef SAVE_LOCALE
945 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwis701abe72004-08-20 06:26:59 +0000946 if (!saved_locale)
947 Py_FatalError("not enough memory to save locale");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000948 setlocale(LC_CTYPE, "");
949#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000950
Guido van Rossum74f31432003-01-07 20:01:29 +0000951 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
952 rl_instream = sys_stdin;
953 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000954#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum74f31432003-01-07 20:01:29 +0000955 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000956#endif
Guido van Rossum74f31432003-01-07 20:01:29 +0000957 }
958
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000959 p = readline_until_enter_or_signal(prompt, &signal);
960
961 /* we got an interrupt signal */
Neal Norwitz5eaf7722006-07-16 02:15:27 +0000962 if (signal) {
963 RESTORE_LOCALE(saved_locale)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000964 return NULL;
965 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000966
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000967 /* We got an EOF, return a empty string. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000968 if (p == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000969 p = PyMem_Malloc(1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000970 if (p != NULL)
971 *p = '\0';
Neal Norwitz5eaf7722006-07-16 02:15:27 +0000972 RESTORE_LOCALE(saved_locale)
Guido van Rossum0969d361997-08-05 21:27:50 +0000973 return p;
974 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000975
976 /* we have a valid line */
Guido van Rossum0969d361997-08-05 21:27:50 +0000977 n = strlen(p);
Skip Montanaroa0392742002-06-11 14:32:46 +0000978 if (n > 0) {
979 char *line;
980 HISTORY_STATE *state = history_get_history_state();
981 if (state->length > 0)
982 line = history_get(state->length)->line;
983 else
984 line = "";
985 if (strcmp(p, line))
986 add_history(p);
987 /* the history docs don't say so, but the address of state
988 changes each time history_get_history_state is called
989 which makes me think it's freshly malloc'd memory...
990 on the other hand, the address of the last line stays the
991 same as long as history isn't extended, so it appears to
992 be malloc'd but managed by the history package... */
993 free(state);
994 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000995 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
996 release the original. */
997 q = p;
998 p = PyMem_Malloc(n+2);
999 if (p != NULL) {
1000 strncpy(p, q, n);
Guido van Rossum0969d361997-08-05 21:27:50 +00001001 p[n] = '\n';
1002 p[n+1] = '\0';
1003 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001004 free(q);
Neal Norwitz5eaf7722006-07-16 02:15:27 +00001005 RESTORE_LOCALE(saved_locale)
Guido van Rossum0969d361997-08-05 21:27:50 +00001006 return p;
1007}
1008
Guido van Rossum290900a1997-09-26 21:51:21 +00001009
1010/* Initialize the module */
1011
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001012PyDoc_STRVAR(doc_module,
1013"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001014
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001015PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001016initreadline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001017{
Guido van Rossum1ea64ea2000-10-02 15:53:08 +00001018 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +00001019
1020 m = Py_InitModule4("readline", readline_methods, doc_module,
1021 (PyObject *)NULL, PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001022 if (m == NULL)
1023 return;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001024
Guido van Rossum74f31432003-01-07 20:01:29 +00001025 PyOS_ReadlineFunctionPointer = call_readline;
1026 setup_readline();
Guido van Rossum0969d361997-08-05 21:27:50 +00001027}