annotate Parser/myreadline.c @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents d2c995ec4290
children 6c060bc3bf3d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /***********************************************************
2060
146890653a57 Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
146890653a57 Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
3 The Netherlands.
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 All Rights Reserved
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4306
diff changeset
7 Permission to use, copy, modify, and distribute this software and its
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4306
diff changeset
8 documentation for any purpose and without fee is hereby granted,
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 provided that the above copyright notice appear in all copies and that
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4306
diff changeset
10 both that copyright notice and this permission notice appear in
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 supporting documentation, and that the names of Stichting Mathematisch
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4306
diff changeset
12 Centrum or CWI or Corporation for National Research Initiatives or
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4306
diff changeset
13 CNRI not be used in advertising or publicity pertaining to
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4306
diff changeset
14 distribution of the software without specific, written prior
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4306
diff changeset
15 permission.
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4306
diff changeset
17 While CWI is the initial source for this software, a modified version
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4306
diff changeset
18 is made available by the Corporation for National Research Initiatives
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4306
diff changeset
19 (CNRI) at the Internet address ftp://ftp.python.org.
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4306
diff changeset
20
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4306
diff changeset
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4306
diff changeset
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4306
diff changeset
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4306
diff changeset
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4306
diff changeset
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4306
diff changeset
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4306
diff changeset
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4306
diff changeset
28 PERFORMANCE OF THIS SOFTWARE.
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 ******************************************************************/
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
5134
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
32 /* Readline interface for tokenizer.c and [raw_]input() in bltinmodule.c.
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
33 By default, or when stdin is not a tty device, we have a super
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
34 simple my_readline function using fgets.
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
35 Optionally, we can use the GNU readline library.
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 my_readline() has a different return value from GNU readline():
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 - NULL if an interrupt occurred or if an error occurred
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 - a malloc'ed empty string if EOF was read
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 - a malloc'ed string ending in \n normally
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 */
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
5134
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
42 #define Py_USE_NEW_NAMES 1
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
43
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1452
diff changeset
44 #include "config.h"
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1452
diff changeset
45
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 #include <stdio.h>
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 #include <string.h>
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1452
diff changeset
48 #include <errno.h>
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1452
diff changeset
50 #include "myproto.h"
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 #include "mymalloc.h"
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1452
diff changeset
52 #include "intrcheck.h"
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
5134
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
54
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1452
diff changeset
55 #ifdef WITH_READLINE
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
56
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 extern char *readline();
4306
e9f48d7e73b1 Explicitly call rl_initialize().
Guido van Rossum <guido@python.org>
parents: 4043
diff changeset
58 extern int rl_initialize();
e9f48d7e73b1 Explicitly call rl_initialize().
Guido van Rossum <guido@python.org>
parents: 4043
diff changeset
59 extern int rl_insert();
4577
fae78cf8541d Make gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
60 extern int rl_bind_key();
fae78cf8541d Make gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
61 extern void add_history();
4306
e9f48d7e73b1 Explicitly call rl_initialize().
Guido van Rossum <guido@python.org>
parents: 4043
diff changeset
62 extern char *rl_readline_name;
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
63
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 #include <setjmp.h>
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 #include <signal.h>
Guido van Rossum <guido@python.org>
parents:
diff changeset
66
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 static jmp_buf jbuf;
Guido van Rossum <guido@python.org>
parents:
diff changeset
68
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 /* ARGSUSED */
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 static RETSIGTYPE
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 onintr(sig)
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 int sig;
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 {
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 longjmp(jbuf, 1);
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 }
Guido van Rossum <guido@python.org>
parents:
diff changeset
76
3494
142cbabdd945 Separate readline initialization into new function PyOS_ReadlineInit().
Guido van Rossum <guido@python.org>
parents: 3332
diff changeset
77 void
142cbabdd945 Separate readline initialization into new function PyOS_ReadlineInit().
Guido van Rossum <guido@python.org>
parents: 3332
diff changeset
78 PyOS_ReadlineInit()
142cbabdd945 Separate readline initialization into new function PyOS_ReadlineInit().
Guido van Rossum <guido@python.org>
parents: 3332
diff changeset
79 {
142cbabdd945 Separate readline initialization into new function PyOS_ReadlineInit().
Guido van Rossum <guido@python.org>
parents: 3332
diff changeset
80 static int been_here;
142cbabdd945 Separate readline initialization into new function PyOS_ReadlineInit().
Guido van Rossum <guido@python.org>
parents: 3332
diff changeset
81 if (!been_here) {
142cbabdd945 Separate readline initialization into new function PyOS_ReadlineInit().
Guido van Rossum <guido@python.org>
parents: 3332
diff changeset
82 /* Force rebind of TAB to insert-tab */
4306
e9f48d7e73b1 Explicitly call rl_initialize().
Guido van Rossum <guido@python.org>
parents: 4043
diff changeset
83 rl_readline_name = "python";
e9f48d7e73b1 Explicitly call rl_initialize().
Guido van Rossum <guido@python.org>
parents: 4043
diff changeset
84 rl_initialize();
3494
142cbabdd945 Separate readline initialization into new function PyOS_ReadlineInit().
Guido van Rossum <guido@python.org>
parents: 3332
diff changeset
85 rl_bind_key('\t', rl_insert);
142cbabdd945 Separate readline initialization into new function PyOS_ReadlineInit().
Guido van Rossum <guido@python.org>
parents: 3332
diff changeset
86 been_here++;
142cbabdd945 Separate readline initialization into new function PyOS_ReadlineInit().
Guido van Rossum <guido@python.org>
parents: 3332
diff changeset
87 }
142cbabdd945 Separate readline initialization into new function PyOS_ReadlineInit().
Guido van Rossum <guido@python.org>
parents: 3332
diff changeset
88 }
142cbabdd945 Separate readline initialization into new function PyOS_ReadlineInit().
Guido van Rossum <guido@python.org>
parents: 3332
diff changeset
89
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 char *
5134
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
91 PyOS_GnuReadline(prompt)
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 char *prompt;
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 {
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 int n;
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 char *p;
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 RETSIGTYPE (*old_inthandler)();
3494
142cbabdd945 Separate readline initialization into new function PyOS_ReadlineInit().
Guido van Rossum <guido@python.org>
parents: 3332
diff changeset
97 PyOS_ReadlineInit();
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 old_inthandler = signal(SIGINT, onintr);
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 if (setjmp(jbuf)) {
3332
95643c0b0c0e add sigrelse() call for SunOS 4.1; add some fflush() calls
Guido van Rossum <guido@python.org>
parents: 2060
diff changeset
100 #ifdef HAVE_SIGRELSE
95643c0b0c0e add sigrelse() call for SunOS 4.1; add some fflush() calls
Guido van Rossum <guido@python.org>
parents: 2060
diff changeset
101 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
95643c0b0c0e add sigrelse() call for SunOS 4.1; add some fflush() calls
Guido van Rossum <guido@python.org>
parents: 2060
diff changeset
102 sigrelse(SIGINT);
95643c0b0c0e add sigrelse() call for SunOS 4.1; add some fflush() calls
Guido van Rossum <guido@python.org>
parents: 2060
diff changeset
103 #endif
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 signal(SIGINT, old_inthandler);
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 return NULL;
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 }
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 p = readline(prompt);
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 signal(SIGINT, old_inthandler);
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 if (p == NULL) {
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 p = malloc(1);
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 if (p != NULL)
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 *p = '\0';
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 return p;
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 }
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 n = strlen(p);
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 if (n > 0)
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 add_history(p);
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 if ((p = realloc(p, n+2)) != NULL) {
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 p[n] = '\n';
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 p[n+1] = '\0';
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 }
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 return p;
5134
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
123 }
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
124
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
125 #endif /* !WITH_READLINE */
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
126
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
127
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
128 /* This function restarts a fgets() after an EINTR error occurred
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
129 except if PyOS_InterruptOccurred() returns true. */
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
130
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
131 static int
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
132 my_fgets(buf, len, fp)
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
133 char *buf;
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
134 int len;
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
135 FILE *fp;
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
136 {
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
137 char *p;
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
138 for (;;) {
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
139 errno = 0;
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
140 p = fgets(buf, len, fp);
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
141 if (p != NULL)
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
142 return 0; /* No error */
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
143 if (feof(fp)) {
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
144 return -1; /* EOF */
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
145 }
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
146 #ifdef EINTR
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
147 if (errno == EINTR) {
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
148 if (PyOS_InterruptOccurred()) {
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
149 return 1; /* Interrupt */
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
150 }
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
151 continue;
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
152 }
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
153 #endif
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
154 if (PyOS_InterruptOccurred()) {
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
155 return 1; /* Interrupt */
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
156 }
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
157 return -2; /* Error */
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
158 }
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
159 /* NOTREACHED */
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
160 }
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
161
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
162
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
163 /* Readline implementation using fgets() */
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
164
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
165 char *
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
166 PyOS_StdioReadline(prompt)
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
167 char *prompt;
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
168 {
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
169 int n;
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
170 char *p;
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 n = 100;
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 if ((p = malloc(n)) == NULL)
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 return NULL;
3332
95643c0b0c0e add sigrelse() call for SunOS 4.1; add some fflush() calls
Guido van Rossum <guido@python.org>
parents: 2060
diff changeset
174 fflush(stdout);
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 if (prompt)
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 fprintf(stderr, "%s", prompt);
3332
95643c0b0c0e add sigrelse() call for SunOS 4.1; add some fflush() calls
Guido van Rossum <guido@python.org>
parents: 2060
diff changeset
177 fflush(stderr);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1452
diff changeset
178 switch (my_fgets(p, n, stdin)) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1452
diff changeset
179 case 0: /* Normal case */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1452
diff changeset
180 break;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1452
diff changeset
181 case 1: /* Interrupt */
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 free(p);
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 return NULL;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1452
diff changeset
184 case -1: /* EOF */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1452
diff changeset
185 case -2: /* Error */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1452
diff changeset
186 default: /* Shouldn't happen */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1452
diff changeset
187 *p = '\0';
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1452
diff changeset
188 break;
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 }
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1452
diff changeset
190 #ifdef MPW
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1452
diff changeset
191 /* Hack for MPW C where the prompt comes right back in the input */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1452
diff changeset
192 /* XXX (Actually this would be rather nice on most systems...) */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1452
diff changeset
193 n = strlen(prompt);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1452
diff changeset
194 if (strncmp(p, prompt, n) == 0)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1452
diff changeset
195 memmove(p, p + n, strlen(p) - n + 1);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1452
diff changeset
196 #endif
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 n = strlen(p);
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 while (n > 0 && p[n-1] != '\n') {
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 int incr = n+2;
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 p = realloc(p, n + incr);
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 if (p == NULL)
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 return NULL;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1452
diff changeset
203 if (my_fgets(p+n, incr, stdin) != 0)
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 break;
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 n += strlen(p+n);
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 }
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 return realloc(p, n+1);
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 }
5134
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
209
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
210
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
211 /* By initializing this function pointer, systems embedding Python can
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
212 override the readline function. */
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
213
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
214 char *(*PyOS_ReadlineFunctionPointer) Py_PROTO((char *));
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
215
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
216
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
217 /* Interface used by tokenizer.c and bltinmodule.c */
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
218
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
219 char *
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
220 PyOS_Readline(prompt)
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
221 char *prompt;
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
222 {
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
223 if (PyOS_ReadlineFunctionPointer == NULL) {
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
224 #ifdef WITH_READLINE
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
225 if (isatty(fileno(stdin)))
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
226 PyOS_ReadlineFunctionPointer = PyOS_GnuReadline;
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
227 else
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
228 #endif
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
229 PyOS_ReadlineFunctionPointer = PyOS_StdioReadline;
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
230 }
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
231 return (*PyOS_ReadlineFunctionPointer)(prompt);
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
232 }