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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
|
/* msstuff.c - ms-dos specific routines */
#include <stdlib.h>
#include <signal.h>
#include "xlisp.h"
#include "xmath.h"
#include "version.h"
#define LBSIZE 200
/* set the size for the overlay buffer */
unsigned _ovrbuffer = 0x1800;
/* external variables */
extern LVAL s_unbound,true;
extern FILEP tfp;
extern int errno;
/* make sure we get a large stack */
int _stklen = 32766;
/* local variables */
static char lbuf[LBSIZE];
static int lpos[LBSIZE];
static int lindex;
static int lcount;
static int lposition;
static long rseed = 1L;
static void errcatch(int);
/* osinit - initialize */
osinit(banner)
char *banner;
{
printf("%s\n",banner);
printf("XLISP-STAT Release %d.%d.%d%s.\n",
XLS_MAJOR_RELEASE, XLS_MINOR_RELEASE, XLS_SUBMINOR_RELEASE,
XLS_RELEASE_STATUS);
printf("Copyright (c) 1989-1999, by Luke Tierney.\n");
printf("Trimmed-down DOS Version");
if (! _OvrInitEms(0, 0, 0))
printf(" -- using extenden memory for swapping.\n");
else if (! _OvrInitExt(0L, 0L))
printf(" -- using expanded memory for swapping.\n");
else
printf(" -- no extra memory for swapping.\n");
printf("Initialization may take a moment.\n\n");
lposition = 0;
lindex = 0;
lcount = 0;
signal(SIGABRT, errcatch);
signal(SIGFPE, errcatch);
signal(SIGILL, errcatch);
signal(SIGSEGV, errcatch);
}
/* osfinish - clean up before returning to the operating system */
osfinish()
{
}
/* oserror - print an error message */
xoserror(msg)
char *msg;
{
printf("error: %s\n",msg);
}
# ifdef XLISP_ONLY
/* osrand - return a random number between 0 and n-1 */
int osrand(n)
int n;
{
long k1;
/* make sure we don't get stuck at zero */
if (rseed == 0L) rseed = 1L;
/* algorithm taken from Dr. Dobbs Journal, November 1985, page 91 */
k1 = rseed / 127773L;
if ((rseed = 16807L * (rseed - k1 * 127773L) - k1 * 2836L) < 0L)
rseed += 2147483647L;
/* return a random number between 0 and n-1 */
return ((int)(rseed % (long)n));
}
#endif /* XLISP_ONLY */
/* osaopen - open an ascii file */
FILE *osaopen(name,mode)
char *name,*mode;
{
return (fopen(name,mode));
}
/* osbopen - open a binary file */
FILE *osbopen(name,mode)
char *name,*mode;
{
char bmode[10];
strcpy(bmode,mode); strcat(bmode,"b");
return (fopen(name,bmode));
}
/* osclose - close a file */
int osclose(fp)
FILE *fp;
{
return (fclose(fp));
}
/* osagetc - get a character from an ascii file */
int osagetc(fp)
FILE *fp;
{
return (getc(fp));
}
/* osaputc - put a character to an ascii file */
int osaputc(ch,fp)
int ch; FILE *fp;
{
return (putc(ch,fp));
}
/* osbgetc - get a character from a binary file */
int osbgetc(fp)
FILE *fp;
{
return (getc(fp));
}
/* osbputc - put a character to a binary file */
int osbputc(ch,fp)
int ch; FILE *fp;
{
return (putc(ch,fp));
}
/* ostgetc - get a character from the terminal */
int ostgetc()
{
int ch;
/* check for a buffered character */
if (lcount--)
return (lbuf[lindex++]);
/* get an input line */
for (lcount = 0; ; )
switch (ch = xgetc()) {
case '\r':
lbuf[lcount++] = '\n';
xputc('\r'); xputc('\n'); lposition = 0;
if (tfp!=CLOSED) OSWRITE(lbuf,1,lcount,tfp);
lindex = 0; lcount--;
return (lbuf[lindex++]);
case '\010':
case '\177':
if (lcount) {
lcount--;
while (lposition > lpos[lcount]) {
xputc('\010'); xputc(' '); xputc('\010');
lposition--;
}
}
break;
case '\032':
xflush();
return (EOF);
default:
if (ch == '\t' || (ch >= 0x20 && ch < 0x7F)) {
lbuf[lcount] = ch;
lpos[lcount] = lposition;
if (ch == '\t')
do {
xputc(' ');
} while (++lposition & 7);
else {
xputc(ch); lposition++;
}
lcount++;
}
else {
xflush();
switch (ch) {
case '\003': xlsigint(); /* control-c */
case '\007': xlcleanup(); /* control-g */
case '\020': xlcontinue(); /* control-p */
case '\032': return (EOF); /* control-z */
default: return (ch);
}
}
}
}
/* ostputc - put a character to the terminal */
ostputc(ch)
int ch;
{
/* check for control characters */
oscheck();
/* output the character */
if (ch == '\n') {
xputc('\r'); xputc('\n');
lposition = 0;
}
else {
xputc(ch);
lposition++;
}
/* output the character to the transcript file */
if (tfp != CLOSED)
OSPUTC(ch,tfp);
}
/* osflush - flush the terminal input buffer */
osflush()
{
lindex = lcount = 0;
}
/* oscheck - check for control characters during execution */
oscheck()
{
int ch;
if (ch = xcheck())
switch (ch) {
case '\002': /* control-b */
xflush();
xlbreak("BREAK",s_unbound);
break;
case '\003': /* control-c */
xflush();
xlsigint();
break;
case '\024': /* control-t */
xinfo();
break;
}
}
ossymbols()
{
statsymbols();
}
osfinit()
{
statfinit();
}
/* xinfo - show information on control-t */
static xinfo()
{
extern int nfree,gccalls;
extern long total;
char buf[80];
sprintf(buf,"\n[ Free: %d, GC calls: %d, Total: %ld ]",
nfree,gccalls,total);
errputstr(buf);
}
/* xflush - flush the input line buffer and start a new line */
static xflush()
{
osflush();
ostputc('\n');
}
/* xgetc - get a character from the terminal without echo */
static int xgetc()
{
return (bdos(7) & 0xFF);
}
/* xputc - put a character to the terminal */
static xputc(ch)
int ch;
{
bdos(6,ch);
}
/* xcheck - check for a character */
static int xcheck()
{
return (bdos(6,0xFF));
}
/* xsystem - execute a system command */
LVAL xsystem()
{
char *cmd="COMMAND";
if (moreargs())
cmd = (char *)getstring(xlgastring());
xllastarg();
return (system(cmd) == 0 ? true : cvfixnum((FIXTYPE)errno));
}
/* xgetkey - get a key from the keyboard */
LVAL xgetkey()
{
xllastarg();
return (cvfixnum((FIXTYPE)xgetc()));
}
#ifdef max
#undef max
#endif
max(x, y)
int x, y;
{
return((x > y) ? x : y);
}
#ifdef min
#undef min
#endif
min(x, y)
int x, y;
{
return((x < y) ? x : y);
}
SysBeep(n)
int n;
{
n = n / 10;
do {
printf("\007");
} while (n-- > 0);
fflush(stdout);
}
#undef getenv
char *getdosenv(char *s)
{
return(getenv(s));
}
static void errcatch(int sig)
{
signal(sig, errcatch);
switch(sig) {
case SIGABRT: xlfail("Abnormal termination signal -- time to bail out");
case SIGFPE: xlfail("Floating point exception.");
case SIGILL: xlfail("Illegal instruction -- time to bail out");
case SIGSEGV: xlfail("Segment violation -- time to bail out");
default: xexit();
}
}
int cdecl matherr(struct exception *e) { return(1); }
int renamebackup(name) char *name; { return(TRUE); }
/* xgetwd - builtin function GET-WORKING-DIRECTORY */
LVAL xgetwd()
{
xllastarg();
if (! getcwd(buf, FNAMEMAX))
return NIL;
else
return cvstring(buf);
}
/* xsetwd - builtin function SET-WORKING-DIRECTORY */
LVAL xsetwd()
{
char *dir = getstring(xlgastring());
xllastarg();
if (chdir(dir))
return NIL;
else
return s_true;
}
|