blob: 0d8394b5be2043a3e806c4477c42db837c7b9e6e [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{
38 char *p;
Benjamin Peterson21896a32010-03-21 22:03:03 +000039 if (PyOS_InputHook != NULL)
40 (void)(PyOS_InputHook)();
41 errno = 0;
42 p = fgets(buf, len, fp);
43 if (p != NULL)
44 return 0; /* No error */
Mark Hammond2f10cb82002-07-14 23:12:29 +000045#ifdef MS_WINDOWS
Benjamin Peterson21896a32010-03-21 22:03:03 +000046 /* In the case of a Ctrl+C or some other external event
47 interrupting the operation:
48 Win2k/NT: ERROR_OPERATION_ABORTED is the most recent Win32
49 error code (and feof() returns TRUE).
50 Win9x: Ctrl+C seems to have no effect on fgets() returning
51 early - the signal handler is called, but the fgets()
52 only returns "normally" (ie, when Enter hit or feof())
53 */
54 if (GetLastError()==ERROR_OPERATION_ABORTED) {
55 /* Signals come asynchronously, so we sleep a brief
56 moment before checking if the handler has been
57 triggered (we cant just return 1 before the
58 signal handler has been called, as the later
59 signal may be treated as a separate interrupt).
Mark Hammond2f10cb82002-07-14 23:12:29 +000060 */
Benjamin Peterson21896a32010-03-21 22:03:03 +000061 Sleep(1);
Guido van Rossumfbd64c81997-02-18 21:53:32 +000062 if (PyOS_InterruptOccurred()) {
63 return 1; /* Interrupt */
64 }
Benjamin Peterson21896a32010-03-21 22:03:03 +000065 /* Either the sleep wasn't long enough (need a
66 short loop retrying?) or not interrupted at all
67 (in which case we should revisit the whole thing!)
68 Logging some warning would be nice. assert is not
69 viable as under the debugger, the various dialogs
70 mean the condition is not true.
71 */
Guido van Rossumfbd64c81997-02-18 21:53:32 +000072 }
Benjamin Peterson21896a32010-03-21 22:03:03 +000073#endif /* MS_WINDOWS */
74 if (feof(fp)) {
75 return -1; /* EOF */
76 }
77#ifdef EINTR
78 if (errno == EINTR) {
79 int s;
80#ifdef WITH_THREAD
81 PyEval_RestoreThread(_PyOS_ReadlineTState);
82#endif
83 s = PyErr_CheckSignals();
84#ifdef WITH_THREAD
85 PyEval_SaveThread();
86#endif
87 if (s < 0) {
88 return 1;
89 }
90 }
91#endif
92 if (PyOS_InterruptOccurred()) {
93 return 1; /* Interrupt */
94 }
95 return -2; /* Error */
Guido van Rossumfbd64c81997-02-18 21:53:32 +000096}
97
98
99/* Readline implementation using fgets() */
100
101char *
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000102PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000103{
Guido van Rossum6da34342000-06-28 22:00:02 +0000104 size_t n;
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000105 char *p;
Guido van Rossum6fa63431993-12-24 10:36:57 +0000106 n = 100;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000107 if ((p = (char *)PyMem_MALLOC(n)) == NULL)
Guido van Rossum6fa63431993-12-24 10:36:57 +0000108 return NULL;
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000109 fflush(sys_stdout);
Guido van Rossum6fa63431993-12-24 10:36:57 +0000110 if (prompt)
111 fprintf(stderr, "%s", prompt);
Guido van Rossumc7fea2f1996-01-12 01:30:55 +0000112 fflush(stderr);
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000113 switch (my_fgets(p, (int)n, sys_stdin)) {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000114 case 0: /* Normal case */
115 break;
116 case 1: /* Interrupt */
Guido van Rossumb18618d2000-05-03 23:44:39 +0000117 PyMem_FREE(p);
Guido van Rossum6fa63431993-12-24 10:36:57 +0000118 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000119 case -1: /* EOF */
120 case -2: /* Error */
121 default: /* Shouldn't happen */
122 *p = '\0';
123 break;
Guido van Rossum6fa63431993-12-24 10:36:57 +0000124 }
125 n = strlen(p);
126 while (n > 0 && p[n-1] != '\n') {
Guido van Rossum6da34342000-06-28 22:00:02 +0000127 size_t incr = n+2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000128 p = (char *)PyMem_REALLOC(p, n + incr);
Guido van Rossum6fa63431993-12-24 10:36:57 +0000129 if (p == NULL)
130 return NULL;
Guido van Rossum6da34342000-06-28 22:00:02 +0000131 if (incr > INT_MAX) {
132 PyErr_SetString(PyExc_OverflowError, "input line too long");
133 }
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000134 if (my_fgets(p+n, (int)incr, sys_stdin) != 0)
Guido van Rossum6fa63431993-12-24 10:36:57 +0000135 break;
136 n += strlen(p+n);
137 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000138 return (char *)PyMem_REALLOC(p, n+1);
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000139}
140
141
142/* By initializing this function pointer, systems embedding Python can
Guido van Rossumb18618d2000-05-03 23:44:39 +0000143 override the readline function.
144
145 Note: Python expects in return a buffer allocated with PyMem_Malloc. */
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000146
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000147char *(*PyOS_ReadlineFunctionPointer)(FILE *, FILE *, char *);
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000148
149
150/* Interface used by tokenizer.c and bltinmodule.c */
151
152char *
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000153PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000154{
Guido van Rossum80c7bcf1998-08-29 16:03:27 +0000155 char *rv;
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000156
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000157 if (_PyOS_ReadlineTState == PyThreadState_GET()) {
158 PyErr_SetString(PyExc_RuntimeError,
159 "can't re-enter readline");
160 return NULL;
161 }
162
163
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000164 if (PyOS_ReadlineFunctionPointer == NULL) {
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000165#ifdef __VMS
166 PyOS_ReadlineFunctionPointer = vms__StdioReadline;
167#else
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000168 PyOS_ReadlineFunctionPointer = PyOS_StdioReadline;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000169#endif
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000170 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000171
Tim Petersb7e898a2004-07-07 20:42:07 +0000172#ifdef WITH_THREAD
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000173 if (_PyOS_ReadlineLock == NULL) {
174 _PyOS_ReadlineLock = PyThread_allocate_lock();
175 }
176#endif
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000177
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000178 _PyOS_ReadlineTState = PyThreadState_GET();
Guido van Rossum8efa47b1998-08-27 19:43:43 +0000179 Py_BEGIN_ALLOW_THREADS
Tim Petersb7e898a2004-07-07 20:42:07 +0000180#ifdef WITH_THREAD
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000181 PyThread_acquire_lock(_PyOS_ReadlineLock, 1);
182#endif
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000183
184 /* This is needed to handle the unlikely case that the
185 * interpreter is in interactive mode *and* stdin/out are not
186 * a tty. This can happen, for example if python is run like
187 * this: python -i < test1.py
188 */
189 if (!isatty (fileno (sys_stdin)) || !isatty (fileno (sys_stdout)))
190 rv = PyOS_StdioReadline (sys_stdin, sys_stdout, prompt);
191 else
192 rv = (*PyOS_ReadlineFunctionPointer)(sys_stdin, sys_stdout,
193 prompt);
Guido van Rossum8efa47b1998-08-27 19:43:43 +0000194 Py_END_ALLOW_THREADS
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000195
Tim Petersb7e898a2004-07-07 20:42:07 +0000196#ifdef WITH_THREAD
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000197 PyThread_release_lock(_PyOS_ReadlineLock);
198#endif
199
200 _PyOS_ReadlineTState = NULL;
201
Guido van Rossum80c7bcf1998-08-29 16:03:27 +0000202 return rv;
Guido van Rossum6fa63431993-12-24 10:36:57 +0000203}