Mercurial > cpython-lru_cache-weakref
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 |
| rev | line source |
|---|---|
| 1452 | 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 | 4 |
| 5 All Rights Reserved | |
| 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 | 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 | 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 | 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 | 29 |
| 30 ******************************************************************/ | |
| 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 | 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 | |
| 40 */ | |
| 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 | 46 #include <stdio.h> |
| 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 | 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 | 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 | 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 | 56 |
| 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 | 60 extern int rl_bind_key(); |
| 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 | 63 |
| 64 #include <setjmp.h> | |
| 65 #include <signal.h> | |
| 66 | |
| 67 static jmp_buf jbuf; | |
| 68 | |
| 69 /* ARGSUSED */ | |
| 70 static RETSIGTYPE | |
| 71 onintr(sig) | |
| 72 int sig; | |
| 73 { | |
| 74 longjmp(jbuf, 1); | |
| 75 } | |
| 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 | 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 | 92 char *prompt; |
| 93 { | |
| 94 int n; | |
| 95 char *p; | |
| 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 | 98 old_inthandler = signal(SIGINT, onintr); |
| 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 | 104 signal(SIGINT, old_inthandler); |
| 105 return NULL; | |
| 106 } | |
| 107 p = readline(prompt); | |
| 108 signal(SIGINT, old_inthandler); | |
| 109 if (p == NULL) { | |
| 110 p = malloc(1); | |
| 111 if (p != NULL) | |
| 112 *p = '\0'; | |
| 113 return p; | |
| 114 } | |
| 115 n = strlen(p); | |
| 116 if (n > 0) | |
| 117 add_history(p); | |
| 118 if ((p = realloc(p, n+2)) != NULL) { | |
| 119 p[n] = '\n'; | |
| 120 p[n+1] = '\0'; | |
| 121 } | |
| 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 | 171 n = 100; |
| 172 if ((p = malloc(n)) == NULL) | |
| 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 | 175 if (prompt) |
| 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 | 182 free(p); |
| 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 | 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 | 197 n = strlen(p); |
| 198 while (n > 0 && p[n-1] != '\n') { | |
| 199 int incr = n+2; | |
| 200 p = realloc(p, n + incr); | |
| 201 if (p == NULL) | |
| 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 | 204 break; |
| 205 n += strlen(p+n); | |
| 206 } | |
| 207 return realloc(p, n+1); | |
| 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 } |
