Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum, |
| 3 | Amsterdam, The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 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. |
| 14 | |
| 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. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
| 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 |
| 33 | */ |
| 34 | |
| 35 | #include <stdio.h> |
| 36 | #include <string.h> |
| 37 | |
| 38 | #include "mymalloc.h" |
| 39 | |
| 40 | #ifdef HAVE_READLINE |
| 41 | |
| 42 | extern char *readline(); |
| 43 | |
| 44 | #include <setjmp.h> |
| 45 | #include <signal.h> |
| 46 | |
| 47 | static jmp_buf jbuf; |
| 48 | |
| 49 | /* ARGSUSED */ |
| 50 | static RETSIGTYPE |
| 51 | onintr(sig) |
| 52 | int sig; |
| 53 | { |
| 54 | longjmp(jbuf, 1); |
| 55 | } |
| 56 | |
| 57 | #endif /* HAVE_READLINE */ |
| 58 | |
| 59 | char * |
| 60 | my_readline(prompt) |
| 61 | char *prompt; |
| 62 | { |
| 63 | int n; |
| 64 | char *p; |
| 65 | #ifdef HAVE_READLINE |
| 66 | RETSIGTYPE (*old_inthandler)(); |
| 67 | static int been_here; |
| 68 | if (!been_here) { |
| 69 | /* Force rebind of TAB to insert-tab */ |
| 70 | extern int rl_insert(); |
| 71 | rl_bind_key('\t', rl_insert); |
| 72 | been_here++; |
| 73 | } |
| 74 | old_inthandler = signal(SIGINT, onintr); |
| 75 | if (setjmp(jbuf)) { |
| 76 | signal(SIGINT, old_inthandler); |
| 77 | return NULL; |
| 78 | } |
| 79 | p = readline(prompt); |
| 80 | signal(SIGINT, old_inthandler); |
| 81 | if (p == NULL) { |
| 82 | p = malloc(1); |
| 83 | if (p != NULL) |
| 84 | *p = '\0'; |
| 85 | return p; |
| 86 | } |
| 87 | n = strlen(p); |
| 88 | if (n > 0) |
| 89 | add_history(p); |
| 90 | if ((p = realloc(p, n+2)) != NULL) { |
| 91 | p[n] = '\n'; |
| 92 | p[n+1] = '\0'; |
| 93 | } |
| 94 | return p; |
| 95 | #else /* !HAVE_READLINE */ |
| 96 | n = 100; |
| 97 | if ((p = malloc(n)) == NULL) |
| 98 | return NULL; |
| 99 | if (prompt) |
| 100 | fprintf(stderr, "%s", prompt); |
| 101 | if (fgets(p, n, stdin) == NULL) |
| 102 | *p = '\0'; |
| 103 | if (intrcheck()) { |
| 104 | free(p); |
| 105 | return NULL; |
| 106 | } |
| 107 | n = strlen(p); |
| 108 | while (n > 0 && p[n-1] != '\n') { |
| 109 | int incr = n+2; |
| 110 | p = realloc(p, n + incr); |
| 111 | if (p == NULL) |
| 112 | return NULL; |
| 113 | if (fgets(p+n, incr, stdin) == NULL) |
| 114 | break; |
| 115 | n += strlen(p+n); |
| 116 | } |
| 117 | return realloc(p, n+1); |
| 118 | #endif /* !HAVE_READLINE */ |
| 119 | } |