blob: 10e39e53685b8edf629af75269e67d2406a7c3a8 [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"
Guido van Rossum6fa63431993-12-24 10:36:57 +000013
Thomas Wouters23c9e002000-07-22 19:20:54 +000014int (*PyOS_InputHook)(void) = NULL;
Guido van Rossumfbd64c81997-02-18 21:53:32 +000015
Guido van Rossum48a680c2001-03-02 06:34:14 +000016#ifdef RISCOS
17int Py_RISCOSWimpFlag;
18#endif
19
Guido van Rossumfbd64c81997-02-18 21:53:32 +000020/* This function restarts a fgets() after an EINTR error occurred
21 except if PyOS_InterruptOccurred() returns true. */
22
23static int
Thomas Wouters23c9e002000-07-22 19:20:54 +000024my_fgets(char *buf, int len, FILE *fp)
Guido van Rossumfbd64c81997-02-18 21:53:32 +000025{
26 char *p;
27 for (;;) {
Guido van Rossum44620641997-08-11 18:57:29 +000028 if (PyOS_InputHook != NULL)
29 (void)(PyOS_InputHook)();
Guido van Rossumfbd64c81997-02-18 21:53:32 +000030 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
56char *
Thomas Wouters23c9e002000-07-22 19:20:54 +000057PyOS_StdioReadline(char *prompt)
Guido van Rossumfbd64c81997-02-18 21:53:32 +000058{
Guido van Rossum6da34342000-06-28 22:00:02 +000059 size_t n;
Guido van Rossumfbd64c81997-02-18 21:53:32 +000060 char *p;
Guido van Rossum6fa63431993-12-24 10:36:57 +000061 n = 100;
Guido van Rossumb18618d2000-05-03 23:44:39 +000062 if ((p = PyMem_MALLOC(n)) == NULL)
Guido van Rossum6fa63431993-12-24 10:36:57 +000063 return NULL;
Guido van Rossumc7fea2f1996-01-12 01:30:55 +000064 fflush(stdout);
Guido van Rossum48a680c2001-03-02 06:34:14 +000065#ifndef RISCOS
Guido van Rossum6fa63431993-12-24 10:36:57 +000066 if (prompt)
67 fprintf(stderr, "%s", prompt);
Guido van Rossum48a680c2001-03-02 06:34:14 +000068#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 Rossumc7fea2f1996-01-12 01:30:55 +000076 fflush(stderr);
Guido van Rossum6da34342000-06-28 22:00:02 +000077 switch (my_fgets(p, (int)n, stdin)) {
Guido van Rossumb6775db1994-08-01 11:34:53 +000078 case 0: /* Normal case */
79 break;
80 case 1: /* Interrupt */
Guido van Rossumb18618d2000-05-03 23:44:39 +000081 PyMem_FREE(p);
Guido van Rossum6fa63431993-12-24 10:36:57 +000082 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +000083 case -1: /* EOF */
84 case -2: /* Error */
85 default: /* Shouldn't happen */
86 *p = '\0';
87 break;
Guido van Rossum6fa63431993-12-24 10:36:57 +000088 }
Guido van Rossumb6775db1994-08-01 11:34:53 +000089#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 Rossum6fa63431993-12-24 10:36:57 +000096 n = strlen(p);
97 while (n > 0 && p[n-1] != '\n') {
Guido van Rossum6da34342000-06-28 22:00:02 +000098 size_t incr = n+2;
Guido van Rossumb18618d2000-05-03 23:44:39 +000099 p = PyMem_REALLOC(p, n + incr);
Guido van Rossum6fa63431993-12-24 10:36:57 +0000100 if (p == NULL)
101 return NULL;
Guido van Rossum6da34342000-06-28 22:00:02 +0000102 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 Rossum6fa63431993-12-24 10:36:57 +0000106 break;
107 n += strlen(p+n);
108 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000109 return PyMem_REALLOC(p, n+1);
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000110}
111
112
113/* By initializing this function pointer, systems embedding Python can
Guido van Rossumb18618d2000-05-03 23:44:39 +0000114 override the readline function.
115
116 Note: Python expects in return a buffer allocated with PyMem_Malloc. */
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000117
Tim Petersdbd9ba62000-07-09 03:09:57 +0000118char *(*PyOS_ReadlineFunctionPointer)(char *);
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000119
120
121/* Interface used by tokenizer.c and bltinmodule.c */
122
123char *
Thomas Wouters23c9e002000-07-22 19:20:54 +0000124PyOS_Readline(char *prompt)
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000125{
Guido van Rossum80c7bcf1998-08-29 16:03:27 +0000126 char *rv;
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000127 if (PyOS_ReadlineFunctionPointer == NULL) {
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000128 PyOS_ReadlineFunctionPointer = PyOS_StdioReadline;
129 }
Guido van Rossum8efa47b1998-08-27 19:43:43 +0000130 Py_BEGIN_ALLOW_THREADS
Guido van Rossum80c7bcf1998-08-29 16:03:27 +0000131 rv = (*PyOS_ReadlineFunctionPointer)(prompt);
Guido van Rossum8efa47b1998-08-27 19:43:43 +0000132 Py_END_ALLOW_THREADS
Guido van Rossum80c7bcf1998-08-29 16:03:27 +0000133 return rv;
Guido van Rossum6fa63431993-12-24 10:36:57 +0000134}