Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | b9f8d6e | 1995-01-04 19:08:09 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 32 | /* Readline interface for tokenizer.c and [raw_]input() in bltinmodule.c. |
| 33 | By default, or when stdin is not a tty device, we have a super |
| 34 | simple my_readline function using fgets. |
| 35 | Optionally, we can use the GNU readline library. |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 36 | my_readline() has a different return value from GNU readline(): |
| 37 | - NULL if an interrupt occurred or if an error occurred |
| 38 | - a malloc'ed empty string if EOF was read |
| 39 | - a malloc'ed string ending in \n normally |
| 40 | */ |
| 41 | |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 42 | #define Py_USE_NEW_NAMES 1 |
| 43 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 44 | #include "config.h" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 46 | #include <stdio.h> |
| 47 | #include <string.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 48 | #include <errno.h> |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 49 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 50 | #include "myproto.h" |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 51 | #include "mymalloc.h" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 52 | #include "intrcheck.h" |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 55 | #ifdef WITH_READLINE |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 56 | |
| 57 | extern char *readline(); |
Guido van Rossum | 0bc253d | 1996-09-13 04:09:26 +0000 | [diff] [blame] | 58 | extern int rl_initialize(); |
| 59 | extern int rl_insert(); |
Guido van Rossum | fd8a393 | 1996-12-02 18:27:33 +0000 | [diff] [blame] | 60 | extern int rl_bind_key(); |
| 61 | extern void add_history(); |
Guido van Rossum | 0bc253d | 1996-09-13 04:09:26 +0000 | [diff] [blame] | 62 | extern char *rl_readline_name; |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 63 | |
| 64 | #include <setjmp.h> |
| 65 | #include <signal.h> |
| 66 | |
| 67 | static jmp_buf jbuf; |
| 68 | |
| 69 | /* ARGSUSED */ |
| 70 | static RETSIGTYPE |
| 71 | onintr(sig) |
| 72 | int sig; |
| 73 | { |
| 74 | longjmp(jbuf, 1); |
| 75 | } |
| 76 | |
Guido van Rossum | adf8769 | 1996-04-09 02:45:31 +0000 | [diff] [blame] | 77 | void |
| 78 | PyOS_ReadlineInit() |
| 79 | { |
| 80 | static int been_here; |
| 81 | if (!been_here) { |
| 82 | /* Force rebind of TAB to insert-tab */ |
Guido van Rossum | 0bc253d | 1996-09-13 04:09:26 +0000 | [diff] [blame] | 83 | rl_readline_name = "python"; |
| 84 | rl_initialize(); |
Guido van Rossum | adf8769 | 1996-04-09 02:45:31 +0000 | [diff] [blame] | 85 | rl_bind_key('\t', rl_insert); |
| 86 | been_here++; |
| 87 | } |
| 88 | } |
Guido van Rossum | adf8769 | 1996-04-09 02:45:31 +0000 | [diff] [blame] | 89 | |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 90 | char * |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 91 | PyOS_GnuReadline(prompt) |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 92 | char *prompt; |
| 93 | { |
| 94 | int n; |
| 95 | char *p; |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 96 | RETSIGTYPE (*old_inthandler)(); |
Guido van Rossum | adf8769 | 1996-04-09 02:45:31 +0000 | [diff] [blame] | 97 | PyOS_ReadlineInit(); |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 98 | old_inthandler = signal(SIGINT, onintr); |
| 99 | if (setjmp(jbuf)) { |
Guido van Rossum | c7fea2f | 1996-01-12 01:30:55 +0000 | [diff] [blame] | 100 | #ifdef HAVE_SIGRELSE |
| 101 | /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */ |
| 102 | sigrelse(SIGINT); |
| 103 | #endif |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 104 | signal(SIGINT, old_inthandler); |
| 105 | return NULL; |
| 106 | } |
| 107 | p = readline(prompt); |
| 108 | signal(SIGINT, old_inthandler); |
| 109 | if (p == NULL) { |
| 110 | p = malloc(1); |
| 111 | if (p != NULL) |
| 112 | *p = '\0'; |
| 113 | return p; |
| 114 | } |
| 115 | n = strlen(p); |
| 116 | if (n > 0) |
| 117 | add_history(p); |
| 118 | if ((p = realloc(p, n+2)) != NULL) { |
| 119 | p[n] = '\n'; |
| 120 | p[n+1] = '\0'; |
| 121 | } |
| 122 | return p; |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | #endif /* !WITH_READLINE */ |
| 126 | |
| 127 | |
| 128 | /* This function restarts a fgets() after an EINTR error occurred |
| 129 | except if PyOS_InterruptOccurred() returns true. */ |
| 130 | |
| 131 | static int |
| 132 | my_fgets(buf, len, fp) |
| 133 | char *buf; |
| 134 | int len; |
| 135 | FILE *fp; |
| 136 | { |
| 137 | char *p; |
| 138 | for (;;) { |
| 139 | errno = 0; |
| 140 | p = fgets(buf, len, fp); |
| 141 | if (p != NULL) |
| 142 | return 0; /* No error */ |
| 143 | if (feof(fp)) { |
| 144 | return -1; /* EOF */ |
| 145 | } |
| 146 | #ifdef EINTR |
| 147 | if (errno == EINTR) { |
| 148 | if (PyOS_InterruptOccurred()) { |
| 149 | return 1; /* Interrupt */ |
| 150 | } |
| 151 | continue; |
| 152 | } |
| 153 | #endif |
| 154 | if (PyOS_InterruptOccurred()) { |
| 155 | return 1; /* Interrupt */ |
| 156 | } |
| 157 | return -2; /* Error */ |
| 158 | } |
| 159 | /* NOTREACHED */ |
| 160 | } |
| 161 | |
| 162 | |
| 163 | /* Readline implementation using fgets() */ |
| 164 | |
| 165 | char * |
| 166 | PyOS_StdioReadline(prompt) |
| 167 | char *prompt; |
| 168 | { |
| 169 | int n; |
| 170 | char *p; |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 171 | n = 100; |
| 172 | if ((p = malloc(n)) == NULL) |
| 173 | return NULL; |
Guido van Rossum | c7fea2f | 1996-01-12 01:30:55 +0000 | [diff] [blame] | 174 | fflush(stdout); |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 175 | if (prompt) |
| 176 | fprintf(stderr, "%s", prompt); |
Guido van Rossum | c7fea2f | 1996-01-12 01:30:55 +0000 | [diff] [blame] | 177 | fflush(stderr); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 178 | switch (my_fgets(p, n, stdin)) { |
| 179 | case 0: /* Normal case */ |
| 180 | break; |
| 181 | case 1: /* Interrupt */ |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 182 | free(p); |
| 183 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 184 | case -1: /* EOF */ |
| 185 | case -2: /* Error */ |
| 186 | default: /* Shouldn't happen */ |
| 187 | *p = '\0'; |
| 188 | break; |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 189 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 190 | #ifdef MPW |
| 191 | /* Hack for MPW C where the prompt comes right back in the input */ |
| 192 | /* XXX (Actually this would be rather nice on most systems...) */ |
| 193 | n = strlen(prompt); |
| 194 | if (strncmp(p, prompt, n) == 0) |
| 195 | memmove(p, p + n, strlen(p) - n + 1); |
| 196 | #endif |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 197 | n = strlen(p); |
| 198 | while (n > 0 && p[n-1] != '\n') { |
| 199 | int incr = n+2; |
| 200 | p = realloc(p, n + incr); |
| 201 | if (p == NULL) |
| 202 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 203 | if (my_fgets(p+n, incr, stdin) != 0) |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 204 | break; |
| 205 | n += strlen(p+n); |
| 206 | } |
| 207 | return realloc(p, n+1); |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | |
| 211 | /* By initializing this function pointer, systems embedding Python can |
| 212 | override the readline function. */ |
| 213 | |
| 214 | char *(*PyOS_ReadlineFunctionPointer) Py_PROTO((char *)); |
| 215 | |
| 216 | |
| 217 | /* Interface used by tokenizer.c and bltinmodule.c */ |
| 218 | |
| 219 | char * |
| 220 | PyOS_Readline(prompt) |
| 221 | char *prompt; |
| 222 | { |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 223 | if (PyOS_ReadlineFunctionPointer == NULL) { |
| 224 | #ifdef WITH_READLINE |
| 225 | if (isatty(fileno(stdin))) |
| 226 | PyOS_ReadlineFunctionPointer = PyOS_GnuReadline; |
| 227 | else |
| 228 | #endif |
| 229 | PyOS_ReadlineFunctionPointer = PyOS_StdioReadline; |
| 230 | } |
| 231 | return (*PyOS_ReadlineFunctionPointer)(prompt); |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 232 | } |