blob: cb1cf0f56d7d24394adbd7aec8e5facf5b3e5f96 [file] [log] [blame]
Guido van Rossum6fa63431993-12-24 10:36:57 +00001
Guido van Rossumfbd64c81997-02-18 21:53:32 +00002/* Readline interface for tokenizer.c and [raw_]input() in bltinmodule.c.
3 By default, or when stdin is not a tty device, we have a super
4 simple my_readline function using fgets.
5 Optionally, we can use the GNU readline library.
Guido van Rossum6fa63431993-12-24 10:36:57 +00006 my_readline() has a different return value from GNU readline():
7 - NULL if an interrupt occurred or if an error occurred
8 - a malloc'ed empty string if EOF was read
9 - a malloc'ed string ending in \n normally
10*/
11
Guido van Rossum8efa47b1998-08-27 19:43:43 +000012#include "Python.h"
Mark Hammond2f10cb82002-07-14 23:12:29 +000013#ifdef MS_WINDOWS
14#define WIN32_LEAN_AND_MEAN
15#include "windows.h"
16#endif /* MS_WINDOWS */
Guido van Rossum6fa63431993-12-24 10:36:57 +000017
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +000018#ifdef __VMS
19extern char* vms__StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt);
20#endif
21
Michael W. Hudson30ea2f22004-07-07 17:44:12 +000022
23PyThreadState* _PyOS_ReadlineTState;
24
Tim Petersb7e898a2004-07-07 20:42:07 +000025#ifdef WITH_THREAD
Michael W. Hudson30ea2f22004-07-07 17:44:12 +000026#include "pythread.h"
27static PyThread_type_lock _PyOS_ReadlineLock = NULL;
28#endif
29
Thomas Wouters23c9e002000-07-22 19:20:54 +000030int (*PyOS_InputHook)(void) = NULL;
Guido van Rossumfbd64c81997-02-18 21:53:32 +000031
32/* This function restarts a fgets() after an EINTR error occurred
33 except if PyOS_InterruptOccurred() returns true. */
34
35static int
Thomas Wouters23c9e002000-07-22 19:20:54 +000036my_fgets(char *buf, int len, FILE *fp)
Guido van Rossumfbd64c81997-02-18 21:53:32 +000037{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 char *p;
Antoine Pitrouc345ce12011-12-16 12:28:32 +010039 int err;
Victor Stinner52c950f2011-04-09 15:55:44 +020040 while (1) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000041 if (PyOS_InputHook != NULL)
42 (void)(PyOS_InputHook)();
43 errno = 0;
Victor Stinner4f711012011-05-30 23:46:00 +020044 clearerr(fp);
Martin v. Löwise654c112012-04-30 06:10:41 +020045 if (_PyVerify_fd(fileno(fp)))
46 p = fgets(buf, len, fp);
47 else
48 p = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000049 if (p != NULL)
50 return 0; /* No error */
Antoine Pitrouc345ce12011-12-16 12:28:32 +010051 err = errno;
Mark Hammond2f10cb82002-07-14 23:12:29 +000052#ifdef MS_WINDOWS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000053 /* In the case of a Ctrl+C or some other external event
54 interrupting the operation:
55 Win2k/NT: ERROR_OPERATION_ABORTED is the most recent Win32
56 error code (and feof() returns TRUE).
57 Win9x: Ctrl+C seems to have no effect on fgets() returning
58 early - the signal handler is called, but the fgets()
59 only returns "normally" (ie, when Enter hit or feof())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000061 if (GetLastError()==ERROR_OPERATION_ABORTED) {
62 /* Signals come asynchronously, so we sleep a brief
63 moment before checking if the handler has been
64 triggered (we cant just return 1 before the
65 signal handler has been called, as the later
66 signal may be treated as a separate interrupt).
67 */
68 Sleep(1);
69 if (PyOS_InterruptOccurred()) {
70 return 1; /* Interrupt */
71 }
72 /* Either the sleep wasn't long enough (need a
73 short loop retrying?) or not interrupted at all
74 (in which case we should revisit the whole thing!)
75 Logging some warning would be nice. assert is not
76 viable as under the debugger, the various dialogs
77 mean the condition is not true.
78 */
79 }
Mark Hammond2f10cb82002-07-14 23:12:29 +000080#endif /* MS_WINDOWS */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000081 if (feof(fp)) {
Victor Stinner4755ab02011-05-10 00:19:53 +020082 clearerr(fp);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000083 return -1; /* EOF */
84 }
Guido van Rossumfbd64c81997-02-18 21:53:32 +000085#ifdef EINTR
Antoine Pitrouc345ce12011-12-16 12:28:32 +010086 if (err == EINTR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000087 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +000088#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000089 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +000090#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000091 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +000092#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000093 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +000094#endif
Victor Stinner52c950f2011-04-09 15:55:44 +020095 if (s < 0)
96 return 1;
Victor Stinnera870e352011-04-09 15:59:25 +020097 /* try again */
Victor Stinner52c950f2011-04-09 15:55:44 +020098 continue;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000099 }
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000100#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 if (PyOS_InterruptOccurred()) {
102 return 1; /* Interrupt */
103 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000104 return -2; /* Error */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000106 /* NOTREACHED */
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000107}
108
109
110/* Readline implementation using fgets() */
111
112char *
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000113PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000114{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 size_t n;
116 char *p;
117 n = 100;
118 if ((p = (char *)PyMem_MALLOC(n)) == NULL)
119 return NULL;
120 fflush(sys_stdout);
121 if (prompt)
122 fprintf(stderr, "%s", prompt);
123 fflush(stderr);
124 switch (my_fgets(p, (int)n, sys_stdin)) {
125 case 0: /* Normal case */
126 break;
127 case 1: /* Interrupt */
128 PyMem_FREE(p);
129 return NULL;
130 case -1: /* EOF */
131 case -2: /* Error */
132 default: /* Shouldn't happen */
133 *p = '\0';
134 break;
135 }
136 n = strlen(p);
137 while (n > 0 && p[n-1] != '\n') {
138 size_t incr = n+2;
139 p = (char *)PyMem_REALLOC(p, n + incr);
140 if (p == NULL)
141 return NULL;
142 if (incr > INT_MAX) {
143 PyErr_SetString(PyExc_OverflowError, "input line too long");
144 }
145 if (my_fgets(p+n, (int)incr, sys_stdin) != 0)
146 break;
147 n += strlen(p+n);
148 }
149 return (char *)PyMem_REALLOC(p, n+1);
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000150}
151
152
153/* By initializing this function pointer, systems embedding Python can
Guido van Rossumb18618d2000-05-03 23:44:39 +0000154 override the readline function.
155
156 Note: Python expects in return a buffer allocated with PyMem_Malloc. */
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000157
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000158char *(*PyOS_ReadlineFunctionPointer)(FILE *, FILE *, char *);
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000159
160
161/* Interface used by tokenizer.c and bltinmodule.c */
162
163char *
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000164PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 char *rv;
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 if (_PyOS_ReadlineTState == PyThreadState_GET()) {
169 PyErr_SetString(PyExc_RuntimeError,
170 "can't re-enter readline");
171 return NULL;
172 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174
175 if (PyOS_ReadlineFunctionPointer == NULL) {
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000176#ifdef __VMS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 PyOS_ReadlineFunctionPointer = vms__StdioReadline;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000178#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179 PyOS_ReadlineFunctionPointer = PyOS_StdioReadline;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000180#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000182
Tim Petersb7e898a2004-07-07 20:42:07 +0000183#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 if (_PyOS_ReadlineLock == NULL) {
185 _PyOS_ReadlineLock = PyThread_allocate_lock();
186 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000187#endif
188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 _PyOS_ReadlineTState = PyThreadState_GET();
190 Py_BEGIN_ALLOW_THREADS
191#ifdef WITH_THREAD
192 PyThread_acquire_lock(_PyOS_ReadlineLock, 1);
193#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 /* This is needed to handle the unlikely case that the
196 * interpreter is in interactive mode *and* stdin/out are not
197 * a tty. This can happen, for example if python is run like
198 * this: python -i < test1.py
199 */
200 if (!isatty (fileno (sys_stdin)) || !isatty (fileno (sys_stdout)))
201 rv = PyOS_StdioReadline (sys_stdin, sys_stdout, prompt);
202 else
203 rv = (*PyOS_ReadlineFunctionPointer)(sys_stdin, sys_stdout,
204 prompt);
205 Py_END_ALLOW_THREADS
206
207#ifdef WITH_THREAD
208 PyThread_release_lock(_PyOS_ReadlineLock);
209#endif
210
211 _PyOS_ReadlineTState = NULL;
212
213 return rv;
Guido van Rossum6fa63431993-12-24 10:36:57 +0000214}