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; |
Antoine Pitrou | c345ce1 | 2011-12-16 12:28:32 +0100 | [diff] [blame] | 39 | int err; |
Jesus Cea | 035997f | 2012-07-03 13:15:03 +0200 | [diff] [blame] | 40 | #ifdef MS_WINDOWS |
| 41 | int i; |
| 42 | #endif |
| 43 | |
Victor Stinner | 52c950f | 2011-04-09 15:55:44 +0200 | [diff] [blame] | 44 | while (1) { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 45 | if (PyOS_InputHook != NULL) |
| 46 | (void)(PyOS_InputHook)(); |
| 47 | errno = 0; |
Victor Stinner | 4f71101 | 2011-05-30 23:46:00 +0200 | [diff] [blame] | 48 | clearerr(fp); |
Martin v. Löwis | e654c11 | 2012-04-30 06:10:41 +0200 | [diff] [blame] | 49 | if (_PyVerify_fd(fileno(fp))) |
| 50 | p = fgets(buf, len, fp); |
| 51 | else |
| 52 | p = NULL; |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 53 | if (p != NULL) |
| 54 | return 0; /* No error */ |
Antoine Pitrou | c345ce1 | 2011-12-16 12:28:32 +0100 | [diff] [blame] | 55 | err = errno; |
Mark Hammond | 2f10cb8 | 2002-07-14 23:12:29 +0000 | [diff] [blame] | 56 | #ifdef MS_WINDOWS |
Tim Golden | b92b757 | 2012-06-29 18:27:08 +0100 | [diff] [blame] | 57 | /* Ctrl-C anywhere on the line or Ctrl-Z if the only character |
| 58 | on a line will set ERROR_OPERATION_ABORTED. Under normal |
| 59 | circumstances Ctrl-C will also have caused the SIGINT handler |
| 60 | to fire. This signal fires in another thread and is not |
| 61 | guaranteed to have occurred before this point in the code. |
| 62 | |
| 63 | Therefore: check in a small loop to see if the trigger has |
| 64 | fired, in which case assume this is a Ctrl-C event. If it |
| 65 | hasn't fired within 10ms assume that this is a Ctrl-Z on its |
| 66 | own or that the signal isn't going to fire for some other |
| 67 | reason and drop through to check for EOF. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 68 | */ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 69 | if (GetLastError()==ERROR_OPERATION_ABORTED) { |
Tim Golden | b92b757 | 2012-06-29 18:27:08 +0100 | [diff] [blame] | 70 | for (i = 0; i < 10; i++) { |
| 71 | if (PyOS_InterruptOccurred()) |
| 72 | return 1; |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 73 | Sleep(1); |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 74 | } |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 75 | } |
Mark Hammond | 2f10cb8 | 2002-07-14 23:12:29 +0000 | [diff] [blame] | 76 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 77 | if (feof(fp)) { |
Victor Stinner | 4755ab0 | 2011-05-10 00:19:53 +0200 | [diff] [blame] | 78 | clearerr(fp); |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 79 | return -1; /* EOF */ |
| 80 | } |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 81 | #ifdef EINTR |
Antoine Pitrou | c345ce1 | 2011-12-16 12:28:32 +0100 | [diff] [blame] | 82 | if (err == EINTR) { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 83 | int s; |
Michael W. Hudson | e3afc59 | 2005-04-07 10:11:19 +0000 | [diff] [blame] | 84 | #ifdef WITH_THREAD |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 85 | PyEval_RestoreThread(_PyOS_ReadlineTState); |
Michael W. Hudson | e3afc59 | 2005-04-07 10:11:19 +0000 | [diff] [blame] | 86 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 87 | s = PyErr_CheckSignals(); |
Michael W. Hudson | e3afc59 | 2005-04-07 10:11:19 +0000 | [diff] [blame] | 88 | #ifdef WITH_THREAD |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 89 | PyEval_SaveThread(); |
Michael W. Hudson | e3afc59 | 2005-04-07 10:11:19 +0000 | [diff] [blame] | 90 | #endif |
Victor Stinner | 52c950f | 2011-04-09 15:55:44 +0200 | [diff] [blame] | 91 | if (s < 0) |
| 92 | return 1; |
Victor Stinner | a870e35 | 2011-04-09 15:59:25 +0200 | [diff] [blame] | 93 | /* try again */ |
Victor Stinner | 52c950f | 2011-04-09 15:55:44 +0200 | [diff] [blame] | 94 | continue; |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 95 | } |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 96 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 97 | if (PyOS_InterruptOccurred()) { |
| 98 | return 1; /* Interrupt */ |
| 99 | } |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 100 | return -2; /* Error */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 101 | } |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 102 | /* NOTREACHED */ |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | |
| 106 | /* Readline implementation using fgets() */ |
| 107 | |
| 108 | char * |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 109 | PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt) |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 110 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 | size_t n; |
| 112 | char *p; |
| 113 | n = 100; |
| 114 | if ((p = (char *)PyMem_MALLOC(n)) == NULL) |
| 115 | return NULL; |
| 116 | fflush(sys_stdout); |
| 117 | if (prompt) |
| 118 | fprintf(stderr, "%s", prompt); |
| 119 | fflush(stderr); |
| 120 | switch (my_fgets(p, (int)n, sys_stdin)) { |
| 121 | case 0: /* Normal case */ |
| 122 | break; |
| 123 | case 1: /* Interrupt */ |
| 124 | PyMem_FREE(p); |
| 125 | return NULL; |
| 126 | case -1: /* EOF */ |
| 127 | case -2: /* Error */ |
| 128 | default: /* Shouldn't happen */ |
| 129 | *p = '\0'; |
| 130 | break; |
| 131 | } |
| 132 | n = strlen(p); |
| 133 | while (n > 0 && p[n-1] != '\n') { |
| 134 | size_t incr = n+2; |
| 135 | p = (char *)PyMem_REALLOC(p, n + incr); |
| 136 | if (p == NULL) |
| 137 | return NULL; |
| 138 | if (incr > INT_MAX) { |
| 139 | PyErr_SetString(PyExc_OverflowError, "input line too long"); |
| 140 | } |
| 141 | if (my_fgets(p+n, (int)incr, sys_stdin) != 0) |
| 142 | break; |
| 143 | n += strlen(p+n); |
| 144 | } |
| 145 | return (char *)PyMem_REALLOC(p, n+1); |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | |
| 149 | /* By initializing this function pointer, systems embedding Python can |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 150 | override the readline function. |
| 151 | |
| 152 | Note: Python expects in return a buffer allocated with PyMem_Malloc. */ |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 153 | |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 154 | char *(*PyOS_ReadlineFunctionPointer)(FILE *, FILE *, char *); |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 155 | |
| 156 | |
| 157 | /* Interface used by tokenizer.c and bltinmodule.c */ |
| 158 | |
| 159 | char * |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 160 | PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt) |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 161 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 162 | char *rv; |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 163 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 164 | if (_PyOS_ReadlineTState == PyThreadState_GET()) { |
| 165 | PyErr_SetString(PyExc_RuntimeError, |
| 166 | "can't re-enter readline"); |
| 167 | return NULL; |
| 168 | } |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 169 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 170 | |
| 171 | if (PyOS_ReadlineFunctionPointer == NULL) { |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 172 | #ifdef __VMS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 173 | PyOS_ReadlineFunctionPointer = vms__StdioReadline; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 174 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 175 | PyOS_ReadlineFunctionPointer = PyOS_StdioReadline; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 176 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 177 | } |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 178 | |
Tim Peters | b7e898a | 2004-07-07 20:42:07 +0000 | [diff] [blame] | 179 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 180 | if (_PyOS_ReadlineLock == NULL) { |
| 181 | _PyOS_ReadlineLock = PyThread_allocate_lock(); |
| 182 | } |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 183 | #endif |
| 184 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 185 | _PyOS_ReadlineTState = PyThreadState_GET(); |
| 186 | Py_BEGIN_ALLOW_THREADS |
| 187 | #ifdef WITH_THREAD |
| 188 | PyThread_acquire_lock(_PyOS_ReadlineLock, 1); |
| 189 | #endif |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 190 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 191 | /* This is needed to handle the unlikely case that the |
| 192 | * interpreter is in interactive mode *and* stdin/out are not |
| 193 | * a tty. This can happen, for example if python is run like |
| 194 | * this: python -i < test1.py |
| 195 | */ |
| 196 | if (!isatty (fileno (sys_stdin)) || !isatty (fileno (sys_stdout))) |
| 197 | rv = PyOS_StdioReadline (sys_stdin, sys_stdout, prompt); |
| 198 | else |
| 199 | rv = (*PyOS_ReadlineFunctionPointer)(sys_stdin, sys_stdout, |
| 200 | prompt); |
| 201 | Py_END_ALLOW_THREADS |
| 202 | |
| 203 | #ifdef WITH_THREAD |
| 204 | PyThread_release_lock(_PyOS_ReadlineLock); |
| 205 | #endif |
| 206 | |
| 207 | _PyOS_ReadlineTState = NULL; |
| 208 | |
| 209 | return rv; |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 210 | } |