blob: 43e5583b8bcc431818f8ca64e566a7314a67aa37 [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"
Victor Stinner621cebe2018-11-12 16:53:38 +010013#include "pycore_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
ValeriyaSinevichce75df32018-07-19 15:34:03 -0700112 n_read = (DWORD)-1;
Steve Dower39294992016-08-30 21:22:36 -0700113 total_read = 0;
114 wbuf = wbuf_local;
115 wbuflen = sizeof(wbuf_local) / sizeof(wbuf_local[0]) - 1;
116 while (1) {
Thomas A Caswell9b9d58f2018-06-28 12:29:44 -0400117 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 }
ValeriyaSinevichce75df32018-07-19 15:34:03 -0700124 if (n_read == (DWORD)-1 && (err = GetLastError()) == ERROR_OPERATION_ABORTED) {
125 break;
126 }
Steve Dower39294992016-08-30 21:22:36 -0700127 if (n_read == 0) {
128 int s;
129 err = GetLastError();
130 if (err != ERROR_OPERATION_ABORTED)
131 goto exit;
132 err = 0;
133 HANDLE hInterruptEvent = _PyOS_SigintEvent();
134 if (WaitForSingleObjectEx(hInterruptEvent, 100, FALSE)
135 == WAIT_OBJECT_0) {
136 ResetEvent(hInterruptEvent);
Steve Dower39294992016-08-30 21:22:36 -0700137 PyEval_RestoreThread(_PyOS_ReadlineTState);
Steve Dower39294992016-08-30 21:22:36 -0700138 s = PyErr_CheckSignals();
Steve Dower39294992016-08-30 21:22:36 -0700139 PyEval_SaveThread();
Steve Dower39294992016-08-30 21:22:36 -0700140 if (s < 0)
141 goto exit;
142 }
143 break;
144 }
145
146 total_read += n_read;
147 if (total_read == 0 || wbuf[total_read - 1] == L'\n') {
148 break;
149 }
150 wbuflen += chunk_size;
151 if (wbuf == wbuf_local) {
152 wbuf[total_read] = '\0';
153 wbuf = (wchar_t*)PyMem_RawMalloc(wbuflen * sizeof(wchar_t));
154 if (wbuf)
155 wcscpy_s(wbuf, wbuflen, wbuf_local);
Zackery Spytz4c49da02018-12-07 03:11:30 -0700156 else {
157 PyErr_NoMemory();
158 goto exit;
159 }
Steve Dower39294992016-08-30 21:22:36 -0700160 }
Zackery Spytz4c49da02018-12-07 03:11:30 -0700161 else {
162 wchar_t *tmp = PyMem_RawRealloc(wbuf, wbuflen * sizeof(wchar_t));
163 if (tmp == NULL) {
164 PyErr_NoMemory();
165 goto exit;
166 }
167 wbuf = tmp;
168 }
Steve Dower39294992016-08-30 21:22:36 -0700169 }
170
171 if (wbuf[0] == '\x1a') {
172 buf = PyMem_RawMalloc(1);
173 if (buf)
174 buf[0] = '\0';
Zackery Spytz4c49da02018-12-07 03:11:30 -0700175 else {
176 PyErr_NoMemory();
177 }
Steve Dower39294992016-08-30 21:22:36 -0700178 goto exit;
179 }
180
181 u8len = WideCharToMultiByte(CP_UTF8, 0, wbuf, total_read, NULL, 0, NULL, NULL);
182 buf = PyMem_RawMalloc(u8len + 1);
Zackery Spytz4c49da02018-12-07 03:11:30 -0700183 if (buf == NULL) {
184 PyErr_NoMemory();
185 goto exit;
186 }
Steve Dower39294992016-08-30 21:22:36 -0700187 u8len = WideCharToMultiByte(CP_UTF8, 0, wbuf, total_read, buf, u8len, NULL, NULL);
188 buf[u8len] = '\0';
Benjamin Petersone2e792d2016-09-19 22:17:16 -0700189
Steve Dower39294992016-08-30 21:22:36 -0700190exit:
191 if (wbuf != wbuf_local)
192 PyMem_RawFree(wbuf);
193
194 if (err) {
Steve Dower39294992016-08-30 21:22:36 -0700195 PyEval_RestoreThread(_PyOS_ReadlineTState);
Steve Dower39294992016-08-30 21:22:36 -0700196 PyErr_SetFromWindowsErr(err);
Steve Dower39294992016-08-30 21:22:36 -0700197 PyEval_SaveThread();
Steve Dower39294992016-08-30 21:22:36 -0700198 }
199
200 return buf;
201}
202
203#endif
204
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000205
206/* Readline implementation using fgets() */
207
208char *
Serhiy Storchakac6792272013-10-19 21:03:34 +0300209PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000210{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 size_t n;
Christian Heimes9ae513c2013-08-06 15:59:16 +0200212 char *p, *pr;
Victor Stinner2fe9bac2013-10-10 16:18:20 +0200213
Steve Dower39294992016-08-30 21:22:36 -0700214#ifdef MS_WINDOWS
215 if (!Py_LegacyWindowsStdioFlag && sys_stdin == stdin) {
Steve Dower3cd187b2016-10-08 12:18:16 -0700216 HANDLE hStdIn, hStdErr;
Benjamin Petersone2e792d2016-09-19 22:17:16 -0700217
Steve Dower39294992016-08-30 21:22:36 -0700218 _Py_BEGIN_SUPPRESS_IPH
219 hStdIn = (HANDLE)_get_osfhandle(fileno(sys_stdin));
Steve Dower3cd187b2016-10-08 12:18:16 -0700220 hStdErr = (HANDLE)_get_osfhandle(fileno(stderr));
Steve Dower39294992016-08-30 21:22:36 -0700221 _Py_END_SUPPRESS_IPH
Benjamin Petersone2e792d2016-09-19 22:17:16 -0700222
Steve Dower39294992016-08-30 21:22:36 -0700223 if (_get_console_type(hStdIn) == 'r') {
224 fflush(sys_stdout);
Steve Dower3cd187b2016-10-08 12:18:16 -0700225 if (prompt) {
226 if (_get_console_type(hStdErr) == 'w') {
227 wchar_t *wbuf;
228 int wlen;
229 wlen = MultiByteToWideChar(CP_UTF8, 0, prompt, -1,
230 NULL, 0);
Zackery Spytz4c49da02018-12-07 03:11:30 -0700231 if (wlen) {
232 wbuf = PyMem_RawMalloc(wlen * sizeof(wchar_t));
233 if (wbuf == NULL) {
234 PyErr_NoMemory();
235 return NULL;
236 }
Steve Dower3cd187b2016-10-08 12:18:16 -0700237 wlen = MultiByteToWideChar(CP_UTF8, 0, prompt, -1,
238 wbuf, wlen);
239 if (wlen) {
240 DWORD n;
241 fflush(stderr);
Steve Dower6c2b9d32016-10-25 11:51:54 -0700242 /* wlen includes null terminator, so subtract 1 */
243 WriteConsoleW(hStdErr, wbuf, wlen - 1, &n, NULL);
Steve Dower3cd187b2016-10-08 12:18:16 -0700244 }
245 PyMem_RawFree(wbuf);
246 }
247 } else {
248 fprintf(stderr, "%s", prompt);
249 fflush(stderr);
250 }
251 }
Steve Dower39294992016-08-30 21:22:36 -0700252 clearerr(sys_stdin);
253 return _PyOS_WindowsConsoleReadline(hStdIn);
254 }
255 }
256#endif
257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 n = 100;
Victor Stinner2fe9bac2013-10-10 16:18:20 +0200259 p = (char *)PyMem_RawMalloc(n);
Zackery Spytz4c49da02018-12-07 03:11:30 -0700260 if (p == NULL) {
261 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 return NULL;
Zackery Spytz4c49da02018-12-07 03:11:30 -0700263 }
Victor Stinner2fe9bac2013-10-10 16:18:20 +0200264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 fflush(sys_stdout);
266 if (prompt)
267 fprintf(stderr, "%s", prompt);
268 fflush(stderr);
Victor Stinner2fe9bac2013-10-10 16:18:20 +0200269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 switch (my_fgets(p, (int)n, sys_stdin)) {
271 case 0: /* Normal case */
272 break;
273 case 1: /* Interrupt */
Victor Stinner2fe9bac2013-10-10 16:18:20 +0200274 PyMem_RawFree(p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 return NULL;
276 case -1: /* EOF */
277 case -2: /* Error */
278 default: /* Shouldn't happen */
279 *p = '\0';
280 break;
281 }
282 n = strlen(p);
283 while (n > 0 && p[n-1] != '\n') {
284 size_t incr = n+2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 if (incr > INT_MAX) {
Victor Stinnerc5486602013-10-19 02:40:16 +0200286 PyMem_RawFree(p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 PyErr_SetString(PyExc_OverflowError, "input line too long");
Christian Heimes9ae513c2013-08-06 15:59:16 +0200288 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 }
Victor Stinner2fe9bac2013-10-10 16:18:20 +0200290 pr = (char *)PyMem_RawRealloc(p, n + incr);
Christian Heimes9ae513c2013-08-06 15:59:16 +0200291 if (pr == NULL) {
Victor Stinnerc5486602013-10-19 02:40:16 +0200292 PyMem_RawFree(p);
Christian Heimes9ae513c2013-08-06 15:59:16 +0200293 PyErr_NoMemory();
294 return NULL;
295 }
296 p = pr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 if (my_fgets(p+n, (int)incr, sys_stdin) != 0)
298 break;
299 n += strlen(p+n);
300 }
Victor Stinner2fe9bac2013-10-10 16:18:20 +0200301 pr = (char *)PyMem_RawRealloc(p, n+1);
Christian Heimes9ae513c2013-08-06 15:59:16 +0200302 if (pr == NULL) {
Victor Stinnerc5486602013-10-19 02:40:16 +0200303 PyMem_RawFree(p);
Christian Heimes9ae513c2013-08-06 15:59:16 +0200304 PyErr_NoMemory();
305 return NULL;
306 }
307 return pr;
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000308}
309
310
311/* By initializing this function pointer, systems embedding Python can
Guido van Rossumb18618d2000-05-03 23:44:39 +0000312 override the readline function.
313
314 Note: Python expects in return a buffer allocated with PyMem_Malloc. */
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000315
Benjamin Peterson0a37a302017-12-31 10:04:13 -0800316char *(*PyOS_ReadlineFunctionPointer)(FILE *, FILE *, const char *) = NULL;
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000317
318
319/* Interface used by tokenizer.c and bltinmodule.c */
320
321char *
Serhiy Storchakac6792272013-10-19 21:03:34 +0300322PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000323{
Victor Stinner2fe9bac2013-10-10 16:18:20 +0200324 char *rv, *res;
325 size_t len;
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000326
Victor Stinner50b48572018-11-01 01:51:40 +0100327 if (_PyOS_ReadlineTState == _PyThreadState_GET()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 PyErr_SetString(PyExc_RuntimeError,
329 "can't re-enter readline");
330 return NULL;
331 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333
334 if (PyOS_ReadlineFunctionPointer == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 PyOS_ReadlineFunctionPointer = PyOS_StdioReadline;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 if (_PyOS_ReadlineLock == NULL) {
339 _PyOS_ReadlineLock = PyThread_allocate_lock();
Zackery Spytz4c49da02018-12-07 03:11:30 -0700340 if (_PyOS_ReadlineLock == NULL) {
341 PyErr_SetString(PyExc_MemoryError, "can't allocate lock");
342 return NULL;
343 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000345
Victor Stinner50b48572018-11-01 01:51:40 +0100346 _PyOS_ReadlineTState = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 Py_BEGIN_ALLOW_THREADS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 PyThread_acquire_lock(_PyOS_ReadlineLock, 1);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 /* This is needed to handle the unlikely case that the
351 * interpreter is in interactive mode *and* stdin/out are not
352 * a tty. This can happen, for example if python is run like
353 * this: python -i < test1.py
354 */
355 if (!isatty (fileno (sys_stdin)) || !isatty (fileno (sys_stdout)))
356 rv = PyOS_StdioReadline (sys_stdin, sys_stdout, prompt);
357 else
358 rv = (*PyOS_ReadlineFunctionPointer)(sys_stdin, sys_stdout,
359 prompt);
360 Py_END_ALLOW_THREADS
361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 PyThread_release_lock(_PyOS_ReadlineLock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363
364 _PyOS_ReadlineTState = NULL;
365
Victor Stinner2fe9bac2013-10-10 16:18:20 +0200366 if (rv == NULL)
367 return NULL;
368
369 len = strlen(rv) + 1;
370 res = PyMem_Malloc(len);
Zackery Spytz4c49da02018-12-07 03:11:30 -0700371 if (res != NULL) {
Victor Stinner2fe9bac2013-10-10 16:18:20 +0200372 memcpy(res, rv, len);
Zackery Spytz4c49da02018-12-07 03:11:30 -0700373 }
374 else {
375 PyErr_NoMemory();
376 }
Victor Stinner2fe9bac2013-10-10 16:18:20 +0200377 PyMem_RawFree(rv);
378
379 return res;
Guido van Rossum6fa63431993-12-24 10:36:57 +0000380}