blob: 4cc4b2cf6b66ef5f756ea1be2e6d3aab159f4e40 [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
32/* This function restarts a fgets() after an EINTR error occurred
33 except if PyOS_InterruptOccurred() returns true. */
34
35static int
Thomas Wouters23c9e002000-07-22 19:20:54 +000036my_fgets(char *buf, int len, FILE *fp)
Guido van Rossumfbd64c81997-02-18 21:53:32 +000037{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000038 char *p;
Victor Stinner52c950f2011-04-09 15:55:44 +020039 while (1) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000040 if (PyOS_InputHook != NULL)
41 (void)(PyOS_InputHook)();
42 errno = 0;
43 p = fgets(buf, len, fp);
44 if (p != NULL)
45 return 0; /* No error */
Mark Hammond2f10cb82002-07-14 23:12:29 +000046#ifdef MS_WINDOWS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000047 /* In the case of a Ctrl+C or some other external event
48 interrupting the operation:
49 Win2k/NT: ERROR_OPERATION_ABORTED is the most recent Win32
50 error code (and feof() returns TRUE).
51 Win9x: Ctrl+C seems to have no effect on fgets() returning
52 early - the signal handler is called, but the fgets()
53 only returns "normally" (ie, when Enter hit or feof())
54 */
55 if (GetLastError()==ERROR_OPERATION_ABORTED) {
56 /* Signals come asynchronously, so we sleep a brief
57 moment before checking if the handler has been
58 triggered (we cant just return 1 before the
59 signal handler has been called, as the later
60 signal may be treated as a separate interrupt).
61 */
62 Sleep(1);
63 if (PyOS_InterruptOccurred()) {
64 return 1; /* Interrupt */
65 }
66 /* Either the sleep wasn't long enough (need a
67 short loop retrying?) or not interrupted at all
68 (in which case we should revisit the whole thing!)
69 Logging some warning would be nice. assert is not
70 viable as under the debugger, the various dialogs
71 mean the condition is not true.
72 */
73 }
Mark Hammond2f10cb82002-07-14 23:12:29 +000074#endif /* MS_WINDOWS */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000075 if (feof(fp)) {
Victor Stinner4755ab02011-05-10 00:19:53 +020076 clearerr(fp);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000077 return -1; /* EOF */
78 }
Guido van Rossumfbd64c81997-02-18 21:53:32 +000079#ifdef EINTR
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000080 if (errno == EINTR) {
81 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +000082#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000083 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +000084#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000085 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +000086#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000087 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +000088#endif
Victor Stinner52c950f2011-04-09 15:55:44 +020089 if (s < 0)
90 return 1;
91 /* try again */
92 continue;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000093 }
Guido van Rossumfbd64c81997-02-18 21:53:32 +000094#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000095 if (PyOS_InterruptOccurred()) {
96 return 1; /* Interrupt */
97 }
98 return -2; /* Error */
99 }
100 /* NOTREACHED */
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000101}
102
103
104/* Readline implementation using fgets() */
105
106char *
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000107PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000108{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000109 size_t n;
110 char *p;
111 n = 100;
112 if ((p = (char *)PyMem_MALLOC(n)) == NULL)
113 return NULL;
114 fflush(sys_stdout);
115 if (prompt)
116 fprintf(stderr, "%s", prompt);
117 fflush(stderr);
118 switch (my_fgets(p, (int)n, sys_stdin)) {
119 case 0: /* Normal case */
120 break;
121 case 1: /* Interrupt */
122 PyMem_FREE(p);
123 return NULL;
124 case -1: /* EOF */
125 case -2: /* Error */
126 default: /* Shouldn't happen */
127 *p = '\0';
128 break;
129 }
130 n = strlen(p);
131 while (n > 0 && p[n-1] != '\n') {
132 size_t incr = n+2;
133 p = (char *)PyMem_REALLOC(p, n + incr);
134 if (p == NULL)
135 return NULL;
136 if (incr > INT_MAX) {
137 PyErr_SetString(PyExc_OverflowError, "input line too long");
138 }
139 if (my_fgets(p+n, (int)incr, sys_stdin) != 0)
140 break;
141 n += strlen(p+n);
142 }
143 return (char *)PyMem_REALLOC(p, n+1);
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000144}
145
146
147/* By initializing this function pointer, systems embedding Python can
Guido van Rossumb18618d2000-05-03 23:44:39 +0000148 override the readline function.
149
150 Note: Python expects in return a buffer allocated with PyMem_Malloc. */
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000151
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000152char *(*PyOS_ReadlineFunctionPointer)(FILE *, FILE *, char *);
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000153
154
155/* Interface used by tokenizer.c and bltinmodule.c */
156
157char *
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000158PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000159{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000160 char *rv;
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000161
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000162 if (_PyOS_ReadlineTState == PyThreadState_GET()) {
163 PyErr_SetString(PyExc_RuntimeError,
164 "can't re-enter readline");
165 return NULL;
166 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000167
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000168
169 if (PyOS_ReadlineFunctionPointer == NULL) {
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000170#ifdef __VMS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000171 PyOS_ReadlineFunctionPointer = vms__StdioReadline;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000172#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000173 PyOS_ReadlineFunctionPointer = PyOS_StdioReadline;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000174#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000175 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000176
Tim Petersb7e898a2004-07-07 20:42:07 +0000177#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000178 if (_PyOS_ReadlineLock == NULL) {
179 _PyOS_ReadlineLock = PyThread_allocate_lock();
180 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000181#endif
182
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000183 _PyOS_ReadlineTState = PyThreadState_GET();
184 Py_BEGIN_ALLOW_THREADS
185#ifdef WITH_THREAD
186 PyThread_acquire_lock(_PyOS_ReadlineLock, 1);
187#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000188
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000189 /* This is needed to handle the unlikely case that the
190 * interpreter is in interactive mode *and* stdin/out are not
191 * a tty. This can happen, for example if python is run like
192 * this: python -i < test1.py
193 */
194 if (!isatty (fileno (sys_stdin)) || !isatty (fileno (sys_stdout)))
195 rv = PyOS_StdioReadline (sys_stdin, sys_stdout, prompt);
196 else
197 rv = (*PyOS_ReadlineFunctionPointer)(sys_stdin, sys_stdout,
198 prompt);
199 Py_END_ALLOW_THREADS
200
201#ifdef WITH_THREAD
202 PyThread_release_lock(_PyOS_ReadlineLock);
203#endif
204
205 _PyOS_ReadlineTState = NULL;
206
207 return rv;
Guido van Rossum6fa63431993-12-24 10:36:57 +0000208}