| 1 |
|
|---|
| 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.
|
|---|
| 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 |
|
|---|
| 12 | #include "Python.h"
|
|---|
| 13 | #ifdef MS_WINDOWS
|
|---|
| 14 | #define WIN32_LEAN_AND_MEAN
|
|---|
| 15 | #include "windows.h"
|
|---|
| 16 | #endif /* MS_WINDOWS */
|
|---|
| 17 |
|
|---|
| 18 | #ifdef __VMS
|
|---|
| 19 | extern char* vms__StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt);
|
|---|
| 20 | #endif
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 | PyThreadState* _PyOS_ReadlineTState;
|
|---|
| 24 |
|
|---|
| 25 | #ifdef WITH_THREAD
|
|---|
| 26 | #include "pythread.h"
|
|---|
| 27 | static PyThread_type_lock _PyOS_ReadlineLock = NULL;
|
|---|
| 28 | #endif
|
|---|
| 29 |
|
|---|
| 30 | int (*PyOS_InputHook)(void) = NULL;
|
|---|
| 31 |
|
|---|
| 32 | #ifdef RISCOS
|
|---|
| 33 | int Py_RISCOSWimpFlag;
|
|---|
| 34 | #endif
|
|---|
| 35 |
|
|---|
| 36 | /* This function restarts a fgets() after an EINTR error occurred
|
|---|
| 37 | except if PyOS_InterruptOccurred() returns true. */
|
|---|
| 38 |
|
|---|
| 39 | static int
|
|---|
| 40 | my_fgets(char *buf, int len, FILE *fp)
|
|---|
| 41 | {
|
|---|
| 42 | char *p;
|
|---|
| 43 | for (;;) {
|
|---|
| 44 | if (PyOS_InputHook != NULL)
|
|---|
| 45 | (void)(PyOS_InputHook)();
|
|---|
| 46 | errno = 0;
|
|---|
| 47 | p = fgets(buf, len, fp);
|
|---|
| 48 | if (p != NULL)
|
|---|
| 49 | return 0; /* No error */
|
|---|
| 50 | #ifdef MS_WINDOWS
|
|---|
| 51 | /* In the case of a Ctrl+C or some other external event
|
|---|
| 52 | interrupting the operation:
|
|---|
| 53 | Win2k/NT: ERROR_OPERATION_ABORTED is the most recent Win32
|
|---|
| 54 | error code (and feof() returns TRUE).
|
|---|
| 55 | Win9x: Ctrl+C seems to have no effect on fgets() returning
|
|---|
| 56 | early - the signal handler is called, but the fgets()
|
|---|
|
|---|