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 */ |
42 |
– |
|
43 |
– |
static double l_if(), l_select(), l_rand(); |
44 |
– |
static double l_floor(), l_ceil(); |
45 |
– |
#ifdef BIGLIB |
46 |
– |
static double l_sqrt(); |
47 |
– |
static double l_sin(), l_cos(), l_tan(); |
48 |
– |
static double l_asin(), l_acos(), l_atan(), l_atan2(); |
49 |
– |
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 |
|
|
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 VARDEF *argf(); |
92 |
– |
|
93 |
– |
#ifdef VARIABLE |
72 |
|
#define resolve(ep) ((ep)->type==VAR?(ep)->v.ln:argf((ep)->v.chan)) |
95 |
– |
#else |
96 |
– |
#define resolve(ep) ((ep)->v.ln) |
97 |
– |
#define varlookup(name) NULL |
98 |
– |
#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) |
110 |
< |
if ((lp = liblookup(fname)) == NULL) |
111 |
< |
return(0); |
112 |
< |
else |
113 |
< |
return(lp->nargs); |
114 |
< |
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"); |
146 |
|
quit(1); |
147 |
|
} |
148 |
|
for (lp = &library[libsize]; lp > library; lp--) |
149 |
< |
if (strcmp(lp[-1].fname, fname) > 0) { |
150 |
< |
lp[0].fname = lp[-1].fname; |
151 |
< |
lp[0].nargs = lp[-1].nargs; |
167 |
< |
lp[0].atyp = lp[-1].atyp; |
168 |
< |
lp[0].f = lp[-1].f; |
169 |
< |
} else |
149 |
> |
if (strcmp(lp[-1].fname, fname) > 0) |
150 |
> |
lp[0] = lp[-1]; |
151 |
> |
else |
152 |
|
break; |
153 |
|
libsize++; |
154 |
|
} |
155 |
|
if (fptr == NULL) { /* delete */ |
156 |
|
while (lp < &library[libsize-1]) { |
157 |
< |
lp[0].fname = lp[1].fname; |
176 |
< |
lp[0].nargs = lp[1].nargs; |
177 |
< |
lp[0].atyp = lp[1].atyp; |
178 |
< |
lp[0].f = lp[1].f; |
157 |
> |
lp[0] = lp[1]; |
158 |
|
lp++; |
159 |
|
} |
160 |
|
libsize--; |
164 |
|
lp[0].atyp = assign; |
165 |
|
lp[0].f = fptr; |
166 |
|
} |
167 |
< |
libupdate(fname); /* relink library */ |
167 |
> |
if (libsize != oldlibsize) |
168 |
> |
libupdate(fname); /* relink library */ |
169 |
|
} |
170 |
|
|
171 |
|
|
172 |
|
int |
173 |
< |
nargum() /* return number of available arguments */ |
173 |
> |
nargum(void) /* return number of available arguments */ |
174 |
|
{ |
175 |
< |
register int n; |
175 |
> |
int n; |
176 |
|
|
177 |
|
if (curact == NULL) |
178 |
|
return(0); |
186 |
|
|
187 |
|
|
188 |
|
double |
189 |
< |
argument(n) /* return nth argument for active function */ |
210 |
< |
register int n; |
189 |
> |
argument(int n) /* return nth argument for active function */ |
190 |
|
{ |
191 |
< |
register ACTIVATION *actp = curact; |
192 |
< |
register EPNODE *ep; |
191 |
> |
ACTIVATION *actp = curact; |
192 |
> |
EPNODE *ep = NULL; |
193 |
|
double aval; |
194 |
|
|
195 |
|
if (actp == NULL || --n < 0) { |
216 |
|
} |
217 |
|
|
218 |
|
|
240 |
– |
#ifdef VARIABLE |
219 |
|
VARDEF * |
220 |
< |
argf(n) /* return function def for nth argument */ |
243 |
< |
int n; |
220 |
> |
argf(int n) /* return function def for nth argument */ |
221 |
|
{ |
222 |
< |
register ACTIVATION *actp; |
223 |
< |
register EPNODE *ep; |
222 |
> |
ACTIVATION *actp; |
223 |
> |
EPNODE *ep; |
224 |
|
|
225 |
|
for (actp = curact; actp != NULL; actp = actp->prev) { |
226 |
|
|
250 |
|
eputs(actp->name); |
251 |
|
eputs(": argument not a function\n"); |
252 |
|
quit(1); |
253 |
+ |
return NULL; /* pro forma return */ |
254 |
|
} |
255 |
|
|
256 |
|
|
257 |
|
char * |
258 |
< |
argfun(n) /* return function name for nth argument */ |
281 |
< |
int n; |
258 |
> |
argfun(int n) /* return function name for nth argument */ |
259 |
|
{ |
260 |
|
return(argf(n)->name); |
261 |
|
} |
285 |
– |
#endif |
262 |
|
|
263 |
|
|
264 |
|
double |
265 |
< |
efunc(ep) /* evaluate a function */ |
290 |
< |
register EPNODE *ep; |
265 |
> |
efunc(EPNODE *ep) /* evaluate a function */ |
266 |
|
{ |
267 |
|
ACTIVATION act; |
268 |
|
double alist[ALISTSIZ]; |
269 |
|
double rval; |
270 |
< |
register VARDEF *dp; |
270 |
> |
VARDEF *dp; |
271 |
|
/* push environment */ |
272 |
|
dp = resolve(ep->v.kid); |
273 |
|
act.name = dp->name; |
288 |
|
|
289 |
|
|
290 |
|
LIBR * |
291 |
< |
liblookup(fname) /* look up a library function */ |
317 |
< |
char *fname; |
291 |
> |
liblookup(char *fname) /* look up a library function */ |
292 |
|
{ |
293 |
|
int upper, lower; |
294 |
< |
register int cm, i; |
294 |
> |
int cm, i; |
295 |
|
|
296 |
|
lower = 0; |
297 |
|
upper = cm = libsize; |
310 |
|
} |
311 |
|
|
312 |
|
|
339 |
– |
#ifndef VARIABLE |
340 |
– |
VARDEF * |
341 |
– |
varinsert(vname) /* dummy variable insert */ |
342 |
– |
char *vname; |
343 |
– |
{ |
344 |
– |
register VARDEF *vp; |
345 |
– |
|
346 |
– |
vp = (VARDEF *)emalloc(sizeof(VARDEF)); |
347 |
– |
vp->name = savestr(vname); |
348 |
– |
vp->nlinks = 1; |
349 |
– |
vp->def = NULL; |
350 |
– |
vp->lib = NULL; |
351 |
– |
vp->next = NULL; |
352 |
– |
return(vp); |
353 |
– |
} |
354 |
– |
|
355 |
– |
|
356 |
– |
varfree(vp) /* free dummy variable */ |
357 |
– |
register VARDEF *vp; |
358 |
– |
{ |
359 |
– |
freestr(vp->name); |
360 |
– |
efree((char *)vp); |
361 |
– |
} |
362 |
– |
#endif |
363 |
– |
|
364 |
– |
|
365 |
– |
|
313 |
|
/* |
314 |
|
* The following routines are for internal use: |
315 |
|
*/ |
316 |
|
|
317 |
|
|
318 |
|
static double |
319 |
< |
libfunc(fname, vp) /* execute library function */ |
320 |
< |
char *fname; |
321 |
< |
register VARDEF *vp; |
319 |
> |
libfunc( /* execute library function */ |
320 |
> |
char *fname, |
321 |
> |
VARDEF *vp |
322 |
> |
) |
323 |
|
{ |
324 |
< |
VARDEF dumdef; |
324 |
> |
LIBR *lp; |
325 |
|
double d; |
326 |
|
int lasterrno; |
327 |
|
|
328 |
< |
if (vp == NULL) { |
329 |
< |
vp = &dumdef; |
330 |
< |
vp->lib = liblookup(fname); |
331 |
< |
} |
332 |
< |
if (vp->lib == NULL) { |
328 |
> |
if (vp != NULL) |
329 |
> |
lp = vp->lib; |
330 |
> |
else |
331 |
> |
lp = liblookup(fname); |
332 |
> |
if (lp == NULL) { |
333 |
|
eputs(fname); |
334 |
|
eputs(": undefined function\n"); |
335 |
|
quit(1); |
336 |
|
} |
337 |
|
lasterrno = errno; |
338 |
|
errno = 0; |
339 |
< |
d = (*vp->lib->f)(vp->lib->fname); |
340 |
< |
#ifdef IEEE |
339 |
> |
d = (*lp->f)(lp->fname); |
340 |
> |
#ifdef isnan |
341 |
|
if (errno == 0) |
342 |
|
if (isnan(d)) |
343 |
|
errno = EDOM; |
344 |
|
else if (isinf(d)) |
345 |
|
errno = ERANGE; |
346 |
|
#endif |
347 |
< |
if (errno) { |
347 |
> |
if (errno == EDOM || errno == ERANGE) { |
348 |
|
wputs(fname); |
349 |
|
if (errno == EDOM) |
350 |
|
wputs(": domain error\n"); |
365 |
|
|
366 |
|
|
367 |
|
static double |
368 |
< |
l_if() /* if(cond, then, else) conditional expression */ |
368 |
> |
l_if(char *nm) /* if(cond, then, else) conditional expression */ |
369 |
|
/* cond evaluates true if greater than zero */ |
370 |
|
{ |
371 |
|
if (argument(1) > 0.0) |
376 |
|
|
377 |
|
|
378 |
|
static double |
379 |
< |
l_select() /* return argument #(A1+1) */ |
379 |
> |
l_select(char *nm) /* return argument #(A1+1) */ |
380 |
|
{ |
381 |
< |
register int n; |
381 |
> |
int n; |
382 |
|
|
383 |
< |
n = argument(1) + .5; |
383 |
> |
n = (int)(argument(1) + .5); |
384 |
|
if (n == 0) |
385 |
|
return(nargum()-1); |
386 |
|
if (n < 1 || n > nargum()-1) { |
392 |
|
|
393 |
|
|
394 |
|
static double |
395 |
< |
l_rand() /* random function between 0 and 1 */ |
395 |
> |
l_rand(char *nm) /* random function between 0 and 1 */ |
396 |
|
{ |
449 |
– |
extern double floor(); |
397 |
|
double x; |
398 |
|
|
399 |
|
x = argument(1); |
405 |
|
|
406 |
|
|
407 |
|
static double |
408 |
< |
l_floor() /* return largest integer not greater than arg1 */ |
408 |
> |
l_floor(char *nm) /* return largest integer not greater than arg1 */ |
409 |
|
{ |
463 |
– |
extern double floor(); |
464 |
– |
|
410 |
|
return(floor(argument(1))); |
411 |
|
} |
412 |
|
|
413 |
|
|
414 |
|
static double |
415 |
< |
l_ceil() /* return smallest integer not less than arg1 */ |
415 |
> |
l_ceil(char *nm) /* return smallest integer not less than arg1 */ |
416 |
|
{ |
472 |
– |
extern double ceil(); |
473 |
– |
|
417 |
|
return(ceil(argument(1))); |
418 |
|
} |
419 |
|
|
420 |
|
|
478 |
– |
#ifdef BIGLIB |
421 |
|
static double |
422 |
< |
l_sqrt() |
422 |
> |
l_sqrt(char *nm) |
423 |
|
{ |
482 |
– |
extern double sqrt(); |
483 |
– |
|
424 |
|
return(sqrt(argument(1))); |
425 |
|
} |
426 |
|
|
427 |
|
|
428 |
|
static double |
429 |
< |
l_sin() |
429 |
> |
l_sin(char *nm) |
430 |
|
{ |
491 |
– |
extern double sin(); |
492 |
– |
|
431 |
|
return(sin(argument(1))); |
432 |
|
} |
433 |
|
|
434 |
|
|
435 |
|
static double |
436 |
< |
l_cos() |
436 |
> |
l_cos(char *nm) |
437 |
|
{ |
500 |
– |
extern double cos(); |
501 |
– |
|
438 |
|
return(cos(argument(1))); |
439 |
|
} |
440 |
|
|
441 |
|
|
442 |
|
static double |
443 |
< |
l_tan() |
443 |
> |
l_tan(char *nm) |
444 |
|
{ |
509 |
– |
extern double tan(); |
510 |
– |
|
445 |
|
return(tan(argument(1))); |
446 |
|
} |
447 |
|
|
448 |
|
|
449 |
|
static double |
450 |
< |
l_asin() |
450 |
> |
l_asin(char *nm) |
451 |
|
{ |
518 |
– |
extern double asin(); |
519 |
– |
|
452 |
|
return(asin(argument(1))); |
453 |
|
} |
454 |
|
|
455 |
|
|
456 |
|
static double |
457 |
< |
l_acos() |
457 |
> |
l_acos(char *nm) |
458 |
|
{ |
527 |
– |
extern double acos(); |
528 |
– |
|
459 |
|
return(acos(argument(1))); |
460 |
|
} |
461 |
|
|
462 |
|
|
463 |
|
static double |
464 |
< |
l_atan() |
464 |
> |
l_atan(char *nm) |
465 |
|
{ |
536 |
– |
extern double atan(); |
537 |
– |
|
466 |
|
return(atan(argument(1))); |
467 |
|
} |
468 |
|
|
469 |
|
|
470 |
|
static double |
471 |
< |
l_atan2() |
471 |
> |
l_atan2(char *nm) |
472 |
|
{ |
545 |
– |
extern double atan2(); |
546 |
– |
|
473 |
|
return(atan2(argument(1), argument(2))); |
474 |
|
} |
475 |
|
|
476 |
|
|
477 |
|
static double |
478 |
< |
l_exp() |
478 |
> |
l_exp(char *nm) |
479 |
|
{ |
554 |
– |
extern double exp(); |
555 |
– |
|
480 |
|
return(exp(argument(1))); |
481 |
|
} |
482 |
|
|
483 |
|
|
484 |
|
static double |
485 |
< |
l_log() |
485 |
> |
l_log(char *nm) |
486 |
|
{ |
563 |
– |
extern double log(); |
564 |
– |
|
487 |
|
return(log(argument(1))); |
488 |
|
} |
489 |
|
|
490 |
|
|
491 |
|
static double |
492 |
< |
l_log10() |
492 |
> |
l_log10(char *nm) |
493 |
|
{ |
572 |
– |
extern double log10(); |
573 |
– |
|
494 |
|
return(log10(argument(1))); |
495 |
|
} |
576 |
– |
#endif |