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> |
22 |
– |
|
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 */ |
44 |
– |
|
45 |
– |
static double l_if(), l_select(), l_rand(); |
46 |
– |
static double l_floor(), l_ceil(); |
47 |
– |
#ifdef BIGLIB |
48 |
– |
static double l_sqrt(); |
49 |
– |
static double l_sin(), l_cos(), l_tan(); |
50 |
– |
static double l_asin(), l_acos(), l_atan(), l_atan2(); |
51 |
– |
static double l_exp(), l_log(), l_log10(); |
41 |
|
#endif |
42 |
|
|
43 |
< |
#ifdef BIGLIB |
43 |
> |
static double l_if(char *), l_select(char *), l_rand(char *); |
44 |
> |
static double l_floor(char *), l_ceil(char *); |
45 |
> |
static double l_sqrt(char *); |
46 |
> |
static double l_sin(char *), l_cos(char *), l_tan(char *); |
47 |
> |
static double l_asin(char *), l_acos(char *), l_atan(char *), l_atan2(char *); |
48 |
> |
static double l_exp(char *), l_log(char *), l_log10(char *); |
49 |
> |
|
50 |
|
/* functions must be listed alphabetically */ |
51 |
|
static LIBR library[MAXLIB] = { |
52 |
|
{ "acos", 1, ':', l_acos }, |
69 |
|
|
70 |
|
static int libsize = 16; |
71 |
|
|
77 |
– |
#else |
78 |
– |
/* functions must be listed alphabetically */ |
79 |
– |
static LIBR library[MAXLIB] = { |
80 |
– |
{ "ceil", 1, ':', l_ceil }, |
81 |
– |
{ "floor", 1, ':', l_floor }, |
82 |
– |
{ "if", 3, ':', l_if }, |
83 |
– |
{ "rand", 1, ':', l_rand }, |
84 |
– |
{ "select", 1, ':', l_select }, |
85 |
– |
}; |
86 |
– |
|
87 |
– |
static int libsize = 5; |
88 |
– |
|
89 |
– |
#endif |
90 |
– |
|
91 |
– |
extern char *savestr(), *emalloc(); |
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; |
76 |
> |
fundefined( /* return # of arguments for function */ |
77 |
> |
char *fname |
78 |
> |
) |
79 |
|
{ |
80 |
|
LIBR *lp; |
81 |
< |
register VARDEF *vp; |
81 |
> |
VARDEF *vp; |
82 |
|
|
83 |
< |
if ((vp = varlookup(fname)) == NULL || vp->def == NULL |
84 |
< |
|| vp->def->v.kid->type != FUNC) |
112 |
< |
if ((lp = liblookup(fname)) == NULL) |
113 |
< |
return(0); |
114 |
< |
else |
115 |
< |
return(lp->nargs); |
116 |
< |
else |
83 |
> |
if ((vp = varlookup(fname)) != NULL && vp->def != NULL |
84 |
> |
&& vp->def->v.kid->type == FUNC) |
85 |
|
return(nekids(vp->def->v.kid) - 1); |
86 |
+ |
lp = vp != NULL ? vp->lib : liblookup(fname); |
87 |
+ |
if (lp == NULL) |
88 |
+ |
return(0); |
89 |
+ |
return(lp->nargs); |
90 |
|
} |
91 |
|
|
92 |
|
|
93 |
|
double |
94 |
< |
funvalue(fname, n, a) /* return a function value to the user */ |
95 |
< |
char *fname; |
96 |
< |
int n; |
97 |
< |
double *a; |
94 |
> |
funvalue( /* return a function value to the user */ |
95 |
> |
char *fname, |
96 |
> |
int n, |
97 |
> |
double *a |
98 |
> |
) |
99 |
|
{ |
100 |
|
ACTIVATION act; |
101 |
< |
register VARDEF *vp; |
101 |
> |
VARDEF *vp; |
102 |
|
double rval; |
103 |
|
/* push environment */ |
104 |
|
act.name = fname; |
122 |
|
} |
123 |
|
|
124 |
|
|
125 |
< |
funset(fname, nargs, assign, fptr) /* set a library function */ |
126 |
< |
char *fname; |
127 |
< |
int nargs; |
128 |
< |
int assign; |
129 |
< |
double (*fptr)(); |
125 |
> |
void |
126 |
> |
funset( /* set a library function */ |
127 |
> |
char *fname, |
128 |
> |
int nargs, |
129 |
> |
int assign, |
130 |
> |
double (*fptr)(char *) |
131 |
> |
) |
132 |
|
{ |
133 |
< |
register LIBR *lp; |
134 |
< |
|
133 |
> |
int oldlibsize = libsize; |
134 |
> |
char *cp; |
135 |
> |
LIBR *lp; |
136 |
> |
/* check for context */ |
137 |
> |
for (cp = fname; *cp; cp++) |
138 |
> |
; |
139 |
> |
if (cp == fname) |
140 |
> |
return; |
141 |
> |
if (cp[-1] == CNTXMARK) |
142 |
> |
*--cp = '\0'; |
143 |
|
if ((lp = liblookup(fname)) == NULL) { /* insert */ |
144 |
|
if (libsize >= MAXLIB) { |
145 |
|
eputs("Too many library functons!\n"); |
170 |
|
lp[0].atyp = assign; |
171 |
|
lp[0].f = fptr; |
172 |
|
} |
173 |
< |
libupdate(fname); /* relink library */ |
173 |
> |
if (libsize != oldlibsize) |
174 |
> |
libupdate(fname); /* relink library */ |
175 |
|
} |
176 |
|
|
177 |
|
|
178 |
|
int |
179 |
< |
nargum() /* return number of available arguments */ |
179 |
> |
nargum(void) /* return number of available arguments */ |
180 |
|
{ |
181 |
< |
register int n; |
181 |
> |
int n; |
182 |
|
|
183 |
|
if (curact == NULL) |
184 |
|
return(0); |
192 |
|
|
193 |
|
|
194 |
|
double |
195 |
< |
argument(n) /* return nth argument for active function */ |
212 |
< |
register int n; |
195 |
> |
argument(int n) /* return nth argument for active function */ |
196 |
|
{ |
197 |
< |
register ACTIVATION *actp = curact; |
198 |
< |
register EPNODE *ep; |
197 |
> |
ACTIVATION *actp = curact; |
198 |
> |
EPNODE *ep = NULL; |
199 |
|
double aval; |
200 |
|
|
201 |
|
if (actp == NULL || --n < 0) { |
222 |
|
} |
223 |
|
|
224 |
|
|
242 |
– |
#ifdef VARIABLE |
225 |
|
VARDEF * |
226 |
< |
argf(n) /* return function def for nth argument */ |
245 |
< |
int n; |
226 |
> |
argf(int n) /* return function def for nth argument */ |
227 |
|
{ |
228 |
< |
register ACTIVATION *actp; |
229 |
< |
register EPNODE *ep; |
228 |
> |
ACTIVATION *actp; |
229 |
> |
EPNODE *ep; |
230 |
|
|
231 |
|
for (actp = curact; actp != NULL; actp = actp->prev) { |
232 |
|
|
256 |
|
eputs(actp->name); |
257 |
|
eputs(": argument not a function\n"); |
258 |
|
quit(1); |
259 |
+ |
return NULL; /* pro forma return */ |
260 |
|
} |
261 |
|
|
262 |
|
|
263 |
|
char * |
264 |
< |
argfun(n) /* return function name for nth argument */ |
283 |
< |
int n; |
264 |
> |
argfun(int n) /* return function name for nth argument */ |
265 |
|
{ |
266 |
|
return(argf(n)->name); |
267 |
|
} |
287 |
– |
#endif |
268 |
|
|
269 |
|
|
270 |
|
double |
271 |
< |
efunc(ep) /* evaluate a function */ |
292 |
< |
register EPNODE *ep; |
271 |
> |
efunc(EPNODE *ep) /* evaluate a function */ |
272 |
|
{ |
273 |
|
ACTIVATION act; |
274 |
|
double alist[ALISTSIZ]; |
275 |
|
double rval; |
276 |
< |
register VARDEF *dp; |
276 |
> |
VARDEF *dp; |
277 |
|
/* push environment */ |
278 |
|
dp = resolve(ep->v.kid); |
279 |
|
act.name = dp->name; |
294 |
|
|
295 |
|
|
296 |
|
LIBR * |
297 |
< |
liblookup(fname) /* look up a library function */ |
319 |
< |
char *fname; |
297 |
> |
liblookup(char *fname) /* look up a library function */ |
298 |
|
{ |
299 |
|
int upper, lower; |
300 |
< |
register int cm, i; |
300 |
> |
int cm, i; |
301 |
|
|
302 |
|
lower = 0; |
303 |
|
upper = cm = libsize; |
316 |
|
} |
317 |
|
|
318 |
|
|
341 |
– |
#ifndef VARIABLE |
342 |
– |
static VARDEF *varlist = NULL; /* our list of dummy variables */ |
343 |
– |
|
344 |
– |
|
345 |
– |
VARDEF * |
346 |
– |
varinsert(vname) /* dummy variable insert */ |
347 |
– |
char *vname; |
348 |
– |
{ |
349 |
– |
register VARDEF *vp; |
350 |
– |
|
351 |
– |
vp = (VARDEF *)emalloc(sizeof(VARDEF)); |
352 |
– |
vp->name = savestr(vname); |
353 |
– |
vp->nlinks = 1; |
354 |
– |
vp->def = NULL; |
355 |
– |
vp->lib = liblookup(vname); |
356 |
– |
vp->next = varlist; |
357 |
– |
varlist = vp; |
358 |
– |
return(vp); |
359 |
– |
} |
360 |
– |
|
361 |
– |
|
362 |
– |
varfree(vp) /* free dummy variable */ |
363 |
– |
register VARDEF *vp; |
364 |
– |
{ |
365 |
– |
register VARDEF *vp2; |
366 |
– |
|
367 |
– |
if (vp == varlist) |
368 |
– |
varlist = vp->next; |
369 |
– |
else { |
370 |
– |
for (vp2 = varlist; vp2->next != vp; vp2 = vp2->next) |
371 |
– |
; |
372 |
– |
vp2->next = vp->next; |
373 |
– |
} |
374 |
– |
freestr(vp->name); |
375 |
– |
efree((char *)vp); |
376 |
– |
} |
377 |
– |
|
378 |
– |
|
379 |
– |
libupdate(nm) /* update library */ |
380 |
– |
char *nm; |
381 |
– |
{ |
382 |
– |
register VARDEF *vp; |
383 |
– |
|
384 |
– |
for (vp = varlist; vp != NULL; vp = vp->next) |
385 |
– |
vp->lib = liblookup(vp->name); |
386 |
– |
} |
387 |
– |
#endif |
388 |
– |
|
389 |
– |
|
390 |
– |
|
319 |
|
/* |
320 |
|
* The following routines are for internal use: |
321 |
|
*/ |
322 |
|
|
323 |
|
|
324 |
|
static double |
325 |
< |
libfunc(fname, vp) /* execute library function */ |
326 |
< |
char *fname; |
327 |
< |
VARDEF *vp; |
325 |
> |
libfunc( /* execute library function */ |
326 |
> |
char *fname, |
327 |
> |
VARDEF *vp |
328 |
> |
) |
329 |
|
{ |
330 |
< |
register LIBR *lp; |
330 |
> |
LIBR *lp; |
331 |
|
double d; |
332 |
|
int lasterrno; |
333 |
|
|
343 |
|
lasterrno = errno; |
344 |
|
errno = 0; |
345 |
|
d = (*lp->f)(lp->fname); |
346 |
< |
#ifdef IEEE |
346 |
> |
#ifdef isnan |
347 |
|
if (errno == 0) |
348 |
|
if (isnan(d)) |
349 |
|
errno = EDOM; |
350 |
|
else if (isinf(d)) |
351 |
|
errno = ERANGE; |
352 |
|
#endif |
353 |
< |
if (errno) { |
353 |
> |
if (errno == EDOM || errno == ERANGE) { |
354 |
|
wputs(fname); |
355 |
|
if (errno == EDOM) |
356 |
|
wputs(": domain error\n"); |
371 |
|
|
372 |
|
|
373 |
|
static double |
374 |
< |
l_if() /* if(cond, then, else) conditional expression */ |
374 |
> |
l_if(char *nm) /* if(cond, then, else) conditional expression */ |
375 |
|
/* cond evaluates true if greater than zero */ |
376 |
|
{ |
377 |
|
if (argument(1) > 0.0) |
382 |
|
|
383 |
|
|
384 |
|
static double |
385 |
< |
l_select() /* return argument #(A1+1) */ |
385 |
> |
l_select(char *nm) /* return argument #(A1+1) */ |
386 |
|
{ |
387 |
< |
register int n; |
387 |
> |
int n; |
388 |
|
|
389 |
< |
n = argument(1) + .5; |
389 |
> |
n = (int)(argument(1) + .5); |
390 |
|
if (n == 0) |
391 |
|
return(nargum()-1); |
392 |
|
if (n < 1 || n > nargum()-1) { |
398 |
|
|
399 |
|
|
400 |
|
static double |
401 |
< |
l_rand() /* random function between 0 and 1 */ |
401 |
> |
l_rand(char *nm) /* random function between 0 and 1 */ |
402 |
|
{ |
403 |
|
double x; |
404 |
|
|
411 |
|
|
412 |
|
|
413 |
|
static double |
414 |
< |
l_floor() /* return largest integer not greater than arg1 */ |
414 |
> |
l_floor(char *nm) /* return largest integer not greater than arg1 */ |
415 |
|
{ |
416 |
|
return(floor(argument(1))); |
417 |
|
} |
418 |
|
|
419 |
|
|
420 |
|
static double |
421 |
< |
l_ceil() /* return smallest integer not less than arg1 */ |
421 |
> |
l_ceil(char *nm) /* return smallest integer not less than arg1 */ |
422 |
|
{ |
423 |
|
return(ceil(argument(1))); |
424 |
|
} |
425 |
|
|
426 |
|
|
498 |
– |
#ifdef BIGLIB |
427 |
|
static double |
428 |
< |
l_sqrt() |
428 |
> |
l_sqrt(char *nm) |
429 |
|
{ |
430 |
|
return(sqrt(argument(1))); |
431 |
|
} |
432 |
|
|
433 |
|
|
434 |
|
static double |
435 |
< |
l_sin() |
435 |
> |
l_sin(char *nm) |
436 |
|
{ |
437 |
|
return(sin(argument(1))); |
438 |
|
} |
439 |
|
|
440 |
|
|
441 |
|
static double |
442 |
< |
l_cos() |
442 |
> |
l_cos(char *nm) |
443 |
|
{ |
444 |
|
return(cos(argument(1))); |
445 |
|
} |
446 |
|
|
447 |
|
|
448 |
|
static double |
449 |
< |
l_tan() |
449 |
> |
l_tan(char *nm) |
450 |
|
{ |
451 |
|
return(tan(argument(1))); |
452 |
|
} |
453 |
|
|
454 |
|
|
455 |
|
static double |
456 |
< |
l_asin() |
456 |
> |
l_asin(char *nm) |
457 |
|
{ |
458 |
|
return(asin(argument(1))); |
459 |
|
} |
460 |
|
|
461 |
|
|
462 |
|
static double |
463 |
< |
l_acos() |
463 |
> |
l_acos(char *nm) |
464 |
|
{ |
465 |
|
return(acos(argument(1))); |
466 |
|
} |
467 |
|
|
468 |
|
|
469 |
|
static double |
470 |
< |
l_atan() |
470 |
> |
l_atan(char *nm) |
471 |
|
{ |
472 |
|
return(atan(argument(1))); |
473 |
|
} |
474 |
|
|
475 |
|
|
476 |
|
static double |
477 |
< |
l_atan2() |
477 |
> |
l_atan2(char *nm) |
478 |
|
{ |
479 |
|
return(atan2(argument(1), argument(2))); |
480 |
|
} |
481 |
|
|
482 |
|
|
483 |
|
static double |
484 |
< |
l_exp() |
484 |
> |
l_exp(char *nm) |
485 |
|
{ |
486 |
|
return(exp(argument(1))); |
487 |
|
} |
488 |
|
|
489 |
|
|
490 |
|
static double |
491 |
< |
l_log() |
491 |
> |
l_log(char *nm) |
492 |
|
{ |
493 |
|
return(log(argument(1))); |
494 |
|
} |
495 |
|
|
496 |
|
|
497 |
|
static double |
498 |
< |
l_log10() |
498 |
> |
l_log10(char *nm) |
499 |
|
{ |
500 |
|
return(log10(argument(1))); |
501 |
|
} |
574 |
– |
#endif |