blob: 6d90d2005943de45c1d4a62dce01922ff590a658 [file] [log] [blame]
Guido van Rossum6fa63431993-12-24 10:36:57 +00001
Guido van Rossumfbd64c81997-02-18 21:53:32 +00002/* 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.
Guido van Rossum6fa63431993-12-24 10:36:57 +00006 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
10*/
11
Guido van Rossum8efa47b1998-08-27 19:43:43 +000012#include "Python.h"
Mark Hammond2f10cb82002-07-14 23:12:29 +000013#ifdef MS_WINDOWS
14#define WIN32_LEAN_AND_MEAN
15#include "windows.h"
16#endif /* MS_WINDOWS */
Guido van Rossum6fa63431993-12-24 10:36:57 +000017
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +000018#ifdef __VMS
19extern char* vms__StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt);
20#endif
21
Michael W. Hudson30ea2f22004-07-07 17:44:12 +000022
23PyThreadState* _PyOS_ReadlineTState;
24
Tim Petersb7e898a2004-07-07 20:42:07 +000025#ifdef WITH_THREAD
Michael W. Hudson30ea2f22004-07-07 17:44:12 +000026#include "pythread.h"
27static PyThread_type_lock _PyOS_ReadlineLock = NULL;
28#endif
29
Thomas Wouters23c9e002000-07-22 19:20:54 +000030int (*PyOS_InputHook)(void) = NULL;
Guido van Rossumfbd64c81997-02-18 21:53:32 +000031
Guido van Rossum48a680c2001-03-02 06:34:14 +000032#ifdef RISCOS
33int Py_RISCOSWimpFlag;
34#endif
35
Guido van Rossumfbd64c81997-02-18 21:53:32 +000036/* This function restarts a fgets() after an EINTR error occurred
37 except if PyOS_InterruptOccurred() returns true. */
38
39static int
Thomas Wouters23c9e002000-07-22 19:20:54 +000040my_fgets(char *buf, int len, FILE *fp)
Guido van Rossumfbd64c81997-02-18 21:53:32 +000041{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000042 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 */
Mark Hammond2f10cb82002-07-14 23:12:29 +000049#ifdef MS_WINDOWS
Antoine Pitrouc83ea132010-05-09 14:46:46 +000050 /* 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())
57 */
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).
64 */
65 Sleep(1);
66 if (PyOS_InterruptOccurred()) {
67 return 1; /* Interrupt */
68 }
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.
75 */
76 }
Andrew M. Kuchlingb64d6132010-02-22 22:48:41 +000077#endif /* MS_WINDOWS */
Antoine Pitrouc83ea132010-05-09 14:46:46 +000078 if (feof(fp)) {
79 return -1; /* EOF */
80 }
Andrew M. Kuchlingb64d6132010-02-22 22:48:41 +000081#ifdef EINTR
Antoine Pitrouc83ea132010-05-09 14:46:46 +000082 if (errno == EINTR) {
83 int s;
Andrew M. Kuchlingb64d6132010-02-22 22:48:41 +000084#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +000085 PyEval_RestoreThread(_PyOS_ReadlineTState);
Andrew M. Kuchlingb64d6132010-02-22 22:48:41 +000086#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +000087 s = PyErr_CheckSignals();
Andrew M. Kuchlingb64d6132010-02-22 22:48:41 +000088#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +000089 PyEval_SaveThread();
Andrew M. Kuchlingb64d6132010-02-22 22:48:41 +000090#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +000091 if (s < 0) {
92 return 1;
93 }
94 }
Andrew M. Kuchlingb64d6132010-02-22 22:48:41 +000095#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +000096 if (PyOS_InterruptOccurred()) {
97 return 1; /* Interrupt */
98 }
99 return -2; /* Error */
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000100}
101
102
103/* Readline implementation using fgets() */
104
105char *
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000106PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000107{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000108 size_t n;
109 char *p;
110 n = 100;
111 if ((p = (char *)PyMem_MALLOC(n)) == NULL)
112 return NULL;
113 fflush(sys_stdout);
Guido van Rossum48a680c2001-03-02 06:34:14 +0000114#ifndef RISCOS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000115 if (prompt)
116 fprintf(stderr, "%s", prompt);
Guido van Rossum48a680c2001-03-02 06:34:14 +0000117#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000118 if (prompt) {
119 if(Py_RISCOSWimpFlag)
120 fprintf(stderr, "\x0cr%s\x0c", prompt);
121 else
122 fprintf(stderr, "%s", prompt);
123 }
Guido van Rossum48a680c2001-03-02 06:34:14 +0000124#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000125 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;
137 }
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");
146 }
147 if (my_fgets(p+n, (int)incr, sys_stdin) != 0)
148 break;
149 n += strlen(p+n);
150 }
151 return (char *)PyMem_REALLOC(p, n+1);
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000152}
153
154
155/* By initializing this function pointer, systems embedding Python can
Guido van Rossumb18618d2000-05-03 23:44:39 +0000156 override the readline function.
157
Neal Norwitz08062d62006-04-11 08:19:15 +0000158 Note: Python expects in return a buffer allocated with PyMem_Malloc. */
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000159
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000160char *(*PyOS_ReadlineFunctionPointer)(FILE *, FILE *, char *);
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000161
162
163/* Interface used by tokenizer.c and bltinmodule.c */
164
165char *
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000166PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000167{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000168 char *rv;
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000169
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000170 if (_PyOS_ReadlineTState == PyThreadState_GET()) {
171 PyErr_SetString(PyExc_RuntimeError,
172 "can't re-enter readline");
173 return NULL;
174 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000175
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000176
177 if (PyOS_ReadlineFunctionPointer == NULL) {
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000178#ifdef __VMS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000179 PyOS_ReadlineFunctionPointer = vms__StdioReadline;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000180#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000181 PyOS_ReadlineFunctionPointer = PyOS_StdioReadline;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000182#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000183 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000184
Tim Petersb7e898a2004-07-07 20:42:07 +0000185#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000186 if (_PyOS_ReadlineLock == NULL) {
187 _PyOS_ReadlineLock = PyThread_allocate_lock();
188 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000189#endif
190
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000191 _PyOS_ReadlineTState = PyThreadState_GET();
192 Py_BEGIN_ALLOW_THREADS
193#ifdef WITH_THREAD
194 PyThread_acquire_lock(_PyOS_ReadlineLock, 1);
195#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000196
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000197 /* 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
201 */
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
208
209#ifdef WITH_THREAD
210 PyThread_release_lock(_PyOS_ReadlineLock);
211#endif
212
213 _PyOS_ReadlineTState = NULL;
214
215 return rv;
Guido van Rossum6fa63431993-12-24 10:36:57 +0000216}