Issue #7673: Fix security vulnerability (CVE-2010-2089) in the audioop module,
[python/dscho.git] / Parser / myreadline.c
blob6d90d2005943de45c1d4a62dce01922ff590a658
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.
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
12 #include "Python.h"
13 #ifdef MS_WINDOWS
14 #define WIN32_LEAN_AND_MEAN
15 #include "windows.h"
16 #endif /* MS_WINDOWS */
18 #ifdef __VMS
19 extern char* vms__StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt);
20 #endif
23 PyThreadState* _PyOS_ReadlineTState;
25 #ifdef WITH_THREAD
26 #include "pythread.h"
27 static PyThread_type_lock _PyOS_ReadlineLock = NULL;
28 #endif
30 int (*PyOS_InputHook)(void) = NULL;
32 #ifdef RISCOS
33 int Py_RISCOSWimpFlag;
34 #endif
36 /* This function restarts a fgets() after an EINTR error occurred
37 except if PyOS_InterruptOccurred() returns true. */
39 static int
40 my_fgets(char *buf, int len, FILE *fp)
42 char *p;
43 if (PyOS_InputHook != NULL)
44 (void)(PyOS_InputHook)();
45 errno = 0;
46 p = fgets(buf, len, fp);
47 if (p != NULL)
48 return 0; /* No error */
49 #ifdef MS_WINDOWS
50 /* In the case of a Ctrl+C or some other external event
51 interrupting the operation:
52 Win2k/NT: ERROR_OPERATION_ABORTED is the most recent Win32
53 error code (and feof() returns TRUE).
54 Win9x: Ctrl+C seems to have no effect on fgets() returning
55 early - the signal handler is called, but the fgets()
56 only returns "normally" (ie, when Enter hit or feof())
58 if (GetLastError()==ERROR_OPERATION_ABORTED) {
59 /* Signals come asynchronously, so we sleep a brief
60 moment before checking if the handler has been
61 triggered (we cant just return 1 before the
62 signal handler has been called, as the later
63 signal may be treated as a separate interrupt).
65 Sleep(1);
66 if (PyOS_InterruptOccurred()) {
67 return 1; /* Interrupt */
69 /* Either the sleep wasn't long enough (need a
70 short loop retrying?) or not interrupted at all
71 (in which case we should revisit the whole thing!)
72 Logging some warning would be nice. assert is not
73 viable as under the debugger, the various dialogs
74 mean the condition is not true.
77 #endif /* MS_WINDOWS */
78 if (feof(fp)) {
79 return -1; /* EOF */
81 #ifdef EINTR
82 if (errno == EINTR) {
83 int s;
84 #ifdef WITH_THREAD
85 PyEval_RestoreThread(_PyOS_ReadlineTState);
86 #endif
87 s = PyErr_CheckSignals();
88 #ifdef WITH_THREAD
89 PyEval_SaveThread();
90 #endif
91 if (s < 0) {
92 return 1;
95 #endif
96 if (PyOS_InterruptOccurred()) {
97 return 1; /* Interrupt */
99 return -2; /* Error */
103 /* Readline implementation using fgets() */
105 char *
106 PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
108 size_t n;
109 char *p;
110 n = 100;
111 if ((p = (char *)PyMem_MALLOC(n)) == NULL)
112 return NULL;
113 fflush(sys_stdout);
114 #ifndef RISCOS
115 if (prompt)
116 fprintf(stderr, "%s", prompt);
117 #else
118 if (prompt) {
119 if(Py_RISCOSWimpFlag)
120 fprintf(stderr, "\x0cr%s\x0c", prompt);
121 else
122 fprintf(stderr, "%s", prompt);
124 #endif
125 fflush(stderr);
126 switch (my_fgets(p, (int)n, sys_stdin)) {
127 case 0: /* Normal case */
128 break;
129 case 1: /* Interrupt */
130 PyMem_FREE(p);
131 return NULL;
132 case -1: /* EOF */
133 case -2: /* Error */
134 default: /* Shouldn't happen */
135 *p = '\0';
136 break;
138 n = strlen(p);
139 while (n > 0 && p[n-1] != '\n') {
140 size_t incr = n+2;
141 p = (char *)PyMem_REALLOC(p, n + incr);
142 if (p == NULL)
143 return NULL;
144 if (incr > INT_MAX) {
145 PyErr_SetString(PyExc_OverflowError, "input line too long");
147 if (my_fgets(p+n, (int)incr, sys_stdin) != 0)
148 break;
149 n += strlen(p+n);
151 return (char *)PyMem_REALLOC(p, n+1);
155 /* By initializing this function pointer, systems embedding Python can
156 override the readline function.
158 Note: Python expects in return a buffer allocated with PyMem_Malloc. */
160 char *(*PyOS_ReadlineFunctionPointer)(FILE *, FILE *, char *);
163 /* Interface used by tokenizer.c and bltinmodule.c */
165 char *
166 PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
168 char *rv;
170 if (_PyOS_ReadlineTState == PyThreadState_GET()) {
171 PyErr_SetString(PyExc_RuntimeError,
172 "can't re-enter readline");
173 return NULL;
177 if (PyOS_ReadlineFunctionPointer == NULL) {
178 #ifdef __VMS
179 PyOS_ReadlineFunctionPointer = vms__StdioReadline;
180 #else
181 PyOS_ReadlineFunctionPointer = PyOS_StdioReadline;
182 #endif
185 #ifdef WITH_THREAD
186 if (_PyOS_ReadlineLock == NULL) {
187 _PyOS_ReadlineLock = PyThread_allocate_lock();
189 #endif
191 _PyOS_ReadlineTState = PyThreadState_GET();
192 Py_BEGIN_ALLOW_THREADS
193 #ifdef WITH_THREAD
194 PyThread_acquire_lock(_PyOS_ReadlineLock, 1);
195 #endif
197 /* This is needed to handle the unlikely case that the
198 * interpreter is in interactive mode *and* stdin/out are not
199 * a tty. This can happen, for example if python is run like
200 * this: python -i < test1.py
202 if (!isatty (fileno (sys_stdin)) || !isatty (fileno (sys_stdout)))
203 rv = PyOS_StdioReadline (sys_stdin, sys_stdout, prompt);
204 else
205 rv = (*PyOS_ReadlineFunctionPointer)(sys_stdin, sys_stdout,
206 prompt);
207 Py_END_ALLOW_THREADS
209 #ifdef WITH_THREAD
210 PyThread_release_lock(_PyOS_ReadlineLock);
211 #endif
213 _PyOS_ReadlineTState = NULL;
215 return rv;