blob: 5eff847d161046116e0d09539607a8975ba4a8d6 [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();
Guido van Rossumfd8a3931996-12-02 18:27:33 +000057extern int rl_bind_key();
58extern void add_history();
Guido van Rossum0bc253d1996-09-13 04:09:26 +000059extern char *rl_readline_name;
Guido van Rossum6fa63431993-12-24 10:36:57 +000060
61#include <setjmp.h>
62#include <signal.h>
63
64static jmp_buf jbuf;
65
66/* ARGSUSED */
67static RETSIGTYPE
68onintr(sig)
69 int sig;
70{
71 longjmp(jbuf, 1);
72}
73
Guido van Rossumb6775db1994-08-01 11:34:53 +000074#else /* !WITH_READLINE */
75
76/* This function restarts a fgets() after an EINTR error occurred
77 except if intrcheck() returns true. */
78
79static int
80my_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 Rossum6fa63431993-12-24 10:36:57 +0000112
Guido van Rossum9ea917e1996-05-24 20:44:12 +0000113#ifdef WITH_READLINE
Guido van Rossumadf87691996-04-09 02:45:31 +0000114void
115PyOS_ReadlineInit()
116{
117 static int been_here;
118 if (!been_here) {
119 /* Force rebind of TAB to insert-tab */
Guido van Rossum0bc253d1996-09-13 04:09:26 +0000120 rl_readline_name = "python";
121 rl_initialize();
Guido van Rossumadf87691996-04-09 02:45:31 +0000122 rl_bind_key('\t', rl_insert);
123 been_here++;
124 }
125}
Guido van Rossum9ea917e1996-05-24 20:44:12 +0000126#endif
Guido van Rossumadf87691996-04-09 02:45:31 +0000127
128
Guido van Rossum6fa63431993-12-24 10:36:57 +0000129char *
130my_readline(prompt)
131 char *prompt;
132{
133 int n;
134 char *p;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000135#ifdef WITH_READLINE
Guido van Rossum6fa63431993-12-24 10:36:57 +0000136 RETSIGTYPE (*old_inthandler)();
Guido van Rossumadf87691996-04-09 02:45:31 +0000137 PyOS_ReadlineInit();
Guido van Rossum6fa63431993-12-24 10:36:57 +0000138 old_inthandler = signal(SIGINT, onintr);
139 if (setjmp(jbuf)) {
Guido van Rossumc7fea2f1996-01-12 01:30:55 +0000140#ifdef HAVE_SIGRELSE
141 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
142 sigrelse(SIGINT);
143#endif
Guido van Rossum6fa63431993-12-24 10:36:57 +0000144 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 Rossumb6775db1994-08-01 11:34:53 +0000163#else /* !WITH_READLINE */
Guido van Rossum6fa63431993-12-24 10:36:57 +0000164 n = 100;
165 if ((p = malloc(n)) == NULL)
166 return NULL;
Guido van Rossumc7fea2f1996-01-12 01:30:55 +0000167 fflush(stdout);
Guido van Rossum6fa63431993-12-24 10:36:57 +0000168 if (prompt)
169 fprintf(stderr, "%s", prompt);
Guido van Rossumc7fea2f1996-01-12 01:30:55 +0000170 fflush(stderr);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000171 switch (my_fgets(p, n, stdin)) {
172 case 0: /* Normal case */
173 break;
174 case 1: /* Interrupt */
Guido van Rossum6fa63431993-12-24 10:36:57 +0000175 free(p);
176 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000177 case -1: /* EOF */
178 case -2: /* Error */
179 default: /* Shouldn't happen */
180 *p = '\0';
181 break;
Guido van Rossum6fa63431993-12-24 10:36:57 +0000182 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000183#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 Rossum6fa63431993-12-24 10:36:57 +0000190 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 Rossumb6775db1994-08-01 11:34:53 +0000196 if (my_fgets(p+n, incr, stdin) != 0)
Guido van Rossum6fa63431993-12-24 10:36:57 +0000197 break;
198 n += strlen(p+n);
199 }
200 return realloc(p, n+1);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000201#endif /* !WITH_READLINE */
Guido van Rossum6fa63431993-12-24 10:36:57 +0000202}