home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / Programming / Source / winterp-1.13 / src-server / xlisp / xlio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-04  |  5.9 KB  |  250 lines

  1. /* -*-C-*-
  2. ********************************************************************************
  3. *
  4. * File:         xlio.c
  5. * RCS:          $Header: xlio.c,v 1.4 91/03/24 22:25:01 mayer Exp $
  6. * Description:  xlisp i/o routines
  7. * Author:       David Michael Betz
  8. * Created:      
  9. * Modified:     Fri Oct  4 04:01:32 1991 (Niels Mayer) mayer@hplnpm
  10. * Language:     C
  11. * Package:      N/A
  12. * Status:       X11r5 contrib tape release
  13. *
  14. * WINTERP Copyright 1989, 1990, 1991 Hewlett-Packard Company (by Niels Mayer).
  15. * XLISP version 2.1, Copyright (c) 1989, by David Betz.
  16. *
  17. * Permission to use, copy, modify, distribute, and sell this software and its
  18. * documentation for any purpose is hereby granted without fee, provided that
  19. * the above copyright notice appear in all copies and that both that
  20. * copyright notice and this permission notice appear in supporting
  21. * documentation, and that the name of Hewlett-Packard and David Betz not be
  22. * used in advertising or publicity pertaining to distribution of the software
  23. * without specific, written prior permission.  Hewlett-Packard and David Betz
  24. * make no representations about the suitability of this software for any
  25. * purpose. It is provided "as is" without express or implied warranty.
  26. *
  27. * HEWLETT-PACKARD AND DAVID BETZ DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
  28. * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
  29. * IN NO EVENT SHALL HEWLETT-PACKARD NOR DAVID BETZ BE LIABLE FOR ANY SPECIAL,
  30. * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  31. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  32. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  33. * PERFORMANCE OF THIS SOFTWARE.
  34. *
  35. * See ./winterp/COPYRIGHT for information on contacting the authors.
  36. * Please send modifications, improvements and bugfixes to mayer@hplabs.hp.com
  37. * Post XLISP-specific questions/information to the newsgroup comp.lang.lisp.x
  38. *
  39. ********************************************************************************
  40. */
  41. static char rcs_identity[] = "@(#)$Header: xlio.c,v 1.4 91/03/24 22:25:01 mayer Exp $";
  42.  
  43. #include "xlisp.h"
  44.  
  45. /* external variables */
  46. extern LVAL s_stdin,s_stdout,s_stderr,s_debugio,s_traceout,s_unbound;
  47. extern int xlfsize;
  48.  
  49. /* xlgetc - get a character from a file or stream */
  50. int xlgetc(fptr)
  51.   LVAL fptr;
  52. {
  53.     LVAL lptr,cptr;
  54.     FILE *fp;
  55.     int ch;
  56.  
  57.     /* check for input from nil */
  58.     if (fptr == NIL)
  59.     ch = EOF;
  60.  
  61.     /* otherwise, check for input from a stream */
  62.     else if (ustreamp(fptr)) {
  63.     if ((lptr = gethead(fptr)) == NIL)
  64.         ch = EOF;
  65.     else {
  66.         if (!consp(lptr) || (cptr = car(lptr)) == NIL || !charp(cptr))
  67.         xlfail("bad stream");
  68.         sethead(fptr,lptr = cdr(lptr));
  69.         if (lptr == NIL)
  70.         settail(fptr,NIL);
  71.         ch = getchcode(cptr);
  72.     }
  73.     }
  74.  
  75.     /* otherwise, check for a buffered character */
  76.     else if (ch = getsavech(fptr))
  77.     setsavech(fptr,'\0');
  78.  
  79.     /* otherwise, check for terminal input or file input */
  80.     else {
  81.     fp = getfile(fptr);
  82.     if (fp == stdin || fp == stderr)
  83.         ch = ostgetc();
  84.     else
  85.         ch = osagetc(fp);
  86.     }
  87.  
  88.     /* return the character */
  89.     return (ch);
  90. }
  91.  
  92. /* xlungetc - unget a character */
  93. xlungetc(fptr,ch)
  94.   LVAL fptr; int ch;
  95. {
  96.     LVAL lptr;
  97.     
  98.     /* check for ungetc from nil */
  99.     if (fptr == NIL)
  100.     ;
  101.     
  102.     /* otherwise, check for ungetc to a stream */
  103.     if (ustreamp(fptr)) {
  104.     if (ch != EOF) {
  105.         lptr = cons(cvchar(ch),gethead(fptr));
  106.         if (gethead(fptr) == NIL)
  107.         settail(fptr,lptr);
  108.         sethead(fptr,lptr);
  109.     }
  110.     }
  111.     
  112.     /* otherwise, it must be a file */
  113.     else
  114.     setsavech(fptr,ch);
  115. }
  116.  
  117. /* xlpeek - peek at a character from a file or stream */
  118. int xlpeek(fptr)
  119.   LVAL fptr;
  120. {
  121.     LVAL lptr,cptr;
  122.     int ch;
  123.  
  124.     /* check for input from nil */
  125.     if (fptr == NIL)
  126.     ch = EOF;
  127.  
  128.     /* otherwise, check for input from a stream */
  129.     else if (ustreamp(fptr)) {
  130.     if ((lptr = gethead(fptr)) == NIL)
  131.         ch = EOF;
  132.     else {
  133.         if (!consp(lptr) || (cptr = car(lptr)) == NIL || !charp(cptr))
  134.         xlfail("bad stream");
  135.         ch = getchcode(cptr);
  136.     }
  137.     }
  138.  
  139.     /* otherwise, get the next file character and save it */
  140.     else {
  141.     ch = xlgetc(fptr);
  142.     setsavech(fptr,ch);
  143.     }
  144.  
  145.     /* return the character */
  146.     return (ch);
  147. }
  148.  
  149. /* xlputc - put a character to a file or stream */
  150. xlputc(fptr,ch)
  151.   LVAL fptr; int ch;
  152. {
  153.     LVAL lptr;
  154.     FILE *fp;
  155.  
  156.     /* count the character */
  157.     ++xlfsize;
  158.  
  159.     /* check for output to nil */
  160.     if (fptr == NIL)
  161.     ;
  162.  
  163.     /* otherwise, check for output to an unnamed stream */
  164.     else if (ustreamp(fptr)) {
  165.     lptr = consa(cvchar(ch));
  166.     if (gettail(fptr))
  167.         rplacd(gettail(fptr),lptr);
  168.     else
  169.         sethead(fptr,lptr);
  170.     settail(fptr,lptr);
  171.     }
  172.  
  173.     /* otherwise, check for terminal output or file output */
  174.     else {
  175.     fp = getfile(fptr);
  176.     if (fp == stdout || fp == stderr)
  177.         ostputc(ch);
  178.     else
  179.         osaputc(ch,fp);
  180.     }
  181. }
  182.  
  183. /* xlflush - flush the input buffer */
  184. int xlflush()
  185. {
  186.     osflush();
  187. }
  188.  
  189. /* stdprint - print to *standard-output* */
  190. stdprint(expr)
  191.   LVAL expr;
  192. {
  193.     xlprint(getvalue(s_stdout),expr,TRUE);
  194.     xlterpri(getvalue(s_stdout));
  195. }
  196.  
  197. /* stdputstr - print a string to *standard-output* */
  198. stdputstr(str)
  199.   char *str;
  200. {
  201.     xlputstr(getvalue(s_stdout),str);
  202. }
  203.  
  204. /* errprint - print to *error-output* */
  205. errprint(expr)
  206.   LVAL expr;
  207. {
  208.     xlprint(getvalue(s_stderr),expr,TRUE);
  209.     xlterpri(getvalue(s_stderr));
  210. }
  211.  
  212. /* errputstr - print a string to *error-output* */
  213. errputstr(str)
  214.   char *str;
  215. {
  216.     xlputstr(getvalue(s_stderr),str);
  217. }
  218.  
  219. /* dbgprint - print to *debug-io* */
  220. dbgprint(expr)
  221.   LVAL expr;
  222. {
  223.     xlprint(getvalue(s_debugio),expr,TRUE);
  224.     xlterpri(getvalue(s_debugio));
  225. }
  226.  
  227. /* dbgputstr - print a string to *debug-io* */
  228. dbgputstr(str)
  229.   char *str;
  230. {
  231.     xlputstr(getvalue(s_debugio),str);
  232. }
  233.  
  234. /* trcprin1 - print to *trace-output* */
  235. trcprin1(expr)
  236.   LVAL expr;
  237. {
  238.     xlprint(getvalue(s_traceout),expr,TRUE);
  239. }
  240.  
  241. /* trcputstr - print a string to *trace-output* */
  242. trcputstr(str)
  243.   char *str;
  244. {
  245.     xlputstr(getvalue(s_traceout),str);
  246. }
  247.  
  248.  
  249.