blob: 07c1d440e0717f60cc9a9557923b600b8f2d6aba [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;
Victor Stinner5de51ac2011-04-09 16:09:08 +020043 while (1) {
44 if (PyOS_InputHook != NULL)
45 (void)(PyOS_InputHook)();
46 errno = 0;
Victor Stinner08563d92011-05-30 23:44:13 +020047 clearerr(fp);
Victor Stinner5de51ac2011-04-09 16:09:08 +020048 p = fgets(buf, len, fp);
49 if (p != NULL)
50 return 0; /* No error */
Mark Hammond2f10cb82002-07-14 23:12:29 +000051#ifdef MS_WINDOWS
Victor Stinner5de51ac2011-04-09 16:09:08 +020052 /* In the case of a Ctrl+C or some other external event
53 interrupting the operation:
54 Win2k/NT: ERROR_OPERATION_ABORTED is the most recent Win32
55 error code (and feof() returns TRUE).
56 Win9x: Ctrl+C seems to have no effect on fgets() returning
57 early - the signal handler is called, but the fgets()
58 only returns "normally" (ie, when Enter hit or feof())
Antoine Pitrouc83ea132010-05-09 14:46:46 +000059 */
Victor Stinner5de51ac2011-04-09 16:09:08 +020060 if (GetLastError()==ERROR_OPERATION_ABORTED) {
61 /* Signals come asynchronously, so we sleep a brief
62 moment before checking if the handler has been
63 triggered (we cant just return 1 before the
64 signal handler has been called, as the later
65 signal may be treated as a separate interrupt).
66 */
67 Sleep(1);
68 if (PyOS_InterruptOccurred()) {
69 return 1; /* Interrupt */
70 }
71 /* Either the sleep wasn't long enough (need a
72 short loop retrying?) or not interrupted at all
73 (in which case we should revisit the whole thing!)
74 Logging some warning would be nice. assert is not
75 viable as under the debugger, the various dialogs
76 mean the condition is not true.
77 */
78 }
79#endif /* MS_WINDOWS */
80 if (feof(fp)) {
Victor Stinner2c585f62011-05-10 00:22:59 +020081 clearerr(fp);
Victor Stinner5de51ac2011-04-09 16:09:08 +020082 return -1; /* EOF */
83 }
84#ifdef EINTR
85 if (errno == EINTR) {
86 int s;
87#ifdef WITH_THREAD
88 PyEval_RestoreThread(_PyOS_ReadlineTState);
89#endif
90 s = PyErr_CheckSignals();
91#ifdef WITH_THREAD
92 PyEval_SaveThread();
93#endif
94 if (s < 0)
95 return 1;
96 /* try again */
97 continue;
98 }
99#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000100 if (PyOS_InterruptOccurred()) {
101 return 1; /* Interrupt */
102 }
Victor Stinner5de51ac2011-04-09 16:09:08 +0200103 return -2; /* Error */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000104 }
Victor Stinner5de51ac2011-04-09 16:09:08 +0200105 /* NOTREACHED */
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000106}
107
108
109/* Readline implementation using fgets() */
110
111char *
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000112PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000113{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000114 size_t n;
115 char *p;
116 n = 100;
117 if ((p = (char *)PyMem_MALLOC(n)) == NULL)
118 return NULL;
119 fflush(sys_stdout);
Guido van Rossum48a680c2001-03-02 06:34:14 +0000120#ifndef RISCOS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000121 if (prompt)
122 fprintf(stderr, "%s", prompt);
Guido van Rossum48a680c2001-03-02 06:34:14 +0000123#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000124 if (prompt) {
125 if(Py_RISCOSWimpFlag)
126 fprintf(stderr, "\x0cr%s\x0c", prompt);
127 else
128 fprintf(stderr, "%s", prompt);
129 }
Guido van Rossum48a680c2001-03-02 06:34:14 +0000130#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000131 fflush(stderr);
132 switch (my_fgets(p, (int)n, sys_stdin)) {
133 case 0: /* Normal case */
134 break;
135 case 1: /* Interrupt */
136 PyMem_FREE(p);
137 return NULL;
138 case -1: /* EOF */
139 case -2: /* Error */
140 default: /* Shouldn't happen */
141 *p = '\0';
142 break;
143 }
144 n = strlen(p);
145 while (n > 0 && p[n-1] != '\n') {
146 size_t incr = n+2;
147 p = (char *)PyMem_REALLOC(p, n + incr);
148 if (p == NULL)
149 return NULL;
150 if (incr > INT_MAX) {
151 PyErr_SetString(PyExc_OverflowError, "input line too long");
152 }
153 if (my_fgets(p+n, (int)incr, sys_stdin) != 0)
154 break;
155 n += strlen(p+n);
156 }
157 return (char *)PyMem_REALLOC(p, n+1);
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000158}
159
160
161/* By initializing this function pointer, systems embedding Python can
Guido van Rossumb18618d2000-05-03 23:44:39 +0000162 override the readline function.
163
Neal Norwitz08062d62006-04-11 08:19:15 +0000164 Note: Python expects in return a buffer allocated with PyMem_Malloc. */
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000165
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000166char *(*PyOS_ReadlineFunctionPointer)(FILE *, FILE *, char *);
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000167
168
169/* Interface used by tokenizer.c and bltinmodule.c */
170
171char *
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000172PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000173{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000174 char *rv;
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000175
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000176 if (_PyOS_ReadlineTState == PyThreadState_GET()) {
177 PyErr_SetString(PyExc_RuntimeError,
178 "can't re-enter readline");
179 return NULL;
180 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000181
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000182
183 if (PyOS_ReadlineFunctionPointer == NULL) {
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000184#ifdef __VMS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000185 PyOS_ReadlineFunctionPointer = vms__StdioReadline;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000186#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000187 PyOS_ReadlineFunctionPointer = PyOS_StdioReadline;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000188#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000189 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000190
Tim Petersb7e898a2004-07-07 20:42:07 +0000191#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000192 if (_PyOS_ReadlineLock == NULL) {
193 _PyOS_ReadlineLock = PyThread_allocate_lock();
194 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000195#endif
196
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000197 _PyOS_ReadlineTState = PyThreadState_GET();
198 Py_BEGIN_ALLOW_THREADS
199#ifdef WITH_THREAD
200 PyThread_acquire_lock(_PyOS_ReadlineLock, 1);
201#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000202
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000203 /* This is needed to handle the unlikely case that the
204 * interpreter is in interactive mode *and* stdin/out are not
205 * a tty. This can happen, for example if python is run like
206 * this: python -i < test1.py
207 */
208 if (!isatty (fileno (sys_stdin)) || !isatty (fileno (sys_stdout)))
209 rv = PyOS_StdioReadline (sys_stdin, sys_stdout, prompt);
210 else
211 rv = (*PyOS_ReadlineFunctionPointer)(sys_stdin, sys_stdout,
212 prompt);
213 Py_END_ALLOW_THREADS
214
215#ifdef WITH_THREAD
216 PyThread_release_lock(_PyOS_ReadlineLock);
217#endif
218
219 _PyOS_ReadlineTState = NULL;
220
221 return rv;
Guido van Rossum6fa63431993-12-24 10:36:57 +0000222}