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" |
Mark Hammond | 2f10cb8 | 2002-07-14 23:12:29 +0000 | [diff] [blame] | 13 | #ifdef MS_WINDOWS |
| 14 | #define WIN32_LEAN_AND_MEAN |
| 15 | #include "windows.h" |
| 16 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 17 | |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 18 | #ifdef __VMS |
| 19 | extern char* vms__StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt); |
| 20 | #endif |
| 21 | |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 22 | |
| 23 | PyThreadState* _PyOS_ReadlineTState; |
| 24 | |
Tim Peters | b7e898a | 2004-07-07 20:42:07 +0000 | [diff] [blame] | 25 | #ifdef WITH_THREAD |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 26 | #include "pythread.h" |
| 27 | static PyThread_type_lock _PyOS_ReadlineLock = NULL; |
| 28 | #endif |
| 29 | |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 30 | int (*PyOS_InputHook)(void) = NULL; |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 31 | |
| 32 | /* This function restarts a fgets() after an EINTR error occurred |
| 33 | except if PyOS_InterruptOccurred() returns true. */ |
| 34 | |
| 35 | static int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 36 | my_fgets(char *buf, int len, FILE *fp) |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 37 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 38 | char *p; |
| 39 | if (PyOS_InputHook != NULL) |
| 40 | (void)(PyOS_InputHook)(); |
| 41 | errno = 0; |
| 42 | p = fgets(buf, len, fp); |
| 43 | if (p != NULL) |
| 44 | return 0; /* No error */ |
Mark Hammond | 2f10cb8 | 2002-07-14 23:12:29 +0000 | [diff] [blame] | 45 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 46 | /* In the case of a Ctrl+C or some other external event |
| 47 | interrupting the operation: |
| 48 | Win2k/NT: ERROR_OPERATION_ABORTED is the most recent Win32 |
| 49 | error code (and feof() returns TRUE). |
| 50 | Win9x: Ctrl+C seems to have no effect on fgets() returning |
| 51 | early - the signal handler is called, but the fgets() |
| 52 | only returns "normally" (ie, when Enter hit or feof()) |
| 53 | */ |
| 54 | if (GetLastError()==ERROR_OPERATION_ABORTED) { |
| 55 | /* Signals come asynchronously, so we sleep a brief |
| 56 | moment before checking if the handler has been |
| 57 | triggered (we cant just return 1 before the |
| 58 | signal handler has been called, as the later |
| 59 | signal may be treated as a separate interrupt). |
| 60 | */ |
| 61 | Sleep(1); |
| 62 | if (PyOS_InterruptOccurred()) { |
| 63 | return 1; /* Interrupt */ |
| 64 | } |
| 65 | /* Either the sleep wasn't long enough (need a |
| 66 | short loop retrying?) or not interrupted at all |
| 67 | (in which case we should revisit the whole thing!) |
| 68 | Logging some warning would be nice. assert is not |
| 69 | viable as under the debugger, the various dialogs |
| 70 | mean the condition is not true. |
| 71 | */ |
| 72 | } |
Benjamin Peterson | 21896a3 | 2010-03-21 22:03:03 +0000 | [diff] [blame] | 73 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 74 | if (feof(fp)) { |
| 75 | return -1; /* EOF */ |
| 76 | } |
Benjamin Peterson | 21896a3 | 2010-03-21 22:03:03 +0000 | [diff] [blame] | 77 | #ifdef EINTR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 78 | if (errno == EINTR) { |
| 79 | int s; |
Benjamin Peterson | 21896a3 | 2010-03-21 22:03:03 +0000 | [diff] [blame] | 80 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 81 | PyEval_RestoreThread(_PyOS_ReadlineTState); |
Benjamin Peterson | 21896a3 | 2010-03-21 22:03:03 +0000 | [diff] [blame] | 82 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 83 | s = PyErr_CheckSignals(); |
Benjamin Peterson | 21896a3 | 2010-03-21 22:03:03 +0000 | [diff] [blame] | 84 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 85 | PyEval_SaveThread(); |
Benjamin Peterson | 21896a3 | 2010-03-21 22:03:03 +0000 | [diff] [blame] | 86 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 87 | if (s < 0) { |
| 88 | return 1; |
| 89 | } |
| 90 | } |
Benjamin Peterson | 21896a3 | 2010-03-21 22:03:03 +0000 | [diff] [blame] | 91 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 92 | if (PyOS_InterruptOccurred()) { |
| 93 | return 1; /* Interrupt */ |
| 94 | } |
| 95 | return -2; /* Error */ |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | |
| 99 | /* Readline implementation using fgets() */ |
| 100 | |
| 101 | char * |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 102 | PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt) |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 103 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 104 | size_t n; |
| 105 | char *p; |
| 106 | n = 100; |
| 107 | if ((p = (char *)PyMem_MALLOC(n)) == NULL) |
| 108 | return NULL; |
| 109 | fflush(sys_stdout); |
| 110 | if (prompt) |
| 111 | fprintf(stderr, "%s", prompt); |
| 112 | fflush(stderr); |
| 113 | switch (my_fgets(p, (int)n, sys_stdin)) { |
| 114 | case 0: /* Normal case */ |
| 115 | break; |
| 116 | case 1: /* Interrupt */ |
| 117 | PyMem_FREE(p); |
| 118 | return NULL; |
| 119 | case -1: /* EOF */ |
| 120 | case -2: /* Error */ |
| 121 | default: /* Shouldn't happen */ |
| 122 | *p = '\0'; |
| 123 | break; |
| 124 | } |
| 125 | n = strlen(p); |
| 126 | while (n > 0 && p[n-1] != '\n') { |
| 127 | size_t incr = n+2; |
| 128 | p = (char *)PyMem_REALLOC(p, n + incr); |
| 129 | if (p == NULL) |
| 130 | return NULL; |
| 131 | if (incr > INT_MAX) { |
| 132 | PyErr_SetString(PyExc_OverflowError, "input line too long"); |
| 133 | } |
| 134 | if (my_fgets(p+n, (int)incr, sys_stdin) != 0) |
| 135 | break; |
| 136 | n += strlen(p+n); |
| 137 | } |
| 138 | return (char *)PyMem_REALLOC(p, n+1); |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | |
| 142 | /* By initializing this function pointer, systems embedding Python can |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 143 | override the readline function. |
| 144 | |
| 145 | Note: Python expects in return a buffer allocated with PyMem_Malloc. */ |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 146 | |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 147 | char *(*PyOS_ReadlineFunctionPointer)(FILE *, FILE *, char *); |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 148 | |
| 149 | |
| 150 | /* Interface used by tokenizer.c and bltinmodule.c */ |
| 151 | |
| 152 | char * |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 153 | PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt) |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 154 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 155 | char *rv; |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 156 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 157 | if (_PyOS_ReadlineTState == PyThreadState_GET()) { |
| 158 | PyErr_SetString(PyExc_RuntimeError, |
| 159 | "can't re-enter readline"); |
| 160 | return NULL; |
| 161 | } |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 162 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 163 | |
| 164 | if (PyOS_ReadlineFunctionPointer == NULL) { |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 165 | #ifdef __VMS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 166 | PyOS_ReadlineFunctionPointer = vms__StdioReadline; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 167 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 168 | PyOS_ReadlineFunctionPointer = PyOS_StdioReadline; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 169 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 170 | } |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 171 | |
Tim Peters | b7e898a | 2004-07-07 20:42:07 +0000 | [diff] [blame] | 172 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 173 | if (_PyOS_ReadlineLock == NULL) { |
| 174 | _PyOS_ReadlineLock = PyThread_allocate_lock(); |
| 175 | } |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 176 | #endif |
| 177 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 178 | _PyOS_ReadlineTState = PyThreadState_GET(); |
| 179 | Py_BEGIN_ALLOW_THREADS |
| 180 | #ifdef WITH_THREAD |
| 181 | PyThread_acquire_lock(_PyOS_ReadlineLock, 1); |
| 182 | #endif |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 183 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 184 | /* This is needed to handle the unlikely case that the |
| 185 | * interpreter is in interactive mode *and* stdin/out are not |
| 186 | * a tty. This can happen, for example if python is run like |
| 187 | * this: python -i < test1.py |
| 188 | */ |
| 189 | if (!isatty (fileno (sys_stdin)) || !isatty (fileno (sys_stdout))) |
| 190 | rv = PyOS_StdioReadline (sys_stdin, sys_stdout, prompt); |
| 191 | else |
| 192 | rv = (*PyOS_ReadlineFunctionPointer)(sys_stdin, sys_stdout, |
| 193 | prompt); |
| 194 | Py_END_ALLOW_THREADS |
| 195 | |
| 196 | #ifdef WITH_THREAD |
| 197 | PyThread_release_lock(_PyOS_ReadlineLock); |
| 198 | #endif |
| 199 | |
| 200 | _PyOS_ReadlineTState = NULL; |
| 201 | |
| 202 | return rv; |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 203 | } |