blob: bb35000d0e286045ef3427f1521f6b043298d91b [file] [log] [blame]
Guido van Rossum6fa63431993-12-24 10:36:57 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossum6fa63431993-12-24 10:36:57 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossum6fa63431993-12-24 10:36:57 +00009******************************************************************/
10
Guido van Rossumfbd64c81997-02-18 21:53:32 +000011/* Readline interface for tokenizer.c and [raw_]input() in bltinmodule.c.
12 By default, or when stdin is not a tty device, we have a super
13 simple my_readline function using fgets.
14 Optionally, we can use the GNU readline library.
Guido van Rossum6fa63431993-12-24 10:36:57 +000015 my_readline() has a different return value from GNU readline():
16 - NULL if an interrupt occurred or if an error occurred
17 - a malloc'ed empty string if EOF was read
18 - a malloc'ed string ending in \n normally
19*/
20
Guido van Rossum8efa47b1998-08-27 19:43:43 +000021#include "Python.h"
Jack Jansen41aa8e52000-07-03 21:39:47 +000022#ifdef HAVE_LIMITS_H
23#include <limits.h>
24#endif
Guido van Rossum6fa63431993-12-24 10:36:57 +000025
Guido van Rossum44620641997-08-11 18:57:29 +000026int (*PyOS_InputHook)() = NULL;
Guido van Rossumfbd64c81997-02-18 21:53:32 +000027
28/* This function restarts a fgets() after an EINTR error occurred
29 except if PyOS_InterruptOccurred() returns true. */
30
31static int
32my_fgets(buf, len, fp)
33 char *buf;
34 int len;
35 FILE *fp;
36{
37 char *p;
38 for (;;) {
Guido van Rossum44620641997-08-11 18:57:29 +000039 if (PyOS_InputHook != NULL)
40 (void)(PyOS_InputHook)();
Guido van Rossumfbd64c81997-02-18 21:53:32 +000041 errno = 0;
42 p = fgets(buf, len, fp);
43 if (p != NULL)
44 return 0; /* No error */
45 if (feof(fp)) {
46 return -1; /* EOF */
47 }
48#ifdef EINTR
49 if (errno == EINTR) {
50 if (PyOS_InterruptOccurred()) {
51 return 1; /* Interrupt */
52 }
53 continue;
54 }
55#endif
56 if (PyOS_InterruptOccurred()) {
57 return 1; /* Interrupt */
58 }
59 return -2; /* Error */
60 }
61 /* NOTREACHED */
62}
63
64
65/* Readline implementation using fgets() */
66
67char *
68PyOS_StdioReadline(prompt)
69 char *prompt;
70{
Guido van Rossum6da34342000-06-28 22:00:02 +000071 size_t n;
Guido van Rossumfbd64c81997-02-18 21:53:32 +000072 char *p;
Guido van Rossum6fa63431993-12-24 10:36:57 +000073 n = 100;
Guido van Rossumb18618d2000-05-03 23:44:39 +000074 if ((p = PyMem_MALLOC(n)) == NULL)
Guido van Rossum6fa63431993-12-24 10:36:57 +000075 return NULL;
Guido van Rossumc7fea2f1996-01-12 01:30:55 +000076 fflush(stdout);
Guido van Rossum6fa63431993-12-24 10:36:57 +000077 if (prompt)
78 fprintf(stderr, "%s", prompt);
Guido van Rossumc7fea2f1996-01-12 01:30:55 +000079 fflush(stderr);
Guido van Rossum6da34342000-06-28 22:00:02 +000080 switch (my_fgets(p, (int)n, stdin)) {
Guido van Rossumb6775db1994-08-01 11:34:53 +000081 case 0: /* Normal case */
82 break;
83 case 1: /* Interrupt */
Guido van Rossumb18618d2000-05-03 23:44:39 +000084 PyMem_FREE(p);
Guido van Rossum6fa63431993-12-24 10:36:57 +000085 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +000086 case -1: /* EOF */
87 case -2: /* Error */
88 default: /* Shouldn't happen */
89 *p = '\0';
90 break;
Guido van Rossum6fa63431993-12-24 10:36:57 +000091 }
Guido van Rossumb6775db1994-08-01 11:34:53 +000092#ifdef MPW
93 /* Hack for MPW C where the prompt comes right back in the input */
94 /* XXX (Actually this would be rather nice on most systems...) */
95 n = strlen(prompt);
96 if (strncmp(p, prompt, n) == 0)
97 memmove(p, p + n, strlen(p) - n + 1);
98#endif
Guido van Rossum6fa63431993-12-24 10:36:57 +000099 n = strlen(p);
100 while (n > 0 && p[n-1] != '\n') {
Guido van Rossum6da34342000-06-28 22:00:02 +0000101 size_t incr = n+2;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000102 p = PyMem_REALLOC(p, n + incr);
Guido van Rossum6fa63431993-12-24 10:36:57 +0000103 if (p == NULL)
104 return NULL;
Guido van Rossum6da34342000-06-28 22:00:02 +0000105 if (incr > INT_MAX) {
106 PyErr_SetString(PyExc_OverflowError, "input line too long");
107 }
108 if (my_fgets(p+n, (int)incr, stdin) != 0)
Guido van Rossum6fa63431993-12-24 10:36:57 +0000109 break;
110 n += strlen(p+n);
111 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000112 return PyMem_REALLOC(p, n+1);
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000113}
114
115
116/* By initializing this function pointer, systems embedding Python can
Guido van Rossumb18618d2000-05-03 23:44:39 +0000117 override the readline function.
118
119 Note: Python expects in return a buffer allocated with PyMem_Malloc. */
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000120
Tim Petersdbd9ba62000-07-09 03:09:57 +0000121char *(*PyOS_ReadlineFunctionPointer)(char *);
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000122
123
124/* Interface used by tokenizer.c and bltinmodule.c */
125
126char *
127PyOS_Readline(prompt)
128 char *prompt;
129{
Guido van Rossum80c7bcf1998-08-29 16:03:27 +0000130 char *rv;
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000131 if (PyOS_ReadlineFunctionPointer == NULL) {
Guido van Rossumfbd64c81997-02-18 21:53:32 +0000132 PyOS_ReadlineFunctionPointer = PyOS_StdioReadline;
133 }
Guido van Rossum8efa47b1998-08-27 19:43:43 +0000134 Py_BEGIN_ALLOW_THREADS
Guido van Rossum80c7bcf1998-08-29 16:03:27 +0000135 rv = (*PyOS_ReadlineFunctionPointer)(prompt);
Guido van Rossum8efa47b1998-08-27 19:43:43 +0000136 Py_END_ALLOW_THREADS
Guido van Rossum80c7bcf1998-08-29 16:03:27 +0000137 return rv;
Guido van Rossum6fa63431993-12-24 10:36:57 +0000138}