eus.c
Go to the documentation of this file.
00001 /****************************************************************/
00002 /* eus.c:
00003 /*      Toplevel, exception(error, signal) handler and initializers
00004 /*      Copyright(c)1988, Toshihiro MATSUI, Electrotechnical Laboratory
00005 /*      1986 April:     created 
00006 /*      1994 June:      multi thread
00007 /*      1996 January:   handles SIGSEGV, SIGBUS
00008 /****************************************************************/
00009 static char *rcsid="@(#) $Id$";
00010 
00011 #include "eus.h"
00012 
00013 #include <signal.h>
00014 #include <dlfcn.h>
00015 #include <fcntl.h>
00016 
00017 #if Solaris2
00018 #include <synch.h>
00019 #include <thread.h>
00020 #elif SunOS4_1
00021 #include <lwp/stackdep.h>
00022 #endif
00023 
00024 #if !THREADED
00025 unsigned int thr_self() { return(1);}
00026 #endif
00027 
00028 
00029 #if Linux
00030 #define M_MMAP_MAX (-4)
00031 #endif
00032 #if Darwin
00033 int _end;
00034 #endif
00035 
00036 /*variables*/
00037 /* process id and program name*/
00038 eusinteger_t mypid;
00039 char *progname;
00040 #if (WORD_SIZE == 64)
00041 eusinteger_t setjmp_val;
00042 #endif
00043 
00044 /* heap management */
00045 /* every free cell is linked to this structure*/
00046 struct buddyfree  buddy[MAXBUDDY+1];
00047 extern pointer *gcstack, *gcsp, *gcsplimit;
00048 #define DEFAULT_MAX_GCSTACK 16384
00049 
00050 
00051 /* System internal objects are connected to sysobj list
00052 /* to protect from being garbage-collected */
00053 
00054 pointer sysobj;
00055 
00056 /* context */
00057 context *mainctx;
00058 pointer mainport;
00059 #if Solaris2
00060   thread_t maintid;
00061 #endif
00062 
00063 
00064 /****************************************************************/
00065 /* system defined (built-in) class index
00066 /*      modified to accept  dynamic type extension (1987-Jan)
00067 /****************************************************************/
00068 
00069 cixpair objectcp;
00070 cixpair conscp;
00071 cixpair propobjcp;
00072 cixpair symbolcp;
00073 cixpair packagecp;
00074 cixpair streamcp;
00075 cixpair filestreamcp;
00076 cixpair iostreamcp;
00077 cixpair metaclasscp;
00078 cixpair vecclasscp;
00079 cixpair codecp;
00080 cixpair fcodecp;
00081 /*cixpair modulecp; */
00082 cixpair ldmodulecp;
00083 cixpair closurecp;
00084 cixpair labrefcp;
00085 cixpair threadcp;
00086 cixpair arraycp;
00087 cixpair readtablecp;
00088 cixpair vectorcp;
00089 cixpair fltvectorcp;
00090 cixpair intvectorcp;
00091 cixpair stringcp;
00092 cixpair bitvectorcp;
00093 /* extended numbers */
00094 cixpair extnumcp;
00095 cixpair ratiocp;
00096 cixpair complexcp;
00097 cixpair bignumcp;
00098 
00099 
00100 struct built_in_cid  builtinclass[64];
00101 int nextbclass;
00102 
00103 long buddysize[MAXBUDDY+1];
00104 struct buddyfree buddy[MAXBUDDY+1];
00105 context *euscontexts[MAXTHREAD];
00106 
00107 
00108 /*symbol management*/
00109 pointer pkglist,lisppkg,keywordpkg,userpkg,syspkg,unixpkg,xpkg;
00110 pointer NIL,PACKAGE,T,QUOTE;
00111 pointer FUNCTION;
00112 pointer QDECLARE,QSPECIAL;
00113 #if SunOS4_1 /* SELF is already used on SunOS 4.1.x. */
00114 pointer QSELF;
00115 #else
00116 pointer SELF;
00117 #endif
00118 pointer CLASS;
00119 pointer STDIN,STDOUT,ERROUT,QSTDIN,QSTDOUT,QERROUT;
00120 pointer QINTEGER,QFIXNUM,QFLOAT,QNUMBER;
00121 pointer TOPLEVEL,QEVALHOOK,ERRHANDLER,FATALERROR;
00122 pointer QGCHOOK, QEXITHOOK;
00123 pointer QUNBOUND,QDEBUG;
00124 pointer QTHREADS;       /* system:*threads* */
00125 pointer QPARAGC;
00126 pointer QVERSION;
00127 pointer QEQ,QEQUAL,QNOT, QAND, QOR;
00128 
00129 /* keywords */
00130 pointer K_IN,K_OUT,K_IO;        /*direction keyword*/
00131 pointer K_FLUSH,K_FILL,K_FILE,K_STRING;
00132 pointer K_NOMETHOD,K_BIT,K_BYTE,K_CHAR,K_SHORT,K_LONG,K_INTEGER,K_POINTER;
00133 pointer K_FLOAT32,K_FLOAT,K_DOUBLE,K_FOREIGN, K_FOREIGN_STRING;
00134 pointer K_DOWNCASE,K_UPCASE, K_PRESERVE, K_INVERT, K_CAPITALIZE;
00135 pointer K_DISPOSE;
00136 
00137 /*class management*/
00138 struct class_desc  classtab[MAXCLASS];
00139 int nextcix;
00140 
00141 /*class cells*/
00142 pointer C_CONS, C_OBJECT, C_SYMBOL, C_PACKAGE;
00143 pointer C_STREAM, C_FILESTREAM, C_IOSTREAM, C_CODE, C_FCODE, C_LDMOD;
00144 pointer C_VECTOR, C_METACLASS, C_CLOSURE, C_LABREF;
00145 pointer C_THREAD;
00146 pointer C_VCLASS, C_FLTVECTOR, C_INTVECTOR, C_STRING, C_BITVECTOR;
00147 pointer C_FOREIGNCODE,C_ARRAY,C_READTABLE;
00148 pointer C_EXTNUM, C_RATIO, C_BIGNUM, C_COMPLEX;
00149 
00150 /*class names*/
00151 pointer QCONS,STRING,STREAM,FILESTREAM,IOSTREAM,SYMBOL, 
00152         CODE,FCODE, LDMODULE, PKGCLASS,METACLASS,CLOSURE,LABREF;
00153 pointer THREAD;
00154 pointer VECTOR,VECCLASS,FLTVECTOR,INTVECTOR,OBJECT,READTABLE;
00155 pointer FOREIGNCODE,ARRAY,BITVECTOR;
00156 pointer EXTNUM, RATIO, COMPLEX, BIGNUM;
00157 
00158 /*toplevel & evaluation control*/
00159 int intsig,intcode;
00160 int ehbypass;
00161 
00162 /*reader variables*/
00163 pointer charmacro[256];
00164 pointer sharpmacro[256];
00165 
00166 extern pointer defvector();
00167 static pointer reploop(context *, char *);
00168 
00169 pointer ALLOWOTHERKEYS,K_ALLOWOTHERKEYS;
00170 pointer OPTIONAL,REST,KEY,AUX,MACRO,LAMBDA,LAMCLOSURE,COMCLOSURE;
00171 pointer PRCIRCLE,PROBJECT,PRSTRUCTURE,PRCASE,PRLENGTH,PRLEVEL;
00172 pointer RANDSTATE,FEATURES,READBASE,PRINTBASE,QREADTABLE,QTERMIO;
00173 pointer GCMERGE,GCMARGIN, QLDENT;
00174 pointer K_PRIN1;
00175 pointer K_FUNCTION_DOCUMENTATION, K_VARIABLE_DOCUMENTATION,
00176         K_CLASS_DOCUMENTATION, K_METHOD_DOCUMENTATION, K_CLASS;
00177 pointer QLOADED_MODULES;
00178 
00179 static pointer PROPOBJ,C_PROPOBJ;
00180 
00181 pointer eussigvec[NSIG];
00182 
00183 pointer sysmod;
00184 jmp_buf topjbuf;
00185 
00186 /****************************************************************/
00187 /* error handler
00188 */
00189 
00190 char *errmsg[100]={
00191         "",                             /*0*/
00192         "stack overflow",               /*1 errcode=1..10 are fatal errors*/
00193         "allocation",                   /*2*/
00194         "",                             /*3*/
00195         "",                             /*4*/
00196         "",                             /*5*/
00197         "",                             /*6*/
00198         "",                             /*7*/
00199         "",                             /*8*/
00200         "",                             /*9*/
00201         "",                             /*10    end of fatal error*/
00202         "attempt to set to constant",   /*11 E_SETCONST */
00203         "unbound variable",             /*12 E_UNBOUND  */
00204         "undefined function",           /*13 E_UNDEF    */
00205         "mismatch argument",            /*14 E_MISMATCHARG */
00206         "illegal function",             /*15 E_ILLFUNC */
00207         "illegal character",            /*16 E_ILLCH */
00208         "illegal delimiter",            /*17 E_READ */
00209         "write?",                       /*18 E_WRITE*/
00210         "too long string",              /*19 E_LONGSTRING */
00211         "symbol expected",
00212         "list expected",
00213         "illegal lambda form",
00214         "illegal lambda parameter syntax",
00215         "no catcher found",
00216         "no such block",
00217         "stream expected",
00218         "illegal stream direction keyword",
00219         "integer expected",
00220         "string expected",
00221         "error in open file",
00222         "EOF hit",
00223         "number expected",
00224         "class table overflow",
00225         "class expected",
00226         "vector expected",
00227         "array size must be positive",
00228         "duplicated object variable name",
00229         "cannot make instance",
00230         "array index out of range",             /*  E_ARRAYINDEX */
00231         "cannot find method",
00232         "circular list",
00233         "unknown sharp macro",
00234         "list expected for an element of an alist",
00235         "macro expected",
00236         "no such package",
00237         "package name",
00238         "invalid lisp object form",
00239         "no such object variable",
00240         "sequence expected",
00241         "illegal start/end index",
00242         "no super class",
00243         "invalid format string",
00244         "float vector expected",
00245         "char code out of range",
00246         "vector dimension mismatch",
00247         "object expected",
00248         "type mismatch",
00249         "declaration is not allowed here",
00250         "illegal declaration form",
00251         "cannot be used for a variable",
00252         "illegal rotation axis",
00253         "multiple variable declaration",
00254         "illegal #n= or #n= label",
00255         "illegal #f( expression",
00256         "illegal #v or #j expression", 
00257         "invalid socket address",
00258         "array expected",
00259         "array dimension mismatch",
00260         "keyword expected for arguments",
00261         "no such keyword",
00262         "integer vector expected",
00263         "sequence index out of range",
00264         "not a bit vector",
00265         "no such external symbol",
00266         "symbol conflict",
00267         "",
00268         "E_END",
00269         };
00270 
00271 static pointer brkloop();
00272 
00273 void unwind(ctx,p)
00274 register context *ctx;
00275 register pointer *p;
00276 { pointer cleanup;
00277   while (ctx->protfp>=(struct protectframe *)p) {       /*clean-up unwind-protect form*/
00278     cleanup=ctx->protfp->cleaner;
00279     ctx->protfp=ctx->protfp->protlink;
00280         /*first, update protfp to avoid endless 
00281           evaluation of cleanup form because of an error*/
00282     ufuncall(ctx,cleanup,cleanup,NULL,NULL,0);}
00283         /*an error may occur if catch, throw or return-from or access to
00284           special variables are taken in clean up forms*/
00285         /*unwind specially bound variables*/
00286   unbindspecial(ctx,(struct specialbindframe *)p);
00287   /*unwind block frames*/
00288   while (ctx->blkfp>(struct blockframe *)p) ctx->blkfp=ctx->blkfp->dynklink;
00289   /*unwind catch frames*/
00290   while (ctx->catchfp>(struct catchframe *)p) ctx->catchfp=ctx->catchfp->nextcatch;
00291   /*unwind flet frames*/
00292   while (ctx->fletfp>(struct fletframe *)p) ctx->fletfp=ctx->fletfp->dynlink;
00293   }
00294 
00295 #ifdef USE_STDARG
00296 pointer error(enum errorcode ec, ...)
00297 #else
00298 pointer error(va_alist)
00299 va_dcl
00300 #endif
00301 { 
00302   va_list args;
00303   pointer errhandler;
00304   register char *errstr;
00305   register int argc;
00306   register context *ctx;
00307   pointer msg;
00308 
00309 #ifdef USE_STDARG
00310   va_start(args,ec);
00311 #else
00312   enum errorcode ec;
00313 
00314   va_start(args);
00315   ec = va_arg(args, enum errorcode);
00316 #endif
00317 
00318   ctx=euscontexts[thr_self()];
00319 
00320   /* error(errstr) must be error(E_USER,errstr) */
00321   if ((int)ec < E_END) errstr=errmsg[(int)ec];
00322   else {
00323       fprintf( stderr, "Internal warning: error: ec will be string.(%lx)\n",
00324               (long)ec );
00325       errstr=(char *)ec;
00326   }
00327 
00328   /*fatal error?  allocation failed or stack overflow? */
00329   if ((unsigned int)ec<=E_FATAL) {
00330     fprintf(stderr,"%s fatal error: th=%d %s\n",progname,thr_self(),errstr);
00331     if (speval(FATALERROR) != NIL) {
00332         fprintf(stderr, "exiting\n"); exit(ec);}
00333     else throw(ctx,makeint(0),NIL);}
00334 
00335   /* get extra message */
00336     switch((unsigned int)ec) {
00337       case E_UNBOUND: case E_UNDEF: case E_NOCLASS: case E_PKGNAME:
00338       case E_NOOBJ: case E_NOOBJVAR: case E_NOPACKAGE: case E_NOMETHOD:
00339       case E_NOKEYPARAM: case E_READLABEL: case E_ILLCH: case E_NOCATCHER:
00340       case E_NOVARIABLE: case E_EXTSYMBOL: case E_SYMBOLCONFLICT:
00341       case E_USER:
00342         msg = va_arg(args,pointer);     break;
00343     }
00344 
00345   /* call user's error handler function */
00346   errhandler=ctx->errhandler;
00347   if (errhandler==NIL || errhandler==NULL)  errhandler=Spevalof(ERRHANDLER);
00348   Spevalof(QEVALHOOK)=NIL;      /* reset eval hook */
00349   if (errhandler!=NIL) {
00350     vpush(makeint((unsigned int)ec));
00351     vpush(makestring(errstr,strlen(errstr)));
00352     if (ctx->callfp) vpush(ctx->callfp->form); else vpush(NIL);
00353     switch((unsigned int)ec) {
00354       case E_UNBOUND: case E_UNDEF: case E_NOCLASS: case E_PKGNAME:
00355       case E_NOOBJ: case E_NOOBJVAR: case E_NOPACKAGE: case E_NOMETHOD:
00356       case E_NOKEYPARAM: case E_READLABEL: case E_ILLCH: case E_NOCATCHER:
00357       case E_NOVARIABLE: case E_EXTSYMBOL: case E_SYMBOLCONFLICT:
00358         vpush(msg); argc=4; break;
00359       case E_USER:
00360         vpush(makestring((char*)msg,strlen((char*)msg))); argc=4; break;
00361     default: argc=3; break;}
00362     ufuncall(ctx,errhandler,errhandler,(pointer)(ctx->vsp-argc),ctx->bindfp,argc);
00363     ctx->vsp-=argc;
00364     }
00365 
00366   /*default error handler*/
00367   flushstream(ERROUT);
00368   fprintf(stderr,"%s: ERROR th=%d %s ",progname,thr_self(),errstr);
00369   switch((int)ec) {
00370       case E_UNBOUND: case E_UNDEF: case E_NOCLASS: case E_PKGNAME:
00371       case E_NOOBJ: case E_NOOBJVAR: case E_NOPACKAGE: case E_NOMETHOD:
00372       case E_NOKEYPARAM: case E_READLABEL: case E_ILLCH: case E_NOCATCHER:
00373       case E_NOVARIABLE: case E_EXTSYMBOL: case E_SYMBOLCONFLICT:
00374         prinx(ctx,msg,ERROUT); flushstream(ERROUT); break;
00375     }
00376   if( ec == E_USER ) {
00377       fprintf( stderr,"%p",msg ); flushstream(ERROUT); }
00378   else if (ispointer(msg)) {prinx(ctx,msg,ERROUT); flushstream(ERROUT); }
00379   if (ctx->callfp) {
00380     fprintf(stderr," in ");
00381     prinx(ctx,ctx->callfp->form,ERROUT);
00382     flushstream(ERROUT);}
00383   /*enter break loop*/
00384   brkloop(ctx,"E: ");
00385   throw(ctx,makeint(0),T);      /*throw to toplevel*/
00386   }
00387 
00388 #ifdef USE_STDARG
00389 pointer basicclass(char *name, ...)
00390 #else
00391 pointer basicclass(va_alist)
00392 va_dcl
00393 #endif
00394 {
00395   va_list ap;
00396   byte *vname;
00397   pointer super;
00398   cixpair *cixp;
00399   pointer classsym,class,varvector,superv,typevector,forwardvector;
00400   int n,i,svcount;
00401   context *ctx=mainctx;
00402 
00403 #ifdef USE_STDARG
00404   va_start(ap, name);
00405 #else
00406   char *name;
00407 
00408   va_start(ap);  
00409   name=va_arg(ap,byte *);
00410 #endif
00411   super=va_arg(ap,pointer);
00412   cixp=va_arg(ap,cixpair *); n=va_arg(ap,int);
00413   
00414   /*class name symbols are defined in lisp package*/
00415   classsym=intern(ctx,(char *)name,strlen(name),lisppkg);
00416   export(classsym,lisppkg);
00417   /* printf("name=%s NIL=%x super=%x\n",name,NIL,super); */
00418   if (super!=NIL) {
00419     superv= super->c.cls.vars;
00420     svcount=vecsize(superv);}
00421   else svcount=0;
00422   /* printf("name=%s super's_vcount=%d own_vcount=%d\n", name, svcount, n);*/
00423   varvector=makevector(C_VECTOR,svcount+n);  vpush(varvector);
00424   typevector=makevector(C_VECTOR,svcount+n);  vpush(typevector);
00425   forwardvector=makevector(C_VECTOR,svcount+n);  vpush(forwardvector);
00426   for (i=0; i<svcount; i++) {
00427     varvector->c.vec.v[i]=superv->c.vec.v[i];
00428     typevector->c.vec.v[i]=super->c.cls.types->c.vec.v[i];
00429     forwardvector->c.vec.v[i]=super->c.cls.forwards->c.vec.v[i];}
00430   for (i=0; i<n; i++) {
00431     vname=va_arg(ap,byte *);
00432     varvector->c.vec.v[i+svcount]=intern(ctx,(char *)vname,strlen((char *)vname),lisppkg);
00433     export(varvector->c.vec.v[i+svcount], lisppkg);  
00434     typevector->c.vec.v[i+svcount]=T;
00435     forwardvector->c.vec.v[i+svcount]=NIL;}
00436   class=makeclass(ctx,classsym,super,varvector,typevector,forwardvector,ELM_FIXED,0);
00437   builtinclass[nextbclass].cls=class;
00438   builtinclass[nextbclass].cp=cixp;
00439   nextbclass++;
00440   cixp->cix=intval(class->c.cls.cix);
00441   cixp->sub=classtab[cixp->cix].subcix;
00442   ctx->vsp-=3;
00443   va_end(ap);
00444   return(classsym);}
00445 
00446 
00447 /****************************************************************/
00448 /* initialization
00449 /****************************************************************/
00450 static void initmemory()
00451 { register int i;
00452   buddysize[0]=3; buddy[0].count=0; buddy[0].bp=0;
00453   buddysize[1]=3; buddy[1].count=0; buddy[1].bp=0;
00454   for (i=2; i<MAXBUDDY; i++) {
00455     buddy[i].count=0;
00456     buddysize[i]=buddysize[i-2]+buddysize[i-1]; /*fibonacci*/
00457     buddy[i].bp=0;}             /*no cells are connected*/
00458   buddy[MAXBUDDY].bp=(bpointer)(-1);    /*sentinel for alloc*/
00459 #if (WORD_SIZE == 64)
00460   buddysize[MAXBUDDY]= 0x7fffffffffffffff;      /*cell size limit*/
00461 #else
00462   buddysize[MAXBUDDY]= 0x7fffffff;      /*cell size limit*/
00463 #endif
00464 
00465   /*allocate enough memory for initialization*/
00466   newchunk(20);
00467   newchunk(18);
00468   gcstack=(pointer *)malloc(DEFAULT_MAX_GCSTACK * sizeof(pointer));
00469   gcsp=gcstack;
00470   gcsplimit= &gcstack[DEFAULT_MAX_GCSTACK -10];
00471   }
00472 
00473 #ifdef RGC
00474 void initmemory_rgc()
00475 { register int i;
00476   buddysize[0]=3; buddy[0].count=0; buddy[0].bp=0;
00477   buddysize[1]=3; buddy[1].count=0; buddy[1].bp=0;
00478   for (i=2; i<MAXBUDDY; i++) {
00479     buddy[i].count=0;
00480     buddysize[i]=buddysize[i-2]+buddysize[i-1]; /*fibonacci*/
00481     buddy[i].bp=0;}             /*no cells are connected*/
00482   buddy[MAXBUDDY].bp=(bpointer)(-1);    /*sentinel for alloc*/
00483 #if (WORD_SIZE == 64)
00484   buddysize[MAXBUDDY]= 0x7fffffffffffffff;      /*cell size limit*/
00485 #else
00486   buddysize[MAXBUDDY]= 0x7fffffff;      /*cell size limit*/
00487 #endif
00488 
00489   {
00490     unsigned int tmp;
00491     tmp = allocate_heap();
00492     fprintf(stderr, "allocate_heap: %d bytes\n", tmp*4);
00493   }
00494 
00495   gcstack=(pointer *)malloc(DEFAULT_MAX_GCSTACK * sizeof(pointer));
00496   gcsp=gcstack;
00497   gcsplimit= &gcstack[DEFAULT_MAX_GCSTACK -10];
00498 
00499 }
00500 #endif
00501 
00502 static void initclassid()
00503 { 
00504   /* built-in class id's*/
00505   nextcix=0;
00506   objectcp.cix=0; objectcp.sub=0;
00507   conscp.cix=1; conscp.sub=1;
00508   propobjcp.cix=2; propobjcp.sub=12;
00509     symbolcp.cix=3; symbolcp.sub=3;
00510     packagecp.cix=4; packagecp.sub=4;
00511     streamcp.cix=5; streamcp.sub=6;
00512       filestreamcp.cix=6; filestreamcp.sub=6;
00513     iostreamcp.cix=7; iostreamcp.sub=7;
00514     metaclasscp.cix=8; metaclasscp.sub=9;
00515       vecclasscp.cix=9; vecclasscp.sub=9;
00516     readtablecp.cix=10; readtablecp.sub=10;
00517     arraycp.cix=11; arraycp.sub=11;
00518     threadcp.cix=12; threadcp.sub=12;
00519 
00520   codecp.cix=13; codecp.sub=16;
00521     fcodecp.cix=14; fcodecp.sub=14;
00522     closurecp.cix=15; closurecp.sub=15;
00523     ldmodulecp.cix=16; ldmodulecp.sub=16;
00524 
00525   labrefcp.cix=17; labrefcp.sub=17;
00526 
00527   vectorcp.cix=18; vectorcp.sub=21;
00528   fltvectorcp.cix=19; fltvectorcp.sub=19;
00529   intvectorcp.cix=20; intvectorcp.sub=20;
00530   stringcp.cix=21; stringcp.sub=21;
00531 }
00532 
00533 static void initpackage()
00534 { register int i;
00535   register context *ctx=mainctx;
00536 
00537   /* GENESIS: First, VECTOR must exist!*/
00538   C_VECTOR=alloc(wordsizeof(struct vecclass),ELM_FIXED,vecclasscp.cix,
00539                  wordsizeof(struct vecclass));
00540   for (i=0; i<(wordsizeof(struct vecclass)); i++) C_VECTOR->c.obj.iv[i]=NULL;
00541   C_VECTOR->c.vcls.cix=makeint(vectorcp.cix);
00542   C_VECTOR->c.vcls.elmtype=makeint(ELM_POINTER);
00543 
00544   /*Then, NIL can be created*/
00545   lisppkg=makepkg(ctx,makestring("LISP",4),makeint(0),makeint(0));
00546   lisppkg->c.pkg.use=makeint(0);        /*prevent islist checking*/
00547   NIL=defconst(ctx,"NIL",NIL,lisppkg);
00548   NIL->c.sym.speval=NIL;
00549   NIL->c.sym.plist=NIL;
00550   sysobj=NIL;
00551   pkglist->c.cons.cdr=NIL;
00552   lisppkg->c.pkg.use=NIL;
00553   lisppkg->c.pkg.names->c.cons.cdr=NIL;
00554   lisppkg->c.pkg.plist=NIL;
00555   lisppkg->c.pkg.shadows=NIL;
00556   lisppkg->c.pkg.used_by=NIL;
00557   NIL->c.sym.homepkg=lisppkg;
00558 
00559   /*default packages*/
00560   keywordpkg=makepkg(ctx,makestring("KEYWORD",7),NIL,NIL);
00561   userpkg=   makepkg(ctx,makestring("USER",4),NIL,rawcons(ctx,lisppkg,NIL));
00562   syspkg=    makepkg(ctx,makestring("SYSTEM",6),NIL,rawcons(ctx,lisppkg,NIL));
00563   unixpkg=   makepkg(ctx,makestring("UNIX",4),NIL,rawcons(ctx,lisppkg,NIL));
00564   xpkg=      makepkg(ctx,makestring("X",1),NIL,rawcons(ctx,lisppkg,NIL));
00565   }
00566 
00567 static void initsymbols()
00568 { register int i;
00569   numunion nu;
00570   register context *ctx=mainctx;
00571 
00572   export_all=0;
00573 
00574   /* symbols */
00575   /* Be careful to define symbol pnames in upcase */
00576   T=defconst(ctx,"T",T,lisppkg);
00577   T->c.sym.speval=T;
00578   PACKAGE=deflocal(ctx,"*PACKAGE*",lisppkg,lisppkg);
00579   OPTIONAL=intern(ctx,"&OPTIONAL",9,lisppkg);
00580   REST=intern(ctx,"&REST",5,lisppkg);
00581   KEY=intern(ctx,"&KEY",4,lisppkg);
00582   AUX=intern(ctx,"&AUX",4,lisppkg);
00583   ALLOWOTHERKEYS=intern(ctx,"&ALLOW-OTHER-KEYS",17,lisppkg);
00584   LAMBDA=intern(ctx,"LAMBDA",6,lisppkg);
00585   MACRO=intern(ctx,"MACRO",5,lisppkg);
00586   FUNCTION=intern(ctx,"FUNCTION",8,lisppkg);
00587   LAMCLOSURE=intern(ctx,"LAMBDA-CLOSURE",14,lisppkg);
00588   COMCLOSURE=intern(ctx,"COMPILED-CLOSURE",16,lisppkg);
00589   QDECLARE=intern(ctx,"DECLARE",7,lisppkg);
00590   QSPECIAL=intern(ctx,"SPECIAL",7,lisppkg);
00591 #if SunOS4_1 /* SELF is already used on SunOS 4.1.x. */
00592   QSELF=intern(ctx,"SELF",4,lisppkg);
00593 #else
00594   SELF=intern(ctx,"SELF",4,lisppkg);
00595 #endif
00596   CLASS=intern(ctx,"CLASS",5,lisppkg);
00597   K_NOMETHOD=defkeyword(ctx,"NOMETHOD");
00598   K_IN=defkeyword(ctx,"INPUT");
00599   K_OUT=defkeyword(ctx,"OUTPUT");
00600   K_IO=defkeyword(ctx,"IO");
00601   K_FLUSH=defkeyword(ctx,"FLUSH");
00602   K_FILL=defkeyword(ctx,"FILL");
00603   K_FILE=defkeyword(ctx,"FILE"); 
00604   K_STRING=defkeyword(ctx,"STRING"); 
00605 /*  K_MSGQ=defkeyword(ctx,"MSGQ"); */
00606   K_DOWNCASE=defkeyword(ctx,"DOWNCASE");
00607   K_UPCASE=defkeyword(ctx,"UPCASE");
00608   K_PRESERVE=defkeyword(ctx,"PRESERVE");
00609   K_INVERT=defkeyword(ctx,"INVERT");
00610   K_CAPITALIZE=defkeyword(ctx,"CAPITALIZE");
00611   K_BIT=defkeyword(ctx,"BIT");
00612   K_CHAR=defkeyword(ctx,"CHAR");
00613   K_BYTE=defkeyword(ctx,"BYTE");
00614   K_SHORT=defkeyword(ctx,"SHORT");
00615   K_LONG=defkeyword(ctx,"LONG");
00616   K_INTEGER=defkeyword(ctx,"INTEGER");
00617   K_POINTER=defkeyword(ctx,"POINTER");
00618   K_FLOAT32=defkeyword(ctx,"FLOAT32");  
00619   K_FLOAT=defkeyword(ctx,"FLOAT");
00620   K_DOUBLE=defkeyword(ctx,"DOUBLE");
00621   K_FOREIGN=defkeyword(ctx,"FOREIGN");
00622   K_FOREIGN_STRING=defkeyword(ctx,"FOREIGN-STRING");
00623   K_ALLOWOTHERKEYS=defkeyword(ctx,"ALLOW-OTHER-KEYS");
00624   K_PRIN1=defkeyword(ctx,"PRIN1");
00625   K_CLASS=defkeyword(ctx,"CLASS");
00626   K_FUNCTION_DOCUMENTATION=defkeyword(ctx,"FUNCTION-DOCUMENTATION");
00627   K_CLASS_DOCUMENTATION=defkeyword(ctx,"CLASS-DOCUMENTATION");
00628   K_VARIABLE_DOCUMENTATION=defkeyword(ctx,"VARIABLE-DOCUMENTATION");
00629   K_METHOD_DOCUMENTATION=defkeyword(ctx,"METHOD-DOCUMENTATION");
00630   K_DISPOSE=defkeyword(ctx,"DISPOSE");
00631   QINTEGER=intern(ctx,"INTEGER",7,lisppkg);
00632   QFLOAT=intern(ctx,"FLOAT",5,lisppkg);
00633   QFIXNUM=intern(ctx,"FIXNUM",6,lisppkg);
00634   QNUMBER=intern(ctx,"NUMBER",6,lisppkg);
00635   QDEBUG=defvar(ctx,"*DEBUG*",NIL,lisppkg);
00636 /*  speval(QDEBUG)=NIL; */
00637   PRCASE=deflocal(ctx,"*PRINT-CASE*",K_DOWNCASE,lisppkg);
00638   PRCIRCLE=deflocal(ctx,"*PRINT-CIRCLE*",NIL,lisppkg);
00639   PROBJECT=deflocal(ctx,"*PRINT-OBJECT*",NIL,lisppkg);
00640   PRSTRUCTURE=deflocal(ctx,"*PRINT-STRUCTURE*",NIL,lisppkg);
00641   PRLENGTH=deflocal(ctx,"*PRINT-LENGTH*",NIL,lisppkg);
00642   PRLEVEL=deflocal(ctx,"*PRINT-LEVEL*",NIL,lisppkg);
00643   QREADTABLE=deflocal(ctx,"*READTABLE*",NIL,lisppkg);
00644   TOPLEVEL=defvar(ctx,"*TOPLEVEL*",NIL,lisppkg);
00645   ERRHANDLER=deflocal(ctx,"*ERROR-HANDLER*",NIL,lisppkg);
00646   QEVALHOOK=deflocal(ctx,"*EVALHOOK*",NIL,lisppkg);
00647   QUNBOUND=intern(ctx,"*UNBOUND*",9,lisppkg);
00648   RANDSTATE=deflocal(ctx,"*RANDOM-STATE*",UNBOUND,lisppkg);
00649   FEATURES=defvar(ctx,"*FEATURES*",NIL,lisppkg);
00650   READBASE=deflocal(ctx,"*READ-BASE*",makeint(10),lisppkg);
00651   PRINTBASE=deflocal(ctx,"*PRINT-BASE*",makeint(10),lisppkg);
00652   /*initialize i/o stream*/
00653   STDIN=mkfilestream(ctx,K_IN,makebuffer(128),0,NIL);
00654   QSTDIN=deflocal(ctx,"*STANDARD-INPUT*",STDIN,lisppkg);
00655   STDOUT=mkfilestream(ctx,K_OUT,makebuffer(256),1,NIL);
00656   QSTDOUT=deflocal(ctx,"*STANDARD-OUTPUT*",STDOUT,lisppkg);
00657   ERROUT=mkfilestream(ctx,K_OUT,makebuffer(128),2,NIL);
00658   QERROUT=deflocal(ctx,"*ERROR-OUTPUT*",ERROUT,lisppkg);
00659   QTERMIO=deflocal(ctx,"*TERMINAL-IO*",NIL,lisppkg);
00660   GCMERGE=defvar(ctx,"*GC-MERGE*",makeflt(0.2),syspkg);
00661   GCMARGIN=defvar(ctx,"*GC-MARGIN*",makeflt(0.4),syspkg);
00662   QLDENT=defvar(ctx,"*LOAD-ENTRIES*", NIL, syspkg);
00663   QTHREADS=defvar(ctx, "*THREADS*", NIL, syspkg);
00664   QPARAGC=defvar(ctx, "*PARALLEL-GC*", NIL, syspkg);
00665   QGCHOOK=defvar(ctx,"*GC-HOOK*",NIL,syspkg);
00666   QEXITHOOK=defvar(ctx,"*EXIT-HOOK*",NIL,syspkg);
00667   FATALERROR=defvar(ctx,"*EXIT-ON-FATAL-ERROR*",NIL,lisppkg);
00668 
00669   /*init character macro table*/
00670   for (i=0; i<256; i++) charmacro[i]=sharpmacro[i]=NIL;
00671 
00672   /*init signal vector*/
00673   for (i=0; i<NSIG; i++) eussigvec[i]=NIL;
00674 }
00675 
00676 static void initclasses()
00677 { extern pointer oblabels[MAXTHREAD];   /*eusio.c*/
00678   register context *ctx=mainctx;
00679   int i;
00680 
00681   /* basic classes */
00682 /*0*/
00683   OBJECT=basicclass("OBJECT",NIL,&objectcp,0);
00684   C_OBJECT=speval(OBJECT);
00685 /*1*/
00686   QCONS=basicclass("CONS",C_OBJECT,&conscp,2,"CAR","CDR");
00687   C_CONS=speval(QCONS);
00688 /*2*/
00689   PROPOBJ=basicclass("PROPERTIED-OBJECT",C_OBJECT, &propobjcp,1,"PLIST");
00690   C_PROPOBJ=speval(PROPOBJ);
00691 /*3*/
00692   SYMBOL=basicclass("SYMBOL",C_PROPOBJ,&symbolcp,5,
00693                     "VALUE","VTYPE","FUNCTION","PNAME","HOMEPKG");
00694   C_SYMBOL=speval(SYMBOL);
00695 /*4*/
00696   PKGCLASS=basicclass("PACKAGE",C_PROPOBJ,&packagecp,
00697                       8,"NAMES","USE","SYMVECTOR","SYMCOUNT",
00698                         "INTSYMVECTOR", "INTSYMCOUNT", "SHADOWS", "USED-BY");
00699   C_PACKAGE=speval(PKGCLASS);
00700 /*5*/
00701   STREAM=basicclass("STREAM",C_PROPOBJ,&streamcp,
00702                     4,"DIRECTION","BUFFER","COUNT","TAIL");
00703   C_STREAM=speval(STREAM);
00704 /*6*/
00705   FILESTREAM=basicclass("FILE-STREAM",C_STREAM,&filestreamcp,2,"FD","FNAME");
00706   C_FILESTREAM=speval(FILESTREAM);
00707 /*7*/
00708   IOSTREAM=basicclass("IO-STREAM",C_PROPOBJ,&iostreamcp,2,"INSTREAM","OUTSTREAM");
00709   C_IOSTREAM=speval(IOSTREAM);
00710 /*8*/
00711   METACLASS=basicclass("METACLASS",C_PROPOBJ,&metaclasscp,
00712                         7,"NAME","SUPER","CIX","VARS","TYPES","FORWARDS","METHODS");
00713   C_METACLASS=speval(METACLASS);
00714 /*9*/
00715   VECCLASS=basicclass("VECTORCLASS",C_METACLASS,&vecclasscp,
00716                       2,"ELEMENT-TYPE","SIZE");
00717   C_VCLASS=speval(VECCLASS);
00718 /*10*/
00719   READTABLE=basicclass("READTABLE",C_PROPOBJ,&readtablecp,
00720                        4,"SYNTAX","MACRO","DISPATCH-MACRO","CASE");
00721   C_READTABLE=speval(READTABLE);
00722 /*11*/
00723   ARRAY=basicclass("ARRAY",C_PROPOBJ,&arraycp,
00724                      11,"ENTITY","RANK","FILL-POINTER","DISPLACED-INDEX-OFFSET",
00725                         "DIM0","DIM1","DIM2","DIM3","DIM4","DIM5","DIM6");
00726   C_ARRAY=speval(ARRAY);
00727 /*12 */
00728   THREAD=basicclass("THREAD", C_PROPOBJ, &threadcp,
00729                         10, "ID", "REQUESTER", "REQUEST-SEM", "DONE-SEM",
00730                            "FUNC", "ARGS", "RESULT", "CONTEXT",
00731                            "IDLE", "WAIT");
00732   C_THREAD=speval(THREAD);
00733 /*13*/
00734   CODE=basicclass("COMPILED-CODE",C_OBJECT,&codecp,4,"CODEVECTOR","QUOTEVECTOR",
00735                   "TYPE","ENTRY");
00736   C_CODE=speval(CODE);
00737 /*14*/
00738   FCODE=basicclass("FOREIGN-CODE",C_CODE,&fcodecp,3,"ENTRY2","PARAMTYPES","RESULTTYPE"); /* kanehiro's patch 2000.12.13 */
00739   C_FCODE=speval(FCODE);
00740 /*15*/
00741 #if (WORD_SIZE == 64)
00742   CLOSURE=basicclass("CLOSURE",C_CODE,&closurecp,3,"ENV0","ENV1","ENV2");
00743 #else
00744   CLOSURE=basicclass("CLOSURE",C_CODE,&closurecp,2,"ENV1","ENV2");
00745 #endif
00746   C_CLOSURE=speval(CLOSURE);
00747 /* 16    ---new for Solaris */
00748   LDMODULE=basicclass("LOAD-MODULE",C_CODE, &ldmodulecp, 3,
00749                         "SYMBOL-TABLE","OBJECT-FILE", "HANDLE");
00750   C_LDMOD=speval(LDMODULE);
00751 /*17*/
00752   LABREF=basicclass("LABEL-REFERENCE",C_OBJECT,&labrefcp,4,
00753                     "LABEL","VALUE","UNSOLVED","NEXT");
00754   C_LABREF=speval(LABREF);
00755 /*18*/
00756   VECTOR=defvector(ctx,"VECTOR",C_OBJECT,ELM_POINTER, 0);   /* alpha */
00757   C_VECTOR=speval(VECTOR);
00758   builtinclass[nextbclass].cls=C_VECTOR;
00759   builtinclass[nextbclass++].cp= &vectorcp;
00760 
00761   FLTVECTOR=defvector(ctx,"FLOAT-VECTOR",C_VECTOR,ELM_FLOAT, 0); /* alpha */
00762   C_FLTVECTOR=speval(FLTVECTOR);
00763   builtinclass[nextbclass].cls=C_FLTVECTOR;
00764   builtinclass[nextbclass++].cp= &fltvectorcp;
00765 
00766   INTVECTOR=defvector(ctx,"INTEGER-VECTOR",C_VECTOR,ELM_INT, 0); /* alpha */
00767   C_INTVECTOR=speval(INTVECTOR);
00768   builtinclass[nextbclass].cls=C_INTVECTOR;
00769   builtinclass[nextbclass++].cp= &intvectorcp;
00770 
00771   STRING=defvector(ctx,"STRING",C_VECTOR,ELM_CHAR, 0); /* alpha */
00772   C_STRING=speval(STRING);
00773   builtinclass[nextbclass].cls=C_STRING;
00774   builtinclass[nextbclass++].cp= &stringcp;
00775 
00776   BITVECTOR=defvector(ctx,"BIT-VECTOR",C_VECTOR,ELM_BIT, 0); /* alpha */
00777   C_BITVECTOR=speval(BITVECTOR);
00778   builtinclass[nextbclass].cls=C_BITVECTOR;
00779   builtinclass[nextbclass++].cp= &bitvectorcp;
00780 
00781 /* extended numbers */
00782   EXTNUM=basicclass("EXTENDED-NUMBER",C_OBJECT,&extnumcp,0);
00783   C_EXTNUM=speval(EXTNUM);
00784   RATIO=basicclass("RATIO",C_EXTNUM, &ratiocp,2,"NUMERATOR","DENOMINATOR");
00785   C_RATIO=speval(RATIO);
00786   COMPLEX=basicclass("COMPLEX", C_EXTNUM, &complexcp, 2, "REAL", "IMAGINARY");
00787   C_COMPLEX=speval(COMPLEX);
00788   BIGNUM=basicclass("BIGNUM", C_EXTNUM, &bignumcp, 2, "SIZE", "BV");
00789   C_BIGNUM=speval(BIGNUM);
00790 
00791   for (i=0;i<MAXTHREAD;i++) {
00792     oblabels[i]=(pointer)makelabref(makeint(-1),UNBOUND,NIL);
00793     sysobj=cons(ctx,oblabels[i],sysobj);
00794   }
00795 }
00796 
00797 static void initfeatures()
00798 { register pointer p;
00799   register context *ctx=mainctx;
00800   extern char *makedate;
00801   extern char *gitrevision;
00802   extern char *compilehost;
00803 
00804   p=makestring(VERSION,strlen(VERSION));
00805   vpush(p);
00806   p=makestring(compilehost,strlen(compilehost));
00807   vpush(p);
00808   p=makestring(makedate,strlen(makedate));
00809   vpush(p);
00810   p=makestring(gitrevision,strlen(gitrevision));
00811   vpush(p);
00812   p=stacknlist(ctx,4);
00813   QVERSION=defvar(ctx, "LISP-IMPLEMENTATION-VERSION", p,lisppkg);
00814 
00815   /*make features*/
00816 
00817   p=NIL;
00818 #if vax
00819   p=cons(ctx,intern(ctx,"VAX",3,keywordpkg),p);
00820 #endif
00821 #if sun
00822   p=cons(ctx,intern(ctx,"SUN",3,keywordpkg),p);
00823 #endif
00824 #if apollo
00825   p=cons(ctx,intern(ctx,"APOLLO",6,keywordpkg),p);
00826 #endif
00827 #if mips
00828   p=cons(ctx,intern(ctx,"MIPS",4,keywordpkg),p);
00829 #endif
00830 #if sun3
00831   p=cons(ctx,intern(ctx,"SUN3",4,keywordpkg),p);
00832 #endif
00833 #if sun4
00834   p=cons(ctx,intern(ctx,"SUN4",4,keywordpkg),p);
00835 #endif
00836 #if news
00837   p=cons(ctx,intern(ctx,"NEWS",4,keywordpkg),p);
00838 #endif
00839 #if sanyo
00840   p=cons(ctx,intern(ctx,"SANYO",5,keywordpkg),p);
00841 #endif
00842 #if bsd4_2
00843   p=cons(ctx,intern(ctx,"BSD4_2",6,keywordpkg),p);
00844 #endif
00845 #if SunOS4
00846   p=cons(ctx,intern(ctx,"SUNOS4",6,keywordpkg),p);
00847 #endif
00848 #if SunOS4_1
00849   p=cons(ctx,intern(ctx,"SUNOS4.1",8,keywordpkg),p);
00850 #endif
00851 #if system5
00852   p=cons(ctx,intern(ctx,"SYSTEM5",7,keywordpkg),p);
00853 #endif
00854 #if coff
00855   p=cons(ctx,intern(ctx,"COFF",4,keywordpkg),p);
00856 #endif
00857 #if Solaris2
00858   p=cons(ctx,intern(ctx,"SOLARIS2",8,keywordpkg),p);
00859 #endif
00860 #if GCC
00861   p=cons(ctx,intern(ctx,"GCC",3,keywordpkg),p);
00862 #endif
00863 #if GCC3
00864   p=cons(ctx,intern(ctx,"GCC3",4,keywordpkg),p);
00865 #endif
00866 #if i386
00867   p=cons(ctx,intern(ctx,"I386",4,keywordpkg),p);
00868 #endif
00869 #if Linux
00870   p=cons(ctx,intern(ctx,"LINUX",5,keywordpkg),p);
00871 #endif
00872 #if Linux_ppc
00873   p=cons(ctx,intern(ctx,"PPC",3,keywordpkg),p);
00874 #endif
00875 #if USE_MULTI_LIB
00876   p=cons(ctx,intern(ctx,"IA32",4,keywordpkg),p);
00877 #endif
00878 #if ELF
00879   p=cons(ctx,intern(ctx,"ELF",3,keywordpkg),p);
00880 #endif
00881 #if IRIX
00882   p=cons(ctx,intern(ctx,"IRIX",4,keywordpkg),p);
00883 #endif
00884 #if IRIX6
00885   p=cons(ctx,intern(ctx,"IRIX6",5,keywordpkg),p);
00886 #endif
00887 #if alpha
00888   p=cons(ctx,intern(ctx,"ALPHA",5,keywordpkg),p);
00889 #endif
00890 #if Cygwin
00891   p=cons(ctx,intern(ctx,"CYGWIN",6,keywordpkg),p);
00892 #endif
00893 #if Darwin
00894   p=cons(ctx,intern(ctx,"DARWIN",6,keywordpkg),p);
00895 #endif
00896 #if THREADED
00897   p=cons(ctx,intern(ctx,"THREAD",6,keywordpkg),p);
00898 #endif
00899 #if PTHREAD
00900   p=cons(ctx,intern(ctx,"PTHREAD",7,keywordpkg),p);
00901 #endif
00902 #if X_V11R6_1
00903   p=cons(ctx,intern(ctx,"X11R6.1",7,keywordpkg),p);
00904 #endif
00905 #if RGC
00906   p=cons(ctx,intern(ctx,"RGC",3,keywordpkg),p);
00907 #endif
00908 #if SH4
00909   p=cons(ctx,intern(ctx,"SH4",3,keywordpkg),p);
00910 #endif
00911 #if x86_64
00912   p=cons(ctx,intern(ctx,"X86_64",6,keywordpkg),p);
00913 #endif
00914 #if ARM
00915   p=cons(ctx,intern(ctx,"ARM",3,keywordpkg),p);
00916 #endif
00917 
00918   defvar(ctx,"*FEATURES*",p,lisppkg);
00919 
00920   /*system function module*/
00921   sysmod=makemodule(ctx,0);
00922   sysmod->c.ldmod.codevec=makeint(0);
00923   sysmod->c.ldmod.handle=makeint((eusinteger_t)dlopen(0, RTLD_LAZY)>>2);
00924   sysobj=cons(ctx,sysmod, sysobj);
00925   }
00926 
00927 /****************************************************************/
00928 /* signal handlers
00929 /****************************************************************/
00930 
00931 extern long gcing;
00932 #include <sys/wait.h>
00933 
00934 void eusint(s,code,x,addr)
00935 register int s;
00936 int code,x;
00937 eusinteger_t addr;
00938 { int stat;
00939   context *ctx;
00940 
00941   ctx = euscontexts[thr_self()];
00942   if (debug) {
00943     fprintf(stderr, ";; eusint: sig=%d, %d; thr=%d ctx=%p\n",
00944                 s,code,thr_self(), ctx);}
00945   if (ctx==NULL) ctx=mainctx;
00946   ctx->intsig=s;
00947   intcode=code;
00948   switch(s) {
00949   case SIGCHLD:
00950     wait(&stat);
00951     if (debug)
00952       fprintf(stderr,";; child proc terminated; wait=0x%x\n",stat);
00953     ctx->intsig=0;
00954     break;
00955   case SIGFPE:
00956     fprintf(stderr,";; floating exception\n");
00957     goto sigfatal;
00958     break;
00959   case SIGPIPE:
00960     fprintf(stderr,";; pipe broken %d %x %lx\n",code,x,(unsigned long)addr);
00961     throw(mainctx,makeint(0),NIL);      /*nonsense*/
00962     break;
00963   case SIGSEGV:
00964     fprintf(stderr,";; Segmentation Fault.\n");
00965     goto sigfatal;
00966   case SIGBUS:
00967     fprintf(stderr,";; Bus Error.\n");
00968 
00969     sigfatal:
00970 
00971     if (speval(FATALERROR) != NIL) exit(s);
00972     if (ctx->callfp) {
00973       fprintf(stderr,";; in ");
00974       prinx(ctx,ctx->callfp->form,ERROUT);
00975       flushstream(ERROUT);
00976       fprintf(stderr,"\n");}
00977     fprintf(stderr,";; You are still in a signal handler.\n;;Try reset or throw to upper level as soon as possible.\n");
00978     fprintf(stderr,";; code=%d x=%x addr=%lx\n",code,x,(unsigned long)addr);
00979     reploop(mainctx, "Fatal: ");
00980     fprintf(stderr,";; No, you cannot continue the previous evaluation.\n");
00981     /* goto sigfatal; */
00982     exit(s);
00983     break;
00984   }
00985 
00986 #if Solaris2 || system5
00987   signal(s,(void (*)())eusint); /*reinstall signal*/
00988 #endif
00989 
00990 #if THREADED
00991     if (isintvector(eussigvec[s])) {
00992 /*      if (s==SIGALRM && gcing) {intsig=0; return;} */
00993         /*ignore Alarm clock during gc*/
00994       sema_post((sema_t *)eussigvec[s]->c.ivec.iv);
00995       ctx->intsig=0;}
00996 #endif
00997   if (debug) { fprintf(stderr, ";; eusint exit: intsig=%d\n",ctx->intsig);}
00998 }
00999 
01000 static pointer brkloop(ctx, prompt)
01001 context *ctx;
01002 char *prompt;
01003 { jmp_buf brkjmp;
01004   pointer val;
01005   int i;
01006   mkcatchframe(ctx,T,brkjmp);
01007   Spevalof(QSTDOUT)=STDOUT;
01008   Spevalof(QSTDIN)=STDIN;
01009   Spevalof(QERROUT)=ERROUT;
01010   if ((val=(pointer)eussetjmp(brkjmp))==0) val=reploop(ctx,prompt);
01011   else if ((eusinteger_t)val==1) val=makeint(0);        /*longjmp cannot return 0*/
01012   ctx->callfp=ctx->catchfp->cf;
01013   ctx->bindfp=ctx->catchfp->bf;
01014   ctx->vsp=(pointer *)ctx->catchfp;
01015   ctx->catchfp=(struct catchframe *)*(ctx->vsp);
01016   return(val);}
01017 
01018 void sigbreak()
01019 { pointer sighandler,*vspsave;
01020   context *ctx=euscontexts[thr_self()];
01021   int is;
01022 
01023   is=ctx->intsig;
01024   if (debug) printf("sigbreak: intsig=%d thr_self=%d\n", is, thr_self());
01025   sighandler=eussigvec[is];
01026   if (isintvector(sighandler)) return;
01027   vspsave=ctx->vsp;
01028   ctx->intsig=0;
01029   QEVALHOOK->c.sym.speval=NIL;
01030   if (sighandler!=NIL) {
01031     vpush(makeint(is)); vpush(makeint(intcode));
01032     ufuncall(ctx,sighandler,sighandler,(pointer)(ctx->vsp-2),ctx->bindfp,2);
01033     ctx->vsp=vspsave;  }
01034   else {
01035     fprintf(stderr,"signal=%d to thread %d, \n",is, thr_self());
01036     /*    brkloop(ctx,"B: "); */
01037     return; }}
01038   
01039 
01040 /****************************************************************/
01041 /* main and toplevel
01042 /****************************************************************/
01043 static pointer reploop(ctx,prompt)
01044 register context *ctx;
01045 char *prompt;
01046 { pointer p,q;
01047   int ttyp;
01048   ehbypass=0;
01049   ttyp=isatty(intval(STDIN->c.fstream.fd));
01050   do {
01051     if (ttyp) {
01052       p=Spevalof(PACKAGE);
01053       if (p!=userpkg) { /*print pkg name*/
01054         printf("%s:",ccar(p->c.pkg.names)->c.str.chars);}
01055       printf("%s",prompt);
01056       fflush(stdout);}
01057     p=reader(ctx,STDIN,NIL);
01058     if (p==(pointer)EOF) return(NIL);
01059     breakck;
01060     q=eval(ctx,p);
01061     if (q!=UNBOUND) { prinx(ctx,q,STDOUT); terpri(STDOUT);}}
01062   while (1);}
01063 
01064 static void toplevel(ctx,argc,argv)
01065 register context *ctx;
01066 int argc;
01067 char *argv[];
01068 { pointer argvp,topform;
01069   int i,j;
01070 
01071   /* reset stack pointer and frame pointers*/
01072   j=(int)eussetjmp(topjbuf);  
01073   ctx->vsp=ctx->stack;
01074   ctx->bindfp=NULL;
01075   ctx->sbindfp=NULL;
01076   ctx->callfp=NULL;
01077   ctx->blkfp=NULL;
01078   ctx->protfp=NULL;
01079   ctx->catchfp=NULL;
01080   ctx->fletfp=NULL;
01081   topform=speval(TOPLEVEL);
01082   if (topform!=NIL) {
01083     if (j==0) {
01084       for (i=0; i<argc; i++) vpush(makestring(argv[i],strlen(argv[i])));
01085       ufuncall(ctx,topform,topform,(pointer)(ctx->vsp-argc),0,argc);}
01086     else ufuncall(ctx,topform,topform,(pointer)(ctx->vsp),0,0);}
01087   else { /*TOPLEVEL not yet defined-- try built-in toplevel*/
01088     mkcatchframe(ctx,makeint(0),topjbuf);
01089     fprintf(stderr, "entering reploop\n");
01090     reploop(ctx,": ");}
01091   }
01092 
01093 /* load initial configuration file from EUSDIR/eusrt0.l */
01094 static void configure_eus(ctx)
01095 register context *ctx;
01096 { pointer argv, p, in;
01097   int i,j;
01098   char *eusdir, *eusrt;
01099   char fname[1024];
01100   extern pointer SRCLOAD();
01101 
01102   /* reset stack pointer and frame pointers*/
01103   j=(int)eussetjmp(topjbuf);  
01104   eusdir=(char *)getenv("EUSDIR");
01105   if (eusdir==NULL) { 
01106     fprintf(stderr, "EUSDIR is not setenved, assuming /usr/local/eus\n");
01107     eusdir= "/usr/local/eus/"; }
01108   sprintf(fname,"%s/lib/eus.init.l", eusdir);
01109 
01110 #if !Solaris2 || GCC
01111   /* read the init-module list from "EUSDIR/lib/eus.init.l" */
01112   in=(pointer)openfile(ctx,fname,O_RDONLY,0,256);
01113   if (in==NULL) {
01114     fprintf(stderr,
01115          "$EUSDIR/lib/eus.init.l was not found\nfailed to initialize.\n"
01116          "Entering raweus.\n"
01117          "To exit, type (unix::exit)\n");
01118     return;}
01119   vpush(in);
01120   p=reader(ctx,in,NIL);
01121   vpop();
01122   closestream(in);
01123   /* prinx(ctx,p,STDOUT);  */
01124 #endif
01125 
01126   vpush(p);
01127   /*  prinx(ctx,p,STDOUT); terpri(STDOUT); */
01128   p=(pointer)list_module_initializers(ctx,p);
01129   vpop();
01130   speval(QLDENT)=p;
01131 
01132   eusrt=(char *)getenv("EUSRT");
01133   if (eusrt==NULL)  sprintf(fname,"%s/lib/eusrt.l", eusdir);
01134   else strcpy(fname, eusrt);
01135   if (isatty(0)!=0) {
01136     fprintf(stderr, "configuring by \"%s\"\n", fname); }
01137   mkcatchframe(ctx,makeint(0), topjbuf);
01138   argv=makestring(fname, strlen(fname));
01139   vpush(argv);
01140   eusstart(ctx);
01141   SRCLOAD(ctx, 1, ctx->vsp-1);
01142   }
01143 
01144 
01145 int mainargc;
01146 char *mainargv[32];
01147 
01148 
01149 void mainthread(ctx)
01150 register context *ctx;
01151 { 
01152   euscontexts[thr_self()]=ctx;
01153 
01154   /*initialize system*/
01155 #ifndef RGC
01156   initmemory();
01157 #endif
01158   initclassid();
01159 
01160   {
01161     int i;
01162     pointer specialtab;
01163     specialtab=alloc(MAX_SPECIALS+1,ELM_POINTER,vectorcp.cix,MAX_SPECIALS+1);
01164     specialtab->c.vec.size=makeint(MAX_SPECIALS);
01165     for (i=0; i<MAX_SPECIALS; i++)     specialtab->c.vec.v[i]=NIL;
01166     ctx->specials=specialtab;    }
01167 
01168   initpackage();
01169   initsymbols();
01170   initclasses();
01171   initfeatures();
01172 
01173   ctx->threadobj=NIL;
01174 
01175 
01176   /* define built-in functions */
01177   lists(ctx,sysmod);    /*list functions and predicates*/
01178   predicates(ctx,sysmod); /*predicates*/
01179   sequence(ctx,sysmod); /*sequence functions*/
01180   specials(ctx,sysmod); /*control forms, special forms*/
01181   lispio(ctx,sysmod);   /*lisp i/o*/
01182   loadsave(ctx,sysmod); /*loader and saver*/
01183   leo(ctx,sysmod);      /*object oriented programming*/
01184   arith(ctx,sysmod);    /*arithmetic functions*/
01185   matrix(ctx,sysmod);   /*floatvector, matrix operation*/
01186   unixcall(ctx,sysmod); /*unix system binding*/
01187   foreign(ctx,sysmod);  /*foreign function interface*/
01188   vectorarray(ctx,sysmod);      /*vector and array functions*/
01189   charstring(ctx,sysmod);
01190 #if THREADED
01191   mthread(ctx,sysmod);
01192 #if Solaris2
01193   mainport=makethreadport(mainctx);
01194   speval(QTHREADS)=cons(ctx, mainport, speval(QTHREADS));
01195   mainport->c.thrp.id=makeint((int)maintid);
01196 #endif
01197 #endif
01198 
01199   initreader(ctx);
01200   sysfunc(ctx,sysmod);
01201 #ifdef RGC
01202   rgcfunc(ctx,sysmod);
01203 #endif
01204   eusioctl(ctx,sysmod);
01205   Spevalof(PACKAGE)=userpkg; 
01206 
01207   defvar(ctx,"*PROGRAM-NAME*",makestring(progname,strlen(progname)),lisppkg);
01208 
01209   /* exec_module_initializers(); */
01210   ctx->vsp=ctx->stack;
01211   configure_eus(ctx); 
01212 
01213   signal(SIGCHLD, (void (*)())eusint);
01214   signal(SIGFPE,  (void (*)())eusint);
01215   signal(SIGPIPE, (void (*)())eusint);
01216 #ifdef RGC
01217 //  signal(SIGSEGV, (void (*)())eusint); /* for debugging. R.Hanai */
01218 #else
01219   signal(SIGSEGV, (void (*)())eusint);
01220 #endif
01221   signal(SIGBUS,  (void (*)())eusint);
01222 
01223   toplevel(ctx,mainargc,mainargv);
01224 
01225   { pointer exithook=speval(QEXITHOOK);
01226     if (exithook != NIL) {
01227       ufuncall(ctx,exithook,exithook,(pointer)(ctx->vsp),0,0);}
01228     }
01229 
01230 #if THREADED
01231 #if SunOS4_1
01232   thr_exit(0);
01233 #else
01234   exit(0);
01235 #endif
01236 #endif
01237   }
01238 
01239 main(argc,argv)
01240 int argc;
01241 char *argv[];
01242 { int i, stat=0;
01243   unsigned char *m;
01244 
01245 #ifdef Darwin
01246   _end = sbrk(0);
01247 #endif
01248 
01249   mypid=getpid();
01250   mainargc=argc;
01251   for (i=0; i<argc; i++) mainargv[i]=argv[i];
01252 
01253   tzset();
01254 
01255 #if Linux && !OLD_LINUX && !Darwin
01256   mallopt(M_MMAP_MAX,0);
01257 #endif
01258 
01259   /* following two lines are just to speed up frequent sbreak at the beginning
01260      of the execution. These lines may be deleted without any harm.*/
01261   m=(unsigned char *)malloc(4*1024*1024);
01262   cfree(m);
01263 
01264 #if vxworks
01265   progname=taskName(mypid);
01266 #else
01267   progname=argv[0];
01268 #endif
01269   /* get stack area and initialize stack/frame pointers */
01270   for (i=0; i<MAXTHREAD; i++) euscontexts[i]=0;
01271   mainctx=(context *)makelispcontext(MAXSTACK);
01272 #if THREADED
01273 #if Solaris2
01274   mutex_init(&mark_lock, USYNC_THREAD, 0);
01275   mutex_init(&p_mark_lock, USYNC_THREAD, 0);
01276   thr_create(0, 2*1024*1024,mainthread, mainctx, 0, &maintid);
01277   { sigset_t mainsig, omainsig;
01278     sigemptyset(&mainsig);
01279     sigaddset(&mainsig, SIGINT);
01280     /* printf("mainthread=%d\n", thr_self()); */         
01281     thr_sigsetmask(SIG_BLOCK, &mainsig, &omainsig);
01282     }
01283   thr_join(maintid, 0, (void *)&stat);
01284 #else
01285 #if SunOS4_1 || alpha || PTHREAD
01286   mthread_init( mainctx );
01287 #ifdef RGC
01288   init_rgc();
01289 #endif
01290 #endif
01291   mainthread(mainctx);
01292 #endif  /* Solaris2 */
01293 #else
01294   mainthread(mainctx);
01295 #endif
01296   
01297   { pointer exithook=speval(QEXITHOOK);
01298     if (exithook != NIL) {
01299       ufuncall(mainctx,exithook,exithook,(pointer)(mainctx->vsp),0,0);}
01300     }
01301 
01302   exit(stat);
01303   }
01304 
01305 #if (WORD_SIZE == 64)
01306 pointer makeint(eusinteger_t v) {
01307   if (v>(eusinteger_t)MAXPOSFIXNUM || v<(eusinteger_t)MINNEGFIXNUM) {
01308     if (v&0x7L) {
01309       return(mkbigint(v));
01310     }
01311     return ((pointer)(v|0x3L)); }
01312   else return((pointer)((v<<2)+2));
01313 }
01314 eusinteger_t intval(pointer p) {
01315   eusinteger_t i=(eusinteger_t)p;
01316   if (p==NULL) {
01317     fprintf(stderr,"p=null\n");
01318     raise(SIGSEGV);
01319     return 0;}
01320   else if ((i&0x3L)==0x3L) {
01321     return (i&~0x3L); }
01322   else if (isbignum(p)) {
01323     return (bigintval(p)); }
01324   else if ((i&0x7)==0x0L) {
01325     fprintf(stderr,";p=pointer?(%p)\n", p);
01326     return (i); }
01327   else return (((eusinteger_t)i)>>2);
01328 }
01329 #else
01330 pointer makeint(eusinteger_t v) {
01331   if (v>(int)MAXPOSFIXNUM || v<(int)MINNEGFIXNUM) {
01332     //    fprintf(stderr, "makeint(%x)\n", v);
01333     if (v&0x3) {
01334       //      fprintf(stderr, "v=%x(bignum)\n", v);
01335       return(mkbigint(v));
01336     }
01337     return ((pointer)(v|0x3)); }
01338   else return((pointer)((v<<2)+2));
01339 }
01340 eusinteger_t intval(pointer p) {
01341   eusinteger_t i=(eusinteger_t)p;
01342   if (p==NULL) {
01343     fprintf(stderr,"p=null\n");
01344     return 0;}
01345   else if ((i&0x3)==0x3) {
01346     return (i&~0x3); }
01347   else if (isbignum(p)) {
01348     return (bigintval(p)); }
01349   else if ((i&0x3)==0x0) {
01350     fprintf(stderr,";p=pointer?(%p)\n", p);
01351     return (i); }
01352   else return (((eusinteger_t)i)>>2);
01353 }
01354 #endif
01355 
01356 eusinteger_t hide_ptr (pointer p) { return (eusinteger_t)p; }


euslisp
Author(s): Toshihiro Matsui
autogenerated on Thu Sep 3 2015 10:36:19