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 int (*PyOS_InputHook
)(void) = NULL
;
17 int Py_RISCOSWimpFlag
;
20 /* This function restarts a fgets() after an EINTR error occurred
21 except if PyOS_InterruptOccurred() returns true. */
24 my_fgets(char *buf
, int len
, FILE *fp
)
28 if (PyOS_InputHook
!= NULL
)
29 (void)(PyOS_InputHook
)();
31 p
= fgets(buf
, len
, fp
);
33 return 0; /* No error */
39 if (PyOS_InterruptOccurred()) {
40 return 1; /* Interrupt */
45 if (PyOS_InterruptOccurred()) {
46 return 1; /* Interrupt */
48 return -2; /* Error */
54 /* Readline implementation using fgets() */
57 PyOS_StdioReadline(char *prompt
)
62 if ((p
= PyMem_MALLOC(n
)) == NULL
)
67 fprintf(stderr
, "%s", prompt
);
71 fprintf(stderr
, "\x0cr%s\x0c", prompt
);
73 fprintf(stderr
, "%s", prompt
);
77 switch (my_fgets(p
, (int)n
, stdin
)) {
78 case 0: /* Normal case */
80 case 1: /* Interrupt */
85 default: /* Shouldn't happen */
90 /* Hack for MPW C where the prompt comes right back in the input */
91 /* XXX (Actually this would be rather nice on most systems...) */
93 if (strncmp(p
, prompt
, n
) == 0)
94 memmove(p
, p
+ n
, strlen(p
) - n
+ 1);
97 while (n
> 0 && p
[n
-1] != '\n') {
99 p
= PyMem_REALLOC(p
, n
+ incr
);
102 if (incr
> INT_MAX
) {
103 PyErr_SetString(PyExc_OverflowError
, "input line too long");
105 if (my_fgets(p
+n
, (int)incr
, stdin
) != 0)
109 return PyMem_REALLOC(p
, n
+1);
113 /* By initializing this function pointer, systems embedding Python can
114 override the readline function.
116 Note: Python expects in return a buffer allocated with PyMem_Malloc. */
118 char *(*PyOS_ReadlineFunctionPointer
)(char *);
121 /* Interface used by tokenizer.c and bltinmodule.c */
124 PyOS_Readline(char *prompt
)
127 if (PyOS_ReadlineFunctionPointer
== NULL
) {
128 PyOS_ReadlineFunctionPointer
= PyOS_StdioReadline
;
130 Py_BEGIN_ALLOW_THREADS
131 rv
= (*PyOS_ReadlineFunctionPointer
)(prompt
);