1 |
< |
/* Copyright (c) 1991 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
20 |
|
|
21 |
|
#include <errno.h> |
22 |
|
|
23 |
+ |
#include <math.h> |
24 |
+ |
|
25 |
|
#include "calcomp.h" |
26 |
|
|
27 |
|
/* bits in argument flag (better be right!) */ |
90 |
|
|
91 |
|
extern char *savestr(), *emalloc(); |
92 |
|
|
91 |
– |
extern LIBR *liblookup(); |
92 |
– |
|
93 |
|
extern VARDEF *argf(); |
94 |
|
|
95 |
|
#ifdef VARIABLE |
155 |
|
int assign; |
156 |
|
double (*fptr)(); |
157 |
|
{ |
158 |
+ |
int oldlibsize = libsize; |
159 |
|
register LIBR *lp; |
160 |
|
|
161 |
< |
if ((lp = liblookup(fname)) == NULL) { |
161 |
> |
if ((lp = liblookup(fname)) == NULL) { /* insert */ |
162 |
|
if (libsize >= MAXLIB) { |
163 |
|
eputs("Too many library functons!\n"); |
164 |
|
quit(1); |
173 |
|
break; |
174 |
|
libsize++; |
175 |
|
} |
176 |
< |
lp[0].fname = fname; /* must be static! */ |
177 |
< |
lp[0].nargs = nargs; |
178 |
< |
lp[0].atyp = assign; |
179 |
< |
lp[0].f = fptr; |
176 |
> |
if (fptr == NULL) { /* delete */ |
177 |
> |
while (lp < &library[libsize-1]) { |
178 |
> |
lp[0].fname = lp[1].fname; |
179 |
> |
lp[0].nargs = lp[1].nargs; |
180 |
> |
lp[0].atyp = lp[1].atyp; |
181 |
> |
lp[0].f = lp[1].f; |
182 |
> |
lp++; |
183 |
> |
} |
184 |
> |
libsize--; |
185 |
> |
} else { /* or assign */ |
186 |
> |
lp[0].fname = fname; /* string must be static! */ |
187 |
> |
lp[0].nargs = nargs; |
188 |
> |
lp[0].atyp = assign; |
189 |
> |
lp[0].f = fptr; |
190 |
> |
} |
191 |
> |
if (libsize != oldlibsize) |
192 |
> |
libupdate(fname); /* relink library */ |
193 |
|
} |
194 |
|
|
195 |
|
|
341 |
|
|
342 |
|
|
343 |
|
#ifndef VARIABLE |
344 |
+ |
static VARDEF *varlist = NULL; /* our list of dummy variables */ |
345 |
+ |
|
346 |
+ |
|
347 |
|
VARDEF * |
348 |
|
varinsert(vname) /* dummy variable insert */ |
349 |
|
char *vname; |
354 |
|
vp->name = savestr(vname); |
355 |
|
vp->nlinks = 1; |
356 |
|
vp->def = NULL; |
357 |
< |
vp->lib = NULL; |
358 |
< |
vp->next = NULL; |
357 |
> |
vp->lib = liblookup(vname); |
358 |
> |
vp->next = varlist; |
359 |
> |
varlist = vp; |
360 |
|
return(vp); |
361 |
|
} |
362 |
|
|
364 |
|
varfree(vp) /* free dummy variable */ |
365 |
|
register VARDEF *vp; |
366 |
|
{ |
367 |
+ |
register VARDEF *vp2; |
368 |
+ |
|
369 |
+ |
if (vp == varlist) |
370 |
+ |
varlist = vp->next; |
371 |
+ |
else { |
372 |
+ |
for (vp2 = varlist; vp2->next != vp; vp2 = vp2->next) |
373 |
+ |
; |
374 |
+ |
vp2->next = vp->next; |
375 |
+ |
} |
376 |
|
freestr(vp->name); |
377 |
|
efree((char *)vp); |
378 |
|
} |
379 |
+ |
|
380 |
+ |
|
381 |
+ |
libupdate(nm) /* update library */ |
382 |
+ |
char *nm; |
383 |
+ |
{ |
384 |
+ |
register VARDEF *vp; |
385 |
+ |
|
386 |
+ |
for (vp = varlist; vp != NULL; vp = vp->next) |
387 |
+ |
vp->lib = liblookup(vp->name); |
388 |
+ |
} |
389 |
|
#endif |
390 |
|
|
391 |
|
|
398 |
|
static double |
399 |
|
libfunc(fname, vp) /* execute library function */ |
400 |
|
char *fname; |
401 |
< |
register VARDEF *vp; |
401 |
> |
VARDEF *vp; |
402 |
|
{ |
403 |
< |
VARDEF dumdef; |
403 |
> |
register LIBR *lp; |
404 |
|
double d; |
405 |
|
int lasterrno; |
406 |
|
|
407 |
< |
if (vp == NULL) { |
408 |
< |
vp = &dumdef; |
409 |
< |
vp->lib = NULL; |
410 |
< |
} |
411 |
< |
if (((vp->lib == NULL || strcmp(fname, vp->lib->fname)) && |
375 |
< |
(vp->lib = liblookup(fname)) == NULL) || |
376 |
< |
vp->lib->f == NULL) { |
407 |
> |
if (vp != NULL) |
408 |
> |
lp = vp->lib; |
409 |
> |
else |
410 |
> |
lp = liblookup(fname); |
411 |
> |
if (lp == NULL) { |
412 |
|
eputs(fname); |
413 |
|
eputs(": undefined function\n"); |
414 |
|
quit(1); |
415 |
|
} |
416 |
|
lasterrno = errno; |
417 |
|
errno = 0; |
418 |
< |
d = (*vp->lib->f)(vp->lib->fname); |
418 |
> |
d = (*lp->f)(lp->fname); |
419 |
|
#ifdef IEEE |
420 |
|
if (errno == 0) |
421 |
|
if (isnan(d)) |
473 |
|
static double |
474 |
|
l_rand() /* random function between 0 and 1 */ |
475 |
|
{ |
441 |
– |
extern double floor(); |
476 |
|
double x; |
477 |
|
|
478 |
|
x = argument(1); |
486 |
|
static double |
487 |
|
l_floor() /* return largest integer not greater than arg1 */ |
488 |
|
{ |
455 |
– |
extern double floor(); |
456 |
– |
|
489 |
|
return(floor(argument(1))); |
490 |
|
} |
491 |
|
|
493 |
|
static double |
494 |
|
l_ceil() /* return smallest integer not less than arg1 */ |
495 |
|
{ |
464 |
– |
extern double ceil(); |
465 |
– |
|
496 |
|
return(ceil(argument(1))); |
497 |
|
} |
498 |
|
|
501 |
|
static double |
502 |
|
l_sqrt() |
503 |
|
{ |
474 |
– |
extern double sqrt(); |
475 |
– |
|
504 |
|
return(sqrt(argument(1))); |
505 |
|
} |
506 |
|
|
508 |
|
static double |
509 |
|
l_sin() |
510 |
|
{ |
483 |
– |
extern double sin(); |
484 |
– |
|
511 |
|
return(sin(argument(1))); |
512 |
|
} |
513 |
|
|
515 |
|
static double |
516 |
|
l_cos() |
517 |
|
{ |
492 |
– |
extern double cos(); |
493 |
– |
|
518 |
|
return(cos(argument(1))); |
519 |
|
} |
520 |
|
|
522 |
|
static double |
523 |
|
l_tan() |
524 |
|
{ |
501 |
– |
extern double tan(); |
502 |
– |
|
525 |
|
return(tan(argument(1))); |
526 |
|
} |
527 |
|
|
529 |
|
static double |
530 |
|
l_asin() |
531 |
|
{ |
510 |
– |
extern double asin(); |
511 |
– |
|
532 |
|
return(asin(argument(1))); |
533 |
|
} |
534 |
|
|
536 |
|
static double |
537 |
|
l_acos() |
538 |
|
{ |
519 |
– |
extern double acos(); |
520 |
– |
|
539 |
|
return(acos(argument(1))); |
540 |
|
} |
541 |
|
|
543 |
|
static double |
544 |
|
l_atan() |
545 |
|
{ |
528 |
– |
extern double atan(); |
529 |
– |
|
546 |
|
return(atan(argument(1))); |
547 |
|
} |
548 |
|
|
550 |
|
static double |
551 |
|
l_atan2() |
552 |
|
{ |
537 |
– |
extern double atan2(); |
538 |
– |
|
553 |
|
return(atan2(argument(1), argument(2))); |
554 |
|
} |
555 |
|
|
557 |
|
static double |
558 |
|
l_exp() |
559 |
|
{ |
546 |
– |
extern double exp(); |
547 |
– |
|
560 |
|
return(exp(argument(1))); |
561 |
|
} |
562 |
|
|
564 |
|
static double |
565 |
|
l_log() |
566 |
|
{ |
555 |
– |
extern double log(); |
556 |
– |
|
567 |
|
return(log(argument(1))); |
568 |
|
} |
569 |
|
|
571 |
|
static double |
572 |
|
l_log10() |
573 |
|
{ |
564 |
– |
extern double log10(); |
565 |
– |
|
574 |
|
return(log10(argument(1))); |
575 |
|
} |
576 |
|
#endif |