| 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 "calcomp.h" |
| 35 |
|
|
| 36 |
|
static double libfunc(); |
| 37 |
|
|
| 38 |
+ |
#ifndef MAXLIB |
| 39 |
|
#define MAXLIB 64 /* maximum number of library functions */ |
| 40 |
+ |
#endif |
| 41 |
|
|
| 42 |
|
static double l_if(), l_select(), l_rand(); |
| 43 |
|
static double l_floor(), l_ceil(); |
| 47 |
– |
#ifdef BIGLIB |
| 44 |
|
static double l_sqrt(); |
| 45 |
|
static double l_sin(), l_cos(), l_tan(); |
| 46 |
|
static double l_asin(), l_acos(), l_atan(), l_atan2(); |
| 47 |
|
static double l_exp(), l_log(), l_log10(); |
| 52 |
– |
#endif |
| 48 |
|
|
| 54 |
– |
#ifdef BIGLIB |
| 49 |
|
/* functions must be listed alphabetically */ |
| 50 |
|
static LIBR library[MAXLIB] = { |
| 51 |
|
{ "acos", 1, ':', l_acos }, |
| 68 |
|
|
| 69 |
|
static int libsize = 16; |
| 70 |
|
|
| 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 |
| 71 |
|
#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 |
| 72 |
|
|
| 73 |
|
|
| 74 |
|
int |
| 75 |
|
fundefined(fname) /* return # of arguments for function */ |
| 76 |
|
char *fname; |
| 77 |
|
{ |
| 78 |
< |
LIBR *lp; |
| 78 |
> |
register LIBR *lp; |
| 79 |
|
register VARDEF *vp; |
| 80 |
|
|
| 81 |
< |
if ((vp = varlookup(fname)) == NULL || vp->def == NULL |
| 82 |
< |
|| vp->def->v.kid->type != FUNC) |
| 112 |
< |
if ((lp = liblookup(fname)) == NULL) |
| 113 |
< |
return(0); |
| 114 |
< |
else |
| 115 |
< |
return(lp->nargs); |
| 116 |
< |
else |
| 81 |
> |
if ((vp = varlookup(fname)) != NULL && vp->def != NULL |
| 82 |
> |
&& vp->def->v.kid->type == FUNC) |
| 83 |
|
return(nekids(vp->def->v.kid) - 1); |
| 84 |
+ |
lp = vp != NULL ? vp->lib : liblookup(fname); |
| 85 |
+ |
if (lp == NULL) |
| 86 |
+ |
return(0); |
| 87 |
+ |
return(lp->nargs); |
| 88 |
|
} |
| 89 |
|
|
| 90 |
|
|
| 119 |
|
} |
| 120 |
|
|
| 121 |
|
|
| 122 |
+ |
void |
| 123 |
|
funset(fname, nargs, assign, fptr) /* set a library function */ |
| 124 |
|
char *fname; |
| 125 |
|
int nargs; |
| 126 |
|
int assign; |
| 127 |
|
double (*fptr)(); |
| 128 |
|
{ |
| 129 |
+ |
int oldlibsize = libsize; |
| 130 |
+ |
char *cp; |
| 131 |
|
register LIBR *lp; |
| 132 |
< |
|
| 132 |
> |
/* check for context */ |
| 133 |
> |
for (cp = fname; *cp; cp++) |
| 134 |
> |
; |
| 135 |
> |
if (cp == fname) |
| 136 |
> |
return; |
| 137 |
> |
if (cp[-1] == CNTXMARK) |
| 138 |
> |
*--cp = '\0'; |
| 139 |
|
if ((lp = liblookup(fname)) == NULL) { /* insert */ |
| 140 |
|
if (libsize >= MAXLIB) { |
| 141 |
|
eputs("Too many library functons!\n"); |
| 166 |
|
lp[0].atyp = assign; |
| 167 |
|
lp[0].f = fptr; |
| 168 |
|
} |
| 169 |
< |
libupdate(fname); /* relink library */ |
| 169 |
> |
if (libsize != oldlibsize) |
| 170 |
> |
libupdate(fname); /* relink library */ |
| 171 |
|
} |
| 172 |
|
|
| 173 |
|
|
| 219 |
|
} |
| 220 |
|
|
| 221 |
|
|
| 242 |
– |
#ifdef VARIABLE |
| 222 |
|
VARDEF * |
| 223 |
|
argf(n) /* return function def for nth argument */ |
| 224 |
|
int n; |
| 254 |
|
eputs(actp->name); |
| 255 |
|
eputs(": argument not a function\n"); |
| 256 |
|
quit(1); |
| 257 |
+ |
return NULL; /* pro forma return */ |
| 258 |
|
} |
| 259 |
|
|
| 260 |
|
|
| 264 |
|
{ |
| 265 |
|
return(argf(n)->name); |
| 266 |
|
} |
| 287 |
– |
#endif |
| 267 |
|
|
| 268 |
|
|
| 269 |
|
double |
| 317 |
|
} |
| 318 |
|
|
| 319 |
|
|
| 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 |
– |
|
| 320 |
|
/* |
| 321 |
|
* The following routines are for internal use: |
| 322 |
|
*/ |
| 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"); |
| 386 |
|
{ |
| 387 |
|
register 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) { |
| 424 |
|
} |
| 425 |
|
|
| 426 |
|
|
| 498 |
– |
#ifdef BIGLIB |
| 427 |
|
static double |
| 428 |
|
l_sqrt() |
| 429 |
|
{ |
| 499 |
|
{ |
| 500 |
|
return(log10(argument(1))); |
| 501 |
|
} |
| 574 |
– |
#endif |