source: vendor/python/2.5/Modules/readline.c

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

File size: 22.1 KB
Line 
1/* 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. More
4 * recently, it was largely rewritten by Guido van Rossum.
5 */
6
7/* Standard definitions */
8#include "Python.h"
9#include <setjmp.h>
10#include <signal.h>
11#include <errno.h>
12#include <sys/time.h>
13
14#if defined(HAVE_SETLOCALE)
15/* 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
23#ifdef SAVE_LOCALE
24# define RESTORE_LOCALE(sl) { setlocale(LC_CTYPE, sl); free(sl); }
25#else
26# define RESTORE_LOCALE(sl)
27#endif
28
29/* GNU readline definitions */
30#undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
31#include <readline/readline.h>
32#include <readline/history.h>
33
34#ifdef HAVE_RL_COMPLETION_MATCHES
35#define completion_matches(x, y) \
36 rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
37#endif
38
39
40/* Exported function to send one line to readline's init file parser */
41
42static PyObject *
43parse_and_bind(PyObject *self, PyObject *args)
44{
45 char *s, *copy;
46 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
47 return NULL;
48 /* Make a copy -- rl_parse_and_bind() modifies its argument */
49 /* Bernard Herzog */
50 copy = malloc(1 + strlen(s));
51 if (copy == NULL)
52 return PyErr_NoMemory();
53 strcpy(copy, s);
54 rl_parse_and_bind(copy);
55 free(copy); /* Free the copy */
56 Py_INCREF(Py_None);
57 return Py_None;
58}
59
60PyDoc_STRVAR(doc_parse_and_bind,
61"parse_and_bind(string) -> None\n\
62Parse and execute single line of a readline init file.");
63
64
65/* Exported function to parse a readline init file */
66
67static PyObject *
68read_init_file(PyObject *self, PyObject *args)
69{
70 char *s = NULL;
71 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
72 return NULL;
73 errno = rl_read_init_file(s);
74 if (errno)
75 return PyErr_SetFromErrno(PyExc_IOError);
76 Py_INCREF(Py_None);
77 return Py_None;
78}
79
80PyDoc_STRVAR(doc_read_init_file,
81"read_init_file([filename]) -> None\n\
82Parse a readline initialization file.\n\
83The default filename is the last filename used.");
84
85
86/* Exported function to load a readline history file */
87
88static PyObject *
89read_history_file(PyObject *self, PyObject *args)
90{
91 char *s = NULL;
92 if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
93 return NULL;
94 errno = read_history(s);
95 if (errno)
96 return PyErr_SetFromErrno(PyExc_IOError);
97 Py_INCREF(Py_None);
98 return Py_None;
99}
100
101static int _history_length = -1; /* do not truncate history by default */
102PyDoc_STRVAR(doc_read_history_file,
103"read_history_file([filename]) -> None\n\
104Load a readline history file.\n\
105The default filename is ~/.history.");
106
107
108/* Exported function to save a readline history file */
109
110static PyObject *
111write_history_file(PyObject *self, PyObject *args)
112{
113 char *s = NULL;
114 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
115 return NULL;
116 errno = write_history(s);
117 if (!errno && _history_length >= 0)
118 history_truncate_file(s, _history_length);
119 if (errno)
120 return PyErr_SetFromErrno(PyExc_IOError);
121 Py_INCREF(Py_None);
122 return Py_None;
123}
124
125PyDoc_STRVAR(doc_write_history_file,
126"write_history_file([filename]) -> None\n\
127Save a readline history file.\n\
128The default filename is ~/.history.");
129
130
131/* Set history length */
132
133static PyObject*
134set_history_length(PyObject *self, PyObject *args)
135{
136 int length = _history_length;
137 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
138 return NULL;
139 _history_length = length;
140 Py_INCREF(Py_None);
141 return Py_None;
142}
143
144PyDoc_STRVAR(set_history_length_doc,
145"set_history_length(length) -> None\n\
146set the maximal number of items which will be written to\n\
147the history file. A negative length is used to inhibit\n\
148history truncation.");
149
150
151/* Get history length */
152
153static PyObject*
154get_history_length(PyObject *self, PyObject *noarg)
155{
156 return PyInt_FromLong(_history_length);
157}
158
159PyDoc_STRVAR(get_history_length_doc,
160"get_history_length() -> int\n\
161return the maximum number of items that will be written to\n\
162the history file.");
163
164
165/* Generic hook function setter */
166
167static PyObject *
168set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
169{
170 PyObject *function = Py_None;
171 char buf[80];
172 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
173 if (!PyArg_ParseTuple(args, buf, &function))
174 return NULL;
175 if (function == Py_None) {
176 Py_XDECREF(*hook_var);
177 *hook_var = NULL;
178 }
179 else if (PyCallable_Check(function)) {
180 PyObject *tmp = *hook_var;
181 Py_INCREF(function);
182 *hook_var = function;
183 Py_XDECREF(tmp);
184 }
185 else {
186 PyOS_snprintf(buf, sizeof(buf),
187 "set_%.50s(func): argument not callable",
188 funcname);
189 PyErr_SetString(PyExc_TypeError, buf);
190 return NULL;
191 }
192 Py_INCREF(Py_None);
193 return Py_None;
194}
195
196
197/* Exported functions to specify hook functions in Python */
198
199static PyObject *startup_hook = NULL;
200
201#ifdef HAVE_RL_PRE_INPUT_HOOK
202static PyObject *pre_input_hook = NULL;
203#endif
204
205static PyObject *
206set_startup_hook(PyObject *self, PyObject *args)
207{
208 return set_hook("startup_hook", &startup_hook, args);
209}
210
211PyDoc_STRVAR(doc_set_startup_hook,
212"set_startup_hook([function]) -> None\n\
213Set or remove the startup_hook function.\n\
214The function is called with no arguments just\n\
215before readline prints the first prompt.");
216
217
218#ifdef HAVE_RL_PRE_INPUT_HOOK
219
220/* Set pre-input hook */
221
222static PyObject *
223set_pre_input_hook(PyObject *self, PyObject *args)
224{
225 return set_hook("pre_input_hook", &pre_input_hook, args);
226}
227
228PyDoc_STRVAR(doc_set_pre_input_hook,
229"set_pre_input_hook([function]) -> None\n\
230Set or remove the pre_input_hook function.\n\
231The function is called with no arguments after the first prompt\n\
232has been printed and just before readline starts reading input\n\
233characters.");
234
235#endif
236
237
238/* Exported function to specify a word completer in Python */
239
240static PyObject *completer = NULL;
241
242static PyObject *begidx = NULL;
243static PyObject *endidx = NULL;
244
245
246/* Get the beginning index for the scope of the tab-completion */
247
248static PyObject *
249get_begidx(PyObject *self, PyObject *noarg)
250{
251 Py_INCREF(begidx);
252 return begidx;
253}
254
255PyDoc_STRVAR(doc_get_begidx,
256"get_begidx() -> int\n\
257get the beginning index of the readline tab-completion scope");
258
259
260/* Get the ending index for the scope of the tab-completion */
261
262static PyObject *
263get_endidx(PyObject *self, PyObject *noarg)
264{
265 Py_INCREF(endidx);
266 return endidx;
267}
268
269PyDoc_STRVAR(doc_get_endidx,
270"get_endidx() -> int\n\
271get the ending index of the readline tab-completion scope");
272
273
274/* Set the tab-completion word-delimiters that readline uses */
275
276static PyObject *
277set_completer_delims(PyObject *self, PyObject *args)
278{
279 char *break_chars;
280
281 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
282 return NULL;
283 }
284 free((void*)rl_completer_word_break_characters);
285 rl_completer_word_break_characters = strdup(break_chars);
286 Py_INCREF(Py_None);
287 return Py_None;
288}
289
290PyDoc_STRVAR(doc_set_completer_delims,
291"set_completer_delims(string) -> None\n\
292set the readline word delimiters for tab-completion");
293
294static PyObject *
295py_remove_history(PyObject *self, PyObject *args)
296{
297 int entry_number;
298 HIST_ENTRY *entry;
299
300 if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
301 return NULL;
302 if (entry_number < 0) {
303 PyErr_SetString(PyExc_ValueError,
304 "History index cannot be negative");
305 return NULL;
306 }
307 entry = remove_history(entry_number);
308 if (!entry) {
309 PyErr_Format(PyExc_ValueError,
310 "No history item at position %d",
311 entry_number);
312 return NULL;
313 }
314 /* free memory allocated for the history entry */
315 if (entry->line)
316 free(entry->line);
317 if (entry->data)
318 free(entry->data);
319 free(entry);
320
321 Py_INCREF(Py_None);
322 return Py_None;
323}
324
325PyDoc_STRVAR(doc_remove_history,
326"remove_history_item(pos) -> None\n\
327remove history item given by its position");
328
329static PyObject *
330py_replace_history(PyObject *self, PyObject *args)
331{
332 int entry_number;
333 char *line;
334 HIST_ENTRY *old_entry;
335
336 if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number, &line)) {
337 return NULL;
338 }
339 if (entry_number < 0) {
340 PyErr_SetString(PyExc_ValueError,
341 "History index cannot be negative");
342 return NULL;
343 }
344 old_entry = replace_history_entry(entry_number, line, (void *)NULL);
345 if (!old_entry) {
346 PyErr_Format(PyExc_ValueError,
347 "No history item at position %d",
348 entry_number);
349 return NULL;
350 }
351 /* free memory allocated for the old history entry */
352 if (old_entry->line)
353 free(old_entry->line);
354 if (old_entry->data)
355 free(old_entry->data);
356 free(old_entry);
357
358 Py_INCREF(Py_None);
359 return Py_None;
360}
361
362PyDoc_STRVAR(doc_replace_history,
363"replace_history_item(pos, line) -> None\n\
364replaces history item given by its position with contents of line");
365
366/* Add a line to the history buffer */
367
368static PyObject *
369py_add_history(PyObject *self, PyObject *args)
370{
371 char *line;
372
373 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
374 return NULL;
375 }
376 add_history(line);
377 Py_INCREF(Py_None);
378 return Py_None;
379}
380
381PyDoc_STRVAR(doc_add_history,
382"add_history(string) -> None\n\
383add a line to the history buffer");
384
385
386/* Get the tab-completion word-delimiters that readline uses */
387
388static PyObject *
389get_completer_delims(PyObject *self, PyObject *noarg)
390{
391 return PyString_FromString(rl_completer_word_break_characters);
392}
393
394PyDoc_STRVAR(doc_get_completer_delims,
395"get_completer_delims() -> string\n\
396get the readline word delimiters for tab-completion");
397
398
399/* Set the completer function */
400
401static PyObject *
402set_completer(PyObject *self, PyObject *args)
403{
404 return set_hook("completer", &completer, args);
405}
406
407PyDoc_STRVAR(doc_set_completer,
408"set_completer([function]) -> None\n\
409Set or remove the completer function.\n\
410The function is called as function(text, state),\n\
411for state in 0, 1, 2, ..., until it returns a non-string.\n\
412It should return the next possible completion starting with 'text'.");
413
414
415static PyObject *
416get_completer(PyObject *self, PyObject *noargs)
417{
418 if (completer == NULL) {
419 Py_INCREF(Py_None);
420 return Py_None;
421 }
422 Py_INCREF(completer);
423 return completer;
424}
425
426PyDoc_STRVAR(doc_get_completer,
427"get_completer() -> function\n\
428\n\
429Returns current completer function.");
430
431/* Exported function to get any element of history */
432
433static PyObject *
434get_history_item(PyObject *self, PyObject *args)
435{
436 int idx = 0;
437 HIST_ENTRY *hist_ent;
438
439 if (!PyArg_ParseTuple(args, "i:index", &idx))
440 return NULL;
441 if ((hist_ent = history_get(idx)))
442 return PyString_FromString(hist_ent->line);
443 else {
444 Py_INCREF(Py_None);
445 return Py_None;
446 }
447}
448
449PyDoc_STRVAR(doc_get_history_item,
450"get_history_item() -> string\n\
451return the current contents of history item at index.");
452
453
454/* Exported function to get current length of history */
455
456static PyObject *
457get_current_history_length(PyObject *self, PyObject *noarg)
458{
459 HISTORY_STATE *hist_st;
460
461 hist_st = history_get_history_state();
462 return PyInt_FromLong(hist_st ? (long) hist_st->length : (long) 0);
463}
464
465PyDoc_STRVAR(doc_get_current_history_length,
466"get_current_history_length() -> integer\n\
467return the current (not the maximum) length of history.");
468
469
470/* Exported function to read the current line buffer */
471
472static PyObject *
473get_line_buffer(PyObject *self, PyObject *noarg)
474{
475 return PyString_FromString(rl_line_buffer);
476}
477
478PyDoc_STRVAR(doc_get_line_buffer,
479"get_line_buffer() -> string\n\
480return the current contents of the line buffer.");
481
482
483#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
484
485/* Exported function to clear the current history */
486
487static PyObject *
488py_clear_history(PyObject *self, PyObject *noarg)
489{
490 clear_history();
491 Py_INCREF(Py_None);
492 return Py_None;
493}
494
495PyDoc_STRVAR(doc_clear_history,
496"clear_history() -> None\n\
497Clear the current readline history.");
498#endif
499
500
501/* Exported function to insert text into the line buffer */
502
503static PyObject *
504insert_text(PyObject *self, PyObject *args)
505{
506 char *s;
507 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
508 return NULL;
509 rl_insert_text(s);
510 Py_INCREF(Py_None);
511 return Py_None;
512}
513
514PyDoc_STRVAR(doc_insert_text,
515"insert_text(string) -> None\n\
516Insert text into the command line.");
517
518
519/* Redisplay the line buffer */
520
521static PyObject *
522redisplay(PyObject *self, PyObject *noarg)
523{
524 rl_redisplay();
525 Py_INCREF(Py_None);
526 return Py_None;
527}
528
529PyDoc_STRVAR(doc_redisplay,
530"redisplay() -> None\n\
531Change what's displayed on the screen to reflect the current\n\
532contents of the line buffer.");
533
534
535/* Table of functions exported by the module */
536
537static struct PyMethodDef readline_methods[] =
538{
539 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
540 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
541 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
542 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
543 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
544 {"read_history_file", read_history_file,
545 METH_VARARGS, doc_read_history_file},
546 {"write_history_file", write_history_file,
547 METH_VARARGS, doc_write_history_file},
548 {"get_history_item", get_history_item,
549 METH_VARARGS, doc_get_history_item},
550 {"get_current_history_length", (PyCFunction)get_current_history_length,
551 METH_NOARGS, doc_get_current_history_length},
552 {"set_history_length", set_history_length,
553 METH_VARARGS, set_history_length_doc},
554 {"get_history_length", get_history_length,
555 METH_NOARGS, get_history_length_doc},
556 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
557 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
558 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
559 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
560
561 {"set_completer_delims", set_completer_delims,
562 METH_VARARGS, doc_set_completer_delims},
563 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
564 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
565 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
566 {"get_completer_delims", get_completer_delims,
567 METH_NOARGS, doc_get_completer_delims},
568
569 {"set_startup_hook", set_startup_hook,
570 METH_VARARGS, doc_set_startup_hook},
571#ifdef HAVE_RL_PRE_INPUT_HOOK
572 {"set_pre_input_hook", set_pre_input_hook,
573 METH_VARARGS, doc_set_pre_input_hook},
574#endif
575#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
576 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
577#endif
578 {0, 0}
579};
580
581
582/* C function to call the Python hooks. */
583
584static int
585on_hook(PyObject *func)
586{
587 int result = 0;
588 if (func != NULL) {
589 PyObject *r;
590#ifdef WITH_THREAD
591 PyGILState_STATE gilstate = PyGILState_Ensure();
592#endif
593 r = PyObject_CallFunction(func, NULL);
594 if (r == NULL)
595 goto error;
596 if (r == Py_None)
597 result = 0;
598 else {
599 result = PyInt_AsLong(r);
600 if (result == -1 && PyErr_Occurred())
601 goto error;
602 }
603 Py_DECREF(r);
604 goto done;
605 error:
606 PyErr_Clear();
607 Py_XDECREF(r);
608 done:
609#ifdef WITH_THREAD
610 PyGILState_Release(gilstate);
611#endif
612 return result;
613 }
614 return result;
615}
616
617static int
618on_startup_hook(void)
619{
620 return on_hook(startup_hook);
621}
622
623#ifdef HAVE_RL_PRE_INPUT_HOOK
624static int
625on_pre_input_hook(void)
626{
627 return on_hook(pre_input_hook);
628}
629#endif
630
631
632/* C function to call the Python completer. */
633
634static char *
635on_completion(char *text, int state)
636{
637 char *result = NULL;
638 if (completer != NULL) {
639 PyObject *r;
640#ifdef WITH_THREAD
641 PyGILState_STATE gilstate = PyGILState_Ensure();
642#endif
643 rl_attempted_completion_over = 1;
644 r = PyObject_CallFunction(completer, "si", text, state);
645 if (r == NULL)
646 goto error;
647 if (r == Py_None) {
648 result = NULL;
649 }
650 else {
651 char *s = PyString_AsString(r);
652 if (s == NULL)
653 goto error;
654 result = strdup(s);
655 }
656 Py_DECREF(r);
657 goto done;
658 error:
659 PyErr_Clear();
660 Py_XDECREF(r);
661 done:
662#ifdef WITH_THREAD
663 PyGILState_Release(gilstate);
664#endif
665 return result;
666 }
667 return result;
668}
669
670
671/* A more flexible constructor that saves the "begidx" and "endidx"
672 * before calling the normal completer */
673
674static char **
675flex_complete(char *text, int start, int end)
676{
677 Py_XDECREF(begidx);
678 Py_XDECREF(endidx);
679 begidx = PyInt_FromLong((long) start);
680 endidx = PyInt_FromLong((long) end);
681 return completion_matches(text, *on_completion);
682}
683
684
685/* Helper to initialize GNU readline properly. */
686
687static void
688setup_readline(void)
689{
690#ifdef SAVE_LOCALE
691 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
692 if (!saved_locale)
693 Py_FatalError("not enough memory to save locale");
694#endif
695
696 using_history();
697
698 rl_readline_name = "python";
699#if defined(PYOS_OS2) && defined(PYCC_GCC)
700 /* Allow $if term= in .inputrc to work */
701 rl_terminal_name = getenv("TERM");
702#endif
703 /* Force rebind of TAB to insert-tab */
704 rl_bind_key('\t', rl_insert);
705 /* Bind both ESC-TAB and ESC-ESC to the completion function */
706 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
707 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
708 /* Set our hook functions */
709 rl_startup_hook = (Function *)on_startup_hook;
710#ifdef HAVE_RL_PRE_INPUT_HOOK
711 rl_pre_input_hook = (Function *)on_pre_input_hook;
712#endif
713 /* Set our completion function */
714 rl_attempted_completion_function = (CPPFunction *)flex_complete;
715 /* Set Python word break characters */
716 rl_completer_word_break_characters =
717 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
718 /* All nonalphanums except '.' */
719#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
720 rl_completion_append_character ='\0';
721#endif
722
723 begidx = PyInt_FromLong(0L);
724 endidx = PyInt_FromLong(0L);
725 /* Initialize (allows .inputrc to override)
726 *
727 * XXX: A bug in the readline-2.2 library causes a memory leak
728 * inside this function. Nothing we can do about it.
729 */
730 rl_initialize();
731
732 RESTORE_LOCALE(saved_locale)
733}
734
735/* Wrapper around GNU readline that handles signals differently. */
736
737
738#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
739
740static char *completed_input_string;
741static void
742rlhandler(char *text)
743{
744 completed_input_string = text;
745 rl_callback_handler_remove();
746}
747
748extern PyThreadState* _PyOS_ReadlineTState;
749
750static char *
751readline_until_enter_or_signal(char *prompt, int *signal)
752{
753 char * not_done_reading = "";
754 fd_set selectset;
755
756 *signal = 0;
757#ifdef HAVE_RL_CATCH_SIGNAL
758 rl_catch_signals = 0;
759#endif
760
761 rl_callback_handler_install (prompt, rlhandler);
762 FD_ZERO(&selectset);
763
764 completed_input_string = not_done_reading;
765
766 while (completed_input_string == not_done_reading) {
767 int has_input = 0;
768
769 while (!has_input)
770 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
771 FD_SET(fileno(rl_instream), &selectset);
772 /* select resets selectset if no input was available */
773 has_input = select(fileno(rl_instream) + 1, &selectset,
774 NULL, NULL, &timeout);
775 if(PyOS_InputHook) PyOS_InputHook();
776 }
777
778 if(has_input > 0) {
779 rl_callback_read_char();
780 }
781 else if (errno == EINTR) {
782 int s;
783#ifdef WITH_THREAD
784 PyEval_RestoreThread(_PyOS_ReadlineTState);
785#endif
786 s = PyErr_CheckSignals();
787#ifdef WITH_THREAD
788 PyEval_SaveThread();
789#endif
790 if (s < 0) {
791 rl_free_line_state();
792 rl_cleanup_after_signal();
793 rl_callback_handler_remove();
794 *signal = 1;
795 completed_input_string = NULL;
796 }
797 }
798 }
799
800 return completed_input_string;
801}
802
803
804#else
805
806/* Interrupt handler */
807
808static jmp_buf jbuf;
809
810/* ARGSUSED */
811static void
812onintr(int sig)
813{
814 longjmp(jbuf, 1);
815}
816
817
818static char *
819readline_until_enter_or_signal(char *prompt, int *signal)
820{
821 PyOS_sighandler_t old_inthandler;
822 char *p;
823
824 *signal = 0;
825
826 old_inthandler = PyOS_setsig(SIGINT, onintr);
827 if (setjmp(jbuf)) {
828#ifdef HAVE_SIGRELSE
829 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
830 sigrelse(SIGINT);
831#endif
832 PyOS_setsig(SIGINT, old_inthandler);
833 *signal = 1;
834 return NULL;
835 }
836 rl_event_hook = PyOS_InputHook;
837 p = readline(prompt);
838 PyOS_setsig(SIGINT, old_inthandler);
839
840 return p;
841}
842#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
843
844
845static char *
846call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
847{
848 size_t n;
849 char *p, *q;
850 int signal;
851
852#ifdef SAVE_LOCALE
853 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
854 if (!saved_locale)
855 Py_FatalError("not enough memory to save locale");
856 setlocale(LC_CTYPE, "");
857#endif
858
859 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
860 rl_instream = sys_stdin;
861 rl_outstream = sys_stdout;
862#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
863 rl_prep_terminal (1);
864#endif
865 }
866
867 p = readline_until_enter_or_signal(prompt, &signal);
868
869 /* we got an interrupt signal */
870 if (signal) {
871 RESTORE_LOCALE(saved_locale)
872 return NULL;
873 }
874
875 /* We got an EOF, return a empty string. */
876 if (p == NULL) {
877 p = PyMem_Malloc(1);
878 if (p != NULL)
879 *p = '\0';
880 RESTORE_LOCALE(saved_locale)
881 return p;
882 }
883
884 /* we have a valid line */
885 n = strlen(p);
886 if (n > 0) {
887 char *line;
888 HISTORY_STATE *state = history_get_history_state();
889 if (state->length > 0)
890 line = history_get(state->length)->line;
891 else
892 line = "";
893 if (strcmp(p, line))
894 add_history(p);
895 /* the history docs don't say so, but the address of state
896 changes each time history_get_history_state is called
897 which makes me think it's freshly malloc'd memory...
898 on the other hand, the address of the last line stays the
899 same as long as history isn't extended, so it appears to
900 be malloc'd but managed by the history package... */
901 free(state);
902 }
903 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
904 release the original. */
905 q = p;
906 p = PyMem_Malloc(n+2);
907 if (p != NULL) {
908 strncpy(p, q, n);
909 p[n] = '\n';
910 p[n+1] = '\0';
911 }
912 free(q);
913 RESTORE_LOCALE(saved_locale)
914 return p;
915}
916
917
918/* Initialize the module */
919
920PyDoc_STRVAR(doc_module,
921"Importing this module enables command line editing using GNU readline.");
922
923PyMODINIT_FUNC
924initreadline(void)
925{
926 PyObject *m;
927
928 m = Py_InitModule4("readline", readline_methods, doc_module,
929 (PyObject *)NULL, PYTHON_API_VERSION);
930 if (m == NULL)
931 return;
932
933 PyOS_ReadlineFunctionPointer = call_readline;
934 setup_readline();
935}
Note: See TracBrowser for help on using the repository browser.