blob: f9c3706156433f0089c1a2e6923ea919f333b300 [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
Guido van Rossumfbd64c81997-02-18 21:53:32 +000032/* 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 Rossum6fa63431993-12-24 10:36:57 +000036 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 Rossumfbd64c81997-02-18 21:53:32 +000042#define Py_USE_NEW_NAMES 1
43
Guido van Rossumb6775db1994-08-01 11:34:53 +000044#include "config.h"
Guido van Rossumb6775db1994-08-01 11:34:53 +000045
Guido van Rossum6fa63431993-12-24 10:36:57 +000046#include <stdio.h>
47#include <string.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +000048#include <errno.h>
Guido van Rossum6fa63431993-12-24 10:36:57 +000049
Guido van Rossumb6775db1994-08-01 11:34:53 +000050#include "myproto.h"
Guido van Rossum6fa63431993-12-24 10:36:57 +000051#include "mymalloc.h"
Guido van Rossumb6775db1994-08-01 11:34:53 +000052#include "intrcheck.h"
Guido van Rossum6fa63431993-12-24 10:36:57 +000053
Guido van Rossumfbd64c81997-02-18 21:53:32 +000054
Guido van Rossumb6775db1994-08-01 11:34:53 +000055#ifdef WITH_READLINE
Guido van Rossum6fa63431993-12-24 10:36:57 +000056
57extern char *readline();
Guido van Rossum0bc253d1996-09-13 04:09:26 +000058extern int rl_initialize();
59extern int rl_insert();
Guido van Rossumfd8a3931996-12-02 18:27:33 +000060extern int rl_bind_key();
61extern void add_history();
Guido van Rossum0bc253d1996-09-13 04:09:26 +000062extern char *rl_readline_name;
Guido van Rossum6fa63431993-12-24 10:36:57 +000063
64#include <setjmp.h>
65#include <signal.h>
66
67static jmp_buf jbuf;
68
69/* ARGSUSED */
70static RETSIGTYPE
71onintr(sig)
72 int sig;
73{
74 longjmp(jbuf, 1);
75}
76
Guido van Rossumadf87691996-04-09 02:45:31 +000077void
78PyOS_ReadlineInit()
79{
80 static int been_here;
81 if (!been_here) {
82 /* Force rebind of TAB to insert-tab */
Guido van Rossum0bc253d1996-09-13 04:09:26 +000083 rl_readline_name = "python";
84 rl_initialize();
Guido van Rossumadf87691996-04-09 02:45:31 +000085 rl_bind_key('\t', rl_insert);
86 been_here++;
87 }
88}
Guido van Rossumadf87691996-04-09 02:45:31 +000089
Guido van Rossum6fa63431993-12-24 10:36:57 +000090char *
Guido van Rossumfbd64c81997-02-18 21:53:32 +000091PyOS_GnuReadline(prompt)
Guido van Rossum6fa63431993-12-24 10:36:57 +000092 char *prompt;
93{
94 int n;
95 char *p;
Guido van Rossum6fa63431993-12-24 10:36:57 +000096 RETSIGTYPE (*old_inthandler)();
Guido van Rossumadf87691996-04-09 02:45:31 +000097 PyOS_ReadlineInit();
Guido van Rossum6fa63431993-12-24 10:36:57 +000098 old_inthandler = signal(SIGINT, onintr);
99 if (setjmp(jbuf)) {
Guido van Rossumc7fea2f1996-01-12 01:30:55 +0000100#ifdef HAVE_SIGRELSE
101 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
102 sigrelse(SIGINT);
103#endif
Guido van Rossum6fa63431993-12-24 10:36:57 +0000104 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 Rossumfbd64c81997-02-18 21:53:32 +0000123}
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
131static int
132my_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
165char *
166PyOS_StdioReadline(prompt)
167 char *prompt;
168{
169 int n;
170 char *p;
Guido van Rossum6fa63431993-12-24 10:36:57 +0000171 n = 100;
172 if ((p = malloc(n)) == NULL)
173 return NULL;
Guido van Rossumc7fea2f1996-01-12 01:30:55 +0000174 fflush(stdout);
Guido van Rossum6fa63431993-12-24 10:36:57 +0000175 if (prompt)
176 fprintf(stderr, "%s", prompt);
Guido van Rossumc7fea2f1996-01-12 01:30:55 +0000177 fflush(stderr);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000178 switch (my_fgets(p, n, stdin)) {
179 case 0: /* Normal case */
180 break;
181 case 1: /* Interrupt */
Guido van Rossum6fa63431993-12-24 10:36:57 +0000182 free(p);
183 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000184 case -1: /* EOF */
185 case -2: /* Error */
186 default: /* Shouldn't happen */
187 *p = '\0';
188 break;
Guido van Rossum6fa63431993-12-24 10:36:57 +0000189 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000190#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 Rossum6fa63431993-12-24 10:36:57 +0000197 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 Rossumb6775db1994-08-01 11:34:53 +0000203 if (my_fgets(p+n, incr, stdin) != 0)
Guido van Rossum6fa63431993-12-24 10:36:57 +0000204 break;
205 n += strlen(p+n);
206 }
207 return realloc(p, n+1);
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000208}
209
210
211/* By initializing this function pointer, systems embedding Python can
212 override the readline function. */
213
214char *(*PyOS_ReadlineFunctionPointer) Py_PROTO((char *));
215
216
217/* Interface used by tokenizer.c and bltinmodule.c */
218
219char *
220PyOS_Readline(prompt)
221 char *prompt;
222{
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000223 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 Rossum6fa63431993-12-24 10:36:57 +0000232}