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" |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 13 | |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 14 | int (*PyOS_InputHook)(void) = NULL; |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 15 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 16 | #ifdef RISCOS |
| 17 | int Py_RISCOSWimpFlag; |
| 18 | #endif |
| 19 | |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 20 | /* This function restarts a fgets() after an EINTR error occurred |
| 21 | except if PyOS_InterruptOccurred() returns true. */ |
| 22 | |
| 23 | static int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 24 | my_fgets(char *buf, int len, FILE *fp) |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 25 | { |
| 26 | char *p; |
| 27 | for (;;) { |
Guido van Rossum | 4462064 | 1997-08-11 18:57:29 +0000 | [diff] [blame] | 28 | if (PyOS_InputHook != NULL) |
| 29 | (void)(PyOS_InputHook)(); |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 30 | errno = 0; |
| 31 | p = fgets(buf, len, fp); |
| 32 | if (p != NULL) |
| 33 | return 0; /* No error */ |
| 34 | if (feof(fp)) { |
| 35 | return -1; /* EOF */ |
| 36 | } |
| 37 | #ifdef EINTR |
| 38 | if (errno == EINTR) { |
| 39 | if (PyOS_InterruptOccurred()) { |
| 40 | return 1; /* Interrupt */ |
| 41 | } |
| 42 | continue; |
| 43 | } |
| 44 | #endif |
| 45 | if (PyOS_InterruptOccurred()) { |
| 46 | return 1; /* Interrupt */ |
| 47 | } |
| 48 | return -2; /* Error */ |
| 49 | } |
| 50 | /* NOTREACHED */ |
| 51 | } |
| 52 | |
| 53 | |
| 54 | /* Readline implementation using fgets() */ |
| 55 | |
| 56 | char * |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 57 | PyOS_StdioReadline(char *prompt) |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 58 | { |
Guido van Rossum | 6da3434 | 2000-06-28 22:00:02 +0000 | [diff] [blame] | 59 | size_t n; |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 60 | char *p; |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 61 | n = 100; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 62 | if ((p = PyMem_MALLOC(n)) == NULL) |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 63 | return NULL; |
Guido van Rossum | c7fea2f | 1996-01-12 01:30:55 +0000 | [diff] [blame] | 64 | fflush(stdout); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 65 | #ifndef RISCOS |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 66 | if (prompt) |
| 67 | fprintf(stderr, "%s", prompt); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 68 | #else |
| 69 | if (prompt) { |
| 70 | if(Py_RISCOSWimpFlag) |
| 71 | fprintf(stderr, "\x0cr%s\x0c", prompt); |
| 72 | else |
| 73 | fprintf(stderr, "%s", prompt); |
| 74 | } |
| 75 | #endif |
Guido van Rossum | c7fea2f | 1996-01-12 01:30:55 +0000 | [diff] [blame] | 76 | fflush(stderr); |
Guido van Rossum | 6da3434 | 2000-06-28 22:00:02 +0000 | [diff] [blame] | 77 | switch (my_fgets(p, (int)n, stdin)) { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 78 | case 0: /* Normal case */ |
| 79 | break; |
| 80 | case 1: /* Interrupt */ |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 81 | PyMem_FREE(p); |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 82 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 83 | case -1: /* EOF */ |
| 84 | case -2: /* Error */ |
| 85 | default: /* Shouldn't happen */ |
| 86 | *p = '\0'; |
| 87 | break; |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 88 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 89 | #ifdef MPW |
| 90 | /* Hack for MPW C where the prompt comes right back in the input */ |
| 91 | /* XXX (Actually this would be rather nice on most systems...) */ |
| 92 | n = strlen(prompt); |
| 93 | if (strncmp(p, prompt, n) == 0) |
| 94 | memmove(p, p + n, strlen(p) - n + 1); |
| 95 | #endif |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 96 | n = strlen(p); |
| 97 | while (n > 0 && p[n-1] != '\n') { |
Guido van Rossum | 6da3434 | 2000-06-28 22:00:02 +0000 | [diff] [blame] | 98 | size_t incr = n+2; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 99 | p = PyMem_REALLOC(p, n + incr); |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 100 | if (p == NULL) |
| 101 | return NULL; |
Guido van Rossum | 6da3434 | 2000-06-28 22:00:02 +0000 | [diff] [blame] | 102 | if (incr > INT_MAX) { |
| 103 | PyErr_SetString(PyExc_OverflowError, "input line too long"); |
| 104 | } |
| 105 | if (my_fgets(p+n, (int)incr, stdin) != 0) |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 106 | break; |
| 107 | n += strlen(p+n); |
| 108 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 109 | return PyMem_REALLOC(p, n+1); |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | |
| 113 | /* By initializing this function pointer, systems embedding Python can |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 114 | override the readline function. |
| 115 | |
| 116 | Note: Python expects in return a buffer allocated with PyMem_Malloc. */ |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 117 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 118 | char *(*PyOS_ReadlineFunctionPointer)(char *); |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 119 | |
| 120 | |
| 121 | /* Interface used by tokenizer.c and bltinmodule.c */ |
| 122 | |
| 123 | char * |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 124 | PyOS_Readline(char *prompt) |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 125 | { |
Guido van Rossum | 80c7bcf | 1998-08-29 16:03:27 +0000 | [diff] [blame] | 126 | char *rv; |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 127 | if (PyOS_ReadlineFunctionPointer == NULL) { |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 128 | PyOS_ReadlineFunctionPointer = PyOS_StdioReadline; |
| 129 | } |
Guido van Rossum | 8efa47b | 1998-08-27 19:43:43 +0000 | [diff] [blame] | 130 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 80c7bcf | 1998-08-29 16:03:27 +0000 | [diff] [blame] | 131 | rv = (*PyOS_ReadlineFunctionPointer)(prompt); |
Guido van Rossum | 8efa47b | 1998-08-27 19:43:43 +0000 | [diff] [blame] | 132 | Py_END_ALLOW_THREADS |
Guido van Rossum | 80c7bcf | 1998-08-29 16:03:27 +0000 | [diff] [blame] | 133 | return rv; |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 134 | } |