--- ray/src/common/calfunc.c 1992/05/15 16:38:47 2.2 +++ ray/src/common/calfunc.c 2003/11/14 17:22:06 2.14 @@ -1,25 +1,23 @@ -/* Copyright (c) 1991 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: calfunc.c,v 2.14 2003/11/14 17:22:06 schorsch Exp $"; #endif - /* * calfunc.c - routines for calcomp using functions. * - * The define BIGLIB pulls in a large number of the - * available math routines. - * - * If VARIABLE is not defined, only library functions + * If VARIABLE is not set, only library functions * can be accessed. * - * 4/2/86 + * 2/19/03 Eliminated conditional compiles in favor of esupport extern. */ -#include +#include "copyright.h" +#include +#include #include +#include +#include "rterror.h" #include "calcomp.h" /* bits in argument flag (better be right!) */ @@ -36,20 +34,19 @@ typedef struct activation { static ACTIVATION *curact = NULL; -static double libfunc(); +static double libfunc(char *fname, VARDEF *vp); +#ifndef MAXLIB #define MAXLIB 64 /* maximum number of library functions */ - -static double l_if(), l_select(), l_rand(); -static double l_floor(), l_ceil(); -#ifdef BIGLIB -static double l_sqrt(); -static double l_sin(), l_cos(), l_tan(); -static double l_asin(), l_acos(), l_atan(), l_atan2(); -static double l_exp(), l_log(), l_log10(); #endif -#ifdef BIGLIB +static double l_if(char *), l_select(char *), l_rand(char *); +static double l_floor(char *), l_ceil(char *); +static double l_sqrt(char *); +static double l_sin(char *), l_cos(char *), l_tan(char *); +static double l_asin(char *), l_acos(char *), l_atan(char *), l_atan2(char *); +static double l_exp(char *), l_log(char *), l_log10(char *); + /* functions must be listed alphabetically */ static LIBR library[MAXLIB] = { { "acos", 1, ':', l_acos }, @@ -72,47 +69,23 @@ static LIBR library[MAXLIB] = { static int libsize = 16; -#else - /* functions must be listed alphabetically */ -static LIBR library[MAXLIB] = { - { "ceil", 1, ':', l_ceil }, - { "floor", 1, ':', l_floor }, - { "if", 3, ':', l_if }, - { "rand", 1, ':', l_rand }, - { "select", 1, ':', l_select }, -}; - -static int libsize = 5; - -#endif - -extern char *savestr(), *emalloc(); - -extern VARDEF *argf(); - -#ifdef VARIABLE #define resolve(ep) ((ep)->type==VAR?(ep)->v.ln:argf((ep)->v.chan)) -#else -#define resolve(ep) ((ep)->v.ln) -#define varlookup(name) NULL -#endif int fundefined(fname) /* return # of arguments for function */ char *fname; { - LIBR *lp; + register LIBR *lp; register VARDEF *vp; - if ((vp = varlookup(fname)) == NULL || vp->def == NULL - || vp->def->v.kid->type != FUNC) - if ((lp = liblookup(fname)) == NULL) - return(0); - else - return(lp->nargs); - else + if ((vp = varlookup(fname)) != NULL && vp->def != NULL + && vp->def->v.kid->type == FUNC) return(nekids(vp->def->v.kid) - 1); + lp = vp != NULL ? vp->lib : liblookup(fname); + if (lp == NULL) + return(0); + return(lp->nargs); } @@ -147,14 +120,23 @@ double *a; } +void funset(fname, nargs, assign, fptr) /* set a library function */ char *fname; int nargs; int assign; -double (*fptr)(); +double (*fptr)(char *); { + int oldlibsize = libsize; + char *cp; register LIBR *lp; - + /* check for context */ + for (cp = fname; *cp; cp++) + ; + if (cp == fname) + return; + if (cp[-1] == CNTXMARK) + *--cp = '\0'; if ((lp = liblookup(fname)) == NULL) { /* insert */ if (libsize >= MAXLIB) { eputs("Too many library functons!\n"); @@ -185,7 +167,8 @@ double (*fptr)(); lp[0].atyp = assign; lp[0].f = fptr; } - libupdate(fname); /* relink library */ + if (libsize != oldlibsize) + libupdate(fname); /* relink library */ } @@ -210,7 +193,7 @@ argument(n) /* return nth argument for active functi register int n; { register ACTIVATION *actp = curact; - register EPNODE *ep; + register EPNODE *ep = NULL; double aval; if (actp == NULL || --n < 0) { @@ -237,7 +220,6 @@ register int n; } -#ifdef VARIABLE VARDEF * argf(n) /* return function def for nth argument */ int n; @@ -273,6 +255,7 @@ badarg: eputs(actp->name); eputs(": argument not a function\n"); quit(1); + return NULL; /* pro forma return */ } @@ -282,7 +265,6 @@ int n; { return(argf(n)->name); } -#endif double @@ -336,33 +318,6 @@ char *fname; } -#ifndef VARIABLE -VARDEF * -varinsert(vname) /* dummy variable insert */ -char *vname; -{ - register VARDEF *vp; - - vp = (VARDEF *)emalloc(sizeof(VARDEF)); - vp->name = savestr(vname); - vp->nlinks = 1; - vp->def = NULL; - vp->lib = NULL; - vp->next = NULL; - return(vp); -} - - -varfree(vp) /* free dummy variable */ -register VARDEF *vp; -{ - freestr(vp->name); - efree((char *)vp); -} -#endif - - - /* * The following routines are for internal use: */ @@ -371,24 +326,24 @@ register VARDEF *vp; static double libfunc(fname, vp) /* execute library function */ char *fname; -register VARDEF *vp; +VARDEF *vp; { - VARDEF dumdef; + register LIBR *lp; double d; int lasterrno; - if (vp == NULL) { - vp = &dumdef; - vp->lib = liblookup(fname); - } - if (vp->lib == NULL) { + if (vp != NULL) + lp = vp->lib; + else + lp = liblookup(fname); + if (lp == NULL) { eputs(fname); eputs(": undefined function\n"); quit(1); } lasterrno = errno; errno = 0; - d = (*vp->lib->f)(vp->lib->fname); + d = (*lp->f)(lp->fname); #ifdef IEEE if (errno == 0) if (isnan(d)) @@ -396,7 +351,7 @@ register VARDEF *vp; else if (isinf(d)) errno = ERANGE; #endif - if (errno) { + if (errno == EDOM || errno == ERANGE) { wputs(fname); if (errno == EDOM) wputs(": domain error\n"); @@ -417,7 +372,7 @@ register VARDEF *vp; static double -l_if() /* if(cond, then, else) conditional expression */ +l_if(char *nm) /* if(cond, then, else) conditional expression */ /* cond evaluates true if greater than zero */ { if (argument(1) > 0.0) @@ -428,11 +383,11 @@ l_if() /* if(cond, then, else) conditional expressio static double -l_select() /* return argument #(A1+1) */ +l_select(char *nm) /* return argument #(A1+1) */ { register int n; - n = argument(1) + .5; + n = (int)(argument(1) + .5); if (n == 0) return(nargum()-1); if (n < 1 || n > nargum()-1) { @@ -444,9 +399,8 @@ l_select() /* return argument #(A1+1) */ static double -l_rand() /* random function between 0 and 1 */ +l_rand(char *nm) /* random function between 0 and 1 */ { - extern double floor(); double x; x = argument(1); @@ -458,119 +412,91 @@ l_rand() /* random function between 0 and 1 */ static double -l_floor() /* return largest integer not greater than arg1 */ +l_floor(char *nm) /* return largest integer not greater than arg1 */ { - extern double floor(); - return(floor(argument(1))); } static double -l_ceil() /* return smallest integer not less than arg1 */ +l_ceil(char *nm) /* return smallest integer not less than arg1 */ { - extern double ceil(); - return(ceil(argument(1))); } -#ifdef BIGLIB static double -l_sqrt() +l_sqrt(char *nm) { - extern double sqrt(); - return(sqrt(argument(1))); } static double -l_sin() +l_sin(char *nm) { - extern double sin(); - return(sin(argument(1))); } static double -l_cos() +l_cos(char *nm) { - extern double cos(); - return(cos(argument(1))); } static double -l_tan() +l_tan(char *nm) { - extern double tan(); - return(tan(argument(1))); } static double -l_asin() +l_asin(char *nm) { - extern double asin(); - return(asin(argument(1))); } static double -l_acos() +l_acos(char *nm) { - extern double acos(); - return(acos(argument(1))); } static double -l_atan() +l_atan(char *nm) { - extern double atan(); - return(atan(argument(1))); } static double -l_atan2() +l_atan2(char *nm) { - extern double atan2(); - return(atan2(argument(1), argument(2))); } static double -l_exp() +l_exp(char *nm) { - extern double exp(); - return(exp(argument(1))); } static double -l_log() +l_log(char *nm) { - extern double log(); - return(log(argument(1))); } static double -l_log10() +l_log10(char *nm) { - extern double log10(); - return(log10(argument(1))); } -#endif