blob: 24bb18c653dc8cf289cb4a794795a59a8cd16151 [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 Rossum44620641997-08-11 18:57:29 +000054int (*PyOS_InputHook)() = NULL;
Guido van Rossumfbd64c81997-02-18 21:53:32 +000055
56/* This function restarts a fgets() after an EINTR error occurred
57 except if PyOS_InterruptOccurred() returns true. */
58
59static int
60my_fgets(buf, len, fp)
61 char *buf;
62 int len;
63 FILE *fp;
64{
65 char *p;
66 for (;;) {
Guido van Rossum44620641997-08-11 18:57:29 +000067 if (PyOS_InputHook != NULL)
68 (void)(PyOS_InputHook)();
Guido van Rossumfbd64c81997-02-18 21:53:32 +000069 errno = 0;
70 p = fgets(buf, len, fp);
71 if (p != NULL)
72 return 0; /* No error */
73 if (feof(fp)) {
74 return -1; /* EOF */
75 }
76#ifdef EINTR
77 if (errno == EINTR) {
78 if (PyOS_InterruptOccurred()) {
79 return 1; /* Interrupt */
80 }
81 continue;
82 }
83#endif
84 if (PyOS_InterruptOccurred()) {
85 return 1; /* Interrupt */
86 }
87 return -2; /* Error */
88 }
89 /* NOTREACHED */
90}
91
92
93/* Readline implementation using fgets() */
94
95char *
96PyOS_StdioReadline(prompt)
97 char *prompt;
98{
99 int n;
100 char *p;
Guido van Rossum6fa63431993-12-24 10:36:57 +0000101 n = 100;
102 if ((p = malloc(n)) == NULL)
103 return NULL;
Guido van Rossumc7fea2f1996-01-12 01:30:55 +0000104 fflush(stdout);
Guido van Rossum6fa63431993-12-24 10:36:57 +0000105 if (prompt)
106 fprintf(stderr, "%s", prompt);
Guido van Rossumc7fea2f1996-01-12 01:30:55 +0000107 fflush(stderr);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000108 switch (my_fgets(p, n, stdin)) {
109 case 0: /* Normal case */
110 break;
111 case 1: /* Interrupt */
Guido van Rossum6fa63431993-12-24 10:36:57 +0000112 free(p);
113 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000114 case -1: /* EOF */
115 case -2: /* Error */
116 default: /* Shouldn't happen */
117 *p = '\0';
118 break;
Guido van Rossum6fa63431993-12-24 10:36:57 +0000119 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000120#ifdef MPW
121 /* Hack for MPW C where the prompt comes right back in the input */
122 /* XXX (Actually this would be rather nice on most systems...) */
123 n = strlen(prompt);
124 if (strncmp(p, prompt, n) == 0)
125 memmove(p, p + n, strlen(p) - n + 1);
126#endif
Guido van Rossum6fa63431993-12-24 10:36:57 +0000127 n = strlen(p);
128 while (n > 0 && p[n-1] != '\n') {
129 int incr = n+2;
130 p = realloc(p, n + incr);
131 if (p == NULL)
132 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000133 if (my_fgets(p+n, incr, stdin) != 0)
Guido van Rossum6fa63431993-12-24 10:36:57 +0000134 break;
135 n += strlen(p+n);
136 }
137 return realloc(p, n+1);
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000138}
139
140
141/* By initializing this function pointer, systems embedding Python can
142 override the readline function. */
143
144char *(*PyOS_ReadlineFunctionPointer) Py_PROTO((char *));
145
146
147/* Interface used by tokenizer.c and bltinmodule.c */
148
149char *
150PyOS_Readline(prompt)
151 char *prompt;
152{
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000153 if (PyOS_ReadlineFunctionPointer == NULL) {
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000154 PyOS_ReadlineFunctionPointer = PyOS_StdioReadline;
155 }
156 return (*PyOS_ReadlineFunctionPointer)(prompt);
Guido van Rossum6fa63431993-12-24 10:36:57 +0000157}