1 |
– |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Store variable definitions. |
6 |
|
* |
19 |
|
* 4/23/91 Added ':' assignment for constant expressions |
20 |
|
* |
21 |
|
* 8/7/91 Added optional context path to append to variable names |
22 |
+ |
* |
23 |
+ |
* 5/17/2001 Fixed clock counter wrapping behavior |
24 |
+ |
* |
25 |
+ |
* 2/19/03 Eliminated conditional compiles in favor of esupport extern. |
26 |
|
*/ |
27 |
|
|
28 |
< |
#include <stdio.h> |
28 |
> |
#include "copyright.h" |
29 |
|
|
30 |
+ |
#include <stdio.h> |
31 |
+ |
#include <string.h> |
32 |
|
#include <ctype.h> |
33 |
|
|
34 |
+ |
#include "rterror.h" |
35 |
+ |
#include "rtmisc.h" |
36 |
|
#include "calcomp.h" |
37 |
|
|
38 |
|
#ifndef NHASH |
43 |
|
|
44 |
|
#define newnode() (EPNODE *)ecalloc(1, sizeof(EPNODE)) |
45 |
|
|
46 |
< |
extern char *ecalloc(), *emalloc(), *savestr(), *strcpy(); |
46 |
> |
static double dvalue(char *name, EPNODE *d); |
47 |
|
|
48 |
< |
static double dvalue(); |
48 |
> |
#define MAXCLOCK (1L<<31) /* clock wrap value */ |
49 |
|
|
50 |
|
unsigned long eclock = 0; /* value storage timer */ |
51 |
|
|
52 |
< |
static char context[MAXWORD+1]; /* current context path */ |
52 |
> |
#define MAXCNTX 1023 /* maximum context length */ |
53 |
|
|
54 |
+ |
static char context[MAXCNTX+1]; /* current context path */ |
55 |
+ |
|
56 |
|
static VARDEF *hashtbl[NHASH]; /* definition list */ |
57 |
|
static int htndx; /* index for */ |
58 |
|
static VARDEF *htpos; /* ...dfirst() and */ |
52 |
– |
#ifdef OUTCHAN |
59 |
|
static EPNODE *ochpos; /* ...dnext */ |
60 |
|
static EPNODE *outchan; |
55 |
– |
#endif |
61 |
|
|
57 |
– |
#ifdef FUNCTION |
62 |
|
EPNODE *curfunc = NULL; |
63 |
|
#define dname(ep) ((ep)->v.kid->type == SYM ? \ |
64 |
|
(ep)->v.kid->v.name : \ |
65 |
|
(ep)->v.kid->v.kid->v.name) |
62 |
– |
#else |
63 |
– |
#define dname(ep) ((ep)->v.kid->v.name) |
64 |
– |
#endif |
66 |
|
|
67 |
|
|
68 |
< |
fcompile(fname) /* get definitions from a file */ |
69 |
< |
char *fname; |
68 |
> |
void |
69 |
> |
fcompile( /* get definitions from a file */ |
70 |
> |
char *fname |
71 |
> |
) |
72 |
|
{ |
73 |
|
FILE *fp; |
74 |
|
|
87 |
|
} |
88 |
|
|
89 |
|
|
90 |
< |
scompile(str, fn, ln) /* get definitions from a string */ |
91 |
< |
char *str; |
92 |
< |
char *fn; |
93 |
< |
int ln; |
90 |
> |
void |
91 |
> |
scompile( /* get definitions from a string */ |
92 |
> |
char *str, |
93 |
> |
char *fn, |
94 |
> |
int ln |
95 |
> |
) |
96 |
|
{ |
97 |
|
initstr(str, fn, ln); |
98 |
|
while (nextc != EOF) |
101 |
|
|
102 |
|
|
103 |
|
double |
104 |
< |
varvalue(vname) /* return a variable's value */ |
105 |
< |
char *vname; |
104 |
> |
varvalue( /* return a variable's value */ |
105 |
> |
char *vname |
106 |
> |
) |
107 |
|
{ |
108 |
|
return(dvalue(vname, dlookup(vname))); |
109 |
|
} |
110 |
|
|
111 |
|
|
112 |
|
double |
113 |
< |
evariable(ep) /* evaluate a variable */ |
114 |
< |
EPNODE *ep; |
113 |
> |
evariable( /* evaluate a variable */ |
114 |
> |
EPNODE *ep |
115 |
> |
) |
116 |
|
{ |
117 |
|
register VARDEF *dp = ep->v.ln; |
118 |
|
|
120 |
|
} |
121 |
|
|
122 |
|
|
123 |
< |
varset(vname, assign, val) /* set a variable's value */ |
124 |
< |
char *vname; |
125 |
< |
int assign; |
126 |
< |
double val; |
123 |
> |
void |
124 |
> |
varset( /* set a variable's value */ |
125 |
> |
char *vname, |
126 |
> |
int assign, |
127 |
> |
double val |
128 |
> |
) |
129 |
|
{ |
130 |
|
char *qname; |
131 |
|
register EPNODE *ep1, *ep2; |
156 |
|
} |
157 |
|
|
158 |
|
|
159 |
< |
dclear(name) /* delete variable definitions of name */ |
160 |
< |
char *name; |
159 |
> |
void |
160 |
> |
dclear( /* delete variable definitions of name */ |
161 |
> |
char *name |
162 |
> |
) |
163 |
|
{ |
164 |
|
register EPNODE *ep; |
165 |
|
|
173 |
|
} |
174 |
|
|
175 |
|
|
176 |
< |
dremove(name) /* delete all definitions of name */ |
177 |
< |
char *name; |
176 |
> |
void |
177 |
> |
dremove( /* delete all definitions of name */ |
178 |
> |
char *name |
179 |
> |
) |
180 |
|
{ |
181 |
|
register EPNODE *ep; |
182 |
|
|
185 |
|
} |
186 |
|
|
187 |
|
|
188 |
< |
vardefined(name) /* return non-zero if variable defined */ |
189 |
< |
char *name; |
188 |
> |
int |
189 |
> |
vardefined( /* return non-zero if variable defined */ |
190 |
> |
char *name |
191 |
> |
) |
192 |
|
{ |
193 |
|
register EPNODE *dp; |
194 |
|
|
197 |
|
|
198 |
|
|
199 |
|
char * |
200 |
< |
setcontext(ctx) /* set a new context path */ |
201 |
< |
register char *ctx; |
200 |
> |
setcontext( /* set a new context path */ |
201 |
> |
register char *ctx |
202 |
> |
) |
203 |
|
{ |
204 |
|
register char *cpp; |
205 |
|
|
214 |
|
cpp = context; /* start context with mark */ |
215 |
|
*cpp++ = CNTXMARK; |
216 |
|
do { /* carefully copy new context */ |
217 |
< |
if (cpp >= context+MAXWORD) |
217 |
> |
if (cpp >= context+MAXCNTX) |
218 |
|
break; /* just copy what we can */ |
219 |
|
if (isid(*ctx)) |
220 |
|
*cpp++ = *ctx++; |
230 |
|
|
231 |
|
|
232 |
|
char * |
233 |
< |
pushcontext(ctx) /* push on another context */ |
234 |
< |
char *ctx; |
233 |
> |
pushcontext( /* push on another context */ |
234 |
> |
char *ctx |
235 |
> |
) |
236 |
|
{ |
237 |
< |
extern char *strncpy(), *strcpy(); |
221 |
< |
char oldcontext[MAXWORD+1]; |
237 |
> |
char oldcontext[MAXCNTX+1]; |
238 |
|
register int n; |
239 |
|
|
240 |
|
strcpy(oldcontext, context); /* save old context */ |
241 |
|
setcontext(ctx); /* set new context */ |
242 |
|
n = strlen(context); /* tack on old */ |
243 |
< |
if (n+strlen(oldcontext) > MAXWORD) { |
244 |
< |
strncpy(context+n, oldcontext, MAXWORD-n); |
245 |
< |
context[MAXWORD] = '\0'; |
243 |
> |
if (n+strlen(oldcontext) > MAXCNTX) { |
244 |
> |
strncpy(context+n, oldcontext, MAXCNTX-n); |
245 |
> |
context[MAXCNTX] = '\0'; |
246 |
|
} else |
247 |
|
strcpy(context+n, oldcontext); |
248 |
|
return(context); |
250 |
|
|
251 |
|
|
252 |
|
char * |
253 |
< |
popcontext() /* pop off top context */ |
253 |
> |
popcontext(void) /* pop off top context */ |
254 |
|
{ |
255 |
|
register char *cp1, *cp2; |
256 |
|
|
260 |
|
while (*++cp2 && *cp2 != CNTXMARK) |
261 |
|
; |
262 |
|
cp1 = context; /* copy tail to front */ |
263 |
< |
while (*cp1++ = *cp2++) |
263 |
> |
while ( (*cp1++ = *cp2++) ) |
264 |
|
; |
265 |
|
return(context); |
266 |
|
} |
267 |
|
|
268 |
|
|
269 |
|
char * |
270 |
< |
qualname(nam, lvl) /* get qualified name */ |
271 |
< |
register char *nam; |
272 |
< |
int lvl; |
270 |
> |
qualname( /* get qualified name */ |
271 |
> |
register char *nam, |
272 |
> |
int lvl |
273 |
> |
) |
274 |
|
{ |
275 |
< |
static char nambuf[MAXWORD+1]; |
275 |
> |
static char nambuf[RMAXWORD+1]; |
276 |
|
register char *cp = nambuf, *cpp; |
277 |
|
/* check for explicit local */ |
278 |
|
if (*nam == CNTXMARK) |
284 |
|
return(lvl > 0 ? NULL : nam); |
285 |
|
/* copy name to static buffer */ |
286 |
|
while (*nam) { |
287 |
< |
if (cp >= nambuf+MAXWORD) |
287 |
> |
if (cp >= nambuf+RMAXWORD) |
288 |
|
goto toolong; |
289 |
|
*cp++ = *nam++; |
290 |
|
} |
303 |
|
; |
304 |
|
} |
305 |
|
while (*cpp) { /* copy context to static buffer */ |
306 |
< |
if (cp >= nambuf+MAXWORD) |
306 |
> |
if (cp >= nambuf+RMAXWORD) |
307 |
|
goto toolong; |
308 |
|
*cp++ = *cpp++; |
309 |
|
} |
313 |
|
} |
314 |
|
|
315 |
|
|
316 |
< |
incontext(qn) /* is qualified name in current context? */ |
317 |
< |
register char *qn; |
316 |
> |
int |
317 |
> |
incontext( /* is qualified name in current context? */ |
318 |
> |
register char *qn |
319 |
> |
) |
320 |
|
{ |
321 |
|
if (!context[0]) /* global context accepts all */ |
322 |
|
return(1); |
326 |
|
} |
327 |
|
|
328 |
|
|
329 |
< |
#ifdef OUTCHAN |
330 |
< |
chanout(cs) /* set output channels */ |
331 |
< |
int (*cs)(); |
329 |
> |
void |
330 |
> |
chanout( /* set output channels */ |
331 |
> |
void (*cs)(int n, double v) |
332 |
> |
) |
333 |
|
{ |
334 |
|
register EPNODE *ep; |
335 |
|
|
337 |
|
(*cs)(ep->v.kid->v.chan, evalue(ep->v.kid->sibling)); |
338 |
|
|
339 |
|
} |
320 |
– |
#endif |
340 |
|
|
341 |
|
|
342 |
< |
dcleanup(lvl) /* clear definitions (0->vars,1->output,2->consts) */ |
343 |
< |
int lvl; |
342 |
> |
void |
343 |
> |
dcleanup( /* clear definitions (0->vars,1->output,2->consts) */ |
344 |
> |
int lvl |
345 |
> |
) |
346 |
|
{ |
347 |
|
register int i; |
348 |
|
register VARDEF *vp; |
350 |
|
/* if context is global, clear all */ |
351 |
|
for (i = 0; i < NHASH; i++) |
352 |
|
for (vp = hashtbl[i]; vp != NULL; vp = vp->next) |
353 |
< |
if (incontext(vp->name)) |
353 |
> |
if (incontext(vp->name)) { |
354 |
|
if (lvl >= 2) |
355 |
|
dremove(vp->name); |
356 |
|
else |
357 |
|
dclear(vp->name); |
358 |
< |
#ifdef OUTCHAN |
358 |
> |
} |
359 |
|
if (lvl >= 1) { |
360 |
|
for (ep = outchan; ep != NULL; ep = ep->sibling) |
361 |
|
epfree(ep); |
362 |
|
outchan = NULL; |
363 |
|
} |
343 |
– |
#endif |
364 |
|
} |
365 |
|
|
366 |
|
|
367 |
|
EPNODE * |
368 |
< |
dlookup(name) /* look up a definition */ |
369 |
< |
char *name; |
368 |
> |
dlookup( /* look up a definition */ |
369 |
> |
char *name |
370 |
> |
) |
371 |
|
{ |
372 |
|
register VARDEF *vp; |
373 |
|
|
378 |
|
|
379 |
|
|
380 |
|
VARDEF * |
381 |
< |
varlookup(name) /* look up a variable */ |
382 |
< |
char *name; |
381 |
> |
varlookup( /* look up a variable */ |
382 |
> |
char *name |
383 |
> |
) |
384 |
|
{ |
385 |
|
int lvl = 0; |
386 |
|
register char *qname; |
395 |
|
|
396 |
|
|
397 |
|
VARDEF * |
398 |
< |
varinsert(name) /* get a link to a variable */ |
399 |
< |
char *name; |
398 |
> |
varinsert( /* get a link to a variable */ |
399 |
> |
char *name |
400 |
> |
) |
401 |
|
{ |
402 |
|
register VARDEF *vp; |
403 |
|
int hv; |
407 |
|
return(vp); |
408 |
|
} |
409 |
|
vp = (VARDEF *)emalloc(sizeof(VARDEF)); |
387 |
– |
#ifdef FUNCTION |
410 |
|
vp->lib = liblookup(name); |
389 |
– |
#else |
390 |
– |
vp->lib = NULL; |
391 |
– |
#endif |
411 |
|
if (vp->lib == NULL) /* if name not in library */ |
412 |
|
name = qualname(name, 0); /* use fully qualified version */ |
413 |
|
hv = hash(name); |
420 |
|
} |
421 |
|
|
422 |
|
|
423 |
< |
#ifdef FUNCTION |
424 |
< |
libupdate(fn) /* update library links */ |
425 |
< |
char *fn; |
423 |
> |
void |
424 |
> |
libupdate( /* update library links */ |
425 |
> |
char *fn |
426 |
> |
) |
427 |
|
{ |
428 |
|
register int i; |
429 |
|
register VARDEF *vp; |
433 |
|
if (vp->lib != NULL || fn == NULL || !strcmp(fn, vp->name)) |
434 |
|
vp->lib = liblookup(vp->name); |
435 |
|
} |
416 |
– |
#endif |
436 |
|
|
437 |
|
|
438 |
< |
varfree(ln) /* release link to variable */ |
439 |
< |
register VARDEF *ln; |
438 |
> |
void |
439 |
> |
varfree( /* release link to variable */ |
440 |
> |
register VARDEF *ln |
441 |
> |
) |
442 |
|
{ |
443 |
|
register VARDEF *vp; |
444 |
|
int hv; |
461 |
|
|
462 |
|
|
463 |
|
EPNODE * |
464 |
< |
dfirst() /* return pointer to first definition */ |
464 |
> |
dfirst(void) /* return pointer to first definition */ |
465 |
|
{ |
466 |
|
htndx = 0; |
467 |
|
htpos = NULL; |
447 |
– |
#ifdef OUTCHAN |
468 |
|
ochpos = outchan; |
449 |
– |
#endif |
469 |
|
return(dnext()); |
470 |
|
} |
471 |
|
|
472 |
|
|
473 |
|
EPNODE * |
474 |
< |
dnext() /* return pointer to next definition */ |
474 |
> |
dnext(void) /* return pointer to next definition */ |
475 |
|
{ |
476 |
|
register EPNODE *ep; |
477 |
|
register char *nm; |
487 |
|
return(ep); |
488 |
|
} |
489 |
|
} |
471 |
– |
#ifdef OUTCHAN |
490 |
|
if ((ep = ochpos) != NULL) |
491 |
|
ochpos = ep->sibling; |
492 |
|
return(ep); |
475 |
– |
#else |
476 |
– |
return(NULL); |
477 |
– |
#endif |
493 |
|
} |
494 |
|
|
495 |
|
|
496 |
|
EPNODE * |
497 |
< |
dpop(name) /* pop a definition */ |
498 |
< |
char *name; |
497 |
> |
dpop( /* pop a definition */ |
498 |
> |
char *name |
499 |
> |
) |
500 |
|
{ |
501 |
|
register VARDEF *vp; |
502 |
|
register EPNODE *dp; |
510 |
|
} |
511 |
|
|
512 |
|
|
513 |
< |
dpush(nm, ep) /* push on a definition */ |
514 |
< |
char *nm; |
515 |
< |
register EPNODE *ep; |
513 |
> |
void |
514 |
> |
dpush( /* push on a definition */ |
515 |
> |
char *nm, |
516 |
> |
register EPNODE *ep |
517 |
> |
) |
518 |
|
{ |
519 |
|
register VARDEF *vp; |
520 |
|
|
524 |
|
} |
525 |
|
|
526 |
|
|
527 |
< |
#ifdef OUTCHAN |
528 |
< |
addchan(sp) /* add an output channel assignment */ |
529 |
< |
EPNODE *sp; |
527 |
> |
void |
528 |
> |
addchan( /* add an output channel assignment */ |
529 |
> |
EPNODE *sp |
530 |
> |
) |
531 |
|
{ |
532 |
|
int ch = sp->v.kid->v.chan; |
533 |
|
register EPNODE *ep, *epl; |
553 |
|
sp->sibling = NULL; |
554 |
|
|
555 |
|
} |
537 |
– |
#endif |
556 |
|
|
557 |
|
|
558 |
< |
getstatement() /* get next statement */ |
558 |
> |
void |
559 |
> |
getstatement(void) /* get next statement */ |
560 |
|
{ |
561 |
|
register EPNODE *ep; |
562 |
|
char *qname; |
566 |
|
scan(); |
567 |
|
return; |
568 |
|
} |
569 |
< |
#ifdef OUTCHAN |
570 |
< |
if (nextc == '$') { /* channel assignment */ |
569 |
> |
if (esupport&E_OUTCHAN && |
570 |
> |
nextc == '$') { /* channel assignment */ |
571 |
|
ep = getchan(); |
572 |
|
addchan(ep); |
573 |
< |
} else |
555 |
< |
#endif |
556 |
< |
{ /* ordinary definition */ |
573 |
> |
} else { /* ordinary definition */ |
574 |
|
ep = getdefn(); |
575 |
|
qname = qualname(dname(ep), 0); |
576 |
< |
#ifdef REDEFW |
560 |
< |
if ((vdef = varlookup(qname)) != NULL) |
576 |
> |
if (esupport&E_REDEFW && (vdef = varlookup(qname)) != NULL) { |
577 |
|
if (vdef->def != NULL && epcmp(ep, vdef->def)) { |
578 |
|
wputs(qname); |
579 |
|
if (vdef->def->type == ':') |
580 |
|
wputs(": redefined constant expression\n"); |
581 |
|
else |
582 |
|
wputs(": redefined\n"); |
583 |
< |
} |
568 |
< |
#ifdef FUNCTION |
569 |
< |
else if (ep->v.kid->type == FUNC && vdef->lib != NULL) { |
583 |
> |
} else if (ep->v.kid->type == FUNC && vdef->lib != NULL) { |
584 |
|
wputs(qname); |
585 |
|
wputs(": definition hides library function\n"); |
586 |
|
} |
587 |
< |
#endif |
574 |
< |
#endif |
587 |
> |
} |
588 |
|
if (ep->type == ':') |
589 |
|
dremove(qname); |
590 |
|
else |
600 |
|
|
601 |
|
|
602 |
|
EPNODE * |
603 |
< |
getdefn() /* A -> SYM = E1 */ |
604 |
< |
/* SYM : E1 */ |
605 |
< |
/* FUNC(SYM,..) = E1 */ |
606 |
< |
/* FUNC(SYM,..) : E1 */ |
603 |
> |
getdefn(void) |
604 |
> |
/* A -> SYM = E1 */ |
605 |
> |
/* SYM : E1 */ |
606 |
> |
/* FUNC(SYM,..) = E1 */ |
607 |
> |
/* FUNC(SYM,..) : E1 */ |
608 |
|
{ |
609 |
|
register EPNODE *ep1, *ep2; |
610 |
|
|
615 |
|
ep1->type = SYM; |
616 |
|
ep1->v.name = savestr(getname()); |
617 |
|
|
618 |
< |
#ifdef FUNCTION |
605 |
< |
if (nextc == '(') { |
618 |
> |
if (esupport&E_FUNCTION && nextc == '(') { |
619 |
|
ep2 = newnode(); |
620 |
|
ep2->type = FUNC; |
621 |
|
addekid(ep2, ep1); |
634 |
|
scan(); |
635 |
|
curfunc = ep1; |
636 |
|
} |
624 |
– |
#endif |
637 |
|
|
638 |
|
if (nextc != '=' && nextc != ':') |
639 |
|
syntax("'=' or ':' expected"); |
644 |
|
addekid(ep2, ep1); |
645 |
|
addekid(ep2, getE1()); |
646 |
|
|
647 |
< |
if ( |
636 |
< |
#ifdef FUNCTION |
637 |
< |
ep1->type == SYM && |
638 |
< |
#endif |
639 |
< |
ep1->sibling->type != NUM) { |
647 |
> |
if (ep1->type == SYM && ep1->sibling->type != NUM) { |
648 |
|
ep1 = newnode(); |
649 |
|
ep1->type = TICK; |
650 |
|
ep1->v.tick = 0; |
653 |
|
ep1->type = NUM; |
654 |
|
addekid(ep2, ep1); |
655 |
|
} |
648 |
– |
|
649 |
– |
#ifdef FUNCTION |
656 |
|
curfunc = NULL; |
651 |
– |
#endif |
657 |
|
|
658 |
|
return(ep2); |
659 |
|
} |
660 |
|
|
661 |
|
|
657 |
– |
#ifdef OUTCHAN |
662 |
|
EPNODE * |
663 |
< |
getchan() /* A -> $N = E1 */ |
663 |
> |
getchan(void) /* A -> $N = E1 */ |
664 |
|
{ |
665 |
|
register EPNODE *ep1, *ep2; |
666 |
|
|
683 |
|
|
684 |
|
return(ep2); |
685 |
|
} |
682 |
– |
#endif |
686 |
|
|
687 |
|
|
688 |
|
|
691 |
|
*/ |
692 |
|
|
693 |
|
|
694 |
< |
static double |
695 |
< |
dvalue(name, d) /* evaluate a variable */ |
693 |
< |
char *name; |
694 |
< |
EPNODE *d; |
694 |
> |
static double /* evaluate a variable */ |
695 |
> |
dvalue(char *name, EPNODE *d) |
696 |
|
{ |
697 |
|
register EPNODE *ep1, *ep2; |
698 |
|
|
705 |
|
if (ep1->type == NUM) |
706 |
|
return(ep1->v.num); /* return if number */ |
707 |
|
ep2 = ep1->sibling; /* check time */ |
708 |
< |
if (ep2->v.tick == 0 || ep2->v.tick < eclock) { |
709 |
< |
ep2->v.tick = d->type == ':' ? ~0L : eclock; |
708 |
> |
if (eclock >= MAXCLOCK) |
709 |
> |
eclock = 1; /* wrap clock counter */ |
710 |
> |
if (ep2->v.tick < MAXCLOCK && |
711 |
> |
(ep2->v.tick == 0) | (ep2->v.tick != eclock)) { |
712 |
> |
ep2->v.tick = d->type == ':' ? MAXCLOCK : eclock; |
713 |
|
ep2 = ep2->sibling; |
714 |
|
ep2->v.num = evalue(ep1); /* needs new value */ |
715 |
|
} else |