diff options
Diffstat (limited to 'src/extnfunc.c')
-rw-r--r-- | src/extnfunc.c | 212 |
1 files changed, 129 insertions, 83 deletions
diff --git a/src/extnfunc.c b/src/extnfunc.c index a87f24f..c0996ab 100644 --- a/src/extnfunc.c +++ b/src/extnfunc.c @@ -1,7 +1,7 @@ /*******************************************************/ /* "C" Language Integrated Production System */ /* */ - /* CLIPS Version 6.24 06/02/06 */ + /* CLIPS Version 6.30 08/16/14 */ /* */ /* EXTERNAL FUNCTION MODULE */ /*******************************************************/ @@ -14,13 +14,23 @@ /* Gary D. Riley */ /* */ /* Contributing Programmer(s): */ -/* Brian L. Donnell */ +/* Brian L. Dantes */ /* */ /* Revision History: */ /* */ /* 6.24: Corrected code to remove run-time program */ /* compiler warning. */ /* */ +/* 6.30: Added support for passing context information */ +/* to user defined functions. */ +/* */ +/* Support for long long integers. */ +/* */ +/* Added const qualifiers to remove C++ */ +/* deprecation warnings. */ +/* */ +/* Converted API macros to function calls. */ +/* */ /*************************************************************/ #define _EXTNFUNC_SOURCE_ @@ -83,7 +93,7 @@ static void DeallocateExternalFunctionData( if (ExternalFunctionData(theEnv)->FunctionHashtable == NULL) { return; } - + for (i = 0; i < SIZE_FUNCTION_HASH; i++) { fhPtr = ExternalFunctionData(theEnv)->FunctionHashtable[i]; @@ -94,83 +104,71 @@ static void DeallocateExternalFunctionData( fhPtr = nextFHPtr; } } - + genfree(theEnv,ExternalFunctionData(theEnv)->FunctionHashtable, (int) sizeof (struct FunctionHash *) * SIZE_FUNCTION_HASH); } #if (! RUN_TIME) -/************************************************************/ -/* DefineFunction: Used to define a system or user external */ -/* function so that the KB can access it. */ -/************************************************************/ -#if (! ENVIRONMENT_API_ONLY) && ALLOW_ENVIRONMENT_GLOBALS -globle int DefineFunction( - char *name, +/******************************************************/ +/* EnvDefineFunction: Used to define a system or user */ +/* external function so that the KB can access it. */ +/******************************************************/ +globle int EnvDefineFunction( + void *theEnv, + const char *name, int returnType, - int (*pointer)(void), - char *actualName) + int (*pointer)(void *), + const char *actualName) { - void *theEnv; - - theEnv = GetCurrentEnvironment(); - - return(DefineFunction3(theEnv,name,returnType, - (int (*)(void *)) pointer, - actualName,NULL,FALSE)); + return(DefineFunction3(theEnv,name,returnType,pointer,actualName,NULL,TRUE,NULL)); } -#endif -/***************************************************************/ -/* EnvDefineFunction: Used to define a system or user external */ -/* function so that the KB can access it. */ -/***************************************************************/ -globle int EnvDefineFunction( +/************************************************************/ +/* EnvDefineFunctionWithContext: Used to define a system or */ +/* user external function so that the KB can access it. */ +/************************************************************/ +globle int EnvDefineFunctionWithContext( void *theEnv, - char *name, + const char *name, int returnType, int (*pointer)(void *), - char *actualName) + const char *actualName, + void *context) { - return(DefineFunction3(theEnv,name,returnType,pointer,actualName,NULL,TRUE)); + return(DefineFunction3(theEnv,name,returnType,pointer,actualName,NULL,TRUE,context)); } - -/*************************************************************/ -/* DefineFunction2: Used to define a system or user external */ -/* function so that the KB can access it. */ -/*************************************************************/ -#if (! ENVIRONMENT_API_ONLY) && ALLOW_ENVIRONMENT_GLOBALS -globle int DefineFunction2( - char *name, + +/*******************************************************/ +/* EnvDefineFunction2: Used to define a system or user */ +/* external function so that the KB can access it. */ +/*******************************************************/ +globle int EnvDefineFunction2( + void *theEnv, + const char *name, int returnType, - int (*pointer)(void), - char *actualName, - char *restrictions) + int (*pointer)(void *), + const char *actualName, + const char *restrictions) { - void *theEnv; - - theEnv = GetCurrentEnvironment(); - - return(DefineFunction3(theEnv,name,returnType, - (int (*)(void *)) pointer, - actualName,restrictions,FALSE)); + return(DefineFunction3(theEnv,name,returnType,pointer,actualName,restrictions,TRUE,NULL)); } -#endif - + /*************************************************************/ -/* EnvDefineFunction2: Used to define a system or user external */ -/* function so that the KB can access it. */ +/* EnvDefineFunction2WithContext: Used to define a system or */ +/* user external function so that the KB can access it. */ /*************************************************************/ -globle int EnvDefineFunction2( +globle int EnvDefineFunction2WithContext( void *theEnv, - char *name, + const char *name, int returnType, int (*pointer)(void *), - char *actualName, - char *restrictions) + const char *actualName, + const char *restrictions, + void *context) { - return(DefineFunction3(theEnv,name,returnType,pointer,actualName,restrictions,TRUE)); + return(DefineFunction3(theEnv,name,returnType,pointer,actualName,restrictions,TRUE,context)); } /*************************************************************/ @@ -183,11 +181,12 @@ globle int EnvDefineFunction2( /* c - character (converted to symbol) */ /* d - double precision float */ /* f - single precision float (converted to double) */ -/* i - integer (converted to long integer) */ +/* g - long long integer */ +/* i - integer (converted to long long integer) */ /* j - unknown (symbol, string, */ /* or instance name by convention) */ /* k - unknown (symbol or string by convention) */ -/* l - long integer */ +/* l - long integer (converted to long long integer) */ /* m - unknown (multifield by convention) */ /* n - unknown (integer or float by convention) */ /* o - instance name */ @@ -199,12 +198,13 @@ globle int EnvDefineFunction2( /*************************************************************/ globle int DefineFunction3( void *theEnv, - char *name, + const char *name, int returnType, int (*pointer)(void *), - char *actualName, - char *restrictions, - intBool environmentAware) + const char *actualName, + const char *restrictions, + intBool environmentAware, + void *context) { struct FunctionDefinition *newFunction; @@ -213,6 +213,7 @@ globle int DefineFunction3( (returnType != 'c') && (returnType != 'd') && (returnType != 'f') && + (returnType != 'g') && (returnType != 'i') && (returnType != 'j') && (returnType != 'k') && @@ -228,6 +229,9 @@ globle int DefineFunction3( #if OBJECT_SYSTEM (returnType != 'x') && #endif +#if DEFTEMPLATE_CONSTRUCT + (returnType != 'y') && +#endif (returnType != 'w') ) { return(0); } @@ -241,7 +245,7 @@ globle int DefineFunction3( ExternalFunctionData(theEnv)->ListOfFunctions = newFunction; AddHashFunction(theEnv,newFunction); } - + newFunction->returnValueType = (char) returnType; newFunction->functionPointer = (int (*)(void)) pointer; newFunction->actualFunctionName = actualName; @@ -258,17 +262,18 @@ globle int DefineFunction3( newFunction->sequenceuseok = TRUE; newFunction->environmentAware = (short) environmentAware; newFunction->usrData = NULL; + newFunction->context = context; return(1); } - + /***********************************************/ /* UndefineFunction: Used to remove a function */ /* definition from the list of functions. */ /***********************************************/ globle int UndefineFunction( void *theEnv, - char *functionName) + const char *functionName) { SYMBOL_HN *findValue; struct FunctionDefinition *fPtr, *lastPtr = NULL; @@ -288,7 +293,7 @@ globle int UndefineFunction( { ExternalFunctionData(theEnv)->ListOfFunctions = fPtr->next; } else { lastPtr->next = fPtr->next; } - + ClearUserDataList(theEnv,fPtr->usrData); rtn_struct(theEnv,FunctionDefinition,fPtr); return(TRUE); @@ -345,8 +350,8 @@ static int RemoveHashFunction( /***************************************************************************/ globle int AddFunctionParser( void *theEnv, - char *functionName, - struct expr *(*fpPtr)(void *,struct expr *,char *)) + const char *functionName, + struct expr *(*fpPtr)(void *,struct expr *,const char *)) { struct FunctionDefinition *fdPtr; @@ -369,7 +374,7 @@ globle int AddFunctionParser( /*********************************************************************/ globle int RemoveFunctionParser( void *theEnv, - char *functionName) + const char *functionName) { struct FunctionDefinition *fdPtr; @@ -391,7 +396,7 @@ globle int RemoveFunctionParser( /*****************************************************************/ globle int FuncSeqOvlFlags( void *theEnv, - char *functionName, + const char *functionName, int seqp, int ovlp) { @@ -414,7 +419,7 @@ globle int FuncSeqOvlFlags( /* GetArgumentTypeName: Returns a descriptive string for */ /* a function argument type (used by DefineFunction2). */ /*********************************************************/ -globle char *GetArgumentTypeName( +globle const char *GetArgumentTypeName( int theRestriction) { switch ((char) theRestriction) @@ -477,9 +482,6 @@ globle char *GetArgumentTypeName( case 'u': return("non-void return value"); - - default: - break; } return("unknown argument type"); @@ -590,14 +592,14 @@ globle void InstallFunctionList( /********************************************************/ globle struct FunctionDefinition *FindFunction( void *theEnv, - char *functionName) + const char *functionName) { struct FunctionHash *fhPtr; unsigned hashValue; SYMBOL_HN *findValue; if (ExternalFunctionData(theEnv)->FunctionHashtable == NULL) return(NULL); - + hashValue = HashSymbol(functionName,SIZE_FUNCTION_HASH); findValue = (SYMBOL_HN *) FindSymbolHN(theEnv,functionName); @@ -658,7 +660,8 @@ static void AddHashFunction( globle int GetMinimumArgs( struct FunctionDefinition *theFunction) { - char theChar[2], *restrictions; + char theChar[2]; + const char *restrictions; restrictions = theFunction->restrictions; if (restrictions == NULL) return(-1); @@ -670,10 +673,10 @@ globle int GetMinimumArgs( { return atoi(theChar); } else if (theChar[0] == '*') { return(-1); } - - return(-1); + + return(-1); } - + /*************************************************/ /* GetMaximumArgs: Returns the maximum number of */ /* arguments expected by an external function. */ @@ -681,7 +684,8 @@ globle int GetMinimumArgs( globle int GetMaximumArgs( struct FunctionDefinition *theFunction) { - char theChar[2], *restrictions; + char theChar[2]; + const char *restrictions; restrictions = theFunction->restrictions; if (restrictions == NULL) return(-1); @@ -694,6 +698,48 @@ globle int GetMaximumArgs( { return atoi(theChar); } else if (theChar[0] == '*') { return(-1); } - - return(-1); + + return(-1); } + +/*#####################################*/ +/* ALLOW_ENVIRONMENT_GLOBALS Functions */ +/*#####################################*/ + +#if ALLOW_ENVIRONMENT_GLOBALS + +#if (! RUN_TIME) +globle int DefineFunction( + const char *name, + int returnType, + int (*pointer)(void), + const char *actualName) + { + void *theEnv; + + theEnv = GetCurrentEnvironment(); + + return(DefineFunction3(theEnv,name,returnType, + (int (*)(void *)) pointer, + actualName,NULL,FALSE,NULL)); + } + +globle int DefineFunction2( + const char *name, + int returnType, + int (*pointer)(void), + const char *actualName, + const char *restrictions) + { + void *theEnv; + + theEnv = GetCurrentEnvironment(); + + return(DefineFunction3(theEnv,name,returnType, + (int (*)(void *)) pointer, + actualName,restrictions,FALSE,NULL)); + } + +#endif /* (! RUN_TIME) */ + +#endif /* ALLOW_ENVIRONMENT_GLOBALS */ |