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 | |
| 32 | /* Readline interface for tokenizer.c. |
| 33 | By default, we have a super simple my_readline function. |
| 34 | Optionally, we can use the GNU readline library (to be found in the |
| 35 | bash distribution). |
| 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 | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 42 | #include "config.h" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 43 | |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 44 | #include <stdio.h> |
| 45 | #include <string.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 46 | #include <errno.h> |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 48 | #include "myproto.h" |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 49 | #include "mymalloc.h" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 50 | #include "intrcheck.h" |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 52 | #ifdef WITH_READLINE |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 53 | |
| 54 | extern char *readline(); |
Guido van Rossum | 0bc253d | 1996-09-13 04:09:26 +0000 | [diff] [blame] | 55 | extern int rl_initialize(); |
| 56 | extern int rl_insert(); |
| 57 | extern char *rl_readline_name; |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 58 | |
| 59 | #include <setjmp.h> |
| 60 | #include <signal.h> |
| 61 | |
| 62 | static jmp_buf jbuf; |
| 63 | |
| 64 | /* ARGSUSED */ |
| 65 | static RETSIGTYPE |
| 66 | onintr(sig) |
| 67 | int sig; |
| 68 | { |
| 69 | longjmp(jbuf, 1); |
| 70 | } |
| 71 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 72 | #else /* !WITH_READLINE */ |
| 73 | |
| 74 | /* This function restarts a fgets() after an EINTR error occurred |
| 75 | except if intrcheck() returns true. */ |
| 76 | |
| 77 | static int |
| 78 | my_fgets(buf, len, fp) |
| 79 | char *buf; |
| 80 | int len; |
| 81 | FILE *fp; |
| 82 | { |
| 83 | char *p; |
| 84 | for (;;) { |
| 85 | errno = 0; |
| 86 | p = fgets(buf, len, fp); |
| 87 | if (p != NULL) |
| 88 | return 0; /* No error */ |
| 89 | if (feof(fp)) { |
| 90 | return -1; /* EOF */ |
| 91 | } |
| 92 | #ifdef EINTR |
| 93 | if (errno == EINTR) { |
| 94 | if (intrcheck()) { |
| 95 | return 1; /* Interrupt */ |
| 96 | } |
| 97 | continue; |
| 98 | } |
| 99 | #endif |
| 100 | if (intrcheck()) { |
| 101 | return 1; /* Interrupt */ |
| 102 | } |
| 103 | return -2; /* Error */ |
| 104 | } |
| 105 | /* NOTREACHED */ |
| 106 | } |
| 107 | |
| 108 | #endif /* WITH_READLINE */ |
| 109 | |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 110 | |
Guido van Rossum | 9ea917e | 1996-05-24 20:44:12 +0000 | [diff] [blame] | 111 | #ifdef WITH_READLINE |
Guido van Rossum | adf8769 | 1996-04-09 02:45:31 +0000 | [diff] [blame] | 112 | void |
| 113 | PyOS_ReadlineInit() |
| 114 | { |
| 115 | static int been_here; |
| 116 | if (!been_here) { |
| 117 | /* Force rebind of TAB to insert-tab */ |
Guido van Rossum | 0bc253d | 1996-09-13 04:09:26 +0000 | [diff] [blame] | 118 | rl_readline_name = "python"; |
| 119 | rl_initialize(); |
Guido van Rossum | adf8769 | 1996-04-09 02:45:31 +0000 | [diff] [blame] | 120 | rl_bind_key('\t', rl_insert); |
| 121 | been_here++; |
| 122 | } |
| 123 | } |
Guido van Rossum | 9ea917e | 1996-05-24 20:44:12 +0000 | [diff] [blame] | 124 | #endif |
Guido van Rossum | adf8769 | 1996-04-09 02:45:31 +0000 | [diff] [blame] | 125 | |
| 126 | |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 127 | char * |
| 128 | my_readline(prompt) |
| 129 | char *prompt; |
| 130 | { |
| 131 | int n; |
| 132 | char *p; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 133 | #ifdef WITH_READLINE |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 134 | RETSIGTYPE (*old_inthandler)(); |
Guido van Rossum | adf8769 | 1996-04-09 02:45:31 +0000 | [diff] [blame] | 135 | PyOS_ReadlineInit(); |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 136 | old_inthandler = signal(SIGINT, onintr); |
| 137 | if (setjmp(jbuf)) { |
Guido van Rossum | c7fea2f | 1996-01-12 01:30:55 +0000 | [diff] [blame] | 138 | #ifdef HAVE_SIGRELSE |
| 139 | /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */ |
| 140 | sigrelse(SIGINT); |
| 141 | #endif |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 142 | signal(SIGINT, old_inthandler); |
| 143 | return NULL; |
| 144 | } |
| 145 | p = readline(prompt); |
| 146 | signal(SIGINT, old_inthandler); |
| 147 | if (p == NULL) { |
| 148 | p = malloc(1); |
| 149 | if (p != NULL) |
| 150 | *p = '\0'; |
| 151 | return p; |
| 152 | } |
| 153 | n = strlen(p); |
| 154 | if (n > 0) |
| 155 | add_history(p); |
| 156 | if ((p = realloc(p, n+2)) != NULL) { |
| 157 | p[n] = '\n'; |
| 158 | p[n+1] = '\0'; |
| 159 | } |
| 160 | return p; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 161 | #else /* !WITH_READLINE */ |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 162 | n = 100; |
| 163 | if ((p = malloc(n)) == NULL) |
| 164 | return NULL; |
Guido van Rossum | c7fea2f | 1996-01-12 01:30:55 +0000 | [diff] [blame] | 165 | fflush(stdout); |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 166 | if (prompt) |
| 167 | fprintf(stderr, "%s", prompt); |
Guido van Rossum | c7fea2f | 1996-01-12 01:30:55 +0000 | [diff] [blame] | 168 | fflush(stderr); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 169 | switch (my_fgets(p, n, stdin)) { |
| 170 | case 0: /* Normal case */ |
| 171 | break; |
| 172 | case 1: /* Interrupt */ |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 173 | free(p); |
| 174 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 175 | case -1: /* EOF */ |
| 176 | case -2: /* Error */ |
| 177 | default: /* Shouldn't happen */ |
| 178 | *p = '\0'; |
| 179 | break; |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 180 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 181 | #ifdef MPW |
| 182 | /* Hack for MPW C where the prompt comes right back in the input */ |
| 183 | /* XXX (Actually this would be rather nice on most systems...) */ |
| 184 | n = strlen(prompt); |
| 185 | if (strncmp(p, prompt, n) == 0) |
| 186 | memmove(p, p + n, strlen(p) - n + 1); |
| 187 | #endif |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 188 | n = strlen(p); |
| 189 | while (n > 0 && p[n-1] != '\n') { |
| 190 | int incr = n+2; |
| 191 | p = realloc(p, n + incr); |
| 192 | if (p == NULL) |
| 193 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 194 | if (my_fgets(p+n, incr, stdin) != 0) |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 195 | break; |
| 196 | n += strlen(p+n); |
| 197 | } |
| 198 | return realloc(p, n+1); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 199 | #endif /* !WITH_READLINE */ |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 200 | } |