blob: 56c343aa6d0b6186db4b337d4968398af9ebc7ce [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 Rossum8efa47b1998-08-27 19:43:43 +000042#include "Python.h"
Guido van Rossum6fa63431993-12-24 10:36:57 +000043
Guido van Rossum44620641997-08-11 18:57:29 +000044int (*PyOS_InputHook)() = NULL;
Guido van Rossumfbd64c81997-02-18 21:53:32 +000045
46/* This function restarts a fgets() after an EINTR error occurred
47 except if PyOS_InterruptOccurred() returns true. */
48
49static int
50my_fgets(buf, len, fp)
51 char *buf;
52 int len;
53 FILE *fp;
54{
55 char *p;
56 for (;;) {
Guido van Rossum44620641997-08-11 18:57:29 +000057 if (PyOS_InputHook != NULL)
58 (void)(PyOS_InputHook)();
Guido van Rossumfbd64c81997-02-18 21:53:32 +000059 errno = 0;
60 p = fgets(buf, len, fp);
61 if (p != NULL)
62 return 0; /* No error */
63 if (feof(fp)) {
64 return -1; /* EOF */
65 }
66#ifdef EINTR
67 if (errno == EINTR) {
68 if (PyOS_InterruptOccurred()) {
69 return 1; /* Interrupt */
70 }
71 continue;
72 }
73#endif
74 if (PyOS_InterruptOccurred()) {
75 return 1; /* Interrupt */
76 }
77 return -2; /* Error */
78 }
79 /* NOTREACHED */
80}
81
82
83/* Readline implementation using fgets() */
84
85char *
86PyOS_StdioReadline(prompt)
87 char *prompt;
88{
89 int n;
90 char *p;
Guido van Rossum6fa63431993-12-24 10:36:57 +000091 n = 100;
92 if ((p = malloc(n)) == NULL)
93 return NULL;
Guido van Rossumc7fea2f1996-01-12 01:30:55 +000094 fflush(stdout);
Guido van Rossum6fa63431993-12-24 10:36:57 +000095 if (prompt)
96 fprintf(stderr, "%s", prompt);
Guido van Rossumc7fea2f1996-01-12 01:30:55 +000097 fflush(stderr);
Guido van Rossumb6775db1994-08-01 11:34:53 +000098 switch (my_fgets(p, n, stdin)) {
99 case 0: /* Normal case */
100 break;
101 case 1: /* Interrupt */
Guido van Rossum6fa63431993-12-24 10:36:57 +0000102 free(p);
103 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000104 case -1: /* EOF */
105 case -2: /* Error */
106 default: /* Shouldn't happen */
107 *p = '\0';
108 break;
Guido van Rossum6fa63431993-12-24 10:36:57 +0000109 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000110#ifdef MPW
111 /* Hack for MPW C where the prompt comes right back in the input */
112 /* XXX (Actually this would be rather nice on most systems...) */
113 n = strlen(prompt);
114 if (strncmp(p, prompt, n) == 0)
115 memmove(p, p + n, strlen(p) - n + 1);
116#endif
Guido van Rossum6fa63431993-12-24 10:36:57 +0000117 n = strlen(p);
118 while (n > 0 && p[n-1] != '\n') {
119 int incr = n+2;
120 p = realloc(p, n + incr);
121 if (p == NULL)
122 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000123 if (my_fgets(p+n, incr, stdin) != 0)
Guido van Rossum6fa63431993-12-24 10:36:57 +0000124 break;
125 n += strlen(p+n);
126 }
127 return realloc(p, n+1);
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000128}
129
130
131/* By initializing this function pointer, systems embedding Python can
132 override the readline function. */
133
134char *(*PyOS_ReadlineFunctionPointer) Py_PROTO((char *));
135
136
137/* Interface used by tokenizer.c and bltinmodule.c */
138
139char *
140PyOS_Readline(prompt)
141 char *prompt;
142{
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000143 if (PyOS_ReadlineFunctionPointer == NULL) {
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000144 PyOS_ReadlineFunctionPointer = PyOS_StdioReadline;
145 }
Guido van Rossum8efa47b1998-08-27 19:43:43 +0000146 Py_BEGIN_ALLOW_THREADS
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000147 return (*PyOS_ReadlineFunctionPointer)(prompt);
Guido van Rossum8efa47b1998-08-27 19:43:43 +0000148 Py_END_ALLOW_THREADS
Guido van Rossum6fa63431993-12-24 10:36:57 +0000149}