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