blob: a1ba13d5ef72587a2e167287246ff587b491ba17 [file] [log] [blame]
Guido van Rossum0969d361997-08-05 21:27:50 +00001/* The readline module makes GNU readline available to Python. It
2 * has ideas contributed by Lee Busby, LLNL, and William Magro,
3 * Cornell Theory Center.
4 */
5
6#ifdef __cplusplus
7extern "C" {
8#endif
9
10#include "Python.h"
11#include <setjmp.h>
12#include <signal.h>
13
14/* Routines needed from outside (but not declared in a header file). */
Guido van Rossum44620641997-08-11 18:57:29 +000015extern int (*PyOS_InputHook)();
Guido van Rossum0969d361997-08-05 21:27:50 +000016extern char *readline();
17extern int rl_initialize();
18extern int rl_insert();
19extern int rl_bind_key();
20extern void add_history();
21extern char *rl_readline_name;
22extern int (*rl_event_hook)();
23extern char *(*PyOS_ReadlineFunctionPointer) Py_PROTO((char *));
24
25/* This module's initialization routine */
26void initreadline (void);
27
28static void PyOS_ReadlineInit();
29static RETSIGTYPE onintr();
30static char *PyOS_GnuReadline();
31static jmp_buf jbuf;
32static PyObject *ReadlineError;
33static int already_initialized = 0;
34static char readline_module_documentation[] =
35"Readline Module, version0.0"
36;
37
38static struct PyMethodDef readline_methods[] =
39{
40 { 0, 0 }
41};
42
43/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
44
45/* Initialize the module. Actually, that's all you can or need to do. */
46void initreadline (void)
47{
48 PyObject *m, *d;
49
50 if (already_initialized)
51 return;
52 m = Py_InitModule4 ("readline", readline_methods,
53 readline_module_documentation,
54 (PyObject *) 0, PYTHON_API_VERSION);
55 d = PyModule_GetDict (m);
56 ReadlineError = PyString_FromString ("Readline.error");
57 PyDict_SetItemString (d, "error", ReadlineError);
58 if (PyErr_Occurred ()) {
59 Py_FatalError ("Cannot initialize module readline");
60 }
61 if (isatty(fileno(stdin))){
62 PyOS_ReadlineFunctionPointer = PyOS_GnuReadline;
63 PyOS_ReadlineInit();
64 }
65 already_initialized = 1;
66}
67
68/* ARGSUSED */
69static RETSIGTYPE
70onintr(sig)
71 int sig;
72{
73 longjmp(jbuf, 1);
74}
75
76static void
77PyOS_ReadlineInit()
78{
79 /* Force rebind of TAB to insert-tab */
80 rl_readline_name = "python";
81 rl_initialize();
82 rl_bind_key('\t', rl_insert);
83}
84
85static char *
86PyOS_GnuReadline(prompt)
87 char *prompt;
88{
89 int n;
90 char *p;
91 RETSIGTYPE (*old_inthandler)();
92 old_inthandler = signal(SIGINT, onintr);
93 if (setjmp(jbuf)) {
94#ifdef HAVE_SIGRELSE
95 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
96 sigrelse(SIGINT);
97#endif
98 signal(SIGINT, old_inthandler);
99 return NULL;
100 }
Guido van Rossum44620641997-08-11 18:57:29 +0000101 rl_event_hook = PyOS_InputHook;
Guido van Rossum0969d361997-08-05 21:27:50 +0000102 p = readline(prompt);
103 signal(SIGINT, old_inthandler);
104 if (p == NULL) {
105 p = malloc(1);
106 if (p != NULL)
107 *p = '\0';
108 return p;
109 }
110 n = strlen(p);
111 if (n > 0)
112 add_history(p);
113 if ((p = realloc(p, n+2)) != NULL) {
114 p[n] = '\n';
115 p[n+1] = '\0';
116 }
117 return p;
118}
119
120#ifdef __cplusplus
121}
122#endif