blob: 9b4d9522b0ea02357a8bbf4e7919012a96cd3392 [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 Rossum290900a1997-09-26 21:51:21 +000015/* GNU readline definitions */
Guido van Rossumb0e51b22001-04-13 18:14:27 +000016#undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
Guido van Rossumbcc20741998-08-04 22:53:56 +000017#include <readline/readline.h>
18#include <readline/history.h>
Guido van Rossum730806d1998-04-10 22:27:42 +000019
Guido van Rossum353ae582001-07-10 16:45:32 +000020#ifdef HAVE_RL_COMPLETION_MATCHES
21#define completion_matches(x, y) rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
22#endif
23
Guido van Rossum290900a1997-09-26 21:51:21 +000024/* Pointers needed from outside (but not declared in a header file). */
Guido van Rossum5a530192001-01-10 21:03:32 +000025extern DL_IMPORT(int) (*PyOS_InputHook)(void);
26extern DL_IMPORT(char) *(*PyOS_ReadlineFunctionPointer)(char *);
Guido van Rossum0969d361997-08-05 21:27:50 +000027
Guido van Rossum0969d361997-08-05 21:27:50 +000028
Guido van Rossum290900a1997-09-26 21:51:21 +000029/* Exported function to send one line to readline's init file parser */
30
31static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000032parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000033{
Guido van Rossum3b5330e1998-12-04 15:34:39 +000034 char *s, *copy;
Guido van Rossum43713e52000-02-29 13:59:29 +000035 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000036 return NULL;
Guido van Rossum3b5330e1998-12-04 15:34:39 +000037 /* Make a copy -- rl_parse_and_bind() modifies its argument */
38 /* Bernard Herzog */
39 copy = malloc(1 + strlen(s));
40 if (copy == NULL)
41 return PyErr_NoMemory();
42 strcpy(copy, s);
43 rl_parse_and_bind(copy);
44 free(copy); /* Free the copy */
Guido van Rossum290900a1997-09-26 21:51:21 +000045 Py_INCREF(Py_None);
46 return Py_None;
47}
48
49static char doc_parse_and_bind[] = "\
50parse_and_bind(string) -> None\n\
51Parse and execute single line of a readline init file.\
52";
53
54
55/* Exported function to parse a readline init file */
56
57static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000058read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000059{
60 char *s = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +000061 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000062 return NULL;
63 errno = rl_read_init_file(s);
64 if (errno)
65 return PyErr_SetFromErrno(PyExc_IOError);
66 Py_INCREF(Py_None);
67 return Py_None;
68}
69
70static char doc_read_init_file[] = "\
71read_init_file([filename]) -> None\n\
72Parse a readline initialization file.\n\
73The default filename is the last filename used.\
74";
75
76
Skip Montanaro28067822000-07-06 18:55:12 +000077/* Exported function to load a readline history file */
78
79static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000080read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +000081{
82 char *s = NULL;
83 if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
84 return NULL;
85 errno = read_history(s);
86 if (errno)
87 return PyErr_SetFromErrno(PyExc_IOError);
88 Py_INCREF(Py_None);
89 return Py_None;
90}
91
Skip Montanaro49bd24d2000-07-19 16:54:53 +000092static int history_length = -1; /* do not truncate history by default */
Skip Montanaro28067822000-07-06 18:55:12 +000093static char doc_read_history_file[] = "\
94read_history_file([filename]) -> None\n\
95Load a readline history file.\n\
96The default filename is ~/.history.\
97";
98
99
100/* Exported function to save a readline history file */
101
102static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000103write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000104{
105 char *s = NULL;
106 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
107 return NULL;
108 errno = write_history(s);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000109 if (!errno && history_length >= 0)
110 history_truncate_file(s, history_length);
Skip Montanaro28067822000-07-06 18:55:12 +0000111 if (errno)
112 return PyErr_SetFromErrno(PyExc_IOError);
113 Py_INCREF(Py_None);
114 return Py_None;
115}
116
117static char doc_write_history_file[] = "\
118write_history_file([filename]) -> None\n\
119Save a readline history file.\n\
120The default filename is ~/.history.\
121";
122
123
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000124static char set_history_length_doc[] = "\
125set_history_length(length) -> None\n\
126set the maximal number of items which will be written to\n\
127the history file. A negative length is used to inhibit\n\
128history truncation.\n\
129";
130
131static PyObject*
132set_history_length(PyObject *self, PyObject *args)
133{
134 int length = history_length;
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000135 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
136 return NULL;
137 history_length = length;
138 Py_INCREF(Py_None);
139 return Py_None;
140}
141
142
143
144static char get_history_length_doc[] = "\
145get_history_length() -> int\n\
146return the current history length value.\n\
147";
148
149static PyObject*
150get_history_length(PyObject *self, PyObject *args)
151{
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000152 if (!PyArg_ParseTuple(args, ":get_history_length"))
153 return NULL;
154 return Py_BuildValue("i", history_length);
155}
156
Martin v. Löwis0daad592001-09-30 21:09:59 +0000157/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000158
Martin v. Löwis0daad592001-09-30 21:09:59 +0000159static PyObject *
160set_hook(const char * funcname, PyObject **hook_var, PyThreadState **tstate, PyObject *args)
161{
162 PyObject *function = Py_None;
163 char buf[80];
Tim Peters885d4572001-11-28 20:27:42 +0000164 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000165 if (!PyArg_ParseTuple(args, buf, &function))
166 return NULL;
167 if (function == Py_None) {
168 Py_XDECREF(*hook_var);
169 *hook_var = NULL;
170 *tstate = NULL;
171 }
172 else if (PyCallable_Check(function)) {
173 PyObject *tmp = *hook_var;
174 Py_INCREF(function);
175 *hook_var = function;
176 Py_XDECREF(tmp);
177 *tstate = PyThreadState_Get();
178 }
179 else {
Tim Peters885d4572001-11-28 20:27:42 +0000180 PyOS_snprintf(buf, sizeof(buf),
181 "set_%.50s(func): argument not callable",
182 funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000183 PyErr_SetString(PyExc_TypeError, buf);
184 return NULL;
185 }
186 Py_INCREF(Py_None);
187 return Py_None;
188}
189
190/* Exported functions to specify hook functions in Python */
191
192static PyObject *startup_hook = NULL;
193static PyThreadState *startup_hook_tstate = NULL;
194
195#ifdef HAVE_RL_PRE_INPUT_HOOK
196static PyObject *pre_input_hook = NULL;
197static PyThreadState *pre_input_hook_tstate = NULL;
198#endif
199
200static PyObject *
201set_startup_hook(PyObject *self, PyObject *args)
202{
203 return set_hook("startup_hook", &startup_hook, &startup_hook_tstate, args);
204}
205
206static char doc_set_startup_hook[] = "\
207set_startup_hook([function]) -> None\n\
208Set or remove the startup_hook function.\n\
209The function is called with no arguments just\n\
210before readline prints the first prompt.\n\
211";
212
213#ifdef HAVE_RL_PRE_INPUT_HOOK
214static PyObject *
215set_pre_input_hook(PyObject *self, PyObject *args)
216{
217 return set_hook("pre_input_hook", &pre_input_hook, &pre_input_hook_tstate, args);
218}
219
220static char doc_set_pre_input_hook[] = "\
221set_pre_input_hook([function]) -> None\n\
222Set or remove the pre_input_hook function.\n\
223The function is called with no arguments after the first prompt\n\
224has been printed and just before readline starts reading input\n\
225characters.\n\
226";
227#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000228
Guido van Rossum290900a1997-09-26 21:51:21 +0000229/* Exported function to specify a word completer in Python */
230
231static PyObject *completer = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000232static PyThreadState *completer_tstate = NULL;
Guido van Rossum290900a1997-09-26 21:51:21 +0000233
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000234static PyObject *begidx = NULL;
235static PyObject *endidx = NULL;
236
237/* get the beginning index for the scope of the tab-completion */
238static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000239get_begidx(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000240{
241 if(!PyArg_NoArgs(args)) {
242 return NULL;
243 }
244 Py_INCREF(begidx);
245 return begidx;
246}
247
248static char doc_get_begidx[] = "\
249get_begidx() -> int\n\
250get the beginning index of the readline tab-completion scope";
251
252/* get the ending index for the scope of the tab-completion */
253static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000254get_endidx(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000255{
256 if(!PyArg_NoArgs(args)) {
257 return NULL;
258 }
259 Py_INCREF(endidx);
260 return endidx;
261}
262
263static char doc_get_endidx[] = "\
264get_endidx() -> int\n\
265get the ending index of the readline tab-completion scope";
266
267
268/* set the tab-completion word-delimiters that readline uses */
269
270static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000271set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000272{
273 char *break_chars;
274
Guido van Rossum43713e52000-02-29 13:59:29 +0000275 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000276 return NULL;
277 }
278 free(rl_completer_word_break_characters);
279 rl_completer_word_break_characters = strdup(break_chars);
280 Py_INCREF(Py_None);
281 return Py_None;
282}
283
284static char doc_set_completer_delims[] = "\
285set_completer_delims(string) -> None\n\
286set the readline word delimiters for tab-completion";
287
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000288static PyObject *
289py_add_history(PyObject *self, PyObject *args)
290{
291 char *line;
292
293 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
294 return NULL;
295 }
296 add_history(line);
297 Py_INCREF(Py_None);
298 return Py_None;
299}
300
301static char doc_add_history[] = "\
302add_history(string) -> None\n\
303add a line to the history buffer";
304
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000305
306/* get the tab-completion word-delimiters that readline uses */
307
308static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000309get_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000310{
311 if(!PyArg_NoArgs(args)) {
312 return NULL;
313 }
314 return PyString_FromString(rl_completer_word_break_characters);
315}
316
317static char doc_get_completer_delims[] = "\
318get_completer_delims() -> string\n\
319get the readline word delimiters for tab-completion";
320
Guido van Rossum290900a1997-09-26 21:51:21 +0000321static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000322set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000323{
Martin v. Löwis0daad592001-09-30 21:09:59 +0000324 return set_hook("completer", &completer, &completer_tstate, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000325}
326
327static char doc_set_completer[] = "\
328set_completer([function]) -> None\n\
329Set or remove the completer function.\n\
330The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000331for state in 0, 1, 2, ..., until it returns a non-string.\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000332It should return the next possible completion starting with 'text'.\
333";
334
Guido van Rossum79378ff1997-10-07 14:53:21 +0000335/* Exported function to read the current line buffer */
336
337static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000338get_line_buffer(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000339{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000340 if (!PyArg_NoArgs(args))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000341 return NULL;
342 return PyString_FromString(rl_line_buffer);
343}
344
345static char doc_get_line_buffer[] = "\
Guido van Rossuma88c5f31998-05-20 15:50:56 +0000346get_line_buffer() -> string\n\
Guido van Rossum79378ff1997-10-07 14:53:21 +0000347return the current contents of the line buffer.\
348";
349
350/* Exported function to insert text into the line buffer */
351
352static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000353insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000354{
355 char *s;
Guido van Rossum43713e52000-02-29 13:59:29 +0000356 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000357 return NULL;
358 rl_insert_text(s);
359 Py_INCREF(Py_None);
360 return Py_None;
361}
362
363
364static char doc_insert_text[] = "\
365insert_text(string) -> None\n\
366Insert text into the command line.\
367";
368
Guido van Rossum290900a1997-09-26 21:51:21 +0000369
370/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000371
372static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000373{
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000374 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000375 {"get_line_buffer", get_line_buffer,
376 METH_OLDARGS, doc_get_line_buffer},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000377 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
378 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
379 {"read_history_file", read_history_file,
380 METH_VARARGS, doc_read_history_file},
381 {"write_history_file", write_history_file,
382 METH_VARARGS, doc_write_history_file},
383 {"set_history_length", set_history_length,
384 METH_VARARGS, set_history_length_doc},
385 {"get_history_length", get_history_length,
386 METH_VARARGS, get_history_length_doc},
387 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000388 {"get_begidx", get_begidx, METH_OLDARGS, doc_get_begidx},
389 {"get_endidx", get_endidx, METH_OLDARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000390
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000391 {"set_completer_delims", set_completer_delims,
392 METH_VARARGS, doc_set_completer_delims},
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000393 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000394 {"get_completer_delims", get_completer_delims,
395 METH_OLDARGS, doc_get_completer_delims},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000396
397 {"set_startup_hook", set_startup_hook, METH_VARARGS, doc_set_startup_hook},
398#ifdef HAVE_RL_PRE_INPUT_HOOK
399 {"set_pre_input_hook", set_pre_input_hook, METH_VARARGS, doc_set_pre_input_hook},
400#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000401 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000402};
403
Martin v. Löwis0daad592001-09-30 21:09:59 +0000404/* C function to call the Python hooks. */
405
406static int
407on_hook(PyObject *func, PyThreadState *tstate)
408{
409 int result = 0;
410 if (func != NULL) {
411 PyObject *r;
412 PyThreadState *save_tstate;
413 /* Note that readline is called with the interpreter
414 lock released! */
415 save_tstate = PyThreadState_Swap(NULL);
416 PyEval_RestoreThread(tstate);
417 r = PyObject_CallFunction(func, NULL);
418 if (r == NULL)
419 goto error;
420 if (r == Py_None)
421 result = 0;
422 else
423 result = PyInt_AsLong(r);
424 Py_DECREF(r);
425 goto done;
426 error:
427 PyErr_Clear();
428 Py_XDECREF(r);
429 done:
430 PyEval_SaveThread();
431 PyThreadState_Swap(save_tstate);
432 }
433 return result;
434}
435
436static int
437on_startup_hook(void)
438{
439 return on_hook(startup_hook, startup_hook_tstate);
440}
441
442#ifdef HAVE_RL_PRE_INPUT_HOOK
443static int
444on_pre_input_hook(void)
445{
446 return on_hook(pre_input_hook, pre_input_hook_tstate);
447}
448#endif
449
Guido van Rossum290900a1997-09-26 21:51:21 +0000450/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000451
Guido van Rossum290900a1997-09-26 21:51:21 +0000452static char *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000453on_completion(char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000454{
Guido van Rossum290900a1997-09-26 21:51:21 +0000455 char *result = NULL;
456 if (completer != NULL) {
457 PyObject *r;
Guido van Rossuma59406a1997-10-10 17:39:19 +0000458 PyThreadState *save_tstate;
Guido van Rossum290900a1997-09-26 21:51:21 +0000459 /* Note that readline is called with the interpreter
460 lock released! */
Guido van Rossuma59406a1997-10-10 17:39:19 +0000461 save_tstate = PyThreadState_Swap(NULL);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000462 PyEval_RestoreThread(completer_tstate);
Michael W. Hudson0c1ceaf2002-02-13 11:58:25 +0000463 /* Don't use the default filename completion if we
464 * have a custom completion function... */
465 rl_attempted_completion_over = 1;
Guido van Rossum290900a1997-09-26 21:51:21 +0000466 r = PyObject_CallFunction(completer, "si", text, state);
467 if (r == NULL)
468 goto error;
469 if (r == Py_None) {
470 result = NULL;
471 }
472 else {
473 char *s = PyString_AsString(r);
474 if (s == NULL)
475 goto error;
476 result = strdup(s);
477 }
478 Py_DECREF(r);
479 goto done;
480 error:
481 PyErr_Clear();
482 Py_XDECREF(r);
483 done:
484 PyEval_SaveThread();
Guido van Rossuma59406a1997-10-10 17:39:19 +0000485 PyThreadState_Swap(save_tstate);
Guido van Rossum290900a1997-09-26 21:51:21 +0000486 }
487 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000488}
489
Guido van Rossum290900a1997-09-26 21:51:21 +0000490
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000491/* a more flexible constructor that saves the "begidx" and "endidx"
492 * before calling the normal completer */
493
494char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000495flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000496{
497 Py_XDECREF(begidx);
498 Py_XDECREF(endidx);
499 begidx = PyInt_FromLong((long) start);
500 endidx = PyInt_FromLong((long) end);
501 return completion_matches(text, *on_completion);
502}
503
Guido van Rossum290900a1997-09-26 21:51:21 +0000504/* Helper to initialize GNU readline properly. */
505
506static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000507setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000508{
509 rl_readline_name = "python";
510 /* Force rebind of TAB to insert-tab */
511 rl_bind_key('\t', rl_insert);
512 /* Bind both ESC-TAB and ESC-ESC to the completion function */
513 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
514 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000515 /* Set our hook functions */
516 rl_startup_hook = (Function *)on_startup_hook;
517#ifdef HAVE_RL_PRE_INPUT_HOOK
518 rl_pre_input_hook = (Function *)on_pre_input_hook;
519#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000520 /* Set our completion function */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000521 rl_attempted_completion_function = (CPPFunction *)flex_complete;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000522 /* Set Python word break characters */
523 rl_completer_word_break_characters =
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000524 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000525 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000526
527 begidx = PyInt_FromLong(0L);
528 endidx = PyInt_FromLong(0L);
Barry Warsawf7612871999-01-29 21:55:03 +0000529 /* Initialize (allows .inputrc to override)
530 *
531 * XXX: A bug in the readline-2.2 library causes a memory leak
532 * inside this function. Nothing we can do about it.
533 */
Guido van Rossum290900a1997-09-26 21:51:21 +0000534 rl_initialize();
535}
536
537
538/* Interrupt handler */
539
540static jmp_buf jbuf;
541
Guido van Rossum0969d361997-08-05 21:27:50 +0000542/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +0000543static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000544onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000545{
Guido van Rossum290900a1997-09-26 21:51:21 +0000546 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000547}
548
Guido van Rossum290900a1997-09-26 21:51:21 +0000549
550/* Wrapper around GNU readline that handles signals differently. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000551
552static char *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000553call_readline(char *prompt)
Guido van Rossum0969d361997-08-05 21:27:50 +0000554{
Guido van Rossum26418a92000-06-28 21:30:31 +0000555 size_t n;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000556 char *p, *q;
Guido van Rossum174efc92000-09-16 16:37:53 +0000557 PyOS_sighandler_t old_inthandler;
558
559 old_inthandler = PyOS_setsig(SIGINT, onintr);
Guido van Rossum0969d361997-08-05 21:27:50 +0000560 if (setjmp(jbuf)) {
561#ifdef HAVE_SIGRELSE
562 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
563 sigrelse(SIGINT);
564#endif
Guido van Rossum174efc92000-09-16 16:37:53 +0000565 PyOS_setsig(SIGINT, old_inthandler);
Guido van Rossum0969d361997-08-05 21:27:50 +0000566 return NULL;
567 }
Guido van Rossum44620641997-08-11 18:57:29 +0000568 rl_event_hook = PyOS_InputHook;
Guido van Rossum0969d361997-08-05 21:27:50 +0000569 p = readline(prompt);
Guido van Rossum174efc92000-09-16 16:37:53 +0000570 PyOS_setsig(SIGINT, old_inthandler);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000571
572 /* We must return a buffer allocated with PyMem_Malloc. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000573 if (p == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000574 p = PyMem_Malloc(1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000575 if (p != NULL)
576 *p = '\0';
577 return p;
578 }
579 n = strlen(p);
580 if (n > 0)
581 add_history(p);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000582 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
583 release the original. */
584 q = p;
585 p = PyMem_Malloc(n+2);
586 if (p != NULL) {
587 strncpy(p, q, n);
Guido van Rossum0969d361997-08-05 21:27:50 +0000588 p[n] = '\n';
589 p[n+1] = '\0';
590 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000591 free(q);
Guido van Rossum0969d361997-08-05 21:27:50 +0000592 return p;
593}
594
Guido van Rossum290900a1997-09-26 21:51:21 +0000595
596/* Initialize the module */
597
598static char doc_module[] =
599"Importing this module enables command line editing using GNU readline.";
600
Guido van Rossum3886bb61998-12-04 18:50:17 +0000601DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000602initreadline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000603{
Guido van Rossum1ea64ea2000-10-02 15:53:08 +0000604 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +0000605
606 m = Py_InitModule4("readline", readline_methods, doc_module,
607 (PyObject *)NULL, PYTHON_API_VERSION);
608 if (isatty(fileno(stdin))) {
609 PyOS_ReadlineFunctionPointer = call_readline;
610 setup_readline();
611 }
Guido van Rossum0969d361997-08-05 21:27:50 +0000612}