Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 1 | /* 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 |
| 7 | extern "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 Rossum | 4462064 | 1997-08-11 18:57:29 +0000 | [diff] [blame] | 15 | extern int (*PyOS_InputHook)(); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 16 | extern char *readline(); |
| 17 | extern int rl_initialize(); |
| 18 | extern int rl_insert(); |
| 19 | extern int rl_bind_key(); |
| 20 | extern void add_history(); |
| 21 | extern char *rl_readline_name; |
| 22 | extern int (*rl_event_hook)(); |
| 23 | extern char *(*PyOS_ReadlineFunctionPointer) Py_PROTO((char *)); |
| 24 | |
| 25 | /* This module's initialization routine */ |
| 26 | void initreadline (void); |
| 27 | |
| 28 | static void PyOS_ReadlineInit(); |
| 29 | static RETSIGTYPE onintr(); |
| 30 | static char *PyOS_GnuReadline(); |
| 31 | static jmp_buf jbuf; |
| 32 | static PyObject *ReadlineError; |
| 33 | static int already_initialized = 0; |
| 34 | static char readline_module_documentation[] = |
| 35 | "Readline Module, version0.0" |
| 36 | ; |
| 37 | |
| 38 | static 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. */ |
| 46 | void 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 */ |
| 69 | static RETSIGTYPE |
| 70 | onintr(sig) |
| 71 | int sig; |
| 72 | { |
| 73 | longjmp(jbuf, 1); |
| 74 | } |
| 75 | |
| 76 | static void |
| 77 | PyOS_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 | |
| 85 | static char * |
| 86 | PyOS_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 Rossum | 4462064 | 1997-08-11 18:57:29 +0000 | [diff] [blame] | 101 | rl_event_hook = PyOS_InputHook; |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 102 | 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 |