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 |
157 |
|
{ |
158 |
|
register LIBR *lp; |
159 |
|
|
160 |
< |
if ((lp = liblookup(fname)) == NULL) { |
160 |
> |
if ((lp = liblookup(fname)) == NULL) { /* insert */ |
161 |
|
if (libsize >= MAXLIB) { |
162 |
|
eputs("Too many library functons!\n"); |
163 |
|
quit(1); |
172 |
|
break; |
173 |
|
libsize++; |
174 |
|
} |
175 |
< |
lp[0].fname = fname; /* must be static! */ |
176 |
< |
lp[0].nargs = nargs; |
177 |
< |
lp[0].atyp = assign; |
178 |
< |
lp[0].f = fptr; |
175 |
> |
if (fptr == NULL) { /* delete */ |
176 |
> |
while (lp < &library[libsize-1]) { |
177 |
> |
lp[0].fname = lp[1].fname; |
178 |
> |
lp[0].nargs = lp[1].nargs; |
179 |
> |
lp[0].atyp = lp[1].atyp; |
180 |
> |
lp[0].f = lp[1].f; |
181 |
> |
lp++; |
182 |
> |
} |
183 |
> |
libsize--; |
184 |
> |
} else { /* or assign */ |
185 |
> |
lp[0].fname = fname; /* string must be static! */ |
186 |
> |
lp[0].nargs = nargs; |
187 |
> |
lp[0].atyp = assign; |
188 |
> |
lp[0].f = fptr; |
189 |
> |
} |
190 |
> |
libupdate(fname); /* relink library */ |
191 |
|
} |
192 |
|
|
193 |
|
|
339 |
|
|
340 |
|
|
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; |
352 |
|
vp->name = savestr(vname); |
353 |
|
vp->nlinks = 1; |
354 |
|
vp->def = NULL; |
355 |
< |
vp->lib = NULL; |
356 |
< |
vp->next = NULL; |
355 |
> |
vp->lib = liblookup(vname); |
356 |
> |
vp->next = varlist; |
357 |
> |
varlist = vp; |
358 |
|
return(vp); |
359 |
|
} |
360 |
|
|
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 |
|
|
396 |
|
static double |
397 |
|
libfunc(fname, vp) /* execute library function */ |
398 |
|
char *fname; |
399 |
< |
register VARDEF *vp; |
399 |
> |
VARDEF *vp; |
400 |
|
{ |
401 |
< |
VARDEF dumdef; |
401 |
> |
register LIBR *lp; |
402 |
|
double d; |
403 |
|
int lasterrno; |
404 |
|
|
405 |
< |
if (vp == NULL) { |
406 |
< |
vp = &dumdef; |
407 |
< |
vp->lib = NULL; |
408 |
< |
} |
409 |
< |
if (((vp->lib == NULL || strcmp(fname, vp->lib->fname)) && |
375 |
< |
(vp->lib = liblookup(fname)) == NULL) || |
376 |
< |
vp->lib->f == NULL) { |
405 |
> |
if (vp != NULL) |
406 |
> |
lp = vp->lib; |
407 |
> |
else |
408 |
> |
lp = liblookup(fname); |
409 |
> |
if (lp == NULL) { |
410 |
|
eputs(fname); |
411 |
|
eputs(": undefined function\n"); |
412 |
|
quit(1); |
413 |
|
} |
414 |
|
lasterrno = errno; |
415 |
|
errno = 0; |
416 |
< |
d = (*vp->lib->f)(vp->lib->fname); |
416 |
> |
d = (*lp->f)(lp->fname); |
417 |
|
#ifdef IEEE |
418 |
|
if (errno == 0) |
419 |
|
if (isnan(d)) |
471 |
|
static double |
472 |
|
l_rand() /* random function between 0 and 1 */ |
473 |
|
{ |
441 |
– |
extern double floor(); |
474 |
|
double x; |
475 |
|
|
476 |
|
x = argument(1); |
484 |
|
static double |
485 |
|
l_floor() /* return largest integer not greater than arg1 */ |
486 |
|
{ |
455 |
– |
extern double floor(); |
456 |
– |
|
487 |
|
return(floor(argument(1))); |
488 |
|
} |
489 |
|
|
491 |
|
static double |
492 |
|
l_ceil() /* return smallest integer not less than arg1 */ |
493 |
|
{ |
464 |
– |
extern double ceil(); |
465 |
– |
|
494 |
|
return(ceil(argument(1))); |
495 |
|
} |
496 |
|
|
499 |
|
static double |
500 |
|
l_sqrt() |
501 |
|
{ |
474 |
– |
extern double sqrt(); |
475 |
– |
|
502 |
|
return(sqrt(argument(1))); |
503 |
|
} |
504 |
|
|
506 |
|
static double |
507 |
|
l_sin() |
508 |
|
{ |
483 |
– |
extern double sin(); |
484 |
– |
|
509 |
|
return(sin(argument(1))); |
510 |
|
} |
511 |
|
|
513 |
|
static double |
514 |
|
l_cos() |
515 |
|
{ |
492 |
– |
extern double cos(); |
493 |
– |
|
516 |
|
return(cos(argument(1))); |
517 |
|
} |
518 |
|
|
520 |
|
static double |
521 |
|
l_tan() |
522 |
|
{ |
501 |
– |
extern double tan(); |
502 |
– |
|
523 |
|
return(tan(argument(1))); |
524 |
|
} |
525 |
|
|
527 |
|
static double |
528 |
|
l_asin() |
529 |
|
{ |
510 |
– |
extern double asin(); |
511 |
– |
|
530 |
|
return(asin(argument(1))); |
531 |
|
} |
532 |
|
|
534 |
|
static double |
535 |
|
l_acos() |
536 |
|
{ |
519 |
– |
extern double acos(); |
520 |
– |
|
537 |
|
return(acos(argument(1))); |
538 |
|
} |
539 |
|
|
541 |
|
static double |
542 |
|
l_atan() |
543 |
|
{ |
528 |
– |
extern double atan(); |
529 |
– |
|
544 |
|
return(atan(argument(1))); |
545 |
|
} |
546 |
|
|
548 |
|
static double |
549 |
|
l_atan2() |
550 |
|
{ |
537 |
– |
extern double atan2(); |
538 |
– |
|
551 |
|
return(atan2(argument(1), argument(2))); |
552 |
|
} |
553 |
|
|
555 |
|
static double |
556 |
|
l_exp() |
557 |
|
{ |
546 |
– |
extern double exp(); |
547 |
– |
|
558 |
|
return(exp(argument(1))); |
559 |
|
} |
560 |
|
|
562 |
|
static double |
563 |
|
l_log() |
564 |
|
{ |
555 |
– |
extern double log(); |
556 |
– |
|
565 |
|
return(log(argument(1))); |
566 |
|
} |
567 |
|
|
569 |
|
static double |
570 |
|
l_log10() |
571 |
|
{ |
564 |
– |
extern double log10(); |
565 |
– |
|
572 |
|
return(log10(argument(1))); |
573 |
|
} |
574 |
|
#endif |