blob: fe653b821b2dcbe2b65c9cadb6ed1e360463b79e [file] [log] [blame]
Guido van Rossum290900a1997-09-26 21:51:21 +00001/* This module makes GNU readline available to Python. It has ideas
2 * contributed by Lee Busby, LLNL, and William Magro, Cornell Theory
3 * Center. The completer interface was inspired by Lele Gaifax.
4 *
5 * More recently, it was largely rewritten by Guido van Rossum who is
6 * now maintaining it.
Guido van Rossum0969d361997-08-05 21:27:50 +00007 */
8
Guido van Rossum290900a1997-09-26 21:51:21 +00009/* Standard definitions */
Guido van Rossum0969d361997-08-05 21:27:50 +000010#include "Python.h"
11#include <setjmp.h>
12#include <signal.h>
Guido van Rossum290900a1997-09-26 21:51:21 +000013#include <errno.h>
Guido van Rossum0969d361997-08-05 21:27:50 +000014
Guido van Rossum73bacfc1998-01-19 22:05:22 +000015#ifdef HAVE_UNISTD_H
16#include <unistd.h> /* For isatty() */
17#endif
18
Guido van Rossum290900a1997-09-26 21:51:21 +000019/* GNU readline definitions */
Guido van Rossumb0e51b22001-04-13 18:14:27 +000020#undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
Guido van Rossumbcc20741998-08-04 22:53:56 +000021#include <readline/readline.h>
22#include <readline/history.h>
Guido van Rossum730806d1998-04-10 22:27:42 +000023
Guido van Rossum353ae582001-07-10 16:45:32 +000024#ifdef HAVE_RL_COMPLETION_MATCHES
25#define completion_matches(x, y) rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
26#endif
27
Guido van Rossum290900a1997-09-26 21:51:21 +000028/* Pointers needed from outside (but not declared in a header file). */
Guido van Rossum5a530192001-01-10 21:03:32 +000029extern DL_IMPORT(int) (*PyOS_InputHook)(void);
30extern DL_IMPORT(char) *(*PyOS_ReadlineFunctionPointer)(char *);
Guido van Rossum0969d361997-08-05 21:27:50 +000031
Guido van Rossum0969d361997-08-05 21:27:50 +000032
Guido van Rossum290900a1997-09-26 21:51:21 +000033/* Exported function to send one line to readline's init file parser */
34
35static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000036parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000037{
Guido van Rossum3b5330e1998-12-04 15:34:39 +000038 char *s, *copy;
Guido van Rossum43713e52000-02-29 13:59:29 +000039 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000040 return NULL;
Guido van Rossum3b5330e1998-12-04 15:34:39 +000041 /* Make a copy -- rl_parse_and_bind() modifies its argument */
42 /* Bernard Herzog */
43 copy = malloc(1 + strlen(s));
44 if (copy == NULL)
45 return PyErr_NoMemory();
46 strcpy(copy, s);
47 rl_parse_and_bind(copy);
48 free(copy); /* Free the copy */
Guido van Rossum290900a1997-09-26 21:51:21 +000049 Py_INCREF(Py_None);
50 return Py_None;
51}
52
53static char doc_parse_and_bind[] = "\
54parse_and_bind(string) -> None\n\
55Parse and execute single line of a readline init file.\
56";
57
58
59/* Exported function to parse a readline init file */
60
61static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000062read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000063{
64 char *s = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +000065 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000066 return NULL;
67 errno = rl_read_init_file(s);
68 if (errno)
69 return PyErr_SetFromErrno(PyExc_IOError);
70 Py_INCREF(Py_None);
71 return Py_None;
72}
73
74static char doc_read_init_file[] = "\
75read_init_file([filename]) -> None\n\
76Parse a readline initialization file.\n\
77The default filename is the last filename used.\
78";
79
80
Skip Montanaro28067822000-07-06 18:55:12 +000081/* Exported function to load a readline history file */
82
83static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000084read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +000085{
86 char *s = NULL;
87 if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
88 return NULL;
89 errno = read_history(s);
90 if (errno)
91 return PyErr_SetFromErrno(PyExc_IOError);
92 Py_INCREF(Py_None);
93 return Py_None;
94}
95
Skip Montanaro49bd24d2000-07-19 16:54:53 +000096static int history_length = -1; /* do not truncate history by default */
Skip Montanaro28067822000-07-06 18:55:12 +000097static char doc_read_history_file[] = "\
98read_history_file([filename]) -> None\n\
99Load a readline history file.\n\
100The default filename is ~/.history.\
101";
102
103
104/* Exported function to save a readline history file */
105
106static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000107write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000108{
109 char *s = NULL;
110 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
111 return NULL;
112 errno = write_history(s);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000113 if (!errno && history_length >= 0)
114 history_truncate_file(s, history_length);
Skip Montanaro28067822000-07-06 18:55:12 +0000115 if (errno)
116 return PyErr_SetFromErrno(PyExc_IOError);
117 Py_INCREF(Py_None);
118 return Py_None;
119}
120
121static char doc_write_history_file[] = "\
122write_history_file([filename]) -> None\n\
123Save a readline history file.\n\
124The default filename is ~/.history.\
125";
126
127
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000128static char set_history_length_doc[] = "\
129set_history_length(length) -> None\n\
130set the maximal number of items which will be written to\n\
131the history file. A negative length is used to inhibit\n\
132history truncation.\n\
133";
134
135static PyObject*
136set_history_length(PyObject *self, PyObject *args)
137{
138 int length = history_length;
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000139 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
140 return NULL;
141 history_length = length;
142 Py_INCREF(Py_None);
143 return Py_None;
144}
145
146
147
148static char get_history_length_doc[] = "\
149get_history_length() -> int\n\
150return the current history length value.\n\
151";
152
153static PyObject*
154get_history_length(PyObject *self, PyObject *args)
155{
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000156 if (!PyArg_ParseTuple(args, ":get_history_length"))
157 return NULL;
158 return Py_BuildValue("i", history_length);
159}
160
Martin v. Löwis0daad592001-09-30 21:09:59 +0000161/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000162
Martin v. Löwis0daad592001-09-30 21:09:59 +0000163static PyObject *
164set_hook(const char * funcname, PyObject **hook_var, PyThreadState **tstate, PyObject *args)
165{
166 PyObject *function = Py_None;
167 char buf[80];
Tim Peters885d4572001-11-28 20:27:42 +0000168 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000169 if (!PyArg_ParseTuple(args, buf, &function))
170 return NULL;
171 if (function == Py_None) {
172 Py_XDECREF(*hook_var);
173 *hook_var = NULL;
174 *tstate = NULL;
175 }
176 else if (PyCallable_Check(function)) {
177 PyObject *tmp = *hook_var;
178 Py_INCREF(function);
179 *hook_var = function;
180 Py_XDECREF(tmp);
181 *tstate = PyThreadState_Get();
182 }
183 else {
Tim Peters885d4572001-11-28 20:27:42 +0000184 PyOS_snprintf(buf, sizeof(buf),
185 "set_%.50s(func): argument not callable",
186 funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000187 PyErr_SetString(PyExc_TypeError, buf);
188 return NULL;
189 }
190 Py_INCREF(Py_None);
191 return Py_None;
192}
193
194/* Exported functions to specify hook functions in Python */
195
196static PyObject *startup_hook = NULL;
197static PyThreadState *startup_hook_tstate = NULL;
198
199#ifdef HAVE_RL_PRE_INPUT_HOOK
200static PyObject *pre_input_hook = NULL;
201static PyThreadState *pre_input_hook_tstate = NULL;
202#endif
203
204static PyObject *
205set_startup_hook(PyObject *self, PyObject *args)
206{
207 return set_hook("startup_hook", &startup_hook, &startup_hook_tstate, args);
208}
209
210static char doc_set_startup_hook[] = "\
211set_startup_hook([function]) -> None\n\
212Set or remove the startup_hook function.\n\
213The function is called with no arguments just\n\
214before readline prints the first prompt.\n\
215";
216
217#ifdef HAVE_RL_PRE_INPUT_HOOK
218static PyObject *
219set_pre_input_hook(PyObject *self, PyObject *args)
220{
221 return set_hook("pre_input_hook", &pre_input_hook, &pre_input_hook_tstate, args);
222}
223
224static char doc_set_pre_input_hook[] = "\
225set_pre_input_hook([function]) -> None\n\
226Set or remove the pre_input_hook function.\n\
227The function is called with no arguments after the first prompt\n\
228has been printed and just before readline starts reading input\n\
229characters.\n\
230";
231#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000232
Guido van Rossum290900a1997-09-26 21:51:21 +0000233/* Exported function to specify a word completer in Python */
234
235static PyObject *completer = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000236static PyThreadState *completer_tstate = NULL;
Guido van Rossum290900a1997-09-26 21:51:21 +0000237
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000238static PyObject *begidx = NULL;
239static PyObject *endidx = NULL;
240
241/* get the beginning index for the scope of the tab-completion */
242static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000243get_begidx(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000244{
245 if(!PyArg_NoArgs(args)) {
246 return NULL;
247 }
248 Py_INCREF(begidx);
249 return begidx;
250}
251
252static char doc_get_begidx[] = "\
253get_begidx() -> int\n\
254get the beginning index of the readline tab-completion scope";
255
256/* get the ending index for the scope of the tab-completion */
257static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000258get_endidx(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000259{
260 if(!PyArg_NoArgs(args)) {
261 return NULL;
262 }
263 Py_INCREF(endidx);
264 return endidx;
265}
266
267static char doc_get_endidx[] = "\
268get_endidx() -> int\n\
269get the ending index of the readline tab-completion scope";
270
271
272/* set the tab-completion word-delimiters that readline uses */
273
274static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000275set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000276{
277 char *break_chars;
278
Guido van Rossum43713e52000-02-29 13:59:29 +0000279 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000280 return NULL;
281 }
282 free(rl_completer_word_break_characters);
283 rl_completer_word_break_characters = strdup(break_chars);
284 Py_INCREF(Py_None);
285 return Py_None;
286}
287
288static char doc_set_completer_delims[] = "\
289set_completer_delims(string) -> None\n\
290set the readline word delimiters for tab-completion";
291
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000292static PyObject *
293py_add_history(PyObject *self, PyObject *args)
294{
295 char *line;
296
297 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
298 return NULL;
299 }
300 add_history(line);
301 Py_INCREF(Py_None);
302 return Py_None;
303}
304
305static char doc_add_history[] = "\
306add_history(string) -> None\n\
307add a line to the history buffer";
308
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000309
310/* get the tab-completion word-delimiters that readline uses */
311
312static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000313get_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000314{
315 if(!PyArg_NoArgs(args)) {
316 return NULL;
317 }
318 return PyString_FromString(rl_completer_word_break_characters);
319}
320
321static char doc_get_completer_delims[] = "\
322get_completer_delims() -> string\n\
323get the readline word delimiters for tab-completion";
324
Guido van Rossum290900a1997-09-26 21:51:21 +0000325static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000326set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000327{
Martin v. Löwis0daad592001-09-30 21:09:59 +0000328 return set_hook("completer", &completer, &completer_tstate, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000329}
330
331static char doc_set_completer[] = "\
332set_completer([function]) -> None\n\
333Set or remove the completer function.\n\
334The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000335for state in 0, 1, 2, ..., until it returns a non-string.\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000336It should return the next possible completion starting with 'text'.\
337";
338
Guido van Rossum79378ff1997-10-07 14:53:21 +0000339/* Exported function to read the current line buffer */
340
341static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000342get_line_buffer(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000343{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000344 if (!PyArg_NoArgs(args))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000345 return NULL;
346 return PyString_FromString(rl_line_buffer);
347}
348
349static char doc_get_line_buffer[] = "\
Guido van Rossuma88c5f31998-05-20 15:50:56 +0000350get_line_buffer() -> string\n\
Guido van Rossum79378ff1997-10-07 14:53:21 +0000351return the current contents of the line buffer.\
352";
353
354/* Exported function to insert text into the line buffer */
355
356static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000357insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000358{
359 char *s;
Guido van Rossum43713e52000-02-29 13:59:29 +0000360 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000361 return NULL;
362 rl_insert_text(s);
363 Py_INCREF(Py_None);
364 return Py_None;
365}
366
367
368static char doc_insert_text[] = "\
369insert_text(string) -> None\n\
370Insert text into the command line.\
371";
372
Guido van Rossum290900a1997-09-26 21:51:21 +0000373
374/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000375
376static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000377{
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000378 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000379 {"get_line_buffer", get_line_buffer,
380 METH_OLDARGS, doc_get_line_buffer},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000381 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
382 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
383 {"read_history_file", read_history_file,
384 METH_VARARGS, doc_read_history_file},
385 {"write_history_file", write_history_file,
386 METH_VARARGS, doc_write_history_file},
387 {"set_history_length", set_history_length,
388 METH_VARARGS, set_history_length_doc},
389 {"get_history_length", get_history_length,
390 METH_VARARGS, get_history_length_doc},
391 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000392 {"get_begidx", get_begidx, METH_OLDARGS, doc_get_begidx},
393 {"get_endidx", get_endidx, METH_OLDARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000394
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000395 {"set_completer_delims", set_completer_delims,
396 METH_VARARGS, doc_set_completer_delims},
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000397 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000398 {"get_completer_delims", get_completer_delims,
399 METH_OLDARGS, doc_get_completer_delims},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000400
401 {"set_startup_hook", set_startup_hook, METH_VARARGS, doc_set_startup_hook},
402#ifdef HAVE_RL_PRE_INPUT_HOOK
403 {"set_pre_input_hook", set_pre_input_hook, METH_VARARGS, doc_set_pre_input_hook},
404#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000405 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000406};
407
Martin v. Löwis0daad592001-09-30 21:09:59 +0000408/* C function to call the Python hooks. */
409
410static int
411on_hook(PyObject *func, PyThreadState *tstate)
412{
413 int result = 0;
414 if (func != NULL) {
415 PyObject *r;
416 PyThreadState *save_tstate;
417 /* Note that readline is called with the interpreter
418 lock released! */
419 save_tstate = PyThreadState_Swap(NULL);
420 PyEval_RestoreThread(tstate);
421 r = PyObject_CallFunction(func, NULL);
422 if (r == NULL)
423 goto error;
424 if (r == Py_None)
425 result = 0;
426 else
427 result = PyInt_AsLong(r);
428 Py_DECREF(r);
429 goto done;
430 error:
431 PyErr_Clear();
432 Py_XDECREF(r);
433 done:
434 PyEval_SaveThread();
435 PyThreadState_Swap(save_tstate);
436 }
437 return result;
438}
439
440static int
441on_startup_hook(void)
442{
443 return on_hook(startup_hook, startup_hook_tstate);
444}
445
446#ifdef HAVE_RL_PRE_INPUT_HOOK
447static int
448on_pre_input_hook(void)
449{
450 return on_hook(pre_input_hook, pre_input_hook_tstate);
451}
452#endif
453
Guido van Rossum290900a1997-09-26 21:51:21 +0000454/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000455
Guido van Rossum290900a1997-09-26 21:51:21 +0000456static char *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000457on_completion(char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000458{
Guido van Rossum290900a1997-09-26 21:51:21 +0000459 char *result = NULL;
460 if (completer != NULL) {
461 PyObject *r;
Guido van Rossuma59406a1997-10-10 17:39:19 +0000462 PyThreadState *save_tstate;
Guido van Rossum290900a1997-09-26 21:51:21 +0000463 /* Note that readline is called with the interpreter
464 lock released! */
Guido van Rossuma59406a1997-10-10 17:39:19 +0000465 save_tstate = PyThreadState_Swap(NULL);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000466 PyEval_RestoreThread(completer_tstate);
Guido van Rossum290900a1997-09-26 21:51:21 +0000467 r = PyObject_CallFunction(completer, "si", text, state);
468 if (r == NULL)
469 goto error;
470 if (r == Py_None) {
471 result = NULL;
472 }
473 else {
474 char *s = PyString_AsString(r);
475 if (s == NULL)
476 goto error;
477 result = strdup(s);
478 }
479 Py_DECREF(r);
480 goto done;
481 error:
482 PyErr_Clear();
483 Py_XDECREF(r);
484 done:
485 PyEval_SaveThread();
Guido van Rossuma59406a1997-10-10 17:39:19 +0000486 PyThreadState_Swap(save_tstate);
Guido van Rossum290900a1997-09-26 21:51:21 +0000487 }
488 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000489}
490
Guido van Rossum290900a1997-09-26 21:51:21 +0000491
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000492/* a more flexible constructor that saves the "begidx" and "endidx"
493 * before calling the normal completer */
494
495char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000496flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000497{
498 Py_XDECREF(begidx);
499 Py_XDECREF(endidx);
500 begidx = PyInt_FromLong((long) start);
501 endidx = PyInt_FromLong((long) end);
502 return completion_matches(text, *on_completion);
503}
504
Guido van Rossum290900a1997-09-26 21:51:21 +0000505/* Helper to initialize GNU readline properly. */
506
507static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000508setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000509{
510 rl_readline_name = "python";
511 /* Force rebind of TAB to insert-tab */
512 rl_bind_key('\t', rl_insert);
513 /* Bind both ESC-TAB and ESC-ESC to the completion function */
514 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
515 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000516 /* Set our hook functions */
517 rl_startup_hook = (Function *)on_startup_hook;
518#ifdef HAVE_RL_PRE_INPUT_HOOK
519 rl_pre_input_hook = (Function *)on_pre_input_hook;
520#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000521 /* Set our completion function */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000522 rl_attempted_completion_function = (CPPFunction *)flex_complete;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000523 /* Set Python word break characters */
524 rl_completer_word_break_characters =
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000525 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000526 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000527
528 begidx = PyInt_FromLong(0L);
529 endidx = PyInt_FromLong(0L);
Barry Warsawf7612871999-01-29 21:55:03 +0000530 /* Initialize (allows .inputrc to override)
531 *
532 * XXX: A bug in the readline-2.2 library causes a memory leak
533 * inside this function. Nothing we can do about it.
534 */
Guido van Rossum290900a1997-09-26 21:51:21 +0000535 rl_initialize();
536}
537
538
539/* Interrupt handler */
540
541static jmp_buf jbuf;
542
Guido van Rossum0969d361997-08-05 21:27:50 +0000543/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +0000544static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000545onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000546{
Guido van Rossum290900a1997-09-26 21:51:21 +0000547 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000548}
549
Guido van Rossum290900a1997-09-26 21:51:21 +0000550
551/* Wrapper around GNU readline that handles signals differently. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000552
553static char *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000554call_readline(char *prompt)
Guido van Rossum0969d361997-08-05 21:27:50 +0000555{
Guido van Rossum26418a92000-06-28 21:30:31 +0000556 size_t n;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000557 char *p, *q;
Guido van Rossum174efc92000-09-16 16:37:53 +0000558 PyOS_sighandler_t old_inthandler;
559
560 old_inthandler = PyOS_setsig(SIGINT, onintr);
Guido van Rossum0969d361997-08-05 21:27:50 +0000561 if (setjmp(jbuf)) {
562#ifdef HAVE_SIGRELSE
563 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
564 sigrelse(SIGINT);
565#endif
Guido van Rossum174efc92000-09-16 16:37:53 +0000566 PyOS_setsig(SIGINT, old_inthandler);
Guido van Rossum0969d361997-08-05 21:27:50 +0000567 return NULL;
568 }
Guido van Rossum44620641997-08-11 18:57:29 +0000569 rl_event_hook = PyOS_InputHook;
Guido van Rossum0969d361997-08-05 21:27:50 +0000570 p = readline(prompt);
Guido van Rossum174efc92000-09-16 16:37:53 +0000571 PyOS_setsig(SIGINT, old_inthandler);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000572
573 /* We must return a buffer allocated with PyMem_Malloc. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000574 if (p == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000575 p = PyMem_Malloc(1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000576 if (p != NULL)
577 *p = '\0';
578 return p;
579 }
580 n = strlen(p);
581 if (n > 0)
582 add_history(p);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000583 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
584 release the original. */
585 q = p;
586 p = PyMem_Malloc(n+2);
587 if (p != NULL) {
588 strncpy(p, q, n);
Guido van Rossum0969d361997-08-05 21:27:50 +0000589 p[n] = '\n';
590 p[n+1] = '\0';
591 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000592 free(q);
Guido van Rossum0969d361997-08-05 21:27:50 +0000593 return p;
594}
595
Guido van Rossum290900a1997-09-26 21:51:21 +0000596
597/* Initialize the module */
598
599static char doc_module[] =
600"Importing this module enables command line editing using GNU readline.";
601
Guido van Rossum3886bb61998-12-04 18:50:17 +0000602DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000603initreadline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000604{
Guido van Rossum1ea64ea2000-10-02 15:53:08 +0000605 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +0000606
607 m = Py_InitModule4("readline", readline_methods, doc_module,
608 (PyObject *)NULL, PYTHON_API_VERSION);
609 if (isatty(fileno(stdin))) {
610 PyOS_ReadlineFunctionPointer = call_readline;
611 setup_readline();
612 }
Guido van Rossum0969d361997-08-05 21:27:50 +0000613}