Move setting of ioready 'wait' earlier in call chain, to
[python/dscho.git] / Parser / myreadline.c
blob7b7ef7e2042e00b4f4f82b376b4bf186589cf583
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 int (*PyOS_InputHook)(void) = NULL;
20 #ifdef RISCOS
21 int Py_RISCOSWimpFlag;
22 #endif
24 /* This function restarts a fgets() after an EINTR error occurred
25 except if PyOS_InterruptOccurred() returns true. */
27 static int
28 my_fgets(char *buf, int len, FILE *fp)
30 char *p;
31 for (;;) {
32 if (PyOS_InputHook != NULL)
33 (void)(PyOS_InputHook)();
34 errno = 0;
35 p = fgets(buf, len, fp);
36 if (p != NULL)
37 return 0; /* No error */
38 #ifdef MS_WINDOWS
39 /* In the case of a Ctrl+C or some other external event
40 interrupting the operation:
41 Win2k/NT: ERROR_OPERATION_ABORTED is the most recent Win32
42 error code (and feof() returns TRUE).
43 Win9x: Ctrl+C seems to have no effect on fgets() returning
44 early - the signal handler is called, but the fgets()
45 only returns "normally" (ie, when Enter hit or feof())
47 if (GetLastError()==ERROR_OPERATION_ABORTED) {
48 /* Signals come asynchronously, so we sleep a brief
49 moment before checking if the handler has been
50 triggered (we cant just return 1 before the
51 signal handler has been called, as the later
52 signal may be treated as a separate interrupt).
54 Sleep(1);
55 if (PyOS_InterruptOccurred()) {
56 return 1; /* Interrupt */
58 /* Either the sleep wasn't long enough (need a
59 short loop retrying?) or not interrupted at all
60 (in which case we should revisit the whole thing!)
61 Logging some warning would be nice. assert is not
62 viable as under the debugger, the various dialogs
63 mean the condition is not true.
66 #endif /* MS_WINDOWS */
67 if (feof(fp)) {
68 return -1; /* EOF */
70 #ifdef EINTR
71 if (errno == EINTR) {
72 if (PyOS_InterruptOccurred()) {
73 return 1; /* Interrupt */
75 continue;
77 #endif
78 if (PyOS_InterruptOccurred()) {
79 return 1; /* Interrupt */
81 return -2; /* Error */
83 /* NOTREACHED */
87 /* Readline implementation using fgets() */
89 char *
90 PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
92 size_t n;
93 char *p;
94 n = 100;
95 if ((p = PyMem_MALLOC(n)) == NULL)
96 return NULL;
97 fflush(sys_stdout);
98 #ifndef RISCOS
99 if (prompt)
100 fprintf(stderr, "%s", prompt);
101 #else
102 if (prompt) {
103 if(Py_RISCOSWimpFlag)
104 fprintf(stderr, "\x0cr%s\x0c", prompt);
105 else
106 fprintf(stderr, "%s", prompt);
108 #endif
109 fflush(stderr);
110 switch (my_fgets(p, (int)n, sys_stdin)) {
111 case 0: /* Normal case */
112 break;
113 case 1: /* Interrupt */
114 PyMem_FREE(p);
115 return NULL;
116 case -1: /* EOF */
117 case -2: /* Error */
118 default: /* Shouldn't happen */
119 *p = '\0';
120 break;
122 #ifdef MPW
123 /* Hack for MPW C where the prompt comes right back in the input */
124 /* XXX (Actually this would be rather nice on most systems...) */
125 n = strlen(prompt);
126 if (strncmp(p, prompt, n) == 0)
127 memmove(p, p + n, strlen(p) - n + 1);
128 #endif
129 n = strlen(p);
130 while (n > 0 && p[n-1] != '\n') {
131 size_t incr = n+2;
132 p = PyMem_REALLOC(p, n + incr);
133 if (p == NULL)
134 return NULL;
135 if (incr > INT_MAX) {
136 PyErr_SetString(PyExc_OverflowError, "input line too long");
138 if (my_fgets(p+n, (int)incr, sys_stdin) != 0)
139 break;
140 n += strlen(p+n);
142 return PyMem_REALLOC(p, n+1);
146 /* By initializing this function pointer, systems embedding Python can
147 override the readline function.
149 Note: Python expects in return a buffer allocated with PyMem_Malloc. */
151 char *(*PyOS_ReadlineFunctionPointer)(FILE *, FILE *, char *);
154 /* Interface used by tokenizer.c and bltinmodule.c */
156 char *
157 PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
159 char *rv;
161 if (PyOS_ReadlineFunctionPointer == NULL) {
162 PyOS_ReadlineFunctionPointer = PyOS_StdioReadline;
165 Py_BEGIN_ALLOW_THREADS
167 /* This is needed to handle the unlikely case that the
168 * interpreter is in interactive mode *and* stdin/out are not
169 * a tty. This can happen, for example if python is run like
170 * this: python -i < test1.py
172 if (!isatty (fileno (sys_stdin)) || !isatty (fileno (sys_stdout)))
173 rv = PyOS_StdioReadline (sys_stdin, sys_stdout, prompt);
174 else
175 rv = (*PyOS_ReadlineFunctionPointer)(sys_stdin, sys_stdout,
176 prompt);
177 Py_END_ALLOW_THREADS
178 return rv;