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
14 #define WIN32_LEAN_AND_MEAN
16 #endif /* MS_WINDOWS */
18 int (*PyOS_InputHook
)(void) = NULL
;
21 int Py_RISCOSWimpFlag
;
24 /* This function restarts a fgets() after an EINTR error occurred
25 except if PyOS_InterruptOccurred() returns true. */
28 my_fgets(char *buf
, int len
, FILE *fp
)
32 if (PyOS_InputHook
!= NULL
)
33 (void)(PyOS_InputHook
)();
35 p
= fgets(buf
, len
, fp
);
37 return 0; /* No error */
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).
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 */
72 if (PyOS_InterruptOccurred()) {
73 return 1; /* Interrupt */
78 if (PyOS_InterruptOccurred()) {
79 return 1; /* Interrupt */
81 return -2; /* Error */
87 /* Readline implementation using fgets() */
90 PyOS_StdioReadline(FILE *sys_stdin
, FILE *sys_stdout
, char *prompt
)
95 if ((p
= PyMem_MALLOC(n
)) == NULL
)
100 fprintf(stderr
, "%s", prompt
);
103 if(Py_RISCOSWimpFlag
)
104 fprintf(stderr
, "\x0cr%s\x0c", prompt
);
106 fprintf(stderr
, "%s", prompt
);
110 switch (my_fgets(p
, (int)n
, sys_stdin
)) {
111 case 0: /* Normal case */
113 case 1: /* Interrupt */
118 default: /* Shouldn't happen */
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...) */
126 if (strncmp(p
, prompt
, n
) == 0)
127 memmove(p
, p
+ n
, strlen(p
) - n
+ 1);
130 while (n
> 0 && p
[n
-1] != '\n') {
132 p
= PyMem_REALLOC(p
, n
+ incr
);
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)
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 */
157 PyOS_Readline(FILE *sys_stdin
, FILE *sys_stdout
, char *prompt
)
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
);
175 rv
= (*PyOS_ReadlineFunctionPointer
)(sys_stdin
, sys_stdout
,