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