This commit was manufactured by cvs2svn to create tag 'release101'.
[python/dscho.git] / Parser / myreadline.c
blobc74404164bc394b9a3542c4a64d7642f1c0efdab
1 /***********************************************************
2 Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
3 Amsterdam, 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 not be used in advertising or publicity pertaining to
13 distribution of the software without specific, written prior permission.
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
25 /* Readline interface for tokenizer.c.
26 By default, we have a super simple my_readline function.
27 Optionally, we can use the GNU readline library (to be found in the
28 bash distribution).
29 my_readline() has a different return value from GNU readline():
30 - NULL if an interrupt occurred or if an error occurred
31 - a malloc'ed empty string if EOF was read
32 - a malloc'ed string ending in \n normally
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
39 #include <stdio.h>
40 #include <string.h>
42 #include "mymalloc.h"
44 #ifdef WITH_READLINE
46 extern char *readline();
48 #include <setjmp.h>
49 #include <signal.h>
51 static jmp_buf jbuf;
53 /* ARGSUSED */
54 static RETSIGTYPE
55 onintr(sig)
56 int sig;
58 longjmp(jbuf, 1);
61 #endif /* WITH_READLINE */
63 char *
64 my_readline(prompt)
65 char *prompt;
67 int n;
68 char *p;
69 #ifdef WITH_READLINE
70 RETSIGTYPE (*old_inthandler)();
71 static int been_here;
72 if (!been_here) {
73 /* Force rebind of TAB to insert-tab */
74 extern int rl_insert();
75 rl_bind_key('\t', rl_insert);
76 been_here++;
78 old_inthandler = signal(SIGINT, onintr);
79 if (setjmp(jbuf)) {
80 signal(SIGINT, old_inthandler);
81 return NULL;
83 p = readline(prompt);
84 signal(SIGINT, old_inthandler);
85 if (p == NULL) {
86 p = malloc(1);
87 if (p != NULL)
88 *p = '\0';
89 return p;
91 n = strlen(p);
92 if (n > 0)
93 add_history(p);
94 if ((p = realloc(p, n+2)) != NULL) {
95 p[n] = '\n';
96 p[n+1] = '\0';
98 return p;
99 #else /* !WITH_READLINE */
100 n = 100;
101 if ((p = malloc(n)) == NULL)
102 return NULL;
103 if (prompt)
104 fprintf(stderr, "%s", prompt);
105 if (fgets(p, n, stdin) == NULL)
106 *p = '\0';
107 if (intrcheck()) {
108 free(p);
109 return NULL;
111 n = strlen(p);
112 while (n > 0 && p[n-1] != '\n') {
113 int incr = n+2;
114 p = realloc(p, n + incr);
115 if (p == NULL)
116 return NULL;
117 if (fgets(p+n, incr, stdin) == NULL)
118 break;
119 n += strlen(p+n);
121 return realloc(p, n+1);
122 #endif /* !WITH_READLINE */