1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
/* xlio - xlisp i/o routines */
/* Copyright (c) 1985, by David Michael Betz
All Rights Reserved
Permission is granted for unrestricted non-commercial use */
/* CHANGE LOG
* --------------------------------------------------------------------
* 28Apr03 dm eliminate some compiler warnings
*/
#include "xlisp.h"
/* external variables */
extern LVAL s_stdin,s_stdout,s_stderr,s_debugio,s_traceout,s_unbound;
extern int xlfsize;
#ifdef DEBUG_INPUT
extern FILE *read_by_xlisp;
#endif
/* xlgetc - get a character from a file or stream */
int xlgetc(LVAL fptr)
{
LVAL lptr, cptr=NULL;
FILE *fp;
int ch;
/* check for input from nil */
if (fptr == NIL)
ch = EOF;
/* otherwise, check for input from a stream */
else if (ustreamp(fptr)) {
if ((lptr = gethead(fptr)) == NIL)
ch = EOF;
else {
if (!consp(lptr) || (cptr = car(lptr)) == NIL || !charp(cptr))
xlfail("bad stream");
sethead(fptr,lptr = cdr(lptr));
if (lptr == NIL)
settail(fptr,NIL);
ch = getchcode(cptr);
}
}
/* otherwise, check for a buffered character */
else if ((ch = getsavech(fptr)))
setsavech(fptr,'\0');
/* otherwise, check for terminal input or file input */
else {
fp = getfile(fptr);
if (fp == stdin || fp == STDERR)
ch = ostgetc();
else
ch = osagetc(fp);
#ifdef DEBUG_INPUT
if (read_by_xlisp && ch != -1) {
putc(ch, read_by_xlisp);
}
#endif
}
/* return the character */
return (ch);
}
/* xlungetc - unget a character */
void xlungetc(LVAL fptr, int ch)
{
LVAL lptr;
/* check for ungetc from nil */
if (fptr == NIL || ch == EOF)
;
/* otherwise, check for ungetc to a stream */
else if (ustreamp(fptr)) {
if (ch != EOF) {
lptr = cons(cvchar(ch),gethead(fptr));
if (gethead(fptr) == NIL)
settail(fptr,lptr);
sethead(fptr,lptr);
}
}
/* otherwise, it must be a file */
else
setsavech(fptr,ch);
}
/* xlpeek - peek at a character from a file or stream */
int xlpeek(LVAL fptr)
{
LVAL lptr, cptr=NULL;
int ch;
/* check for input from nil */
if (fptr == NIL)
ch = EOF;
/* otherwise, check for input from a stream */
else if (ustreamp(fptr)) {
if ((lptr = gethead(fptr)) == NIL)
ch = EOF;
else {
if (!consp(lptr) || (cptr = car(lptr)) == NIL || !charp(cptr))
xlfail("bad stream");
ch = getchcode(cptr);
}
}
/* otherwise, get the next file character and save it */
else {
ch = xlgetc(fptr);
setsavech(fptr,ch);
}
/* return the character */
return (ch);
}
/* xlputc - put a character to a file or stream */
void xlputc(LVAL fptr, int ch)
{
LVAL lptr;
FILE *fp;
/* count the character */
++xlfsize;
/* check for output to nil */
if (fptr == NIL)
;
/* otherwise, check for output to an unnamed stream */
else if (ustreamp(fptr)) {
lptr = consa(cvchar(ch));
if (gettail(fptr))
rplacd(gettail(fptr),lptr);
else
sethead(fptr,lptr);
settail(fptr,lptr);
}
/* otherwise, check for terminal output or file output */
else {
fp = getfile(fptr);
if (!fp)
xlfail("file not open");
else if (fp == stdout || fp == STDERR)
ostputc(ch);
else
osaputc(ch,fp);
}
}
/* xloutflush -- flush output buffer */
void xloutflush(LVAL fptr)
{
FILE *fp;
/* check for output to nil or unnamed stream */
if (fptr == NIL || ustreamp(fptr))
;
/* otherwise, check for terminal output or file output */
else {
fp = getfile(fptr);
if (!fp)
xlfail("file not open");
else if (fp == stdout || fp == STDERR)
ostoutflush();
else
osoutflush(fp);
}
}
/* xlflush - flush the input buffer */
void xlflush(void)
{
osflush();
}
/* stdprint - print to *standard-output* */
void stdprint(LVAL expr)
{
xlprint(getvalue(s_stdout),expr,TRUE);
xlterpri(getvalue(s_stdout));
}
/* stdputstr - print a string to *standard-output* */
void stdputstr(const char *str)
{
xlputstr(getvalue(s_stdout),str);
}
/* stdflush - flush the *standard-output* buffer */
void stdflush()
{
xloutflush(getvalue(s_stdout));
}
/* errprint - print to *error-output* */
void errprint(LVAL expr)
{
xlprint(getvalue(s_stderr),expr,TRUE);
xlterpri(getvalue(s_stderr));
}
/* errputstr - print a string to *error-output* */
void errputstr(const const char *str)
{
xlputstr(getvalue(s_stderr),str);
}
/* dbgprint - print to *debug-io* */
void dbgprint(LVAL expr)
{
xlprint(getvalue(s_debugio),expr,TRUE);
xlterpri(getvalue(s_debugio));
}
/* dbgputstr - print a string to *debug-io* */
void dbgputstr(const char *str)
{
xlputstr(getvalue(s_debugio),str);
}
/* trcprin1 - print to *trace-output* */
void trcprin1(LVAL expr)
{
xlprint(getvalue(s_traceout),expr,TRUE);
}
/* trcputstr - print a string to *trace-output* */
void trcputstr(const char *str)
{
xlputstr(getvalue(s_traceout),str);
}
|