Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 2 | /* 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 Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 6 | 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 Rossum | 8efa47b | 1998-08-27 19:43:43 +0000 | [diff] [blame] | 12 | #include "Python.h" |
Mark Hammond | 2f10cb8 | 2002-07-14 23:12:29 +0000 | [diff] [blame] | 13 | #ifdef MS_WINDOWS |
| 14 | #define WIN32_LEAN_AND_MEAN |
| 15 | #include "windows.h" |
| 16 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 17 | |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 18 | #ifdef __VMS |
| 19 | extern char* vms__StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt); |
| 20 | #endif |
| 21 | |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 22 | int (*PyOS_InputHook)(void) = NULL; |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 24 | #ifdef RISCOS |
| 25 | int Py_RISCOSWimpFlag; |
| 26 | #endif |
| 27 | |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 28 | /* This function restarts a fgets() after an EINTR error occurred |
| 29 | except if PyOS_InterruptOccurred() returns true. */ |
| 30 | |
| 31 | static int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 32 | my_fgets(char *buf, int len, FILE *fp) |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 33 | { |
| 34 | char *p; |
| 35 | for (;;) { |
Guido van Rossum | 4462064 | 1997-08-11 18:57:29 +0000 | [diff] [blame] | 36 | if (PyOS_InputHook != NULL) |
| 37 | (void)(PyOS_InputHook)(); |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 38 | errno = 0; |
| 39 | p = fgets(buf, len, fp); |
| 40 | if (p != NULL) |
| 41 | return 0; /* No error */ |
Mark Hammond | 2f10cb8 | 2002-07-14 23:12:29 +0000 | [diff] [blame] | 42 | #ifdef MS_WINDOWS |
| 43 | /* In the case of a Ctrl+C or some other external event |
| 44 | interrupting the operation: |
| 45 | Win2k/NT: ERROR_OPERATION_ABORTED is the most recent Win32 |
| 46 | error code (and feof() returns TRUE). |
| 47 | Win9x: Ctrl+C seems to have no effect on fgets() returning |
| 48 | early - the signal handler is called, but the fgets() |
| 49 | only returns "normally" (ie, when Enter hit or feof()) |
| 50 | */ |
| 51 | if (GetLastError()==ERROR_OPERATION_ABORTED) { |
| 52 | /* Signals come asynchronously, so we sleep a brief |
| 53 | moment before checking if the handler has been |
| 54 | triggered (we cant just return 1 before the |
| 55 | signal handler has been called, as the later |
| 56 | signal may be treated as a separate interrupt). |
| 57 | */ |
| 58 | Sleep(1); |
| 59 | if (PyOS_InterruptOccurred()) { |
| 60 | return 1; /* Interrupt */ |
| 61 | } |
| 62 | /* Either the sleep wasn't long enough (need a |
| 63 | short loop retrying?) or not interrupted at all |
| 64 | (in which case we should revisit the whole thing!) |
| 65 | Logging some warning would be nice. assert is not |
| 66 | viable as under the debugger, the various dialogs |
| 67 | mean the condition is not true. |
| 68 | */ |
| 69 | } |
| 70 | #endif /* MS_WINDOWS */ |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 71 | if (feof(fp)) { |
| 72 | return -1; /* EOF */ |
| 73 | } |
| 74 | #ifdef EINTR |
| 75 | if (errno == EINTR) { |
| 76 | if (PyOS_InterruptOccurred()) { |
| 77 | return 1; /* Interrupt */ |
| 78 | } |
| 79 | continue; |
| 80 | } |
| 81 | #endif |
| 82 | if (PyOS_InterruptOccurred()) { |
| 83 | return 1; /* Interrupt */ |
| 84 | } |
| 85 | return -2; /* Error */ |
| 86 | } |
| 87 | /* NOTREACHED */ |
| 88 | } |
| 89 | |
| 90 | |
| 91 | /* Readline implementation using fgets() */ |
| 92 | |
| 93 | char * |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 94 | PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt) |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 95 | { |
Guido van Rossum | 6da3434 | 2000-06-28 22:00:02 +0000 | [diff] [blame] | 96 | size_t n; |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 97 | char *p; |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 98 | n = 100; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 99 | if ((p = PyMem_MALLOC(n)) == NULL) |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 100 | return NULL; |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 101 | fflush(sys_stdout); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 102 | #ifndef RISCOS |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 103 | if (prompt) |
| 104 | fprintf(stderr, "%s", prompt); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 105 | #else |
| 106 | if (prompt) { |
| 107 | if(Py_RISCOSWimpFlag) |
| 108 | fprintf(stderr, "\x0cr%s\x0c", prompt); |
| 109 | else |
| 110 | fprintf(stderr, "%s", prompt); |
| 111 | } |
| 112 | #endif |
Guido van Rossum | c7fea2f | 1996-01-12 01:30:55 +0000 | [diff] [blame] | 113 | fflush(stderr); |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 114 | switch (my_fgets(p, (int)n, sys_stdin)) { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 115 | case 0: /* Normal case */ |
| 116 | break; |
| 117 | case 1: /* Interrupt */ |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 118 | PyMem_FREE(p); |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 119 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 120 | case -1: /* EOF */ |
| 121 | case -2: /* Error */ |
| 122 | default: /* Shouldn't happen */ |
| 123 | *p = '\0'; |
| 124 | break; |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 125 | } |
| 126 | n = strlen(p); |
| 127 | while (n > 0 && p[n-1] != '\n') { |
Guido van Rossum | 6da3434 | 2000-06-28 22:00:02 +0000 | [diff] [blame] | 128 | size_t incr = n+2; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 129 | p = PyMem_REALLOC(p, n + incr); |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 130 | if (p == NULL) |
| 131 | return NULL; |
Guido van Rossum | 6da3434 | 2000-06-28 22:00:02 +0000 | [diff] [blame] | 132 | if (incr > INT_MAX) { |
| 133 | PyErr_SetString(PyExc_OverflowError, "input line too long"); |
| 134 | } |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 135 | if (my_fgets(p+n, (int)incr, sys_stdin) != 0) |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 136 | break; |
| 137 | n += strlen(p+n); |
| 138 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 139 | return PyMem_REALLOC(p, n+1); |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | |
| 143 | /* By initializing this function pointer, systems embedding Python can |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 144 | override the readline function. |
| 145 | |
| 146 | Note: Python expects in return a buffer allocated with PyMem_Malloc. */ |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 147 | |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 148 | char *(*PyOS_ReadlineFunctionPointer)(FILE *, FILE *, char *); |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 149 | |
| 150 | |
| 151 | /* Interface used by tokenizer.c and bltinmodule.c */ |
| 152 | |
| 153 | char * |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 154 | PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt) |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 155 | { |
Guido van Rossum | 80c7bcf | 1998-08-29 16:03:27 +0000 | [diff] [blame] | 156 | char *rv; |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 157 | |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 158 | if (PyOS_ReadlineFunctionPointer == NULL) { |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 159 | #ifdef __VMS |
| 160 | PyOS_ReadlineFunctionPointer = vms__StdioReadline; |
| 161 | #else |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 162 | PyOS_ReadlineFunctionPointer = PyOS_StdioReadline; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 163 | #endif |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 164 | } |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 165 | |
Guido van Rossum | 8efa47b | 1998-08-27 19:43:43 +0000 | [diff] [blame] | 166 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 167 | |
| 168 | /* This is needed to handle the unlikely case that the |
| 169 | * interpreter is in interactive mode *and* stdin/out are not |
| 170 | * a tty. This can happen, for example if python is run like |
| 171 | * this: python -i < test1.py |
| 172 | */ |
| 173 | if (!isatty (fileno (sys_stdin)) || !isatty (fileno (sys_stdout))) |
| 174 | rv = PyOS_StdioReadline (sys_stdin, sys_stdout, prompt); |
| 175 | else |
| 176 | rv = (*PyOS_ReadlineFunctionPointer)(sys_stdin, sys_stdout, |
| 177 | prompt); |
Guido van Rossum | 8efa47b | 1998-08-27 19:43:43 +0000 | [diff] [blame] | 178 | Py_END_ALLOW_THREADS |
Guido van Rossum | 80c7bcf | 1998-08-29 16:03:27 +0000 | [diff] [blame] | 179 | return rv; |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 180 | } |