blob: f0b51f8e1ff53e218073ddc926aadef2004cf0a1 [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 Pitrouf95a1b32010-05-09 15:52:27 +000038 char *p;
Tim Goldenb92b7572012-06-29 18:27:08 +010039 int i;
Antoine Pitrouc345ce12011-12-16 12:28:32 +010040 int err;
Victor Stinner52c950f2011-04-09 15:55:44 +020041 while (1) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000042 if (PyOS_InputHook != NULL)
43 (void)(PyOS_InputHook)();
44 errno = 0;
Victor Stinner4f711012011-05-30 23:46:00 +020045 clearerr(fp);
Martin v. Löwise654c112012-04-30 06:10:41 +020046 if (_PyVerify_fd(fileno(fp)))
47 p = fgets(buf, len, fp);
48 else
49 p = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000050 if (p != NULL)
51 return 0; /* No error */
Antoine Pitrouc345ce12011-12-16 12:28:32 +010052 err = errno;
Mark Hammond2f10cb82002-07-14 23:12:29 +000053#ifdef MS_WINDOWS
Tim Goldenb92b7572012-06-29 18:27:08 +010054 /* Ctrl-C anywhere on the line or Ctrl-Z if the only character
55 on a line will set ERROR_OPERATION_ABORTED. Under normal
56 circumstances Ctrl-C will also have caused the SIGINT handler
57 to fire. This signal fires in another thread and is not
58 guaranteed to have occurred before this point in the code.
59
60 Therefore: check in a small loop to see if the trigger has
61 fired, in which case assume this is a Ctrl-C event. If it
62 hasn't fired within 10ms assume that this is a Ctrl-Z on its
63 own or that the signal isn't going to fire for some other
64 reason and drop through to check for EOF.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000066 if (GetLastError()==ERROR_OPERATION_ABORTED) {
Tim Goldenb92b7572012-06-29 18:27:08 +010067 for (i = 0; i < 10; i++) {
68 if (PyOS_InterruptOccurred())
69 return 1;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000070 Sleep(1);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000071 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000072 }
Mark Hammond2f10cb82002-07-14 23:12:29 +000073#endif /* MS_WINDOWS */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000074 if (feof(fp)) {
Victor Stinner4755ab02011-05-10 00:19:53 +020075 clearerr(fp);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000076 return -1; /* EOF */
77 }
Guido van Rossumfbd64c81997-02-18 21:53:32 +000078#ifdef EINTR
Antoine Pitrouc345ce12011-12-16 12:28:32 +010079 if (err == EINTR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000080 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +000081#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000082 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +000083#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000084 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +000085#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000086 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +000087#endif
Victor Stinner52c950f2011-04-09 15:55:44 +020088 if (s < 0)
89 return 1;
Victor Stinnera870e352011-04-09 15:59:25 +020090 /* try again */
Victor Stinner52c950f2011-04-09 15:55:44 +020091 continue;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000092 }
Guido van Rossumfbd64c81997-02-18 21:53:32 +000093#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 if (PyOS_InterruptOccurred()) {
95 return 1; /* Interrupt */
96 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000097 return -2; /* Error */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000099 /* NOTREACHED */
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 Pitrouf95a1b32010-05-09 15:52:27 +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);
114 if (prompt)
115 fprintf(stderr, "%s", prompt);
116 fflush(stderr);
117 switch (my_fgets(p, (int)n, sys_stdin)) {
118 case 0: /* Normal case */
119 break;
120 case 1: /* Interrupt */
121 PyMem_FREE(p);
122 return NULL;
123 case -1: /* EOF */
124 case -2: /* Error */
125 default: /* Shouldn't happen */
126 *p = '\0';
127 break;
128 }
129 n = strlen(p);
130 while (n > 0 && p[n-1] != '\n') {
131 size_t incr = n+2;
132 p = (char *)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");
137 }
138 if (my_fgets(p+n, (int)incr, sys_stdin) != 0)
139 break;
140 n += strlen(p+n);
141 }
142 return (char *)PyMem_REALLOC(p, n+1);
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000143}
144
145
146/* By initializing this function pointer, systems embedding Python can
Guido van Rossumb18618d2000-05-03 23:44:39 +0000147 override the readline function.
148
149 Note: Python expects in return a buffer allocated with PyMem_Malloc. */
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000150
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000151char *(*PyOS_ReadlineFunctionPointer)(FILE *, FILE *, char *);
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000152
153
154/* Interface used by tokenizer.c and bltinmodule.c */
155
156char *
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000157PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 char *rv;
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000161 if (_PyOS_ReadlineTState == PyThreadState_GET()) {
162 PyErr_SetString(PyExc_RuntimeError,
163 "can't re-enter readline");
164 return NULL;
165 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167
168 if (PyOS_ReadlineFunctionPointer == NULL) {
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000169#ifdef __VMS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 PyOS_ReadlineFunctionPointer = vms__StdioReadline;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000171#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 PyOS_ReadlineFunctionPointer = PyOS_StdioReadline;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000173#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000175
Tim Petersb7e898a2004-07-07 20:42:07 +0000176#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 if (_PyOS_ReadlineLock == NULL) {
178 _PyOS_ReadlineLock = PyThread_allocate_lock();
179 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000180#endif
181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000182 _PyOS_ReadlineTState = PyThreadState_GET();
183 Py_BEGIN_ALLOW_THREADS
184#ifdef WITH_THREAD
185 PyThread_acquire_lock(_PyOS_ReadlineLock, 1);
186#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 /* This is needed to handle the unlikely case that the
189 * interpreter is in interactive mode *and* stdin/out are not
190 * a tty. This can happen, for example if python is run like
191 * this: python -i < test1.py
192 */
193 if (!isatty (fileno (sys_stdin)) || !isatty (fileno (sys_stdout)))
194 rv = PyOS_StdioReadline (sys_stdin, sys_stdout, prompt);
195 else
196 rv = (*PyOS_ReadlineFunctionPointer)(sys_stdin, sys_stdout,
197 prompt);
198 Py_END_ALLOW_THREADS
199
200#ifdef WITH_THREAD
201 PyThread_release_lock(_PyOS_ReadlineLock);
202#endif
203
204 _PyOS_ReadlineTState = NULL;
205
206 return rv;
Guido van Rossum6fa63431993-12-24 10:36:57 +0000207}