home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / cpm / xlisp / xlisp12.ark / XLIO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1985-02-20  |  3.3 KB  |  152 lines

  1. /* xlio - xlisp i/o routines */
  2.  
  3. #ifdef AZTEC
  4. #include "stdio.h"
  5. #else
  6. #include <stdio.h>
  7. #endif
  8.  
  9. #include "xlisp.h"
  10.  
  11. /* global variables */
  12. int xlplevel=0;
  13. int xlfsize=0;
  14.  
  15. /* external variables */
  16. extern struct node *xlstack;
  17. extern struct node *s_stdin;
  18.  
  19. /* local variables */
  20. static int prompt=TRUE;
  21.  
  22. /* xlgetc - get a character from a file or stream */
  23. int xlgetc(fptr)
  24.   struct node *fptr;
  25. {
  26.     struct node *lptr,*cptr;
  27.     FILE *fp;
  28.     int ch;
  29.  
  30.     /* check for input from nil */
  31.     if (fptr == NULL)
  32.     ch = EOF;
  33.  
  34.     /* otherwise, check for input from a stream */
  35.     else if (fptr->n_type == LIST) {
  36.     if ((lptr = fptr->n_listvalue) == NULL)
  37.         ch = EOF;
  38.     else {
  39.         if (lptr->n_type != LIST ||
  40.         (cptr = lptr->n_listvalue) == NULL || cptr->n_type != INT)
  41.         xlfail("bad stream");
  42.         if ((fptr->n_listvalue = lptr->n_listnext) == NULL)
  43.         fptr->n_listnext = NULL;
  44.         ch = cptr->n_int;
  45.     }
  46.     }
  47.  
  48.     /* otherwise, check for a buffered file character */
  49.     else if (ch = fptr->n_savech)
  50.     fptr->n_savech = 0;
  51.  
  52.     /* otherwise, get a new character */
  53.     else {
  54.  
  55.     /* get the file pointer */
  56.     fp = fptr->n_fp;
  57.  
  58.     /* prompt if necessary */
  59.     if (prompt && fp == stdin) {
  60.         if (xlplevel > 0)
  61.         printf("%d> ",xlplevel);
  62.         else
  63.         printf("> ");
  64.         prompt = FALSE;
  65.     }
  66.  
  67.     /* get the character */
  68.     if ((ch = getc(fp)) == '\n' && fp == stdin)
  69.         prompt = TRUE;
  70.  
  71.     /* check for input abort */
  72.     if (fp == stdin && ch == '\007') {
  73.         putchar('\n');
  74.         xlfail("input aborted");
  75.     }
  76.     }
  77.  
  78.     /* return the character */
  79.     return (ch);
  80. }
  81.  
  82. /* xlpeek - peek at a character from a file or stream */
  83. int xlpeek(fptr)
  84.   struct node *fptr;
  85. {
  86.     struct node *lptr,*cptr;
  87.     int ch;
  88.  
  89.     /* check for input from nil */
  90.     if (fptr == NULL)
  91.     ch = EOF;
  92.  
  93.     /* otherwise, check for input from a stream */
  94.     else if (fptr->n_type == LIST) {
  95.     if ((lptr = fptr->n_listvalue) == NULL)
  96.         ch = EOF;
  97.     else {
  98.         if (lptr->n_type != LIST ||
  99.         (cptr = lptr->n_listvalue) == NULL || cptr->n_type != INT)
  100.         xlfail("bad stream");
  101.         ch = cptr->n_int;
  102.     }
  103.     }
  104.  
  105.     /* otherwise, get the next file character and save it */
  106.     else
  107.     ch = fptr->n_savech = xlgetc(fptr);
  108.  
  109.     /* return the character */
  110.     return (ch);
  111. }
  112.  
  113. /* xlputc - put a character to a file or stream */
  114. xlputc(fptr,ch)
  115.   struct node *fptr; int ch;
  116. {
  117.     struct node *oldstk,lptr;
  118.  
  119.     /* count the character */
  120.     xlfsize++;
  121.  
  122.     /* check for output to nil */
  123.     if (fptr == NULL)
  124.     ;
  125.  
  126.     /* otherwise, check for output to a stream */
  127.     else if (fptr->n_type == LIST) {
  128.     oldstk = xlsave(&lptr,NULL);
  129.     lptr.n_ptr = newnode(LIST);
  130.     lptr.n_ptr->n_listvalue = newnode(INT);
  131.     lptr.n_ptr->n_listvalue->n_int = ch;
  132.     if (fptr->n_listnext)
  133.         fptr->n_listnext->n_listnext = lptr.n_ptr;
  134.     else
  135.         fptr->n_listvalue = lptr.n_ptr;
  136.     fptr->n_listnext = lptr.n_ptr;
  137.     xlstack = oldstk;
  138.     }
  139.  
  140.     /* otherwise, output the character to a file */
  141.     else
  142.     putc(ch,fptr->n_fp);
  143. }
  144.  
  145. /* xlflush - flush the input buffer */
  146. int xlflush()
  147. {
  148.     if (!prompt)
  149.     while (xlgetc(s_stdin->n_symvalue) != '\n')
  150.         ;
  151. }
  152.