home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / py2s152.zip / Parser / myreadline.c < prev    next >
C/C++ Source or Header  |  1999-06-27  |  4KB  |  152 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 and [raw_]input() in bltinmodule.c.
  33.    By default, or when stdin is not a tty device, we have a super
  34.    simple my_readline function using fgets.
  35.    Optionally, we can use the GNU readline library.
  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.  
  42. #include "Python.h"
  43.  
  44. int (*PyOS_InputHook)() = NULL;
  45.  
  46. /* This function restarts a fgets() after an EINTR error occurred
  47.    except if PyOS_InterruptOccurred() returns true. */
  48.  
  49. static int
  50. my_fgets(buf, len, fp)
  51.     char *buf;
  52.     int len;
  53.     FILE *fp;
  54. {
  55.     char *p;
  56.     for (;;) {
  57.         if (PyOS_InputHook != NULL)
  58.             (void)(PyOS_InputHook)();
  59.         errno = 0;
  60.         p = fgets(buf, len, fp);
  61.         if (p != NULL)
  62.             return 0; /* No error */
  63.         if (feof(fp)) {
  64.             return -1; /* EOF */
  65.         }
  66. #ifdef EINTR
  67.         if (errno == EINTR) {
  68.             if (PyOS_InterruptOccurred()) {
  69.                 return 1; /* Interrupt */
  70.             }
  71.             continue;
  72.         }
  73. #endif
  74.         if (PyOS_InterruptOccurred()) {
  75.             return 1; /* Interrupt */
  76.         }
  77.         return -2; /* Error */
  78.     }
  79.     /* NOTREACHED */
  80. }
  81.  
  82.  
  83. /* Readline implementation using fgets() */
  84.  
  85. char *
  86. PyOS_StdioReadline(prompt)
  87.     char *prompt;
  88. {
  89.     int n;
  90.     char *p;
  91.     n = 100;
  92.     if ((p = malloc(n)) == NULL)
  93.         return NULL;
  94.     fflush(stdout);
  95.     if (prompt)
  96.         fprintf(stderr, "%s", prompt);
  97.     fflush(stderr);
  98.     switch (my_fgets(p, n, stdin)) {
  99.     case 0: /* Normal case */
  100.         break;
  101.     case 1: /* Interrupt */
  102.         free(p);
  103.         return NULL;
  104.     case -1: /* EOF */
  105.     case -2: /* Error */
  106.     default: /* Shouldn't happen */
  107.         *p = '\0';
  108.         break;
  109.     }
  110. #ifdef MPW
  111.     /* Hack for MPW C where the prompt comes right back in the input */
  112.     /* XXX (Actually this would be rather nice on most systems...) */
  113.     n = strlen(prompt);
  114.     if (strncmp(p, prompt, n) == 0)
  115.         memmove(p, p + n, strlen(p) - n + 1);
  116. #endif
  117.     n = strlen(p);
  118.     while (n > 0 && p[n-1] != '\n') {
  119.         int incr = n+2;
  120.         p = realloc(p, n + incr);
  121.         if (p == NULL)
  122.             return NULL;
  123.         if (my_fgets(p+n, incr, stdin) != 0)
  124.             break;
  125.         n += strlen(p+n);
  126.     }
  127.     return realloc(p, n+1);
  128. }
  129.  
  130.  
  131. /* By initializing this function pointer, systems embedding Python can
  132.    override the readline function. */
  133.  
  134. char *(*PyOS_ReadlineFunctionPointer) Py_PROTO((char *));
  135.  
  136.  
  137. /* Interface used by tokenizer.c and bltinmodule.c */
  138.  
  139. char *
  140. PyOS_Readline(prompt)
  141.     char *prompt;
  142. {
  143.     char *rv;
  144.     if (PyOS_ReadlineFunctionPointer == NULL) {
  145.             PyOS_ReadlineFunctionPointer = PyOS_StdioReadline;
  146.     }
  147.     Py_BEGIN_ALLOW_THREADS
  148.     rv = (*PyOS_ReadlineFunctionPointer)(prompt);
  149.     Py_END_ALLOW_THREADS
  150.     return rv;
  151. }
  152.