blob: 2aa3bef2b0bbf6ede7b1e2dbf422db9fab39cde2 [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"
Eric Snow2ebc5ce2017-09-07 23:51:28 -060013#include "internal/pystate.h"
Mark Hammond2f10cb82002-07-14 23:12:29 +000014#ifdef MS_WINDOWS
15#define WIN32_LEAN_AND_MEAN
16#include "windows.h"
17#endif /* MS_WINDOWS */
Guido van Rossum6fa63431993-12-24 10:36:57 +000018
Michael W. Hudson30ea2f22004-07-07 17:44:12 +000019
Benjamin Peterson0a37a302017-12-31 10:04:13 -080020PyThreadState* _PyOS_ReadlineTState = NULL;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +000021
Michael W. Hudson30ea2f22004-07-07 17:44:12 +000022#include "pythread.h"
23static PyThread_type_lock _PyOS_ReadlineLock = NULL;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +000024
Thomas Wouters23c9e002000-07-22 19:20:54 +000025int (*PyOS_InputHook)(void) = NULL;
Guido van Rossumfbd64c81997-02-18 21:53:32 +000026
27/* This function restarts a fgets() after an EINTR error occurred
28 except if PyOS_InterruptOccurred() returns true. */
29
30static int
Thomas Wouters23c9e002000-07-22 19:20:54 +000031my_fgets(char *buf, int len, FILE *fp)
Guido van Rossumfbd64c81997-02-18 21:53:32 +000032{
Tim Golden9175c3d2012-06-29 18:39:26 +010033#ifdef MS_WINDOWS
34 HANDLE hInterruptEvent;
35#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036 char *p;
Antoine Pitrouc345ce12011-12-16 12:28:32 +010037 int err;
Victor Stinner52c950f2011-04-09 15:55:44 +020038 while (1) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000039 if (PyOS_InputHook != NULL)
40 (void)(PyOS_InputHook)();
41 errno = 0;
Victor Stinner4f711012011-05-30 23:46:00 +020042 clearerr(fp);
Steve Dower940f33a2016-09-08 11:21:54 -070043 p = fgets(buf, len, fp);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000044 if (p != NULL)
45 return 0; /* No error */
Antoine Pitrouc345ce12011-12-16 12:28:32 +010046 err = errno;
Mark Hammond2f10cb82002-07-14 23:12:29 +000047#ifdef MS_WINDOWS
Tim Golden9175c3d2012-06-29 18:39:26 +010048 /* Ctrl-C anywhere on the line or Ctrl-Z if the only character
49 on a line will set ERROR_OPERATION_ABORTED. Under normal
50 circumstances Ctrl-C will also have caused the SIGINT handler
51 to fire which will have set the event object returned by
52 _PyOS_SigintEvent. This signal fires in another thread and
53 is not guaranteed to have occurred before this point in the
54 code.
55
56 Therefore: check whether the event is set with a small timeout.
57 If it is, assume this is a Ctrl-C and reset the event. If it
58 isn't set assume that this is a Ctrl-Z on its own and drop
59 through to check for EOF.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000061 if (GetLastError()==ERROR_OPERATION_ABORTED) {
Tim Golden9175c3d2012-06-29 18:39:26 +010062 hInterruptEvent = _PyOS_SigintEvent();
Martin v. Löwisb26a9b12013-01-25 14:25:48 +010063 switch (WaitForSingleObjectEx(hInterruptEvent, 10, FALSE)) {
Tim Golden9175c3d2012-06-29 18:39:26 +010064 case WAIT_OBJECT_0:
65 ResetEvent(hInterruptEvent);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000066 return 1; /* Interrupt */
Tim Golden9175c3d2012-06-29 18:39:26 +010067 case WAIT_FAILED:
68 return -2; /* Error */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000069 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000070 }
Mark Hammond2f10cb82002-07-14 23:12:29 +000071#endif /* MS_WINDOWS */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000072 if (feof(fp)) {
Victor Stinner4755ab02011-05-10 00:19:53 +020073 clearerr(fp);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000074 return -1; /* EOF */
75 }
Guido van Rossumfbd64c81997-02-18 21:53:32 +000076#ifdef EINTR
Antoine Pitrouc345ce12011-12-16 12:28:32 +010077 if (err == EINTR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000078 int s;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000079 PyEval_RestoreThread(_PyOS_ReadlineTState);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000080 s = PyErr_CheckSignals();
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000081 PyEval_SaveThread();
Victor Stinner52c950f2011-04-09 15:55:44 +020082 if (s < 0)
83 return 1;
Tim Golden9175c3d2012-06-29 18:39:26 +010084 /* try again */
Victor Stinner52c950f2011-04-09 15:55:44 +020085 continue;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000086 }
Guido van Rossumfbd64c81997-02-18 21:53:32 +000087#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 if (PyOS_InterruptOccurred()) {
89 return 1; /* Interrupt */
90 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000091 return -2; /* Error */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000093 /* NOTREACHED */
Guido van Rossumfbd64c81997-02-18 21:53:32 +000094}
95
Steve Dower39294992016-08-30 21:22:36 -070096#ifdef MS_WINDOWS
97/* Readline implementation using ReadConsoleW */
98
99extern char _get_console_type(HANDLE handle);
100
101char *
102_PyOS_WindowsConsoleReadline(HANDLE hStdIn)
103{
104 static wchar_t wbuf_local[1024 * 16];
105 const DWORD chunk_size = 1024;
106
107 DWORD n_read, total_read, wbuflen, u8len;
108 wchar_t *wbuf;
109 char *buf = NULL;
110 int err = 0;
111
112 n_read = 0;
113 total_read = 0;
114 wbuf = wbuf_local;
115 wbuflen = sizeof(wbuf_local) / sizeof(wbuf_local[0]) - 1;
116 while (1) {
Miss Islington (bot)9d925242018-06-28 10:48:37 -0700117 if (PyOS_InputHook != NULL) {
118 (void)(PyOS_InputHook)();
119 }
Steve Dower39294992016-08-30 21:22:36 -0700120 if (!ReadConsoleW(hStdIn, &wbuf[total_read], wbuflen - total_read, &n_read, NULL)) {
121 err = GetLastError();
122 goto exit;
123 }
124 if (n_read == 0) {
125 int s;
126 err = GetLastError();
127 if (err != ERROR_OPERATION_ABORTED)
128 goto exit;
129 err = 0;
130 HANDLE hInterruptEvent = _PyOS_SigintEvent();
131 if (WaitForSingleObjectEx(hInterruptEvent, 100, FALSE)
132 == WAIT_OBJECT_0) {
133 ResetEvent(hInterruptEvent);
Steve Dower39294992016-08-30 21:22:36 -0700134 PyEval_RestoreThread(_PyOS_ReadlineTState);
Steve Dower39294992016-08-30 21:22:36 -0700135 s = PyErr_CheckSignals();
Steve Dower39294992016-08-30 21:22:36 -0700136 PyEval_SaveThread();
Steve Dower39294992016-08-30 21:22:36 -0700137 if (s < 0)
138 goto exit;
139 }
140 break;
141 }
142
143 total_read += n_read;
144 if (total_read == 0 || wbuf[total_read - 1] == L'\n') {
145 break;
146 }
147 wbuflen += chunk_size;
148 if (wbuf == wbuf_local) {
149 wbuf[total_read] = '\0';
150 wbuf = (wchar_t*)PyMem_RawMalloc(wbuflen * sizeof(wchar_t));
151 if (wbuf)
152 wcscpy_s(wbuf, wbuflen, wbuf_local);
153 }
154 else
155 wbuf = (wchar_t*)PyMem_RawRealloc(wbuf, wbuflen * sizeof(wchar_t));
156 }
157
158 if (wbuf[0] == '\x1a') {
159 buf = PyMem_RawMalloc(1);
160 if (buf)
161 buf[0] = '\0';
162 goto exit;
163 }
164
165 u8len = WideCharToMultiByte(CP_UTF8, 0, wbuf, total_read, NULL, 0, NULL, NULL);
166 buf = PyMem_RawMalloc(u8len + 1);
167 u8len = WideCharToMultiByte(CP_UTF8, 0, wbuf, total_read, buf, u8len, NULL, NULL);
168 buf[u8len] = '\0';
Benjamin Petersone2e792d2016-09-19 22:17:16 -0700169
Steve Dower39294992016-08-30 21:22:36 -0700170exit:
171 if (wbuf != wbuf_local)
172 PyMem_RawFree(wbuf);
173
174 if (err) {
Steve Dower39294992016-08-30 21:22:36 -0700175 PyEval_RestoreThread(_PyOS_ReadlineTState);
Steve Dower39294992016-08-30 21:22:36 -0700176 PyErr_SetFromWindowsErr(err);
Steve Dower39294992016-08-30 21:22:36 -0700177 PyEval_SaveThread();
Steve Dower39294992016-08-30 21:22:36 -0700178 }
179
180 return buf;
181}
182
183#endif
184
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000185
186/* Readline implementation using fgets() */
187
188char *
Serhiy Storchakac6792272013-10-19 21:03:34 +0300189PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000190{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 size_t n;
Christian Heimes9ae513c2013-08-06 15:59:16 +0200192 char *p, *pr;
Victor Stinner2fe9bac2013-10-10 16:18:20 +0200193
Steve Dower39294992016-08-30 21:22:36 -0700194#ifdef MS_WINDOWS
195 if (!Py_LegacyWindowsStdioFlag && sys_stdin == stdin) {
Steve Dower3cd187b2016-10-08 12:18:16 -0700196 HANDLE hStdIn, hStdErr;
Benjamin Petersone2e792d2016-09-19 22:17:16 -0700197
Steve Dower39294992016-08-30 21:22:36 -0700198 _Py_BEGIN_SUPPRESS_IPH
199 hStdIn = (HANDLE)_get_osfhandle(fileno(sys_stdin));
Steve Dower3cd187b2016-10-08 12:18:16 -0700200 hStdErr = (HANDLE)_get_osfhandle(fileno(stderr));
Steve Dower39294992016-08-30 21:22:36 -0700201 _Py_END_SUPPRESS_IPH
Benjamin Petersone2e792d2016-09-19 22:17:16 -0700202
Steve Dower39294992016-08-30 21:22:36 -0700203 if (_get_console_type(hStdIn) == 'r') {
204 fflush(sys_stdout);
Steve Dower3cd187b2016-10-08 12:18:16 -0700205 if (prompt) {
206 if (_get_console_type(hStdErr) == 'w') {
207 wchar_t *wbuf;
208 int wlen;
209 wlen = MultiByteToWideChar(CP_UTF8, 0, prompt, -1,
210 NULL, 0);
Steve Dower59bd34f2016-10-08 12:20:45 -0700211 if (wlen &&
Steve Dower3cd187b2016-10-08 12:18:16 -0700212 (wbuf = PyMem_RawMalloc(wlen * sizeof(wchar_t)))) {
213 wlen = MultiByteToWideChar(CP_UTF8, 0, prompt, -1,
214 wbuf, wlen);
215 if (wlen) {
216 DWORD n;
217 fflush(stderr);
Steve Dower6c2b9d32016-10-25 11:51:54 -0700218 /* wlen includes null terminator, so subtract 1 */
219 WriteConsoleW(hStdErr, wbuf, wlen - 1, &n, NULL);
Steve Dower3cd187b2016-10-08 12:18:16 -0700220 }
221 PyMem_RawFree(wbuf);
222 }
223 } else {
224 fprintf(stderr, "%s", prompt);
225 fflush(stderr);
226 }
227 }
Steve Dower39294992016-08-30 21:22:36 -0700228 clearerr(sys_stdin);
229 return _PyOS_WindowsConsoleReadline(hStdIn);
230 }
231 }
232#endif
233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 n = 100;
Victor Stinner2fe9bac2013-10-10 16:18:20 +0200235 p = (char *)PyMem_RawMalloc(n);
236 if (p == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 return NULL;
Victor Stinner2fe9bac2013-10-10 16:18:20 +0200238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 fflush(sys_stdout);
240 if (prompt)
241 fprintf(stderr, "%s", prompt);
242 fflush(stderr);
Victor Stinner2fe9bac2013-10-10 16:18:20 +0200243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 switch (my_fgets(p, (int)n, sys_stdin)) {
245 case 0: /* Normal case */
246 break;
247 case 1: /* Interrupt */
Victor Stinner2fe9bac2013-10-10 16:18:20 +0200248 PyMem_RawFree(p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 return NULL;
250 case -1: /* EOF */
251 case -2: /* Error */
252 default: /* Shouldn't happen */
253 *p = '\0';
254 break;
255 }
256 n = strlen(p);
257 while (n > 0 && p[n-1] != '\n') {
258 size_t incr = n+2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 if (incr > INT_MAX) {
Victor Stinnerc5486602013-10-19 02:40:16 +0200260 PyMem_RawFree(p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 PyErr_SetString(PyExc_OverflowError, "input line too long");
Christian Heimes9ae513c2013-08-06 15:59:16 +0200262 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 }
Victor Stinner2fe9bac2013-10-10 16:18:20 +0200264 pr = (char *)PyMem_RawRealloc(p, n + incr);
Christian Heimes9ae513c2013-08-06 15:59:16 +0200265 if (pr == NULL) {
Victor Stinnerc5486602013-10-19 02:40:16 +0200266 PyMem_RawFree(p);
Christian Heimes9ae513c2013-08-06 15:59:16 +0200267 PyErr_NoMemory();
268 return NULL;
269 }
270 p = pr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 if (my_fgets(p+n, (int)incr, sys_stdin) != 0)
272 break;
273 n += strlen(p+n);
274 }
Victor Stinner2fe9bac2013-10-10 16:18:20 +0200275 pr = (char *)PyMem_RawRealloc(p, n+1);
Christian Heimes9ae513c2013-08-06 15:59:16 +0200276 if (pr == NULL) {
Victor Stinnerc5486602013-10-19 02:40:16 +0200277 PyMem_RawFree(p);
Christian Heimes9ae513c2013-08-06 15:59:16 +0200278 PyErr_NoMemory();
279 return NULL;
280 }
281 return pr;
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000282}
283
284
285/* By initializing this function pointer, systems embedding Python can
Guido van Rossumb18618d2000-05-03 23:44:39 +0000286 override the readline function.
287
288 Note: Python expects in return a buffer allocated with PyMem_Malloc. */
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000289
Benjamin Peterson0a37a302017-12-31 10:04:13 -0800290char *(*PyOS_ReadlineFunctionPointer)(FILE *, FILE *, const char *) = NULL;
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000291
292
293/* Interface used by tokenizer.c and bltinmodule.c */
294
295char *
Serhiy Storchakac6792272013-10-19 21:03:34 +0300296PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000297{
Victor Stinner2fe9bac2013-10-10 16:18:20 +0200298 char *rv, *res;
299 size_t len;
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 if (_PyOS_ReadlineTState == PyThreadState_GET()) {
302 PyErr_SetString(PyExc_RuntimeError,
303 "can't re-enter readline");
304 return NULL;
305 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307
308 if (PyOS_ReadlineFunctionPointer == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 PyOS_ReadlineFunctionPointer = PyOS_StdioReadline;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 if (_PyOS_ReadlineLock == NULL) {
313 _PyOS_ReadlineLock = PyThread_allocate_lock();
314 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 _PyOS_ReadlineTState = PyThreadState_GET();
317 Py_BEGIN_ALLOW_THREADS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 PyThread_acquire_lock(_PyOS_ReadlineLock, 1);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 /* This is needed to handle the unlikely case that the
321 * interpreter is in interactive mode *and* stdin/out are not
322 * a tty. This can happen, for example if python is run like
323 * this: python -i < test1.py
324 */
325 if (!isatty (fileno (sys_stdin)) || !isatty (fileno (sys_stdout)))
326 rv = PyOS_StdioReadline (sys_stdin, sys_stdout, prompt);
327 else
328 rv = (*PyOS_ReadlineFunctionPointer)(sys_stdin, sys_stdout,
329 prompt);
330 Py_END_ALLOW_THREADS
331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 PyThread_release_lock(_PyOS_ReadlineLock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333
334 _PyOS_ReadlineTState = NULL;
335
Victor Stinner2fe9bac2013-10-10 16:18:20 +0200336 if (rv == NULL)
337 return NULL;
338
339 len = strlen(rv) + 1;
340 res = PyMem_Malloc(len);
341 if (res != NULL)
342 memcpy(res, rv, len);
343 PyMem_RawFree(rv);
344
345 return res;
Guido van Rossum6fa63431993-12-24 10:36:57 +0000346}