1 |
– |
/* Copyright (c) 1991 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 |
|
* calfunc.c - routines for calcomp using functions. |
6 |
|
* |
7 |
< |
* The define BIGLIB pulls in a large number of the |
11 |
< |
* available math routines. |
12 |
< |
* |
13 |
< |
* If VARIABLE is not defined, only library functions |
7 |
> |
* If VARIABLE is not set, only library functions |
8 |
|
* can be accessed. |
9 |
|
* |
10 |
< |
* 4/2/86 |
10 |
> |
* 2/19/03 Eliminated conditional compiles in favor of esupport extern. |
11 |
|
*/ |
12 |
|
|
13 |
< |
#include <stdio.h> |
13 |
> |
#include "copyright.h" |
14 |
|
|
15 |
+ |
#include <stdio.h> |
16 |
+ |
#include <string.h> |
17 |
|
#include <errno.h> |
18 |
+ |
#include <math.h> |
19 |
|
|
20 |
+ |
#include "rterror.h" |
21 |
|
#include "calcomp.h" |
22 |
|
|
23 |
|
/* bits in argument flag (better be right!) */ |
34 |
|
|
35 |
|
static ACTIVATION *curact = NULL; |
36 |
|
|
37 |
< |
static double libfunc(); |
37 |
> |
static double libfunc(char *fname, VARDEF *vp); |
38 |
|
|
39 |
+ |
#ifndef MAXLIB |
40 |
|
#define MAXLIB 64 /* maximum number of library functions */ |
41 |
+ |
#endif |
42 |
|
|
43 |
|
static double l_if(), l_select(), l_rand(); |
44 |
|
static double l_floor(), l_ceil(); |
45 |
– |
#ifdef BIGLIB |
45 |
|
static double l_sqrt(); |
46 |
|
static double l_sin(), l_cos(), l_tan(); |
47 |
|
static double l_asin(), l_acos(), l_atan(), l_atan2(); |
48 |
|
static double l_exp(), l_log(), l_log10(); |
50 |
– |
#endif |
49 |
|
|
52 |
– |
#ifdef BIGLIB |
50 |
|
/* functions must be listed alphabetically */ |
51 |
|
static LIBR library[MAXLIB] = { |
52 |
|
{ "acos", 1, ':', l_acos }, |
69 |
|
|
70 |
|
static int libsize = 16; |
71 |
|
|
75 |
– |
#else |
76 |
– |
/* functions must be listed alphabetically */ |
77 |
– |
static LIBR library[MAXLIB] = { |
78 |
– |
{ "ceil", 1, ':', l_ceil }, |
79 |
– |
{ "floor", 1, ':', l_floor }, |
80 |
– |
{ "if", 3, ':', l_if }, |
81 |
– |
{ "rand", 1, ':', l_rand }, |
82 |
– |
{ "select", 1, ':', l_select }, |
83 |
– |
}; |
84 |
– |
|
85 |
– |
static int libsize = 5; |
86 |
– |
|
87 |
– |
#endif |
88 |
– |
|
89 |
– |
extern char *savestr(), *emalloc(); |
90 |
– |
|
91 |
– |
extern LIBR *liblookup(); |
92 |
– |
|
93 |
– |
extern VARDEF *argf(); |
94 |
– |
|
95 |
– |
#ifdef VARIABLE |
72 |
|
#define resolve(ep) ((ep)->type==VAR?(ep)->v.ln:argf((ep)->v.chan)) |
97 |
– |
#else |
98 |
– |
#define resolve(ep) ((ep)->v.ln) |
99 |
– |
#define varlookup(name) NULL |
100 |
– |
#endif |
73 |
|
|
74 |
|
|
75 |
|
int |
76 |
|
fundefined(fname) /* return # of arguments for function */ |
77 |
|
char *fname; |
78 |
|
{ |
79 |
< |
LIBR *lp; |
79 |
> |
register LIBR *lp; |
80 |
|
register VARDEF *vp; |
81 |
|
|
82 |
< |
if ((vp = varlookup(fname)) == NULL || vp->def == NULL |
83 |
< |
|| vp->def->v.kid->type != FUNC) |
112 |
< |
if ((lp = liblookup(fname)) == NULL) |
113 |
< |
return(0); |
114 |
< |
else |
115 |
< |
return(lp->nargs); |
116 |
< |
else |
82 |
> |
if ((vp = varlookup(fname)) != NULL && vp->def != NULL |
83 |
> |
&& vp->def->v.kid->type == FUNC) |
84 |
|
return(nekids(vp->def->v.kid) - 1); |
85 |
+ |
lp = vp != NULL ? vp->lib : liblookup(fname); |
86 |
+ |
if (lp == NULL) |
87 |
+ |
return(0); |
88 |
+ |
return(lp->nargs); |
89 |
|
} |
90 |
|
|
91 |
|
|
120 |
|
} |
121 |
|
|
122 |
|
|
123 |
+ |
void |
124 |
|
funset(fname, nargs, assign, fptr) /* set a library function */ |
125 |
|
char *fname; |
126 |
|
int nargs; |
127 |
|
int assign; |
128 |
|
double (*fptr)(); |
129 |
|
{ |
130 |
+ |
int oldlibsize = libsize; |
131 |
+ |
char *cp; |
132 |
|
register LIBR *lp; |
133 |
< |
|
134 |
< |
if ((lp = liblookup(fname)) == NULL) { |
133 |
> |
/* check for context */ |
134 |
> |
for (cp = fname; *cp; cp++) |
135 |
> |
; |
136 |
> |
if (cp == fname) |
137 |
> |
return; |
138 |
> |
if (cp[-1] == CNTXMARK) |
139 |
> |
*--cp = '\0'; |
140 |
> |
if ((lp = liblookup(fname)) == NULL) { /* insert */ |
141 |
|
if (libsize >= MAXLIB) { |
142 |
|
eputs("Too many library functons!\n"); |
143 |
|
quit(1); |
152 |
|
break; |
153 |
|
libsize++; |
154 |
|
} |
155 |
< |
lp[0].fname = fname; /* must be static! */ |
156 |
< |
lp[0].nargs = nargs; |
157 |
< |
lp[0].atyp = assign; |
158 |
< |
lp[0].f = fptr; |
155 |
> |
if (fptr == NULL) { /* delete */ |
156 |
> |
while (lp < &library[libsize-1]) { |
157 |
> |
lp[0].fname = lp[1].fname; |
158 |
> |
lp[0].nargs = lp[1].nargs; |
159 |
> |
lp[0].atyp = lp[1].atyp; |
160 |
> |
lp[0].f = lp[1].f; |
161 |
> |
lp++; |
162 |
> |
} |
163 |
> |
libsize--; |
164 |
> |
} else { /* or assign */ |
165 |
> |
lp[0].fname = fname; /* string must be static! */ |
166 |
> |
lp[0].nargs = nargs; |
167 |
> |
lp[0].atyp = assign; |
168 |
> |
lp[0].f = fptr; |
169 |
> |
} |
170 |
> |
if (libsize != oldlibsize) |
171 |
> |
libupdate(fname); /* relink library */ |
172 |
|
} |
173 |
|
|
174 |
|
|
193 |
|
register int n; |
194 |
|
{ |
195 |
|
register ACTIVATION *actp = curact; |
196 |
< |
EPNODE *ep; |
196 |
> |
register EPNODE *ep; |
197 |
|
double aval; |
198 |
|
|
199 |
|
if (actp == NULL || --n < 0) { |
220 |
|
} |
221 |
|
|
222 |
|
|
230 |
– |
#ifdef VARIABLE |
223 |
|
VARDEF * |
224 |
|
argf(n) /* return function def for nth argument */ |
225 |
|
int n; |
255 |
|
eputs(actp->name); |
256 |
|
eputs(": argument not a function\n"); |
257 |
|
quit(1); |
258 |
+ |
return NULL; /* pro forma return */ |
259 |
|
} |
260 |
|
|
261 |
|
|
265 |
|
{ |
266 |
|
return(argf(n)->name); |
267 |
|
} |
275 |
– |
#endif |
268 |
|
|
269 |
|
|
270 |
|
double |
318 |
|
} |
319 |
|
|
320 |
|
|
329 |
– |
#ifndef VARIABLE |
330 |
– |
VARDEF * |
331 |
– |
varinsert(vname) /* dummy variable insert */ |
332 |
– |
char *vname; |
333 |
– |
{ |
334 |
– |
register VARDEF *vp; |
335 |
– |
|
336 |
– |
vp = (VARDEF *)emalloc(sizeof(VARDEF)); |
337 |
– |
vp->name = savestr(vname); |
338 |
– |
vp->nlinks = 1; |
339 |
– |
vp->def = NULL; |
340 |
– |
vp->lib = NULL; |
341 |
– |
vp->next = NULL; |
342 |
– |
return(vp); |
343 |
– |
} |
344 |
– |
|
345 |
– |
|
346 |
– |
varfree(vp) /* free dummy variable */ |
347 |
– |
register VARDEF *vp; |
348 |
– |
{ |
349 |
– |
freestr(vp->name); |
350 |
– |
efree((char *)vp); |
351 |
– |
} |
352 |
– |
#endif |
353 |
– |
|
354 |
– |
|
355 |
– |
|
321 |
|
/* |
322 |
|
* The following routines are for internal use: |
323 |
|
*/ |
326 |
|
static double |
327 |
|
libfunc(fname, vp) /* execute library function */ |
328 |
|
char *fname; |
329 |
< |
register VARDEF *vp; |
329 |
> |
VARDEF *vp; |
330 |
|
{ |
331 |
< |
VARDEF dumdef; |
331 |
> |
register LIBR *lp; |
332 |
|
double d; |
333 |
|
int lasterrno; |
334 |
|
|
335 |
< |
if (vp == NULL) { |
336 |
< |
vp = &dumdef; |
337 |
< |
vp->lib = NULL; |
338 |
< |
} |
339 |
< |
if (((vp->lib == NULL || strcmp(fname, vp->lib->fname)) && |
375 |
< |
(vp->lib = liblookup(fname)) == NULL) || |
376 |
< |
vp->lib->f == NULL) { |
335 |
> |
if (vp != NULL) |
336 |
> |
lp = vp->lib; |
337 |
> |
else |
338 |
> |
lp = liblookup(fname); |
339 |
> |
if (lp == NULL) { |
340 |
|
eputs(fname); |
341 |
|
eputs(": undefined function\n"); |
342 |
|
quit(1); |
343 |
|
} |
344 |
|
lasterrno = errno; |
345 |
|
errno = 0; |
346 |
< |
d = (*vp->lib->f)(vp->lib->fname); |
346 |
> |
d = (*lp->f)(lp->fname); |
347 |
|
#ifdef IEEE |
348 |
< |
if (!finite(d)) |
349 |
< |
errno = EDOM; |
348 |
> |
if (errno == 0) |
349 |
> |
if (isnan(d)) |
350 |
> |
errno = EDOM; |
351 |
> |
else if (isinf(d)) |
352 |
> |
errno = ERANGE; |
353 |
|
#endif |
354 |
< |
if (errno) { |
354 |
> |
if (errno == EDOM || errno == ERANGE) { |
355 |
|
wputs(fname); |
356 |
< |
wputs(": bad call\n"); |
356 |
> |
if (errno == EDOM) |
357 |
> |
wputs(": domain error\n"); |
358 |
> |
else if (errno == ERANGE) |
359 |
> |
wputs(": range error\n"); |
360 |
> |
else |
361 |
> |
wputs(": error in call\n"); |
362 |
|
return(0.0); |
363 |
|
} |
364 |
|
errno = lasterrno; |
387 |
|
{ |
388 |
|
register int n; |
389 |
|
|
390 |
< |
n = argument(1) + .5; |
390 |
> |
n = (int)(argument(1) + .5); |
391 |
|
if (n == 0) |
392 |
|
return(nargum()-1); |
393 |
|
if (n < 1 || n > nargum()-1) { |
401 |
|
static double |
402 |
|
l_rand() /* random function between 0 and 1 */ |
403 |
|
{ |
433 |
– |
extern double floor(); |
404 |
|
double x; |
405 |
|
|
406 |
|
x = argument(1); |
414 |
|
static double |
415 |
|
l_floor() /* return largest integer not greater than arg1 */ |
416 |
|
{ |
447 |
– |
extern double floor(); |
448 |
– |
|
417 |
|
return(floor(argument(1))); |
418 |
|
} |
419 |
|
|
421 |
|
static double |
422 |
|
l_ceil() /* return smallest integer not less than arg1 */ |
423 |
|
{ |
456 |
– |
extern double ceil(); |
457 |
– |
|
424 |
|
return(ceil(argument(1))); |
425 |
|
} |
426 |
|
|
427 |
|
|
462 |
– |
#ifdef BIGLIB |
428 |
|
static double |
429 |
|
l_sqrt() |
430 |
|
{ |
466 |
– |
extern double sqrt(); |
467 |
– |
|
431 |
|
return(sqrt(argument(1))); |
432 |
|
} |
433 |
|
|
435 |
|
static double |
436 |
|
l_sin() |
437 |
|
{ |
475 |
– |
extern double sin(); |
476 |
– |
|
438 |
|
return(sin(argument(1))); |
439 |
|
} |
440 |
|
|
442 |
|
static double |
443 |
|
l_cos() |
444 |
|
{ |
484 |
– |
extern double cos(); |
485 |
– |
|
445 |
|
return(cos(argument(1))); |
446 |
|
} |
447 |
|
|
449 |
|
static double |
450 |
|
l_tan() |
451 |
|
{ |
493 |
– |
extern double tan(); |
494 |
– |
|
452 |
|
return(tan(argument(1))); |
453 |
|
} |
454 |
|
|
456 |
|
static double |
457 |
|
l_asin() |
458 |
|
{ |
502 |
– |
extern double asin(); |
503 |
– |
|
459 |
|
return(asin(argument(1))); |
460 |
|
} |
461 |
|
|
463 |
|
static double |
464 |
|
l_acos() |
465 |
|
{ |
511 |
– |
extern double acos(); |
512 |
– |
|
466 |
|
return(acos(argument(1))); |
467 |
|
} |
468 |
|
|
470 |
|
static double |
471 |
|
l_atan() |
472 |
|
{ |
520 |
– |
extern double atan(); |
521 |
– |
|
473 |
|
return(atan(argument(1))); |
474 |
|
} |
475 |
|
|
477 |
|
static double |
478 |
|
l_atan2() |
479 |
|
{ |
529 |
– |
extern double atan2(); |
530 |
– |
|
480 |
|
return(atan2(argument(1), argument(2))); |
481 |
|
} |
482 |
|
|
484 |
|
static double |
485 |
|
l_exp() |
486 |
|
{ |
538 |
– |
extern double exp(); |
539 |
– |
|
487 |
|
return(exp(argument(1))); |
488 |
|
} |
489 |
|
|
491 |
|
static double |
492 |
|
l_log() |
493 |
|
{ |
547 |
– |
extern double log(); |
548 |
– |
|
494 |
|
return(log(argument(1))); |
495 |
|
} |
496 |
|
|
498 |
|
static double |
499 |
|
l_log10() |
500 |
|
{ |
556 |
– |
extern double log10(); |
557 |
– |
|
501 |
|
return(log10(argument(1))); |
502 |
|
} |
560 |
– |
#endif |