blob: a94df8f549c0df1c66553200e96330f278c2b39c [file] [log] [blame]
Guido van Rossum6fa63431993-12-24 10:36:57 +00001/***********************************************************
Guido van Rossumb9f8d6e1995-01-04 19:08:09 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossum6fa63431993-12-24 10:36:57 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossum6fa63431993-12-24 10:36:57 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossum6fa63431993-12-24 10:36:57 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossum6fa63431993-12-24 10:36:57 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum6fa63431993-12-24 10:36:57 +000029
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 Rossumb6775db1994-08-01 11:34:53 +000042#include "config.h"
Guido van Rossumb6775db1994-08-01 11:34:53 +000043
Guido van Rossum6fa63431993-12-24 10:36:57 +000044#include <stdio.h>
45#include <string.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +000046#include <errno.h>
Guido van Rossum6fa63431993-12-24 10:36:57 +000047
Guido van Rossumb6775db1994-08-01 11:34:53 +000048#include "myproto.h"
Guido van Rossum6fa63431993-12-24 10:36:57 +000049#include "mymalloc.h"
Guido van Rossumb6775db1994-08-01 11:34:53 +000050#include "intrcheck.h"
Guido van Rossum6fa63431993-12-24 10:36:57 +000051
Guido van Rossumb6775db1994-08-01 11:34:53 +000052#ifdef WITH_READLINE
Guido van Rossum6fa63431993-12-24 10:36:57 +000053
54extern char *readline();
Guido van Rossum0bc253d1996-09-13 04:09:26 +000055extern int rl_initialize();
56extern int rl_insert();
57extern char *rl_readline_name;
Guido van Rossum6fa63431993-12-24 10:36:57 +000058
59#include <setjmp.h>
60#include <signal.h>
61
62static jmp_buf jbuf;
63
64/* ARGSUSED */
65static RETSIGTYPE
66onintr(sig)
67 int sig;
68{
69 longjmp(jbuf, 1);
70}
71
Guido van Rossumb6775db1994-08-01 11:34:53 +000072#else /* !WITH_READLINE */
73
74/* This function restarts a fgets() after an EINTR error occurred
75 except if intrcheck() returns true. */
76
77static int
78my_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 Rossum6fa63431993-12-24 10:36:57 +0000110
Guido van Rossum9ea917e1996-05-24 20:44:12 +0000111#ifdef WITH_READLINE
Guido van Rossumadf87691996-04-09 02:45:31 +0000112void
113PyOS_ReadlineInit()
114{
115 static int been_here;
116 if (!been_here) {
117 /* Force rebind of TAB to insert-tab */
Guido van Rossum0bc253d1996-09-13 04:09:26 +0000118 rl_readline_name = "python";
119 rl_initialize();
Guido van Rossumadf87691996-04-09 02:45:31 +0000120 rl_bind_key('\t', rl_insert);
121 been_here++;
122 }
123}
Guido van Rossum9ea917e1996-05-24 20:44:12 +0000124#endif
Guido van Rossumadf87691996-04-09 02:45:31 +0000125
126
Guido van Rossum6fa63431993-12-24 10:36:57 +0000127char *
128my_readline(prompt)
129 char *prompt;
130{
131 int n;
132 char *p;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000133#ifdef WITH_READLINE
Guido van Rossum6fa63431993-12-24 10:36:57 +0000134 RETSIGTYPE (*old_inthandler)();
Guido van Rossumadf87691996-04-09 02:45:31 +0000135 PyOS_ReadlineInit();
Guido van Rossum6fa63431993-12-24 10:36:57 +0000136 old_inthandler = signal(SIGINT, onintr);
137 if (setjmp(jbuf)) {
Guido van Rossumc7fea2f1996-01-12 01:30:55 +0000138#ifdef HAVE_SIGRELSE
139 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
140 sigrelse(SIGINT);
141#endif
Guido van Rossum6fa63431993-12-24 10:36:57 +0000142 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 Rossumb6775db1994-08-01 11:34:53 +0000161#else /* !WITH_READLINE */
Guido van Rossum6fa63431993-12-24 10:36:57 +0000162 n = 100;
163 if ((p = malloc(n)) == NULL)
164 return NULL;
Guido van Rossumc7fea2f1996-01-12 01:30:55 +0000165 fflush(stdout);
Guido van Rossum6fa63431993-12-24 10:36:57 +0000166 if (prompt)
167 fprintf(stderr, "%s", prompt);
Guido van Rossumc7fea2f1996-01-12 01:30:55 +0000168 fflush(stderr);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000169 switch (my_fgets(p, n, stdin)) {
170 case 0: /* Normal case */
171 break;
172 case 1: /* Interrupt */
Guido van Rossum6fa63431993-12-24 10:36:57 +0000173 free(p);
174 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000175 case -1: /* EOF */
176 case -2: /* Error */
177 default: /* Shouldn't happen */
178 *p = '\0';
179 break;
Guido van Rossum6fa63431993-12-24 10:36:57 +0000180 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000181#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 Rossum6fa63431993-12-24 10:36:57 +0000188 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 Rossumb6775db1994-08-01 11:34:53 +0000194 if (my_fgets(p+n, incr, stdin) != 0)
Guido van Rossum6fa63431993-12-24 10:36:57 +0000195 break;
196 n += strlen(p+n);
197 }
198 return realloc(p, n+1);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000199#endif /* !WITH_READLINE */
Guido van Rossum6fa63431993-12-24 10:36:57 +0000200}