Improved some error messages for command line processing.
[python/dscho.git] / Parser / myreadline.c
blob24bb18c653dc8cf289cb4a794795a59a8cd16151
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
15 permission.
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 /* Readline interface for tokenizer.c and [raw_]input() in bltinmodule.c.
33 By default, or when stdin is not a tty device, we have a super
34 simple my_readline function using fgets.
35 Optionally, we can use the GNU readline library.
36 my_readline() has a different return value from GNU readline():
37 - NULL if an interrupt occurred or if an error occurred
38 - a malloc'ed empty string if EOF was read
39 - a malloc'ed string ending in \n normally
42 #define Py_USE_NEW_NAMES 1
44 #include "config.h"
46 #include <stdio.h>
47 #include <string.h>
48 #include <errno.h>
50 #include "myproto.h"
51 #include "mymalloc.h"
52 #include "intrcheck.h"
54 int (*PyOS_InputHook)() = NULL;
56 /* This function restarts a fgets() after an EINTR error occurred
57 except if PyOS_InterruptOccurred() returns true. */
59 static int
60 my_fgets(buf, len, fp)
61 char *buf;
62 int len;
63 FILE *fp;
65 char *p;
66 for (;;) {
67 if (PyOS_InputHook != NULL)
68 (void)(PyOS_InputHook)();
69 errno = 0;
70 p = fgets(buf, len, fp);
71 if (p != NULL)
72 return 0; /* No error */
73 if (feof(fp)) {
74 return -1; /* EOF */
76 #ifdef EINTR
77 if (errno == EINTR) {
78 if (PyOS_InterruptOccurred()) {
79 return 1; /* Interrupt */
81 continue;
83 #endif
84 if (PyOS_InterruptOccurred()) {
85 return 1; /* Interrupt */
87 return -2; /* Error */
89 /* NOTREACHED */
93 /* Readline implementation using fgets() */
95 char *
96 PyOS_StdioReadline(prompt)
97 char *prompt;
99 int n;
100 char *p;
101 n = 100;
102 if ((p = malloc(n)) == NULL)
103 return NULL;
104 fflush(stdout);
105 if (prompt)
106 fprintf(stderr, "%s", prompt);
107 fflush(stderr);
108 switch (my_fgets(p, n, stdin)) {
109 case 0: /* Normal case */
110 break;
111 case 1: /* Interrupt */
112 free(p);
113 return NULL;
114 case -1: /* EOF */
115 case -2: /* Error */
116 default: /* Shouldn't happen */
117 *p = '\0';
118 break;
120 #ifdef MPW
121 /* Hack for MPW C where the prompt comes right back in the input */
122 /* XXX (Actually this would be rather nice on most systems...) */
123 n = strlen(prompt);
124 if (strncmp(p, prompt, n) == 0)
125 memmove(p, p + n, strlen(p) - n + 1);
126 #endif
127 n = strlen(p);
128 while (n > 0 && p[n-1] != '\n') {
129 int incr = n+2;
130 p = realloc(p, n + incr);
131 if (p == NULL)
132 return NULL;
133 if (my_fgets(p+n, incr, stdin) != 0)
134 break;
135 n += strlen(p+n);
137 return realloc(p, n+1);
141 /* By initializing this function pointer, systems embedding Python can
142 override the readline function. */
144 char *(*PyOS_ReadlineFunctionPointer) Py_PROTO((char *));
147 /* Interface used by tokenizer.c and bltinmodule.c */
149 char *
150 PyOS_Readline(prompt)
151 char *prompt;
153 if (PyOS_ReadlineFunctionPointer == NULL) {
154 PyOS_ReadlineFunctionPointer = PyOS_StdioReadline;
156 return (*PyOS_ReadlineFunctionPointer)(prompt);