| 1 | n/a | |
|---|
| 2 | n/a | /* Readline interface for tokenizer.c and [raw_]input() in bltinmodule.c. |
|---|
| 3 | n/a | By default, or when stdin is not a tty device, we have a super |
|---|
| 4 | n/a | simple my_readline function using fgets. |
|---|
| 5 | n/a | Optionally, we can use the GNU readline library. |
|---|
| 6 | n/a | my_readline() has a different return value from GNU readline(): |
|---|
| 7 | n/a | - NULL if an interrupt occurred or if an error occurred |
|---|
| 8 | n/a | - a malloc'ed empty string if EOF was read |
|---|
| 9 | n/a | - a malloc'ed string ending in \n normally |
|---|
| 10 | n/a | */ |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | #include "Python.h" |
|---|
| 13 | n/a | #ifdef MS_WINDOWS |
|---|
| 14 | n/a | #define WIN32_LEAN_AND_MEAN |
|---|
| 15 | n/a | #include "windows.h" |
|---|
| 16 | n/a | #endif /* MS_WINDOWS */ |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | PyThreadState* _PyOS_ReadlineTState; |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | #ifdef WITH_THREAD |
|---|
| 22 | n/a | #include "pythread.h" |
|---|
| 23 | n/a | static PyThread_type_lock _PyOS_ReadlineLock = NULL; |
|---|
| 24 | n/a | #endif |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | int (*PyOS_InputHook)(void) = NULL; |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | /* This function restarts a fgets() after an EINTR error occurred |
|---|
| 29 | n/a | except if PyOS_InterruptOccurred() returns true. */ |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | static int |
|---|
| 32 | n/a | my_fgets(char *buf, int len, FILE *fp) |
|---|
| 33 | n/a | { |
|---|
| 34 | n/a | #ifdef MS_WINDOWS |
|---|
| 35 | n/a | HANDLE hInterruptEvent; |
|---|
| 36 | n/a | #endif |
|---|
| 37 | n/a | char *p; |
|---|
| 38 | n/a | int err; |
|---|
| 39 | n/a | while (1) { |
|---|
| 40 | n/a | if (PyOS_InputHook != NULL) |
|---|
| 41 | n/a | (void)(PyOS_InputHook)(); |
|---|
| 42 | n/a | errno = 0; |
|---|
| 43 | n/a | clearerr(fp); |
|---|
| 44 | n/a | p = fgets(buf, len, fp); |
|---|
| 45 | n/a | if (p != NULL) |
|---|
| 46 | n/a | return 0; /* No error */ |
|---|
| 47 | n/a | err = errno; |
|---|
| 48 | n/a | #ifdef MS_WINDOWS |
|---|
| 49 | n/a | /* Ctrl-C anywhere on the line or Ctrl-Z if the only character |
|---|
| 50 | n/a | on a line will set ERROR_OPERATION_ABORTED. Under normal |
|---|
| 51 | n/a | circumstances Ctrl-C will also have caused the SIGINT handler |
|---|
| 52 | n/a | to fire which will have set the event object returned by |
|---|
| 53 | n/a | _PyOS_SigintEvent. This signal fires in another thread and |
|---|
| 54 | n/a | is not guaranteed to have occurred before this point in the |
|---|
| 55 | n/a | code. |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | Therefore: check whether the event is set with a small timeout. |
|---|
| 58 | n/a | If it is, assume this is a Ctrl-C and reset the event. If it |
|---|
| 59 | n/a | isn't set assume that this is a Ctrl-Z on its own and drop |
|---|
| 60 | n/a | through to check for EOF. |
|---|
| 61 | n/a | */ |
|---|
| 62 | n/a | if (GetLastError()==ERROR_OPERATION_ABORTED) { |
|---|
| 63 | n/a | hInterruptEvent = _PyOS_SigintEvent(); |
|---|
| 64 | n/a | switch (WaitForSingleObjectEx(hInterruptEvent, 10, FALSE)) { |
|---|
| 65 | n/a | case WAIT_OBJECT_0: |
|---|
| 66 | n/a | ResetEvent(hInterruptEvent); |
|---|
| 67 | n/a | return 1; /* Interrupt */ |
|---|
| 68 | n/a | case WAIT_FAILED: |
|---|
| 69 | n/a | return -2; /* Error */ |
|---|
| 70 | n/a | } |
|---|
| 71 | n/a | } |
|---|
| 72 | n/a | #endif /* MS_WINDOWS */ |
|---|
| 73 | n/a | if (feof(fp)) { |
|---|
| 74 | n/a | clearerr(fp); |
|---|
| 75 | n/a | return -1; /* EOF */ |
|---|
| 76 | n/a | } |
|---|
| 77 | n/a | #ifdef EINTR |
|---|
| 78 | n/a | if (err == EINTR) { |
|---|
| 79 | n/a | int s; |
|---|
| 80 | n/a | #ifdef WITH_THREAD |
|---|
| 81 | n/a | PyEval_RestoreThread(_PyOS_ReadlineTState); |
|---|
| 82 | n/a | #endif |
|---|
| 83 | n/a | s = PyErr_CheckSignals(); |
|---|
| 84 | n/a | #ifdef WITH_THREAD |
|---|
| 85 | n/a | PyEval_SaveThread(); |
|---|
| 86 | n/a | #endif |
|---|
| 87 | n/a | if (s < 0) |
|---|
| 88 | n/a | return 1; |
|---|
| 89 | n/a | /* try again */ |
|---|
| 90 | n/a | continue; |
|---|
| 91 | n/a | } |
|---|
| 92 | n/a | #endif |
|---|
| 93 | n/a | if (PyOS_InterruptOccurred()) { |
|---|
| 94 | n/a | return 1; /* Interrupt */ |
|---|
| 95 | n/a | } |
|---|
| 96 | n/a | return -2; /* Error */ |
|---|
| 97 | n/a | } |
|---|
| 98 | n/a | /* NOTREACHED */ |
|---|
| 99 | n/a | } |
|---|
| 100 | n/a | |
|---|
| 101 | n/a | #ifdef MS_WINDOWS |
|---|
| 102 | n/a | /* Readline implementation using ReadConsoleW */ |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | extern char _get_console_type(HANDLE handle); |
|---|
| 105 | n/a | |
|---|
| 106 | n/a | char * |
|---|
| 107 | n/a | _PyOS_WindowsConsoleReadline(HANDLE hStdIn) |
|---|
| 108 | n/a | { |
|---|
| 109 | n/a | static wchar_t wbuf_local[1024 * 16]; |
|---|
| 110 | n/a | const DWORD chunk_size = 1024; |
|---|
| 111 | n/a | |
|---|
| 112 | n/a | DWORD n_read, total_read, wbuflen, u8len; |
|---|
| 113 | n/a | wchar_t *wbuf; |
|---|
| 114 | n/a | char *buf = NULL; |
|---|
| 115 | n/a | int err = 0; |
|---|
| 116 | n/a | |
|---|
| 117 | n/a | n_read = 0; |
|---|
| 118 | n/a | total_read = 0; |
|---|
| 119 | n/a | wbuf = wbuf_local; |
|---|
| 120 | n/a | wbuflen = sizeof(wbuf_local) / sizeof(wbuf_local[0]) - 1; |
|---|
| 121 | n/a | while (1) { |
|---|
| 122 | n/a | if (!ReadConsoleW(hStdIn, &wbuf[total_read], wbuflen - total_read, &n_read, NULL)) { |
|---|
| 123 | n/a | err = GetLastError(); |
|---|
| 124 | n/a | goto exit; |
|---|
| 125 | n/a | } |
|---|
| 126 | n/a | if (n_read == 0) { |
|---|
| 127 | n/a | int s; |
|---|
| 128 | n/a | err = GetLastError(); |
|---|
| 129 | n/a | if (err != ERROR_OPERATION_ABORTED) |
|---|
| 130 | n/a | goto exit; |
|---|
| 131 | n/a | err = 0; |
|---|
| 132 | n/a | HANDLE hInterruptEvent = _PyOS_SigintEvent(); |
|---|
| 133 | n/a | if (WaitForSingleObjectEx(hInterruptEvent, 100, FALSE) |
|---|
| 134 | n/a | == WAIT_OBJECT_0) { |
|---|
| 135 | n/a | ResetEvent(hInterruptEvent); |
|---|
| 136 | n/a | #ifdef WITH_THREAD |
|---|
| 137 | n/a | PyEval_RestoreThread(_PyOS_ReadlineTState); |
|---|
| 138 | n/a | #endif |
|---|
| 139 | n/a | s = PyErr_CheckSignals(); |
|---|
| 140 | n/a | #ifdef WITH_THREAD |
|---|
| 141 | n/a | PyEval_SaveThread(); |
|---|
| 142 | n/a | #endif |
|---|
| 143 | n/a | if (s < 0) |
|---|
| 144 | n/a | goto exit; |
|---|
| 145 | n/a | } |
|---|
| 146 | n/a | break; |
|---|
| 147 | n/a | } |
|---|
| 148 | n/a | |
|---|
| 149 | n/a | total_read += n_read; |
|---|
| 150 | n/a | if (total_read == 0 || wbuf[total_read - 1] == L'\n') { |
|---|
| 151 | n/a | break; |
|---|
| 152 | n/a | } |
|---|
| 153 | n/a | wbuflen += chunk_size; |
|---|
| 154 | n/a | if (wbuf == wbuf_local) { |
|---|
| 155 | n/a | wbuf[total_read] = '\0'; |
|---|
| 156 | n/a | wbuf = (wchar_t*)PyMem_RawMalloc(wbuflen * sizeof(wchar_t)); |
|---|
| 157 | n/a | if (wbuf) |
|---|
| 158 | n/a | wcscpy_s(wbuf, wbuflen, wbuf_local); |
|---|
| 159 | n/a | } |
|---|
| 160 | n/a | else |
|---|
| 161 | n/a | wbuf = (wchar_t*)PyMem_RawRealloc(wbuf, wbuflen * sizeof(wchar_t)); |
|---|
| 162 | n/a | } |
|---|
| 163 | n/a | |
|---|
| 164 | n/a | if (wbuf[0] == '\x1a') { |
|---|
| 165 | n/a | buf = PyMem_RawMalloc(1); |
|---|
| 166 | n/a | if (buf) |
|---|
| 167 | n/a | buf[0] = '\0'; |
|---|
| 168 | n/a | goto exit; |
|---|
| 169 | n/a | } |
|---|
| 170 | n/a | |
|---|
| 171 | n/a | u8len = WideCharToMultiByte(CP_UTF8, 0, wbuf, total_read, NULL, 0, NULL, NULL); |
|---|
| 172 | n/a | buf = PyMem_RawMalloc(u8len + 1); |
|---|
| 173 | n/a | u8len = WideCharToMultiByte(CP_UTF8, 0, wbuf, total_read, buf, u8len, NULL, NULL); |
|---|
| 174 | n/a | buf[u8len] = '\0'; |
|---|
| 175 | n/a | |
|---|
| 176 | n/a | exit: |
|---|
| 177 | n/a | if (wbuf != wbuf_local) |
|---|
| 178 | n/a | PyMem_RawFree(wbuf); |
|---|
| 179 | n/a | |
|---|
| 180 | n/a | if (err) { |
|---|
| 181 | n/a | #ifdef WITH_THREAD |
|---|
| 182 | n/a | PyEval_RestoreThread(_PyOS_ReadlineTState); |
|---|
| 183 | n/a | #endif |
|---|
| 184 | n/a | PyErr_SetFromWindowsErr(err); |
|---|
| 185 | n/a | #ifdef WITH_THREAD |
|---|
| 186 | n/a | PyEval_SaveThread(); |
|---|
| 187 | n/a | #endif |
|---|
| 188 | n/a | } |
|---|
| 189 | n/a | |
|---|
| 190 | n/a | return buf; |
|---|
| 191 | n/a | } |
|---|
| 192 | n/a | |
|---|
| 193 | n/a | #endif |
|---|
| 194 | n/a | |
|---|
| 195 | n/a | |
|---|
| 196 | n/a | /* Readline implementation using fgets() */ |
|---|
| 197 | n/a | |
|---|
| 198 | n/a | char * |
|---|
| 199 | n/a | PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt) |
|---|
| 200 | n/a | { |
|---|
| 201 | n/a | size_t n; |
|---|
| 202 | n/a | char *p, *pr; |
|---|
| 203 | n/a | |
|---|
| 204 | n/a | #ifdef MS_WINDOWS |
|---|
| 205 | n/a | if (!Py_LegacyWindowsStdioFlag && sys_stdin == stdin) { |
|---|
| 206 | n/a | HANDLE hStdIn, hStdErr; |
|---|
| 207 | n/a | |
|---|
| 208 | n/a | _Py_BEGIN_SUPPRESS_IPH |
|---|
| 209 | n/a | hStdIn = (HANDLE)_get_osfhandle(fileno(sys_stdin)); |
|---|
| 210 | n/a | hStdErr = (HANDLE)_get_osfhandle(fileno(stderr)); |
|---|
| 211 | n/a | _Py_END_SUPPRESS_IPH |
|---|
| 212 | n/a | |
|---|
| 213 | n/a | if (_get_console_type(hStdIn) == 'r') { |
|---|
| 214 | n/a | fflush(sys_stdout); |
|---|
| 215 | n/a | if (prompt) { |
|---|
| 216 | n/a | if (_get_console_type(hStdErr) == 'w') { |
|---|
| 217 | n/a | wchar_t *wbuf; |
|---|
| 218 | n/a | int wlen; |
|---|
| 219 | n/a | wlen = MultiByteToWideChar(CP_UTF8, 0, prompt, -1, |
|---|
| 220 | n/a | NULL, 0); |
|---|
| 221 | n/a | if (wlen && |
|---|
| 222 | n/a | (wbuf = PyMem_RawMalloc(wlen * sizeof(wchar_t)))) { |
|---|
| 223 | n/a | wlen = MultiByteToWideChar(CP_UTF8, 0, prompt, -1, |
|---|
| 224 | n/a | wbuf, wlen); |
|---|
| 225 | n/a | if (wlen) { |
|---|
| 226 | n/a | DWORD n; |
|---|
| 227 | n/a | fflush(stderr); |
|---|
| 228 | n/a | /* wlen includes null terminator, so subtract 1 */ |
|---|
| 229 | n/a | WriteConsoleW(hStdErr, wbuf, wlen - 1, &n, NULL); |
|---|
| 230 | n/a | } |
|---|
| 231 | n/a | PyMem_RawFree(wbuf); |
|---|
| 232 | n/a | } |
|---|
| 233 | n/a | } else { |
|---|
| 234 | n/a | fprintf(stderr, "%s", prompt); |
|---|
| 235 | n/a | fflush(stderr); |
|---|
| 236 | n/a | } |
|---|
| 237 | n/a | } |
|---|
| 238 | n/a | clearerr(sys_stdin); |
|---|
| 239 | n/a | return _PyOS_WindowsConsoleReadline(hStdIn); |
|---|
| 240 | n/a | } |
|---|
| 241 | n/a | } |
|---|
| 242 | n/a | #endif |
|---|
| 243 | n/a | |
|---|
| 244 | n/a | n = 100; |
|---|
| 245 | n/a | p = (char *)PyMem_RawMalloc(n); |
|---|
| 246 | n/a | if (p == NULL) |
|---|
| 247 | n/a | return NULL; |
|---|
| 248 | n/a | |
|---|
| 249 | n/a | fflush(sys_stdout); |
|---|
| 250 | n/a | if (prompt) |
|---|
| 251 | n/a | fprintf(stderr, "%s", prompt); |
|---|
| 252 | n/a | fflush(stderr); |
|---|
| 253 | n/a | |
|---|
| 254 | n/a | switch (my_fgets(p, (int)n, sys_stdin)) { |
|---|
| 255 | n/a | case 0: /* Normal case */ |
|---|
| 256 | n/a | break; |
|---|
| 257 | n/a | case 1: /* Interrupt */ |
|---|
| 258 | n/a | PyMem_RawFree(p); |
|---|
| 259 | n/a | return NULL; |
|---|
| 260 | n/a | case -1: /* EOF */ |
|---|
| 261 | n/a | case -2: /* Error */ |
|---|
| 262 | n/a | default: /* Shouldn't happen */ |
|---|
| 263 | n/a | *p = '\0'; |
|---|
| 264 | n/a | break; |
|---|
| 265 | n/a | } |
|---|
| 266 | n/a | n = strlen(p); |
|---|
| 267 | n/a | while (n > 0 && p[n-1] != '\n') { |
|---|
| 268 | n/a | size_t incr = n+2; |
|---|
| 269 | n/a | if (incr > INT_MAX) { |
|---|
| 270 | n/a | PyMem_RawFree(p); |
|---|
| 271 | n/a | PyErr_SetString(PyExc_OverflowError, "input line too long"); |
|---|
| 272 | n/a | return NULL; |
|---|
| 273 | n/a | } |
|---|
| 274 | n/a | pr = (char *)PyMem_RawRealloc(p, n + incr); |
|---|
| 275 | n/a | if (pr == NULL) { |
|---|
| 276 | n/a | PyMem_RawFree(p); |
|---|
| 277 | n/a | PyErr_NoMemory(); |
|---|
| 278 | n/a | return NULL; |
|---|
| 279 | n/a | } |
|---|
| 280 | n/a | p = pr; |
|---|
| 281 | n/a | if (my_fgets(p+n, (int)incr, sys_stdin) != 0) |
|---|
| 282 | n/a | break; |
|---|
| 283 | n/a | n += strlen(p+n); |
|---|
| 284 | n/a | } |
|---|
| 285 | n/a | pr = (char *)PyMem_RawRealloc(p, n+1); |
|---|
| 286 | n/a | if (pr == NULL) { |
|---|
| 287 | n/a | PyMem_RawFree(p); |
|---|
| 288 | n/a | PyErr_NoMemory(); |
|---|
| 289 | n/a | return NULL; |
|---|
| 290 | n/a | } |
|---|
| 291 | n/a | return pr; |
|---|
| 292 | n/a | } |
|---|
| 293 | n/a | |
|---|
| 294 | n/a | |
|---|
| 295 | n/a | /* By initializing this function pointer, systems embedding Python can |
|---|
| 296 | n/a | override the readline function. |
|---|
| 297 | n/a | |
|---|
| 298 | n/a | Note: Python expects in return a buffer allocated with PyMem_Malloc. */ |
|---|
| 299 | n/a | |
|---|
| 300 | n/a | char *(*PyOS_ReadlineFunctionPointer)(FILE *, FILE *, const char *); |
|---|
| 301 | n/a | |
|---|
| 302 | n/a | |
|---|
| 303 | n/a | /* Interface used by tokenizer.c and bltinmodule.c */ |
|---|
| 304 | n/a | |
|---|
| 305 | n/a | char * |
|---|
| 306 | n/a | PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt) |
|---|
| 307 | n/a | { |
|---|
| 308 | n/a | char *rv, *res; |
|---|
| 309 | n/a | size_t len; |
|---|
| 310 | n/a | |
|---|
| 311 | n/a | if (_PyOS_ReadlineTState == PyThreadState_GET()) { |
|---|
| 312 | n/a | PyErr_SetString(PyExc_RuntimeError, |
|---|
| 313 | n/a | "can't re-enter readline"); |
|---|
| 314 | n/a | return NULL; |
|---|
| 315 | n/a | } |
|---|
| 316 | n/a | |
|---|
| 317 | n/a | |
|---|
| 318 | n/a | if (PyOS_ReadlineFunctionPointer == NULL) { |
|---|
| 319 | n/a | PyOS_ReadlineFunctionPointer = PyOS_StdioReadline; |
|---|
| 320 | n/a | } |
|---|
| 321 | n/a | |
|---|
| 322 | n/a | #ifdef WITH_THREAD |
|---|
| 323 | n/a | if (_PyOS_ReadlineLock == NULL) { |
|---|
| 324 | n/a | _PyOS_ReadlineLock = PyThread_allocate_lock(); |
|---|
| 325 | n/a | } |
|---|
| 326 | n/a | #endif |
|---|
| 327 | n/a | |
|---|
| 328 | n/a | _PyOS_ReadlineTState = PyThreadState_GET(); |
|---|
| 329 | n/a | Py_BEGIN_ALLOW_THREADS |
|---|
| 330 | n/a | #ifdef WITH_THREAD |
|---|
| 331 | n/a | PyThread_acquire_lock(_PyOS_ReadlineLock, 1); |
|---|
| 332 | n/a | #endif |
|---|
| 333 | n/a | |
|---|
| 334 | n/a | /* This is needed to handle the unlikely case that the |
|---|
| 335 | n/a | * interpreter is in interactive mode *and* stdin/out are not |
|---|
| 336 | n/a | * a tty. This can happen, for example if python is run like |
|---|
| 337 | n/a | * this: python -i < test1.py |
|---|
| 338 | n/a | */ |
|---|
| 339 | n/a | if (!isatty (fileno (sys_stdin)) || !isatty (fileno (sys_stdout))) |
|---|
| 340 | n/a | rv = PyOS_StdioReadline (sys_stdin, sys_stdout, prompt); |
|---|
| 341 | n/a | else |
|---|
| 342 | n/a | rv = (*PyOS_ReadlineFunctionPointer)(sys_stdin, sys_stdout, |
|---|
| 343 | n/a | prompt); |
|---|
| 344 | n/a | Py_END_ALLOW_THREADS |
|---|
| 345 | n/a | |
|---|
| 346 | n/a | #ifdef WITH_THREAD |
|---|
| 347 | n/a | PyThread_release_lock(_PyOS_ReadlineLock); |
|---|
| 348 | n/a | #endif |
|---|
| 349 | n/a | |
|---|
| 350 | n/a | _PyOS_ReadlineTState = NULL; |
|---|
| 351 | n/a | |
|---|
| 352 | n/a | if (rv == NULL) |
|---|
| 353 | n/a | return NULL; |
|---|
| 354 | n/a | |
|---|
| 355 | n/a | len = strlen(rv) + 1; |
|---|
| 356 | n/a | res = PyMem_Malloc(len); |
|---|
| 357 | n/a | if (res != NULL) |
|---|
| 358 | n/a | memcpy(res, rv, len); |
|---|
| 359 | n/a | PyMem_RawFree(rv); |
|---|
| 360 | n/a | |
|---|
| 361 | n/a | return res; |
|---|
| 362 | n/a | } |
|---|