home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Parser / myreadline.c < prev    next >
C/C++ Source or Header  |  1994-04-14  |  4KB  |  173 lines

  1. /***********************************************************
  2. Copyright 1991, 1992, 1993, 1994 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. #ifdef HAVE_CONFIG_H
  36. #include "config.h"
  37. #endif
  38.  
  39. #include <stdio.h>
  40. #include <string.h>
  41. #include <errno.h>
  42.  
  43. #include "mymalloc.h"
  44.  
  45. #ifdef WITH_READLINE
  46.  
  47. extern char *readline();
  48.  
  49. #include <setjmp.h>
  50. #include <signal.h>
  51.  
  52. static jmp_buf jbuf;
  53.  
  54. /* ARGSUSED */
  55. static RETSIGTYPE
  56. onintr(sig)
  57.     int sig;
  58. {
  59.     longjmp(jbuf, 1);
  60. }
  61.  
  62. #else /* !WITH_READLINE */
  63.  
  64. /* This function restarts a fgets() after an EINTR error occurred
  65.    except if intrcheck() returns true. */
  66.  
  67. static int
  68. my_fgets(buf, len, fp)
  69.     char *buf;
  70.     int len;
  71.     FILE *fp;
  72. {
  73.     char *p;
  74.     for (;;) {
  75.         errno = 0;
  76.         p = fgets(buf, len, fp);
  77.         if (p != NULL)
  78.             return 0; /* No error */
  79.         if (feof(fp)) {
  80. /*fprintf(stderr, "my_fgets: EOF\n");*/
  81.             return -1; /* EOF */
  82.         }
  83. #ifdef EINTR
  84.         if (errno == EINTR) {
  85.             if (intrcheck()) {
  86. /*fprintf(stderr, "my_fgets: EINTR with intrcheck()\n");*/
  87.                 return 1; /* Interrupt */
  88.             }
  89. /*fprintf(stderr, "my_fgets: EINTR\n");*/
  90.             continue;
  91. #endif
  92.         }
  93.         if (intrcheck()) {
  94. /*fprintf(stderr, "my_fgets: intrcheck()\n");*/
  95.             return 1; /* Interrupt */
  96.         }
  97. /*fprintf(stderr, "my_fgets: error\n");*/
  98.         return -2; /* Error */
  99.     }
  100.     /* NOTREACHED */
  101. }
  102.  
  103. #endif /* WITH_READLINE */
  104.  
  105.  
  106. char *
  107. my_readline(prompt)
  108.     char *prompt;
  109. {
  110.     int n;
  111.     char *p;
  112. #ifdef WITH_READLINE
  113.     RETSIGTYPE (*old_inthandler)();
  114.     static int been_here;
  115.     if (!been_here) {
  116.         /* Force rebind of TAB to insert-tab */
  117.         extern int rl_insert();
  118.         rl_bind_key('\t', rl_insert);
  119.         been_here++;
  120.     }
  121.     old_inthandler = signal(SIGINT, onintr);
  122.     if (setjmp(jbuf)) {
  123.         signal(SIGINT, old_inthandler);
  124.         return NULL;
  125.     }
  126.     p = readline(prompt);
  127.     signal(SIGINT, old_inthandler);
  128.     if (p == NULL) {
  129.         p = malloc(1);
  130.         if (p != NULL)
  131.             *p = '\0';
  132.         return p;
  133.     }
  134.     n = strlen(p);
  135.     if (n > 0)
  136.         add_history(p);
  137.     if ((p = realloc(p, n+2)) != NULL) {
  138.         p[n] = '\n';
  139.         p[n+1] = '\0';
  140.     }
  141.     return p;
  142. #else /* !WITH_READLINE */
  143.     n = 100;
  144.     if ((p = malloc(n)) == NULL)
  145.         return NULL;
  146.     if (prompt)
  147.         fprintf(stderr, "%s", prompt);
  148.     switch (my_fgets(p, n, stdin)) {
  149.     case 0: /* Normal case */
  150.         break;
  151.     case 1: /* Interrupt */
  152.         free(p);
  153.         return NULL;
  154.     case -1: /* EOF */
  155.     case -2: /* Error */
  156.     default: /* Shouldn't happen */
  157.         *p = '\0';
  158.         break;
  159.     }
  160.     n = strlen(p);
  161.     while (n > 0 && p[n-1] != '\n') {
  162.         int incr = n+2;
  163.         p = realloc(p, n + incr);
  164.         if (p == NULL)
  165.             return NULL;
  166.         if (my_fgets(p+n, incr, stdin) != 0)
  167.             break;
  168.         n += strlen(p+n);
  169.     }
  170.     return realloc(p, n+1);
  171. #endif /* !WITH_READLINE */
  172. }
  173.