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