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 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
|
/* mswstuff.c - ms-windows specific routines */
#include <dos.h>
#include <signal.h>
#include <mem.h>
#include <dir.h>
#include <time.h>
#include <float.h>
#include <sys/stat.h>
#include "xlisp.h"
#include "xlstat.h"
#include "xlgraph.h"
#include "wxlisp.h"
#include "ledit.h"
#include "winutils.h"
#include "version.h"
#define LBSIZE 200
/* external variables */
extern FILEP tfp;
extern int errno;
extern int Exiting;
extern char *iniFile;
extern LVAL s_event_queue, s_in_callback;
/* local variables */
static char lbuf[LBSIZE];
static int lindex;
static int lcount;
#ifdef XLISP_ONLY
static long rseed = 1L;
#endif
static char *xfgets _((char *s, int n));
static void errcatch(int);
char *stackbase;
int xlisp_mono;
/* osinit - initialize */
void osinit(banner)
char *banner;
{
#ifdef STSZ
stackbase = (char *)&banner; /* find base of stack */
#endif
GetPrivateProfileString("Graphics", "COLOR", "", buf, STRMAX, iniFile);
if (! stricmp(buf, "off") || ! stricmp(buf, "no") || getenv("XLISPMONO"))
xlisp_mono = TRUE;
else
xlisp_mono = FALSE;
sprintf(lbuf, "%s\n",banner);
TTYPutStr(lbuf);
sprintf(lbuf, "XLISP-STAT Release %d.%d.%d%s.\n",
XLS_MAJOR_RELEASE, XLS_MINOR_RELEASE, XLS_SUBMINOR_RELEASE,
XLS_RELEASE_STATUS);
TTYPutStr(lbuf);
TTYPutStr("Copyright (c) 1989-1999, by Luke Tierney.\n\n");
lposition = 0;
lindex = 0;
lcount = 0;
#ifndef MinGW32
_control87(MCW_EM, MCW_EM);
#endif
signal(SIGABRT, errcatch);
signal(SIGFPE, errcatch);
signal(SIGILL, errcatch);
signal(SIGSEGV, errcatch);
}
/* osfinish - clean up before returning to the operating system */
void osfinish()
{
CONTEXT cntxt;
xlbegin(&cntxt,CF_TOPLEVEL|CF_CLEANUP|CF_BRKLEVEL,(LVAL)1);
if (setjmp(cntxt.c_jmpbuf))
exit(0); /*** this probably needs to be done differently */
if (s_breakenable != NULL) setvalue(s_breakenable, NIL);
#ifdef CONDITIONS
if (s_condition_hook != NULL) setvalue(s_condition_hook, NIL);
#endif /* CONDITIONS */
Exiting = TRUE;
PostQuitMessage(0);
ExitXLS();
}
/* xoserror - print an error message */
void xoserror(msg)
char *msg;
{
WarningBox(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 */
#ifdef MinGW32
int truename(char *name, char *rname)
{
if (GetFullPathName(name, FNAMEMAX + 1, rname, NULL) == 0)
return FALSE;
else {
char *p;
/* lowercase the whole string */
for (p = rname; *p != 0; p++)
if (isupper(*p))
*p = (char) tolower(*p);
return TRUE;
}
}
#else
int truename(char *name, char *rname)
{
int i;
char *cp;
int drive; /* drive letter */
char pathbuf[FNAMEMAX+1]; /* copy of path part of name */
char curdir[FNAMEMAX+1]; /* current directory of drive */
char *fname; /* pointer to file name part of name */
/* use backslashes consistantly */
for (cp = name; (cp = strchr(cp, '/')) != NULL; *cp = '\\') ;
/* parse any drive specifier */
if ((cp = strrchr(name, ':')) != NULL) {
if (cp != name+1 || !isalpha(*name)) return FALSE;
drive = toupper(*name);
name = cp+1; /* name now excludes drivespec */
}
else {
drive = 'A' + getdisk();
}
/* check for absolute path (good news!) */
if (*name == '\\') {
sprintf(rname,"%c:%s",drive,name);
}
else {
strcpy(pathbuf, name);
if ((cp = strrchr(pathbuf, '\\')) != NULL) { /* path present */
cp[1] = 0;
fname = strrchr(name, '\\') + 1;
}
else {
pathbuf[0] = 0;
fname = name;
}
/* get the current directory of the selected drive */
if (getcurdir(drive - 'A' + 1, curdir))
return FALSE; /* invalid drive */
/* peel off "..\"s */
while (strncmp(pathbuf, "..\\", 3) == 0) {
if (*curdir == 0) return FALSE; /* already at root */
strcpy(pathbuf, pathbuf+3);
if ((cp=strrchr(curdir, '\\')) != NULL)
*cp = 0; /* peel one depth of directories */
else
*curdir = 0; /* peeled back to root */
}
/* allow for a ".\" */
if (strncmp(pathbuf, ".\\", 2) == 0)
strcpy(pathbuf, pathbuf+2);
/* final name is drive:\curdir\pathbuf\fname */
if (strlen(pathbuf)+strlen(curdir)+strlen(fname)+4 > FNAMEMAX)
return FALSE;
if (*curdir)
sprintf(rname, "%c:\\%s\\%s%s", drive, curdir, pathbuf, fname);
else
sprintf(rname, "%c:\\%s%s", drive, pathbuf, fname);
}
/* lowercase the whole string */
for (cp = rname; (i = *cp) != 0; cp++) {
if (isupper(i)) *cp = (char) tolower(i);
}
return TRUE;
}
#endif
int getslot(VOID)
{
int i=0;
for (; i < FTABSIZE; i++) /* look for available slot */
if (filetab[i].fp == NULL) return i;
gc(); /* is this safe??????? */
for (i=0; i < FTABSIZE; i++) /* try again -- maybe one has been freed */
if (filetab[i].fp == NULL) return i;
xlfail("too many open files");
return 0; /* never returns */
}
FILEP osaopen(const char *name, const char *mode)
{
int i=getslot();
char namebuf[FNAMEMAX+1];
FILE *fp;
if (!truename((char *)name, namebuf))
strcpy(namebuf, name); /* should not happen */
if ((filetab[i].tname = malloc(strlen(namebuf)+1)) == NULL) {
xlfail("insufficient memory");
}
if ((fp = fopen(name,mode)) == NULL) {
free(filetab[i].tname);
return CLOSED;
}
filetab[i].fp = fp;
strcpy(filetab[i].tname, namebuf);
/* calculate mode to re-open file */
if (mode[0]=='w') {
strcpy(filetab[i].reopenmode, "r+");
if (mode[strlen(mode)-1]=='b') strcat(filetab[i].reopenmode, "b");
}
else strcpy(filetab[i].reopenmode, mode);
return i;
}
FILEP osbopen(const char *name, const char *mode)
{
char bmode[10];
strcpy(bmode,mode); strcat(bmode,"b");
return osaopen(name, bmode);
}
VOID osclose(FILEP f)
{
fclose(filetab[f].fp);
free(filetab[f].tname);
filetab[f].tname = NULL;
filetab[f].fp = NULL;
}
/* ostgetc - get a character from the terminal */
int ostgetc()
{
while(--lcount < 0 ) {
if ( xfgets(lbuf,LBSIZE) == NULL )
return( EOF );
lcount = strlen( lbuf );
if (tfp!=CLOSED) OSWRITE(lbuf,1,lcount,tfp);
lindex = 0;
lposition = 0;
}
return( lbuf[lindex++] );
}
LOCAL char *xfgets(s, n)
char *s;
int n;
{
int c;
char *cs;
cs = s;
while (--n > 0 && (c = TTYGetC()) != EOF) {
*cs++ = (char) c;
if (c == '\n') break;
}
if (c == EOF && cs==s) return(NULL);
*cs++ = '\0';
return(s);
}
/* ostputc - put a character to the terminal */
void ostputc(ch)
int ch;
{
if (ch == '\n') lposition = 0;
else lposition++;
if (tfp != CLOSED) OSPUTC(ch,tfp);
TTYPutC(ch);
}
/* osflush - flush the terminal input buffer */
void osflush()
{
TTYFlush();
lindex = lcount = 0;
}
VOID osforce(fp)
FILEP fp;
{
if (fp == CONSOLE)
TTYFlush();
else
fflush(filetab[fp].fp);
}
void ossymbols()
{
statsymbols();
}
void osfinit()
{
}
#ifdef max
#undef max
#endif
int max(x, y)
int x, y;
{
return((x > y) ? x : y);
}
#ifdef min
#undef min
#endif
int min(x, y)
int x, y;
{
return((x < y) ? x : y);
}
LVAL string2stream(char *s)
{
LVAL stream, newustream();
xlsave1(stream);
stream = newustream();
for (; *s != '\0'; s++) xlputc(stream, *s);
xlpop();
return(stream);
}
LVAL readevalstream(LVAL stream)
{
LVAL expr, oldenv, oldfenv, val;
CONTEXT cntxt;
if (! ustreamp(stream)) xlfail("not a ustream");
/* protect some pointers */
xlstkcheck(4);
xlprotect(stream);
xlsave(expr);
xlsave(oldenv);
xlsave(oldfenv);
/* set the lexical environment to null */
oldenv = xlenv; xlenv = NIL;
oldfenv = xlfenv; xlfenv = NIL;
val = NIL;
/* read and evaluate each expression in the stream */
xlbegin(&cntxt,CF_ERROR,s_true);
if (! setjmp(cntxt.c_jmpbuf)) {
while (xlread(stream,&expr,FALSE,FALSE)) {
val = xleval(expr);
}
}
xlend(&cntxt);
/* reset the environment */
xlenv = oldenv;
xlfenv = oldfenv;
xlpopn(4);
return val;
}
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();
}
}
#if defined(__TURBOC__) && (__TURBOC__ >= 0x0400)
int _FARFUNC _matherr (struct exception *e)
#pragma argsused
{
return 1;
}
#else
int __cdecl matherr(struct exception *e)
{
return(1);
}
#endif
void osreset()
{
#ifndef NOGRAPHICS
MSWResetMenus();
MSWResetDialogs();
MSWResetGraphics();
#endif /* NOGRAPHICS */
}
#ifdef NOGRAPHICS
set_gc_cursor() {}
#endif /* NOGRAPHICS */
#ifndef HZ
#define HZ 60
#endif
unsigned long ticks_per_second() { return((unsigned long) HZ); }
unsigned long real_tick_count()
{
return((unsigned long) ((HZ / CLK_TCK) * clock()));
}
unsigned long run_tick_count()
{
return real_tick_count();
}
unsigned long system_tick_count()
{
return((unsigned long) (time((unsigned long *) NULL)));
}
int renamebackup(char *name)
#pragma argsused
{
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;
}
VOID disable_interrupts() {}
VOID enable_interrupts() {}
int osmtime(fname, mtime)
char *fname;
time_t *mtime;
{
struct stat s;
if (stat(fname, &s))
return -1;
*mtime = s.st_mtime;
return 0;
}
/* internal version of directory function */
/***** probably needs to be protected by check that dirent is available */
/***** need to drop non-files, i.e. directories, using stat() call */
#include <dirent.h>
LVAL dirlist P1C(char *, name)
{
LVAL val;
DIR *dir;
disable_interrupts();
xlsave1(val);
val = NIL;
if ((dir = opendir(name))) {
struct dirent *dentry;
while ((dentry = readdir(dir)))
val = cons(cvstring(dentry->d_name), val);
closedir(dir);
}
xlpop();
enable_interrupts();
return val;
}
void get_directory(char *s)
{
int n;
char *dir = getenv("XLISPLIB");
if (dir == NULL) dir = "";
strcpy(s, dir);
n = strlen(s);
if (n > 0 && s[n - 1] != '\\')
strcat(s, "\\");
}
int MSWAnyEventsQueued()
{
LVAL queue = getvalue(s_event_queue);
return consp(queue) ? TRUE : FALSE;
}
void MSWDoEventQueue(void)
{
LVAL task, queue, oldenv, oldfenv, olddenv;
queue = getvalue(s_event_queue);
if (consp(queue)) {
olddenv = xldenv;
xldbind(s_in_callback, s_true);
/* set the lexical environment to null */
xlstkcheck(2);
xlsave(oldenv);
xlsave(oldfenv);
oldenv = xlenv; xlenv = NIL;
oldfenv = xlfenv; xlfenv = NIL;
task = car(queue);
setvalue(s_event_queue, cdr(queue));
xleval(task);
/* reset the environment */
xlenv = oldenv;
xlfenv = oldfenv;
xlpopn(2);
xlunbind(olddenv);
}
}
#ifdef BIGNUMS
/* We need this because the one that Borland supplies doesn't work for
exp values out of range */
unsigned char infp[8] = {0,0,0,0,0,0,0xf0, 0x7f};
unsigned char infn[8] = {0,0,0,0,0,0,0xf0, 0xff};
double myldexp(double val, int exp) {
if (exp > DBL_MAX_EXP)
return (val > 0 ? *(double *)&infp : *(double *)&infn);
if (exp < DBL_MIN_EXP) return 0.0;
return ldexp(val, exp);
}
#endif
|