blob: 90a5cc75aa414bb4bd4f862058745a96be45cca0 [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
Guido van Rossum0969d361997-08-05 21:27:50 +000041
Guido van Rossum290900a1997-09-26 21:51:21 +000042/* Exported function to send one line to readline's init file parser */
43
44static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000045parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000046{
Guido van Rossum3b5330e1998-12-04 15:34:39 +000047 char *s, *copy;
Guido van Rossum43713e52000-02-29 13:59:29 +000048 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000049 return NULL;
Guido van Rossum3b5330e1998-12-04 15:34:39 +000050 /* Make a copy -- rl_parse_and_bind() modifies its argument */
51 /* Bernard Herzog */
52 copy = malloc(1 + strlen(s));
53 if (copy == NULL)
54 return PyErr_NoMemory();
55 strcpy(copy, s);
56 rl_parse_and_bind(copy);
57 free(copy); /* Free the copy */
Guido van Rossum290900a1997-09-26 21:51:21 +000058 Py_INCREF(Py_None);
59 return Py_None;
60}
61
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000062PyDoc_STRVAR(doc_parse_and_bind,
63"parse_and_bind(string) -> None\n\
64Parse and execute single line of a readline init file.");
Guido van Rossum290900a1997-09-26 21:51:21 +000065
66
67/* Exported function to parse a readline init file */
68
69static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000070read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000071{
72 char *s = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +000073 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000074 return NULL;
75 errno = rl_read_init_file(s);
76 if (errno)
77 return PyErr_SetFromErrno(PyExc_IOError);
78 Py_INCREF(Py_None);
79 return Py_None;
80}
81
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000082PyDoc_STRVAR(doc_read_init_file,
83"read_init_file([filename]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +000084Parse a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000085The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +000086
87
Skip Montanaro28067822000-07-06 18:55:12 +000088/* Exported function to load a readline history file */
89
90static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000091read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +000092{
93 char *s = NULL;
94 if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
95 return NULL;
96 errno = read_history(s);
97 if (errno)
98 return PyErr_SetFromErrno(PyExc_IOError);
99 Py_INCREF(Py_None);
100 return Py_None;
101}
102
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000103static int _history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000104PyDoc_STRVAR(doc_read_history_file,
105"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000106Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000107The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000108
109
110/* Exported function to save a readline history file */
111
112static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000113write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000114{
115 char *s = NULL;
116 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
117 return NULL;
118 errno = write_history(s);
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000119 if (!errno && _history_length >= 0)
120 history_truncate_file(s, _history_length);
Skip Montanaro28067822000-07-06 18:55:12 +0000121 if (errno)
122 return PyErr_SetFromErrno(PyExc_IOError);
123 Py_INCREF(Py_None);
124 return Py_None;
125}
126
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000127PyDoc_STRVAR(doc_write_history_file,
128"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000129Save a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000130The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000131
132
Guido van Rossum74f31432003-01-07 20:01:29 +0000133/* Set history length */
134
135static PyObject*
136set_history_length(PyObject *self, PyObject *args)
137{
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000138 int length = _history_length;
Guido van Rossum74f31432003-01-07 20:01:29 +0000139 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
140 return NULL;
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000141 _history_length = length;
Guido van Rossum74f31432003-01-07 20:01:29 +0000142 Py_INCREF(Py_None);
143 return Py_None;
144}
145
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000146PyDoc_STRVAR(set_history_length_doc,
147"set_history_length(length) -> None\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000148set the maximal number of items which will be written to\n\
149the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000150history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000151
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000152
Guido van Rossum74f31432003-01-07 20:01:29 +0000153/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000154
155static PyObject*
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000156get_history_length(PyObject *self, PyObject *noarg)
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000157{
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000158 return PyInt_FromLong(_history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000159}
160
Guido van Rossum74f31432003-01-07 20:01:29 +0000161PyDoc_STRVAR(get_history_length_doc,
162"get_history_length() -> int\n\
163return the maximum number of items that will be written to\n\
164the history file.");
165
166
Martin v. Löwis0daad592001-09-30 21:09:59 +0000167/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000168
Martin v. Löwis0daad592001-09-30 21:09:59 +0000169static PyObject *
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000170set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000171{
172 PyObject *function = Py_None;
173 char buf[80];
Tim Peters885d4572001-11-28 20:27:42 +0000174 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000175 if (!PyArg_ParseTuple(args, buf, &function))
176 return NULL;
177 if (function == Py_None) {
178 Py_XDECREF(*hook_var);
179 *hook_var = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000180 }
181 else if (PyCallable_Check(function)) {
182 PyObject *tmp = *hook_var;
183 Py_INCREF(function);
184 *hook_var = function;
185 Py_XDECREF(tmp);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000186 }
187 else {
Tim Peters885d4572001-11-28 20:27:42 +0000188 PyOS_snprintf(buf, sizeof(buf),
189 "set_%.50s(func): argument not callable",
190 funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000191 PyErr_SetString(PyExc_TypeError, buf);
192 return NULL;
193 }
194 Py_INCREF(Py_None);
195 return Py_None;
196}
197
Guido van Rossum74f31432003-01-07 20:01:29 +0000198
Martin v. Löwis0daad592001-09-30 21:09:59 +0000199/* Exported functions to specify hook functions in Python */
200
Thomas Wouters89d996e2007-09-08 17:39:28 +0000201static PyObject *completion_display_matches_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000202static PyObject *startup_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000203
204#ifdef HAVE_RL_PRE_INPUT_HOOK
205static PyObject *pre_input_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000206#endif
207
208static PyObject *
Thomas Wouters89d996e2007-09-08 17:39:28 +0000209set_completion_display_matches_hook(PyObject *self, PyObject *args)
210{
211 return set_hook("completion_display_matches_hook",
212 &completion_display_matches_hook, args);
213}
214
215PyDoc_STRVAR(doc_set_completion_display_matches_hook,
216"set_completion_display_matches_hook([function]) -> None\n\
217Set or remove the completion display function.\n\
218The function is called as\n\
219 function(substitution, [matches], longest_match_length)\n\
220once each time matches need to be displayed.");
221
222static PyObject *
Martin v. Löwis0daad592001-09-30 21:09:59 +0000223set_startup_hook(PyObject *self, PyObject *args)
224{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000225 return set_hook("startup_hook", &startup_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000226}
227
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000228PyDoc_STRVAR(doc_set_startup_hook,
229"set_startup_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000230Set or remove the startup_hook function.\n\
231The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000232before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000233
Guido van Rossum74f31432003-01-07 20:01:29 +0000234
Martin v. Löwis0daad592001-09-30 21:09:59 +0000235#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000236
237/* Set pre-input hook */
238
Martin v. Löwis0daad592001-09-30 21:09:59 +0000239static PyObject *
240set_pre_input_hook(PyObject *self, PyObject *args)
241{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000242 return set_hook("pre_input_hook", &pre_input_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000243}
244
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000245PyDoc_STRVAR(doc_set_pre_input_hook,
246"set_pre_input_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000247Set or remove the pre_input_hook function.\n\
248The function is called with no arguments after the first prompt\n\
249has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000250characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000251
Martin v. Löwis0daad592001-09-30 21:09:59 +0000252#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000253
Guido van Rossum74f31432003-01-07 20:01:29 +0000254
Guido van Rossum290900a1997-09-26 21:51:21 +0000255/* Exported function to specify a word completer in Python */
256
257static PyObject *completer = NULL;
Guido van Rossum290900a1997-09-26 21:51:21 +0000258
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000259static PyObject *begidx = NULL;
260static PyObject *endidx = NULL;
261
Guido van Rossum74f31432003-01-07 20:01:29 +0000262
Thomas Wouters89d996e2007-09-08 17:39:28 +0000263/* Get the completion type for the scope of the tab-completion */
264static PyObject *
265get_completion_type(PyObject *self, PyObject *noarg)
266{
267 return PyInt_FromLong(rl_completion_type);
268}
269
270PyDoc_STRVAR(doc_get_completion_type,
271"get_completion_type() -> int\n\
272Get the type of completion being attempted.");
273
274
Guido van Rossum74f31432003-01-07 20:01:29 +0000275/* Get the beginning index for the scope of the tab-completion */
276
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000277static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000278get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000279{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000280 Py_INCREF(begidx);
281 return begidx;
282}
283
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000284PyDoc_STRVAR(doc_get_begidx,
285"get_begidx() -> int\n\
286get the beginning index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000287
Guido van Rossum74f31432003-01-07 20:01:29 +0000288
289/* Get the ending index for the scope of the tab-completion */
290
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000291static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000292get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000293{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000294 Py_INCREF(endidx);
295 return endidx;
296}
297
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000298PyDoc_STRVAR(doc_get_endidx,
299"get_endidx() -> int\n\
300get the ending index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000301
302
Guido van Rossum74f31432003-01-07 20:01:29 +0000303/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000304
305static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000306set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000307{
308 char *break_chars;
309
Guido van Rossum43713e52000-02-29 13:59:29 +0000310 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000311 return NULL;
312 }
Neal Norwitz0e0ee592002-04-21 15:03:18 +0000313 free((void*)rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000314 rl_completer_word_break_characters = strdup(break_chars);
315 Py_INCREF(Py_None);
316 return Py_None;
317}
318
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000319PyDoc_STRVAR(doc_set_completer_delims,
320"set_completer_delims(string) -> None\n\
321set the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000322
Skip Montanaroe5069012004-08-15 14:32:06 +0000323static PyObject *
324py_remove_history(PyObject *self, PyObject *args)
325{
326 int entry_number;
327 HIST_ENTRY *entry;
328
329 if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
330 return NULL;
Martin v. Löwis9533e342005-02-27 20:33:25 +0000331 if (entry_number < 0) {
332 PyErr_SetString(PyExc_ValueError,
333 "History index cannot be negative");
334 return NULL;
335 }
Skip Montanaroe5069012004-08-15 14:32:06 +0000336 entry = remove_history(entry_number);
337 if (!entry) {
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000338 PyErr_Format(PyExc_ValueError,
339 "No history item at position %d",
340 entry_number);
Skip Montanaroe5069012004-08-15 14:32:06 +0000341 return NULL;
342 }
343 /* free memory allocated for the history entry */
344 if (entry->line)
345 free(entry->line);
346 if (entry->data)
347 free(entry->data);
348 free(entry);
349
350 Py_INCREF(Py_None);
351 return Py_None;
352}
353
354PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000355"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000356remove history item given by its position");
357
358static PyObject *
359py_replace_history(PyObject *self, PyObject *args)
360{
361 int entry_number;
362 char *line;
363 HIST_ENTRY *old_entry;
364
365 if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number, &line)) {
366 return NULL;
367 }
Martin v. Löwis9533e342005-02-27 20:33:25 +0000368 if (entry_number < 0) {
369 PyErr_SetString(PyExc_ValueError,
370 "History index cannot be negative");
371 return NULL;
372 }
Skip Montanaroe5069012004-08-15 14:32:06 +0000373 old_entry = replace_history_entry(entry_number, line, (void *)NULL);
374 if (!old_entry) {
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000375 PyErr_Format(PyExc_ValueError,
376 "No history item at position %d",
377 entry_number);
Skip Montanaroe5069012004-08-15 14:32:06 +0000378 return NULL;
379 }
380 /* free memory allocated for the old history entry */
381 if (old_entry->line)
382 free(old_entry->line);
383 if (old_entry->data)
384 free(old_entry->data);
385 free(old_entry);
386
387 Py_INCREF(Py_None);
388 return Py_None;
389}
390
391PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000392"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000393replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000394
395/* Add a line to the history buffer */
396
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000397static PyObject *
398py_add_history(PyObject *self, PyObject *args)
399{
400 char *line;
401
402 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
403 return NULL;
404 }
405 add_history(line);
406 Py_INCREF(Py_None);
407 return Py_None;
408}
409
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000410PyDoc_STRVAR(doc_add_history,
411"add_history(string) -> None\n\
412add a line to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000413
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000414
Guido van Rossum74f31432003-01-07 20:01:29 +0000415/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000416
417static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000418get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000419{
Christian Heimesaec75c32007-11-11 22:42:36 +0000420 return PyUnicode_FromString(rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000421}
Guido van Rossum74f31432003-01-07 20:01:29 +0000422
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000423PyDoc_STRVAR(doc_get_completer_delims,
424"get_completer_delims() -> string\n\
425get the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000426
Guido van Rossum74f31432003-01-07 20:01:29 +0000427
428/* Set the completer function */
429
Guido van Rossum290900a1997-09-26 21:51:21 +0000430static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000431set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000432{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000433 return set_hook("completer", &completer, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000434}
435
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000436PyDoc_STRVAR(doc_set_completer,
437"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000438Set or remove the completer function.\n\
439The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000440for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000441It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000442
Guido van Rossum74f31432003-01-07 20:01:29 +0000443
Michael W. Hudson796df152003-01-30 10:12:51 +0000444static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000445get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000446{
447 if (completer == NULL) {
448 Py_INCREF(Py_None);
449 return Py_None;
450 }
451 Py_INCREF(completer);
452 return completer;
453}
454
455PyDoc_STRVAR(doc_get_completer,
456"get_completer() -> function\n\
457\n\
458Returns current completer function.");
459
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000460/* Exported function to get any element of history */
461
462static PyObject *
463get_history_item(PyObject *self, PyObject *args)
464{
465 int idx = 0;
466 HIST_ENTRY *hist_ent;
467
468 if (!PyArg_ParseTuple(args, "i:index", &idx))
469 return NULL;
470 if ((hist_ent = history_get(idx)))
Christian Heimesaec75c32007-11-11 22:42:36 +0000471 return PyUnicode_FromString(hist_ent->line);
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000472 else {
473 Py_INCREF(Py_None);
474 return Py_None;
475 }
476}
477
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000478PyDoc_STRVAR(doc_get_history_item,
479"get_history_item() -> string\n\
480return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000481
Guido van Rossum74f31432003-01-07 20:01:29 +0000482
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000483/* Exported function to get current length of history */
484
485static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000486get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000487{
488 HISTORY_STATE *hist_st;
489
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000490 hist_st = history_get_history_state();
491 return PyInt_FromLong(hist_st ? (long) hist_st->length : (long) 0);
492}
493
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000494PyDoc_STRVAR(doc_get_current_history_length,
495"get_current_history_length() -> integer\n\
496return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000497
Guido van Rossum74f31432003-01-07 20:01:29 +0000498
Guido van Rossum79378ff1997-10-07 14:53:21 +0000499/* Exported function to read the current line buffer */
500
501static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000502get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000503{
Christian Heimesaec75c32007-11-11 22:42:36 +0000504 return PyUnicode_FromString(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000505}
506
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000507PyDoc_STRVAR(doc_get_line_buffer,
508"get_line_buffer() -> string\n\
509return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000510
Guido van Rossum74f31432003-01-07 20:01:29 +0000511
Martin v. Löwise7a97962003-09-20 16:08:33 +0000512#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
513
514/* Exported function to clear the current history */
515
516static PyObject *
517py_clear_history(PyObject *self, PyObject *noarg)
518{
519 clear_history();
520 Py_INCREF(Py_None);
521 return Py_None;
522}
523
524PyDoc_STRVAR(doc_clear_history,
525"clear_history() -> None\n\
526Clear the current readline history.");
527#endif
528
529
Guido van Rossum79378ff1997-10-07 14:53:21 +0000530/* Exported function to insert text into the line buffer */
531
532static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000533insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000534{
535 char *s;
Guido van Rossum43713e52000-02-29 13:59:29 +0000536 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000537 return NULL;
538 rl_insert_text(s);
539 Py_INCREF(Py_None);
540 return Py_None;
541}
542
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000543PyDoc_STRVAR(doc_insert_text,
544"insert_text(string) -> None\n\
545Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000546
Guido van Rossum74f31432003-01-07 20:01:29 +0000547
548/* Redisplay the line buffer */
549
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000550static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000551redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000552{
553 rl_redisplay();
554 Py_INCREF(Py_None);
555 return Py_None;
556}
557
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000558PyDoc_STRVAR(doc_redisplay,
559"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000560Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000561contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000562
Guido van Rossum74f31432003-01-07 20:01:29 +0000563
Guido van Rossum290900a1997-09-26 21:51:21 +0000564/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000565
566static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000567{
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000568 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000569 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000570 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000571 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000572 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000573 {"read_history_file", read_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000574 METH_VARARGS, doc_read_history_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000575 {"write_history_file", write_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000576 METH_VARARGS, doc_write_history_file},
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000577 {"get_history_item", get_history_item,
578 METH_VARARGS, doc_get_history_item},
Neal Norwitz767f8352002-03-31 16:13:39 +0000579 {"get_current_history_length", (PyCFunction)get_current_history_length,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000580 METH_NOARGS, doc_get_current_history_length},
Guido van Rossum74f31432003-01-07 20:01:29 +0000581 {"set_history_length", set_history_length,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000582 METH_VARARGS, set_history_length_doc},
Guido van Rossum74f31432003-01-07 20:01:29 +0000583 {"get_history_length", get_history_length,
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000584 METH_NOARGS, get_history_length_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000585 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
Michael W. Hudson796df152003-01-30 10:12:51 +0000586 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
Thomas Wouters89d996e2007-09-08 17:39:28 +0000587 {"get_completion_type", get_completion_type,
588 METH_NOARGS, doc_get_completion_type},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000589 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
590 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000591
Guido van Rossum74f31432003-01-07 20:01:29 +0000592 {"set_completer_delims", set_completer_delims,
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000593 METH_VARARGS, doc_set_completer_delims},
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000594 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
Skip Montanaroe5069012004-08-15 14:32:06 +0000595 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
596 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000597 {"get_completer_delims", get_completer_delims,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000598 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000599
Thomas Wouters89d996e2007-09-08 17:39:28 +0000600 {"set_completion_display_matches_hook", set_completion_display_matches_hook,
601 METH_VARARGS, doc_set_completion_display_matches_hook},
Guido van Rossum74f31432003-01-07 20:01:29 +0000602 {"set_startup_hook", set_startup_hook,
603 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000604#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000605 {"set_pre_input_hook", set_pre_input_hook,
606 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000607#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000608#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
609 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
610#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000611 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000612};
613
Guido van Rossum05ac4492003-01-07 20:04:12 +0000614
Martin v. Löwis0daad592001-09-30 21:09:59 +0000615/* C function to call the Python hooks. */
616
617static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000618on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000619{
620 int result = 0;
621 if (func != NULL) {
622 PyObject *r;
Christian Heimesaec75c32007-11-11 22:42:36 +0000623#ifdef WITH_THREAD
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000624 PyGILState_STATE gilstate = PyGILState_Ensure();
625#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000626 r = PyObject_CallFunction(func, NULL);
627 if (r == NULL)
628 goto error;
Guido van Rossum74f31432003-01-07 20:01:29 +0000629 if (r == Py_None)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000630 result = 0;
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000631 else {
Martin v. Löwis0daad592001-09-30 21:09:59 +0000632 result = PyInt_AsLong(r);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000633 if (result == -1 && PyErr_Occurred())
634 goto error;
635 }
Martin v. Löwis0daad592001-09-30 21:09:59 +0000636 Py_DECREF(r);
637 goto done;
638 error:
639 PyErr_Clear();
640 Py_XDECREF(r);
641 done:
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000642#ifdef WITH_THREAD
643 PyGILState_Release(gilstate);
644#endif
Georg Brandle677adc2005-09-29 13:40:49 +0000645 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000646 }
647 return result;
648}
649
650static int
651on_startup_hook(void)
652{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000653 return on_hook(startup_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000654}
655
656#ifdef HAVE_RL_PRE_INPUT_HOOK
657static int
658on_pre_input_hook(void)
659{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000660 return on_hook(pre_input_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000661}
662#endif
663
Guido van Rossum05ac4492003-01-07 20:04:12 +0000664
Thomas Wouters89d996e2007-09-08 17:39:28 +0000665/* C function to call the Python completion_display_matches */
666
667static void
668on_completion_display_matches_hook(char **matches,
669 int num_matches, int max_length)
670{
671 if (completion_display_matches_hook != NULL) {
672 int i;
Christian Heimesaec75c32007-11-11 22:42:36 +0000673 PyObject *m, *s, *match;
Thomas Wouters89d996e2007-09-08 17:39:28 +0000674 PyObject *r;
Christian Heimesaec75c32007-11-11 22:42:36 +0000675#ifdef WITH_THREAD
Thomas Wouters89d996e2007-09-08 17:39:28 +0000676 PyGILState_STATE gilstate = PyGILState_Ensure();
677#endif
678 m = PyList_New(num_matches);
679 for (i = 0; i < num_matches; i++) {
Christian Heimesaec75c32007-11-11 22:42:36 +0000680 s = PyUnicode_FromString(matches[i+1]);
681 if (s) {
682 PyList_SetItem(m, i, s);
683 }
684 else {
685 goto error;
686 }
Thomas Wouters89d996e2007-09-08 17:39:28 +0000687 }
Thomas Wouters89d996e2007-09-08 17:39:28 +0000688 r = PyObject_CallFunction(completion_display_matches_hook,
689 "sOi", matches[0], m, max_length);
690
Christian Heimesaec75c32007-11-11 22:42:36 +0000691 Py_DECREF(m), m=NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +0000692
693 if (r == NULL ||
694 (r != Py_None && PyInt_AsLong(r) == -1 && PyErr_Occurred())) {
695 goto error;
696 }
697
698 Py_DECREF(r);
699 goto done;
700 error:
701 PyErr_Clear();
Christian Heimesaec75c32007-11-11 22:42:36 +0000702 Py_XDECREF(m);
Thomas Wouters89d996e2007-09-08 17:39:28 +0000703 Py_XDECREF(r);
704 done:
Christian Heimesaec75c32007-11-11 22:42:36 +0000705#ifdef WITH_THREAD
Thomas Wouters89d996e2007-09-08 17:39:28 +0000706 PyGILState_Release(gilstate);
707#endif
708 }
709}
710
711
Guido van Rossum290900a1997-09-26 21:51:21 +0000712/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000713
Guido van Rossum290900a1997-09-26 21:51:21 +0000714static char *
Guido van Rossumd8faa362007-04-27 19:54:29 +0000715on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000716{
Guido van Rossum290900a1997-09-26 21:51:21 +0000717 char *result = NULL;
718 if (completer != NULL) {
719 PyObject *r;
Christian Heimesaec75c32007-11-11 22:42:36 +0000720#ifdef WITH_THREAD
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000721 PyGILState_STATE gilstate = PyGILState_Ensure();
722#endif
Michael W. Hudson0c1ceaf2002-02-13 11:58:25 +0000723 rl_attempted_completion_over = 1;
Guido van Rossum290900a1997-09-26 21:51:21 +0000724 r = PyObject_CallFunction(completer, "si", text, state);
725 if (r == NULL)
726 goto error;
727 if (r == Py_None) {
728 result = NULL;
729 }
730 else {
Christian Heimesaec75c32007-11-11 22:42:36 +0000731 char *s = PyUnicode_AsString(r);
Guido van Rossum290900a1997-09-26 21:51:21 +0000732 if (s == NULL)
733 goto error;
734 result = strdup(s);
735 }
736 Py_DECREF(r);
737 goto done;
738 error:
739 PyErr_Clear();
740 Py_XDECREF(r);
741 done:
Christian Heimesaec75c32007-11-11 22:42:36 +0000742#ifdef WITH_THREAD
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000743 PyGILState_Release(gilstate);
744#endif
Georg Brandle677adc2005-09-29 13:40:49 +0000745 return result;
Guido van Rossum290900a1997-09-26 21:51:21 +0000746 }
747 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000748}
749
Guido van Rossum290900a1997-09-26 21:51:21 +0000750
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000751/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000752 * before calling the normal completer */
753
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000754static char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000755flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000756{
757 Py_XDECREF(begidx);
758 Py_XDECREF(endidx);
759 begidx = PyInt_FromLong((long) start);
760 endidx = PyInt_FromLong((long) end);
761 return completion_matches(text, *on_completion);
762}
763
Guido van Rossum05ac4492003-01-07 20:04:12 +0000764
Guido van Rossum290900a1997-09-26 21:51:21 +0000765/* Helper to initialize GNU readline properly. */
766
767static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000768setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000769{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000770#ifdef SAVE_LOCALE
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000771 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwis701abe72004-08-20 06:26:59 +0000772 if (!saved_locale)
773 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000774#endif
775
Skip Montanaroa0392742002-06-11 14:32:46 +0000776 using_history();
777
Guido van Rossum290900a1997-09-26 21:51:21 +0000778 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000779#if defined(PYOS_OS2) && defined(PYCC_GCC)
780 /* Allow $if term= in .inputrc to work */
781 rl_terminal_name = getenv("TERM");
782#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000783 /* Force rebind of TAB to insert-tab */
784 rl_bind_key('\t', rl_insert);
785 /* Bind both ESC-TAB and ESC-ESC to the completion function */
786 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
787 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000788 /* Set our hook functions */
Thomas Wouters89d996e2007-09-08 17:39:28 +0000789#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
790 rl_completion_display_matches_hook =
791 (rl_compdisp_func_t *)on_completion_display_matches_hook;
792#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000793 rl_startup_hook = (Function *)on_startup_hook;
794#ifdef HAVE_RL_PRE_INPUT_HOOK
795 rl_pre_input_hook = (Function *)on_pre_input_hook;
796#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000797 /* Set our completion function */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000798 rl_attempted_completion_function = (CPPFunction *)flex_complete;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000799 /* Set Python word break characters */
800 rl_completer_word_break_characters =
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000801 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000802 /* All nonalphanums except '.' */
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000803#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum84271bb2002-05-30 15:41:56 +0000804 rl_completion_append_character ='\0';
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000805#endif
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000806
807 begidx = PyInt_FromLong(0L);
808 endidx = PyInt_FromLong(0L);
Barry Warsawf7612871999-01-29 21:55:03 +0000809 /* Initialize (allows .inputrc to override)
810 *
811 * XXX: A bug in the readline-2.2 library causes a memory leak
812 * inside this function. Nothing we can do about it.
813 */
Guido van Rossum290900a1997-09-26 21:51:21 +0000814 rl_initialize();
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000815
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000816 RESTORE_LOCALE(saved_locale)
Guido van Rossum290900a1997-09-26 21:51:21 +0000817}
818
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000819/* Wrapper around GNU readline that handles signals differently. */
820
821
822#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
823
824static char *completed_input_string;
825static void
826rlhandler(char *text)
827{
828 completed_input_string = text;
829 rl_callback_handler_remove();
830}
831
832extern PyThreadState* _PyOS_ReadlineTState;
833
834static char *
835readline_until_enter_or_signal(char *prompt, int *signal)
836{
837 char * not_done_reading = "";
838 fd_set selectset;
839
840 *signal = 0;
841#ifdef HAVE_RL_CATCH_SIGNAL
842 rl_catch_signals = 0;
843#endif
844
845 rl_callback_handler_install (prompt, rlhandler);
846 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000847
848 completed_input_string = not_done_reading;
849
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000850 while (completed_input_string == not_done_reading) {
851 int has_input = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000852
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000853 while (!has_input)
854 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000855
856 /* [Bug #1552726] Only limit the pause if an input hook has been
857 defined. */
858 struct timeval *timeoutp = NULL;
859 if (PyOS_InputHook)
860 timeoutp = &timeout;
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000861 FD_SET(fileno(rl_instream), &selectset);
862 /* select resets selectset if no input was available */
863 has_input = select(fileno(rl_instream) + 1, &selectset,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000864 NULL, NULL, timeoutp);
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000865 if(PyOS_InputHook) PyOS_InputHook();
866 }
867
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000868 if(has_input > 0) {
869 rl_callback_read_char();
870 }
871 else if (errno == EINTR) {
872 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000873#ifdef WITH_THREAD
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000874 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000875#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000876 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000877#ifdef WITH_THREAD
Michael W. Hudson23849902004-07-08 15:28:26 +0000878 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000879#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000880 if (s < 0) {
881 rl_free_line_state();
882 rl_cleanup_after_signal();
883 rl_callback_handler_remove();
884 *signal = 1;
885 completed_input_string = NULL;
886 }
887 }
888 }
889
890 return completed_input_string;
891}
892
893
894#else
Guido van Rossum290900a1997-09-26 21:51:21 +0000895
896/* Interrupt handler */
897
898static jmp_buf jbuf;
899
Guido van Rossum0969d361997-08-05 21:27:50 +0000900/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +0000901static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000902onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000903{
Guido van Rossum290900a1997-09-26 21:51:21 +0000904 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000905}
906
Guido van Rossum290900a1997-09-26 21:51:21 +0000907
Guido van Rossum0969d361997-08-05 21:27:50 +0000908static char *
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000909readline_until_enter_or_signal(char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +0000910{
Guido van Rossum174efc92000-09-16 16:37:53 +0000911 PyOS_sighandler_t old_inthandler;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000912 char *p;
913
914 *signal = 0;
Guido van Rossum74f31432003-01-07 20:01:29 +0000915
Guido van Rossum174efc92000-09-16 16:37:53 +0000916 old_inthandler = PyOS_setsig(SIGINT, onintr);
Guido van Rossum0969d361997-08-05 21:27:50 +0000917 if (setjmp(jbuf)) {
918#ifdef HAVE_SIGRELSE
919 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
920 sigrelse(SIGINT);
921#endif
Guido van Rossum174efc92000-09-16 16:37:53 +0000922 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000923 *signal = 1;
Guido van Rossum0969d361997-08-05 21:27:50 +0000924 return NULL;
925 }
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000926 rl_event_hook = PyOS_InputHook;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000927 p = readline(prompt);
928 PyOS_setsig(SIGINT, old_inthandler);
929
930 return p;
931}
932#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
933
934
935static char *
936call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
937{
Neal Norwitz1fa040b2004-08-25 01:20:18 +0000938 size_t n;
939 char *p, *q;
940 int signal;
941
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000942#ifdef SAVE_LOCALE
943 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwis701abe72004-08-20 06:26:59 +0000944 if (!saved_locale)
945 Py_FatalError("not enough memory to save locale");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000946 setlocale(LC_CTYPE, "");
947#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000948
Guido van Rossum74f31432003-01-07 20:01:29 +0000949 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
950 rl_instream = sys_stdin;
951 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000952#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum74f31432003-01-07 20:01:29 +0000953 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000954#endif
Guido van Rossum74f31432003-01-07 20:01:29 +0000955 }
956
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000957 p = readline_until_enter_or_signal(prompt, &signal);
958
959 /* we got an interrupt signal */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000960 if (signal) {
961 RESTORE_LOCALE(saved_locale)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000962 return NULL;
963 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000964
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000965 /* We got an EOF, return a empty string. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000966 if (p == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000967 p = PyMem_Malloc(1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000968 if (p != NULL)
969 *p = '\0';
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000970 RESTORE_LOCALE(saved_locale)
Guido van Rossum0969d361997-08-05 21:27:50 +0000971 return p;
972 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000973
974 /* we have a valid line */
Guido van Rossum0969d361997-08-05 21:27:50 +0000975 n = strlen(p);
Skip Montanaroa0392742002-06-11 14:32:46 +0000976 if (n > 0) {
977 char *line;
978 HISTORY_STATE *state = history_get_history_state();
979 if (state->length > 0)
980 line = history_get(state->length)->line;
981 else
982 line = "";
983 if (strcmp(p, line))
984 add_history(p);
985 /* the history docs don't say so, but the address of state
986 changes each time history_get_history_state is called
987 which makes me think it's freshly malloc'd memory...
988 on the other hand, the address of the last line stays the
989 same as long as history isn't extended, so it appears to
990 be malloc'd but managed by the history package... */
991 free(state);
992 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000993 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
994 release the original. */
995 q = p;
996 p = PyMem_Malloc(n+2);
997 if (p != NULL) {
998 strncpy(p, q, n);
Guido van Rossum0969d361997-08-05 21:27:50 +0000999 p[n] = '\n';
1000 p[n+1] = '\0';
1001 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001002 free(q);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001003 RESTORE_LOCALE(saved_locale)
Guido van Rossum0969d361997-08-05 21:27:50 +00001004 return p;
1005}
1006
Guido van Rossum290900a1997-09-26 21:51:21 +00001007
1008/* Initialize the module */
1009
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001010PyDoc_STRVAR(doc_module,
1011"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001012
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001013PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001014initreadline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001015{
Guido van Rossum1ea64ea2000-10-02 15:53:08 +00001016 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +00001017
1018 m = Py_InitModule4("readline", readline_methods, doc_module,
1019 (PyObject *)NULL, PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001020 if (m == NULL)
1021 return;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001022
Guido van Rossum74f31432003-01-07 20:01:29 +00001023 PyOS_ReadlineFunctionPointer = call_readline;
1024 setup_readline();
Guido van Rossum0969d361997-08-05 21:27:50 +00001025}