home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / OPENSTEP / Languages / Python / python-14-src / Parser / myreadline.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-17  |  3.6 KB  |  135 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. 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 or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Readline interface for tokenizer.c.
  33.  
  34.    By default, we have a super simple my_readline function. It may
  35.    be overridden by a powerful one by importing the module ``pyrl'',
  36.    that provides access to the GNU readline library (to be found in the
  37.    bash distribution).
  38.  
  39.    my_readline() has a different return value from GNU readline():
  40.    - NULL if an interrupt occurred or if an error occurred
  41.    - a malloc'ed empty string if EOF was read
  42.    - a malloc'ed string ending in \n normally */
  43.  
  44. #include "config.h"
  45. #include "allobjects.h"
  46. #include <stdio.h>
  47. #include <string.h>
  48. #include <errno.h>
  49.  
  50. #include "myproto.h"
  51. #include "mymalloc.h"
  52. #include "intrcheck.h"
  53.  
  54. /* This function restarts a fgets() after an EINTR error occurred
  55.    except if intrcheck() returns true. */
  56.  
  57. static int
  58. my_fgets(buf, len, fp)
  59.     char *buf;
  60.     int len;
  61.     FILE *fp;
  62. {
  63.     char *p;
  64.     for (;;) {
  65.         errno = 0;
  66.         p = fgets(buf, len, fp);
  67.         if (p != NULL)
  68.             return 0; /* No error */
  69.         if (feof(fp)) {
  70.             return -1; /* EOF */
  71.         }
  72. #ifdef EINTR
  73.         if (errno == EINTR) {
  74.             if (intrcheck()) {
  75.                 return 1; /* Interrupt */
  76.             }
  77.             continue;
  78.         }
  79. #endif
  80.         if (intrcheck()) {
  81.             return 1; /* Interrupt */
  82.         }
  83.         return -2; /* Error */
  84.     }
  85.     /* NOTREACHED */
  86. }
  87.  
  88. static char *
  89. simple_readline (prompt)
  90.     char *prompt;
  91. {
  92.     int n;
  93.     char *p;
  94.  
  95.     n = 100;
  96.     if ((p = malloc(n)) == NULL)
  97.         return NULL;
  98.     fflush(stdout);
  99.     if (prompt)
  100.         fprintf(stderr, "%s", prompt);
  101.     fflush(stderr);
  102.     switch (my_fgets(p, n, stdin)) {
  103.     case 0: /* Normal case */
  104.         break;
  105.     case 1: /* Interrupt */
  106.         free(p);
  107.         return NULL;
  108.     case -1: /* EOF */
  109.     case -2: /* Error */
  110.     default: /* Shouldn't happen */
  111.         *p = '\0';
  112.         break;
  113.     }
  114. #ifdef MPW
  115.     /* Hack for MPW C where the prompt comes right back in the input */
  116.     /* XXX (Actually this would be rather nice on most systems...) */
  117.     n = strlen(prompt);
  118.     if (strncmp(p, prompt, n) == 0)
  119.         memmove(p, p + n, strlen(p) - n + 1);
  120. #endif
  121.     n = strlen(p);
  122.     while (n > 0 && p[n-1] != '\n') {
  123.         int incr = n+2;
  124.         p = realloc(p, n + incr);
  125.         if (p == NULL)
  126.             return NULL;
  127.         if (my_fgets(p+n, incr, stdin) != 0)
  128.             break;
  129.         n += strlen(p+n);
  130.     }
  131.     return realloc(p, n+1);
  132. }
  133.  
  134. char * (*my_readline) PROTO((char *)) = simple_readline;
  135.