| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#include "Python.h" |
| 13 |
#ifdef MS_WINDOWS |
| 14 |
#define WIN32_LEAN_AND_MEAN |
| 15 |
#include "windows.h" |
| 16 |
#endif |
| 17 |
|
| 18 |
#ifdef __VMS |
| 19 |
extern char* vms__StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt); |
| 20 |
#endif |
| 21 |
|
| 22 |
|
| 23 |
PyThreadState* _PyOS_ReadlineTState; |
| 24 |
|
| 25 |
#ifdef WITH_THREAD |
| 26 |
#include "pythread.h" |
| 27 |
static PyThread_type_lock _PyOS_ReadlineLock = NULL; |
| 28 |
#endif |
| 29 |
|
| 30 |
int (*PyOS_InputHook)(void) = NULL; |
| 31 |
|
| 32 |
#ifdef RISCOS |
| 33 |
int Py_RISCOSWimpFlag; |
| 34 |
#endif |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
static int |
| 40 |
my_fgets(char *buf, int len, FILE *fp) |
| 41 |
{ |
| 42 |
char *p; |
| 43 |
#ifdef MS_WINDOWS |
| 44 |
int i; |
| 45 |
#endif |
| 46 |
|
| 47 |
while (1) { |
| 48 |
if (PyOS_InputHook != NULL) |
| 49 |
(void)(PyOS_InputHook)(); |
| 50 |
errno = 0; |
| 51 |
clearerr(fp); |
| 52 |
p = fgets(buf, len, fp); |
| 53 |
if (p != NULL) |
| 54 |
return 0; |
| 55 |
#ifdef MS_WINDOWS |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
if (GetLastError()==ERROR_OPERATION_ABORTED) { |
| 69 |
for (i = 0; i < 10; i++) { |
| 70 |
if (PyOS_InterruptOccurred()) |
| 71 |
return 1; |
| 72 |
Sleep(1); |
| 73 |
} |
| 74 |
} |
| 75 |
#endif |
| 76 |
if (feof(fp)) { |
| 77 |
clearerr(fp); |
| 78 |
return -1; |
| 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 |
|
| 93 |
continue; |
| 94 |
} |
| 95 |
#endif |
| 96 |
if (PyOS_InterruptOccurred()) { |
| 97 |
return 1; |
| 98 |
} |
| 99 |
return -2; |
| 100 |
} |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
char * |
| 108 |
PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt) |
| 109 |
{ |
| 110 |
size_t n; |
| 111 |
char *p, *pr; |
| 112 |
n = 100; |
| 113 |
if ((p = (char *)PyMem_MALLOC(n)) == NULL) |
| 114 |
return NULL; |
| 115 |
fflush(sys_stdout); |
| 116 |
#ifndef RISCOS |
| 117 |
if (prompt) |
| 118 |
fprintf(stderr, "%s", prompt); |
| 119 |
#else |
| 120 |
if (prompt) { |
| 121 |
if(Py_RISCOSWimpFlag) |
| 122 |
fprintf(stderr, "\x0cr%s\x0c", prompt); |
| 123 |
else |
| 124 |
fprintf(stderr, "%s", prompt); |
| 125 |
} |
| 126 |
#endif |
| 127 |
fflush(stderr); |
| 128 |
switch (my_fgets(p, (int)n, sys_stdin)) { |
| 129 |
case 0: |
| 130 |
break; |
| 131 |
case 1: |
| 132 |
PyMem_FREE(p); |
| 133 |
return NULL; |
| 134 |
case -1: |
| 135 |
case -2: |
| 136 |
default: |
| 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 |
if (incr > INT_MAX) { |
| 144 |
PyMem_FREE(p); |
| 145 |
PyErr_SetString(PyExc_OverflowError, "input line too long"); |
| 146 |
return NULL; |
| 147 |
} |
| 148 |
pr = (char *)PyMem_REALLOC(p, n + incr); |
| 149 |
if (pr == NULL) { |
| 150 |
PyMem_FREE(p); |
| 151 |
PyErr_NoMemory(); |
| 152 |
return NULL; |
| 153 |
} |
| 154 |
p = pr; |
| 155 |
if (my_fgets(p+n, (int)incr, sys_stdin) != 0) |
| 156 |
break; |
| 157 |
n += strlen(p+n); |
| 158 |
} |
| 159 |
pr = (char *)PyMem_REALLOC(p, n+1); |
| 160 |
if (pr == NULL) { |
| 161 |
PyMem_FREE(p); |
| 162 |
PyErr_NoMemory(); |
| 163 |
return NULL; |
| 164 |
} |
| 165 |
return pr; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
char *(*PyOS_ReadlineFunctionPointer)(FILE *, FILE *, char *); |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
char * |
| 180 |
PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt) |
| 181 |
{ |
| 182 |
char *rv; |
| 183 |
|
| 184 |
if (_PyOS_ReadlineTState == PyThreadState_GET()) { |
| 185 |
PyErr_SetString(PyExc_RuntimeError, |
| 186 |
"can't re-enter readline"); |
| 187 |
return NULL; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
if (PyOS_ReadlineFunctionPointer == NULL) { |
| 192 |
#ifdef __VMS |
| 193 |
PyOS_ReadlineFunctionPointer = vms__StdioReadline; |
| 194 |
#else |
| 195 |
PyOS_ReadlineFunctionPointer = PyOS_StdioReadline; |
| 196 |
#endif |
| 197 |
} |
| 198 |
|
| 199 |
#ifdef WITH_THREAD |
| 200 |
if (_PyOS_ReadlineLock == NULL) { |
| 201 |
_PyOS_ReadlineLock = PyThread_allocate_lock(); |
| 202 |
} |
| 203 |
#endif |
| 204 |
|
| 205 |
_PyOS_ReadlineTState = PyThreadState_GET(); |
| 206 |
Py_BEGIN_ALLOW_THREADS |
| 207 |
#ifdef WITH_THREAD |
| 208 |
PyThread_acquire_lock(_PyOS_ReadlineLock, 1); |
| 209 |
#endif |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
if (!isatty (fileno (sys_stdin)) || !isatty (fileno (sys_stdout))) |
| 217 |
rv = PyOS_StdioReadline (sys_stdin, sys_stdout, prompt); |
| 218 |
else |
| 219 |
rv = (*PyOS_ReadlineFunctionPointer)(sys_stdin, sys_stdout, |
| 220 |
prompt); |
| 221 |
Py_END_ALLOW_THREADS |
| 222 |
|
| 223 |
#ifdef WITH_THREAD |
| 224 |
PyThread_release_lock(_PyOS_ReadlineLock); |
| 225 |
#endif |
| 226 |
|
| 227 |
_PyOS_ReadlineTState = NULL; |
| 228 |
|
| 229 |
return rv; |
| 230 |
} |