Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 2 | /* 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 Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 6 | 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 Rossum | 8efa47b | 1998-08-27 19:43:43 +0000 | [diff] [blame] | 12 | #include "Python.h" |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 13 | #include "internal/pystate.h" |
Mark Hammond | 2f10cb8 | 2002-07-14 23:12:29 +0000 | [diff] [blame] | 14 | #ifdef MS_WINDOWS |
| 15 | #define WIN32_LEAN_AND_MEAN |
| 16 | #include "windows.h" |
| 17 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 18 | |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 19 | |
Benjamin Peterson | 0a37a30 | 2017-12-31 10:04:13 -0800 | [diff] [blame] | 20 | PyThreadState* _PyOS_ReadlineTState = NULL; |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 21 | |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 22 | #include "pythread.h" |
| 23 | static PyThread_type_lock _PyOS_ReadlineLock = NULL; |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 24 | |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 25 | int (*PyOS_InputHook)(void) = NULL; |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 26 | |
| 27 | /* This function restarts a fgets() after an EINTR error occurred |
| 28 | except if PyOS_InterruptOccurred() returns true. */ |
| 29 | |
| 30 | static int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 31 | my_fgets(char *buf, int len, FILE *fp) |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 32 | { |
Tim Golden | 9175c3d | 2012-06-29 18:39:26 +0100 | [diff] [blame] | 33 | #ifdef MS_WINDOWS |
| 34 | HANDLE hInterruptEvent; |
| 35 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 36 | char *p; |
Antoine Pitrou | c345ce1 | 2011-12-16 12:28:32 +0100 | [diff] [blame] | 37 | int err; |
Victor Stinner | 52c950f | 2011-04-09 15:55:44 +0200 | [diff] [blame] | 38 | while (1) { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 39 | if (PyOS_InputHook != NULL) |
| 40 | (void)(PyOS_InputHook)(); |
| 41 | errno = 0; |
Victor Stinner | 4f71101 | 2011-05-30 23:46:00 +0200 | [diff] [blame] | 42 | clearerr(fp); |
Steve Dower | 940f33a | 2016-09-08 11:21:54 -0700 | [diff] [blame] | 43 | p = fgets(buf, len, fp); |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 44 | if (p != NULL) |
| 45 | return 0; /* No error */ |
Antoine Pitrou | c345ce1 | 2011-12-16 12:28:32 +0100 | [diff] [blame] | 46 | err = errno; |
Mark Hammond | 2f10cb8 | 2002-07-14 23:12:29 +0000 | [diff] [blame] | 47 | #ifdef MS_WINDOWS |
Tim Golden | 9175c3d | 2012-06-29 18:39:26 +0100 | [diff] [blame] | 48 | /* 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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 60 | */ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 61 | if (GetLastError()==ERROR_OPERATION_ABORTED) { |
Tim Golden | 9175c3d | 2012-06-29 18:39:26 +0100 | [diff] [blame] | 62 | hInterruptEvent = _PyOS_SigintEvent(); |
Martin v. Löwis | b26a9b1 | 2013-01-25 14:25:48 +0100 | [diff] [blame] | 63 | switch (WaitForSingleObjectEx(hInterruptEvent, 10, FALSE)) { |
Tim Golden | 9175c3d | 2012-06-29 18:39:26 +0100 | [diff] [blame] | 64 | case WAIT_OBJECT_0: |
| 65 | ResetEvent(hInterruptEvent); |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 66 | return 1; /* Interrupt */ |
Tim Golden | 9175c3d | 2012-06-29 18:39:26 +0100 | [diff] [blame] | 67 | case WAIT_FAILED: |
| 68 | return -2; /* Error */ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 69 | } |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 70 | } |
Mark Hammond | 2f10cb8 | 2002-07-14 23:12:29 +0000 | [diff] [blame] | 71 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 72 | if (feof(fp)) { |
Victor Stinner | 4755ab0 | 2011-05-10 00:19:53 +0200 | [diff] [blame] | 73 | clearerr(fp); |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 74 | return -1; /* EOF */ |
| 75 | } |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 76 | #ifdef EINTR |
Antoine Pitrou | c345ce1 | 2011-12-16 12:28:32 +0100 | [diff] [blame] | 77 | if (err == EINTR) { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 78 | int s; |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 79 | PyEval_RestoreThread(_PyOS_ReadlineTState); |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 80 | s = PyErr_CheckSignals(); |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 81 | PyEval_SaveThread(); |
Victor Stinner | 52c950f | 2011-04-09 15:55:44 +0200 | [diff] [blame] | 82 | if (s < 0) |
| 83 | return 1; |
Tim Golden | 9175c3d | 2012-06-29 18:39:26 +0100 | [diff] [blame] | 84 | /* try again */ |
Victor Stinner | 52c950f | 2011-04-09 15:55:44 +0200 | [diff] [blame] | 85 | continue; |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 86 | } |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 87 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 88 | if (PyOS_InterruptOccurred()) { |
| 89 | return 1; /* Interrupt */ |
| 90 | } |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 91 | return -2; /* Error */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 92 | } |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 93 | /* NOTREACHED */ |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 96 | #ifdef MS_WINDOWS |
| 97 | /* Readline implementation using ReadConsoleW */ |
| 98 | |
| 99 | extern char _get_console_type(HANDLE handle); |
| 100 | |
| 101 | char * |
| 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 | |
ValeriyaSinevich | ce75df3 | 2018-07-19 15:34:03 -0700 | [diff] [blame] | 112 | n_read = (DWORD)-1; |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 113 | total_read = 0; |
| 114 | wbuf = wbuf_local; |
| 115 | wbuflen = sizeof(wbuf_local) / sizeof(wbuf_local[0]) - 1; |
| 116 | while (1) { |
Thomas A Caswell | 9b9d58f | 2018-06-28 12:29:44 -0400 | [diff] [blame] | 117 | if (PyOS_InputHook != NULL) { |
| 118 | (void)(PyOS_InputHook)(); |
| 119 | } |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 120 | if (!ReadConsoleW(hStdIn, &wbuf[total_read], wbuflen - total_read, &n_read, NULL)) { |
| 121 | err = GetLastError(); |
| 122 | goto exit; |
| 123 | } |
ValeriyaSinevich | ce75df3 | 2018-07-19 15:34:03 -0700 | [diff] [blame] | 124 | if (n_read == (DWORD)-1 && (err = GetLastError()) == ERROR_OPERATION_ABORTED) { |
| 125 | break; |
| 126 | } |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 127 | 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 Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 137 | PyEval_RestoreThread(_PyOS_ReadlineTState); |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 138 | s = PyErr_CheckSignals(); |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 139 | PyEval_SaveThread(); |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 140 | 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); |
| 156 | } |
| 157 | else |
| 158 | wbuf = (wchar_t*)PyMem_RawRealloc(wbuf, wbuflen * sizeof(wchar_t)); |
| 159 | } |
| 160 | |
| 161 | if (wbuf[0] == '\x1a') { |
| 162 | buf = PyMem_RawMalloc(1); |
| 163 | if (buf) |
| 164 | buf[0] = '\0'; |
| 165 | goto exit; |
| 166 | } |
| 167 | |
| 168 | u8len = WideCharToMultiByte(CP_UTF8, 0, wbuf, total_read, NULL, 0, NULL, NULL); |
| 169 | buf = PyMem_RawMalloc(u8len + 1); |
| 170 | u8len = WideCharToMultiByte(CP_UTF8, 0, wbuf, total_read, buf, u8len, NULL, NULL); |
| 171 | buf[u8len] = '\0'; |
Benjamin Peterson | e2e792d | 2016-09-19 22:17:16 -0700 | [diff] [blame] | 172 | |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 173 | exit: |
| 174 | if (wbuf != wbuf_local) |
| 175 | PyMem_RawFree(wbuf); |
| 176 | |
| 177 | if (err) { |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 178 | PyEval_RestoreThread(_PyOS_ReadlineTState); |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 179 | PyErr_SetFromWindowsErr(err); |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 180 | PyEval_SaveThread(); |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | return buf; |
| 184 | } |
| 185 | |
| 186 | #endif |
| 187 | |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 188 | |
| 189 | /* Readline implementation using fgets() */ |
| 190 | |
| 191 | char * |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 192 | PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt) |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 193 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 194 | size_t n; |
Christian Heimes | 9ae513c | 2013-08-06 15:59:16 +0200 | [diff] [blame] | 195 | char *p, *pr; |
Victor Stinner | 2fe9bac | 2013-10-10 16:18:20 +0200 | [diff] [blame] | 196 | |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 197 | #ifdef MS_WINDOWS |
| 198 | if (!Py_LegacyWindowsStdioFlag && sys_stdin == stdin) { |
Steve Dower | 3cd187b | 2016-10-08 12:18:16 -0700 | [diff] [blame] | 199 | HANDLE hStdIn, hStdErr; |
Benjamin Peterson | e2e792d | 2016-09-19 22:17:16 -0700 | [diff] [blame] | 200 | |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 201 | _Py_BEGIN_SUPPRESS_IPH |
| 202 | hStdIn = (HANDLE)_get_osfhandle(fileno(sys_stdin)); |
Steve Dower | 3cd187b | 2016-10-08 12:18:16 -0700 | [diff] [blame] | 203 | hStdErr = (HANDLE)_get_osfhandle(fileno(stderr)); |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 204 | _Py_END_SUPPRESS_IPH |
Benjamin Peterson | e2e792d | 2016-09-19 22:17:16 -0700 | [diff] [blame] | 205 | |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 206 | if (_get_console_type(hStdIn) == 'r') { |
| 207 | fflush(sys_stdout); |
Steve Dower | 3cd187b | 2016-10-08 12:18:16 -0700 | [diff] [blame] | 208 | if (prompt) { |
| 209 | if (_get_console_type(hStdErr) == 'w') { |
| 210 | wchar_t *wbuf; |
| 211 | int wlen; |
| 212 | wlen = MultiByteToWideChar(CP_UTF8, 0, prompt, -1, |
| 213 | NULL, 0); |
Steve Dower | 59bd34f | 2016-10-08 12:20:45 -0700 | [diff] [blame] | 214 | if (wlen && |
Steve Dower | 3cd187b | 2016-10-08 12:18:16 -0700 | [diff] [blame] | 215 | (wbuf = PyMem_RawMalloc(wlen * sizeof(wchar_t)))) { |
| 216 | wlen = MultiByteToWideChar(CP_UTF8, 0, prompt, -1, |
| 217 | wbuf, wlen); |
| 218 | if (wlen) { |
| 219 | DWORD n; |
| 220 | fflush(stderr); |
Steve Dower | 6c2b9d3 | 2016-10-25 11:51:54 -0700 | [diff] [blame] | 221 | /* wlen includes null terminator, so subtract 1 */ |
| 222 | WriteConsoleW(hStdErr, wbuf, wlen - 1, &n, NULL); |
Steve Dower | 3cd187b | 2016-10-08 12:18:16 -0700 | [diff] [blame] | 223 | } |
| 224 | PyMem_RawFree(wbuf); |
| 225 | } |
| 226 | } else { |
| 227 | fprintf(stderr, "%s", prompt); |
| 228 | fflush(stderr); |
| 229 | } |
| 230 | } |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 231 | clearerr(sys_stdin); |
| 232 | return _PyOS_WindowsConsoleReadline(hStdIn); |
| 233 | } |
| 234 | } |
| 235 | #endif |
| 236 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 237 | n = 100; |
Victor Stinner | 2fe9bac | 2013-10-10 16:18:20 +0200 | [diff] [blame] | 238 | p = (char *)PyMem_RawMalloc(n); |
| 239 | if (p == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 240 | return NULL; |
Victor Stinner | 2fe9bac | 2013-10-10 16:18:20 +0200 | [diff] [blame] | 241 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 242 | fflush(sys_stdout); |
| 243 | if (prompt) |
| 244 | fprintf(stderr, "%s", prompt); |
| 245 | fflush(stderr); |
Victor Stinner | 2fe9bac | 2013-10-10 16:18:20 +0200 | [diff] [blame] | 246 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 247 | switch (my_fgets(p, (int)n, sys_stdin)) { |
| 248 | case 0: /* Normal case */ |
| 249 | break; |
| 250 | case 1: /* Interrupt */ |
Victor Stinner | 2fe9bac | 2013-10-10 16:18:20 +0200 | [diff] [blame] | 251 | PyMem_RawFree(p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 252 | return NULL; |
| 253 | case -1: /* EOF */ |
| 254 | case -2: /* Error */ |
| 255 | default: /* Shouldn't happen */ |
| 256 | *p = '\0'; |
| 257 | break; |
| 258 | } |
| 259 | n = strlen(p); |
| 260 | while (n > 0 && p[n-1] != '\n') { |
| 261 | size_t incr = n+2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 262 | if (incr > INT_MAX) { |
Victor Stinner | c548660 | 2013-10-19 02:40:16 +0200 | [diff] [blame] | 263 | PyMem_RawFree(p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 264 | PyErr_SetString(PyExc_OverflowError, "input line too long"); |
Christian Heimes | 9ae513c | 2013-08-06 15:59:16 +0200 | [diff] [blame] | 265 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 266 | } |
Victor Stinner | 2fe9bac | 2013-10-10 16:18:20 +0200 | [diff] [blame] | 267 | pr = (char *)PyMem_RawRealloc(p, n + incr); |
Christian Heimes | 9ae513c | 2013-08-06 15:59:16 +0200 | [diff] [blame] | 268 | if (pr == NULL) { |
Victor Stinner | c548660 | 2013-10-19 02:40:16 +0200 | [diff] [blame] | 269 | PyMem_RawFree(p); |
Christian Heimes | 9ae513c | 2013-08-06 15:59:16 +0200 | [diff] [blame] | 270 | PyErr_NoMemory(); |
| 271 | return NULL; |
| 272 | } |
| 273 | p = pr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 274 | if (my_fgets(p+n, (int)incr, sys_stdin) != 0) |
| 275 | break; |
| 276 | n += strlen(p+n); |
| 277 | } |
Victor Stinner | 2fe9bac | 2013-10-10 16:18:20 +0200 | [diff] [blame] | 278 | pr = (char *)PyMem_RawRealloc(p, n+1); |
Christian Heimes | 9ae513c | 2013-08-06 15:59:16 +0200 | [diff] [blame] | 279 | if (pr == NULL) { |
Victor Stinner | c548660 | 2013-10-19 02:40:16 +0200 | [diff] [blame] | 280 | PyMem_RawFree(p); |
Christian Heimes | 9ae513c | 2013-08-06 15:59:16 +0200 | [diff] [blame] | 281 | PyErr_NoMemory(); |
| 282 | return NULL; |
| 283 | } |
| 284 | return pr; |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | |
| 288 | /* By initializing this function pointer, systems embedding Python can |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 289 | override the readline function. |
| 290 | |
| 291 | Note: Python expects in return a buffer allocated with PyMem_Malloc. */ |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 292 | |
Benjamin Peterson | 0a37a30 | 2017-12-31 10:04:13 -0800 | [diff] [blame] | 293 | char *(*PyOS_ReadlineFunctionPointer)(FILE *, FILE *, const char *) = NULL; |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 294 | |
| 295 | |
| 296 | /* Interface used by tokenizer.c and bltinmodule.c */ |
| 297 | |
| 298 | char * |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 299 | PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt) |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 300 | { |
Victor Stinner | 2fe9bac | 2013-10-10 16:18:20 +0200 | [diff] [blame] | 301 | char *rv, *res; |
| 302 | size_t len; |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 303 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 304 | if (_PyOS_ReadlineTState == PyThreadState_GET()) { |
| 305 | PyErr_SetString(PyExc_RuntimeError, |
| 306 | "can't re-enter readline"); |
| 307 | return NULL; |
| 308 | } |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 309 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 310 | |
| 311 | if (PyOS_ReadlineFunctionPointer == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 312 | PyOS_ReadlineFunctionPointer = PyOS_StdioReadline; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 313 | } |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 314 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 315 | if (_PyOS_ReadlineLock == NULL) { |
| 316 | _PyOS_ReadlineLock = PyThread_allocate_lock(); |
| 317 | } |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 318 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 319 | _PyOS_ReadlineTState = PyThreadState_GET(); |
| 320 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 321 | PyThread_acquire_lock(_PyOS_ReadlineLock, 1); |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 322 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 323 | /* This is needed to handle the unlikely case that the |
| 324 | * interpreter is in interactive mode *and* stdin/out are not |
| 325 | * a tty. This can happen, for example if python is run like |
| 326 | * this: python -i < test1.py |
| 327 | */ |
| 328 | if (!isatty (fileno (sys_stdin)) || !isatty (fileno (sys_stdout))) |
| 329 | rv = PyOS_StdioReadline (sys_stdin, sys_stdout, prompt); |
| 330 | else |
| 331 | rv = (*PyOS_ReadlineFunctionPointer)(sys_stdin, sys_stdout, |
| 332 | prompt); |
| 333 | Py_END_ALLOW_THREADS |
| 334 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 335 | PyThread_release_lock(_PyOS_ReadlineLock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 336 | |
| 337 | _PyOS_ReadlineTState = NULL; |
| 338 | |
Victor Stinner | 2fe9bac | 2013-10-10 16:18:20 +0200 | [diff] [blame] | 339 | if (rv == NULL) |
| 340 | return NULL; |
| 341 | |
| 342 | len = strlen(rv) + 1; |
| 343 | res = PyMem_Malloc(len); |
| 344 | if (res != NULL) |
| 345 | memcpy(res, rv, len); |
| 346 | PyMem_RawFree(rv); |
| 347 | |
| 348 | return res; |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 349 | } |