blob: 8a76e0c23b4cc889d93b25baa841dfaeb2558121 [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;
47 p = fgets(buf, len, fp);
48 if (p != NULL)
49 return 0; /* No error */
Mark Hammond2f10cb82002-07-14 23:12:29 +000050#ifdef MS_WINDOWS
Victor Stinner5de51ac2011-04-09 16:09:08 +020051 /* In the case of a Ctrl+C or some other external event
52 interrupting the operation:
53 Win2k/NT: ERROR_OPERATION_ABORTED is the most recent Win32
54 error code (and feof() returns TRUE).
55 Win9x: Ctrl+C seems to have no effect on fgets() returning
56 early - the signal handler is called, but the fgets()
57 only returns "normally" (ie, when Enter hit or feof())
Antoine Pitrouc83ea132010-05-09 14:46:46 +000058 */
Victor Stinner5de51ac2011-04-09 16:09:08 +020059 if (GetLastError()==ERROR_OPERATION_ABORTED) {
60 /* Signals come asynchronously, so we sleep a brief
61 moment before checking if the handler has been
62 triggered (we cant just return 1 before the
63 signal handler has been called, as the later
64 signal may be treated as a separate interrupt).
65 */
66 Sleep(1);
67 if (PyOS_InterruptOccurred()) {
68 return 1; /* Interrupt */
69 }
70 /* Either the sleep wasn't long enough (need a
71 short loop retrying?) or not interrupted at all
72 (in which case we should revisit the whole thing!)
73 Logging some warning would be nice. assert is not
74 viable as under the debugger, the various dialogs
75 mean the condition is not true.
76 */
77 }
78#endif /* MS_WINDOWS */
79 if (feof(fp)) {
80 return -1; /* EOF */
81 }
82#ifdef EINTR
83 if (errno == EINTR) {
84 int s;
85#ifdef WITH_THREAD
86 PyEval_RestoreThread(_PyOS_ReadlineTState);
87#endif
88 s = PyErr_CheckSignals();
89#ifdef WITH_THREAD
90 PyEval_SaveThread();
91#endif
92 if (s < 0)
93 return 1;
94 /* try again */
95 continue;
96 }
97#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +000098 if (PyOS_InterruptOccurred()) {
99 return 1; /* Interrupt */
100 }
Victor Stinner5de51ac2011-04-09 16:09:08 +0200101 return -2; /* Error */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000102 }
Victor Stinner5de51ac2011-04-09 16:09:08 +0200103 /* NOTREACHED */
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000104}
105
106
107/* Readline implementation using fgets() */
108
109char *
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000110PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000111{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000112 size_t n;
113 char *p;
114 n = 100;
115 if ((p = (char *)PyMem_MALLOC(n)) == NULL)
116 return NULL;
117 fflush(sys_stdout);
Guido van Rossum48a680c2001-03-02 06:34:14 +0000118#ifndef RISCOS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000119 if (prompt)
120 fprintf(stderr, "%s", prompt);
Guido van Rossum48a680c2001-03-02 06:34:14 +0000121#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000122 if (prompt) {
123 if(Py_RISCOSWimpFlag)
124 fprintf(stderr, "\x0cr%s\x0c", prompt);
125 else
126 fprintf(stderr, "%s", prompt);
127 }
Guido van Rossum48a680c2001-03-02 06:34:14 +0000128#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000129 fflush(stderr);
130 switch (my_fgets(p, (int)n, sys_stdin)) {
131 case 0: /* Normal case */
132 break;
133 case 1: /* Interrupt */
134 PyMem_FREE(p);
135 return NULL;
136 case -1: /* EOF */
137 case -2: /* Error */
138 default: /* Shouldn't happen */
139 *p = '\0';
140 break;
141 }
142 n = strlen(p);
143 while (n > 0 && p[n-1] != '\n') {
144 size_t incr = n+2;
145 p = (char *)PyMem_REALLOC(p, n + incr);
146 if (p == NULL)
147 return NULL;
148 if (incr > INT_MAX) {
149 PyErr_SetString(PyExc_OverflowError, "input line too long");
150 }
151 if (my_fgets(p+n, (int)incr, sys_stdin) != 0)
152 break;
153 n += strlen(p+n);
154 }
155 return (char *)PyMem_REALLOC(p, n+1);
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000156}
157
158
159/* By initializing this function pointer, systems embedding Python can
Guido van Rossumb18618d2000-05-03 23:44:39 +0000160 override the readline function.
161
Neal Norwitz08062d62006-04-11 08:19:15 +0000162 Note: Python expects in return a buffer allocated with PyMem_Malloc. */
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000163
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000164char *(*PyOS_ReadlineFunctionPointer)(FILE *, FILE *, char *);
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000165
166
167/* Interface used by tokenizer.c and bltinmodule.c */
168
169char *
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000170PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000171{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000172 char *rv;
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000173
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000174 if (_PyOS_ReadlineTState == PyThreadState_GET()) {
175 PyErr_SetString(PyExc_RuntimeError,
176 "can't re-enter readline");
177 return NULL;
178 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000179
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000180
181 if (PyOS_ReadlineFunctionPointer == NULL) {
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000182#ifdef __VMS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000183 PyOS_ReadlineFunctionPointer = vms__StdioReadline;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000184#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000185 PyOS_ReadlineFunctionPointer = PyOS_StdioReadline;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000186#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000187 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000188
Tim Petersb7e898a2004-07-07 20:42:07 +0000189#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000190 if (_PyOS_ReadlineLock == NULL) {
191 _PyOS_ReadlineLock = PyThread_allocate_lock();
192 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000193#endif
194
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000195 _PyOS_ReadlineTState = PyThreadState_GET();
196 Py_BEGIN_ALLOW_THREADS
197#ifdef WITH_THREAD
198 PyThread_acquire_lock(_PyOS_ReadlineLock, 1);
199#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000200
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000201 /* This is needed to handle the unlikely case that the
202 * interpreter is in interactive mode *and* stdin/out are not
203 * a tty. This can happen, for example if python is run like
204 * this: python -i < test1.py
205 */
206 if (!isatty (fileno (sys_stdin)) || !isatty (fileno (sys_stdout)))
207 rv = PyOS_StdioReadline (sys_stdin, sys_stdout, prompt);
208 else
209 rv = (*PyOS_ReadlineFunctionPointer)(sys_stdin, sys_stdout,
210 prompt);
211 Py_END_ALLOW_THREADS
212
213#ifdef WITH_THREAD
214 PyThread_release_lock(_PyOS_ReadlineLock);
215#endif
216
217 _PyOS_ReadlineTState = NULL;
218
219 return rv;
Guido van Rossum6fa63431993-12-24 10:36:57 +0000220}