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