--- ray/src/common/caldefn.c 1997/01/08 17:10:24 2.10 +++ ray/src/common/caldefn.c 2003/10/27 10:19:31 2.21 @@ -1,9 +1,6 @@ -/* Copyright (c) 1992 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: caldefn.c,v 2.21 2003/10/27 10:19:31 schorsch Exp $"; #endif - /* * Store variable definitions. * @@ -22,12 +19,20 @@ static char SCCSid[] = "$SunId$ LBL"; * 4/23/91 Added ':' assignment for constant expressions * * 8/7/91 Added optional context path to append to variable names + * + * 5/17/2001 Fixed clock counter wrapping behavior + * + * 2/19/03 Eliminated conditional compiles in favor of esupport extern. */ -#include +#include "copyright.h" +#include +#include #include +#include "rterror.h" +#include "rtmisc.h" #include "calcomp.h" #ifndef NHASH @@ -38,34 +43,32 @@ static char SCCSid[] = "$SunId$ LBL"; #define newnode() (EPNODE *)ecalloc(1, sizeof(EPNODE)) -extern char *ecalloc(), *emalloc(), *savestr(), *strcpy(); +static double dvalue(char *name, EPNODE *d); -static double dvalue(); +#define MAXCLOCK (1L<<31) /* clock wrap value */ unsigned long eclock = 0; /* value storage timer */ -static char context[MAXWORD+1]; /* current context path */ +#define MAXCNTX 1023 /* maximum context length */ +static char context[MAXCNTX+1]; /* current context path */ + static VARDEF *hashtbl[NHASH]; /* definition list */ static int htndx; /* index for */ static VARDEF *htpos; /* ...dfirst() and */ -#ifdef OUTCHAN static EPNODE *ochpos; /* ...dnext */ static EPNODE *outchan; -#endif -#ifdef FUNCTION EPNODE *curfunc = NULL; #define dname(ep) ((ep)->v.kid->type == SYM ? \ (ep)->v.kid->v.name : \ (ep)->v.kid->v.kid->v.name) -#else -#define dname(ep) ((ep)->v.kid->v.name) -#endif -fcompile(fname) /* get definitions from a file */ -char *fname; +void +fcompile( /* get definitions from a file */ + char *fname +) { FILE *fp; @@ -84,10 +87,12 @@ char *fname; } -scompile(str, fn, ln) /* get definitions from a string */ -char *str; -char *fn; -int ln; +void +scompile( /* get definitions from a string */ + char *str, + char *fn, + int ln +) { initstr(str, fn, ln); while (nextc != EOF) @@ -96,16 +101,18 @@ int ln; double -varvalue(vname) /* return a variable's value */ -char *vname; +varvalue( /* return a variable's value */ + char *vname +) { return(dvalue(vname, dlookup(vname))); } double -evariable(ep) /* evaluate a variable */ -EPNODE *ep; +evariable( /* evaluate a variable */ + EPNODE *ep +) { register VARDEF *dp = ep->v.ln; @@ -113,10 +120,12 @@ EPNODE *ep; } -varset(vname, assign, val) /* set a variable's value */ -char *vname; -int assign; -double val; +void +varset( /* set a variable's value */ + char *vname, + int assign, + double val +) { char *qname; register EPNODE *ep1, *ep2; @@ -147,8 +156,10 @@ double val; } -dclear(name) /* delete variable definitions of name */ -char *name; +void +dclear( /* delete variable definitions of name */ + char *name +) { register EPNODE *ep; @@ -162,8 +173,10 @@ char *name; } -dremove(name) /* delete all definitions of name */ -char *name; +void +dremove( /* delete all definitions of name */ + char *name +) { register EPNODE *ep; @@ -172,8 +185,10 @@ char *name; } -vardefined(name) /* return non-zero if variable defined */ -char *name; +int +vardefined( /* return non-zero if variable defined */ + char *name +) { register EPNODE *dp; @@ -182,8 +197,9 @@ char *name; char * -setcontext(ctx) /* set a new context path */ -register char *ctx; +setcontext( /* set a new context path */ + register char *ctx +) { register char *cpp; @@ -198,7 +214,7 @@ register char *ctx; cpp = context; /* start context with mark */ *cpp++ = CNTXMARK; do { /* carefully copy new context */ - if (cpp >= context+MAXWORD) + if (cpp >= context+MAXCNTX) break; /* just copy what we can */ if (isid(*ctx)) *cpp++ = *ctx++; @@ -214,19 +230,19 @@ register char *ctx; char * -pushcontext(ctx) /* push on another context */ -char *ctx; +pushcontext( /* push on another context */ + char *ctx +) { - extern char *strncpy(), *strcpy(); - char oldcontext[MAXWORD+1]; + char oldcontext[MAXCNTX+1]; register int n; strcpy(oldcontext, context); /* save old context */ setcontext(ctx); /* set new context */ n = strlen(context); /* tack on old */ - if (n+strlen(oldcontext) > MAXWORD) { - strncpy(context+n, oldcontext, MAXWORD-n); - context[MAXWORD] = '\0'; + if (n+strlen(oldcontext) > MAXCNTX) { + strncpy(context+n, oldcontext, MAXCNTX-n); + context[MAXCNTX] = '\0'; } else strcpy(context+n, oldcontext); return(context); @@ -234,7 +250,7 @@ char *ctx; char * -popcontext() /* pop off top context */ +popcontext(void) /* pop off top context */ { register char *cp1, *cp2; @@ -244,18 +260,19 @@ popcontext() /* pop off top context */ while (*++cp2 && *cp2 != CNTXMARK) ; cp1 = context; /* copy tail to front */ - while (*cp1++ = *cp2++) + while ( (*cp1++ = *cp2++) ) ; return(context); } char * -qualname(nam, lvl) /* get qualified name */ -register char *nam; -int lvl; +qualname( /* get qualified name */ + register char *nam, + int lvl +) { - static char nambuf[MAXWORD+1]; + static char nambuf[RMAXWORD+1]; register char *cp = nambuf, *cpp; /* check for explicit local */ if (*nam == CNTXMARK) @@ -267,7 +284,7 @@ int lvl; return(lvl > 0 ? NULL : nam); /* copy name to static buffer */ while (*nam) { - if (cp >= nambuf+MAXWORD) + if (cp >= nambuf+RMAXWORD) goto toolong; *cp++ = *nam++; } @@ -286,7 +303,7 @@ int lvl; ; } while (*cpp) { /* copy context to static buffer */ - if (cp >= nambuf+MAXWORD) + if (cp >= nambuf+RMAXWORD) goto toolong; *cp++ = *cpp++; } @@ -296,8 +313,10 @@ toolong: } -incontext(qn) /* is qualified name in current context? */ -register char *qn; +int +incontext( /* is qualified name in current context? */ + register char *qn +) { if (!context[0]) /* global context accepts all */ return(1); @@ -307,9 +326,10 @@ register char *qn; } -#ifdef OUTCHAN -chanout(cs) /* set output channels */ -int (*cs)(); +void +chanout( /* set output channels */ + void (*cs)(int n, double v) +) { register EPNODE *ep; @@ -317,11 +337,12 @@ int (*cs)(); (*cs)(ep->v.kid->v.chan, evalue(ep->v.kid->sibling)); } -#endif -dcleanup(lvl) /* clear definitions (0->vars,1->output,2->consts) */ -int lvl; +void +dcleanup( /* clear definitions (0->vars,1->output,2->consts) */ + int lvl +) { register int i; register VARDEF *vp; @@ -329,24 +350,24 @@ int lvl; /* if context is global, clear all */ for (i = 0; i < NHASH; i++) for (vp = hashtbl[i]; vp != NULL; vp = vp->next) - if (incontext(vp->name)) + if (incontext(vp->name)) { if (lvl >= 2) dremove(vp->name); else dclear(vp->name); -#ifdef OUTCHAN + } if (lvl >= 1) { for (ep = outchan; ep != NULL; ep = ep->sibling) epfree(ep); outchan = NULL; } -#endif } EPNODE * -dlookup(name) /* look up a definition */ -char *name; +dlookup( /* look up a definition */ + char *name +) { register VARDEF *vp; @@ -357,8 +378,9 @@ char *name; VARDEF * -varlookup(name) /* look up a variable */ -char *name; +varlookup( /* look up a variable */ + char *name +) { int lvl = 0; register char *qname; @@ -373,8 +395,9 @@ char *name; VARDEF * -varinsert(name) /* get a link to a variable */ -char *name; +varinsert( /* get a link to a variable */ + char *name +) { register VARDEF *vp; int hv; @@ -384,11 +407,7 @@ char *name; return(vp); } vp = (VARDEF *)emalloc(sizeof(VARDEF)); -#ifdef FUNCTION vp->lib = liblookup(name); -#else - vp->lib = NULL; -#endif if (vp->lib == NULL) /* if name not in library */ name = qualname(name, 0); /* use fully qualified version */ hv = hash(name); @@ -401,9 +420,10 @@ char *name; } -#ifdef FUNCTION -libupdate(fn) /* update library links */ -char *fn; +void +libupdate( /* update library links */ + char *fn +) { register int i; register VARDEF *vp; @@ -413,11 +433,12 @@ char *fn; if (vp->lib != NULL || fn == NULL || !strcmp(fn, vp->name)) vp->lib = liblookup(vp->name); } -#endif -varfree(ln) /* release link to variable */ -register VARDEF *ln; +void +varfree( /* release link to variable */ + register VARDEF *ln +) { register VARDEF *vp; int hv; @@ -440,19 +461,17 @@ register VARDEF *ln; EPNODE * -dfirst() /* return pointer to first definition */ +dfirst(void) /* return pointer to first definition */ { htndx = 0; htpos = NULL; -#ifdef OUTCHAN ochpos = outchan; -#endif return(dnext()); } EPNODE * -dnext() /* return pointer to next definition */ +dnext(void) /* return pointer to next definition */ { register EPNODE *ep; register char *nm; @@ -468,19 +487,16 @@ dnext() /* return pointer to next definition */ return(ep); } } -#ifdef OUTCHAN if ((ep = ochpos) != NULL) ochpos = ep->sibling; return(ep); -#else - return(NULL); -#endif } EPNODE * -dpop(name) /* pop a definition */ -char *name; +dpop( /* pop a definition */ + char *name +) { register VARDEF *vp; register EPNODE *dp; @@ -494,9 +510,11 @@ char *name; } -dpush(nm, ep) /* push on a definition */ -char *nm; -register EPNODE *ep; +void +dpush( /* push on a definition */ + char *nm, + register EPNODE *ep +) { register VARDEF *vp; @@ -506,9 +524,10 @@ register EPNODE *ep; } -#ifdef OUTCHAN -addchan(sp) /* add an output channel assignment */ -EPNODE *sp; +void +addchan( /* add an output channel assignment */ + EPNODE *sp +) { int ch = sp->v.kid->v.chan; register EPNODE *ep, *epl; @@ -534,10 +553,10 @@ EPNODE *sp; sp->sibling = NULL; } -#endif -getstatement() /* get next statement */ +void +getstatement(void) /* get next statement */ { register EPNODE *ep; char *qname; @@ -547,31 +566,25 @@ getstatement() /* get next statement */ scan(); return; } -#ifdef OUTCHAN - if (nextc == '$') { /* channel assignment */ + if (esupport&E_OUTCHAN && + nextc == '$') { /* channel assignment */ ep = getchan(); addchan(ep); - } else -#endif - { /* ordinary definition */ + } else { /* ordinary definition */ ep = getdefn(); qname = qualname(dname(ep), 0); -#ifdef REDEFW - if ((vdef = varlookup(qname)) != NULL) + if (esupport&E_REDEFW && (vdef = varlookup(qname)) != NULL) { if (vdef->def != NULL && epcmp(ep, vdef->def)) { wputs(qname); if (vdef->def->type == ':') wputs(": redefined constant expression\n"); else wputs(": redefined\n"); - } -#ifdef FUNCTION - else if (ep->v.kid->type == FUNC && vdef->lib != NULL) { + } else if (ep->v.kid->type == FUNC && vdef->lib != NULL) { wputs(qname); wputs(": definition hides library function\n"); } -#endif -#endif + } if (ep->type == ':') dremove(qname); else @@ -587,10 +600,11 @@ getstatement() /* get next statement */ EPNODE * -getdefn() /* A -> SYM = E1 */ - /* SYM : E1 */ - /* FUNC(SYM,..) = E1 */ - /* FUNC(SYM,..) : E1 */ +getdefn(void) + /* A -> SYM = E1 */ + /* SYM : E1 */ + /* FUNC(SYM,..) = E1 */ + /* FUNC(SYM,..) : E1 */ { register EPNODE *ep1, *ep2; @@ -601,8 +615,7 @@ getdefn() /* A -> SYM = E1 */ ep1->type = SYM; ep1->v.name = savestr(getname()); -#ifdef FUNCTION - if (nextc == '(') { + if (esupport&E_FUNCTION && nextc == '(') { ep2 = newnode(); ep2->type = FUNC; addekid(ep2, ep1); @@ -621,7 +634,6 @@ getdefn() /* A -> SYM = E1 */ scan(); curfunc = ep1; } -#endif if (nextc != '=' && nextc != ':') syntax("'=' or ':' expected"); @@ -632,11 +644,7 @@ getdefn() /* A -> SYM = E1 */ addekid(ep2, ep1); addekid(ep2, getE1()); - if ( -#ifdef FUNCTION - ep1->type == SYM && -#endif - ep1->sibling->type != NUM) { + if (ep1->type == SYM && ep1->sibling->type != NUM) { ep1 = newnode(); ep1->type = TICK; ep1->v.tick = 0; @@ -645,18 +653,14 @@ getdefn() /* A -> SYM = E1 */ ep1->type = NUM; addekid(ep2, ep1); } - -#ifdef FUNCTION curfunc = NULL; -#endif return(ep2); } -#ifdef OUTCHAN EPNODE * -getchan() /* A -> $N = E1 */ +getchan(void) /* A -> $N = E1 */ { register EPNODE *ep1, *ep2; @@ -679,7 +683,6 @@ getchan() /* A -> $N = E1 */ return(ep2); } -#endif @@ -688,10 +691,8 @@ getchan() /* A -> $N = E1 */ */ -static double -dvalue(name, d) /* evaluate a variable */ -char *name; -EPNODE *d; +static double /* evaluate a variable */ +dvalue(char *name, EPNODE *d) { register EPNODE *ep1, *ep2; @@ -704,8 +705,11 @@ EPNODE *d; if (ep1->type == NUM) return(ep1->v.num); /* return if number */ ep2 = ep1->sibling; /* check time */ - if (ep2->v.tick == 0 || ep2->v.tick < eclock) { - ep2->v.tick = d->type == ':' ? ~0L : eclock; + if (eclock >= MAXCLOCK) + eclock = 1; /* wrap clock counter */ + if (ep2->v.tick < MAXCLOCK && + (ep2->v.tick == 0) | (ep2->v.tick != eclock)) { + ep2->v.tick = d->type == ':' ? MAXCLOCK : eclock; ep2 = ep2->sibling; ep2->v.num = evalue(ep1); /* needs new value */ } else