1 |
– |
/* Copyright (c) 1997 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 "rtio.h" |
36 |
+ |
#include "rtmisc.h" |
37 |
|
#include "calcomp.h" |
38 |
|
|
39 |
|
#ifndef NHASH |
44 |
|
|
45 |
|
#define newnode() (EPNODE *)ecalloc(1, sizeof(EPNODE)) |
46 |
|
|
47 |
< |
extern char *ecalloc(), *emalloc(), *savestr(), *strcpy(); |
47 |
> |
static double dvalue(char *name, EPNODE *d); |
48 |
|
|
49 |
< |
static double dvalue(); |
49 |
> |
#define MAXCLOCK (1L<<31) /* clock wrap value */ |
50 |
|
|
51 |
|
unsigned long eclock = 0; /* value storage timer */ |
52 |
|
|
57 |
|
static VARDEF *hashtbl[NHASH]; /* definition list */ |
58 |
|
static int htndx; /* index for */ |
59 |
|
static VARDEF *htpos; /* ...dfirst() and */ |
54 |
– |
#ifdef OUTCHAN |
60 |
|
static EPNODE *ochpos; /* ...dnext */ |
61 |
|
static EPNODE *outchan; |
57 |
– |
#endif |
62 |
|
|
59 |
– |
#ifdef FUNCTION |
63 |
|
EPNODE *curfunc = NULL; |
64 |
|
#define dname(ep) ((ep)->v.kid->type == SYM ? \ |
65 |
|
(ep)->v.kid->v.name : \ |
66 |
|
(ep)->v.kid->v.kid->v.name) |
64 |
– |
#else |
65 |
– |
#define dname(ep) ((ep)->v.kid->v.name) |
66 |
– |
#endif |
67 |
|
|
68 |
|
|
69 |
< |
fcompile(fname) /* get definitions from a file */ |
70 |
< |
char *fname; |
69 |
> |
void |
70 |
> |
fcompile( /* get definitions from a file */ |
71 |
> |
char *fname |
72 |
> |
) |
73 |
|
{ |
74 |
|
FILE *fp; |
75 |
|
|
88 |
|
} |
89 |
|
|
90 |
|
|
91 |
< |
scompile(str, fn, ln) /* get definitions from a string */ |
92 |
< |
char *str; |
93 |
< |
char *fn; |
94 |
< |
int ln; |
91 |
> |
void |
92 |
> |
scompile( /* get definitions from a string */ |
93 |
> |
char *str, |
94 |
> |
char *fn, |
95 |
> |
int ln |
96 |
> |
) |
97 |
|
{ |
98 |
|
initstr(str, fn, ln); |
99 |
|
while (nextc != EOF) |
102 |
|
|
103 |
|
|
104 |
|
double |
105 |
< |
varvalue(vname) /* return a variable's value */ |
106 |
< |
char *vname; |
105 |
> |
varvalue( /* return a variable's value */ |
106 |
> |
char *vname |
107 |
> |
) |
108 |
|
{ |
109 |
|
return(dvalue(vname, dlookup(vname))); |
110 |
|
} |
111 |
|
|
112 |
|
|
113 |
|
double |
114 |
< |
evariable(ep) /* evaluate a variable */ |
115 |
< |
EPNODE *ep; |
114 |
> |
evariable( /* evaluate a variable */ |
115 |
> |
EPNODE *ep |
116 |
> |
) |
117 |
|
{ |
118 |
|
register VARDEF *dp = ep->v.ln; |
119 |
|
|
121 |
|
} |
122 |
|
|
123 |
|
|
124 |
< |
varset(vname, assign, val) /* set a variable's value */ |
125 |
< |
char *vname; |
126 |
< |
int assign; |
127 |
< |
double val; |
124 |
> |
void |
125 |
> |
varset( /* set a variable's value */ |
126 |
> |
char *vname, |
127 |
> |
int assign, |
128 |
> |
double val |
129 |
> |
) |
130 |
|
{ |
131 |
|
char *qname; |
132 |
|
register EPNODE *ep1, *ep2; |
157 |
|
} |
158 |
|
|
159 |
|
|
160 |
< |
dclear(name) /* delete variable definitions of name */ |
161 |
< |
char *name; |
160 |
> |
void |
161 |
> |
dclear( /* delete variable definitions of name */ |
162 |
> |
char *name |
163 |
> |
) |
164 |
|
{ |
165 |
|
register EPNODE *ep; |
166 |
|
|
174 |
|
} |
175 |
|
|
176 |
|
|
177 |
< |
dremove(name) /* delete all definitions of name */ |
178 |
< |
char *name; |
177 |
> |
void |
178 |
> |
dremove( /* delete all definitions of name */ |
179 |
> |
char *name |
180 |
> |
) |
181 |
|
{ |
182 |
|
register EPNODE *ep; |
183 |
|
|
186 |
|
} |
187 |
|
|
188 |
|
|
189 |
< |
vardefined(name) /* return non-zero if variable defined */ |
190 |
< |
char *name; |
189 |
> |
int |
190 |
> |
vardefined( /* return non-zero if variable defined */ |
191 |
> |
char *name |
192 |
> |
) |
193 |
|
{ |
194 |
|
register EPNODE *dp; |
195 |
|
|
198 |
|
|
199 |
|
|
200 |
|
char * |
201 |
< |
setcontext(ctx) /* set a new context path */ |
202 |
< |
register char *ctx; |
201 |
> |
setcontext( /* set a new context path */ |
202 |
> |
register char *ctx |
203 |
> |
) |
204 |
|
{ |
205 |
|
register char *cpp; |
206 |
|
|
231 |
|
|
232 |
|
|
233 |
|
char * |
234 |
< |
pushcontext(ctx) /* push on another context */ |
235 |
< |
char *ctx; |
234 |
> |
pushcontext( /* push on another context */ |
235 |
> |
char *ctx |
236 |
> |
) |
237 |
|
{ |
222 |
– |
extern char *strncpy(), *strcpy(); |
238 |
|
char oldcontext[MAXCNTX+1]; |
239 |
|
register int n; |
240 |
|
|
251 |
|
|
252 |
|
|
253 |
|
char * |
254 |
< |
popcontext() /* pop off top context */ |
254 |
> |
popcontext(void) /* pop off top context */ |
255 |
|
{ |
256 |
|
register char *cp1, *cp2; |
257 |
|
|
261 |
|
while (*++cp2 && *cp2 != CNTXMARK) |
262 |
|
; |
263 |
|
cp1 = context; /* copy tail to front */ |
264 |
< |
while (*cp1++ = *cp2++) |
264 |
> |
while ( (*cp1++ = *cp2++) ) |
265 |
|
; |
266 |
|
return(context); |
267 |
|
} |
268 |
|
|
269 |
|
|
270 |
|
char * |
271 |
< |
qualname(nam, lvl) /* get qualified name */ |
272 |
< |
register char *nam; |
273 |
< |
int lvl; |
271 |
> |
qualname( /* get qualified name */ |
272 |
> |
register char *nam, |
273 |
> |
int lvl |
274 |
> |
) |
275 |
|
{ |
276 |
< |
static char nambuf[MAXWORD+1]; |
276 |
> |
static char nambuf[RMAXWORD+1]; |
277 |
|
register char *cp = nambuf, *cpp; |
278 |
|
/* check for explicit local */ |
279 |
|
if (*nam == CNTXMARK) |
285 |
|
return(lvl > 0 ? NULL : nam); |
286 |
|
/* copy name to static buffer */ |
287 |
|
while (*nam) { |
288 |
< |
if (cp >= nambuf+MAXWORD) |
288 |
> |
if (cp >= nambuf+RMAXWORD) |
289 |
|
goto toolong; |
290 |
|
*cp++ = *nam++; |
291 |
|
} |
304 |
|
; |
305 |
|
} |
306 |
|
while (*cpp) { /* copy context to static buffer */ |
307 |
< |
if (cp >= nambuf+MAXWORD) |
307 |
> |
if (cp >= nambuf+RMAXWORD) |
308 |
|
goto toolong; |
309 |
|
*cp++ = *cpp++; |
310 |
|
} |
314 |
|
} |
315 |
|
|
316 |
|
|
317 |
< |
incontext(qn) /* is qualified name in current context? */ |
318 |
< |
register char *qn; |
317 |
> |
int |
318 |
> |
incontext( /* is qualified name in current context? */ |
319 |
> |
register char *qn |
320 |
> |
) |
321 |
|
{ |
322 |
|
if (!context[0]) /* global context accepts all */ |
323 |
|
return(1); |
327 |
|
} |
328 |
|
|
329 |
|
|
330 |
< |
#ifdef OUTCHAN |
331 |
< |
chanout(cs) /* set output channels */ |
332 |
< |
int (*cs)(); |
330 |
> |
void |
331 |
> |
chanout( /* set output channels */ |
332 |
> |
void (*cs)(int n, double v) |
333 |
> |
) |
334 |
|
{ |
335 |
|
register EPNODE *ep; |
336 |
|
|
338 |
|
(*cs)(ep->v.kid->v.chan, evalue(ep->v.kid->sibling)); |
339 |
|
|
340 |
|
} |
322 |
– |
#endif |
341 |
|
|
342 |
|
|
343 |
< |
dcleanup(lvl) /* clear definitions (0->vars,1->output,2->consts) */ |
344 |
< |
int lvl; |
343 |
> |
void |
344 |
> |
dcleanup( /* clear definitions (0->vars,1->output,2->consts) */ |
345 |
> |
int lvl |
346 |
> |
) |
347 |
|
{ |
348 |
|
register int i; |
349 |
|
register VARDEF *vp; |
351 |
|
/* if context is global, clear all */ |
352 |
|
for (i = 0; i < NHASH; i++) |
353 |
|
for (vp = hashtbl[i]; vp != NULL; vp = vp->next) |
354 |
< |
if (incontext(vp->name)) |
354 |
> |
if (incontext(vp->name)) { |
355 |
|
if (lvl >= 2) |
356 |
|
dremove(vp->name); |
357 |
|
else |
358 |
|
dclear(vp->name); |
359 |
< |
#ifdef OUTCHAN |
359 |
> |
} |
360 |
|
if (lvl >= 1) { |
361 |
|
for (ep = outchan; ep != NULL; ep = ep->sibling) |
362 |
|
epfree(ep); |
363 |
|
outchan = NULL; |
364 |
|
} |
345 |
– |
#endif |
365 |
|
} |
366 |
|
|
367 |
|
|
368 |
|
EPNODE * |
369 |
< |
dlookup(name) /* look up a definition */ |
370 |
< |
char *name; |
369 |
> |
dlookup( /* look up a definition */ |
370 |
> |
char *name |
371 |
> |
) |
372 |
|
{ |
373 |
|
register VARDEF *vp; |
374 |
|
|
379 |
|
|
380 |
|
|
381 |
|
VARDEF * |
382 |
< |
varlookup(name) /* look up a variable */ |
383 |
< |
char *name; |
382 |
> |
varlookup( /* look up a variable */ |
383 |
> |
char *name |
384 |
> |
) |
385 |
|
{ |
386 |
|
int lvl = 0; |
387 |
|
register char *qname; |
396 |
|
|
397 |
|
|
398 |
|
VARDEF * |
399 |
< |
varinsert(name) /* get a link to a variable */ |
400 |
< |
char *name; |
399 |
> |
varinsert( /* get a link to a variable */ |
400 |
> |
char *name |
401 |
> |
) |
402 |
|
{ |
403 |
|
register VARDEF *vp; |
404 |
|
int hv; |
408 |
|
return(vp); |
409 |
|
} |
410 |
|
vp = (VARDEF *)emalloc(sizeof(VARDEF)); |
389 |
– |
#ifdef FUNCTION |
411 |
|
vp->lib = liblookup(name); |
391 |
– |
#else |
392 |
– |
vp->lib = NULL; |
393 |
– |
#endif |
412 |
|
if (vp->lib == NULL) /* if name not in library */ |
413 |
|
name = qualname(name, 0); /* use fully qualified version */ |
414 |
|
hv = hash(name); |
421 |
|
} |
422 |
|
|
423 |
|
|
424 |
< |
#ifdef FUNCTION |
425 |
< |
libupdate(fn) /* update library links */ |
426 |
< |
char *fn; |
424 |
> |
void |
425 |
> |
libupdate( /* update library links */ |
426 |
> |
char *fn |
427 |
> |
) |
428 |
|
{ |
429 |
|
register int i; |
430 |
|
register VARDEF *vp; |
434 |
|
if (vp->lib != NULL || fn == NULL || !strcmp(fn, vp->name)) |
435 |
|
vp->lib = liblookup(vp->name); |
436 |
|
} |
418 |
– |
#endif |
437 |
|
|
438 |
|
|
439 |
< |
varfree(ln) /* release link to variable */ |
440 |
< |
register VARDEF *ln; |
439 |
> |
void |
440 |
> |
varfree( /* release link to variable */ |
441 |
> |
register VARDEF *ln |
442 |
> |
) |
443 |
|
{ |
444 |
|
register VARDEF *vp; |
445 |
|
int hv; |
462 |
|
|
463 |
|
|
464 |
|
EPNODE * |
465 |
< |
dfirst() /* return pointer to first definition */ |
465 |
> |
dfirst(void) /* return pointer to first definition */ |
466 |
|
{ |
467 |
|
htndx = 0; |
468 |
|
htpos = NULL; |
449 |
– |
#ifdef OUTCHAN |
469 |
|
ochpos = outchan; |
451 |
– |
#endif |
470 |
|
return(dnext()); |
471 |
|
} |
472 |
|
|
473 |
|
|
474 |
|
EPNODE * |
475 |
< |
dnext() /* return pointer to next definition */ |
475 |
> |
dnext(void) /* return pointer to next definition */ |
476 |
|
{ |
477 |
|
register EPNODE *ep; |
478 |
|
register char *nm; |
488 |
|
return(ep); |
489 |
|
} |
490 |
|
} |
473 |
– |
#ifdef OUTCHAN |
491 |
|
if ((ep = ochpos) != NULL) |
492 |
|
ochpos = ep->sibling; |
493 |
|
return(ep); |
477 |
– |
#else |
478 |
– |
return(NULL); |
479 |
– |
#endif |
494 |
|
} |
495 |
|
|
496 |
|
|
497 |
|
EPNODE * |
498 |
< |
dpop(name) /* pop a definition */ |
499 |
< |
char *name; |
498 |
> |
dpop( /* pop a definition */ |
499 |
> |
char *name |
500 |
> |
) |
501 |
|
{ |
502 |
|
register VARDEF *vp; |
503 |
|
register EPNODE *dp; |
511 |
|
} |
512 |
|
|
513 |
|
|
514 |
< |
dpush(nm, ep) /* push on a definition */ |
515 |
< |
char *nm; |
516 |
< |
register EPNODE *ep; |
514 |
> |
void |
515 |
> |
dpush( /* push on a definition */ |
516 |
> |
char *nm, |
517 |
> |
register EPNODE *ep |
518 |
> |
) |
519 |
|
{ |
520 |
|
register VARDEF *vp; |
521 |
|
|
525 |
|
} |
526 |
|
|
527 |
|
|
528 |
< |
#ifdef OUTCHAN |
529 |
< |
addchan(sp) /* add an output channel assignment */ |
530 |
< |
EPNODE *sp; |
528 |
> |
void |
529 |
> |
addchan( /* add an output channel assignment */ |
530 |
> |
EPNODE *sp |
531 |
> |
) |
532 |
|
{ |
533 |
|
int ch = sp->v.kid->v.chan; |
534 |
|
register EPNODE *ep, *epl; |
554 |
|
sp->sibling = NULL; |
555 |
|
|
556 |
|
} |
539 |
– |
#endif |
557 |
|
|
558 |
|
|
559 |
< |
getstatement() /* get next statement */ |
559 |
> |
void |
560 |
> |
getstatement(void) /* get next statement */ |
561 |
|
{ |
562 |
|
register EPNODE *ep; |
563 |
|
char *qname; |
567 |
|
scan(); |
568 |
|
return; |
569 |
|
} |
570 |
< |
#ifdef OUTCHAN |
571 |
< |
if (nextc == '$') { /* channel assignment */ |
570 |
> |
if (esupport&E_OUTCHAN && |
571 |
> |
nextc == '$') { /* channel assignment */ |
572 |
|
ep = getchan(); |
573 |
|
addchan(ep); |
574 |
< |
} else |
557 |
< |
#endif |
558 |
< |
{ /* ordinary definition */ |
574 |
> |
} else { /* ordinary definition */ |
575 |
|
ep = getdefn(); |
576 |
|
qname = qualname(dname(ep), 0); |
577 |
< |
#ifdef REDEFW |
562 |
< |
if ((vdef = varlookup(qname)) != NULL) |
577 |
> |
if (esupport&E_REDEFW && (vdef = varlookup(qname)) != NULL) { |
578 |
|
if (vdef->def != NULL && epcmp(ep, vdef->def)) { |
579 |
|
wputs(qname); |
580 |
|
if (vdef->def->type == ':') |
581 |
|
wputs(": redefined constant expression\n"); |
582 |
|
else |
583 |
|
wputs(": redefined\n"); |
584 |
< |
} |
570 |
< |
#ifdef FUNCTION |
571 |
< |
else if (ep->v.kid->type == FUNC && vdef->lib != NULL) { |
584 |
> |
} else if (ep->v.kid->type == FUNC && vdef->lib != NULL) { |
585 |
|
wputs(qname); |
586 |
|
wputs(": definition hides library function\n"); |
587 |
|
} |
588 |
< |
#endif |
576 |
< |
#endif |
588 |
> |
} |
589 |
|
if (ep->type == ':') |
590 |
|
dremove(qname); |
591 |
|
else |
601 |
|
|
602 |
|
|
603 |
|
EPNODE * |
604 |
< |
getdefn() /* A -> SYM = E1 */ |
605 |
< |
/* SYM : E1 */ |
606 |
< |
/* FUNC(SYM,..) = E1 */ |
607 |
< |
/* FUNC(SYM,..) : E1 */ |
604 |
> |
getdefn(void) |
605 |
> |
/* A -> SYM = E1 */ |
606 |
> |
/* SYM : E1 */ |
607 |
> |
/* FUNC(SYM,..) = E1 */ |
608 |
> |
/* FUNC(SYM,..) : E1 */ |
609 |
|
{ |
610 |
|
register EPNODE *ep1, *ep2; |
611 |
|
|
616 |
|
ep1->type = SYM; |
617 |
|
ep1->v.name = savestr(getname()); |
618 |
|
|
619 |
< |
#ifdef FUNCTION |
607 |
< |
if (nextc == '(') { |
619 |
> |
if (esupport&E_FUNCTION && nextc == '(') { |
620 |
|
ep2 = newnode(); |
621 |
|
ep2->type = FUNC; |
622 |
|
addekid(ep2, ep1); |
635 |
|
scan(); |
636 |
|
curfunc = ep1; |
637 |
|
} |
626 |
– |
#endif |
638 |
|
|
639 |
|
if (nextc != '=' && nextc != ':') |
640 |
|
syntax("'=' or ':' expected"); |
645 |
|
addekid(ep2, ep1); |
646 |
|
addekid(ep2, getE1()); |
647 |
|
|
648 |
< |
if ( |
638 |
< |
#ifdef FUNCTION |
639 |
< |
ep1->type == SYM && |
640 |
< |
#endif |
641 |
< |
ep1->sibling->type != NUM) { |
648 |
> |
if (ep1->type == SYM && ep1->sibling->type != NUM) { |
649 |
|
ep1 = newnode(); |
650 |
|
ep1->type = TICK; |
651 |
|
ep1->v.tick = 0; |
654 |
|
ep1->type = NUM; |
655 |
|
addekid(ep2, ep1); |
656 |
|
} |
650 |
– |
|
651 |
– |
#ifdef FUNCTION |
657 |
|
curfunc = NULL; |
653 |
– |
#endif |
658 |
|
|
659 |
|
return(ep2); |
660 |
|
} |
661 |
|
|
662 |
|
|
659 |
– |
#ifdef OUTCHAN |
663 |
|
EPNODE * |
664 |
< |
getchan() /* A -> $N = E1 */ |
664 |
> |
getchan(void) /* A -> $N = E1 */ |
665 |
|
{ |
666 |
|
register EPNODE *ep1, *ep2; |
667 |
|
|
684 |
|
|
685 |
|
return(ep2); |
686 |
|
} |
684 |
– |
#endif |
687 |
|
|
688 |
|
|
689 |
|
|
692 |
|
*/ |
693 |
|
|
694 |
|
|
695 |
< |
static double |
696 |
< |
dvalue(name, d) /* evaluate a variable */ |
695 |
< |
char *name; |
696 |
< |
EPNODE *d; |
695 |
> |
static double /* evaluate a variable */ |
696 |
> |
dvalue(char *name, EPNODE *d) |
697 |
|
{ |
698 |
|
register EPNODE *ep1, *ep2; |
699 |
|
|
706 |
|
if (ep1->type == NUM) |
707 |
|
return(ep1->v.num); /* return if number */ |
708 |
|
ep2 = ep1->sibling; /* check time */ |
709 |
< |
if (ep2->v.tick == 0 || ep2->v.tick < eclock) { |
710 |
< |
ep2->v.tick = d->type == ':' ? ~0L : eclock; |
709 |
> |
if (eclock >= MAXCLOCK) |
710 |
> |
eclock = 1; /* wrap clock counter */ |
711 |
> |
if (ep2->v.tick < MAXCLOCK && |
712 |
> |
(ep2->v.tick == 0) | (ep2->v.tick != eclock)) { |
713 |
> |
ep2->v.tick = d->type == ':' ? MAXCLOCK : eclock; |
714 |
|
ep2 = ep2->sibling; |
715 |
|
ep2->v.num = evalue(ep1); /* needs new value */ |
716 |
|
} else |